blob: a7944b57376cd34a23b66ae0fc424a9d002dc0aa [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
5 * Most of this source has been derived from the Linux USB
6 * project.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 *
26 */
27
28/* Note:
29 * Currently only the CBI transport protocoll has been implemented, and it
30 * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
31 * transport protocoll may work as well.
32 */
33
34
wdenkaffae2b2002-08-17 09:36:01 +000035#include <common.h>
36#include <command.h>
37#include <asm/processor.h>
38
39
40#if (CONFIG_COMMANDS & CFG_CMD_USB)
41#include <usb.h>
42
43#ifdef CONFIG_USB_STORAGE
44
45#undef USB_STOR_DEBUG
46
47#ifdef USB_STOR_DEBUG
48#define USB_STOR_PRINTF(fmt,args...) printf (fmt ,##args)
49#else
50#define USB_STOR_PRINTF(fmt,args...)
51#endif
52
53#include <scsi.h>
54/* direction table -- this indicates the direction of the data
55 * transfer for each command code -- a 1 indicates input
56 */
57unsigned char us_direction[256/8] = {
58 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
59 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
62};
63#define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
64
65static unsigned char usb_stor_buf[512];
66static ccb usb_ccb;
67
68/*
69 * CBI style
70 */
71
72#define US_CBI_ADSC 0
73
74
75#define USB_MAX_STOR_DEV 5
76static int usb_max_devs; /* number of highest available usb device */
77
78static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
79
80struct us_data;
81typedef int (*trans_cmnd)(ccb*, struct us_data*);
82typedef int (*trans_reset)(struct us_data*);
83
84struct us_data {
85 struct usb_device *pusb_dev; /* this usb_device */
86 unsigned int flags; /* from filter initially */
87 unsigned char ifnum; /* interface number */
88 unsigned char ep_in; /* in endpoint */
89 unsigned char ep_out; /* out ....... */
90 unsigned char ep_int; /* interrupt . */
91 unsigned char subclass; /* as in overview */
92 unsigned char protocol; /* .............. */
93 unsigned char attention_done; /* force attn on first cmd */
94 unsigned short ip_data; /* interrupt data */
95 int action; /* what to do */
96 int ip_wanted; /* needed */
97 int *irq_handle; /* for USB int requests */
98 unsigned int irqpipe; /* pipe for release_irq */
99 unsigned char irqmaxp; /* max packed for irq Pipe */
100 unsigned char irqinterval; /* Intervall for IRQ Pipe */
101 ccb *srb; /* current srb */
102 trans_reset transport_reset; /* reset routine */
103 trans_cmnd transport; /* transport routine */
104};
105
106static struct us_data usb_stor[USB_MAX_STOR_DEV];
107
108
wdenkaffae2b2002-08-17 09:36:01 +0000109#define USB_STOR_TRANSPORT_GOOD 0
110#define USB_STOR_TRANSPORT_FAILED -1
111#define USB_STOR_TRANSPORT_ERROR -2
112
113
wdenkaffae2b2002-08-17 09:36:01 +0000114int usb_stor_get_info(struct usb_device *dev, struct us_data *us, block_dev_desc_t *dev_desc);
115int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss);
116unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer);
117struct usb_device * usb_get_dev_index(int index);
118void uhci_show_temp_int_td(void);
119
120block_dev_desc_t *usb_stor_get_dev(int index)
121{
122 return &usb_dev_desc[index];
123}
124
125
126void usb_show_progress(void)
127{
128 printf(".");
129}
130
131/*********************************************************************************
132 * (re)-scan the usb and reports device info
133 * to the user if mode = 1
134 * returns current device or -1 if no
135 */
136int usb_stor_scan(int mode)
137{
138 unsigned char i;
139 struct usb_device *dev;
140
141 if(mode==1) {
142 printf("scanning bus for storage devices...\n");
143 }
144 usb_disable_asynch(1); /* asynch transfer not allowed */
145
146 for(i=0;i<USB_MAX_STOR_DEV;i++) {
147 memset(&usb_dev_desc[i],0,sizeof(block_dev_desc_t));
148 usb_dev_desc[i].target=0xff;
149 usb_dev_desc[i].if_type=IF_TYPE_USB;
150 usb_dev_desc[i].dev=i;
151 usb_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
152 usb_dev_desc[i].block_read=usb_stor_read;
153 }
154 usb_max_devs=0;
155 for(i=0;i<USB_MAX_DEVICE;i++) {
156 dev=usb_get_dev_index(i); /* get device */
157 USB_STOR_PRINTF("i=%d\n",i);
158 if(dev==NULL) {
159 break; /* no more devices avaiable */
160 }
161 if(usb_storage_probe(dev,0,&usb_stor[usb_max_devs])) { /* ok, it is a storage devices */
162 /* get info and fill it in */
163
164 if(usb_stor_get_info(dev, &usb_stor[usb_max_devs], &usb_dev_desc[usb_max_devs])) {
165 if(mode==1) {
166 printf (" Device %d: ", usb_max_devs);
167 dev_print(&usb_dev_desc[usb_max_devs]);
168 } /* if mode */
169 usb_max_devs++;
170 } /* if get info ok */
171 } /* if storage device */
172 if(usb_max_devs==USB_MAX_STOR_DEV) {
173 printf("max USB Storage Device reached: %d stopping\n",usb_max_devs);
174 break;
175 }
176 } /* for */
177 usb_disable_asynch(0); /* asynch transfer allowed */
178 if(usb_max_devs>0)
179 return 0;
180 else
181 return-1;
182}
183
184static int usb_stor_irq(struct usb_device *dev)
185{
186 struct us_data *us;
187 us=(struct us_data *)dev->privptr;
188
189 if(us->ip_wanted) {
190 us->ip_wanted=0;
191 }
192 return 0;
193}
194
195
196#ifdef USB_STOR_DEBUG
197
198static void usb_show_srb(ccb * pccb)
199{
200 int i;
201 printf("SRB: len %d datalen 0x%lX\n ",pccb->cmdlen,pccb->datalen);
202 for(i=0;i<12;i++) {
203 printf("%02X ",pccb->cmd[i]);
204 }
205 printf("\n");
206}
207
208static void display_int_status(unsigned long tmp)
209{
210 printf("Status: %s %s %s %s %s %s %s\n",
211 (tmp & USB_ST_ACTIVE) ? "Active" : "",
212 (tmp & USB_ST_STALLED) ? "Stalled" : "",
213 (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
214 (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
215 (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
216 (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
217 (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
218}
219#endif
220/***********************************************************************
221 * Data transfer routines
222 ***********************************************************************/
223
224static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
225{
226 int max_size;
227 int this_xfer;
228 int result;
229 int partial;
230 int maxtry;
231 int stat;
232
233 /* determine the maximum packet size for these transfers */
234 max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
235
236 /* while we have data left to transfer */
237 while (length) {
238
239 /* calculate how long this will be -- maximum or a remainder */
240 this_xfer = length > max_size ? max_size : length;
241 length -= this_xfer;
242
243 /* setup the retry counter */
244 maxtry = 10;
245
246 /* set up the transfer loop */
247 do {
248 /* transfer the data */
249 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
250 (unsigned int)buf, this_xfer, 11 - maxtry);
251 result = usb_bulk_msg(us->pusb_dev, pipe, buf,
252 this_xfer, &partial, USB_CNTL_TIMEOUT*5);
253 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
254 result, partial, this_xfer);
255 if(us->pusb_dev->status!=0) {
256 /* if we stall, we need to clear it before we go on */
257#ifdef USB_STOR_DEBUG
258 display_int_status(us->pusb_dev->status);
259#endif
260 if (us->pusb_dev->status & USB_ST_STALLED) {
261 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
262 stat = us->pusb_dev->status;
263 usb_clear_halt(us->pusb_dev, pipe);
264 us->pusb_dev->status=stat;
265 if(this_xfer == partial) {
266 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us->pusb_dev->status);
267 return 0;
268 }
269 else
270 return result;
271 }
272 if (us->pusb_dev->status & USB_ST_NAK_REC) {
273 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
274 return result;
275 }
276 if(this_xfer == partial) {
277 USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us->pusb_dev->status);
278 return 0;
279 }
280 /* if our try counter reaches 0, bail out */
281 USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us->pusb_dev->status,partial);
282 if (!maxtry--)
283 return result;
284 }
285 /* update to show what data was transferred */
286 this_xfer -= partial;
287 buf += partial;
288 /* continue until this transfer is done */
289 } while ( this_xfer );
290 }
291
292 /* if we get here, we're done and successful */
293 return 0;
294}
295
296/* FIXME: this reset function doesn't really reset the port, and it
297 * should. Actually it should probably do what it's doing here, and
298 * reset the port physically
299 */
300static int usb_stor_CB_reset(struct us_data *us)
301{
302 unsigned char cmd[12];
303 int result;
304
305 USB_STOR_PRINTF("CB_reset\n");
306 memset(cmd, 0xFF, sizeof(cmd));
307 cmd[0] = SCSI_SEND_DIAG;
308 cmd[1] = 4;
309 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
310 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
311 0, us->ifnum, cmd, sizeof(cmd), USB_CNTL_TIMEOUT*5);
312
313 /* long wait for reset */
314 wait_ms(1500);
315 USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result,us->pusb_dev->status);
316 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
317 usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
318
319 USB_STOR_PRINTF("CB_reset done\n");
320 return 0;
321}
322
323/* FIXME: we also need a CBI_command which sets up the completion
324 * interrupt, and waits for it
325 */
326int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
327{
328 int result;
329 int dir_in,retry;
330 unsigned int pipe;
331 unsigned long status;
332
333 retry=5;
334 dir_in=US_DIRECTION(srb->cmd[0]);
335
336 if(dir_in)
337 pipe=usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
338 else
339 pipe=usb_sndbulkpipe(us->pusb_dev, us->ep_out);
340 while(retry--) {
341 USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry);
342#ifdef USB_STOR_DEBUG
343 usb_show_srb(srb);
344#endif
345 /* let's send the command via the control pipe */
346 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
347 US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
348 0, us->ifnum,
349 srb->cmd, srb->cmdlen, USB_CNTL_TIMEOUT*5);
350 USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result,us->pusb_dev->status);
351 /* check the return code for the command */
352 if (result < 0) {
353 if(us->pusb_dev->status & USB_ST_STALLED) {
354 status=us->pusb_dev->status;
355 USB_STOR_PRINTF(" stall during command found, clear pipe\n");
356 usb_clear_halt(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0));
357 us->pusb_dev->status=status;
358 }
359 USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb->cmd[0],us->pusb_dev->status);
360 return result;
361 }
362 /* transfer the data payload for this command, if one exists*/
363
364 USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result,dir_in ? "IN" : "OUT",srb->datalen);
365 if (srb->datalen) {
366 result = us_one_transfer(us, pipe, srb->pdata,srb->datalen);
367 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);
368 if(!(us->pusb_dev->status & USB_ST_NAK_REC))
369 break;
370 } /* if (srb->datalen) */
371 else
372 break;
373 }
374 /* return result */
375
376 return result;
377}
378
379
380int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
381{
382 int timeout;
383
384 us->ip_wanted=1;
385 submit_int_msg(us->pusb_dev,us->irqpipe,
386 (void *)&us->ip_data,us->irqmaxp ,us->irqinterval);
387 timeout=1000;
388 while(timeout--) {
389 if((volatile int *)us->ip_wanted==0)
390 break;
391 wait_ms(10);
392 }
393 if (us->ip_wanted) {
394 printf(" Did not get interrupt on CBI\n");
395 us->ip_wanted = 0;
396 return USB_STOR_TRANSPORT_ERROR;
397 }
398 USB_STOR_PRINTF("Got interrupt data 0x%x, transfered %d status 0x%lX\n", us->ip_data,us->pusb_dev->irq_act_len,us->pusb_dev->irq_status);
399 /* UFI gives us ASC and ASCQ, like a request sense */
400 if (us->subclass == US_SC_UFI) {
401 if (srb->cmd[0] == SCSI_REQ_SENSE ||
402 srb->cmd[0] == SCSI_INQUIRY)
403 return USB_STOR_TRANSPORT_GOOD; /* Good */
404 else
405 if (us->ip_data)
406 return USB_STOR_TRANSPORT_FAILED;
407 else
408 return USB_STOR_TRANSPORT_GOOD;
409 }
410 /* otherwise, we interpret the data normally */
411 switch (us->ip_data) {
412 case 0x0001:
413 return USB_STOR_TRANSPORT_GOOD;
414 case 0x0002:
415 return USB_STOR_TRANSPORT_FAILED;
416 default:
417 return USB_STOR_TRANSPORT_ERROR;
418 } /* switch */
419 return USB_STOR_TRANSPORT_ERROR;
420}
421
422#define USB_TRANSPORT_UNKNOWN_RETRY 5
423#define USB_TRANSPORT_NOT_READY_RETRY 10
424
425int usb_stor_CB_transport(ccb *srb, struct us_data *us)
426{
427 int result,status;
428 ccb *psrb;
429 ccb reqsrb;
430 int retry,notready;
431
432 psrb=&reqsrb;
433 status=USB_STOR_TRANSPORT_GOOD;
434 retry=0;
435 notready=0;
436 /* issue the command */
437do_retry:
438 result=usb_stor_CB_comdat(srb,us);
439 USB_STOR_PRINTF("command / Data returned %d, status %X\n",result,us->pusb_dev->status);
440 /* if this is an CBI Protocol, get IRQ */
441 if(us->protocol==US_PR_CBI) {
442 status=usb_stor_CBI_get_status(srb,us);
443 /* if the status is error, report it */
444 if(status==USB_STOR_TRANSPORT_ERROR) {
445 USB_STOR_PRINTF(" USB CBI Command Error\n");
446 return status;
447 }
448 srb->sense_buf[12]=(unsigned char)(us->ip_data>>8);
449 srb->sense_buf[13]=(unsigned char)(us->ip_data&0xff);
450 if(!us->ip_data) {
451 /* if the status is good, report it */
452 if(status==USB_STOR_TRANSPORT_GOOD) {
453 USB_STOR_PRINTF(" USB CBI Command Good\n");
454 return status;
455 }
456 }
457 }
458 /* do we have to issue an auto request? */
459 /* HERE we have to check the result */
460 if((result<0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
461 USB_STOR_PRINTF("ERROR %X\n",us->pusb_dev->status);
462 us->transport_reset(us);
463 return USB_STOR_TRANSPORT_ERROR;
464 }
465 if((us->protocol==US_PR_CBI) &&
466 ((srb->cmd[0]==SCSI_REQ_SENSE) ||
467 (srb->cmd[0]==SCSI_INQUIRY))) { /* do not issue an autorequest after request sense */
468 USB_STOR_PRINTF("No auto request and good\n");
469 return USB_STOR_TRANSPORT_GOOD;
470 }
471 /* issue an request_sense */
472 memset(&psrb->cmd[0],0,12);
473 psrb->cmd[0]=SCSI_REQ_SENSE;
474 psrb->cmd[1]=srb->lun<<5;
475 psrb->cmd[4]=18;
476 psrb->datalen=18;
477 psrb->pdata=&srb->sense_buf[0];
478 psrb->cmdlen=12;
479 /* issue the command */
480 result=usb_stor_CB_comdat(psrb,us);
481 USB_STOR_PRINTF("auto request returned %d\n",result);
482 /* if this is an CBI Protocol, get IRQ */
483 if(us->protocol==US_PR_CBI) {
484 status=usb_stor_CBI_get_status(psrb,us);
485 }
486 if((result<0)&&!(us->pusb_dev->status & USB_ST_STALLED)) {
487 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us->pusb_dev->status);
488 return USB_STOR_TRANSPORT_ERROR;
489 }
490 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]);
491 /* Check the auto request result */
492 if((srb->sense_buf[2]==0) &&
493 (srb->sense_buf[12]==0) &&
494 (srb->sense_buf[13]==0)) /* ok, no sense */
495 return USB_STOR_TRANSPORT_GOOD;
496 /* Check the auto request result */
497 switch(srb->sense_buf[2]) {
498 case 0x01: /* Recovered Error */
499 return USB_STOR_TRANSPORT_GOOD;
500 break;
501 case 0x02: /* Not Ready */
502 if(notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
503 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
504 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
505 return USB_STOR_TRANSPORT_FAILED;
506 }
507 else {
508 wait_ms(100);
509 goto do_retry;
510 }
511 break;
512 default:
513 if(retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
514 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
515 srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
516 return USB_STOR_TRANSPORT_FAILED;
517 }
518 else
519 goto do_retry;
520 break;
521 }
522 return USB_STOR_TRANSPORT_FAILED;
523}
524
525
wdenkaffae2b2002-08-17 09:36:01 +0000526static int usb_inquiry(ccb *srb,struct us_data *ss)
527{
528 int retry,i;
529 retry=3;
530 do {
531 memset(&srb->cmd[0],0,12);
532 srb->cmd[0]=SCSI_INQUIRY;
533 srb->cmd[1]=srb->lun<<5;
534 srb->cmd[4]=36;
535 srb->datalen=36;
536 srb->cmdlen=12;
537 i=ss->transport(srb,ss);
538 USB_STOR_PRINTF("inquiry returns %d\n",i);
539 if(i==0)
540 break;
541 }while(retry--);
542 if(!retry) {
543 printf("error in inquiry\n");
544 return -1;
545 }
546 return 0;
547}
548
549static int usb_request_sense(ccb *srb,struct us_data *ss)
550{
551 char *ptr;
552 return 0;
553 ptr=srb->pdata;
554 memset(&srb->cmd[0],0,12);
555 srb->cmd[0]=SCSI_REQ_SENSE;
556 srb->cmd[1]=srb->lun<<5;
557 srb->cmd[4]=18;
558 srb->datalen=18;
559 srb->pdata=&srb->sense_buf[0];
560 srb->cmdlen=12;
561 ss->transport(srb,ss);
562 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
563 srb->pdata=ptr;
564 return 0;
565}
566
567static int usb_test_unit_ready(ccb *srb,struct us_data *ss)
568{
569 int retries=10;
570 do {
571 memset(&srb->cmd[0],0,12);
572 srb->cmd[0]=SCSI_TST_U_RDY;
573 srb->cmd[1]=srb->lun<<5;
574 srb->datalen=0;
575 srb->cmdlen=12;
576 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD)
577 {
578 return 0;
579 }
580 } while(retries--);
581 return -1;
582}
583
584static int usb_read_capacity(ccb *srb,struct us_data *ss)
585{
586 int retry;
587 retry=2; /* retries */
588 do {
589 memset(&srb->cmd[0],0,12);
590 srb->cmd[0]=SCSI_RD_CAPAC;
591 srb->cmd[1]=srb->lun<<5;
592 srb->datalen=8;
593 srb->cmdlen=12;
594 if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
595 return 0;
596 }
597 }while(retry--);
598 return -1;
599}
600
601static int usb_read_10(ccb *srb,struct us_data *ss, unsigned long start, unsigned short blocks)
602{
603 memset(&srb->cmd[0],0,12);
604 srb->cmd[0]=SCSI_READ10;
605 srb->cmd[1]=srb->lun<<5;
606 srb->cmd[2]=((unsigned char) (start>>24))&0xff;
607 srb->cmd[3]=((unsigned char) (start>>16))&0xff;
608 srb->cmd[4]=((unsigned char) (start>>8))&0xff;
609 srb->cmd[5]=((unsigned char) (start))&0xff;
610 srb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
611 srb->cmd[8]=(unsigned char) blocks & 0xff;
612 srb->cmdlen=12;
613 USB_STOR_PRINTF("read10: start %lx blocks %x\n",start,blocks);
614 return ss->transport(srb,ss);
615}
616
617
618#define USB_MAX_READ_BLK 20
619
620unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer)
621{
622 unsigned long start,blks, buf_addr;
623 unsigned short smallblks;
624 struct usb_device *dev;
625 int retry,i;
626 ccb *srb=&usb_ccb;
627 device&=0xff;
628 /* Setup device
629 */
630 USB_STOR_PRINTF("\nusb_read: dev %d \n",device);
631 dev=NULL;
632 for(i=0;i<USB_MAX_DEVICE;i++) {
633 dev=usb_get_dev_index(i);
634 if(dev==NULL) {
635 return 0;
636 }
637 if(dev->devnum==usb_dev_desc[device].target)
638 break;
639 }
640
641 usb_disable_asynch(1); /* asynch transfer not allowed */
642 srb->lun=usb_dev_desc[device].lun;
643 buf_addr=(unsigned long)buffer;
644 start=blknr;
645 blks=blkcnt;
646 if(usb_test_unit_ready(srb,(struct us_data *)dev->privptr)) {
647 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
648 srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
649 return 0;
650 }
651 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks, buf_addr);
652 do {
653 retry=2;
654 srb->pdata=(unsigned char *)buf_addr;
655 if(blks>USB_MAX_READ_BLK) {
656 smallblks=USB_MAX_READ_BLK;
657 }
658 else {
659 smallblks=(unsigned short) blks;
660 }
661retry_it:
662 if(smallblks==USB_MAX_READ_BLK)
663 usb_show_progress();
664 srb->datalen=usb_dev_desc[device].blksz * smallblks;
665 srb->pdata=(unsigned char *)buf_addr;
666 if(usb_read_10(srb,(struct us_data *)dev->privptr, start, smallblks)) {
667 USB_STOR_PRINTF("Read ERROR\n");
668 usb_request_sense(srb,(struct us_data *)dev->privptr);
669 if(retry--)
670 goto retry_it;
671 blkcnt-=blks;
672 break;
673 }
674 start+=smallblks;
675 blks-=smallblks;
676 buf_addr+=srb->datalen;
677 } while(blks!=0);
678 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
679 usb_disable_asynch(0); /* asynch transfer allowed */
680 if(blkcnt>=USB_MAX_READ_BLK)
681 printf("\n");
682 return(blkcnt);
683}
684
685
686/* Probe to see if a new device is actually a Storage device */
687int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss)
688{
689 struct usb_interface_descriptor *iface;
690 int i;
691 unsigned int flags = 0;
692
693 int protocol = 0;
694 int subclass = 0;
695
696
697 memset(ss, 0, sizeof(struct us_data));
698
699 /* let's examine the device now */
700 iface = &dev->config.if_desc[ifnum];
701
702#if 0
703 /* this is the place to patch some storage devices */
704 USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
705 if ((dev->descriptor.idVendor) == 0x066b && (dev->descriptor.idProduct) == 0x0103) {
706 USB_STOR_PRINTF("patched for E-USB\n");
707 protocol = US_PR_CB;
708 subclass = US_SC_UFI; /* an assumption */
709 }
710#endif
711
712 if (dev->descriptor.bDeviceClass != 0 ||
713 iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
714 iface->bInterfaceSubClass < US_SC_MIN ||
715 iface->bInterfaceSubClass > US_SC_MAX) {
716 /* if it's not a mass storage, we go no further */
717 return 0;
718 }
719
720 /* At this point, we know we've got a live one */
721 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
722
723 /* Initialize the us_data structure with some useful info */
724 ss->flags = flags;
725 ss->ifnum = ifnum;
726 ss->pusb_dev = dev;
727 ss->attention_done = 0;
728
729 /* If the device has subclass and protocol, then use that. Otherwise,
730 * take data from the specific interface.
731 */
732 if (subclass) {
733 ss->subclass = subclass;
734 ss->protocol = protocol;
735 } else {
736 ss->subclass = iface->bInterfaceSubClass;
737 ss->protocol = iface->bInterfaceProtocol;
738 }
739
740 /* set the handler pointers based on the protocol */
741 USB_STOR_PRINTF("Transport: ");
742 switch (ss->protocol) {
743 case US_PR_CB:
744 USB_STOR_PRINTF("Control/Bulk\n");
745 ss->transport = usb_stor_CB_transport;
746 ss->transport_reset = usb_stor_CB_reset;
747 break;
748
749 case US_PR_CBI:
750 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
751 ss->transport = usb_stor_CB_transport;
752 ss->transport_reset = usb_stor_CB_reset;
753 break;
754 default:
755 printf("USB Starage Transport unknown / not yet implemented\n");
756 return 0;
757 break;
758 }
759
760 /*
761 * We are expecting a minimum of 2 endpoints - in and out (bulk).
762 * An optional interrupt is OK (necessary for CBI protocol).
763 * We will ignore any others.
764 */
765 for (i = 0; i < iface->bNumEndpoints; i++) {
766 /* is it an BULK endpoint? */
767 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
768 == USB_ENDPOINT_XFER_BULK) {
769 if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
770 ss->ep_in = iface->ep_desc[i].bEndpointAddress &
771 USB_ENDPOINT_NUMBER_MASK;
772 else
773 ss->ep_out = iface->ep_desc[i].bEndpointAddress &
774 USB_ENDPOINT_NUMBER_MASK;
775 }
776
777 /* is it an interrupt endpoint? */
778 if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
779 == USB_ENDPOINT_XFER_INT) {
780 ss->ep_int = iface->ep_desc[i].bEndpointAddress &
781 USB_ENDPOINT_NUMBER_MASK;
782 ss->irqinterval = iface->ep_desc[i].bInterval;
783 }
784 }
785 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
786 ss->ep_in, ss->ep_out, ss->ep_int);
787
788 /* Do some basic sanity checks, and bail if we find a problem */
789 if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
790 !ss->ep_in || !ss->ep_out ||
791 (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
792 USB_STOR_PRINTF("Problems with device\n");
793 return 0;
794 }
795 /* set class specific stuff */
796 /* We only handle certain protocols. Currently, this is
797 * the only one.
798 */
799 if (ss->subclass != US_SC_UFI) {
800 printf("Sorry, protocol %d not yet supported.\n",ss->subclass);
801 return 0;
802 }
803 if(ss->ep_int) /* we had found an interrupt endpoint, prepare irq pipe */
804 {
805 /* set up the IRQ pipe and handler */
806
807 ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
808 ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
809 ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
810 dev->irq_handle=usb_stor_irq;
811 dev->privptr=(void *)ss;
812 }
813 return 1;
814}
815
816int usb_stor_get_info(struct usb_device *dev,struct us_data *ss,block_dev_desc_t *dev_desc)
817{
818 unsigned char perq,modi;
819 unsigned long cap[2];
820 unsigned long *capacity,*blksz;
821 ccb *pccb=&usb_ccb;
822
823 ss->transport_reset(ss);
824 pccb->pdata=usb_stor_buf;
825
826 dev_desc->target=dev->devnum;
827 pccb->lun=dev_desc->lun;
828 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
829
830 if(usb_inquiry(pccb,ss))
831 return -1;
832 perq=usb_stor_buf[0];
833 modi=usb_stor_buf[1];
834 if((perq & 0x1f)==0x1f) {
835 return 0; /* skip unknown devices */
836 }
837 if((modi&0x80)==0x80) {/* drive is removable */
838 dev_desc->removable=1;
839 }
840 memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
841 memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
842 memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
843 dev_desc->vendor[8]=0;
844 dev_desc->product[16]=0;
845 dev_desc->revision[4]=0;
846 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf[2],usb_stor_buf[3]);
847 if(usb_test_unit_ready(pccb,ss)) {
848 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",pccb->sense_buf[2],pccb->sense_buf[12],pccb->sense_buf[13]);
849 if(dev_desc->removable==1) {
850 dev_desc->type=perq;
851 return 1;
852 }
853 else
854 return 0;
855 }
856 pccb->pdata=(unsigned char *)&cap[0];
857 memset(pccb->pdata,0,8);
858 if(usb_read_capacity(pccb,ss)!=0) {
859 printf("READ_CAP ERROR\n");
860 cap[0]=2880;
861 cap[1]=0x200;
862 }
863 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap[0],cap[1]);
864#if 0
865 if(cap[0]>(0x200000 * 10)) /* greater than 10 GByte */
866 cap[0]>>=16;
867#endif
868 cap[0]+=1;
869 capacity=&cap[0];
870 blksz=&cap[1];
871 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity,*blksz);
872 dev_desc->lba=*capacity;
873 dev_desc->blksz=*blksz;
874 dev_desc->type=perq;
875 USB_STOR_PRINTF(" address %d\n",dev_desc->target);
876 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
877
878 init_part(dev_desc);
879
880 USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
881 return 1;
882}
883
884#endif
885#endif /* CONFIG_USB_STORAGE */