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
59 #define USB_STOR_PRINTF(fmt,args...) printf (fmt ,##args)
61 #define USB_STOR_PRINTF(fmt,args...)
65 /* direction table -- this indicates the direction of the data
66 * transfer for each command code -- a 1 indicates input
68 unsigned char us_direction[256/8] = {
69 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
70 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
74 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
76 static unsigned char usb_stor_buf[512];
88 #define US_BBB_RESET 0xff
89 #define US_BBB_GET_MAX_LUN 0xfe
91 /* Command Block Wrapper */
94 # define CBWSIGNATURE 0x43425355
96 __u32 dCBWDataTransferLength;
98 # define CBWFLAGS_OUT 0x00
99 # define CBWFLAGS_IN 0x80
102 # define CBWCDBLENGTH 16
103 __u8 CBWCDB[CBWCDBLENGTH];
105 #define UMASS_BBB_CBW_SIZE 31
106 static __u32 CBWTag = 0;
108 /* Command Status Wrapper */
111 # define CSWSIGNATURE 0x53425355
113 __u32 dCSWDataResidue;
115 # define CSWSTATUS_GOOD 0x0
116 # define CSWSTATUS_FAILED 0x1
117 # define CSWSTATUS_PHASE 0x2
119 #define UMASS_BBB_CSW_SIZE 13
121 #define USB_MAX_STOR_DEV 5
122 static int usb_max_devs; /* number of highest available usb device */
124 static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
127 typedef int (*trans_cmnd)(ccb*, struct us_data*);
128 typedef int (*trans_reset)(struct us_data*);
131 struct usb_device *pusb_dev; /* this usb_device */
132 unsigned int flags; /* from filter initially */
133 unsigned char ifnum; /* interface number */
134 unsigned char ep_in; /* in endpoint */
135 unsigned char ep_out; /* out ....... */
136 unsigned char ep_int; /* interrupt . */
137 unsigned char subclass; /* as in overview */
138 unsigned char protocol; /* .............. */
139 unsigned char attention_done; /* force attn on first cmd */
140 unsigned short ip_data; /* interrupt data */
141 int action; /* what to do */
142 int ip_wanted; /* needed */
143 int *irq_handle; /* for USB int requests */
144 unsigned int irqpipe; /* pipe for release_irq */
145 unsigned char irqmaxp; /* max packed for irq Pipe */
146 unsigned char irqinterval; /* Intervall for IRQ Pipe */
147 ccb *srb; /* current srb */
148 trans_reset transport_reset; /* reset routine */
149 trans_cmnd transport; /* transport routine */
152 static struct us_data usb_stor[USB_MAX_STOR_DEV];
155 #define USB_STOR_TRANSPORT_GOOD 0
156 #define USB_STOR_TRANSPORT_FAILED -1
157 #define USB_STOR_TRANSPORT_ERROR -2
160 int usb_stor_get_info(struct usb_device *dev, struct us_data *us, block_dev_desc_t *dev_desc);
161 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss);
162 unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer);
163 struct usb_device * usb_get_dev_index(int index);
164 void uhci_show_temp_int_td(void);
166 block_dev_desc_t *usb_stor_get_dev(int index)
168 return &usb_dev_desc[index];
172 void usb_show_progress(void)
177 /*********************************************************************************
178 * (re)-scan the usb and reports device info
179 * to the user if mode = 1
180 * returns current device or -1 if no
182 int usb_stor_scan(int mode)
185 struct usb_device *dev;
188 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
191 printf(" scanning bus for storage devices...\n");
193 usb_disable_asynch(1); /* asynch transfer not allowed */
195 for(i=0;i<USB_MAX_STOR_DEV;i++) {
196 memset(&usb_dev_desc[i],0,sizeof(block_dev_desc_t));
197 usb_dev_desc[i].target=0xff;
198 usb_dev_desc[i].if_type=IF_TYPE_USB;
199 usb_dev_desc[i].dev=i;
200 usb_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
201 usb_dev_desc[i].block_read=usb_stor_read;
204 for(i=0;i<USB_MAX_DEVICE;i++) {
205 dev=usb_get_dev_index(i); /* get device */
206 USB_STOR_PRINTF("i=%d\n",i);
208 break; /* no more devices avaiable */
210 if(usb_storage_probe(dev,0,&usb_stor[usb_max_devs])) { /* ok, it is a storage devices */
211 /* get info and fill it in */
213 if(usb_stor_get_info(dev, &usb_stor[usb_max_devs], &usb_dev_desc[usb_max_devs])) {
215 printf (" Device %d: ", usb_max_devs);
216 dev_print(&usb_dev_desc[usb_max_devs]);
219 } /* if get info ok */
220 } /* if storage device */
221 if(usb_max_devs==USB_MAX_STOR_DEV) {
222 printf("max USB Storage Device reached: %d stopping\n",usb_max_devs);
226 usb_disable_asynch(0); /* asynch transfer allowed */
233 static int usb_stor_irq(struct usb_device *dev)
236 us=(struct us_data *)dev->privptr;
245 #ifdef USB_STOR_DEBUG
247 static void usb_show_srb(ccb * pccb)
250 printf("SRB: len %d datalen 0x%lX\n ",pccb->cmdlen,pccb->datalen);
252 printf("%02X ",pccb->cmd[i]);
257 static void display_int_status(unsigned long tmp)
259 printf("Status: %s %s %s %s %s %s %s\n",
260 (tmp & USB_ST_ACTIVE) ? "Active" : "",
261 (tmp & USB_ST_STALLED) ? "Stalled" : "",
262 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
263 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
264 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
265 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
266 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
269 /***********************************************************************
270 * Data transfer routines
271 ***********************************************************************/
273 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
282 /* determine the maximum packet size for these transfers */
283 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
285 /* while we have data left to transfer */
288 /* calculate how long this will be -- maximum or a remainder */
289 this_xfer = length > max_size ? max_size : length;
292 /* setup the retry counter */
295 /* set up the transfer loop */
297 /* transfer the data */
298 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
299 (unsigned int)buf, this_xfer, 11 - maxtry);
300 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
301 this_xfer, &partial, USB_CNTL_TIMEOUT*5);
302 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
303 result, partial, this_xfer);
304 if(us->pusb_dev->status!=0) {
305 /* if we stall, we need to clear it before we go on */
306 #ifdef USB_STOR_DEBUG
307 display_int_status(us->pusb_dev->status);
309 if (us->pusb_dev->status & USB_ST_STALLED) {
310 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
311 stat = us->pusb_dev->status;
312 usb_clear_halt(us->pusb_dev, pipe);
313 us->pusb_dev->status=stat;
314 if(this_xfer == partial) {
315 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us->pusb_dev->status);
321 if (us->pusb_dev->status & USB_ST_NAK_REC) {
322 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
325 if(this_xfer == partial) {
326 USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us->pusb_dev->status);
329 /* if our try counter reaches 0, bail out */
330 USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us->pusb_dev->status,partial);
334 /* update to show what data was transferred */
335 this_xfer -= partial;
337 /* continue until this transfer is done */
338 } while ( this_xfer );
341 /* if we get here, we're done and successful */
345 static int usb_stor_BBB_reset(struct us_data *us)
351 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
353 * For Reset Recovery the host shall issue in the following order:
354 * a) a Bulk-Only Mass Storage Reset
355 * b) a Clear Feature HALT to the Bulk-In endpoint
356 * c) a Clear Feature HALT to the Bulk-Out endpoint
358 * This is done in 3 steps.
360 * If the reset doesn't succeed, the device should be port reset.
362 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
364 USB_STOR_PRINTF("BBB_reset\n");
365 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
366 US_BBB_RESET, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
367 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT*5);
368 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED))
370 USB_STOR_PRINTF("RESET:stall\n");
373 /* long wait for reset */
375 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n",result,us->pusb_dev->status);
376 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
377 result = usb_clear_halt(us->pusb_dev, pipe);
378 /* long wait for reset */
380 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",result,us->pusb_dev->status);
381 /* long wait for reset */
382 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
383 result = usb_clear_halt(us->pusb_dev, pipe);
385 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing OUT endpoint\n",result,us->pusb_dev->status);
386 USB_STOR_PRINTF("BBB_reset done\n");
390 /* FIXME: this reset function doesn't really reset the port, and it
391 * should. Actually it should probably do what it's doing here, and
392 * reset the port physically
394 static int usb_stor_CB_reset(struct us_data *us)
396 unsigned char cmd[12];
399 USB_STOR_PRINTF("CB_reset\n");
400 memset(cmd, 0xFF, sizeof(cmd));
401 cmd[0] = SCSI_SEND_DIAG;
403 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
404 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
405 0, us->ifnum, cmd, sizeof(cmd), USB_CNTL_TIMEOUT*5);
407 /* long wait for reset */
409 USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result,us->pusb_dev->status);
410 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
411 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
413 USB_STOR_PRINTF("CB_reset done\n");
418 * Set up the command for a BBB device. Note that the actual SCSI
419 * command is copied into cbw.CBWCDB.
421 int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
429 dir_in = US_DIRECTION(srb->cmd[0]);
431 #ifdef BBB_COMDAT_TRACE
432 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);
434 for(result = 0;result < srb->cmdlen;result++)
435 printf("cmd[%d] %#x ", result, srb->cmd[result]);
440 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
441 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
445 /* always OUT to the ep */
446 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
448 cbw.dCBWSignature = swap_32(CBWSIGNATURE);
449 cbw.dCBWTag = swap_32(CBWTag++);
450 cbw.dCBWDataTransferLength = swap_32(srb->datalen);
451 cbw.bCBWFlags = (dir_in? CBWFLAGS_IN : CBWFLAGS_OUT);
452 cbw.bCBWLUN = srb->lun;
453 cbw.bCDBLength = srb->cmdlen;
454 /* copy the command data into the CBW command data buffer */
456 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
457 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE, &actlen, USB_CNTL_TIMEOUT*5);
459 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
463 /* FIXME: we also need a CBI_command which sets up the completion
464 * interrupt, and waits for it
466 int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
471 unsigned long status;
474 dir_in=US_DIRECTION(srb->cmd[0]);
477 pipe=usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
479 pipe=usb_sndbulkpipe(us->pusb_dev, us->ep_out);
481 USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry);
482 #ifdef USB_STOR_DEBUG
485 /* let's send the command via the control pipe */
486 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
487 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
489 srb->cmd, srb->cmdlen, USB_CNTL_TIMEOUT*5);
490 USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result,us->pusb_dev->status);
491 /* check the return code for the command */
493 if(us->pusb_dev->status & USB_ST_STALLED) {
494 status=us->pusb_dev->status;
495 USB_STOR_PRINTF(" stall during command found, clear pipe\n");
496 usb_clear_halt(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0));
497 us->pusb_dev->status=status;
499 USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb->cmd[0],us->pusb_dev->status);
502 /* transfer the data payload for this command, if one exists*/
504 USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result,dir_in ? "IN" : "OUT",srb->datalen);
506 result = us_one_transfer(us, pipe, srb->pdata,srb->datalen);
507 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);
508 if(!(us->pusb_dev->status & USB_ST_NAK_REC))
510 } /* if (srb->datalen) */
520 int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
525 submit_int_msg(us->pusb_dev,us->irqpipe,
526 (void *)&us->ip_data,us->irqmaxp ,us->irqinterval);
529 if((volatile int *)us->ip_wanted==0)
534 printf(" Did not get interrupt on CBI\n");
536 return USB_STOR_TRANSPORT_ERROR;
538 USB_STOR_PRINTF("Got interrupt data 0x%x, transfered %d status 0x%lX\n", us->ip_data,us->pusb_dev->irq_act_len,us->pusb_dev->irq_status);
539 /* UFI gives us ASC and ASCQ, like a request sense */
540 if (us->subclass == US_SC_UFI) {
541 if (srb->cmd[0] == SCSI_REQ_SENSE ||
542 srb->cmd[0] == SCSI_INQUIRY)
543 return USB_STOR_TRANSPORT_GOOD; /* Good */
546 return USB_STOR_TRANSPORT_FAILED;
548 return USB_STOR_TRANSPORT_GOOD;
550 /* otherwise, we interpret the data normally */
551 switch (us->ip_data) {
553 return USB_STOR_TRANSPORT_GOOD;
555 return USB_STOR_TRANSPORT_FAILED;
557 return USB_STOR_TRANSPORT_ERROR;
559 return USB_STOR_TRANSPORT_ERROR;
562 #define USB_TRANSPORT_UNKNOWN_RETRY 5
563 #define USB_TRANSPORT_NOT_READY_RETRY 10
565 /* clear a stall on an endpoint - special for BBB devices */
566 int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
570 /* ENDPOINT_HALT = 0, so set value to 0 */
571 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
572 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
573 0, endpt, 0, 0, USB_CNTL_TIMEOUT*5);
577 int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
581 int actlen, data_actlen;
582 unsigned int pipe, pipein, pipeout;
584 #ifdef BBB_XPORT_TRACE
589 dir_in = US_DIRECTION(srb->cmd[0]);
592 USB_STOR_PRINTF("COMMAND phase\n");
593 result = usb_stor_BBB_comdat(srb, us);
595 USB_STOR_PRINTF("failed to send CBW status %ld\n",
596 us->pusb_dev->status);
597 usb_stor_BBB_reset(us);
598 return USB_STOR_TRANSPORT_FAILED;
601 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
602 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
603 /* DATA phase + error handling */
604 USB_STOR_PRINTF("DATA phase\n");
606 /* no data, go immediately to the STATUS phase */
607 if (srb->datalen == 0)
613 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen, &data_actlen, USB_CNTL_TIMEOUT*5);
614 /* special handling of STALL in DATA phase */
615 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
616 USB_STOR_PRINTF("DATA:stall\n");
617 /* clear the STALL on the endpoint */
618 result = usb_stor_BBB_clear_endpt_stall(us, dir_in? us->ep_in : us->ep_out);
620 /* continue on to STATUS phase */
624 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
625 us->pusb_dev->status);
626 usb_stor_BBB_reset(us);
627 return USB_STOR_TRANSPORT_FAILED;
629 #ifdef BBB_XPORT_TRACE
630 for (index = 0; index < data_actlen; index++)
631 printf("pdata[%d] %#x ", index, srb->pdata[index]);
634 /* STATUS phase + error handling */
638 USB_STOR_PRINTF("STATUS phase\n");
639 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE, &actlen, USB_CNTL_TIMEOUT*5);
640 /* special handling of STALL in STATUS phase */
641 if((result < 0) && (retry < 1) && (us->pusb_dev->status & USB_ST_STALLED)) {
642 USB_STOR_PRINTF("STATUS:stall\n");
643 /* clear the STALL on the endpoint */
644 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
645 if (result >= 0 && (retry++ < 1))
650 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
651 us->pusb_dev->status);
652 usb_stor_BBB_reset(us);
653 return USB_STOR_TRANSPORT_FAILED;
655 #ifdef BBB_XPORT_TRACE
656 ptr = (unsigned char *)&csw;
657 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
658 printf("ptr[%d] %#x ", index, ptr[index]);
661 /* misuse pipe to get the residue */
662 pipe = swap_32(csw.dCSWDataResidue);
663 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
664 pipe = srb->datalen - data_actlen;
665 if (CSWSIGNATURE != swap_32(csw.dCSWSignature)) {
666 USB_STOR_PRINTF("!CSWSIGNATURE\n");
667 usb_stor_BBB_reset(us);
668 return USB_STOR_TRANSPORT_FAILED;
669 } else if ((CBWTag - 1) != swap_32(csw.dCSWTag)) {
670 USB_STOR_PRINTF("!Tag\n");
671 usb_stor_BBB_reset(us);
672 return USB_STOR_TRANSPORT_FAILED;
673 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
674 USB_STOR_PRINTF(">PHASE\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 (data_actlen > srb->datalen) {
682 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
683 data_actlen, srb->datalen);
684 return USB_STOR_TRANSPORT_FAILED;
685 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
686 USB_STOR_PRINTF("FAILED\n");
687 return USB_STOR_TRANSPORT_FAILED;
693 int usb_stor_CB_transport(ccb *srb, struct us_data *us)
701 status=USB_STOR_TRANSPORT_GOOD;
704 /* issue the command */
706 result=usb_stor_CB_comdat(srb,us);
707 USB_STOR_PRINTF("command / Data returned %d, status %X\n",result,us->pusb_dev->status);
708 /* if this is an CBI Protocol, get IRQ */
709 if(us->protocol==US_PR_CBI) {
710 status=usb_stor_CBI_get_status(srb,us);
711 /* if the status is error, report it */
712 if(status==USB_STOR_TRANSPORT_ERROR) {
713 USB_STOR_PRINTF(" USB CBI Command Error\n");
716 srb->sense_buf[12]=(unsigned char)(us->ip_data>>8);
717 srb->sense_buf[13]=(unsigned char)(us->ip_data&0xff);
719 /* if the status is good, report it */
720 if(status==USB_STOR_TRANSPORT_GOOD) {
721 USB_STOR_PRINTF(" USB CBI Command Good\n");
726 /* do we have to issue an auto request? */
727 /* HERE we have to check the result */
728 if((result<0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
729 USB_STOR_PRINTF("ERROR %X\n",us->pusb_dev->status);
730 us->transport_reset(us);
731 return USB_STOR_TRANSPORT_ERROR;
733 if((us->protocol==US_PR_CBI) &&
734 ((srb->cmd[0]==SCSI_REQ_SENSE) ||
735 (srb->cmd[0]==SCSI_INQUIRY))) { /* do not issue an autorequest after request sense */
736 USB_STOR_PRINTF("No auto request and good\n");
737 return USB_STOR_TRANSPORT_GOOD;
739 /* issue an request_sense */
740 memset(&psrb->cmd[0],0,12);
741 psrb->cmd[0]=SCSI_REQ_SENSE;
742 psrb->cmd[1]=srb->lun<<5;
745 psrb->pdata=&srb->sense_buf[0];
747 /* issue the command */
748 result=usb_stor_CB_comdat(psrb,us);
749 USB_STOR_PRINTF("auto request returned %d\n",result);
750 /* if this is an CBI Protocol, get IRQ */
751 if(us->protocol==US_PR_CBI) {
752 status=usb_stor_CBI_get_status(psrb,us);
754 if((result<0)&&!(us->pusb_dev->status & USB_ST_STALLED)) {
755 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us->pusb_dev->status);
756 return USB_STOR_TRANSPORT_ERROR;
758 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]);
759 /* Check the auto request result */
760 if((srb->sense_buf[2]==0) &&
761 (srb->sense_buf[12]==0) &&
762 (srb->sense_buf[13]==0)) /* ok, no sense */
763 return USB_STOR_TRANSPORT_GOOD;
764 /* Check the auto request result */
765 switch(srb->sense_buf[2]) {
766 case 0x01: /* Recovered Error */
767 return USB_STOR_TRANSPORT_GOOD;
769 case 0x02: /* Not Ready */
770 if(notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
771 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
772 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
773 return USB_STOR_TRANSPORT_FAILED;
780 if(retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
781 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
782 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
783 return USB_STOR_TRANSPORT_FAILED;
789 return USB_STOR_TRANSPORT_FAILED;
793 static int usb_inquiry(ccb *srb,struct us_data *ss)
798 memset(&srb->cmd[0],0,12);
799 srb->cmd[0]=SCSI_INQUIRY;
800 srb->cmd[1]=srb->lun<<5;
804 i=ss->transport(srb,ss);
805 USB_STOR_PRINTF("inquiry returns %d\n",i);
811 printf("error in inquiry\n");
817 static int usb_request_sense(ccb *srb,struct us_data *ss)
822 memset(&srb->cmd[0],0,12);
823 srb->cmd[0]=SCSI_REQ_SENSE;
824 srb->cmd[1]=srb->lun<<5;
827 srb->pdata=&srb->sense_buf[0];
829 ss->transport(srb,ss);
830 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
835 static int usb_test_unit_ready(ccb *srb,struct us_data *ss)
840 memset(&srb->cmd[0],0,12);
841 srb->cmd[0]=SCSI_TST_U_RDY;
842 srb->cmd[1]=srb->lun<<5;
845 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
853 static int usb_read_capacity(ccb *srb,struct us_data *ss)
856 retry=2; /* retries */
858 memset(&srb->cmd[0],0,12);
859 srb->cmd[0]=SCSI_RD_CAPAC;
860 srb->cmd[1]=srb->lun<<5;
863 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
871 static int usb_read_10(ccb *srb,struct us_data *ss, unsigned long start, unsigned short blocks)
873 memset(&srb->cmd[0],0,12);
874 srb->cmd[0]=SCSI_READ10;
875 srb->cmd[1]=srb->lun<<5;
876 srb->cmd[2]=((unsigned char) (start>>24))&0xff;
877 srb->cmd[3]=((unsigned char) (start>>16))&0xff;
878 srb->cmd[4]=((unsigned char) (start>>8))&0xff;
879 srb->cmd[5]=((unsigned char) (start))&0xff;
880 srb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
881 srb->cmd[8]=(unsigned char) blocks & 0xff;
883 USB_STOR_PRINTF("read10: start %lx blocks %x\n",start,blocks);
884 return ss->transport(srb,ss);
888 #define USB_MAX_READ_BLK 20
890 unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer)
892 unsigned long start,blks, buf_addr;
893 unsigned short smallblks;
894 struct usb_device *dev;
900 USB_STOR_PRINTF("\nusb_read: dev %d \n",device);
902 for(i=0;i<USB_MAX_DEVICE;i++) {
903 dev=usb_get_dev_index(i);
907 if(dev->devnum==usb_dev_desc[device].target)
911 usb_disable_asynch(1); /* asynch transfer not allowed */
912 srb->lun=usb_dev_desc[device].lun;
913 buf_addr=(unsigned long)buffer;
916 if(usb_test_unit_ready(srb,(struct us_data *)dev->privptr)) {
917 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
918 srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
921 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks, buf_addr);
924 srb->pdata=(unsigned char *)buf_addr;
925 if(blks>USB_MAX_READ_BLK) {
926 smallblks=USB_MAX_READ_BLK;
928 smallblks=(unsigned short) blks;
931 if(smallblks==USB_MAX_READ_BLK)
933 srb->datalen=usb_dev_desc[device].blksz * smallblks;
934 srb->pdata=(unsigned char *)buf_addr;
935 if(usb_read_10(srb,(struct us_data *)dev->privptr, start, smallblks)) {
936 USB_STOR_PRINTF("Read ERROR\n");
937 usb_request_sense(srb,(struct us_data *)dev->privptr);
945 buf_addr+=srb->datalen;
947 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
948 usb_disable_asynch(0); /* asynch transfer allowed */
949 if(blkcnt>=USB_MAX_READ_BLK)
955 /* Probe to see if a new device is actually a Storage device */
956 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss)
958 struct usb_interface_descriptor *iface;
960 unsigned int flags = 0;
966 memset(ss, 0, sizeof(struct us_data));
968 /* let's examine the device now */
969 iface = &dev->config.if_desc[ifnum];
972 /* this is the place to patch some storage devices */
973 USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
974 if ((dev->descriptor.idVendor) == 0x066b && (dev->descriptor.idProduct) == 0x0103) {
975 USB_STOR_PRINTF("patched for E-USB\n");
977 subclass = US_SC_UFI; /* an assumption */
981 if (dev->descriptor.bDeviceClass != 0 ||
982 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
983 iface->bInterfaceSubClass < US_SC_MIN ||
984 iface->bInterfaceSubClass > US_SC_MAX) {
985 /* if it's not a mass storage, we go no further */
989 /* At this point, we know we've got a live one */
990 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
992 /* Initialize the us_data structure with some useful info */
996 ss->attention_done = 0;
998 /* If the device has subclass and protocol, then use that. Otherwise,
999 * take data from the specific interface.
1002 ss->subclass = subclass;
1003 ss->protocol = protocol;
1005 ss->subclass = iface->bInterfaceSubClass;
1006 ss->protocol = iface->bInterfaceProtocol;
1009 /* set the handler pointers based on the protocol */
1010 USB_STOR_PRINTF("Transport: ");
1011 switch (ss->protocol) {
1013 USB_STOR_PRINTF("Control/Bulk\n");
1014 ss->transport = usb_stor_CB_transport;
1015 ss->transport_reset = usb_stor_CB_reset;
1019 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1020 ss->transport = usb_stor_CB_transport;
1021 ss->transport_reset = usb_stor_CB_reset;
1024 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1025 ss->transport = usb_stor_BBB_transport;
1026 ss->transport_reset = usb_stor_BBB_reset;
1029 printf("USB Starage Transport unknown / not yet implemented\n");
1035 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1036 * An optional interrupt is OK (necessary for CBI protocol).
1037 * We will ignore any others.
1039 for (i = 0; i < iface->bNumEndpoints; i++) {
1040 /* is it an BULK endpoint? */
1041 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1042 == USB_ENDPOINT_XFER_BULK) {
1043 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1044 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1045 USB_ENDPOINT_NUMBER_MASK;
1047 ss->ep_out = iface->ep_desc[i].bEndpointAddress &
1048 USB_ENDPOINT_NUMBER_MASK;
1051 /* is it an interrupt endpoint? */
1052 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1053 == USB_ENDPOINT_XFER_INT) {
1054 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1055 USB_ENDPOINT_NUMBER_MASK;
1056 ss->irqinterval = iface->ep_desc[i].bInterval;
1059 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1060 ss->ep_in, ss->ep_out, ss->ep_int);
1062 /* Do some basic sanity checks, and bail if we find a problem */
1063 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
1064 !ss->ep_in || !ss->ep_out ||
1065 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1066 USB_STOR_PRINTF("Problems with device\n");
1069 /* set class specific stuff */
1070 /* We only handle certain protocols. Currently, these are
1073 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI) {
1074 printf("Sorry, protocol %d not yet supported.\n",ss->subclass);
1077 if(ss->ep_int) { /* we had found an interrupt endpoint, prepare irq pipe */
1078 /* set up the IRQ pipe and handler */
1080 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1081 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1082 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1083 dev->irq_handle=usb_stor_irq;
1085 dev->privptr=(void *)ss;
1089 int usb_stor_get_info(struct usb_device *dev,struct us_data *ss,block_dev_desc_t *dev_desc)
1091 unsigned char perq,modi;
1092 unsigned long cap[2];
1093 unsigned long *capacity,*blksz;
1096 ss->transport_reset(ss);
1097 pccb->pdata=usb_stor_buf;
1099 dev_desc->target=dev->devnum;
1100 pccb->lun=dev_desc->lun;
1101 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1103 if(usb_inquiry(pccb,ss))
1105 perq=usb_stor_buf[0];
1106 modi=usb_stor_buf[1];
1107 if((perq & 0x1f)==0x1f) {
1108 return 0; /* skip unknown devices */
1110 if((modi&0x80)==0x80) {/* drive is removable */
1111 dev_desc->removable=1;
1113 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1114 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1115 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
1116 dev_desc->vendor[8]=0;
1117 dev_desc->product[16]=0;
1118 dev_desc->revision[4]=0;
1119 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf[2],usb_stor_buf[3]);
1120 if(usb_test_unit_ready(pccb,ss)) {
1121 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",pccb->sense_buf[2],pccb->sense_buf[12],pccb->sense_buf[13]);
1122 if(dev_desc->removable==1) {
1123 dev_desc->type=perq;
1129 pccb->pdata=(unsigned char *)&cap[0];
1130 memset(pccb->pdata,0,8);
1131 if(usb_read_capacity(pccb,ss)!=0) {
1132 printf("READ_CAP ERROR\n");
1136 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap[0],cap[1]);
1138 if(cap[0]>(0x200000 * 10)) /* greater than 10 GByte */
1142 cap[0] = ((unsigned long)(
1143 (((unsigned long)(cap[0]) & (unsigned long)0x000000ffUL) << 24) |
1144 (((unsigned long)(cap[0]) & (unsigned long)0x0000ff00UL) << 8) |
1145 (((unsigned long)(cap[0]) & (unsigned long)0x00ff0000UL) >> 8) |
1146 (((unsigned long)(cap[0]) & (unsigned long)0xff000000UL) >> 24) ));
1147 cap[1] = ((unsigned long)(
1148 (((unsigned long)(cap[1]) & (unsigned long)0x000000ffUL) << 24) |
1149 (((unsigned long)(cap[1]) & (unsigned long)0x0000ff00UL) << 8) |
1150 (((unsigned long)(cap[1]) & (unsigned long)0x00ff0000UL) >> 8) |
1151 (((unsigned long)(cap[1]) & (unsigned long)0xff000000UL) >> 24) ));
1153 /* this assumes bigendian! */
1157 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity,*blksz);
1158 dev_desc->lba=*capacity;
1159 dev_desc->blksz=*blksz;
1160 dev_desc->type=perq;
1161 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1162 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1164 init_part(dev_desc);
1166 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1170 #endif /* CONFIG_USB_STORAGE */
1171 #endif /* CFG_CMD_USB */