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