Skip to contentSkip to conversation

usb: storage: realtek_cr: Improve function parameter data types

PATCH In review Thorsten Blum 3 participants 9 messages Aug 13, 2025

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

PATCH 1/3 usb: storage: realtek_cr: Improve function parameter data types

In rts51x_bulk_transport() and rts51x_read_status(), change the function parameters 'buf_len' and 'len' from 'int' to 'unsigned int' because their values cannot be negative.

---
 drivers/usb/storage/realtek_cr.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/usb/storage/realtek_cr.c +3 -2
index 7dea28c2b8ee..d2c3e3a39693 100644
@@ -199,7 +199,8 @@ static const struct us_unusual_dev realtek_cr_unusual_dev_list[] = {
 #undef UNUSUAL_DEV
 
 static int rts51x_bulk_transport(struct us_data *us, u8 lun,
-				 u8 *cmd, int cmd_len, u8 *buf, int buf_len,
+				 u8 *cmd, int cmd_len, u8 *buf,
+				 unsigned int buf_len,
 				 enum dma_data_direction dir, int *act_len)
 {
 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *)us->iobuf;
@@ -417,7 +418,7 @@ static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
 }
 
 static int rts51x_read_status(struct us_data *us,
-			      u8 lun, u8 *status, int len, int *actlen)
+			      u8 lun, u8 *status, unsigned int len, int *actlen)
 {
 	int retval;
 	u8 cmnd[12] = { 0 };
Signature · 1 line
2.50.1
#1 lore raw

PATCH 2/3 usb: storage: realtek_cr: Simplify residue calculation in rts51x_bulk_transport()

Simplify the calculation of 'residue' in rts51x_bulk_transport() and avoid unnecessarily reassigning 'residue' to itself.

---
 drivers/usb/storage/realtek_cr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/storage/realtek_cr.c +2 -2
index d2c3e3a39693..8a4d7c0f2662 100644
@@ -261,8 +261,8 @@ static int rts51x_bulk_transport(struct us_data *us, u8 lun,
 	 * try to compute the actual residue, based on how much data
 	 * was really transferred and what the device tells us
 	 */
-	if (residue)
-		residue = residue < buf_len ? residue : buf_len;
+	if (residue > buf_len)
+		residue = buf_len;
 
 	if (act_len)
 		*act_len = buf_len - residue;
Signature · 1 line
2.50.1
maintainer Acked-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] usb: storage: realtek_cr: Simplify residue calculation in rts51x_bulk_transport()

Quoted 13 lines · Thorsten Blum

Simplify the calculation of 'residue' in rts51x_bulk_transport() and avoid unnecessarily reassigning 'residue' to itself.

---
 drivers/usb/storage/realtek_cr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/storage/realtek_cr.c +0 -0
index d2c3e3a39693..8a4d7c0f2662 100644

drivers/usb/storage/realtek_cr.c static int rts51x_bulk_transport(struct us_data *us, u8 lun, · line 268

Quoted 10 lines
drivers/usb/storage/realtek_cr.c +2 -2
@@ -261,8 +261,8 @@ static int rts51x_bulk_transport(struct us_data *us, u8 lun,
 	 * try to compute the actual residue, based on how much data
 	 * was really transferred and what the device tells us
 	 */
-	if (residue)
-		residue = residue < buf_len ? residue : buf_len;
+	if (residue > buf_len)
+		residue = buf_len;
 
 	if (act_len)
 		*act_len = buf_len - residue;
#3 lore raw

PATCH 3/3 usb: storage: realtek_cr: Use correct byte order for bcs->Residue

In reply to Thorsten Blum

Since 'bcs->Residue' has the data type '__le32', we must convert it to the correct byte order of the CPU using this driver when assigning it to the local variable 'residue'.

Fixes 50a6cb932d5c ("USB: usb_storage: add ums-realtek driver")
---
 drivers/usb/storage/realtek_cr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/storage/realtek_cr.c +1 -1
index 8a4d7c0f2662..758258a569a6 100644
@@ -253,7 +253,7 @@ static int rts51x_bulk_transport(struct us_data *us, u8 lun,
 		return USB_STOR_TRANSPORT_ERROR;
 	}
 
-	residue = bcs->Residue;
+	residue = le32_to_cpu(bcs->Residue);
 	if (bcs->Tag != us->tag)
 		return USB_STOR_TRANSPORT_ERROR;
 
Signature · 1 line
2.50.1
maintainer Acked-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] usb: storage: realtek_cr: Use correct byte order for bcs->Residue

Quoted 17 lines · Thorsten Blum

Since 'bcs->Residue' has the data type '__le32', we must convert it to the correct byte order of the CPU using this driver when assigning it to the local variable 'residue'.

Fixes 50a6cb932d5c ("USB: usb_storage: add ums-realtek driver")
---
 drivers/usb/storage/realtek_cr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/storage/realtek_cr.c +0 -0
index 8a4d7c0f2662..758258a569a6 100644

drivers/usb/storage/realtek_cr.c static int rts51x_bulk_transport(struct us_data *us, u8 lun, · line 259

Quoted 8 lines
drivers/usb/storage/realtek_cr.c +1 -1
@@ -253,7 +253,7 @@ static int rts51x_bulk_transport(struct us_data *us, u8 lun,
 		return USB_STOR_TRANSPORT_ERROR;
 	}
 
-	residue = bcs->Residue;
+	residue = le32_to_cpu(bcs->Residue);
 	if (bcs->Tag != us->tag)
 		return USB_STOR_TRANSPORT_ERROR;
 
#5 lore raw

Re: [PATCH 3/3] usb: storage: realtek_cr: Use correct byte order for bcs->Residue

In reply to Thorsten Blum
Quoted 5 lines · Thorsten Blum

Since 'bcs->Residue' has the data type '__le32', we must convert it to the correct byte order of the CPU using this driver when assigning it to the local variable 'residue'.

When you have a bugfix, don't put it last in the patch series, as that doesn't make much sense if you want to backport it anywhere, like you are asking to do here.

Please just send this as a separate patch, and do the cleanups in a different series.

thanks,

greg k-h

#6 lore raw

Re: [PATCH 3/3] usb: storage: realtek_cr: Use correct byte order for bcs->Residue

Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Thorsten Blum

Since 'bcs->Residue' has the data type '__le32', we must convert it to the correct byte order of the CPU using this driver when assigning it to the local variable 'residue'.

Quoted 8 lines
Quoted 1 line

When you have a bugfix, don't put it last in the patch series, as that doesn't make much sense if you want to backport it anywhere, like you are asking to do here.

Please just send this as a separate patch, and do the cleanups in a different series.

Ok, you can find it here:

https://lore.kernel.org/lkml/[email protected]/

Thanks,
Thorsten

maintainer
#7 lore raw
In reply to Thorsten Blum
Quoted 24 lines · Thorsten Blum

In rts51x_bulk_transport() and rts51x_read_status(), change the function parameters 'buf_len' and 'len' from 'int' to 'unsigned int' because their values cannot be negative.

---
 drivers/usb/storage/realtek_cr.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/usb/storage/realtek_cr.c +2 -1
index 7dea28c2b8ee..d2c3e3a39693 100644
@@ -199,7 +199,8 @@ static const struct us_unusual_dev realtek_cr_unusual_dev_list[] = {
 #undef UNUSUAL_DEV
 
 static int rts51x_bulk_transport(struct us_data *us, u8 lun,
-				 u8 *cmd, int cmd_len, u8 *buf, int buf_len,
+				 u8 *cmd, int cmd_len, u8 *buf,
+				 unsigned int buf_len,
 				 enum dma_data_direction dir, int *act_len)
 {
 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *)us->iobuf;

drivers/usb/storage/realtek_cr.c static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len) · line 424

Quoted 8 lines
drivers/usb/storage/realtek_cr.c +1 -1
@@ -417,7 +418,7 @@ static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
 }
 
 static int rts51x_read_status(struct us_data *us,
-			      u8 lun, u8 *status, int len, int *actlen)
+			      u8 lun, u8 *status, unsigned int len, int *actlen)
 {
 	int retval;
 	u8 cmnd[12] = { 0 };

I just looked through the original source file. What about rts51x_bulk_transport_special()? Shouldn't its buf_len parameter also be unsigned?

For that matter, what about cmd_len in both routines?

And have you checked the corresponding values in all the other usb-storage subdrivers?

As you can see, worrying about the difference between signed and unsigned values, when it doesn't really matter, quickly leads to a morass.

Alan Stern

#8 lore raw
Quoted 4 lines · Alan Stern

I just looked through the original source file. What about rts51x_bulk_transport_special()? Shouldn't its buf_len parameter also be unsigned?

Quoted 8 lines

For that matter, what about cmd_len in both routines?

And have you checked the corresponding values in all the other usb-storage subdrivers?

As you can see, worrying about the difference between signed and unsigned values, when it doesn't really matter, quickly leads to a morass.

There are many other instances throughout the kernel where types could be improved, which is why I originally combined this with the if check change and limited the data type changes to that scope. Feel free to skip this one, as it might not be worthwhile as a standalone patch.

Thanks,
Thorsten