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