]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/usb_storage.c
Add GPL-2.0+ SPDX-License-Identifier to source files
[people/ms/u-boot.git] / common / usb_storage.c
1 /*
2 * Most of this source has been derived from the Linux USB
3 * project:
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.
8 *
9 *
10 * Adapted for U-Boot:
11 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
12 *
13 * For BBB support (C) Copyright 2003
14 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
15 *
16 * BBB support based on /sys/dev/usb/umass.c from
17 * FreeBSD.
18 *
19 * SPDX-License-Identifier: GPL-2.0+
20 */
21
22 /* Note:
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.
26 */
27 /*
28 * New Note:
29 * Support for USB Mass Storage Devices (BBB) has been added. It has
30 * only been tested with USB memory sticks.
31 */
32
33
34 #include <common.h>
35 #include <command.h>
36 #include <asm/byteorder.h>
37 #include <asm/processor.h>
38
39 #include <part.h>
40 #include <usb.h>
41
42 #undef BBB_COMDAT_TRACE
43 #undef BBB_XPORT_TRACE
44
45 #include <scsi.h>
46 /* direction table -- this indicates the direction of the data
47 * transfer for each command code -- a 1 indicates input
48 */
49 static const unsigned char us_direction[256/8] = {
50 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
51 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
54 };
55 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
56
57 static ccb usb_ccb __attribute__((aligned(ARCH_DMA_MINALIGN)));
58
59 /*
60 * CBI style
61 */
62
63 #define US_CBI_ADSC 0
64
65 /*
66 * BULK only
67 */
68 #define US_BBB_RESET 0xff
69 #define US_BBB_GET_MAX_LUN 0xfe
70
71 /* Command Block Wrapper */
72 typedef struct {
73 __u32 dCBWSignature;
74 # define CBWSIGNATURE 0x43425355
75 __u32 dCBWTag;
76 __u32 dCBWDataTransferLength;
77 __u8 bCBWFlags;
78 # define CBWFLAGS_OUT 0x00
79 # define CBWFLAGS_IN 0x80
80 __u8 bCBWLUN;
81 __u8 bCDBLength;
82 # define CBWCDBLENGTH 16
83 __u8 CBWCDB[CBWCDBLENGTH];
84 } umass_bbb_cbw_t;
85 #define UMASS_BBB_CBW_SIZE 31
86 static __u32 CBWTag;
87
88 /* Command Status Wrapper */
89 typedef struct {
90 __u32 dCSWSignature;
91 # define CSWSIGNATURE 0x53425355
92 __u32 dCSWTag;
93 __u32 dCSWDataResidue;
94 __u8 bCSWStatus;
95 # define CSWSTATUS_GOOD 0x0
96 # define CSWSTATUS_FAILED 0x1
97 # define CSWSTATUS_PHASE 0x2
98 } umass_bbb_csw_t;
99 #define UMASS_BBB_CSW_SIZE 13
100
101 #define USB_MAX_STOR_DEV 5
102 static int usb_max_devs; /* number of highest available usb device */
103
104 static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
105
106 struct us_data;
107 typedef int (*trans_cmnd)(ccb *cb, struct us_data *data);
108 typedef int (*trans_reset)(struct us_data *data);
109
110 struct us_data {
111 struct usb_device *pusb_dev; /* this usb_device */
112
113 unsigned int flags; /* from filter initially */
114 # define USB_READY (1 << 0)
115 unsigned char ifnum; /* interface number */
116 unsigned char ep_in; /* in endpoint */
117 unsigned char ep_out; /* out ....... */
118 unsigned char ep_int; /* interrupt . */
119 unsigned char subclass; /* as in overview */
120 unsigned char protocol; /* .............. */
121 unsigned char attention_done; /* force attn on first cmd */
122 unsigned short ip_data; /* interrupt data */
123 int action; /* what to do */
124 int ip_wanted; /* needed */
125 int *irq_handle; /* for USB int requests */
126 unsigned int irqpipe; /* pipe for release_irq */
127 unsigned char irqmaxp; /* max packed for irq Pipe */
128 unsigned char irqinterval; /* Intervall for IRQ Pipe */
129 ccb *srb; /* current srb */
130 trans_reset transport_reset; /* reset routine */
131 trans_cmnd transport; /* transport routine */
132 };
133
134 #ifdef CONFIG_USB_EHCI
135 /*
136 * The U-Boot EHCI driver can handle any transfer length as long as there is
137 * enough free heap space left, but the SCSI READ(10) and WRITE(10) commands are
138 * limited to 65535 blocks.
139 */
140 #define USB_MAX_XFER_BLK 65535
141 #else
142 #define USB_MAX_XFER_BLK 20
143 #endif
144
145 static struct us_data usb_stor[USB_MAX_STOR_DEV];
146
147
148 #define USB_STOR_TRANSPORT_GOOD 0
149 #define USB_STOR_TRANSPORT_FAILED -1
150 #define USB_STOR_TRANSPORT_ERROR -2
151
152 int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
153 block_dev_desc_t *dev_desc);
154 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
155 struct us_data *ss);
156 unsigned long usb_stor_read(int device, lbaint_t blknr,
157 lbaint_t blkcnt, void *buffer);
158 unsigned long usb_stor_write(int device, lbaint_t blknr,
159 lbaint_t blkcnt, const void *buffer);
160 struct usb_device * usb_get_dev_index(int index);
161 void uhci_show_temp_int_td(void);
162
163 #ifdef CONFIG_PARTITIONS
164 block_dev_desc_t *usb_stor_get_dev(int index)
165 {
166 return (index < usb_max_devs) ? &usb_dev_desc[index] : NULL;
167 }
168 #endif
169
170 static void usb_show_progress(void)
171 {
172 debug(".");
173 }
174
175 /*******************************************************************************
176 * show info on storage devices; 'usb start/init' must be invoked earlier
177 * as we only retrieve structures populated during devices initialization
178 */
179 int usb_stor_info(void)
180 {
181 int i;
182
183 if (usb_max_devs > 0) {
184 for (i = 0; i < usb_max_devs; i++) {
185 printf(" Device %d: ", i);
186 dev_print(&usb_dev_desc[i]);
187 }
188 return 0;
189 }
190
191 printf("No storage devices, perhaps not 'usb start'ed..?\n");
192 return 1;
193 }
194
195 static unsigned int usb_get_max_lun(struct us_data *us)
196 {
197 int len;
198 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
199 len = usb_control_msg(us->pusb_dev,
200 usb_rcvctrlpipe(us->pusb_dev, 0),
201 US_BBB_GET_MAX_LUN,
202 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
203 0, us->ifnum,
204 result, sizeof(char),
205 USB_CNTL_TIMEOUT * 5);
206 debug("Get Max LUN -> len = %i, result = %i\n", len, (int) *result);
207 return (len > 0) ? *result : 0;
208 }
209
210 /*******************************************************************************
211 * scan the usb and reports device info
212 * to the user if mode = 1
213 * returns current device or -1 if no
214 */
215 int usb_stor_scan(int mode)
216 {
217 unsigned char i;
218 struct usb_device *dev;
219
220 if (mode == 1)
221 printf(" scanning usb for storage devices... ");
222
223 usb_disable_asynch(1); /* asynch transfer not allowed */
224
225 for (i = 0; i < USB_MAX_STOR_DEV; i++) {
226 memset(&usb_dev_desc[i], 0, sizeof(block_dev_desc_t));
227 usb_dev_desc[i].if_type = IF_TYPE_USB;
228 usb_dev_desc[i].dev = i;
229 usb_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
230 usb_dev_desc[i].target = 0xff;
231 usb_dev_desc[i].type = DEV_TYPE_UNKNOWN;
232 usb_dev_desc[i].block_read = usb_stor_read;
233 usb_dev_desc[i].block_write = usb_stor_write;
234 }
235
236 usb_max_devs = 0;
237 for (i = 0; i < USB_MAX_DEVICE; i++) {
238 dev = usb_get_dev_index(i); /* get device */
239 debug("i=%d\n", i);
240 if (dev == NULL)
241 break; /* no more devices available */
242
243 if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) {
244 /* OK, it's a storage device. Iterate over its LUNs
245 * and populate `usb_dev_desc'.
246 */
247 int lun, max_lun, start = usb_max_devs;
248
249 max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
250 for (lun = 0;
251 lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
252 lun++) {
253 usb_dev_desc[usb_max_devs].lun = lun;
254 if (usb_stor_get_info(dev, &usb_stor[start],
255 &usb_dev_desc[usb_max_devs]) == 1) {
256 usb_max_devs++;
257 }
258 }
259 }
260 /* if storage device */
261 if (usb_max_devs == USB_MAX_STOR_DEV) {
262 printf("max USB Storage Device reached: %d stopping\n",
263 usb_max_devs);
264 break;
265 }
266 } /* for */
267
268 usb_disable_asynch(0); /* asynch transfer allowed */
269 printf("%d Storage Device(s) found\n", usb_max_devs);
270 if (usb_max_devs > 0)
271 return 0;
272 return -1;
273 }
274
275 static int usb_stor_irq(struct usb_device *dev)
276 {
277 struct us_data *us;
278 us = (struct us_data *)dev->privptr;
279
280 if (us->ip_wanted)
281 us->ip_wanted = 0;
282 return 0;
283 }
284
285
286 #ifdef DEBUG
287
288 static void usb_show_srb(ccb *pccb)
289 {
290 int i;
291 printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
292 for (i = 0; i < 12; i++)
293 printf("%02X ", pccb->cmd[i]);
294 printf("\n");
295 }
296
297 static void display_int_status(unsigned long tmp)
298 {
299 printf("Status: %s %s %s %s %s %s %s\n",
300 (tmp & USB_ST_ACTIVE) ? "Active" : "",
301 (tmp & USB_ST_STALLED) ? "Stalled" : "",
302 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
303 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
304 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
305 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
306 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
307 }
308 #endif
309 /***********************************************************************
310 * Data transfer routines
311 ***********************************************************************/
312
313 static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
314 {
315 int max_size;
316 int this_xfer;
317 int result;
318 int partial;
319 int maxtry;
320 int stat;
321
322 /* determine the maximum packet size for these transfers */
323 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
324
325 /* while we have data left to transfer */
326 while (length) {
327
328 /* calculate how long this will be -- maximum or a remainder */
329 this_xfer = length > max_size ? max_size : length;
330 length -= this_xfer;
331
332 /* setup the retry counter */
333 maxtry = 10;
334
335 /* set up the transfer loop */
336 do {
337 /* transfer the data */
338 debug("Bulk xfer 0x%x(%d) try #%d\n",
339 (unsigned int)buf, this_xfer, 11 - maxtry);
340 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
341 this_xfer, &partial,
342 USB_CNTL_TIMEOUT * 5);
343 debug("bulk_msg returned %d xferred %d/%d\n",
344 result, partial, this_xfer);
345 if (us->pusb_dev->status != 0) {
346 /* if we stall, we need to clear it before
347 * we go on
348 */
349 #ifdef DEBUG
350 display_int_status(us->pusb_dev->status);
351 #endif
352 if (us->pusb_dev->status & USB_ST_STALLED) {
353 debug("stalled ->clearing endpoint" \
354 "halt for pipe 0x%x\n", pipe);
355 stat = us->pusb_dev->status;
356 usb_clear_halt(us->pusb_dev, pipe);
357 us->pusb_dev->status = stat;
358 if (this_xfer == partial) {
359 debug("bulk transferred" \
360 "with error %lX," \
361 " but data ok\n",
362 us->pusb_dev->status);
363 return 0;
364 }
365 else
366 return result;
367 }
368 if (us->pusb_dev->status & USB_ST_NAK_REC) {
369 debug("Device NAKed bulk_msg\n");
370 return result;
371 }
372 debug("bulk transferred with error");
373 if (this_xfer == partial) {
374 debug(" %ld, but data ok\n",
375 us->pusb_dev->status);
376 return 0;
377 }
378 /* if our try counter reaches 0, bail out */
379 debug(" %ld, data %d\n",
380 us->pusb_dev->status, partial);
381 if (!maxtry--)
382 return result;
383 }
384 /* update to show what data was transferred */
385 this_xfer -= partial;
386 buf += partial;
387 /* continue until this transfer is done */
388 } while (this_xfer);
389 }
390
391 /* if we get here, we're done and successful */
392 return 0;
393 }
394
395 static int usb_stor_BBB_reset(struct us_data *us)
396 {
397 int result;
398 unsigned int pipe;
399
400 /*
401 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
402 *
403 * For Reset Recovery the host shall issue in the following order:
404 * a) a Bulk-Only Mass Storage Reset
405 * b) a Clear Feature HALT to the Bulk-In endpoint
406 * c) a Clear Feature HALT to the Bulk-Out endpoint
407 *
408 * This is done in 3 steps.
409 *
410 * If the reset doesn't succeed, the device should be port reset.
411 *
412 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
413 */
414 debug("BBB_reset\n");
415 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
416 US_BBB_RESET,
417 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
418 0, us->ifnum, NULL, 0, USB_CNTL_TIMEOUT * 5);
419
420 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
421 debug("RESET:stall\n");
422 return -1;
423 }
424
425 /* long wait for reset */
426 mdelay(150);
427 debug("BBB_reset result %d: status %lX reset\n",
428 result, us->pusb_dev->status);
429 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
430 result = usb_clear_halt(us->pusb_dev, pipe);
431 /* long wait for reset */
432 mdelay(150);
433 debug("BBB_reset result %d: status %lX clearing IN endpoint\n",
434 result, us->pusb_dev->status);
435 /* long wait for reset */
436 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
437 result = usb_clear_halt(us->pusb_dev, pipe);
438 mdelay(150);
439 debug("BBB_reset result %d: status %lX clearing OUT endpoint\n",
440 result, us->pusb_dev->status);
441 debug("BBB_reset done\n");
442 return 0;
443 }
444
445 /* FIXME: this reset function doesn't really reset the port, and it
446 * should. Actually it should probably do what it's doing here, and
447 * reset the port physically
448 */
449 static int usb_stor_CB_reset(struct us_data *us)
450 {
451 unsigned char cmd[12];
452 int result;
453
454 debug("CB_reset\n");
455 memset(cmd, 0xff, sizeof(cmd));
456 cmd[0] = SCSI_SEND_DIAG;
457 cmd[1] = 4;
458 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
459 US_CBI_ADSC,
460 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
461 0, us->ifnum, cmd, sizeof(cmd),
462 USB_CNTL_TIMEOUT * 5);
463
464 /* long wait for reset */
465 mdelay(1500);
466 debug("CB_reset result %d: status %lX clearing endpoint halt\n",
467 result, us->pusb_dev->status);
468 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
469 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
470
471 debug("CB_reset done\n");
472 return 0;
473 }
474
475 /*
476 * Set up the command for a BBB device. Note that the actual SCSI
477 * command is copied into cbw.CBWCDB.
478 */
479 static int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
480 {
481 int result;
482 int actlen;
483 int dir_in;
484 unsigned int pipe;
485 ALLOC_CACHE_ALIGN_BUFFER(umass_bbb_cbw_t, cbw, 1);
486
487 dir_in = US_DIRECTION(srb->cmd[0]);
488
489 #ifdef BBB_COMDAT_TRACE
490 printf("dir %d lun %d cmdlen %d cmd %p datalen %lu pdata %p\n",
491 dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
492 srb->pdata);
493 if (srb->cmdlen) {
494 for (result = 0; result < srb->cmdlen; result++)
495 printf("cmd[%d] %#x ", result, srb->cmd[result]);
496 printf("\n");
497 }
498 #endif
499 /* sanity checks */
500 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
501 debug("usb_stor_BBB_comdat:cmdlen too large\n");
502 return -1;
503 }
504
505 /* always OUT to the ep */
506 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
507
508 cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE);
509 cbw->dCBWTag = cpu_to_le32(CBWTag++);
510 cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen);
511 cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
512 cbw->bCBWLUN = srb->lun;
513 cbw->bCDBLength = srb->cmdlen;
514 /* copy the command data into the CBW command data buffer */
515 /* DST SRC LEN!!! */
516 memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
517 result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
518 &actlen, USB_CNTL_TIMEOUT * 5);
519 if (result < 0)
520 debug("usb_stor_BBB_comdat:usb_bulk_msg error\n");
521 return result;
522 }
523
524 /* FIXME: we also need a CBI_command which sets up the completion
525 * interrupt, and waits for it
526 */
527 static int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
528 {
529 int result = 0;
530 int dir_in, retry;
531 unsigned int pipe;
532 unsigned long status;
533
534 retry = 5;
535 dir_in = US_DIRECTION(srb->cmd[0]);
536
537 if (dir_in)
538 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
539 else
540 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
541
542 while (retry--) {
543 debug("CBI gets a command: Try %d\n", 5 - retry);
544 #ifdef DEBUG
545 usb_show_srb(srb);
546 #endif
547 /* let's send the command via the control pipe */
548 result = usb_control_msg(us->pusb_dev,
549 usb_sndctrlpipe(us->pusb_dev , 0),
550 US_CBI_ADSC,
551 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
552 0, us->ifnum,
553 srb->cmd, srb->cmdlen,
554 USB_CNTL_TIMEOUT * 5);
555 debug("CB_transport: control msg returned %d, status %lX\n",
556 result, us->pusb_dev->status);
557 /* check the return code for the command */
558 if (result < 0) {
559 if (us->pusb_dev->status & USB_ST_STALLED) {
560 status = us->pusb_dev->status;
561 debug(" stall during command found," \
562 " clear pipe\n");
563 usb_clear_halt(us->pusb_dev,
564 usb_sndctrlpipe(us->pusb_dev, 0));
565 us->pusb_dev->status = status;
566 }
567 debug(" error during command %02X" \
568 " Stat = %lX\n", srb->cmd[0],
569 us->pusb_dev->status);
570 return result;
571 }
572 /* transfer the data payload for this command, if one exists*/
573
574 debug("CB_transport: control msg returned %d," \
575 " direction is %s to go 0x%lx\n", result,
576 dir_in ? "IN" : "OUT", srb->datalen);
577 if (srb->datalen) {
578 result = us_one_transfer(us, pipe, (char *)srb->pdata,
579 srb->datalen);
580 debug("CBI attempted to transfer data," \
581 " result is %d status %lX, len %d\n",
582 result, us->pusb_dev->status,
583 us->pusb_dev->act_len);
584 if (!(us->pusb_dev->status & USB_ST_NAK_REC))
585 break;
586 } /* if (srb->datalen) */
587 else
588 break;
589 }
590 /* return result */
591
592 return result;
593 }
594
595
596 static int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
597 {
598 int timeout;
599
600 us->ip_wanted = 1;
601 submit_int_msg(us->pusb_dev, us->irqpipe,
602 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
603 timeout = 1000;
604 while (timeout--) {
605 if ((volatile int *) us->ip_wanted == NULL)
606 break;
607 mdelay(10);
608 }
609 if (us->ip_wanted) {
610 printf(" Did not get interrupt on CBI\n");
611 us->ip_wanted = 0;
612 return USB_STOR_TRANSPORT_ERROR;
613 }
614 debug("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
615 us->ip_data, us->pusb_dev->irq_act_len,
616 us->pusb_dev->irq_status);
617 /* UFI gives us ASC and ASCQ, like a request sense */
618 if (us->subclass == US_SC_UFI) {
619 if (srb->cmd[0] == SCSI_REQ_SENSE ||
620 srb->cmd[0] == SCSI_INQUIRY)
621 return USB_STOR_TRANSPORT_GOOD; /* Good */
622 else if (us->ip_data)
623 return USB_STOR_TRANSPORT_FAILED;
624 else
625 return USB_STOR_TRANSPORT_GOOD;
626 }
627 /* otherwise, we interpret the data normally */
628 switch (us->ip_data) {
629 case 0x0001:
630 return USB_STOR_TRANSPORT_GOOD;
631 case 0x0002:
632 return USB_STOR_TRANSPORT_FAILED;
633 default:
634 return USB_STOR_TRANSPORT_ERROR;
635 } /* switch */
636 return USB_STOR_TRANSPORT_ERROR;
637 }
638
639 #define USB_TRANSPORT_UNKNOWN_RETRY 5
640 #define USB_TRANSPORT_NOT_READY_RETRY 10
641
642 /* clear a stall on an endpoint - special for BBB devices */
643 static int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
644 {
645 int result;
646
647 /* ENDPOINT_HALT = 0, so set value to 0 */
648 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
649 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
650 0, endpt, NULL, 0, USB_CNTL_TIMEOUT * 5);
651 return result;
652 }
653
654 static int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
655 {
656 int result, retry;
657 int dir_in;
658 int actlen, data_actlen;
659 unsigned int pipe, pipein, pipeout;
660 ALLOC_CACHE_ALIGN_BUFFER(umass_bbb_csw_t, csw, 1);
661 #ifdef BBB_XPORT_TRACE
662 unsigned char *ptr;
663 int index;
664 #endif
665
666 dir_in = US_DIRECTION(srb->cmd[0]);
667
668 /* COMMAND phase */
669 debug("COMMAND phase\n");
670 result = usb_stor_BBB_comdat(srb, us);
671 if (result < 0) {
672 debug("failed to send CBW status %ld\n",
673 us->pusb_dev->status);
674 usb_stor_BBB_reset(us);
675 return USB_STOR_TRANSPORT_FAILED;
676 }
677 if (!(us->flags & USB_READY))
678 mdelay(5);
679 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
680 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
681 /* DATA phase + error handling */
682 data_actlen = 0;
683 /* no data, go immediately to the STATUS phase */
684 if (srb->datalen == 0)
685 goto st;
686 debug("DATA phase\n");
687 if (dir_in)
688 pipe = pipein;
689 else
690 pipe = pipeout;
691 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
692 &data_actlen, USB_CNTL_TIMEOUT * 5);
693 /* special handling of STALL in DATA phase */
694 if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
695 debug("DATA:stall\n");
696 /* clear the STALL on the endpoint */
697 result = usb_stor_BBB_clear_endpt_stall(us,
698 dir_in ? us->ep_in : us->ep_out);
699 if (result >= 0)
700 /* continue on to STATUS phase */
701 goto st;
702 }
703 if (result < 0) {
704 debug("usb_bulk_msg error status %ld\n",
705 us->pusb_dev->status);
706 usb_stor_BBB_reset(us);
707 return USB_STOR_TRANSPORT_FAILED;
708 }
709 #ifdef BBB_XPORT_TRACE
710 for (index = 0; index < data_actlen; index++)
711 printf("pdata[%d] %#x ", index, srb->pdata[index]);
712 printf("\n");
713 #endif
714 /* STATUS phase + error handling */
715 st:
716 retry = 0;
717 again:
718 debug("STATUS phase\n");
719 result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE,
720 &actlen, USB_CNTL_TIMEOUT*5);
721
722 /* special handling of STALL in STATUS phase */
723 if ((result < 0) && (retry < 1) &&
724 (us->pusb_dev->status & USB_ST_STALLED)) {
725 debug("STATUS:stall\n");
726 /* clear the STALL on the endpoint */
727 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
728 if (result >= 0 && (retry++ < 1))
729 /* do a retry */
730 goto again;
731 }
732 if (result < 0) {
733 debug("usb_bulk_msg error status %ld\n",
734 us->pusb_dev->status);
735 usb_stor_BBB_reset(us);
736 return USB_STOR_TRANSPORT_FAILED;
737 }
738 #ifdef BBB_XPORT_TRACE
739 ptr = (unsigned char *)csw;
740 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
741 printf("ptr[%d] %#x ", index, ptr[index]);
742 printf("\n");
743 #endif
744 /* misuse pipe to get the residue */
745 pipe = le32_to_cpu(csw->dCSWDataResidue);
746 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
747 pipe = srb->datalen - data_actlen;
748 if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) {
749 debug("!CSWSIGNATURE\n");
750 usb_stor_BBB_reset(us);
751 return USB_STOR_TRANSPORT_FAILED;
752 } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) {
753 debug("!Tag\n");
754 usb_stor_BBB_reset(us);
755 return USB_STOR_TRANSPORT_FAILED;
756 } else if (csw->bCSWStatus > CSWSTATUS_PHASE) {
757 debug(">PHASE\n");
758 usb_stor_BBB_reset(us);
759 return USB_STOR_TRANSPORT_FAILED;
760 } else if (csw->bCSWStatus == CSWSTATUS_PHASE) {
761 debug("=PHASE\n");
762 usb_stor_BBB_reset(us);
763 return USB_STOR_TRANSPORT_FAILED;
764 } else if (data_actlen > srb->datalen) {
765 debug("transferred %dB instead of %ldB\n",
766 data_actlen, srb->datalen);
767 return USB_STOR_TRANSPORT_FAILED;
768 } else if (csw->bCSWStatus == CSWSTATUS_FAILED) {
769 debug("FAILED\n");
770 return USB_STOR_TRANSPORT_FAILED;
771 }
772
773 return result;
774 }
775
776 static int usb_stor_CB_transport(ccb *srb, struct us_data *us)
777 {
778 int result, status;
779 ccb *psrb;
780 ccb reqsrb;
781 int retry, notready;
782
783 psrb = &reqsrb;
784 status = USB_STOR_TRANSPORT_GOOD;
785 retry = 0;
786 notready = 0;
787 /* issue the command */
788 do_retry:
789 result = usb_stor_CB_comdat(srb, us);
790 debug("command / Data returned %d, status %lX\n",
791 result, us->pusb_dev->status);
792 /* if this is an CBI Protocol, get IRQ */
793 if (us->protocol == US_PR_CBI) {
794 status = usb_stor_CBI_get_status(srb, us);
795 /* if the status is error, report it */
796 if (status == USB_STOR_TRANSPORT_ERROR) {
797 debug(" USB CBI Command Error\n");
798 return status;
799 }
800 srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
801 srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
802 if (!us->ip_data) {
803 /* if the status is good, report it */
804 if (status == USB_STOR_TRANSPORT_GOOD) {
805 debug(" USB CBI Command Good\n");
806 return status;
807 }
808 }
809 }
810 /* do we have to issue an auto request? */
811 /* HERE we have to check the result */
812 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
813 debug("ERROR %lX\n", us->pusb_dev->status);
814 us->transport_reset(us);
815 return USB_STOR_TRANSPORT_ERROR;
816 }
817 if ((us->protocol == US_PR_CBI) &&
818 ((srb->cmd[0] == SCSI_REQ_SENSE) ||
819 (srb->cmd[0] == SCSI_INQUIRY))) {
820 /* do not issue an autorequest after request sense */
821 debug("No auto request and good\n");
822 return USB_STOR_TRANSPORT_GOOD;
823 }
824 /* issue an request_sense */
825 memset(&psrb->cmd[0], 0, 12);
826 psrb->cmd[0] = SCSI_REQ_SENSE;
827 psrb->cmd[1] = srb->lun << 5;
828 psrb->cmd[4] = 18;
829 psrb->datalen = 18;
830 psrb->pdata = &srb->sense_buf[0];
831 psrb->cmdlen = 12;
832 /* issue the command */
833 result = usb_stor_CB_comdat(psrb, us);
834 debug("auto request returned %d\n", result);
835 /* if this is an CBI Protocol, get IRQ */
836 if (us->protocol == US_PR_CBI)
837 status = usb_stor_CBI_get_status(psrb, us);
838
839 if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
840 debug(" AUTO REQUEST ERROR %ld\n",
841 us->pusb_dev->status);
842 return USB_STOR_TRANSPORT_ERROR;
843 }
844 debug("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
845 srb->sense_buf[0], srb->sense_buf[2],
846 srb->sense_buf[12], srb->sense_buf[13]);
847 /* Check the auto request result */
848 if ((srb->sense_buf[2] == 0) &&
849 (srb->sense_buf[12] == 0) &&
850 (srb->sense_buf[13] == 0)) {
851 /* ok, no sense */
852 return USB_STOR_TRANSPORT_GOOD;
853 }
854
855 /* Check the auto request result */
856 switch (srb->sense_buf[2]) {
857 case 0x01:
858 /* Recovered Error */
859 return USB_STOR_TRANSPORT_GOOD;
860 break;
861 case 0x02:
862 /* Not Ready */
863 if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
864 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
865 " 0x%02X (NOT READY)\n", srb->cmd[0],
866 srb->sense_buf[0], srb->sense_buf[2],
867 srb->sense_buf[12], srb->sense_buf[13]);
868 return USB_STOR_TRANSPORT_FAILED;
869 } else {
870 mdelay(100);
871 goto do_retry;
872 }
873 break;
874 default:
875 if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
876 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
877 " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
878 srb->sense_buf[2], srb->sense_buf[12],
879 srb->sense_buf[13]);
880 return USB_STOR_TRANSPORT_FAILED;
881 } else
882 goto do_retry;
883 break;
884 }
885 return USB_STOR_TRANSPORT_FAILED;
886 }
887
888
889 static int usb_inquiry(ccb *srb, struct us_data *ss)
890 {
891 int retry, i;
892 retry = 5;
893 do {
894 memset(&srb->cmd[0], 0, 12);
895 srb->cmd[0] = SCSI_INQUIRY;
896 srb->cmd[1] = srb->lun << 5;
897 srb->cmd[4] = 36;
898 srb->datalen = 36;
899 srb->cmdlen = 12;
900 i = ss->transport(srb, ss);
901 debug("inquiry returns %d\n", i);
902 if (i == 0)
903 break;
904 } while (--retry);
905
906 if (!retry) {
907 printf("error in inquiry\n");
908 return -1;
909 }
910 return 0;
911 }
912
913 static int usb_request_sense(ccb *srb, struct us_data *ss)
914 {
915 char *ptr;
916
917 ptr = (char *)srb->pdata;
918 memset(&srb->cmd[0], 0, 12);
919 srb->cmd[0] = SCSI_REQ_SENSE;
920 srb->cmd[1] = srb->lun << 5;
921 srb->cmd[4] = 18;
922 srb->datalen = 18;
923 srb->pdata = &srb->sense_buf[0];
924 srb->cmdlen = 12;
925 ss->transport(srb, ss);
926 debug("Request Sense returned %02X %02X %02X\n",
927 srb->sense_buf[2], srb->sense_buf[12],
928 srb->sense_buf[13]);
929 srb->pdata = (uchar *)ptr;
930 return 0;
931 }
932
933 static int usb_test_unit_ready(ccb *srb, struct us_data *ss)
934 {
935 int retries = 10;
936
937 do {
938 memset(&srb->cmd[0], 0, 12);
939 srb->cmd[0] = SCSI_TST_U_RDY;
940 srb->cmd[1] = srb->lun << 5;
941 srb->datalen = 0;
942 srb->cmdlen = 12;
943 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD) {
944 ss->flags |= USB_READY;
945 return 0;
946 }
947 usb_request_sense(srb, ss);
948 /*
949 * Check the Key Code Qualifier, if it matches
950 * "Not Ready - medium not present"
951 * (the sense Key equals 0x2 and the ASC is 0x3a)
952 * return immediately as the medium being absent won't change
953 * unless there is a user action.
954 */
955 if ((srb->sense_buf[2] == 0x02) &&
956 (srb->sense_buf[12] == 0x3a))
957 return -1;
958 mdelay(100);
959 } while (retries--);
960
961 return -1;
962 }
963
964 static int usb_read_capacity(ccb *srb, struct us_data *ss)
965 {
966 int retry;
967 /* XXX retries */
968 retry = 3;
969 do {
970 memset(&srb->cmd[0], 0, 12);
971 srb->cmd[0] = SCSI_RD_CAPAC;
972 srb->cmd[1] = srb->lun << 5;
973 srb->datalen = 8;
974 srb->cmdlen = 12;
975 if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
976 return 0;
977 } while (retry--);
978
979 return -1;
980 }
981
982 static int usb_read_10(ccb *srb, struct us_data *ss, unsigned long start,
983 unsigned short blocks)
984 {
985 memset(&srb->cmd[0], 0, 12);
986 srb->cmd[0] = SCSI_READ10;
987 srb->cmd[1] = srb->lun << 5;
988 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
989 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
990 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
991 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
992 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
993 srb->cmd[8] = (unsigned char) blocks & 0xff;
994 srb->cmdlen = 12;
995 debug("read10: start %lx blocks %x\n", start, blocks);
996 return ss->transport(srb, ss);
997 }
998
999 static int usb_write_10(ccb *srb, struct us_data *ss, unsigned long start,
1000 unsigned short blocks)
1001 {
1002 memset(&srb->cmd[0], 0, 12);
1003 srb->cmd[0] = SCSI_WRITE10;
1004 srb->cmd[1] = srb->lun << 5;
1005 srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
1006 srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
1007 srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
1008 srb->cmd[5] = ((unsigned char) (start)) & 0xff;
1009 srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
1010 srb->cmd[8] = (unsigned char) blocks & 0xff;
1011 srb->cmdlen = 12;
1012 debug("write10: start %lx blocks %x\n", start, blocks);
1013 return ss->transport(srb, ss);
1014 }
1015
1016
1017 #ifdef CONFIG_USB_BIN_FIXUP
1018 /*
1019 * Some USB storage devices queried for SCSI identification data respond with
1020 * binary strings, which if output to the console freeze the terminal. The
1021 * workaround is to modify the vendor and product strings read from such
1022 * device with proper values (as reported by 'usb info').
1023 *
1024 * Vendor and product length limits are taken from the definition of
1025 * block_dev_desc_t in include/part.h.
1026 */
1027 static void usb_bin_fixup(struct usb_device_descriptor descriptor,
1028 unsigned char vendor[],
1029 unsigned char product[]) {
1030 const unsigned char max_vendor_len = 40;
1031 const unsigned char max_product_len = 20;
1032 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
1033 strncpy((char *)vendor, "SMSC", max_vendor_len);
1034 strncpy((char *)product, "Flash Media Cntrller",
1035 max_product_len);
1036 }
1037 }
1038 #endif /* CONFIG_USB_BIN_FIXUP */
1039
1040 unsigned long usb_stor_read(int device, lbaint_t blknr,
1041 lbaint_t blkcnt, void *buffer)
1042 {
1043 lbaint_t start, blks;
1044 uintptr_t buf_addr;
1045 unsigned short smallblks;
1046 struct usb_device *dev;
1047 struct us_data *ss;
1048 int retry, i;
1049 ccb *srb = &usb_ccb;
1050
1051 if (blkcnt == 0)
1052 return 0;
1053
1054 device &= 0xff;
1055 /* Setup device */
1056 debug("\nusb_read: dev %d \n", device);
1057 dev = NULL;
1058 for (i = 0; i < USB_MAX_DEVICE; i++) {
1059 dev = usb_get_dev_index(i);
1060 if (dev == NULL)
1061 return 0;
1062 if (dev->devnum == usb_dev_desc[device].target)
1063 break;
1064 }
1065 ss = (struct us_data *)dev->privptr;
1066
1067 usb_disable_asynch(1); /* asynch transfer not allowed */
1068 srb->lun = usb_dev_desc[device].lun;
1069 buf_addr = (unsigned long)buffer;
1070 start = blknr;
1071 blks = blkcnt;
1072
1073 debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF
1074 " buffer %lx\n", device, start, blks, buf_addr);
1075
1076 do {
1077 /* XXX need some comment here */
1078 retry = 2;
1079 srb->pdata = (unsigned char *)buf_addr;
1080 if (blks > USB_MAX_XFER_BLK)
1081 smallblks = USB_MAX_XFER_BLK;
1082 else
1083 smallblks = (unsigned short) blks;
1084 retry_it:
1085 if (smallblks == USB_MAX_XFER_BLK)
1086 usb_show_progress();
1087 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1088 srb->pdata = (unsigned char *)buf_addr;
1089 if (usb_read_10(srb, ss, start, smallblks)) {
1090 debug("Read ERROR\n");
1091 usb_request_sense(srb, ss);
1092 if (retry--)
1093 goto retry_it;
1094 blkcnt -= blks;
1095 break;
1096 }
1097 start += smallblks;
1098 blks -= smallblks;
1099 buf_addr += srb->datalen;
1100 } while (blks != 0);
1101 ss->flags &= ~USB_READY;
1102
1103 debug("usb_read: end startblk " LBAF
1104 ", blccnt %x buffer %lx\n",
1105 start, smallblks, buf_addr);
1106
1107 usb_disable_asynch(0); /* asynch transfer allowed */
1108 if (blkcnt >= USB_MAX_XFER_BLK)
1109 debug("\n");
1110 return blkcnt;
1111 }
1112
1113 unsigned long usb_stor_write(int device, lbaint_t blknr,
1114 lbaint_t blkcnt, const void *buffer)
1115 {
1116 lbaint_t start, blks;
1117 uintptr_t buf_addr;
1118 unsigned short smallblks;
1119 struct usb_device *dev;
1120 struct us_data *ss;
1121 int retry, i;
1122 ccb *srb = &usb_ccb;
1123
1124 if (blkcnt == 0)
1125 return 0;
1126
1127 device &= 0xff;
1128 /* Setup device */
1129 debug("\nusb_write: dev %d \n", device);
1130 dev = NULL;
1131 for (i = 0; i < USB_MAX_DEVICE; i++) {
1132 dev = usb_get_dev_index(i);
1133 if (dev == NULL)
1134 return 0;
1135 if (dev->devnum == usb_dev_desc[device].target)
1136 break;
1137 }
1138 ss = (struct us_data *)dev->privptr;
1139
1140 usb_disable_asynch(1); /* asynch transfer not allowed */
1141
1142 srb->lun = usb_dev_desc[device].lun;
1143 buf_addr = (unsigned long)buffer;
1144 start = blknr;
1145 blks = blkcnt;
1146
1147 debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF
1148 " buffer %lx\n", device, start, blks, buf_addr);
1149
1150 do {
1151 /* If write fails retry for max retry count else
1152 * return with number of blocks written successfully.
1153 */
1154 retry = 2;
1155 srb->pdata = (unsigned char *)buf_addr;
1156 if (blks > USB_MAX_XFER_BLK)
1157 smallblks = USB_MAX_XFER_BLK;
1158 else
1159 smallblks = (unsigned short) blks;
1160 retry_it:
1161 if (smallblks == USB_MAX_XFER_BLK)
1162 usb_show_progress();
1163 srb->datalen = usb_dev_desc[device].blksz * smallblks;
1164 srb->pdata = (unsigned char *)buf_addr;
1165 if (usb_write_10(srb, ss, start, smallblks)) {
1166 debug("Write ERROR\n");
1167 usb_request_sense(srb, ss);
1168 if (retry--)
1169 goto retry_it;
1170 blkcnt -= blks;
1171 break;
1172 }
1173 start += smallblks;
1174 blks -= smallblks;
1175 buf_addr += srb->datalen;
1176 } while (blks != 0);
1177 ss->flags &= ~USB_READY;
1178
1179 debug("usb_write: end startblk " LBAF ", blccnt %x buffer %lx\n",
1180 start, smallblks, buf_addr);
1181
1182 usb_disable_asynch(0); /* asynch transfer allowed */
1183 if (blkcnt >= USB_MAX_XFER_BLK)
1184 debug("\n");
1185 return blkcnt;
1186
1187 }
1188
1189 /* Probe to see if a new device is actually a Storage device */
1190 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
1191 struct us_data *ss)
1192 {
1193 struct usb_interface *iface;
1194 int i;
1195 struct usb_endpoint_descriptor *ep_desc;
1196 unsigned int flags = 0;
1197
1198 int protocol = 0;
1199 int subclass = 0;
1200
1201 /* let's examine the device now */
1202 iface = &dev->config.if_desc[ifnum];
1203
1204 #if 0
1205 /* this is the place to patch some storage devices */
1206 debug("iVendor %X iProduct %X\n", dev->descriptor.idVendor,
1207 dev->descriptor.idProduct);
1208
1209 if ((dev->descriptor.idVendor) == 0x066b &&
1210 (dev->descriptor.idProduct) == 0x0103) {
1211 debug("patched for E-USB\n");
1212 protocol = US_PR_CB;
1213 subclass = US_SC_UFI; /* an assumption */
1214 }
1215 #endif
1216
1217 if (dev->descriptor.bDeviceClass != 0 ||
1218 iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1219 iface->desc.bInterfaceSubClass < US_SC_MIN ||
1220 iface->desc.bInterfaceSubClass > US_SC_MAX) {
1221 /* if it's not a mass storage, we go no further */
1222 return 0;
1223 }
1224
1225 memset(ss, 0, sizeof(struct us_data));
1226
1227 /* At this point, we know we've got a live one */
1228 debug("\n\nUSB Mass Storage device detected\n");
1229
1230 /* Initialize the us_data structure with some useful info */
1231 ss->flags = flags;
1232 ss->ifnum = ifnum;
1233 ss->pusb_dev = dev;
1234 ss->attention_done = 0;
1235
1236 /* If the device has subclass and protocol, then use that. Otherwise,
1237 * take data from the specific interface.
1238 */
1239 if (subclass) {
1240 ss->subclass = subclass;
1241 ss->protocol = protocol;
1242 } else {
1243 ss->subclass = iface->desc.bInterfaceSubClass;
1244 ss->protocol = iface->desc.bInterfaceProtocol;
1245 }
1246
1247 /* set the handler pointers based on the protocol */
1248 debug("Transport: ");
1249 switch (ss->protocol) {
1250 case US_PR_CB:
1251 debug("Control/Bulk\n");
1252 ss->transport = usb_stor_CB_transport;
1253 ss->transport_reset = usb_stor_CB_reset;
1254 break;
1255
1256 case US_PR_CBI:
1257 debug("Control/Bulk/Interrupt\n");
1258 ss->transport = usb_stor_CB_transport;
1259 ss->transport_reset = usb_stor_CB_reset;
1260 break;
1261 case US_PR_BULK:
1262 debug("Bulk/Bulk/Bulk\n");
1263 ss->transport = usb_stor_BBB_transport;
1264 ss->transport_reset = usb_stor_BBB_reset;
1265 break;
1266 default:
1267 printf("USB Storage Transport unknown / not yet implemented\n");
1268 return 0;
1269 break;
1270 }
1271
1272 /*
1273 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1274 * An optional interrupt is OK (necessary for CBI protocol).
1275 * We will ignore any others.
1276 */
1277 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1278 ep_desc = &iface->ep_desc[i];
1279 /* is it an BULK endpoint? */
1280 if ((ep_desc->bmAttributes &
1281 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
1282 if (ep_desc->bEndpointAddress & USB_DIR_IN)
1283 ss->ep_in = ep_desc->bEndpointAddress &
1284 USB_ENDPOINT_NUMBER_MASK;
1285 else
1286 ss->ep_out =
1287 ep_desc->bEndpointAddress &
1288 USB_ENDPOINT_NUMBER_MASK;
1289 }
1290
1291 /* is it an interrupt endpoint? */
1292 if ((ep_desc->bmAttributes &
1293 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
1294 ss->ep_int = ep_desc->bEndpointAddress &
1295 USB_ENDPOINT_NUMBER_MASK;
1296 ss->irqinterval = ep_desc->bInterval;
1297 }
1298 }
1299 debug("Endpoints In %d Out %d Int %d\n",
1300 ss->ep_in, ss->ep_out, ss->ep_int);
1301
1302 /* Do some basic sanity checks, and bail if we find a problem */
1303 if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
1304 !ss->ep_in || !ss->ep_out ||
1305 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1306 debug("Problems with device\n");
1307 return 0;
1308 }
1309 /* set class specific stuff */
1310 /* We only handle certain protocols. Currently, these are
1311 * the only ones.
1312 * The SFF8070 accepts the requests used in u-boot
1313 */
1314 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1315 ss->subclass != US_SC_8070) {
1316 printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
1317 return 0;
1318 }
1319 if (ss->ep_int) {
1320 /* we had found an interrupt endpoint, prepare irq pipe
1321 * set up the IRQ pipe and handler
1322 */
1323 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1324 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1325 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1326 dev->irq_handle = usb_stor_irq;
1327 }
1328 dev->privptr = (void *)ss;
1329 return 1;
1330 }
1331
1332 int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
1333 block_dev_desc_t *dev_desc)
1334 {
1335 unsigned char perq, modi;
1336 ALLOC_CACHE_ALIGN_BUFFER(unsigned long, cap, 2);
1337 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, usb_stor_buf, 36);
1338 unsigned long *capacity, *blksz;
1339 ccb *pccb = &usb_ccb;
1340
1341 pccb->pdata = usb_stor_buf;
1342
1343 dev_desc->target = dev->devnum;
1344 pccb->lun = dev_desc->lun;
1345 debug(" address %d\n", dev_desc->target);
1346
1347 if (usb_inquiry(pccb, ss))
1348 return -1;
1349
1350 perq = usb_stor_buf[0];
1351 modi = usb_stor_buf[1];
1352
1353 if ((perq & 0x1f) == 0x1f) {
1354 /* skip unknown devices */
1355 return 0;
1356 }
1357 if ((modi&0x80) == 0x80) {
1358 /* drive is removable */
1359 dev_desc->removable = 1;
1360 }
1361 memcpy(&dev_desc->vendor[0], (const void *) &usb_stor_buf[8], 8);
1362 memcpy(&dev_desc->product[0], (const void *) &usb_stor_buf[16], 16);
1363 memcpy(&dev_desc->revision[0], (const void *) &usb_stor_buf[32], 4);
1364 dev_desc->vendor[8] = 0;
1365 dev_desc->product[16] = 0;
1366 dev_desc->revision[4] = 0;
1367 #ifdef CONFIG_USB_BIN_FIXUP
1368 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
1369 (uchar *)dev_desc->product);
1370 #endif /* CONFIG_USB_BIN_FIXUP */
1371 debug("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
1372 usb_stor_buf[3]);
1373 if (usb_test_unit_ready(pccb, ss)) {
1374 printf("Device NOT ready\n"
1375 " Request Sense returned %02X %02X %02X\n",
1376 pccb->sense_buf[2], pccb->sense_buf[12],
1377 pccb->sense_buf[13]);
1378 if (dev_desc->removable == 1) {
1379 dev_desc->type = perq;
1380 return 1;
1381 }
1382 return 0;
1383 }
1384 pccb->pdata = (unsigned char *)&cap[0];
1385 memset(pccb->pdata, 0, 8);
1386 if (usb_read_capacity(pccb, ss) != 0) {
1387 printf("READ_CAP ERROR\n");
1388 cap[0] = 2880;
1389 cap[1] = 0x200;
1390 }
1391 ss->flags &= ~USB_READY;
1392 debug("Read Capacity returns: 0x%lx, 0x%lx\n", cap[0], cap[1]);
1393 #if 0
1394 if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
1395 cap[0] >>= 16;
1396 #endif
1397 cap[0] = cpu_to_be32(cap[0]);
1398 cap[1] = cpu_to_be32(cap[1]);
1399
1400 /* this assumes bigendian! */
1401 cap[0] += 1;
1402 capacity = &cap[0];
1403 blksz = &cap[1];
1404 debug("Capacity = 0x%lx, blocksz = 0x%lx\n", *capacity, *blksz);
1405 dev_desc->lba = *capacity;
1406 dev_desc->blksz = *blksz;
1407 dev_desc->log2blksz = LOG2(dev_desc->blksz);
1408 dev_desc->type = perq;
1409 debug(" address %d\n", dev_desc->target);
1410 debug("partype: %d\n", dev_desc->part_type);
1411
1412 init_part(dev_desc);
1413
1414 debug("partype: %d\n", dev_desc->part_type);
1415 return 1;
1416 }