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>
53 #include <linux/suspend.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
58 #include <scsi/scsi.h>
59 #include <scsi/scsi_cmnd.h>
60 #include <scsi/scsi_device.h>
64 #include "transport.h"
67 #include "initializers.h"
69 #ifdef CONFIG_USB_STORAGE_USBAT
70 #include "shuttle_usbat.h"
72 #ifdef CONFIG_USB_STORAGE_SDDR09
75 #ifdef CONFIG_USB_STORAGE_SDDR55
78 #ifdef CONFIG_USB_STORAGE_DPCM
81 #ifdef CONFIG_USB_STORAGE_FREECOM
84 #ifdef CONFIG_USB_STORAGE_ISD200
87 #ifdef CONFIG_USB_STORAGE_DATAFAB
90 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
93 #ifdef CONFIG_USB_STORAGE_ONETOUCH
97 /* Some informational data */
98 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
99 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
100 MODULE_LICENSE("GPL");
102 static unsigned int delay_use = 5;
103 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
104 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
107 /* These are used to make sure the module doesn't unload before all the
108 * threads have exited.
110 static atomic_t total_threads = ATOMIC_INIT(0);
111 static DECLARE_COMPLETION(threads_gone);
114 static int storage_probe(struct usb_interface *iface,
115 const struct usb_device_id *id);
117 static void storage_disconnect(struct usb_interface *iface);
119 /* The entries in this table, except for final ones here
120 * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
121 * line for line with the entries of us_unsuaul_dev_list[].
124 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
125 vendorName, productName,useProtocol, useTransport, \
126 initFunction, flags) \
127 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
129 static struct usb_device_id storage_usb_ids [] = {
131 # include "unusual_devs.h"
133 /* Control/Bulk transport for all SubClass values */
134 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CB) },
135 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CB) },
136 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CB) },
137 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CB) },
138 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CB) },
139 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CB) },
141 /* Control/Bulk/Interrupt transport for all SubClass values */
142 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CBI) },
143 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CBI) },
144 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CBI) },
145 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CBI) },
146 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CBI) },
147 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CBI) },
149 /* Bulk-only transport for all SubClass values */
150 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_BULK) },
151 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_BULK) },
152 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_BULK) },
153 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_BULK) },
154 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_BULK) },
155 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
157 /* Terminating entry */
161 MODULE_DEVICE_TABLE (usb, storage_usb_ids);
163 /* This is the list of devices we recognize, along with their flag data */
165 /* The vendor name should be kept at eight characters or less, and
166 * the product name should be kept at 16 characters or less. If a device
167 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
168 * normally generated by a device thorugh the INQUIRY response will be
169 * taken from this list, and this is the reason for the above size
170 * restriction. However, if the flag is not present, then you
171 * are free to use as many characters as you like.
175 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
176 vendor_name, product_name, use_protocol, use_transport, \
177 init_function, Flags) \
179 .vendorName = vendor_name, \
180 .productName = product_name, \
181 .useProtocol = use_protocol, \
182 .useTransport = use_transport, \
183 .initFunction = init_function, \
187 static struct us_unusual_dev us_unusual_dev_list[] = {
188 # include "unusual_devs.h"
190 /* Control/Bulk transport for all SubClass values */
191 { .useProtocol = US_SC_RBC,
192 .useTransport = US_PR_CB},
193 { .useProtocol = US_SC_8020,
194 .useTransport = US_PR_CB},
195 { .useProtocol = US_SC_QIC,
196 .useTransport = US_PR_CB},
197 { .useProtocol = US_SC_UFI,
198 .useTransport = US_PR_CB},
199 { .useProtocol = US_SC_8070,
200 .useTransport = US_PR_CB},
201 { .useProtocol = US_SC_SCSI,
202 .useTransport = US_PR_CB},
204 /* Control/Bulk/Interrupt transport for all SubClass values */
205 { .useProtocol = US_SC_RBC,
206 .useTransport = US_PR_CBI},
207 { .useProtocol = US_SC_8020,
208 .useTransport = US_PR_CBI},
209 { .useProtocol = US_SC_QIC,
210 .useTransport = US_PR_CBI},
211 { .useProtocol = US_SC_UFI,
212 .useTransport = US_PR_CBI},
213 { .useProtocol = US_SC_8070,
214 .useTransport = US_PR_CBI},
215 { .useProtocol = US_SC_SCSI,
216 .useTransport = US_PR_CBI},
218 /* Bulk-only transport for all SubClass values */
219 { .useProtocol = US_SC_RBC,
220 .useTransport = US_PR_BULK},
221 { .useProtocol = US_SC_8020,
222 .useTransport = US_PR_BULK},
223 { .useProtocol = US_SC_QIC,
224 .useTransport = US_PR_BULK},
225 { .useProtocol = US_SC_UFI,
226 .useTransport = US_PR_BULK},
227 { .useProtocol = US_SC_8070,
228 .useTransport = US_PR_BULK},
229 { .useProtocol = US_SC_SCSI,
230 .useTransport = US_PR_BULK},
232 /* Terminating entry */
236 static struct usb_driver usb_storage_driver = {
237 .owner = THIS_MODULE,
238 .name = "usb-storage",
239 .probe = storage_probe,
240 .disconnect = storage_disconnect,
241 .id_table = storage_usb_ids,
245 * fill_inquiry_response takes an unsigned char array (which must
246 * be at least 36 characters) and populates the vendor name,
247 * product name, and revision fields. Then the array is copied
248 * into the SCSI command's response buffer (oddly enough
249 * called request_buffer). data_len contains the length of the
250 * data array, which again must be at least 36.
253 void fill_inquiry_response(struct us_data *us, unsigned char *data,
254 unsigned int data_len)
256 if (data_len<36) // You lose.
259 if(data[0]&0x20) { /* USB device currently not connected. Return
260 peripheral qualifier 001b ("...however, the
261 physical device is not currently connected
262 to this logical unit") and leave vendor and
263 product identification empty. ("If the target
264 does store some of the INQUIRY data on the
265 device, it may return zeros or ASCII spaces
266 (20h) in those fields until the data is
267 available from the device."). */
270 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
271 memcpy(data+8, us->unusual_dev->vendorName,
272 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
273 strlen(us->unusual_dev->vendorName));
274 memcpy(data+16, us->unusual_dev->productName,
275 strlen(us->unusual_dev->productName) > 16 ? 16 :
276 strlen(us->unusual_dev->productName));
277 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
278 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
279 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
280 data[35] = 0x30 + ((bcdDevice) & 0x0F);
283 usb_stor_set_xfer_buf(data, data_len, us->srb);
286 static int usb_stor_control_thread(void * __us)
288 struct us_data *us = (struct us_data *)__us;
289 struct Scsi_Host *host = us_to_host(us);
294 * This thread doesn't need any user-level access,
295 * so get rid of all our resources.
297 daemonize("usb-storage");
298 current->flags |= PF_NOFREEZE;
301 /* acquire a reference to the host, so it won't be deallocated
302 * until we're ready to exit */
305 /* signal that we've started the thread */
306 complete(&(us->notify));
309 US_DEBUGP("*** thread sleeping.\n");
310 if(down_interruptible(&us->sema))
313 US_DEBUGP("*** thread awakened.\n");
315 /* lock the device pointers */
316 down(&(us->dev_semaphore));
318 /* if the device has disconnected, we are free to exit */
319 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
320 US_DEBUGP("-- exiting\n");
321 up(&(us->dev_semaphore));
325 /* lock access to the state */
328 /* has the command timed out *already* ? */
329 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
330 us->srb->result = DID_ABORT << 16;
336 /* reject the command if the direction indicator
339 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
340 US_DEBUGP("UNKNOWN data direction\n");
341 us->srb->result = DID_ERROR << 16;
344 /* reject if target != 0 or if LUN is higher than
345 * the maximum known LUN
347 else if (us->srb->device->id &&
348 !(us->flags & US_FL_SCM_MULT_TARG)) {
349 US_DEBUGP("Bad target number (%d:%d)\n",
350 us->srb->device->id, us->srb->device->lun);
351 us->srb->result = DID_BAD_TARGET << 16;
354 else if (us->srb->device->lun > us->max_lun) {
355 US_DEBUGP("Bad LUN (%d:%d)\n",
356 us->srb->device->id, us->srb->device->lun);
357 us->srb->result = DID_BAD_TARGET << 16;
360 /* Handle those devices which need us to fake
361 * their inquiry data */
362 else if ((us->srb->cmnd[0] == INQUIRY) &&
363 (us->flags & US_FL_FIX_INQUIRY)) {
364 unsigned char data_ptr[36] = {
365 0x00, 0x80, 0x02, 0x02,
366 0x1F, 0x00, 0x00, 0x00};
368 US_DEBUGP("Faking INQUIRY command\n");
369 fill_inquiry_response(us, data_ptr, 36);
370 us->srb->result = SAM_STAT_GOOD;
373 /* we've got a command, let's do it! */
375 US_DEBUG(usb_stor_show_command(us->srb));
376 us->proto_handler(us->srb, us);
379 /* lock access to the state */
382 /* indicate that the command is done */
383 if (us->srb->result != DID_ABORT << 16) {
384 US_DEBUGP("scsi cmd done, result=0x%x\n",
386 us->srb->scsi_done(us->srb);
389 US_DEBUGP("scsi command aborted\n");
392 /* If an abort request was received we need to signal that
393 * the abort has finished. The proper test for this is
394 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
395 * a timeout/abort request might be received after all the
396 * USB processing was complete. */
397 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags))
398 complete(&(us->notify));
400 /* finished working on this command */
404 /* unlock the device pointers */
405 up(&(us->dev_semaphore));
410 /* notify the exit routine that we're actually exiting now
412 * complete()/wait_for_completion() is similar to up()/down(),
413 * except that complete() is safe in the case where the structure
414 * is getting deleted in a parallel mode of execution (i.e. just
415 * after the down() -- that's necessary for the thread-shutdown
418 * complete_and_exit() goes even further than this -- it is safe in
419 * the case that the thread of the caller is going away (not just
420 * the structure) -- this is necessary for the module-remove case.
421 * This is important in preemption kernels, which transfer the flow
422 * of execution immediately upon a complete().
424 complete_and_exit(&threads_gone, 0);
427 /***********************************************************************
428 * Device probing and disconnecting
429 ***********************************************************************/
431 /* Associate our private data with the USB device */
432 static int associate_dev(struct us_data *us, struct usb_interface *intf)
434 US_DEBUGP("-- %s\n", __FUNCTION__);
436 /* Fill in the device-related fields */
437 us->pusb_dev = interface_to_usbdev(intf);
438 us->pusb_intf = intf;
439 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
440 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
441 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
442 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
443 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
444 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
445 intf->cur_altsetting->desc.bInterfaceSubClass,
446 intf->cur_altsetting->desc.bInterfaceProtocol);
448 /* Store our private data in the interface */
449 usb_set_intfdata(intf, us);
451 /* Allocate the device-related DMA-mapped buffers */
452 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
453 GFP_KERNEL, &us->cr_dma);
455 US_DEBUGP("usb_ctrlrequest allocation failed\n");
459 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
460 GFP_KERNEL, &us->iobuf_dma);
462 US_DEBUGP("I/O buffer allocation failed\n");
468 /* Get the unusual_devs entries and the string descriptors */
469 static void get_device_info(struct us_data *us, int id_index)
471 struct usb_device *dev = us->pusb_dev;
472 struct usb_interface_descriptor *idesc =
473 &us->pusb_intf->cur_altsetting->desc;
474 struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
475 struct usb_device_id *id = &storage_usb_ids[id_index];
477 /* Store the entries */
478 us->unusual_dev = unusual_dev;
479 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
480 idesc->bInterfaceSubClass :
481 unusual_dev->useProtocol;
482 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
483 idesc->bInterfaceProtocol :
484 unusual_dev->useTransport;
485 us->flags = unusual_dev->flags;
488 * This flag is only needed when we're in high-speed, so let's
489 * disable it if we're in full-speed
491 if (dev->speed != USB_SPEED_HIGH)
492 us->flags &= ~US_FL_GO_SLOW;
494 /* Log a message if a non-generic unusual_dev entry contains an
495 * unnecessary subclass or protocol override. This may stimulate
496 * reports from users that will help us remove unneeded entries
497 * from the unusual_devs.h table.
499 if (id->idVendor || id->idProduct) {
500 static char *msgs[3] = {
501 "an unneeded SubClass entry",
502 "an unneeded Protocol entry",
503 "unneeded SubClass and Protocol entries"};
504 struct usb_device_descriptor *ddesc = &dev->descriptor;
507 if (unusual_dev->useProtocol != US_SC_DEVICE &&
508 us->subclass == idesc->bInterfaceSubClass)
510 if (unusual_dev->useTransport != US_PR_DEVICE &&
511 us->protocol == idesc->bInterfaceProtocol)
513 if (msg >= 0 && !(unusual_dev->flags & US_FL_NEED_OVERRIDE))
514 printk(KERN_NOTICE USB_STORAGE "This device "
515 "(%04x,%04x,%04x S %02x P %02x)"
516 " has %s in unusual_devs.h\n"
517 " Please send a copy of this message to "
518 "<linux-usb-devel@lists.sourceforge.net>\n",
519 le16_to_cpu(ddesc->idVendor),
520 le16_to_cpu(ddesc->idProduct),
521 le16_to_cpu(ddesc->bcdDevice),
522 idesc->bInterfaceSubClass,
523 idesc->bInterfaceProtocol,
528 /* Get the transport settings */
529 static int get_transport(struct us_data *us)
531 switch (us->protocol) {
533 us->transport_name = "Control/Bulk";
534 us->transport = usb_stor_CB_transport;
535 us->transport_reset = usb_stor_CB_reset;
540 us->transport_name = "Control/Bulk/Interrupt";
541 us->transport = usb_stor_CBI_transport;
542 us->transport_reset = usb_stor_CB_reset;
547 us->transport_name = "Bulk";
548 us->transport = usb_stor_Bulk_transport;
549 us->transport_reset = usb_stor_Bulk_reset;
552 #ifdef CONFIG_USB_STORAGE_USBAT
553 case US_PR_SCM_ATAPI:
554 us->transport_name = "SCM/ATAPI";
555 us->transport = usbat_transport;
556 us->transport_reset = usb_stor_CB_reset;
561 #ifdef CONFIG_USB_STORAGE_SDDR09
562 case US_PR_EUSB_SDDR09:
563 us->transport_name = "EUSB/SDDR09";
564 us->transport = sddr09_transport;
565 us->transport_reset = usb_stor_CB_reset;
570 #ifdef CONFIG_USB_STORAGE_SDDR55
572 us->transport_name = "SDDR55";
573 us->transport = sddr55_transport;
574 us->transport_reset = sddr55_reset;
579 #ifdef CONFIG_USB_STORAGE_DPCM
581 us->transport_name = "Control/Bulk-EUSB/SDDR09";
582 us->transport = dpcm_transport;
583 us->transport_reset = usb_stor_CB_reset;
588 #ifdef CONFIG_USB_STORAGE_FREECOM
590 us->transport_name = "Freecom";
591 us->transport = freecom_transport;
592 us->transport_reset = usb_stor_freecom_reset;
597 #ifdef CONFIG_USB_STORAGE_DATAFAB
599 us->transport_name = "Datafab Bulk-Only";
600 us->transport = datafab_transport;
601 us->transport_reset = usb_stor_Bulk_reset;
606 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
608 us->transport_name = "Lexar Jumpshot Control/Bulk";
609 us->transport = jumpshot_transport;
610 us->transport_reset = usb_stor_Bulk_reset;
618 US_DEBUGP("Transport: %s\n", us->transport_name);
620 /* fix for single-lun devices */
621 if (us->flags & US_FL_SINGLE_LUN)
626 /* Get the protocol settings */
627 static int get_protocol(struct us_data *us)
629 switch (us->subclass) {
631 us->protocol_name = "Reduced Block Commands (RBC)";
632 us->proto_handler = usb_stor_transparent_scsi_command;
636 us->protocol_name = "8020i";
637 us->proto_handler = usb_stor_ATAPI_command;
642 us->protocol_name = "QIC-157";
643 us->proto_handler = usb_stor_qic157_command;
648 us->protocol_name = "8070i";
649 us->proto_handler = usb_stor_ATAPI_command;
654 us->protocol_name = "Transparent SCSI";
655 us->proto_handler = usb_stor_transparent_scsi_command;
659 us->protocol_name = "Uniform Floppy Interface (UFI)";
660 us->proto_handler = usb_stor_ufi_command;
663 #ifdef CONFIG_USB_STORAGE_ISD200
665 us->protocol_name = "ISD200 ATA/ATAPI";
666 us->proto_handler = isd200_ata_command;
673 US_DEBUGP("Protocol: %s\n", us->protocol_name);
677 /* Get the pipe settings */
678 static int get_pipes(struct us_data *us)
680 struct usb_host_interface *altsetting =
681 us->pusb_intf->cur_altsetting;
683 struct usb_endpoint_descriptor *ep;
684 struct usb_endpoint_descriptor *ep_in = NULL;
685 struct usb_endpoint_descriptor *ep_out = NULL;
686 struct usb_endpoint_descriptor *ep_int = NULL;
689 * Find the endpoints we need.
690 * We are expecting a minimum of 2 endpoints - in and out (bulk).
691 * An optional interrupt is OK (necessary for CBI protocol).
692 * We will ignore any others.
694 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
695 ep = &altsetting->endpoint[i].desc;
697 /* Is it a BULK endpoint? */
698 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
699 == USB_ENDPOINT_XFER_BULK) {
700 /* BULK in or out? */
701 if (ep->bEndpointAddress & USB_DIR_IN)
707 /* Is it an interrupt endpoint? */
708 else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
709 == USB_ENDPOINT_XFER_INT) {
714 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
715 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
719 /* Calculate and store the pipe values */
720 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
721 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
722 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
723 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
724 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
725 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
727 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
728 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
729 us->ep_bInterval = ep_int->bInterval;
734 /* Initialize all the dynamic resources we need */
735 static int usb_stor_acquire_resources(struct us_data *us)
739 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
740 if (!us->current_urb) {
741 US_DEBUGP("URB allocation failed\n");
745 /* Lock the device while we carry out the next two operations */
746 down(&us->dev_semaphore);
748 /* For bulk-only devices, determine the max LUN value */
749 if (us->protocol == US_PR_BULK) {
750 p = usb_stor_Bulk_max_lun(us);
752 up(&us->dev_semaphore);
758 /* Just before we start our control thread, initialize
759 * the device if it needs initialization */
760 if (us->unusual_dev->initFunction)
761 us->unusual_dev->initFunction(us);
763 up(&us->dev_semaphore);
765 /* Start up our control thread */
766 p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
768 printk(KERN_WARNING USB_STORAGE
769 "Unable to start control thread\n");
773 atomic_inc(&total_threads);
775 /* Wait for the thread to start */
776 wait_for_completion(&(us->notify));
781 /* Release all our dynamic resources */
782 static void usb_stor_release_resources(struct us_data *us)
784 US_DEBUGP("-- %s\n", __FUNCTION__);
786 /* Tell the control thread to exit. The SCSI host must
787 * already have been removed so it won't try to queue
790 US_DEBUGP("-- sending exit command to thread\n");
791 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
794 /* Call the destructor routine, if it exists */
795 if (us->extra_destructor) {
796 US_DEBUGP("-- calling extra_destructor()\n");
797 us->extra_destructor(us->extra);
800 /* Free the extra data and the URB */
802 usb_free_urb(us->current_urb);
805 /* Dissociate from the USB device */
806 static void dissociate_dev(struct us_data *us)
808 US_DEBUGP("-- %s\n", __FUNCTION__);
810 /* Free the device-related DMA-mapped buffers */
812 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
815 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
818 /* Remove our private data from the interface */
819 usb_set_intfdata(us->pusb_intf, NULL);
822 /* First stage of disconnect processing: stop all commands and remove
824 static void quiesce_and_remove_host(struct us_data *us)
826 /* Prevent new USB transfers, stop the current command, and
827 * interrupt a SCSI-scan or device-reset delay */
828 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
829 usb_stor_stop_transport(us);
830 wake_up(&us->delay_wait);
832 /* It doesn't matter if the SCSI-scanning thread is still running.
833 * The thread will exit when it sees the DISCONNECTING flag. */
835 /* Wait for the current command to finish, then remove the host */
836 down(&us->dev_semaphore);
837 up(&us->dev_semaphore);
839 /* queuecommand won't accept any new commands and the control
840 * thread won't execute a previously-queued command. If there
841 * is such a command pending, complete it with an error. */
843 us->srb->result = DID_NO_CONNECT << 16;
844 scsi_lock(us_to_host(us));
845 us->srb->scsi_done(us->srb);
847 scsi_unlock(us_to_host(us));
850 /* Now we own no commands so it's safe to remove the SCSI host */
851 scsi_remove_host(us_to_host(us));
854 /* Second stage of disconnect processing: deallocate all resources */
855 static void release_everything(struct us_data *us)
857 usb_stor_release_resources(us);
860 /* Drop our reference to the host; the SCSI core will free it
861 * (and "us" along with it) when the refcount becomes 0. */
862 scsi_host_put(us_to_host(us));
865 /* Thread to carry out delayed SCSI-device scanning */
866 static int usb_stor_scan_thread(void * __us)
868 struct us_data *us = (struct us_data *)__us;
871 * This thread doesn't need any user-level access,
872 * so get rid of all our resources.
875 daemonize("usb-stor-scan");
878 /* Acquire a reference to the host, so it won't be deallocated
879 * until we're ready to exit */
880 scsi_host_get(us_to_host(us));
882 /* Signal that we've started the thread */
883 complete(&(us->notify));
886 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
888 /* Wait for the timeout to expire or for a disconnect */
890 printk(KERN_DEBUG "usb-storage: waiting for device "
891 "to settle before scanning\n");
893 wait_event_interruptible_timeout(us->delay_wait,
894 test_bit(US_FLIDX_DISCONNECTING, &us->flags),
900 /* If the device is still connected, perform the scanning */
901 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
902 scsi_scan_host(us_to_host(us));
903 printk(KERN_DEBUG "usb-storage: device scan complete\n");
905 /* Should we unbind if no devices were detected? */
908 scsi_host_put(us_to_host(us));
909 complete_and_exit(&threads_gone, 0);
913 /* Probe to see if we can drive a newly-connected USB device */
914 static int storage_probe(struct usb_interface *intf,
915 const struct usb_device_id *id)
917 struct Scsi_Host *host;
919 const int id_index = id - storage_usb_ids;
922 US_DEBUGP("USB Mass Storage device detected\n");
925 * Ask the SCSI layer to allocate a host structure, with extra
926 * space at the end for our private us_data structure.
928 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
930 printk(KERN_WARNING USB_STORAGE
931 "Unable to allocate the scsi host\n");
935 us = host_to_us(host);
936 memset(us, 0, sizeof(struct us_data));
937 init_MUTEX(&(us->dev_semaphore));
938 init_MUTEX_LOCKED(&(us->sema));
939 init_completion(&(us->notify));
940 init_waitqueue_head(&us->delay_wait);
942 /* Associate the us_data structure with the USB device */
943 result = associate_dev(us, intf);
948 * Get the unusual_devs entries and the descriptors
950 * id_index is calculated in the declaration to be the index number
951 * of the match from the usb_device_id table, so we can find the
952 * corresponding entry in the private table.
954 get_device_info(us, id_index);
956 #ifdef CONFIG_USB_STORAGE_SDDR09
957 if (us->protocol == US_PR_EUSB_SDDR09 ||
958 us->protocol == US_PR_DPCM_USB) {
959 /* set the configuration -- STALL is an acceptable response here */
960 if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
961 US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev
962 ->actconfig->desc.bConfigurationValue);
965 result = usb_reset_configuration(us->pusb_dev);
967 US_DEBUGP("Result of usb_reset_configuration is %d\n", result);
968 if (result == -EPIPE) {
969 US_DEBUGP("-- stall on control interface\n");
970 } else if (result != 0) {
971 /* it's not a stall, but another error -- time to bail */
972 US_DEBUGP("-- Unknown error. Rejecting device\n");
978 /* Get the transport, protocol, and pipe settings */
979 result = get_transport(us);
982 result = get_protocol(us);
985 result = get_pipes(us);
989 /* Acquire all the other resources and add the host */
990 result = usb_stor_acquire_resources(us);
993 result = scsi_add_host(host, &intf->dev);
995 printk(KERN_WARNING USB_STORAGE
996 "Unable to add the scsi host\n");
1000 /* Start up the thread for delayed SCSI-device scanning */
1001 result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM);
1003 printk(KERN_WARNING USB_STORAGE
1004 "Unable to start the device-scanning thread\n");
1005 quiesce_and_remove_host(us);
1008 atomic_inc(&total_threads);
1010 /* Wait for the thread to start */
1011 wait_for_completion(&(us->notify));
1015 /* We come here if there are any problems */
1017 US_DEBUGP("storage_probe() failed\n");
1018 release_everything(us);
1022 /* Handle a disconnect event from the USB core */
1023 static void storage_disconnect(struct usb_interface *intf)
1025 struct us_data *us = usb_get_intfdata(intf);
1027 US_DEBUGP("storage_disconnect() called\n");
1028 quiesce_and_remove_host(us);
1029 release_everything(us);
1032 /***********************************************************************
1033 * Initialization and registration
1034 ***********************************************************************/
1036 static int __init usb_stor_init(void)
1039 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1041 /* register the driver, return usb_register return code if error */
1042 retval = usb_register(&usb_storage_driver);
1044 printk(KERN_INFO "USB Mass Storage support registered.\n");
1049 static void __exit usb_stor_exit(void)
1051 US_DEBUGP("usb_stor_exit() called\n");
1053 /* Deregister the driver
1054 * This will cause disconnect() to be called for each
1057 US_DEBUGP("-- calling usb_deregister()\n");
1058 usb_deregister(&usb_storage_driver) ;
1060 /* Don't return until all of our control and scanning threads
1061 * have exited. Since each thread signals threads_gone as its
1062 * last act, we have to call wait_for_completion the right number
1065 while (atomic_read(&total_threads) > 0) {
1066 wait_for_completion(&threads_gone);
1067 atomic_dec(&total_threads);
1071 module_init(usb_stor_init);
1072 module_exit(usb_stor_exit);