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