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 * See file CREDITS for list of people who contributed to this
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
40 * Currently only the CBI transport protocoll has been implemented, and it
41 * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
42 * transport protocoll may work as well.
46 * Support for USB Mass Storage Devices (BBB) has been added. It has
47 * only been tested with USB memory sticks.
53 #include <asm/byteorder.h>
54 #include <asm/processor.h>
59 #undef BBB_COMDAT_TRACE
60 #undef BBB_XPORT_TRACE
63 #define USB_BLK_DEBUG 1
65 #define USB_BLK_DEBUG 0
68 #define USB_STOR_PRINTF(fmt, args...) debug_cond(USB_BLK_DEBUG, fmt, ##args)
71 /* direction table -- this indicates the direction of the data
72 * transfer for each command code -- a 1 indicates input
74 static const unsigned char us_direction[256/8] = {
75 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
76 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
80 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
82 static ccb usb_ccb __attribute__((aligned(ARCH_DMA_MINALIGN)));
93 #define US_BBB_RESET 0xff
94 #define US_BBB_GET_MAX_LUN 0xfe
96 /* Command Block Wrapper */
99 # define CBWSIGNATURE 0x43425355
101 __u32 dCBWDataTransferLength;
103 # define CBWFLAGS_OUT 0x00
104 # define CBWFLAGS_IN 0x80
107 # define CBWCDBLENGTH 16
108 __u8 CBWCDB[CBWCDBLENGTH];
110 #define UMASS_BBB_CBW_SIZE 31
113 /* Command Status Wrapper */
116 # define CSWSIGNATURE 0x53425355
118 __u32 dCSWDataResidue;
120 # define CSWSTATUS_GOOD 0x0
121 # define CSWSTATUS_FAILED 0x1
122 # define CSWSTATUS_PHASE 0x2
124 #define UMASS_BBB_CSW_SIZE 13
126 #define USB_MAX_STOR_DEV 5
127 static int usb_max_devs; /* number of highest available usb device */
129 static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
132 typedef int (*trans_cmnd)(ccb *cb, struct us_data *data);
133 typedef int (*trans_reset)(struct us_data *data);
136 struct usb_device *pusb_dev; /* this usb_device */
138 unsigned int flags; /* from filter initially */
139 unsigned char ifnum; /* interface number */
140 unsigned char ep_in; /* in endpoint */
141 unsigned char ep_out; /* out ....... */
142 unsigned char ep_int; /* interrupt . */
143 unsigned char subclass; /* as in overview */
144 unsigned char protocol; /* .............. */
145 unsigned char attention_done; /* force attn on first cmd */
146 unsigned short ip_data; /* interrupt data */
147 int action; /* what to do */
148 int ip_wanted; /* needed */
149 int *irq_handle; /* for USB int requests */
150 unsigned int irqpipe; /* pipe for release_irq */
151 unsigned char irqmaxp; /* max packed for irq Pipe */
152 unsigned char irqinterval; /* Intervall for IRQ Pipe */
153 ccb *srb; /* current srb */
154 trans_reset transport_reset; /* reset routine */
155 trans_cmnd transport; /* transport routine */
158 #ifdef CONFIG_USB_EHCI
160 * The U-Boot EHCI driver can handle any transfer length as long as there is
161 * enough free heap space left, but the SCSI READ(10) and WRITE(10) commands are
162 * limited to 65535 blocks.
164 #define USB_MAX_XFER_BLK 65535
166 #define USB_MAX_XFER_BLK 20
169 static struct us_data usb_stor[USB_MAX_STOR_DEV];
172 #define USB_STOR_TRANSPORT_GOOD 0
173 #define USB_STOR_TRANSPORT_FAILED -1
174 #define USB_STOR_TRANSPORT_ERROR -2
176 int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
177 block_dev_desc_t *dev_desc);
178 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
180 unsigned long usb_stor_read(int device, unsigned long blknr,
181 unsigned long blkcnt, void *buffer);
182 unsigned long usb_stor_write(int device, unsigned long blknr,
183 unsigned long blkcnt, const void *buffer);
184 struct usb_device * usb_get_dev_index(int index);
185 void uhci_show_temp_int_td(void);
187 #ifdef CONFIG_PARTITIONS
188 block_dev_desc_t *usb_stor_get_dev(int index)
190 return (index < usb_max_devs) ? &usb_dev_desc[index] : NULL;
194 void usb_show_progress(void)
199 /*******************************************************************************
200 * show info on storage devices; 'usb start/init' must be invoked earlier
201 * as we only retrieve structures populated during devices initialization
203 int usb_stor_info(void)
207 if (usb_max_devs > 0) {
208 for (i = 0; i < usb_max_devs; i++) {
209 printf(" Device %d: ", i);
210 dev_print(&usb_dev_desc[i]);
215 printf("No storage devices, perhaps not 'usb start'ed..?\n");
219 static unsigned int usb_get_max_lun(struct us_data *us)
222 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
223 len = usb_control_msg(us->pusb_dev,
224 usb_rcvctrlpipe(us->pusb_dev, 0),
226 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
228 result, sizeof(char),
229 USB_CNTL_TIMEOUT * 5);
230 USB_STOR_PRINTF("Get Max LUN -> len = %i, result = %i\n",
232 return (len > 0) ? *result : 0;
235 /*******************************************************************************
236 * scan the usb and reports device info
237 * to the user if mode = 1
238 * returns current device or -1 if no
240 int usb_stor_scan(int mode)
243 struct usb_device *dev;
246 printf(" scanning bus for storage devices... ");
248 usb_disable_asynch(1); /* asynch transfer not allowed */
250 for (i = 0; i < USB_MAX_STOR_DEV; i++) {
251 memset(&usb_dev_desc[i], 0, sizeof(block_dev_desc_t));
252 usb_dev_desc[i].if_type = IF_TYPE_USB;
253 usb_dev_desc[i].dev = i;
254 usb_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
255 usb_dev_desc[i].target = 0xff;
256 usb_dev_desc[i].type = DEV_TYPE_UNKNOWN;
257 usb_dev_desc[i].block_read = usb_stor_read;
258 usb_dev_desc[i].block_write = usb_stor_write;
262 for (i = 0; i < USB_MAX_DEVICE; i++) {
263 dev = usb_get_dev_index(i); /* get device */
264 USB_STOR_PRINTF("i=%d\n", i);
266 break; /* no more devices available */
268 if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
269 /* OK, it's a storage device. Iterate over its LUNs
270 * and populate `usb_dev_desc'.
272 int lun, max_lun, start = usb_max_devs;
274 max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
276 lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
278 usb_dev_desc[usb_max_devs].lun = lun;
279 if (usb_stor_get_info(dev, &usb_stor[start],
280 &usb_dev_desc[usb_max_devs]) == 1) {
285 /* if storage device */
286 if (usb_max_devs == USB_MAX_STOR_DEV) {
287 printf("max USB Storage Device reached: %d stopping\n",
293 usb_disable_asynch(0); /* asynch transfer allowed */
294 printf("%d Storage Device(s) found\n", usb_max_devs);
295 if (usb_max_devs > 0)
300 static int usb_stor_irq(struct usb_device *dev)
303 us = (struct us_data *)dev->privptr;
311 #ifdef USB_STOR_DEBUG
313 static void usb_show_srb(ccb *pccb)
316 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
317 for (i = 0; i < 12; i++)
318 printf("%02X ", pccb->cmd[i]);
322 static void display_int_status(unsigned long tmp)
324 printf("Status: %s %s %s %s %s %s %s\n",
325 (tmp & USB_ST_ACTIVE) ? "Active" : "",
326 (tmp & USB_ST_STALLED) ? "Stalled" : "",
327 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
328 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
329 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
330 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
331 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
334 /***********************************************************************
335 * Data transfer routines
336 ***********************************************************************/
338 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
347 /* determine the maximum packet size for these transfers */
348 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
350 /* while we have data left to transfer */
353 /* calculate how long this will be -- maximum or a remainder */
354 this_xfer = length > max_size ? max_size : length;
357 /* setup the retry counter */
360 /* set up the transfer loop */
362 /* transfer the data */
363 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
364 (unsigned int)buf, this_xfer, 11 - maxtry);
365 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
367 USB_CNTL_TIMEOUT * 5);
368 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
369 result, partial, this_xfer);
370 if (us->pusb_dev->status != 0) {
371 /* if we stall, we need to clear it before
374 #ifdef USB_STOR_DEBUG
375 display_int_status(us->pusb_dev->status);
377 if (us->pusb_dev->status & USB_ST_STALLED) {
378 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
379 stat = us->pusb_dev->status;
380 usb_clear_halt(us->pusb_dev, pipe);
381 us->pusb_dev->status = stat;
382 if (this_xfer == partial) {
383 USB_STOR_PRINTF("bulk transferred with error %lX, but data ok\n", us->pusb_dev->status);
389 if (us->pusb_dev->status & USB_ST_NAK_REC) {
390 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
393 USB_STOR_PRINTF("bulk transferred with error");
394 if (this_xfer == partial) {
395 USB_STOR_PRINTF(" %ld, but data ok\n",
396 us->pusb_dev->status);
399 /* if our try counter reaches 0, bail out */
400 USB_STOR_PRINTF(" %ld, data %d\n",
401 us->pusb_dev->status, partial);
405 /* update to show what data was transferred */
406 this_xfer -= partial;
408 /* continue until this transfer is done */
412 /* if we get here, we're done and successful */
416 static int usb_stor_BBB_reset(struct us_data *us)
422 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
424 * For Reset Recovery the host shall issue in the following order:
425 * a) a Bulk-Only Mass Storage Reset
426 * b) a Clear Feature HALT to the Bulk-In endpoint
427 * c) a Clear Feature HALT to the Bulk-Out endpoint
429 * This is done in 3 steps.
431 * If the reset doesn't succeed, the device should be port reset.
433 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
435 USB_STOR_PRINTF("BBB_reset\n");
436 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
438 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
439 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT * 5);
441 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
442 USB_STOR_PRINTF("RESET:stall\n");
446 /* long wait for reset */
448 USB_STOR_PRINTF("BBB_reset result %d: status %lX reset\n", result,
449 us->pusb_dev->status);
450 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
451 result = usb_clear_halt(us->pusb_dev, pipe);
452 /* long wait for reset */
454 USB_STOR_PRINTF("BBB_reset result %d: status %lX clearing IN endpoint\n",
455 result, us->pusb_dev->status);
456 /* long wait for reset */
457 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
458 result = usb_clear_halt(us->pusb_dev, pipe);
460 USB_STOR_PRINTF("BBB_reset result %d: status %lX"
461 " clearing OUT endpoint\n", result,
462 us->pusb_dev->status);
463 USB_STOR_PRINTF("BBB_reset done\n");
467 /* FIXME: this reset function doesn't really reset the port, and it
468 * should. Actually it should probably do what it's doing here, and
469 * reset the port physically
471 static int usb_stor_CB_reset(struct us_data *us)
473 unsigned char cmd[12];
476 USB_STOR_PRINTF("CB_reset\n");
477 memset(cmd, 0xff, sizeof(cmd));
478 cmd[0] = SCSI_SEND_DIAG;
480 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
482 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
483 0, us->ifnum, cmd, sizeof(cmd),
484 USB_CNTL_TIMEOUT * 5);
486 /* long wait for reset */
488 USB_STOR_PRINTF("CB_reset result %d: status %lX"
489 " clearing endpoint halt\n", result,
490 us->pusb_dev->status);
491 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
492 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
494 USB_STOR_PRINTF("CB_reset done\n");
499 * Set up the command for a BBB device. Note that the actual SCSI
500 * command is copied into cbw.CBWCDB.
502 int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
508 ALLOC_CACHE_ALIGN_BUFFER(umass_bbb_cbw_t, cbw, 1);
510 dir_in = US_DIRECTION(srb->cmd[0]);
512 #ifdef BBB_COMDAT_TRACE
513 printf("dir %d lun %d cmdlen %d cmd %p datalen %d pdata %p\n",
514 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
517 for (result = 0; result < srb->cmdlen; result++)
518 printf("cmd[%d] %#x ", result, srb->cmd[result]);
523 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
524 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
528 /* always OUT to the ep */
529 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
531 cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE);
532 cbw->dCBWTag = cpu_to_le32(CBWTag++);
533 cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen);
534 cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
535 cbw->bCBWLUN = srb->lun;
536 cbw->bCDBLength = srb->cmdlen;
537 /* copy the command data into the CBW command data buffer */
539 memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
540 result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
541 &actlen, USB_CNTL_TIMEOUT * 5);
543 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
547 /* FIXME: we also need a CBI_command which sets up the completion
548 * interrupt, and waits for it
550 int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
555 unsigned long status;
558 dir_in = US_DIRECTION(srb->cmd[0]);
561 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
563 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
566 USB_STOR_PRINTF("CBI gets a command: Try %d\n", 5 - retry);
567 #ifdef USB_STOR_DEBUG
570 /* let's send the command via the control pipe */
571 result = usb_control_msg(us->pusb_dev,
572 usb_sndctrlpipe(us->pusb_dev , 0),
574 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
576 srb->cmd, srb->cmdlen,
577 USB_CNTL_TIMEOUT * 5);
578 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
579 " status %lX\n", result, us->pusb_dev->status);
580 /* check the return code for the command */
582 if (us->pusb_dev->status & USB_ST_STALLED) {
583 status = us->pusb_dev->status;
584 USB_STOR_PRINTF(" stall during command found,"
586 usb_clear_halt(us->pusb_dev,
587 usb_sndctrlpipe(us->pusb_dev, 0));
588 us->pusb_dev->status = status;
590 USB_STOR_PRINTF(" error during command %02X"
591 " Stat = %lX\n", srb->cmd[0],
592 us->pusb_dev->status);
595 /* transfer the data payload for this command, if one exists*/
597 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
598 " direction is %s to go 0x%lx\n", result,
599 dir_in ? "IN" : "OUT", srb->datalen);
601 result = us_one_transfer(us, pipe, (char *)srb->pdata,
603 USB_STOR_PRINTF("CBI attempted to transfer data,"
604 " result is %d status %lX, len %d\n",
605 result, us->pusb_dev->status,
606 us->pusb_dev->act_len);
607 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
609 } /* if (srb->datalen) */
619 int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
624 submit_int_msg(us->pusb_dev, us->irqpipe,
625 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
628 if ((volatile int *) us->ip_wanted == 0)
633 printf(" Did not get interrupt on CBI\n");
635 return USB_STOR_TRANSPORT_ERROR;
638 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
639 us->ip_data, us->pusb_dev->irq_act_len,
640 us->pusb_dev->irq_status);
641 /* UFI gives us ASC and ASCQ, like a request sense */
642 if (us->subclass == US_SC_UFI) {
643 if (srb->cmd[0] == SCSI_REQ_SENSE ||
644 srb->cmd[0] == SCSI_INQUIRY)
645 return USB_STOR_TRANSPORT_GOOD; /* Good */
646 else if (us->ip_data)
647 return USB_STOR_TRANSPORT_FAILED;
649 return USB_STOR_TRANSPORT_GOOD;
651 /* otherwise, we interpret the data normally */
652 switch (us->ip_data) {
654 return USB_STOR_TRANSPORT_GOOD;
656 return USB_STOR_TRANSPORT_FAILED;
658 return USB_STOR_TRANSPORT_ERROR;
660 return USB_STOR_TRANSPORT_ERROR;
663 #define USB_TRANSPORT_UNKNOWN_RETRY 5
664 #define USB_TRANSPORT_NOT_READY_RETRY 10
666 /* clear a stall on an endpoint - special for BBB devices */
667 int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
671 /* ENDPOINT_HALT = 0, so set value to 0 */
672 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
673 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
674 0, endpt, 0, 0, USB_CNTL_TIMEOUT * 5);
678 int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
682 int actlen, data_actlen;
683 unsigned int pipe, pipein, pipeout;
684 ALLOC_CACHE_ALIGN_BUFFER(umass_bbb_csw_t, csw, 1);
685 #ifdef BBB_XPORT_TRACE
690 dir_in = US_DIRECTION(srb->cmd[0]);
693 USB_STOR_PRINTF("COMMAND phase\n");
694 result = usb_stor_BBB_comdat(srb, us);
696 USB_STOR_PRINTF("failed to send CBW status %ld\n",
697 us->pusb_dev->status);
698 usb_stor_BBB_reset(us);
699 return USB_STOR_TRANSPORT_FAILED;
702 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
703 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
704 /* DATA phase + error handling */
706 /* no data, go immediately to the STATUS phase */
707 if (srb->datalen == 0)
709 USB_STOR_PRINTF("DATA phase\n");
714 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
715 &data_actlen, USB_CNTL_TIMEOUT * 5);
716 /* special handling of STALL in DATA phase */
717 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
718 USB_STOR_PRINTF("DATA:stall\n");
719 /* clear the STALL on the endpoint */
720 result = usb_stor_BBB_clear_endpt_stall(us,
721 dir_in ? us->ep_in : us->ep_out);
723 /* continue on to STATUS phase */
727 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
728 us->pusb_dev->status);
729 usb_stor_BBB_reset(us);
730 return USB_STOR_TRANSPORT_FAILED;
732 #ifdef BBB_XPORT_TRACE
733 for (index = 0; index < data_actlen; index++)
734 printf("pdata[%d] %#x ", index, srb->pdata[index]);
737 /* STATUS phase + error handling */
741 USB_STOR_PRINTF("STATUS phase\n");
742 result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE,
743 &actlen, USB_CNTL_TIMEOUT*5);
745 /* special handling of STALL in STATUS phase */
746 if ((result < 0) && (retry < 1) &&
747 (us->pusb_dev->status & USB_ST_STALLED)) {
748 USB_STOR_PRINTF("STATUS:stall\n");
749 /* clear the STALL on the endpoint */
750 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
751 if (result >= 0 && (retry++ < 1))
756 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
757 us->pusb_dev->status);
758 usb_stor_BBB_reset(us);
759 return USB_STOR_TRANSPORT_FAILED;
761 #ifdef BBB_XPORT_TRACE
762 ptr = (unsigned char *)csw;
763 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
764 printf("ptr[%d] %#x ", index, ptr[index]);
767 /* misuse pipe to get the residue */
768 pipe = le32_to_cpu(csw->dCSWDataResidue);
769 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
770 pipe = srb->datalen - data_actlen;
771 if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) {
772 USB_STOR_PRINTF("!CSWSIGNATURE\n");
773 usb_stor_BBB_reset(us);
774 return USB_STOR_TRANSPORT_FAILED;
775 } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) {
776 USB_STOR_PRINTF("!Tag\n");
777 usb_stor_BBB_reset(us);
778 return USB_STOR_TRANSPORT_FAILED;
779 } else if (csw->bCSWStatus > CSWSTATUS_PHASE) {
780 USB_STOR_PRINTF(">PHASE\n");
781 usb_stor_BBB_reset(us);
782 return USB_STOR_TRANSPORT_FAILED;
783 } else if (csw->bCSWStatus == CSWSTATUS_PHASE) {
784 USB_STOR_PRINTF("=PHASE\n");
785 usb_stor_BBB_reset(us);
786 return USB_STOR_TRANSPORT_FAILED;
787 } else if (data_actlen > srb->datalen) {
788 USB_STOR_PRINTF("transferred %dB instead of %ldB\n",
789 data_actlen, srb->datalen);
790 return USB_STOR_TRANSPORT_FAILED;
791 } else if (csw->bCSWStatus == CSWSTATUS_FAILED) {
792 USB_STOR_PRINTF("FAILED\n");
793 return USB_STOR_TRANSPORT_FAILED;
799 int usb_stor_CB_transport(ccb *srb, struct us_data *us)
807 status = USB_STOR_TRANSPORT_GOOD;
810 /* issue the command */
812 result = usb_stor_CB_comdat(srb, us);
813 USB_STOR_PRINTF("command / Data returned %d, status %lX\n",
814 result, us->pusb_dev->status);
815 /* if this is an CBI Protocol, get IRQ */
816 if (us->protocol == US_PR_CBI) {
817 status = usb_stor_CBI_get_status(srb, us);
818 /* if the status is error, report it */
819 if (status == USB_STOR_TRANSPORT_ERROR) {
820 USB_STOR_PRINTF(" USB CBI Command Error\n");
823 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
824 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
826 /* if the status is good, report it */
827 if (status == USB_STOR_TRANSPORT_GOOD) {
828 USB_STOR_PRINTF(" USB CBI Command Good\n");
833 /* do we have to issue an auto request? */
834 /* HERE we have to check the result */
835 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
836 USB_STOR_PRINTF("ERROR %lX\n", us->pusb_dev->status);
837 us->transport_reset(us);
838 return USB_STOR_TRANSPORT_ERROR;
840 if ((us->protocol == US_PR_CBI) &&
841 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
842 (srb->cmd[0] == SCSI_INQUIRY))) {
843 /* do not issue an autorequest after request sense */
844 USB_STOR_PRINTF("No auto request and good\n");
845 return USB_STOR_TRANSPORT_GOOD;
847 /* issue an request_sense */
848 memset(&psrb->cmd[0], 0, 12);
849 psrb->cmd[0] = SCSI_REQ_SENSE;
850 psrb->cmd[1] = srb->lun << 5;
853 psrb->pdata = &srb->sense_buf[0];
855 /* issue the command */
856 result = usb_stor_CB_comdat(psrb, us);
857 USB_STOR_PRINTF("auto request returned %d\n", result);
858 /* if this is an CBI Protocol, get IRQ */
859 if (us->protocol == US_PR_CBI)
860 status = usb_stor_CBI_get_status(psrb, us);
862 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
863 USB_STOR_PRINTF(" AUTO REQUEST ERROR %ld\n",
864 us->pusb_dev->status);
865 return USB_STOR_TRANSPORT_ERROR;
867 USB_STOR_PRINTF("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
868 srb->sense_buf[0], srb->sense_buf[2],
869 srb->sense_buf[12], srb->sense_buf[13]);
870 /* Check the auto request result */
871 if ((srb->sense_buf[2] == 0) &&
872 (srb->sense_buf[12] == 0) &&
873 (srb->sense_buf[13] == 0)) {
875 return USB_STOR_TRANSPORT_GOOD;
878 /* Check the auto request result */
879 switch (srb->sense_buf[2]) {
881 /* Recovered Error */
882 return USB_STOR_TRANSPORT_GOOD;
886 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
887 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
888 " 0x%02X (NOT READY)\n", srb->cmd[0],
889 srb->sense_buf[0], srb->sense_buf[2],
890 srb->sense_buf[12], srb->sense_buf[13]);
891 return USB_STOR_TRANSPORT_FAILED;
898 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
899 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
900 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
901 srb->sense_buf[2], srb->sense_buf[12],
903 return USB_STOR_TRANSPORT_FAILED;
908 return USB_STOR_TRANSPORT_FAILED;
912 static int usb_inquiry(ccb *srb, struct us_data *ss)
917 memset(&srb->cmd[0], 0, 12);
918 srb->cmd[0] = SCSI_INQUIRY;
919 srb->cmd[1] = srb->lun << 5;
923 i = ss->transport(srb, ss);
924 USB_STOR_PRINTF("inquiry returns %d\n", i);
930 printf("error in inquiry\n");
936 static int usb_request_sense(ccb *srb, struct us_data *ss)
940 ptr = (char *)srb->pdata;
941 memset(&srb->cmd[0], 0, 12);
942 srb->cmd[0] = SCSI_REQ_SENSE;
943 srb->cmd[1] = srb->lun << 5;
946 srb->pdata = &srb->sense_buf[0];
948 ss->transport(srb, ss);
949 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",
950 srb->sense_buf[2], srb->sense_buf[12],
952 srb->pdata = (uchar *)ptr;
956 static int usb_test_unit_ready(ccb *srb, struct us_data *ss)
961 memset(&srb->cmd[0], 0, 12);
962 srb->cmd[0] = SCSI_TST_U_RDY;
963 srb->cmd[1] = srb->lun << 5;
966 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
968 usb_request_sense(srb, ss);
975 static int usb_read_capacity(ccb *srb, struct us_data *ss)
981 memset(&srb->cmd[0], 0, 12);
982 srb->cmd[0] = SCSI_RD_CAPAC;
983 srb->cmd[1] = srb->lun << 5;
986 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
993 static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start,
994 unsigned short blocks)
996 memset(&srb->cmd[0], 0, 12);
997 srb->cmd[0] = SCSI_READ10;
998 srb->cmd[1] = srb->lun << 5;
999 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1000 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1001 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1002 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1003 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1004 srb->cmd[8] = (unsigned char) blocks & 0xff;
1006 USB_STOR_PRINTF("read10: start %lx blocks %x\n", start, blocks);
1007 return ss->transport(srb, ss);
1010 static int usb_write_10(ccb *srb, struct us_data *ss, unsigned long start,
1011 unsigned short blocks)
1013 memset(&srb->cmd[0], 0, 12);
1014 srb->cmd[0] = SCSI_WRITE10;
1015 srb->cmd[1] = srb->lun << 5;
1016 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1017 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1018 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1019 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1020 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1021 srb->cmd[8] = (unsigned char) blocks & 0xff;
1023 USB_STOR_PRINTF("write10: start %lx blocks %x\n", start, blocks);
1024 return ss->transport(srb, ss);
1028 #ifdef CONFIG_USB_BIN_FIXUP
1030 * Some USB storage devices queried for SCSI identification data respond with
1031 * binary strings, which if output to the console freeze the terminal. The
1032 * workaround is to modify the vendor and product strings read from such
1033 * device with proper values (as reported by 'usb info').
1035 * Vendor and product length limits are taken from the definition of
1036 * block_dev_desc_t in include/part.h.
1038 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
1039 unsigned char vendor[],
1040 unsigned char product[]) {
1041 const unsigned char max_vendor_len = 40;
1042 const unsigned char max_product_len = 20;
1043 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
1044 strncpy((char *)vendor, "SMSC", max_vendor_len);
1045 strncpy((char *)product, "Flash Media Cntrller",
1049 #endif /* CONFIG_USB_BIN_FIXUP */
1051 unsigned long usb_stor_read(int device, unsigned long blknr,
1052 unsigned long blkcnt, void *buffer)
1054 unsigned long start, blks, buf_addr;
1055 unsigned short smallblks;
1056 struct usb_device *dev;
1059 ccb *srb = &usb_ccb;
1066 USB_STOR_PRINTF("\nusb_read: dev %d \n", device);
1068 for (i = 0; i < USB_MAX_DEVICE; i++) {
1069 dev = usb_get_dev_index(i);
1072 if (dev->devnum == usb_dev_desc[device].target)
1075 ss = (struct us_data *)dev->privptr;
1077 usb_disable_asynch(1); /* asynch transfer not allowed */
1078 srb->lun = usb_dev_desc[device].lun;
1079 buf_addr = (unsigned long)buffer;
1082 if (usb_test_unit_ready(srb, ss)) {
1083 printf("Device NOT ready\n Request Sense returned %02X %02X"
1084 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1085 srb->sense_buf[13]);
1089 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx"
1090 " buffer %lx\n", device, start, blks, buf_addr);
1093 /* XXX need some comment here */
1095 srb->pdata = (unsigned char *)buf_addr;
1096 if (blks > USB_MAX_XFER_BLK)
1097 smallblks = USB_MAX_XFER_BLK;
1099 smallblks = (unsigned short) blks;
1101 if (smallblks == USB_MAX_XFER_BLK)
1102 usb_show_progress();
1103 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1104 srb->pdata = (unsigned char *)buf_addr;
1105 if (usb_read_10(srb, ss, start, smallblks)) {
1106 USB_STOR_PRINTF("Read ERROR\n");
1107 usb_request_sense(srb, ss);
1115 buf_addr += srb->datalen;
1116 } while (blks != 0);
1118 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",
1119 start, smallblks, buf_addr);
1121 usb_disable_asynch(0); /* asynch transfer allowed */
1122 if (blkcnt >= USB_MAX_XFER_BLK)
1127 unsigned long usb_stor_write(int device, unsigned long blknr,
1128 unsigned long blkcnt, const void *buffer)
1130 unsigned long start, blks, buf_addr;
1131 unsigned short smallblks;
1132 struct usb_device *dev;
1135 ccb *srb = &usb_ccb;
1142 USB_STOR_PRINTF("\nusb_write: dev %d \n", device);
1144 for (i = 0; i < USB_MAX_DEVICE; i++) {
1145 dev = usb_get_dev_index(i);
1148 if (dev->devnum == usb_dev_desc[device].target)
1151 ss = (struct us_data *)dev->privptr;
1153 usb_disable_asynch(1); /* asynch transfer not allowed */
1155 srb->lun = usb_dev_desc[device].lun;
1156 buf_addr = (unsigned long)buffer;
1159 if (usb_test_unit_ready(srb, ss)) {
1160 printf("Device NOT ready\n Request Sense returned %02X %02X"
1161 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1162 srb->sense_buf[13]);
1166 USB_STOR_PRINTF("\nusb_write: dev %d startblk %lx, blccnt %lx"
1167 " buffer %lx\n", device, start, blks, buf_addr);
1170 /* If write fails retry for max retry count else
1171 * return with number of blocks written successfully.
1174 srb->pdata = (unsigned char *)buf_addr;
1175 if (blks > USB_MAX_XFER_BLK)
1176 smallblks = USB_MAX_XFER_BLK;
1178 smallblks = (unsigned short) blks;
1180 if (smallblks == USB_MAX_XFER_BLK)
1181 usb_show_progress();
1182 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1183 srb->pdata = (unsigned char *)buf_addr;
1184 if (usb_write_10(srb, ss, start, smallblks)) {
1185 USB_STOR_PRINTF("Write ERROR\n");
1186 usb_request_sense(srb, ss);
1194 buf_addr += srb->datalen;
1195 } while (blks != 0);
1197 USB_STOR_PRINTF("usb_write: end startblk %lx, blccnt %x buffer %lx\n",
1198 start, smallblks, buf_addr);
1200 usb_disable_asynch(0); /* asynch transfer allowed */
1201 if (blkcnt >= USB_MAX_XFER_BLK)
1207 /* Probe to see if a new device is actually a Storage device */
1208 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1211 struct usb_interface *iface;
1213 unsigned int flags = 0;
1218 /* let's examine the device now */
1219 iface = &dev->config.if_desc[ifnum];
1222 /* this is the place to patch some storage devices */
1223 USB_STOR_PRINTF("iVendor %X iProduct %X\n", dev->descriptor.idVendor,
1224 dev->descriptor.idProduct);
1226 if ((dev->descriptor.idVendor) == 0x066b &&
1227 (dev->descriptor.idProduct) == 0x0103) {
1228 USB_STOR_PRINTF("patched for E-USB\n");
1229 protocol = US_PR_CB;
1230 subclass = US_SC_UFI; /* an assumption */
1234 if (dev->descriptor.bDeviceClass != 0 ||
1235 iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1236 iface->desc.bInterfaceSubClass < US_SC_MIN ||
1237 iface->desc.bInterfaceSubClass > US_SC_MAX) {
1238 /* if it's not a mass storage, we go no further */
1242 memset(ss, 0, sizeof(struct us_data));
1244 /* At this point, we know we've got a live one */
1245 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1247 /* Initialize the us_data structure with some useful info */
1251 ss->attention_done = 0;
1253 /* If the device has subclass and protocol, then use that. Otherwise,
1254 * take data from the specific interface.
1257 ss->subclass = subclass;
1258 ss->protocol = protocol;
1260 ss->subclass = iface->desc.bInterfaceSubClass;
1261 ss->protocol = iface->desc.bInterfaceProtocol;
1264 /* set the handler pointers based on the protocol */
1265 USB_STOR_PRINTF("Transport: ");
1266 switch (ss->protocol) {
1268 USB_STOR_PRINTF("Control/Bulk\n");
1269 ss->transport = usb_stor_CB_transport;
1270 ss->transport_reset = usb_stor_CB_reset;
1274 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1275 ss->transport = usb_stor_CB_transport;
1276 ss->transport_reset = usb_stor_CB_reset;
1279 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1280 ss->transport = usb_stor_BBB_transport;
1281 ss->transport_reset = usb_stor_BBB_reset;
1284 printf("USB Storage Transport unknown / not yet implemented\n");
1290 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1291 * An optional interrupt is OK (necessary for CBI protocol).
1292 * We will ignore any others.
1294 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1295 /* is it an BULK endpoint? */
1296 if ((iface->ep_desc[i].bmAttributes &
1297 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1298 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1299 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1300 USB_ENDPOINT_NUMBER_MASK;
1303 iface->ep_desc[i].bEndpointAddress &
1304 USB_ENDPOINT_NUMBER_MASK;
1307 /* is it an interrupt endpoint? */
1308 if ((iface->ep_desc[i].bmAttributes &
1309 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1310 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1311 USB_ENDPOINT_NUMBER_MASK;
1312 ss->irqinterval = iface->ep_desc[i].bInterval;
1315 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1316 ss->ep_in, ss->ep_out, ss->ep_int);
1318 /* Do some basic sanity checks, and bail if we find a problem */
1319 if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
1320 !ss->ep_in || !ss->ep_out ||
1321 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1322 USB_STOR_PRINTF("Problems with device\n");
1325 /* set class specific stuff */
1326 /* We only handle certain protocols. Currently, these are
1328 * The SFF8070 accepts the requests used in u-boot
1330 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1331 ss->subclass != US_SC_8070) {
1332 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
1336 /* we had found an interrupt endpoint, prepare irq pipe
1337 * set up the IRQ pipe and handler
1339 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1340 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1341 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1342 dev->irq_handle = usb_stor_irq;
1344 dev->privptr = (void *)ss;
1348 int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1349 block_dev_desc_t *dev_desc)
1351 unsigned char perq, modi;
1352 ALLOC_CACHE_ALIGN_BUFFER(unsigned long, cap, 2);
1353 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, usb_stor_buf, 36);
1354 unsigned long *capacity, *blksz;
1355 ccb *pccb = &usb_ccb;
1357 pccb->pdata = usb_stor_buf;
1359 dev_desc->target = dev->devnum;
1360 pccb->lun = dev_desc->lun;
1361 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1363 if (usb_inquiry(pccb, ss))
1366 perq = usb_stor_buf[0];
1367 modi = usb_stor_buf[1];
1369 if ((perq & 0x1f) == 0x1f) {
1370 /* skip unknown devices */
1373 if ((modi&0x80) == 0x80) {
1374 /* drive is removable */
1375 dev_desc->removable = 1;
1377 memcpy(&dev_desc->vendor[0], (const void *) &usb_stor_buf[8], 8);
1378 memcpy(&dev_desc->product[0], (const void *) &usb_stor_buf[16], 16);
1379 memcpy(&dev_desc->revision[0], (const void *) &usb_stor_buf[32], 4);
1380 dev_desc->vendor[8] = 0;
1381 dev_desc->product[16] = 0;
1382 dev_desc->revision[4] = 0;
1383 #ifdef CONFIG_USB_BIN_FIXUP
1384 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1385 (uchar *)dev_desc->product);
1386 #endif /* CONFIG_USB_BIN_FIXUP */
1387 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1389 if (usb_test_unit_ready(pccb, ss)) {
1390 printf("Device NOT ready\n"
1391 " Request Sense returned %02X %02X %02X\n",
1392 pccb->sense_buf[2], pccb->sense_buf[12],
1393 pccb->sense_buf[13]);
1394 if (dev_desc->removable == 1) {
1395 dev_desc->type = perq;
1400 pccb->pdata = (unsigned char *)&cap[0];
1401 memset(pccb->pdata, 0, 8);
1402 if (usb_read_capacity(pccb, ss) != 0) {
1403 printf("READ_CAP ERROR\n");
1407 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0],
1410 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1413 cap[0] = cpu_to_be32(cap[0]);
1414 cap[1] = cpu_to_be32(cap[1]);
1416 /* this assumes bigendian! */
1420 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",
1422 dev_desc->lba = *capacity;
1423 dev_desc->blksz = *blksz;
1424 dev_desc->type = perq;
1425 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1426 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);
1428 init_part(dev_desc);
1430 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);