summaryrefslogtreecommitdiff
path: root/sys/dev/pci/bktr/bktr_card.c
blob: 586f02c5edfd9dc94a0a6ebc7c1936d9a454c581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
/*	$OpenBSD: bktr_card.c,v 1.16 2022/02/21 12:18:54 jsg Exp $	*/
/* $FreeBSD: src/sys/dev/bktr/bktr_card.c,v 1.16 2000/10/31 13:09:56 roger Exp $ */

/*
 * This is part of the Driver for Video Capture Cards (Frame grabbers)
 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
 * chipset.
 * Copyright Roger Hardiman and Amancio Hasty.
 *
 * bktr_card : This deals with identifying TV cards.
 *		trying to find the card make and model of card.
 *		trying to find the type of tuner fitted.
 *		reading the configuration EEPROM.
 *		locating i2c devices.
 *
 */

/*
 * 1. Redistributions of source code must retain the
 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Amancio Hasty and
 *      Roger Hardiman
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/vnode.h>

#include <dev/ic/bt8xx.h>	/* OpenBSD location for .h files */
#include <dev/pci/bktr/bktr_reg.h>
#include <dev/pci/bktr/bktr_core.h>
#include <dev/pci/bktr/bktr_tuner.h>
#include <dev/pci/bktr/bktr_card.h>
#include <dev/pci/bktr/bktr_audio.h>

/* Include the PCI Vendor definitions */
#include <dev/pci/pcidevs.h>
#include <dev/pci/pcireg.h>

/* Various defines */
#define HAUP_REMOTE_INT_WADDR   0x30
#define HAUP_REMOTE_INT_RADDR   0x31

#define HAUP_REMOTE_EXT_WADDR   0x34
#define HAUP_REMOTE_EXT_RADDR   0x35

/* address of BTSC/SAP decoder chip */
#define TDA9850_WADDR		0xb6
#define TDA9850_RADDR		0xb7

/* address of MSP3400C chip */
#define MSP3400C_WADDR		0x80
#define MSP3400C_RADDR		0x81

/* address of DPL3518A chip */
#define DPL3518A_WADDR		0x84
#define DPL3518A_RADDR		0x85

/* EEProm (128 * 8) on an STB card */
#define X24C01_WADDR		0xae
#define X24C01_RADDR		0xaf

/* EEProm (256 * 8) on a Hauppauge card */
/* and on most BT878s cards to store the sub-system vendor id */
#define PFC8582_WADDR		0xa0
#define PFC8582_RADDR		0xa1

#if BKTR_SYSTEM_DEFAULT == BROOKTREE_PAL
#define DEFAULT_TUNER   PHILIPS_PALI
#else
#define DEFAULT_TUNER   PHILIPS_NTSC
#endif




/*
 * the data for each type of card
 *
 * Note:
 *   these entries MUST be kept in the order defined by the CARD_XXX defines!
 */
static const struct CARDTYPE cards[] = {

	{  CARD_UNKNOWN,			/* the card id */
	  "Unknown",				/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx unknown */
	   0,
	   0,
	   0,					/* EEProm unknown */
	   0,					/* EEProm unknown */
	   { 0, 0, 0, 0, 0 },
	   0 },					/* GPIO mask */

	{  CARD_MIRO,				/* the card id */
	  "Pinnacle/Miro TV",			/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx unknown */
	   0,
	   0,
	   0,					/* EEProm unknown */
	   0,					/* size unknown */
	   { 0x02, 0x01, 0x00, 0x0a, 1 },	/* audio MUX values */
	   0x0f },				/* GPIO mask */

	{  CARD_HAUPPAUGE,			/* the card id */
	  "Hauppauge WinCast/TV",		/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,
	   0,
	   PFC8582_WADDR,			/* EEProm type */
	   (u_char)(256 / EEPROMBLOCKSIZE),	/* 256 bytes */
	   { 0x00, 0x02, 0x01, 0x04, 1 },	/* audio MUX values */
	   0x0f },				/* GPIO mask */

	{  CARD_STB,				/* the card id */
	  "STB TV/PCI",				/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,
	   0,
	   X24C01_WADDR,			/* EEProm type */
	   (u_char)(128 / EEPROMBLOCKSIZE),	/* 128 bytes */
	   { 0x00, 0x01, 0x02, 0x02, 1 }, 	/* audio MUX values */
	   0x0f },				/* GPIO mask */

	{  CARD_INTEL,				/* the card id */
	  "Intel Smart Video III/VideoLogic Captivator PCI", /* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,
	   0,
	   0,
	   0,
	   0,
	   { 0, 0, 0, 0, 0 }, 			/* audio MUX values */
	   0x00 },				/* GPIO mask */

	{  CARD_IMS_TURBO,			/* the card id */
	  "IMS TV Turbo",			/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,
	   0,
	   PFC8582_WADDR,			/* EEProm type */
	   (u_char)(256 / EEPROMBLOCKSIZE),	/* 256 bytes */
	   { 0x01, 0x02, 0x01, 0x00, 1 },	/* audio MUX values */
	   0x0f },				/* GPIO mask */

	{  CARD_AVER_MEDIA,			/* the card id */
	   "AVer Media TV/FM",			/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,
	   0,
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0x0c, 0x08, 0x04, 0x00, 1 },	/* audio MUX values */
	   0x1f },				/* GPIO mask */

	{  CARD_OSPREY,				/* the card id */
	   "MMAC Osprey",			/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,
	   0,
	   PFC8582_WADDR,			/* EEProm type */
	   (u_char)(256 / EEPROMBLOCKSIZE),	/* 256 bytes */
	   { 0x00, 0x00, 0x00, 0x00, 0 },	/* audio MUX values */
	   0 },					/* GPIO mask */

	{  CARD_NEC_PK,				/* the card id */
	   "NEC PK-UG-X017",			/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,
	   0,
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0x01, 0x02, 0x01, 0x00, 1 },	/* audio MUX values */
	   0x0f },				/* GPIO mask */

	{  CARD_IO_GV,				/* the card id */
	   "I/O DATA GV-BCTV2/PCI",		/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,
	   0,
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0x00, 0x00, 0x00, 0x00, 1 },	/* Has special MUX handler */
	   0x0f },				/* GPIO mask */

	{  CARD_FLYVIDEO,			/* the card id */
	   "FlyVideo",				/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,					/* msp34xx is optional */
	   0,					/* dpl3518a is optional */
	   0xac,				/* EEProm type */
	   (u_char)(256 / EEPROMBLOCKSIZE),	/* 256 bytes */
	   { 0x000, 0x800, 0x400, 0x8dff00, 1 },/* audio MUX values */
	   0x8dff00 },				/* GPIO mask */

	{  CARD_ZOLTRIX,			/* the card id */
	  "Zoltrix",				/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,					/* msp34xx is optional */
	   0,					/* dpl3518a is optional */
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0x04, 0x01, 0x00, 0x0a, 1 },	/* audio MUX values */
	   0x0f },				/* GPIO mask */

	{  CARD_KISS,				/* the card id */
	  "KISS TV/FM PCI",			/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,					/* msp34xx is optional */
	   0,					/* dpl3518a is optional */
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0x0c, 0x00, 0x0b, 0x0b, 1 },	/* audio MUX values */
	   0x0f },				/* GPIO mask */

	{  CARD_VIDEO_HIGHWAY_XTREME,		/* the card id */
	  "Video Highway Xtreme",		/* the 'name' */
	  NULL,				/* the tuner */
	   0,
	   0,
	   0,
	   0,
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0x00, 0x02, 0x01, 0x04, 1 },	/* audio MUX values */
	   0x0f },				/* GPIO mask */

	{  CARD_ASKEY_DYNALINK_MAGIC_TVIEW,	/* the card id */
	  "Askey/Dynalink Magic TView",		/* the 'name' */
	   NULL,				/* the tuner */
	   0,
	   0,
	   0,
	   0,
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0x400, 0xE00, 0x400, 0xC00, 1 },	/* audio MUX values */
	   0xE00 },				/* GPIO mask */

	{  CARD_LEADTEK,			/* the card id */
	  "Leadtek WinFast TV 2000",		/* the 'name' */
	   NULL,				/* the tuner */
	   0,
	   0,
	   0,
	   0,
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   /* Tuner, Extern, Intern, Mute, Enabled */
	   { 0x621000, 0x621000, 0x621000, 0xE21000, 1 }, /* audio MUX values */
	   0xfff000 },				/* GPIO mask */

	{  CARD_TERRATVPLUS,			/* the card id */
	   "TerraTVplus",			/* the 'name' */
	   NULL,				/* the tuner */
	   0,
	   0,
	   0,
	   0,
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0x20000, 0x00000, 0x30000, 0x40000, 1 }, /* audio MUX values*/
	   0x70000 },				/* GPIO mask */

	{  CARD_TVWONDER,			/* the card id */
	   "ATI TV-Wonder/VE",			/* the 'name' */
	   NULL,				/* the tuner */
	   0,
	   0,
	   0,
	   0,
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0x1002, 0x1002, 0x3003, 0x3003, 0x3003 },	/* audio MUX values*/
	   0x300f },				/* GPIO mask */

	{  CARD_IO_BCTV3,			/* the card id */
	  "I/O DATA GV-BCTV3/PCI",		/* the 'name' */
	  NULL,					/* the tuner */
	  0,					/* the tuner i2c address */
	  0,					/* dbx is optional */
	  0,
	  0,
	  0,					/* EEProm type */
	  0,					/* EEProm size */
	   /* Tuner, Extern, Intern, Mute, Enabled */
	   { 0x10000, 0, 0x10000, 0, 1 },	/* audio MUX values */
	   0x10f00 },				/* GPIO mask */

	{  CARD_AOPEN_VA1000,			/* the card id */
	  "AOpen VA1000",			/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,
	   0,
	   0,					/* EEProm unknown */
	   0,					/* size unknown */
	   { 0x02, 0x00, 0x00, 0x00, 1 },	/* audio MUX values */
	   0x18e0 },				/* GPIO mask */

	{  CARD_ZOLTRIX_GENIE_FM,		/* the card id */
	   "Zoltrix Genie TV/FM",		/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,					/* msp34xx is optional */
	   0,					/* dpl3518a is optional */
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0xbc803f, 0xbcb03f, 0xbc903f, 0x0, 1 },	/* audio MUX values*/
	   0xbcf03f },				/* GPIO mask */

	{  CARD_LEADTEK_XP,			/* the card id */
	   "Leadtek WinFast TV 2000 XP",	/* the 'name' */
	   NULL,				/* the tuner */
	   0,					/* the tuner i2c address */
	   0,					/* dbx is optional */
	   0,					/* msp34xx is optional */
	   0,					/* dpl3518a is optional */
	   0,					/* EEProm type */
	   0,					/* EEProm size */
	   { 0x122000, 0x1000, 0x620000, 0x0, 1 },	/* audio MUX values*/
	   0xb3300 },				/* GPIO mask */
};

struct bt848_card_sig bt848_card_signature[1]= {
  /* IMS TURBO TV : card 5 */
    {  5,9, {00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 02, 00, 00, 00}}
};


/*
 * Write to the configuration EEPROM on the card.
 * This is dangerous and will mess up your card. Therefore it is not
 * implemented.
 */
int
writeEEProm( bktr_ptr_t bktr, int offset, int count, u_char *data )
{
	return( -1 );
}

/*
 * Read the contents of the configuration EEPROM on the card.
 * (This is not fitted to all makes of card. All Hauppauge cards have them
 * and so do newer Bt878 based cards.
 */
int
readEEProm(bktr_ptr_t bktr, int offset, int count, u_char *data)
{
	int	x;
	int	addr;
	int	max;
	int	byte;

	/* get the address of the EEProm */
	addr = (int)(bktr->card.eepromAddr & 0xff);
	if ( addr == 0 )
		return( -1 );

	max = (int)(bktr->card.eepromSize * EEPROMBLOCKSIZE);
	if ( (offset + count) > max )
		return( -1 );

	/* set the start address */
	if ( i2cWrite( bktr, addr, offset, -1 ) == -1 )
		return( -1 );

	/* the read cycle */
	for ( x = 0; x < count; ++x ) {
		if ( (byte = i2cRead( bktr, (addr | 1) )) == -1 )
			return( -1 );
		data[ x ] = byte;
	}

	return( 0 );
}


#define ABSENT		(-1)

/*
 * get a signature of the card
 * read all 128 possible i2c read addresses from 0x01 thru 0xff
 * build a bit array with a 1 bit for each i2c device that responds
 *
 * XXX FIXME: use offset & count args
 */
int
signCard( bktr_ptr_t bktr, int offset, int count, u_char *sig )
{
	int	x;

	for ( x = 0; x < 16; ++x )
		sig[ x ] = 0;

	for ( x = 0; x < count; ++x ) {
		if ( i2cRead( bktr, (2 * x) + 1 ) != ABSENT ) {
			sig[ x / 8 ] |= (1 << (x % 8) );
		}
	}

	return( 0 );
}


/*
 * check_for_i2c_devices.
 * Some BT848 cards have no tuner and no additional i2c devices
 * eg stereo decoder. These are used for video conferencing or capture from
 * a video camera. (eg VideoLogic Captivator PCI, Intel SmartCapture card).
 *
 * Determine if there are any i2c devices present. There are none present if
 *  a) reading from all 128 devices returns ABSENT (-1) for each one
 *     (eg VideoLogic Captivator PCI with BT848)
 *  b) reading from all 128 devices returns 0 for each one
 *     (eg VideoLogic Captivator PCI rev. 2F with BT848A)
 */
static int check_for_i2c_devices( bktr_ptr_t bktr ){
  int x, temp_read;
  int i2c_all_0 = 1;
  int i2c_all_absent = 1;
  for ( x = 0; x < 128; ++x ) {
    temp_read = i2cRead( bktr, (2 * x) + 1 );
    if (temp_read != 0)      i2c_all_0 = 0;
    if (temp_read != ABSENT) i2c_all_absent = 0;
  }

  if ((i2c_all_0) || (i2c_all_absent)) return 0;
  else return 1;
}


/*
 * Temic/Philips datasheets say tuners can be at i2c addresses 0xc0, 0xc2,
 * 0xc4 or 0xc6, settable by links on the tuner.
 * Determine the actual address used on the TV card by probing read addresses.
 */
static int locate_tuner_address( bktr_ptr_t bktr) {
  if (i2cRead( bktr, 0xc1) != ABSENT) return 0xc0;
  if (i2cRead( bktr, 0xc3) != ABSENT) return 0xc2;
  if (i2cRead( bktr, 0xc5) != ABSENT) return 0xc4;
  if (i2cRead( bktr, 0xc7) != ABSENT) return 0xc6;
  return -1; /* no tuner found */
}


/*
 * Search for a configuration EEPROM on the i2c bus by looking at i2c addresses
 * where EEPROMs are usually found.
 * On some cards, the EEPROM appears in several locations, but all in the
 * range 0xa0 to 0xae.
 */
static int locate_eeprom_address( bktr_ptr_t bktr) {
  if (i2cRead(bktr, 0xa0) != ABSENT) return 0xa0;
  if (i2cRead(bktr, 0xac) != ABSENT) return 0xac;
  if (i2cRead(bktr, 0xae) != ABSENT) return 0xae;
  return -1; /* no eeprom found */
}


/*
 * determine the card brand/model
 * BKTR_OVERRIDE_CARD, BKTR_OVERRIDE_TUNER, BKTR_OVERRIDE_DBX and
 * BKTR_OVERRIDE_MSP can be used to select a specific device,
 * regardless of the autodetection and i2c device checks.
 *
 * The scheme used for probing cards faces these problems:
 *  It is impossible to work out which type of tuner is actually fitted,
 *  (the driver cannot tell if the Tuner is PAL or NTSC, Temic or Philips)
 *  It is impossible to determine what audio-mux hardware is connected.
 *  It is impossible to determine if there is extra hardware connected to the
 *  GPIO pins  (eg radio chips or MSP34xx reset logic)
 *
 * However some makes of card (eg Hauppauge) come with a configuration eeprom
 * which tells us the make of the card. Most eeproms also tell us the
 * tuner type and other features of the cards.
 *
 * The current probe code works as follows
 * A) If the card uses a Bt878/879:
 *   1) Read the sub-system vendor id from the configuration EEPROM.
 *      Select the required tuner, audio mux arrangement and any other
 *      onboard features. If this fails, move to step B.
 * B) If it card uses a Bt848, 848A, 849A or an unknown Bt878/879:
 *   1) Look for I2C devices. If there are none fitted, it is an Intel or
 *      VideoLogic cards.
 *   2) Look for a configuration EEPROM.
 *   2a) If there is one at I2C address 0xa0 it may be
 *       a Hauppauge or an Osprey. Check the EEPROM contents to determine which
 *       one it is. For Hauppauge, select the tuner type and audio hardware.
 *   2b) If there is an EEPROM at I2C address 0xa8 it will be an STB card.
 *       We still have to guess on the tuner type.
 *
 * C) If we do not know the card type from (A) or (B), guess at the tuner
 *    type based on the I2C address of the tuner.
 *
 * D) After determining the Tuner Type, we probe the i2c bus for other
 *    devices at known locations, eg IR-Remote Control, MSP34xx and TDA
 *    stereo chips.
 */


/*
 * These are the sub-system vendor ID codes stored in the
 * configuration EEPROM used on Bt878/879 cards. They should match the
 * number assigned to the company by the PCI Special Interest Group
 */
/* Following not confirmed with http://members.hyperlink.net.au/~chart,
   so not added to OpenBSD's pcidevs */
#define PCI_VENDOR_LEADTEK_ALT	0x6606	/* this is swapped w/ prod id */
#define PCI_VENDOR_LEADTEK_ALT_2        0x6607
#define PCI_VENDOR_LEADTEK_ALT_3        0x107d
#define PCI_VENDOR_FLYVIDEO	0x1851
#define PCI_VENDOR_FLYVIDEO_2	0x1852
#define PCI_VENDOR_PINNACLE_ALT	0xBD11	/* this is swapped w/ prod id */

void
probeCard( bktr_ptr_t bktr, int verbose, int unit )
{
	int		card, i,j, card_found;
	int		status;
	u_char 		probe_signature[128], *probe_temp;
	int		any_i2c_devices;
	u_char 		eeprom[256];
	int 		tuner_i2c_address = -1;
	int 		eeprom_i2c_address = -1;

	/* Select all GPIO bits as inputs */
	OUTL(bktr, BKTR_GPIO_OUT_EN, 0);
	if (bootverbose)
	    printf("%s: GPIO is 0x%08x\n", bktr_name(bktr),
		   INL(bktr, BKTR_GPIO_DATA));

#ifdef HAUPPAUGE_MSP_RESET
	/* Reset the MSP34xx audio chip. This resolves bootup card
	 * detection problems with old Bt848 based Hauppauge cards with
	 * MSP34xx stereo audio chips. This must be user enabled because
	 * at this point the probe function does not know the card type. */
	OUTL(bktr, BKTR_GPIO_OUT_EN, INL(bktr, BKTR_GPIO_OUT_EN) | (1<<5));
	OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
	DELAY(2500); /* wait 2.5ms */
	OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) & ~(1<<5)); /* write '0' */
	DELAY(2500); /* wait 2.5ms */
	OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
	DELAY(2500); /* wait 2.5ms */
#endif

	/* Check for the presence of i2c devices */
	any_i2c_devices = check_for_i2c_devices( bktr );


	/* Check for a user specified override on the card selection */
#if defined( BKTR_OVERRIDE_CARD )
	bktr->card = cards[ (card = BKTR_OVERRIDE_CARD) ];
	goto checkEEPROM;
#endif
	if (bktr->bt848_card != -1 ) {
	  bktr->card = cards[ (card = bktr->bt848_card) ];
	  goto checkEEPROM;
	}


	/* No override, so try and determine the make of the card */

	/* On BT878/879 cards, read the sub-system vendor id */
	/* This identifies the manufacturer of the card and the model */
	/* In theory this can be read from PCI registers but this does not */
	/* appear to work on the FlyVideo 98. Hauppauge also warned that */
	/* the PCI registers are sometimes not loaded correctly. */
	/* Therefore, I will read the sub-system vendor ID from the EEPROM */
	/* (just like the Bt878 does during power up initialisation) */

	if ((bktr->id==BROOKTREE_878) || (bktr->id==BROOKTREE_879)) {
	    /* Try and locate the EEPROM */
	    eeprom_i2c_address = locate_eeprom_address( bktr );
	    if (eeprom_i2c_address != -1) {

		unsigned int subsystem_vendor_id; /* vendors PCI-SIG ID */
		unsigned int subsystem_id;        /* board model number */
		unsigned int byte_252, byte_253, byte_254, byte_255;

		bktr->card = cards[ (card = CARD_UNKNOWN) ];
		bktr->card.eepromAddr = eeprom_i2c_address;
		bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);

	        if (readEEProm(bktr, 0, 256, eeprom) && bootverbose)
			printf("%s: error reading EEPROM\n", bktr_name(bktr));
		byte_252 = (unsigned int)eeprom[252];
		byte_253 = (unsigned int)eeprom[253];
		byte_254 = (unsigned int)eeprom[254];
		byte_255 = (unsigned int)eeprom[255];

		subsystem_id        = (byte_252 << 8) | byte_253;
		subsystem_vendor_id = (byte_254 << 8) | byte_255;

	        if (bootverbose)
	            printf("%s: subsystem 0x%04x 0x%04x\n", bktr_name(bktr),
			   subsystem_vendor_id, subsystem_id);

		if (subsystem_vendor_id == PCI_VENDOR_AVERMEDIA) {
			bktr->card = cards[ (card = CARD_AVER_MEDIA) ];
			bktr->card.eepromAddr = eeprom_i2c_address;
			bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
			goto checkTuner;
		}

		if (subsystem_vendor_id == PCI_VENDOR_HAUPPAUGE) {
			bktr->card = cards[ (card = CARD_HAUPPAUGE) ];
			bktr->card.eepromAddr = eeprom_i2c_address;
			bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
			goto checkTuner;
		}

		if ((subsystem_vendor_id == PCI_VENDOR_FLYVIDEO) ||
		    (subsystem_vendor_id == PCI_VENDOR_FLYVIDEO_2) ) {
			bktr->card = cards[ (card = CARD_FLYVIDEO) ];
			bktr->card.eepromAddr = eeprom_i2c_address;
			bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
			goto checkTuner;
		}

                if (subsystem_vendor_id == PCI_VENDOR_STB) {
                    bktr->card = cards[ (card = CARD_STB) ];
		    bktr->card.eepromAddr = eeprom_i2c_address;
		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
                    goto checkTuner;
                }

                if (subsystem_vendor_id == PCI_VENDOR_ASKEY) {
                    bktr->card = cards[ (card = CARD_ASKEY_DYNALINK_MAGIC_TVIEW) ];
		    bktr->card.eepromAddr = eeprom_i2c_address;
		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
                    goto checkTuner;
                }

                if ((subsystem_vendor_id == PCI_VENDOR_LEADTEK)
                 || (subsystem_vendor_id == PCI_VENDOR_LEADTEK_ALT)
		 || (subsystem_vendor_id == PCI_VENDOR_LEADTEK_ALT_2)
		 || (subsystem_vendor_id == PCI_VENDOR_LEADTEK_ALT_3)) {
		    if (subsystem_id == PCI_PRODUCT_LEADTEK_WINFAST_XP)
		        bktr->card = cards[ (card = CARD_LEADTEK_XP) ];
		    else
		        bktr->card = cards[ (card = CARD_LEADTEK) ];
		    bktr->card.eepromAddr = eeprom_i2c_address;
		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
                    goto checkTuner;
                }

		if (subsystem_vendor_id == PCI_VENDOR_PINNACLE_ALT) {
                    bktr->card = cards[ (card = CARD_MIRO) ];
		    bktr->card.eepromAddr = eeprom_i2c_address;
		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
                    goto checkTuner;
                }

		if (subsystem_vendor_id == PCI_VENDOR_TERRATEC) {
                    bktr->card = cards[ (card = CARD_TERRATVPLUS) ];
		    bktr->card.eepromAddr = eeprom_i2c_address;
		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
                    goto checkTuner;
                }

		if (subsystem_vendor_id == PCI_VENDOR_ATI) {
                    bktr->card = cards[ (card = CARD_TVWONDER) ];
		    bktr->card.eepromAddr = eeprom_i2c_address;
		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
                    goto checkTuner;
                }

		/*
		 * Check which card as the GV-BCTV4 and GV-BCTV5 IODATA make
		 * are somewhat different to the GV-BCTV3.
		 */

		if (subsystem_vendor_id == PCI_VENDOR_IODATA &&
		    subsystem_id == PCI_PRODUCT_IODATA_GV_BCTV3) {
		    bktr->card = cards[ (card = CARD_IO_BCTV3) ];
		    bktr->card.eepromAddr = eeprom_i2c_address;
		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
		    goto checkTuner;
		}

		if (subsystem_vendor_id == PCI_VENDOR_ZOLTRIX) {
		    if (subsystem_id == PCI_PRODUCT_ZOLTRIX_GENIE_TV_FM) {
			bktr->card = cards[ (card = CARD_ZOLTRIX_GENIE_FM) ];
		    } else {
			bktr->card = cards[ (card = CARD_ZOLTRIX) ];
		    }
 		    bktr->card.eepromAddr = eeprom_i2c_address;
 		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
		    goto checkTuner;
		}

                /* Vendor is unknown. We will use the standard probe code */
		/* which may not give best results */
                printf("%s: Warning "
		    "- card vendor 0x%04x (model 0x%04x) unknown.\n",
		    bktr_name(bktr), subsystem_vendor_id, subsystem_id);
            } else
                printf("%s: Card has no configuration EEPROM. "
		    "Cannot determine card make.\n", bktr_name(bktr));
	} /* end of bt878/bt879 card detection code */

	/* If we get to this point, we must have a Bt848/848A/849A card */
	/* or a Bt878/879 with an unknown subsystem vendor id */
        /* Try and determine the make of card by clever i2c probing */

   	/* Check for i2c devices. If none, move on */
	if (!any_i2c_devices) {
		bktr->card = cards[ (card = CARD_INTEL) ];
		bktr->card.eepromAddr = 0;
		bktr->card.eepromSize = 0;
		goto checkTuner;
	}

        /* Look for Hauppauge, STB and Osprey cards by the presence */
	/* of an EEPROM */
        /* Note: Bt878 based cards also use EEPROMs so we can only do this */
        /* test on BT848/848A and 849A based cards. */
	if ((bktr->id==BROOKTREE_848)  ||
	    (bktr->id==BROOKTREE_848A) ||
	    (bktr->id==BROOKTREE_849A)) {

            /* At i2c address 0xa0, look for Hauppauge and Osprey cards */
            if ( (status = i2cRead( bktr, PFC8582_RADDR )) != ABSENT ) {

		    /* Read the eeprom contents */
		    bktr->card = cards[ (card = CARD_UNKNOWN) ];
		    bktr->card.eepromAddr = PFC8582_WADDR;
		    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
	            readEEProm(bktr, 0, 128, eeprom );

		    /* For Hauppauge, check the EEPROM begins with 0x84 */
		    if (eeprom[0] == 0x84) {
                            bktr->card = cards[ (card = CARD_HAUPPAUGE) ];
			    bktr->card.eepromAddr = PFC8582_WADDR;
			    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
                            goto checkTuner;
		    }

		    /* For Osprey, check the EEPROM begins with "MMAC" */
		    if (  (eeprom[0] == 'M') &&(eeprom[1] == 'M')
			&&(eeprom[2] == 'A') &&(eeprom[3] == 'C')) {
                            bktr->card = cards[ (card = CARD_OSPREY) ];
			    bktr->card.eepromAddr = PFC8582_WADDR;
			    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
                            goto checkTuner;
		    }
		    printf("%s: Warning: Unknown card type. EEPROM data not recognised\n",
			   bktr_name(bktr));
		    printf("%s: %x %x %x %x\n", bktr_name(bktr),
			   eeprom[0],eeprom[1],eeprom[2],eeprom[3]);
            }

            /* look for an STB card */
            if ( (status = i2cRead( bktr, X24C01_RADDR )) != ABSENT ) {
                    bktr->card = cards[ (card = CARD_STB) ];
		    bktr->card.eepromAddr = X24C01_WADDR;
		    bktr->card.eepromSize = (u_char)(128 / EEPROMBLOCKSIZE);
                    goto checkTuner;
            }

	}

	signCard( bktr, 1, 128, (u_char *)  &probe_signature );

	if (bootverbose) {
	  printf("%s: card signature: ", bktr_name(bktr));
	  for (j = 0; j < Bt848_MAX_SIGN; j++) {
	    printf(" %02x ", probe_signature[j]);
	  }
	  printf("\n\n");
	}
	for (i = 0;
	     i < (sizeof bt848_card_signature)/ sizeof (struct bt848_card_sig);
	     i++ ) {

	  card_found = 1;
	  probe_temp = (u_char *) &bt848_card_signature[i].signature;

	  for (j = 0; j < Bt848_MAX_SIGN; j++) {
	    if ((probe_temp[j] & 0xf) != (probe_signature[j] & 0xf)) {
	      card_found = 0;
	      break;
	    }

	  }
	  if (card_found) {
	    bktr->card = cards[ card = bt848_card_signature[i].card];
	    eeprom_i2c_address = locate_eeprom_address( bktr );
	    if (eeprom_i2c_address != -1) {
		bktr->card.eepromAddr = eeprom_i2c_address;
		bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
	    } else {
		bktr->card.eepromAddr = 0;
		bktr->card.eepromSize = 0;
	    }
	    tuner_i2c_address = locate_tuner_address( bktr );
	    select_tuner( bktr, bt848_card_signature[i].tuner );
	    goto checkDBX;
	  }
	}

	/* We do not know the card type. Default to Miro */
	bktr->card = cards[ (card = CARD_MIRO) ];


checkEEPROM:
	/* look for a configuration eeprom */
	eeprom_i2c_address = locate_eeprom_address( bktr );
	if (eeprom_i2c_address != -1) {
	    bktr->card.eepromAddr = eeprom_i2c_address;
	    bktr->card.eepromSize = (u_char)(256 / EEPROMBLOCKSIZE);
	} else {
	    bktr->card.eepromAddr = 0;
	    bktr->card.eepromSize = 0;
	}


checkTuner:

	/* look for a tuner */
	tuner_i2c_address = locate_tuner_address( bktr );
	if ( tuner_i2c_address == -1 ) {
		select_tuner( bktr, NO_TUNER );
		goto checkDBX;
	}

#if defined( BKTR_OVERRIDE_TUNER )
	select_tuner( bktr, BKTR_OVERRIDE_TUNER );
	goto checkDBX;
#endif
	if (bktr->bt848_tuner != -1 ) {
	  select_tuner( bktr, bktr->bt848_tuner & 0xff );
	  goto checkDBX;
	}

	/* Check for i2c devices */
	if (!any_i2c_devices) {
		select_tuner( bktr, NO_TUNER );
		goto checkDBX;
	}

	/* differentiate type of tuner */

	switch (card) {
	case CARD_MIRO:
	    switch (((INL(bktr, BKTR_GPIO_DATA) >> 10)-1)&7) {
	    case 0: select_tuner( bktr, TEMIC_PAL ); break;
	    case 1: select_tuner( bktr, PHILIPS_PAL ); break;
	    case 2: select_tuner( bktr, PHILIPS_NTSC ); break;
	    case 3: select_tuner( bktr, PHILIPS_SECAM ); break;
	    case 4: select_tuner( bktr, NO_TUNER ); break;
	    case 5: select_tuner( bktr, PHILIPS_PALI ); break;
	    case 6: select_tuner( bktr, TEMIC_NTSC ); break;
	    case 7: select_tuner( bktr, TEMIC_PALI ); break;
	    }
	    goto checkDBX;
	    break;

	case CARD_HAUPPAUGE:
	    /* Hauppauge kindly supplied the following Tuner Table */
	    /* FIXME: I think the tuners the driver selects for types */
	    /* 0x08 and 0x15 may be incorrect but no one has complained. */
	    /* Old Temic tuners had their own API, but newer Temic tuners */
	    /* have the same API as Philips tuners */
	    /*
  ID  Tuner Model           Format			We select Format
 0x00 NONE
 0x01 EXTERNAL
 0x02 OTHER
 0x03 Philips FI1216        BG
 0x04 Philips FI1216MF      BGLL'			PHILIPS_SECAM
 0x05 Philips FI1236        MN 				PHILIPS_NTSC
 0x06 Philips FI1246        I 				PHILIPS_PALI
 0x07 Philips FI1256        DK
 0x08 Philips FI1216 MK2    BG 				PHILIPS_PALI
 0x09 Philips FI1216MF MK2  BGLL' 			PHILIPS_SECAM
 0x0a Philips FI1236 MK2    MN 				PHILIPS_NTSC
 0x0b Philips FI1246 MK2    I 				PHILIPS_PALI
 0x0c Philips FI1256 MK2    DK
 0x0d Temic 4032FY5         NTSC			TEMIC_NTSC
 0x0e Temic 4002FH5         BG				TEMIC_PAL
 0x0f Temic 4062FY5         I 				TEMIC_PALI
 0x10 Philips FR1216 MK2    BG
 0x11 Philips FR1216MF MK2  BGLL' 			PHILIPS_FR1236_SECAM
 0x12 Philips FR1236 MK2    MN 				PHILIPS_FR1236_NTSC
 0x13 Philips FR1246 MK2    I
 0x14 Philips FR1256 MK2    DK
 0x15 Philips FM1216        BG 				PHILIPS_FR1216_PAL
 0x16 Philips FM1216MF      BGLL' 			PHILIPS_FR1236_SECAM
 0x17 Philips FM1236        MN 				PHILIPS_FR1236_NTSC
 0x18 Philips FM1246        I
 0x19 Philips FM1256        DK
 0x1a Temic 4036FY5         MN (FI1236 MK2 clone)	PHILIPS_NTSC
 0x1b Samsung TCPN9082D     MN
 0x1c Samsung TCPM9092P     Pal BG/I/DK
 0x1d Temic 4006FH5         BG				PHILIPS_PALI
 0x1e Samsung TCPN9085D     MN/Radio
 0x1f Samsung TCPB9085P     Pal BG/I/DK / Radio
 0x20 Samsung TCPL9091P     Pal BG & Secam L/L'
 0x21 Temic 4039FY5         NTSC Radio
 0x22 Philips FQ1216ME      Pal BGIDK & Secam L/L'
 0x23 Temic 4066FY5         Pal I (FI1246 MK2 clone)	PHILIPS_PALI
 0x24 Philips TD1536        MN/ATSCDigital
 0x25 Philips TD1536D       MN/ATSCDigital DUAL INPUT
 0x26 Philips FMR1236       M/N FM(no demod)
 0x27 Philips FI1256MP      B/G, D/K
 0x28 Samsung TCPQ9091P     BG/I/DK, L/L'
 0x29 Temic 4006FN5         BG/I/DK
 0x2a Temic 4009FR5         BG FM			PHILIPS_FR1216_PAL
 0x2b Temic 4046FM5         B/G, I, D/K, L/L'
 0x2c Temic 4009FN5         B/G, I, D/K, FM (no demod)
 0x2d Philips TD1536D_FH_44 MN/ATSCDigital DUAL INPUT
	    */


	    /* Determine the model number from the eeprom */
	    if (bktr->card.eepromAddr != 0) {
	        /* eeprom data block structure */
	        unsigned char *block_1, *block_2, *block_3, *block_4;
	        int block_1_data_size,  block_2_data_size, block_3_data_size;
	        int block_1_total_size, block_2_total_size, block_3_total_size;
		int block_4_header_size;

		unsigned int model,revision;
		unsigned char tuner_code;
		unsigned char no_audio_mux;

		readEEProm(bktr, 0, 128, eeprom);

	        /* LOCATE THE EEPROM DATA BLOCKS */
	        block_1 = &eeprom[0];
	        block_1_data_size = (block_1[2] << 8 | block_1[1]);
	        block_1_total_size = block_1_data_size + 3; /* Header bytes */

	        block_2 = &eeprom[block_1_total_size];
	        block_2_data_size = (block_2[2] << 8 | block_2[1]);
	        block_2_total_size = block_2_data_size + 3; /* Header bytes */

	        block_3 = &eeprom[block_1_total_size + block_2_total_size];
	        block_3_data_size = (block_3[0] &0x07);
	        block_3_total_size = block_3_data_size + 1; /* Header size */

	        block_4 = &eeprom[block_1_total_size +block_2_total_size +block_3_total_size];
	        block_4_header_size = 1;

		model    = (block_1[12] << 8  | block_1[11]);
		revision = (block_1[15] << 16 | block_1[14] << 8 | block_1[13]);

		tuner_code = block_1[9];

		no_audio_mux = ((block_3[3] >> 7) &0x01);

		if (no_audio_mux) bktr->audio_mux_present = 0;

		if (verbose)
		    printf("%s: Hauppauge Model %d %c%c%c%c\n",
			   bktr_name(bktr),
			   model,
			   ((revision >> 18) & 0x3f) + 32,
			   ((revision >> 12) & 0x3f) + 32,
			   ((revision >>  6) & 0x3f) + 32,
			   ((revision >>  0) & 0x3f) + 32 );

	        /* Determine the tuner type from the eeprom */

		switch (tuner_code) {

		  case 0x5:
                  case 0x0a:
                  case 0x1a:
		    select_tuner( bktr, PHILIPS_NTSC );
		    goto checkDBX;

		  case 0x4:
                  case 0x9:
		    select_tuner( bktr, PHILIPS_SECAM );
		    goto checkDBX;

                  case 0x11:
	          case 0x16:
		    select_tuner( bktr, PHILIPS_FR1236_SECAM );
		    goto checkDBX;

		  case 0x12:
		  case 0x17:
		  case 0x21:	/* hauppage #191 wintv-fm -wsr 2003/1/13 */
		    select_tuner( bktr, PHILIPS_FR1236_NTSC );
		    goto checkDBX;

		  case 0x6:
	          case 0x8:
	          case 0xb:
	          case 0x1d:
	          case 0x23:
		    select_tuner( bktr, PHILIPS_PALI );
		    goto checkDBX;

	          case 0xd:
		    select_tuner( bktr, TEMIC_NTSC );
		    goto checkDBX;

                  case 0xe:
		    select_tuner( bktr, TEMIC_PAL );
		    goto checkDBX;

	          case 0xf:
		    select_tuner( bktr, TEMIC_PALI );
		    goto checkDBX;

                  case 0x15:
		    select_tuner( bktr, PHILIPS_FR1216_PAL );
		    goto checkDBX;

                  case 0x2a:
		    bktr->msp_use_mono_source = 1;
		    select_tuner( bktr, PHILIPS_FR1216_PAL );
		    goto checkDBX;

	          default :
		    printf("%s: Warning - Unknown Hauppauge Tuner 0x%x\n",
			   bktr_name(bktr), tuner_code);
		}
	    }
	    break;


	case CARD_AVER_MEDIA:
	    /* AVerMedia kindly supplied some details of their EEPROM contents
	     * which allow us to auto select the Tuner Type.
	     * Only the newer AVerMedia cards actually have an EEPROM.
	     */
	    if (bktr->card.eepromAddr != 0) {

		u_char tuner_make;   /* Eg Philips, Temic */
		u_char tuner_tv_fm;  /* TV or TV with FM Radio */
		u_char tuner_format; /* Eg NTSC, PAL, SECAM */
		int    tuner;

		int tuner_0_table[] = {
			PHILIPS_NTSC,  PHILIPS_PAL,
			PHILIPS_PAL,   PHILIPS_PAL,
			PHILIPS_PAL,   PHILIPS_PAL,
			PHILIPS_SECAM, PHILIPS_SECAM,
			PHILIPS_SECAM, PHILIPS_PAL};

		int tuner_0_fm_table[] = {
			PHILIPS_FR1236_NTSC,  PHILIPS_FR1216_PAL,
			PHILIPS_FR1216_PAL,   PHILIPS_FR1216_PAL,
			PHILIPS_FR1216_PAL,   PHILIPS_FR1216_PAL,
			PHILIPS_FR1236_SECAM, PHILIPS_FR1236_SECAM,
			PHILIPS_FR1236_SECAM, PHILIPS_FR1216_PAL};

		int tuner_1_table[] = {
			TEMIC_NTSC,  TEMIC_PAL,   TEMIC_PAL,
			TEMIC_PAL,   TEMIC_PAL,   TEMIC_PAL,
			TEMIC_SECAM, TEMIC_SECAM, TEMIC_SECAM,
			TEMIC_PAL};


		/* Extract information from the EEPROM data */
	    	readEEProm(bktr, 0, 128, eeprom);

		tuner_make   = (eeprom[0x41] & 0x7);
		tuner_tv_fm  = (eeprom[0x41] & 0x18) >> 3;
		tuner_format = (eeprom[0x42] & 0xf0) >> 4;

		/* Treat tuner make 0 (Philips) and make 2 (LG) the same */
		if ( ((tuner_make == 0) || (tuner_make == 2))
		    && (tuner_format <= 9) && (tuner_tv_fm == 0) ) {
			tuner = tuner_0_table[tuner_format];
			select_tuner( bktr, tuner );
			goto checkDBX;
		}

		if ( ((tuner_make == 0) || (tuner_make == 2))
		    && (tuner_format <= 9) && (tuner_tv_fm == 1) ) {
			tuner = tuner_0_fm_table[tuner_format];
			select_tuner( bktr, tuner );
			goto checkDBX;
		}

		if ( (tuner_make == 1) && (tuner_format <= 9) ) {
			tuner = tuner_1_table[tuner_format];
			select_tuner( bktr, tuner );
			goto checkDBX;
		}

	    	printf("%s: Warning - Unknown AVerMedia Tuner Make %d Format %d\n",
			bktr_name(bktr), tuner_make, tuner_format);
	    }
	    break;

	case CARD_LEADTEK:
#if BKTR_SYSTEM_DEFAULT == BROOKTREE_PAL
	    select_tuner( bktr, PHILIPS_FR1216_PAL );
#else
	    select_tuner( bktr, PHILIPS_FR1236_NTSC );
#endif
            goto checkDBX;
	    break;

	case CARD_TVWONDER:
	    select_tuner( bktr, PHILIPS_NTSC );
	    goto checkDBX;
	    break;

	case CARD_ZOLTRIX_GENIE_FM:
	    select_tuner( bktr, PHILIPS_FR1236_NTSC );
	    goto checkDBX;
	    break;

	case CARD_IO_BCTV3:
	    select_tuner( bktr, ALPS_TSCH5 ); /* ALPS_TSCH6, in fact. */
	    goto checkDBX;
 	    break;

	case CARD_LEADTEK_XP:
	    select_tuner( bktr, TIVISION_TVF5533 );
	    goto checkDBX;
	    break;

	} /* end switch(card) */


	/* At this point, a goto checkDBX has not occurred */
	/* We have not been able to select a Tuner */
	/* Some cards make use of the tuner address to */
	/* identify the make/model of tuner */

	/* At address 0xc0/0xc1 we often find a TEMIC NTSC */
	if ( i2cRead( bktr, 0xc1 ) != ABSENT ) {
	    select_tuner( bktr, TEMIC_NTSC );
	    goto checkDBX;
	}

	/* At address 0xc6/0xc7 we often find a PHILIPS NTSC Tuner */
	if ( i2cRead( bktr, 0xc7 ) != ABSENT ) {
	    select_tuner( bktr, PHILIPS_NTSC );
	    goto checkDBX;
	}

	/* Address 0xc2/0xc3 is default (or common address) for several */
	/* tuners and we cannot tell which is which. */
	/* And for all other tuner i2c addresses, select the default */
	select_tuner( bktr, DEFAULT_TUNER );


checkDBX:
#if defined( BKTR_OVERRIDE_DBX )
	bktr->card.dbx = BKTR_OVERRIDE_DBX;
	goto checkMSP;
#endif
   /* Check for i2c devices */
	if (!any_i2c_devices) {
		goto checkMSP;
	}

	/* probe for BTSC (dbx) chip */
	if ( i2cRead( bktr, TDA9850_RADDR ) != ABSENT )
		bktr->card.dbx = 1;

checkMSP:
	/* If this is a Hauppauge Bt878 card, we need to enable the
	 * MSP 34xx audio chip.
	 * If this is a Hauppauge Bt848 card, reset the MSP device.
	 * The MSP reset line is wired to GPIO pin 5. On Bt878 cards a pulldown
	 * resistor holds the device in reset until we set GPIO pin 5.
	 */

	/* Optionally skip the MSP reset. This is handy if you initialise the
	 * MSP audio in another operating system (eg Windows) first and then
	 * do a soft reboot.
	 */

#ifndef BKTR_NO_MSP_RESET
	if (card == CARD_HAUPPAUGE) {
	    OUTL(bktr, BKTR_GPIO_OUT_EN, INL(bktr, BKTR_GPIO_OUT_EN) | (1<<5));
	    OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
	    DELAY(2500); /* wait 2.5ms */
	    OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) & ~(1<<5)); /* write '0' */
	    DELAY(2500); /* wait 2.5ms */
	    OUTL(bktr, BKTR_GPIO_DATA, INL(bktr, BKTR_GPIO_DATA) | (1<<5));  /* write '1' */
	    DELAY(2500); /* wait 2.5ms */
	}
#endif

#if defined( BKTR_OVERRIDE_MSP )
	bktr->card.msp3400c = BKTR_OVERRIDE_MSP;
	goto checkMSPEnd;
#endif

	/* Check for i2c devices */
	if (!any_i2c_devices) {
		goto checkMSPEnd;
	}

	if ( i2cRead( bktr, MSP3400C_RADDR ) != ABSENT ) {
		bktr->card.msp3400c = 1;
	}

checkMSPEnd:

	if (bktr->card.msp3400c) {
		bktr->msp_addr = MSP3400C_WADDR;
		msp_read_id( bktr );
		printf("%s: Detected a MSP%s at 0x%x\n", bktr_name(bktr),
		       bktr->msp_version_string,
		       bktr->msp_addr);

	}

/* Check for Dolby Surround Sound DPL3518A sound chip */
	if ( i2cRead( bktr, DPL3518A_RADDR ) != ABSENT ) {
		bktr->card.dpl3518a = 1;
	}

	if (bktr->card.dpl3518a) {
		bktr->dpl_addr = DPL3518A_WADDR;
		dpl_read_id( bktr );
		printf("%s: Detected a DPL%s at 0x%x\n", bktr_name(bktr),
		       bktr->dpl_version_string,
		       bktr->dpl_addr);
	}

/* Start of Check Remote */
	/* Check for the Hauppauge IR Remote Control */
	/* If there is an external unit, the internal will be ignored */

	bktr->remote_control = 0; /* initial value */

	if (any_i2c_devices) {
		if (i2cRead( bktr, HAUP_REMOTE_EXT_RADDR ) != ABSENT ) {
			bktr->remote_control      = 1;
			bktr->remote_control_addr = HAUP_REMOTE_EXT_RADDR;
		} else if (i2cRead( bktr, HAUP_REMOTE_INT_RADDR ) != ABSENT ) {
			bktr->remote_control      = 1;
			bktr->remote_control_addr = HAUP_REMOTE_INT_RADDR;
		}
	}

	/* If a remote control is found, poll it 5 times to turn off the LED */
	if (bktr->remote_control) {
		int i;
		for (i=0; i<5; i++)
			i2cRead( bktr, bktr->remote_control_addr );
	}
/* End of Check Remote */

#if defined( BKTR_USE_PLL )
	bktr->xtal_pll_mode = BT848_USE_PLL;
	goto checkPLLEnd;
#endif
	/* Default is to use XTALS and not PLL mode */
	bktr->xtal_pll_mode = BT848_USE_XTALS;

	/* Enable PLL mode for OSPREY users */
	if (card == CARD_OSPREY)
		bktr->xtal_pll_mode = BT848_USE_PLL;

	/* Enable PLL mode for Video Highway Xtreme users */
	if (card == CARD_VIDEO_HIGHWAY_XTREME)
		bktr->xtal_pll_mode = BT848_USE_PLL;

	/* Enable PLL mode for ATI TV_Wonder/VE */
	if (card == CARD_TVWONDER)
		bktr->xtal_pll_mode = BT848_USE_PLL;


	/* Most (perhaps all) Bt878 cards need to be switched to PLL mode */
	/* as they only fit the NTSC crystal to their cards */
	/* Default to enabling PLL mode for all Bt878/879 cards */

	if ((bktr->id==BROOKTREE_878 || bktr->id==BROOKTREE_879) )
		bktr->xtal_pll_mode = BT848_USE_PLL;


#if defined( BKTR_USE_PLL )
checkPLLEnd:
#endif


	bktr->card.tuner_pllAddr = tuner_i2c_address;

	if ( verbose ) {
		printf( "%s: %s", bktr_name(bktr), bktr->card.name );
		if ( bktr->card.tuner )
			printf( ", %s tuner", bktr->card.tuner->name );
		if ( bktr->card.dbx )
			printf( ", dbx stereo" );
		if ( bktr->card.msp3400c )
			printf( ", msp3400c stereo" );
		if ( bktr->card.dpl3518a )
			printf( ", dpl3518a dolby" );
		if ( bktr->remote_control )
			printf( ", remote control" );
		printf( ".\n" );
	}
}

#undef ABSENT