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