]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/tm6000/tm6000-cards.c
cedd9044022f9dc514f6d89489cf137dd7fca262
[karo-tx-linux.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    = TM6010_GPIO_0,
235                         .demod_reset    = TM6010_GPIO_1,
236                         .power_led      = TM6010_GPIO_6,
237                 },
238         },
239         [TM6010_BOARD_BEHOLD_VOYAGER] = {
240                 .name         = "Beholder Voyager TV/FM USB2.0",
241                 .tuner_type   = TUNER_XC5000,
242                 .tuner_addr   = 0xc2 >> 1,
243                 .type         = TM6010,
244                 .caps = {
245                         .has_tuner    = 1,
246                         .has_dvb      = 0,
247                         .has_zl10353  = 0,
248                         .has_eeprom   = 1,
249                         .has_remote   = 1,
250                 },
251                 .gpio = {
252                         .tuner_reset    = TM6010_GPIO_0,
253                         .power_led      = TM6010_GPIO_6,
254                 },
255         },
256         [TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE] = {
257                 .name         = "Terratec Cinergy Hybrid XE / Cinergy Hybrid-Stick",
258                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
259                 .tuner_addr   = 0xc2 >> 1,
260                 .demod_addr   = 0x1e >> 1,
261                 .type         = TM6010,
262                 .caps = {
263                         .has_tuner    = 1,
264                         .has_dvb      = 1,
265                         .has_zl10353  = 1,
266                         .has_eeprom   = 1,
267                         .has_remote   = 1,
268                 },
269                 .gpio = {
270                         .tuner_reset    = TM6010_GPIO_2,
271                         .tuner_on       = TM6010_GPIO_3,
272                         .demod_reset    = TM6010_GPIO_1,
273                         .demod_on       = TM6010_GPIO_4,
274                         .power_led      = TM6010_GPIO_7,
275                         .dvb_led        = TM6010_GPIO_5,
276                         .ir             = TM6010_GPIO_0,
277                 },
278         },
279         [TM6010_BOARD_TWINHAN_TU501] = {
280                 .name         = "Twinhan TU501(704D1)",
281                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
282                 .tuner_addr   = 0xc2 >> 1,
283                 .demod_addr   = 0x1e >> 1,
284                 .type         = TM6010,
285                 .caps = {
286                         .has_tuner    = 1,
287                         .has_dvb      = 1,
288                         .has_zl10353  = 1,
289                         .has_eeprom   = 1,
290                         .has_remote   = 1,
291                 },
292                 .gpio = {
293                         .tuner_reset    = TM6010_GPIO_2,
294                         .tuner_on       = TM6010_GPIO_3,
295                         .demod_reset    = TM6010_GPIO_1,
296                         .demod_on       = TM6010_GPIO_4,
297                         .power_led      = TM6010_GPIO_7,
298                         .dvb_led        = TM6010_GPIO_5,
299                         .ir             = TM6010_GPIO_0,
300                 },
301         }
302 };
303
304 /* table of devices that work with this driver */
305 struct usb_device_id tm6000_id_table[] = {
306         { USB_DEVICE(0x6000, 0x0001), .driver_info = TM5600_BOARD_10MOONS_UT821 },
307         { USB_DEVICE(0x6000, 0x0002), .driver_info = TM6010_BOARD_GENERIC },
308         { USB_DEVICE(0x06e1, 0xf332), .driver_info = TM6000_BOARD_ADSTECH_DUAL_TV },
309         { USB_DEVICE(0x14aa, 0x0620), .driver_info = TM6000_BOARD_FREECOM_AND_SIMILAR },
310         { USB_DEVICE(0x06e1, 0xb339), .driver_info = TM6000_BOARD_ADSTECH_MINI_DUAL_TV },
311         { USB_DEVICE(0x2040, 0x6600), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
312         { USB_DEVICE(0x2040, 0x6601), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
313         { USB_DEVICE(0x2040, 0x6610), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
314         { USB_DEVICE(0x2040, 0x6611), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
315         { USB_DEVICE(0x6000, 0xdec0), .driver_info = TM6010_BOARD_BEHOLD_WANDER },
316         { USB_DEVICE(0x6000, 0xdec1), .driver_info = TM6010_BOARD_BEHOLD_VOYAGER },
317         { USB_DEVICE(0x0ccd, 0x0086), .driver_info = TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE },
318         { USB_DEVICE(0x0ccd, 0x00A5), .driver_info = TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE },
319         { USB_DEVICE(0x13d3, 0x3240), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
320         { USB_DEVICE(0x13d3, 0x3241), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
321         { USB_DEVICE(0x13d3, 0x3243), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
322         { USB_DEVICE(0x13d3, 0x3264), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
323         { },
324 };
325
326 /* Tuner callback to provide the proper gpio changes needed for xc5000 */
327 int tm6000_xc5000_callback(void *ptr, int component, int command, int arg)
328 {
329         int rc = 0;
330         struct tm6000_core *dev = ptr;
331
332         if (dev->tuner_type != TUNER_XC5000)
333                 return 0;
334
335         switch (command) {
336         case XC5000_TUNER_RESET:
337                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
338                                dev->gpio.tuner_reset, 0x01);
339                 msleep(15);
340                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
341                                dev->gpio.tuner_reset, 0x00);
342                 msleep(15);
343                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
344                                dev->gpio.tuner_reset, 0x01);
345                 break;
346         }
347         return (rc);
348 }
349
350
351 /* Tuner callback to provide the proper gpio changes needed for xc2028 */
352
353 int tm6000_tuner_callback(void *ptr, int component, int command, int arg)
354 {
355         int rc = 0;
356         struct tm6000_core *dev = ptr;
357
358         if (dev->tuner_type != TUNER_XC2028)
359                 return 0;
360
361         switch (command) {
362         case XC2028_RESET_CLK:
363                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT,
364                                         0x02, arg);
365                 msleep(10);
366                 rc = tm6000_i2c_reset(dev, 10);
367                 break;
368         case XC2028_TUNER_RESET:
369                 /* Reset codes during load firmware */
370                 switch (arg) {
371                 case 0:
372                         /* newer tuner can faster reset */
373                         switch (dev->model) {
374                         case TM5600_BOARD_10MOONS_UT821:
375                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
376                                                dev->gpio.tuner_reset, 0x01);
377                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
378                                                0x300, 0x01);
379                                 msleep(10);
380                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
381                                                dev->gpio.tuner_reset, 0x00);
382                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
383                                                0x300, 0x00);
384                                 msleep(10);
385                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
386                                                dev->gpio.tuner_reset, 0x01);
387                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
388                                                0x300, 0x01);
389                                 break;
390                         case TM6010_BOARD_HAUPPAUGE_900H:
391                         case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
392                         case TM6010_BOARD_TWINHAN_TU501:
393                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
394                                                dev->gpio.tuner_reset, 0x01);
395                                 msleep(60);
396                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
397                                                dev->gpio.tuner_reset, 0x00);
398                                 msleep(75);
399                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
400                                                dev->gpio.tuner_reset, 0x01);
401                                 msleep(60);
402                                 break;
403                         default:
404                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
405                                                dev->gpio.tuner_reset, 0x00);
406                                 msleep(130);
407                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
408                                                dev->gpio.tuner_reset, 0x01);
409                                 msleep(130);
410                                 break;
411                         }
412                         break;
413                 case 1:
414                         tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT,
415                                                 0x02, 0x01);
416                         msleep(10);
417                         break;
418
419                 case 2:
420                         rc = tm6000_i2c_reset(dev, 100);
421                         break;
422                 }
423         }
424         return rc;
425 }
426
427 int tm6000_cards_setup(struct tm6000_core *dev)
428 {
429         int i, rc;
430
431         /*
432          * Board-specific initialization sequence. Handles all GPIO
433          * initialization sequences that are board-specific.
434          * Up to now, all found devices use GPIO1 and GPIO4 at the same way.
435          * Probably, they're all based on some reference device. Due to that,
436          * there's a common routine at the end to handle those GPIO's. Devices
437          * that use different pinups or init sequences can just return at
438          * the board-specific session.
439          */
440         switch (dev->model) {
441         case TM6010_BOARD_HAUPPAUGE_900H:
442         case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
443         case TM6010_BOARD_TWINHAN_TU501:
444         case TM6010_BOARD_GENERIC:
445                 /* Turn xceive 3028 on */
446                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.tuner_on, 0x01);
447                 msleep(15);
448                 /* Turn zarlink zl10353 on */
449                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_on, 0x00);
450                 msleep(15);
451                 /* Reset zarlink zl10353 */
452                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_reset, 0x00);
453                 msleep(50);
454                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_reset, 0x01);
455                 msleep(15);
456                 /* Turn zarlink zl10353 off */
457                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_on, 0x01);
458                 msleep(15);
459                 /* ir ? */
460                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.ir, 0x01);
461                 msleep(15);
462                 /* Power led on (blue) */
463                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.power_led, 0x00);
464                 msleep(15);
465                 /* DVB led off (orange) */
466                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.dvb_led, 0x01);
467                 msleep(15);
468                 /* Turn zarlink zl10353 on */
469                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_on, 0x00);
470                 msleep(15);
471                 break;
472         case TM6010_BOARD_BEHOLD_WANDER:
473                 /* Power led on (blue) */
474                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.power_led, 0x01);
475                 msleep(15);
476                 /* Reset zarlink zl10353 */
477                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_reset, 0x00);
478                 msleep(50);
479                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_reset, 0x01);
480                 msleep(15);
481                 break;
482         case TM6010_BOARD_BEHOLD_VOYAGER:
483                 /* Power led on (blue) */
484                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.power_led, 0x01);
485                 msleep(15);
486                 break;
487         default:
488                 break;
489         }
490
491         /*
492          * Default initialization. Most of the devices seem to use GPIO1
493          * and GPIO4.on the same way, so, this handles the common sequence
494          * used by most devices.
495          * If a device uses a different sequence or different GPIO pins for
496          * reset, just add the code at the board-specific part
497          */
498
499         if (dev->gpio.tuner_reset) {
500                 for (i = 0; i < 2; i++) {
501                         rc = tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
502                                                 dev->gpio.tuner_reset, 0x00);
503                         if (rc < 0) {
504                                 printk(KERN_ERR "Error %i doing tuner reset\n", rc);
505                                 return rc;
506                         }
507
508                         msleep(10); /* Just to be conservative */
509                         rc = tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
510                                                 dev->gpio.tuner_reset, 0x01);
511                         if (rc < 0) {
512                                 printk(KERN_ERR "Error %i doing tuner reset\n", rc);
513                                 return rc;
514                         }
515                         msleep(10);
516
517                         if (!i) {
518                                 rc = tm6000_get_reg32(dev, REQ_40_GET_VERSION, 0, 0);
519                                 if (rc >= 0)
520                                         printk(KERN_DEBUG "board=0x%08x\n", rc);
521                         }
522                 }
523         } else {
524                 printk(KERN_ERR "Tuner reset is not configured\n");
525                 return -1;
526         }
527
528         msleep(50);
529
530         return 0;
531 };
532
533 static void tm6000_config_tuner(struct tm6000_core *dev)
534 {
535         struct tuner_setup tun_setup;
536
537         /* Load tuner module */
538         v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
539                 "tuner", "tuner", dev->tuner_addr, NULL);
540
541         memset(&tun_setup, 0, sizeof(tun_setup));
542         tun_setup.type = dev->tuner_type;
543         tun_setup.addr = dev->tuner_addr;
544
545         tun_setup.mode_mask = 0;
546         if (dev->caps.has_tuner)
547                 tun_setup.mode_mask |= (T_ANALOG_TV | T_RADIO);
548         if (dev->caps.has_dvb)
549                 tun_setup.mode_mask |= T_DIGITAL_TV;
550
551         switch (dev->tuner_type) {
552         case TUNER_XC2028:
553                 tun_setup.tuner_callback = tm6000_tuner_callback;
554                 break;
555         case TUNER_XC5000:
556                 tun_setup.tuner_callback = tm6000_xc5000_callback;
557                 break;
558         }
559
560         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_type_addr, &tun_setup);
561
562         switch (dev->tuner_type) {
563         case TUNER_XC2028: {
564                 struct v4l2_priv_tun_config xc2028_cfg;
565                 struct xc2028_ctrl ctl;
566
567                 memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
568                 memset(&ctl, 0, sizeof(ctl));
569
570                 ctl.input1 = 1;
571                 ctl.read_not_reliable = 0;
572                 ctl.msleep = 10;
573                 ctl.demod = XC3028_FE_ZARLINK456;
574                 ctl.vhfbw7 = 1;
575                 ctl.uhfbw8 = 1;
576                 xc2028_cfg.tuner = TUNER_XC2028;
577                 xc2028_cfg.priv  = &ctl;
578
579                 switch (dev->model) {
580                 case TM6010_BOARD_HAUPPAUGE_900H:
581                 case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
582                 case TM6010_BOARD_TWINHAN_TU501:
583                         ctl.fname = "xc3028L-v36.fw";
584                         break;
585                 default:
586                         if (dev->dev_type == TM6010)
587                                 ctl.fname = "xc3028-v27.fw";
588                         else
589                                 ctl.fname = "xc3028-v24.fw";
590                 }
591
592                 printk(KERN_INFO "Setting firmware parameters for xc2028\n");
593                 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_config,
594                                      &xc2028_cfg);
595
596                 }
597                 break;
598         case TUNER_XC5000:
599                 {
600                 struct v4l2_priv_tun_config  xc5000_cfg;
601                 struct xc5000_config ctl = {
602                         .i2c_address = dev->tuner_addr,
603                         .if_khz      = 4570,
604                         .radio_input = XC5000_RADIO_FM1,
605                         };
606
607                 xc5000_cfg.tuner = TUNER_XC5000;
608                 xc5000_cfg.priv  = &ctl;
609
610
611                 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_config,
612                                      &xc5000_cfg);
613                 }
614                 break;
615         default:
616                 printk(KERN_INFO "Unknown tuner type. Tuner is not configured.\n");
617                 break;
618         }
619 }
620
621 static int tm6000_init_dev(struct tm6000_core *dev)
622 {
623         struct v4l2_frequency f;
624         int rc = 0;
625
626         mutex_init(&dev->lock);
627
628         mutex_lock(&dev->lock);
629
630         /* Initializa board-specific data */
631         dev->dev_type   = tm6000_boards[dev->model].type;
632         dev->tuner_type = tm6000_boards[dev->model].tuner_type;
633         dev->tuner_addr = tm6000_boards[dev->model].tuner_addr;
634
635         dev->gpio = tm6000_boards[dev->model].gpio;
636
637         dev->demod_addr = tm6000_boards[dev->model].demod_addr;
638
639         dev->caps = tm6000_boards[dev->model].caps;
640
641         /* initialize hardware */
642         rc = tm6000_init(dev);
643         if (rc < 0)
644                 goto err;
645
646         rc = v4l2_device_register(&dev->udev->dev, &dev->v4l2_dev);
647         if (rc < 0)
648                 goto err;
649
650         /* register i2c bus */
651         rc = tm6000_i2c_register(dev);
652         if (rc < 0)
653                 goto err;
654
655         /* Default values for STD and resolutions */
656         dev->width = 720;
657         dev->height = 480;
658         dev->norm = V4L2_STD_PAL_M;
659
660         /* Configure tuner */
661         tm6000_config_tuner(dev);
662
663         /* Set video standard */
664         v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
665
666         /* Set tuner frequency - also loads firmware on xc2028/xc3028 */
667         f.tuner = 0;
668         f.type = V4L2_TUNER_ANALOG_TV;
669         f.frequency = 3092;     /* 193.25 MHz */
670         dev->freq = f.frequency;
671         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
672
673         if (dev->caps.has_tda9874)
674                 v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
675                         "tvaudio", "tvaudio", I2C_ADDR_TDA9874, NULL);
676
677         /* register and initialize V4L2 */
678         rc = tm6000_v4l2_register(dev);
679         if (rc < 0)
680                 goto err;
681
682         tm6000_add_into_devlist(dev);
683
684         tm6000_init_extension(dev);
685
686         if (dev->caps.has_dvb) {
687                 dev->dvb = kzalloc(sizeof(*(dev->dvb)), GFP_KERNEL);
688                 if (!dev->dvb) {
689                         rc = -ENOMEM;
690                         goto err2;
691                 }
692
693 #ifdef CONFIG_VIDEO_TM6000_DVB
694                 rc = tm6000_dvb_register(dev);
695                 if (rc < 0) {
696                         kfree(dev->dvb);
697                         dev->dvb = NULL;
698                         goto err2;
699                 }
700 #endif
701         }
702         mutex_unlock(&dev->lock);
703         return 0;
704
705 err2:
706         v4l2_device_unregister(&dev->v4l2_dev);
707
708 err:
709         mutex_unlock(&dev->lock);
710         return rc;
711 }
712
713 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
714 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
715
716 static void get_max_endpoint(struct usb_device *udev,
717                              struct usb_host_interface *alt,
718                              char *msgtype,
719                              struct usb_host_endpoint *curr_e,
720                              struct tm6000_endpoint *tm_ep)
721 {
722         u16 tmp = le16_to_cpu(curr_e->desc.wMaxPacketSize);
723         unsigned int size = tmp & 0x7ff;
724
725         if (udev->speed == USB_SPEED_HIGH)
726                 size = size * hb_mult (tmp);
727
728         if (size > tm_ep->maxsize) {
729                 tm_ep->endp = curr_e;
730                 tm_ep->maxsize = size;
731                 tm_ep->bInterfaceNumber = alt->desc.bInterfaceNumber;
732                 tm_ep->bAlternateSetting = alt->desc.bAlternateSetting;
733
734                 printk(KERN_INFO "tm6000: %s endpoint: 0x%02x (max size=%u bytes)\n",
735                                         msgtype, curr_e->desc.bEndpointAddress,
736                                         size);
737         }
738 }
739
740 /*
741  * tm6000_usb_probe()
742  * checks for supported devices
743  */
744 static int tm6000_usb_probe(struct usb_interface *interface,
745                             const struct usb_device_id *id)
746 {
747         struct usb_device *usbdev;
748         struct tm6000_core *dev = NULL;
749         int i, rc = 0;
750         int nr = 0;
751         char *speed;
752
753         usbdev = usb_get_dev(interface_to_usbdev(interface));
754
755         /* Selects the proper interface */
756         rc = usb_set_interface(usbdev, 0, 1);
757         if (rc < 0)
758                 goto err;
759
760         /* Check to see next free device and mark as used */
761         nr = find_first_zero_bit(&tm6000_devused, TM6000_MAXBOARDS);
762         if (nr >= TM6000_MAXBOARDS) {
763                 printk(KERN_ERR "tm6000: Supports only %i tm60xx boards.\n", TM6000_MAXBOARDS);
764                 usb_put_dev(usbdev);
765                 return -ENOMEM;
766         }
767
768         /* Create and initialize dev struct */
769         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
770         if (dev == NULL) {
771                 printk(KERN_ERR "tm6000" ": out of memory!\n");
772                 usb_put_dev(usbdev);
773                 return -ENOMEM;
774         }
775         spin_lock_init(&dev->slock);
776
777         /* Increment usage count */
778         tm6000_devused |= 1<<nr;
779         snprintf(dev->name, 29, "tm6000 #%d", nr);
780
781         dev->model = id->driver_info;
782         if ((card[nr] >= 0) && (card[nr] < ARRAY_SIZE(tm6000_boards)))
783                 dev->model = card[nr];
784
785         dev->udev = usbdev;
786         dev->devno = nr;
787
788         switch (usbdev->speed) {
789         case USB_SPEED_LOW:
790                 speed = "1.5";
791                 break;
792         case USB_SPEED_UNKNOWN:
793         case USB_SPEED_FULL:
794                 speed = "12";
795                 break;
796         case USB_SPEED_HIGH:
797                 speed = "480";
798                 break;
799         default:
800                 speed = "unknown";
801         }
802
803
804
805         /* Get endpoints */
806         for (i = 0; i < interface->num_altsetting; i++) {
807                 int ep;
808
809                 for (ep = 0; ep < interface->altsetting[i].desc.bNumEndpoints; ep++) {
810                         struct usb_host_endpoint        *e;
811                         int dir_out;
812
813                         e = &interface->altsetting[i].endpoint[ep];
814
815                         dir_out = ((e->desc.bEndpointAddress &
816                                         USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
817
818                         printk(KERN_INFO "tm6000: alt %d, interface %i, class %i\n",
819                                i,
820                                interface->altsetting[i].desc.bInterfaceNumber,
821                                interface->altsetting[i].desc.bInterfaceClass);
822
823                         switch (e->desc.bmAttributes) {
824                         case USB_ENDPOINT_XFER_BULK:
825                                 if (!dir_out) {
826                                         get_max_endpoint(usbdev,
827                                                          &interface->altsetting[i],
828                                                          "Bulk IN", e,
829                                                          &dev->bulk_in);
830                                 } else {
831                                         get_max_endpoint(usbdev,
832                                                          &interface->altsetting[i],
833                                                          "Bulk OUT", e,
834                                                          &dev->bulk_out);
835                                 }
836                                 break;
837                         case USB_ENDPOINT_XFER_ISOC:
838                                 if (!dir_out) {
839                                         get_max_endpoint(usbdev,
840                                                          &interface->altsetting[i],
841                                                          "ISOC IN", e,
842                                                          &dev->isoc_in);
843                                 } else {
844                                         get_max_endpoint(usbdev,
845                                                          &interface->altsetting[i],
846                                                          "ISOC OUT", e,
847                                                          &dev->isoc_out);
848                                 }
849                                 break;
850                         }
851                 }
852         }
853
854
855         printk(KERN_INFO "tm6000: New video device @ %s Mbps (%04x:%04x, ifnum %d)\n",
856                 speed,
857                 le16_to_cpu(dev->udev->descriptor.idVendor),
858                 le16_to_cpu(dev->udev->descriptor.idProduct),
859                 interface->altsetting->desc.bInterfaceNumber);
860
861 /* check if the the device has the iso in endpoint at the correct place */
862         if (!dev->isoc_in.endp) {
863                 printk(KERN_ERR "tm6000: probing error: no IN ISOC endpoint!\n");
864                 rc = -ENODEV;
865
866                 goto err;
867         }
868
869         /* save our data pointer in this interface device */
870         usb_set_intfdata(interface, dev);
871
872         printk(KERN_INFO "tm6000: Found %s\n", tm6000_boards[dev->model].name);
873
874         rc = tm6000_init_dev(dev);
875
876         if (rc < 0)
877                 goto err;
878
879         return 0;
880
881 err:
882         printk(KERN_ERR "tm6000: Error %d while registering\n", rc);
883
884         tm6000_devused &= ~(1<<nr);
885         usb_put_dev(usbdev);
886
887         kfree(dev);
888         return rc;
889 }
890
891 /*
892  * tm6000_usb_disconnect()
893  * called when the device gets diconencted
894  * video device will be unregistered on v4l2_close in case it is still open
895  */
896 static void tm6000_usb_disconnect(struct usb_interface *interface)
897 {
898         struct tm6000_core *dev = usb_get_intfdata(interface);
899         usb_set_intfdata(interface, NULL);
900
901         if (!dev)
902                 return;
903
904         printk(KERN_INFO "tm6000: disconnecting %s\n", dev->name);
905
906         mutex_lock(&dev->lock);
907
908 #ifdef CONFIG_VIDEO_TM6000_DVB
909         if (dev->dvb) {
910                 tm6000_dvb_unregister(dev);
911                 kfree(dev->dvb);
912         }
913 #endif
914
915         if (dev->gpio.power_led) {
916                 switch (dev->model) {
917                 case TM6010_BOARD_HAUPPAUGE_900H:
918                 case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
919                 case TM6010_BOARD_TWINHAN_TU501:
920                         /* Power led off */
921                         tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
922                                 dev->gpio.power_led, 0x01);
923                         msleep(15);
924                         break;
925                 case TM6010_BOARD_BEHOLD_WANDER:
926                 case TM6010_BOARD_BEHOLD_VOYAGER:
927                         /* Power led off */
928                         tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
929                                 dev->gpio.power_led, 0x00);
930                         msleep(15);
931                         break;
932                 }
933         }
934         tm6000_v4l2_unregister(dev);
935
936         tm6000_i2c_unregister(dev);
937
938         v4l2_device_unregister(&dev->v4l2_dev);
939
940         dev->state |= DEV_DISCONNECTED;
941
942         usb_put_dev(dev->udev);
943
944         tm6000_remove_from_devlist(dev);
945         tm6000_close_extension(dev);
946
947         mutex_unlock(&dev->lock);
948         kfree(dev);
949 }
950
951 static struct usb_driver tm6000_usb_driver = {
952                 .name = "tm6000",
953                 .probe = tm6000_usb_probe,
954                 .disconnect = tm6000_usb_disconnect,
955                 .id_table = tm6000_id_table,
956 };
957
958 static int __init tm6000_module_init(void)
959 {
960         int result;
961
962         printk(KERN_INFO "tm6000" " v4l2 driver version %d.%d.%d loaded\n",
963                (TM6000_VERSION  >> 16) & 0xff,
964                (TM6000_VERSION  >> 8) & 0xff, TM6000_VERSION  & 0xff);
965
966         /* register this driver with the USB subsystem */
967         result = usb_register(&tm6000_usb_driver);
968         if (result)
969                 printk(KERN_ERR "tm6000"
970                            " usb_register failed. Error number %d.\n", result);
971
972         return result;
973 }
974
975 static void __exit tm6000_module_exit(void)
976 {
977         /* deregister at USB subsystem */
978         usb_deregister(&tm6000_usb_driver);
979 }
980
981 module_init(tm6000_module_init);
982 module_exit(tm6000_module_exit);
983
984 MODULE_DESCRIPTION("Trident TVMaster TM5600/TM6000/TM6010 USB2 adapter");
985 MODULE_AUTHOR("Mauro Carvalho Chehab");
986 MODULE_LICENSE("GPL");