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