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