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