blob: 06ea99b2f1a750d441db8e13999970d76ceaa825 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
Wolfgang Denk6a3d6b02005-08-04 01:14:12 +02002 * 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
wdenkaffae2b2002-08-17 09:36:01 +000012 *
wdenkde887eb2003-09-10 18:20:28 +000013 * For BBB support (C) Copyright 2003
14 * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
15 *
Wolfgang Denk6a3d6b02005-08-04 01:14:12 +020016 * BBB support based on /sys/dev/usb/umass.c from
wdenkde887eb2003-09-10 18:20:28 +000017 * FreeBSD.
wdenkaffae2b2002-08-17 09:36:01 +000018 *
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
wdenk5f495752004-02-26 23:46:20 +000029 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkaffae2b2002-08-17 09:36:01 +000030 * 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 */
wdenkde887eb2003-09-10 18:20:28 +000044/*
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 */
wdenkaffae2b2002-08-17 09:36:01 +000051
52
wdenkaffae2b2002-08-17 09:36:01 +000053#include <common.h>
54#include <command.h>
55#include <asm/processor.h>
56
57
58#if (CONFIG_COMMANDS & CFG_CMD_USB)
59#include <usb.h>
60
61#ifdef CONFIG_USB_STORAGE
62
wdenk5f495752004-02-26 23:46:20 +000063#undef USB_STOR_DEBUG
64#undef BBB_COMDAT_TRACE
65#undef BBB_XPORT_TRACE
wdenkaffae2b2002-08-17 09:36:01 +000066
67#ifdef USB_STOR_DEBUG
wdenk5f495752004-02-26 23:46:20 +000068#define USB_STOR_PRINTF(fmt,args...) printf (fmt ,##args)
wdenkaffae2b2002-08-17 09:36:01 +000069#else
70#define USB_STOR_PRINTF(fmt,args...)
71#endif
72
73#include <scsi.h>
74/* direction table -- this indicates the direction of the data
75 * transfer for each command code -- a 1 indicates input
76 */
77unsigned char us_direction[256/8] = {
78 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
79 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
82};
83#define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
84
85static unsigned char usb_stor_buf[512];
86static ccb usb_ccb;
87
88/*
89 * CBI style
90 */
91
92#define US_CBI_ADSC 0
93
wdenkde887eb2003-09-10 18:20:28 +000094/*
95 * BULK only
96 */
97#define US_BBB_RESET 0xff
98#define US_BBB_GET_MAX_LUN 0xfe
99
100/* Command Block Wrapper */
101typedef struct {
102 __u32 dCBWSignature;
103# define CBWSIGNATURE 0x43425355
104 __u32 dCBWTag;
105 __u32 dCBWDataTransferLength;
106 __u8 bCBWFlags;
107# define CBWFLAGS_OUT 0x00
108# define CBWFLAGS_IN 0x80
109 __u8 bCBWLUN;
110 __u8 bCDBLength;
111# define CBWCDBLENGTH 16
112 __u8 CBWCDB[CBWCDBLENGTH];
113} umass_bbb_cbw_t;
wdenk5f495752004-02-26 23:46:20 +0000114#define UMASS_BBB_CBW_SIZE 31
wdenkde887eb2003-09-10 18:20:28 +0000115static __u32 CBWTag = 0;
116
117/* Command Status Wrapper */
118typedef struct {
119 __u32 dCSWSignature;
120# define CSWSIGNATURE 0x53425355
121 __u32 dCSWTag;
122 __u32 dCSWDataResidue;
123 __u8 bCSWStatus;
124# define CSWSTATUS_GOOD 0x0
wdenk5f495752004-02-26 23:46:20 +0000125# define CSWSTATUS_FAILED 0x1
wdenkde887eb2003-09-10 18:20:28 +0000126# define CSWSTATUS_PHASE 0x2
127} umass_bbb_csw_t;
wdenk5f495752004-02-26 23:46:20 +0000128#define UMASS_BBB_CSW_SIZE 13
wdenkaffae2b2002-08-17 09:36:01 +0000129
130#define USB_MAX_STOR_DEV 5
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200131static int usb_max_devs = 0; /* number of highest available usb device */
wdenkaffae2b2002-08-17 09:36:01 +0000132
133static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
134
135struct us_data;
136typedef int (*trans_cmnd)(ccb*, struct us_data*);
137typedef int (*trans_reset)(struct us_data*);
138
139struct us_data {
wdenk5f495752004-02-26 23:46:20 +0000140 struct usb_device *pusb_dev; /* this usb_device */
wdenkaffae2b2002-08-17 09:36:01 +0000141 unsigned int flags; /* from filter initially */
142 unsigned char ifnum; /* interface number */
143 unsigned char ep_in; /* in endpoint */
144 unsigned char ep_out; /* out ....... */
145 unsigned char ep_int; /* interrupt . */
146 unsigned char subclass; /* as in overview */
147 unsigned char protocol; /* .............. */
wdenk5f495752004-02-26 23:46:20 +0000148 unsigned char attention_done; /* force attn on first cmd */
wdenkaffae2b2002-08-17 09:36:01 +0000149 unsigned short ip_data; /* interrupt data */
150 int action; /* what to do */
151 int ip_wanted; /* needed */
152 int *irq_handle; /* for USB int requests */
153 unsigned int irqpipe; /* pipe for release_irq */
154 unsigned char irqmaxp; /* max packed for irq Pipe */
wdenk5f495752004-02-26 23:46:20 +0000155 unsigned char irqinterval; /* Intervall for IRQ Pipe */
wdenkaffae2b2002-08-17 09:36:01 +0000156 ccb *srb; /* current srb */
157 trans_reset transport_reset; /* reset routine */
158 trans_cmnd transport; /* transport routine */
159};
160
161static struct us_data usb_stor[USB_MAX_STOR_DEV];
162
163
wdenk5f495752004-02-26 23:46:20 +0000164#define USB_STOR_TRANSPORT_GOOD 0
wdenkaffae2b2002-08-17 09:36:01 +0000165#define USB_STOR_TRANSPORT_FAILED -1
166#define USB_STOR_TRANSPORT_ERROR -2
167
168
wdenkaffae2b2002-08-17 09:36:01 +0000169int usb_stor_get_info(struct usb_device *dev, struct us_data *us, block_dev_desc_t *dev_desc);
170int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss);
171unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer);
172struct usb_device * usb_get_dev_index(int index);
173void uhci_show_temp_int_td(void);
174
175block_dev_desc_t *usb_stor_get_dev(int index)
176{
177 return &usb_dev_desc[index];
178}
179
180
181void usb_show_progress(void)
182{
183 printf(".");
184}
185
186/*********************************************************************************
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200187 * show info on storage devices; 'usb start/init' must be invoked earlier
188 * as we only retrieve structures populated during devices initialization
189 */
190void usb_stor_info(void)
191{
192 int i;
193
194 if (usb_max_devs > 0)
195 for (i = 0; i < usb_max_devs; i++) {
196 printf (" Device %d: ", i);
197 dev_print(&usb_dev_desc[i]);
198 }
199 else
200 printf("No storage devices, perhaps not 'usb start'ed..?\n");
201}
202
203/*********************************************************************************
204 * scan the usb and reports device info
wdenkaffae2b2002-08-17 09:36:01 +0000205 * to the user if mode = 1
206 * returns current device or -1 if no
207 */
208int usb_stor_scan(int mode)
209{
210 unsigned char i;
211 struct usb_device *dev;
212
wdenkde887eb2003-09-10 18:20:28 +0000213 /* GJ */
214 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
215
wdenkaffae2b2002-08-17 09:36:01 +0000216 if(mode==1) {
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200217 printf(" scanning bus for storage devices... ");
wdenkaffae2b2002-08-17 09:36:01 +0000218 }
219 usb_disable_asynch(1); /* asynch transfer not allowed */
220
221 for(i=0;i<USB_MAX_STOR_DEV;i++) {
222 memset(&usb_dev_desc[i],0,sizeof(block_dev_desc_t));
223 usb_dev_desc[i].target=0xff;
224 usb_dev_desc[i].if_type=IF_TYPE_USB;
225 usb_dev_desc[i].dev=i;
226 usb_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
227 usb_dev_desc[i].block_read=usb_stor_read;
228 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200229
wdenkaffae2b2002-08-17 09:36:01 +0000230 usb_max_devs=0;
231 for(i=0;i<USB_MAX_DEVICE;i++) {
232 dev=usb_get_dev_index(i); /* get device */
233 USB_STOR_PRINTF("i=%d\n",i);
234 if(dev==NULL) {
235 break; /* no more devices avaiable */
236 }
237 if(usb_storage_probe(dev,0,&usb_stor[usb_max_devs])) { /* ok, it is a storage devices */
238 /* get info and fill it in */
Wolfgang Denkd06ce5d2005-08-02 17:06:17 +0200239 if(usb_stor_get_info(dev, &usb_stor[usb_max_devs], &usb_dev_desc[usb_max_devs]))
wdenkaffae2b2002-08-17 09:36:01 +0000240 usb_max_devs++;
wdenkaffae2b2002-08-17 09:36:01 +0000241 } /* if storage device */
242 if(usb_max_devs==USB_MAX_STOR_DEV) {
243 printf("max USB Storage Device reached: %d stopping\n",usb_max_devs);
244 break;
245 }
246 } /* for */
Wolfgang Denkd06ce5d2005-08-02 17:06:17 +0200247
wdenkaffae2b2002-08-17 09:36:01 +0000248 usb_disable_asynch(0); /* asynch transfer allowed */
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200249 printf("%d Storage Device(s) found\n", usb_max_devs);
wdenkaffae2b2002-08-17 09:36:01 +0000250 if(usb_max_devs>0)
251 return 0;
252 else
253 return-1;
254}
255
256static int usb_stor_irq(struct usb_device *dev)
257{
258 struct us_data *us;
259 us=(struct us_data *)dev->privptr;
260
261 if(us->ip_wanted) {
262 us->ip_wanted=0;
263 }
264 return 0;
265}
266
267
268#ifdef USB_STOR_DEBUG
269
270static void usb_show_srb(ccb * pccb)
271{
272 int i;
273 printf("SRB: len %d datalen 0x%lX\n ",pccb->cmdlen,pccb->datalen);
274 for(i=0;i<12;i++) {
275 printf("%02X ",pccb->cmd[i]);
276 }
277 printf("\n");
278}
279
280static void display_int_status(unsigned long tmp)
281{
282 printf("Status: %s %s %s %s %s %s %s\n",
283 (tmp & USB_ST_ACTIVE) ? "Active" : "",
284 (tmp & USB_ST_STALLED) ? "Stalled" : "",
285 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
286 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
287 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
288 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
289 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
290}
291#endif
292/***********************************************************************
293 * Data transfer routines
294 ***********************************************************************/
295
296static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
297{
298 int max_size;
299 int this_xfer;
300 int result;
301 int partial;
302 int maxtry;
303 int stat;
304
305 /* determine the maximum packet size for these transfers */
306 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
307
308 /* while we have data left to transfer */
309 while (length) {
310
311 /* calculate how long this will be -- maximum or a remainder */
312 this_xfer = length > max_size ? max_size : length;
313 length -= this_xfer;
314
315 /* setup the retry counter */
316 maxtry = 10;
317
318 /* set up the transfer loop */
319 do {
320 /* transfer the data */
321 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
322 (unsigned int)buf, this_xfer, 11 - maxtry);
323 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
324 this_xfer, &partial, USB_CNTL_TIMEOUT*5);
325 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
326 result, partial, this_xfer);
327 if(us->pusb_dev->status!=0) {
328 /* if we stall, we need to clear it before we go on */
329#ifdef USB_STOR_DEBUG
330 display_int_status(us->pusb_dev->status);
331#endif
332 if (us->pusb_dev->status & USB_ST_STALLED) {
333 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
334 stat = us->pusb_dev->status;
335 usb_clear_halt(us->pusb_dev, pipe);
336 us->pusb_dev->status=stat;
337 if(this_xfer == partial) {
338 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us->pusb_dev->status);
339 return 0;
340 }
341 else
342 return result;
343 }
344 if (us->pusb_dev->status & USB_ST_NAK_REC) {
345 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
346 return result;
347 }
348 if(this_xfer == partial) {
349 USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us->pusb_dev->status);
350 return 0;
351 }
352 /* if our try counter reaches 0, bail out */
353 USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us->pusb_dev->status,partial);
354 if (!maxtry--)
355 return result;
356 }
357 /* update to show what data was transferred */
358 this_xfer -= partial;
359 buf += partial;
360 /* continue until this transfer is done */
361 } while ( this_xfer );
362 }
363
364 /* if we get here, we're done and successful */
365 return 0;
366}
367
wdenkde887eb2003-09-10 18:20:28 +0000368static int usb_stor_BBB_reset(struct us_data *us)
369{
370 int result;
371 unsigned int pipe;
372
373 /*
374 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
375 *
376 * For Reset Recovery the host shall issue in the following order:
377 * a) a Bulk-Only Mass Storage Reset
378 * b) a Clear Feature HALT to the Bulk-In endpoint
379 * c) a Clear Feature HALT to the Bulk-Out endpoint
380 *
381 * This is done in 3 steps.
382 *
383 * If the reset doesn't succeed, the device should be port reset.
384 *
385 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
386 */
387 USB_STOR_PRINTF("BBB_reset\n");
388 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
389 US_BBB_RESET, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
390 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT*5);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200391
wdenkde887eb2003-09-10 18:20:28 +0000392 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED))
393 {
394 USB_STOR_PRINTF("RESET:stall\n");
395 return -1;
396 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200397
wdenkde887eb2003-09-10 18:20:28 +0000398 /* long wait for reset */
399 wait_ms(150);
400 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n",result,us->pusb_dev->status);
401 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
402 result = usb_clear_halt(us->pusb_dev, pipe);
403 /* long wait for reset */
404 wait_ms(150);
405 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",result,us->pusb_dev->status);
406 /* long wait for reset */
407 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
408 result = usb_clear_halt(us->pusb_dev, pipe);
409 wait_ms(150);
410 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing OUT endpoint\n",result,us->pusb_dev->status);
411 USB_STOR_PRINTF("BBB_reset done\n");
412 return 0;
413}
414
wdenkaffae2b2002-08-17 09:36:01 +0000415/* FIXME: this reset function doesn't really reset the port, and it
416 * should. Actually it should probably do what it's doing here, and
417 * reset the port physically
418 */
419static int usb_stor_CB_reset(struct us_data *us)
420{
421 unsigned char cmd[12];
422 int result;
423
424 USB_STOR_PRINTF("CB_reset\n");
425 memset(cmd, 0xFF, sizeof(cmd));
426 cmd[0] = SCSI_SEND_DIAG;
427 cmd[1] = 4;
428 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
429 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
430 0, us->ifnum, cmd, sizeof(cmd), USB_CNTL_TIMEOUT*5);
431
432 /* long wait for reset */
433 wait_ms(1500);
434 USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result,us->pusb_dev->status);
435 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
436 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
437
438 USB_STOR_PRINTF("CB_reset done\n");
439 return 0;
440}
441
wdenkde887eb2003-09-10 18:20:28 +0000442/*
443 * Set up the command for a BBB device. Note that the actual SCSI
444 * command is copied into cbw.CBWCDB.
445 */
446int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
447{
448 int result;
449 int actlen;
450 int dir_in;
451 unsigned int pipe;
452 umass_bbb_cbw_t cbw;
453
454 dir_in = US_DIRECTION(srb->cmd[0]);
455
456#ifdef BBB_COMDAT_TRACE
457 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);
458 if (srb->cmdlen) {
459 for(result = 0;result < srb->cmdlen;result++)
460 printf("cmd[%d] %#x ", result, srb->cmd[result]);
461 printf("\n");
462 }
463#endif
464 /* sanity checks */
465 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
466 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
467 return -1;
468 }
469
470 /* always OUT to the ep */
471 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
472
473 cbw.dCBWSignature = swap_32(CBWSIGNATURE);
474 cbw.dCBWTag = swap_32(CBWTag++);
475 cbw.dCBWDataTransferLength = swap_32(srb->datalen);
476 cbw.bCBWFlags = (dir_in? CBWFLAGS_IN : CBWFLAGS_OUT);
477 cbw.bCBWLUN = srb->lun;
478 cbw.bCDBLength = srb->cmdlen;
479 /* copy the command data into the CBW command data buffer */
480 /* DST SRC LEN!!! */
481 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
482 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE, &actlen, USB_CNTL_TIMEOUT*5);
483 if (result < 0)
484 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
485 return result;
486}
487
wdenkaffae2b2002-08-17 09:36:01 +0000488/* FIXME: we also need a CBI_command which sets up the completion
489 * interrupt, and waits for it
490 */
491int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
492{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200493 int result = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000494 int dir_in,retry;
495 unsigned int pipe;
496 unsigned long status;
497
498 retry=5;
499 dir_in=US_DIRECTION(srb->cmd[0]);
500
501 if(dir_in)
502 pipe=usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
503 else
504 pipe=usb_sndbulkpipe(us->pusb_dev, us->ep_out);
505 while(retry--) {
506 USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry);
507#ifdef USB_STOR_DEBUG
508 usb_show_srb(srb);
509#endif
510 /* let's send the command via the control pipe */
511 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
512 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
513 0, us->ifnum,
514 srb->cmd, srb->cmdlen, USB_CNTL_TIMEOUT*5);
515 USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result,us->pusb_dev->status);
516 /* check the return code for the command */
517 if (result < 0) {
518 if(us->pusb_dev->status & USB_ST_STALLED) {
519 status=us->pusb_dev->status;
520 USB_STOR_PRINTF(" stall during command found, clear pipe\n");
521 usb_clear_halt(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0));
522 us->pusb_dev->status=status;
523 }
524 USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb->cmd[0],us->pusb_dev->status);
525 return result;
526 }
527 /* transfer the data payload for this command, if one exists*/
528
529 USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result,dir_in ? "IN" : "OUT",srb->datalen);
530 if (srb->datalen) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200531 result = us_one_transfer(us, pipe, (char *)srb->pdata,srb->datalen);
wdenkaffae2b2002-08-17 09:36:01 +0000532 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);
533 if(!(us->pusb_dev->status & USB_ST_NAK_REC))
534 break;
535 } /* if (srb->datalen) */
536 else
537 break;
538 }
539 /* return result */
540
541 return result;
542}
543
544
wdenk5f495752004-02-26 23:46:20 +0000545int usb_stor_CBI_get_status (ccb * srb, struct us_data *us)
wdenkaffae2b2002-08-17 09:36:01 +0000546{
547 int timeout;
548
wdenk5f495752004-02-26 23:46:20 +0000549 us->ip_wanted = 1;
550 submit_int_msg (us->pusb_dev, us->irqpipe,
551 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
552 timeout = 1000;
553 while (timeout--) {
554 if ((volatile int *) us->ip_wanted == 0)
wdenkaffae2b2002-08-17 09:36:01 +0000555 break;
wdenk5f495752004-02-26 23:46:20 +0000556 wait_ms (10);
wdenkaffae2b2002-08-17 09:36:01 +0000557 }
558 if (us->ip_wanted) {
wdenk5f495752004-02-26 23:46:20 +0000559 printf (" Did not get interrupt on CBI\n");
wdenkaffae2b2002-08-17 09:36:01 +0000560 us->ip_wanted = 0;
561 return USB_STOR_TRANSPORT_ERROR;
562 }
wdenk5f495752004-02-26 23:46:20 +0000563 USB_STOR_PRINTF
564 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
565 us->ip_data, us->pusb_dev->irq_act_len,
566 us->pusb_dev->irq_status);
wdenkaffae2b2002-08-17 09:36:01 +0000567 /* UFI gives us ASC and ASCQ, like a request sense */
568 if (us->subclass == US_SC_UFI) {
569 if (srb->cmd[0] == SCSI_REQ_SENSE ||
570 srb->cmd[0] == SCSI_INQUIRY)
571 return USB_STOR_TRANSPORT_GOOD; /* Good */
wdenk5f495752004-02-26 23:46:20 +0000572 else if (us->ip_data)
573 return USB_STOR_TRANSPORT_FAILED;
wdenkaffae2b2002-08-17 09:36:01 +0000574 else
wdenk5f495752004-02-26 23:46:20 +0000575 return USB_STOR_TRANSPORT_GOOD;
wdenkaffae2b2002-08-17 09:36:01 +0000576 }
577 /* otherwise, we interpret the data normally */
578 switch (us->ip_data) {
wdenk5f495752004-02-26 23:46:20 +0000579 case 0x0001:
580 return USB_STOR_TRANSPORT_GOOD;
581 case 0x0002:
582 return USB_STOR_TRANSPORT_FAILED;
583 default:
584 return USB_STOR_TRANSPORT_ERROR;
585 } /* switch */
wdenkaffae2b2002-08-17 09:36:01 +0000586 return USB_STOR_TRANSPORT_ERROR;
587}
588
589#define USB_TRANSPORT_UNKNOWN_RETRY 5
590#define USB_TRANSPORT_NOT_READY_RETRY 10
591
wdenkde887eb2003-09-10 18:20:28 +0000592/* clear a stall on an endpoint - special for BBB devices */
593int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
594{
595 int result;
596
597 /* ENDPOINT_HALT = 0, so set value to 0 */
598 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
599 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
600 0, endpt, 0, 0, USB_CNTL_TIMEOUT*5);
601 return result;
602}
603
604int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
605{
606 int result, retry;
607 int dir_in;
608 int actlen, data_actlen;
609 unsigned int pipe, pipein, pipeout;
610 umass_bbb_csw_t csw;
611#ifdef BBB_XPORT_TRACE
612 unsigned char *ptr;
613 int index;
614#endif
615
616 dir_in = US_DIRECTION(srb->cmd[0]);
617
618 /* COMMAND phase */
619 USB_STOR_PRINTF("COMMAND phase\n");
620 result = usb_stor_BBB_comdat(srb, us);
621 if (result < 0) {
622 USB_STOR_PRINTF("failed to send CBW status %ld\n",
623 us->pusb_dev->status);
624 usb_stor_BBB_reset(us);
625 return USB_STOR_TRANSPORT_FAILED;
626 }
627 wait_ms(5);
628 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
629 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
630 /* DATA phase + error handling */
wdenkde887eb2003-09-10 18:20:28 +0000631 data_actlen = 0;
632 /* no data, go immediately to the STATUS phase */
633 if (srb->datalen == 0)
634 goto st;
wdenk5f495752004-02-26 23:46:20 +0000635 USB_STOR_PRINTF("DATA phase\n");
wdenkde887eb2003-09-10 18:20:28 +0000636 if (dir_in)
637 pipe = pipein;
638 else
639 pipe = pipeout;
640 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen, &data_actlen, USB_CNTL_TIMEOUT*5);
641 /* special handling of STALL in DATA phase */
642 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
wdenk934c4f82003-09-11 19:48:06 +0000643 USB_STOR_PRINTF("DATA:stall\n");
wdenkde887eb2003-09-10 18:20:28 +0000644 /* clear the STALL on the endpoint */
645 result = usb_stor_BBB_clear_endpt_stall(us, dir_in? us->ep_in : us->ep_out);
646 if (result >= 0)
647 /* continue on to STATUS phase */
648 goto st;
649 }
650 if (result < 0) {
651 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
652 us->pusb_dev->status);
653 usb_stor_BBB_reset(us);
654 return USB_STOR_TRANSPORT_FAILED;
655 }
656#ifdef BBB_XPORT_TRACE
657 for (index = 0; index < data_actlen; index++)
658 printf("pdata[%d] %#x ", index, srb->pdata[index]);
659 printf("\n");
660#endif
661 /* STATUS phase + error handling */
662 st:
663 retry = 0;
664 again:
665 USB_STOR_PRINTF("STATUS phase\n");
Wolfgang Denkd06ce5d2005-08-02 17:06:17 +0200666 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200667 &actlen, USB_CNTL_TIMEOUT*5);
668
wdenkde887eb2003-09-10 18:20:28 +0000669 /* special handling of STALL in STATUS phase */
670 if((result < 0) && (retry < 1) && (us->pusb_dev->status & USB_ST_STALLED)) {
671 USB_STOR_PRINTF("STATUS:stall\n");
672 /* clear the STALL on the endpoint */
673 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
674 if (result >= 0 && (retry++ < 1))
675 /* do a retry */
676 goto again;
677 }
678 if (result < 0) {
679 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
680 us->pusb_dev->status);
681 usb_stor_BBB_reset(us);
682 return USB_STOR_TRANSPORT_FAILED;
683 }
684#ifdef BBB_XPORT_TRACE
685 ptr = (unsigned char *)&csw;
686 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
687 printf("ptr[%d] %#x ", index, ptr[index]);
688 printf("\n");
689#endif
690 /* misuse pipe to get the residue */
691 pipe = swap_32(csw.dCSWDataResidue);
692 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
693 pipe = srb->datalen - data_actlen;
694 if (CSWSIGNATURE != swap_32(csw.dCSWSignature)) {
695 USB_STOR_PRINTF("!CSWSIGNATURE\n");
696 usb_stor_BBB_reset(us);
697 return USB_STOR_TRANSPORT_FAILED;
698 } else if ((CBWTag - 1) != swap_32(csw.dCSWTag)) {
699 USB_STOR_PRINTF("!Tag\n");
700 usb_stor_BBB_reset(us);
701 return USB_STOR_TRANSPORT_FAILED;
702 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
703 USB_STOR_PRINTF(">PHASE\n");
704 usb_stor_BBB_reset(us);
705 return USB_STOR_TRANSPORT_FAILED;
706 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
707 USB_STOR_PRINTF("=PHASE\n");
708 usb_stor_BBB_reset(us);
709 return USB_STOR_TRANSPORT_FAILED;
710 } else if (data_actlen > srb->datalen) {
711 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
712 data_actlen, srb->datalen);
713 return USB_STOR_TRANSPORT_FAILED;
714 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
715 USB_STOR_PRINTF("FAILED\n");
716 return USB_STOR_TRANSPORT_FAILED;
717 }
718
719 return result;
720}
721
wdenkaffae2b2002-08-17 09:36:01 +0000722int usb_stor_CB_transport(ccb *srb, struct us_data *us)
723{
724 int result,status;
725 ccb *psrb;
726 ccb reqsrb;
727 int retry,notready;
728
729 psrb=&reqsrb;
730 status=USB_STOR_TRANSPORT_GOOD;
731 retry=0;
732 notready=0;
733 /* issue the command */
734do_retry:
735 result=usb_stor_CB_comdat(srb,us);
736 USB_STOR_PRINTF("command / Data returned %d, status %X\n",result,us->pusb_dev->status);
737 /* if this is an CBI Protocol, get IRQ */
738 if(us->protocol==US_PR_CBI) {
739 status=usb_stor_CBI_get_status(srb,us);
740 /* if the status is error, report it */
741 if(status==USB_STOR_TRANSPORT_ERROR) {
742 USB_STOR_PRINTF(" USB CBI Command Error\n");
743 return status;
744 }
745 srb->sense_buf[12]=(unsigned char)(us->ip_data>>8);
746 srb->sense_buf[13]=(unsigned char)(us->ip_data&0xff);
747 if(!us->ip_data) {
748 /* if the status is good, report it */
749 if(status==USB_STOR_TRANSPORT_GOOD) {
750 USB_STOR_PRINTF(" USB CBI Command Good\n");
751 return status;
752 }
753 }
754 }
755 /* do we have to issue an auto request? */
756 /* HERE we have to check the result */
757 if((result<0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
758 USB_STOR_PRINTF("ERROR %X\n",us->pusb_dev->status);
759 us->transport_reset(us);
760 return USB_STOR_TRANSPORT_ERROR;
761 }
762 if((us->protocol==US_PR_CBI) &&
763 ((srb->cmd[0]==SCSI_REQ_SENSE) ||
wdenk5f495752004-02-26 23:46:20 +0000764 (srb->cmd[0]==SCSI_INQUIRY))) { /* do not issue an autorequest after request sense */
wdenkaffae2b2002-08-17 09:36:01 +0000765 USB_STOR_PRINTF("No auto request and good\n");
766 return USB_STOR_TRANSPORT_GOOD;
767 }
768 /* issue an request_sense */
769 memset(&psrb->cmd[0],0,12);
770 psrb->cmd[0]=SCSI_REQ_SENSE;
771 psrb->cmd[1]=srb->lun<<5;
772 psrb->cmd[4]=18;
773 psrb->datalen=18;
774 psrb->pdata=&srb->sense_buf[0];
775 psrb->cmdlen=12;
776 /* issue the command */
777 result=usb_stor_CB_comdat(psrb,us);
778 USB_STOR_PRINTF("auto request returned %d\n",result);
779 /* if this is an CBI Protocol, get IRQ */
780 if(us->protocol==US_PR_CBI) {
wdenk5f495752004-02-26 23:46:20 +0000781 status=usb_stor_CBI_get_status(psrb,us);
wdenkaffae2b2002-08-17 09:36:01 +0000782 }
783 if((result<0)&&!(us->pusb_dev->status & USB_ST_STALLED)) {
784 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us->pusb_dev->status);
785 return USB_STOR_TRANSPORT_ERROR;
786 }
787 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]);
788 /* Check the auto request result */
789 if((srb->sense_buf[2]==0) &&
790 (srb->sense_buf[12]==0) &&
791 (srb->sense_buf[13]==0)) /* ok, no sense */
792 return USB_STOR_TRANSPORT_GOOD;
793 /* Check the auto request result */
794 switch(srb->sense_buf[2]) {
wdenkde887eb2003-09-10 18:20:28 +0000795 case 0x01: /* Recovered Error */
796 return USB_STOR_TRANSPORT_GOOD;
wdenk5f495752004-02-26 23:46:20 +0000797 break;
wdenkde887eb2003-09-10 18:20:28 +0000798 case 0x02: /* Not Ready */
799 if(notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
800 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
801 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
802 return USB_STOR_TRANSPORT_FAILED;
803 } else {
804 wait_ms(100);
805 goto do_retry;
806 }
807 break;
808 default:
809 if(retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
810 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
811 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
812 return USB_STOR_TRANSPORT_FAILED;
813 } else {
814 goto do_retry;
815 }
816 break;
wdenkaffae2b2002-08-17 09:36:01 +0000817 }
818 return USB_STOR_TRANSPORT_FAILED;
819}
820
821
wdenkaffae2b2002-08-17 09:36:01 +0000822static int usb_inquiry(ccb *srb,struct us_data *ss)
823{
824 int retry,i;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200825 retry=5;
wdenkaffae2b2002-08-17 09:36:01 +0000826 do {
827 memset(&srb->cmd[0],0,12);
828 srb->cmd[0]=SCSI_INQUIRY;
829 srb->cmd[1]=srb->lun<<5;
830 srb->cmd[4]=36;
831 srb->datalen=36;
832 srb->cmdlen=12;
833 i=ss->transport(srb,ss);
834 USB_STOR_PRINTF("inquiry returns %d\n",i);
835 if(i==0)
836 break;
wdenkde887eb2003-09-10 18:20:28 +0000837 } while(retry--);
838
wdenkaffae2b2002-08-17 09:36:01 +0000839 if(!retry) {
840 printf("error in inquiry\n");
841 return -1;
842 }
843 return 0;
844}
845
846static int usb_request_sense(ccb *srb,struct us_data *ss)
847{
848 char *ptr;
wdenk5f495752004-02-26 23:46:20 +0000849
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200850 ptr=(char *)srb->pdata;
wdenkaffae2b2002-08-17 09:36:01 +0000851 memset(&srb->cmd[0],0,12);
852 srb->cmd[0]=SCSI_REQ_SENSE;
853 srb->cmd[1]=srb->lun<<5;
854 srb->cmd[4]=18;
855 srb->datalen=18;
856 srb->pdata=&srb->sense_buf[0];
857 srb->cmdlen=12;
858 ss->transport(srb,ss);
859 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200860 srb->pdata=(uchar *)ptr;
wdenkaffae2b2002-08-17 09:36:01 +0000861 return 0;
862}
863
864static int usb_test_unit_ready(ccb *srb,struct us_data *ss)
865{
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200866 int retries = 10;
wdenkde887eb2003-09-10 18:20:28 +0000867
wdenkaffae2b2002-08-17 09:36:01 +0000868 do {
869 memset(&srb->cmd[0],0,12);
870 srb->cmd[0]=SCSI_TST_U_RDY;
871 srb->cmd[1]=srb->lun<<5;
872 srb->datalen=0;
873 srb->cmdlen=12;
wdenkde887eb2003-09-10 18:20:28 +0000874 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
wdenkaffae2b2002-08-17 09:36:01 +0000875 return 0;
876 }
wdenk5f495752004-02-26 23:46:20 +0000877 usb_request_sense (srb, ss);
878 wait_ms (100);
wdenkaffae2b2002-08-17 09:36:01 +0000879 } while(retries--);
wdenkde887eb2003-09-10 18:20:28 +0000880
wdenkaffae2b2002-08-17 09:36:01 +0000881 return -1;
882}
883
884static int usb_read_capacity(ccb *srb,struct us_data *ss)
885{
886 int retry;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200887 retry = 3; /* retries */
wdenkaffae2b2002-08-17 09:36:01 +0000888 do {
889 memset(&srb->cmd[0],0,12);
890 srb->cmd[0]=SCSI_RD_CAPAC;
891 srb->cmd[1]=srb->lun<<5;
892 srb->datalen=8;
893 srb->cmdlen=12;
894 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
895 return 0;
896 }
wdenkde887eb2003-09-10 18:20:28 +0000897 } while(retry--);
898
wdenkaffae2b2002-08-17 09:36:01 +0000899 return -1;
900}
901
902static int usb_read_10(ccb *srb,struct us_data *ss, unsigned long start, unsigned short blocks)
903{
904 memset(&srb->cmd[0],0,12);
905 srb->cmd[0]=SCSI_READ10;
906 srb->cmd[1]=srb->lun<<5;
907 srb->cmd[2]=((unsigned char) (start>>24))&0xff;
908 srb->cmd[3]=((unsigned char) (start>>16))&0xff;
909 srb->cmd[4]=((unsigned char) (start>>8))&0xff;
910 srb->cmd[5]=((unsigned char) (start))&0xff;
911 srb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
912 srb->cmd[8]=(unsigned char) blocks & 0xff;
913 srb->cmdlen=12;
914 USB_STOR_PRINTF("read10: start %lx blocks %x\n",start,blocks);
915 return ss->transport(srb,ss);
916}
917
918
Bartlomiej Sieka9d47ad42006-08-22 10:38:18 +0200919#ifdef CONFIG_USB_BIN_FIXUP
920/*
921 * Some USB storage devices queried for SCSI identification data respond with
922 * binary strings, which if output to the console freeze the terminal. The
923 * workaround is to modify the vendor and product strings read from such
924 * device with proper values (as reported by 'usb info').
925 *
926 * Vendor and product length limits are taken from the definition of
927 * block_dev_desc_t in include/part.h.
928 */
929static void usb_bin_fixup(struct usb_device_descriptor descriptor,
930 unsigned char vendor[],
931 unsigned char product[]) {
932 const unsigned char max_vendor_len = 40;
933 const unsigned char max_product_len = 20;
934 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
Wolfgang Denk511b1e32006-10-20 12:02:59 +0200935 strncpy ((char *)vendor, "SMSC", max_vendor_len);
936 strncpy ((char *)product, "Flash Media Cntrller", max_product_len);
Bartlomiej Sieka9d47ad42006-08-22 10:38:18 +0200937 }
938}
939#endif /* CONFIG_USB_BIN_FIXUP */
940
wdenkaffae2b2002-08-17 09:36:01 +0000941#define USB_MAX_READ_BLK 20
942
943unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer)
944{
945 unsigned long start,blks, buf_addr;
946 unsigned short smallblks;
947 struct usb_device *dev;
948 int retry,i;
wdenk0e2874cb2004-03-02 14:05:39 +0000949 ccb *srb = &usb_ccb;
950
951 if (blkcnt == 0)
952 return 0;
953
954 device &= 0xff;
wdenkaffae2b2002-08-17 09:36:01 +0000955 /* Setup device
956 */
957 USB_STOR_PRINTF("\nusb_read: dev %d \n",device);
958 dev=NULL;
959 for(i=0;i<USB_MAX_DEVICE;i++) {
960 dev=usb_get_dev_index(i);
961 if(dev==NULL) {
962 return 0;
963 }
964 if(dev->devnum==usb_dev_desc[device].target)
965 break;
966 }
967
968 usb_disable_asynch(1); /* asynch transfer not allowed */
969 srb->lun=usb_dev_desc[device].lun;
970 buf_addr=(unsigned long)buffer;
971 start=blknr;
972 blks=blkcnt;
973 if(usb_test_unit_ready(srb,(struct us_data *)dev->privptr)) {
974 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
975 srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
976 return 0;
977 }
978 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks, buf_addr);
979 do {
980 retry=2;
981 srb->pdata=(unsigned char *)buf_addr;
982 if(blks>USB_MAX_READ_BLK) {
983 smallblks=USB_MAX_READ_BLK;
wdenkde887eb2003-09-10 18:20:28 +0000984 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000985 smallblks=(unsigned short) blks;
986 }
987retry_it:
988 if(smallblks==USB_MAX_READ_BLK)
989 usb_show_progress();
990 srb->datalen=usb_dev_desc[device].blksz * smallblks;
991 srb->pdata=(unsigned char *)buf_addr;
992 if(usb_read_10(srb,(struct us_data *)dev->privptr, start, smallblks)) {
993 USB_STOR_PRINTF("Read ERROR\n");
994 usb_request_sense(srb,(struct us_data *)dev->privptr);
995 if(retry--)
996 goto retry_it;
997 blkcnt-=blks;
998 break;
999 }
1000 start+=smallblks;
1001 blks-=smallblks;
1002 buf_addr+=srb->datalen;
1003 } while(blks!=0);
1004 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
1005 usb_disable_asynch(0); /* asynch transfer allowed */
1006 if(blkcnt>=USB_MAX_READ_BLK)
1007 printf("\n");
1008 return(blkcnt);
1009}
1010
1011
1012/* Probe to see if a new device is actually a Storage device */
1013int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss)
1014{
1015 struct usb_interface_descriptor *iface;
1016 int i;
1017 unsigned int flags = 0;
1018
1019 int protocol = 0;
1020 int subclass = 0;
1021
wdenkaffae2b2002-08-17 09:36:01 +00001022 /* let's examine the device now */
1023 iface = &dev->config.if_desc[ifnum];
1024
1025#if 0
1026 /* this is the place to patch some storage devices */
1027 USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
1028 if ((dev->descriptor.idVendor) == 0x066b && (dev->descriptor.idProduct) == 0x0103) {
1029 USB_STOR_PRINTF("patched for E-USB\n");
1030 protocol = US_PR_CB;
1031 subclass = US_SC_UFI; /* an assumption */
1032 }
1033#endif
1034
1035 if (dev->descriptor.bDeviceClass != 0 ||
1036 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1037 iface->bInterfaceSubClass < US_SC_MIN ||
1038 iface->bInterfaceSubClass > US_SC_MAX) {
1039 /* if it's not a mass storage, we go no further */
1040 return 0;
1041 }
1042
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001043 memset(ss, 0, sizeof(struct us_data));
1044
wdenkaffae2b2002-08-17 09:36:01 +00001045 /* At this point, we know we've got a live one */
1046 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1047
1048 /* Initialize the us_data structure with some useful info */
1049 ss->flags = flags;
1050 ss->ifnum = ifnum;
1051 ss->pusb_dev = dev;
1052 ss->attention_done = 0;
1053
1054 /* If the device has subclass and protocol, then use that. Otherwise,
1055 * take data from the specific interface.
1056 */
1057 if (subclass) {
1058 ss->subclass = subclass;
1059 ss->protocol = protocol;
1060 } else {
1061 ss->subclass = iface->bInterfaceSubClass;
1062 ss->protocol = iface->bInterfaceProtocol;
1063 }
1064
1065 /* set the handler pointers based on the protocol */
1066 USB_STOR_PRINTF("Transport: ");
1067 switch (ss->protocol) {
1068 case US_PR_CB:
1069 USB_STOR_PRINTF("Control/Bulk\n");
1070 ss->transport = usb_stor_CB_transport;
1071 ss->transport_reset = usb_stor_CB_reset;
1072 break;
1073
1074 case US_PR_CBI:
1075 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1076 ss->transport = usb_stor_CB_transport;
1077 ss->transport_reset = usb_stor_CB_reset;
1078 break;
wdenkde887eb2003-09-10 18:20:28 +00001079 case US_PR_BULK:
1080 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1081 ss->transport = usb_stor_BBB_transport;
1082 ss->transport_reset = usb_stor_BBB_reset;
1083 break;
wdenkaffae2b2002-08-17 09:36:01 +00001084 default:
wdenk5f495752004-02-26 23:46:20 +00001085 printf("USB Storage Transport unknown / not yet implemented\n");
wdenkaffae2b2002-08-17 09:36:01 +00001086 return 0;
1087 break;
1088 }
1089
1090 /*
1091 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1092 * An optional interrupt is OK (necessary for CBI protocol).
1093 * We will ignore any others.
1094 */
1095 for (i = 0; i < iface->bNumEndpoints; i++) {
1096 /* is it an BULK endpoint? */
1097 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1098 == USB_ENDPOINT_XFER_BULK) {
1099 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1100 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1101 USB_ENDPOINT_NUMBER_MASK;
1102 else
1103 ss->ep_out = iface->ep_desc[i].bEndpointAddress &
1104 USB_ENDPOINT_NUMBER_MASK;
1105 }
1106
1107 /* is it an interrupt endpoint? */
1108 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1109 == USB_ENDPOINT_XFER_INT) {
1110 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1111 USB_ENDPOINT_NUMBER_MASK;
1112 ss->irqinterval = iface->ep_desc[i].bInterval;
1113 }
1114 }
1115 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1116 ss->ep_in, ss->ep_out, ss->ep_int);
1117
1118 /* Do some basic sanity checks, and bail if we find a problem */
1119 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
1120 !ss->ep_in || !ss->ep_out ||
1121 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1122 USB_STOR_PRINTF("Problems with device\n");
1123 return 0;
1124 }
1125 /* set class specific stuff */
wdenkde887eb2003-09-10 18:20:28 +00001126 /* We only handle certain protocols. Currently, these are
1127 * the only ones.
wdenk5f495752004-02-26 23:46:20 +00001128 * The SFF8070 accepts the requests used in u-boot
wdenkaffae2b2002-08-17 09:36:01 +00001129 */
wdenk5f495752004-02-26 23:46:20 +00001130 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1131 ss->subclass != US_SC_8070) {
wdenkaffae2b2002-08-17 09:36:01 +00001132 printf("Sorry, protocol %d not yet supported.\n",ss->subclass);
1133 return 0;
1134 }
wdenkde887eb2003-09-10 18:20:28 +00001135 if(ss->ep_int) { /* we had found an interrupt endpoint, prepare irq pipe */
wdenkaffae2b2002-08-17 09:36:01 +00001136 /* set up the IRQ pipe and handler */
1137
1138 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1139 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1140 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1141 dev->irq_handle=usb_stor_irq;
wdenkaffae2b2002-08-17 09:36:01 +00001142 }
wdenk934c4f82003-09-11 19:48:06 +00001143 dev->privptr=(void *)ss;
wdenkaffae2b2002-08-17 09:36:01 +00001144 return 1;
1145}
1146
1147int usb_stor_get_info(struct usb_device *dev,struct us_data *ss,block_dev_desc_t *dev_desc)
1148{
1149 unsigned char perq,modi;
1150 unsigned long cap[2];
1151 unsigned long *capacity,*blksz;
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001152 ccb *pccb = &usb_ccb;
wdenkaffae2b2002-08-17 09:36:01 +00001153
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001154 /* for some reasons a couple of devices would not survive this reset */
1155 if (
1156 /* Sony USM256E */
1157 (dev->descriptor.idVendor == 0x054c &&
1158 dev->descriptor.idProduct == 0x019e)
1159
1160 ||
1161 /* USB007 Mini-USB2 Flash Drive */
1162 (dev->descriptor.idVendor == 0x066f &&
1163 dev->descriptor.idProduct == 0x2010)
Bartlomiej Sieka76559012006-07-13 15:32:16 +02001164 ||
1165 /* SanDisk Corporation Cruzer Micro 20044318410546613953 */
1166 (dev->descriptor.idVendor == 0x0781 &&
1167 dev->descriptor.idProduct == 0x5151)
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001168 )
1169 USB_STOR_PRINTF("usb_stor_get_info: skipping RESET..\n");
Wolfgang Denkd06ce5d2005-08-02 17:06:17 +02001170 else
wdenk6c59edc2004-05-03 20:45:30 +00001171 ss->transport_reset(ss);
wdenkaffae2b2002-08-17 09:36:01 +00001172
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001173 pccb->pdata = usb_stor_buf;
1174
1175 dev_desc->target = dev->devnum;
1176 pccb->lun = dev_desc->lun;
wdenkaffae2b2002-08-17 09:36:01 +00001177 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1178
1179 if(usb_inquiry(pccb,ss))
1180 return -1;
Wolfgang Denkd06ce5d2005-08-02 17:06:17 +02001181
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001182 perq = usb_stor_buf[0];
1183 modi = usb_stor_buf[1];
1184 if((perq & 0x1f) == 0x1f) {
wdenkaffae2b2002-08-17 09:36:01 +00001185 return 0; /* skip unknown devices */
1186 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001187 if((modi&0x80) == 0x80) {/* drive is removable */
1188 dev_desc->removable = 1;
wdenkaffae2b2002-08-17 09:36:01 +00001189 }
1190 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1191 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1192 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001193 dev_desc->vendor[8] = 0;
1194 dev_desc->product[16] = 0;
1195 dev_desc->revision[4] = 0;
Bartlomiej Sieka9d47ad42006-08-22 10:38:18 +02001196#ifdef CONFIG_USB_BIN_FIXUP
1197 usb_bin_fixup(dev->descriptor, dev_desc->vendor, dev_desc->product);
1198#endif /* CONFIG_USB_BIN_FIXUP */
wdenkaffae2b2002-08-17 09:36:01 +00001199 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf[2],usb_stor_buf[3]);
1200 if(usb_test_unit_ready(pccb,ss)) {
1201 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",pccb->sense_buf[2],pccb->sense_buf[12],pccb->sense_buf[13]);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001202 if(dev_desc->removable == 1) {
1203 dev_desc->type = perq;
wdenkaffae2b2002-08-17 09:36:01 +00001204 return 1;
1205 }
1206 else
1207 return 0;
1208 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001209 pccb->pdata = (unsigned char *)&cap[0];
wdenkaffae2b2002-08-17 09:36:01 +00001210 memset(pccb->pdata,0,8);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001211 if(usb_read_capacity(pccb,ss) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +00001212 printf("READ_CAP ERROR\n");
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001213 cap[0] = 2880;
1214 cap[1] = 0x200;
wdenkaffae2b2002-08-17 09:36:01 +00001215 }
1216 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap[0],cap[1]);
1217#if 0
1218 if(cap[0]>(0x200000 * 10)) /* greater than 10 GByte */
1219 cap[0]>>=16;
1220#endif
wdenkde887eb2003-09-10 18:20:28 +00001221#ifdef LITTLEENDIAN
1222 cap[0] = ((unsigned long)(
1223 (((unsigned long)(cap[0]) & (unsigned long)0x000000ffUL) << 24) |
1224 (((unsigned long)(cap[0]) & (unsigned long)0x0000ff00UL) << 8) |
1225 (((unsigned long)(cap[0]) & (unsigned long)0x00ff0000UL) >> 8) |
1226 (((unsigned long)(cap[0]) & (unsigned long)0xff000000UL) >> 24) ));
1227 cap[1] = ((unsigned long)(
1228 (((unsigned long)(cap[1]) & (unsigned long)0x000000ffUL) << 24) |
1229 (((unsigned long)(cap[1]) & (unsigned long)0x0000ff00UL) << 8) |
1230 (((unsigned long)(cap[1]) & (unsigned long)0x00ff0000UL) >> 8) |
1231 (((unsigned long)(cap[1]) & (unsigned long)0xff000000UL) >> 24) ));
1232#endif
1233 /* this assumes bigendian! */
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001234 cap[0] += 1;
1235 capacity = &cap[0];
1236 blksz = &cap[1];
wdenkaffae2b2002-08-17 09:36:01 +00001237 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity,*blksz);
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +02001238 dev_desc->lba = *capacity;
1239 dev_desc->blksz = *blksz;
1240 dev_desc->type = perq;
wdenkaffae2b2002-08-17 09:36:01 +00001241 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1242 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1243
1244 init_part(dev_desc);
1245
1246 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1247 return 1;
1248}
1249
wdenkaffae2b2002-08-17 09:36:01 +00001250#endif /* CONFIG_USB_STORAGE */
wdenkde887eb2003-09-10 18:20:28 +00001251#endif /* CFG_CMD_USB */