]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/tm6000/tm6000-cards.c
V4L/DVB (12772): tm6000: fix module load/unload
[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
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
40 static unsigned int card[]     = {[0 ... (TM6000_MAXBOARDS - 1)] = UNSET };
41
42 module_param_array(card,  int, NULL, 0444);
43
44 struct 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
54 struct 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 */
113 struct 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
119 static 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
163 err:
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
171 static 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  */
196 static 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;
232
233         INIT_LIST_HEAD(&dev->tm6000_corelist);
234         dev->udev= usbdev;
235         dev->model=id->driver_info;
236         snprintf(dev->name, 29, "tm6000 #%d", nr);
237         dev->devno=nr;
238
239         switch (usbdev->speed) {
240         case USB_SPEED_LOW:
241                 speed = "1.5";
242                 break;
243         case USB_SPEED_UNKNOWN:
244         case USB_SPEED_FULL:
245                 speed = "12";
246                 break;
247         case USB_SPEED_HIGH:
248                 speed = "480";
249                 break;
250         default:
251                 speed = "unknown";
252         }
253
254
255
256         /* Get endpoints */
257         for (i = 0; i < interface->num_altsetting; i++) {
258                 int ep;
259
260                 for (ep = 0; ep < interface->altsetting[i].desc.bNumEndpoints; ep++) {
261                         struct usb_host_endpoint        *e;
262                         int dir_out;
263
264                         e = &interface->altsetting[i].endpoint[ep];
265
266                         dir_out = ((e->desc.bEndpointAddress &
267                                         USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
268
269                         printk("tm6000: alt %d, interface %i, class %i\n",
270                                i,
271                                interface->altsetting[i].desc.bInterfaceNumber,
272                                interface->altsetting[i].desc.bInterfaceClass);
273
274                         switch (e->desc.bmAttributes) {
275                         case USB_ENDPOINT_XFER_BULK:
276                                 if (!dir_out) {
277                                         get_max_endpoint (usbdev, "Bulk IN", e,
278                                                         &dev->max_bulk_in,
279                                                         &dev->bulk_in);
280                                 } else {
281                                         get_max_endpoint (usbdev, "Bulk OUT", e,
282                                                         &dev->max_bulk_out,
283                                                         &dev->bulk_out);
284                                 }
285                                 break;
286                         case USB_ENDPOINT_XFER_ISOC:
287                                 if (!dir_out) {
288                                         get_max_endpoint (usbdev, "ISOC IN", e,
289                                                         &dev->max_isoc_in,
290                                                         &dev->isoc_in);
291                                 } else {
292                                         get_max_endpoint (usbdev, "ISOC OUT", e,
293                                                         &dev->max_isoc_out,
294                                                         &dev->isoc_out);
295                                 }
296                                 break;
297                         }
298                 }
299         }
300
301         if (interface->altsetting->desc.bAlternateSetting) {
302                 printk("selecting alt setting %d\n",
303                        interface->altsetting->desc.bAlternateSetting);
304                 rc = usb_set_interface (usbdev,
305                                 interface->altsetting->desc.bInterfaceNumber,
306                                 interface->altsetting->desc.bAlternateSetting);
307                 if (rc<0)
308                         goto err;
309         }
310
311         printk("tm6000: New video device @ %s Mbps (%04x:%04x, ifnum %d)\n",
312                 speed,
313                 le16_to_cpu(dev->udev->descriptor.idVendor),
314                 le16_to_cpu(dev->udev->descriptor.idProduct),
315                 interface->altsetting->desc.bInterfaceNumber);
316
317 /* check if the the device has the iso in endpoint at the correct place */
318         if (!dev->isoc_in) {
319                 printk("tm6000: probing error: no IN ISOC endpoint!\n");
320                 rc= -ENODEV;
321
322                 goto err;
323         }
324
325         /* save our data pointer in this interface device */
326         usb_set_intfdata(interface, dev);
327
328         printk("tm6000: Found %s\n", tm6000_boards[dev->model].name);
329
330         rc=tm6000_init_dev(dev);
331
332         if (rc<0)
333                 goto err;
334
335         return 0;
336
337 err:
338         tm6000_devused&=~(1<<nr);
339         usb_put_dev(usbdev);
340
341         kfree(dev);
342         return rc;
343 }
344
345 /*
346  * tm6000_usb_disconnect()
347  * called when the device gets diconencted
348  * video device will be unregistered on v4l2_close in case it is still open
349  */
350 static void tm6000_usb_disconnect(struct usb_interface *interface)
351 {
352         struct tm6000_core *dev = usb_get_intfdata(interface);
353         usb_set_intfdata(interface, NULL);
354
355         if (!dev)
356                 return;
357
358         printk("tm6000: disconnecting %s\n", dev->name);
359
360         mutex_lock(&dev->lock);
361
362         tm6000_i2c_unregister(dev);
363
364         tm6000_v4l2_unregister(dev);
365
366 //      wake_up_interruptible_all(&dev->open);
367
368         dev->state |= DEV_DISCONNECTED;
369
370         mutex_unlock(&dev->lock);
371         kfree(dev);
372 }
373
374 static struct usb_driver tm6000_usb_driver = {
375                 .name = "tm6000",
376                 .probe = tm6000_usb_probe,
377                 .disconnect = tm6000_usb_disconnect,
378                 .id_table = tm6000_id_table,
379 };
380
381 static int __init tm6000_module_init(void)
382 {
383         int result;
384
385         printk(KERN_INFO "tm6000" " v4l2 driver version %d.%d.%d loaded\n",
386                (TM6000_VERSION  >> 16) & 0xff,
387                (TM6000_VERSION  >> 8) & 0xff, TM6000_VERSION  & 0xff);
388
389         /* register this driver with the USB subsystem */
390         result = usb_register(&tm6000_usb_driver);
391         if (result)
392                 printk("tm6000"
393                            " usb_register failed. Error number %d.\n", result);
394
395         return result;
396 }
397
398 static void __exit tm6000_module_exit(void)
399 {
400         /* deregister at USB subsystem */
401         usb_deregister(&tm6000_usb_driver);
402 }
403
404 module_init(tm6000_module_init);
405 module_exit(tm6000_module_exit);
406
407 MODULE_DESCRIPTION("Trident TVMaster TM5600/TM6000 USB2 adapter");
408 MODULE_AUTHOR("Mauro Carvalho Chehab");
409 MODULE_LICENSE("GPL");