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