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