blob: 8c488f1ad81bdefe8d4359db7e26820abc7301fd [file] [log] [blame]
Al Cho126bb032010-09-08 00:42:32 -07001#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"
Randy Dunlapd8aba9d2011-05-09 10:46:24 -070017#include "smil.h"
Al Cho126bb032010-09-08 00:42:32 -070018#include "transport.h"
19
20/* Some informational data */
21MODULE_AUTHOR("Domao");
22MODULE_DESCRIPTION("ENE USB Mass Storage driver for Linux");
23MODULE_LICENSE("GPL");
24
Al Choa8718692010-09-16 11:51:48 +080025static unsigned int delay_use = 1;
26
Al Cho126bb032010-09-08 00:42:32 -070027static 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 */
32};
33MODULE_DEVICE_TABLE (usb, eucr_usb_ids);
34
35
Randy Dunlape23f7732010-09-14 15:41:41 -070036#ifdef CONFIG_PM
Al Cho126bb032010-09-08 00:42:32 -070037
Randy Dunlapd8aba9d2011-05-09 10:46:24 -070038static int eucr_suspend(struct usb_interface *iface, pm_message_t message)
Al Cho126bb032010-09-08 00:42:32 -070039{
40 struct us_data *us = usb_get_intfdata(iface);
Cho, Yu-Chen871f8472011-04-01 14:38:43 +080041 pr_info("--- eucr_suspend ---\n");
Al Cho126bb032010-09-08 00:42:32 -070042 /* Wait until no command is running */
43 mutex_lock(&us->dev_mutex);
44
Al Cho126bb032010-09-08 00:42:32 -070045 if (us->suspend_resume_hook)
46 (us->suspend_resume_hook)(us, US_SUSPEND);
47
Al Cho126bb032010-09-08 00:42:32 -070048 mutex_unlock(&us->dev_mutex);
49 return 0;
50}
Al Cho126bb032010-09-08 00:42:32 -070051
Randy Dunlapd8aba9d2011-05-09 10:46:24 -070052static int eucr_resume(struct usb_interface *iface)
Al Cho126bb032010-09-08 00:42:32 -070053{
54 BYTE tmp = 0;
55
56 struct us_data *us = usb_get_intfdata(iface);
Cho, Yu-Chen871f8472011-04-01 14:38:43 +080057 pr_info("--- eucr_resume---\n");
Al Cho126bb032010-09-08 00:42:32 -070058 mutex_lock(&us->dev_mutex);
59
Al Cho126bb032010-09-08 00:42:32 -070060 if (us->suspend_resume_hook)
61 (us->suspend_resume_hook)(us, US_RESUME);
62
63
64 mutex_unlock(&us->dev_mutex);
65
Kurt Kanzenbach531c3362012-11-30 12:18:17 +010066 us->Power_IsResum = true;
67
68 us->SM_Status = *(PSM_STATUS)&tmp;
Kurt Kanzenbach2c392da2012-11-30 12:18:18 +010069
Al Cho126bb032010-09-08 00:42:32 -070070 return 0;
71}
Kurt Kanzenbach531c3362012-11-30 12:18:17 +010072
Randy Dunlapd8aba9d2011-05-09 10:46:24 -070073static int eucr_reset_resume(struct usb_interface *iface)
Al Cho126bb032010-09-08 00:42:32 -070074{
75 BYTE tmp = 0;
76 struct us_data *us = usb_get_intfdata(iface);
77
Cho, Yu-Chen871f8472011-04-01 14:38:43 +080078 pr_info("--- eucr_reset_resume---\n");
Al Cho126bb032010-09-08 00:42:32 -070079
80 /* Report the reset to the SCSI core */
81 usb_stor_report_bus_reset(us);
82
Kurt Kanzenbach531c3362012-11-30 12:18:17 +010083 /*
84 * FIXME: Notify the subdrivers that they need to reinitialize
85 * the device
86 */
87
Al Cho126bb032010-09-08 00:42:32 -070088 us->Power_IsResum = true;
Kurt Kanzenbach531c3362012-11-30 12:18:17 +010089
90 us->SM_Status = *(PSM_STATUS)&tmp;
91
Al Cho126bb032010-09-08 00:42:32 -070092 return 0;
93}
Al Cho126bb032010-09-08 00:42:32 -070094
Randy Dunlape23f7732010-09-14 15:41:41 -070095#else
96
97#define eucr_suspend NULL
98#define eucr_resume NULL
99#define eucr_reset_resume NULL
100
101#endif
102
Al Cho126bb032010-09-08 00:42:32 -0700103static int eucr_pre_reset(struct usb_interface *iface)
104{
105 struct us_data *us = usb_get_intfdata(iface);
106
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800107 pr_info("usb --- eucr_pre_reset\n");
Al Cho126bb032010-09-08 00:42:32 -0700108
109 /* Make sure no command runs during the reset */
110 mutex_lock(&us->dev_mutex);
111 return 0;
112}
113
Al Cho126bb032010-09-08 00:42:32 -0700114static int eucr_post_reset(struct usb_interface *iface)
115{
116 struct us_data *us = usb_get_intfdata(iface);
117
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800118 pr_info("usb --- eucr_post_reset\n");
Al Cho126bb032010-09-08 00:42:32 -0700119
120 /* Report the reset to the SCSI core */
121 usb_stor_report_bus_reset(us);
122
123 mutex_unlock(&us->dev_mutex);
124 return 0;
125}
126
Al Cho126bb032010-09-08 00:42:32 -0700127void fill_inquiry_response(struct us_data *us, unsigned char *data, unsigned int data_len)
128{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800129 pr_info("usb --- fill_inquiry_response\n");
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100130 if (data_len < 36) /* You lose. */
Al Cho126bb032010-09-08 00:42:32 -0700131 return;
132
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100133 if (data[0]&0x20) {
Amarjargal Gundjalam99b49742013-05-17 01:04:02 -0700134 memset(data+8, 0, 28);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100135 } else {
Al Cho126bb032010-09-08 00:42:32 -0700136 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);
147 }
148 usb_stor_set_xfer_buf(us, data, data_len, us->srb, TO_XFER_BUF);
149}
150
Al Cho126bb032010-09-08 00:42:32 -0700151static int usb_stor_control_thread(void * __us)
152{
153 struct us_data *us = (struct us_data *)__us;
154 struct Scsi_Host *host = us_to_host(us);
155
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800156 pr_info("usb --- usb_stor_control_thread\n");
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100157 for (;;) {
Al Cho126bb032010-09-08 00:42:32 -0700158 if (wait_for_completion_interruptible(&us->cmnd_ready))
159 break;
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100160
Al Cho126bb032010-09-08 00:42:32 -0700161 /* lock the device pointers */
162 mutex_lock(&(us->dev_mutex));
163
164 /* if the device has disconnected, we are free to exit */
Al Choa8718692010-09-16 11:51:48 +0800165 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
Al Cho126bb032010-09-08 00:42:32 -0700166 mutex_unlock(&us->dev_mutex);
167 break;
Al Choa8718692010-09-16 11:51:48 +0800168 }
Al Cho126bb032010-09-08 00:42:32 -0700169
170 /* lock access to the state */
171 scsi_lock(host);
172
173 /* When we are called with no command pending, we're done */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100174 if (us->srb == NULL) {
Al Cho126bb032010-09-08 00:42:32 -0700175 scsi_unlock(host);
176 mutex_unlock(&us->dev_mutex);
Al Cho126bb032010-09-08 00:42:32 -0700177 break;
178 }
179
180 /* has the command timed out *already* ? */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100181 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Al Cho126bb032010-09-08 00:42:32 -0700182 us->srb->result = DID_ABORT << 16;
183 goto SkipForAbort;
184 }
185
186 scsi_unlock(host);
187
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100188 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
Al Cho126bb032010-09-08 00:42:32 -0700189 us->srb->result = DID_ERROR << 16;
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100190 } else if (us->srb->device->id
191 && !(us->fflags & US_FL_SCM_MULT_TARG)) {
Al Cho126bb032010-09-08 00:42:32 -0700192 us->srb->result = DID_BAD_TARGET << 16;
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100193 } else if (us->srb->device->lun > us->max_lun) {
Al Cho126bb032010-09-08 00:42:32 -0700194 us->srb->result = DID_BAD_TARGET << 16;
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100195 } else if ((us->srb->cmnd[0] == INQUIRY)
196 && (us->fflags & US_FL_FIX_INQUIRY)) {
Al Cho126bb032010-09-08 00:42:32 -0700197 unsigned char data_ptr[36] = {0x00, 0x80, 0x02, 0x02, 0x1F, 0x00, 0x00, 0x00};
198
199 fill_inquiry_response(us, data_ptr, 36);
200 us->srb->result = SAM_STAT_GOOD;
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100201 } else {
Al Cho126bb032010-09-08 00:42:32 -0700202 us->proto_handler(us->srb, us);
203 }
204
205 /* lock access to the state */
206 scsi_lock(host);
207
208 /* indicate that the command is done */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100209 if (us->srb->result != DID_ABORT << 16) {
Al Cho126bb032010-09-08 00:42:32 -0700210 us->srb->scsi_done(us->srb);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100211 } else {
Al Cho126bb032010-09-08 00:42:32 -0700212SkipForAbort:
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800213 pr_info("scsi command aborted\n");
Al Cho126bb032010-09-08 00:42:32 -0700214 }
215
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100216 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Al Cho126bb032010-09-08 00:42:32 -0700217 complete(&(us->notify));
218
219 /* Allow USB transfers to resume */
220 clear_bit(US_FLIDX_ABORTING, &us->dflags);
221 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
222 }
223
224 /* finished working on this command */
225 us->srb = NULL;
226 scsi_unlock(host);
227
228 /* unlock the device pointers */
229 mutex_unlock(&us->dev_mutex);
230 } /* for (;;) */
231
232 /* Wait until we are told to stop */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100233 for (;;) {
Al Cho126bb032010-09-08 00:42:32 -0700234 set_current_state(TASK_INTERRUPTIBLE);
235 if (kthread_should_stop())
236 break;
237 schedule();
238 }
239 __set_current_state(TASK_RUNNING);
240 return 0;
Kurt Kanzenbach2c392da2012-11-30 12:18:18 +0100241}
Al Cho126bb032010-09-08 00:42:32 -0700242
Al Cho126bb032010-09-08 00:42:32 -0700243static int associate_dev(struct us_data *us, struct usb_interface *intf)
244{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800245 pr_info("usb --- associate_dev\n");
Al Cho126bb032010-09-08 00:42:32 -0700246
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;
251
252 /* Store our private data in the interface */
253 usb_set_intfdata(intf, us);
254
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);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100257 if (!us->cr) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800258 pr_info("usb_ctrlrequest allocation failed\n");
Al Cho126bb032010-09-08 00:42:32 -0700259 return -ENOMEM;
260 }
261
262 us->iobuf = usb_alloc_coherent(us->pusb_dev, US_IOBUF_SIZE, GFP_KERNEL, &us->iobuf_dma);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100263 if (!us->iobuf) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800264 pr_info("I/O buffer allocation failed\n");
Al Cho126bb032010-09-08 00:42:32 -0700265 return -ENOMEM;
266 }
267
268 us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800269 if (!us->sensebuf)
Al Cho126bb032010-09-08 00:42:32 -0700270 return -ENOMEM;
Joe Perches78110bb2013-02-11 09:41:29 -0800271
Al Cho126bb032010-09-08 00:42:32 -0700272 return 0;
273}
274
Al Cho126bb032010-09-08 00:42:32 -0700275static int get_device_info(struct us_data *us, const struct usb_device_id *id)
276{
277 struct usb_device *dev = us->pusb_dev;
278 struct usb_interface_descriptor *idesc = &us->pusb_intf->cur_altsetting->desc;
279
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800280 pr_info("usb --- get_device_info\n");
Al Cho126bb032010-09-08 00:42:32 -0700281
282 us->subclass = idesc->bInterfaceSubClass;
283 us->protocol = idesc->bInterfaceProtocol;
Sebastian Andrzej Siewiorf61870e2012-08-28 22:37:13 +0200284 us->fflags = id->driver_info;
Al Cho126bb032010-09-08 00:42:32 -0700285 us->Power_IsResum = false;
286
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100287 if (us->fflags & US_FL_IGNORE_DEVICE) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800288 pr_info("device ignored\n");
Al Cho126bb032010-09-08 00:42:32 -0700289 return -ENODEV;
290 }
291
292 if (dev->speed != USB_SPEED_HIGH)
293 us->fflags &= ~US_FL_GO_SLOW;
294
295 return 0;
296}
297
Al Cho126bb032010-09-08 00:42:32 -0700298static int get_transport(struct us_data *us)
299{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800300 pr_info("usb --- get_transport\n");
Al Cho126bb032010-09-08 00:42:32 -0700301 switch (us->protocol) {
Greg Kroah-Hartmanbceaddd2010-10-28 09:53:38 -0700302 case USB_PR_BULK:
Al Cho126bb032010-09-08 00:42:32 -0700303 us->transport_name = "Bulk";
304 us->transport = usb_stor_Bulk_transport;
305 us->transport_reset = usb_stor_Bulk_reset;
306 break;
307
308 default:
309 return -EIO;
310 }
Al Cho126bb032010-09-08 00:42:32 -0700311
312 /* fix for single-lun devices */
313 if (us->fflags & US_FL_SINGLE_LUN)
314 us->max_lun = 0;
315 return 0;
316}
317
Al Cho126bb032010-09-08 00:42:32 -0700318static int get_protocol(struct us_data *us)
319{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800320 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);
Al Cho126bb032010-09-08 00:42:32 -0700325 switch (us->subclass) {
Greg Kroah-Hartmanbceaddd2010-10-28 09:53:38 -0700326 case USB_SC_SCSI:
Al Cho126bb032010-09-08 00:42:32 -0700327 us->protocol_name = "Transparent SCSI";
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100328 if ((us->pusb_dev->descriptor.idVendor == 0x0CF2)
329 && (us->pusb_dev->descriptor.idProduct == 0x6250))
Al Cho126bb032010-09-08 00:42:32 -0700330 us->proto_handler = ENE_stor_invoke_transport;
331 else
332 us->proto_handler = usb_stor_invoke_transport;
333 break;
334
335 default:
336 return -EIO;
337 }
Al Cho126bb032010-09-08 00:42:32 -0700338 return 0;
339}
340
Al Cho126bb032010-09-08 00:42:32 -0700341static int get_pipes(struct us_data *us)
342{
343 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
344 int i;
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;
349
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800350 pr_info("usb --- get_pipes\n");
Al Cho126bb032010-09-08 00:42:32 -0700351
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100352 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
Al Cho126bb032010-09-08 00:42:32 -0700353 ep = &altsetting->endpoint[i].desc;
354
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100355 if (usb_endpoint_xfer_bulk(ep)) {
356 if (usb_endpoint_dir_in(ep)) {
Al Cho126bb032010-09-08 00:42:32 -0700357 if (!ep_in)
358 ep_in = ep;
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100359 } else {
Al Cho126bb032010-09-08 00:42:32 -0700360 if (!ep_out)
361 ep_out = ep;
362 }
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100363 } else if (usb_endpoint_is_int_in(ep)) {
Al Cho126bb032010-09-08 00:42:32 -0700364 if (!ep_int)
365 ep_int = ep;
366 }
367 }
368
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100369 if (!ep_in || !ep_out || (us->protocol == USB_PR_CBI && !ep_int)) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800370 pr_info("Endpoint sanity check failed! Rejecting dev.\n");
Al Cho126bb032010-09-08 00:42:32 -0700371 return -EIO;
372 }
373
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);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100379 if (ep_int) {
Al Cho126bb032010-09-08 00:42:32 -0700380 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev, ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
381 us->ep_bInterval = ep_int->bInterval;
382 }
383 return 0;
384}
385
Al Cho126bb032010-09-08 00:42:32 -0700386static int usb_stor_acquire_resources(struct us_data *us)
387{
388 struct task_struct *th;
389
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800390 pr_info("usb --- usb_stor_acquire_resources\n");
Al Cho126bb032010-09-08 00:42:32 -0700391 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100392 if (!us->current_urb) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800393 pr_info("URB allocation failed\n");
Al Cho126bb032010-09-08 00:42:32 -0700394 return -ENOMEM;
395 }
396
397 /* Start up our control thread */
398 th = kthread_run(usb_stor_control_thread, us, "eucr-storage");
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100399 if (IS_ERR(th)) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800400 pr_info("Unable to start control thread\n");
Al Cho126bb032010-09-08 00:42:32 -0700401 return PTR_ERR(th);
402 }
403 us->ctl_thread = th;
404
405 return 0;
406}
407
Al Cho126bb032010-09-08 00:42:32 -0700408static void usb_stor_release_resources(struct us_data *us)
409{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800410 pr_info("usb --- usb_stor_release_resources\n");
Al Cho126bb032010-09-08 00:42:32 -0700411
412 SM_FreeMem();
413
414 complete(&us->cmnd_ready);
415 if (us->ctl_thread)
416 kthread_stop(us->ctl_thread);
417
418 /* Call the destructor routine, if it exists */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100419 if (us->extra_destructor) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800420 pr_info("-- calling extra_destructor()\n");
Al Cho126bb032010-09-08 00:42:32 -0700421 us->extra_destructor(us->extra);
422 }
423
424 /* Free the extra data and the URB */
425 kfree(us->extra);
426 usb_free_urb(us->current_urb);
427}
428
Al Cho126bb032010-09-08 00:42:32 -0700429static void dissociate_dev(struct us_data *us)
430{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800431 pr_info("usb --- dissociate_dev\n");
Al Cho126bb032010-09-08 00:42:32 -0700432
433 kfree(us->sensebuf);
434
435 /* Free the device-related DMA-mapped buffers */
436 if (us->cr)
437 usb_free_coherent(us->pusb_dev, sizeof(*us->cr), us->cr, us->cr_dma);
438 if (us->iobuf)
439 usb_free_coherent(us->pusb_dev, US_IOBUF_SIZE, us->iobuf, us->iobuf_dma);
440
441 /* Remove our private data from the interface */
442 usb_set_intfdata(us->pusb_intf, NULL);
443}
444
Al Cho126bb032010-09-08 00:42:32 -0700445static void quiesce_and_remove_host(struct us_data *us)
446{
447 struct Scsi_Host *host = us_to_host(us);
448
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800449 pr_info("usb --- quiesce_and_remove_host\n");
Al Cho126bb032010-09-08 00:42:32 -0700450
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);
454
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100455 /*
456 * Prevent SCSI-scanning (if it hasn't started yet)
Al Cho126bb032010-09-08 00:42:32 -0700457 * and wait for the SCSI-scanning thread to stop.
458 */
459 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
460 wake_up(&us->delay_wait);
461 wait_for_completion(&us->scanning_done);
462
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100463 /*
464 * Removing the host will perform an orderly shutdown: caches
Al Cho126bb032010-09-08 00:42:32 -0700465 * synchronized, disks spun down, etc.
466 */
467 scsi_remove_host(host);
468
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100469 /*
470 * Prevent any new commands from being accepted and cut short
Al Cho126bb032010-09-08 00:42:32 -0700471 * reset delays.
472 */
473 scsi_lock(host);
474 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
475 scsi_unlock(host);
476 wake_up(&us->delay_wait);
477}
478
Al Cho126bb032010-09-08 00:42:32 -0700479static void release_everything(struct us_data *us)
480{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800481 pr_info("usb --- release_everything\n");
Al Cho126bb032010-09-08 00:42:32 -0700482
483 usb_stor_release_resources(us);
484 dissociate_dev(us);
485 scsi_host_put(us_to_host(us));
486}
487
Al Cho126bb032010-09-08 00:42:32 -0700488static int usb_stor_scan_thread(void * __us)
489{
490 struct us_data *us = (struct us_data *)__us;
491
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800492 pr_info("usb --- usb_stor_scan_thread\n");
493 pr_info("EUCR : device found at %d\n", us->pusb_dev->devnum);
Al Cho126bb032010-09-08 00:42:32 -0700494
Al Choa8718692010-09-16 11:51:48 +0800495 set_freezable();
496 /* Wait for the timeout to expire or for a disconnect */
497 if (delay_use > 0) {
498 wait_event_freezable_timeout(us->delay_wait,
499 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
500 delay_use * HZ);
501 }
Al Cho126bb032010-09-08 00:42:32 -0700502
503 /* If the device is still connected, perform the scanning */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100504 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
Al Cho126bb032010-09-08 00:42:32 -0700505 /* For bulk-only devices, determine the max LUN value */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100506 if (us->protocol == USB_PR_BULK
507 && !(us->fflags & US_FL_SINGLE_LUN)) {
Al Cho126bb032010-09-08 00:42:32 -0700508 mutex_lock(&us->dev_mutex);
509 us->max_lun = usb_stor_Bulk_max_lun(us);
510 mutex_unlock(&us->dev_mutex);
511 }
512 scsi_scan_host(us_to_host(us));
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800513 pr_info("EUCR : device scan complete\n");
Al Cho126bb032010-09-08 00:42:32 -0700514 }
515 complete_and_exit(&us->scanning_done, 0);
516}
517
Al Cho126bb032010-09-08 00:42:32 -0700518static int eucr_probe(struct usb_interface *intf, const struct usb_device_id *id)
519{
520 struct Scsi_Host *host;
521 struct us_data *us;
522 int result;
huajun li41e568d2011-03-04 10:56:18 +0800523 BYTE MiscReg03 = 0;
Al Cho126bb032010-09-08 00:42:32 -0700524 struct task_struct *th;
525
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800526 pr_info("usb --- eucr_probe\n");
Al Cho126bb032010-09-08 00:42:32 -0700527
528 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100529 if (!host) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800530 pr_info("Unable to allocate the scsi host\n");
Al Cho126bb032010-09-08 00:42:32 -0700531 return -ENOMEM;
532 }
533
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);
543
544 /* Associate the us_data structure with the USB device */
545 result = associate_dev(us, intf);
546 if (result)
547 goto BadDevice;
548
549 /* Get Device info */
550 result = get_device_info(us, id);
551 if (result)
552 goto BadDevice;
553
554 /* Get the transport, protocol, and pipe settings */
555 result = get_transport(us);
556 if (result)
557 goto BadDevice;
558 result = get_protocol(us);
559 if (result)
560 goto BadDevice;
561 result = get_pipes(us);
562 if (result)
563 goto BadDevice;
564
565 /* Acquire all the other resources and add the host */
566 result = usb_stor_acquire_resources(us);
567 if (result)
568 goto BadDevice;
569
570 result = scsi_add_host(host, &intf->dev);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100571 if (result) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800572 pr_info("Unable to add the scsi host\n");
Al Cho126bb032010-09-08 00:42:32 -0700573 goto BadDevice;
574 }
575
576 /* Start up the thread for delayed SCSI-device scanning */
577 th = kthread_create(usb_stor_scan_thread, us, "eucr-stor-scan");
Zac Storer7d51b772011-06-19 03:23:20 -0600578 if (IS_ERR(th)) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800579 pr_info("Unable to start the device-scanning thread\n");
Al Cho126bb032010-09-08 00:42:32 -0700580 complete(&us->scanning_done);
581 quiesce_and_remove_host(us);
582 result = PTR_ERR(th);
583 goto BadDevice;
584 }
585 wake_up_process(th);
huajun li41e568d2011-03-04 10:56:18 +0800586
587 /* probe card type */
Katrina Prosise9799d122013-05-12 17:17:41 -0700588 result = ene_read_byte(us, REG_CARD_STATUS, &MiscReg03);
huajun li41e568d2011-03-04 10:56:18 +0800589 if (result != USB_STOR_XFER_GOOD) {
590 result = USB_STOR_TRANSPORT_ERROR;
591 quiesce_and_remove_host(us);
592 goto BadDevice;
593 }
594
595 if (!(MiscReg03 & 0x02)) {
596 result = -ENODEV;
597 quiesce_and_remove_host(us);
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800598 pr_info("keucr: The driver only supports SM/MS card.\
huajun li41e568d2011-03-04 10:56:18 +0800599 To use SD card, \
600 please build driver/usb/storage/ums-eneub6250.ko\n");
601 goto BadDevice;
602 }
603
Al Cho126bb032010-09-08 00:42:32 -0700604 return 0;
605
606 /* We come here if there are any problems */
607BadDevice:
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800608 pr_info("usb --- eucr_probe failed\n");
Al Cho126bb032010-09-08 00:42:32 -0700609 release_everything(us);
610 return result;
611}
612
Al Cho126bb032010-09-08 00:42:32 -0700613static void eucr_disconnect(struct usb_interface *intf)
614{
615 struct us_data *us = usb_get_intfdata(intf);
616
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800617 pr_info("usb --- eucr_disconnect\n");
Al Cho126bb032010-09-08 00:42:32 -0700618 quiesce_and_remove_host(us);
619 release_everything(us);
620}
621
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100622/* Initialization and registration */
Al Cho126bb032010-09-08 00:42:32 -0700623static struct usb_driver usb_storage_driver = {
624 .name = "eucr",
625 .probe = eucr_probe,
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,
633 .soft_unbind = 1,
634};
635
Greg Kroah-Hartmanbac2c122011-11-18 09:42:11 -0800636module_usb_driver(usb_storage_driver);