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