- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / usb / storage / usb.c
blob6c254c7014da16e93c170e492fe7b7063dc88709
1 /* Driver for USB Mass Storage compliant devices
3 * $Id: usb.c,v 1.57 2000/11/21 02:56:41 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * Developed with the assistance of:
9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
11 * Initial work by:
12 * (c) 1999 Michael Gee (michael@linuxspecific.com)
14 * This driver is based on the 'USB Mass Storage Class' document. This
15 * describes in detail the protocol used to communicate with such
16 * devices. Clearly, the designers had SCSI and ATAPI commands in
17 * mind when they created this document. The commands are all very
18 * similar to commands in the SCSI-II and ATAPI specifications.
20 * It is important to note that in a number of cases this class
21 * exhibits class-specific exemptions from the USB specification.
22 * Notably the usage of NAK, STALL and ACK differs from the norm, in
23 * that they are used to communicate wait, failed and OK on commands.
25 * Also, for certain devices, the interrupt endpoint is used to convey
26 * status of a command.
28 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
29 * information about this driver.
31 * This program is free software; you can redistribute it and/or modify it
32 * under the terms of the GNU General Public License as published by the
33 * Free Software Foundation; either version 2, or (at your option) any
34 * later version.
36 * This program is distributed in the hope that it will be useful, but
37 * WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * General Public License for more details.
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, write to the Free Software Foundation, Inc.,
43 * 675 Mass Ave, Cambridge, MA 02139, USA.
46 #include <linux/config.h>
47 #include "usb.h"
48 #include "scsiglue.h"
49 #include "transport.h"
50 #include "protocol.h"
51 #include "debug.h"
52 #include "initializers.h"
53 #ifdef CONFIG_USB_STORAGE_HP8200e
54 #include "shuttle_usbat.h"
55 #endif
56 #ifdef CONFIG_USB_STORAGE_SDDR09
57 #include "sddr09.h"
58 #endif
59 #ifdef CONFIG_USB_STORAGE_DPCM
60 #include "dpcm.h"
61 #endif
62 #ifdef CONFIG_USB_STORAGE_FREECOM
63 #include "freecom.h"
64 #endif
66 #include <linux/module.h>
67 #include <linux/sched.h>
68 #include <linux/errno.h>
69 #include <linux/init.h>
70 #include <linux/malloc.h>
72 /* Some informational data */
73 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
74 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
77 * Per device data
80 static int my_host_number;
83 * kernel thread actions
86 #define US_ACT_COMMAND 1
87 #define US_ACT_DEVICE_RESET 2
88 #define US_ACT_BUS_RESET 3
89 #define US_ACT_HOST_RESET 4
90 #define US_ACT_EXIT 5
92 /* The list of structures and the protective lock for them */
93 struct us_data *us_list;
94 struct semaphore us_list_semaphore;
96 static void * storage_probe(struct usb_device *dev, unsigned int ifnum,
97 const struct usb_device_id *id);
98 static void storage_disconnect(struct usb_device *dev, void *ptr);
99 struct usb_driver usb_storage_driver = {
100 name: "usb-storage",
101 probe: storage_probe,
102 disconnect: storage_disconnect,
106 * fill_inquiry_response takes an unsigned char array (which must
107 * be at least 36 characters) and populates the vendor name,
108 * product name, and revision fields. Then the array is copied
109 * into the SCSI command's response buffer (oddly enough
110 * called request_buffer). data_len contains the length of the
111 * data array, which again must be at least 36.
114 void fill_inquiry_response(struct us_data *us, unsigned char *data,
115 unsigned int data_len) {
117 int i;
118 struct scatterlist *sg;
119 int len =
120 us->srb->request_bufflen > data_len ? data_len :
121 us->srb->request_bufflen;
122 int transferred;
123 int amt;
125 if (data_len<36) // You lose.
126 return;
128 memcpy(data+8, us->unusual_dev->vendorName,
129 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
130 strlen(us->unusual_dev->vendorName));
131 memcpy(data+16, us->unusual_dev->productName,
132 strlen(us->unusual_dev->productName) > 16 ? 16 :
133 strlen(us->unusual_dev->productName));
134 data[32] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>12) & 0x0F);
135 data[33] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>8) & 0x0F);
136 data[34] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>4) & 0x0F);
137 data[35] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice) & 0x0F);
139 if (us->srb->use_sg) {
140 sg = (struct scatterlist *)us->srb->request_buffer;
141 for (i=0; i<us->srb->use_sg; i++)
142 memset(sg[i].address, 0, sg[i].length);
143 for (i=0, transferred=0;
144 i<us->srb->use_sg && transferred < len;
145 i++) {
146 amt = sg[i].length > len-transferred ?
147 len-transferred : sg[i].length;
148 memcpy(sg[i].address, data+transferred, amt);
149 transferred -= amt;
151 } else {
152 memset(us->srb->request_buffer, 0, us->srb->request_bufflen);
153 memcpy(us->srb->request_buffer, data, len);
157 static int usb_stor_control_thread(void * __us)
159 wait_queue_t wait;
160 struct us_data *us = (struct us_data *)__us;
161 int action;
163 lock_kernel();
166 * This thread doesn't need any user-level access,
167 * so get rid of all our resources..
169 exit_files(current);
170 current->files = init_task.files;
171 atomic_inc(&current->files->count);
172 daemonize();
174 /* set our name for identification purposes */
175 sprintf(current->comm, "usb-storage-%d", us->host_number);
177 unlock_kernel();
179 /* set up for wakeups by new commands */
180 init_waitqueue_entry(&wait, current);
181 init_waitqueue_head(&(us->wqh));
182 add_wait_queue(&(us->wqh), &wait);
184 /* signal that we've started the thread */
185 up(&(us->notify));
186 set_current_state(TASK_INTERRUPTIBLE);
188 for(;;) {
189 US_DEBUGP("*** thread sleeping.\n");
190 schedule();
191 US_DEBUGP("*** thread awakened.\n");
193 /* lock access to the queue element */
194 down(&(us->queue_exclusion));
196 /* take the command off the queue */
197 action = us->action;
198 us->action = 0;
199 us->srb = us->queue_srb;
201 /* release the queue lock as fast as possible */
202 up(&(us->queue_exclusion));
204 switch (action) {
205 case US_ACT_COMMAND:
206 /* reject the command if the direction indicator
207 * is UNKNOWN
209 if (us->srb->sc_data_direction == SCSI_DATA_UNKNOWN) {
210 US_DEBUGP("UNKNOWN data direction\n");
211 us->srb->result = DID_ERROR << 16;
212 set_current_state(TASK_INTERRUPTIBLE);
213 us->srb->scsi_done(us->srb);
214 us->srb = NULL;
215 break;
218 /* reject if target != 0 or if LUN is higher than
219 * the maximum known LUN
221 if (us->srb->target &&
222 !(us->flags & US_FL_SCM_MULT_TARG)) {
223 US_DEBUGP("Bad target number (%d/%d)\n",
224 us->srb->target, us->srb->lun);
225 us->srb->result = DID_BAD_TARGET << 16;
227 set_current_state(TASK_INTERRUPTIBLE);
228 us->srb->scsi_done(us->srb);
229 us->srb = NULL;
230 break;
233 if (us->srb->lun > us->max_lun) {
234 US_DEBUGP("Bad LUN (%d/%d)\n",
235 us->srb->target, us->srb->lun);
236 us->srb->result = DID_BAD_TARGET << 16;
238 set_current_state(TASK_INTERRUPTIBLE);
239 us->srb->scsi_done(us->srb);
240 us->srb = NULL;
241 break;
244 /* handle those devices which can't do a START_STOP */
245 if ((us->srb->cmnd[0] == START_STOP) &&
246 (us->flags & US_FL_START_STOP)) {
247 US_DEBUGP("Skipping START_STOP command\n");
248 us->srb->result = GOOD << 1;
250 set_current_state(TASK_INTERRUPTIBLE);
251 us->srb->scsi_done(us->srb);
252 us->srb = NULL;
253 break;
256 /* lock the device pointers */
257 down(&(us->dev_semaphore));
259 /* our device has gone - pretend not ready */
260 if (!us->pusb_dev) {
261 US_DEBUGP("Request is for removed device\n");
262 /* For REQUEST_SENSE, it's the data. But
263 * for anything else, it should look like
264 * we auto-sensed for it.
266 if (us->srb->cmnd[0] == REQUEST_SENSE) {
267 memcpy(us->srb->request_buffer,
268 usb_stor_sense_notready,
269 sizeof(usb_stor_sense_notready));
270 us->srb->result = GOOD << 1;
271 } else {
272 memcpy(us->srb->sense_buffer,
273 usb_stor_sense_notready,
274 sizeof(usb_stor_sense_notready));
275 us->srb->result = CHECK_CONDITION << 1;
277 } else { /* !us->pusb_dev */
278 /* we've got a command, let's do it! */
279 US_DEBUG(usb_stor_show_command(us->srb));
280 us->proto_handler(us->srb, us);
283 /* unlock the device pointers */
284 up(&(us->dev_semaphore));
286 /* indicate that the command is done */
287 if (us->srb->result != DID_ABORT << 16) {
288 US_DEBUGP("scsi cmd done, result=0x%x\n",
289 us->srb->result);
290 set_current_state(TASK_INTERRUPTIBLE);
291 us->srb->scsi_done(us->srb);
292 } else {
293 US_DEBUGP("scsi command aborted\n");
294 set_current_state(TASK_INTERRUPTIBLE);
295 up(&(us->notify));
297 us->srb = NULL;
298 break;
300 case US_ACT_DEVICE_RESET:
301 break;
303 case US_ACT_BUS_RESET:
304 break;
306 case US_ACT_HOST_RESET:
307 break;
309 } /* end switch on action */
311 /* exit if we get a signal to exit */
312 if (action == US_ACT_EXIT) {
313 US_DEBUGP("-- US_ACT_EXIT command recieved\n");
314 break;
316 } /* for (;;) */
318 /* clean up after ourselves */
319 set_current_state(TASK_INTERRUPTIBLE);
320 remove_wait_queue(&(us->wqh), &wait);
322 /* notify the exit routine that we're actually exiting now */
323 up(&(us->notify));
325 return 0;
328 /* This is the list of devices we recognize, along with their flag data */
330 /* The vendor name should be kept at eight characters or less, and
331 * the product name should be kept at 16 characters or less. If a device
332 * has the US_FL_DUMMY_INQUIRY flag, then the vendor and product names
333 * normally generated by a device thorugh the INQUIRY response will be
334 * taken from this list, and this is the reason for the above size
335 * restriction. However, if the flag is not present, then you
336 * are free to use as many characters as you like.
338 static struct us_unusual_dev us_unusual_dev_list[] = {
340 { 0x03ee, 0x0000, 0x0000, 0x0245,
341 "Mitsumi",
342 "CD-R/RW Drive",
343 US_SC_8020, US_PR_CBI, NULL, 0},
345 { 0x03f0, 0x0107, 0x0200, 0x0200,
346 "HP",
347 "CD-Writer+",
348 US_SC_8070, US_PR_CB, NULL, 0},
350 #ifdef CONFIG_USB_STORAGE_HP8200e
351 { 0x03f0, 0x0207, 0x0001, 0x0001,
352 "HP",
353 "CD-Writer+ 8200e",
354 US_SC_8070, US_PR_SCM_ATAPI, init_8200e, 0},
355 #endif
357 { 0x04e6, 0x0001, 0x0200, 0x0200,
358 "Matshita",
359 "LS-120",
360 US_SC_8020, US_PR_CB, NULL, 0},
362 { 0x04e6, 0x0002, 0x0100, 0x0100,
363 "Shuttle",
364 "eUSCSI Bridge",
365 US_SC_SCSI, US_PR_BULK, usb_stor_euscsi_init,
366 US_FL_SCM_MULT_TARG },
368 #ifdef CONFIG_USB_STORAGE_SDDR09
369 { 0x04e6, 0x0003, 0x0000, 0x9999,
370 "Sandisk",
371 "ImageMate SDDR09",
372 US_SC_SCSI, US_PR_EUSB_SDDR09, NULL,
373 US_FL_SINGLE_LUN | US_FL_START_STOP },
374 #endif
376 #ifdef CONFIG_USB_STORAGE_DPCM
377 { 0x0436, 0x0005, 0x0100, 0x0100,
378 "Microtech",
379 "CameraMate (DPCM_USB)",
380 US_SC_SCSI, US_PR_DPCM_USB, NULL,
381 US_FL_START_STOP },
382 #endif
384 { 0x04e6, 0x0006, 0x0100, 0x0200,
385 "Shuttle",
386 "eUSB MMC Adapter",
387 US_SC_SCSI, US_PR_CB, NULL,
388 US_FL_SINGLE_LUN},
390 { 0x04e6, 0x0007, 0x0100, 0x0200,
391 "Sony",
392 "Hifd",
393 US_SC_SCSI, US_PR_CB, NULL,
394 US_FL_SINGLE_LUN},
396 { 0x04e6, 0x0009, 0x0200, 0x0200,
397 "Shuttle",
398 "eUSB ATA/ATAPI Adapter",
399 US_SC_8020, US_PR_CB, NULL, 0},
401 { 0x04e6, 0x000a, 0x0200, 0x0200,
402 "Shuttle",
403 "eUSB CompactFlash Adapter",
404 US_SC_8020, US_PR_CB, NULL, 0},
406 { 0x04e6, 0x000B, 0x0100, 0x0100,
407 "Shuttle",
408 "eUSCSI Bridge",
409 US_SC_SCSI, US_PR_BULK, usb_stor_euscsi_init,
410 US_FL_SCM_MULT_TARG },
412 { 0x04e6, 0x000C, 0x0100, 0x0100,
413 "Shuttle",
414 "eUSCSI Bridge",
415 US_SC_SCSI, US_PR_BULK, usb_stor_euscsi_init,
416 US_FL_SCM_MULT_TARG },
418 { 0x04e6, 0x0101, 0x0200, 0x0200,
419 "Shuttle",
420 "CD-RW Device",
421 US_SC_8020, US_PR_CB, NULL, 0},
423 { 0x054c, 0x0010, 0x0106, 0x0210,
424 "Sony",
425 "DSC-S30/S70/505V/F505",
426 US_SC_SCSI, US_PR_CB, NULL,
427 US_FL_SINGLE_LUN | US_FL_START_STOP | US_FL_MODE_XLATE },
429 { 0x054c, 0x002d, 0x0100, 0x0100,
430 "Sony",
431 "Memorystick MSAC-US1",
432 US_SC_UFI, US_PR_CB, NULL,
433 US_FL_SINGLE_LUN | US_FL_START_STOP },
435 { 0x057b, 0x0000, 0x0000, 0x0299,
436 "Y-E Data",
437 "Flashbuster-U",
438 US_SC_UFI, US_PR_CB, NULL,
439 US_FL_SINGLE_LUN},
441 { 0x057b, 0x0000, 0x0300, 0x9999,
442 "Y-E Data",
443 "Flashbuster-U",
444 US_SC_UFI, US_PR_CBI, NULL,
445 US_FL_SINGLE_LUN},
447 { 0x059f, 0xa601, 0x0200, 0x0200,
448 "LaCie",
449 "USB Hard Disk",
450 US_SC_RBC, US_PR_CB, NULL, 0 },
452 { 0x05ab, 0x0031, 0x0100, 0x0100,
453 "In-System",
454 "USB/IDE Bridge (ATAPI ONLY!)",
455 US_SC_8070, US_PR_BULK, NULL, 0 },
457 { 0x0644, 0x0000, 0x0100, 0x0100,
458 "TEAC",
459 "Floppy Drive",
460 US_SC_UFI, US_PR_CB, NULL, 0 },
462 #ifdef CONFIG_USB_STORAGE_SDDR09
463 { 0x066b, 0x0105, 0x0100, 0x0100,
464 "Olympus",
465 "Camedia MAUSB-2",
466 US_SC_SCSI, US_PR_EUSB_SDDR09, NULL,
467 US_FL_SINGLE_LUN | US_FL_START_STOP },
468 #endif
470 { 0x0693, 0x0002, 0x0100, 0x0100,
471 "Hagiwara",
472 "FlashGate SmartMedia",
473 US_SC_SCSI, US_PR_BULK, NULL, 0 },
475 { 0x0693, 0x0005, 0x0100, 0x0100,
476 "Hagiwara",
477 "Flashgate",
478 US_SC_SCSI, US_PR_BULK, NULL, 0 },
480 { 0x0781, 0x0001, 0x0200, 0x0200,
481 "Sandisk",
482 "ImageMate SDDR-05a",
483 US_SC_SCSI, US_PR_CB, NULL,
484 US_FL_SINGLE_LUN | US_FL_START_STOP},
486 { 0x0781, 0x0100, 0x0100, 0x0100,
487 "Sandisk",
488 "ImageMate SDDR-12",
489 US_SC_SCSI, US_PR_CB, NULL,
490 US_FL_SINGLE_LUN },
492 #ifdef CONFIG_USB_STORAGE_SDDR09
493 { 0x0781, 0x0200, 0x0100, 0x0208,
494 "Sandisk",
495 "ImageMate SDDR-09",
496 US_SC_SCSI, US_PR_EUSB_SDDR09, NULL,
497 US_FL_SINGLE_LUN | US_FL_START_STOP },
498 #endif
500 { 0x0781, 0x0002, 0x0009, 0x0009,
501 "Sandisk",
502 "ImageMate SDDR-31",
503 US_SC_SCSI, US_PR_BULK, NULL,
504 US_FL_IGNORE_SER},
506 { 0x07af, 0x0004, 0x0100, 0x0100,
507 "Microtech",
508 "USB-SCSI-DB25",
509 US_SC_SCSI, US_PR_BULK, usb_stor_euscsi_init,
510 US_FL_SCM_MULT_TARG },
512 #ifdef CONFIG_USB_STORAGE_FREECOM
513 { 0x07ab, 0xfc01, 0x0000, 0x9999,
514 "Freecom",
515 "USB-IDE",
516 US_SC_QIC, US_PR_FREECOM, freecom_init, 0},
517 #endif
519 { 0x07af, 0x0005, 0x0100, 0x0100,
520 "Microtech",
521 "USB-SCSI-HD50",
522 US_SC_SCSI, US_PR_BULK, usb_stor_euscsi_init,
523 US_FL_SCM_MULT_TARG },
525 #ifdef CONFIG_USB_STORAGE_DPCM
526 { 0x07af, 0x0006, 0x0100, 0x0100,
527 "Microtech",
528 "CameraMate (DPCM_USB)",
529 US_SC_SCSI, US_PR_DPCM_USB, NULL,
530 US_FL_START_STOP },
531 #endif
532 { 0 }
535 /* Search our ususual device list, based on vendor/product combinations
536 * to see if we can support this device. Returns a pointer to a structure
537 * defining how we should support this device, or NULL if it's not in the
538 * list
540 static struct us_unusual_dev* us_find_dev(u16 idVendor, u16 idProduct,
541 u16 bcdDevice)
543 struct us_unusual_dev* ptr;
545 US_DEBUGP("Searching unusual device list for (0x%x, 0x%x, 0x%x)...\n",
546 idVendor, idProduct, bcdDevice);
548 ptr = us_unusual_dev_list;
549 while ((ptr->idVendor != 0x0000) &&
550 !((ptr->idVendor == idVendor) &&
551 (ptr->idProduct == idProduct) &&
552 (ptr->bcdDeviceMin <= bcdDevice) &&
553 (ptr->bcdDeviceMax >= bcdDevice)))
554 ptr++;
556 /* if the search ended because we hit the end record, we failed */
557 if (ptr->idVendor == 0x0000) {
558 US_DEBUGP("-- did not find a matching device\n");
559 return NULL;
562 /* otherwise, we found one! */
563 US_DEBUGP("-- found matching device: %s %s\n", ptr->vendorName,
564 ptr->productName);
565 return ptr;
568 /* Set up the IRQ pipe and handler
569 * Note that this function assumes that all the data in the us_data
570 * strucuture is current. This includes the ep_int field, which gives us
571 * the endpoint for the interrupt.
572 * Returns non-zero on failure, zero on success
574 static int usb_stor_allocate_irq(struct us_data *ss)
576 unsigned int pipe;
577 int maxp;
578 int result;
580 US_DEBUGP("Allocating IRQ for CBI transport\n");
582 /* lock access to the data structure */
583 down(&(ss->irq_urb_sem));
585 /* allocate the URB */
586 ss->irq_urb = usb_alloc_urb(0);
587 if (!ss->irq_urb) {
588 up(&(ss->irq_urb_sem));
589 US_DEBUGP("couldn't allocate interrupt URB");
590 return 1;
593 /* calculate the pipe and max packet size */
594 pipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int->bEndpointAddress &
595 USB_ENDPOINT_NUMBER_MASK);
596 maxp = usb_maxpacket(ss->pusb_dev, pipe, usb_pipeout(pipe));
597 if (maxp > sizeof(ss->irqbuf))
598 maxp = sizeof(ss->irqbuf);
600 /* fill in the URB with our data */
601 FILL_INT_URB(ss->irq_urb, ss->pusb_dev, pipe, ss->irqbuf, maxp,
602 usb_stor_CBI_irq, ss, ss->ep_int->bInterval);
604 /* submit the URB for processing */
605 result = usb_submit_urb(ss->irq_urb);
606 US_DEBUGP("usb_submit_urb() returns %d\n", result);
607 if (result) {
608 usb_free_urb(ss->irq_urb);
609 up(&(ss->irq_urb_sem));
610 return 2;
613 /* unlock the data structure and return success */
614 up(&(ss->irq_urb_sem));
615 return 0;
618 /* Probe to see if a new device is actually a SCSI device */
619 static void * storage_probe(struct usb_device *dev, unsigned int ifnum,
620 const struct usb_device_id *id)
622 int i;
623 char mf[USB_STOR_STRING_LEN]; /* manufacturer */
624 char prod[USB_STOR_STRING_LEN]; /* product */
625 char serial[USB_STOR_STRING_LEN]; /* serial number */
626 GUID(guid); /* Global Unique Identifier */
627 unsigned int flags;
628 struct us_unusual_dev *unusual_dev;
629 struct us_data *ss = NULL;
630 #ifdef CONFIG_USB_STORAGE_SDDR09
631 int result;
632 #endif
634 /* these are temporary copies -- we test on these, then put them
635 * in the us-data structure
637 struct usb_endpoint_descriptor *ep_in = NULL;
638 struct usb_endpoint_descriptor *ep_out = NULL;
639 struct usb_endpoint_descriptor *ep_int = NULL;
640 u8 subclass = 0;
641 u8 protocol = 0;
643 /* the altsettting 0 on the interface we're probing */
644 struct usb_interface_descriptor *altsetting =
645 &(dev->actconfig->interface[ifnum].altsetting[0]);
647 /* clear the temporary strings */
648 memset(mf, 0, sizeof(mf));
649 memset(prod, 0, sizeof(prod));
650 memset(serial, 0, sizeof(serial));
652 /* search for this device in our unusual device list */
653 unusual_dev = us_find_dev(dev->descriptor.idVendor,
654 dev->descriptor.idProduct,
655 dev->descriptor.bcdDevice);
658 * Can we support this device, either because we know about it
659 * from our unusual device list, or because it advertises that it's
660 * compliant to the specification?
662 if (!unusual_dev &&
663 !(dev->descriptor.bDeviceClass == 0 &&
664 altsetting->bInterfaceClass == USB_CLASS_MASS_STORAGE &&
665 altsetting->bInterfaceSubClass >= US_SC_MIN &&
666 altsetting->bInterfaceSubClass <= US_SC_MAX)) {
667 /* if it's not a mass storage, we go no further */
668 return NULL;
671 /* At this point, we know we've got a live one */
672 US_DEBUGP("USB Mass Storage device detected\n");
674 /* Determine subclass and protocol, or copy from the interface */
675 if (unusual_dev) {
676 subclass = unusual_dev->useProtocol;
677 protocol = unusual_dev->useTransport;
678 flags = unusual_dev->flags;
679 } else {
680 subclass = altsetting->bInterfaceSubClass;
681 protocol = altsetting->bInterfaceProtocol;
682 flags = 0;
686 * Find the endpoints we need
687 * We are expecting a minimum of 2 endpoints - in and out (bulk).
688 * An optional interrupt is OK (necessary for CBI protocol).
689 * We will ignore any others.
691 for (i = 0; i < altsetting->bNumEndpoints; i++) {
692 /* is it an BULK endpoint? */
693 if ((altsetting->endpoint[i].bmAttributes &
694 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
695 /* BULK in or out? */
696 if (altsetting->endpoint[i].bEndpointAddress &
697 USB_DIR_IN)
698 ep_in = &altsetting->endpoint[i];
699 else
700 ep_out = &altsetting->endpoint[i];
703 /* is it an interrupt endpoint? */
704 if ((altsetting->endpoint[i].bmAttributes &
705 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
706 ep_int = &altsetting->endpoint[i];
709 US_DEBUGP("Endpoints: In: 0x%p Out: 0x%p Int: 0x%p (Period %d)\n",
710 ep_in, ep_out, ep_int, ep_int ? ep_int->bInterval : 0);
712 #ifdef CONFIG_USB_STORAGE_SDDR09
713 if (protocol == US_PR_EUSB_SDDR09 || protocol == US_PR_DPCM_USB) {
714 /* set the configuration -- STALL is an acceptable response here */
715 result = usb_set_configuration(dev, 1);
717 US_DEBUGP("Result from usb_set_configuration is %d\n", result);
718 if (result == -EPIPE) {
719 US_DEBUGP("-- clearing stall on control interface\n");
720 usb_clear_halt(dev, usb_sndctrlpipe(dev, 0));
721 } else if (result != 0) {
722 /* it's not a stall, but another error -- time to bail */
723 US_DEBUGP("-- Unknown error. Rejecting device\n");
724 return NULL;
727 #endif
729 /* Do some basic sanity checks, and bail if we find a problem */
730 if (!ep_in || !ep_out || (protocol == US_PR_CBI && !ep_int)) {
731 US_DEBUGP("Sanity check failed. Rejecting device.\n");
732 return NULL;
735 /* At this point, we're committed to using the device */
736 usb_inc_dev_use(dev);
738 /* clear the GUID and fetch the strings */
739 GUID_CLEAR(guid);
740 if (dev->descriptor.iManufacturer)
741 usb_string(dev, dev->descriptor.iManufacturer,
742 mf, sizeof(mf));
743 if (dev->descriptor.iProduct)
744 usb_string(dev, dev->descriptor.iProduct,
745 prod, sizeof(prod));
746 if (dev->descriptor.iSerialNumber && !(flags & US_FL_IGNORE_SER))
747 usb_string(dev, dev->descriptor.iSerialNumber,
748 serial, sizeof(serial));
750 /* Create a GUID for this device */
751 if (dev->descriptor.iSerialNumber && serial[0]) {
752 /* If we have a serial number, and it's a non-NULL string */
753 make_guid(guid, dev->descriptor.idVendor,
754 dev->descriptor.idProduct, serial);
755 } else {
756 /* We don't have a serial number, so we use 0 */
757 make_guid(guid, dev->descriptor.idVendor,
758 dev->descriptor.idProduct, "0");
762 * Now check if we have seen this GUID before
763 * We're looking for a device with a matching GUID that isn't
764 * already on the system
766 ss = us_list;
767 while ((ss != NULL) &&
768 ((ss->pusb_dev) || !GUID_EQUAL(guid, ss->guid)))
769 ss = ss->next;
771 if (ss != NULL) {
772 /* Existing device -- re-connect */
773 US_DEBUGP("Found existing GUID " GUID_FORMAT "\n",
774 GUID_ARGS(guid));
776 /* lock the device pointers */
777 down(&(ss->dev_semaphore));
779 /* establish the connection to the new device upon reconnect */
780 ss->ifnum = ifnum;
781 ss->pusb_dev = dev;
783 /* copy over the endpoint data */
784 if (ep_in)
785 ss->ep_in = ep_in->bEndpointAddress &
786 USB_ENDPOINT_NUMBER_MASK;
787 if (ep_out)
788 ss->ep_out = ep_out->bEndpointAddress &
789 USB_ENDPOINT_NUMBER_MASK;
790 ss->ep_int = ep_int;
792 /* allocate an IRQ callback if one is needed */
793 if ((ss->protocol == US_PR_CBI) && usb_stor_allocate_irq(ss)) {
794 usb_dec_dev_use(dev);
795 return NULL;
798 /* allocate the URB we're going to use */
799 ss->current_urb = usb_alloc_urb(0);
800 if (!ss->current_urb) {
801 usb_dec_dev_use(dev);
802 return NULL;
805 /* Re-Initialize the device if it needs it */
806 if (unusual_dev && unusual_dev->initFunction)
807 (unusual_dev->initFunction)(ss);
809 /* unlock the device pointers */
810 up(&(ss->dev_semaphore));
812 } else {
813 /* New device -- allocate memory and initialize */
814 US_DEBUGP("New GUID " GUID_FORMAT "\n", GUID_ARGS(guid));
816 if ((ss = (struct us_data *)kmalloc(sizeof(struct us_data),
817 GFP_KERNEL)) == NULL) {
818 printk(KERN_WARNING USB_STORAGE "Out of memory\n");
819 usb_dec_dev_use(dev);
820 return NULL;
822 memset(ss, 0, sizeof(struct us_data));
824 /* allocate the URB we're going to use */
825 ss->current_urb = usb_alloc_urb(0);
826 if (!ss->current_urb) {
827 kfree(ss);
828 usb_dec_dev_use(dev);
829 return NULL;
832 /* Initialize the mutexes only when the struct is new */
833 init_MUTEX_LOCKED(&(ss->notify));
834 init_MUTEX_LOCKED(&(ss->ip_waitq));
835 init_MUTEX(&(ss->queue_exclusion));
836 init_MUTEX(&(ss->irq_urb_sem));
837 init_MUTEX(&(ss->current_urb_sem));
838 init_MUTEX(&(ss->dev_semaphore));
840 /* copy over the subclass and protocol data */
841 ss->subclass = subclass;
842 ss->protocol = protocol;
843 ss->flags = flags;
844 ss->unusual_dev = unusual_dev;
846 /* copy over the endpoint data */
847 if (ep_in)
848 ss->ep_in = ep_in->bEndpointAddress &
849 USB_ENDPOINT_NUMBER_MASK;
850 if (ep_out)
851 ss->ep_out = ep_out->bEndpointAddress &
852 USB_ENDPOINT_NUMBER_MASK;
853 ss->ep_int = ep_int;
855 /* establish the connection to the new device */
856 ss->ifnum = ifnum;
857 ss->pusb_dev = dev;
859 /* copy over the identifiying strings */
860 strncpy(ss->vendor, mf, USB_STOR_STRING_LEN);
861 strncpy(ss->product, prod, USB_STOR_STRING_LEN);
862 strncpy(ss->serial, serial, USB_STOR_STRING_LEN);
863 if (strlen(ss->vendor) == 0) {
864 if (unusual_dev)
865 strncpy(ss->vendor, unusual_dev->vendorName,
866 USB_STOR_STRING_LEN);
867 else
868 strncpy(ss->vendor, "Unknown",
869 USB_STOR_STRING_LEN);
871 if (strlen(ss->product) == 0) {
872 if (unusual_dev)
873 strncpy(ss->product, unusual_dev->productName,
874 USB_STOR_STRING_LEN);
875 else
876 strncpy(ss->product, "Unknown",
877 USB_STOR_STRING_LEN);
879 if (strlen(ss->serial) == 0)
880 strncpy(ss->serial, "None", USB_STOR_STRING_LEN);
882 /* copy the GUID we created before */
883 memcpy(ss->guid, guid, sizeof(guid));
886 * Set the handler pointers based on the protocol
887 * Again, this data is persistant across reattachments
889 switch (ss->protocol) {
890 case US_PR_CB:
891 ss->transport_name = "Control/Bulk";
892 ss->transport = usb_stor_CB_transport;
893 ss->transport_reset = usb_stor_CB_reset;
894 ss->max_lun = 7;
895 break;
897 case US_PR_CBI:
898 ss->transport_name = "Control/Bulk/Interrupt";
899 ss->transport = usb_stor_CBI_transport;
900 ss->transport_reset = usb_stor_CB_reset;
901 ss->max_lun = 7;
902 break;
904 case US_PR_BULK:
905 ss->transport_name = "Bulk";
906 ss->transport = usb_stor_Bulk_transport;
907 ss->transport_reset = usb_stor_Bulk_reset;
908 ss->max_lun = usb_stor_Bulk_max_lun(ss);
909 break;
911 #ifdef CONFIG_USB_STORAGE_HP8200e
912 case US_PR_SCM_ATAPI:
913 ss->transport_name = "SCM/ATAPI";
914 ss->transport = hp8200e_transport;
915 ss->transport_reset = usb_stor_CB_reset;
916 ss->max_lun = 1;
917 break;
918 #endif
920 #ifdef CONFIG_USB_STORAGE_SDDR09
921 case US_PR_EUSB_SDDR09:
922 ss->transport_name = "EUSB/SDDR09";
923 ss->transport = sddr09_transport;
924 ss->transport_reset = usb_stor_CB_reset;
925 ss->max_lun = 0;
926 break;
927 #endif
929 #ifdef CONFIG_USB_STORAGE_DPCM
930 case US_PR_DPCM_USB:
931 ss->transport_name = "Control/Bulk-EUSB/SDDR09";
932 ss->transport = dpcm_transport;
933 ss->transport_reset = usb_stor_CB_reset;
934 ss->max_lun = 1;
935 break;
936 #endif
938 #ifdef CONFIG_USB_STORAGE_FREECOM
939 case US_PR_FREECOM:
940 ss->transport_name = "Freecom";
941 ss->transport = freecom_transport;
942 ss->transport_reset = usb_stor_freecom_reset;
943 ss->max_lun = 0;
944 break;
945 #endif
947 default:
948 ss->transport_name = "Unknown";
949 kfree(ss->current_urb);
950 kfree(ss);
951 usb_dec_dev_use(dev);
952 return NULL;
953 break;
955 US_DEBUGP("Transport: %s\n", ss->transport_name);
957 /* fix for single-lun devices */
958 if (ss->flags & US_FL_SINGLE_LUN)
959 ss->max_lun = 0;
961 switch (ss->subclass) {
962 case US_SC_RBC:
963 ss->protocol_name = "Reduced Block Commands (RBC)";
964 ss->proto_handler = usb_stor_transparent_scsi_command;
965 break;
967 case US_SC_8020:
968 ss->protocol_name = "8020i";
969 ss->proto_handler = usb_stor_ATAPI_command;
970 ss->max_lun = 0;
971 break;
973 case US_SC_QIC:
974 ss->protocol_name = "QIC-157";
975 ss->proto_handler = usb_stor_qic157_command;
976 ss->max_lun = 0;
977 break;
979 case US_SC_8070:
980 ss->protocol_name = "8070i";
981 ss->proto_handler = usb_stor_ATAPI_command;
982 ss->max_lun = 0;
983 break;
985 case US_SC_SCSI:
986 ss->protocol_name = "Transparent SCSI";
987 ss->proto_handler = usb_stor_transparent_scsi_command;
988 break;
990 case US_SC_UFI:
991 ss->protocol_name = "Uniform Floppy Interface (UFI)";
992 ss->proto_handler = usb_stor_ufi_command;
993 break;
995 default:
996 ss->protocol_name = "Unknown";
997 kfree(ss->current_urb);
998 kfree(ss);
999 return NULL;
1000 break;
1002 US_DEBUGP("Protocol: %s\n", ss->protocol_name);
1004 /* allocate an IRQ callback if one is needed */
1005 if ((ss->protocol == US_PR_CBI) && usb_stor_allocate_irq(ss)) {
1006 usb_dec_dev_use(dev);
1007 return NULL;
1011 * Since this is a new device, we need to generate a scsi
1012 * host definition, and register with the higher SCSI layers
1015 /* Initialize the host template based on the default one */
1016 memcpy(&(ss->htmplt), &usb_stor_host_template,
1017 sizeof(usb_stor_host_template));
1019 /* Grab the next host number */
1020 ss->host_number = my_host_number++;
1022 /* We abuse this pointer so we can pass the ss pointer to
1023 * the host controler thread in us_detect. But how else are
1024 * we to do it?
1026 (struct us_data *)ss->htmplt.proc_dir = ss;
1028 /* Just before we start our control thread, initialize
1029 * the device if it needs initialization */
1030 if (unusual_dev && unusual_dev->initFunction)
1031 unusual_dev->initFunction(ss);
1033 /* start up our control thread */
1034 ss->pid = kernel_thread(usb_stor_control_thread, ss,
1035 CLONE_VM);
1036 if (ss->pid < 0) {
1037 printk(KERN_WARNING USB_STORAGE
1038 "Unable to start control thread\n");
1039 kfree(ss->current_urb);
1040 kfree(ss);
1041 usb_dec_dev_use(dev);
1042 return NULL;
1045 /* wait for the thread to start */
1046 down(&(ss->notify));
1048 /* now register - our detect function will be called */
1049 ss->htmplt.module = THIS_MODULE;
1050 scsi_register_module(MODULE_SCSI_HA, &(ss->htmplt));
1052 /* lock access to the data structures */
1053 down(&us_list_semaphore);
1055 /* put us in the list */
1056 ss->next = us_list;
1057 us_list = ss;
1059 /* release the data structure lock */
1060 up(&us_list_semaphore);
1063 printk(KERN_DEBUG
1064 "WARNING: USB Mass Storage data integrity not assured\n");
1065 printk(KERN_DEBUG
1066 "USB Mass Storage device found at %d\n", dev->devnum);
1068 /* return a pointer for the disconnect function */
1069 return ss;
1072 /* Handle a disconnect event from the USB core */
1073 static void storage_disconnect(struct usb_device *dev, void *ptr)
1075 struct us_data *ss = ptr;
1076 int result;
1078 US_DEBUGP("storage_disconnect() called\n");
1080 /* this is the odd case -- we disconnected but weren't using it */
1081 if (!ss) {
1082 US_DEBUGP("-- device was not in use\n");
1083 return;
1086 /* lock access to the device data structure */
1087 down(&(ss->dev_semaphore));
1089 /* release the IRQ, if we have one */
1090 down(&(ss->irq_urb_sem));
1091 if (ss->irq_urb) {
1092 US_DEBUGP("-- releasing irq URB\n");
1093 result = usb_unlink_urb(ss->irq_urb);
1094 US_DEBUGP("-- usb_unlink_urb() returned %d\n", result);
1095 usb_free_urb(ss->irq_urb);
1096 ss->irq_urb = NULL;
1098 up(&(ss->irq_urb_sem));
1100 /* free up the main URB for this device */
1101 US_DEBUGP("-- releasing main URB\n");
1102 result = usb_unlink_urb(ss->current_urb);
1103 US_DEBUGP("-- usb_unlink_urb() returned %d\n", result);
1104 usb_free_urb(ss->current_urb);
1105 ss->current_urb = NULL;
1107 /* mark the device as gone */
1108 usb_dec_dev_use(ss->pusb_dev);
1109 ss->pusb_dev = NULL;
1111 /* unlock access to the device data structure */
1112 up(&(ss->dev_semaphore));
1115 /***********************************************************************
1116 * Initialization and registration
1117 ***********************************************************************/
1119 int __init usb_stor_init(void)
1121 /* initialize internal global data elements */
1122 us_list = NULL;
1123 init_MUTEX(&us_list_semaphore);
1124 my_host_number = 0;
1126 /* register the driver, return -1 if error */
1127 if (usb_register(&usb_storage_driver) < 0)
1128 return -1;
1130 /* we're all set */
1131 printk(KERN_INFO "USB Mass Storage support registered.\n");
1132 return 0;
1135 void __exit usb_stor_exit(void)
1137 struct us_data *next;
1139 US_DEBUGP("usb_stor_exit() called\n");
1141 /* Deregister the driver
1142 * This eliminates races with probes and disconnects
1144 US_DEBUGP("-- calling usb_deregister()\n");
1145 usb_deregister(&usb_storage_driver) ;
1147 /* While there are still virtual hosts, unregister them
1148 * Note that it's important to do this completely before removing
1149 * the structures because of possible races with the /proc
1150 * interface
1152 for (next = us_list; next; next = next->next) {
1153 US_DEBUGP("-- calling scsi_unregister_module()\n");
1154 scsi_unregister_module(MODULE_SCSI_HA, &(next->htmplt));
1157 /* While there are still structures, free them. Note that we are
1158 * now race-free, since these structures can no longer be accessed
1159 * from either the SCSI command layer or the /proc interface
1161 while (us_list) {
1162 /* keep track of where the next one is */
1163 next = us_list->next;
1165 /* If there's extra data in the us_data structure then
1166 * free that first */
1167 if (us_list->extra) {
1168 /* call the destructor routine, if it exists */
1169 if (us_list->extra_destructor) {
1170 US_DEBUGP("-- calling extra_destructor()\n");
1171 us_list->extra_destructor(us_list->extra);
1174 /* destroy the extra data */
1175 US_DEBUGP("-- freeing the data structure\n");
1176 kfree(us_list->extra);
1179 /* free the structure itself */
1180 kfree (us_list);
1182 /* advance the list pointer */
1183 us_list = next;
1187 module_init(usb_stor_init);
1188 module_exit(usb_stor_exit);