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>
60 #undef BBB_COMDAT_TRACE
61 #undef BBB_XPORT_TRACE
64 #define USB_STOR_PRINTF(fmt, args...) printf(fmt , ##args)
66 #define USB_STOR_PRINTF(fmt, args...)
70 /* direction table -- this indicates the direction of the data
71 * transfer for each command code -- a 1 indicates input
73 static const unsigned char us_direction[256/8] = {
74 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
75 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
79 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
81 static unsigned char usb_stor_buf[512];
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 static struct us_data usb_stor[USB_MAX_STOR_DEV];
161 #define USB_STOR_TRANSPORT_GOOD 0
162 #define USB_STOR_TRANSPORT_FAILED -1
163 #define USB_STOR_TRANSPORT_ERROR -2
165 int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
166 block_dev_desc_t *dev_desc);
167 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
169 unsigned long usb_stor_read(int device, unsigned long blknr,
170 unsigned long blkcnt, void *buffer);
171 unsigned long usb_stor_write(int device, unsigned long blknr,
172 unsigned long blkcnt, const void *buffer);
173 struct usb_device * usb_get_dev_index(int index);
174 void uhci_show_temp_int_td(void);
176 #ifdef CONFIG_PARTITIONS
177 block_dev_desc_t *usb_stor_get_dev(int index)
179 return (index < usb_max_devs) ? &usb_dev_desc[index] : NULL;
183 void usb_show_progress(void)
188 /*******************************************************************************
189 * show info on storage devices; 'usb start/init' must be invoked earlier
190 * as we only retrieve structures populated during devices initialization
192 int usb_stor_info(void)
196 if (usb_max_devs > 0) {
197 for (i = 0; i < usb_max_devs; i++) {
198 printf(" Device %d: ", i);
199 dev_print(&usb_dev_desc[i]);
204 printf("No storage devices, perhaps not 'usb start'ed..?\n");
208 static unsigned int usb_get_max_lun(struct us_data *us)
211 unsigned char result;
212 len = usb_control_msg(us->pusb_dev,
213 usb_rcvctrlpipe(us->pusb_dev, 0),
215 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
217 &result, sizeof(result),
218 USB_CNTL_TIMEOUT * 5);
219 USB_STOR_PRINTF("Get Max LUN -> len = %i, result = %i\n",
221 return (len > 0) ? result : 0;
224 /*******************************************************************************
225 * scan the usb and reports device info
226 * to the user if mode = 1
227 * returns current device or -1 if no
229 int usb_stor_scan(int mode)
232 struct usb_device *dev;
235 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
238 printf(" scanning bus for storage devices... ");
240 usb_disable_asynch(1); /* asynch transfer not allowed */
242 for (i = 0; i < USB_MAX_STOR_DEV; i++) {
243 memset(&usb_dev_desc[i], 0, sizeof(block_dev_desc_t));
244 usb_dev_desc[i].if_type = IF_TYPE_USB;
245 usb_dev_desc[i].dev = i;
246 usb_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
247 usb_dev_desc[i].target = 0xff;
248 usb_dev_desc[i].type = DEV_TYPE_UNKNOWN;
249 usb_dev_desc[i].block_read = usb_stor_read;
250 usb_dev_desc[i].block_write = usb_stor_write;
254 for (i = 0; i < USB_MAX_DEVICE; i++) {
255 dev = usb_get_dev_index(i); /* get device */
256 USB_STOR_PRINTF("i=%d\n", i);
258 break; /* no more devices available */
260 if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
261 /* OK, it's a storage device. Iterate over its LUNs
262 * and populate `usb_dev_desc'.
264 int lun, max_lun, start = usb_max_devs;
266 max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
268 lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
270 usb_dev_desc[usb_max_devs].lun = lun;
271 if (usb_stor_get_info(dev, &usb_stor[start],
272 &usb_dev_desc[usb_max_devs]) == 1) {
277 /* if storage device */
278 if (usb_max_devs == USB_MAX_STOR_DEV) {
279 printf("max USB Storage Device reached: %d stopping\n",
285 usb_disable_asynch(0); /* asynch transfer allowed */
286 printf("%d Storage Device(s) found\n", usb_max_devs);
287 if (usb_max_devs > 0)
292 static int usb_stor_irq(struct usb_device *dev)
295 us = (struct us_data *)dev->privptr;
303 #ifdef USB_STOR_DEBUG
305 static void usb_show_srb(ccb *pccb)
308 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
309 for (i = 0; i < 12; i++)
310 printf("%02X ", pccb->cmd[i]);
314 static void display_int_status(unsigned long tmp)
316 printf("Status: %s %s %s %s %s %s %s\n",
317 (tmp & USB_ST_ACTIVE) ? "Active" : "",
318 (tmp & USB_ST_STALLED) ? "Stalled" : "",
319 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
320 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
321 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
322 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
323 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
326 /***********************************************************************
327 * Data transfer routines
328 ***********************************************************************/
330 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
339 /* determine the maximum packet size for these transfers */
340 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
342 /* while we have data left to transfer */
345 /* calculate how long this will be -- maximum or a remainder */
346 this_xfer = length > max_size ? max_size : length;
349 /* setup the retry counter */
352 /* set up the transfer loop */
354 /* transfer the data */
355 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
356 (unsigned int)buf, this_xfer, 11 - maxtry);
357 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
359 USB_CNTL_TIMEOUT * 5);
360 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
361 result, partial, this_xfer);
362 if (us->pusb_dev->status != 0) {
363 /* if we stall, we need to clear it before
366 #ifdef USB_STOR_DEBUG
367 display_int_status(us->pusb_dev->status);
369 if (us->pusb_dev->status & USB_ST_STALLED) {
370 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
371 stat = us->pusb_dev->status;
372 usb_clear_halt(us->pusb_dev, pipe);
373 us->pusb_dev->status = stat;
374 if (this_xfer == partial) {
375 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n", us->pusb_dev->status);
381 if (us->pusb_dev->status & USB_ST_NAK_REC) {
382 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
385 USB_STOR_PRINTF("bulk transferred with error");
386 if (this_xfer == partial) {
387 USB_STOR_PRINTF(" %d, but data ok\n",
388 us->pusb_dev->status);
391 /* if our try counter reaches 0, bail out */
392 USB_STOR_PRINTF(" %d, data %d\n",
393 us->pusb_dev->status, partial);
397 /* update to show what data was transferred */
398 this_xfer -= partial;
400 /* continue until this transfer is done */
404 /* if we get here, we're done and successful */
408 static int usb_stor_BBB_reset(struct us_data *us)
414 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
416 * For Reset Recovery the host shall issue in the following order:
417 * a) a Bulk-Only Mass Storage Reset
418 * b) a Clear Feature HALT to the Bulk-In endpoint
419 * c) a Clear Feature HALT to the Bulk-Out endpoint
421 * This is done in 3 steps.
423 * If the reset doesn't succeed, the device should be port reset.
425 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
427 USB_STOR_PRINTF("BBB_reset\n");
428 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
430 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
431 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT * 5);
433 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
434 USB_STOR_PRINTF("RESET:stall\n");
438 /* long wait for reset */
440 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n", result,
441 us->pusb_dev->status);
442 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
443 result = usb_clear_halt(us->pusb_dev, pipe);
444 /* long wait for reset */
446 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",
447 result, us->pusb_dev->status);
448 /* long wait for reset */
449 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
450 result = usb_clear_halt(us->pusb_dev, pipe);
452 USB_STOR_PRINTF("BBB_reset result %d: status %X"
453 " clearing OUT endpoint\n", result,
454 us->pusb_dev->status);
455 USB_STOR_PRINTF("BBB_reset done\n");
459 /* FIXME: this reset function doesn't really reset the port, and it
460 * should. Actually it should probably do what it's doing here, and
461 * reset the port physically
463 static int usb_stor_CB_reset(struct us_data *us)
465 unsigned char cmd[12];
468 USB_STOR_PRINTF("CB_reset\n");
469 memset(cmd, 0xff, sizeof(cmd));
470 cmd[0] = SCSI_SEND_DIAG;
472 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
474 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
475 0, us->ifnum, cmd, sizeof(cmd),
476 USB_CNTL_TIMEOUT * 5);
478 /* long wait for reset */
480 USB_STOR_PRINTF("CB_reset result %d: status %X"
481 " clearing endpoint halt\n", result,
482 us->pusb_dev->status);
483 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
484 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
486 USB_STOR_PRINTF("CB_reset done\n");
491 * Set up the command for a BBB device. Note that the actual SCSI
492 * command is copied into cbw.CBWCDB.
494 int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
502 dir_in = US_DIRECTION(srb->cmd[0]);
504 #ifdef BBB_COMDAT_TRACE
505 printf("dir %d lun %d cmdlen %d cmd %p datalen %d pdata %p\n",
506 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
509 for (result = 0; result < srb->cmdlen; result++)
510 printf("cmd[%d] %#x ", result, srb->cmd[result]);
515 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
516 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
520 /* always OUT to the ep */
521 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
523 cbw.dCBWSignature = cpu_to_le32(CBWSIGNATURE);
524 cbw.dCBWTag = cpu_to_le32(CBWTag++);
525 cbw.dCBWDataTransferLength = cpu_to_le32(srb->datalen);
526 cbw.bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
527 cbw.bCBWLUN = srb->lun;
528 cbw.bCDBLength = srb->cmdlen;
529 /* copy the command data into the CBW command data buffer */
531 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
532 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE,
533 &actlen, USB_CNTL_TIMEOUT * 5);
535 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
539 /* FIXME: we also need a CBI_command which sets up the completion
540 * interrupt, and waits for it
542 int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
547 unsigned long status;
550 dir_in = US_DIRECTION(srb->cmd[0]);
553 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
555 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
558 USB_STOR_PRINTF("CBI gets a command: Try %d\n", 5 - retry);
559 #ifdef USB_STOR_DEBUG
562 /* let's send the command via the control pipe */
563 result = usb_control_msg(us->pusb_dev,
564 usb_sndctrlpipe(us->pusb_dev , 0),
566 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
568 srb->cmd, srb->cmdlen,
569 USB_CNTL_TIMEOUT * 5);
570 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
571 " status %X\n", result, us->pusb_dev->status);
572 /* check the return code for the command */
574 if (us->pusb_dev->status & USB_ST_STALLED) {
575 status = us->pusb_dev->status;
576 USB_STOR_PRINTF(" stall during command found,"
578 usb_clear_halt(us->pusb_dev,
579 usb_sndctrlpipe(us->pusb_dev, 0));
580 us->pusb_dev->status = status;
582 USB_STOR_PRINTF(" error during command %02X"
583 " Stat = %X\n", srb->cmd[0],
584 us->pusb_dev->status);
587 /* transfer the data payload for this command, if one exists*/
589 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
590 " direction is %s to go 0x%lx\n", result,
591 dir_in ? "IN" : "OUT", srb->datalen);
593 result = us_one_transfer(us, pipe, (char *)srb->pdata,
595 USB_STOR_PRINTF("CBI attempted to transfer data,"
596 " result is %d status %lX, len %d\n",
597 result, us->pusb_dev->status,
598 us->pusb_dev->act_len);
599 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
601 } /* if (srb->datalen) */
611 int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
616 submit_int_msg(us->pusb_dev, us->irqpipe,
617 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
620 if ((volatile int *) us->ip_wanted == 0)
625 printf(" Did not get interrupt on CBI\n");
627 return USB_STOR_TRANSPORT_ERROR;
630 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
631 us->ip_data, us->pusb_dev->irq_act_len,
632 us->pusb_dev->irq_status);
633 /* UFI gives us ASC and ASCQ, like a request sense */
634 if (us->subclass == US_SC_UFI) {
635 if (srb->cmd[0] == SCSI_REQ_SENSE ||
636 srb->cmd[0] == SCSI_INQUIRY)
637 return USB_STOR_TRANSPORT_GOOD; /* Good */
638 else if (us->ip_data)
639 return USB_STOR_TRANSPORT_FAILED;
641 return USB_STOR_TRANSPORT_GOOD;
643 /* otherwise, we interpret the data normally */
644 switch (us->ip_data) {
646 return USB_STOR_TRANSPORT_GOOD;
648 return USB_STOR_TRANSPORT_FAILED;
650 return USB_STOR_TRANSPORT_ERROR;
652 return USB_STOR_TRANSPORT_ERROR;
655 #define USB_TRANSPORT_UNKNOWN_RETRY 5
656 #define USB_TRANSPORT_NOT_READY_RETRY 10
658 /* clear a stall on an endpoint - special for BBB devices */
659 int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
663 /* ENDPOINT_HALT = 0, so set value to 0 */
664 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
665 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
666 0, endpt, 0, 0, USB_CNTL_TIMEOUT * 5);
670 int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
674 int actlen, data_actlen;
675 unsigned int pipe, pipein, pipeout;
677 #ifdef BBB_XPORT_TRACE
682 dir_in = US_DIRECTION(srb->cmd[0]);
685 USB_STOR_PRINTF("COMMAND phase\n");
686 result = usb_stor_BBB_comdat(srb, us);
688 USB_STOR_PRINTF("failed to send CBW status %ld\n",
689 us->pusb_dev->status);
690 usb_stor_BBB_reset(us);
691 return USB_STOR_TRANSPORT_FAILED;
694 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
695 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
696 /* DATA phase + error handling */
698 /* no data, go immediately to the STATUS phase */
699 if (srb->datalen == 0)
701 USB_STOR_PRINTF("DATA phase\n");
706 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
707 &data_actlen, USB_CNTL_TIMEOUT * 5);
708 /* special handling of STALL in DATA phase */
709 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
710 USB_STOR_PRINTF("DATA:stall\n");
711 /* clear the STALL on the endpoint */
712 result = usb_stor_BBB_clear_endpt_stall(us,
713 dir_in ? us->ep_in : us->ep_out);
715 /* continue on to STATUS phase */
719 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
720 us->pusb_dev->status);
721 usb_stor_BBB_reset(us);
722 return USB_STOR_TRANSPORT_FAILED;
724 #ifdef BBB_XPORT_TRACE
725 for (index = 0; index < data_actlen; index++)
726 printf("pdata[%d] %#x ", index, srb->pdata[index]);
729 /* STATUS phase + error handling */
733 USB_STOR_PRINTF("STATUS phase\n");
734 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
735 &actlen, USB_CNTL_TIMEOUT*5);
737 /* special handling of STALL in STATUS phase */
738 if ((result < 0) && (retry < 1) &&
739 (us->pusb_dev->status & USB_ST_STALLED)) {
740 USB_STOR_PRINTF("STATUS:stall\n");
741 /* clear the STALL on the endpoint */
742 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
743 if (result >= 0 && (retry++ < 1))
748 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
749 us->pusb_dev->status);
750 usb_stor_BBB_reset(us);
751 return USB_STOR_TRANSPORT_FAILED;
753 #ifdef BBB_XPORT_TRACE
754 ptr = (unsigned char *)&csw;
755 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
756 printf("ptr[%d] %#x ", index, ptr[index]);
759 /* misuse pipe to get the residue */
760 pipe = le32_to_cpu(csw.dCSWDataResidue);
761 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
762 pipe = srb->datalen - data_actlen;
763 if (CSWSIGNATURE != le32_to_cpu(csw.dCSWSignature)) {
764 USB_STOR_PRINTF("!CSWSIGNATURE\n");
765 usb_stor_BBB_reset(us);
766 return USB_STOR_TRANSPORT_FAILED;
767 } else if ((CBWTag - 1) != le32_to_cpu(csw.dCSWTag)) {
768 USB_STOR_PRINTF("!Tag\n");
769 usb_stor_BBB_reset(us);
770 return USB_STOR_TRANSPORT_FAILED;
771 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
772 USB_STOR_PRINTF(">PHASE\n");
773 usb_stor_BBB_reset(us);
774 return USB_STOR_TRANSPORT_FAILED;
775 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
776 USB_STOR_PRINTF("=PHASE\n");
777 usb_stor_BBB_reset(us);
778 return USB_STOR_TRANSPORT_FAILED;
779 } else if (data_actlen > srb->datalen) {
780 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
781 data_actlen, srb->datalen);
782 return USB_STOR_TRANSPORT_FAILED;
783 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
784 USB_STOR_PRINTF("FAILED\n");
785 return USB_STOR_TRANSPORT_FAILED;
791 int usb_stor_CB_transport(ccb *srb, struct us_data *us)
799 status = USB_STOR_TRANSPORT_GOOD;
802 /* issue the command */
804 result = usb_stor_CB_comdat(srb, us);
805 USB_STOR_PRINTF("command / Data returned %d, status %X\n",
806 result, us->pusb_dev->status);
807 /* if this is an CBI Protocol, get IRQ */
808 if (us->protocol == US_PR_CBI) {
809 status = usb_stor_CBI_get_status(srb, us);
810 /* if the status is error, report it */
811 if (status == USB_STOR_TRANSPORT_ERROR) {
812 USB_STOR_PRINTF(" USB CBI Command Error\n");
815 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
816 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
818 /* if the status is good, report it */
819 if (status == USB_STOR_TRANSPORT_GOOD) {
820 USB_STOR_PRINTF(" USB CBI Command Good\n");
825 /* do we have to issue an auto request? */
826 /* HERE we have to check the result */
827 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
828 USB_STOR_PRINTF("ERROR %X\n", us->pusb_dev->status);
829 us->transport_reset(us);
830 return USB_STOR_TRANSPORT_ERROR;
832 if ((us->protocol == US_PR_CBI) &&
833 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
834 (srb->cmd[0] == SCSI_INQUIRY))) {
835 /* do not issue an autorequest after request sense */
836 USB_STOR_PRINTF("No auto request and good\n");
837 return USB_STOR_TRANSPORT_GOOD;
839 /* issue an request_sense */
840 memset(&psrb->cmd[0], 0, 12);
841 psrb->cmd[0] = SCSI_REQ_SENSE;
842 psrb->cmd[1] = srb->lun << 5;
845 psrb->pdata = &srb->sense_buf[0];
847 /* issue the command */
848 result = usb_stor_CB_comdat(psrb, us);
849 USB_STOR_PRINTF("auto request returned %d\n", result);
850 /* if this is an CBI Protocol, get IRQ */
851 if (us->protocol == US_PR_CBI)
852 status = usb_stor_CBI_get_status(psrb, us);
854 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
855 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",
856 us->pusb_dev->status);
857 return USB_STOR_TRANSPORT_ERROR;
859 USB_STOR_PRINTF("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
860 srb->sense_buf[0], srb->sense_buf[2],
861 srb->sense_buf[12], srb->sense_buf[13]);
862 /* Check the auto request result */
863 if ((srb->sense_buf[2] == 0) &&
864 (srb->sense_buf[12] == 0) &&
865 (srb->sense_buf[13] == 0)) {
867 return USB_STOR_TRANSPORT_GOOD;
870 /* Check the auto request result */
871 switch (srb->sense_buf[2]) {
873 /* Recovered Error */
874 return USB_STOR_TRANSPORT_GOOD;
878 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
879 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
880 " 0x%02X (NOT READY)\n", srb->cmd[0],
881 srb->sense_buf[0], srb->sense_buf[2],
882 srb->sense_buf[12], srb->sense_buf[13]);
883 return USB_STOR_TRANSPORT_FAILED;
890 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
891 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
892 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
893 srb->sense_buf[2], srb->sense_buf[12],
895 return USB_STOR_TRANSPORT_FAILED;
900 return USB_STOR_TRANSPORT_FAILED;
904 static int usb_inquiry(ccb *srb, struct us_data *ss)
909 memset(&srb->cmd[0], 0, 12);
910 srb->cmd[0] = SCSI_INQUIRY;
911 srb->cmd[1] = srb->lun << 5;
915 i = ss->transport(srb, ss);
916 USB_STOR_PRINTF("inquiry returns %d\n", i);
922 printf("error in inquiry\n");
928 static int usb_request_sense(ccb *srb, struct us_data *ss)
932 ptr = (char *)srb->pdata;
933 memset(&srb->cmd[0], 0, 12);
934 srb->cmd[0] = SCSI_REQ_SENSE;
935 srb->cmd[1] = srb->lun << 5;
938 srb->pdata = &srb->sense_buf[0];
940 ss->transport(srb, ss);
941 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",
942 srb->sense_buf[2], srb->sense_buf[12],
944 srb->pdata = (uchar *)ptr;
948 static int usb_test_unit_ready(ccb *srb, struct us_data *ss)
953 memset(&srb->cmd[0], 0, 12);
954 srb->cmd[0] = SCSI_TST_U_RDY;
955 srb->cmd[1] = srb->lun << 5;
958 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
960 usb_request_sense(srb, ss);
967 static int usb_read_capacity(ccb *srb, struct us_data *ss)
973 memset(&srb->cmd[0], 0, 12);
974 srb->cmd[0] = SCSI_RD_CAPAC;
975 srb->cmd[1] = srb->lun << 5;
978 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
985 static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start,
986 unsigned short blocks)
988 memset(&srb->cmd[0], 0, 12);
989 srb->cmd[0] = SCSI_READ10;
990 srb->cmd[1] = srb->lun << 5;
991 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
992 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
993 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
994 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
995 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
996 srb->cmd[8] = (unsigned char) blocks & 0xff;
998 USB_STOR_PRINTF("read10: start %lx blocks %x\n", start, blocks);
999 return ss->transport(srb, ss);
1002 static int usb_write_10(ccb *srb, struct us_data *ss, unsigned long start,
1003 unsigned short blocks)
1005 memset(&srb->cmd[0], 0, 12);
1006 srb->cmd[0] = SCSI_WRITE10;
1007 srb->cmd[1] = srb->lun << 5;
1008 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1009 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1010 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1011 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1012 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1013 srb->cmd[8] = (unsigned char) blocks & 0xff;
1015 USB_STOR_PRINTF("write10: start %lx blocks %x\n", start, blocks);
1016 return ss->transport(srb, ss);
1020 #ifdef CONFIG_USB_BIN_FIXUP
1022 * Some USB storage devices queried for SCSI identification data respond with
1023 * binary strings, which if output to the console freeze the terminal. The
1024 * workaround is to modify the vendor and product strings read from such
1025 * device with proper values (as reported by 'usb info').
1027 * Vendor and product length limits are taken from the definition of
1028 * block_dev_desc_t in include/part.h.
1030 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
1031 unsigned char vendor[],
1032 unsigned char product[]) {
1033 const unsigned char max_vendor_len = 40;
1034 const unsigned char max_product_len = 20;
1035 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
1036 strncpy((char *)vendor, "SMSC", max_vendor_len);
1037 strncpy((char *)product, "Flash Media Cntrller",
1041 #endif /* CONFIG_USB_BIN_FIXUP */
1043 #define USB_MAX_READ_BLK 20
1045 unsigned long usb_stor_read(int device, unsigned long blknr,
1046 unsigned long blkcnt, void *buffer)
1048 unsigned long start, blks, buf_addr;
1049 unsigned short smallblks;
1050 struct usb_device *dev;
1052 ccb *srb = &usb_ccb;
1059 USB_STOR_PRINTF("\nusb_read: dev %d \n", device);
1061 for (i = 0; i < USB_MAX_DEVICE; i++) {
1062 dev = usb_get_dev_index(i);
1065 if (dev->devnum == usb_dev_desc[device].target)
1069 usb_disable_asynch(1); /* asynch transfer not allowed */
1070 srb->lun = usb_dev_desc[device].lun;
1071 buf_addr = (unsigned long)buffer;
1074 if (usb_test_unit_ready(srb, (struct us_data *)dev->privptr)) {
1075 printf("Device NOT ready\n Request Sense returned %02X %02X"
1076 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1077 srb->sense_buf[13]);
1081 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx"
1082 " buffer %lx\n", device, start, blks, buf_addr);
1085 /* XXX need some comment here */
1087 srb->pdata = (unsigned char *)buf_addr;
1088 if (blks > USB_MAX_READ_BLK)
1089 smallblks = USB_MAX_READ_BLK;
1091 smallblks = (unsigned short) blks;
1093 if (smallblks == USB_MAX_READ_BLK)
1094 usb_show_progress();
1095 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1096 srb->pdata = (unsigned char *)buf_addr;
1097 if (usb_read_10(srb, (struct us_data *)dev->privptr, start,
1099 USB_STOR_PRINTF("Read ERROR\n");
1100 usb_request_sense(srb, (struct us_data *)dev->privptr);
1108 buf_addr += srb->datalen;
1109 } while (blks != 0);
1111 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",
1112 start, smallblks, buf_addr);
1114 usb_disable_asynch(0); /* asynch transfer allowed */
1115 if (blkcnt >= USB_MAX_READ_BLK)
1120 #define USB_MAX_WRITE_BLK 20
1122 unsigned long usb_stor_write(int device, unsigned long blknr,
1123 unsigned long blkcnt, const void *buffer)
1125 unsigned long start, blks, buf_addr;
1126 unsigned short smallblks;
1127 struct usb_device *dev;
1129 ccb *srb = &usb_ccb;
1136 USB_STOR_PRINTF("\nusb_write: dev %d \n", device);
1138 for (i = 0; i < USB_MAX_DEVICE; i++) {
1139 dev = usb_get_dev_index(i);
1142 if (dev->devnum == usb_dev_desc[device].target)
1146 usb_disable_asynch(1); /* asynch transfer not allowed */
1148 srb->lun = usb_dev_desc[device].lun;
1149 buf_addr = (unsigned long)buffer;
1152 if (usb_test_unit_ready(srb, (struct us_data *)dev->privptr)) {
1153 printf("Device NOT ready\n Request Sense returned %02X %02X"
1154 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1155 srb->sense_buf[13]);
1159 USB_STOR_PRINTF("\nusb_write: dev %d startblk %lx, blccnt %lx"
1160 " buffer %lx\n", device, start, blks, buf_addr);
1163 /* If write fails retry for max retry count else
1164 * return with number of blocks written successfully.
1167 srb->pdata = (unsigned char *)buf_addr;
1168 if (blks > USB_MAX_WRITE_BLK)
1169 smallblks = USB_MAX_WRITE_BLK;
1171 smallblks = (unsigned short) blks;
1173 if (smallblks == USB_MAX_WRITE_BLK)
1174 usb_show_progress();
1175 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1176 srb->pdata = (unsigned char *)buf_addr;
1177 if (usb_write_10(srb, (struct us_data *)dev->privptr, start,
1179 USB_STOR_PRINTF("Write ERROR\n");
1180 usb_request_sense(srb, (struct us_data *)dev->privptr);
1188 buf_addr += srb->datalen;
1189 } while (blks != 0);
1191 USB_STOR_PRINTF("usb_write: end startblk %lx, blccnt %x buffer %lx\n",
1192 start, smallblks, buf_addr);
1194 usb_disable_asynch(0); /* asynch transfer allowed */
1195 if (blkcnt >= USB_MAX_WRITE_BLK)
1201 /* Probe to see if a new device is actually a Storage device */
1202 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1205 struct usb_interface *iface;
1207 unsigned int flags = 0;
1212 /* let's examine the device now */
1213 iface = &dev->config.if_desc[ifnum];
1216 /* this is the place to patch some storage devices */
1217 USB_STOR_PRINTF("iVendor %X iProduct %X\n", dev->descriptor.idVendor,
1218 dev->descriptor.idProduct);
1220 if ((dev->descriptor.idVendor) == 0x066b &&
1221 (dev->descriptor.idProduct) == 0x0103) {
1222 USB_STOR_PRINTF("patched for E-USB\n");
1223 protocol = US_PR_CB;
1224 subclass = US_SC_UFI; /* an assumption */
1228 if (dev->descriptor.bDeviceClass != 0 ||
1229 iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1230 iface->desc.bInterfaceSubClass < US_SC_MIN ||
1231 iface->desc.bInterfaceSubClass > US_SC_MAX) {
1232 /* if it's not a mass storage, we go no further */
1236 memset(ss, 0, sizeof(struct us_data));
1238 /* At this point, we know we've got a live one */
1239 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1241 /* Initialize the us_data structure with some useful info */
1245 ss->attention_done = 0;
1247 /* If the device has subclass and protocol, then use that. Otherwise,
1248 * take data from the specific interface.
1251 ss->subclass = subclass;
1252 ss->protocol = protocol;
1254 ss->subclass = iface->desc.bInterfaceSubClass;
1255 ss->protocol = iface->desc.bInterfaceProtocol;
1258 /* set the handler pointers based on the protocol */
1259 USB_STOR_PRINTF("Transport: ");
1260 switch (ss->protocol) {
1262 USB_STOR_PRINTF("Control/Bulk\n");
1263 ss->transport = usb_stor_CB_transport;
1264 ss->transport_reset = usb_stor_CB_reset;
1268 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1269 ss->transport = usb_stor_CB_transport;
1270 ss->transport_reset = usb_stor_CB_reset;
1273 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1274 ss->transport = usb_stor_BBB_transport;
1275 ss->transport_reset = usb_stor_BBB_reset;
1278 printf("USB Storage Transport unknown / not yet implemented\n");
1284 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1285 * An optional interrupt is OK (necessary for CBI protocol).
1286 * We will ignore any others.
1288 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1289 /* is it an BULK endpoint? */
1290 if ((iface->ep_desc[i].bmAttributes &
1291 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1292 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1293 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1294 USB_ENDPOINT_NUMBER_MASK;
1297 iface->ep_desc[i].bEndpointAddress &
1298 USB_ENDPOINT_NUMBER_MASK;
1301 /* is it an interrupt endpoint? */
1302 if ((iface->ep_desc[i].bmAttributes &
1303 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1304 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1305 USB_ENDPOINT_NUMBER_MASK;
1306 ss->irqinterval = iface->ep_desc[i].bInterval;
1309 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1310 ss->ep_in, ss->ep_out, ss->ep_int);
1312 /* Do some basic sanity checks, and bail if we find a problem */
1313 if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
1314 !ss->ep_in || !ss->ep_out ||
1315 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1316 USB_STOR_PRINTF("Problems with device\n");
1319 /* set class specific stuff */
1320 /* We only handle certain protocols. Currently, these are
1322 * The SFF8070 accepts the requests used in u-boot
1324 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1325 ss->subclass != US_SC_8070) {
1326 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
1330 /* we had found an interrupt endpoint, prepare irq pipe
1331 * set up the IRQ pipe and handler
1333 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1334 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1335 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1336 dev->irq_handle = usb_stor_irq;
1338 dev->privptr = (void *)ss;
1342 int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1343 block_dev_desc_t *dev_desc)
1345 unsigned char perq, modi;
1346 unsigned long cap[2];
1347 unsigned long *capacity, *blksz;
1348 ccb *pccb = &usb_ccb;
1350 pccb->pdata = usb_stor_buf;
1352 dev_desc->target = dev->devnum;
1353 pccb->lun = dev_desc->lun;
1354 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1356 if (usb_inquiry(pccb, ss))
1359 perq = usb_stor_buf[0];
1360 modi = usb_stor_buf[1];
1362 if ((perq & 0x1f) == 0x1f) {
1363 /* skip unknown devices */
1366 if ((modi&0x80) == 0x80) {
1367 /* drive is removable */
1368 dev_desc->removable = 1;
1370 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1371 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1372 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
1373 dev_desc->vendor[8] = 0;
1374 dev_desc->product[16] = 0;
1375 dev_desc->revision[4] = 0;
1376 #ifdef CONFIG_USB_BIN_FIXUP
1377 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1378 (uchar *)dev_desc->product);
1379 #endif /* CONFIG_USB_BIN_FIXUP */
1380 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1382 if (usb_test_unit_ready(pccb, ss)) {
1383 printf("Device NOT ready\n"
1384 " Request Sense returned %02X %02X %02X\n",
1385 pccb->sense_buf[2], pccb->sense_buf[12],
1386 pccb->sense_buf[13]);
1387 if (dev_desc->removable == 1) {
1388 dev_desc->type = perq;
1393 pccb->pdata = (unsigned char *)&cap[0];
1394 memset(pccb->pdata, 0, 8);
1395 if (usb_read_capacity(pccb, ss) != 0) {
1396 printf("READ_CAP ERROR\n");
1400 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0],
1403 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1406 cap[0] = cpu_to_be32(cap[0]);
1407 cap[1] = cpu_to_be32(cap[1]);
1409 /* this assumes bigendian! */
1413 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",
1415 dev_desc->lba = *capacity;
1416 dev_desc->blksz = *blksz;
1417 dev_desc->type = perq;
1418 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1419 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);
1421 init_part(dev_desc);
1423 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);