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