Commit | Line | Data |
---|---|---|
5b3f03f0 HS |
1 | /* |
2 | * device driver for Telegent tlg2300 based TV cards | |
3 | * | |
4 | * Author : | |
5 | * Kang Yong <kangyong@telegent.com> | |
6 | * Zhang Xiaobing <xbzhang@telegent.com> | |
7 | * Huang Shijie <zyziii@telegent.com> or <shijie8@gmail.com> | |
8 | * | |
9 | * (c) 2009 Telegent Systems | |
10 | * (c) 2010 Telegent Systems | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program; if not, write to the Free Software | |
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | */ | |
26 | ||
5b3f03f0 HS |
27 | #include <linux/kernel.h> |
28 | #include <linux/errno.h> | |
29 | #include <linux/init.h> | |
30 | #include <linux/slab.h> | |
31 | #include <linux/module.h> | |
32 | #include <linux/kref.h> | |
33 | #include <linux/suspend.h> | |
34 | #include <linux/usb/quirks.h> | |
35 | #include <linux/ctype.h> | |
36 | #include <linux/string.h> | |
37 | #include <linux/types.h> | |
38 | #include <linux/firmware.h> | |
5b3f03f0 HS |
39 | |
40 | #include "vendorcmds.h" | |
41 | #include "pd-common.h" | |
42 | ||
43 | #define VENDOR_ID 0x1B24 | |
44 | #define PRODUCT_ID 0x4001 | |
45 | static struct usb_device_id id_table[] = { | |
46 | { USB_DEVICE_AND_INTERFACE_INFO(VENDOR_ID, PRODUCT_ID, 255, 1, 0) }, | |
47 | { USB_DEVICE_AND_INTERFACE_INFO(VENDOR_ID, PRODUCT_ID, 255, 1, 1) }, | |
48 | { }, | |
49 | }; | |
50 | MODULE_DEVICE_TABLE(usb, id_table); | |
51 | ||
52 | int debug_mode; | |
53 | module_param(debug_mode, int, 0644); | |
54 | MODULE_PARM_DESC(debug_mode, "0 = disable, 1 = enable, 2 = verbose"); | |
55 | ||
278a58cf MN |
56 | static const char *firmware_name = "tlg2300_firmware.bin"; |
57 | static struct usb_driver poseidon_driver; | |
5b3f03f0 HS |
58 | static LIST_HEAD(pd_device_list); |
59 | ||
60 | /* | |
61 | * send set request to USB firmware. | |
62 | */ | |
63 | s32 send_set_req(struct poseidon *pd, u8 cmdid, s32 param, s32 *cmd_status) | |
64 | { | |
65 | s32 ret; | |
66 | s8 data[32] = {}; | |
67 | u16 lower_16, upper_16; | |
68 | ||
69 | if (pd->state & POSEIDON_STATE_DISCONNECT) | |
70 | return -ENODEV; | |
71 | ||
72 | mdelay(30); | |
73 | ||
74 | if (param == 0) { | |
75 | upper_16 = lower_16 = 0; | |
76 | } else { | |
77 | /* send 32 bit param as two 16 bit param,little endian */ | |
78 | lower_16 = (unsigned short)(param & 0xffff); | |
79 | upper_16 = (unsigned short)((param >> 16) & 0xffff); | |
80 | } | |
81 | ret = usb_control_msg(pd->udev, | |
82 | usb_rcvctrlpipe(pd->udev, 0), | |
83 | REQ_SET_CMD | cmdid, | |
84 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
85 | lower_16, | |
86 | upper_16, | |
87 | &data, | |
88 | sizeof(*cmd_status), | |
89 | USB_CTRL_GET_TIMEOUT); | |
90 | ||
91 | if (!ret) { | |
92 | return -ENXIO; | |
93 | } else { | |
94 | /* 1st 4 bytes into cmd_status */ | |
95 | memcpy((char *)cmd_status, &(data[0]), sizeof(*cmd_status)); | |
96 | } | |
97 | return 0; | |
98 | } | |
99 | ||
100 | /* | |
101 | * send get request to Poseidon firmware. | |
102 | */ | |
103 | s32 send_get_req(struct poseidon *pd, u8 cmdid, s32 param, | |
104 | void *buf, s32 *cmd_status, s32 datalen) | |
105 | { | |
106 | s32 ret; | |
107 | s8 data[128] = {}; | |
108 | u16 lower_16, upper_16; | |
109 | ||
110 | if (pd->state & POSEIDON_STATE_DISCONNECT) | |
111 | return -ENODEV; | |
112 | ||
113 | mdelay(30); | |
114 | if (param == 0) { | |
115 | upper_16 = lower_16 = 0; | |
116 | } else { | |
117 | /*send 32 bit param as two 16 bit param, little endian */ | |
118 | lower_16 = (unsigned short)(param & 0xffff); | |
119 | upper_16 = (unsigned short)((param >> 16) & 0xffff); | |
120 | } | |
121 | ret = usb_control_msg(pd->udev, | |
122 | usb_rcvctrlpipe(pd->udev, 0), | |
123 | REQ_GET_CMD | cmdid, | |
124 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
125 | lower_16, | |
126 | upper_16, | |
127 | &data, | |
128 | (datalen + sizeof(*cmd_status)), | |
129 | USB_CTRL_GET_TIMEOUT); | |
130 | ||
131 | if (ret < 0) { | |
132 | return -ENXIO; | |
133 | } else { | |
134 | /* 1st 4 bytes into cmd_status, remaining data into cmd_data */ | |
135 | memcpy((char *)cmd_status, &data[0], sizeof(*cmd_status)); | |
136 | memcpy((char *)buf, &data[sizeof(*cmd_status)], datalen); | |
137 | } | |
138 | return 0; | |
139 | } | |
140 | ||
141 | static int pm_notifier_block(struct notifier_block *nb, | |
142 | unsigned long event, void *dummy) | |
143 | { | |
144 | struct poseidon *pd = NULL; | |
145 | struct list_head *node, *next; | |
146 | ||
147 | switch (event) { | |
148 | case PM_POST_HIBERNATION: | |
149 | list_for_each_safe(node, next, &pd_device_list) { | |
150 | struct usb_device *udev; | |
151 | struct usb_interface *iface; | |
152 | int rc = 0; | |
153 | ||
154 | pd = container_of(node, struct poseidon, device_list); | |
155 | udev = pd->udev; | |
156 | iface = pd->interface; | |
157 | ||
158 | /* It will cause the system to reload the firmware */ | |
159 | rc = usb_lock_device_for_reset(udev, iface); | |
160 | if (rc >= 0) { | |
161 | usb_reset_device(udev); | |
162 | usb_unlock_device(udev); | |
163 | } | |
164 | } | |
165 | break; | |
166 | default: | |
167 | break; | |
168 | } | |
169 | log("event :%ld\n", event); | |
170 | return 0; | |
171 | } | |
172 | ||
173 | static struct notifier_block pm_notifer = { | |
174 | .notifier_call = pm_notifier_block, | |
175 | }; | |
176 | ||
177 | int set_tuner_mode(struct poseidon *pd, unsigned char mode) | |
178 | { | |
179 | s32 ret, cmd_status; | |
180 | ||
181 | if (pd->state & POSEIDON_STATE_DISCONNECT) | |
182 | return -ENODEV; | |
183 | ||
184 | ret = send_set_req(pd, TUNE_MODE_SELECT, mode, &cmd_status); | |
185 | if (ret || cmd_status) | |
186 | return -ENXIO; | |
187 | return 0; | |
188 | } | |
189 | ||
5b3f03f0 HS |
190 | void poseidon_delete(struct kref *kref) |
191 | { | |
192 | struct poseidon *pd = container_of(kref, struct poseidon, kref); | |
193 | ||
194 | if (!pd) | |
195 | return; | |
196 | list_del_init(&pd->device_list); | |
197 | ||
198 | pd_dvb_usb_device_cleanup(pd); | |
199 | /* clean_audio_data(&pd->audio_data);*/ | |
200 | ||
201 | if (pd->udev) { | |
202 | usb_put_dev(pd->udev); | |
203 | pd->udev = NULL; | |
204 | } | |
205 | if (pd->interface) { | |
206 | usb_put_intf(pd->interface); | |
207 | pd->interface = NULL; | |
208 | } | |
209 | kfree(pd); | |
210 | log(); | |
211 | } | |
212 | ||
213 | static int firmware_download(struct usb_device *udev) | |
214 | { | |
215 | int ret = 0, actual_length; | |
216 | const struct firmware *fw = NULL; | |
217 | void *fwbuf = NULL; | |
218 | size_t fwlength = 0, offset; | |
219 | size_t max_packet_size; | |
220 | ||
221 | ret = request_firmware(&fw, firmware_name, &udev->dev); | |
222 | if (ret) { | |
223 | log("download err : %d", ret); | |
224 | return ret; | |
225 | } | |
226 | ||
227 | fwlength = fw->size; | |
228 | ||
2b3c543a | 229 | fwbuf = kmemdup(fw->data, fwlength, GFP_KERNEL); |
5b3f03f0 HS |
230 | if (!fwbuf) { |
231 | ret = -ENOMEM; | |
232 | goto out; | |
233 | } | |
5b3f03f0 HS |
234 | |
235 | max_packet_size = udev->ep_out[0x1]->desc.wMaxPacketSize; | |
236 | log("\t\t download size : %d", (int)max_packet_size); | |
237 | ||
238 | for (offset = 0; offset < fwlength; offset += max_packet_size) { | |
239 | actual_length = 0; | |
240 | ret = usb_bulk_msg(udev, | |
241 | usb_sndbulkpipe(udev, 0x01), /* ep 1 */ | |
242 | fwbuf + offset, | |
243 | min(max_packet_size, fwlength - offset), | |
244 | &actual_length, | |
245 | HZ * 10); | |
246 | if (ret) | |
247 | break; | |
248 | } | |
249 | kfree(fwbuf); | |
250 | out: | |
251 | release_firmware(fw); | |
252 | return ret; | |
253 | } | |
254 | ||
08cf8a57 RD |
255 | static inline struct poseidon *get_pd(struct usb_interface *intf) |
256 | { | |
257 | return usb_get_intfdata(intf); | |
258 | } | |
259 | ||
5b3f03f0 HS |
260 | #ifdef CONFIG_PM |
261 | /* one-to-one map : poseidon{} <----> usb_device{}'s port */ | |
262 | static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev) | |
263 | { | |
264 | pd->portnum = udev->portnum; | |
265 | } | |
266 | ||
267 | static inline int get_autopm_ref(struct poseidon *pd) | |
268 | { | |
269 | return pd->video_data.users + pd->vbi_data.users + pd->audio.users | |
270 | + atomic_read(&pd->dvb_data.users) + pd->radio_data.users; | |
271 | } | |
272 | ||
273 | /* fixup something for poseidon */ | |
274 | static inline struct poseidon *fixup(struct poseidon *pd) | |
275 | { | |
276 | int count; | |
277 | ||
278 | /* old udev and interface have gone, so put back reference . */ | |
279 | count = get_autopm_ref(pd); | |
280 | log("count : %d, ref count : %d", count, get_pm_count(pd)); | |
281 | while (count--) | |
282 | usb_autopm_put_interface(pd->interface); | |
283 | /*usb_autopm_set_interface(pd->interface); */ | |
284 | ||
285 | usb_put_dev(pd->udev); | |
286 | usb_put_intf(pd->interface); | |
287 | log("event : %d\n", pd->msg.event); | |
288 | return pd; | |
289 | } | |
290 | ||
291 | static struct poseidon *find_old_poseidon(struct usb_device *udev) | |
292 | { | |
293 | struct poseidon *pd; | |
294 | ||
295 | list_for_each_entry(pd, &pd_device_list, device_list) { | |
296 | if (pd->portnum == udev->portnum && in_hibernation(pd)) | |
297 | return fixup(pd); | |
298 | } | |
299 | return NULL; | |
300 | } | |
301 | ||
302 | /* Is the card working now ? */ | |
303 | static inline int is_working(struct poseidon *pd) | |
304 | { | |
305 | return get_pm_count(pd) > 0; | |
306 | } | |
307 | ||
5b3f03f0 HS |
308 | static int poseidon_suspend(struct usb_interface *intf, pm_message_t msg) |
309 | { | |
310 | struct poseidon *pd = get_pd(intf); | |
311 | ||
312 | if (!pd) | |
313 | return 0; | |
314 | if (!is_working(pd)) { | |
315 | if (get_pm_count(pd) <= 0 && !in_hibernation(pd)) { | |
316 | pd->msg.event = PM_EVENT_AUTO_SUSPEND; | |
317 | pd->pm_resume = NULL; /* a good guard */ | |
318 | printk(KERN_DEBUG "\n\t+ TLG2300 auto suspend +\n\n"); | |
319 | } | |
320 | return 0; | |
321 | } | |
322 | pd->msg = msg; /* save it here */ | |
323 | logpm(pd); | |
324 | return pd->pm_suspend ? pd->pm_suspend(pd) : 0; | |
325 | } | |
326 | ||
327 | static int poseidon_resume(struct usb_interface *intf) | |
328 | { | |
329 | struct poseidon *pd = get_pd(intf); | |
330 | ||
331 | if (!pd) | |
332 | return 0; | |
333 | printk(KERN_DEBUG "\n\t ++ TLG2300 resume ++\n\n"); | |
334 | ||
335 | if (!is_working(pd)) { | |
336 | if (PM_EVENT_AUTO_SUSPEND == pd->msg.event) | |
337 | pd->msg = PMSG_ON; | |
338 | return 0; | |
339 | } | |
340 | if (in_hibernation(pd)) { | |
341 | logpm(pd); | |
342 | return 0; | |
343 | } | |
344 | logpm(pd); | |
345 | return pd->pm_resume ? pd->pm_resume(pd) : 0; | |
346 | } | |
347 | ||
348 | static void hibernation_resume(struct work_struct *w) | |
349 | { | |
350 | struct poseidon *pd = container_of(w, struct poseidon, pm_work); | |
351 | int count; | |
352 | ||
353 | pd->msg.event = 0; /* clear it here */ | |
354 | pd->state &= ~POSEIDON_STATE_DISCONNECT; | |
355 | ||
356 | /* set the new interface's reference */ | |
357 | count = get_autopm_ref(pd); | |
358 | while (count--) | |
359 | usb_autopm_get_interface(pd->interface); | |
360 | ||
361 | /* resume the context */ | |
362 | logpm(pd); | |
363 | if (pd->pm_resume) | |
364 | pd->pm_resume(pd); | |
365 | } | |
08cf8a57 RD |
366 | #else /* CONFIG_PM is not enabled: */ |
367 | static inline struct poseidon *find_old_poseidon(struct usb_device *udev) | |
368 | { | |
369 | return NULL; | |
370 | } | |
371 | ||
372 | static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev) | |
373 | { | |
374 | } | |
5b3f03f0 HS |
375 | #endif |
376 | ||
e9ae4d4f | 377 | static int check_firmware(struct usb_device *udev, int *down_firmware) |
5b3f03f0 HS |
378 | { |
379 | void *buf; | |
380 | int ret; | |
381 | struct cmd_firmware_vers_s *cmd_firm; | |
382 | ||
383 | buf = kzalloc(sizeof(*cmd_firm) + sizeof(u32), GFP_KERNEL); | |
384 | if (!buf) | |
385 | return -ENOMEM; | |
386 | ret = usb_control_msg(udev, | |
387 | usb_rcvctrlpipe(udev, 0), | |
388 | REQ_GET_CMD | GET_FW_ID, | |
389 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
390 | 0, | |
391 | 0, | |
392 | buf, | |
393 | sizeof(*cmd_firm) + sizeof(u32), | |
394 | USB_CTRL_GET_TIMEOUT); | |
395 | kfree(buf); | |
396 | ||
397 | if (ret < 0) { | |
398 | *down_firmware = 1; | |
399 | return firmware_download(udev); | |
400 | } | |
e9ae4d4f | 401 | return 0; |
5b3f03f0 HS |
402 | } |
403 | ||
404 | static int poseidon_probe(struct usb_interface *interface, | |
405 | const struct usb_device_id *id) | |
406 | { | |
407 | struct usb_device *udev = interface_to_usbdev(interface); | |
408 | struct poseidon *pd = NULL; | |
409 | int ret = 0; | |
410 | int new_one = 0; | |
411 | ||
412 | /* download firmware */ | |
413 | check_firmware(udev, &ret); | |
414 | if (ret) | |
415 | return 0; | |
416 | ||
417 | /* Do I recovery from the hibernate ? */ | |
418 | pd = find_old_poseidon(udev); | |
419 | if (!pd) { | |
420 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); | |
421 | if (!pd) | |
422 | return -ENOMEM; | |
423 | kref_init(&pd->kref); | |
424 | set_map_flags(pd, udev); | |
425 | new_one = 1; | |
426 | } | |
427 | ||
428 | pd->udev = usb_get_dev(udev); | |
429 | pd->interface = usb_get_intf(interface); | |
430 | usb_set_intfdata(interface, pd); | |
431 | ||
432 | if (new_one) { | |
433 | struct device *dev = &interface->dev; | |
434 | ||
435 | logpm(pd); | |
5b3f03f0 HS |
436 | mutex_init(&pd->lock); |
437 | ||
438 | /* register v4l2 device */ | |
439 | snprintf(pd->v4l2_dev.name, sizeof(pd->v4l2_dev.name), "%s %s", | |
440 | dev->driver->name, dev_name(dev)); | |
441 | ret = v4l2_device_register(NULL, &pd->v4l2_dev); | |
442 | ||
443 | /* register devices in directory /dev */ | |
444 | ret = pd_video_init(pd); | |
445 | poseidon_audio_init(pd); | |
446 | poseidon_fm_init(pd); | |
447 | pd_dvb_usb_device_init(pd); | |
448 | ||
449 | INIT_LIST_HEAD(&pd->device_list); | |
450 | list_add_tail(&pd->device_list, &pd_device_list); | |
451 | } | |
452 | ||
453 | device_init_wakeup(&udev->dev, 1); | |
454 | #ifdef CONFIG_PM | |
2f15744c AS |
455 | pm_runtime_set_autosuspend_delay(&pd->udev->dev, |
456 | 1000 * PM_SUSPEND_DELAY); | |
beb7b04a | 457 | usb_enable_autosuspend(pd->udev); |
5b3f03f0 HS |
458 | |
459 | if (in_hibernation(pd)) { | |
460 | INIT_WORK(&pd->pm_work, hibernation_resume); | |
461 | schedule_work(&pd->pm_work); | |
462 | } | |
463 | #endif | |
464 | return 0; | |
465 | } | |
466 | ||
467 | static void poseidon_disconnect(struct usb_interface *interface) | |
468 | { | |
469 | struct poseidon *pd = get_pd(interface); | |
470 | ||
471 | if (!pd) | |
472 | return; | |
473 | logpm(pd); | |
474 | if (in_hibernation(pd)) | |
475 | return; | |
476 | ||
477 | mutex_lock(&pd->lock); | |
478 | pd->state |= POSEIDON_STATE_DISCONNECT; | |
479 | mutex_unlock(&pd->lock); | |
480 | ||
481 | /* stop urb transferring */ | |
482 | stop_all_video_stream(pd); | |
483 | dvb_stop_streaming(&pd->dvb_data); | |
484 | ||
485 | /*unregister v4l2 device */ | |
486 | v4l2_device_unregister(&pd->v4l2_dev); | |
487 | ||
0edf2e5e AB |
488 | pd_dvb_usb_device_exit(pd); |
489 | poseidon_fm_exit(pd); | |
5b3f03f0 | 490 | |
0edf2e5e AB |
491 | poseidon_audio_free(pd); |
492 | pd_video_exit(pd); | |
5b3f03f0 HS |
493 | |
494 | usb_set_intfdata(interface, NULL); | |
495 | kref_put(&pd->kref, poseidon_delete); | |
496 | } | |
497 | ||
278a58cf | 498 | static struct usb_driver poseidon_driver = { |
5b3f03f0 HS |
499 | .name = "poseidon", |
500 | .probe = poseidon_probe, | |
501 | .disconnect = poseidon_disconnect, | |
502 | .id_table = id_table, | |
503 | #ifdef CONFIG_PM | |
504 | .suspend = poseidon_suspend, | |
505 | .resume = poseidon_resume, | |
506 | #endif | |
507 | .supports_autosuspend = 1, | |
508 | }; | |
509 | ||
510 | static int __init poseidon_init(void) | |
511 | { | |
512 | int ret; | |
513 | ||
514 | ret = usb_register(&poseidon_driver); | |
515 | if (ret) | |
516 | return ret; | |
517 | register_pm_notifier(&pm_notifer); | |
518 | return ret; | |
519 | } | |
520 | ||
521 | static void __exit poseidon_exit(void) | |
522 | { | |
523 | log(); | |
524 | unregister_pm_notifier(&pm_notifer); | |
525 | usb_deregister(&poseidon_driver); | |
526 | } | |
527 | ||
528 | module_init(poseidon_init); | |
529 | module_exit(poseidon_exit); | |
530 | ||
531 | MODULE_AUTHOR("Telegent Systems"); | |
532 | MODULE_DESCRIPTION("For tlg2300-based USB device "); | |
533 | MODULE_LICENSE("GPL"); | |
1990d50b | 534 | MODULE_VERSION("0.0.2"); |