1 /* Driver for USB Mass Storage compliant devices
3 * $Id: usb.c,v 1.75 2002/04/22 03:39:43 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 1999-2003 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)
10 * (c) 2003 Alan Stern (stern@rowland.harvard.edu)
13 * (c) 1999 Michael Gee (michael@linuxspecific.com)
15 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
16 * (c) 2000 Yggdrasil Computing, Inc.
18 * This driver is based on the 'USB Mass Storage Class' document. This
19 * describes in detail the protocol used to communicate with such
20 * devices. Clearly, the designers had SCSI and ATAPI commands in
21 * mind when they created this document. The commands are all very
22 * similar to commands in the SCSI-II and ATAPI specifications.
24 * It is important to note that in a number of cases this class
25 * exhibits class-specific exemptions from the USB specification.
26 * Notably the usage of NAK, STALL and ACK differs from the norm, in
27 * that they are used to communicate wait, failed and OK on commands.
29 * Also, for certain devices, the interrupt endpoint is used to convey
30 * status of a command.
32 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
33 * information about this driver.
35 * This program is free software; you can redistribute it and/or modify it
36 * under the terms of the GNU General Public License as published by the
37 * Free Software Foundation; either version 2, or (at your option) any
40 * This program is distributed in the hope that it will be useful, but
41 * WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 * General Public License for more details.
45 * You should have received a copy of the GNU General Public License along
46 * with this program; if not, write to the Free Software Foundation, Inc.,
47 * 675 Mass Ave, Cambridge, MA 02139, USA.
50 #include <linux/config.h>
51 #include <linux/sched.h>
52 #include <linux/errno.h>
53 #include <linux/suspend.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
58 #include <scsi/scsi.h>
59 #include <scsi/scsi_cmnd.h>
60 #include <scsi/scsi_device.h>
64 #include "transport.h"
67 #include "initializers.h"
69 #ifdef CONFIG_USB_STORAGE_USBAT
70 #include "shuttle_usbat.h"
72 #ifdef CONFIG_USB_STORAGE_SDDR09
75 #ifdef CONFIG_USB_STORAGE_SDDR55
78 #ifdef CONFIG_USB_STORAGE_DPCM
81 #ifdef CONFIG_USB_STORAGE_FREECOM
84 #ifdef CONFIG_USB_STORAGE_ISD200
87 #ifdef CONFIG_USB_STORAGE_DATAFAB
90 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
95 /* Some informational data */
96 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
97 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
98 MODULE_LICENSE("GPL");
100 static unsigned int delay_use
= 5;
101 module_param(delay_use
, uint
, S_IRUGO
| S_IWUSR
);
102 MODULE_PARM_DESC(delay_use
, "seconds to delay before using a new device");
105 /* These are used to make sure the module doesn't unload before all the
106 * threads have exited.
108 static atomic_t total_threads
= ATOMIC_INIT(0);
109 static DECLARE_COMPLETION(threads_gone
);
112 static int storage_probe(struct usb_interface
*iface
,
113 const struct usb_device_id
*id
);
115 static void storage_disconnect(struct usb_interface
*iface
);
117 /* The entries in this table, except for final ones here
118 * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
119 * line for line with the entries of us_unsuaul_dev_list[].
122 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
123 vendorName, productName,useProtocol, useTransport, \
124 initFunction, flags) \
125 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
127 static struct usb_device_id storage_usb_ids
[] = {
129 # include "unusual_devs.h"
131 /* Control/Bulk transport for all SubClass values */
132 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_RBC
, US_PR_CB
) },
133 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8020
, US_PR_CB
) },
134 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_QIC
, US_PR_CB
) },
135 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_UFI
, US_PR_CB
) },
136 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8070
, US_PR_CB
) },
137 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_SCSI
, US_PR_CB
) },
139 /* Control/Bulk/Interrupt transport for all SubClass values */
140 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_RBC
, US_PR_CBI
) },
141 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8020
, US_PR_CBI
) },
142 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_QIC
, US_PR_CBI
) },
143 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_UFI
, US_PR_CBI
) },
144 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8070
, US_PR_CBI
) },
145 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_SCSI
, US_PR_CBI
) },
147 /* Bulk-only transport for all SubClass values */
148 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_RBC
, US_PR_BULK
) },
149 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8020
, US_PR_BULK
) },
150 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_QIC
, US_PR_BULK
) },
151 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_UFI
, US_PR_BULK
) },
152 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_8070
, US_PR_BULK
) },
153 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, US_SC_SCSI
, US_PR_BULK
) },
155 /* Terminating entry */
159 MODULE_DEVICE_TABLE (usb
, storage_usb_ids
);
161 /* This is the list of devices we recognize, along with their flag data */
163 /* The vendor name should be kept at eight characters or less, and
164 * the product name should be kept at 16 characters or less. If a device
165 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
166 * normally generated by a device thorugh the INQUIRY response will be
167 * taken from this list, and this is the reason for the above size
168 * restriction. However, if the flag is not present, then you
169 * are free to use as many characters as you like.
173 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
174 vendor_name, product_name, use_protocol, use_transport, \
175 init_function, Flags) \
177 .vendorName = vendor_name, \
178 .productName = product_name, \
179 .useProtocol = use_protocol, \
180 .useTransport = use_transport, \
181 .initFunction = init_function, \
185 static struct us_unusual_dev us_unusual_dev_list
[] = {
186 # include "unusual_devs.h"
188 /* Control/Bulk transport for all SubClass values */
189 { .useProtocol
= US_SC_RBC
,
190 .useTransport
= US_PR_CB
},
191 { .useProtocol
= US_SC_8020
,
192 .useTransport
= US_PR_CB
},
193 { .useProtocol
= US_SC_QIC
,
194 .useTransport
= US_PR_CB
},
195 { .useProtocol
= US_SC_UFI
,
196 .useTransport
= US_PR_CB
},
197 { .useProtocol
= US_SC_8070
,
198 .useTransport
= US_PR_CB
},
199 { .useProtocol
= US_SC_SCSI
,
200 .useTransport
= US_PR_CB
},
202 /* Control/Bulk/Interrupt transport for all SubClass values */
203 { .useProtocol
= US_SC_RBC
,
204 .useTransport
= US_PR_CBI
},
205 { .useProtocol
= US_SC_8020
,
206 .useTransport
= US_PR_CBI
},
207 { .useProtocol
= US_SC_QIC
,
208 .useTransport
= US_PR_CBI
},
209 { .useProtocol
= US_SC_UFI
,
210 .useTransport
= US_PR_CBI
},
211 { .useProtocol
= US_SC_8070
,
212 .useTransport
= US_PR_CBI
},
213 { .useProtocol
= US_SC_SCSI
,
214 .useTransport
= US_PR_CBI
},
216 /* Bulk-only transport for all SubClass values */
217 { .useProtocol
= US_SC_RBC
,
218 .useTransport
= US_PR_BULK
},
219 { .useProtocol
= US_SC_8020
,
220 .useTransport
= US_PR_BULK
},
221 { .useProtocol
= US_SC_QIC
,
222 .useTransport
= US_PR_BULK
},
223 { .useProtocol
= US_SC_UFI
,
224 .useTransport
= US_PR_BULK
},
225 { .useProtocol
= US_SC_8070
,
226 .useTransport
= US_PR_BULK
},
227 { .useProtocol
= US_SC_SCSI
,
228 .useTransport
= US_PR_BULK
},
230 /* Terminating entry */
234 static struct usb_driver usb_storage_driver
= {
235 .owner
= THIS_MODULE
,
236 .name
= "usb-storage",
237 .probe
= storage_probe
,
238 .disconnect
= storage_disconnect
,
239 .id_table
= storage_usb_ids
,
243 * fill_inquiry_response takes an unsigned char array (which must
244 * be at least 36 characters) and populates the vendor name,
245 * product name, and revision fields. Then the array is copied
246 * into the SCSI command's response buffer (oddly enough
247 * called request_buffer). data_len contains the length of the
248 * data array, which again must be at least 36.
251 void fill_inquiry_response(struct us_data
*us
, unsigned char *data
,
252 unsigned int data_len
)
254 if (data_len
<36) // You lose.
257 if(data
[0]&0x20) { /* USB device currently not connected. Return
258 peripheral qualifier 001b ("...however, the
259 physical device is not currently connected
260 to this logical unit") and leave vendor and
261 product identification empty. ("If the target
262 does store some of the INQUIRY data on the
263 device, it may return zeros or ASCII spaces
264 (20h) in those fields until the data is
265 available from the device."). */
268 u16 bcdDevice
= le16_to_cpu(us
->pusb_dev
->descriptor
.bcdDevice
);
269 memcpy(data
+8, us
->unusual_dev
->vendorName
,
270 strlen(us
->unusual_dev
->vendorName
) > 8 ? 8 :
271 strlen(us
->unusual_dev
->vendorName
));
272 memcpy(data
+16, us
->unusual_dev
->productName
,
273 strlen(us
->unusual_dev
->productName
) > 16 ? 16 :
274 strlen(us
->unusual_dev
->productName
));
275 data
[32] = 0x30 + ((bcdDevice
>>12) & 0x0F);
276 data
[33] = 0x30 + ((bcdDevice
>>8) & 0x0F);
277 data
[34] = 0x30 + ((bcdDevice
>>4) & 0x0F);
278 data
[35] = 0x30 + ((bcdDevice
) & 0x0F);
281 usb_stor_set_xfer_buf(data
, data_len
, us
->srb
);
284 static int usb_stor_control_thread(void * __us
)
286 struct us_data
*us
= (struct us_data
*)__us
;
287 struct Scsi_Host
*host
= us_to_host(us
);
292 * This thread doesn't need any user-level access,
293 * so get rid of all our resources.
295 daemonize("usb-storage");
296 current
->flags
|= PF_NOFREEZE
;
299 /* acquire a reference to the host, so it won't be deallocated
300 * until we're ready to exit */
303 /* signal that we've started the thread */
304 complete(&(us
->notify
));
307 US_DEBUGP("*** thread sleeping.\n");
308 if(down_interruptible(&us
->sema
))
311 US_DEBUGP("*** thread awakened.\n");
313 /* lock the device pointers */
314 down(&(us
->dev_semaphore
));
316 /* if the device has disconnected, we are free to exit */
317 if (test_bit(US_FLIDX_DISCONNECTING
, &us
->flags
)) {
318 US_DEBUGP("-- exiting\n");
319 up(&(us
->dev_semaphore
));
323 /* lock access to the state */
326 /* has the command timed out *already* ? */
327 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->flags
)) {
328 us
->srb
->result
= DID_ABORT
<< 16;
334 /* reject the command if the direction indicator
337 if (us
->srb
->sc_data_direction
== DMA_BIDIRECTIONAL
) {
338 US_DEBUGP("UNKNOWN data direction\n");
339 us
->srb
->result
= DID_ERROR
<< 16;
342 /* reject if target != 0 or if LUN is higher than
343 * the maximum known LUN
345 else if (us
->srb
->device
->id
&&
346 !(us
->flags
& US_FL_SCM_MULT_TARG
)) {
347 US_DEBUGP("Bad target number (%d:%d)\n",
348 us
->srb
->device
->id
, us
->srb
->device
->lun
);
349 us
->srb
->result
= DID_BAD_TARGET
<< 16;
352 else if (us
->srb
->device
->lun
> us
->max_lun
) {
353 US_DEBUGP("Bad LUN (%d:%d)\n",
354 us
->srb
->device
->id
, us
->srb
->device
->lun
);
355 us
->srb
->result
= DID_BAD_TARGET
<< 16;
358 /* Handle those devices which need us to fake
359 * their inquiry data */
360 else if ((us
->srb
->cmnd
[0] == INQUIRY
) &&
361 (us
->flags
& US_FL_FIX_INQUIRY
)) {
362 unsigned char data_ptr
[36] = {
363 0x00, 0x80, 0x02, 0x02,
364 0x1F, 0x00, 0x00, 0x00};
366 US_DEBUGP("Faking INQUIRY command\n");
367 fill_inquiry_response(us
, data_ptr
, 36);
368 us
->srb
->result
= SAM_STAT_GOOD
;
371 /* we've got a command, let's do it! */
373 US_DEBUG(usb_stor_show_command(us
->srb
));
374 us
->proto_handler(us
->srb
, us
);
377 /* lock access to the state */
380 /* indicate that the command is done */
381 if (us
->srb
->result
!= DID_ABORT
<< 16) {
382 US_DEBUGP("scsi cmd done, result=0x%x\n",
384 us
->srb
->scsi_done(us
->srb
);
387 US_DEBUGP("scsi command aborted\n");
390 /* If an abort request was received we need to signal that
391 * the abort has finished. The proper test for this is
392 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
393 * a timeout/abort request might be received after all the
394 * USB processing was complete. */
395 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->flags
))
396 complete(&(us
->notify
));
398 /* finished working on this command */
402 /* unlock the device pointers */
403 up(&(us
->dev_semaphore
));
408 /* notify the exit routine that we're actually exiting now
410 * complete()/wait_for_completion() is similar to up()/down(),
411 * except that complete() is safe in the case where the structure
412 * is getting deleted in a parallel mode of execution (i.e. just
413 * after the down() -- that's necessary for the thread-shutdown
416 * complete_and_exit() goes even further than this -- it is safe in
417 * the case that the thread of the caller is going away (not just
418 * the structure) -- this is necessary for the module-remove case.
419 * This is important in preemption kernels, which transfer the flow
420 * of execution immediately upon a complete().
422 complete_and_exit(&threads_gone
, 0);
425 /***********************************************************************
426 * Device probing and disconnecting
427 ***********************************************************************/
429 /* Associate our private data with the USB device */
430 static int associate_dev(struct us_data
*us
, struct usb_interface
*intf
)
432 US_DEBUGP("-- %s\n", __FUNCTION__
);
434 /* Fill in the device-related fields */
435 us
->pusb_dev
= interface_to_usbdev(intf
);
436 us
->pusb_intf
= intf
;
437 us
->ifnum
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
438 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
439 le16_to_cpu(us
->pusb_dev
->descriptor
.idVendor
),
440 le16_to_cpu(us
->pusb_dev
->descriptor
.idProduct
),
441 le16_to_cpu(us
->pusb_dev
->descriptor
.bcdDevice
));
442 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
443 intf
->cur_altsetting
->desc
.bInterfaceSubClass
,
444 intf
->cur_altsetting
->desc
.bInterfaceProtocol
);
446 /* Store our private data in the interface */
447 usb_set_intfdata(intf
, us
);
449 /* Allocate the device-related DMA-mapped buffers */
450 us
->cr
= usb_buffer_alloc(us
->pusb_dev
, sizeof(*us
->cr
),
451 GFP_KERNEL
, &us
->cr_dma
);
453 US_DEBUGP("usb_ctrlrequest allocation failed\n");
457 us
->iobuf
= usb_buffer_alloc(us
->pusb_dev
, US_IOBUF_SIZE
,
458 GFP_KERNEL
, &us
->iobuf_dma
);
460 US_DEBUGP("I/O buffer allocation failed\n");
466 /* Get the unusual_devs entries and the string descriptors */
467 static void get_device_info(struct us_data
*us
, int id_index
)
469 struct usb_device
*dev
= us
->pusb_dev
;
470 struct usb_interface_descriptor
*idesc
=
471 &us
->pusb_intf
->cur_altsetting
->desc
;
472 struct us_unusual_dev
*unusual_dev
= &us_unusual_dev_list
[id_index
];
473 struct usb_device_id
*id
= &storage_usb_ids
[id_index
];
475 /* Store the entries */
476 us
->unusual_dev
= unusual_dev
;
477 us
->subclass
= (unusual_dev
->useProtocol
== US_SC_DEVICE
) ?
478 idesc
->bInterfaceSubClass
:
479 unusual_dev
->useProtocol
;
480 us
->protocol
= (unusual_dev
->useTransport
== US_PR_DEVICE
) ?
481 idesc
->bInterfaceProtocol
:
482 unusual_dev
->useTransport
;
483 us
->flags
= unusual_dev
->flags
;
486 * This flag is only needed when we're in high-speed, so let's
487 * disable it if we're in full-speed
489 if (dev
->speed
!= USB_SPEED_HIGH
)
490 us
->flags
&= ~US_FL_GO_SLOW
;
492 /* Log a message if a non-generic unusual_dev entry contains an
493 * unnecessary subclass or protocol override. This may stimulate
494 * reports from users that will help us remove unneeded entries
495 * from the unusual_devs.h table.
497 if (id
->idVendor
|| id
->idProduct
) {
498 static char *msgs
[3] = {
499 "an unneeded SubClass entry",
500 "an unneeded Protocol entry",
501 "unneeded SubClass and Protocol entries"};
502 struct usb_device_descriptor
*ddesc
= &dev
->descriptor
;
505 if (unusual_dev
->useProtocol
!= US_SC_DEVICE
&&
506 us
->subclass
== idesc
->bInterfaceSubClass
)
508 if (unusual_dev
->useTransport
!= US_PR_DEVICE
&&
509 us
->protocol
== idesc
->bInterfaceProtocol
)
511 if (msg
>= 0 && !(unusual_dev
->flags
& US_FL_NEED_OVERRIDE
))
512 printk(KERN_NOTICE USB_STORAGE
"This device "
513 "(%04x,%04x,%04x S %02x P %02x)"
514 " has %s in unusual_devs.h\n"
515 " Please send a copy of this message to "
516 "<linux-usb-devel@lists.sourceforge.net>\n",
517 le16_to_cpu(ddesc
->idVendor
),
518 le16_to_cpu(ddesc
->idProduct
),
519 le16_to_cpu(ddesc
->bcdDevice
),
520 idesc
->bInterfaceSubClass
,
521 idesc
->bInterfaceProtocol
,
526 /* Get the transport settings */
527 static int get_transport(struct us_data
*us
)
529 switch (us
->protocol
) {
531 us
->transport_name
= "Control/Bulk";
532 us
->transport
= usb_stor_CB_transport
;
533 us
->transport_reset
= usb_stor_CB_reset
;
538 us
->transport_name
= "Control/Bulk/Interrupt";
539 us
->transport
= usb_stor_CBI_transport
;
540 us
->transport_reset
= usb_stor_CB_reset
;
545 us
->transport_name
= "Bulk";
546 us
->transport
= usb_stor_Bulk_transport
;
547 us
->transport_reset
= usb_stor_Bulk_reset
;
550 #ifdef CONFIG_USB_STORAGE_USBAT
551 case US_PR_SCM_ATAPI
:
552 us
->transport_name
= "SCM/ATAPI";
553 us
->transport
= usbat_transport
;
554 us
->transport_reset
= usb_stor_CB_reset
;
559 #ifdef CONFIG_USB_STORAGE_SDDR09
560 case US_PR_EUSB_SDDR09
:
561 us
->transport_name
= "EUSB/SDDR09";
562 us
->transport
= sddr09_transport
;
563 us
->transport_reset
= usb_stor_CB_reset
;
568 #ifdef CONFIG_USB_STORAGE_SDDR55
570 us
->transport_name
= "SDDR55";
571 us
->transport
= sddr55_transport
;
572 us
->transport_reset
= sddr55_reset
;
577 #ifdef CONFIG_USB_STORAGE_DPCM
579 us
->transport_name
= "Control/Bulk-EUSB/SDDR09";
580 us
->transport
= dpcm_transport
;
581 us
->transport_reset
= usb_stor_CB_reset
;
586 #ifdef CONFIG_USB_STORAGE_FREECOM
588 us
->transport_name
= "Freecom";
589 us
->transport
= freecom_transport
;
590 us
->transport_reset
= usb_stor_freecom_reset
;
595 #ifdef CONFIG_USB_STORAGE_DATAFAB
597 us
->transport_name
= "Datafab Bulk-Only";
598 us
->transport
= datafab_transport
;
599 us
->transport_reset
= usb_stor_Bulk_reset
;
604 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
606 us
->transport_name
= "Lexar Jumpshot Control/Bulk";
607 us
->transport
= jumpshot_transport
;
608 us
->transport_reset
= usb_stor_Bulk_reset
;
616 US_DEBUGP("Transport: %s\n", us
->transport_name
);
618 /* fix for single-lun devices */
619 if (us
->flags
& US_FL_SINGLE_LUN
)
624 /* Get the protocol settings */
625 static int get_protocol(struct us_data
*us
)
627 switch (us
->subclass
) {
629 us
->protocol_name
= "Reduced Block Commands (RBC)";
630 us
->proto_handler
= usb_stor_transparent_scsi_command
;
634 us
->protocol_name
= "8020i";
635 us
->proto_handler
= usb_stor_ATAPI_command
;
640 us
->protocol_name
= "QIC-157";
641 us
->proto_handler
= usb_stor_qic157_command
;
646 us
->protocol_name
= "8070i";
647 us
->proto_handler
= usb_stor_ATAPI_command
;
652 us
->protocol_name
= "Transparent SCSI";
653 us
->proto_handler
= usb_stor_transparent_scsi_command
;
657 us
->protocol_name
= "Uniform Floppy Interface (UFI)";
658 us
->proto_handler
= usb_stor_ufi_command
;
661 #ifdef CONFIG_USB_STORAGE_ISD200
663 us
->protocol_name
= "ISD200 ATA/ATAPI";
664 us
->proto_handler
= isd200_ata_command
;
671 US_DEBUGP("Protocol: %s\n", us
->protocol_name
);
675 /* Get the pipe settings */
676 static int get_pipes(struct us_data
*us
)
678 struct usb_host_interface
*altsetting
=
679 us
->pusb_intf
->cur_altsetting
;
681 struct usb_endpoint_descriptor
*ep
;
682 struct usb_endpoint_descriptor
*ep_in
= NULL
;
683 struct usb_endpoint_descriptor
*ep_out
= NULL
;
684 struct usb_endpoint_descriptor
*ep_int
= NULL
;
687 * Find the endpoints we need.
688 * We are expecting a minimum of 2 endpoints - in and out (bulk).
689 * An optional interrupt is OK (necessary for CBI protocol).
690 * We will ignore any others.
692 for (i
= 0; i
< altsetting
->desc
.bNumEndpoints
; i
++) {
693 ep
= &altsetting
->endpoint
[i
].desc
;
695 /* Is it a BULK endpoint? */
696 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
697 == USB_ENDPOINT_XFER_BULK
) {
698 /* BULK in or out? */
699 if (ep
->bEndpointAddress
& USB_DIR_IN
)
705 /* Is it an interrupt endpoint? */
706 else if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
707 == USB_ENDPOINT_XFER_INT
) {
712 if (!ep_in
|| !ep_out
|| (us
->protocol
== US_PR_CBI
&& !ep_int
)) {
713 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
717 /* Calculate and store the pipe values */
718 us
->send_ctrl_pipe
= usb_sndctrlpipe(us
->pusb_dev
, 0);
719 us
->recv_ctrl_pipe
= usb_rcvctrlpipe(us
->pusb_dev
, 0);
720 us
->send_bulk_pipe
= usb_sndbulkpipe(us
->pusb_dev
,
721 ep_out
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
722 us
->recv_bulk_pipe
= usb_rcvbulkpipe(us
->pusb_dev
,
723 ep_in
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
725 us
->recv_intr_pipe
= usb_rcvintpipe(us
->pusb_dev
,
726 ep_int
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
727 us
->ep_bInterval
= ep_int
->bInterval
;
732 /* Initialize all the dynamic resources we need */
733 static int usb_stor_acquire_resources(struct us_data
*us
)
737 us
->current_urb
= usb_alloc_urb(0, GFP_KERNEL
);
738 if (!us
->current_urb
) {
739 US_DEBUGP("URB allocation failed\n");
743 /* Lock the device while we carry out the next two operations */
744 down(&us
->dev_semaphore
);
746 /* For bulk-only devices, determine the max LUN value */
747 if (us
->protocol
== US_PR_BULK
) {
748 p
= usb_stor_Bulk_max_lun(us
);
750 up(&us
->dev_semaphore
);
756 /* Just before we start our control thread, initialize
757 * the device if it needs initialization */
758 if (us
->unusual_dev
->initFunction
)
759 us
->unusual_dev
->initFunction(us
);
761 up(&us
->dev_semaphore
);
763 /* Start up our control thread */
764 p
= kernel_thread(usb_stor_control_thread
, us
, CLONE_VM
);
766 printk(KERN_WARNING USB_STORAGE
767 "Unable to start control thread\n");
771 atomic_inc(&total_threads
);
773 /* Wait for the thread to start */
774 wait_for_completion(&(us
->notify
));
779 /* Release all our dynamic resources */
780 static void usb_stor_release_resources(struct us_data
*us
)
782 US_DEBUGP("-- %s\n", __FUNCTION__
);
784 /* Tell the control thread to exit. The SCSI host must
785 * already have been removed so it won't try to queue
788 US_DEBUGP("-- sending exit command to thread\n");
791 /* Call the destructor routine, if it exists */
792 if (us
->extra_destructor
) {
793 US_DEBUGP("-- calling extra_destructor()\n");
794 us
->extra_destructor(us
->extra
);
797 /* Free the extra data and the URB */
799 usb_free_urb(us
->current_urb
);
802 /* Dissociate from the USB device */
803 static void dissociate_dev(struct us_data
*us
)
805 US_DEBUGP("-- %s\n", __FUNCTION__
);
807 /* Free the device-related DMA-mapped buffers */
809 usb_buffer_free(us
->pusb_dev
, sizeof(*us
->cr
), us
->cr
,
812 usb_buffer_free(us
->pusb_dev
, US_IOBUF_SIZE
, us
->iobuf
,
815 /* Remove our private data from the interface */
816 usb_set_intfdata(us
->pusb_intf
, NULL
);
819 /* Thread to carry out delayed SCSI-device scanning */
820 static int usb_stor_scan_thread(void * __us
)
822 struct us_data
*us
= (struct us_data
*)__us
;
825 * This thread doesn't need any user-level access,
826 * so get rid of all our resources.
829 daemonize("usb-stor-scan");
832 /* Acquire a reference to the host, so it won't be deallocated
833 * until we're ready to exit */
834 scsi_host_get(us_to_host(us
));
836 /* Signal that we've started the thread */
837 complete(&(us
->notify
));
840 "usb-storage: device found at %d\n", us
->pusb_dev
->devnum
);
842 /* Wait for the timeout to expire or for a disconnect */
844 printk(KERN_DEBUG
"usb-storage: waiting for device "
845 "to settle before scanning\n");
847 wait_event_interruptible_timeout(us
->delay_wait
,
848 test_bit(US_FLIDX_DISCONNECTING
, &us
->flags
),
850 if (current
->flags
& PF_FREEZE
) {
851 refrigerator(PF_FREEZE
);
856 /* If the device is still connected, perform the scanning */
857 if (!test_bit(US_FLIDX_DISCONNECTING
, &us
->flags
)) {
858 scsi_scan_host(us_to_host(us
));
859 printk(KERN_DEBUG
"usb-storage: device scan complete\n");
861 /* Should we unbind if no devices were detected? */
864 scsi_host_put(us_to_host(us
));
865 complete_and_exit(&threads_gone
, 0);
869 /* Probe to see if we can drive a newly-connected USB device */
870 static int storage_probe(struct usb_interface
*intf
,
871 const struct usb_device_id
*id
)
873 struct Scsi_Host
*host
;
875 const int id_index
= id
- storage_usb_ids
;
878 US_DEBUGP("USB Mass Storage device detected\n");
881 * Ask the SCSI layer to allocate a host structure, with extra
882 * space at the end for our private us_data structure.
884 host
= scsi_host_alloc(&usb_stor_host_template
, sizeof(*us
));
886 printk(KERN_WARNING USB_STORAGE
887 "Unable to allocate the scsi host\n");
891 us
= host_to_us(host
);
892 memset(us
, 0, sizeof(struct us_data
));
893 init_MUTEX(&(us
->dev_semaphore
));
894 init_MUTEX_LOCKED(&(us
->sema
));
895 init_completion(&(us
->notify
));
896 init_waitqueue_head(&us
->delay_wait
);
898 /* Associate the us_data structure with the USB device */
899 result
= associate_dev(us
, intf
);
904 * Get the unusual_devs entries and the descriptors
906 * id_index is calculated in the declaration to be the index number
907 * of the match from the usb_device_id table, so we can find the
908 * corresponding entry in the private table.
910 get_device_info(us
, id_index
);
912 #ifdef CONFIG_USB_STORAGE_SDDR09
913 if (us
->protocol
== US_PR_EUSB_SDDR09
||
914 us
->protocol
== US_PR_DPCM_USB
) {
915 /* set the configuration -- STALL is an acceptable response here */
916 if (us
->pusb_dev
->actconfig
->desc
.bConfigurationValue
!= 1) {
917 US_DEBUGP("active config #%d != 1 ??\n", us
->pusb_dev
918 ->actconfig
->desc
.bConfigurationValue
);
921 result
= usb_reset_configuration(us
->pusb_dev
);
923 US_DEBUGP("Result of usb_reset_configuration is %d\n", result
);
924 if (result
== -EPIPE
) {
925 US_DEBUGP("-- stall on control interface\n");
926 } else if (result
!= 0) {
927 /* it's not a stall, but another error -- time to bail */
928 US_DEBUGP("-- Unknown error. Rejecting device\n");
934 /* Get the transport, protocol, and pipe settings */
935 result
= get_transport(us
);
938 result
= get_protocol(us
);
941 result
= get_pipes(us
);
945 /* Acquire all the other resources and add the host */
946 result
= usb_stor_acquire_resources(us
);
949 result
= scsi_add_host(host
, &intf
->dev
);
951 printk(KERN_WARNING USB_STORAGE
952 "Unable to add the scsi host\n");
956 /* Start up the thread for delayed SCSI-device scanning */
957 result
= kernel_thread(usb_stor_scan_thread
, us
, CLONE_VM
);
959 printk(KERN_WARNING USB_STORAGE
960 "Unable to start the device-scanning thread\n");
961 scsi_remove_host(host
);
964 atomic_inc(&total_threads
);
966 /* Wait for the thread to start */
967 wait_for_completion(&(us
->notify
));
971 /* We come here if there are any problems */
973 US_DEBUGP("storage_probe() failed\n");
974 set_bit(US_FLIDX_DISCONNECTING
, &us
->flags
);
975 usb_stor_release_resources(us
);
981 /* Handle a disconnect event from the USB core */
982 static void storage_disconnect(struct usb_interface
*intf
)
984 struct us_data
*us
= usb_get_intfdata(intf
);
986 US_DEBUGP("storage_disconnect() called\n");
988 /* Prevent new USB transfers, stop the current command, and
989 * interrupt a SCSI-scan or device-reset delay */
990 set_bit(US_FLIDX_DISCONNECTING
, &us
->flags
);
991 usb_stor_stop_transport(us
);
992 wake_up(&us
->delay_wait
);
994 /* It doesn't matter if the SCSI-scanning thread is still running.
995 * The thread will exit when it sees the DISCONNECTING flag. */
997 /* Wait for the current command to finish, then remove the host */
998 down(&us
->dev_semaphore
);
999 up(&us
->dev_semaphore
);
1000 scsi_remove_host(us_to_host(us
));
1002 /* Wait for everything to become idle and release all our resources */
1003 usb_stor_release_resources(us
);
1006 /* Drop our reference to the host; the SCSI core will free it
1007 * (and "us" along with it) when the refcount becomes 0. */
1008 scsi_host_put(us_to_host(us
));
1011 /***********************************************************************
1012 * Initialization and registration
1013 ***********************************************************************/
1015 static int __init
usb_stor_init(void)
1018 printk(KERN_INFO
"Initializing USB Mass Storage driver...\n");
1020 /* register the driver, return usb_register return code if error */
1021 retval
= usb_register(&usb_storage_driver
);
1023 printk(KERN_INFO
"USB Mass Storage support registered.\n");
1028 static void __exit
usb_stor_exit(void)
1030 US_DEBUGP("usb_stor_exit() called\n");
1032 /* Deregister the driver
1033 * This will cause disconnect() to be called for each
1036 US_DEBUGP("-- calling usb_deregister()\n");
1037 usb_deregister(&usb_storage_driver
) ;
1039 /* Don't return until all of our control and scanning threads
1040 * have exited. Since each thread signals threads_gone as its
1041 * last act, we have to call wait_for_completion the right number
1044 while (atomic_read(&total_threads
) > 0) {
1045 wait_for_completion(&threads_gone
);
1046 atomic_dec(&total_threads
);
1050 module_init(usb_stor_init
);
1051 module_exit(usb_stor_exit
);