1dac4186 |
1 | /* Driver for Realtek RTS51xx USB card reader |
2 | * |
3 | * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License as published by the |
7 | * Free Software Foundation; either version 2, or (at your option) any |
8 | * later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, but |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, see <http://www.gnu.org/licenses/>. |
17 | * |
18 | * Author: |
19 | * wwang (wei_wang@realsil.com.cn) |
20 | * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China |
21 | * Maintainer: |
22 | * Edwin Rong (edwin_rong@realsil.com.cn) |
23 | * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China |
24 | */ |
25 | |
26 | #include <linux/blkdev.h> |
27 | #include <linux/kthread.h> |
28 | #include <linux/sched.h> |
29 | #include <linux/workqueue.h> |
30 | #include <linux/errno.h> |
31 | #include <linux/freezer.h> |
32 | #include <linux/module.h> |
1dac4186 |
33 | #include <linux/slab.h> |
34 | #include <linux/mutex.h> |
35 | #include <linux/utsname.h> |
36 | #include <linux/usb.h> |
37 | |
38 | #include <scsi/scsi.h> |
39 | #include <scsi/scsi_cmnd.h> |
40 | #include <scsi/scsi_device.h> |
41 | #include <scsi/scsi_devinfo.h> |
42 | #include <scsi/scsi_eh.h> |
43 | #include <scsi/scsi_host.h> |
44 | |
45 | #include "debug.h" |
46 | #include "ms.h" |
47 | #include "rts51x.h" |
48 | #include "rts51x_chip.h" |
49 | #include "rts51x_card.h" |
50 | #include "rts51x_scsi.h" |
51 | #include "rts51x_transport.h" |
52 | #include "rts51x_fop.h" |
53 | |
54 | MODULE_DESCRIPTION(RTS51X_DESC); |
55 | MODULE_LICENSE("GPL"); |
56 | MODULE_VERSION(DRIVER_VERSION); |
57 | |
1dac4186 |
58 | static int auto_delink_en; |
59 | module_param(auto_delink_en, int, S_IRUGO | S_IWUSR); |
60 | MODULE_PARM_DESC(auto_delink_en, "enable auto delink"); |
61 | |
62 | static int ss_en; |
63 | module_param(ss_en, int, S_IRUGO | S_IWUSR); |
64 | MODULE_PARM_DESC(ss_en, "enable selective suspend"); |
65 | |
66 | static int ss_delay = 50; |
67 | module_param(ss_delay, int, S_IRUGO | S_IWUSR); |
68 | MODULE_PARM_DESC(ss_delay, |
69 | "seconds to delay before entering selective suspend"); |
70 | |
71 | static int needs_remote_wakeup; |
72 | module_param(needs_remote_wakeup, int, S_IRUGO | S_IWUSR); |
73 | MODULE_PARM_DESC(needs_remote_wakeup, "ss state needs remote wakeup supported"); |
74 | |
75 | #ifdef SUPPORT_FILE_OP |
76 | static const struct file_operations rts51x_fops = { |
77 | .owner = THIS_MODULE, |
78 | .read = rts51x_read, |
79 | .write = rts51x_write, |
80 | .unlocked_ioctl = rts51x_ioctl, |
81 | .open = rts51x_open, |
82 | .release = rts51x_release, |
83 | }; |
84 | |
85 | /* |
86 | * usb class driver info in order to get a minor number from the usb core, |
87 | * and to have the device registered with the driver core |
88 | */ |
89 | static struct usb_class_driver rts51x_class = { |
90 | .name = "rts51x%d", |
91 | .fops = &rts51x_fops, |
92 | .minor_base = 192, |
93 | }; |
94 | #endif |
95 | |
96 | #ifdef CONFIG_PM /* Minimal support for suspend and resume */ |
97 | |
98 | static inline void usb_autopm_enable(struct usb_interface *intf) |
99 | { |
100 | atomic_set(&intf->pm_usage_cnt, 1); |
101 | usb_autopm_put_interface(intf); |
102 | } |
103 | |
104 | static inline void usb_autopm_disable(struct usb_interface *intf) |
105 | { |
106 | atomic_set(&intf->pm_usage_cnt, 0); |
107 | usb_autopm_get_interface(intf); |
108 | } |
109 | |
60c15301 |
110 | static void rts51x_try_to_enter_ss(struct rts51x_chip *chip) |
1dac4186 |
111 | { |
112 | RTS51X_DEBUGP("Ready to enter SS state\n"); |
113 | usb_autopm_enable(chip->usb->pusb_intf); |
114 | } |
115 | |
116 | void rts51x_try_to_exit_ss(struct rts51x_chip *chip) |
117 | { |
118 | RTS51X_DEBUGP("Exit from SS state\n"); |
119 | usb_autopm_disable(chip->usb->pusb_intf); |
120 | } |
121 | |
122 | int rts51x_suspend(struct usb_interface *iface, pm_message_t message) |
123 | { |
124 | struct rts51x_chip *chip = usb_get_intfdata(iface); |
125 | |
126 | RTS51X_DEBUGP("%s, message.event = 0x%x\n", __func__, message.event); |
127 | |
128 | /* Wait until no command is running */ |
129 | mutex_lock(&chip->usb->dev_mutex); |
130 | |
131 | chip->fake_card_ready = chip->card_ready; |
132 | rts51x_do_before_power_down(chip); |
133 | |
134 | if (message.event == PM_EVENT_AUTO_SUSPEND) { |
135 | RTS51X_DEBUGP("Enter SS state"); |
136 | chip->resume_from_scsi = 0; |
137 | RTS51X_SET_STAT(chip, STAT_SS); |
138 | } else { |
139 | RTS51X_DEBUGP("Enter SUSPEND state"); |
140 | RTS51X_SET_STAT(chip, STAT_SUSPEND); |
141 | } |
142 | |
143 | /* When runtime PM is working, we'll set a flag to indicate |
144 | * whether we should autoresume when a SCSI request arrives. */ |
145 | |
146 | mutex_unlock(&chip->usb->dev_mutex); |
147 | return 0; |
148 | } |
149 | |
150 | int rts51x_resume(struct usb_interface *iface) |
151 | { |
152 | struct rts51x_chip *chip = usb_get_intfdata(iface); |
153 | |
154 | RTS51X_DEBUGP("%s\n", __func__); |
155 | |
156 | if (!RTS51X_CHK_STAT(chip, STAT_SS) || !chip->resume_from_scsi) { |
157 | mutex_lock(&chip->usb->dev_mutex); |
158 | |
159 | if (chip->option.ss_en) { |
160 | if (GET_PM_USAGE_CNT(chip) <= 0) { |
161 | /* Remote wake up, increase pm_usage_cnt */ |
162 | RTS51X_DEBUGP("Incr pm_usage_cnt\n"); |
163 | SET_PM_USAGE_CNT(chip, 1); |
164 | } |
165 | } |
166 | |
167 | RTS51X_SET_STAT(chip, STAT_RUN); |
168 | |
169 | rts51x_init_chip(chip); |
170 | rts51x_init_cards(chip); |
171 | |
172 | mutex_unlock(&chip->usb->dev_mutex); |
173 | } |
174 | |
175 | return 0; |
176 | } |
177 | |
178 | int rts51x_reset_resume(struct usb_interface *iface) |
179 | { |
180 | struct rts51x_chip *chip = usb_get_intfdata(iface); |
181 | |
182 | RTS51X_DEBUGP("%s\n", __func__); |
183 | |
184 | mutex_lock(&chip->usb->dev_mutex); |
185 | |
186 | RTS51X_SET_STAT(chip, STAT_RUN); |
187 | |
188 | if (chip->option.ss_en) |
189 | SET_PM_USAGE_CNT(chip, 1); |
190 | |
191 | rts51x_init_chip(chip); |
192 | rts51x_init_cards(chip); |
193 | |
194 | mutex_unlock(&chip->usb->dev_mutex); |
195 | |
196 | /* FIXME: Notify the subdrivers that they need to reinitialize |
197 | * the device */ |
198 | return 0; |
199 | } |
200 | |
201 | #else /* CONFIG_PM */ |
202 | |
60c15301 |
203 | static void rts51x_try_to_enter_ss(struct rts51x_chip *chip) |
1dac4186 |
204 | { |
205 | } |
206 | |
207 | void rts51x_try_to_exit_ss(struct rts51x_chip *chip) |
208 | { |
209 | } |
210 | |
211 | #endif /* CONFIG_PM */ |
212 | |
213 | /* |
214 | * The next two routines get called just before and just after |
215 | * a USB port reset, whether from this driver or a different one. |
216 | */ |
217 | |
218 | int rts51x_pre_reset(struct usb_interface *iface) |
219 | { |
220 | struct rts51x_chip *chip = usb_get_intfdata(iface); |
221 | |
222 | RTS51X_DEBUGP("%s\n", __func__); |
223 | |
224 | /* Make sure no command runs during the reset */ |
225 | mutex_lock(&chip->usb->dev_mutex); |
226 | return 0; |
227 | } |
228 | |
229 | int rts51x_post_reset(struct usb_interface *iface) |
230 | { |
231 | struct rts51x_chip *chip = usb_get_intfdata(iface); |
232 | |
233 | RTS51X_DEBUGP("%s\n", __func__); |
234 | |
235 | /* Report the reset to the SCSI core */ |
236 | /* usb_stor_report_bus_reset(us); */ |
237 | |
238 | /* FIXME: Notify the subdrivers that they need to reinitialize |
239 | * the device */ |
240 | |
241 | mutex_unlock(&chip->usb->dev_mutex); |
242 | return 0; |
243 | } |
244 | |
245 | static int rts51x_control_thread(void *__chip) |
246 | { |
247 | struct rts51x_chip *chip = (struct rts51x_chip *)__chip; |
248 | struct Scsi_Host *host = rts51x_to_host(chip); |
249 | |
250 | for (;;) { |
251 | if (wait_for_completion_interruptible(&chip->usb->cmnd_ready)) |
252 | break; |
253 | |
254 | if (test_bit(FLIDX_DISCONNECTING, &chip->usb->dflags)) { |
255 | RTS51X_DEBUGP("-- exiting from rts51x-control\n"); |
256 | break; |
257 | } |
258 | |
259 | /* lock the device pointers */ |
260 | mutex_lock(&(chip->usb->dev_mutex)); |
261 | |
262 | /* lock access to the state */ |
263 | scsi_lock(host); |
264 | |
265 | /* When we are called with no command pending, we're done */ |
266 | if (chip->srb == NULL) { |
267 | scsi_unlock(host); |
268 | mutex_unlock(&chip->usb->dev_mutex); |
269 | RTS51X_DEBUGP("-- exiting from control thread\n"); |
270 | break; |
271 | } |
272 | |
273 | /* has the command timed out *already* ? */ |
274 | if (test_bit(FLIDX_TIMED_OUT, &chip->usb->dflags)) { |
275 | chip->srb->result = DID_ABORT << 16; |
276 | goto SkipForAbort; |
277 | } |
278 | |
279 | scsi_unlock(host); |
280 | |
281 | /* reject the command if the direction indicator |
282 | * is UNKNOWN |
283 | */ |
284 | if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) { |
285 | RTS51X_DEBUGP("UNKNOWN data direction\n"); |
286 | chip->srb->result = DID_ERROR << 16; |
287 | } |
288 | |
289 | /* reject if target != 0 or if LUN is higher than |
290 | * the maximum known LUN |
291 | */ |
292 | else if (chip->srb->device->id) { |
293 | RTS51X_DEBUGP("Bad target number (%d:%d)\n", |
294 | chip->srb->device->id, |
295 | chip->srb->device->lun); |
296 | chip->srb->result = DID_BAD_TARGET << 16; |
297 | } |
298 | |
299 | else if (chip->srb->device->lun > chip->max_lun) { |
300 | RTS51X_DEBUGP("Bad LUN (%d:%d)\n", |
301 | chip->srb->device->id, |
302 | chip->srb->device->lun); |
303 | chip->srb->result = DID_BAD_TARGET << 16; |
304 | } |
305 | |
306 | /* we've got a command, let's do it! */ |
307 | else { |
355d8ae5 |
308 | RTS51X_DEBUG(rts51x_scsi_show_command(chip->srb)); |
1dac4186 |
309 | rts51x_invoke_transport(chip->srb, chip); |
310 | } |
311 | |
312 | /* lock access to the state */ |
313 | scsi_lock(host); |
314 | |
315 | /* indicate that the command is done */ |
316 | if (chip->srb->result != DID_ABORT << 16) |
317 | chip->srb->scsi_done(chip->srb); |
318 | else |
319 | SkipForAbort : |
320 | RTS51X_DEBUGP("scsi command aborted\n"); |
321 | |
322 | /* If an abort request was received we need to signal that |
323 | * the abort has finished. The proper test for this is |
324 | * the TIMED_OUT flag, not srb->result == DID_ABORT, because |
325 | * the timeout might have occurred after the command had |
326 | * already completed with a different result code. */ |
327 | if (test_bit(FLIDX_TIMED_OUT, &chip->usb->dflags)) { |
328 | complete(&(chip->usb->notify)); |
329 | |
330 | /* Allow USB transfers to resume */ |
331 | clear_bit(FLIDX_ABORTING, &chip->usb->dflags); |
332 | clear_bit(FLIDX_TIMED_OUT, &chip->usb->dflags); |
333 | } |
334 | |
335 | /* finished working on this command */ |
336 | chip->srb = NULL; |
337 | scsi_unlock(host); |
338 | |
339 | /* unlock the device pointers */ |
340 | mutex_unlock(&chip->usb->dev_mutex); |
341 | } /* for (;;) */ |
342 | |
343 | complete(&chip->usb->control_exit); |
344 | |
345 | /* Wait until we are told to stop */ |
346 | /* for (;;) { |
347 | set_current_state(TASK_INTERRUPTIBLE); |
348 | if (kthread_should_stop()) |
349 | break; |
350 | schedule(); |
351 | } |
352 | __set_current_state(TASK_RUNNING);*/ |
353 | return 0; |
354 | } |
355 | |
356 | static int rts51x_polling_thread(void *__chip) |
357 | { |
358 | struct rts51x_chip *chip = (struct rts51x_chip *)__chip; |
359 | |
1dac4186 |
360 | for (;;) { |
361 | wait_timeout(POLLING_INTERVAL); |
362 | |
363 | /* if the device has disconnected, we are free to exit */ |
364 | if (test_bit(FLIDX_DISCONNECTING, &chip->usb->dflags)) { |
365 | RTS51X_DEBUGP("-- exiting from rts51x-polling\n"); |
366 | break; |
367 | } |
368 | |
369 | /* if the device has disconnected, we are free to exit */ |
370 | /* if (kthread_should_stop()) { |
371 | printk(KERN_INFO "Stop polling thread!\n"); |
372 | break; |
373 | } */ |
374 | |
375 | #ifdef CONFIG_PM |
376 | if (RTS51X_CHK_STAT(chip, STAT_SS) || |
377 | RTS51X_CHK_STAT(chip, STAT_SS_PRE) || |
378 | RTS51X_CHK_STAT(chip, STAT_SUSPEND)) { |
379 | continue; |
380 | } |
381 | |
382 | if (ss_en) { |
383 | if (RTS51X_CHK_STAT(chip, STAT_IDLE)) { |
384 | if (chip->ss_counter < |
385 | (ss_delay * 1000 / POLLING_INTERVAL)) { |
386 | chip->ss_counter++; |
387 | } else { |
388 | /* Prepare SS state */ |
389 | RTS51X_SET_STAT(chip, STAT_SS_PRE); |
390 | rts51x_try_to_enter_ss(chip); |
391 | continue; |
392 | } |
393 | } else { |
394 | chip->ss_counter = 0; |
395 | } |
396 | } |
397 | #endif |
398 | |
355d8ae5 |
399 | rts51x_mspro_polling_format_status(chip); |
1dac4186 |
400 | |
401 | /* lock the device pointers */ |
402 | mutex_lock(&(chip->usb->dev_mutex)); |
403 | |
404 | rts51x_polling_func(chip); |
405 | |
406 | /* unlock the device pointers */ |
407 | mutex_unlock(&chip->usb->dev_mutex); |
408 | } /* for (;;) */ |
409 | |
410 | complete(&chip->usb->polling_exit); |
411 | |
412 | /* Wait until we are told to stop */ |
413 | /* for (;;) { |
414 | set_current_state(TASK_INTERRUPTIBLE); |
415 | if (kthread_should_stop()) |
416 | break; |
417 | schedule(); |
418 | } |
419 | __set_current_state(TASK_RUNNING); */ |
420 | return 0; |
421 | } |
422 | |
1dac4186 |
423 | /* Associate our private data with the USB device */ |
424 | static int associate_dev(struct rts51x_chip *chip, struct usb_interface *intf) |
425 | { |
426 | struct rts51x_usb *rts51x = chip->usb; |
427 | #ifdef SUPPORT_FILE_OP |
428 | int retval; |
429 | #endif |
430 | |
431 | /* Fill in the device-related fields */ |
432 | rts51x->pusb_dev = interface_to_usbdev(intf); |
433 | rts51x->pusb_intf = intf; |
434 | rts51x->ifnum = intf->cur_altsetting->desc.bInterfaceNumber; |
435 | RTS51X_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n", |
436 | le16_to_cpu(rts51x->pusb_dev->descriptor.idVendor), |
437 | le16_to_cpu(rts51x->pusb_dev->descriptor.idProduct), |
438 | le16_to_cpu(rts51x->pusb_dev->descriptor.bcdDevice)); |
439 | RTS51X_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n", |
440 | intf->cur_altsetting->desc.bInterfaceSubClass, |
441 | intf->cur_altsetting->desc.bInterfaceProtocol); |
442 | |
443 | /* Store our private data in the interface */ |
444 | usb_set_intfdata(intf, chip); |
445 | |
446 | #ifdef SUPPORT_FILE_OP |
447 | /* we can register the device now, as it is ready */ |
448 | retval = usb_register_dev(intf, &rts51x_class); |
449 | if (retval) { |
450 | /* something prevented us from registering this driver */ |
451 | RTS51X_DEBUGP("Not able to get a minor for this device."); |
452 | usb_set_intfdata(intf, NULL); |
453 | return -ENOMEM; |
454 | } |
455 | #endif |
456 | |
457 | /* Allocate the device-related DMA-mapped buffers */ |
458 | rts51x->cr = usb_buffer_alloc(rts51x->pusb_dev, sizeof(*rts51x->cr), |
459 | GFP_KERNEL, &rts51x->cr_dma); |
460 | if (!rts51x->cr) { |
461 | RTS51X_DEBUGP("usb_ctrlrequest allocation failed\n"); |
462 | usb_set_intfdata(intf, NULL); |
463 | return -ENOMEM; |
464 | } |
465 | |
466 | rts51x->iobuf = usb_buffer_alloc(rts51x->pusb_dev, RTS51X_IOBUF_SIZE, |
467 | GFP_KERNEL, &rts51x->iobuf_dma); |
468 | if (!rts51x->iobuf) { |
469 | RTS51X_DEBUGP("I/O buffer allocation failed\n"); |
470 | usb_set_intfdata(intf, NULL); |
471 | return -ENOMEM; |
472 | } |
473 | return 0; |
474 | } |
475 | |
476 | static void rts51x_init_options(struct rts51x_chip *chip) |
477 | { |
478 | struct rts51x_option *option = &(chip->option); |
479 | |
355d8ae5 |
480 | option->rts51x_mspro_formatter_enable = 1; |
1dac4186 |
481 | |
482 | option->fpga_sd_sdr104_clk = CLK_100; |
483 | option->fpga_sd_sdr50_clk = CLK_100; |
484 | option->fpga_sd_ddr50_clk = CLK_100; |
485 | option->fpga_sd_hs_clk = CLK_100; |
486 | option->fpga_mmc_52m_clk = CLK_80; |
487 | option->fpga_ms_hg_clk = CLK_80; |
488 | option->fpga_ms_4bit_clk = CLK_80; |
489 | |
490 | option->asic_sd_sdr104_clk = 98; |
491 | option->asic_sd_sdr50_clk = 98; |
492 | option->asic_sd_ddr50_clk = 98; |
493 | option->asic_sd_hs_clk = 97; |
494 | option->asic_mmc_52m_clk = 95; |
495 | option->asic_ms_hg_clk = 116; |
496 | option->asic_ms_4bit_clk = 77; |
497 | |
498 | option->sd_ddr_tx_phase = 0; |
499 | option->mmc_ddr_tx_phase = 1; |
500 | |
501 | option->sd_speed_prior = 0; |
502 | option->sd_ctl = |
503 | SD_PUSH_POINT_AUTO | SD_SAMPLE_POINT_AUTO | SUPPORT_UHS50_MMC44; |
504 | |
505 | option->ss_en = ss_en; |
506 | option->ss_delay = ss_delay; |
1dac4186 |
507 | |
508 | option->auto_delink_en = auto_delink_en; |
509 | |
510 | option->FT2_fast_mode = 0; |
511 | option->pwr_delay = 800; |
355d8ae5 |
512 | option->rts51x_xd_rw_step = 0; |
1dac4186 |
513 | option->D3318_off_delay = 50; |
514 | option->delink_delay = 100; |
515 | option->rts5129_D3318_off_enable = 0; |
516 | option->sd20_pad_drive = 0; |
517 | option->reset_or_rw_fail_set_pad_drive = 1; |
1dac4186 |
518 | option->debounce_num = 2; |
1dac4186 |
519 | option->led_toggle_interval = 6; |
355d8ae5 |
520 | option->rts51x_xd_rwn_step = 0; |
1dac4186 |
521 | option->sd_send_status_en = 0; |
522 | option->sdr50_tx_phase = 0x01; |
523 | option->sdr50_rx_phase = 0x05; |
524 | option->ddr50_tx_phase = 0x09; |
525 | option->ddr50_rx_phase = 0x06; |
526 | option->sdr50_phase_sel = 0; |
527 | option->sd30_pad_drive = 1; |
528 | option->ms_errreg_fix = 0; |
529 | option->reset_mmc_first = 0; |
530 | option->speed_mmc = 1; |
531 | option->led_always_on = 0; |
532 | } |
533 | |
534 | /* Get the pipe settings */ |
535 | static int get_pipes(struct rts51x_chip *chip) |
536 | { |
537 | struct rts51x_usb *rts51x = chip->usb; |
538 | struct usb_host_interface *altsetting = |
539 | rts51x->pusb_intf->cur_altsetting; |
540 | int i; |
541 | struct usb_endpoint_descriptor *ep; |
542 | struct usb_endpoint_descriptor *ep_in = NULL; |
543 | struct usb_endpoint_descriptor *ep_out = NULL; |
544 | struct usb_endpoint_descriptor *ep_int = NULL; |
545 | |
546 | /* |
547 | * Find the first endpoint of each type we need. |
548 | * We are expecting a minimum of 2 endpoints - in and out (bulk). |
549 | * An optional interrupt-in is OK (necessary for CBI protocol). |
550 | * We will ignore any others. |
551 | */ |
552 | for (i = 0; i < altsetting->desc.bNumEndpoints; i++) { |
553 | ep = &altsetting->endpoint[i].desc; |
554 | |
555 | if (usb_endpoint_xfer_bulk(ep)) { |
556 | if (usb_endpoint_dir_in(ep)) { |
557 | if (!ep_in) |
558 | ep_in = ep; |
559 | } else { |
560 | if (!ep_out) |
561 | ep_out = ep; |
562 | } |
563 | } |
564 | |
565 | else if (usb_endpoint_is_int_in(ep)) { |
566 | if (!ep_int) |
567 | ep_int = ep; |
568 | } |
569 | } |
570 | |
571 | if (!ep_in || !ep_out) { |
572 | RTS51X_DEBUGP("Endpoint sanity check failed!" |
573 | "Rejecting dev.\n"); |
574 | return -EIO; |
575 | } |
576 | |
577 | /* Calculate and store the pipe values */ |
578 | rts51x->send_ctrl_pipe = usb_sndctrlpipe(rts51x->pusb_dev, 0); |
579 | rts51x->recv_ctrl_pipe = usb_rcvctrlpipe(rts51x->pusb_dev, 0); |
580 | rts51x->send_bulk_pipe = usb_sndbulkpipe(rts51x->pusb_dev, |
581 | usb_endpoint_num(ep_out)); |
582 | rts51x->recv_bulk_pipe = usb_rcvbulkpipe(rts51x->pusb_dev, |
583 | usb_endpoint_num(ep_in)); |
584 | if (ep_int) { |
585 | rts51x->recv_intr_pipe = usb_rcvintpipe(rts51x->pusb_dev, |
586 | usb_endpoint_num |
587 | (ep_int)); |
588 | rts51x->ep_bInterval = ep_int->bInterval; |
589 | } |
590 | return 0; |
591 | } |
592 | |
593 | /* Initialize all the dynamic resources we need */ |
594 | static int rts51x_acquire_resources(struct rts51x_chip *chip) |
595 | { |
596 | struct rts51x_usb *rts51x = chip->usb; |
597 | int retval; |
598 | |
599 | rts51x->current_urb = usb_alloc_urb(0, GFP_KERNEL); |
600 | if (!rts51x->current_urb) { |
601 | RTS51X_DEBUGP("URB allocation failed\n"); |
602 | return -ENOMEM; |
603 | } |
604 | |
605 | rts51x->intr_urb = usb_alloc_urb(0, GFP_KERNEL); |
606 | if (!rts51x->intr_urb) { |
607 | RTS51X_DEBUGP("URB allocation failed\n"); |
608 | return -ENOMEM; |
609 | } |
610 | |
611 | chip->cmd_buf = chip->rsp_buf = rts51x->iobuf; |
612 | |
613 | rts51x_init_options(chip); |
614 | |
615 | /* Init rts51xx device */ |
616 | retval = rts51x_init_chip(chip); |
617 | if (retval != STATUS_SUCCESS) |
618 | return -EIO; |
619 | |
620 | return 0; |
621 | } |
622 | |
623 | /* Release all our dynamic resources */ |
624 | static void rts51x_release_resources(struct rts51x_chip *chip) |
625 | { |
626 | RTS51X_DEBUGP("-- %s\n", __func__); |
627 | |
628 | /* Tell the control thread to exit. The SCSI host must |
629 | * already have been removed and the DISCONNECTING flag set |
630 | * so that we won't accept any more commands. |
631 | */ |
632 | RTS51X_DEBUGP("-- sending exit command to thread\n"); |
633 | complete(&chip->usb->cmnd_ready); |
634 | if (chip->usb->ctl_thread) |
635 | wait_for_completion(&chip->usb->control_exit); |
636 | /* kthread_stop(chip->usb->ctl_thread); */ |
637 | if (chip->usb->polling_thread) |
638 | wait_for_completion(&chip->usb->polling_exit); |
639 | |
640 | /* if (chip->usb->polling_thread) |
641 | kthread_stop(chip->usb->polling_thread); */ |
642 | |
643 | wait_timeout(200); |
644 | |
645 | /* Release rts51xx device here */ |
646 | rts51x_release_chip(chip); |
647 | |
648 | usb_free_urb(chip->usb->current_urb); |
649 | usb_free_urb(chip->usb->intr_urb); |
650 | } |
651 | |
652 | /* Dissociate from the USB device */ |
653 | static void dissociate_dev(struct rts51x_chip *chip) |
654 | { |
655 | struct rts51x_usb *rts51x = chip->usb; |
656 | |
657 | RTS51X_DEBUGP("-- %s\n", __func__); |
658 | |
659 | /* Free the device-related DMA-mapped buffers */ |
660 | if (rts51x->cr) |
661 | usb_buffer_free(rts51x->pusb_dev, sizeof(*rts51x->cr), |
662 | rts51x->cr, rts51x->cr_dma); |
663 | if (rts51x->iobuf) |
664 | usb_buffer_free(rts51x->pusb_dev, RTS51X_IOBUF_SIZE, |
665 | rts51x->iobuf, rts51x->iobuf_dma); |
666 | |
667 | /* Remove our private data from the interface */ |
668 | usb_set_intfdata(rts51x->pusb_intf, NULL); |
669 | |
670 | #ifdef SUPPORT_FILE_OP |
671 | /* give back our minor */ |
672 | usb_deregister_dev(rts51x->pusb_intf, &rts51x_class); |
673 | #endif |
674 | |
675 | kfree(rts51x); |
676 | chip->usb = NULL; |
677 | } |
678 | |
679 | /* First stage of disconnect processing: stop SCSI scanning, |
680 | * remove the host, and stop accepting new commands |
681 | */ |
682 | static void quiesce_and_remove_host(struct rts51x_chip *chip) |
683 | { |
684 | struct rts51x_usb *rts51x = chip->usb; |
685 | struct Scsi_Host *host = rts51x_to_host(chip); |
686 | |
687 | /* If the device is really gone, cut short reset delays */ |
688 | if (rts51x->pusb_dev->state == USB_STATE_NOTATTACHED) |
689 | set_bit(FLIDX_DISCONNECTING, &rts51x->dflags); |
690 | |
1dac4186 |
691 | /* Removing the host will perform an orderly shutdown: caches |
692 | * synchronized, disks spun down, etc. |
693 | */ |
694 | scsi_remove_host(host); |
695 | |
696 | /* Prevent any new commands from being accepted and cut short |
697 | * reset delays. |
698 | */ |
699 | scsi_lock(host); |
700 | set_bit(FLIDX_DISCONNECTING, &rts51x->dflags); |
701 | scsi_unlock(host); |
1dac4186 |
702 | } |
703 | |
704 | /* Second stage of disconnect processing: deallocate all resources */ |
705 | static void release_everything(struct rts51x_chip *chip) |
706 | { |
707 | rts51x_release_resources(chip); |
708 | dissociate_dev(chip); |
709 | |
710 | /* Drop our reference to the host; the SCSI core will free it |
711 | * (and "chip" along with it) when the refcount becomes 0. */ |
712 | scsi_host_put(rts51x_to_host(chip)); |
713 | } |
714 | |
715 | static int rts51x_probe(struct usb_interface *intf, |
716 | const struct usb_device_id *id) |
717 | { |
718 | struct Scsi_Host *host; |
719 | struct rts51x_chip *chip; |
720 | struct rts51x_usb *rts51x; |
721 | int result; |
722 | struct task_struct *th; |
723 | |
724 | RTS51X_DEBUGP("%s detected\n", RTS51X_NAME); |
725 | |
726 | rts51x = kzalloc(sizeof(struct rts51x_usb), GFP_KERNEL); |
727 | if (!rts51x) { |
728 | printk(KERN_WARNING RTS51X_TIP |
729 | "Unable to allocate rts51x_usb\n"); |
730 | return -ENOMEM; |
731 | } |
732 | |
733 | /* |
734 | * Ask the SCSI layer to allocate a host structure, with extra |
735 | * space at the end for our private us_data structure. |
736 | */ |
737 | host = scsi_host_alloc(&rts51x_host_template, sizeof(*chip)); |
738 | if (!host) { |
739 | printk(KERN_WARNING RTS51X_TIP |
740 | "Unable to allocate the scsi host\n"); |
741 | kfree(rts51x); |
742 | return -ENOMEM; |
743 | } |
744 | |
745 | /* |
746 | * Allow 16-byte CDBs and thus > 2TB |
747 | */ |
748 | host->max_cmd_len = 16; |
749 | chip = host_to_rts51x(host); |
750 | memset(chip, 0, sizeof(struct rts51x_chip)); |
751 | |
752 | chip->vendor_id = id->idVendor; |
753 | chip->product_id = id->idProduct; |
754 | |
755 | mutex_init(&(rts51x->dev_mutex)); |
756 | init_completion(&rts51x->cmnd_ready); |
757 | init_completion(&rts51x->control_exit); |
758 | init_completion(&rts51x->polling_exit); |
759 | init_completion(&(rts51x->notify)); |
1dac4186 |
760 | |
761 | chip->usb = rts51x; |
762 | |
763 | /* Associate the us_data structure with the USB device */ |
764 | result = associate_dev(chip, intf); |
765 | if (result) |
766 | goto BadDevice; |
767 | |
768 | /* Find the endpoints and calculate pipe values */ |
769 | result = get_pipes(chip); |
770 | if (result) |
771 | goto BadDevice; |
772 | |
773 | /* Acquire all the other resources and add the host */ |
774 | result = rts51x_acquire_resources(chip); |
775 | if (result) |
776 | goto BadDevice; |
777 | |
778 | /* Start up our control thread */ |
779 | th = kthread_run(rts51x_control_thread, chip, RTS51X_CTL_THREAD); |
780 | if (IS_ERR(th)) { |
781 | printk(KERN_WARNING RTS51X_TIP |
782 | "Unable to start control thread\n"); |
783 | result = PTR_ERR(th); |
784 | goto BadDevice; |
785 | } |
786 | rts51x->ctl_thread = th; |
787 | |
788 | result = scsi_add_host(rts51x_to_host(chip), &rts51x->pusb_intf->dev); |
789 | if (result) { |
790 | printk(KERN_WARNING RTS51X_TIP "Unable to add the scsi host\n"); |
791 | goto BadDevice; |
792 | } |
1dac4186 |
793 | scsi_scan_host(rts51x_to_host(chip)); |
1dac4186 |
794 | |
795 | /* Start up our polling thread */ |
796 | th = kthread_run(rts51x_polling_thread, chip, RTS51X_POLLING_THREAD); |
797 | if (IS_ERR(th)) { |
798 | printk(KERN_WARNING RTS51X_TIP |
799 | "Unable to start polling thread\n"); |
800 | result = PTR_ERR(th); |
801 | goto BadDevice; |
802 | } |
803 | rts51x->polling_thread = th; |
804 | |
805 | #ifdef CONFIG_PM |
806 | if (ss_en) { |
807 | rts51x->pusb_intf->needs_remote_wakeup = needs_remote_wakeup; |
808 | SET_PM_USAGE_CNT(chip, 1); |
809 | RTS51X_DEBUGP("pm_usage_cnt = %d\n", GET_PM_USAGE_CNT(chip)); |
810 | } |
811 | #endif |
812 | |
813 | return 0; |
814 | |
815 | /* We come here if there are any problems */ |
816 | BadDevice: |
817 | RTS51X_DEBUGP("rts51x_probe() failed\n"); |
818 | release_everything(chip); |
819 | return result; |
820 | } |
821 | |
822 | static void rts51x_disconnect(struct usb_interface *intf) |
823 | { |
824 | struct rts51x_chip *chip = (struct rts51x_chip *)usb_get_intfdata(intf); |
825 | |
826 | RTS51X_DEBUGP("rts51x_disconnect() called\n"); |
827 | quiesce_and_remove_host(chip); |
828 | release_everything(chip); |
829 | } |
830 | |
831 | /*********************************************************************** |
832 | * Initialization and registration |
833 | ***********************************************************************/ |
834 | |
835 | struct usb_device_id rts5139_usb_ids[] = { |
836 | {USB_DEVICE(0x0BDA, 0x0139)}, |
837 | {USB_DEVICE(0x0BDA, 0x0129)}, |
838 | {} /* Terminating entry */ |
839 | }; |
840 | EXPORT_SYMBOL_GPL(rts5139_usb_ids); |
841 | |
842 | MODULE_DEVICE_TABLE(usb, rts5139_usb_ids); |
843 | |
844 | struct usb_driver rts51x_driver = { |
845 | .name = RTS51X_NAME, |
846 | .probe = rts51x_probe, |
847 | .disconnect = rts51x_disconnect, |
848 | .suspend = rts51x_suspend, |
849 | .resume = rts51x_resume, |
850 | .reset_resume = rts51x_reset_resume, |
851 | .pre_reset = rts51x_pre_reset, |
852 | .post_reset = rts51x_post_reset, |
853 | .id_table = rts5139_usb_ids, |
854 | .soft_unbind = 1, |
855 | }; |
856 | |
bac2c126 |
857 | module_usb_driver(rts51x_driver); |