[PATCH] PATCH: usb-storage: allocate separate sense buffer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / storage / usb.c
blobeb901817b7d20626e1b1545bb51ec739a1602b55
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)
12 * Initial work by:
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
38 * later version.
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>
62 #include "usb.h"
63 #include "scsiglue.h"
64 #include "transport.h"
65 #include "protocol.h"
66 #include "debug.h"
67 #include "initializers.h"
69 #ifdef CONFIG_USB_STORAGE_USBAT
70 #include "shuttle_usbat.h"
71 #endif
72 #ifdef CONFIG_USB_STORAGE_SDDR09
73 #include "sddr09.h"
74 #endif
75 #ifdef CONFIG_USB_STORAGE_SDDR55
76 #include "sddr55.h"
77 #endif
78 #ifdef CONFIG_USB_STORAGE_DPCM
79 #include "dpcm.h"
80 #endif
81 #ifdef CONFIG_USB_STORAGE_FREECOM
82 #include "freecom.h"
83 #endif
84 #ifdef CONFIG_USB_STORAGE_ISD200
85 #include "isd200.h"
86 #endif
87 #ifdef CONFIG_USB_STORAGE_DATAFAB
88 #include "datafab.h"
89 #endif
90 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
91 #include "jumpshot.h"
92 #endif
93 #ifdef CONFIG_USB_STORAGE_ONETOUCH
94 #include "onetouch.h"
95 #endif
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"
132 #undef UNUSUAL_DEV
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.
174 #undef UNUSUAL_DEV
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, \
184 .flags = Flags, \
187 static struct us_unusual_dev us_unusual_dev_list[] = {
188 # include "unusual_devs.h"
189 # undef UNUSUAL_DEV
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 */
233 { NULL }
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.
257 return;
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."). */
268 memset(data+8,0,28);
269 } else {
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);
291 lock_kernel();
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;
299 unlock_kernel();
301 /* acquire a reference to the host, so it won't be deallocated
302 * until we're ready to exit */
303 scsi_host_get(host);
305 /* signal that we've started the thread */
306 complete(&(us->notify));
308 for(;;) {
309 US_DEBUGP("*** thread sleeping.\n");
310 if(down_interruptible(&us->sema))
311 break;
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));
322 break;
325 /* lock access to the state */
326 scsi_lock(host);
328 /* has the command timed out *already* ? */
329 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
330 us->srb->result = DID_ABORT << 16;
331 goto SkipForAbort;
334 scsi_unlock(host);
336 /* reject the command if the direction indicator
337 * is UNKNOWN
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! */
374 else {
375 US_DEBUG(usb_stor_show_command(us->srb));
376 us->proto_handler(us->srb, us);
379 /* lock access to the state */
380 scsi_lock(host);
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",
385 us->srb->result);
386 us->srb->scsi_done(us->srb);
387 } else {
388 SkipForAbort:
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 * the timeout might have occurred after the command had
396 * already completed with a different result code. */
397 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
398 complete(&(us->notify));
400 /* Allow USB transfers to resume */
401 clear_bit(US_FLIDX_ABORTING, &us->flags);
402 clear_bit(US_FLIDX_TIMED_OUT, &us->flags);
405 /* finished working on this command */
406 us->srb = NULL;
407 scsi_unlock(host);
409 /* unlock the device pointers */
410 up(&(us->dev_semaphore));
411 } /* for (;;) */
413 scsi_host_put(host);
415 /* notify the exit routine that we're actually exiting now
417 * complete()/wait_for_completion() is similar to up()/down(),
418 * except that complete() is safe in the case where the structure
419 * is getting deleted in a parallel mode of execution (i.e. just
420 * after the down() -- that's necessary for the thread-shutdown
421 * case.
423 * complete_and_exit() goes even further than this -- it is safe in
424 * the case that the thread of the caller is going away (not just
425 * the structure) -- this is necessary for the module-remove case.
426 * This is important in preemption kernels, which transfer the flow
427 * of execution immediately upon a complete().
429 complete_and_exit(&threads_gone, 0);
432 /***********************************************************************
433 * Device probing and disconnecting
434 ***********************************************************************/
436 /* Associate our private data with the USB device */
437 static int associate_dev(struct us_data *us, struct usb_interface *intf)
439 US_DEBUGP("-- %s\n", __FUNCTION__);
441 /* Fill in the device-related fields */
442 us->pusb_dev = interface_to_usbdev(intf);
443 us->pusb_intf = intf;
444 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
445 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
446 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
447 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
448 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
449 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
450 intf->cur_altsetting->desc.bInterfaceSubClass,
451 intf->cur_altsetting->desc.bInterfaceProtocol);
453 /* Store our private data in the interface */
454 usb_set_intfdata(intf, us);
456 /* Allocate the device-related DMA-mapped buffers */
457 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
458 GFP_KERNEL, &us->cr_dma);
459 if (!us->cr) {
460 US_DEBUGP("usb_ctrlrequest allocation failed\n");
461 return -ENOMEM;
464 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
465 GFP_KERNEL, &us->iobuf_dma);
466 if (!us->iobuf) {
467 US_DEBUGP("I/O buffer allocation failed\n");
468 return -ENOMEM;
471 us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
472 if (!us->sensebuf) {
473 US_DEBUGP("Sense buffer allocation failed\n");
474 return -ENOMEM;
476 return 0;
479 /* Get the unusual_devs entries and the string descriptors */
480 static void get_device_info(struct us_data *us, int id_index)
482 struct usb_device *dev = us->pusb_dev;
483 struct usb_interface_descriptor *idesc =
484 &us->pusb_intf->cur_altsetting->desc;
485 struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
486 struct usb_device_id *id = &storage_usb_ids[id_index];
488 /* Store the entries */
489 us->unusual_dev = unusual_dev;
490 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
491 idesc->bInterfaceSubClass :
492 unusual_dev->useProtocol;
493 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
494 idesc->bInterfaceProtocol :
495 unusual_dev->useTransport;
496 us->flags = unusual_dev->flags;
499 * This flag is only needed when we're in high-speed, so let's
500 * disable it if we're in full-speed
502 if (dev->speed != USB_SPEED_HIGH)
503 us->flags &= ~US_FL_GO_SLOW;
505 /* Log a message if a non-generic unusual_dev entry contains an
506 * unnecessary subclass or protocol override. This may stimulate
507 * reports from users that will help us remove unneeded entries
508 * from the unusual_devs.h table.
510 if (id->idVendor || id->idProduct) {
511 static char *msgs[3] = {
512 "an unneeded SubClass entry",
513 "an unneeded Protocol entry",
514 "unneeded SubClass and Protocol entries"};
515 struct usb_device_descriptor *ddesc = &dev->descriptor;
516 int msg = -1;
518 if (unusual_dev->useProtocol != US_SC_DEVICE &&
519 us->subclass == idesc->bInterfaceSubClass)
520 msg += 1;
521 if (unusual_dev->useTransport != US_PR_DEVICE &&
522 us->protocol == idesc->bInterfaceProtocol)
523 msg += 2;
524 if (msg >= 0 && !(unusual_dev->flags & US_FL_NEED_OVERRIDE))
525 printk(KERN_NOTICE USB_STORAGE "This device "
526 "(%04x,%04x,%04x S %02x P %02x)"
527 " has %s in unusual_devs.h\n"
528 " Please send a copy of this message to "
529 "<linux-usb-devel@lists.sourceforge.net>\n",
530 le16_to_cpu(ddesc->idVendor),
531 le16_to_cpu(ddesc->idProduct),
532 le16_to_cpu(ddesc->bcdDevice),
533 idesc->bInterfaceSubClass,
534 idesc->bInterfaceProtocol,
535 msgs[msg]);
539 /* Get the transport settings */
540 static int get_transport(struct us_data *us)
542 switch (us->protocol) {
543 case US_PR_CB:
544 us->transport_name = "Control/Bulk";
545 us->transport = usb_stor_CB_transport;
546 us->transport_reset = usb_stor_CB_reset;
547 us->max_lun = 7;
548 break;
550 case US_PR_CBI:
551 us->transport_name = "Control/Bulk/Interrupt";
552 us->transport = usb_stor_CBI_transport;
553 us->transport_reset = usb_stor_CB_reset;
554 us->max_lun = 7;
555 break;
557 case US_PR_BULK:
558 us->transport_name = "Bulk";
559 us->transport = usb_stor_Bulk_transport;
560 us->transport_reset = usb_stor_Bulk_reset;
561 break;
563 #ifdef CONFIG_USB_STORAGE_USBAT
564 case US_PR_USBAT:
565 us->transport_name = "Shuttle USBAT";
566 us->transport = usbat_transport;
567 us->transport_reset = usb_stor_CB_reset;
568 us->max_lun = 1;
569 break;
570 #endif
572 #ifdef CONFIG_USB_STORAGE_SDDR09
573 case US_PR_EUSB_SDDR09:
574 us->transport_name = "EUSB/SDDR09";
575 us->transport = sddr09_transport;
576 us->transport_reset = usb_stor_CB_reset;
577 us->max_lun = 0;
578 break;
579 #endif
581 #ifdef CONFIG_USB_STORAGE_SDDR55
582 case US_PR_SDDR55:
583 us->transport_name = "SDDR55";
584 us->transport = sddr55_transport;
585 us->transport_reset = sddr55_reset;
586 us->max_lun = 0;
587 break;
588 #endif
590 #ifdef CONFIG_USB_STORAGE_DPCM
591 case US_PR_DPCM_USB:
592 us->transport_name = "Control/Bulk-EUSB/SDDR09";
593 us->transport = dpcm_transport;
594 us->transport_reset = usb_stor_CB_reset;
595 us->max_lun = 1;
596 break;
597 #endif
599 #ifdef CONFIG_USB_STORAGE_FREECOM
600 case US_PR_FREECOM:
601 us->transport_name = "Freecom";
602 us->transport = freecom_transport;
603 us->transport_reset = usb_stor_freecom_reset;
604 us->max_lun = 0;
605 break;
606 #endif
608 #ifdef CONFIG_USB_STORAGE_DATAFAB
609 case US_PR_DATAFAB:
610 us->transport_name = "Datafab Bulk-Only";
611 us->transport = datafab_transport;
612 us->transport_reset = usb_stor_Bulk_reset;
613 us->max_lun = 1;
614 break;
615 #endif
617 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
618 case US_PR_JUMPSHOT:
619 us->transport_name = "Lexar Jumpshot Control/Bulk";
620 us->transport = jumpshot_transport;
621 us->transport_reset = usb_stor_Bulk_reset;
622 us->max_lun = 1;
623 break;
624 #endif
626 default:
627 return -EIO;
629 US_DEBUGP("Transport: %s\n", us->transport_name);
631 /* fix for single-lun devices */
632 if (us->flags & US_FL_SINGLE_LUN)
633 us->max_lun = 0;
634 return 0;
637 /* Get the protocol settings */
638 static int get_protocol(struct us_data *us)
640 switch (us->subclass) {
641 case US_SC_RBC:
642 us->protocol_name = "Reduced Block Commands (RBC)";
643 us->proto_handler = usb_stor_transparent_scsi_command;
644 break;
646 case US_SC_8020:
647 us->protocol_name = "8020i";
648 us->proto_handler = usb_stor_ATAPI_command;
649 us->max_lun = 0;
650 break;
652 case US_SC_QIC:
653 us->protocol_name = "QIC-157";
654 us->proto_handler = usb_stor_qic157_command;
655 us->max_lun = 0;
656 break;
658 case US_SC_8070:
659 us->protocol_name = "8070i";
660 us->proto_handler = usb_stor_ATAPI_command;
661 us->max_lun = 0;
662 break;
664 case US_SC_SCSI:
665 us->protocol_name = "Transparent SCSI";
666 us->proto_handler = usb_stor_transparent_scsi_command;
667 break;
669 case US_SC_UFI:
670 us->protocol_name = "Uniform Floppy Interface (UFI)";
671 us->proto_handler = usb_stor_ufi_command;
672 break;
674 #ifdef CONFIG_USB_STORAGE_ISD200
675 case US_SC_ISD200:
676 us->protocol_name = "ISD200 ATA/ATAPI";
677 us->proto_handler = isd200_ata_command;
678 break;
679 #endif
681 default:
682 return -EIO;
684 US_DEBUGP("Protocol: %s\n", us->protocol_name);
685 return 0;
688 /* Get the pipe settings */
689 static int get_pipes(struct us_data *us)
691 struct usb_host_interface *altsetting =
692 us->pusb_intf->cur_altsetting;
693 int i;
694 struct usb_endpoint_descriptor *ep;
695 struct usb_endpoint_descriptor *ep_in = NULL;
696 struct usb_endpoint_descriptor *ep_out = NULL;
697 struct usb_endpoint_descriptor *ep_int = NULL;
700 * Find the endpoints we need.
701 * We are expecting a minimum of 2 endpoints - in and out (bulk).
702 * An optional interrupt is OK (necessary for CBI protocol).
703 * We will ignore any others.
705 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
706 ep = &altsetting->endpoint[i].desc;
708 /* Is it a BULK endpoint? */
709 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
710 == USB_ENDPOINT_XFER_BULK) {
711 /* BULK in or out? */
712 if (ep->bEndpointAddress & USB_DIR_IN)
713 ep_in = ep;
714 else
715 ep_out = ep;
718 /* Is it an interrupt endpoint? */
719 else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
720 == USB_ENDPOINT_XFER_INT) {
721 ep_int = ep;
725 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
726 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
727 return -EIO;
730 /* Calculate and store the pipe values */
731 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
732 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
733 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
734 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
735 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
736 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
737 if (ep_int) {
738 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
739 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
740 us->ep_bInterval = ep_int->bInterval;
742 return 0;
745 /* Initialize all the dynamic resources we need */
746 static int usb_stor_acquire_resources(struct us_data *us)
748 int p;
750 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
751 if (!us->current_urb) {
752 US_DEBUGP("URB allocation failed\n");
753 return -ENOMEM;
756 /* Just before we start our control thread, initialize
757 * the device if it needs initialization */
758 if (us->unusual_dev->initFunction) {
759 p = us->unusual_dev->initFunction(us);
760 if (p)
761 return p;
764 /* Start up our control thread */
765 p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
766 if (p < 0) {
767 printk(KERN_WARNING USB_STORAGE
768 "Unable to start control thread\n");
769 return p;
771 us->pid = p;
772 atomic_inc(&total_threads);
774 /* Wait for the thread to start */
775 wait_for_completion(&(us->notify));
777 return 0;
780 /* Release all our dynamic resources */
781 static void usb_stor_release_resources(struct us_data *us)
783 US_DEBUGP("-- %s\n", __FUNCTION__);
785 /* Tell the control thread to exit. The SCSI host must
786 * already have been removed so it won't try to queue
787 * any more commands.
789 US_DEBUGP("-- sending exit command to thread\n");
790 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
791 up(&us->sema);
793 /* Call the destructor routine, if it exists */
794 if (us->extra_destructor) {
795 US_DEBUGP("-- calling extra_destructor()\n");
796 us->extra_destructor(us->extra);
799 /* Free the extra data and the URB */
800 kfree(us->extra);
801 usb_free_urb(us->current_urb);
804 /* Dissociate from the USB device */
805 static void dissociate_dev(struct us_data *us)
807 US_DEBUGP("-- %s\n", __FUNCTION__);
809 kfree(us->sensebuf);
811 /* Free the device-related DMA-mapped buffers */
812 if (us->cr)
813 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
814 us->cr_dma);
815 if (us->iobuf)
816 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
817 us->iobuf_dma);
819 /* Remove our private data from the interface */
820 usb_set_intfdata(us->pusb_intf, NULL);
823 /* First stage of disconnect processing: stop all commands and remove
824 * the host */
825 static void quiesce_and_remove_host(struct us_data *us)
827 /* Prevent new USB transfers, stop the current command, and
828 * interrupt a SCSI-scan or device-reset delay */
829 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
830 usb_stor_stop_transport(us);
831 wake_up(&us->delay_wait);
833 /* It doesn't matter if the SCSI-scanning thread is still running.
834 * The thread will exit when it sees the DISCONNECTING flag. */
836 /* Wait for the current command to finish, then remove the host */
837 down(&us->dev_semaphore);
838 up(&us->dev_semaphore);
840 /* queuecommand won't accept any new commands and the control
841 * thread won't execute a previously-queued command. If there
842 * is such a command pending, complete it with an error. */
843 if (us->srb) {
844 us->srb->result = DID_NO_CONNECT << 16;
845 scsi_lock(us_to_host(us));
846 us->srb->scsi_done(us->srb);
847 us->srb = NULL;
848 scsi_unlock(us_to_host(us));
851 /* Now we own no commands so it's safe to remove the SCSI host */
852 scsi_remove_host(us_to_host(us));
855 /* Second stage of disconnect processing: deallocate all resources */
856 static void release_everything(struct us_data *us)
858 usb_stor_release_resources(us);
859 dissociate_dev(us);
861 /* Drop our reference to the host; the SCSI core will free it
862 * (and "us" along with it) when the refcount becomes 0. */
863 scsi_host_put(us_to_host(us));
866 /* Thread to carry out delayed SCSI-device scanning */
867 static int usb_stor_scan_thread(void * __us)
869 struct us_data *us = (struct us_data *)__us;
872 * This thread doesn't need any user-level access,
873 * so get rid of all our resources.
875 lock_kernel();
876 daemonize("usb-stor-scan");
877 unlock_kernel();
879 /* Acquire a reference to the host, so it won't be deallocated
880 * until we're ready to exit */
881 scsi_host_get(us_to_host(us));
883 /* Signal that we've started the thread */
884 complete(&(us->notify));
886 printk(KERN_DEBUG
887 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
889 /* Wait for the timeout to expire or for a disconnect */
890 if (delay_use > 0) {
891 printk(KERN_DEBUG "usb-storage: waiting for device "
892 "to settle before scanning\n");
893 retry:
894 wait_event_interruptible_timeout(us->delay_wait,
895 test_bit(US_FLIDX_DISCONNECTING, &us->flags),
896 delay_use * HZ);
897 if (try_to_freeze())
898 goto retry;
901 /* If the device is still connected, perform the scanning */
902 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
904 /* For bulk-only devices, determine the max LUN value */
905 if (us->protocol == US_PR_BULK &&
906 !(us->flags & US_FL_SINGLE_LUN)) {
907 down(&us->dev_semaphore);
908 us->max_lun = usb_stor_Bulk_max_lun(us);
909 up(&us->dev_semaphore);
911 scsi_scan_host(us_to_host(us));
912 printk(KERN_DEBUG "usb-storage: device scan complete\n");
914 /* Should we unbind if no devices were detected? */
917 scsi_host_put(us_to_host(us));
918 complete_and_exit(&threads_gone, 0);
922 /* Probe to see if we can drive a newly-connected USB device */
923 static int storage_probe(struct usb_interface *intf,
924 const struct usb_device_id *id)
926 struct Scsi_Host *host;
927 struct us_data *us;
928 const int id_index = id - storage_usb_ids;
929 int result;
931 US_DEBUGP("USB Mass Storage device detected\n");
934 * Ask the SCSI layer to allocate a host structure, with extra
935 * space at the end for our private us_data structure.
937 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
938 if (!host) {
939 printk(KERN_WARNING USB_STORAGE
940 "Unable to allocate the scsi host\n");
941 return -ENOMEM;
944 us = host_to_us(host);
945 memset(us, 0, sizeof(struct us_data));
946 init_MUTEX(&(us->dev_semaphore));
947 init_MUTEX_LOCKED(&(us->sema));
948 init_completion(&(us->notify));
949 init_waitqueue_head(&us->delay_wait);
951 /* Associate the us_data structure with the USB device */
952 result = associate_dev(us, intf);
953 if (result)
954 goto BadDevice;
957 * Get the unusual_devs entries and the descriptors
959 * id_index is calculated in the declaration to be the index number
960 * of the match from the usb_device_id table, so we can find the
961 * corresponding entry in the private table.
963 get_device_info(us, id_index);
965 #ifdef CONFIG_USB_STORAGE_SDDR09
966 if (us->protocol == US_PR_EUSB_SDDR09 ||
967 us->protocol == US_PR_DPCM_USB) {
968 /* set the configuration -- STALL is an acceptable response here */
969 if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
970 US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev
971 ->actconfig->desc.bConfigurationValue);
972 goto BadDevice;
974 result = usb_reset_configuration(us->pusb_dev);
976 US_DEBUGP("Result of usb_reset_configuration is %d\n", result);
977 if (result == -EPIPE) {
978 US_DEBUGP("-- stall on control interface\n");
979 } else if (result != 0) {
980 /* it's not a stall, but another error -- time to bail */
981 US_DEBUGP("-- Unknown error. Rejecting device\n");
982 goto BadDevice;
985 #endif
987 /* Get the transport, protocol, and pipe settings */
988 result = get_transport(us);
989 if (result)
990 goto BadDevice;
991 result = get_protocol(us);
992 if (result)
993 goto BadDevice;
994 result = get_pipes(us);
995 if (result)
996 goto BadDevice;
998 /* Acquire all the other resources and add the host */
999 result = usb_stor_acquire_resources(us);
1000 if (result)
1001 goto BadDevice;
1002 result = scsi_add_host(host, &intf->dev);
1003 if (result) {
1004 printk(KERN_WARNING USB_STORAGE
1005 "Unable to add the scsi host\n");
1006 goto BadDevice;
1009 /* Start up the thread for delayed SCSI-device scanning */
1010 result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM);
1011 if (result < 0) {
1012 printk(KERN_WARNING USB_STORAGE
1013 "Unable to start the device-scanning thread\n");
1014 quiesce_and_remove_host(us);
1015 goto BadDevice;
1017 atomic_inc(&total_threads);
1019 /* Wait for the thread to start */
1020 wait_for_completion(&(us->notify));
1022 return 0;
1024 /* We come here if there are any problems */
1025 BadDevice:
1026 US_DEBUGP("storage_probe() failed\n");
1027 release_everything(us);
1028 return result;
1031 /* Handle a disconnect event from the USB core */
1032 static void storage_disconnect(struct usb_interface *intf)
1034 struct us_data *us = usb_get_intfdata(intf);
1036 US_DEBUGP("storage_disconnect() called\n");
1037 quiesce_and_remove_host(us);
1038 release_everything(us);
1041 /***********************************************************************
1042 * Initialization and registration
1043 ***********************************************************************/
1045 static int __init usb_stor_init(void)
1047 int retval;
1048 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1050 /* register the driver, return usb_register return code if error */
1051 retval = usb_register(&usb_storage_driver);
1052 if (retval == 0)
1053 printk(KERN_INFO "USB Mass Storage support registered.\n");
1055 return retval;
1058 static void __exit usb_stor_exit(void)
1060 US_DEBUGP("usb_stor_exit() called\n");
1062 /* Deregister the driver
1063 * This will cause disconnect() to be called for each
1064 * attached unit
1066 US_DEBUGP("-- calling usb_deregister()\n");
1067 usb_deregister(&usb_storage_driver) ;
1069 /* Don't return until all of our control and scanning threads
1070 * have exited. Since each thread signals threads_gone as its
1071 * last act, we have to call wait_for_completion the right number
1072 * of times.
1074 while (atomic_read(&total_threads) > 0) {
1075 wait_for_completion(&threads_gone);
1076 atomic_dec(&total_threads);
1080 module_init(usb_stor_init);
1081 module_exit(usb_stor_exit);