3 * Denis Peter, MPL AG Switzerland
5 * Most of this source has been derived from the Linux USB
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
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.
38 #include <asm/processor.h>
41 #if (CONFIG_COMMANDS & CFG_CMD_USB)
44 #ifdef CONFIG_USB_STORAGE
49 #define USB_STOR_PRINTF(fmt,args...) printf (fmt ,##args)
51 #define USB_STOR_PRINTF(fmt,args...)
55 /* direction table -- this indicates the direction of the data
56 * transfer for each command code -- a 1 indicates input
58 unsigned char us_direction
[256/8] = {
59 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
60 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
64 #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
66 static unsigned char usb_stor_buf
[512];
76 #define USB_MAX_STOR_DEV 5
77 static int usb_max_devs
; /* number of highest available usb device */
79 static block_dev_desc_t usb_dev_desc
[USB_MAX_STOR_DEV
];
82 typedef int (*trans_cmnd
)(ccb
*, struct us_data
*);
83 typedef int (*trans_reset
)(struct us_data
*);
86 struct usb_device
*pusb_dev
; /* this usb_device */
87 unsigned int flags
; /* from filter initially */
88 unsigned char ifnum
; /* interface number */
89 unsigned char ep_in
; /* in endpoint */
90 unsigned char ep_out
; /* out ....... */
91 unsigned char ep_int
; /* interrupt . */
92 unsigned char subclass
; /* as in overview */
93 unsigned char protocol
; /* .............. */
94 unsigned char attention_done
; /* force attn on first cmd */
95 unsigned short ip_data
; /* interrupt data */
96 int action
; /* what to do */
97 int ip_wanted
; /* needed */
98 int *irq_handle
; /* for USB int requests */
99 unsigned int irqpipe
; /* pipe for release_irq */
100 unsigned char irqmaxp
; /* max packed for irq Pipe */
101 unsigned char irqinterval
; /* Intervall for IRQ Pipe */
102 ccb
*srb
; /* current srb */
103 trans_reset transport_reset
; /* reset routine */
104 trans_cmnd transport
; /* transport routine */
107 static struct us_data usb_stor
[USB_MAX_STOR_DEV
];
111 #define USB_STOR_TRANSPORT_GOOD 0
112 #define USB_STOR_TRANSPORT_FAILED -1
113 #define USB_STOR_TRANSPORT_ERROR -2
120 int usb_stor_get_info(struct usb_device
*dev
, struct us_data
*us
, block_dev_desc_t
*dev_desc
);
121 int usb_storage_probe(struct usb_device
*dev
, unsigned int ifnum
,struct us_data
*ss
);
122 unsigned long usb_stor_read(int device
, unsigned long blknr
, unsigned long blkcnt
, unsigned long *buffer
);
123 struct usb_device
* usb_get_dev_index(int index
);
124 void uhci_show_temp_int_td(void);
126 block_dev_desc_t
*usb_stor_get_dev(int index
)
128 return &usb_dev_desc
[index
];
132 void usb_show_progress(void)
137 /*********************************************************************************
138 * (re)-scan the usb and reports device info
139 * to the user if mode = 1
140 * returns current device or -1 if no
142 int usb_stor_scan(int mode
)
145 struct usb_device
*dev
;
148 printf("scanning bus for storage devices...\n");
150 usb_disable_asynch(1); /* asynch transfer not allowed */
152 for(i
=0;i
<USB_MAX_STOR_DEV
;i
++) {
153 memset(&usb_dev_desc
[i
],0,sizeof(block_dev_desc_t
));
154 usb_dev_desc
[i
].target
=0xff;
155 usb_dev_desc
[i
].if_type
=IF_TYPE_USB
;
156 usb_dev_desc
[i
].dev
=i
;
157 usb_dev_desc
[i
].part_type
=PART_TYPE_UNKNOWN
;
158 usb_dev_desc
[i
].block_read
=usb_stor_read
;
161 for(i
=0;i
<USB_MAX_DEVICE
;i
++) {
162 dev
=usb_get_dev_index(i
); /* get device */
163 USB_STOR_PRINTF("i=%d\n",i
);
165 break; /* no more devices avaiable */
167 if(usb_storage_probe(dev
,0,&usb_stor
[usb_max_devs
])) { /* ok, it is a storage devices */
168 /* get info and fill it in */
170 if(usb_stor_get_info(dev
, &usb_stor
[usb_max_devs
], &usb_dev_desc
[usb_max_devs
])) {
172 printf (" Device %d: ", usb_max_devs
);
173 dev_print(&usb_dev_desc
[usb_max_devs
]);
176 } /* if get info ok */
177 } /* if storage device */
178 if(usb_max_devs
==USB_MAX_STOR_DEV
) {
179 printf("max USB Storage Device reached: %d stopping\n",usb_max_devs
);
183 usb_disable_asynch(0); /* asynch transfer allowed */
190 static int usb_stor_irq(struct usb_device
*dev
)
193 us
=(struct us_data
*)dev
->privptr
;
202 #ifdef USB_STOR_DEBUG
204 static void usb_show_srb(ccb
* pccb
)
207 printf("SRB: len %d datalen 0x%lX\n ",pccb
->cmdlen
,pccb
->datalen
);
209 printf("%02X ",pccb
->cmd
[i
]);
214 static void display_int_status(unsigned long tmp
)
216 printf("Status: %s %s %s %s %s %s %s\n",
217 (tmp
& USB_ST_ACTIVE
) ? "Active" : "",
218 (tmp
& USB_ST_STALLED
) ? "Stalled" : "",
219 (tmp
& USB_ST_BUF_ERR
) ? "Buffer Error" : "",
220 (tmp
& USB_ST_BABBLE_DET
) ? "Babble Det" : "",
221 (tmp
& USB_ST_NAK_REC
) ? "NAKed" : "",
222 (tmp
& USB_ST_CRC_ERR
) ? "CRC Error" : "",
223 (tmp
& USB_ST_BIT_ERR
) ? "Bitstuff Error" : "");
226 /***********************************************************************
227 * Data transfer routines
228 ***********************************************************************/
230 static int us_one_transfer(struct us_data
*us
, int pipe
, char *buf
, int length
)
239 /* determine the maximum packet size for these transfers */
240 max_size
= usb_maxpacket(us
->pusb_dev
, pipe
) * 16;
242 /* while we have data left to transfer */
245 /* calculate how long this will be -- maximum or a remainder */
246 this_xfer
= length
> max_size
? max_size
: length
;
249 /* setup the retry counter */
252 /* set up the transfer loop */
254 /* transfer the data */
255 USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
256 (unsigned int)buf
, this_xfer
, 11 - maxtry
);
257 result
= usb_bulk_msg(us
->pusb_dev
, pipe
, buf
,
258 this_xfer
, &partial
, USB_CNTL_TIMEOUT
*5);
259 USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
260 result
, partial
, this_xfer
);
261 if(us
->pusb_dev
->status
!=0) {
262 /* if we stall, we need to clear it before we go on */
263 #ifdef USB_STOR_DEBUG
264 display_int_status(us
->pusb_dev
->status
);
266 if (us
->pusb_dev
->status
& USB_ST_STALLED
) {
267 USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe
);
268 stat
= us
->pusb_dev
->status
;
269 usb_clear_halt(us
->pusb_dev
, pipe
);
270 us
->pusb_dev
->status
=stat
;
271 if(this_xfer
== partial
) {
272 USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us
->pusb_dev
->status
);
278 if (us
->pusb_dev
->status
& USB_ST_NAK_REC
) {
279 USB_STOR_PRINTF("Device NAKed bulk_msg\n");
282 if(this_xfer
== partial
) {
283 USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us
->pusb_dev
->status
);
286 /* if our try counter reaches 0, bail out */
287 USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us
->pusb_dev
->status
,partial
);
291 /* update to show what data was transferred */
292 this_xfer
-= partial
;
294 /* continue until this transfer is done */
295 } while ( this_xfer
);
298 /* if we get here, we're done and successful */
302 /* FIXME: this reset function doesn't really reset the port, and it
303 * should. Actually it should probably do what it's doing here, and
304 * reset the port physically
306 static int usb_stor_CB_reset(struct us_data
*us
)
308 unsigned char cmd
[12];
311 USB_STOR_PRINTF("CB_reset\n");
312 memset(cmd
, 0xFF, sizeof(cmd
));
313 cmd
[0] = SCSI_SEND_DIAG
;
315 result
= usb_control_msg(us
->pusb_dev
, usb_sndctrlpipe(us
->pusb_dev
,0),
316 US_CBI_ADSC
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
317 0, us
->ifnum
, cmd
, sizeof(cmd
), USB_CNTL_TIMEOUT
*5);
319 /* long wait for reset */
321 USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result
,us
->pusb_dev
->status
);
322 usb_clear_halt(us
->pusb_dev
, usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_in
));
323 usb_clear_halt(us
->pusb_dev
, usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_out
));
325 USB_STOR_PRINTF("CB_reset done\n");
329 /* FIXME: we also need a CBI_command which sets up the completion
330 * interrupt, and waits for it
332 int usb_stor_CB_comdat(ccb
*srb
, struct us_data
*us
)
337 unsigned long status
;
340 dir_in
=US_DIRECTION(srb
->cmd
[0]);
343 pipe
=usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_in
);
345 pipe
=usb_sndbulkpipe(us
->pusb_dev
, us
->ep_out
);
347 USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry
);
348 #ifdef USB_STOR_DEBUG
351 /* let's send the command via the control pipe */
352 result
= usb_control_msg(us
->pusb_dev
, usb_sndctrlpipe(us
->pusb_dev
,0),
353 US_CBI_ADSC
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
355 srb
->cmd
, srb
->cmdlen
, USB_CNTL_TIMEOUT
*5);
356 USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result
,us
->pusb_dev
->status
);
357 /* check the return code for the command */
359 if(us
->pusb_dev
->status
& USB_ST_STALLED
) {
360 status
=us
->pusb_dev
->status
;
361 USB_STOR_PRINTF(" stall during command found, clear pipe\n");
362 usb_clear_halt(us
->pusb_dev
, usb_sndctrlpipe(us
->pusb_dev
,0));
363 us
->pusb_dev
->status
=status
;
365 USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb
->cmd
[0],us
->pusb_dev
->status
);
368 /* transfer the data payload for this command, if one exists*/
370 USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result
,dir_in
? "IN" : "OUT",srb
->datalen
);
372 result
= us_one_transfer(us
, pipe
, srb
->pdata
,srb
->datalen
);
373 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
);
374 if(!(us
->pusb_dev
->status
& USB_ST_NAK_REC
))
376 } /* if (srb->datalen) */
386 int usb_stor_CBI_get_status(ccb
*srb
, struct us_data
*us
)
391 submit_int_msg(us
->pusb_dev
,us
->irqpipe
,
392 (void *)&us
->ip_data
,us
->irqmaxp
,us
->irqinterval
);
395 if((volatile int *)us
->ip_wanted
==0)
400 printf(" Did not get interrupt on CBI\n");
402 return USB_STOR_TRANSPORT_ERROR
;
404 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
);
405 /* UFI gives us ASC and ASCQ, like a request sense */
406 if (us
->subclass
== US_SC_UFI
) {
407 if (srb
->cmd
[0] == SCSI_REQ_SENSE
||
408 srb
->cmd
[0] == SCSI_INQUIRY
)
409 return USB_STOR_TRANSPORT_GOOD
; /* Good */
412 return USB_STOR_TRANSPORT_FAILED
;
414 return USB_STOR_TRANSPORT_GOOD
;
416 /* otherwise, we interpret the data normally */
417 switch (us
->ip_data
) {
419 return USB_STOR_TRANSPORT_GOOD
;
421 return USB_STOR_TRANSPORT_FAILED
;
423 return USB_STOR_TRANSPORT_ERROR
;
425 return USB_STOR_TRANSPORT_ERROR
;
428 #define USB_TRANSPORT_UNKNOWN_RETRY 5
429 #define USB_TRANSPORT_NOT_READY_RETRY 10
431 int usb_stor_CB_transport(ccb
*srb
, struct us_data
*us
)
439 status
=USB_STOR_TRANSPORT_GOOD
;
442 /* issue the command */
444 result
=usb_stor_CB_comdat(srb
,us
);
445 USB_STOR_PRINTF("command / Data returned %d, status %X\n",result
,us
->pusb_dev
->status
);
446 /* if this is an CBI Protocol, get IRQ */
447 if(us
->protocol
==US_PR_CBI
) {
448 status
=usb_stor_CBI_get_status(srb
,us
);
449 /* if the status is error, report it */
450 if(status
==USB_STOR_TRANSPORT_ERROR
) {
451 USB_STOR_PRINTF(" USB CBI Command Error\n");
454 srb
->sense_buf
[12]=(unsigned char)(us
->ip_data
>>8);
455 srb
->sense_buf
[13]=(unsigned char)(us
->ip_data
&0xff);
457 /* if the status is good, report it */
458 if(status
==USB_STOR_TRANSPORT_GOOD
) {
459 USB_STOR_PRINTF(" USB CBI Command Good\n");
464 /* do we have to issue an auto request? */
465 /* HERE we have to check the result */
466 if((result
<0) && !(us
->pusb_dev
->status
& USB_ST_STALLED
)) {
467 USB_STOR_PRINTF("ERROR %X\n",us
->pusb_dev
->status
);
468 us
->transport_reset(us
);
469 return USB_STOR_TRANSPORT_ERROR
;
471 if((us
->protocol
==US_PR_CBI
) &&
472 ((srb
->cmd
[0]==SCSI_REQ_SENSE
) ||
473 (srb
->cmd
[0]==SCSI_INQUIRY
))) { /* do not issue an autorequest after request sense */
474 USB_STOR_PRINTF("No auto request and good\n");
475 return USB_STOR_TRANSPORT_GOOD
;
477 /* issue an request_sense */
478 memset(&psrb
->cmd
[0],0,12);
479 psrb
->cmd
[0]=SCSI_REQ_SENSE
;
480 psrb
->cmd
[1]=srb
->lun
<<5;
483 psrb
->pdata
=&srb
->sense_buf
[0];
485 /* issue the command */
486 result
=usb_stor_CB_comdat(psrb
,us
);
487 USB_STOR_PRINTF("auto request returned %d\n",result
);
488 /* if this is an CBI Protocol, get IRQ */
489 if(us
->protocol
==US_PR_CBI
) {
490 status
=usb_stor_CBI_get_status(psrb
,us
);
492 if((result
<0)&&!(us
->pusb_dev
->status
& USB_ST_STALLED
)) {
493 USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us
->pusb_dev
->status
);
494 return USB_STOR_TRANSPORT_ERROR
;
496 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]);
497 /* Check the auto request result */
498 if((srb
->sense_buf
[2]==0) &&
499 (srb
->sense_buf
[12]==0) &&
500 (srb
->sense_buf
[13]==0)) /* ok, no sense */
501 return USB_STOR_TRANSPORT_GOOD
;
502 /* Check the auto request result */
503 switch(srb
->sense_buf
[2]) {
504 case 0x01: /* Recovered Error */
505 return USB_STOR_TRANSPORT_GOOD
;
507 case 0x02: /* Not Ready */
508 if(notready
++ > USB_TRANSPORT_NOT_READY_RETRY
) {
509 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
510 srb
->cmd
[0],srb
->sense_buf
[0],srb
->sense_buf
[2],srb
->sense_buf
[12],srb
->sense_buf
[13]);
511 return USB_STOR_TRANSPORT_FAILED
;
519 if(retry
++ > USB_TRANSPORT_UNKNOWN_RETRY
) {
520 printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
521 srb
->cmd
[0],srb
->sense_buf
[0],srb
->sense_buf
[2],srb
->sense_buf
[12],srb
->sense_buf
[13]);
522 return USB_STOR_TRANSPORT_FAILED
;
528 return USB_STOR_TRANSPORT_FAILED
;
533 static int usb_inquiry(ccb
*srb
,struct us_data
*ss
)
538 memset(&srb
->cmd
[0],0,12);
539 srb
->cmd
[0]=SCSI_INQUIRY
;
540 srb
->cmd
[1]=srb
->lun
<<5;
544 i
=ss
->transport(srb
,ss
);
545 USB_STOR_PRINTF("inquiry returns %d\n",i
);
550 printf("error in inquiry\n");
556 static int usb_request_sense(ccb
*srb
,struct us_data
*ss
)
561 memset(&srb
->cmd
[0],0,12);
562 srb
->cmd
[0]=SCSI_REQ_SENSE
;
563 srb
->cmd
[1]=srb
->lun
<<5;
566 srb
->pdata
=&srb
->sense_buf
[0];
568 ss
->transport(srb
,ss
);
569 USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",srb
->sense_buf
[2],srb
->sense_buf
[12],srb
->sense_buf
[13]);
574 static int usb_test_unit_ready(ccb
*srb
,struct us_data
*ss
)
578 memset(&srb
->cmd
[0],0,12);
579 srb
->cmd
[0]=SCSI_TST_U_RDY
;
580 srb
->cmd
[1]=srb
->lun
<<5;
583 if(ss
->transport(srb
,ss
)==USB_STOR_TRANSPORT_GOOD
)
591 static int usb_read_capacity(ccb
*srb
,struct us_data
*ss
)
594 retry
=2; /* retries */
596 memset(&srb
->cmd
[0],0,12);
597 srb
->cmd
[0]=SCSI_RD_CAPAC
;
598 srb
->cmd
[1]=srb
->lun
<<5;
601 if(ss
->transport(srb
,ss
)==USB_STOR_TRANSPORT_GOOD
) {
608 static int usb_read_10(ccb
*srb
,struct us_data
*ss
, unsigned long start
, unsigned short blocks
)
610 memset(&srb
->cmd
[0],0,12);
611 srb
->cmd
[0]=SCSI_READ10
;
612 srb
->cmd
[1]=srb
->lun
<<5;
613 srb
->cmd
[2]=((unsigned char) (start
>>24))&0xff;
614 srb
->cmd
[3]=((unsigned char) (start
>>16))&0xff;
615 srb
->cmd
[4]=((unsigned char) (start
>>8))&0xff;
616 srb
->cmd
[5]=((unsigned char) (start
))&0xff;
617 srb
->cmd
[7]=((unsigned char) (blocks
>>8))&0xff;
618 srb
->cmd
[8]=(unsigned char) blocks
& 0xff;
620 USB_STOR_PRINTF("read10: start %lx blocks %x\n",start
,blocks
);
621 return ss
->transport(srb
,ss
);
625 #define USB_MAX_READ_BLK 20
627 unsigned long usb_stor_read(int device
, unsigned long blknr
, unsigned long blkcnt
, unsigned long *buffer
)
629 unsigned long start
,blks
, buf_addr
;
630 unsigned short smallblks
;
631 struct usb_device
*dev
;
637 USB_STOR_PRINTF("\nusb_read: dev %d \n",device
);
639 for(i
=0;i
<USB_MAX_DEVICE
;i
++) {
640 dev
=usb_get_dev_index(i
);
644 if(dev
->devnum
==usb_dev_desc
[device
].target
)
648 usb_disable_asynch(1); /* asynch transfer not allowed */
649 srb
->lun
=usb_dev_desc
[device
].lun
;
650 buf_addr
=(unsigned long)buffer
;
653 if(usb_test_unit_ready(srb
,(struct us_data
*)dev
->privptr
)) {
654 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
655 srb
->sense_buf
[2],srb
->sense_buf
[12],srb
->sense_buf
[13]);
658 USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device
,start
,blks
, buf_addr
);
661 srb
->pdata
=(unsigned char *)buf_addr
;
662 if(blks
>USB_MAX_READ_BLK
) {
663 smallblks
=USB_MAX_READ_BLK
;
666 smallblks
=(unsigned short) blks
;
669 if(smallblks
==USB_MAX_READ_BLK
)
671 srb
->datalen
=usb_dev_desc
[device
].blksz
* smallblks
;
672 srb
->pdata
=(unsigned char *)buf_addr
;
673 if(usb_read_10(srb
,(struct us_data
*)dev
->privptr
, start
, smallblks
)) {
674 USB_STOR_PRINTF("Read ERROR\n");
675 usb_request_sense(srb
,(struct us_data
*)dev
->privptr
);
683 buf_addr
+=srb
->datalen
;
685 USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start
,smallblks
,buf_addr
);
686 usb_disable_asynch(0); /* asynch transfer allowed */
687 if(blkcnt
>=USB_MAX_READ_BLK
)
693 /* Probe to see if a new device is actually a Storage device */
694 int usb_storage_probe(struct usb_device
*dev
, unsigned int ifnum
,struct us_data
*ss
)
696 struct usb_interface_descriptor
*iface
;
698 unsigned int flags
= 0;
704 memset(ss
, 0, sizeof(struct us_data
));
706 /* let's examine the device now */
707 iface
= &dev
->config
.if_desc
[ifnum
];
710 /* this is the place to patch some storage devices */
711 USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev
->descriptor
.idVendor
,dev
->descriptor
.idProduct
);
712 if ((dev
->descriptor
.idVendor
) == 0x066b && (dev
->descriptor
.idProduct
) == 0x0103) {
713 USB_STOR_PRINTF("patched for E-USB\n");
715 subclass
= US_SC_UFI
; /* an assumption */
719 if (dev
->descriptor
.bDeviceClass
!= 0 ||
720 iface
->bInterfaceClass
!= USB_CLASS_MASS_STORAGE
||
721 iface
->bInterfaceSubClass
< US_SC_MIN
||
722 iface
->bInterfaceSubClass
> US_SC_MAX
) {
723 /* if it's not a mass storage, we go no further */
727 /* At this point, we know we've got a live one */
728 USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
730 /* Initialize the us_data structure with some useful info */
734 ss
->attention_done
= 0;
736 /* If the device has subclass and protocol, then use that. Otherwise,
737 * take data from the specific interface.
740 ss
->subclass
= subclass
;
741 ss
->protocol
= protocol
;
743 ss
->subclass
= iface
->bInterfaceSubClass
;
744 ss
->protocol
= iface
->bInterfaceProtocol
;
747 /* set the handler pointers based on the protocol */
748 USB_STOR_PRINTF("Transport: ");
749 switch (ss
->protocol
) {
751 USB_STOR_PRINTF("Control/Bulk\n");
752 ss
->transport
= usb_stor_CB_transport
;
753 ss
->transport_reset
= usb_stor_CB_reset
;
757 USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
758 ss
->transport
= usb_stor_CB_transport
;
759 ss
->transport_reset
= usb_stor_CB_reset
;
762 printf("USB Starage Transport unknown / not yet implemented\n");
768 * We are expecting a minimum of 2 endpoints - in and out (bulk).
769 * An optional interrupt is OK (necessary for CBI protocol).
770 * We will ignore any others.
772 for (i
= 0; i
< iface
->bNumEndpoints
; i
++) {
773 /* is it an BULK endpoint? */
774 if ((iface
->ep_desc
[i
].bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
775 == USB_ENDPOINT_XFER_BULK
) {
776 if (iface
->ep_desc
[i
].bEndpointAddress
& USB_DIR_IN
)
777 ss
->ep_in
= iface
->ep_desc
[i
].bEndpointAddress
&
778 USB_ENDPOINT_NUMBER_MASK
;
780 ss
->ep_out
= iface
->ep_desc
[i
].bEndpointAddress
&
781 USB_ENDPOINT_NUMBER_MASK
;
784 /* is it an interrupt endpoint? */
785 if ((iface
->ep_desc
[i
].bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
786 == USB_ENDPOINT_XFER_INT
) {
787 ss
->ep_int
= iface
->ep_desc
[i
].bEndpointAddress
&
788 USB_ENDPOINT_NUMBER_MASK
;
789 ss
->irqinterval
= iface
->ep_desc
[i
].bInterval
;
792 USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
793 ss
->ep_in
, ss
->ep_out
, ss
->ep_int
);
795 /* Do some basic sanity checks, and bail if we find a problem */
796 if (usb_set_interface(dev
, iface
->bInterfaceNumber
, 0) ||
797 !ss
->ep_in
|| !ss
->ep_out
||
798 (ss
->protocol
== US_PR_CBI
&& ss
->ep_int
== 0)) {
799 USB_STOR_PRINTF("Problems with device\n");
802 /* set class specific stuff */
803 /* We only handle certain protocols. Currently, this is
806 if (ss
->subclass
!= US_SC_UFI
) {
807 printf("Sorry, protocol %d not yet supported.\n",ss
->subclass
);
810 if(ss
->ep_int
) /* we had found an interrupt endpoint, prepare irq pipe */
812 /* set up the IRQ pipe and handler */
814 ss
->irqinterval
= (ss
->irqinterval
> 0) ? ss
->irqinterval
: 255;
815 ss
->irqpipe
= usb_rcvintpipe(ss
->pusb_dev
, ss
->ep_int
);
816 ss
->irqmaxp
= usb_maxpacket(dev
, ss
->irqpipe
);
817 dev
->irq_handle
=usb_stor_irq
;
818 dev
->privptr
=(void *)ss
;
823 int usb_stor_get_info(struct usb_device
*dev
,struct us_data
*ss
,block_dev_desc_t
*dev_desc
)
825 unsigned char perq
,modi
;
826 unsigned long cap
[2];
827 unsigned long *capacity
,*blksz
;
830 ss
->transport_reset(ss
);
831 pccb
->pdata
=usb_stor_buf
;
833 dev_desc
->target
=dev
->devnum
;
834 pccb
->lun
=dev_desc
->lun
;
835 USB_STOR_PRINTF(" address %d\n",dev_desc
->target
);
837 if(usb_inquiry(pccb
,ss
))
839 perq
=usb_stor_buf
[0];
840 modi
=usb_stor_buf
[1];
841 if((perq
& 0x1f)==0x1f) {
842 return 0; /* skip unknown devices */
844 if((modi
&0x80)==0x80) {/* drive is removable */
845 dev_desc
->removable
=1;
847 memcpy(&dev_desc
->vendor
[0], &usb_stor_buf
[8], 8);
848 memcpy(&dev_desc
->product
[0], &usb_stor_buf
[16], 16);
849 memcpy(&dev_desc
->revision
[0], &usb_stor_buf
[32], 4);
850 dev_desc
->vendor
[8]=0;
851 dev_desc
->product
[16]=0;
852 dev_desc
->revision
[4]=0;
853 USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf
[2],usb_stor_buf
[3]);
854 if(usb_test_unit_ready(pccb
,ss
)) {
855 printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",pccb
->sense_buf
[2],pccb
->sense_buf
[12],pccb
->sense_buf
[13]);
856 if(dev_desc
->removable
==1) {
863 pccb
->pdata
=(unsigned char *)&cap
[0];
864 memset(pccb
->pdata
,0,8);
865 if(usb_read_capacity(pccb
,ss
)!=0) {
866 printf("READ_CAP ERROR\n");
870 USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap
[0],cap
[1]);
872 if(cap
[0]>(0x200000 * 10)) /* greater than 10 GByte */
878 USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity
,*blksz
);
879 dev_desc
->lba
=*capacity
;
880 dev_desc
->blksz
=*blksz
;
882 USB_STOR_PRINTF(" address %d\n",dev_desc
->target
);
883 USB_STOR_PRINTF("partype: %d\n",dev_desc
->part_type
);
887 USB_STOR_PRINTF("partype: %d\n",dev_desc
->part_type
);
892 #endif /* CONFIG_USB_STORAGE */