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 <gj@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.
48 * Nota bene: if you are using the BBB support with a little-endian
49 * CPU then you MUST define LITTLEENDIAN in the configuration file!
55 #include <asm/byteorder.h>
56 #include <asm/processor.h>
62 #undef BBB_COMDAT_TRACE
63 #undef BBB_XPORT_TRACE
66 #define USB_STOR_PRINTF(fmt, args...) printf(fmt , ##args)
68 #define USB_STOR_PRINTF(fmt, args...)
72 /* direction table -- this indicates the direction of the data
73 * transfer for each command code -- a 1 indicates input
75 unsigned char us_direction[256/8] = {
76 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
77 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
81 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
83 static unsigned char usb_stor_buf[512];
95 #define US_BBB_RESET 0xff
96 #define US_BBB_GET_MAX_LUN 0xfe
98 /* Command Block Wrapper */
101 # define CBWSIGNATURE 0x43425355
103 __u32 dCBWDataTransferLength;
105 # define CBWFLAGS_OUT 0x00
106 # define CBWFLAGS_IN 0x80
109 # define CBWCDBLENGTH 16
110 __u8 CBWCDB[CBWCDBLENGTH];
112 #define UMASS_BBB_CBW_SIZE 31
115 /* Command Status Wrapper */
118 # define CSWSIGNATURE 0x53425355
120 __u32 dCSWDataResidue;
122 # define CSWSTATUS_GOOD 0x0
123 # define CSWSTATUS_FAILED 0x1
124 # define CSWSTATUS_PHASE 0x2
126 #define UMASS_BBB_CSW_SIZE 13
128 #define USB_MAX_STOR_DEV 5
129 static int usb_max_devs; /* number of highest available usb device */
131 static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
134 typedef int (*trans_cmnd)(ccb *cb, struct us_data *data);
135 typedef int (*trans_reset)(struct us_data *data);
138 struct usb_device *pusb_dev; /* this usb_device */
140 unsigned int flags; /* from filter initially */
141 unsigned char ifnum; /* interface number */
142 unsigned char ep_in; /* in endpoint */
143 unsigned char ep_out; /* out ....... */
144 unsigned char ep_int; /* interrupt . */
145 unsigned char subclass; /* as in overview */
146 unsigned char protocol; /* .............. */
147 unsigned char attention_done; /* force attn on first cmd */
148 unsigned short ip_data; /* interrupt data */
149 int action; /* what to do */
150 int ip_wanted; /* needed */
151 int *irq_handle; /* for USB int requests */
152 unsigned int irqpipe; /* pipe for release_irq */
153 unsigned char irqmaxp; /* max packed for irq Pipe */
154 unsigned char irqinterval; /* Intervall for IRQ Pipe */
155 ccb *srb; /* current srb */
156 trans_reset transport_reset; /* reset routine */
157 trans_cmnd transport; /* transport routine */
160 static struct us_data usb_stor[USB_MAX_STOR_DEV];
163 #define USB_STOR_TRANSPORT_GOOD 0
164 #define USB_STOR_TRANSPORT_FAILED -1
165 #define USB_STOR_TRANSPORT_ERROR -2
167 int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
168 block_dev_desc_t *dev_desc);
169 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
171 unsigned long usb_stor_read(int device, unsigned long blknr,
172 unsigned long blkcnt, void *buffer);
173 struct usb_device * usb_get_dev_index(int index);
174 void uhci_show_temp_int_td(void);
176 block_dev_desc_t *usb_stor_get_dev(int index)
178 return (index < USB_MAX_STOR_DEV) ? &usb_dev_desc[index] : NULL;
182 void usb_show_progress(void)
187 /*******************************************************************************
188 * show info on storage devices; 'usb start/init' must be invoked earlier
189 * as we only retrieve structures populated during devices initialization
191 int usb_stor_info(void)
195 if (usb_max_devs > 0) {
196 for (i = 0; i < usb_max_devs; i++) {
197 printf(" Device %d: ", i);
198 dev_print(&usb_dev_desc[i]);
203 printf("No storage devices, perhaps not 'usb start'ed..?\n");
207 /*******************************************************************************
208 * scan the usb and reports device info
209 * to the user if mode = 1
210 * returns current device or -1 if no
212 int usb_stor_scan(int mode)
215 struct usb_device *dev;
218 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
221 printf(" scanning bus for storage devices... ");
223 usb_disable_asynch(1); /* asynch transfer not allowed */
225 for (i = 0; i < USB_MAX_STOR_DEV; i++) {
226 memset(&usb_dev_desc[i], 0, sizeof(block_dev_desc_t));
227 usb_dev_desc[i].target = 0xff;
228 usb_dev_desc[i].if_type = IF_TYPE_USB;
229 usb_dev_desc[i].dev = i;
230 usb_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
231 usb_dev_desc[i].block_read = usb_stor_read;
235 for (i = 0; i < USB_MAX_DEVICE; i++) {
236 dev = usb_get_dev_index(i); /* get device */
237 USB_STOR_PRINTF("i=%d\n", i);
239 break; /* no more devices avaiable */
241 if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
242 /* ok, it is a storage devices
243 * get info and fill it in
245 if (usb_stor_get_info(dev, &usb_stor[usb_max_devs],
246 &usb_dev_desc[usb_max_devs]))
249 /* if storage device */
250 if (usb_max_devs == USB_MAX_STOR_DEV) {
251 printf("max USB Storage Device reached: %d stopping\n",
257 usb_disable_asynch(0); /* asynch transfer allowed */
258 printf("%d Storage Device(s) found\n", usb_max_devs);
259 if (usb_max_devs > 0)
264 static int usb_stor_irq(struct usb_device *dev)
267 us = (struct us_data *)dev->privptr;
275 #ifdef USB_STOR_DEBUG
277 static void usb_show_srb(ccb *pccb)
280 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
281 for (i = 0; i < 12; i++)
282 printf("%02X ", pccb->cmd[i]);
286 static void display_int_status(unsigned long tmp)
288 printf("Status: %s %s %s %s %s %s %s\n",
289 (tmp & USB_ST_ACTIVE) ? "Active" : "",
290 (tmp & USB_ST_STALLED) ? "Stalled" : "",
291 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
292 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
293 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
294 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
295 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
298 /***********************************************************************
299 * Data transfer routines
300 ***********************************************************************/
302 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
311 /* determine the maximum packet size for these transfers */
312 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
314 /* while we have data left to transfer */
317 /* calculate how long this will be -- maximum or a remainder */
318 this_xfer = length > max_size ? max_size : length;
321 /* setup the retry counter */
324 /* set up the transfer loop */
326 /* transfer the data */
327 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
328 (unsigned int)buf, this_xfer, 11 - maxtry);
329 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
331 USB_CNTL_TIMEOUT * 5);
332 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
333 result, partial, this_xfer);
334 if (us->pusb_dev->status != 0) {
335 /* if we stall, we need to clear it before
338 #ifdef USB_STOR_DEBUG
339 display_int_status(us->pusb_dev->status);
341 if (us->pusb_dev->status & USB_ST_STALLED) {
342 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
343 stat = us->pusb_dev->status;
344 usb_clear_halt(us->pusb_dev, pipe);
345 us->pusb_dev->status = stat;
346 if (this_xfer == partial) {
347 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n", us->pusb_dev->status);
353 if (us->pusb_dev->status & USB_ST_NAK_REC) {
354 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
357 USB_STOR_PRINTF("bulk transferred with error");
358 if (this_xfer == partial) {
359 USB_STOR_PRINTF(" %d, but data ok\n",
360 us->pusb_dev->status);
363 /* if our try counter reaches 0, bail out */
364 USB_STOR_PRINTF(" %d, data %d\n",
365 us->pusb_dev->status, partial);
369 /* update to show what data was transferred */
370 this_xfer -= partial;
372 /* continue until this transfer is done */
376 /* if we get here, we're done and successful */
380 static int usb_stor_BBB_reset(struct us_data *us)
386 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
388 * For Reset Recovery the host shall issue in the following order:
389 * a) a Bulk-Only Mass Storage Reset
390 * b) a Clear Feature HALT to the Bulk-In endpoint
391 * c) a Clear Feature HALT to the Bulk-Out endpoint
393 * This is done in 3 steps.
395 * If the reset doesn't succeed, the device should be port reset.
397 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
399 USB_STOR_PRINTF("BBB_reset\n");
400 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
402 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
403 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT * 5);
405 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
406 USB_STOR_PRINTF("RESET:stall\n");
410 /* long wait for reset */
412 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n", result,
413 us->pusb_dev->status);
414 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
415 result = usb_clear_halt(us->pusb_dev, pipe);
416 /* long wait for reset */
418 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",
419 result, us->pusb_dev->status);
420 /* long wait for reset */
421 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
422 result = usb_clear_halt(us->pusb_dev, pipe);
424 USB_STOR_PRINTF("BBB_reset result %d: status %X"
425 " clearing OUT endpoint\n", result,
426 us->pusb_dev->status);
427 USB_STOR_PRINTF("BBB_reset done\n");
431 /* FIXME: this reset function doesn't really reset the port, and it
432 * should. Actually it should probably do what it's doing here, and
433 * reset the port physically
435 static int usb_stor_CB_reset(struct us_data *us)
437 unsigned char cmd[12];
440 USB_STOR_PRINTF("CB_reset\n");
441 memset(cmd, 0xff, sizeof(cmd));
442 cmd[0] = SCSI_SEND_DIAG;
444 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
446 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
447 0, us->ifnum, cmd, sizeof(cmd),
448 USB_CNTL_TIMEOUT * 5);
450 /* long wait for reset */
452 USB_STOR_PRINTF("CB_reset result %d: status %X"
453 " clearing endpoint halt\n", result,
454 us->pusb_dev->status);
455 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
456 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
458 USB_STOR_PRINTF("CB_reset done\n");
463 * Set up the command for a BBB device. Note that the actual SCSI
464 * command is copied into cbw.CBWCDB.
466 int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
474 dir_in = US_DIRECTION(srb->cmd[0]);
476 #ifdef BBB_COMDAT_TRACE
477 printf("dir %d lun %d cmdlen %d cmd %p datalen %d pdata %p\n",
478 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
481 for (result = 0; result < srb->cmdlen; result++)
482 printf("cmd[%d] %#x ", result, srb->cmd[result]);
487 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
488 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
492 /* always OUT to the ep */
493 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
495 cbw.dCBWSignature = cpu_to_le32(CBWSIGNATURE);
496 cbw.dCBWTag = cpu_to_le32(CBWTag++);
497 cbw.dCBWDataTransferLength = cpu_to_le32(srb->datalen);
498 cbw.bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
499 cbw.bCBWLUN = srb->lun;
500 cbw.bCDBLength = srb->cmdlen;
501 /* copy the command data into the CBW command data buffer */
503 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
504 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE,
505 &actlen, USB_CNTL_TIMEOUT * 5);
507 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
511 /* FIXME: we also need a CBI_command which sets up the completion
512 * interrupt, and waits for it
514 int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
519 unsigned long status;
522 dir_in = US_DIRECTION(srb->cmd[0]);
525 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
527 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
530 USB_STOR_PRINTF("CBI gets a command: Try %d\n", 5 - retry);
531 #ifdef USB_STOR_DEBUG
534 /* let's send the command via the control pipe */
535 result = usb_control_msg(us->pusb_dev,
536 usb_sndctrlpipe(us->pusb_dev , 0),
538 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
540 srb->cmd, srb->cmdlen,
541 USB_CNTL_TIMEOUT * 5);
542 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
543 " status %X\n", result, us->pusb_dev->status);
544 /* check the return code for the command */
546 if (us->pusb_dev->status & USB_ST_STALLED) {
547 status = us->pusb_dev->status;
548 USB_STOR_PRINTF(" stall during command found,"
550 usb_clear_halt(us->pusb_dev,
551 usb_sndctrlpipe(us->pusb_dev, 0));
552 us->pusb_dev->status = status;
554 USB_STOR_PRINTF(" error during command %02X"
555 " Stat = %X\n", srb->cmd[0],
556 us->pusb_dev->status);
559 /* transfer the data payload for this command, if one exists*/
561 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
562 " direction is %s to go 0x%lx\n", result,
563 dir_in ? "IN" : "OUT", srb->datalen);
565 result = us_one_transfer(us, pipe, (char *)srb->pdata,
567 USB_STOR_PRINTF("CBI attempted to transfer data,"
568 " result is %d status %lX, len %d\n",
569 result, us->pusb_dev->status,
570 us->pusb_dev->act_len);
571 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
573 } /* if (srb->datalen) */
583 int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
588 submit_int_msg(us->pusb_dev, us->irqpipe,
589 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
592 if ((volatile int *) us->ip_wanted == 0)
597 printf(" Did not get interrupt on CBI\n");
599 return USB_STOR_TRANSPORT_ERROR;
602 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
603 us->ip_data, us->pusb_dev->irq_act_len,
604 us->pusb_dev->irq_status);
605 /* UFI gives us ASC and ASCQ, like a request sense */
606 if (us->subclass == US_SC_UFI) {
607 if (srb->cmd[0] == SCSI_REQ_SENSE ||
608 srb->cmd[0] == SCSI_INQUIRY)
609 return USB_STOR_TRANSPORT_GOOD; /* Good */
610 else if (us->ip_data)
611 return USB_STOR_TRANSPORT_FAILED;
613 return USB_STOR_TRANSPORT_GOOD;
615 /* otherwise, we interpret the data normally */
616 switch (us->ip_data) {
618 return USB_STOR_TRANSPORT_GOOD;
620 return USB_STOR_TRANSPORT_FAILED;
622 return USB_STOR_TRANSPORT_ERROR;
624 return USB_STOR_TRANSPORT_ERROR;
627 #define USB_TRANSPORT_UNKNOWN_RETRY 5
628 #define USB_TRANSPORT_NOT_READY_RETRY 10
630 /* clear a stall on an endpoint - special for BBB devices */
631 int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
635 /* ENDPOINT_HALT = 0, so set value to 0 */
636 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
637 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
638 0, endpt, 0, 0, USB_CNTL_TIMEOUT * 5);
642 int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
646 int actlen, data_actlen;
647 unsigned int pipe, pipein, pipeout;
649 #ifdef BBB_XPORT_TRACE
654 dir_in = US_DIRECTION(srb->cmd[0]);
657 USB_STOR_PRINTF("COMMAND phase\n");
658 result = usb_stor_BBB_comdat(srb, us);
660 USB_STOR_PRINTF("failed to send CBW status %ld\n",
661 us->pusb_dev->status);
662 usb_stor_BBB_reset(us);
663 return USB_STOR_TRANSPORT_FAILED;
666 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
667 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
668 /* DATA phase + error handling */
670 /* no data, go immediately to the STATUS phase */
671 if (srb->datalen == 0)
673 USB_STOR_PRINTF("DATA phase\n");
678 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
679 &data_actlen, USB_CNTL_TIMEOUT * 5);
680 /* special handling of STALL in DATA phase */
681 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
682 USB_STOR_PRINTF("DATA:stall\n");
683 /* clear the STALL on the endpoint */
684 result = usb_stor_BBB_clear_endpt_stall(us,
685 dir_in ? us->ep_in : us->ep_out);
687 /* continue on to STATUS phase */
691 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
692 us->pusb_dev->status);
693 usb_stor_BBB_reset(us);
694 return USB_STOR_TRANSPORT_FAILED;
696 #ifdef BBB_XPORT_TRACE
697 for (index = 0; index < data_actlen; index++)
698 printf("pdata[%d] %#x ", index, srb->pdata[index]);
701 /* STATUS phase + error handling */
705 USB_STOR_PRINTF("STATUS phase\n");
706 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
707 &actlen, USB_CNTL_TIMEOUT*5);
709 /* special handling of STALL in STATUS phase */
710 if ((result < 0) && (retry < 1) &&
711 (us->pusb_dev->status & USB_ST_STALLED)) {
712 USB_STOR_PRINTF("STATUS:stall\n");
713 /* clear the STALL on the endpoint */
714 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
715 if (result >= 0 && (retry++ < 1))
720 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
721 us->pusb_dev->status);
722 usb_stor_BBB_reset(us);
723 return USB_STOR_TRANSPORT_FAILED;
725 #ifdef BBB_XPORT_TRACE
726 ptr = (unsigned char *)&csw;
727 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
728 printf("ptr[%d] %#x ", index, ptr[index]);
731 /* misuse pipe to get the residue */
732 pipe = le32_to_cpu(csw.dCSWDataResidue);
733 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
734 pipe = srb->datalen - data_actlen;
735 if (CSWSIGNATURE != le32_to_cpu(csw.dCSWSignature)) {
736 USB_STOR_PRINTF("!CSWSIGNATURE\n");
737 usb_stor_BBB_reset(us);
738 return USB_STOR_TRANSPORT_FAILED;
739 } else if ((CBWTag - 1) != le32_to_cpu(csw.dCSWTag)) {
740 USB_STOR_PRINTF("!Tag\n");
741 usb_stor_BBB_reset(us);
742 return USB_STOR_TRANSPORT_FAILED;
743 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
744 USB_STOR_PRINTF(">PHASE\n");
745 usb_stor_BBB_reset(us);
746 return USB_STOR_TRANSPORT_FAILED;
747 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
748 USB_STOR_PRINTF("=PHASE\n");
749 usb_stor_BBB_reset(us);
750 return USB_STOR_TRANSPORT_FAILED;
751 } else if (data_actlen > srb->datalen) {
752 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
753 data_actlen, srb->datalen);
754 return USB_STOR_TRANSPORT_FAILED;
755 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
756 USB_STOR_PRINTF("FAILED\n");
757 return USB_STOR_TRANSPORT_FAILED;
763 int usb_stor_CB_transport(ccb *srb, struct us_data *us)
771 status = USB_STOR_TRANSPORT_GOOD;
774 /* issue the command */
776 result = usb_stor_CB_comdat(srb, us);
777 USB_STOR_PRINTF("command / Data returned %d, status %X\n",
778 result, us->pusb_dev->status);
779 /* if this is an CBI Protocol, get IRQ */
780 if (us->protocol == US_PR_CBI) {
781 status = usb_stor_CBI_get_status(srb, us);
782 /* if the status is error, report it */
783 if (status == USB_STOR_TRANSPORT_ERROR) {
784 USB_STOR_PRINTF(" USB CBI Command Error\n");
787 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
788 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
790 /* if the status is good, report it */
791 if (status == USB_STOR_TRANSPORT_GOOD) {
792 USB_STOR_PRINTF(" USB CBI Command Good\n");
797 /* do we have to issue an auto request? */
798 /* HERE we have to check the result */
799 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
800 USB_STOR_PRINTF("ERROR %X\n", us->pusb_dev->status);
801 us->transport_reset(us);
802 return USB_STOR_TRANSPORT_ERROR;
804 if ((us->protocol == US_PR_CBI) &&
805 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
806 (srb->cmd[0] == SCSI_INQUIRY))) {
807 /* do not issue an autorequest after request sense */
808 USB_STOR_PRINTF("No auto request and good\n");
809 return USB_STOR_TRANSPORT_GOOD;
811 /* issue an request_sense */
812 memset(&psrb->cmd[0], 0, 12);
813 psrb->cmd[0] = SCSI_REQ_SENSE;
814 psrb->cmd[1] = srb->lun << 5;
817 psrb->pdata = &srb->sense_buf[0];
819 /* issue the command */
820 result = usb_stor_CB_comdat(psrb, us);
821 USB_STOR_PRINTF("auto request returned %d\n", result);
822 /* if this is an CBI Protocol, get IRQ */
823 if (us->protocol == US_PR_CBI)
824 status = usb_stor_CBI_get_status(psrb, us);
826 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
827 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",
828 us->pusb_dev->status);
829 return USB_STOR_TRANSPORT_ERROR;
831 USB_STOR_PRINTF("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
832 srb->sense_buf[0], srb->sense_buf[2],
833 srb->sense_buf[12], srb->sense_buf[13]);
834 /* Check the auto request result */
835 if ((srb->sense_buf[2] == 0) &&
836 (srb->sense_buf[12] == 0) &&
837 (srb->sense_buf[13] == 0)) {
839 return USB_STOR_TRANSPORT_GOOD;
842 /* Check the auto request result */
843 switch (srb->sense_buf[2]) {
845 /* Recovered Error */
846 return USB_STOR_TRANSPORT_GOOD;
850 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
851 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
852 " 0x%02X (NOT READY)\n", srb->cmd[0],
853 srb->sense_buf[0], srb->sense_buf[2],
854 srb->sense_buf[12], srb->sense_buf[13]);
855 return USB_STOR_TRANSPORT_FAILED;
862 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
863 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
864 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
865 srb->sense_buf[2], srb->sense_buf[12],
867 return USB_STOR_TRANSPORT_FAILED;
872 return USB_STOR_TRANSPORT_FAILED;
876 static int usb_inquiry(ccb *srb, struct us_data *ss)
881 memset(&srb->cmd[0], 0, 12);
882 srb->cmd[0] = SCSI_INQUIRY;
883 srb->cmd[1] = srb->lun<<5;
887 i = ss->transport(srb, ss);
888 USB_STOR_PRINTF("inquiry returns %d\n", i);
894 printf("error in inquiry\n");
900 static int usb_request_sense(ccb *srb, struct us_data *ss)
904 ptr = (char *)srb->pdata;
905 memset(&srb->cmd[0], 0, 12);
906 srb->cmd[0] = SCSI_REQ_SENSE;
907 srb->cmd[1] = srb->lun << 5;
910 srb->pdata = &srb->sense_buf[0];
912 ss->transport(srb, ss);
913 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",
914 srb->sense_buf[2], srb->sense_buf[12],
916 srb->pdata = (uchar *)ptr;
920 static int usb_test_unit_ready(ccb *srb, struct us_data *ss)
925 memset(&srb->cmd[0], 0, 12);
926 srb->cmd[0] = SCSI_TST_U_RDY;
927 srb->cmd[1] = srb->lun << 5;
930 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
932 usb_request_sense(srb, ss);
939 static int usb_read_capacity(ccb *srb, struct us_data *ss)
945 memset(&srb->cmd[0], 0, 12);
946 srb->cmd[0] = SCSI_RD_CAPAC;
947 srb->cmd[1] = srb->lun << 5;
950 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
957 static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start,
958 unsigned short blocks)
960 memset(&srb->cmd[0], 0, 12);
961 srb->cmd[0] = SCSI_READ10;
962 srb->cmd[1] = srb->lun << 5;
963 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
964 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
965 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
966 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
967 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
968 srb->cmd[8] = (unsigned char) blocks & 0xff;
970 USB_STOR_PRINTF("read10: start %lx blocks %x\n", start, blocks);
971 return ss->transport(srb, ss);
975 #ifdef CONFIG_USB_BIN_FIXUP
977 * Some USB storage devices queried for SCSI identification data respond with
978 * binary strings, which if output to the console freeze the terminal. The
979 * workaround is to modify the vendor and product strings read from such
980 * device with proper values (as reported by 'usb info').
982 * Vendor and product length limits are taken from the definition of
983 * block_dev_desc_t in include/part.h.
985 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
986 unsigned char vendor[],
987 unsigned char product[]) {
988 const unsigned char max_vendor_len = 40;
989 const unsigned char max_product_len = 20;
990 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
991 strncpy((char *)vendor, "SMSC", max_vendor_len);
992 strncpy((char *)product, "Flash Media Cntrller",
996 #endif /* CONFIG_USB_BIN_FIXUP */
998 #define USB_MAX_READ_BLK 20
1000 unsigned long usb_stor_read(int device, unsigned long blknr,
1001 unsigned long blkcnt, void *buffer)
1003 unsigned long start, blks, buf_addr;
1004 unsigned short smallblks;
1005 struct usb_device *dev;
1007 ccb *srb = &usb_ccb;
1014 USB_STOR_PRINTF("\nusb_read: dev %d \n", device);
1016 for (i = 0; i < USB_MAX_DEVICE; i++) {
1017 dev = usb_get_dev_index(i);
1020 if (dev->devnum == usb_dev_desc[device].target)
1024 usb_disable_asynch(1); /* asynch transfer not allowed */
1025 srb->lun = usb_dev_desc[device].lun;
1026 buf_addr = (unsigned long)buffer;
1029 if (usb_test_unit_ready(srb, (struct us_data *)dev->privptr)) {
1030 printf("Device NOT ready\n Request Sense returned %02X %02X"
1031 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1032 srb->sense_buf[13]);
1036 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx"
1037 " buffer %lx\n", device, start, blks, buf_addr);
1040 /* XXX need some comment here */
1042 srb->pdata = (unsigned char *)buf_addr;
1043 if (blks > USB_MAX_READ_BLK)
1044 smallblks = USB_MAX_READ_BLK;
1046 smallblks = (unsigned short) blks;
1048 if (smallblks == USB_MAX_READ_BLK)
1049 usb_show_progress();
1050 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1051 srb->pdata = (unsigned char *)buf_addr;
1052 if (usb_read_10(srb, (struct us_data *)dev->privptr, start,
1054 USB_STOR_PRINTF("Read ERROR\n");
1055 usb_request_sense(srb, (struct us_data *)dev->privptr);
1063 buf_addr += srb->datalen;
1064 } while (blks != 0);
1066 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",
1067 start, smallblks, buf_addr);
1069 usb_disable_asynch(0); /* asynch transfer allowed */
1070 if (blkcnt >= USB_MAX_READ_BLK)
1076 /* Probe to see if a new device is actually a Storage device */
1077 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1080 struct usb_interface_descriptor *iface;
1082 unsigned int flags = 0;
1087 /* let's examine the device now */
1088 iface = &dev->config.if_desc[ifnum];
1091 /* this is the place to patch some storage devices */
1092 USB_STOR_PRINTF("iVendor %X iProduct %X\n", dev->descriptor.idVendor,
1093 dev->descriptor.idProduct);
1095 if ((dev->descriptor.idVendor) == 0x066b &&
1096 (dev->descriptor.idProduct) == 0x0103) {
1097 USB_STOR_PRINTF("patched for E-USB\n");
1098 protocol = US_PR_CB;
1099 subclass = US_SC_UFI; /* an assumption */
1103 if (dev->descriptor.bDeviceClass != 0 ||
1104 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1105 iface->bInterfaceSubClass < US_SC_MIN ||
1106 iface->bInterfaceSubClass > US_SC_MAX) {
1107 /* if it's not a mass storage, we go no further */
1111 memset(ss, 0, sizeof(struct us_data));
1113 /* At this point, we know we've got a live one */
1114 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1116 /* Initialize the us_data structure with some useful info */
1120 ss->attention_done = 0;
1122 /* If the device has subclass and protocol, then use that. Otherwise,
1123 * take data from the specific interface.
1126 ss->subclass = subclass;
1127 ss->protocol = protocol;
1129 ss->subclass = iface->bInterfaceSubClass;
1130 ss->protocol = iface->bInterfaceProtocol;
1133 /* set the handler pointers based on the protocol */
1134 USB_STOR_PRINTF("Transport: ");
1135 switch (ss->protocol) {
1137 USB_STOR_PRINTF("Control/Bulk\n");
1138 ss->transport = usb_stor_CB_transport;
1139 ss->transport_reset = usb_stor_CB_reset;
1143 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1144 ss->transport = usb_stor_CB_transport;
1145 ss->transport_reset = usb_stor_CB_reset;
1148 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1149 ss->transport = usb_stor_BBB_transport;
1150 ss->transport_reset = usb_stor_BBB_reset;
1153 printf("USB Storage Transport unknown / not yet implemented\n");
1159 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1160 * An optional interrupt is OK (necessary for CBI protocol).
1161 * We will ignore any others.
1163 for (i = 0; i < iface->bNumEndpoints; i++) {
1164 /* is it an BULK endpoint? */
1165 if ((iface->ep_desc[i].bmAttributes &
1166 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1167 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1168 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1169 USB_ENDPOINT_NUMBER_MASK;
1172 iface->ep_desc[i].bEndpointAddress &
1173 USB_ENDPOINT_NUMBER_MASK;
1176 /* is it an interrupt endpoint? */
1177 if ((iface->ep_desc[i].bmAttributes &
1178 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1179 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1180 USB_ENDPOINT_NUMBER_MASK;
1181 ss->irqinterval = iface->ep_desc[i].bInterval;
1184 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1185 ss->ep_in, ss->ep_out, ss->ep_int);
1187 /* Do some basic sanity checks, and bail if we find a problem */
1188 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
1189 !ss->ep_in || !ss->ep_out ||
1190 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1191 USB_STOR_PRINTF("Problems with device\n");
1194 /* set class specific stuff */
1195 /* We only handle certain protocols. Currently, these are
1197 * The SFF8070 accepts the requests used in u-boot
1199 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1200 ss->subclass != US_SC_8070) {
1201 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
1205 /* we had found an interrupt endpoint, prepare irq pipe
1206 * set up the IRQ pipe and handler
1208 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1209 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1210 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1211 dev->irq_handle = usb_stor_irq;
1213 dev->privptr = (void *)ss;
1217 int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1218 block_dev_desc_t *dev_desc)
1220 unsigned char perq, modi;
1221 unsigned long cap[2];
1222 unsigned long *capacity, *blksz;
1223 ccb *pccb = &usb_ccb;
1225 /* for some reasons a couple of devices would not survive this reset */
1228 (dev->descriptor.idVendor == 0x054c &&
1229 dev->descriptor.idProduct == 0x019e)
1231 /* USB007 Mini-USB2 Flash Drive */
1232 (dev->descriptor.idVendor == 0x066f &&
1233 dev->descriptor.idProduct == 0x2010)
1235 /* SanDisk Corporation Cruzer Micro 20044318410546613953 */
1236 (dev->descriptor.idVendor == 0x0781 &&
1237 dev->descriptor.idProduct == 0x5151)
1239 USB_STOR_PRINTF("usb_stor_get_info: skipping RESET..\n");
1241 ss->transport_reset(ss);
1243 pccb->pdata = usb_stor_buf;
1245 dev_desc->target = dev->devnum;
1246 pccb->lun = dev_desc->lun;
1247 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1249 if (usb_inquiry(pccb, ss))
1252 perq = usb_stor_buf[0];
1253 modi = usb_stor_buf[1];
1255 if ((perq & 0x1f) == 0x1f) {
1256 /* skip unknown devices */
1259 if ((modi&0x80) == 0x80) {
1260 /* drive is removable */
1261 dev_desc->removable = 1;
1263 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1264 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1265 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
1266 dev_desc->vendor[8] = 0;
1267 dev_desc->product[16] = 0;
1268 dev_desc->revision[4] = 0;
1269 #ifdef CONFIG_USB_BIN_FIXUP
1270 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1271 (uchar *)dev_desc->product);
1272 #endif /* CONFIG_USB_BIN_FIXUP */
1273 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1275 if (usb_test_unit_ready(pccb, ss)) {
1276 printf("Device NOT ready\n"
1277 " Request Sense returned %02X %02X %02X\n",
1278 pccb->sense_buf[2], pccb->sense_buf[12],
1279 pccb->sense_buf[13]);
1280 if (dev_desc->removable == 1) {
1281 dev_desc->type = perq;
1286 pccb->pdata = (unsigned char *)&cap[0];
1287 memset(pccb->pdata, 0, 8);
1288 if (usb_read_capacity(pccb, ss) != 0) {
1289 printf("READ_CAP ERROR\n");
1293 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0],
1296 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1299 cap[0] = cpu_to_be32(cap[0]);
1300 cap[1] = cpu_to_be32(cap[1]);
1302 /* this assumes bigendian! */
1306 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",
1308 dev_desc->lba = *capacity;
1309 dev_desc->blksz = *blksz;
1310 dev_desc->type = perq;
1311 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1312 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);
1314 init_part(dev_desc);
1316 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);