USB: mark "reject" field of struct urb as atomic_t
[linux-2.6-block.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)
8 * (c) 2003 Alan Stern (stern@rowland.harvard.edu)
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
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
1da177e4
LT
78#ifdef CONFIG_USB_STORAGE_FREECOM
79#include "freecom.h"
80#endif
81#ifdef CONFIG_USB_STORAGE_ISD200
82#include "isd200.h"
83#endif
84#ifdef CONFIG_USB_STORAGE_DATAFAB
85#include "datafab.h"
86#endif
87#ifdef CONFIG_USB_STORAGE_JUMPSHOT
88#include "jumpshot.h"
89#endif
34008dbf
MD
90#ifdef CONFIG_USB_STORAGE_ONETOUCH
91#include "onetouch.h"
92#endif
e80b0fad
MD
93#ifdef CONFIG_USB_STORAGE_ALAUDA
94#include "alauda.h"
95#endif
dfe0d3ba
MD
96#ifdef CONFIG_USB_STORAGE_KARMA
97#include "karma.h"
98#endif
d277064e 99#ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
100#include "cypress_atacb.h"
101#endif
32fe5e39 102#include "sierra_ms.h"
1da177e4
LT
103
104/* Some informational data */
105MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
106MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
107MODULE_LICENSE("GPL");
108
109static unsigned int delay_use = 5;
110module_param(delay_use, uint, S_IRUGO | S_IWUSR);
111MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
112
d4f373e5
AS
113static char *quirks;
114module_param(quirks, charp, S_IRUGO);
115MODULE_PARM_DESC(quirks, "supplemental list of device IDs and their quirks");
116
117struct quirks_entry {
118 u16 vid, pid;
119 u32 fflags;
120};
121static struct quirks_entry *quirks_list, *quirks_end;
122
1da177e4 123
a00828e9
PZ
124/*
125 * The entries in this table correspond, line for line,
126 * with the entries of us_unusual_dev_list[].
1da177e4 127 */
a00828e9 128#ifndef CONFIG_USB_LIBUSUAL
1da177e4
LT
129
130#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
131 vendorName, productName,useProtocol, useTransport, \
132 initFunction, flags) \
a00828e9
PZ
133{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
134 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
135
136#define USUAL_DEV(useProto, useTrans, useType) \
137{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
138 .driver_info = (USB_US_TYPE_STOR<<24) }
1da177e4
LT
139
140static struct usb_device_id storage_usb_ids [] = {
141
142# include "unusual_devs.h"
143#undef UNUSUAL_DEV
a00828e9 144#undef USUAL_DEV
1da177e4
LT
145 /* Terminating entry */
146 { }
147};
148
149MODULE_DEVICE_TABLE (usb, storage_usb_ids);
a00828e9 150#endif /* CONFIG_USB_LIBUSUAL */
1da177e4
LT
151
152/* This is the list of devices we recognize, along with their flag data */
153
154/* The vendor name should be kept at eight characters or less, and
155 * the product name should be kept at 16 characters or less. If a device
156 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
157 * normally generated by a device thorugh the INQUIRY response will be
158 * taken from this list, and this is the reason for the above size
159 * restriction. However, if the flag is not present, then you
160 * are free to use as many characters as you like.
161 */
162
1da177e4
LT
163#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
164 vendor_name, product_name, use_protocol, use_transport, \
165 init_function, Flags) \
166{ \
167 .vendorName = vendor_name, \
168 .productName = product_name, \
169 .useProtocol = use_protocol, \
170 .useTransport = use_transport, \
171 .initFunction = init_function, \
a00828e9
PZ
172}
173
174#define USUAL_DEV(use_protocol, use_transport, use_type) \
175{ \
176 .useProtocol = use_protocol, \
177 .useTransport = use_transport, \
1da177e4
LT
178}
179
180static struct us_unusual_dev us_unusual_dev_list[] = {
181# include "unusual_devs.h"
182# undef UNUSUAL_DEV
a00828e9 183# undef USUAL_DEV
1da177e4
LT
184
185 /* Terminating entry */
186 { NULL }
187};
188
ce2596df
AS
189
190#ifdef CONFIG_PM /* Minimal support for suspend and resume */
191
192static int storage_suspend(struct usb_interface *iface, pm_message_t message)
193{
194 struct us_data *us = usb_get_intfdata(iface);
195
196 /* Wait until no command is running */
4186ecf8 197 mutex_lock(&us->dev_mutex);
ce2596df 198
441b62c1 199 US_DEBUGP("%s\n", __func__);
7931e1c6
MD
200 if (us->suspend_resume_hook)
201 (us->suspend_resume_hook)(us, US_SUSPEND);
ce2596df 202
d526875d
GKH
203 /* When runtime PM is working, we'll set a flag to indicate
204 * whether we should autoresume when a SCSI request arrives. */
205
4186ecf8 206 mutex_unlock(&us->dev_mutex);
ce2596df
AS
207 return 0;
208}
209
210static int storage_resume(struct usb_interface *iface)
211{
212 struct us_data *us = usb_get_intfdata(iface);
213
d526875d 214 mutex_lock(&us->dev_mutex);
8dfe4b14 215
441b62c1 216 US_DEBUGP("%s\n", __func__);
7931e1c6
MD
217 if (us->suspend_resume_hook)
218 (us->suspend_resume_hook)(us, US_RESUME);
ce2596df 219
d526875d 220 mutex_unlock(&us->dev_mutex);
ce2596df
AS
221 return 0;
222}
223
f07600cf
AS
224static int storage_reset_resume(struct usb_interface *iface)
225{
226 struct us_data *us = usb_get_intfdata(iface);
227
441b62c1 228 US_DEBUGP("%s\n", __func__);
f07600cf
AS
229
230 /* Report the reset to the SCSI core */
231 usb_stor_report_bus_reset(us);
232
233 /* FIXME: Notify the subdrivers that they need to reinitialize
234 * the device */
235 return 0;
236}
237
ce2596df 238#endif /* CONFIG_PM */
1da177e4 239
47104b0d
AS
240/*
241 * The next two routines get called just before and just after
242 * a USB port reset, whether from this driver or a different one.
243 */
244
f07600cf 245static int storage_pre_reset(struct usb_interface *iface)
47104b0d
AS
246{
247 struct us_data *us = usb_get_intfdata(iface);
248
441b62c1 249 US_DEBUGP("%s\n", __func__);
47104b0d
AS
250
251 /* Make sure no command runs during the reset */
252 mutex_lock(&us->dev_mutex);
f07600cf 253 return 0;
47104b0d
AS
254}
255
f07600cf 256static int storage_post_reset(struct usb_interface *iface)
47104b0d
AS
257{
258 struct us_data *us = usb_get_intfdata(iface);
259
441b62c1 260 US_DEBUGP("%s\n", __func__);
47104b0d
AS
261
262 /* Report the reset to the SCSI core */
47104b0d 263 usb_stor_report_bus_reset(us);
47104b0d
AS
264
265 /* FIXME: Notify the subdrivers that they need to reinitialize
266 * the device */
0458d5b4 267
f07600cf
AS
268 mutex_unlock(&us->dev_mutex);
269 return 0;
47104b0d
AS
270}
271
1da177e4
LT
272/*
273 * fill_inquiry_response takes an unsigned char array (which must
274 * be at least 36 characters) and populates the vendor name,
275 * product name, and revision fields. Then the array is copied
276 * into the SCSI command's response buffer (oddly enough
277 * called request_buffer). data_len contains the length of the
278 * data array, which again must be at least 36.
279 */
280
281void fill_inquiry_response(struct us_data *us, unsigned char *data,
282 unsigned int data_len)
283{
284 if (data_len<36) // You lose.
285 return;
286
287 if(data[0]&0x20) { /* USB device currently not connected. Return
288 peripheral qualifier 001b ("...however, the
289 physical device is not currently connected
290 to this logical unit") and leave vendor and
291 product identification empty. ("If the target
292 does store some of the INQUIRY data on the
293 device, it may return zeros or ASCII spaces
294 (20h) in those fields until the data is
295 available from the device."). */
296 memset(data+8,0,28);
297 } else {
298 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
299 memcpy(data+8, us->unusual_dev->vendorName,
300 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
301 strlen(us->unusual_dev->vendorName));
302 memcpy(data+16, us->unusual_dev->productName,
303 strlen(us->unusual_dev->productName) > 16 ? 16 :
304 strlen(us->unusual_dev->productName));
305 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
306 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
307 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
308 data[35] = 0x30 + ((bcdDevice) & 0x0F);
309 }
310
311 usb_stor_set_xfer_buf(data, data_len, us->srb);
312}
313
314static int usb_stor_control_thread(void * __us)
315{
316 struct us_data *us = (struct us_data *)__us;
317 struct Scsi_Host *host = us_to_host(us);
318
1da177e4
LT
319 for(;;) {
320 US_DEBUGP("*** thread sleeping.\n");
7119e3c3 321 if (wait_for_completion_interruptible(&us->cmnd_ready))
1da177e4 322 break;
7119e3c3 323
1da177e4
LT
324 US_DEBUGP("*** thread awakened.\n");
325
326 /* lock the device pointers */
4186ecf8 327 mutex_lock(&(us->dev_mutex));
1da177e4 328
543f7810
AS
329 /* lock access to the state */
330 scsi_lock(host);
331
332 /* When we are called with no command pending, we're done */
333 if (us->srb == NULL) {
334 scsi_unlock(host);
4186ecf8 335 mutex_unlock(&us->dev_mutex);
543f7810 336 US_DEBUGP("-- exiting\n");
1da177e4
LT
337 break;
338 }
339
1da177e4 340 /* has the command timed out *already* ? */
7e4d6c38 341 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
1da177e4
LT
342 us->srb->result = DID_ABORT << 16;
343 goto SkipForAbort;
344 }
345
346 scsi_unlock(host);
347
348 /* reject the command if the direction indicator
349 * is UNKNOWN
350 */
351 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
352 US_DEBUGP("UNKNOWN data direction\n");
353 us->srb->result = DID_ERROR << 16;
354 }
355
356 /* reject if target != 0 or if LUN is higher than
357 * the maximum known LUN
358 */
359 else if (us->srb->device->id &&
7e4d6c38 360 !(us->fflags & US_FL_SCM_MULT_TARG)) {
1da177e4
LT
361 US_DEBUGP("Bad target number (%d:%d)\n",
362 us->srb->device->id, us->srb->device->lun);
363 us->srb->result = DID_BAD_TARGET << 16;
364 }
365
366 else if (us->srb->device->lun > us->max_lun) {
367 US_DEBUGP("Bad LUN (%d:%d)\n",
368 us->srb->device->id, us->srb->device->lun);
369 us->srb->result = DID_BAD_TARGET << 16;
370 }
371
372 /* Handle those devices which need us to fake
373 * their inquiry data */
374 else if ((us->srb->cmnd[0] == INQUIRY) &&
7e4d6c38 375 (us->fflags & US_FL_FIX_INQUIRY)) {
1da177e4
LT
376 unsigned char data_ptr[36] = {
377 0x00, 0x80, 0x02, 0x02,
378 0x1F, 0x00, 0x00, 0x00};
379
380 US_DEBUGP("Faking INQUIRY command\n");
381 fill_inquiry_response(us, data_ptr, 36);
382 us->srb->result = SAM_STAT_GOOD;
383 }
384
385 /* we've got a command, let's do it! */
386 else {
387 US_DEBUG(usb_stor_show_command(us->srb));
388 us->proto_handler(us->srb, us);
389 }
390
391 /* lock access to the state */
392 scsi_lock(host);
393
394 /* indicate that the command is done */
543f7810 395 if (us->srb->result != DID_ABORT << 16) {
1da177e4
LT
396 US_DEBUGP("scsi cmd done, result=0x%x\n",
397 us->srb->result);
398 us->srb->scsi_done(us->srb);
399 } else {
400SkipForAbort:
401 US_DEBUGP("scsi command aborted\n");
402 }
403
404 /* If an abort request was received we need to signal that
405 * the abort has finished. The proper test for this is
406 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
226173ed
MD
407 * the timeout might have occurred after the command had
408 * already completed with a different result code. */
7e4d6c38 409 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
1da177e4
LT
410 complete(&(us->notify));
411
226173ed 412 /* Allow USB transfers to resume */
7e4d6c38
AS
413 clear_bit(US_FLIDX_ABORTING, &us->dflags);
414 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
226173ed
MD
415 }
416
1da177e4
LT
417 /* finished working on this command */
418 us->srb = NULL;
419 scsi_unlock(host);
420
421 /* unlock the device pointers */
4186ecf8 422 mutex_unlock(&us->dev_mutex);
1da177e4
LT
423 } /* for (;;) */
424
ed76cacb
AS
425 /* Wait until we are told to stop */
426 for (;;) {
427 set_current_state(TASK_INTERRUPTIBLE);
428 if (kthread_should_stop())
429 break;
430 schedule();
431 }
432 __set_current_state(TASK_RUNNING);
433 return 0;
1da177e4
LT
434}
435
436/***********************************************************************
437 * Device probing and disconnecting
438 ***********************************************************************/
439
440/* Associate our private data with the USB device */
441static int associate_dev(struct us_data *us, struct usb_interface *intf)
442{
441b62c1 443 US_DEBUGP("-- %s\n", __func__);
1da177e4
LT
444
445 /* Fill in the device-related fields */
446 us->pusb_dev = interface_to_usbdev(intf);
447 us->pusb_intf = intf;
448 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
449 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
450 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
451 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
452 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
453 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
454 intf->cur_altsetting->desc.bInterfaceSubClass,
455 intf->cur_altsetting->desc.bInterfaceProtocol);
456
457 /* Store our private data in the interface */
458 usb_set_intfdata(intf, us);
459
460 /* Allocate the device-related DMA-mapped buffers */
461 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
462 GFP_KERNEL, &us->cr_dma);
463 if (!us->cr) {
464 US_DEBUGP("usb_ctrlrequest allocation failed\n");
465 return -ENOMEM;
466 }
467
468 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
469 GFP_KERNEL, &us->iobuf_dma);
470 if (!us->iobuf) {
471 US_DEBUGP("I/O buffer allocation failed\n");
472 return -ENOMEM;
473 }
474 return 0;
475}
476
d4f373e5
AS
477/* Adjust device flags based on the "quirks=" module parameter */
478static void adjust_quirks(struct us_data *us)
479{
480 u16 vid, pid;
481 struct quirks_entry *q;
482 unsigned int mask = (US_FL_FIX_CAPACITY | US_FL_IGNORE_DEVICE |
483 US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 |
484 US_FL_IGNORE_RESIDUE | US_FL_SINGLE_LUN |
485 US_FL_NO_WP_DETECT);
486
487 vid = le16_to_cpu(us->pusb_dev->descriptor.idVendor);
488 pid = le16_to_cpu(us->pusb_dev->descriptor.idProduct);
489
490 for (q = quirks_list; q != quirks_end; ++q) {
491 if (q->vid == vid && q->pid == pid) {
492 us->fflags = (us->fflags & ~mask) | q->fflags;
493 dev_info(&us->pusb_intf->dev, "Quirks match for "
494 "vid %04x pid %04x: %x\n",
495 vid, pid, q->fflags);
496 break;
497 }
498 }
499}
500
a00828e9
PZ
501/* Find an unusual_dev descriptor (always succeeds in the current code) */
502static struct us_unusual_dev *find_unusual(const struct usb_device_id *id)
503{
504 const int id_index = id - storage_usb_ids;
505 return &us_unusual_dev_list[id_index];
506}
507
1da177e4 508/* Get the unusual_devs entries and the string descriptors */
3c332422 509static int get_device_info(struct us_data *us, const struct usb_device_id *id)
1da177e4
LT
510{
511 struct usb_device *dev = us->pusb_dev;
512 struct usb_interface_descriptor *idesc =
513 &us->pusb_intf->cur_altsetting->desc;
a00828e9 514 struct us_unusual_dev *unusual_dev = find_unusual(id);
1da177e4
LT
515
516 /* Store the entries */
517 us->unusual_dev = unusual_dev;
518 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
519 idesc->bInterfaceSubClass :
520 unusual_dev->useProtocol;
521 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
522 idesc->bInterfaceProtocol :
523 unusual_dev->useTransport;
7e4d6c38 524 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
d4f373e5 525 adjust_quirks(us);
1da177e4 526
7e4d6c38 527 if (us->fflags & US_FL_IGNORE_DEVICE) {
3c332422
DD
528 printk(KERN_INFO USB_STORAGE "device ignored\n");
529 return -ENODEV;
530 }
531
1da177e4
LT
532 /*
533 * This flag is only needed when we're in high-speed, so let's
534 * disable it if we're in full-speed
535 */
536 if (dev->speed != USB_SPEED_HIGH)
7e4d6c38 537 us->fflags &= ~US_FL_GO_SLOW;
1da177e4
LT
538
539 /* Log a message if a non-generic unusual_dev entry contains an
540 * unnecessary subclass or protocol override. This may stimulate
541 * reports from users that will help us remove unneeded entries
542 * from the unusual_devs.h table.
543 */
544 if (id->idVendor || id->idProduct) {
4c4c9432 545 static const char *msgs[3] = {
1da177e4
LT
546 "an unneeded SubClass entry",
547 "an unneeded Protocol entry",
548 "unneeded SubClass and Protocol entries"};
549 struct usb_device_descriptor *ddesc = &dev->descriptor;
550 int msg = -1;
551
552 if (unusual_dev->useProtocol != US_SC_DEVICE &&
553 us->subclass == idesc->bInterfaceSubClass)
554 msg += 1;
555 if (unusual_dev->useTransport != US_PR_DEVICE &&
556 us->protocol == idesc->bInterfaceProtocol)
557 msg += 2;
7e4d6c38 558 if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
1da177e4
LT
559 printk(KERN_NOTICE USB_STORAGE "This device "
560 "(%04x,%04x,%04x S %02x P %02x)"
5650b4dd
PD
561 " has %s in unusual_devs.h (kernel"
562 " %s)\n"
1da177e4 563 " Please send a copy of this message to "
cef03f8f
AL
564 "<linux-usb@vger.kernel.org> and "
565 "<usb-storage@lists.one-eyed-alien.net>\n",
1da177e4
LT
566 le16_to_cpu(ddesc->idVendor),
567 le16_to_cpu(ddesc->idProduct),
568 le16_to_cpu(ddesc->bcdDevice),
569 idesc->bInterfaceSubClass,
570 idesc->bInterfaceProtocol,
5650b4dd 571 msgs[msg],
00f8b0c1 572 utsname()->release);
1da177e4 573 }
3c332422
DD
574
575 return 0;
1da177e4
LT
576}
577
578/* Get the transport settings */
579static int get_transport(struct us_data *us)
580{
581 switch (us->protocol) {
582 case US_PR_CB:
583 us->transport_name = "Control/Bulk";
584 us->transport = usb_stor_CB_transport;
585 us->transport_reset = usb_stor_CB_reset;
586 us->max_lun = 7;
587 break;
588
589 case US_PR_CBI:
590 us->transport_name = "Control/Bulk/Interrupt";
64648a9d 591 us->transport = usb_stor_CB_transport;
1da177e4
LT
592 us->transport_reset = usb_stor_CB_reset;
593 us->max_lun = 7;
594 break;
595
596 case US_PR_BULK:
597 us->transport_name = "Bulk";
598 us->transport = usb_stor_Bulk_transport;
599 us->transport_reset = usb_stor_Bulk_reset;
600 break;
601
602#ifdef CONFIG_USB_STORAGE_USBAT
b7b1e655
DD
603 case US_PR_USBAT:
604 us->transport_name = "Shuttle USBAT";
1da177e4
LT
605 us->transport = usbat_transport;
606 us->transport_reset = usb_stor_CB_reset;
607 us->max_lun = 1;
608 break;
609#endif
610
611#ifdef CONFIG_USB_STORAGE_SDDR09
612 case US_PR_EUSB_SDDR09:
613 us->transport_name = "EUSB/SDDR09";
614 us->transport = sddr09_transport;
615 us->transport_reset = usb_stor_CB_reset;
616 us->max_lun = 0;
617 break;
618#endif
619
620#ifdef CONFIG_USB_STORAGE_SDDR55
621 case US_PR_SDDR55:
622 us->transport_name = "SDDR55";
623 us->transport = sddr55_transport;
624 us->transport_reset = sddr55_reset;
625 us->max_lun = 0;
626 break;
627#endif
628
629#ifdef CONFIG_USB_STORAGE_DPCM
630 case US_PR_DPCM_USB:
631 us->transport_name = "Control/Bulk-EUSB/SDDR09";
632 us->transport = dpcm_transport;
633 us->transport_reset = usb_stor_CB_reset;
634 us->max_lun = 1;
635 break;
636#endif
637
638#ifdef CONFIG_USB_STORAGE_FREECOM
639 case US_PR_FREECOM:
640 us->transport_name = "Freecom";
641 us->transport = freecom_transport;
642 us->transport_reset = usb_stor_freecom_reset;
643 us->max_lun = 0;
644 break;
645#endif
646
647#ifdef CONFIG_USB_STORAGE_DATAFAB
648 case US_PR_DATAFAB:
649 us->transport_name = "Datafab Bulk-Only";
650 us->transport = datafab_transport;
651 us->transport_reset = usb_stor_Bulk_reset;
652 us->max_lun = 1;
653 break;
654#endif
655
656#ifdef CONFIG_USB_STORAGE_JUMPSHOT
657 case US_PR_JUMPSHOT:
658 us->transport_name = "Lexar Jumpshot Control/Bulk";
659 us->transport = jumpshot_transport;
660 us->transport_reset = usb_stor_Bulk_reset;
661 us->max_lun = 1;
662 break;
663#endif
664
b383539e
DD
665#ifdef CONFIG_USB_STORAGE_ALAUDA
666 case US_PR_ALAUDA:
667 us->transport_name = "Alauda Control/Bulk";
668 us->transport = alauda_transport;
669 us->transport_reset = usb_stor_Bulk_reset;
670 us->max_lun = 1;
671 break;
672#endif
673
dfe0d3ba
MD
674#ifdef CONFIG_USB_STORAGE_KARMA
675 case US_PR_KARMA:
676 us->transport_name = "Rio Karma/Bulk";
677 us->transport = rio_karma_transport;
678 us->transport_reset = usb_stor_Bulk_reset;
679 break;
680#endif
681
1da177e4
LT
682 default:
683 return -EIO;
684 }
685 US_DEBUGP("Transport: %s\n", us->transport_name);
686
687 /* fix for single-lun devices */
7e4d6c38 688 if (us->fflags & US_FL_SINGLE_LUN)
1da177e4
LT
689 us->max_lun = 0;
690 return 0;
691}
692
693/* Get the protocol settings */
694static int get_protocol(struct us_data *us)
695{
696 switch (us->subclass) {
697 case US_SC_RBC:
698 us->protocol_name = "Reduced Block Commands (RBC)";
699 us->proto_handler = usb_stor_transparent_scsi_command;
700 break;
701
702 case US_SC_8020:
703 us->protocol_name = "8020i";
3dae5345 704 us->proto_handler = usb_stor_pad12_command;
1da177e4
LT
705 us->max_lun = 0;
706 break;
707
708 case US_SC_QIC:
709 us->protocol_name = "QIC-157";
3dae5345 710 us->proto_handler = usb_stor_pad12_command;
1da177e4
LT
711 us->max_lun = 0;
712 break;
713
714 case US_SC_8070:
715 us->protocol_name = "8070i";
3dae5345 716 us->proto_handler = usb_stor_pad12_command;
1da177e4
LT
717 us->max_lun = 0;
718 break;
719
720 case US_SC_SCSI:
721 us->protocol_name = "Transparent SCSI";
722 us->proto_handler = usb_stor_transparent_scsi_command;
723 break;
724
725 case US_SC_UFI:
726 us->protocol_name = "Uniform Floppy Interface (UFI)";
727 us->proto_handler = usb_stor_ufi_command;
728 break;
729
730#ifdef CONFIG_USB_STORAGE_ISD200
731 case US_SC_ISD200:
732 us->protocol_name = "ISD200 ATA/ATAPI";
733 us->proto_handler = isd200_ata_command;
734 break;
735#endif
736
d277064e 737#ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
738 case US_SC_CYP_ATACB:
739 us->protocol_name = "Transparent SCSI with Cypress ATACB";
740 us->proto_handler = cypress_atacb_passthrough;
741 break;
742#endif
743
1da177e4
LT
744 default:
745 return -EIO;
746 }
747 US_DEBUGP("Protocol: %s\n", us->protocol_name);
748 return 0;
749}
750
751/* Get the pipe settings */
752static int get_pipes(struct us_data *us)
753{
754 struct usb_host_interface *altsetting =
755 us->pusb_intf->cur_altsetting;
756 int i;
757 struct usb_endpoint_descriptor *ep;
758 struct usb_endpoint_descriptor *ep_in = NULL;
759 struct usb_endpoint_descriptor *ep_out = NULL;
760 struct usb_endpoint_descriptor *ep_int = NULL;
761
762 /*
1096f780 763 * Find the first endpoint of each type we need.
1da177e4 764 * We are expecting a minimum of 2 endpoints - in and out (bulk).
1096f780 765 * An optional interrupt-in is OK (necessary for CBI protocol).
1da177e4
LT
766 * We will ignore any others.
767 */
768 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
769 ep = &altsetting->endpoint[i].desc;
770
04720747 771 if (usb_endpoint_xfer_bulk(ep)) {
1096f780
AS
772 if (usb_endpoint_dir_in(ep)) {
773 if (!ep_in)
774 ep_in = ep;
775 } else {
776 if (!ep_out)
777 ep_out = ep;
778 }
1da177e4
LT
779 }
780
1096f780
AS
781 else if (usb_endpoint_is_int_in(ep)) {
782 if (!ep_int)
783 ep_int = ep;
1da177e4
LT
784 }
785 }
786
787 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
788 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
789 return -EIO;
790 }
791
792 /* Calculate and store the pipe values */
793 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
794 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
795 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
796 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
797 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
798 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
799 if (ep_int) {
800 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
801 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
802 us->ep_bInterval = ep_int->bInterval;
803 }
804 return 0;
805}
806
807/* Initialize all the dynamic resources we need */
808static int usb_stor_acquire_resources(struct us_data *us)
809{
810 int p;
3f13e66e 811 struct task_struct *th;
1da177e4
LT
812
813 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
814 if (!us->current_urb) {
815 US_DEBUGP("URB allocation failed\n");
816 return -ENOMEM;
817 }
818
1da177e4
LT
819 /* Just before we start our control thread, initialize
820 * the device if it needs initialization */
b876aef7
AS
821 if (us->unusual_dev->initFunction) {
822 p = us->unusual_dev->initFunction(us);
823 if (p)
824 return p;
825 }
1da177e4
LT
826
827 /* Start up our control thread */
ed76cacb 828 th = kthread_run(usb_stor_control_thread, us, "usb-storage");
3f13e66e 829 if (IS_ERR(th)) {
1da177e4
LT
830 printk(KERN_WARNING USB_STORAGE
831 "Unable to start control thread\n");
3f13e66e 832 return PTR_ERR(th);
1da177e4 833 }
ed76cacb 834 us->ctl_thread = th;
1da177e4
LT
835
836 return 0;
837}
838
839/* Release all our dynamic resources */
840static void usb_stor_release_resources(struct us_data *us)
841{
441b62c1 842 US_DEBUGP("-- %s\n", __func__);
1da177e4
LT
843
844 /* Tell the control thread to exit. The SCSI host must
543f7810
AS
845 * already have been removed and the DISCONNECTING flag set
846 * so that we won't accept any more commands.
1da177e4
LT
847 */
848 US_DEBUGP("-- sending exit command to thread\n");
7119e3c3 849 complete(&us->cmnd_ready);
ed76cacb
AS
850 if (us->ctl_thread)
851 kthread_stop(us->ctl_thread);
1da177e4
LT
852
853 /* Call the destructor routine, if it exists */
854 if (us->extra_destructor) {
855 US_DEBUGP("-- calling extra_destructor()\n");
856 us->extra_destructor(us->extra);
857 }
858
859 /* Free the extra data and the URB */
860 kfree(us->extra);
861 usb_free_urb(us->current_urb);
862}
863
864/* Dissociate from the USB device */
865static void dissociate_dev(struct us_data *us)
866{
441b62c1 867 US_DEBUGP("-- %s\n", __func__);
1da177e4
LT
868
869 /* Free the device-related DMA-mapped buffers */
870 if (us->cr)
871 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
872 us->cr_dma);
873 if (us->iobuf)
874 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
875 us->iobuf_dma);
876
877 /* Remove our private data from the interface */
878 usb_set_intfdata(us->pusb_intf, NULL);
879}
880
543f7810
AS
881/* First stage of disconnect processing: stop SCSI scanning,
882 * remove the host, and stop accepting new commands
883 */
77f46328
MD
884static void quiesce_and_remove_host(struct us_data *us)
885{
eecd11ed
AS
886 struct Scsi_Host *host = us_to_host(us);
887
543f7810
AS
888 /* If the device is really gone, cut short reset delays */
889 if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
890 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
77f46328 891
543f7810
AS
892 /* Prevent SCSI-scanning (if it hasn't started yet)
893 * and wait for the SCSI-scanning thread to stop.
894 */
895 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
896 wake_up(&us->delay_wait);
897 wait_for_completion(&us->scanning_done);
26186ba7 898
543f7810
AS
899 /* Removing the host will perform an orderly shutdown: caches
900 * synchronized, disks spun down, etc.
901 */
eecd11ed 902 scsi_remove_host(host);
2f67cd5b 903
543f7810
AS
904 /* Prevent any new commands from being accepted and cut short
905 * reset delays.
906 */
907 scsi_lock(host);
908 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
909 scsi_unlock(host);
910 wake_up(&us->delay_wait);
77f46328
MD
911}
912
913/* Second stage of disconnect processing: deallocate all resources */
914static void release_everything(struct us_data *us)
915{
916 usb_stor_release_resources(us);
917 dissociate_dev(us);
918
919 /* Drop our reference to the host; the SCSI core will free it
920 * (and "us" along with it) when the refcount becomes 0. */
921 scsi_host_put(us_to_host(us));
922}
923
1da177e4
LT
924/* Thread to carry out delayed SCSI-device scanning */
925static int usb_stor_scan_thread(void * __us)
926{
927 struct us_data *us = (struct us_data *)__us;
928
1da177e4
LT
929 printk(KERN_DEBUG
930 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
931
83144186 932 set_freezable();
1da177e4
LT
933 /* Wait for the timeout to expire or for a disconnect */
934 if (delay_use > 0) {
935 printk(KERN_DEBUG "usb-storage: waiting for device "
936 "to settle before scanning\n");
e42837bc 937 wait_event_freezable_timeout(us->delay_wait,
543f7810 938 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
1da177e4 939 delay_use * HZ);
1da177e4
LT
940 }
941
942 /* If the device is still connected, perform the scanning */
543f7810 943 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
b876aef7
AS
944
945 /* For bulk-only devices, determine the max LUN value */
946 if (us->protocol == US_PR_BULK &&
7e4d6c38 947 !(us->fflags & US_FL_SINGLE_LUN)) {
4186ecf8 948 mutex_lock(&us->dev_mutex);
b876aef7 949 us->max_lun = usb_stor_Bulk_max_lun(us);
4186ecf8 950 mutex_unlock(&us->dev_mutex);
b876aef7 951 }
1da177e4
LT
952 scsi_scan_host(us_to_host(us));
953 printk(KERN_DEBUG "usb-storage: device scan complete\n");
954
955 /* Should we unbind if no devices were detected? */
956 }
957
2f67cd5b 958 complete_and_exit(&us->scanning_done, 0);
1da177e4
LT
959}
960
961
962/* Probe to see if we can drive a newly-connected USB device */
963static int storage_probe(struct usb_interface *intf,
964 const struct usb_device_id *id)
965{
966 struct Scsi_Host *host;
967 struct us_data *us;
1da177e4 968 int result;
3f13e66e 969 struct task_struct *th;
1da177e4 970
a00828e9
PZ
971 if (usb_usual_check_type(id, USB_US_TYPE_STOR))
972 return -ENXIO;
973
1da177e4
LT
974 US_DEBUGP("USB Mass Storage device detected\n");
975
976 /*
977 * Ask the SCSI layer to allocate a host structure, with extra
978 * space at the end for our private us_data structure.
979 */
980 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
981 if (!host) {
982 printk(KERN_WARNING USB_STORAGE
983 "Unable to allocate the scsi host\n");
984 return -ENOMEM;
985 }
986
17f06022
RS
987 /*
988 * Allow 16-byte CDBs and thus > 2TB
989 */
990 host->max_cmd_len = 16;
1da177e4
LT
991 us = host_to_us(host);
992 memset(us, 0, sizeof(struct us_data));
4186ecf8 993 mutex_init(&(us->dev_mutex));
7119e3c3 994 init_completion(&us->cmnd_ready);
1da177e4
LT
995 init_completion(&(us->notify));
996 init_waitqueue_head(&us->delay_wait);
2f67cd5b 997 init_completion(&us->scanning_done);
1da177e4
LT
998
999 /* Associate the us_data structure with the USB device */
1000 result = associate_dev(us, intf);
1001 if (result)
1002 goto BadDevice;
1003
1004 /*
1005 * Get the unusual_devs entries and the descriptors
1006 *
1007 * id_index is calculated in the declaration to be the index number
1008 * of the match from the usb_device_id table, so we can find the
1009 * corresponding entry in the private table.
1010 */
3c332422
DD
1011 result = get_device_info(us, id);
1012 if (result)
1013 goto BadDevice;
1da177e4 1014
1da177e4
LT
1015 /* Get the transport, protocol, and pipe settings */
1016 result = get_transport(us);
1017 if (result)
1018 goto BadDevice;
1019 result = get_protocol(us);
1020 if (result)
1021 goto BadDevice;
1022 result = get_pipes(us);
1023 if (result)
1024 goto BadDevice;
1025
1026 /* Acquire all the other resources and add the host */
1027 result = usb_stor_acquire_resources(us);
1028 if (result)
1029 goto BadDevice;
1030 result = scsi_add_host(host, &intf->dev);
1031 if (result) {
1032 printk(KERN_WARNING USB_STORAGE
1033 "Unable to add the scsi host\n");
1034 goto BadDevice;
1035 }
1036
1037 /* Start up the thread for delayed SCSI-device scanning */
3f13e66e
AS
1038 th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
1039 if (IS_ERR(th)) {
1da177e4
LT
1040 printk(KERN_WARNING USB_STORAGE
1041 "Unable to start the device-scanning thread\n");
543f7810 1042 complete(&us->scanning_done);
77f46328 1043 quiesce_and_remove_host(us);
3f13e66e 1044 result = PTR_ERR(th);
1da177e4
LT
1045 goto BadDevice;
1046 }
1da177e4 1047
3f13e66e 1048 wake_up_process(th);
1da177e4
LT
1049
1050 return 0;
1051
1052 /* We come here if there are any problems */
1053BadDevice:
1054 US_DEBUGP("storage_probe() failed\n");
77f46328 1055 release_everything(us);
1da177e4
LT
1056 return result;
1057}
1058
1059/* Handle a disconnect event from the USB core */
1060static void storage_disconnect(struct usb_interface *intf)
1061{
1062 struct us_data *us = usb_get_intfdata(intf);
1063
1064 US_DEBUGP("storage_disconnect() called\n");
77f46328
MD
1065 quiesce_and_remove_host(us);
1066 release_everything(us);
1da177e4
LT
1067}
1068
1069/***********************************************************************
1070 * Initialization and registration
1071 ***********************************************************************/
1072
ce2596df 1073static struct usb_driver usb_storage_driver = {
ce2596df
AS
1074 .name = "usb-storage",
1075 .probe = storage_probe,
1076 .disconnect = storage_disconnect,
1077#ifdef CONFIG_PM
1078 .suspend = storage_suspend,
1079 .resume = storage_resume,
f07600cf 1080 .reset_resume = storage_reset_resume,
ce2596df 1081#endif
47104b0d
AS
1082 .pre_reset = storage_pre_reset,
1083 .post_reset = storage_post_reset,
ce2596df 1084 .id_table = storage_usb_ids,
543f7810 1085 .soft_unbind = 1,
ce2596df
AS
1086};
1087
d4f373e5
AS
1088/* Works only for digits and letters, but small and fast */
1089#define TOLOWER(x) ((x) | 0x20)
1090
1091static void __init parse_quirks(void)
1092{
1093 int n, i;
1094 char *p;
1095
1096 if (!quirks)
1097 return;
1098
1099 /* Count the ':' characters to get 2 * the number of entries */
1100 n = 0;
1101 for (p = quirks; *p; ++p) {
1102 if (*p == ':')
1103 ++n;
1104 }
1105 n /= 2;
1106 if (n == 0)
1107 return; /* Don't allocate 0 bytes */
1108
1109 quirks_list = kmalloc(n * sizeof(*quirks_list), GFP_KERNEL);
1110 if (!quirks_list)
1111 return;
1112
1113 p = quirks;
1114 quirks_end = quirks_list;
1115 for (i = 0; i < n && *p; ++i) {
1116 unsigned f = 0;
1117
1118 /* Each entry consists of VID:PID:flags */
1119 quirks_end->vid = simple_strtoul(p, &p, 16);
1120 if (*p != ':')
1121 goto skip_to_next;
1122 quirks_end->pid = simple_strtoul(p+1, &p, 16);
1123 if (*p != ':')
1124 goto skip_to_next;
1125
1126 while (*++p && *p != ',') {
1127 switch (TOLOWER(*p)) {
1128 case 'c':
1129 f |= US_FL_FIX_CAPACITY;
1130 break;
1131 case 'i':
1132 f |= US_FL_IGNORE_DEVICE;
1133 break;
1134 case 'l':
1135 f |= US_FL_NOT_LOCKABLE;
1136 break;
1137 case 'm':
1138 f |= US_FL_MAX_SECTORS_64;
1139 break;
1140 case 'r':
1141 f |= US_FL_IGNORE_RESIDUE;
1142 break;
1143 case 's':
1144 f |= US_FL_SINGLE_LUN;
1145 break;
1146 case 'w':
1147 f |= US_FL_NO_WP_DETECT;
1148 break;
1149 /* Ignore unrecognized flag characters */
1150 }
1151 }
1152 quirks_end->fflags = f;
1153 ++quirks_end;
1154
1155 skip_to_next:
1156 /* Entries are separated by commas */
1157 while (*p) {
1158 if (*p++ == ',')
1159 break;
1160 }
1161 } /* for (i = 0; ...) */
1162}
1163
1da177e4
LT
1164static int __init usb_stor_init(void)
1165{
1166 int retval;
d4f373e5 1167
1da177e4 1168 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
d4f373e5 1169 parse_quirks();
1da177e4
LT
1170
1171 /* register the driver, return usb_register return code if error */
1172 retval = usb_register(&usb_storage_driver);
a00828e9 1173 if (retval == 0) {
1da177e4 1174 printk(KERN_INFO "USB Mass Storage support registered.\n");
a00828e9
PZ
1175 usb_usual_set_present(USB_US_TYPE_STOR);
1176 }
1da177e4
LT
1177 return retval;
1178}
1179
1180static void __exit usb_stor_exit(void)
1181{
1182 US_DEBUGP("usb_stor_exit() called\n");
1183
1184 /* Deregister the driver
1185 * This will cause disconnect() to be called for each
1186 * attached unit
1187 */
1188 US_DEBUGP("-- calling usb_deregister()\n");
1189 usb_deregister(&usb_storage_driver) ;
1190
a00828e9 1191 usb_usual_clear_present(USB_US_TYPE_STOR);
1da177e4
LT
1192}
1193
1194module_init(usb_stor_init);
1195module_exit(usb_stor_exit);