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>
18 #include "transport.h"
20 /* Some informational data */
21 MODULE_AUTHOR("Domao");
22 MODULE_DESCRIPTION("ENE USB Mass Storage driver for Linux");
23 MODULE_LICENSE("GPL");
25 static unsigned int delay_use
= 1;
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 */
33 MODULE_DEVICE_TABLE (usb
, eucr_usb_ids
);
38 static int eucr_suspend(struct usb_interface
*iface
, pm_message_t message
)
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
);
45 if (us
->suspend_resume_hook
)
46 (us
->suspend_resume_hook
)(us
, US_SUSPEND
);
48 mutex_unlock(&us
->dev_mutex
);
52 static int eucr_resume(struct usb_interface
*iface
)
56 struct us_data
*us
= usb_get_intfdata(iface
);
57 pr_info("--- eucr_resume---\n");
58 mutex_lock(&us
->dev_mutex
);
60 if (us
->suspend_resume_hook
)
61 (us
->suspend_resume_hook
)(us
, US_RESUME
);
64 mutex_unlock(&us
->dev_mutex
);
66 us
->Power_IsResum
= true;
68 us
->SM_Status
= *(PSM_STATUS
)&tmp
;
73 static int eucr_reset_resume(struct usb_interface
*iface
)
76 struct us_data
*us
= usb_get_intfdata(iface
);
78 pr_info("--- eucr_reset_resume---\n");
80 /* Report the reset to the SCSI core */
81 usb_stor_report_bus_reset(us
);
84 * FIXME: Notify the subdrivers that they need to reinitialize
88 us
->Power_IsResum
= true;
90 us
->SM_Status
= *(PSM_STATUS
)&tmp
;
97 #define eucr_suspend NULL
98 #define eucr_resume NULL
99 #define eucr_reset_resume NULL
103 static int eucr_pre_reset(struct usb_interface
*iface
)
105 struct us_data
*us
= usb_get_intfdata(iface
);
107 pr_info("usb --- eucr_pre_reset\n");
109 /* Make sure no command runs during the reset */
110 mutex_lock(&us
->dev_mutex
);
114 static int eucr_post_reset(struct usb_interface
*iface
)
116 struct us_data
*us
= usb_get_intfdata(iface
);
118 pr_info("usb --- eucr_post_reset\n");
120 /* Report the reset to the SCSI core */
121 usb_stor_report_bus_reset(us
);
123 mutex_unlock(&us
->dev_mutex
);
127 void fill_inquiry_response(struct us_data
*us
, unsigned char *data
, unsigned int data_len
)
129 pr_info("usb --- fill_inquiry_response\n");
130 if (data_len
< 36) /* You lose. */
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);
148 usb_stor_set_xfer_buf(us
, data
, data_len
, us
->srb
, TO_XFER_BUF
);
151 static int usb_stor_control_thread(void * __us
)
153 struct us_data
*us
= (struct us_data
*)__us
;
154 struct Scsi_Host
*host
= us_to_host(us
);
156 pr_info("usb --- usb_stor_control_thread\n");
158 if (wait_for_completion_interruptible(&us
->cmnd_ready
))
161 /* lock the device pointers */
162 mutex_lock(&(us
->dev_mutex
));
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
);
170 /* lock access to the state */
173 /* When we are called with no command pending, we're done */
174 if (us
->srb
== NULL
) {
176 mutex_unlock(&us
->dev_mutex
);
180 /* has the command timed out *already* ? */
181 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
)) {
182 us
->srb
->result
= DID_ABORT
<< 16;
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};
199 fill_inquiry_response(us
, data_ptr
, 36);
200 us
->srb
->result
= SAM_STAT_GOOD
;
202 us
->proto_handler(us
->srb
, us
);
205 /* lock access to the state */
208 /* indicate that the command is done */
209 if (us
->srb
->result
!= DID_ABORT
<< 16) {
210 us
->srb
->scsi_done(us
->srb
);
213 pr_info("scsi command aborted\n");
216 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
)) {
217 complete(&(us
->notify
));
219 /* Allow USB transfers to resume */
220 clear_bit(US_FLIDX_ABORTING
, &us
->dflags
);
221 clear_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
);
224 /* finished working on this command */
228 /* unlock the device pointers */
229 mutex_unlock(&us
->dev_mutex
);
232 /* Wait until we are told to stop */
234 set_current_state(TASK_INTERRUPTIBLE
);
235 if (kthread_should_stop())
239 __set_current_state(TASK_RUNNING
);
243 static int associate_dev(struct us_data
*us
, struct usb_interface
*intf
)
245 pr_info("usb --- associate_dev\n");
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
;
252 /* Store our private data in the interface */
253 usb_set_intfdata(intf
, us
);
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
);
258 pr_info("usb_ctrlrequest allocation failed\n");
262 us
->iobuf
= usb_alloc_coherent(us
->pusb_dev
, US_IOBUF_SIZE
, GFP_KERNEL
, &us
->iobuf_dma
);
264 pr_info("I/O buffer allocation failed\n");
268 us
->sensebuf
= kmalloc(US_SENSE_SIZE
, GFP_KERNEL
);
275 static int get_device_info(struct us_data
*us
, const struct usb_device_id
*id
)
277 struct usb_device
*dev
= us
->pusb_dev
;
278 struct usb_interface_descriptor
*idesc
= &us
->pusb_intf
->cur_altsetting
->desc
;
280 pr_info("usb --- get_device_info\n");
282 us
->subclass
= idesc
->bInterfaceSubClass
;
283 us
->protocol
= idesc
->bInterfaceProtocol
;
284 us
->fflags
= id
->driver_info
;
285 us
->Power_IsResum
= false;
287 if (us
->fflags
& US_FL_IGNORE_DEVICE
) {
288 pr_info("device ignored\n");
292 if (dev
->speed
!= USB_SPEED_HIGH
)
293 us
->fflags
&= ~US_FL_GO_SLOW
;
298 static int get_transport(struct us_data
*us
)
300 pr_info("usb --- get_transport\n");
301 switch (us
->protocol
) {
303 us
->transport_name
= "Bulk";
304 us
->transport
= usb_stor_Bulk_transport
;
305 us
->transport_reset
= usb_stor_Bulk_reset
;
312 /* fix for single-lun devices */
313 if (us
->fflags
& US_FL_SINGLE_LUN
)
318 static int get_protocol(struct us_data
*us
)
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
) {
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
;
332 us
->proto_handler
= usb_stor_invoke_transport
;
341 static int get_pipes(struct us_data
*us
)
343 struct usb_host_interface
*altsetting
= us
->pusb_intf
->cur_altsetting
;
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
;
350 pr_info("usb --- get_pipes\n");
352 for (i
= 0; i
< altsetting
->desc
.bNumEndpoints
; i
++) {
353 ep
= &altsetting
->endpoint
[i
].desc
;
355 if (usb_endpoint_xfer_bulk(ep
)) {
356 if (usb_endpoint_dir_in(ep
)) {
363 } else if (usb_endpoint_is_int_in(ep
)) {
369 if (!ep_in
|| !ep_out
|| (us
->protocol
== USB_PR_CBI
&& !ep_int
)) {
370 pr_info("Endpoint sanity check failed! Rejecting dev.\n");
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
);
380 us
->recv_intr_pipe
= usb_rcvintpipe(us
->pusb_dev
, ep_int
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
381 us
->ep_bInterval
= ep_int
->bInterval
;
386 static int usb_stor_acquire_resources(struct us_data
*us
)
388 struct task_struct
*th
;
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");
397 /* Start up our control thread */
398 th
= kthread_run(usb_stor_control_thread
, us
, "eucr-storage");
400 pr_info("Unable to start control thread\n");
408 static void usb_stor_release_resources(struct us_data
*us
)
410 pr_info("usb --- usb_stor_release_resources\n");
414 complete(&us
->cmnd_ready
);
416 kthread_stop(us
->ctl_thread
);
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
);
424 /* Free the extra data and the URB */
426 usb_free_urb(us
->current_urb
);
429 static void dissociate_dev(struct us_data
*us
)
431 pr_info("usb --- dissociate_dev\n");
435 /* Free the device-related DMA-mapped buffers */
437 usb_free_coherent(us
->pusb_dev
, sizeof(*us
->cr
), us
->cr
, us
->cr_dma
);
439 usb_free_coherent(us
->pusb_dev
, US_IOBUF_SIZE
, us
->iobuf
, us
->iobuf_dma
);
441 /* Remove our private data from the interface */
442 usb_set_intfdata(us
->pusb_intf
, NULL
);
445 static void quiesce_and_remove_host(struct us_data
*us
)
447 struct Scsi_Host
*host
= us_to_host(us
);
449 pr_info("usb --- quiesce_and_remove_host\n");
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
);
456 * Prevent SCSI-scanning (if it hasn't started yet)
457 * and wait for the SCSI-scanning thread to stop.
459 set_bit(US_FLIDX_DONT_SCAN
, &us
->dflags
);
460 wake_up(&us
->delay_wait
);
461 wait_for_completion(&us
->scanning_done
);
464 * Removing the host will perform an orderly shutdown: caches
465 * synchronized, disks spun down, etc.
467 scsi_remove_host(host
);
470 * Prevent any new commands from being accepted and cut short
474 set_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
);
476 wake_up(&us
->delay_wait
);
479 static void release_everything(struct us_data
*us
)
481 pr_info("usb --- release_everything\n");
483 usb_stor_release_resources(us
);
485 scsi_host_put(us_to_host(us
));
488 static int usb_stor_scan_thread(void * __us
)
490 struct us_data
*us
= (struct us_data
*)__us
;
492 pr_info("usb --- usb_stor_scan_thread\n");
493 pr_info("EUCR : device found at %d\n", us
->pusb_dev
->devnum
);
496 /* Wait for the timeout to expire or for a disconnect */
498 wait_event_freezable_timeout(us
->delay_wait
,
499 test_bit(US_FLIDX_DONT_SCAN
, &us
->dflags
),
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
);
512 scsi_scan_host(us_to_host(us
));
513 pr_info("EUCR : device scan complete\n");
515 complete_and_exit(&us
->scanning_done
, 0);
518 static int eucr_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
520 struct Scsi_Host
*host
;
524 struct task_struct
*th
;
526 pr_info("usb --- eucr_probe\n");
528 host
= scsi_host_alloc(&usb_stor_host_template
, sizeof(*us
));
530 pr_info("Unable to allocate the scsi host\n");
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
);
544 /* Associate the us_data structure with the USB device */
545 result
= associate_dev(us
, intf
);
549 /* Get Device info */
550 result
= get_device_info(us
, id
);
554 /* Get the transport, protocol, and pipe settings */
555 result
= get_transport(us
);
558 result
= get_protocol(us
);
561 result
= get_pipes(us
);
565 /* Acquire all the other resources and add the host */
566 result
= usb_stor_acquire_resources(us
);
570 result
= scsi_add_host(host
, &intf
->dev
);
572 pr_info("Unable to add the scsi host\n");
576 /* Start up the thread for delayed SCSI-device scanning */
577 th
= kthread_create(usb_stor_scan_thread
, us
, "eucr-stor-scan");
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
);
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
);
595 if (!(MiscReg03
& 0x02)) {
597 quiesce_and_remove_host(us
);
598 pr_info("keucr: The driver only supports SM/MS card.\
600 please build driver/usb/storage/ums-eneub6250.ko\n");
606 /* We come here if there are any problems */
608 pr_info("usb --- eucr_probe failed\n");
609 release_everything(us
);
613 static void eucr_disconnect(struct usb_interface
*intf
)
615 struct us_data
*us
= usb_get_intfdata(intf
);
617 pr_info("usb --- eucr_disconnect\n");
618 quiesce_and_remove_host(us
);
619 release_everything(us
);
622 /* Initialization and registration */
623 static struct usb_driver usb_storage_driver
= {
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
,
636 module_usb_driver(usb_storage_driver
);