blob: 3e3ca6365fbc27081b3e53e9508e032b250e3594 [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>
Al Cho126bb032010-09-08 00:42:32 -07005#include <linux/slab.h>
6#include <linux/kthread.h>
7#include <linux/mutex.h>
8#include <linux/utsname.h>
9
10#include <scsi/scsi.h>
11#include <scsi/scsi_cmnd.h>
12#include <scsi/scsi_device.h>
13
14#include "usb.h"
15#include "scsiglue.h"
Randy Dunlapd8aba9d2011-05-09 10:46:24 -070016#include "smil.h"
Al Cho126bb032010-09-08 00:42:32 -070017#include "transport.h"
18
19/* Some informational data */
20MODULE_AUTHOR("Domao");
21MODULE_DESCRIPTION("ENE USB Mass Storage driver for Linux");
22MODULE_LICENSE("GPL");
23
Al Choa8718692010-09-16 11:51:48 +080024static unsigned int delay_use = 1;
25
Amarjargal Gundjalama3539592013-05-17 01:05:04 -070026static struct usb_device_id eucr_usb_ids[] = {
Al Cho126bb032010-09-08 00:42:32 -070027 { USB_DEVICE(0x058f, 0x6366) },
28 { USB_DEVICE(0x0cf2, 0x6230) },
29 { USB_DEVICE(0x0cf2, 0x6250) },
30 { } /* Terminating entry */
31};
Amarjargal Gundjalam96dfb4b2013-05-17 01:04:49 -070032MODULE_DEVICE_TABLE(usb, eucr_usb_ids);
Al Cho126bb032010-09-08 00:42:32 -070033
34
Randy Dunlape23f7732010-09-14 15:41:41 -070035#ifdef CONFIG_PM
Al Cho126bb032010-09-08 00:42:32 -070036
Randy Dunlapd8aba9d2011-05-09 10:46:24 -070037static int eucr_suspend(struct usb_interface *iface, pm_message_t message)
Al Cho126bb032010-09-08 00:42:32 -070038{
39 struct us_data *us = usb_get_intfdata(iface);
Cho, Yu-Chen871f8472011-04-01 14:38:43 +080040 pr_info("--- eucr_suspend ---\n");
Al Cho126bb032010-09-08 00:42:32 -070041 /* Wait until no command is running */
42 mutex_lock(&us->dev_mutex);
43
Al Cho126bb032010-09-08 00:42:32 -070044 if (us->suspend_resume_hook)
45 (us->suspend_resume_hook)(us, US_SUSPEND);
46
Al Cho126bb032010-09-08 00:42:32 -070047 mutex_unlock(&us->dev_mutex);
48 return 0;
49}
Al Cho126bb032010-09-08 00:42:32 -070050
Randy Dunlapd8aba9d2011-05-09 10:46:24 -070051static int eucr_resume(struct usb_interface *iface)
Al Cho126bb032010-09-08 00:42:32 -070052{
53 BYTE tmp = 0;
54
55 struct us_data *us = usb_get_intfdata(iface);
Cho, Yu-Chen871f8472011-04-01 14:38:43 +080056 pr_info("--- eucr_resume---\n");
Al Cho126bb032010-09-08 00:42:32 -070057 mutex_lock(&us->dev_mutex);
58
Al Cho126bb032010-09-08 00:42:32 -070059 if (us->suspend_resume_hook)
60 (us->suspend_resume_hook)(us, US_RESUME);
61
62
63 mutex_unlock(&us->dev_mutex);
64
Kurt Kanzenbach531c3362012-11-30 12:18:17 +010065 us->Power_IsResum = true;
66
Johannes Schilling8be88042013-06-06 18:10:45 +020067 us->SM_Status = *(struct keucr_sm_status *)&tmp;
Kurt Kanzenbach2c392da2012-11-30 12:18:18 +010068
Al Cho126bb032010-09-08 00:42:32 -070069 return 0;
70}
Kurt Kanzenbach531c3362012-11-30 12:18:17 +010071
Randy Dunlapd8aba9d2011-05-09 10:46:24 -070072static int eucr_reset_resume(struct usb_interface *iface)
Al Cho126bb032010-09-08 00:42:32 -070073{
74 BYTE tmp = 0;
75 struct us_data *us = usb_get_intfdata(iface);
76
Cho, Yu-Chen871f8472011-04-01 14:38:43 +080077 pr_info("--- eucr_reset_resume---\n");
Al Cho126bb032010-09-08 00:42:32 -070078
79 /* Report the reset to the SCSI core */
80 usb_stor_report_bus_reset(us);
81
Kurt Kanzenbach531c3362012-11-30 12:18:17 +010082 /*
83 * FIXME: Notify the subdrivers that they need to reinitialize
84 * the device
85 */
86
Amarjargal Gundjalam5a5097a2013-05-17 01:06:19 -070087 us->Power_IsResum = true;
Kurt Kanzenbach531c3362012-11-30 12:18:17 +010088
Johannes Schilling8be88042013-06-06 18:10:45 +020089 us->SM_Status = *(struct keucr_sm_status *)&tmp;
Kurt Kanzenbach531c3362012-11-30 12:18:17 +010090
Al Cho126bb032010-09-08 00:42:32 -070091 return 0;
92}
Al Cho126bb032010-09-08 00:42:32 -070093
Randy Dunlape23f7732010-09-14 15:41:41 -070094#else
95
96#define eucr_suspend NULL
97#define eucr_resume NULL
98#define eucr_reset_resume NULL
99
100#endif
101
Al Cho126bb032010-09-08 00:42:32 -0700102static int eucr_pre_reset(struct usb_interface *iface)
103{
104 struct us_data *us = usb_get_intfdata(iface);
105
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800106 pr_info("usb --- eucr_pre_reset\n");
Al Cho126bb032010-09-08 00:42:32 -0700107
108 /* Make sure no command runs during the reset */
109 mutex_lock(&us->dev_mutex);
110 return 0;
111}
112
Al Cho126bb032010-09-08 00:42:32 -0700113static int eucr_post_reset(struct usb_interface *iface)
114{
115 struct us_data *us = usb_get_intfdata(iface);
116
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800117 pr_info("usb --- eucr_post_reset\n");
Al Cho126bb032010-09-08 00:42:32 -0700118
119 /* Report the reset to the SCSI core */
120 usb_stor_report_bus_reset(us);
121
122 mutex_unlock(&us->dev_mutex);
123 return 0;
124}
125
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700126void fill_inquiry_response(struct us_data *us, unsigned char *data,
127 unsigned int data_len)
Al Cho126bb032010-09-08 00:42:32 -0700128{
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 {
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700136 u16 bcdDevice =
137 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
Al Cho126bb032010-09-08 00:42:32 -0700138 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);
148 }
149 usb_stor_set_xfer_buf(us, data, data_len, us->srb, TO_XFER_BUF);
150}
151
Johannes Schilling0ea8a162013-06-06 18:10:50 +0200152static int usb_stor_control_thread(void *__us)
Al Cho126bb032010-09-08 00:42:32 -0700153{
154 struct us_data *us = (struct us_data *)__us;
155 struct Scsi_Host *host = us_to_host(us);
156
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800157 pr_info("usb --- usb_stor_control_thread\n");
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100158 for (;;) {
Al Cho126bb032010-09-08 00:42:32 -0700159 if (wait_for_completion_interruptible(&us->cmnd_ready))
160 break;
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100161
Al Cho126bb032010-09-08 00:42:32 -0700162 /* lock the device pointers */
163 mutex_lock(&(us->dev_mutex));
164
165 /* if the device has disconnected, we are free to exit */
Al Choa8718692010-09-16 11:51:48 +0800166 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
Al Cho126bb032010-09-08 00:42:32 -0700167 mutex_unlock(&us->dev_mutex);
168 break;
Al Choa8718692010-09-16 11:51:48 +0800169 }
Al Cho126bb032010-09-08 00:42:32 -0700170
171 /* lock access to the state */
172 scsi_lock(host);
173
174 /* When we are called with no command pending, we're done */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100175 if (us->srb == NULL) {
Al Cho126bb032010-09-08 00:42:32 -0700176 scsi_unlock(host);
177 mutex_unlock(&us->dev_mutex);
Al Cho126bb032010-09-08 00:42:32 -0700178 break;
179 }
180
181 /* has the command timed out *already* ? */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100182 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Al Cho126bb032010-09-08 00:42:32 -0700183 us->srb->result = DID_ABORT << 16;
184 goto SkipForAbort;
185 }
186
187 scsi_unlock(host);
188
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100189 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
Al Cho126bb032010-09-08 00:42:32 -0700190 us->srb->result = DID_ERROR << 16;
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100191 } else if (us->srb->device->id
192 && !(us->fflags & US_FL_SCM_MULT_TARG)) {
Al Cho126bb032010-09-08 00:42:32 -0700193 us->srb->result = DID_BAD_TARGET << 16;
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100194 } else if (us->srb->device->lun > us->max_lun) {
Al Cho126bb032010-09-08 00:42:32 -0700195 us->srb->result = DID_BAD_TARGET << 16;
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100196 } else if ((us->srb->cmnd[0] == INQUIRY)
197 && (us->fflags & US_FL_FIX_INQUIRY)) {
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700198 unsigned char data_ptr[36] = {0x00, 0x80, 0x02, 0x02,
199 0x1F, 0x00, 0x00, 0x00};
Al Cho126bb032010-09-08 00:42:32 -0700200
201 fill_inquiry_response(us, data_ptr, 36);
202 us->srb->result = SAM_STAT_GOOD;
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100203 } else {
Al Cho126bb032010-09-08 00:42:32 -0700204 us->proto_handler(us->srb, us);
205 }
206
207 /* lock access to the state */
208 scsi_lock(host);
209
210 /* indicate that the command is done */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100211 if (us->srb->result != DID_ABORT << 16) {
Al Cho126bb032010-09-08 00:42:32 -0700212 us->srb->scsi_done(us->srb);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100213 } else {
Al Cho126bb032010-09-08 00:42:32 -0700214SkipForAbort:
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800215 pr_info("scsi command aborted\n");
Al Cho126bb032010-09-08 00:42:32 -0700216 }
217
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100218 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Al Cho126bb032010-09-08 00:42:32 -0700219 complete(&(us->notify));
220
221 /* Allow USB transfers to resume */
222 clear_bit(US_FLIDX_ABORTING, &us->dflags);
223 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
224 }
225
226 /* finished working on this command */
227 us->srb = NULL;
228 scsi_unlock(host);
229
230 /* unlock the device pointers */
231 mutex_unlock(&us->dev_mutex);
232 } /* for (;;) */
233
234 /* Wait until we are told to stop */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100235 for (;;) {
Al Cho126bb032010-09-08 00:42:32 -0700236 set_current_state(TASK_INTERRUPTIBLE);
237 if (kthread_should_stop())
238 break;
239 schedule();
240 }
241 __set_current_state(TASK_RUNNING);
242 return 0;
Kurt Kanzenbach2c392da2012-11-30 12:18:18 +0100243}
Al Cho126bb032010-09-08 00:42:32 -0700244
Al Cho126bb032010-09-08 00:42:32 -0700245static int associate_dev(struct us_data *us, struct usb_interface *intf)
246{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800247 pr_info("usb --- associate_dev\n");
Al Cho126bb032010-09-08 00:42:32 -0700248
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;
253
254 /* Store our private data in the interface */
255 usb_set_intfdata(intf, us);
256
257 /* Allocate the device-related DMA-mapped buffers */
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700258 us->cr = usb_alloc_coherent(us->pusb_dev, sizeof(*us->cr), GFP_KERNEL,
259 &us->cr_dma);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100260 if (!us->cr) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800261 pr_info("usb_ctrlrequest allocation failed\n");
Al Cho126bb032010-09-08 00:42:32 -0700262 return -ENOMEM;
263 }
264
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700265 us->iobuf = usb_alloc_coherent(us->pusb_dev, US_IOBUF_SIZE, GFP_KERNEL,
266 &us->iobuf_dma);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100267 if (!us->iobuf) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800268 pr_info("I/O buffer allocation failed\n");
Al Cho126bb032010-09-08 00:42:32 -0700269 return -ENOMEM;
270 }
271
272 us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800273 if (!us->sensebuf)
Al Cho126bb032010-09-08 00:42:32 -0700274 return -ENOMEM;
Joe Perches78110bb2013-02-11 09:41:29 -0800275
Al Cho126bb032010-09-08 00:42:32 -0700276 return 0;
277}
278
Al Cho126bb032010-09-08 00:42:32 -0700279static int get_device_info(struct us_data *us, const struct usb_device_id *id)
280{
281 struct usb_device *dev = us->pusb_dev;
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700282 struct usb_interface_descriptor *idesc =
283 &us->pusb_intf->cur_altsetting->desc;
Al Cho126bb032010-09-08 00:42:32 -0700284
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800285 pr_info("usb --- get_device_info\n");
Al Cho126bb032010-09-08 00:42:32 -0700286
287 us->subclass = idesc->bInterfaceSubClass;
288 us->protocol = idesc->bInterfaceProtocol;
Sebastian Andrzej Siewiorf61870e2012-08-28 22:37:13 +0200289 us->fflags = id->driver_info;
Al Cho126bb032010-09-08 00:42:32 -0700290 us->Power_IsResum = false;
291
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100292 if (us->fflags & US_FL_IGNORE_DEVICE) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800293 pr_info("device ignored\n");
Al Cho126bb032010-09-08 00:42:32 -0700294 return -ENODEV;
295 }
296
297 if (dev->speed != USB_SPEED_HIGH)
298 us->fflags &= ~US_FL_GO_SLOW;
299
300 return 0;
301}
302
Al Cho126bb032010-09-08 00:42:32 -0700303static int get_transport(struct us_data *us)
304{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800305 pr_info("usb --- get_transport\n");
Al Cho126bb032010-09-08 00:42:32 -0700306 switch (us->protocol) {
Greg Kroah-Hartmanbceaddd2010-10-28 09:53:38 -0700307 case USB_PR_BULK:
Al Cho126bb032010-09-08 00:42:32 -0700308 us->transport_name = "Bulk";
309 us->transport = usb_stor_Bulk_transport;
310 us->transport_reset = usb_stor_Bulk_reset;
311 break;
312
313 default:
314 return -EIO;
315 }
Al Cho126bb032010-09-08 00:42:32 -0700316
317 /* fix for single-lun devices */
318 if (us->fflags & US_FL_SINGLE_LUN)
319 us->max_lun = 0;
320 return 0;
321}
322
Al Cho126bb032010-09-08 00:42:32 -0700323static int get_protocol(struct us_data *us)
324{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800325 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);
Al Cho126bb032010-09-08 00:42:32 -0700330 switch (us->subclass) {
Greg Kroah-Hartmanbceaddd2010-10-28 09:53:38 -0700331 case USB_SC_SCSI:
Al Cho126bb032010-09-08 00:42:32 -0700332 us->protocol_name = "Transparent SCSI";
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100333 if ((us->pusb_dev->descriptor.idVendor == 0x0CF2)
334 && (us->pusb_dev->descriptor.idProduct == 0x6250))
Al Cho126bb032010-09-08 00:42:32 -0700335 us->proto_handler = ENE_stor_invoke_transport;
336 else
337 us->proto_handler = usb_stor_invoke_transport;
338 break;
339
340 default:
341 return -EIO;
342 }
Al Cho126bb032010-09-08 00:42:32 -0700343 return 0;
344}
345
Al Cho126bb032010-09-08 00:42:32 -0700346static int get_pipes(struct us_data *us)
347{
348 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
349 int i;
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;
354
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800355 pr_info("usb --- get_pipes\n");
Al Cho126bb032010-09-08 00:42:32 -0700356
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100357 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
Al Cho126bb032010-09-08 00:42:32 -0700358 ep = &altsetting->endpoint[i].desc;
359
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100360 if (usb_endpoint_xfer_bulk(ep)) {
361 if (usb_endpoint_dir_in(ep)) {
Al Cho126bb032010-09-08 00:42:32 -0700362 if (!ep_in)
363 ep_in = ep;
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100364 } else {
Al Cho126bb032010-09-08 00:42:32 -0700365 if (!ep_out)
366 ep_out = ep;
367 }
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100368 } else if (usb_endpoint_is_int_in(ep)) {
Al Cho126bb032010-09-08 00:42:32 -0700369 if (!ep_int)
370 ep_int = ep;
371 }
372 }
373
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100374 if (!ep_in || !ep_out || (us->protocol == USB_PR_CBI && !ep_int)) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800375 pr_info("Endpoint sanity check failed! Rejecting dev.\n");
Al Cho126bb032010-09-08 00:42:32 -0700376 return -EIO;
377 }
378
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);
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700382 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);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100386 if (ep_int) {
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700387 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
388 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Al Cho126bb032010-09-08 00:42:32 -0700389 us->ep_bInterval = ep_int->bInterval;
390 }
391 return 0;
392}
393
Al Cho126bb032010-09-08 00:42:32 -0700394static int usb_stor_acquire_resources(struct us_data *us)
395{
396 struct task_struct *th;
397
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800398 pr_info("usb --- usb_stor_acquire_resources\n");
Al Cho126bb032010-09-08 00:42:32 -0700399 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100400 if (!us->current_urb) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800401 pr_info("URB allocation failed\n");
Al Cho126bb032010-09-08 00:42:32 -0700402 return -ENOMEM;
403 }
404
405 /* Start up our control thread */
406 th = kthread_run(usb_stor_control_thread, us, "eucr-storage");
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100407 if (IS_ERR(th)) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800408 pr_info("Unable to start control thread\n");
Al Cho126bb032010-09-08 00:42:32 -0700409 return PTR_ERR(th);
410 }
411 us->ctl_thread = th;
412
413 return 0;
414}
415
Al Cho126bb032010-09-08 00:42:32 -0700416static void usb_stor_release_resources(struct us_data *us)
417{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800418 pr_info("usb --- usb_stor_release_resources\n");
Al Cho126bb032010-09-08 00:42:32 -0700419
420 SM_FreeMem();
421
422 complete(&us->cmnd_ready);
423 if (us->ctl_thread)
424 kthread_stop(us->ctl_thread);
425
426 /* Call the destructor routine, if it exists */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100427 if (us->extra_destructor) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800428 pr_info("-- calling extra_destructor()\n");
Al Cho126bb032010-09-08 00:42:32 -0700429 us->extra_destructor(us->extra);
430 }
431
432 /* Free the extra data and the URB */
433 kfree(us->extra);
434 usb_free_urb(us->current_urb);
435}
436
Al Cho126bb032010-09-08 00:42:32 -0700437static void dissociate_dev(struct us_data *us)
438{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800439 pr_info("usb --- dissociate_dev\n");
Al Cho126bb032010-09-08 00:42:32 -0700440
441 kfree(us->sensebuf);
442
443 /* Free the device-related DMA-mapped buffers */
Wei Yongjunaba12e32013-05-23 17:28:34 +0800444 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,
446 us->iobuf_dma);
Al Cho126bb032010-09-08 00:42:32 -0700447
448 /* Remove our private data from the interface */
449 usb_set_intfdata(us->pusb_intf, NULL);
450}
451
Al Cho126bb032010-09-08 00:42:32 -0700452static void quiesce_and_remove_host(struct us_data *us)
453{
454 struct Scsi_Host *host = us_to_host(us);
455
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800456 pr_info("usb --- quiesce_and_remove_host\n");
Al Cho126bb032010-09-08 00:42:32 -0700457
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);
461
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100462 /*
463 * Prevent SCSI-scanning (if it hasn't started yet)
Al Cho126bb032010-09-08 00:42:32 -0700464 * and wait for the SCSI-scanning thread to stop.
465 */
466 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
467 wake_up(&us->delay_wait);
468 wait_for_completion(&us->scanning_done);
469
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100470 /*
471 * Removing the host will perform an orderly shutdown: caches
Al Cho126bb032010-09-08 00:42:32 -0700472 * synchronized, disks spun down, etc.
473 */
474 scsi_remove_host(host);
475
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100476 /*
477 * Prevent any new commands from being accepted and cut short
Al Cho126bb032010-09-08 00:42:32 -0700478 * reset delays.
479 */
480 scsi_lock(host);
481 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
482 scsi_unlock(host);
483 wake_up(&us->delay_wait);
484}
485
Al Cho126bb032010-09-08 00:42:32 -0700486static void release_everything(struct us_data *us)
487{
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800488 pr_info("usb --- release_everything\n");
Al Cho126bb032010-09-08 00:42:32 -0700489
490 usb_stor_release_resources(us);
491 dissociate_dev(us);
492 scsi_host_put(us_to_host(us));
493}
494
Johannes Schilling0ea8a162013-06-06 18:10:50 +0200495static int usb_stor_scan_thread(void *__us)
Al Cho126bb032010-09-08 00:42:32 -0700496{
497 struct us_data *us = (struct us_data *)__us;
498
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800499 pr_info("usb --- usb_stor_scan_thread\n");
500 pr_info("EUCR : device found at %d\n", us->pusb_dev->devnum);
Al Cho126bb032010-09-08 00:42:32 -0700501
Al Choa8718692010-09-16 11:51:48 +0800502 set_freezable();
503 /* Wait for the timeout to expire or for a disconnect */
504 if (delay_use > 0) {
505 wait_event_freezable_timeout(us->delay_wait,
506 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
507 delay_use * HZ);
508 }
Al Cho126bb032010-09-08 00:42:32 -0700509
510 /* If the device is still connected, perform the scanning */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100511 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
Al Cho126bb032010-09-08 00:42:32 -0700512 /* For bulk-only devices, determine the max LUN value */
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100513 if (us->protocol == USB_PR_BULK
514 && !(us->fflags & US_FL_SINGLE_LUN)) {
Al Cho126bb032010-09-08 00:42:32 -0700515 mutex_lock(&us->dev_mutex);
516 us->max_lun = usb_stor_Bulk_max_lun(us);
517 mutex_unlock(&us->dev_mutex);
518 }
519 scsi_scan_host(us_to_host(us));
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800520 pr_info("EUCR : device scan complete\n");
Al Cho126bb032010-09-08 00:42:32 -0700521 }
522 complete_and_exit(&us->scanning_done, 0);
523}
524
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700525static int eucr_probe(struct usb_interface *intf,
526 const struct usb_device_id *id)
Al Cho126bb032010-09-08 00:42:32 -0700527{
528 struct Scsi_Host *host;
529 struct us_data *us;
530 int result;
huajun li41e568d2011-03-04 10:56:18 +0800531 BYTE MiscReg03 = 0;
Al Cho126bb032010-09-08 00:42:32 -0700532 struct task_struct *th;
533
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800534 pr_info("usb --- eucr_probe\n");
Al Cho126bb032010-09-08 00:42:32 -0700535
Amarjargal Gundjalam5a5097a2013-05-17 01:06:19 -0700536 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100537 if (!host) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800538 pr_info("Unable to allocate the scsi host\n");
Al Cho126bb032010-09-08 00:42:32 -0700539 return -ENOMEM;
540 }
541
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);
551
552 /* Associate the us_data structure with the USB device */
553 result = associate_dev(us, intf);
554 if (result)
555 goto BadDevice;
556
557 /* Get Device info */
558 result = get_device_info(us, id);
559 if (result)
560 goto BadDevice;
561
562 /* Get the transport, protocol, and pipe settings */
563 result = get_transport(us);
564 if (result)
565 goto BadDevice;
566 result = get_protocol(us);
567 if (result)
568 goto BadDevice;
569 result = get_pipes(us);
570 if (result)
571 goto BadDevice;
572
573 /* Acquire all the other resources and add the host */
574 result = usb_stor_acquire_resources(us);
575 if (result)
576 goto BadDevice;
577
578 result = scsi_add_host(host, &intf->dev);
Kurt Kanzenbachb466eb52012-11-30 12:18:19 +0100579 if (result) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800580 pr_info("Unable to add the scsi host\n");
Al Cho126bb032010-09-08 00:42:32 -0700581 goto BadDevice;
582 }
583
584 /* Start up the thread for delayed SCSI-device scanning */
585 th = kthread_create(usb_stor_scan_thread, us, "eucr-stor-scan");
Zac Storer7d51b772011-06-19 03:23:20 -0600586 if (IS_ERR(th)) {
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800587 pr_info("Unable to start the device-scanning thread\n");
Al Cho126bb032010-09-08 00:42:32 -0700588 complete(&us->scanning_done);
589 quiesce_and_remove_host(us);
590 result = PTR_ERR(th);
591 goto BadDevice;
592 }
593 wake_up_process(th);
huajun li41e568d2011-03-04 10:56:18 +0800594
595 /* probe card type */
Katrina Prosise9799d122013-05-12 17:17:41 -0700596 result = ene_read_byte(us, REG_CARD_STATUS, &MiscReg03);
huajun li41e568d2011-03-04 10:56:18 +0800597 if (result != USB_STOR_XFER_GOOD) {
598 result = USB_STOR_TRANSPORT_ERROR;
599 quiesce_and_remove_host(us);
600 goto BadDevice;
601 }
602
603 if (!(MiscReg03 & 0x02)) {
604 result = -ENODEV;
605 quiesce_and_remove_host(us);
Ebru Akagunduz49100712013-10-05 17:11:39 +0300606 pr_info("keucr: The driver only supports SM/MS card. To use SD card, please build driver/usb/storage/ums-eneub6250.ko\n");
huajun li41e568d2011-03-04 10:56:18 +0800607 goto BadDevice;
608 }
609
Al Cho126bb032010-09-08 00:42:32 -0700610 return 0;
611
612 /* We come here if there are any problems */
613BadDevice:
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800614 pr_info("usb --- eucr_probe failed\n");
Al Cho126bb032010-09-08 00:42:32 -0700615 release_everything(us);
616 return result;
617}
618
Al Cho126bb032010-09-08 00:42:32 -0700619static void eucr_disconnect(struct usb_interface *intf)
620{
621 struct us_data *us = usb_get_intfdata(intf);
622
Cho, Yu-Chen871f8472011-04-01 14:38:43 +0800623 pr_info("usb --- eucr_disconnect\n");
Al Cho126bb032010-09-08 00:42:32 -0700624 quiesce_and_remove_host(us);
625 release_everything(us);
626}
627
Kurt Kanzenbach531c3362012-11-30 12:18:17 +0100628/* Initialization and registration */
Al Cho126bb032010-09-08 00:42:32 -0700629static struct usb_driver usb_storage_driver = {
630 .name = "eucr",
631 .probe = eucr_probe,
Amarjargal Gundjalam5a5097a2013-05-17 01:06:19 -0700632 .suspend = eucr_suspend,
Al Cho126bb032010-09-08 00:42:32 -0700633 .resume = eucr_resume,
Amarjargal Gundjalam5a5097a2013-05-17 01:06:19 -0700634 .reset_resume = eucr_reset_resume,
Al Cho126bb032010-09-08 00:42:32 -0700635 .disconnect = eucr_disconnect,
636 .pre_reset = eucr_pre_reset,
637 .post_reset = eucr_post_reset,
638 .id_table = eucr_usb_ids,
639 .soft_unbind = 1,
640};
641
Greg Kroah-Hartmanbac2c122011-11-18 09:42:11 -0800642module_usb_driver(usb_storage_driver);