xref: /openbmc/linux/drivers/usb/storage/usb.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1 /* 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 
50 #include <linux/config.h>
51 #include <linux/sched.h>
52 #include <linux/errno.h>
53 #include <linux/suspend.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
57 
58 #include <scsi/scsi.h>
59 #include <scsi/scsi_cmnd.h>
60 #include <scsi/scsi_device.h>
61 
62 #include "usb.h"
63 #include "scsiglue.h"
64 #include "transport.h"
65 #include "protocol.h"
66 #include "debug.h"
67 #include "initializers.h"
68 
69 #ifdef CONFIG_USB_STORAGE_USBAT
70 #include "shuttle_usbat.h"
71 #endif
72 #ifdef CONFIG_USB_STORAGE_SDDR09
73 #include "sddr09.h"
74 #endif
75 #ifdef CONFIG_USB_STORAGE_SDDR55
76 #include "sddr55.h"
77 #endif
78 #ifdef CONFIG_USB_STORAGE_DPCM
79 #include "dpcm.h"
80 #endif
81 #ifdef CONFIG_USB_STORAGE_FREECOM
82 #include "freecom.h"
83 #endif
84 #ifdef CONFIG_USB_STORAGE_ISD200
85 #include "isd200.h"
86 #endif
87 #ifdef CONFIG_USB_STORAGE_DATAFAB
88 #include "datafab.h"
89 #endif
90 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
91 #include "jumpshot.h"
92 #endif
93 
94 
95 /* Some informational data */
96 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
97 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
98 MODULE_LICENSE("GPL");
99 
100 static unsigned int delay_use = 5;
101 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
102 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
103 
104 
105 /* These are used to make sure the module doesn't unload before all the
106  * threads have exited.
107  */
108 static atomic_t total_threads = ATOMIC_INIT(0);
109 static DECLARE_COMPLETION(threads_gone);
110 
111 
112 static int storage_probe(struct usb_interface *iface,
113 			 const struct usb_device_id *id);
114 
115 static void storage_disconnect(struct usb_interface *iface);
116 
117 /* The entries in this table, except for final ones here
118  * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
119  * line for line with the entries of us_unsuaul_dev_list[].
120  */
121 
122 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
123 		    vendorName, productName,useProtocol, useTransport, \
124 		    initFunction, flags) \
125 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
126 
127 static struct usb_device_id storage_usb_ids [] = {
128 
129 #	include "unusual_devs.h"
130 #undef UNUSUAL_DEV
131 	/* Control/Bulk transport for all SubClass values */
132 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CB) },
133 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CB) },
134 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CB) },
135 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CB) },
136 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CB) },
137 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CB) },
138 
139 	/* Control/Bulk/Interrupt transport for all SubClass values */
140 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CBI) },
141 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CBI) },
142 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CBI) },
143 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CBI) },
144 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CBI) },
145 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CBI) },
146 
147 	/* Bulk-only transport for all SubClass values */
148 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_BULK) },
149 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_BULK) },
150 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_BULK) },
151 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_BULK) },
152 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_BULK) },
153 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
154 
155 	/* Terminating entry */
156 	{ }
157 };
158 
159 MODULE_DEVICE_TABLE (usb, storage_usb_ids);
160 
161 /* This is the list of devices we recognize, along with their flag data */
162 
163 /* The vendor name should be kept at eight characters or less, and
164  * the product name should be kept at 16 characters or less. If a device
165  * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
166  * normally generated by a device thorugh the INQUIRY response will be
167  * taken from this list, and this is the reason for the above size
168  * restriction. However, if the flag is not present, then you
169  * are free to use as many characters as you like.
170  */
171 
172 #undef UNUSUAL_DEV
173 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
174 		    vendor_name, product_name, use_protocol, use_transport, \
175 		    init_function, Flags) \
176 { \
177 	.vendorName = vendor_name,	\
178 	.productName = product_name,	\
179 	.useProtocol = use_protocol,	\
180 	.useTransport = use_transport,	\
181 	.initFunction = init_function,	\
182 	.flags = Flags, \
183 }
184 
185 static struct us_unusual_dev us_unusual_dev_list[] = {
186 #	include "unusual_devs.h"
187 #	undef UNUSUAL_DEV
188 	/* Control/Bulk transport for all SubClass values */
189 	{ .useProtocol = US_SC_RBC,
190 	  .useTransport = US_PR_CB},
191 	{ .useProtocol = US_SC_8020,
192 	  .useTransport = US_PR_CB},
193 	{ .useProtocol = US_SC_QIC,
194 	  .useTransport = US_PR_CB},
195 	{ .useProtocol = US_SC_UFI,
196 	  .useTransport = US_PR_CB},
197 	{ .useProtocol = US_SC_8070,
198 	  .useTransport = US_PR_CB},
199 	{ .useProtocol = US_SC_SCSI,
200 	  .useTransport = US_PR_CB},
201 
202 	/* Control/Bulk/Interrupt transport for all SubClass values */
203 	{ .useProtocol = US_SC_RBC,
204 	  .useTransport = US_PR_CBI},
205 	{ .useProtocol = US_SC_8020,
206 	  .useTransport = US_PR_CBI},
207 	{ .useProtocol = US_SC_QIC,
208 	  .useTransport = US_PR_CBI},
209 	{ .useProtocol = US_SC_UFI,
210 	  .useTransport = US_PR_CBI},
211 	{ .useProtocol = US_SC_8070,
212 	  .useTransport = US_PR_CBI},
213 	{ .useProtocol = US_SC_SCSI,
214 	  .useTransport = US_PR_CBI},
215 
216 	/* Bulk-only transport for all SubClass values */
217 	{ .useProtocol = US_SC_RBC,
218 	  .useTransport = US_PR_BULK},
219 	{ .useProtocol = US_SC_8020,
220 	  .useTransport = US_PR_BULK},
221 	{ .useProtocol = US_SC_QIC,
222 	  .useTransport = US_PR_BULK},
223 	{ .useProtocol = US_SC_UFI,
224 	  .useTransport = US_PR_BULK},
225 	{ .useProtocol = US_SC_8070,
226 	  .useTransport = US_PR_BULK},
227 	{ .useProtocol = US_SC_SCSI,
228 	  .useTransport = US_PR_BULK},
229 
230 	/* Terminating entry */
231 	{ NULL }
232 };
233 
234 static struct usb_driver usb_storage_driver = {
235 	.owner =	THIS_MODULE,
236 	.name =		"usb-storage",
237 	.probe =	storage_probe,
238 	.disconnect =	storage_disconnect,
239 	.id_table =	storage_usb_ids,
240 };
241 
242 /*
243  * fill_inquiry_response takes an unsigned char array (which must
244  * be at least 36 characters) and populates the vendor name,
245  * product name, and revision fields. Then the array is copied
246  * into the SCSI command's response buffer (oddly enough
247  * called request_buffer). data_len contains the length of the
248  * data array, which again must be at least 36.
249  */
250 
251 void fill_inquiry_response(struct us_data *us, unsigned char *data,
252 		unsigned int data_len)
253 {
254 	if (data_len<36) // You lose.
255 		return;
256 
257 	if(data[0]&0x20) { /* USB device currently not connected. Return
258 			      peripheral qualifier 001b ("...however, the
259 			      physical device is not currently connected
260 			      to this logical unit") and leave vendor and
261 			      product identification empty. ("If the target
262 			      does store some of the INQUIRY data on the
263 			      device, it may return zeros or ASCII spaces
264 			      (20h) in those fields until the data is
265 			      available from the device."). */
266 		memset(data+8,0,28);
267 	} else {
268 		u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
269 		memcpy(data+8, us->unusual_dev->vendorName,
270 			strlen(us->unusual_dev->vendorName) > 8 ? 8 :
271 			strlen(us->unusual_dev->vendorName));
272 		memcpy(data+16, us->unusual_dev->productName,
273 			strlen(us->unusual_dev->productName) > 16 ? 16 :
274 			strlen(us->unusual_dev->productName));
275 		data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
276 		data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
277 		data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
278 		data[35] = 0x30 + ((bcdDevice) & 0x0F);
279 	}
280 
281 	usb_stor_set_xfer_buf(data, data_len, us->srb);
282 }
283 
284 static int usb_stor_control_thread(void * __us)
285 {
286 	struct us_data *us = (struct us_data *)__us;
287 	struct Scsi_Host *host = us_to_host(us);
288 
289 	lock_kernel();
290 
291 	/*
292 	 * This thread doesn't need any user-level access,
293 	 * so get rid of all our resources.
294 	 */
295 	daemonize("usb-storage");
296 	current->flags |= PF_NOFREEZE;
297 	unlock_kernel();
298 
299 	/* acquire a reference to the host, so it won't be deallocated
300 	 * until we're ready to exit */
301 	scsi_host_get(host);
302 
303 	/* signal that we've started the thread */
304 	complete(&(us->notify));
305 
306 	for(;;) {
307 		US_DEBUGP("*** thread sleeping.\n");
308 		if(down_interruptible(&us->sema))
309 			break;
310 
311 		US_DEBUGP("*** thread awakened.\n");
312 
313 		/* lock the device pointers */
314 		down(&(us->dev_semaphore));
315 
316 		/* if the device has disconnected, we are free to exit */
317 		if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
318 			US_DEBUGP("-- exiting\n");
319 			up(&(us->dev_semaphore));
320 			break;
321 		}
322 
323 		/* lock access to the state */
324 		scsi_lock(host);
325 
326 		/* has the command timed out *already* ? */
327 		if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
328 			us->srb->result = DID_ABORT << 16;
329 			goto SkipForAbort;
330 		}
331 
332 		scsi_unlock(host);
333 
334 		/* reject the command if the direction indicator
335 		 * is UNKNOWN
336 		 */
337 		if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
338 			US_DEBUGP("UNKNOWN data direction\n");
339 			us->srb->result = DID_ERROR << 16;
340 		}
341 
342 		/* reject if target != 0 or if LUN is higher than
343 		 * the maximum known LUN
344 		 */
345 		else if (us->srb->device->id &&
346 				!(us->flags & US_FL_SCM_MULT_TARG)) {
347 			US_DEBUGP("Bad target number (%d:%d)\n",
348 				  us->srb->device->id, us->srb->device->lun);
349 			us->srb->result = DID_BAD_TARGET << 16;
350 		}
351 
352 		else if (us->srb->device->lun > us->max_lun) {
353 			US_DEBUGP("Bad LUN (%d:%d)\n",
354 				  us->srb->device->id, us->srb->device->lun);
355 			us->srb->result = DID_BAD_TARGET << 16;
356 		}
357 
358 		/* Handle those devices which need us to fake
359 		 * their inquiry data */
360 		else if ((us->srb->cmnd[0] == INQUIRY) &&
361 			    (us->flags & US_FL_FIX_INQUIRY)) {
362 			unsigned char data_ptr[36] = {
363 			    0x00, 0x80, 0x02, 0x02,
364 			    0x1F, 0x00, 0x00, 0x00};
365 
366 			US_DEBUGP("Faking INQUIRY command\n");
367 			fill_inquiry_response(us, data_ptr, 36);
368 			us->srb->result = SAM_STAT_GOOD;
369 		}
370 
371 		/* we've got a command, let's do it! */
372 		else {
373 			US_DEBUG(usb_stor_show_command(us->srb));
374 			us->proto_handler(us->srb, us);
375 		}
376 
377 		/* lock access to the state */
378 		scsi_lock(host);
379 
380 		/* indicate that the command is done */
381 		if (us->srb->result != DID_ABORT << 16) {
382 			US_DEBUGP("scsi cmd done, result=0x%x\n",
383 				   us->srb->result);
384 			us->srb->scsi_done(us->srb);
385 		} else {
386 SkipForAbort:
387 			US_DEBUGP("scsi command aborted\n");
388 		}
389 
390 		/* If an abort request was received we need to signal that
391 		 * the abort has finished.  The proper test for this is
392 		 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
393 		 * a timeout/abort request might be received after all the
394 		 * USB processing was complete. */
395 		if (test_bit(US_FLIDX_TIMED_OUT, &us->flags))
396 			complete(&(us->notify));
397 
398 		/* finished working on this command */
399 		us->srb = NULL;
400 		scsi_unlock(host);
401 
402 		/* unlock the device pointers */
403 		up(&(us->dev_semaphore));
404 	} /* for (;;) */
405 
406 	scsi_host_put(host);
407 
408 	/* notify the exit routine that we're actually exiting now
409 	 *
410 	 * complete()/wait_for_completion() is similar to up()/down(),
411 	 * except that complete() is safe in the case where the structure
412 	 * is getting deleted in a parallel mode of execution (i.e. just
413 	 * after the down() -- that's necessary for the thread-shutdown
414 	 * case.
415 	 *
416 	 * complete_and_exit() goes even further than this -- it is safe in
417 	 * the case that the thread of the caller is going away (not just
418 	 * the structure) -- this is necessary for the module-remove case.
419 	 * This is important in preemption kernels, which transfer the flow
420 	 * of execution immediately upon a complete().
421 	 */
422 	complete_and_exit(&threads_gone, 0);
423 }
424 
425 /***********************************************************************
426  * Device probing and disconnecting
427  ***********************************************************************/
428 
429 /* Associate our private data with the USB device */
430 static int associate_dev(struct us_data *us, struct usb_interface *intf)
431 {
432 	US_DEBUGP("-- %s\n", __FUNCTION__);
433 
434 	/* Fill in the device-related fields */
435 	us->pusb_dev = interface_to_usbdev(intf);
436 	us->pusb_intf = intf;
437 	us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
438 	US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
439 			le16_to_cpu(us->pusb_dev->descriptor.idVendor),
440 			le16_to_cpu(us->pusb_dev->descriptor.idProduct),
441 			le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
442 	US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
443 			intf->cur_altsetting->desc.bInterfaceSubClass,
444 			intf->cur_altsetting->desc.bInterfaceProtocol);
445 
446 	/* Store our private data in the interface */
447 	usb_set_intfdata(intf, us);
448 
449 	/* Allocate the device-related DMA-mapped buffers */
450 	us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
451 			GFP_KERNEL, &us->cr_dma);
452 	if (!us->cr) {
453 		US_DEBUGP("usb_ctrlrequest allocation failed\n");
454 		return -ENOMEM;
455 	}
456 
457 	us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
458 			GFP_KERNEL, &us->iobuf_dma);
459 	if (!us->iobuf) {
460 		US_DEBUGP("I/O buffer allocation failed\n");
461 		return -ENOMEM;
462 	}
463 	return 0;
464 }
465 
466 /* Get the unusual_devs entries and the string descriptors */
467 static void get_device_info(struct us_data *us, int id_index)
468 {
469 	struct usb_device *dev = us->pusb_dev;
470 	struct usb_interface_descriptor *idesc =
471 		&us->pusb_intf->cur_altsetting->desc;
472 	struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
473 	struct usb_device_id *id = &storage_usb_ids[id_index];
474 
475 	/* Store the entries */
476 	us->unusual_dev = unusual_dev;
477 	us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
478 			idesc->bInterfaceSubClass :
479 			unusual_dev->useProtocol;
480 	us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
481 			idesc->bInterfaceProtocol :
482 			unusual_dev->useTransport;
483 	us->flags = unusual_dev->flags;
484 
485 	/*
486 	 * This flag is only needed when we're in high-speed, so let's
487 	 * disable it if we're in full-speed
488 	 */
489 	if (dev->speed != USB_SPEED_HIGH)
490 		us->flags &= ~US_FL_GO_SLOW;
491 
492 	/* Log a message if a non-generic unusual_dev entry contains an
493 	 * unnecessary subclass or protocol override.  This may stimulate
494 	 * reports from users that will help us remove unneeded entries
495 	 * from the unusual_devs.h table.
496 	 */
497 	if (id->idVendor || id->idProduct) {
498 		static char *msgs[3] = {
499 			"an unneeded SubClass entry",
500 			"an unneeded Protocol entry",
501 			"unneeded SubClass and Protocol entries"};
502 		struct usb_device_descriptor *ddesc = &dev->descriptor;
503 		int msg = -1;
504 
505 		if (unusual_dev->useProtocol != US_SC_DEVICE &&
506 			us->subclass == idesc->bInterfaceSubClass)
507 			msg += 1;
508 		if (unusual_dev->useTransport != US_PR_DEVICE &&
509 			us->protocol == idesc->bInterfaceProtocol)
510 			msg += 2;
511 		if (msg >= 0 && !(unusual_dev->flags & US_FL_NEED_OVERRIDE))
512 			printk(KERN_NOTICE USB_STORAGE "This device "
513 				"(%04x,%04x,%04x S %02x P %02x)"
514 				" has %s in unusual_devs.h\n"
515 				"   Please send a copy of this message to "
516 				"<linux-usb-devel@lists.sourceforge.net>\n",
517 				le16_to_cpu(ddesc->idVendor),
518 				le16_to_cpu(ddesc->idProduct),
519 				le16_to_cpu(ddesc->bcdDevice),
520 				idesc->bInterfaceSubClass,
521 				idesc->bInterfaceProtocol,
522 				msgs[msg]);
523 	}
524 }
525 
526 /* Get the transport settings */
527 static int get_transport(struct us_data *us)
528 {
529 	switch (us->protocol) {
530 	case US_PR_CB:
531 		us->transport_name = "Control/Bulk";
532 		us->transport = usb_stor_CB_transport;
533 		us->transport_reset = usb_stor_CB_reset;
534 		us->max_lun = 7;
535 		break;
536 
537 	case US_PR_CBI:
538 		us->transport_name = "Control/Bulk/Interrupt";
539 		us->transport = usb_stor_CBI_transport;
540 		us->transport_reset = usb_stor_CB_reset;
541 		us->max_lun = 7;
542 		break;
543 
544 	case US_PR_BULK:
545 		us->transport_name = "Bulk";
546 		us->transport = usb_stor_Bulk_transport;
547 		us->transport_reset = usb_stor_Bulk_reset;
548 		break;
549 
550 #ifdef CONFIG_USB_STORAGE_USBAT
551 	case US_PR_SCM_ATAPI:
552 		us->transport_name = "SCM/ATAPI";
553 		us->transport = usbat_transport;
554 		us->transport_reset = usb_stor_CB_reset;
555 		us->max_lun = 1;
556 		break;
557 #endif
558 
559 #ifdef CONFIG_USB_STORAGE_SDDR09
560 	case US_PR_EUSB_SDDR09:
561 		us->transport_name = "EUSB/SDDR09";
562 		us->transport = sddr09_transport;
563 		us->transport_reset = usb_stor_CB_reset;
564 		us->max_lun = 0;
565 		break;
566 #endif
567 
568 #ifdef CONFIG_USB_STORAGE_SDDR55
569 	case US_PR_SDDR55:
570 		us->transport_name = "SDDR55";
571 		us->transport = sddr55_transport;
572 		us->transport_reset = sddr55_reset;
573 		us->max_lun = 0;
574 		break;
575 #endif
576 
577 #ifdef CONFIG_USB_STORAGE_DPCM
578 	case US_PR_DPCM_USB:
579 		us->transport_name = "Control/Bulk-EUSB/SDDR09";
580 		us->transport = dpcm_transport;
581 		us->transport_reset = usb_stor_CB_reset;
582 		us->max_lun = 1;
583 		break;
584 #endif
585 
586 #ifdef CONFIG_USB_STORAGE_FREECOM
587 	case US_PR_FREECOM:
588 		us->transport_name = "Freecom";
589 		us->transport = freecom_transport;
590 		us->transport_reset = usb_stor_freecom_reset;
591 		us->max_lun = 0;
592 		break;
593 #endif
594 
595 #ifdef CONFIG_USB_STORAGE_DATAFAB
596 	case US_PR_DATAFAB:
597 		us->transport_name  = "Datafab Bulk-Only";
598 		us->transport = datafab_transport;
599 		us->transport_reset = usb_stor_Bulk_reset;
600 		us->max_lun = 1;
601 		break;
602 #endif
603 
604 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
605 	case US_PR_JUMPSHOT:
606 		us->transport_name  = "Lexar Jumpshot Control/Bulk";
607 		us->transport = jumpshot_transport;
608 		us->transport_reset = usb_stor_Bulk_reset;
609 		us->max_lun = 1;
610 		break;
611 #endif
612 
613 	default:
614 		return -EIO;
615 	}
616 	US_DEBUGP("Transport: %s\n", us->transport_name);
617 
618 	/* fix for single-lun devices */
619 	if (us->flags & US_FL_SINGLE_LUN)
620 		us->max_lun = 0;
621 	return 0;
622 }
623 
624 /* Get the protocol settings */
625 static int get_protocol(struct us_data *us)
626 {
627 	switch (us->subclass) {
628 	case US_SC_RBC:
629 		us->protocol_name = "Reduced Block Commands (RBC)";
630 		us->proto_handler = usb_stor_transparent_scsi_command;
631 		break;
632 
633 	case US_SC_8020:
634 		us->protocol_name = "8020i";
635 		us->proto_handler = usb_stor_ATAPI_command;
636 		us->max_lun = 0;
637 		break;
638 
639 	case US_SC_QIC:
640 		us->protocol_name = "QIC-157";
641 		us->proto_handler = usb_stor_qic157_command;
642 		us->max_lun = 0;
643 		break;
644 
645 	case US_SC_8070:
646 		us->protocol_name = "8070i";
647 		us->proto_handler = usb_stor_ATAPI_command;
648 		us->max_lun = 0;
649 		break;
650 
651 	case US_SC_SCSI:
652 		us->protocol_name = "Transparent SCSI";
653 		us->proto_handler = usb_stor_transparent_scsi_command;
654 		break;
655 
656 	case US_SC_UFI:
657 		us->protocol_name = "Uniform Floppy Interface (UFI)";
658 		us->proto_handler = usb_stor_ufi_command;
659 		break;
660 
661 #ifdef CONFIG_USB_STORAGE_ISD200
662 	case US_SC_ISD200:
663 		us->protocol_name = "ISD200 ATA/ATAPI";
664 		us->proto_handler = isd200_ata_command;
665 		break;
666 #endif
667 
668 	default:
669 		return -EIO;
670 	}
671 	US_DEBUGP("Protocol: %s\n", us->protocol_name);
672 	return 0;
673 }
674 
675 /* Get the pipe settings */
676 static int get_pipes(struct us_data *us)
677 {
678 	struct usb_host_interface *altsetting =
679 		us->pusb_intf->cur_altsetting;
680 	int i;
681 	struct usb_endpoint_descriptor *ep;
682 	struct usb_endpoint_descriptor *ep_in = NULL;
683 	struct usb_endpoint_descriptor *ep_out = NULL;
684 	struct usb_endpoint_descriptor *ep_int = NULL;
685 
686 	/*
687 	 * Find the endpoints we need.
688 	 * We are expecting a minimum of 2 endpoints - in and out (bulk).
689 	 * An optional interrupt is OK (necessary for CBI protocol).
690 	 * We will ignore any others.
691 	 */
692 	for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
693 		ep = &altsetting->endpoint[i].desc;
694 
695 		/* Is it a BULK endpoint? */
696 		if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
697 				== USB_ENDPOINT_XFER_BULK) {
698 			/* BULK in or out? */
699 			if (ep->bEndpointAddress & USB_DIR_IN)
700 				ep_in = ep;
701 			else
702 				ep_out = ep;
703 		}
704 
705 		/* Is it an interrupt endpoint? */
706 		else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
707 				== USB_ENDPOINT_XFER_INT) {
708 			ep_int = ep;
709 		}
710 	}
711 
712 	if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
713 		US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
714 		return -EIO;
715 	}
716 
717 	/* Calculate and store the pipe values */
718 	us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
719 	us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
720 	us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
721 		ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
722 	us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
723 		ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
724 	if (ep_int) {
725 		us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
726 			ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
727 		us->ep_bInterval = ep_int->bInterval;
728 	}
729 	return 0;
730 }
731 
732 /* Initialize all the dynamic resources we need */
733 static int usb_stor_acquire_resources(struct us_data *us)
734 {
735 	int p;
736 
737 	us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
738 	if (!us->current_urb) {
739 		US_DEBUGP("URB allocation failed\n");
740 		return -ENOMEM;
741 	}
742 
743 	/* Lock the device while we carry out the next two operations */
744 	down(&us->dev_semaphore);
745 
746 	/* For bulk-only devices, determine the max LUN value */
747 	if (us->protocol == US_PR_BULK) {
748 		p = usb_stor_Bulk_max_lun(us);
749 		if (p < 0) {
750 			up(&us->dev_semaphore);
751 			return p;
752 		}
753 		us->max_lun = p;
754 	}
755 
756 	/* Just before we start our control thread, initialize
757 	 * the device if it needs initialization */
758 	if (us->unusual_dev->initFunction)
759 		us->unusual_dev->initFunction(us);
760 
761 	up(&us->dev_semaphore);
762 
763 	/* Start up our control thread */
764 	p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
765 	if (p < 0) {
766 		printk(KERN_WARNING USB_STORAGE
767 		       "Unable to start control thread\n");
768 		return p;
769 	}
770 	us->pid = p;
771 	atomic_inc(&total_threads);
772 
773 	/* Wait for the thread to start */
774 	wait_for_completion(&(us->notify));
775 
776 	return 0;
777 }
778 
779 /* Release all our dynamic resources */
780 static void usb_stor_release_resources(struct us_data *us)
781 {
782 	US_DEBUGP("-- %s\n", __FUNCTION__);
783 
784 	/* Tell the control thread to exit.  The SCSI host must
785 	 * already have been removed so it won't try to queue
786 	 * any more commands.
787 	 */
788 	US_DEBUGP("-- sending exit command to thread\n");
789 	up(&us->sema);
790 
791 	/* Call the destructor routine, if it exists */
792 	if (us->extra_destructor) {
793 		US_DEBUGP("-- calling extra_destructor()\n");
794 		us->extra_destructor(us->extra);
795 	}
796 
797 	/* Free the extra data and the URB */
798 	kfree(us->extra);
799 	usb_free_urb(us->current_urb);
800 }
801 
802 /* Dissociate from the USB device */
803 static void dissociate_dev(struct us_data *us)
804 {
805 	US_DEBUGP("-- %s\n", __FUNCTION__);
806 
807 	/* Free the device-related DMA-mapped buffers */
808 	if (us->cr)
809 		usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
810 				us->cr_dma);
811 	if (us->iobuf)
812 		usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
813 				us->iobuf_dma);
814 
815 	/* Remove our private data from the interface */
816 	usb_set_intfdata(us->pusb_intf, NULL);
817 }
818 
819 /* Thread to carry out delayed SCSI-device scanning */
820 static int usb_stor_scan_thread(void * __us)
821 {
822 	struct us_data *us = (struct us_data *)__us;
823 
824 	/*
825 	 * This thread doesn't need any user-level access,
826 	 * so get rid of all our resources.
827 	 */
828 	lock_kernel();
829 	daemonize("usb-stor-scan");
830 	unlock_kernel();
831 
832 	/* Acquire a reference to the host, so it won't be deallocated
833 	 * until we're ready to exit */
834 	scsi_host_get(us_to_host(us));
835 
836 	/* Signal that we've started the thread */
837 	complete(&(us->notify));
838 
839 	printk(KERN_DEBUG
840 		"usb-storage: device found at %d\n", us->pusb_dev->devnum);
841 
842 	/* Wait for the timeout to expire or for a disconnect */
843 	if (delay_use > 0) {
844 		printk(KERN_DEBUG "usb-storage: waiting for device "
845 				"to settle before scanning\n");
846 retry:
847 		wait_event_interruptible_timeout(us->delay_wait,
848 				test_bit(US_FLIDX_DISCONNECTING, &us->flags),
849 				delay_use * HZ);
850 		if (current->flags & PF_FREEZE) {
851 			refrigerator(PF_FREEZE);
852 			goto retry;
853 		}
854 	}
855 
856 	/* If the device is still connected, perform the scanning */
857 	if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
858 		scsi_scan_host(us_to_host(us));
859 		printk(KERN_DEBUG "usb-storage: device scan complete\n");
860 
861 		/* Should we unbind if no devices were detected? */
862 	}
863 
864 	scsi_host_put(us_to_host(us));
865 	complete_and_exit(&threads_gone, 0);
866 }
867 
868 
869 /* Probe to see if we can drive a newly-connected USB device */
870 static int storage_probe(struct usb_interface *intf,
871 			 const struct usb_device_id *id)
872 {
873 	struct Scsi_Host *host;
874 	struct us_data *us;
875 	const int id_index = id - storage_usb_ids;
876 	int result;
877 
878 	US_DEBUGP("USB Mass Storage device detected\n");
879 
880 	/*
881 	 * Ask the SCSI layer to allocate a host structure, with extra
882 	 * space at the end for our private us_data structure.
883 	 */
884 	host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
885 	if (!host) {
886 		printk(KERN_WARNING USB_STORAGE
887 			"Unable to allocate the scsi host\n");
888 		return -ENOMEM;
889 	}
890 
891 	us = host_to_us(host);
892 	memset(us, 0, sizeof(struct us_data));
893 	init_MUTEX(&(us->dev_semaphore));
894 	init_MUTEX_LOCKED(&(us->sema));
895 	init_completion(&(us->notify));
896 	init_waitqueue_head(&us->delay_wait);
897 
898 	/* Associate the us_data structure with the USB device */
899 	result = associate_dev(us, intf);
900 	if (result)
901 		goto BadDevice;
902 
903 	/*
904 	 * Get the unusual_devs entries and the descriptors
905 	 *
906 	 * id_index is calculated in the declaration to be the index number
907 	 * of the match from the usb_device_id table, so we can find the
908 	 * corresponding entry in the private table.
909 	 */
910 	get_device_info(us, id_index);
911 
912 #ifdef CONFIG_USB_STORAGE_SDDR09
913 	if (us->protocol == US_PR_EUSB_SDDR09 ||
914 			us->protocol == US_PR_DPCM_USB) {
915 		/* set the configuration -- STALL is an acceptable response here */
916 		if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
917 			US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev
918 				->actconfig->desc.bConfigurationValue);
919 			goto BadDevice;
920 		}
921 		result = usb_reset_configuration(us->pusb_dev);
922 
923 		US_DEBUGP("Result of usb_reset_configuration is %d\n", result);
924 		if (result == -EPIPE) {
925 			US_DEBUGP("-- stall on control interface\n");
926 		} else if (result != 0) {
927 			/* it's not a stall, but another error -- time to bail */
928 			US_DEBUGP("-- Unknown error.  Rejecting device\n");
929 			goto BadDevice;
930 		}
931 	}
932 #endif
933 
934 	/* Get the transport, protocol, and pipe settings */
935 	result = get_transport(us);
936 	if (result)
937 		goto BadDevice;
938 	result = get_protocol(us);
939 	if (result)
940 		goto BadDevice;
941 	result = get_pipes(us);
942 	if (result)
943 		goto BadDevice;
944 
945 	/* Acquire all the other resources and add the host */
946 	result = usb_stor_acquire_resources(us);
947 	if (result)
948 		goto BadDevice;
949 	result = scsi_add_host(host, &intf->dev);
950 	if (result) {
951 		printk(KERN_WARNING USB_STORAGE
952 			"Unable to add the scsi host\n");
953 		goto BadDevice;
954 	}
955 
956 	/* Start up the thread for delayed SCSI-device scanning */
957 	result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM);
958 	if (result < 0) {
959 		printk(KERN_WARNING USB_STORAGE
960 		       "Unable to start the device-scanning thread\n");
961 		scsi_remove_host(host);
962 		goto BadDevice;
963 	}
964 	atomic_inc(&total_threads);
965 
966 	/* Wait for the thread to start */
967 	wait_for_completion(&(us->notify));
968 
969 	return 0;
970 
971 	/* We come here if there are any problems */
972 BadDevice:
973 	US_DEBUGP("storage_probe() failed\n");
974 	set_bit(US_FLIDX_DISCONNECTING, &us->flags);
975 	usb_stor_release_resources(us);
976 	dissociate_dev(us);
977 	scsi_host_put(host);
978 	return result;
979 }
980 
981 /* Handle a disconnect event from the USB core */
982 static void storage_disconnect(struct usb_interface *intf)
983 {
984 	struct us_data *us = usb_get_intfdata(intf);
985 
986 	US_DEBUGP("storage_disconnect() called\n");
987 
988 	/* Prevent new USB transfers, stop the current command, and
989 	 * interrupt a SCSI-scan or device-reset delay */
990 	set_bit(US_FLIDX_DISCONNECTING, &us->flags);
991 	usb_stor_stop_transport(us);
992 	wake_up(&us->delay_wait);
993 
994 	/* It doesn't matter if the SCSI-scanning thread is still running.
995 	 * The thread will exit when it sees the DISCONNECTING flag. */
996 
997 	/* Wait for the current command to finish, then remove the host */
998 	down(&us->dev_semaphore);
999 	up(&us->dev_semaphore);
1000 	scsi_remove_host(us_to_host(us));
1001 
1002 	/* Wait for everything to become idle and release all our resources */
1003 	usb_stor_release_resources(us);
1004 	dissociate_dev(us);
1005 
1006 	/* Drop our reference to the host; the SCSI core will free it
1007 	 * (and "us" along with it) when the refcount becomes 0. */
1008 	scsi_host_put(us_to_host(us));
1009 }
1010 
1011 /***********************************************************************
1012  * Initialization and registration
1013  ***********************************************************************/
1014 
1015 static int __init usb_stor_init(void)
1016 {
1017 	int retval;
1018 	printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1019 
1020 	/* register the driver, return usb_register return code if error */
1021 	retval = usb_register(&usb_storage_driver);
1022 	if (retval == 0)
1023 		printk(KERN_INFO "USB Mass Storage support registered.\n");
1024 
1025 	return retval;
1026 }
1027 
1028 static void __exit usb_stor_exit(void)
1029 {
1030 	US_DEBUGP("usb_stor_exit() called\n");
1031 
1032 	/* Deregister the driver
1033 	 * This will cause disconnect() to be called for each
1034 	 * attached unit
1035 	 */
1036 	US_DEBUGP("-- calling usb_deregister()\n");
1037 	usb_deregister(&usb_storage_driver) ;
1038 
1039 	/* Don't return until all of our control and scanning threads
1040 	 * have exited.  Since each thread signals threads_gone as its
1041 	 * last act, we have to call wait_for_completion the right number
1042 	 * of times.
1043 	 */
1044 	while (atomic_read(&total_threads) > 0) {
1045 		wait_for_completion(&threads_gone);
1046 		atomic_dec(&total_threads);
1047 	}
1048 }
1049 
1050 module_init(usb_stor_init);
1051 module_exit(usb_stor_exit);
1052