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