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