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 /* The entries in this table, except for final ones here
115 * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
116 * line for line with the entries of us_unsuaul_dev_list[].
119 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
120 vendorName, productName,useProtocol, useTransport, \
121 initFunction, flags) \
122 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
124 static struct usb_device_id storage_usb_ids [] = {
126 # include "unusual_devs.h"
128 /* Control/Bulk transport for all SubClass values */
129 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CB) },
130 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CB) },
131 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CB) },
132 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CB) },
133 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CB) },
134 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CB) },
136 /* Control/Bulk/Interrupt transport for all SubClass values */
137 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CBI) },
138 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CBI) },
139 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CBI) },
140 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CBI) },
141 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CBI) },
142 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CBI) },
144 /* Bulk-only transport for all SubClass values */
145 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_BULK) },
146 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_BULK) },
147 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_BULK) },
148 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_BULK) },
149 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_BULK) },
150 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
152 /* Terminating entry */
156 MODULE_DEVICE_TABLE (usb, storage_usb_ids);
158 /* This is the list of devices we recognize, along with their flag data */
160 /* The vendor name should be kept at eight characters or less, and
161 * the product name should be kept at 16 characters or less. If a device
162 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
163 * normally generated by a device thorugh the INQUIRY response will be
164 * taken from this list, and this is the reason for the above size
165 * restriction. However, if the flag is not present, then you
166 * are free to use as many characters as you like.
170 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
171 vendor_name, product_name, use_protocol, use_transport, \
172 init_function, Flags) \
174 .vendorName = vendor_name, \
175 .productName = product_name, \
176 .useProtocol = use_protocol, \
177 .useTransport = use_transport, \
178 .initFunction = init_function, \
182 static struct us_unusual_dev us_unusual_dev_list[] = {
183 # include "unusual_devs.h"
185 /* Control/Bulk transport for all SubClass values */
186 { .useProtocol = US_SC_RBC,
187 .useTransport = US_PR_CB},
188 { .useProtocol = US_SC_8020,
189 .useTransport = US_PR_CB},
190 { .useProtocol = US_SC_QIC,
191 .useTransport = US_PR_CB},
192 { .useProtocol = US_SC_UFI,
193 .useTransport = US_PR_CB},
194 { .useProtocol = US_SC_8070,
195 .useTransport = US_PR_CB},
196 { .useProtocol = US_SC_SCSI,
197 .useTransport = US_PR_CB},
199 /* Control/Bulk/Interrupt transport for all SubClass values */
200 { .useProtocol = US_SC_RBC,
201 .useTransport = US_PR_CBI},
202 { .useProtocol = US_SC_8020,
203 .useTransport = US_PR_CBI},
204 { .useProtocol = US_SC_QIC,
205 .useTransport = US_PR_CBI},
206 { .useProtocol = US_SC_UFI,
207 .useTransport = US_PR_CBI},
208 { .useProtocol = US_SC_8070,
209 .useTransport = US_PR_CBI},
210 { .useProtocol = US_SC_SCSI,
211 .useTransport = US_PR_CBI},
213 /* Bulk-only transport for all SubClass values */
214 { .useProtocol = US_SC_RBC,
215 .useTransport = US_PR_BULK},
216 { .useProtocol = US_SC_8020,
217 .useTransport = US_PR_BULK},
218 { .useProtocol = US_SC_QIC,
219 .useTransport = US_PR_BULK},
220 { .useProtocol = US_SC_UFI,
221 .useTransport = US_PR_BULK},
222 { .useProtocol = US_SC_8070,
223 .useTransport = US_PR_BULK},
224 { .useProtocol = US_SC_SCSI,
225 .useTransport = US_PR_BULK},
227 /* Terminating entry */
232 #ifdef CONFIG_PM /* Minimal support for suspend and resume */
234 static int storage_suspend(struct usb_interface *iface, pm_message_t message)
236 struct us_data *us = usb_get_intfdata(iface);
238 /* Wait until no command is running */
239 down(&us->dev_semaphore);
241 US_DEBUGP("%s\n", __FUNCTION__);
242 iface->dev.power.power_state.event = message.event;
244 /* When runtime PM is working, we'll set a flag to indicate
245 * whether we should autoresume when a SCSI request arrives. */
247 up(&us->dev_semaphore);
251 static int storage_resume(struct usb_interface *iface)
253 struct us_data *us = usb_get_intfdata(iface);
255 down(&us->dev_semaphore);
257 US_DEBUGP("%s\n", __FUNCTION__);
258 iface->dev.power.power_state.event = PM_EVENT_ON;
260 up(&us->dev_semaphore);
264 #endif /* CONFIG_PM */
267 * fill_inquiry_response takes an unsigned char array (which must
268 * be at least 36 characters) and populates the vendor name,
269 * product name, and revision fields. Then the array is copied
270 * into the SCSI command's response buffer (oddly enough
271 * called request_buffer). data_len contains the length of the
272 * data array, which again must be at least 36.
275 void fill_inquiry_response(struct us_data *us, unsigned char *data,
276 unsigned int data_len)
278 if (data_len<36) // You lose.
281 if(data[0]&0x20) { /* USB device currently not connected. Return
282 peripheral qualifier 001b ("...however, the
283 physical device is not currently connected
284 to this logical unit") and leave vendor and
285 product identification empty. ("If the target
286 does store some of the INQUIRY data on the
287 device, it may return zeros or ASCII spaces
288 (20h) in those fields until the data is
289 available from the device."). */
292 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
293 memcpy(data+8, us->unusual_dev->vendorName,
294 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
295 strlen(us->unusual_dev->vendorName));
296 memcpy(data+16, us->unusual_dev->productName,
297 strlen(us->unusual_dev->productName) > 16 ? 16 :
298 strlen(us->unusual_dev->productName));
299 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
300 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
301 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
302 data[35] = 0x30 + ((bcdDevice) & 0x0F);
305 usb_stor_set_xfer_buf(data, data_len, us->srb);
308 static int usb_stor_control_thread(void * __us)
310 struct us_data *us = (struct us_data *)__us;
311 struct Scsi_Host *host = us_to_host(us);
316 * This thread doesn't need any user-level access,
317 * so get rid of all our resources.
319 daemonize("usb-storage");
320 current->flags |= PF_NOFREEZE;
323 /* acquire a reference to the host, so it won't be deallocated
324 * until we're ready to exit */
327 /* signal that we've started the thread */
328 complete(&(us->notify));
331 US_DEBUGP("*** thread sleeping.\n");
332 if(down_interruptible(&us->sema))
335 US_DEBUGP("*** thread awakened.\n");
337 /* lock the device pointers */
338 down(&(us->dev_semaphore));
340 /* if the device has disconnected, we are free to exit */
341 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
342 US_DEBUGP("-- exiting\n");
343 up(&(us->dev_semaphore));
347 /* lock access to the state */
350 /* has the command timed out *already* ? */
351 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
352 us->srb->result = DID_ABORT << 16;
358 /* reject the command if the direction indicator
361 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
362 US_DEBUGP("UNKNOWN data direction\n");
363 us->srb->result = DID_ERROR << 16;
366 /* reject if target != 0 or if LUN is higher than
367 * the maximum known LUN
369 else if (us->srb->device->id &&
370 !(us->flags & US_FL_SCM_MULT_TARG)) {
371 US_DEBUGP("Bad target number (%d:%d)\n",
372 us->srb->device->id, us->srb->device->lun);
373 us->srb->result = DID_BAD_TARGET << 16;
376 else if (us->srb->device->lun > us->max_lun) {
377 US_DEBUGP("Bad LUN (%d:%d)\n",
378 us->srb->device->id, us->srb->device->lun);
379 us->srb->result = DID_BAD_TARGET << 16;
382 /* Handle those devices which need us to fake
383 * their inquiry data */
384 else if ((us->srb->cmnd[0] == INQUIRY) &&
385 (us->flags & US_FL_FIX_INQUIRY)) {
386 unsigned char data_ptr[36] = {
387 0x00, 0x80, 0x02, 0x02,
388 0x1F, 0x00, 0x00, 0x00};
390 US_DEBUGP("Faking INQUIRY command\n");
391 fill_inquiry_response(us, data_ptr, 36);
392 us->srb->result = SAM_STAT_GOOD;
395 /* we've got a command, let's do it! */
397 US_DEBUG(usb_stor_show_command(us->srb));
398 us->proto_handler(us->srb, us);
401 /* lock access to the state */
404 /* indicate that the command is done */
405 if (us->srb->result != DID_ABORT << 16) {
406 US_DEBUGP("scsi cmd done, result=0x%x\n",
408 us->srb->scsi_done(us->srb);
411 US_DEBUGP("scsi command aborted\n");
414 /* If an abort request was received we need to signal that
415 * the abort has finished. The proper test for this is
416 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
417 * the timeout might have occurred after the command had
418 * already completed with a different result code. */
419 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
420 complete(&(us->notify));
422 /* Allow USB transfers to resume */
423 clear_bit(US_FLIDX_ABORTING, &us->flags);
424 clear_bit(US_FLIDX_TIMED_OUT, &us->flags);
427 /* finished working on this command */
431 /* unlock the device pointers */
432 up(&(us->dev_semaphore));
437 /* notify the exit routine that we're actually exiting now
439 * complete()/wait_for_completion() is similar to up()/down(),
440 * except that complete() is safe in the case where the structure
441 * is getting deleted in a parallel mode of execution (i.e. just
442 * after the down() -- that's necessary for the thread-shutdown
445 * complete_and_exit() goes even further than this -- it is safe in
446 * the case that the thread of the caller is going away (not just
447 * the structure) -- this is necessary for the module-remove case.
448 * This is important in preemption kernels, which transfer the flow
449 * of execution immediately upon a complete().
451 complete_and_exit(&threads_gone, 0);
454 /***********************************************************************
455 * Device probing and disconnecting
456 ***********************************************************************/
458 /* Associate our private data with the USB device */
459 static int associate_dev(struct us_data *us, struct usb_interface *intf)
461 US_DEBUGP("-- %s\n", __FUNCTION__);
463 /* Fill in the device-related fields */
464 us->pusb_dev = interface_to_usbdev(intf);
465 us->pusb_intf = intf;
466 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
467 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
468 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
469 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
470 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
471 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
472 intf->cur_altsetting->desc.bInterfaceSubClass,
473 intf->cur_altsetting->desc.bInterfaceProtocol);
475 /* Store our private data in the interface */
476 usb_set_intfdata(intf, us);
478 /* Allocate the device-related DMA-mapped buffers */
479 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
480 GFP_KERNEL, &us->cr_dma);
482 US_DEBUGP("usb_ctrlrequest allocation failed\n");
486 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
487 GFP_KERNEL, &us->iobuf_dma);
489 US_DEBUGP("I/O buffer allocation failed\n");
493 us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
495 US_DEBUGP("Sense buffer allocation failed\n");
501 /* Get the unusual_devs entries and the string descriptors */
502 static void get_device_info(struct us_data *us, int id_index)
504 struct usb_device *dev = us->pusb_dev;
505 struct usb_interface_descriptor *idesc =
506 &us->pusb_intf->cur_altsetting->desc;
507 struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
508 struct usb_device_id *id = &storage_usb_ids[id_index];
510 /* Store the entries */
511 us->unusual_dev = unusual_dev;
512 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
513 idesc->bInterfaceSubClass :
514 unusual_dev->useProtocol;
515 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
516 idesc->bInterfaceProtocol :
517 unusual_dev->useTransport;
518 us->flags = unusual_dev->flags;
521 * This flag is only needed when we're in high-speed, so let's
522 * disable it if we're in full-speed
524 if (dev->speed != USB_SPEED_HIGH)
525 us->flags &= ~US_FL_GO_SLOW;
527 /* Log a message if a non-generic unusual_dev entry contains an
528 * unnecessary subclass or protocol override. This may stimulate
529 * reports from users that will help us remove unneeded entries
530 * from the unusual_devs.h table.
532 if (id->idVendor || id->idProduct) {
533 static char *msgs[3] = {
534 "an unneeded SubClass entry",
535 "an unneeded Protocol entry",
536 "unneeded SubClass and Protocol entries"};
537 struct usb_device_descriptor *ddesc = &dev->descriptor;
540 if (unusual_dev->useProtocol != US_SC_DEVICE &&
541 us->subclass == idesc->bInterfaceSubClass)
543 if (unusual_dev->useTransport != US_PR_DEVICE &&
544 us->protocol == idesc->bInterfaceProtocol)
546 if (msg >= 0 && !(unusual_dev->flags & US_FL_NEED_OVERRIDE))
547 printk(KERN_NOTICE USB_STORAGE "This device "
548 "(%04x,%04x,%04x S %02x P %02x)"
549 " has %s in unusual_devs.h\n"
550 " Please send a copy of this message to "
551 "<linux-usb-devel@lists.sourceforge.net>\n",
552 le16_to_cpu(ddesc->idVendor),
553 le16_to_cpu(ddesc->idProduct),
554 le16_to_cpu(ddesc->bcdDevice),
555 idesc->bInterfaceSubClass,
556 idesc->bInterfaceProtocol,
561 /* Get the transport settings */
562 static int get_transport(struct us_data *us)
564 switch (us->protocol) {
566 us->transport_name = "Control/Bulk";
567 us->transport = usb_stor_CB_transport;
568 us->transport_reset = usb_stor_CB_reset;
573 us->transport_name = "Control/Bulk/Interrupt";
574 us->transport = usb_stor_CBI_transport;
575 us->transport_reset = usb_stor_CB_reset;
580 us->transport_name = "Bulk";
581 us->transport = usb_stor_Bulk_transport;
582 us->transport_reset = usb_stor_Bulk_reset;
585 #ifdef CONFIG_USB_STORAGE_USBAT
587 us->transport_name = "Shuttle USBAT";
588 us->transport = usbat_transport;
589 us->transport_reset = usb_stor_CB_reset;
594 #ifdef CONFIG_USB_STORAGE_SDDR09
595 case US_PR_EUSB_SDDR09:
596 us->transport_name = "EUSB/SDDR09";
597 us->transport = sddr09_transport;
598 us->transport_reset = usb_stor_CB_reset;
603 #ifdef CONFIG_USB_STORAGE_SDDR55
605 us->transport_name = "SDDR55";
606 us->transport = sddr55_transport;
607 us->transport_reset = sddr55_reset;
612 #ifdef CONFIG_USB_STORAGE_DPCM
614 us->transport_name = "Control/Bulk-EUSB/SDDR09";
615 us->transport = dpcm_transport;
616 us->transport_reset = usb_stor_CB_reset;
621 #ifdef CONFIG_USB_STORAGE_FREECOM
623 us->transport_name = "Freecom";
624 us->transport = freecom_transport;
625 us->transport_reset = usb_stor_freecom_reset;
630 #ifdef CONFIG_USB_STORAGE_DATAFAB
632 us->transport_name = "Datafab Bulk-Only";
633 us->transport = datafab_transport;
634 us->transport_reset = usb_stor_Bulk_reset;
639 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
641 us->transport_name = "Lexar Jumpshot Control/Bulk";
642 us->transport = jumpshot_transport;
643 us->transport_reset = usb_stor_Bulk_reset;
651 US_DEBUGP("Transport: %s\n", us->transport_name);
653 /* fix for single-lun devices */
654 if (us->flags & US_FL_SINGLE_LUN)
659 /* Get the protocol settings */
660 static int get_protocol(struct us_data *us)
662 switch (us->subclass) {
664 us->protocol_name = "Reduced Block Commands (RBC)";
665 us->proto_handler = usb_stor_transparent_scsi_command;
669 us->protocol_name = "8020i";
670 us->proto_handler = usb_stor_ATAPI_command;
675 us->protocol_name = "QIC-157";
676 us->proto_handler = usb_stor_qic157_command;
681 us->protocol_name = "8070i";
682 us->proto_handler = usb_stor_ATAPI_command;
687 us->protocol_name = "Transparent SCSI";
688 us->proto_handler = usb_stor_transparent_scsi_command;
692 us->protocol_name = "Uniform Floppy Interface (UFI)";
693 us->proto_handler = usb_stor_ufi_command;
696 #ifdef CONFIG_USB_STORAGE_ISD200
698 us->protocol_name = "ISD200 ATA/ATAPI";
699 us->proto_handler = isd200_ata_command;
706 US_DEBUGP("Protocol: %s\n", us->protocol_name);
710 /* Get the pipe settings */
711 static int get_pipes(struct us_data *us)
713 struct usb_host_interface *altsetting =
714 us->pusb_intf->cur_altsetting;
716 struct usb_endpoint_descriptor *ep;
717 struct usb_endpoint_descriptor *ep_in = NULL;
718 struct usb_endpoint_descriptor *ep_out = NULL;
719 struct usb_endpoint_descriptor *ep_int = NULL;
722 * Find the endpoints we need.
723 * We are expecting a minimum of 2 endpoints - in and out (bulk).
724 * An optional interrupt is OK (necessary for CBI protocol).
725 * We will ignore any others.
727 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
728 ep = &altsetting->endpoint[i].desc;
730 /* Is it a BULK endpoint? */
731 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
732 == USB_ENDPOINT_XFER_BULK) {
733 /* BULK in or out? */
734 if (ep->bEndpointAddress & USB_DIR_IN)
740 /* Is it an interrupt endpoint? */
741 else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
742 == USB_ENDPOINT_XFER_INT) {
747 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
748 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
752 /* Calculate and store the pipe values */
753 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
754 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
755 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
756 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
757 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
758 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
760 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
761 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
762 us->ep_bInterval = ep_int->bInterval;
767 /* Initialize all the dynamic resources we need */
768 static int usb_stor_acquire_resources(struct us_data *us)
772 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
773 if (!us->current_urb) {
774 US_DEBUGP("URB allocation failed\n");
778 /* Just before we start our control thread, initialize
779 * the device if it needs initialization */
780 if (us->unusual_dev->initFunction) {
781 p = us->unusual_dev->initFunction(us);
786 /* Start up our control thread */
787 p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
789 printk(KERN_WARNING USB_STORAGE
790 "Unable to start control thread\n");
794 atomic_inc(&total_threads);
796 /* Wait for the thread to start */
797 wait_for_completion(&(us->notify));
802 /* Release all our dynamic resources */
803 static void usb_stor_release_resources(struct us_data *us)
805 US_DEBUGP("-- %s\n", __FUNCTION__);
807 /* Tell the control thread to exit. The SCSI host must
808 * already have been removed so it won't try to queue
811 US_DEBUGP("-- sending exit command to thread\n");
812 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
815 /* Call the destructor routine, if it exists */
816 if (us->extra_destructor) {
817 US_DEBUGP("-- calling extra_destructor()\n");
818 us->extra_destructor(us->extra);
821 /* Free the extra data and the URB */
823 usb_free_urb(us->current_urb);
826 /* Dissociate from the USB device */
827 static void dissociate_dev(struct us_data *us)
829 US_DEBUGP("-- %s\n", __FUNCTION__);
833 /* Free the device-related DMA-mapped buffers */
835 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
838 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
841 /* Remove our private data from the interface */
842 usb_set_intfdata(us->pusb_intf, NULL);
845 /* First stage of disconnect processing: stop all commands and remove
847 static void quiesce_and_remove_host(struct us_data *us)
849 /* Prevent new USB transfers, stop the current command, and
850 * interrupt a SCSI-scan or device-reset delay */
851 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
852 usb_stor_stop_transport(us);
853 wake_up(&us->delay_wait);
855 /* It doesn't matter if the SCSI-scanning thread is still running.
856 * The thread will exit when it sees the DISCONNECTING flag. */
858 /* Wait for the current command to finish, then remove the host */
859 down(&us->dev_semaphore);
860 up(&us->dev_semaphore);
862 /* queuecommand won't accept any new commands and the control
863 * thread won't execute a previously-queued command. If there
864 * is such a command pending, complete it with an error. */
866 us->srb->result = DID_NO_CONNECT << 16;
867 scsi_lock(us_to_host(us));
868 us->srb->scsi_done(us->srb);
870 scsi_unlock(us_to_host(us));
873 /* Now we own no commands so it's safe to remove the SCSI host */
874 scsi_remove_host(us_to_host(us));
877 /* Second stage of disconnect processing: deallocate all resources */
878 static void release_everything(struct us_data *us)
880 usb_stor_release_resources(us);
883 /* Drop our reference to the host; the SCSI core will free it
884 * (and "us" along with it) when the refcount becomes 0. */
885 scsi_host_put(us_to_host(us));
888 /* Thread to carry out delayed SCSI-device scanning */
889 static int usb_stor_scan_thread(void * __us)
891 struct us_data *us = (struct us_data *)__us;
894 * This thread doesn't need any user-level access,
895 * so get rid of all our resources.
898 daemonize("usb-stor-scan");
901 /* Acquire a reference to the host, so it won't be deallocated
902 * until we're ready to exit */
903 scsi_host_get(us_to_host(us));
905 /* Signal that we've started the thread */
906 complete(&(us->notify));
909 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
911 /* Wait for the timeout to expire or for a disconnect */
913 printk(KERN_DEBUG "usb-storage: waiting for device "
914 "to settle before scanning\n");
916 wait_event_interruptible_timeout(us->delay_wait,
917 test_bit(US_FLIDX_DISCONNECTING, &us->flags),
923 /* If the device is still connected, perform the scanning */
924 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
926 /* For bulk-only devices, determine the max LUN value */
927 if (us->protocol == US_PR_BULK &&
928 !(us->flags & US_FL_SINGLE_LUN)) {
929 down(&us->dev_semaphore);
930 us->max_lun = usb_stor_Bulk_max_lun(us);
931 up(&us->dev_semaphore);
933 scsi_scan_host(us_to_host(us));
934 printk(KERN_DEBUG "usb-storage: device scan complete\n");
936 /* Should we unbind if no devices were detected? */
939 scsi_host_put(us_to_host(us));
940 complete_and_exit(&threads_gone, 0);
944 /* Probe to see if we can drive a newly-connected USB device */
945 static int storage_probe(struct usb_interface *intf,
946 const struct usb_device_id *id)
948 struct Scsi_Host *host;
950 const int id_index = id - storage_usb_ids;
953 US_DEBUGP("USB Mass Storage device detected\n");
956 * Ask the SCSI layer to allocate a host structure, with extra
957 * space at the end for our private us_data structure.
959 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
961 printk(KERN_WARNING USB_STORAGE
962 "Unable to allocate the scsi host\n");
966 us = host_to_us(host);
967 memset(us, 0, sizeof(struct us_data));
968 init_MUTEX(&(us->dev_semaphore));
969 init_MUTEX_LOCKED(&(us->sema));
970 init_completion(&(us->notify));
971 init_waitqueue_head(&us->delay_wait);
973 /* Associate the us_data structure with the USB device */
974 result = associate_dev(us, intf);
979 * Get the unusual_devs entries and the descriptors
981 * id_index is calculated in the declaration to be the index number
982 * of the match from the usb_device_id table, so we can find the
983 * corresponding entry in the private table.
985 get_device_info(us, id_index);
987 #ifdef CONFIG_USB_STORAGE_SDDR09
988 if (us->protocol == US_PR_EUSB_SDDR09 ||
989 us->protocol == US_PR_DPCM_USB) {
990 /* set the configuration -- STALL is an acceptable response here */
991 if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
992 US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev
993 ->actconfig->desc.bConfigurationValue);
996 result = usb_reset_configuration(us->pusb_dev);
998 US_DEBUGP("Result of usb_reset_configuration is %d\n", result);
999 if (result == -EPIPE) {
1000 US_DEBUGP("-- stall on control interface\n");
1001 } else if (result != 0) {
1002 /* it's not a stall, but another error -- time to bail */
1003 US_DEBUGP("-- Unknown error. Rejecting device\n");
1009 /* Get the transport, protocol, and pipe settings */
1010 result = get_transport(us);
1013 result = get_protocol(us);
1016 result = get_pipes(us);
1020 /* Acquire all the other resources and add the host */
1021 result = usb_stor_acquire_resources(us);
1024 result = scsi_add_host(host, &intf->dev);
1026 printk(KERN_WARNING USB_STORAGE
1027 "Unable to add the scsi host\n");
1031 /* Start up the thread for delayed SCSI-device scanning */
1032 result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM);
1034 printk(KERN_WARNING USB_STORAGE
1035 "Unable to start the device-scanning thread\n");
1036 quiesce_and_remove_host(us);
1039 atomic_inc(&total_threads);
1041 /* Wait for the thread to start */
1042 wait_for_completion(&(us->notify));
1046 /* We come here if there are any problems */
1048 US_DEBUGP("storage_probe() failed\n");
1049 release_everything(us);
1053 /* Handle a disconnect event from the USB core */
1054 static void storage_disconnect(struct usb_interface *intf)
1056 struct us_data *us = usb_get_intfdata(intf);
1058 US_DEBUGP("storage_disconnect() called\n");
1059 quiesce_and_remove_host(us);
1060 release_everything(us);
1063 /***********************************************************************
1064 * Initialization and registration
1065 ***********************************************************************/
1067 static struct usb_driver usb_storage_driver = {
1068 .owner = THIS_MODULE,
1069 .name = "usb-storage",
1070 .probe = storage_probe,
1071 .disconnect = storage_disconnect,
1073 .suspend = storage_suspend,
1074 .resume = storage_resume,
1076 .id_table = storage_usb_ids,
1079 static int __init usb_stor_init(void)
1082 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1084 /* register the driver, return usb_register return code if error */
1085 retval = usb_register(&usb_storage_driver);
1087 printk(KERN_INFO "USB Mass Storage support registered.\n");
1092 static void __exit usb_stor_exit(void)
1094 US_DEBUGP("usb_stor_exit() called\n");
1096 /* Deregister the driver
1097 * This will cause disconnect() to be called for each
1100 US_DEBUGP("-- calling usb_deregister()\n");
1101 usb_deregister(&usb_storage_driver) ;
1103 /* Don't return until all of our control and scanning threads
1104 * have exited. Since each thread signals threads_gone as its
1105 * last act, we have to call wait_for_completion the right number
1108 while (atomic_read(&total_threads) > 0) {
1109 wait_for_completion(&threads_gone);
1110 atomic_dec(&total_threads);
1114 module_init(usb_stor_init);
1115 module_exit(usb_stor_exit);