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