c26cf2f73e19f654457c52aac09538c927dd1486
[safe/jmp/linux-2.6] / drivers / staging / tm6000 / tm6000-cards.c
1 /*
2    tm6000-cards.c - driver for TM5600/TM6000/TM6010 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 #include <media/tvaudio.h>
30 #include <media/i2c-addr.h>
31
32 #include "tm6000.h"
33 #include "tm6000-regs.h"
34 #include "tuner-xc2028.h"
35 #include "xc5000.h"
36
37 #define TM6000_BOARD_UNKNOWN                    0
38 #define TM5600_BOARD_GENERIC                    1
39 #define TM6000_BOARD_GENERIC                    2
40 #define TM6010_BOARD_GENERIC                    3
41 #define TM5600_BOARD_10MOONS_UT821              4
42 #define TM5600_BOARD_10MOONS_UT330              5
43 #define TM6000_BOARD_ADSTECH_DUAL_TV            6
44 #define TM6000_BOARD_FREECOM_AND_SIMILAR        7
45 #define TM6000_BOARD_ADSTECH_MINI_DUAL_TV       8
46 #define TM6010_BOARD_HAUPPAUGE_900H             9
47 #define TM6010_BOARD_BEHOLD_WANDER              10
48 #define TM6010_BOARD_BEHOLD_VOYAGER             11
49 #define TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE 12
50 #define TM6010_BOARD_TWINHAN_TU501              13
51
52 #define TM6000_MAXBOARDS        16
53 static unsigned int card[]     = {[0 ... (TM6000_MAXBOARDS - 1)] = UNSET };
54
55 module_param_array(card,  int, NULL, 0444);
56
57 static unsigned long tm6000_devused;
58
59
60 struct tm6000_board {
61         char            *name;
62
63         struct tm6000_capabilities caps;
64
65         enum            tm6000_devtype type;    /* variant of the chipset */
66         int             tuner_type;     /* type of the tuner */
67         int             tuner_addr;     /* tuner address */
68         int             demod_addr;     /* demodulator address */
69         int             gpio_addr_tun_reset;    /* GPIO used for tuner reset */
70 };
71
72 struct tm6000_board tm6000_boards[] = {
73         [TM6000_BOARD_UNKNOWN] = {
74                 .name         = "Unknown tm6000 video grabber",
75                 .caps = {
76                         .has_tuner    = 1,
77                 },
78                 .gpio_addr_tun_reset = TM6000_GPIO_1,
79         },
80         [TM5600_BOARD_GENERIC] = {
81                 .name         = "Generic tm5600 board",
82                 .type         = TM5600,
83                 .tuner_type   = TUNER_XC2028,
84                 .tuner_addr   = 0xc2 >> 1,
85                 .caps = {
86                         .has_tuner      = 1,
87                 },
88                 .gpio_addr_tun_reset = TM6000_GPIO_1,
89         },
90         [TM6000_BOARD_GENERIC] = {
91                 .name         = "Generic tm6000 board",
92                 .tuner_type   = TUNER_XC2028,
93                 .tuner_addr   = 0xc2 >> 1,
94                 .caps = {
95                         .has_tuner      = 1,
96                         .has_dvb        = 1,
97                 },
98                 .gpio_addr_tun_reset = TM6000_GPIO_1,
99         },
100         [TM6010_BOARD_GENERIC] = {
101                 .name         = "Generic tm6010 board",
102                 .type         = TM6010,
103                 .tuner_type   = TUNER_XC2028,
104                 .tuner_addr   = 0xc2 >> 1,
105                 .caps = {
106                         .has_tuner      = 1,
107                         .has_dvb        = 1,
108                 },
109                 .gpio_addr_tun_reset = TM6010_GPIO_4,
110         },
111         [TM5600_BOARD_10MOONS_UT821] = {
112                 .name         = "10Moons UT 821",
113                 .tuner_type   = TUNER_XC2028,
114                 .type         = TM5600,
115                 .tuner_addr   = 0xc2 >> 1,
116                 .caps = {
117                         .has_tuner    = 1,
118                         .has_eeprom   = 1,
119                 },
120                 .gpio_addr_tun_reset = TM6000_GPIO_1,
121         },
122         [TM5600_BOARD_10MOONS_UT330] = {
123                 .name         = "10Moons UT 330",
124                 .tuner_type   = TUNER_PHILIPS_FQ1216AME_MK4,
125                 .tuner_addr   = 0xc8 >> 1,
126                 .caps = {
127                         .has_tuner    = 1,
128                         .has_dvb      = 0,
129                         .has_zl10353  = 0,
130                         .has_eeprom   = 1,
131                 },
132         },
133         [TM6000_BOARD_ADSTECH_DUAL_TV] = {
134                 .name         = "ADSTECH Dual TV USB",
135                 .tuner_type   = TUNER_XC2028,
136                 .tuner_addr   = 0xc8 >> 1,
137                 .caps = {
138                         .has_tuner    = 1,
139                         .has_tda9874  = 1,
140                         .has_dvb      = 1,
141                         .has_zl10353  = 1,
142                         .has_eeprom   = 1,
143                 },
144         },
145         [TM6000_BOARD_FREECOM_AND_SIMILAR] = {
146                 .name         = "Freecom Hybrid Stick / Moka DVB-T Receiver Dual",
147                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
148                 .tuner_addr   = 0xc2 >> 1,
149                 .demod_addr   = 0x1e >> 1,
150                 .caps = {
151                         .has_tuner    = 1,
152                         .has_dvb      = 1,
153                         .has_zl10353  = 1,
154                         .has_eeprom   = 0,
155                         .has_remote   = 1,
156                 },
157                 .gpio_addr_tun_reset = TM6000_GPIO_4,
158         },
159         [TM6000_BOARD_ADSTECH_MINI_DUAL_TV] = {
160                 .name         = "ADSTECH Mini Dual TV USB",
161                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
162                 .tuner_addr   = 0xc8 >> 1,
163                 .demod_addr   = 0x1e >> 1,
164                 .caps = {
165                         .has_tuner    = 1,
166                         .has_dvb      = 1,
167                         .has_zl10353  = 1,
168                         .has_eeprom   = 0,
169                 },
170                 .gpio_addr_tun_reset = TM6000_GPIO_4,
171         },
172         [TM6010_BOARD_HAUPPAUGE_900H] = {
173                 .name         = "Hauppauge WinTV HVR-900H / WinTV USB2-Stick",
174                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
175                 .tuner_addr   = 0xc2 >> 1,
176                 .demod_addr   = 0x1e >> 1,
177                 .type         = TM6010,
178                 .caps = {
179                         .has_tuner    = 1,
180                         .has_dvb      = 1,
181                         .has_zl10353  = 1,
182                         .has_eeprom   = 1,
183                 },
184                 .gpio_addr_tun_reset = TM6010_GPIO_2,
185         },
186         [TM6010_BOARD_BEHOLD_WANDER] = {
187                 .name         = "Beholder Wander DVB-T/TV/FM USB2.0",
188                 .tuner_type   = TUNER_XC5000,
189                 .tuner_addr   = 0xc2 >> 1,
190                 .demod_addr   = 0x1e >> 1,
191                 .type         = TM6010,
192                 .caps = {
193                         .has_tuner    = 1,
194                         .has_dvb      = 1,
195                         .has_zl10353  = 1,
196                         .has_eeprom   = 1,
197                         .has_remote   = 1,
198                 },
199                 .gpio_addr_tun_reset = TM6000_GPIO_2,
200         },
201         [TM6010_BOARD_BEHOLD_VOYAGER] = {
202                 .name         = "Beholder Voyager TV/FM USB2.0",
203                 .tuner_type   = TUNER_XC5000,
204                 .tuner_addr   = 0xc2 >> 1,
205                 .type         = TM6010,
206                 .caps = {
207                         .has_tuner    = 1,
208                         .has_dvb      = 0,
209                         .has_zl10353  = 0,
210                         .has_eeprom   = 1,
211                         .has_remote   = 1,
212                 },
213                 .gpio_addr_tun_reset = TM6000_GPIO_2,
214         },
215         [TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE] = {
216                 .name         = "Terratec Cinergy Hybrid XE / Cinergy Hybrid-Stick",
217                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
218                 .tuner_addr   = 0xc2 >> 1,
219                 .demod_addr   = 0x1e >> 1,
220                 .type         = TM6010,
221                 .caps = {
222                         .has_tuner    = 1,
223                         .has_dvb      = 1,
224                         .has_zl10353  = 1,
225                         .has_eeprom   = 1,
226                         .has_remote   = 1,
227                 },
228                 .gpio_addr_tun_reset = TM6010_GPIO_2,
229         },
230         [TM6010_BOARD_TWINHAN_TU501] = {
231                 .name         = "Twinhan TU501(704D1)",
232                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
233                 .tuner_addr   = 0xc2 >> 1,
234                 .demod_addr   = 0x1e >> 1,
235                 .type         = TM6010,
236                 .caps = {
237                         .has_tuner    = 1,
238                         .has_dvb      = 1,
239                         .has_zl10353  = 1,
240                         .has_eeprom   = 1,
241                         .has_remote   = 1,
242                 },
243                 .gpio_addr_tun_reset = TM6010_GPIO_2,
244         }
245 };
246
247 /* table of devices that work with this driver */
248 struct usb_device_id tm6000_id_table [] = {
249         { USB_DEVICE(0x6000, 0x0001), .driver_info = TM5600_BOARD_10MOONS_UT821 },
250         { USB_DEVICE(0x6000, 0x0002), .driver_info = TM6010_BOARD_GENERIC },
251         { USB_DEVICE(0x06e1, 0xf332), .driver_info = TM6000_BOARD_ADSTECH_DUAL_TV },
252         { USB_DEVICE(0x14aa, 0x0620), .driver_info = TM6000_BOARD_FREECOM_AND_SIMILAR },
253         { USB_DEVICE(0x06e1, 0xb339), .driver_info = TM6000_BOARD_ADSTECH_MINI_DUAL_TV },
254         { USB_DEVICE(0x2040, 0x6600), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
255         { USB_DEVICE(0x2040, 0x6601), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
256         { USB_DEVICE(0x2040, 0x6610), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
257         { USB_DEVICE(0x2040, 0x6611), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
258         { USB_DEVICE(0x6000, 0xdec0), .driver_info = TM6010_BOARD_BEHOLD_WANDER },
259         { USB_DEVICE(0x6000, 0xdec1), .driver_info = TM6010_BOARD_BEHOLD_VOYAGER },
260         { USB_DEVICE(0x0ccd, 0x0086), .driver_info = TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE },
261         { USB_DEVICE(0x0ccd, 0x00A5), .driver_info = TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE },
262         { USB_DEVICE(0x13d3, 0x3240), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
263         { USB_DEVICE(0x13d3, 0x3241), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
264         { USB_DEVICE(0x13d3, 0x3243), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
265         { USB_DEVICE(0x13d3, 0x3264), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
266         { },
267 };
268
269 /* Tuner callback to provide the proper gpio changes needed for xc2028 */
270
271 int tm6000_tuner_callback(void *ptr, int component, int command, int arg)
272 {
273         int rc=0;
274         struct tm6000_core *dev = ptr;
275
276         if (dev->tuner_type!=TUNER_XC2028)
277                 return 0;
278
279         switch (command) {
280         case XC2028_RESET_CLK:
281                 tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT,
282                                         0x02, arg);
283                 msleep(10);
284                 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
285                                         TM6000_GPIO_CLK, 0);
286                 if (rc<0)
287                         return rc;
288                 msleep(10);
289                 rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
290                                         TM6000_GPIO_CLK, 1);
291                 break;
292         case XC2028_TUNER_RESET:
293                 /* Reset codes during load firmware */
294                 switch (arg) {
295                 case 0:
296                         /* newer tuner can faster reset */
297                         switch (dev->model) {
298                         case TM6010_BOARD_HAUPPAUGE_900H:
299                         case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
300                         case TM6010_BOARD_TWINHAN_TU501:
301                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
302                                                dev->tuner_reset_gpio, 0x01);
303                                 msleep(60);
304                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
305                                                dev->tuner_reset_gpio, 0x00);
306                                 msleep(75);
307                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
308                                                dev->tuner_reset_gpio, 0x01);
309                                 msleep(60);
310                                 break;
311                         default:
312                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
313                                                dev->tuner_reset_gpio, 0x00);
314                                 msleep(130);
315                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
316                                                dev->tuner_reset_gpio, 0x01);
317                                 msleep(130);
318                                 break;
319                         }
320                         break;
321                 case 1:
322                         tm6000_set_reg (dev, REQ_04_EN_DISABLE_MCU_INT,
323                                                 0x02, 0x01);
324                         msleep(10);
325                         break;
326
327                 case 2:
328                         rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
329                                                 TM6000_GPIO_CLK, 0);
330                         if (rc<0)
331                                 return rc;
332                         msleep(100);
333                         rc=tm6000_set_reg (dev, REQ_03_SET_GET_MCU_PIN,
334                                                 TM6000_GPIO_CLK, 1);
335                         msleep(100);
336                         break;
337                 }
338         }
339         return (rc);
340 }
341
342 int tm6000_cards_setup(struct tm6000_core *dev)
343 {
344         int i, rc;
345
346         /*
347          * Board-specific initialization sequence. Handles all GPIO
348          * initialization sequences that are board-specific.
349          * Up to now, all found devices use GPIO1 and GPIO4 at the same way.
350          * Probably, they're all based on some reference device. Due to that,
351          * there's a common routine at the end to handle those GPIO's. Devices
352          * that use different pinups or init sequences can just return at
353          * the board-specific session.
354          */
355         switch (dev->model) {
356         case TM6010_BOARD_HAUPPAUGE_900H:
357         case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
358         case TM6010_BOARD_TWINHAN_TU501:
359                 /* Turn xceive 3028 on */
360                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6010_GPIO_3, 0x01);
361                 msleep(15);
362                 /* Turn zarlink zl10353 on */
363                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6010_GPIO_4, 0x00);
364                 msleep(15);
365                 /* Reset zarlink zl10353 */
366                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6010_GPIO_1, 0x00);
367                 msleep(50);
368                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6010_GPIO_1, 0x01);
369                 msleep(15);
370                 /* Turn zarlink zl10353 off */
371                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6010_GPIO_4, 0x01);
372                 msleep(15);
373                 /* ir ? */
374                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6010_GPIO_0, 0x01);
375                 msleep(15);
376                 /* Power led on (blue) */
377                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6010_GPIO_7, 0x00);
378                 msleep(15);
379                 /* DVB led off (orange) */
380                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6010_GPIO_5, 0x01);
381                 msleep(15);
382                 /* Turn zarlink zl10353 on */
383                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6010_GPIO_4, 0x00);
384                 msleep(15);
385                 break;
386         default:
387                 break;
388         }
389
390         /*
391          * Default initialization. Most of the devices seem to use GPIO1
392          * and GPIO4.on the same way, so, this handles the common sequence
393          * used by most devices.
394          * If a device uses a different sequence or different GPIO pins for
395          * reset, just add the code at the board-specific part
396          */
397         for (i = 0; i < 2; i++) {
398                 rc = tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
399                                         dev->tuner_reset_gpio, 0x00);
400                 if (rc < 0) {
401                         printk(KERN_ERR "Error %i doing GPIO1 reset\n", rc);
402                         return rc;
403                 }
404
405                 msleep(10); /* Just to be conservative */
406                 rc = tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
407                                         dev->tuner_reset_gpio, 0x01);
408                 if (rc < 0) {
409                         printk(KERN_ERR "Error %i doing GPIO1 reset\n", rc);
410                         return rc;
411                 }
412
413                 msleep(10);
414                 rc = tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6000_GPIO_4, 0);
415                 if (rc < 0) {
416                         printk(KERN_ERR "Error %i doing GPIO4 reset\n", rc);
417                         return rc;
418                 }
419
420                 msleep(10);
421                 rc = tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, TM6000_GPIO_4, 1);
422                 if (rc < 0) {
423                         printk(KERN_ERR "Error %i doing GPIO4 reset\n", rc);
424                         return rc;
425                 }
426
427                 if (!i) {
428                         rc = tm6000_get_reg16(dev, 0x40, 0, 0);
429                         if (rc >= 0)
430                                 printk(KERN_DEBUG "board=%d\n", rc);
431                 }
432         }
433
434         msleep(50);
435
436         return 0;
437 };
438
439 static void tm6000_config_tuner (struct tm6000_core *dev)
440 {
441         struct tuner_setup           tun_setup;
442
443         /* Load tuner module */
444         v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
445                 "tuner", "tuner",dev->tuner_addr, NULL);
446
447         memset(&tun_setup, 0, sizeof(tun_setup));
448         tun_setup.type   = dev->tuner_type;
449         tun_setup.addr   = dev->tuner_addr;
450         tun_setup.mode_mask = T_ANALOG_TV | T_RADIO | T_DIGITAL_TV;
451         tun_setup.tuner_callback = tm6000_tuner_callback;
452
453         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_type_addr, &tun_setup);
454
455         if (dev->tuner_type == TUNER_XC2028) {
456                 struct v4l2_priv_tun_config  xc2028_cfg;
457                 struct xc2028_ctrl           ctl;
458
459                 memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
460                 memset (&ctl,0,sizeof(ctl));
461
462                 ctl.input1 = 1;
463                 ctl.read_not_reliable = 0;
464                 ctl.msleep = 10;
465                 ctl.demod = XC3028_FE_ZARLINK456;
466                 ctl.vhfbw7 = 1;
467                 ctl.uhfbw8 = 1;
468                 xc2028_cfg.tuner = TUNER_XC2028;
469                 xc2028_cfg.priv  = &ctl;
470
471                 switch(dev->model) {
472                 case TM6010_BOARD_HAUPPAUGE_900H:
473                 case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
474                 case TM6010_BOARD_TWINHAN_TU501:
475                         ctl.fname = "xc3028L-v36.fw";
476                         break;
477                 default:
478                         if (dev->dev_type == TM6010)
479                                 ctl.fname = "xc3028-v27.fw";
480                         else
481                                 ctl.fname = "tm6000-xc3028.fw";
482                 }
483
484                 printk(KERN_INFO "Setting firmware parameters for xc2028\n");
485
486                 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_config,
487                                      &xc2028_cfg);
488         }
489 }
490
491 static int tm6000_init_dev(struct tm6000_core *dev)
492 {
493         struct v4l2_frequency f;
494         int rc = 0;
495
496         mutex_init(&dev->lock);
497
498         mutex_lock(&dev->lock);
499
500         /* Initializa board-specific data */
501         dev->dev_type   = tm6000_boards[dev->model].type;
502         dev->tuner_type = tm6000_boards[dev->model].tuner_type;
503         dev->tuner_addr = tm6000_boards[dev->model].tuner_addr;
504         dev->tuner_reset_gpio = tm6000_boards[dev->model].gpio_addr_tun_reset;
505
506         dev->demod_addr = tm6000_boards[dev->model].demod_addr;
507
508         dev->caps = tm6000_boards[dev->model].caps;
509
510         /* initialize hardware */
511         rc=tm6000_init (dev);
512         if (rc<0)
513                 goto err;
514
515         rc = v4l2_device_register(&dev->udev->dev, &dev->v4l2_dev);
516         if (rc < 0)
517                 goto err;
518
519         /* register i2c bus */
520         rc=tm6000_i2c_register(dev);
521         if (rc<0)
522                 goto err;
523
524         /* Default values for STD and resolutions */
525         dev->width = 720;
526         dev->height = 480;
527         dev->norm = V4L2_STD_PAL_M;
528
529         /* Configure tuner */
530         tm6000_config_tuner (dev);
531
532         /* Set video standard */
533         v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
534
535         /* Set tuner frequency - also loads firmware on xc2028/xc3028 */
536         f.tuner = 0;
537         f.type = V4L2_TUNER_ANALOG_TV;
538         f.frequency = 3092;     /* 193.25 MHz */
539         dev->freq = f.frequency;
540         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
541
542         if (dev->caps.has_tda9874)
543                 v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
544                         "tvaudio", "tvaudio", I2C_ADDR_TDA9874, NULL);
545
546         /* register and initialize V4L2 */
547         rc=tm6000_v4l2_register(dev);
548         if (rc<0)
549                 goto err;
550
551         if(dev->caps.has_dvb) {
552                 dev->dvb = kzalloc(sizeof(*(dev->dvb)), GFP_KERNEL);
553                 if(!dev->dvb) {
554                         rc = -ENOMEM;
555                         goto err2;
556                 }
557
558 #ifdef CONFIG_VIDEO_TM6000_DVB
559                 rc = tm6000_dvb_register(dev);
560                 if(rc < 0) {
561                         kfree(dev->dvb);
562                         dev->dvb = NULL;
563                         goto err2;
564                 }
565 #endif
566         }
567         mutex_unlock(&dev->lock);
568         return 0;
569
570 err2:
571         v4l2_device_unregister(&dev->v4l2_dev);
572
573 err:
574         mutex_unlock(&dev->lock);
575         return rc;
576 }
577
578 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
579 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
580
581 static void get_max_endpoint (  struct usb_device *usbdev,
582                                 char *msgtype,
583                                 struct usb_host_endpoint *curr_e,
584                                 unsigned int *maxsize,
585                                 struct usb_host_endpoint **ep  )
586 {
587         u16 tmp = le16_to_cpu(curr_e->desc.wMaxPacketSize);
588         unsigned int size = tmp & 0x7ff;
589
590         if (usbdev->speed == USB_SPEED_HIGH)
591                 size = size * hb_mult (tmp);
592
593         if (size>*maxsize) {
594                 *ep = curr_e;
595                 *maxsize = size;
596                 printk("tm6000: %s endpoint: 0x%02x (max size=%u bytes)\n",
597                                         msgtype, curr_e->desc.bEndpointAddress,
598                                         size);
599         }
600 }
601
602 /*
603  * tm6000_usb_probe()
604  * checks for supported devices
605  */
606 static int tm6000_usb_probe(struct usb_interface *interface,
607                             const struct usb_device_id *id)
608 {
609         struct usb_device *usbdev;
610         struct tm6000_core *dev = NULL;
611         int i,rc=0;
612         int nr=0;
613         char *speed;
614
615
616         usbdev=usb_get_dev(interface_to_usbdev(interface));
617
618         /* Selects the proper interface */
619         rc=usb_set_interface(usbdev,0,1);
620         if (rc<0)
621                 goto err;
622
623         /* Check to see next free device and mark as used */
624         nr=find_first_zero_bit(&tm6000_devused,TM6000_MAXBOARDS);
625         if (nr >= TM6000_MAXBOARDS) {
626                 printk ("tm6000: Supports only %i tm60xx boards.\n",TM6000_MAXBOARDS);
627                 usb_put_dev(usbdev);
628                 return -ENOMEM;
629         }
630
631         /* Create and initialize dev struct */
632         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
633         if (dev == NULL) {
634                 printk ("tm6000" ": out of memory!\n");
635                 usb_put_dev(usbdev);
636                 return -ENOMEM;
637         }
638         spin_lock_init(&dev->slock);
639
640         /* Increment usage count */
641         tm6000_devused|=1<<nr;
642         snprintf(dev->name, 29, "tm6000 #%d", nr);
643
644         dev->model=id->driver_info;
645         if ((card[nr]>=0) && (card[nr]<ARRAY_SIZE(tm6000_boards))) {
646                 dev->model=card[nr];
647         }
648
649         INIT_LIST_HEAD(&dev->tm6000_corelist);
650         dev->udev= usbdev;
651         dev->devno=nr;
652
653         switch (usbdev->speed) {
654         case USB_SPEED_LOW:
655                 speed = "1.5";
656                 break;
657         case USB_SPEED_UNKNOWN:
658         case USB_SPEED_FULL:
659                 speed = "12";
660                 break;
661         case USB_SPEED_HIGH:
662                 speed = "480";
663                 break;
664         default:
665                 speed = "unknown";
666         }
667
668
669
670         /* Get endpoints */
671         for (i = 0; i < interface->num_altsetting; i++) {
672                 int ep;
673
674                 for (ep = 0; ep < interface->altsetting[i].desc.bNumEndpoints; ep++) {
675                         struct usb_host_endpoint        *e;
676                         int dir_out;
677
678                         e = &interface->altsetting[i].endpoint[ep];
679
680                         dir_out = ((e->desc.bEndpointAddress &
681                                         USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
682
683                         printk("tm6000: alt %d, interface %i, class %i\n",
684                                i,
685                                interface->altsetting[i].desc.bInterfaceNumber,
686                                interface->altsetting[i].desc.bInterfaceClass);
687
688                         switch (e->desc.bmAttributes) {
689                         case USB_ENDPOINT_XFER_BULK:
690                                 if (!dir_out) {
691                                         get_max_endpoint (usbdev, "Bulk IN", e,
692                                                         &dev->max_bulk_in,
693                                                         &dev->bulk_in);
694                                 } else {
695                                         get_max_endpoint (usbdev, "Bulk OUT", e,
696                                                         &dev->max_bulk_out,
697                                                         &dev->bulk_out);
698                                 }
699                                 break;
700                         case USB_ENDPOINT_XFER_ISOC:
701                                 if (!dir_out) {
702                                         get_max_endpoint (usbdev, "ISOC IN", e,
703                                                         &dev->max_isoc_in,
704                                                         &dev->isoc_in);
705                                 } else {
706                                         get_max_endpoint (usbdev, "ISOC OUT", e,
707                                                         &dev->max_isoc_out,
708                                                         &dev->isoc_out);
709                                 }
710                                 break;
711                         }
712                 }
713         }
714
715
716         printk("tm6000: New video device @ %s Mbps (%04x:%04x, ifnum %d)\n",
717                 speed,
718                 le16_to_cpu(dev->udev->descriptor.idVendor),
719                 le16_to_cpu(dev->udev->descriptor.idProduct),
720                 interface->altsetting->desc.bInterfaceNumber);
721
722 /* check if the the device has the iso in endpoint at the correct place */
723         if (!dev->isoc_in) {
724                 printk("tm6000: probing error: no IN ISOC endpoint!\n");
725                 rc= -ENODEV;
726
727                 goto err;
728         }
729
730         /* save our data pointer in this interface device */
731         usb_set_intfdata(interface, dev);
732
733         printk("tm6000: Found %s\n", tm6000_boards[dev->model].name);
734
735         rc=tm6000_init_dev(dev);
736
737         if (rc<0)
738                 goto err;
739
740         return 0;
741
742 err:
743         printk("tm6000: Error %d while registering\n", rc);
744
745         tm6000_devused&=~(1<<nr);
746         usb_put_dev(usbdev);
747
748         kfree(dev);
749         return rc;
750 }
751
752 /*
753  * tm6000_usb_disconnect()
754  * called when the device gets diconencted
755  * video device will be unregistered on v4l2_close in case it is still open
756  */
757 static void tm6000_usb_disconnect(struct usb_interface *interface)
758 {
759         struct tm6000_core *dev = usb_get_intfdata(interface);
760         usb_set_intfdata(interface, NULL);
761
762         if (!dev)
763                 return;
764
765         printk("tm6000: disconnecting %s\n", dev->name);
766
767         mutex_lock(&dev->lock);
768
769 #ifdef CONFIG_VIDEO_TM6000_DVB
770         if(dev->dvb) {
771                 tm6000_dvb_unregister(dev);
772                 kfree(dev->dvb);
773         }
774 #endif
775
776         tm6000_v4l2_unregister(dev);
777
778         tm6000_i2c_unregister(dev);
779
780         v4l2_device_unregister(&dev->v4l2_dev);
781
782 //      wake_up_interruptible_all(&dev->open);
783
784         dev->state |= DEV_DISCONNECTED;
785
786         usb_put_dev(dev->udev);
787
788         mutex_unlock(&dev->lock);
789         kfree(dev);
790 }
791
792 static struct usb_driver tm6000_usb_driver = {
793                 .name = "tm6000",
794                 .probe = tm6000_usb_probe,
795                 .disconnect = tm6000_usb_disconnect,
796                 .id_table = tm6000_id_table,
797 };
798
799 static int __init tm6000_module_init(void)
800 {
801         int result;
802
803         printk(KERN_INFO "tm6000" " v4l2 driver version %d.%d.%d loaded\n",
804                (TM6000_VERSION  >> 16) & 0xff,
805                (TM6000_VERSION  >> 8) & 0xff, TM6000_VERSION  & 0xff);
806
807         /* register this driver with the USB subsystem */
808         result = usb_register(&tm6000_usb_driver);
809         if (result)
810                 printk("tm6000"
811                            " usb_register failed. Error number %d.\n", result);
812
813         return result;
814 }
815
816 static void __exit tm6000_module_exit(void)
817 {
818         /* deregister at USB subsystem */
819         usb_deregister(&tm6000_usb_driver);
820 }
821
822 module_init(tm6000_module_init);
823 module_exit(tm6000_module_exit);
824
825 MODULE_DESCRIPTION("Trident TVMaster TM5600/TM6000/TM6010 USB2 adapter");
826 MODULE_AUTHOR("Mauro Carvalho Chehab");
827 MODULE_LICENSE("GPL");