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 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 static unsigned int usb_get_max_lun(struct us_data *us)
210 unsigned char result;
211 len = usb_control_msg(us->pusb_dev,
212 usb_rcvctrlpipe(us->pusb_dev, 0),
214 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
216 &result, sizeof(result),
217 USB_CNTL_TIMEOUT * 5);
218 USB_STOR_PRINTF("Get Max LUN -> len = %i, result = %i\n",
220 return (len > 0) ? result : 0;
223 /*******************************************************************************
224 * scan the usb and reports device info
225 * to the user if mode = 1
226 * returns current device or -1 if no
228 int usb_stor_scan(int mode)
231 struct usb_device *dev;
234 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
237 printf(" scanning bus for storage devices... ");
239 usb_disable_asynch(1); /* asynch transfer not allowed */
241 for (i = 0; i < USB_MAX_STOR_DEV; i++) {
242 memset(&usb_dev_desc[i], 0, sizeof(block_dev_desc_t));
243 usb_dev_desc[i].if_type = IF_TYPE_USB;
244 usb_dev_desc[i].dev = i;
245 usb_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
246 usb_dev_desc[i].target = 0xff;
247 usb_dev_desc[i].type = DEV_TYPE_UNKNOWN;
248 usb_dev_desc[i].block_read = usb_stor_read;
249 usb_dev_desc[i].block_write = usb_stor_write;
253 for (i = 0; i < USB_MAX_DEVICE; i++) {
254 dev = usb_get_dev_index(i); /* get device */
255 USB_STOR_PRINTF("i=%d\n", i);
257 break; /* no more devices avaiable */
259 if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
260 /* OK, it's a storage device. Iterate over its LUNs
261 * and populate `usb_dev_desc'.
263 int lun, max_lun, start = usb_max_devs;
265 max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
267 lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
269 usb_dev_desc[usb_max_devs].lun = lun;
270 if (usb_stor_get_info(dev, &usb_stor[start],
271 &usb_dev_desc[usb_max_devs]) == 1) {
276 /* if storage device */
277 if (usb_max_devs == USB_MAX_STOR_DEV) {
278 printf("max USB Storage Device reached: %d stopping\n",
284 usb_disable_asynch(0); /* asynch transfer allowed */
285 printf("%d Storage Device(s) found\n", usb_max_devs);
286 if (usb_max_devs > 0)
291 static int usb_stor_irq(struct usb_device *dev)
294 us = (struct us_data *)dev->privptr;
302 #ifdef USB_STOR_DEBUG
304 static void usb_show_srb(ccb *pccb)
307 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
308 for (i = 0; i < 12; i++)
309 printf("%02X ", pccb->cmd[i]);
313 static void display_int_status(unsigned long tmp)
315 printf("Status: %s %s %s %s %s %s %s\n",
316 (tmp & USB_ST_ACTIVE) ? "Active" : "",
317 (tmp & USB_ST_STALLED) ? "Stalled" : "",
318 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
319 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
320 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
321 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
322 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
325 /***********************************************************************
326 * Data transfer routines
327 ***********************************************************************/
329 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
338 /* determine the maximum packet size for these transfers */
339 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
341 /* while we have data left to transfer */
344 /* calculate how long this will be -- maximum or a remainder */
345 this_xfer = length > max_size ? max_size : length;
348 /* setup the retry counter */
351 /* set up the transfer loop */
353 /* transfer the data */
354 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
355 (unsigned int)buf, this_xfer, 11 - maxtry);
356 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
358 USB_CNTL_TIMEOUT * 5);
359 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
360 result, partial, this_xfer);
361 if (us->pusb_dev->status != 0) {
362 /* if we stall, we need to clear it before
365 #ifdef USB_STOR_DEBUG
366 display_int_status(us->pusb_dev->status);
368 if (us->pusb_dev->status & USB_ST_STALLED) {
369 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
370 stat = us->pusb_dev->status;
371 usb_clear_halt(us->pusb_dev, pipe);
372 us->pusb_dev->status = stat;
373 if (this_xfer == partial) {
374 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n", us->pusb_dev->status);
380 if (us->pusb_dev->status & USB_ST_NAK_REC) {
381 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
384 USB_STOR_PRINTF("bulk transferred with error");
385 if (this_xfer == partial) {
386 USB_STOR_PRINTF(" %d, but data ok\n",
387 us->pusb_dev->status);
390 /* if our try counter reaches 0, bail out */
391 USB_STOR_PRINTF(" %d, data %d\n",
392 us->pusb_dev->status, partial);
396 /* update to show what data was transferred */
397 this_xfer -= partial;
399 /* continue until this transfer is done */
403 /* if we get here, we're done and successful */
407 static int usb_stor_BBB_reset(struct us_data *us)
413 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
415 * For Reset Recovery the host shall issue in the following order:
416 * a) a Bulk-Only Mass Storage Reset
417 * b) a Clear Feature HALT to the Bulk-In endpoint
418 * c) a Clear Feature HALT to the Bulk-Out endpoint
420 * This is done in 3 steps.
422 * If the reset doesn't succeed, the device should be port reset.
424 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
426 USB_STOR_PRINTF("BBB_reset\n");
427 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
429 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
430 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT * 5);
432 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
433 USB_STOR_PRINTF("RESET:stall\n");
437 /* long wait for reset */
439 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n", result,
440 us->pusb_dev->status);
441 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
442 result = usb_clear_halt(us->pusb_dev, pipe);
443 /* long wait for reset */
445 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",
446 result, us->pusb_dev->status);
447 /* long wait for reset */
448 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
449 result = usb_clear_halt(us->pusb_dev, pipe);
451 USB_STOR_PRINTF("BBB_reset result %d: status %X"
452 " clearing OUT endpoint\n", result,
453 us->pusb_dev->status);
454 USB_STOR_PRINTF("BBB_reset done\n");
458 /* FIXME: this reset function doesn't really reset the port, and it
459 * should. Actually it should probably do what it's doing here, and
460 * reset the port physically
462 static int usb_stor_CB_reset(struct us_data *us)
464 unsigned char cmd[12];
467 USB_STOR_PRINTF("CB_reset\n");
468 memset(cmd, 0xff, sizeof(cmd));
469 cmd[0] = SCSI_SEND_DIAG;
471 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
473 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
474 0, us->ifnum, cmd, sizeof(cmd),
475 USB_CNTL_TIMEOUT * 5);
477 /* long wait for reset */
479 USB_STOR_PRINTF("CB_reset result %d: status %X"
480 " clearing endpoint halt\n", result,
481 us->pusb_dev->status);
482 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
483 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
485 USB_STOR_PRINTF("CB_reset done\n");
490 * Set up the command for a BBB device. Note that the actual SCSI
491 * command is copied into cbw.CBWCDB.
493 int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
501 dir_in = US_DIRECTION(srb->cmd[0]);
503 #ifdef BBB_COMDAT_TRACE
504 printf("dir %d lun %d cmdlen %d cmd %p datalen %d pdata %p\n",
505 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
508 for (result = 0; result < srb->cmdlen; result++)
509 printf("cmd[%d] %#x ", result, srb->cmd[result]);
514 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
515 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
519 /* always OUT to the ep */
520 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
522 cbw.dCBWSignature = cpu_to_le32(CBWSIGNATURE);
523 cbw.dCBWTag = cpu_to_le32(CBWTag++);
524 cbw.dCBWDataTransferLength = cpu_to_le32(srb->datalen);
525 cbw.bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
526 cbw.bCBWLUN = srb->lun;
527 cbw.bCDBLength = srb->cmdlen;
528 /* copy the command data into the CBW command data buffer */
530 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
531 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE,
532 &actlen, USB_CNTL_TIMEOUT * 5);
534 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
538 /* FIXME: we also need a CBI_command which sets up the completion
539 * interrupt, and waits for it
541 int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
546 unsigned long status;
549 dir_in = US_DIRECTION(srb->cmd[0]);
552 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
554 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
557 USB_STOR_PRINTF("CBI gets a command: Try %d\n", 5 - retry);
558 #ifdef USB_STOR_DEBUG
561 /* let's send the command via the control pipe */
562 result = usb_control_msg(us->pusb_dev,
563 usb_sndctrlpipe(us->pusb_dev , 0),
565 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
567 srb->cmd, srb->cmdlen,
568 USB_CNTL_TIMEOUT * 5);
569 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
570 " status %X\n", result, us->pusb_dev->status);
571 /* check the return code for the command */
573 if (us->pusb_dev->status & USB_ST_STALLED) {
574 status = us->pusb_dev->status;
575 USB_STOR_PRINTF(" stall during command found,"
577 usb_clear_halt(us->pusb_dev,
578 usb_sndctrlpipe(us->pusb_dev, 0));
579 us->pusb_dev->status = status;
581 USB_STOR_PRINTF(" error during command %02X"
582 " Stat = %X\n", srb->cmd[0],
583 us->pusb_dev->status);
586 /* transfer the data payload for this command, if one exists*/
588 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
589 " direction is %s to go 0x%lx\n", result,
590 dir_in ? "IN" : "OUT", srb->datalen);
592 result = us_one_transfer(us, pipe, (char *)srb->pdata,
594 USB_STOR_PRINTF("CBI attempted to transfer data,"
595 " result is %d status %lX, len %d\n",
596 result, us->pusb_dev->status,
597 us->pusb_dev->act_len);
598 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
600 } /* if (srb->datalen) */
610 int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
615 submit_int_msg(us->pusb_dev, us->irqpipe,
616 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
619 if ((volatile int *) us->ip_wanted == 0)
624 printf(" Did not get interrupt on CBI\n");
626 return USB_STOR_TRANSPORT_ERROR;
629 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
630 us->ip_data, us->pusb_dev->irq_act_len,
631 us->pusb_dev->irq_status);
632 /* UFI gives us ASC and ASCQ, like a request sense */
633 if (us->subclass == US_SC_UFI) {
634 if (srb->cmd[0] == SCSI_REQ_SENSE ||
635 srb->cmd[0] == SCSI_INQUIRY)
636 return USB_STOR_TRANSPORT_GOOD; /* Good */
637 else if (us->ip_data)
638 return USB_STOR_TRANSPORT_FAILED;
640 return USB_STOR_TRANSPORT_GOOD;
642 /* otherwise, we interpret the data normally */
643 switch (us->ip_data) {
645 return USB_STOR_TRANSPORT_GOOD;
647 return USB_STOR_TRANSPORT_FAILED;
649 return USB_STOR_TRANSPORT_ERROR;
651 return USB_STOR_TRANSPORT_ERROR;
654 #define USB_TRANSPORT_UNKNOWN_RETRY 5
655 #define USB_TRANSPORT_NOT_READY_RETRY 10
657 /* clear a stall on an endpoint - special for BBB devices */
658 int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
662 /* ENDPOINT_HALT = 0, so set value to 0 */
663 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
664 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
665 0, endpt, 0, 0, USB_CNTL_TIMEOUT * 5);
669 int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
673 int actlen, data_actlen;
674 unsigned int pipe, pipein, pipeout;
676 #ifdef BBB_XPORT_TRACE
681 dir_in = US_DIRECTION(srb->cmd[0]);
684 USB_STOR_PRINTF("COMMAND phase\n");
685 result = usb_stor_BBB_comdat(srb, us);
687 USB_STOR_PRINTF("failed to send CBW status %ld\n",
688 us->pusb_dev->status);
689 usb_stor_BBB_reset(us);
690 return USB_STOR_TRANSPORT_FAILED;
693 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
694 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
695 /* DATA phase + error handling */
697 /* no data, go immediately to the STATUS phase */
698 if (srb->datalen == 0)
700 USB_STOR_PRINTF("DATA phase\n");
705 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
706 &data_actlen, USB_CNTL_TIMEOUT * 5);
707 /* special handling of STALL in DATA phase */
708 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
709 USB_STOR_PRINTF("DATA:stall\n");
710 /* clear the STALL on the endpoint */
711 result = usb_stor_BBB_clear_endpt_stall(us,
712 dir_in ? us->ep_in : us->ep_out);
714 /* continue on to STATUS phase */
718 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
719 us->pusb_dev->status);
720 usb_stor_BBB_reset(us);
721 return USB_STOR_TRANSPORT_FAILED;
723 #ifdef BBB_XPORT_TRACE
724 for (index = 0; index < data_actlen; index++)
725 printf("pdata[%d] %#x ", index, srb->pdata[index]);
728 /* STATUS phase + error handling */
732 USB_STOR_PRINTF("STATUS phase\n");
733 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
734 &actlen, USB_CNTL_TIMEOUT*5);
736 /* special handling of STALL in STATUS phase */
737 if ((result < 0) && (retry < 1) &&
738 (us->pusb_dev->status & USB_ST_STALLED)) {
739 USB_STOR_PRINTF("STATUS:stall\n");
740 /* clear the STALL on the endpoint */
741 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
742 if (result >= 0 && (retry++ < 1))
747 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
748 us->pusb_dev->status);
749 usb_stor_BBB_reset(us);
750 return USB_STOR_TRANSPORT_FAILED;
752 #ifdef BBB_XPORT_TRACE
753 ptr = (unsigned char *)&csw;
754 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
755 printf("ptr[%d] %#x ", index, ptr[index]);
758 /* misuse pipe to get the residue */
759 pipe = le32_to_cpu(csw.dCSWDataResidue);
760 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
761 pipe = srb->datalen - data_actlen;
762 if (CSWSIGNATURE != le32_to_cpu(csw.dCSWSignature)) {
763 USB_STOR_PRINTF("!CSWSIGNATURE\n");
764 usb_stor_BBB_reset(us);
765 return USB_STOR_TRANSPORT_FAILED;
766 } else if ((CBWTag - 1) != le32_to_cpu(csw.dCSWTag)) {
767 USB_STOR_PRINTF("!Tag\n");
768 usb_stor_BBB_reset(us);
769 return USB_STOR_TRANSPORT_FAILED;
770 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
771 USB_STOR_PRINTF(">PHASE\n");
772 usb_stor_BBB_reset(us);
773 return USB_STOR_TRANSPORT_FAILED;
774 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
775 USB_STOR_PRINTF("=PHASE\n");
776 usb_stor_BBB_reset(us);
777 return USB_STOR_TRANSPORT_FAILED;
778 } else if (data_actlen > srb->datalen) {
779 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
780 data_actlen, srb->datalen);
781 return USB_STOR_TRANSPORT_FAILED;
782 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
783 USB_STOR_PRINTF("FAILED\n");
784 return USB_STOR_TRANSPORT_FAILED;
790 int usb_stor_CB_transport(ccb *srb, struct us_data *us)
798 status = USB_STOR_TRANSPORT_GOOD;
801 /* issue the command */
803 result = usb_stor_CB_comdat(srb, us);
804 USB_STOR_PRINTF("command / Data returned %d, status %X\n",
805 result, us->pusb_dev->status);
806 /* if this is an CBI Protocol, get IRQ */
807 if (us->protocol == US_PR_CBI) {
808 status = usb_stor_CBI_get_status(srb, us);
809 /* if the status is error, report it */
810 if (status == USB_STOR_TRANSPORT_ERROR) {
811 USB_STOR_PRINTF(" USB CBI Command Error\n");
814 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
815 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
817 /* if the status is good, report it */
818 if (status == USB_STOR_TRANSPORT_GOOD) {
819 USB_STOR_PRINTF(" USB CBI Command Good\n");
824 /* do we have to issue an auto request? */
825 /* HERE we have to check the result */
826 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
827 USB_STOR_PRINTF("ERROR %X\n", us->pusb_dev->status);
828 us->transport_reset(us);
829 return USB_STOR_TRANSPORT_ERROR;
831 if ((us->protocol == US_PR_CBI) &&
832 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
833 (srb->cmd[0] == SCSI_INQUIRY))) {
834 /* do not issue an autorequest after request sense */
835 USB_STOR_PRINTF("No auto request and good\n");
836 return USB_STOR_TRANSPORT_GOOD;
838 /* issue an request_sense */
839 memset(&psrb->cmd[0], 0, 12);
840 psrb->cmd[0] = SCSI_REQ_SENSE;
841 psrb->cmd[1] = srb->lun << 5;
844 psrb->pdata = &srb->sense_buf[0];
846 /* issue the command */
847 result = usb_stor_CB_comdat(psrb, us);
848 USB_STOR_PRINTF("auto request returned %d\n", result);
849 /* if this is an CBI Protocol, get IRQ */
850 if (us->protocol == US_PR_CBI)
851 status = usb_stor_CBI_get_status(psrb, us);
853 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
854 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",
855 us->pusb_dev->status);
856 return USB_STOR_TRANSPORT_ERROR;
858 USB_STOR_PRINTF("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
859 srb->sense_buf[0], srb->sense_buf[2],
860 srb->sense_buf[12], srb->sense_buf[13]);
861 /* Check the auto request result */
862 if ((srb->sense_buf[2] == 0) &&
863 (srb->sense_buf[12] == 0) &&
864 (srb->sense_buf[13] == 0)) {
866 return USB_STOR_TRANSPORT_GOOD;
869 /* Check the auto request result */
870 switch (srb->sense_buf[2]) {
872 /* Recovered Error */
873 return USB_STOR_TRANSPORT_GOOD;
877 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
878 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
879 " 0x%02X (NOT READY)\n", srb->cmd[0],
880 srb->sense_buf[0], srb->sense_buf[2],
881 srb->sense_buf[12], srb->sense_buf[13]);
882 return USB_STOR_TRANSPORT_FAILED;
889 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
890 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
891 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
892 srb->sense_buf[2], srb->sense_buf[12],
894 return USB_STOR_TRANSPORT_FAILED;
899 return USB_STOR_TRANSPORT_FAILED;
903 static int usb_inquiry(ccb *srb, struct us_data *ss)
908 memset(&srb->cmd[0], 0, 12);
909 srb->cmd[0] = SCSI_INQUIRY;
910 srb->cmd[1] = srb->lun << 5;
914 i = ss->transport(srb, ss);
915 USB_STOR_PRINTF("inquiry returns %d\n", i);
921 printf("error in inquiry\n");
927 static int usb_request_sense(ccb *srb, struct us_data *ss)
931 ptr = (char *)srb->pdata;
932 memset(&srb->cmd[0], 0, 12);
933 srb->cmd[0] = SCSI_REQ_SENSE;
934 srb->cmd[1] = srb->lun << 5;
937 srb->pdata = &srb->sense_buf[0];
939 ss->transport(srb, ss);
940 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",
941 srb->sense_buf[2], srb->sense_buf[12],
943 srb->pdata = (uchar *)ptr;
947 static int usb_test_unit_ready(ccb *srb, struct us_data *ss)
952 memset(&srb->cmd[0], 0, 12);
953 srb->cmd[0] = SCSI_TST_U_RDY;
954 srb->cmd[1] = srb->lun << 5;
957 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
959 usb_request_sense(srb, ss);
966 static int usb_read_capacity(ccb *srb, struct us_data *ss)
972 memset(&srb->cmd[0], 0, 12);
973 srb->cmd[0] = SCSI_RD_CAPAC;
974 srb->cmd[1] = srb->lun << 5;
977 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
984 static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start,
985 unsigned short blocks)
987 memset(&srb->cmd[0], 0, 12);
988 srb->cmd[0] = SCSI_READ10;
989 srb->cmd[1] = srb->lun << 5;
990 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
991 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
992 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
993 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
994 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
995 srb->cmd[8] = (unsigned char) blocks & 0xff;
997 USB_STOR_PRINTF("read10: start %lx blocks %x\n", start, blocks);
998 return ss->transport(srb, ss);
1001 static int usb_write_10(ccb *srb, struct us_data *ss, unsigned long start,
1002 unsigned short blocks)
1004 memset(&srb->cmd[0], 0, 12);
1005 srb->cmd[0] = SCSI_WRITE10;
1006 srb->cmd[1] = srb->lun << 5;
1007 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1008 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1009 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1010 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1011 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1012 srb->cmd[8] = (unsigned char) blocks & 0xff;
1014 USB_STOR_PRINTF("write10: start %lx blocks %x\n", start, blocks);
1015 return ss->transport(srb, ss);
1019 #ifdef CONFIG_USB_BIN_FIXUP
1021 * Some USB storage devices queried for SCSI identification data respond with
1022 * binary strings, which if output to the console freeze the terminal. The
1023 * workaround is to modify the vendor and product strings read from such
1024 * device with proper values (as reported by 'usb info').
1026 * Vendor and product length limits are taken from the definition of
1027 * block_dev_desc_t in include/part.h.
1029 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
1030 unsigned char vendor[],
1031 unsigned char product[]) {
1032 const unsigned char max_vendor_len = 40;
1033 const unsigned char max_product_len = 20;
1034 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
1035 strncpy((char *)vendor, "SMSC", max_vendor_len);
1036 strncpy((char *)product, "Flash Media Cntrller",
1040 #endif /* CONFIG_USB_BIN_FIXUP */
1042 #define USB_MAX_READ_BLK 20
1044 unsigned long usb_stor_read(int device, unsigned long blknr,
1045 unsigned long blkcnt, void *buffer)
1047 unsigned long start, blks, buf_addr;
1048 unsigned short smallblks;
1049 struct usb_device *dev;
1051 ccb *srb = &usb_ccb;
1058 USB_STOR_PRINTF("\nusb_read: dev %d \n", device);
1060 for (i = 0; i < USB_MAX_DEVICE; i++) {
1061 dev = usb_get_dev_index(i);
1064 if (dev->devnum == usb_dev_desc[device].target)
1068 usb_disable_asynch(1); /* asynch transfer not allowed */
1069 srb->lun = usb_dev_desc[device].lun;
1070 buf_addr = (unsigned long)buffer;
1073 if (usb_test_unit_ready(srb, (struct us_data *)dev->privptr)) {
1074 printf("Device NOT ready\n Request Sense returned %02X %02X"
1075 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1076 srb->sense_buf[13]);
1080 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx"
1081 " buffer %lx\n", device, start, blks, buf_addr);
1084 /* XXX need some comment here */
1086 srb->pdata = (unsigned char *)buf_addr;
1087 if (blks > USB_MAX_READ_BLK)
1088 smallblks = USB_MAX_READ_BLK;
1090 smallblks = (unsigned short) blks;
1092 if (smallblks == USB_MAX_READ_BLK)
1093 usb_show_progress();
1094 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1095 srb->pdata = (unsigned char *)buf_addr;
1096 if (usb_read_10(srb, (struct us_data *)dev->privptr, start,
1098 USB_STOR_PRINTF("Read ERROR\n");
1099 usb_request_sense(srb, (struct us_data *)dev->privptr);
1107 buf_addr += srb->datalen;
1108 } while (blks != 0);
1110 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",
1111 start, smallblks, buf_addr);
1113 usb_disable_asynch(0); /* asynch transfer allowed */
1114 if (blkcnt >= USB_MAX_READ_BLK)
1119 #define USB_MAX_WRITE_BLK 20
1121 unsigned long usb_stor_write(int device, unsigned long blknr,
1122 unsigned long blkcnt, const void *buffer)
1124 unsigned long start, blks, buf_addr;
1125 unsigned short smallblks;
1126 struct usb_device *dev;
1128 ccb *srb = &usb_ccb;
1135 USB_STOR_PRINTF("\nusb_write: dev %d \n", device);
1137 for (i = 0; i < USB_MAX_DEVICE; i++) {
1138 dev = usb_get_dev_index(i);
1141 if (dev->devnum == usb_dev_desc[device].target)
1145 usb_disable_asynch(1); /* asynch transfer not allowed */
1147 srb->lun = usb_dev_desc[device].lun;
1148 buf_addr = (unsigned long)buffer;
1151 if (usb_test_unit_ready(srb, (struct us_data *)dev->privptr)) {
1152 printf("Device NOT ready\n Request Sense returned %02X %02X"
1153 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1154 srb->sense_buf[13]);
1158 USB_STOR_PRINTF("\nusb_write: dev %d startblk %lx, blccnt %lx"
1159 " buffer %lx\n", device, start, blks, buf_addr);
1162 /* If write fails retry for max retry count else
1163 * return with number of blocks written successfully.
1166 srb->pdata = (unsigned char *)buf_addr;
1167 if (blks > USB_MAX_WRITE_BLK)
1168 smallblks = USB_MAX_WRITE_BLK;
1170 smallblks = (unsigned short) blks;
1172 if (smallblks == USB_MAX_WRITE_BLK)
1173 usb_show_progress();
1174 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1175 srb->pdata = (unsigned char *)buf_addr;
1176 if (usb_write_10(srb, (struct us_data *)dev->privptr, start,
1178 USB_STOR_PRINTF("Write ERROR\n");
1179 usb_request_sense(srb, (struct us_data *)dev->privptr);
1187 buf_addr += srb->datalen;
1188 } while (blks != 0);
1190 USB_STOR_PRINTF("usb_write: end startblk %lx, blccnt %x buffer %lx\n",
1191 start, smallblks, buf_addr);
1193 usb_disable_asynch(0); /* asynch transfer allowed */
1194 if (blkcnt >= USB_MAX_WRITE_BLK)
1200 /* Probe to see if a new device is actually a Storage device */
1201 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1204 struct usb_interface *iface;
1206 unsigned int flags = 0;
1211 /* let's examine the device now */
1212 iface = &dev->config.if_desc[ifnum];
1215 /* this is the place to patch some storage devices */
1216 USB_STOR_PRINTF("iVendor %X iProduct %X\n", dev->descriptor.idVendor,
1217 dev->descriptor.idProduct);
1219 if ((dev->descriptor.idVendor) == 0x066b &&
1220 (dev->descriptor.idProduct) == 0x0103) {
1221 USB_STOR_PRINTF("patched for E-USB\n");
1222 protocol = US_PR_CB;
1223 subclass = US_SC_UFI; /* an assumption */
1227 if (dev->descriptor.bDeviceClass != 0 ||
1228 iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1229 iface->desc.bInterfaceSubClass < US_SC_MIN ||
1230 iface->desc.bInterfaceSubClass > US_SC_MAX) {
1231 /* if it's not a mass storage, we go no further */
1235 memset(ss, 0, sizeof(struct us_data));
1237 /* At this point, we know we've got a live one */
1238 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1240 /* Initialize the us_data structure with some useful info */
1244 ss->attention_done = 0;
1246 /* If the device has subclass and protocol, then use that. Otherwise,
1247 * take data from the specific interface.
1250 ss->subclass = subclass;
1251 ss->protocol = protocol;
1253 ss->subclass = iface->desc.bInterfaceSubClass;
1254 ss->protocol = iface->desc.bInterfaceProtocol;
1257 /* set the handler pointers based on the protocol */
1258 USB_STOR_PRINTF("Transport: ");
1259 switch (ss->protocol) {
1261 USB_STOR_PRINTF("Control/Bulk\n");
1262 ss->transport = usb_stor_CB_transport;
1263 ss->transport_reset = usb_stor_CB_reset;
1267 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1268 ss->transport = usb_stor_CB_transport;
1269 ss->transport_reset = usb_stor_CB_reset;
1272 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1273 ss->transport = usb_stor_BBB_transport;
1274 ss->transport_reset = usb_stor_BBB_reset;
1277 printf("USB Storage Transport unknown / not yet implemented\n");
1283 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1284 * An optional interrupt is OK (necessary for CBI protocol).
1285 * We will ignore any others.
1287 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1288 /* is it an BULK endpoint? */
1289 if ((iface->ep_desc[i].bmAttributes &
1290 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1291 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1292 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1293 USB_ENDPOINT_NUMBER_MASK;
1296 iface->ep_desc[i].bEndpointAddress &
1297 USB_ENDPOINT_NUMBER_MASK;
1300 /* is it an interrupt endpoint? */
1301 if ((iface->ep_desc[i].bmAttributes &
1302 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1303 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1304 USB_ENDPOINT_NUMBER_MASK;
1305 ss->irqinterval = iface->ep_desc[i].bInterval;
1308 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1309 ss->ep_in, ss->ep_out, ss->ep_int);
1311 /* Do some basic sanity checks, and bail if we find a problem */
1312 if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
1313 !ss->ep_in || !ss->ep_out ||
1314 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1315 USB_STOR_PRINTF("Problems with device\n");
1318 /* set class specific stuff */
1319 /* We only handle certain protocols. Currently, these are
1321 * The SFF8070 accepts the requests used in u-boot
1323 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1324 ss->subclass != US_SC_8070) {
1325 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
1329 /* we had found an interrupt endpoint, prepare irq pipe
1330 * set up the IRQ pipe and handler
1332 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1333 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1334 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1335 dev->irq_handle = usb_stor_irq;
1337 dev->privptr = (void *)ss;
1341 int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1342 block_dev_desc_t *dev_desc)
1344 unsigned char perq, modi;
1345 unsigned long cap[2];
1346 unsigned long *capacity, *blksz;
1347 ccb *pccb = &usb_ccb;
1349 /* for some reasons a couple of devices would not survive this reset */
1352 (dev->descriptor.idVendor == 0x054c &&
1353 dev->descriptor.idProduct == 0x019e)
1355 /* USB007 Mini-USB2 Flash Drive */
1356 (dev->descriptor.idVendor == 0x066f &&
1357 dev->descriptor.idProduct == 0x2010)
1359 /* SanDisk Corporation Cruzer Micro 20044318410546613953 */
1360 (dev->descriptor.idVendor == 0x0781 &&
1361 dev->descriptor.idProduct == 0x5151)
1364 * SanDisk Corporation U3 Cruzer Micro 1/4GB
1365 * Flash Drive 000016244373FFB4
1367 (dev->descriptor.idVendor == 0x0781 &&
1368 dev->descriptor.idProduct == 0x5406)
1370 USB_STOR_PRINTF("usb_stor_get_info: skipping RESET..\n");
1372 ss->transport_reset(ss);
1374 pccb->pdata = usb_stor_buf;
1376 dev_desc->target = dev->devnum;
1377 pccb->lun = dev_desc->lun;
1378 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1380 if (usb_inquiry(pccb, ss))
1383 perq = usb_stor_buf[0];
1384 modi = usb_stor_buf[1];
1386 if ((perq & 0x1f) == 0x1f) {
1387 /* skip unknown devices */
1390 if ((modi&0x80) == 0x80) {
1391 /* drive is removable */
1392 dev_desc->removable = 1;
1394 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1395 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1396 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
1397 dev_desc->vendor[8] = 0;
1398 dev_desc->product[16] = 0;
1399 dev_desc->revision[4] = 0;
1400 #ifdef CONFIG_USB_BIN_FIXUP
1401 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1402 (uchar *)dev_desc->product);
1403 #endif /* CONFIG_USB_BIN_FIXUP */
1404 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1406 if (usb_test_unit_ready(pccb, ss)) {
1407 printf("Device NOT ready\n"
1408 " Request Sense returned %02X %02X %02X\n",
1409 pccb->sense_buf[2], pccb->sense_buf[12],
1410 pccb->sense_buf[13]);
1411 if (dev_desc->removable == 1) {
1412 dev_desc->type = perq;
1417 pccb->pdata = (unsigned char *)&cap[0];
1418 memset(pccb->pdata, 0, 8);
1419 if (usb_read_capacity(pccb, ss) != 0) {
1420 printf("READ_CAP ERROR\n");
1424 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0],
1427 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1430 cap[0] = cpu_to_be32(cap[0]);
1431 cap[1] = cpu_to_be32(cap[1]);
1433 /* this assumes bigendian! */
1437 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",
1439 dev_desc->lba = *capacity;
1440 dev_desc->blksz = *blksz;
1441 dev_desc->type = perq;
1442 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1443 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);
1445 init_part(dev_desc);
1447 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);