Merge branch 'x86-pat-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / media / video / tlg2300 / pd-main.c
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
47 static 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 };
52 MODULE_DEVICE_TABLE(usb, id_table);
53
54 int debug_mode;
55 module_param(debug_mode, int, 0644);
56 MODULE_PARM_DESC(debug_mode, "0 = disable, 1 = enable, 2 = verbose");
57
58 const char *firmware_name = "tlg2300_firmware.bin";
59 struct usb_driver poseidon_driver;
60 static LIST_HEAD(pd_device_list);
61
62 /*
63  * send set request to USB firmware.
64  */
65 s32 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  */
105 s32 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
143 static 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
175 static struct notifier_block pm_notifer = {
176         .notifier_call = pm_notifier_block,
177 };
178
179 int 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
192 void poseidon_delete(struct kref *kref)
193 {
194         struct poseidon *pd = container_of(kref, struct poseidon, kref);
195
196         if (!pd)
197                 return;
198         list_del_init(&pd->device_list);
199
200         pd_dvb_usb_device_cleanup(pd);
201         /* clean_audio_data(&pd->audio_data);*/
202
203         if (pd->udev) {
204                 usb_put_dev(pd->udev);
205                 pd->udev = NULL;
206         }
207         if (pd->interface) {
208                 usb_put_intf(pd->interface);
209                 pd->interface = NULL;
210         }
211         kfree(pd);
212         log();
213 }
214
215 static int firmware_download(struct usb_device *udev)
216 {
217         int ret = 0, actual_length;
218         const struct firmware *fw = NULL;
219         void *fwbuf = NULL;
220         size_t fwlength = 0, offset;
221         size_t max_packet_size;
222
223         ret = request_firmware(&fw, firmware_name, &udev->dev);
224         if (ret) {
225                 log("download err : %d", ret);
226                 return ret;
227         }
228
229         fwlength = fw->size;
230
231         fwbuf = kzalloc(fwlength, GFP_KERNEL);
232         if (!fwbuf) {
233                 ret = -ENOMEM;
234                 goto out;
235         }
236         memcpy(fwbuf, fw->data, fwlength);
237
238         max_packet_size = udev->ep_out[0x1]->desc.wMaxPacketSize;
239         log("\t\t download size : %d", (int)max_packet_size);
240
241         for (offset = 0; offset < fwlength; offset += max_packet_size) {
242                 actual_length = 0;
243                 ret = usb_bulk_msg(udev,
244                                 usb_sndbulkpipe(udev, 0x01), /* ep 1 */
245                                 fwbuf + offset,
246                                 min(max_packet_size, fwlength - offset),
247                                 &actual_length,
248                                 HZ * 10);
249                 if (ret)
250                         break;
251         }
252         kfree(fwbuf);
253 out:
254         release_firmware(fw);
255         return ret;
256 }
257
258 static inline struct poseidon *get_pd(struct usb_interface *intf)
259 {
260         return usb_get_intfdata(intf);
261 }
262
263 #ifdef CONFIG_PM
264 /* one-to-one map : poseidon{} <----> usb_device{}'s port */
265 static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev)
266 {
267         pd->portnum = udev->portnum;
268 }
269
270 static inline int get_autopm_ref(struct poseidon *pd)
271 {
272         return  pd->video_data.users + pd->vbi_data.users + pd->audio.users
273                 + atomic_read(&pd->dvb_data.users) + pd->radio_data.users;
274 }
275
276 /* fixup something for poseidon */
277 static inline struct poseidon *fixup(struct poseidon *pd)
278 {
279         int count;
280
281         /* old udev and interface have gone, so put back reference . */
282         count = get_autopm_ref(pd);
283         log("count : %d, ref count : %d", count, get_pm_count(pd));
284         while (count--)
285                 usb_autopm_put_interface(pd->interface);
286         /*usb_autopm_set_interface(pd->interface); */
287
288         usb_put_dev(pd->udev);
289         usb_put_intf(pd->interface);
290         log("event : %d\n", pd->msg.event);
291         return pd;
292 }
293
294 static struct poseidon *find_old_poseidon(struct usb_device *udev)
295 {
296         struct poseidon *pd;
297
298         list_for_each_entry(pd, &pd_device_list, device_list) {
299                 if (pd->portnum == udev->portnum && in_hibernation(pd))
300                         return fixup(pd);
301         }
302         return NULL;
303 }
304
305 /* Is the card working now ? */
306 static inline int is_working(struct poseidon *pd)
307 {
308         return get_pm_count(pd) > 0;
309 }
310
311 static int poseidon_suspend(struct usb_interface *intf, pm_message_t msg)
312 {
313         struct poseidon *pd = get_pd(intf);
314
315         if (!pd)
316                 return 0;
317         if (!is_working(pd)) {
318                 if (get_pm_count(pd) <= 0 && !in_hibernation(pd)) {
319                         pd->msg.event = PM_EVENT_AUTO_SUSPEND;
320                         pd->pm_resume = NULL; /*  a good guard */
321                         printk(KERN_DEBUG "\n\t+ TLG2300 auto suspend +\n\n");
322                 }
323                 return 0;
324         }
325         pd->msg = msg; /* save it here */
326         logpm(pd);
327         return pd->pm_suspend ? pd->pm_suspend(pd) : 0;
328 }
329
330 static int poseidon_resume(struct usb_interface *intf)
331 {
332         struct poseidon *pd = get_pd(intf);
333
334         if (!pd)
335                 return 0;
336         printk(KERN_DEBUG "\n\t ++ TLG2300 resume ++\n\n");
337
338         if (!is_working(pd)) {
339                 if (PM_EVENT_AUTO_SUSPEND == pd->msg.event)
340                         pd->msg = PMSG_ON;
341                 return 0;
342         }
343         if (in_hibernation(pd)) {
344                 logpm(pd);
345                 return 0;
346         }
347         logpm(pd);
348         return pd->pm_resume ? pd->pm_resume(pd) : 0;
349 }
350
351 static void hibernation_resume(struct work_struct *w)
352 {
353         struct poseidon *pd = container_of(w, struct poseidon, pm_work);
354         int count;
355
356         pd->msg.event = 0; /* clear it here */
357         pd->state &= ~POSEIDON_STATE_DISCONNECT;
358
359         /* set the new interface's reference */
360         count = get_autopm_ref(pd);
361         while (count--)
362                 usb_autopm_get_interface(pd->interface);
363
364         /* resume the context */
365         logpm(pd);
366         if (pd->pm_resume)
367                 pd->pm_resume(pd);
368 }
369 #else /* CONFIG_PM is not enabled: */
370 static inline struct poseidon *find_old_poseidon(struct usb_device *udev)
371 {
372         return NULL;
373 }
374
375 static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev)
376 {
377 }
378 #endif
379
380 static bool check_firmware(struct usb_device *udev, int *down_firmware)
381 {
382         void *buf;
383         int ret;
384         struct cmd_firmware_vers_s *cmd_firm;
385
386         buf = kzalloc(sizeof(*cmd_firm) + sizeof(u32), GFP_KERNEL);
387         if (!buf)
388                 return -ENOMEM;
389         ret = usb_control_msg(udev,
390                          usb_rcvctrlpipe(udev, 0),
391                          REQ_GET_CMD | GET_FW_ID,
392                          USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
393                          0,
394                          0,
395                          buf,
396                          sizeof(*cmd_firm) + sizeof(u32),
397                          USB_CTRL_GET_TIMEOUT);
398         kfree(buf);
399
400         if (ret < 0) {
401                 *down_firmware = 1;
402                 return firmware_download(udev);
403         }
404         return ret;
405 }
406
407 static int poseidon_probe(struct usb_interface *interface,
408                                 const struct usb_device_id *id)
409 {
410         struct usb_device *udev = interface_to_usbdev(interface);
411         struct poseidon *pd = NULL;
412         int ret = 0;
413         int new_one = 0;
414
415         /* download firmware */
416         check_firmware(udev, &ret);
417         if (ret)
418                 return 0;
419
420         /* Do I recovery from the hibernate ? */
421         pd = find_old_poseidon(udev);
422         if (!pd) {
423                 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
424                 if (!pd)
425                         return -ENOMEM;
426                 kref_init(&pd->kref);
427                 set_map_flags(pd, udev);
428                 new_one = 1;
429         }
430
431         pd->udev        = usb_get_dev(udev);
432         pd->interface   = usb_get_intf(interface);
433         usb_set_intfdata(interface, pd);
434
435         if (new_one) {
436                 struct device *dev = &interface->dev;
437
438                 logpm(pd);
439                 mutex_init(&pd->lock);
440
441                 /* register v4l2 device */
442                 snprintf(pd->v4l2_dev.name, sizeof(pd->v4l2_dev.name), "%s %s",
443                         dev->driver->name, dev_name(dev));
444                 ret = v4l2_device_register(NULL, &pd->v4l2_dev);
445
446                 /* register devices in directory /dev */
447                 ret = pd_video_init(pd);
448                 poseidon_audio_init(pd);
449                 poseidon_fm_init(pd);
450                 pd_dvb_usb_device_init(pd);
451
452                 INIT_LIST_HEAD(&pd->device_list);
453                 list_add_tail(&pd->device_list, &pd_device_list);
454         }
455
456         device_init_wakeup(&udev->dev, 1);
457 #ifdef CONFIG_PM
458         pd->udev->autosuspend_disabled = 0;
459         pd->udev->autosuspend_delay = HZ * PM_SUSPEND_DELAY;
460
461         if (in_hibernation(pd)) {
462                 INIT_WORK(&pd->pm_work, hibernation_resume);
463                 schedule_work(&pd->pm_work);
464         }
465 #endif
466         return 0;
467 }
468
469 static void poseidon_disconnect(struct usb_interface *interface)
470 {
471         struct poseidon *pd = get_pd(interface);
472
473         if (!pd)
474                 return;
475         logpm(pd);
476         if (in_hibernation(pd))
477                 return;
478
479         mutex_lock(&pd->lock);
480         pd->state |= POSEIDON_STATE_DISCONNECT;
481         mutex_unlock(&pd->lock);
482
483         /* stop urb transferring */
484         stop_all_video_stream(pd);
485         dvb_stop_streaming(&pd->dvb_data);
486
487         /*unregister v4l2 device */
488         v4l2_device_unregister(&pd->v4l2_dev);
489
490         lock_kernel();
491         {
492                 pd_dvb_usb_device_exit(pd);
493                 poseidon_fm_exit(pd);
494
495                 poseidon_audio_free(pd);
496                 pd_video_exit(pd);
497         }
498         unlock_kernel();
499
500         usb_set_intfdata(interface, NULL);
501         kref_put(&pd->kref, poseidon_delete);
502 }
503
504 struct usb_driver poseidon_driver = {
505         .name           = "poseidon",
506         .probe          = poseidon_probe,
507         .disconnect     = poseidon_disconnect,
508         .id_table       = id_table,
509 #ifdef CONFIG_PM
510         .suspend        = poseidon_suspend,
511         .resume         = poseidon_resume,
512 #endif
513         .supports_autosuspend = 1,
514 };
515
516 static int __init poseidon_init(void)
517 {
518         int ret;
519
520         ret = usb_register(&poseidon_driver);
521         if (ret)
522                 return ret;
523         register_pm_notifier(&pm_notifer);
524         return ret;
525 }
526
527 static void __exit poseidon_exit(void)
528 {
529         log();
530         unregister_pm_notifier(&pm_notifer);
531         usb_deregister(&poseidon_driver);
532 }
533
534 module_init(poseidon_init);
535 module_exit(poseidon_exit);
536
537 MODULE_AUTHOR("Telegent Systems");
538 MODULE_DESCRIPTION("For tlg2300-based USB device ");
539 MODULE_LICENSE("GPL");