2 * Most of this source has been derived from the Linux USB
4 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
6 * (c) 1999 Michael Gee (michael@linuxspecific.com)
7 * (c) 2000 Yggdrasil Computing, Inc.
11 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
13 * For BBB support (C) Copyright 2003
14 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
16 * BBB support based on /sys/dev/usb/umass.c from
19 * See file CREDITS for list of people who contributed to this
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
40 * Currently only the CBI transport protocoll has been implemented, and it
41 * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
42 * transport protocoll may work as well.
46 * Support for USB Mass Storage Devices (BBB) has been added. It has
47 * only been tested with USB memory sticks.
53 #include <asm/byteorder.h>
54 #include <asm/processor.h>
59 #undef BBB_COMDAT_TRACE
60 #undef BBB_XPORT_TRACE
63 #define USB_BLK_DEBUG 1
65 #define USB_BLK_DEBUG 0
68 #define USB_STOR_PRINTF(fmt, args...) debug_cond(USB_BLK_DEBUG, fmt, ##args)
71 /* direction table -- this indicates the direction of the data
72 * transfer for each command code -- a 1 indicates input
74 static const unsigned char us_direction[256/8] = {
75 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
76 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
80 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
82 static ccb usb_ccb __attribute__((aligned(ARCH_DMA_MINALIGN)));
93 #define US_BBB_RESET 0xff
94 #define US_BBB_GET_MAX_LUN 0xfe
96 /* Command Block Wrapper */
99 # define CBWSIGNATURE 0x43425355
101 __u32 dCBWDataTransferLength;
103 # define CBWFLAGS_OUT 0x00
104 # define CBWFLAGS_IN 0x80
107 # define CBWCDBLENGTH 16
108 __u8 CBWCDB[CBWCDBLENGTH];
110 #define UMASS_BBB_CBW_SIZE 31
113 /* Command Status Wrapper */
116 # define CSWSIGNATURE 0x53425355
118 __u32 dCSWDataResidue;
120 # define CSWSTATUS_GOOD 0x0
121 # define CSWSTATUS_FAILED 0x1
122 # define CSWSTATUS_PHASE 0x2
124 #define UMASS_BBB_CSW_SIZE 13
126 #define USB_MAX_STOR_DEV 5
127 static int usb_max_devs; /* number of highest available usb device */
129 static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
132 typedef int (*trans_cmnd)(ccb *cb, struct us_data *data);
133 typedef int (*trans_reset)(struct us_data *data);
136 struct usb_device *pusb_dev; /* this usb_device */
138 unsigned int flags; /* from filter initially */
139 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 unsigned long max_xfer_blk; /* Max blocks per xfer */
154 ccb *srb; /* current srb */
155 trans_reset transport_reset; /* reset routine */
156 trans_cmnd transport; /* transport routine */
159 static struct us_data usb_stor[USB_MAX_STOR_DEV];
162 #define USB_STOR_TRANSPORT_GOOD 0
163 #define USB_STOR_TRANSPORT_FAILED -1
164 #define USB_STOR_TRANSPORT_ERROR -2
166 int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
167 block_dev_desc_t *dev_desc);
168 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
170 unsigned long usb_stor_read(int device, unsigned long blknr,
171 unsigned long blkcnt, void *buffer);
172 unsigned long usb_stor_write(int device, unsigned long blknr,
173 unsigned long blkcnt, const void *buffer);
174 struct usb_device * usb_get_dev_index(int index);
175 void uhci_show_temp_int_td(void);
177 #ifdef CONFIG_PARTITIONS
178 block_dev_desc_t *usb_stor_get_dev(int index)
180 return (index < usb_max_devs) ? &usb_dev_desc[index] : NULL;
184 void usb_show_progress(void)
189 /*******************************************************************************
190 * show info on storage devices; 'usb start/init' must be invoked earlier
191 * as we only retrieve structures populated during devices initialization
193 int usb_stor_info(void)
197 if (usb_max_devs > 0) {
198 for (i = 0; i < usb_max_devs; i++) {
199 printf(" Device %d: ", i);
200 dev_print(&usb_dev_desc[i]);
205 printf("No storage devices, perhaps not 'usb start'ed..?\n");
209 static unsigned int usb_get_max_lun(struct us_data *us)
212 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
213 len = usb_control_msg(us->pusb_dev,
214 usb_rcvctrlpipe(us->pusb_dev, 0),
216 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
218 result, sizeof(char),
219 USB_CNTL_TIMEOUT * 5);
220 USB_STOR_PRINTF("Get Max LUN -> len = %i, result = %i\n",
222 return (len > 0) ? *result : 0;
225 /*******************************************************************************
226 * scan the usb and reports device info
227 * to the user if mode = 1
228 * returns current device or -1 if no
230 int usb_stor_scan(int mode)
233 struct usb_device *dev;
236 printf(" scanning bus for storage devices... ");
238 usb_disable_asynch(1); /* asynch transfer not allowed */
240 for (i = 0; i < USB_MAX_STOR_DEV; i++) {
241 memset(&usb_dev_desc[i], 0, sizeof(block_dev_desc_t));
242 usb_dev_desc[i].if_type = IF_TYPE_USB;
243 usb_dev_desc[i].dev = i;
244 usb_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
245 usb_dev_desc[i].target = 0xff;
246 usb_dev_desc[i].type = DEV_TYPE_UNKNOWN;
247 usb_dev_desc[i].block_read = usb_stor_read;
248 usb_dev_desc[i].block_write = usb_stor_write;
252 for (i = 0; i < USB_MAX_DEVICE; i++) {
253 dev = usb_get_dev_index(i); /* get device */
254 USB_STOR_PRINTF("i=%d\n", i);
256 break; /* no more devices available */
258 if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
259 /* OK, it's a storage device. Iterate over its LUNs
260 * and populate `usb_dev_desc'.
262 int lun, max_lun, start = usb_max_devs;
264 max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
266 lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
268 usb_dev_desc[usb_max_devs].lun = lun;
269 if (usb_stor_get_info(dev, &usb_stor[start],
270 &usb_dev_desc[usb_max_devs]) == 1) {
275 /* if storage device */
276 if (usb_max_devs == USB_MAX_STOR_DEV) {
277 printf("max USB Storage Device reached: %d stopping\n",
283 usb_disable_asynch(0); /* asynch transfer allowed */
284 printf("%d Storage Device(s) found\n", usb_max_devs);
285 if (usb_max_devs > 0)
290 static int usb_stor_irq(struct usb_device *dev)
293 us = (struct us_data *)dev->privptr;
301 #ifdef USB_STOR_DEBUG
303 static void usb_show_srb(ccb *pccb)
306 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
307 for (i = 0; i < 12; i++)
308 printf("%02X ", pccb->cmd[i]);
312 static void display_int_status(unsigned long tmp)
314 printf("Status: %s %s %s %s %s %s %s\n",
315 (tmp & USB_ST_ACTIVE) ? "Active" : "",
316 (tmp & USB_ST_STALLED) ? "Stalled" : "",
317 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
318 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
319 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
320 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
321 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
324 /***********************************************************************
325 * Data transfer routines
326 ***********************************************************************/
328 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
337 /* determine the maximum packet size for these transfers */
338 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
340 /* while we have data left to transfer */
343 /* calculate how long this will be -- maximum or a remainder */
344 this_xfer = length > max_size ? max_size : length;
347 /* setup the retry counter */
350 /* set up the transfer loop */
352 /* transfer the data */
353 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
354 (unsigned int)buf, this_xfer, 11 - maxtry);
355 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
357 USB_CNTL_TIMEOUT * 5);
358 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
359 result, partial, this_xfer);
360 if (us->pusb_dev->status != 0) {
361 /* if we stall, we need to clear it before
364 #ifdef USB_STOR_DEBUG
365 display_int_status(us->pusb_dev->status);
367 if (us->pusb_dev->status & USB_ST_STALLED) {
368 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
369 stat = us->pusb_dev->status;
370 usb_clear_halt(us->pusb_dev, pipe);
371 us->pusb_dev->status = stat;
372 if (this_xfer == partial) {
373 USB_STOR_PRINTF("bulk transferred with error %lX, but data ok\n", us->pusb_dev->status);
379 if (us->pusb_dev->status & USB_ST_NAK_REC) {
380 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
383 USB_STOR_PRINTF("bulk transferred with error");
384 if (this_xfer == partial) {
385 USB_STOR_PRINTF(" %ld, but data ok\n",
386 us->pusb_dev->status);
389 /* if our try counter reaches 0, bail out */
390 USB_STOR_PRINTF(" %ld, data %d\n",
391 us->pusb_dev->status, partial);
395 /* update to show what data was transferred */
396 this_xfer -= partial;
398 /* continue until this transfer is done */
402 /* if we get here, we're done and successful */
406 static int usb_stor_BBB_reset(struct us_data *us)
412 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
414 * For Reset Recovery the host shall issue in the following order:
415 * a) a Bulk-Only Mass Storage Reset
416 * b) a Clear Feature HALT to the Bulk-In endpoint
417 * c) a Clear Feature HALT to the Bulk-Out endpoint
419 * This is done in 3 steps.
421 * If the reset doesn't succeed, the device should be port reset.
423 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
425 USB_STOR_PRINTF("BBB_reset\n");
426 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
428 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
429 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT * 5);
431 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
432 USB_STOR_PRINTF("RESET:stall\n");
436 /* long wait for reset */
438 USB_STOR_PRINTF("BBB_reset result %d: status %lX reset\n", result,
439 us->pusb_dev->status);
440 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
441 result = usb_clear_halt(us->pusb_dev, pipe);
442 /* long wait for reset */
444 USB_STOR_PRINTF("BBB_reset result %d: status %lX clearing IN endpoint\n",
445 result, us->pusb_dev->status);
446 /* long wait for reset */
447 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
448 result = usb_clear_halt(us->pusb_dev, pipe);
450 USB_STOR_PRINTF("BBB_reset result %d: status %lX"
451 " clearing OUT endpoint\n", result,
452 us->pusb_dev->status);
453 USB_STOR_PRINTF("BBB_reset done\n");
457 /* FIXME: this reset function doesn't really reset the port, and it
458 * should. Actually it should probably do what it's doing here, and
459 * reset the port physically
461 static int usb_stor_CB_reset(struct us_data *us)
463 unsigned char cmd[12];
466 USB_STOR_PRINTF("CB_reset\n");
467 memset(cmd, 0xff, sizeof(cmd));
468 cmd[0] = SCSI_SEND_DIAG;
470 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
472 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
473 0, us->ifnum, cmd, sizeof(cmd),
474 USB_CNTL_TIMEOUT * 5);
476 /* long wait for reset */
478 USB_STOR_PRINTF("CB_reset result %d: status %lX"
479 " clearing endpoint halt\n", result,
480 us->pusb_dev->status);
481 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
482 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
484 USB_STOR_PRINTF("CB_reset done\n");
489 * Set up the command for a BBB device. Note that the actual SCSI
490 * command is copied into cbw.CBWCDB.
492 int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
498 ALLOC_CACHE_ALIGN_BUFFER(umass_bbb_cbw_t, cbw, 1);
500 dir_in = US_DIRECTION(srb->cmd[0]);
502 #ifdef BBB_COMDAT_TRACE
503 printf("dir %d lun %d cmdlen %d cmd %p datalen %d pdata %p\n",
504 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
507 for (result = 0; result < srb->cmdlen; result++)
508 printf("cmd[%d] %#x ", result, srb->cmd[result]);
513 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
514 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
518 /* always OUT to the ep */
519 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
521 cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE);
522 cbw->dCBWTag = cpu_to_le32(CBWTag++);
523 cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen);
524 cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
525 cbw->bCBWLUN = srb->lun;
526 cbw->bCDBLength = srb->cmdlen;
527 /* copy the command data into the CBW command data buffer */
529 memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
530 result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
531 &actlen, USB_CNTL_TIMEOUT * 5);
533 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
537 /* FIXME: we also need a CBI_command which sets up the completion
538 * interrupt, and waits for it
540 int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
545 unsigned long status;
548 dir_in = US_DIRECTION(srb->cmd[0]);
551 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
553 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
556 USB_STOR_PRINTF("CBI gets a command: Try %d\n", 5 - retry);
557 #ifdef USB_STOR_DEBUG
560 /* let's send the command via the control pipe */
561 result = usb_control_msg(us->pusb_dev,
562 usb_sndctrlpipe(us->pusb_dev , 0),
564 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
566 srb->cmd, srb->cmdlen,
567 USB_CNTL_TIMEOUT * 5);
568 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
569 " status %lX\n", result, us->pusb_dev->status);
570 /* check the return code for the command */
572 if (us->pusb_dev->status & USB_ST_STALLED) {
573 status = us->pusb_dev->status;
574 USB_STOR_PRINTF(" stall during command found,"
576 usb_clear_halt(us->pusb_dev,
577 usb_sndctrlpipe(us->pusb_dev, 0));
578 us->pusb_dev->status = status;
580 USB_STOR_PRINTF(" error during command %02X"
581 " Stat = %lX\n", srb->cmd[0],
582 us->pusb_dev->status);
585 /* transfer the data payload for this command, if one exists*/
587 USB_STOR_PRINTF("CB_transport: control msg returned %d,"
588 " direction is %s to go 0x%lx\n", result,
589 dir_in ? "IN" : "OUT", srb->datalen);
591 result = us_one_transfer(us, pipe, (char *)srb->pdata,
593 USB_STOR_PRINTF("CBI attempted to transfer data,"
594 " result is %d status %lX, len %d\n",
595 result, us->pusb_dev->status,
596 us->pusb_dev->act_len);
597 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
599 } /* if (srb->datalen) */
609 int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
614 submit_int_msg(us->pusb_dev, us->irqpipe,
615 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
618 if ((volatile int *) us->ip_wanted == 0)
623 printf(" Did not get interrupt on CBI\n");
625 return USB_STOR_TRANSPORT_ERROR;
628 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
629 us->ip_data, us->pusb_dev->irq_act_len,
630 us->pusb_dev->irq_status);
631 /* UFI gives us ASC and ASCQ, like a request sense */
632 if (us->subclass == US_SC_UFI) {
633 if (srb->cmd[0] == SCSI_REQ_SENSE ||
634 srb->cmd[0] == SCSI_INQUIRY)
635 return USB_STOR_TRANSPORT_GOOD; /* Good */
636 else if (us->ip_data)
637 return USB_STOR_TRANSPORT_FAILED;
639 return USB_STOR_TRANSPORT_GOOD;
641 /* otherwise, we interpret the data normally */
642 switch (us->ip_data) {
644 return USB_STOR_TRANSPORT_GOOD;
646 return USB_STOR_TRANSPORT_FAILED;
648 return USB_STOR_TRANSPORT_ERROR;
650 return USB_STOR_TRANSPORT_ERROR;
653 #define USB_TRANSPORT_UNKNOWN_RETRY 5
654 #define USB_TRANSPORT_NOT_READY_RETRY 10
656 /* clear a stall on an endpoint - special for BBB devices */
657 int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
661 /* ENDPOINT_HALT = 0, so set value to 0 */
662 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
663 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
664 0, endpt, 0, 0, USB_CNTL_TIMEOUT * 5);
668 int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
672 int actlen, data_actlen;
673 unsigned int pipe, pipein, pipeout;
674 ALLOC_CACHE_ALIGN_BUFFER(umass_bbb_csw_t, csw, 1);
675 #ifdef BBB_XPORT_TRACE
680 dir_in = US_DIRECTION(srb->cmd[0]);
683 USB_STOR_PRINTF("COMMAND phase\n");
684 result = usb_stor_BBB_comdat(srb, us);
686 USB_STOR_PRINTF("failed to send CBW status %ld\n",
687 us->pusb_dev->status);
688 usb_stor_BBB_reset(us);
689 return USB_STOR_TRANSPORT_FAILED;
692 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
693 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
694 /* DATA phase + error handling */
696 /* no data, go immediately to the STATUS phase */
697 if (srb->datalen == 0)
699 USB_STOR_PRINTF("DATA phase\n");
704 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
705 &data_actlen, USB_CNTL_TIMEOUT * 5);
706 /* special handling of STALL in DATA phase */
707 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
708 USB_STOR_PRINTF("DATA:stall\n");
709 /* clear the STALL on the endpoint */
710 result = usb_stor_BBB_clear_endpt_stall(us,
711 dir_in ? us->ep_in : us->ep_out);
713 /* continue on to STATUS phase */
717 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
718 us->pusb_dev->status);
719 usb_stor_BBB_reset(us);
720 return USB_STOR_TRANSPORT_FAILED;
722 #ifdef BBB_XPORT_TRACE
723 for (index = 0; index < data_actlen; index++)
724 printf("pdata[%d] %#x ", index, srb->pdata[index]);
727 /* STATUS phase + error handling */
731 USB_STOR_PRINTF("STATUS phase\n");
732 result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE,
733 &actlen, USB_CNTL_TIMEOUT*5);
735 /* special handling of STALL in STATUS phase */
736 if ((result < 0) && (retry < 1) &&
737 (us->pusb_dev->status & USB_ST_STALLED)) {
738 USB_STOR_PRINTF("STATUS:stall\n");
739 /* clear the STALL on the endpoint */
740 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
741 if (result >= 0 && (retry++ < 1))
746 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
747 us->pusb_dev->status);
748 usb_stor_BBB_reset(us);
749 return USB_STOR_TRANSPORT_FAILED;
751 #ifdef BBB_XPORT_TRACE
752 ptr = (unsigned char *)csw;
753 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
754 printf("ptr[%d] %#x ", index, ptr[index]);
757 /* misuse pipe to get the residue */
758 pipe = le32_to_cpu(csw->dCSWDataResidue);
759 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
760 pipe = srb->datalen - data_actlen;
761 if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) {
762 USB_STOR_PRINTF("!CSWSIGNATURE\n");
763 usb_stor_BBB_reset(us);
764 return USB_STOR_TRANSPORT_FAILED;
765 } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) {
766 USB_STOR_PRINTF("!Tag\n");
767 usb_stor_BBB_reset(us);
768 return USB_STOR_TRANSPORT_FAILED;
769 } else if (csw->bCSWStatus > CSWSTATUS_PHASE) {
770 USB_STOR_PRINTF(">PHASE\n");
771 usb_stor_BBB_reset(us);
772 return USB_STOR_TRANSPORT_FAILED;
773 } else if (csw->bCSWStatus == CSWSTATUS_PHASE) {
774 USB_STOR_PRINTF("=PHASE\n");
775 usb_stor_BBB_reset(us);
776 return USB_STOR_TRANSPORT_FAILED;
777 } else if (data_actlen > srb->datalen) {
778 USB_STOR_PRINTF("transferred %dB instead of %ldB\n",
779 data_actlen, srb->datalen);
780 return USB_STOR_TRANSPORT_FAILED;
781 } else if (csw->bCSWStatus == CSWSTATUS_FAILED) {
782 USB_STOR_PRINTF("FAILED\n");
783 return USB_STOR_TRANSPORT_FAILED;
789 int usb_stor_CB_transport(ccb *srb, struct us_data *us)
797 status = USB_STOR_TRANSPORT_GOOD;
800 /* issue the command */
802 result = usb_stor_CB_comdat(srb, us);
803 USB_STOR_PRINTF("command / Data returned %d, status %lX\n",
804 result, us->pusb_dev->status);
805 /* if this is an CBI Protocol, get IRQ */
806 if (us->protocol == US_PR_CBI) {
807 status = usb_stor_CBI_get_status(srb, us);
808 /* if the status is error, report it */
809 if (status == USB_STOR_TRANSPORT_ERROR) {
810 USB_STOR_PRINTF(" USB CBI Command Error\n");
813 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
814 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
816 /* if the status is good, report it */
817 if (status == USB_STOR_TRANSPORT_GOOD) {
818 USB_STOR_PRINTF(" USB CBI Command Good\n");
823 /* do we have to issue an auto request? */
824 /* HERE we have to check the result */
825 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
826 USB_STOR_PRINTF("ERROR %lX\n", us->pusb_dev->status);
827 us->transport_reset(us);
828 return USB_STOR_TRANSPORT_ERROR;
830 if ((us->protocol == US_PR_CBI) &&
831 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
832 (srb->cmd[0] == SCSI_INQUIRY))) {
833 /* do not issue an autorequest after request sense */
834 USB_STOR_PRINTF("No auto request and good\n");
835 return USB_STOR_TRANSPORT_GOOD;
837 /* issue an request_sense */
838 memset(&psrb->cmd[0], 0, 12);
839 psrb->cmd[0] = SCSI_REQ_SENSE;
840 psrb->cmd[1] = srb->lun << 5;
843 psrb->pdata = &srb->sense_buf[0];
845 /* issue the command */
846 result = usb_stor_CB_comdat(psrb, us);
847 USB_STOR_PRINTF("auto request returned %d\n", result);
848 /* if this is an CBI Protocol, get IRQ */
849 if (us->protocol == US_PR_CBI)
850 status = usb_stor_CBI_get_status(psrb, us);
852 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
853 USB_STOR_PRINTF(" AUTO REQUEST ERROR %ld\n",
854 us->pusb_dev->status);
855 return USB_STOR_TRANSPORT_ERROR;
857 USB_STOR_PRINTF("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
858 srb->sense_buf[0], srb->sense_buf[2],
859 srb->sense_buf[12], srb->sense_buf[13]);
860 /* Check the auto request result */
861 if ((srb->sense_buf[2] == 0) &&
862 (srb->sense_buf[12] == 0) &&
863 (srb->sense_buf[13] == 0)) {
865 return USB_STOR_TRANSPORT_GOOD;
868 /* Check the auto request result */
869 switch (srb->sense_buf[2]) {
871 /* Recovered Error */
872 return USB_STOR_TRANSPORT_GOOD;
876 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
877 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
878 " 0x%02X (NOT READY)\n", srb->cmd[0],
879 srb->sense_buf[0], srb->sense_buf[2],
880 srb->sense_buf[12], srb->sense_buf[13]);
881 return USB_STOR_TRANSPORT_FAILED;
888 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
889 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
890 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
891 srb->sense_buf[2], srb->sense_buf[12],
893 return USB_STOR_TRANSPORT_FAILED;
898 return USB_STOR_TRANSPORT_FAILED;
902 static int usb_inquiry(ccb *srb, struct us_data *ss)
907 memset(&srb->cmd[0], 0, 12);
908 srb->cmd[0] = SCSI_INQUIRY;
909 srb->cmd[1] = srb->lun << 5;
913 i = ss->transport(srb, ss);
914 USB_STOR_PRINTF("inquiry returns %d\n", i);
920 printf("error in inquiry\n");
926 static int usb_request_sense(ccb *srb, struct us_data *ss)
930 ptr = (char *)srb->pdata;
931 memset(&srb->cmd[0], 0, 12);
932 srb->cmd[0] = SCSI_REQ_SENSE;
933 srb->cmd[1] = srb->lun << 5;
936 srb->pdata = &srb->sense_buf[0];
938 ss->transport(srb, ss);
939 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",
940 srb->sense_buf[2], srb->sense_buf[12],
942 srb->pdata = (uchar *)ptr;
946 static int usb_test_unit_ready(ccb *srb, struct us_data *ss)
951 memset(&srb->cmd[0], 0, 12);
952 srb->cmd[0] = SCSI_TST_U_RDY;
953 srb->cmd[1] = srb->lun << 5;
956 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
958 usb_request_sense(srb, ss);
965 static int usb_read_capacity(ccb *srb, struct us_data *ss)
971 memset(&srb->cmd[0], 0, 12);
972 srb->cmd[0] = SCSI_RD_CAPAC;
973 srb->cmd[1] = srb->lun << 5;
976 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
983 static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start,
984 unsigned short blocks)
986 memset(&srb->cmd[0], 0, 12);
987 srb->cmd[0] = SCSI_READ10;
988 srb->cmd[1] = srb->lun << 5;
989 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
990 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
991 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
992 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
993 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
994 srb->cmd[8] = (unsigned char) blocks & 0xff;
996 USB_STOR_PRINTF("read10: start %lx blocks %x\n", start, blocks);
997 return ss->transport(srb, ss);
1000 static int usb_write_10(ccb *srb, struct us_data *ss, unsigned long start,
1001 unsigned short blocks)
1003 memset(&srb->cmd[0], 0, 12);
1004 srb->cmd[0] = SCSI_WRITE10;
1005 srb->cmd[1] = srb->lun << 5;
1006 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1007 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1008 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1009 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1010 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1011 srb->cmd[8] = (unsigned char) blocks & 0xff;
1013 USB_STOR_PRINTF("write10: start %lx blocks %x\n", start, blocks);
1014 return ss->transport(srb, ss);
1018 #ifdef CONFIG_USB_BIN_FIXUP
1020 * Some USB storage devices queried for SCSI identification data respond with
1021 * binary strings, which if output to the console freeze the terminal. The
1022 * workaround is to modify the vendor and product strings read from such
1023 * device with proper values (as reported by 'usb info').
1025 * Vendor and product length limits are taken from the definition of
1026 * block_dev_desc_t in include/part.h.
1028 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
1029 unsigned char vendor[],
1030 unsigned char product[]) {
1031 const unsigned char max_vendor_len = 40;
1032 const unsigned char max_product_len = 20;
1033 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
1034 strncpy((char *)vendor, "SMSC", max_vendor_len);
1035 strncpy((char *)product, "Flash Media Cntrller",
1039 #endif /* CONFIG_USB_BIN_FIXUP */
1041 unsigned long usb_stor_read(int device, unsigned long blknr,
1042 unsigned long blkcnt, void *buffer)
1044 unsigned long start, blks, buf_addr;
1045 unsigned short smallblks;
1046 struct usb_device *dev;
1049 ccb *srb = &usb_ccb;
1056 USB_STOR_PRINTF("\nusb_read: dev %d \n", device);
1058 for (i = 0; i < USB_MAX_DEVICE; i++) {
1059 dev = usb_get_dev_index(i);
1062 if (dev->devnum == usb_dev_desc[device].target)
1065 ss = (struct us_data *)dev->privptr;
1067 usb_disable_asynch(1); /* asynch transfer not allowed */
1068 srb->lun = usb_dev_desc[device].lun;
1069 buf_addr = (unsigned long)buffer;
1072 if (usb_test_unit_ready(srb, ss)) {
1073 printf("Device NOT ready\n Request Sense returned %02X %02X"
1074 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1075 srb->sense_buf[13]);
1079 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx"
1080 " buffer %lx\n", device, start, blks, buf_addr);
1083 /* XXX need some comment here */
1085 srb->pdata = (unsigned char *)buf_addr;
1086 if (blks > ss->max_xfer_blk)
1087 smallblks = ss->max_xfer_blk;
1089 smallblks = (unsigned short) blks;
1091 if (smallblks == ss->max_xfer_blk)
1092 usb_show_progress();
1093 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1094 srb->pdata = (unsigned char *)buf_addr;
1095 if (usb_read_10(srb, ss, start, smallblks)) {
1096 USB_STOR_PRINTF("Read ERROR\n");
1097 usb_request_sense(srb, ss);
1105 buf_addr += srb->datalen;
1106 } while (blks != 0);
1108 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",
1109 start, smallblks, buf_addr);
1111 usb_disable_asynch(0); /* asynch transfer allowed */
1112 if (blkcnt >= ss->max_xfer_blk)
1117 unsigned long usb_stor_write(int device, unsigned long blknr,
1118 unsigned long blkcnt, const void *buffer)
1120 unsigned long start, blks, buf_addr;
1121 unsigned short smallblks;
1122 struct usb_device *dev;
1125 ccb *srb = &usb_ccb;
1132 USB_STOR_PRINTF("\nusb_write: dev %d \n", device);
1134 for (i = 0; i < USB_MAX_DEVICE; i++) {
1135 dev = usb_get_dev_index(i);
1138 if (dev->devnum == usb_dev_desc[device].target)
1141 ss = (struct us_data *)dev->privptr;
1143 usb_disable_asynch(1); /* asynch transfer not allowed */
1145 srb->lun = usb_dev_desc[device].lun;
1146 buf_addr = (unsigned long)buffer;
1149 if (usb_test_unit_ready(srb, ss)) {
1150 printf("Device NOT ready\n Request Sense returned %02X %02X"
1151 " %02X\n", srb->sense_buf[2], srb->sense_buf[12],
1152 srb->sense_buf[13]);
1156 USB_STOR_PRINTF("\nusb_write: dev %d startblk %lx, blccnt %lx"
1157 " buffer %lx\n", device, start, blks, buf_addr);
1160 /* If write fails retry for max retry count else
1161 * return with number of blocks written successfully.
1164 srb->pdata = (unsigned char *)buf_addr;
1165 if (blks > ss->max_xfer_blk)
1166 smallblks = ss->max_xfer_blk;
1168 smallblks = (unsigned short) blks;
1170 if (smallblks == ss->max_xfer_blk)
1171 usb_show_progress();
1172 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1173 srb->pdata = (unsigned char *)buf_addr;
1174 if (usb_write_10(srb, ss, start, smallblks)) {
1175 USB_STOR_PRINTF("Write ERROR\n");
1176 usb_request_sense(srb, ss);
1184 buf_addr += srb->datalen;
1185 } while (blks != 0);
1187 USB_STOR_PRINTF("usb_write: end startblk %lx, blccnt %x buffer %lx\n",
1188 start, smallblks, buf_addr);
1190 usb_disable_asynch(0); /* asynch transfer allowed */
1191 if (blkcnt >= ss->max_xfer_blk)
1197 /* Probe to see if a new device is actually a Storage device */
1198 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1201 struct usb_interface *iface;
1203 unsigned int flags = 0;
1208 /* let's examine the device now */
1209 iface = &dev->config.if_desc[ifnum];
1212 /* this is the place to patch some storage devices */
1213 USB_STOR_PRINTF("iVendor %X iProduct %X\n", dev->descriptor.idVendor,
1214 dev->descriptor.idProduct);
1216 if ((dev->descriptor.idVendor) == 0x066b &&
1217 (dev->descriptor.idProduct) == 0x0103) {
1218 USB_STOR_PRINTF("patched for E-USB\n");
1219 protocol = US_PR_CB;
1220 subclass = US_SC_UFI; /* an assumption */
1224 if (dev->descriptor.bDeviceClass != 0 ||
1225 iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1226 iface->desc.bInterfaceSubClass < US_SC_MIN ||
1227 iface->desc.bInterfaceSubClass > US_SC_MAX) {
1228 /* if it's not a mass storage, we go no further */
1232 memset(ss, 0, sizeof(struct us_data));
1234 /* At this point, we know we've got a live one */
1235 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1237 /* Initialize the us_data structure with some useful info */
1241 ss->attention_done = 0;
1243 /* If the device has subclass and protocol, then use that. Otherwise,
1244 * take data from the specific interface.
1247 ss->subclass = subclass;
1248 ss->protocol = protocol;
1250 ss->subclass = iface->desc.bInterfaceSubClass;
1251 ss->protocol = iface->desc.bInterfaceProtocol;
1254 /* set the handler pointers based on the protocol */
1255 USB_STOR_PRINTF("Transport: ");
1256 switch (ss->protocol) {
1258 USB_STOR_PRINTF("Control/Bulk\n");
1259 ss->transport = usb_stor_CB_transport;
1260 ss->transport_reset = usb_stor_CB_reset;
1264 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1265 ss->transport = usb_stor_CB_transport;
1266 ss->transport_reset = usb_stor_CB_reset;
1269 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1270 ss->transport = usb_stor_BBB_transport;
1271 ss->transport_reset = usb_stor_BBB_reset;
1274 printf("USB Storage Transport unknown / not yet implemented\n");
1280 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1281 * An optional interrupt is OK (necessary for CBI protocol).
1282 * We will ignore any others.
1284 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1285 /* is it an BULK endpoint? */
1286 if ((iface->ep_desc[i].bmAttributes &
1287 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1288 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1289 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1290 USB_ENDPOINT_NUMBER_MASK;
1293 iface->ep_desc[i].bEndpointAddress &
1294 USB_ENDPOINT_NUMBER_MASK;
1297 /* is it an interrupt endpoint? */
1298 if ((iface->ep_desc[i].bmAttributes &
1299 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1300 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1301 USB_ENDPOINT_NUMBER_MASK;
1302 ss->irqinterval = iface->ep_desc[i].bInterval;
1305 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1306 ss->ep_in, ss->ep_out, ss->ep_int);
1308 /* Do some basic sanity checks, and bail if we find a problem */
1309 if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
1310 !ss->ep_in || !ss->ep_out ||
1311 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1312 USB_STOR_PRINTF("Problems with device\n");
1315 /* set class specific stuff */
1316 /* We only handle certain protocols. Currently, these are
1318 * The SFF8070 accepts the requests used in u-boot
1320 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1321 ss->subclass != US_SC_8070) {
1322 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
1326 /* we had found an interrupt endpoint, prepare irq pipe
1327 * set up the IRQ pipe and handler
1329 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1330 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1331 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1332 dev->irq_handle = usb_stor_irq;
1334 dev->privptr = (void *)ss;
1338 int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1339 block_dev_desc_t *dev_desc)
1341 unsigned char perq, modi;
1342 ALLOC_CACHE_ALIGN_BUFFER(unsigned long, cap, 2);
1343 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, usb_stor_buf, 36);
1344 unsigned long *capacity, *blksz;
1345 ccb *pccb = &usb_ccb;
1347 pccb->pdata = usb_stor_buf;
1349 dev_desc->target = dev->devnum;
1350 pccb->lun = dev_desc->lun;
1351 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1353 if (usb_inquiry(pccb, ss))
1356 perq = usb_stor_buf[0];
1357 modi = usb_stor_buf[1];
1359 if ((perq & 0x1f) == 0x1f) {
1360 /* skip unknown devices */
1363 if ((modi&0x80) == 0x80) {
1364 /* drive is removable */
1365 dev_desc->removable = 1;
1367 memcpy(&dev_desc->vendor[0], (const void *) &usb_stor_buf[8], 8);
1368 memcpy(&dev_desc->product[0], (const void *) &usb_stor_buf[16], 16);
1369 memcpy(&dev_desc->revision[0], (const void *) &usb_stor_buf[32], 4);
1370 dev_desc->vendor[8] = 0;
1371 dev_desc->product[16] = 0;
1372 dev_desc->revision[4] = 0;
1373 #ifdef CONFIG_USB_BIN_FIXUP
1374 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1375 (uchar *)dev_desc->product);
1376 #endif /* CONFIG_USB_BIN_FIXUP */
1377 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1379 if (usb_test_unit_ready(pccb, ss)) {
1380 printf("Device NOT ready\n"
1381 " Request Sense returned %02X %02X %02X\n",
1382 pccb->sense_buf[2], pccb->sense_buf[12],
1383 pccb->sense_buf[13]);
1384 if (dev_desc->removable == 1) {
1385 dev_desc->type = perq;
1390 pccb->pdata = (unsigned char *)&cap[0];
1391 memset(pccb->pdata, 0, 8);
1392 if (usb_read_capacity(pccb, ss) != 0) {
1393 printf("READ_CAP ERROR\n");
1397 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0],
1400 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1403 cap[0] = cpu_to_be32(cap[0]);
1404 cap[1] = cpu_to_be32(cap[1]);
1406 /* this assumes bigendian! */
1410 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",
1412 dev_desc->lba = *capacity;
1413 dev_desc->blksz = *blksz;
1414 dev_desc->type = perq;
1415 USB_STOR_PRINTF(" address %d\n", dev_desc->target);
1416 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);
1419 * The U-Boot EHCI driver cannot handle more than 4096 * 5 bytes in a
1420 * transfer without running itself out of qt_buffers.
1422 ss->max_xfer_blk = (4096 * 5) / dev_desc->blksz;
1424 init_part(dev_desc);
1426 USB_STOR_PRINTF("partype: %d\n", dev_desc->part_type);