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