2 * Most of this source has been derived from the Linux USB
4 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
6 * (c) 1999 Michael Gee (michael@linuxspecific.com)
7 * (c) 2000 Yggdrasil Computing, Inc.
11 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
13 * For BBB support (C) Copyright 2003
14 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
16 * BBB support based on /sys/dev/usb/umass.c from
19 * SPDX-License-Identifier: GPL-2.0+
23 * Currently only the CBI transport protocoll has been implemented, and it
24 * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
25 * transport protocoll may work as well.
29 * Support for USB Mass Storage Devices (BBB) has been added. It has
30 * only been tested with USB memory sticks.
37 #include <asm/byteorder.h>
38 #include <asm/processor.h>
43 #undef BBB_COMDAT_TRACE
44 #undef BBB_XPORT_TRACE
47 /* direction table -- this indicates the direction of the data
48 * transfer for each command code -- a 1 indicates input
50 static const unsigned char us_direction
[256/8] = {
51 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
52 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
56 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
58 static ccb usb_ccb
__attribute__((aligned(ARCH_DMA_MINALIGN
)));
61 #define USB_MAX_STOR_DEV 5
62 static int usb_max_devs
; /* number of highest available usb device */
64 static block_dev_desc_t usb_dev_desc
[USB_MAX_STOR_DEV
];
67 typedef int (*trans_cmnd
)(ccb
*cb
, struct us_data
*data
);
68 typedef int (*trans_reset
)(struct us_data
*data
);
71 struct usb_device
*pusb_dev
; /* this usb_device */
73 unsigned int flags
; /* from filter initially */
74 # define USB_READY (1 << 0)
75 unsigned char ifnum
; /* interface number */
76 unsigned char ep_in
; /* in endpoint */
77 unsigned char ep_out
; /* out ....... */
78 unsigned char ep_int
; /* interrupt . */
79 unsigned char subclass
; /* as in overview */
80 unsigned char protocol
; /* .............. */
81 unsigned char attention_done
; /* force attn on first cmd */
82 unsigned short ip_data
; /* interrupt data */
83 int action
; /* what to do */
84 int ip_wanted
; /* needed */
85 int *irq_handle
; /* for USB int requests */
86 unsigned int irqpipe
; /* pipe for release_irq */
87 unsigned char irqmaxp
; /* max packed for irq Pipe */
88 unsigned char irqinterval
; /* Intervall for IRQ Pipe */
89 ccb
*srb
; /* current srb */
90 trans_reset transport_reset
; /* reset routine */
91 trans_cmnd transport
; /* transport routine */
94 #ifdef CONFIG_USB_EHCI
96 * The U-Boot EHCI driver can handle any transfer length as long as there is
97 * enough free heap space left, but the SCSI READ(10) and WRITE(10) commands are
98 * limited to 65535 blocks.
100 #define USB_MAX_XFER_BLK 65535
102 #define USB_MAX_XFER_BLK 20
105 static struct us_data usb_stor
[USB_MAX_STOR_DEV
];
108 #define USB_STOR_TRANSPORT_GOOD 0
109 #define USB_STOR_TRANSPORT_FAILED -1
110 #define USB_STOR_TRANSPORT_ERROR -2
112 int usb_stor_get_info(struct usb_device
*dev
, struct us_data
*us
,
113 block_dev_desc_t
*dev_desc
);
114 int usb_storage_probe(struct usb_device
*dev
, unsigned int ifnum
,
116 unsigned long usb_stor_read(int device
, lbaint_t blknr
,
117 lbaint_t blkcnt
, void *buffer
);
118 unsigned long usb_stor_write(int device
, lbaint_t blknr
,
119 lbaint_t blkcnt
, const void *buffer
);
120 struct usb_device
* usb_get_dev_index(int index
);
121 void uhci_show_temp_int_td(void);
123 #ifdef CONFIG_PARTITIONS
124 block_dev_desc_t
*usb_stor_get_dev(int index
)
126 return (index
< usb_max_devs
) ? &usb_dev_desc
[index
] : NULL
;
130 static void usb_show_progress(void)
135 /*******************************************************************************
136 * show info on storage devices; 'usb start/init' must be invoked earlier
137 * as we only retrieve structures populated during devices initialization
139 int usb_stor_info(void)
143 if (usb_max_devs
> 0) {
144 for (i
= 0; i
< usb_max_devs
; i
++) {
145 printf(" Device %d: ", i
);
146 dev_print(&usb_dev_desc
[i
]);
151 printf("No storage devices, perhaps not 'usb start'ed..?\n");
155 static unsigned int usb_get_max_lun(struct us_data
*us
)
158 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result
, 1);
159 len
= usb_control_msg(us
->pusb_dev
,
160 usb_rcvctrlpipe(us
->pusb_dev
, 0),
162 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
164 result
, sizeof(char),
165 USB_CNTL_TIMEOUT
* 5);
166 debug("Get Max LUN -> len = %i, result = %i\n", len
, (int) *result
);
167 return (len
> 0) ? *result
: 0;
170 static int usb_storage_register(struct usb_device
*dev
, unsigned char iface
)
172 int lun
, max_lun
, start
= usb_max_devs
;
175 if (!usb_storage_probe(dev
, iface
, &usb_stor
[usb_max_devs
]))
179 * OK, it's a storage device. Iterate over its LUNs
180 * and populate `usb_dev_desc'.
182 max_lun
= usb_get_max_lun(&usb_stor
[usb_max_devs
]);
183 for (lun
= 0; lun
<= max_lun
&& usb_max_devs
< USB_MAX_STOR_DEV
; lun
++) {
184 usb_dev_desc
[usb_max_devs
].lun
= lun
;
185 if (usb_stor_get_info(dev
, &usb_stor
[start
],
186 &usb_dev_desc
[usb_max_devs
]) == 1) {
194 /*******************************************************************************
195 * scan the usb and reports device info
196 * to the user if mode = 1
197 * returns current device or -1 if no
199 int usb_stor_scan(int mode
)
201 unsigned char i
, iface
;
202 struct usb_device
*dev
;
205 printf(" scanning usb for storage devices... ");
207 usb_disable_asynch(1); /* asynch transfer not allowed */
209 for (i
= 0; i
< USB_MAX_STOR_DEV
; i
++) {
210 memset(&usb_dev_desc
[i
], 0, sizeof(block_dev_desc_t
));
211 usb_dev_desc
[i
].if_type
= IF_TYPE_USB
;
212 usb_dev_desc
[i
].dev
= i
;
213 usb_dev_desc
[i
].part_type
= PART_TYPE_UNKNOWN
;
214 usb_dev_desc
[i
].target
= 0xff;
215 usb_dev_desc
[i
].type
= DEV_TYPE_UNKNOWN
;
216 usb_dev_desc
[i
].block_read
= usb_stor_read
;
217 usb_dev_desc
[i
].block_write
= usb_stor_write
;
221 for (i
= 0; i
< USB_MAX_DEVICE
; i
++) {
222 dev
= usb_get_dev_index(i
); /* get device */
225 break; /* no more devices available */
227 for (iface
= 0; iface
< dev
->config
.no_of_if
; iface
++) {
228 usb_max_devs
+= usb_storage_register(dev
, iface
);
231 /* if storage device */
232 if (usb_max_devs
== USB_MAX_STOR_DEV
) {
233 printf("max USB Storage Device reached: %d stopping\n",
239 usb_disable_asynch(0); /* asynch transfer allowed */
240 printf("%d Storage Device(s) found\n", usb_max_devs
);
241 if (usb_max_devs
> 0)
246 static int usb_stor_irq(struct usb_device
*dev
)
249 us
= (struct us_data
*)dev
->privptr
;
259 static void usb_show_srb(ccb
*pccb
)
262 printf("SRB: len %d datalen 0x%lX\n ", pccb
->cmdlen
, pccb
->datalen
);
263 for (i
= 0; i
< 12; i
++)
264 printf("%02X ", pccb
->cmd
[i
]);
268 static void display_int_status(unsigned long tmp
)
270 printf("Status: %s %s %s %s %s %s %s\n",
271 (tmp
& USB_ST_ACTIVE
) ? "Active" : "",
272 (tmp
& USB_ST_STALLED
) ? "Stalled" : "",
273 (tmp
& USB_ST_BUF_ERR
) ? "Buffer Error" : "",
274 (tmp
& USB_ST_BABBLE_DET
) ? "Babble Det" : "",
275 (tmp
& USB_ST_NAK_REC
) ? "NAKed" : "",
276 (tmp
& USB_ST_CRC_ERR
) ? "CRC Error" : "",
277 (tmp
& USB_ST_BIT_ERR
) ? "Bitstuff Error" : "");
280 /***********************************************************************
281 * Data transfer routines
282 ***********************************************************************/
284 static int us_one_transfer(struct us_data
*us
, int pipe
, char *buf
, int length
)
293 /* determine the maximum packet size for these transfers */
294 max_size
= usb_maxpacket(us
->pusb_dev
, pipe
) * 16;
296 /* while we have data left to transfer */
299 /* calculate how long this will be -- maximum or a remainder */
300 this_xfer
= length
> max_size
? max_size
: length
;
303 /* setup the retry counter */
306 /* set up the transfer loop */
308 /* transfer the data */
309 debug("Bulk xfer %p(%d) try #%d\n",
310 buf
, this_xfer
, 11 - maxtry
);
311 result
= usb_bulk_msg(us
->pusb_dev
, pipe
, buf
,
313 USB_CNTL_TIMEOUT
* 5);
314 debug("bulk_msg returned %d xferred %d/%d\n",
315 result
, partial
, this_xfer
);
316 if (us
->pusb_dev
->status
!= 0) {
317 /* if we stall, we need to clear it before
321 display_int_status(us
->pusb_dev
->status
);
323 if (us
->pusb_dev
->status
& USB_ST_STALLED
) {
324 debug("stalled ->clearing endpoint" \
325 "halt for pipe 0x%x\n", pipe
);
326 stat
= us
->pusb_dev
->status
;
327 usb_clear_halt(us
->pusb_dev
, pipe
);
328 us
->pusb_dev
->status
= stat
;
329 if (this_xfer
== partial
) {
330 debug("bulk transferred" \
333 us
->pusb_dev
->status
);
339 if (us
->pusb_dev
->status
& USB_ST_NAK_REC
) {
340 debug("Device NAKed bulk_msg\n");
343 debug("bulk transferred with error");
344 if (this_xfer
== partial
) {
345 debug(" %ld, but data ok\n",
346 us
->pusb_dev
->status
);
349 /* if our try counter reaches 0, bail out */
350 debug(" %ld, data %d\n",
351 us
->pusb_dev
->status
, partial
);
355 /* update to show what data was transferred */
356 this_xfer
-= partial
;
358 /* continue until this transfer is done */
362 /* if we get here, we're done and successful */
366 static int usb_stor_BBB_reset(struct us_data
*us
)
372 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
374 * For Reset Recovery the host shall issue in the following order:
375 * a) a Bulk-Only Mass Storage Reset
376 * b) a Clear Feature HALT to the Bulk-In endpoint
377 * c) a Clear Feature HALT to the Bulk-Out endpoint
379 * This is done in 3 steps.
381 * If the reset doesn't succeed, the device should be port reset.
383 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
385 debug("BBB_reset\n");
386 result
= usb_control_msg(us
->pusb_dev
, usb_sndctrlpipe(us
->pusb_dev
, 0),
388 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
389 0, us
->ifnum
, NULL
, 0, USB_CNTL_TIMEOUT
* 5);
391 if ((result
< 0) && (us
->pusb_dev
->status
& USB_ST_STALLED
)) {
392 debug("RESET:stall\n");
396 /* long wait for reset */
398 debug("BBB_reset result %d: status %lX reset\n",
399 result
, us
->pusb_dev
->status
);
400 pipe
= usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_in
);
401 result
= usb_clear_halt(us
->pusb_dev
, pipe
);
402 /* long wait for reset */
404 debug("BBB_reset result %d: status %lX clearing IN endpoint\n",
405 result
, us
->pusb_dev
->status
);
406 /* long wait for reset */
407 pipe
= usb_sndbulkpipe(us
->pusb_dev
, us
->ep_out
);
408 result
= usb_clear_halt(us
->pusb_dev
, pipe
);
410 debug("BBB_reset result %d: status %lX clearing OUT endpoint\n",
411 result
, us
->pusb_dev
->status
);
412 debug("BBB_reset done\n");
416 /* FIXME: this reset function doesn't really reset the port, and it
417 * should. Actually it should probably do what it's doing here, and
418 * reset the port physically
420 static int usb_stor_CB_reset(struct us_data
*us
)
422 unsigned char cmd
[12];
426 memset(cmd
, 0xff, sizeof(cmd
));
427 cmd
[0] = SCSI_SEND_DIAG
;
429 result
= usb_control_msg(us
->pusb_dev
, usb_sndctrlpipe(us
->pusb_dev
, 0),
431 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
432 0, us
->ifnum
, cmd
, sizeof(cmd
),
433 USB_CNTL_TIMEOUT
* 5);
435 /* long wait for reset */
437 debug("CB_reset result %d: status %lX clearing endpoint halt\n",
438 result
, us
->pusb_dev
->status
);
439 usb_clear_halt(us
->pusb_dev
, usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_in
));
440 usb_clear_halt(us
->pusb_dev
, usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_out
));
442 debug("CB_reset done\n");
447 * Set up the command for a BBB device. Note that the actual SCSI
448 * command is copied into cbw.CBWCDB.
450 static int usb_stor_BBB_comdat(ccb
*srb
, struct us_data
*us
)
456 ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_cbw
, cbw
, 1);
458 dir_in
= US_DIRECTION(srb
->cmd
[0]);
460 #ifdef BBB_COMDAT_TRACE
461 printf("dir %d lun %d cmdlen %d cmd %p datalen %lu pdata %p\n",
462 dir_in
, srb
->lun
, srb
->cmdlen
, srb
->cmd
, srb
->datalen
,
465 for (result
= 0; result
< srb
->cmdlen
; result
++)
466 printf("cmd[%d] %#x ", result
, srb
->cmd
[result
]);
471 if (!(srb
->cmdlen
<= CBWCDBLENGTH
)) {
472 debug("usb_stor_BBB_comdat:cmdlen too large\n");
476 /* always OUT to the ep */
477 pipe
= usb_sndbulkpipe(us
->pusb_dev
, us
->ep_out
);
479 cbw
->dCBWSignature
= cpu_to_le32(CBWSIGNATURE
);
480 cbw
->dCBWTag
= cpu_to_le32(CBWTag
++);
481 cbw
->dCBWDataTransferLength
= cpu_to_le32(srb
->datalen
);
482 cbw
->bCBWFlags
= (dir_in
? CBWFLAGS_IN
: CBWFLAGS_OUT
);
483 cbw
->bCBWLUN
= srb
->lun
;
484 cbw
->bCDBLength
= srb
->cmdlen
;
485 /* copy the command data into the CBW command data buffer */
488 memcpy(cbw
->CBWCDB
, srb
->cmd
, srb
->cmdlen
);
489 result
= usb_bulk_msg(us
->pusb_dev
, pipe
, cbw
, UMASS_BBB_CBW_SIZE
,
490 &actlen
, USB_CNTL_TIMEOUT
* 5);
492 debug("usb_stor_BBB_comdat:usb_bulk_msg error\n");
496 /* FIXME: we also need a CBI_command which sets up the completion
497 * interrupt, and waits for it
499 static int usb_stor_CB_comdat(ccb
*srb
, struct us_data
*us
)
504 unsigned long status
;
507 dir_in
= US_DIRECTION(srb
->cmd
[0]);
510 pipe
= usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_in
);
512 pipe
= usb_sndbulkpipe(us
->pusb_dev
, us
->ep_out
);
515 debug("CBI gets a command: Try %d\n", 5 - retry
);
519 /* let's send the command via the control pipe */
520 result
= usb_control_msg(us
->pusb_dev
,
521 usb_sndctrlpipe(us
->pusb_dev
, 0),
523 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
525 srb
->cmd
, srb
->cmdlen
,
526 USB_CNTL_TIMEOUT
* 5);
527 debug("CB_transport: control msg returned %d, status %lX\n",
528 result
, us
->pusb_dev
->status
);
529 /* check the return code for the command */
531 if (us
->pusb_dev
->status
& USB_ST_STALLED
) {
532 status
= us
->pusb_dev
->status
;
533 debug(" stall during command found," \
535 usb_clear_halt(us
->pusb_dev
,
536 usb_sndctrlpipe(us
->pusb_dev
, 0));
537 us
->pusb_dev
->status
= status
;
539 debug(" error during command %02X" \
540 " Stat = %lX\n", srb
->cmd
[0],
541 us
->pusb_dev
->status
);
544 /* transfer the data payload for this command, if one exists*/
546 debug("CB_transport: control msg returned %d," \
547 " direction is %s to go 0x%lx\n", result
,
548 dir_in
? "IN" : "OUT", srb
->datalen
);
550 result
= us_one_transfer(us
, pipe
, (char *)srb
->pdata
,
552 debug("CBI attempted to transfer data," \
553 " result is %d status %lX, len %d\n",
554 result
, us
->pusb_dev
->status
,
555 us
->pusb_dev
->act_len
);
556 if (!(us
->pusb_dev
->status
& USB_ST_NAK_REC
))
558 } /* if (srb->datalen) */
568 static int usb_stor_CBI_get_status(ccb
*srb
, struct us_data
*us
)
573 submit_int_msg(us
->pusb_dev
, us
->irqpipe
,
574 (void *) &us
->ip_data
, us
->irqmaxp
, us
->irqinterval
);
577 if (us
->ip_wanted
== 0)
582 printf(" Did not get interrupt on CBI\n");
584 return USB_STOR_TRANSPORT_ERROR
;
586 debug("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
587 us
->ip_data
, us
->pusb_dev
->irq_act_len
,
588 us
->pusb_dev
->irq_status
);
589 /* UFI gives us ASC and ASCQ, like a request sense */
590 if (us
->subclass
== US_SC_UFI
) {
591 if (srb
->cmd
[0] == SCSI_REQ_SENSE
||
592 srb
->cmd
[0] == SCSI_INQUIRY
)
593 return USB_STOR_TRANSPORT_GOOD
; /* Good */
594 else if (us
->ip_data
)
595 return USB_STOR_TRANSPORT_FAILED
;
597 return USB_STOR_TRANSPORT_GOOD
;
599 /* otherwise, we interpret the data normally */
600 switch (us
->ip_data
) {
602 return USB_STOR_TRANSPORT_GOOD
;
604 return USB_STOR_TRANSPORT_FAILED
;
606 return USB_STOR_TRANSPORT_ERROR
;
608 return USB_STOR_TRANSPORT_ERROR
;
611 #define USB_TRANSPORT_UNKNOWN_RETRY 5
612 #define USB_TRANSPORT_NOT_READY_RETRY 10
614 /* clear a stall on an endpoint - special for BBB devices */
615 static int usb_stor_BBB_clear_endpt_stall(struct us_data
*us
, __u8 endpt
)
619 /* ENDPOINT_HALT = 0, so set value to 0 */
620 result
= usb_control_msg(us
->pusb_dev
, usb_sndctrlpipe(us
->pusb_dev
, 0),
621 USB_REQ_CLEAR_FEATURE
, USB_RECIP_ENDPOINT
,
622 0, endpt
, NULL
, 0, USB_CNTL_TIMEOUT
* 5);
626 static int usb_stor_BBB_transport(ccb
*srb
, struct us_data
*us
)
630 int actlen
, data_actlen
;
631 unsigned int pipe
, pipein
, pipeout
;
632 ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_csw
, csw
, 1);
633 #ifdef BBB_XPORT_TRACE
638 dir_in
= US_DIRECTION(srb
->cmd
[0]);
641 debug("COMMAND phase\n");
642 result
= usb_stor_BBB_comdat(srb
, us
);
644 debug("failed to send CBW status %ld\n",
645 us
->pusb_dev
->status
);
646 usb_stor_BBB_reset(us
);
647 return USB_STOR_TRANSPORT_FAILED
;
649 if (!(us
->flags
& USB_READY
))
651 pipein
= usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_in
);
652 pipeout
= usb_sndbulkpipe(us
->pusb_dev
, us
->ep_out
);
653 /* DATA phase + error handling */
655 /* no data, go immediately to the STATUS phase */
656 if (srb
->datalen
== 0)
658 debug("DATA phase\n");
664 result
= usb_bulk_msg(us
->pusb_dev
, pipe
, srb
->pdata
, srb
->datalen
,
665 &data_actlen
, USB_CNTL_TIMEOUT
* 5);
666 /* special handling of STALL in DATA phase */
667 if ((result
< 0) && (us
->pusb_dev
->status
& USB_ST_STALLED
)) {
668 debug("DATA:stall\n");
669 /* clear the STALL on the endpoint */
670 result
= usb_stor_BBB_clear_endpt_stall(us
,
671 dir_in
? us
->ep_in
: us
->ep_out
);
673 /* continue on to STATUS phase */
677 debug("usb_bulk_msg error status %ld\n",
678 us
->pusb_dev
->status
);
679 usb_stor_BBB_reset(us
);
680 return USB_STOR_TRANSPORT_FAILED
;
682 #ifdef BBB_XPORT_TRACE
683 for (index
= 0; index
< data_actlen
; index
++)
684 printf("pdata[%d] %#x ", index
, srb
->pdata
[index
]);
687 /* STATUS phase + error handling */
691 debug("STATUS phase\n");
692 result
= usb_bulk_msg(us
->pusb_dev
, pipein
, csw
, UMASS_BBB_CSW_SIZE
,
693 &actlen
, USB_CNTL_TIMEOUT
*5);
695 /* special handling of STALL in STATUS phase */
696 if ((result
< 0) && (retry
< 1) &&
697 (us
->pusb_dev
->status
& USB_ST_STALLED
)) {
698 debug("STATUS:stall\n");
699 /* clear the STALL on the endpoint */
700 result
= usb_stor_BBB_clear_endpt_stall(us
, us
->ep_in
);
701 if (result
>= 0 && (retry
++ < 1))
706 debug("usb_bulk_msg error status %ld\n",
707 us
->pusb_dev
->status
);
708 usb_stor_BBB_reset(us
);
709 return USB_STOR_TRANSPORT_FAILED
;
711 #ifdef BBB_XPORT_TRACE
712 ptr
= (unsigned char *)csw
;
713 for (index
= 0; index
< UMASS_BBB_CSW_SIZE
; index
++)
714 printf("ptr[%d] %#x ", index
, ptr
[index
]);
717 /* misuse pipe to get the residue */
718 pipe
= le32_to_cpu(csw
->dCSWDataResidue
);
719 if (pipe
== 0 && srb
->datalen
!= 0 && srb
->datalen
- data_actlen
!= 0)
720 pipe
= srb
->datalen
- data_actlen
;
721 if (CSWSIGNATURE
!= le32_to_cpu(csw
->dCSWSignature
)) {
722 debug("!CSWSIGNATURE\n");
723 usb_stor_BBB_reset(us
);
724 return USB_STOR_TRANSPORT_FAILED
;
725 } else if ((CBWTag
- 1) != le32_to_cpu(csw
->dCSWTag
)) {
727 usb_stor_BBB_reset(us
);
728 return USB_STOR_TRANSPORT_FAILED
;
729 } else if (csw
->bCSWStatus
> CSWSTATUS_PHASE
) {
731 usb_stor_BBB_reset(us
);
732 return USB_STOR_TRANSPORT_FAILED
;
733 } else if (csw
->bCSWStatus
== CSWSTATUS_PHASE
) {
735 usb_stor_BBB_reset(us
);
736 return USB_STOR_TRANSPORT_FAILED
;
737 } else if (data_actlen
> srb
->datalen
) {
738 debug("transferred %dB instead of %ldB\n",
739 data_actlen
, srb
->datalen
);
740 return USB_STOR_TRANSPORT_FAILED
;
741 } else if (csw
->bCSWStatus
== CSWSTATUS_FAILED
) {
743 return USB_STOR_TRANSPORT_FAILED
;
749 static int usb_stor_CB_transport(ccb
*srb
, struct us_data
*us
)
757 status
= USB_STOR_TRANSPORT_GOOD
;
760 /* issue the command */
762 result
= usb_stor_CB_comdat(srb
, us
);
763 debug("command / Data returned %d, status %lX\n",
764 result
, us
->pusb_dev
->status
);
765 /* if this is an CBI Protocol, get IRQ */
766 if (us
->protocol
== US_PR_CBI
) {
767 status
= usb_stor_CBI_get_status(srb
, us
);
768 /* if the status is error, report it */
769 if (status
== USB_STOR_TRANSPORT_ERROR
) {
770 debug(" USB CBI Command Error\n");
773 srb
->sense_buf
[12] = (unsigned char)(us
->ip_data
>> 8);
774 srb
->sense_buf
[13] = (unsigned char)(us
->ip_data
& 0xff);
776 /* if the status is good, report it */
777 if (status
== USB_STOR_TRANSPORT_GOOD
) {
778 debug(" USB CBI Command Good\n");
783 /* do we have to issue an auto request? */
784 /* HERE we have to check the result */
785 if ((result
< 0) && !(us
->pusb_dev
->status
& USB_ST_STALLED
)) {
786 debug("ERROR %lX\n", us
->pusb_dev
->status
);
787 us
->transport_reset(us
);
788 return USB_STOR_TRANSPORT_ERROR
;
790 if ((us
->protocol
== US_PR_CBI
) &&
791 ((srb
->cmd
[0] == SCSI_REQ_SENSE
) ||
792 (srb
->cmd
[0] == SCSI_INQUIRY
))) {
793 /* do not issue an autorequest after request sense */
794 debug("No auto request and good\n");
795 return USB_STOR_TRANSPORT_GOOD
;
797 /* issue an request_sense */
798 memset(&psrb
->cmd
[0], 0, 12);
799 psrb
->cmd
[0] = SCSI_REQ_SENSE
;
800 psrb
->cmd
[1] = srb
->lun
<< 5;
803 psrb
->pdata
= &srb
->sense_buf
[0];
805 /* issue the command */
806 result
= usb_stor_CB_comdat(psrb
, us
);
807 debug("auto request returned %d\n", result
);
808 /* if this is an CBI Protocol, get IRQ */
809 if (us
->protocol
== US_PR_CBI
)
810 status
= usb_stor_CBI_get_status(psrb
, us
);
812 if ((result
< 0) && !(us
->pusb_dev
->status
& USB_ST_STALLED
)) {
813 debug(" AUTO REQUEST ERROR %ld\n",
814 us
->pusb_dev
->status
);
815 return USB_STOR_TRANSPORT_ERROR
;
817 debug("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
818 srb
->sense_buf
[0], srb
->sense_buf
[2],
819 srb
->sense_buf
[12], srb
->sense_buf
[13]);
820 /* Check the auto request result */
821 if ((srb
->sense_buf
[2] == 0) &&
822 (srb
->sense_buf
[12] == 0) &&
823 (srb
->sense_buf
[13] == 0)) {
825 return USB_STOR_TRANSPORT_GOOD
;
828 /* Check the auto request result */
829 switch (srb
->sense_buf
[2]) {
831 /* Recovered Error */
832 return USB_STOR_TRANSPORT_GOOD
;
836 if (notready
++ > USB_TRANSPORT_NOT_READY_RETRY
) {
837 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
838 " 0x%02X (NOT READY)\n", srb
->cmd
[0],
839 srb
->sense_buf
[0], srb
->sense_buf
[2],
840 srb
->sense_buf
[12], srb
->sense_buf
[13]);
841 return USB_STOR_TRANSPORT_FAILED
;
848 if (retry
++ > USB_TRANSPORT_UNKNOWN_RETRY
) {
849 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
850 " 0x%02X\n", srb
->cmd
[0], srb
->sense_buf
[0],
851 srb
->sense_buf
[2], srb
->sense_buf
[12],
853 return USB_STOR_TRANSPORT_FAILED
;
858 return USB_STOR_TRANSPORT_FAILED
;
862 static int usb_inquiry(ccb
*srb
, struct us_data
*ss
)
867 memset(&srb
->cmd
[0], 0, 12);
868 srb
->cmd
[0] = SCSI_INQUIRY
;
869 srb
->cmd
[1] = srb
->lun
<< 5;
873 i
= ss
->transport(srb
, ss
);
874 debug("inquiry returns %d\n", i
);
880 printf("error in inquiry\n");
886 static int usb_request_sense(ccb
*srb
, struct us_data
*ss
)
890 ptr
= (char *)srb
->pdata
;
891 memset(&srb
->cmd
[0], 0, 12);
892 srb
->cmd
[0] = SCSI_REQ_SENSE
;
893 srb
->cmd
[1] = srb
->lun
<< 5;
896 srb
->pdata
= &srb
->sense_buf
[0];
898 ss
->transport(srb
, ss
);
899 debug("Request Sense returned %02X %02X %02X\n",
900 srb
->sense_buf
[2], srb
->sense_buf
[12],
902 srb
->pdata
= (uchar
*)ptr
;
906 static int usb_test_unit_ready(ccb
*srb
, struct us_data
*ss
)
911 memset(&srb
->cmd
[0], 0, 12);
912 srb
->cmd
[0] = SCSI_TST_U_RDY
;
913 srb
->cmd
[1] = srb
->lun
<< 5;
916 if (ss
->transport(srb
, ss
) == USB_STOR_TRANSPORT_GOOD
) {
917 ss
->flags
|= USB_READY
;
920 usb_request_sense(srb
, ss
);
922 * Check the Key Code Qualifier, if it matches
923 * "Not Ready - medium not present"
924 * (the sense Key equals 0x2 and the ASC is 0x3a)
925 * return immediately as the medium being absent won't change
926 * unless there is a user action.
928 if ((srb
->sense_buf
[2] == 0x02) &&
929 (srb
->sense_buf
[12] == 0x3a))
937 static int usb_read_capacity(ccb
*srb
, struct us_data
*ss
)
943 memset(&srb
->cmd
[0], 0, 12);
944 srb
->cmd
[0] = SCSI_RD_CAPAC
;
945 srb
->cmd
[1] = srb
->lun
<< 5;
948 if (ss
->transport(srb
, ss
) == USB_STOR_TRANSPORT_GOOD
)
955 static int usb_read_10(ccb
*srb
, struct us_data
*ss
, unsigned long start
,
956 unsigned short blocks
)
958 memset(&srb
->cmd
[0], 0, 12);
959 srb
->cmd
[0] = SCSI_READ10
;
960 srb
->cmd
[1] = srb
->lun
<< 5;
961 srb
->cmd
[2] = ((unsigned char) (start
>> 24)) & 0xff;
962 srb
->cmd
[3] = ((unsigned char) (start
>> 16)) & 0xff;
963 srb
->cmd
[4] = ((unsigned char) (start
>> 8)) & 0xff;
964 srb
->cmd
[5] = ((unsigned char) (start
)) & 0xff;
965 srb
->cmd
[7] = ((unsigned char) (blocks
>> 8)) & 0xff;
966 srb
->cmd
[8] = (unsigned char) blocks
& 0xff;
968 debug("read10: start %lx blocks %x\n", start
, blocks
);
969 return ss
->transport(srb
, ss
);
972 static int usb_write_10(ccb
*srb
, struct us_data
*ss
, unsigned long start
,
973 unsigned short blocks
)
975 memset(&srb
->cmd
[0], 0, 12);
976 srb
->cmd
[0] = SCSI_WRITE10
;
977 srb
->cmd
[1] = srb
->lun
<< 5;
978 srb
->cmd
[2] = ((unsigned char) (start
>> 24)) & 0xff;
979 srb
->cmd
[3] = ((unsigned char) (start
>> 16)) & 0xff;
980 srb
->cmd
[4] = ((unsigned char) (start
>> 8)) & 0xff;
981 srb
->cmd
[5] = ((unsigned char) (start
)) & 0xff;
982 srb
->cmd
[7] = ((unsigned char) (blocks
>> 8)) & 0xff;
983 srb
->cmd
[8] = (unsigned char) blocks
& 0xff;
985 debug("write10: start %lx blocks %x\n", start
, blocks
);
986 return ss
->transport(srb
, ss
);
990 #ifdef CONFIG_USB_BIN_FIXUP
992 * Some USB storage devices queried for SCSI identification data respond with
993 * binary strings, which if output to the console freeze the terminal. The
994 * workaround is to modify the vendor and product strings read from such
995 * device with proper values (as reported by 'usb info').
997 * Vendor and product length limits are taken from the definition of
998 * block_dev_desc_t in include/part.h.
1000 static void usb_bin_fixup(struct usb_device_descriptor descriptor
,
1001 unsigned char vendor
[],
1002 unsigned char product
[]) {
1003 const unsigned char max_vendor_len
= 40;
1004 const unsigned char max_product_len
= 20;
1005 if (descriptor
.idVendor
== 0x0424 && descriptor
.idProduct
== 0x223a) {
1006 strncpy((char *)vendor
, "SMSC", max_vendor_len
);
1007 strncpy((char *)product
, "Flash Media Cntrller",
1011 #endif /* CONFIG_USB_BIN_FIXUP */
1013 unsigned long usb_stor_read(int device
, lbaint_t blknr
,
1014 lbaint_t blkcnt
, void *buffer
)
1016 lbaint_t start
, blks
;
1018 unsigned short smallblks
;
1019 struct usb_device
*dev
;
1022 ccb
*srb
= &usb_ccb
;
1029 debug("\nusb_read: dev %d\n", device
);
1030 dev
= usb_dev_desc
[device
].priv
;
1032 debug("%s: No device\n", __func__
);
1035 ss
= (struct us_data
*)dev
->privptr
;
1037 usb_disable_asynch(1); /* asynch transfer not allowed */
1038 srb
->lun
= usb_dev_desc
[device
].lun
;
1039 buf_addr
= (uintptr_t)buffer
;
1043 debug("\nusb_read: dev %d startblk " LBAF
", blccnt " LBAF
1044 " buffer %" PRIxPTR
"\n", device
, start
, blks
, buf_addr
);
1047 /* XXX need some comment here */
1049 srb
->pdata
= (unsigned char *)buf_addr
;
1050 if (blks
> USB_MAX_XFER_BLK
)
1051 smallblks
= USB_MAX_XFER_BLK
;
1053 smallblks
= (unsigned short) blks
;
1055 if (smallblks
== USB_MAX_XFER_BLK
)
1056 usb_show_progress();
1057 srb
->datalen
= usb_dev_desc
[device
].blksz
* smallblks
;
1058 srb
->pdata
= (unsigned char *)buf_addr
;
1059 if (usb_read_10(srb
, ss
, start
, smallblks
)) {
1060 debug("Read ERROR\n");
1061 usb_request_sense(srb
, ss
);
1069 buf_addr
+= srb
->datalen
;
1070 } while (blks
!= 0);
1071 ss
->flags
&= ~USB_READY
;
1073 debug("usb_read: end startblk " LBAF
1074 ", blccnt %x buffer %" PRIxPTR
"\n",
1075 start
, smallblks
, buf_addr
);
1077 usb_disable_asynch(0); /* asynch transfer allowed */
1078 if (blkcnt
>= USB_MAX_XFER_BLK
)
1083 unsigned long usb_stor_write(int device
, lbaint_t blknr
,
1084 lbaint_t blkcnt
, const void *buffer
)
1086 lbaint_t start
, blks
;
1088 unsigned short smallblks
;
1089 struct usb_device
*dev
;
1092 ccb
*srb
= &usb_ccb
;
1099 debug("\nusb_write: dev %d\n", device
);
1100 dev
= usb_dev_desc
[device
].priv
;
1103 ss
= (struct us_data
*)dev
->privptr
;
1105 usb_disable_asynch(1); /* asynch transfer not allowed */
1107 srb
->lun
= usb_dev_desc
[device
].lun
;
1108 buf_addr
= (uintptr_t)buffer
;
1112 debug("\nusb_write: dev %d startblk " LBAF
", blccnt " LBAF
1113 " buffer %" PRIxPTR
"\n", device
, start
, blks
, buf_addr
);
1116 /* If write fails retry for max retry count else
1117 * return with number of blocks written successfully.
1120 srb
->pdata
= (unsigned char *)buf_addr
;
1121 if (blks
> USB_MAX_XFER_BLK
)
1122 smallblks
= USB_MAX_XFER_BLK
;
1124 smallblks
= (unsigned short) blks
;
1126 if (smallblks
== USB_MAX_XFER_BLK
)
1127 usb_show_progress();
1128 srb
->datalen
= usb_dev_desc
[device
].blksz
* smallblks
;
1129 srb
->pdata
= (unsigned char *)buf_addr
;
1130 if (usb_write_10(srb
, ss
, start
, smallblks
)) {
1131 debug("Write ERROR\n");
1132 usb_request_sense(srb
, ss
);
1140 buf_addr
+= srb
->datalen
;
1141 } while (blks
!= 0);
1142 ss
->flags
&= ~USB_READY
;
1144 debug("usb_write: end startblk " LBAF
", blccnt %x buffer %"
1145 PRIxPTR
"\n", start
, smallblks
, buf_addr
);
1147 usb_disable_asynch(0); /* asynch transfer allowed */
1148 if (blkcnt
>= USB_MAX_XFER_BLK
)
1154 /* Probe to see if a new device is actually a Storage device */
1155 int usb_storage_probe(struct usb_device
*dev
, unsigned int ifnum
,
1158 struct usb_interface
*iface
;
1160 struct usb_endpoint_descriptor
*ep_desc
;
1161 unsigned int flags
= 0;
1166 /* let's examine the device now */
1167 iface
= &dev
->config
.if_desc
[ifnum
];
1170 /* this is the place to patch some storage devices */
1171 debug("iVendor %X iProduct %X\n", dev
->descriptor
.idVendor
,
1172 dev
->descriptor
.idProduct
);
1174 if ((dev
->descriptor
.idVendor
) == 0x066b &&
1175 (dev
->descriptor
.idProduct
) == 0x0103) {
1176 debug("patched for E-USB\n");
1177 protocol
= US_PR_CB
;
1178 subclass
= US_SC_UFI
; /* an assumption */
1182 if (dev
->descriptor
.bDeviceClass
!= 0 ||
1183 iface
->desc
.bInterfaceClass
!= USB_CLASS_MASS_STORAGE
||
1184 iface
->desc
.bInterfaceSubClass
< US_SC_MIN
||
1185 iface
->desc
.bInterfaceSubClass
> US_SC_MAX
) {
1186 debug("Not mass storage\n");
1187 /* if it's not a mass storage, we go no further */
1191 memset(ss
, 0, sizeof(struct us_data
));
1193 /* At this point, we know we've got a live one */
1194 debug("\n\nUSB Mass Storage device detected\n");
1196 /* Initialize the us_data structure with some useful info */
1200 ss
->attention_done
= 0;
1202 /* If the device has subclass and protocol, then use that. Otherwise,
1203 * take data from the specific interface.
1206 ss
->subclass
= subclass
;
1207 ss
->protocol
= protocol
;
1209 ss
->subclass
= iface
->desc
.bInterfaceSubClass
;
1210 ss
->protocol
= iface
->desc
.bInterfaceProtocol
;
1213 /* set the handler pointers based on the protocol */
1214 debug("Transport: ");
1215 switch (ss
->protocol
) {
1217 debug("Control/Bulk\n");
1218 ss
->transport
= usb_stor_CB_transport
;
1219 ss
->transport_reset
= usb_stor_CB_reset
;
1223 debug("Control/Bulk/Interrupt\n");
1224 ss
->transport
= usb_stor_CB_transport
;
1225 ss
->transport_reset
= usb_stor_CB_reset
;
1228 debug("Bulk/Bulk/Bulk\n");
1229 ss
->transport
= usb_stor_BBB_transport
;
1230 ss
->transport_reset
= usb_stor_BBB_reset
;
1233 printf("USB Storage Transport unknown / not yet implemented\n");
1239 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1240 * An optional interrupt is OK (necessary for CBI protocol).
1241 * We will ignore any others.
1243 for (i
= 0; i
< iface
->desc
.bNumEndpoints
; i
++) {
1244 ep_desc
= &iface
->ep_desc
[i
];
1245 /* is it an BULK endpoint? */
1246 if ((ep_desc
->bmAttributes
&
1247 USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_BULK
) {
1248 if (ep_desc
->bEndpointAddress
& USB_DIR_IN
)
1249 ss
->ep_in
= ep_desc
->bEndpointAddress
&
1250 USB_ENDPOINT_NUMBER_MASK
;
1253 ep_desc
->bEndpointAddress
&
1254 USB_ENDPOINT_NUMBER_MASK
;
1257 /* is it an interrupt endpoint? */
1258 if ((ep_desc
->bmAttributes
&
1259 USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
) {
1260 ss
->ep_int
= ep_desc
->bEndpointAddress
&
1261 USB_ENDPOINT_NUMBER_MASK
;
1262 ss
->irqinterval
= ep_desc
->bInterval
;
1265 debug("Endpoints In %d Out %d Int %d\n",
1266 ss
->ep_in
, ss
->ep_out
, ss
->ep_int
);
1268 /* Do some basic sanity checks, and bail if we find a problem */
1269 if (usb_set_interface(dev
, iface
->desc
.bInterfaceNumber
, 0) ||
1270 !ss
->ep_in
|| !ss
->ep_out
||
1271 (ss
->protocol
== US_PR_CBI
&& ss
->ep_int
== 0)) {
1272 debug("Problems with device\n");
1275 /* set class specific stuff */
1276 /* We only handle certain protocols. Currently, these are
1278 * The SFF8070 accepts the requests used in u-boot
1280 if (ss
->subclass
!= US_SC_UFI
&& ss
->subclass
!= US_SC_SCSI
&&
1281 ss
->subclass
!= US_SC_8070
) {
1282 printf("Sorry, protocol %d not yet supported.\n", ss
->subclass
);
1286 /* we had found an interrupt endpoint, prepare irq pipe
1287 * set up the IRQ pipe and handler
1289 ss
->irqinterval
= (ss
->irqinterval
> 0) ? ss
->irqinterval
: 255;
1290 ss
->irqpipe
= usb_rcvintpipe(ss
->pusb_dev
, ss
->ep_int
);
1291 ss
->irqmaxp
= usb_maxpacket(dev
, ss
->irqpipe
);
1292 dev
->irq_handle
= usb_stor_irq
;
1294 dev
->privptr
= (void *)ss
;
1298 int usb_stor_get_info(struct usb_device
*dev
, struct us_data
*ss
,
1299 block_dev_desc_t
*dev_desc
)
1301 unsigned char perq
, modi
;
1302 ALLOC_CACHE_ALIGN_BUFFER(u32
, cap
, 2);
1303 ALLOC_CACHE_ALIGN_BUFFER(u8
, usb_stor_buf
, 36);
1304 u32 capacity
, blksz
;
1305 ccb
*pccb
= &usb_ccb
;
1307 pccb
->pdata
= usb_stor_buf
;
1309 dev_desc
->target
= dev
->devnum
;
1310 pccb
->lun
= dev_desc
->lun
;
1311 debug(" address %d\n", dev_desc
->target
);
1313 if (usb_inquiry(pccb
, ss
)) {
1314 debug("%s: usb_inquiry() failed\n", __func__
);
1318 perq
= usb_stor_buf
[0];
1319 modi
= usb_stor_buf
[1];
1322 * Skip unknown devices (0x1f) and enclosure service devices (0x0d),
1323 * they would not respond to test_unit_ready .
1325 if (((perq
& 0x1f) == 0x1f) || ((perq
& 0x1f) == 0x0d)) {
1326 debug("%s: unknown/unsupported device\n", __func__
);
1329 if ((modi
&0x80) == 0x80) {
1330 /* drive is removable */
1331 dev_desc
->removable
= 1;
1333 memcpy(dev_desc
->vendor
, (const void *)&usb_stor_buf
[8], 8);
1334 memcpy(dev_desc
->product
, (const void *)&usb_stor_buf
[16], 16);
1335 memcpy(dev_desc
->revision
, (const void *)&usb_stor_buf
[32], 4);
1336 dev_desc
->vendor
[8] = 0;
1337 dev_desc
->product
[16] = 0;
1338 dev_desc
->revision
[4] = 0;
1339 #ifdef CONFIG_USB_BIN_FIXUP
1340 usb_bin_fixup(dev
->descriptor
, (uchar
*)dev_desc
->vendor
,
1341 (uchar
*)dev_desc
->product
);
1342 #endif /* CONFIG_USB_BIN_FIXUP */
1343 debug("ISO Vers %X, Response Data %X\n", usb_stor_buf
[2],
1345 if (usb_test_unit_ready(pccb
, ss
)) {
1346 printf("Device NOT ready\n"
1347 " Request Sense returned %02X %02X %02X\n",
1348 pccb
->sense_buf
[2], pccb
->sense_buf
[12],
1349 pccb
->sense_buf
[13]);
1350 if (dev_desc
->removable
== 1) {
1351 dev_desc
->type
= perq
;
1356 pccb
->pdata
= (unsigned char *)cap
;
1357 memset(pccb
->pdata
, 0, 8);
1358 if (usb_read_capacity(pccb
, ss
) != 0) {
1359 printf("READ_CAP ERROR\n");
1363 ss
->flags
&= ~USB_READY
;
1364 debug("Read Capacity returns: 0x%08x, 0x%08x\n", cap
[0], cap
[1]);
1366 if (cap
[0] > (0x200000 * 10)) /* greater than 10 GByte */
1369 cap
[0] = cpu_to_be32(cap
[0]);
1370 cap
[1] = cpu_to_be32(cap
[1]);
1373 capacity
= be32_to_cpu(cap
[0]) + 1;
1374 blksz
= be32_to_cpu(cap
[1]);
1376 debug("Capacity = 0x%08x, blocksz = 0x%08x\n", capacity
, blksz
);
1377 dev_desc
->lba
= capacity
;
1378 dev_desc
->blksz
= blksz
;
1379 dev_desc
->log2blksz
= LOG2(dev_desc
->blksz
);
1380 dev_desc
->type
= perq
;
1381 debug(" address %d\n", dev_desc
->target
);
1382 debug("partype: %d\n", dev_desc
->part_type
);
1384 init_part(dev_desc
);
1386 debug("partype: %d\n", dev_desc
->part_type
);