2 * device driver for Telegent tlg2300 based TV cards
5 * Kang Yong <kangyong@telegent.com>
6 * Zhang Xiaobing <xbzhang@telegent.com>
7 * Huang Shijie <zyziii@telegent.com> or <shijie8@gmail.com>
9 * (c) 2009 Telegent Systems
10 * (c) 2010 Telegent Systems
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.
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.
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.
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>
39 #include <linux/smp_lock.h>
41 #include "vendorcmds.h"
42 #include "pd-common.h"
44 #define VENDOR_ID 0x1B24
45 #define PRODUCT_ID 0x4001
46 static struct usb_device_id id_table[] = {
47 { USB_DEVICE_AND_INTERFACE_INFO(VENDOR_ID, PRODUCT_ID, 255, 1, 0) },
48 { USB_DEVICE_AND_INTERFACE_INFO(VENDOR_ID, PRODUCT_ID, 255, 1, 1) },
51 MODULE_DEVICE_TABLE(usb, id_table);
54 module_param(debug_mode, int, 0644);
55 MODULE_PARM_DESC(debug_mode, "0 = disable, 1 = enable, 2 = verbose");
57 static const char *firmware_name = "tlg2300_firmware.bin";
58 static struct usb_driver poseidon_driver;
59 static LIST_HEAD(pd_device_list);
62 * send set request to USB firmware.
64 s32 send_set_req(struct poseidon *pd, u8 cmdid, s32 param, s32 *cmd_status)
68 u16 lower_16, upper_16;
70 if (pd->state & POSEIDON_STATE_DISCONNECT)
76 upper_16 = lower_16 = 0;
78 /* send 32 bit param as two 16 bit param,little endian */
79 lower_16 = (unsigned short)(param & 0xffff);
80 upper_16 = (unsigned short)((param >> 16) & 0xffff);
82 ret = usb_control_msg(pd->udev,
83 usb_rcvctrlpipe(pd->udev, 0),
85 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
90 USB_CTRL_GET_TIMEOUT);
95 /* 1st 4 bytes into cmd_status */
96 memcpy((char *)cmd_status, &(data[0]), sizeof(*cmd_status));
102 * send get request to Poseidon firmware.
104 s32 send_get_req(struct poseidon *pd, u8 cmdid, s32 param,
105 void *buf, s32 *cmd_status, s32 datalen)
109 u16 lower_16, upper_16;
111 if (pd->state & POSEIDON_STATE_DISCONNECT)
116 upper_16 = lower_16 = 0;
118 /*send 32 bit param as two 16 bit param, little endian */
119 lower_16 = (unsigned short)(param & 0xffff);
120 upper_16 = (unsigned short)((param >> 16) & 0xffff);
122 ret = usb_control_msg(pd->udev,
123 usb_rcvctrlpipe(pd->udev, 0),
125 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
129 (datalen + sizeof(*cmd_status)),
130 USB_CTRL_GET_TIMEOUT);
135 /* 1st 4 bytes into cmd_status, remaining data into cmd_data */
136 memcpy((char *)cmd_status, &data[0], sizeof(*cmd_status));
137 memcpy((char *)buf, &data[sizeof(*cmd_status)], datalen);
142 static int pm_notifier_block(struct notifier_block *nb,
143 unsigned long event, void *dummy)
145 struct poseidon *pd = NULL;
146 struct list_head *node, *next;
149 case PM_POST_HIBERNATION:
150 list_for_each_safe(node, next, &pd_device_list) {
151 struct usb_device *udev;
152 struct usb_interface *iface;
155 pd = container_of(node, struct poseidon, device_list);
157 iface = pd->interface;
159 /* It will cause the system to reload the firmware */
160 rc = usb_lock_device_for_reset(udev, iface);
162 usb_reset_device(udev);
163 usb_unlock_device(udev);
170 log("event :%ld\n", event);
174 static struct notifier_block pm_notifer = {
175 .notifier_call = pm_notifier_block,
178 int set_tuner_mode(struct poseidon *pd, unsigned char mode)
182 if (pd->state & POSEIDON_STATE_DISCONNECT)
185 ret = send_set_req(pd, TUNE_MODE_SELECT, mode, &cmd_status);
186 if (ret || cmd_status)
191 void poseidon_delete(struct kref *kref)
193 struct poseidon *pd = container_of(kref, struct poseidon, kref);
197 list_del_init(&pd->device_list);
199 pd_dvb_usb_device_cleanup(pd);
200 /* clean_audio_data(&pd->audio_data);*/
203 usb_put_dev(pd->udev);
207 usb_put_intf(pd->interface);
208 pd->interface = NULL;
214 static int firmware_download(struct usb_device *udev)
216 int ret = 0, actual_length;
217 const struct firmware *fw = NULL;
219 size_t fwlength = 0, offset;
220 size_t max_packet_size;
222 ret = request_firmware(&fw, firmware_name, &udev->dev);
224 log("download err : %d", ret);
230 fwbuf = kzalloc(fwlength, GFP_KERNEL);
235 memcpy(fwbuf, fw->data, fwlength);
237 max_packet_size = udev->ep_out[0x1]->desc.wMaxPacketSize;
238 log("\t\t download size : %d", (int)max_packet_size);
240 for (offset = 0; offset < fwlength; offset += max_packet_size) {
242 ret = usb_bulk_msg(udev,
243 usb_sndbulkpipe(udev, 0x01), /* ep 1 */
245 min(max_packet_size, fwlength - offset),
253 release_firmware(fw);
257 static inline struct poseidon *get_pd(struct usb_interface *intf)
259 return usb_get_intfdata(intf);
263 /* one-to-one map : poseidon{} <----> usb_device{}'s port */
264 static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev)
266 pd->portnum = udev->portnum;
269 static inline int get_autopm_ref(struct poseidon *pd)
271 return pd->video_data.users + pd->vbi_data.users + pd->audio.users
272 + atomic_read(&pd->dvb_data.users) + pd->radio_data.users;
275 /* fixup something for poseidon */
276 static inline struct poseidon *fixup(struct poseidon *pd)
280 /* old udev and interface have gone, so put back reference . */
281 count = get_autopm_ref(pd);
282 log("count : %d, ref count : %d", count, get_pm_count(pd));
284 usb_autopm_put_interface(pd->interface);
285 /*usb_autopm_set_interface(pd->interface); */
287 usb_put_dev(pd->udev);
288 usb_put_intf(pd->interface);
289 log("event : %d\n", pd->msg.event);
293 static struct poseidon *find_old_poseidon(struct usb_device *udev)
297 list_for_each_entry(pd, &pd_device_list, device_list) {
298 if (pd->portnum == udev->portnum && in_hibernation(pd))
304 /* Is the card working now ? */
305 static inline int is_working(struct poseidon *pd)
307 return get_pm_count(pd) > 0;
310 static int poseidon_suspend(struct usb_interface *intf, pm_message_t msg)
312 struct poseidon *pd = get_pd(intf);
316 if (!is_working(pd)) {
317 if (get_pm_count(pd) <= 0 && !in_hibernation(pd)) {
318 pd->msg.event = PM_EVENT_AUTO_SUSPEND;
319 pd->pm_resume = NULL; /* a good guard */
320 printk(KERN_DEBUG "\n\t+ TLG2300 auto suspend +\n\n");
324 pd->msg = msg; /* save it here */
326 return pd->pm_suspend ? pd->pm_suspend(pd) : 0;
329 static int poseidon_resume(struct usb_interface *intf)
331 struct poseidon *pd = get_pd(intf);
335 printk(KERN_DEBUG "\n\t ++ TLG2300 resume ++\n\n");
337 if (!is_working(pd)) {
338 if (PM_EVENT_AUTO_SUSPEND == pd->msg.event)
342 if (in_hibernation(pd)) {
347 return pd->pm_resume ? pd->pm_resume(pd) : 0;
350 static void hibernation_resume(struct work_struct *w)
352 struct poseidon *pd = container_of(w, struct poseidon, pm_work);
355 pd->msg.event = 0; /* clear it here */
356 pd->state &= ~POSEIDON_STATE_DISCONNECT;
358 /* set the new interface's reference */
359 count = get_autopm_ref(pd);
361 usb_autopm_get_interface(pd->interface);
363 /* resume the context */
368 #else /* CONFIG_PM is not enabled: */
369 static inline struct poseidon *find_old_poseidon(struct usb_device *udev)
374 static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev)
379 static bool check_firmware(struct usb_device *udev, int *down_firmware)
383 struct cmd_firmware_vers_s *cmd_firm;
385 buf = kzalloc(sizeof(*cmd_firm) + sizeof(u32), GFP_KERNEL);
388 ret = usb_control_msg(udev,
389 usb_rcvctrlpipe(udev, 0),
390 REQ_GET_CMD | GET_FW_ID,
391 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
395 sizeof(*cmd_firm) + sizeof(u32),
396 USB_CTRL_GET_TIMEOUT);
401 return firmware_download(udev);
406 static int poseidon_probe(struct usb_interface *interface,
407 const struct usb_device_id *id)
409 struct usb_device *udev = interface_to_usbdev(interface);
410 struct poseidon *pd = NULL;
414 /* download firmware */
415 check_firmware(udev, &ret);
419 /* Do I recovery from the hibernate ? */
420 pd = find_old_poseidon(udev);
422 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
425 kref_init(&pd->kref);
426 set_map_flags(pd, udev);
430 pd->udev = usb_get_dev(udev);
431 pd->interface = usb_get_intf(interface);
432 usb_set_intfdata(interface, pd);
435 struct device *dev = &interface->dev;
438 mutex_init(&pd->lock);
440 /* register v4l2 device */
441 snprintf(pd->v4l2_dev.name, sizeof(pd->v4l2_dev.name), "%s %s",
442 dev->driver->name, dev_name(dev));
443 ret = v4l2_device_register(NULL, &pd->v4l2_dev);
445 /* register devices in directory /dev */
446 ret = pd_video_init(pd);
447 poseidon_audio_init(pd);
448 poseidon_fm_init(pd);
449 pd_dvb_usb_device_init(pd);
451 INIT_LIST_HEAD(&pd->device_list);
452 list_add_tail(&pd->device_list, &pd_device_list);
455 device_init_wakeup(&udev->dev, 1);
457 pd->udev->autosuspend_delay = HZ * PM_SUSPEND_DELAY;
458 usb_enable_autosuspend(pd->udev);
460 if (in_hibernation(pd)) {
461 INIT_WORK(&pd->pm_work, hibernation_resume);
462 schedule_work(&pd->pm_work);
468 static void poseidon_disconnect(struct usb_interface *interface)
470 struct poseidon *pd = get_pd(interface);
475 if (in_hibernation(pd))
478 mutex_lock(&pd->lock);
479 pd->state |= POSEIDON_STATE_DISCONNECT;
480 mutex_unlock(&pd->lock);
482 /* stop urb transferring */
483 stop_all_video_stream(pd);
484 dvb_stop_streaming(&pd->dvb_data);
486 /*unregister v4l2 device */
487 v4l2_device_unregister(&pd->v4l2_dev);
491 pd_dvb_usb_device_exit(pd);
492 poseidon_fm_exit(pd);
494 poseidon_audio_free(pd);
499 usb_set_intfdata(interface, NULL);
500 kref_put(&pd->kref, poseidon_delete);
503 static struct usb_driver poseidon_driver = {
505 .probe = poseidon_probe,
506 .disconnect = poseidon_disconnect,
507 .id_table = id_table,
509 .suspend = poseidon_suspend,
510 .resume = poseidon_resume,
512 .supports_autosuspend = 1,
515 static int __init poseidon_init(void)
519 ret = usb_register(&poseidon_driver);
522 register_pm_notifier(&pm_notifer);
526 static void __exit poseidon_exit(void)
529 unregister_pm_notifier(&pm_notifer);
530 usb_deregister(&poseidon_driver);
533 module_init(poseidon_init);
534 module_exit(poseidon_exit);
536 MODULE_AUTHOR("Telegent Systems");
537 MODULE_DESCRIPTION("For tlg2300-based USB device ");
538 MODULE_LICENSE("GPL");