linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: fix protocol translation in drivers/usb/storage/transport.c
@ 2025-10-03 10:46 Shi Hao
  2025-10-03 11:39 ` Greg KH
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Shi Hao @ 2025-10-03 10:46 UTC (permalink / raw)
  To: gregkh; +Cc: shuah, linux-usb, Shi Hao

Previously, only a few USB subclasses had srb->cmd_len translated to 6 or 12 bytes, which was insufficient for some SCSI opcodes. This patch extends translation to the correct opcode ranges so that command lengths are set properly.

Signed-off-by: Shi Hao <i.shihao.999@gmail.com>
---
 drivers/usb/storage/transport.c | 41 ++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 6 deletions(-)

diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
index 1aa1bd26c81f..4dd6bfa86c23 100644
--- a/drivers/usb/storage/transport.c
+++ b/drivers/usb/storage/transport.c
@@ -718,12 +718,41 @@ void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
 
 		scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sense_size);
 
-		/* FIXME: we must do the protocol translation here */
-		if (us->subclass == USB_SC_RBC || us->subclass == USB_SC_SCSI ||
-				us->subclass == USB_SC_CYP_ATACB)
-			srb->cmd_len = 6;
-		else
-			srb->cmd_len = 12;
+		/* Protocol translation per SCSI opcode group */
+                switch(us->subclass)
+                {
+                case USB_SC_UFI:
+                case USB_SC_8020:
+                case USB_SC_8070:
+                case USB_SC_QIC:
+                         srb->cmd_len = 12 ;             /* ATAPI/UFI devices always use 12-byte CDBs */
+                         break;
+
+                case USB_SC_RBC:
+                case USB_SC_SCSI:
+                case USB_SC_CYP_ATACB:                   /* Determine cmd_len based on SCSI opcode group */
+
+                         if(opcode <= 0x1F)             /* Group 0 */
+                         {
+                                 srb->cmd_len = 6 ;
+                         }else if( opcode <= 0x7F)      /* Group 1 & 2 */
+                         {
+                                 srb->cmd_len = 10;
+                         }else if(opcode <= 0x9F )      /* Group 5 */
+                         {
+                                srb->cmd_len = 16 ;
+                         }else if(opcode <=0xBF)        /* Group 6 */
+                         {
+                                srb->cmd_len = 12 ;
+                         }else if( opcode <=0xDF)       /* Group 7 */
+                         {
+                                srb->cmd_len = 16;
+                         }else{
+                                ;                       /* Leaving cmd_len  value unchanged  for  0xE0–0xFF vendor-specific*/
+
+                         }
+                        break;
+                }
 
 		/* issue the auto-sense command */
 		scsi_set_resid(srb, 0);
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2025-10-09 21:15 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-03 10:46 [PATCH] usb: fix protocol translation in drivers/usb/storage/transport.c Shi Hao
2025-10-03 11:39 ` Greg KH
2025-10-03 11:40 ` Greg KH
2025-10-06 12:31 ` [PATCH v3] usb-storage: Protocol translation for scsi opcode Shi Hao
2025-10-06 13:47   ` Alan Stern
2025-10-07 11:37     ` [PATCH v4] usb-storage v4: Simplify protocol translation Shi Hao
2025-10-07 14:35       ` Alan Stern
2025-10-08  7:04         ` [PATCH v5] usb-storage: " Shi Hao
2025-10-08  7:31           ` Greg KH
2025-10-08  9:18             ` ShiHao
2025-10-08 10:04               ` Greg KH
2025-10-08 11:09                 ` [PATCH v6] usb-storage: Fix comment indentation level Shi Hao
2025-10-08 11:21                   ` Greg KH
2025-10-09 21:15 ` [PATCH] usb: fix protocol translation in drivers/usb/storage/transport.c kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).