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