Skip to contentSkip to conversation

VESA, EDID & flat panels

PATCH Superseded Benjamin Herrenschmidt 8 participants 19 messages Oct 3, 2005 – Jun 12, 2020 2 messages not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi !

Somebody around here has access to the VESA specs ? I'm trying to figure out if the EDID block, at least on recent flat panels, tells us wether the panel digital input is 6:6:6 or 8:8:8 ... If it's 6:6:6, we should enable dithering in drivers like radeon and "nv", while if 8:8:8, we should not. If not enabling it on a 6 bits panel, we end up with crappy looking gradients, if enabling it on a 8 bits panel, we basically end up sending a 6 bits signal to a 8 bits panel, thus reducing the quality.

Ben.

#1 lore raw
Quoted 7 lines

Somebody around here has access to the VESA specs ? I'm trying to figure out if the EDID block, at least on recent flat panels, tells us wether the panel digital input is 6:6:6 or 8:8:8 ... If it's 6:6:6, we should enable dithering in drivers like radeon and "nv", while if 8:8:8, we should not. If not enabling it on a 6 bits panel, we end up with crappy looking gradients, if enabling it on a 8 bits panel, we basically end up sending a 6 bits signal to a 8 bits panel, thus reducing the quality.

We have a copy from a few years ago. Its not too descriptive if IIRC. I'll try to look at it tomorrow.

--
Richard A. Smith

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

#2 lore raw
Quoted 10 lines · Richard Smith
Quoted 7 lines

Somebody around here has access to the VESA specs ? I'm trying to figure out if the EDID block, at least on recent flat panels, tells us wether the panel digital input is 6:6:6 or 8:8:8 ... If it's 6:6:6, we should enable dithering in drivers like radeon and "nv", while if 8:8:8, we should not. If not enabling it on a 6 bits panel, we end up with crappy looking gradients, if enabling it on a 8 bits panel, we basically end up sending a 6 bits signal to a 8 bits panel, thus reducing the quality.

We have a copy from a few years ago. Its not too descriptive if IIRC. I'll try to look at it tomorrow.

I found a lot of stuff on VESA Public site in fact. Looks like we really need to write some infrastructure to get to the EDID extensions :) They contain all sort of useful infos, though unfortunately, all of the panels I've come accross so far didn't seem to implement them =P

Ben.

#3 lore raw
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 7 lines · Richard Smith
Quoted 7 lines

Somebody around here has access to the VESA specs ? I'm trying to figure out if the EDID block, at least on recent flat panels, tells us wether the panel digital input is 6:6:6 or 8:8:8 ... If it's 6:6:6, we should enable dithering in drivers like radeon and "nv", while if 8:8:8, we should not. If not enabling it on a 6 bits panel, we end up with crappy looking gradients, if enabling it on a 8 bits panel, we basically end up sending a 6 bits signal to a 8 bits panel, thus reducing the quality.

Quoted 8 lines
Quoted 2 lines

We have a copy from a few years ago. Its not too descriptive if IIRC. I'll try to look at it tomorrow.

I found a lot of stuff on VESA Public site in fact. Looks like we really need to write some infrastructure to get to the EDID extensions :) They contain all sort of useful infos, though unfortunately, all of the panels I've come accross so far didn't seem to implement them =P

It think it's because the (basic) EDID is retrieved with (basic) DDC (address 0x50). If I'm not mistaken, E-EDID is retrieved with E-DDC (address 0xa0). I don't exactly know the protocol (E-DDDC specs are not freely available), but try probing your i2c busses with:

i2cdump 0,1,2,etc 0xa0

and see if you get anything.

Tony

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

#4 lore raw
Quoted 9 lines

It think it's because the (basic) EDID is retrieved with (basic) DDC (address 0x50). If I'm not mistaken, E-EDID is retrieved with E-DDC (address 0xa0). I don't exactly know the protocol (E-DDDC specs are not freely available), but try probing your i2c busses with:

i2cdump 0,1,2,etc 0xa0

and see if you get anything.

Are you sure we aren't talking about the same address here ? Address 0xa0 _is_ 0x50 shifted one bit left ... (The low bit of an i2c address is the RW bit). i2cdump takes shifted addresses, thus you can't pass it anything above 0x7f, thus passing it 0x50 is what gives you address 0xa0 :)

Ben.

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

#5 lore raw
Quoted 7 lines · Benjamin Herrenschmidt
Quoted 7 lines

It think it's because the (basic) EDID is retrieved with (basic) DDC (address 0x50). If I'm not mistaken, E-EDID is retrieved with E-DDC (address 0xa0). I don't exactly know the protocol (E-DDDC specs are not freely available), but try probing your i2c busses with:

i2cdump 0,1,2,etc 0xa0

Quoted 8 lines
Quoted 1 line

and see if you get anything.

Are you sure we aren't talking about the same address here ? Address 0xa0 _is_ 0x50 shifted one bit left ... (The low bit of an i2c address is the RW bit). i2cdump takes shifted addresses, thus you can't pass it anything above 0x7f, thus passing it 0x50 is what gives you address 0xa0 :)

Yeah, I just realized that :-)

Tony

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

#6 lore raw
In reply to Benjamin Herrenschmidt
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 8 lines

It think it's because the (basic) EDID is retrieved with (basic) DDC (address 0x50). If I'm not mistaken, E-EDID is retrieved with E-DDC (address 0xa0). I don't exactly know the protocol (E-DDDC specs are not freely available), but try probing your i2c busses with:

i2cdump 0,1,2,etc 0xa0

and see if you get anything.

Quoted 10 lines

Are you sure we aren't talking about the same address here ? Address 0xa0 _is_ 0x50 shifted one bit left ... (The low bit of an i2c address is the RW bit). i2cdump takes shifted addresses, thus you can't pass it anything above 0x7f, thus passing it 0x50 is what gives you address 0xa0 :)

Ben.

Here's a snippet from the HDMI specs. Not sure how helpful this is

"8.4.3 Segment pointer
Enhanced DDC allows access of up to 32 Kbytes of data. This is accomplished using a combination of the 0xA0/0xA1 address pair and a segment pointer. For each value of the segment pointer, 256 bytes of data are available at the 0xA0/0xA1 address pair. An unspecified segment pointer references the same data as when the segment pointer is zero. Each successive value of the segment pointer allows access to the next two blocks of E-EDID (128 bytes each). The value of the segment pointer register cannot be read since it is reset at the completion of each command."

Tony

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

#7 lore raw
Quoted 12 lines

Here's a snippet from the HDMI specs. Not sure how helpful this is

"8.4.3 Segment pointer
Enhanced DDC allows access of up to 32 Kbytes of data. This is accomplished using a combination of the 0xA0/0xA1 address pair and a segment pointer. For each value of the segment pointer, 256 bytes of data are available at the 0xA0/0xA1 address pair. An unspecified segment pointer references the same data as when the segment pointer is zero. Each successive value of the segment pointer allows access to the next two blocks of E-EDID (128 bytes each). The value of the segment pointer register cannot be read since it is reset at the completion of each command."

Yes, I have that. The problem is that the "primary" (old style) EDID block is supposed to contain in byte 0x7e, a value that indicates if there are extended stuffs available... Actually, it should contain:

0 - no extended infos
1 - one more 128 bytes extension (basically to be read from the same
0xa0 address, as each i2c address can actually return up to 256 bytes of data)
3 - more extension blocks. The EEDID spec then talks about a "map" of
the segments but I'm not sure where that is to be found.

The problem is that so far, all EDID blocks I've seen have this bit clear. Also, on the couple of machines i've tested, the additional 128 bytes at address 0xa0 aren't there neither.

So I wonder how widespread really is that EEDID stuff... I'm afraid the reason a lot of monitors come with a windows install CD is that they do _not_ provide those infos via EDID, but windows has a database with per-monitor infos (as Apple actually has in OS X) and we should probably do something similar. (but not in the kernel)

Ben.

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

#8 lore raw
Quoted 5 lines

So I wonder how widespread really is that EEDID stuff... I'm afraid the reason a lot of monitors come with a windows install CD is that they do _not_ provide those infos via EDID, but windows has a database with per-monitor infos (as Apple actually has in OS X) and we should probably do something similar. (but not in the kernel)

Our experience agrees with you that the EEDID stuff is not well used. We make some LCD driver products and I'm pretty sure we didn't include the EEDID info.

I gave the specs a quick look and I don't see anything in (thats obvious) there that would allow you to see if the panel is 666 or 888. The specs are somewhat less than clear in several cases so I'll be happy to review any sections closer if you think one might be a possibility.

--
Richard A. Smith

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

#9 lore raw
Quoted 10 lines · Richard Smith
Quoted 5 lines

So I wonder how widespread really is that EEDID stuff... I'm afraid the reason a lot of monitors come with a windows install CD is that they do _not_ provide those infos via EDID, but windows has a database with per-monitor infos (as Apple actually has in OS X) and we should probably do something similar. (but not in the kernel)

Our experience agrees with you that the EEDID stuff is not well used. We make some LCD driver products and I'm pretty sure we didn't include the EEDID info.

Quoted 5 lines

I gave the specs a quick look and I don't see anything in (thats obvious) there that would allow you to see if the panel is 666 or 888. The specs are somewhat less than clear in several cases so I'll be happy to review any sections closer if you think one might be a possibility.

Look at the EDID EXTENSIONS spec. Extension 0x40 contains the panel component bit definition iirc. I found so far only one monitor that implemented that extended block (the Apple 23" Cinema HD display) but the content seem to be junk =P

Ben.

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

#10 lore raw
In reply to Benjamin Herrenschmidt
Quoted 16 lines · Benjamin Herrenschmidt
Quoted 11 lines

Here's a snippet from the HDMI specs. Not sure how helpful this is

"8.4.3 Segment pointer
Enhanced DDC allows access of up to 32 Kbytes of data. This is accomplished using a combination of the 0xA0/0xA1 address pair and a segment pointer. For each value of the segment pointer, 256 bytes of data are available at the 0xA0/0xA1 address pair. An unspecified segment pointer references the same data as when the segment pointer is zero. Each successive value of the segment pointer allows access to the next two blocks of E-EDID (128 bytes each). The value of the segment pointer register cannot be read since it is reset at the completion of each command."

Yes, I have that. The problem is that the "primary" (old style) EDID block is supposed to contain in byte 0x7e, a value that indicates if there are extended stuffs available... Actually, it should contain:

Quoted 6 lines

0 - no extended infos
1 - one more 128 bytes extension (basically to be read from the same
0xa0 address, as each i2c address can actually return up to 256 bytes of data)
3 - more extension blocks. The EEDID spec then talks about a "map" of
the segments but I'm not sure where that is to be found.

One of the specs describes this map, should not be too difficult to parse.

Quoted 5 lines

The problem is that so far, all EDID blocks I've seen have this bit clear. Also, on the couple of machines i've tested, the additional 128 bytes at address 0xa0 aren't there neither.

Me too. I've never encountered a display with extended blocks. I had plans to write a parser for these blocks, but their rarity makes it not worth the effort.

Quoted 5 lines

So I wonder how widespread really is that EEDID stuff... I'm afraid the reason a lot of monitors come with a windows install CD is that they do _not_ provide those infos via EDID, but windows has a database with per-monitor infos (as Apple actually has in OS X) and we should probably do something similar. (but not in the kernel)

Yes, I agree.

Tony

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

#11 lore raw
In reply to Benjamin Herrenschmidt
Quoted 10 lines · Benjamin Herrenschmidt

Hi !

Somebody around here has access to the VESA specs ? I'm trying to figure out if the EDID block, at least on recent flat panels, tells us wether the panel digital input is 6:6:6 or 8:8:8 ... If it's 6:6:6, we should enable dithering in drivers like radeon and "nv", while if 8:8:8, we should not. If not enabling it on a 6 bits panel, we end up with crappy looking gradients, if enabling it on a 8 bits panel, we basically end up sending a 6 bits signal to a 8 bits panel, thus reducing the quality.

The main 128-byte block of EDID version 1.x does not carry information for that. However, EDID version 1.3 and later support extended 128-byte blocks, and one of them, DI-EXT may carry the information you need. Specifically, section 3.4.5 of this doc might help:

http://www.vesa.org/public/EDID%20EXTENSIONS/DIEXT.pdf

You will still need to know how to get these extended blocks from i2c. I think vesa's site also has public docs on how to do that.

EDID version 2 (which is 256 bytes in size) may also have that information, but I don't know if the specs are freely available.

Tony

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

A message that is not in the archive (sent off-list or not yet received): [email protected]

#13 lore raw
Quoted 10 lines · Benjamin Herrenschmidt

Hi !

Somebody around here has access to the VESA specs ? I'm trying to figure out if the EDID block, at least on recent flat panels, tells us wether the panel digital input is 6:6:6 or 8:8:8 ... If it's 6:6:6, we should enable dithering in drivers like radeon and "nv", while if 8:8:8, we should not. If not enabling it on a 6 bits panel, we end up with crappy looking gradients, if enabling it on a 8 bits panel, we basically end up sending a 6 bits signal to a 8 bits panel, thus reducing the quality.

This information is encoded in EDID Version 2 (not to be confused with the revisions of version 1).
Currently we have no code in X to read out this information.

Cheers,
Egbert.

-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl

A message that is not in the archive (sent off-list or not yet received): EJ Hsu <[email protected]

#15 lore raw

PATCH 1/1 usb/gadget/function: introduce Built-in CDROM support

Introduce Built-In CDROM (BICR) support.
This feature depends on USB_CONFIGFS_MASS_STORAGE option.

1. Some settings and new function is introduced for BICR.
2. Some work around for adapting Android settings is intorduced as well.

---
 drivers/usb/gadget/Kconfig                   | 16 +++++++
 drivers/usb/gadget/function/f_mass_storage.c | 49 +++++++++++++++++++-
 drivers/usb/gadget/function/f_mass_storage.h |  5 +-
 drivers/usb/gadget/function/storage_common.c | 23 +++++++++
 4 files changed, 90 insertions(+), 3 deletions(-)
drivers/usb/gadget/Kconfig +16 -0
index 4dc4d48fe6a6..686ba01bedb5 100644
@@ -188,6 +188,9 @@ config USB_F_RNDIS
 config USB_F_MASS_STORAGE
 	tristate
 
+config USB_F_BICR
+	tristate
+
 config USB_F_FS
 	tristate
 
@@ -357,6 +360,19 @@ config USB_CONFIGFS_MASS_STORAGE
 	  device (in much the same way as the "loop" device driver),
 	  specified as a module parameter or sysfs option.
 
+config USB_CONFIGFS_BICR
+	bool "Built-In CDROM emulation"
+	depends on USB_CONFIGFS
+	depends on BLOCK
+	depends on USB_CONFIGFS_MASS_STORAGE
+	select USB_F_BICR
+	help
+	  The Build-In CDROM Gadget acts as a CDROM emulation disk drive.
+	  It is based on kernel option "USB_CONFIGFS_MASS_STORAGE".
+	  As its storage repository it can use a regular file or a block
+	  device (in much the same way as the "loop" device driver),
+	  specified as a module parameter or sysfs option.
+
 config USB_CONFIGFS_F_LB_SS
 	bool "Loopback and sourcesink function (for testing)"
 	depends on USB_CONFIGFS
drivers/usb/gadget/function/f_mass_storage.c +47 -2

91 lines not on this page · raw message

drivers/usb/gadget/function/f_mass_storage.h +4 -1
index 3b8c4ce2a40a..7097e2ea5cc9 100644
@@ -140,5 +140,8 @@ void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
 void fsg_config_from_params(struct fsg_config *cfg,
 			    const struct fsg_module_parameters *params,
 			    unsigned int fsg_num_buffers);
-
+#ifdef CONFIG_USB_CONFIGFS_BICR
+ssize_t fsg_bicr_show(struct fsg_common *common, char *buf);
+ssize_t fsg_bicr_store(struct fsg_common *common, const char *buf, size_t size);
+#endif
 #endif /* USB_F_MASS_STORAGE_H */
drivers/usb/gadget/function/storage_common.c +23 -0
index f7e6c42558eb..8fe96eeddf35 100644
@@ -441,6 +441,29 @@ ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 		return -EBUSY;				/* "Door is locked" */
 	}
 
+	pr_notice("%s file=%s, count=%d, curlun->cdrom=%d\n",
+			__func__, buf, (int)count, curlun->cdrom);
+
+	/*
+	 * WORKAROUND for Android:
+	 *   VOLD would clean the file path after switching to bicr.
+	 *   So when the lun is being a CD-ROM a.k.a. BICR.
+	 *   Don't clean the file path to empty.
+	 */
+	if (curlun->cdrom == 1 && count == 1)
+		return count;
+
+	/*
+	 * WORKAROUND: Should be closed the fsg lun for virtual cd-rom,
+	 * when switch to other usb functions.
+	 * Use the special keyword "off", because the init can
+	 * not parse the char '\n' in rc file and write into the sysfs.
+	 */
+	if (count == 3 &&
+			buf[0] == 'o' && buf[1] == 'f' && buf[2] == 'f' &&
+			fsg_lun_is_open(curlun))
+		((char *) buf)[0] = 0;
+
 	/* Remove a trailing newline */
 	if (count > 0 && buf[count-1] == '\n')
 		((char *) buf)[count-1] = 0;		/* Ugh! */
Signature · 1 line
2.18.0
#16 lore raw

Re: [PATCH] usb/gadget/function: introduce Built-in CDROM support

Quoted 5 lines · Macpaul Lin

Introduce Built-In CDROM (BICR) support.
This feature depends on USB_CONFIGFS_MASS_STORAGE option.

1. Some settings and new function is introduced for BICR.
2. Some work around for adapting Android settings is intorduced as well.

%s/intorduced/introduced

Quoted 13 lines
drivers/usb/gadget/function/f_mass_storage.c +3 -0
@@ -315,6 +315,9 @@ struct fsg_common {
 	void			*private_data;
 
 	char inquiry_string[INQUIRY_STRING_LEN];
+
+	/* For build-in CDROM */
+	u8 bicr;
 };
 
 struct fsg_dev {

drivers/usb/gadget/function/f_mass_storage.c static void set_bulk_out_req_length(struct fsg_common *common, · line 379

Quoted 8 lines
drivers/usb/gadget/function/f_mass_storage.c +4 -0
@@ -369,6 +372,10 @@ static void set_bulk_out_req_length(struct fsg_common *common,
 	if (rem > 0)
 		length += common->bulk_out_maxpacket - rem;
 	bh->outreq->length = length;
+
+	/* some USB 2.0 hardware requires this setting */
+	if (IS_ENABLED(USB_CONFIGFS_BICR))
+		bh->outreq->short_not_ok = 1;
 }

Why not use fsg_common.bicr instead of MACRO?

Peter

Quoted 129 lines
drivers/usb/gadget/function/f_mass_storage.c +40 -2
@@ -527,7 +534,16 @@ static int fsg_setup(struct usb_function *f,
 				w_length != 1)
 			return -EDOM;
 		VDBG(fsg, "get max LUN\n");
-		*(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common);
+		if (IS_ENABLED(USB_CONFIGFS_BICR) && fsg->common->bicr) {
+			/*
+			 * When Built-In CDROM is enabled,
+			 * we share only one LUN.
+			 */
+			*(u8 *)req->buf = 0;
+		} else {
+			*(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common);
+		}
+		INFO(fsg, "get max LUN = %d\n", *(u8 *)req->buf);
 
 		/* Respond with data/status */
 		req->length = min((u16)1, w_length);
@@ -1329,7 +1345,7 @@ static int do_start_stop(struct fsg_common *common)
 	}
 
 	/* Are we allowed to unload the media? */
-	if (curlun->prevent_medium_removal) {
+	if (!curlun->nofua && curlun->prevent_medium_removal) {
 		LDBG(curlun, "unload attempt prevented\n");
 		curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
 		return -EINVAL;
@@ -2692,6 +2708,7 @@ int fsg_common_set_cdev(struct fsg_common *common,
 	common->ep0 = cdev->gadget->ep0;
 	common->ep0req = cdev->req;
 	common->cdev = cdev;
+	common->bicr = 0;
 
 	us = usb_gstrings_attach(cdev, fsg_strings_array,
 				 ARRAY_SIZE(fsg_strings));
@@ -2895,6 +2912,33 @@ static void fsg_common_release(struct fsg_common *common)
 		kfree(common);
 }
 
+#ifdef USB_CONFIGFS_BICR
+ssize_t fsg_bicr_show(struct fsg_common *common, char *buf)
+{
+	return sprintf(buf, "%d\n", common->bicr);
+}
+
+ssize_t fsg_bicr_store(struct fsg_common *common, const char *buf, size_t size)
+{
+	int ret;
+
+	ret = kstrtou8(buf, 10, &common->bicr);
+	if (ret)
+		return -EINVAL;
+
+	/* Set Lun[0] is a CDROM when enable bicr.*/
+	if (!strcmp(buf, "1"))
+		common->luns[0]->cdrom = 1;
+	else {
+		common->luns[0]->cdrom = 0;
+		common->luns[0]->blkbits = 0;
+		common->luns[0]->blksize = 0;
+		common->luns[0]->num_sectors = 0;
+	}
+
+	return size;
+}
+#endif
 
 /*-------------------------------------------------------------------------*/
 
@@ -3463,6 +3507,7 @@ void fsg_config_from_params(struct fsg_config *cfg,
 		lun->ro = !!params->ro[i];
 		lun->cdrom = !!params->cdrom[i];
 		lun->removable = !!params->removable[i];
+		lun->nofua = !!params->nofua[i];
 		lun->filename =
 			params->file_count > i && params->file[i][0]
 			? params->file[i]
drivers/usb/gadget/function/f_mass_storage.h +4 -1
index 3b8c4ce2a40a..7097e2ea5cc9 100644
@@ -140,5 +140,8 @@ void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
 void fsg_config_from_params(struct fsg_config *cfg,
 			    const struct fsg_module_parameters *params,
 			    unsigned int fsg_num_buffers);
-
+#ifdef CONFIG_USB_CONFIGFS_BICR
+ssize_t fsg_bicr_show(struct fsg_common *common, char *buf);
+ssize_t fsg_bicr_store(struct fsg_common *common, const char *buf, size_t size);
+#endif
 #endif /* USB_F_MASS_STORAGE_H */
drivers/usb/gadget/function/storage_common.c +23 -0
index f7e6c42558eb..8fe96eeddf35 100644
@@ -441,6 +441,29 @@ ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 		return -EBUSY;				/* "Door is locked" */
 	}
 
+	pr_notice("%s file=%s, count=%d, curlun->cdrom=%d\n",
+			__func__, buf, (int)count, curlun->cdrom);
+
+	/*
+	 * WORKAROUND for Android:
+	 *   VOLD would clean the file path after switching to bicr.
+	 *   So when the lun is being a CD-ROM a.k.a. BICR.
+	 *   Don't clean the file path to empty.
+	 */
+	if (curlun->cdrom == 1 && count == 1)
+		return count;
+
+	/*
+	 * WORKAROUND: Should be closed the fsg lun for virtual cd-rom,
+	 * when switch to other usb functions.
+	 * Use the special keyword "off", because the init can
+	 * not parse the char '\n' in rc file and write into the sysfs.
+	 */
+	if (count == 3 &&
+			buf[0] == 'o' && buf[1] == 'f' && buf[2] == 'f' &&
+			fsg_lun_is_open(curlun))
+		((char *) buf)[0] = 0;
+
 	/* Remove a trailing newline */
 	if (count > 0 && buf[count-1] == '\n')
 		((char *) buf)[count-1] = 0;		/* Ugh! */
Signature · 1 line
2.18.0
Signature · 2 lines
Thanks,
Peter Chen
#17 lore raw

PATCH v2 1/1 usb/gadget/function: introduce Built-in CDROM support

In reply to Macpaul Lin

Introduce Built-In CDROM (BICR) support.
This feature depends on USB_CONFIGFS_MASS_STORAGE option.

1. Some settings and new function is introduced for BICR.
2. Some work around for adapting Android settings is introduced as well.

---
Changes for v2:
- Thanks for Peter's review.
- Fix typo in commit message.
- use variable common->bicr instead of IS_ENABLED().
- Fix #ifdef CONFIG_USB_CONFIGFS_BICR.

 drivers/usb/gadget/Kconfig                   | 16 +++++++
 drivers/usb/gadget/function/f_mass_storage.c | 49 +++++++++++++++++++-
 drivers/usb/gadget/function/f_mass_storage.h |  5 +-
 drivers/usb/gadget/function/storage_common.c | 23 +++++++++
 4 files changed, 90 insertions(+), 3 deletions(-)
drivers/usb/gadget/Kconfig +16 -0
index 4dc4d48fe6a6..686ba01bedb5 100644
@@ -188,6 +188,9 @@ config USB_F_RNDIS
 config USB_F_MASS_STORAGE
 	tristate
 
+config USB_F_BICR
+	tristate
+
 config USB_F_FS
 	tristate
 
@@ -357,6 +360,19 @@ config USB_CONFIGFS_MASS_STORAGE
 	  device (in much the same way as the "loop" device driver),
 	  specified as a module parameter or sysfs option.
 
+config USB_CONFIGFS_BICR
+	bool "Built-In CDROM emulation"
+	depends on USB_CONFIGFS
+	depends on BLOCK
+	depends on USB_CONFIGFS_MASS_STORAGE
+	select USB_F_BICR
+	help
+	  The Build-In CDROM Gadget acts as a CDROM emulation disk drive.
+	  It is based on kernel option "USB_CONFIGFS_MASS_STORAGE".
+	  As its storage repository it can use a regular file or a block
+	  device (in much the same way as the "loop" device driver),
+	  specified as a module parameter or sysfs option.
+
 config USB_CONFIGFS_F_LB_SS
 	bool "Loopback and sourcesink function (for testing)"
 	depends on USB_CONFIGFS
drivers/usb/gadget/function/f_mass_storage.c +47 -2

91 lines not on this page · raw message

drivers/usb/gadget/function/f_mass_storage.h +4 -1
index 3b8c4ce2a40a..7097e2ea5cc9 100644
@@ -140,5 +140,8 @@ void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
 void fsg_config_from_params(struct fsg_config *cfg,
 			    const struct fsg_module_parameters *params,
 			    unsigned int fsg_num_buffers);
-
+#ifdef CONFIG_USB_CONFIGFS_BICR
+ssize_t fsg_bicr_show(struct fsg_common *common, char *buf);
+ssize_t fsg_bicr_store(struct fsg_common *common, const char *buf, size_t size);
+#endif
 #endif /* USB_F_MASS_STORAGE_H */
drivers/usb/gadget/function/storage_common.c +23 -0
index f7e6c42558eb..8fe96eeddf35 100644
@@ -441,6 +441,29 @@ ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 		return -EBUSY;				/* "Door is locked" */
 	}
 
+	pr_notice("%s file=%s, count=%d, curlun->cdrom=%d\n",
+			__func__, buf, (int)count, curlun->cdrom);
+
+	/*
+	 * WORKAROUND for Android:
+	 *   VOLD would clean the file path after switching to bicr.
+	 *   So when the lun is being a CD-ROM a.k.a. BICR.
+	 *   Don't clean the file path to empty.
+	 */
+	if (curlun->cdrom == 1 && count == 1)
+		return count;
+
+	/*
+	 * WORKAROUND: Should be closed the fsg lun for virtual cd-rom,
+	 * when switch to other usb functions.
+	 * Use the special keyword "off", because the init can
+	 * not parse the char '\n' in rc file and write into the sysfs.
+	 */
+	if (count == 3 &&
+			buf[0] == 'o' && buf[1] == 'f' && buf[2] == 'f' &&
+			fsg_lun_is_open(curlun))
+		((char *) buf)[0] = 0;
+
 	/* Remove a trailing newline */
 	if (count > 0 && buf[count-1] == '\n')
 		((char *) buf)[count-1] = 0;		/* Ugh! */
Signature · 1 line
2.18.0
#18 lore raw

Re: [PATCH v2] usb/gadget/function: introduce Built-in CDROM support

Quoted 5 lines · Macpaul Lin

Introduce Built-In CDROM (BICR) support.
This feature depends on USB_CONFIGFS_MASS_STORAGE option.

1. Some settings and new function is introduced for BICR.
2. Some work around for adapting Android settings is introduced as well.

You're going to have to give a much better explanation of what this does. For people who don't know what Built-In CDROM support is, what you wrote is meaningless.

For example, how is BICR support different from the CDROM support already present in the driver? And what's so special about it that it needs its own kconfig setting?

drivers/usb/gadget/function/f_mass_storage.c static void set_bulk_out_req_length(struct fsg_common *common, · line 378

Quoted 8 lines
drivers/usb/gadget/function/f_mass_storage.c +4 -0
@@ -369,6 +372,10 @@ static void set_bulk_out_req_length(struct fsg_common *common,
 	if (rem > 0)
 		length += common->bulk_out_maxpacket - rem;
 	bh->outreq->length = length;
+
+	/* some USB 2.0 hardware requires this setting */
+	if (common->bicr)
+		bh->outreq->short_not_ok = 1;

How is this connected with BICR? If some USB 2.0 hardware requires this setting, shouldn't it always be turned on?

Besides, why does some hardware require this? What goes wrong if short_not_ok is set to 0? If it causes problems, why didn't we become aware of them many years ago?

Quoted 4 lines
drivers/usb/gadget/function/f_mass_storage.c +0 -0
@@ -527,7 +534,16 @@ static int fsg_setup(struct usb_function *f,
 				w_length != 1)
 			return -EDOM;
 		VDBG(fsg, "get max LUN\n");

drivers/usb/gadget/function/f_mass_storage.c static int fsg_setup(struct usb_function *f, · line 545

Quoted 10 lines
drivers/usb/gadget/function/f_mass_storage.c +9 -1
-		*(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common);
+		if (IS_ENABLED(USB_CONFIGFS_BICR) && fsg->common->bicr) {
+			/*
+			 * When Built-In CDROM is enabled,
+			 * we share only one LUN.
+			 */
+			*(u8 *)req->buf = 0;
+		} else {
+			*(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common);
+		}

This is a very strange way of enforcing a single-LUN restriction. Why do it here? A much more logical place would be where cfg->nluns is set up originally.

drivers/usb/gadget/function/f_mass_storage.c static int fsg_setup(struct usb_function *f, · line 546

Quoted 1 line
drivers/usb/gadget/function/f_mass_storage.c +1 -0
+		INFO(fsg, "get max LUN = %d\n", *(u8 *)req->buf);

This debugging line isn't needed.

drivers/usb/gadget/function/f_mass_storage.c static int do_start_stop(struct fsg_common *common) · line 1348

Quoted 8 lines
drivers/usb/gadget/function/f_mass_storage.c +1 -1
 		/* Respond with data/status */
 		req->length = min((u16)1, w_length);
@@ -1329,7 +1345,7 @@ static int do_start_stop(struct fsg_common *common)
 	}
 
 	/* Are we allowed to unload the media? */
-	if (curlun->prevent_medium_removal) {
+	if (!curlun->nofua && curlun->prevent_medium_removal) {

How is nofua connected to BICR? Or to prevent_medium_removal?

Quoted 31 lines
drivers/usb/gadget/function/f_mass_storage.c +17 -0
 		LDBG(curlun, "unload attempt prevented\n");
 		curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
 		return -EINVAL;
@@ -2692,6 +2708,7 @@ int fsg_common_set_cdev(struct fsg_common *common,
 	common->ep0 = cdev->gadget->ep0;
 	common->ep0req = cdev->req;
 	common->cdev = cdev;
+	common->bicr = 0;
 
 	us = usb_gstrings_attach(cdev, fsg_strings_array,
 				 ARRAY_SIZE(fsg_strings));
@@ -2895,6 +2912,33 @@ static void fsg_common_release(struct fsg_common *common)
 		kfree(common);
 }
 
+#ifdef CONFIG_USB_CONFIGFS_BICR
+ssize_t fsg_bicr_show(struct fsg_common *common, char *buf)
+{
+	return sprintf(buf, "%d\n", common->bicr);
+}
+
+ssize_t fsg_bicr_store(struct fsg_common *common, const char *buf, size_t size)
+{
+	int ret;
+
+	ret = kstrtou8(buf, 10, &common->bicr);
+	if (ret)
+		return -EINVAL;
+
+	/* Set Lun[0] is a CDROM when enable bicr.*/
+	if (!strcmp(buf, "1"))

drivers/usb/gadget/function/f_mass_storage.c static void fsg_common_release(struct fsg_common *common) · line 2940

Quoted 10 lines
drivers/usb/gadget/function/f_mass_storage.c +10 -0
+		common->luns[0]->cdrom = 1;
+	else {
+		common->luns[0]->cdrom = 0;
+		common->luns[0]->blkbits = 0;
+		common->luns[0]->blksize = 0;
+		common->luns[0]->num_sectors = 0;
+	}
+
+	return size;
+}

Where do these attributes ever get exported to sysfs?

drivers/usb/gadget/function/f_mass_storage.c void fsg_config_from_params(struct fsg_config *cfg, · line 3510

Quoted 9 lines
drivers/usb/gadget/function/f_mass_storage.c +2 -0
+#endif
 
 /*-------------------------------------------------------------------------*/
 
@@ -3463,6 +3507,7 @@ void fsg_config_from_params(struct fsg_config *cfg,
 		lun->ro = !!params->ro[i];
 		lun->cdrom = !!params->cdrom[i];
 		lun->removable = !!params->removable[i];
+		lun->nofua = !!params->nofua[i];

Isn't this set already? If not, it is a bug that has nothing to do with BICR.

Quoted 22 lines
drivers/usb/gadget/function/f_mass_storage.c +0 -0
 		lun->filename =
 			params->file_count > i && params->file[i][0]
 			? params->file[i]
drivers/usb/gadget/function/f_mass_storage.h +4 -1
index 3b8c4ce2a40a..7097e2ea5cc9 100644
@@ -140,5 +140,8 @@ void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
 void fsg_config_from_params(struct fsg_config *cfg,
 			    const struct fsg_module_parameters *params,
 			    unsigned int fsg_num_buffers);
-
+#ifdef CONFIG_USB_CONFIGFS_BICR
+ssize_t fsg_bicr_show(struct fsg_common *common, char *buf);
+ssize_t fsg_bicr_store(struct fsg_common *common, const char *buf, size_t size);
+#endif
 #endif /* USB_F_MASS_STORAGE_H */
drivers/usb/gadget/function/storage_common.c +0 -0
index f7e6c42558eb..8fe96eeddf35 100644

drivers/usb/gadget/function/storage_common.c ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, · line 445

Quoted 5 lines
drivers/usb/gadget/function/storage_common.c +2 -0
@@ -441,6 +441,29 @@ ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 		return -EBUSY;				/* "Door is locked" */
 	}
 
+	pr_notice("%s file=%s, count=%d, curlun->cdrom=%d\n",
+			__func__, buf, (int)count, curlun->cdrom);

Another debugging line that shouldn't be present in the final patch.

drivers/usb/gadget/function/storage_common.c ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, · line 458

Quoted 13 lines
drivers/usb/gadget/function/storage_common.c +13 -0
+
+	/*
+	 * WORKAROUND for Android:
+	 *   VOLD would clean the file path after switching to bicr.
+	 *   So when the lun is being a CD-ROM a.k.a. BICR.
+	 *   Don't clean the file path to empty.
+	 */
+	if (curlun->cdrom == 1 && count == 1)
+		return count;
+
+	/*
+	 * WORKAROUND: Should be closed the fsg lun for virtual cd-rom,
+	 * when switch to other usb functions.

That is not a grammatical English sentence.

drivers/usb/gadget/function/storage_common.c ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, · line 465

Quoted 7 lines
drivers/usb/gadget/function/storage_common.c +7 -0
+	 * Use the special keyword "off", because the init can
+	 * not parse the char '\n' in rc file and write into the sysfs.
+	 */
+	if (count == 3 &&
+			buf[0] == 'o' && buf[1] == 'f' && buf[2] == 'f' &&
+			fsg_lun_is_open(curlun))
+		((char *) buf)[0] = 0;

This seems like another bug fix that has no connection with BICR.

Alan Stern

Quoted 6 lines
drivers/usb/gadget/function/storage_common.c +1 -0
+
 	/* Remove a trailing newline */
 	if (count > 0 && buf[count-1] == '\n')
 		((char *) buf)[count-1] = 0;		/* Ugh! */
Signature · 1 line
2.18.0
#19 lore raw

Re: [PATCH v2] usb/gadget/function: introduce Built-in CDROM support

Quoted 27 lines · Alan Stern
Quoted 5 lines · Macpaul Lin

Introduce Built-In CDROM (BICR) support.
This feature depends on USB_CONFIGFS_MASS_STORAGE option.

1. Some settings and new function is introduced for BICR.
2. Some work around for adapting Android settings is introduced as well.

You're going to have to give a much better explanation of what this does. For people who don't know what Built-In CDROM support is, what you wrote is meaningless.

For example, how is BICR support different from the CDROM support already present in the driver? And what's so special about it that it needs its own kconfig setting?

drivers/usb/gadget/function/f_mass_storage.c static void set_bulk_out_req_length(struct fsg_common *common, · line 378

Quoted 8 lines
drivers/usb/gadget/function/f_mass_storage.c +4 -0
@@ -369,6 +372,10 @@ static void set_bulk_out_req_length(struct fsg_common *common,
 	if (rem > 0)
 		length += common->bulk_out_maxpacket - rem;
 	bh->outreq->length = length;
+
+	/* some USB 2.0 hardware requires this setting */
+	if (common->bicr)
+		bh->outreq->short_not_ok = 1;

How is this connected with BICR? If some USB 2.0 hardware requires this setting, shouldn't it always be turned on?

Quoted 3 lines

Besides, why does some hardware require this? What goes wrong if short_not_ok is set to 0? If it causes problems, why didn't we become aware of them many years ago?

Thanks for Alan and Greg's suggestion, we will check these issues and see if a better solution could be work out.

Quoted 147 lines
Quoted 4 lines
drivers/usb/gadget/function/f_mass_storage.c +0 -0
@@ -527,7 +534,16 @@ static int fsg_setup(struct usb_function *f,
 				w_length != 1)
 			return -EDOM;
 		VDBG(fsg, "get max LUN\n");

drivers/usb/gadget/function/f_mass_storage.c static int fsg_setup(struct usb_function *f, · line 545

Quoted 10 lines
drivers/usb/gadget/function/f_mass_storage.c +9 -1
-		*(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common);
+		if (IS_ENABLED(USB_CONFIGFS_BICR) && fsg->common->bicr) {
+			/*
+			 * When Built-In CDROM is enabled,
+			 * we share only one LUN.
+			 */
+			*(u8 *)req->buf = 0;
+		} else {
+			*(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common);
+		}

This is a very strange way of enforcing a single-LUN restriction. Why do it here? A much more logical place would be where cfg->nluns is set up originally.

drivers/usb/gadget/function/f_mass_storage.c static int fsg_setup(struct usb_function *f, · line 546

Quoted 1 line
drivers/usb/gadget/function/f_mass_storage.c +1 -0
+		INFO(fsg, "get max LUN = %d\n", *(u8 *)req->buf);

This debugging line isn't needed.

drivers/usb/gadget/function/f_mass_storage.c static int do_start_stop(struct fsg_common *common) · line 1348

Quoted 8 lines
drivers/usb/gadget/function/f_mass_storage.c +1 -1
 		/* Respond with data/status */
 		req->length = min((u16)1, w_length);
@@ -1329,7 +1345,7 @@ static int do_start_stop(struct fsg_common *common)
 	}
 
 	/* Are we allowed to unload the media? */
-	if (curlun->prevent_medium_removal) {
+	if (!curlun->nofua && curlun->prevent_medium_removal) {

How is nofua connected to BICR? Or to prevent_medium_removal?

Quoted 31 lines
drivers/usb/gadget/function/f_mass_storage.c +17 -0
 		LDBG(curlun, "unload attempt prevented\n");
 		curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
 		return -EINVAL;
@@ -2692,6 +2708,7 @@ int fsg_common_set_cdev(struct fsg_common *common,
 	common->ep0 = cdev->gadget->ep0;
 	common->ep0req = cdev->req;
 	common->cdev = cdev;
+	common->bicr = 0;
 
 	us = usb_gstrings_attach(cdev, fsg_strings_array,
 				 ARRAY_SIZE(fsg_strings));
@@ -2895,6 +2912,33 @@ static void fsg_common_release(struct fsg_common *common)
 		kfree(common);
 }
 
+#ifdef CONFIG_USB_CONFIGFS_BICR
+ssize_t fsg_bicr_show(struct fsg_common *common, char *buf)
+{
+	return sprintf(buf, "%d\n", common->bicr);
+}
+
+ssize_t fsg_bicr_store(struct fsg_common *common, const char *buf, size_t size)
+{
+	int ret;
+
+	ret = kstrtou8(buf, 10, &common->bicr);
+	if (ret)
+		return -EINVAL;
+
+	/* Set Lun[0] is a CDROM when enable bicr.*/
+	if (!strcmp(buf, "1"))

drivers/usb/gadget/function/f_mass_storage.c static void fsg_common_release(struct fsg_common *common) · line 2940

Quoted 10 lines
drivers/usb/gadget/function/f_mass_storage.c +10 -0
+		common->luns[0]->cdrom = 1;
+	else {
+		common->luns[0]->cdrom = 0;
+		common->luns[0]->blkbits = 0;
+		common->luns[0]->blksize = 0;
+		common->luns[0]->num_sectors = 0;
+	}
+
+	return size;
+}

Where do these attributes ever get exported to sysfs?

drivers/usb/gadget/function/f_mass_storage.c void fsg_config_from_params(struct fsg_config *cfg, · line 3510

Quoted 9 lines
drivers/usb/gadget/function/f_mass_storage.c +2 -0
+#endif
 
 /*-------------------------------------------------------------------------*/
 
@@ -3463,6 +3507,7 @@ void fsg_config_from_params(struct fsg_config *cfg,
 		lun->ro = !!params->ro[i];
 		lun->cdrom = !!params->cdrom[i];
 		lun->removable = !!params->removable[i];
+		lun->nofua = !!params->nofua[i];

Isn't this set already? If not, it is a bug that has nothing to do with BICR.

Quoted 22 lines
drivers/usb/gadget/function/f_mass_storage.c +0 -0
 		lun->filename =
 			params->file_count > i && params->file[i][0]
 			? params->file[i]
drivers/usb/gadget/function/f_mass_storage.h +4 -1
index 3b8c4ce2a40a..7097e2ea5cc9 100644
@@ -140,5 +140,8 @@ void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
 void fsg_config_from_params(struct fsg_config *cfg,
 			    const struct fsg_module_parameters *params,
 			    unsigned int fsg_num_buffers);
-
+#ifdef CONFIG_USB_CONFIGFS_BICR
+ssize_t fsg_bicr_show(struct fsg_common *common, char *buf);
+ssize_t fsg_bicr_store(struct fsg_common *common, const char *buf, size_t size);
+#endif
 #endif /* USB_F_MASS_STORAGE_H */
drivers/usb/gadget/function/storage_common.c +0 -0
index f7e6c42558eb..8fe96eeddf35 100644

drivers/usb/gadget/function/storage_common.c ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, · line 445

Quoted 5 lines
drivers/usb/gadget/function/storage_common.c +2 -0
@@ -441,6 +441,29 @@ ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 		return -EBUSY;				/* "Door is locked" */
 	}
 
+	pr_notice("%s file=%s, count=%d, curlun->cdrom=%d\n",
+			__func__, buf, (int)count, curlun->cdrom);

Another debugging line that shouldn't be present in the final patch.

drivers/usb/gadget/function/storage_common.c ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, · line 458

Quoted 13 lines
drivers/usb/gadget/function/storage_common.c +13 -0
+
+	/*
+	 * WORKAROUND for Android:
+	 *   VOLD would clean the file path after switching to bicr.
+	 *   So when the lun is being a CD-ROM a.k.a. BICR.
+	 *   Don't clean the file path to empty.
+	 */
+	if (curlun->cdrom == 1 && count == 1)
+		return count;
+
+	/*
+	 * WORKAROUND: Should be closed the fsg lun for virtual cd-rom,
+	 * when switch to other usb functions.

That is not a grammatical English sentence.

drivers/usb/gadget/function/storage_common.c ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, · line 465

Quoted 7 lines
drivers/usb/gadget/function/storage_common.c +7 -0
+	 * Use the special keyword "off", because the init can
+	 * not parse the char '\n' in rc file and write into the sysfs.
+	 */
+	if (count == 3 &&
+			buf[0] == 'o' && buf[1] == 'f' && buf[2] == 'f' &&
+			fsg_lun_is_open(curlun))
+		((char *) buf)[0] = 0;

This seems like another bug fix that has no connection with BICR.

Quoted 8 lines

Alan Stern

Quoted 6 lines
drivers/usb/gadget/function/storage_common.c +1 -0
+
 	/* Remove a trailing newline */
 	if (count > 0 && buf[count-1] == '\n')
 		((char *) buf)[count-1] = 0;		/* Ugh! */
Signature · 1 line
2.18.0

Thanks!
Macpaul Lin

#20 lore raw

Re: [PATCH] usb/gadget/function: introduce Built-in CDROM support

In reply to Macpaul Lin
Quoted 5 lines · Macpaul Lin

Introduce Built-In CDROM (BICR) support.
This feature depends on USB_CONFIGFS_MASS_STORAGE option.

1. Some settings and new function is introduced for BICR.
2. Some work around for adapting Android settings is intorduced as well.

If you have to list a number of things done in a single patch, you should break this up into multiple patches, as each patch should only do one thing. Please make this a patch series.

Also, you added new configuration settings, where are they documented?

Quoted 75 lines
---
 drivers/usb/gadget/Kconfig                   | 16 +++++++
 drivers/usb/gadget/function/f_mass_storage.c | 49 +++++++++++++++++++-
 drivers/usb/gadget/function/f_mass_storage.h |  5 +-
 drivers/usb/gadget/function/storage_common.c | 23 +++++++++
 4 files changed, 90 insertions(+), 3 deletions(-)
drivers/usb/gadget/Kconfig +16 -0
index 4dc4d48fe6a6..686ba01bedb5 100644
@@ -188,6 +188,9 @@ config USB_F_RNDIS
 config USB_F_MASS_STORAGE
 	tristate
 
+config USB_F_BICR
+	tristate
+
 config USB_F_FS
 	tristate
 
@@ -357,6 +360,19 @@ config USB_CONFIGFS_MASS_STORAGE
 	  device (in much the same way as the "loop" device driver),
 	  specified as a module parameter or sysfs option.
 
+config USB_CONFIGFS_BICR
+	bool "Built-In CDROM emulation"
+	depends on USB_CONFIGFS
+	depends on BLOCK
+	depends on USB_CONFIGFS_MASS_STORAGE
+	select USB_F_BICR
+	help
+	  The Build-In CDROM Gadget acts as a CDROM emulation disk drive.
+	  It is based on kernel option "USB_CONFIGFS_MASS_STORAGE".
+	  As its storage repository it can use a regular file or a block
+	  device (in much the same way as the "loop" device driver),
+	  specified as a module parameter or sysfs option.
+
 config USB_CONFIGFS_F_LB_SS
 	bool "Loopback and sourcesink function (for testing)"
 	depends on USB_CONFIGFS
drivers/usb/gadget/function/f_mass_storage.c +7 -1
index 33c2264a0e35..9de1cd465635 100644
@@ -315,6 +315,9 @@ struct fsg_common {
 	void			*private_data;
 
 	char inquiry_string[INQUIRY_STRING_LEN];
+
+	/* For build-in CDROM */
+	u8 bicr;
 };
 
 struct fsg_dev {
@@ -369,6 +372,10 @@ static void set_bulk_out_req_length(struct fsg_common *common,
 	if (rem > 0)
 		length += common->bulk_out_maxpacket - rem;
 	bh->outreq->length = length;
+
+	/* some USB 2.0 hardware requires this setting */
+	if (IS_ENABLED(USB_CONFIGFS_BICR))
+		bh->outreq->short_not_ok = 1;
 }
 
 
@@ -527,7 +534,16 @@ static int fsg_setup(struct usb_function *f,
 				w_length != 1)
 			return -EDOM;
 		VDBG(fsg, "get max LUN\n");
-		*(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common);

drivers/usb/gadget/function/f_mass_storage.c static int fsg_setup(struct usb_function *f, · line 546

Quoted 10 lines
drivers/usb/gadget/function/f_mass_storage.c +10 -0
+		if (IS_ENABLED(USB_CONFIGFS_BICR) && fsg->common->bicr) {
+			/*
+			 * When Built-In CDROM is enabled,
+			 * we share only one LUN.
+			 */
+			*(u8 *)req->buf = 0;
+		} else {
+			*(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common);
+		}
+		INFO(fsg, "get max LUN = %d\n", *(u8 *)req->buf);

Why this message all the time? Drivers should be quiet if all is good.

Quoted 96 lines
drivers/usb/gadget/function/f_mass_storage.c +30 -1
 		/* Respond with data/status */
 		req->length = min((u16)1, w_length);
@@ -1329,7 +1345,7 @@ static int do_start_stop(struct fsg_common *common)
 	}
 
 	/* Are we allowed to unload the media? */
-	if (curlun->prevent_medium_removal) {
+	if (!curlun->nofua && curlun->prevent_medium_removal) {
 		LDBG(curlun, "unload attempt prevented\n");
 		curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
 		return -EINVAL;
@@ -2692,6 +2708,7 @@ int fsg_common_set_cdev(struct fsg_common *common,
 	common->ep0 = cdev->gadget->ep0;
 	common->ep0req = cdev->req;
 	common->cdev = cdev;
+	common->bicr = 0;
 
 	us = usb_gstrings_attach(cdev, fsg_strings_array,
 				 ARRAY_SIZE(fsg_strings));
@@ -2895,6 +2912,33 @@ static void fsg_common_release(struct fsg_common *common)
 		kfree(common);
 }
 
+#ifdef USB_CONFIGFS_BICR
+ssize_t fsg_bicr_show(struct fsg_common *common, char *buf)
+{
+	return sprintf(buf, "%d\n", common->bicr);
+}
+
+ssize_t fsg_bicr_store(struct fsg_common *common, const char *buf, size_t size)
+{
+	int ret;
+
+	ret = kstrtou8(buf, 10, &common->bicr);
+	if (ret)
+		return -EINVAL;
+
+	/* Set Lun[0] is a CDROM when enable bicr.*/
+	if (!strcmp(buf, "1"))
+		common->luns[0]->cdrom = 1;
+	else {
+		common->luns[0]->cdrom = 0;
+		common->luns[0]->blkbits = 0;
+		common->luns[0]->blksize = 0;
+		common->luns[0]->num_sectors = 0;
+	}
+
+	return size;
+}
+#endif
 
 /*-------------------------------------------------------------------------*/
 
@@ -3463,6 +3507,7 @@ void fsg_config_from_params(struct fsg_config *cfg,
 		lun->ro = !!params->ro[i];
 		lun->cdrom = !!params->cdrom[i];
 		lun->removable = !!params->removable[i];
+		lun->nofua = !!params->nofua[i];
 		lun->filename =
 			params->file_count > i && params->file[i][0]
 			? params->file[i]
drivers/usb/gadget/function/f_mass_storage.h +4 -1
index 3b8c4ce2a40a..7097e2ea5cc9 100644
@@ -140,5 +140,8 @@ void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
 void fsg_config_from_params(struct fsg_config *cfg,
 			    const struct fsg_module_parameters *params,
 			    unsigned int fsg_num_buffers);
-
+#ifdef CONFIG_USB_CONFIGFS_BICR
+ssize_t fsg_bicr_show(struct fsg_common *common, char *buf);
+ssize_t fsg_bicr_store(struct fsg_common *common, const char *buf, size_t size);
+#endif
 #endif /* USB_F_MASS_STORAGE_H */
drivers/usb/gadget/function/storage_common.c +12 -0
index f7e6c42558eb..8fe96eeddf35 100644
@@ -441,6 +441,29 @@ ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
 		return -EBUSY;				/* "Door is locked" */
 	}
 
+	pr_notice("%s file=%s, count=%d, curlun->cdrom=%d\n",
+			__func__, buf, (int)count, curlun->cdrom);
+
+	/*
+	 * WORKAROUND for Android:
+	 *   VOLD would clean the file path after switching to bicr.
+	 *   So when the lun is being a CD-ROM a.k.a. BICR.
+	 *   Don't clean the file path to empty.
+	 */
+	if (curlun->cdrom == 1 && count == 1)
+		return count;
+

drivers/usb/gadget/function/storage_common.c ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, · line 465

Quoted 10 lines
drivers/usb/gadget/function/storage_common.c +10 -0
+	/*
+	 * WORKAROUND: Should be closed the fsg lun for virtual cd-rom,
+	 * when switch to other usb functions.
+	 * Use the special keyword "off", because the init can
+	 * not parse the char '\n' in rc file and write into the sysfs.
+	 */
+	if (count == 3 &&
+			buf[0] == 'o' && buf[1] == 'f' && buf[2] == 'f' &&
+			fsg_lun_is_open(curlun))
+		((char *) buf)[0] = 0;

Why not fix Android userspace? Wouldn't that be easier?

And the indentation here is really odd...

thanks,

greg k-h