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