1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include <linux/sched.h>
49#include <linux/errno.h>
50#include <linux/freezer.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/slab.h>
54#include <linux/kthread.h>
55#include <linux/mutex.h>
56#include <linux/utsname.h>
57
58#include <scsi/scsi.h>
59#include <scsi/scsi_cmnd.h>
60#include <scsi/scsi_device.h>
61
62#include "usb.h"
63#include "scsiglue.h"
64#include "transport.h"
65#include "protocol.h"
66#include "debug.h"
67#include "initializers.h"
68
69#include "sierra_ms.h"
70#include "option_ms.h"
71
72
73MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
74MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
75MODULE_LICENSE("GPL");
76
77static unsigned int delay_use = 5;
78module_param(delay_use, uint, S_IRUGO | S_IWUSR);
79MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
80
81static char quirks[128];
82module_param_string(quirks, quirks, sizeof(quirks), S_IRUGO | S_IWUSR);
83MODULE_PARM_DESC(quirks, "supplemental list of device IDs and their quirks");
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
101 vendor_name, product_name, use_protocol, use_transport, \
102 init_function, Flags) \
103{ \
104 .vendorName = vendor_name, \
105 .productName = product_name, \
106 .useProtocol = use_protocol, \
107 .useTransport = use_transport, \
108 .initFunction = init_function, \
109}
110
111#define COMPLIANT_DEV UNUSUAL_DEV
112
113#define USUAL_DEV(use_protocol, use_transport, use_type) \
114{ \
115 .useProtocol = use_protocol, \
116 .useTransport = use_transport, \
117}
118
119static struct us_unusual_dev us_unusual_dev_list[] = {
120# include "unusual_devs.h"
121 { }
122};
123
124#undef UNUSUAL_DEV
125#undef COMPLIANT_DEV
126#undef USUAL_DEV
127
128
129#ifdef CONFIG_PM
130
131int usb_stor_suspend(struct usb_interface *iface, pm_message_t message)
132{
133 struct us_data *us = usb_get_intfdata(iface);
134
135
136 mutex_lock(&us->dev_mutex);
137
138 US_DEBUGP("%s\n", __func__);
139 if (us->suspend_resume_hook)
140 (us->suspend_resume_hook)(us, US_SUSPEND);
141
142
143
144
145 mutex_unlock(&us->dev_mutex);
146 return 0;
147}
148EXPORT_SYMBOL_GPL(usb_stor_suspend);
149
150int usb_stor_resume(struct usb_interface *iface)
151{
152 struct us_data *us = usb_get_intfdata(iface);
153
154 mutex_lock(&us->dev_mutex);
155
156 US_DEBUGP("%s\n", __func__);
157 if (us->suspend_resume_hook)
158 (us->suspend_resume_hook)(us, US_RESUME);
159
160 mutex_unlock(&us->dev_mutex);
161 return 0;
162}
163EXPORT_SYMBOL_GPL(usb_stor_resume);
164
165int usb_stor_reset_resume(struct usb_interface *iface)
166{
167 struct us_data *us = usb_get_intfdata(iface);
168
169 US_DEBUGP("%s\n", __func__);
170
171
172 usb_stor_report_bus_reset(us);
173
174
175
176 return 0;
177}
178EXPORT_SYMBOL_GPL(usb_stor_reset_resume);
179
180#endif
181
182
183
184
185
186
187int usb_stor_pre_reset(struct usb_interface *iface)
188{
189 struct us_data *us = usb_get_intfdata(iface);
190
191 US_DEBUGP("%s\n", __func__);
192
193
194 mutex_lock(&us->dev_mutex);
195 return 0;
196}
197EXPORT_SYMBOL_GPL(usb_stor_pre_reset);
198
199int usb_stor_post_reset(struct usb_interface *iface)
200{
201 struct us_data *us = usb_get_intfdata(iface);
202
203 US_DEBUGP("%s\n", __func__);
204
205
206 usb_stor_report_bus_reset(us);
207
208
209
210
211 mutex_unlock(&us->dev_mutex);
212 return 0;
213}
214EXPORT_SYMBOL_GPL(usb_stor_post_reset);
215
216
217
218
219
220
221
222
223
224
225void fill_inquiry_response(struct us_data *us, unsigned char *data,
226 unsigned int data_len)
227{
228 if (data_len<36)
229 return;
230
231 if(data[0]&0x20) {
232
233
234
235
236
237
238
239
240 memset(data+8,0,28);
241 } else {
242 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
243 memcpy(data+8, us->unusual_dev->vendorName,
244 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
245 strlen(us->unusual_dev->vendorName));
246 memcpy(data+16, us->unusual_dev->productName,
247 strlen(us->unusual_dev->productName) > 16 ? 16 :
248 strlen(us->unusual_dev->productName));
249 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
250 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
251 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
252 data[35] = 0x30 + ((bcdDevice) & 0x0F);
253 }
254
255 usb_stor_set_xfer_buf(data, data_len, us->srb);
256}
257EXPORT_SYMBOL_GPL(fill_inquiry_response);
258
259static int usb_stor_control_thread(void * __us)
260{
261 struct us_data *us = (struct us_data *)__us;
262 struct Scsi_Host *host = us_to_host(us);
263
264 for(;;) {
265 US_DEBUGP("*** thread sleeping.\n");
266 if (wait_for_completion_interruptible(&us->cmnd_ready))
267 break;
268
269 US_DEBUGP("*** thread awakened.\n");
270
271
272 mutex_lock(&(us->dev_mutex));
273
274
275 scsi_lock(host);
276
277
278 if (us->srb == NULL) {
279 scsi_unlock(host);
280 mutex_unlock(&us->dev_mutex);
281 US_DEBUGP("-- exiting\n");
282 break;
283 }
284
285
286 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
287 us->srb->result = DID_ABORT << 16;
288 goto SkipForAbort;
289 }
290
291 scsi_unlock(host);
292
293
294
295
296 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
297 US_DEBUGP("UNKNOWN data direction\n");
298 us->srb->result = DID_ERROR << 16;
299 }
300
301
302
303
304 else if (us->srb->device->id &&
305 !(us->fflags & US_FL_SCM_MULT_TARG)) {
306 US_DEBUGP("Bad target number (%d:%d)\n",
307 us->srb->device->id, us->srb->device->lun);
308 us->srb->result = DID_BAD_TARGET << 16;
309 }
310
311 else if (us->srb->device->lun > us->max_lun) {
312 US_DEBUGP("Bad LUN (%d:%d)\n",
313 us->srb->device->id, us->srb->device->lun);
314 us->srb->result = DID_BAD_TARGET << 16;
315 }
316
317
318
319 else if ((us->srb->cmnd[0] == INQUIRY) &&
320 (us->fflags & US_FL_FIX_INQUIRY)) {
321 unsigned char data_ptr[36] = {
322 0x00, 0x80, 0x02, 0x02,
323 0x1F, 0x00, 0x00, 0x00};
324
325 US_DEBUGP("Faking INQUIRY command\n");
326 fill_inquiry_response(us, data_ptr, 36);
327 us->srb->result = SAM_STAT_GOOD;
328 }
329
330
331 else {
332 US_DEBUG(usb_stor_show_command(us->srb));
333 us->proto_handler(us->srb, us);
334 }
335
336
337 scsi_lock(host);
338
339
340 if (us->srb->result != DID_ABORT << 16) {
341 US_DEBUGP("scsi cmd done, result=0x%x\n",
342 us->srb->result);
343 us->srb->scsi_done(us->srb);
344 } else {
345SkipForAbort:
346 US_DEBUGP("scsi command aborted\n");
347 }
348
349
350
351
352
353
354 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
355 complete(&(us->notify));
356
357
358 clear_bit(US_FLIDX_ABORTING, &us->dflags);
359 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
360 }
361
362
363 us->srb = NULL;
364 scsi_unlock(host);
365
366
367 mutex_unlock(&us->dev_mutex);
368 }
369
370
371 for (;;) {
372 set_current_state(TASK_INTERRUPTIBLE);
373 if (kthread_should_stop())
374 break;
375 schedule();
376 }
377 __set_current_state(TASK_RUNNING);
378 return 0;
379}
380
381
382
383
384
385
386static int associate_dev(struct us_data *us, struct usb_interface *intf)
387{
388 US_DEBUGP("-- %s\n", __func__);
389
390
391 us->pusb_dev = interface_to_usbdev(intf);
392 us->pusb_intf = intf;
393 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
394 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
395 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
396 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
397 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
398 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
399 intf->cur_altsetting->desc.bInterfaceSubClass,
400 intf->cur_altsetting->desc.bInterfaceProtocol);
401
402
403 usb_set_intfdata(intf, us);
404
405
406 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
407 GFP_KERNEL, &us->cr_dma);
408 if (!us->cr) {
409 US_DEBUGP("usb_ctrlrequest allocation failed\n");
410 return -ENOMEM;
411 }
412
413 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
414 GFP_KERNEL, &us->iobuf_dma);
415 if (!us->iobuf) {
416 US_DEBUGP("I/O buffer allocation failed\n");
417 return -ENOMEM;
418 }
419 return 0;
420}
421
422
423#define TOLOWER(x) ((x) | 0x20)
424
425
426static void adjust_quirks(struct us_data *us)
427{
428 char *p;
429 u16 vid = le16_to_cpu(us->pusb_dev->descriptor.idVendor);
430 u16 pid = le16_to_cpu(us->pusb_dev->descriptor.idProduct);
431 unsigned f = 0;
432 unsigned int mask = (US_FL_SANE_SENSE | US_FL_FIX_CAPACITY |
433 US_FL_CAPACITY_HEURISTICS | US_FL_IGNORE_DEVICE |
434 US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 |
435 US_FL_CAPACITY_OK | US_FL_IGNORE_RESIDUE |
436 US_FL_SINGLE_LUN | US_FL_NO_WP_DETECT);
437
438 p = quirks;
439 while (*p) {
440
441 if (vid == simple_strtoul(p, &p, 16) &&
442 *p == ':' &&
443 pid == simple_strtoul(p+1, &p, 16) &&
444 *p == ':')
445 break;
446
447
448 while (*p) {
449 if (*p++ == ',')
450 break;
451 }
452 }
453 if (!*p)
454 return;
455
456
457 while (*++p && *p != ',') {
458 switch (TOLOWER(*p)) {
459 case 'a':
460 f |= US_FL_SANE_SENSE;
461 break;
462 case 'c':
463 f |= US_FL_FIX_CAPACITY;
464 break;
465 case 'h':
466 f |= US_FL_CAPACITY_HEURISTICS;
467 break;
468 case 'i':
469 f |= US_FL_IGNORE_DEVICE;
470 break;
471 case 'l':
472 f |= US_FL_NOT_LOCKABLE;
473 break;
474 case 'm':
475 f |= US_FL_MAX_SECTORS_64;
476 break;
477 case 'o':
478 f |= US_FL_CAPACITY_OK;
479 break;
480 case 'r':
481 f |= US_FL_IGNORE_RESIDUE;
482 break;
483 case 's':
484 f |= US_FL_SINGLE_LUN;
485 break;
486 case 'w':
487 f |= US_FL_NO_WP_DETECT;
488 break;
489
490 }
491 }
492 us->fflags = (us->fflags & ~mask) | f;
493 dev_info(&us->pusb_intf->dev, "Quirks match for "
494 "vid %04x pid %04x: %x\n",
495 vid, pid, f);
496}
497
498
499static int get_device_info(struct us_data *us, const struct usb_device_id *id,
500 struct us_unusual_dev *unusual_dev)
501{
502 struct usb_device *dev = us->pusb_dev;
503 struct usb_interface_descriptor *idesc =
504 &us->pusb_intf->cur_altsetting->desc;
505
506
507 us->unusual_dev = unusual_dev;
508 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
509 idesc->bInterfaceSubClass :
510 unusual_dev->useProtocol;
511 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
512 idesc->bInterfaceProtocol :
513 unusual_dev->useTransport;
514 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
515 adjust_quirks(us);
516
517 if (us->fflags & US_FL_IGNORE_DEVICE) {
518 printk(KERN_INFO USB_STORAGE "device ignored\n");
519 return -ENODEV;
520 }
521
522
523
524
525
526 if (dev->speed != USB_SPEED_HIGH)
527 us->fflags &= ~US_FL_GO_SLOW;
528
529
530
531
532
533
534 if (id->idVendor || id->idProduct) {
535 static const char *msgs[3] = {
536 "an unneeded SubClass entry",
537 "an unneeded Protocol entry",
538 "unneeded SubClass and Protocol entries"};
539 struct usb_device_descriptor *ddesc = &dev->descriptor;
540 int msg = -1;
541
542 if (unusual_dev->useProtocol != US_SC_DEVICE &&
543 us->subclass == idesc->bInterfaceSubClass)
544 msg += 1;
545 if (unusual_dev->useTransport != US_PR_DEVICE &&
546 us->protocol == idesc->bInterfaceProtocol)
547 msg += 2;
548 if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
549 printk(KERN_NOTICE USB_STORAGE "This device "
550 "(%04x,%04x,%04x S %02x P %02x)"
551 " has %s in unusual_devs.h (kernel"
552 " %s)\n"
553 " Please send a copy of this message to "
554 "<linux-usb@vger.kernel.org> and "
555 "<usb-storage@lists.one-eyed-alien.net>\n",
556 le16_to_cpu(ddesc->idVendor),
557 le16_to_cpu(ddesc->idProduct),
558 le16_to_cpu(ddesc->bcdDevice),
559 idesc->bInterfaceSubClass,
560 idesc->bInterfaceProtocol,
561 msgs[msg],
562 utsname()->release);
563 }
564
565 return 0;
566}
567
568
569static void get_transport(struct us_data *us)
570{
571 switch (us->protocol) {
572 case US_PR_CB:
573 us->transport_name = "Control/Bulk";
574 us->transport = usb_stor_CB_transport;
575 us->transport_reset = usb_stor_CB_reset;
576 us->max_lun = 7;
577 break;
578
579 case US_PR_CBI:
580 us->transport_name = "Control/Bulk/Interrupt";
581 us->transport = usb_stor_CB_transport;
582 us->transport_reset = usb_stor_CB_reset;
583 us->max_lun = 7;
584 break;
585
586 case US_PR_BULK:
587 us->transport_name = "Bulk";
588 us->transport = usb_stor_Bulk_transport;
589 us->transport_reset = usb_stor_Bulk_reset;
590 break;
591 }
592}
593
594
595static void get_protocol(struct us_data *us)
596{
597 switch (us->subclass) {
598 case US_SC_RBC:
599 us->protocol_name = "Reduced Block Commands (RBC)";
600 us->proto_handler = usb_stor_transparent_scsi_command;
601 break;
602
603 case US_SC_8020:
604 us->protocol_name = "8020i";
605 us->proto_handler = usb_stor_pad12_command;
606 us->max_lun = 0;
607 break;
608
609 case US_SC_QIC:
610 us->protocol_name = "QIC-157";
611 us->proto_handler = usb_stor_pad12_command;
612 us->max_lun = 0;
613 break;
614
615 case US_SC_8070:
616 us->protocol_name = "8070i";
617 us->proto_handler = usb_stor_pad12_command;
618 us->max_lun = 0;
619 break;
620
621 case US_SC_SCSI:
622 us->protocol_name = "Transparent SCSI";
623 us->proto_handler = usb_stor_transparent_scsi_command;
624 break;
625
626 case US_SC_UFI:
627 us->protocol_name = "Uniform Floppy Interface (UFI)";
628 us->proto_handler = usb_stor_ufi_command;
629 break;
630 }
631}
632
633
634static int get_pipes(struct us_data *us)
635{
636 struct usb_host_interface *altsetting =
637 us->pusb_intf->cur_altsetting;
638 int i;
639 struct usb_endpoint_descriptor *ep;
640 struct usb_endpoint_descriptor *ep_in = NULL;
641 struct usb_endpoint_descriptor *ep_out = NULL;
642 struct usb_endpoint_descriptor *ep_int = NULL;
643
644
645
646
647
648
649
650 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
651 ep = &altsetting->endpoint[i].desc;
652
653 if (usb_endpoint_xfer_bulk(ep)) {
654 if (usb_endpoint_dir_in(ep)) {
655 if (!ep_in)
656 ep_in = ep;
657 } else {
658 if (!ep_out)
659 ep_out = ep;
660 }
661 }
662
663 else if (usb_endpoint_is_int_in(ep)) {
664 if (!ep_int)
665 ep_int = ep;
666 }
667 }
668
669 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
670 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
671 return -EIO;
672 }
673
674
675 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
676 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
677 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
678 usb_endpoint_num(ep_out));
679 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
680 usb_endpoint_num(ep_in));
681 if (ep_int) {
682 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
683 usb_endpoint_num(ep_int));
684 us->ep_bInterval = ep_int->bInterval;
685 }
686 return 0;
687}
688
689
690static int usb_stor_acquire_resources(struct us_data *us)
691{
692 int p;
693 struct task_struct *th;
694
695 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
696 if (!us->current_urb) {
697 US_DEBUGP("URB allocation failed\n");
698 return -ENOMEM;
699 }
700
701
702
703 if (us->unusual_dev->initFunction) {
704 p = us->unusual_dev->initFunction(us);
705 if (p)
706 return p;
707 }
708
709
710 th = kthread_run(usb_stor_control_thread, us, "usb-storage");
711 if (IS_ERR(th)) {
712 printk(KERN_WARNING USB_STORAGE
713 "Unable to start control thread\n");
714 return PTR_ERR(th);
715 }
716 us->ctl_thread = th;
717
718 return 0;
719}
720
721
722static void usb_stor_release_resources(struct us_data *us)
723{
724 US_DEBUGP("-- %s\n", __func__);
725
726
727
728
729
730 US_DEBUGP("-- sending exit command to thread\n");
731 complete(&us->cmnd_ready);
732 if (us->ctl_thread)
733 kthread_stop(us->ctl_thread);
734
735
736 if (us->extra_destructor) {
737 US_DEBUGP("-- calling extra_destructor()\n");
738 us->extra_destructor(us->extra);
739 }
740
741
742 kfree(us->extra);
743 usb_free_urb(us->current_urb);
744}
745
746
747static void dissociate_dev(struct us_data *us)
748{
749 US_DEBUGP("-- %s\n", __func__);
750
751
752 if (us->cr)
753 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
754 us->cr_dma);
755 if (us->iobuf)
756 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
757 us->iobuf_dma);
758
759
760 usb_set_intfdata(us->pusb_intf, NULL);
761}
762
763
764
765
766static void quiesce_and_remove_host(struct us_data *us)
767{
768 struct Scsi_Host *host = us_to_host(us);
769
770
771 if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
772 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
773
774
775
776
777 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
778 wake_up(&us->delay_wait);
779 wait_for_completion(&us->scanning_done);
780
781
782
783
784 scsi_remove_host(host);
785
786
787
788
789 scsi_lock(host);
790 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
791 scsi_unlock(host);
792 wake_up(&us->delay_wait);
793}
794
795
796static void release_everything(struct us_data *us)
797{
798 usb_stor_release_resources(us);
799 dissociate_dev(us);
800
801
802
803 scsi_host_put(us_to_host(us));
804}
805
806
807static int usb_stor_scan_thread(void * __us)
808{
809 struct us_data *us = (struct us_data *)__us;
810
811 printk(KERN_DEBUG
812 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
813
814 set_freezable();
815
816 if (delay_use > 0) {
817 printk(KERN_DEBUG "usb-storage: waiting for device "
818 "to settle before scanning\n");
819 wait_event_freezable_timeout(us->delay_wait,
820 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
821 delay_use * HZ);
822 }
823
824
825 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
826
827
828 if (us->protocol == US_PR_BULK &&
829 !(us->fflags & US_FL_SINGLE_LUN)) {
830 mutex_lock(&us->dev_mutex);
831 us->max_lun = usb_stor_Bulk_max_lun(us);
832 mutex_unlock(&us->dev_mutex);
833 }
834 scsi_scan_host(us_to_host(us));
835 printk(KERN_DEBUG "usb-storage: device scan complete\n");
836
837
838 }
839
840 complete_and_exit(&us->scanning_done, 0);
841}
842
843
844
845int usb_stor_probe1(struct us_data **pus,
846 struct usb_interface *intf,
847 const struct usb_device_id *id,
848 struct us_unusual_dev *unusual_dev)
849{
850 struct Scsi_Host *host;
851 struct us_data *us;
852 int result;
853
854 US_DEBUGP("USB Mass Storage device detected\n");
855
856
857
858
859
860 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
861 if (!host) {
862 printk(KERN_WARNING USB_STORAGE
863 "Unable to allocate the scsi host\n");
864 return -ENOMEM;
865 }
866
867
868
869
870 host->max_cmd_len = 16;
871 *pus = us = host_to_us(host);
872 memset(us, 0, sizeof(struct us_data));
873 mutex_init(&(us->dev_mutex));
874 init_completion(&us->cmnd_ready);
875 init_completion(&(us->notify));
876 init_waitqueue_head(&us->delay_wait);
877 init_completion(&us->scanning_done);
878
879
880 result = associate_dev(us, intf);
881 if (result)
882 goto BadDevice;
883
884
885 result = get_device_info(us, id, unusual_dev);
886 if (result)
887 goto BadDevice;
888
889
890 get_transport(us);
891 get_protocol(us);
892
893
894
895
896 return 0;
897
898BadDevice:
899 US_DEBUGP("storage_probe() failed\n");
900 release_everything(us);
901 return result;
902}
903EXPORT_SYMBOL_GPL(usb_stor_probe1);
904
905
906int usb_stor_probe2(struct us_data *us)
907{
908 struct task_struct *th;
909 int result;
910
911
912 if (!us->transport || !us->proto_handler) {
913 result = -ENXIO;
914 goto BadDevice;
915 }
916 US_DEBUGP("Transport: %s\n", us->transport_name);
917 US_DEBUGP("Protocol: %s\n", us->protocol_name);
918
919
920 if (us->fflags & US_FL_SINGLE_LUN)
921 us->max_lun = 0;
922
923
924 result = get_pipes(us);
925 if (result)
926 goto BadDevice;
927
928
929 result = usb_stor_acquire_resources(us);
930 if (result)
931 goto BadDevice;
932 result = scsi_add_host(us_to_host(us), &us->pusb_intf->dev);
933 if (result) {
934 printk(KERN_WARNING USB_STORAGE
935 "Unable to add the scsi host\n");
936 goto BadDevice;
937 }
938
939
940 th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
941 if (IS_ERR(th)) {
942 printk(KERN_WARNING USB_STORAGE
943 "Unable to start the device-scanning thread\n");
944 complete(&us->scanning_done);
945 quiesce_and_remove_host(us);
946 result = PTR_ERR(th);
947 goto BadDevice;
948 }
949
950 wake_up_process(th);
951
952 return 0;
953
954
955BadDevice:
956 US_DEBUGP("storage_probe() failed\n");
957 release_everything(us);
958 return result;
959}
960EXPORT_SYMBOL_GPL(usb_stor_probe2);
961
962
963void usb_stor_disconnect(struct usb_interface *intf)
964{
965 struct us_data *us = usb_get_intfdata(intf);
966
967 US_DEBUGP("storage_disconnect() called\n");
968 quiesce_and_remove_host(us);
969 release_everything(us);
970}
971EXPORT_SYMBOL_GPL(usb_stor_disconnect);
972
973
974static int storage_probe(struct usb_interface *intf,
975 const struct usb_device_id *id)
976{
977 struct us_data *us;
978 int result;
979
980
981
982
983
984
985
986 if (usb_usual_check_type(id, USB_US_TYPE_STOR) ||
987 usb_usual_ignore_device(intf))
988 return -ENXIO;
989
990
991
992
993
994
995
996
997 result = usb_stor_probe1(&us, intf, id,
998 (id - usb_storage_usb_ids) + us_unusual_dev_list);
999 if (result)
1000 return result;
1001
1002
1003
1004 result = usb_stor_probe2(us);
1005 return result;
1006}
1007
1008
1009
1010
1011
1012static struct usb_driver usb_storage_driver = {
1013 .name = "usb-storage",
1014 .probe = storage_probe,
1015 .disconnect = usb_stor_disconnect,
1016 .suspend = usb_stor_suspend,
1017 .resume = usb_stor_resume,
1018 .reset_resume = usb_stor_reset_resume,
1019 .pre_reset = usb_stor_pre_reset,
1020 .post_reset = usb_stor_post_reset,
1021 .id_table = usb_storage_usb_ids,
1022 .soft_unbind = 1,
1023};
1024
1025static int __init usb_stor_init(void)
1026{
1027 int retval;
1028
1029 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1030
1031
1032 retval = usb_register(&usb_storage_driver);
1033 if (retval == 0) {
1034 printk(KERN_INFO "USB Mass Storage support registered.\n");
1035 usb_usual_set_present(USB_US_TYPE_STOR);
1036 }
1037 return retval;
1038}
1039
1040static void __exit usb_stor_exit(void)
1041{
1042 US_DEBUGP("usb_stor_exit() called\n");
1043
1044
1045
1046
1047
1048 US_DEBUGP("-- calling usb_deregister()\n");
1049 usb_deregister(&usb_storage_driver) ;
1050
1051 usb_usual_clear_present(USB_US_TYPE_STOR);
1052}
1053
1054module_init(usb_stor_init);
1055module_exit(usb_stor_exit);
1056