blob: 7c08f957752a34e7d8a973f3a2836093439a4be1 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
Wolfgang Denk460c3222005-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 *
wdenk149dded2003-09-10 18:20:28 +000013 * For BBB support (C) Copyright 2003
14 * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
15 *
Wolfgang Denk460c3222005-08-04 01:14:12 +020016 * BBB support based on /sys/dev/usb/umass.c from
wdenk149dded2003-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
wdenk80885a92004-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 */
wdenk149dded2003-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
Jon Loeligerc3517f92007-07-08 18:10:08 -050058#if defined(CONFIG_CMD_USB)
Grant Likely735dd972007-02-20 09:04:34 +010059#include <part.h>
wdenkaffae2b2002-08-17 09:36:01 +000060#include <usb.h>
61
62#ifdef CONFIG_USB_STORAGE
63
wdenk80885a92004-02-26 23:46:20 +000064#undef USB_STOR_DEBUG
65#undef BBB_COMDAT_TRACE
66#undef BBB_XPORT_TRACE
wdenkaffae2b2002-08-17 09:36:01 +000067
68#ifdef USB_STOR_DEBUG
wdenk80885a92004-02-26 23:46:20 +000069#define USB_STOR_PRINTF(fmt,args...) printf (fmt ,##args)
wdenkaffae2b2002-08-17 09:36:01 +000070#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 */
78unsigned 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
86static unsigned char usb_stor_buf[512];
87static ccb usb_ccb;
88
89/*
90 * CBI style
91 */
92
93#define US_CBI_ADSC 0
94
wdenk149dded2003-09-10 18:20:28 +000095/*
96 * BULK only
97 */
98#define US_BBB_RESET 0xff
99#define US_BBB_GET_MAX_LUN 0xfe
100
101/* Command Block Wrapper */
102typedef 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;
wdenk80885a92004-02-26 23:46:20 +0000115#define UMASS_BBB_CBW_SIZE 31
wdenk149dded2003-09-10 18:20:28 +0000116static __u32 CBWTag = 0;
117
118/* Command Status Wrapper */
119typedef struct {
120 __u32 dCSWSignature;
121# define CSWSIGNATURE 0x53425355
122 __u32 dCSWTag;
123 __u32 dCSWDataResidue;
124 __u8 bCSWStatus;
125# define CSWSTATUS_GOOD 0x0
wdenk80885a92004-02-26 23:46:20 +0000126# define CSWSTATUS_FAILED 0x1
wdenk149dded2003-09-10 18:20:28 +0000127# define CSWSTATUS_PHASE 0x2
128} umass_bbb_csw_t;
wdenk80885a92004-02-26 23:46:20 +0000129#define UMASS_BBB_CSW_SIZE 13
wdenkaffae2b2002-08-17 09:36:01 +0000130
131#define USB_MAX_STOR_DEV 5
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200132static int usb_max_devs = 0; /* number of highest available usb device */
wdenkaffae2b2002-08-17 09:36:01 +0000133
134static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
135
136struct us_data;
137typedef int (*trans_cmnd)(ccb*, struct us_data*);
138typedef int (*trans_reset)(struct us_data*);
139
140struct us_data {
wdenk80885a92004-02-26 23:46:20 +0000141 struct usb_device *pusb_dev; /* this usb_device */
wdenkaffae2b2002-08-17 09:36:01 +0000142 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; /* .............. */
wdenk80885a92004-02-26 23:46:20 +0000149 unsigned char attention_done; /* force attn on first cmd */
wdenkaffae2b2002-08-17 09:36:01 +0000150 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 */
wdenk80885a92004-02-26 23:46:20 +0000156 unsigned char irqinterval; /* Intervall for IRQ Pipe */
wdenkaffae2b2002-08-17 09:36:01 +0000157 ccb *srb; /* current srb */
158 trans_reset transport_reset; /* reset routine */
159 trans_cmnd transport; /* transport routine */
160};
161
162static struct us_data usb_stor[USB_MAX_STOR_DEV];
163
164
wdenk80885a92004-02-26 23:46:20 +0000165#define USB_STOR_TRANSPORT_GOOD 0
wdenkaffae2b2002-08-17 09:36:01 +0000166#define USB_STOR_TRANSPORT_FAILED -1
167#define USB_STOR_TRANSPORT_ERROR -2
168
169
wdenkaffae2b2002-08-17 09:36:01 +0000170int usb_stor_get_info(struct usb_device *dev, struct us_data *us, block_dev_desc_t *dev_desc);
171int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss);
Grant Likelyeb867a72007-02-20 09:05:45 +0100172unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, void *buffer);
wdenkaffae2b2002-08-17 09:36:01 +0000173struct usb_device * usb_get_dev_index(int index);
174void uhci_show_temp_int_td(void);
175
176block_dev_desc_t *usb_stor_get_dev(int index)
177{
Grant Likely735dd972007-02-20 09:04:34 +0100178 return (index < USB_MAX_STOR_DEV) ? &usb_dev_desc[index] : NULL;
wdenkaffae2b2002-08-17 09:36:01 +0000179}
180
181
182void usb_show_progress(void)
183{
184 printf(".");
185}
186
187/*********************************************************************************
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200188 * show info on storage devices; 'usb start/init' must be invoked earlier
189 * as we only retrieve structures populated during devices initialization
190 */
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100191int usb_stor_info(void)
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200192{
193 int i;
194
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100195 if (usb_max_devs > 0) {
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200196 for (i = 0; i < usb_max_devs; i++) {
197 printf (" Device %d: ", i);
198 dev_print(&usb_dev_desc[i]);
199 }
Markus Klotzbuecherb9e749e2008-03-26 18:26:43 +0100200 return 0;
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100201 }
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700202
Markus Klotzbuecherb9e749e2008-03-26 18:26:43 +0100203 printf("No storage devices, perhaps not 'usb start'ed..?\n");
204 return 1;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200205}
206
207/*********************************************************************************
208 * scan the usb and reports device info
wdenkaffae2b2002-08-17 09:36:01 +0000209 * to the user if mode = 1
210 * returns current device or -1 if no
211 */
212int usb_stor_scan(int mode)
213{
214 unsigned char i;
215 struct usb_device *dev;
216
wdenk149dded2003-09-10 18:20:28 +0000217 /* GJ */
218 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
219
wdenkaffae2b2002-08-17 09:36:01 +0000220 if(mode==1) {
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200221 printf(" scanning bus for storage devices... ");
wdenkaffae2b2002-08-17 09:36:01 +0000222 }
223 usb_disable_asynch(1); /* asynch transfer not allowed */
224
225 for(i=0;i<USB_MAX_STOR_DEV;i++) {
226 memset(&usb_dev_desc[i],0,sizeof(block_dev_desc_t));
227 usb_dev_desc[i].target=0xff;
228 usb_dev_desc[i].if_type=IF_TYPE_USB;
229 usb_dev_desc[i].dev=i;
230 usb_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
231 usb_dev_desc[i].block_read=usb_stor_read;
232 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200233
wdenkaffae2b2002-08-17 09:36:01 +0000234 usb_max_devs=0;
235 for(i=0;i<USB_MAX_DEVICE;i++) {
236 dev=usb_get_dev_index(i); /* get device */
237 USB_STOR_PRINTF("i=%d\n",i);
238 if(dev==NULL) {
239 break; /* no more devices avaiable */
240 }
241 if(usb_storage_probe(dev,0,&usb_stor[usb_max_devs])) { /* ok, it is a storage devices */
242 /* get info and fill it in */
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200243 if(usb_stor_get_info(dev, &usb_stor[usb_max_devs], &usb_dev_desc[usb_max_devs]))
wdenkaffae2b2002-08-17 09:36:01 +0000244 usb_max_devs++;
wdenkaffae2b2002-08-17 09:36:01 +0000245 } /* if storage device */
246 if(usb_max_devs==USB_MAX_STOR_DEV) {
247 printf("max USB Storage Device reached: %d stopping\n",usb_max_devs);
248 break;
249 }
250 } /* for */
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200251
wdenkaffae2b2002-08-17 09:36:01 +0000252 usb_disable_asynch(0); /* asynch transfer allowed */
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200253 printf("%d Storage Device(s) found\n", usb_max_devs);
wdenkaffae2b2002-08-17 09:36:01 +0000254 if(usb_max_devs>0)
255 return 0;
256 else
257 return-1;
258}
259
260static int usb_stor_irq(struct usb_device *dev)
261{
262 struct us_data *us;
263 us=(struct us_data *)dev->privptr;
264
265 if(us->ip_wanted) {
266 us->ip_wanted=0;
267 }
268 return 0;
269}
270
271
272#ifdef USB_STOR_DEBUG
273
274static void usb_show_srb(ccb * pccb)
275{
276 int i;
277 printf("SRB: len %d datalen 0x%lX\n ",pccb->cmdlen,pccb->datalen);
278 for(i=0;i<12;i++) {
279 printf("%02X ",pccb->cmd[i]);
280 }
281 printf("\n");
282}
283
284static void display_int_status(unsigned long tmp)
285{
286 printf("Status: %s %s %s %s %s %s %s\n",
287 (tmp & USB_ST_ACTIVE) ? "Active" : "",
288 (tmp & USB_ST_STALLED) ? "Stalled" : "",
289 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
290 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
291 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
292 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
293 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
294}
295#endif
296/***********************************************************************
297 * Data transfer routines
298 ***********************************************************************/
299
300static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
301{
302 int max_size;
303 int this_xfer;
304 int result;
305 int partial;
306 int maxtry;
307 int stat;
308
309 /* determine the maximum packet size for these transfers */
310 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
311
312 /* while we have data left to transfer */
313 while (length) {
314
315 /* calculate how long this will be -- maximum or a remainder */
316 this_xfer = length > max_size ? max_size : length;
317 length -= this_xfer;
318
319 /* setup the retry counter */
320 maxtry = 10;
321
322 /* set up the transfer loop */
323 do {
324 /* transfer the data */
325 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
326 (unsigned int)buf, this_xfer, 11 - maxtry);
327 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
328 this_xfer, &partial, USB_CNTL_TIMEOUT*5);
329 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
330 result, partial, this_xfer);
331 if(us->pusb_dev->status!=0) {
332 /* if we stall, we need to clear it before we go on */
333#ifdef USB_STOR_DEBUG
334 display_int_status(us->pusb_dev->status);
335#endif
336 if (us->pusb_dev->status & USB_ST_STALLED) {
337 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
338 stat = us->pusb_dev->status;
339 usb_clear_halt(us->pusb_dev, pipe);
340 us->pusb_dev->status=stat;
341 if(this_xfer == partial) {
342 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us->pusb_dev->status);
343 return 0;
344 }
345 else
346 return result;
347 }
348 if (us->pusb_dev->status & USB_ST_NAK_REC) {
349 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
350 return result;
351 }
352 if(this_xfer == partial) {
353 USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us->pusb_dev->status);
354 return 0;
355 }
356 /* if our try counter reaches 0, bail out */
357 USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us->pusb_dev->status,partial);
358 if (!maxtry--)
359 return result;
360 }
361 /* update to show what data was transferred */
362 this_xfer -= partial;
363 buf += partial;
364 /* continue until this transfer is done */
365 } while ( this_xfer );
366 }
367
368 /* if we get here, we're done and successful */
369 return 0;
370}
371
wdenk149dded2003-09-10 18:20:28 +0000372static int usb_stor_BBB_reset(struct us_data *us)
373{
374 int result;
375 unsigned int pipe;
376
377 /*
378 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
379 *
380 * For Reset Recovery the host shall issue in the following order:
381 * a) a Bulk-Only Mass Storage Reset
382 * b) a Clear Feature HALT to the Bulk-In endpoint
383 * c) a Clear Feature HALT to the Bulk-Out endpoint
384 *
385 * This is done in 3 steps.
386 *
387 * If the reset doesn't succeed, the device should be port reset.
388 *
389 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
390 */
391 USB_STOR_PRINTF("BBB_reset\n");
392 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
393 US_BBB_RESET, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
394 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT*5);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200395
wdenk149dded2003-09-10 18:20:28 +0000396 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED))
397 {
398 USB_STOR_PRINTF("RESET:stall\n");
399 return -1;
400 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200401
wdenk149dded2003-09-10 18:20:28 +0000402 /* long wait for reset */
403 wait_ms(150);
404 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n",result,us->pusb_dev->status);
405 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
406 result = usb_clear_halt(us->pusb_dev, pipe);
407 /* long wait for reset */
408 wait_ms(150);
409 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",result,us->pusb_dev->status);
410 /* long wait for reset */
411 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
412 result = usb_clear_halt(us->pusb_dev, pipe);
413 wait_ms(150);
414 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing OUT endpoint\n",result,us->pusb_dev->status);
415 USB_STOR_PRINTF("BBB_reset done\n");
416 return 0;
417}
418
wdenkaffae2b2002-08-17 09:36:01 +0000419/* FIXME: this reset function doesn't really reset the port, and it
420 * should. Actually it should probably do what it's doing here, and
421 * reset the port physically
422 */
423static int usb_stor_CB_reset(struct us_data *us)
424{
425 unsigned char cmd[12];
426 int result;
427
428 USB_STOR_PRINTF("CB_reset\n");
429 memset(cmd, 0xFF, sizeof(cmd));
430 cmd[0] = SCSI_SEND_DIAG;
431 cmd[1] = 4;
432 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
433 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
434 0, us->ifnum, cmd, sizeof(cmd), USB_CNTL_TIMEOUT*5);
435
436 /* long wait for reset */
437 wait_ms(1500);
438 USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result,us->pusb_dev->status);
439 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
440 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
441
442 USB_STOR_PRINTF("CB_reset done\n");
443 return 0;
444}
445
wdenk149dded2003-09-10 18:20:28 +0000446/*
447 * Set up the command for a BBB device. Note that the actual SCSI
448 * command is copied into cbw.CBWCDB.
449 */
450int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
451{
452 int result;
453 int actlen;
454 int dir_in;
455 unsigned int pipe;
456 umass_bbb_cbw_t cbw;
457
458 dir_in = US_DIRECTION(srb->cmd[0]);
459
460#ifdef BBB_COMDAT_TRACE
461 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);
462 if (srb->cmdlen) {
463 for(result = 0;result < srb->cmdlen;result++)
464 printf("cmd[%d] %#x ", result, srb->cmd[result]);
465 printf("\n");
466 }
467#endif
468 /* sanity checks */
469 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
470 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
471 return -1;
472 }
473
474 /* always OUT to the ep */
475 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
476
477 cbw.dCBWSignature = swap_32(CBWSIGNATURE);
478 cbw.dCBWTag = swap_32(CBWTag++);
479 cbw.dCBWDataTransferLength = swap_32(srb->datalen);
480 cbw.bCBWFlags = (dir_in? CBWFLAGS_IN : CBWFLAGS_OUT);
481 cbw.bCBWLUN = srb->lun;
482 cbw.bCDBLength = srb->cmdlen;
483 /* copy the command data into the CBW command data buffer */
484 /* DST SRC LEN!!! */
485 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
486 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE, &actlen, USB_CNTL_TIMEOUT*5);
487 if (result < 0)
488 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
489 return result;
490}
491
wdenkaffae2b2002-08-17 09:36:01 +0000492/* FIXME: we also need a CBI_command which sets up the completion
493 * interrupt, and waits for it
494 */
495int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
496{
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200497 int result = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000498 int dir_in,retry;
499 unsigned int pipe;
500 unsigned long status;
501
502 retry=5;
503 dir_in=US_DIRECTION(srb->cmd[0]);
504
505 if(dir_in)
506 pipe=usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
507 else
508 pipe=usb_sndbulkpipe(us->pusb_dev, us->ep_out);
509 while(retry--) {
510 USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry);
511#ifdef USB_STOR_DEBUG
512 usb_show_srb(srb);
513#endif
514 /* let's send the command via the control pipe */
515 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
516 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
517 0, us->ifnum,
518 srb->cmd, srb->cmdlen, USB_CNTL_TIMEOUT*5);
519 USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result,us->pusb_dev->status);
520 /* check the return code for the command */
521 if (result < 0) {
522 if(us->pusb_dev->status & USB_ST_STALLED) {
523 status=us->pusb_dev->status;
524 USB_STOR_PRINTF(" stall during command found, clear pipe\n");
525 usb_clear_halt(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0));
526 us->pusb_dev->status=status;
527 }
528 USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb->cmd[0],us->pusb_dev->status);
529 return result;
530 }
531 /* transfer the data payload for this command, if one exists*/
532
533 USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result,dir_in ? "IN" : "OUT",srb->datalen);
534 if (srb->datalen) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200535 result = us_one_transfer(us, pipe, (char *)srb->pdata,srb->datalen);
wdenkaffae2b2002-08-17 09:36:01 +0000536 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);
537 if(!(us->pusb_dev->status & USB_ST_NAK_REC))
538 break;
539 } /* if (srb->datalen) */
540 else
541 break;
542 }
543 /* return result */
544
545 return result;
546}
547
548
wdenk80885a92004-02-26 23:46:20 +0000549int usb_stor_CBI_get_status (ccb * srb, struct us_data *us)
wdenkaffae2b2002-08-17 09:36:01 +0000550{
551 int timeout;
552
wdenk80885a92004-02-26 23:46:20 +0000553 us->ip_wanted = 1;
554 submit_int_msg (us->pusb_dev, us->irqpipe,
555 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
556 timeout = 1000;
557 while (timeout--) {
558 if ((volatile int *) us->ip_wanted == 0)
wdenkaffae2b2002-08-17 09:36:01 +0000559 break;
wdenk80885a92004-02-26 23:46:20 +0000560 wait_ms (10);
wdenkaffae2b2002-08-17 09:36:01 +0000561 }
562 if (us->ip_wanted) {
wdenk80885a92004-02-26 23:46:20 +0000563 printf (" Did not get interrupt on CBI\n");
wdenkaffae2b2002-08-17 09:36:01 +0000564 us->ip_wanted = 0;
565 return USB_STOR_TRANSPORT_ERROR;
566 }
wdenk80885a92004-02-26 23:46:20 +0000567 USB_STOR_PRINTF
568 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
569 us->ip_data, us->pusb_dev->irq_act_len,
570 us->pusb_dev->irq_status);
wdenkaffae2b2002-08-17 09:36:01 +0000571 /* UFI gives us ASC and ASCQ, like a request sense */
572 if (us->subclass == US_SC_UFI) {
573 if (srb->cmd[0] == SCSI_REQ_SENSE ||
574 srb->cmd[0] == SCSI_INQUIRY)
575 return USB_STOR_TRANSPORT_GOOD; /* Good */
wdenk80885a92004-02-26 23:46:20 +0000576 else if (us->ip_data)
577 return USB_STOR_TRANSPORT_FAILED;
wdenkaffae2b2002-08-17 09:36:01 +0000578 else
wdenk80885a92004-02-26 23:46:20 +0000579 return USB_STOR_TRANSPORT_GOOD;
wdenkaffae2b2002-08-17 09:36:01 +0000580 }
581 /* otherwise, we interpret the data normally */
582 switch (us->ip_data) {
wdenk80885a92004-02-26 23:46:20 +0000583 case 0x0001:
584 return USB_STOR_TRANSPORT_GOOD;
585 case 0x0002:
586 return USB_STOR_TRANSPORT_FAILED;
587 default:
588 return USB_STOR_TRANSPORT_ERROR;
589 } /* switch */
wdenkaffae2b2002-08-17 09:36:01 +0000590 return USB_STOR_TRANSPORT_ERROR;
591}
592
593#define USB_TRANSPORT_UNKNOWN_RETRY 5
594#define USB_TRANSPORT_NOT_READY_RETRY 10
595
wdenk149dded2003-09-10 18:20:28 +0000596/* clear a stall on an endpoint - special for BBB devices */
597int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
598{
599 int result;
600
601 /* ENDPOINT_HALT = 0, so set value to 0 */
602 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
603 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
604 0, endpt, 0, 0, USB_CNTL_TIMEOUT*5);
605 return result;
606}
607
608int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
609{
610 int result, retry;
611 int dir_in;
612 int actlen, data_actlen;
613 unsigned int pipe, pipein, pipeout;
614 umass_bbb_csw_t csw;
615#ifdef BBB_XPORT_TRACE
616 unsigned char *ptr;
617 int index;
618#endif
619
620 dir_in = US_DIRECTION(srb->cmd[0]);
621
622 /* COMMAND phase */
623 USB_STOR_PRINTF("COMMAND phase\n");
624 result = usb_stor_BBB_comdat(srb, us);
625 if (result < 0) {
626 USB_STOR_PRINTF("failed to send CBW status %ld\n",
627 us->pusb_dev->status);
628 usb_stor_BBB_reset(us);
629 return USB_STOR_TRANSPORT_FAILED;
630 }
631 wait_ms(5);
632 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
633 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
634 /* DATA phase + error handling */
wdenk149dded2003-09-10 18:20:28 +0000635 data_actlen = 0;
636 /* no data, go immediately to the STATUS phase */
637 if (srb->datalen == 0)
638 goto st;
wdenk80885a92004-02-26 23:46:20 +0000639 USB_STOR_PRINTF("DATA phase\n");
wdenk149dded2003-09-10 18:20:28 +0000640 if (dir_in)
641 pipe = pipein;
642 else
643 pipe = pipeout;
644 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen, &data_actlen, USB_CNTL_TIMEOUT*5);
645 /* special handling of STALL in DATA phase */
646 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
wdenka43278a2003-09-11 19:48:06 +0000647 USB_STOR_PRINTF("DATA:stall\n");
wdenk149dded2003-09-10 18:20:28 +0000648 /* clear the STALL on the endpoint */
649 result = usb_stor_BBB_clear_endpt_stall(us, dir_in? us->ep_in : us->ep_out);
650 if (result >= 0)
651 /* continue on to STATUS phase */
652 goto st;
653 }
654 if (result < 0) {
655 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
656 us->pusb_dev->status);
657 usb_stor_BBB_reset(us);
658 return USB_STOR_TRANSPORT_FAILED;
659 }
660#ifdef BBB_XPORT_TRACE
661 for (index = 0; index < data_actlen; index++)
662 printf("pdata[%d] %#x ", index, srb->pdata[index]);
663 printf("\n");
664#endif
665 /* STATUS phase + error handling */
666 st:
667 retry = 0;
668 again:
669 USB_STOR_PRINTF("STATUS phase\n");
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200670 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200671 &actlen, USB_CNTL_TIMEOUT*5);
672
wdenk149dded2003-09-10 18:20:28 +0000673 /* special handling of STALL in STATUS phase */
674 if((result < 0) && (retry < 1) && (us->pusb_dev->status & USB_ST_STALLED)) {
675 USB_STOR_PRINTF("STATUS:stall\n");
676 /* clear the STALL on the endpoint */
677 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
678 if (result >= 0 && (retry++ < 1))
679 /* do a retry */
680 goto again;
681 }
682 if (result < 0) {
683 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
684 us->pusb_dev->status);
685 usb_stor_BBB_reset(us);
686 return USB_STOR_TRANSPORT_FAILED;
687 }
688#ifdef BBB_XPORT_TRACE
689 ptr = (unsigned char *)&csw;
690 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
691 printf("ptr[%d] %#x ", index, ptr[index]);
692 printf("\n");
693#endif
694 /* misuse pipe to get the residue */
695 pipe = swap_32(csw.dCSWDataResidue);
696 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
697 pipe = srb->datalen - data_actlen;
698 if (CSWSIGNATURE != swap_32(csw.dCSWSignature)) {
699 USB_STOR_PRINTF("!CSWSIGNATURE\n");
700 usb_stor_BBB_reset(us);
701 return USB_STOR_TRANSPORT_FAILED;
702 } else if ((CBWTag - 1) != swap_32(csw.dCSWTag)) {
703 USB_STOR_PRINTF("!Tag\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 (csw.bCSWStatus == CSWSTATUS_PHASE) {
711 USB_STOR_PRINTF("=PHASE\n");
712 usb_stor_BBB_reset(us);
713 return USB_STOR_TRANSPORT_FAILED;
714 } else if (data_actlen > srb->datalen) {
715 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
716 data_actlen, srb->datalen);
717 return USB_STOR_TRANSPORT_FAILED;
718 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
719 USB_STOR_PRINTF("FAILED\n");
720 return USB_STOR_TRANSPORT_FAILED;
721 }
722
723 return result;
724}
725
wdenkaffae2b2002-08-17 09:36:01 +0000726int usb_stor_CB_transport(ccb *srb, struct us_data *us)
727{
728 int result,status;
729 ccb *psrb;
730 ccb reqsrb;
731 int retry,notready;
732
733 psrb=&reqsrb;
734 status=USB_STOR_TRANSPORT_GOOD;
735 retry=0;
736 notready=0;
737 /* issue the command */
738do_retry:
739 result=usb_stor_CB_comdat(srb,us);
740 USB_STOR_PRINTF("command / Data returned %d, status %X\n",result,us->pusb_dev->status);
741 /* if this is an CBI Protocol, get IRQ */
742 if(us->protocol==US_PR_CBI) {
743 status=usb_stor_CBI_get_status(srb,us);
744 /* if the status is error, report it */
745 if(status==USB_STOR_TRANSPORT_ERROR) {
746 USB_STOR_PRINTF(" USB CBI Command Error\n");
747 return status;
748 }
749 srb->sense_buf[12]=(unsigned char)(us->ip_data>>8);
750 srb->sense_buf[13]=(unsigned char)(us->ip_data&0xff);
751 if(!us->ip_data) {
752 /* if the status is good, report it */
753 if(status==USB_STOR_TRANSPORT_GOOD) {
754 USB_STOR_PRINTF(" USB CBI Command Good\n");
755 return status;
756 }
757 }
758 }
759 /* do we have to issue an auto request? */
760 /* HERE we have to check the result */
761 if((result<0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
762 USB_STOR_PRINTF("ERROR %X\n",us->pusb_dev->status);
763 us->transport_reset(us);
764 return USB_STOR_TRANSPORT_ERROR;
765 }
766 if((us->protocol==US_PR_CBI) &&
767 ((srb->cmd[0]==SCSI_REQ_SENSE) ||
wdenk80885a92004-02-26 23:46:20 +0000768 (srb->cmd[0]==SCSI_INQUIRY))) { /* do not issue an autorequest after request sense */
wdenkaffae2b2002-08-17 09:36:01 +0000769 USB_STOR_PRINTF("No auto request and good\n");
770 return USB_STOR_TRANSPORT_GOOD;
771 }
772 /* issue an request_sense */
773 memset(&psrb->cmd[0],0,12);
774 psrb->cmd[0]=SCSI_REQ_SENSE;
775 psrb->cmd[1]=srb->lun<<5;
776 psrb->cmd[4]=18;
777 psrb->datalen=18;
778 psrb->pdata=&srb->sense_buf[0];
779 psrb->cmdlen=12;
780 /* issue the command */
781 result=usb_stor_CB_comdat(psrb,us);
782 USB_STOR_PRINTF("auto request returned %d\n",result);
783 /* if this is an CBI Protocol, get IRQ */
784 if(us->protocol==US_PR_CBI) {
wdenk80885a92004-02-26 23:46:20 +0000785 status=usb_stor_CBI_get_status(psrb,us);
wdenkaffae2b2002-08-17 09:36:01 +0000786 }
787 if((result<0)&&!(us->pusb_dev->status & USB_ST_STALLED)) {
788 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us->pusb_dev->status);
789 return USB_STOR_TRANSPORT_ERROR;
790 }
791 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]);
792 /* Check the auto request result */
793 if((srb->sense_buf[2]==0) &&
794 (srb->sense_buf[12]==0) &&
795 (srb->sense_buf[13]==0)) /* ok, no sense */
796 return USB_STOR_TRANSPORT_GOOD;
797 /* Check the auto request result */
798 switch(srb->sense_buf[2]) {
wdenk149dded2003-09-10 18:20:28 +0000799 case 0x01: /* Recovered Error */
800 return USB_STOR_TRANSPORT_GOOD;
wdenk80885a92004-02-26 23:46:20 +0000801 break;
wdenk149dded2003-09-10 18:20:28 +0000802 case 0x02: /* Not Ready */
803 if(notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
804 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
805 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
806 return USB_STOR_TRANSPORT_FAILED;
807 } else {
808 wait_ms(100);
809 goto do_retry;
810 }
811 break;
812 default:
813 if(retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
814 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
815 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
816 return USB_STOR_TRANSPORT_FAILED;
817 } else {
818 goto do_retry;
819 }
820 break;
wdenkaffae2b2002-08-17 09:36:01 +0000821 }
822 return USB_STOR_TRANSPORT_FAILED;
823}
824
825
wdenkaffae2b2002-08-17 09:36:01 +0000826static int usb_inquiry(ccb *srb,struct us_data *ss)
827{
828 int retry,i;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200829 retry=5;
wdenkaffae2b2002-08-17 09:36:01 +0000830 do {
831 memset(&srb->cmd[0],0,12);
832 srb->cmd[0]=SCSI_INQUIRY;
833 srb->cmd[1]=srb->lun<<5;
834 srb->cmd[4]=36;
835 srb->datalen=36;
836 srb->cmdlen=12;
837 i=ss->transport(srb,ss);
838 USB_STOR_PRINTF("inquiry returns %d\n",i);
839 if(i==0)
840 break;
wdenk149dded2003-09-10 18:20:28 +0000841 } while(retry--);
842
wdenkaffae2b2002-08-17 09:36:01 +0000843 if(!retry) {
844 printf("error in inquiry\n");
845 return -1;
846 }
847 return 0;
848}
849
850static int usb_request_sense(ccb *srb,struct us_data *ss)
851{
852 char *ptr;
wdenk80885a92004-02-26 23:46:20 +0000853
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200854 ptr=(char *)srb->pdata;
wdenkaffae2b2002-08-17 09:36:01 +0000855 memset(&srb->cmd[0],0,12);
856 srb->cmd[0]=SCSI_REQ_SENSE;
857 srb->cmd[1]=srb->lun<<5;
858 srb->cmd[4]=18;
859 srb->datalen=18;
860 srb->pdata=&srb->sense_buf[0];
861 srb->cmdlen=12;
862 ss->transport(srb,ss);
863 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200864 srb->pdata=(uchar *)ptr;
wdenkaffae2b2002-08-17 09:36:01 +0000865 return 0;
866}
867
868static int usb_test_unit_ready(ccb *srb,struct us_data *ss)
869{
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200870 int retries = 10;
wdenk149dded2003-09-10 18:20:28 +0000871
wdenkaffae2b2002-08-17 09:36:01 +0000872 do {
873 memset(&srb->cmd[0],0,12);
874 srb->cmd[0]=SCSI_TST_U_RDY;
875 srb->cmd[1]=srb->lun<<5;
876 srb->datalen=0;
877 srb->cmdlen=12;
wdenk149dded2003-09-10 18:20:28 +0000878 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
wdenkaffae2b2002-08-17 09:36:01 +0000879 return 0;
880 }
wdenk80885a92004-02-26 23:46:20 +0000881 usb_request_sense (srb, ss);
882 wait_ms (100);
wdenkaffae2b2002-08-17 09:36:01 +0000883 } while(retries--);
wdenk149dded2003-09-10 18:20:28 +0000884
wdenkaffae2b2002-08-17 09:36:01 +0000885 return -1;
886}
887
888static int usb_read_capacity(ccb *srb,struct us_data *ss)
889{
890 int retry;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200891 retry = 3; /* retries */
wdenkaffae2b2002-08-17 09:36:01 +0000892 do {
893 memset(&srb->cmd[0],0,12);
894 srb->cmd[0]=SCSI_RD_CAPAC;
895 srb->cmd[1]=srb->lun<<5;
896 srb->datalen=8;
897 srb->cmdlen=12;
898 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
899 return 0;
900 }
wdenk149dded2003-09-10 18:20:28 +0000901 } while(retry--);
902
wdenkaffae2b2002-08-17 09:36:01 +0000903 return -1;
904}
905
906static int usb_read_10(ccb *srb,struct us_data *ss, unsigned long start, unsigned short blocks)
907{
908 memset(&srb->cmd[0],0,12);
909 srb->cmd[0]=SCSI_READ10;
910 srb->cmd[1]=srb->lun<<5;
911 srb->cmd[2]=((unsigned char) (start>>24))&0xff;
912 srb->cmd[3]=((unsigned char) (start>>16))&0xff;
913 srb->cmd[4]=((unsigned char) (start>>8))&0xff;
914 srb->cmd[5]=((unsigned char) (start))&0xff;
915 srb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
916 srb->cmd[8]=(unsigned char) blocks & 0xff;
917 srb->cmdlen=12;
918 USB_STOR_PRINTF("read10: start %lx blocks %x\n",start,blocks);
919 return ss->transport(srb,ss);
920}
921
922
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +0200923#ifdef CONFIG_USB_BIN_FIXUP
924/*
925 * Some USB storage devices queried for SCSI identification data respond with
926 * binary strings, which if output to the console freeze the terminal. The
927 * workaround is to modify the vendor and product strings read from such
928 * device with proper values (as reported by 'usb info').
929 *
930 * Vendor and product length limits are taken from the definition of
931 * block_dev_desc_t in include/part.h.
932 */
933static void usb_bin_fixup(struct usb_device_descriptor descriptor,
934 unsigned char vendor[],
935 unsigned char product[]) {
936 const unsigned char max_vendor_len = 40;
937 const unsigned char max_product_len = 20;
938 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
Wolfgang Denk73652692006-10-20 12:02:59 +0200939 strncpy ((char *)vendor, "SMSC", max_vendor_len);
940 strncpy ((char *)product, "Flash Media Cntrller", max_product_len);
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +0200941 }
942}
943#endif /* CONFIG_USB_BIN_FIXUP */
944
wdenkaffae2b2002-08-17 09:36:01 +0000945#define USB_MAX_READ_BLK 20
946
Grant Likelyeb867a72007-02-20 09:05:45 +0100947unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, void *buffer)
wdenkaffae2b2002-08-17 09:36:01 +0000948{
949 unsigned long start,blks, buf_addr;
950 unsigned short smallblks;
951 struct usb_device *dev;
952 int retry,i;
wdenkf8d813e2004-03-02 14:05:39 +0000953 ccb *srb = &usb_ccb;
954
955 if (blkcnt == 0)
956 return 0;
957
958 device &= 0xff;
wdenkaffae2b2002-08-17 09:36:01 +0000959 /* Setup device
960 */
961 USB_STOR_PRINTF("\nusb_read: dev %d \n",device);
962 dev=NULL;
963 for(i=0;i<USB_MAX_DEVICE;i++) {
964 dev=usb_get_dev_index(i);
965 if(dev==NULL) {
966 return 0;
967 }
968 if(dev->devnum==usb_dev_desc[device].target)
969 break;
970 }
971
972 usb_disable_asynch(1); /* asynch transfer not allowed */
973 srb->lun=usb_dev_desc[device].lun;
974 buf_addr=(unsigned long)buffer;
975 start=blknr;
976 blks=blkcnt;
977 if(usb_test_unit_ready(srb,(struct us_data *)dev->privptr)) {
978 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
979 srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
980 return 0;
981 }
982 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks, buf_addr);
983 do {
984 retry=2;
985 srb->pdata=(unsigned char *)buf_addr;
986 if(blks>USB_MAX_READ_BLK) {
987 smallblks=USB_MAX_READ_BLK;
wdenk149dded2003-09-10 18:20:28 +0000988 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000989 smallblks=(unsigned short) blks;
990 }
991retry_it:
992 if(smallblks==USB_MAX_READ_BLK)
993 usb_show_progress();
994 srb->datalen=usb_dev_desc[device].blksz * smallblks;
995 srb->pdata=(unsigned char *)buf_addr;
996 if(usb_read_10(srb,(struct us_data *)dev->privptr, start, smallblks)) {
997 USB_STOR_PRINTF("Read ERROR\n");
998 usb_request_sense(srb,(struct us_data *)dev->privptr);
999 if(retry--)
1000 goto retry_it;
1001 blkcnt-=blks;
1002 break;
1003 }
1004 start+=smallblks;
1005 blks-=smallblks;
1006 buf_addr+=srb->datalen;
1007 } while(blks!=0);
1008 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
1009 usb_disable_asynch(0); /* asynch transfer allowed */
1010 if(blkcnt>=USB_MAX_READ_BLK)
1011 printf("\n");
1012 return(blkcnt);
1013}
1014
1015
1016/* Probe to see if a new device is actually a Storage device */
1017int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss)
1018{
1019 struct usb_interface_descriptor *iface;
1020 int i;
1021 unsigned int flags = 0;
1022
1023 int protocol = 0;
1024 int subclass = 0;
1025
wdenkaffae2b2002-08-17 09:36:01 +00001026 /* let's examine the device now */
1027 iface = &dev->config.if_desc[ifnum];
1028
1029#if 0
1030 /* this is the place to patch some storage devices */
1031 USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
1032 if ((dev->descriptor.idVendor) == 0x066b && (dev->descriptor.idProduct) == 0x0103) {
1033 USB_STOR_PRINTF("patched for E-USB\n");
1034 protocol = US_PR_CB;
1035 subclass = US_SC_UFI; /* an assumption */
1036 }
1037#endif
1038
1039 if (dev->descriptor.bDeviceClass != 0 ||
1040 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1041 iface->bInterfaceSubClass < US_SC_MIN ||
1042 iface->bInterfaceSubClass > US_SC_MAX) {
1043 /* if it's not a mass storage, we go no further */
1044 return 0;
1045 }
1046
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001047 memset(ss, 0, sizeof(struct us_data));
1048
wdenkaffae2b2002-08-17 09:36:01 +00001049 /* At this point, we know we've got a live one */
1050 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1051
1052 /* Initialize the us_data structure with some useful info */
1053 ss->flags = flags;
1054 ss->ifnum = ifnum;
1055 ss->pusb_dev = dev;
1056 ss->attention_done = 0;
1057
1058 /* If the device has subclass and protocol, then use that. Otherwise,
1059 * take data from the specific interface.
1060 */
1061 if (subclass) {
1062 ss->subclass = subclass;
1063 ss->protocol = protocol;
1064 } else {
1065 ss->subclass = iface->bInterfaceSubClass;
1066 ss->protocol = iface->bInterfaceProtocol;
1067 }
1068
1069 /* set the handler pointers based on the protocol */
1070 USB_STOR_PRINTF("Transport: ");
1071 switch (ss->protocol) {
1072 case US_PR_CB:
1073 USB_STOR_PRINTF("Control/Bulk\n");
1074 ss->transport = usb_stor_CB_transport;
1075 ss->transport_reset = usb_stor_CB_reset;
1076 break;
1077
1078 case US_PR_CBI:
1079 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1080 ss->transport = usb_stor_CB_transport;
1081 ss->transport_reset = usb_stor_CB_reset;
1082 break;
wdenk149dded2003-09-10 18:20:28 +00001083 case US_PR_BULK:
1084 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1085 ss->transport = usb_stor_BBB_transport;
1086 ss->transport_reset = usb_stor_BBB_reset;
1087 break;
wdenkaffae2b2002-08-17 09:36:01 +00001088 default:
wdenk80885a92004-02-26 23:46:20 +00001089 printf("USB Storage Transport unknown / not yet implemented\n");
wdenkaffae2b2002-08-17 09:36:01 +00001090 return 0;
1091 break;
1092 }
1093
1094 /*
1095 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1096 * An optional interrupt is OK (necessary for CBI protocol).
1097 * We will ignore any others.
1098 */
1099 for (i = 0; i < iface->bNumEndpoints; i++) {
1100 /* is it an BULK endpoint? */
1101 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1102 == USB_ENDPOINT_XFER_BULK) {
1103 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1104 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1105 USB_ENDPOINT_NUMBER_MASK;
1106 else
1107 ss->ep_out = iface->ep_desc[i].bEndpointAddress &
1108 USB_ENDPOINT_NUMBER_MASK;
1109 }
1110
1111 /* is it an interrupt endpoint? */
1112 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1113 == USB_ENDPOINT_XFER_INT) {
1114 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1115 USB_ENDPOINT_NUMBER_MASK;
1116 ss->irqinterval = iface->ep_desc[i].bInterval;
1117 }
1118 }
1119 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1120 ss->ep_in, ss->ep_out, ss->ep_int);
1121
1122 /* Do some basic sanity checks, and bail if we find a problem */
1123 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
1124 !ss->ep_in || !ss->ep_out ||
1125 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1126 USB_STOR_PRINTF("Problems with device\n");
1127 return 0;
1128 }
1129 /* set class specific stuff */
wdenk149dded2003-09-10 18:20:28 +00001130 /* We only handle certain protocols. Currently, these are
1131 * the only ones.
wdenk80885a92004-02-26 23:46:20 +00001132 * The SFF8070 accepts the requests used in u-boot
wdenkaffae2b2002-08-17 09:36:01 +00001133 */
wdenk80885a92004-02-26 23:46:20 +00001134 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1135 ss->subclass != US_SC_8070) {
wdenkaffae2b2002-08-17 09:36:01 +00001136 printf("Sorry, protocol %d not yet supported.\n",ss->subclass);
1137 return 0;
1138 }
wdenk149dded2003-09-10 18:20:28 +00001139 if(ss->ep_int) { /* we had found an interrupt endpoint, prepare irq pipe */
wdenkaffae2b2002-08-17 09:36:01 +00001140 /* set up the IRQ pipe and handler */
1141
1142 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1143 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1144 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1145 dev->irq_handle=usb_stor_irq;
wdenkaffae2b2002-08-17 09:36:01 +00001146 }
wdenka43278a2003-09-11 19:48:06 +00001147 dev->privptr=(void *)ss;
wdenkaffae2b2002-08-17 09:36:01 +00001148 return 1;
1149}
1150
1151int usb_stor_get_info(struct usb_device *dev,struct us_data *ss,block_dev_desc_t *dev_desc)
1152{
1153 unsigned char perq,modi;
1154 unsigned long cap[2];
1155 unsigned long *capacity,*blksz;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001156 ccb *pccb = &usb_ccb;
wdenkaffae2b2002-08-17 09:36:01 +00001157
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001158 /* for some reasons a couple of devices would not survive this reset */
1159 if (
1160 /* Sony USM256E */
1161 (dev->descriptor.idVendor == 0x054c &&
1162 dev->descriptor.idProduct == 0x019e)
1163
1164 ||
1165 /* USB007 Mini-USB2 Flash Drive */
1166 (dev->descriptor.idVendor == 0x066f &&
1167 dev->descriptor.idProduct == 0x2010)
Bartlomiej Siekaf88a0ae2006-07-13 15:32:16 +02001168 ||
1169 /* SanDisk Corporation Cruzer Micro 20044318410546613953 */
1170 (dev->descriptor.idVendor == 0x0781 &&
1171 dev->descriptor.idProduct == 0x5151)
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001172 )
1173 USB_STOR_PRINTF("usb_stor_get_info: skipping RESET..\n");
Wolfgang Denk095b8a32005-08-02 17:06:17 +02001174 else
wdenk2729af92004-05-03 20:45:30 +00001175 ss->transport_reset(ss);
wdenkaffae2b2002-08-17 09:36:01 +00001176
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001177 pccb->pdata = usb_stor_buf;
1178
1179 dev_desc->target = dev->devnum;
1180 pccb->lun = dev_desc->lun;
wdenkaffae2b2002-08-17 09:36:01 +00001181 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1182
1183 if(usb_inquiry(pccb,ss))
1184 return -1;
Wolfgang Denk095b8a32005-08-02 17:06:17 +02001185
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001186 perq = usb_stor_buf[0];
1187 modi = usb_stor_buf[1];
1188 if((perq & 0x1f) == 0x1f) {
wdenkaffae2b2002-08-17 09:36:01 +00001189 return 0; /* skip unknown devices */
1190 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001191 if((modi&0x80) == 0x80) {/* drive is removable */
1192 dev_desc->removable = 1;
wdenkaffae2b2002-08-17 09:36:01 +00001193 }
1194 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1195 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1196 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001197 dev_desc->vendor[8] = 0;
1198 dev_desc->product[16] = 0;
1199 dev_desc->revision[4] = 0;
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +02001200#ifdef CONFIG_USB_BIN_FIXUP
Wolfgang Denk409ecdc2007-11-18 16:36:27 +01001201 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor, (uchar *)dev_desc->product);
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +02001202#endif /* CONFIG_USB_BIN_FIXUP */
wdenkaffae2b2002-08-17 09:36:01 +00001203 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf[2],usb_stor_buf[3]);
1204 if(usb_test_unit_ready(pccb,ss)) {
1205 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 Denk9c998aa2005-07-21 11:57:57 +02001206 if(dev_desc->removable == 1) {
1207 dev_desc->type = perq;
wdenkaffae2b2002-08-17 09:36:01 +00001208 return 1;
1209 }
1210 else
1211 return 0;
1212 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001213 pccb->pdata = (unsigned char *)&cap[0];
wdenkaffae2b2002-08-17 09:36:01 +00001214 memset(pccb->pdata,0,8);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001215 if(usb_read_capacity(pccb,ss) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +00001216 printf("READ_CAP ERROR\n");
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001217 cap[0] = 2880;
1218 cap[1] = 0x200;
wdenkaffae2b2002-08-17 09:36:01 +00001219 }
1220 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap[0],cap[1]);
1221#if 0
1222 if(cap[0]>(0x200000 * 10)) /* greater than 10 GByte */
1223 cap[0]>>=16;
1224#endif
wdenk149dded2003-09-10 18:20:28 +00001225#ifdef LITTLEENDIAN
1226 cap[0] = ((unsigned long)(
1227 (((unsigned long)(cap[0]) & (unsigned long)0x000000ffUL) << 24) |
1228 (((unsigned long)(cap[0]) & (unsigned long)0x0000ff00UL) << 8) |
1229 (((unsigned long)(cap[0]) & (unsigned long)0x00ff0000UL) >> 8) |
1230 (((unsigned long)(cap[0]) & (unsigned long)0xff000000UL) >> 24) ));
1231 cap[1] = ((unsigned long)(
1232 (((unsigned long)(cap[1]) & (unsigned long)0x000000ffUL) << 24) |
1233 (((unsigned long)(cap[1]) & (unsigned long)0x0000ff00UL) << 8) |
1234 (((unsigned long)(cap[1]) & (unsigned long)0x00ff0000UL) >> 8) |
1235 (((unsigned long)(cap[1]) & (unsigned long)0xff000000UL) >> 24) ));
1236#endif
1237 /* this assumes bigendian! */
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001238 cap[0] += 1;
1239 capacity = &cap[0];
1240 blksz = &cap[1];
wdenkaffae2b2002-08-17 09:36:01 +00001241 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity,*blksz);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001242 dev_desc->lba = *capacity;
1243 dev_desc->blksz = *blksz;
1244 dev_desc->type = perq;
wdenkaffae2b2002-08-17 09:36:01 +00001245 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1246 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1247
1248 init_part(dev_desc);
1249
1250 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1251 return 1;
1252}
1253
wdenkaffae2b2002-08-17 09:36:01 +00001254#endif /* CONFIG_USB_STORAGE */
Jon Loeliger90253172007-07-10 11:02:44 -05001255#endif