]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/storage/usb.c
USB: usb-storage: merge DPCM support into SDDR09
[linux-2.6-omap-h63xx.git] / drivers / usb / storage / usb.c
1 /* Driver for USB Mass Storage compliant devices
2  *
3  * Current development and maintenance by:
4  *   (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5  *
6  * Developed with the assistance of:
7  *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
8  *   (c) 2003 Alan Stern (stern@rowland.harvard.edu)
9  *
10  * Initial work by:
11  *   (c) 1999 Michael Gee (michael@linuxspecific.com)
12  *
13  * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
14  *   (c) 2000 Yggdrasil Computing, Inc.
15  *
16  * This driver is based on the 'USB Mass Storage Class' document. This
17  * describes in detail the protocol used to communicate with such
18  * devices.  Clearly, the designers had SCSI and ATAPI commands in
19  * mind when they created this document.  The commands are all very
20  * similar to commands in the SCSI-II and ATAPI specifications.
21  *
22  * It is important to note that in a number of cases this class
23  * exhibits class-specific exemptions from the USB specification.
24  * Notably the usage of NAK, STALL and ACK differs from the norm, in
25  * that they are used to communicate wait, failed and OK on commands.
26  *
27  * Also, for certain devices, the interrupt endpoint is used to convey
28  * status of a command.
29  *
30  * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31  * information about this driver.
32  *
33  * This program is free software; you can redistribute it and/or modify it
34  * under the terms of the GNU General Public License as published by the
35  * Free Software Foundation; either version 2, or (at your option) any
36  * later version.
37  *
38  * This program is distributed in the hope that it will be useful, but
39  * WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
41  * General Public License for more details.
42  *
43  * You should have received a copy of the GNU General Public License along
44  * with this program; if not, write to the Free Software Foundation, Inc.,
45  * 675 Mass Ave, Cambridge, MA 02139, USA.
46  */
47
48 #include <linux/sched.h>
49 #include <linux/errno.h>
50 #include <linux/freezer.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/kthread.h>
55 #include <linux/mutex.h>
56 #include <linux/utsname.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_FREECOM
79 #include "freecom.h"
80 #endif
81 #ifdef CONFIG_USB_STORAGE_ISD200
82 #include "isd200.h"
83 #endif
84 #ifdef CONFIG_USB_STORAGE_DATAFAB
85 #include "datafab.h"
86 #endif
87 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
88 #include "jumpshot.h"
89 #endif
90 #ifdef CONFIG_USB_STORAGE_ONETOUCH
91 #include "onetouch.h"
92 #endif
93 #ifdef CONFIG_USB_STORAGE_ALAUDA
94 #include "alauda.h"
95 #endif
96 #ifdef CONFIG_USB_STORAGE_KARMA
97 #include "karma.h"
98 #endif
99 #ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
100 #include "cypress_atacb.h"
101 #endif
102 #include "sierra_ms.h"
103
104 /* Some informational data */
105 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
106 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
107 MODULE_LICENSE("GPL");
108
109 static unsigned int delay_use = 5;
110 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
111 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
112
113 static char *quirks;
114 module_param(quirks, charp, S_IRUGO);
115 MODULE_PARM_DESC(quirks, "supplemental list of device IDs and their quirks");
116
117 struct quirks_entry {
118         u16     vid, pid;
119         u32     fflags;
120 };
121 static struct quirks_entry *quirks_list, *quirks_end;
122
123
124 /*
125  * The entries in this table correspond, line for line,
126  * with the entries of us_unusual_dev_list[].
127  */
128 #ifndef CONFIG_USB_LIBUSUAL
129
130 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
131                     vendorName, productName,useProtocol, useTransport, \
132                     initFunction, flags) \
133 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
134   .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
135
136 #define USUAL_DEV(useProto, useTrans, useType) \
137 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
138   .driver_info = (USB_US_TYPE_STOR<<24) }
139
140 static struct usb_device_id storage_usb_ids [] = {
141
142 #       include "unusual_devs.h"
143 #undef UNUSUAL_DEV
144 #undef USUAL_DEV
145         /* Terminating entry */
146         { }
147 };
148
149 MODULE_DEVICE_TABLE (usb, storage_usb_ids);
150 #endif /* CONFIG_USB_LIBUSUAL */
151
152 /* This is the list of devices we recognize, along with their flag data */
153
154 /* The vendor name should be kept at eight characters or less, and
155  * the product name should be kept at 16 characters or less. If a device
156  * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
157  * normally generated by a device thorugh the INQUIRY response will be
158  * taken from this list, and this is the reason for the above size
159  * restriction. However, if the flag is not present, then you
160  * are free to use as many characters as you like.
161  */
162
163 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
164                     vendor_name, product_name, use_protocol, use_transport, \
165                     init_function, Flags) \
166 { \
167         .vendorName = vendor_name,      \
168         .productName = product_name,    \
169         .useProtocol = use_protocol,    \
170         .useTransport = use_transport,  \
171         .initFunction = init_function,  \
172 }
173
174 #define USUAL_DEV(use_protocol, use_transport, use_type) \
175 { \
176         .useProtocol = use_protocol,    \
177         .useTransport = use_transport,  \
178 }
179
180 static struct us_unusual_dev us_unusual_dev_list[] = {
181 #       include "unusual_devs.h" 
182 #       undef UNUSUAL_DEV
183 #       undef USUAL_DEV
184
185         /* Terminating entry */
186         { NULL }
187 };
188
189
190 #ifdef CONFIG_PM        /* Minimal support for suspend and resume */
191
192 static int storage_suspend(struct usb_interface *iface, pm_message_t message)
193 {
194         struct us_data *us = usb_get_intfdata(iface);
195
196         /* Wait until no command is running */
197         mutex_lock(&us->dev_mutex);
198
199         US_DEBUGP("%s\n", __func__);
200         if (us->suspend_resume_hook)
201                 (us->suspend_resume_hook)(us, US_SUSPEND);
202
203         /* When runtime PM is working, we'll set a flag to indicate
204          * whether we should autoresume when a SCSI request arrives. */
205
206         mutex_unlock(&us->dev_mutex);
207         return 0;
208 }
209
210 static int storage_resume(struct usb_interface *iface)
211 {
212         struct us_data *us = usb_get_intfdata(iface);
213
214         mutex_lock(&us->dev_mutex);
215
216         US_DEBUGP("%s\n", __func__);
217         if (us->suspend_resume_hook)
218                 (us->suspend_resume_hook)(us, US_RESUME);
219
220         mutex_unlock(&us->dev_mutex);
221         return 0;
222 }
223
224 static int storage_reset_resume(struct usb_interface *iface)
225 {
226         struct us_data *us = usb_get_intfdata(iface);
227
228         US_DEBUGP("%s\n", __func__);
229
230         /* Report the reset to the SCSI core */
231         usb_stor_report_bus_reset(us);
232
233         /* FIXME: Notify the subdrivers that they need to reinitialize
234          * the device */
235         return 0;
236 }
237
238 #endif /* CONFIG_PM */
239
240 /*
241  * The next two routines get called just before and just after
242  * a USB port reset, whether from this driver or a different one.
243  */
244
245 static int storage_pre_reset(struct usb_interface *iface)
246 {
247         struct us_data *us = usb_get_intfdata(iface);
248
249         US_DEBUGP("%s\n", __func__);
250
251         /* Make sure no command runs during the reset */
252         mutex_lock(&us->dev_mutex);
253         return 0;
254 }
255
256 static int storage_post_reset(struct usb_interface *iface)
257 {
258         struct us_data *us = usb_get_intfdata(iface);
259
260         US_DEBUGP("%s\n", __func__);
261
262         /* Report the reset to the SCSI core */
263         usb_stor_report_bus_reset(us);
264
265         /* FIXME: Notify the subdrivers that they need to reinitialize
266          * the device */
267
268         mutex_unlock(&us->dev_mutex);
269         return 0;
270 }
271
272 /*
273  * fill_inquiry_response takes an unsigned char array (which must
274  * be at least 36 characters) and populates the vendor name,
275  * product name, and revision fields. Then the array is copied
276  * into the SCSI command's response buffer (oddly enough
277  * called request_buffer). data_len contains the length of the
278  * data array, which again must be at least 36.
279  */
280
281 void fill_inquiry_response(struct us_data *us, unsigned char *data,
282                 unsigned int data_len)
283 {
284         if (data_len<36) // You lose.
285                 return;
286
287         if(data[0]&0x20) { /* USB device currently not connected. Return
288                               peripheral qualifier 001b ("...however, the
289                               physical device is not currently connected
290                               to this logical unit") and leave vendor and
291                               product identification empty. ("If the target
292                               does store some of the INQUIRY data on the
293                               device, it may return zeros or ASCII spaces 
294                               (20h) in those fields until the data is
295                               available from the device."). */
296                 memset(data+8,0,28);
297         } else {
298                 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
299                 memcpy(data+8, us->unusual_dev->vendorName, 
300                         strlen(us->unusual_dev->vendorName) > 8 ? 8 :
301                         strlen(us->unusual_dev->vendorName));
302                 memcpy(data+16, us->unusual_dev->productName, 
303                         strlen(us->unusual_dev->productName) > 16 ? 16 :
304                         strlen(us->unusual_dev->productName));
305                 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
306                 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
307                 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
308                 data[35] = 0x30 + ((bcdDevice) & 0x0F);
309         }
310
311         usb_stor_set_xfer_buf(data, data_len, us->srb);
312 }
313
314 static int usb_stor_control_thread(void * __us)
315 {
316         struct us_data *us = (struct us_data *)__us;
317         struct Scsi_Host *host = us_to_host(us);
318
319         for(;;) {
320                 US_DEBUGP("*** thread sleeping.\n");
321                 if (wait_for_completion_interruptible(&us->cmnd_ready))
322                         break;
323
324                 US_DEBUGP("*** thread awakened.\n");
325
326                 /* lock the device pointers */
327                 mutex_lock(&(us->dev_mutex));
328
329                 /* lock access to the state */
330                 scsi_lock(host);
331
332                 /* When we are called with no command pending, we're done */
333                 if (us->srb == NULL) {
334                         scsi_unlock(host);
335                         mutex_unlock(&us->dev_mutex);
336                         US_DEBUGP("-- exiting\n");
337                         break;
338                 }
339
340                 /* has the command timed out *already* ? */
341                 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
342                         us->srb->result = DID_ABORT << 16;
343                         goto SkipForAbort;
344                 }
345
346                 scsi_unlock(host);
347
348                 /* reject the command if the direction indicator 
349                  * is UNKNOWN
350                  */
351                 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
352                         US_DEBUGP("UNKNOWN data direction\n");
353                         us->srb->result = DID_ERROR << 16;
354                 }
355
356                 /* reject if target != 0 or if LUN is higher than
357                  * the maximum known LUN
358                  */
359                 else if (us->srb->device->id && 
360                                 !(us->fflags & US_FL_SCM_MULT_TARG)) {
361                         US_DEBUGP("Bad target number (%d:%d)\n",
362                                   us->srb->device->id, us->srb->device->lun);
363                         us->srb->result = DID_BAD_TARGET << 16;
364                 }
365
366                 else if (us->srb->device->lun > us->max_lun) {
367                         US_DEBUGP("Bad LUN (%d:%d)\n",
368                                   us->srb->device->id, us->srb->device->lun);
369                         us->srb->result = DID_BAD_TARGET << 16;
370                 }
371
372                 /* Handle those devices which need us to fake 
373                  * their inquiry data */
374                 else if ((us->srb->cmnd[0] == INQUIRY) &&
375                             (us->fflags & US_FL_FIX_INQUIRY)) {
376                         unsigned char data_ptr[36] = {
377                             0x00, 0x80, 0x02, 0x02,
378                             0x1F, 0x00, 0x00, 0x00};
379
380                         US_DEBUGP("Faking INQUIRY command\n");
381                         fill_inquiry_response(us, data_ptr, 36);
382                         us->srb->result = SAM_STAT_GOOD;
383                 }
384
385                 /* we've got a command, let's do it! */
386                 else {
387                         US_DEBUG(usb_stor_show_command(us->srb));
388                         us->proto_handler(us->srb, us);
389                 }
390
391                 /* lock access to the state */
392                 scsi_lock(host);
393
394                 /* indicate that the command is done */
395                 if (us->srb->result != DID_ABORT << 16) {
396                         US_DEBUGP("scsi cmd done, result=0x%x\n", 
397                                    us->srb->result);
398                         us->srb->scsi_done(us->srb);
399                 } else {
400 SkipForAbort:
401                         US_DEBUGP("scsi command aborted\n");
402                 }
403
404                 /* If an abort request was received we need to signal that
405                  * the abort has finished.  The proper test for this is
406                  * the TIMED_OUT flag, not srb->result == DID_ABORT, because
407                  * the timeout might have occurred after the command had
408                  * already completed with a different result code. */
409                 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
410                         complete(&(us->notify));
411
412                         /* Allow USB transfers to resume */
413                         clear_bit(US_FLIDX_ABORTING, &us->dflags);
414                         clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
415                 }
416
417                 /* finished working on this command */
418                 us->srb = NULL;
419                 scsi_unlock(host);
420
421                 /* unlock the device pointers */
422                 mutex_unlock(&us->dev_mutex);
423         } /* for (;;) */
424
425         /* Wait until we are told to stop */
426         for (;;) {
427                 set_current_state(TASK_INTERRUPTIBLE);
428                 if (kthread_should_stop())
429                         break;
430                 schedule();
431         }
432         __set_current_state(TASK_RUNNING);
433         return 0;
434 }       
435
436 /***********************************************************************
437  * Device probing and disconnecting
438  ***********************************************************************/
439
440 /* Associate our private data with the USB device */
441 static int associate_dev(struct us_data *us, struct usb_interface *intf)
442 {
443         US_DEBUGP("-- %s\n", __func__);
444
445         /* Fill in the device-related fields */
446         us->pusb_dev = interface_to_usbdev(intf);
447         us->pusb_intf = intf;
448         us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
449         US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
450                         le16_to_cpu(us->pusb_dev->descriptor.idVendor),
451                         le16_to_cpu(us->pusb_dev->descriptor.idProduct),
452                         le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
453         US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
454                         intf->cur_altsetting->desc.bInterfaceSubClass,
455                         intf->cur_altsetting->desc.bInterfaceProtocol);
456
457         /* Store our private data in the interface */
458         usb_set_intfdata(intf, us);
459
460         /* Allocate the device-related DMA-mapped buffers */
461         us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
462                         GFP_KERNEL, &us->cr_dma);
463         if (!us->cr) {
464                 US_DEBUGP("usb_ctrlrequest allocation failed\n");
465                 return -ENOMEM;
466         }
467
468         us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
469                         GFP_KERNEL, &us->iobuf_dma);
470         if (!us->iobuf) {
471                 US_DEBUGP("I/O buffer allocation failed\n");
472                 return -ENOMEM;
473         }
474         return 0;
475 }
476
477 /* Adjust device flags based on the "quirks=" module parameter */
478 static void adjust_quirks(struct us_data *us)
479 {
480         u16 vid, pid;
481         struct quirks_entry *q;
482         unsigned int mask = (US_FL_FIX_CAPACITY | US_FL_IGNORE_DEVICE |
483                         US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 |
484                         US_FL_IGNORE_RESIDUE | US_FL_SINGLE_LUN |
485                         US_FL_NO_WP_DETECT);
486
487         vid = le16_to_cpu(us->pusb_dev->descriptor.idVendor);
488         pid = le16_to_cpu(us->pusb_dev->descriptor.idProduct);
489
490         for (q = quirks_list; q != quirks_end; ++q) {
491                 if (q->vid == vid && q->pid == pid) {
492                         us->fflags = (us->fflags & ~mask) | q->fflags;
493                         dev_info(&us->pusb_intf->dev, "Quirks match for "
494                                         "vid %04x pid %04x: %x\n",
495                                         vid, pid, q->fflags);
496                         break;
497                 }
498         }
499 }
500
501 /* Find an unusual_dev descriptor (always succeeds in the current code) */
502 static struct us_unusual_dev *find_unusual(const struct usb_device_id *id)
503 {
504         const int id_index = id - storage_usb_ids;
505         return &us_unusual_dev_list[id_index];
506 }
507
508 /* Get the unusual_devs entries and the string descriptors */
509 static int get_device_info(struct us_data *us, const struct usb_device_id *id)
510 {
511         struct usb_device *dev = us->pusb_dev;
512         struct usb_interface_descriptor *idesc =
513                 &us->pusb_intf->cur_altsetting->desc;
514         struct us_unusual_dev *unusual_dev = find_unusual(id);
515
516         /* Store the entries */
517         us->unusual_dev = unusual_dev;
518         us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
519                         idesc->bInterfaceSubClass :
520                         unusual_dev->useProtocol;
521         us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
522                         idesc->bInterfaceProtocol :
523                         unusual_dev->useTransport;
524         us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
525         adjust_quirks(us);
526
527         if (us->fflags & US_FL_IGNORE_DEVICE) {
528                 printk(KERN_INFO USB_STORAGE "device ignored\n");
529                 return -ENODEV;
530         }
531
532         /*
533          * This flag is only needed when we're in high-speed, so let's
534          * disable it if we're in full-speed
535          */
536         if (dev->speed != USB_SPEED_HIGH)
537                 us->fflags &= ~US_FL_GO_SLOW;
538
539         /* Log a message if a non-generic unusual_dev entry contains an
540          * unnecessary subclass or protocol override.  This may stimulate
541          * reports from users that will help us remove unneeded entries
542          * from the unusual_devs.h table.
543          */
544         if (id->idVendor || id->idProduct) {
545                 static const char *msgs[3] = {
546                         "an unneeded SubClass entry",
547                         "an unneeded Protocol entry",
548                         "unneeded SubClass and Protocol entries"};
549                 struct usb_device_descriptor *ddesc = &dev->descriptor;
550                 int msg = -1;
551
552                 if (unusual_dev->useProtocol != US_SC_DEVICE &&
553                         us->subclass == idesc->bInterfaceSubClass)
554                         msg += 1;
555                 if (unusual_dev->useTransport != US_PR_DEVICE &&
556                         us->protocol == idesc->bInterfaceProtocol)
557                         msg += 2;
558                 if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
559                         printk(KERN_NOTICE USB_STORAGE "This device "
560                                 "(%04x,%04x,%04x S %02x P %02x)"
561                                 " has %s in unusual_devs.h (kernel"
562                                 " %s)\n"
563                                 "   Please send a copy of this message to "
564                                 "<linux-usb@vger.kernel.org> and "
565                                 "<usb-storage@lists.one-eyed-alien.net>\n",
566                                 le16_to_cpu(ddesc->idVendor),
567                                 le16_to_cpu(ddesc->idProduct),
568                                 le16_to_cpu(ddesc->bcdDevice),
569                                 idesc->bInterfaceSubClass,
570                                 idesc->bInterfaceProtocol,
571                                 msgs[msg],
572                                 utsname()->release);
573         }
574
575         return 0;
576 }
577
578 /* Get the transport settings */
579 static int get_transport(struct us_data *us)
580 {
581         switch (us->protocol) {
582         case US_PR_CB:
583                 us->transport_name = "Control/Bulk";
584                 us->transport = usb_stor_CB_transport;
585                 us->transport_reset = usb_stor_CB_reset;
586                 us->max_lun = 7;
587                 break;
588
589         case US_PR_CBI:
590                 us->transport_name = "Control/Bulk/Interrupt";
591                 us->transport = usb_stor_CB_transport;
592                 us->transport_reset = usb_stor_CB_reset;
593                 us->max_lun = 7;
594                 break;
595
596         case US_PR_BULK:
597                 us->transport_name = "Bulk";
598                 us->transport = usb_stor_Bulk_transport;
599                 us->transport_reset = usb_stor_Bulk_reset;
600                 break;
601
602 #ifdef CONFIG_USB_STORAGE_USBAT
603         case US_PR_USBAT:
604                 us->transport_name = "Shuttle USBAT";
605                 us->transport = usbat_transport;
606                 us->transport_reset = usb_stor_CB_reset;
607                 us->max_lun = 1;
608                 break;
609 #endif
610
611 #ifdef CONFIG_USB_STORAGE_SDDR09
612         case US_PR_EUSB_SDDR09:
613                 us->transport_name = "EUSB/SDDR09";
614                 us->transport = sddr09_transport;
615                 us->transport_reset = usb_stor_CB_reset;
616                 us->max_lun = 0;
617                 break;
618 #endif
619
620 #ifdef CONFIG_USB_STORAGE_SDDR55
621         case US_PR_SDDR55:
622                 us->transport_name = "SDDR55";
623                 us->transport = sddr55_transport;
624                 us->transport_reset = sddr55_reset;
625                 us->max_lun = 0;
626                 break;
627 #endif
628
629 #ifdef CONFIG_USB_STORAGE_DPCM
630         case US_PR_DPCM_USB:
631                 us->transport_name = "Control/Bulk-EUSB/SDDR09";
632                 us->transport = dpcm_transport;
633                 us->transport_reset = usb_stor_CB_reset;
634                 us->max_lun = 1;
635                 break;
636 #endif
637
638 #ifdef CONFIG_USB_STORAGE_FREECOM
639         case US_PR_FREECOM:
640                 us->transport_name = "Freecom";
641                 us->transport = freecom_transport;
642                 us->transport_reset = usb_stor_freecom_reset;
643                 us->max_lun = 0;
644                 break;
645 #endif
646
647 #ifdef CONFIG_USB_STORAGE_DATAFAB
648         case US_PR_DATAFAB:
649                 us->transport_name  = "Datafab Bulk-Only";
650                 us->transport = datafab_transport;
651                 us->transport_reset = usb_stor_Bulk_reset;
652                 us->max_lun = 1;
653                 break;
654 #endif
655
656 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
657         case US_PR_JUMPSHOT:
658                 us->transport_name  = "Lexar Jumpshot Control/Bulk";
659                 us->transport = jumpshot_transport;
660                 us->transport_reset = usb_stor_Bulk_reset;
661                 us->max_lun = 1;
662                 break;
663 #endif
664
665 #ifdef CONFIG_USB_STORAGE_ALAUDA
666         case US_PR_ALAUDA:
667                 us->transport_name  = "Alauda Control/Bulk";
668                 us->transport = alauda_transport;
669                 us->transport_reset = usb_stor_Bulk_reset;
670                 us->max_lun = 1;
671                 break;
672 #endif
673
674 #ifdef CONFIG_USB_STORAGE_KARMA
675         case US_PR_KARMA:
676                 us->transport_name = "Rio Karma/Bulk";
677                 us->transport = rio_karma_transport;
678                 us->transport_reset = usb_stor_Bulk_reset;
679                 break;
680 #endif
681
682         default:
683                 return -EIO;
684         }
685         US_DEBUGP("Transport: %s\n", us->transport_name);
686
687         /* fix for single-lun devices */
688         if (us->fflags & US_FL_SINGLE_LUN)
689                 us->max_lun = 0;
690         return 0;
691 }
692
693 /* Get the protocol settings */
694 static int get_protocol(struct us_data *us)
695 {
696         switch (us->subclass) {
697         case US_SC_RBC:
698                 us->protocol_name = "Reduced Block Commands (RBC)";
699                 us->proto_handler = usb_stor_transparent_scsi_command;
700                 break;
701
702         case US_SC_8020:
703                 us->protocol_name = "8020i";
704                 us->proto_handler = usb_stor_pad12_command;
705                 us->max_lun = 0;
706                 break;
707
708         case US_SC_QIC:
709                 us->protocol_name = "QIC-157";
710                 us->proto_handler = usb_stor_pad12_command;
711                 us->max_lun = 0;
712                 break;
713
714         case US_SC_8070:
715                 us->protocol_name = "8070i";
716                 us->proto_handler = usb_stor_pad12_command;
717                 us->max_lun = 0;
718                 break;
719
720         case US_SC_SCSI:
721                 us->protocol_name = "Transparent SCSI";
722                 us->proto_handler = usb_stor_transparent_scsi_command;
723                 break;
724
725         case US_SC_UFI:
726                 us->protocol_name = "Uniform Floppy Interface (UFI)";
727                 us->proto_handler = usb_stor_ufi_command;
728                 break;
729
730 #ifdef CONFIG_USB_STORAGE_ISD200
731         case US_SC_ISD200:
732                 us->protocol_name = "ISD200 ATA/ATAPI";
733                 us->proto_handler = isd200_ata_command;
734                 break;
735 #endif
736
737 #ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
738         case US_SC_CYP_ATACB:
739                 us->protocol_name = "Transparent SCSI with Cypress ATACB";
740                 us->proto_handler = cypress_atacb_passthrough;
741                 break;
742 #endif
743
744         default:
745                 return -EIO;
746         }
747         US_DEBUGP("Protocol: %s\n", us->protocol_name);
748         return 0;
749 }
750
751 /* Get the pipe settings */
752 static int get_pipes(struct us_data *us)
753 {
754         struct usb_host_interface *altsetting =
755                 us->pusb_intf->cur_altsetting;
756         int i;
757         struct usb_endpoint_descriptor *ep;
758         struct usb_endpoint_descriptor *ep_in = NULL;
759         struct usb_endpoint_descriptor *ep_out = NULL;
760         struct usb_endpoint_descriptor *ep_int = NULL;
761
762         /*
763          * Find the first endpoint of each type we need.
764          * We are expecting a minimum of 2 endpoints - in and out (bulk).
765          * An optional interrupt-in is OK (necessary for CBI protocol).
766          * We will ignore any others.
767          */
768         for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
769                 ep = &altsetting->endpoint[i].desc;
770
771                 if (usb_endpoint_xfer_bulk(ep)) {
772                         if (usb_endpoint_dir_in(ep)) {
773                                 if (!ep_in)
774                                         ep_in = ep;
775                         } else {
776                                 if (!ep_out)
777                                         ep_out = ep;
778                         }
779                 }
780
781                 else if (usb_endpoint_is_int_in(ep)) {
782                         if (!ep_int)
783                                 ep_int = ep;
784                 }
785         }
786
787         if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
788                 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
789                 return -EIO;
790         }
791
792         /* Calculate and store the pipe values */
793         us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
794         us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
795         us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
796                 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
797         us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev, 
798                 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
799         if (ep_int) {
800                 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
801                         ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
802                 us->ep_bInterval = ep_int->bInterval;
803         }
804         return 0;
805 }
806
807 /* Initialize all the dynamic resources we need */
808 static int usb_stor_acquire_resources(struct us_data *us)
809 {
810         int p;
811         struct task_struct *th;
812
813         us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
814         if (!us->current_urb) {
815                 US_DEBUGP("URB allocation failed\n");
816                 return -ENOMEM;
817         }
818
819         /* Just before we start our control thread, initialize
820          * the device if it needs initialization */
821         if (us->unusual_dev->initFunction) {
822                 p = us->unusual_dev->initFunction(us);
823                 if (p)
824                         return p;
825         }
826
827         /* Start up our control thread */
828         th = kthread_run(usb_stor_control_thread, us, "usb-storage");
829         if (IS_ERR(th)) {
830                 printk(KERN_WARNING USB_STORAGE 
831                        "Unable to start control thread\n");
832                 return PTR_ERR(th);
833         }
834         us->ctl_thread = th;
835
836         return 0;
837 }
838
839 /* Release all our dynamic resources */
840 static void usb_stor_release_resources(struct us_data *us)
841 {
842         US_DEBUGP("-- %s\n", __func__);
843
844         /* Tell the control thread to exit.  The SCSI host must
845          * already have been removed and the DISCONNECTING flag set
846          * so that we won't accept any more commands.
847          */
848         US_DEBUGP("-- sending exit command to thread\n");
849         complete(&us->cmnd_ready);
850         if (us->ctl_thread)
851                 kthread_stop(us->ctl_thread);
852
853         /* Call the destructor routine, if it exists */
854         if (us->extra_destructor) {
855                 US_DEBUGP("-- calling extra_destructor()\n");
856                 us->extra_destructor(us->extra);
857         }
858
859         /* Free the extra data and the URB */
860         kfree(us->extra);
861         usb_free_urb(us->current_urb);
862 }
863
864 /* Dissociate from the USB device */
865 static void dissociate_dev(struct us_data *us)
866 {
867         US_DEBUGP("-- %s\n", __func__);
868
869         /* Free the device-related DMA-mapped buffers */
870         if (us->cr)
871                 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
872                                 us->cr_dma);
873         if (us->iobuf)
874                 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
875                                 us->iobuf_dma);
876
877         /* Remove our private data from the interface */
878         usb_set_intfdata(us->pusb_intf, NULL);
879 }
880
881 /* First stage of disconnect processing: stop SCSI scanning,
882  * remove the host, and stop accepting new commands
883  */
884 static void quiesce_and_remove_host(struct us_data *us)
885 {
886         struct Scsi_Host *host = us_to_host(us);
887
888         /* If the device is really gone, cut short reset delays */
889         if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
890                 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
891
892         /* Prevent SCSI-scanning (if it hasn't started yet)
893          * and wait for the SCSI-scanning thread to stop.
894          */
895         set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
896         wake_up(&us->delay_wait);
897         wait_for_completion(&us->scanning_done);
898
899         /* Removing the host will perform an orderly shutdown: caches
900          * synchronized, disks spun down, etc.
901          */
902         scsi_remove_host(host);
903
904         /* Prevent any new commands from being accepted and cut short
905          * reset delays.
906          */
907         scsi_lock(host);
908         set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
909         scsi_unlock(host);
910         wake_up(&us->delay_wait);
911 }
912
913 /* Second stage of disconnect processing: deallocate all resources */
914 static void release_everything(struct us_data *us)
915 {
916         usb_stor_release_resources(us);
917         dissociate_dev(us);
918
919         /* Drop our reference to the host; the SCSI core will free it
920          * (and "us" along with it) when the refcount becomes 0. */
921         scsi_host_put(us_to_host(us));
922 }
923
924 /* Thread to carry out delayed SCSI-device scanning */
925 static int usb_stor_scan_thread(void * __us)
926 {
927         struct us_data *us = (struct us_data *)__us;
928
929         printk(KERN_DEBUG
930                 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
931
932         set_freezable();
933         /* Wait for the timeout to expire or for a disconnect */
934         if (delay_use > 0) {
935                 printk(KERN_DEBUG "usb-storage: waiting for device "
936                                 "to settle before scanning\n");
937                 wait_event_freezable_timeout(us->delay_wait,
938                                 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
939                                 delay_use * HZ);
940         }
941
942         /* If the device is still connected, perform the scanning */
943         if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
944
945                 /* For bulk-only devices, determine the max LUN value */
946                 if (us->protocol == US_PR_BULK &&
947                                 !(us->fflags & US_FL_SINGLE_LUN)) {
948                         mutex_lock(&us->dev_mutex);
949                         us->max_lun = usb_stor_Bulk_max_lun(us);
950                         mutex_unlock(&us->dev_mutex);
951                 }
952                 scsi_scan_host(us_to_host(us));
953                 printk(KERN_DEBUG "usb-storage: device scan complete\n");
954
955                 /* Should we unbind if no devices were detected? */
956         }
957
958         complete_and_exit(&us->scanning_done, 0);
959 }
960
961
962 /* Probe to see if we can drive a newly-connected USB device */
963 static int storage_probe(struct usb_interface *intf,
964                          const struct usb_device_id *id)
965 {
966         struct Scsi_Host *host;
967         struct us_data *us;
968         int result;
969         struct task_struct *th;
970
971         if (usb_usual_check_type(id, USB_US_TYPE_STOR))
972                 return -ENXIO;
973
974         US_DEBUGP("USB Mass Storage device detected\n");
975
976         /*
977          * Ask the SCSI layer to allocate a host structure, with extra
978          * space at the end for our private us_data structure.
979          */
980         host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
981         if (!host) {
982                 printk(KERN_WARNING USB_STORAGE
983                         "Unable to allocate the scsi host\n");
984                 return -ENOMEM;
985         }
986
987         /*
988          * Allow 16-byte CDBs and thus > 2TB
989          */
990         host->max_cmd_len = 16;
991         us = host_to_us(host);
992         memset(us, 0, sizeof(struct us_data));
993         mutex_init(&(us->dev_mutex));
994         init_completion(&us->cmnd_ready);
995         init_completion(&(us->notify));
996         init_waitqueue_head(&us->delay_wait);
997         init_completion(&us->scanning_done);
998
999         /* Associate the us_data structure with the USB device */
1000         result = associate_dev(us, intf);
1001         if (result)
1002                 goto BadDevice;
1003
1004         /*
1005          * Get the unusual_devs entries and the descriptors
1006          *
1007          * id_index is calculated in the declaration to be the index number
1008          * of the match from the usb_device_id table, so we can find the
1009          * corresponding entry in the private table.
1010          */
1011         result = get_device_info(us, id);
1012         if (result)
1013                 goto BadDevice;
1014
1015         /* Get the transport, protocol, and pipe settings */
1016         result = get_transport(us);
1017         if (result)
1018                 goto BadDevice;
1019         result = get_protocol(us);
1020         if (result)
1021                 goto BadDevice;
1022         result = get_pipes(us);
1023         if (result)
1024                 goto BadDevice;
1025
1026         /* Acquire all the other resources and add the host */
1027         result = usb_stor_acquire_resources(us);
1028         if (result)
1029                 goto BadDevice;
1030         result = scsi_add_host(host, &intf->dev);
1031         if (result) {
1032                 printk(KERN_WARNING USB_STORAGE
1033                         "Unable to add the scsi host\n");
1034                 goto BadDevice;
1035         }
1036
1037         /* Start up the thread for delayed SCSI-device scanning */
1038         th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
1039         if (IS_ERR(th)) {
1040                 printk(KERN_WARNING USB_STORAGE 
1041                        "Unable to start the device-scanning thread\n");
1042                 complete(&us->scanning_done);
1043                 quiesce_and_remove_host(us);
1044                 result = PTR_ERR(th);
1045                 goto BadDevice;
1046         }
1047
1048         wake_up_process(th);
1049
1050         return 0;
1051
1052         /* We come here if there are any problems */
1053 BadDevice:
1054         US_DEBUGP("storage_probe() failed\n");
1055         release_everything(us);
1056         return result;
1057 }
1058
1059 /* Handle a disconnect event from the USB core */
1060 static void storage_disconnect(struct usb_interface *intf)
1061 {
1062         struct us_data *us = usb_get_intfdata(intf);
1063
1064         US_DEBUGP("storage_disconnect() called\n");
1065         quiesce_and_remove_host(us);
1066         release_everything(us);
1067 }
1068
1069 /***********************************************************************
1070  * Initialization and registration
1071  ***********************************************************************/
1072
1073 static struct usb_driver usb_storage_driver = {
1074         .name =         "usb-storage",
1075         .probe =        storage_probe,
1076         .disconnect =   storage_disconnect,
1077 #ifdef CONFIG_PM
1078         .suspend =      storage_suspend,
1079         .resume =       storage_resume,
1080         .reset_resume = storage_reset_resume,
1081 #endif
1082         .pre_reset =    storage_pre_reset,
1083         .post_reset =   storage_post_reset,
1084         .id_table =     storage_usb_ids,
1085         .soft_unbind =  1,
1086 };
1087
1088 /* Works only for digits and letters, but small and fast */
1089 #define TOLOWER(x) ((x) | 0x20)
1090
1091 static void __init parse_quirks(void)
1092 {
1093         int n, i;
1094         char *p;
1095
1096         if (!quirks)
1097                 return;
1098
1099         /* Count the ':' characters to get 2 * the number of entries */
1100         n = 0;
1101         for (p = quirks; *p; ++p) {
1102                 if (*p == ':')
1103                         ++n;
1104         }
1105         n /= 2;
1106         if (n == 0)
1107                 return;         /* Don't allocate 0 bytes */
1108
1109         quirks_list = kmalloc(n * sizeof(*quirks_list), GFP_KERNEL);
1110         if (!quirks_list)
1111                 return;
1112
1113         p = quirks;
1114         quirks_end = quirks_list;
1115         for (i = 0; i < n && *p; ++i) {
1116                 unsigned f = 0;
1117
1118                 /* Each entry consists of VID:PID:flags */
1119                 quirks_end->vid = simple_strtoul(p, &p, 16);
1120                 if (*p != ':')
1121                         goto skip_to_next;
1122                 quirks_end->pid = simple_strtoul(p+1, &p, 16);
1123                 if (*p != ':')
1124                         goto skip_to_next;
1125
1126                 while (*++p && *p != ',') {
1127                         switch (TOLOWER(*p)) {
1128                         case 'c':
1129                                 f |= US_FL_FIX_CAPACITY;
1130                                 break;
1131                         case 'i':
1132                                 f |= US_FL_IGNORE_DEVICE;
1133                                 break;
1134                         case 'l':
1135                                 f |= US_FL_NOT_LOCKABLE;
1136                                 break;
1137                         case 'm':
1138                                 f |= US_FL_MAX_SECTORS_64;
1139                                 break;
1140                         case 'r':
1141                                 f |= US_FL_IGNORE_RESIDUE;
1142                                 break;
1143                         case 's':
1144                                 f |= US_FL_SINGLE_LUN;
1145                                 break;
1146                         case 'w':
1147                                 f |= US_FL_NO_WP_DETECT;
1148                                 break;
1149                         /* Ignore unrecognized flag characters */
1150                         }
1151                 }
1152                 quirks_end->fflags = f;
1153                 ++quirks_end;
1154
1155  skip_to_next:
1156                 /* Entries are separated by commas */
1157                 while (*p) {
1158                         if (*p++ == ',')
1159                                 break;
1160                 }
1161         } /* for (i = 0; ...) */
1162 }
1163
1164 static int __init usb_stor_init(void)
1165 {
1166         int retval;
1167
1168         printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1169         parse_quirks();
1170
1171         /* register the driver, return usb_register return code if error */
1172         retval = usb_register(&usb_storage_driver);
1173         if (retval == 0) {
1174                 printk(KERN_INFO "USB Mass Storage support registered.\n");
1175                 usb_usual_set_present(USB_US_TYPE_STOR);
1176         }
1177         return retval;
1178 }
1179
1180 static void __exit usb_stor_exit(void)
1181 {
1182         US_DEBUGP("usb_stor_exit() called\n");
1183
1184         /* Deregister the driver
1185          * This will cause disconnect() to be called for each
1186          * attached unit
1187          */
1188         US_DEBUGP("-- calling usb_deregister()\n");
1189         usb_deregister(&usb_storage_driver) ;
1190
1191         usb_usual_clear_present(USB_US_TYPE_STOR);
1192 }
1193
1194 module_init(usb_stor_init);
1195 module_exit(usb_stor_exit);