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)
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
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>
49 #include "transport.h"
52 #ifdef CONFIG_USB_STORAGE_HP8200e
53 #include "shuttle_usbat.h"
55 #ifdef CONFIG_USB_STORAGE_SDDR09
58 #ifdef CONFIG_USB_STORAGE_DPCM
61 #ifdef CONFIG_USB_STORAGE_FREECOM
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");
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
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
= {
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
) {
116 struct scatterlist
*sg
;
118 us
->srb
->request_bufflen
> data_len
? data_len
:
119 us
->srb
->request_bufflen
;
123 if (data_len
<36) // You lose.
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
;
144 amt
= sg
[i
].length
> len
-transferred
?
145 len
-transferred
: sg
[i
].length
;
146 memcpy(sg
[i
].address
, data
+transferred
, amt
);
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
)
158 struct us_data
*us
= (struct us_data
*)__us
;
164 * This thread doesn't need any user-level access,
165 * so get rid of all our resources..
168 current
->files
= init_task
.files
;
169 atomic_inc(¤t
->files
->count
);
172 /* set our name for identification purposes */
173 sprintf(current
->comm
, "usb-storage-%d", us
->host_number
);
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 */
184 set_current_state(TASK_INTERRUPTIBLE
);
187 US_DEBUGP("*** thread sleeping.\n");
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 */
197 us
->srb
= us
->queue_srb
;
199 /* release the queue lock as fast as possible */
200 up(&(us
->queue_exclusion
));
204 /* reject the command if the direction indicator
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
);
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
);
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
);
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
);
253 /* lock the device pointers */
254 down(&(us
->dev_semaphore
));
256 /* our device has gone - pretend not ready */
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
;
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",
287 set_current_state(TASK_INTERRUPTIBLE
);
288 us
->srb
->scsi_done(us
->srb
);
290 US_DEBUGP("scsi command aborted\n");
291 set_current_state(TASK_INTERRUPTIBLE
);
297 case US_ACT_DEVICE_RESET
:
300 case US_ACT_BUS_RESET
:
303 case US_ACT_HOST_RESET
:
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");
315 /* notify the exit routine that we're actually exiting now */
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;
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,
350 US_SC_8070
, US_PR_CB
, NULL
,
353 #ifdef CONFIG_USB_STORAGE_HP8200e
354 { 0x03f0, 0x0207, 0x0001, 0x0001,
357 US_SC_8070
, US_PR_SCM_ATAPI
, init_8200e
,
361 { 0x04e6, 0x0001, 0x0200, 0x0200,
364 US_SC_8020
, US_PR_CB
, NULL
,
367 { 0x04e6, 0x0002, 0x0100, 0x0100,
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,
377 US_SC_SCSI
, US_PR_EUSB_SDDR09
, NULL
,
378 US_FL_SINGLE_LUN
| US_FL_START_STOP
},
381 #ifdef CONFIG_USB_STORAGE_DPCM
382 { 0x0436, 0x0005, 0x0100, 0x0100,
384 "CameraMate (DPCM_USB)",
385 US_SC_SCSI
, US_PR_DPCM_USB
, NULL
,
389 { 0x04e6, 0x0006, 0x0100, 0x0200,
392 US_SC_SCSI
, US_PR_CB
, NULL
,
395 { 0x04e6, 0x0009, 0x0200, 0x0200,
398 US_SC_8020
, US_PR_CB
, NULL
,
401 { 0x04e6, 0x000A, 0x0200, 0x0200,
403 "Compact Flash Reader",
404 US_SC_8020
, US_PR_CB
, NULL
,
407 { 0x04e6, 0x000B, 0x0100, 0x0100,
410 US_SC_SCSI
, US_PR_BULK
, euscsi_init
,
411 US_FL_SCM_MULT_TARG
},
413 { 0x04e6, 0x000C, 0x0100, 0x0100,
416 US_SC_SCSI
, US_PR_BULK
, euscsi_init
,
417 US_FL_SCM_MULT_TARG
},
419 { 0x04e6, 0x0101, 0x0200, 0x0200,
422 US_SC_8020
, US_PR_CB
, NULL
,
425 { 0x054c, 0x0010, 0x0210, 0x0210,
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,
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,
440 US_SC_UFI
, US_PR_CB
, NULL
,
443 { 0x057b, 0x0000, 0x0300, 0x9999,
446 US_SC_UFI
, US_PR_CBI
, NULL
,
449 { 0x059f, 0xa601, 0x0200, 0x0200,
452 US_SC_RBC
, US_PR_CB
, NULL
,
455 { 0x05ab, 0x0031, 0x0100, 0x0100,
458 US_SC_8070
, US_PR_BULK
, NULL
,
461 { 0x0693, 0x0005, 0x0100, 0x0100,
464 US_SC_SCSI
, US_PR_BULK
, NULL
,
467 { 0x0693, 0x0002, 0x0100, 0x0100,
469 "FlashGate SmartMedia",
470 US_SC_SCSI
, US_PR_BULK
, NULL
,
473 { 0x0781, 0x0001, 0x0200, 0x0200,
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,
483 US_SC_SCSI
, US_PR_EUSB_SDDR09
, NULL
,
484 US_FL_SINGLE_LUN
| US_FL_START_STOP
},
487 { 0x0781, 0x0002, 0x0009, 0x0009,
490 US_SC_SCSI
, US_PR_BULK
, NULL
,
493 { 0x07af, 0x0004, 0x0100, 0x0100,
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,
503 US_SC_8070
, US_PR_FREECOM
, NULL
, US_FL_SINGLE_LUN
},
506 { 0x07af, 0x0005, 0x0100, 0x0100,
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,
515 "CameraMate (DPCM_USB)",
516 US_SC_SCSI
, US_PR_DPCM_USB
, NULL
,
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
527 static struct us_unusual_dev
* us_find_dev(u16 idVendor
, u16 idProduct
,
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
)))
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");
549 /* otherwise, we found one! */
550 US_DEBUGP("-- found matching device: %s %s\n", ptr
->vendorName
,
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
)
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);
575 up(&(ss
->irq_urb_sem
));
576 US_DEBUGP("couldn't allocate interrupt URB");
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
);
595 usb_free_urb(ss
->irq_urb
);
596 up(&(ss
->irq_urb_sem
));
600 /* unlock the data structure and return success */
601 up(&(ss
->irq_urb_sem
));
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
)
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 */
614 struct us_unusual_dev
*unusual_dev
;
615 struct us_data
*ss
= NULL
;
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
;
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?
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 */
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 */
660 subclass
= unusual_dev
->useProtocol
;
661 protocol
= unusual_dev
->useTransport
;
662 flags
= unusual_dev
->flags
;
664 subclass
= altsetting
->bInterfaceSubClass
;
665 protocol
= altsetting
->bInterfaceProtocol
;
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
&
682 ep_in
= &altsetting
->endpoint
[i
];
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");
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");
718 /* At this point, we're committed to using the device */
720 /* clear the GUID and fetch the strings */
722 if (dev
->descriptor
.iManufacturer
)
723 usb_string(dev
, dev
->descriptor
.iManufacturer
,
725 if (dev
->descriptor
.iProduct
)
726 usb_string(dev
, dev
->descriptor
.iProduct
,
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
);
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
752 while ((ss
!= NULL
) &&
753 ((ss
->pusb_dev
) || !GUID_EQUAL(guid
, ss
->guid
)))
757 /* Existing device -- re-connect */
758 US_DEBUGP("Found existing GUID " GUID_FORMAT
"\n",
761 /* establish the connection to the new device upon reconnect */
765 /* copy over the endpoint data */
767 ss
->ep_in
= ep_in
->bEndpointAddress
&
768 USB_ENDPOINT_NUMBER_MASK
;
770 ss
->ep_out
= ep_out
->bEndpointAddress
&
771 USB_ENDPOINT_NUMBER_MASK
;
774 /* allocate an IRQ callback if one is needed */
775 if ((ss
->protocol
== US_PR_CBI
) && usb_stor_allocate_irq(ss
))
778 /* Re-Initialize the device if it needs it */
780 if (unusual_dev
&& unusual_dev
->initFunction
)
781 (unusual_dev
->initFunction
)(ss
);
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
);
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
) {
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
;
814 ss
->unusual_dev
= unusual_dev
;
816 /* copy over the endpoint data */
818 ss
->ep_in
= ep_in
->bEndpointAddress
&
819 USB_ENDPOINT_NUMBER_MASK
;
821 ss
->ep_out
= ep_out
->bEndpointAddress
&
822 USB_ENDPOINT_NUMBER_MASK
;
825 /* establish the connection to the new device */
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) {
835 strncpy(ss
->vendor
, unusual_dev
->vendorName
,
836 USB_STOR_STRING_LEN
);
838 strncpy(ss
->vendor
, "Unknown",
839 USB_STOR_STRING_LEN
);
841 if (strlen(ss
->product
) == 0) {
843 strncpy(ss
->product
, unusual_dev
->productName
,
844 USB_STOR_STRING_LEN
);
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
) {
861 ss
->transport_name
= "Control/Bulk";
862 ss
->transport
= usb_stor_CB_transport
;
863 ss
->transport_reset
= usb_stor_CB_reset
;
868 ss
->transport_name
= "Control/Bulk/Interrupt";
869 ss
->transport
= usb_stor_CBI_transport
;
870 ss
->transport_reset
= usb_stor_CB_reset
;
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
);
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
;
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
;
899 #ifdef CONFIG_USB_STORAGE_DPCM
901 ss
->transport_name
= "Control/Bulk-EUSB/SDDR09";
902 ss
->transport
= dpcm_transport
;
903 ss
->transport_reset
= usb_stor_CB_reset
;
908 #ifdef CONFIG_USB_STORAGE_FREECOM
910 ss
->transport_name
= "Freecom";
911 ss
->transport
= freecom_transport
;
912 ss
->transport_reset
= usb_stor_freecom_reset
;
918 ss
->transport_name
= "Unknown";
919 up(&us_list_semaphore
);
920 kfree(ss
->current_urb
);
925 US_DEBUGP("Transport: %s\n", ss
->transport_name
);
927 /* fix for single-lun devices */
928 if (ss
->flags
& US_FL_SINGLE_LUN
)
931 switch (ss
->subclass
) {
933 ss
->protocol_name
= "Reduced Block Commands (RBC)";
934 ss
->proto_handler
= usb_stor_transparent_scsi_command
;
938 ss
->protocol_name
= "8020i";
939 ss
->proto_handler
= usb_stor_ATAPI_command
;
943 ss
->protocol_name
= "QIC-157";
944 ss
->proto_handler
= usb_stor_qic157_command
;
948 ss
->protocol_name
= "8070i";
949 ss
->proto_handler
= usb_stor_ATAPI_command
;
953 ss
->protocol_name
= "Transparent SCSI";
954 ss
->proto_handler
= usb_stor_transparent_scsi_command
;
958 ss
->protocol_name
= "Uniform Floppy Interface (UFI)";
959 ss
->proto_handler
= usb_stor_ufi_command
;
963 ss
->protocol_name
= "Unknown";
964 up(&us_list_semaphore
);
965 kfree(ss
->current_urb
);
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
))
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
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
,
1003 printk(KERN_WARNING USB_STORAGE
1004 "Unable to start control thread\n");
1005 kfree(ss
->current_urb
);
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 */
1022 /* release the data structure lock */
1023 up(&us_list_semaphore
);
1026 "WARNING: USB Mass Storage data integrity not assured\n");
1028 "USB Mass Storage device found at %d\n", dev
->devnum
);
1030 /* return a pointer for the disconnect function */
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
;
1040 US_DEBUGP("storage_disconnect() called\n");
1042 /* this is the odd case -- we disconnected but weren't using it */
1044 US_DEBUGP("-- device was not in use\n");
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
));
1054 US_DEBUGP("-- releasing irq handle\n");
1055 result
= usb_unlink_urb(ss
->irq_urb
);
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 */
1077 init_MUTEX(&us_list_semaphore
);
1080 /* register the driver, return -1 if error */
1081 if (usb_register(&storage_driver
) < 0)
1085 printk(KERN_INFO
"USB Mass Storage support registered.\n");
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.
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
)(
1128 kfree(us_list
->extra
);
1132 /* advance the list pointer */
1136 /* unlock the data structures */
1137 up(&us_list_semaphore
);
1140 module_init(usb_stor_init
) ;
1141 module_exit(usb_stor_exit
) ;