[PATCH] USB: relax usbcore reset timings
[linux-2.6-block.git] / drivers / usb / storage / usb.c
CommitLineData
1da177e4
LT
1/* Driver for USB Mass Storage compliant devices
2 *
3 * $Id: usb.c,v 1.75 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by:
6 * (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 *
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)
11 *
12 * Initial work by:
13 * (c) 1999 Michael Gee (michael@linuxspecific.com)
14 *
15 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
16 * (c) 2000 Yggdrasil Computing, Inc.
17 *
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.
23 *
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.
28 *
29 * Also, for certain devices, the interrupt endpoint is used to convey
30 * status of a command.
31 *
32 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
33 * information about this driver.
34 *
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.
39 *
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.
44 *
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.
48 */
49
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>
57
58#include <scsi/scsi.h>
59#include <scsi/scsi_cmnd.h>
60#include <scsi/scsi_device.h>
61
62#include "usb.h"
63#include "scsiglue.h"
64#include "transport.h"
65#include "protocol.h"
66#include "debug.h"
67#include "initializers.h"
68
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
34008dbf
MD
93#ifdef CONFIG_USB_STORAGE_ONETOUCH
94#include "onetouch.h"
95#endif
1da177e4
LT
96
97/* Some informational data */
98MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
99MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
100MODULE_LICENSE("GPL");
101
102static unsigned int delay_use = 5;
103module_param(delay_use, uint, S_IRUGO | S_IWUSR);
104MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
105
106
107/* These are used to make sure the module doesn't unload before all the
108 * threads have exited.
109 */
110static atomic_t total_threads = ATOMIC_INIT(0);
111static DECLARE_COMPLETION(threads_gone);
112
113
114static int storage_probe(struct usb_interface *iface,
115 const struct usb_device_id *id);
116
117static void storage_disconnect(struct usb_interface *iface);
118
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[].
122 */
123
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) }
128
129static struct usb_device_id storage_usb_ids [] = {
130
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) },
140
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) },
148
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) },
156
157 /* Terminating entry */
158 { }
159};
160
161MODULE_DEVICE_TABLE (usb, storage_usb_ids);
162
163/* This is the list of devices we recognize, along with their flag data */
164
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.
172 */
173
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) \
178{ \
179 .vendorName = vendor_name, \
180 .productName = product_name, \
181 .useProtocol = use_protocol, \
182 .useTransport = use_transport, \
183 .initFunction = init_function, \
184 .flags = Flags, \
185}
186
187static 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},
203
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},
217
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},
231
232 /* Terminating entry */
233 { NULL }
234};
235
236static 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,
242};
243
244/*
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.
251 */
252
253void fill_inquiry_response(struct us_data *us, unsigned char *data,
254 unsigned int data_len)
255{
256 if (data_len<36) // You lose.
257 return;
258
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);
281 }
282
283 usb_stor_set_xfer_buf(data, data_len, us->srb);
284}
285
286static int usb_stor_control_thread(void * __us)
287{
288 struct us_data *us = (struct us_data *)__us;
289 struct Scsi_Host *host = us_to_host(us);
290
291 lock_kernel();
292
293 /*
294 * This thread doesn't need any user-level access,
295 * so get rid of all our resources.
296 */
297 daemonize("usb-storage");
298 current->flags |= PF_NOFREEZE;
299 unlock_kernel();
300
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);
304
305 /* signal that we've started the thread */
306 complete(&(us->notify));
307
308 for(;;) {
309 US_DEBUGP("*** thread sleeping.\n");
310 if(down_interruptible(&us->sema))
311 break;
312
313 US_DEBUGP("*** thread awakened.\n");
314
315 /* lock the device pointers */
316 down(&(us->dev_semaphore));
317
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;
323 }
324
325 /* lock access to the state */
326 scsi_lock(host);
327
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;
332 }
333
334 scsi_unlock(host);
335
336 /* reject the command if the direction indicator
337 * is UNKNOWN
338 */
339 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
340 US_DEBUGP("UNKNOWN data direction\n");
341 us->srb->result = DID_ERROR << 16;
342 }
343
344 /* reject if target != 0 or if LUN is higher than
345 * the maximum known LUN
346 */
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;
352 }
353
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;
358 }
359
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};
367
368 US_DEBUGP("Faking INQUIRY command\n");
369 fill_inquiry_response(us, data_ptr, 36);
370 us->srb->result = SAM_STAT_GOOD;
371 }
372
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);
377 }
378
379 /* lock access to the state */
380 scsi_lock(host);
381
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 {
388SkipForAbort:
389 US_DEBUGP("scsi command aborted\n");
390 }
391
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));
399
400 /* finished working on this command */
401 us->srb = NULL;
402 scsi_unlock(host);
403
404 /* unlock the device pointers */
405 up(&(us->dev_semaphore));
406 } /* for (;;) */
407
408 scsi_host_put(host);
409
410 /* notify the exit routine that we're actually exiting now
411 *
412 * complete()/wait_for_completion() is similar to up()/down(),
413 * except that complete() is safe in the case where the structure
414 * is getting deleted in a parallel mode of execution (i.e. just
415 * after the down() -- that's necessary for the thread-shutdown
416 * case.
417 *
418 * complete_and_exit() goes even further than this -- it is safe in
419 * the case that the thread of the caller is going away (not just
420 * the structure) -- this is necessary for the module-remove case.
421 * This is important in preemption kernels, which transfer the flow
422 * of execution immediately upon a complete().
423 */
424 complete_and_exit(&threads_gone, 0);
425}
426
427/***********************************************************************
428 * Device probing and disconnecting
429 ***********************************************************************/
430
431/* Associate our private data with the USB device */
432static int associate_dev(struct us_data *us, struct usb_interface *intf)
433{
434 US_DEBUGP("-- %s\n", __FUNCTION__);
435
436 /* Fill in the device-related fields */
437 us->pusb_dev = interface_to_usbdev(intf);
438 us->pusb_intf = intf;
439 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
440 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
441 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
442 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
443 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
444 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
445 intf->cur_altsetting->desc.bInterfaceSubClass,
446 intf->cur_altsetting->desc.bInterfaceProtocol);
447
448 /* Store our private data in the interface */
449 usb_set_intfdata(intf, us);
450
451 /* Allocate the device-related DMA-mapped buffers */
452 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
453 GFP_KERNEL, &us->cr_dma);
454 if (!us->cr) {
455 US_DEBUGP("usb_ctrlrequest allocation failed\n");
456 return -ENOMEM;
457 }
458
459 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
460 GFP_KERNEL, &us->iobuf_dma);
461 if (!us->iobuf) {
462 US_DEBUGP("I/O buffer allocation failed\n");
463 return -ENOMEM;
464 }
465 return 0;
466}
467
468/* Get the unusual_devs entries and the string descriptors */
469static void get_device_info(struct us_data *us, int id_index)
470{
471 struct usb_device *dev = us->pusb_dev;
472 struct usb_interface_descriptor *idesc =
473 &us->pusb_intf->cur_altsetting->desc;
474 struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
475 struct usb_device_id *id = &storage_usb_ids[id_index];
476
477 /* Store the entries */
478 us->unusual_dev = unusual_dev;
479 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
480 idesc->bInterfaceSubClass :
481 unusual_dev->useProtocol;
482 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
483 idesc->bInterfaceProtocol :
484 unusual_dev->useTransport;
485 us->flags = unusual_dev->flags;
486
487 /*
488 * This flag is only needed when we're in high-speed, so let's
489 * disable it if we're in full-speed
490 */
491 if (dev->speed != USB_SPEED_HIGH)
492 us->flags &= ~US_FL_GO_SLOW;
493
494 /* Log a message if a non-generic unusual_dev entry contains an
495 * unnecessary subclass or protocol override. This may stimulate
496 * reports from users that will help us remove unneeded entries
497 * from the unusual_devs.h table.
498 */
499 if (id->idVendor || id->idProduct) {
500 static char *msgs[3] = {
501 "an unneeded SubClass entry",
502 "an unneeded Protocol entry",
503 "unneeded SubClass and Protocol entries"};
504 struct usb_device_descriptor *ddesc = &dev->descriptor;
505 int msg = -1;
506
507 if (unusual_dev->useProtocol != US_SC_DEVICE &&
508 us->subclass == idesc->bInterfaceSubClass)
509 msg += 1;
510 if (unusual_dev->useTransport != US_PR_DEVICE &&
511 us->protocol == idesc->bInterfaceProtocol)
512 msg += 2;
513 if (msg >= 0 && !(unusual_dev->flags & US_FL_NEED_OVERRIDE))
514 printk(KERN_NOTICE USB_STORAGE "This device "
515 "(%04x,%04x,%04x S %02x P %02x)"
516 " has %s in unusual_devs.h\n"
517 " Please send a copy of this message to "
518 "<linux-usb-devel@lists.sourceforge.net>\n",
519 le16_to_cpu(ddesc->idVendor),
520 le16_to_cpu(ddesc->idProduct),
521 le16_to_cpu(ddesc->bcdDevice),
522 idesc->bInterfaceSubClass,
523 idesc->bInterfaceProtocol,
524 msgs[msg]);
525 }
526}
527
528/* Get the transport settings */
529static int get_transport(struct us_data *us)
530{
531 switch (us->protocol) {
532 case US_PR_CB:
533 us->transport_name = "Control/Bulk";
534 us->transport = usb_stor_CB_transport;
535 us->transport_reset = usb_stor_CB_reset;
536 us->max_lun = 7;
537 break;
538
539 case US_PR_CBI:
540 us->transport_name = "Control/Bulk/Interrupt";
541 us->transport = usb_stor_CBI_transport;
542 us->transport_reset = usb_stor_CB_reset;
543 us->max_lun = 7;
544 break;
545
546 case US_PR_BULK:
547 us->transport_name = "Bulk";
548 us->transport = usb_stor_Bulk_transport;
549 us->transport_reset = usb_stor_Bulk_reset;
550 break;
551
552#ifdef CONFIG_USB_STORAGE_USBAT
553 case US_PR_SCM_ATAPI:
554 us->transport_name = "SCM/ATAPI";
555 us->transport = usbat_transport;
556 us->transport_reset = usb_stor_CB_reset;
557 us->max_lun = 1;
558 break;
559#endif
560
561#ifdef CONFIG_USB_STORAGE_SDDR09
562 case US_PR_EUSB_SDDR09:
563 us->transport_name = "EUSB/SDDR09";
564 us->transport = sddr09_transport;
565 us->transport_reset = usb_stor_CB_reset;
566 us->max_lun = 0;
567 break;
568#endif
569
570#ifdef CONFIG_USB_STORAGE_SDDR55
571 case US_PR_SDDR55:
572 us->transport_name = "SDDR55";
573 us->transport = sddr55_transport;
574 us->transport_reset = sddr55_reset;
575 us->max_lun = 0;
576 break;
577#endif
578
579#ifdef CONFIG_USB_STORAGE_DPCM
580 case US_PR_DPCM_USB:
581 us->transport_name = "Control/Bulk-EUSB/SDDR09";
582 us->transport = dpcm_transport;
583 us->transport_reset = usb_stor_CB_reset;
584 us->max_lun = 1;
585 break;
586#endif
587
588#ifdef CONFIG_USB_STORAGE_FREECOM
589 case US_PR_FREECOM:
590 us->transport_name = "Freecom";
591 us->transport = freecom_transport;
592 us->transport_reset = usb_stor_freecom_reset;
593 us->max_lun = 0;
594 break;
595#endif
596
597#ifdef CONFIG_USB_STORAGE_DATAFAB
598 case US_PR_DATAFAB:
599 us->transport_name = "Datafab Bulk-Only";
600 us->transport = datafab_transport;
601 us->transport_reset = usb_stor_Bulk_reset;
602 us->max_lun = 1;
603 break;
604#endif
605
606#ifdef CONFIG_USB_STORAGE_JUMPSHOT
607 case US_PR_JUMPSHOT:
608 us->transport_name = "Lexar Jumpshot Control/Bulk";
609 us->transport = jumpshot_transport;
610 us->transport_reset = usb_stor_Bulk_reset;
611 us->max_lun = 1;
612 break;
613#endif
614
615 default:
616 return -EIO;
617 }
618 US_DEBUGP("Transport: %s\n", us->transport_name);
619
620 /* fix for single-lun devices */
621 if (us->flags & US_FL_SINGLE_LUN)
622 us->max_lun = 0;
623 return 0;
624}
625
626/* Get the protocol settings */
627static int get_protocol(struct us_data *us)
628{
629 switch (us->subclass) {
630 case US_SC_RBC:
631 us->protocol_name = "Reduced Block Commands (RBC)";
632 us->proto_handler = usb_stor_transparent_scsi_command;
633 break;
634
635 case US_SC_8020:
636 us->protocol_name = "8020i";
637 us->proto_handler = usb_stor_ATAPI_command;
638 us->max_lun = 0;
639 break;
640
641 case US_SC_QIC:
642 us->protocol_name = "QIC-157";
643 us->proto_handler = usb_stor_qic157_command;
644 us->max_lun = 0;
645 break;
646
647 case US_SC_8070:
648 us->protocol_name = "8070i";
649 us->proto_handler = usb_stor_ATAPI_command;
650 us->max_lun = 0;
651 break;
652
653 case US_SC_SCSI:
654 us->protocol_name = "Transparent SCSI";
655 us->proto_handler = usb_stor_transparent_scsi_command;
656 break;
657
658 case US_SC_UFI:
659 us->protocol_name = "Uniform Floppy Interface (UFI)";
660 us->proto_handler = usb_stor_ufi_command;
661 break;
662
663#ifdef CONFIG_USB_STORAGE_ISD200
664 case US_SC_ISD200:
665 us->protocol_name = "ISD200 ATA/ATAPI";
666 us->proto_handler = isd200_ata_command;
667 break;
668#endif
669
670 default:
671 return -EIO;
672 }
673 US_DEBUGP("Protocol: %s\n", us->protocol_name);
674 return 0;
675}
676
677/* Get the pipe settings */
678static int get_pipes(struct us_data *us)
679{
680 struct usb_host_interface *altsetting =
681 us->pusb_intf->cur_altsetting;
682 int i;
683 struct usb_endpoint_descriptor *ep;
684 struct usb_endpoint_descriptor *ep_in = NULL;
685 struct usb_endpoint_descriptor *ep_out = NULL;
686 struct usb_endpoint_descriptor *ep_int = NULL;
687
688 /*
689 * Find the endpoints we need.
690 * We are expecting a minimum of 2 endpoints - in and out (bulk).
691 * An optional interrupt is OK (necessary for CBI protocol).
692 * We will ignore any others.
693 */
694 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
695 ep = &altsetting->endpoint[i].desc;
696
697 /* Is it a BULK endpoint? */
698 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
699 == USB_ENDPOINT_XFER_BULK) {
700 /* BULK in or out? */
701 if (ep->bEndpointAddress & USB_DIR_IN)
702 ep_in = ep;
703 else
704 ep_out = ep;
705 }
706
707 /* Is it an interrupt endpoint? */
708 else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
709 == USB_ENDPOINT_XFER_INT) {
710 ep_int = ep;
711 }
712 }
713
714 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
715 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
716 return -EIO;
717 }
718
719 /* Calculate and store the pipe values */
720 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
721 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
722 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
723 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
724 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
725 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
726 if (ep_int) {
727 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
728 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
729 us->ep_bInterval = ep_int->bInterval;
730 }
731 return 0;
732}
733
734/* Initialize all the dynamic resources we need */
735static int usb_stor_acquire_resources(struct us_data *us)
736{
737 int p;
738
739 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
740 if (!us->current_urb) {
741 US_DEBUGP("URB allocation failed\n");
742 return -ENOMEM;
743 }
744
745 /* Lock the device while we carry out the next two operations */
746 down(&us->dev_semaphore);
747
748 /* For bulk-only devices, determine the max LUN value */
749 if (us->protocol == US_PR_BULK) {
750 p = usb_stor_Bulk_max_lun(us);
751 if (p < 0) {
752 up(&us->dev_semaphore);
753 return p;
754 }
755 us->max_lun = p;
756 }
757
758 /* Just before we start our control thread, initialize
759 * the device if it needs initialization */
760 if (us->unusual_dev->initFunction)
761 us->unusual_dev->initFunction(us);
762
763 up(&us->dev_semaphore);
764
765 /* Start up our control thread */
766 p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
767 if (p < 0) {
768 printk(KERN_WARNING USB_STORAGE
769 "Unable to start control thread\n");
770 return p;
771 }
772 us->pid = p;
773 atomic_inc(&total_threads);
774
775 /* Wait for the thread to start */
776 wait_for_completion(&(us->notify));
777
778 return 0;
779}
780
781/* Release all our dynamic resources */
782static void usb_stor_release_resources(struct us_data *us)
783{
784 US_DEBUGP("-- %s\n", __FUNCTION__);
785
786 /* Tell the control thread to exit. The SCSI host must
787 * already have been removed so it won't try to queue
788 * any more commands.
789 */
790 US_DEBUGP("-- sending exit command to thread\n");
77f46328 791 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
1da177e4
LT
792 up(&us->sema);
793
794 /* Call the destructor routine, if it exists */
795 if (us->extra_destructor) {
796 US_DEBUGP("-- calling extra_destructor()\n");
797 us->extra_destructor(us->extra);
798 }
799
800 /* Free the extra data and the URB */
801 kfree(us->extra);
802 usb_free_urb(us->current_urb);
803}
804
805/* Dissociate from the USB device */
806static void dissociate_dev(struct us_data *us)
807{
808 US_DEBUGP("-- %s\n", __FUNCTION__);
809
810 /* Free the device-related DMA-mapped buffers */
811 if (us->cr)
812 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
813 us->cr_dma);
814 if (us->iobuf)
815 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
816 us->iobuf_dma);
817
818 /* Remove our private data from the interface */
819 usb_set_intfdata(us->pusb_intf, NULL);
820}
821
77f46328
MD
822/* First stage of disconnect processing: stop all commands and remove
823 * the host */
824static void quiesce_and_remove_host(struct us_data *us)
825{
826 /* Prevent new USB transfers, stop the current command, and
827 * interrupt a SCSI-scan or device-reset delay */
828 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
829 usb_stor_stop_transport(us);
830 wake_up(&us->delay_wait);
831
832 /* It doesn't matter if the SCSI-scanning thread is still running.
833 * The thread will exit when it sees the DISCONNECTING flag. */
834
835 /* Wait for the current command to finish, then remove the host */
836 down(&us->dev_semaphore);
837 up(&us->dev_semaphore);
26186ba7
MD
838
839 /* queuecommand won't accept any new commands and the control
840 * thread won't execute a previously-queued command. If there
841 * is such a command pending, complete it with an error. */
842 if (us->srb) {
843 us->srb->result = DID_NO_CONNECT << 16;
844 scsi_lock(us_to_host(us));
845 us->srb->scsi_done(us->srb);
846 us->srb = NULL;
847 scsi_unlock(us_to_host(us));
848 }
849
850 /* Now we own no commands so it's safe to remove the SCSI host */
77f46328
MD
851 scsi_remove_host(us_to_host(us));
852}
853
854/* Second stage of disconnect processing: deallocate all resources */
855static void release_everything(struct us_data *us)
856{
857 usb_stor_release_resources(us);
858 dissociate_dev(us);
859
860 /* Drop our reference to the host; the SCSI core will free it
861 * (and "us" along with it) when the refcount becomes 0. */
862 scsi_host_put(us_to_host(us));
863}
864
1da177e4
LT
865/* Thread to carry out delayed SCSI-device scanning */
866static int usb_stor_scan_thread(void * __us)
867{
868 struct us_data *us = (struct us_data *)__us;
869
870 /*
871 * This thread doesn't need any user-level access,
872 * so get rid of all our resources.
873 */
874 lock_kernel();
875 daemonize("usb-stor-scan");
876 unlock_kernel();
877
878 /* Acquire a reference to the host, so it won't be deallocated
879 * until we're ready to exit */
880 scsi_host_get(us_to_host(us));
881
882 /* Signal that we've started the thread */
883 complete(&(us->notify));
884
885 printk(KERN_DEBUG
886 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
887
888 /* Wait for the timeout to expire or for a disconnect */
889 if (delay_use > 0) {
890 printk(KERN_DEBUG "usb-storage: waiting for device "
891 "to settle before scanning\n");
892retry:
893 wait_event_interruptible_timeout(us->delay_wait,
894 test_bit(US_FLIDX_DISCONNECTING, &us->flags),
895 delay_use * HZ);
3e1d1d28 896 if (try_to_freeze())
1da177e4 897 goto retry;
1da177e4
LT
898 }
899
900 /* If the device is still connected, perform the scanning */
901 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
902 scsi_scan_host(us_to_host(us));
903 printk(KERN_DEBUG "usb-storage: device scan complete\n");
904
905 /* Should we unbind if no devices were detected? */
906 }
907
908 scsi_host_put(us_to_host(us));
909 complete_and_exit(&threads_gone, 0);
910}
911
912
913/* Probe to see if we can drive a newly-connected USB device */
914static int storage_probe(struct usb_interface *intf,
915 const struct usb_device_id *id)
916{
917 struct Scsi_Host *host;
918 struct us_data *us;
919 const int id_index = id - storage_usb_ids;
920 int result;
921
922 US_DEBUGP("USB Mass Storage device detected\n");
923
924 /*
925 * Ask the SCSI layer to allocate a host structure, with extra
926 * space at the end for our private us_data structure.
927 */
928 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
929 if (!host) {
930 printk(KERN_WARNING USB_STORAGE
931 "Unable to allocate the scsi host\n");
932 return -ENOMEM;
933 }
934
935 us = host_to_us(host);
936 memset(us, 0, sizeof(struct us_data));
937 init_MUTEX(&(us->dev_semaphore));
938 init_MUTEX_LOCKED(&(us->sema));
939 init_completion(&(us->notify));
940 init_waitqueue_head(&us->delay_wait);
941
942 /* Associate the us_data structure with the USB device */
943 result = associate_dev(us, intf);
944 if (result)
945 goto BadDevice;
946
947 /*
948 * Get the unusual_devs entries and the descriptors
949 *
950 * id_index is calculated in the declaration to be the index number
951 * of the match from the usb_device_id table, so we can find the
952 * corresponding entry in the private table.
953 */
954 get_device_info(us, id_index);
955
956#ifdef CONFIG_USB_STORAGE_SDDR09
957 if (us->protocol == US_PR_EUSB_SDDR09 ||
958 us->protocol == US_PR_DPCM_USB) {
959 /* set the configuration -- STALL is an acceptable response here */
960 if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
961 US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev
962 ->actconfig->desc.bConfigurationValue);
963 goto BadDevice;
964 }
965 result = usb_reset_configuration(us->pusb_dev);
966
967 US_DEBUGP("Result of usb_reset_configuration is %d\n", result);
968 if (result == -EPIPE) {
969 US_DEBUGP("-- stall on control interface\n");
970 } else if (result != 0) {
971 /* it's not a stall, but another error -- time to bail */
972 US_DEBUGP("-- Unknown error. Rejecting device\n");
973 goto BadDevice;
974 }
975 }
976#endif
977
978 /* Get the transport, protocol, and pipe settings */
979 result = get_transport(us);
980 if (result)
981 goto BadDevice;
982 result = get_protocol(us);
983 if (result)
984 goto BadDevice;
985 result = get_pipes(us);
986 if (result)
987 goto BadDevice;
988
989 /* Acquire all the other resources and add the host */
990 result = usb_stor_acquire_resources(us);
991 if (result)
992 goto BadDevice;
993 result = scsi_add_host(host, &intf->dev);
994 if (result) {
995 printk(KERN_WARNING USB_STORAGE
996 "Unable to add the scsi host\n");
997 goto BadDevice;
998 }
999
1000 /* Start up the thread for delayed SCSI-device scanning */
1001 result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM);
1002 if (result < 0) {
1003 printk(KERN_WARNING USB_STORAGE
1004 "Unable to start the device-scanning thread\n");
77f46328 1005 quiesce_and_remove_host(us);
1da177e4
LT
1006 goto BadDevice;
1007 }
1008 atomic_inc(&total_threads);
1009
1010 /* Wait for the thread to start */
1011 wait_for_completion(&(us->notify));
1012
1013 return 0;
1014
1015 /* We come here if there are any problems */
1016BadDevice:
1017 US_DEBUGP("storage_probe() failed\n");
77f46328 1018 release_everything(us);
1da177e4
LT
1019 return result;
1020}
1021
1022/* Handle a disconnect event from the USB core */
1023static void storage_disconnect(struct usb_interface *intf)
1024{
1025 struct us_data *us = usb_get_intfdata(intf);
1026
1027 US_DEBUGP("storage_disconnect() called\n");
77f46328
MD
1028 quiesce_and_remove_host(us);
1029 release_everything(us);
1da177e4
LT
1030}
1031
1032/***********************************************************************
1033 * Initialization and registration
1034 ***********************************************************************/
1035
1036static int __init usb_stor_init(void)
1037{
1038 int retval;
1039 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1040
1041 /* register the driver, return usb_register return code if error */
1042 retval = usb_register(&usb_storage_driver);
1043 if (retval == 0)
1044 printk(KERN_INFO "USB Mass Storage support registered.\n");
1045
1046 return retval;
1047}
1048
1049static void __exit usb_stor_exit(void)
1050{
1051 US_DEBUGP("usb_stor_exit() called\n");
1052
1053 /* Deregister the driver
1054 * This will cause disconnect() to be called for each
1055 * attached unit
1056 */
1057 US_DEBUGP("-- calling usb_deregister()\n");
1058 usb_deregister(&usb_storage_driver) ;
1059
1060 /* Don't return until all of our control and scanning threads
1061 * have exited. Since each thread signals threads_gone as its
1062 * last act, we have to call wait_for_completion the right number
1063 * of times.
1064 */
1065 while (atomic_read(&total_threads) > 0) {
1066 wait_for_completion(&threads_gone);
1067 atomic_dec(&total_threads);
1068 }
1069}
1070
1071module_init(usb_stor_init);
1072module_exit(usb_stor_exit);