1 /* Driver for USB Mass Storage compliant devices
3 * $Id: usb.c,v 1.75 2002/04/22 03:39:43 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * Developed with the assistance of:
9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
10 * (c) 2003 Alan Stern (stern@rowland.harvard.edu)
13 * (c) 1999 Michael Gee (michael@linuxspecific.com)
15 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
16 * (c) 2000 Yggdrasil Computing, Inc.
18 * This driver is based on the 'USB Mass Storage Class' document. This
19 * describes in detail the protocol used to communicate with such
20 * devices. Clearly, the designers had SCSI and ATAPI commands in
21 * mind when they created this document. The commands are all very
22 * similar to commands in the SCSI-II and ATAPI specifications.
24 * It is important to note that in a number of cases this class
25 * exhibits class-specific exemptions from the USB specification.
26 * Notably the usage of NAK, STALL and ACK differs from the norm, in
27 * that they are used to communicate wait, failed and OK on commands.
29 * Also, for certain devices, the interrupt endpoint is used to convey
30 * status of a command.
32 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
33 * information about this driver.
35 * This program is free software; you can redistribute it and/or modify it
36 * under the terms of the GNU General Public License as published by the
37 * Free Software Foundation; either version 2, or (at your option) any
40 * This program is distributed in the hope that it will be useful, but
41 * WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 * General Public License for more details.
45 * You should have received a copy of the GNU General Public License along
46 * with this program; if not, write to the Free Software Foundation, Inc.,
47 * 675 Mass Ave, Cambridge, MA 02139, USA.
50 #include <linux/config.h>
51 #include <linux/sched.h>
52 #include <linux/errno.h>
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_device.h>
60 #include "transport.h"
63 #include "initializers.h"
65 #ifdef CONFIG_USB_STORAGE_HP8200e
66 #include "shuttle_usbat.h"
68 #ifdef CONFIG_USB_STORAGE_SDDR09
71 #ifdef CONFIG_USB_STORAGE_SDDR55
74 #ifdef CONFIG_USB_STORAGE_DPCM
77 #ifdef CONFIG_USB_STORAGE_FREECOM
80 #ifdef CONFIG_USB_STORAGE_ISD200
83 #ifdef CONFIG_USB_STORAGE_DATAFAB
86 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
91 #include <linux/module.h>
92 #include <linux/init.h>
93 #include <linux/slab.h>
95 /* Some informational data */
96 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
97 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
98 MODULE_LICENSE("GPL");
100 static int storage_probe(struct usb_interface
*iface
,
101 const struct usb_device_id
*id
);
103 static void storage_disconnect(struct usb_interface
*iface
);
105 /* The entries in this table, except for final ones here
106 * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
107 * line for line with the entries of us_unsuaul_dev_list[].
110 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
111 vendorName, productName,useProtocol, useTransport, \
112 initFunction, flags) \
113 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
115 static struct usb_device_id storage_usb_ids
[] = {
117 # include "unusual_devs.h"
119 /* Control/Bulk transport for all SubClass values */
120 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_RBC
, US_PR_CB
) },
121 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8020
, US_PR_CB
) },
122 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_QIC
, US_PR_CB
) },
123 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_UFI
, US_PR_CB
) },
124 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8070
, US_PR_CB
) },
125 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_SCSI
, US_PR_CB
) },
127 /* Control/Bulk/Interrupt transport for all SubClass values */
128 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_RBC
, US_PR_CBI
) },
129 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8020
, US_PR_CBI
) },
130 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_QIC
, US_PR_CBI
) },
131 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_UFI
, US_PR_CBI
) },
132 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8070
, US_PR_CBI
) },
133 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_SCSI
, US_PR_CBI
) },
135 /* Bulk-only transport for all SubClass values */
136 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_RBC
, US_PR_BULK
) },
137 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8020
, US_PR_BULK
) },
138 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_QIC
, US_PR_BULK
) },
139 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_UFI
, US_PR_BULK
) },
140 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8070
, US_PR_BULK
) },
141 #if !defined(CONFIG_BLK_DEV_UB) && !defined(CONFIG_BLK_DEV_UB_MODULE)
142 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_SCSI
, US_PR_BULK
) },
145 /* Terminating entry */
149 MODULE_DEVICE_TABLE (usb
, storage_usb_ids
);
151 /* This is the list of devices we recognize, along with their flag data */
153 /* The vendor name should be kept at eight characters or less, and
154 * the product name should be kept at 16 characters or less. If a device
155 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
156 * normally generated by a device thorugh the INQUIRY response will be
157 * taken from this list, and this is the reason for the above size
158 * restriction. However, if the flag is not present, then you
159 * are free to use as many characters as you like.
163 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
164 vendor_name, product_name, use_protocol, use_transport, \
165 init_function, Flags) \
167 .vendorName = vendor_name, \
168 .productName = product_name, \
169 .useProtocol = use_protocol, \
170 .useTransport = use_transport, \
171 .initFunction = init_function, \
175 static struct us_unusual_dev us_unusual_dev_list
[] = {
176 # include "unusual_devs.h"
178 /* Control/Bulk transport for all SubClass values */
179 { .useProtocol
= US_SC_RBC
,
180 .useTransport
= US_PR_CB
},
181 { .useProtocol
= US_SC_8020
,
182 .useTransport
= US_PR_CB
},
183 { .useProtocol
= US_SC_QIC
,
184 .useTransport
= US_PR_CB
},
185 { .useProtocol
= US_SC_UFI
,
186 .useTransport
= US_PR_CB
},
187 { .useProtocol
= US_SC_8070
,
188 .useTransport
= US_PR_CB
},
189 { .useProtocol
= US_SC_SCSI
,
190 .useTransport
= US_PR_CB
},
192 /* Control/Bulk/Interrupt transport for all SubClass values */
193 { .useProtocol
= US_SC_RBC
,
194 .useTransport
= US_PR_CBI
},
195 { .useProtocol
= US_SC_8020
,
196 .useTransport
= US_PR_CBI
},
197 { .useProtocol
= US_SC_QIC
,
198 .useTransport
= US_PR_CBI
},
199 { .useProtocol
= US_SC_UFI
,
200 .useTransport
= US_PR_CBI
},
201 { .useProtocol
= US_SC_8070
,
202 .useTransport
= US_PR_CBI
},
203 { .useProtocol
= US_SC_SCSI
,
204 .useTransport
= US_PR_CBI
},
206 /* Bulk-only transport for all SubClass values */
207 { .useProtocol
= US_SC_RBC
,
208 .useTransport
= US_PR_BULK
},
209 { .useProtocol
= US_SC_8020
,
210 .useTransport
= US_PR_BULK
},
211 { .useProtocol
= US_SC_QIC
,
212 .useTransport
= US_PR_BULK
},
213 { .useProtocol
= US_SC_UFI
,
214 .useTransport
= US_PR_BULK
},
215 { .useProtocol
= US_SC_8070
,
216 .useTransport
= US_PR_BULK
},
217 #if !defined(CONFIG_BLK_DEV_UB) && !defined(CONFIG_BLK_DEV_UB_MODULE)
218 { .useProtocol
= US_SC_SCSI
,
219 .useTransport
= US_PR_BULK
},
222 /* Terminating entry */
226 #if (defined CONFIG_ARCH_IA241_32128)||(defined CONFIG_ARCH_IA241_16128)// add by Victor Yu. 05-22-2007
227 #define CONFIG_ARCH_IA241
230 struct usb_driver usb_storage_driver
= {
231 .owner
= THIS_MODULE
,
232 .name
= "usb-storage",
233 .probe
= storage_probe
,
234 .disconnect
= storage_disconnect
,
235 .id_table
= storage_usb_ids
,
239 * fill_inquiry_response takes an unsigned char array (which must
240 * be at least 36 characters) and populates the vendor name,
241 * product name, and revision fields. Then the array is copied
242 * into the SCSI command's response buffer (oddly enough
243 * called request_buffer). data_len contains the length of the
244 * data array, which again must be at least 36.
247 void fill_inquiry_response(struct us_data
*us
, unsigned char *data
,
248 unsigned int data_len
)
250 if (data_len
<36) // You lose.
253 if(data
[0]&0x20) { /* USB device currently not connected. Return
254 peripheral qualifier 001b ("...however, the
255 physical device is not currently connected
256 to this logical unit") and leave vendor and
257 product identification empty. ("If the target
258 does store some of the INQUIRY data on the
259 device, it may return zeros or ASCII spaces
260 (20h) in those fields until the data is
261 available from the device."). */
264 memcpy(data
+8, us
->unusual_dev
->vendorName
,
265 strlen(us
->unusual_dev
->vendorName
) > 8 ? 8 :
266 strlen(us
->unusual_dev
->vendorName
));
267 memcpy(data
+16, us
->unusual_dev
->productName
,
268 strlen(us
->unusual_dev
->productName
) > 16 ? 16 :
269 strlen(us
->unusual_dev
->productName
));
270 data
[32] = 0x30 + ((us
->pusb_dev
->descriptor
.bcdDevice
>>12) & 0x0F);
271 data
[33] = 0x30 + ((us
->pusb_dev
->descriptor
.bcdDevice
>>8) & 0x0F);
272 data
[34] = 0x30 + ((us
->pusb_dev
->descriptor
.bcdDevice
>>4) & 0x0F);
273 data
[35] = 0x30 + ((us
->pusb_dev
->descriptor
.bcdDevice
) & 0x0F);
276 usb_stor_set_xfer_buf(data
, data_len
, us
->srb
);
279 static int usb_stor_control_thread(void * __us
)
281 struct us_data
*us
= (struct us_data
*)__us
;
282 struct Scsi_Host
*host
= us
->host
;
287 * This thread doesn't need any user-level access,
288 * so get rid of all our resources.
290 daemonize("usb-storage");
292 current
->flags
|= PF_NOFREEZE
;
296 /* signal that we've started the thread */
297 complete(&(us
->notify
));
300 US_DEBUGP("*** thread sleeping.\n");
301 if(down_interruptible(&us
->sema
))
304 US_DEBUGP("*** thread awakened.\n");
306 /* lock the device pointers */
307 down(&(us
->dev_semaphore
));
309 /* if us->srb is NULL, we are being asked to exit */
310 if (us
->srb
== NULL
) {
311 US_DEBUGP("-- exit command received\n");
312 up(&(us
->dev_semaphore
));
316 /* lock access to the state */
319 /* has the command been aborted *already* ? */
320 if (us
->sm_state
== US_STATE_ABORTING
) {
321 us
->srb
->result
= DID_ABORT
<< 16;
325 /* don't do anything if we are disconnecting */
326 if (test_bit(US_FLIDX_DISCONNECTING
, &us
->flags
)) {
327 US_DEBUGP("No command during disconnect\n");
328 goto SkipForDisconnect
;
331 /* set the state and release the lock */
332 us
->sm_state
= US_STATE_RUNNING
;
335 /* reject the command if the direction indicator
338 if (us
->srb
->sc_data_direction
== DMA_BIDIRECTIONAL
) {
339 US_DEBUGP("UNKNOWN data direction\n");
340 us
->srb
->result
= DID_ERROR
<< 16;
343 /* reject if target != 0 or if LUN is higher than
344 * the maximum known LUN
346 else if (us
->srb
->device
->id
&&
347 !(us
->flags
& US_FL_SCM_MULT_TARG
)) {
348 US_DEBUGP("Bad target number (%d:%d)\n",
349 us
->srb
->device
->id
, us
->srb
->device
->lun
);
350 us
->srb
->result
= DID_BAD_TARGET
<< 16;
353 else if (us
->srb
->device
->lun
> us
->max_lun
) {
354 US_DEBUGP("Bad LUN (%d:%d)\n",
355 us
->srb
->device
->id
, us
->srb
->device
->lun
);
356 us
->srb
->result
= DID_BAD_TARGET
<< 16;
359 /* Handle those devices which need us to fake
360 * their inquiry data */
361 else if ((us
->srb
->cmnd
[0] == INQUIRY
) &&
362 (us
->flags
& US_FL_FIX_INQUIRY
)) {
363 unsigned char data_ptr
[36] = {
364 0x00, 0x80, 0x02, 0x02,
365 0x1F, 0x00, 0x00, 0x00};
367 US_DEBUGP("Faking INQUIRY command\n");
368 fill_inquiry_response(us
, data_ptr
, 36);
369 us
->srb
->result
= SAM_STAT_GOOD
;
372 /* we've got a command, let's do it! */
374 US_DEBUG(usb_stor_show_command(us
->srb
));
375 us
->proto_handler(us
->srb
, us
);
378 /* lock access to the state */
381 /* indicate that the command is done */
382 if (us
->srb
->result
!= DID_ABORT
<< 16) {
383 US_DEBUGP("scsi cmd done, result=0x%x\n",
385 us
->srb
->scsi_done(us
->srb
);
388 US_DEBUGP("scsi command aborted\n");
391 /* If an abort request was received we need to signal that
392 * the abort has finished. The proper test for this is
393 * sm_state == US_STATE_ABORTING, not srb->result == DID_ABORT,
394 * because an abort request might be received after all the
395 * USB processing was complete. */
396 if (us
->sm_state
== US_STATE_ABORTING
)
397 complete(&(us
->notify
));
399 /* empty the queue, reset the state, and release the lock */
402 us
->sm_state
= US_STATE_IDLE
;
405 /* unlock the device pointers */
406 up(&(us
->dev_semaphore
));
409 /* notify the exit routine that we're actually exiting now
411 * complete()/wait_for_completion() is similar to up()/down(),
412 * except that complete() is safe in the case where the structure
413 * is getting deleted in a parallel mode of execution (i.e. just
414 * after the down() -- that's necessary for the thread-shutdown
417 * complete_and_exit() goes even further than this -- it is safe in
418 * the case that the thread of the caller is going away (not just
419 * the structure) -- this is necessary for the module-remove case.
420 * This is important in preemption kernels, which transfer the flow
421 * of execution immediately upon a complete().
423 complete_and_exit(&(us
->notify
), 0);
426 /***********************************************************************
427 * Device probing and disconnecting
428 ***********************************************************************/
430 /* Associate our private data with the USB device */
431 static int associate_dev(struct us_data
*us
, struct usb_interface
*intf
)
433 US_DEBUGP("-- %s\n", __FUNCTION__
);
435 /* Fill in the device-related fields */
436 us
->pusb_dev
= interface_to_usbdev(intf
);
437 us
->pusb_intf
= intf
;
438 us
->ifnum
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
439 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
440 us
->pusb_dev
->descriptor
.idVendor
,
441 us
->pusb_dev
->descriptor
.idProduct
,
442 us
->pusb_dev
->descriptor
.bcdDevice
);
443 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
444 intf
->cur_altsetting
->desc
.bInterfaceSubClass
,
445 intf
->cur_altsetting
->desc
.bInterfaceProtocol
);
447 /* Store our private data in the interface */
448 usb_set_intfdata(intf
, us
);
450 /* Allocate the device-related DMA-mapped buffers */
451 us
->cr
= usb_buffer_alloc(us
->pusb_dev
, sizeof(*us
->cr
),
452 GFP_KERNEL
, &us
->cr_dma
);
454 US_DEBUGP("usb_ctrlrequest allocation failed\n");
458 us
->iobuf
= usb_buffer_alloc(us
->pusb_dev
, US_IOBUF_SIZE
,
459 GFP_KERNEL
, &us
->iobuf_dma
);
461 US_DEBUGP("I/O buffer allocation failed\n");
467 /* Get the unusual_devs entries and the string descriptors */
468 #if 0 // mask by Victor Yu. 06-13-2007
469 static void get_device_info(struct us_data
*us
, int id_index
)
470 #else // add by Victor Yu. 06-13-2007
471 static int get_device_info(struct us_data
*us
, int id_index
)
474 struct usb_device
*dev
= us
->pusb_dev
;
475 struct usb_interface_descriptor
*idesc
=
476 &us
->pusb_intf
->cur_altsetting
->desc
;
477 struct us_unusual_dev
*unusual_dev
= &us_unusual_dev_list
[id_index
];
478 struct usb_device_id
*id
= &storage_usb_ids
[id_index
];
480 /* Store the entries */
481 us
->unusual_dev
= unusual_dev
;
482 us
->subclass
= (unusual_dev
->useProtocol
== US_SC_DEVICE
) ?
483 idesc
->bInterfaceSubClass
:
484 unusual_dev
->useProtocol
;
485 us
->protocol
= (unusual_dev
->useTransport
== US_PR_DEVICE
) ?
486 idesc
->bInterfaceProtocol
:
487 unusual_dev
->useTransport
;
488 us
->flags
= unusual_dev
->flags
;
490 #if 1 // add by Victor Yu. 06-13-2007
491 if (us
->flags
& US_FL_IGNORE_DEVICE
) {
492 printk(KERN_INFO USB_STORAGE
"device ignored\n");
497 /* Log a message if a non-generic unusual_dev entry contains an
498 * unnecessary subclass or protocol override. This may stimulate
499 * reports from users that will help us remove unneeded entries
500 * from the unusual_devs.h table.
502 if (id
->idVendor
|| id
->idProduct
) {
503 static char *msgs
[3] = {
504 "an unneeded SubClass entry",
505 "an unneeded Protocol entry",
506 "unneeded SubClass and Protocol entries"};
507 struct usb_device_descriptor
*ddesc
= &dev
->descriptor
;
510 if (unusual_dev
->useProtocol
!= US_SC_DEVICE
&&
511 us
->subclass
== idesc
->bInterfaceSubClass
)
513 if (unusual_dev
->useTransport
!= US_PR_DEVICE
&&
514 us
->protocol
== idesc
->bInterfaceProtocol
)
516 if (msg
>= 0 && !(unusual_dev
->flags
& US_FL_NEED_OVERRIDE
))
517 printk(KERN_NOTICE USB_STORAGE
"This device "
518 "(%04x,%04x,%04x S %02x P %02x)"
519 " has %s in unusual_devs.h\n"
520 " Please send a copy of this message to "
521 "<linux-usb-devel@lists.sourceforge.net>\n",
522 ddesc
->idVendor
, ddesc
->idProduct
,
524 idesc
->bInterfaceSubClass
,
525 idesc
->bInterfaceProtocol
,
529 /* Read the device's string descriptors */
530 if (dev
->descriptor
.iManufacturer
)
531 usb_string(dev
, dev
->descriptor
.iManufacturer
,
532 us
->vendor
, sizeof(us
->vendor
));
533 if (dev
->descriptor
.iProduct
)
534 usb_string(dev
, dev
->descriptor
.iProduct
,
535 us
->product
, sizeof(us
->product
));
536 if (dev
->descriptor
.iSerialNumber
)
537 usb_string(dev
, dev
->descriptor
.iSerialNumber
,
538 us
->serial
, sizeof(us
->serial
));
540 /* Use the unusual_dev strings if the device didn't provide them */
541 if (strlen(us
->vendor
) == 0) {
542 if (unusual_dev
->vendorName
)
543 strlcpy(us
->vendor
, unusual_dev
->vendorName
,
546 strcpy(us
->vendor
, "Unknown");
548 if (strlen(us
->product
) == 0) {
549 if (unusual_dev
->productName
)
550 strlcpy(us
->product
, unusual_dev
->productName
,
551 sizeof(us
->product
));
553 strcpy(us
->product
, "Unknown");
555 if (strlen(us
->serial
) == 0)
556 strcpy(us
->serial
, "None");
558 US_DEBUGP("Vendor: %s, Product: %s\n", us
->vendor
, us
->product
);
559 #if 1 // add by Victor Yu. 06-13-2007
564 /* Get the transport settings */
565 static int get_transport(struct us_data
*us
)
567 switch (us
->protocol
) {
569 us
->transport_name
= "Control/Bulk";
570 us
->transport
= usb_stor_CB_transport
;
571 us
->transport_reset
= usb_stor_CB_reset
;
576 us
->transport_name
= "Control/Bulk/Interrupt";
577 us
->transport
= usb_stor_CBI_transport
;
578 us
->transport_reset
= usb_stor_CB_reset
;
583 us
->transport_name
= "Bulk";
584 us
->transport
= usb_stor_Bulk_transport
;
585 us
->transport_reset
= usb_stor_Bulk_reset
;
588 #ifdef CONFIG_USB_STORAGE_HP8200e
589 case US_PR_SCM_ATAPI
:
590 us
->transport_name
= "SCM/ATAPI";
591 us
->transport
= hp8200e_transport
;
592 us
->transport_reset
= usb_stor_CB_reset
;
597 #ifdef CONFIG_USB_STORAGE_SDDR09
598 case US_PR_EUSB_SDDR09
:
599 us
->transport_name
= "EUSB/SDDR09";
600 us
->transport
= sddr09_transport
;
601 us
->transport_reset
= usb_stor_CB_reset
;
606 #ifdef CONFIG_USB_STORAGE_SDDR55
608 us
->transport_name
= "SDDR55";
609 us
->transport
= sddr55_transport
;
610 us
->transport_reset
= sddr55_reset
;
615 #ifdef CONFIG_USB_STORAGE_DPCM
617 us
->transport_name
= "Control/Bulk-EUSB/SDDR09";
618 us
->transport
= dpcm_transport
;
619 us
->transport_reset
= usb_stor_CB_reset
;
624 #ifdef CONFIG_USB_STORAGE_FREECOM
626 us
->transport_name
= "Freecom";
627 us
->transport
= freecom_transport
;
628 us
->transport_reset
= usb_stor_freecom_reset
;
633 #ifdef CONFIG_USB_STORAGE_DATAFAB
635 us
->transport_name
= "Datafab Bulk-Only";
636 us
->transport
= datafab_transport
;
637 us
->transport_reset
= usb_stor_Bulk_reset
;
642 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
644 us
->transport_name
= "Lexar Jumpshot Control/Bulk";
645 us
->transport
= jumpshot_transport
;
646 us
->transport_reset
= usb_stor_Bulk_reset
;
654 US_DEBUGP("Transport: %s\n", us
->transport_name
);
656 /* fix for single-lun devices */
657 if (us
->flags
& US_FL_SINGLE_LUN
)
662 /* Get the protocol settings */
663 static int get_protocol(struct us_data
*us
)
665 switch (us
->subclass
) {
667 us
->protocol_name
= "Reduced Block Commands (RBC)";
668 us
->proto_handler
= usb_stor_transparent_scsi_command
;
672 us
->protocol_name
= "8020i";
673 us
->proto_handler
= usb_stor_ATAPI_command
;
678 us
->protocol_name
= "QIC-157";
679 us
->proto_handler
= usb_stor_qic157_command
;
684 us
->protocol_name
= "8070i";
685 us
->proto_handler
= usb_stor_ATAPI_command
;
690 us
->protocol_name
= "Transparent SCSI";
691 us
->proto_handler
= usb_stor_transparent_scsi_command
;
695 us
->protocol_name
= "Uniform Floppy Interface (UFI)";
696 us
->proto_handler
= usb_stor_ufi_command
;
699 #ifdef CONFIG_USB_STORAGE_ISD200
701 us
->protocol_name
= "ISD200 ATA/ATAPI";
702 us
->proto_handler
= isd200_ata_command
;
709 US_DEBUGP("Protocol: %s\n", us
->protocol_name
);
713 /* Get the pipe settings */
714 static int get_pipes(struct us_data
*us
)
716 struct usb_host_interface
*altsetting
=
717 us
->pusb_intf
->cur_altsetting
;
719 struct usb_endpoint_descriptor
*ep
;
720 struct usb_endpoint_descriptor
*ep_in
= NULL
;
721 struct usb_endpoint_descriptor
*ep_out
= NULL
;
722 struct usb_endpoint_descriptor
*ep_int
= NULL
;
725 * Find the endpoints we need.
726 * We are expecting a minimum of 2 endpoints - in and out (bulk).
727 * An optional interrupt is OK (necessary for CBI protocol).
728 * We will ignore any others.
730 for (i
= 0; i
< altsetting
->desc
.bNumEndpoints
; i
++) {
731 ep
= &altsetting
->endpoint
[i
].desc
;
733 /* Is it a BULK endpoint? */
734 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
735 == USB_ENDPOINT_XFER_BULK
) {
736 /* BULK in or out? */
737 if (ep
->bEndpointAddress
& USB_DIR_IN
)
743 /* Is it an interrupt endpoint? */
744 else if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
745 == USB_ENDPOINT_XFER_INT
) {
750 if (!ep_in
|| !ep_out
|| (us
->protocol
== US_PR_CBI
&& !ep_int
)) {
751 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
755 /* Calculate and store the pipe values */
756 us
->send_ctrl_pipe
= usb_sndctrlpipe(us
->pusb_dev
, 0);
757 us
->recv_ctrl_pipe
= usb_rcvctrlpipe(us
->pusb_dev
, 0);
758 us
->send_bulk_pipe
= usb_sndbulkpipe(us
->pusb_dev
,
759 ep_out
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
760 us
->recv_bulk_pipe
= usb_rcvbulkpipe(us
->pusb_dev
,
761 ep_in
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
763 us
->recv_intr_pipe
= usb_rcvintpipe(us
->pusb_dev
,
764 ep_int
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
765 us
->ep_bInterval
= ep_int
->bInterval
;
770 /* Initialize all the dynamic resources we need */
771 static int usb_stor_acquire_resources(struct us_data
*us
)
775 us
->current_urb
= usb_alloc_urb(0, GFP_KERNEL
);
776 if (!us
->current_urb
) {
777 US_DEBUGP("URB allocation failed\n");
781 /* Lock the device while we carry out the next two operations */
782 down(&us
->dev_semaphore
);
784 /* For bulk-only devices, determine the max LUN value */
785 if (us
->protocol
== US_PR_BULK
) {
786 p
= usb_stor_Bulk_max_lun(us
);
788 up(&us
->dev_semaphore
);
794 /* Just before we start our control thread, initialize
795 * the device if it needs initialization */
796 if (us
->unusual_dev
->initFunction
)
797 us
->unusual_dev
->initFunction(us
);
799 up(&us
->dev_semaphore
);
802 * Since this is a new device, we need to register a SCSI
803 * host definition with the higher SCSI layers.
805 us
->host
= scsi_host_alloc(&usb_stor_host_template
, sizeof(us
));
807 printk(KERN_WARNING USB_STORAGE
808 "Unable to allocate the scsi host\n");
812 /* Set the hostdata to prepare for scanning */
813 us
->host
->hostdata
[0] = (unsigned long) us
;
815 /* Start up our control thread */
816 us
->sm_state
= US_STATE_IDLE
;
817 p
= kernel_thread(usb_stor_control_thread
, us
, CLONE_VM
);
819 printk(KERN_WARNING USB_STORAGE
820 "Unable to start control thread\n");
825 /* Wait for the thread to start */
826 wait_for_completion(&(us
->notify
));
831 /* Release all our dynamic resources */
832 void usb_stor_release_resources(struct us_data
*us
)
834 US_DEBUGP("-- %s\n", __FUNCTION__
);
836 /* Kill the control thread. The SCSI host must already have been
837 * removed so it won't try to queue any more commands.
841 /* Wait for the thread to be idle */
842 down(&us
->dev_semaphore
);
843 US_DEBUGP("-- sending exit command to thread\n");
844 BUG_ON(us
->sm_state
!= US_STATE_IDLE
);
846 /* If the SCSI midlayer queued a final command just before
847 * scsi_remove_host() was called, us->srb might not be
848 * NULL. We can overwrite it safely, because the midlayer
849 * will not wait for the command to finish. Also the
850 * control thread will already have been awakened.
851 * That's okay, an extra up() on us->sema won't hurt.
853 * Enqueue the command, wake up the thread, and wait for
854 * notification that it has exited.
858 scsi_unlock(us
->host
);
859 up(&us
->dev_semaphore
);
862 wait_for_completion(&us
->notify
);
865 /* Call the destructor routine, if it exists */
866 if (us
->extra_destructor
) {
867 US_DEBUGP("-- calling extra_destructor()\n");
868 us
->extra_destructor(us
->extra
);
871 /* Finish the host removal sequence */
873 scsi_host_put(us
->host
);
875 /* Free the extra data and the URB */
879 usb_free_urb(us
->current_urb
);
883 /* Dissociate from the USB device */
884 static void dissociate_dev(struct us_data
*us
)
886 US_DEBUGP("-- %s\n", __FUNCTION__
);
888 /* Free the device-related DMA-mapped buffers */
890 usb_buffer_free(us
->pusb_dev
, sizeof(*us
->cr
), us
->cr
,
893 usb_buffer_free(us
->pusb_dev
, US_IOBUF_SIZE
, us
->iobuf
,
896 /* Remove our private data from the interface */
897 usb_set_intfdata(us
->pusb_intf
, NULL
);
899 /* Free the structure itself */
903 #include <linux/kmod.h>
905 #if ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 ) || ( defined CONFIG_ARCH_W341 )
906 #define MAX_USBDISK 2
907 char usbdisk_queue
[MAX_USBDISK
][10];
913 void set_usbsd_disk(char *disk
)
918 if ( usbdisk_num
== 0 || ( usbdisk_num
== 1 && disk
== NULL
) )
920 memset(usbdisk
,0,10);
926 strcpy(usbdisk
,disk
);
932 if ( usbdisk_num
== 1 && disk
!= NULL
)
935 memset( usbdisk_queue
[0],0,10);
936 strcpy( usbdisk_queue
[0],usbdisk
);
937 memset( usbdisk_queue
[1],0,10);
938 strcpy( usbdisk_queue
[1],disk
);
939 memset( usbdisk
,0,10);
948 if ( usbdisk_num
== handle_index
&& disk
== NULL
)
950 for ( count
= 0 ; count
< MAX_USBDISK
; count
++ )
951 memset( usbdisk_queue
[count
],0,10 );
960 #elif ( defined CONFIG_ARCH_IA240 ) || ( defined CONFIG_ARCH_IA241 )
966 #if ( defined CONFIG_ARCH_IA240 ) || ( defined CONFIG_ARCH_IA241 )
967 static void mountusb(char *action
)
968 #elif ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 ) || ( defined CONFIG_ARCH_W341 )
969 void mountusb(char *action
)
973 char *argv
[2], **envp
, *buf
, *scratch
;
976 #if ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 ) || ( defined CONFIG_ARCH_W341 )
981 if ( in_interrupt() )
983 if ( !current
->fs
->root
)
985 if ( !(envp
=(char **)kmalloc(10*sizeof(char *), GFP_KERNEL
)) )
987 if ( !(buf
=kmalloc(256, GFP_KERNEL
)) )
990 #if ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 ) || ( defined CONFIG_ARCH_W341 )
991 if ( !(buf1
=kmalloc(256, GFP_KERNEL
)) )
997 argv
[0] = "/bin/usbstpnp";
999 envp
[i
++] = "HOME=/";
1000 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1002 envp
[i
++] = scratch
;
1003 scratch
+= sprintf(scratch
, "ACTION=%s", action
) + 1;
1005 #if ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 )
1007 envp
[i
++] = scratch
;
1008 if ( usbdisk_num
> 1 )
1010 scratch
+= sprintf(scratch
, "DISK=%s", usbdisk_queue
[ handle_index
] ) + 1;
1016 scratch
+= sprintf(scratch
, "DISK=%s", usbdisk
) + 1;
1018 #elif ( defined CONFIG_ARCH_IA240 ) || ( defined CONFIG_ARCH_IA241 )
1019 scratch
+= sprintf(scratch
, "DISK=%s", usbdisk
) + 1;
1023 call_usermodehelper(argv
[0], argv
, envp
, 0);
1025 #if ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 )
1026 set_usbsd_disk(NULL
);
1031 #if ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345_IMP1 )
1042 /* Probe to see if we can drive a newly-connected USB device */
1043 static int storage_probe(struct usb_interface
*intf
,
1044 const struct usb_device_id
*id
)
1047 const int id_index
= id
- storage_usb_ids
;
1050 US_DEBUGP("USB Mass Storage device detected\n");
1052 /* Allocate the us_data structure and initialize the mutexes */
1053 us
= (struct us_data
*) kmalloc(sizeof(*us
), GFP_KERNEL
);
1055 printk(KERN_WARNING USB_STORAGE
"Out of memory\n");
1058 memset(us
, 0, sizeof(struct us_data
));
1059 init_MUTEX(&(us
->dev_semaphore
));
1060 init_MUTEX_LOCKED(&(us
->sema
));
1061 init_completion(&(us
->notify
));
1062 init_waitqueue_head(&us
->dev_reset_wait
);
1064 /* Associate the us_data structure with the USB device */
1065 result
= associate_dev(us
, intf
);
1070 * Get the unusual_devs entries and the descriptors
1072 * id_index is calculated in the declaration to be the index number
1073 * of the match from the usb_device_id table, so we can find the
1074 * corresponding entry in the private table.
1076 #if 0 // mask by Victor Yu. 06-13-2007
1077 get_device_info(us
, id_index
);
1078 #else // add by Victor Yu. 06-13-2007
1079 result
= get_device_info(us
, id_index
);
1084 #ifdef CONFIG_USB_STORAGE_SDDR09
1085 if (us
->protocol
== US_PR_EUSB_SDDR09
||
1086 us
->protocol
== US_PR_DPCM_USB
) {
1087 /* set the configuration -- STALL is an acceptable response here */
1088 if (us
->pusb_dev
->actconfig
->desc
.bConfigurationValue
!= 1) {
1089 US_DEBUGP("active config #%d != 1 ??\n", us
->pusb_dev
1090 ->actconfig
->desc
.bConfigurationValue
);
1093 result
= usb_reset_configuration(us
->pusb_dev
);
1095 US_DEBUGP("Result of usb_reset_configuration is %d\n", result
);
1096 if (result
== -EPIPE
) {
1097 US_DEBUGP("-- stall on control interface\n");
1098 } else if (result
!= 0) {
1099 /* it's not a stall, but another error -- time to bail */
1100 US_DEBUGP("-- Unknown error. Rejecting device\n");
1106 /* Get the transport, protocol, and pipe settings */
1107 result
= get_transport(us
);
1110 result
= get_protocol(us
);
1113 result
= get_pipes(us
);
1117 /* Acquire all the other resources */
1118 result
= usb_stor_acquire_resources(us
);
1122 /* Finally, add the host (this does SCSI device scanning) */
1123 result
= scsi_add_host(us
->host
, &intf
->dev
);
1125 printk(KERN_WARNING USB_STORAGE
1126 "Unable to add the scsi host\n");
1130 scsi_scan_host(us
->host
);
1133 "USB Mass Storage device found at %d\n", us
->pusb_dev
->devnum
);
1134 #if ( defined CONFIG_ARCH_IA240 ) || ( defined CONFIG_ARCH_IA241 )
1139 /* We come here if there are any problems */
1141 US_DEBUGP("storage_probe() failed\n");
1142 usb_stor_release_resources(us
);
1147 /* Handle a disconnect event from the USB core */
1148 static void storage_disconnect(struct usb_interface
*intf
)
1150 struct us_data
*us
= usb_get_intfdata(intf
);
1152 US_DEBUGP("storage_disconnect() called\n");
1154 /* Prevent new USB transfers, stop the current command, and
1155 * interrupt a device-reset delay */
1156 set_bit(US_FLIDX_DISCONNECTING
, &us
->flags
);
1157 usb_stor_stop_transport(us
);
1158 wake_up(&us
->dev_reset_wait
);
1160 /* Wait for the current command to finish, then remove the host */
1161 #if ( defined CONFIG_ARCH_IA240 ) || ( defined CONFIG_ARCH_IA241 )
1164 down(&us
->dev_semaphore
);
1165 up(&us
->dev_semaphore
);
1166 scsi_remove_host(us
->host
);
1168 /* Wait for everything to become idle and release all our resources */
1169 usb_stor_release_resources(us
);
1173 /***********************************************************************
1174 * Initialization and registration
1175 ***********************************************************************/
1177 static int __init
usb_stor_init(void)
1180 printk(KERN_INFO
"Initializing USB Mass Storage driver...\n");
1182 /* register the driver, return usb_register return code if error */
1183 retval
= usb_register(&usb_storage_driver
);
1188 printk(KERN_INFO
"USB Mass Storage support registered.\n");
1193 static void __exit
usb_stor_exit(void)
1195 US_DEBUGP("usb_stor_exit() called\n");
1197 /* Deregister the driver
1198 * This will cause disconnect() to be called for each
1201 US_DEBUGP("-- calling usb_deregister()\n");
1202 usb_deregister(&usb_storage_driver
) ;
1205 module_init(usb_stor_init
);
1206 module_exit(usb_stor_exit
);
1208 #if ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345_IMP1 )
1209 EXPORT_SYMBOL(mountusb
);
1210 EXPORT_SYMBOL(set_usbsd_disk
);