Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[firefly-linux-kernel-4.4.55.git] / drivers / staging / keucr / usb.c
1 #include <linux/sched.h>
2 #include <linux/errno.h>
3 #include <linux/freezer.h>
4 #include <linux/module.h>
5 #include <linux/slab.h>
6 #include <linux/kthread.h>
7 #include <linux/mutex.h>
8 #include <linux/utsname.h>
9
10 #include <scsi/scsi.h>
11 #include <scsi/scsi_cmnd.h>
12 #include <scsi/scsi_device.h>
13
14 #include "usb.h"
15 #include "scsiglue.h"
16 #include "smil.h"
17 #include "transport.h"
18
19 /* Some informational data */
20 MODULE_AUTHOR("Domao");
21 MODULE_DESCRIPTION("ENE USB Mass Storage driver for Linux");
22 MODULE_LICENSE("GPL");
23
24 static unsigned int delay_use = 1;
25
26 static struct usb_device_id eucr_usb_ids[] = {
27         { USB_DEVICE(0x058f, 0x6366) },
28         { USB_DEVICE(0x0cf2, 0x6230) },
29         { USB_DEVICE(0x0cf2, 0x6250) },
30         { }                                            /* Terminating entry */
31 };
32 MODULE_DEVICE_TABLE(usb, eucr_usb_ids);
33
34
35 #ifdef CONFIG_PM
36
37 static int eucr_suspend(struct usb_interface *iface, pm_message_t message)
38 {
39         struct us_data *us = usb_get_intfdata(iface);
40         pr_info("--- eucr_suspend ---\n");
41         /* Wait until no command is running */
42         mutex_lock(&us->dev_mutex);
43
44         if (us->suspend_resume_hook)
45                 (us->suspend_resume_hook)(us, US_SUSPEND);
46
47         mutex_unlock(&us->dev_mutex);
48         return 0;
49 }
50
51 static int eucr_resume(struct usb_interface *iface)
52 {
53         u8    tmp = 0;
54
55         struct us_data *us = usb_get_intfdata(iface);
56         pr_info("--- eucr_resume---\n");
57         mutex_lock(&us->dev_mutex);
58
59         if (us->suspend_resume_hook)
60                 (us->suspend_resume_hook)(us, US_RESUME);
61
62
63         mutex_unlock(&us->dev_mutex);
64
65         us->Power_IsResum = true;
66
67         us->SM_Status = *(struct keucr_sm_status *)&tmp;
68
69         return 0;
70 }
71
72 static int eucr_reset_resume(struct usb_interface *iface)
73 {
74         u8    tmp = 0;
75         struct us_data *us = usb_get_intfdata(iface);
76
77         pr_info("--- eucr_reset_resume---\n");
78
79         /* Report the reset to the SCSI core */
80         usb_stor_report_bus_reset(us);
81
82         /*
83          * FIXME: Notify the subdrivers that they need to reinitialize
84          * the device
85          */
86
87         us->Power_IsResum = true;
88
89         us->SM_Status = *(struct keucr_sm_status *)&tmp;
90
91         return 0;
92 }
93
94 #else
95
96 #define eucr_suspend            NULL
97 #define eucr_resume             NULL
98 #define eucr_reset_resume       NULL
99
100 #endif
101
102 static int eucr_pre_reset(struct usb_interface *iface)
103 {
104         struct us_data *us = usb_get_intfdata(iface);
105
106         pr_info("usb --- eucr_pre_reset\n");
107
108         /* Make sure no command runs during the reset */
109         mutex_lock(&us->dev_mutex);
110         return 0;
111 }
112
113 static int eucr_post_reset(struct usb_interface *iface)
114 {
115         struct us_data *us = usb_get_intfdata(iface);
116
117         pr_info("usb --- eucr_post_reset\n");
118
119         /* Report the reset to the SCSI core */
120         usb_stor_report_bus_reset(us);
121
122         mutex_unlock(&us->dev_mutex);
123         return 0;
124 }
125
126 void fill_inquiry_response(struct us_data *us, unsigned char *data,
127                                                         unsigned int data_len)
128 {
129         pr_info("usb --- fill_inquiry_response\n");
130         if (data_len < 36) /* You lose. */
131                 return;
132
133         if (data[0]&0x20) {
134                 memset(data+8, 0, 28);
135         } else {
136                 u16 bcdDevice =
137                         le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
138                 memcpy(data+8, us->unusual_dev->vendorName,
139                         strlen(us->unusual_dev->vendorName) > 8 ? 8 :
140                         strlen(us->unusual_dev->vendorName));
141                 memcpy(data+16, us->unusual_dev->productName,
142                         strlen(us->unusual_dev->productName) > 16 ? 16 :
143                         strlen(us->unusual_dev->productName));
144                 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
145                 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
146                 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
147                 data[35] = 0x30 + ((bcdDevice) & 0x0F);
148         }
149         usb_stor_set_xfer_buf(us, data, data_len, us->srb, TO_XFER_BUF);
150 }
151
152 static int usb_stor_control_thread(void *__us)
153 {
154         struct us_data *us = (struct us_data *)__us;
155         struct Scsi_Host *host = us_to_host(us);
156
157         pr_info("usb --- usb_stor_control_thread\n");
158         for (;;) {
159                 if (wait_for_completion_interruptible(&us->cmnd_ready))
160                         break;
161
162                 /* lock the device pointers */
163                 mutex_lock(&(us->dev_mutex));
164
165                 /* if the device has disconnected, we are free to exit */
166                 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
167                         mutex_unlock(&us->dev_mutex);
168                         break;
169                 }
170
171                 /* lock access to the state */
172                 scsi_lock(host);
173
174                 /* When we are called with no command pending, we're done */
175                 if (us->srb == NULL) {
176                         scsi_unlock(host);
177                         mutex_unlock(&us->dev_mutex);
178                         break;
179                 }
180
181                 /* has the command timed out *already* ? */
182                 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
183                         us->srb->result = DID_ABORT << 16;
184                         goto SkipForAbort;
185                 }
186
187                 scsi_unlock(host);
188
189                 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
190                         us->srb->result = DID_ERROR << 16;
191                 } else if (us->srb->device->id
192                            && !(us->fflags & US_FL_SCM_MULT_TARG)) {
193                         us->srb->result = DID_BAD_TARGET << 16;
194                 } else if (us->srb->device->lun > us->max_lun) {
195                         us->srb->result = DID_BAD_TARGET << 16;
196                 } else if ((us->srb->cmnd[0] == INQUIRY)
197                            && (us->fflags & US_FL_FIX_INQUIRY)) {
198                         unsigned char data_ptr[36] = {0x00, 0x80, 0x02, 0x02,
199                                                 0x1F, 0x00, 0x00, 0x00};
200
201                         fill_inquiry_response(us, data_ptr, 36);
202                         us->srb->result = SAM_STAT_GOOD;
203                 } else {
204                         us->proto_handler(us->srb, us);
205                 }
206
207                 /* lock access to the state */
208                 scsi_lock(host);
209
210                 /* indicate that the command is done */
211                 if (us->srb->result != DID_ABORT << 16) {
212                         us->srb->scsi_done(us->srb);
213                 } else {
214 SkipForAbort:
215                         pr_info("scsi command aborted\n");
216                 }
217
218                 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
219                         complete(&(us->notify));
220
221                         /* Allow USB transfers to resume */
222                         clear_bit(US_FLIDX_ABORTING, &us->dflags);
223                         clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
224                 }
225
226                 /* finished working on this command */
227                 us->srb = NULL;
228                 scsi_unlock(host);
229
230                 /* unlock the device pointers */
231                 mutex_unlock(&us->dev_mutex);
232         } /* for (;;) */
233
234         /* Wait until we are told to stop */
235         for (;;) {
236                 set_current_state(TASK_INTERRUPTIBLE);
237                 if (kthread_should_stop())
238                         break;
239                 schedule();
240         }
241         __set_current_state(TASK_RUNNING);
242         return 0;
243 }
244
245 static int associate_dev(struct us_data *us, struct usb_interface *intf)
246 {
247         pr_info("usb --- associate_dev\n");
248
249         /* Fill in the device-related fields */
250         us->pusb_dev = interface_to_usbdev(intf);
251         us->pusb_intf = intf;
252         us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
253
254         /* Store our private data in the interface */
255         usb_set_intfdata(intf, us);
256
257         /* Allocate the device-related DMA-mapped buffers */
258         us->cr = usb_alloc_coherent(us->pusb_dev, sizeof(*us->cr), GFP_KERNEL,
259                                                         &us->cr_dma);
260         if (!us->cr) {
261                 pr_info("usb_ctrlrequest allocation failed\n");
262                 return -ENOMEM;
263         }
264
265         us->iobuf = usb_alloc_coherent(us->pusb_dev, US_IOBUF_SIZE, GFP_KERNEL,
266                                                         &us->iobuf_dma);
267         if (!us->iobuf) {
268                 pr_info("I/O buffer allocation failed\n");
269                 return -ENOMEM;
270         }
271
272         us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
273         if (!us->sensebuf)
274                 return -ENOMEM;
275
276         return 0;
277 }
278
279 static int get_device_info(struct us_data *us, const struct usb_device_id *id)
280 {
281         struct usb_device *dev = us->pusb_dev;
282         struct usb_interface_descriptor *idesc =
283                                         &us->pusb_intf->cur_altsetting->desc;
284
285         pr_info("usb --- get_device_info\n");
286
287         us->subclass = idesc->bInterfaceSubClass;
288         us->protocol = idesc->bInterfaceProtocol;
289         us->fflags = id->driver_info;
290         us->Power_IsResum = false;
291
292         if (us->fflags & US_FL_IGNORE_DEVICE) {
293                 pr_info("device ignored\n");
294                 return -ENODEV;
295         }
296
297         if (dev->speed != USB_SPEED_HIGH)
298                 us->fflags &= ~US_FL_GO_SLOW;
299
300         return 0;
301 }
302
303 static int get_transport(struct us_data *us)
304 {
305         pr_info("usb --- get_transport\n");
306         switch (us->protocol) {
307         case USB_PR_BULK:
308                 us->transport_name = "Bulk";
309                 us->transport = usb_stor_Bulk_transport;
310                 us->transport_reset = usb_stor_Bulk_reset;
311                 break;
312
313         default:
314                 return -EIO;
315         }
316
317         /* fix for single-lun devices */
318         if (us->fflags & US_FL_SINGLE_LUN)
319                 us->max_lun = 0;
320         return 0;
321 }
322
323 static int get_protocol(struct us_data *us)
324 {
325         pr_info("usb --- get_protocol\n");
326         pr_info("us->pusb_dev->descriptor.idVendor = %x\n",
327                         us->pusb_dev->descriptor.idVendor);
328         pr_info("us->pusb_dev->descriptor.idProduct = %x\n",
329                         us->pusb_dev->descriptor.idProduct);
330         switch (us->subclass) {
331         case USB_SC_SCSI:
332                 us->protocol_name = "Transparent SCSI";
333                 if ((us->pusb_dev->descriptor.idVendor == 0x0CF2)
334                     && (us->pusb_dev->descriptor.idProduct == 0x6250))
335                         us->proto_handler = ENE_stor_invoke_transport;
336                 else
337                         us->proto_handler = usb_stor_invoke_transport;
338                 break;
339
340         default:
341                 return -EIO;
342         }
343         return 0;
344 }
345
346 static int get_pipes(struct us_data *us)
347 {
348         struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
349         int i;
350         struct usb_endpoint_descriptor *ep;
351         struct usb_endpoint_descriptor *ep_in = NULL;
352         struct usb_endpoint_descriptor *ep_out = NULL;
353         struct usb_endpoint_descriptor *ep_int = NULL;
354
355         pr_info("usb --- get_pipes\n");
356
357         for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
358                 ep = &altsetting->endpoint[i].desc;
359
360                 if (usb_endpoint_xfer_bulk(ep)) {
361                         if (usb_endpoint_dir_in(ep)) {
362                                 if (!ep_in)
363                                         ep_in = ep;
364                         } else {
365                                 if (!ep_out)
366                                         ep_out = ep;
367                         }
368                 } else if (usb_endpoint_is_int_in(ep)) {
369                         if (!ep_int)
370                                 ep_int = ep;
371                 }
372         }
373
374         if (!ep_in || !ep_out || (us->protocol == USB_PR_CBI && !ep_int)) {
375                 pr_info("Endpoint sanity check failed! Rejecting dev.\n");
376                 return -EIO;
377         }
378
379         /* Calculate and store the pipe values */
380         us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
381         us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
382         us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
383                         ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
384         us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
385                         ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
386         if (ep_int) {
387                 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
388                         ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
389                 us->ep_bInterval = ep_int->bInterval;
390         }
391         return 0;
392 }
393
394 static int usb_stor_acquire_resources(struct us_data *us)
395 {
396         struct task_struct *th;
397
398         pr_info("usb --- usb_stor_acquire_resources\n");
399         us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
400         if (!us->current_urb) {
401                 pr_info("URB allocation failed\n");
402                 return -ENOMEM;
403         }
404
405         /* Start up our control thread */
406         th = kthread_run(usb_stor_control_thread, us, "eucr-storage");
407         if (IS_ERR(th)) {
408                 pr_info("Unable to start control thread\n");
409                 return PTR_ERR(th);
410         }
411         us->ctl_thread = th;
412
413         return 0;
414 }
415
416 static void usb_stor_release_resources(struct us_data *us)
417 {
418         pr_info("usb --- usb_stor_release_resources\n");
419
420         SM_FreeMem();
421
422         complete(&us->cmnd_ready);
423         if (us->ctl_thread)
424                 kthread_stop(us->ctl_thread);
425
426         /* Call the destructor routine, if it exists */
427         if (us->extra_destructor) {
428                 pr_info("-- calling extra_destructor()\n");
429                 us->extra_destructor(us->extra);
430         }
431
432         /* Free the extra data and the URB */
433         kfree(us->extra);
434         usb_free_urb(us->current_urb);
435 }
436
437 static void dissociate_dev(struct us_data *us)
438 {
439         pr_info("usb --- dissociate_dev\n");
440
441         kfree(us->sensebuf);
442
443         /* Free the device-related DMA-mapped buffers */
444         usb_free_coherent(us->pusb_dev, sizeof(*us->cr), us->cr, us->cr_dma);
445         usb_free_coherent(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
446                           us->iobuf_dma);
447
448         /* Remove our private data from the interface */
449         usb_set_intfdata(us->pusb_intf, NULL);
450 }
451
452 static void quiesce_and_remove_host(struct us_data *us)
453 {
454         struct Scsi_Host *host = us_to_host(us);
455
456         pr_info("usb --- quiesce_and_remove_host\n");
457
458         /* If the device is really gone, cut short reset delays */
459         if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
460                 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
461
462         /*
463          * Prevent SCSI-scanning (if it hasn't started yet)
464          * and wait for the SCSI-scanning thread to stop.
465          */
466         set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
467         wake_up(&us->delay_wait);
468         wait_for_completion(&us->scanning_done);
469
470         /*
471          * Removing the host will perform an orderly shutdown: caches
472          * synchronized, disks spun down, etc.
473          */
474         scsi_remove_host(host);
475
476         /*
477          * Prevent any new commands from being accepted and cut short
478          * reset delays.
479          */
480         scsi_lock(host);
481         set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
482         scsi_unlock(host);
483         wake_up(&us->delay_wait);
484 }
485
486 static void release_everything(struct us_data *us)
487 {
488         pr_info("usb --- release_everything\n");
489
490         usb_stor_release_resources(us);
491         dissociate_dev(us);
492         scsi_host_put(us_to_host(us));
493 }
494
495 static int usb_stor_scan_thread(void *__us)
496 {
497         struct us_data *us = (struct us_data *)__us;
498
499         pr_info("usb --- usb_stor_scan_thread\n");
500         pr_info("EUCR : device found at %d\n", us->pusb_dev->devnum);
501
502         set_freezable();
503         /* Wait for the timeout to expire or for a disconnect */
504         if (delay_use > 0) {
505                 wait_event_freezable_timeout(us->delay_wait,
506                                 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
507                                 delay_use * HZ);
508         }
509
510         /* If the device is still connected, perform the scanning */
511         if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
512                 /* For bulk-only devices, determine the max LUN value */
513                 if (us->protocol == USB_PR_BULK
514                     && !(us->fflags & US_FL_SINGLE_LUN)) {
515                         mutex_lock(&us->dev_mutex);
516                         us->max_lun = usb_stor_Bulk_max_lun(us);
517                         mutex_unlock(&us->dev_mutex);
518                 }
519                 scsi_scan_host(us_to_host(us));
520                 pr_info("EUCR : device scan complete\n");
521         }
522         complete_and_exit(&us->scanning_done, 0);
523 }
524
525 static int eucr_probe(struct usb_interface *intf,
526                                         const struct usb_device_id *id)
527 {
528         struct Scsi_Host *host;
529         struct us_data *us;
530         int result;
531         u8      MiscReg03 = 0;
532         struct task_struct *th;
533
534         pr_info("usb --- eucr_probe\n");
535
536         host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
537         if (!host) {
538                 pr_info("Unable to allocate the scsi host\n");
539                 return -ENOMEM;
540         }
541
542         /* Allow 16-byte CDBs and thus > 2TB */
543         host->max_cmd_len = 16;
544         us = host_to_us(host);
545         memset(us, 0, sizeof(struct us_data));
546         mutex_init(&(us->dev_mutex));
547         init_completion(&us->cmnd_ready);
548         init_completion(&(us->notify));
549         init_waitqueue_head(&us->delay_wait);
550         init_completion(&us->scanning_done);
551
552         /* Associate the us_data structure with the USB device */
553         result = associate_dev(us, intf);
554         if (result)
555                 goto BadDevice;
556
557         /* Get Device info */
558         result = get_device_info(us, id);
559         if (result)
560                 goto BadDevice;
561
562         /* Get the transport, protocol, and pipe settings */
563         result = get_transport(us);
564         if (result)
565                 goto BadDevice;
566         result = get_protocol(us);
567         if (result)
568                 goto BadDevice;
569         result = get_pipes(us);
570         if (result)
571                 goto BadDevice;
572
573         /* Acquire all the other resources and add the host */
574         result = usb_stor_acquire_resources(us);
575         if (result)
576                 goto BadDevice;
577
578         result = scsi_add_host(host, &intf->dev);
579         if (result) {
580                 pr_info("Unable to add the scsi host\n");
581                 goto BadDevice;
582         }
583
584         /* Start up the thread for delayed SCSI-device scanning */
585         th = kthread_create(usb_stor_scan_thread, us, "eucr-stor-scan");
586         if (IS_ERR(th)) {
587                 pr_info("Unable to start the device-scanning thread\n");
588                 complete(&us->scanning_done);
589                 quiesce_and_remove_host(us);
590                 result = PTR_ERR(th);
591                 goto BadDevice;
592         }
593         wake_up_process(th);
594
595         /* probe card type */
596         result = ene_read_byte(us, REG_CARD_STATUS, &MiscReg03);
597         if (result != USB_STOR_XFER_GOOD) {
598                 result = USB_STOR_TRANSPORT_ERROR;
599                 quiesce_and_remove_host(us);
600                 goto BadDevice;
601         }
602
603         if (!(MiscReg03 & 0x02)) {
604                 result = -ENODEV;
605                 quiesce_and_remove_host(us);
606                 pr_info("keucr: The driver only supports SM/MS card. To use SD card, please build driver/usb/storage/ums-eneub6250.ko\n");
607                 goto BadDevice;
608         }
609
610         return 0;
611
612         /* We come here if there are any problems */
613 BadDevice:
614         pr_info("usb --- eucr_probe failed\n");
615         release_everything(us);
616         return result;
617 }
618
619 static void eucr_disconnect(struct usb_interface *intf)
620 {
621         struct us_data *us = usb_get_intfdata(intf);
622
623         pr_info("usb --- eucr_disconnect\n");
624         quiesce_and_remove_host(us);
625         release_everything(us);
626 }
627
628 /* Initialization and registration */
629 static struct usb_driver usb_storage_driver = {
630         .name =         "eucr",
631         .probe =                eucr_probe,
632         .suspend =          eucr_suspend,
633         .resume =           eucr_resume,
634         .reset_resume = eucr_reset_resume,
635         .disconnect =   eucr_disconnect,
636         .pre_reset =    eucr_pre_reset,
637         .post_reset =   eucr_post_reset,
638         .id_table =             eucr_usb_ids,
639         .soft_unbind =  1,
640 };
641
642 module_usb_driver(usb_storage_driver);