Home | History | Annotate | Line # | Download | only in bktr
bktr_card.c revision 1.21
      1 /* $SourceForge: bktr_card.c,v 1.3 2003/03/11 23:11:21 thomasklausner Exp $ */
      2 
      3 /*	$NetBSD: bktr_card.c,v 1.21 2005/12/06 23:43:57 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.21 2005/12/06 23:43:57 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         {  CARD_TERRATVALUE,                    /* the card id */
    352           "Terratec TValue",                    /* the 'name' */
    353            NULL,                                /* the tuner */
    354            0,
    355            0,
    356            0,
    357            0,
    358            0,                                   /* EEProm type */
    359            0,                                   /* EEProm size */
    360            { 0x500, 0x900, 0x300, 0x900, 1 },   /* audio MUX values*/
    361            0xffff00 },                          /* GPIO mask */
    362 
    363 };
    364 
    365 struct bt848_card_sig bt848_card_signature[1]= {
    366   /* IMS TURBO TV : card 5 */
    367     {  5,9, {00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 02, 00, 00, 00}}
    368 
    369 
    370 };
    371 
    372 
    373 /*
    374  * Write to the configuration EEPROM on the card.
    375  * This is dangerous and will mess up your card. Therefore it is not
    376  * implemented.
    377  */
    378 int
    379 writeEEProm(bktr_ptr_t bktr, int offset, int count, u_char *data)
    380 {
    381         return(-1);
    382 }
    383 
    384 /*
    385  * Read the contents of the configuration EEPROM on the card.
    386  * (This is not fitted to all makes of card. All Hauppauge cards have them
    387  * and so do newer Bt878 based cards.
    388  */
    389 int
    390 readEEProm(bktr_ptr_t bktr, int offset, int count, u_char *data)
    391 {
    392 	int	x;
    393 	int	addr;
    394 	int	xmax;
    395 	int	byte;
    396 
    397 	/* get the address of the EEProm */
    398 	addr = (int)(bktr->card.eepromAddr & 0xff);
    399 	if (addr == 0)
    400 		return(-1);
    401 
    402 	xmax = (int)(bktr->card.eepromSize * EEPROMBLOCKSIZE);
    403 	if ((offset + count) > xmax)
    404 		return(-1);
    405 
    406 	/* set the start address */
    407 	if (i2cWrite(bktr, addr, offset, -1) == -1)
    408 		return(-1);
    409 
    410 	/* the read cycle */
    411 	for (x = 0; x < count; ++x) {
    412 		if ((byte = i2cRead(bktr, (addr | 1))) == -1)
    413 			return(-1);
    414 		data[x] = byte;
    415 	}
    416 
    417 	return(0);
    418 }
    419 
    420 
    421 #define ABSENT		(-1)
    422 
    423 /*
    424  * get a signature of the card
    425  * read all 128 possible i2c read addresses from 0x01 thru 0xff
    426  * build a bit array with a 1 bit for each i2c device that responds
    427  *
    428  * XXX FIXME: use offset & count args
    429  */
    430 int
    431 signCard(bktr_ptr_t bktr, int offset, int count, u_char* sig)
    432 {
    433 	int	x;
    434 
    435 	for (x = 0; x < 16; ++x)
    436 		sig[x] = 0;
    437 
    438 	for (x = 0; x < count; ++x) {
    439 		if (i2cRead(bktr, (2 * x) + 1) != ABSENT) {
    440 			sig[x / 8] |= (1 << (x % 8));
    441 		}
    442 	}
    443 
    444 	return(0);
    445 }
    446 
    447 
    448 /*
    449  * check_for_i2c_devices.
    450  * Some BT848 cards have no tuner and no additional i2c devices
    451  * eg stereo decoder. These are used for video conferencing or capture from
    452  * a video camera. (eg VideoLogic Captivator PCI, Intel SmartCapture card).
    453  *
    454  * Determine if there are any i2c devices present. There are none present if
    455  *  a) reading from all 128 devices returns ABSENT (-1) for each one
    456  *     (eg VideoLogic Captivator PCI with BT848)
    457  *  b) reading from all 128 devices returns 0 for each one
    458  *     (eg VideoLogic Captivator PCI rev. 2F with BT848A)
    459  */
    460 static int check_for_i2c_devices(bktr_ptr_t bktr) {
    461   int x, temp_read;
    462   int i2c_all_0 = 1;
    463   int i2c_all_absent = 1;
    464   for (x = 0; x < 128; ++x) {
    465     temp_read = i2cRead(bktr, (2 * x) + 1);
    466     if (temp_read != 0)      i2c_all_0 = 0;
    467     if (temp_read != ABSENT) i2c_all_absent = 0;
    468   }
    469 
    470   if ((i2c_all_0) || (i2c_all_absent)) return 0;
    471   else return 1;
    472 }
    473 
    474 
    475 /*
    476  * Temic/Philips datasheets say tuners can be at i2c addresses 0xc0, 0xc2,
    477  * 0xc4 or 0xc6, settable by links on the tuner.
    478  * Determine the actual address used on the TV card by probing read addresses.
    479  */
    480 static int locate_tuner_address(bktr_ptr_t bktr) {
    481   if (i2cRead(bktr, 0xc1) != ABSENT) return 0xc0;
    482   if (i2cRead(bktr, 0xc3) != ABSENT) return 0xc2;
    483   if (i2cRead(bktr, 0xc5) != ABSENT) return 0xc4;
    484   if (i2cRead(bktr, 0xc7) != ABSENT) return 0xc6;
    485   return -1; /* no tuner found */
    486 }
    487 
    488 
    489 /*
    490  * Search for a configuration EEPROM on the i2c bus by looking at i2c addresses
    491  * where EEPROMs are usually found.
    492  * On some cards, the EEPROM appears in several locations, but all in the
    493  * range 0xa0 to 0xae.
    494  */
    495 static int locate_eeprom_address(bktr_ptr_t bktr) {
    496   if (i2cRead(bktr, 0xa0) != ABSENT) return 0xa0;
    497   if (i2cRead(bktr, 0xac) != ABSENT) return 0xac;
    498   if (i2cRead(bktr, 0xae) != ABSENT) return 0xae;
    499   return -1; /* no eeprom found */
    500 }
    501 
    502 
    503 /*
    504  * determine the card brand/model
    505  * BKTR_OVERRIDE_CARD, BKTR_OVERRIDE_TUNER, BKTR_OVERRIDE_DBX and
    506  * BKTR_OVERRIDE_MSP can be used to select a specific device,
    507  * regardless of the autodetection and i2c device checks.
    508  *
    509  * The scheme used for probing cards faces these problems:
    510  *  It is impossible to work out which type of tuner is actually fitted,
    511  *  (the driver cannot tell if the Tuner is PAL or NTSC, Temic or Philips)
    512  *  It is impossible to determine what audio-mux hardware is connected.
    513  *  It is impossible to determine if there is extra hardware connected to the
    514  *  GPIO pins  (eg radio chips or MSP34xx reset logic)
    515  *
    516  * However some makes of card (eg Hauppauge) come with a configuration eeprom
    517  * which tells us the make of the card. Most eeproms also tell us the
    518  * tuner type and other features of the cards.
    519  *
    520  * The current probe code works as follows
    521  * A) If the card uses a Bt878/879:
    522  *   1) Read the sub-system vendor id from the configuration EEPROM.
    523  *      Select the required tuner, audio mux arrangement and any other
    524  *      onboard features. If this fails, move to step B.
    525  * B) If it card uses a Bt848, 848A, 849A or an unknown Bt878/879:
    526  *   1) Look for I2C devices. If there are none fitted, it is an Intel or
    527  *      VideoLogic cards.
    528  *   2) Look for a configuration EEPROM.
    529  *   2a) If there is one at I2C address 0xa0 it may be
    530  *       a Hauppauge or an Osprey. Check the EEPROM contents to determine which
    531  *       one it is. For Hauppauge, select the tuner type and audio hardware.
    532  *   2b) If there is an EEPROM at I2C address 0xa8 it will be an STB card.
    533  *       We still have to guess on the tuner type.
    534  *
    535  * C) If we do not know the card type from (A) or (B), guess at the tuner
    536  *    type based on the I2C address of the tuner.
    537  *
    538  * D) After determining the Tuner Type, we probe the i2c bus for other
    539  *    devices at known locations, eg IR-Remote Control, MSP34xx and TDA
    540  *    stereo chips.
    541  */
    542 
    543 
    544 /*
    545  * These are the sub-system vendor ID codes stored in the
    546  * configuration EEPROM used on Bt878/879 cards. They should match the
    547  * number assigned to the company by the PCI Special Interest Group
    548  */
    549 #ifndef __NetBSD__
    550 #define PCI_VENDOR_HAUPPAUGE	0x0070
    551 #define PCI_VENDOR_AVERMEDIA	0x1461
    552 #define PCI_VENDOR_STB		0x10B4
    553 #define PCI_VENDOR_ASKEY	0x144F
    554 #define PCI_VENDOR_TERRATEC	0x153B
    555 #endif
    556 /* Following not confirmed with http://www.pcidatabase.com/,
    557    so not added to NetBSD's pcidevs */
    558 #define PCI_VENDOR_LEADTEK_ALT	0x6606
    559 #define PCI_VENDOR_LEADTEK_ALT_2	0x6607
    560 #define PCI_VENDOR_LEADTEK_ALT_3	0x107D
    561 #define PCI_VENDOR_FLYVIDEO	0x1851
    562 #define PCI_VENDOR_FLYVIDEO_2	0x1852
    563 #define PCI_VENDOR_PINNACLE_ALT	0xBD11
    564 
    565 #define MODEL_TERRATVALUE_1118	0x1118
    566 #define MODEL_TERRATVALUE_1134	0x1134
    567 
    568 void
    569 probeCard(bktr_ptr_t bktr, int verbose, int unit)
    570 {
    571 	int		card, i,j, card_found;
    572 	int		status;
    573 	u_char 		probe_signature[128], *probe_temp;
    574         int   		any_i2c_devices;
    575 	u_char 		eeprom[256];
    576 	int 		tuner_i2c_address = -1;
    577 	int 		eeprom_i2c_address = -1;
    578 
    579 	/* Select all GPIO bits as inputs */
    580 	OUTL(bktr, BKTR_GPIO_OUT_EN, 0);
    581 	if (bootverbose)
    582 	    printf("%s: GPIO is 0x%08x\n", bktr_name(bktr),
    583 		   INL(bktr, BKTR_GPIO_DATA));
    584 
    585 #ifdef HAUPPAUGE_MSP_RESET
    586 	/* Reset the MSP34xx audio chip. This resolves bootup card
    587 	 * detection problems with old Bt848 based Hauppauge cards with
    588 	 * MSP34xx stereo audio chips. This must be user enabled because
    589 	 * at this point the probe function does not know the card type. */
    590         OUTL(bktr, BKTR_GPIO_OUT_EN, INL(bktr, BKTR_GPIO_OUT_EN) | (1<<5));
    591         OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
    592         DELAY(2500); /* wait 2.5ms */
    593         OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) & ~(1<<5)); /* write '0' */
    594         DELAY(2500); /* wait 2.5ms */
    595         OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
    596         DELAY(2500); /* wait 2.5ms */
    597 #endif
    598 
    599 	/* Check for the presence of i2c devices */
    600         any_i2c_devices = check_for_i2c_devices(bktr);
    601 
    602 
    603 	/* Check for a user specified override on the card selection */
    604 #if defined(BKTR_OVERRIDE_CARD)
    605 	bktr->card = cards[(card = BKTR_OVERRIDE_CARD)];
    606 	goto checkEEPROM;
    607 #endif
    608 	if (bktr->bt848_card != -1) {
    609 	  bktr->card = cards[(card = bktr->bt848_card)];
    610 	  goto checkEEPROM;
    611 	}
    612 
    613 
    614 	/* No override, so try and determine the make of the card */
    615 
    616         /* On BT878/879 cards, read the sub-system vendor id */
    617 	/* This identifies the manufacturer of the card and the model */
    618 	/* In theory this can be read from PCI registers but this does not */
    619 	/* appear to work on the FlyVideo 98. Hauppauge also warned that */
    620 	/* the PCI registers are sometimes not loaded correctly. */
    621 	/* Therefore, I will read the sub-system vendor ID from the EEPROM */
    622 	/* (just like the Bt878 does during power up initialisation) */
    623 
    624         if ((bktr->id==BROOKTREE_878) || (bktr->id==BROOKTREE_879)) {
    625 	    /* Try and locate the EEPROM */
    626 	    eeprom_i2c_address = locate_eeprom_address(bktr);
    627 	    if (eeprom_i2c_address != -1) {
    628 
    629                 unsigned int subsystem_vendor_id; /* vendors PCI-SIG ID */
    630                 unsigned int subsystem_id;        /* board model number */
    631 		unsigned int byte_252, byte_253, byte_254, byte_255;
    632 
    633 		bktr->card = cards[(card = CARD_UNKNOWN)];
    634 		bktr->card.eepromAddr = eeprom_i2c_address;
    635 		bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    636 
    637 	        if(readEEProm(bktr, 0, 256, (u_char *) &eeprom))
    638 		    printf("%s: error reading EEPROM\n", bktr_name(bktr));
    639 
    640                 byte_252 = (unsigned int)eeprom[252];
    641                 byte_253 = (unsigned int)eeprom[253];
    642                 byte_254 = (unsigned int)eeprom[254];
    643                 byte_255 = (unsigned int)eeprom[255];
    644 
    645                 subsystem_id        = (byte_252 << 8) | byte_253;
    646                 subsystem_vendor_id = (byte_254 << 8) | byte_255;
    647 
    648 	        if (bootverbose)
    649 	            printf("%s: subsystem 0x%04x 0x%04x\n", bktr_name(bktr),
    650 			   subsystem_vendor_id, subsystem_id);
    651 
    652                 if (subsystem_vendor_id == PCI_VENDOR_AVERMEDIA) {
    653                     bktr->card = cards[(card = CARD_AVER_MEDIA)];
    654 		    bktr->card.eepromAddr = eeprom_i2c_address;
    655 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    656                     goto checkTuner;
    657                 }
    658 
    659                 if (subsystem_vendor_id == PCI_VENDOR_HAUPPAUGE) {
    660                     bktr->card = cards[(card = CARD_HAUPPAUGE)];
    661 		    bktr->card.eepromAddr = eeprom_i2c_address;
    662 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    663                     goto checkTuner;
    664                 }
    665 
    666                 if ((subsystem_vendor_id == PCI_VENDOR_FLYVIDEO)
    667                  || (subsystem_vendor_id == PCI_VENDOR_FLYVIDEO_2)) {
    668                     bktr->card = cards[(card = CARD_FLYVIDEO)];
    669 		    bktr->card.eepromAddr = eeprom_i2c_address;
    670 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    671                     goto checkTuner;
    672                 }
    673 
    674                 if (subsystem_vendor_id == PCI_VENDOR_STB) {
    675                     bktr->card = cards[(card = CARD_STB)];
    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_ASKEY) {
    682                     bktr->card = cards[(card = CARD_ASKEY_DYNALINK_MAGIC_TVIEW)];
    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_LEADTEK)
    689                  || (subsystem_vendor_id == PCI_VENDOR_LEADTEK_ALT)
    690                  || (subsystem_vendor_id == PCI_VENDOR_LEADTEK_ALT_2)
    691                  || (subsystem_vendor_id == PCI_VENDOR_LEADTEK_ALT_3)) {
    692                     bktr->card = cards[(card = CARD_LEADTEK)];
    693 		    bktr->card.eepromAddr = eeprom_i2c_address;
    694 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    695                     goto checkTuner;
    696                 }
    697 
    698 		if (subsystem_vendor_id == PCI_VENDOR_PINNACLE_ALT) {
    699                     bktr->card = cards[(card = CARD_MIRO)];
    700 		    bktr->card.eepromAddr = eeprom_i2c_address;
    701 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    702                     goto checkTuner;
    703                 }
    704 
    705     	    	if (subsystem_vendor_id == PCI_VENDOR_TERRATEC
    706 		    && (subsystem_id == MODEL_TERRATVALUE_1118
    707 		        || subsystem_id == MODEL_TERRATVALUE_1134)) {
    708     	    	    bktr->card = cards[(card = CARD_TERRATVALUE)];
    709     	    	    bktr->card.eepromAddr = eeprom_i2c_address;
    710     	    	    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    711     	    	    goto checkTuner;
    712     	    	}
    713 
    714     	    	if (subsystem_vendor_id == PCI_VENDOR_TERRATEC) {
    715     	    	    bktr->card = cards[(card = CARD_TERRATVPLUS)];
    716     	    	    bktr->card.eepromAddr = eeprom_i2c_address;
    717     	    	    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    718     	    	    goto checkTuner;
    719     	    	}
    720 
    721                 /* Vendor is unknown. We will use the standard probe code */
    722 		/* which may not give best results */
    723                 printf("%s: Warning - card vendor 0x%04x (model 0x%04x) unknown.\n",
    724 		       bktr_name(bktr), subsystem_vendor_id, subsystem_id);
    725             }
    726 	    else
    727 	    {
    728                 printf("%s: Card has no configuration EEPROM. Cannot determine card make.\n",
    729 		       bktr_name(bktr));
    730 	    }
    731 	} /* end of bt878/bt879 card detection code */
    732 
    733 	/* If we get to this point, we must have a Bt848/848A/849A card */
    734 	/* or a Bt878/879 with an unknown subsystem vendor id */
    735         /* Try and determine the make of card by clever i2c probing */
    736 
    737    	/* Check for i2c devices. If none, move on */
    738 	if (!any_i2c_devices) {
    739 		bktr->card = cards[(card = CARD_INTEL)];
    740 		bktr->card.eepromAddr = 0;
    741 		bktr->card.eepromSize = 0;
    742 		goto checkTuner;
    743 	}
    744 
    745         /* Look for Hauppauge, STB and Osprey cards by the presence */
    746 	/* of an EEPROM */
    747         /* Note: Bt878 based cards also use EEPROMs so we can only do this */
    748         /* test on BT848/848A and 849A based cards. */
    749 	if ((bktr->id==BROOKTREE_848)  ||
    750 	    (bktr->id==BROOKTREE_848A) ||
    751 	    (bktr->id==BROOKTREE_849A)) {
    752 
    753             /* At i2c address 0xa0, look for Hauppauge and Osprey cards */
    754             if ((status = i2cRead(bktr, PFC8582_RADDR)) != ABSENT) {
    755 
    756 		    /* Read the eeprom contents */
    757 		    bktr->card = cards[(card = CARD_UNKNOWN)];
    758 		    bktr->card.eepromAddr = PFC8582_WADDR;
    759 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    760 	            readEEProm(bktr, 0, 128, (u_char *) &eeprom);
    761 
    762 		    /* For Hauppauge, check the EEPROM begins with 0x84 */
    763 		    if (eeprom[0] == 0x84) {
    764                             bktr->card = cards[(card = CARD_HAUPPAUGE)];
    765 			    bktr->card.eepromAddr = PFC8582_WADDR;
    766 			    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    767                             goto checkTuner;
    768 		    }
    769 
    770 		    /* For Osprey, check the EEPROM begins with "MMAC" */
    771 		    if ((eeprom[0] == 'M') &&(eeprom[1] == 'M')
    772 			&&(eeprom[2] == 'A') &&(eeprom[3] == 'C')) {
    773                             bktr->card = cards[(card = CARD_OSPREY)];
    774 			    bktr->card.eepromAddr = PFC8582_WADDR;
    775 			    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    776                             goto checkTuner;
    777 		    }
    778 		    printf("%s: Warning: Unknown card type. EEPROM data not recognised\n",
    779 			   bktr_name(bktr));
    780 		    printf("%s: %x %x %x %x\n", bktr_name(bktr),
    781 			   eeprom[0],eeprom[1],eeprom[2],eeprom[3]);
    782             }
    783 
    784             /* look for an STB card */
    785             if ((status = i2cRead(bktr, X24C01_RADDR)) != ABSENT) {
    786                     bktr->card = cards[(card = CARD_STB)];
    787 		    bktr->card.eepromAddr = X24C01_WADDR;
    788 		    bktr->card.eepromSize = (u_char)(128 / EEPROMBLOCKSIZE);
    789                     goto checkTuner;
    790             }
    791 
    792 	}
    793 
    794 	signCard(bktr, 1, 128, (u_char *)  &probe_signature);
    795 
    796 	if (bootverbose) {
    797 	  printf("%s: card signature: ", bktr_name(bktr));
    798 	  for (j = 0; j < Bt848_MAX_SIGN; j++) {
    799 	    printf(" %02x ", probe_signature[j]);
    800 	  }
    801 	  printf("\n\n");
    802 	}
    803 	for (i = 0;
    804 	     i < (sizeof bt848_card_signature)/ sizeof (struct bt848_card_sig);
    805 	     i++) {
    806 
    807 	  card_found = 1;
    808 	  probe_temp = (u_char *) &bt848_card_signature[i].signature;
    809 
    810 	  for (j = 0; j < Bt848_MAX_SIGN; j++) {
    811 	    if ((probe_temp[j] & 0xf) != (probe_signature[j] & 0xf)) {
    812 	      card_found = 0;
    813 	      break;
    814 	    }
    815 
    816 	  }
    817 	  if (card_found) {
    818 	    bktr->card = cards[card = bt848_card_signature[i].card];
    819 	    eeprom_i2c_address = locate_eeprom_address(bktr);
    820 	    if (eeprom_i2c_address != -1) {
    821 		bktr->card.eepromAddr = eeprom_i2c_address;
    822 		bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    823 	    } else {
    824 		bktr->card.eepromAddr = 0;
    825 		bktr->card.eepromSize = 0;
    826 	    }
    827 	    tuner_i2c_address = locate_tuner_address(bktr);
    828 	    select_tuner(bktr, bt848_card_signature[i].tuner);
    829 	    goto checkDBX;
    830 	  }
    831 	}
    832 
    833 	/* We do not know the card type. Default to Miro */
    834 	bktr->card = cards[(card = CARD_MIRO)];
    835 
    836 
    837 checkEEPROM:
    838 	/* look for a configuration eeprom */
    839 	eeprom_i2c_address = locate_eeprom_address(bktr);
    840 	if (eeprom_i2c_address != -1) {
    841 	    bktr->card.eepromAddr = eeprom_i2c_address;
    842 	    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
    843 	} else {
    844 	    bktr->card.eepromAddr = 0;
    845 	    bktr->card.eepromSize = 0;
    846 	}
    847 
    848 
    849 checkTuner:
    850 
    851 	/* look for a tuner */
    852 	tuner_i2c_address = locate_tuner_address(bktr);
    853 	if (tuner_i2c_address == -1) {
    854 		select_tuner(bktr, NO_TUNER);
    855 		goto checkDBX;
    856 	}
    857 
    858 #if defined(BKTR_OVERRIDE_TUNER)
    859 	select_tuner(bktr, BKTR_OVERRIDE_TUNER);
    860 	goto checkDBX;
    861 #endif
    862 	if (bktr->bt848_tuner != -1) {
    863 	  select_tuner(bktr, bktr->bt848_tuner & 0xff);
    864 	  goto checkDBX;
    865 	}
    866 
    867 	/* Check for i2c devices */
    868 	if (!any_i2c_devices) {
    869 		select_tuner(bktr, NO_TUNER);
    870 		goto checkDBX;
    871 	}
    872 
    873 	/* differentiate type of tuner */
    874 
    875 	switch (card) {
    876 	case CARD_MIRO:
    877 	    switch (((INL(bktr, BKTR_GPIO_DATA) >> 10)-1)&7) {
    878 	    case 0: select_tuner(bktr, TEMIC_PAL); break;
    879 	    case 1: select_tuner(bktr, PHILIPS_PAL); break;
    880 	    case 2: select_tuner(bktr, PHILIPS_NTSC); break;
    881 	    case 3: select_tuner(bktr, PHILIPS_SECAM); break;
    882 	    case 4: select_tuner(bktr, NO_TUNER); break;
    883 	    case 5: select_tuner(bktr, PHILIPS_PALI); break;
    884 	    case 6: select_tuner(bktr, TEMIC_NTSC); break;
    885 	    case 7: select_tuner(bktr, TEMIC_PALI); break;
    886 	    }
    887 	    goto checkDBX;
    888 	    break;
    889 
    890 	case CARD_HAUPPAUGE:
    891 	    /* Hauppauge kindly supplied the following Tuner Table */
    892 	    /* FIXME: I think the tuners the driver selects for types */
    893 	    /* 0x08 and 0x15 may be incorrect but no one has complained. */
    894 	    /* Old Temic tuners had their own API, but newer Temic tuners */
    895 	    /* have the same API as Philips tuners */
    896 	    /*
    897   ID  Tuner Model           Format			We select Format
    898  0x00 NONE
    899  0x01 EXTERNAL
    900  0x02 OTHER
    901  0x03 Philips FI1216        BG
    902  0x04 Philips FI1216MF      BGLL'			PHILIPS_SECAM
    903  0x05 Philips FI1236        MN 				PHILIPS_NTSC
    904  0x06 Philips FI1246        I 				PHILIPS_PALI
    905  0x07 Philips FI1256        DK
    906  0x08 Philips FI1216 MK2    BG 				PHILIPS_PALI
    907  0x09 Philips FI1216MF MK2  BGLL' 			PHILIPS_SECAM
    908  0x0a Philips FI1236 MK2    MN 				PHILIPS_NTSC
    909  0x0b Philips FI1246 MK2    I 				PHILIPS_PALI
    910  0x0c Philips FI1256 MK2    DK
    911  0x0d Temic 4032FY5         NTSC			TEMIC_NTSC
    912  0x0e Temic 4002FH5         BG				TEMIC_PAL
    913  0x0f Temic 4062FY5         I 				TEMIC_PALI
    914  0x10 Philips FR1216 MK2    BG
    915  0x11 Philips FR1216MF MK2  BGLL' 			PHILIPS_FR1236_SECAM
    916  0x12 Philips FR1236 MK2    MN 				PHILIPS_FR1236_NTSC
    917  0x13 Philips FR1246 MK2    I
    918  0x14 Philips FR1256 MK2    DK
    919  0x15 Philips FM1216        BG 				PHILIPS_FR1216_PAL
    920  0x16 Philips FM1216MF      BGLL' 			PHILIPS_FR1236_SECAM
    921  0x17 Philips FM1236        MN 				PHILIPS_FR1236_NTSC
    922  0x18 Philips FM1246        I
    923  0x19 Philips FM1256        DK
    924  0x1a Temic 4036FY5         MN (FI1236 MK2 clone)	PHILIPS_NTSC
    925  0x1b Samsung TCPN9082D     MN
    926  0x1c Samsung TCPM9092P     Pal BG/I/DK
    927  0x1d Temic 4006FH5         BG				PHILIPS_PALI
    928  0x1e Samsung TCPN9085D     MN/Radio
    929  0x1f Samsung TCPB9085P     Pal BG/I/DK / Radio
    930  0x20 Samsung TCPL9091P     Pal BG & Secam L/L'
    931  0x21 Temic 4039FY5         NTSC Radio
    932  0x22 Philips FQ1216ME      Pal BGIDK & Secam L/L'
    933  0x23 Temic 4066FY5         Pal I (FI1246 MK2 clone)	PHILIPS_PALI
    934  0x24 Philips TD1536        MN/ATSCDigital
    935  0x25 Philips TD1536D       MN/ATSCDigital DUAL INPUT
    936  0x26 Philips FMR1236       M/N FM(no demod)
    937  0x27 Philips FI1256MP      B/G, D/K
    938  0x28 Samsung TCPQ9091P     BG/I/DK, L/L'
    939  0x29 Temic 4006FN5         BG/I/DK
    940  0x2a Temic 4009FR5         BG FM			PHILIPS_FR1216_PAL
    941  0x2b Temic 4046FM5         B/G, I, D/K, L/L'
    942  0x2c Temic 4009FN5         B/G, I, D/K, FM (no demod)
    943  0x2d Philips TD1536D_FH_44 MN/ATSCDigital DUAL INPUT
    944 	    */
    945 
    946 
    947 	    /* Determine the model number from the eeprom */
    948 	    if (bktr->card.eepromAddr != 0) {
    949 	        /* eeprom data block structure */
    950 	        unsigned char *block_1, *block_2, *block_3;
    951 	        int block_1_data_size,  block_2_data_size, block_3_data_size;
    952 	        int block_1_total_size, block_2_total_size, block_3_total_size;
    953 
    954 		unsigned int model,revision;
    955 		unsigned char tuner_code;
    956 		unsigned char no_audio_mux;
    957 
    958 		readEEProm(bktr, 0, 128, (u_char *) &eeprom);
    959 
    960 	        /* LOCATE THE EEPROM DATA BLOCKS */
    961 	        block_1 = &eeprom[0];
    962 	        block_1_data_size = (block_1[2] << 8 | block_1[1]);
    963 	        block_1_total_size = block_1_data_size + 3; /* Header bytes */
    964 
    965 	        block_2 = &eeprom[block_1_total_size];
    966 	        block_2_data_size = (block_2[2] << 8 | block_2[1]);
    967 	        block_2_total_size = block_2_data_size + 3; /* Header bytes */
    968 
    969 	        block_3 = &eeprom[block_1_total_size + block_2_total_size];
    970 	        block_3_data_size = (block_3[0] &0x07);
    971 	        block_3_total_size = block_3_data_size + 1; /* Header size */
    972 
    973 		model    = (block_1[12] << 8  | block_1[11]);
    974 		revision = (block_1[15] << 16 | block_1[14] << 8 | block_1[13]);
    975 
    976 		tuner_code = block_1[9];
    977 
    978 		no_audio_mux = ((block_3[3] >> 7) &0x01);
    979 
    980 		if (no_audio_mux) bktr->audio_mux_present = 0;
    981 
    982 		if (verbose)
    983 		    printf("%s: Hauppauge Model %d %c%c%c%c\n",
    984 			   bktr_name(bktr),
    985 			   model,
    986 			   ((revision >> 18) & 0x3f) + 32,
    987 			   ((revision >> 12) & 0x3f) + 32,
    988 			   ((revision >>  6) & 0x3f) + 32,
    989 			   ((revision >>  0) & 0x3f) + 32);
    990 
    991 	        /* Determine the tuner type from the eeprom */
    992 
    993 		switch (tuner_code) {
    994 
    995 		  case 0x5:
    996                   case 0x0a:
    997                   case 0x1a:
    998 		    select_tuner(bktr, PHILIPS_NTSC);
    999 		    goto checkDBX;
   1000 
   1001 		  case 0x4:
   1002                   case 0x9:
   1003 		    select_tuner(bktr, PHILIPS_SECAM);
   1004 		    goto checkDBX;
   1005 
   1006                   case 0x11:
   1007 	          case 0x16:
   1008 		    select_tuner(bktr, PHILIPS_FR1236_SECAM);
   1009 		    goto checkDBX;
   1010 
   1011                   case 0x12:
   1012 	          case 0x17:
   1013 	          case 0x21: /* Hauppauge WinTV-GO-FM (model 00191) */
   1014 		    select_tuner(bktr, PHILIPS_FR1236_NTSC);
   1015 		    goto checkDBX;
   1016 
   1017 		  case 0x6:
   1018 	          case 0x8:
   1019 	          case 0xb:
   1020 	          case 0x1d:
   1021 	          case 0x23:
   1022 		    select_tuner(bktr, PHILIPS_PALI);
   1023 		    goto checkDBX;
   1024 
   1025 	          case 0xd:
   1026 		    select_tuner(bktr, TEMIC_NTSC);
   1027 		    goto checkDBX;
   1028 
   1029                   case 0xe:
   1030 		    select_tuner(bktr, TEMIC_PAL);
   1031 		    goto checkDBX;
   1032 
   1033 	          case 0xf:
   1034 		    select_tuner(bktr, TEMIC_PALI);
   1035 		    goto checkDBX;
   1036 
   1037                   case 0x15:
   1038 		    select_tuner(bktr, PHILIPS_FR1216_PAL);
   1039 		    goto checkDBX;
   1040 
   1041                   case 0x2a:
   1042 		    bktr->msp_use_mono_source = 1;
   1043 		    select_tuner(bktr, PHILIPS_FR1216_PAL);
   1044 		    goto checkDBX;
   1045 
   1046 	          default :
   1047 		    printf("%s: Warning - Unknown Hauppauge Tuner 0x%x\n",
   1048 			   bktr_name(bktr), tuner_code);
   1049 		}
   1050 	    }
   1051 	    break;
   1052 
   1053 
   1054 	case CARD_AVER_MEDIA:
   1055 	    /* AVerMedia kindly supplied some details of their EEPROM contents
   1056 	     * which allow us to auto select the Tuner Type.
   1057 	     * Only the newer AVerMedia cards actually have an EEPROM.
   1058 	     */
   1059 	    if (bktr->card.eepromAddr != 0) {
   1060 
   1061 		u_char tuner_make;   /* Eg Philips, Temic */
   1062 		u_char tuner_tv_fm;  /* TV or TV with FM Radio */
   1063 		u_char tuner_format; /* Eg NTSC, PAL, SECAM */
   1064 		int    tuner;
   1065 
   1066 		int tuner_0_table[] = {
   1067 			PHILIPS_NTSC,  PHILIPS_PAL,
   1068 			PHILIPS_PAL,   PHILIPS_PAL,
   1069 			PHILIPS_PAL,   PHILIPS_PAL,
   1070 			PHILIPS_SECAM, PHILIPS_SECAM,
   1071 			PHILIPS_SECAM, PHILIPS_PAL};
   1072 
   1073 		int tuner_0_fm_table[] = {
   1074 			PHILIPS_FR1236_NTSC,  PHILIPS_FR1216_PAL,
   1075 			PHILIPS_FR1216_PAL,   PHILIPS_FR1216_PAL,
   1076 			PHILIPS_FR1216_PAL,   PHILIPS_FR1216_PAL,
   1077 			PHILIPS_FR1236_SECAM, PHILIPS_FR1236_SECAM,
   1078 			PHILIPS_FR1236_SECAM, PHILIPS_FR1216_PAL};
   1079 
   1080 		int tuner_1_table[] = {
   1081 			TEMIC_NTSC,  TEMIC_PAL,   TEMIC_PAL,
   1082 			TEMIC_PAL,   TEMIC_PAL,   TEMIC_PAL,
   1083 			TEMIC_SECAM, TEMIC_SECAM, TEMIC_SECAM,
   1084 			TEMIC_PAL};
   1085 
   1086 
   1087 		/* Extract information from the EEPROM data */
   1088 	    	readEEProm(bktr, 0, 128, (u_char *) &eeprom);
   1089 
   1090 		tuner_make   = (eeprom[0x41] & 0x7);
   1091 		tuner_tv_fm  = (eeprom[0x41] & 0x18) >> 3;
   1092 		tuner_format = (eeprom[0x42] & 0xf0) >> 4;
   1093 
   1094 		/* Treat tuner make 0 (Philips) and make 2 (LG) the same */
   1095 		if (((tuner_make == 0) || (tuner_make == 2))
   1096 		    && (tuner_format <= 9) && (tuner_tv_fm == 0)) {
   1097 			tuner = tuner_0_table[tuner_format];
   1098 			select_tuner(bktr, tuner);
   1099 			goto checkDBX;
   1100 		}
   1101 
   1102 		if (((tuner_make == 0) || (tuner_make == 2))
   1103 		    && (tuner_format <= 9) && (tuner_tv_fm == 1)) {
   1104 			tuner = tuner_0_fm_table[tuner_format];
   1105 			select_tuner(bktr, tuner);
   1106 			goto checkDBX;
   1107 		}
   1108 
   1109 		if ((tuner_make == 1) && (tuner_format <= 9)) {
   1110 			tuner = tuner_1_table[tuner_format];
   1111 			select_tuner(bktr, tuner);
   1112 			goto checkDBX;
   1113 		}
   1114 
   1115 	    	printf("%s: Warning - Unknown AVerMedia Tuner Make %d Format %d\n",
   1116 			bktr_name(bktr), tuner_make, tuner_format);
   1117 	    }
   1118 	    break;
   1119 
   1120 	case CARD_LEADTEK:
   1121 #if BKTR_SYSTEM_DEFAULT == BROOKTREE_PAL
   1122 	    select_tuner(bktr, PHILIPS_FR1216_PAL);
   1123 #else
   1124 	    select_tuner(bktr, PHILIPS_FR1236_NTSC);
   1125 #endif
   1126             goto checkDBX;
   1127 	    break;
   1128 
   1129 	case CARD_TERRATVALUE:
   1130             select_tuner(bktr, PHILIPS_PAL);
   1131             goto checkDBX;
   1132             break;
   1133 
   1134 	} /* end switch(card) */
   1135 
   1136 
   1137         /* At this point, a goto checkDBX has not occurred */
   1138         /* We have not been able to select a Tuner */
   1139         /* Some cards make use of the tuner address to */
   1140         /* identify the make/model of tuner */
   1141 
   1142         /* At address 0xc0/0xc1 we often find a TEMIC NTSC */
   1143         if (i2cRead(bktr, 0xc1) != ABSENT) {
   1144 	    select_tuner(bktr, TEMIC_NTSC);
   1145             goto checkDBX;
   1146         }
   1147 
   1148         /* At address 0xc6/0xc7 we often find a PHILIPS NTSC Tuner */
   1149         if (i2cRead(bktr, 0xc7) != ABSENT) {
   1150 	    select_tuner(bktr, PHILIPS_NTSC);
   1151             goto checkDBX;
   1152         }
   1153 
   1154         /* Address 0xc2/0xc3 is default (or common address) for several */
   1155 	/* tuners and we cannot tell which is which. */
   1156 	/* And for all other tuner i2c addresses, select the default */
   1157 	select_tuner(bktr, DEFAULT_TUNER);
   1158 
   1159 
   1160 checkDBX:
   1161 #if defined(BKTR_OVERRIDE_DBX)
   1162 	bktr->card.dbx = BKTR_OVERRIDE_DBX;
   1163 	goto checkMSP;
   1164 #endif
   1165    /* Check for i2c devices */
   1166 	if (!any_i2c_devices) {
   1167 		goto checkMSP;
   1168 	}
   1169 
   1170 	/* probe for BTSC (dbx) chip */
   1171 	if (i2cRead(bktr, TDA9850_RADDR) != ABSENT)
   1172 		bktr->card.dbx = 1;
   1173 
   1174 checkMSP:
   1175 	/* If this is a Hauppauge Bt878 card, we need to enable the
   1176 	 * MSP 34xx audio chip.
   1177 	 * If this is a Hauppauge Bt848 card, reset the MSP device.
   1178 	 * The MSP reset line is wired to GPIO pin 5. On Bt878 cards a pulldown
   1179 	 * resistor holds the device in reset until we set GPIO pin 5.
   1180          */
   1181 
   1182 	/* Optionally skip the MSP reset. This is handy if you initialise the
   1183 	 * MSP audio in another operating system (eg Windows) first and then
   1184 	 * do a soft reboot.
   1185 	 */
   1186 
   1187 #ifndef BKTR_NO_MSP_RESET
   1188 	if (card == CARD_HAUPPAUGE) {
   1189             OUTL(bktr, BKTR_GPIO_OUT_EN, INL(bktr, BKTR_GPIO_OUT_EN) | (1<<5));
   1190             OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
   1191             DELAY(2500); /* wait 2.5ms */
   1192             OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) & ~(1<<5)); /* write '0' */
   1193             DELAY(2500); /* wait 2.5ms */
   1194             OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
   1195             DELAY(2500); /* wait 2.5ms */
   1196         }
   1197 #endif
   1198 
   1199 #if defined(BKTR_OVERRIDE_MSP)
   1200 	bktr->card.msp3400c = BKTR_OVERRIDE_MSP;
   1201 	goto checkMSPEnd;
   1202 #endif
   1203 
   1204 	/* Check for i2c devices */
   1205 	if (!any_i2c_devices) {
   1206 		goto checkMSPEnd;
   1207 	}
   1208 
   1209 	if (i2cRead(bktr, MSP3400C_RADDR) != ABSENT) {
   1210 		bktr->card.msp3400c = 1;
   1211 	}
   1212 
   1213 checkMSPEnd:
   1214 
   1215 	if (bktr->card.msp3400c) {
   1216 		bktr->msp_addr = MSP3400C_WADDR;
   1217 		msp_read_id(bktr);
   1218 		printf("%s: Detected a MSP%s at 0x%x\n", bktr_name(bktr),
   1219 		       bktr->msp_version_string,
   1220 		       bktr->msp_addr);
   1221 
   1222 	}
   1223 
   1224 /* Check for Dolby Surround Sound DPL3518A sound chip */
   1225 	if (i2cRead(bktr, DPL3518A_RADDR) != ABSENT) {
   1226 		bktr->card.dpl3518a = 1;
   1227 	}
   1228 
   1229 	if (bktr->card.dpl3518a) {
   1230 		bktr->dpl_addr = DPL3518A_WADDR;
   1231 		dpl_read_id(bktr);
   1232 		printf("%s: Detected a DPL%s at 0x%x\n", bktr_name(bktr),
   1233 		       bktr->dpl_version_string,
   1234 		       bktr->dpl_addr);
   1235 	}
   1236 
   1237 /* Start of Check Remote */
   1238         /* Check for the Hauppauge IR Remote Control */
   1239         /* If there is an external unit, the internal will be ignored */
   1240 
   1241         bktr->remote_control = 0; /* initial value */
   1242 
   1243         if (any_i2c_devices) {
   1244             if (i2cRead(bktr, HAUP_REMOTE_EXT_RADDR) != ABSENT)
   1245                 {
   1246                 bktr->remote_control      = 1;
   1247                 bktr->remote_control_addr = HAUP_REMOTE_EXT_RADDR;
   1248                 }
   1249             else if (i2cRead(bktr, HAUP_REMOTE_INT_RADDR) != ABSENT)
   1250                 {
   1251                 bktr->remote_control      = 1;
   1252                 bktr->remote_control_addr = HAUP_REMOTE_INT_RADDR;
   1253                 }
   1254 
   1255         }
   1256         /* If a remote control is found, poll it 5 times to turn off the LED */
   1257         if (bktr->remote_control) {
   1258                 int ii;
   1259                 for (ii=0; ii<5; ii++)
   1260                         i2cRead(bktr, bktr->remote_control_addr);
   1261         }
   1262 /* End of Check Remote */
   1263 
   1264 #if defined(BKTR_USE_PLL)
   1265 	bktr->xtal_pll_mode = BT848_USE_PLL;
   1266 	goto checkPLLEnd;
   1267 #endif
   1268 	/* Default is to use XTALS and not PLL mode */
   1269 	bktr->xtal_pll_mode = BT848_USE_XTALS;
   1270 
   1271 	/* Enable PLL mode for OSPREY users */
   1272 	if (card == CARD_OSPREY)
   1273 		bktr->xtal_pll_mode = BT848_USE_PLL;
   1274 
   1275 	/* Enable PLL mode for Video Highway Xtreme users */
   1276 	if (card == CARD_VIDEO_HIGHWAY_XTREME)
   1277 		bktr->xtal_pll_mode = BT848_USE_PLL;
   1278 
   1279 
   1280 	/* Most (perhaps all) Bt878 cards need to be switched to PLL mode */
   1281 	/* as they only fit the NTSC crystal to their cards */
   1282 	/* Default to enabling PLL mode for all Bt878/879 cards */
   1283 
   1284 	if ((bktr->id==BROOKTREE_878 || bktr->id==BROOKTREE_879))
   1285 		bktr->xtal_pll_mode = BT848_USE_PLL;
   1286 
   1287 
   1288 #if defined(BKTR_USE_PLL)
   1289 checkPLLEnd:
   1290 #endif
   1291 
   1292 
   1293 	bktr->card.tuner_pllAddr = tuner_i2c_address;
   1294 
   1295 	if (verbose) {
   1296 		printf("%s: %s", bktr_name(bktr), bktr->card.name);
   1297 		if (bktr->card.tuner)
   1298 			printf(", %s tuner", bktr->card.tuner->name);
   1299 		if (bktr->card.dbx)
   1300 			printf(", dbx stereo");
   1301 		if (bktr->card.msp3400c)
   1302 			printf(", msp3400c stereo");
   1303 		if (bktr->card.dpl3518a)
   1304 			printf(", dpl3518a dolby");
   1305                 if (bktr->remote_control)
   1306                         printf(", remote control");
   1307 		printf(".\n");
   1308 	}
   1309 }
   1310 
   1311 #undef ABSENT
   1312