2 * Most of this source has been derived from the Linux USB
4 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
6 * (c) 1999 Michael Gee (michael@linuxspecific.com)
7 * (c) 2000 Yggdrasil Computing, Inc.
11 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
13 * For BBB support (C) Copyright 2003
14 * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
16 * BBB support based on /sys/dev/usb/umass.c from
19 * See file CREDITS for list of people who contributed to this
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
40 * Currently only the CBI transport protocoll has been implemented, and it
41 * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
42 * transport protocoll may work as well.
46 * Support for USB Mass Storage Devices (BBB) has been added. It has
47 * only been tested with USB memory sticks.
48 * Nota bene: if you are using the BBB support with a little-endian
49 * CPU then you MUST define LITTLEENDIAN in the configuration file!
55 #include <asm/byteorder.h>
56 #include <asm/processor.h>
59 #if defined(CONFIG_CMD_USB)
63 #ifdef CONFIG_USB_STORAGE
66 #undef BBB_COMDAT_TRACE
67 #undef BBB_XPORT_TRACE
70 #define USB_STOR_PRINTF(fmt,args...) printf (fmt ,##args)
72 #define USB_STOR_PRINTF(fmt,args...)
76 /* direction table -- this indicates the direction of the data
77 * transfer for each command code -- a 1 indicates input
79 unsigned char us_direction[256/8] = {
80 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
81 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
85 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
87 static unsigned char usb_stor_buf[512];
99 #define US_BBB_RESET 0xff
100 #define US_BBB_GET_MAX_LUN 0xfe
102 /* Command Block Wrapper */
105 # define CBWSIGNATURE 0x43425355
107 __u32 dCBWDataTransferLength;
109 # define CBWFLAGS_OUT 0x00
110 # define CBWFLAGS_IN 0x80
113 # define CBWCDBLENGTH 16
114 __u8 CBWCDB[CBWCDBLENGTH];
116 #define UMASS_BBB_CBW_SIZE 31
117 static __u32 CBWTag = 0;
119 /* Command Status Wrapper */
122 # define CSWSIGNATURE 0x53425355
124 __u32 dCSWDataResidue;
126 # define CSWSTATUS_GOOD 0x0
127 # define CSWSTATUS_FAILED 0x1
128 # define CSWSTATUS_PHASE 0x2
130 #define UMASS_BBB_CSW_SIZE 13
132 #define USB_MAX_STOR_DEV 5
133 static int usb_max_devs = 0; /* number of highest available usb device */
135 static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
138 typedef int (*trans_cmnd)(ccb*, struct us_data*);
139 typedef int (*trans_reset)(struct us_data*);
142 struct usb_device *pusb_dev; /* this usb_device */
143 unsigned int flags; /* from filter initially */
144 unsigned char ifnum; /* interface number */
145 unsigned char ep_in; /* in endpoint */
146 unsigned char ep_out; /* out ....... */
147 unsigned char ep_int; /* interrupt . */
148 unsigned char subclass; /* as in overview */
149 unsigned char protocol; /* .............. */
150 unsigned char attention_done; /* force attn on first cmd */
151 unsigned short ip_data; /* interrupt data */
152 int action; /* what to do */
153 int ip_wanted; /* needed */
154 int *irq_handle; /* for USB int requests */
155 unsigned int irqpipe; /* pipe for release_irq */
156 unsigned char irqmaxp; /* max packed for irq Pipe */
157 unsigned char irqinterval; /* Intervall for IRQ Pipe */
158 ccb *srb; /* current srb */
159 trans_reset transport_reset; /* reset routine */
160 trans_cmnd transport; /* transport routine */
163 static struct us_data usb_stor[USB_MAX_STOR_DEV];
166 #define USB_STOR_TRANSPORT_GOOD 0
167 #define USB_STOR_TRANSPORT_FAILED -1
168 #define USB_STOR_TRANSPORT_ERROR -2
171 int usb_stor_get_info(struct usb_device *dev, struct us_data *us, block_dev_desc_t *dev_desc);
172 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss);
173 unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, void *buffer);
174 struct usb_device * usb_get_dev_index(int index);
175 void uhci_show_temp_int_td(void);
177 block_dev_desc_t *usb_stor_get_dev(int index)
179 return (index < USB_MAX_STOR_DEV) ? &usb_dev_desc[index] : NULL;
183 void usb_show_progress(void)
188 /*********************************************************************************
189 * show info on storage devices; 'usb start/init' must be invoked earlier
190 * as we only retrieve structures populated during devices initialization
192 int usb_stor_info(void)
196 if (usb_max_devs > 0) {
197 for (i = 0; i < usb_max_devs; i++) {
198 printf (" Device %d: ", i);
199 dev_print(&usb_dev_desc[i]);
204 printf("No storage devices, perhaps not 'usb start'ed..?\n");
208 /*********************************************************************************
209 * scan the usb and reports device info
210 * to the user if mode = 1
211 * returns current device or -1 if no
213 int usb_stor_scan(int mode)
216 struct usb_device *dev;
219 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
222 printf(" scanning bus for storage devices... ");
224 usb_disable_asynch(1); /* asynch transfer not allowed */
226 for(i=0;i<USB_MAX_STOR_DEV;i++) {
227 memset(&usb_dev_desc[i],0,sizeof(block_dev_desc_t));
228 usb_dev_desc[i].target=0xff;
229 usb_dev_desc[i].if_type=IF_TYPE_USB;
230 usb_dev_desc[i].dev=i;
231 usb_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
232 usb_dev_desc[i].block_read=usb_stor_read;
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])) { /* ok, it is a storage devices */
243 /* get info and fill it in */
244 if(usb_stor_get_info(dev, &usb_stor[usb_max_devs], &usb_dev_desc[usb_max_devs]))
246 } /* if storage device */
247 if(usb_max_devs==USB_MAX_STOR_DEV) {
248 printf("max USB Storage Device reached: %d stopping\n",usb_max_devs);
253 usb_disable_asynch(0); /* asynch transfer allowed */
254 printf("%d Storage Device(s) found\n", usb_max_devs);
261 static int usb_stor_irq(struct usb_device *dev)
264 us=(struct us_data *)dev->privptr;
273 #ifdef USB_STOR_DEBUG
275 static void usb_show_srb(ccb * pccb)
278 printf("SRB: len %d datalen 0x%lX\n ",pccb->cmdlen,pccb->datalen);
280 printf("%02X ",pccb->cmd[i]);
285 static void display_int_status(unsigned long tmp)
287 printf("Status: %s %s %s %s %s %s %s\n",
288 (tmp & USB_ST_ACTIVE) ? "Active" : "",
289 (tmp & USB_ST_STALLED) ? "Stalled" : "",
290 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
291 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
292 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
293 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
294 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
297 /***********************************************************************
298 * Data transfer routines
299 ***********************************************************************/
301 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
310 /* determine the maximum packet size for these transfers */
311 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
313 /* while we have data left to transfer */
316 /* calculate how long this will be -- maximum or a remainder */
317 this_xfer = length > max_size ? max_size : length;
320 /* setup the retry counter */
323 /* set up the transfer loop */
325 /* transfer the data */
326 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
327 (unsigned int)buf, this_xfer, 11 - maxtry);
328 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
329 this_xfer, &partial, USB_CNTL_TIMEOUT*5);
330 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
331 result, partial, this_xfer);
332 if(us->pusb_dev->status!=0) {
333 /* if we stall, we need to clear it before we go on */
334 #ifdef USB_STOR_DEBUG
335 display_int_status(us->pusb_dev->status);
337 if (us->pusb_dev->status & USB_ST_STALLED) {
338 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
339 stat = us->pusb_dev->status;
340 usb_clear_halt(us->pusb_dev, pipe);
341 us->pusb_dev->status=stat;
342 if(this_xfer == partial) {
343 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us->pusb_dev->status);
349 if (us->pusb_dev->status & USB_ST_NAK_REC) {
350 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
353 if(this_xfer == partial) {
354 USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us->pusb_dev->status);
357 /* if our try counter reaches 0, bail out */
358 USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us->pusb_dev->status,partial);
362 /* update to show what data was transferred */
363 this_xfer -= partial;
365 /* continue until this transfer is done */
366 } while ( this_xfer );
369 /* if we get here, we're done and successful */
373 static int usb_stor_BBB_reset(struct us_data *us)
379 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
381 * For Reset Recovery the host shall issue in the following order:
382 * a) a Bulk-Only Mass Storage Reset
383 * b) a Clear Feature HALT to the Bulk-In endpoint
384 * c) a Clear Feature HALT to the Bulk-Out endpoint
386 * This is done in 3 steps.
388 * If the reset doesn't succeed, the device should be port reset.
390 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
392 USB_STOR_PRINTF("BBB_reset\n");
393 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
394 US_BBB_RESET, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
395 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT*5);
397 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED))
399 USB_STOR_PRINTF("RESET:stall\n");
403 /* long wait for reset */
405 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n",result,us->pusb_dev->status);
406 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
407 result = usb_clear_halt(us->pusb_dev, pipe);
408 /* long wait for reset */
410 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",result,us->pusb_dev->status);
411 /* long wait for reset */
412 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
413 result = usb_clear_halt(us->pusb_dev, pipe);
415 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing OUT endpoint\n",result,us->pusb_dev->status);
416 USB_STOR_PRINTF("BBB_reset done\n");
420 /* FIXME: this reset function doesn't really reset the port, and it
421 * should. Actually it should probably do what it's doing here, and
422 * reset the port physically
424 static int usb_stor_CB_reset(struct us_data *us)
426 unsigned char cmd[12];
429 USB_STOR_PRINTF("CB_reset\n");
430 memset(cmd, 0xFF, sizeof(cmd));
431 cmd[0] = SCSI_SEND_DIAG;
433 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
434 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
435 0, us->ifnum, cmd, sizeof(cmd), USB_CNTL_TIMEOUT*5);
437 /* long wait for reset */
439 USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result,us->pusb_dev->status);
440 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
441 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
443 USB_STOR_PRINTF("CB_reset done\n");
448 * Set up the command for a BBB device. Note that the actual SCSI
449 * command is copied into cbw.CBWCDB.
451 int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
459 dir_in = US_DIRECTION(srb->cmd[0]);
461 #ifdef BBB_COMDAT_TRACE
462 printf("dir %d lun %d cmdlen %d cmd %p datalen %d pdata %p\n", dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen, srb->pdata);
464 for(result = 0;result < srb->cmdlen;result++)
465 printf("cmd[%d] %#x ", result, srb->cmd[result]);
470 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
471 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
475 /* always OUT to the ep */
476 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
478 cbw.dCBWSignature = cpu_to_le32(CBWSIGNATURE);
479 cbw.dCBWTag = cpu_to_le32(CBWTag++);
480 cbw.dCBWDataTransferLength = cpu_to_le32(srb->datalen);
481 cbw.bCBWFlags = (dir_in? CBWFLAGS_IN : CBWFLAGS_OUT);
482 cbw.bCBWLUN = srb->lun;
483 cbw.bCDBLength = srb->cmdlen;
484 /* copy the command data into the CBW command data buffer */
486 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
487 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE, &actlen, USB_CNTL_TIMEOUT*5);
489 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
493 /* FIXME: we also need a CBI_command which sets up the completion
494 * interrupt, and waits for it
496 int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
501 unsigned long status;
504 dir_in=US_DIRECTION(srb->cmd[0]);
507 pipe=usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
509 pipe=usb_sndbulkpipe(us->pusb_dev, us->ep_out);
511 USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry);
512 #ifdef USB_STOR_DEBUG
515 /* let's send the command via the control pipe */
516 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
517 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
519 srb->cmd, srb->cmdlen, USB_CNTL_TIMEOUT*5);
520 USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result,us->pusb_dev->status);
521 /* check the return code for the command */
523 if(us->pusb_dev->status & USB_ST_STALLED) {
524 status=us->pusb_dev->status;
525 USB_STOR_PRINTF(" stall during command found, clear pipe\n");
526 usb_clear_halt(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0));
527 us->pusb_dev->status=status;
529 USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb->cmd[0],us->pusb_dev->status);
532 /* transfer the data payload for this command, if one exists*/
534 USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result,dir_in ? "IN" : "OUT",srb->datalen);
536 result = us_one_transfer(us, pipe, (char *)srb->pdata,srb->datalen);
537 USB_STOR_PRINTF("CBI attempted to transfer data, result is %d status %lX, len %d\n", result,us->pusb_dev->status,us->pusb_dev->act_len);
538 if(!(us->pusb_dev->status & USB_ST_NAK_REC))
540 } /* if (srb->datalen) */
550 int usb_stor_CBI_get_status (ccb * srb, struct us_data *us)
555 submit_int_msg (us->pusb_dev, us->irqpipe,
556 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
559 if ((volatile int *) us->ip_wanted == 0)
564 printf (" Did not get interrupt on CBI\n");
566 return USB_STOR_TRANSPORT_ERROR;
569 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
570 us->ip_data, us->pusb_dev->irq_act_len,
571 us->pusb_dev->irq_status);
572 /* UFI gives us ASC and ASCQ, like a request sense */
573 if (us->subclass == US_SC_UFI) {
574 if (srb->cmd[0] == SCSI_REQ_SENSE ||
575 srb->cmd[0] == SCSI_INQUIRY)
576 return USB_STOR_TRANSPORT_GOOD; /* Good */
577 else if (us->ip_data)
578 return USB_STOR_TRANSPORT_FAILED;
580 return USB_STOR_TRANSPORT_GOOD;
582 /* otherwise, we interpret the data normally */
583 switch (us->ip_data) {
585 return USB_STOR_TRANSPORT_GOOD;
587 return USB_STOR_TRANSPORT_FAILED;
589 return USB_STOR_TRANSPORT_ERROR;
591 return USB_STOR_TRANSPORT_ERROR;
594 #define USB_TRANSPORT_UNKNOWN_RETRY 5
595 #define USB_TRANSPORT_NOT_READY_RETRY 10
597 /* clear a stall on an endpoint - special for BBB devices */
598 int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
602 /* ENDPOINT_HALT = 0, so set value to 0 */
603 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
604 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
605 0, endpt, 0, 0, USB_CNTL_TIMEOUT*5);
609 int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
613 int actlen, data_actlen;
614 unsigned int pipe, pipein, pipeout;
616 #ifdef BBB_XPORT_TRACE
621 dir_in = US_DIRECTION(srb->cmd[0]);
624 USB_STOR_PRINTF("COMMAND phase\n");
625 result = usb_stor_BBB_comdat(srb, us);
627 USB_STOR_PRINTF("failed to send CBW status %ld\n",
628 us->pusb_dev->status);
629 usb_stor_BBB_reset(us);
630 return USB_STOR_TRANSPORT_FAILED;
633 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
634 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
635 /* DATA phase + error handling */
637 /* no data, go immediately to the STATUS phase */
638 if (srb->datalen == 0)
640 USB_STOR_PRINTF("DATA phase\n");
645 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen, &data_actlen, USB_CNTL_TIMEOUT*5);
646 /* special handling of STALL in DATA phase */
647 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
648 USB_STOR_PRINTF("DATA:stall\n");
649 /* clear the STALL on the endpoint */
650 result = usb_stor_BBB_clear_endpt_stall(us, dir_in? us->ep_in : us->ep_out);
652 /* continue on to STATUS phase */
656 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
657 us->pusb_dev->status);
658 usb_stor_BBB_reset(us);
659 return USB_STOR_TRANSPORT_FAILED;
661 #ifdef BBB_XPORT_TRACE
662 for (index = 0; index < data_actlen; index++)
663 printf("pdata[%d] %#x ", index, srb->pdata[index]);
666 /* STATUS phase + error handling */
670 USB_STOR_PRINTF("STATUS phase\n");
671 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
672 &actlen, USB_CNTL_TIMEOUT*5);
674 /* special handling of STALL in STATUS phase */
675 if((result < 0) && (retry < 1) && (us->pusb_dev->status & USB_ST_STALLED)) {
676 USB_STOR_PRINTF("STATUS:stall\n");
677 /* clear the STALL on the endpoint */
678 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
679 if (result >= 0 && (retry++ < 1))
684 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
685 us->pusb_dev->status);
686 usb_stor_BBB_reset(us);
687 return USB_STOR_TRANSPORT_FAILED;
689 #ifdef BBB_XPORT_TRACE
690 ptr = (unsigned char *)&csw;
691 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
692 printf("ptr[%d] %#x ", index, ptr[index]);
695 /* misuse pipe to get the residue */
696 pipe = le32_to_cpu(csw.dCSWDataResidue);
697 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
698 pipe = srb->datalen - data_actlen;
699 if (CSWSIGNATURE != le32_to_cpu(csw.dCSWSignature)) {
700 USB_STOR_PRINTF("!CSWSIGNATURE\n");
701 usb_stor_BBB_reset(us);
702 return USB_STOR_TRANSPORT_FAILED;
703 } else if ((CBWTag - 1) != le32_to_cpu(csw.dCSWTag)) {
704 USB_STOR_PRINTF("!Tag\n");
705 usb_stor_BBB_reset(us);
706 return USB_STOR_TRANSPORT_FAILED;
707 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
708 USB_STOR_PRINTF(">PHASE\n");
709 usb_stor_BBB_reset(us);
710 return USB_STOR_TRANSPORT_FAILED;
711 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
712 USB_STOR_PRINTF("=PHASE\n");
713 usb_stor_BBB_reset(us);
714 return USB_STOR_TRANSPORT_FAILED;
715 } else if (data_actlen > srb->datalen) {
716 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
717 data_actlen, srb->datalen);
718 return USB_STOR_TRANSPORT_FAILED;
719 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
720 USB_STOR_PRINTF("FAILED\n");
721 return USB_STOR_TRANSPORT_FAILED;
727 int usb_stor_CB_transport(ccb *srb, struct us_data *us)
735 status=USB_STOR_TRANSPORT_GOOD;
738 /* issue the command */
740 result=usb_stor_CB_comdat(srb,us);
741 USB_STOR_PRINTF("command / Data returned %d, status %X\n",result,us->pusb_dev->status);
742 /* if this is an CBI Protocol, get IRQ */
743 if(us->protocol==US_PR_CBI) {
744 status=usb_stor_CBI_get_status(srb,us);
745 /* if the status is error, report it */
746 if(status==USB_STOR_TRANSPORT_ERROR) {
747 USB_STOR_PRINTF(" USB CBI Command Error\n");
750 srb->sense_buf[12]=(unsigned char)(us->ip_data>>8);
751 srb->sense_buf[13]=(unsigned char)(us->ip_data&0xff);
753 /* if the status is good, report it */
754 if(status==USB_STOR_TRANSPORT_GOOD) {
755 USB_STOR_PRINTF(" USB CBI Command Good\n");
760 /* do we have to issue an auto request? */
761 /* HERE we have to check the result */
762 if((result<0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
763 USB_STOR_PRINTF("ERROR %X\n",us->pusb_dev->status);
764 us->transport_reset(us);
765 return USB_STOR_TRANSPORT_ERROR;
767 if((us->protocol==US_PR_CBI) &&
768 ((srb->cmd[0]==SCSI_REQ_SENSE) ||
769 (srb->cmd[0]==SCSI_INQUIRY))) { /* do not issue an autorequest after request sense */
770 USB_STOR_PRINTF("No auto request and good\n");
771 return USB_STOR_TRANSPORT_GOOD;
773 /* issue an request_sense */
774 memset(&psrb->cmd[0],0,12);
775 psrb->cmd[0]=SCSI_REQ_SENSE;
776 psrb->cmd[1]=srb->lun<<5;
779 psrb->pdata = &srb->sense_buf[0];
781 /* issue the command */
782 result=usb_stor_CB_comdat(psrb,us);
783 USB_STOR_PRINTF("auto request returned %d\n",result);
784 /* if this is an CBI Protocol, get IRQ */
785 if(us->protocol==US_PR_CBI) {
786 status=usb_stor_CBI_get_status(psrb,us);
788 if((result<0)&&!(us->pusb_dev->status & USB_ST_STALLED)) {
789 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us->pusb_dev->status);
790 return USB_STOR_TRANSPORT_ERROR;
792 USB_STOR_PRINTF("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
793 /* Check the auto request result */
794 if((srb->sense_buf[2]==0) &&
795 (srb->sense_buf[12]==0) &&
796 (srb->sense_buf[13]==0)) /* ok, no sense */
797 return USB_STOR_TRANSPORT_GOOD;
798 /* Check the auto request result */
799 switch(srb->sense_buf[2]) {
800 case 0x01: /* Recovered Error */
801 return USB_STOR_TRANSPORT_GOOD;
803 case 0x02: /* Not Ready */
804 if(notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
805 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
806 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
807 return USB_STOR_TRANSPORT_FAILED;
814 if(retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
815 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
816 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
817 return USB_STOR_TRANSPORT_FAILED;
823 return USB_STOR_TRANSPORT_FAILED;
827 static int usb_inquiry(ccb *srb,struct us_data *ss)
832 memset(&srb->cmd[0],0,12);
833 srb->cmd[0]=SCSI_INQUIRY;
834 srb->cmd[1]=srb->lun<<5;
838 i=ss->transport(srb,ss);
839 USB_STOR_PRINTF("inquiry returns %d\n",i);
845 printf("error in inquiry\n");
851 static int usb_request_sense(ccb *srb,struct us_data *ss)
855 ptr=(char *)srb->pdata;
856 memset(&srb->cmd[0],0,12);
857 srb->cmd[0]=SCSI_REQ_SENSE;
858 srb->cmd[1]=srb->lun<<5;
861 srb->pdata = &srb->sense_buf[0];
863 ss->transport(srb,ss);
864 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
865 srb->pdata=(uchar *)ptr;
869 static int usb_test_unit_ready(ccb *srb,struct us_data *ss)
874 memset(&srb->cmd[0],0,12);
875 srb->cmd[0]=SCSI_TST_U_RDY;
876 srb->cmd[1]=srb->lun<<5;
879 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
882 usb_request_sense (srb, ss);
889 static int usb_read_capacity(ccb *srb,struct us_data *ss)
892 retry = 3; /* retries */
894 memset(&srb->cmd[0],0,12);
895 srb->cmd[0]=SCSI_RD_CAPAC;
896 srb->cmd[1]=srb->lun<<5;
899 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
907 static int usb_read_10(ccb *srb,struct us_data *ss, unsigned long start, unsigned short blocks)
909 memset(&srb->cmd[0],0,12);
910 srb->cmd[0]=SCSI_READ10;
911 srb->cmd[1]=srb->lun<<5;
912 srb->cmd[2]=((unsigned char) (start>>24))&0xff;
913 srb->cmd[3]=((unsigned char) (start>>16))&0xff;
914 srb->cmd[4]=((unsigned char) (start>>8))&0xff;
915 srb->cmd[5]=((unsigned char) (start))&0xff;
916 srb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
917 srb->cmd[8]=(unsigned char) blocks & 0xff;
919 USB_STOR_PRINTF("read10: start %lx blocks %x\n",start,blocks);
920 return ss->transport(srb,ss);
924 #ifdef CONFIG_USB_BIN_FIXUP
926 * Some USB storage devices queried for SCSI identification data respond with
927 * binary strings, which if output to the console freeze the terminal. The
928 * workaround is to modify the vendor and product strings read from such
929 * device with proper values (as reported by 'usb info').
931 * Vendor and product length limits are taken from the definition of
932 * block_dev_desc_t in include/part.h.
934 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
935 unsigned char vendor[],
936 unsigned char product[]) {
937 const unsigned char max_vendor_len = 40;
938 const unsigned char max_product_len = 20;
939 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
940 strncpy ((char *)vendor, "SMSC", max_vendor_len);
941 strncpy ((char *)product, "Flash Media Cntrller", max_product_len);
944 #endif /* CONFIG_USB_BIN_FIXUP */
946 #define USB_MAX_READ_BLK 20
948 unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, void *buffer)
950 unsigned long start,blks, buf_addr;
951 unsigned short smallblks;
952 struct usb_device *dev;
962 USB_STOR_PRINTF("\nusb_read: dev %d \n",device);
964 for(i=0;i<USB_MAX_DEVICE;i++) {
965 dev=usb_get_dev_index(i);
969 if(dev->devnum==usb_dev_desc[device].target)
973 usb_disable_asynch(1); /* asynch transfer not allowed */
974 srb->lun=usb_dev_desc[device].lun;
975 buf_addr=(unsigned long)buffer;
978 if(usb_test_unit_ready(srb,(struct us_data *)dev->privptr)) {
979 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
980 srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
983 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks, buf_addr);
986 srb->pdata=(unsigned char *)buf_addr;
987 if(blks>USB_MAX_READ_BLK) {
988 smallblks=USB_MAX_READ_BLK;
990 smallblks=(unsigned short) blks;
993 if(smallblks==USB_MAX_READ_BLK)
995 srb->datalen=usb_dev_desc[device].blksz * smallblks;
996 srb->pdata=(unsigned char *)buf_addr;
997 if(usb_read_10(srb,(struct us_data *)dev->privptr, start, smallblks)) {
998 USB_STOR_PRINTF("Read ERROR\n");
999 usb_request_sense(srb,(struct us_data *)dev->privptr);
1007 buf_addr+=srb->datalen;
1009 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
1010 usb_disable_asynch(0); /* asynch transfer allowed */
1011 if(blkcnt>=USB_MAX_READ_BLK)
1017 /* Probe to see if a new device is actually a Storage device */
1018 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss)
1020 struct usb_interface_descriptor *iface;
1022 unsigned int flags = 0;
1027 /* let's examine the device now */
1028 iface = &dev->config.if_desc[ifnum];
1031 /* this is the place to patch some storage devices */
1032 USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
1033 if ((dev->descriptor.idVendor) == 0x066b && (dev->descriptor.idProduct) == 0x0103) {
1034 USB_STOR_PRINTF("patched for E-USB\n");
1035 protocol = US_PR_CB;
1036 subclass = US_SC_UFI; /* an assumption */
1040 if (dev->descriptor.bDeviceClass != 0 ||
1041 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1042 iface->bInterfaceSubClass < US_SC_MIN ||
1043 iface->bInterfaceSubClass > US_SC_MAX) {
1044 /* if it's not a mass storage, we go no further */
1048 memset(ss, 0, sizeof(struct us_data));
1050 /* At this point, we know we've got a live one */
1051 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1053 /* Initialize the us_data structure with some useful info */
1057 ss->attention_done = 0;
1059 /* If the device has subclass and protocol, then use that. Otherwise,
1060 * take data from the specific interface.
1063 ss->subclass = subclass;
1064 ss->protocol = protocol;
1066 ss->subclass = iface->bInterfaceSubClass;
1067 ss->protocol = iface->bInterfaceProtocol;
1070 /* set the handler pointers based on the protocol */
1071 USB_STOR_PRINTF("Transport: ");
1072 switch (ss->protocol) {
1074 USB_STOR_PRINTF("Control/Bulk\n");
1075 ss->transport = usb_stor_CB_transport;
1076 ss->transport_reset = usb_stor_CB_reset;
1080 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1081 ss->transport = usb_stor_CB_transport;
1082 ss->transport_reset = usb_stor_CB_reset;
1085 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1086 ss->transport = usb_stor_BBB_transport;
1087 ss->transport_reset = usb_stor_BBB_reset;
1090 printf("USB Storage Transport unknown / not yet implemented\n");
1096 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1097 * An optional interrupt is OK (necessary for CBI protocol).
1098 * We will ignore any others.
1100 for (i = 0; i < iface->bNumEndpoints; i++) {
1101 /* is it an BULK endpoint? */
1102 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1103 == USB_ENDPOINT_XFER_BULK) {
1104 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1105 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1106 USB_ENDPOINT_NUMBER_MASK;
1108 ss->ep_out = iface->ep_desc[i].bEndpointAddress &
1109 USB_ENDPOINT_NUMBER_MASK;
1112 /* is it an interrupt endpoint? */
1113 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1114 == USB_ENDPOINT_XFER_INT) {
1115 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1116 USB_ENDPOINT_NUMBER_MASK;
1117 ss->irqinterval = iface->ep_desc[i].bInterval;
1120 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1121 ss->ep_in, ss->ep_out, ss->ep_int);
1123 /* Do some basic sanity checks, and bail if we find a problem */
1124 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
1125 !ss->ep_in || !ss->ep_out ||
1126 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1127 USB_STOR_PRINTF("Problems with device\n");
1130 /* set class specific stuff */
1131 /* We only handle certain protocols. Currently, these are
1133 * The SFF8070 accepts the requests used in u-boot
1135 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1136 ss->subclass != US_SC_8070) {
1137 printf("Sorry, protocol %d not yet supported.\n",ss->subclass);
1140 if(ss->ep_int) { /* we had found an interrupt endpoint, prepare irq pipe */
1141 /* set up the IRQ pipe and handler */
1143 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1144 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1145 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1146 dev->irq_handle=usb_stor_irq;
1148 dev->privptr=(void *)ss;
1152 int usb_stor_get_info(struct usb_device *dev,struct us_data *ss,block_dev_desc_t *dev_desc)
1154 unsigned char perq,modi;
1155 unsigned long cap[2];
1156 unsigned long *capacity,*blksz;
1157 ccb *pccb = &usb_ccb;
1159 /* for some reasons a couple of devices would not survive this reset */
1162 (dev->descriptor.idVendor == 0x054c &&
1163 dev->descriptor.idProduct == 0x019e)
1166 /* USB007 Mini-USB2 Flash Drive */
1167 (dev->descriptor.idVendor == 0x066f &&
1168 dev->descriptor.idProduct == 0x2010)
1170 /* SanDisk Corporation Cruzer Micro 20044318410546613953 */
1171 (dev->descriptor.idVendor == 0x0781 &&
1172 dev->descriptor.idProduct == 0x5151)
1174 USB_STOR_PRINTF("usb_stor_get_info: skipping RESET..\n");
1176 ss->transport_reset(ss);
1178 pccb->pdata = usb_stor_buf;
1180 dev_desc->target = dev->devnum;
1181 pccb->lun = dev_desc->lun;
1182 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1184 if(usb_inquiry(pccb,ss))
1187 perq = usb_stor_buf[0];
1188 modi = usb_stor_buf[1];
1189 if((perq & 0x1f) == 0x1f) {
1190 return 0; /* skip unknown devices */
1192 if((modi&0x80) == 0x80) {/* drive is removable */
1193 dev_desc->removable = 1;
1195 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1196 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1197 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
1198 dev_desc->vendor[8] = 0;
1199 dev_desc->product[16] = 0;
1200 dev_desc->revision[4] = 0;
1201 #ifdef CONFIG_USB_BIN_FIXUP
1202 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor, (uchar *)dev_desc->product);
1203 #endif /* CONFIG_USB_BIN_FIXUP */
1204 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf[2],usb_stor_buf[3]);
1205 if(usb_test_unit_ready(pccb,ss)) {
1206 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",pccb->sense_buf[2],pccb->sense_buf[12],pccb->sense_buf[13]);
1207 if(dev_desc->removable == 1) {
1208 dev_desc->type = perq;
1214 pccb->pdata = (unsigned char *)&cap[0];
1215 memset(pccb->pdata,0,8);
1216 if(usb_read_capacity(pccb,ss) != 0) {
1217 printf("READ_CAP ERROR\n");
1221 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap[0],cap[1]);
1223 if(cap[0]>(0x200000 * 10)) /* greater than 10 GByte */
1226 cap[0] = cpu_to_be32(cap[0]);
1227 cap[1] = cpu_to_be32(cap[1]);
1229 /* this assumes bigendian! */
1233 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity,*blksz);
1234 dev_desc->lba = *capacity;
1235 dev_desc->blksz = *blksz;
1236 dev_desc->type = perq;
1237 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1238 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1240 init_part(dev_desc);
1242 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1246 #endif /* CONFIG_USB_STORAGE */