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 = 0; /* 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 * show info on storage devices; 'usb start/init' must be invoked earlier
181 * as we only retrieve structures populated during devices initialization
183 void usb_stor_info(void)
187 if (usb_max_devs > 0)
188 for (i = 0; i < usb_max_devs; i++) {
189 printf (" Device %d: ", i);
190 dev_print(&usb_dev_desc[i]);
193 printf("No storage devices, perhaps not 'usb start'ed..?\n");
196 /*********************************************************************************
197 * scan the usb and reports device info
198 * to the user if mode = 1
199 * returns current device or -1 if no
201 int usb_stor_scan(int mode)
204 struct usb_device *dev;
207 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
210 printf(" scanning bus for storage devices... ");
212 usb_disable_asynch(1); /* asynch transfer not allowed */
214 for(i=0;i<USB_MAX_STOR_DEV;i++) {
215 memset(&usb_dev_desc[i],0,sizeof(block_dev_desc_t));
216 usb_dev_desc[i].target=0xff;
217 usb_dev_desc[i].if_type=IF_TYPE_USB;
218 usb_dev_desc[i].dev=i;
219 usb_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
220 usb_dev_desc[i].block_read=usb_stor_read;
224 for(i=0;i<USB_MAX_DEVICE;i++) {
225 dev=usb_get_dev_index(i); /* get device */
226 USB_STOR_PRINTF("i=%d\n",i);
228 break; /* no more devices avaiable */
230 if(usb_storage_probe(dev,0,&usb_stor[usb_max_devs])) { /* ok, it is a storage devices */
231 /* get info and fill it in */
232 if(usb_stor_get_info(dev, &usb_stor[usb_max_devs], &usb_dev_desc[usb_max_devs]))
234 } /* if storage device */
235 if(usb_max_devs==USB_MAX_STOR_DEV) {
236 printf("max USB Storage Device reached: %d stopping\n",usb_max_devs);
241 usb_disable_asynch(0); /* asynch transfer allowed */
242 printf("%d Storage Device(s) found\n", usb_max_devs);
249 static int usb_stor_irq(struct usb_device *dev)
252 us=(struct us_data *)dev->privptr;
261 #ifdef USB_STOR_DEBUG
263 static void usb_show_srb(ccb * pccb)
266 printf("SRB: len %d datalen 0x%lX\n ",pccb->cmdlen,pccb->datalen);
268 printf("%02X ",pccb->cmd[i]);
273 static void display_int_status(unsigned long tmp)
275 printf("Status: %s %s %s %s %s %s %s\n",
276 (tmp & USB_ST_ACTIVE) ? "Active" : "",
277 (tmp & USB_ST_STALLED) ? "Stalled" : "",
278 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
279 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
280 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
281 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
282 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
285 /***********************************************************************
286 * Data transfer routines
287 ***********************************************************************/
289 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
298 /* determine the maximum packet size for these transfers */
299 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
301 /* while we have data left to transfer */
304 /* calculate how long this will be -- maximum or a remainder */
305 this_xfer = length > max_size ? max_size : length;
308 /* setup the retry counter */
311 /* set up the transfer loop */
313 /* transfer the data */
314 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
315 (unsigned int)buf, this_xfer, 11 - maxtry);
316 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
317 this_xfer, &partial, USB_CNTL_TIMEOUT*5);
318 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
319 result, partial, this_xfer);
320 if(us->pusb_dev->status!=0) {
321 /* if we stall, we need to clear it before we go on */
322 #ifdef USB_STOR_DEBUG
323 display_int_status(us->pusb_dev->status);
325 if (us->pusb_dev->status & USB_ST_STALLED) {
326 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
327 stat = us->pusb_dev->status;
328 usb_clear_halt(us->pusb_dev, pipe);
329 us->pusb_dev->status=stat;
330 if(this_xfer == partial) {
331 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us->pusb_dev->status);
337 if (us->pusb_dev->status & USB_ST_NAK_REC) {
338 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
341 if(this_xfer == partial) {
342 USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us->pusb_dev->status);
345 /* if our try counter reaches 0, bail out */
346 USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us->pusb_dev->status,partial);
350 /* update to show what data was transferred */
351 this_xfer -= partial;
353 /* continue until this transfer is done */
354 } while ( this_xfer );
357 /* if we get here, we're done and successful */
361 static int usb_stor_BBB_reset(struct us_data *us)
367 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
369 * For Reset Recovery the host shall issue in the following order:
370 * a) a Bulk-Only Mass Storage Reset
371 * b) a Clear Feature HALT to the Bulk-In endpoint
372 * c) a Clear Feature HALT to the Bulk-Out endpoint
374 * This is done in 3 steps.
376 * If the reset doesn't succeed, the device should be port reset.
378 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
380 USB_STOR_PRINTF("BBB_reset\n");
381 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
382 US_BBB_RESET, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
383 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT*5);
385 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED))
387 USB_STOR_PRINTF("RESET:stall\n");
391 /* long wait for reset */
393 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n",result,us->pusb_dev->status);
394 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
395 result = usb_clear_halt(us->pusb_dev, pipe);
396 /* long wait for reset */
398 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",result,us->pusb_dev->status);
399 /* long wait for reset */
400 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
401 result = usb_clear_halt(us->pusb_dev, pipe);
403 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing OUT endpoint\n",result,us->pusb_dev->status);
404 USB_STOR_PRINTF("BBB_reset done\n");
408 /* FIXME: this reset function doesn't really reset the port, and it
409 * should. Actually it should probably do what it's doing here, and
410 * reset the port physically
412 static int usb_stor_CB_reset(struct us_data *us)
414 unsigned char cmd[12];
417 USB_STOR_PRINTF("CB_reset\n");
418 memset(cmd, 0xFF, sizeof(cmd));
419 cmd[0] = SCSI_SEND_DIAG;
421 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
422 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
423 0, us->ifnum, cmd, sizeof(cmd), USB_CNTL_TIMEOUT*5);
425 /* long wait for reset */
427 USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result,us->pusb_dev->status);
428 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
429 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
431 USB_STOR_PRINTF("CB_reset done\n");
436 * Set up the command for a BBB device. Note that the actual SCSI
437 * command is copied into cbw.CBWCDB.
439 int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
447 dir_in = US_DIRECTION(srb->cmd[0]);
449 #ifdef BBB_COMDAT_TRACE
450 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);
452 for(result = 0;result < srb->cmdlen;result++)
453 printf("cmd[%d] %#x ", result, srb->cmd[result]);
458 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
459 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
463 /* always OUT to the ep */
464 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
466 cbw.dCBWSignature = swap_32(CBWSIGNATURE);
467 cbw.dCBWTag = swap_32(CBWTag++);
468 cbw.dCBWDataTransferLength = swap_32(srb->datalen);
469 cbw.bCBWFlags = (dir_in? CBWFLAGS_IN : CBWFLAGS_OUT);
470 cbw.bCBWLUN = srb->lun;
471 cbw.bCDBLength = srb->cmdlen;
472 /* copy the command data into the CBW command data buffer */
474 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
475 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE, &actlen, USB_CNTL_TIMEOUT*5);
477 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
481 /* FIXME: we also need a CBI_command which sets up the completion
482 * interrupt, and waits for it
484 int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
489 unsigned long status;
492 dir_in=US_DIRECTION(srb->cmd[0]);
495 pipe=usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
497 pipe=usb_sndbulkpipe(us->pusb_dev, us->ep_out);
499 USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry);
500 #ifdef USB_STOR_DEBUG
503 /* let's send the command via the control pipe */
504 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
505 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
507 srb->cmd, srb->cmdlen, USB_CNTL_TIMEOUT*5);
508 USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result,us->pusb_dev->status);
509 /* check the return code for the command */
511 if(us->pusb_dev->status & USB_ST_STALLED) {
512 status=us->pusb_dev->status;
513 USB_STOR_PRINTF(" stall during command found, clear pipe\n");
514 usb_clear_halt(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0));
515 us->pusb_dev->status=status;
517 USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb->cmd[0],us->pusb_dev->status);
520 /* transfer the data payload for this command, if one exists*/
522 USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result,dir_in ? "IN" : "OUT",srb->datalen);
524 result = us_one_transfer(us, pipe, srb->pdata,srb->datalen);
525 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);
526 if(!(us->pusb_dev->status & USB_ST_NAK_REC))
528 } /* if (srb->datalen) */
538 int usb_stor_CBI_get_status (ccb * srb, struct us_data *us)
543 submit_int_msg (us->pusb_dev, us->irqpipe,
544 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
547 if ((volatile int *) us->ip_wanted == 0)
552 printf (" Did not get interrupt on CBI\n");
554 return USB_STOR_TRANSPORT_ERROR;
557 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
558 us->ip_data, us->pusb_dev->irq_act_len,
559 us->pusb_dev->irq_status);
560 /* UFI gives us ASC and ASCQ, like a request sense */
561 if (us->subclass == US_SC_UFI) {
562 if (srb->cmd[0] == SCSI_REQ_SENSE ||
563 srb->cmd[0] == SCSI_INQUIRY)
564 return USB_STOR_TRANSPORT_GOOD; /* Good */
565 else if (us->ip_data)
566 return USB_STOR_TRANSPORT_FAILED;
568 return USB_STOR_TRANSPORT_GOOD;
570 /* otherwise, we interpret the data normally */
571 switch (us->ip_data) {
573 return USB_STOR_TRANSPORT_GOOD;
575 return USB_STOR_TRANSPORT_FAILED;
577 return USB_STOR_TRANSPORT_ERROR;
579 return USB_STOR_TRANSPORT_ERROR;
582 #define USB_TRANSPORT_UNKNOWN_RETRY 5
583 #define USB_TRANSPORT_NOT_READY_RETRY 10
585 /* clear a stall on an endpoint - special for BBB devices */
586 int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
590 /* ENDPOINT_HALT = 0, so set value to 0 */
591 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
592 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
593 0, endpt, 0, 0, USB_CNTL_TIMEOUT*5);
597 int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
601 int actlen, data_actlen;
602 unsigned int pipe, pipein, pipeout;
604 #ifdef BBB_XPORT_TRACE
609 dir_in = US_DIRECTION(srb->cmd[0]);
612 USB_STOR_PRINTF("COMMAND phase\n");
613 result = usb_stor_BBB_comdat(srb, us);
615 USB_STOR_PRINTF("failed to send CBW status %ld\n",
616 us->pusb_dev->status);
617 usb_stor_BBB_reset(us);
618 return USB_STOR_TRANSPORT_FAILED;
621 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
622 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
623 /* DATA phase + error handling */
625 /* no data, go immediately to the STATUS phase */
626 if (srb->datalen == 0)
628 USB_STOR_PRINTF("DATA phase\n");
633 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen, &data_actlen, USB_CNTL_TIMEOUT*5);
634 /* special handling of STALL in DATA phase */
635 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
636 USB_STOR_PRINTF("DATA:stall\n");
637 /* clear the STALL on the endpoint */
638 result = usb_stor_BBB_clear_endpt_stall(us, dir_in? us->ep_in : us->ep_out);
640 /* continue on to STATUS phase */
644 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
645 us->pusb_dev->status);
646 usb_stor_BBB_reset(us);
647 return USB_STOR_TRANSPORT_FAILED;
649 #ifdef BBB_XPORT_TRACE
650 for (index = 0; index < data_actlen; index++)
651 printf("pdata[%d] %#x ", index, srb->pdata[index]);
654 /* STATUS phase + error handling */
658 USB_STOR_PRINTF("STATUS phase\n");
659 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
660 &actlen, USB_CNTL_TIMEOUT*5);
662 /* special handling of STALL in STATUS phase */
663 if((result < 0) && (retry < 1) && (us->pusb_dev->status & USB_ST_STALLED)) {
664 USB_STOR_PRINTF("STATUS:stall\n");
665 /* clear the STALL on the endpoint */
666 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
667 if (result >= 0 && (retry++ < 1))
672 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
673 us->pusb_dev->status);
674 usb_stor_BBB_reset(us);
675 return USB_STOR_TRANSPORT_FAILED;
677 #ifdef BBB_XPORT_TRACE
678 ptr = (unsigned char *)&csw;
679 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
680 printf("ptr[%d] %#x ", index, ptr[index]);
683 /* misuse pipe to get the residue */
684 pipe = swap_32(csw.dCSWDataResidue);
685 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
686 pipe = srb->datalen - data_actlen;
687 if (CSWSIGNATURE != swap_32(csw.dCSWSignature)) {
688 USB_STOR_PRINTF("!CSWSIGNATURE\n");
689 usb_stor_BBB_reset(us);
690 return USB_STOR_TRANSPORT_FAILED;
691 } else if ((CBWTag - 1) != swap_32(csw.dCSWTag)) {
692 USB_STOR_PRINTF("!Tag\n");
693 usb_stor_BBB_reset(us);
694 return USB_STOR_TRANSPORT_FAILED;
695 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
696 USB_STOR_PRINTF(">PHASE\n");
697 usb_stor_BBB_reset(us);
698 return USB_STOR_TRANSPORT_FAILED;
699 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
700 USB_STOR_PRINTF("=PHASE\n");
701 usb_stor_BBB_reset(us);
702 return USB_STOR_TRANSPORT_FAILED;
703 } else if (data_actlen > srb->datalen) {
704 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
705 data_actlen, srb->datalen);
706 return USB_STOR_TRANSPORT_FAILED;
707 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
708 USB_STOR_PRINTF("FAILED\n");
709 return USB_STOR_TRANSPORT_FAILED;
715 int usb_stor_CB_transport(ccb *srb, struct us_data *us)
723 status=USB_STOR_TRANSPORT_GOOD;
726 /* issue the command */
728 result=usb_stor_CB_comdat(srb,us);
729 USB_STOR_PRINTF("command / Data returned %d, status %X\n",result,us->pusb_dev->status);
730 /* if this is an CBI Protocol, get IRQ */
731 if(us->protocol==US_PR_CBI) {
732 status=usb_stor_CBI_get_status(srb,us);
733 /* if the status is error, report it */
734 if(status==USB_STOR_TRANSPORT_ERROR) {
735 USB_STOR_PRINTF(" USB CBI Command Error\n");
738 srb->sense_buf[12]=(unsigned char)(us->ip_data>>8);
739 srb->sense_buf[13]=(unsigned char)(us->ip_data&0xff);
741 /* if the status is good, report it */
742 if(status==USB_STOR_TRANSPORT_GOOD) {
743 USB_STOR_PRINTF(" USB CBI Command Good\n");
748 /* do we have to issue an auto request? */
749 /* HERE we have to check the result */
750 if((result<0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
751 USB_STOR_PRINTF("ERROR %X\n",us->pusb_dev->status);
752 us->transport_reset(us);
753 return USB_STOR_TRANSPORT_ERROR;
755 if((us->protocol==US_PR_CBI) &&
756 ((srb->cmd[0]==SCSI_REQ_SENSE) ||
757 (srb->cmd[0]==SCSI_INQUIRY))) { /* do not issue an autorequest after request sense */
758 USB_STOR_PRINTF("No auto request and good\n");
759 return USB_STOR_TRANSPORT_GOOD;
761 /* issue an request_sense */
762 memset(&psrb->cmd[0],0,12);
763 psrb->cmd[0]=SCSI_REQ_SENSE;
764 psrb->cmd[1]=srb->lun<<5;
767 psrb->pdata=&srb->sense_buf[0];
769 /* issue the command */
770 result=usb_stor_CB_comdat(psrb,us);
771 USB_STOR_PRINTF("auto request returned %d\n",result);
772 /* if this is an CBI Protocol, get IRQ */
773 if(us->protocol==US_PR_CBI) {
774 status=usb_stor_CBI_get_status(psrb,us);
776 if((result<0)&&!(us->pusb_dev->status & USB_ST_STALLED)) {
777 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us->pusb_dev->status);
778 return USB_STOR_TRANSPORT_ERROR;
780 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]);
781 /* Check the auto request result */
782 if((srb->sense_buf[2]==0) &&
783 (srb->sense_buf[12]==0) &&
784 (srb->sense_buf[13]==0)) /* ok, no sense */
785 return USB_STOR_TRANSPORT_GOOD;
786 /* Check the auto request result */
787 switch(srb->sense_buf[2]) {
788 case 0x01: /* Recovered Error */
789 return USB_STOR_TRANSPORT_GOOD;
791 case 0x02: /* Not Ready */
792 if(notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
793 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
794 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
795 return USB_STOR_TRANSPORT_FAILED;
802 if(retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
803 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
804 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
805 return USB_STOR_TRANSPORT_FAILED;
811 return USB_STOR_TRANSPORT_FAILED;
815 static int usb_inquiry(ccb *srb,struct us_data *ss)
820 memset(&srb->cmd[0],0,12);
821 srb->cmd[0]=SCSI_INQUIRY;
822 srb->cmd[1]=srb->lun<<5;
826 i=ss->transport(srb,ss);
827 USB_STOR_PRINTF("inquiry returns %d\n",i);
833 printf("error in inquiry\n");
839 static int usb_request_sense(ccb *srb,struct us_data *ss)
844 memset(&srb->cmd[0],0,12);
845 srb->cmd[0]=SCSI_REQ_SENSE;
846 srb->cmd[1]=srb->lun<<5;
849 srb->pdata=&srb->sense_buf[0];
851 ss->transport(srb,ss);
852 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
857 static int usb_test_unit_ready(ccb *srb,struct us_data *ss)
862 memset(&srb->cmd[0],0,12);
863 srb->cmd[0]=SCSI_TST_U_RDY;
864 srb->cmd[1]=srb->lun<<5;
867 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
870 usb_request_sense (srb, ss);
877 static int usb_read_capacity(ccb *srb,struct us_data *ss)
880 retry = 3; /* retries */
882 memset(&srb->cmd[0],0,12);
883 srb->cmd[0]=SCSI_RD_CAPAC;
884 srb->cmd[1]=srb->lun<<5;
887 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
895 static int usb_read_10(ccb *srb,struct us_data *ss, unsigned long start, unsigned short blocks)
897 memset(&srb->cmd[0],0,12);
898 srb->cmd[0]=SCSI_READ10;
899 srb->cmd[1]=srb->lun<<5;
900 srb->cmd[2]=((unsigned char) (start>>24))&0xff;
901 srb->cmd[3]=((unsigned char) (start>>16))&0xff;
902 srb->cmd[4]=((unsigned char) (start>>8))&0xff;
903 srb->cmd[5]=((unsigned char) (start))&0xff;
904 srb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
905 srb->cmd[8]=(unsigned char) blocks & 0xff;
907 USB_STOR_PRINTF("read10: start %lx blocks %x\n",start,blocks);
908 return ss->transport(srb,ss);
912 #define USB_MAX_READ_BLK 20
914 unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer)
916 unsigned long start,blks, buf_addr;
917 unsigned short smallblks;
918 struct usb_device *dev;
928 USB_STOR_PRINTF("\nusb_read: dev %d \n",device);
930 for(i=0;i<USB_MAX_DEVICE;i++) {
931 dev=usb_get_dev_index(i);
935 if(dev->devnum==usb_dev_desc[device].target)
939 usb_disable_asynch(1); /* asynch transfer not allowed */
940 srb->lun=usb_dev_desc[device].lun;
941 buf_addr=(unsigned long)buffer;
944 if(usb_test_unit_ready(srb,(struct us_data *)dev->privptr)) {
945 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
946 srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
949 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks, buf_addr);
952 srb->pdata=(unsigned char *)buf_addr;
953 if(blks>USB_MAX_READ_BLK) {
954 smallblks=USB_MAX_READ_BLK;
956 smallblks=(unsigned short) blks;
959 if(smallblks==USB_MAX_READ_BLK)
961 srb->datalen=usb_dev_desc[device].blksz * smallblks;
962 srb->pdata=(unsigned char *)buf_addr;
963 if(usb_read_10(srb,(struct us_data *)dev->privptr, start, smallblks)) {
964 USB_STOR_PRINTF("Read ERROR\n");
965 usb_request_sense(srb,(struct us_data *)dev->privptr);
973 buf_addr+=srb->datalen;
975 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
976 usb_disable_asynch(0); /* asynch transfer allowed */
977 if(blkcnt>=USB_MAX_READ_BLK)
983 /* Probe to see if a new device is actually a Storage device */
984 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss)
986 struct usb_interface_descriptor *iface;
988 unsigned int flags = 0;
993 /* let's examine the device now */
994 iface = &dev->config.if_desc[ifnum];
997 /* this is the place to patch some storage devices */
998 USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
999 if ((dev->descriptor.idVendor) == 0x066b && (dev->descriptor.idProduct) == 0x0103) {
1000 USB_STOR_PRINTF("patched for E-USB\n");
1001 protocol = US_PR_CB;
1002 subclass = US_SC_UFI; /* an assumption */
1006 if (dev->descriptor.bDeviceClass != 0 ||
1007 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1008 iface->bInterfaceSubClass < US_SC_MIN ||
1009 iface->bInterfaceSubClass > US_SC_MAX) {
1010 /* if it's not a mass storage, we go no further */
1014 memset(ss, 0, sizeof(struct us_data));
1016 /* At this point, we know we've got a live one */
1017 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1019 /* Initialize the us_data structure with some useful info */
1023 ss->attention_done = 0;
1025 /* If the device has subclass and protocol, then use that. Otherwise,
1026 * take data from the specific interface.
1029 ss->subclass = subclass;
1030 ss->protocol = protocol;
1032 ss->subclass = iface->bInterfaceSubClass;
1033 ss->protocol = iface->bInterfaceProtocol;
1036 /* set the handler pointers based on the protocol */
1037 USB_STOR_PRINTF("Transport: ");
1038 switch (ss->protocol) {
1040 USB_STOR_PRINTF("Control/Bulk\n");
1041 ss->transport = usb_stor_CB_transport;
1042 ss->transport_reset = usb_stor_CB_reset;
1046 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1047 ss->transport = usb_stor_CB_transport;
1048 ss->transport_reset = usb_stor_CB_reset;
1051 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1052 ss->transport = usb_stor_BBB_transport;
1053 ss->transport_reset = usb_stor_BBB_reset;
1056 printf("USB Storage Transport unknown / not yet implemented\n");
1062 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1063 * An optional interrupt is OK (necessary for CBI protocol).
1064 * We will ignore any others.
1066 for (i = 0; i < iface->bNumEndpoints; i++) {
1067 /* is it an BULK endpoint? */
1068 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1069 == USB_ENDPOINT_XFER_BULK) {
1070 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1071 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1072 USB_ENDPOINT_NUMBER_MASK;
1074 ss->ep_out = iface->ep_desc[i].bEndpointAddress &
1075 USB_ENDPOINT_NUMBER_MASK;
1078 /* is it an interrupt endpoint? */
1079 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1080 == USB_ENDPOINT_XFER_INT) {
1081 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1082 USB_ENDPOINT_NUMBER_MASK;
1083 ss->irqinterval = iface->ep_desc[i].bInterval;
1086 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1087 ss->ep_in, ss->ep_out, ss->ep_int);
1089 /* Do some basic sanity checks, and bail if we find a problem */
1090 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
1091 !ss->ep_in || !ss->ep_out ||
1092 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1093 USB_STOR_PRINTF("Problems with device\n");
1096 /* set class specific stuff */
1097 /* We only handle certain protocols. Currently, these are
1099 * The SFF8070 accepts the requests used in u-boot
1101 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1102 ss->subclass != US_SC_8070) {
1103 printf("Sorry, protocol %d not yet supported.\n",ss->subclass);
1106 if(ss->ep_int) { /* we had found an interrupt endpoint, prepare irq pipe */
1107 /* set up the IRQ pipe and handler */
1109 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1110 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1111 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1112 dev->irq_handle=usb_stor_irq;
1114 dev->privptr=(void *)ss;
1118 int usb_stor_get_info(struct usb_device *dev,struct us_data *ss,block_dev_desc_t *dev_desc)
1120 unsigned char perq,modi;
1121 unsigned long cap[2];
1122 unsigned long *capacity,*blksz;
1123 ccb *pccb = &usb_ccb;
1125 /* for some reasons a couple of devices would not survive this reset */
1128 (dev->descriptor.idVendor == 0x054c &&
1129 dev->descriptor.idProduct == 0x019e)
1132 /* USB007 Mini-USB2 Flash Drive */
1133 (dev->descriptor.idVendor == 0x066f &&
1134 dev->descriptor.idProduct == 0x2010)
1136 USB_STOR_PRINTF("usb_stor_get_info: skipping RESET..\n");
1138 ss->transport_reset(ss);
1140 pccb->pdata = usb_stor_buf;
1142 dev_desc->target = dev->devnum;
1143 pccb->lun = dev_desc->lun;
1144 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1146 if(usb_inquiry(pccb,ss))
1149 perq = usb_stor_buf[0];
1150 modi = usb_stor_buf[1];
1151 if((perq & 0x1f) == 0x1f) {
1152 return 0; /* skip unknown devices */
1154 if((modi&0x80) == 0x80) {/* drive is removable */
1155 dev_desc->removable = 1;
1157 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1158 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1159 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
1160 dev_desc->vendor[8] = 0;
1161 dev_desc->product[16] = 0;
1162 dev_desc->revision[4] = 0;
1163 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf[2],usb_stor_buf[3]);
1164 if(usb_test_unit_ready(pccb,ss)) {
1165 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",pccb->sense_buf[2],pccb->sense_buf[12],pccb->sense_buf[13]);
1166 if(dev_desc->removable == 1) {
1167 dev_desc->type = perq;
1173 pccb->pdata = (unsigned char *)&cap[0];
1174 memset(pccb->pdata,0,8);
1175 if(usb_read_capacity(pccb,ss) != 0) {
1176 printf("READ_CAP ERROR\n");
1180 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap[0],cap[1]);
1182 if(cap[0]>(0x200000 * 10)) /* greater than 10 GByte */
1186 cap[0] = ((unsigned long)(
1187 (((unsigned long)(cap[0]) & (unsigned long)0x000000ffUL) << 24) |
1188 (((unsigned long)(cap[0]) & (unsigned long)0x0000ff00UL) << 8) |
1189 (((unsigned long)(cap[0]) & (unsigned long)0x00ff0000UL) >> 8) |
1190 (((unsigned long)(cap[0]) & (unsigned long)0xff000000UL) >> 24) ));
1191 cap[1] = ((unsigned long)(
1192 (((unsigned long)(cap[1]) & (unsigned long)0x000000ffUL) << 24) |
1193 (((unsigned long)(cap[1]) & (unsigned long)0x0000ff00UL) << 8) |
1194 (((unsigned long)(cap[1]) & (unsigned long)0x00ff0000UL) >> 8) |
1195 (((unsigned long)(cap[1]) & (unsigned long)0xff000000UL) >> 24) ));
1197 /* this assumes bigendian! */
1201 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity,*blksz);
1202 dev_desc->lba = *capacity;
1203 dev_desc->blksz = *blksz;
1204 dev_desc->type = perq;
1205 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1206 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1208 init_part(dev_desc);
1210 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1214 #endif /* CONFIG_USB_STORAGE */
1215 #endif /* CFG_CMD_USB */