3 * Denis Peter, MPL AG Switzerland
5 * For BBB support (C) Copyright 2003
6 * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
8 * Most of this source has been derived from the Linux USB
9 * project. BBB support based on /sys/dev/usb/umass.c from
12 * See file CREDITS for list of people who contributed to this
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
33 * Currently only the CBI transport protocoll has been implemented, and it
34 * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
35 * transport protocoll may work as well.
39 * Support for USB Mass Storage Devices (BBB) has been added. It has
40 * only been tested with USB memory sticks.
41 * Nota bene: if you are using the BBB support with a little-endian
42 * CPU then you MUST define LITTLEENDIAN in the configuration file!
48 #include <asm/processor.h>
51 #if (CONFIG_COMMANDS & CFG_CMD_USB)
54 #ifdef CONFIG_USB_STORAGE
57 #undef BBB_COMDAT_TRACE
58 #undef BBB_XPORT_TRACE
61 #define USB_STOR_PRINTF(fmt,args...) printf (fmt ,##args)
63 #define USB_STOR_PRINTF(fmt,args...)
67 /* direction table -- this indicates the direction of the data
68 * transfer for each command code -- a 1 indicates input
70 unsigned char us_direction[256/8] = {
71 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
72 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
76 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
78 static unsigned char usb_stor_buf[512];
90 #define US_BBB_RESET 0xff
91 #define US_BBB_GET_MAX_LUN 0xfe
93 /* Command Block Wrapper */
96 # define CBWSIGNATURE 0x43425355
98 __u32 dCBWDataTransferLength;
100 # define CBWFLAGS_OUT 0x00
101 # define CBWFLAGS_IN 0x80
104 # define CBWCDBLENGTH 16
105 __u8 CBWCDB[CBWCDBLENGTH];
107 #define UMASS_BBB_CBW_SIZE 31
108 static __u32 CBWTag = 0;
110 /* Command Status Wrapper */
113 # define CSWSIGNATURE 0x53425355
115 __u32 dCSWDataResidue;
117 # define CSWSTATUS_GOOD 0x0
118 # define CSWSTATUS_FAILED 0x1
119 # define CSWSTATUS_PHASE 0x2
121 #define UMASS_BBB_CSW_SIZE 13
123 #define USB_MAX_STOR_DEV 5
124 static int usb_max_devs; /* number of highest available usb device */
126 static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
129 typedef int (*trans_cmnd)(ccb*, struct us_data*);
130 typedef int (*trans_reset)(struct us_data*);
133 struct usb_device *pusb_dev; /* this usb_device */
134 unsigned int flags; /* from filter initially */
135 unsigned char ifnum; /* interface number */
136 unsigned char ep_in; /* in endpoint */
137 unsigned char ep_out; /* out ....... */
138 unsigned char ep_int; /* interrupt . */
139 unsigned char subclass; /* as in overview */
140 unsigned char protocol; /* .............. */
141 unsigned char attention_done; /* force attn on first cmd */
142 unsigned short ip_data; /* interrupt data */
143 int action; /* what to do */
144 int ip_wanted; /* needed */
145 int *irq_handle; /* for USB int requests */
146 unsigned int irqpipe; /* pipe for release_irq */
147 unsigned char irqmaxp; /* max packed for irq Pipe */
148 unsigned char irqinterval; /* Intervall for IRQ Pipe */
149 ccb *srb; /* current srb */
150 trans_reset transport_reset; /* reset routine */
151 trans_cmnd transport; /* transport routine */
154 static struct us_data usb_stor[USB_MAX_STOR_DEV];
157 #define USB_STOR_TRANSPORT_GOOD 0
158 #define USB_STOR_TRANSPORT_FAILED -1
159 #define USB_STOR_TRANSPORT_ERROR -2
162 int usb_stor_get_info(struct usb_device *dev, struct us_data *us, block_dev_desc_t *dev_desc);
163 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss);
164 unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer);
165 struct usb_device * usb_get_dev_index(int index);
166 void uhci_show_temp_int_td(void);
168 block_dev_desc_t *usb_stor_get_dev(int index)
170 return &usb_dev_desc[index];
174 void usb_show_progress(void)
179 /*********************************************************************************
180 * (re)-scan the usb and reports device info
181 * to the user if mode = 1
182 * returns current device or -1 if no
184 int usb_stor_scan(int mode)
187 struct usb_device *dev;
190 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
193 printf(" scanning bus for storage devices...\n");
195 usb_disable_asynch(1); /* asynch transfer not allowed */
197 for(i=0;i<USB_MAX_STOR_DEV;i++) {
198 memset(&usb_dev_desc[i],0,sizeof(block_dev_desc_t));
199 usb_dev_desc[i].target=0xff;
200 usb_dev_desc[i].if_type=IF_TYPE_USB;
201 usb_dev_desc[i].dev=i;
202 usb_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
203 usb_dev_desc[i].block_read=usb_stor_read;
206 for(i=0;i<USB_MAX_DEVICE;i++) {
207 dev=usb_get_dev_index(i); /* get device */
208 USB_STOR_PRINTF("i=%d\n",i);
210 break; /* no more devices avaiable */
212 if(usb_storage_probe(dev,0,&usb_stor[usb_max_devs])) { /* ok, it is a storage devices */
213 /* get info and fill it in */
215 if(usb_stor_get_info(dev, &usb_stor[usb_max_devs], &usb_dev_desc[usb_max_devs])) {
217 printf (" Device %d: ", usb_max_devs);
218 dev_print(&usb_dev_desc[usb_max_devs]);
221 } /* if get info ok */
222 } /* if storage device */
223 if(usb_max_devs==USB_MAX_STOR_DEV) {
224 printf("max USB Storage Device reached: %d stopping\n",usb_max_devs);
228 usb_disable_asynch(0); /* asynch transfer allowed */
235 static int usb_stor_irq(struct usb_device *dev)
238 us=(struct us_data *)dev->privptr;
247 #ifdef USB_STOR_DEBUG
249 static void usb_show_srb(ccb * pccb)
252 printf("SRB: len %d datalen 0x%lX\n ",pccb->cmdlen,pccb->datalen);
254 printf("%02X ",pccb->cmd[i]);
259 static void display_int_status(unsigned long tmp)
261 printf("Status: %s %s %s %s %s %s %s\n",
262 (tmp & USB_ST_ACTIVE) ? "Active" : "",
263 (tmp & USB_ST_STALLED) ? "Stalled" : "",
264 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
265 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
266 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
267 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
268 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
271 /***********************************************************************
272 * Data transfer routines
273 ***********************************************************************/
275 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
284 /* determine the maximum packet size for these transfers */
285 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
287 /* while we have data left to transfer */
290 /* calculate how long this will be -- maximum or a remainder */
291 this_xfer = length > max_size ? max_size : length;
294 /* setup the retry counter */
297 /* set up the transfer loop */
299 /* transfer the data */
300 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
301 (unsigned int)buf, this_xfer, 11 - maxtry);
302 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
303 this_xfer, &partial, USB_CNTL_TIMEOUT*5);
304 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
305 result, partial, this_xfer);
306 if(us->pusb_dev->status!=0) {
307 /* if we stall, we need to clear it before we go on */
308 #ifdef USB_STOR_DEBUG
309 display_int_status(us->pusb_dev->status);
311 if (us->pusb_dev->status & USB_ST_STALLED) {
312 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
313 stat = us->pusb_dev->status;
314 usb_clear_halt(us->pusb_dev, pipe);
315 us->pusb_dev->status=stat;
316 if(this_xfer == partial) {
317 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us->pusb_dev->status);
323 if (us->pusb_dev->status & USB_ST_NAK_REC) {
324 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
327 if(this_xfer == partial) {
328 USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us->pusb_dev->status);
331 /* if our try counter reaches 0, bail out */
332 USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us->pusb_dev->status,partial);
336 /* update to show what data was transferred */
337 this_xfer -= partial;
339 /* continue until this transfer is done */
340 } while ( this_xfer );
343 /* if we get here, we're done and successful */
347 static int usb_stor_BBB_reset(struct us_data *us)
353 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
355 * For Reset Recovery the host shall issue in the following order:
356 * a) a Bulk-Only Mass Storage Reset
357 * b) a Clear Feature HALT to the Bulk-In endpoint
358 * c) a Clear Feature HALT to the Bulk-Out endpoint
360 * This is done in 3 steps.
362 * If the reset doesn't succeed, the device should be port reset.
364 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
366 USB_STOR_PRINTF("BBB_reset\n");
367 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
368 US_BBB_RESET, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
369 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT*5);
370 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED))
372 USB_STOR_PRINTF("RESET:stall\n");
375 /* long wait for reset */
377 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n",result,us->pusb_dev->status);
378 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
379 result = usb_clear_halt(us->pusb_dev, pipe);
380 /* long wait for reset */
382 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",result,us->pusb_dev->status);
383 /* long wait for reset */
384 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
385 result = usb_clear_halt(us->pusb_dev, pipe);
387 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing OUT endpoint\n",result,us->pusb_dev->status);
388 USB_STOR_PRINTF("BBB_reset done\n");
392 /* FIXME: this reset function doesn't really reset the port, and it
393 * should. Actually it should probably do what it's doing here, and
394 * reset the port physically
396 static int usb_stor_CB_reset(struct us_data *us)
398 unsigned char cmd[12];
401 USB_STOR_PRINTF("CB_reset\n");
402 memset(cmd, 0xFF, sizeof(cmd));
403 cmd[0] = SCSI_SEND_DIAG;
405 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
406 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
407 0, us->ifnum, cmd, sizeof(cmd), USB_CNTL_TIMEOUT*5);
409 /* long wait for reset */
411 USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result,us->pusb_dev->status);
412 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
413 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
415 USB_STOR_PRINTF("CB_reset done\n");
420 * Set up the command for a BBB device. Note that the actual SCSI
421 * command is copied into cbw.CBWCDB.
423 int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
431 dir_in = US_DIRECTION(srb->cmd[0]);
433 #ifdef BBB_COMDAT_TRACE
434 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);
436 for(result = 0;result < srb->cmdlen;result++)
437 printf("cmd[%d] %#x ", result, srb->cmd[result]);
442 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
443 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
447 /* always OUT to the ep */
448 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
450 cbw.dCBWSignature = swap_32(CBWSIGNATURE);
451 cbw.dCBWTag = swap_32(CBWTag++);
452 cbw.dCBWDataTransferLength = swap_32(srb->datalen);
453 cbw.bCBWFlags = (dir_in? CBWFLAGS_IN : CBWFLAGS_OUT);
454 cbw.bCBWLUN = srb->lun;
455 cbw.bCDBLength = srb->cmdlen;
456 /* copy the command data into the CBW command data buffer */
458 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
459 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE, &actlen, USB_CNTL_TIMEOUT*5);
461 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
465 /* FIXME: we also need a CBI_command which sets up the completion
466 * interrupt, and waits for it
468 int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
473 unsigned long status;
476 dir_in=US_DIRECTION(srb->cmd[0]);
479 pipe=usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
481 pipe=usb_sndbulkpipe(us->pusb_dev, us->ep_out);
483 USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry);
484 #ifdef USB_STOR_DEBUG
487 /* let's send the command via the control pipe */
488 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
489 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
491 srb->cmd, srb->cmdlen, USB_CNTL_TIMEOUT*5);
492 USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result,us->pusb_dev->status);
493 /* check the return code for the command */
495 if(us->pusb_dev->status & USB_ST_STALLED) {
496 status=us->pusb_dev->status;
497 USB_STOR_PRINTF(" stall during command found, clear pipe\n");
498 usb_clear_halt(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0));
499 us->pusb_dev->status=status;
501 USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb->cmd[0],us->pusb_dev->status);
504 /* transfer the data payload for this command, if one exists*/
506 USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result,dir_in ? "IN" : "OUT",srb->datalen);
508 result = us_one_transfer(us, pipe, srb->pdata,srb->datalen);
509 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);
510 if(!(us->pusb_dev->status & USB_ST_NAK_REC))
512 } /* if (srb->datalen) */
522 int usb_stor_CBI_get_status (ccb * srb, struct us_data *us)
527 submit_int_msg (us->pusb_dev, us->irqpipe,
528 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
531 if ((volatile int *) us->ip_wanted == 0)
536 printf (" Did not get interrupt on CBI\n");
538 return USB_STOR_TRANSPORT_ERROR;
541 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
542 us->ip_data, us->pusb_dev->irq_act_len,
543 us->pusb_dev->irq_status);
544 /* UFI gives us ASC and ASCQ, like a request sense */
545 if (us->subclass == US_SC_UFI) {
546 if (srb->cmd[0] == SCSI_REQ_SENSE ||
547 srb->cmd[0] == SCSI_INQUIRY)
548 return USB_STOR_TRANSPORT_GOOD; /* Good */
549 else if (us->ip_data)
550 return USB_STOR_TRANSPORT_FAILED;
552 return USB_STOR_TRANSPORT_GOOD;
554 /* otherwise, we interpret the data normally */
555 switch (us->ip_data) {
557 return USB_STOR_TRANSPORT_GOOD;
559 return USB_STOR_TRANSPORT_FAILED;
561 return USB_STOR_TRANSPORT_ERROR;
563 return USB_STOR_TRANSPORT_ERROR;
566 #define USB_TRANSPORT_UNKNOWN_RETRY 5
567 #define USB_TRANSPORT_NOT_READY_RETRY 10
569 /* clear a stall on an endpoint - special for BBB devices */
570 int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
574 /* ENDPOINT_HALT = 0, so set value to 0 */
575 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
576 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
577 0, endpt, 0, 0, USB_CNTL_TIMEOUT*5);
581 int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
585 int actlen, data_actlen;
586 unsigned int pipe, pipein, pipeout;
588 #ifdef BBB_XPORT_TRACE
593 dir_in = US_DIRECTION(srb->cmd[0]);
596 USB_STOR_PRINTF("COMMAND phase\n");
597 result = usb_stor_BBB_comdat(srb, us);
599 USB_STOR_PRINTF("failed to send CBW status %ld\n",
600 us->pusb_dev->status);
601 usb_stor_BBB_reset(us);
602 return USB_STOR_TRANSPORT_FAILED;
605 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
606 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
607 /* DATA phase + error handling */
609 /* no data, go immediately to the STATUS phase */
610 if (srb->datalen == 0)
612 USB_STOR_PRINTF("DATA phase\n");
617 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen, &data_actlen, USB_CNTL_TIMEOUT*5);
618 /* special handling of STALL in DATA phase */
619 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
620 USB_STOR_PRINTF("DATA:stall\n");
621 /* clear the STALL on the endpoint */
622 result = usb_stor_BBB_clear_endpt_stall(us, dir_in? us->ep_in : us->ep_out);
624 /* continue on to STATUS phase */
628 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
629 us->pusb_dev->status);
630 usb_stor_BBB_reset(us);
631 return USB_STOR_TRANSPORT_FAILED;
633 #ifdef BBB_XPORT_TRACE
634 for (index = 0; index < data_actlen; index++)
635 printf("pdata[%d] %#x ", index, srb->pdata[index]);
638 /* STATUS phase + error handling */
642 USB_STOR_PRINTF("STATUS phase\n");
643 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE, &actlen, USB_CNTL_TIMEOUT*5);
644 /* special handling of STALL in STATUS phase */
645 if((result < 0) && (retry < 1) && (us->pusb_dev->status & USB_ST_STALLED)) {
646 USB_STOR_PRINTF("STATUS:stall\n");
647 /* clear the STALL on the endpoint */
648 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
649 if (result >= 0 && (retry++ < 1))
654 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
655 us->pusb_dev->status);
656 usb_stor_BBB_reset(us);
657 return USB_STOR_TRANSPORT_FAILED;
659 #ifdef BBB_XPORT_TRACE
660 ptr = (unsigned char *)&csw;
661 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
662 printf("ptr[%d] %#x ", index, ptr[index]);
665 /* misuse pipe to get the residue */
666 pipe = swap_32(csw.dCSWDataResidue);
667 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
668 pipe = srb->datalen - data_actlen;
669 if (CSWSIGNATURE != swap_32(csw.dCSWSignature)) {
670 USB_STOR_PRINTF("!CSWSIGNATURE\n");
671 usb_stor_BBB_reset(us);
672 return USB_STOR_TRANSPORT_FAILED;
673 } else if ((CBWTag - 1) != swap_32(csw.dCSWTag)) {
674 USB_STOR_PRINTF("!Tag\n");
675 usb_stor_BBB_reset(us);
676 return USB_STOR_TRANSPORT_FAILED;
677 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
678 USB_STOR_PRINTF(">PHASE\n");
679 usb_stor_BBB_reset(us);
680 return USB_STOR_TRANSPORT_FAILED;
681 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
682 USB_STOR_PRINTF("=PHASE\n");
683 usb_stor_BBB_reset(us);
684 return USB_STOR_TRANSPORT_FAILED;
685 } else if (data_actlen > srb->datalen) {
686 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
687 data_actlen, srb->datalen);
688 return USB_STOR_TRANSPORT_FAILED;
689 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
690 USB_STOR_PRINTF("FAILED\n");
691 return USB_STOR_TRANSPORT_FAILED;
697 int usb_stor_CB_transport(ccb *srb, struct us_data *us)
705 status=USB_STOR_TRANSPORT_GOOD;
708 /* issue the command */
710 result=usb_stor_CB_comdat(srb,us);
711 USB_STOR_PRINTF("command / Data returned %d, status %X\n",result,us->pusb_dev->status);
712 /* if this is an CBI Protocol, get IRQ */
713 if(us->protocol==US_PR_CBI) {
714 status=usb_stor_CBI_get_status(srb,us);
715 /* if the status is error, report it */
716 if(status==USB_STOR_TRANSPORT_ERROR) {
717 USB_STOR_PRINTF(" USB CBI Command Error\n");
720 srb->sense_buf[12]=(unsigned char)(us->ip_data>>8);
721 srb->sense_buf[13]=(unsigned char)(us->ip_data&0xff);
723 /* if the status is good, report it */
724 if(status==USB_STOR_TRANSPORT_GOOD) {
725 USB_STOR_PRINTF(" USB CBI Command Good\n");
730 /* do we have to issue an auto request? */
731 /* HERE we have to check the result */
732 if((result<0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
733 USB_STOR_PRINTF("ERROR %X\n",us->pusb_dev->status);
734 us->transport_reset(us);
735 return USB_STOR_TRANSPORT_ERROR;
737 if((us->protocol==US_PR_CBI) &&
738 ((srb->cmd[0]==SCSI_REQ_SENSE) ||
739 (srb->cmd[0]==SCSI_INQUIRY))) { /* do not issue an autorequest after request sense */
740 USB_STOR_PRINTF("No auto request and good\n");
741 return USB_STOR_TRANSPORT_GOOD;
743 /* issue an request_sense */
744 memset(&psrb->cmd[0],0,12);
745 psrb->cmd[0]=SCSI_REQ_SENSE;
746 psrb->cmd[1]=srb->lun<<5;
749 psrb->pdata=&srb->sense_buf[0];
751 /* issue the command */
752 result=usb_stor_CB_comdat(psrb,us);
753 USB_STOR_PRINTF("auto request returned %d\n",result);
754 /* if this is an CBI Protocol, get IRQ */
755 if(us->protocol==US_PR_CBI) {
756 status=usb_stor_CBI_get_status(psrb,us);
758 if((result<0)&&!(us->pusb_dev->status & USB_ST_STALLED)) {
759 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us->pusb_dev->status);
760 return USB_STOR_TRANSPORT_ERROR;
762 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]);
763 /* Check the auto request result */
764 if((srb->sense_buf[2]==0) &&
765 (srb->sense_buf[12]==0) &&
766 (srb->sense_buf[13]==0)) /* ok, no sense */
767 return USB_STOR_TRANSPORT_GOOD;
768 /* Check the auto request result */
769 switch(srb->sense_buf[2]) {
770 case 0x01: /* Recovered Error */
771 return USB_STOR_TRANSPORT_GOOD;
773 case 0x02: /* Not Ready */
774 if(notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
775 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
776 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
777 return USB_STOR_TRANSPORT_FAILED;
784 if(retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
785 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
786 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
787 return USB_STOR_TRANSPORT_FAILED;
793 return USB_STOR_TRANSPORT_FAILED;
797 static int usb_inquiry(ccb *srb,struct us_data *ss)
802 memset(&srb->cmd[0],0,12);
803 srb->cmd[0]=SCSI_INQUIRY;
804 srb->cmd[1]=srb->lun<<5;
808 i=ss->transport(srb,ss);
809 USB_STOR_PRINTF("inquiry returns %d\n",i);
815 printf("error in inquiry\n");
821 static int usb_request_sense(ccb *srb,struct us_data *ss)
826 memset(&srb->cmd[0],0,12);
827 srb->cmd[0]=SCSI_REQ_SENSE;
828 srb->cmd[1]=srb->lun<<5;
831 srb->pdata=&srb->sense_buf[0];
833 ss->transport(srb,ss);
834 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
839 static int usb_test_unit_ready(ccb *srb,struct us_data *ss)
844 memset(&srb->cmd[0],0,12);
845 srb->cmd[0]=SCSI_TST_U_RDY;
846 srb->cmd[1]=srb->lun<<5;
849 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
852 usb_request_sense (srb, ss);
859 static int usb_read_capacity(ccb *srb,struct us_data *ss)
862 retry=2; /* retries */
864 memset(&srb->cmd[0],0,12);
865 srb->cmd[0]=SCSI_RD_CAPAC;
866 srb->cmd[1]=srb->lun<<5;
869 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
877 static int usb_read_10(ccb *srb,struct us_data *ss, unsigned long start, unsigned short blocks)
879 memset(&srb->cmd[0],0,12);
880 srb->cmd[0]=SCSI_READ10;
881 srb->cmd[1]=srb->lun<<5;
882 srb->cmd[2]=((unsigned char) (start>>24))&0xff;
883 srb->cmd[3]=((unsigned char) (start>>16))&0xff;
884 srb->cmd[4]=((unsigned char) (start>>8))&0xff;
885 srb->cmd[5]=((unsigned char) (start))&0xff;
886 srb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
887 srb->cmd[8]=(unsigned char) blocks & 0xff;
889 USB_STOR_PRINTF("read10: start %lx blocks %x\n",start,blocks);
890 return ss->transport(srb,ss);
894 #define USB_MAX_READ_BLK 20
896 unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer)
898 unsigned long start,blks, buf_addr;
899 unsigned short smallblks;
900 struct usb_device *dev;
910 USB_STOR_PRINTF("\nusb_read: dev %d \n",device);
912 for(i=0;i<USB_MAX_DEVICE;i++) {
913 dev=usb_get_dev_index(i);
917 if(dev->devnum==usb_dev_desc[device].target)
921 usb_disable_asynch(1); /* asynch transfer not allowed */
922 srb->lun=usb_dev_desc[device].lun;
923 buf_addr=(unsigned long)buffer;
926 if(usb_test_unit_ready(srb,(struct us_data *)dev->privptr)) {
927 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
928 srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
931 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks, buf_addr);
934 srb->pdata=(unsigned char *)buf_addr;
935 if(blks>USB_MAX_READ_BLK) {
936 smallblks=USB_MAX_READ_BLK;
938 smallblks=(unsigned short) blks;
941 if(smallblks==USB_MAX_READ_BLK)
943 srb->datalen=usb_dev_desc[device].blksz * smallblks;
944 srb->pdata=(unsigned char *)buf_addr;
945 if(usb_read_10(srb,(struct us_data *)dev->privptr, start, smallblks)) {
946 USB_STOR_PRINTF("Read ERROR\n");
947 usb_request_sense(srb,(struct us_data *)dev->privptr);
955 buf_addr+=srb->datalen;
957 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
958 usb_disable_asynch(0); /* asynch transfer allowed */
959 if(blkcnt>=USB_MAX_READ_BLK)
965 /* Probe to see if a new device is actually a Storage device */
966 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss)
968 struct usb_interface_descriptor *iface;
970 unsigned int flags = 0;
976 memset(ss, 0, sizeof(struct us_data));
978 /* let's examine the device now */
979 iface = &dev->config.if_desc[ifnum];
982 /* this is the place to patch some storage devices */
983 USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
984 if ((dev->descriptor.idVendor) == 0x066b && (dev->descriptor.idProduct) == 0x0103) {
985 USB_STOR_PRINTF("patched for E-USB\n");
987 subclass = US_SC_UFI; /* an assumption */
991 if (dev->descriptor.bDeviceClass != 0 ||
992 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
993 iface->bInterfaceSubClass < US_SC_MIN ||
994 iface->bInterfaceSubClass > US_SC_MAX) {
995 /* if it's not a mass storage, we go no further */
999 /* At this point, we know we've got a live one */
1000 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1002 /* Initialize the us_data structure with some useful info */
1006 ss->attention_done = 0;
1008 /* If the device has subclass and protocol, then use that. Otherwise,
1009 * take data from the specific interface.
1012 ss->subclass = subclass;
1013 ss->protocol = protocol;
1015 ss->subclass = iface->bInterfaceSubClass;
1016 ss->protocol = iface->bInterfaceProtocol;
1019 /* set the handler pointers based on the protocol */
1020 USB_STOR_PRINTF("Transport: ");
1021 switch (ss->protocol) {
1023 USB_STOR_PRINTF("Control/Bulk\n");
1024 ss->transport = usb_stor_CB_transport;
1025 ss->transport_reset = usb_stor_CB_reset;
1029 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1030 ss->transport = usb_stor_CB_transport;
1031 ss->transport_reset = usb_stor_CB_reset;
1034 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1035 ss->transport = usb_stor_BBB_transport;
1036 ss->transport_reset = usb_stor_BBB_reset;
1039 printf("USB Storage Transport unknown / not yet implemented\n");
1045 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1046 * An optional interrupt is OK (necessary for CBI protocol).
1047 * We will ignore any others.
1049 for (i = 0; i < iface->bNumEndpoints; i++) {
1050 /* is it an BULK endpoint? */
1051 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1052 == USB_ENDPOINT_XFER_BULK) {
1053 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1054 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1055 USB_ENDPOINT_NUMBER_MASK;
1057 ss->ep_out = iface->ep_desc[i].bEndpointAddress &
1058 USB_ENDPOINT_NUMBER_MASK;
1061 /* is it an interrupt endpoint? */
1062 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1063 == USB_ENDPOINT_XFER_INT) {
1064 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1065 USB_ENDPOINT_NUMBER_MASK;
1066 ss->irqinterval = iface->ep_desc[i].bInterval;
1069 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1070 ss->ep_in, ss->ep_out, ss->ep_int);
1072 /* Do some basic sanity checks, and bail if we find a problem */
1073 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
1074 !ss->ep_in || !ss->ep_out ||
1075 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1076 USB_STOR_PRINTF("Problems with device\n");
1079 /* set class specific stuff */
1080 /* We only handle certain protocols. Currently, these are
1082 * The SFF8070 accepts the requests used in u-boot
1084 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1085 ss->subclass != US_SC_8070) {
1086 printf("Sorry, protocol %d not yet supported.\n",ss->subclass);
1089 if(ss->ep_int) { /* we had found an interrupt endpoint, prepare irq pipe */
1090 /* set up the IRQ pipe and handler */
1092 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1093 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1094 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1095 dev->irq_handle=usb_stor_irq;
1097 dev->privptr=(void *)ss;
1101 int usb_stor_get_info(struct usb_device *dev,struct us_data *ss,block_dev_desc_t *dev_desc)
1103 unsigned char perq,modi;
1104 unsigned long cap[2];
1105 unsigned long *capacity,*blksz;
1108 ss->transport_reset(ss);
1109 pccb->pdata=usb_stor_buf;
1111 dev_desc->target=dev->devnum;
1112 pccb->lun=dev_desc->lun;
1113 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1115 if(usb_inquiry(pccb,ss))
1117 perq=usb_stor_buf[0];
1118 modi=usb_stor_buf[1];
1119 if((perq & 0x1f)==0x1f) {
1120 return 0; /* skip unknown devices */
1122 if((modi&0x80)==0x80) {/* drive is removable */
1123 dev_desc->removable=1;
1125 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1126 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1127 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
1128 dev_desc->vendor[8]=0;
1129 dev_desc->product[16]=0;
1130 dev_desc->revision[4]=0;
1131 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf[2],usb_stor_buf[3]);
1132 if(usb_test_unit_ready(pccb,ss)) {
1133 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",pccb->sense_buf[2],pccb->sense_buf[12],pccb->sense_buf[13]);
1134 if(dev_desc->removable==1) {
1135 dev_desc->type=perq;
1141 pccb->pdata=(unsigned char *)&cap[0];
1142 memset(pccb->pdata,0,8);
1143 if(usb_read_capacity(pccb,ss)!=0) {
1144 printf("READ_CAP ERROR\n");
1148 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap[0],cap[1]);
1150 if(cap[0]>(0x200000 * 10)) /* greater than 10 GByte */
1154 cap[0] = ((unsigned long)(
1155 (((unsigned long)(cap[0]) & (unsigned long)0x000000ffUL) << 24) |
1156 (((unsigned long)(cap[0]) & (unsigned long)0x0000ff00UL) << 8) |
1157 (((unsigned long)(cap[0]) & (unsigned long)0x00ff0000UL) >> 8) |
1158 (((unsigned long)(cap[0]) & (unsigned long)0xff000000UL) >> 24) ));
1159 cap[1] = ((unsigned long)(
1160 (((unsigned long)(cap[1]) & (unsigned long)0x000000ffUL) << 24) |
1161 (((unsigned long)(cap[1]) & (unsigned long)0x0000ff00UL) << 8) |
1162 (((unsigned long)(cap[1]) & (unsigned long)0x00ff0000UL) >> 8) |
1163 (((unsigned long)(cap[1]) & (unsigned long)0xff000000UL) >> 24) ));
1165 /* this assumes bigendian! */
1169 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity,*blksz);
1170 dev_desc->lba=*capacity;
1171 dev_desc->blksz=*blksz;
1172 dev_desc->type=perq;
1173 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1174 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1176 init_part(dev_desc);
1178 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1182 #endif /* CONFIG_USB_STORAGE */
1183 #endif /* CFG_CMD_USB */