[media] tm6000: add radio capabilities
[pandora-kernel.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 <linux/slab.h>
28 #include <media/v4l2-common.h>
29 #include <media/tuner.h>
30 #include <media/tvaudio.h>
31 #include <media/i2c-addr.h>
32 #include <media/rc-map.h>
33
34 #include "tm6000.h"
35 #include "tm6000-regs.h"
36 #include "tuner-xc2028.h"
37 #include "xc5000.h"
38
39 #define TM6000_BOARD_UNKNOWN                    0
40 #define TM5600_BOARD_GENERIC                    1
41 #define TM6000_BOARD_GENERIC                    2
42 #define TM6010_BOARD_GENERIC                    3
43 #define TM5600_BOARD_10MOONS_UT821              4
44 #define TM5600_BOARD_10MOONS_UT330              5
45 #define TM6000_BOARD_ADSTECH_DUAL_TV            6
46 #define TM6000_BOARD_FREECOM_AND_SIMILAR        7
47 #define TM6000_BOARD_ADSTECH_MINI_DUAL_TV       8
48 #define TM6010_BOARD_HAUPPAUGE_900H             9
49 #define TM6010_BOARD_BEHOLD_WANDER              10
50 #define TM6010_BOARD_BEHOLD_VOYAGER             11
51 #define TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE 12
52 #define TM6010_BOARD_TWINHAN_TU501              13
53 #define TM6010_BOARD_BEHOLD_WANDER_LITE         14
54 #define TM6010_BOARD_BEHOLD_VOYAGER_LITE        15
55 #define TM5600_BOARD_TERRATEC_GRABSTER          16
56
57 #define is_generic(model) ((model == TM6000_BOARD_UNKNOWN) || \
58                            (model == TM5600_BOARD_GENERIC) || \
59                            (model == TM6000_BOARD_GENERIC) || \
60                            (model == TM6010_BOARD_GENERIC))
61
62 #define TM6000_MAXBOARDS        16
63 static unsigned int card[]     = {[0 ... (TM6000_MAXBOARDS - 1)] = UNSET };
64
65 module_param_array(card,  int, NULL, 0444);
66
67 static unsigned long tm6000_devused;
68
69
70 struct tm6000_board {
71         char            *name;
72         char            eename[16];             /* EEPROM name */
73         unsigned        eename_size;            /* size of EEPROM name */
74         unsigned        eename_pos;             /* Position where it appears at ROM */
75
76         struct tm6000_capabilities caps;
77         enum            tm6000_inaudio aradio;
78         enum            tm6000_inaudio avideo;
79
80         enum            tm6000_devtype type;    /* variant of the chipset */
81         int             tuner_type;     /* type of the tuner */
82         int             tuner_addr;     /* tuner address */
83         int             demod_addr;     /* demodulator address */
84
85         struct tm6000_gpio gpio;
86
87         char            *ir_codes;
88 };
89
90 struct tm6000_board tm6000_boards[] = {
91         [TM6000_BOARD_UNKNOWN] = {
92                 .name         = "Unknown tm6000 video grabber",
93                 .caps = {
94                         .has_tuner    = 1,
95                 },
96                 .gpio = {
97                         .tuner_reset    = TM6000_GPIO_1,
98                 },
99         },
100         [TM5600_BOARD_GENERIC] = {
101                 .name         = "Generic tm5600 board",
102                 .type         = TM5600,
103                 .tuner_type   = TUNER_XC2028,
104                 .tuner_addr   = 0xc2 >> 1,
105                 .caps = {
106                         .has_tuner      = 1,
107                 },
108                 .gpio = {
109                         .tuner_reset    = TM6000_GPIO_1,
110                 },
111         },
112         [TM6000_BOARD_GENERIC] = {
113                 .name         = "Generic tm6000 board",
114                 .tuner_type   = TUNER_XC2028,
115                 .tuner_addr   = 0xc2 >> 1,
116                 .caps = {
117                         .has_tuner      = 1,
118                         .has_dvb        = 1,
119                 },
120                 .gpio = {
121                         .tuner_reset    = TM6000_GPIO_1,
122                 },
123         },
124         [TM6010_BOARD_GENERIC] = {
125                 .name         = "Generic tm6010 board",
126                 .type         = TM6010,
127                 .tuner_type   = TUNER_XC2028,
128                 .tuner_addr   = 0xc2 >> 1,
129                 .demod_addr   = 0x1e >> 1,
130                 .caps = {
131                         .has_tuner      = 1,
132                         .has_dvb        = 1,
133                         .has_zl10353    = 1,
134                         .has_eeprom     = 1,
135                         .has_remote     = 1,
136                 },
137                 .gpio = {
138                         .tuner_reset    = TM6010_GPIO_2,
139                         .tuner_on       = TM6010_GPIO_3,
140                         .demod_reset    = TM6010_GPIO_1,
141                         .demod_on       = TM6010_GPIO_4,
142                         .power_led      = TM6010_GPIO_7,
143                         .dvb_led        = TM6010_GPIO_5,
144                         .ir             = TM6010_GPIO_0,
145                 },
146         },
147         [TM5600_BOARD_10MOONS_UT821] = {
148                 .name         = "10Moons UT 821",
149                 .tuner_type   = TUNER_XC2028,
150                 .eename       = { '1', '0', 'M', 'O', 'O', 'N', 'S', '5', '6', '0', '0', 0xff, 0x45, 0x5b},
151                 .eename_size  = 14,
152                 .eename_pos   = 0x14,
153                 .type         = TM5600,
154                 .tuner_addr   = 0xc2 >> 1,
155                 .caps = {
156                         .has_tuner    = 1,
157                         .has_eeprom   = 1,
158                 },
159                 .gpio = {
160                         .tuner_reset    = TM6000_GPIO_1,
161                 },
162         },
163         [TM5600_BOARD_10MOONS_UT330] = {
164                 .name         = "10Moons UT 330",
165                 .tuner_type   = TUNER_PHILIPS_FQ1216AME_MK4,
166                 .tuner_addr   = 0xc8 >> 1,
167                 .caps = {
168                         .has_tuner    = 1,
169                         .has_dvb      = 0,
170                         .has_zl10353  = 0,
171                         .has_eeprom   = 1,
172                 },
173         },
174         [TM6000_BOARD_ADSTECH_DUAL_TV] = {
175                 .name         = "ADSTECH Dual TV USB",
176                 .tuner_type   = TUNER_XC2028,
177                 .tuner_addr   = 0xc8 >> 1,
178                 .caps = {
179                         .has_tuner    = 1,
180                         .has_tda9874  = 1,
181                         .has_dvb      = 1,
182                         .has_zl10353  = 1,
183                         .has_eeprom   = 1,
184                 },
185         },
186         [TM6000_BOARD_FREECOM_AND_SIMILAR] = {
187                 .name         = "Freecom Hybrid Stick / Moka DVB-T Receiver Dual",
188                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
189                 .tuner_addr   = 0xc2 >> 1,
190                 .demod_addr   = 0x1e >> 1,
191                 .caps = {
192                         .has_tuner    = 1,
193                         .has_dvb      = 1,
194                         .has_zl10353  = 1,
195                         .has_eeprom   = 0,
196                         .has_remote   = 1,
197                 },
198                 .gpio = {
199                         .tuner_reset    = TM6000_GPIO_4,
200                 },
201         },
202         [TM6000_BOARD_ADSTECH_MINI_DUAL_TV] = {
203                 .name         = "ADSTECH Mini Dual TV USB",
204                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
205                 .tuner_addr   = 0xc8 >> 1,
206                 .demod_addr   = 0x1e >> 1,
207                 .caps = {
208                         .has_tuner    = 1,
209                         .has_dvb      = 1,
210                         .has_zl10353  = 1,
211                         .has_eeprom   = 0,
212                 },
213                 .gpio = {
214                         .tuner_reset    = TM6000_GPIO_4,
215                 },
216         },
217         [TM6010_BOARD_HAUPPAUGE_900H] = {
218                 .name         = "Hauppauge WinTV HVR-900H / WinTV USB2-Stick",
219                 .eename       = { 'H', 0, 'V', 0, 'R', 0, '9', 0, '0', 0, '0', 0, 'H', 0 },
220                 .eename_size  = 14,
221                 .eename_pos   = 0x42,
222                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
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_2,
235                         .tuner_on       = TM6010_GPIO_3,
236                         .demod_reset    = TM6010_GPIO_1,
237                         .demod_on       = TM6010_GPIO_4,
238                         .power_led      = TM6010_GPIO_7,
239                         .dvb_led        = TM6010_GPIO_5,
240                         .ir             = TM6010_GPIO_0,
241                 },
242         },
243         [TM6010_BOARD_BEHOLD_WANDER] = {
244                 .name         = "Beholder Wander DVB-T/TV/FM USB2.0",
245                 .tuner_type   = TUNER_XC5000,
246                 .tuner_addr   = 0xc2 >> 1,
247                 .demod_addr   = 0x1e >> 1,
248                 .type         = TM6010,
249                 .avideo       = TM6000_AIP_SIF1,
250                 .aradio       = TM6000_AIP_LINE1,
251                 .caps = {
252                         .has_tuner      = 1,
253                         .has_dvb        = 1,
254                         .has_zl10353    = 1,
255                         .has_eeprom     = 1,
256                         .has_remote     = 1,
257                         .has_radio      = 1.
258                         .has_input_comp = 1,
259                         .has_input_svid = 1,
260                 },
261                 .gpio = {
262                         .tuner_reset    = TM6010_GPIO_0,
263                         .demod_reset    = TM6010_GPIO_1,
264                         .power_led      = TM6010_GPIO_6,
265                 },
266         },
267         [TM6010_BOARD_BEHOLD_VOYAGER] = {
268                 .name         = "Beholder Voyager TV/FM USB2.0",
269                 .tuner_type   = TUNER_XC5000,
270                 .tuner_addr   = 0xc2 >> 1,
271                 .type         = TM6010,
272                 .avideo       = TM6000_AIP_SIF1,
273                 .aradio       = TM6000_AIP_LINE1,
274                 .caps = {
275                         .has_tuner      = 1,
276                         .has_dvb        = 0,
277                         .has_zl10353    = 0,
278                         .has_eeprom     = 1,
279                         .has_remote     = 1,
280                         .has_radio      = 1,
281                         .has_input_comp = 1,
282                         .has_input_svid = 1,
283                 },
284                 .gpio = {
285                         .tuner_reset    = TM6010_GPIO_0,
286                         .power_led      = TM6010_GPIO_6,
287                 },
288         },
289         [TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE] = {
290                 .name         = "Terratec Cinergy Hybrid XE / Cinergy Hybrid-Stick",
291                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
292                 .tuner_addr   = 0xc2 >> 1,
293                 .demod_addr   = 0x1e >> 1,
294                 .type         = TM6010,
295                 .caps = {
296                         .has_tuner    = 1,
297                         .has_dvb      = 1,
298                         .has_zl10353  = 1,
299                         .has_eeprom   = 1,
300                         .has_remote   = 1,
301                 },
302                 .gpio = {
303                         .tuner_reset    = TM6010_GPIO_2,
304                         .tuner_on       = TM6010_GPIO_3,
305                         .demod_reset    = TM6010_GPIO_1,
306                         .demod_on       = TM6010_GPIO_4,
307                         .power_led      = TM6010_GPIO_7,
308                         .dvb_led        = TM6010_GPIO_5,
309                         .ir             = TM6010_GPIO_0,
310                 },
311                 .ir_codes = RC_MAP_NEC_TERRATEC_CINERGY_XS,
312         },
313         [TM5600_BOARD_TERRATEC_GRABSTER] = {
314                 .name         = "Terratec Grabster AV 150/250 MX",
315                 .type         = TM5600,
316                 .tuner_type   = TUNER_ABSENT,
317         },
318         [TM6010_BOARD_TWINHAN_TU501] = {
319                 .name         = "Twinhan TU501(704D1)",
320                 .tuner_type   = TUNER_XC2028, /* has a XC3028 */
321                 .tuner_addr   = 0xc2 >> 1,
322                 .demod_addr   = 0x1e >> 1,
323                 .type         = TM6010,
324                 .caps = {
325                         .has_tuner    = 1,
326                         .has_dvb      = 1,
327                         .has_zl10353  = 1,
328                         .has_eeprom   = 1,
329                         .has_remote   = 1,
330                 },
331                 .gpio = {
332                         .tuner_reset    = TM6010_GPIO_2,
333                         .tuner_on       = TM6010_GPIO_3,
334                         .demod_reset    = TM6010_GPIO_1,
335                         .demod_on       = TM6010_GPIO_4,
336                         .power_led      = TM6010_GPIO_7,
337                         .dvb_led        = TM6010_GPIO_5,
338                         .ir             = TM6010_GPIO_0,
339                 },
340         },
341         [TM6010_BOARD_BEHOLD_WANDER_LITE] = {
342                 .name         = "Beholder Wander Lite DVB-T/TV/FM USB2.0",
343                 .tuner_type   = TUNER_XC5000,
344                 .tuner_addr   = 0xc2 >> 1,
345                 .demod_addr   = 0x1e >> 1,
346                 .type         = TM6010,
347                 .avideo       = TM6000_AIP_SIF1,
348                 .aradio       = TM6000_AIP_LINE1,
349                 .caps = {
350                         .has_tuner      = 1,
351                         .has_dvb        = 1,
352                         .has_zl10353    = 1,
353                         .has_eeprom     = 1,
354                         .has_remote     = 0,
355                         .has_radio      = 1,
356                         .has_input_comp = 0,
357                         .has_input_svid = 0,
358                 },
359                 .gpio = {
360                         .tuner_reset    = TM6010_GPIO_0,
361                         .demod_reset    = TM6010_GPIO_1,
362                         .power_led      = TM6010_GPIO_6,
363                 },
364         },
365         [TM6010_BOARD_BEHOLD_VOYAGER_LITE] = {
366                 .name         = "Beholder Voyager Lite TV/FM USB2.0",
367                 .tuner_type   = TUNER_XC5000,
368                 .tuner_addr   = 0xc2 >> 1,
369                 .type         = TM6010,
370                 .avideo       = TM6000_AIP_SIF1,
371                 .aradio       = TM6000_AIP_LINE1,
372                 .caps = {
373                         .has_tuner      = 1,
374                         .has_dvb        = 0,
375                         .has_zl10353    = 0,
376                         .has_eeprom     = 1,
377                         .has_remote     = 0,
378                         .has_radio      = 1,
379                         .has_input_comp = 0,
380                         .has_input_svid = 0,
381                 },
382                 .gpio = {
383                         .tuner_reset    = TM6010_GPIO_0,
384                         .power_led      = TM6010_GPIO_6,
385                 },
386         },
387 };
388
389 /* table of devices that work with this driver */
390 struct usb_device_id tm6000_id_table[] = {
391         { USB_DEVICE(0x6000, 0x0001), .driver_info = TM5600_BOARD_GENERIC },
392         { USB_DEVICE(0x6000, 0x0002), .driver_info = TM6010_BOARD_GENERIC },
393         { USB_DEVICE(0x06e1, 0xf332), .driver_info = TM6000_BOARD_ADSTECH_DUAL_TV },
394         { USB_DEVICE(0x14aa, 0x0620), .driver_info = TM6000_BOARD_FREECOM_AND_SIMILAR },
395         { USB_DEVICE(0x06e1, 0xb339), .driver_info = TM6000_BOARD_ADSTECH_MINI_DUAL_TV },
396         { USB_DEVICE(0x2040, 0x6600), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
397         { USB_DEVICE(0x2040, 0x6601), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
398         { USB_DEVICE(0x2040, 0x6610), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
399         { USB_DEVICE(0x2040, 0x6611), .driver_info = TM6010_BOARD_HAUPPAUGE_900H },
400         { USB_DEVICE(0x6000, 0xdec0), .driver_info = TM6010_BOARD_BEHOLD_WANDER },
401         { USB_DEVICE(0x6000, 0xdec1), .driver_info = TM6010_BOARD_BEHOLD_VOYAGER },
402         { USB_DEVICE(0x0ccd, 0x0086), .driver_info = TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE },
403         { USB_DEVICE(0x0ccd, 0x00A5), .driver_info = TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE },
404         { USB_DEVICE(0x0ccd, 0x0079), .driver_info = TM5600_BOARD_TERRATEC_GRABSTER },
405         { USB_DEVICE(0x13d3, 0x3240), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
406         { USB_DEVICE(0x13d3, 0x3241), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
407         { USB_DEVICE(0x13d3, 0x3243), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
408         { USB_DEVICE(0x13d3, 0x3264), .driver_info = TM6010_BOARD_TWINHAN_TU501 },
409         { USB_DEVICE(0x6000, 0xdec2), .driver_info = TM6010_BOARD_BEHOLD_WANDER_LITE },
410         { USB_DEVICE(0x6000, 0xdec3), .driver_info = TM6010_BOARD_BEHOLD_VOYAGER_LITE },
411         { },
412 };
413
414 /* Control power led for show some activity */
415 void tm6000_flash_led(struct tm6000_core *dev, u8 state)
416 {
417         /* Power LED unconfigured */
418         if (!dev->gpio.power_led)
419                 return;
420
421         /* ON Power LED */
422         if (state) {
423                 switch (dev->model) {
424                 case TM6010_BOARD_HAUPPAUGE_900H:
425                 case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
426                 case TM6010_BOARD_TWINHAN_TU501:
427                         tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
428                                 dev->gpio.power_led, 0x00);
429                         break;
430                 case TM6010_BOARD_BEHOLD_WANDER:
431                 case TM6010_BOARD_BEHOLD_VOYAGER:
432                 case TM6010_BOARD_BEHOLD_WANDER_LITE:
433                 case TM6010_BOARD_BEHOLD_VOYAGER_LITE:
434                         tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
435                                 dev->gpio.power_led, 0x01);
436                         break;
437                 }
438         }
439         /* OFF Power LED */
440         else {
441                 switch (dev->model) {
442                 case TM6010_BOARD_HAUPPAUGE_900H:
443                 case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
444                 case TM6010_BOARD_TWINHAN_TU501:
445                         tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
446                                 dev->gpio.power_led, 0x01);
447                         break;
448                 case TM6010_BOARD_BEHOLD_WANDER:
449                 case TM6010_BOARD_BEHOLD_VOYAGER:
450                 case TM6010_BOARD_BEHOLD_WANDER_LITE:
451                 case TM6010_BOARD_BEHOLD_VOYAGER_LITE:
452                         tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
453                                 dev->gpio.power_led, 0x00);
454                         break;
455                 }
456         }
457 }
458
459 /* Tuner callback to provide the proper gpio changes needed for xc5000 */
460 int tm6000_xc5000_callback(void *ptr, int component, int command, int arg)
461 {
462         int rc = 0;
463         struct tm6000_core *dev = ptr;
464
465         if (dev->tuner_type != TUNER_XC5000)
466                 return 0;
467
468         switch (command) {
469         case XC5000_TUNER_RESET:
470                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
471                                dev->gpio.tuner_reset, 0x01);
472                 msleep(15);
473                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
474                                dev->gpio.tuner_reset, 0x00);
475                 msleep(15);
476                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
477                                dev->gpio.tuner_reset, 0x01);
478                 break;
479         }
480         return rc;
481 }
482 EXPORT_SYMBOL_GPL(tm6000_xc5000_callback);
483
484 /* Tuner callback to provide the proper gpio changes needed for xc2028 */
485
486 int tm6000_tuner_callback(void *ptr, int component, int command, int arg)
487 {
488         int rc = 0;
489         struct tm6000_core *dev = ptr;
490
491         if (dev->tuner_type != TUNER_XC2028)
492                 return 0;
493
494         switch (command) {
495         case XC2028_RESET_CLK:
496                 tm6000_ir_wait(dev, 0);
497
498                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT,
499                                         0x02, arg);
500                 msleep(10);
501                 rc = tm6000_i2c_reset(dev, 10);
502                 break;
503         case XC2028_TUNER_RESET:
504                 /* Reset codes during load firmware */
505                 switch (arg) {
506                 case 0:
507                         /* newer tuner can faster reset */
508                         switch (dev->model) {
509                         case TM5600_BOARD_10MOONS_UT821:
510                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
511                                                dev->gpio.tuner_reset, 0x01);
512                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
513                                                0x300, 0x01);
514                                 msleep(10);
515                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
516                                                dev->gpio.tuner_reset, 0x00);
517                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
518                                                0x300, 0x00);
519                                 msleep(10);
520                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
521                                                dev->gpio.tuner_reset, 0x01);
522                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
523                                                0x300, 0x01);
524                                 break;
525                         case TM6010_BOARD_HAUPPAUGE_900H:
526                         case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
527                         case TM6010_BOARD_TWINHAN_TU501:
528                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
529                                                dev->gpio.tuner_reset, 0x01);
530                                 msleep(60);
531                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
532                                                dev->gpio.tuner_reset, 0x00);
533                                 msleep(75);
534                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
535                                                dev->gpio.tuner_reset, 0x01);
536                                 msleep(60);
537                                 break;
538                         default:
539                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
540                                                dev->gpio.tuner_reset, 0x00);
541                                 msleep(130);
542                                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
543                                                dev->gpio.tuner_reset, 0x01);
544                                 msleep(130);
545                                 break;
546                         }
547
548                         tm6000_ir_wait(dev, 1);
549                         break;
550                 case 1:
551                         tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT,
552                                                 0x02, 0x01);
553                         msleep(10);
554                         break;
555                 case 2:
556                         rc = tm6000_i2c_reset(dev, 100);
557                         break;
558                 }
559         }
560         return rc;
561 }
562 EXPORT_SYMBOL_GPL(tm6000_tuner_callback);
563
564 int tm6000_cards_setup(struct tm6000_core *dev)
565 {
566         int i, rc;
567
568         /*
569          * Board-specific initialization sequence. Handles all GPIO
570          * initialization sequences that are board-specific.
571          * Up to now, all found devices use GPIO1 and GPIO4 at the same way.
572          * Probably, they're all based on some reference device. Due to that,
573          * there's a common routine at the end to handle those GPIO's. Devices
574          * that use different pinups or init sequences can just return at
575          * the board-specific session.
576          */
577         switch (dev->model) {
578         case TM6010_BOARD_HAUPPAUGE_900H:
579         case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
580         case TM6010_BOARD_TWINHAN_TU501:
581         case TM6010_BOARD_GENERIC:
582                 /* Turn xceive 3028 on */
583                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.tuner_on, 0x01);
584                 msleep(15);
585                 /* Turn zarlink zl10353 on */
586                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_on, 0x00);
587                 msleep(15);
588                 /* Reset zarlink zl10353 */
589                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_reset, 0x00);
590                 msleep(50);
591                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_reset, 0x01);
592                 msleep(15);
593                 /* Turn zarlink zl10353 off */
594                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_on, 0x01);
595                 msleep(15);
596                 /* ir ? */
597                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.ir, 0x01);
598                 msleep(15);
599                 /* Power led on (blue) */
600                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.power_led, 0x00);
601                 msleep(15);
602                 /* DVB led off (orange) */
603                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.dvb_led, 0x01);
604                 msleep(15);
605                 /* Turn zarlink zl10353 on */
606                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_on, 0x00);
607                 msleep(15);
608                 break;
609         case TM6010_BOARD_BEHOLD_WANDER:
610         case TM6010_BOARD_BEHOLD_WANDER_LITE:
611                 /* Power led on (blue) */
612                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.power_led, 0x01);
613                 msleep(15);
614                 /* Reset zarlink zl10353 */
615                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_reset, 0x00);
616                 msleep(50);
617                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.demod_reset, 0x01);
618                 msleep(15);
619                 break;
620         case TM6010_BOARD_BEHOLD_VOYAGER:
621         case TM6010_BOARD_BEHOLD_VOYAGER_LITE:
622                 /* Power led on (blue) */
623                 tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN, dev->gpio.power_led, 0x01);
624                 msleep(15);
625                 break;
626         default:
627                 break;
628         }
629
630         /*
631          * Default initialization. Most of the devices seem to use GPIO1
632          * and GPIO4.on the same way, so, this handles the common sequence
633          * used by most devices.
634          * If a device uses a different sequence or different GPIO pins for
635          * reset, just add the code at the board-specific part
636          */
637
638         if (dev->gpio.tuner_reset) {
639                 for (i = 0; i < 2; i++) {
640                         rc = tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
641                                                 dev->gpio.tuner_reset, 0x00);
642                         if (rc < 0) {
643                                 printk(KERN_ERR "Error %i doing tuner reset\n", rc);
644                                 return rc;
645                         }
646
647                         msleep(10); /* Just to be conservative */
648                         rc = tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
649                                                 dev->gpio.tuner_reset, 0x01);
650                         if (rc < 0) {
651                                 printk(KERN_ERR "Error %i doing tuner reset\n", rc);
652                                 return rc;
653                         }
654                 }
655         } else {
656                 printk(KERN_ERR "Tuner reset is not configured\n");
657                 return -1;
658         }
659
660         msleep(50);
661
662         return 0;
663 };
664
665 static void tm6000_config_tuner(struct tm6000_core *dev)
666 {
667         struct tuner_setup tun_setup;
668
669         /* Load tuner module */
670         v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
671                 "tuner", dev->tuner_addr, NULL);
672
673         memset(&tun_setup, 0, sizeof(tun_setup));
674         tun_setup.type = dev->tuner_type;
675         tun_setup.addr = dev->tuner_addr;
676
677         tun_setup.mode_mask = 0;
678         if (dev->caps.has_tuner)
679                 tun_setup.mode_mask |= (T_ANALOG_TV | T_RADIO);
680
681         switch (dev->tuner_type) {
682         case TUNER_XC2028:
683                 tun_setup.tuner_callback = tm6000_tuner_callback;
684                 break;
685         case TUNER_XC5000:
686                 tun_setup.tuner_callback = tm6000_xc5000_callback;
687                 break;
688         }
689
690         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_type_addr, &tun_setup);
691
692         switch (dev->tuner_type) {
693         case TUNER_XC2028: {
694                 struct v4l2_priv_tun_config xc2028_cfg;
695                 struct xc2028_ctrl ctl;
696
697                 memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
698                 memset(&ctl, 0, sizeof(ctl));
699
700                 ctl.input1 = 1;
701                 ctl.read_not_reliable = 0;
702                 ctl.msleep = 10;
703                 ctl.demod = XC3028_FE_ZARLINK456;
704                 ctl.vhfbw7 = 1;
705                 ctl.uhfbw8 = 1;
706                 xc2028_cfg.tuner = TUNER_XC2028;
707                 xc2028_cfg.priv  = &ctl;
708
709                 switch (dev->model) {
710                 case TM6010_BOARD_HAUPPAUGE_900H:
711                 case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
712                 case TM6010_BOARD_TWINHAN_TU501:
713                         ctl.fname = "xc3028L-v36.fw";
714                         break;
715                 default:
716                         if (dev->dev_type == TM6010)
717                                 ctl.fname = "xc3028-v27.fw";
718                         else
719                                 ctl.fname = "xc3028-v24.fw";
720                 }
721
722                 printk(KERN_INFO "Setting firmware parameters for xc2028\n");
723                 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_config,
724                                      &xc2028_cfg);
725
726                 }
727                 break;
728         case TUNER_XC5000:
729                 {
730                 struct v4l2_priv_tun_config  xc5000_cfg;
731                 struct xc5000_config ctl = {
732                         .i2c_address = dev->tuner_addr,
733                         .if_khz      = 4570,
734                         .radio_input = XC5000_RADIO_FM1_MONO,
735                         };
736
737                 xc5000_cfg.tuner = TUNER_XC5000;
738                 xc5000_cfg.priv  = &ctl;
739
740                 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_config,
741                                      &xc5000_cfg);
742                 }
743                 break;
744         default:
745                 printk(KERN_INFO "Unknown tuner type. Tuner is not configured.\n");
746                 break;
747         }
748 }
749
750 static int fill_board_specific_data(struct tm6000_core *dev)
751 {
752         int rc;
753
754         dev->dev_type   = tm6000_boards[dev->model].type;
755         dev->tuner_type = tm6000_boards[dev->model].tuner_type;
756         dev->tuner_addr = tm6000_boards[dev->model].tuner_addr;
757
758         dev->gpio = tm6000_boards[dev->model].gpio;
759
760         dev->ir_codes = tm6000_boards[dev->model].ir_codes;
761
762         dev->demod_addr = tm6000_boards[dev->model].demod_addr;
763
764         dev->caps = tm6000_boards[dev->model].caps;
765
766         dev->avideo = tm6000_boards[dev->model].avideo;
767         dev->aradio = tm6000_boards[dev->model].aradio;
768         /* initialize hardware */
769         rc = tm6000_init(dev);
770         if (rc < 0)
771                 return rc;
772
773         rc = v4l2_device_register(&dev->udev->dev, &dev->v4l2_dev);
774         if (rc < 0)
775                 return rc;
776
777         /* initialize hardware */
778         rc = tm6000_init(dev);
779
780         return rc;
781 }
782
783
784 static void use_alternative_detection_method(struct tm6000_core *dev)
785 {
786         int i, model = -1;
787
788         if (!dev->eedata_size)
789                 return;
790
791         for (i = 0; i < ARRAY_SIZE(tm6000_boards); i++) {
792                 if (!tm6000_boards[i].eename_size)
793                         continue;
794                 if (dev->eedata_size < tm6000_boards[i].eename_pos +
795                                        tm6000_boards[i].eename_size)
796                         continue;
797
798                 if (!memcmp(&dev->eedata[tm6000_boards[i].eename_pos],
799                             tm6000_boards[i].eename,
800                             tm6000_boards[i].eename_size)) {
801                         model = i;
802                         break;
803                 }
804         }
805         if (model < 0) {
806                 printk(KERN_INFO "Device has eeprom but is currently unknown\n");
807                 return;
808         }
809
810         dev->model = model;
811
812         printk(KERN_INFO "Device identified via eeprom as %s (type = %d)\n",
813                tm6000_boards[model].name, model);
814 }
815
816 static int tm6000_init_dev(struct tm6000_core *dev)
817 {
818         struct v4l2_frequency f;
819         int rc = 0;
820
821         mutex_init(&dev->lock);
822         mutex_lock(&dev->lock);
823
824         if (!is_generic(dev->model)) {
825                 rc = fill_board_specific_data(dev);
826                 if (rc < 0)
827                         goto err;
828
829                 /* register i2c bus */
830                 rc = tm6000_i2c_register(dev);
831                 if (rc < 0)
832                         goto err;
833         } else {
834                 /* register i2c bus */
835                 rc = tm6000_i2c_register(dev);
836                 if (rc < 0)
837                         goto err;
838
839                 use_alternative_detection_method(dev);
840
841                 rc = fill_board_specific_data(dev);
842                 if (rc < 0)
843                         goto err;
844         }
845
846         /* Default values for STD and resolutions */
847         dev->width = 720;
848         dev->height = 480;
849         dev->norm = V4L2_STD_PAL_M;
850
851         /* Configure tuner */
852         tm6000_config_tuner(dev);
853
854         /* Set video standard */
855         v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
856
857         /* Set tuner frequency - also loads firmware on xc2028/xc3028 */
858         f.tuner = 0;
859         f.type = V4L2_TUNER_ANALOG_TV;
860         f.frequency = 3092;     /* 193.25 MHz */
861         dev->freq = f.frequency;
862         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
863
864         if (dev->caps.has_tda9874)
865                 v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
866                         "tvaudio", I2C_ADDR_TDA9874, NULL);
867
868         /* register and initialize V4L2 */
869         rc = tm6000_v4l2_register(dev);
870         if (rc < 0)
871                 goto err;
872
873         tm6000_add_into_devlist(dev);
874         tm6000_init_extension(dev);
875
876         tm6000_ir_init(dev);
877
878         mutex_unlock(&dev->lock);
879         return 0;
880
881 err:
882         mutex_unlock(&dev->lock);
883         return rc;
884 }
885
886 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
887 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
888
889 static void get_max_endpoint(struct usb_device *udev,
890                              struct usb_host_interface *alt,
891                              char *msgtype,
892                              struct usb_host_endpoint *curr_e,
893                              struct tm6000_endpoint *tm_ep)
894 {
895         u16 tmp = le16_to_cpu(curr_e->desc.wMaxPacketSize);
896         unsigned int size = tmp & 0x7ff;
897
898         if (udev->speed == USB_SPEED_HIGH)
899                 size = size * hb_mult(tmp);
900
901         if (size > tm_ep->maxsize) {
902                 tm_ep->endp = curr_e;
903                 tm_ep->maxsize = size;
904                 tm_ep->bInterfaceNumber = alt->desc.bInterfaceNumber;
905                 tm_ep->bAlternateSetting = alt->desc.bAlternateSetting;
906
907                 printk(KERN_INFO "tm6000: %s endpoint: 0x%02x (max size=%u bytes)\n",
908                                         msgtype, curr_e->desc.bEndpointAddress,
909                                         size);
910         }
911 }
912
913 /*
914  * tm6000_usb_probe()
915  * checks for supported devices
916  */
917 static int tm6000_usb_probe(struct usb_interface *interface,
918                             const struct usb_device_id *id)
919 {
920         struct usb_device *usbdev;
921         struct tm6000_core *dev = NULL;
922         int i, rc = 0;
923         int nr = 0;
924         char *speed;
925
926         usbdev = usb_get_dev(interface_to_usbdev(interface));
927
928         /* Selects the proper interface */
929         rc = usb_set_interface(usbdev, 0, 1);
930         if (rc < 0)
931                 goto err;
932
933         /* Check to see next free device and mark as used */
934         nr = find_first_zero_bit(&tm6000_devused, TM6000_MAXBOARDS);
935         if (nr >= TM6000_MAXBOARDS) {
936                 printk(KERN_ERR "tm6000: Supports only %i tm60xx boards.\n", TM6000_MAXBOARDS);
937                 usb_put_dev(usbdev);
938                 return -ENOMEM;
939         }
940
941         /* Create and initialize dev struct */
942         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
943         if (dev == NULL) {
944                 printk(KERN_ERR "tm6000" ": out of memory!\n");
945                 usb_put_dev(usbdev);
946                 return -ENOMEM;
947         }
948         spin_lock_init(&dev->slock);
949
950         /* Increment usage count */
951         tm6000_devused |= 1<<nr;
952         snprintf(dev->name, 29, "tm6000 #%d", nr);
953
954         dev->model = id->driver_info;
955         if ((card[nr] >= 0) && (card[nr] < ARRAY_SIZE(tm6000_boards)))
956                 dev->model = card[nr];
957
958         dev->udev = usbdev;
959         dev->devno = nr;
960
961         switch (usbdev->speed) {
962         case USB_SPEED_LOW:
963                 speed = "1.5";
964                 break;
965         case USB_SPEED_UNKNOWN:
966         case USB_SPEED_FULL:
967                 speed = "12";
968                 break;
969         case USB_SPEED_HIGH:
970                 speed = "480";
971                 break;
972         default:
973                 speed = "unknown";
974         }
975
976
977
978         /* Get endpoints */
979         for (i = 0; i < interface->num_altsetting; i++) {
980                 int ep;
981
982                 for (ep = 0; ep < interface->altsetting[i].desc.bNumEndpoints; ep++) {
983                         struct usb_host_endpoint        *e;
984                         int dir_out;
985
986                         e = &interface->altsetting[i].endpoint[ep];
987
988                         dir_out = ((e->desc.bEndpointAddress &
989                                         USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
990
991                         printk(KERN_INFO "tm6000: alt %d, interface %i, class %i\n",
992                                i,
993                                interface->altsetting[i].desc.bInterfaceNumber,
994                                interface->altsetting[i].desc.bInterfaceClass);
995
996                         switch (e->desc.bmAttributes) {
997                         case USB_ENDPOINT_XFER_BULK:
998                                 if (!dir_out) {
999                                         get_max_endpoint(usbdev,
1000                                                          &interface->altsetting[i],
1001                                                          "Bulk IN", e,
1002                                                          &dev->bulk_in);
1003                                 } else {
1004                                         get_max_endpoint(usbdev,
1005                                                          &interface->altsetting[i],
1006                                                          "Bulk OUT", e,
1007                                                          &dev->bulk_out);
1008                                 }
1009                                 break;
1010                         case USB_ENDPOINT_XFER_ISOC:
1011                                 if (!dir_out) {
1012                                         get_max_endpoint(usbdev,
1013                                                          &interface->altsetting[i],
1014                                                          "ISOC IN", e,
1015                                                          &dev->isoc_in);
1016                                 } else {
1017                                         get_max_endpoint(usbdev,
1018                                                          &interface->altsetting[i],
1019                                                          "ISOC OUT", e,
1020                                                          &dev->isoc_out);
1021                                 }
1022                                 break;
1023                         case USB_ENDPOINT_XFER_INT:
1024                                 if (!dir_out) {
1025                                         get_max_endpoint(usbdev,
1026                                                         &interface->altsetting[i],
1027                                                         "INT IN", e,
1028                                                         &dev->int_in);
1029                                 } else {
1030                                         get_max_endpoint(usbdev,
1031                                                         &interface->altsetting[i],
1032                                                         "INT OUT", e,
1033                                                         &dev->int_out);
1034                                 }
1035                                 break;
1036                         }
1037                 }
1038         }
1039
1040
1041         printk(KERN_INFO "tm6000: New video device @ %s Mbps (%04x:%04x, ifnum %d)\n",
1042                 speed,
1043                 le16_to_cpu(dev->udev->descriptor.idVendor),
1044                 le16_to_cpu(dev->udev->descriptor.idProduct),
1045                 interface->altsetting->desc.bInterfaceNumber);
1046
1047 /* check if the the device has the iso in endpoint at the correct place */
1048         if (!dev->isoc_in.endp) {
1049                 printk(KERN_ERR "tm6000: probing error: no IN ISOC endpoint!\n");
1050                 rc = -ENODEV;
1051
1052                 goto err;
1053         }
1054
1055         /* save our data pointer in this interface device */
1056         usb_set_intfdata(interface, dev);
1057
1058         printk(KERN_INFO "tm6000: Found %s\n", tm6000_boards[dev->model].name);
1059
1060         rc = tm6000_init_dev(dev);
1061
1062         if (rc < 0)
1063                 goto err;
1064
1065         return 0;
1066
1067 err:
1068         printk(KERN_ERR "tm6000: Error %d while registering\n", rc);
1069
1070         tm6000_devused &= ~(1<<nr);
1071         usb_put_dev(usbdev);
1072
1073         kfree(dev);
1074         return rc;
1075 }
1076
1077 /*
1078  * tm6000_usb_disconnect()
1079  * called when the device gets diconencted
1080  * video device will be unregistered on v4l2_close in case it is still open
1081  */
1082 static void tm6000_usb_disconnect(struct usb_interface *interface)
1083 {
1084         struct tm6000_core *dev = usb_get_intfdata(interface);
1085         usb_set_intfdata(interface, NULL);
1086
1087         if (!dev)
1088                 return;
1089
1090         printk(KERN_INFO "tm6000: disconnecting %s\n", dev->name);
1091
1092         tm6000_ir_fini(dev);
1093
1094         if (dev->gpio.power_led) {
1095                 switch (dev->model) {
1096                 case TM6010_BOARD_HAUPPAUGE_900H:
1097                 case TM6010_BOARD_TERRATEC_CINERGY_HYBRID_XE:
1098                 case TM6010_BOARD_TWINHAN_TU501:
1099                         /* Power led off */
1100                         tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
1101                                 dev->gpio.power_led, 0x01);
1102                         msleep(15);
1103                         break;
1104                 case TM6010_BOARD_BEHOLD_WANDER:
1105                 case TM6010_BOARD_BEHOLD_VOYAGER:
1106                 case TM6010_BOARD_BEHOLD_WANDER_LITE:
1107                 case TM6010_BOARD_BEHOLD_VOYAGER_LITE:
1108                         /* Power led off */
1109                         tm6000_set_reg(dev, REQ_03_SET_GET_MCU_PIN,
1110                                 dev->gpio.power_led, 0x00);
1111                         msleep(15);
1112                         break;
1113                 }
1114         }
1115         tm6000_v4l2_unregister(dev);
1116
1117         tm6000_i2c_unregister(dev);
1118
1119         v4l2_device_unregister(&dev->v4l2_dev);
1120
1121         dev->state |= DEV_DISCONNECTED;
1122
1123         usb_put_dev(dev->udev);
1124
1125         tm6000_close_extension(dev);
1126         tm6000_remove_from_devlist(dev);
1127
1128         kfree(dev);
1129 }
1130
1131 static struct usb_driver tm6000_usb_driver = {
1132                 .name = "tm6000",
1133                 .probe = tm6000_usb_probe,
1134                 .disconnect = tm6000_usb_disconnect,
1135                 .id_table = tm6000_id_table,
1136 };
1137
1138 static int __init tm6000_module_init(void)
1139 {
1140         int result;
1141
1142         printk(KERN_INFO "tm6000" " v4l2 driver version %d.%d.%d loaded\n",
1143                (TM6000_VERSION  >> 16) & 0xff,
1144                (TM6000_VERSION  >> 8) & 0xff, TM6000_VERSION  & 0xff);
1145
1146         /* register this driver with the USB subsystem */
1147         result = usb_register(&tm6000_usb_driver);
1148         if (result)
1149                 printk(KERN_ERR "tm6000"
1150                            " usb_register failed. Error number %d.\n", result);
1151
1152         return result;
1153 }
1154
1155 static void __exit tm6000_module_exit(void)
1156 {
1157         /* deregister at USB subsystem */
1158         usb_deregister(&tm6000_usb_driver);
1159 }
1160
1161 module_init(tm6000_module_init);
1162 module_exit(tm6000_module_exit);
1163
1164 MODULE_DESCRIPTION("Trident TVMaster TM5600/TM6000/TM6010 USB2 adapter");
1165 MODULE_AUTHOR("Mauro Carvalho Chehab");
1166 MODULE_LICENSE("GPL");