blob: 6cf62e40dd63c150764328f2c2467c4068fab939 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
wdenk149dded2003-09-10 18:20:28 +00005 * For BBB support (C) Copyright 2003
6 * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
7 *
wdenkaffae2b2002-08-17 09:36:01 +00008 * Most of this source has been derived from the Linux USB
wdenk149dded2003-09-10 18:20:28 +00009 * project. BBB support based on /sys/dev/usb/umass.c from
10 * FreeBSD.
wdenkaffae2b2002-08-17 09:36:01 +000011 *
12 * See file CREDITS for list of people who contributed to this
13 * project.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wdenk80885a92004-02-26 23:46:20 +000022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkaffae2b2002-08-17 09:36:01 +000023 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * MA 02111-1307 USA
29 *
30 */
31
32/* Note:
33 * Currently only the CBI transport protocoll has been implemented, and it
34 * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
35 * transport protocoll may work as well.
36 */
wdenk149dded2003-09-10 18:20:28 +000037/*
38 * New Note:
39 * Support for USB Mass Storage Devices (BBB) has been added. It has
40 * only been tested with USB memory sticks.
41 * Nota bene: if you are using the BBB support with a little-endian
42 * CPU then you MUST define LITTLEENDIAN in the configuration file!
43 */
wdenkaffae2b2002-08-17 09:36:01 +000044
45
wdenkaffae2b2002-08-17 09:36:01 +000046#include <common.h>
47#include <command.h>
48#include <asm/processor.h>
49
50
51#if (CONFIG_COMMANDS & CFG_CMD_USB)
52#include <usb.h>
53
54#ifdef CONFIG_USB_STORAGE
55
wdenk80885a92004-02-26 23:46:20 +000056#undef USB_STOR_DEBUG
57#undef BBB_COMDAT_TRACE
58#undef BBB_XPORT_TRACE
wdenkaffae2b2002-08-17 09:36:01 +000059
60#ifdef USB_STOR_DEBUG
wdenk80885a92004-02-26 23:46:20 +000061#define USB_STOR_PRINTF(fmt,args...) printf (fmt ,##args)
wdenkaffae2b2002-08-17 09:36:01 +000062#else
63#define USB_STOR_PRINTF(fmt,args...)
64#endif
65
66#include <scsi.h>
67/* direction table -- this indicates the direction of the data
68 * transfer for each command code -- a 1 indicates input
69 */
70unsigned char us_direction[256/8] = {
71 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
72 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
75};
76#define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
77
78static unsigned char usb_stor_buf[512];
79static ccb usb_ccb;
80
81/*
82 * CBI style
83 */
84
85#define US_CBI_ADSC 0
86
wdenk149dded2003-09-10 18:20:28 +000087/*
88 * BULK only
89 */
90#define US_BBB_RESET 0xff
91#define US_BBB_GET_MAX_LUN 0xfe
92
93/* Command Block Wrapper */
94typedef struct {
95 __u32 dCBWSignature;
96# define CBWSIGNATURE 0x43425355
97 __u32 dCBWTag;
98 __u32 dCBWDataTransferLength;
99 __u8 bCBWFlags;
100# define CBWFLAGS_OUT 0x00
101# define CBWFLAGS_IN 0x80
102 __u8 bCBWLUN;
103 __u8 bCDBLength;
104# define CBWCDBLENGTH 16
105 __u8 CBWCDB[CBWCDBLENGTH];
106} umass_bbb_cbw_t;
wdenk80885a92004-02-26 23:46:20 +0000107#define UMASS_BBB_CBW_SIZE 31
wdenk149dded2003-09-10 18:20:28 +0000108static __u32 CBWTag = 0;
109
110/* Command Status Wrapper */
111typedef struct {
112 __u32 dCSWSignature;
113# define CSWSIGNATURE 0x53425355
114 __u32 dCSWTag;
115 __u32 dCSWDataResidue;
116 __u8 bCSWStatus;
117# define CSWSTATUS_GOOD 0x0
wdenk80885a92004-02-26 23:46:20 +0000118# define CSWSTATUS_FAILED 0x1
wdenk149dded2003-09-10 18:20:28 +0000119# define CSWSTATUS_PHASE 0x2
120} umass_bbb_csw_t;
wdenk80885a92004-02-26 23:46:20 +0000121#define UMASS_BBB_CSW_SIZE 13
wdenkaffae2b2002-08-17 09:36:01 +0000122
123#define USB_MAX_STOR_DEV 5
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200124static int usb_max_devs = 0; /* number of highest available usb device */
wdenkaffae2b2002-08-17 09:36:01 +0000125
126static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
127
128struct us_data;
129typedef int (*trans_cmnd)(ccb*, struct us_data*);
130typedef int (*trans_reset)(struct us_data*);
131
132struct us_data {
wdenk80885a92004-02-26 23:46:20 +0000133 struct usb_device *pusb_dev; /* this usb_device */
wdenkaffae2b2002-08-17 09:36:01 +0000134 unsigned int flags; /* from filter initially */
135 unsigned char ifnum; /* interface number */
136 unsigned char ep_in; /* in endpoint */
137 unsigned char ep_out; /* out ....... */
138 unsigned char ep_int; /* interrupt . */
139 unsigned char subclass; /* as in overview */
140 unsigned char protocol; /* .............. */
wdenk80885a92004-02-26 23:46:20 +0000141 unsigned char attention_done; /* force attn on first cmd */
wdenkaffae2b2002-08-17 09:36:01 +0000142 unsigned short ip_data; /* interrupt data */
143 int action; /* what to do */
144 int ip_wanted; /* needed */
145 int *irq_handle; /* for USB int requests */
146 unsigned int irqpipe; /* pipe for release_irq */
147 unsigned char irqmaxp; /* max packed for irq Pipe */
wdenk80885a92004-02-26 23:46:20 +0000148 unsigned char irqinterval; /* Intervall for IRQ Pipe */
wdenkaffae2b2002-08-17 09:36:01 +0000149 ccb *srb; /* current srb */
150 trans_reset transport_reset; /* reset routine */
151 trans_cmnd transport; /* transport routine */
152};
153
154static struct us_data usb_stor[USB_MAX_STOR_DEV];
155
156
wdenk80885a92004-02-26 23:46:20 +0000157#define USB_STOR_TRANSPORT_GOOD 0
wdenkaffae2b2002-08-17 09:36:01 +0000158#define USB_STOR_TRANSPORT_FAILED -1
159#define USB_STOR_TRANSPORT_ERROR -2
160
161
wdenkaffae2b2002-08-17 09:36:01 +0000162int usb_stor_get_info(struct usb_device *dev, struct us_data *us, block_dev_desc_t *dev_desc);
163int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss);
164unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer);
165struct usb_device * usb_get_dev_index(int index);
166void uhci_show_temp_int_td(void);
167
168block_dev_desc_t *usb_stor_get_dev(int index)
169{
170 return &usb_dev_desc[index];
171}
172
173
174void usb_show_progress(void)
175{
176 printf(".");
177}
178
179/*********************************************************************************
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200180 * show info on storage devices; 'usb start/init' must be invoked earlier
181 * as we only retrieve structures populated during devices initialization
182 */
183void usb_stor_info(void)
184{
185 int i;
186
187 if (usb_max_devs > 0)
188 for (i = 0; i < usb_max_devs; i++) {
189 printf (" Device %d: ", i);
190 dev_print(&usb_dev_desc[i]);
191 }
192 else
193 printf("No storage devices, perhaps not 'usb start'ed..?\n");
194}
195
196/*********************************************************************************
197 * scan the usb and reports device info
wdenkaffae2b2002-08-17 09:36:01 +0000198 * to the user if mode = 1
199 * returns current device or -1 if no
200 */
201int usb_stor_scan(int mode)
202{
203 unsigned char i;
204 struct usb_device *dev;
205
wdenk149dded2003-09-10 18:20:28 +0000206 /* GJ */
207 memset(usb_stor_buf, 0, sizeof(usb_stor_buf));
208
wdenkaffae2b2002-08-17 09:36:01 +0000209 if(mode==1) {
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200210 printf(" scanning bus for storage devices... ");
wdenkaffae2b2002-08-17 09:36:01 +0000211 }
212 usb_disable_asynch(1); /* asynch transfer not allowed */
213
214 for(i=0;i<USB_MAX_STOR_DEV;i++) {
215 memset(&usb_dev_desc[i],0,sizeof(block_dev_desc_t));
216 usb_dev_desc[i].target=0xff;
217 usb_dev_desc[i].if_type=IF_TYPE_USB;
218 usb_dev_desc[i].dev=i;
219 usb_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
220 usb_dev_desc[i].block_read=usb_stor_read;
221 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200222
wdenkaffae2b2002-08-17 09:36:01 +0000223 usb_max_devs=0;
224 for(i=0;i<USB_MAX_DEVICE;i++) {
225 dev=usb_get_dev_index(i); /* get device */
226 USB_STOR_PRINTF("i=%d\n",i);
227 if(dev==NULL) {
228 break; /* no more devices avaiable */
229 }
230 if(usb_storage_probe(dev,0,&usb_stor[usb_max_devs])) { /* ok, it is a storage devices */
231 /* get info and fill it in */
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200232 if(usb_stor_get_info(dev, &usb_stor[usb_max_devs], &usb_dev_desc[usb_max_devs]))
wdenkaffae2b2002-08-17 09:36:01 +0000233 usb_max_devs++;
wdenkaffae2b2002-08-17 09:36:01 +0000234 } /* if storage device */
235 if(usb_max_devs==USB_MAX_STOR_DEV) {
236 printf("max USB Storage Device reached: %d stopping\n",usb_max_devs);
237 break;
238 }
239 } /* for */
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200240
wdenkaffae2b2002-08-17 09:36:01 +0000241 usb_disable_asynch(0); /* asynch transfer allowed */
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200242 printf("%d Storage Device(s) found\n", usb_max_devs);
wdenkaffae2b2002-08-17 09:36:01 +0000243 if(usb_max_devs>0)
244 return 0;
245 else
246 return-1;
247}
248
249static int usb_stor_irq(struct usb_device *dev)
250{
251 struct us_data *us;
252 us=(struct us_data *)dev->privptr;
253
254 if(us->ip_wanted) {
255 us->ip_wanted=0;
256 }
257 return 0;
258}
259
260
261#ifdef USB_STOR_DEBUG
262
263static void usb_show_srb(ccb * pccb)
264{
265 int i;
266 printf("SRB: len %d datalen 0x%lX\n ",pccb->cmdlen,pccb->datalen);
267 for(i=0;i<12;i++) {
268 printf("%02X ",pccb->cmd[i]);
269 }
270 printf("\n");
271}
272
273static void display_int_status(unsigned long tmp)
274{
275 printf("Status: %s %s %s %s %s %s %s\n",
276 (tmp & USB_ST_ACTIVE) ? "Active" : "",
277 (tmp & USB_ST_STALLED) ? "Stalled" : "",
278 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
279 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
280 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
281 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
282 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
283}
284#endif
285/***********************************************************************
286 * Data transfer routines
287 ***********************************************************************/
288
289static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
290{
291 int max_size;
292 int this_xfer;
293 int result;
294 int partial;
295 int maxtry;
296 int stat;
297
298 /* determine the maximum packet size for these transfers */
299 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
300
301 /* while we have data left to transfer */
302 while (length) {
303
304 /* calculate how long this will be -- maximum or a remainder */
305 this_xfer = length > max_size ? max_size : length;
306 length -= this_xfer;
307
308 /* setup the retry counter */
309 maxtry = 10;
310
311 /* set up the transfer loop */
312 do {
313 /* transfer the data */
314 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
315 (unsigned int)buf, this_xfer, 11 - maxtry);
316 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
317 this_xfer, &partial, USB_CNTL_TIMEOUT*5);
318 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
319 result, partial, this_xfer);
320 if(us->pusb_dev->status!=0) {
321 /* if we stall, we need to clear it before we go on */
322#ifdef USB_STOR_DEBUG
323 display_int_status(us->pusb_dev->status);
324#endif
325 if (us->pusb_dev->status & USB_ST_STALLED) {
326 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
327 stat = us->pusb_dev->status;
328 usb_clear_halt(us->pusb_dev, pipe);
329 us->pusb_dev->status=stat;
330 if(this_xfer == partial) {
331 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us->pusb_dev->status);
332 return 0;
333 }
334 else
335 return result;
336 }
337 if (us->pusb_dev->status & USB_ST_NAK_REC) {
338 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
339 return result;
340 }
341 if(this_xfer == partial) {
342 USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us->pusb_dev->status);
343 return 0;
344 }
345 /* if our try counter reaches 0, bail out */
346 USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us->pusb_dev->status,partial);
347 if (!maxtry--)
348 return result;
349 }
350 /* update to show what data was transferred */
351 this_xfer -= partial;
352 buf += partial;
353 /* continue until this transfer is done */
354 } while ( this_xfer );
355 }
356
357 /* if we get here, we're done and successful */
358 return 0;
359}
360
wdenk149dded2003-09-10 18:20:28 +0000361static int usb_stor_BBB_reset(struct us_data *us)
362{
363 int result;
364 unsigned int pipe;
365
366 /*
367 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
368 *
369 * For Reset Recovery the host shall issue in the following order:
370 * a) a Bulk-Only Mass Storage Reset
371 * b) a Clear Feature HALT to the Bulk-In endpoint
372 * c) a Clear Feature HALT to the Bulk-Out endpoint
373 *
374 * This is done in 3 steps.
375 *
376 * If the reset doesn't succeed, the device should be port reset.
377 *
378 * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
379 */
380 USB_STOR_PRINTF("BBB_reset\n");
381 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
382 US_BBB_RESET, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
383 0, us->ifnum, 0, 0, USB_CNTL_TIMEOUT*5);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200384
wdenk149dded2003-09-10 18:20:28 +0000385 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED))
386 {
387 USB_STOR_PRINTF("RESET:stall\n");
388 return -1;
389 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200390
wdenk149dded2003-09-10 18:20:28 +0000391 /* long wait for reset */
392 wait_ms(150);
393 USB_STOR_PRINTF("BBB_reset result %d: status %X reset\n",result,us->pusb_dev->status);
394 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
395 result = usb_clear_halt(us->pusb_dev, pipe);
396 /* long wait for reset */
397 wait_ms(150);
398 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing IN endpoint\n",result,us->pusb_dev->status);
399 /* long wait for reset */
400 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
401 result = usb_clear_halt(us->pusb_dev, pipe);
402 wait_ms(150);
403 USB_STOR_PRINTF("BBB_reset result %d: status %X clearing OUT endpoint\n",result,us->pusb_dev->status);
404 USB_STOR_PRINTF("BBB_reset done\n");
405 return 0;
406}
407
wdenkaffae2b2002-08-17 09:36:01 +0000408/* FIXME: this reset function doesn't really reset the port, and it
409 * should. Actually it should probably do what it's doing here, and
410 * reset the port physically
411 */
412static int usb_stor_CB_reset(struct us_data *us)
413{
414 unsigned char cmd[12];
415 int result;
416
417 USB_STOR_PRINTF("CB_reset\n");
418 memset(cmd, 0xFF, sizeof(cmd));
419 cmd[0] = SCSI_SEND_DIAG;
420 cmd[1] = 4;
421 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
422 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
423 0, us->ifnum, cmd, sizeof(cmd), USB_CNTL_TIMEOUT*5);
424
425 /* long wait for reset */
426 wait_ms(1500);
427 USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result,us->pusb_dev->status);
428 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
429 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
430
431 USB_STOR_PRINTF("CB_reset done\n");
432 return 0;
433}
434
wdenk149dded2003-09-10 18:20:28 +0000435/*
436 * Set up the command for a BBB device. Note that the actual SCSI
437 * command is copied into cbw.CBWCDB.
438 */
439int usb_stor_BBB_comdat(ccb *srb, struct us_data *us)
440{
441 int result;
442 int actlen;
443 int dir_in;
444 unsigned int pipe;
445 umass_bbb_cbw_t cbw;
446
447 dir_in = US_DIRECTION(srb->cmd[0]);
448
449#ifdef BBB_COMDAT_TRACE
450 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);
451 if (srb->cmdlen) {
452 for(result = 0;result < srb->cmdlen;result++)
453 printf("cmd[%d] %#x ", result, srb->cmd[result]);
454 printf("\n");
455 }
456#endif
457 /* sanity checks */
458 if (!(srb->cmdlen <= CBWCDBLENGTH)) {
459 USB_STOR_PRINTF("usb_stor_BBB_comdat:cmdlen too large\n");
460 return -1;
461 }
462
463 /* always OUT to the ep */
464 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
465
466 cbw.dCBWSignature = swap_32(CBWSIGNATURE);
467 cbw.dCBWTag = swap_32(CBWTag++);
468 cbw.dCBWDataTransferLength = swap_32(srb->datalen);
469 cbw.bCBWFlags = (dir_in? CBWFLAGS_IN : CBWFLAGS_OUT);
470 cbw.bCBWLUN = srb->lun;
471 cbw.bCDBLength = srb->cmdlen;
472 /* copy the command data into the CBW command data buffer */
473 /* DST SRC LEN!!! */
474 memcpy(cbw.CBWCDB, srb->cmd, srb->cmdlen);
475 result = usb_bulk_msg(us->pusb_dev, pipe, &cbw, UMASS_BBB_CBW_SIZE, &actlen, USB_CNTL_TIMEOUT*5);
476 if (result < 0)
477 USB_STOR_PRINTF("usb_stor_BBB_comdat:usb_bulk_msg error\n");
478 return result;
479}
480
wdenkaffae2b2002-08-17 09:36:01 +0000481/* FIXME: we also need a CBI_command which sets up the completion
482 * interrupt, and waits for it
483 */
484int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
485{
486 int result;
487 int dir_in,retry;
488 unsigned int pipe;
489 unsigned long status;
490
491 retry=5;
492 dir_in=US_DIRECTION(srb->cmd[0]);
493
494 if(dir_in)
495 pipe=usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
496 else
497 pipe=usb_sndbulkpipe(us->pusb_dev, us->ep_out);
498 while(retry--) {
499 USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry);
500#ifdef USB_STOR_DEBUG
501 usb_show_srb(srb);
502#endif
503 /* let's send the command via the control pipe */
504 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
505 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
506 0, us->ifnum,
507 srb->cmd, srb->cmdlen, USB_CNTL_TIMEOUT*5);
508 USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result,us->pusb_dev->status);
509 /* check the return code for the command */
510 if (result < 0) {
511 if(us->pusb_dev->status & USB_ST_STALLED) {
512 status=us->pusb_dev->status;
513 USB_STOR_PRINTF(" stall during command found, clear pipe\n");
514 usb_clear_halt(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0));
515 us->pusb_dev->status=status;
516 }
517 USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb->cmd[0],us->pusb_dev->status);
518 return result;
519 }
520 /* transfer the data payload for this command, if one exists*/
521
522 USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result,dir_in ? "IN" : "OUT",srb->datalen);
523 if (srb->datalen) {
524 result = us_one_transfer(us, pipe, srb->pdata,srb->datalen);
525 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);
526 if(!(us->pusb_dev->status & USB_ST_NAK_REC))
527 break;
528 } /* if (srb->datalen) */
529 else
530 break;
531 }
532 /* return result */
533
534 return result;
535}
536
537
wdenk80885a92004-02-26 23:46:20 +0000538int usb_stor_CBI_get_status (ccb * srb, struct us_data *us)
wdenkaffae2b2002-08-17 09:36:01 +0000539{
540 int timeout;
541
wdenk80885a92004-02-26 23:46:20 +0000542 us->ip_wanted = 1;
543 submit_int_msg (us->pusb_dev, us->irqpipe,
544 (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
545 timeout = 1000;
546 while (timeout--) {
547 if ((volatile int *) us->ip_wanted == 0)
wdenkaffae2b2002-08-17 09:36:01 +0000548 break;
wdenk80885a92004-02-26 23:46:20 +0000549 wait_ms (10);
wdenkaffae2b2002-08-17 09:36:01 +0000550 }
551 if (us->ip_wanted) {
wdenk80885a92004-02-26 23:46:20 +0000552 printf (" Did not get interrupt on CBI\n");
wdenkaffae2b2002-08-17 09:36:01 +0000553 us->ip_wanted = 0;
554 return USB_STOR_TRANSPORT_ERROR;
555 }
wdenk80885a92004-02-26 23:46:20 +0000556 USB_STOR_PRINTF
557 ("Got interrupt data 0x%x, transfered %d status 0x%lX\n",
558 us->ip_data, us->pusb_dev->irq_act_len,
559 us->pusb_dev->irq_status);
wdenkaffae2b2002-08-17 09:36:01 +0000560 /* UFI gives us ASC and ASCQ, like a request sense */
561 if (us->subclass == US_SC_UFI) {
562 if (srb->cmd[0] == SCSI_REQ_SENSE ||
563 srb->cmd[0] == SCSI_INQUIRY)
564 return USB_STOR_TRANSPORT_GOOD; /* Good */
wdenk80885a92004-02-26 23:46:20 +0000565 else if (us->ip_data)
566 return USB_STOR_TRANSPORT_FAILED;
wdenkaffae2b2002-08-17 09:36:01 +0000567 else
wdenk80885a92004-02-26 23:46:20 +0000568 return USB_STOR_TRANSPORT_GOOD;
wdenkaffae2b2002-08-17 09:36:01 +0000569 }
570 /* otherwise, we interpret the data normally */
571 switch (us->ip_data) {
wdenk80885a92004-02-26 23:46:20 +0000572 case 0x0001:
573 return USB_STOR_TRANSPORT_GOOD;
574 case 0x0002:
575 return USB_STOR_TRANSPORT_FAILED;
576 default:
577 return USB_STOR_TRANSPORT_ERROR;
578 } /* switch */
wdenkaffae2b2002-08-17 09:36:01 +0000579 return USB_STOR_TRANSPORT_ERROR;
580}
581
582#define USB_TRANSPORT_UNKNOWN_RETRY 5
583#define USB_TRANSPORT_NOT_READY_RETRY 10
584
wdenk149dded2003-09-10 18:20:28 +0000585/* clear a stall on an endpoint - special for BBB devices */
586int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
587{
588 int result;
589
590 /* ENDPOINT_HALT = 0, so set value to 0 */
591 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
592 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
593 0, endpt, 0, 0, USB_CNTL_TIMEOUT*5);
594 return result;
595}
596
597int usb_stor_BBB_transport(ccb *srb, struct us_data *us)
598{
599 int result, retry;
600 int dir_in;
601 int actlen, data_actlen;
602 unsigned int pipe, pipein, pipeout;
603 umass_bbb_csw_t csw;
604#ifdef BBB_XPORT_TRACE
605 unsigned char *ptr;
606 int index;
607#endif
608
609 dir_in = US_DIRECTION(srb->cmd[0]);
610
611 /* COMMAND phase */
612 USB_STOR_PRINTF("COMMAND phase\n");
613 result = usb_stor_BBB_comdat(srb, us);
614 if (result < 0) {
615 USB_STOR_PRINTF("failed to send CBW status %ld\n",
616 us->pusb_dev->status);
617 usb_stor_BBB_reset(us);
618 return USB_STOR_TRANSPORT_FAILED;
619 }
620 wait_ms(5);
621 pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
622 pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
623 /* DATA phase + error handling */
wdenk149dded2003-09-10 18:20:28 +0000624 data_actlen = 0;
625 /* no data, go immediately to the STATUS phase */
626 if (srb->datalen == 0)
627 goto st;
wdenk80885a92004-02-26 23:46:20 +0000628 USB_STOR_PRINTF("DATA phase\n");
wdenk149dded2003-09-10 18:20:28 +0000629 if (dir_in)
630 pipe = pipein;
631 else
632 pipe = pipeout;
633 result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen, &data_actlen, USB_CNTL_TIMEOUT*5);
634 /* special handling of STALL in DATA phase */
635 if((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
wdenka43278a2003-09-11 19:48:06 +0000636 USB_STOR_PRINTF("DATA:stall\n");
wdenk149dded2003-09-10 18:20:28 +0000637 /* clear the STALL on the endpoint */
638 result = usb_stor_BBB_clear_endpt_stall(us, dir_in? us->ep_in : us->ep_out);
639 if (result >= 0)
640 /* continue on to STATUS phase */
641 goto st;
642 }
643 if (result < 0) {
644 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
645 us->pusb_dev->status);
646 usb_stor_BBB_reset(us);
647 return USB_STOR_TRANSPORT_FAILED;
648 }
649#ifdef BBB_XPORT_TRACE
650 for (index = 0; index < data_actlen; index++)
651 printf("pdata[%d] %#x ", index, srb->pdata[index]);
652 printf("\n");
653#endif
654 /* STATUS phase + error handling */
655 st:
656 retry = 0;
657 again:
658 USB_STOR_PRINTF("STATUS phase\n");
Wolfgang Denk095b8a32005-08-02 17:06:17 +0200659 result = usb_bulk_msg(us->pusb_dev, pipein, &csw, UMASS_BBB_CSW_SIZE,
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200660 &actlen, USB_CNTL_TIMEOUT*5);
661
wdenk149dded2003-09-10 18:20:28 +0000662 /* special handling of STALL in STATUS phase */
663 if((result < 0) && (retry < 1) && (us->pusb_dev->status & USB_ST_STALLED)) {
664 USB_STOR_PRINTF("STATUS:stall\n");
665 /* clear the STALL on the endpoint */
666 result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
667 if (result >= 0 && (retry++ < 1))
668 /* do a retry */
669 goto again;
670 }
671 if (result < 0) {
672 USB_STOR_PRINTF("usb_bulk_msg error status %ld\n",
673 us->pusb_dev->status);
674 usb_stor_BBB_reset(us);
675 return USB_STOR_TRANSPORT_FAILED;
676 }
677#ifdef BBB_XPORT_TRACE
678 ptr = (unsigned char *)&csw;
679 for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
680 printf("ptr[%d] %#x ", index, ptr[index]);
681 printf("\n");
682#endif
683 /* misuse pipe to get the residue */
684 pipe = swap_32(csw.dCSWDataResidue);
685 if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
686 pipe = srb->datalen - data_actlen;
687 if (CSWSIGNATURE != swap_32(csw.dCSWSignature)) {
688 USB_STOR_PRINTF("!CSWSIGNATURE\n");
689 usb_stor_BBB_reset(us);
690 return USB_STOR_TRANSPORT_FAILED;
691 } else if ((CBWTag - 1) != swap_32(csw.dCSWTag)) {
692 USB_STOR_PRINTF("!Tag\n");
693 usb_stor_BBB_reset(us);
694 return USB_STOR_TRANSPORT_FAILED;
695 } else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
696 USB_STOR_PRINTF(">PHASE\n");
697 usb_stor_BBB_reset(us);
698 return USB_STOR_TRANSPORT_FAILED;
699 } else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
700 USB_STOR_PRINTF("=PHASE\n");
701 usb_stor_BBB_reset(us);
702 return USB_STOR_TRANSPORT_FAILED;
703 } else if (data_actlen > srb->datalen) {
704 USB_STOR_PRINTF("transferred %dB instead of %dB\n",
705 data_actlen, srb->datalen);
706 return USB_STOR_TRANSPORT_FAILED;
707 } else if (csw.bCSWStatus == CSWSTATUS_FAILED) {
708 USB_STOR_PRINTF("FAILED\n");
709 return USB_STOR_TRANSPORT_FAILED;
710 }
711
712 return result;
713}
714
wdenkaffae2b2002-08-17 09:36:01 +0000715int usb_stor_CB_transport(ccb *srb, struct us_data *us)
716{
717 int result,status;
718 ccb *psrb;
719 ccb reqsrb;
720 int retry,notready;
721
722 psrb=&reqsrb;
723 status=USB_STOR_TRANSPORT_GOOD;
724 retry=0;
725 notready=0;
726 /* issue the command */
727do_retry:
728 result=usb_stor_CB_comdat(srb,us);
729 USB_STOR_PRINTF("command / Data returned %d, status %X\n",result,us->pusb_dev->status);
730 /* if this is an CBI Protocol, get IRQ */
731 if(us->protocol==US_PR_CBI) {
732 status=usb_stor_CBI_get_status(srb,us);
733 /* if the status is error, report it */
734 if(status==USB_STOR_TRANSPORT_ERROR) {
735 USB_STOR_PRINTF(" USB CBI Command Error\n");
736 return status;
737 }
738 srb->sense_buf[12]=(unsigned char)(us->ip_data>>8);
739 srb->sense_buf[13]=(unsigned char)(us->ip_data&0xff);
740 if(!us->ip_data) {
741 /* if the status is good, report it */
742 if(status==USB_STOR_TRANSPORT_GOOD) {
743 USB_STOR_PRINTF(" USB CBI Command Good\n");
744 return status;
745 }
746 }
747 }
748 /* do we have to issue an auto request? */
749 /* HERE we have to check the result */
750 if((result<0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
751 USB_STOR_PRINTF("ERROR %X\n",us->pusb_dev->status);
752 us->transport_reset(us);
753 return USB_STOR_TRANSPORT_ERROR;
754 }
755 if((us->protocol==US_PR_CBI) &&
756 ((srb->cmd[0]==SCSI_REQ_SENSE) ||
wdenk80885a92004-02-26 23:46:20 +0000757 (srb->cmd[0]==SCSI_INQUIRY))) { /* do not issue an autorequest after request sense */
wdenkaffae2b2002-08-17 09:36:01 +0000758 USB_STOR_PRINTF("No auto request and good\n");
759 return USB_STOR_TRANSPORT_GOOD;
760 }
761 /* issue an request_sense */
762 memset(&psrb->cmd[0],0,12);
763 psrb->cmd[0]=SCSI_REQ_SENSE;
764 psrb->cmd[1]=srb->lun<<5;
765 psrb->cmd[4]=18;
766 psrb->datalen=18;
767 psrb->pdata=&srb->sense_buf[0];
768 psrb->cmdlen=12;
769 /* issue the command */
770 result=usb_stor_CB_comdat(psrb,us);
771 USB_STOR_PRINTF("auto request returned %d\n",result);
772 /* if this is an CBI Protocol, get IRQ */
773 if(us->protocol==US_PR_CBI) {
wdenk80885a92004-02-26 23:46:20 +0000774 status=usb_stor_CBI_get_status(psrb,us);
wdenkaffae2b2002-08-17 09:36:01 +0000775 }
776 if((result<0)&&!(us->pusb_dev->status & USB_ST_STALLED)) {
777 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us->pusb_dev->status);
778 return USB_STOR_TRANSPORT_ERROR;
779 }
780 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]);
781 /* Check the auto request result */
782 if((srb->sense_buf[2]==0) &&
783 (srb->sense_buf[12]==0) &&
784 (srb->sense_buf[13]==0)) /* ok, no sense */
785 return USB_STOR_TRANSPORT_GOOD;
786 /* Check the auto request result */
787 switch(srb->sense_buf[2]) {
wdenk149dded2003-09-10 18:20:28 +0000788 case 0x01: /* Recovered Error */
789 return USB_STOR_TRANSPORT_GOOD;
wdenk80885a92004-02-26 23:46:20 +0000790 break;
wdenk149dded2003-09-10 18:20:28 +0000791 case 0x02: /* Not Ready */
792 if(notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
793 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
794 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
795 return USB_STOR_TRANSPORT_FAILED;
796 } else {
797 wait_ms(100);
798 goto do_retry;
799 }
800 break;
801 default:
802 if(retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
803 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
804 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
805 return USB_STOR_TRANSPORT_FAILED;
806 } else {
807 goto do_retry;
808 }
809 break;
wdenkaffae2b2002-08-17 09:36:01 +0000810 }
811 return USB_STOR_TRANSPORT_FAILED;
812}
813
814
wdenkaffae2b2002-08-17 09:36:01 +0000815static int usb_inquiry(ccb *srb,struct us_data *ss)
816{
817 int retry,i;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200818 retry=5;
wdenkaffae2b2002-08-17 09:36:01 +0000819 do {
820 memset(&srb->cmd[0],0,12);
821 srb->cmd[0]=SCSI_INQUIRY;
822 srb->cmd[1]=srb->lun<<5;
823 srb->cmd[4]=36;
824 srb->datalen=36;
825 srb->cmdlen=12;
826 i=ss->transport(srb,ss);
827 USB_STOR_PRINTF("inquiry returns %d\n",i);
828 if(i==0)
829 break;
wdenk149dded2003-09-10 18:20:28 +0000830 } while(retry--);
831
wdenkaffae2b2002-08-17 09:36:01 +0000832 if(!retry) {
833 printf("error in inquiry\n");
834 return -1;
835 }
836 return 0;
837}
838
839static int usb_request_sense(ccb *srb,struct us_data *ss)
840{
841 char *ptr;
wdenk80885a92004-02-26 23:46:20 +0000842
wdenkaffae2b2002-08-17 09:36:01 +0000843 ptr=srb->pdata;
844 memset(&srb->cmd[0],0,12);
845 srb->cmd[0]=SCSI_REQ_SENSE;
846 srb->cmd[1]=srb->lun<<5;
847 srb->cmd[4]=18;
848 srb->datalen=18;
849 srb->pdata=&srb->sense_buf[0];
850 srb->cmdlen=12;
851 ss->transport(srb,ss);
852 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
853 srb->pdata=ptr;
854 return 0;
855}
856
857static int usb_test_unit_ready(ccb *srb,struct us_data *ss)
858{
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200859 int retries = 10;
wdenk149dded2003-09-10 18:20:28 +0000860
wdenkaffae2b2002-08-17 09:36:01 +0000861 do {
862 memset(&srb->cmd[0],0,12);
863 srb->cmd[0]=SCSI_TST_U_RDY;
864 srb->cmd[1]=srb->lun<<5;
865 srb->datalen=0;
866 srb->cmdlen=12;
wdenk149dded2003-09-10 18:20:28 +0000867 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
wdenkaffae2b2002-08-17 09:36:01 +0000868 return 0;
869 }
wdenk80885a92004-02-26 23:46:20 +0000870 usb_request_sense (srb, ss);
871 wait_ms (100);
wdenkaffae2b2002-08-17 09:36:01 +0000872 } while(retries--);
wdenk149dded2003-09-10 18:20:28 +0000873
wdenkaffae2b2002-08-17 09:36:01 +0000874 return -1;
875}
876
877static int usb_read_capacity(ccb *srb,struct us_data *ss)
878{
879 int retry;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200880 retry = 3; /* retries */
wdenkaffae2b2002-08-17 09:36:01 +0000881 do {
882 memset(&srb->cmd[0],0,12);
883 srb->cmd[0]=SCSI_RD_CAPAC;
884 srb->cmd[1]=srb->lun<<5;
885 srb->datalen=8;
886 srb->cmdlen=12;
887 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
888 return 0;
889 }
wdenk149dded2003-09-10 18:20:28 +0000890 } while(retry--);
891
wdenkaffae2b2002-08-17 09:36:01 +0000892 return -1;
893}
894
895static int usb_read_10(ccb *srb,struct us_data *ss, unsigned long start, unsigned short blocks)
896{
897 memset(&srb->cmd[0],0,12);
898 srb->cmd[0]=SCSI_READ10;
899 srb->cmd[1]=srb->lun<<5;
900 srb->cmd[2]=((unsigned char) (start>>24))&0xff;
901 srb->cmd[3]=((unsigned char) (start>>16))&0xff;
902 srb->cmd[4]=((unsigned char) (start>>8))&0xff;
903 srb->cmd[5]=((unsigned char) (start))&0xff;
904 srb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
905 srb->cmd[8]=(unsigned char) blocks & 0xff;
906 srb->cmdlen=12;
907 USB_STOR_PRINTF("read10: start %lx blocks %x\n",start,blocks);
908 return ss->transport(srb,ss);
909}
910
911
912#define USB_MAX_READ_BLK 20
913
914unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer)
915{
916 unsigned long start,blks, buf_addr;
917 unsigned short smallblks;
918 struct usb_device *dev;
919 int retry,i;
wdenkf8d813e2004-03-02 14:05:39 +0000920 ccb *srb = &usb_ccb;
921
922 if (blkcnt == 0)
923 return 0;
924
925 device &= 0xff;
wdenkaffae2b2002-08-17 09:36:01 +0000926 /* Setup device
927 */
928 USB_STOR_PRINTF("\nusb_read: dev %d \n",device);
929 dev=NULL;
930 for(i=0;i<USB_MAX_DEVICE;i++) {
931 dev=usb_get_dev_index(i);
932 if(dev==NULL) {
933 return 0;
934 }
935 if(dev->devnum==usb_dev_desc[device].target)
936 break;
937 }
938
939 usb_disable_asynch(1); /* asynch transfer not allowed */
940 srb->lun=usb_dev_desc[device].lun;
941 buf_addr=(unsigned long)buffer;
942 start=blknr;
943 blks=blkcnt;
944 if(usb_test_unit_ready(srb,(struct us_data *)dev->privptr)) {
945 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
946 srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
947 return 0;
948 }
949 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks, buf_addr);
950 do {
951 retry=2;
952 srb->pdata=(unsigned char *)buf_addr;
953 if(blks>USB_MAX_READ_BLK) {
954 smallblks=USB_MAX_READ_BLK;
wdenk149dded2003-09-10 18:20:28 +0000955 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000956 smallblks=(unsigned short) blks;
957 }
958retry_it:
959 if(smallblks==USB_MAX_READ_BLK)
960 usb_show_progress();
961 srb->datalen=usb_dev_desc[device].blksz * smallblks;
962 srb->pdata=(unsigned char *)buf_addr;
963 if(usb_read_10(srb,(struct us_data *)dev->privptr, start, smallblks)) {
964 USB_STOR_PRINTF("Read ERROR\n");
965 usb_request_sense(srb,(struct us_data *)dev->privptr);
966 if(retry--)
967 goto retry_it;
968 blkcnt-=blks;
969 break;
970 }
971 start+=smallblks;
972 blks-=smallblks;
973 buf_addr+=srb->datalen;
974 } while(blks!=0);
975 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
976 usb_disable_asynch(0); /* asynch transfer allowed */
977 if(blkcnt>=USB_MAX_READ_BLK)
978 printf("\n");
979 return(blkcnt);
980}
981
982
983/* Probe to see if a new device is actually a Storage device */
984int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss)
985{
986 struct usb_interface_descriptor *iface;
987 int i;
988 unsigned int flags = 0;
989
990 int protocol = 0;
991 int subclass = 0;
992
wdenkaffae2b2002-08-17 09:36:01 +0000993 /* let's examine the device now */
994 iface = &dev->config.if_desc[ifnum];
995
996#if 0
997 /* this is the place to patch some storage devices */
998 USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
999 if ((dev->descriptor.idVendor) == 0x066b && (dev->descriptor.idProduct) == 0x0103) {
1000 USB_STOR_PRINTF("patched for E-USB\n");
1001 protocol = US_PR_CB;
1002 subclass = US_SC_UFI; /* an assumption */
1003 }
1004#endif
1005
1006 if (dev->descriptor.bDeviceClass != 0 ||
1007 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
1008 iface->bInterfaceSubClass < US_SC_MIN ||
1009 iface->bInterfaceSubClass > US_SC_MAX) {
1010 /* if it's not a mass storage, we go no further */
1011 return 0;
1012 }
1013
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001014 memset(ss, 0, sizeof(struct us_data));
1015
wdenkaffae2b2002-08-17 09:36:01 +00001016 /* At this point, we know we've got a live one */
1017 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
1018
1019 /* Initialize the us_data structure with some useful info */
1020 ss->flags = flags;
1021 ss->ifnum = ifnum;
1022 ss->pusb_dev = dev;
1023 ss->attention_done = 0;
1024
1025 /* If the device has subclass and protocol, then use that. Otherwise,
1026 * take data from the specific interface.
1027 */
1028 if (subclass) {
1029 ss->subclass = subclass;
1030 ss->protocol = protocol;
1031 } else {
1032 ss->subclass = iface->bInterfaceSubClass;
1033 ss->protocol = iface->bInterfaceProtocol;
1034 }
1035
1036 /* set the handler pointers based on the protocol */
1037 USB_STOR_PRINTF("Transport: ");
1038 switch (ss->protocol) {
1039 case US_PR_CB:
1040 USB_STOR_PRINTF("Control/Bulk\n");
1041 ss->transport = usb_stor_CB_transport;
1042 ss->transport_reset = usb_stor_CB_reset;
1043 break;
1044
1045 case US_PR_CBI:
1046 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
1047 ss->transport = usb_stor_CB_transport;
1048 ss->transport_reset = usb_stor_CB_reset;
1049 break;
wdenk149dded2003-09-10 18:20:28 +00001050 case US_PR_BULK:
1051 USB_STOR_PRINTF("Bulk/Bulk/Bulk\n");
1052 ss->transport = usb_stor_BBB_transport;
1053 ss->transport_reset = usb_stor_BBB_reset;
1054 break;
wdenkaffae2b2002-08-17 09:36:01 +00001055 default:
wdenk80885a92004-02-26 23:46:20 +00001056 printf("USB Storage Transport unknown / not yet implemented\n");
wdenkaffae2b2002-08-17 09:36:01 +00001057 return 0;
1058 break;
1059 }
1060
1061 /*
1062 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1063 * An optional interrupt is OK (necessary for CBI protocol).
1064 * We will ignore any others.
1065 */
1066 for (i = 0; i < iface->bNumEndpoints; i++) {
1067 /* is it an BULK endpoint? */
1068 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1069 == USB_ENDPOINT_XFER_BULK) {
1070 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
1071 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
1072 USB_ENDPOINT_NUMBER_MASK;
1073 else
1074 ss->ep_out = iface->ep_desc[i].bEndpointAddress &
1075 USB_ENDPOINT_NUMBER_MASK;
1076 }
1077
1078 /* is it an interrupt endpoint? */
1079 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1080 == USB_ENDPOINT_XFER_INT) {
1081 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
1082 USB_ENDPOINT_NUMBER_MASK;
1083 ss->irqinterval = iface->ep_desc[i].bInterval;
1084 }
1085 }
1086 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
1087 ss->ep_in, ss->ep_out, ss->ep_int);
1088
1089 /* Do some basic sanity checks, and bail if we find a problem */
1090 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
1091 !ss->ep_in || !ss->ep_out ||
1092 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
1093 USB_STOR_PRINTF("Problems with device\n");
1094 return 0;
1095 }
1096 /* set class specific stuff */
wdenk149dded2003-09-10 18:20:28 +00001097 /* We only handle certain protocols. Currently, these are
1098 * the only ones.
wdenk80885a92004-02-26 23:46:20 +00001099 * The SFF8070 accepts the requests used in u-boot
wdenkaffae2b2002-08-17 09:36:01 +00001100 */
wdenk80885a92004-02-26 23:46:20 +00001101 if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
1102 ss->subclass != US_SC_8070) {
wdenkaffae2b2002-08-17 09:36:01 +00001103 printf("Sorry, protocol %d not yet supported.\n",ss->subclass);
1104 return 0;
1105 }
wdenk149dded2003-09-10 18:20:28 +00001106 if(ss->ep_int) { /* we had found an interrupt endpoint, prepare irq pipe */
wdenkaffae2b2002-08-17 09:36:01 +00001107 /* set up the IRQ pipe and handler */
1108
1109 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
1110 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
1111 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
1112 dev->irq_handle=usb_stor_irq;
wdenkaffae2b2002-08-17 09:36:01 +00001113 }
wdenka43278a2003-09-11 19:48:06 +00001114 dev->privptr=(void *)ss;
wdenkaffae2b2002-08-17 09:36:01 +00001115 return 1;
1116}
1117
1118int usb_stor_get_info(struct usb_device *dev,struct us_data *ss,block_dev_desc_t *dev_desc)
1119{
1120 unsigned char perq,modi;
1121 unsigned long cap[2];
1122 unsigned long *capacity,*blksz;
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001123 ccb *pccb = &usb_ccb;
wdenkaffae2b2002-08-17 09:36:01 +00001124
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001125 /* for some reasons a couple of devices would not survive this reset */
1126 if (
1127 /* Sony USM256E */
1128 (dev->descriptor.idVendor == 0x054c &&
1129 dev->descriptor.idProduct == 0x019e)
1130
1131 ||
1132 /* USB007 Mini-USB2 Flash Drive */
1133 (dev->descriptor.idVendor == 0x066f &&
1134 dev->descriptor.idProduct == 0x2010)
1135 )
1136 USB_STOR_PRINTF("usb_stor_get_info: skipping RESET..\n");
Wolfgang Denk095b8a32005-08-02 17:06:17 +02001137 else
wdenk2729af92004-05-03 20:45:30 +00001138 ss->transport_reset(ss);
wdenkaffae2b2002-08-17 09:36:01 +00001139
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001140 pccb->pdata = usb_stor_buf;
1141
1142 dev_desc->target = dev->devnum;
1143 pccb->lun = dev_desc->lun;
wdenkaffae2b2002-08-17 09:36:01 +00001144 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1145
1146 if(usb_inquiry(pccb,ss))
1147 return -1;
Wolfgang Denk095b8a32005-08-02 17:06:17 +02001148
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001149 perq = usb_stor_buf[0];
1150 modi = usb_stor_buf[1];
1151 if((perq & 0x1f) == 0x1f) {
wdenkaffae2b2002-08-17 09:36:01 +00001152 return 0; /* skip unknown devices */
1153 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001154 if((modi&0x80) == 0x80) {/* drive is removable */
1155 dev_desc->removable = 1;
wdenkaffae2b2002-08-17 09:36:01 +00001156 }
1157 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
1158 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
1159 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001160 dev_desc->vendor[8] = 0;
1161 dev_desc->product[16] = 0;
1162 dev_desc->revision[4] = 0;
wdenkaffae2b2002-08-17 09:36:01 +00001163 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf[2],usb_stor_buf[3]);
1164 if(usb_test_unit_ready(pccb,ss)) {
1165 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 +02001166 if(dev_desc->removable == 1) {
1167 dev_desc->type = perq;
wdenkaffae2b2002-08-17 09:36:01 +00001168 return 1;
1169 }
1170 else
1171 return 0;
1172 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001173 pccb->pdata = (unsigned char *)&cap[0];
wdenkaffae2b2002-08-17 09:36:01 +00001174 memset(pccb->pdata,0,8);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001175 if(usb_read_capacity(pccb,ss) != 0) {
wdenkaffae2b2002-08-17 09:36:01 +00001176 printf("READ_CAP ERROR\n");
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001177 cap[0] = 2880;
1178 cap[1] = 0x200;
wdenkaffae2b2002-08-17 09:36:01 +00001179 }
1180 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap[0],cap[1]);
1181#if 0
1182 if(cap[0]>(0x200000 * 10)) /* greater than 10 GByte */
1183 cap[0]>>=16;
1184#endif
wdenk149dded2003-09-10 18:20:28 +00001185#ifdef LITTLEENDIAN
1186 cap[0] = ((unsigned long)(
1187 (((unsigned long)(cap[0]) & (unsigned long)0x000000ffUL) << 24) |
1188 (((unsigned long)(cap[0]) & (unsigned long)0x0000ff00UL) << 8) |
1189 (((unsigned long)(cap[0]) & (unsigned long)0x00ff0000UL) >> 8) |
1190 (((unsigned long)(cap[0]) & (unsigned long)0xff000000UL) >> 24) ));
1191 cap[1] = ((unsigned long)(
1192 (((unsigned long)(cap[1]) & (unsigned long)0x000000ffUL) << 24) |
1193 (((unsigned long)(cap[1]) & (unsigned long)0x0000ff00UL) << 8) |
1194 (((unsigned long)(cap[1]) & (unsigned long)0x00ff0000UL) >> 8) |
1195 (((unsigned long)(cap[1]) & (unsigned long)0xff000000UL) >> 24) ));
1196#endif
1197 /* this assumes bigendian! */
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001198 cap[0] += 1;
1199 capacity = &cap[0];
1200 blksz = &cap[1];
wdenkaffae2b2002-08-17 09:36:01 +00001201 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity,*blksz);
Wolfgang Denk9c998aa2005-07-21 11:57:57 +02001202 dev_desc->lba = *capacity;
1203 dev_desc->blksz = *blksz;
1204 dev_desc->type = perq;
wdenkaffae2b2002-08-17 09:36:01 +00001205 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
1206 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1207
1208 init_part(dev_desc);
1209
1210 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
1211 return 1;
1212}
1213
wdenkaffae2b2002-08-17 09:36:01 +00001214#endif /* CONFIG_USB_STORAGE */
wdenk149dded2003-09-10 18:20:28 +00001215#endif /* CFG_CMD_USB */