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