blob: 783548485868ef1a32006d97e8be8729de9d87fd [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)
Alan Sterne6e244b2009-02-12 14:47:44 -05008 * (c) 2003-2009 Alan Stern (stern@rowland.harvard.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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 Ravnborg00f8b0c12007-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
Kevin Lloyd32fe5e32008-07-10 14:14:57 -070069#include "sierra_ms.h"
Dan Williams281b0642008-12-14 12:39:22 -050070#include "option_ms.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
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
Alan Sternc838ea42008-12-15 10:40:06 -050081static char quirks[128];
82module_param_string(quirks, quirks, sizeof(quirks), S_IRUGO | S_IWUSR);
Alan Sternd4f373e2008-11-10 14:07:45 -050083MODULE_PARM_DESC(quirks, "supplemental list of device IDs and their quirks");
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Pete Zaitceva00828e2005-10-22 20:15:09 -070086/*
87 * The entries in this table correspond, line for line,
Alan Sterne6e244b2009-02-12 14:47:44 -050088 * with the entries in usb_storage_usb_ids[], defined in usual-tables.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#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, \
Pete Zaitceva00828e2005-10-22 20:15:09 -0700109}
110
Alan Stern25ff1c32008-12-15 12:43:41 -0500111#define COMPLIANT_DEV UNUSUAL_DEV
112
Pete Zaitceva00828e2005-10-22 20:15:09 -0700113#define USUAL_DEV(use_protocol, use_transport, use_type) \
114{ \
115 .useProtocol = use_protocol, \
116 .useTransport = use_transport, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static struct us_unusual_dev us_unusual_dev_list[] = {
120# include "unusual_devs.h"
Alan Sterne6e244b2009-02-12 14:47:44 -0500121 { } /* Terminating entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
Alan Sterne6e244b2009-02-12 14:47:44 -0500124#undef UNUSUAL_DEV
125#undef COMPLIANT_DEV
126#undef USUAL_DEV
127
Alan Sternce2596d2005-10-23 19:41:39 -0700128
129#ifdef CONFIG_PM /* Minimal support for suspend and resume */
130
Alan Sterne6e244b2009-02-12 14:47:44 -0500131int usb_stor_suspend(struct usb_interface *iface, pm_message_t message)
Alan Sternce2596d2005-10-23 19:41:39 -0700132{
133 struct us_data *us = usb_get_intfdata(iface);
134
135 /* Wait until no command is running */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100136 mutex_lock(&us->dev_mutex);
Alan Sternce2596d2005-10-23 19:41:39 -0700137
Harvey Harrison441b62c2008-03-03 16:08:34 -0800138 US_DEBUGP("%s\n", __func__);
Matthew Dharm7931e1c2005-12-04 21:56:51 -0800139 if (us->suspend_resume_hook)
140 (us->suspend_resume_hook)(us, US_SUSPEND);
Alan Sternce2596d2005-10-23 19:41:39 -0700141
Greg Kroah-Hartmand5268752007-09-13 06:01:24 -0700142 /* When runtime PM is working, we'll set a flag to indicate
143 * whether we should autoresume when a SCSI request arrives. */
144
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100145 mutex_unlock(&us->dev_mutex);
Alan Sternce2596d2005-10-23 19:41:39 -0700146 return 0;
147}
Alan Sterne6e244b2009-02-12 14:47:44 -0500148EXPORT_SYMBOL_GPL(usb_stor_suspend);
Alan Sternce2596d2005-10-23 19:41:39 -0700149
Alan Sterne6e244b2009-02-12 14:47:44 -0500150int usb_stor_resume(struct usb_interface *iface)
Alan Sternce2596d2005-10-23 19:41:39 -0700151{
152 struct us_data *us = usb_get_intfdata(iface);
153
Greg Kroah-Hartmand5268752007-09-13 06:01:24 -0700154 mutex_lock(&us->dev_mutex);
Alan Stern8dfe4b12007-07-06 14:24:27 -0400155
Harvey Harrison441b62c2008-03-03 16:08:34 -0800156 US_DEBUGP("%s\n", __func__);
Matthew Dharm7931e1c2005-12-04 21:56:51 -0800157 if (us->suspend_resume_hook)
158 (us->suspend_resume_hook)(us, US_RESUME);
Alan Sternce2596d2005-10-23 19:41:39 -0700159
Greg Kroah-Hartmand5268752007-09-13 06:01:24 -0700160 mutex_unlock(&us->dev_mutex);
Alan Sternce2596d2005-10-23 19:41:39 -0700161 return 0;
162}
Alan Sterne6e244b2009-02-12 14:47:44 -0500163EXPORT_SYMBOL_GPL(usb_stor_resume);
Alan Sternce2596d2005-10-23 19:41:39 -0700164
Alan Sterne6e244b2009-02-12 14:47:44 -0500165int usb_stor_reset_resume(struct usb_interface *iface)
Alan Sternf07600c2007-05-30 15:38:16 -0400166{
167 struct us_data *us = usb_get_intfdata(iface);
168
Harvey Harrison441b62c2008-03-03 16:08:34 -0800169 US_DEBUGP("%s\n", __func__);
Alan Sternf07600c2007-05-30 15:38:16 -0400170
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}
Alan Sterne6e244b2009-02-12 14:47:44 -0500178EXPORT_SYMBOL_GPL(usb_stor_reset_resume);
Alan Sternf07600c2007-05-30 15:38:16 -0400179
Alan Sternce2596d2005-10-23 19:41:39 -0700180#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/*
Alan Stern47104b02006-06-01 13:52:56 -0400183 * 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
Alan Sterne6e244b2009-02-12 14:47:44 -0500187int usb_stor_pre_reset(struct usb_interface *iface)
Alan Stern47104b02006-06-01 13:52:56 -0400188{
189 struct us_data *us = usb_get_intfdata(iface);
190
Harvey Harrison441b62c2008-03-03 16:08:34 -0800191 US_DEBUGP("%s\n", __func__);
Alan Stern47104b02006-06-01 13:52:56 -0400192
193 /* Make sure no command runs during the reset */
194 mutex_lock(&us->dev_mutex);
Alan Sternf07600c2007-05-30 15:38:16 -0400195 return 0;
Alan Stern47104b02006-06-01 13:52:56 -0400196}
Alan Sterne6e244b2009-02-12 14:47:44 -0500197EXPORT_SYMBOL_GPL(usb_stor_pre_reset);
Alan Stern47104b02006-06-01 13:52:56 -0400198
Alan Sterne6e244b2009-02-12 14:47:44 -0500199int usb_stor_post_reset(struct usb_interface *iface)
Alan Stern47104b02006-06-01 13:52:56 -0400200{
201 struct us_data *us = usb_get_intfdata(iface);
202
Harvey Harrison441b62c2008-03-03 16:08:34 -0800203 US_DEBUGP("%s\n", __func__);
Alan Stern47104b02006-06-01 13:52:56 -0400204
205 /* Report the reset to the SCSI core */
Alan Stern47104b02006-06-01 13:52:56 -0400206 usb_stor_report_bus_reset(us);
Alan Stern47104b02006-06-01 13:52:56 -0400207
208 /* FIXME: Notify the subdrivers that they need to reinitialize
209 * the device */
Alan Stern0458d5b2007-05-04 11:52:20 -0400210
Alan Sternf07600c2007-05-30 15:38:16 -0400211 mutex_unlock(&us->dev_mutex);
212 return 0;
Alan Stern47104b02006-06-01 13:52:56 -0400213}
Alan Sterne6e244b2009-02-12 14:47:44 -0500214EXPORT_SYMBOL_GPL(usb_stor_post_reset);
Alan Stern47104b02006-06-01 13:52:56 -0400215
216/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * 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}
Alan Sterne6e244b2009-02-12 14:47:44 -0500257EXPORT_SYMBOL_GPL(fill_inquiry_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 for(;;) {
265 US_DEBUGP("*** thread sleeping.\n");
Alan Stern7119e3c2008-05-01 15:36:13 -0400266 if (wait_for_completion_interruptible(&us->cmnd_ready))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break;
Alan Stern7119e3c2008-05-01 15:36:13 -0400268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 US_DEBUGP("*** thread awakened.\n");
270
271 /* lock the device pointers */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100272 mutex_lock(&(us->dev_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* lock access to the state */
275 scsi_lock(host);
276
Alan Stern543f7812008-05-08 11:55:59 -0400277 /* When we are called with no command pending, we're done */
278 if (us->srb == NULL) {
279 scsi_unlock(host);
280 mutex_unlock(&us->dev_mutex);
281 US_DEBUGP("-- exiting\n");
282 break;
283 }
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* has the command timed out *already* ? */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400286 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 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 &&
Alan Stern7e4d6c32008-05-01 15:35:18 -0400305 !(us->fflags & US_FL_SCM_MULT_TARG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 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) &&
Alan Stern7e4d6c32008-05-01 15:35:18 -0400320 (us->fflags & US_FL_FIX_INQUIRY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 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 */
Alan Stern543f7812008-05-08 11:55:59 -0400340 if (us->srb->result != DID_ABORT << 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 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
Matthew Dharm226173e2005-08-25 20:03:50 -0700352 * the timeout might have occurred after the command had
353 * already completed with a different result code. */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400354 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 complete(&(us->notify));
356
Matthew Dharm226173e2005-08-25 20:03:50 -0700357 /* Allow USB transfers to resume */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400358 clear_bit(US_FLIDX_ABORTING, &us->dflags);
359 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
Matthew Dharm226173e2005-08-25 20:03:50 -0700360 }
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* finished working on this command */
363 us->srb = NULL;
364 scsi_unlock(host);
365
366 /* unlock the device pointers */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100367 mutex_unlock(&us->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 } /* for (;;) */
369
Alan Sterned76cac2007-06-07 17:12:25 -0400370 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
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{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800388 US_DEBUGP("-- %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
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
Alan Sternc838ea42008-12-15 10:40:06 -0500422/* Works only for digits and letters, but small and fast */
423#define TOLOWER(x) ((x) | 0x20)
424
Alan Sternd4f373e2008-11-10 14:07:45 -0500425/* Adjust device flags based on the "quirks=" module parameter */
426static void adjust_quirks(struct us_data *us)
427{
Alan Sternc838ea42008-12-15 10:40:06 -0500428 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 |
Alan Sternd4f373e2008-11-10 14:07:45 -0500434 US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 |
Alan Sternc838ea42008-12-15 10:40:06 -0500435 US_FL_CAPACITY_OK | US_FL_IGNORE_RESIDUE |
436 US_FL_SINGLE_LUN | US_FL_NO_WP_DETECT);
Alan Sternd4f373e2008-11-10 14:07:45 -0500437
Alan Sternc838ea42008-12-15 10:40:06 -0500438 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 == ':')
Alan Sternd4f373e2008-11-10 14:07:45 -0500445 break;
Alan Sternc838ea42008-12-15 10:40:06 -0500446
447 /* Move forward to the next entry */
448 while (*p) {
449 if (*p++ == ',')
450 break;
Alan Sternd4f373e2008-11-10 14:07:45 -0500451 }
452 }
Alan Sternc838ea42008-12-15 10:40:06 -0500453 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);
Alan Sternd4f373e2008-11-10 14:07:45 -0500496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/* Get the unusual_devs entries and the string descriptors */
Alan Sterne6e244b2009-02-12 14:47:44 -0500499static int get_device_info(struct us_data *us, const struct usb_device_id *id,
500 struct us_unusual_dev *unusual_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct usb_device *dev = us->pusb_dev;
503 struct usb_interface_descriptor *idesc =
504 &us->pusb_intf->cur_altsetting->desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
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;
Alan Stern7e4d6c32008-05-01 15:35:18 -0400514 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
Alan Sternd4f373e2008-11-10 14:07:45 -0500515 adjust_quirks(us);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Alan Stern7e4d6c32008-05-01 15:35:18 -0400517 if (us->fflags & US_FL_IGNORE_DEVICE) {
Daniel Drake3c332422006-07-26 13:59:23 +0100518 printk(KERN_INFO USB_STORAGE "device ignored\n");
519 return -ENODEV;
520 }
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /*
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)
Alan Stern7e4d6c32008-05-01 15:35:18 -0400527 us->fflags &= ~US_FL_GO_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
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) {
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100535 static const char *msgs[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 "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;
Alan Stern7e4d6c32008-05-01 15:35:18 -0400548 if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 printk(KERN_NOTICE USB_STORAGE "This device "
550 "(%04x,%04x,%04x S %02x P %02x)"
Phil Dibowitz5650b4d2006-06-24 17:27:54 -0700551 " has %s in unusual_devs.h (kernel"
552 " %s)\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 " Please send a copy of this message to "
Andrew Lunncef03f82008-04-23 22:04:30 +0200554 "<linux-usb@vger.kernel.org> and "
555 "<usb-storage@lists.one-eyed-alien.net>\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 le16_to_cpu(ddesc->idVendor),
557 le16_to_cpu(ddesc->idProduct),
558 le16_to_cpu(ddesc->bcdDevice),
559 idesc->bInterfaceSubClass,
560 idesc->bInterfaceProtocol,
Phil Dibowitz5650b4d2006-06-24 17:27:54 -0700561 msgs[msg],
Sam Ravnborg00f8b0c12007-03-05 00:30:55 -0800562 utsname()->release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Daniel Drake3c332422006-07-26 13:59:23 +0100564
565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/* Get the transport settings */
Alan Sterne6e244b2009-02-12 14:47:44 -0500569static void get_transport(struct us_data *us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
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";
Alan Stern64648a92008-11-20 14:20:03 -0500581 us->transport = usb_stor_CB_transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594/* Get the protocol settings */
Alan Sterne6e244b2009-02-12 14:47:44 -0500595static void get_protocol(struct us_data *us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
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";
Alan Stern3dae5342008-11-20 14:22:18 -0500605 us->proto_handler = usb_stor_pad12_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 us->max_lun = 0;
607 break;
608
609 case US_SC_QIC:
610 us->protocol_name = "QIC-157";
Alan Stern3dae5342008-11-20 14:22:18 -0500611 us->proto_handler = usb_stor_pad12_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 us->max_lun = 0;
613 break;
614
615 case US_SC_8070:
616 us->protocol_name = "8070i";
Alan Stern3dae5342008-11-20 14:22:18 -0500617 us->proto_handler = usb_stor_pad12_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
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 /*
Alan Stern1096f782007-01-22 11:58:34 -0500645 * Find the first endpoint of each type we need.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 * We are expecting a minimum of 2 endpoints - in and out (bulk).
Alan Stern1096f782007-01-22 11:58:34 -0500647 * An optional interrupt-in is OK (necessary for CBI protocol).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * We will ignore any others.
649 */
650 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
651 ep = &altsetting->endpoint[i].desc;
652
Luiz Fernando N. Capitulino04720742006-10-26 13:03:03 -0300653 if (usb_endpoint_xfer_bulk(ep)) {
Alan Stern1096f782007-01-22 11:58:34 -0500654 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Alan Stern1096f782007-01-22 11:58:34 -0500663 else if (usb_endpoint_is_int_in(ep)) {
664 if (!ep_int)
665 ep_int = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
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,
Julia Lawall2e0fe702008-12-29 11:22:14 +0100678 usb_endpoint_num(ep_out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
Julia Lawall2e0fe702008-12-29 11:22:14 +0100680 usb_endpoint_num(ep_in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (ep_int) {
682 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
Julia Lawall2e0fe702008-12-29 11:22:14 +0100683 usb_endpoint_num(ep_int));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 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;
Alan Stern3f13e662005-10-23 19:43:36 -0700693 struct task_struct *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* Just before we start our control thread, initialize
702 * the device if it needs initialization */
Alan Sternb876aef2005-10-23 19:38:56 -0700703 if (us->unusual_dev->initFunction) {
704 p = us->unusual_dev->initFunction(us);
705 if (p)
706 return p;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* Start up our control thread */
Alan Sterned76cac2007-06-07 17:12:25 -0400710 th = kthread_run(usb_stor_control_thread, us, "usb-storage");
Alan Stern3f13e662005-10-23 19:43:36 -0700711 if (IS_ERR(th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 printk(KERN_WARNING USB_STORAGE
713 "Unable to start control thread\n");
Alan Stern3f13e662005-10-23 19:43:36 -0700714 return PTR_ERR(th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Alan Sterned76cac2007-06-07 17:12:25 -0400716 us->ctl_thread = th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 return 0;
719}
720
721/* Release all our dynamic resources */
722static void usb_stor_release_resources(struct us_data *us)
723{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800724 US_DEBUGP("-- %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /* Tell the control thread to exit. The SCSI host must
Alan Stern543f7812008-05-08 11:55:59 -0400727 * already have been removed and the DISCONNECTING flag set
728 * so that we won't accept any more commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
730 US_DEBUGP("-- sending exit command to thread\n");
Alan Stern7119e3c2008-05-01 15:36:13 -0400731 complete(&us->cmnd_ready);
Alan Sterned76cac2007-06-07 17:12:25 -0400732 if (us->ctl_thread)
733 kthread_stop(us->ctl_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
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{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800749 US_DEBUGP("-- %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
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
Alan Stern543f7812008-05-08 11:55:59 -0400763/* First stage of disconnect processing: stop SCSI scanning,
764 * remove the host, and stop accepting new commands
765 */
Matthew Dharm77f46322005-07-28 14:44:29 -0700766static void quiesce_and_remove_host(struct us_data *us)
767{
Alan Sterneecd11e2006-06-19 14:50:15 -0400768 struct Scsi_Host *host = us_to_host(us);
769
Alan Stern543f7812008-05-08 11:55:59 -0400770 /* 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);
773
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);
780
781 /* Removing the host will perform an orderly shutdown: caches
782 * synchronized, disks spun down, etc.
783 */
784 scsi_remove_host(host);
785
786 /* Prevent any new commands from being accepted and cut short
787 * reset delays.
788 */
Alan Sterneecd11e2006-06-19 14:50:15 -0400789 scsi_lock(host);
Alan Stern7e4d6c32008-05-01 15:35:18 -0400790 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
Alan Sterneecd11e2006-06-19 14:50:15 -0400791 scsi_unlock(host);
Matthew Dharm77f46322005-07-28 14:44:29 -0700792 wake_up(&us->delay_wait);
Matthew Dharm77f46322005-07-28 14:44:29 -0700793}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 printk(KERN_DEBUG
812 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
813
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700814 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* 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");
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700819 wait_event_freezable_timeout(us->delay_wait,
Alan Stern543f7812008-05-08 11:55:59 -0400820 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 delay_use * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
824 /* If the device is still connected, perform the scanning */
Alan Stern543f7812008-05-08 11:55:59 -0400825 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
Alan Sternb876aef2005-10-23 19:38:56 -0700826
827 /* For bulk-only devices, determine the max LUN value */
828 if (us->protocol == US_PR_BULK &&
Alan Stern7e4d6c32008-05-01 15:35:18 -0400829 !(us->fflags & US_FL_SINGLE_LUN)) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100830 mutex_lock(&us->dev_mutex);
Alan Sternb876aef2005-10-23 19:38:56 -0700831 us->max_lun = usb_stor_Bulk_max_lun(us);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100832 mutex_unlock(&us->dev_mutex);
Alan Sternb876aef2005-10-23 19:38:56 -0700833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 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
Alan Stern2f67cd52007-08-16 16:16:00 -0400840 complete_and_exit(&us->scanning_done, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843
Alan Sterne6e244b2009-02-12 14:47:44 -0500844/* 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct Scsi_Host *host;
851 struct us_data *us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int result;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 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
Richard Sharpe17f060222007-10-10 10:56:28 -0700867 /*
868 * Allow 16-byte CDBs and thus > 2TB
869 */
870 host->max_cmd_len = 16;
Alan Sterne6e244b2009-02-12 14:47:44 -0500871 *pus = us = host_to_us(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 memset(us, 0, sizeof(struct us_data));
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100873 mutex_init(&(us->dev_mutex));
Alan Stern7119e3c2008-05-01 15:36:13 -0400874 init_completion(&us->cmnd_ready);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 init_completion(&(us->notify));
876 init_waitqueue_head(&us->delay_wait);
Alan Stern2f67cd52007-08-16 16:16:00 -0400877 init_completion(&us->scanning_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /* Associate the us_data structure with the USB device */
880 result = associate_dev(us, intf);
881 if (result)
882 goto BadDevice;
883
Alan Sterne6e244b2009-02-12 14:47:44 -0500884 /* Get the unusual_devs entries and the descriptors */
885 result = get_device_info(us, id, unusual_dev);
Daniel Drake3c332422006-07-26 13:59:23 +0100886 if (result)
887 goto BadDevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Alan Sterne6e244b2009-02-12 14:47:44 -0500889 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 goto BadDevice;
Alan Sterne6e244b2009-02-12 14:47:44 -0500915 }
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 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;
Matthew Wilcox00fa43e2009-09-24 16:19:11 -0600932 snprintf(us->scsi_name, sizeof(us->scsi_name), "usb-storage %s",
933 dev_name(&us->pusb_intf->dev));
Alan Sterne6e244b2009-02-12 14:47:44 -0500934 result = scsi_add_host(us_to_host(us), &us->pusb_intf->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 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 */
Alan Stern3f13e662005-10-23 19:43:36 -0700942 th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
943 if (IS_ERR(th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 printk(KERN_WARNING USB_STORAGE
945 "Unable to start the device-scanning thread\n");
Alan Stern543f7812008-05-08 11:55:59 -0400946 complete(&us->scanning_done);
Matthew Dharm77f46322005-07-28 14:44:29 -0700947 quiesce_and_remove_host(us);
Alan Stern3f13e662005-10-23 19:43:36 -0700948 result = PTR_ERR(th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 goto BadDevice;
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Alan Stern3f13e662005-10-23 19:43:36 -0700952 wake_up_process(th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 return 0;
955
956 /* We come here if there are any problems */
957BadDevice:
958 US_DEBUGP("storage_probe() failed\n");
Matthew Dharm77f46322005-07-28 14:44:29 -0700959 release_everything(us);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return result;
961}
Alan Sterne6e244b2009-02-12 14:47:44 -0500962EXPORT_SYMBOL_GPL(usb_stor_probe2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Alan Sterne6e244b2009-02-12 14:47:44 -0500964/* Handle a USB mass-storage disconnect */
965void usb_stor_disconnect(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 struct us_data *us = usb_get_intfdata(intf);
968
969 US_DEBUGP("storage_disconnect() called\n");
Matthew Dharm77f46322005-07-28 14:44:29 -0700970 quiesce_and_remove_host(us);
971 release_everything(us);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
Alan Sterne6e244b2009-02-12 14:47:44 -0500973EXPORT_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}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010/***********************************************************************
1011 * Initialization and registration
1012 ***********************************************************************/
1013
Alan Sternce2596d2005-10-23 19:41:39 -07001014static struct usb_driver usb_storage_driver = {
Alan Sternce2596d2005-10-23 19:41:39 -07001015 .name = "usb-storage",
1016 .probe = storage_probe,
Alan Sterne6e244b2009-02-12 14:47:44 -05001017 .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,
Alan Stern543f7812008-05-08 11:55:59 -04001024 .soft_unbind = 1,
Alan Sternce2596d2005-10-23 19:41:39 -07001025};
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027static int __init usb_stor_init(void)
1028{
1029 int retval;
Alan Sternd4f373e2008-11-10 14:07:45 -05001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 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);
Pete Zaitceva00828e2005-10-22 20:15:09 -07001035 if (retval == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 printk(KERN_INFO "USB Mass Storage support registered.\n");
Pete Zaitceva00828e2005-10-22 20:15:09 -07001037 usb_usual_set_present(USB_US_TYPE_STOR);
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 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
Pete Zaitceva00828e2005-10-22 20:15:09 -07001053 usb_usual_clear_present(USB_US_TYPE_STOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056module_init(usb_stor_init);
1057module_exit(usb_stor_exit);