blob: b25c448d5eb72c6715a442e70c1f77cbb5da47c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Driver for USB Mass Storage compliant devices
2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/sched.h>
49#include <linux/errno.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080050#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/slab.h>
Alan Stern3f13e662005-10-23 19:43:36 -070054#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010055#include <linux/mutex.h>
Sam Ravnborg00f8b0c2007-03-05 00:30:55 -080056#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#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
Matthew Dharm34008db2005-07-28 14:49:01 -070090#ifdef CONFIG_USB_STORAGE_ONETOUCH
91#include "onetouch.h"
92#endif
Matthew Dharme80b0fa2005-12-04 22:02:44 -080093#ifdef CONFIG_USB_STORAGE_ALAUDA
94#include "alauda.h"
95#endif
Matthew Dharmdfe0d3b2006-08-13 17:30:14 -070096#ifdef CONFIG_USB_STORAGE_KARMA
97#include "karma.h"
98#endif
matthieu castetd2770642008-03-19 19:40:52 +010099#ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
100#include "cypress_atacb.h"
101#endif
Kevin Lloyd32fe5e32008-07-10 14:14:57 -0700102#include "sierra_ms.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
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
Alan Sternd4f373e2008-11-10 14:07:45 -0500113static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Pete Zaitceva00828e2005-10-22 20:15:09 -0700124/*
125 * The entries in this table correspond, line for line,
126 * with the entries of us_unusual_dev_list[].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 */
Pete Zaitceva00828e2005-10-22 20:15:09 -0700128#ifndef CONFIG_USB_LIBUSUAL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
131 vendorName, productName,useProtocol, useTransport, \
132 initFunction, flags) \
Pete Zaitceva00828e2005-10-22 20:15:09 -0700133{ 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) }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140static struct usb_device_id storage_usb_ids [] = {
141
142# include "unusual_devs.h"
143#undef UNUSUAL_DEV
Pete Zaitceva00828e2005-10-22 20:15:09 -0700144#undef USUAL_DEV
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Terminating entry */
146 { }
147};
148
149MODULE_DEVICE_TABLE (usb, storage_usb_ids);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700150#endif /* CONFIG_USB_LIBUSUAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#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, \
Pete Zaitceva00828e2005-10-22 20:15:09 -0700172}
173
174#define USUAL_DEV(use_protocol, use_transport, use_type) \
175{ \
176 .useProtocol = use_protocol, \
177 .useTransport = use_transport, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static struct us_unusual_dev us_unusual_dev_list[] = {
181# include "unusual_devs.h"
182# undef UNUSUAL_DEV
Pete Zaitceva00828e2005-10-22 20:15:09 -0700183# undef USUAL_DEV
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /* Terminating entry */
186 { NULL }
187};
188
Alan Sternce2596d2005-10-23 19:41:39 -0700189
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 */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100197 mutex_lock(&us->dev_mutex);
Alan Sternce2596d2005-10-23 19:41:39 -0700198
Harvey Harrison441b62c2008-03-03 16:08:34 -0800199 US_DEBUGP("%s\n", __func__);
Matthew Dharm7931e1c2005-12-04 21:56:51 -0800200 if (us->suspend_resume_hook)
201 (us->suspend_resume_hook)(us, US_SUSPEND);
Alan Sternce2596d2005-10-23 19:41:39 -0700202
Greg Kroah-Hartmand5268752007-09-13 06:01:24 -0700203 /* When runtime PM is working, we'll set a flag to indicate
204 * whether we should autoresume when a SCSI request arrives. */
205
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100206 mutex_unlock(&us->dev_mutex);
Alan Sternce2596d2005-10-23 19:41:39 -0700207 return 0;
208}
209
210static int storage_resume(struct usb_interface *iface)
211{
212 struct us_data *us = usb_get_intfdata(iface);
213
Greg Kroah-Hartmand5268752007-09-13 06:01:24 -0700214 mutex_lock(&us->dev_mutex);
Alan Stern8dfe4b12007-07-06 14:24:27 -0400215
Harvey Harrison441b62c2008-03-03 16:08:34 -0800216 US_DEBUGP("%s\n", __func__);
Matthew Dharm7931e1c2005-12-04 21:56:51 -0800217 if (us->suspend_resume_hook)
218 (us->suspend_resume_hook)(us, US_RESUME);
Alan Sternce2596d2005-10-23 19:41:39 -0700219
Greg Kroah-Hartmand5268752007-09-13 06:01:24 -0700220 mutex_unlock(&us->dev_mutex);
Alan Sternce2596d2005-10-23 19:41:39 -0700221 return 0;
222}
223
Alan Sternf07600c2007-05-30 15:38:16 -0400224static int storage_reset_resume(struct usb_interface *iface)
225{
226 struct us_data *us = usb_get_intfdata(iface);
227
Harvey Harrison441b62c2008-03-03 16:08:34 -0800228 US_DEBUGP("%s\n", __func__);
Alan Sternf07600c2007-05-30 15:38:16 -0400229
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
Alan Sternce2596d2005-10-23 19:41:39 -0700238#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240/*
Alan Stern47104b02006-06-01 13:52:56 -0400241 * 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
Alan Sternf07600c2007-05-30 15:38:16 -0400245static int storage_pre_reset(struct usb_interface *iface)
Alan Stern47104b02006-06-01 13:52:56 -0400246{
247 struct us_data *us = usb_get_intfdata(iface);
248
Harvey Harrison441b62c2008-03-03 16:08:34 -0800249 US_DEBUGP("%s\n", __func__);
Alan Stern47104b02006-06-01 13:52:56 -0400250
251 /* Make sure no command runs during the reset */
252 mutex_lock(&us->dev_mutex);
Alan Sternf07600c2007-05-30 15:38:16 -0400253 return 0;
Alan Stern47104b02006-06-01 13:52:56 -0400254}
255
Alan Sternf07600c2007-05-30 15:38:16 -0400256static int storage_post_reset(struct usb_interface *iface)
Alan Stern47104b02006-06-01 13:52:56 -0400257{
258 struct us_data *us = usb_get_intfdata(iface);
259
Harvey Harrison441b62c2008-03-03 16:08:34 -0800260 US_DEBUGP("%s\n", __func__);
Alan Stern47104b02006-06-01 13:52:56 -0400261
262 /* Report the reset to the SCSI core */
Alan Stern47104b02006-06-01 13:52:56 -0400263 usb_stor_report_bus_reset(us);
Alan Stern47104b02006-06-01 13:52:56 -0400264
265 /* FIXME: Notify the subdrivers that they need to reinitialize
266 * the device */
Alan Stern0458d5b2007-05-04 11:52:20 -0400267
Alan Sternf07600c2007-05-30 15:38:16 -0400268 mutex_unlock(&us->dev_mutex);
269 return 0;
Alan Stern47104b02006-06-01 13:52:56 -0400270}
271
272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 for(;;) {
320 US_DEBUGP("*** thread sleeping.\n");
Alan Stern7119e3c2008-05-01 15:36:13 -0400321 if (wait_for_completion_interruptible(&us->cmnd_ready))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
Alan Stern7119e3c2008-05-01 15:36:13 -0400323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 US_DEBUGP("*** thread awakened.\n");
325
326 /* lock the device pointers */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100327 mutex_lock(&(us->dev_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /* lock access to the state */
330 scsi_lock(host);
331
Alan Stern543f7812008-05-08 11:55:59 -0400332 /* When we are called with no command pending, we're done */
333 if (us->srb == NULL) {
334 scsi_unlock(host);
335 mutex_unlock(&us->dev_mutex);
336 US_DEBUGP("-- exiting\n");
337 break;
338 }
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* has the command timed out *already* ? */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400341 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 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 &&
Alan Stern7e4d6c32008-05-01 15:35:18 -0400360 !(us->fflags & US_FL_SCM_MULT_TARG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 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) &&
Alan Stern7e4d6c32008-05-01 15:35:18 -0400375 (us->fflags & US_FL_FIX_INQUIRY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 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 */
Alan Stern543f7812008-05-08 11:55:59 -0400395 if (us->srb->result != DID_ABORT << 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 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
Matthew Dharm226173e2005-08-25 20:03:50 -0700407 * the timeout might have occurred after the command had
408 * already completed with a different result code. */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400409 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 complete(&(us->notify));
411
Matthew Dharm226173e2005-08-25 20:03:50 -0700412 /* Allow USB transfers to resume */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400413 clear_bit(US_FLIDX_ABORTING, &us->dflags);
414 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
Matthew Dharm226173e2005-08-25 20:03:50 -0700415 }
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* finished working on this command */
418 us->srb = NULL;
419 scsi_unlock(host);
420
421 /* unlock the device pointers */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100422 mutex_unlock(&us->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 } /* for (;;) */
424
Alan Sterned76cac2007-06-07 17:12:25 -0400425 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
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{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800443 US_DEBUGP("-- %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
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
Alan Sternd4f373e2008-11-10 14:07:45 -0500477/* 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
Pete Zaitceva00828e2005-10-22 20:15:09 -0700501/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/* Get the unusual_devs entries and the string descriptors */
Daniel Drake3c332422006-07-26 13:59:23 +0100509static int get_device_info(struct us_data *us, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct usb_device *dev = us->pusb_dev;
512 struct usb_interface_descriptor *idesc =
513 &us->pusb_intf->cur_altsetting->desc;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700514 struct us_unusual_dev *unusual_dev = find_unusual(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
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;
Alan Stern7e4d6c32008-05-01 15:35:18 -0400524 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
Alan Sternd4f373e2008-11-10 14:07:45 -0500525 adjust_quirks(us);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Alan Stern7e4d6c32008-05-01 15:35:18 -0400527 if (us->fflags & US_FL_IGNORE_DEVICE) {
Daniel Drake3c332422006-07-26 13:59:23 +0100528 printk(KERN_INFO USB_STORAGE "device ignored\n");
529 return -ENODEV;
530 }
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /*
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)
Alan Stern7e4d6c32008-05-01 15:35:18 -0400537 us->fflags &= ~US_FL_GO_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
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) {
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100545 static const char *msgs[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 "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;
Alan Stern7e4d6c32008-05-01 15:35:18 -0400558 if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 printk(KERN_NOTICE USB_STORAGE "This device "
560 "(%04x,%04x,%04x S %02x P %02x)"
Phil Dibowitz5650b4d2006-06-24 17:27:54 -0700561 " has %s in unusual_devs.h (kernel"
562 " %s)\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 " Please send a copy of this message to "
Andrew Lunncef03f82008-04-23 22:04:30 +0200564 "<linux-usb@vger.kernel.org> and "
565 "<usb-storage@lists.one-eyed-alien.net>\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 le16_to_cpu(ddesc->idVendor),
567 le16_to_cpu(ddesc->idProduct),
568 le16_to_cpu(ddesc->bcdDevice),
569 idesc->bInterfaceSubClass,
570 idesc->bInterfaceProtocol,
Phil Dibowitz5650b4d2006-06-24 17:27:54 -0700571 msgs[msg],
Sam Ravnborg00f8b0c2007-03-05 00:30:55 -0800572 utsname()->release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Daniel Drake3c332422006-07-26 13:59:23 +0100574
575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
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";
Alan Stern64648a92008-11-20 14:20:03 -0500591 us->transport = usb_stor_CB_transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 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
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100603 case US_PR_USBAT:
604 us->transport_name = "Shuttle USBAT";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 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
Daniel Drakeb3835392006-05-09 01:45:27 +0100665#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
Matthew Dharmdfe0d3b2006-08-13 17:30:14 -0700674#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 default:
683 return -EIO;
684 }
685 US_DEBUGP("Transport: %s\n", us->transport_name);
686
687 /* fix for single-lun devices */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400688 if (us->fflags & US_FL_SINGLE_LUN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 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";
Alan Stern3dae5342008-11-20 14:22:18 -0500704 us->proto_handler = usb_stor_pad12_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 us->max_lun = 0;
706 break;
707
708 case US_SC_QIC:
709 us->protocol_name = "QIC-157";
Alan Stern3dae5342008-11-20 14:22:18 -0500710 us->proto_handler = usb_stor_pad12_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 us->max_lun = 0;
712 break;
713
714 case US_SC_8070:
715 us->protocol_name = "8070i";
Alan Stern3dae5342008-11-20 14:22:18 -0500716 us->proto_handler = usb_stor_pad12_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 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
matthieu castetd2770642008-03-19 19:40:52 +0100737#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 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 /*
Alan Stern1096f782007-01-22 11:58:34 -0500763 * Find the first endpoint of each type we need.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * We are expecting a minimum of 2 endpoints - in and out (bulk).
Alan Stern1096f782007-01-22 11:58:34 -0500765 * An optional interrupt-in is OK (necessary for CBI protocol).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * We will ignore any others.
767 */
768 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
769 ep = &altsetting->endpoint[i].desc;
770
Luiz Fernando N. Capitulino04720742006-10-26 13:03:03 -0300771 if (usb_endpoint_xfer_bulk(ep)) {
Alan Stern1096f782007-01-22 11:58:34 -0500772 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
Alan Stern1096f782007-01-22 11:58:34 -0500781 else if (usb_endpoint_is_int_in(ep)) {
782 if (!ep_int)
783 ep_int = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
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;
Alan Stern3f13e662005-10-23 19:43:36 -0700811 struct task_struct *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* Just before we start our control thread, initialize
820 * the device if it needs initialization */
Alan Sternb876aef2005-10-23 19:38:56 -0700821 if (us->unusual_dev->initFunction) {
822 p = us->unusual_dev->initFunction(us);
823 if (p)
824 return p;
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* Start up our control thread */
Alan Sterned76cac2007-06-07 17:12:25 -0400828 th = kthread_run(usb_stor_control_thread, us, "usb-storage");
Alan Stern3f13e662005-10-23 19:43:36 -0700829 if (IS_ERR(th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 printk(KERN_WARNING USB_STORAGE
831 "Unable to start control thread\n");
Alan Stern3f13e662005-10-23 19:43:36 -0700832 return PTR_ERR(th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
Alan Sterned76cac2007-06-07 17:12:25 -0400834 us->ctl_thread = th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 return 0;
837}
838
839/* Release all our dynamic resources */
840static void usb_stor_release_resources(struct us_data *us)
841{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800842 US_DEBUGP("-- %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* Tell the control thread to exit. The SCSI host must
Alan Stern543f7812008-05-08 11:55:59 -0400845 * already have been removed and the DISCONNECTING flag set
846 * so that we won't accept any more commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
848 US_DEBUGP("-- sending exit command to thread\n");
Alan Stern7119e3c2008-05-01 15:36:13 -0400849 complete(&us->cmnd_ready);
Alan Sterned76cac2007-06-07 17:12:25 -0400850 if (us->ctl_thread)
851 kthread_stop(us->ctl_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
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{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800867 US_DEBUGP("-- %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
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
Alan Stern543f7812008-05-08 11:55:59 -0400881/* First stage of disconnect processing: stop SCSI scanning,
882 * remove the host, and stop accepting new commands
883 */
Matthew Dharm77f46322005-07-28 14:44:29 -0700884static void quiesce_and_remove_host(struct us_data *us)
885{
Alan Sterneecd11e2006-06-19 14:50:15 -0400886 struct Scsi_Host *host = us_to_host(us);
887
Alan Stern543f7812008-05-08 11:55:59 -0400888 /* 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);
891
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);
898
899 /* Removing the host will perform an orderly shutdown: caches
900 * synchronized, disks spun down, etc.
901 */
902 scsi_remove_host(host);
903
904 /* Prevent any new commands from being accepted and cut short
905 * reset delays.
906 */
Alan Sterneecd11e2006-06-19 14:50:15 -0400907 scsi_lock(host);
Alan Stern7e4d6c32008-05-01 15:35:18 -0400908 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
Alan Sterneecd11e2006-06-19 14:50:15 -0400909 scsi_unlock(host);
Matthew Dharm77f46322005-07-28 14:44:29 -0700910 wake_up(&us->delay_wait);
Matthew Dharm77f46322005-07-28 14:44:29 -0700911}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 printk(KERN_DEBUG
930 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
931
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700932 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* 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");
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700937 wait_event_freezable_timeout(us->delay_wait,
Alan Stern543f7812008-05-08 11:55:59 -0400938 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 delay_use * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
942 /* If the device is still connected, perform the scanning */
Alan Stern543f7812008-05-08 11:55:59 -0400943 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
Alan Sternb876aef2005-10-23 19:38:56 -0700944
945 /* For bulk-only devices, determine the max LUN value */
946 if (us->protocol == US_PR_BULK &&
Alan Stern7e4d6c32008-05-01 15:35:18 -0400947 !(us->fflags & US_FL_SINGLE_LUN)) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100948 mutex_lock(&us->dev_mutex);
Alan Sternb876aef2005-10-23 19:38:56 -0700949 us->max_lun = usb_stor_Bulk_max_lun(us);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100950 mutex_unlock(&us->dev_mutex);
Alan Sternb876aef2005-10-23 19:38:56 -0700951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 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
Alan Stern2f67cd52007-08-16 16:16:00 -0400958 complete_and_exit(&us->scanning_done, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int result;
Alan Stern3f13e662005-10-23 19:43:36 -0700969 struct task_struct *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Pete Zaitceva00828e2005-10-22 20:15:09 -0700971 if (usb_usual_check_type(id, USB_US_TYPE_STOR))
972 return -ENXIO;
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 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
Richard Sharpe17f06022007-10-10 10:56:28 -0700987 /*
988 * Allow 16-byte CDBs and thus > 2TB
989 */
990 host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 us = host_to_us(host);
992 memset(us, 0, sizeof(struct us_data));
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100993 mutex_init(&(us->dev_mutex));
Alan Stern7119e3c2008-05-01 15:36:13 -0400994 init_completion(&us->cmnd_ready);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 init_completion(&(us->notify));
996 init_waitqueue_head(&us->delay_wait);
Alan Stern2f67cd52007-08-16 16:16:00 -0400997 init_completion(&us->scanning_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
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 */
Daniel Drake3c332422006-07-26 13:59:23 +01001011 result = get_device_info(us, id);
1012 if (result)
1013 goto BadDevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /* 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 */
Alan Stern3f13e662005-10-23 19:43:36 -07001038 th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
1039 if (IS_ERR(th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 printk(KERN_WARNING USB_STORAGE
1041 "Unable to start the device-scanning thread\n");
Alan Stern543f7812008-05-08 11:55:59 -04001042 complete(&us->scanning_done);
Matthew Dharm77f46322005-07-28 14:44:29 -07001043 quiesce_and_remove_host(us);
Alan Stern3f13e662005-10-23 19:43:36 -07001044 result = PTR_ERR(th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 goto BadDevice;
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Alan Stern3f13e662005-10-23 19:43:36 -07001048 wake_up_process(th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 return 0;
1051
1052 /* We come here if there are any problems */
1053BadDevice:
1054 US_DEBUGP("storage_probe() failed\n");
Matthew Dharm77f46322005-07-28 14:44:29 -07001055 release_everything(us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 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");
Matthew Dharm77f46322005-07-28 14:44:29 -07001065 quiesce_and_remove_host(us);
1066 release_everything(us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069/***********************************************************************
1070 * Initialization and registration
1071 ***********************************************************************/
1072
Alan Sternce2596d2005-10-23 19:41:39 -07001073static struct usb_driver usb_storage_driver = {
Alan Sternce2596d2005-10-23 19:41:39 -07001074 .name = "usb-storage",
1075 .probe = storage_probe,
1076 .disconnect = storage_disconnect,
1077#ifdef CONFIG_PM
1078 .suspend = storage_suspend,
1079 .resume = storage_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04001080 .reset_resume = storage_reset_resume,
Alan Sternce2596d2005-10-23 19:41:39 -07001081#endif
Alan Stern47104b02006-06-01 13:52:56 -04001082 .pre_reset = storage_pre_reset,
1083 .post_reset = storage_post_reset,
Alan Sternce2596d2005-10-23 19:41:39 -07001084 .id_table = storage_usb_ids,
Alan Stern543f7812008-05-08 11:55:59 -04001085 .soft_unbind = 1,
Alan Sternce2596d2005-10-23 19:41:39 -07001086};
1087
Alan Sternd4f373e2008-11-10 14:07:45 -05001088/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164static int __init usb_stor_init(void)
1165{
1166 int retval;
Alan Sternd4f373e2008-11-10 14:07:45 -05001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
Alan Sternd4f373e2008-11-10 14:07:45 -05001169 parse_quirks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 /* register the driver, return usb_register return code if error */
1172 retval = usb_register(&usb_storage_driver);
Pete Zaitceva00828e2005-10-22 20:15:09 -07001173 if (retval == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 printk(KERN_INFO "USB Mass Storage support registered.\n");
Pete Zaitceva00828e2005-10-22 20:15:09 -07001175 usb_usual_set_present(USB_US_TYPE_STOR);
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 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
Pete Zaitceva00828e2005-10-22 20:15:09 -07001191 usb_usual_clear_present(USB_US_TYPE_STOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194module_init(usb_stor_init);
1195module_exit(usb_stor_exit);