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