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