[PATCH v1] usb: storage: add a macro for the upper limit of max LUN

Dingyan Li posted 1 patch 1 year, 3 months ago
drivers/usb/gadget/function/f_tcm.c          | 8 ++------
drivers/usb/gadget/function/storage_common.h | 2 +-
drivers/usb/storage/transport.c              | 8 ++------
include/linux/usb/storage.h                  | 8 ++++++++
4 files changed, 13 insertions(+), 13 deletions(-)
[PATCH v1] usb: storage: add a macro for the upper limit of max LUN
Posted by Dingyan Li 1 year, 3 months ago
The meaning of this value is already used in several places,
but with constant values and comments to explain it separately.
It's better to have a central place to do this then use the macro
in those places for better readability.

Signed-off-by: Dingyan Li <18500469033@163.com>
---
 drivers/usb/gadget/function/f_tcm.c          | 8 ++------
 drivers/usb/gadget/function/storage_common.h | 2 +-
 drivers/usb/storage/transport.c              | 8 ++------
 include/linux/usb/storage.h                  | 8 ++++++++
 4 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
index 15bb3aa12aa8..e1914b20f816 100644
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -441,14 +441,10 @@ static int usbg_bot_setup(struct usb_function *f,
 			pr_err("No LUNs configured?\n");
 			return -EINVAL;
 		}
-		/*
-		 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
-		 * accessed. The upper limit is 0xf
-		 */
 		luns--;
-		if (luns > 0xf) {
+		if (luns > US_BULK_MAX_LUN_LIMIT) {
 			pr_info_once("Limiting the number of luns to 16\n");
-			luns = 0xf;
+			luns = US_BULK_MAX_LUN_LIMIT;
 		}
 		ret_lun = cdev->req->buf;
 		*ret_lun = luns;
diff --git a/drivers/usb/gadget/function/storage_common.h b/drivers/usb/gadget/function/storage_common.h
index ced5d2b09234..11ac785d5eee 100644
--- a/drivers/usb/gadget/function/storage_common.h
+++ b/drivers/usb/gadget/function/storage_common.h
@@ -131,7 +131,7 @@ static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
 #define FSG_BUFLEN	((u32)16384)
 
 /* Maximal number of LUNs supported in mass storage function */
-#define FSG_MAX_LUNS	16
+#define FSG_MAX_LUNS	(US_BULK_MAX_LUN_LIMIT + 1)
 
 enum fsg_buffer_state {
 	BUF_STATE_SENDING = -2,
diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
index 9d767f6bf722..e6bc8ecaecbb 100644
--- a/drivers/usb/storage/transport.c
+++ b/drivers/usb/storage/transport.c
@@ -1087,13 +1087,9 @@ int usb_stor_Bulk_max_lun(struct us_data *us)
 	usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
 		     result, us->iobuf[0]);
 
-	/*
-	 * If we have a successful request, return the result if valid. The
-	 * CBW LUN field is 4 bits wide, so the value reported by the device
-	 * should fit into that.
-	 */
+	/* If we have a successful request, return the result if valid. */
 	if (result > 0) {
-		if (us->iobuf[0] < 16) {
+		if (us->iobuf[0] <= US_BULK_MAX_LUN_LIMIT) {
 			return us->iobuf[0];
 		} else {
 			dev_info(&us->pusb_intf->dev,
diff --git a/include/linux/usb/storage.h b/include/linux/usb/storage.h
index 8539956bc2be..51be3bb8fccb 100644
--- a/include/linux/usb/storage.h
+++ b/include/linux/usb/storage.h
@@ -82,4 +82,12 @@ struct bulk_cs_wrap {
 #define US_BULK_RESET_REQUEST   0xff
 #define US_BULK_GET_MAX_LUN     0xfe
 
+/*
+ * If 4 LUNs are supported then the LUNs would be
+ * numbered from 0 to 3, and the return value for
+ * US_BULK_GET_MAX_LUN request would be 3. The valid
+ * LUN field is 4 bits wide, the upper limit is 0x0f.
+ */
+#define US_BULK_MAX_LUN_LIMIT   0x0f
+
 #endif
-- 
2.25.1
Re:[PATCH v1] usb: storage: add a macro for the upper limit of max LUN
Posted by Dingyan Li 1 year, 2 months ago
Hi Experts,

Any thoughts on this patch?

Regards,


At 2024-10-30 16:38:58, "Dingyan Li" <18500469033@163.com> wrote:
>The meaning of this value is already used in several places,
>but with constant values and comments to explain it separately.
>It's better to have a central place to do this then use the macro
>in those places for better readability.
>
>Signed-off-by: Dingyan Li <18500469033@163.com>
>---
> drivers/usb/gadget/function/f_tcm.c          | 8 ++------
> drivers/usb/gadget/function/storage_common.h | 2 +-
> drivers/usb/storage/transport.c              | 8 ++------
> include/linux/usb/storage.h                  | 8 ++++++++
> 4 files changed, 13 insertions(+), 13 deletions(-)
>
>diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
>index 15bb3aa12aa8..e1914b20f816 100644
>--- a/drivers/usb/gadget/function/f_tcm.c
>+++ b/drivers/usb/gadget/function/f_tcm.c
>@@ -441,14 +441,10 @@ static int usbg_bot_setup(struct usb_function *f,
> 			pr_err("No LUNs configured?\n");
> 			return -EINVAL;
> 		}
>-		/*
>-		 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
>-		 * accessed. The upper limit is 0xf
>-		 */
> 		luns--;
>-		if (luns > 0xf) {
>+		if (luns > US_BULK_MAX_LUN_LIMIT) {
> 			pr_info_once("Limiting the number of luns to 16\n");
>-			luns = 0xf;
>+			luns = US_BULK_MAX_LUN_LIMIT;
> 		}
> 		ret_lun = cdev->req->buf;
> 		*ret_lun = luns;
>diff --git a/drivers/usb/gadget/function/storage_common.h b/drivers/usb/gadget/function/storage_common.h
>index ced5d2b09234..11ac785d5eee 100644
>--- a/drivers/usb/gadget/function/storage_common.h
>+++ b/drivers/usb/gadget/function/storage_common.h
>@@ -131,7 +131,7 @@ static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
> #define FSG_BUFLEN	((u32)16384)
> 
> /* Maximal number of LUNs supported in mass storage function */
>-#define FSG_MAX_LUNS	16
>+#define FSG_MAX_LUNS	(US_BULK_MAX_LUN_LIMIT + 1)
> 
> enum fsg_buffer_state {
> 	BUF_STATE_SENDING = -2,
>diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
>index 9d767f6bf722..e6bc8ecaecbb 100644
>--- a/drivers/usb/storage/transport.c
>+++ b/drivers/usb/storage/transport.c
>@@ -1087,13 +1087,9 @@ int usb_stor_Bulk_max_lun(struct us_data *us)
> 	usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
> 		     result, us->iobuf[0]);
> 
>-	/*
>-	 * If we have a successful request, return the result if valid. The
>-	 * CBW LUN field is 4 bits wide, so the value reported by the device
>-	 * should fit into that.
>-	 */
>+	/* If we have a successful request, return the result if valid. */
> 	if (result > 0) {
>-		if (us->iobuf[0] < 16) {
>+		if (us->iobuf[0] <= US_BULK_MAX_LUN_LIMIT) {
> 			return us->iobuf[0];
> 		} else {
> 			dev_info(&us->pusb_intf->dev,
>diff --git a/include/linux/usb/storage.h b/include/linux/usb/storage.h
>index 8539956bc2be..51be3bb8fccb 100644
>--- a/include/linux/usb/storage.h
>+++ b/include/linux/usb/storage.h
>@@ -82,4 +82,12 @@ struct bulk_cs_wrap {
> #define US_BULK_RESET_REQUEST   0xff
> #define US_BULK_GET_MAX_LUN     0xfe
> 
>+/*
>+ * If 4 LUNs are supported then the LUNs would be
>+ * numbered from 0 to 3, and the return value for
>+ * US_BULK_GET_MAX_LUN request would be 3. The valid
>+ * LUN field is 4 bits wide, the upper limit is 0x0f.
>+ */
>+#define US_BULK_MAX_LUN_LIMIT   0x0f
>+
> #endif
>-- 
>2.25.1
Re: [PATCH v1] usb: storage: add a macro for the upper limit of max LUN
Posted by Alan Stern 1 year, 2 months ago
On Mon, Nov 25, 2024 at 04:01:36PM +0800, Dingyan Li wrote:
> Hi Experts,
> 
> Any thoughts on this patch?
> 
> Regards,

It looks fine to me.

Reviewed-by: Alan Stern <stern@rowland.harvard.edu>

Alan Stern

> At 2024-10-30 16:38:58, "Dingyan Li" <18500469033@163.com> wrote:
> >The meaning of this value is already used in several places,
> >but with constant values and comments to explain it separately.
> >It's better to have a central place to do this then use the macro
> >in those places for better readability.
> >
> >Signed-off-by: Dingyan Li <18500469033@163.com>
> >---
> > drivers/usb/gadget/function/f_tcm.c          | 8 ++------
> > drivers/usb/gadget/function/storage_common.h | 2 +-
> > drivers/usb/storage/transport.c              | 8 ++------
> > include/linux/usb/storage.h                  | 8 ++++++++
> > 4 files changed, 13 insertions(+), 13 deletions(-)
> >
> >diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
> >index 15bb3aa12aa8..e1914b20f816 100644
> >--- a/drivers/usb/gadget/function/f_tcm.c
> >+++ b/drivers/usb/gadget/function/f_tcm.c
> >@@ -441,14 +441,10 @@ static int usbg_bot_setup(struct usb_function *f,
> > 			pr_err("No LUNs configured?\n");
> > 			return -EINVAL;
> > 		}
> >-		/*
> >-		 * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
> >-		 * accessed. The upper limit is 0xf
> >-		 */
> > 		luns--;
> >-		if (luns > 0xf) {
> >+		if (luns > US_BULK_MAX_LUN_LIMIT) {
> > 			pr_info_once("Limiting the number of luns to 16\n");
> >-			luns = 0xf;
> >+			luns = US_BULK_MAX_LUN_LIMIT;
> > 		}
> > 		ret_lun = cdev->req->buf;
> > 		*ret_lun = luns;
> >diff --git a/drivers/usb/gadget/function/storage_common.h b/drivers/usb/gadget/function/storage_common.h
> >index ced5d2b09234..11ac785d5eee 100644
> >--- a/drivers/usb/gadget/function/storage_common.h
> >+++ b/drivers/usb/gadget/function/storage_common.h
> >@@ -131,7 +131,7 @@ static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
> > #define FSG_BUFLEN	((u32)16384)
> > 
> > /* Maximal number of LUNs supported in mass storage function */
> >-#define FSG_MAX_LUNS	16
> >+#define FSG_MAX_LUNS	(US_BULK_MAX_LUN_LIMIT + 1)
> > 
> > enum fsg_buffer_state {
> > 	BUF_STATE_SENDING = -2,
> >diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
> >index 9d767f6bf722..e6bc8ecaecbb 100644
> >--- a/drivers/usb/storage/transport.c
> >+++ b/drivers/usb/storage/transport.c
> >@@ -1087,13 +1087,9 @@ int usb_stor_Bulk_max_lun(struct us_data *us)
> > 	usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
> > 		     result, us->iobuf[0]);
> > 
> >-	/*
> >-	 * If we have a successful request, return the result if valid. The
> >-	 * CBW LUN field is 4 bits wide, so the value reported by the device
> >-	 * should fit into that.
> >-	 */
> >+	/* If we have a successful request, return the result if valid. */
> > 	if (result > 0) {
> >-		if (us->iobuf[0] < 16) {
> >+		if (us->iobuf[0] <= US_BULK_MAX_LUN_LIMIT) {
> > 			return us->iobuf[0];
> > 		} else {
> > 			dev_info(&us->pusb_intf->dev,
> >diff --git a/include/linux/usb/storage.h b/include/linux/usb/storage.h
> >index 8539956bc2be..51be3bb8fccb 100644
> >--- a/include/linux/usb/storage.h
> >+++ b/include/linux/usb/storage.h
> >@@ -82,4 +82,12 @@ struct bulk_cs_wrap {
> > #define US_BULK_RESET_REQUEST   0xff
> > #define US_BULK_GET_MAX_LUN     0xfe
> > 
> >+/*
> >+ * If 4 LUNs are supported then the LUNs would be
> >+ * numbered from 0 to 3, and the return value for
> >+ * US_BULK_GET_MAX_LUN request would be 3. The valid
> >+ * LUN field is 4 bits wide, the upper limit is 0x0f.
> >+ */
> >+#define US_BULK_MAX_LUN_LIMIT   0x0f
> >+
> > #endif
> >-- 
> >2.25.1
> 
> -- 
> You received this message because you are subscribed to the Google Groups "USB Mass Storage on Linux" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to usb-storage+unsubscribe@lists.one-eyed-alien.net.
> To view this discussion visit https://groups.google.com/a/lists.one-eyed-alien.net/d/msgid/usb-storage/56abaf44.86c3.19362571bee.Coremail.18500469033%40163.com.