blob: 6bfd99dd57aaadb4e23eba00cf670466b849ca2a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Driver for USB Mass Storage compliant devices
2 *
3 * $Id: usb.c,v 1.75 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by:
6 * (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 *
8 * Developed with the assistance of:
9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
10 * (c) 2003 Alan Stern (stern@rowland.harvard.edu)
11 *
12 * Initial work by:
13 * (c) 1999 Michael Gee (michael@linuxspecific.com)
14 *
15 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
16 * (c) 2000 Yggdrasil Computing, Inc.
17 *
18 * This driver is based on the 'USB Mass Storage Class' document. This
19 * describes in detail the protocol used to communicate with such
20 * devices. Clearly, the designers had SCSI and ATAPI commands in
21 * mind when they created this document. The commands are all very
22 * similar to commands in the SCSI-II and ATAPI specifications.
23 *
24 * It is important to note that in a number of cases this class
25 * exhibits class-specific exemptions from the USB specification.
26 * Notably the usage of NAK, STALL and ACK differs from the norm, in
27 * that they are used to communicate wait, failed and OK on commands.
28 *
29 * Also, for certain devices, the interrupt endpoint is used to convey
30 * status of a command.
31 *
32 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
33 * information about this driver.
34 *
35 * This program is free software; you can redistribute it and/or modify it
36 * under the terms of the GNU General Public License as published by the
37 * Free Software Foundation; either version 2, or (at your option) any
38 * later version.
39 *
40 * This program is distributed in the hope that it will be useful, but
41 * WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 * General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License along
46 * with this program; if not, write to the Free Software Foundation, Inc.,
47 * 675 Mass Ave, Cambridge, MA 02139, USA.
48 */
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/sched.h>
51#include <linux/errno.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080052#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/module.h>
54#include <linux/init.h>
55#include <linux/slab.h>
Alan Stern3f13e662005-10-23 19:43:36 -070056#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010057#include <linux/mutex.h>
Sam Ravnborg00f8b0c12007-03-05 00:30:55 -080058#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#include <scsi/scsi.h>
61#include <scsi/scsi_cmnd.h>
62#include <scsi/scsi_device.h>
63
64#include "usb.h"
65#include "scsiglue.h"
66#include "transport.h"
67#include "protocol.h"
68#include "debug.h"
69#include "initializers.h"
70
71#ifdef CONFIG_USB_STORAGE_USBAT
72#include "shuttle_usbat.h"
73#endif
74#ifdef CONFIG_USB_STORAGE_SDDR09
75#include "sddr09.h"
76#endif
77#ifdef CONFIG_USB_STORAGE_SDDR55
78#include "sddr55.h"
79#endif
80#ifdef CONFIG_USB_STORAGE_DPCM
81#include "dpcm.h"
82#endif
83#ifdef CONFIG_USB_STORAGE_FREECOM
84#include "freecom.h"
85#endif
86#ifdef CONFIG_USB_STORAGE_ISD200
87#include "isd200.h"
88#endif
89#ifdef CONFIG_USB_STORAGE_DATAFAB
90#include "datafab.h"
91#endif
92#ifdef CONFIG_USB_STORAGE_JUMPSHOT
93#include "jumpshot.h"
94#endif
Matthew Dharm34008db2005-07-28 14:49:01 -070095#ifdef CONFIG_USB_STORAGE_ONETOUCH
96#include "onetouch.h"
97#endif
Matthew Dharme80b0fa2005-12-04 22:02:44 -080098#ifdef CONFIG_USB_STORAGE_ALAUDA
99#include "alauda.h"
100#endif
Matthew Dharmdfe0d3b2006-08-13 17:30:14 -0700101#ifdef CONFIG_USB_STORAGE_KARMA
102#include "karma.h"
103#endif
matthieu castetd2770642008-03-19 19:40:52 +0100104#ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
105#include "cypress_atacb.h"
106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/* Some informational data */
109MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
110MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
111MODULE_LICENSE("GPL");
112
113static unsigned int delay_use = 5;
114module_param(delay_use, uint, S_IRUGO | S_IWUSR);
115MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
116
117
Pete Zaitceva00828e2005-10-22 20:15:09 -0700118/*
119 * The entries in this table correspond, line for line,
120 * with the entries of us_unusual_dev_list[].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
Pete Zaitceva00828e2005-10-22 20:15:09 -0700122#ifndef CONFIG_USB_LIBUSUAL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
125 vendorName, productName,useProtocol, useTransport, \
126 initFunction, flags) \
Pete Zaitceva00828e2005-10-22 20:15:09 -0700127{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
128 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
129
130#define USUAL_DEV(useProto, useTrans, useType) \
131{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
132 .driver_info = (USB_US_TYPE_STOR<<24) }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134static struct usb_device_id storage_usb_ids [] = {
135
136# include "unusual_devs.h"
137#undef UNUSUAL_DEV
Pete Zaitceva00828e2005-10-22 20:15:09 -0700138#undef USUAL_DEV
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* Terminating entry */
140 { }
141};
142
143MODULE_DEVICE_TABLE (usb, storage_usb_ids);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700144#endif /* CONFIG_USB_LIBUSUAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/* This is the list of devices we recognize, along with their flag data */
147
148/* The vendor name should be kept at eight characters or less, and
149 * the product name should be kept at 16 characters or less. If a device
150 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
151 * normally generated by a device thorugh the INQUIRY response will be
152 * taken from this list, and this is the reason for the above size
153 * restriction. However, if the flag is not present, then you
154 * are free to use as many characters as you like.
155 */
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
158 vendor_name, product_name, use_protocol, use_transport, \
159 init_function, Flags) \
160{ \
161 .vendorName = vendor_name, \
162 .productName = product_name, \
163 .useProtocol = use_protocol, \
164 .useTransport = use_transport, \
165 .initFunction = init_function, \
Pete Zaitceva00828e2005-10-22 20:15:09 -0700166}
167
168#define USUAL_DEV(use_protocol, use_transport, use_type) \
169{ \
170 .useProtocol = use_protocol, \
171 .useTransport = use_transport, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174static struct us_unusual_dev us_unusual_dev_list[] = {
175# include "unusual_devs.h"
176# undef UNUSUAL_DEV
Pete Zaitceva00828e2005-10-22 20:15:09 -0700177# undef USUAL_DEV
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /* Terminating entry */
180 { NULL }
181};
182
Alan Sternce2596d2005-10-23 19:41:39 -0700183
184#ifdef CONFIG_PM /* Minimal support for suspend and resume */
185
186static int storage_suspend(struct usb_interface *iface, pm_message_t message)
187{
188 struct us_data *us = usb_get_intfdata(iface);
189
190 /* Wait until no command is running */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100191 mutex_lock(&us->dev_mutex);
Alan Sternce2596d2005-10-23 19:41:39 -0700192
Harvey Harrison441b62c2008-03-03 16:08:34 -0800193 US_DEBUGP("%s\n", __func__);
Matthew Dharm7931e1c2005-12-04 21:56:51 -0800194 if (us->suspend_resume_hook)
195 (us->suspend_resume_hook)(us, US_SUSPEND);
Alan Sternce2596d2005-10-23 19:41:39 -0700196
Greg Kroah-Hartmand5268752007-09-13 06:01:24 -0700197 /* When runtime PM is working, we'll set a flag to indicate
198 * whether we should autoresume when a SCSI request arrives. */
199
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100200 mutex_unlock(&us->dev_mutex);
Alan Sternce2596d2005-10-23 19:41:39 -0700201 return 0;
202}
203
204static int storage_resume(struct usb_interface *iface)
205{
206 struct us_data *us = usb_get_intfdata(iface);
207
Greg Kroah-Hartmand5268752007-09-13 06:01:24 -0700208 mutex_lock(&us->dev_mutex);
Alan Stern8dfe4b12007-07-06 14:24:27 -0400209
Harvey Harrison441b62c2008-03-03 16:08:34 -0800210 US_DEBUGP("%s\n", __func__);
Matthew Dharm7931e1c2005-12-04 21:56:51 -0800211 if (us->suspend_resume_hook)
212 (us->suspend_resume_hook)(us, US_RESUME);
Alan Sternce2596d2005-10-23 19:41:39 -0700213
Greg Kroah-Hartmand5268752007-09-13 06:01:24 -0700214 mutex_unlock(&us->dev_mutex);
Alan Sternce2596d2005-10-23 19:41:39 -0700215 return 0;
216}
217
Alan Sternf07600c2007-05-30 15:38:16 -0400218static int storage_reset_resume(struct usb_interface *iface)
219{
220 struct us_data *us = usb_get_intfdata(iface);
221
Harvey Harrison441b62c2008-03-03 16:08:34 -0800222 US_DEBUGP("%s\n", __func__);
Alan Sternf07600c2007-05-30 15:38:16 -0400223
224 /* Report the reset to the SCSI core */
225 usb_stor_report_bus_reset(us);
226
227 /* FIXME: Notify the subdrivers that they need to reinitialize
228 * the device */
229 return 0;
230}
231
Alan Sternce2596d2005-10-23 19:41:39 -0700232#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234/*
Alan Stern47104b02006-06-01 13:52:56 -0400235 * The next two routines get called just before and just after
236 * a USB port reset, whether from this driver or a different one.
237 */
238
Alan Sternf07600c2007-05-30 15:38:16 -0400239static int storage_pre_reset(struct usb_interface *iface)
Alan Stern47104b02006-06-01 13:52:56 -0400240{
241 struct us_data *us = usb_get_intfdata(iface);
242
Harvey Harrison441b62c2008-03-03 16:08:34 -0800243 US_DEBUGP("%s\n", __func__);
Alan Stern47104b02006-06-01 13:52:56 -0400244
245 /* Make sure no command runs during the reset */
246 mutex_lock(&us->dev_mutex);
Alan Sternf07600c2007-05-30 15:38:16 -0400247 return 0;
Alan Stern47104b02006-06-01 13:52:56 -0400248}
249
Alan Sternf07600c2007-05-30 15:38:16 -0400250static int storage_post_reset(struct usb_interface *iface)
Alan Stern47104b02006-06-01 13:52:56 -0400251{
252 struct us_data *us = usb_get_intfdata(iface);
253
Harvey Harrison441b62c2008-03-03 16:08:34 -0800254 US_DEBUGP("%s\n", __func__);
Alan Stern47104b02006-06-01 13:52:56 -0400255
256 /* Report the reset to the SCSI core */
Alan Stern47104b02006-06-01 13:52:56 -0400257 usb_stor_report_bus_reset(us);
Alan Stern47104b02006-06-01 13:52:56 -0400258
259 /* FIXME: Notify the subdrivers that they need to reinitialize
260 * the device */
Alan Stern0458d5b2007-05-04 11:52:20 -0400261
Alan Sternf07600c2007-05-30 15:38:16 -0400262 mutex_unlock(&us->dev_mutex);
263 return 0;
Alan Stern47104b02006-06-01 13:52:56 -0400264}
265
266/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * fill_inquiry_response takes an unsigned char array (which must
268 * be at least 36 characters) and populates the vendor name,
269 * product name, and revision fields. Then the array is copied
270 * into the SCSI command's response buffer (oddly enough
271 * called request_buffer). data_len contains the length of the
272 * data array, which again must be at least 36.
273 */
274
275void fill_inquiry_response(struct us_data *us, unsigned char *data,
276 unsigned int data_len)
277{
278 if (data_len<36) // You lose.
279 return;
280
281 if(data[0]&0x20) { /* USB device currently not connected. Return
282 peripheral qualifier 001b ("...however, the
283 physical device is not currently connected
284 to this logical unit") and leave vendor and
285 product identification empty. ("If the target
286 does store some of the INQUIRY data on the
287 device, it may return zeros or ASCII spaces
288 (20h) in those fields until the data is
289 available from the device."). */
290 memset(data+8,0,28);
291 } else {
292 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
293 memcpy(data+8, us->unusual_dev->vendorName,
294 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
295 strlen(us->unusual_dev->vendorName));
296 memcpy(data+16, us->unusual_dev->productName,
297 strlen(us->unusual_dev->productName) > 16 ? 16 :
298 strlen(us->unusual_dev->productName));
299 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
300 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
301 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
302 data[35] = 0x30 + ((bcdDevice) & 0x0F);
303 }
304
305 usb_stor_set_xfer_buf(data, data_len, us->srb);
306}
307
308static int usb_stor_control_thread(void * __us)
309{
310 struct us_data *us = (struct us_data *)__us;
311 struct Scsi_Host *host = us_to_host(us);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 for(;;) {
314 US_DEBUGP("*** thread sleeping.\n");
Alan Stern7119e3c2008-05-01 15:36:13 -0400315 if (wait_for_completion_interruptible(&us->cmnd_ready))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
Alan Stern7119e3c2008-05-01 15:36:13 -0400317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 US_DEBUGP("*** thread awakened.\n");
319
320 /* lock the device pointers */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100321 mutex_lock(&(us->dev_mutex));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* lock access to the state */
324 scsi_lock(host);
325
Alan Stern543f7812008-05-08 11:55:59 -0400326 /* When we are called with no command pending, we're done */
327 if (us->srb == NULL) {
328 scsi_unlock(host);
329 mutex_unlock(&us->dev_mutex);
330 US_DEBUGP("-- exiting\n");
331 break;
332 }
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /* has the command timed out *already* ? */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400335 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 us->srb->result = DID_ABORT << 16;
337 goto SkipForAbort;
338 }
339
340 scsi_unlock(host);
341
342 /* reject the command if the direction indicator
343 * is UNKNOWN
344 */
345 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
346 US_DEBUGP("UNKNOWN data direction\n");
347 us->srb->result = DID_ERROR << 16;
348 }
349
350 /* reject if target != 0 or if LUN is higher than
351 * the maximum known LUN
352 */
353 else if (us->srb->device->id &&
Alan Stern7e4d6c32008-05-01 15:35:18 -0400354 !(us->fflags & US_FL_SCM_MULT_TARG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 US_DEBUGP("Bad target number (%d:%d)\n",
356 us->srb->device->id, us->srb->device->lun);
357 us->srb->result = DID_BAD_TARGET << 16;
358 }
359
360 else if (us->srb->device->lun > us->max_lun) {
361 US_DEBUGP("Bad LUN (%d:%d)\n",
362 us->srb->device->id, us->srb->device->lun);
363 us->srb->result = DID_BAD_TARGET << 16;
364 }
365
366 /* Handle those devices which need us to fake
367 * their inquiry data */
368 else if ((us->srb->cmnd[0] == INQUIRY) &&
Alan Stern7e4d6c32008-05-01 15:35:18 -0400369 (us->fflags & US_FL_FIX_INQUIRY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned char data_ptr[36] = {
371 0x00, 0x80, 0x02, 0x02,
372 0x1F, 0x00, 0x00, 0x00};
373
374 US_DEBUGP("Faking INQUIRY command\n");
375 fill_inquiry_response(us, data_ptr, 36);
376 us->srb->result = SAM_STAT_GOOD;
377 }
378
379 /* we've got a command, let's do it! */
380 else {
381 US_DEBUG(usb_stor_show_command(us->srb));
382 us->proto_handler(us->srb, us);
383 }
384
385 /* lock access to the state */
386 scsi_lock(host);
387
388 /* indicate that the command is done */
Alan Stern543f7812008-05-08 11:55:59 -0400389 if (us->srb->result != DID_ABORT << 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 US_DEBUGP("scsi cmd done, result=0x%x\n",
391 us->srb->result);
392 us->srb->scsi_done(us->srb);
393 } else {
394SkipForAbort:
395 US_DEBUGP("scsi command aborted\n");
396 }
397
398 /* If an abort request was received we need to signal that
399 * the abort has finished. The proper test for this is
400 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
Matthew Dharm226173e2005-08-25 20:03:50 -0700401 * the timeout might have occurred after the command had
402 * already completed with a different result code. */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400403 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 complete(&(us->notify));
405
Matthew Dharm226173e2005-08-25 20:03:50 -0700406 /* Allow USB transfers to resume */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400407 clear_bit(US_FLIDX_ABORTING, &us->dflags);
408 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
Matthew Dharm226173e2005-08-25 20:03:50 -0700409 }
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* finished working on this command */
412 us->srb = NULL;
413 scsi_unlock(host);
414
415 /* unlock the device pointers */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100416 mutex_unlock(&us->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 } /* for (;;) */
418
Alan Sterned76cac2007-06-07 17:12:25 -0400419 /* Wait until we are told to stop */
420 for (;;) {
421 set_current_state(TASK_INTERRUPTIBLE);
422 if (kthread_should_stop())
423 break;
424 schedule();
425 }
426 __set_current_state(TASK_RUNNING);
427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/***********************************************************************
431 * Device probing and disconnecting
432 ***********************************************************************/
433
434/* Associate our private data with the USB device */
435static int associate_dev(struct us_data *us, struct usb_interface *intf)
436{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800437 US_DEBUGP("-- %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* Fill in the device-related fields */
440 us->pusb_dev = interface_to_usbdev(intf);
441 us->pusb_intf = intf;
442 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
443 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
444 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
445 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
446 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
447 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
448 intf->cur_altsetting->desc.bInterfaceSubClass,
449 intf->cur_altsetting->desc.bInterfaceProtocol);
450
451 /* Store our private data in the interface */
452 usb_set_intfdata(intf, us);
453
454 /* Allocate the device-related DMA-mapped buffers */
455 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
456 GFP_KERNEL, &us->cr_dma);
457 if (!us->cr) {
458 US_DEBUGP("usb_ctrlrequest allocation failed\n");
459 return -ENOMEM;
460 }
461
462 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
463 GFP_KERNEL, &us->iobuf_dma);
464 if (!us->iobuf) {
465 US_DEBUGP("I/O buffer allocation failed\n");
466 return -ENOMEM;
467 }
Alan Sternbbafa462005-10-23 19:40:22 -0700468
469 us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
470 if (!us->sensebuf) {
471 US_DEBUGP("Sense buffer allocation failed\n");
472 return -ENOMEM;
473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
475}
476
Pete Zaitceva00828e2005-10-22 20:15:09 -0700477/* Find an unusual_dev descriptor (always succeeds in the current code) */
478static struct us_unusual_dev *find_unusual(const struct usb_device_id *id)
479{
480 const int id_index = id - storage_usb_ids;
481 return &us_unusual_dev_list[id_index];
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/* Get the unusual_devs entries and the string descriptors */
Daniel Drake3c332422006-07-26 13:59:23 +0100485static int get_device_info(struct us_data *us, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct usb_device *dev = us->pusb_dev;
488 struct usb_interface_descriptor *idesc =
489 &us->pusb_intf->cur_altsetting->desc;
Pete Zaitceva00828e2005-10-22 20:15:09 -0700490 struct us_unusual_dev *unusual_dev = find_unusual(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* Store the entries */
493 us->unusual_dev = unusual_dev;
494 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
495 idesc->bInterfaceSubClass :
496 unusual_dev->useProtocol;
497 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
498 idesc->bInterfaceProtocol :
499 unusual_dev->useTransport;
Alan Stern7e4d6c32008-05-01 15:35:18 -0400500 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Alan Stern7e4d6c32008-05-01 15:35:18 -0400502 if (us->fflags & US_FL_IGNORE_DEVICE) {
Daniel Drake3c332422006-07-26 13:59:23 +0100503 printk(KERN_INFO USB_STORAGE "device ignored\n");
504 return -ENODEV;
505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /*
508 * This flag is only needed when we're in high-speed, so let's
509 * disable it if we're in full-speed
510 */
511 if (dev->speed != USB_SPEED_HIGH)
Alan Stern7e4d6c32008-05-01 15:35:18 -0400512 us->fflags &= ~US_FL_GO_SLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /* Log a message if a non-generic unusual_dev entry contains an
515 * unnecessary subclass or protocol override. This may stimulate
516 * reports from users that will help us remove unneeded entries
517 * from the unusual_devs.h table.
518 */
519 if (id->idVendor || id->idProduct) {
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100520 static const char *msgs[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 "an unneeded SubClass entry",
522 "an unneeded Protocol entry",
523 "unneeded SubClass and Protocol entries"};
524 struct usb_device_descriptor *ddesc = &dev->descriptor;
525 int msg = -1;
526
527 if (unusual_dev->useProtocol != US_SC_DEVICE &&
528 us->subclass == idesc->bInterfaceSubClass)
529 msg += 1;
530 if (unusual_dev->useTransport != US_PR_DEVICE &&
531 us->protocol == idesc->bInterfaceProtocol)
532 msg += 2;
Alan Stern7e4d6c32008-05-01 15:35:18 -0400533 if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 printk(KERN_NOTICE USB_STORAGE "This device "
535 "(%04x,%04x,%04x S %02x P %02x)"
Phil Dibowitz5650b4d2006-06-24 17:27:54 -0700536 " has %s in unusual_devs.h (kernel"
537 " %s)\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 " Please send a copy of this message to "
Andrew Lunncef03f82008-04-23 22:04:30 +0200539 "<linux-usb@vger.kernel.org> and "
540 "<usb-storage@lists.one-eyed-alien.net>\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 le16_to_cpu(ddesc->idVendor),
542 le16_to_cpu(ddesc->idProduct),
543 le16_to_cpu(ddesc->bcdDevice),
544 idesc->bInterfaceSubClass,
545 idesc->bInterfaceProtocol,
Phil Dibowitz5650b4d2006-06-24 17:27:54 -0700546 msgs[msg],
Sam Ravnborg00f8b0c12007-03-05 00:30:55 -0800547 utsname()->release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Daniel Drake3c332422006-07-26 13:59:23 +0100549
550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/* Get the transport settings */
554static int get_transport(struct us_data *us)
555{
556 switch (us->protocol) {
557 case US_PR_CB:
558 us->transport_name = "Control/Bulk";
559 us->transport = usb_stor_CB_transport;
560 us->transport_reset = usb_stor_CB_reset;
561 us->max_lun = 7;
562 break;
563
564 case US_PR_CBI:
565 us->transport_name = "Control/Bulk/Interrupt";
566 us->transport = usb_stor_CBI_transport;
567 us->transport_reset = usb_stor_CB_reset;
568 us->max_lun = 7;
569 break;
570
571 case US_PR_BULK:
572 us->transport_name = "Bulk";
573 us->transport = usb_stor_Bulk_transport;
574 us->transport_reset = usb_stor_Bulk_reset;
575 break;
576
577#ifdef CONFIG_USB_STORAGE_USBAT
Daniel Drakeb7b1e652005-09-30 12:49:36 +0100578 case US_PR_USBAT:
579 us->transport_name = "Shuttle USBAT";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 us->transport = usbat_transport;
581 us->transport_reset = usb_stor_CB_reset;
582 us->max_lun = 1;
583 break;
584#endif
585
586#ifdef CONFIG_USB_STORAGE_SDDR09
587 case US_PR_EUSB_SDDR09:
588 us->transport_name = "EUSB/SDDR09";
589 us->transport = sddr09_transport;
590 us->transport_reset = usb_stor_CB_reset;
591 us->max_lun = 0;
592 break;
593#endif
594
595#ifdef CONFIG_USB_STORAGE_SDDR55
596 case US_PR_SDDR55:
597 us->transport_name = "SDDR55";
598 us->transport = sddr55_transport;
599 us->transport_reset = sddr55_reset;
600 us->max_lun = 0;
601 break;
602#endif
603
604#ifdef CONFIG_USB_STORAGE_DPCM
605 case US_PR_DPCM_USB:
606 us->transport_name = "Control/Bulk-EUSB/SDDR09";
607 us->transport = dpcm_transport;
608 us->transport_reset = usb_stor_CB_reset;
609 us->max_lun = 1;
610 break;
611#endif
612
613#ifdef CONFIG_USB_STORAGE_FREECOM
614 case US_PR_FREECOM:
615 us->transport_name = "Freecom";
616 us->transport = freecom_transport;
617 us->transport_reset = usb_stor_freecom_reset;
618 us->max_lun = 0;
619 break;
620#endif
621
622#ifdef CONFIG_USB_STORAGE_DATAFAB
623 case US_PR_DATAFAB:
624 us->transport_name = "Datafab Bulk-Only";
625 us->transport = datafab_transport;
626 us->transport_reset = usb_stor_Bulk_reset;
627 us->max_lun = 1;
628 break;
629#endif
630
631#ifdef CONFIG_USB_STORAGE_JUMPSHOT
632 case US_PR_JUMPSHOT:
633 us->transport_name = "Lexar Jumpshot Control/Bulk";
634 us->transport = jumpshot_transport;
635 us->transport_reset = usb_stor_Bulk_reset;
636 us->max_lun = 1;
637 break;
638#endif
639
Daniel Drakeb3835392006-05-09 01:45:27 +0100640#ifdef CONFIG_USB_STORAGE_ALAUDA
641 case US_PR_ALAUDA:
642 us->transport_name = "Alauda Control/Bulk";
643 us->transport = alauda_transport;
644 us->transport_reset = usb_stor_Bulk_reset;
645 us->max_lun = 1;
646 break;
647#endif
648
Matthew Dharmdfe0d3b2006-08-13 17:30:14 -0700649#ifdef CONFIG_USB_STORAGE_KARMA
650 case US_PR_KARMA:
651 us->transport_name = "Rio Karma/Bulk";
652 us->transport = rio_karma_transport;
653 us->transport_reset = usb_stor_Bulk_reset;
654 break;
655#endif
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 default:
658 return -EIO;
659 }
660 US_DEBUGP("Transport: %s\n", us->transport_name);
661
662 /* fix for single-lun devices */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400663 if (us->fflags & US_FL_SINGLE_LUN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 us->max_lun = 0;
665 return 0;
666}
667
668/* Get the protocol settings */
669static int get_protocol(struct us_data *us)
670{
671 switch (us->subclass) {
672 case US_SC_RBC:
673 us->protocol_name = "Reduced Block Commands (RBC)";
674 us->proto_handler = usb_stor_transparent_scsi_command;
675 break;
676
677 case US_SC_8020:
678 us->protocol_name = "8020i";
679 us->proto_handler = usb_stor_ATAPI_command;
680 us->max_lun = 0;
681 break;
682
683 case US_SC_QIC:
684 us->protocol_name = "QIC-157";
685 us->proto_handler = usb_stor_qic157_command;
686 us->max_lun = 0;
687 break;
688
689 case US_SC_8070:
690 us->protocol_name = "8070i";
691 us->proto_handler = usb_stor_ATAPI_command;
692 us->max_lun = 0;
693 break;
694
695 case US_SC_SCSI:
696 us->protocol_name = "Transparent SCSI";
697 us->proto_handler = usb_stor_transparent_scsi_command;
698 break;
699
700 case US_SC_UFI:
701 us->protocol_name = "Uniform Floppy Interface (UFI)";
702 us->proto_handler = usb_stor_ufi_command;
703 break;
704
705#ifdef CONFIG_USB_STORAGE_ISD200
706 case US_SC_ISD200:
707 us->protocol_name = "ISD200 ATA/ATAPI";
708 us->proto_handler = isd200_ata_command;
709 break;
710#endif
711
matthieu castetd2770642008-03-19 19:40:52 +0100712#ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
713 case US_SC_CYP_ATACB:
714 us->protocol_name = "Transparent SCSI with Cypress ATACB";
715 us->proto_handler = cypress_atacb_passthrough;
716 break;
717#endif
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 default:
720 return -EIO;
721 }
722 US_DEBUGP("Protocol: %s\n", us->protocol_name);
723 return 0;
724}
725
726/* Get the pipe settings */
727static int get_pipes(struct us_data *us)
728{
729 struct usb_host_interface *altsetting =
730 us->pusb_intf->cur_altsetting;
731 int i;
732 struct usb_endpoint_descriptor *ep;
733 struct usb_endpoint_descriptor *ep_in = NULL;
734 struct usb_endpoint_descriptor *ep_out = NULL;
735 struct usb_endpoint_descriptor *ep_int = NULL;
736
737 /*
Alan Stern1096f782007-01-22 11:58:34 -0500738 * Find the first endpoint of each type we need.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * We are expecting a minimum of 2 endpoints - in and out (bulk).
Alan Stern1096f782007-01-22 11:58:34 -0500740 * An optional interrupt-in is OK (necessary for CBI protocol).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * We will ignore any others.
742 */
743 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
744 ep = &altsetting->endpoint[i].desc;
745
Luiz Fernando N. Capitulino04720742006-10-26 13:03:03 -0300746 if (usb_endpoint_xfer_bulk(ep)) {
Alan Stern1096f782007-01-22 11:58:34 -0500747 if (usb_endpoint_dir_in(ep)) {
748 if (!ep_in)
749 ep_in = ep;
750 } else {
751 if (!ep_out)
752 ep_out = ep;
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
Alan Stern1096f782007-01-22 11:58:34 -0500756 else if (usb_endpoint_is_int_in(ep)) {
757 if (!ep_int)
758 ep_int = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 }
761
762 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
763 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
764 return -EIO;
765 }
766
767 /* Calculate and store the pipe values */
768 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
769 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
770 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
771 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
772 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
773 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
774 if (ep_int) {
775 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
776 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
777 us->ep_bInterval = ep_int->bInterval;
778 }
779 return 0;
780}
781
782/* Initialize all the dynamic resources we need */
783static int usb_stor_acquire_resources(struct us_data *us)
784{
785 int p;
Alan Stern3f13e662005-10-23 19:43:36 -0700786 struct task_struct *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
789 if (!us->current_urb) {
790 US_DEBUGP("URB allocation failed\n");
791 return -ENOMEM;
792 }
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /* Just before we start our control thread, initialize
795 * the device if it needs initialization */
Alan Sternb876aef2005-10-23 19:38:56 -0700796 if (us->unusual_dev->initFunction) {
797 p = us->unusual_dev->initFunction(us);
798 if (p)
799 return p;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* Start up our control thread */
Alan Sterned76cac2007-06-07 17:12:25 -0400803 th = kthread_run(usb_stor_control_thread, us, "usb-storage");
Alan Stern3f13e662005-10-23 19:43:36 -0700804 if (IS_ERR(th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 printk(KERN_WARNING USB_STORAGE
806 "Unable to start control thread\n");
Alan Stern3f13e662005-10-23 19:43:36 -0700807 return PTR_ERR(th);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Alan Sterned76cac2007-06-07 17:12:25 -0400809 us->ctl_thread = th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 return 0;
812}
813
814/* Release all our dynamic resources */
815static void usb_stor_release_resources(struct us_data *us)
816{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800817 US_DEBUGP("-- %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /* Tell the control thread to exit. The SCSI host must
Alan Stern543f7812008-05-08 11:55:59 -0400820 * already have been removed and the DISCONNECTING flag set
821 * so that we won't accept any more commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823 US_DEBUGP("-- sending exit command to thread\n");
Alan Stern7119e3c2008-05-01 15:36:13 -0400824 complete(&us->cmnd_ready);
Alan Sterned76cac2007-06-07 17:12:25 -0400825 if (us->ctl_thread)
826 kthread_stop(us->ctl_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 /* Call the destructor routine, if it exists */
829 if (us->extra_destructor) {
830 US_DEBUGP("-- calling extra_destructor()\n");
831 us->extra_destructor(us->extra);
832 }
833
834 /* Free the extra data and the URB */
835 kfree(us->extra);
836 usb_free_urb(us->current_urb);
837}
838
839/* Dissociate from the USB device */
840static void dissociate_dev(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
Alan Sternbbafa462005-10-23 19:40:22 -0700844 kfree(us->sensebuf);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* Free the device-related DMA-mapped buffers */
847 if (us->cr)
848 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
849 us->cr_dma);
850 if (us->iobuf)
851 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
852 us->iobuf_dma);
853
854 /* Remove our private data from the interface */
855 usb_set_intfdata(us->pusb_intf, NULL);
856}
857
Alan Stern543f7812008-05-08 11:55:59 -0400858/* First stage of disconnect processing: stop SCSI scanning,
859 * remove the host, and stop accepting new commands
860 */
Matthew Dharm77f46322005-07-28 14:44:29 -0700861static void quiesce_and_remove_host(struct us_data *us)
862{
Alan Sterneecd11e2006-06-19 14:50:15 -0400863 struct Scsi_Host *host = us_to_host(us);
864
Alan Stern543f7812008-05-08 11:55:59 -0400865 /* If the device is really gone, cut short reset delays */
866 if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
867 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
868
869 /* Prevent SCSI-scanning (if it hasn't started yet)
870 * and wait for the SCSI-scanning thread to stop.
871 */
872 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
873 wake_up(&us->delay_wait);
874 wait_for_completion(&us->scanning_done);
875
876 /* Removing the host will perform an orderly shutdown: caches
877 * synchronized, disks spun down, etc.
878 */
879 scsi_remove_host(host);
880
881 /* Prevent any new commands from being accepted and cut short
882 * reset delays.
883 */
Alan Sterneecd11e2006-06-19 14:50:15 -0400884 scsi_lock(host);
Alan Stern7e4d6c32008-05-01 15:35:18 -0400885 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
Alan Sterneecd11e2006-06-19 14:50:15 -0400886 scsi_unlock(host);
Matthew Dharm77f46322005-07-28 14:44:29 -0700887 wake_up(&us->delay_wait);
Matthew Dharm77f46322005-07-28 14:44:29 -0700888}
889
890/* Second stage of disconnect processing: deallocate all resources */
891static void release_everything(struct us_data *us)
892{
893 usb_stor_release_resources(us);
894 dissociate_dev(us);
895
896 /* Drop our reference to the host; the SCSI core will free it
897 * (and "us" along with it) when the refcount becomes 0. */
898 scsi_host_put(us_to_host(us));
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/* Thread to carry out delayed SCSI-device scanning */
902static int usb_stor_scan_thread(void * __us)
903{
904 struct us_data *us = (struct us_data *)__us;
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 printk(KERN_DEBUG
907 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
908
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700909 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* Wait for the timeout to expire or for a disconnect */
911 if (delay_use > 0) {
912 printk(KERN_DEBUG "usb-storage: waiting for device "
913 "to settle before scanning\n");
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700914 wait_event_freezable_timeout(us->delay_wait,
Alan Stern543f7812008-05-08 11:55:59 -0400915 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 delay_use * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
919 /* If the device is still connected, perform the scanning */
Alan Stern543f7812008-05-08 11:55:59 -0400920 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
Alan Sternb876aef2005-10-23 19:38:56 -0700921
922 /* For bulk-only devices, determine the max LUN value */
923 if (us->protocol == US_PR_BULK &&
Alan Stern7e4d6c32008-05-01 15:35:18 -0400924 !(us->fflags & US_FL_SINGLE_LUN)) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100925 mutex_lock(&us->dev_mutex);
Alan Sternb876aef2005-10-23 19:38:56 -0700926 us->max_lun = usb_stor_Bulk_max_lun(us);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100927 mutex_unlock(&us->dev_mutex);
Alan Sternb876aef2005-10-23 19:38:56 -0700928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 scsi_scan_host(us_to_host(us));
930 printk(KERN_DEBUG "usb-storage: device scan complete\n");
931
932 /* Should we unbind if no devices were detected? */
933 }
934
Alan Stern2f67cd52007-08-16 16:16:00 -0400935 complete_and_exit(&us->scanning_done, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938
939/* Probe to see if we can drive a newly-connected USB device */
940static int storage_probe(struct usb_interface *intf,
941 const struct usb_device_id *id)
942{
943 struct Scsi_Host *host;
944 struct us_data *us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int result;
Alan Stern3f13e662005-10-23 19:43:36 -0700946 struct task_struct *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Pete Zaitceva00828e2005-10-22 20:15:09 -0700948 if (usb_usual_check_type(id, USB_US_TYPE_STOR))
949 return -ENXIO;
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 US_DEBUGP("USB Mass Storage device detected\n");
952
953 /*
954 * Ask the SCSI layer to allocate a host structure, with extra
955 * space at the end for our private us_data structure.
956 */
957 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
958 if (!host) {
959 printk(KERN_WARNING USB_STORAGE
960 "Unable to allocate the scsi host\n");
961 return -ENOMEM;
962 }
963
Richard Sharpe17f060222007-10-10 10:56:28 -0700964 /*
965 * Allow 16-byte CDBs and thus > 2TB
966 */
967 host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 us = host_to_us(host);
969 memset(us, 0, sizeof(struct us_data));
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100970 mutex_init(&(us->dev_mutex));
Alan Stern7119e3c2008-05-01 15:36:13 -0400971 init_completion(&us->cmnd_ready);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 init_completion(&(us->notify));
973 init_waitqueue_head(&us->delay_wait);
Alan Stern2f67cd52007-08-16 16:16:00 -0400974 init_completion(&us->scanning_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 /* Associate the us_data structure with the USB device */
977 result = associate_dev(us, intf);
978 if (result)
979 goto BadDevice;
980
981 /*
982 * Get the unusual_devs entries and the descriptors
983 *
984 * id_index is calculated in the declaration to be the index number
985 * of the match from the usb_device_id table, so we can find the
986 * corresponding entry in the private table.
987 */
Daniel Drake3c332422006-07-26 13:59:23 +0100988 result = get_device_info(us, id);
989 if (result)
990 goto BadDevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* Get the transport, protocol, and pipe settings */
993 result = get_transport(us);
994 if (result)
995 goto BadDevice;
996 result = get_protocol(us);
997 if (result)
998 goto BadDevice;
999 result = get_pipes(us);
1000 if (result)
1001 goto BadDevice;
1002
1003 /* Acquire all the other resources and add the host */
1004 result = usb_stor_acquire_resources(us);
1005 if (result)
1006 goto BadDevice;
1007 result = scsi_add_host(host, &intf->dev);
1008 if (result) {
1009 printk(KERN_WARNING USB_STORAGE
1010 "Unable to add the scsi host\n");
1011 goto BadDevice;
1012 }
1013
1014 /* Start up the thread for delayed SCSI-device scanning */
Alan Stern3f13e662005-10-23 19:43:36 -07001015 th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
1016 if (IS_ERR(th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 printk(KERN_WARNING USB_STORAGE
1018 "Unable to start the device-scanning thread\n");
Alan Stern543f7812008-05-08 11:55:59 -04001019 complete(&us->scanning_done);
Matthew Dharm77f46322005-07-28 14:44:29 -07001020 quiesce_and_remove_host(us);
Alan Stern3f13e662005-10-23 19:43:36 -07001021 result = PTR_ERR(th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto BadDevice;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Alan Stern3f13e662005-10-23 19:43:36 -07001025 wake_up_process(th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 return 0;
1028
1029 /* We come here if there are any problems */
1030BadDevice:
1031 US_DEBUGP("storage_probe() failed\n");
Matthew Dharm77f46322005-07-28 14:44:29 -07001032 release_everything(us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return result;
1034}
1035
1036/* Handle a disconnect event from the USB core */
1037static void storage_disconnect(struct usb_interface *intf)
1038{
1039 struct us_data *us = usb_get_intfdata(intf);
1040
1041 US_DEBUGP("storage_disconnect() called\n");
Matthew Dharm77f46322005-07-28 14:44:29 -07001042 quiesce_and_remove_host(us);
1043 release_everything(us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046/***********************************************************************
1047 * Initialization and registration
1048 ***********************************************************************/
1049
Alan Sternce2596d2005-10-23 19:41:39 -07001050static struct usb_driver usb_storage_driver = {
Alan Sternce2596d2005-10-23 19:41:39 -07001051 .name = "usb-storage",
1052 .probe = storage_probe,
1053 .disconnect = storage_disconnect,
1054#ifdef CONFIG_PM
1055 .suspend = storage_suspend,
1056 .resume = storage_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04001057 .reset_resume = storage_reset_resume,
Alan Sternce2596d2005-10-23 19:41:39 -07001058#endif
Alan Stern47104b02006-06-01 13:52:56 -04001059 .pre_reset = storage_pre_reset,
1060 .post_reset = storage_post_reset,
Alan Sternce2596d2005-10-23 19:41:39 -07001061 .id_table = storage_usb_ids,
Alan Stern543f7812008-05-08 11:55:59 -04001062 .soft_unbind = 1,
Alan Sternce2596d2005-10-23 19:41:39 -07001063};
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065static int __init usb_stor_init(void)
1066{
1067 int retval;
1068 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1069
1070 /* register the driver, return usb_register return code if error */
1071 retval = usb_register(&usb_storage_driver);
Pete Zaitceva00828e2005-10-22 20:15:09 -07001072 if (retval == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 printk(KERN_INFO "USB Mass Storage support registered.\n");
Pete Zaitceva00828e2005-10-22 20:15:09 -07001074 usb_usual_set_present(USB_US_TYPE_STOR);
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return retval;
1077}
1078
1079static void __exit usb_stor_exit(void)
1080{
1081 US_DEBUGP("usb_stor_exit() called\n");
1082
1083 /* Deregister the driver
1084 * This will cause disconnect() to be called for each
1085 * attached unit
1086 */
1087 US_DEBUGP("-- calling usb_deregister()\n");
1088 usb_deregister(&usb_storage_driver) ;
1089
Pete Zaitceva00828e2005-10-22 20:15:09 -07001090 usb_usual_clear_present(USB_US_TYPE_STOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093module_init(usb_stor_init);
1094module_exit(usb_stor_exit);