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>
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_cmnd.h>
57 #include <scsi/scsi_device.h>
61 #include "transport.h"
64 #include "initializers.h"
66 #ifdef CONFIG_USB_STORAGE_HP8200e
67 #include "shuttle_usbat.h"
69 #ifdef CONFIG_USB_STORAGE_SDDR09
72 #ifdef CONFIG_USB_STORAGE_SDDR55
75 #ifdef CONFIG_USB_STORAGE_DPCM
78 #ifdef CONFIG_USB_STORAGE_FREECOM
81 #ifdef CONFIG_USB_STORAGE_ISD200
84 #ifdef CONFIG_USB_STORAGE_DATAFAB
87 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
92 #include <linux/module.h>
93 #include <linux/init.h>
94 #include <linux/slab.h>
96 /* Some informational data */
97 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
98 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
99 MODULE_LICENSE("GPL");
101 static unsigned int delay_use = 5;
102 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
103 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
106 static int storage_probe(struct usb_interface *iface,
107 const struct usb_device_id *id);
109 static void storage_disconnect(struct usb_interface *iface);
111 /* The entries in this table, except for final ones here
112 * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
113 * line for line with the entries of us_unsuaul_dev_list[].
116 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
117 vendorName, productName,useProtocol, useTransport, \
118 initFunction, flags) \
119 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
121 static struct usb_device_id storage_usb_ids [] = {
123 # include "unusual_devs.h"
125 /* Control/Bulk transport for all SubClass values */
126 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CB) },
127 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CB) },
128 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CB) },
129 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CB) },
130 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CB) },
131 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CB) },
133 /* Control/Bulk/Interrupt transport for all SubClass values */
134 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CBI) },
135 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CBI) },
136 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CBI) },
137 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CBI) },
138 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CBI) },
139 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CBI) },
141 /* Bulk-only transport for all SubClass values */
142 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_BULK) },
143 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_BULK) },
144 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_BULK) },
145 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_BULK) },
146 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_BULK) },
147 #if !defined(CONFIG_BLK_DEV_UB) && !defined(CONFIG_BLK_DEV_UB_MODULE)
148 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
151 /* Terminating entry */
155 MODULE_DEVICE_TABLE (usb, storage_usb_ids);
157 /* This is the list of devices we recognize, along with their flag data */
159 /* The vendor name should be kept at eight characters or less, and
160 * the product name should be kept at 16 characters or less. If a device
161 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
162 * normally generated by a device thorugh the INQUIRY response will be
163 * taken from this list, and this is the reason for the above size
164 * restriction. However, if the flag is not present, then you
165 * are free to use as many characters as you like.
169 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
170 vendor_name, product_name, use_protocol, use_transport, \
171 init_function, Flags) \
173 .vendorName = vendor_name, \
174 .productName = product_name, \
175 .useProtocol = use_protocol, \
176 .useTransport = use_transport, \
177 .initFunction = init_function, \
181 static struct us_unusual_dev us_unusual_dev_list[] = {
182 # include "unusual_devs.h"
184 /* Control/Bulk transport for all SubClass values */
185 { .useProtocol = US_SC_RBC,
186 .useTransport = US_PR_CB},
187 { .useProtocol = US_SC_8020,
188 .useTransport = US_PR_CB},
189 { .useProtocol = US_SC_QIC,
190 .useTransport = US_PR_CB},
191 { .useProtocol = US_SC_UFI,
192 .useTransport = US_PR_CB},
193 { .useProtocol = US_SC_8070,
194 .useTransport = US_PR_CB},
195 { .useProtocol = US_SC_SCSI,
196 .useTransport = US_PR_CB},
198 /* Control/Bulk/Interrupt transport for all SubClass values */
199 { .useProtocol = US_SC_RBC,
200 .useTransport = US_PR_CBI},
201 { .useProtocol = US_SC_8020,
202 .useTransport = US_PR_CBI},
203 { .useProtocol = US_SC_QIC,
204 .useTransport = US_PR_CBI},
205 { .useProtocol = US_SC_UFI,
206 .useTransport = US_PR_CBI},
207 { .useProtocol = US_SC_8070,
208 .useTransport = US_PR_CBI},
209 { .useProtocol = US_SC_SCSI,
210 .useTransport = US_PR_CBI},
212 /* Bulk-only transport for all SubClass values */
213 { .useProtocol = US_SC_RBC,
214 .useTransport = US_PR_BULK},
215 { .useProtocol = US_SC_8020,
216 .useTransport = US_PR_BULK},
217 { .useProtocol = US_SC_QIC,
218 .useTransport = US_PR_BULK},
219 { .useProtocol = US_SC_UFI,
220 .useTransport = US_PR_BULK},
221 { .useProtocol = US_SC_8070,
222 .useTransport = US_PR_BULK},
223 #if !defined(CONFIG_BLK_DEV_UB) && !defined(CONFIG_BLK_DEV_UB_MODULE)
224 { .useProtocol = US_SC_SCSI,
225 .useTransport = US_PR_BULK},
228 /* Terminating entry */
232 struct usb_driver usb_storage_driver = {
233 .owner = THIS_MODULE,
234 .name = "usb-storage",
235 .probe = storage_probe,
236 .disconnect = storage_disconnect,
237 .id_table = storage_usb_ids,
241 * fill_inquiry_response takes an unsigned char array (which must
242 * be at least 36 characters) and populates the vendor name,
243 * product name, and revision fields. Then the array is copied
244 * into the SCSI command's response buffer (oddly enough
245 * called request_buffer). data_len contains the length of the
246 * data array, which again must be at least 36.
249 void fill_inquiry_response(struct us_data *us, unsigned char *data,
250 unsigned int data_len)
252 if (data_len<36) // You lose.
255 if(data[0]&0x20) { /* USB device currently not connected. Return
256 peripheral qualifier 001b ("...however, the
257 physical device is not currently connected
258 to this logical unit") and leave vendor and
259 product identification empty. ("If the target
260 does store some of the INQUIRY data on the
261 device, it may return zeros or ASCII spaces
262 (20h) in those fields until the data is
263 available from the device."). */
266 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
267 memcpy(data+8, us->unusual_dev->vendorName,
268 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
269 strlen(us->unusual_dev->vendorName));
270 memcpy(data+16, us->unusual_dev->productName,
271 strlen(us->unusual_dev->productName) > 16 ? 16 :
272 strlen(us->unusual_dev->productName));
273 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
274 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
275 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
276 data[35] = 0x30 + ((bcdDevice) & 0x0F);
279 usb_stor_set_xfer_buf(data, data_len, us->srb);
282 static int usb_stor_control_thread(void * __us)
284 struct us_data *us = (struct us_data *)__us;
285 struct Scsi_Host *host = us->host;
290 * This thread doesn't need any user-level access,
291 * so get rid of all our resources.
293 daemonize("usb-storage");
295 current->flags |= PF_NOFREEZE;
299 /* signal that we've started the thread */
300 complete(&(us->notify));
303 US_DEBUGP("*** thread sleeping.\n");
304 if(down_interruptible(&us->sema))
307 US_DEBUGP("*** thread awakened.\n");
309 /* lock the device pointers */
310 down(&(us->dev_semaphore));
312 /* if us->srb is NULL, we are being asked to exit */
313 if (us->srb == NULL) {
314 US_DEBUGP("-- exit command received\n");
315 up(&(us->dev_semaphore));
319 /* lock access to the state */
322 /* has the command timed out *already* ? */
323 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
324 us->srb->result = DID_ABORT << 16;
328 /* don't do anything if we are disconnecting */
329 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
330 US_DEBUGP("No command during disconnect\n");
331 goto SkipForDisconnect;
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 */
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 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
441 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
442 le16_to_cpu(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 static void get_device_info(struct us_data *us, int id_index)
470 struct usb_device *dev = us->pusb_dev;
471 struct usb_interface_descriptor *idesc =
472 &us->pusb_intf->cur_altsetting->desc;
473 struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
474 struct usb_device_id *id = &storage_usb_ids[id_index];
476 /* Store the entries */
477 us->unusual_dev = unusual_dev;
478 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
479 idesc->bInterfaceSubClass :
480 unusual_dev->useProtocol;
481 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
482 idesc->bInterfaceProtocol :
483 unusual_dev->useTransport;
484 us->flags = unusual_dev->flags;
486 /* Log a message if a non-generic unusual_dev entry contains an
487 * unnecessary subclass or protocol override. This may stimulate
488 * reports from users that will help us remove unneeded entries
489 * from the unusual_devs.h table.
491 if (id->idVendor || id->idProduct) {
492 static char *msgs[3] = {
493 "an unneeded SubClass entry",
494 "an unneeded Protocol entry",
495 "unneeded SubClass and Protocol entries"};
496 struct usb_device_descriptor *ddesc = &dev->descriptor;
499 if (unusual_dev->useProtocol != US_SC_DEVICE &&
500 us->subclass == idesc->bInterfaceSubClass)
502 if (unusual_dev->useTransport != US_PR_DEVICE &&
503 us->protocol == idesc->bInterfaceProtocol)
505 if (msg >= 0 && !(unusual_dev->flags & US_FL_NEED_OVERRIDE))
506 printk(KERN_NOTICE USB_STORAGE "This device "
507 "(%04x,%04x,%04x S %02x P %02x)"
508 " has %s in unusual_devs.h\n"
509 " Please send a copy of this message to "
510 "<linux-usb-devel@lists.sourceforge.net>\n",
511 le16_to_cpu(ddesc->idVendor),
512 le16_to_cpu(ddesc->idProduct),
513 le16_to_cpu(ddesc->bcdDevice),
514 idesc->bInterfaceSubClass,
515 idesc->bInterfaceProtocol,
519 /* Read the device's string descriptors */
520 if (dev->descriptor.iManufacturer)
521 usb_string(dev, dev->descriptor.iManufacturer,
522 us->vendor, sizeof(us->vendor));
523 if (dev->descriptor.iProduct)
524 usb_string(dev, dev->descriptor.iProduct,
525 us->product, sizeof(us->product));
526 if (dev->descriptor.iSerialNumber)
527 usb_string(dev, dev->descriptor.iSerialNumber,
528 us->serial, sizeof(us->serial));
530 /* Use the unusual_dev strings if the device didn't provide them */
531 if (strlen(us->vendor) == 0) {
532 if (unusual_dev->vendorName)
533 strlcpy(us->vendor, unusual_dev->vendorName,
536 strcpy(us->vendor, "Unknown");
538 if (strlen(us->product) == 0) {
539 if (unusual_dev->productName)
540 strlcpy(us->product, unusual_dev->productName,
541 sizeof(us->product));
543 strcpy(us->product, "Unknown");
545 if (strlen(us->serial) == 0)
546 strcpy(us->serial, "None");
548 US_DEBUGP("Vendor: %s, Product: %s\n", us->vendor, us->product);
551 /* Get the transport settings */
552 static int get_transport(struct us_data *us)
554 switch (us->protocol) {
556 us->transport_name = "Control/Bulk";
557 us->transport = usb_stor_CB_transport;
558 us->transport_reset = usb_stor_CB_reset;
563 us->transport_name = "Control/Bulk/Interrupt";
564 us->transport = usb_stor_CBI_transport;
565 us->transport_reset = usb_stor_CB_reset;
570 us->transport_name = "Bulk";
571 us->transport = usb_stor_Bulk_transport;
572 us->transport_reset = usb_stor_Bulk_reset;
575 #ifdef CONFIG_USB_STORAGE_HP8200e
576 case US_PR_SCM_ATAPI:
577 us->transport_name = "SCM/ATAPI";
578 us->transport = hp8200e_transport;
579 us->transport_reset = usb_stor_CB_reset;
584 #ifdef CONFIG_USB_STORAGE_SDDR09
585 case US_PR_EUSB_SDDR09:
586 us->transport_name = "EUSB/SDDR09";
587 us->transport = sddr09_transport;
588 us->transport_reset = usb_stor_CB_reset;
593 #ifdef CONFIG_USB_STORAGE_SDDR55
595 us->transport_name = "SDDR55";
596 us->transport = sddr55_transport;
597 us->transport_reset = sddr55_reset;
602 #ifdef CONFIG_USB_STORAGE_DPCM
604 us->transport_name = "Control/Bulk-EUSB/SDDR09";
605 us->transport = dpcm_transport;
606 us->transport_reset = usb_stor_CB_reset;
611 #ifdef CONFIG_USB_STORAGE_FREECOM
613 us->transport_name = "Freecom";
614 us->transport = freecom_transport;
615 us->transport_reset = usb_stor_freecom_reset;
620 #ifdef CONFIG_USB_STORAGE_DATAFAB
622 us->transport_name = "Datafab Bulk-Only";
623 us->transport = datafab_transport;
624 us->transport_reset = usb_stor_Bulk_reset;
629 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
631 us->transport_name = "Lexar Jumpshot Control/Bulk";
632 us->transport = jumpshot_transport;
633 us->transport_reset = usb_stor_Bulk_reset;
641 US_DEBUGP("Transport: %s\n", us->transport_name);
643 /* fix for single-lun devices */
644 if (us->flags & US_FL_SINGLE_LUN)
649 /* Get the protocol settings */
650 static int get_protocol(struct us_data *us)
652 switch (us->subclass) {
654 us->protocol_name = "Reduced Block Commands (RBC)";
655 us->proto_handler = usb_stor_transparent_scsi_command;
659 us->protocol_name = "8020i";
660 us->proto_handler = usb_stor_ATAPI_command;
665 us->protocol_name = "QIC-157";
666 us->proto_handler = usb_stor_qic157_command;
671 us->protocol_name = "8070i";
672 us->proto_handler = usb_stor_ATAPI_command;
677 us->protocol_name = "Transparent SCSI";
678 us->proto_handler = usb_stor_transparent_scsi_command;
682 us->protocol_name = "Uniform Floppy Interface (UFI)";
683 us->proto_handler = usb_stor_ufi_command;
686 #ifdef CONFIG_USB_STORAGE_ISD200
688 us->protocol_name = "ISD200 ATA/ATAPI";
689 us->proto_handler = isd200_ata_command;
696 US_DEBUGP("Protocol: %s\n", us->protocol_name);
700 /* Get the pipe settings */
701 static int get_pipes(struct us_data *us)
703 struct usb_host_interface *altsetting =
704 us->pusb_intf->cur_altsetting;
706 struct usb_endpoint_descriptor *ep;
707 struct usb_endpoint_descriptor *ep_in = NULL;
708 struct usb_endpoint_descriptor *ep_out = NULL;
709 struct usb_endpoint_descriptor *ep_int = NULL;
712 * Find the endpoints we need.
713 * We are expecting a minimum of 2 endpoints - in and out (bulk).
714 * An optional interrupt is OK (necessary for CBI protocol).
715 * We will ignore any others.
717 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
718 ep = &altsetting->endpoint[i].desc;
720 /* Is it a BULK endpoint? */
721 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
722 == USB_ENDPOINT_XFER_BULK) {
723 /* BULK in or out? */
724 if (ep->bEndpointAddress & USB_DIR_IN)
730 /* Is it an interrupt endpoint? */
731 else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
732 == USB_ENDPOINT_XFER_INT) {
737 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
738 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
742 /* Calculate and store the pipe values */
743 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
744 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
745 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
746 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
747 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
748 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
750 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
751 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
752 us->ep_bInterval = ep_int->bInterval;
757 /* Initialize all the dynamic resources we need */
758 static int usb_stor_acquire_resources(struct us_data *us)
762 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
763 if (!us->current_urb) {
764 US_DEBUGP("URB allocation failed\n");
768 /* Lock the device while we carry out the next two operations */
769 down(&us->dev_semaphore);
771 /* For bulk-only devices, determine the max LUN value */
772 if (us->protocol == US_PR_BULK) {
773 p = usb_stor_Bulk_max_lun(us);
775 up(&us->dev_semaphore);
781 /* Just before we start our control thread, initialize
782 * the device if it needs initialization */
783 if (us->unusual_dev->initFunction)
784 us->unusual_dev->initFunction(us);
786 up(&us->dev_semaphore);
789 * Since this is a new device, we need to register a SCSI
790 * host definition with the higher SCSI layers.
792 us->host = scsi_host_alloc(&usb_stor_host_template, sizeof(us));
794 printk(KERN_WARNING USB_STORAGE
795 "Unable to allocate the scsi host\n");
799 /* Set the hostdata to prepare for scanning */
800 us->host->hostdata[0] = (unsigned long) us;
802 /* Start up our control thread */
803 p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
805 printk(KERN_WARNING USB_STORAGE
806 "Unable to start control thread\n");
811 /* Wait for the thread to start */
812 wait_for_completion(&(us->notify));
817 /* Release all our dynamic resources */
818 void usb_stor_release_resources(struct us_data *us)
820 US_DEBUGP("-- %s\n", __FUNCTION__);
822 /* Kill the control thread. The SCSI host must already have been
823 * removed so it won't try to queue any more commands.
827 /* Wait for the thread to be idle */
828 down(&us->dev_semaphore);
829 US_DEBUGP("-- sending exit command to thread\n");
831 /* If the SCSI midlayer queued a final command just before
832 * scsi_remove_host() was called, us->srb might not be
833 * NULL. We can overwrite it safely, because the midlayer
834 * will not wait for the command to finish. Also the
835 * control thread will already have been awakened.
836 * That's okay, an extra up() on us->sema won't hurt.
838 * Enqueue the command, wake up the thread, and wait for
839 * notification that it has exited.
843 scsi_unlock(us->host);
844 up(&us->dev_semaphore);
847 wait_for_completion(&us->notify);
850 /* Call the destructor routine, if it exists */
851 if (us->extra_destructor) {
852 US_DEBUGP("-- calling extra_destructor()\n");
853 us->extra_destructor(us->extra);
856 /* Finish the host removal sequence */
858 scsi_host_put(us->host);
860 /* Free the extra data and the URB */
864 usb_free_urb(us->current_urb);
868 /* Dissociate from the USB device */
869 static void dissociate_dev(struct us_data *us)
871 US_DEBUGP("-- %s\n", __FUNCTION__);
873 /* Free the device-related DMA-mapped buffers */
875 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
878 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
881 /* Remove our private data from the interface */
882 usb_set_intfdata(us->pusb_intf, NULL);
884 /* Free the structure itself */
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");
902 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
904 /* Wait for the timeout to expire or for a disconnect */
906 printk(KERN_DEBUG "usb-storage: waiting for device "
907 "to settle before scanning\n");
909 wait_event_interruptible_timeout(us->scsi_scan_wait,
910 test_bit(US_FLIDX_DISCONNECTING, &us->flags),
912 if (current->flags & PF_FREEZE) {
913 refrigerator(PF_FREEZE);
918 /* If the device is still connected, perform the scanning */
919 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
920 scsi_scan_host(us->host);
921 printk(KERN_DEBUG "usb-storage: device scan complete\n");
924 complete_and_exit(&us->scsi_scan_done, 0);
928 /* Probe to see if we can drive a newly-connected USB device */
929 static int storage_probe(struct usb_interface *intf,
930 const struct usb_device_id *id)
933 const int id_index = id - storage_usb_ids;
936 US_DEBUGP("USB Mass Storage device detected\n");
938 /* Allocate the us_data structure and initialize the mutexes */
939 us = (struct us_data *) kmalloc(sizeof(*us), GFP_KERNEL);
941 printk(KERN_WARNING USB_STORAGE "Out of memory\n");
944 memset(us, 0, sizeof(struct us_data));
945 init_MUTEX(&(us->dev_semaphore));
946 init_MUTEX_LOCKED(&(us->sema));
947 init_completion(&(us->notify));
948 init_waitqueue_head(&us->dev_reset_wait);
949 init_waitqueue_head(&us->scsi_scan_wait);
950 init_completion(&us->scsi_scan_done);
952 /* Associate the us_data structure with the USB device */
953 result = associate_dev(us, intf);
958 * Get the unusual_devs entries and the descriptors
960 * id_index is calculated in the declaration to be the index number
961 * of the match from the usb_device_id table, so we can find the
962 * corresponding entry in the private table.
964 get_device_info(us, id_index);
966 #ifdef CONFIG_USB_STORAGE_SDDR09
967 if (us->protocol == US_PR_EUSB_SDDR09 ||
968 us->protocol == US_PR_DPCM_USB) {
969 /* set the configuration -- STALL is an acceptable response here */
970 if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
971 US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev
972 ->actconfig->desc.bConfigurationValue);
975 result = usb_reset_configuration(us->pusb_dev);
977 US_DEBUGP("Result of usb_reset_configuration is %d\n", result);
978 if (result == -EPIPE) {
979 US_DEBUGP("-- stall on control interface\n");
980 } else if (result != 0) {
981 /* it's not a stall, but another error -- time to bail */
982 US_DEBUGP("-- Unknown error. Rejecting device\n");
988 /* Get the transport, protocol, and pipe settings */
989 result = get_transport(us);
992 result = get_protocol(us);
995 result = get_pipes(us);
999 /* Acquire all the other resources and add the host */
1000 result = usb_stor_acquire_resources(us);
1003 result = scsi_add_host(us->host, &intf->dev);
1005 printk(KERN_WARNING USB_STORAGE
1006 "Unable to add the scsi host\n");
1010 /* Start up the thread for delayed SCSI-device scanning */
1011 result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM);
1013 printk(KERN_WARNING USB_STORAGE
1014 "Unable to start the device-scanning thread\n");
1015 scsi_remove_host(us->host);
1021 /* We come here if there are any problems */
1023 US_DEBUGP("storage_probe() failed\n");
1024 usb_stor_release_resources(us);
1029 /* Handle a disconnect event from the USB core */
1030 static void storage_disconnect(struct usb_interface *intf)
1032 struct us_data *us = usb_get_intfdata(intf);
1034 US_DEBUGP("storage_disconnect() called\n");
1036 /* Prevent new USB transfers, stop the current command, and
1037 * interrupt a device-reset delay */
1038 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
1039 usb_stor_stop_transport(us);
1040 wake_up(&us->dev_reset_wait);
1042 /* Interrupt the SCSI-device-scanning thread's time delay, and
1043 * wait for the thread to finish */
1044 wake_up(&us->scsi_scan_wait);
1045 wait_for_completion(&us->scsi_scan_done);
1047 /* Wait for the current command to finish, then remove the host */
1048 down(&us->dev_semaphore);
1049 up(&us->dev_semaphore);
1050 scsi_remove_host(us->host);
1052 /* Wait for everything to become idle and release all our resources */
1053 usb_stor_release_resources(us);
1057 /***********************************************************************
1058 * Initialization and registration
1059 ***********************************************************************/
1061 static int __init usb_stor_init(void)
1064 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1066 /* register the driver, return usb_register return code if error */
1067 retval = usb_register(&usb_storage_driver);
1069 printk(KERN_INFO "USB Mass Storage support registered.\n");
1074 static void __exit usb_stor_exit(void)
1076 US_DEBUGP("usb_stor_exit() called\n");
1078 /* Deregister the driver
1079 * This will cause disconnect() to be called for each
1082 US_DEBUGP("-- calling usb_deregister()\n");
1083 usb_deregister(&usb_storage_driver) ;
1086 module_init(usb_stor_init);
1087 module_exit(usb_stor_exit);