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 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 block_dev_desc_t *usb_stor_get_dev(int index)
178 return (index < usb_max_devs) ? &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;
232 usb_dev_desc[i].block_write = usb_stor_write;
236 for (i = 0; i < USB_MAX_DEVICE; i++) {
237 dev = usb_get_dev_index(i); /* get device */
238 USB_STOR_PRINTF("i=%d\n", i);
240 break; /* no more devices avaiable */
242 if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
243 /* ok, it is a storage devices
244 * get info and fill it in
246 if (usb_stor_get_info(dev, &usb_stor[usb_max_devs],
247 &usb_dev_desc[usb_max_devs]) == 1)
250 /* if storage device */
251 if (usb_max_devs == USB_MAX_STOR_DEV) {
252 printf("max USB Storage Device reached: %d stopping\n",
258 usb_disable_asynch(0); /* asynch transfer allowed */
259 printf("%d Storage Device(s) found\n", usb_max_devs);
260 if (usb_max_devs > 0)
265 static int usb_stor_irq(struct usb_device *dev)
268 us = (struct us_data *)dev->privptr;
276 #ifdef USB_STOR_DEBUG
278 static void usb_show_srb(ccb *pccb)
281 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
282 for (i = 0; i < 12; i++)
283 printf("%02X ", pccb->cmd[i]);
287 static void display_int_status(unsigned long tmp)
289 printf("Status: %s %s %s %s %s %s %s\n",
290 (tmp & USB_ST_ACTIVE) ? "Active" : "",
291 (tmp & USB_ST_STALLED) ? "Stalled" : "",
292 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
293 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
294 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
295 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
296 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
299 /***********************************************************************
300 * Data transfer routines
301 ***********************************************************************/
303 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
312 /* determine the maximum packet size for these transfers */
313 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
315 /* while we have data left to transfer */
318 /* calculate how long this will be -- maximum or a remainder */
319 this_xfer = length > max_size ? max_size : length;
322 /* setup the retry counter */
325 /* set up the transfer loop */
327 /* transfer the data */
328 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
329 (unsigned int)buf, this_xfer, 11 - maxtry);
330 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
332 USB_CNTL_TIMEOUT * 5);
333 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
334 result, partial, this_xfer);
335 if (us->pusb_dev->status != 0) {
336 /* if we stall, we need to clear it before
339 #ifdef USB_STOR_DEBUG
340 display_int_status(us->pusb_dev->status);
342 if (us->pusb_dev->status & USB_ST_STALLED) {
343 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
344 stat = us->pusb_dev->status;
345 usb_clear_halt(us->pusb_dev, pipe);
346 us->pusb_dev->status = stat;
347 if (this_xfer == partial) {
348 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n", us->pusb_dev->status);
354 if (us->pusb_dev->status & USB_ST_NAK_REC) {
355 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
358 USB_STOR_PRINTF("bulk transferred with error");
359 if (this_xfer == partial) {
360 USB_STOR_PRINTF(" %d, but data ok\n",
361 us->pusb_dev->status);
364 /* if our try counter reaches 0, bail out */
365 USB_STOR_PRINTF(" %d, data %d\n",
366 us->pusb_dev->status, partial);
370 /* update to show what data was transferred */
371 this_xfer -= partial;
373 /* continue until this transfer is done */
377 /* if we get here, we're done and successful */
381 static int usb_stor_BBB_reset(struct us_data *us)
387 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
389 * For Reset Recovery the host shall issue in the following order:
390 * a) a Bulk-Only Mass Storage Reset
391 * b) a Clear Feature HALT to the Bulk-In endpoint
392 * c) a Clear Feature HALT to the Bulk-Out endpoint
394 * This is done in 3 steps.
396 * If the reset doesn't succeed, the device should be port reset.
398 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
400 USB_STOR_PRINTF("BBB_reset\n");
401 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
403 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
404 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT * 5);
406 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
407 USB_STOR_PRINTF("RESET:stall\n");
411 /* long wait for reset */
413 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n", result,
414 us->pusb_dev->status);
415 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
416 result = usb_clear_halt(us->pusb_dev, pipe);
417 /* long wait for reset */
419 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",
420 result, us->pusb_dev->status);
421 /* long wait for reset */
422 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
423 result = usb_clear_halt(us->pusb_dev, pipe);
425 USB_STOR_PRINTF("BBB_reset result %d: status %X"
426 " clearing OUT endpoint\n", result,
427 us->pusb_dev->status);
428 USB_STOR_PRINTF("BBB_reset done\n");
432 /* FIXME: this reset function doesn't really reset the port, and it
433 * should. Actually it should probably do what it's doing here, and
434 * reset the port physically
436 static int usb_stor_CB_reset(struct us_data *us)
438 unsigned char cmd[12];
441 USB_STOR_PRINTF("CB_reset\n");
442 memset(cmd, 0xff, sizeof(cmd));
443 cmd[0] = SCSI_SEND_DIAG;
445 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
447 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
448 0, us->ifnum, cmd, sizeof(cmd),
449 USB_CNTL_TIMEOUT * 5);
451 /* long wait for reset */
453 USB_STOR_PRINTF("CB_reset result %d: status %X"
454 " clearing endpoint halt\n", result,
455 us->pusb_dev->status);
456 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
457 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
459 USB_STOR_PRINTF("CB_reset done\n");
464 * Set up the command for a BBB device. Note that the actual SCSI
465 * command is copied into cbw.CBWCDB.
467 int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
475 dir_in = US_DIRECTION(srb->cmd[0]);
477 #ifdef BBB_COMDAT_TRACE
478 printf("dir %d lun %d cmdlen %d cmd %p datalen %d pdata %p\n",
479 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
482 for (result = 0; result < srb->cmdlen; result++)
483 printf("cmd[%d] %#x ", result, srb->cmd[result]);
488 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
489 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
493 /* always OUT to the ep */
494 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
496 cbw.dCBWSignature = cpu_to_le32(CBWSIGNATURE);
497 cbw.dCBWTag = cpu_to_le32(CBWTag++);
498 cbw.dCBWDataTransferLength = cpu_to_le32(srb->datalen);
499 cbw.bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
500 cbw.bCBWLUN = srb->lun;
501 cbw.bCDBLength = srb->cmdlen;
502 /* copy the command data into the CBW command data buffer */
504 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
505 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE,
506 &actlen, USB_CNTL_TIMEOUT * 5);
508 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
512 /* FIXME: we also need a CBI_command which sets up the completion
513 * interrupt, and waits for it
515 int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
520 unsigned long status;
523 dir_in = US_DIRECTION(srb->cmd[0]);
526 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
528 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
531 USB_STOR_PRINTF("CBI gets a command: Try %d\n", 5 - retry);
532 #ifdef USB_STOR_DEBUG
535 /* let's send the command via the control pipe */
536 result = usb_control_msg(us->pusb_dev,
537 usb_sndctrlpipe(us->pusb_dev , 0),
539 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
541 srb->cmd, srb->cmdlen,
542 USB_CNTL_TIMEOUT * 5);
543 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
544 " status %X\n", result, us->pusb_dev->status);
545 /* check the return code for the command */
547 if (us->pusb_dev->status & USB_ST_STALLED) {
548 status = us->pusb_dev->status;
549 USB_STOR_PRINTF(" stall during command found,"
551 usb_clear_halt(us->pusb_dev,
552 usb_sndctrlpipe(us->pusb_dev, 0));
553 us->pusb_dev->status = status;
555 USB_STOR_PRINTF(" error during command %02X"
556 " Stat = %X\n", srb->cmd[0],
557 us->pusb_dev->status);
560 /* transfer the data payload for this command, if one exists*/
562 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
563 " direction is %s to go 0x%lx\n", result,
564 dir_in ? "IN" : "OUT", srb->datalen);
566 result = us_one_transfer(us, pipe, (char *)srb->pdata,
568 USB_STOR_PRINTF("CBI attempted to transfer data,"
569 " result is %d status %lX, len %d\n",
570 result, us->pusb_dev->status,
571 us->pusb_dev->act_len);
572 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
574 } /* if (srb->datalen) */
584 int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
589 submit_int_msg(us->pusb_dev, us->irqpipe,
590 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
593 if ((volatile int *) us->ip_wanted == 0)
598 printf(" Did not get interrupt on CBI\n");
600 return USB_STOR_TRANSPORT_ERROR;
603 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
604 us->ip_data, us->pusb_dev->irq_act_len,
605 us->pusb_dev->irq_status);
606 /* UFI gives us ASC and ASCQ, like a request sense */
607 if (us->subclass == US_SC_UFI) {
608 if (srb->cmd[0] == SCSI_REQ_SENSE ||
609 srb->cmd[0] == SCSI_INQUIRY)
610 return USB_STOR_TRANSPORT_GOOD; /* Good */
611 else if (us->ip_data)
612 return USB_STOR_TRANSPORT_FAILED;
614 return USB_STOR_TRANSPORT_GOOD;
616 /* otherwise, we interpret the data normally */
617 switch (us->ip_data) {
619 return USB_STOR_TRANSPORT_GOOD;
621 return USB_STOR_TRANSPORT_FAILED;
623 return USB_STOR_TRANSPORT_ERROR;
625 return USB_STOR_TRANSPORT_ERROR;
628 #define USB_TRANSPORT_UNKNOWN_RETRY 5
629 #define USB_TRANSPORT_NOT_READY_RETRY 10
631 /* clear a stall on an endpoint - special for BBB devices */
632 int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
636 /* ENDPOINT_HALT = 0, so set value to 0 */
637 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
638 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
639 0, endpt, 0, 0, USB_CNTL_TIMEOUT * 5);
643 int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
647 int actlen, data_actlen;
648 unsigned int pipe, pipein, pipeout;
650 #ifdef BBB_XPORT_TRACE
655 dir_in = US_DIRECTION(srb->cmd[0]);
658 USB_STOR_PRINTF("COMMAND phase\n");
659 result = usb_stor_BBB_comdat(srb, us);
661 USB_STOR_PRINTF("failed to send CBW status %ld\n",
662 us->pusb_dev->status);
663 usb_stor_BBB_reset(us);
664 return USB_STOR_TRANSPORT_FAILED;
667 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
668 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
669 /* DATA phase + error handling */
671 /* no data, go immediately to the STATUS phase */
672 if (srb->datalen == 0)
674 USB_STOR_PRINTF("DATA phase\n");
679 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
680 &data_actlen, USB_CNTL_TIMEOUT * 5);
681 /* special handling of STALL in DATA phase */
682 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
683 USB_STOR_PRINTF("DATA:stall\n");
684 /* clear the STALL on the endpoint */
685 result = usb_stor_BBB_clear_endpt_stall(us,
686 dir_in ? us->ep_in : us->ep_out);
688 /* continue on to STATUS phase */
692 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
693 us->pusb_dev->status);
694 usb_stor_BBB_reset(us);
695 return USB_STOR_TRANSPORT_FAILED;
697 #ifdef BBB_XPORT_TRACE
698 for (index = 0; index < data_actlen; index++)
699 printf("pdata[%d] %#x ", index, srb->pdata[index]);
702 /* STATUS phase + error handling */
706 USB_STOR_PRINTF("STATUS phase\n");
707 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
708 &actlen, USB_CNTL_TIMEOUT*5);
710 /* special handling of STALL in STATUS phase */
711 if ((result < 0) && (retry < 1) &&
712 (us->pusb_dev->status & USB_ST_STALLED)) {
713 USB_STOR_PRINTF("STATUS:stall\n");
714 /* clear the STALL on the endpoint */
715 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
716 if (result >= 0 && (retry++ < 1))
721 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
722 us->pusb_dev->status);
723 usb_stor_BBB_reset(us);
724 return USB_STOR_TRANSPORT_FAILED;
726 #ifdef BBB_XPORT_TRACE
727 ptr = (unsigned char *)&csw;
728 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
729 printf("ptr[%d] %#x ", index, ptr[index]);
732 /* misuse pipe to get the residue */
733 pipe = le32_to_cpu(csw.dCSWDataResidue);
734 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
735 pipe = srb->datalen - data_actlen;
736 if (CSWSIGNATURE != le32_to_cpu(csw.dCSWSignature)) {
737 USB_STOR_PRINTF("!CSWSIGNATURE\n");
738 usb_stor_BBB_reset(us);
739 return USB_STOR_TRANSPORT_FAILED;
740 } else if ((CBWTag - 1) != le32_to_cpu(csw.dCSWTag)) {
741 USB_STOR_PRINTF("!Tag\n");
742 usb_stor_BBB_reset(us);
743 return USB_STOR_TRANSPORT_FAILED;
744 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
745 USB_STOR_PRINTF(">PHASE\n");
746 usb_stor_BBB_reset(us);
747 return USB_STOR_TRANSPORT_FAILED;
748 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
749 USB_STOR_PRINTF("=PHASE\n");
750 usb_stor_BBB_reset(us);
751 return USB_STOR_TRANSPORT_FAILED;
752 } else if (data_actlen > srb->datalen) {
753 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
754 data_actlen, srb->datalen);
755 return USB_STOR_TRANSPORT_FAILED;
756 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
757 USB_STOR_PRINTF("FAILED\n");
758 return USB_STOR_TRANSPORT_FAILED;
764 int usb_stor_CB_transport(ccb *srb, struct us_data *us)
772 status = USB_STOR_TRANSPORT_GOOD;
775 /* issue the command */
777 result = usb_stor_CB_comdat(srb, us);
778 USB_STOR_PRINTF("command / Data returned %d, status %X\n",
779 result, us->pusb_dev->status);
780 /* if this is an CBI Protocol, get IRQ */
781 if (us->protocol == US_PR_CBI) {
782 status = usb_stor_CBI_get_status(srb, us);
783 /* if the status is error, report it */
784 if (status == USB_STOR_TRANSPORT_ERROR) {
785 USB_STOR_PRINTF(" USB CBI Command Error\n");
788 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
789 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
791 /* if the status is good, report it */
792 if (status == USB_STOR_TRANSPORT_GOOD) {
793 USB_STOR_PRINTF(" USB CBI Command Good\n");
798 /* do we have to issue an auto request? */
799 /* HERE we have to check the result */
800 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
801 USB_STOR_PRINTF("ERROR %X\n", us->pusb_dev->status);
802 us->transport_reset(us);
803 return USB_STOR_TRANSPORT_ERROR;
805 if ((us->protocol == US_PR_CBI) &&
806 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
807 (srb->cmd[0] == SCSI_INQUIRY))) {
808 /* do not issue an autorequest after request sense */
809 USB_STOR_PRINTF("No auto request and good\n");
810 return USB_STOR_TRANSPORT_GOOD;
812 /* issue an request_sense */
813 memset(&psrb->cmd[0], 0, 12);
814 psrb->cmd[0] = SCSI_REQ_SENSE;
815 psrb->cmd[1] = srb->lun << 5;
818 psrb->pdata = &srb->sense_buf[0];
820 /* issue the command */
821 result = usb_stor_CB_comdat(psrb, us);
822 USB_STOR_PRINTF("auto request returned %d\n", result);
823 /* if this is an CBI Protocol, get IRQ */
824 if (us->protocol == US_PR_CBI)
825 status = usb_stor_CBI_get_status(psrb, us);
827 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
828 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",
829 us->pusb_dev->status);
830 return USB_STOR_TRANSPORT_ERROR;
832 USB_STOR_PRINTF("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
833 srb->sense_buf[0], srb->sense_buf[2],
834 srb->sense_buf[12], srb->sense_buf[13]);
835 /* Check the auto request result */
836 if ((srb->sense_buf[2] == 0) &&
837 (srb->sense_buf[12] == 0) &&
838 (srb->sense_buf[13] == 0)) {
840 return USB_STOR_TRANSPORT_GOOD;
843 /* Check the auto request result */
844 switch (srb->sense_buf[2]) {
846 /* Recovered Error */
847 return USB_STOR_TRANSPORT_GOOD;
851 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
852 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
853 " 0x%02X (NOT READY)\n", srb->cmd[0],
854 srb->sense_buf[0], srb->sense_buf[2],
855 srb->sense_buf[12], srb->sense_buf[13]);
856 return USB_STOR_TRANSPORT_FAILED;
863 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
864 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
865 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
866 srb->sense_buf[2], srb->sense_buf[12],
868 return USB_STOR_TRANSPORT_FAILED;
873 return USB_STOR_TRANSPORT_FAILED;
877 static int usb_inquiry(ccb *srb, struct us_data *ss)
882 memset(&srb->cmd[0], 0, 12);
883 srb->cmd[0] = SCSI_INQUIRY;
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;
909 srb->pdata = &srb->sense_buf[0];
911 ss->transport(srb, ss);
912 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",
913 srb->sense_buf[2], srb->sense_buf[12],
915 srb->pdata = (uchar *)ptr;
919 static int usb_test_unit_ready(ccb *srb, struct us_data *ss)
924 memset(&srb->cmd[0], 0, 12);
925 srb->cmd[0] = SCSI_TST_U_RDY;
928 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
930 usb_request_sense(srb, ss);
937 static int usb_read_capacity(ccb *srb, struct us_data *ss)
943 memset(&srb->cmd[0], 0, 12);
944 srb->cmd[0] = SCSI_RD_CAPAC;
947 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
954 static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start,
955 unsigned short blocks)
957 memset(&srb->cmd[0], 0, 12);
958 srb->cmd[0] = SCSI_READ10;
959 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
960 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
961 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
962 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
963 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
964 srb->cmd[8] = (unsigned char) blocks & 0xff;
966 USB_STOR_PRINTF("read10: start %lx blocks %x\n", start, blocks);
967 return ss->transport(srb, ss);
970 static int usb_write_10(ccb *srb, struct us_data *ss, unsigned long start,
971 unsigned short blocks)
973 memset(&srb->cmd[0], 0, 12);
974 srb->cmd[0] = SCSI_WRITE10;
975 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
976 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
977 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
978 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
979 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
980 srb->cmd[8] = (unsigned char) blocks & 0xff;
982 USB_STOR_PRINTF("write10: start %lx blocks %x\n", start, blocks);
983 return ss->transport(srb, ss);
987 #ifdef CONFIG_USB_BIN_FIXUP
989 * Some USB storage devices queried for SCSI identification data respond with
990 * binary strings, which if output to the console freeze the terminal. The
991 * workaround is to modify the vendor and product strings read from such
992 * device with proper values (as reported by 'usb info').
994 * Vendor and product length limits are taken from the definition of
995 * block_dev_desc_t in include/part.h.
997 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
998 unsigned char vendor[],
999 unsigned char product[]) {
1000 const unsigned char max_vendor_len = 40;
1001 const unsigned char max_product_len = 20;
1002 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
1003 strncpy((char *)vendor, "SMSC", max_vendor_len);
1004 strncpy((char *)product, "Flash Media Cntrller",
1008 #endif /* CONFIG_USB_BIN_FIXUP */
1010 #define USB_MAX_READ_BLK 20
1012 unsigned long usb_stor_read(int device, unsigned long blknr,
1013 unsigned long blkcnt, void *buffer)
1015 unsigned long start, blks, buf_addr;
1016 unsigned short smallblks;
1017 struct usb_device *dev;
1019 ccb *srb = &usb_ccb;
1026 USB_STOR_PRINTF("\nusb_read: dev %d \n", device);
1028 for (i = 0; i < USB_MAX_DEVICE; i++) {
1029 dev = usb_get_dev_index(i);
1032 if (dev->devnum == usb_dev_desc[device].target)
1036 usb_disable_asynch(1); /* asynch transfer not allowed */
1037 srb->lun = usb_dev_desc[device].lun;
1038 buf_addr = (unsigned long)buffer;
1041 if (usb_test_unit_ready(srb, (struct us_data *)dev->privptr)) {
1042 printf("Device NOT ready\n Request Sense returned %02X %02X"
1043 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1044 srb->sense_buf[13]);
1048 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx"
1049 " buffer %lx\n", device, start, blks, buf_addr);
1052 /* XXX need some comment here */
1054 srb->pdata = (unsigned char *)buf_addr;
1055 if (blks > USB_MAX_READ_BLK)
1056 smallblks = USB_MAX_READ_BLK;
1058 smallblks = (unsigned short) blks;
1060 if (smallblks == USB_MAX_READ_BLK)
1061 usb_show_progress();
1062 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1063 srb->pdata = (unsigned char *)buf_addr;
1064 if (usb_read_10(srb, (struct us_data *)dev->privptr, start,
1066 USB_STOR_PRINTF("Read ERROR\n");
1067 usb_request_sense(srb, (struct us_data *)dev->privptr);
1075 buf_addr += srb->datalen;
1076 } while (blks != 0);
1078 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",
1079 start, smallblks, buf_addr);
1081 usb_disable_asynch(0); /* asynch transfer allowed */
1082 if (blkcnt >= USB_MAX_READ_BLK)
1087 #define USB_MAX_WRITE_BLK 20
1089 unsigned long usb_stor_write(int device, unsigned long blknr,
1090 unsigned long blkcnt, const void *buffer)
1092 unsigned long start, blks, buf_addr;
1093 unsigned short smallblks;
1094 struct usb_device *dev;
1096 ccb *srb = &usb_ccb;
1103 USB_STOR_PRINTF("\nusb_write: dev %d \n", device);
1105 for (i = 0; i < USB_MAX_DEVICE; i++) {
1106 dev = usb_get_dev_index(i);
1109 if (dev->devnum == usb_dev_desc[device].target)
1113 usb_disable_asynch(1); /* asynch transfer not allowed */
1115 srb->lun = usb_dev_desc[device].lun;
1116 buf_addr = (unsigned long)buffer;
1119 if (usb_test_unit_ready(srb, (struct us_data *)dev->privptr)) {
1120 printf("Device NOT ready\n Request Sense returned %02X %02X"
1121 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1122 srb->sense_buf[13]);
1126 USB_STOR_PRINTF("\nusb_write: dev %d startblk %lx, blccnt %lx"
1127 " buffer %lx\n", device, start, blks, buf_addr);
1130 /* If write fails retry for max retry count else
1131 * return with number of blocks written successfully.
1134 srb->pdata = (unsigned char *)buf_addr;
1135 if (blks > USB_MAX_WRITE_BLK)
1136 smallblks = USB_MAX_WRITE_BLK;
1138 smallblks = (unsigned short) blks;
1140 if (smallblks == USB_MAX_WRITE_BLK)
1141 usb_show_progress();
1142 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1143 srb->pdata = (unsigned char *)buf_addr;
1144 if (usb_write_10(srb, (struct us_data *)dev->privptr, start,
1146 USB_STOR_PRINTF("Write ERROR\n");
1147 usb_request_sense(srb, (struct us_data *)dev->privptr);
1155 buf_addr += srb->datalen;
1156 } while (blks != 0);
1158 USB_STOR_PRINTF("usb_write: end startblk %lx, blccnt %x buffer %lx\n",
1159 start, smallblks, buf_addr);
1161 usb_disable_asynch(0); /* asynch transfer allowed */
1162 if (blkcnt >= USB_MAX_WRITE_BLK)
1168 /* Probe to see if a new device is actually a Storage device */
1169 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1172 struct usb_interface *iface;
1174 unsigned int flags = 0;
1179 /* let's examine the device now */
1180 iface = &dev->config.if_desc[ifnum];
1183 /* this is the place to patch some storage devices */
1184 USB_STOR_PRINTF("iVendor %X iProduct %X\n", dev->descriptor.idVendor,
1185 dev->descriptor.idProduct);
1187 if ((dev->descriptor.idVendor) == 0x066b &&
1188 (dev->descriptor.idProduct) == 0x0103) {
1189 USB_STOR_PRINTF("patched for E-USB\n");
1190 protocol = US_PR_CB;
1191 subclass = US_SC_UFI; /* an assumption */
1195 if (dev->descriptor.bDeviceClass != 0 ||
1196 iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1197 iface->desc.bInterfaceSubClass < US_SC_MIN ||
1198 iface->desc.bInterfaceSubClass > US_SC_MAX) {
1199 /* if it's not a mass storage, we go no further */
1203 memset(ss, 0, sizeof(struct us_data));
1205 /* At this point, we know we've got a live one */
1206 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1208 /* Initialize the us_data structure with some useful info */
1212 ss->attention_done = 0;
1214 /* If the device has subclass and protocol, then use that. Otherwise,
1215 * take data from the specific interface.
1218 ss->subclass = subclass;
1219 ss->protocol = protocol;
1221 ss->subclass = iface->desc.bInterfaceSubClass;
1222 ss->protocol = iface->desc.bInterfaceProtocol;
1225 /* set the handler pointers based on the protocol */
1226 USB_STOR_PRINTF("Transport: ");
1227 switch (ss->protocol) {
1229 USB_STOR_PRINTF("Control/Bulk\n");
1230 ss->transport = usb_stor_CB_transport;
1231 ss->transport_reset = usb_stor_CB_reset;
1235 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1236 ss->transport = usb_stor_CB_transport;
1237 ss->transport_reset = usb_stor_CB_reset;
1240 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1241 ss->transport = usb_stor_BBB_transport;
1242 ss->transport_reset = usb_stor_BBB_reset;
1245 printf("USB Storage Transport unknown / not yet implemented\n");
1251 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1252 * An optional interrupt is OK (necessary for CBI protocol).
1253 * We will ignore any others.
1255 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1256 /* is it an BULK endpoint? */
1257 if ((iface->ep_desc[i].bmAttributes &
1258 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1259 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1260 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1261 USB_ENDPOINT_NUMBER_MASK;
1264 iface->ep_desc[i].bEndpointAddress &
1265 USB_ENDPOINT_NUMBER_MASK;
1268 /* is it an interrupt endpoint? */
1269 if ((iface->ep_desc[i].bmAttributes &
1270 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1271 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1272 USB_ENDPOINT_NUMBER_MASK;
1273 ss->irqinterval = iface->ep_desc[i].bInterval;
1276 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1277 ss->ep_in, ss->ep_out, ss->ep_int);
1279 /* Do some basic sanity checks, and bail if we find a problem */
1280 if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
1281 !ss->ep_in || !ss->ep_out ||
1282 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1283 USB_STOR_PRINTF("Problems with device\n");
1286 /* set class specific stuff */
1287 /* We only handle certain protocols. Currently, these are
1289 * The SFF8070 accepts the requests used in u-boot
1291 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1292 ss->subclass != US_SC_8070) {
1293 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
1297 /* we had found an interrupt endpoint, prepare irq pipe
1298 * set up the IRQ pipe and handler
1300 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1301 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1302 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1303 dev->irq_handle = usb_stor_irq;
1305 dev->privptr = (void *)ss;
1309 int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1310 block_dev_desc_t *dev_desc)
1312 unsigned char perq, modi;
1313 unsigned long cap[2];
1314 unsigned long *capacity, *blksz;
1315 ccb *pccb = &usb_ccb;
1317 /* for some reasons a couple of devices would not survive this reset */
1320 (dev->descriptor.idVendor == 0x054c &&
1321 dev->descriptor.idProduct == 0x019e)
1323 /* USB007 Mini-USB2 Flash Drive */
1324 (dev->descriptor.idVendor == 0x066f &&
1325 dev->descriptor.idProduct == 0x2010)
1327 /* SanDisk Corporation Cruzer Micro 20044318410546613953 */
1328 (dev->descriptor.idVendor == 0x0781 &&
1329 dev->descriptor.idProduct == 0x5151)
1332 * SanDisk Corporation U3 Cruzer Micro 1/4GB
1333 * Flash Drive 000016244373FFB4
1335 (dev->descriptor.idVendor == 0x0781 &&
1336 dev->descriptor.idProduct == 0x5406)
1338 USB_STOR_PRINTF("usb_stor_get_info: skipping RESET..\n");
1340 ss->transport_reset(ss);
1342 pccb->pdata = usb_stor_buf;
1344 dev_desc->target = dev->devnum;
1345 pccb->lun = dev_desc->lun;
1346 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1348 if (usb_inquiry(pccb, ss))
1351 perq = usb_stor_buf[0];
1352 modi = usb_stor_buf[1];
1354 if ((perq & 0x1f) == 0x1f) {
1355 /* skip unknown devices */
1358 if ((modi&0x80) == 0x80) {
1359 /* drive is removable */
1360 dev_desc->removable = 1;
1362 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1363 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1364 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
1365 dev_desc->vendor[8] = 0;
1366 dev_desc->product[16] = 0;
1367 dev_desc->revision[4] = 0;
1368 #ifdef CONFIG_USB_BIN_FIXUP
1369 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1370 (uchar *)dev_desc->product);
1371 #endif /* CONFIG_USB_BIN_FIXUP */
1372 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1374 if (usb_test_unit_ready(pccb, ss)) {
1375 printf("Device NOT ready\n"
1376 " Request Sense returned %02X %02X %02X\n",
1377 pccb->sense_buf[2], pccb->sense_buf[12],
1378 pccb->sense_buf[13]);
1379 if (dev_desc->removable == 1) {
1380 dev_desc->type = perq;
1385 pccb->pdata = (unsigned char *)&cap[0];
1386 memset(pccb->pdata, 0, 8);
1387 if (usb_read_capacity(pccb, ss) != 0) {
1388 printf("READ_CAP ERROR\n");
1392 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0],
1395 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1398 cap[0] = cpu_to_be32(cap[0]);
1399 cap[1] = cpu_to_be32(cap[1]);
1401 /* this assumes bigendian! */
1405 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",
1407 dev_desc->lba = *capacity;
1408 dev_desc->blksz = *blksz;
1409 dev_desc->type = perq;
1410 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1411 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);
1413 init_part(dev_desc);
1415 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);