- pre5:
[davej-history.git] / drivers / usb / storage / usb.c
blobabf0bf1c8e9a9a14c894d00469dd1c4a10cd0585
1 /* Driver for USB Mass Storage compliant devices
3 * $Id: usb.c,v 1.33 2000/08/25 00:13:51 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 #ifdef CONFIG_USB_STORAGE_HP8200e
53 #include "shuttle_usbat.h"
54 #endif
55 #ifdef CONFIG_USB_STORAGE_SDDR09
56 #include "sddr09.h"
57 #endif
58 #ifdef CONFIG_USB_STORAGE_DPCM
59 #include "dpcm.h"
60 #endif
61 #ifdef CONFIG_USB_STORAGE_FREECOM
62 #include "freecom.h"
63 #endif
65 #include <linux/module.h>
66 #include <linux/sched.h>
67 #include <linux/errno.h>
68 #include <linux/init.h>
69 #include <linux/malloc.h>
71 /* Some informational data */
72 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
73 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
76 * Per device data
79 static int my_host_number;
82 * kernel thread actions
85 #define US_ACT_COMMAND 1
86 #define US_ACT_DEVICE_RESET 2
87 #define US_ACT_BUS_RESET 3
88 #define US_ACT_HOST_RESET 4
89 #define US_ACT_EXIT 5
91 /* The list of structures and the protective lock for them */
92 struct us_data *us_list;
93 struct semaphore us_list_semaphore;
95 static void * storage_probe(struct usb_device *dev, unsigned int ifnum);
96 static void storage_disconnect(struct usb_device *dev, void *ptr);
97 static struct usb_driver storage_driver = {
98 name: "usb-storage",
99 probe: storage_probe,
100 disconnect: storage_disconnect,
104 * fill_inquiry_response takes an unsigned char array (which must
105 * be at least 36 characters) and populates the vendor name,
106 * product name, and revision fields. Then the array is copied
107 * into the SCSI command's response buffer (oddly enough
108 * called request_buffer). data_len contains the length of the
109 * data array, which again must be at least 36.
112 void fill_inquiry_response(struct us_data *us, unsigned char *data,
113 unsigned int data_len) {
115 int i;
116 struct scatterlist *sg;
117 int len =
118 us->srb->request_bufflen > data_len ? data_len :
119 us->srb->request_bufflen;
120 int transferred;
121 int amt;
123 if (data_len<36) // You lose.
124 return;
126 memcpy(data+8, us->unusual_dev->vendorName,
127 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
128 strlen(us->unusual_dev->vendorName));
129 memcpy(data+16, us->unusual_dev->productName,
130 strlen(us->unusual_dev->productName) > 16 ? 16 :
131 strlen(us->unusual_dev->productName));
132 data[32] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>12) & 0x0F);
133 data[33] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>8) & 0x0F);
134 data[34] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>4) & 0x0F);
135 data[35] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice) & 0x0F);
137 if (us->srb->use_sg) {
138 sg = (struct scatterlist *)us->srb->request_buffer;
139 for (i=0; i<us->srb->use_sg; i++)
140 memset(sg[i].address, 0, sg[i].length);
141 for (i=0, transferred=0;
142 i<us->srb->use_sg && transferred < len;
143 i++) {
144 amt = sg[i].length > len-transferred ?
145 len-transferred : sg[i].length;
146 memcpy(sg[i].address, data+transferred, amt);
147 transferred -= amt;
149 } else {
150 memset(us->srb->request_buffer, 0, us->srb->request_bufflen);
151 memcpy(us->srb->request_buffer, data, len);
155 static int usb_stor_control_thread(void * __us)
157 wait_queue_t wait;
158 struct us_data *us = (struct us_data *)__us;
159 int action;
161 lock_kernel();
164 * This thread doesn't need any user-level access,
165 * so get rid of all our resources..
167 exit_files(current);
168 current->files = init_task.files;
169 atomic_inc(&current->files->count);
170 daemonize();
172 /* set our name for identification purposes */
173 sprintf(current->comm, "usb-storage-%d", us->host_number);
175 unlock_kernel();
177 /* set up for wakeups by new commands */
178 init_waitqueue_entry(&wait, current);
179 init_waitqueue_head(&(us->wqh));
180 add_wait_queue(&(us->wqh), &wait);
182 /* signal that we've started the thread */
183 up(&(us->notify));
184 set_current_state(TASK_INTERRUPTIBLE);
186 for(;;) {
187 US_DEBUGP("*** thread sleeping.\n");
188 schedule();
189 US_DEBUGP("*** thread awakened.\n");
191 /* lock access to the queue element */
192 down(&(us->queue_exclusion));
194 /* take the command off the queue */
195 action = us->action;
196 us->action = 0;
197 us->srb = us->queue_srb;
199 /* release the queue lock as fast as possible */
200 up(&(us->queue_exclusion));
202 switch (action) {
203 case US_ACT_COMMAND:
204 /* reject the command if the direction indicator
205 * is UNKNOWN
207 if (us->srb->sc_data_direction == SCSI_DATA_UNKNOWN) {
208 US_DEBUGP("UNKNOWN data direction\n");
209 us->srb->result = DID_ERROR;
210 set_current_state(TASK_INTERRUPTIBLE);
211 us->srb->scsi_done(us->srb);
212 us->srb = NULL;
213 break;
216 /* reject if target != 0 or if LUN is higher than
217 * the maximum known LUN
219 if (us->srb->target &&
220 !(us->flags & US_FL_SCM_MULT_TARG)) {
221 US_DEBUGP("Bad target number (%d/%d)\n",
222 us->srb->target, us->srb->lun);
223 us->srb->result = DID_BAD_TARGET << 16;
225 set_current_state(TASK_INTERRUPTIBLE);
226 us->srb->scsi_done(us->srb);
227 us->srb = NULL;
228 break;
231 if (us->srb->lun > us->max_lun) {
232 US_DEBUGP("Bad LUN (%d/%d)\n",
233 us->srb->target, us->srb->lun);
234 us->srb->result = DID_BAD_TARGET << 16;
236 set_current_state(TASK_INTERRUPTIBLE);
237 us->srb->scsi_done(us->srb);
238 us->srb = NULL;
239 break;
242 /* handle those devices which can't do a START_STOP */
243 if ((us->srb->cmnd[0] == START_STOP) &&
244 (us->flags & US_FL_START_STOP)) {
245 us->srb->result = GOOD;
247 set_current_state(TASK_INTERRUPTIBLE);
248 us->srb->scsi_done(us->srb);
249 us->srb = NULL;
250 break;
253 /* lock the device pointers */
254 down(&(us->dev_semaphore));
256 /* our device has gone - pretend not ready */
257 if (!us->pusb_dev) {
258 US_DEBUGP("Request is for removed device\n");
259 /* For REQUEST_SENSE, it's the data. But
260 * for anything else, it should look like
261 * we auto-sensed for it.
263 if (us->srb->cmnd[0] == REQUEST_SENSE) {
264 memcpy(us->srb->request_buffer,
265 usb_stor_sense_notready,
266 sizeof(usb_stor_sense_notready));
267 us->srb->result = GOOD;
268 } else {
269 memcpy(us->srb->sense_buffer,
270 usb_stor_sense_notready,
271 sizeof(usb_stor_sense_notready));
272 us->srb->result = CHECK_CONDITION;
274 } else { /* !us->pusb_dev */
275 /* we've got a command, let's do it! */
276 US_DEBUG(usb_stor_show_command(us->srb));
277 us->proto_handler(us->srb, us);
280 /* unlock the device pointers */
281 up(&(us->dev_semaphore));
283 /* indicate that the command is done */
284 if (us->srb->result != DID_ABORT << 16) {
285 US_DEBUGP("scsi cmd done, result=0x%x\n",
286 us->srb->result);
287 set_current_state(TASK_INTERRUPTIBLE);
288 us->srb->scsi_done(us->srb);
289 } else {
290 US_DEBUGP("scsi command aborted\n");
291 set_current_state(TASK_INTERRUPTIBLE);
292 up(&(us->notify));
294 us->srb = NULL;
295 break;
297 case US_ACT_DEVICE_RESET:
298 break;
300 case US_ACT_BUS_RESET:
301 break;
303 case US_ACT_HOST_RESET:
304 break;
306 } /* end switch on action */
308 /* exit if we get a signal to exit */
309 if (action == US_ACT_EXIT) {
310 US_DEBUGP("-- US_ACT_EXIT command recieved\n");
311 break;
313 } /* for (;;) */
315 /* notify the exit routine that we're actually exiting now */
316 up(&(us->notify));
318 return 0;
321 /* This is the list of devices we recognize, along with their flag data */
323 /* The vendor name should be kept at eight characters or less, and
324 * the product name should be kept at 16 characters or less. If a device
325 * has the US_FL_DUMMY_INQUIRY flag, then the vendor and product names
326 * normally generated by a device thorugh the INQUIRY response will be
327 * taken from this list, and this is the reason for the above size
328 * restriction. However, if the flag is not present, then you
329 * are free to use as many characters as you like.
332 int euscsi_init(struct us_data *us)
334 unsigned char bar = 0x1;
335 int result;
337 US_DEBUGP("Attempting to init eUSCSI bridge...\n");
338 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
339 0x0C, USB_RECIP_INTERFACE | USB_TYPE_VENDOR,
340 0x01, 0x0, &bar, 0x1, 5*HZ);
341 US_DEBUGP("-- result is %d\n", result);
342 US_DEBUGP("-- bar afterwards is %d\n", bar);
345 static struct us_unusual_dev us_unusual_dev_list[] = {
347 { 0x03f0, 0x0107, 0x0200, 0x0200,
348 "HP",
349 "CD-Writer+",
350 US_SC_8070, US_PR_CB, NULL,
351 0},
353 #ifdef CONFIG_USB_STORAGE_HP8200e
354 { 0x03f0, 0x0207, 0x0001, 0x0001,
355 "HP",
356 "CD-Writer+ 8200e",
357 US_SC_8070, US_PR_SCM_ATAPI, init_8200e,
358 US_FL_SINGLE_LUN},
359 #endif
361 { 0x04e6, 0x0001, 0x0200, 0x0200,
362 "Matshita",
363 "LS-120",
364 US_SC_8020, US_PR_CB, NULL,
365 US_FL_SINGLE_LUN},
367 { 0x04e6, 0x0002, 0x0100, 0x0100,
368 "Shuttle",
369 "eUSCSI Bridge",
370 US_SC_SCSI, US_PR_BULK, euscsi_init,
371 US_FL_SCM_MULT_TARG },
373 #ifdef CONFIG_USB_STORAGE_SDDR09
374 { 0x04e6, 0x0003, 0x0000, 0x9999,
375 "Sandisk",
376 "ImageMate SDDR09",
377 US_SC_SCSI, US_PR_EUSB_SDDR09, NULL,
378 US_FL_SINGLE_LUN | US_FL_START_STOP },
379 #endif
381 #ifdef CONFIG_USB_STORAGE_DPCM
382 { 0x0436, 0x0005, 0x0100, 0x0100,
383 "Microtech",
384 "CameraMate (DPCM_USB)",
385 US_SC_SCSI, US_PR_DPCM_USB, NULL,
386 US_FL_START_STOP },
387 #endif
389 { 0x04e6, 0x0006, 0x0100, 0x0200,
390 "Shuttle",
391 "eUSB MMC Adapter",
392 US_SC_SCSI, US_PR_CB, NULL,
393 US_FL_SINGLE_LUN},
395 { 0x04e6, 0x0009, 0x0200, 0x0200,
396 "Shuttle",
397 "ATA/ATAPI Bridge",
398 US_SC_8020, US_PR_CB, NULL,
399 US_FL_SINGLE_LUN},
401 { 0x04e6, 0x000A, 0x0200, 0x0200,
402 "Shuttle",
403 "Compact Flash Reader",
404 US_SC_8020, US_PR_CB, NULL,
405 US_FL_SINGLE_LUN},
407 { 0x04e6, 0x000B, 0x0100, 0x0100,
408 "Shuttle",
409 "eUSCSI Bridge",
410 US_SC_SCSI, US_PR_BULK, euscsi_init,
411 US_FL_SCM_MULT_TARG },
413 { 0x04e6, 0x000C, 0x0100, 0x0100,
414 "Shuttle",
415 "eUSCSI Bridge",
416 US_SC_SCSI, US_PR_BULK, euscsi_init,
417 US_FL_SCM_MULT_TARG },
419 { 0x04e6, 0x0101, 0x0200, 0x0200,
420 "Shuttle",
421 "CD-RW Device",
422 US_SC_8020, US_PR_CB, NULL,
423 US_FL_SINGLE_LUN},
425 { 0x054c, 0x0010, 0x0210, 0x0210,
426 "Sony",
427 "DSC-S30/S70",
428 US_SC_SCSI, US_PR_CB, NULL,
429 US_FL_SINGLE_LUN | US_FL_START_STOP | US_FL_MODE_XLATE },
431 { 0x054c, 0x002d, 0x0100, 0x0100,
432 "Sony",
433 "Memorystick MSAC-US1",
434 US_SC_SCSI, US_PR_CB, NULL,
435 US_FL_SINGLE_LUN | US_FL_START_STOP | US_FL_MODE_XLATE },
437 { 0x057b, 0x0000, 0x0000, 0x0299,
438 "Y-E Data",
439 "Flashbuster-U",
440 US_SC_UFI, US_PR_CB, NULL,
441 US_FL_SINGLE_LUN},
443 { 0x057b, 0x0000, 0x0300, 0x9999,
444 "Y-E Data",
445 "Flashbuster-U",
446 US_SC_UFI, US_PR_CBI, NULL,
447 US_FL_SINGLE_LUN},
449 { 0x059f, 0xa601, 0x0200, 0x0200,
450 "LaCie",
451 "USB Hard Disk",
452 US_SC_RBC, US_PR_CB, NULL,
453 0 },
455 { 0x05ab, 0x0031, 0x0100, 0x0100,
456 "In-System",
457 "USB/IDE Bridge",
458 US_SC_8070, US_PR_BULK, NULL,
459 0 },
461 { 0x0693, 0x0005, 0x0100, 0x0100,
462 "Hagiwara",
463 "Flashgate",
464 US_SC_SCSI, US_PR_BULK, NULL,
465 0 },
467 { 0x0693, 0x0002, 0x0100, 0x0100,
468 "Hagiwara",
469 "FlashGate SmartMedia",
470 US_SC_SCSI, US_PR_BULK, NULL,
471 0 },
473 { 0x0781, 0x0001, 0x0200, 0x0200,
474 "Sandisk",
475 "ImageMate SDDR05a",
476 US_SC_SCSI, US_PR_CB, NULL,
477 US_FL_SINGLE_LUN | US_FL_START_STOP},
479 #ifdef CONFIG_USB_STORAGE_SDDR09
480 { 0x0781, 0x0200, 0x0100, 0x0100,
481 "Sandisk",
482 "ImageMate SDDR09",
483 US_SC_SCSI, US_PR_EUSB_SDDR09, NULL,
484 US_FL_SINGLE_LUN | US_FL_START_STOP },
485 #endif
487 { 0x0781, 0x0002, 0x0009, 0x0009,
488 "Sandisk",
489 "ImageMate SDDR31",
490 US_SC_SCSI, US_PR_BULK, NULL,
491 US_FL_IGNORE_SER},
493 { 0x07af, 0x0004, 0x0100, 0x0100,
494 "Microtech",
495 "USB-SCSI-DB25",
496 US_SC_SCSI, US_PR_BULK, euscsi_init,
497 US_FL_SCM_MULT_TARG },
499 #ifdef CONFIG_USB_STORAGE_FREECOM
500 { 0x07ab, 0xfc01, 0x0921, 0x0921,
501 "Freecom",
502 "USB-IDE",
503 US_SC_8070, US_PR_FREECOM, NULL, US_FL_SINGLE_LUN },
504 #endif
506 { 0x07af, 0x0005, 0x0100, 0x0100,
507 "Microtech",
508 "USB-SCSI-HD50",
509 US_SC_SCSI, US_PR_BULK, euscsi_init,
510 US_FL_SCM_MULT_TARG },
512 #ifdef CONFIG_USB_STORAGE_DPCM
513 { 0x07af, 0x0006, 0x0100, 0x0100,
514 "Microtech",
515 "CameraMate (DPCM_USB)",
516 US_SC_SCSI, US_PR_DPCM_USB, NULL,
517 US_FL_START_STOP },
518 #endif
519 { 0 }
522 /* Search our ususual device list, based on vendor/product combinations
523 * to see if we can support this device. Returns a pointer to a structure
524 * defining how we should support this device, or NULL if it's not in the
525 * list
527 static struct us_unusual_dev* us_find_dev(u16 idVendor, u16 idProduct,
528 u16 bcdDevice)
530 struct us_unusual_dev* ptr;
532 US_DEBUGP("Searching unusual device list for (0x%x, 0x%x, 0x%x)...\n",
533 idVendor, idProduct, bcdDevice);
535 ptr = us_unusual_dev_list;
536 while ((ptr->idVendor != 0x0000) &&
537 !((ptr->idVendor == idVendor) &&
538 (ptr->idProduct == idProduct) &&
539 (ptr->bcdDeviceMin <= bcdDevice) &&
540 (ptr->bcdDeviceMax >= bcdDevice)))
541 ptr++;
543 /* if the search ended because we hit the end record, we failed */
544 if (ptr->idVendor == 0x0000) {
545 US_DEBUGP("-- did not find a matching device\n");
546 return NULL;
549 /* otherwise, we found one! */
550 US_DEBUGP("-- found matching device: %s %s\n", ptr->vendorName,
551 ptr->productName);
552 return ptr;
555 /* Set up the IRQ pipe and handler
556 * Note that this function assumes that all the data in the us_data
557 * strucuture is current. This includes the ep_int field, which gives us
558 * the endpoint for the interrupt.
559 * Returns non-zero on failure, zero on success
561 static int usb_stor_allocate_irq(struct us_data *ss)
563 unsigned int pipe;
564 int maxp;
565 int result;
567 US_DEBUGP("Allocating IRQ for CBI transport\n");
569 /* lock access to the data structure */
570 down(&(ss->irq_urb_sem));
572 /* allocate the URB */
573 ss->irq_urb = usb_alloc_urb(0);
574 if (!ss->irq_urb) {
575 up(&(ss->irq_urb_sem));
576 US_DEBUGP("couldn't allocate interrupt URB");
577 return 1;
580 /* calculate the pipe and max packet size */
581 pipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int->bEndpointAddress &
582 USB_ENDPOINT_NUMBER_MASK);
583 maxp = usb_maxpacket(ss->pusb_dev, pipe, usb_pipeout(pipe));
584 if (maxp > sizeof(ss->irqbuf))
585 maxp = sizeof(ss->irqbuf);
587 /* fill in the URB with our data */
588 FILL_INT_URB(ss->irq_urb, ss->pusb_dev, pipe, ss->irqbuf, maxp,
589 usb_stor_CBI_irq, ss, ss->ep_int->bInterval);
591 /* submit the URB for processing */
592 result = usb_submit_urb(ss->irq_urb);
593 US_DEBUGP("usb_submit_urb() returns %d\n", result);
594 if (result) {
595 usb_free_urb(ss->irq_urb);
596 up(&(ss->irq_urb_sem));
597 return 2;
600 /* unlock the data structure and return success */
601 up(&(ss->irq_urb_sem));
602 return 0;
605 /* Probe to see if a new device is actually a SCSI device */
606 static void * storage_probe(struct usb_device *dev, unsigned int ifnum)
608 int i;
609 char mf[USB_STOR_STRING_LEN]; /* manufacturer */
610 char prod[USB_STOR_STRING_LEN]; /* product */
611 char serial[USB_STOR_STRING_LEN]; /* serial number */
612 GUID(guid); /* Global Unique Identifier */
613 unsigned int flags;
614 struct us_unusual_dev *unusual_dev;
615 struct us_data *ss = NULL;
616 int result;
618 /* these are temporary copies -- we test on these, then put them
619 * in the us-data structure
621 struct usb_endpoint_descriptor *ep_in = NULL;
622 struct usb_endpoint_descriptor *ep_out = NULL;
623 struct usb_endpoint_descriptor *ep_int = NULL;
624 u8 subclass = 0;
625 u8 protocol = 0;
627 /* the altsettting 0 on the interface we're probing */
628 struct usb_interface_descriptor *altsetting =
629 &(dev->actconfig->interface[ifnum].altsetting[0]);
631 /* clear the temporary strings */
632 memset(mf, 0, sizeof(mf));
633 memset(prod, 0, sizeof(prod));
634 memset(serial, 0, sizeof(serial));
636 /* search for this device in our unusual device list */
637 unusual_dev = us_find_dev(dev->descriptor.idVendor,
638 dev->descriptor.idProduct,
639 dev->descriptor.bcdDevice);
642 * Can we support this device, either because we know about it
643 * from our unusual device list, or because it advertises that it's
644 * compliant to the specification?
646 if (!unusual_dev &&
647 !(dev->descriptor.bDeviceClass == 0 &&
648 altsetting->bInterfaceClass == USB_CLASS_MASS_STORAGE &&
649 altsetting->bInterfaceSubClass >= US_SC_MIN &&
650 altsetting->bInterfaceSubClass <= US_SC_MAX)) {
651 /* if it's not a mass storage, we go no further */
652 return NULL;
655 /* At this point, we know we've got a live one */
656 US_DEBUGP("USB Mass Storage device detected\n");
658 /* Determine subclass and protocol, or copy from the interface */
659 if (unusual_dev) {
660 subclass = unusual_dev->useProtocol;
661 protocol = unusual_dev->useTransport;
662 flags = unusual_dev->flags;
663 } else {
664 subclass = altsetting->bInterfaceSubClass;
665 protocol = altsetting->bInterfaceProtocol;
666 flags = 0;
670 * Find the endpoints we need
671 * We are expecting a minimum of 2 endpoints - in and out (bulk).
672 * An optional interrupt is OK (necessary for CBI protocol).
673 * We will ignore any others.
675 for (i = 0; i < altsetting->bNumEndpoints; i++) {
676 /* is it an BULK endpoint? */
677 if ((altsetting->endpoint[i].bmAttributes &
678 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
679 /* BULK in or out? */
680 if (altsetting->endpoint[i].bEndpointAddress &
681 USB_DIR_IN)
682 ep_in = &altsetting->endpoint[i];
683 else
684 ep_out = &altsetting->endpoint[i];
687 /* is it an interrupt endpoint? */
688 if ((altsetting->endpoint[i].bmAttributes &
689 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
690 ep_int = &altsetting->endpoint[i];
693 US_DEBUGP("Endpoints: In: 0x%p Out: 0x%p Int: 0x%p (Period %d)\n",
694 ep_in, ep_out, ep_int, ep_int ? ep_int->bInterval : 0);
696 /* set the interface -- STALL is an acceptable response here */
697 #ifdef CONFIG_USB_STORAGE_SDDR09
698 if (protocol == US_PR_EUSB_SDDR09)
699 result = usb_set_configuration(dev, 1);
701 US_DEBUGP("Result from usb_set_configuration is %d\n", result);
702 if (result == -EPIPE) {
703 US_DEBUGP("-- clearing stall on control interface\n");
704 usb_clear_halt(dev, usb_sndctrlpipe(dev, 0));
705 } else if (result != 0) {
706 /* it's not a stall, but another error -- time to bail */
707 US_DEBUGP("-- Unknown error. Rejecting device\n");
708 return NULL;
710 #endif
712 /* Do some basic sanity checks, and bail if we find a problem */
713 if (!ep_in || !ep_out || (protocol == US_PR_CBI && !ep_int)) {
714 US_DEBUGP("Sanity check failed. Rejecting device.\n");
715 return NULL;
718 /* At this point, we're committed to using the device */
720 /* clear the GUID and fetch the strings */
721 GUID_CLEAR(guid);
722 if (dev->descriptor.iManufacturer)
723 usb_string(dev, dev->descriptor.iManufacturer,
724 mf, sizeof(mf));
725 if (dev->descriptor.iProduct)
726 usb_string(dev, dev->descriptor.iProduct,
727 prod, sizeof(prod));
728 if (dev->descriptor.iSerialNumber && !(flags & US_FL_IGNORE_SER))
729 usb_string(dev, dev->descriptor.iSerialNumber,
730 serial, sizeof(serial));
732 /* Create a GUID for this device */
733 if (dev->descriptor.iSerialNumber && serial[0]) {
734 /* If we have a serial number, and it's a non-NULL string */
735 make_guid(guid, dev->descriptor.idVendor,
736 dev->descriptor.idProduct, serial);
737 } else {
738 /* We don't have a serial number, so we use 0 */
739 make_guid(guid, dev->descriptor.idVendor,
740 dev->descriptor.idProduct, "0");
743 /* lock access to the data structures */
744 down(&us_list_semaphore);
747 * Now check if we have seen this GUID before
748 * We're looking for a device with a matching GUID that isn't
749 * allready on the system
751 ss = us_list;
752 while ((ss != NULL) &&
753 ((ss->pusb_dev) || !GUID_EQUAL(guid, ss->guid)))
754 ss = ss->next;
756 if (ss != NULL) {
757 /* Existing device -- re-connect */
758 US_DEBUGP("Found existing GUID " GUID_FORMAT "\n",
759 GUID_ARGS(guid));
761 /* establish the connection to the new device upon reconnect */
762 ss->ifnum = ifnum;
763 ss->pusb_dev = dev;
765 /* copy over the endpoint data */
766 if (ep_in)
767 ss->ep_in = ep_in->bEndpointAddress &
768 USB_ENDPOINT_NUMBER_MASK;
769 if (ep_out)
770 ss->ep_out = ep_out->bEndpointAddress &
771 USB_ENDPOINT_NUMBER_MASK;
772 ss->ep_int = ep_int;
774 /* allocate an IRQ callback if one is needed */
775 if ((ss->protocol == US_PR_CBI) && usb_stor_allocate_irq(ss))
776 return NULL;
778 /* Re-Initialize the device if it needs it */
780 if (unusual_dev && unusual_dev->initFunction)
781 (unusual_dev->initFunction)(ss);
783 } else {
784 /* New device -- allocate memory and initialize */
785 US_DEBUGP("New GUID " GUID_FORMAT "\n", GUID_ARGS(guid));
787 if ((ss = (struct us_data *)kmalloc(sizeof(struct us_data),
788 GFP_KERNEL)) == NULL) {
789 printk(KERN_WARNING USB_STORAGE "Out of memory\n");
790 up(&us_list_semaphore);
791 return NULL;
793 memset(ss, 0, sizeof(struct us_data));
795 /* allocate the URB we're going to use */
796 ss->current_urb = usb_alloc_urb(0);
797 if (!ss->current_urb) {
798 kfree(ss);
799 return NULL;
802 /* Initialize the mutexes only when the struct is new */
803 init_MUTEX_LOCKED(&(ss->notify));
804 init_MUTEX_LOCKED(&(ss->ip_waitq));
805 init_MUTEX(&(ss->queue_exclusion));
806 init_MUTEX(&(ss->irq_urb_sem));
807 init_MUTEX(&(ss->current_urb_sem));
808 init_MUTEX(&(ss->dev_semaphore));
810 /* copy over the subclass and protocol data */
811 ss->subclass = subclass;
812 ss->protocol = protocol;
813 ss->flags = flags;
814 ss->unusual_dev = unusual_dev;
816 /* copy over the endpoint data */
817 if (ep_in)
818 ss->ep_in = ep_in->bEndpointAddress &
819 USB_ENDPOINT_NUMBER_MASK;
820 if (ep_out)
821 ss->ep_out = ep_out->bEndpointAddress &
822 USB_ENDPOINT_NUMBER_MASK;
823 ss->ep_int = ep_int;
825 /* establish the connection to the new device */
826 ss->ifnum = ifnum;
827 ss->pusb_dev = dev;
829 /* copy over the identifiying strings */
830 strncpy(ss->vendor, mf, USB_STOR_STRING_LEN);
831 strncpy(ss->product, prod, USB_STOR_STRING_LEN);
832 strncpy(ss->serial, serial, USB_STOR_STRING_LEN);
833 if (strlen(ss->vendor) == 0) {
834 if (unusual_dev)
835 strncpy(ss->vendor, unusual_dev->vendorName,
836 USB_STOR_STRING_LEN);
837 else
838 strncpy(ss->vendor, "Unknown",
839 USB_STOR_STRING_LEN);
841 if (strlen(ss->product) == 0) {
842 if (unusual_dev)
843 strncpy(ss->product, unusual_dev->productName,
844 USB_STOR_STRING_LEN);
845 else
846 strncpy(ss->product, "Unknown",
847 USB_STOR_STRING_LEN);
849 if (strlen(ss->serial) == 0)
850 strncpy(ss->serial, "None", USB_STOR_STRING_LEN);
852 /* copy the GUID we created before */
853 memcpy(ss->guid, guid, sizeof(guid));
856 * Set the handler pointers based on the protocol
857 * Again, this data is persistant across reattachments
859 switch (ss->protocol) {
860 case US_PR_CB:
861 ss->transport_name = "Control/Bulk";
862 ss->transport = usb_stor_CB_transport;
863 ss->transport_reset = usb_stor_CB_reset;
864 ss->max_lun = 7;
865 break;
867 case US_PR_CBI:
868 ss->transport_name = "Control/Bulk/Interrupt";
869 ss->transport = usb_stor_CBI_transport;
870 ss->transport_reset = usb_stor_CB_reset;
871 ss->max_lun = 7;
872 break;
874 case US_PR_BULK:
875 ss->transport_name = "Bulk";
876 ss->transport = usb_stor_Bulk_transport;
877 ss->transport_reset = usb_stor_Bulk_reset;
878 ss->max_lun = usb_stor_Bulk_max_lun(ss);
879 break;
881 #ifdef CONFIG_USB_STORAGE_HP8200e
882 case US_PR_SCM_ATAPI:
883 ss->transport_name = "SCM/ATAPI";
884 ss->transport = hp8200e_transport;
885 ss->transport_reset = usb_stor_CB_reset;
886 ss->max_lun = 1;
887 break;
888 #endif
890 #ifdef CONFIG_USB_STORAGE_SDDR09
891 case US_PR_EUSB_SDDR09:
892 ss->transport_name = "EUSB/SDDR09";
893 ss->transport = sddr09_transport;
894 ss->transport_reset = usb_stor_CB_reset;
895 ss->max_lun = 1;
896 break;
897 #endif
899 #ifdef CONFIG_USB_STORAGE_DPCM
900 case US_PR_DPCM_USB:
901 ss->transport_name = "Control/Bulk-EUSB/SDDR09";
902 ss->transport = dpcm_transport;
903 ss->transport_reset = usb_stor_CB_reset;
904 ss->max_lun = 1;
905 break;
906 #endif
908 #ifdef CONFIG_USB_STORAGE_FREECOM
909 case US_PR_FREECOM:
910 ss->transport_name = "Freecom";
911 ss->transport = freecom_transport;
912 ss->transport_reset = usb_stor_freecom_reset;
913 ss->max_lun = 0;
914 break;
915 #endif
917 default:
918 ss->transport_name = "Unknown";
919 up(&us_list_semaphore);
920 kfree(ss->current_urb);
921 kfree(ss);
922 return NULL;
923 break;
925 US_DEBUGP("Transport: %s\n", ss->transport_name);
927 /* fix for single-lun devices */
928 if (ss->flags & US_FL_SINGLE_LUN)
929 ss->max_lun = 0;
931 switch (ss->subclass) {
932 case US_SC_RBC:
933 ss->protocol_name = "Reduced Block Commands (RBC)";
934 ss->proto_handler = usb_stor_transparent_scsi_command;
935 break;
937 case US_SC_8020:
938 ss->protocol_name = "8020i";
939 ss->proto_handler = usb_stor_ATAPI_command;
940 break;
942 case US_SC_QIC:
943 ss->protocol_name = "QIC-157";
944 ss->proto_handler = usb_stor_qic157_command;
945 break;
947 case US_SC_8070:
948 ss->protocol_name = "8070i";
949 ss->proto_handler = usb_stor_ATAPI_command;
950 break;
952 case US_SC_SCSI:
953 ss->protocol_name = "Transparent SCSI";
954 ss->proto_handler = usb_stor_transparent_scsi_command;
955 break;
957 case US_SC_UFI:
958 ss->protocol_name = "Uniform Floppy Interface (UFI)";
959 ss->proto_handler = usb_stor_ufi_command;
960 break;
962 default:
963 ss->protocol_name = "Unknown";
964 up(&us_list_semaphore);
965 kfree(ss->current_urb);
966 kfree(ss);
967 return NULL;
968 break;
970 US_DEBUGP("Protocol: %s\n", ss->protocol_name);
972 /* allocate an IRQ callback if one is needed */
973 if ((ss->protocol == US_PR_CBI) && usb_stor_allocate_irq(ss))
974 return NULL;
977 * Since this is a new device, we need to generate a scsi
978 * host definition, and register with the higher SCSI layers
981 /* Initialize the host template based on the default one */
982 memcpy(&(ss->htmplt), &usb_stor_host_template,
983 sizeof(usb_stor_host_template));
985 /* Grab the next host number */
986 ss->host_number = my_host_number++;
988 /* We abuse this pointer so we can pass the ss pointer to
989 * the host controler thread in us_detect. But how else are
990 * we to do it?
992 (struct us_data *)ss->htmplt.proc_dir = ss;
994 /* Just before we start our control thread, initialize
995 * the device if it needs initialization */
996 if (unusual_dev && unusual_dev->initFunction)
997 (unusual_dev->initFunction)(ss);
999 /* start up our control thread */
1000 ss->pid = kernel_thread(usb_stor_control_thread, ss,
1001 CLONE_VM);
1002 if (ss->pid < 0) {
1003 printk(KERN_WARNING USB_STORAGE
1004 "Unable to start control thread\n");
1005 kfree(ss->current_urb);
1006 kfree(ss);
1007 return NULL;
1010 /* wait for the thread to start */
1011 down(&(ss->notify));
1013 /* now register - our detect function will be called */
1014 ss->htmplt.module = THIS_MODULE;
1015 scsi_register_module(MODULE_SCSI_HA, &(ss->htmplt));
1017 /* put us in the list */
1018 ss->next = us_list;
1019 us_list = ss;
1022 /* release the data structure lock */
1023 up(&us_list_semaphore);
1025 printk(KERN_DEBUG
1026 "WARNING: USB Mass Storage data integrity not assured\n");
1027 printk(KERN_DEBUG
1028 "USB Mass Storage device found at %d\n", dev->devnum);
1030 /* return a pointer for the disconnect function */
1031 return ss;
1034 /* Handle a disconnect event from the USB core */
1035 static void storage_disconnect(struct usb_device *dev, void *ptr)
1037 struct us_data *ss = ptr;
1038 int result;
1040 US_DEBUGP("storage_disconnect() called\n");
1042 /* this is the odd case -- we disconnected but weren't using it */
1043 if (!ss) {
1044 US_DEBUGP("-- device was not in use\n");
1045 return;
1048 /* lock access to the device data structure */
1049 down(&(ss->dev_semaphore));
1051 /* release the IRQ, if we have one */
1052 down(&(ss->irq_urb_sem));
1053 if (ss->irq_urb) {
1054 US_DEBUGP("-- releasing irq handle\n");
1055 result = usb_unlink_urb(ss->irq_urb);
1056 ss->irq_urb = NULL;
1057 US_DEBUGP("-- usb_unlink_urb() returned %d\n", result);
1058 usb_free_urb(ss->irq_urb);
1060 up(&(ss->irq_urb_sem));
1062 /* mark the device as gone */
1063 ss->pusb_dev = NULL;
1065 /* lock access to the device data structure */
1066 up(&(ss->dev_semaphore));
1069 /***********************************************************************
1070 * Initialization and registration
1071 ***********************************************************************/
1073 int __init usb_stor_init(void)
1075 /* initialize internal global data elements */
1076 us_list = NULL;
1077 init_MUTEX(&us_list_semaphore);
1078 my_host_number = 0;
1080 /* register the driver, return -1 if error */
1081 if (usb_register(&storage_driver) < 0)
1082 return -1;
1084 /* we're all set */
1085 printk(KERN_INFO "USB Mass Storage support registered.\n");
1086 return 0;
1089 void __exit usb_stor_exit(void)
1091 struct us_data *next;
1093 US_DEBUGP("usb_stor_exit() called\n");
1095 /* Deregister the driver
1096 * This eliminates races with probes and disconnects
1098 US_DEBUGP("-- calling usb_deregister()\n");
1099 usb_deregister(&storage_driver) ;
1101 /* lock access to the data structures */
1102 down(&us_list_semaphore);
1104 /* While there are still virtual hosts, unregister them
1106 * Note that the us_release() routine will destroy the local data
1107 * structure. So we have to peel these off the top of the list
1108 * and keep updating the head pointer as we go.
1110 while (us_list) {
1111 /* keep track of where the next one is */
1112 next = us_list->next;
1114 US_DEBUGP("-- calling scsi_unregister_module()\n");
1115 scsi_unregister_module(MODULE_SCSI_HA, &(us_list->htmplt));
1117 /* Now that scsi_unregister_module is done with the host
1118 * template, we can free the us_data structure (the host
1119 * template is inline in this structure). */
1121 /* If there's extra data in the us_data structure then
1122 * free that first */
1124 if (us_list->extra) {
1125 if (us_list->extra_destructor)
1126 (*us_list->extra_destructor)(
1127 us_list->extra);
1128 kfree(us_list->extra);
1130 kfree (us_list);
1132 /* advance the list pointer */
1133 us_list = next;
1136 /* unlock the data structures */
1137 up(&us_list_semaphore);
1140 module_init(usb_stor_init) ;
1141 module_exit(usb_stor_exit) ;