xref: /openbsd/sys/dev/pci/bktr/bktr_card.c (revision 6f7c7389)
1 /*	$OpenBSD: bktr_card.c,v 1.13 2007/01/08 08:29:37 pedro Exp $	*/
2 /* $FreeBSD: src/sys/dev/bktr/bktr_card.c,v 1.16 2000/10/31 13:09:56 roger Exp $ */
3 
4 /*
5  * This is part of the Driver for Video Capture Cards (Frame grabbers)
6  * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
7  * chipset.
8  * Copyright Roger Hardiman and Amancio Hasty.
9  *
10  * bktr_card : This deals with identifying TV cards.
11  *		trying to find the card make and model of card.
12  *		trying to find the type of tuner fitted.
13  *		reading the configuration EEPROM.
14  *		locating i2c devices.
15  *
16  */
17 
18 /*
19  * 1. Redistributions of source code must retain the
20  * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
21  * All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *      This product includes software developed by Amancio Hasty and
34  *      Roger Hardiman
35  * 4. The name of the author may not be used to endorse or promote products
36  *    derived from this software without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
39  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
40  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
44  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
47  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48  * POSSIBILITY OF SUCH DAMAGE.
49  */
50 
51 #ifndef __OpenBSD__
52 #include "opt_bktr.h"		/* Include any kernel config options */
53 #endif
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/vnode.h>
58 
59 #ifdef __FreeBSD__
60 
61 #if (__FreeBSD_version < 500000)
62 #include <machine/clock.h>	/* for DELAY */
63 #endif
64 
65 #include <pci/pcivar.h>
66 
67 #if (__FreeBSD_version >=300000)
68 #include <machine/bus_memio.h>	/* for bus space */
69 #include <machine/bus.h>
70 #include <sys/bus.h>
71 #endif
72 #endif
73 
74 #if defined(__NetBSD__) || defined(__OpenBSD__)
75 #include <dev/ic/bt8xx.h>	/* NetBSD location for .h files */
76 #include <dev/pci/bktr/bktr_reg.h>
77 #include <dev/pci/bktr/bktr_core.h>
78 #include <dev/pci/bktr/bktr_tuner.h>
79 #include <dev/pci/bktr/bktr_card.h>
80 #include <dev/pci/bktr/bktr_audio.h>
81 #else
82 #include <machine/ioctl_meteor.h>	/* Traditional location for .h files */
83 #include <machine/ioctl_bt848.h>	/* extensions to ioctl_meteor.h */
84 #include <dev/bktr/bktr_reg.h>
85 #include <dev/bktr/bktr_core.h>
86 #include <dev/bktr/bktr_tuner.h>
87 #include <dev/bktr/bktr_card.h>
88 #include <dev/bktr/bktr_audio.h>
89 #endif
90 
91 /* Include the PCI Vendor definitions */
92 #include <dev/pci/pcidevs.h>
93 #include <dev/pci/pcireg.h>
94 
95 /* Various defines */
96 #define HAUP_REMOTE_INT_WADDR   0x30
97 #define HAUP_REMOTE_INT_RADDR   0x31
98 
99 #define HAUP_REMOTE_EXT_WADDR   0x34
100 #define HAUP_REMOTE_EXT_RADDR   0x35
101 
102 /* address of BTSC/SAP decoder chip */
103 #define TDA9850_WADDR		0xb6
104 #define TDA9850_RADDR		0xb7
105 
106 /* address of MSP3400C chip */
107 #define MSP3400C_WADDR		0x80
108 #define MSP3400C_RADDR		0x81
109 
110 /* address of DPL3518A chip */
111 #define DPL3518A_WADDR		0x84
112 #define DPL3518A_RADDR		0x85
113 
114 /* EEProm (128 * 8) on an STB card */
115 #define X24C01_WADDR		0xae
116 #define X24C01_RADDR		0xaf
117 
118 /* EEProm (256 * 8) on a Hauppauge card */
119 /* and on most BT878s cards to store the sub-system vendor id */
120 #define PFC8582_WADDR		0xa0
121 #define PFC8582_RADDR		0xa1
122 
123 #if BKTR_SYSTEM_DEFAULT == BROOKTREE_PAL
124 #define DEFAULT_TUNER   PHILIPS_PALI
125 #else
126 #define DEFAULT_TUNER   PHILIPS_NTSC
127 #endif
128 
129 
130 
131 
132 /*
133  * the data for each type of card
134  *
135  * Note:
136  *   these entried MUST be kept in the order defined by the CARD_XXX defines!
137  */
138 static const struct CARDTYPE cards[] = {
139 
140 	{  CARD_UNKNOWN,			/* the card id */
141 	  "Unknown",				/* the 'name' */
142 	   NULL,				/* the tuner */
143 	   0,					/* the tuner i2c address */
144 	   0,					/* dbx unknown */
145 	   0,
146 	   0,
147 	   0,					/* EEProm unknown */
148 	   0,					/* EEProm unknown */
149 	   { 0, 0, 0, 0, 0 },
150 	   0 },					/* GPIO mask */
151 
152 	{  CARD_MIRO,				/* the card id */
153 	  "Pinnacle/Miro TV",			/* the 'name' */
154 	   NULL,				/* the tuner */
155 	   0,					/* the tuner i2c address */
156 	   0,					/* dbx unknown */
157 	   0,
158 	   0,
159 	   0,					/* EEProm unknown */
160 	   0,					/* size unknown */
161 	   { 0x02, 0x01, 0x00, 0x0a, 1 },	/* audio MUX values */
162 	   0x0f },				/* GPIO mask */
163 
164 	{  CARD_HAUPPAUGE,			/* the card id */
165 	  "Hauppauge WinCast/TV",		/* the 'name' */
166 	   NULL,				/* the tuner */
167 	   0,					/* the tuner i2c address */
168 	   0,					/* dbx is optional */
169 	   0,
170 	   0,
171 	   PFC8582_WADDR,			/* EEProm type */
172 	   (u_char)(256 / EEPROMBLOCKSIZE),	/* 256 bytes */
173 	   { 0x00, 0x02, 0x01, 0x04, 1 },	/* audio MUX values */
174 	   0x0f },				/* GPIO mask */
175 
176 	{  CARD_STB,				/* the card id */
177 	  "STB TV/PCI",				/* the 'name' */
178 	   NULL,				/* the tuner */
179 	   0,					/* the tuner i2c address */
180 	   0,					/* dbx is optional */
181 	   0,
182 	   0,
183 	   X24C01_WADDR,			/* EEProm type */
184 	   (u_char)(128 / EEPROMBLOCKSIZE),	/* 128 bytes */
185 	   { 0x00, 0x01, 0x02, 0x02, 1 }, 	/* audio MUX values */
186 	   0x0f },				/* GPIO mask */
187 
188 	{  CARD_INTEL,				/* the card id */
189 	  "Intel Smart Video III/VideoLogic Captivator PCI", /* the 'name' */
190 	   NULL,				/* the tuner */
191 	   0,					/* the tuner i2c address */
192 	   0,
193 	   0,
194 	   0,
195 	   0,
196 	   0,
197 	   { 0, 0, 0, 0, 0 }, 			/* audio MUX values */
198 	   0x00 },				/* GPIO mask */
199 
200 	{  CARD_IMS_TURBO,			/* the card id */
201 	  "IMS TV Turbo",			/* the 'name' */
202 	   NULL,				/* the tuner */
203 	   0,					/* the tuner i2c address */
204 	   0,					/* dbx is optional */
205 	   0,
206 	   0,
207 	   PFC8582_WADDR,			/* EEProm type */
208 	   (u_char)(256 / EEPROMBLOCKSIZE),	/* 256 bytes */
209 	   { 0x01, 0x02, 0x01, 0x00, 1 },	/* audio MUX values */
210 	   0x0f },				/* GPIO mask */
211 
212 	{  CARD_AVER_MEDIA,			/* the card id */
213 	   "AVer Media TV/FM",			/* the 'name' */
214 	   NULL,				/* the tuner */
215 	   0,					/* the tuner i2c address */
216 	   0,					/* dbx is optional */
217 	   0,
218 	   0,
219 	   0,					/* EEProm type */
220 	   0,					/* EEProm size */
221 	   { 0x0c, 0x08, 0x04, 0x00, 1 },	/* audio MUX values */
222 	   0x1f },				/* GPIO mask */
223 
224 	{  CARD_OSPREY,				/* the card id */
225 	   "MMAC Osprey",			/* the 'name' */
226 	   NULL,				/* the tuner */
227 	   0,					/* the tuner i2c address */
228 	   0,					/* dbx is optional */
229 	   0,
230 	   0,
231 	   PFC8582_WADDR,			/* EEProm type */
232 	   (u_char)(256 / EEPROMBLOCKSIZE),	/* 256 bytes */
233 	   { 0x00, 0x00, 0x00, 0x00, 0 },	/* audio MUX values */
234 	   0 },					/* GPIO mask */
235 
236 	{  CARD_NEC_PK,				/* the card id */
237 	   "NEC PK-UG-X017",			/* the 'name' */
238 	   NULL,				/* the tuner */
239 	   0,					/* the tuner i2c address */
240 	   0,					/* dbx is optional */
241 	   0,
242 	   0,
243 	   0,					/* EEProm type */
244 	   0,					/* EEProm size */
245 	   { 0x01, 0x02, 0x01, 0x00, 1 },	/* audio MUX values */
246 	   0x0f },				/* GPIO mask */
247 
248 	{  CARD_IO_GV,				/* the card id */
249 	   "I/O DATA GV-BCTV2/PCI",		/* the 'name' */
250 	   NULL,				/* the tuner */
251 	   0,					/* the tuner i2c address */
252 	   0,					/* dbx is optional */
253 	   0,
254 	   0,
255 	   0,					/* EEProm type */
256 	   0,					/* EEProm size */
257 	   { 0x00, 0x00, 0x00, 0x00, 1 },	/* Has special MUX handler */
258 	   0x0f },				/* GPIO mask */
259 
260 	{  CARD_FLYVIDEO,			/* the card id */
261 	   "FlyVideo",				/* the 'name' */
262 	   NULL,				/* the tuner */
263 	   0,					/* the tuner i2c address */
264 	   0,					/* dbx is optional */
265 	   0,					/* msp34xx is optional */
266 	   0,					/* dpl3518a is optional */
267 	   0xac,				/* EEProm type */
268 	   (u_char)(256 / EEPROMBLOCKSIZE),	/* 256 bytes */
269 	   { 0x000, 0x800, 0x400, 0x8dff00, 1 },/* audio MUX values */
270 	   0x8dff00 },				/* GPIO mask */
271 
272 	{  CARD_ZOLTRIX,			/* the card id */
273 	  "Zoltrix",				/* the 'name' */
274 	   NULL,				/* the tuner */
275 	   0,					/* the tuner i2c address */
276 	   0,					/* dbx is optional */
277 	   0,					/* msp34xx is optional */
278 	   0,					/* dpl3518a is optional */
279 	   0,					/* EEProm type */
280 	   0,					/* EEProm size */
281 	   { 0x04, 0x01, 0x00, 0x0a, 1 },	/* audio MUX values */
282 	   0x0f },				/* GPIO mask */
283 
284 	{  CARD_KISS,				/* the card id */
285 	  "KISS TV/FM PCI",			/* the 'name' */
286 	   NULL,				/* the tuner */
287 	   0,					/* the tuner i2c address */
288 	   0,					/* dbx is optional */
289 	   0,					/* msp34xx is optional */
290 	   0,					/* dpl3518a is optional */
291 	   0,					/* EEProm type */
292 	   0,					/* EEProm size */
293 	   { 0x0c, 0x00, 0x0b, 0x0b, 1 },	/* audio MUX values */
294 	   0x0f },				/* GPIO mask */
295 
296 	{  CARD_VIDEO_HIGHWAY_XTREME,		/* the card id */
297 	  "Video Highway Xtreme",		/* the 'name' */
298 	  NULL,				/* the tuner */
299 	   0,
300 	   0,
301 	   0,
302 	   0,
303 	   0,					/* EEProm type */
304 	   0,					/* EEProm size */
305 	   { 0x00, 0x02, 0x01, 0x04, 1 },	/* audio MUX values */
306 	   0x0f },				/* GPIO mask */
307 
308 	{  CARD_ASKEY_DYNALINK_MAGIC_TVIEW,	/* the card id */
309 	  "Askey/Dynalink Magic TView",		/* the 'name' */
310 	   NULL,				/* the tuner */
311 	   0,
312 	   0,
313 	   0,
314 	   0,
315 	   0,					/* EEProm type */
316 	   0,					/* EEProm size */
317 	   { 0x400, 0xE00, 0x400, 0xC00, 1 },	/* audio MUX values */
318 	   0xE00 },				/* GPIO mask */
319 
320 	{  CARD_LEADTEK,			/* the card id */
321 	  "Leadtek WinFast TV 2000",		/* the 'name' */
322 	   NULL,				/* the tuner */
323 	   0,
324 	   0,
325 	   0,
326 	   0,
327 	   0,					/* EEProm type */
328 	   0,					/* EEProm size */
329 	   /* Tuner, Extern, Intern, Mute, Enabled */
330 	   { 0x621000, 0x621000, 0x621000, 0xE21000, 1 }, /* audio MUX values */
331 	   0xfff000 },				/* GPIO mask */
332 
333 	{  CARD_TERRATVPLUS,			/* the card id */
334 	   "TerraTVplus",			/* the 'name' */
335 	   NULL,				/* the tuner */
336 	   0,
337 	   0,
338 	   0,
339 	   0,
340 	   0,					/* EEProm type */
341 	   0,					/* EEProm size */
342 	   { 0x20000, 0x00000, 0x30000, 0x40000, 1 }, /* audio MUX values*/
343 	   0x70000 },				/* GPIO mask */
344 
345 	{  CARD_TVWONDER,			/* the card id */
346 	   "ATI TV-Wonder/VE",			/* the 'name' */
347 	   NULL,				/* the tuner */
348 	   0,
349 	   0,
350 	   0,
351 	   0,
352 	   0,					/* EEProm type */
353 	   0,					/* EEProm size */
354 	   { 0x1002, 0x1002, 0x3003, 0x3003, 0x3003 },	/* audio MUX values*/
355 	   0x300f },				/* GPIO mask */
356 
357 	{  CARD_IO_BCTV3,			/* the card id */
358 	  "I/O DATA GV-BCTV3/PCI",		/* the 'name' */
359 	  NULL,					/* the tuner */
360 	  0,					/* the tuner i2c address */
361 	  0,					/* dbx is optional */
362 	  0,
363 	  0,
364 	  0,					/* EEProm type */
365 	  0,					/* EEProm size */
366 	   /* Tuner, Extern, Intern, Mute, Enabled */
367 	   { 0x10000, 0, 0x10000, 0, 1 },	/* audio MUX values */
368 	   0x10f00 },				/* GPIO mask */
369 
370 	{  CARD_AOPEN_VA1000,			/* the card id */
371 	  "AOpen VA1000",			/* the 'name' */
372 	   NULL,				/* the tuner */
373 	   0,					/* the tuner i2c address */
374 	   0,					/* dbx is optional */
375 	   0,
376 	   0,
377 	   0,					/* EEProm unknown */
378 	   0,					/* size unknown */
379 	   { 0x02, 0x00, 0x00, 0x00, 1 },	/* audio MUX values */
380 	   0x18e0 },				/* GPIO mask */
381 
382 	{  CARD_ZOLTRIX_GENIE_FM,		/* the card id */
383 	   "Zoltrix Genie TV/FM",		/* the 'name' */
384 	   NULL,				/* the tuner */
385 	   0,					/* the tuner i2c address */
386 	   0,					/* dbx is optional */
387 	   0,					/* msp34xx is optional */
388 	   0,					/* dpl3518a is optional */
389 	   0,					/* EEProm type */
390 	   0,					/* EEProm size */
391 	   { 0xbc803f, 0xbcb03f, 0xbc903f, 0x0, 1 },	/* audio MUX values*/
392 	   0xbcf03f },				/* GPIO mask */
393 
394 	{  CARD_LEADTEK_XP,			/* the card id */
395 	   "Leadtek WinFast TV 2000 XP",	/* the 'name' */
396 	   NULL,				/* the tuner */
397 	   0,					/* the tuner i2c address */
398 	   0,					/* dbx is optional */
399 	   0,					/* msp34xx is optional */
400 	   0,					/* dpl3518a is optional */
401 	   0,					/* EEProm type */
402 	   0,					/* EEProm size */
403 	   { 0x122000, 0x1000, 0x620000, 0x0, 1 },	/* audio MUX values*/
404 	   0xb3300 },				/* GPIO mask */
405 };
406 
407 struct bt848_card_sig bt848_card_signature[1]= {
408   /* IMS TURBO TV : card 5 */
409     {  5,9, {00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 02, 00, 00, 00}}
410 };
411 
412 
413 /*
414  * Write to the configuration EEPROM on the card.
415  * This is dangerous and will mess up your card. Therefore it is not
416  * implemented.
417  */
418 int
419 writeEEProm( bktr_ptr_t bktr, int offset, int count, u_char *data )
420 {
421 	return( -1 );
422 }
423 
424 /*
425  * Read the contents of the configuration EEPROM on the card.
426  * (This is not fitted to all makes of card. All Hauppauge cards have them
427  * and so do newer Bt878 based cards.
428  */
429 int
430 readEEProm(bktr_ptr_t bktr, int offset, int count, u_char *data)
431 {
432 	int	x;
433 	int	addr;
434 	int	max;
435 	int	byte;
436 
437 	/* get the address of the EEProm */
438 	addr = (int)(bktr->card.eepromAddr & 0xff);
439 	if ( addr == 0 )
440 		return( -1 );
441 
442 	max = (int)(bktr->card.eepromSize * EEPROMBLOCKSIZE);
443 	if ( (offset + count) > max )
444 		return( -1 );
445 
446 	/* set the start address */
447 	if ( i2cWrite( bktr, addr, offset, -1 ) == -1 )
448 		return( -1 );
449 
450 	/* the read cycle */
451 	for ( x = 0; x < count; ++x ) {
452 		if ( (byte = i2cRead( bktr, (addr | 1) )) == -1 )
453 			return( -1 );
454 		data[ x ] = byte;
455 	}
456 
457 	return( 0 );
458 }
459 
460 
461 #define ABSENT		(-1)
462 
463 /*
464  * get a signature of the card
465  * read all 128 possible i2c read addresses from 0x01 thru 0xff
466  * build a bit array with a 1 bit for each i2c device that responds
467  *
468  * XXX FIXME: use offset & count args
469  */
470 int
471 signCard( bktr_ptr_t bktr, int offset, int count, u_char *sig )
472 {
473 	int	x;
474 
475 	for ( x = 0; x < 16; ++x )
476 		sig[ x ] = 0;
477 
478 	for ( x = 0; x < count; ++x ) {
479 		if ( i2cRead( bktr, (2 * x) + 1 ) != ABSENT ) {
480 			sig[ x / 8 ] |= (1 << (x % 8) );
481 		}
482 	}
483 
484 	return( 0 );
485 }
486 
487 
488 /*
489  * check_for_i2c_devices.
490  * Some BT848 cards have no tuner and no additional i2c devices
491  * eg stereo decoder. These are used for video conferencing or capture from
492  * a video camera. (eg VideoLogic Captivator PCI, Intel SmartCapture card).
493  *
494  * Determine if there are any i2c devices present. There are none present if
495  *  a) reading from all 128 devices returns ABSENT (-1) for each one
496  *     (eg VideoLogic Captivator PCI with BT848)
497  *  b) reading from all 128 devices returns 0 for each one
498  *     (eg VideoLogic Captivator PCI rev. 2F with BT848A)
499  */
500 static int check_for_i2c_devices( bktr_ptr_t bktr ){
501   int x, temp_read;
502   int i2c_all_0 = 1;
503   int i2c_all_absent = 1;
504   for ( x = 0; x < 128; ++x ) {
505     temp_read = i2cRead( bktr, (2 * x) + 1 );
506     if (temp_read != 0)      i2c_all_0 = 0;
507     if (temp_read != ABSENT) i2c_all_absent = 0;
508   }
509 
510   if ((i2c_all_0) || (i2c_all_absent)) return 0;
511   else return 1;
512 }
513 
514 
515 /*
516  * Temic/Philips datasheets say tuners can be at i2c addresses 0xc0, 0xc2,
517  * 0xc4 or 0xc6, settable by links on the tuner.
518  * Determine the actual address used on the TV card by probing read addresses.
519  */
520 static int locate_tuner_address( bktr_ptr_t bktr) {
521   if (i2cRead( bktr, 0xc1) != ABSENT) return 0xc0;
522   if (i2cRead( bktr, 0xc3) != ABSENT) return 0xc2;
523   if (i2cRead( bktr, 0xc5) != ABSENT) return 0xc4;
524   if (i2cRead( bktr, 0xc7) != ABSENT) return 0xc6;
525   return -1; /* no tuner found */
526 }
527 
528 
529 /*
530  * Search for a configuration EEPROM on the i2c bus by looking at i2c addresses
531  * where EEPROMs are usually found.
532  * On some cards, the EEPROM appears in several locations, but all in the
533  * range 0xa0 to 0xae.
534  */
535 static int locate_eeprom_address( bktr_ptr_t bktr) {
536   if (i2cRead(bktr, 0xa0) != ABSENT) return 0xa0;
537   if (i2cRead(bktr, 0xac) != ABSENT) return 0xac;
538   if (i2cRead(bktr, 0xae) != ABSENT) return 0xae;
539   return -1; /* no eeprom found */
540 }
541 
542 
543 /*
544  * determine the card brand/model
545  * BKTR_OVERRIDE_CARD, BKTR_OVERRIDE_TUNER, BKTR_OVERRIDE_DBX and
546  * BKTR_OVERRIDE_MSP can be used to select a specific device,
547  * regardless of the autodetection and i2c device checks.
548  *
549  * The scheme used for probing cards faces these problems:
550  *  It is impossible to work out which type of tuner is actually fitted,
551  *  (the driver cannot tell if the Tuner is PAL or NTSC, Temic or Philips)
552  *  It is impossible to determine what audio-mux hardware is connected.
553  *  It is impossible to determine if there is extra hardware connected to the
554  *  GPIO pins  (eg radio chips or MSP34xx reset logic)
555  *
556  * However some makes of card (eg Hauppauge) come with a configuration eeprom
557  * which tells us the make of the card. Most eeproms also tell us the
558  * tuner type and other features of the cards.
559  *
560  * The current probe code works as follows
561  * A) If the card uses a Bt878/879:
562  *   1) Read the sub-system vendor id from the configuration EEPROM.
563  *      Select the required tuner, audio mux arrangement and any other
564  *      onboard features. If this fails, move to step B.
565  * B) If it card uses a Bt848, 848A, 849A or an unknown Bt878/879:
566  *   1) Look for I2C devices. If there are none fitted, it is an Intel or
567  *      VideoLogic cards.
568  *   2) Look for a configuration EEPROM.
569  *   2a) If there is one at I2C address 0xa0 it may be
570  *       a Hauppauge or an Osprey. Check the EEPROM contents to determine which
571  *       one it is. For Hauppauge, select the tuner type and audio hardware.
572  *   2b) If there is an EEPROM at I2C address 0xa8 it will be an STB card.
573  *       We still have to guess on the tuner type.
574  *
575  * C) If we do not know the card type from (A) or (B), guess at the tuner
576  *    type based on the I2C address of the tuner.
577  *
578  * D) After determining the Tuner Type, we probe the i2c bus for other
579  *    devices at known locations, eg IR-Remote Control, MSP34xx and TDA
580  *    stereo chips.
581  */
582 
583 
584 /*
585  * These are the sub-system vendor ID codes stored in the
586  * configuration EEPROM used on Bt878/879 cards. They should match the
587  * number assigned to the company by the PCI Special Interest Group
588  */
589 /* Following not confirmed with http://members.hyperlink.net.au/~chart,
590    so not added to NetBSD's pcidevs */
591 #define PCI_VENDOR_LEADTEK_ALT	0x6606	/* this is swapped w/ prod id */
592 #define PCI_VENDOR_LEADTEK_ALT_2        0x6607
593 #define PCI_VENDOR_LEADTEK_ALT_3        0x107d
594 #define PCI_VENDOR_FLYVIDEO	0x1851
595 #define PCI_VENDOR_FLYVIDEO_2	0x1852
596 #define PCI_VENDOR_PINNACLE_ALT	0xBD11	/* this is swapped w/ prod id */
597 
598 void
599 probeCard( bktr_ptr_t bktr, int verbose, int unit )
600 {
601 	int		card, i,j, card_found;
602 	int		status;
603 	u_char 		probe_signature[128], *probe_temp;
604 	int		any_i2c_devices;
605 	u_char 		eeprom[256];
606 	int 		tuner_i2c_address = -1;
607 	int 		eeprom_i2c_address = -1;
608 
609 	/* Select all GPIO bits as inputs */
610 	OUTL(bktr, BKTR_GPIO_OUT_EN, 0);
611 	if (bootverbose)
612 	    printf("%s: GPIO is 0x%08x\n", bktr_name(bktr),
613 		   INL(bktr, BKTR_GPIO_DATA));
614 
615 #ifdef HAUPPAUGE_MSP_RESET
616 	/* Reset the MSP34xx audio chip. This resolves bootup card
617 	 * detection problems with old Bt848 based Hauppauge cards with
618 	 * MSP34xx stereo audio chips. This must be user enabled because
619 	 * at this point the probe function does not know the card type. */
620 	OUTL(bktr, BKTR_GPIO_OUT_EN, INL(bktr, BKTR_GPIO_OUT_EN) | (1<<5));
621 	OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
622 	DELAY(2500); /* wait 2.5ms */
623 	OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) & ~(1<<5)); /* write '0' */
624 	DELAY(2500); /* wait 2.5ms */
625 	OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
626 	DELAY(2500); /* wait 2.5ms */
627 #endif
628 
629 	/* Check for the presence of i2c devices */
630 	any_i2c_devices = check_for_i2c_devices( bktr );
631 
632 
633 	/* Check for a user specified override on the card selection */
634 #if defined( BKTR_OVERRIDE_CARD )
635 	bktr->card = cards[ (card = BKTR_OVERRIDE_CARD) ];
636 	goto checkEEPROM;
637 #endif
638 	if (bktr->bt848_card != -1 ) {
639 	  bktr->card = cards[ (card = bktr->bt848_card) ];
640 	  goto checkEEPROM;
641 	}
642 
643 
644 	/* No override, so try and determine the make of the card */
645 
646 	/* On BT878/879 cards, read the sub-system vendor id */
647 	/* This identifies the manufacturer of the card and the model */
648 	/* In theory this can be read from PCI registers but this does not */
649 	/* appear to work on the FlyVideo 98. Hauppauge also warned that */
650 	/* the PCI registers are sometimes not loaded correctly. */
651 	/* Therefore, I will read the sub-system vendor ID from the EEPROM */
652 	/* (just like the Bt878 does during power up initialisation) */
653 
654 	if ((bktr->id==BROOKTREE_878) || (bktr->id==BROOKTREE_879)) {
655 	    /* Try and locate the EEPROM */
656 	    eeprom_i2c_address = locate_eeprom_address( bktr );
657 	    if (eeprom_i2c_address != -1) {
658 
659 		unsigned int subsystem_vendor_id; /* vendors PCI-SIG ID */
660 		unsigned int subsystem_id;        /* board model number */
661 		unsigned int byte_252, byte_253, byte_254, byte_255;
662 
663 		bktr->card = cards[ (card = CARD_UNKNOWN) ];
664 		bktr->card.eepromAddr = eeprom_i2c_address;
665 		bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
666 
667 	        if (readEEProm(bktr, 0, 256, eeprom) && bootverbose)
668 			printf("%s: error reading EEPROM\n", bktr_name(bktr));
669 		byte_252 = (unsigned int)eeprom[252];
670 		byte_253 = (unsigned int)eeprom[253];
671 		byte_254 = (unsigned int)eeprom[254];
672 		byte_255 = (unsigned int)eeprom[255];
673 
674 		subsystem_id        = (byte_252 << 8) | byte_253;
675 		subsystem_vendor_id = (byte_254 << 8) | byte_255;
676 
677 	        if (bootverbose)
678 	            printf("%s: subsystem 0x%04x 0x%04x\n", bktr_name(bktr),
679 			   subsystem_vendor_id, subsystem_id);
680 
681 		if (subsystem_vendor_id == PCI_VENDOR_AVERMEDIA) {
682 			bktr->card = cards[ (card = CARD_AVER_MEDIA) ];
683 			bktr->card.eepromAddr = eeprom_i2c_address;
684 			bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
685 			goto checkTuner;
686 		}
687 
688 		if (subsystem_vendor_id == PCI_VENDOR_HAUPPAUGE) {
689 			bktr->card = cards[ (card = CARD_HAUPPAUGE) ];
690 			bktr->card.eepromAddr = eeprom_i2c_address;
691 			bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
692 			goto checkTuner;
693 		}
694 
695 		if ((subsystem_vendor_id == PCI_VENDOR_FLYVIDEO) ||
696 		    (subsystem_vendor_id == PCI_VENDOR_FLYVIDEO_2) ) {
697 			bktr->card = cards[ (card = CARD_FLYVIDEO) ];
698 			bktr->card.eepromAddr = eeprom_i2c_address;
699 			bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
700 			goto checkTuner;
701 		}
702 
703                 if (subsystem_vendor_id == PCI_VENDOR_STB) {
704                     bktr->card = cards[ (card = CARD_STB) ];
705 		    bktr->card.eepromAddr = eeprom_i2c_address;
706 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
707                     goto checkTuner;
708                 }
709 
710                 if (subsystem_vendor_id == PCI_VENDOR_ASKEY) {
711                     bktr->card = cards[ (card = CARD_ASKEY_DYNALINK_MAGIC_TVIEW) ];
712 		    bktr->card.eepromAddr = eeprom_i2c_address;
713 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
714                     goto checkTuner;
715                 }
716 
717                 if ((subsystem_vendor_id == PCI_VENDOR_LEADTEK)
718                  || (subsystem_vendor_id == PCI_VENDOR_LEADTEK_ALT)
719 		 || (subsystem_vendor_id == PCI_VENDOR_LEADTEK_ALT_2)
720 		 || (subsystem_vendor_id == PCI_VENDOR_LEADTEK_ALT_3)) {
721 		    if (subsystem_id == PCI_PRODUCT_LEADTEK_WINFAST_XP)
722 		        bktr->card = cards[ (card = CARD_LEADTEK_XP) ];
723 		    else
724 		        bktr->card = cards[ (card = CARD_LEADTEK) ];
725 		    bktr->card.eepromAddr = eeprom_i2c_address;
726 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
727                     goto checkTuner;
728                 }
729 
730 		if (subsystem_vendor_id == PCI_VENDOR_PINNACLE_ALT) {
731                     bktr->card = cards[ (card = CARD_MIRO) ];
732 		    bktr->card.eepromAddr = eeprom_i2c_address;
733 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
734                     goto checkTuner;
735                 }
736 
737 		if (subsystem_vendor_id == PCI_VENDOR_TERRATEC) {
738                     bktr->card = cards[ (card = CARD_TERRATVPLUS) ];
739 		    bktr->card.eepromAddr = eeprom_i2c_address;
740 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
741                     goto checkTuner;
742                 }
743 
744 		if (subsystem_vendor_id == PCI_VENDOR_ATI) {
745                     bktr->card = cards[ (card = CARD_TVWONDER) ];
746 		    bktr->card.eepromAddr = eeprom_i2c_address;
747 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
748                     goto checkTuner;
749                 }
750 
751 		/*
752 		 * Check which card as the GV-BCTV4 and GV-BCTV5 IODATA make
753 		 * are somewhat different to the GV-BCTV3.
754 		 */
755 
756 		if (subsystem_vendor_id == PCI_VENDOR_IODATA &&
757 		    subsystem_id == PCI_PRODUCT_IODATA_GV_BCTV3) {
758 		    bktr->card = cards[ (card = CARD_IO_BCTV3) ];
759 		    bktr->card.eepromAddr = eeprom_i2c_address;
760 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
761 		    goto checkTuner;
762 		}
763 
764 		if (subsystem_vendor_id == PCI_VENDOR_ZOLTRIX) {
765 		    if (subsystem_id == PCI_PRODUCT_ZOLTRIX_GENIE_TV_FM) {
766 			bktr->card = cards[ (card = CARD_ZOLTRIX_GENIE_FM) ];
767 		    } else {
768 			bktr->card = cards[ (card = CARD_ZOLTRIX) ];
769 		    }
770  		    bktr->card.eepromAddr = eeprom_i2c_address;
771  		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
772 		    goto checkTuner;
773 		}
774 
775                 /* Vendor is unknown. We will use the standard probe code */
776 		/* which may not give best results */
777                 printf("%s: Warning "
778 		    "- card vendor 0x%04x (model 0x%04x) unknown.\n",
779 		    bktr_name(bktr), subsystem_vendor_id, subsystem_id);
780             } else
781                 printf("%s: Card has no configuration EEPROM. "
782 		    "Cannot determine card make.\n", bktr_name(bktr));
783 	} /* end of bt878/bt879 card detection code */
784 
785 	/* If we get to this point, we must have a Bt848/848A/849A card */
786 	/* or a Bt878/879 with an unknown subsystem vendor id */
787         /* Try and determine the make of card by clever i2c probing */
788 
789    	/* Check for i2c devices. If none, move on */
790 	if (!any_i2c_devices) {
791 		bktr->card = cards[ (card = CARD_INTEL) ];
792 		bktr->card.eepromAddr = 0;
793 		bktr->card.eepromSize = 0;
794 		goto checkTuner;
795 	}
796 
797         /* Look for Hauppauge, STB and Osprey cards by the presence */
798 	/* of an EEPROM */
799         /* Note: Bt878 based cards also use EEPROMs so we can only do this */
800         /* test on BT848/848A and 849A based cards. */
801 	if ((bktr->id==BROOKTREE_848)  ||
802 	    (bktr->id==BROOKTREE_848A) ||
803 	    (bktr->id==BROOKTREE_849A)) {
804 
805             /* At i2c address 0xa0, look for Hauppauge and Osprey cards */
806             if ( (status = i2cRead( bktr, PFC8582_RADDR )) != ABSENT ) {
807 
808 		    /* Read the eeprom contents */
809 		    bktr->card = cards[ (card = CARD_UNKNOWN) ];
810 		    bktr->card.eepromAddr = PFC8582_WADDR;
811 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
812 	            readEEProm(bktr, 0, 128, eeprom );
813 
814 		    /* For Hauppauge, check the EEPROM begins with 0x84 */
815 		    if (eeprom[0] == 0x84) {
816                             bktr->card = cards[ (card = CARD_HAUPPAUGE) ];
817 			    bktr->card.eepromAddr = PFC8582_WADDR;
818 			    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
819                             goto checkTuner;
820 		    }
821 
822 		    /* For Osprey, check the EEPROM begins with "MMAC" */
823 		    if (  (eeprom[0] == 'M') &&(eeprom[1] == 'M')
824 			&&(eeprom[2] == 'A') &&(eeprom[3] == 'C')) {
825                             bktr->card = cards[ (card = CARD_OSPREY) ];
826 			    bktr->card.eepromAddr = PFC8582_WADDR;
827 			    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
828                             goto checkTuner;
829 		    }
830 		    printf("%s: Warning: Unknown card type. EEPROM data not recognised\n",
831 			   bktr_name(bktr));
832 		    printf("%s: %x %x %x %x\n", bktr_name(bktr),
833 			   eeprom[0],eeprom[1],eeprom[2],eeprom[3]);
834             }
835 
836             /* look for an STB card */
837             if ( (status = i2cRead( bktr, X24C01_RADDR )) != ABSENT ) {
838                     bktr->card = cards[ (card = CARD_STB) ];
839 		    bktr->card.eepromAddr = X24C01_WADDR;
840 		    bktr->card.eepromSize = (u_char)(128 / EEPROMBLOCKSIZE);
841                     goto checkTuner;
842             }
843 
844 	}
845 
846 	signCard( bktr, 1, 128, (u_char *)  &probe_signature );
847 
848 	if (bootverbose) {
849 	  printf("%s: card signature: ", bktr_name(bktr));
850 	  for (j = 0; j < Bt848_MAX_SIGN; j++) {
851 	    printf(" %02x ", probe_signature[j]);
852 	  }
853 	  printf("\n\n");
854 	}
855 	for (i = 0;
856 	     i < (sizeof bt848_card_signature)/ sizeof (struct bt848_card_sig);
857 	     i++ ) {
858 
859 	  card_found = 1;
860 	  probe_temp = (u_char *) &bt848_card_signature[i].signature;
861 
862 	  for (j = 0; j < Bt848_MAX_SIGN; j++) {
863 	    if ((probe_temp[j] & 0xf) != (probe_signature[j] & 0xf)) {
864 	      card_found = 0;
865 	      break;
866 	    }
867 
868 	  }
869 	  if (card_found) {
870 	    bktr->card = cards[ card = bt848_card_signature[i].card];
871 	    eeprom_i2c_address = locate_eeprom_address( bktr );
872 	    if (eeprom_i2c_address != -1) {
873 		bktr->card.eepromAddr = eeprom_i2c_address;
874 		bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
875 	    } else {
876 		bktr->card.eepromAddr = 0;
877 		bktr->card.eepromSize = 0;
878 	    }
879 	    tuner_i2c_address = locate_tuner_address( bktr );
880 	    select_tuner( bktr, bt848_card_signature[i].tuner );
881 	    goto checkDBX;
882 	  }
883 	}
884 
885 	/* We do not know the card type. Default to Miro */
886 	bktr->card = cards[ (card = CARD_MIRO) ];
887 
888 
889 checkEEPROM:
890 	/* look for a configuration eeprom */
891 	eeprom_i2c_address = locate_eeprom_address( bktr );
892 	if (eeprom_i2c_address != -1) {
893 	    bktr->card.eepromAddr = eeprom_i2c_address;
894 	    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
895 	} else {
896 	    bktr->card.eepromAddr = 0;
897 	    bktr->card.eepromSize = 0;
898 	}
899 
900 
901 checkTuner:
902 
903 	/* look for a tuner */
904 	tuner_i2c_address = locate_tuner_address( bktr );
905 	if ( tuner_i2c_address == -1 ) {
906 		select_tuner( bktr, NO_TUNER );
907 		goto checkDBX;
908 	}
909 
910 #if defined( BKTR_OVERRIDE_TUNER )
911 	select_tuner( bktr, BKTR_OVERRIDE_TUNER );
912 	goto checkDBX;
913 #endif
914 	if (bktr->bt848_tuner != -1 ) {
915 	  select_tuner( bktr, bktr->bt848_tuner & 0xff );
916 	  goto checkDBX;
917 	}
918 
919 	/* Check for i2c devices */
920 	if (!any_i2c_devices) {
921 		select_tuner( bktr, NO_TUNER );
922 		goto checkDBX;
923 	}
924 
925 	/* differentiate type of tuner */
926 
927 	switch (card) {
928 	case CARD_MIRO:
929 	    switch (((INL(bktr, BKTR_GPIO_DATA) >> 10)-1)&7) {
930 	    case 0: select_tuner( bktr, TEMIC_PAL ); break;
931 	    case 1: select_tuner( bktr, PHILIPS_PAL ); break;
932 	    case 2: select_tuner( bktr, PHILIPS_NTSC ); break;
933 	    case 3: select_tuner( bktr, PHILIPS_SECAM ); break;
934 	    case 4: select_tuner( bktr, NO_TUNER ); break;
935 	    case 5: select_tuner( bktr, PHILIPS_PALI ); break;
936 	    case 6: select_tuner( bktr, TEMIC_NTSC ); break;
937 	    case 7: select_tuner( bktr, TEMIC_PALI ); break;
938 	    }
939 	    goto checkDBX;
940 	    break;
941 
942 	case CARD_HAUPPAUGE:
943 	    /* Hauppauge kindly supplied the following Tuner Table */
944 	    /* FIXME: I think the tuners the driver selects for types */
945 	    /* 0x08 and 0x15 may be incorrect but no one has complained. */
946 	    /* Old Temic tuners had their own API, but newer Temic tuners */
947 	    /* have the same API as Philips tuners */
948 	    /*
949   ID  Tuner Model           Format			We select Format
950  0x00 NONE
951  0x01 EXTERNAL
952  0x02 OTHER
953  0x03 Philips FI1216        BG
954  0x04 Philips FI1216MF      BGLL'			PHILIPS_SECAM
955  0x05 Philips FI1236        MN 				PHILIPS_NTSC
956  0x06 Philips FI1246        I 				PHILIPS_PALI
957  0x07 Philips FI1256        DK
958  0x08 Philips FI1216 MK2    BG 				PHILIPS_PALI
959  0x09 Philips FI1216MF MK2  BGLL' 			PHILIPS_SECAM
960  0x0a Philips FI1236 MK2    MN 				PHILIPS_NTSC
961  0x0b Philips FI1246 MK2    I 				PHILIPS_PALI
962  0x0c Philips FI1256 MK2    DK
963  0x0d Temic 4032FY5         NTSC			TEMIC_NTSC
964  0x0e Temic 4002FH5         BG				TEMIC_PAL
965  0x0f Temic 4062FY5         I 				TEMIC_PALI
966  0x10 Philips FR1216 MK2    BG
967  0x11 Philips FR1216MF MK2  BGLL' 			PHILIPS_FR1236_SECAM
968  0x12 Philips FR1236 MK2    MN 				PHILIPS_FR1236_NTSC
969  0x13 Philips FR1246 MK2    I
970  0x14 Philips FR1256 MK2    DK
971  0x15 Philips FM1216        BG 				PHILIPS_FR1216_PAL
972  0x16 Philips FM1216MF      BGLL' 			PHILIPS_FR1236_SECAM
973  0x17 Philips FM1236        MN 				PHILIPS_FR1236_NTSC
974  0x18 Philips FM1246        I
975  0x19 Philips FM1256        DK
976  0x1a Temic 4036FY5         MN (FI1236 MK2 clone)	PHILIPS_NTSC
977  0x1b Samsung TCPN9082D     MN
978  0x1c Samsung TCPM9092P     Pal BG/I/DK
979  0x1d Temic 4006FH5         BG				PHILIPS_PALI
980  0x1e Samsung TCPN9085D     MN/Radio
981  0x1f Samsung TCPB9085P     Pal BG/I/DK / Radio
982  0x20 Samsung TCPL9091P     Pal BG & Secam L/L'
983  0x21 Temic 4039FY5         NTSC Radio
984  0x22 Philips FQ1216ME      Pal BGIDK & Secam L/L'
985  0x23 Temic 4066FY5         Pal I (FI1246 MK2 clone)	PHILIPS_PALI
986  0x24 Philips TD1536        MN/ATSCDigital
987  0x25 Philips TD1536D       MN/ATSCDigital DUAL INPUT
988  0x26 Philips FMR1236       M/N FM(no demod)
989  0x27 Philips FI1256MP      B/G, D/K
990  0x28 Samsung TCPQ9091P     BG/I/DK, L/L'
991  0x29 Temic 4006FN5         BG/I/DK
992  0x2a Temic 4009FR5         BG FM			PHILIPS_FR1216_PAL
993  0x2b Temic 4046FM5         B/G, I, D/K, L/L'
994  0x2c Temic 4009FN5         B/G, I, D/K, FM (no demod)
995  0x2d Philips TD1536D_FH_44 MN/ATSCDigital DUAL INPUT
996 	    */
997 
998 
999 	    /* Determine the model number from the eeprom */
1000 	    if (bktr->card.eepromAddr != 0) {
1001 	        /* eeprom data block structure */
1002 	        unsigned char *block_1, *block_2, *block_3, *block_4;
1003 	        int block_1_data_size,  block_2_data_size, block_3_data_size;
1004 	        int block_1_total_size, block_2_total_size, block_3_total_size;
1005 		int block_4_header_size;
1006 
1007 		unsigned int model,revision;
1008 		unsigned char tuner_code;
1009 		unsigned char no_audio_mux;
1010 
1011 		readEEProm(bktr, 0, 128, eeprom);
1012 
1013 	        /* LOCATE THE EEPROM DATA BLOCKS */
1014 	        block_1 = &eeprom[0];
1015 	        block_1_data_size = (block_1[2] << 8 | block_1[1]);
1016 	        block_1_total_size = block_1_data_size + 3; /* Header bytes */
1017 
1018 	        block_2 = &eeprom[block_1_total_size];
1019 	        block_2_data_size = (block_2[2] << 8 | block_2[1]);
1020 	        block_2_total_size = block_2_data_size + 3; /* Header bytes */
1021 
1022 	        block_3 = &eeprom[block_1_total_size + block_2_total_size];
1023 	        block_3_data_size = (block_3[0] &0x07);
1024 	        block_3_total_size = block_3_data_size + 1; /* Header size */
1025 
1026 	        block_4 = &eeprom[block_1_total_size +block_2_total_size +block_3_total_size];
1027 	        block_4_header_size = 1;
1028 
1029 		model    = (block_1[12] << 8  | block_1[11]);
1030 		revision = (block_1[15] << 16 | block_1[14] << 8 | block_1[13]);
1031 
1032 		tuner_code = block_1[9];
1033 
1034 		no_audio_mux = ((block_3[3] >> 7) &0x01);
1035 
1036 		if (no_audio_mux) bktr->audio_mux_present = 0;
1037 
1038 		if (verbose)
1039 		    printf("%s: Hauppauge Model %d %c%c%c%c\n",
1040 			   bktr_name(bktr),
1041 			   model,
1042 			   ((revision >> 18) & 0x3f) + 32,
1043 			   ((revision >> 12) & 0x3f) + 32,
1044 			   ((revision >>  6) & 0x3f) + 32,
1045 			   ((revision >>  0) & 0x3f) + 32 );
1046 
1047 	        /* Determine the tuner type from the eeprom */
1048 
1049 		switch (tuner_code) {
1050 
1051 		  case 0x5:
1052                   case 0x0a:
1053                   case 0x1a:
1054 		    select_tuner( bktr, PHILIPS_NTSC );
1055 		    goto checkDBX;
1056 
1057 		  case 0x4:
1058                   case 0x9:
1059 		    select_tuner( bktr, PHILIPS_SECAM );
1060 		    goto checkDBX;
1061 
1062                   case 0x11:
1063 	          case 0x16:
1064 		    select_tuner( bktr, PHILIPS_FR1236_SECAM );
1065 		    goto checkDBX;
1066 
1067 		  case 0x12:
1068 		  case 0x17:
1069 		  case 0x21:	/* hauppage #191 wintv-fm -wsr 2003/1/13 */
1070 		    select_tuner( bktr, PHILIPS_FR1236_NTSC );
1071 		    goto checkDBX;
1072 
1073 		  case 0x6:
1074 	          case 0x8:
1075 	          case 0xb:
1076 	          case 0x1d:
1077 	          case 0x23:
1078 		    select_tuner( bktr, PHILIPS_PALI );
1079 		    goto checkDBX;
1080 
1081 	          case 0xd:
1082 		    select_tuner( bktr, TEMIC_NTSC );
1083 		    goto checkDBX;
1084 
1085                   case 0xe:
1086 		    select_tuner( bktr, TEMIC_PAL );
1087 		    goto checkDBX;
1088 
1089 	          case 0xf:
1090 		    select_tuner( bktr, TEMIC_PALI );
1091 		    goto checkDBX;
1092 
1093                   case 0x15:
1094 		    select_tuner( bktr, PHILIPS_FR1216_PAL );
1095 		    goto checkDBX;
1096 
1097                   case 0x2a:
1098 		    bktr->msp_use_mono_source = 1;
1099 		    select_tuner( bktr, PHILIPS_FR1216_PAL );
1100 		    goto checkDBX;
1101 
1102 	          default :
1103 		    printf("%s: Warning - Unknown Hauppauge Tuner 0x%x\n",
1104 			   bktr_name(bktr), tuner_code);
1105 		}
1106 	    }
1107 	    break;
1108 
1109 
1110 	case CARD_AVER_MEDIA:
1111 	    /* AVerMedia kindly supplied some details of their EEPROM contents
1112 	     * which allow us to auto select the Tuner Type.
1113 	     * Only the newer AVerMedia cards actually have an EEPROM.
1114 	     */
1115 	    if (bktr->card.eepromAddr != 0) {
1116 
1117 		u_char tuner_make;   /* Eg Philips, Temic */
1118 		u_char tuner_tv_fm;  /* TV or TV with FM Radio */
1119 		u_char tuner_format; /* Eg NTSC, PAL, SECAM */
1120 		int    tuner;
1121 
1122 		int tuner_0_table[] = {
1123 			PHILIPS_NTSC,  PHILIPS_PAL,
1124 			PHILIPS_PAL,   PHILIPS_PAL,
1125 			PHILIPS_PAL,   PHILIPS_PAL,
1126 			PHILIPS_SECAM, PHILIPS_SECAM,
1127 			PHILIPS_SECAM, PHILIPS_PAL};
1128 
1129 		int tuner_0_fm_table[] = {
1130 			PHILIPS_FR1236_NTSC,  PHILIPS_FR1216_PAL,
1131 			PHILIPS_FR1216_PAL,   PHILIPS_FR1216_PAL,
1132 			PHILIPS_FR1216_PAL,   PHILIPS_FR1216_PAL,
1133 			PHILIPS_FR1236_SECAM, PHILIPS_FR1236_SECAM,
1134 			PHILIPS_FR1236_SECAM, PHILIPS_FR1216_PAL};
1135 
1136 		int tuner_1_table[] = {
1137 			TEMIC_NTSC,  TEMIC_PAL,   TEMIC_PAL,
1138 			TEMIC_PAL,   TEMIC_PAL,   TEMIC_PAL,
1139 			TEMIC_SECAM, TEMIC_SECAM, TEMIC_SECAM,
1140 			TEMIC_PAL};
1141 
1142 
1143 		/* Extract information from the EEPROM data */
1144 	    	readEEProm(bktr, 0, 128, eeprom);
1145 
1146 		tuner_make   = (eeprom[0x41] & 0x7);
1147 		tuner_tv_fm  = (eeprom[0x41] & 0x18) >> 3;
1148 		tuner_format = (eeprom[0x42] & 0xf0) >> 4;
1149 
1150 		/* Treat tuner make 0 (Philips) and make 2 (LG) the same */
1151 		if ( ((tuner_make == 0) || (tuner_make == 2))
1152 		    && (tuner_format <= 9) && (tuner_tv_fm == 0) ) {
1153 			tuner = tuner_0_table[tuner_format];
1154 			select_tuner( bktr, tuner );
1155 			goto checkDBX;
1156 		}
1157 
1158 		if ( ((tuner_make == 0) || (tuner_make == 2))
1159 		    && (tuner_format <= 9) && (tuner_tv_fm == 1) ) {
1160 			tuner = tuner_0_fm_table[tuner_format];
1161 			select_tuner( bktr, tuner );
1162 			goto checkDBX;
1163 		}
1164 
1165 		if ( (tuner_make == 1) && (tuner_format <= 9) ) {
1166 			tuner = tuner_1_table[tuner_format];
1167 			select_tuner( bktr, tuner );
1168 			goto checkDBX;
1169 		}
1170 
1171 	    	printf("%s: Warning - Unknown AVerMedia Tuner Make %d Format %d\n",
1172 			bktr_name(bktr), tuner_make, tuner_format);
1173 	    }
1174 	    break;
1175 
1176 	case CARD_LEADTEK:
1177 #if BKTR_SYSTEM_DEFAULT == BROOKTREE_PAL
1178 	    select_tuner( bktr, PHILIPS_FR1216_PAL );
1179 #else
1180 	    select_tuner( bktr, PHILIPS_FR1236_NTSC );
1181 #endif
1182             goto checkDBX;
1183 	    break;
1184 
1185 	case CARD_TVWONDER:
1186 	    select_tuner( bktr, PHILIPS_NTSC );
1187 	    goto checkDBX;
1188 	    break;
1189 
1190 	case CARD_ZOLTRIX_GENIE_FM:
1191 	    select_tuner( bktr, PHILIPS_FR1236_NTSC );
1192 	    goto checkDBX;
1193 	    break;
1194 
1195 	case CARD_IO_BCTV3:
1196 	    select_tuner( bktr, ALPS_TSCH5 ); /* ALPS_TSCH6, in fact. */
1197 	    goto checkDBX;
1198  	    break;
1199 
1200 	case CARD_LEADTEK_XP:
1201 	    select_tuner( bktr, TIVISION_TVF5533 );
1202 	    goto checkDBX;
1203 	    break;
1204 
1205 	} /* end switch(card) */
1206 
1207 
1208 	/* At this point, a goto checkDBX has not occurred */
1209 	/* We have not been able to select a Tuner */
1210 	/* Some cards make use of the tuner address to */
1211 	/* identify the make/model of tuner */
1212 
1213 	/* At address 0xc0/0xc1 we often find a TEMIC NTSC */
1214 	if ( i2cRead( bktr, 0xc1 ) != ABSENT ) {
1215 	    select_tuner( bktr, TEMIC_NTSC );
1216 	    goto checkDBX;
1217 	}
1218 
1219 	/* At address 0xc6/0xc7 we often find a PHILIPS NTSC Tuner */
1220 	if ( i2cRead( bktr, 0xc7 ) != ABSENT ) {
1221 	    select_tuner( bktr, PHILIPS_NTSC );
1222 	    goto checkDBX;
1223 	}
1224 
1225 	/* Address 0xc2/0xc3 is default (or common address) for several */
1226 	/* tuners and we cannot tell which is which. */
1227 	/* And for all other tuner i2c addresses, select the default */
1228 	select_tuner( bktr, DEFAULT_TUNER );
1229 
1230 
1231 checkDBX:
1232 #if defined( BKTR_OVERRIDE_DBX )
1233 	bktr->card.dbx = BKTR_OVERRIDE_DBX;
1234 	goto checkMSP;
1235 #endif
1236    /* Check for i2c devices */
1237 	if (!any_i2c_devices) {
1238 		goto checkMSP;
1239 	}
1240 
1241 	/* probe for BTSC (dbx) chip */
1242 	if ( i2cRead( bktr, TDA9850_RADDR ) != ABSENT )
1243 		bktr->card.dbx = 1;
1244 
1245 checkMSP:
1246 	/* If this is a Hauppauge Bt878 card, we need to enable the
1247 	 * MSP 34xx audio chip.
1248 	 * If this is a Hauppauge Bt848 card, reset the MSP device.
1249 	 * The MSP reset line is wired to GPIO pin 5. On Bt878 cards a pulldown
1250 	 * resistor holds the device in reset until we set GPIO pin 5.
1251 	 */
1252 
1253 	/* Optionally skip the MSP reset. This is handy if you initialise the
1254 	 * MSP audio in another operating system (eg Windows) first and then
1255 	 * do a soft reboot.
1256 	 */
1257 
1258 #ifndef BKTR_NO_MSP_RESET
1259 	if (card == CARD_HAUPPAUGE) {
1260 	    OUTL(bktr, BKTR_GPIO_OUT_EN, INL(bktr, BKTR_GPIO_OUT_EN) | (1<<5));
1261 	    OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
1262 	    DELAY(2500); /* wait 2.5ms */
1263 	    OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) & ~(1<<5)); /* write '0' */
1264 	    DELAY(2500); /* wait 2.5ms */
1265 	    OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
1266 	    DELAY(2500); /* wait 2.5ms */
1267 	}
1268 #endif
1269 
1270 #if defined( BKTR_OVERRIDE_MSP )
1271 	bktr->card.msp3400c = BKTR_OVERRIDE_MSP;
1272 	goto checkMSPEnd;
1273 #endif
1274 
1275 	/* Check for i2c devices */
1276 	if (!any_i2c_devices) {
1277 		goto checkMSPEnd;
1278 	}
1279 
1280 	if ( i2cRead( bktr, MSP3400C_RADDR ) != ABSENT ) {
1281 		bktr->card.msp3400c = 1;
1282 	}
1283 
1284 checkMSPEnd:
1285 
1286 	if (bktr->card.msp3400c) {
1287 		bktr->msp_addr = MSP3400C_WADDR;
1288 		msp_read_id( bktr );
1289 		printf("%s: Detected a MSP%s at 0x%x\n", bktr_name(bktr),
1290 		       bktr->msp_version_string,
1291 		       bktr->msp_addr);
1292 
1293 	}
1294 
1295 /* Check for Dolby Surround Sound DPL3518A sound chip */
1296 	if ( i2cRead( bktr, DPL3518A_RADDR ) != ABSENT ) {
1297 		bktr->card.dpl3518a = 1;
1298 	}
1299 
1300 	if (bktr->card.dpl3518a) {
1301 		bktr->dpl_addr = DPL3518A_WADDR;
1302 		dpl_read_id( bktr );
1303 		printf("%s: Detected a DPL%s at 0x%x\n", bktr_name(bktr),
1304 		       bktr->dpl_version_string,
1305 		       bktr->dpl_addr);
1306 	}
1307 
1308 /* Start of Check Remote */
1309 	/* Check for the Hauppauge IR Remote Control */
1310 	/* If there is an external unit, the internal will be ignored */
1311 
1312 	bktr->remote_control = 0; /* initial value */
1313 
1314 	if (any_i2c_devices) {
1315 		if (i2cRead( bktr, HAUP_REMOTE_EXT_RADDR ) != ABSENT ) {
1316 			bktr->remote_control      = 1;
1317 			bktr->remote_control_addr = HAUP_REMOTE_EXT_RADDR;
1318 		} else if (i2cRead( bktr, HAUP_REMOTE_INT_RADDR ) != ABSENT ) {
1319 			bktr->remote_control      = 1;
1320 			bktr->remote_control_addr = HAUP_REMOTE_INT_RADDR;
1321 		}
1322 	}
1323 
1324 	/* If a remote control is found, poll it 5 times to turn off the LED */
1325 	if (bktr->remote_control) {
1326 		int i;
1327 		for (i=0; i<5; i++)
1328 			i2cRead( bktr, bktr->remote_control_addr );
1329 	}
1330 /* End of Check Remote */
1331 
1332 #if defined( BKTR_USE_PLL )
1333 	bktr->xtal_pll_mode = BT848_USE_PLL;
1334 	goto checkPLLEnd;
1335 #endif
1336 	/* Default is to use XTALS and not PLL mode */
1337 	bktr->xtal_pll_mode = BT848_USE_XTALS;
1338 
1339 	/* Enable PLL mode for OSPREY users */
1340 	if (card == CARD_OSPREY)
1341 		bktr->xtal_pll_mode = BT848_USE_PLL;
1342 
1343 	/* Enable PLL mode for Video Highway Xtreme users */
1344 	if (card == CARD_VIDEO_HIGHWAY_XTREME)
1345 		bktr->xtal_pll_mode = BT848_USE_PLL;
1346 
1347 	/* Enable PLL mode for ATI TV_Wonder/VE */
1348 	if (card == CARD_TVWONDER)
1349 		bktr->xtal_pll_mode = BT848_USE_PLL;
1350 
1351 
1352 	/* Most (perhaps all) Bt878 cards need to be switched to PLL mode */
1353 	/* as they only fit the NTSC crystal to their cards */
1354 	/* Default to enabling PLL mode for all Bt878/879 cards */
1355 
1356 	if ((bktr->id==BROOKTREE_878 || bktr->id==BROOKTREE_879) )
1357 		bktr->xtal_pll_mode = BT848_USE_PLL;
1358 
1359 
1360 #if defined( BKTR_USE_PLL )
1361 checkPLLEnd:
1362 #endif
1363 
1364 
1365 	bktr->card.tuner_pllAddr = tuner_i2c_address;
1366 
1367 	if ( verbose ) {
1368 		printf( "%s: %s", bktr_name(bktr), bktr->card.name );
1369 		if ( bktr->card.tuner )
1370 			printf( ", %s tuner", bktr->card.tuner->name );
1371 		if ( bktr->card.dbx )
1372 			printf( ", dbx stereo" );
1373 		if ( bktr->card.msp3400c )
1374 			printf( ", msp3400c stereo" );
1375 		if ( bktr->card.dpl3518a )
1376 			printf( ", dpl3518a dolby" );
1377 		if ( bktr->remote_control )
1378 			printf( ", remote control" );
1379 		printf( ".\n" );
1380 	}
1381 }
1382 
1383 #undef ABSENT
1384