MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / storage / usb.c
blob894cb09100c8e5214cbd66216cc92ee3114b9307
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)
12 * Initial work by:
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
38 * later version.
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>
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_device.h>
58 #include "usb.h"
59 #include "scsiglue.h"
60 #include "transport.h"
61 #include "protocol.h"
62 #include "debug.h"
63 #include "initializers.h"
65 #ifdef CONFIG_USB_STORAGE_HP8200e
66 #include "shuttle_usbat.h"
67 #endif
68 #ifdef CONFIG_USB_STORAGE_SDDR09
69 #include "sddr09.h"
70 #endif
71 #ifdef CONFIG_USB_STORAGE_SDDR55
72 #include "sddr55.h"
73 #endif
74 #ifdef CONFIG_USB_STORAGE_DPCM
75 #include "dpcm.h"
76 #endif
77 #ifdef CONFIG_USB_STORAGE_FREECOM
78 #include "freecom.h"
79 #endif
80 #ifdef CONFIG_USB_STORAGE_ISD200
81 #include "isd200.h"
82 #endif
83 #ifdef CONFIG_USB_STORAGE_DATAFAB
84 #include "datafab.h"
85 #endif
86 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
87 #include "jumpshot.h"
88 #endif
91 #include <linux/module.h>
92 #include <linux/init.h>
93 #include <linux/slab.h>
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 int storage_probe(struct usb_interface *iface,
101 const struct usb_device_id *id);
103 static void storage_disconnect(struct usb_interface *iface);
105 /* The entries in this table, except for final ones here
106 * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
107 * line for line with the entries of us_unsuaul_dev_list[].
110 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
111 vendorName, productName,useProtocol, useTransport, \
112 initFunction, flags) \
113 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
115 static struct usb_device_id storage_usb_ids [] = {
117 # include "unusual_devs.h"
118 #undef UNUSUAL_DEV
119 /* Control/Bulk transport for all SubClass values */
120 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CB) },
121 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CB) },
122 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CB) },
123 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CB) },
124 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CB) },
125 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CB) },
127 /* Control/Bulk/Interrupt transport for all SubClass values */
128 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CBI) },
129 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CBI) },
130 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CBI) },
131 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CBI) },
132 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CBI) },
133 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CBI) },
135 /* Bulk-only transport for all SubClass values */
136 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_BULK) },
137 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_BULK) },
138 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_BULK) },
139 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_BULK) },
140 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_BULK) },
141 #if !defined(CONFIG_BLK_DEV_UB) && !defined(CONFIG_BLK_DEV_UB_MODULE)
142 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
143 #endif
145 /* Terminating entry */
149 MODULE_DEVICE_TABLE (usb, storage_usb_ids);
151 /* This is the list of devices we recognize, along with their flag data */
153 /* The vendor name should be kept at eight characters or less, and
154 * the product name should be kept at 16 characters or less. If a device
155 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
156 * normally generated by a device thorugh the INQUIRY response will be
157 * taken from this list, and this is the reason for the above size
158 * restriction. However, if the flag is not present, then you
159 * are free to use as many characters as you like.
162 #undef UNUSUAL_DEV
163 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
164 vendor_name, product_name, use_protocol, use_transport, \
165 init_function, Flags) \
167 .vendorName = vendor_name, \
168 .productName = product_name, \
169 .useProtocol = use_protocol, \
170 .useTransport = use_transport, \
171 .initFunction = init_function, \
172 .flags = Flags, \
175 static struct us_unusual_dev us_unusual_dev_list[] = {
176 # include "unusual_devs.h"
177 # undef UNUSUAL_DEV
178 /* Control/Bulk transport for all SubClass values */
179 { .useProtocol = US_SC_RBC,
180 .useTransport = US_PR_CB},
181 { .useProtocol = US_SC_8020,
182 .useTransport = US_PR_CB},
183 { .useProtocol = US_SC_QIC,
184 .useTransport = US_PR_CB},
185 { .useProtocol = US_SC_UFI,
186 .useTransport = US_PR_CB},
187 { .useProtocol = US_SC_8070,
188 .useTransport = US_PR_CB},
189 { .useProtocol = US_SC_SCSI,
190 .useTransport = US_PR_CB},
192 /* Control/Bulk/Interrupt transport for all SubClass values */
193 { .useProtocol = US_SC_RBC,
194 .useTransport = US_PR_CBI},
195 { .useProtocol = US_SC_8020,
196 .useTransport = US_PR_CBI},
197 { .useProtocol = US_SC_QIC,
198 .useTransport = US_PR_CBI},
199 { .useProtocol = US_SC_UFI,
200 .useTransport = US_PR_CBI},
201 { .useProtocol = US_SC_8070,
202 .useTransport = US_PR_CBI},
203 { .useProtocol = US_SC_SCSI,
204 .useTransport = US_PR_CBI},
206 /* Bulk-only transport for all SubClass values */
207 { .useProtocol = US_SC_RBC,
208 .useTransport = US_PR_BULK},
209 { .useProtocol = US_SC_8020,
210 .useTransport = US_PR_BULK},
211 { .useProtocol = US_SC_QIC,
212 .useTransport = US_PR_BULK},
213 { .useProtocol = US_SC_UFI,
214 .useTransport = US_PR_BULK},
215 { .useProtocol = US_SC_8070,
216 .useTransport = US_PR_BULK},
217 #if !defined(CONFIG_BLK_DEV_UB) && !defined(CONFIG_BLK_DEV_UB_MODULE)
218 { .useProtocol = US_SC_SCSI,
219 .useTransport = US_PR_BULK},
220 #endif
222 /* Terminating entry */
223 { NULL }
226 #if (defined CONFIG_ARCH_IA241_32128)||(defined CONFIG_ARCH_IA241_16128)// add by Victor Yu. 05-22-2007
227 #define CONFIG_ARCH_IA241
228 #endif
230 struct usb_driver usb_storage_driver = {
231 .owner = THIS_MODULE,
232 .name = "usb-storage",
233 .probe = storage_probe,
234 .disconnect = storage_disconnect,
235 .id_table = storage_usb_ids,
239 * fill_inquiry_response takes an unsigned char array (which must
240 * be at least 36 characters) and populates the vendor name,
241 * product name, and revision fields. Then the array is copied
242 * into the SCSI command's response buffer (oddly enough
243 * called request_buffer). data_len contains the length of the
244 * data array, which again must be at least 36.
247 void fill_inquiry_response(struct us_data *us, unsigned char *data,
248 unsigned int data_len)
250 if (data_len<36) // You lose.
251 return;
253 if(data[0]&0x20) { /* USB device currently not connected. Return
254 peripheral qualifier 001b ("...however, the
255 physical device is not currently connected
256 to this logical unit") and leave vendor and
257 product identification empty. ("If the target
258 does store some of the INQUIRY data on the
259 device, it may return zeros or ASCII spaces
260 (20h) in those fields until the data is
261 available from the device."). */
262 memset(data+8,0,28);
263 } else {
264 memcpy(data+8, us->unusual_dev->vendorName,
265 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
266 strlen(us->unusual_dev->vendorName));
267 memcpy(data+16, us->unusual_dev->productName,
268 strlen(us->unusual_dev->productName) > 16 ? 16 :
269 strlen(us->unusual_dev->productName));
270 data[32] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>12) & 0x0F);
271 data[33] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>8) & 0x0F);
272 data[34] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice>>4) & 0x0F);
273 data[35] = 0x30 + ((us->pusb_dev->descriptor.bcdDevice) & 0x0F);
276 usb_stor_set_xfer_buf(data, data_len, us->srb);
279 static int usb_stor_control_thread(void * __us)
281 struct us_data *us = (struct us_data *)__us;
282 struct Scsi_Host *host = us->host;
284 lock_kernel();
287 * This thread doesn't need any user-level access,
288 * so get rid of all our resources.
290 daemonize("usb-storage");
292 current->flags |= PF_NOFREEZE;
294 unlock_kernel();
296 /* signal that we've started the thread */
297 complete(&(us->notify));
299 for(;;) {
300 US_DEBUGP("*** thread sleeping.\n");
301 if(down_interruptible(&us->sema))
302 break;
304 US_DEBUGP("*** thread awakened.\n");
306 /* lock the device pointers */
307 down(&(us->dev_semaphore));
309 /* if us->srb is NULL, we are being asked to exit */
310 if (us->srb == NULL) {
311 US_DEBUGP("-- exit command received\n");
312 up(&(us->dev_semaphore));
313 break;
316 /* lock access to the state */
317 scsi_lock(host);
319 /* has the command been aborted *already* ? */
320 if (us->sm_state == US_STATE_ABORTING) {
321 us->srb->result = DID_ABORT << 16;
322 goto SkipForAbort;
325 /* don't do anything if we are disconnecting */
326 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
327 US_DEBUGP("No command during disconnect\n");
328 goto SkipForDisconnect;
331 /* set the state and release the lock */
332 us->sm_state = US_STATE_RUNNING;
333 scsi_unlock(host);
335 /* reject the command if the direction indicator
336 * is UNKNOWN
338 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
339 US_DEBUGP("UNKNOWN data direction\n");
340 us->srb->result = DID_ERROR << 16;
343 /* reject if target != 0 or if LUN is higher than
344 * the maximum known LUN
346 else if (us->srb->device->id &&
347 !(us->flags & US_FL_SCM_MULT_TARG)) {
348 US_DEBUGP("Bad target number (%d:%d)\n",
349 us->srb->device->id, us->srb->device->lun);
350 us->srb->result = DID_BAD_TARGET << 16;
353 else if (us->srb->device->lun > us->max_lun) {
354 US_DEBUGP("Bad LUN (%d:%d)\n",
355 us->srb->device->id, us->srb->device->lun);
356 us->srb->result = DID_BAD_TARGET << 16;
359 /* Handle those devices which need us to fake
360 * their inquiry data */
361 else if ((us->srb->cmnd[0] == INQUIRY) &&
362 (us->flags & US_FL_FIX_INQUIRY)) {
363 unsigned char data_ptr[36] = {
364 0x00, 0x80, 0x02, 0x02,
365 0x1F, 0x00, 0x00, 0x00};
367 US_DEBUGP("Faking INQUIRY command\n");
368 fill_inquiry_response(us, data_ptr, 36);
369 us->srb->result = SAM_STAT_GOOD;
372 /* we've got a command, let's do it! */
373 else {
374 US_DEBUG(usb_stor_show_command(us->srb));
375 us->proto_handler(us->srb, us);
378 /* lock access to the state */
379 scsi_lock(host);
381 /* indicate that the command is done */
382 if (us->srb->result != DID_ABORT << 16) {
383 US_DEBUGP("scsi cmd done, result=0x%x\n",
384 us->srb->result);
385 us->srb->scsi_done(us->srb);
386 } else {
387 SkipForAbort:
388 US_DEBUGP("scsi command aborted\n");
391 /* If an abort request was received we need to signal that
392 * the abort has finished. The proper test for this is
393 * sm_state == US_STATE_ABORTING, not srb->result == DID_ABORT,
394 * because an abort request might be received after all the
395 * USB processing was complete. */
396 if (us->sm_state == US_STATE_ABORTING)
397 complete(&(us->notify));
399 /* empty the queue, reset the state, and release the lock */
400 SkipForDisconnect:
401 us->srb = NULL;
402 us->sm_state = US_STATE_IDLE;
403 scsi_unlock(host);
405 /* unlock the device pointers */
406 up(&(us->dev_semaphore));
407 } /* for (;;) */
409 /* notify the exit routine that we're actually exiting now
411 * complete()/wait_for_completion() is similar to up()/down(),
412 * except that complete() is safe in the case where the structure
413 * is getting deleted in a parallel mode of execution (i.e. just
414 * after the down() -- that's necessary for the thread-shutdown
415 * case.
417 * complete_and_exit() goes even further than this -- it is safe in
418 * the case that the thread of the caller is going away (not just
419 * the structure) -- this is necessary for the module-remove case.
420 * This is important in preemption kernels, which transfer the flow
421 * of execution immediately upon a complete().
423 complete_and_exit(&(us->notify), 0);
426 /***********************************************************************
427 * Device probing and disconnecting
428 ***********************************************************************/
430 /* Associate our private data with the USB device */
431 static int associate_dev(struct us_data *us, struct usb_interface *intf)
433 US_DEBUGP("-- %s\n", __FUNCTION__);
435 /* Fill in the device-related fields */
436 us->pusb_dev = interface_to_usbdev(intf);
437 us->pusb_intf = intf;
438 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
439 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
440 us->pusb_dev->descriptor.idVendor,
441 us->pusb_dev->descriptor.idProduct,
442 us->pusb_dev->descriptor.bcdDevice);
443 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
444 intf->cur_altsetting->desc.bInterfaceSubClass,
445 intf->cur_altsetting->desc.bInterfaceProtocol);
447 /* Store our private data in the interface */
448 usb_set_intfdata(intf, us);
450 /* Allocate the device-related DMA-mapped buffers */
451 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
452 GFP_KERNEL, &us->cr_dma);
453 if (!us->cr) {
454 US_DEBUGP("usb_ctrlrequest allocation failed\n");
455 return -ENOMEM;
458 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
459 GFP_KERNEL, &us->iobuf_dma);
460 if (!us->iobuf) {
461 US_DEBUGP("I/O buffer allocation failed\n");
462 return -ENOMEM;
464 return 0;
467 /* Get the unusual_devs entries and the string descriptors */
468 #if 0 // mask by Victor Yu. 06-13-2007
469 static void get_device_info(struct us_data *us, int id_index)
470 #else // add by Victor Yu. 06-13-2007
471 static int get_device_info(struct us_data *us, int id_index)
472 #endif
474 struct usb_device *dev = us->pusb_dev;
475 struct usb_interface_descriptor *idesc =
476 &us->pusb_intf->cur_altsetting->desc;
477 struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
478 struct usb_device_id *id = &storage_usb_ids[id_index];
480 /* Store the entries */
481 us->unusual_dev = unusual_dev;
482 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
483 idesc->bInterfaceSubClass :
484 unusual_dev->useProtocol;
485 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
486 idesc->bInterfaceProtocol :
487 unusual_dev->useTransport;
488 us->flags = unusual_dev->flags;
490 #if 1 // add by Victor Yu. 06-13-2007
491 if (us->flags & US_FL_IGNORE_DEVICE) {
492 printk(KERN_INFO USB_STORAGE "device ignored\n");
493 return -ENODEV;
495 #endif
497 /* Log a message if a non-generic unusual_dev entry contains an
498 * unnecessary subclass or protocol override. This may stimulate
499 * reports from users that will help us remove unneeded entries
500 * from the unusual_devs.h table.
502 if (id->idVendor || id->idProduct) {
503 static char *msgs[3] = {
504 "an unneeded SubClass entry",
505 "an unneeded Protocol entry",
506 "unneeded SubClass and Protocol entries"};
507 struct usb_device_descriptor *ddesc = &dev->descriptor;
508 int msg = -1;
510 if (unusual_dev->useProtocol != US_SC_DEVICE &&
511 us->subclass == idesc->bInterfaceSubClass)
512 msg += 1;
513 if (unusual_dev->useTransport != US_PR_DEVICE &&
514 us->protocol == idesc->bInterfaceProtocol)
515 msg += 2;
516 if (msg >= 0 && !(unusual_dev->flags & US_FL_NEED_OVERRIDE))
517 printk(KERN_NOTICE USB_STORAGE "This device "
518 "(%04x,%04x,%04x S %02x P %02x)"
519 " has %s in unusual_devs.h\n"
520 " Please send a copy of this message to "
521 "<linux-usb-devel@lists.sourceforge.net>\n",
522 ddesc->idVendor, ddesc->idProduct,
523 ddesc->bcdDevice,
524 idesc->bInterfaceSubClass,
525 idesc->bInterfaceProtocol,
526 msgs[msg]);
529 /* Read the device's string descriptors */
530 if (dev->descriptor.iManufacturer)
531 usb_string(dev, dev->descriptor.iManufacturer,
532 us->vendor, sizeof(us->vendor));
533 if (dev->descriptor.iProduct)
534 usb_string(dev, dev->descriptor.iProduct,
535 us->product, sizeof(us->product));
536 if (dev->descriptor.iSerialNumber)
537 usb_string(dev, dev->descriptor.iSerialNumber,
538 us->serial, sizeof(us->serial));
540 /* Use the unusual_dev strings if the device didn't provide them */
541 if (strlen(us->vendor) == 0) {
542 if (unusual_dev->vendorName)
543 strlcpy(us->vendor, unusual_dev->vendorName,
544 sizeof(us->vendor));
545 else
546 strcpy(us->vendor, "Unknown");
548 if (strlen(us->product) == 0) {
549 if (unusual_dev->productName)
550 strlcpy(us->product, unusual_dev->productName,
551 sizeof(us->product));
552 else
553 strcpy(us->product, "Unknown");
555 if (strlen(us->serial) == 0)
556 strcpy(us->serial, "None");
558 US_DEBUGP("Vendor: %s, Product: %s\n", us->vendor, us->product);
559 #if 1 // add by Victor Yu. 06-13-2007
560 return 0;
561 #endif
564 /* Get the transport settings */
565 static int get_transport(struct us_data *us)
567 switch (us->protocol) {
568 case US_PR_CB:
569 us->transport_name = "Control/Bulk";
570 us->transport = usb_stor_CB_transport;
571 us->transport_reset = usb_stor_CB_reset;
572 us->max_lun = 7;
573 break;
575 case US_PR_CBI:
576 us->transport_name = "Control/Bulk/Interrupt";
577 us->transport = usb_stor_CBI_transport;
578 us->transport_reset = usb_stor_CB_reset;
579 us->max_lun = 7;
580 break;
582 case US_PR_BULK:
583 us->transport_name = "Bulk";
584 us->transport = usb_stor_Bulk_transport;
585 us->transport_reset = usb_stor_Bulk_reset;
586 break;
588 #ifdef CONFIG_USB_STORAGE_HP8200e
589 case US_PR_SCM_ATAPI:
590 us->transport_name = "SCM/ATAPI";
591 us->transport = hp8200e_transport;
592 us->transport_reset = usb_stor_CB_reset;
593 us->max_lun = 1;
594 break;
595 #endif
597 #ifdef CONFIG_USB_STORAGE_SDDR09
598 case US_PR_EUSB_SDDR09:
599 us->transport_name = "EUSB/SDDR09";
600 us->transport = sddr09_transport;
601 us->transport_reset = usb_stor_CB_reset;
602 us->max_lun = 0;
603 break;
604 #endif
606 #ifdef CONFIG_USB_STORAGE_SDDR55
607 case US_PR_SDDR55:
608 us->transport_name = "SDDR55";
609 us->transport = sddr55_transport;
610 us->transport_reset = sddr55_reset;
611 us->max_lun = 0;
612 break;
613 #endif
615 #ifdef CONFIG_USB_STORAGE_DPCM
616 case US_PR_DPCM_USB:
617 us->transport_name = "Control/Bulk-EUSB/SDDR09";
618 us->transport = dpcm_transport;
619 us->transport_reset = usb_stor_CB_reset;
620 us->max_lun = 1;
621 break;
622 #endif
624 #ifdef CONFIG_USB_STORAGE_FREECOM
625 case US_PR_FREECOM:
626 us->transport_name = "Freecom";
627 us->transport = freecom_transport;
628 us->transport_reset = usb_stor_freecom_reset;
629 us->max_lun = 0;
630 break;
631 #endif
633 #ifdef CONFIG_USB_STORAGE_DATAFAB
634 case US_PR_DATAFAB:
635 us->transport_name = "Datafab Bulk-Only";
636 us->transport = datafab_transport;
637 us->transport_reset = usb_stor_Bulk_reset;
638 us->max_lun = 1;
639 break;
640 #endif
642 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
643 case US_PR_JUMPSHOT:
644 us->transport_name = "Lexar Jumpshot Control/Bulk";
645 us->transport = jumpshot_transport;
646 us->transport_reset = usb_stor_Bulk_reset;
647 us->max_lun = 1;
648 break;
649 #endif
651 default:
652 return -EIO;
654 US_DEBUGP("Transport: %s\n", us->transport_name);
656 /* fix for single-lun devices */
657 if (us->flags & US_FL_SINGLE_LUN)
658 us->max_lun = 0;
659 return 0;
662 /* Get the protocol settings */
663 static int get_protocol(struct us_data *us)
665 switch (us->subclass) {
666 case US_SC_RBC:
667 us->protocol_name = "Reduced Block Commands (RBC)";
668 us->proto_handler = usb_stor_transparent_scsi_command;
669 break;
671 case US_SC_8020:
672 us->protocol_name = "8020i";
673 us->proto_handler = usb_stor_ATAPI_command;
674 us->max_lun = 0;
675 break;
677 case US_SC_QIC:
678 us->protocol_name = "QIC-157";
679 us->proto_handler = usb_stor_qic157_command;
680 us->max_lun = 0;
681 break;
683 case US_SC_8070:
684 us->protocol_name = "8070i";
685 us->proto_handler = usb_stor_ATAPI_command;
686 us->max_lun = 0;
687 break;
689 case US_SC_SCSI:
690 us->protocol_name = "Transparent SCSI";
691 us->proto_handler = usb_stor_transparent_scsi_command;
692 break;
694 case US_SC_UFI:
695 us->protocol_name = "Uniform Floppy Interface (UFI)";
696 us->proto_handler = usb_stor_ufi_command;
697 break;
699 #ifdef CONFIG_USB_STORAGE_ISD200
700 case US_SC_ISD200:
701 us->protocol_name = "ISD200 ATA/ATAPI";
702 us->proto_handler = isd200_ata_command;
703 break;
704 #endif
706 default:
707 return -EIO;
709 US_DEBUGP("Protocol: %s\n", us->protocol_name);
710 return 0;
713 /* Get the pipe settings */
714 static int get_pipes(struct us_data *us)
716 struct usb_host_interface *altsetting =
717 us->pusb_intf->cur_altsetting;
718 int i;
719 struct usb_endpoint_descriptor *ep;
720 struct usb_endpoint_descriptor *ep_in = NULL;
721 struct usb_endpoint_descriptor *ep_out = NULL;
722 struct usb_endpoint_descriptor *ep_int = NULL;
725 * Find the endpoints we need.
726 * We are expecting a minimum of 2 endpoints - in and out (bulk).
727 * An optional interrupt is OK (necessary for CBI protocol).
728 * We will ignore any others.
730 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
731 ep = &altsetting->endpoint[i].desc;
733 /* Is it a BULK endpoint? */
734 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
735 == USB_ENDPOINT_XFER_BULK) {
736 /* BULK in or out? */
737 if (ep->bEndpointAddress & USB_DIR_IN)
738 ep_in = ep;
739 else
740 ep_out = ep;
743 /* Is it an interrupt endpoint? */
744 else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
745 == USB_ENDPOINT_XFER_INT) {
746 ep_int = ep;
750 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
751 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
752 return -EIO;
755 /* Calculate and store the pipe values */
756 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
757 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
758 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
759 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
760 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
761 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
762 if (ep_int) {
763 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
764 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
765 us->ep_bInterval = ep_int->bInterval;
767 return 0;
770 /* Initialize all the dynamic resources we need */
771 static int usb_stor_acquire_resources(struct us_data *us)
773 int p;
775 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
776 if (!us->current_urb) {
777 US_DEBUGP("URB allocation failed\n");
778 return -ENOMEM;
781 /* Lock the device while we carry out the next two operations */
782 down(&us->dev_semaphore);
784 /* For bulk-only devices, determine the max LUN value */
785 if (us->protocol == US_PR_BULK) {
786 p = usb_stor_Bulk_max_lun(us);
787 if (p < 0) {
788 up(&us->dev_semaphore);
789 return p;
791 us->max_lun = p;
794 /* Just before we start our control thread, initialize
795 * the device if it needs initialization */
796 if (us->unusual_dev->initFunction)
797 us->unusual_dev->initFunction(us);
799 up(&us->dev_semaphore);
802 * Since this is a new device, we need to register a SCSI
803 * host definition with the higher SCSI layers.
805 us->host = scsi_host_alloc(&usb_stor_host_template, sizeof(us));
806 if (!us->host) {
807 printk(KERN_WARNING USB_STORAGE
808 "Unable to allocate the scsi host\n");
809 return -EBUSY;
812 /* Set the hostdata to prepare for scanning */
813 us->host->hostdata[0] = (unsigned long) us;
815 /* Start up our control thread */
816 us->sm_state = US_STATE_IDLE;
817 p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
818 if (p < 0) {
819 printk(KERN_WARNING USB_STORAGE
820 "Unable to start control thread\n");
821 return p;
823 us->pid = p;
825 /* Wait for the thread to start */
826 wait_for_completion(&(us->notify));
828 return 0;
831 /* Release all our dynamic resources */
832 void usb_stor_release_resources(struct us_data *us)
834 US_DEBUGP("-- %s\n", __FUNCTION__);
836 /* Kill the control thread. The SCSI host must already have been
837 * removed so it won't try to queue any more commands.
839 if (us->pid) {
841 /* Wait for the thread to be idle */
842 down(&us->dev_semaphore);
843 US_DEBUGP("-- sending exit command to thread\n");
844 BUG_ON(us->sm_state != US_STATE_IDLE);
846 /* If the SCSI midlayer queued a final command just before
847 * scsi_remove_host() was called, us->srb might not be
848 * NULL. We can overwrite it safely, because the midlayer
849 * will not wait for the command to finish. Also the
850 * control thread will already have been awakened.
851 * That's okay, an extra up() on us->sema won't hurt.
853 * Enqueue the command, wake up the thread, and wait for
854 * notification that it has exited.
856 scsi_lock(us->host);
857 us->srb = NULL;
858 scsi_unlock(us->host);
859 up(&us->dev_semaphore);
861 up(&us->sema);
862 wait_for_completion(&us->notify);
865 /* Call the destructor routine, if it exists */
866 if (us->extra_destructor) {
867 US_DEBUGP("-- calling extra_destructor()\n");
868 us->extra_destructor(us->extra);
871 /* Finish the host removal sequence */
872 if (us->host)
873 scsi_host_put(us->host);
875 /* Free the extra data and the URB */
876 if (us->extra)
877 kfree(us->extra);
878 if (us->current_urb)
879 usb_free_urb(us->current_urb);
883 /* Dissociate from the USB device */
884 static void dissociate_dev(struct us_data *us)
886 US_DEBUGP("-- %s\n", __FUNCTION__);
888 /* Free the device-related DMA-mapped buffers */
889 if (us->cr)
890 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
891 us->cr_dma);
892 if (us->iobuf)
893 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
894 us->iobuf_dma);
896 /* Remove our private data from the interface */
897 usb_set_intfdata(us->pusb_intf, NULL);
899 /* Free the structure itself */
900 kfree(us);
903 #include <linux/kmod.h>
905 #if ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 ) || ( defined CONFIG_ARCH_W341 )
906 #define MAX_USBDISK 2
907 char usbdisk_queue[MAX_USBDISK][10];
908 int usbdisk_num;
909 int handle_index;
910 char usbdisk[10]="";
913 void set_usbsd_disk(char *disk)
915 int count;
918 if ( usbdisk_num == 0 || ( usbdisk_num == 1 && disk == NULL ) )
920 memset(usbdisk,0,10);
921 if ( disk == NULL )
923 usbdisk_num = 0;
924 return;
926 strcpy(usbdisk,disk);
927 usbdisk_num++;
928 return;
932 if ( usbdisk_num == 1 && disk != NULL )
935 memset( usbdisk_queue[0],0,10);
936 strcpy( usbdisk_queue[0],usbdisk);
937 memset( usbdisk_queue[1],0,10);
938 strcpy( usbdisk_queue[1],disk);
939 memset( usbdisk,0,10);
940 usbdisk_num++;
941 return;
943 else
948 if ( usbdisk_num == handle_index && disk == NULL )
950 for ( count = 0 ; count < MAX_USBDISK ; count++ )
951 memset( usbdisk_queue[count],0,10 );
952 usbdisk_num = 0;
953 handle_index = 0;
956 return;
960 #elif ( defined CONFIG_ARCH_IA240 ) || ( defined CONFIG_ARCH_IA241 )
961 char usbdisk[10]="";
962 #endif
966 #if ( defined CONFIG_ARCH_IA240 ) || ( defined CONFIG_ARCH_IA241 )
967 static void mountusb(char *action)
968 #elif ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 ) || ( defined CONFIG_ARCH_W341 )
969 void mountusb(char *action)
970 #endif
973 char *argv[2], **envp, *buf, *scratch;
974 int i=0;
976 #if ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 ) || ( defined CONFIG_ARCH_W341 )
977 char *buf1;
978 #endif
981 if ( in_interrupt() )
982 return;
983 if ( !current->fs->root )
984 return;
985 if ( !(envp=(char **)kmalloc(10*sizeof(char *), GFP_KERNEL)) )
986 return;
987 if ( !(buf=kmalloc(256, GFP_KERNEL)) )
988 return;
990 #if ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 ) || ( defined CONFIG_ARCH_W341 )
991 if ( !(buf1=kmalloc(256, GFP_KERNEL)) )
992 return;
993 #endif
997 argv[0] = "/bin/usbstpnp";
998 argv[1] = 0;
999 envp[i++] = "HOME=/";
1000 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1001 scratch = buf;
1002 envp[i++] = scratch;
1003 scratch += sprintf(scratch, "ACTION=%s", action) + 1;
1005 #if ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 )
1006 scratch = buf1;
1007 envp[i++] = scratch;
1008 if ( usbdisk_num > 1 )
1010 scratch += sprintf(scratch, "DISK=%s", usbdisk_queue[ handle_index ] ) + 1;
1011 handle_index++;
1013 else
1016 scratch += sprintf(scratch, "DISK=%s", usbdisk ) + 1;
1018 #elif ( defined CONFIG_ARCH_IA240 ) || ( defined CONFIG_ARCH_IA241 )
1019 scratch += sprintf(scratch, "DISK=%s", usbdisk ) + 1;
1020 #endif
1021 envp[i++] = 0;
1023 call_usermodehelper(argv[0], argv, envp, 0);
1025 #if ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 )
1026 set_usbsd_disk(NULL);
1027 #endif
1029 kfree(envp);
1030 kfree(buf);
1031 #if ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345_IMP1 )
1032 kfree(buf1);
1033 #endif
1042 /* Probe to see if we can drive a newly-connected USB device */
1043 static int storage_probe(struct usb_interface *intf,
1044 const struct usb_device_id *id)
1046 struct us_data *us;
1047 const int id_index = id - storage_usb_ids;
1048 int result;
1050 US_DEBUGP("USB Mass Storage device detected\n");
1052 /* Allocate the us_data structure and initialize the mutexes */
1053 us = (struct us_data *) kmalloc(sizeof(*us), GFP_KERNEL);
1054 if (!us) {
1055 printk(KERN_WARNING USB_STORAGE "Out of memory\n");
1056 return -ENOMEM;
1058 memset(us, 0, sizeof(struct us_data));
1059 init_MUTEX(&(us->dev_semaphore));
1060 init_MUTEX_LOCKED(&(us->sema));
1061 init_completion(&(us->notify));
1062 init_waitqueue_head(&us->dev_reset_wait);
1064 /* Associate the us_data structure with the USB device */
1065 result = associate_dev(us, intf);
1066 if (result)
1067 goto BadDevice;
1070 * Get the unusual_devs entries and the descriptors
1072 * id_index is calculated in the declaration to be the index number
1073 * of the match from the usb_device_id table, so we can find the
1074 * corresponding entry in the private table.
1076 #if 0 // mask by Victor Yu. 06-13-2007
1077 get_device_info(us, id_index);
1078 #else // add by Victor Yu. 06-13-2007
1079 result = get_device_info(us, id_index);
1080 if (result)
1081 goto BadDevice;
1082 #endif
1084 #ifdef CONFIG_USB_STORAGE_SDDR09
1085 if (us->protocol == US_PR_EUSB_SDDR09 ||
1086 us->protocol == US_PR_DPCM_USB) {
1087 /* set the configuration -- STALL is an acceptable response here */
1088 if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
1089 US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev
1090 ->actconfig->desc.bConfigurationValue);
1091 goto BadDevice;
1093 result = usb_reset_configuration(us->pusb_dev);
1095 US_DEBUGP("Result of usb_reset_configuration is %d\n", result);
1096 if (result == -EPIPE) {
1097 US_DEBUGP("-- stall on control interface\n");
1098 } else if (result != 0) {
1099 /* it's not a stall, but another error -- time to bail */
1100 US_DEBUGP("-- Unknown error. Rejecting device\n");
1101 goto BadDevice;
1104 #endif
1106 /* Get the transport, protocol, and pipe settings */
1107 result = get_transport(us);
1108 if (result)
1109 goto BadDevice;
1110 result = get_protocol(us);
1111 if (result)
1112 goto BadDevice;
1113 result = get_pipes(us);
1114 if (result)
1115 goto BadDevice;
1117 /* Acquire all the other resources */
1118 result = usb_stor_acquire_resources(us);
1119 if (result)
1120 goto BadDevice;
1122 /* Finally, add the host (this does SCSI device scanning) */
1123 result = scsi_add_host(us->host, &intf->dev);
1124 if (result) {
1125 printk(KERN_WARNING USB_STORAGE
1126 "Unable to add the scsi host\n");
1127 goto BadDevice;
1130 scsi_scan_host(us->host);
1132 printk(KERN_DEBUG
1133 "USB Mass Storage device found at %d\n", us->pusb_dev->devnum);
1134 #if ( defined CONFIG_ARCH_IA240 ) || ( defined CONFIG_ARCH_IA241 )
1135 mountusb("add");
1136 #endif
1137 return 0;
1139 /* We come here if there are any problems */
1140 BadDevice:
1141 US_DEBUGP("storage_probe() failed\n");
1142 usb_stor_release_resources(us);
1143 dissociate_dev(us);
1144 return result;
1147 /* Handle a disconnect event from the USB core */
1148 static void storage_disconnect(struct usb_interface *intf)
1150 struct us_data *us = usb_get_intfdata(intf);
1152 US_DEBUGP("storage_disconnect() called\n");
1154 /* Prevent new USB transfers, stop the current command, and
1155 * interrupt a device-reset delay */
1156 set_bit(US_FLIDX_DISCONNECTING, &us->flags);
1157 usb_stor_stop_transport(us);
1158 wake_up(&us->dev_reset_wait);
1160 /* Wait for the current command to finish, then remove the host */
1161 #if ( defined CONFIG_ARCH_IA240 ) || ( defined CONFIG_ARCH_IA241 )
1162 mountusb("remove");
1163 #endif
1164 down(&us->dev_semaphore);
1165 up(&us->dev_semaphore);
1166 scsi_remove_host(us->host);
1168 /* Wait for everything to become idle and release all our resources */
1169 usb_stor_release_resources(us);
1170 dissociate_dev(us);
1173 /***********************************************************************
1174 * Initialization and registration
1175 ***********************************************************************/
1177 static int __init usb_stor_init(void)
1179 int retval;
1180 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1182 /* register the driver, return usb_register return code if error */
1183 retval = usb_register(&usb_storage_driver);
1184 if (retval)
1185 goto out;
1187 /* we're all set */
1188 printk(KERN_INFO "USB Mass Storage support registered.\n");
1189 out:
1190 return retval;
1193 static void __exit usb_stor_exit(void)
1195 US_DEBUGP("usb_stor_exit() called\n");
1197 /* Deregister the driver
1198 * This will cause disconnect() to be called for each
1199 * attached unit
1201 US_DEBUGP("-- calling usb_deregister()\n");
1202 usb_deregister(&usb_storage_driver) ;
1205 module_init(usb_stor_init);
1206 module_exit(usb_stor_exit);
1208 #if ( defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345_IMP1 )
1209 EXPORT_SYMBOL(mountusb);
1210 EXPORT_SYMBOL(set_usbsd_disk);
1211 #endif