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 # define USB_READY (1 << 0)
140 unsigned char ifnum; /* interface number */
141 unsigned char ep_in; /* in endpoint */
142 unsigned char ep_out; /* out ....... */
143 unsigned char ep_int; /* interrupt . */
144 unsigned char subclass; /* as in overview */
145 unsigned char protocol; /* .............. */
146 unsigned char attention_done; /* force attn on first cmd */
147 unsigned short ip_data; /* interrupt data */
148 int action; /* what to do */
149 int ip_wanted; /* needed */
150 int *irq_handle; /* for USB int requests */
151 unsigned int irqpipe; /* pipe for release_irq */
152 unsigned char irqmaxp; /* max packed for irq Pipe */
153 unsigned char irqinterval; /* Intervall for IRQ Pipe */
154 ccb *srb; /* current srb */
155 trans_reset transport_reset; /* reset routine */
156 trans_cmnd transport; /* transport routine */
159 #ifdef CONFIG_USB_EHCI
161 * The U-Boot EHCI driver can handle any transfer length as long as there is
162 * enough free heap space left, but the SCSI READ(10) and WRITE(10) commands are
163 * limited to 65535 blocks.
165 #define USB_MAX_XFER_BLK 65535
167 #define USB_MAX_XFER_BLK 20
170 static struct us_data usb_stor[USB_MAX_STOR_DEV];
173 #define USB_STOR_TRANSPORT_GOOD 0
174 #define USB_STOR_TRANSPORT_FAILED -1
175 #define USB_STOR_TRANSPORT_ERROR -2
177 int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
178 block_dev_desc_t *dev_desc);
179 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
181 unsigned long usb_stor_read(int device, unsigned long blknr,
182 lbaint_t blkcnt, void *buffer);
183 unsigned long usb_stor_write(int device, unsigned long blknr,
184 lbaint_t blkcnt, const void *buffer);
185 struct usb_device * usb_get_dev_index(int index);
186 void uhci_show_temp_int_td(void);
188 #ifdef CONFIG_PARTITIONS
189 block_dev_desc_t *usb_stor_get_dev(int index)
191 return (index < usb_max_devs) ? &usb_dev_desc[index] : NULL;
195 static void usb_show_progress(void)
200 /*******************************************************************************
201 * show info on storage devices; 'usb start/init' must be invoked earlier
202 * as we only retrieve structures populated during devices initialization
204 int usb_stor_info(void)
208 if (usb_max_devs > 0) {
209 for (i = 0; i < usb_max_devs; i++) {
210 printf(" Device %d: ", i);
211 dev_print(&usb_dev_desc[i]);
216 printf("No storage devices, perhaps not 'usb start'ed..?\n");
220 static unsigned int usb_get_max_lun(struct us_data *us)
223 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
224 len = usb_control_msg(us->pusb_dev,
225 usb_rcvctrlpipe(us->pusb_dev, 0),
227 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
229 result, sizeof(char),
230 USB_CNTL_TIMEOUT * 5);
231 USB_STOR_PRINTF("Get Max LUN -> len = %i, result = %i\n",
233 return (len > 0) ? *result : 0;
236 /*******************************************************************************
237 * scan the usb and reports device info
238 * to the user if mode = 1
239 * returns current device or -1 if no
241 int usb_stor_scan(int mode)
244 struct usb_device *dev;
247 printf(" scanning usb for storage devices... ");
249 usb_disable_asynch(1); /* asynch transfer not allowed */
251 for (i = 0; i < USB_MAX_STOR_DEV; i++) {
252 memset(&usb_dev_desc[i], 0, sizeof(block_dev_desc_t));
253 usb_dev_desc[i].if_type = IF_TYPE_USB;
254 usb_dev_desc[i].dev = i;
255 usb_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
256 usb_dev_desc[i].target = 0xff;
257 usb_dev_desc[i].type = DEV_TYPE_UNKNOWN;
258 usb_dev_desc[i].block_read = usb_stor_read;
259 usb_dev_desc[i].block_write = usb_stor_write;
263 for (i = 0; i < USB_MAX_DEVICE; i++) {
264 dev = usb_get_dev_index(i); /* get device */
265 USB_STOR_PRINTF("i=%d\n", i);
267 break; /* no more devices available */
269 if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
270 /* OK, it's a storage device. Iterate over its LUNs
271 * and populate `usb_dev_desc'.
273 int lun, max_lun, start = usb_max_devs;
275 max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
277 lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
279 usb_dev_desc[usb_max_devs].lun = lun;
280 if (usb_stor_get_info(dev, &usb_stor[start],
281 &usb_dev_desc[usb_max_devs]) == 1) {
286 /* if storage device */
287 if (usb_max_devs == USB_MAX_STOR_DEV) {
288 printf("max USB Storage Device reached: %d stopping\n",
294 usb_disable_asynch(0); /* asynch transfer allowed */
295 printf("%d Storage Device(s) found\n", usb_max_devs);
296 if (usb_max_devs > 0)
301 static int usb_stor_irq(struct usb_device *dev)
304 us = (struct us_data *)dev->privptr;
312 #ifdef USB_STOR_DEBUG
314 static void usb_show_srb(ccb *pccb)
317 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
318 for (i = 0; i < 12; i++)
319 printf("%02X ", pccb->cmd[i]);
323 static void display_int_status(unsigned long tmp)
325 printf("Status: %s %s %s %s %s %s %s\n",
326 (tmp & USB_ST_ACTIVE) ? "Active" : "",
327 (tmp & USB_ST_STALLED) ? "Stalled" : "",
328 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
329 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
330 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
331 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
332 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
335 /***********************************************************************
336 * Data transfer routines
337 ***********************************************************************/
339 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
348 /* determine the maximum packet size for these transfers */
349 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
351 /* while we have data left to transfer */
354 /* calculate how long this will be -- maximum or a remainder */
355 this_xfer = length > max_size ? max_size : length;
358 /* setup the retry counter */
361 /* set up the transfer loop */
363 /* transfer the data */
364 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
365 (unsigned int)buf, this_xfer, 11 - maxtry);
366 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
368 USB_CNTL_TIMEOUT * 5);
369 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
370 result, partial, this_xfer);
371 if (us->pusb_dev->status != 0) {
372 /* if we stall, we need to clear it before
375 #ifdef USB_STOR_DEBUG
376 display_int_status(us->pusb_dev->status);
378 if (us->pusb_dev->status & USB_ST_STALLED) {
379 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
380 stat = us->pusb_dev->status;
381 usb_clear_halt(us->pusb_dev, pipe);
382 us->pusb_dev->status = stat;
383 if (this_xfer == partial) {
384 USB_STOR_PRINTF("bulk transferred with error %lX, but data ok\n", us->pusb_dev->status);
390 if (us->pusb_dev->status & USB_ST_NAK_REC) {
391 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
394 USB_STOR_PRINTF("bulk transferred with error");
395 if (this_xfer == partial) {
396 USB_STOR_PRINTF(" %ld, but data ok\n",
397 us->pusb_dev->status);
400 /* if our try counter reaches 0, bail out */
401 USB_STOR_PRINTF(" %ld, data %d\n",
402 us->pusb_dev->status, partial);
406 /* update to show what data was transferred */
407 this_xfer -= partial;
409 /* continue until this transfer is done */
413 /* if we get here, we're done and successful */
417 static int usb_stor_BBB_reset(struct us_data *us)
423 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
425 * For Reset Recovery the host shall issue in the following order:
426 * a) a Bulk-Only Mass Storage Reset
427 * b) a Clear Feature HALT to the Bulk-In endpoint
428 * c) a Clear Feature HALT to the Bulk-Out endpoint
430 * This is done in 3 steps.
432 * If the reset doesn't succeed, the device should be port reset.
434 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
436 USB_STOR_PRINTF("BBB_reset\n");
437 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
439 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
440 0, us->ifnum, NULL, 0, USB_CNTL_TIMEOUT * 5);
442 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
443 USB_STOR_PRINTF("RESET:stall\n");
447 /* long wait for reset */
449 USB_STOR_PRINTF("BBB_reset result %d: status %lX reset\n", result,
450 us->pusb_dev->status);
451 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
452 result = usb_clear_halt(us->pusb_dev, pipe);
453 /* long wait for reset */
455 USB_STOR_PRINTF("BBB_reset result %d: status %lX clearing IN endpoint\n",
456 result, us->pusb_dev->status);
457 /* long wait for reset */
458 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
459 result = usb_clear_halt(us->pusb_dev, pipe);
461 USB_STOR_PRINTF("BBB_reset result %d: status %lX"
462 " clearing OUT endpoint\n", result,
463 us->pusb_dev->status);
464 USB_STOR_PRINTF("BBB_reset done\n");
468 /* FIXME: this reset function doesn't really reset the port, and it
469 * should. Actually it should probably do what it's doing here, and
470 * reset the port physically
472 static int usb_stor_CB_reset(struct us_data *us)
474 unsigned char cmd[12];
477 USB_STOR_PRINTF("CB_reset\n");
478 memset(cmd, 0xff, sizeof(cmd));
479 cmd[0] = SCSI_SEND_DIAG;
481 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
483 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
484 0, us->ifnum, cmd, sizeof(cmd),
485 USB_CNTL_TIMEOUT * 5);
487 /* long wait for reset */
489 USB_STOR_PRINTF("CB_reset result %d: status %lX"
490 " clearing endpoint halt\n", result,
491 us->pusb_dev->status);
492 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
493 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
495 USB_STOR_PRINTF("CB_reset done\n");
500 * Set up the command for a BBB device. Note that the actual SCSI
501 * command is copied into cbw.CBWCDB.
503 static int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
509 ALLOC_CACHE_ALIGN_BUFFER(umass_bbb_cbw_t, cbw, 1);
511 dir_in = US_DIRECTION(srb->cmd[0]);
513 #ifdef BBB_COMDAT_TRACE
514 printf("dir %d lun %d cmdlen %d cmd %p datalen %d pdata %p\n",
515 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
518 for (result = 0; result < srb->cmdlen; result++)
519 printf("cmd[%d] %#x ", result, srb->cmd[result]);
524 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
525 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
529 /* always OUT to the ep */
530 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
532 cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE);
533 cbw->dCBWTag = cpu_to_le32(CBWTag++);
534 cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen);
535 cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
536 cbw->bCBWLUN = srb->lun;
537 cbw->bCDBLength = srb->cmdlen;
538 /* copy the command data into the CBW command data buffer */
540 memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
541 result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
542 &actlen, USB_CNTL_TIMEOUT * 5);
544 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
548 /* FIXME: we also need a CBI_command which sets up the completion
549 * interrupt, and waits for it
551 static int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
556 unsigned long status;
559 dir_in = US_DIRECTION(srb->cmd[0]);
562 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
564 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
567 USB_STOR_PRINTF("CBI gets a command: Try %d\n", 5 - retry);
568 #ifdef USB_STOR_DEBUG
571 /* let's send the command via the control pipe */
572 result = usb_control_msg(us->pusb_dev,
573 usb_sndctrlpipe(us->pusb_dev , 0),
575 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
577 srb->cmd, srb->cmdlen,
578 USB_CNTL_TIMEOUT * 5);
579 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
580 " status %lX\n", result, us->pusb_dev->status);
581 /* check the return code for the command */
583 if (us->pusb_dev->status & USB_ST_STALLED) {
584 status = us->pusb_dev->status;
585 USB_STOR_PRINTF(" stall during command found,"
587 usb_clear_halt(us->pusb_dev,
588 usb_sndctrlpipe(us->pusb_dev, 0));
589 us->pusb_dev->status = status;
591 USB_STOR_PRINTF(" error during command %02X"
592 " Stat = %lX\n", srb->cmd[0],
593 us->pusb_dev->status);
596 /* transfer the data payload for this command, if one exists*/
598 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
599 " direction is %s to go 0x%lx\n", result,
600 dir_in ? "IN" : "OUT", srb->datalen);
602 result = us_one_transfer(us, pipe, (char *)srb->pdata,
604 USB_STOR_PRINTF("CBI attempted to transfer data,"
605 " result is %d status %lX, len %d\n",
606 result, us->pusb_dev->status,
607 us->pusb_dev->act_len);
608 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
610 } /* if (srb->datalen) */
620 static int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
625 submit_int_msg(us->pusb_dev, us->irqpipe,
626 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
629 if ((volatile int *) us->ip_wanted == NULL)
634 printf(" Did not get interrupt on CBI\n");
636 return USB_STOR_TRANSPORT_ERROR;
639 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
640 us->ip_data, us->pusb_dev->irq_act_len,
641 us->pusb_dev->irq_status);
642 /* UFI gives us ASC and ASCQ, like a request sense */
643 if (us->subclass == US_SC_UFI) {
644 if (srb->cmd[0] == SCSI_REQ_SENSE ||
645 srb->cmd[0] == SCSI_INQUIRY)
646 return USB_STOR_TRANSPORT_GOOD; /* Good */
647 else if (us->ip_data)
648 return USB_STOR_TRANSPORT_FAILED;
650 return USB_STOR_TRANSPORT_GOOD;
652 /* otherwise, we interpret the data normally */
653 switch (us->ip_data) {
655 return USB_STOR_TRANSPORT_GOOD;
657 return USB_STOR_TRANSPORT_FAILED;
659 return USB_STOR_TRANSPORT_ERROR;
661 return USB_STOR_TRANSPORT_ERROR;
664 #define USB_TRANSPORT_UNKNOWN_RETRY 5
665 #define USB_TRANSPORT_NOT_READY_RETRY 10
667 /* clear a stall on an endpoint - special for BBB devices */
668 static int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
672 /* ENDPOINT_HALT = 0, so set value to 0 */
673 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
674 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
675 0, endpt, NULL, 0, USB_CNTL_TIMEOUT * 5);
679 static int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
683 int actlen, data_actlen;
684 unsigned int pipe, pipein, pipeout;
685 ALLOC_CACHE_ALIGN_BUFFER(umass_bbb_csw_t, csw, 1);
686 #ifdef BBB_XPORT_TRACE
691 dir_in = US_DIRECTION(srb->cmd[0]);
694 USB_STOR_PRINTF("COMMAND phase\n");
695 result = usb_stor_BBB_comdat(srb, us);
697 USB_STOR_PRINTF("failed to send CBW status %ld\n",
698 us->pusb_dev->status);
699 usb_stor_BBB_reset(us);
700 return USB_STOR_TRANSPORT_FAILED;
702 if (!(us->flags & USB_READY))
704 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
705 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
706 /* DATA phase + error handling */
708 /* no data, go immediately to the STATUS phase */
709 if (srb->datalen == 0)
711 USB_STOR_PRINTF("DATA phase\n");
716 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
717 &data_actlen, USB_CNTL_TIMEOUT * 5);
718 /* special handling of STALL in DATA phase */
719 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
720 USB_STOR_PRINTF("DATA:stall\n");
721 /* clear the STALL on the endpoint */
722 result = usb_stor_BBB_clear_endpt_stall(us,
723 dir_in ? us->ep_in : us->ep_out);
725 /* continue on to STATUS phase */
729 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
730 us->pusb_dev->status);
731 usb_stor_BBB_reset(us);
732 return USB_STOR_TRANSPORT_FAILED;
734 #ifdef BBB_XPORT_TRACE
735 for (index = 0; index < data_actlen; index++)
736 printf("pdata[%d] %#x ", index, srb->pdata[index]);
739 /* STATUS phase + error handling */
743 USB_STOR_PRINTF("STATUS phase\n");
744 result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE,
745 &actlen, USB_CNTL_TIMEOUT*5);
747 /* special handling of STALL in STATUS phase */
748 if ((result < 0) && (retry < 1) &&
749 (us->pusb_dev->status & USB_ST_STALLED)) {
750 USB_STOR_PRINTF("STATUS:stall\n");
751 /* clear the STALL on the endpoint */
752 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
753 if (result >= 0 && (retry++ < 1))
758 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
759 us->pusb_dev->status);
760 usb_stor_BBB_reset(us);
761 return USB_STOR_TRANSPORT_FAILED;
763 #ifdef BBB_XPORT_TRACE
764 ptr = (unsigned char *)csw;
765 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
766 printf("ptr[%d] %#x ", index, ptr[index]);
769 /* misuse pipe to get the residue */
770 pipe = le32_to_cpu(csw->dCSWDataResidue);
771 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
772 pipe = srb->datalen - data_actlen;
773 if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) {
774 USB_STOR_PRINTF("!CSWSIGNATURE\n");
775 usb_stor_BBB_reset(us);
776 return USB_STOR_TRANSPORT_FAILED;
777 } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) {
778 USB_STOR_PRINTF("!Tag\n");
779 usb_stor_BBB_reset(us);
780 return USB_STOR_TRANSPORT_FAILED;
781 } else if (csw->bCSWStatus > CSWSTATUS_PHASE) {
782 USB_STOR_PRINTF(">PHASE\n");
783 usb_stor_BBB_reset(us);
784 return USB_STOR_TRANSPORT_FAILED;
785 } else if (csw->bCSWStatus == CSWSTATUS_PHASE) {
786 USB_STOR_PRINTF("=PHASE\n");
787 usb_stor_BBB_reset(us);
788 return USB_STOR_TRANSPORT_FAILED;
789 } else if (data_actlen > srb->datalen) {
790 USB_STOR_PRINTF("transferred %dB instead of %ldB\n",
791 data_actlen, srb->datalen);
792 return USB_STOR_TRANSPORT_FAILED;
793 } else if (csw->bCSWStatus == CSWSTATUS_FAILED) {
794 USB_STOR_PRINTF("FAILED\n");
795 return USB_STOR_TRANSPORT_FAILED;
801 static int usb_stor_CB_transport(ccb *srb, struct us_data *us)
809 status = USB_STOR_TRANSPORT_GOOD;
812 /* issue the command */
814 result = usb_stor_CB_comdat(srb, us);
815 USB_STOR_PRINTF("command / Data returned %d, status %lX\n",
816 result, us->pusb_dev->status);
817 /* if this is an CBI Protocol, get IRQ */
818 if (us->protocol == US_PR_CBI) {
819 status = usb_stor_CBI_get_status(srb, us);
820 /* if the status is error, report it */
821 if (status == USB_STOR_TRANSPORT_ERROR) {
822 USB_STOR_PRINTF(" USB CBI Command Error\n");
825 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
826 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
828 /* if the status is good, report it */
829 if (status == USB_STOR_TRANSPORT_GOOD) {
830 USB_STOR_PRINTF(" USB CBI Command Good\n");
835 /* do we have to issue an auto request? */
836 /* HERE we have to check the result */
837 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
838 USB_STOR_PRINTF("ERROR %lX\n", us->pusb_dev->status);
839 us->transport_reset(us);
840 return USB_STOR_TRANSPORT_ERROR;
842 if ((us->protocol == US_PR_CBI) &&
843 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
844 (srb->cmd[0] == SCSI_INQUIRY))) {
845 /* do not issue an autorequest after request sense */
846 USB_STOR_PRINTF("No auto request and good\n");
847 return USB_STOR_TRANSPORT_GOOD;
849 /* issue an request_sense */
850 memset(&psrb->cmd[0], 0, 12);
851 psrb->cmd[0] = SCSI_REQ_SENSE;
852 psrb->cmd[1] = srb->lun << 5;
855 psrb->pdata = &srb->sense_buf[0];
857 /* issue the command */
858 result = usb_stor_CB_comdat(psrb, us);
859 USB_STOR_PRINTF("auto request returned %d\n", result);
860 /* if this is an CBI Protocol, get IRQ */
861 if (us->protocol == US_PR_CBI)
862 status = usb_stor_CBI_get_status(psrb, us);
864 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
865 USB_STOR_PRINTF(" AUTO REQUEST ERROR %ld\n",
866 us->pusb_dev->status);
867 return USB_STOR_TRANSPORT_ERROR;
869 USB_STOR_PRINTF("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
870 srb->sense_buf[0], srb->sense_buf[2],
871 srb->sense_buf[12], srb->sense_buf[13]);
872 /* Check the auto request result */
873 if ((srb->sense_buf[2] == 0) &&
874 (srb->sense_buf[12] == 0) &&
875 (srb->sense_buf[13] == 0)) {
877 return USB_STOR_TRANSPORT_GOOD;
880 /* Check the auto request result */
881 switch (srb->sense_buf[2]) {
883 /* Recovered Error */
884 return USB_STOR_TRANSPORT_GOOD;
888 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
889 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
890 " 0x%02X (NOT READY)\n", srb->cmd[0],
891 srb->sense_buf[0], srb->sense_buf[2],
892 srb->sense_buf[12], srb->sense_buf[13]);
893 return USB_STOR_TRANSPORT_FAILED;
900 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
901 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
902 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
903 srb->sense_buf[2], srb->sense_buf[12],
905 return USB_STOR_TRANSPORT_FAILED;
910 return USB_STOR_TRANSPORT_FAILED;
914 static int usb_inquiry(ccb *srb, struct us_data *ss)
919 memset(&srb->cmd[0], 0, 12);
920 srb->cmd[0] = SCSI_INQUIRY;
921 srb->cmd[1] = srb->lun << 5;
925 i = ss->transport(srb, ss);
926 USB_STOR_PRINTF("inquiry returns %d\n", i);
932 printf("error in inquiry\n");
938 static int usb_request_sense(ccb *srb, struct us_data *ss)
942 ptr = (char *)srb->pdata;
943 memset(&srb->cmd[0], 0, 12);
944 srb->cmd[0] = SCSI_REQ_SENSE;
945 srb->cmd[1] = srb->lun << 5;
948 srb->pdata = &srb->sense_buf[0];
950 ss->transport(srb, ss);
951 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",
952 srb->sense_buf[2], srb->sense_buf[12],
954 srb->pdata = (uchar *)ptr;
958 static int usb_test_unit_ready(ccb *srb, struct us_data *ss)
963 memset(&srb->cmd[0], 0, 12);
964 srb->cmd[0] = SCSI_TST_U_RDY;
965 srb->cmd[1] = srb->lun << 5;
968 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD) {
969 ss->flags |= USB_READY;
972 usb_request_sense(srb, ss);
974 * Check the Key Code Qualifier, if it matches
975 * "Not Ready - medium not present"
976 * (the sense Key equals 0x2 and the ASC is 0x3a)
977 * return immediately as the medium being absent won't change
978 * unless there is a user action.
980 if ((srb->sense_buf[2] == 0x02) &&
981 (srb->sense_buf[12] == 0x3a))
989 static int usb_read_capacity(ccb *srb, struct us_data *ss)
995 memset(&srb->cmd[0], 0, 12);
996 srb->cmd[0] = SCSI_RD_CAPAC;
997 srb->cmd[1] = srb->lun << 5;
1000 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
1007 static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start,
1008 unsigned short blocks)
1010 memset(&srb->cmd[0], 0, 12);
1011 srb->cmd[0] = SCSI_READ10;
1012 srb->cmd[1] = srb->lun << 5;
1013 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1014 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1015 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1016 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1017 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1018 srb->cmd[8] = (unsigned char) blocks & 0xff;
1020 USB_STOR_PRINTF("read10: start %lx blocks %x\n", start, blocks);
1021 return ss->transport(srb, ss);
1024 static int usb_write_10(ccb *srb, struct us_data *ss, unsigned long start,
1025 unsigned short blocks)
1027 memset(&srb->cmd[0], 0, 12);
1028 srb->cmd[0] = SCSI_WRITE10;
1029 srb->cmd[1] = srb->lun << 5;
1030 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1031 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1032 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1033 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1034 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1035 srb->cmd[8] = (unsigned char) blocks & 0xff;
1037 USB_STOR_PRINTF("write10: start %lx blocks %x\n", start, blocks);
1038 return ss->transport(srb, ss);
1042 #ifdef CONFIG_USB_BIN_FIXUP
1044 * Some USB storage devices queried for SCSI identification data respond with
1045 * binary strings, which if output to the console freeze the terminal. The
1046 * workaround is to modify the vendor and product strings read from such
1047 * device with proper values (as reported by 'usb info').
1049 * Vendor and product length limits are taken from the definition of
1050 * block_dev_desc_t in include/part.h.
1052 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
1053 unsigned char vendor[],
1054 unsigned char product[]) {
1055 const unsigned char max_vendor_len = 40;
1056 const unsigned char max_product_len = 20;
1057 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
1058 strncpy((char *)vendor, "SMSC", max_vendor_len);
1059 strncpy((char *)product, "Flash Media Cntrller",
1063 #endif /* CONFIG_USB_BIN_FIXUP */
1065 unsigned long usb_stor_read(int device, unsigned long blknr,
1066 lbaint_t blkcnt, void *buffer)
1068 lbaint_t start, blks;
1070 unsigned short smallblks;
1071 struct usb_device *dev;
1074 ccb *srb = &usb_ccb;
1081 USB_STOR_PRINTF("\nusb_read: dev %d \n", device);
1083 for (i = 0; i < USB_MAX_DEVICE; i++) {
1084 dev = usb_get_dev_index(i);
1087 if (dev->devnum == usb_dev_desc[device].target)
1090 ss = (struct us_data *)dev->privptr;
1092 usb_disable_asynch(1); /* asynch transfer not allowed */
1093 srb->lun = usb_dev_desc[device].lun;
1094 buf_addr = (unsigned long)buffer;
1098 USB_STOR_PRINTF("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF
1099 " buffer %lx\n", device, start, blks, buf_addr);
1102 /* XXX need some comment here */
1104 srb->pdata = (unsigned char *)buf_addr;
1105 if (blks > USB_MAX_XFER_BLK)
1106 smallblks = USB_MAX_XFER_BLK;
1108 smallblks = (unsigned short) blks;
1110 if (smallblks == USB_MAX_XFER_BLK)
1111 usb_show_progress();
1112 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1113 srb->pdata = (unsigned char *)buf_addr;
1114 if (usb_read_10(srb, ss, start, smallblks)) {
1115 USB_STOR_PRINTF("Read ERROR\n");
1116 usb_request_sense(srb, ss);
1124 buf_addr += srb->datalen;
1125 } while (blks != 0);
1126 ss->flags &= ~USB_READY;
1128 USB_STOR_PRINTF("usb_read: end startblk " LBAF
1129 ", blccnt %x buffer %lx\n",
1130 start, smallblks, buf_addr);
1132 usb_disable_asynch(0); /* asynch transfer allowed */
1133 if (blkcnt >= USB_MAX_XFER_BLK)
1138 unsigned long usb_stor_write(int device, unsigned long blknr,
1139 lbaint_t blkcnt, const void *buffer)
1141 lbaint_t start, blks;
1143 unsigned short smallblks;
1144 struct usb_device *dev;
1147 ccb *srb = &usb_ccb;
1154 USB_STOR_PRINTF("\nusb_write: dev %d \n", device);
1156 for (i = 0; i < USB_MAX_DEVICE; i++) {
1157 dev = usb_get_dev_index(i);
1160 if (dev->devnum == usb_dev_desc[device].target)
1163 ss = (struct us_data *)dev->privptr;
1165 usb_disable_asynch(1); /* asynch transfer not allowed */
1167 srb->lun = usb_dev_desc[device].lun;
1168 buf_addr = (unsigned long)buffer;
1172 USB_STOR_PRINTF("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF
1173 " buffer %lx\n", device, start, blks, buf_addr);
1176 /* If write fails retry for max retry count else
1177 * return with number of blocks written successfully.
1180 srb->pdata = (unsigned char *)buf_addr;
1181 if (blks > USB_MAX_XFER_BLK)
1182 smallblks = USB_MAX_XFER_BLK;
1184 smallblks = (unsigned short) blks;
1186 if (smallblks == USB_MAX_XFER_BLK)
1187 usb_show_progress();
1188 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1189 srb->pdata = (unsigned char *)buf_addr;
1190 if (usb_write_10(srb, ss, start, smallblks)) {
1191 USB_STOR_PRINTF("Write ERROR\n");
1192 usb_request_sense(srb, ss);
1200 buf_addr += srb->datalen;
1201 } while (blks != 0);
1202 ss->flags &= ~USB_READY;
1204 USB_STOR_PRINTF("usb_write: end startblk " LBAF
1205 ", blccnt %x buffer %lx\n",
1206 start, smallblks, buf_addr);
1208 usb_disable_asynch(0); /* asynch transfer allowed */
1209 if (blkcnt >= USB_MAX_XFER_BLK)
1215 /* Probe to see if a new device is actually a Storage device */
1216 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1219 struct usb_interface *iface;
1221 unsigned int flags = 0;
1226 /* let's examine the device now */
1227 iface = &dev->config.if_desc[ifnum];
1230 /* this is the place to patch some storage devices */
1231 USB_STOR_PRINTF("iVendor %X iProduct %X\n", dev->descriptor.idVendor,
1232 dev->descriptor.idProduct);
1234 if ((dev->descriptor.idVendor) == 0x066b &&
1235 (dev->descriptor.idProduct) == 0x0103) {
1236 USB_STOR_PRINTF("patched for E-USB\n");
1237 protocol = US_PR_CB;
1238 subclass = US_SC_UFI; /* an assumption */
1242 if (dev->descriptor.bDeviceClass != 0 ||
1243 iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1244 iface->desc.bInterfaceSubClass < US_SC_MIN ||
1245 iface->desc.bInterfaceSubClass > US_SC_MAX) {
1246 /* if it's not a mass storage, we go no further */
1250 memset(ss, 0, sizeof(struct us_data));
1252 /* At this point, we know we've got a live one */
1253 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1255 /* Initialize the us_data structure with some useful info */
1259 ss->attention_done = 0;
1261 /* If the device has subclass and protocol, then use that. Otherwise,
1262 * take data from the specific interface.
1265 ss->subclass = subclass;
1266 ss->protocol = protocol;
1268 ss->subclass = iface->desc.bInterfaceSubClass;
1269 ss->protocol = iface->desc.bInterfaceProtocol;
1272 /* set the handler pointers based on the protocol */
1273 USB_STOR_PRINTF("Transport: ");
1274 switch (ss->protocol) {
1276 USB_STOR_PRINTF("Control/Bulk\n");
1277 ss->transport = usb_stor_CB_transport;
1278 ss->transport_reset = usb_stor_CB_reset;
1282 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1283 ss->transport = usb_stor_CB_transport;
1284 ss->transport_reset = usb_stor_CB_reset;
1287 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1288 ss->transport = usb_stor_BBB_transport;
1289 ss->transport_reset = usb_stor_BBB_reset;
1292 printf("USB Storage Transport unknown / not yet implemented\n");
1298 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1299 * An optional interrupt is OK (necessary for CBI protocol).
1300 * We will ignore any others.
1302 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1303 /* is it an BULK endpoint? */
1304 if ((iface->ep_desc[i].bmAttributes &
1305 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1306 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1307 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1308 USB_ENDPOINT_NUMBER_MASK;
1311 iface->ep_desc[i].bEndpointAddress &
1312 USB_ENDPOINT_NUMBER_MASK;
1315 /* is it an interrupt endpoint? */
1316 if ((iface->ep_desc[i].bmAttributes &
1317 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1318 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1319 USB_ENDPOINT_NUMBER_MASK;
1320 ss->irqinterval = iface->ep_desc[i].bInterval;
1323 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1324 ss->ep_in, ss->ep_out, ss->ep_int);
1326 /* Do some basic sanity checks, and bail if we find a problem */
1327 if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
1328 !ss->ep_in || !ss->ep_out ||
1329 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1330 USB_STOR_PRINTF("Problems with device\n");
1333 /* set class specific stuff */
1334 /* We only handle certain protocols. Currently, these are
1336 * The SFF8070 accepts the requests used in u-boot
1338 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1339 ss->subclass != US_SC_8070) {
1340 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
1344 /* we had found an interrupt endpoint, prepare irq pipe
1345 * set up the IRQ pipe and handler
1347 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1348 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1349 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1350 dev->irq_handle = usb_stor_irq;
1352 dev->privptr = (void *)ss;
1356 int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1357 block_dev_desc_t *dev_desc)
1359 unsigned char perq, modi;
1360 ALLOC_CACHE_ALIGN_BUFFER(unsigned long, cap, 2);
1361 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, usb_stor_buf, 36);
1362 unsigned long *capacity, *blksz;
1363 ccb *pccb = &usb_ccb;
1365 pccb->pdata = usb_stor_buf;
1367 dev_desc->target = dev->devnum;
1368 pccb->lun = dev_desc->lun;
1369 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1371 if (usb_inquiry(pccb, ss))
1374 perq = usb_stor_buf[0];
1375 modi = usb_stor_buf[1];
1377 if ((perq & 0x1f) == 0x1f) {
1378 /* skip unknown devices */
1381 if ((modi&0x80) == 0x80) {
1382 /* drive is removable */
1383 dev_desc->removable = 1;
1385 memcpy(&dev_desc->vendor[0], (const void *) &usb_stor_buf[8], 8);
1386 memcpy(&dev_desc->product[0], (const void *) &usb_stor_buf[16], 16);
1387 memcpy(&dev_desc->revision[0], (const void *) &usb_stor_buf[32], 4);
1388 dev_desc->vendor[8] = 0;
1389 dev_desc->product[16] = 0;
1390 dev_desc->revision[4] = 0;
1391 #ifdef CONFIG_USB_BIN_FIXUP
1392 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1393 (uchar *)dev_desc->product);
1394 #endif /* CONFIG_USB_BIN_FIXUP */
1395 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1397 if (usb_test_unit_ready(pccb, ss)) {
1398 printf("Device NOT ready\n"
1399 " Request Sense returned %02X %02X %02X\n",
1400 pccb->sense_buf[2], pccb->sense_buf[12],
1401 pccb->sense_buf[13]);
1402 if (dev_desc->removable == 1) {
1403 dev_desc->type = perq;
1408 pccb->pdata = (unsigned char *)&cap[0];
1409 memset(pccb->pdata, 0, 8);
1410 if (usb_read_capacity(pccb, ss) != 0) {
1411 printf("READ_CAP ERROR\n");
1415 ss->flags &= ~USB_READY;
1416 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0],
1419 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1422 cap[0] = cpu_to_be32(cap[0]);
1423 cap[1] = cpu_to_be32(cap[1]);
1425 /* this assumes bigendian! */
1429 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",
1431 dev_desc->lba = *capacity;
1432 dev_desc->blksz = *blksz;
1433 dev_desc->log2blksz = LOG2(dev_desc->blksz);
1434 dev_desc->type = perq;
1435 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1436 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);
1438 init_part(dev_desc);
1440 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);