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