1 // SPDX-License-Identifier: GPL-2.0+
3 * Most of this source has been derived from the Linux USB
5 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
6 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
7 * (c) 1999 Michael Gee (michael@linuxspecific.com)
8 * (c) 2000 Yggdrasil Computing, Inc.
12 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
13 * Driver model conversion:
14 * (C) Copyright 2015 Google, Inc
16 * For BBB support (C) Copyright 2003
17 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
19 * BBB support based on /sys/dev/usb/umass.c from
24 * Currently only the CBI transport protocoll has been implemented, and it
25 * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
26 * transport protocoll may work as well.
30 * Support for USB Mass Storage Devices (BBB) has been added. It has
31 * only been tested with USB memory sticks.
43 #include <asm/byteorder.h>
44 #include <asm/cache.h>
45 #include <asm/processor.h>
46 #include <dm/device-internal.h>
48 #include <linux/delay.h>
53 #undef BBB_COMDAT_TRACE
54 #undef BBB_XPORT_TRACE
57 /* direction table -- this indicates the direction of the data
58 * transfer for each command code -- a 1 indicates input
60 static const unsigned char us_direction[256/8] = {
61 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
62 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
66 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
68 static struct scsi_cmd usb_ccb __aligned(ARCH_DMA_MINALIGN);
71 static int usb_max_devs; /* number of highest available usb device */
73 #if !CONFIG_IS_ENABLED(BLK)
74 static struct blk_desc usb_dev_desc[USB_MAX_STOR_DEV];
78 typedef int (*trans_cmnd)(struct scsi_cmd *cb, struct us_data *data);
79 typedef int (*trans_reset)(struct us_data *data);
82 struct usb_device *pusb_dev; /* this usb_device */
84 unsigned int flags; /* from filter initially */
85 # define USB_READY (1 << 0)
86 unsigned char ifnum; /* interface number */
87 unsigned char ep_in; /* in endpoint */
88 unsigned char ep_out; /* out ....... */
89 unsigned char ep_int; /* interrupt . */
90 unsigned char subclass; /* as in overview */
91 unsigned char protocol; /* .............. */
92 unsigned char attention_done; /* force attn on first cmd */
93 unsigned short ip_data; /* interrupt data */
94 int action; /* what to do */
95 int ip_wanted; /* needed */
96 int *irq_handle; /* for USB int requests */
97 unsigned int irqpipe; /* pipe for release_irq */
98 unsigned char irqmaxp; /* max packed for irq Pipe */
99 unsigned char irqinterval; /* Intervall for IRQ Pipe */
100 struct scsi_cmd *srb; /* current srb */
101 trans_reset transport_reset; /* reset routine */
102 trans_cmnd transport; /* transport routine */
103 unsigned short max_xfer_blk; /* maximum transfer blocks */
106 #if !CONFIG_IS_ENABLED(BLK)
107 static struct us_data usb_stor[USB_MAX_STOR_DEV];
110 #define USB_STOR_TRANSPORT_GOOD 0
111 #define USB_STOR_TRANSPORT_FAILED -1
112 #define USB_STOR_TRANSPORT_ERROR -2
114 int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
115 struct blk_desc *dev_desc);
116 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
118 #if CONFIG_IS_ENABLED(BLK)
119 static unsigned long usb_stor_read(struct udevice *dev, lbaint_t blknr,
120 lbaint_t blkcnt, void *buffer);
121 static unsigned long usb_stor_write(struct udevice *dev, lbaint_t blknr,
122 lbaint_t blkcnt, const void *buffer);
124 static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
125 lbaint_t blkcnt, void *buffer);
126 static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr,
127 lbaint_t blkcnt, const void *buffer);
129 void uhci_show_temp_int_td(void);
131 static void usb_show_progress(void)
136 /*******************************************************************************
137 * show info on storage devices; 'usb start/init' must be invoked earlier
138 * as we only retrieve structures populated during devices initialization
140 int usb_stor_info(void)
143 #if CONFIG_IS_ENABLED(BLK)
146 for (blk_first_device(IF_TYPE_USB, &dev);
148 blk_next_device(&dev)) {
149 struct blk_desc *desc = dev_get_uclass_plat(dev);
151 printf(" Device %d: ", desc->devnum);
158 if (usb_max_devs > 0) {
159 for (i = 0; i < usb_max_devs; i++) {
160 printf(" Device %d: ", i);
161 dev_print(&usb_dev_desc[i]);
167 printf("No storage devices, perhaps not 'usb start'ed..?\n");
174 static unsigned int usb_get_max_lun(struct us_data *us)
177 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
178 len = usb_control_msg(us->pusb_dev,
179 usb_rcvctrlpipe(us->pusb_dev, 0),
181 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
183 result, sizeof(char),
184 USB_CNTL_TIMEOUT * 5);
185 debug("Get Max LUN -> len = %i, result = %i\n", len, (int) *result);
186 return (len > 0) ? *result : 0;
189 static int usb_stor_probe_device(struct usb_device *udev)
193 #if CONFIG_IS_ENABLED(BLK)
194 struct us_data *data;
200 return -ENOENT; /* no more devices available */
203 debug("\n\nProbing for storage\n");
204 #if CONFIG_IS_ENABLED(BLK)
206 * We store the us_data in the mass storage device's plat. It
207 * is shared by all LUNs (block devices) attached to this mass storage
210 data = dev_get_plat(udev->dev);
211 if (!usb_storage_probe(udev, 0, data))
213 max_lun = usb_get_max_lun(data);
214 for (lun = 0; lun <= max_lun; lun++) {
215 struct blk_desc *blkdev;
219 snprintf(str, sizeof(str), "lun%d", lun);
220 ret = blk_create_devicef(udev->dev, "usb_storage_blk", str,
221 IF_TYPE_USB, usb_max_devs, 512, 0,
224 debug("Cannot bind driver\n");
228 blkdev = dev_get_uclass_plat(dev);
229 blkdev->target = 0xff;
232 ret = usb_stor_get_info(udev, data, blkdev);
235 debug("%s: Found device %p\n", __func__, udev);
237 debug("usb_stor_get_info: Invalid device\n");
238 ret = device_unbind(dev);
243 ret = blk_probe_or_unbind(dev);
248 /* We don't have space to even probe if we hit the maximum */
249 if (usb_max_devs == USB_MAX_STOR_DEV) {
250 printf("max USB Storage Device reached: %d stopping\n",
255 if (!usb_storage_probe(udev, 0, &usb_stor[usb_max_devs]))
259 * OK, it's a storage device. Iterate over its LUNs and populate
262 start = usb_max_devs;
264 max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
265 for (lun = 0; lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
267 struct blk_desc *blkdev;
269 blkdev = &usb_dev_desc[usb_max_devs];
270 memset(blkdev, '\0', sizeof(struct blk_desc));
271 blkdev->if_type = IF_TYPE_USB;
272 blkdev->devnum = usb_max_devs;
273 blkdev->part_type = PART_TYPE_UNKNOWN;
274 blkdev->target = 0xff;
275 blkdev->type = DEV_TYPE_UNKNOWN;
276 blkdev->block_read = usb_stor_read;
277 blkdev->block_write = usb_stor_write;
281 if (usb_stor_get_info(udev, &usb_stor[start],
282 &usb_dev_desc[usb_max_devs]) == 1) {
283 debug("partype: %d\n", blkdev->part_type);
285 debug("partype: %d\n", blkdev->part_type);
287 debug("%s: Found device %p\n", __func__, udev);
295 void usb_stor_reset(void)
300 /*******************************************************************************
301 * scan the usb and reports device info
302 * to the user if mode = 1
303 * returns current device or -1 if no
305 int usb_stor_scan(int mode)
308 printf(" scanning usb for storage devices... ");
310 #if !CONFIG_IS_ENABLED(DM_USB)
313 usb_disable_asynch(1); /* asynch transfer not allowed */
316 for (i = 0; i < USB_MAX_DEVICE; i++) {
317 struct usb_device *dev;
319 dev = usb_get_dev_index(i); /* get device */
321 if (usb_stor_probe_device(dev))
325 usb_disable_asynch(0); /* asynch transfer allowed */
327 printf("%d Storage Device(s) found\n", usb_max_devs);
328 if (usb_max_devs > 0)
333 static int usb_stor_irq(struct usb_device *dev)
336 us = (struct us_data *)dev->privptr;
346 static void usb_show_srb(struct scsi_cmd *pccb)
349 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
350 for (i = 0; i < 12; i++)
351 printf("%02X ", pccb->cmd[i]);
355 static void display_int_status(unsigned long tmp)
357 printf("Status: %s %s %s %s %s %s %s\n",
358 (tmp & USB_ST_ACTIVE) ? "Active" : "",
359 (tmp & USB_ST_STALLED) ? "Stalled" : "",
360 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
361 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
362 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
363 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
364 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
367 /***********************************************************************
368 * Data transfer routines
369 ***********************************************************************/
371 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
380 /* determine the maximum packet size for these transfers */
381 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
383 /* while we have data left to transfer */
386 /* calculate how long this will be -- maximum or a remainder */
387 this_xfer = length > max_size ? max_size : length;
390 /* setup the retry counter */
393 /* set up the transfer loop */
395 /* transfer the data */
396 debug("Bulk xfer 0x%lx(%d) try #%d\n",
397 (ulong)map_to_sysmem(buf), this_xfer,
399 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
401 USB_CNTL_TIMEOUT * 5);
402 debug("bulk_msg returned %d xferred %d/%d\n",
403 result, partial, this_xfer);
404 if (us->pusb_dev->status != 0) {
405 /* if we stall, we need to clear it before
409 display_int_status(us->pusb_dev->status);
411 if (us->pusb_dev->status & USB_ST_STALLED) {
412 debug("stalled ->clearing endpoint" \
413 "halt for pipe 0x%x\n", pipe);
414 stat = us->pusb_dev->status;
415 usb_clear_halt(us->pusb_dev, pipe);
416 us->pusb_dev->status = stat;
417 if (this_xfer == partial) {
418 debug("bulk transferred" \
421 us->pusb_dev->status);
427 if (us->pusb_dev->status & USB_ST_NAK_REC) {
428 debug("Device NAKed bulk_msg\n");
431 debug("bulk transferred with error");
432 if (this_xfer == partial) {
433 debug(" %ld, but data ok\n",
434 us->pusb_dev->status);
437 /* if our try counter reaches 0, bail out */
438 debug(" %ld, data %d\n",
439 us->pusb_dev->status, partial);
443 /* update to show what data was transferred */
444 this_xfer -= partial;
446 /* continue until this transfer is done */
450 /* if we get here, we're done and successful */
454 static int usb_stor_BBB_reset(struct us_data *us)
460 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
462 * For Reset Recovery the host shall issue in the following order:
463 * a) a Bulk-Only Mass Storage Reset
464 * b) a Clear Feature HALT to the Bulk-In endpoint
465 * c) a Clear Feature HALT to the Bulk-Out endpoint
467 * This is done in 3 steps.
469 * If the reset doesn't succeed, the device should be port reset.
471 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
473 debug("BBB_reset\n");
474 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
476 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
477 0, us->ifnum, NULL, 0, USB_CNTL_TIMEOUT * 5);
479 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
480 debug("RESET:stall\n");
484 /* long wait for reset */
486 debug("BBB_reset result %d: status %lX reset\n",
487 result, us->pusb_dev->status);
488 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
489 result = usb_clear_halt(us->pusb_dev, pipe);
490 /* long wait for reset */
492 debug("BBB_reset result %d: status %lX clearing IN endpoint\n",
493 result, us->pusb_dev->status);
494 /* long wait for reset */
495 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
496 result = usb_clear_halt(us->pusb_dev, pipe);
498 debug("BBB_reset result %d: status %lX clearing OUT endpoint\n",
499 result, us->pusb_dev->status);
500 debug("BBB_reset done\n");
504 /* FIXME: this reset function doesn't really reset the port, and it
505 * should. Actually it should probably do what it's doing here, and
506 * reset the port physically
508 static int usb_stor_CB_reset(struct us_data *us)
510 unsigned char cmd[12];
514 memset(cmd, 0xff, sizeof(cmd));
515 cmd[0] = SCSI_SEND_DIAG;
517 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
519 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
520 0, us->ifnum, cmd, sizeof(cmd),
521 USB_CNTL_TIMEOUT * 5);
523 /* long wait for reset */
525 debug("CB_reset result %d: status %lX clearing endpoint halt\n",
526 result, us->pusb_dev->status);
527 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
528 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
530 debug("CB_reset done\n");
535 * Set up the command for a BBB device. Note that the actual SCSI
536 * command is copied into cbw.CBWCDB.
538 static int usb_stor_BBB_comdat(struct scsi_cmd *srb, struct us_data *us)
544 ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_cbw, cbw, 1);
546 dir_in = US_DIRECTION(srb->cmd[0]);
548 #ifdef BBB_COMDAT_TRACE
549 printf("dir %d lun %d cmdlen %d cmd %p datalen %lu pdata %p\n",
550 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
553 for (result = 0; result < srb->cmdlen; result++)
554 printf("cmd[%d] %#x ", result, srb->cmd[result]);
559 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
560 debug("usb_stor_BBB_comdat:cmdlen too large\n");
564 /* always OUT to the ep */
565 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
567 cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE);
568 cbw->dCBWTag = cpu_to_le32(CBWTag++);
569 cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen);
570 cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
571 cbw->bCBWLUN = srb->lun;
572 cbw->bCDBLength = srb->cmdlen;
573 /* copy the command data into the CBW command data buffer */
576 memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
577 result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
578 &actlen, USB_CNTL_TIMEOUT * 5);
580 debug("usb_stor_BBB_comdat:usb_bulk_msg error\n");
584 /* FIXME: we also need a CBI_command which sets up the completion
585 * interrupt, and waits for it
587 static int usb_stor_CB_comdat(struct scsi_cmd *srb, struct us_data *us)
592 unsigned long status;
595 dir_in = US_DIRECTION(srb->cmd[0]);
598 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
600 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
603 debug("CBI gets a command: Try %d\n", 5 - retry);
607 /* let's send the command via the control pipe */
608 result = usb_control_msg(us->pusb_dev,
609 usb_sndctrlpipe(us->pusb_dev , 0),
611 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
613 srb->cmd, srb->cmdlen,
614 USB_CNTL_TIMEOUT * 5);
615 debug("CB_transport: control msg returned %d, status %lX\n",
616 result, us->pusb_dev->status);
617 /* check the return code for the command */
619 if (us->pusb_dev->status & USB_ST_STALLED) {
620 status = us->pusb_dev->status;
621 debug(" stall during command found," \
623 usb_clear_halt(us->pusb_dev,
624 usb_sndctrlpipe(us->pusb_dev, 0));
625 us->pusb_dev->status = status;
627 debug(" error during command %02X" \
628 " Stat = %lX\n", srb->cmd[0],
629 us->pusb_dev->status);
632 /* transfer the data payload for this command, if one exists*/
634 debug("CB_transport: control msg returned %d," \
635 " direction is %s to go 0x%lx\n", result,
636 dir_in ? "IN" : "OUT", srb->datalen);
638 result = us_one_transfer(us, pipe, (char *)srb->pdata,
640 debug("CBI attempted to transfer data," \
641 " result is %d status %lX, len %d\n",
642 result, us->pusb_dev->status,
643 us->pusb_dev->act_len);
644 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
646 } /* if (srb->datalen) */
656 static int usb_stor_CBI_get_status(struct scsi_cmd *srb, struct us_data *us)
661 usb_int_msg(us->pusb_dev, us->irqpipe,
662 (void *)&us->ip_data, us->irqmaxp, us->irqinterval, false);
665 if (us->ip_wanted == 0)
670 printf(" Did not get interrupt on CBI\n");
672 return USB_STOR_TRANSPORT_ERROR;
674 debug("Got interrupt data 0x%x, transferred %d status 0x%lX\n",
675 us->ip_data, us->pusb_dev->irq_act_len,
676 us->pusb_dev->irq_status);
677 /* UFI gives us ASC and ASCQ, like a request sense */
678 if (us->subclass == US_SC_UFI) {
679 if (srb->cmd[0] == SCSI_REQ_SENSE ||
680 srb->cmd[0] == SCSI_INQUIRY)
681 return USB_STOR_TRANSPORT_GOOD; /* Good */
682 else if (us->ip_data)
683 return USB_STOR_TRANSPORT_FAILED;
685 return USB_STOR_TRANSPORT_GOOD;
687 /* otherwise, we interpret the data normally */
688 switch (us->ip_data) {
690 return USB_STOR_TRANSPORT_GOOD;
692 return USB_STOR_TRANSPORT_FAILED;
694 return USB_STOR_TRANSPORT_ERROR;
696 return USB_STOR_TRANSPORT_ERROR;
699 #define USB_TRANSPORT_UNKNOWN_RETRY 5
700 #define USB_TRANSPORT_NOT_READY_RETRY 10
702 /* clear a stall on an endpoint - special for BBB devices */
703 static int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
705 /* ENDPOINT_HALT = 0, so set value to 0 */
706 return usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
707 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
708 endpt, NULL, 0, USB_CNTL_TIMEOUT * 5);
711 static int usb_stor_BBB_transport(struct scsi_cmd *srb, struct us_data *us)
715 int actlen, data_actlen;
716 unsigned int pipe, pipein, pipeout;
717 ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_csw, csw, 1);
718 #ifdef BBB_XPORT_TRACE
723 dir_in = US_DIRECTION(srb->cmd[0]);
726 debug("COMMAND phase\n");
727 result = usb_stor_BBB_comdat(srb, us);
729 debug("failed to send CBW status %ld\n",
730 us->pusb_dev->status);
731 usb_stor_BBB_reset(us);
732 return USB_STOR_TRANSPORT_FAILED;
734 if (!(us->flags & USB_READY))
736 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
737 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
738 /* DATA phase + error handling */
740 /* no data, go immediately to the STATUS phase */
741 if (srb->datalen == 0)
743 debug("DATA phase\n");
749 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
750 &data_actlen, USB_CNTL_TIMEOUT * 5);
751 /* special handling of STALL in DATA phase */
752 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
753 debug("DATA:stall\n");
754 /* clear the STALL on the endpoint */
755 result = usb_stor_BBB_clear_endpt_stall(us,
756 dir_in ? us->ep_in : us->ep_out);
758 /* continue on to STATUS phase */
762 debug("usb_bulk_msg error status %ld\n",
763 us->pusb_dev->status);
764 usb_stor_BBB_reset(us);
765 return USB_STOR_TRANSPORT_FAILED;
767 #ifdef BBB_XPORT_TRACE
768 for (index = 0; index < data_actlen; index++)
769 printf("pdata[%d] %#x ", index, srb->pdata[index]);
772 /* STATUS phase + error handling */
776 debug("STATUS phase\n");
777 result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE,
778 &actlen, USB_CNTL_TIMEOUT*5);
780 /* special handling of STALL in STATUS phase */
781 if ((result < 0) && (retry < 1) &&
782 (us->pusb_dev->status & USB_ST_STALLED)) {
783 debug("STATUS:stall\n");
784 /* clear the STALL on the endpoint */
785 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
786 if (result >= 0 && (retry++ < 1))
791 debug("usb_bulk_msg error status %ld\n",
792 us->pusb_dev->status);
793 usb_stor_BBB_reset(us);
794 return USB_STOR_TRANSPORT_FAILED;
796 #ifdef BBB_XPORT_TRACE
797 ptr = (unsigned char *)csw;
798 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
799 printf("ptr[%d] %#x ", index, ptr[index]);
802 /* misuse pipe to get the residue */
803 pipe = le32_to_cpu(csw->dCSWDataResidue);
804 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
805 pipe = srb->datalen - data_actlen;
806 if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) {
807 debug("!CSWSIGNATURE\n");
808 usb_stor_BBB_reset(us);
809 return USB_STOR_TRANSPORT_FAILED;
810 } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) {
812 usb_stor_BBB_reset(us);
813 return USB_STOR_TRANSPORT_FAILED;
814 } else if (csw->bCSWStatus > CSWSTATUS_PHASE) {
816 usb_stor_BBB_reset(us);
817 return USB_STOR_TRANSPORT_FAILED;
818 } else if (csw->bCSWStatus == CSWSTATUS_PHASE) {
820 usb_stor_BBB_reset(us);
821 return USB_STOR_TRANSPORT_FAILED;
822 } else if (data_actlen > srb->datalen) {
823 debug("transferred %dB instead of %ldB\n",
824 data_actlen, srb->datalen);
825 return USB_STOR_TRANSPORT_FAILED;
826 } else if (csw->bCSWStatus == CSWSTATUS_FAILED) {
828 return USB_STOR_TRANSPORT_FAILED;
834 static int usb_stor_CB_transport(struct scsi_cmd *srb, struct us_data *us)
837 struct scsi_cmd *psrb;
838 struct scsi_cmd reqsrb;
842 status = USB_STOR_TRANSPORT_GOOD;
845 /* issue the command */
847 result = usb_stor_CB_comdat(srb, us);
848 debug("command / Data returned %d, status %lX\n",
849 result, us->pusb_dev->status);
850 /* if this is an CBI Protocol, get IRQ */
851 if (us->protocol == US_PR_CBI) {
852 status = usb_stor_CBI_get_status(srb, us);
853 /* if the status is error, report it */
854 if (status == USB_STOR_TRANSPORT_ERROR) {
855 debug(" USB CBI Command Error\n");
858 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
859 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
861 /* if the status is good, report it */
862 if (status == USB_STOR_TRANSPORT_GOOD) {
863 debug(" USB CBI Command Good\n");
868 /* do we have to issue an auto request? */
869 /* HERE we have to check the result */
870 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
871 debug("ERROR %lX\n", us->pusb_dev->status);
872 us->transport_reset(us);
873 return USB_STOR_TRANSPORT_ERROR;
875 if ((us->protocol == US_PR_CBI) &&
876 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
877 (srb->cmd[0] == SCSI_INQUIRY))) {
878 /* do not issue an autorequest after request sense */
879 debug("No auto request and good\n");
880 return USB_STOR_TRANSPORT_GOOD;
882 /* issue an request_sense */
883 memset(&psrb->cmd[0], 0, 12);
884 psrb->cmd[0] = SCSI_REQ_SENSE;
885 psrb->cmd[1] = srb->lun << 5;
888 psrb->pdata = &srb->sense_buf[0];
890 /* issue the command */
891 result = usb_stor_CB_comdat(psrb, us);
892 debug("auto request returned %d\n", result);
893 /* if this is an CBI Protocol, get IRQ */
894 if (us->protocol == US_PR_CBI)
895 status = usb_stor_CBI_get_status(psrb, us);
897 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
898 debug(" AUTO REQUEST ERROR %ld\n",
899 us->pusb_dev->status);
900 return USB_STOR_TRANSPORT_ERROR;
902 debug("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
903 srb->sense_buf[0], srb->sense_buf[2],
904 srb->sense_buf[12], srb->sense_buf[13]);
905 /* Check the auto request result */
906 if ((srb->sense_buf[2] == 0) &&
907 (srb->sense_buf[12] == 0) &&
908 (srb->sense_buf[13] == 0)) {
910 return USB_STOR_TRANSPORT_GOOD;
913 /* Check the auto request result */
914 switch (srb->sense_buf[2]) {
916 /* Recovered Error */
917 return USB_STOR_TRANSPORT_GOOD;
921 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
922 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
923 " 0x%02X (NOT READY)\n", srb->cmd[0],
924 srb->sense_buf[0], srb->sense_buf[2],
925 srb->sense_buf[12], srb->sense_buf[13]);
926 return USB_STOR_TRANSPORT_FAILED;
933 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
934 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
935 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
936 srb->sense_buf[2], srb->sense_buf[12],
938 return USB_STOR_TRANSPORT_FAILED;
943 return USB_STOR_TRANSPORT_FAILED;
946 static void usb_stor_set_max_xfer_blk(struct usb_device *udev,
950 * Limit the total size of a transfer to 120 KB.
952 * Some devices are known to choke with anything larger. It seems like
953 * the problem stems from the fact that original IDE controllers had
954 * only an 8-bit register to hold the number of sectors in one transfer
955 * and even those couldn't handle a full 256 sectors.
957 * Because we want to make sure we interoperate with as many devices as
958 * possible, we will maintain a 240 sector transfer size limit for USB
959 * Mass Storage devices.
961 * Tests show that other operating have similar limits with Microsoft
962 * Windows 7 limiting transfers to 128 sectors for both USB2 and USB3
963 * and Apple Mac OS X 10.11 limiting transfers to 256 sectors for USB2
964 * and 2048 for USB3 devices.
966 unsigned short blk = 240;
968 #if CONFIG_IS_ENABLED(DM_USB)
972 ret = usb_get_max_xfer_size(udev, (size_t *)&size);
973 if ((ret >= 0) && (size < blk * 512))
977 us->max_xfer_blk = blk;
980 static int usb_inquiry(struct scsi_cmd *srb, struct us_data *ss)
985 memset(&srb->cmd[0], 0, 12);
986 srb->cmd[0] = SCSI_INQUIRY;
987 srb->cmd[1] = srb->lun << 5;
991 i = ss->transport(srb, ss);
992 debug("inquiry returns %d\n", i);
998 printf("error in inquiry\n");
1004 static int usb_request_sense(struct scsi_cmd *srb, struct us_data *ss)
1008 ptr = (char *)srb->pdata;
1009 memset(&srb->cmd[0], 0, 12);
1010 srb->cmd[0] = SCSI_REQ_SENSE;
1011 srb->cmd[1] = srb->lun << 5;
1014 srb->pdata = &srb->sense_buf[0];
1016 ss->transport(srb, ss);
1017 debug("Request Sense returned %02X %02X %02X\n",
1018 srb->sense_buf[2], srb->sense_buf[12],
1019 srb->sense_buf[13]);
1020 srb->pdata = (uchar *)ptr;
1024 static int usb_test_unit_ready(struct scsi_cmd *srb, struct us_data *ss)
1029 memset(&srb->cmd[0], 0, 12);
1030 srb->cmd[0] = SCSI_TST_U_RDY;
1031 srb->cmd[1] = srb->lun << 5;
1034 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD) {
1035 ss->flags |= USB_READY;
1038 usb_request_sense(srb, ss);
1040 * Check the Key Code Qualifier, if it matches
1041 * "Not Ready - medium not present"
1042 * (the sense Key equals 0x2 and the ASC is 0x3a)
1043 * return immediately as the medium being absent won't change
1044 * unless there is a user action.
1046 if ((srb->sense_buf[2] == 0x02) &&
1047 (srb->sense_buf[12] == 0x3a))
1050 } while (retries--);
1055 static int usb_read_capacity(struct scsi_cmd *srb, struct us_data *ss)
1061 memset(&srb->cmd[0], 0, 12);
1062 srb->cmd[0] = SCSI_RD_CAPAC;
1063 srb->cmd[1] = srb->lun << 5;
1066 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
1073 static int usb_read_10(struct scsi_cmd *srb, struct us_data *ss,
1074 unsigned long start, unsigned short blocks)
1076 memset(&srb->cmd[0], 0, 12);
1077 srb->cmd[0] = SCSI_READ10;
1078 srb->cmd[1] = srb->lun << 5;
1079 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1080 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1081 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1082 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1083 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1084 srb->cmd[8] = (unsigned char) blocks & 0xff;
1086 debug("read10: start %lx blocks %x\n", start, blocks);
1087 return ss->transport(srb, ss);
1090 static int usb_write_10(struct scsi_cmd *srb, struct us_data *ss,
1091 unsigned long start, unsigned short blocks)
1093 memset(&srb->cmd[0], 0, 12);
1094 srb->cmd[0] = SCSI_WRITE10;
1095 srb->cmd[1] = srb->lun << 5;
1096 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1097 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1098 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1099 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1100 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1101 srb->cmd[8] = (unsigned char) blocks & 0xff;
1103 debug("write10: start %lx blocks %x\n", start, blocks);
1104 return ss->transport(srb, ss);
1108 #ifdef CONFIG_USB_BIN_FIXUP
1110 * Some USB storage devices queried for SCSI identification data respond with
1111 * binary strings, which if output to the console freeze the terminal. The
1112 * workaround is to modify the vendor and product strings read from such
1113 * device with proper values (as reported by 'usb info').
1115 * Vendor and product length limits are taken from the definition of
1116 * struct blk_desc in include/part.h.
1118 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
1119 unsigned char vendor[],
1120 unsigned char product[]) {
1121 const unsigned char max_vendor_len = 40;
1122 const unsigned char max_product_len = 20;
1123 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
1124 strncpy((char *)vendor, "SMSC", max_vendor_len);
1125 strncpy((char *)product, "Flash Media Cntrller",
1129 #endif /* CONFIG_USB_BIN_FIXUP */
1131 #if CONFIG_IS_ENABLED(BLK)
1132 static unsigned long usb_stor_read(struct udevice *dev, lbaint_t blknr,
1133 lbaint_t blkcnt, void *buffer)
1135 static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
1136 lbaint_t blkcnt, void *buffer)
1139 lbaint_t start, blks;
1141 unsigned short smallblks;
1142 struct usb_device *udev;
1145 struct scsi_cmd *srb = &usb_ccb;
1146 #if CONFIG_IS_ENABLED(BLK)
1147 struct blk_desc *block_dev;
1153 #if CONFIG_IS_ENABLED(BLK)
1154 block_dev = dev_get_uclass_plat(dev);
1155 udev = dev_get_parent_priv(dev_get_parent(dev));
1156 debug("\nusb_read: udev %d\n", block_dev->devnum);
1158 debug("\nusb_read: udev %d\n", block_dev->devnum);
1159 udev = usb_dev_desc[block_dev->devnum].priv;
1161 debug("%s: No device\n", __func__);
1165 ss = (struct us_data *)udev->privptr;
1167 usb_disable_asynch(1); /* asynch transfer not allowed */
1168 usb_lock_async(udev, 1);
1169 srb->lun = block_dev->lun;
1170 buf_addr = (uintptr_t)buffer;
1174 debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
1175 block_dev->devnum, start, blks, buf_addr);
1178 /* XXX need some comment here */
1180 srb->pdata = (unsigned char *)buf_addr;
1181 if (blks > ss->max_xfer_blk)
1182 smallblks = ss->max_xfer_blk;
1184 smallblks = (unsigned short) blks;
1186 if (smallblks == ss->max_xfer_blk)
1187 usb_show_progress();
1188 srb->datalen = block_dev->blksz * smallblks;
1189 srb->pdata = (unsigned char *)buf_addr;
1190 if (usb_read_10(srb, ss, start, smallblks)) {
1191 debug("Read ERROR\n");
1192 ss->flags &= ~USB_READY;
1193 usb_request_sense(srb, ss);
1201 buf_addr += srb->datalen;
1202 } while (blks != 0);
1204 debug("usb_read: end startblk " LBAF ", blccnt %x buffer %lx\n",
1205 start, smallblks, buf_addr);
1207 usb_lock_async(udev, 0);
1208 usb_disable_asynch(0); /* asynch transfer allowed */
1209 if (blkcnt >= ss->max_xfer_blk)
1214 #if CONFIG_IS_ENABLED(BLK)
1215 static unsigned long usb_stor_write(struct udevice *dev, lbaint_t blknr,
1216 lbaint_t blkcnt, const void *buffer)
1218 static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr,
1219 lbaint_t blkcnt, const void *buffer)
1222 lbaint_t start, blks;
1224 unsigned short smallblks;
1225 struct usb_device *udev;
1228 struct scsi_cmd *srb = &usb_ccb;
1229 #if CONFIG_IS_ENABLED(BLK)
1230 struct blk_desc *block_dev;
1237 #if CONFIG_IS_ENABLED(BLK)
1238 block_dev = dev_get_uclass_plat(dev);
1239 udev = dev_get_parent_priv(dev_get_parent(dev));
1240 debug("\nusb_read: udev %d\n", block_dev->devnum);
1242 debug("\nusb_read: udev %d\n", block_dev->devnum);
1243 udev = usb_dev_desc[block_dev->devnum].priv;
1245 debug("%s: No device\n", __func__);
1249 ss = (struct us_data *)udev->privptr;
1251 usb_disable_asynch(1); /* asynch transfer not allowed */
1252 usb_lock_async(udev, 1);
1254 srb->lun = block_dev->lun;
1255 buf_addr = (uintptr_t)buffer;
1259 debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
1260 block_dev->devnum, start, blks, buf_addr);
1263 /* If write fails retry for max retry count else
1264 * return with number of blocks written successfully.
1267 srb->pdata = (unsigned char *)buf_addr;
1268 if (blks > ss->max_xfer_blk)
1269 smallblks = ss->max_xfer_blk;
1271 smallblks = (unsigned short) blks;
1273 if (smallblks == ss->max_xfer_blk)
1274 usb_show_progress();
1275 srb->datalen = block_dev->blksz * smallblks;
1276 srb->pdata = (unsigned char *)buf_addr;
1277 if (usb_write_10(srb, ss, start, smallblks)) {
1278 debug("Write ERROR\n");
1279 ss->flags &= ~USB_READY;
1280 usb_request_sense(srb, ss);
1288 buf_addr += srb->datalen;
1289 } while (blks != 0);
1291 debug("usb_write: end startblk " LBAF ", blccnt %x buffer %lx\n",
1292 start, smallblks, buf_addr);
1294 usb_lock_async(udev, 0);
1295 usb_disable_asynch(0); /* asynch transfer allowed */
1296 if (blkcnt >= ss->max_xfer_blk)
1302 /* Probe to see if a new device is actually a Storage device */
1303 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1306 struct usb_interface *iface;
1308 struct usb_endpoint_descriptor *ep_desc;
1309 unsigned int flags = 0;
1311 /* let's examine the device now */
1312 iface = &dev->config.if_desc[ifnum];
1314 if (dev->descriptor.bDeviceClass != 0 ||
1315 iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1316 iface->desc.bInterfaceSubClass < US_SC_MIN ||
1317 iface->desc.bInterfaceSubClass > US_SC_MAX) {
1318 debug("Not mass storage\n");
1319 /* if it's not a mass storage, we go no further */
1323 memset(ss, 0, sizeof(struct us_data));
1325 /* At this point, we know we've got a live one */
1326 debug("\n\nUSB Mass Storage device detected\n");
1328 /* Initialize the us_data structure with some useful info */
1332 ss->attention_done = 0;
1333 ss->subclass = iface->desc.bInterfaceSubClass;
1334 ss->protocol = iface->desc.bInterfaceProtocol;
1336 /* set the handler pointers based on the protocol */
1337 debug("Transport: ");
1338 switch (ss->protocol) {
1340 debug("Control/Bulk\n");
1341 ss->transport = usb_stor_CB_transport;
1342 ss->transport_reset = usb_stor_CB_reset;
1346 debug("Control/Bulk/Interrupt\n");
1347 ss->transport = usb_stor_CB_transport;
1348 ss->transport_reset = usb_stor_CB_reset;
1351 debug("Bulk/Bulk/Bulk\n");
1352 ss->transport = usb_stor_BBB_transport;
1353 ss->transport_reset = usb_stor_BBB_reset;
1356 printf("USB Storage Transport unknown / not yet implemented\n");
1362 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1363 * An optional interrupt is OK (necessary for CBI protocol).
1364 * We will ignore any others.
1366 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1367 ep_desc = &iface->ep_desc[i];
1368 /* is it an BULK endpoint? */
1369 if ((ep_desc->bmAttributes &
1370 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1371 if (ep_desc->bEndpointAddress & USB_DIR_IN)
1372 ss->ep_in = ep_desc->bEndpointAddress &
1373 USB_ENDPOINT_NUMBER_MASK;
1376 ep_desc->bEndpointAddress &
1377 USB_ENDPOINT_NUMBER_MASK;
1380 /* is it an interrupt endpoint? */
1381 if ((ep_desc->bmAttributes &
1382 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1383 ss->ep_int = ep_desc->bEndpointAddress &
1384 USB_ENDPOINT_NUMBER_MASK;
1385 ss->irqinterval = ep_desc->bInterval;
1388 debug("Endpoints In %d Out %d Int %d\n",
1389 ss->ep_in, ss->ep_out, ss->ep_int);
1391 /* Do some basic sanity checks, and bail if we find a problem */
1392 if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
1393 !ss->ep_in || !ss->ep_out ||
1394 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1395 debug("Problems with device\n");
1398 /* set class specific stuff */
1399 /* We only handle certain protocols. Currently, these are
1401 * The SFF8070 accepts the requests used in u-boot
1403 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1404 ss->subclass != US_SC_8070) {
1405 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
1409 /* we had found an interrupt endpoint, prepare irq pipe
1410 * set up the IRQ pipe and handler
1412 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1413 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1414 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1415 dev->irq_handle = usb_stor_irq;
1418 /* Set the maximum transfer size per host controller setting */
1419 usb_stor_set_max_xfer_blk(dev, ss);
1421 dev->privptr = (void *)ss;
1425 int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1426 struct blk_desc *dev_desc)
1428 unsigned char perq, modi;
1429 ALLOC_CACHE_ALIGN_BUFFER(u32, cap, 2);
1430 ALLOC_CACHE_ALIGN_BUFFER(u8, usb_stor_buf, 36);
1431 u32 capacity, blksz;
1432 struct scsi_cmd *pccb = &usb_ccb;
1434 pccb->pdata = usb_stor_buf;
1436 dev_desc->target = dev->devnum;
1437 pccb->lun = dev_desc->lun;
1438 debug(" address %d\n", dev_desc->target);
1440 if (usb_inquiry(pccb, ss)) {
1441 debug("%s: usb_inquiry() failed\n", __func__);
1445 perq = usb_stor_buf[0];
1446 modi = usb_stor_buf[1];
1449 * Skip unknown devices (0x1f) and enclosure service devices (0x0d),
1450 * they would not respond to test_unit_ready .
1452 if (((perq & 0x1f) == 0x1f) || ((perq & 0x1f) == 0x0d)) {
1453 debug("%s: unknown/unsupported device\n", __func__);
1456 if ((modi&0x80) == 0x80) {
1457 /* drive is removable */
1458 dev_desc->removable = 1;
1460 memcpy(dev_desc->vendor, (const void *)&usb_stor_buf[8], 8);
1461 memcpy(dev_desc->product, (const void *)&usb_stor_buf[16], 16);
1462 memcpy(dev_desc->revision, (const void *)&usb_stor_buf[32], 4);
1463 dev_desc->vendor[8] = 0;
1464 dev_desc->product[16] = 0;
1465 dev_desc->revision[4] = 0;
1466 #ifdef CONFIG_USB_BIN_FIXUP
1467 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1468 (uchar *)dev_desc->product);
1469 #endif /* CONFIG_USB_BIN_FIXUP */
1470 debug("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1472 if (usb_test_unit_ready(pccb, ss)) {
1473 printf("Device NOT ready\n"
1474 " Request Sense returned %02X %02X %02X\n",
1475 pccb->sense_buf[2], pccb->sense_buf[12],
1476 pccb->sense_buf[13]);
1477 if (dev_desc->removable == 1)
1478 dev_desc->type = perq;
1481 pccb->pdata = (unsigned char *)cap;
1482 memset(pccb->pdata, 0, 8);
1483 if (usb_read_capacity(pccb, ss) != 0) {
1484 printf("READ_CAP ERROR\n");
1485 ss->flags &= ~USB_READY;
1489 debug("Read Capacity returns: 0x%08x, 0x%08x\n", cap[0], cap[1]);
1491 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1494 cap[0] = cpu_to_be32(cap[0]);
1495 cap[1] = cpu_to_be32(cap[1]);
1498 capacity = be32_to_cpu(cap[0]) + 1;
1499 blksz = be32_to_cpu(cap[1]);
1501 debug("Capacity = 0x%08x, blocksz = 0x%08x\n", capacity, blksz);
1502 dev_desc->lba = capacity;
1503 dev_desc->blksz = blksz;
1504 dev_desc->log2blksz = LOG2(dev_desc->blksz);
1505 dev_desc->type = perq;
1506 debug(" address %d\n", dev_desc->target);
1511 #if CONFIG_IS_ENABLED(DM_USB)
1513 static int usb_mass_storage_probe(struct udevice *dev)
1515 struct usb_device *udev = dev_get_parent_priv(dev);
1518 usb_disable_asynch(1); /* asynch transfer not allowed */
1519 ret = usb_stor_probe_device(udev);
1520 usb_disable_asynch(0); /* asynch transfer allowed */
1525 static const struct udevice_id usb_mass_storage_ids[] = {
1526 { .compatible = "usb-mass-storage" },
1530 U_BOOT_DRIVER(usb_mass_storage) = {
1531 .name = "usb_mass_storage",
1532 .id = UCLASS_MASS_STORAGE,
1533 .of_match = usb_mass_storage_ids,
1534 .probe = usb_mass_storage_probe,
1535 #if CONFIG_IS_ENABLED(BLK)
1536 .plat_auto = sizeof(struct us_data),
1540 UCLASS_DRIVER(usb_mass_storage) = {
1541 .id = UCLASS_MASS_STORAGE,
1542 .name = "usb_mass_storage",
1545 static const struct usb_device_id mass_storage_id_table[] = {
1547 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1548 .bInterfaceClass = USB_CLASS_MASS_STORAGE
1550 { } /* Terminating entry */
1553 U_BOOT_USB_DEVICE(usb_mass_storage, mass_storage_id_table);
1556 #if CONFIG_IS_ENABLED(BLK)
1557 static const struct blk_ops usb_storage_ops = {
1558 .read = usb_stor_read,
1559 .write = usb_stor_write,
1562 U_BOOT_DRIVER(usb_storage_blk) = {
1563 .name = "usb_storage_blk",
1565 .ops = &usb_storage_ops,
1568 U_BOOT_LEGACY_BLK(usb) = {
1569 .if_typename = "usb",
1570 .if_type = IF_TYPE_USB,
1571 .max_devs = USB_MAX_STOR_DEV,
1572 .desc = usb_dev_desc,