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