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