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>
10 #include <scsi/scsi.h>
11 #include <scsi/scsi_cmnd.h>
12 #include <scsi/scsi_device.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
);
37 static int eucr_suspend(struct usb_interface
*iface
, pm_message_t message
)
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
);
44 if (us
->suspend_resume_hook
)
45 (us
->suspend_resume_hook
)(us
, US_SUSPEND
);
47 mutex_unlock(&us
->dev_mutex
);
51 static int eucr_resume(struct usb_interface
*iface
)
55 struct us_data
*us
= usb_get_intfdata(iface
);
56 pr_info("--- eucr_resume---\n");
57 mutex_lock(&us
->dev_mutex
);
59 if (us
->suspend_resume_hook
)
60 (us
->suspend_resume_hook
)(us
, US_RESUME
);
63 mutex_unlock(&us
->dev_mutex
);
65 us
->Power_IsResum
= true;
67 us
->SM_Status
= *(struct keucr_sm_status
*)&tmp
;
72 static int eucr_reset_resume(struct usb_interface
*iface
)
75 struct us_data
*us
= usb_get_intfdata(iface
);
77 pr_info("--- eucr_reset_resume---\n");
79 /* Report the reset to the SCSI core */
80 usb_stor_report_bus_reset(us
);
83 * FIXME: Notify the subdrivers that they need to reinitialize
87 us
->Power_IsResum
= true;
89 us
->SM_Status
= *(struct keucr_sm_status
*)&tmp
;
96 #define eucr_suspend NULL
97 #define eucr_resume NULL
98 #define eucr_reset_resume NULL
102 static int eucr_pre_reset(struct usb_interface
*iface
)
104 struct us_data
*us
= usb_get_intfdata(iface
);
106 pr_info("usb --- eucr_pre_reset\n");
108 /* Make sure no command runs during the reset */
109 mutex_lock(&us
->dev_mutex
);
113 static int eucr_post_reset(struct usb_interface
*iface
)
115 struct us_data
*us
= usb_get_intfdata(iface
);
117 pr_info("usb --- eucr_post_reset\n");
119 /* Report the reset to the SCSI core */
120 usb_stor_report_bus_reset(us
);
122 mutex_unlock(&us
->dev_mutex
);
126 void fill_inquiry_response(struct us_data
*us
, unsigned char *data
,
127 unsigned int data_len
)
129 pr_info("usb --- fill_inquiry_response\n");
130 if (data_len
< 36) /* You lose. */
134 memset(data
+8, 0, 28);
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);
149 usb_stor_set_xfer_buf(us
, data
, data_len
, us
->srb
, TO_XFER_BUF
);
152 static int usb_stor_control_thread(void *__us
)
154 struct us_data
*us
= (struct us_data
*)__us
;
155 struct Scsi_Host
*host
= us_to_host(us
);
157 pr_info("usb --- usb_stor_control_thread\n");
159 if (wait_for_completion_interruptible(&us
->cmnd_ready
))
162 /* lock the device pointers */
163 mutex_lock(&(us
->dev_mutex
));
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
);
171 /* lock access to the state */
174 /* When we are called with no command pending, we're done */
175 if (us
->srb
== NULL
) {
177 mutex_unlock(&us
->dev_mutex
);
181 /* has the command timed out *already* ? */
182 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
)) {
183 us
->srb
->result
= DID_ABORT
<< 16;
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};
201 fill_inquiry_response(us
, data_ptr
, 36);
202 us
->srb
->result
= SAM_STAT_GOOD
;
204 us
->proto_handler(us
->srb
, us
);
207 /* lock access to the state */
210 /* indicate that the command is done */
211 if (us
->srb
->result
!= DID_ABORT
<< 16) {
212 us
->srb
->scsi_done(us
->srb
);
215 pr_info("scsi command aborted\n");
218 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
)) {
219 complete(&(us
->notify
));
221 /* Allow USB transfers to resume */
222 clear_bit(US_FLIDX_ABORTING
, &us
->dflags
);
223 clear_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
);
226 /* finished working on this command */
230 /* unlock the device pointers */
231 mutex_unlock(&us
->dev_mutex
);
234 /* Wait until we are told to stop */
236 set_current_state(TASK_INTERRUPTIBLE
);
237 if (kthread_should_stop())
241 __set_current_state(TASK_RUNNING
);
245 static int associate_dev(struct us_data
*us
, struct usb_interface
*intf
)
247 pr_info("usb --- associate_dev\n");
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
;
254 /* Store our private data in the interface */
255 usb_set_intfdata(intf
, us
);
257 /* Allocate the device-related DMA-mapped buffers */
258 us
->cr
= usb_alloc_coherent(us
->pusb_dev
, sizeof(*us
->cr
), GFP_KERNEL
,
261 pr_info("usb_ctrlrequest allocation failed\n");
265 us
->iobuf
= usb_alloc_coherent(us
->pusb_dev
, US_IOBUF_SIZE
, GFP_KERNEL
,
268 pr_info("I/O buffer allocation failed\n");
272 us
->sensebuf
= kmalloc(US_SENSE_SIZE
, GFP_KERNEL
);
279 static int get_device_info(struct us_data
*us
, const struct usb_device_id
*id
)
281 struct usb_device
*dev
= us
->pusb_dev
;
282 struct usb_interface_descriptor
*idesc
=
283 &us
->pusb_intf
->cur_altsetting
->desc
;
285 pr_info("usb --- get_device_info\n");
287 us
->subclass
= idesc
->bInterfaceSubClass
;
288 us
->protocol
= idesc
->bInterfaceProtocol
;
289 us
->fflags
= id
->driver_info
;
290 us
->Power_IsResum
= false;
292 if (us
->fflags
& US_FL_IGNORE_DEVICE
) {
293 pr_info("device ignored\n");
297 if (dev
->speed
!= USB_SPEED_HIGH
)
298 us
->fflags
&= ~US_FL_GO_SLOW
;
303 static int get_transport(struct us_data
*us
)
305 pr_info("usb --- get_transport\n");
306 switch (us
->protocol
) {
308 us
->transport_name
= "Bulk";
309 us
->transport
= usb_stor_Bulk_transport
;
310 us
->transport_reset
= usb_stor_Bulk_reset
;
317 /* fix for single-lun devices */
318 if (us
->fflags
& US_FL_SINGLE_LUN
)
323 static int get_protocol(struct us_data
*us
)
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
) {
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
;
337 us
->proto_handler
= usb_stor_invoke_transport
;
346 static int get_pipes(struct us_data
*us
)
348 struct usb_host_interface
*altsetting
= us
->pusb_intf
->cur_altsetting
;
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
;
355 pr_info("usb --- get_pipes\n");
357 for (i
= 0; i
< altsetting
->desc
.bNumEndpoints
; i
++) {
358 ep
= &altsetting
->endpoint
[i
].desc
;
360 if (usb_endpoint_xfer_bulk(ep
)) {
361 if (usb_endpoint_dir_in(ep
)) {
368 } else if (usb_endpoint_is_int_in(ep
)) {
374 if (!ep_in
|| !ep_out
|| (us
->protocol
== USB_PR_CBI
&& !ep_int
)) {
375 pr_info("Endpoint sanity check failed! Rejecting dev.\n");
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
);
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
;
394 static int usb_stor_acquire_resources(struct us_data
*us
)
396 struct task_struct
*th
;
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");
405 /* Start up our control thread */
406 th
= kthread_run(usb_stor_control_thread
, us
, "eucr-storage");
408 pr_info("Unable to start control thread\n");
416 static void usb_stor_release_resources(struct us_data
*us
)
418 pr_info("usb --- usb_stor_release_resources\n");
422 complete(&us
->cmnd_ready
);
424 kthread_stop(us
->ctl_thread
);
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
);
432 /* Free the extra data and the URB */
434 usb_free_urb(us
->current_urb
);
437 static void dissociate_dev(struct us_data
*us
)
439 pr_info("usb --- dissociate_dev\n");
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
,
448 /* Remove our private data from the interface */
449 usb_set_intfdata(us
->pusb_intf
, NULL
);
452 static void quiesce_and_remove_host(struct us_data
*us
)
454 struct Scsi_Host
*host
= us_to_host(us
);
456 pr_info("usb --- quiesce_and_remove_host\n");
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
);
463 * Prevent SCSI-scanning (if it hasn't started yet)
464 * and wait for the SCSI-scanning thread to stop.
466 set_bit(US_FLIDX_DONT_SCAN
, &us
->dflags
);
467 wake_up(&us
->delay_wait
);
468 wait_for_completion(&us
->scanning_done
);
471 * Removing the host will perform an orderly shutdown: caches
472 * synchronized, disks spun down, etc.
474 scsi_remove_host(host
);
477 * Prevent any new commands from being accepted and cut short
481 set_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
);
483 wake_up(&us
->delay_wait
);
486 static void release_everything(struct us_data
*us
)
488 pr_info("usb --- release_everything\n");
490 usb_stor_release_resources(us
);
492 scsi_host_put(us_to_host(us
));
495 static int usb_stor_scan_thread(void *__us
)
497 struct us_data
*us
= (struct us_data
*)__us
;
499 pr_info("usb --- usb_stor_scan_thread\n");
500 pr_info("EUCR : device found at %d\n", us
->pusb_dev
->devnum
);
503 /* Wait for the timeout to expire or for a disconnect */
505 wait_event_freezable_timeout(us
->delay_wait
,
506 test_bit(US_FLIDX_DONT_SCAN
, &us
->dflags
),
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
);
519 scsi_scan_host(us_to_host(us
));
520 pr_info("EUCR : device scan complete\n");
522 complete_and_exit(&us
->scanning_done
, 0);
525 static int eucr_probe(struct usb_interface
*intf
,
526 const struct usb_device_id
*id
)
528 struct Scsi_Host
*host
;
532 struct task_struct
*th
;
534 pr_info("usb --- eucr_probe\n");
536 host
= scsi_host_alloc(&usb_stor_host_template
, sizeof(*us
));
538 pr_info("Unable to allocate the scsi host\n");
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
);
552 /* Associate the us_data structure with the USB device */
553 result
= associate_dev(us
, intf
);
557 /* Get Device info */
558 result
= get_device_info(us
, id
);
562 /* Get the transport, protocol, and pipe settings */
563 result
= get_transport(us
);
566 result
= get_protocol(us
);
569 result
= get_pipes(us
);
573 /* Acquire all the other resources and add the host */
574 result
= usb_stor_acquire_resources(us
);
578 result
= scsi_add_host(host
, &intf
->dev
);
580 pr_info("Unable to add the scsi host\n");
584 /* Start up the thread for delayed SCSI-device scanning */
585 th
= kthread_create(usb_stor_scan_thread
, us
, "eucr-stor-scan");
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
);
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
);
603 if (!(MiscReg03
& 0x02)) {
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");
612 /* We come here if there are any problems */
614 pr_info("usb --- eucr_probe failed\n");
615 release_everything(us
);
619 static void eucr_disconnect(struct usb_interface
*intf
)
621 struct us_data
*us
= usb_get_intfdata(intf
);
623 pr_info("usb --- eucr_disconnect\n");
624 quiesce_and_remove_host(us
);
625 release_everything(us
);
628 /* Initialization and registration */
629 static struct usb_driver usb_storage_driver
= {
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
,
642 module_usb_driver(usb_storage_driver
);