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