1#include <linux/sched.h>
2#include <linux/errno.h>
3#include <linux/freezer.h>
4#include <linux/module.h>
5#include <linux/init.h>
6#include <linux/slab.h>
7#include <linux/kthread.h>
8#include <linux/mutex.h>
9#include <linux/utsname.h>
10
11#include <scsi/scsi.h>
12#include <scsi/scsi_cmnd.h>
13#include <scsi/scsi_device.h>
14
15#include "usb.h"
16#include "scsiglue.h"
17#include "smil.h"
18#include "transport.h"
19
20
21MODULE_AUTHOR("Domao");
22MODULE_DESCRIPTION("ENE USB Mass Storage driver for Linux");
23MODULE_LICENSE("GPL");
24
25static unsigned int delay_use = 1;
26
27static struct usb_device_id eucr_usb_ids [] = {
28 { USB_DEVICE(0x058f, 0x6366) },
29 { USB_DEVICE(0x0cf2, 0x6230) },
30 { USB_DEVICE(0x0cf2, 0x6250) },
31 { }
32};
33MODULE_DEVICE_TABLE (usb, eucr_usb_ids);
34
35
36#ifdef CONFIG_PM
37
38static int eucr_suspend(struct usb_interface *iface, pm_message_t message)
39{
40 struct us_data *us = usb_get_intfdata(iface);
41 pr_info("--- eucr_suspend ---\n");
42
43 mutex_lock(&us->dev_mutex);
44
45
46 if (us->suspend_resume_hook)
47 (us->suspend_resume_hook)(us, US_SUSPEND);
48
49
50
51
52
53
54 mutex_unlock(&us->dev_mutex);
55 return 0;
56}
57
58
59static int eucr_resume(struct usb_interface *iface)
60{
61 BYTE tmp = 0;
62
63 struct us_data *us = usb_get_intfdata(iface);
64 pr_info("--- eucr_resume---\n");
65 mutex_lock(&us->dev_mutex);
66
67
68 if (us->suspend_resume_hook)
69 (us->suspend_resume_hook)(us, US_RESUME);
70
71
72 mutex_unlock(&us->dev_mutex);
73
74
75 us->Power_IsResum = true;
76
77
78 us->SM_Status = *(PSM_STATUS)&tmp;
79
80 return 0;
81}
82
83static int eucr_reset_resume(struct usb_interface *iface)
84{
85 BYTE tmp = 0;
86 struct us_data *us = usb_get_intfdata(iface);
87
88 pr_info("--- eucr_reset_resume---\n");
89
90
91
92 usb_stor_report_bus_reset(us);
93
94
95
96
97 us->Power_IsResum = true;
98
99
100 us->SM_Status = *(PSM_STATUS)&tmp;
101 return 0;
102}
103
104
105#else
106
107#define eucr_suspend NULL
108#define eucr_resume NULL
109#define eucr_reset_resume NULL
110
111#endif
112
113
114static int eucr_pre_reset(struct usb_interface *iface)
115{
116 struct us_data *us = usb_get_intfdata(iface);
117
118 pr_info("usb --- eucr_pre_reset\n");
119
120
121 mutex_lock(&us->dev_mutex);
122 return 0;
123}
124
125
126static int eucr_post_reset(struct usb_interface *iface)
127{
128 struct us_data *us = usb_get_intfdata(iface);
129
130 pr_info("usb --- eucr_post_reset\n");
131
132
133 usb_stor_report_bus_reset(us);
134
135 mutex_unlock(&us->dev_mutex);
136 return 0;
137}
138
139
140void fill_inquiry_response(struct us_data *us, unsigned char *data, unsigned int data_len)
141{
142 pr_info("usb --- fill_inquiry_response\n");
143 if (data_len<36)
144 return;
145
146 if (data[0]&0x20)
147 {
148 memset(data+8,0,28);
149 }
150 else
151 {
152 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
153 memcpy(data+8, us->unusual_dev->vendorName,
154 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
155 strlen(us->unusual_dev->vendorName));
156 memcpy(data+16, us->unusual_dev->productName,
157 strlen(us->unusual_dev->productName) > 16 ? 16 :
158 strlen(us->unusual_dev->productName));
159 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
160 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
161 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
162 data[35] = 0x30 + ((bcdDevice) & 0x0F);
163 }
164 usb_stor_set_xfer_buf(us, data, data_len, us->srb, TO_XFER_BUF);
165}
166
167
168static int usb_stor_control_thread(void * __us)
169{
170 struct us_data *us = (struct us_data *)__us;
171 struct Scsi_Host *host = us_to_host(us);
172
173 pr_info("usb --- usb_stor_control_thread\n");
174 for(;;)
175 {
176 if (wait_for_completion_interruptible(&us->cmnd_ready))
177 break;
178
179
180 mutex_lock(&(us->dev_mutex));
181
182
183 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
184 mutex_unlock(&us->dev_mutex);
185 break;
186 }
187
188
189 scsi_lock(host);
190
191
192 if (us->srb == NULL)
193 {
194 scsi_unlock(host);
195 mutex_unlock(&us->dev_mutex);
196
197 break;
198 }
199
200
201 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags))
202 {
203 us->srb->result = DID_ABORT << 16;
204 goto SkipForAbort;
205 }
206
207 scsi_unlock(host);
208
209 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL)
210 {
211 us->srb->result = DID_ERROR << 16;
212 }
213 else if (us->srb->device->id && !(us->fflags & US_FL_SCM_MULT_TARG))
214 {
215 us->srb->result = DID_BAD_TARGET << 16;
216 }
217 else if (us->srb->device->lun > us->max_lun)
218 {
219 us->srb->result = DID_BAD_TARGET << 16;
220 }
221 else if ((us->srb->cmnd[0] == INQUIRY) && (us->fflags & US_FL_FIX_INQUIRY))
222 {
223 unsigned char data_ptr[36] = {0x00, 0x80, 0x02, 0x02, 0x1F, 0x00, 0x00, 0x00};
224
225 fill_inquiry_response(us, data_ptr, 36);
226 us->srb->result = SAM_STAT_GOOD;
227 }
228 else
229 {
230 us->proto_handler(us->srb, us);
231 }
232
233
234 scsi_lock(host);
235
236
237 if (us->srb->result != DID_ABORT << 16)
238 {
239 us->srb->scsi_done(us->srb);
240 }
241 else
242 {
243SkipForAbort:
244 pr_info("scsi command aborted\n");
245 }
246
247 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags))
248 {
249 complete(&(us->notify));
250
251
252 clear_bit(US_FLIDX_ABORTING, &us->dflags);
253 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
254 }
255
256
257 us->srb = NULL;
258 scsi_unlock(host);
259
260
261 mutex_unlock(&us->dev_mutex);
262 }
263
264
265 for (;;)
266 {
267 set_current_state(TASK_INTERRUPTIBLE);
268 if (kthread_should_stop())
269 break;
270 schedule();
271 }
272 __set_current_state(TASK_RUNNING);
273 return 0;
274}
275
276
277static int associate_dev(struct us_data *us, struct usb_interface *intf)
278{
279 pr_info("usb --- associate_dev\n");
280
281
282 us->pusb_dev = interface_to_usbdev(intf);
283 us->pusb_intf = intf;
284 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
285
286
287 usb_set_intfdata(intf, us);
288
289
290 us->cr = usb_alloc_coherent(us->pusb_dev, sizeof(*us->cr), GFP_KERNEL, &us->cr_dma);
291 if (!us->cr)
292 {
293 pr_info("usb_ctrlrequest allocation failed\n");
294 return -ENOMEM;
295 }
296
297 us->iobuf = usb_alloc_coherent(us->pusb_dev, US_IOBUF_SIZE, GFP_KERNEL, &us->iobuf_dma);
298 if (!us->iobuf)
299 {
300 pr_info("I/O buffer allocation failed\n");
301 return -ENOMEM;
302 }
303
304 us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
305 if (!us->sensebuf)
306 {
307 pr_info("Sense buffer allocation failed\n");
308 return -ENOMEM;
309 }
310 return 0;
311}
312
313
314static int get_device_info(struct us_data *us, const struct usb_device_id *id)
315{
316 struct usb_device *dev = us->pusb_dev;
317 struct usb_interface_descriptor *idesc = &us->pusb_intf->cur_altsetting->desc;
318
319 pr_info("usb --- get_device_info\n");
320
321 us->subclass = idesc->bInterfaceSubClass;
322 us->protocol = idesc->bInterfaceProtocol;
323 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
324 us->Power_IsResum = false;
325
326 if (us->fflags & US_FL_IGNORE_DEVICE)
327 {
328 pr_info("device ignored\n");
329 return -ENODEV;
330 }
331
332 if (dev->speed != USB_SPEED_HIGH)
333 us->fflags &= ~US_FL_GO_SLOW;
334
335 return 0;
336}
337
338
339static int get_transport(struct us_data *us)
340{
341 pr_info("usb --- get_transport\n");
342 switch (us->protocol) {
343 case USB_PR_BULK:
344 us->transport_name = "Bulk";
345 us->transport = usb_stor_Bulk_transport;
346 us->transport_reset = usb_stor_Bulk_reset;
347 break;
348
349 default:
350 return -EIO;
351 }
352
353
354
355 if (us->fflags & US_FL_SINGLE_LUN)
356 us->max_lun = 0;
357 return 0;
358}
359
360
361static int get_protocol(struct us_data *us)
362{
363 pr_info("usb --- get_protocol\n");
364 pr_info("us->pusb_dev->descriptor.idVendor = %x\n",
365 us->pusb_dev->descriptor.idVendor);
366 pr_info("us->pusb_dev->descriptor.idProduct = %x\n",
367 us->pusb_dev->descriptor.idProduct);
368 switch (us->subclass) {
369 case USB_SC_SCSI:
370 us->protocol_name = "Transparent SCSI";
371 if( (us->pusb_dev->descriptor.idVendor == 0x0CF2) && (us->pusb_dev->descriptor.idProduct == 0x6250) )
372 us->proto_handler = ENE_stor_invoke_transport;
373 else
374 us->proto_handler = usb_stor_invoke_transport;
375 break;
376
377 default:
378 return -EIO;
379 }
380
381 return 0;
382}
383
384
385static int get_pipes(struct us_data *us)
386{
387 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
388 int i;
389 struct usb_endpoint_descriptor *ep;
390 struct usb_endpoint_descriptor *ep_in = NULL;
391 struct usb_endpoint_descriptor *ep_out = NULL;
392 struct usb_endpoint_descriptor *ep_int = NULL;
393
394 pr_info("usb --- get_pipes\n");
395
396 for (i = 0; i < altsetting->desc.bNumEndpoints; i++)
397 {
398 ep = &altsetting->endpoint[i].desc;
399
400 if (usb_endpoint_xfer_bulk(ep))
401 {
402 if (usb_endpoint_dir_in(ep))
403 {
404 if (!ep_in)
405 ep_in = ep;
406 }
407 else
408 {
409 if (!ep_out)
410 ep_out = ep;
411 }
412 }
413 else if (usb_endpoint_is_int_in(ep))
414 {
415 if (!ep_int)
416 ep_int = ep;
417 }
418 }
419
420 if (!ep_in || !ep_out || (us->protocol == USB_PR_CBI && !ep_int))
421 {
422 pr_info("Endpoint sanity check failed! Rejecting dev.\n");
423 return -EIO;
424 }
425
426
427 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
428 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
429 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev, ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
430 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev, ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
431 if (ep_int)
432 {
433 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev, ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
434 us->ep_bInterval = ep_int->bInterval;
435 }
436 return 0;
437}
438
439
440static int usb_stor_acquire_resources(struct us_data *us)
441{
442 struct task_struct *th;
443
444 pr_info("usb --- usb_stor_acquire_resources\n");
445 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
446 if (!us->current_urb)
447 {
448 pr_info("URB allocation failed\n");
449 return -ENOMEM;
450 }
451
452
453 th = kthread_run(usb_stor_control_thread, us, "eucr-storage");
454 if (IS_ERR(th))
455 {
456 pr_info("Unable to start control thread\n");
457 return PTR_ERR(th);
458 }
459 us->ctl_thread = th;
460
461 return 0;
462}
463
464
465static void usb_stor_release_resources(struct us_data *us)
466{
467 pr_info("usb --- usb_stor_release_resources\n");
468
469 SM_FreeMem();
470
471 complete(&us->cmnd_ready);
472 if (us->ctl_thread)
473 kthread_stop(us->ctl_thread);
474
475
476 if (us->extra_destructor)
477 {
478 pr_info("-- calling extra_destructor()\n");
479 us->extra_destructor(us->extra);
480 }
481
482
483 kfree(us->extra);
484 usb_free_urb(us->current_urb);
485}
486
487
488static void dissociate_dev(struct us_data *us)
489{
490 pr_info("usb --- dissociate_dev\n");
491
492 kfree(us->sensebuf);
493
494
495 if (us->cr)
496 usb_free_coherent(us->pusb_dev, sizeof(*us->cr), us->cr, us->cr_dma);
497 if (us->iobuf)
498 usb_free_coherent(us->pusb_dev, US_IOBUF_SIZE, us->iobuf, us->iobuf_dma);
499
500
501 usb_set_intfdata(us->pusb_intf, NULL);
502}
503
504
505static void quiesce_and_remove_host(struct us_data *us)
506{
507 struct Scsi_Host *host = us_to_host(us);
508
509 pr_info("usb --- quiesce_and_remove_host\n");
510
511
512 if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
513 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
514
515
516
517
518 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
519 wake_up(&us->delay_wait);
520 wait_for_completion(&us->scanning_done);
521
522
523
524
525 scsi_remove_host(host);
526
527
528
529
530 scsi_lock(host);
531 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
532 scsi_unlock(host);
533 wake_up(&us->delay_wait);
534}
535
536
537static void release_everything(struct us_data *us)
538{
539 pr_info("usb --- release_everything\n");
540
541 usb_stor_release_resources(us);
542 dissociate_dev(us);
543 scsi_host_put(us_to_host(us));
544}
545
546
547static int usb_stor_scan_thread(void * __us)
548{
549 struct us_data *us = (struct us_data *)__us;
550
551 pr_info("usb --- usb_stor_scan_thread\n");
552 pr_info("EUCR : device found at %d\n", us->pusb_dev->devnum);
553
554 set_freezable();
555
556 if (delay_use > 0) {
557 wait_event_freezable_timeout(us->delay_wait,
558 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
559 delay_use * HZ);
560 }
561
562
563 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags))
564 {
565
566 if (us->protocol == USB_PR_BULK && !(us->fflags & US_FL_SINGLE_LUN))
567 {
568 mutex_lock(&us->dev_mutex);
569 us->max_lun = usb_stor_Bulk_max_lun(us);
570 mutex_unlock(&us->dev_mutex);
571 }
572 scsi_scan_host(us_to_host(us));
573 pr_info("EUCR : device scan complete\n");
574 }
575 complete_and_exit(&us->scanning_done, 0);
576}
577
578
579static int eucr_probe(struct usb_interface *intf, const struct usb_device_id *id)
580{
581 struct Scsi_Host *host;
582 struct us_data *us;
583 int result;
584 BYTE MiscReg03 = 0;
585 struct task_struct *th;
586
587 pr_info("usb --- eucr_probe\n");
588
589 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
590 if (!host)
591 {
592 pr_info("Unable to allocate the scsi host\n");
593 return -ENOMEM;
594 }
595
596
597 host->max_cmd_len = 16;
598 us = host_to_us(host);
599 memset(us, 0, sizeof(struct us_data));
600 mutex_init(&(us->dev_mutex));
601 init_completion(&us->cmnd_ready);
602 init_completion(&(us->notify));
603 init_waitqueue_head(&us->delay_wait);
604 init_completion(&us->scanning_done);
605
606
607 result = associate_dev(us, intf);
608 if (result)
609 goto BadDevice;
610
611
612 result = get_device_info(us, id);
613 if (result)
614 goto BadDevice;
615
616
617 result = get_transport(us);
618 if (result)
619 goto BadDevice;
620 result = get_protocol(us);
621 if (result)
622 goto BadDevice;
623 result = get_pipes(us);
624 if (result)
625 goto BadDevice;
626
627
628 result = usb_stor_acquire_resources(us);
629 if (result)
630 goto BadDevice;
631
632 result = scsi_add_host(host, &intf->dev);
633 if (result)
634 {
635 pr_info("Unable to add the scsi host\n");
636 goto BadDevice;
637 }
638
639
640 th = kthread_create(usb_stor_scan_thread, us, "eucr-stor-scan");
641 if (IS_ERR(th)) {
642 pr_info("Unable to start the device-scanning thread\n");
643 complete(&us->scanning_done);
644 quiesce_and_remove_host(us);
645 result = PTR_ERR(th);
646 goto BadDevice;
647 }
648 wake_up_process(th);
649
650
651 result = ENE_Read_BYTE(us, REG_CARD_STATUS, &MiscReg03);
652 if (result != USB_STOR_XFER_GOOD) {
653 result = USB_STOR_TRANSPORT_ERROR;
654 quiesce_and_remove_host(us);
655 goto BadDevice;
656 }
657
658 if (!(MiscReg03 & 0x02)) {
659 result = -ENODEV;
660 quiesce_and_remove_host(us);
661 pr_info("keucr: The driver only supports SM/MS card.\
662 To use SD card, \
663 please build driver/usb/storage/ums-eneub6250.ko\n");
664 goto BadDevice;
665 }
666
667 return 0;
668
669
670BadDevice:
671 pr_info("usb --- eucr_probe failed\n");
672 release_everything(us);
673 return result;
674}
675
676
677static void eucr_disconnect(struct usb_interface *intf)
678{
679 struct us_data *us = usb_get_intfdata(intf);
680
681 pr_info("usb --- eucr_disconnect\n");
682 quiesce_and_remove_host(us);
683 release_everything(us);
684}
685
686
687
688
689
690
691static struct usb_driver usb_storage_driver = {
692 .name = "eucr",
693 .probe = eucr_probe,
694 .suspend = eucr_suspend,
695 .resume = eucr_resume,
696 .reset_resume = eucr_reset_resume,
697 .disconnect = eucr_disconnect,
698 .pre_reset = eucr_pre_reset,
699 .post_reset = eucr_post_reset,
700 .id_table = eucr_usb_ids,
701 .soft_unbind = 1,
702};
703
704
705static int __init usb_stor_init(void)
706{
707 int retval;
708 pr_info("usb --- usb_stor_init start\n");
709
710 retval = usb_register(&usb_storage_driver);
711 if (retval == 0)
712 pr_info("ENE USB Mass Storage support registered.\n");
713
714 return retval;
715}
716
717
718static void __exit usb_stor_exit(void)
719{
720 pr_info("usb --- usb_stor_exit\n");
721
722 usb_deregister(&usb_storage_driver) ;
723}
724
725module_init(usb_stor_init);
726module_exit(usb_stor_exit);
727