blob: 3e113b4084ee64bb11c71a358d0b574e1ef04b74 [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>
Christian Eggersc9182612008-05-21 22:12:00 +020055#include <asm/byteorder.h>
wdenkaffae2b2002-08-17 09:36:01 +000056#include <asm/processor.h>
57
58
Jon Loeligerc3517f92007-07-08 18:10:08 -050059#if defined(CONFIG_CMD_USB)
Grant Likely735dd972007-02-20 09:04:34 +010060#include <part.h>
wdenkaffae2b2002-08-17 09:36:01 +000061#include <usb.h>
62
63#ifdef CONFIG_USB_STORAGE
64
wdenk80885a92004-02-26 23:46:20 +000065#undef USB_STOR_DEBUG
66#undef BBB_COMDAT_TRACE
67#undef BBB_XPORT_TRACE
wdenkaffae2b2002-08-17 09:36:01 +000068
69#ifdef USB_STOR_DEBUG
wdenk80885a92004-02-26 23:46:20 +000070#define USB_STOR_PRINTF(fmt,args...) printf (fmt ,##args)
wdenkaffae2b2002-08-17 09:36:01 +000071#else
72#define USB_STOR_PRINTF(fmt,args...)
73#endif
74
75#include <scsi.h>
76/* direction table -- this indicates the direction of the data
77 * transfer for each command code -- a 1 indicates input
78 */
79unsigned char us_direction[256/8] = {
80 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
81 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
84};
85#define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
86
87static unsigned char usb_stor_buf[512];
88static ccb usb_ccb;
89
90/*
91 * CBI style
92 */
93
94#define US_CBI_ADSC 0
95
wdenk149dded2003-09-10 18:20:28 +000096/*
97 * BULK only
98 */
99#define US_BBB_RESET 0xff
100#define US_BBB_GET_MAX_LUN 0xfe
101
102/* Command Block Wrapper */
103typedef struct {
104 __u32 dCBWSignature;
105# define CBWSIGNATURE 0x43425355
106 __u32 dCBWTag;
107 __u32 dCBWDataTransferLength;
108 __u8 bCBWFlags;
109# define CBWFLAGS_OUT 0x00
110# define CBWFLAGS_IN 0x80
111 __u8 bCBWLUN;
112 __u8 bCDBLength;
113# define CBWCDBLENGTH 16
114 __u8 CBWCDB[CBWCDBLENGTH];
115} umass_bbb_cbw_t;
wdenk80885a92004-02-26 23:46:20 +0000116#define UMASS_BBB_CBW_SIZE 31
wdenk149dded2003-09-10 18:20:28 +0000117static __u32 CBWTag = 0;
118
119/* Command Status Wrapper */
120typedef struct {
121 __u32 dCSWSignature;
122# define CSWSIGNATURE 0x53425355
123 __u32 dCSWTag;
124 __u32 dCSWDataResidue;
125 __u8 bCSWStatus;
126# define CSWSTATUS_GOOD 0x0
wdenk80885a92004-02-26 23:46:20 +0000127# define CSWSTATUS_FAILED 0x1
wdenk149dded2003-09-10 18:20:28 +0000128# define CSWSTATUS_PHASE 0x2
129} umass_bbb_csw_t;
wdenk80885a92004-02-26 23:46:20 +0000130#define UMASS_BBB_CSW_SIZE 13
wdenkaffae2b2002-08-17 09:36:01 +0000131
132#define USB_MAX_STOR_DEV 5
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200133static int usb_max_devs = 0; /* number of highest available usb device */
wdenkaffae2b2002-08-17 09:36:01 +0000134
135static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
136
137struct us_data;
138typedef int (*trans_cmnd)(ccb*, struct us_data*);
139typedef int (*trans_reset)(struct us_data*);
140
141struct us_data {
wdenk80885a92004-02-26 23:46:20 +0000142 struct usb_device *pusb_dev; /* this usb_device */
wdenkaffae2b2002-08-17 09:36:01 +0000143 unsigned int flags; /* from filter initially */
144 unsigned char ifnum; /* interface number */
145 unsigned char ep_in; /* in endpoint */
146 unsigned char ep_out; /* out ....... */
147 unsigned char ep_int; /* interrupt . */
148 unsigned char subclass; /* as in overview */
149 unsigned char protocol; /* .............. */
wdenk80885a92004-02-26 23:46:20 +0000150 unsigned char attention_done; /* force attn on first cmd */
wdenkaffae2b2002-08-17 09:36:01 +0000151 unsigned short ip_data; /* interrupt data */
152 int action; /* what to do */
153 int ip_wanted; /* needed */
154 int *irq_handle; /* for USB int requests */
155 unsigned int irqpipe; /* pipe for release_irq */
156 unsigned char irqmaxp; /* max packed for irq Pipe */
wdenk80885a92004-02-26 23:46:20 +0000157 unsigned char irqinterval; /* Intervall for IRQ Pipe */
wdenkaffae2b2002-08-17 09:36:01 +0000158 ccb *srb; /* current srb */
159 trans_reset transport_reset; /* reset routine */
160 trans_cmnd transport; /* transport routine */
161};
162
163static struct us_data usb_stor[USB_MAX_STOR_DEV];
164
165
wdenk80885a92004-02-26 23:46:20 +0000166#define USB_STOR_TRANSPORT_GOOD 0
wdenkaffae2b2002-08-17 09:36:01 +0000167#define USB_STOR_TRANSPORT_FAILED -1
168#define USB_STOR_TRANSPORT_ERROR -2
169
170
wdenkaffae2b2002-08-17 09:36:01 +0000171int usb_stor_get_info(struct usb_device *dev, struct us_data *us, block_dev_desc_t *dev_desc);
172int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss);
Grant Likelyeb867a72007-02-20 09:05:45 +0100173unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, void *buffer);
wdenkaffae2b2002-08-17 09:36:01 +0000174struct usb_device * usb_get_dev_index(int index);
175void uhci_show_temp_int_td(void);
176
177block_dev_desc_t *usb_stor_get_dev(int index)
178{
Grant Likely735dd972007-02-20 09:04:34 +0100179 return (index < USB_MAX_STOR_DEV) ? &usb_dev_desc[index] : NULL;
wdenkaffae2b2002-08-17 09:36:01 +0000180}
181
182
183void usb_show_progress(void)
184{
185 printf(".");
186}
187
188/*********************************************************************************
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200189 * show info on storage devices; 'usb start/init' must be invoked earlier
190 * as we only retrieve structures populated during devices initialization
191 */
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100192int usb_stor_info(void)
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200193{
194 int i;
195
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100196 if (usb_max_devs > 0) {
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200197 for (i = 0; i < usb_max_devs; i++) {
198 printf (" Device %d: ", i);
199 dev_print(&usb_dev_desc[i]);
200 }
Markus Klotzbuecherb9e749e2008-03-26 18:26:43 +0100201 return 0;
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100202 }
Wolfgang Denk1aeed8d2008-04-13 09:59:26 -0700203
Markus Klotzbuecherb9e749e2008-03-26 18:26:43 +0100204 printf("No storage devices, perhaps not 'usb start'ed..?\n");
205 return 1;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200206}
207
208/*********************************************************************************
209 * scan the usb and reports device info
wdenkaffae2b2002-08-17 09:36:01 +0000210 * to the user if mode = 1
211 * returns current device or -1 if no
212 */
213int usb_stor_scan(int mode)
214{
215 unsigned char i;
216 struct usb_device *dev;
217
wdenk149dded2003-09-10 18:20:28 +0000218 /* GJ */
219 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
220
wdenkaffae2b2002-08-17 09:36:01 +0000221 if(mode==1) {
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200222 printf(" scanning bus for storage devices... ");
wdenkaffae2b2002-08-17 09:36:01 +0000223 }
224 usb_disable_asynch(1); /* asynch transfer not allowed */
225
226 for(i=0;i<USB_MAX_STOR_DEV;i++) {
227 memset(&usb_dev_desc[i],0,sizeof(block_dev_desc_t));
228 usb_dev_desc[i].target=0xff;
229 usb_dev_desc[i].if_type=IF_TYPE_USB;
230 usb_dev_desc[i].dev=i;
231 usb_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
232 usb_dev_desc[i].block_read=usb_stor_read;
233 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200234
wdenkaffae2b2002-08-17 09:36:01 +0000235 usb_max_devs=0;
236 for(i=0;i<USB_MAX_DEVICE;i++) {
237 dev=usb_get_dev_index(i); /* get device */
238 USB_STOR_PRINTF("i=%d\n",i);
239 if(dev==NULL) {
240 break; /* no more devices avaiable */
241 }
242 if(usb_storage_probe(dev,0,&usb_stor[usb_max_devs])) { /* ok, it is a storage devices */
243 /* get info and fill it in */
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200244 if(usb_stor_get_info(dev, &usb_stor[usb_max_devs], &usb_dev_desc[usb_max_devs]))
wdenkaffae2b2002-08-17 09:36:01 +0000245 usb_max_devs++;
wdenkaffae2b2002-08-17 09:36:01 +0000246 } /* if storage device */
247 if(usb_max_devs==USB_MAX_STOR_DEV) {
248 printf("max USB Storage Device reached: %d stopping\n",usb_max_devs);
249 break;
250 }
251 } /* for */
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200252
wdenkaffae2b2002-08-17 09:36:01 +0000253 usb_disable_asynch(0); /* asynch transfer allowed */
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200254 printf("%d Storage Device(s) found\n", usb_max_devs);
wdenkaffae2b2002-08-17 09:36:01 +0000255 if(usb_max_devs>0)
256 return 0;
257 else
258 return-1;
259}
260
261static int usb_stor_irq(struct usb_device *dev)
262{
263 struct us_data *us;
264 us=(struct us_data *)dev->privptr;
265
266 if(us->ip_wanted) {
267 us->ip_wanted=0;
268 }
269 return 0;
270}
271
272
273#ifdef USB_STOR_DEBUG
274
275static void usb_show_srb(ccb * pccb)
276{
277 int i;
278 printf("SRB: len %d datalen 0x%lX\n ",pccb->cmdlen,pccb->datalen);
279 for(i=0;i<12;i++) {
280 printf("%02X ",pccb->cmd[i]);
281 }
282 printf("\n");
283}
284
285static void display_int_status(unsigned long tmp)
286{
287 printf("Status: %s %s %s %s %s %s %s\n",
288 (tmp & USB_ST_ACTIVE) ? "Active" : "",
289 (tmp & USB_ST_STALLED) ? "Stalled" : "",
290 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
291 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
292 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
293 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
294 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
295}
296#endif
297/***********************************************************************
298 * Data transfer routines
299 ***********************************************************************/
300
301static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
302{
303 int max_size;
304 int this_xfer;
305 int result;
306 int partial;
307 int maxtry;
308 int stat;
309
310 /* determine the maximum packet size for these transfers */
311 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
312
313 /* while we have data left to transfer */
314 while (length) {
315
316 /* calculate how long this will be -- maximum or a remainder */
317 this_xfer = length > max_size ? max_size : length;
318 length -= this_xfer;
319
320 /* setup the retry counter */
321 maxtry = 10;
322
323 /* set up the transfer loop */
324 do {
325 /* transfer the data */
326 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
327 (unsigned int)buf, this_xfer, 11 - maxtry);
328 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
329 this_xfer, &partial, USB_CNTL_TIMEOUT*5);
330 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
331 result, partial, this_xfer);
332 if(us->pusb_dev->status!=0) {
333 /* if we stall, we need to clear it before we go on */
334#ifdef USB_STOR_DEBUG
335 display_int_status(us->pusb_dev->status);
336#endif
337 if (us->pusb_dev->status & USB_ST_STALLED) {
338 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
339 stat = us->pusb_dev->status;
340 usb_clear_halt(us->pusb_dev, pipe);
341 us->pusb_dev->status=stat;
342 if(this_xfer == partial) {
343 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us->pusb_dev->status);
344 return 0;
345 }
346 else
347 return result;
348 }
349 if (us->pusb_dev->status & USB_ST_NAK_REC) {
350 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
351 return result;
352 }
353 if(this_xfer == partial) {
354 USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us->pusb_dev->status);
355 return 0;
356 }
357 /* if our try counter reaches 0, bail out */
358 USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us->pusb_dev->status,partial);
359 if (!maxtry--)
360 return result;
361 }
362 /* update to show what data was transferred */
363 this_xfer -= partial;
364 buf += partial;
365 /* continue until this transfer is done */
366 } while ( this_xfer );
367 }
368
369 /* if we get here, we're done and successful */
370 return 0;
371}
372
wdenk149dded2003-09-10 18:20:28 +0000373static int usb_stor_BBB_reset(struct us_data *us)
374{
375 int result;
376 unsigned int pipe;
377
378 /*
379 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
380 *
381 * For Reset Recovery the host shall issue in the following order:
382 * a) a Bulk-Only Mass Storage Reset
383 * b) a Clear Feature HALT to the Bulk-In endpoint
384 * c) a Clear Feature HALT to the Bulk-Out endpoint
385 *
386 * This is done in 3 steps.
387 *
388 * If the reset doesn't succeed, the device should be port reset.
389 *
390 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
391 */
392 USB_STOR_PRINTF("BBB_reset\n");
393 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
394 US_BBB_RESET, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
395 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT*5);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200396
wdenk149dded2003-09-10 18:20:28 +0000397 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED))
398 {
399 USB_STOR_PRINTF("RESET:stall\n");
400 return -1;
401 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200402
wdenk149dded2003-09-10 18:20:28 +0000403 /* long wait for reset */
404 wait_ms(150);
405 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n",result,us->pusb_dev->status);
406 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
407 result = usb_clear_halt(us->pusb_dev, pipe);
408 /* long wait for reset */
409 wait_ms(150);
410 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",result,us->pusb_dev->status);
411 /* long wait for reset */
412 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
413 result = usb_clear_halt(us->pusb_dev, pipe);
414 wait_ms(150);
415 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing OUT endpoint\n",result,us->pusb_dev->status);
416 USB_STOR_PRINTF("BBB_reset done\n");
417 return 0;
418}
419
wdenkaffae2b2002-08-17 09:36:01 +0000420/* FIXME: this reset function doesn't really reset the port, and it
421 * should. Actually it should probably do what it's doing here, and
422 * reset the port physically
423 */
424static int usb_stor_CB_reset(struct us_data *us)
425{
426 unsigned char cmd[12];
427 int result;
428
429 USB_STOR_PRINTF("CB_reset\n");
430 memset(cmd, 0xFF, sizeof(cmd));
431 cmd[0] = SCSI_SEND_DIAG;
432 cmd[1] = 4;
433 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
434 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
435 0, us->ifnum, cmd, sizeof(cmd), USB_CNTL_TIMEOUT*5);
436
437 /* long wait for reset */
438 wait_ms(1500);
439 USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result,us->pusb_dev->status);
440 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
441 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
442
443 USB_STOR_PRINTF("CB_reset done\n");
444 return 0;
445}
446
wdenk149dded2003-09-10 18:20:28 +0000447/*
448 * Set up the command for a BBB device. Note that the actual SCSI
449 * command is copied into cbw.CBWCDB.
450 */
451int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
452{
453 int result;
454 int actlen;
455 int dir_in;
456 unsigned int pipe;
457 umass_bbb_cbw_t cbw;
458
459 dir_in = US_DIRECTION(srb->cmd[0]);
460
461#ifdef BBB_COMDAT_TRACE
462 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);
463 if (srb->cmdlen) {
464 for(result = 0;result < srb->cmdlen;result++)
465 printf("cmd[%d] %#x ", result, srb->cmd[result]);
466 printf("\n");
467 }
468#endif
469 /* sanity checks */
470 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
471 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
472 return -1;
473 }
474
475 /* always OUT to the ep */
476 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
477
Christian Eggersc9182612008-05-21 22:12:00 +0200478 cbw.dCBWSignature = cpu_to_le32(CBWSIGNATURE);
479 cbw.dCBWTag = cpu_to_le32(CBWTag++);
480 cbw.dCBWDataTransferLength = cpu_to_le32(srb->datalen);
wdenk149dded2003-09-10 18:20:28 +0000481 cbw.bCBWFlags = (dir_in? CBWFLAGS_IN : CBWFLAGS_OUT);
482 cbw.bCBWLUN = srb->lun;
483 cbw.bCDBLength = srb->cmdlen;
484 /* copy the command data into the CBW command data buffer */
485 /* DST SRC LEN!!! */
486 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
487 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE, &actlen, USB_CNTL_TIMEOUT*5);
488 if (result < 0)
489 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
490 return result;
491}
492
wdenkaffae2b2002-08-17 09:36:01 +0000493/* FIXME: we also need a CBI_command which sets up the completion
494 * interrupt, and waits for it
495 */
496int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
497{
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200498 int result = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000499 int dir_in,retry;
500 unsigned int pipe;
501 unsigned long status;
502
503 retry=5;
504 dir_in=US_DIRECTION(srb->cmd[0]);
505
506 if(dir_in)
507 pipe=usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
508 else
509 pipe=usb_sndbulkpipe(us->pusb_dev, us->ep_out);
510 while(retry--) {
511 USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry);
512#ifdef USB_STOR_DEBUG
513 usb_show_srb(srb);
514#endif
515 /* let's send the command via the control pipe */
516 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
517 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
518 0, us->ifnum,
519 srb->cmd, srb->cmdlen, USB_CNTL_TIMEOUT*5);
520 USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result,us->pusb_dev->status);
521 /* check the return code for the command */
522 if (result < 0) {
523 if(us->pusb_dev->status & USB_ST_STALLED) {
524 status=us->pusb_dev->status;
525 USB_STOR_PRINTF(" stall during command found, clear pipe\n");
526 usb_clear_halt(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0));
527 us->pusb_dev->status=status;
528 }
529 USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb->cmd[0],us->pusb_dev->status);
530 return result;
531 }
532 /* transfer the data payload for this command, if one exists*/
533
534 USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result,dir_in ? "IN" : "OUT",srb->datalen);
535 if (srb->datalen) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200536 result = us_one_transfer(us, pipe, (char *)srb->pdata,srb->datalen);
wdenkaffae2b2002-08-17 09:36:01 +0000537 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);
538 if(!(us->pusb_dev->status & USB_ST_NAK_REC))
539 break;
540 } /* if (srb->datalen) */
541 else
542 break;
543 }
544 /* return result */
545
546 return result;
547}
548
549
wdenk80885a92004-02-26 23:46:20 +0000550int usb_stor_CBI_get_status (ccb * srb, struct us_data *us)
wdenkaffae2b2002-08-17 09:36:01 +0000551{
552 int timeout;
553
wdenk80885a92004-02-26 23:46:20 +0000554 us->ip_wanted = 1;
555 submit_int_msg (us->pusb_dev, us->irqpipe,
556 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
557 timeout = 1000;
558 while (timeout--) {
559 if ((volatile int *) us->ip_wanted == 0)
wdenkaffae2b2002-08-17 09:36:01 +0000560 break;
wdenk80885a92004-02-26 23:46:20 +0000561 wait_ms (10);
wdenkaffae2b2002-08-17 09:36:01 +0000562 }
563 if (us->ip_wanted) {
wdenk80885a92004-02-26 23:46:20 +0000564 printf (" Did not get interrupt on CBI\n");
wdenkaffae2b2002-08-17 09:36:01 +0000565 us->ip_wanted = 0;
566 return USB_STOR_TRANSPORT_ERROR;
567 }
wdenk80885a92004-02-26 23:46:20 +0000568 USB_STOR_PRINTF
569 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
570 us->ip_data, us->pusb_dev->irq_act_len,
571 us->pusb_dev->irq_status);
wdenkaffae2b2002-08-17 09:36:01 +0000572 /* UFI gives us ASC and ASCQ, like a request sense */
573 if (us->subclass == US_SC_UFI) {
574 if (srb->cmd[0] == SCSI_REQ_SENSE ||
575 srb->cmd[0] == SCSI_INQUIRY)
576 return USB_STOR_TRANSPORT_GOOD; /* Good */
wdenk80885a92004-02-26 23:46:20 +0000577 else if (us->ip_data)
578 return USB_STOR_TRANSPORT_FAILED;
wdenkaffae2b2002-08-17 09:36:01 +0000579 else
wdenk80885a92004-02-26 23:46:20 +0000580 return USB_STOR_TRANSPORT_GOOD;
wdenkaffae2b2002-08-17 09:36:01 +0000581 }
582 /* otherwise, we interpret the data normally */
583 switch (us->ip_data) {
wdenk80885a92004-02-26 23:46:20 +0000584 case 0x0001:
585 return USB_STOR_TRANSPORT_GOOD;
586 case 0x0002:
587 return USB_STOR_TRANSPORT_FAILED;
588 default:
589 return USB_STOR_TRANSPORT_ERROR;
590 } /* switch */
wdenkaffae2b2002-08-17 09:36:01 +0000591 return USB_STOR_TRANSPORT_ERROR;
592}
593
594#define USB_TRANSPORT_UNKNOWN_RETRY 5
595#define USB_TRANSPORT_NOT_READY_RETRY 10
596
wdenk149dded2003-09-10 18:20:28 +0000597/* clear a stall on an endpoint - special for BBB devices */
598int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
599{
600 int result;
601
602 /* ENDPOINT_HALT = 0, so set value to 0 */
603 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
604 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
605 0, endpt, 0, 0, USB_CNTL_TIMEOUT*5);
606 return result;
607}
608
609int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
610{
611 int result, retry;
612 int dir_in;
613 int actlen, data_actlen;
614 unsigned int pipe, pipein, pipeout;
615 umass_bbb_csw_t csw;
616#ifdef BBB_XPORT_TRACE
617 unsigned char *ptr;
618 int index;
619#endif
620
621 dir_in = US_DIRECTION(srb->cmd[0]);
622
623 /* COMMAND phase */
624 USB_STOR_PRINTF("COMMAND phase\n");
625 result = usb_stor_BBB_comdat(srb, us);
626 if (result < 0) {
627 USB_STOR_PRINTF("failed to send CBW status %ld\n",
628 us->pusb_dev->status);
629 usb_stor_BBB_reset(us);
630 return USB_STOR_TRANSPORT_FAILED;
631 }
632 wait_ms(5);
633 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
634 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
635 /* DATA phase + error handling */
wdenk149dded2003-09-10 18:20:28 +0000636 data_actlen = 0;
637 /* no data, go immediately to the STATUS phase */
638 if (srb->datalen == 0)
639 goto st;
wdenk80885a92004-02-26 23:46:20 +0000640 USB_STOR_PRINTF("DATA phase\n");
wdenk149dded2003-09-10 18:20:28 +0000641 if (dir_in)
642 pipe = pipein;
643 else
644 pipe = pipeout;
645 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen, &data_actlen, USB_CNTL_TIMEOUT*5);
646 /* special handling of STALL in DATA phase */
647 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
wdenka43278a2003-09-11 19:48:06 +0000648 USB_STOR_PRINTF("DATA:stall\n");
wdenk149dded2003-09-10 18:20:28 +0000649 /* clear the STALL on the endpoint */
650 result = usb_stor_BBB_clear_endpt_stall(us, dir_in? us->ep_in : us->ep_out);
651 if (result >= 0)
652 /* continue on to STATUS phase */
653 goto st;
654 }
655 if (result < 0) {
656 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
657 us->pusb_dev->status);
658 usb_stor_BBB_reset(us);
659 return USB_STOR_TRANSPORT_FAILED;
660 }
661#ifdef BBB_XPORT_TRACE
662 for (index = 0; index < data_actlen; index++)
663 printf("pdata[%d] %#x ", index, srb->pdata[index]);
664 printf("\n");
665#endif
666 /* STATUS phase + error handling */
667 st:
668 retry = 0;
669 again:
670 USB_STOR_PRINTF("STATUS phase\n");
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200671 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200672 &actlen, USB_CNTL_TIMEOUT*5);
673
wdenk149dded2003-09-10 18:20:28 +0000674 /* special handling of STALL in STATUS phase */
675 if((result < 0) && (retry < 1) && (us->pusb_dev->status & USB_ST_STALLED)) {
676 USB_STOR_PRINTF("STATUS:stall\n");
677 /* clear the STALL on the endpoint */
678 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
679 if (result >= 0 && (retry++ < 1))
680 /* do a retry */
681 goto again;
682 }
683 if (result < 0) {
684 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
685 us->pusb_dev->status);
686 usb_stor_BBB_reset(us);
687 return USB_STOR_TRANSPORT_FAILED;
688 }
689#ifdef BBB_XPORT_TRACE
690 ptr = (unsigned char *)&csw;
691 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
692 printf("ptr[%d] %#x ", index, ptr[index]);
693 printf("\n");
694#endif
695 /* misuse pipe to get the residue */
Christian Eggersc9182612008-05-21 22:12:00 +0200696 pipe = le32_to_cpu(csw.dCSWDataResidue);
wdenk149dded2003-09-10 18:20:28 +0000697 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
698 pipe = srb->datalen - data_actlen;
Christian Eggersc9182612008-05-21 22:12:00 +0200699 if (CSWSIGNATURE != le32_to_cpu(csw.dCSWSignature)) {
wdenk149dded2003-09-10 18:20:28 +0000700 USB_STOR_PRINTF("!CSWSIGNATURE\n");
701 usb_stor_BBB_reset(us);
702 return USB_STOR_TRANSPORT_FAILED;
Christian Eggersc9182612008-05-21 22:12:00 +0200703 } else if ((CBWTag - 1) != le32_to_cpu(csw.dCSWTag)) {
wdenk149dded2003-09-10 18:20:28 +0000704 USB_STOR_PRINTF("!Tag\n");
705 usb_stor_BBB_reset(us);
706 return USB_STOR_TRANSPORT_FAILED;
707 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
708 USB_STOR_PRINTF(">PHASE\n");
709 usb_stor_BBB_reset(us);
710 return USB_STOR_TRANSPORT_FAILED;
711 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
712 USB_STOR_PRINTF("=PHASE\n");
713 usb_stor_BBB_reset(us);
714 return USB_STOR_TRANSPORT_FAILED;
715 } else if (data_actlen > srb->datalen) {
716 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
717 data_actlen, srb->datalen);
718 return USB_STOR_TRANSPORT_FAILED;
719 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
720 USB_STOR_PRINTF("FAILED\n");
721 return USB_STOR_TRANSPORT_FAILED;
722 }
723
724 return result;
725}
726
wdenkaffae2b2002-08-17 09:36:01 +0000727int usb_stor_CB_transport(ccb *srb, struct us_data *us)
728{
729 int result,status;
730 ccb *psrb;
731 ccb reqsrb;
732 int retry,notready;
733
734 psrb=&reqsrb;
735 status=USB_STOR_TRANSPORT_GOOD;
736 retry=0;
737 notready=0;
738 /* issue the command */
739do_retry:
740 result=usb_stor_CB_comdat(srb,us);
741 USB_STOR_PRINTF("command / Data returned %d, status %X\n",result,us->pusb_dev->status);
742 /* if this is an CBI Protocol, get IRQ */
743 if(us->protocol==US_PR_CBI) {
744 status=usb_stor_CBI_get_status(srb,us);
745 /* if the status is error, report it */
746 if(status==USB_STOR_TRANSPORT_ERROR) {
747 USB_STOR_PRINTF(" USB CBI Command Error\n");
748 return status;
749 }
750 srb->sense_buf[12]=(unsigned char)(us->ip_data>>8);
751 srb->sense_buf[13]=(unsigned char)(us->ip_data&0xff);
752 if(!us->ip_data) {
753 /* if the status is good, report it */
754 if(status==USB_STOR_TRANSPORT_GOOD) {
755 USB_STOR_PRINTF(" USB CBI Command Good\n");
756 return status;
757 }
758 }
759 }
760 /* do we have to issue an auto request? */
761 /* HERE we have to check the result */
762 if((result<0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
763 USB_STOR_PRINTF("ERROR %X\n",us->pusb_dev->status);
764 us->transport_reset(us);
765 return USB_STOR_TRANSPORT_ERROR;
766 }
767 if((us->protocol==US_PR_CBI) &&
768 ((srb->cmd[0]==SCSI_REQ_SENSE) ||
wdenk80885a92004-02-26 23:46:20 +0000769 (srb->cmd[0]==SCSI_INQUIRY))) { /* do not issue an autorequest after request sense */
wdenkaffae2b2002-08-17 09:36:01 +0000770 USB_STOR_PRINTF("No auto request and good\n");
771 return USB_STOR_TRANSPORT_GOOD;
772 }
773 /* issue an request_sense */
774 memset(&psrb->cmd[0],0,12);
775 psrb->cmd[0]=SCSI_REQ_SENSE;
776 psrb->cmd[1]=srb->lun<<5;
777 psrb->cmd[4]=18;
778 psrb->datalen=18;
779 psrb->pdata=&srb->sense_buf[0];
780 psrb->cmdlen=12;
781 /* issue the command */
782 result=usb_stor_CB_comdat(psrb,us);
783 USB_STOR_PRINTF("auto request returned %d\n",result);
784 /* if this is an CBI Protocol, get IRQ */
785 if(us->protocol==US_PR_CBI) {
wdenk80885a92004-02-26 23:46:20 +0000786 status=usb_stor_CBI_get_status(psrb,us);
wdenkaffae2b2002-08-17 09:36:01 +0000787 }
788 if((result<0)&&!(us->pusb_dev->status & USB_ST_STALLED)) {
789 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us->pusb_dev->status);
790 return USB_STOR_TRANSPORT_ERROR;
791 }
792 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]);
793 /* Check the auto request result */
794 if((srb->sense_buf[2]==0) &&
795 (srb->sense_buf[12]==0) &&
796 (srb->sense_buf[13]==0)) /* ok, no sense */
797 return USB_STOR_TRANSPORT_GOOD;
798 /* Check the auto request result */
799 switch(srb->sense_buf[2]) {
wdenk149dded2003-09-10 18:20:28 +0000800 case 0x01: /* Recovered Error */
801 return USB_STOR_TRANSPORT_GOOD;
wdenk80885a92004-02-26 23:46:20 +0000802 break;
wdenk149dded2003-09-10 18:20:28 +0000803 case 0x02: /* Not Ready */
804 if(notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
805 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
806 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
807 return USB_STOR_TRANSPORT_FAILED;
808 } else {
809 wait_ms(100);
810 goto do_retry;
811 }
812 break;
813 default:
814 if(retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
815 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
816 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
817 return USB_STOR_TRANSPORT_FAILED;
818 } else {
819 goto do_retry;
820 }
821 break;
wdenkaffae2b2002-08-17 09:36:01 +0000822 }
823 return USB_STOR_TRANSPORT_FAILED;
824}
825
826
wdenkaffae2b2002-08-17 09:36:01 +0000827static int usb_inquiry(ccb *srb,struct us_data *ss)
828{
829 int retry,i;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200830 retry=5;
wdenkaffae2b2002-08-17 09:36:01 +0000831 do {
832 memset(&srb->cmd[0],0,12);
833 srb->cmd[0]=SCSI_INQUIRY;
834 srb->cmd[1]=srb->lun<<5;
835 srb->cmd[4]=36;
836 srb->datalen=36;
837 srb->cmdlen=12;
838 i=ss->transport(srb,ss);
839 USB_STOR_PRINTF("inquiry returns %d\n",i);
840 if(i==0)
841 break;
wdenk149dded2003-09-10 18:20:28 +0000842 } while(retry--);
843
wdenkaffae2b2002-08-17 09:36:01 +0000844 if(!retry) {
845 printf("error in inquiry\n");
846 return -1;
847 }
848 return 0;
849}
850
851static int usb_request_sense(ccb *srb,struct us_data *ss)
852{
853 char *ptr;
wdenk80885a92004-02-26 23:46:20 +0000854
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200855 ptr=(char *)srb->pdata;
wdenkaffae2b2002-08-17 09:36:01 +0000856 memset(&srb->cmd[0],0,12);
857 srb->cmd[0]=SCSI_REQ_SENSE;
858 srb->cmd[1]=srb->lun<<5;
859 srb->cmd[4]=18;
860 srb->datalen=18;
861 srb->pdata=&srb->sense_buf[0];
862 srb->cmdlen=12;
863 ss->transport(srb,ss);
864 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 +0200865 srb->pdata=(uchar *)ptr;
wdenkaffae2b2002-08-17 09:36:01 +0000866 return 0;
867}
868
869static int usb_test_unit_ready(ccb *srb,struct us_data *ss)
870{
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200871 int retries = 10;
wdenk149dded2003-09-10 18:20:28 +0000872
wdenkaffae2b2002-08-17 09:36:01 +0000873 do {
874 memset(&srb->cmd[0],0,12);
875 srb->cmd[0]=SCSI_TST_U_RDY;
876 srb->cmd[1]=srb->lun<<5;
877 srb->datalen=0;
878 srb->cmdlen=12;
wdenk149dded2003-09-10 18:20:28 +0000879 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
wdenkaffae2b2002-08-17 09:36:01 +0000880 return 0;
881 }
wdenk80885a92004-02-26 23:46:20 +0000882 usb_request_sense (srb, ss);
883 wait_ms (100);
wdenkaffae2b2002-08-17 09:36:01 +0000884 } while(retries--);
wdenk149dded2003-09-10 18:20:28 +0000885
wdenkaffae2b2002-08-17 09:36:01 +0000886 return -1;
887}
888
889static int usb_read_capacity(ccb *srb,struct us_data *ss)
890{
891 int retry;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200892 retry = 3; /* retries */
wdenkaffae2b2002-08-17 09:36:01 +0000893 do {
894 memset(&srb->cmd[0],0,12);
895 srb->cmd[0]=SCSI_RD_CAPAC;
896 srb->cmd[1]=srb->lun<<5;
897 srb->datalen=8;
898 srb->cmdlen=12;
899 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
900 return 0;
901 }
wdenk149dded2003-09-10 18:20:28 +0000902 } while(retry--);
903
wdenkaffae2b2002-08-17 09:36:01 +0000904 return -1;
905}
906
907static int usb_read_10(ccb *srb,struct us_data *ss, unsigned long start, unsigned short blocks)
908{
909 memset(&srb->cmd[0],0,12);
910 srb->cmd[0]=SCSI_READ10;
911 srb->cmd[1]=srb->lun<<5;
912 srb->cmd[2]=((unsigned char) (start>>24))&0xff;
913 srb->cmd[3]=((unsigned char) (start>>16))&0xff;
914 srb->cmd[4]=((unsigned char) (start>>8))&0xff;
915 srb->cmd[5]=((unsigned char) (start))&0xff;
916 srb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
917 srb->cmd[8]=(unsigned char) blocks & 0xff;
918 srb->cmdlen=12;
919 USB_STOR_PRINTF("read10: start %lx blocks %x\n",start,blocks);
920 return ss->transport(srb,ss);
921}
922
923
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +0200924#ifdef CONFIG_USB_BIN_FIXUP
925/*
926 * Some USB storage devices queried for SCSI identification data respond with
927 * binary strings, which if output to the console freeze the terminal. The
928 * workaround is to modify the vendor and product strings read from such
929 * device with proper values (as reported by 'usb info').
930 *
931 * Vendor and product length limits are taken from the definition of
932 * block_dev_desc_t in include/part.h.
933 */
934static void usb_bin_fixup(struct usb_device_descriptor descriptor,
935 unsigned char vendor[],
936 unsigned char product[]) {
937 const unsigned char max_vendor_len = 40;
938 const unsigned char max_product_len = 20;
939 if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
Wolfgang Denk73652692006-10-20 12:02:59 +0200940 strncpy ((char *)vendor, "SMSC", max_vendor_len);
941 strncpy ((char *)product, "Flash Media Cntrller", max_product_len);
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +0200942 }
943}
944#endif /* CONFIG_USB_BIN_FIXUP */
945
wdenkaffae2b2002-08-17 09:36:01 +0000946#define USB_MAX_READ_BLK 20
947
Grant Likelyeb867a72007-02-20 09:05:45 +0100948unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, void *buffer)
wdenkaffae2b2002-08-17 09:36:01 +0000949{
950 unsigned long start,blks, buf_addr;
951 unsigned short smallblks;
952 struct usb_device *dev;
953 int retry,i;
wdenkf8d813e2004-03-02 14:05:39 +0000954 ccb *srb = &usb_ccb;
955
956 if (blkcnt == 0)
957 return 0;
958
959 device &= 0xff;
wdenkaffae2b2002-08-17 09:36:01 +0000960 /* Setup device
961 */
962 USB_STOR_PRINTF("\nusb_read: dev %d \n",device);
963 dev=NULL;
964 for(i=0;i<USB_MAX_DEVICE;i++) {
965 dev=usb_get_dev_index(i);
966 if(dev==NULL) {
967 return 0;
968 }
969 if(dev->devnum==usb_dev_desc[device].target)
970 break;
971 }
972
973 usb_disable_asynch(1); /* asynch transfer not allowed */
974 srb->lun=usb_dev_desc[device].lun;
975 buf_addr=(unsigned long)buffer;
976 start=blknr;
977 blks=blkcnt;
978 if(usb_test_unit_ready(srb,(struct us_data *)dev->privptr)) {
979 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
980 srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
981 return 0;
982 }
983 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks, buf_addr);
984 do {
985 retry=2;
986 srb->pdata=(unsigned char *)buf_addr;
987 if(blks>USB_MAX_READ_BLK) {
988 smallblks=USB_MAX_READ_BLK;
wdenk149dded2003-09-10 18:20:28 +0000989 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000990 smallblks=(unsigned short) blks;
991 }
992retry_it:
993 if(smallblks==USB_MAX_READ_BLK)
994 usb_show_progress();
995 srb->datalen=usb_dev_desc[device].blksz * smallblks;
996 srb->pdata=(unsigned char *)buf_addr;
997 if(usb_read_10(srb,(struct us_data *)dev->privptr, start, smallblks)) {
998 USB_STOR_PRINTF("Read ERROR\n");
999 usb_request_sense(srb,(struct us_data *)dev->privptr);
1000 if(retry--)
1001 goto retry_it;
1002 blkcnt-=blks;
1003 break;
1004 }
1005 start+=smallblks;
1006 blks-=smallblks;
1007 buf_addr+=srb->datalen;
1008 } while(blks!=0);
1009 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
1010 usb_disable_asynch(0); /* asynch transfer allowed */
1011 if(blkcnt>=USB_MAX_READ_BLK)
1012 printf("\n");
1013 return(blkcnt);
1014}
1015
1016
1017/* Probe to see if a new device is actually a Storage device */
1018int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss)
1019{
1020 struct usb_interface_descriptor *iface;
1021 int i;
1022 unsigned int flags = 0;
1023
1024 int protocol = 0;
1025 int subclass = 0;
1026
wdenkaffae2b2002-08-17 09:36:01 +00001027 /* let's examine the device now */
1028 iface = &dev->config.if_desc[ifnum];
1029
1030#if 0
1031 /* this is the place to patch some storage devices */
1032 USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
1033 if ((dev->descriptor.idVendor) == 0x066b && (dev->descriptor.idProduct) == 0x0103) {
1034 USB_STOR_PRINTF("patched for E-USB\n");
1035 protocol = US_PR_CB;
1036 subclass = US_SC_UFI; /* an assumption */
1037 }
1038#endif
1039
1040 if (dev->descriptor.bDeviceClass != 0 ||
1041 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1042 iface->bInterfaceSubClass < US_SC_MIN ||
1043 iface->bInterfaceSubClass > US_SC_MAX) {
1044 /* if it's not a mass storage, we go no further */
1045 return 0;
1046 }
1047
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001048 memset(ss, 0, sizeof(struct us_data));
1049
wdenkaffae2b2002-08-17 09:36:01 +00001050 /* At this point, we know we've got a live one */
1051 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1052
1053 /* Initialize the us_data structure with some useful info */
1054 ss->flags = flags;
1055 ss->ifnum = ifnum;
1056 ss->pusb_dev = dev;
1057 ss->attention_done = 0;
1058
1059 /* If the device has subclass and protocol, then use that. Otherwise,
1060 * take data from the specific interface.
1061 */
1062 if (subclass) {
1063 ss->subclass = subclass;
1064 ss->protocol = protocol;
1065 } else {
1066 ss->subclass = iface->bInterfaceSubClass;
1067 ss->protocol = iface->bInterfaceProtocol;
1068 }
1069
1070 /* set the handler pointers based on the protocol */
1071 USB_STOR_PRINTF("Transport: ");
1072 switch (ss->protocol) {
1073 case US_PR_CB:
1074 USB_STOR_PRINTF("Control/Bulk\n");
1075 ss->transport = usb_stor_CB_transport;
1076 ss->transport_reset = usb_stor_CB_reset;
1077 break;
1078
1079 case US_PR_CBI:
1080 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1081 ss->transport = usb_stor_CB_transport;
1082 ss->transport_reset = usb_stor_CB_reset;
1083 break;
wdenk149dded2003-09-10 18:20:28 +00001084 case US_PR_BULK:
1085 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1086 ss->transport = usb_stor_BBB_transport;
1087 ss->transport_reset = usb_stor_BBB_reset;
1088 break;
wdenkaffae2b2002-08-17 09:36:01 +00001089 default:
wdenk80885a92004-02-26 23:46:20 +00001090 printf("USB Storage Transport unknown / not yet implemented\n");
wdenkaffae2b2002-08-17 09:36:01 +00001091 return 0;
1092 break;
1093 }
1094
1095 /*
1096 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1097 * An optional interrupt is OK (necessary for CBI protocol).
1098 * We will ignore any others.
1099 */
1100 for (i = 0; i < iface->bNumEndpoints; i++) {
1101 /* is it an BULK endpoint? */
1102 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1103 == USB_ENDPOINT_XFER_BULK) {
1104 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1105 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1106 USB_ENDPOINT_NUMBER_MASK;
1107 else
1108 ss->ep_out = iface->ep_desc[i].bEndpointAddress &
1109 USB_ENDPOINT_NUMBER_MASK;
1110 }
1111
1112 /* is it an interrupt endpoint? */
1113 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1114 == USB_ENDPOINT_XFER_INT) {
1115 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1116 USB_ENDPOINT_NUMBER_MASK;
1117 ss->irqinterval = iface->ep_desc[i].bInterval;
1118 }
1119 }
1120 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1121 ss->ep_in, ss->ep_out, ss->ep_int);
1122
1123 /* Do some basic sanity checks, and bail if we find a problem */
1124 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
1125 !ss->ep_in || !ss->ep_out ||
1126 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1127 USB_STOR_PRINTF("Problems with device\n");
1128 return 0;
1129 }
1130 /* set class specific stuff */
wdenk149dded2003-09-10 18:20:28 +00001131 /* We only handle certain protocols. Currently, these are
1132 * the only ones.
wdenk80885a92004-02-26 23:46:20 +00001133 * The SFF8070 accepts the requests used in u-boot
wdenkaffae2b2002-08-17 09:36:01 +00001134 */
wdenk80885a92004-02-26 23:46:20 +00001135 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1136 ss->subclass != US_SC_8070) {
wdenkaffae2b2002-08-17 09:36:01 +00001137 printf("Sorry, protocol %d not yet supported.\n",ss->subclass);
1138 return 0;
1139 }
wdenk149dded2003-09-10 18:20:28 +00001140 if(ss->ep_int) { /* we had found an interrupt endpoint, prepare irq pipe */
wdenkaffae2b2002-08-17 09:36:01 +00001141 /* set up the IRQ pipe and handler */
1142
1143 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1144 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1145 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1146 dev->irq_handle=usb_stor_irq;
wdenkaffae2b2002-08-17 09:36:01 +00001147 }
wdenka43278a2003-09-11 19:48:06 +00001148 dev->privptr=(void *)ss;
wdenkaffae2b2002-08-17 09:36:01 +00001149 return 1;
1150}
1151
1152int usb_stor_get_info(struct usb_device *dev,struct us_data *ss,block_dev_desc_t *dev_desc)
1153{
1154 unsigned char perq,modi;
1155 unsigned long cap[2];
1156 unsigned long *capacity,*blksz;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001157 ccb *pccb = &usb_ccb;
wdenkaffae2b2002-08-17 09:36:01 +00001158
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001159 /* for some reasons a couple of devices would not survive this reset */
1160 if (
1161 /* Sony USM256E */
1162 (dev->descriptor.idVendor == 0x054c &&
1163 dev->descriptor.idProduct == 0x019e)
1164
1165 ||
1166 /* USB007 Mini-USB2 Flash Drive */
1167 (dev->descriptor.idVendor == 0x066f &&
1168 dev->descriptor.idProduct == 0x2010)
Bartlomiej Siekaf88a0ae2006-07-13 15:32:16 +02001169 ||
1170 /* SanDisk Corporation Cruzer Micro 20044318410546613953 */
1171 (dev->descriptor.idVendor == 0x0781 &&
1172 dev->descriptor.idProduct == 0x5151)
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001173 )
1174 USB_STOR_PRINTF("usb_stor_get_info: skipping RESET..\n");
Wolfgang Denk095b8a32005-08-02 17:06:17 +02001175 else
wdenk2729af92004-05-03 20:45:30 +00001176 ss->transport_reset(ss);
wdenkaffae2b2002-08-17 09:36:01 +00001177
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001178 pccb->pdata = usb_stor_buf;
1179
1180 dev_desc->target = dev->devnum;
1181 pccb->lun = dev_desc->lun;
wdenkaffae2b2002-08-17 09:36:01 +00001182 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1183
1184 if(usb_inquiry(pccb,ss))
1185 return -1;
Wolfgang Denk095b8a32005-08-02 17:06:17 +02001186
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001187 perq = usb_stor_buf[0];
1188 modi = usb_stor_buf[1];
1189 if((perq & 0x1f) == 0x1f) {
wdenkaffae2b2002-08-17 09:36:01 +00001190 return 0; /* skip unknown devices */
1191 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001192 if((modi&0x80) == 0x80) {/* drive is removable */
1193 dev_desc->removable = 1;
wdenkaffae2b2002-08-17 09:36:01 +00001194 }
1195 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1196 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1197 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001198 dev_desc->vendor[8] = 0;
1199 dev_desc->product[16] = 0;
1200 dev_desc->revision[4] = 0;
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +02001201#ifdef CONFIG_USB_BIN_FIXUP
Wolfgang Denk409ecdc2007-11-18 16:36:27 +01001202 usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor, (uchar *)dev_desc->product);
Bartlomiej Siekaddde6b72006-08-22 10:38:18 +02001203#endif /* CONFIG_USB_BIN_FIXUP */
wdenkaffae2b2002-08-17 09:36:01 +00001204 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf[2],usb_stor_buf[3]);
1205 if(usb_test_unit_ready(pccb,ss)) {
1206 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 +02001207 if(dev_desc->removable == 1) {
1208 dev_desc->type = perq;
wdenkaffae2b2002-08-17 09:36:01 +00001209 return 1;
1210 }
1211 else
1212 return 0;
1213 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001214 pccb->pdata = (unsigned char *)&cap[0];
wdenkaffae2b2002-08-17 09:36:01 +00001215 memset(pccb->pdata,0,8);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001216 if(usb_read_capacity(pccb,ss) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +00001217 printf("READ_CAP ERROR\n");
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001218 cap[0] = 2880;
1219 cap[1] = 0x200;
wdenkaffae2b2002-08-17 09:36:01 +00001220 }
1221 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap[0],cap[1]);
1222#if 0
1223 if(cap[0]>(0x200000 * 10)) /* greater than 10 GByte */
1224 cap[0]>>=16;
1225#endif
Christian Eggersc9182612008-05-21 22:12:00 +02001226 cap[0] = cpu_to_be32(cap[0]);
1227 cap[1] = cpu_to_be32(cap[1]);
1228
wdenk149dded2003-09-10 18:20:28 +00001229 /* this assumes bigendian! */
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001230 cap[0] += 1;
1231 capacity = &cap[0];
1232 blksz = &cap[1];
wdenkaffae2b2002-08-17 09:36:01 +00001233 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity,*blksz);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001234 dev_desc->lba = *capacity;
1235 dev_desc->blksz = *blksz;
1236 dev_desc->type = perq;
wdenkaffae2b2002-08-17 09:36:01 +00001237 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1238 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1239
1240 init_part(dev_desc);
1241
1242 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1243 return 1;
1244}
1245
wdenkaffae2b2002-08-17 09:36:01 +00001246#endif /* CONFIG_USB_STORAGE */
Jon Loeliger90253172007-07-10 11:02:44 -05001247#endif