]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/usb/storage/usb.c
USB: usb-storage: Associate the name of the interface with the scsi host
[net-next-2.6.git] / drivers / usb / storage / usb.c
CommitLineData
1da177e4 1/* Driver for USB Mass Storage compliant devices
1da177e4
LT
2 *
3 * Current development and maintenance by:
4 * (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 *
6 * Developed with the assistance of:
7 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
e6e244b6 8 * (c) 2003-2009 Alan Stern (stern@rowland.harvard.edu)
1da177e4
LT
9 *
10 * Initial work by:
11 * (c) 1999 Michael Gee (michael@linuxspecific.com)
12 *
13 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
14 * (c) 2000 Yggdrasil Computing, Inc.
15 *
16 * This driver is based on the 'USB Mass Storage Class' document. This
17 * describes in detail the protocol used to communicate with such
18 * devices. Clearly, the designers had SCSI and ATAPI commands in
19 * mind when they created this document. The commands are all very
20 * similar to commands in the SCSI-II and ATAPI specifications.
21 *
22 * It is important to note that in a number of cases this class
23 * exhibits class-specific exemptions from the USB specification.
24 * Notably the usage of NAK, STALL and ACK differs from the norm, in
25 * that they are used to communicate wait, failed and OK on commands.
26 *
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
29 *
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
32 *
33 * This program is free software; you can redistribute it and/or modify it
34 * under the terms of the GNU General Public License as published by the
35 * Free Software Foundation; either version 2, or (at your option) any
36 * later version.
37 *
38 * This program is distributed in the hope that it will be useful, but
39 * WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 * General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 675 Mass Ave, Cambridge, MA 02139, USA.
46 */
47
1da177e4
LT
48#include <linux/sched.h>
49#include <linux/errno.h>
7dfb7103 50#include <linux/freezer.h>
1da177e4
LT
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/slab.h>
3f13e66e 54#include <linux/kthread.h>
4186ecf8 55#include <linux/mutex.h>
00f8b0c1 56#include <linux/utsname.h>
1da177e4
LT
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
32fe5e39 69#include "sierra_ms.h"
281b064f 70#include "option_ms.h"
1da177e4
LT
71
72/* Some informational data */
73MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
74MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
75MODULE_LICENSE("GPL");
76
77static unsigned int delay_use = 5;
78module_param(delay_use, uint, S_IRUGO | S_IWUSR);
79MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
80
c838ea46
AS
81static char quirks[128];
82module_param_string(quirks, quirks, sizeof(quirks), S_IRUGO | S_IWUSR);
d4f373e5
AS
83MODULE_PARM_DESC(quirks, "supplemental list of device IDs and their quirks");
84
1da177e4 85
a00828e9
PZ
86/*
87 * The entries in this table correspond, line for line,
e6e244b6 88 * with the entries in usb_storage_usb_ids[], defined in usual-tables.c.
1da177e4 89 */
1da177e4
LT
90
91/* The vendor name should be kept at eight characters or less, and
92 * the product name should be kept at 16 characters or less. If a device
93 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
94 * normally generated by a device thorugh the INQUIRY response will be
95 * taken from this list, and this is the reason for the above size
96 * restriction. However, if the flag is not present, then you
97 * are free to use as many characters as you like.
98 */
99
1da177e4
LT
100#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
101 vendor_name, product_name, use_protocol, use_transport, \
102 init_function, Flags) \
103{ \
104 .vendorName = vendor_name, \
105 .productName = product_name, \
106 .useProtocol = use_protocol, \
107 .useTransport = use_transport, \
108 .initFunction = init_function, \
a00828e9
PZ
109}
110
25ff1c31
AS
111#define COMPLIANT_DEV UNUSUAL_DEV
112
a00828e9
PZ
113#define USUAL_DEV(use_protocol, use_transport, use_type) \
114{ \
115 .useProtocol = use_protocol, \
116 .useTransport = use_transport, \
1da177e4
LT
117}
118
119static struct us_unusual_dev us_unusual_dev_list[] = {
120# include "unusual_devs.h"
e6e244b6 121 { } /* Terminating entry */
1da177e4
LT
122};
123
e6e244b6
AS
124#undef UNUSUAL_DEV
125#undef COMPLIANT_DEV
126#undef USUAL_DEV
127
ce2596df
AS
128
129#ifdef CONFIG_PM /* Minimal support for suspend and resume */
130
e6e244b6 131int usb_stor_suspend(struct usb_interface *iface, pm_message_t message)
ce2596df
AS
132{
133 struct us_data *us = usb_get_intfdata(iface);
134
135 /* Wait until no command is running */
4186ecf8 136 mutex_lock(&us->dev_mutex);
ce2596df 137
441b62c1 138 US_DEBUGP("%s\n", __func__);
7931e1c6
MD
139 if (us->suspend_resume_hook)
140 (us->suspend_resume_hook)(us, US_SUSPEND);
ce2596df 141
d526875d
GKH
142 /* When runtime PM is working, we'll set a flag to indicate
143 * whether we should autoresume when a SCSI request arrives. */
144
4186ecf8 145 mutex_unlock(&us->dev_mutex);
ce2596df
AS
146 return 0;
147}
e6e244b6 148EXPORT_SYMBOL_GPL(usb_stor_suspend);
ce2596df 149
e6e244b6 150int usb_stor_resume(struct usb_interface *iface)
ce2596df
AS
151{
152 struct us_data *us = usb_get_intfdata(iface);
153
d526875d 154 mutex_lock(&us->dev_mutex);
8dfe4b14 155
441b62c1 156 US_DEBUGP("%s\n", __func__);
7931e1c6
MD
157 if (us->suspend_resume_hook)
158 (us->suspend_resume_hook)(us, US_RESUME);
ce2596df 159
d526875d 160 mutex_unlock(&us->dev_mutex);
ce2596df
AS
161 return 0;
162}
e6e244b6 163EXPORT_SYMBOL_GPL(usb_stor_resume);
ce2596df 164
e6e244b6 165int usb_stor_reset_resume(struct usb_interface *iface)
f07600cf
AS
166{
167 struct us_data *us = usb_get_intfdata(iface);
168
441b62c1 169 US_DEBUGP("%s\n", __func__);
f07600cf
AS
170
171 /* Report the reset to the SCSI core */
172 usb_stor_report_bus_reset(us);
173
174 /* FIXME: Notify the subdrivers that they need to reinitialize
175 * the device */
176 return 0;
177}
e6e244b6 178EXPORT_SYMBOL_GPL(usb_stor_reset_resume);
f07600cf 179
ce2596df 180#endif /* CONFIG_PM */
1da177e4 181
47104b0d
AS
182/*
183 * The next two routines get called just before and just after
184 * a USB port reset, whether from this driver or a different one.
185 */
186
e6e244b6 187int usb_stor_pre_reset(struct usb_interface *iface)
47104b0d
AS
188{
189 struct us_data *us = usb_get_intfdata(iface);
190
441b62c1 191 US_DEBUGP("%s\n", __func__);
47104b0d
AS
192
193 /* Make sure no command runs during the reset */
194 mutex_lock(&us->dev_mutex);
f07600cf 195 return 0;
47104b0d 196}
e6e244b6 197EXPORT_SYMBOL_GPL(usb_stor_pre_reset);
47104b0d 198
e6e244b6 199int usb_stor_post_reset(struct usb_interface *iface)
47104b0d
AS
200{
201 struct us_data *us = usb_get_intfdata(iface);
202
441b62c1 203 US_DEBUGP("%s\n", __func__);
47104b0d
AS
204
205 /* Report the reset to the SCSI core */
47104b0d 206 usb_stor_report_bus_reset(us);
47104b0d
AS
207
208 /* FIXME: Notify the subdrivers that they need to reinitialize
209 * the device */
0458d5b4 210
f07600cf
AS
211 mutex_unlock(&us->dev_mutex);
212 return 0;
47104b0d 213}
e6e244b6 214EXPORT_SYMBOL_GPL(usb_stor_post_reset);
47104b0d 215
1da177e4
LT
216/*
217 * fill_inquiry_response takes an unsigned char array (which must
218 * be at least 36 characters) and populates the vendor name,
219 * product name, and revision fields. Then the array is copied
220 * into the SCSI command's response buffer (oddly enough
221 * called request_buffer). data_len contains the length of the
222 * data array, which again must be at least 36.
223 */
224
225void fill_inquiry_response(struct us_data *us, unsigned char *data,
226 unsigned int data_len)
227{
228 if (data_len<36) // You lose.
229 return;
230
231 if(data[0]&0x20) { /* USB device currently not connected. Return
232 peripheral qualifier 001b ("...however, the
233 physical device is not currently connected
234 to this logical unit") and leave vendor and
235 product identification empty. ("If the target
236 does store some of the INQUIRY data on the
237 device, it may return zeros or ASCII spaces
238 (20h) in those fields until the data is
239 available from the device."). */
240 memset(data+8,0,28);
241 } else {
242 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
243 memcpy(data+8, us->unusual_dev->vendorName,
244 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
245 strlen(us->unusual_dev->vendorName));
246 memcpy(data+16, us->unusual_dev->productName,
247 strlen(us->unusual_dev->productName) > 16 ? 16 :
248 strlen(us->unusual_dev->productName));
249 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
250 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
251 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
252 data[35] = 0x30 + ((bcdDevice) & 0x0F);
253 }
254
255 usb_stor_set_xfer_buf(data, data_len, us->srb);
256}
e6e244b6 257EXPORT_SYMBOL_GPL(fill_inquiry_response);
1da177e4
LT
258
259static int usb_stor_control_thread(void * __us)
260{
261 struct us_data *us = (struct us_data *)__us;
262 struct Scsi_Host *host = us_to_host(us);
263
1da177e4
LT
264 for(;;) {
265 US_DEBUGP("*** thread sleeping.\n");
7119e3c3 266 if (wait_for_completion_interruptible(&us->cmnd_ready))
1da177e4 267 break;
7119e3c3 268
1da177e4
LT
269 US_DEBUGP("*** thread awakened.\n");
270
271 /* lock the device pointers */
4186ecf8 272 mutex_lock(&(us->dev_mutex));
1da177e4 273
543f7810
AS
274 /* lock access to the state */
275 scsi_lock(host);
276
277 /* When we are called with no command pending, we're done */
278 if (us->srb == NULL) {
279 scsi_unlock(host);
4186ecf8 280 mutex_unlock(&us->dev_mutex);
543f7810 281 US_DEBUGP("-- exiting\n");
1da177e4
LT
282 break;
283 }
284
1da177e4 285 /* has the command timed out *already* ? */
7e4d6c38 286 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
1da177e4
LT
287 us->srb->result = DID_ABORT << 16;
288 goto SkipForAbort;
289 }
290
291 scsi_unlock(host);
292
293 /* reject the command if the direction indicator
294 * is UNKNOWN
295 */
296 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
297 US_DEBUGP("UNKNOWN data direction\n");
298 us->srb->result = DID_ERROR << 16;
299 }
300
301 /* reject if target != 0 or if LUN is higher than
302 * the maximum known LUN
303 */
304 else if (us->srb->device->id &&
7e4d6c38 305 !(us->fflags & US_FL_SCM_MULT_TARG)) {
1da177e4
LT
306 US_DEBUGP("Bad target number (%d:%d)\n",
307 us->srb->device->id, us->srb->device->lun);
308 us->srb->result = DID_BAD_TARGET << 16;
309 }
310
311 else if (us->srb->device->lun > us->max_lun) {
312 US_DEBUGP("Bad LUN (%d:%d)\n",
313 us->srb->device->id, us->srb->device->lun);
314 us->srb->result = DID_BAD_TARGET << 16;
315 }
316
317 /* Handle those devices which need us to fake
318 * their inquiry data */
319 else if ((us->srb->cmnd[0] == INQUIRY) &&
7e4d6c38 320 (us->fflags & US_FL_FIX_INQUIRY)) {
1da177e4
LT
321 unsigned char data_ptr[36] = {
322 0x00, 0x80, 0x02, 0x02,
323 0x1F, 0x00, 0x00, 0x00};
324
325 US_DEBUGP("Faking INQUIRY command\n");
326 fill_inquiry_response(us, data_ptr, 36);
327 us->srb->result = SAM_STAT_GOOD;
328 }
329
330 /* we've got a command, let's do it! */
331 else {
332 US_DEBUG(usb_stor_show_command(us->srb));
333 us->proto_handler(us->srb, us);
334 }
335
336 /* lock access to the state */
337 scsi_lock(host);
338
339 /* indicate that the command is done */
543f7810 340 if (us->srb->result != DID_ABORT << 16) {
1da177e4
LT
341 US_DEBUGP("scsi cmd done, result=0x%x\n",
342 us->srb->result);
343 us->srb->scsi_done(us->srb);
344 } else {
345SkipForAbort:
346 US_DEBUGP("scsi command aborted\n");
347 }
348
349 /* If an abort request was received we need to signal that
350 * the abort has finished. The proper test for this is
351 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
226173ed
MD
352 * the timeout might have occurred after the command had
353 * already completed with a different result code. */
7e4d6c38 354 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
1da177e4
LT
355 complete(&(us->notify));
356
226173ed 357 /* Allow USB transfers to resume */
7e4d6c38
AS
358 clear_bit(US_FLIDX_ABORTING, &us->dflags);
359 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
226173ed
MD
360 }
361
1da177e4
LT
362 /* finished working on this command */
363 us->srb = NULL;
364 scsi_unlock(host);
365
366 /* unlock the device pointers */
4186ecf8 367 mutex_unlock(&us->dev_mutex);
1da177e4
LT
368 } /* for (;;) */
369
ed76cacb
AS
370 /* Wait until we are told to stop */
371 for (;;) {
372 set_current_state(TASK_INTERRUPTIBLE);
373 if (kthread_should_stop())
374 break;
375 schedule();
376 }
377 __set_current_state(TASK_RUNNING);
378 return 0;
1da177e4
LT
379}
380
381/***********************************************************************
382 * Device probing and disconnecting
383 ***********************************************************************/
384
385/* Associate our private data with the USB device */
386static int associate_dev(struct us_data *us, struct usb_interface *intf)
387{
441b62c1 388 US_DEBUGP("-- %s\n", __func__);
1da177e4
LT
389
390 /* Fill in the device-related fields */
391 us->pusb_dev = interface_to_usbdev(intf);
392 us->pusb_intf = intf;
393 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
394 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
395 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
396 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
397 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
398 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
399 intf->cur_altsetting->desc.bInterfaceSubClass,
400 intf->cur_altsetting->desc.bInterfaceProtocol);
401
402 /* Store our private data in the interface */
403 usb_set_intfdata(intf, us);
404
405 /* Allocate the device-related DMA-mapped buffers */
406 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
407 GFP_KERNEL, &us->cr_dma);
408 if (!us->cr) {
409 US_DEBUGP("usb_ctrlrequest allocation failed\n");
410 return -ENOMEM;
411 }
412
413 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
414 GFP_KERNEL, &us->iobuf_dma);
415 if (!us->iobuf) {
416 US_DEBUGP("I/O buffer allocation failed\n");
417 return -ENOMEM;
418 }
419 return 0;
420}
421
c838ea46
AS
422/* Works only for digits and letters, but small and fast */
423#define TOLOWER(x) ((x) | 0x20)
424
d4f373e5
AS
425/* Adjust device flags based on the "quirks=" module parameter */
426static void adjust_quirks(struct us_data *us)
427{
c838ea46
AS
428 char *p;
429 u16 vid = le16_to_cpu(us->pusb_dev->descriptor.idVendor);
430 u16 pid = le16_to_cpu(us->pusb_dev->descriptor.idProduct);
431 unsigned f = 0;
432 unsigned int mask = (US_FL_SANE_SENSE | US_FL_FIX_CAPACITY |
433 US_FL_CAPACITY_HEURISTICS | US_FL_IGNORE_DEVICE |
d4f373e5 434 US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 |
c838ea46
AS
435 US_FL_CAPACITY_OK | US_FL_IGNORE_RESIDUE |
436 US_FL_SINGLE_LUN | US_FL_NO_WP_DETECT);
437
438 p = quirks;
439 while (*p) {
440 /* Each entry consists of VID:PID:flags */
441 if (vid == simple_strtoul(p, &p, 16) &&
442 *p == ':' &&
443 pid == simple_strtoul(p+1, &p, 16) &&
444 *p == ':')
d4f373e5 445 break;
c838ea46
AS
446
447 /* Move forward to the next entry */
448 while (*p) {
449 if (*p++ == ',')
450 break;
d4f373e5
AS
451 }
452 }
c838ea46
AS
453 if (!*p) /* No match */
454 return;
455
456 /* Collect the flags */
457 while (*++p && *p != ',') {
458 switch (TOLOWER(*p)) {
459 case 'a':
460 f |= US_FL_SANE_SENSE;
461 break;
462 case 'c':
463 f |= US_FL_FIX_CAPACITY;
464 break;
465 case 'h':
466 f |= US_FL_CAPACITY_HEURISTICS;
467 break;
468 case 'i':
469 f |= US_FL_IGNORE_DEVICE;
470 break;
471 case 'l':
472 f |= US_FL_NOT_LOCKABLE;
473 break;
474 case 'm':
475 f |= US_FL_MAX_SECTORS_64;
476 break;
477 case 'o':
478 f |= US_FL_CAPACITY_OK;
479 break;
480 case 'r':
481 f |= US_FL_IGNORE_RESIDUE;
482 break;
483 case 's':
484 f |= US_FL_SINGLE_LUN;
485 break;
486 case 'w':
487 f |= US_FL_NO_WP_DETECT;
488 break;
489 /* Ignore unrecognized flag characters */
490 }
491 }
492 us->fflags = (us->fflags & ~mask) | f;
493 dev_info(&us->pusb_intf->dev, "Quirks match for "
494 "vid %04x pid %04x: %x\n",
495 vid, pid, f);
d4f373e5
AS
496}
497
1da177e4 498/* Get the unusual_devs entries and the string descriptors */
e6e244b6
AS
499static int get_device_info(struct us_data *us, const struct usb_device_id *id,
500 struct us_unusual_dev *unusual_dev)
1da177e4
LT
501{
502 struct usb_device *dev = us->pusb_dev;
503 struct usb_interface_descriptor *idesc =
504 &us->pusb_intf->cur_altsetting->desc;
1da177e4
LT
505
506 /* Store the entries */
507 us->unusual_dev = unusual_dev;
508 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
509 idesc->bInterfaceSubClass :
510 unusual_dev->useProtocol;
511 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
512 idesc->bInterfaceProtocol :
513 unusual_dev->useTransport;
7e4d6c38 514 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
d4f373e5 515 adjust_quirks(us);
1da177e4 516
7e4d6c38 517 if (us->fflags & US_FL_IGNORE_DEVICE) {
3c332422
DD
518 printk(KERN_INFO USB_STORAGE "device ignored\n");
519 return -ENODEV;
520 }
521
1da177e4
LT
522 /*
523 * This flag is only needed when we're in high-speed, so let's
524 * disable it if we're in full-speed
525 */
526 if (dev->speed != USB_SPEED_HIGH)
7e4d6c38 527 us->fflags &= ~US_FL_GO_SLOW;
1da177e4
LT
528
529 /* Log a message if a non-generic unusual_dev entry contains an
530 * unnecessary subclass or protocol override. This may stimulate
531 * reports from users that will help us remove unneeded entries
532 * from the unusual_devs.h table.
533 */
534 if (id->idVendor || id->idProduct) {
4c4c9432 535 static const char *msgs[3] = {
1da177e4
LT
536 "an unneeded SubClass entry",
537 "an unneeded Protocol entry",
538 "unneeded SubClass and Protocol entries"};
539 struct usb_device_descriptor *ddesc = &dev->descriptor;
540 int msg = -1;
541
542 if (unusual_dev->useProtocol != US_SC_DEVICE &&
543 us->subclass == idesc->bInterfaceSubClass)
544 msg += 1;
545 if (unusual_dev->useTransport != US_PR_DEVICE &&
546 us->protocol == idesc->bInterfaceProtocol)
547 msg += 2;
7e4d6c38 548 if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
1da177e4
LT
549 printk(KERN_NOTICE USB_STORAGE "This device "
550 "(%04x,%04x,%04x S %02x P %02x)"
5650b4dd
PD
551 " has %s in unusual_devs.h (kernel"
552 " %s)\n"
1da177e4 553 " Please send a copy of this message to "
cef03f8f
AL
554 "<linux-usb@vger.kernel.org> and "
555 "<usb-storage@lists.one-eyed-alien.net>\n",
1da177e4
LT
556 le16_to_cpu(ddesc->idVendor),
557 le16_to_cpu(ddesc->idProduct),
558 le16_to_cpu(ddesc->bcdDevice),
559 idesc->bInterfaceSubClass,
560 idesc->bInterfaceProtocol,
5650b4dd 561 msgs[msg],
00f8b0c1 562 utsname()->release);
1da177e4 563 }
3c332422
DD
564
565 return 0;
1da177e4
LT
566}
567
568/* Get the transport settings */
e6e244b6 569static void get_transport(struct us_data *us)
1da177e4
LT
570{
571 switch (us->protocol) {
572 case US_PR_CB:
573 us->transport_name = "Control/Bulk";
574 us->transport = usb_stor_CB_transport;
575 us->transport_reset = usb_stor_CB_reset;
576 us->max_lun = 7;
577 break;
578
579 case US_PR_CBI:
580 us->transport_name = "Control/Bulk/Interrupt";
64648a9d 581 us->transport = usb_stor_CB_transport;
1da177e4
LT
582 us->transport_reset = usb_stor_CB_reset;
583 us->max_lun = 7;
584 break;
585
586 case US_PR_BULK:
587 us->transport_name = "Bulk";
588 us->transport = usb_stor_Bulk_transport;
589 us->transport_reset = usb_stor_Bulk_reset;
590 break;
1da177e4 591 }
1da177e4
LT
592}
593
594/* Get the protocol settings */
e6e244b6 595static void get_protocol(struct us_data *us)
1da177e4
LT
596{
597 switch (us->subclass) {
598 case US_SC_RBC:
599 us->protocol_name = "Reduced Block Commands (RBC)";
600 us->proto_handler = usb_stor_transparent_scsi_command;
601 break;
602
603 case US_SC_8020:
604 us->protocol_name = "8020i";
3dae5345 605 us->proto_handler = usb_stor_pad12_command;
1da177e4
LT
606 us->max_lun = 0;
607 break;
608
609 case US_SC_QIC:
610 us->protocol_name = "QIC-157";
3dae5345 611 us->proto_handler = usb_stor_pad12_command;
1da177e4
LT
612 us->max_lun = 0;
613 break;
614
615 case US_SC_8070:
616 us->protocol_name = "8070i";
3dae5345 617 us->proto_handler = usb_stor_pad12_command;
1da177e4
LT
618 us->max_lun = 0;
619 break;
620
621 case US_SC_SCSI:
622 us->protocol_name = "Transparent SCSI";
623 us->proto_handler = usb_stor_transparent_scsi_command;
624 break;
625
626 case US_SC_UFI:
627 us->protocol_name = "Uniform Floppy Interface (UFI)";
628 us->proto_handler = usb_stor_ufi_command;
629 break;
1da177e4 630 }
1da177e4
LT
631}
632
633/* Get the pipe settings */
634static int get_pipes(struct us_data *us)
635{
636 struct usb_host_interface *altsetting =
637 us->pusb_intf->cur_altsetting;
638 int i;
639 struct usb_endpoint_descriptor *ep;
640 struct usb_endpoint_descriptor *ep_in = NULL;
641 struct usb_endpoint_descriptor *ep_out = NULL;
642 struct usb_endpoint_descriptor *ep_int = NULL;
643
644 /*
1096f780 645 * Find the first endpoint of each type we need.
1da177e4 646 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1096f780 647 * An optional interrupt-in is OK (necessary for CBI protocol).
1da177e4
LT
648 * We will ignore any others.
649 */
650 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
651 ep = &altsetting->endpoint[i].desc;
652
04720747 653 if (usb_endpoint_xfer_bulk(ep)) {
1096f780
AS
654 if (usb_endpoint_dir_in(ep)) {
655 if (!ep_in)
656 ep_in = ep;
657 } else {
658 if (!ep_out)
659 ep_out = ep;
660 }
1da177e4
LT
661 }
662
1096f780
AS
663 else if (usb_endpoint_is_int_in(ep)) {
664 if (!ep_int)
665 ep_int = ep;
1da177e4
LT
666 }
667 }
668
669 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
670 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
671 return -EIO;
672 }
673
674 /* Calculate and store the pipe values */
675 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
676 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
677 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
2e0fe709 678 usb_endpoint_num(ep_out));
1da177e4 679 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
2e0fe709 680 usb_endpoint_num(ep_in));
1da177e4
LT
681 if (ep_int) {
682 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
2e0fe709 683 usb_endpoint_num(ep_int));
1da177e4
LT
684 us->ep_bInterval = ep_int->bInterval;
685 }
686 return 0;
687}
688
689/* Initialize all the dynamic resources we need */
690static int usb_stor_acquire_resources(struct us_data *us)
691{
692 int p;
3f13e66e 693 struct task_struct *th;
1da177e4
LT
694
695 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
696 if (!us->current_urb) {
697 US_DEBUGP("URB allocation failed\n");
698 return -ENOMEM;
699 }
700
1da177e4
LT
701 /* Just before we start our control thread, initialize
702 * the device if it needs initialization */
b876aef7
AS
703 if (us->unusual_dev->initFunction) {
704 p = us->unusual_dev->initFunction(us);
705 if (p)
706 return p;
707 }
1da177e4
LT
708
709 /* Start up our control thread */
ed76cacb 710 th = kthread_run(usb_stor_control_thread, us, "usb-storage");
3f13e66e 711 if (IS_ERR(th)) {
1da177e4
LT
712 printk(KERN_WARNING USB_STORAGE
713 "Unable to start control thread\n");
3f13e66e 714 return PTR_ERR(th);
1da177e4 715 }
ed76cacb 716 us->ctl_thread = th;
1da177e4
LT
717
718 return 0;
719}
720
721/* Release all our dynamic resources */
722static void usb_stor_release_resources(struct us_data *us)
723{
441b62c1 724 US_DEBUGP("-- %s\n", __func__);
1da177e4
LT
725
726 /* Tell the control thread to exit. The SCSI host must
543f7810
AS
727 * already have been removed and the DISCONNECTING flag set
728 * so that we won't accept any more commands.
1da177e4
LT
729 */
730 US_DEBUGP("-- sending exit command to thread\n");
7119e3c3 731 complete(&us->cmnd_ready);
ed76cacb
AS
732 if (us->ctl_thread)
733 kthread_stop(us->ctl_thread);
1da177e4
LT
734
735 /* Call the destructor routine, if it exists */
736 if (us->extra_destructor) {
737 US_DEBUGP("-- calling extra_destructor()\n");
738 us->extra_destructor(us->extra);
739 }
740
741 /* Free the extra data and the URB */
742 kfree(us->extra);
743 usb_free_urb(us->current_urb);
744}
745
746/* Dissociate from the USB device */
747static void dissociate_dev(struct us_data *us)
748{
441b62c1 749 US_DEBUGP("-- %s\n", __func__);
1da177e4
LT
750
751 /* Free the device-related DMA-mapped buffers */
752 if (us->cr)
753 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
754 us->cr_dma);
755 if (us->iobuf)
756 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
757 us->iobuf_dma);
758
759 /* Remove our private data from the interface */
760 usb_set_intfdata(us->pusb_intf, NULL);
761}
762
543f7810
AS
763/* First stage of disconnect processing: stop SCSI scanning,
764 * remove the host, and stop accepting new commands
765 */
77f46328
MD
766static void quiesce_and_remove_host(struct us_data *us)
767{
eecd11ed
AS
768 struct Scsi_Host *host = us_to_host(us);
769
543f7810
AS
770 /* If the device is really gone, cut short reset delays */
771 if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
772 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
77f46328 773
543f7810
AS
774 /* Prevent SCSI-scanning (if it hasn't started yet)
775 * and wait for the SCSI-scanning thread to stop.
776 */
777 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
778 wake_up(&us->delay_wait);
779 wait_for_completion(&us->scanning_done);
26186ba7 780
543f7810
AS
781 /* Removing the host will perform an orderly shutdown: caches
782 * synchronized, disks spun down, etc.
783 */
eecd11ed 784 scsi_remove_host(host);
2f67cd5b 785
543f7810
AS
786 /* Prevent any new commands from being accepted and cut short
787 * reset delays.
788 */
789 scsi_lock(host);
790 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
791 scsi_unlock(host);
792 wake_up(&us->delay_wait);
77f46328
MD
793}
794
795/* Second stage of disconnect processing: deallocate all resources */
796static void release_everything(struct us_data *us)
797{
798 usb_stor_release_resources(us);
799 dissociate_dev(us);
800
801 /* Drop our reference to the host; the SCSI core will free it
802 * (and "us" along with it) when the refcount becomes 0. */
803 scsi_host_put(us_to_host(us));
804}
805
1da177e4
LT
806/* Thread to carry out delayed SCSI-device scanning */
807static int usb_stor_scan_thread(void * __us)
808{
809 struct us_data *us = (struct us_data *)__us;
810
1da177e4
LT
811 printk(KERN_DEBUG
812 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
813
83144186 814 set_freezable();
1da177e4
LT
815 /* Wait for the timeout to expire or for a disconnect */
816 if (delay_use > 0) {
817 printk(KERN_DEBUG "usb-storage: waiting for device "
818 "to settle before scanning\n");
e42837bc 819 wait_event_freezable_timeout(us->delay_wait,
543f7810 820 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
1da177e4 821 delay_use * HZ);
1da177e4
LT
822 }
823
824 /* If the device is still connected, perform the scanning */
543f7810 825 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
b876aef7
AS
826
827 /* For bulk-only devices, determine the max LUN value */
828 if (us->protocol == US_PR_BULK &&
7e4d6c38 829 !(us->fflags & US_FL_SINGLE_LUN)) {
4186ecf8 830 mutex_lock(&us->dev_mutex);
b876aef7 831 us->max_lun = usb_stor_Bulk_max_lun(us);
4186ecf8 832 mutex_unlock(&us->dev_mutex);
b876aef7 833 }
1da177e4
LT
834 scsi_scan_host(us_to_host(us));
835 printk(KERN_DEBUG "usb-storage: device scan complete\n");
836
837 /* Should we unbind if no devices were detected? */
838 }
839
2f67cd5b 840 complete_and_exit(&us->scanning_done, 0);
1da177e4
LT
841}
842
843
e6e244b6
AS
844/* First part of general USB mass-storage probing */
845int usb_stor_probe1(struct us_data **pus,
846 struct usb_interface *intf,
847 const struct usb_device_id *id,
848 struct us_unusual_dev *unusual_dev)
1da177e4
LT
849{
850 struct Scsi_Host *host;
851 struct us_data *us;
1da177e4 852 int result;
a00828e9 853
1da177e4
LT
854 US_DEBUGP("USB Mass Storage device detected\n");
855
856 /*
857 * Ask the SCSI layer to allocate a host structure, with extra
858 * space at the end for our private us_data structure.
859 */
860 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
861 if (!host) {
862 printk(KERN_WARNING USB_STORAGE
863 "Unable to allocate the scsi host\n");
864 return -ENOMEM;
865 }
866
17f06022
RS
867 /*
868 * Allow 16-byte CDBs and thus > 2TB
869 */
870 host->max_cmd_len = 16;
e6e244b6 871 *pus = us = host_to_us(host);
1da177e4 872 memset(us, 0, sizeof(struct us_data));
4186ecf8 873 mutex_init(&(us->dev_mutex));
7119e3c3 874 init_completion(&us->cmnd_ready);
1da177e4
LT
875 init_completion(&(us->notify));
876 init_waitqueue_head(&us->delay_wait);
2f67cd5b 877 init_completion(&us->scanning_done);
1da177e4
LT
878
879 /* Associate the us_data structure with the USB device */
880 result = associate_dev(us, intf);
881 if (result)
882 goto BadDevice;
883
e6e244b6
AS
884 /* Get the unusual_devs entries and the descriptors */
885 result = get_device_info(us, id, unusual_dev);
3c332422
DD
886 if (result)
887 goto BadDevice;
1da177e4 888
e6e244b6
AS
889 /* Get standard transport and protocol settings */
890 get_transport(us);
891 get_protocol(us);
892
893 /* Give the caller a chance to fill in specialized transport
894 * or protocol settings.
895 */
896 return 0;
897
898BadDevice:
899 US_DEBUGP("storage_probe() failed\n");
900 release_everything(us);
901 return result;
902}
903EXPORT_SYMBOL_GPL(usb_stor_probe1);
904
905/* Second part of general USB mass-storage probing */
906int usb_stor_probe2(struct us_data *us)
907{
908 struct task_struct *th;
909 int result;
910
911 /* Make sure the transport and protocol have both been set */
912 if (!us->transport || !us->proto_handler) {
913 result = -ENXIO;
1da177e4 914 goto BadDevice;
e6e244b6
AS
915 }
916 US_DEBUGP("Transport: %s\n", us->transport_name);
917 US_DEBUGP("Protocol: %s\n", us->protocol_name);
918
919 /* fix for single-lun devices */
920 if (us->fflags & US_FL_SINGLE_LUN)
921 us->max_lun = 0;
922
923 /* Find the endpoints and calculate pipe values */
1da177e4
LT
924 result = get_pipes(us);
925 if (result)
926 goto BadDevice;
927
928 /* Acquire all the other resources and add the host */
929 result = usb_stor_acquire_resources(us);
930 if (result)
931 goto BadDevice;
00fa43ef
MW
932 snprintf(us->scsi_name, sizeof(us->scsi_name), "usb-storage %s",
933 dev_name(&us->pusb_intf->dev));
e6e244b6 934 result = scsi_add_host(us_to_host(us), &us->pusb_intf->dev);
1da177e4
LT
935 if (result) {
936 printk(KERN_WARNING USB_STORAGE
937 "Unable to add the scsi host\n");
938 goto BadDevice;
939 }
940
941 /* Start up the thread for delayed SCSI-device scanning */
3f13e66e
AS
942 th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
943 if (IS_ERR(th)) {
1da177e4
LT
944 printk(KERN_WARNING USB_STORAGE
945 "Unable to start the device-scanning thread\n");
543f7810 946 complete(&us->scanning_done);
77f46328 947 quiesce_and_remove_host(us);
3f13e66e 948 result = PTR_ERR(th);
1da177e4
LT
949 goto BadDevice;
950 }
1da177e4 951
3f13e66e 952 wake_up_process(th);
1da177e4
LT
953
954 return 0;
955
956 /* We come here if there are any problems */
957BadDevice:
958 US_DEBUGP("storage_probe() failed\n");
77f46328 959 release_everything(us);
1da177e4
LT
960 return result;
961}
e6e244b6 962EXPORT_SYMBOL_GPL(usb_stor_probe2);
1da177e4 963
e6e244b6
AS
964/* Handle a USB mass-storage disconnect */
965void usb_stor_disconnect(struct usb_interface *intf)
1da177e4
LT
966{
967 struct us_data *us = usb_get_intfdata(intf);
968
969 US_DEBUGP("storage_disconnect() called\n");
77f46328
MD
970 quiesce_and_remove_host(us);
971 release_everything(us);
1da177e4 972}
e6e244b6
AS
973EXPORT_SYMBOL_GPL(usb_stor_disconnect);
974
975/* The main probe routine for standard devices */
976static int storage_probe(struct usb_interface *intf,
977 const struct usb_device_id *id)
978{
979 struct us_data *us;
980 int result;
981
982 /*
983 * If libusual is configured, let it decide whether a standard
984 * device should be handled by usb-storage or by ub.
985 * If the device isn't standard (is handled by a subdriver
986 * module) then don't accept it.
987 */
988 if (usb_usual_check_type(id, USB_US_TYPE_STOR) ||
989 usb_usual_ignore_device(intf))
990 return -ENXIO;
991
992 /*
993 * Call the general probe procedures.
994 *
995 * The unusual_dev_list array is parallel to the usb_storage_usb_ids
996 * table, so we use the index of the id entry to find the
997 * corresponding unusual_devs entry.
998 */
999 result = usb_stor_probe1(&us, intf, id,
1000 (id - usb_storage_usb_ids) + us_unusual_dev_list);
1001 if (result)
1002 return result;
1003
1004 /* No special transport or protocol settings in the main module */
1005
1006 result = usb_stor_probe2(us);
1007 return result;
1008}
1da177e4
LT
1009
1010/***********************************************************************
1011 * Initialization and registration
1012 ***********************************************************************/
1013
ce2596df 1014static struct usb_driver usb_storage_driver = {
ce2596df
AS
1015 .name = "usb-storage",
1016 .probe = storage_probe,
e6e244b6
AS
1017 .disconnect = usb_stor_disconnect,
1018 .suspend = usb_stor_suspend,
1019 .resume = usb_stor_resume,
1020 .reset_resume = usb_stor_reset_resume,
1021 .pre_reset = usb_stor_pre_reset,
1022 .post_reset = usb_stor_post_reset,
1023 .id_table = usb_storage_usb_ids,
543f7810 1024 .soft_unbind = 1,
ce2596df
AS
1025};
1026
1da177e4
LT
1027static int __init usb_stor_init(void)
1028{
1029 int retval;
d4f373e5 1030
1da177e4
LT
1031 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1032
1033 /* register the driver, return usb_register return code if error */
1034 retval = usb_register(&usb_storage_driver);
a00828e9 1035 if (retval == 0) {
1da177e4 1036 printk(KERN_INFO "USB Mass Storage support registered.\n");
a00828e9
PZ
1037 usb_usual_set_present(USB_US_TYPE_STOR);
1038 }
1da177e4
LT
1039 return retval;
1040}
1041
1042static void __exit usb_stor_exit(void)
1043{
1044 US_DEBUGP("usb_stor_exit() called\n");
1045
1046 /* Deregister the driver
1047 * This will cause disconnect() to be called for each
1048 * attached unit
1049 */
1050 US_DEBUGP("-- calling usb_deregister()\n");
1051 usb_deregister(&usb_storage_driver) ;
1052
a00828e9 1053 usb_usual_clear_present(USB_US_TYPE_STOR);
1da177e4
LT
1054}
1055
1056module_init(usb_stor_init);
1057module_exit(usb_stor_exit);