wl12xx: set the actual tid instead of the ac
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / keucr / usb.c
blob8c2332ec4f5c20ac4c2d161338b9d641fafe9d51
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>
11 #include <scsi/scsi.h>
12 #include <scsi/scsi_cmnd.h>
13 #include <scsi/scsi_device.h>
15 #include "usb.h"
16 #include "scsiglue.h"
17 #include "transport.h"
19 /* Some informational data */
20 MODULE_AUTHOR("Domao");
21 MODULE_DESCRIPTION("ENE USB Mass Storage driver for Linux");
22 MODULE_LICENSE("GPL");
24 static unsigned int delay_use = 1;
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 */
32 MODULE_DEVICE_TABLE (usb, eucr_usb_ids);
35 #ifdef CONFIG_PM
37 int eucr_suspend(struct usb_interface *iface, pm_message_t message)
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);
44 //US_DEBUGP("%s\n", __func__);
45 if (us->suspend_resume_hook)
46 (us->suspend_resume_hook)(us, US_SUSPEND);
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;
53 mutex_unlock(&us->dev_mutex);
54 return 0;
56 //EXPORT_SYMBOL_GPL(eucr_suspend);
58 int eucr_resume(struct usb_interface *iface)
60 BYTE tmp = 0;
62 struct us_data *us = usb_get_intfdata(iface);
63 printk("--- eucr_resume---\n");
64 mutex_lock(&us->dev_mutex);
66 //US_DEBUGP("%s\n", __func__);
67 if (us->suspend_resume_hook)
68 (us->suspend_resume_hook)(us, US_RESUME);
71 mutex_unlock(&us->dev_mutex);
74 us->Power_IsResum = true;
76 //us->SD_Status.Ready = 0; //??
77 us->MS_Status = *(PMS_STATUS)&tmp;
78 us->SM_Status = *(PSM_STATUS)&tmp;
80 return 0;
82 //EXPORT_SYMBOL_GPL(eucr_resume);
83 int eucr_reset_resume(struct usb_interface *iface)
85 BYTE tmp = 0;
86 struct us_data *us = usb_get_intfdata(iface);
88 printk("--- eucr_reset_resume---\n");
89 //US_DEBUGP("%s\n", __func__);
91 /* Report the reset to the SCSI core */
92 usb_stor_report_bus_reset(us);
94 /* FIXME: Notify the subdrivers that they need to reinitialize
95 * the device */
96 //ENE_InitMedia(us);
97 us->Power_IsResum = true;
99 //us->SD_Status.Ready = 0; //??
100 us->MS_Status = *(PMS_STATUS)&tmp;
101 us->SM_Status = *(PSM_STATUS)&tmp;
102 return 0;
104 //EXPORT_SYMBOL_GPL(usb_stor_reset_resume);
106 #else
108 #define eucr_suspend NULL
109 #define eucr_resume NULL
110 #define eucr_reset_resume NULL
112 #endif
114 //----- eucr_pre_reset() ---------------------
115 static int eucr_pre_reset(struct usb_interface *iface)
117 struct us_data *us = usb_get_intfdata(iface);
119 printk("usb --- eucr_pre_reset\n");
121 /* Make sure no command runs during the reset */
122 mutex_lock(&us->dev_mutex);
123 return 0;
126 //----- eucr_post_reset() ---------------------
127 static int eucr_post_reset(struct usb_interface *iface)
129 struct us_data *us = usb_get_intfdata(iface);
131 printk("usb --- eucr_post_reset\n");
133 /* Report the reset to the SCSI core */
134 usb_stor_report_bus_reset(us);
136 mutex_unlock(&us->dev_mutex);
137 return 0;
140 //----- fill_inquiry_response() ---------------------
141 void fill_inquiry_response(struct us_data *us, unsigned char *data, unsigned int data_len)
143 printk("usb --- fill_inquiry_response\n");
144 if (data_len<36) // You lose.
145 return;
147 if (data[0]&0x20)
149 memset(data+8,0,28);
151 else
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);
165 usb_stor_set_xfer_buf(us, data, data_len, us->srb, TO_XFER_BUF);
168 //----- usb_stor_control_thread() ---------------------
169 static int usb_stor_control_thread(void * __us)
171 struct us_data *us = (struct us_data *)__us;
172 struct Scsi_Host *host = us_to_host(us);
174 printk("usb --- usb_stor_control_thread\n");
175 for(;;)
177 if (wait_for_completion_interruptible(&us->cmnd_ready))
178 break;
180 /* lock the device pointers */
181 mutex_lock(&(us->dev_mutex));
183 /* if the device has disconnected, we are free to exit */
184 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
185 mutex_unlock(&us->dev_mutex);
186 break;
189 /* lock access to the state */
190 scsi_lock(host);
192 /* When we are called with no command pending, we're done */
193 if (us->srb == NULL)
195 scsi_unlock(host);
196 mutex_unlock(&us->dev_mutex);
197 //US_DEBUGP("-- exiting\n");
198 break;
201 /* has the command timed out *already* ? */
202 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags))
204 us->srb->result = DID_ABORT << 16;
205 goto SkipForAbort;
208 scsi_unlock(host);
210 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL)
212 us->srb->result = DID_ERROR << 16;
214 else if (us->srb->device->id && !(us->fflags & US_FL_SCM_MULT_TARG))
216 us->srb->result = DID_BAD_TARGET << 16;
218 else if (us->srb->device->lun > us->max_lun)
220 us->srb->result = DID_BAD_TARGET << 16;
222 else if ((us->srb->cmnd[0] == INQUIRY) && (us->fflags & US_FL_FIX_INQUIRY))
224 unsigned char data_ptr[36] = {0x00, 0x80, 0x02, 0x02, 0x1F, 0x00, 0x00, 0x00};
226 fill_inquiry_response(us, data_ptr, 36);
227 us->srb->result = SAM_STAT_GOOD;
229 else
231 us->proto_handler(us->srb, us);
234 /* lock access to the state */
235 scsi_lock(host);
237 /* indicate that the command is done */
238 if (us->srb->result != DID_ABORT << 16)
240 us->srb->scsi_done(us->srb);
242 else
244 SkipForAbort:
245 printk("scsi command aborted\n");
248 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags))
250 complete(&(us->notify));
252 /* Allow USB transfers to resume */
253 clear_bit(US_FLIDX_ABORTING, &us->dflags);
254 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
257 /* finished working on this command */
258 us->srb = NULL;
259 scsi_unlock(host);
261 /* unlock the device pointers */
262 mutex_unlock(&us->dev_mutex);
263 } /* for (;;) */
265 /* Wait until we are told to stop */
266 for (;;)
268 set_current_state(TASK_INTERRUPTIBLE);
269 if (kthread_should_stop())
270 break;
271 schedule();
273 __set_current_state(TASK_RUNNING);
274 return 0;
277 //----- associate_dev() ---------------------
278 static int associate_dev(struct us_data *us, struct usb_interface *intf)
280 printk("usb --- associate_dev\n");
282 /* Fill in the device-related fields */
283 us->pusb_dev = interface_to_usbdev(intf);
284 us->pusb_intf = intf;
285 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
287 /* Store our private data in the interface */
288 usb_set_intfdata(intf, us);
290 /* Allocate the device-related DMA-mapped buffers */
291 us->cr = usb_alloc_coherent(us->pusb_dev, sizeof(*us->cr), GFP_KERNEL, &us->cr_dma);
292 if (!us->cr)
294 printk("usb_ctrlrequest allocation failed\n");
295 return -ENOMEM;
298 us->iobuf = usb_alloc_coherent(us->pusb_dev, US_IOBUF_SIZE, GFP_KERNEL, &us->iobuf_dma);
299 if (!us->iobuf)
301 printk("I/O buffer allocation failed\n");
302 return -ENOMEM;
305 us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
306 if (!us->sensebuf)
308 printk("Sense buffer allocation failed\n");
309 return -ENOMEM;
311 return 0;
314 //----- get_device_info() ---------------------
315 static int get_device_info(struct us_data *us, const struct usb_device_id *id)
317 struct usb_device *dev = us->pusb_dev;
318 struct usb_interface_descriptor *idesc = &us->pusb_intf->cur_altsetting->desc;
320 printk("usb --- get_device_info\n");
322 us->subclass = idesc->bInterfaceSubClass;
323 us->protocol = idesc->bInterfaceProtocol;
324 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
325 us->Power_IsResum = false;
327 if (us->fflags & US_FL_IGNORE_DEVICE)
329 printk("device ignored\n");
330 return -ENODEV;
333 if (dev->speed != USB_SPEED_HIGH)
334 us->fflags &= ~US_FL_GO_SLOW;
336 return 0;
339 //----- get_transport() ---------------------
340 static int get_transport(struct us_data *us)
342 printk("usb --- get_transport\n");
343 switch (us->protocol) {
344 case USB_PR_BULK:
345 us->transport_name = "Bulk";
346 us->transport = usb_stor_Bulk_transport;
347 us->transport_reset = usb_stor_Bulk_reset;
348 break;
350 default:
351 return -EIO;
353 //printk("Transport: %s\n", us->transport_name);
355 /* fix for single-lun devices */
356 if (us->fflags & US_FL_SINGLE_LUN)
357 us->max_lun = 0;
358 return 0;
361 //----- get_protocol() ---------------------
362 static int get_protocol(struct us_data *us)
364 printk("usb --- get_protocol\n");
365 printk("us->pusb_dev->descriptor.idVendor = %x\n", us->pusb_dev->descriptor.idVendor);
366 printk("us->pusb_dev->descriptor.idProduct = %x\n", us->pusb_dev->descriptor.idProduct);
367 switch (us->subclass) {
368 case USB_SC_SCSI:
369 us->protocol_name = "Transparent SCSI";
370 if( (us->pusb_dev->descriptor.idVendor == 0x0CF2) && (us->pusb_dev->descriptor.idProduct == 0x6250) )
371 us->proto_handler = ENE_stor_invoke_transport;
372 else
373 us->proto_handler = usb_stor_invoke_transport;
374 break;
376 default:
377 return -EIO;
379 //printk("Protocol: %s\n", us->protocol_name);
380 return 0;
383 //----- get_pipes() ---------------------
384 static int get_pipes(struct us_data *us)
386 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
387 int i;
388 struct usb_endpoint_descriptor *ep;
389 struct usb_endpoint_descriptor *ep_in = NULL;
390 struct usb_endpoint_descriptor *ep_out = NULL;
391 struct usb_endpoint_descriptor *ep_int = NULL;
393 printk("usb --- get_pipes\n");
395 for (i = 0; i < altsetting->desc.bNumEndpoints; i++)
397 ep = &altsetting->endpoint[i].desc;
399 if (usb_endpoint_xfer_bulk(ep))
401 if (usb_endpoint_dir_in(ep))
403 if (!ep_in)
404 ep_in = ep;
406 else
408 if (!ep_out)
409 ep_out = ep;
412 else if (usb_endpoint_is_int_in(ep))
414 if (!ep_int)
415 ep_int = ep;
419 if (!ep_in || !ep_out || (us->protocol == USB_PR_CBI && !ep_int))
421 printk("Endpoint sanity check failed! Rejecting dev.\n");
422 return -EIO;
425 /* Calculate and store the pipe values */
426 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
427 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
428 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev, ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
429 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev, ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
430 if (ep_int)
432 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev, ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
433 us->ep_bInterval = ep_int->bInterval;
435 return 0;
438 //----- usb_stor_acquire_resources() ---------------------
439 static int usb_stor_acquire_resources(struct us_data *us)
441 struct task_struct *th;
443 printk("usb --- usb_stor_acquire_resources\n");
444 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
445 if (!us->current_urb)
447 printk("URB allocation failed\n");
448 return -ENOMEM;
451 /* Start up our control thread */
452 th = kthread_run(usb_stor_control_thread, us, "eucr-storage");
453 if (IS_ERR(th))
455 printk("Unable to start control thread\n");
456 return PTR_ERR(th);
458 us->ctl_thread = th;
460 return 0;
463 //----- usb_stor_release_resources() ---------------------
464 static void usb_stor_release_resources(struct us_data *us)
466 printk("usb --- usb_stor_release_resources\n");
468 SM_FreeMem();
470 complete(&us->cmnd_ready);
471 if (us->ctl_thread)
472 kthread_stop(us->ctl_thread);
474 /* Call the destructor routine, if it exists */
475 if (us->extra_destructor)
477 printk("-- calling extra_destructor()\n");
478 us->extra_destructor(us->extra);
481 /* Free the extra data and the URB */
482 kfree(us->extra);
483 usb_free_urb(us->current_urb);
486 //----- dissociate_dev() ---------------------
487 static void dissociate_dev(struct us_data *us)
489 printk("usb --- dissociate_dev\n");
491 kfree(us->sensebuf);
493 /* Free the device-related DMA-mapped buffers */
494 if (us->cr)
495 usb_free_coherent(us->pusb_dev, sizeof(*us->cr), us->cr, us->cr_dma);
496 if (us->iobuf)
497 usb_free_coherent(us->pusb_dev, US_IOBUF_SIZE, us->iobuf, us->iobuf_dma);
499 /* Remove our private data from the interface */
500 usb_set_intfdata(us->pusb_intf, NULL);
503 //----- quiesce_and_remove_host() ---------------------
504 static void quiesce_and_remove_host(struct us_data *us)
506 struct Scsi_Host *host = us_to_host(us);
508 printk("usb --- quiesce_and_remove_host\n");
510 /* If the device is really gone, cut short reset delays */
511 if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
512 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
514 /* Prevent SCSI-scanning (if it hasn't started yet)
515 * and wait for the SCSI-scanning thread to stop.
517 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
518 wake_up(&us->delay_wait);
519 wait_for_completion(&us->scanning_done);
521 /* Removing the host will perform an orderly shutdown: caches
522 * synchronized, disks spun down, etc.
524 scsi_remove_host(host);
526 /* Prevent any new commands from being accepted and cut short
527 * reset delays.
529 scsi_lock(host);
530 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
531 scsi_unlock(host);
532 wake_up(&us->delay_wait);
535 //----- release_everything() ---------------------
536 static void release_everything(struct us_data *us)
538 printk("usb --- release_everything\n");
540 usb_stor_release_resources(us);
541 dissociate_dev(us);
542 scsi_host_put(us_to_host(us));
545 //----- usb_stor_scan_thread() ---------------------
546 static int usb_stor_scan_thread(void * __us)
548 struct us_data *us = (struct us_data *)__us;
550 printk("usb --- usb_stor_scan_thread\n");
551 printk("EUCR : device found at %d\n", us->pusb_dev->devnum);
553 set_freezable();
554 /* Wait for the timeout to expire or for a disconnect */
555 if (delay_use > 0) {
556 wait_event_freezable_timeout(us->delay_wait,
557 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
558 delay_use * HZ);
561 /* If the device is still connected, perform the scanning */
562 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags))
564 /* For bulk-only devices, determine the max LUN value */
565 if (us->protocol == USB_PR_BULK && !(us->fflags & US_FL_SINGLE_LUN))
567 mutex_lock(&us->dev_mutex);
568 us->max_lun = usb_stor_Bulk_max_lun(us);
569 mutex_unlock(&us->dev_mutex);
571 scsi_scan_host(us_to_host(us));
572 printk("EUCR : device scan complete\n");
574 complete_and_exit(&us->scanning_done, 0);
577 //----- eucr_probe() ---------------------
578 static int eucr_probe(struct usb_interface *intf, const struct usb_device_id *id)
580 struct Scsi_Host *host;
581 struct us_data *us;
582 int result;
583 BYTE MiscReg03 = 0;
584 struct task_struct *th;
586 printk("usb --- eucr_probe\n");
588 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
589 if (!host)
591 printk("Unable to allocate the scsi host\n");
592 return -ENOMEM;
595 /* Allow 16-byte CDBs and thus > 2TB */
596 host->max_cmd_len = 16;
597 us = host_to_us(host);
598 memset(us, 0, sizeof(struct us_data));
599 mutex_init(&(us->dev_mutex));
600 init_completion(&us->cmnd_ready);
601 init_completion(&(us->notify));
602 init_waitqueue_head(&us->delay_wait);
603 init_completion(&us->scanning_done);
605 /* Associate the us_data structure with the USB device */
606 result = associate_dev(us, intf);
607 if (result)
608 goto BadDevice;
610 /* Get Device info */
611 result = get_device_info(us, id);
612 if (result)
613 goto BadDevice;
615 /* Get the transport, protocol, and pipe settings */
616 result = get_transport(us);
617 if (result)
618 goto BadDevice;
619 result = get_protocol(us);
620 if (result)
621 goto BadDevice;
622 result = get_pipes(us);
623 if (result)
624 goto BadDevice;
626 /* Acquire all the other resources and add the host */
627 result = usb_stor_acquire_resources(us);
628 if (result)
629 goto BadDevice;
631 result = scsi_add_host(host, &intf->dev);
632 if (result)
634 printk("Unable to add the scsi host\n");
635 goto BadDevice;
638 /* Start up the thread for delayed SCSI-device scanning */
639 th = kthread_create(usb_stor_scan_thread, us, "eucr-stor-scan");
640 if (IS_ERR(th))
642 printk("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;
648 wake_up_process(th);
650 /* probe card type */
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;
658 if (!(MiscReg03 & 0x02)) {
659 result = -ENODEV;
660 quiesce_and_remove_host(us);
661 printk(KERN_NOTICE "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;
667 return 0;
669 /* We come here if there are any problems */
670 BadDevice:
671 printk("usb --- eucr_probe failed\n");
672 release_everything(us);
673 return result;
676 //----- eucr_disconnect() ---------------------
677 static void eucr_disconnect(struct usb_interface *intf)
679 struct us_data *us = usb_get_intfdata(intf);
681 printk("usb --- eucr_disconnect\n");
682 quiesce_and_remove_host(us);
683 release_everything(us);
686 /***********************************************************************
687 * Initialization and registration
688 ***********************************************************************/
690 //----- usb_storage_driver() ---------------------
691 static 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,
704 //----- usb_stor_init() ---------------------
705 static int __init usb_stor_init(void)
707 int retval;
708 printk("usb --- usb_stor_init start\n");
710 retval = usb_register(&usb_storage_driver);
711 if (retval == 0)
712 printk("ENE USB Mass Storage support registered.\n");
714 return retval;
717 //----- usb_stor_exit() ---------------------
718 static void __exit usb_stor_exit(void)
720 printk("usb --- usb_stor_exit\n");
722 usb_deregister(&usb_storage_driver) ;
725 module_init(usb_stor_init);
726 module_exit(usb_stor_exit);