V4L/DVB (12840): tm6000: add support for HVR900H
[pandora-kernel.git] / drivers / staging / tm6000 / tm6000-cards.c
1 /*
2    tm6000-cards.c - driver for TM5600/TM6000 USB video capture devices
3
4    Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation version 2
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/usb.h>
26 #include <linux/version.h>
27 #include <media/v4l2-common.h>
28 #include <media/tuner.h>
29
30 #include "tm6000.h"
31 #include "tm6000-regs.h"
32 #include "tuner-xc2028.h"
33
34 #define TM6000_BOARD_UNKNOWN                    0
35 #define TM5600_BOARD_GENERIC                    1
36 #define TM6000_BOARD_GENERIC                    2
37 #define TM6010_BOARD_GENERIC                    3
38 #define TM5600_BOARD_10MOONS_UT821              4
39 #define TM5600_BOARD_10MOONS_UT330              5
40 #define TM6000_BOARD_ADSTECH_DUAL_TV            6
41 #define TM6000_BOARD_FREECOM_AND_SIMILAR        7
42 #define TM6000_BOARD_ADSTECH_MINI_DUAL_TV       8
43 #define TM6010_BOARD_HAUPPAUGE_900H             9
44
45 #define TM6000_MAXBOARDS        16
46 static unsigned int card[]     = {[0 ... (TM6000_MAXBOARDS - 1)] = UNSET };
47
48 module_param_array(card,  int, NULL, 0444);
49
50 static unsigned long tm6000_devused;
51
52
53 struct tm6000_board {
54         char            *name;
55
56         struct tm6000_capabilities caps;
57
58         enum            tm6000_devtype type;    /* variant of the chipset */
59         int             tuner_type;     /* type of the tuner */
60         int             tuner_addr;     /* tuner address */
61         int             demod_addr;     /* demodulator address */
62         int             gpio_addr_tun_reset;    /* GPIO used for tuner reset */
63 };
64
65 struct tm6000_board tm6000_boards[] = {
66         [TM6000_BOARD_UNKNOWN] = {
67                 .name         = "Unknown tm6000 video grabber",
68                 .caps = {
69                         .has_tuner    = 1,
70                 },
71                 .gpio_addr_tun_reset = TM6000_GPIO_1,
72         },
73         [TM5600_BOARD_GENERIC] = {
74                 .name         = "Generic tm5600 board",
75                 .type         = TM5600,
76                 .tuner_type   = TUNER_XC2028,
77                 .tuner_addr   = 0xc2,
78                 .caps = {
79                         .has_tuner      = 1,
80                 },
81                 .gpio_addr_tun_reset = TM6000_GPIO_1,
82         },
83         [TM6000_BOARD_GENERIC] = {
84                 .name         = "Generic tm6000 board",
85                 .tuner_type   = TUNER_XC2028,
86                 .tuner_addr   = 0xc2,
87                 .caps = {
88                         .has_tuner      = 1,
89                         .has_dvb        = 1,
90                 },
91                 .gpio_addr_tun_reset = TM6000_GPIO_1,
92         },
93         [TM6010_BOARD_GENERIC] = {
94                 .name         = "Generic tm6010 board",
95                 .type         = TM6010,
96                 .tuner_type   = TUNER_XC2028,
97                 .tuner_addr   = 0xc2,
98                 .caps = {
99                         .has_tuner      = 1,
100                         .has_dvb        = 1,
101                 },
102                 .gpio_addr_tun_reset = TM6010_GPIO_4,
103         },
104         [TM5600_BOARD_10MOONS_UT821] = {
105                 .name         = "10Moons UT 821",
106                 .tuner_type   = TUNER_XC2028,
107                 .type         = TM5600,
108                 .tuner_addr   = 0xc2,
109                 .caps = {
110                         .has_tuner    = 1,
111                         .has_eeprom   = 1,
112                 },
113                 .gpio_addr_tun_reset = TM6000_GPIO_1,
114         },
115         [TM5600_BOARD_10MOONS_UT330] = {
116                 .name         = "10Moons UT 330",
117                 .tuner_type   = TUNER_PHILIPS_FQ1216AME_MK4,
118                 .tuner_addr   = 0xc8,
119                 .caps = {
120                         .has_tuner    = 1,
121                         .has_dvb      = 0,
122                         .has_zl10353  = 0,
123                         .has_eeprom   = 1,
124                 },
125         },
126         [TM6000_BOARD_ADSTECH_DUAL_TV] = {
127                 .name         = "ADSTECH Dual TV USB",
128                 .tuner_type   = TUNER_XC2028,
129                 .tuner_addr   = 0xc8,
130                 .caps = {
131                         .has_tuner    = 1,
132                         .has_tda9874  = 1,
133                         .has_dvb      = 1,
134                         .has_zl10353  = 1,
135                         .has_eeprom   = 1,
136                 },
137         },
138         [TM6000_BOARD_FREECOM_AND_SIMILAR] = {
139                 .name         = "Freecom Hybrid Stick / Moka DVB-T Receiver Dual",
140                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
141                 .tuner_addr   = 0xc2,
142                 .demod_addr   = 0x1e,
143                 .caps = {
144                         .has_tuner    = 1,
145                         .has_dvb      = 1,
146                         .has_zl10353  = 1,
147                         .has_eeprom   = 0,
148                         .has_remote   = 1,
149                 },
150                 .gpio_addr_tun_reset = TM6000_GPIO_4,
151         },
152         [TM6000_BOARD_ADSTECH_MINI_DUAL_TV] = {
153                 .name         = "ADSTECH Mini Dual TV USB",
154                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
155                 .tuner_addr   = 0xc8,
156                 .demod_addr   = 0x1e,
157                 .caps = {
158                         .has_tuner    = 1,
159                         .has_dvb      = 1,
160                         .has_zl10353  = 1,
161                         .has_eeprom   = 0,
162                 },
163                 .gpio_addr_tun_reset = TM6000_GPIO_4,
164         },
165         [TM6010_BOARD_HAUPPAUGE_900H] = {
166                 .name         = "Hauppauge HVR-900H",
167                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
168                 .tuner_addr   = 0xc2,
169                 .demod_addr   = 0x1e,
170                 .type         = TM6010,
171                 .caps = {
172                         .has_tuner    = 1,
173                         .has_dvb      = 1,
174                         .has_zl10353  = 1,
175                         .has_eeprom   = 1,
176                 },
177                 .gpio_addr_tun_reset = TM6000_GPIO_2,
178         },
179 };
180
181 /* table of devices that work with this driver */
182 struct usb_device_id tm6000_id_table [] = {
183         { USB_DEVICE(0x6000, 0x0001), .driver_info = TM5600_BOARD_10MOONS_UT821 },
184         { USB_DEVICE(0x6000, 0x0002), .driver_info = TM6010_BOARD_GENERIC },
185         { USB_DEVICE(0x06e1, 0xf332), .driver_info = TM6000_BOARD_ADSTECH_DUAL_TV },
186         { USB_DEVICE(0x14aa, 0x0620), .driver_info = TM6000_BOARD_FREECOM_AND_SIMILAR },
187         { USB_DEVICE(0x06e1, 0xb339), .driver_info = TM6000_BOARD_ADSTECH_MINI_DUAL_TV },
188         { USB_DEVICE(0x2040, 0x6600), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
189         { },
190 };
191
192 static void tm6000_config_tuner (struct tm6000_core *dev)
193 {
194         request_module ("tuner");
195
196         if (dev->tuner_type == TUNER_XC2028) {
197                 struct v4l2_priv_tun_config  xc2028_cfg;
198                 struct xc2028_ctrl           ctl;
199
200                 memset (&ctl,0,sizeof(ctl));
201
202                 ctl.mts   = 1;
203                 ctl.read_not_reliable = 1;
204                 ctl.msleep = 10;
205
206                 xc2028_cfg.tuner = TUNER_XC2028;
207                 xc2028_cfg.priv  = &ctl;
208
209                 switch(dev->model) {
210                 case TM6010_BOARD_HAUPPAUGE_900H:
211                         ctl.fname = "xc3028L-v36.fw";
212                         break;
213                 default:
214                         if (dev->dev_type == TM6010)
215                                 ctl.fname = "xc3028-v27.fw";
216                         else
217                                 ctl.fname = "tm6000-xc3028.fw";
218                 }
219
220                 printk(KERN_INFO "Setting firmware parameters for xc2028\n");
221
222                 tm6000_i2c_call_clients(dev, TUNER_SET_CONFIG, &xc2028_cfg);
223         }
224 }
225
226 static int tm6000_init_dev(struct tm6000_core *dev)
227 {
228         struct v4l2_frequency f;
229         int rc = 0;
230
231         mutex_init(&dev->lock);
232
233         mutex_lock(&dev->lock);
234
235         /* Initializa board-specific data */
236         dev->dev_type   = tm6000_boards[dev->model].type;
237         dev->tuner_type = tm6000_boards[dev->model].tuner_type;
238         dev->tuner_addr = tm6000_boards[dev->model].tuner_addr;
239         dev->tuner_reset_gpio = tm6000_boards[dev->model].gpio_addr_tun_reset;
240
241         dev->demod_addr = tm6000_boards[dev->model].demod_addr;
242
243         dev->caps = tm6000_boards[dev->model].caps;
244
245         /* initialize hardware */
246         rc=tm6000_init (dev);
247         if (rc<0)
248                 goto err;
249
250         /* register i2c bus */
251         rc=tm6000_i2c_register(dev);
252         if (rc<0)
253                 goto err;
254
255         /* register and initialize V4L2 */
256         rc=tm6000_v4l2_register(dev);
257         if (rc<0)
258                 goto err;
259
260         /* Default values for STD and resolutions */
261         dev->width = 720;
262         dev->height = 480;
263         dev->norm = V4L2_STD_PAL_M;
264
265         /* Configure tuner */
266         tm6000_config_tuner (dev);
267
268         /* Set video standard */
269         tm6000_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
270
271         /* Set tuner frequency - also loads firmware on xc2028/xc3028 */
272         f.tuner = 0;
273         f.type = V4L2_TUNER_ANALOG_TV;
274         f.frequency = 3092;     /* 193.25 MHz */
275         dev->freq = f.frequency;
276         tm6000_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
277
278         if(dev->caps.has_dvb) {
279                 dev->dvb = kzalloc(sizeof(*(dev->dvb)), GFP_KERNEL);
280                 if(!dev->dvb) {
281                         rc = -ENOMEM;
282                         goto err;
283                 }
284 #ifdef CONFIG_VIDEO_TM6000_DVB
285                 rc = tm6000_dvb_register(dev);
286                 if(rc < 0) {
287                         kfree(dev->dvb);
288                         dev->dvb = NULL;
289                         goto err;
290                 }
291 #endif
292         }
293 err:
294         mutex_unlock(&dev->lock);
295         return rc;
296 }
297
298 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
299 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
300
301 static void get_max_endpoint (  struct usb_device *usbdev,
302                                 char *msgtype,
303                                 struct usb_host_endpoint *curr_e,
304                                 unsigned int *maxsize,
305                                 struct usb_host_endpoint **ep  )
306 {
307         u16 tmp = le16_to_cpu(curr_e->desc.wMaxPacketSize);
308         unsigned int size = tmp & 0x7ff;
309
310         if (usbdev->speed == USB_SPEED_HIGH)
311                 size = size * hb_mult (tmp);
312
313         if (size>*maxsize) {
314                 *ep = curr_e;
315                 *maxsize = size;
316                 printk("tm6000: %s endpoint: 0x%02x (max size=%u bytes)\n",
317                                         msgtype, curr_e->desc.bEndpointAddress,
318                                         size);
319         }
320 }
321
322 /*
323  * tm6000_usb_probe()
324  * checks for supported devices
325  */
326 static int tm6000_usb_probe(struct usb_interface *interface,
327                             const struct usb_device_id *id)
328 {
329         struct usb_device *usbdev;
330         struct tm6000_core *dev = NULL;
331         int i,rc=0;
332         int nr=0;
333         char *speed;
334
335
336         usbdev=usb_get_dev(interface_to_usbdev(interface));
337
338         /* Selects the proper interface */
339         rc=usb_set_interface(usbdev,0,1);
340         if (rc<0)
341                 goto err;
342
343         /* Check to see next free device and mark as used */
344         nr=find_first_zero_bit(&tm6000_devused,TM6000_MAXBOARDS);
345         if (nr >= TM6000_MAXBOARDS) {
346                 printk ("tm6000: Supports only %i em28xx boards.\n",TM6000_MAXBOARDS);
347                 usb_put_dev(usbdev);
348                 return -ENOMEM;
349         }
350
351         /* Create and initialize dev struct */
352         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
353         if (dev == NULL) {
354                 printk ("tm6000" ": out of memory!\n");
355                 usb_put_dev(usbdev);
356                 return -ENOMEM;
357         }
358         spin_lock_init(&dev->slock);
359
360         /* Increment usage count */
361         tm6000_devused|=1<<nr;
362         snprintf(dev->name, 29, "tm6000 #%d", nr);
363
364         dev->model=id->driver_info;
365         if ((card[nr]>=0) && (card[nr]<ARRAY_SIZE(tm6000_boards))) {
366                 dev->model=card[nr];
367         }
368
369         INIT_LIST_HEAD(&dev->tm6000_corelist);
370         dev->udev= usbdev;
371         dev->devno=nr;
372
373         switch (usbdev->speed) {
374         case USB_SPEED_LOW:
375                 speed = "1.5";
376                 break;
377         case USB_SPEED_UNKNOWN:
378         case USB_SPEED_FULL:
379                 speed = "12";
380                 break;
381         case USB_SPEED_HIGH:
382                 speed = "480";
383                 break;
384         default:
385                 speed = "unknown";
386         }
387
388
389
390         /* Get endpoints */
391         for (i = 0; i < interface->num_altsetting; i++) {
392                 int ep;
393
394                 for (ep = 0; ep < interface->altsetting[i].desc.bNumEndpoints; ep++) {
395                         struct usb_host_endpoint        *e;
396                         int dir_out;
397
398                         e = &interface->altsetting[i].endpoint[ep];
399
400                         dir_out = ((e->desc.bEndpointAddress &
401                                         USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
402
403                         printk("tm6000: alt %d, interface %i, class %i\n",
404                                i,
405                                interface->altsetting[i].desc.bInterfaceNumber,
406                                interface->altsetting[i].desc.bInterfaceClass);
407
408                         switch (e->desc.bmAttributes) {
409                         case USB_ENDPOINT_XFER_BULK:
410                                 if (!dir_out) {
411                                         get_max_endpoint (usbdev, "Bulk IN", e,
412                                                         &dev->max_bulk_in,
413                                                         &dev->bulk_in);
414                                 } else {
415                                         get_max_endpoint (usbdev, "Bulk OUT", e,
416                                                         &dev->max_bulk_out,
417                                                         &dev->bulk_out);
418                                 }
419                                 break;
420                         case USB_ENDPOINT_XFER_ISOC:
421                                 if (!dir_out) {
422                                         get_max_endpoint (usbdev, "ISOC IN", e,
423                                                         &dev->max_isoc_in,
424                                                         &dev->isoc_in);
425                                 } else {
426                                         get_max_endpoint (usbdev, "ISOC OUT", e,
427                                                         &dev->max_isoc_out,
428                                                         &dev->isoc_out);
429                                 }
430                                 break;
431                         }
432                 }
433         }
434
435
436         printk("tm6000: New video device @ %s Mbps (%04x:%04x, ifnum %d)\n",
437                 speed,
438                 le16_to_cpu(dev->udev->descriptor.idVendor),
439                 le16_to_cpu(dev->udev->descriptor.idProduct),
440                 interface->altsetting->desc.bInterfaceNumber);
441
442 /* check if the the device has the iso in endpoint at the correct place */
443         if (!dev->isoc_in) {
444                 printk("tm6000: probing error: no IN ISOC endpoint!\n");
445                 rc= -ENODEV;
446
447                 goto err;
448         }
449
450         /* save our data pointer in this interface device */
451         usb_set_intfdata(interface, dev);
452
453         printk("tm6000: Found %s\n", tm6000_boards[dev->model].name);
454
455         rc=tm6000_init_dev(dev);
456
457         if (rc<0)
458                 goto err;
459
460         return 0;
461
462 err:
463         tm6000_devused&=~(1<<nr);
464         usb_put_dev(usbdev);
465
466         kfree(dev);
467         return rc;
468 }
469
470 /*
471  * tm6000_usb_disconnect()
472  * called when the device gets diconencted
473  * video device will be unregistered on v4l2_close in case it is still open
474  */
475 static void tm6000_usb_disconnect(struct usb_interface *interface)
476 {
477         struct tm6000_core *dev = usb_get_intfdata(interface);
478         usb_set_intfdata(interface, NULL);
479
480         if (!dev)
481                 return;
482
483         printk("tm6000: disconnecting %s\n", dev->name);
484
485         mutex_lock(&dev->lock);
486
487 #ifdef CONFIG_VIDEO_TM6000_DVB
488         if(dev->dvb) {
489                 tm6000_dvb_unregister(dev);
490                 kfree(dev->dvb);
491         }
492 #endif
493
494         tm6000_v4l2_unregister(dev);
495
496         tm6000_i2c_unregister(dev);
497
498 //      wake_up_interruptible_all(&dev->open);
499
500         dev->state |= DEV_DISCONNECTED;
501
502         usb_put_dev(dev->udev);
503
504         mutex_unlock(&dev->lock);
505         kfree(dev);
506 }
507
508 static struct usb_driver tm6000_usb_driver = {
509                 .name = "tm6000",
510                 .probe = tm6000_usb_probe,
511                 .disconnect = tm6000_usb_disconnect,
512                 .id_table = tm6000_id_table,
513 };
514
515 static int __init tm6000_module_init(void)
516 {
517         int result;
518
519         printk(KERN_INFO "tm6000" " v4l2 driver version %d.%d.%d loaded\n",
520                (TM6000_VERSION  >> 16) & 0xff,
521                (TM6000_VERSION  >> 8) & 0xff, TM6000_VERSION  & 0xff);
522
523         /* register this driver with the USB subsystem */
524         result = usb_register(&tm6000_usb_driver);
525         if (result)
526                 printk("tm6000"
527                            " usb_register failed. Error number %d.\n", result);
528
529         return result;
530 }
531
532 static void __exit tm6000_module_exit(void)
533 {
534         /* deregister at USB subsystem */
535         usb_deregister(&tm6000_usb_driver);
536 }
537
538 module_init(tm6000_module_init);
539 module_exit(tm6000_module_exit);
540
541 MODULE_DESCRIPTION("Trident TVMaster TM5600/TM6000 USB2 adapter");
542 MODULE_AUTHOR("Mauro Carvalho Chehab");
543 MODULE_LICENSE("GPL");