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