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