2 * Most of this source has been derived from the Linux USB
4 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
6 * (c) 1999 Michael Gee (michael@linuxspecific.com)
7 * (c) 2000 Yggdrasil Computing, Inc.
11 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
13 * For BBB support (C) Copyright 2003
14 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
16 * BBB support based on /sys/dev/usb/umass.c from
19 * SPDX-License-Identifier: GPL-2.0+
23 * Currently only the CBI transport protocoll has been implemented, and it
24 * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
25 * transport protocoll may work as well.
29 * Support for USB Mass Storage Devices (BBB) has been added. It has
30 * only been tested with USB memory sticks.
36 #include <asm/byteorder.h>
37 #include <asm/processor.h>
42 #undef BBB_COMDAT_TRACE
43 #undef BBB_XPORT_TRACE
46 /* direction table -- this indicates the direction of the data
47 * transfer for each command code -- a 1 indicates input
49 static const unsigned char us_direction[256/8] = {
50 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
51 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
55 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
57 static ccb usb_ccb __attribute__((aligned(ARCH_DMA_MINALIGN)));
68 #define US_BBB_RESET 0xff
69 #define US_BBB_GET_MAX_LUN 0xfe
71 /* Command Block Wrapper */
74 # define CBWSIGNATURE 0x43425355
76 __u32 dCBWDataTransferLength;
78 # define CBWFLAGS_OUT 0x00
79 # define CBWFLAGS_IN 0x80
82 # define CBWCDBLENGTH 16
83 __u8 CBWCDB[CBWCDBLENGTH];
85 #define UMASS_BBB_CBW_SIZE 31
88 /* Command Status Wrapper */
91 # define CSWSIGNATURE 0x53425355
93 __u32 dCSWDataResidue;
95 # define CSWSTATUS_GOOD 0x0
96 # define CSWSTATUS_FAILED 0x1
97 # define CSWSTATUS_PHASE 0x2
99 #define UMASS_BBB_CSW_SIZE 13
101 #define USB_MAX_STOR_DEV 5
102 static int usb_max_devs; /* number of highest available usb device */
104 static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
107 typedef int (*trans_cmnd)(ccb *cb, struct us_data *data);
108 typedef int (*trans_reset)(struct us_data *data);
111 struct usb_device *pusb_dev; /* this usb_device */
113 unsigned int flags; /* from filter initially */
114 # define USB_READY (1 << 0)
115 unsigned char ifnum; /* interface number */
116 unsigned char ep_in; /* in endpoint */
117 unsigned char ep_out; /* out ....... */
118 unsigned char ep_int; /* interrupt . */
119 unsigned char subclass; /* as in overview */
120 unsigned char protocol; /* .............. */
121 unsigned char attention_done; /* force attn on first cmd */
122 unsigned short ip_data; /* interrupt data */
123 int action; /* what to do */
124 int ip_wanted; /* needed */
125 int *irq_handle; /* for USB int requests */
126 unsigned int irqpipe; /* pipe for release_irq */
127 unsigned char irqmaxp; /* max packed for irq Pipe */
128 unsigned char irqinterval; /* Intervall for IRQ Pipe */
129 ccb *srb; /* current srb */
130 trans_reset transport_reset; /* reset routine */
131 trans_cmnd transport; /* transport routine */
134 #ifdef CONFIG_USB_EHCI
136 * The U-Boot EHCI driver can handle any transfer length as long as there is
137 * enough free heap space left, but the SCSI READ(10) and WRITE(10) commands are
138 * limited to 65535 blocks.
140 #define USB_MAX_XFER_BLK 65535
142 #define USB_MAX_XFER_BLK 20
145 static struct us_data usb_stor[USB_MAX_STOR_DEV];
148 #define USB_STOR_TRANSPORT_GOOD 0
149 #define USB_STOR_TRANSPORT_FAILED -1
150 #define USB_STOR_TRANSPORT_ERROR -2
152 int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
153 block_dev_desc_t *dev_desc);
154 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
156 unsigned long usb_stor_read(int device, lbaint_t blknr,
157 lbaint_t blkcnt, void *buffer);
158 unsigned long usb_stor_write(int device, lbaint_t blknr,
159 lbaint_t blkcnt, const void *buffer);
160 struct usb_device * usb_get_dev_index(int index);
161 void uhci_show_temp_int_td(void);
163 #ifdef CONFIG_PARTITIONS
164 block_dev_desc_t *usb_stor_get_dev(int index)
166 return (index < usb_max_devs) ? &usb_dev_desc[index] : NULL;
170 static void usb_show_progress(void)
175 /*******************************************************************************
176 * show info on storage devices; 'usb start/init' must be invoked earlier
177 * as we only retrieve structures populated during devices initialization
179 int usb_stor_info(void)
183 if (usb_max_devs > 0) {
184 for (i = 0; i < usb_max_devs; i++) {
185 printf(" Device %d: ", i);
186 dev_print(&usb_dev_desc[i]);
191 printf("No storage devices, perhaps not 'usb start'ed..?\n");
195 static unsigned int usb_get_max_lun(struct us_data *us)
198 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
199 len = usb_control_msg(us->pusb_dev,
200 usb_rcvctrlpipe(us->pusb_dev, 0),
202 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
204 result, sizeof(char),
205 USB_CNTL_TIMEOUT * 5);
206 debug("Get Max LUN -> len = %i, result = %i\n", len, (int) *result);
207 return (len > 0) ? *result : 0;
210 /*******************************************************************************
211 * scan the usb and reports device info
212 * to the user if mode = 1
213 * returns current device or -1 if no
215 int usb_stor_scan(int mode)
218 struct usb_device *dev;
221 printf(" scanning usb for storage devices... ");
223 usb_disable_asynch(1); /* asynch transfer not allowed */
225 for (i = 0; i < USB_MAX_STOR_DEV; i++) {
226 memset(&usb_dev_desc[i], 0, sizeof(block_dev_desc_t));
227 usb_dev_desc[i].if_type = IF_TYPE_USB;
228 usb_dev_desc[i].dev = i;
229 usb_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
230 usb_dev_desc[i].target = 0xff;
231 usb_dev_desc[i].type = DEV_TYPE_UNKNOWN;
232 usb_dev_desc[i].block_read = usb_stor_read;
233 usb_dev_desc[i].block_write = usb_stor_write;
237 for (i = 0; i < USB_MAX_DEVICE; i++) {
238 dev = usb_get_dev_index(i); /* get device */
241 break; /* no more devices available */
243 if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
244 /* OK, it's a storage device. Iterate over its LUNs
245 * and populate `usb_dev_desc'.
247 int lun, max_lun, start = usb_max_devs;
249 max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
251 lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
253 usb_dev_desc[usb_max_devs].lun = lun;
254 if (usb_stor_get_info(dev, &usb_stor[start],
255 &usb_dev_desc[usb_max_devs]) == 1) {
260 /* if storage device */
261 if (usb_max_devs == USB_MAX_STOR_DEV) {
262 printf("max USB Storage Device reached: %d stopping\n",
268 usb_disable_asynch(0); /* asynch transfer allowed */
269 printf("%d Storage Device(s) found\n", usb_max_devs);
270 if (usb_max_devs > 0)
275 static int usb_stor_irq(struct usb_device *dev)
278 us = (struct us_data *)dev->privptr;
288 static void usb_show_srb(ccb *pccb)
291 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
292 for (i = 0; i < 12; i++)
293 printf("%02X ", pccb->cmd[i]);
297 static void display_int_status(unsigned long tmp)
299 printf("Status: %s %s %s %s %s %s %s\n",
300 (tmp & USB_ST_ACTIVE) ? "Active" : "",
301 (tmp & USB_ST_STALLED) ? "Stalled" : "",
302 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
303 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
304 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
305 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
306 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
309 /***********************************************************************
310 * Data transfer routines
311 ***********************************************************************/
313 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
322 /* determine the maximum packet size for these transfers */
323 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
325 /* while we have data left to transfer */
328 /* calculate how long this will be -- maximum or a remainder */
329 this_xfer = length > max_size ? max_size : length;
332 /* setup the retry counter */
335 /* set up the transfer loop */
337 /* transfer the data */
338 debug("Bulk xfer 0x%x(%d) try #%d\n",
339 (unsigned int)buf, this_xfer, 11 - maxtry);
340 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
342 USB_CNTL_TIMEOUT * 5);
343 debug("bulk_msg returned %d xferred %d/%d\n",
344 result, partial, this_xfer);
345 if (us->pusb_dev->status != 0) {
346 /* if we stall, we need to clear it before
350 display_int_status(us->pusb_dev->status);
352 if (us->pusb_dev->status & USB_ST_STALLED) {
353 debug("stalled ->clearing endpoint" \
354 "halt for pipe 0x%x\n", pipe);
355 stat = us->pusb_dev->status;
356 usb_clear_halt(us->pusb_dev, pipe);
357 us->pusb_dev->status = stat;
358 if (this_xfer == partial) {
359 debug("bulk transferred" \
362 us->pusb_dev->status);
368 if (us->pusb_dev->status & USB_ST_NAK_REC) {
369 debug("Device NAKed bulk_msg\n");
372 debug("bulk transferred with error");
373 if (this_xfer == partial) {
374 debug(" %ld, but data ok\n",
375 us->pusb_dev->status);
378 /* if our try counter reaches 0, bail out */
379 debug(" %ld, data %d\n",
380 us->pusb_dev->status, partial);
384 /* update to show what data was transferred */
385 this_xfer -= partial;
387 /* continue until this transfer is done */
391 /* if we get here, we're done and successful */
395 static int usb_stor_BBB_reset(struct us_data *us)
401 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
403 * For Reset Recovery the host shall issue in the following order:
404 * a) a Bulk-Only Mass Storage Reset
405 * b) a Clear Feature HALT to the Bulk-In endpoint
406 * c) a Clear Feature HALT to the Bulk-Out endpoint
408 * This is done in 3 steps.
410 * If the reset doesn't succeed, the device should be port reset.
412 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
414 debug("BBB_reset\n");
415 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
417 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
418 0, us->ifnum, NULL, 0, USB_CNTL_TIMEOUT * 5);
420 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
421 debug("RESET:stall\n");
425 /* long wait for reset */
427 debug("BBB_reset result %d: status %lX reset\n",
428 result, us->pusb_dev->status);
429 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
430 result = usb_clear_halt(us->pusb_dev, pipe);
431 /* long wait for reset */
433 debug("BBB_reset result %d: status %lX clearing IN endpoint\n",
434 result, us->pusb_dev->status);
435 /* long wait for reset */
436 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
437 result = usb_clear_halt(us->pusb_dev, pipe);
439 debug("BBB_reset result %d: status %lX clearing OUT endpoint\n",
440 result, us->pusb_dev->status);
441 debug("BBB_reset done\n");
445 /* FIXME: this reset function doesn't really reset the port, and it
446 * should. Actually it should probably do what it's doing here, and
447 * reset the port physically
449 static int usb_stor_CB_reset(struct us_data *us)
451 unsigned char cmd[12];
455 memset(cmd, 0xff, sizeof(cmd));
456 cmd[0] = SCSI_SEND_DIAG;
458 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
460 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
461 0, us->ifnum, cmd, sizeof(cmd),
462 USB_CNTL_TIMEOUT * 5);
464 /* long wait for reset */
466 debug("CB_reset result %d: status %lX clearing endpoint halt\n",
467 result, us->pusb_dev->status);
468 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
469 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
471 debug("CB_reset done\n");
476 * Set up the command for a BBB device. Note that the actual SCSI
477 * command is copied into cbw.CBWCDB.
479 static int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
485 ALLOC_CACHE_ALIGN_BUFFER(umass_bbb_cbw_t, cbw, 1);
487 dir_in = US_DIRECTION(srb->cmd[0]);
489 #ifdef BBB_COMDAT_TRACE
490 printf("dir %d lun %d cmdlen %d cmd %p datalen %lu pdata %p\n",
491 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
494 for (result = 0; result < srb->cmdlen; result++)
495 printf("cmd[%d] %#x ", result, srb->cmd[result]);
500 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
501 debug("usb_stor_BBB_comdat:cmdlen too large\n");
505 /* always OUT to the ep */
506 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
508 cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE);
509 cbw->dCBWTag = cpu_to_le32(CBWTag++);
510 cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen);
511 cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
512 cbw->bCBWLUN = srb->lun;
513 cbw->bCDBLength = srb->cmdlen;
514 /* copy the command data into the CBW command data buffer */
516 memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
517 result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
518 &actlen, USB_CNTL_TIMEOUT * 5);
520 debug("usb_stor_BBB_comdat:usb_bulk_msg error\n");
524 /* FIXME: we also need a CBI_command which sets up the completion
525 * interrupt, and waits for it
527 static int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
532 unsigned long status;
535 dir_in = US_DIRECTION(srb->cmd[0]);
538 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
540 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
543 debug("CBI gets a command: Try %d\n", 5 - retry);
547 /* let's send the command via the control pipe */
548 result = usb_control_msg(us->pusb_dev,
549 usb_sndctrlpipe(us->pusb_dev , 0),
551 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
553 srb->cmd, srb->cmdlen,
554 USB_CNTL_TIMEOUT * 5);
555 debug("CB_transport: control msg returned %d, status %lX\n",
556 result, us->pusb_dev->status);
557 /* check the return code for the command */
559 if (us->pusb_dev->status & USB_ST_STALLED) {
560 status = us->pusb_dev->status;
561 debug(" stall during command found," \
563 usb_clear_halt(us->pusb_dev,
564 usb_sndctrlpipe(us->pusb_dev, 0));
565 us->pusb_dev->status = status;
567 debug(" error during command %02X" \
568 " Stat = %lX\n", srb->cmd[0],
569 us->pusb_dev->status);
572 /* transfer the data payload for this command, if one exists*/
574 debug("CB_transport: control msg returned %d," \
575 " direction is %s to go 0x%lx\n", result,
576 dir_in ? "IN" : "OUT", srb->datalen);
578 result = us_one_transfer(us, pipe, (char *)srb->pdata,
580 debug("CBI attempted to transfer data," \
581 " result is %d status %lX, len %d\n",
582 result, us->pusb_dev->status,
583 us->pusb_dev->act_len);
584 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
586 } /* if (srb->datalen) */
596 static int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
601 submit_int_msg(us->pusb_dev, us->irqpipe,
602 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
605 if ((volatile int *) us->ip_wanted == NULL)
610 printf(" Did not get interrupt on CBI\n");
612 return USB_STOR_TRANSPORT_ERROR;
614 debug("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
615 us->ip_data, us->pusb_dev->irq_act_len,
616 us->pusb_dev->irq_status);
617 /* UFI gives us ASC and ASCQ, like a request sense */
618 if (us->subclass == US_SC_UFI) {
619 if (srb->cmd[0] == SCSI_REQ_SENSE ||
620 srb->cmd[0] == SCSI_INQUIRY)
621 return USB_STOR_TRANSPORT_GOOD; /* Good */
622 else if (us->ip_data)
623 return USB_STOR_TRANSPORT_FAILED;
625 return USB_STOR_TRANSPORT_GOOD;
627 /* otherwise, we interpret the data normally */
628 switch (us->ip_data) {
630 return USB_STOR_TRANSPORT_GOOD;
632 return USB_STOR_TRANSPORT_FAILED;
634 return USB_STOR_TRANSPORT_ERROR;
636 return USB_STOR_TRANSPORT_ERROR;
639 #define USB_TRANSPORT_UNKNOWN_RETRY 5
640 #define USB_TRANSPORT_NOT_READY_RETRY 10
642 /* clear a stall on an endpoint - special for BBB devices */
643 static int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
647 /* ENDPOINT_HALT = 0, so set value to 0 */
648 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
649 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
650 0, endpt, NULL, 0, USB_CNTL_TIMEOUT * 5);
654 static int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
658 int actlen, data_actlen;
659 unsigned int pipe, pipein, pipeout;
660 ALLOC_CACHE_ALIGN_BUFFER(umass_bbb_csw_t, csw, 1);
661 #ifdef BBB_XPORT_TRACE
666 dir_in = US_DIRECTION(srb->cmd[0]);
669 debug("COMMAND phase\n");
670 result = usb_stor_BBB_comdat(srb, us);
672 debug("failed to send CBW status %ld\n",
673 us->pusb_dev->status);
674 usb_stor_BBB_reset(us);
675 return USB_STOR_TRANSPORT_FAILED;
677 if (!(us->flags & USB_READY))
679 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
680 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
681 /* DATA phase + error handling */
683 /* no data, go immediately to the STATUS phase */
684 if (srb->datalen == 0)
686 debug("DATA phase\n");
691 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
692 &data_actlen, USB_CNTL_TIMEOUT * 5);
693 /* special handling of STALL in DATA phase */
694 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
695 debug("DATA:stall\n");
696 /* clear the STALL on the endpoint */
697 result = usb_stor_BBB_clear_endpt_stall(us,
698 dir_in ? us->ep_in : us->ep_out);
700 /* continue on to STATUS phase */
704 debug("usb_bulk_msg error status %ld\n",
705 us->pusb_dev->status);
706 usb_stor_BBB_reset(us);
707 return USB_STOR_TRANSPORT_FAILED;
709 #ifdef BBB_XPORT_TRACE
710 for (index = 0; index < data_actlen; index++)
711 printf("pdata[%d] %#x ", index, srb->pdata[index]);
714 /* STATUS phase + error handling */
718 debug("STATUS phase\n");
719 result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE,
720 &actlen, USB_CNTL_TIMEOUT*5);
722 /* special handling of STALL in STATUS phase */
723 if ((result < 0) && (retry < 1) &&
724 (us->pusb_dev->status & USB_ST_STALLED)) {
725 debug("STATUS:stall\n");
726 /* clear the STALL on the endpoint */
727 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
728 if (result >= 0 && (retry++ < 1))
733 debug("usb_bulk_msg error status %ld\n",
734 us->pusb_dev->status);
735 usb_stor_BBB_reset(us);
736 return USB_STOR_TRANSPORT_FAILED;
738 #ifdef BBB_XPORT_TRACE
739 ptr = (unsigned char *)csw;
740 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
741 printf("ptr[%d] %#x ", index, ptr[index]);
744 /* misuse pipe to get the residue */
745 pipe = le32_to_cpu(csw->dCSWDataResidue);
746 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
747 pipe = srb->datalen - data_actlen;
748 if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) {
749 debug("!CSWSIGNATURE\n");
750 usb_stor_BBB_reset(us);
751 return USB_STOR_TRANSPORT_FAILED;
752 } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) {
754 usb_stor_BBB_reset(us);
755 return USB_STOR_TRANSPORT_FAILED;
756 } else if (csw->bCSWStatus > CSWSTATUS_PHASE) {
758 usb_stor_BBB_reset(us);
759 return USB_STOR_TRANSPORT_FAILED;
760 } else if (csw->bCSWStatus == CSWSTATUS_PHASE) {
762 usb_stor_BBB_reset(us);
763 return USB_STOR_TRANSPORT_FAILED;
764 } else if (data_actlen > srb->datalen) {
765 debug("transferred %dB instead of %ldB\n",
766 data_actlen, srb->datalen);
767 return USB_STOR_TRANSPORT_FAILED;
768 } else if (csw->bCSWStatus == CSWSTATUS_FAILED) {
770 return USB_STOR_TRANSPORT_FAILED;
776 static int usb_stor_CB_transport(ccb *srb, struct us_data *us)
784 status = USB_STOR_TRANSPORT_GOOD;
787 /* issue the command */
789 result = usb_stor_CB_comdat(srb, us);
790 debug("command / Data returned %d, status %lX\n",
791 result, us->pusb_dev->status);
792 /* if this is an CBI Protocol, get IRQ */
793 if (us->protocol == US_PR_CBI) {
794 status = usb_stor_CBI_get_status(srb, us);
795 /* if the status is error, report it */
796 if (status == USB_STOR_TRANSPORT_ERROR) {
797 debug(" USB CBI Command Error\n");
800 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
801 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
803 /* if the status is good, report it */
804 if (status == USB_STOR_TRANSPORT_GOOD) {
805 debug(" USB CBI Command Good\n");
810 /* do we have to issue an auto request? */
811 /* HERE we have to check the result */
812 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
813 debug("ERROR %lX\n", us->pusb_dev->status);
814 us->transport_reset(us);
815 return USB_STOR_TRANSPORT_ERROR;
817 if ((us->protocol == US_PR_CBI) &&
818 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
819 (srb->cmd[0] == SCSI_INQUIRY))) {
820 /* do not issue an autorequest after request sense */
821 debug("No auto request and good\n");
822 return USB_STOR_TRANSPORT_GOOD;
824 /* issue an request_sense */
825 memset(&psrb->cmd[0], 0, 12);
826 psrb->cmd[0] = SCSI_REQ_SENSE;
827 psrb->cmd[1] = srb->lun << 5;
830 psrb->pdata = &srb->sense_buf[0];
832 /* issue the command */
833 result = usb_stor_CB_comdat(psrb, us);
834 debug("auto request returned %d\n", result);
835 /* if this is an CBI Protocol, get IRQ */
836 if (us->protocol == US_PR_CBI)
837 status = usb_stor_CBI_get_status(psrb, us);
839 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
840 debug(" AUTO REQUEST ERROR %ld\n",
841 us->pusb_dev->status);
842 return USB_STOR_TRANSPORT_ERROR;
844 debug("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
845 srb->sense_buf[0], srb->sense_buf[2],
846 srb->sense_buf[12], srb->sense_buf[13]);
847 /* Check the auto request result */
848 if ((srb->sense_buf[2] == 0) &&
849 (srb->sense_buf[12] == 0) &&
850 (srb->sense_buf[13] == 0)) {
852 return USB_STOR_TRANSPORT_GOOD;
855 /* Check the auto request result */
856 switch (srb->sense_buf[2]) {
858 /* Recovered Error */
859 return USB_STOR_TRANSPORT_GOOD;
863 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
864 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
865 " 0x%02X (NOT READY)\n", srb->cmd[0],
866 srb->sense_buf[0], srb->sense_buf[2],
867 srb->sense_buf[12], srb->sense_buf[13]);
868 return USB_STOR_TRANSPORT_FAILED;
875 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
876 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
877 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
878 srb->sense_buf[2], srb->sense_buf[12],
880 return USB_STOR_TRANSPORT_FAILED;
885 return USB_STOR_TRANSPORT_FAILED;
889 static int usb_inquiry(ccb *srb, struct us_data *ss)
894 memset(&srb->cmd[0], 0, 12);
895 srb->cmd[0] = SCSI_INQUIRY;
896 srb->cmd[1] = srb->lun << 5;
900 i = ss->transport(srb, ss);
901 debug("inquiry returns %d\n", i);
907 printf("error in inquiry\n");
913 static int usb_request_sense(ccb *srb, struct us_data *ss)
917 ptr = (char *)srb->pdata;
918 memset(&srb->cmd[0], 0, 12);
919 srb->cmd[0] = SCSI_REQ_SENSE;
920 srb->cmd[1] = srb->lun << 5;
923 srb->pdata = &srb->sense_buf[0];
925 ss->transport(srb, ss);
926 debug("Request Sense returned %02X %02X %02X\n",
927 srb->sense_buf[2], srb->sense_buf[12],
929 srb->pdata = (uchar *)ptr;
933 static int usb_test_unit_ready(ccb *srb, struct us_data *ss)
938 memset(&srb->cmd[0], 0, 12);
939 srb->cmd[0] = SCSI_TST_U_RDY;
940 srb->cmd[1] = srb->lun << 5;
943 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD) {
944 ss->flags |= USB_READY;
947 usb_request_sense(srb, ss);
949 * Check the Key Code Qualifier, if it matches
950 * "Not Ready - medium not present"
951 * (the sense Key equals 0x2 and the ASC is 0x3a)
952 * return immediately as the medium being absent won't change
953 * unless there is a user action.
955 if ((srb->sense_buf[2] == 0x02) &&
956 (srb->sense_buf[12] == 0x3a))
964 static int usb_read_capacity(ccb *srb, struct us_data *ss)
970 memset(&srb->cmd[0], 0, 12);
971 srb->cmd[0] = SCSI_RD_CAPAC;
972 srb->cmd[1] = srb->lun << 5;
975 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
982 static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start,
983 unsigned short blocks)
985 memset(&srb->cmd[0], 0, 12);
986 srb->cmd[0] = SCSI_READ10;
987 srb->cmd[1] = srb->lun << 5;
988 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
989 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
990 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
991 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
992 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
993 srb->cmd[8] = (unsigned char) blocks & 0xff;
995 debug("read10: start %lx blocks %x\n", start, blocks);
996 return ss->transport(srb, ss);
999 static int usb_write_10(ccb *srb, struct us_data *ss, unsigned long start,
1000 unsigned short blocks)
1002 memset(&srb->cmd[0], 0, 12);
1003 srb->cmd[0] = SCSI_WRITE10;
1004 srb->cmd[1] = srb->lun << 5;
1005 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1006 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1007 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1008 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1009 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1010 srb->cmd[8] = (unsigned char) blocks & 0xff;
1012 debug("write10: start %lx blocks %x\n", start, blocks);
1013 return ss->transport(srb, ss);
1017 #ifdef CONFIG_USB_BIN_FIXUP
1019 * Some USB storage devices queried for SCSI identification data respond with
1020 * binary strings, which if output to the console freeze the terminal. The
1021 * workaround is to modify the vendor and product strings read from such
1022 * device with proper values (as reported by 'usb info').
1024 * Vendor and product length limits are taken from the definition of
1025 * block_dev_desc_t in include/part.h.
1027 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
1028 unsigned char vendor[],
1029 unsigned char product[]) {
1030 const unsigned char max_vendor_len = 40;
1031 const unsigned char max_product_len = 20;
1032 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
1033 strncpy((char *)vendor, "SMSC", max_vendor_len);
1034 strncpy((char *)product, "Flash Media Cntrller",
1038 #endif /* CONFIG_USB_BIN_FIXUP */
1040 unsigned long usb_stor_read(int device, lbaint_t blknr,
1041 lbaint_t blkcnt, void *buffer)
1043 lbaint_t start, blks;
1045 unsigned short smallblks;
1046 struct usb_device *dev;
1049 ccb *srb = &usb_ccb;
1056 debug("\nusb_read: dev %d \n", device);
1058 for (i = 0; i < USB_MAX_DEVICE; i++) {
1059 dev = usb_get_dev_index(i);
1062 if (dev->devnum == usb_dev_desc[device].target)
1065 ss = (struct us_data *)dev->privptr;
1067 usb_disable_asynch(1); /* asynch transfer not allowed */
1068 srb->lun = usb_dev_desc[device].lun;
1069 buf_addr = (unsigned long)buffer;
1073 debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF
1074 " buffer %lx\n", device, start, blks, buf_addr);
1077 /* XXX need some comment here */
1079 srb->pdata = (unsigned char *)buf_addr;
1080 if (blks > USB_MAX_XFER_BLK)
1081 smallblks = USB_MAX_XFER_BLK;
1083 smallblks = (unsigned short) blks;
1085 if (smallblks == USB_MAX_XFER_BLK)
1086 usb_show_progress();
1087 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1088 srb->pdata = (unsigned char *)buf_addr;
1089 if (usb_read_10(srb, ss, start, smallblks)) {
1090 debug("Read ERROR\n");
1091 usb_request_sense(srb, ss);
1099 buf_addr += srb->datalen;
1100 } while (blks != 0);
1101 ss->flags &= ~USB_READY;
1103 debug("usb_read: end startblk " LBAF
1104 ", blccnt %x buffer %lx\n",
1105 start, smallblks, buf_addr);
1107 usb_disable_asynch(0); /* asynch transfer allowed */
1108 if (blkcnt >= USB_MAX_XFER_BLK)
1113 unsigned long usb_stor_write(int device, lbaint_t blknr,
1114 lbaint_t blkcnt, const void *buffer)
1116 lbaint_t start, blks;
1118 unsigned short smallblks;
1119 struct usb_device *dev;
1122 ccb *srb = &usb_ccb;
1129 debug("\nusb_write: dev %d \n", device);
1131 for (i = 0; i < USB_MAX_DEVICE; i++) {
1132 dev = usb_get_dev_index(i);
1135 if (dev->devnum == usb_dev_desc[device].target)
1138 ss = (struct us_data *)dev->privptr;
1140 usb_disable_asynch(1); /* asynch transfer not allowed */
1142 srb->lun = usb_dev_desc[device].lun;
1143 buf_addr = (unsigned long)buffer;
1147 debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF
1148 " buffer %lx\n", device, start, blks, buf_addr);
1151 /* If write fails retry for max retry count else
1152 * return with number of blocks written successfully.
1155 srb->pdata = (unsigned char *)buf_addr;
1156 if (blks > USB_MAX_XFER_BLK)
1157 smallblks = USB_MAX_XFER_BLK;
1159 smallblks = (unsigned short) blks;
1161 if (smallblks == USB_MAX_XFER_BLK)
1162 usb_show_progress();
1163 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1164 srb->pdata = (unsigned char *)buf_addr;
1165 if (usb_write_10(srb, ss, start, smallblks)) {
1166 debug("Write ERROR\n");
1167 usb_request_sense(srb, ss);
1175 buf_addr += srb->datalen;
1176 } while (blks != 0);
1177 ss->flags &= ~USB_READY;
1179 debug("usb_write: end startblk " LBAF ", blccnt %x buffer %lx\n",
1180 start, smallblks, buf_addr);
1182 usb_disable_asynch(0); /* asynch transfer allowed */
1183 if (blkcnt >= USB_MAX_XFER_BLK)
1189 /* Probe to see if a new device is actually a Storage device */
1190 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1193 struct usb_interface *iface;
1195 struct usb_endpoint_descriptor *ep_desc;
1196 unsigned int flags = 0;
1201 /* let's examine the device now */
1202 iface = &dev->config.if_desc[ifnum];
1205 /* this is the place to patch some storage devices */
1206 debug("iVendor %X iProduct %X\n", dev->descriptor.idVendor,
1207 dev->descriptor.idProduct);
1209 if ((dev->descriptor.idVendor) == 0x066b &&
1210 (dev->descriptor.idProduct) == 0x0103) {
1211 debug("patched for E-USB\n");
1212 protocol = US_PR_CB;
1213 subclass = US_SC_UFI; /* an assumption */
1217 if (dev->descriptor.bDeviceClass != 0 ||
1218 iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1219 iface->desc.bInterfaceSubClass < US_SC_MIN ||
1220 iface->desc.bInterfaceSubClass > US_SC_MAX) {
1221 /* if it's not a mass storage, we go no further */
1225 memset(ss, 0, sizeof(struct us_data));
1227 /* At this point, we know we've got a live one */
1228 debug("\n\nUSB Mass Storage device detected\n");
1230 /* Initialize the us_data structure with some useful info */
1234 ss->attention_done = 0;
1236 /* If the device has subclass and protocol, then use that. Otherwise,
1237 * take data from the specific interface.
1240 ss->subclass = subclass;
1241 ss->protocol = protocol;
1243 ss->subclass = iface->desc.bInterfaceSubClass;
1244 ss->protocol = iface->desc.bInterfaceProtocol;
1247 /* set the handler pointers based on the protocol */
1248 debug("Transport: ");
1249 switch (ss->protocol) {
1251 debug("Control/Bulk\n");
1252 ss->transport = usb_stor_CB_transport;
1253 ss->transport_reset = usb_stor_CB_reset;
1257 debug("Control/Bulk/Interrupt\n");
1258 ss->transport = usb_stor_CB_transport;
1259 ss->transport_reset = usb_stor_CB_reset;
1262 debug("Bulk/Bulk/Bulk\n");
1263 ss->transport = usb_stor_BBB_transport;
1264 ss->transport_reset = usb_stor_BBB_reset;
1267 printf("USB Storage Transport unknown / not yet implemented\n");
1273 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1274 * An optional interrupt is OK (necessary for CBI protocol).
1275 * We will ignore any others.
1277 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1278 ep_desc = &iface->ep_desc[i];
1279 /* is it an BULK endpoint? */
1280 if ((ep_desc->bmAttributes &
1281 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1282 if (ep_desc->bEndpointAddress & USB_DIR_IN)
1283 ss->ep_in = ep_desc->bEndpointAddress &
1284 USB_ENDPOINT_NUMBER_MASK;
1287 ep_desc->bEndpointAddress &
1288 USB_ENDPOINT_NUMBER_MASK;
1291 /* is it an interrupt endpoint? */
1292 if ((ep_desc->bmAttributes &
1293 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1294 ss->ep_int = ep_desc->bEndpointAddress &
1295 USB_ENDPOINT_NUMBER_MASK;
1296 ss->irqinterval = ep_desc->bInterval;
1299 debug("Endpoints In %d Out %d Int %d\n",
1300 ss->ep_in, ss->ep_out, ss->ep_int);
1302 /* Do some basic sanity checks, and bail if we find a problem */
1303 if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
1304 !ss->ep_in || !ss->ep_out ||
1305 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1306 debug("Problems with device\n");
1309 /* set class specific stuff */
1310 /* We only handle certain protocols. Currently, these are
1312 * The SFF8070 accepts the requests used in u-boot
1314 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1315 ss->subclass != US_SC_8070) {
1316 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
1320 /* we had found an interrupt endpoint, prepare irq pipe
1321 * set up the IRQ pipe and handler
1323 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1324 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1325 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1326 dev->irq_handle = usb_stor_irq;
1328 dev->privptr = (void *)ss;
1332 int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1333 block_dev_desc_t *dev_desc)
1335 unsigned char perq, modi;
1336 ALLOC_CACHE_ALIGN_BUFFER(unsigned long, cap, 2);
1337 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, usb_stor_buf, 36);
1338 unsigned long *capacity, *blksz;
1339 ccb *pccb = &usb_ccb;
1341 pccb->pdata = usb_stor_buf;
1343 dev_desc->target = dev->devnum;
1344 pccb->lun = dev_desc->lun;
1345 debug(" address %d\n", dev_desc->target);
1347 if (usb_inquiry(pccb, ss))
1350 perq = usb_stor_buf[0];
1351 modi = usb_stor_buf[1];
1353 if ((perq & 0x1f) == 0x1f) {
1354 /* skip unknown devices */
1357 if ((modi&0x80) == 0x80) {
1358 /* drive is removable */
1359 dev_desc->removable = 1;
1361 memcpy(&dev_desc->vendor[0], (const void *) &usb_stor_buf[8], 8);
1362 memcpy(&dev_desc->product[0], (const void *) &usb_stor_buf[16], 16);
1363 memcpy(&dev_desc->revision[0], (const void *) &usb_stor_buf[32], 4);
1364 dev_desc->vendor[8] = 0;
1365 dev_desc->product[16] = 0;
1366 dev_desc->revision[4] = 0;
1367 #ifdef CONFIG_USB_BIN_FIXUP
1368 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1369 (uchar *)dev_desc->product);
1370 #endif /* CONFIG_USB_BIN_FIXUP */
1371 debug("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1373 if (usb_test_unit_ready(pccb, ss)) {
1374 printf("Device NOT ready\n"
1375 " Request Sense returned %02X %02X %02X\n",
1376 pccb->sense_buf[2], pccb->sense_buf[12],
1377 pccb->sense_buf[13]);
1378 if (dev_desc->removable == 1) {
1379 dev_desc->type = perq;
1384 pccb->pdata = (unsigned char *)&cap[0];
1385 memset(pccb->pdata, 0, 8);
1386 if (usb_read_capacity(pccb, ss) != 0) {
1387 printf("READ_CAP ERROR\n");
1391 ss->flags &= ~USB_READY;
1392 debug("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0], cap[1]);
1394 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1397 cap[0] = cpu_to_be32(cap[0]);
1398 cap[1] = cpu_to_be32(cap[1]);
1400 /* this assumes bigendian! */
1404 debug("Capacity = 0x%lx, blocksz = 0x%lx\n", *capacity, *blksz);
1405 dev_desc->lba = *capacity;
1406 dev_desc->blksz = *blksz;
1407 dev_desc->log2blksz = LOG2(dev_desc->blksz);
1408 dev_desc->type = perq;
1409 debug(" address %d\n", dev_desc->target);
1410 debug("partype: %d\n", dev_desc->part_type);
1412 init_part(dev_desc);
1414 debug("partype: %d\n", dev_desc->part_type);