V4L/DVB (8116): videodev: allow PRIVATE_BASE controls when called through VIDIOC_G...
[linux-2.6] / drivers / media / video / em28xx / em28xx-cards.c
1 /*
2    em28xx-cards.c - driver for Empia EM2800/EM2820/2840 USB
3                     video capture devices
4
5    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6                       Markus Rechberger <mrechberger@gmail.com>
7                       Mauro Carvalho Chehab <mchehab@infradead.org>
8                       Sascha Sommer <saschasommer@freenet.de>
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include <linux/i2c.h>
29 #include <linux/usb.h>
30 #include <media/tuner.h>
31 #include <media/msp3400.h>
32 #include <media/saa7115.h>
33 #include <media/tvp5150.h>
34 #include <media/tveeprom.h>
35 #include <media/audiochip.h>
36 #include <media/v4l2-common.h>
37
38 #include "em28xx.h"
39
40 static int tuner = -1;
41 module_param(tuner, int, 0444);
42 MODULE_PARM_DESC(tuner, "tuner type");
43
44 static unsigned int disable_ir;
45 module_param(disable_ir, int, 0444);
46 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
47
48 struct em28xx_hash_table {
49         unsigned long hash;
50         unsigned int  model;
51         unsigned int  tuner;
52 };
53
54 struct em28xx_board em28xx_boards[] = {
55         [EM2800_BOARD_UNKNOWN] = {
56                 .name         = "Unknown EM2800 video grabber",
57                 .is_em2800    = 1,
58                 .vchannels    = 2,
59                 .tda9887_conf = TDA9887_PRESENT,
60                 .decoder      = EM28XX_SAA7113,
61                 .input           = { {
62                         .type     = EM28XX_VMUX_COMPOSITE1,
63                         .vmux     = SAA7115_COMPOSITE0,
64                         .amux     = 1,
65                 }, {
66                         .type     = EM28XX_VMUX_SVIDEO,
67                         .vmux     = SAA7115_SVIDEO3,
68                         .amux     = 1,
69                 } },
70         },
71         [EM2820_BOARD_UNKNOWN] = {
72                 .name         = "Unknown EM2750/28xx video grabber",
73                 .is_em2800    = 0,
74                 .tuner_type   = TUNER_ABSENT,
75         },
76         [EM2820_BOARD_KWORLD_PVRTV2800RF] = {
77                 .name         = "Kworld PVR TV 2800 RF",
78                 .is_em2800    = 0,
79                 .vchannels    = 2,
80                 .tuner_type   = TUNER_TEMIC_PAL,
81                 .tda9887_conf = TDA9887_PRESENT,
82                 .decoder      = EM28XX_SAA7113,
83                 .input           = { {
84                         .type     = EM28XX_VMUX_COMPOSITE1,
85                         .vmux     = SAA7115_COMPOSITE0,
86                         .amux     = 1,
87                 }, {
88                         .type     = EM28XX_VMUX_SVIDEO,
89                         .vmux     = SAA7115_SVIDEO3,
90                         .amux     = 1,
91                 } },
92         },
93         [EM2820_BOARD_TERRATEC_CINERGY_250] = {
94                 .name         = "Terratec Cinergy 250 USB",
95                 .vchannels    = 3,
96                 .tuner_type   = TUNER_LG_PAL_NEW_TAPC,
97                 .tda9887_conf = TDA9887_PRESENT,
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                 .tuner_type   = TUNER_LG_PAL_NEW_TAPC,
117                 .tda9887_conf = TDA9887_PRESENT,
118                 .decoder      = EM28XX_SAA7113,
119                 .input          = { {
120                         .type     = EM28XX_VMUX_TELEVISION,
121                         .vmux     = SAA7115_COMPOSITE2,
122                         .amux     = 0,
123                 }, {
124                         .type     = EM28XX_VMUX_COMPOSITE1,
125                         .vmux     = SAA7115_COMPOSITE0,
126                         .amux     = 1,
127                 }, {
128                         .type     = EM28XX_VMUX_SVIDEO,
129                         .vmux     = SAA7115_SVIDEO3,
130                         .amux     = 1,
131                 } },
132         },
133         [EM2820_BOARD_HAUPPAUGE_WINTV_USB_2] = {
134                 .name         = "Hauppauge WinTV USB 2",
135                 .vchannels    = 3,
136                 .tuner_type   = TUNER_PHILIPS_FM1236_MK3,
137                 .tda9887_conf = TDA9887_PRESENT |
138                                 TDA9887_PORT1_ACTIVE|
139                                 TDA9887_PORT2_ACTIVE,
140                 .decoder      = EM28XX_TVP5150,
141                 .has_msp34xx  = 1,
142                 /*FIXME: S-Video not tested */
143                 .input          = { {
144                         .type     = EM28XX_VMUX_TELEVISION,
145                         .vmux     = TVP5150_COMPOSITE0,
146                         .amux     = MSP_INPUT_DEFAULT,
147                 }, {
148                         .type     = EM28XX_VMUX_SVIDEO,
149                         .vmux     = TVP5150_SVIDEO,
150                         .amux     = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
151                                         MSP_DSP_IN_SCART, MSP_DSP_IN_SCART),
152                 } },
153         },
154         [EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900] = {
155                 .name         = "Hauppauge WinTV HVR 900",
156                 .vchannels    = 3,
157                 .tda9887_conf = TDA9887_PRESENT,
158                 .tuner_type   = TUNER_XC2028,
159                 .mts_firmware = 1,
160                 .has_dvb        = 1,
161                 .decoder      = EM28XX_TVP5150,
162                 .input          = { {
163                         .type     = EM28XX_VMUX_TELEVISION,
164                         .vmux     = TVP5150_COMPOSITE0,
165                         .amux     = 0,
166                 }, {
167                         .type     = EM28XX_VMUX_COMPOSITE1,
168                         .vmux     = TVP5150_COMPOSITE1,
169                         .amux     = 1,
170                 }, {
171                         .type     = EM28XX_VMUX_SVIDEO,
172                         .vmux     = TVP5150_SVIDEO,
173                         .amux     = 1,
174                 } },
175         },
176         [EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2] = {
177                 .name         = "Hauppauge WinTV HVR 900 (R2)",
178                 .vchannels    = 3,
179                 .tda9887_conf = TDA9887_PRESENT,
180                 .tuner_type   = TUNER_XC2028,
181                 .mts_firmware = 1,
182                 .decoder      = EM28XX_TVP5150,
183                 .input          = { {
184                         .type     = EM28XX_VMUX_TELEVISION,
185                         .vmux     = TVP5150_COMPOSITE0,
186                         .amux     = 0,
187                 }, {
188                         .type     = EM28XX_VMUX_COMPOSITE1,
189                         .vmux     = TVP5150_COMPOSITE1,
190                         .amux     = 1,
191                 }, {
192                         .type     = EM28XX_VMUX_SVIDEO,
193                         .vmux     = TVP5150_SVIDEO,
194                         .amux     = 1,
195                 } },
196         },
197         [EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950] = {
198                 .name           = "Hauppauge WinTV HVR 950",
199                 .vchannels      = 3,
200                 .tda9887_conf   = TDA9887_PRESENT,
201                 .tuner_type     = TUNER_XC2028,
202                 .mts_firmware   = 1,
203                 .has_12mhz_i2s  = 1,
204                 .has_dvb        = 1,
205                 .decoder        = EM28XX_TVP5150,
206                 .input          = { {
207                         .type     = EM28XX_VMUX_TELEVISION,
208                         .vmux     = TVP5150_COMPOSITE0,
209                         .amux     = 0,
210                 }, {
211                         .type     = EM28XX_VMUX_COMPOSITE1,
212                         .vmux     = TVP5150_COMPOSITE1,
213                         .amux     = 1,
214                 }, {
215                         .type     = EM28XX_VMUX_SVIDEO,
216                         .vmux     = TVP5150_SVIDEO,
217                         .amux     = 1,
218                 } },
219         },
220         [EM2880_BOARD_PINNACLE_PCTV_HD_PRO] = {
221                 .name           = "Pinnacle PCTV HD Pro Stick",
222                 .vchannels      = 3,
223                 .tda9887_conf   = TDA9887_PRESENT,
224                 .tuner_type     = TUNER_XC2028,
225                 .mts_firmware   = 1,
226                 .has_12mhz_i2s  = 1,
227                 .has_dvb        = 1,
228                 .decoder        = EM28XX_TVP5150,
229                 .input          = { {
230                         .type     = EM28XX_VMUX_TELEVISION,
231                         .vmux     = TVP5150_COMPOSITE0,
232                         .amux     = 0,
233                 }, {
234                         .type     = EM28XX_VMUX_COMPOSITE1,
235                         .vmux     = TVP5150_COMPOSITE1,
236                         .amux     = 1,
237                 }, {
238                         .type     = EM28XX_VMUX_SVIDEO,
239                         .vmux     = TVP5150_SVIDEO,
240                         .amux     = 1,
241                 } },
242         },
243         [EM2880_BOARD_TERRATEC_HYBRID_XS] = {
244                 .name         = "Terratec Hybrid XS",
245                 .vchannels    = 3,
246                 .tda9887_conf = TDA9887_PRESENT,
247                 .tuner_type   = TUNER_XC2028,
248                 .decoder      = EM28XX_TVP5150,
249                 .input          = { {
250                         .type     = EM28XX_VMUX_TELEVISION,
251                         .vmux     = TVP5150_COMPOSITE0,
252                         .amux     = 0,
253                 }, {
254                         .type     = EM28XX_VMUX_COMPOSITE1,
255                         .vmux     = TVP5150_COMPOSITE1,
256                         .amux     = 1,
257                 }, {
258                         .type     = EM28XX_VMUX_SVIDEO,
259                         .vmux     = TVP5150_SVIDEO,
260                         .amux     = 1,
261                 } },
262         },
263         /* maybe there's a reason behind it why Terratec sells the Hybrid XS
264            as Prodigy XS with a different PID, let's keep it separated for now
265            maybe we'll need it lateron */
266         [EM2880_BOARD_TERRATEC_PRODIGY_XS] = {
267                 .name         = "Terratec Prodigy XS",
268                 .vchannels    = 3,
269                 .tda9887_conf = TDA9887_PRESENT,
270                 .tuner_type   = TUNER_XC2028,
271                 .decoder      = EM28XX_TVP5150,
272                 .input          = { {
273                         .type     = EM28XX_VMUX_TELEVISION,
274                         .vmux     = TVP5150_COMPOSITE0,
275                         .amux     = 0,
276                 }, {
277                         .type     = EM28XX_VMUX_COMPOSITE1,
278                         .vmux     = TVP5150_COMPOSITE1,
279                         .amux     = 1,
280                 }, {
281                         .type     = EM28XX_VMUX_SVIDEO,
282                         .vmux     = TVP5150_SVIDEO,
283                         .amux     = 1,
284                 } },
285         },
286         [EM2820_BOARD_MSI_VOX_USB_2] = {
287                 .name              = "MSI VOX USB 2.0",
288                 .vchannels         = 3,
289                 .tuner_type        = TUNER_LG_PAL_NEW_TAPC,
290                 .tda9887_conf      = TDA9887_PRESENT      |
291                                      TDA9887_PORT1_ACTIVE |
292                                      TDA9887_PORT2_ACTIVE,
293                 .max_range_640_480 = 1,
294
295                 .decoder           = EM28XX_SAA7114,
296                 .input             = { {
297                         .type      = EM28XX_VMUX_TELEVISION,
298                         .vmux      = SAA7115_COMPOSITE4,
299                         .amux      = 0,
300                 }, {
301                         .type      = EM28XX_VMUX_COMPOSITE1,
302                         .vmux      = SAA7115_COMPOSITE0,
303                         .amux      = 1,
304                 }, {
305                         .type      = EM28XX_VMUX_SVIDEO,
306                         .vmux      = SAA7115_SVIDEO3,
307                         .amux      = 1,
308                 } },
309         },
310         [EM2800_BOARD_TERRATEC_CINERGY_200] = {
311                 .name         = "Terratec Cinergy 200 USB",
312                 .is_em2800    = 1,
313                 .vchannels    = 3,
314                 .tuner_type   = TUNER_LG_PAL_NEW_TAPC,
315                 .tda9887_conf = TDA9887_PRESENT,
316                 .decoder      = EM28XX_SAA7113,
317                 .input          = { {
318                         .type     = EM28XX_VMUX_TELEVISION,
319                         .vmux     = SAA7115_COMPOSITE2,
320                         .amux     = 0,
321                 }, {
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         [EM2800_BOARD_LEADTEK_WINFAST_USBII] = {
332                 .name         = "Leadtek Winfast USB II",
333                 .is_em2800    = 1,
334                 .vchannels    = 3,
335                 .tuner_type   = TUNER_LG_PAL_NEW_TAPC,
336                 .tda9887_conf = TDA9887_PRESENT,
337                 .decoder      = EM28XX_SAA7113,
338                 .input          = { {
339                         .type     = EM28XX_VMUX_TELEVISION,
340                         .vmux     = SAA7115_COMPOSITE2,
341                         .amux     = 0,
342                 }, {
343                         .type     = EM28XX_VMUX_COMPOSITE1,
344                         .vmux     = SAA7115_COMPOSITE0,
345                         .amux     = 1,
346                 }, {
347                         .type     = EM28XX_VMUX_SVIDEO,
348                         .vmux     = SAA7115_SVIDEO3,
349                         .amux     = 1,
350                 } },
351         },
352         [EM2800_BOARD_KWORLD_USB2800] = {
353                 .name         = "Kworld USB2800",
354                 .is_em2800    = 1,
355                 .vchannels    = 3,
356                 .tuner_type   = TUNER_PHILIPS_FCV1236D,
357                 .tda9887_conf = TDA9887_PRESENT,
358                 .decoder      = EM28XX_SAA7113,
359                 .input          = { {
360                         .type     = EM28XX_VMUX_TELEVISION,
361                         .vmux     = SAA7115_COMPOSITE2,
362                         .amux     = 0,
363                 }, {
364                         .type     = EM28XX_VMUX_COMPOSITE1,
365                         .vmux     = SAA7115_COMPOSITE0,
366                         .amux     = 1,
367                 }, {
368                         .type     = EM28XX_VMUX_SVIDEO,
369                         .vmux     = SAA7115_SVIDEO3,
370                         .amux     = 1,
371                 } },
372         },
373         [EM2820_BOARD_PINNACLE_DVC_90] = {
374                 .name         = "Pinnacle Dazzle DVC 90/DVC 100",
375                 .vchannels    = 3,
376                 .tuner_type   = TUNER_ABSENT,
377                 .decoder      = EM28XX_SAA7113,
378                 .input          = { {
379                         .type     = EM28XX_VMUX_COMPOSITE1,
380                         .vmux     = SAA7115_COMPOSITE0,
381                         .amux     = 1,
382                 }, {
383                         .type     = EM28XX_VMUX_SVIDEO,
384                         .vmux     = SAA7115_SVIDEO3,
385                         .amux     = 1,
386                 } },
387         },
388         [EM2800_BOARD_VGEAR_POCKETTV] = {
389                 .name         = "V-Gear PocketTV",
390                 .is_em2800    = 1,
391                 .vchannels    = 3,
392                 .tuner_type   = TUNER_LG_PAL_NEW_TAPC,
393                 .tda9887_conf = TDA9887_PRESENT,
394                 .decoder      = EM28XX_SAA7113,
395                 .input          = { {
396                         .type     = EM28XX_VMUX_TELEVISION,
397                         .vmux     = SAA7115_COMPOSITE2,
398                         .amux     = 0,
399                 }, {
400                         .type     = EM28XX_VMUX_COMPOSITE1,
401                         .vmux     = SAA7115_COMPOSITE0,
402                         .amux     = 1,
403                 }, {
404                         .type     = EM28XX_VMUX_SVIDEO,
405                         .vmux     = SAA7115_SVIDEO3,
406                         .amux     = 1,
407                 } },
408         },
409         [EM2820_BOARD_PROLINK_PLAYTV_USB2] = {
410                 .name         = "Pixelview Prolink PlayTV USB 2.0",
411                 .vchannels    = 3,
412                 .tda9887_conf = TDA9887_PRESENT,
413                 .tuner_type   = TUNER_YMEC_TVF_5533MF,
414                 .decoder      = EM28XX_SAA7113,
415                 .input          = { {
416                         .type     = EM28XX_VMUX_TELEVISION,
417                         .vmux     = SAA7115_COMPOSITE2,
418                         .amux     = EM28XX_AMUX_LINE_IN,
419                 }, {
420                         .type     = EM28XX_VMUX_COMPOSITE1,
421                         .vmux     = SAA7115_COMPOSITE0,
422                         .amux     = EM28XX_AMUX_LINE_IN,
423                 }, {
424                         .type     = EM28XX_VMUX_SVIDEO,
425                         .vmux     = SAA7115_SVIDEO3,
426                         .amux     = EM28XX_AMUX_LINE_IN,
427                 } },
428         },
429 };
430 const unsigned int em28xx_bcount = ARRAY_SIZE(em28xx_boards);
431
432 /* table of devices that work with this driver */
433 struct usb_device_id em28xx_id_table [] = {
434         { USB_DEVICE(0xeb1a, 0x2750),
435                         .driver_info = EM2820_BOARD_UNKNOWN },
436         { USB_DEVICE(0xeb1a, 0x2800),
437                         .driver_info = EM2800_BOARD_UNKNOWN },
438         { USB_DEVICE(0xeb1a, 0x2820),
439                         .driver_info = EM2820_BOARD_UNKNOWN },
440         { USB_DEVICE(0xeb1a, 0x2821),
441                         .driver_info = EM2820_BOARD_UNKNOWN },
442         { USB_DEVICE(0xeb1a, 0x2860),
443                         .driver_info = EM2820_BOARD_UNKNOWN },
444         { USB_DEVICE(0xeb1a, 0x2861),
445                         .driver_info = EM2820_BOARD_UNKNOWN },
446         { USB_DEVICE(0xeb1a, 0x2870),
447                         .driver_info = EM2820_BOARD_UNKNOWN },
448         { USB_DEVICE(0xeb1a, 0x2881),
449                         .driver_info = EM2820_BOARD_UNKNOWN },
450         { USB_DEVICE(0xeb1a, 0x2883),
451                         .driver_info = EM2820_BOARD_UNKNOWN },
452         { USB_DEVICE(0x0ccd, 0x0036),
453                         .driver_info = EM2820_BOARD_TERRATEC_CINERGY_250 },
454         { USB_DEVICE(0x2304, 0x0208),
455                         .driver_info = EM2820_BOARD_PINNACLE_USB_2 },
456         { USB_DEVICE(0x2040, 0x4200),
457                         .driver_info = EM2820_BOARD_HAUPPAUGE_WINTV_USB_2 },
458         { USB_DEVICE(0x2040, 0x4201),
459                         .driver_info = EM2820_BOARD_HAUPPAUGE_WINTV_USB_2 },
460         { USB_DEVICE(0x2304, 0x0207),
461                         .driver_info = EM2820_BOARD_PINNACLE_DVC_90 },
462         { USB_DEVICE(0x2304, 0x021a),
463                         .driver_info = EM2820_BOARD_PINNACLE_DVC_90 },
464         { USB_DEVICE(0x2304, 0x0227),
465                         .driver_info = EM2880_BOARD_PINNACLE_PCTV_HD_PRO },
466         { USB_DEVICE(0x2040, 0x6500),
467                         .driver_info = EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900 },
468         { USB_DEVICE(0x2040, 0x6502),
469                         .driver_info = EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2 },
470         { USB_DEVICE(0x2040, 0x6513), /* HCW HVR-980 */
471                         .driver_info = EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950 },
472         { USB_DEVICE(0x2040, 0x6517), /* HP  HVR-950 */
473                         .driver_info = EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950 },
474         { USB_DEVICE(0x2040, 0x651b), /* RP  HVR-950 */
475                         .driver_info = EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950 },
476         { USB_DEVICE(0x2040, 0x651f), /* HCW HVR-850 */
477                         .driver_info = EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950 },
478         { USB_DEVICE(0x0ccd, 0x0042),
479                         .driver_info = EM2880_BOARD_TERRATEC_HYBRID_XS },
480         { USB_DEVICE(0x0ccd, 0x0047),
481                         .driver_info = EM2880_BOARD_TERRATEC_PRODIGY_XS },
482         { },
483 };
484 MODULE_DEVICE_TABLE(usb, em28xx_id_table);
485
486 /*
487  *  Reset sequences for analog/digital modes
488  */
489
490 /* Board Hauppauge WinTV HVR 900 analog */
491 static struct em28xx_reg_seq hauppauge_wintv_hvr_900_analog[] = {
492         {EM28XX_R08_GPIO,       0x2d,   ~EM_GPIO_4,     10},
493         {0x05,                  0xff,   0x10,           10},
494         {  -1,                  -1,     -1,             -1},
495 };
496
497 /* Board Hauppauge WinTV HVR 900 digital */
498 static struct em28xx_reg_seq hauppauge_wintv_hvr_900_digital[] = {
499         {EM28XX_R08_GPIO,       0x2e,   ~EM_GPIO_4,     10},
500         {EM2880_R04_GPO,        0x04,   0x0f,           10},
501         {EM2880_R04_GPO,        0x0c,   0x0f,           10},
502         { -1,                   -1,     -1,             -1},
503 };
504
505 /* Board Hauppauge WinTV HVR 900 tuner_callback */
506 static struct em28xx_reg_seq hauppauge_wintv_hvr_900_tuner_callback[] = {
507         {EM28XX_R08_GPIO,       EM_GPIO_4,      EM_GPIO_4,      10},
508         {EM28XX_R08_GPIO,       0,              EM_GPIO_4,      10},
509         {EM28XX_R08_GPIO,       EM_GPIO_4,      EM_GPIO_4,      10},
510         {  -1,                  -1,             -1,             -1},
511 };
512
513 /*
514  * EEPROM hash table for devices with generic USB IDs
515  */
516 static struct em28xx_hash_table em28xx_eeprom_hash [] = {
517         /* P/N: SA 60002070465 Tuner: TVF7533-MF */
518         {0x6ce05a8f, EM2820_BOARD_PROLINK_PLAYTV_USB2, TUNER_YMEC_TVF_5533MF},
519 };
520
521 /* I2C devicelist hash table for devices with generic USB IDs */
522 static struct em28xx_hash_table em28xx_i2c_hash[] = {
523         {0xb06a32c3, EM2800_BOARD_TERRATEC_CINERGY_200, TUNER_LG_PAL_NEW_TAPC},
524         {0xf51200e3, EM2800_BOARD_VGEAR_POCKETTV, TUNER_LG_PAL_NEW_TAPC},
525 };
526
527 int em28xx_tuner_callback(void *ptr, int command, int arg)
528 {
529         int rc = 0;
530         struct em28xx *dev = ptr;
531
532         if (dev->tuner_type != TUNER_XC2028)
533                 return 0;
534
535         if (command != XC2028_TUNER_RESET)
536                 return 0;
537
538         if (dev->mode == EM28XX_ANALOG_MODE)
539                 rc = em28xx_gpio_set(dev, dev->tun_analog_gpio);
540         else
541                 rc = em28xx_gpio_set(dev, dev->tun_digital_gpio);
542
543         return rc;
544 }
545 EXPORT_SYMBOL_GPL(em28xx_tuner_callback);
546
547 static void em28xx_set_model(struct em28xx *dev)
548 {
549         dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
550         dev->has_msp34xx = em28xx_boards[dev->model].has_msp34xx;
551         dev->tda9887_conf = em28xx_boards[dev->model].tda9887_conf;
552         dev->decoder = em28xx_boards[dev->model].decoder;
553         dev->video_inputs = em28xx_boards[dev->model].vchannels;
554         dev->has_12mhz_i2s = em28xx_boards[dev->model].has_12mhz_i2s;
555         dev->max_range_640_480 = em28xx_boards[dev->model].max_range_640_480;
556         dev->has_dvb = em28xx_boards[dev->model].has_dvb;
557 }
558
559 /* Since em28xx_pre_card_setup() requires a proper dev->model,
560  * this won't work for boards with generic PCI IDs
561  */
562 void em28xx_pre_card_setup(struct em28xx *dev)
563 {
564         int rc;
565
566         rc = em28xx_read_reg(dev, EM2880_R04_GPO);
567         if (rc >= 0)
568                 dev->reg_gpo = rc;
569
570         dev->wait_after_write = 5;
571         rc = em28xx_read_reg(dev, EM28XX_R0A_CHIPID);
572         if (rc > 0) {
573                 switch (rc) {
574                 case CHIP_ID_EM2860:
575                         em28xx_info("chip ID is em2860\n");
576                         break;
577                 case CHIP_ID_EM2883:
578                         em28xx_info("chip ID is em2882/em2883\n");
579                         dev->wait_after_write = 0;
580                         break;
581                 default:
582                         em28xx_info("em28xx chip ID = %d\n", rc);
583                 }
584         }
585         em28xx_set_model(dev);
586
587         /* request some modules */
588         switch (dev->model) {
589         case EM2880_BOARD_TERRATEC_PRODIGY_XS:
590         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
591         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
592         case EM2880_BOARD_TERRATEC_HYBRID_XS:
593         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
594         case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
595                 em28xx_write_regs(dev, EM28XX_R0F_XCLK,    "\x27", 1);
596                 em28xx_write_regs(dev, EM28XX_R06_I2C_CLK, "\x40", 1);
597                 msleep(50);
598
599                 /* Sets GPO/GPIO sequences for this device */
600                 dev->analog_gpio      = hauppauge_wintv_hvr_900_analog;
601                 dev->digital_gpio     = hauppauge_wintv_hvr_900_digital;
602                 dev->tun_analog_gpio  = hauppauge_wintv_hvr_900_tuner_callback;
603                 dev->tun_digital_gpio = hauppauge_wintv_hvr_900_tuner_callback;
604
605                 break;
606         }
607
608         em28xx_gpio_set(dev, dev->tun_analog_gpio);
609         em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
610
611         /* Unlock device */
612         em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
613 }
614
615 static void em28xx_setup_xc3028(struct em28xx *dev, struct xc2028_ctrl *ctl)
616 {
617         memset(ctl, 0, sizeof(*ctl));
618
619         ctl->fname   = XC2028_DEFAULT_FIRMWARE;
620         ctl->max_len = 64;
621         ctl->mts = em28xx_boards[dev->model].mts_firmware;
622
623         switch (dev->model) {
624         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
625                 ctl->demod = XC3028_FE_ZARLINK456;
626                 break;
627         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
628                 /* djh - Not sure which demod we need here */
629                 ctl->demod = XC3028_FE_DEFAULT;
630                 break;
631         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
632         case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
633                 /* FIXME: Better to specify the needed IF */
634                 ctl->demod = XC3028_FE_DEFAULT;
635                 break;
636         default:
637                 ctl->demod = XC3028_FE_OREN538;
638         }
639 }
640
641 static void em28xx_config_tuner(struct em28xx *dev)
642 {
643         struct v4l2_priv_tun_config  xc2028_cfg;
644         struct tuner_setup           tun_setup;
645         struct v4l2_frequency        f;
646
647         if (dev->tuner_type == TUNER_ABSENT)
648                 return;
649
650         tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
651         tun_setup.type = dev->tuner_type;
652         tun_setup.addr = dev->tuner_addr;
653         tun_setup.tuner_callback = em28xx_tuner_callback;
654
655         em28xx_i2c_call_clients(dev, TUNER_SET_TYPE_ADDR, &tun_setup);
656
657         if (dev->tuner_type == TUNER_XC2028) {
658                 struct xc2028_ctrl           ctl;
659
660                 em28xx_setup_xc3028(dev, &ctl);
661
662                 xc2028_cfg.tuner = TUNER_XC2028;
663                 xc2028_cfg.priv  = &ctl;
664
665                 em28xx_i2c_call_clients(dev, TUNER_SET_CONFIG, &xc2028_cfg);
666         }
667
668         /* configure tuner */
669         f.tuner = 0;
670         f.type = V4L2_TUNER_ANALOG_TV;
671         f.frequency = 9076;     /* just a magic number */
672         dev->ctl_freq = f.frequency;
673         em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
674 }
675
676 static int em28xx_hint_board(struct em28xx *dev)
677 {
678         int i;
679
680         /* HINT method: EEPROM
681          *
682          * This method works only for boards with eeprom.
683          * Uses a hash of all eeprom bytes. The hash should be
684          * unique for a vendor/tuner pair.
685          * There are a high chance that tuners for different
686          * video standards produce different hashes.
687          */
688         for (i = 0; i < ARRAY_SIZE(em28xx_eeprom_hash); i++) {
689                 if (dev->hash == em28xx_eeprom_hash[i].hash) {
690                         dev->model = em28xx_eeprom_hash[i].model;
691                         dev->tuner_type = em28xx_eeprom_hash[i].tuner;
692
693                         em28xx_errdev("Your board has no unique USB ID.\n");
694                         em28xx_errdev("A hint were successfully done, "
695                                       "based on eeprom hash.\n");
696                         em28xx_errdev("This method is not 100%% failproof.\n");
697                         em28xx_errdev("If the board were missdetected, "
698                                       "please email this log to:\n");
699                         em28xx_errdev("\tV4L Mailing List "
700                                       " <video4linux-list@redhat.com>\n");
701                         em28xx_errdev("Board detected as %s\n",
702                                       em28xx_boards[dev->model].name);
703
704                         return 0;
705                 }
706         }
707
708         /* HINT method: I2C attached devices
709          *
710          * This method works for all boards.
711          * Uses a hash of i2c scanned devices.
712          * Devices with the same i2c attached chips will
713          * be considered equal.
714          * This method is less precise than the eeprom one.
715          */
716
717         /* user did not request i2c scanning => do it now */
718         if (!dev->i2c_hash)
719                 em28xx_do_i2c_scan(dev);
720
721         for (i = 0; i < ARRAY_SIZE(em28xx_i2c_hash); i++) {
722                 if (dev->i2c_hash == em28xx_i2c_hash[i].hash) {
723                         dev->model = em28xx_i2c_hash[i].model;
724                         dev->tuner_type = em28xx_i2c_hash[i].tuner;
725                         em28xx_errdev("Your board has no unique USB ID.\n");
726                         em28xx_errdev("A hint were successfully done, "
727                                       "based on i2c devicelist hash.\n");
728                         em28xx_errdev("This method is not 100%% failproof.\n");
729                         em28xx_errdev("If the board were missdetected, "
730                                       "please email this log to:\n");
731                         em28xx_errdev("\tV4L Mailing List "
732                                       " <video4linux-list@redhat.com>\n");
733                         em28xx_errdev("Board detected as %s\n",
734                                       em28xx_boards[dev->model].name);
735
736                         return 0;
737                 }
738         }
739
740         em28xx_errdev("Your board has no unique USB ID and thus need a "
741                       "hint to be detected.\n");
742         em28xx_errdev("You may try to use card=<n> insmod option to "
743                       "workaround that.\n");
744         em28xx_errdev("Please send an email with this log to:\n");
745         em28xx_errdev("\tV4L Mailing List <video4linux-list@redhat.com>\n");
746         em28xx_errdev("Board eeprom hash is 0x%08lx\n", dev->hash);
747         em28xx_errdev("Board i2c devicelist hash is 0x%08lx\n", dev->i2c_hash);
748
749         em28xx_errdev("Here is a list of valid choices for the card=<n>"
750                       " insmod option:\n");
751         for (i = 0; i < em28xx_bcount; i++) {
752                 em28xx_errdev("    card=%d -> %s\n",
753                                 i, em28xx_boards[i].name);
754         }
755         return -1;
756 }
757
758 /* ----------------------------------------------------------------------- */
759 void em28xx_set_ir(struct em28xx *dev, struct IR_i2c *ir)
760 {
761         if (disable_ir) {
762                 ir->get_key = NULL;
763                 return ;
764         }
765
766         /* detect & configure */
767         switch (dev->model) {
768         case (EM2800_BOARD_UNKNOWN):
769                 break;
770         case (EM2820_BOARD_UNKNOWN):
771                 break;
772         case (EM2800_BOARD_TERRATEC_CINERGY_200):
773         case (EM2820_BOARD_TERRATEC_CINERGY_250):
774                 ir->ir_codes = ir_codes_em_terratec;
775                 ir->get_key = em28xx_get_key_terratec;
776                 snprintf(ir->c.name, sizeof(ir->c.name),
777                          "i2c IR (EM28XX Terratec)");
778                 break;
779         case (EM2820_BOARD_PINNACLE_USB_2):
780                 ir->ir_codes = ir_codes_pinnacle_grey;
781                 ir->get_key = em28xx_get_key_pinnacle_usb_grey;
782                 snprintf(ir->c.name, sizeof(ir->c.name),
783                          "i2c IR (EM28XX Pinnacle PCTV)");
784                 break;
785         case (EM2820_BOARD_HAUPPAUGE_WINTV_USB_2):
786                 ir->ir_codes = ir_codes_hauppauge_new;
787                 ir->get_key = em28xx_get_key_em_haup;
788                 snprintf(ir->c.name, sizeof(ir->c.name),
789                          "i2c IR (EM2840 Hauppauge)");
790                 break;
791         case (EM2820_BOARD_MSI_VOX_USB_2):
792                 break;
793         case (EM2800_BOARD_LEADTEK_WINFAST_USBII):
794                 break;
795         case (EM2800_BOARD_KWORLD_USB2800):
796                 break;
797         }
798 }
799
800 void em28xx_card_setup(struct em28xx *dev)
801 {
802         em28xx_set_model(dev);
803
804         dev->tuner_type = em28xx_boards[dev->model].tuner_type;
805
806         /* request some modules */
807         switch (dev->model) {
808         case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
809         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
810         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
811         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
812         {
813                 struct tveeprom tv;
814 #ifdef CONFIG_MODULES
815                 request_module("tveeprom");
816 #endif
817                 /* Call first TVeeprom */
818
819                 dev->i2c_client.addr = 0xa0 >> 1;
820                 tveeprom_hauppauge_analog(&dev->i2c_client, &tv, dev->eedata);
821
822                 dev->tuner_type = tv.tuner_type;
823
824                 if (tv.audio_processor == AUDIO_CHIP_MSP34XX) {
825                         dev->i2s_speed = 2048000;
826                         dev->has_msp34xx = 1;
827                 }
828 #ifdef CONFIG_MODULES
829                 if (tv.has_ir)
830                         request_module("ir-kbd-i2c");
831 #endif
832                 break;
833         }
834         case EM2820_BOARD_KWORLD_PVRTV2800RF:
835                 /* GPIO enables sound on KWORLD PVR TV 2800RF */
836                 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf9", 1);
837                 break;
838         case EM2820_BOARD_UNKNOWN:
839         case EM2800_BOARD_UNKNOWN:
840                 if (!em28xx_hint_board(dev))
841                         em28xx_set_model(dev);
842         }
843
844         /* Allow override tuner type by a module parameter */
845         if (tuner >= 0)
846                 dev->tuner_type = tuner;
847
848 #ifdef CONFIG_MODULES
849         /* request some modules */
850         if (dev->has_msp34xx)
851                 request_module("msp3400");
852         if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
853                 request_module("saa7115");
854         if (dev->decoder == EM28XX_TVP5150)
855                 request_module("tvp5150");
856         if (dev->tuner_type != TUNER_ABSENT)
857                 request_module("tuner");
858 #endif
859
860         em28xx_config_tuner(dev);
861 }