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>
55 #include "transport.h"
58 #include "initializers.h"
60 #ifdef CONFIG_USB_STORAGE_HP8200e
61 #include "shuttle_usbat.h"
63 #ifdef CONFIG_USB_STORAGE_SDDR09
66 #ifdef CONFIG_USB_STORAGE_SDDR55
69 #ifdef CONFIG_USB_STORAGE_DPCM
72 #ifdef CONFIG_USB_STORAGE_FREECOM
75 #ifdef CONFIG_USB_STORAGE_ISD200
78 #ifdef CONFIG_USB_STORAGE_DATAFAB
81 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
86 #include <linux/module.h>
87 #include <linux/init.h>
88 #include <linux/slab.h>
90 /* Some informational data */
91 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
92 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
93 MODULE_LICENSE("GPL");
95 static int storage_probe(struct usb_interface *iface,
96 const struct usb_device_id *id);
98 static void storage_disconnect(struct usb_interface *iface);
100 /* The entries in this table, except for final ones here
101 * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
102 * line for line with the entries of us_unsuaul_dev_list[].
105 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
106 vendorName, productName,useProtocol, useTransport, \
107 initFunction, flags) \
108 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
110 static struct usb_device_id storage_usb_ids [] = {
112 # include "unusual_devs.h"
114 /* Control/Bulk transport for all SubClass values */
115 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CB) },
116 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CB) },
117 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CB) },
118 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CB) },
119 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CB) },
120 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CB) },
122 /* Control/Bulk/Interrupt transport for all SubClass values */
123 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CBI) },
124 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CBI) },
125 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CBI) },
126 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CBI) },
127 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CBI) },
128 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CBI) },
130 /* Bulk-only transport for all SubClass values */
131 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_BULK) },
132 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_BULK) },
133 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_BULK) },
134 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_BULK) },
135 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_BULK) },
136 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
138 /* Terminating entry */
142 MODULE_DEVICE_TABLE (usb, storage_usb_ids);
144 /* This is the list of devices we recognize, along with their flag data */
146 /* The vendor name should be kept at eight characters or less, and
147 * the product name should be kept at 16 characters or less. If a device
148 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
149 * normally generated by a device thorugh the INQUIRY response will be
150 * taken from this list, and this is the reason for the above size
151 * restriction. However, if the flag is not present, then you
152 * are free to use as many characters as you like.
156 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
157 vendor_name, product_name, use_protocol, use_transport, \
158 init_function, Flags) \
160 .vendorName = vendor_name, \
161 .productName = product_name, \
162 .useProtocol = use_protocol, \
163 .useTransport = use_transport, \
164 .initFunction = init_function, \
168 static struct us_unusual_dev us_unusual_dev_list[] = {
169 # include "unusual_devs.h"
171 /* Control/Bulk transport for all SubClass values */
172 { .useProtocol = US_SC_RBC,
173 .useTransport = US_PR_CB},
174 { .useProtocol = US_SC_8020,
175 .useTransport = US_PR_CB},
176 { .useProtocol = US_SC_QIC,
177 .useTransport = US_PR_CB},
178 { .useProtocol = US_SC_UFI,
179 .useTransport = US_PR_CB},
180 { .useProtocol = US_SC_8070,
181 .useTransport = US_PR_CB},
182 { .useProtocol = US_SC_SCSI,
183 .useTransport = US_PR_CB},
185 /* Control/Bulk/Interrupt transport for all SubClass values */
186 { .useProtocol = US_SC_RBC,
187 .useTransport = US_PR_CBI},
188 { .useProtocol = US_SC_8020,
189 .useTransport = US_PR_CBI},
190 { .useProtocol = US_SC_QIC,
191 .useTransport = US_PR_CBI},
192 { .useProtocol = US_SC_UFI,
193 .useTransport = US_PR_CBI},
194 { .useProtocol = US_SC_8070,
195 .useTransport = US_PR_CBI},
196 { .useProtocol = US_SC_SCSI,
197 .useTransport = US_PR_CBI},
199 /* Bulk-only transport for all SubClass values */
200 { .useProtocol = US_SC_RBC,
201 .useTransport = US_PR_BULK},
202 { .useProtocol = US_SC_8020,
203 .useTransport = US_PR_BULK},
204 { .useProtocol = US_SC_QIC,
205 .useTransport = US_PR_BULK},
206 { .useProtocol = US_SC_UFI,
207 .useTransport = US_PR_BULK},
208 { .useProtocol = US_SC_8070,
209 .useTransport = US_PR_BULK},
210 { .useProtocol = US_SC_SCSI,
211 .useTransport = US_PR_BULK},
213 /* Terminating entry */
217 struct usb_driver usb_storage_driver = {
218 .owner = THIS_MODULE,
219 .name = "usb-storage",
220 .probe = storage_probe,
221 .disconnect = storage_disconnect,
222 .id_table = storage_usb_ids,
226 * fill_inquiry_response takes an unsigned char array (which must
227 * be at least 36 characters) and populates the vendor name,
228 * product name, and revision fields. Then the array is copied
229 * into the SCSI command's response buffer (oddly enough
230 * called request_buffer). data_len contains the length of the
231 * data array, which again must be at least 36.
234 void fill_inquiry_response(struct us_data *us, unsigned char *data,
235 unsigned int data_len)
237 if (data_len<36) // You lose.
240 if(data[0]&0x20) { /* USB device currently not connected. Return
241 peripheral qualifier 001b ("...however, the
242 physical device is not currently connected
243 to this logical unit") and leave vendor and
244 product identification empty. ("If the target
245 does store some of the INQUIRY data on the
246 device, it may return zeros or ASCII spaces
247 (20h) in those fields until the data is
248 available from the device."). */
251 memcpy(data+8, us->unusual_dev->vendorName,
252 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
253 strlen(us->unusual_dev->vendorName));
254 memcpy(data+16, us->unusual_dev->productName,
255 strlen(us->unusual_dev->productName) > 16 ? 16 :
256 strlen(us->unusual_dev->productName));
257 data[32] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>12) & 0x0F);
258 data[33] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>8) & 0x0F);
259 data[34] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>4) & 0x0F);
260 data[35] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice) & 0x0F);
263 usb_stor_set_xfer_buf(data, data_len, us->srb);
266 static int usb_stor_control_thread(void * __us)
268 struct us_data *us = (struct us_data *)__us;
269 struct Scsi_Host *host = us->host;
274 * This thread doesn't need any user-level access,
275 * so get rid of all our resources.
277 daemonize("usb-storage");
279 current->flags |= PF_NOFREEZE;
283 /* signal that we've started the thread */
284 complete(&(us->notify));
287 US_DEBUGP("*** thread sleeping.\n");
288 if(down_interruptible(&us->sema))
291 US_DEBUGP("*** thread awakened.\n");
293 /* lock the device pointers */
294 down(&(us->dev_semaphore));
296 /* if us->srb is NULL, we are being asked to exit */
297 if (us->srb == NULL) {
298 US_DEBUGP("-- exit command received\n");
299 up(&(us->dev_semaphore));
303 /* lock access to the state */
306 /* has the command been aborted *already* ? */
307 if (us->sm_state == US_STATE_ABORTING) {
308 us->srb->result = DID_ABORT << 16;
312 /* don't do anything if we are disconnecting */
313 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
314 US_DEBUGP("No command during disconnect\n");
315 goto SkipForDisconnect;
318 /* set the state and release the lock */
319 us->sm_state = US_STATE_RUNNING;
322 /* reject the command if the direction indicator
325 if (us->srb->sc_data_direction == SCSI_DATA_UNKNOWN) {
326 US_DEBUGP("UNKNOWN data direction\n");
327 us->srb->result = DID_ERROR << 16;
330 /* reject if target != 0 or if LUN is higher than
331 * the maximum known LUN
333 else if (us->srb->device->id &&
334 !(us->flags & US_FL_SCM_MULT_TARG)) {
335 US_DEBUGP("Bad target number (%d:%d)\n",
336 us->srb->device->id, us->srb->device->lun);
337 us->srb->result = DID_BAD_TARGET << 16;
340 else if (us->srb->device->lun > us->max_lun) {
341 US_DEBUGP("Bad LUN (%d:%d)\n",
342 us->srb->device->id, us->srb->device->lun);
343 us->srb->result = DID_BAD_TARGET << 16;
346 /* Handle those devices which need us to fake
347 * their inquiry data */
348 else if ((us->srb->cmnd[0] == INQUIRY) &&
349 (us->flags & US_FL_FIX_INQUIRY)) {
350 unsigned char data_ptr[36] = {
351 0x00, 0x80, 0x02, 0x02,
352 0x1F, 0x00, 0x00, 0x00};
354 US_DEBUGP("Faking INQUIRY command\n");
355 fill_inquiry_response(us, data_ptr, 36);
356 us->srb->result = SAM_STAT_GOOD;
359 /* we've got a command, let's do it! */
361 US_DEBUG(usb_stor_show_command(us->srb));
362 us->proto_handler(us->srb, us);
365 /* lock access to the state */
368 /* indicate that the command is done */
369 if (us->srb->result != DID_ABORT << 16) {
370 US_DEBUGP("scsi cmd done, result=0x%x\n",
372 us->srb->scsi_done(us->srb);
375 US_DEBUGP("scsi command aborted\n");
378 /* If an abort request was received we need to signal that
379 * the abort has finished. The proper test for this is
380 * sm_state == US_STATE_ABORTING, not srb->result == DID_ABORT,
381 * because an abort request might be received after all the
382 * USB processing was complete. */
383 if (us->sm_state == US_STATE_ABORTING)
384 complete(&(us->notify));
386 /* empty the queue, reset the state, and release the lock */
389 us->sm_state = US_STATE_IDLE;
392 /* unlock the device pointers */
393 up(&(us->dev_semaphore));
396 /* notify the exit routine that we're actually exiting now
398 * complete()/wait_for_completion() is similar to up()/down(),
399 * except that complete() is safe in the case where the structure
400 * is getting deleted in a parallel mode of execution (i.e. just
401 * after the down() -- that's necessary for the thread-shutdown
404 * complete_and_exit() goes even further than this -- it is safe in
405 * the case that the thread of the caller is going away (not just
406 * the structure) -- this is necessary for the module-remove case.
407 * This is important in preemption kernels, which transfer the flow
408 * of execution immediately upon a complete().
410 complete_and_exit(&(us->notify), 0);
413 /***********************************************************************
414 * Device probing and disconnecting
415 ***********************************************************************/
417 /* Associate our private data with the USB device */
418 static int associate_dev(struct us_data *us, struct usb_interface *intf)
420 US_DEBUGP("-- %s\n", __FUNCTION__);
422 /* Fill in the device-related fields */
423 us->pusb_dev = interface_to_usbdev(intf);
424 us->pusb_intf = intf;
425 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
427 /* Store our private data in the interface */
428 usb_set_intfdata(intf, us);
430 /* Allocate the device-related DMA-mapped buffers */
431 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
432 GFP_KERNEL, &us->cr_dma);
434 US_DEBUGP("usb_ctrlrequest allocation failed\n");
438 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
439 GFP_KERNEL, &us->iobuf_dma);
441 US_DEBUGP("I/O buffer allocation failed\n");
447 /* Get the unusual_devs entries and the string descriptors */
448 static void get_device_info(struct us_data *us, int id_index)
450 struct usb_device *dev = us->pusb_dev;
451 struct usb_interface_descriptor *idesc =
452 &us->pusb_intf->cur_altsetting->desc;
453 struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
454 struct usb_device_id *id = &storage_usb_ids[id_index];
456 if (unusual_dev->vendorName)
457 US_DEBUGP("Vendor: %s\n", unusual_dev->vendorName);
458 if (unusual_dev->productName)
459 US_DEBUGP("Product: %s\n", unusual_dev->productName);
461 /* Store the entries */
462 us->unusual_dev = unusual_dev;
463 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
464 idesc->bInterfaceSubClass :
465 unusual_dev->useProtocol;
466 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
467 idesc->bInterfaceProtocol :
468 unusual_dev->useTransport;
469 us->flags = unusual_dev->flags;
471 /* Log a message if a non-generic unusual_dev entry contains an
472 * unnecessary subclass or protocol override. This may stimulate
473 * reports from users that will help us remove unneeded entries
474 * from the unusual_devs.h table.
476 if (id->idVendor || id->idProduct) {
477 static char *msgs[3] = {
478 "an unneeded SubClass entry",
479 "an unneeded Protocol entry",
480 "unneeded SubClass and Protocol entries"};
481 struct usb_device_descriptor *ddesc = &dev->descriptor;
484 if (unusual_dev->useProtocol != US_SC_DEVICE &&
485 us->subclass == idesc->bInterfaceSubClass)
487 if (unusual_dev->useTransport != US_PR_DEVICE &&
488 us->protocol == idesc->bInterfaceProtocol)
490 if (msg >= 0 && !(unusual_dev->flags & US_FL_NEED_OVERRIDE))
491 printk(KERN_NOTICE USB_STORAGE "This device "
492 "(%04x,%04x,%04x S %02x P %02x)"
493 " has %s in unusual_devs.h\n"
494 " Please send a copy of this message to "
495 "<linux-usb-devel@lists.sourceforge.net>\n",
496 ddesc->idVendor, ddesc->idProduct,
498 idesc->bInterfaceSubClass,
499 idesc->bInterfaceProtocol,
503 /* Read the device's string descriptors */
504 if (dev->descriptor.iManufacturer)
505 usb_string(dev, dev->descriptor.iManufacturer,
506 us->vendor, sizeof(us->vendor));
507 if (dev->descriptor.iProduct)
508 usb_string(dev, dev->descriptor.iProduct,
509 us->product, sizeof(us->product));
510 if (dev->descriptor.iSerialNumber)
511 usb_string(dev, dev->descriptor.iSerialNumber,
512 us->serial, sizeof(us->serial));
514 /* Use the unusual_dev strings if the device didn't provide them */
515 if (strlen(us->vendor) == 0) {
516 if (unusual_dev->vendorName)
517 strlcpy(us->vendor, unusual_dev->vendorName,
520 strcpy(us->vendor, "Unknown");
522 if (strlen(us->product) == 0) {
523 if (unusual_dev->productName)
524 strlcpy(us->product, unusual_dev->productName,
525 sizeof(us->product));
527 strcpy(us->product, "Unknown");
529 if (strlen(us->serial) == 0)
530 strcpy(us->serial, "None");
533 /* Get the transport settings */
534 static int get_transport(struct us_data *us)
536 switch (us->protocol) {
538 us->transport_name = "Control/Bulk";
539 us->transport = usb_stor_CB_transport;
540 us->transport_reset = usb_stor_CB_reset;
545 us->transport_name = "Control/Bulk/Interrupt";
546 us->transport = usb_stor_CBI_transport;
547 us->transport_reset = usb_stor_CB_reset;
552 us->transport_name = "Bulk";
553 us->transport = usb_stor_Bulk_transport;
554 us->transport_reset = usb_stor_Bulk_reset;
557 #ifdef CONFIG_USB_STORAGE_HP8200e
558 case US_PR_SCM_ATAPI:
559 us->transport_name = "SCM/ATAPI";
560 us->transport = hp8200e_transport;
561 us->transport_reset = usb_stor_CB_reset;
566 #ifdef CONFIG_USB_STORAGE_SDDR09
567 case US_PR_EUSB_SDDR09:
568 us->transport_name = "EUSB/SDDR09";
569 us->transport = sddr09_transport;
570 us->transport_reset = usb_stor_CB_reset;
575 #ifdef CONFIG_USB_STORAGE_SDDR55
577 us->transport_name = "SDDR55";
578 us->transport = sddr55_transport;
579 us->transport_reset = sddr55_reset;
584 #ifdef CONFIG_USB_STORAGE_DPCM
586 us->transport_name = "Control/Bulk-EUSB/SDDR09";
587 us->transport = dpcm_transport;
588 us->transport_reset = usb_stor_CB_reset;
593 #ifdef CONFIG_USB_STORAGE_FREECOM
595 us->transport_name = "Freecom";
596 us->transport = freecom_transport;
597 us->transport_reset = usb_stor_freecom_reset;
602 #ifdef CONFIG_USB_STORAGE_DATAFAB
604 us->transport_name = "Datafab Bulk-Only";
605 us->transport = datafab_transport;
606 us->transport_reset = usb_stor_Bulk_reset;
611 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
613 us->transport_name = "Lexar Jumpshot Control/Bulk";
614 us->transport = jumpshot_transport;
615 us->transport_reset = usb_stor_Bulk_reset;
623 US_DEBUGP("Transport: %s\n", us->transport_name);
625 /* fix for single-lun devices */
626 if (us->flags & US_FL_SINGLE_LUN)
631 /* Get the protocol settings */
632 static int get_protocol(struct us_data *us)
634 switch (us->subclass) {
636 us->protocol_name = "Reduced Block Commands (RBC)";
637 us->proto_handler = usb_stor_transparent_scsi_command;
641 us->protocol_name = "8020i";
642 us->proto_handler = usb_stor_ATAPI_command;
647 us->protocol_name = "QIC-157";
648 us->proto_handler = usb_stor_qic157_command;
653 us->protocol_name = "8070i";
654 us->proto_handler = usb_stor_ATAPI_command;
659 us->protocol_name = "Transparent SCSI";
660 us->proto_handler = usb_stor_transparent_scsi_command;
664 us->protocol_name = "Uniform Floppy Interface (UFI)";
665 us->proto_handler = usb_stor_ufi_command;
668 #ifdef CONFIG_USB_STORAGE_ISD200
670 us->protocol_name = "ISD200 ATA/ATAPI";
671 us->proto_handler = isd200_ata_command;
678 US_DEBUGP("Protocol: %s\n", us->protocol_name);
682 /* Get the pipe settings */
683 static int get_pipes(struct us_data *us)
685 struct usb_host_interface *altsetting =
686 us->pusb_intf->cur_altsetting;
688 struct usb_endpoint_descriptor *ep;
689 struct usb_endpoint_descriptor *ep_in = NULL;
690 struct usb_endpoint_descriptor *ep_out = NULL;
691 struct usb_endpoint_descriptor *ep_int = NULL;
694 * Find the endpoints we need.
695 * We are expecting a minimum of 2 endpoints - in and out (bulk).
696 * An optional interrupt is OK (necessary for CBI protocol).
697 * We will ignore any others.
699 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
700 ep = &altsetting->endpoint[i].desc;
702 /* Is it a BULK endpoint? */
703 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
704 == USB_ENDPOINT_XFER_BULK) {
705 /* BULK in or out? */
706 if (ep->bEndpointAddress & USB_DIR_IN)
712 /* Is it an interrupt endpoint? */
713 else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
714 == USB_ENDPOINT_XFER_INT) {
718 US_DEBUGP("Endpoints: In: 0x%p Out: 0x%p Int: 0x%p (Period %d)\n",
719 ep_in, ep_out, ep_int, ep_int ? ep_int->bInterval : 0);
721 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
722 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
726 /* Calculate and store the pipe values */
727 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
728 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
729 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
730 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
731 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
732 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
734 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
735 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
736 us->ep_bInterval = ep_int->bInterval;
741 /* Initialize all the dynamic resources we need */
742 static int usb_stor_acquire_resources(struct us_data *us)
746 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
747 if (!us->current_urb) {
748 US_DEBUGP("URB allocation failed\n");
752 /* Lock the device while we carry out the next two operations */
753 down(&us->dev_semaphore);
755 /* For bulk-only devices, determine the max LUN value */
756 if (us->protocol == US_PR_BULK) {
757 p = usb_stor_Bulk_max_lun(us);
759 up(&us->dev_semaphore);
765 /* Just before we start our control thread, initialize
766 * the device if it needs initialization */
767 if (us->unusual_dev->initFunction)
768 us->unusual_dev->initFunction(us);
770 up(&us->dev_semaphore);
773 * Since this is a new device, we need to register a SCSI
774 * host definition with the higher SCSI layers.
776 us->host = scsi_host_alloc(&usb_stor_host_template, sizeof(us));
778 printk(KERN_WARNING USB_STORAGE
779 "Unable to allocate the scsi host\n");
783 /* Set the hostdata to prepare for scanning */
784 us->host->hostdata[0] = (unsigned long) us;
786 /* Start up our control thread */
787 us->sm_state = US_STATE_IDLE;
788 p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
790 printk(KERN_WARNING USB_STORAGE
791 "Unable to start control thread\n");
796 /* Wait for the thread to start */
797 wait_for_completion(&(us->notify));
802 /* Release all our dynamic resources */
803 void usb_stor_release_resources(struct us_data *us)
805 US_DEBUGP("-- %s\n", __FUNCTION__);
807 /* Kill the control thread. The SCSI host must already have been
808 * removed so it won't try to queue any more commands.
812 /* Wait for the thread to be idle */
813 down(&us->dev_semaphore);
814 US_DEBUGP("-- sending exit command to thread\n");
815 BUG_ON(us->sm_state != US_STATE_IDLE);
817 /* If the SCSI midlayer queued a final command just before
818 * scsi_remove_host() was called, us->srb might not be
819 * NULL. We can overwrite it safely, because the midlayer
820 * will not wait for the command to finish. Also the
821 * control thread will already have been awakened.
822 * That's okay, an extra up() on us->sema won't hurt.
824 * Enqueue the command, wake up the thread, and wait for
825 * notification that it has exited.
829 scsi_unlock(us->host);
830 up(&us->dev_semaphore);
833 wait_for_completion(&us->notify);
836 /* Call the destructor routine, if it exists */
837 if (us->extra_destructor) {
838 US_DEBUGP("-- calling extra_destructor()\n");
839 us->extra_destructor(us->extra);
842 /* Finish the host removal sequence */
844 scsi_host_put(us->host);
846 /* Free the extra data and the URB */
850 usb_free_urb(us->current_urb);
854 /* Dissociate from the USB device */
855 static void dissociate_dev(struct us_data *us)
857 US_DEBUGP("-- %s\n", __FUNCTION__);
859 /* Free the device-related DMA-mapped buffers */
861 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
864 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
867 /* Remove our private data from the interface */
868 usb_set_intfdata(us->pusb_intf, NULL);
870 /* Free the structure itself */
874 /* Probe to see if we can drive a newly-connected USB device */
875 static int storage_probe(struct usb_interface *intf,
876 const struct usb_device_id *id)
879 const int id_index = id - storage_usb_ids;
882 US_DEBUGP("USB Mass Storage device detected\n");
883 US_DEBUGP("altsetting is %d, id_index is %d\n",
884 intf->cur_altsetting->desc.bAlternateSetting,
887 /* Allocate the us_data structure and initialize the mutexes */
888 us = (struct us_data *) kmalloc(sizeof(*us), GFP_KERNEL);
890 printk(KERN_WARNING USB_STORAGE "Out of memory\n");
893 memset(us, 0, sizeof(struct us_data));
894 init_MUTEX(&(us->dev_semaphore));
895 init_MUTEX_LOCKED(&(us->sema));
896 init_completion(&(us->notify));
897 init_waitqueue_head(&us->dev_reset_wait);
899 /* Associate the us_data structure with the USB device */
900 result = associate_dev(us, intf);
905 * Get the unusual_devs entries and the descriptors
907 * id_index is calculated in the declaration to be the index number
908 * of the match from the usb_device_id table, so we can find the
909 * corresponding entry in the private table.
911 get_device_info(us, id_index);
913 #ifdef CONFIG_USB_STORAGE_SDDR09
914 if (us->protocol == US_PR_EUSB_SDDR09 ||
915 us->protocol == US_PR_DPCM_USB) {
916 /* set the configuration -- STALL is an acceptable response here */
917 if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
918 US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev
919 ->actconfig->desc.bConfigurationValue);
922 result = usb_reset_configuration(us->pusb_dev);
924 US_DEBUGP("Result of usb_reset_configuration is %d\n", result);
925 if (result == -EPIPE) {
926 US_DEBUGP("-- stall on control interface\n");
927 } else if (result != 0) {
928 /* it's not a stall, but another error -- time to bail */
929 US_DEBUGP("-- Unknown error. Rejecting device\n");
935 /* Get the transport, protocol, and pipe settings */
936 result = get_transport(us);
939 result = get_protocol(us);
942 result = get_pipes(us);
946 /* Acquire all the other resources */
947 result = usb_stor_acquire_resources(us);
951 /* Finally, add the host (this does SCSI device scanning) */
952 result = scsi_add_host(us->host, &intf->dev);
954 printk(KERN_WARNING USB_STORAGE
955 "Unable to add the scsi host\n");
959 scsi_scan_host(us->host);
962 "USB Mass Storage device found at %d\n", us->pusb_dev->devnum);
965 /* We come here if there are any problems */
967 US_DEBUGP("storage_probe() failed\n");
968 usb_stor_release_resources(us);
973 /* Handle a disconnect event from the USB core */
974 static void storage_disconnect(struct usb_interface *intf)
976 struct us_data *us = usb_get_intfdata(intf);
978 US_DEBUGP("storage_disconnect() called\n");
980 /* Prevent new USB transfers, stop the current command, and
981 * interrupt a device-reset delay */
982 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
983 usb_stor_stop_transport(us);
984 wake_up(&us->dev_reset_wait);
986 /* Wait for the current command to finish, then remove the host */
987 down(&us->dev_semaphore);
988 up(&us->dev_semaphore);
989 scsi_remove_host(us->host);
991 /* Wait for everything to become idle and release all our resources */
992 usb_stor_release_resources(us);
996 /***********************************************************************
997 * Initialization and registration
998 ***********************************************************************/
1000 static int __init usb_stor_init(void)
1003 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1005 /* register the driver, return usb_register return code if error */
1006 retval = usb_register(&usb_storage_driver);
1011 printk(KERN_INFO "USB Mass Storage support registered.\n");
1016 static void __exit usb_stor_exit(void)
1018 US_DEBUGP("usb_stor_exit() called\n");
1020 /* Deregister the driver
1021 * This will cause disconnect() to be called for each
1024 US_DEBUGP("-- calling usb_deregister()\n");
1025 usb_deregister(&usb_storage_driver) ;
1028 module_init(usb_stor_init);
1029 module_exit(usb_stor_exit);