4cd49415eef20db960c81dc3d22fb04284b73b47
[safe/jmp/linux-2.6] / drivers / media / video / em28xx / em28xx-cards.c
1 /*
2    em28xx-cards.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
3
4    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5                       Markus Rechberger <mrechberger@gmail.com>
6                       Mauro Carvalho Chehab <mchehab@infradead.org>
7                       Sascha Sommer <saschasommer@freenet.de>
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/delay.h>
27 #include <linux/i2c.h>
28 #include <linux/usb.h>
29 #include <media/tuner.h>
30 #include <media/msp3400.h>
31 #include <media/saa7115.h>
32 #include <media/tvp5150.h>
33 #include <media/tveeprom.h>
34 #include <media/audiochip.h>
35 #include <media/v4l2-common.h>
36
37 #include "em28xx.h"
38 #include "tuner-xc2028.h"
39
40 static int tuner = -1;
41 module_param(tuner, int, 0444);
42 MODULE_PARM_DESC(tuner, "tuner type");
43
44 struct em28xx_hash_table {
45         unsigned long hash;
46         unsigned int  model;
47         unsigned int  tuner;
48 };
49
50 struct em28xx_board em28xx_boards[] = {
51         [EM2800_BOARD_UNKNOWN] = {
52                 .name         = "Unknown EM2800 video grabber",
53                 .is_em2800    = 1,
54                 .vchannels    = 2,
55                 .norm         = VIDEO_MODE_PAL,
56                 .tda9887_conf = TDA9887_PRESENT,
57                 .has_tuner    = 1,
58                 .decoder      = EM28XX_SAA7113,
59                 .input           = {{
60                         .type     = EM28XX_VMUX_COMPOSITE1,
61                         .vmux     = SAA7115_COMPOSITE0,
62                         .amux     = 1,
63                 },{
64                         .type     = EM28XX_VMUX_SVIDEO,
65                         .vmux     = SAA7115_SVIDEO3,
66                         .amux     = 1,
67                 }},
68         },
69         [EM2820_BOARD_UNKNOWN] = {
70                 .name         = "Unknown EM2750/28xx video grabber",
71                 .is_em2800    = 0,
72         },
73         [EM2820_BOARD_KWORLD_PVRTV2800RF] = {
74                 .name         = "Kworld PVR TV 2800 RF",
75                 .is_em2800    = 0,
76                 .vchannels    = 2,
77                 .norm         = VIDEO_MODE_PAL,
78                 .tda9887_conf = TDA9887_PRESENT,
79                 .has_tuner    = 1,
80                 .decoder      = EM28XX_SAA7113,
81                 .input           = {{
82                         .type     = EM28XX_VMUX_COMPOSITE1,
83                         .vmux     = SAA7115_COMPOSITE0,
84                         .amux     = 1,
85                 },{
86                         .type     = EM28XX_VMUX_SVIDEO,
87                         .vmux     = SAA7115_SVIDEO3,
88                         .amux     = 1,
89                 }},
90         },
91         [EM2820_BOARD_TERRATEC_CINERGY_250] = {
92                 .name         = "Terratec Cinergy 250 USB",
93                 .vchannels    = 3,
94                 .norm         = VIDEO_MODE_PAL,
95                 .tuner_type   = TUNER_LG_PAL_NEW_TAPC,
96                 .tda9887_conf = TDA9887_PRESENT,
97                 .has_tuner    = 1,
98                 .decoder      = EM28XX_SAA7113,
99                 .input          = {{
100                         .type     = EM28XX_VMUX_TELEVISION,
101                         .vmux     = SAA7115_COMPOSITE2,
102                         .amux     = 1,
103                 },{
104                         .type     = EM28XX_VMUX_COMPOSITE1,
105                         .vmux     = SAA7115_COMPOSITE0,
106                         .amux     = 1,
107                 },{
108                         .type     = EM28XX_VMUX_SVIDEO,
109                         .vmux     = SAA7115_SVIDEO3,
110                         .amux     = 1,
111                 }},
112         },
113         [EM2820_BOARD_PINNACLE_USB_2] = {
114                 .name         = "Pinnacle PCTV USB 2",
115                 .vchannels    = 3,
116                 .norm         = VIDEO_MODE_PAL,
117                 .tuner_type   = TUNER_LG_PAL_NEW_TAPC,
118                 .tda9887_conf = TDA9887_PRESENT,
119                 .has_tuner    = 1,
120                 .decoder      = EM28XX_SAA7113,
121                 .input          = {{
122                         .type     = EM28XX_VMUX_TELEVISION,
123                         .vmux     = SAA7115_COMPOSITE2,
124                         .amux     = 0,
125                 },{
126                         .type     = EM28XX_VMUX_COMPOSITE1,
127                         .vmux     = SAA7115_COMPOSITE0,
128                         .amux     = 1,
129                 },{
130                         .type     = EM28XX_VMUX_SVIDEO,
131                         .vmux     = SAA7115_SVIDEO3,
132                         .amux     = 1,
133                 }},
134         },
135         [EM2820_BOARD_HAUPPAUGE_WINTV_USB_2] = {
136                 .name         = "Hauppauge WinTV USB 2",
137                 .vchannels    = 3,
138                 .norm         = VIDEO_MODE_NTSC,
139                 .tuner_type   = TUNER_PHILIPS_FM1236_MK3,
140                 .tda9887_conf = TDA9887_PRESENT|TDA9887_PORT1_ACTIVE|TDA9887_PORT2_ACTIVE,
141                 .has_tuner    = 1,
142                 .decoder      = EM28XX_TVP5150,
143                 .has_msp34xx  = 1,
144                 /*FIXME: S-Video not tested */
145                 .input          = {{
146                         .type     = EM28XX_VMUX_TELEVISION,
147                         .vmux     = TVP5150_COMPOSITE0,
148                         .amux     = MSP_INPUT_DEFAULT,
149                 },{
150                         .type     = EM28XX_VMUX_SVIDEO,
151                         .vmux     = TVP5150_SVIDEO,
152                         .amux     = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
153                                         MSP_DSP_IN_SCART, MSP_DSP_IN_SCART),
154                 }},
155         },
156         [EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900] = {
157                 .name         = "Hauppauge WinTV HVR 900/950",
158                 .vchannels    = 3,
159                 .norm         = VIDEO_MODE_PAL,
160                 .tda9887_conf = TDA9887_PRESENT,
161                 .tuner_type   = TUNER_XC2028,
162                 .has_tuner    = 1,
163                 .decoder      = EM28XX_TVP5150,
164                 .input          = {{
165                         .type     = EM28XX_VMUX_TELEVISION,
166                         .vmux     = TVP5150_COMPOSITE0,
167                         .amux     = 0,
168                 },{
169                         .type     = EM28XX_VMUX_COMPOSITE1,
170                         .vmux     = TVP5150_COMPOSITE1,
171                         .amux     = 1,
172                 },{
173                         .type     = EM28XX_VMUX_SVIDEO,
174                         .vmux     = TVP5150_SVIDEO,
175                         .amux     = 1,
176                 }},
177         },
178         [EM2880_BOARD_TERRATEC_HYBRID_XS] = {
179                 .name         = "Terratec Hybrid XS",
180                 .vchannels    = 3,
181                 .norm         = VIDEO_MODE_PAL,
182                 .tda9887_conf = TDA9887_PRESENT,
183                 .has_tuner    = 1,
184                 .tuner_type   = TUNER_XC2028,
185                 .decoder      = EM28XX_TVP5150,
186                 .input          = {{
187                         .type     = EM28XX_VMUX_TELEVISION,
188                         .vmux     = TVP5150_COMPOSITE0,
189                         .amux     = 0,
190                 },{
191                         .type     = EM28XX_VMUX_COMPOSITE1,
192                         .vmux     = TVP5150_COMPOSITE1,
193                         .amux     = 1,
194                 },{
195                         .type     = EM28XX_VMUX_SVIDEO,
196                         .vmux     = TVP5150_SVIDEO,
197                         .amux     = 1,
198                 }},
199         },
200         /* maybe there's a reason behind it why Terratec sells the Hybrid XS as Prodigy XS with a
201          * different PID, let's keep it separated for now maybe we'll need it lateron */
202         [EM2880_BOARD_TERRATEC_PRODIGY_XS] = {
203                 .name         = "Terratec Prodigy XS",
204                 .vchannels    = 3,
205                 .norm         = VIDEO_MODE_PAL,
206                 .tda9887_conf = TDA9887_PRESENT,
207                 .has_tuner    = 1,
208                 .tuner_type   = TUNER_XC2028,
209                 .decoder      = EM28XX_TVP5150,
210                 .input          = {{
211                         .type     = EM28XX_VMUX_TELEVISION,
212                         .vmux     = TVP5150_COMPOSITE0,
213                         .amux     = 0,
214                 },{
215                         .type     = EM28XX_VMUX_COMPOSITE1,
216                         .vmux     = TVP5150_COMPOSITE1,
217                         .amux     = 1,
218                 },{
219                         .type     = EM28XX_VMUX_SVIDEO,
220                         .vmux     = TVP5150_SVIDEO,
221                         .amux     = 1,
222                 }},
223         },
224         [EM2820_BOARD_MSI_VOX_USB_2] = {
225                 .name           = "MSI VOX USB 2.0",
226                 .vchannels      = 3,
227                 .norm           = VIDEO_MODE_PAL,
228                 .tuner_type     = TUNER_LG_PAL_NEW_TAPC,
229                 .tda9887_conf   = TDA9887_PRESENT|TDA9887_PORT1_ACTIVE|TDA9887_PORT2_ACTIVE,
230                 .has_tuner      = 1,
231                 .decoder        = EM28XX_SAA7114,
232                 .input          = {{
233                         .type     = EM28XX_VMUX_TELEVISION,
234                         .vmux     = SAA7115_COMPOSITE4,
235                         .amux     = 0,
236                 },{
237                         .type     = EM28XX_VMUX_COMPOSITE1,
238                         .vmux     = SAA7115_COMPOSITE0,
239                         .amux     = 1,
240                 },{
241                         .type     = EM28XX_VMUX_SVIDEO,
242                         .vmux     = SAA7115_SVIDEO3,
243                         .amux     = 1,
244                 }},
245         },
246         [EM2800_BOARD_TERRATEC_CINERGY_200] = {
247                 .name         = "Terratec Cinergy 200 USB",
248                 .is_em2800    = 1,
249                 .vchannels    = 3,
250                 .norm         = VIDEO_MODE_PAL,
251                 .tuner_type   = TUNER_LG_PAL_NEW_TAPC,
252                 .tda9887_conf = TDA9887_PRESENT,
253                 .has_tuner    = 1,
254                 .decoder      = EM28XX_SAA7113,
255                 .input          = {{
256                         .type     = EM28XX_VMUX_TELEVISION,
257                         .vmux     = SAA7115_COMPOSITE2,
258                         .amux     = 0,
259                 },{
260                         .type     = EM28XX_VMUX_COMPOSITE1,
261                         .vmux     = SAA7115_COMPOSITE0,
262                         .amux     = 1,
263                 },{
264                         .type     = EM28XX_VMUX_SVIDEO,
265                         .vmux     = SAA7115_SVIDEO3,
266                         .amux     = 1,
267                 }},
268         },
269         [EM2800_BOARD_LEADTEK_WINFAST_USBII] = {
270                 .name         = "Leadtek Winfast USB II",
271                 .is_em2800    = 1,
272                 .vchannels    = 3,
273                 .norm         = VIDEO_MODE_PAL,
274                 .tuner_type   = TUNER_LG_PAL_NEW_TAPC,
275                 .tda9887_conf = TDA9887_PRESENT,
276                 .has_tuner    = 1,
277                 .decoder      = EM28XX_SAA7113,
278                 .input          = {{
279                         .type     = EM28XX_VMUX_TELEVISION,
280                         .vmux     = SAA7115_COMPOSITE2,
281                         .amux     = 0,
282                 },{
283                         .type     = EM28XX_VMUX_COMPOSITE1,
284                         .vmux     = SAA7115_COMPOSITE0,
285                         .amux     = 1,
286                 },{
287                         .type     = EM28XX_VMUX_SVIDEO,
288                         .vmux     = SAA7115_SVIDEO3,
289                         .amux     = 1,
290                 }},
291         },
292         [EM2800_BOARD_KWORLD_USB2800] = {
293                 .name         = "Kworld USB2800",
294                 .is_em2800    = 1,
295                 .vchannels    = 3,
296                 .norm         = VIDEO_MODE_PAL,
297                 .tuner_type   = TUNER_PHILIPS_ATSC,
298                 .tda9887_conf = TDA9887_PRESENT,
299                 .has_tuner    = 1,
300                 .decoder      = EM28XX_SAA7113,
301                 .input          = {{
302                         .type     = EM28XX_VMUX_TELEVISION,
303                         .vmux     = SAA7115_COMPOSITE2,
304                         .amux     = 0,
305                 },{
306                         .type     = EM28XX_VMUX_COMPOSITE1,
307                         .vmux     = SAA7115_COMPOSITE0,
308                         .amux     = 1,
309                 },{
310                         .type     = EM28XX_VMUX_SVIDEO,
311                         .vmux     = SAA7115_SVIDEO3,
312                         .amux     = 1,
313                 }},
314         },
315         [EM2820_BOARD_PINNACLE_DVC_90] = {
316                 .name         = "Pinnacle Dazzle DVC 90",
317                 .vchannels    = 3,
318                 .norm         = VIDEO_MODE_PAL,
319                 .has_tuner    = 0,
320                 .decoder      = EM28XX_SAA7113,
321                 .input          = {{
322                         .type     = EM28XX_VMUX_COMPOSITE1,
323                         .vmux     = SAA7115_COMPOSITE0,
324                         .amux     = 1,
325                 },{
326                         .type     = EM28XX_VMUX_SVIDEO,
327                         .vmux     = SAA7115_SVIDEO3,
328                         .amux     = 1,
329                 }},
330         },
331 };
332 const unsigned int em28xx_bcount = ARRAY_SIZE(em28xx_boards);
333
334 /* table of devices that work with this driver */
335 struct usb_device_id em28xx_id_table [] = {
336         { USB_DEVICE(0xeb1a, 0x2750), .driver_info = EM2820_BOARD_UNKNOWN },
337         { USB_DEVICE(0xeb1a, 0x2800), .driver_info = EM2800_BOARD_UNKNOWN },
338         { USB_DEVICE(0xeb1a, 0x2820), .driver_info = EM2820_BOARD_UNKNOWN },
339         { USB_DEVICE(0xeb1a, 0x2821), .driver_info = EM2820_BOARD_UNKNOWN },
340         { USB_DEVICE(0xeb1a, 0x2860), .driver_info = EM2820_BOARD_UNKNOWN },
341         { USB_DEVICE(0xeb1a, 0x2861), .driver_info = EM2820_BOARD_UNKNOWN },
342         { USB_DEVICE(0xeb1a, 0x2870), .driver_info = EM2820_BOARD_UNKNOWN },
343         { USB_DEVICE(0xeb1a, 0x2881), .driver_info = EM2820_BOARD_UNKNOWN },
344         { USB_DEVICE(0xeb1a, 0x2883), .driver_info = EM2820_BOARD_UNKNOWN },
345         { USB_DEVICE(0x0ccd, 0x0036), .driver_info = EM2820_BOARD_TERRATEC_CINERGY_250 },
346         { USB_DEVICE(0x2304, 0x0208), .driver_info = EM2820_BOARD_PINNACLE_USB_2 },
347         { USB_DEVICE(0x2040, 0x4200), .driver_info = EM2820_BOARD_HAUPPAUGE_WINTV_USB_2 },
348         { USB_DEVICE(0x2304, 0x0207), .driver_info = EM2820_BOARD_PINNACLE_DVC_90 },
349         { USB_DEVICE(0x2040, 0x6500), .driver_info = EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900 },
350         { USB_DEVICE(0x2040, 0x6513), .driver_info = EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900 },
351         { USB_DEVICE(0x0ccd, 0x0042), .driver_info = EM2880_BOARD_TERRATEC_HYBRID_XS },
352         { USB_DEVICE(0x0ccd, 0x0047), .driver_info = EM2880_BOARD_TERRATEC_PRODIGY_XS },
353         { },
354 };
355 MODULE_DEVICE_TABLE (usb, em28xx_id_table);
356
357 static struct em28xx_hash_table em28xx_hash [] = {
358         { 0, 0, 0 },
359 };
360
361 /* Since em28xx_pre_card_setup() requires a proper dev->model,
362  * this won't work for boards with generic PCI IDs
363  */
364 void em28xx_pre_card_setup(struct em28xx *dev)
365 {
366         /* request some modules */
367         switch(dev->model){
368         case EM2880_BOARD_TERRATEC_PRODIGY_XS:
369         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
370         case EM2880_BOARD_TERRATEC_HYBRID_XS:
371                 /* reset through GPIO? */
372                 em28xx_write_regs_req(dev, 0x00, 0x08, "\x7d", 1);
373                 break;
374         }
375 }
376
377 static int em28xx_tuner_callback(void *ptr, int command, int arg)
378 {
379         int rc = 0;
380         struct em28xx *dev = ptr;
381
382         if (dev->tuner_type != TUNER_XC2028)
383                 return 0;
384
385         switch (command) {
386         case XC2028_TUNER_RESET:
387                 /* FIXME: This is device-dependent */
388                 dev->em28xx_write_regs_req(dev, 0x00, 0x48, "\x00", 1);
389                 dev->em28xx_write_regs_req(dev, 0x00, 0x12, "\x67", 1);
390
391                 msleep(140);
392                 break;
393         }
394         return rc;
395 }
396
397 static void em28xx_config_tuner (struct em28xx *dev)
398 {
399         struct v4l2_priv_tun_config  xc2028_cfg;
400         struct xc2028_ctrl           ctl;
401         struct tuner_setup           tun_setup;
402         struct v4l2_frequency        f;
403
404         if (!dev->has_tuner)
405                 return;
406
407         tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
408         tun_setup.type = dev->tuner_type;
409         tun_setup.addr = dev->tuner_addr;
410         tun_setup.tuner_callback = em28xx_tuner_callback;
411
412         em28xx_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup);
413
414         if (dev->tuner_type == TUNER_XC2028) {
415                 memset (&ctl, 0, sizeof(ctl));
416
417                 ctl.fname   = XC2028_DEFAULT_FIRMWARE;
418                 ctl.max_len = 64;
419
420                 xc2028_cfg.tuner = TUNER_XC2028;
421                 xc2028_cfg.priv  = &ctl;
422
423                 em28xx_i2c_call_clients(dev, TUNER_SET_CONFIG, &xc2028_cfg);
424         }
425
426         /* configure tuner */
427         f.tuner = 0;
428         f.type = V4L2_TUNER_ANALOG_TV;
429         f.frequency = 9076;     /* just a magic number */
430         dev->ctl_freq = f.frequency;
431         em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
432 }
433
434 static int em28xx_hint_board(struct em28xx *dev)
435 {
436         int i;
437
438         for (i = 0; i < ARRAY_SIZE(em28xx_hash); i++) {
439                 if (dev->hash == em28xx_hash[i].hash) {
440                         dev->model = em28xx_hash[i].model;
441                         dev->tuner_type = em28xx_hash[i].tuner;
442
443                         em28xx_errdev("Your board has no unique USB ID.\n");
444                         em28xx_errdev("A hint were successfully done, "
445                                       "based on eeprom hash.\n");
446                         em28xx_errdev("This method is not 100%% failproof.\n");
447                         em28xx_errdev("If the board were missdetected, "
448                                       "please email this log to:\n");
449                         em28xx_errdev("\tV4L Mailing List "
450                                       " <video4linux-list@redhat.com>\n");
451                         em28xx_errdev("Board detected as %s\n",
452                                       em28xx_boards[dev->model].name);
453
454                         return 0;
455                 }
456         }
457         em28xx_errdev("Your board has no unique USB ID and thus need a "
458                       "hint to be detected.\n");
459         em28xx_errdev("You may try to use card=<n> insmod option to "
460                       "workaround that.\n");
461         em28xx_errdev("Please send an email with this log to:\n");
462         em28xx_errdev("\tV4L Mailing List <video4linux-list@redhat.com>\n");
463         em28xx_errdev("Board eeprom hash is 0x%08lx\n", dev->hash);
464
465         em28xx_errdev("Here is a list of valid choices for the card=<n>"
466                       " insmod option:\n");
467         for (i = 0; i < em28xx_bcount; i++) {
468                 em28xx_errdev("    card=%d -> %s\n",
469                                 i, em28xx_boards[i].name);
470         }
471         return -1;
472 }
473
474 void em28xx_card_setup(struct em28xx *dev)
475 {
476         /* request some modules */
477         switch (dev->model) {
478         case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
479         {
480                 struct tveeprom tv;
481 #ifdef CONFIG_MODULES
482                 request_module("tveeprom");
483                 request_module("ir-kbd-i2c");
484 #endif
485                 /* Call first TVeeprom */
486
487                 dev->i2c_client.addr = 0xa0 >> 1;
488                 tveeprom_hauppauge_analog(&dev->i2c_client, &tv, dev->eedata);
489
490                 dev->tuner_type = tv.tuner_type;
491                 if (tv.audio_processor == AUDIO_CHIP_MSP34XX) {
492                         dev->i2s_speed = 2048000;
493                         dev->has_msp34xx = 1;
494                 }
495                 break;
496         }
497         case EM2820_BOARD_KWORLD_PVRTV2800RF:
498                 /* GPIO enables sound on KWORLD PVR TV 2800RF */
499                 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf9", 1);
500                 break;
501         case EM2820_BOARD_UNKNOWN:
502         case EM2800_BOARD_UNKNOWN:
503                 em28xx_hint_board(dev);
504         }
505
506         dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
507         dev->has_tuner = em28xx_boards[dev->model].has_tuner;
508         dev->has_msp34xx = em28xx_boards[dev->model].has_msp34xx;
509         dev->tda9887_conf = em28xx_boards[dev->model].tda9887_conf;
510         dev->decoder = em28xx_boards[dev->model].decoder;
511         dev->video_inputs = em28xx_boards[dev->model].vchannels;
512
513         if (tuner >= 0)
514                 dev->tuner_type = tuner;
515
516 #ifdef CONFIG_MODULES
517         /* request some modules */
518         if (dev->has_msp34xx)
519                 request_module("msp3400");
520         if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
521                 request_module("saa7115");
522         if (dev->decoder == EM28XX_TVP5150)
523                 request_module("tvp5150");
524         if (dev->has_tuner)
525                 request_module("tuner");
526 #endif
527
528         em28xx_config_tuner (dev);
529 }