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