USB storage improvements
Subsystems (3)
cover USB storage improvements
Everyone:
While creating fix found in "usb: storage: Don't use "unsigned long" for 32-bit values" I spent some time reading code of our USB storage implementation. The other 25 patches is the result of that effort and my attempt to improve/simplify that codebase. Hopefuly each patch is self-explanatory.
Feedback is welcome!
Thanks,
Andrey Smirnov
Andrey Smirnov (26):
usb: storage: Simplify memory allocation in usb_stor_probe()
usb: storage: Replace custom debug tracing with dev_dbg
usb: storage: Don't use "unsigned long" for 32-bit values
usb: storage: Make usb_stor_read_capacity() a standalone function
usb: storage: Make usb_stor_inquiry() a standalone function
usb: storage: Make usb_stor_test_unit_ready() a standalone function
usb: storage: Make usb_stor_read_10() a standalone function
usb: storage: Make usb_stor_write_10() a standalone function
usb: storage: Drop extra call to transport in usb_stor_write_10()
usb: storage: Share code for READ(10) and WRITE(10)
usb: storage: Make usb_stor_request_sense() a standalone function
usb: storage: Remove unused variables
usb: storage: Drop struct SCSI_cmd_block
usb: hub: Do not include <scsi.h>
ata: ahci: Do not include <scsi.h>
usb: storage: Drop unused fields in struct us_data
usb: storage: Drop unused us_blkdev_list
usb: storage: Introduce usb_stor_transport()
usb: storage: Use put_unaligned_be* helpers
usb: Drop usb_disable_asynch()
usb: storage: Drop unnecessary check in usb_stor_blk_io()
usb: storage: Drop needless macro
usb: storage: Use simple boolean to speficy read vs. write operation
usb: storage: Simplify I/O loop in usb_stor_blk_io()
usb: storage: Drop unnecessary assignment
usb: storage: Inline usb_limit_blk_cnt()
drivers/ata/ahci.c | 1 - drivers/usb/core/hub.c | 1 - drivers/usb/core/usb.c | 24 +- drivers/usb/storage/transport.c | 87 +++--- drivers/usb/storage/transport.h | 2 +- drivers/usb/storage/usb.c | 484 ++++++++++++++------------------ drivers/usb/storage/usb.h | 19 +- include/scsi.h | 22 -- include/usb/usb.h | 1 - 9 files changed, 265 insertions(+), 376 deletions(-)
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 1/26 usb: storage: Simplify memory allocation in usb_stor_probe()
Replace explicit malloc() + OOM check and memset() with a single call to xzalloc().
--- drivers/usb/storage/usb.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/usb/storage/usb.c +1 -4
@@ -541,10 +541,7 @@ static int usb_stor_probe(struct usb_device *usbdev,US_DEBUGP ("Selected interface %d\n" , (int )intf->desc.bInterfaceNumber);/* allocate us_data structure */ - us = (struct us_data *)malloc (sizeof (struct us_data )); -if (!us) -return -ENOMEM ; -memset (us,0 ,sizeof (struct us_data )); + us =xzalloc (sizeof (*us));/* initialize the us_data structure */ us->pusb_dev = usbdev;
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 2/26 usb: storage: Replace custom debug tracing with dev_dbg
In reply to Andrey SmirnovReplace custom debug tracing macros with a call to dev_dbg to simplify code and add device info into degug output.
--- drivers/usb/storage/transport.c | 59 ++++++++-------- drivers/usb/storage/usb.c | 118 +++++++++++++++++--------------- drivers/usb/storage/usb.h | 7 -- 3 files changed, 94 insertions(+), 90 deletions(-)
drivers/usb/storage/transport.c +30 -29
@@ -24,8 +24,6 @@#include <errno.h> #include <dma.h> -#undef USB_STOR_DEBUG -#include "usb.h" #include "transport.h"
@@ -62,6 +60,7 @@ static int usb_stor_Bulk_clear_endpt_stall(struct us_data *us, unsigned int pipe/* Determine what the maximum LUN supported is */ int usb_stor_Bulk_max_lun (struct us_data *us) { +struct device_d *dev = &us->pusb_dev->dev;int len, ret =0 ;unsigned char *iobuf =dma_alloc (1 );
@@ -73,8 +72,8 @@ int usb_stor_Bulk_max_lun(struct us_data *us)USB_DIR_IN |USB_TYPE_CLASS |USB_RECIP_INTERFACE , 0, us->ifnum, iobuf, 1, USB_CNTL_TIMEOUT); -US_DEBUGP ("GetMaxLUN command result is %d, data is %d\n" , - len, (int )iobuf[0 ]); +dev_dbg (dev,"GetMaxLUN command result is %d, data is %d\n" , + len, (int )iobuf[0 ]);/* if we have a successful request, return the result */ if (len >0 )
@@ -94,6 +93,7 @@ int usb_stor_Bulk_max_lun(struct us_data *us)int usb_stor_Bulk_transport (ccb *srb,struct us_data *us) { +struct device_d *dev = &us->pusb_dev->dev;struct bulk_cb_wrap cbw;struct bulk_cs_wrap csw;int actlen, data_actlen;
@@ -117,14 +117,14 @@ int usb_stor_Bulk_transport(ccb *srb, struct us_data *us)memcpy (cbw.CDB, srb->cmd, cbw.Length);/* send it to out endpoint */ -US_DEBUGP ("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n" , -le32_to_cpu (cbw.Signature), cbw.Tag, -le32_to_cpu (cbw.DataTransferLength), cbw.Flags, - (cbw.Lun >>4 ), (cbw.Lun &0x0F ), - cbw.Length); +dev_dbg (dev,"Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n" , +le32_to_cpu (cbw.Signature), cbw.Tag, +le32_to_cpu (cbw.DataTransferLength), cbw.Flags, + (cbw.Lun >>4 ), (cbw.Lun &0x0F ), + cbw.Length); result =usb_bulk_msg (us->pusb_dev, pipeout, &cbw,US_BULK_CB_WRAP_LEN , &actlen,USB_BULK_TO ); -US_DEBUGP ("Bulk command transfer result=%d\n" , result); +dev_dbg (dev,"Bulk command transfer result=%d\n" , result);if (result <0 ) {usb_stor_Bulk_reset (us);return USB_STOR_TRANSPORT_FAILED ;
@@ -140,32 +140,32 @@ int usb_stor_Bulk_transport(ccb *srb, struct us_data *us)unsigned int pipe = dir_in ? pipein : pipeout; result =usb_bulk_msg (us->pusb_dev, pipe, srb->pdata, srb->datalen, &data_actlen,USB_BULK_TO ); -US_DEBUGP ("Bulk data transfer result 0x%x\n" , result); +dev_dbg (dev,"Bulk data transfer result 0x%x\n" , result);/* special handling of STALL in DATA phase */ if ((result <0 ) && (us->pusb_dev->status &USB_ST_STALLED )) { -US_DEBUGP ("DATA: stall\n" ); +dev_dbg (dev,"DATA: stall\n" );/* clear the STALL on the endpoint */ result =usb_stor_Bulk_clear_endpt_stall (us, pipe); }if (result <0 ) { -US_DEBUGP ("Device status: %lx\n" , us->pusb_dev->status); +dev_dbg (dev,"Device status: %lx\n" , us->pusb_dev->status);usb_stor_Bulk_reset (us);return USB_STOR_TRANSPORT_FAILED ; } }/* STATUS phase + error handling */ -US_DEBUGP ("Attempting to get CSW...\n" ); +dev_dbg (dev,"Attempting to get CSW...\n" ); result =usb_bulk_msg (us->pusb_dev, pipein, &csw,US_BULK_CS_WRAP_LEN , &actlen,USB_BULK_TO );/* did the endpoint stall? */ if ((result <0 ) && (us->pusb_dev->status &USB_ST_STALLED )) { -US_DEBUGP ("STATUS: stall\n" ); +dev_dbg (dev,"STATUS: stall\n" );/* clear the STALL on the endpoint */ result =usb_stor_Bulk_clear_endpt_stall (us, pipein);if (result >=0 ) { -US_DEBUGP ("Attempting to get CSW...\n" ); +dev_dbg (dev,"Attempting to get CSW...\n" ); result =usb_bulk_msg (us->pusb_dev, pipein, &csw,US_BULK_CS_WRAP_LEN , &actlen,USB_BULK_TO );
@@ -173,33 +173,33 @@ int usb_stor_Bulk_transport(ccb *srb, struct us_data *us) }if (result <0 ) { -US_DEBUGP ("Device status: %lx\n" , us->pusb_dev->status); +dev_dbg (dev,"Device status: %lx\n" , us->pusb_dev->status);usb_stor_Bulk_reset (us);return USB_STOR_TRANSPORT_FAILED ; }/* check bulk status */ residue =le32_to_cpu (csw.Residue); -US_DEBUGP ("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n" , -le32_to_cpu (csw.Signature), csw.Tag, residue, csw.Status); +dev_dbg (dev,"Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n" , +le32_to_cpu (csw.Signature), csw.Tag, residue, csw.Status);if (csw.Signature !=cpu_to_le32 (US_BULK_CS_SIGN )) { -US_DEBUGP ("Bad CSW signature\n" ); +dev_dbg (dev,"Bad CSW signature\n" );usb_stor_Bulk_reset (us);return USB_STOR_TRANSPORT_FAILED ; }else if (csw.Tag != cbw_tag) { -US_DEBUGP ("Mismatching tag\n" ); +dev_dbg (dev,"Mismatching tag\n" );usb_stor_Bulk_reset (us);return USB_STOR_TRANSPORT_FAILED ; }else if (csw.Status >=US_BULK_STAT_PHASE ) { -US_DEBUGP ("Status >= phase\n" ); +dev_dbg (dev,"Status >= phase\n" );usb_stor_Bulk_reset (us);return USB_STOR_TRANSPORT_ERROR ; }else if (residue > srb->datalen) { -US_DEBUGP ("residue (%uB) > req data (%luB)\n" , +dev_dbg (dev,"residue (%uB) > req data (%luB)\n" , residue, srb->datalen);return USB_STOR_TRANSPORT_FAILED ; }else if (csw.Status ==US_BULK_STAT_FAIL ) { -US_DEBUGP ("FAILED\n" ); +dev_dbg (dev,"FAILED\n" );return USB_STOR_TRANSPORT_FAILED ; } srb->trans_bytes =min (srb->datalen - residue, (ulong )data_actlen);
@@ -213,11 +213,12 @@ int usb_stor_Bulk_transport(ccb *srb, struct us_data *us)*/ int usb_stor_Bulk_reset (struct us_data *us) { +struct device_d *dev = &us->pusb_dev->dev;int result;int result2;unsigned int pipe; -US_DEBUGP ("%s called\n" , __func__); +dev_dbg (dev,"%s called\n" , __func__);/* issue the command */ result = usb_control_msg(us->pusb_dev,
@@ -226,24 +227,24 @@ int usb_stor_Bulk_reset(struct us_data *us)USB_TYPE_CLASS |USB_RECIP_INTERFACE , 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT);if ((result <0 ) && (us->pusb_dev->status &USB_ST_STALLED )) { -US_DEBUGP ("Soft reset stalled: %d\n" , result); +dev_dbg (dev,"Soft reset stalled: %d\n" , result);return result; }mdelay (150 );/* clear the bulk endpoints halt */ -US_DEBUGP ("Soft reset: clearing %s endpoint halt\n" ,"bulk-in" ); +dev_dbg (dev,"Soft reset: clearing %s endpoint halt\n" ,"bulk-in" ); pipe =usb_rcvbulkpipe (us->pusb_dev, us->recv_bulk_ep); result =usb_clear_halt (us->pusb_dev, pipe);mdelay (150 ); -US_DEBUGP ("Soft reset: clearing %s endpoint halt\n" ,"bulk-out" ); +dev_dbg (dev,"Soft reset: clearing %s endpoint halt\n" ,"bulk-out" ); pipe =usb_sndbulkpipe (us->pusb_dev, us->send_bulk_ep); result2 =usb_clear_halt (us->pusb_dev, pipe);mdelay (150 );if (result >=0 ) result = result2; -US_DEBUGP ("Soft reset %s\n" , ((result <0 ) ?"failed" :"done" )); +dev_dbg (dev,"Soft reset %s\n" , ((result <0 ) ?"failed" :"done" ));return result; }
drivers/usb/storage/usb.c +64 -54
394 lines not on this page · raw message
drivers/usb/storage/usb.h +0 -7
@@ -28,13 +28,6 @@#include <linux/list.h> -#ifdef USB_STOR_DEBUG -#define US_DEBUGP(fmt, args...) printf(fmt , ##args) -#else -#define US_DEBUGP(fmt, args...) -#endif - -/* some defines, similar to ch9.h */ #define USB_EP_NUM (epd) \ ((epd)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 3/26 usb: storage: Don't use "unsigned long" for 32-bit values
In reply to Andrey SmirnovUnsignled long will expand to 64-bit unsigned integer on 64-bit CPUs. This will break current code using it to read out two 32-bit values returned by READ_CAPACITY. Fix the proble by using "u32" explicitly.
--- drivers/usb/storage/usb.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/usb/storage/usb.c +3 -3
@@ -322,7 +322,7 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)struct us_data *us = pblk_dev->us;struct device_d *dev = &us->pusb_dev->dev;ccb us_ccb; -unsigned long *pcap; +u32 *pcap;int result =0 ; us_ccb.pdata = us_io_buf;
@@ -363,8 +363,8 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev) result = -EIO ; goto Exit; } - pcap = (unsigned long *)us_ccb.pdata; -dev_dbg (dev,"Read Capacity returns: 0x%lx, 0x%lx\n" , pcap[0 ], pcap[1 ]); + pcap = (u32 *)us_ccb.pdata; +dev_dbg (dev,"Read Capacity returns: 0x%x, 0x%x\n" , pcap[0 ], pcap[1 ]); pblk_dev->blk.num_blocks =usb_limit_blk_cnt (be32_to_cpu (pcap[0 ]) +1 );if (be32_to_cpu (pcap[1 ]) !=SECTOR_SIZE )pr_warn ("Support only %d bytes sectors\n" ,SECTOR_SIZE );
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 4/26 usb: storage: Make usb_stor_read_capacity() a standalone function
In reply to Andrey SmirnovMove special preparation steps done before and after call to usb_stor_read_capacity() inside the function itself for clarity and to allow futher code improvements. No functional change intended.
--- drivers/usb/storage/usb.c | 53 +++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 22 deletions(-)
drivers/usb/storage/usb.c +31 -22
@@ -108,28 +108,39 @@ static int usb_stor_test_unit_ready(ccb *srb, struct us_data *us)return -1 ; } -static int usb_stor_read_capacity (ccb *srb,struct us_data *us) +static int usb_stor_read_capacity (struct us_blk_dev *usb_blkdev, +u32 *last_lba,u32 *block_length) { +struct us_data *us = usb_blkdev->us;struct device_d *dev = &us->pusb_dev->dev;int retries, result; - -if (srb->datalen <8 ) { -dev_dbg (dev,"SCSI_RD_CAPAC: invalid data buffer size\n" ); -return -EINVAL ; - } +ccb srb; +u32 *data =xzalloc (8 ); retries =3 ;do {dev_dbg (dev,"SCSI_RD_CAPAC\n" ); -memset (&srb->cmd[0 ],0 ,10 ); - srb->cmdlen =10 ; - srb->cmd[0 ] =SCSI_RD_CAPAC ; - srb->datalen =8 ; - result = us->transport (srb, us); +memset (&srb.cmd[0 ],0 ,10 ); + srb.cmdlen =10 ; + srb.lun = usb_blkdev->lun; + srb.cmd[0 ] =SCSI_RD_CAPAC ; + srb.pdata = (void *)data; + srb.datalen =8 ; + result = us->transport (&srb, us);dev_dbg (dev,"SCSI_RD_CAPAC returns %d\n" , result); - }while ((result !=USB_STOR_TRANSPORT_GOOD ) && retries--); -return (result !=USB_STOR_TRANSPORT_GOOD ) ? -EIO :0 ; +if (result ==USB_STOR_TRANSPORT_GOOD ) { +dev_dbg (dev,"Read Capacity returns: 0x%x, 0x%x\n" , + data[0 ], data[1 ]); + *last_lba =be32_to_cpu (data[0 ]); + *block_length =be32_to_cpu (data[1 ]); +break ; + } + }while (retries--); + +free (data); + +return result ? -EIO :0 ; } static int usb_stor_read_10(ccb *srb, struct us_data *us,
@@ -322,7 +333,7 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)struct us_data *us = pblk_dev->us;struct device_d *dev = &us->pusb_dev->dev;ccb us_ccb; -u32 *pcap; +u32 last_lba =0 , block_length =0 ;int result =0 ; us_ccb.pdata = us_io_buf;
@@ -356,17 +367,15 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)/* read capacity */ dev_dbg (dev,"Reading capacity\n" ); -memset (us_ccb.pdata,0 ,8 ); - us_ccb.datalen =sizeof (us_io_buf); -if (usb_stor_read_capacity (&us_ccb, us) !=0 ) { + + result =usb_stor_read_capacity (pblk_dev, &last_lba, &block_length); +if (result <0 ) {dev_dbg (dev,"Cannot read device capacity\n" ); - result = -EIO ; goto Exit; } - pcap = (u32 *)us_ccb.pdata; -dev_dbg (dev,"Read Capacity returns: 0x%x, 0x%x\n" , pcap[0 ], pcap[1 ]); - pblk_dev->blk.num_blocks =usb_limit_blk_cnt (be32_to_cpu (pcap[0 ]) +1 ); -if (be32_to_cpu (pcap[1 ]) !=SECTOR_SIZE ) + + pblk_dev->blk.num_blocks =usb_limit_blk_cnt (last_lba +1 ); +if (block_length !=SECTOR_SIZE )pr_warn ("Support only %d bytes sectors\n" ,SECTOR_SIZE ); pblk_dev->blk.blockbits =SECTOR_SHIFT ; dev_dbg(dev, "Capacity = 0x%x, blockshift = 0x%x\n",
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 5/26 usb: storage: Make usb_stor_inquiry() a standalone function
In reply to Andrey SmirnovMove special preparation steps done before and after call to usb_stor_inquiry() inside the function itself for clarity and to allow futher code improvements. No functional change intended.
--- drivers/usb/storage/usb.c | 52 ++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 22 deletions(-)
drivers/usb/storage/usb.c +30 -22
@@ -37,30 +37,43 @@ static LIST_HEAD(us_blkdev_list);* USB Storage routines ***********************************************************************/ -static int usb_stor_inquiry (ccb *srb,struct us_data *us) +static int usb_stor_inquiry (struct us_blk_dev *usb_blkdev) { +struct us_data *us = usb_blkdev->us;struct device_d *dev = &us->pusb_dev->dev;int retries, result; +ccb srb; +u8 *data =xzalloc (36 ); - srb->datalen =min (128UL , srb->datalen); -if (srb->datalen <5 ) { -dev_dbg (dev,"SCSI_INQUIRY: invalid data buffer size\n" ); -return -EINVAL ; - } + srb.lun = usb_blkdev->lun; + srb.pdata = data; + srb.datalen =36 ; retries =3 ;do {dev_dbg (dev,"SCSI_INQUIRY\n" ); -memset (&srb->cmd[0 ],0 ,6 ); - srb->cmdlen =6 ; - srb->cmd[0 ] =SCSI_INQUIRY ; - srb->cmd[3 ] = (u8)(srb->datalen >>8 ); - srb->cmd[4 ] = (u8)(srb->datalen >>0 ); - result = us->transport (srb, us); +memset (&srb.cmd[0 ],0 ,6 ); + srb.cmdlen =6 ; + srb.cmd[0 ] =SCSI_INQUIRY ; + srb.cmd[3 ] = (u8)(srb.datalen >>8 ); + srb.cmd[4 ] = (u8)(srb.datalen >>0 ); + result = us->transport (&srb, us);dev_dbg (dev,"SCSI_INQUIRY returns %d\n" , result); - }while ((result !=USB_STOR_TRANSPORT_GOOD ) && retries--); +if (result ==USB_STOR_TRANSPORT_GOOD ) { +dev_dbg (dev,"Peripheral type: %x, removable: %x\n" , + data[0 ], (data[1 ] >>7 )); +dev_dbg (dev,"ISO ver: %x, resp format: %x\n" , + data[2 ], data[3 ]); +dev_dbg (dev,"Vendor/product/rev: %28s\n" , + &data[8 ]); +// TODO: process and store device info +break ; + } + }while (retries--); -return (result !=USB_STOR_TRANSPORT_GOOD ) ? -EIO :0 ; +free (data); + +return result ? -ENODEV :0 ; }static int usb_stor_request_sense (ccb *srb,struct us_data *us)
@@ -344,17 +357,12 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)/* get device info */ dev_dbg (dev,"Reading device info\n" ); - us_ccb.datalen =36 ; -if (usb_stor_inquiry (&us_ccb, us)) { + + result =usb_stor_inquiry (pblk_dev); +if (result) {dev_dbg (dev,"Cannot read device info\n" ); - result = -ENODEV ; goto Exit; } -dev_dbg (dev,"Peripheral type: %x, removable: %x\n" , - us_io_buf[0 ], (us_io_buf[1 ] >>7 )); -dev_dbg (dev,"ISO ver: %x, resp format: %x\n" , us_io_buf[2 ], us_io_buf[3 ]); -dev_dbg (dev,"Vendor/product/rev: %28s\n" , &us_io_buf[8 ]); -// TODO: process and store device info /* ensure unit ready */ dev_dbg (dev,"Testing for unit ready\n" );
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 6/26 usb: storage: Make usb_stor_test_unit_ready() a standalone function
In reply to Andrey SmirnovMove special preparation steps done before the call to usb_stor_test_unit_ready() inside the function itself for clarity and to allow futher code improvements. No functional change intended.
--- drivers/usb/storage/usb.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-)
drivers/usb/storage/usb.c +16 -12
@@ -98,27 +98,31 @@ static int usb_stor_request_sense(ccb *srb, struct us_data *us)return 0 ; } -static int usb_stor_test_unit_ready (ccb *srb,struct us_data *us) +static int usb_stor_test_unit_ready (struct us_blk_dev *usb_blkdev) { +struct us_data *us = usb_blkdev->us;struct device_d *dev = &us->pusb_dev->dev;int retries, result; +ccb srb; + + srb.lun = usb_blkdev->lun; retries =10 ;do {dev_dbg (dev,"SCSI_TST_U_RDY\n" ); -memset (&srb->cmd[0 ],0 ,12 ); - srb->cmdlen =12 ; - srb->cmd[0 ] =SCSI_TST_U_RDY ; - srb->datalen =0 ; - result = us->transport (srb, us); +memset (&srb.cmd[0 ],0 ,12 ); + srb.cmdlen =12 ; + srb.cmd[0 ] =SCSI_TST_U_RDY ; + srb.datalen =0 ; + result = us->transport (&srb, us);dev_dbg (dev,"SCSI_TST_U_RDY returns %d\n" , result);if (result ==USB_STOR_TRANSPORT_GOOD )return 0 ; -usb_stor_request_sense (srb, us); +usb_stor_request_sense (&srb, us);mdelay (100 ); }while (retries--); -return -1 ; +return -ENODEV ; } static int usb_stor_read_capacity(struct us_blk_dev *usb_blkdev,
@@ -255,7 +259,7 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,/* ensure unit ready */ dev_dbg (dev,"Testing for unit ready\n" ); -if (usb_stor_test_unit_ready (&us_ccb, us)) { +if (usb_stor_test_unit_ready (pblk_dev)) {dev_dbg (dev,"Device NOT ready\n" );usb_disable_asynch (0 );return -EIO ;
@@ -366,10 +370,10 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)/* ensure unit ready */ dev_dbg (dev,"Testing for unit ready\n" ); - us_ccb.datalen =0 ; -if (usb_stor_test_unit_ready (&us_ccb, us)) { + + result =usb_stor_test_unit_ready (pblk_dev); +if (result) {dev_dbg (dev,"Device NOT ready\n" ); - result = -ENODEV ; goto Exit; }
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 7/26 usb: storage: Make usb_stor_read_10() a standalone function
In reply to Andrey SmirnovMove special preparation steps done before the call to usb_stor_read_10() inside the function itself for clarity and to allow futher code improvements. No functional change intended.
--- drivers/usb/storage/usb.c | 41 ++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 16 deletions(-)
drivers/usb/storage/usb.c +25 -16
@@ -160,29 +160,36 @@ static int usb_stor_read_capacity(struct us_blk_dev *usb_blkdev,return result ? -EIO :0 ; } -static int usb_stor_read_10 (ccb *srb,struct us_data *us, -unsigned long start,unsigned short blocks) +static int usb_stor_read_10 (struct us_blk_dev *usb_blkdev, +unsigned long start,u8 *data, +unsigned short blocks) { +struct us_data *us = usb_blkdev->us;struct device_d *dev = &us->pusb_dev->dev;int retries, result; +ccb srb; + + srb.lun = usb_blkdev->lun; + srb.pdata = data; + srb.datalen = blocks *SECTOR_SIZE ; retries =2 ;do {dev_dbg (dev,"SCSI_READ10: start %lx blocks %x\n" , start, blocks); -memset (&srb->cmd[0 ],0 ,10 ); - srb->cmdlen =10 ; - srb->cmd[0 ] =SCSI_READ10 ; - srb->cmd[2 ] = (u8)(start >>24 ); - srb->cmd[3 ] = (u8)(start >>16 ); - srb->cmd[4 ] = (u8)(start >>8 ); - srb->cmd[5 ] = (u8)(start >>0 ); - srb->cmd[7 ] = (u8)(blocks >>8 ); - srb->cmd[8 ] = (u8)(blocks >>0 ); - result = us->transport (srb, us); +memset (&srb.cmd[0 ],0 ,10 ); + srb.cmdlen =10 ; + srb.cmd[0 ] =SCSI_READ10 ; + srb.cmd[2 ] = (u8)(start >>24 ); + srb.cmd[3 ] = (u8)(start >>16 ); + srb.cmd[4 ] = (u8)(start >>8 ); + srb.cmd[5 ] = (u8)(start >>0 ); + srb.cmd[7 ] = (u8)(blocks >>8 ); + srb.cmd[8 ] = (u8)(blocks >>0 ); + result = us->transport (&srb, us);dev_dbg (dev,"SCSI_READ10 returns %d\n" , result);if (result ==USB_STOR_TRANSPORT_GOOD )return 0 ; -usb_stor_request_sense (srb, us); +usb_stor_request_sense (&srb, us); }while (retries--);return -EIO ;
@@ -283,11 +290,13 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,while (sector_count >0 ) {int result;unsigned n =min (sector_count,US_MAX_IO_BLK ); - us_ccb.pdata = buffer + (sectors_done *SECTOR_SIZE ); +u8 *data = buffer + (sectors_done *SECTOR_SIZE ); + us_ccb.pdata = data; us_ccb.datalen = n *SECTOR_SIZE ;if (io_op == io_rd) - result =usb_stor_read_10 (&us_ccb, us, - (ulong )sector_start, n); + result =usb_stor_read_10 (pblk_dev, + (ulong )sector_start, + data, n);else result =usb_stor_write_10 (&us_ccb, us, (ulong )sector_start, n);
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 8/26 usb: storage: Make usb_stor_write_10() a standalone function
In reply to Andrey SmirnovMove special preparation steps done before the call to usb_stor_write_10() inside the function itself for clarity and to allow futher code improvements. No functional change intended.
--- drivers/usb/storage/usb.c | 43 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-)
drivers/usb/storage/usb.c +25 -18
@@ -195,32 +195,39 @@ static int usb_stor_read_10(struct us_blk_dev *usb_blkdev,return -EIO ; } -static int usb_stor_write_10 (ccb *srb,struct us_data *us, -unsigned long start,unsigned short blocks) +static int usb_stor_write_10 (struct us_blk_dev *usb_blkdev, +unsigned long start,u8 *data, +unsigned short blocks) { +struct us_data *us = usb_blkdev->us;struct device_d *dev = &us->pusb_dev->dev;int retries, result; +ccb srb; + + srb.lun = usb_blkdev->lun; + srb.pdata = data; + srb.datalen = blocks *SECTOR_SIZE ; retries =2 ;do {dev_dbg (dev,"SCSI_WRITE10: start %lx blocks %x\n" , start, blocks); -memset (&srb->cmd[0 ],0 ,10 ); - srb->cmdlen =10 ; - srb->cmd[0 ] =SCSI_WRITE10 ; - srb->cmd[2 ] = (u8)(start >>24 ); - srb->cmd[3 ] = (u8)(start >>16 ); - srb->cmd[4 ] = (u8)(start >>8 ); - srb->cmd[5 ] = (u8)(start >>0 ); - srb->cmd[7 ] = (u8)(blocks >>8 ); - srb->cmd[8 ] = (u8)(blocks >>0 ); - result = us->transport (srb, us); +memset (&srb.cmd[0 ],0 ,10 ); + srb.cmdlen =10 ; + srb.cmd[0 ] =SCSI_WRITE10 ; + srb.cmd[2 ] = (u8)(start >>24 ); + srb.cmd[3 ] = (u8)(start >>16 ); + srb.cmd[4 ] = (u8)(start >>8 ); + srb.cmd[5 ] = (u8)(start >>0 ); + srb.cmd[7 ] = (u8)(blocks >>8 ); + srb.cmd[8 ] = (u8)(blocks >>0 ); + result = us->transport (&srb, us);dev_dbg (dev,"SCSI_WRITE10 returns %d\n" , result);if (result ==USB_STOR_TRANSPORT_GOOD )return 0 ; -usb_stor_request_sense (srb, us); +usb_stor_request_sense (&srb, us); }while (retries--); -return us->transport (srb, us); +return us->transport (&srb, us); }
@@ -291,15 +298,15 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,int result;unsigned n =min (sector_count,US_MAX_IO_BLK );u8 *data = buffer + (sectors_done *SECTOR_SIZE ); - us_ccb.pdata = data; - us_ccb.datalen = n *SECTOR_SIZE ; +if (io_op == io_rd) result =usb_stor_read_10 (pblk_dev, (ulong )sector_start, data, n);else - result =usb_stor_write_10 (&us_ccb, us, - (ulong )sector_start, n); + result =usb_stor_write_10 (pblk_dev, + (ulong )sector_start, + data, n);if (result !=0 ) {dev_dbg (dev,"I/O error at sector %d\n" , sector_start);break ;
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 9/26 usb: storage: Drop extra call to transport in usb_stor_write_10()
In reply to Andrey SmirnovThere doesn't seem to be a particularly good reason to call ->trasport() one last time after 2 failures. Drop the call so allow sharing this code with usb_stor_read_10() in the following commit.
--- drivers/usb/storage/usb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/storage/usb.c +1 -1
@@ -227,7 +227,7 @@ static int usb_stor_write_10(struct us_blk_dev *usb_blkdev,usb_stor_request_sense (&srb, us); }while (retries--); -return us->transport (&srb, us); +return -EIO ; }
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 10/26 usb: storage: Share code for READ(10) and WRITE(10)
In reply to Andrey SmirnovBoth usb_stor_read_10() and usb_stor_write_10() do almost exactly the same thing, so merge them into a signle routine and adjust all of the users accordingly.
--- drivers/usb/storage/usb.c | 60 ++++++--------------------------------- 1 file changed, 9 insertions(+), 51 deletions(-)
drivers/usb/storage/usb.c +9 -51
@@ -160,12 +160,11 @@ static int usb_stor_read_capacity(struct us_blk_dev *usb_blkdev,return result ? -EIO :0 ; } -static int usb_stor_read_10 (struct us_blk_dev *usb_blkdev, -unsigned long start,u8 *data, -unsigned short blocks) +static int usb_stor_io_10 (struct us_blk_dev *usb_blkdev,u8 cmd, +unsigned long start,u8 *data, +unsigned short blocks) {struct us_data *us = usb_blkdev->us; -struct device_d *dev = &us->pusb_dev->dev;int retries, result;ccb srb;
@@ -175,10 +174,9 @@ static int usb_stor_read_10(struct us_blk_dev *usb_blkdev, retries =2 ; do { - dev_dbg(dev, "SCSI_READ10: start %lx blocks %x\n", start, blocks); memset(&srb.cmd[0], 0, 10); srb.cmdlen = 10; - srb.cmd[0] = SCSI_READ10; + srb.cmd[0] = cmd; srb.cmd[2] = (u8)(start >> 24); srb.cmd[3] = (u8)(start >> 16); srb.cmd[4] = (u8)(start >> 8);
@@ -186,7 +184,6 @@ static int usb_stor_read_10(struct us_blk_dev *usb_blkdev, srb.cmd[7 ] = (u8)(blocks >>8 ); srb.cmd[8 ] = (u8)(blocks >>0 ); result = us->transport (&srb, us); -dev_dbg (dev,"SCSI_READ10 returns %d\n" , result);if (result ==USB_STOR_TRANSPORT_GOOD )return 0 ;usb_stor_request_sense (&srb, us);
@@ -195,42 +192,6 @@ static int usb_stor_read_10(struct us_blk_dev *usb_blkdev,return -EIO ; } -static int usb_stor_write_10 (struct us_blk_dev *usb_blkdev, -unsigned long start,u8 *data, -unsigned short blocks) -{ -struct us_data *us = usb_blkdev->us; -struct device_d *dev = &us->pusb_dev->dev; -int retries, result; -ccb srb; - - srb.lun = usb_blkdev->lun; - srb.pdata = data; - srb.datalen = blocks *SECTOR_SIZE ; - - retries =2 ; -do { -dev_dbg (dev,"SCSI_WRITE10: start %lx blocks %x\n" , start, blocks); -memset (&srb.cmd[0 ],0 ,10 ); - srb.cmdlen =10 ; - srb.cmd[0 ] =SCSI_WRITE10 ; - srb.cmd[2 ] = (u8)(start >>24 ); - srb.cmd[3 ] = (u8)(start >>16 ); - srb.cmd[4 ] = (u8)(start >>8 ); - srb.cmd[5 ] = (u8)(start >>0 ); - srb.cmd[7 ] = (u8)(blocks >>8 ); - srb.cmd[8 ] = (u8)(blocks >>0 ); - result = us->transport (&srb, us); -dev_dbg (dev,"SCSI_WRITE10 returns %d\n" , result); -if (result ==USB_STOR_TRANSPORT_GOOD ) -return 0 ; -usb_stor_request_sense (&srb, us); - }while (retries--); - -return -EIO ; -} - -/*********************************************************************** * Disk driver interface ***********************************************************************/
@@ -299,14 +260,11 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,unsigned n =min (sector_count,US_MAX_IO_BLK );u8 *data = buffer + (sectors_done *SECTOR_SIZE ); -if (io_op == io_rd) - result =usb_stor_read_10 (pblk_dev, - (ulong )sector_start, - data, n); -else - result =usb_stor_write_10 (pblk_dev, - (ulong )sector_start, - data, n); + result =usb_stor_io_10 (pblk_dev, + (io_op == io_rd) ? +SCSI_READ10 :SCSI_WRITE10 , + (ulong )sector_start, + data, n);if (result !=0 ) {dev_dbg (dev,"I/O error at sector %d\n" , sector_start);break ;
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 11/26 usb: storage: Make usb_stor_request_sense() a standalone function
In reply to Andrey SmirnovDon't try to re-use caller's "ccb" and instead convert usb_stor_request_sense() to use its own for clarity and to allow futher code improvements. No functional change intended.
--- drivers/usb/storage/usb.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-)
drivers/usb/storage/usb.c +18 -15
@@ -76,24 +76,27 @@ static int usb_stor_inquiry(struct us_blk_dev *usb_blkdev)return result ? -ENODEV :0 ; } -static int usb_stor_request_sense (ccb *srb,struct us_data *us) +static int usb_stor_request_sense (struct us_blk_dev *usb_blkdev) { +struct us_data *us = usb_blkdev->us;struct device_d *dev = &us->pusb_dev->dev; -unsigned char *pdata = srb->pdata; -unsigned long datalen = srb->datalen; +ccb srb; +u8 *data =xzalloc (18 ); + + srb.lun = usb_blkdev->lun;dev_dbg (dev,"SCSI_REQ_SENSE\n" ); - srb->pdata = &srb->sense_buf[0 ]; - srb->datalen =18 ; -memset (&srb->cmd[0 ],0 ,6 ); - srb->cmdlen =6 ; - srb->cmd[0 ] =SCSI_REQ_SENSE ; - srb->cmd[4 ] = (u8)(srb->datalen >>0 ); - us->transport (srb, us); + srb.pdata = data; + srb.datalen =18 ; +memset (&srb.cmd[0 ],0 ,6 ); + srb.cmdlen =6 ; + srb.cmd[0 ] =SCSI_REQ_SENSE ; + srb.cmd[4 ] = (u8)(srb.datalen >>0 ); + us->transport (&srb, us);dev_dbg (dev,"Request Sense returned %02X %02X %02X\n" , - srb->sense_buf[2 ], srb->sense_buf[12 ], srb->sense_buf[13 ]); - srb->pdata = pdata; - srb->datalen = datalen; + data[2 ], data[12 ], data[13 ]); + +free (data);return 0 ; }
@@ -118,7 +121,7 @@ static int usb_stor_test_unit_ready(struct us_blk_dev *usb_blkdev)dev_dbg (dev,"SCSI_TST_U_RDY returns %d\n" , result);if (result ==USB_STOR_TRANSPORT_GOOD )return 0 ; -usb_stor_request_sense (&srb, us); +usb_stor_request_sense (usb_blkdev);mdelay (100 ); }while (retries--);
@@ -186,7 +189,7 @@ static int usb_stor_io_10(struct us_blk_dev *usb_blkdev, u8 cmd, result = us->transport (&srb, us);if (result ==USB_STOR_TRANSPORT_GOOD )return 0 ; -usb_stor_request_sense (&srb, us); +usb_stor_request_sense (usb_blkdev); }while (retries--);return -EIO ;
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 12/26 usb: storage: Remove unused variables
In reply to Andrey SmirnovDrop a number of variables no longer used in the code.
--- drivers/usb/storage/usb.c | 8 -------- 1 file changed, 8 deletions(-)
drivers/usb/storage/usb.c +0 -8
@@ -212,7 +212,6 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,struct us_blk_dev *pblk_dev =to_usb_mass_storage (disk_dev);struct us_data *us = pblk_dev->us;struct device_d *dev = &us->pusb_dev->dev; -ccb us_ccb;unsigned sectors_done;if (sector_count ==0 )
@@ -232,7 +231,6 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,return -EINVAL ; } - us_ccb.lun = pblk_dev->lun;usb_disable_asynch (1 );/* ensure unit ready */
@@ -309,8 +307,6 @@ static struct block_device_ops usb_mass_storage_ops = {* Block device routines ***********************************************************************/ -static unsigned char us_io_buf[512 ]; -static int usb_limit_blk_cnt (unsigned cnt) {if (cnt >0x7fffffff ) {
@@ -326,13 +322,9 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev) {struct us_data *us = pblk_dev->us;struct device_d *dev = &us->pusb_dev->dev; -ccb us_ccb;u32 last_lba =0 , block_length =0 ;int result =0 ; - us_ccb.pdata = us_io_buf; - us_ccb.lun = pblk_dev->lun; - pblk_dev->blk.num_blocks =0 ;usb_disable_asynch (1 );
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 13/26 usb: storage: Drop struct SCSI_cmd_block
In reply to Andrey SmirnovAll the info we need to pass to transport function can be captured in a struct us_blk_dev and two byte arrays, so having a dedicated struct with many unused fields doesn't really buy us anything. Drop the struct and convert the rest of the code to pass needed data explicitly.
--- drivers/usb/storage/transport.c | 30 +++++----- drivers/usb/storage/transport.h | 2 +- drivers/usb/storage/usb.c | 100 ++++++++++++++------------------ drivers/usb/storage/usb.h | 8 ++- include/scsi.h | 22 ------- 5 files changed, 65 insertions(+), 97 deletions(-)
drivers/usb/storage/transport.c +15 -15
@@ -91,8 +91,11 @@ int usb_stor_Bulk_max_lun(struct us_data *us)return ret; } -int usb_stor_Bulk_transport (ccb *srb,struct us_data *us) +int usb_stor_Bulk_transport (struct us_blk_dev *usb_blkdev, +const u8 *cmd,u8 cmdlen, +void *data,u32 datalen) { +struct us_data *us = usb_blkdev->us;struct device_d *dev = &us->pusb_dev->dev;struct bulk_cb_wrap cbw;struct bulk_cs_wrap csw;
@@ -101,20 +104,18 @@ int usb_stor_Bulk_transport(ccb *srb, struct us_data *us)unsigned int residue;unsigned int pipein =usb_rcvbulkpipe (us->pusb_dev, us->recv_bulk_ep);unsigned int pipeout =usb_sndbulkpipe (us->pusb_dev, us->send_bulk_ep); -int dir_in =US_DIRECTION (srb->cmd[0 ]); - - srb->trans_bytes =0 ; +int dir_in =US_DIRECTION (cmd[0 ]);/* set up the command wrapper */ cbw.Signature =cpu_to_le32 (US_BULK_CB_SIGN ); - cbw.DataTransferLength =cpu_to_le32 (srb->datalen); + cbw.DataTransferLength =cpu_to_le32 (datalen); cbw.Flags = (dir_in ?US_BULK_FLAG_IN :US_BULK_FLAG_OUT ); cbw.Tag = ++cbw_tag; - cbw.Lun = srb->lun; - cbw.Length = srb->cmdlen; + cbw.Lun = usb_blkdev->lun; + cbw.Length = cmdlen;/* copy the command payload */ -memcpy (cbw.CDB, srb->cmd, cbw.Length); +memcpy (cbw.CDB, cmd, cbw.Length);/* send it to out endpoint */ dev_dbg(dev, "Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
@@ -136,10 +137,10 @@ int usb_stor_Bulk_transport(ccb *srb, struct us_data *us)mdelay (1 ); data_actlen =0 ; -if (srb->datalen) { +if (datalen) {unsigned int pipe = dir_in ? pipein : pipeout; - result =usb_bulk_msg (us->pusb_dev, pipe, srb->pdata, - srb->datalen, &data_actlen,USB_BULK_TO ); + result =usb_bulk_msg (us->pusb_dev, pipe, data, + datalen, &data_actlen,USB_BULK_TO );dev_dbg (dev,"Bulk data transfer result 0x%x\n" , result);/* special handling of STALL in DATA phase */ if ((result <0 ) && (us->pusb_dev->status &USB_ST_STALLED )) {
@@ -194,15 +195,14 @@ int usb_stor_Bulk_transport(ccb *srb, struct us_data *us)dev_dbg (dev,"Status >= phase\n" );usb_stor_Bulk_reset (us);return USB_STOR_TRANSPORT_ERROR ; - }else if (residue > srb->datalen) { -dev_dbg (dev,"residue (%uB) > req data (%luB)\n" , - residue, srb->datalen); + }else if (residue > datalen) { +dev_dbg (dev,"residue (%uB) > req data (%uB)\n" , + residue, datalen);return USB_STOR_TRANSPORT_FAILED ; }else if (csw.Status ==US_BULK_STAT_FAIL ) {dev_dbg (dev,"FAILED\n" );return USB_STOR_TRANSPORT_FAILED ; } - srb->trans_bytes =min (srb->datalen - residue, (ulong )data_actlen);return 0 ; }
drivers/usb/storage/transport.h +1 -1
@@ -84,7 +84,7 @@ struct bulk_cs_wrap {struct us_data ; -extern int usb_stor_Bulk_transport (ccb *,struct us_data *); +extern trans_cmnd usb_stor_Bulk_transport;extern int usb_stor_Bulk_max_lun (struct us_data *);extern int usb_stor_Bulk_reset (struct us_data *);
drivers/usb/storage/usb.c +44 -56
153 lines not on this page · raw message
drivers/usb/storage/usb.h +5 -3
@@ -44,8 +44,11 @@struct us_data ; +struct us_blk_dev ; -typedef int (*trans_cmnd )(ccb *cb,struct us_data *data); +typedef int (trans_cmnd )(struct us_blk_dev *usb_blkdev, +const u8 *cmd,u8 cmdlen, +void *data,u32 datalen);typedef int (*trans_reset )(struct us_data *data);/* one us_data object allocated per usb storage device */
@@ -65,11 +68,10 @@ struct us_data {char *transport_name; -trans_cmnd transport;/* transport function */ +trans_cmnd *transport;/* transport function */ trans_reset transport_reset;/* transport device reset */ /* SCSI interfaces */ -ccb *srb;/* current srb */ struct list_head blk_dev_list; };
include/scsi.h +0 -22
@@ -20,26 +20,6 @@#ifndef _SCSI_H#define _SCSI_H -typedef struct SCSI_cmd_block { -unsigned char cmd[16 ];/* command */ -unsigned char sense_buf[64 ];/* for request sense */ -unsigned char status;/* SCSI Status */ -unsigned char target;/* Target ID */ -unsigned char lun;/* Target LUN */ -unsigned char cmdlen;/* command len */ -unsigned long datalen;/* Total data length */ -unsigned char * pdata;/* pointer to data */ -unsigned char msgout[12 ];/* Messge out buffer (NOT USED) */ -unsigned char msgin[12 ];/* Message in buffer */ -unsigned char sensecmdlen;/* Sense command len */ -unsigned long sensedatalen;/* Sense data len */ -unsigned char sensecmd[6 ];/* Sense command */ -unsigned long contr_stat;/* Controller Status */ -unsigned long trans_bytes;/* tranfered bytes */ - -unsigned int priv; -}ccb ; -/*----------------------------------------------------------- ** ** SCSI constants.
@@ -171,8 +151,6 @@ typedef struct SCSI_cmd_block {* decleration of functions which have to reside in the LowLevel Part Driver */ -void scsi_print_error (ccb *pccb); -int scsi_exec (ccb *pccb);void scsi_bus_reset (void );void scsi_low_level_init (int busdevfunc);
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 14/26 usb: hub: Do not include <scsi.h>
In reply to Andrey SmirnovNo symbols found in <scsi.h> are used in this file. Drop it.
--- drivers/usb/core/hub.c | 1 - 1 file changed, 1 deletion(-)
drivers/usb/core/hub.c +0 -1
@@ -21,7 +21,6 @@#include <init.h> #include <malloc.h> #include <errno.h> -#include <scsi.h> #include <usb/phy.h> #include <usb/usb.h> #include <usb/usb_defs.h>
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 15/26 ata: ahci: Do not include <scsi.h>
In reply to Andrey SmirnovNo symbols found in <scsi.h> are used in this file. Drop it.
--- drivers/ata/ahci.c | 1 - 1 file changed, 1 deletion(-)
drivers/ata/ahci.c +0 -1
@@ -27,7 +27,6 @@#include <io.h> #include <of.h> #include <malloc.h> -#include <scsi.h> #include <linux/ctype.h> #include <linux/err.h> #include <disks.h>
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 16/26 usb: storage: Drop unused fields in struct us_data
In reply to Andrey SmirnovOnly bulk-only transport is supported by the currennt codebase, so ep_bInterval and recv_intr_ep are not really used. Remove them and all related code. While at it remove flags and subclass as well since they are not really used anywhere in the codebase.
--- drivers/usb/storage/usb.c | 14 ++------------ drivers/usb/storage/usb.h | 4 ---- 2 files changed, 2 insertions(+), 16 deletions(-)
drivers/usb/storage/usb.c +2 -12
@@ -433,7 +433,6 @@ static int get_pipes(struct us_data *us, struct usb_interface *intf)struct usb_endpoint_descriptor *ep;struct usb_endpoint_descriptor *ep_in =NULL ;struct usb_endpoint_descriptor *ep_out =NULL ; -struct usb_endpoint_descriptor *ep_int =NULL ;/* * Find the first endpoint of each type we need.
@@ -454,12 +453,8 @@ static int get_pipes(struct us_data *us, struct usb_interface *intf) ep_out = ep; } } -else if (USB_EP_IS_INT_IN (ep)) { -if (!ep_int) - ep_int = ep; - } } -if (!ep_in || !ep_out || (us->protocol ==US_PR_CBI && !ep_int)) { +if (!ep_in || !ep_out || us->protocol ==US_PR_CBI ) {dev_dbg (dev,"Endpoint sanity check failed! Rejecting dev.\n" );return -EIO ; }
@@ -467,10 +462,7 @@ static int get_pipes(struct us_data *us, struct usb_interface *intf)/* Store the pipe values */ us->send_bulk_ep =USB_EP_NUM (ep_out); us->recv_bulk_ep =USB_EP_NUM (ep_in); -if (ep_int) { - us->recv_intr_ep =USB_EP_NUM (ep_int); - us->ep_bInterval = ep_int->bInterval; - } +return 0 ; }
@@ -533,9 +525,7 @@ static int usb_stor_probe(struct usb_device *usbdev,/* initialize the us_data structure */ us->pusb_dev = usbdev; - us->flags =0 ; us->ifnum = intf->desc.bInterfaceNumber; - us->subclass = intf->desc.bInterfaceSubClass; us->protocol = intf->desc.bInterfaceProtocol;INIT_LIST_HEAD (&us->blk_dev_list);
drivers/usb/storage/usb.h +0 -4
@@ -54,17 +54,13 @@ typedef int (*trans_reset)(struct us_data *data);/* one us_data object allocated per usb storage device */ struct us_data {struct usb_device *pusb_dev;/* this usb_device */ -unsigned int flags;/* from filter */ unsigned char send_bulk_ep;/* used endpoints */ unsigned char recv_bulk_ep; -unsigned char recv_intr_ep;unsigned char ifnum;/* interface number */ -unsigned char subclass;unsigned char protocol;unsigned char max_lun; -unsigned char ep_bInterval;char *transport_name;
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 17/26 usb: storage: Drop unused us_blkdev_list
In reply to Andrey SmirnovThere are no users of us_blkdev_list in the file. Drop it.
--- drivers/usb/storage/usb.c | 3 --- 1 file changed, 3 deletions(-)
drivers/usb/storage/usb.c +0 -3
@@ -30,9 +30,6 @@#include "transport.h" -static LIST_HEAD (us_blkdev_list); - -/*********************************************************************** * USB Storage routines ***********************************************************************/
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 18/26 usb: storage: Introduce usb_stor_transport()
In reply to Andrey SmirnovMove retry logic found in all of the low-level usb_stor_* functions into a standalone subroutine and convert the rest of the code to use it.
--- drivers/usb/storage/usb.c | 213 +++++++++++++++++++++----------------- 1 file changed, 117 insertions(+), 96 deletions(-)
drivers/usb/storage/usb.c +117 -96
@@ -34,41 +34,7 @@* USB Storage routines ***********************************************************************/ -static int usb_stor_inquiry (struct us_blk_dev *usb_blkdev) -{ -struct us_data *us = usb_blkdev->us; -struct device_d *dev = &us->pusb_dev->dev; -int retries, result; -u8 cmd[6 ]; -const u32 datalen =36 ; -u8 *data =xzalloc (datalen); - - retries =3 ; -do { -dev_dbg (dev,"SCSI_INQUIRY\n" ); -memset (cmd,0 ,sizeof (cmd)); - cmd[0 ] =SCSI_INQUIRY ; - cmd[3 ] = (u8)(datalen >>8 ); - cmd[4 ] = (u8)(datalen >>0 ); - result = us->transport (usb_blkdev, cmd,sizeof (cmd), - data, datalen); -dev_dbg (dev,"SCSI_INQUIRY returns %d\n" , result); -if (result ==USB_STOR_TRANSPORT_GOOD ) { -dev_dbg (dev,"Peripheral type: %x, removable: %x\n" , - data[0 ], (data[1 ] >>7 )); -dev_dbg (dev,"ISO ver: %x, resp format: %x\n" , - data[2 ], data[3 ]); -dev_dbg (dev,"Vendor/product/rev: %28s\n" , - &data[8 ]); -// TODO: process and store device info -break ; - } - }while (retries--); - -free (data); - -return result ? -ENODEV :0 ; -} +#define USB_STOR_NO_REQUEST_SENSE -1static int usb_stor_request_sense (struct us_blk_dev *usb_blkdev) {
@@ -92,92 +58,147 @@ static int usb_stor_request_sense(struct us_blk_dev *usb_blkdev)return 0 ; } -static int usb_stor_test_unit_ready (struct us_blk_dev *usb_blkdev) +static const char *usb_stor_opcode_name (u8 opcode) +{ +switch (opcode) { +case SCSI_INQUIRY : +return "SCSI_INQUIRY" ; +case SCSI_TST_U_RDY : +return "SCSI_TST_U_RDY" ; +case SCSI_RD_CAPAC : +return "SCSI_RD_CAPAC" ; +case SCSI_READ10 : +return "SCSI_READ10" ; +case SCSI_WRITE10 : +return "SCSI_WRITE10" ; + }; + +return "UNKNOWN" ; +} + +static int usb_stor_transport (struct us_blk_dev *usb_blkdev, +const u8 *cmd,u8 cmdlen, +void *data,u32 datalen, +int retries,int request_sense_delay_ms) {struct us_data *us = usb_blkdev->us;struct device_d *dev = &us->pusb_dev->dev; -u8 cmd[12 ]; -int retries, result; - - retries =10 ; -do { -dev_dbg (dev,"SCSI_TST_U_RDY\n" ); -memset (cmd,0 ,sizeof (cmd)); - cmd[0 ] =SCSI_TST_U_RDY ; - result = us->transport (usb_blkdev, cmd,sizeof (cmd), -NULL ,0 ); -dev_dbg (dev,"SCSI_TST_U_RDY returns %d\n" , result); -if (result ==USB_STOR_TRANSPORT_GOOD ) +int i, ret; + + +for (i =0 ; i < retries; i++) { +dev_dbg (dev,"%s\n" ,usb_stor_opcode_name (cmd[0 ])); + ret = us->transport (usb_blkdev, cmd, cmdlen, data, datalen); +dev_dbg (dev,"%s returns %d\n" ,usb_stor_opcode_name (cmd[0 ]), + ret); +if (ret ==USB_STOR_TRANSPORT_GOOD )return 0 ; + +if (request_sense_delay_ms ==USB_STOR_NO_REQUEST_SENSE ) +continue ; +usb_stor_request_sense (usb_blkdev); -mdelay (100 ); - }while (retries--); -return -ENODEV ; +if (request_sense_delay_ms) +mdelay (request_sense_delay_ms); + } + +return -EIO ; +} + +static int usb_stor_inquiry (struct us_blk_dev *usb_blkdev) +{ +struct device_d *dev = &usb_blkdev->us->pusb_dev->dev; +int ret; +u8 cmd[6 ]; +const u32 datalen =36 ; +u8 *data =xzalloc (datalen); + +memset (cmd,0 ,sizeof (cmd)); + cmd[0 ] =SCSI_INQUIRY ; + cmd[3 ] = (u8)(datalen >>8 ); + cmd[4 ] = (u8)(datalen >>0 ); + + ret =usb_stor_transport (usb_blkdev, cmd,sizeof (cmd), data, datalen, +3 ,USB_STOR_NO_REQUEST_SENSE ); +if (ret <0 ) { + ret = -ENODEV ; + goto exit; + } + +dev_dbg (dev,"Peripheral type: %x, removable: %x\n" , + data[0 ], (data[1 ] >>7 )); +dev_dbg (dev,"ISO ver: %x, resp format: %x\n" , + data[2 ], data[3 ]); +dev_dbg (dev,"Vendor/product/rev: %28s\n" , + &data[8 ]); +// TODO: process and store device info + +exit: +free (data); +return ret; +} + +static int usb_stor_test_unit_ready (struct us_blk_dev *usb_blkdev) +{ +u8 cmd[12 ]; +int ret; + +memset (cmd,0 ,sizeof (cmd)); + cmd[0 ] =SCSI_TST_U_RDY ; + + ret =usb_stor_transport (usb_blkdev, cmd,sizeof (cmd),NULL ,0 , +10 ,100 ); +if (ret <0 ) +return -ENODEV ; + +return 0 ; }static int usb_stor_read_capacity (struct us_blk_dev *usb_blkdev,u32 *last_lba,u32 *block_length) { -struct us_data *us = usb_blkdev->us; -struct device_d *dev = &us->pusb_dev->dev; -int retries, result; +struct device_d *dev = &usb_blkdev->us->pusb_dev->dev;const u32 datalen =8 ;u32 *data =xzalloc (datalen);u8 cmd[10 ]; +int ret; - retries =3 ; -do { -dev_dbg (dev,"SCSI_RD_CAPAC\n" ); -memset (cmd,0 ,sizeof (cmd)); - cmd[0 ] =SCSI_RD_CAPAC ; - - result = us->transport (usb_blkdev, cmd,sizeof (cmd), data, - datalen); -dev_dbg (dev,"SCSI_RD_CAPAC returns %d\n" , result); - -if (result ==USB_STOR_TRANSPORT_GOOD ) { -dev_dbg (dev,"Read Capacity returns: 0x%x, 0x%x\n" , - data[0 ], data[1 ]); - *last_lba =be32_to_cpu (data[0 ]); - *block_length =be32_to_cpu (data[1 ]); -break ; - } - }while (retries--); +memset (cmd,0 ,sizeof (cmd)); + cmd[0 ] =SCSI_RD_CAPAC ; -free (data); + ret =usb_stor_transport (usb_blkdev, cmd,sizeof (cmd), data, datalen, +3 ,USB_STOR_NO_REQUEST_SENSE ); +if (ret <0 ) + goto exit; + +dev_dbg (dev,"Read Capacity returns: 0x%x, 0x%x\n" , + data[0 ], data[1 ]); + *last_lba =be32_to_cpu (data[0 ]); + *block_length =be32_to_cpu (data[1 ]); -return result ? -EIO :0 ; +exit: +free (data); +return ret; }static int usb_stor_io_10 (struct us_blk_dev *usb_blkdev,u8 opcode,unsigned long start,u8 *data,unsigned short blocks) { -struct us_data *us = usb_blkdev->us; -int retries, result; -const u32 datalen = blocks *SECTOR_SIZE ;u8 cmd[10 ]; - retries =2 ; -do { -memset (cmd,0 ,sizeof (cmd)); - - cmd[0 ] = opcode; - cmd[2 ] = (u8)(start >>24 ); - cmd[3 ] = (u8)(start >>16 ); - cmd[4 ] = (u8)(start >>8 ); - cmd[5 ] = (u8)(start >>0 ); - cmd[7 ] = (u8)(blocks >>8 ); - cmd[8 ] = (u8)(blocks >>0 ); - result = us->transport (usb_blkdev, cmd,sizeof (cmd), - data, datalen); -if (result ==USB_STOR_TRANSPORT_GOOD ) -return 0 ; -usb_stor_request_sense (usb_blkdev); - }while (retries--); - -return -EIO ; +memset (cmd,0 ,sizeof (cmd)); + cmd[0 ] = opcode; + cmd[2 ] = (u8)(start >>24 ); + cmd[3 ] = (u8)(start >>16 ); + cmd[4 ] = (u8)(start >>8 ); + cmd[5 ] = (u8)(start >>0 ); + cmd[7 ] = (u8)(blocks >>8 ); + cmd[8 ] = (u8)(blocks >>0 ); + +return usb_stor_transport (usb_blkdev, cmd,sizeof (cmd), data, + blocks *SECTOR_SIZE ,2 ,0 ); }/***********************************************************************
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 19/26 usb: storage: Use put_unaligned_be* helpers
In reply to Andrey SmirnovReplace explicit endianness casts with put_unaligned_be* helpers.
--- drivers/usb/storage/usb.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/usb/storage/usb.c +7 -11
@@ -26,6 +26,8 @@#include <usb/usb.h> #include <usb/usb_defs.h> +#include <asm/unaligned.h> +#include "usb.h" #include "transport.h"
@@ -111,13 +113,12 @@ static int usb_stor_inquiry(struct us_blk_dev *usb_blkdev)struct device_d *dev = &usb_blkdev->us->pusb_dev->dev;int ret;u8 cmd[6 ]; -const u32 datalen =36 ; +const u16 datalen =36 ;u8 *data =xzalloc (datalen);memset (cmd,0 ,sizeof (cmd)); cmd[0 ] =SCSI_INQUIRY ; - cmd[3 ] = (u8)(datalen >>8 ); - cmd[4 ] = (u8)(datalen >>0 ); +put_unaligned_be16 (datalen, &cmd[3 ]); ret =usb_stor_transport (usb_blkdev, cmd,sizeof (cmd), data, datalen,3 ,USB_STOR_NO_REQUEST_SENSE );
@@ -183,19 +184,14 @@ exit: }static int usb_stor_io_10 (struct us_blk_dev *usb_blkdev,u8 opcode, -unsigned long start,u8 *data, -unsigned short blocks) +u32 start,u8 *data,u16 blocks) {u8 cmd[10 ];memset (cmd,0 ,sizeof (cmd)); cmd[0 ] = opcode; - cmd[2 ] = (u8)(start >>24 ); - cmd[3 ] = (u8)(start >>16 ); - cmd[4 ] = (u8)(start >>8 ); - cmd[5 ] = (u8)(start >>0 ); - cmd[7 ] = (u8)(blocks >>8 ); - cmd[8 ] = (u8)(blocks >>0 ); +put_unaligned_be32 (start, &cmd[2 ]); +put_unaligned_be16 (blocks, &cmd[7 ]);return usb_stor_transport (usb_blkdev, cmd,sizeof (cmd), data, blocks *SECTOR_SIZE ,2 ,0 );
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 20/26 usb: Drop usb_disable_asynch()
In reply to Andrey SmirnovThere are no users of usb_control_msg() in the codebase that pass timeout of 0, so it doesn't look like usb_disable_asynch() has any effect on USB operation. Drop that function and remove all of its uses to simplify things.
--- drivers/usb/core/usb.c | 24 +++--------------------- drivers/usb/storage/usb.c | 16 ++++------------ include/usb/usb.h | 1 - 3 files changed, 7 insertions(+), 34 deletions(-)
drivers/usb/core/usb.c +3 -21
@@ -63,7 +63,6 @@static int dev_count;static int dev_index; -static int asynch_allowed;static LIST_HEAD (host_list);LIST_HEAD (usb_device_list);
@@ -98,7 +97,6 @@ int usb_register_host(struct usb_host *host)list_add_tail (&host->list, &host_list); host->busnum = host_busnum++; host->sem =0 ; - asynch_allowed =1 ;return 0 ; }
@@ -559,16 +557,6 @@ void usb_rescan(void)pr_info ("%d USB Device(s) found\n" , dev_count); } -/* -* disables the asynch behaviour of the control message. This is used for data -* transfers that uses the exclusiv access to the control and bulk messages. -*/ -void usb_disable_asynch (int disable) -{ - asynch_allowed = !disable; -} - -/*------------------------------------------------------------------- * Message wrappers. *
@@ -597,10 +585,9 @@ int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,/* * submits a control message and waits for completion (at least timeout * 1ms) * If timeout is 0, we don't wait for completion (used as example to set and -* clear keyboards LEDs). For data transfers, (storage transfers) we don't -* allow control messages with 0 timeout, by previously resetting the flag -* asynch_allowed (usb_disable_asynch(1)). -* returns the transfered length if OK or -1 if error. The transfered length +* clear keyboards LEDs). +* +* Returns the transfered length if OK or -1 if error. The transfered length * and the current status are stored in the dev->act_len and dev->status. */ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
@@ -612,11 +599,6 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,int ret;struct devrequest *setup_packet = dev->setup_packet; -if ((timeout ==0 ) && (!asynch_allowed)) { -/* request for a asynch control pipe is not allowed */ -return -1 ; - } - ret =usb_host_acquire (host);if (ret)return ret;
drivers/usb/storage/usb.c +4 -12
@@ -233,13 +233,10 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,return -EINVAL ; } -usb_disable_asynch (1 ); -/* ensure unit ready */ dev_dbg (dev,"Testing for unit ready\n" );if (usb_stor_test_unit_ready (pblk_dev)) {dev_dbg (dev,"Device NOT ready\n" ); -usb_disable_asynch (0 );return -EIO ; }
@@ -277,8 +274,6 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev, sectors_done += n; } -usb_disable_asynch (0 ); -dev_dbg (dev,"Successful I/O of %d blocks\n" , sectors_done);return (sector_count !=0 ) ? -EIO :0 ;
@@ -328,7 +323,6 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)int result =0 ; pblk_dev->blk.num_blocks =0 ; -usb_disable_asynch (1 );/* get device info */ dev_dbg (dev,"Reading device info\n" );
@@ -336,7 +330,7 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev) result =usb_stor_inquiry (pblk_dev);if (result) {dev_dbg (dev,"Cannot read device info\n" ); - goto Exit; +return result; }/* ensure unit ready */
@@ -345,7 +339,7 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev) result =usb_stor_test_unit_ready (pblk_dev);if (result) {dev_dbg (dev,"Device NOT ready\n" ); - goto Exit; +return result; }/* read capacity */
@@ -354,7 +348,7 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev) result =usb_stor_read_capacity (pblk_dev, &last_lba, &block_length);if (result <0 ) {dev_dbg (dev,"Cannot read device capacity\n" ); - goto Exit; +return result; } pblk_dev->blk.num_blocks =usb_limit_blk_cnt (last_lba +1 );
@@ -364,9 +358,7 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)dev_dbg (dev,"Capacity = 0x%x, blockshift = 0x%x\n" , pblk_dev->blk.num_blocks, pblk_dev->blk.blockbits); -Exit: -usb_disable_asynch (0 ); -return result; +return 0 ; }/* Create and register a disk device for the specified LUN */
include/usb/usb.h +0 -1
@@ -172,7 +172,6 @@ int usb_bulk_msg(struct usb_device *dev, unsigned int pipe, void *data, int len, int *actual_length, int timeout);int usb_submit_int_msg (struct usb_device *dev,unsigned long pipe,void *buffer,int transfer_len,int interval); -void usb_disable_asynch (int disable);int usb_maxpacket (struct usb_device *dev,unsigned long pipe);int usb_get_configuration_no (struct usb_device *dev,unsigned char *buffer,int cfgno);
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 21/26 usb: storage: Drop unnecessary check in usb_stor_blk_io()
In reply to Andrey SmirnovChecking that sector_count is zero, shouldn't be necessary since block layer won't call this function if there's no data to be read. Drop it.
Checking that blockbits is eqal to SECTOR_SHIFT isn't necessary, since that field is filled by the driver and is not changed outsied of it. We know it is going to be SECTOR_SHIFT. Drop it.
Checking sector_start > (ulong)-1 doesn't make sense at all since sector start is 'int' and it can't possibly be greater that ULONG_MAX. Drop it.
Checking for sector_start >= pblk_dev->blk.num_blocks isn't necessary either, since we shouldn't receive request for invalid read from block layer. Drop it.
Ditto for sector_count > INT_MAX and sector_start + sector_count > pblk_dev->blk.num_blocks.
--- drivers/usb/storage/usb.c | 27 --------------------------- 1 file changed, 27 deletions(-)
drivers/usb/storage/usb.c +0 -27
@@ -216,23 +216,6 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,struct device_d *dev = &us->pusb_dev->dev;unsigned sectors_done; -if (sector_count ==0 ) -return 0 ; - -/* check for unsupported block size */ -if (pblk_dev->blk.blockbits !=SECTOR_SHIFT ) { -dev_dbg (dev,"%s: unsupported block shift %d\n" , - __func__, pblk_dev->blk.blockbits); -return -EINVAL ; - } - -/* check for invalid sector_start */ -if (sector_start >= pblk_dev->blk.num_blocks || sector_start > (ulong)-1 ) { -dev_dbg (dev,"%s: start sector %d too large\n" , - __func__, sector_start); -return -EINVAL ; - } -/* ensure unit ready */ dev_dbg (dev,"Testing for unit ready\n" );if (usb_stor_test_unit_ready (pblk_dev)) {
@@ -240,16 +223,6 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,return -EIO ; } -/* possibly limit the amount of I/O data */ -if (sector_count >INT_MAX ) { - sector_count =INT_MAX ; -dev_dbg (dev,"Restricting I/O to %u blocks\n" , sector_count); - } -if (sector_start + sector_count > pblk_dev->blk.num_blocks) { - sector_count = pblk_dev->blk.num_blocks - sector_start; -dev_dbg (dev,"Restricting I/O to %u blocks\n" , sector_count); - } -/* read / write the requested data */ dev_dbg(dev, "%s %u block(s), starting from %d\n", (io_op == io_rd) ? "Read" : "Write",
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 22/26 usb: storage: Drop needless macro
In reply to Andrey SmirnovThere's only user of the to_usb_mass_storage() so we may as well inline it.
--- drivers/usb/storage/usb.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/usb/storage/usb.c +3 -3
@@ -203,15 +203,15 @@ static int usb_stor_io_10(struct us_blk_dev *usb_blkdev, u8 opcode,#define US_MAX_IO_BLK 32 -#define to_usb_mass_storage (x) container_of((x), struct us_blk_dev, blk) -enum { io_rd, io_wr };/* Read / write a chunk of sectors on media */ static int usb_stor_blk_io (int io_op,struct block_device *disk_dev,int sector_start,int sector_count,void *buffer) { -struct us_blk_dev *pblk_dev =to_usb_mass_storage (disk_dev); +struct us_blk_dev *pblk_dev =container_of (disk_dev, + struct us_blk_dev, + blk);struct us_data *us = pblk_dev->us;struct device_d *dev = &us->pusb_dev->dev;unsigned sectors_done;
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 23/26 usb: storage: Use simple boolean to speficy read vs. write operation
In reply to Andrey SmirnovUse simple boolean to speficy read vs. write operation usb_stor_blk_io() instead of having a custom anonymous enum.
--- drivers/usb/storage/usb.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/usb/storage/usb.c +8 -10
@@ -203,11 +203,10 @@ static int usb_stor_io_10(struct us_blk_dev *usb_blkdev, u8 opcode,#define US_MAX_IO_BLK 32 -enum { io_rd, io_wr }; -/* Read / write a chunk of sectors on media */ -static int usb_stor_blk_io (int io_op,struct block_device *disk_dev, -int sector_start,int sector_count,void *buffer) +static int usb_stor_blk_io (struct block_device *disk_dev, +int sector_start,int sector_count,void *buffer, +bool read) { struct us_blk_dev *pblk_dev = container_of(disk_dev, struct us_blk_dev,
@@ -225,8 +224,8 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,/* read / write the requested data */ dev_dbg (dev,"%s %u block(s), starting from %d\n" , - (io_op == io_rd) ?"Read" :"Write" , - sector_count, sector_start); + read ?"Read" :"Write" , + sector_count, sector_start); sectors_done =0 ;while (sector_count >0 ) {int result;
@@ -234,8 +233,7 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,u8 *data = buffer + (sectors_done *SECTOR_SIZE ); result =usb_stor_io_10 (pblk_dev, - (io_op == io_rd) ? -SCSI_READ10 :SCSI_WRITE10 , + read ?SCSI_READ10 :SCSI_WRITE10 , (ulong )sector_start, data, n);if (result !=0 ) {
@@ -256,14 +254,14 @@ static int usb_stor_blk_io(int io_op, struct block_device *disk_dev,static int __maybe_unused usb_stor_blk_write (struct block_device *blk,const void *buffer,int block,int num_blocks) { -return usb_stor_blk_io (io_wr, blk, block, num_blocks, (void *)buffer); +return usb_stor_blk_io (blk, block, num_blocks, (void *)buffer, false); }/* Read a chunk of sectors from media */ static int usb_stor_blk_read (struct block_device *blk,void *buffer,int block,int num_blocks) { -return usb_stor_blk_io (io_rd, blk, block, num_blocks, buffer); +return usb_stor_blk_io (blk, block, num_blocks, buffer, true); }static struct block_device_ops usb_mass_storage_ops = {
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 24/26 usb: storage: Simplify I/O loop in usb_stor_blk_io()
In reply to Andrey SmirnovSimplify I/O loop a bit, by re-arranging things and dropping "sectors_done", "result" and "data" local variables.
--- drivers/usb/storage/usb.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
drivers/usb/storage/usb.c +7 -13
@@ -213,7 +213,6 @@ static int usb_stor_blk_io(struct block_device *disk_dev, blk);struct us_data *us = pblk_dev->us;struct device_d *dev = &us->pusb_dev->dev; -unsigned sectors_done;/* ensure unit ready */ dev_dbg (dev,"Testing for unit ready\n" );
@@ -226,28 +225,23 @@ static int usb_stor_blk_io(struct block_device *disk_dev,dev_dbg (dev,"%s %u block(s), starting from %d\n" , read ?"Read" :"Write" , sector_count, sector_start); - sectors_done =0 ; +while (sector_count >0 ) { -int result;unsigned n =min (sector_count,US_MAX_IO_BLK ); -u8 *data = buffer + (sectors_done *SECTOR_SIZE ); - result =usb_stor_io_10 (pblk_dev, - read ?SCSI_READ10 :SCSI_WRITE10 , - (ulong )sector_start, - data, n); -if (result !=0 ) { +if (usb_stor_io_10 (pblk_dev, + read ?SCSI_READ10 :SCSI_WRITE10 , + sector_start, + buffer, n)) {dev_dbg (dev,"I/O error at sector %d\n" , sector_start);break ; } sector_start += n; sector_count -= n; - sectors_done += n; + buffer += n *SECTOR_SIZE ; } -dev_dbg (dev,"Successful I/O of %d blocks\n" , sectors_done); - -return (sector_count !=0 ) ? -EIO :0 ; +return sector_count ? -EIO :0 ; }/* Write a chunk of sectors to media */
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 25/26 usb: storage: Drop unnecessary assignment
In reply to Andrey Smirnov--- drivers/usb/storage/usb.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/usb/storage/usb.c +1 -3
@@ -285,9 +285,7 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)struct us_data *us = pblk_dev->us;struct device_d *dev = &us->pusb_dev->dev;u32 last_lba =0 , block_length =0 ; -int result =0 ; - - pblk_dev->blk.num_blocks =0 ; +int result;/* get device info */ dev_dbg (dev,"Reading device info\n" );
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 26/26 usb: storage: Inline usb_limit_blk_cnt()
In reply to Andrey SmirnovUsb_limit_blk_cnt() has only one user so we may as well inline it.
--- drivers/usb/storage/usb.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
drivers/usb/storage/usb.c +7 -11
@@ -269,16 +269,6 @@ static struct block_device_ops usb_mass_storage_ops = {* Block device routines ***********************************************************************/ -static int usb_limit_blk_cnt (unsigned cnt) -{ -if (cnt >0x7fffffff ) { -pr_warn ("Limiting device size due to 31 bit contraints\n" ); -return 0x7fffffff ; - } - -return (int )cnt; -} -/* Prepare a disk device */ static int usb_stor_init_blkdev (struct us_blk_dev *pblk_dev) {
@@ -314,7 +304,13 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)return result; } - pblk_dev->blk.num_blocks =usb_limit_blk_cnt (last_lba +1 ); +if (last_lba >INT_MAX -1 ) { + last_lba =INT_MAX -1 ; +dev_warn (dev, +"Limiting device size due to 31 bit contraints\n" ); + } + + pblk_dev->blk.num_blocks = last_lba +1 ;if (block_length !=SECTOR_SIZE )pr_warn ("Support only %d bytes sectors\n" ,SECTOR_SIZE ); pblk_dev->blk.blockbits =SECTOR_SHIFT ;
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Sascha Hauer
AppliedQuoted 9 lines · Andrey Smirnov
Everyone:
While creating fix found in "usb: storage: Don't use "unsigned long" for 32-bit values" I spent some time reading code of our USB storage implementation. The other 25 patches is the result of that effort and my attempt to improve/simplify that codebase. Hopefuly each patch is self-explanatory.
Feedback is welcome!
Applied, thanks
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox