2 tm6000-cards.c - driver for TM5600/TM6000 USB video capture devices
4 Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
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
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.
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.
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 #include <media/tvaudio.h>
30 #include <media/i2c-addr.h>
33 #include "tm6000-regs.h"
34 #include "tuner-xc2028.h"
37 #define TM6000_BOARD_UNKNOWN 0
38 #define TM5600_BOARD_GENERIC 1
39 #define TM6000_BOARD_GENERIC 2
40 #define TM6010_BOARD_GENERIC 3
41 #define TM5600_BOARD_10MOONS_UT821 4
42 #define TM5600_BOARD_10MOONS_UT330 5
43 #define TM6000_BOARD_ADSTECH_DUAL_TV 6
44 #define TM6000_BOARD_FREECOM_AND_SIMILAR 7
45 #define TM6000_BOARD_ADSTECH_MINI_DUAL_TV 8
46 #define TM6010_BOARD_HAUPPAUGE_900H 9
47 #define TM6010_BOARD_BEHOLD_WANDER 10
48 #define TM6010_BOARD_BEHOLD_VOYAGER 11
50 #define TM6000_MAXBOARDS 16
51 static unsigned int card[] = {[0 ... (TM6000_MAXBOARDS - 1)] = UNSET };
53 module_param_array(card, int, NULL, 0444);
55 static unsigned long tm6000_devused;
61 struct tm6000_capabilities caps;
63 enum tm6000_devtype type; /* variant of the chipset */
64 int tuner_type; /* type of the tuner */
65 int tuner_addr; /* tuner address */
66 int demod_addr; /* demodulator address */
67 int gpio_addr_tun_reset; /* GPIO used for tuner reset */
70 struct tm6000_board tm6000_boards[] = {
71 [TM6000_BOARD_UNKNOWN] = {
72 .name = "Unknown tm6000 video grabber",
76 .gpio_addr_tun_reset = TM6000_GPIO_1,
78 [TM5600_BOARD_GENERIC] = {
79 .name = "Generic tm5600 board",
81 .tuner_type = TUNER_XC2028,
82 .tuner_addr = 0xc2 >> 1,
86 .gpio_addr_tun_reset = TM6000_GPIO_1,
88 [TM6000_BOARD_GENERIC] = {
89 .name = "Generic tm6000 board",
90 .tuner_type = TUNER_XC2028,
91 .tuner_addr = 0xc2 >> 1,
96 .gpio_addr_tun_reset = TM6000_GPIO_1,
98 [TM6010_BOARD_GENERIC] = {
99 .name = "Generic tm6010 board",
101 .tuner_type = TUNER_XC2028,
102 .tuner_addr = 0xc2 >> 1,
107 .gpio_addr_tun_reset = TM6010_GPIO_4,
109 [TM5600_BOARD_10MOONS_UT821] = {
110 .name = "10Moons UT 821",
111 .tuner_type = TUNER_XC2028,
113 .tuner_addr = 0xc2 >> 1,
118 .gpio_addr_tun_reset = TM6000_GPIO_1,
120 [TM5600_BOARD_10MOONS_UT330] = {
121 .name = "10Moons UT 330",
122 .tuner_type = TUNER_PHILIPS_FQ1216AME_MK4,
123 .tuner_addr = 0xc8 >> 1,
131 [TM6000_BOARD_ADSTECH_DUAL_TV] = {
132 .name = "ADSTECH Dual TV USB",
133 .tuner_type = TUNER_XC2028,
134 .tuner_addr = 0xc8 >> 1,
143 [TM6000_BOARD_FREECOM_AND_SIMILAR] = {
144 .name = "Freecom Hybrid Stick / Moka DVB-T Receiver Dual",
145 .tuner_type = TUNER_XC2028, /* has a XC3028 */
146 .tuner_addr = 0xc2 >> 1,
147 .demod_addr = 0x1e >> 1,
155 .gpio_addr_tun_reset = TM6000_GPIO_4,
157 [TM6000_BOARD_ADSTECH_MINI_DUAL_TV] = {
158 .name = "ADSTECH Mini Dual TV USB",
159 .tuner_type = TUNER_XC2028, /* has a XC3028 */
160 .tuner_addr = 0xc8 >> 1,
161 .demod_addr = 0x1e >> 1,
168 .gpio_addr_tun_reset = TM6000_GPIO_4,
170 [TM6010_BOARD_HAUPPAUGE_900H] = {
171 .name = "Hauppauge HVR-900H",
172 .tuner_type = TUNER_XC2028, /* has a XC3028 */
173 .tuner_addr = 0xc2 >> 1,
174 .demod_addr = 0x1e >> 1,
182 .gpio_addr_tun_reset = TM6000_GPIO_2,
184 [TM6010_BOARD_BEHOLD_WANDER] = {
185 .name = "Beholder Wander DVB-T/TV/FM USB2.0",
186 .tuner_type = TUNER_XC5000,
187 .tuner_addr = 0xc2 >> 1,
188 .demod_addr = 0x1e >> 1,
197 .gpio_addr_tun_reset = TM6000_GPIO_2,
199 [TM6010_BOARD_BEHOLD_VOYAGER] = {
200 .name = "Beholder Voyager TV/FM USB2.0",
201 .tuner_type = TUNER_XC5000,
202 .tuner_addr = 0xc2 >> 1,
211 .gpio_addr_tun_reset = TM6000_GPIO_2,
216 /* table of devices that work with this driver */
217 struct usb_device_id tm6000_id_table [] = {
218 { USB_DEVICE(0x6000, 0x0001), .driver_info = TM5600_BOARD_10MOONS_UT821 },
219 { USB_DEVICE(0x6000, 0x0002), .driver_info = TM6010_BOARD_GENERIC },
220 { USB_DEVICE(0x06e1, 0xf332), .driver_info = TM6000_BOARD_ADSTECH_DUAL_TV },
221 { USB_DEVICE(0x14aa, 0x0620), .driver_info = TM6000_BOARD_FREECOM_AND_SIMILAR },
222 { USB_DEVICE(0x06e1, 0xb339), .driver_info = TM6000_BOARD_ADSTECH_MINI_DUAL_TV },
223 { USB_DEVICE(0x2040, 0x6600), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
224 { USB_DEVICE(0x6000, 0xdec0), .driver_info = TM6010_BOARD_BEHOLD_WANDER },
225 { USB_DEVICE(0x6000, 0xdec1), .driver_info = TM6010_BOARD_BEHOLD_VOYAGER },
229 /* Tuner callback to provide the proper gpio changes needed for xc2028 */
231 static int tm6000_tuner_callback(void *ptr, int component, int command, int arg)
234 struct tm6000_core *dev = ptr;
236 if (dev->tuner_type!=TUNER_XC2028)
240 case XC2028_RESET_CLK:
241 tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT,
244 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
249 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
252 case XC2028_TUNER_RESET:
253 /* Reset codes during load firmware */
256 tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
257 dev->tuner_reset_gpio, 0x00);
259 tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
260 dev->tuner_reset_gpio, 0x01);
264 tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT,
270 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
275 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
284 static void tm6000_config_tuner (struct tm6000_core *dev)
286 struct tuner_setup tun_setup;
288 /* Load tuner module */
289 v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
290 "tuner", "tuner",dev->tuner_addr, NULL);
292 memset(&tun_setup, 0, sizeof(tun_setup));
293 tun_setup.type = dev->tuner_type;
294 tun_setup.addr = dev->tuner_addr;
295 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
296 tun_setup.tuner_callback = tm6000_tuner_callback;
298 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_type_addr, &tun_setup);
300 if (dev->tuner_type == TUNER_XC2028) {
301 struct v4l2_priv_tun_config xc2028_cfg;
302 struct xc2028_ctrl ctl;
304 memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
305 memset (&ctl,0,sizeof(ctl));
308 ctl.read_not_reliable = 1;
311 xc2028_cfg.tuner = TUNER_XC2028;
312 xc2028_cfg.priv = &ctl;
315 case TM6010_BOARD_HAUPPAUGE_900H:
316 ctl.fname = "xc3028L-v36.fw";
319 if (dev->dev_type == TM6010)
320 ctl.fname = "xc3028-v27.fw";
322 ctl.fname = "tm6000-xc3028.fw";
325 printk(KERN_INFO "Setting firmware parameters for xc2028\n");
327 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_config,
332 static int tm6000_init_dev(struct tm6000_core *dev)
334 struct v4l2_frequency f;
337 mutex_init(&dev->lock);
339 mutex_lock(&dev->lock);
341 /* Initializa board-specific data */
342 dev->dev_type = tm6000_boards[dev->model].type;
343 dev->tuner_type = tm6000_boards[dev->model].tuner_type;
344 dev->tuner_addr = tm6000_boards[dev->model].tuner_addr;
345 dev->tuner_reset_gpio = tm6000_boards[dev->model].gpio_addr_tun_reset;
347 dev->demod_addr = tm6000_boards[dev->model].demod_addr;
349 dev->caps = tm6000_boards[dev->model].caps;
351 /* initialize hardware */
352 rc=tm6000_init (dev);
356 rc = v4l2_device_register(&dev->udev->dev, &dev->v4l2_dev);
360 /* register i2c bus */
361 rc=tm6000_i2c_register(dev);
365 /* register and initialize V4L2 */
366 rc=tm6000_v4l2_register(dev);
370 /* Default values for STD and resolutions */
373 dev->norm = V4L2_STD_PAL_M;
375 /* Configure tuner */
376 tm6000_config_tuner (dev);
378 /* Set video standard */
379 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
381 /* Set tuner frequency - also loads firmware on xc2028/xc3028 */
383 f.type = V4L2_TUNER_ANALOG_TV;
384 f.frequency = 3092; /* 193.25 MHz */
385 dev->freq = f.frequency;
386 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
388 if (dev->caps.has_tda9874)
389 v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
390 "tvaudio", "tvaudio", I2C_ADDR_TDA9874, NULL);
392 if(dev->caps.has_dvb) {
393 dev->dvb = kzalloc(sizeof(*(dev->dvb)), GFP_KERNEL);
398 #ifdef CONFIG_VIDEO_TM6000_DVB
399 rc = tm6000_dvb_register(dev);
409 v4l2_device_unregister(&dev->v4l2_dev);
412 mutex_unlock(&dev->lock);
416 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
417 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
419 static void get_max_endpoint ( struct usb_device *usbdev,
421 struct usb_host_endpoint *curr_e,
422 unsigned int *maxsize,
423 struct usb_host_endpoint **ep )
425 u16 tmp = le16_to_cpu(curr_e->desc.wMaxPacketSize);
426 unsigned int size = tmp & 0x7ff;
428 if (usbdev->speed == USB_SPEED_HIGH)
429 size = size * hb_mult (tmp);
434 printk("tm6000: %s endpoint: 0x%02x (max size=%u bytes)\n",
435 msgtype, curr_e->desc.bEndpointAddress,
442 * checks for supported devices
444 static int tm6000_usb_probe(struct usb_interface *interface,
445 const struct usb_device_id *id)
447 struct usb_device *usbdev;
448 struct tm6000_core *dev = NULL;
454 usbdev=usb_get_dev(interface_to_usbdev(interface));
456 /* Selects the proper interface */
457 rc=usb_set_interface(usbdev,0,1);
461 /* Check to see next free device and mark as used */
462 nr=find_first_zero_bit(&tm6000_devused,TM6000_MAXBOARDS);
463 if (nr >= TM6000_MAXBOARDS) {
464 printk ("tm6000: Supports only %i em28xx boards.\n",TM6000_MAXBOARDS);
469 /* Create and initialize dev struct */
470 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
472 printk ("tm6000" ": out of memory!\n");
476 spin_lock_init(&dev->slock);
478 /* Increment usage count */
479 tm6000_devused|=1<<nr;
480 snprintf(dev->name, 29, "tm6000 #%d", nr);
482 dev->model=id->driver_info;
483 if ((card[nr]>=0) && (card[nr]<ARRAY_SIZE(tm6000_boards))) {
487 INIT_LIST_HEAD(&dev->tm6000_corelist);
491 switch (usbdev->speed) {
495 case USB_SPEED_UNKNOWN:
509 for (i = 0; i < interface->num_altsetting; i++) {
512 for (ep = 0; ep < interface->altsetting[i].desc.bNumEndpoints; ep++) {
513 struct usb_host_endpoint *e;
516 e = &interface->altsetting[i].endpoint[ep];
518 dir_out = ((e->desc.bEndpointAddress &
519 USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
521 printk("tm6000: alt %d, interface %i, class %i\n",
523 interface->altsetting[i].desc.bInterfaceNumber,
524 interface->altsetting[i].desc.bInterfaceClass);
526 switch (e->desc.bmAttributes) {
527 case USB_ENDPOINT_XFER_BULK:
529 get_max_endpoint (usbdev, "Bulk IN", e,
533 get_max_endpoint (usbdev, "Bulk OUT", e,
538 case USB_ENDPOINT_XFER_ISOC:
540 get_max_endpoint (usbdev, "ISOC IN", e,
544 get_max_endpoint (usbdev, "ISOC OUT", e,
554 printk("tm6000: New video device @ %s Mbps (%04x:%04x, ifnum %d)\n",
556 le16_to_cpu(dev->udev->descriptor.idVendor),
557 le16_to_cpu(dev->udev->descriptor.idProduct),
558 interface->altsetting->desc.bInterfaceNumber);
560 /* check if the the device has the iso in endpoint at the correct place */
562 printk("tm6000: probing error: no IN ISOC endpoint!\n");
568 /* save our data pointer in this interface device */
569 usb_set_intfdata(interface, dev);
571 printk("tm6000: Found %s\n", tm6000_boards[dev->model].name);
573 rc=tm6000_init_dev(dev);
581 printk("tm6000: Error %d while registering\n", rc);
583 tm6000_devused&=~(1<<nr);
591 * tm6000_usb_disconnect()
592 * called when the device gets diconencted
593 * video device will be unregistered on v4l2_close in case it is still open
595 static void tm6000_usb_disconnect(struct usb_interface *interface)
597 struct tm6000_core *dev = usb_get_intfdata(interface);
598 usb_set_intfdata(interface, NULL);
603 printk("tm6000: disconnecting %s\n", dev->name);
605 mutex_lock(&dev->lock);
607 #ifdef CONFIG_VIDEO_TM6000_DVB
609 tm6000_dvb_unregister(dev);
614 tm6000_v4l2_unregister(dev);
616 tm6000_i2c_unregister(dev);
618 v4l2_device_unregister(&dev->v4l2_dev);
620 // wake_up_interruptible_all(&dev->open);
622 dev->state |= DEV_DISCONNECTED;
624 usb_put_dev(dev->udev);
626 mutex_unlock(&dev->lock);
630 static struct usb_driver tm6000_usb_driver = {
632 .probe = tm6000_usb_probe,
633 .disconnect = tm6000_usb_disconnect,
634 .id_table = tm6000_id_table,
637 static int __init tm6000_module_init(void)
641 printk(KERN_INFO "tm6000" " v4l2 driver version %d.%d.%d loaded\n",
642 (TM6000_VERSION >> 16) & 0xff,
643 (TM6000_VERSION >> 8) & 0xff, TM6000_VERSION & 0xff);
645 /* register this driver with the USB subsystem */
646 result = usb_register(&tm6000_usb_driver);
649 " usb_register failed. Error number %d.\n", result);
654 static void __exit tm6000_module_exit(void)
656 /* deregister at USB subsystem */
657 usb_deregister(&tm6000_usb_driver);
660 module_init(tm6000_module_init);
661 module_exit(tm6000_module_exit);
663 MODULE_DESCRIPTION("Trident TVMaster TM5600/TM6000 USB2 adapter");
664 MODULE_AUTHOR("Mauro Carvalho Chehab");
665 MODULE_LICENSE("GPL");