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