root/lm-sensors/trunk/prog/detect/sensors-detect @ 967

Revision 967, 79.5 KB (checked in by mds, 13 years ago)

serverworks now uses piix4 driver.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl -w
2
3#
4#    sensors-detect - Detect PCI bus and chips
5#    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
6#
7#    This program is free software; you can redistribute it and/or modify
8#    it under the terms of the GNU General Public License as published by
9#    the Free Software Foundation; either version 2 of the License, or
10#    (at your option) any later version.
11#
12#    This program is distributed in the hope that it will be useful,
13#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#    GNU General Public License for more details.
16#
17#    You should have received a copy of the GNU General Public License
18#    along with this program; if not, write to the Free Software
19#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20#
21
22# TODO: Better handling of chips with several addresses
23
24# A Perl wizard really ought to look upon this; the PCI and I2C stuff should
25# each be put in a separate file, using modules and packages. That is beyond
26# me.
27
28require 5.004;
29
30use strict;
31
32#########################
33# CONSTANT DECLARATIONS #
34#########################
35
36use vars qw(@pci_adapters @chip_ids @undetectable_adapters);
37
38@undetectable_adapters = ( "i2c-elektor","i2c-elv", "i2c-philips-par",
39                           "i2c-velleman" );
40
41# This is the list of SMBus or I2C adapters we recognize by their PCI
42# signature. This is an easy and fast way to determine which SMBus or I2C
43# adapters should be present.
44# Each entry must have a vindid (Vendor ID), devid (Device ID), func (PCI
45# Function) and procid (string as appears in /proc/pci; see linux/driver/pci,
46# either pci.c or oldproc.c). If no driver is written yet, omit the
47# driver (Driver Name) field. The match (Match Description) field should
48# contain a function which returns zero if its two parameter matches
49# the text as it would appear in /proc/bus/i2c.
50@pci_adapters = ( 
51     { 
52       vendid => 0x8086,
53       devid  => 0x7113,
54       func => 3,
55       procid => "Intel 82371AB PIIX4 ACPI",
56       driver => "i2c-piix4",
57       match => sub { $_[0] =~ /^SMBus PIIX4 adapter at [0-9,a-f]{4}/ },
58     } , 
59     { 
60       vendid => 0x8086,
61       devid  => 0x2413,
62       func => 3,
63       procid => "Intel 82801AA ICH",
64       driver => "i2c-i801",
65       match => sub { $_[0] =~ /^SMBus I801 adapter at [0-9,a-f]{4}/ },
66     } , 
67     { 
68       vendid => 0x8086,
69       devid  => 0x2423,
70       func => 3,
71       procid => "Intel 82801AB ICH0",
72       driver => "i2c-i801",
73       match => sub { $_[0] =~ /^SMBus I801 adapter at [0-9,a-f]{4}/ },
74     } , 
75     { 
76       vendid => 0x8086,
77       devid  => 0x2443,
78       func => 3,
79       procid => "Intel 82801BA ICH2",
80       driver => "i2c-i801",
81       match => sub { $_[0] =~ /^SMBus I801 adapter at [0-9,a-f]{4}/ },
82     } , 
83     { 
84       vendid => 0x1106,
85       devid  => 0x3040,
86       func => 3,
87       procid => "VIA Technologies VT 82C586B Apollo ACPI",
88       driver => "i2c-via",
89       match => sub { $_[0] =~ /^VIA i2c/ },
90     } ,
91     { 
92       vendid => 0x1106,
93       devid  => 0x3050,
94       func => 3,
95       procid => "VIA Technologies VT 82C596 Apollo ACPI",
96       driver => "i2c-viapro",
97       match => sub { $_[0] =~ /^SMBus vt82c596 adapter at [0-9,a-f]{4}/ },
98     } ,
99     { 
100       vendid => 0x1106,
101       devid  => 0x3051,
102       func => 3,
103       procid => "VIA Technologies VT 82C596B ACPI",
104       driver => "i2c-viapro",
105       match => sub { $_[0] =~ /^SMBus vt82c596 adapter at [0-9,a-f]{4}/ },
106     } ,
107     { 
108       vendid => 0x1106,
109       devid  => 0x3057,
110       func => 4,
111       procid => "VIA Technologies VT 82C686 Apollo ACPI",
112       driver => "i2c-viapro",
113       match => sub { $_[0] =~ /^SMBus vt82c596 adapter at [0-9,a-f]{4}/ },
114     } ,
115     {
116       vendid => 0x1039,
117       devid  => 0x0008,
118       func => 0,
119       procid => "Silicon Integrated Systems SIS5595",
120       driver => "i2c-sis5595",
121       match => sub {  $_[0] =~ /^SMBus SIS5595 adapter at [0-9,a-f]{4}/ },
122     } ,
123     {
124       vendid => 0x10b9,
125       devid => 0x7101,
126       funcid => 0,
127       procid => "Acer Labs M7101",
128       driver => "i2c-ali15x3",
129       match => sub { $_[0] =~ /^SMBus ALI15X3 adapter at [0-9,a-f]{4}/ },
130     },
131     { 
132       vendid => 0x106b,
133       devid  => 0x000e,
134       func => 0,
135       procid => "Apple Computer Inc. Hydra Mac I/O",
136       driver => "i2c-hydra",
137       match => sub { $_[0] =~ /^Hydra i2c/ },
138     },
139     { 
140       vendid => 0x1022,
141       devid  => 0x740b,
142       func => 3,
143       procid => "AMD-756 Athlon ACPI",
144       driver => "i2c-amd756",
145       match => sub { $_[0] =~ /^SMBus AMD756 adapter at [0-9,a-f]{4}/ },
146     },
147     {
148       vendid => 0x102b,
149       devid  => 0x0519,
150       func   => 0,
151       procid => "MGA 2064W [Millennium]",
152       driver => "i2c-matroxfb",
153       match  => sub { $_[0] =~ /^DDC:fb[0-9]{1,2}/ },
154     },
155     {
156       vendid => 0x102b,
157       devid  => 0x051a,
158       func   => 0,
159       procid => "MGA 1064SG [Mystique]",
160       driver => "i2c-matroxfb",
161       match  => sub { $_[0] =~ /^DDC:fb[0-9]{1,2}/ },
162     },
163     {
164       vendid => 0x102b,
165       devid  => 0x051b,
166       func   => 0,
167       procid => "MGA 2164W [Millennium II]",
168       driver => "i2c-matroxfb",
169       match  => sub { $_[0] =~ /^DDC:fb[0-9]{1,2}/ },
170     },
171     {
172       vendid => 0x102b,
173       devid  => 0x051e,
174       func   => 0,
175       procid => "MGA 1064SG [Mystique] AGP",
176       driver => "i2c-matroxfb",
177       match  => sub { $_[0] =~ /^DDC:fb[0-9]{1,2}/ },
178     },
179     {
180       vendid => 0x102b,
181       devid  => 0x051f,
182       func   => 0,
183       procid => "MGA 2164W [Millennium II] AGP",
184       driver => "i2c-matroxfb",
185       match  => sub { $_[0] =~ /^DDC:fb[0-9]{1,2}/ },
186     },
187     {
188       vendid => 0x102b,
189       devid  => 0x1000,
190       func   => 0,
191       procid => "MGA G100 [Productiva]",
192       driver => "i2c-matroxfb",
193       match  => sub { $_[0] =~ /^DDC:fb[0-9]{1,2}/ },
194     },
195     {
196       vendid => 0x102b,
197       devid  => 0x1001,
198       func   => 0,
199       procid => "MGA G100 [Productiva] AGP",
200       driver => "i2c-matroxfb",
201       match  => sub { $_[0] =~ /^DDC:fb[0-9]{1,2}/ },
202     },
203     {
204       vendid => 0x102b,
205       devid  => 0x0520,
206       func   => 0,
207       procid => "MGA G200",
208       driver => "i2c-matroxfb",
209       match  => sub { $_[0] =~ /^DDC:fb[0-9]{1,2}/ },
210     },
211     {
212       vendid => 0x102b,
213       devid  => 0x0521,
214       func   => 0,
215       procid => "MGA G200 AGP",
216       driver => "i2c-matroxfb",
217       match  => sub { $_[0] =~ /^DDC:fb[0-9]{1,2}/ },
218     },
219     {
220       vendid => 0x102b,
221       devid  => 0x0525,
222       func   => 0,
223       procid => "MGA G400 AGP",
224       driver => "i2c-matroxfb",
225       match  => sub { $_[0] =~ /^(DDC,MAVEN):fb[0-9]{1,2}/ },
226     },
227     {
228       vendid => 0x121a,
229       devid  => 0x0005,
230       func   => 0,
231       procid => "3Dfx Voodoo3",
232       driver => "i2c-voodoo3",
233       match  => sub { $_[0] =~ /Banshee adapter/ },
234     },
235     {
236       vendid => 0x121a,
237       devid  => 0x0003,
238       func   => 0,
239       procid => "3Dfx Voodoo Banshee",
240       driver => "i2c-voodoo3",
241       match  => sub { $_[0] =~ /Banshee adapter/ },
242     },
243     { 
244       vendid => 0x8086,
245       devid  => 0x7121,
246       func => 0,
247       procid => "Intel 82810 GMCH",
248       driver => "i2c-i810",
249       match => sub { $_[0] =~ /^I810/ },
250     } , 
251     { 
252       vendid => 0x8086,
253       devid  => 0x7123,
254       func => 0,
255       procid => "Intel 82810-DC100 GMCH",
256       driver => "i2c-i810",
257       match => sub { $_[0] =~ /^I810/ },
258     } , 
259     { 
260       vendid => 0x8086,
261       devid  => 0x1132,
262       func => 0,
263       procid => "Intel 82815 GMCH",
264       driver => "i2c-i810",
265       match => sub { $_[0] =~ /^I810/ },
266     } , 
267     { 
268       vendid => 0x12d2,
269       devid  => 0x0018,
270       func => 0,
271       procid => "RIVA 128",
272       driver => "i2c-riva",
273       match => sub { $_[0] =~ /^NVIDIA display/ },
274     } , 
275     { 
276       vendid => 0x10de,
277       devid  => 0x0020,
278       func => 0,
279       procid => "RIVA TNT",
280       driver => "i2c-riva",
281       match => sub { $_[0] =~ /^NVIDIA display/ },
282     } , 
283     { 
284       vendid => 0x10de,
285       devid  => 0x0028,
286       func => 0,
287       procid => "RIVA TNT2",
288       driver => "i2c-riva",
289       match => sub { $_[0] =~ /^NVIDIA display/ },
290     } , 
291     { 
292       vendid => 0x10de,
293       devid  => 0x0029,
294       func => 0,
295       procid => "RIVA UTNT2",
296       driver => "i2c-riva",
297       match => sub { $_[0] =~ /^NVIDIA display/ },
298     } , 
299     { 
300       vendid => 0x10de,
301       devid  => 0x002c,
302       func => 0,
303       procid => "RIVA VTNT2",
304       driver => "i2c-riva",
305       match => sub { $_[0] =~ /^NVIDIA display/ },
306     } , 
307     { 
308       vendid => 0x10de,
309       devid  => 0x002d,
310       func => 0,
311       procid => "RIVA UVTNT2",
312       driver => "i2c-riva",
313       match => sub { $_[0] =~ /^NVIDIA display/ },
314     } , 
315     { 
316       vendid => 0x10de,
317       devid  => 0x00a0,
318       func => 0,
319       procid => "RIVA ITNT2",
320       driver => "i2c-riva",
321       match => sub { $_[0] =~ /^NVIDIA display/ },
322     } , 
323     { 
324       vendid => 0x10de,
325       devid  => 0x0100,
326       func => 0,
327       procid => "GeForce 256",
328       driver => "i2c-riva",
329       match => sub { $_[0] =~ /^NVIDIA display/ },
330     } , 
331     { 
332       vendid => 0x10de,
333       devid  => 0x0101,
334       func => 0,
335       procid => "GeForce DDR",
336       driver => "i2c-riva",
337       match => sub { $_[0] =~ /^NVIDIA display/ },
338     } , 
339     { 
340       vendid => 0x10de,
341       devid  => 0x0102,
342       func => 0,
343       procid => "Quadro",
344       driver => "i2c-riva",
345       match => sub { $_[0] =~ /^NVIDIA display/ },
346     } , 
347     { 
348       vendid => 0x10de,
349       devid  => 0x0150,
350       func => 0,
351       procid => "GeForce GTS",
352       driver => "i2c-riva",
353       match => sub { $_[0] =~ /^NVIDIA display/ },
354     } , 
355     { 
356       vendid => 0x1166,
357       devid  => 0x0200,
358       func => 0,
359       procid => "ServerWorks South Bridge",
360       driver => "i2c-piix4",
361       match => sub { $_[0] =~ /^SMBus OSB4 adapter at / },
362     } , 
363);
364
365use subs qw(mtp008_detect lm78_detect lm78_isa_detect lm78_alias_detect
366            lm75_detect lm80_detect w83781d_detect w83781d_alias_detect
367            adm1025_detect w83781d_isa_detect gl518sm_detect gl520sm_detect
368            adm9240_detect adm1021_detect sis5595_isa_detect eeprom_detect
369            via686a_isa_detect adm1022_detect ltc1710_detect gl525sm_detect
370            lm87_detect ite_detect ite_isa_detect ite_alias_detect
371            ddcmonitor_detect ds1621_detect);
372
373# This is a list of all recognized chips.
374# Each entry must have the following fields:
375#  name: The full chip name
376#  driver: The driver name (without .o extension). Put in something like
377#      "Unwritten: <drivername>" if it is not yet available.
378#  i2c_addrs (optional): For I2C chips, the range of valid I2C addresses to
379#      probe. Recommend avoiding 0x69 because of clock chips.
380#  i2c_driver_addrs (optional): For I2C chips, the range of valid I2C
381#      addresses probed by the kernel driver. Strictly optional.
382#  i2c_detect (optional): For I2C chips, the function to call to detect
383#      this chip. The function should take two parameters: an open file
384#      descriptor to access the bus, and the I2C address to probe.
385#  isa_addrs (optional): For ISA chips, the range of valid port addresses to
386#      probe.
387#  isa_driver_addrs (optional): For ISA chips, the range of valid ISA
388#      addresses probed by the kernel driver. Strictly optional.
389#  isa_detect (optional): For ISA chips, the function to call to detect
390#      this chip. The function should take one parameter: the ISA address
391#      to probe.
392#  alias_detect (optional): For chips which can be both on the ISA and the
393#      I2C bus, a function which detectes whether two entries are the same.
394#      The function should take three parameters: The ISA address, the
395#      I2C bus number, and the I2C address.
396@chip_ids = (
397     {
398       name => "Myson MTP008",
399       driver => "mtp008",
400       i2c_addrs => [0x2c..0x2e], 
401       i2c_detect => sub { mtp008_detect @_},
402     } ,
403     {
404       name => "National Semiconductor LM78",
405       driver => "lm78",
406       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
407       i2c_driver_addrs => [0x20..0x2f], 
408       i2c_detect => sub { lm78_detect 0, @_},
409       isa_addrs => [0x290],
410       isa_detect => sub { lm78_isa_detect 0, @_ },
411       alias_detect => sub { lm78_alias_detect 0, @_ },
412     } ,
413     {
414       name => "National Semiconductor LM78-J",
415       driver => "lm78",
416       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
417       i2c_driver_addrs => [0x20..0x2f], 
418       i2c_detect => sub { lm78_detect 1, @_ },
419       isa_addrs => [0x290],
420       isa_detect => sub { lm78_isa_detect 1, @_ },
421       alias_detect => sub { lm78_alias_detect 1, @_ },
422     } ,
423     {
424       name => "National Semiconductor LM79",
425       driver => "lm78",
426       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
427       i2c_driver_addrs => [0x20..0x2f], 
428       i2c_detect => sub { lm78_detect 2, @_ },
429       isa_addrs => [0x290],
430       isa_detect => sub { lm78_isa_detect 2, @_ },
431       alias_detect => sub { lm78_alias_detect 2, @_ },
432     } ,
433     {
434       name => "National Semiconductor LM75",
435       driver => "lm75",
436       i2c_addrs => [0x48..0x4f],
437       i2c_detect => sub { lm75_detect @_},
438     } ,
439     {
440       name => "National Semiconductor LM80",
441       driver => "lm80",
442       i2c_addrs => [0x28..0x2f],
443       i2c_detect => sub { lm80_detect @_} ,
444     },
445     {
446       name => "National Semiconductor LM87",
447       driver => "lm87",
448       i2c_addrs => [0x2c..0x2e],
449       i2c_detect => sub { lm87_detect @_} ,
450     },
451     {
452       name => "Winbond W83781D",
453       driver => "w83781d",
454       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
455       i2c_detect => sub { w83781d_detect 0, @_},
456       i2c_driver_addrs => [0x20..0x2f], 
457       isa_addrs => [0x290],
458       isa_detect => sub { w83781d_isa_detect 0, @_ },
459       alias_detect => sub { w83781d_alias_detect 0, @_ },
460     } ,
461     {
462       name => "Winbond W83782D",
463       driver => "w83781d",
464       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
465       i2c_driver_addrs => [0x20..0x2f], 
466       i2c_detect => sub { w83781d_detect 1, @_},
467       isa_addrs => [0x290],
468       isa_detect => sub { w83781d_isa_detect 1, @_ },
469       alias_detect => sub { w83781d_alias_detect 1, @_ },
470     } ,
471     {
472       name => "Winbond W83783S",
473       driver => "w83781d",
474       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
475       i2c_driver_addrs => [0x20..0x2f], 
476       i2c_detect => sub { w83781d_detect 2, @_},
477     } ,
478     {
479       name => "Winbond W83627HF",
480       driver => "w83781d",
481       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
482       i2c_driver_addrs => [0x20..0x2f], 
483       i2c_detect => sub { w83781d_detect 3, @_},
484       isa_addrs => [0x290],
485       isa_detect => sub { w83781d_isa_detect 3, @_ },
486       alias_detect => sub { w83781d_alias_detect 3, @_ },
487     } ,
488     {
489       name => "Asus AS99127F",
490       driver => "w83781d",
491       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
492       i2c_driver_addrs => [0x20..0x2f], 
493       i2c_detect => sub { w83781d_detect 4, @_},
494     } ,
495     {
496       name => "Winbond W83697HF",
497       driver => "Unwritten (W83697HF)",
498       isa_addrs => [0x290],
499       isa_detect => sub { w83781d_isa_detect 5, @_ },
500     } ,
501     {
502       name => "Genesys Logic GL518SM Revision 0x00",
503       driver => "gl518sm",
504       i2c_addrs => [0x2c, 0x2d],
505       i2c_detect => sub { gl518sm_detect 0, @_} ,
506     },
507     {
508       name => "Genesys Logic GL518SM Revision 0x80",
509       driver => "gl518sm",
510       i2c_addrs => [0x2c, 0x2d],
511       i2c_detect => sub { gl518sm_detect 1, @_} ,
512     },
513     {
514       name => "Genesys Logic GL520SM",
515       driver => "gl520sm",
516       i2c_addrs => [0x2c, 0x2d],
517       i2c_detect => sub { gl520sm_detect @_} ,
518     },
519     {
520       name => "Genesys Logic GL525SM",
521       driver => "Unwritten (GL525SM)",
522       i2c_addrs => [0x2d],
523       i2c_detect => sub { gl525sm_detect @_} ,
524     },
525     {
526       name => "Analog Devices ADM9240",
527       driver => "adm9240",
528       i2c_addrs => [0x2c..0x2f],
529       i2c_detect => sub { adm9240_detect 0, @_ }
530     },
531     {
532       name => "Dallas Semiconductor DS1621",
533       driver => "ds1621",
534       i2c_addrs => [0x48..0x4f],
535       i2c_detect => sub { ds1621_detect @_},
536     } ,
537     {
538       name => "Dallas Semiconductor DS1780",
539       driver => "adm9240",
540       i2c_addrs => [0x2c..0x2f],
541       i2c_detect => sub { adm9240_detect 1, @_ }
542     },
543     {
544       name => "National Semiconductor LM81",
545       driver => "adm9240",
546       i2c_addrs => [0x2c..0x2f],
547       i2c_detect => sub { adm9240_detect 2, @_ }
548     },
549     {
550       name => "Analog Devices ADM1025",
551       driver => "adm1025",
552       i2c_addrs => [0x2c..0x2e],
553       i2c_detect => sub { adm1025_detect 0, @_ }
554     },
555     {
556       name => "Analog Devices ADM1021",
557       driver => "adm1021",
558       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
559       i2c_detect => sub { adm1021_detect 0, @_ },
560     },
561     {
562       name => "Maxim MAX1617",
563       driver => "adm1021",
564       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
565       i2c_detect => sub { adm1021_detect 1, @_ },
566     },
567     {
568       name => "Maxim MAX1617A",
569       driver => "adm1021",
570       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
571       i2c_detect => sub { adm1021_detect 2, @_ },
572     },
573     {
574       name => "TI THMC10",
575       driver => "adm1021",
576       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
577       i2c_detect => sub { adm1021_detect 3, @_ },
578     },
579     {
580       name => "National Semiconductor LM84",
581       driver => "adm1021",
582       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
583       i2c_detect => sub { adm1021_detect 4, @_ },
584     },
585     {
586       name => "Genesys Logic GL523SM",
587       driver => "adm1021",
588       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
589       i2c_detect => sub { adm1021_detect 5, @_ },
590     },
591     {
592       name => "Analog Devices ADM1022",
593       driver => "thmc50",
594       i2c_addrs => [0x2c..0x2f],
595       i2c_detect => sub { adm1022_detect 0, @_ },
596     },
597     {
598       name => "Texas Instruments THMC50",
599       driver => "thmc50",
600       i2c_addrs => [0x2c..0x2f],
601       i2c_detect => sub { adm1022_detect 1, @_ },
602     },
603     {
604       name => "Silicon Integrated Systems SIS5595",
605       driver => "sis5595",
606       isa_addrs => [ 0 ],
607       isa_detect => sub { sis5595_isa_detect @_ },
608     },
609     {
610       name => "VIA Technologies VT 82C686 Integrated Sensors",
611       driver => "via686a",
612       isa_addrs => [ 0 ],
613       isa_detect => sub { via686a_isa_detect @_ },
614     },
615     {
616       name => "ITE IT8705F / IT8712F",
617       driver => "Unwritten (IT87xxF)",
618       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
619       i2c_driver_addrs => [0x20..0x2f], 
620       i2c_detect => sub { ite_detect 0, @_},
621       isa_addrs => [0x290],
622       isa_detect => sub { ite_isa_detect 0, @_ },
623       alias_detect => sub { ite_alias_detect 0, @_ },
624     } ,
625     {
626       name => "Serial EEPROM (PC-100 DIMM)",
627       driver => "eeprom",
628       i2c_addrs => [0x50..0x57],
629       i2c_detect => sub { eeprom_detect @_ },
630     },
631     {
632       name => "LTC1710",
633       driver => "ltc1710",
634       i2c_addrs => [0x58..0x5a],
635       i2c_detect => sub { ltc1710_detect @_ },
636     },
637     {
638       name => "DDC monitor",
639       driver => "ddcmon",
640       i2c_addrs => [0x50],
641       i2c_detect => sub { ddcmonitor_detect @_ },
642     },
643);
644
645
646#######################
647# AUXILIARY FUNCTIONS #
648#######################
649
650sub swap_bytes
651{
652  return (($_[0] & 0xff00) >> 8) + (($_[0] & 0x00ff) << 8)
653}
654
655# $_[0] is the sought value
656# @_[1..] is the list to seek in
657# Returns: 0 on failure, 1 if found.
658sub contains
659{
660  my $sought = shift;
661  foreach (@_) {
662    return 1 if $sought eq $_;
663  }
664  return 0;
665}
666
667sub parse_not_to_scan
668{
669  my ($min,$max,$to_parse) = @_;
670  my @ranges = split /\s*,\s*/, $to_parse;
671  my @res = ();
672  my $range;
673  foreach $range (@ranges) {
674    my ($start,$end) = split /\s*-s*/, $range;
675    $start = oct $start if $start =~ /^0/;
676    if (defined $end) {
677      $end = oct $end if $end =~ /^0/;
678      $start = $min if $start < $min;
679      $end = $max if $end > $max;
680      push @res, ($start+0..$end+0);
681    } else {
682      push @res, $start+0 if $start >= $min and $start <= $max;
683    }
684  }
685  return sort { $a <=> $b } @res;
686}
687
688# @_[0]: Reference to list 1
689# @_[1]: Reference to list 2
690# Result: 0 if they have no elements in common, 1 if they have
691# Elements must be numeric.
692sub any_list_match
693{
694  my ($list1,$list2) = @_;
695  my ($el1,$el2);
696  foreach $el1 (@$list1) {
697    foreach $el2 (@$list2) {
698      return 1 if $el1 == $el2;
699    }
700  }
701  return 0;
702}
703
704###################
705# I/O port access #
706###################
707
708sub initialize_ioports
709{
710  sysopen IOPORTS, "/dev/port", 2;
711}
712
713# $_[0]: port to read
714# Returns: -1 on failure, read value on success.
715sub inb
716{
717  my ($res,$nrchars);
718  sysseek IOPORTS, $_[0], 0 or return -1;
719  $nrchars = sysread IOPORTS, $res, 1;
720  return -1 if not defined $nrchars or $nrchars != 1;
721  $res = unpack "C",$res ;
722  return $res;
723}
724
725# $_[0]: port to write
726# $_[1]: value to write
727# Returns: -1 on failure, 0 on success.
728sub outb
729{
730  my $towrite = pack "C", $_[1];
731  sysseek IOPORTS, $_[0], 0 or return -1;
732  my $nrchars = syswrite IOPORTS, $towrite, 1;
733  return -1 if not defined $nrchars or $nrchars != 1;
734  return 0;
735}
736
737# $_[0]: Address register
738# $_[1]: Data register
739# $_[2]: Register to read
740# Returns: read value
741sub isa_read_byte
742{
743  outb $_[0],$_[2];
744  return inb $_[1];
745}
746
747# $_[0]: Address register
748# $_[1]: Data register
749# $_[2]: Register to write
750# $_[3}: Value to write
751# Returns: nothing
752sub isa_write_byte
753{
754  outb $_[0],$_[2];
755  outb $_[1],$_[3];
756}
757
758###########
759# MODULES #
760###########
761
762use vars qw(@modules_list);
763
764sub initialize_modules_list
765{
766  open INPUTFILE, "/proc/modules" or die "Can't access /proc/modules!";
767  while (<INPUTFILE>) {
768    push @modules_list, /^(\S*)/ ;
769  }
770  close INPUTFILE;
771}
772
773##############
774# PCI ACCESS #
775##############
776
777use vars qw(@pci_list);
778
779# This function returns a list of hashes. Each hash has some PCI information
780# (more than we will ever need, probably). The most important
781# fields are 'bus', 'slot', 'func' (they uniquely identify a PCI device in
782# a computer) and 'vendid','devid' (they uniquely identify a type of device).
783# /proc/bus/pci/devices is only available on late 2.1 and 2.2 kernels.
784sub read_proc_dev_pci
785{
786  my ($dfn,$vend,@pci_list);
787  open INPUTFILE, "/proc/bus/pci/devices" or return;
788  while (<INPUTFILE>) {
789    my $record = {};
790    ($dfn,$vend,$record->{irq},$record->{base_addr0},$record->{base_addr1},
791          $record->{base_addr2},$record->{base_addr3},$record->{base_addr4},
792          $record->{base_addr5},$record->{rom_base_addr}) = 
793          map { oct "0x$_" } split;
794    $record->{bus} = $dfn >> 8;
795    $record->{slot} = ($dfn & 0xf8) >> 3;
796    $record->{func} = $dfn & 0x07;
797    $record->{vendid} = $vend >> 16;
798    $record->{devid} = $vend & 0xffff;
799  push @pci_list,$record;
800  }
801  close INPUTFILE or return;
802  return @pci_list;
803}
804
805# This function returns a list of hashes. Each hash has some PCI
806# information. The important fields here are 'bus', 'slot', 'func' (they
807# uniquely identify a PCI device in a computer) and 'desc' (a functional
808# description of the PCI device). If this is an 'unknown device', the
809# vendid and devid fields are set instead.
810sub read_proc_pci
811{
812  my @pci_list;
813  open INPUTFILE, "/proc/pci" or return;
814  while (<INPUTFILE>) {
815    my $record = {};
816    if (($record->{bus},$record->{slot},$record->{func}) = 
817        /^\s*Bus\s*(\S)+\s*,\s*device\s*(\S+)\s*,\s*function\s*(\S+)\s*:\s*$/) {
818      my $desc = <INPUTFILE>;
819      $_ = <INPUTFILE>;
820      if (($desc =~ /Unknown device/) and
821              (($record->{vendid},$record->{devid}) = 
822                         /^\s*Vendor id=(\S+)\.\s*Device id=(\S+)\.$/)) {
823        $record->{vendid} = hex $record->{vendid};
824        $record->{devid} = hex $record->{devid};
825      } else {
826        $record->{desc} = $desc;
827      }
828      push @pci_list,$record;
829    }
830  }
831  close INPUTFILE or return;
832  return @pci_list;
833}
834
835sub initialize_proc_pci
836{
837  @pci_list = read_proc_dev_pci;
838  @pci_list = read_proc_pci     if not defined @pci_list;
839  die "Can't access either /proc/bus/pci/ or /proc/pci!" 
840                                    if not defined @pci_list;
841}
842
843#####################
844# ADAPTER DETECTION #
845#####################
846
847sub all_available_adapters
848{
849  my @res = ();
850  my ($module,$adapter);
851  MODULES:
852  foreach $module (@modules_list) {
853    foreach $adapter (@pci_adapters) {
854      if (exists $adapter->{driver} and $module eq $adapter->{driver}) {
855        push @res, $module;
856        next MODULES;
857      }
858    }
859  }
860  return @res;
861}
862
863sub adapter_pci_detection
864{
865  my ($device,$try,@res);
866  print "Probing for PCI bus adapters...\n";
867
868  foreach $device (@pci_list) {
869    foreach $try (@pci_adapters) {
870      if ((defined($device->{vendid}) and 
871           $try->{vendid} == $device->{vendid} and
872           $try->{devid} == $device->{devid} and
873           $try->{func} == $device->{func}) or
874          (! defined($device->{vendid}) and
875           $device->{desc} =~ /$try->{procid}/ and
876           $try->{func} == $device->{func})) {
877        printf "Use driver `%s' for device %02x:%02x.%x: %s\n",
878               $try->{driver}?$try->{driver}:"<To Be Written>",
879               $device->{bus},$device->{slot},$device->{func},$try->{procid};
880        push @res,$try->{driver};
881      }
882    }
883  }
884  if (! @res) {
885    print ("Sorry, no PCI bus adapters found.\n");
886  } else {
887    printf ("Probe succesfully concluded.\n");
888  }
889  return @res;
890}
891
892# $_[0]: Adapter description as found in /proc/bus/i2c
893# $_[1]: Algorithm description as found in /proc/bus/i2c
894sub find_adapter_driver
895{
896  my $adapter;
897  for $adapter (@pci_adapters) {
898    return $adapter->{driver} if &{$adapter->{match}} ($_[0],$_[1]);
899  }
900  return "UNKNOWN";
901}
902
903#############################
904# I2C AND SMBUS /DEV ACCESS #
905#############################
906
907# This should really go into a separate module/package.
908
909# To do: support i2c-level access (through sysread/syswrite, probably).
910# I can't test this at all (PIIX4 does not support this), so I have not
911# included it.
912
913use vars qw($IOCTL_I2C_RETRIES $IOCTL_I2C_TIMEOUT $IOCTL_I2C_UDELAY
914            $IOCTL_I2C_MDELAY $IOCTL_I2C_SLAVE $IOCTL_I2C_TENBIT
915            $IOCTL_I2C_SMBUS);
916
917# These are copied from <linux/i2c.h> and <linux/smbus.h>
918
919# For bit-adapters:
920$IOCTL_I2C_RETRIES = 0x0701;
921$IOCTL_I2C_TIMEOUT = 0x0702;
922$IOCTL_I2C_UDELAY = 0x0705;
923$IOCTL_I2C_MDELAY = 0x0706;
924
925# General ones:
926$IOCTL_I2C_SLAVE = 0x0703;
927$IOCTL_I2C_TENBIT = 0x0704;
928$IOCTL_I2C_SMBUS = 0x0720;
929
930
931
932use vars qw($SMBUS_READ $SMBUS_WRITE $SMBUS_QUICK $SMBUS_BYTE $SMBUS_BYTE_DATA
933            $SMBUS_WORD_DATA $SMBUS_PROC_CALL $SMBUS_BLOCK_DATA);
934
935# These are copied from <linux/smbus.h>
936
937$SMBUS_READ = 1;
938$SMBUS_WRITE = 0;
939$SMBUS_QUICK = 0;
940$SMBUS_BYTE = 1;
941$SMBUS_BYTE_DATA  = 2;
942$SMBUS_WORD_DATA  = 3;
943$SMBUS_PROC_CALL = 4;
944$SMBUS_BLOCK_DATA = 5;
945
946# Select the device to communicate with through its address.
947# $_[0]: Reference to an opened filehandle
948# $_[1]: Address to select
949# Returns: 0 on failure, 1 on success.
950sub i2c_set_slave_addr
951{
952  my ($file,$addr) = @_;
953  ioctl $file, $IOCTL_I2C_SLAVE, $addr or return 0;
954  return 1;
955}
956
957# i2c_smbus_access is based upon the corresponding C function (see
958# <linux/i2c-dev.h>). You should not need to call this directly.
959# Exact calling conventions are intricate; read i2c-dev.c if you really need
960# to know.
961# $_[0]: Reference to an opened filehandle
962# $_[1]: $SMBUS_READ for reading, $SMBUS_WRITE for writing
963# $_[2]: Command (usually register number)
964# $_[3]: Transaction kind ($SMBUS_BYTE, $SMBUS_BYTE_DATA, etc.)
965# $_[4]: Reference to an array used for input/output of data
966# Returns: 0 on failure, 1 on success.
967# Note that we need to get back to Integer boundaries through the 'x2'
968# in the pack. This is very compiler-dependent; I wish there was some other
969# way to do this.
970sub i2c_smbus_access
971{
972  my ($file,$read_write,$command,$size,$data) = @_;
973  my $data_array = pack "C32", @$data;
974  my $ioctl_data = pack "C2x2Ip", ($read_write,$command,$size,$data_array);
975  ioctl $file, $IOCTL_I2C_SMBUS, $ioctl_data or return 0;
976  $_[4] = [ unpack "C32",$data_array ];
977  return 1;
978}
979
980# $_[0]: Reference to an opened filehandle
981# $_[1]: Either 0 or 1
982# Returns: -1 on failure, the 0 on success.
983sub i2c_smbus_write_quick
984{
985  my ($file,$value) = @_;
986  my $data = [];
987  i2c_smbus_access $file, $value, 0, $SMBUS_QUICK, $data 
988         or return -1;
989  return 0;
990}
991
992# $_[0]: Reference to an opened filehandle
993# Returns: -1 on failure, the read byte on success.
994sub i2c_smbus_read_byte
995{
996  my ($file) = @_;
997  my $data = [];
998  i2c_smbus_access $file, $SMBUS_READ, 0, $SMBUS_BYTE, $data 
999         or return -1;
1000  return $$data[0];
1001}
1002
1003# $_[0]: Reference to an opened filehandle
1004# $_[1]: Byte to write
1005# Returns: -1 on failure, 0 on success.
1006sub i2c_smbus_write_byte
1007{
1008  my ($file,$command) = @_;
1009  my $data = [$command];
1010  i2c_smbus_access $file, $SMBUS_WRITE, 0, $SMBUS_BYTE, $data 
1011         or return -1;
1012  return 0;
1013}
1014
1015# $_[0]: Reference to an opened filehandle
1016# $_[1]: Command byte (usually register number)
1017# Returns: -1 on failure, the read byte on success.
1018sub i2c_smbus_read_byte_data
1019{
1020  my ($file,$command) = @_;
1021  my $data = [];
1022  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_BYTE_DATA, $data 
1023         or return -1;
1024  return $$data[0];
1025}
1026 
1027# $_[0]: Reference to an opened filehandle
1028# $_[1]: Command byte (usually register number)
1029# $_[2]: Byte to write
1030# Returns: -1 on failure, 0 on success.
1031sub i2c_smbus_write_byte_data
1032{
1033  my ($file,$command,$value) = @_;
1034  my $data = [$value];
1035  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_BYTE_DATA, $data 
1036         or return -1;
1037  return 0;
1038}
1039
1040# $_[0]: Reference to an opened filehandle
1041# $_[1]: Command byte (usually register number)
1042# Returns: -1 on failure, the read word on success.
1043# Note: some devices use the wrong endiannes; use swap_bytes to correct for
1044# this.
1045sub i2c_smbus_read_word_data
1046{
1047  my ($file,$command) = @_;
1048  my $data = [];
1049  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_WORD_DATA, $data 
1050         or return -1;
1051  return $$data[0] + 256 * $$data[1];
1052}
1053
1054# $_[0]: Reference to an opened filehandle
1055# $_[1]: Command byte (usually register number)
1056# $_[2]: Byte to write
1057# Returns: -1 on failure, 0 on success.
1058# Note: some devices use the wrong endiannes; use swap_bytes to correct for
1059# this.
1060sub i2c_smbus_write_word_data
1061{
1062  my ($file,$command,$value) = @_;
1063  my $data = [$value & 0xff, $value >> 8];
1064  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_WORD_DATA, $data 
1065         or return -1;
1066  return 0;
1067}
1068
1069# $_[0]: Reference to an opened filehandle
1070# $_[1]: Command byte (usually register number)
1071# $_[2]: Word to write
1072# Returns: -1 on failure, read word on success.
1073# Note: some devices use the wrong endiannes; use swap_bytes to correct for
1074# this.
1075sub i2c_smbus_process_call
1076{
1077  my ($file,$command,$value) = @_;
1078  my $data = [$value & 0xff, $value >> 8];
1079  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_PROC_CALL, $data 
1080         or return -1;
1081  return $$data[0] + 256 * $$data[1];
1082}
1083
1084# $_[0]: Reference to an opened filehandle
1085# $_[1]: Command byte (usually register number)
1086# Returns: Undefined on failure, a list of read bytes on success
1087# Note: some devices use the wrong endiannes; use swap_bytes to correct for
1088# this.
1089sub i2c_smbus_read_block_data
1090{
1091  my ($file,$command) = @_;
1092  my $data = [];
1093  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_BLOCK_DATA, $data 
1094         or return;
1095  shift @$data;
1096  return @$data;
1097}
1098
1099# $_[0]: Reference to an opened filehandle
1100# $_[1]: Command byte (usually register number)
1101# @_[2..]: List of values to write
1102# Returns: -1 on failure, 0 on success.
1103# Note: some devices use the wrong endiannes; use swap_bytes to correct for
1104# this.
1105sub i2c_smbus_write_block_data
1106{
1107  my ($file,$command,@data) = @_;
1108  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_BLOCK_DATA, \@data 
1109         or return;
1110  return 0;
1111}
1112
1113####################
1114# ADAPTER SCANNING #
1115####################
1116
1117use vars qw(@chips_detected);
1118
1119# We will build a complicated structure @chips_detected here, being:
1120# A list of
1121#  references to hashes
1122#    with field 'driver', being a string with the driver name for this chip;
1123#    with field 'detected'
1124#      being a reference to a list of
1125#        references to hashes of type 'detect_data';
1126#    with field 'misdetected'
1127#      being a reference to a list of
1128#        references to hashes of type 'detect_data'
1129
1130# Type detect_data:
1131# A hash
1132#   with field 'i2c_adap' containing an adapter string as appearing
1133#        in /proc/bus/i2c (if this is an I2C detection)
1134#  with field 'i2c_algo' containing an algorithm string as appearing
1135#       in /proc/bus/i2c (if this is an I2C detection)
1136#  with field 'i2c_devnr', contianing the /dev/i2c-* number of this
1137#       adapter (if this is an I2C detection)
1138#  with field 'i2c_driver', containing the driver name for this adapter
1139#       (if this is an I2C detection)
1140#  with field 'i2c_addr', containing the I2C address of the detection;
1141#       (if this is an I2C detection)
1142#  with field 'i2c_sub_addrs', containing a reference to a list of
1143#       other I2C addresses (if this is an I2C detection)
1144#  with field 'i2c_extra' if this is an I2C detection and the address
1145#       is not normally probed by the kernel driver
1146#  with field 'isa_addr' containing the ISA address this chip is on
1147#       (if this is an ISA detection)
1148#  with field 'isa_extra' if this is an ISA detection and the address
1149#       is not normally probed by the kernel driver
1150#  with field 'conf', containing the confidence level of this detection
1151#  with field 'chipname', containing the chip name
1152
1153# This adds a detection to the above structure. We do no alias detection
1154# here; so you should do ISA detections *after* all I2C detections.
1155# Not all possibilities of i2c_addr and i2c_sub_addrs are exhausted.
1156# In all normal cases, it should be all right.
1157# $_[0]: chip driver
1158# $_[1]: reference to data hash
1159# Returns: Nothing
1160sub add_i2c_to_chips_detected
1161{
1162  my ($chipdriver,$datahash) = @_;
1163  my ($i,$new_detected_ref,$new_misdetected_ref,$detected_ref,$misdetected_ref,
1164      $main_entry,$detected_entry,$put_in_detected,@hash_addrs,@entry_addrs);
1165
1166  # First determine where the hash has to be added.
1167  for ($i = 0; $i < @chips_detected; $i++) {
1168    last if ($chips_detected[$i]->{driver} eq $chipdriver);
1169  }
1170  if ($i == @chips_detected) {
1171    push @chips_detected, { driver => $chipdriver,
1172                            detected => [],
1173                            misdetected => [] };
1174  }
1175  $new_detected_ref = $chips_detected[$i]->{detected};
1176  $new_misdetected_ref = $chips_detected[$i]->{misdetected};
1177
1178  # Find out whether our new entry should go into the detected or the
1179  # misdetected list. We compare all i2c addresses; if at least one matches,
1180  # but our conf value is lower, we assume this is a misdetect.
1181  @hash_addrs = ($datahash->{i2c_addr});
1182  push @hash_addrs, @{$datahash->{i2c_sub_addrs}}
1183       if exists $datahash->{i2c_sub_addrs};
1184  $put_in_detected = 1;
1185  FIND_LOOP:
1186  foreach $main_entry (@chips_detected) {
1187    foreach $detected_entry (@{$main_entry->{detected}}) {
1188      @entry_addrs = ($detected_entry->{i2c_addr});
1189      push @entry_addrs, @{$detected_entry->{i2c_sub_addrs}}
1190               if exists $detected_entry->{i2c_sub_addrs};
1191      if ($detected_entry->{i2c_devnr} == $datahash->{i2c_devnr} and
1192          any_list_match \@entry_addrs, \@hash_addrs) {
1193        if ($detected_entry->{conf} >= $datahash->{conf}) {
1194          $put_in_detected = 0;
1195        }
1196        last FIND_LOOP;
1197      }
1198    }
1199  }
1200
1201  if ($put_in_detected) {
1202    # Here, we move all entries from detected to misdetected which
1203    # match at least in one main or sub address. This may not be the
1204    # best idea to do, as it may remove detections without replacing
1205    # them with second-best ones. Too bad.
1206    @hash_addrs = ($datahash->{i2c_addr});
1207    push @hash_addrs, @{$datahash->{i2c_sub_addrs}} 
1208         if exists $datahash->{i2c_sub_addrs};
1209    foreach $main_entry (@chips_detected) {
1210      $detected_ref = $main_entry->{detected};
1211      $misdetected_ref = $main_entry->{misdetected};
1212      for ($i = @$detected_ref-1; $i >=0; $i--) {
1213        @entry_addrs = ($detected_ref->[$i]->{i2c_addr});
1214        push @entry_addrs, @{$detected_ref->[$i]->{i2c_sub_addrs}}
1215             if exists $detected_ref->[$i]->{i2c_sub_addrs};
1216        if ($detected_ref->[$i]->{i2c_devnr} == $datahash->{i2c_devnr} and
1217            any_list_match \@entry_addrs, \@hash_addrs) {
1218          push @$misdetected_ref,$detected_ref->[$i];
1219          splice @$detected_ref, $i, 1;
1220        }
1221      }
1222    }
1223
1224    # Now add the new entry to detected
1225    push @$new_detected_ref, $datahash;
1226  } else {
1227    # No hard work here
1228    push @$new_misdetected_ref, $datahash;
1229  }
1230}
1231
1232# This adds a detection to the above structure. We also do alias detection
1233# here; so you should do ISA detections *after* all I2C detections.
1234# $_[0]: alias detection function
1235# $_[1]: chip driver
1236# $_[2]: reference to data hash
1237# Returns: 0 if it is not an alias, datahash reference if it is.
1238sub add_isa_to_chips_detected
1239{
1240  my ($alias_detect,$chipdriver,$datahash) = @_;
1241  my ($i,$new_detected_ref,$new_misdetected_ref,$detected_ref,$misdetected_ref,
1242      $main_entry,$isalias);
1243
1244  # First determine where the hash has to be added.
1245  $isalias=0;
1246  for ($i = 0; $i < @chips_detected; $i++) {
1247    last if ($chips_detected[$i]->{driver} eq $chipdriver);
1248  }
1249  if ($i == @chips_detected) {
1250    push @chips_detected, { driver => $chipdriver,
1251                            detected => [],
1252                            misdetected => [] };
1253  }
1254  $new_detected_ref = $chips_detected[$i]->{detected};
1255  $new_misdetected_ref = $chips_detected[$i]->{misdetected};
1256
1257  # Now, we are looking for aliases. An alias can only be the same chiptype.
1258  # If an alias is found in the misdetected list, we add the new information
1259  # and terminate this function. If it is found in the detected list, we
1260  # still have to check whether another chip has claimed this ISA address.
1261  # So we remove the old entry from the detected list and put it in datahash.
1262
1263  # Misdetected alias detection:
1264  for ($i = 0; $i < @$new_misdetected_ref; $i++) {
1265    if (exists $new_misdetected_ref->[$i]->{i2c_addr} and
1266        not exists $new_misdetected_ref->[$i]->{isa_addr} and
1267        defined $alias_detect and
1268        $new_misdetected_ref->[$i]->{chipname} eq $datahash->{chipname}) {
1269      open FILE,"/dev/i2c-$new_misdetected_ref->[$i]->{i2c_devnr}" or
1270        open FILE,"/dev/i2c$new_misdetected_ref->[$i]->{i2c_devnr}" or
1271           print("Can't open ",
1272                 "/dev/i2c[-]$new_misdetected_ref->[$i]->{i2c_devnr}?!?\n"),
1273           next;
1274      i2c_set_slave_addr \*FILE,$new_misdetected_ref->[$i]->{i2c_addr} or
1275           print("Can't set I2C address for ",
1276                 "/dev/i2c[-]$new_misdetected_ref->[$i]->{i2c_devnr}?!?\n"),
1277           next;
1278      if (&$alias_detect ($datahash->{isa_addr},\*FILE,
1279                          $new_misdetected_ref->[$i]->{i2c_addr})) {
1280        $new_misdetected_ref->[$i]->{isa_addr} = $datahash->{isa_addr};
1281        $new_misdetected_ref->[$i]->{isa_extra} = $datahash->{isa_extra} 
1282               if exists $datahash->{isa_extra};
1283        close FILE;
1284        return $new_misdetected_ref->[$i]; 
1285      }
1286      close FILE;
1287    }
1288  }
1289
1290  # Detected alias detection:
1291  for ($i = 0; $i < @$new_detected_ref; $i++) {
1292    if (exists $new_detected_ref->[$i]->{i2c_addr} and
1293        not exists $new_detected_ref->[$i]->{isa_addr} and
1294        defined $alias_detect and
1295        $new_detected_ref->[$i]->{chipname} eq $datahash->{chipname}) {
1296      open FILE,"/dev/i2c-$new_detected_ref->[$i]->{i2c_devnr}" or
1297        open FILE,"/dev/i2c$new_detected_ref->[$i]->{i2c_devnr}" or
1298           print("Can't open ",
1299                 "/dev/i2c[-]$new_detected_ref->[$i]->{i2c_devnr}?!?\n"),
1300           next;
1301      i2c_set_slave_addr \*FILE,$new_detected_ref->[$i]->{i2c_addr} or
1302           print("Can't set I2C address for ",
1303                 "/dev/i2c[-]$new_detected_ref->[$i]->{i2c_devnr}?!?\n"),
1304           next;
1305      if (&$alias_detect ($datahash->{isa_addr},\*FILE,
1306                          $new_detected_ref->[$i]->{i2c_addr})) {
1307        $new_detected_ref->[$i]->{isa_addr} = $datahash->{isa_addr};
1308        $new_detected_ref->[$i]->{isa_extra} = $datahash->{isa_extra} 
1309               if exists $datahash->{isa_extra};
1310        ($datahash) = splice (@$new_detected_ref, $i, 1);
1311        close FILE;
1312        $isalias=1;
1313        last;
1314      }
1315      close FILE;
1316    }
1317  }
1318
1319
1320  # Find out whether our new entry should go into the detected or the
1321  # misdetected list. We only compare main isa_addr here, of course.
1322  foreach $main_entry (@chips_detected) {
1323    $detected_ref = $main_entry->{detected};
1324    $misdetected_ref = $main_entry->{misdetected};
1325    for ($i = 0; $i < @{$main_entry->{detected}}; $i++) {
1326      if (exists $detected_ref->[$i]->{isa_addr} and
1327          $detected_ref->[$i]->{isa_addr} == $datahash->{isa_addr}) {
1328        if ($detected_ref->[$i]->{conf} >= $datahash->{conf}) {
1329          push @$new_misdetected_ref, $datahash;
1330        } else {
1331          push @$misdetected_ref,$detected_ref->[$i];
1332          splice @$detected_ref, $i,1;
1333          push @$new_detected_ref, $datahash;
1334        }
1335        if ($isalias) {
1336          return $datahash;
1337        } else {
1338          return 0;
1339        }
1340      }
1341    }
1342  }
1343
1344  # Not found? OK, put it in the detected list
1345  push @$new_detected_ref, $datahash;
1346  if ($isalias) {
1347    return $datahash;
1348  } else {
1349    return 0;
1350  }
1351}
1352
1353# $_[0]: The number of the adapter to scan
1354# $_[1]: The name of the adapter, as appearing in /proc/bus/i2c
1355# $_[2]: The name of the algorithm, as appearing in /proc/bus/i2c
1356# $_[3]: The driver of the adapter
1357# @_[4..]: Addresses not to scan
1358sub scan_adapter
1359{
1360  my ( $adapter_nr,$adapter_name,$algorithm_name,$adapter_driver, 
1361       $not_to_scan) = @_;
1362  my ($chip, $addr, $conf,@chips,$new_hash,$other_addr);
1363
1364  # As we modify it, we need a copy
1365  my @not_to_scan = @$not_to_scan;
1366
1367  open FILE,"/dev/i2c-$adapter_nr" or 
1368    open FILE,"/dev/i2c$adapter_nr" or 
1369       (print "Can't open /dev/i2c[-]$adapter_nr\n"), return;
1370
1371  # Now scan each address in turn
1372  foreach $addr (0..0x7f) {
1373    # As the not_to_scan list is sorted, we can check it fast
1374    if (@not_to_scan and $not_to_scan[0] == $addr) {
1375      shift @not_to_scan;
1376      next;
1377    }
1378
1379    i2c_set_slave_addr(\*FILE,$addr) or 
1380        printf("Client at address 0x%02x can not be probed - unload all client drivers first!\n",$addr), next;
1381
1382    next unless i2c_smbus_write_quick(\*FILE,$SMBUS_WRITE) >= 0;
1383    printf "Client found at address 0x%02x\n",$addr;
1384
1385    foreach $chip (@chip_ids) {
1386      if (exists $$chip{i2c_addrs} and contains $addr, @{$$chip{i2c_addrs}}) {
1387        print "Probing for `$$chip{name}'... ";
1388        if (($conf,@chips) = &{$$chip{i2c_detect}} (\*FILE ,$addr)) {
1389          print "Success!\n",
1390                "    (confidence $conf, driver `$$chip{driver}')";
1391          if (@chips) {
1392            print ", other addresses:";
1393            @chips = sort @chips;
1394            foreach $other_addr (sort @chips) {
1395              printf(" 0x%02x",$other_addr);
1396            }
1397          }
1398          printf "\n";
1399          $new_hash = { conf => $conf,
1400                        i2c_addr => $addr,
1401                        chipname =>  $$chip{name},
1402                        i2c_adap => $adapter_name,
1403                        i2c_algo => $algorithm_name,
1404                        i2c_driver => $adapter_driver,
1405                        i2c_devnr => $adapter_nr,
1406                      };
1407          if (@chips) {
1408            my @chips_copy = @chips;
1409            $new_hash->{i2c_sub_addrs} = \@chips_copy;
1410          }
1411          $new_hash->{i2c_extra} = 0 
1412                 if exists $chip->{i2c_driver_addrs} and
1413                    not contains( $addr , @{$chip->{i2c_driver_addrs}});
1414          add_i2c_to_chips_detected $$chip{driver}, $new_hash;
1415        } else {
1416          print "Failed!\n";
1417        }
1418      }
1419    }
1420  }
1421}
1422
1423sub scan_isa_bus
1424{
1425  my ($chip,$addr,$conf);
1426  foreach $chip (@chip_ids) {
1427    next if not exists $$chip{isa_addrs} or not exists $$chip{isa_detect};
1428    print "Probing for `$$chip{name}'\n";
1429    foreach $addr (@{$$chip{isa_addrs}}) {
1430      if ($addr) {
1431        printf "  Trying address 0x%04x... ", $addr;
1432      } else {
1433        print "  Trying general detect... ";
1434      }
1435      $conf = &{$$chip{isa_detect}} ($addr);
1436      print("Failed!\n"), next if not defined $conf;
1437      print "Success!\n";
1438      printf "    (confidence %d, driver `%s')\n", $conf, $$chip{driver};
1439      my $new_hash = { conf => $conf,
1440                       isa_addr => $addr,
1441                       chipname =>  $$chip{name}
1442                     };
1443      $new_hash->{isa_extra} = 0 
1444             if exists $chip->{isa_driver_addrs} and
1445                not contains ($addr, @{$chip->{isa_driver_addrs}});
1446      $new_hash = add_isa_to_chips_detected $$chip{alias_detect},$$chip{driver},
1447                                            $new_hash;
1448      if ($new_hash) {
1449        printf "    Alias of the chip on I2C bus `%s', address 0x%04x\n",
1450                        $new_hash->{i2c_adap},$new_hash->{i2c_addr};
1451      }
1452    }
1453  }
1454}
1455
1456
1457##################
1458# CHIP DETECTION #
1459##################
1460
1461# Each function returns a confidence value. The higher this value, the more
1462# sure we are about this chip. A Winbond W83781D, for example, will be
1463# detected as a LM78 too; but as the Winbond detection has a higher confidence
1464# factor, you should identify it as a Winbond.
1465
1466# Each function returns a list. The first element is the confidence value;
1467# Each element after it is an SMBus address. In this way, we can detect
1468# chips with several SMBus addresses. The SMBus address for which the
1469# function was called is never returned.
1470
1471# If there are devices which get confused if they are only read from, then
1472# this program will surely confuse them. But we guarantee never to write to
1473# any of these devices.
1474
1475
1476# $_[0]: A reference to the file descriptor to access this chip.
1477#        We may assume an i2c_set_slave_addr was already done.
1478# $_[1]: Address
1479# Returns: undef if not detected, (7) if detected.
1480# Registers used: 0x58
1481sub mtp008_detect
1482{
1483  my ($file,$addr) = @_;
1484  return if (i2c_smbus_read_byte_data($file,0x58)) != 0xac;
1485  return (8);
1486}
1487 
1488# $_[0]: Chip to detect (0 = LM78, 1 = LM78-J, 2 = LM79)
1489# $_[1]: A reference to the file descriptor to access this chip.
1490#        We may assume an i2c_set_slave_addr was already done.
1491# $_[2]: Address
1492# Returns: undef if not detected, (7) if detected.
1493# Registers used:
1494#   0x40: Configuration
1495#   0x48: Full I2C Address
1496#   0x49: Device ID
1497# Note that this function is always called through a closure, so the
1498# arguments are shifted by one place.
1499sub lm78_detect
1500{
1501  my $reg;
1502  my ($chip,$file,$addr) = @_;
1503  return unless i2c_smbus_read_byte_data($file,0x48) == $addr;
1504  return unless (i2c_smbus_read_byte_data($file,0x40) & 0x80) == 0x00;
1505  $reg = i2c_smbus_read_byte_data($file,0x49);
1506  return unless ($chip == 0 and $reg == 0x00) or
1507                    ($chip == 1 and $reg == 0x40) or
1508                    ($chip == 2 and ($reg & 0xfe) == 0xc0);
1509  return (7);
1510}
1511
1512# $_[0]: Chip to detect (0 = LM78, 1 = LM78-J, 2 = LM79)
1513# $_[1]: Address
1514# Returns: undef if not detected, 7 if detected.
1515# Note: Only address 0x290 is scanned at this moment.
1516sub lm78_isa_detect
1517{
1518  my ($chip,$addr) = @_ ;
1519  my $val = inb ($addr + 1);
1520  return if inb ($addr + 2) != $val or inb ($addr + 3) != $val or 
1521            inb ($addr + 7) != $val;
1522
1523  $val = inb($addr + 5) & 0x7f;
1524  outb($addr+5,~ $val);
1525  if ((inb ($addr+5) & 0x7f) != (~ $val & 0x7f)) {
1526    outb($addr+5,$val);
1527    return;
1528  }
1529  my $readproc = sub { isa_read_byte $addr + 5, $addr + 6, @_ };
1530  return unless (&$readproc(0x40) & 0x80) == 0x00;
1531  my $reg = &$readproc(0x49);
1532  return unless ($chip == 0 and $reg == 0x00) or
1533                ($chip == 1 and $reg == 0x40) or
1534                ($chip == 2 and ($reg & 0xfe) == 0xc0);
1535  return 7;
1536}
1537
1538
1539# $_[0]: Chip to detect (0 = LM78, 1 = LM78-J, 2 = LM79)
1540# $_[1]: ISA address
1541# $_[2]: I2C file handle
1542# $_[3]: I2C address
1543sub lm78_alias_detect
1544{
1545  my ($chip,$isa_addr,$file,$i2c_addr) = @_;
1546  my $i;
1547  my $readproc = sub { isa_read_byte $isa_addr + 5, $isa_addr + 6, @_ };
1548  return 0 unless &$readproc(0x48) == $i2c_addr;
1549  for ($i = 0x2b; $i <= 0x3d; $i ++) {
1550    return 0 unless &$readproc($i) == i2c_smbus_read_byte_data($file,$i);
1551  }
1552  return 1;
1553}
1554
1555# $_[0]: A reference to the file descriptor to access this chip.
1556#        We may assume an i2c_set_slave_addr was already done.
1557# $_[1]: Address
1558# Returns: undef if not detected, (3) if detected.
1559# Registers used:
1560#   0x01: Configuration
1561#   0x02: Hysteresis
1562#   0x03: Overtemperature Shutdown
1563# Detection really sucks! It is only based on the fact that the LM75 has only
1564# four registers. Any other chip in the valid address range with only four
1565# registers will be detected too.
1566# Note that register $00 may change, so we can't use the modulo trick on it.
1567sub lm75_detect
1568{
1569  my $i;
1570  my ($file,$addr) = @_;
1571  my $cur = i2c_smbus_read_word_data($file,0x00);
1572  my $conf = i2c_smbus_read_byte_data($file,0x01);
1573  my $hyst = i2c_smbus_read_word_data($file,0x02);
1574  my $os = i2c_smbus_read_word_data($file,0x03);
1575  for ($i = 0x00; $i <= 0x1f; $i += 1) {
1576    return if i2c_smbus_read_byte_data($file,($i * 0x08) + 0x01) != $conf;
1577    return if i2c_smbus_read_word_data($file,($i * 0x08) + 0x02) != $hyst;
1578    return if i2c_smbus_read_word_data($file,($i * 0x08) + 0x03) != $os;
1579  }
1580  return (3);
1581}
1582 
1583# $_[0]: A reference to the file descriptor to access this chip.
1584#        We may assume an i2c_set_slave_addr was already done.
1585# $_[1]: Address
1586# Returns: undef if not detected, (3) if detected,
1587#   (6) or (9) if even more bits match.
1588# Registers used:
1589#   0xAC: Configuration
1590# Detection is weak. We check if Bit 3 is set and Bit 2 is clear.
1591# The DS1621 will aways have a config like 0x????10??. A even better
1592# match would be 0x0??01000.
1593sub ds1621_detect
1594{
1595  my $i;
1596  my ($file,$addr) = @_;
1597  my $conf = i2c_smbus_read_byte_data($file,0xAC);
1598  return (9) if ($conf & 0x9F) == 0x98;
1599  return (6) if ($conf & 0x0F) == 0x08;
1600  return (3) if ($conf & 0x0C) == 0x08;
1601  return ;
1602}
1603
1604# $_[0]: A reference to the file descriptor to access this chip.
1605#        We may assume an i2c_set_slave_addr was already done.
1606# $_[1]: Address
1607# Returns: undef if not detected, (3) if detected.
1608# Registers used:
1609# Registers used:
1610#   0x02: Interrupt state register
1611# How to detect this beast?
1612sub lm80_detect
1613{
1614  my $i;
1615  my ($file,$addr) = @_;
1616  return if (i2c_smbus_read_byte_data($file,0x02) & 0xc0) != 0;
1617  for ($i = 0x2a; $i <= 0x3d; $i++) {
1618    my $reg = i2c_smbus_read_byte_data($file,$i);
1619    return if i2c_smbus_read_byte_data($file,$i+0x40) != $reg;
1620    return if i2c_smbus_read_byte_data($file,$i+0x80) != $reg;
1621    return if i2c_smbus_read_byte_data($file,$i+0xc0) != $reg;
1622  }
1623  return (3);
1624}
1625 
1626# $_[0]: A reference to the file descriptor to access this chip.
1627#        We may assume an i2c_set_slave_addr was already done.
1628# $_[1]: Address
1629# Returns: undef if not detected, (7) if detected.
1630# Registers used: 0x3E, 0x3F
1631#        Assume lower 2 bits of reg 0x3F are for revisions.
1632sub lm87_detect
1633{
1634  my ($file,$addr) = @_;
1635  return if (i2c_smbus_read_byte_data($file,0x3e)) != 0x02;
1636  return if (i2c_smbus_read_byte_data($file,0x3f) & 0xfc) != 0x04;
1637  return (7);
1638}
1639 
1640# $_[0]: Chip to detect (0 = W83781D, 1 = W83782D, 2 = W83783S,
1641#                        3 = W83627HF, 4 = AS99127F)
1642# $_[1]: A reference to the file descriptor to access this chip.
1643#        We may assume an i2c_set_slave_addr was already done.
1644# $_[2]: Address
1645# Returns: undef if not detected, (8,addr1,addr2) if detected, but only
1646#          if the LM75 chip emulation is enabled.
1647# Registers used:
1648#   0x48: Full I2C Address
1649#   0x4a: I2C addresses of emulated LM75 chips
1650#   0x4e: Vendor ID byte selection, and bank selection
1651#   0x4f: Vendor ID
1652#   0x58: Device ID (only when in bank 0); ignore LSB.
1653# Note: Fails if the W8378xD is not in bank 0!
1654# Note: Detection overrules a previous LM78 detection
1655# Note: AS99127F address register 0x48 not supported?
1656sub w83781d_detect
1657{
1658  my ($reg1,$reg2,@res);
1659  my ($chip,$file,$addr) = @_;
1660  return unless (i2c_smbus_read_byte_data($file,0x48) == $addr)
1661    or ($chip == 4);
1662  $reg1 = i2c_smbus_read_byte_data($file,0x4e);
1663  $reg2 = i2c_smbus_read_byte_data($file,0x4f);
1664  if ($chip != 4) {
1665    return unless (($reg1 & 0x80) == 0x00 and $reg2 == 0xa3) or 
1666                  (($reg1 & 0x80) == 0x80 and $reg2 == 0x5c);
1667  }
1668  if ($chip == 4) {
1669    return unless (($reg1 & 0x80) == 0x00 and $reg2 == 0xc3) or 
1670                  (($reg1 & 0x80) == 0x80 and $reg2 == 0x12);
1671  }
1672  return unless ($reg1 & 0x07) == 0x00;
1673  $reg1 = i2c_smbus_read_byte_data($file,0x58) & 0xfe;
1674  return if $chip == 0 and  $reg1 != 0x10;
1675  return if $chip == 1 and  $reg1 != 0x30;
1676  return if $chip == 2 and  $reg1 != 0x40;
1677  return if $chip == 3 and  $reg1 != 0x20;
1678  return if $chip == 4 and  $reg1 != 0x30;
1679  $reg1 = i2c_smbus_read_byte_data($file,0x4a);
1680  @res = (8);
1681  push @res, ($reg1 & 0x07) + 0x48 unless $reg1 & 0x08 ;
1682  push @res, (($reg1 & 0x80) >> 4) + 0x48 unless ($reg1 & 0x80 or $chip == 2);
1683  return @res;
1684}
1685
1686# $_[0]: Chip to detect (0 = W83781D, 1 = W83782D, 2 = W83783S, 3 = W83627HF)
1687# $_[1]: ISA address
1688# $_[2]: I2C file handle
1689# $_[3]: I2C address
1690sub w83781d_alias_detect
1691{
1692  my ($chip,$isa_addr,$file,$i2c_addr) = @_;
1693  my $i;
1694  my $readproc = sub { isa_read_byte $isa_addr + 5, $isa_addr + 6, @_ };
1695  return 0 unless &$readproc(0x48) == $i2c_addr;
1696  for ($i = 0x2b; $i <= 0x3d; $i ++) {
1697    return 0 unless &$readproc($i) == i2c_smbus_read_byte_data($file,$i);
1698  }
1699  return 1;
1700}
1701
1702# $_[0]: Chip to detect (0 = W83781D, 1 = W83782D, 3 = W83627HF, 5 = W83697HF)
1703#        (W83783S and AS99127F not on ISA bus)
1704# $_[1]: Address
1705# Returns: undef if not detected, (8) if detected.
1706sub w83781d_isa_detect
1707{
1708  my ($chip,$addr) = @_ ;
1709  my ($reg1,$reg2);
1710  my $val = inb ($addr + 1);
1711  return if inb ($addr + 2) != $val or inb ($addr + 3) != $val or
1712            inb ($addr + 7) != $val;
1713
1714  $val = inb($addr + 5) & 0x7f;
1715  outb($addr+5,~ $val);
1716  if ((inb ($addr+5) & 0x7f) != (~ $val & 0x7f)) {
1717    outb($addr+5,$val);
1718    return;
1719  }
1720
1721  my $read_proc = sub { isa_read_byte $addr + 5, $addr + 6, @_ };
1722  $reg1 = &$read_proc(0x4e);
1723  $reg2 = &$read_proc(0x4f);
1724  return unless (($reg1 & 0x80) == 0x00 and $reg2 == 0xa3) or 
1725                (($reg1 & 0x80) == 0x80 and $reg2 == 0x5c);
1726  return unless ($reg1 & 0x07) == 0x00;
1727  $reg1 = &$read_proc(0x58) & 0xfe;
1728  return if $chip == 0 and  $reg1 != 0x10;
1729  return if $chip == 1 and  $reg1 != 0x30;
1730  return if $chip == 3 and  $reg1 != 0x20;
1731  return if $chip == 5 and  $reg1 != 0x60;
1732  return 8;
1733}
1734
1735# $_[0]: Chip to detect (0 = Revision 0x00, 1 = Revision 0x80)
1736# $_[1]: A reference to the file descriptor to access this chip.
1737#        We may assume an i2c_set_slave_addr was already done.
1738# $_[2]: Address
1739# Returns: undef if not detected, (6) if detected.
1740# Registers used:
1741#   0x00: Device ID
1742#   0x01: Revision ID
1743#   0x03: Configuration
1744# Mediocre detection
1745sub gl518sm_detect
1746{
1747  my $reg;
1748  my ($chip,$file,$addr) = @_;
1749  return unless i2c_smbus_read_byte_data($file,0x00) == 0x80;
1750  return unless (i2c_smbus_read_byte_data($file,0x03) & 0x80) == 0x00;
1751  $reg = i2c_smbus_read_byte_data($file,0x01);
1752  return unless ($chip == 0 and $reg == 0x00) or
1753                ($chip == 1 and $reg == 0x80);
1754  return (6);
1755}
1756
1757# $_[0]: A reference to the file descriptor to access this chip.
1758#        We may assume an i2c_set_slave_addr was already done.
1759# $_[1]: Address
1760# Returns: undef if not detected, (5) if detected.
1761# Registers used:
1762#   0x00: Device ID
1763#   0x01: Revision ID
1764#   0x03: Configuration
1765# Mediocre detection
1766sub gl520sm_detect
1767{
1768  my ($file,$addr) = @_;
1769  return unless i2c_smbus_read_byte_data($file,0x00) == 0x20;
1770  return unless (i2c_smbus_read_byte_data($file,0x03) & 0x80) == 0x00;
1771  # The line below must be better checked before I dare to use it.
1772  # return unless i2c_smbus_read_byte_data($file,0x01) == 0x00;
1773  return (5);
1774}
1775
1776# $_[0]: A reference to the file descriptor to access this chip.
1777#        We may assume an i2c_set_slave_addr was already done.
1778# $_[1]: Address
1779# Returns: undef if not detected, (5) if detected.
1780# Registers used:
1781#   0x00: Device ID
1782# Mediocre detection
1783sub gl525sm_detect
1784{
1785  my ($file,$addr) = @_;
1786  return unless i2c_smbus_read_byte_data($file,0x00) == 0x25;
1787  return (5);
1788}
1789
1790# $_[0]: Chip to detect (0 = ADM9240, 1 = DS1780, 2 = LM81)
1791# $_[1]: A reference to the file descriptor to access this chip.
1792#        We may assume an i2c_set_slave_addr was already done.
1793# $_[2]: Address
1794# Returns: undef if not detected, (7) if detected.
1795# Registers used:
1796#   0x3e: Company ID
1797#   0x40: Configuration
1798#   0x48: Full I2C Address
1799# Note: Detection overrules a previous LM78 detection
1800sub adm9240_detect
1801{
1802  my $reg;
1803  my ($chip, $file,$addr) = @_;
1804  $reg = i2c_smbus_read_byte_data($file,0x3e);
1805  return unless ($chip == 0 and $reg == 0x23) or
1806                ($chip == 1 and $reg == 0xda) or
1807                ($chip == 2 and $reg == 0x01);
1808  return unless (i2c_smbus_read_byte_data($file,0x40) & 0x80) == 0x00;
1809  return unless i2c_smbus_read_byte_data($file,0x48) == $addr;
1810 
1811  return (7);
1812}
1813
1814# $_[0]: Chip to detect (0 = ADM1022, 1 = THMC50)
1815# $_[1]: A reference to the file descriptor to access this chip.
1816#        We may assume an i2c_set_slave_addr was already done.
1817# $_[2]: Address
1818# Returns: undef if not detected, (8) if detected.
1819# Registers used:
1820#   0x3e: Company ID
1821#   0x3f: Revision
1822#   0x40: Configuration
1823# Note: Detection overrules a previous LM78 or ADM9240 detection
1824sub adm1022_detect
1825{
1826  my $reg;
1827  my ($chip, $file,$addr) = @_;
1828  $reg = i2c_smbus_read_byte_data($file,0x3e);
1829  return unless ($chip == 0 and $reg == 0x41) or
1830                ($chip == 1 and $reg == 0x49);
1831  return unless (i2c_smbus_read_byte_data($file,0x40) & 0x80) == 0x00;
1832  return unless (i2c_smbus_read_byte_data($file,0x3f) & 0xc0) == 0xc0;
1833  return (8);
1834}
1835
1836# $_[0]: Chip to detect (0 = ADM1025)
1837# $_[1]: A reference to the file descriptor to access this chip.
1838#        We may assume an i2c_set_slave_addr was already done.
1839# $_[2]: Address
1840# Returns: undef if not detected, (8) if detected.
1841# Registers used:
1842#   0x3e: Company ID
1843#   0x3f: Revision
1844#   0x40: Configuration
1845# Note: Detection overrules a previous LM78 or ADM9240 detection
1846sub adm1025_detect
1847{
1848  my $reg;
1849  my ($chip, $file,$addr) = @_;
1850  $reg = i2c_smbus_read_byte_data($file,0x3e);
1851  return unless ($reg == 0x41);
1852  return unless (i2c_smbus_read_byte_data($file,0x40) & 0x80) == 0x00;
1853  return unless (i2c_smbus_read_byte_data($file,0x3f) & 0xc0) == 0x20;
1854  return (8);
1855}
1856
1857# $_[0]: Chip to detect
1858#   (0 = ADM1021, 1 = MAX1617, 2 = MAX1617A, 3 = THMC10, 4 = LM84, 5 = GL523)
1859# $_[1]: A reference to the file descriptor to access this chip.
1860#        We may assume an i2c_set_slave_addr was already done.
1861# $_[2]: Address
1862# Returns: undef if not detected, (6) or (3) if detected.
1863# Registers used:
1864#   0x04: Company ID (LM84 only)
1865#   0xfe: Company ID
1866#   0xff: Revision (Maxim only)
1867#   0x02: Status
1868# Note: Especially the Maxim has very bad detection; we give it a low
1869# confidence value.
1870sub adm1021_detect
1871{
1872  my $reg;
1873  my ($chip, $file,$addr) = @_;
1874  return if $chip == 0 and i2c_smbus_read_byte_data($file,0xfe) != 0x41;
1875  return if $chip == 3 and i2c_smbus_read_byte_data($file,0xfe) != 0x49;
1876  return if $chip == 4 and i2c_smbus_read_byte_data($file,0x04) != 0x00;
1877  return if $chip == 5 and i2c_smbus_read_byte_data($file,0xfe) != 0x23;
1878  return if $chip == 2 and i2c_smbus_read_byte_data($file,0xfe) != 0x4d and
1879                           i2c_smbus_read_byte_data($file,0xff) != 0x01;
1880  # The remaining things are flaky at best. Perhaps something can be done
1881  # with the fact that some registers are unreadable?
1882  return if (i2c_smbus_read_byte_data($file,0x02) & 0x03) != 0;
1883  if ($chip == 1) {
1884    return (3);
1885  } else {
1886    return (6);
1887  }
1888}
1889
1890# $_[0]: Address
1891# Returns: undef if not detected, (9) if detected.
1892# Note: It is already 99% certain this chip exists if we find the PCI
1893# entry. The exact address is encoded in PCI space.
1894sub sis5595_isa_detect
1895{
1896  my ($addr) = @_;
1897  my ($adapter,$try,$local_try);
1898  my $found = 0;
1899  foreach $local_try (@pci_adapters) {
1900    if ($local_try->{procid} eq "Silicon Integrated Systems SIS5595") {
1901      $try = $local_try;
1902      $found = 1;
1903      last;
1904    }
1905  }
1906  return if not $found;
1907
1908  $found = 0;
1909  foreach $adapter (@pci_list) {
1910    if ((defined($adapter->{vendid}) and 
1911         $try->{vendid} == $adapter->{vendid} and
1912         $try->{devid} == $adapter->{devid} and
1913         $try->{func} == $adapter->{func}) or
1914        (! defined($adapter->{vendid}) and
1915         $adapter->{desc} =~ /$try->{procid}/ and
1916         $try->{func} == $adapter->{func})) {
1917      $found = 1;
1918      last;
1919    }
1920  }
1921  return if not $found;
1922
1923  return 9;
1924}
1925
1926# $_[0]: Address
1927# Returns: undef if not detected, (9) if detected.
1928# Note: It is already 99% certain this chip exists if we find the PCI
1929# entry. The exact address is encoded in PCI space.
1930sub via686a_isa_detect
1931{
1932  my ($addr) = @_;
1933  my ($adapter,$try,$local_try);
1934  my $found = 0;
1935  foreach $local_try (@pci_adapters) {
1936    if ($local_try->{procid} eq "VIA Technologies VT 82C686 Apollo ACPI") {
1937      $try = $local_try;
1938      $found = 1;
1939      last;
1940    }
1941  }
1942  return if not $found;
1943
1944  $found = 0;
1945  foreach $adapter (@pci_list) {
1946    if ((defined($adapter->{vendid}) and 
1947         $try->{vendid} == $adapter->{vendid} and
1948         $try->{devid} == $adapter->{devid} and
1949         $try->{func} == $adapter->{func}) or
1950        (! defined($adapter->{vendid}) and
1951         $adapter->{desc} =~ /$try->{procid}/ and
1952         $try->{func} == $adapter->{func})) {
1953      $found = 1;
1954      last;
1955    }
1956  }
1957  return if not $found;
1958
1959  return 9;
1960}
1961
1962# $_[0]: Chip to detect (0 = ..., 1 = ...)
1963# $_[1]: A reference to the file descriptor to access this chip.
1964#        We may assume an i2c_set_slave_addr was already done.
1965# $_[2]: Address
1966# Returns: undef if not detected, (7) if detected.
1967# Registers used:
1968#   0x40: Configuration
1969#   0x48: Full I2C Address
1970#   0x58: Mfr ID
1971# Note that this function is always called through a closure, so the
1972# arguments are shifted by one place.
1973sub ite_detect
1974{
1975  my $reg;
1976  my ($chip,$file,$addr) = @_;
1977  return unless i2c_smbus_read_byte_data($file,0x48) == $addr;
1978  return unless (i2c_smbus_read_byte_data($file,0x40) & 0x80) == 0x00;
1979  return unless i2c_smbus_read_byte_data($file,0x58) == 0x90;
1980  return (7);
1981}
1982
1983# $_[0]: Chip to detect (0 = ..., 1 = ...)
1984# $_[1]: Address
1985# Returns: undef if not detected, 7 if detected.
1986# Note: Only address 0x290 is scanned at this moment.
1987sub ite_isa_detect
1988{
1989  my ($chip,$addr) = @_ ;
1990  my $val = inb ($addr + 1);
1991  return if inb ($addr + 2) != $val or inb ($addr + 3) != $val or 
1992            inb ($addr + 7) != $val;
1993
1994  $val = inb($addr + 5) & 0x7f;
1995  outb($addr+5,~ $val);
1996  if ((inb ($addr+5) & 0x7f) != (~ $val & 0x7f)) {
1997    outb($addr+5,$val);
1998    return;
1999  }
2000  my $readproc = sub { isa_read_byte $addr + 5, $addr + 6, @_ };
2001  return unless (&$readproc(0x40) & 0x80) == 0x00;
2002  my $reg = &$readproc(0x58);
2003  return unless ($reg == 0x90);
2004  return 7;
2005}
2006
2007
2008# $_[0]: Chip to detect (0 = ..., 1 = ...)
2009# $_[1]: ISA address
2010# $_[2]: I2C file handle
2011# $_[3]: I2C address
2012sub ite_alias_detect
2013{
2014  my ($chip,$isa_addr,$file,$i2c_addr) = @_;
2015  my $i;
2016  my $readproc = sub { isa_read_byte $isa_addr + 5, $isa_addr + 6, @_ };
2017  return 0 unless &$readproc(0x48) == $i2c_addr;
2018  for ($i = 0x2b; $i <= 0x3d; $i ++) {
2019    return 0 unless &$readproc($i) == i2c_smbus_read_byte_data($file,$i);
2020  }
2021  return 1;
2022}
2023
2024# $_[0]: A reference to the file descriptor to access this chip.
2025#        We may assume an i2c_set_slave_addr was already done.
2026# $_[1]: Address
2027# Returns: undef if not detected, (5) if detected.
2028# Registers used:
2029#   0x00-0x63: PC-100 Data and Checksum
2030sub eeprom_detect
2031{
2032  my ($file,$addr) = @_;
2033  # Check the checksum for validity (only works for PC-100 DIMMs)
2034  my $checksum = 0;
2035  for (my $i = 0; $i <= 62; $i = $i + 1) {
2036    $checksum = $checksum + i2c_smbus_read_byte_data($file,$i);
2037  }
2038  $checksum=$checksum & 255;
2039  if (i2c_smbus_read_byte_data($file,63) == $checksum) {
2040        return (8);
2041  }
2042  # Even if checksum test fails, it still may be an eeprom
2043  return (1);
2044}
2045
2046# $_[0]: A reference to the file descriptor to access this chip.
2047#        We may assume an i2c_set_slave_addr was already done.
2048# $_[1]: Address
2049# Returns: undef if not detected, (1) if detected.
2050# Detection is impossible!
2051sub ltc1710_detect
2052{
2053  return (1);
2054}
2055
2056# $_[0]: A reference to the file descriptor to access this chip.
2057#        We may assume an i2c_set_slave_addr was already done.
2058# $_[1]: Address
2059# Returns: undef if not detected, (1) if detected.
2060# Registers used:
2061#   0x00..0x07: DDC signature
2062#   0x08..0x7E: checksumed area
2063#   0x7F:       checksum
2064### commented out additional location checks for now - don't work?
2065sub ddcmonitor_detect
2066{
2067  my ($file,$addr) = @_;
2068  my $i;
2069###  for ($i = 0; $i < 8; $i ++) {
2070###    i2c_set_slave_addr \*FILE,$addr+$i or goto FAILURE;
2071    i2c_smbus_read_byte_data($file,0x00) == 0x00 or goto FAILURE;
2072    i2c_smbus_read_byte_data($file,0x01) == 0xFF or goto FAILURE;
2073    i2c_smbus_read_byte_data($file,0x02) == 0xFF or goto FAILURE;
2074    i2c_smbus_read_byte_data($file,0x03) == 0xFF or goto FAILURE;
2075    i2c_smbus_read_byte_data($file,0x04) == 0xFF or goto FAILURE;
2076    i2c_smbus_read_byte_data($file,0x05) == 0xFF or goto FAILURE;
2077    i2c_smbus_read_byte_data($file,0x06) == 0xFF or goto FAILURE;
2078    i2c_smbus_read_byte_data($file,0x07) == 0x00 or goto FAILURE;
2079###  }
2080###  i2c_set_slave_addr \*FILE,$addr or return;
2081  # Check the checksum for validity. We should do this for all addresses,
2082  # but it would be too slow.
2083  my $checksum = 0;
2084  for ($i = 0; $i <= 127; $i = $i + 1) {
2085    $checksum = $checksum + i2c_smbus_read_byte_data($file,$i);
2086  }
2087  $checksum=$checksum & 255;
2088  if ($checksum != 0) {
2089    # I have one such monitor...
2090    return (2,$addr+1,$addr+2,$addr+3,$addr+4,$addr+5,$addr+6,$addr+7);
2091  }
2092  return (8,$addr+1,$addr+2,$addr+3,$addr+4,$addr+5,$addr+6,$addr+7);
2093FAILURE:
2094  i2c_set_slave_addr \*FILE,$addr;
2095  return;
2096}
2097
2098################
2099# MAIN PROGRAM #
2100################
2101
2102# $_[0]: reference to a list of chip hashes
2103sub print_chips_report 
2104{
2105  my ($listref) = @_;
2106  my $data;
2107 
2108  foreach $data (@$listref) {
2109    my $is_i2c = exists $data->{i2c_addr};
2110    my $is_isa = exists $data->{isa_addr};
2111    print "  * ";
2112    if ($is_i2c) {
2113      printf "Bus `%s' (%s)\n", $data->{i2c_adap}, $data->{i2c_algo};
2114      printf "    Busdriver `%s', I2C address 0x%02x", 
2115             $data->{i2c_driver}, $data->{i2c_addr};
2116      if (exists $data->{i2c_sub_addrs}) {
2117        print " (and";
2118        my $sub_addr;
2119        foreach $sub_addr (@{$data->{i2c_sub_addrs}}) {
2120          printf " 0x%02x",$sub_addr;
2121        }
2122        print ")"
2123      }
2124      print "\n";
2125    }
2126    if ($is_isa) {
2127      print "    " if  $is_i2c;
2128      if ($data->{isa_addr}) {
2129        printf "ISA bus address 0x%04x (Busdriver `i2c-isa')\n", 
2130               $data->{isa_addr};
2131      } else {
2132        printf "ISA bus, undetermined address (Busdriver `i2c-isa')\n"
2133      }
2134    }
2135    printf "    Chip `%s' (confidence: %d)\n",
2136           $data->{chipname},  $data->{conf};
2137  }
2138}
2139
2140# $_[0]: 1 if ISA bus is prefered, 0 for SMBus
2141# We build here an array adapters, indexed on the number the adapter has
2142# at this moment (we assume only loaded adapters are interesting at all;
2143# everything that got scanned also got loaded). Each entry is a reference
2144# to a hash containing:
2145#  driver: Name of the adapter driver
2146#  nr_now: Number of the bus now
2147#  nr_later: Number of the bus when the modprobes are done (not included if the
2148#        driver should not be loaded)
2149# A second array, called
2150sub generate_modprobes
2151{
2152  my ($prefer_isa) = @_;
2153
2154  my ($chip,$detection,$nr,$i,@optionlist,@probelist,$driver,$isa,$adap);
2155  my @adapters;
2156  my $modprobes = "";
2157  my $configfile = "";
2158
2159  # These are always needed
2160  $configfile .= "# I2C module options\n";
2161  $configfile .= "alias char-major-89 i2c-dev\n";
2162
2163  # Collect all loaded adapters
2164  open INPUTFILE,"/proc/bus/i2c" or die "Couldn't open /proc/bus/i2c?!?";
2165  while (<INPUTFILE>) {
2166    my ($dev_nr,$type,$adap,$algo) = /^i2c-(\S+)\s+(\S+)\s+(.*?)\s*\t\s*(.*?)\s+$/;
2167    next if ($type eq "dummy");
2168    $adapters[$dev_nr]->{driver} = find_adapter_driver($adap,$algo);
2169    $adapters[$dev_nr]->{adapname} = $adap;
2170    $adapters[$dev_nr]->{algoname} = $algo;
2171  }
2172  close INPUTFILE;
2173
2174  # Collect all adapters used
2175  $nr = 0;
2176  $isa = 0;
2177  $modprobes .= "# I2C adapter drivers\n";
2178  foreach $chip (@chips_detected) {
2179    foreach $detection (@{$chip->{detected}}) {
2180      # If there is more than one bus detected by a driver, they are
2181      # still all added. So we number them in the correct order
2182      if (exists $detection->{i2c_driver} and
2183          not exists $adapters[$detection->{i2c_devnr}]->{nr_later} and 
2184          not (exists $detection->{isa_addr} and $prefer_isa)) {
2185         foreach $adap (@adapters) {
2186           $adap->{nr_later} = $nr++ if $adap->{driver} eq $detection->{i2c_driver};
2187         }
2188      }
2189      if (exists $detection->{isa_addr} and
2190          not (exists $detection->{i2c_driver} and not $prefer_isa)) {
2191           $isa=1;
2192      }
2193    }
2194  }
2195
2196  for ($i = 0; $i < $nr; $i++) {
2197    foreach $adap (@adapters) {
2198      if ($adap->{driver} eq "UNKNOWN") {
2199        $modprobes .= "# modprobe unknown adapter ".$adap->{adapname}." using ". $adap->{algoname}."\n";
2200      } else {
2201        $modprobes .= "modprobe $adap->{driver}\n" if (defined($adap->{nr_later}) and $adap->{nr_later} == $i) and not $modprobes =~ /modprobe $adap->{driver}\n/;
2202      }
2203    }
2204  }
2205  $modprobes .= "modprobe i2c-isa\n" if ($isa);
2206
2207  # Now determine the chip probe lines
2208  $modprobes .= "# I2C chip drivers\n";
2209  foreach $chip (@chips_detected) {
2210    next if not @{$chip->{detected}};
2211    $modprobes .= "modprobe $chip->{driver}\n";
2212    @optionlist = ();
2213    @probelist = ();
2214
2215    # Handle detects at addresses normally not probed
2216    foreach $detection (@{$chip->{detected}}) {
2217      push @probelist, $adapters[$detection->{i2c_devnr}]->{nr_later},
2218                       $detection->{i2c_addr}
2219           if exists $detection->{i2c_addr} and
2220              exists $detection->{i2c_extra};
2221      push @probelist, -1, $detection->{isa_addr}
2222           if exists $detection->{isa_addr} and
2223              exists $detection->{isa_extra};
2224    }
2225
2226    # Handle misdetects
2227    foreach $detection (@{$chip->{misdetected}}) {
2228      push @optionlist, $adapters[$detection->{i2c_devnr}]->{nr_later},
2229                       $detection->{i2c_addr}
2230           if exists $detection->{i2c_addr} and
2231              exists $adapters[$detection->{i2c_devnr}]->{nr_later};
2232      push @optionlist, -1, $detection->{isa_addr}
2233           if exists $detection->{isa_addr} and $isa;
2234    }
2235
2236    # Handle aliases
2237    foreach $detection (@{$chip->{detected}}) {
2238      if (exists $detection->{i2c_driver} and 
2239          exists $detection->{isa_addr} and
2240          exists $adapters[$detection->{i2c_devnr}]->{nr_later} and
2241          $isa) {
2242        if ($prefer_isa) {
2243          push @optionlist,$adapters[$detection->{i2c_devnr}]->{nr_later},
2244                           $detection->{i2c_addr};
2245        } else {
2246          push @optionlist, -1, $detection->{isa_addr}
2247        }
2248      }
2249    }
2250
2251    next if not (@probelist or @optionlist);
2252    $configfile .= "options $chip->{driver}";
2253    $configfile .= sprintf " ignore=%d,0x%02x",shift @optionlist, 
2254                                               shift @optionlist
2255                  if @optionlist;
2256    $configfile .= sprintf ",%d,0x%02x",shift @optionlist, shift @optionlist
2257                  while @optionlist;
2258    $configfile .= sprintf " probe=%d,0x%02x",shift @probelist,
2259                                              shift @probelist
2260                  if @probelist;
2261    $configfile .= sprintf ",%d,0x%02x",shift @probelist, shift @probelist
2262                  while @probelist;
2263    $configfile .= "\n";
2264  }
2265
2266  return ($modprobes,$configfile);
2267 
2268}
2269
2270sub main
2271{
2272  my (@adapters,$res,$did_adapter_detection,$detect_others,$adapter);
2273
2274  initialize_proc_pci;
2275  initialize_modules_list;
2276
2277  print " This program will help you to determine which I2C/SMBus modules you ",
2278        "need to\n",
2279        " load to use lm_sensors most effectively.\n";
2280  print " You need to have done a `make install', issued a `depmod -a' and ",
2281        "made sure\n",
2282        " `/etc/conf.modules' (or `/etc/modules.conf') contains the ",
2283        "appropriate\n",
2284        " module path before you can use some functions of this utility. ",
2285        "Read\n",
2286        " doc/modules for more information.\n";
2287  print " Also, you need to be `root', or at least have access to the ",
2288        "/dev/i2c[-]* files\n",
2289        " for some things. You can use prog/mkdev/mkdev.sh to create these ",
2290        "/dev files\n",
2291        " if you do not have them already.\n";
2292  print " If you have patched your kernel and have some drivers built-in ",
2293        "you can\n",
2294        " safely answer NO if asked to load some modules. In this case, ",
2295        "things may\n",
2296        " seem a bit confusing, but they will still work.\n\n";
2297
2298  print " We can start with probing for (PCI) I2C or SMBus adapters.\n";
2299  print " You do not need any special privileges for this.\n";
2300  print " Do you want to probe now? (YES/no): ";
2301  @adapters = adapter_pci_detection
2302                        if ($did_adapter_detection = not <STDIN> =~ /\s*[Nn]/);
2303
2304  print "\n";
2305
2306  if (not $did_adapter_detection) {
2307    print " As you skipped adapter detection, we will only scan already ",
2308          "loaded adapter\n",
2309          " modules. You can still be prompted for non-detectable adapters.\n",
2310          " Do you want to? (yes/NO): ";
2311    $detect_others = <STDIN> =~ /^\s*[Yy]/;
2312  } elsif ($> != 0) {
2313    print " As you are not root, we can't load adapter modules. We will only ",
2314          "scan\n",
2315          " already loaded adapters.\n";
2316    $detect_others = 0;
2317  } else {
2318    print " We will now try to load each adapter module in turn.\n";
2319    foreach $adapter (@adapters) {
2320      if (contains $adapter, @modules_list) {
2321        print "Module `$adapter' already loaded.\n";
2322      } else {
2323        print "Load `$adapter' (say NO if built into your kernel)? (YES/no): ";
2324        unless (<STDIN> =~ /^\s*[Nn]/) {
2325          if (system ("modprobe", $adapter)) {
2326            print "Loading failed ($!)... skipping.\n";
2327            if ($adapter eq "i2c-riva") {
2328              print "** Note: i2c-riva module is available at \n";
2329              print "** http://drama.obuda.kando.hu/~fero/cgi-bin/rivatv.shtml\n"; 
2330            }
2331          } else {
2332            print "Module loaded succesfully.\n";
2333          }
2334        }
2335      }
2336    }
2337    print " Do you now want to be prompted for non-detectable adapters? ",
2338          "(yes/NO): ";
2339    $detect_others = <STDIN> =~ /^\s*[Yy]/ ;
2340  }
2341
2342  if ($detect_others) {
2343    foreach $adapter (@undetectable_adapters) {
2344      print "Load `$adapter' (say NO if built into your kernel)? (YES/no): ";
2345      unless (<STDIN> =~ /^\s*[Nn]/) {
2346        if (system ("modprobe", $adapter)) {
2347          print "Loading failed ($!)... skipping.\n";
2348        } else {
2349          print "Module loaded succesfully.\n";
2350        }
2351      }
2352    }
2353  }
2354
2355  print " To continue, we need module `i2c-dev' to be loaded.\n";
2356  print " If it is built-in into your kernel, you can safely skip this.\n";
2357  if (contains "i2c-dev", @modules_list) {
2358    print "i2c-dev is already loaded.\n";
2359  } else {
2360    if ($> != 0) {
2361      print " i2c-dev is not loaded. As you are not root, we will just hope ",
2362            "you edited\n",
2363            " `/etc/conf.modules' (or `/etc/modules.conf') for automatic ",
2364            "loading of\n",
2365            " this module. If not, you won't be able to open any /dev/i2c[-]* ",
2366            "file.\n";
2367    } else {
2368      print " i2c-dev is not loaded. Do you want to load it now? (YES/no): ";
2369      if (<STDIN> =~ /^\s*[Nn]/) {
2370        print " Well, you will know best. We will just hope you edited ",
2371              "`/etc/conf.modules'\n",
2372              " (or `/etc/modules.conf') for automatic loading of this ",
2373              "module. If not,\n",
2374              " you won't be able to open any /dev/i2c[-]* file (unless you",
2375              "have it built-in\n",
2376              " into your kernel)\n";
2377      } elsif (system "modprobe","i2c-dev") {
2378        print " Loading failed ($!), expect problems later on.\n";
2379      } else {
2380        print " Module loaded succesfully.\n";
2381      }
2382    }
2383  }
2384
2385  print "\n We are now going to do the adapter probings. Some adapters may ",
2386        "hang halfway\n",
2387        " through; we can't really help that. Also, some chips will be double ",
2388        "detected;\n",
2389        " we choose the one with the highest confidence value in that case.\n",
2390        " If you found that the adapter hung after probing a certain address, ",
2391        "you can\n",
2392        " specify that address to remain unprobed. If you have a PIIX4, that ",
2393        "often\n",
2394        " includes addresses 0x69 and/or 0x6a.\n";
2395
2396  my ($inp,@not_to_scan,$inp2);
2397  open INPUTFILE,"/proc/bus/i2c" or die "Couldn't open /proc/bus/i2c?!?";
2398  while (<INPUTFILE>) {
2399    my ($dev_nr,$type,$adap,$algo) = /^i2c-(\S+)\s+(\S+)\s+(.*?)\s*\t\s*(.*?)\s+$/;
2400    next if ($type eq "dummy");
2401    print "\n";
2402    print "Next adapter: $adap ($algo)\n";
2403    print "Do you want to scan it? (YES/no/selectively): ";
2404   
2405    $inp = <STDIN>;
2406    @not_to_scan=();
2407    if ($inp =~ /^\s*[Ss]/) {
2408      print "Please enter one or more addresses not to scan. Separate them ",
2409            "with comma's.\n",
2410            "You can specify a range by using dashes. Addresses may be ",
2411            "decimal (like 54)\n",
2412            "or hexadecimal (like 0x33).\n",
2413            "Addresses: ";
2414      $inp2 = <STDIN>;
2415      chop $inp2;
2416      @not_to_scan = parse_not_to_scan 0,0x7f,$inp2;
2417    }
2418    scan_adapter $dev_nr, $adap, $algo, find_adapter_driver($adap,$algo),
2419                 \@not_to_scan   unless $inp =~ /^\s*[Nn]/;
2420  }
2421
2422  print "\n Some chips are also accessible through the ISA bus. ISA probes ",
2423        "are\n",
2424        " typically a bit more dangerous, as we have to write to I/O ports ",
2425        "to do\n",
2426        " this. ";
2427  if ($> != 0) {
2428    print "As you are not root, we shall skip this step.\n";
2429  } else {
2430    print " Do you want to scan the ISA bus? (YES/no): ";
2431    if (not <STDIN> =~ /^\s*[Nn]/) {
2432      initialize_ioports or die "Sorry, can't access /dev/port ($!)?!?";
2433      scan_isa_bus;
2434    }
2435  }
2436
2437  print "\n Now follows a summary of the probes I have just done.\n";
2438  print " Just press ENTER to continue: ";
2439  <STDIN>;
2440
2441  my ($chip,$data);
2442  foreach $chip (@chips_detected) {
2443    print "\nDriver `$$chip{driver}' ";
2444    if (@{$$chip{detected}}) {
2445      if (@{$$chip{misdetected}}) {
2446        print "(should be inserted but causes problems):\n";
2447      } else {
2448        print "(should be inserted):\n";
2449      }
2450    } else {
2451      if (@{$$chip{misdetected}}) {
2452        print "(may not be inserted):\n";
2453      } else {
2454        print "(should not be inserted, but is harmless):\n";
2455      }
2456    }
2457    if (@{$$chip{detected}}) {
2458      print "  Detects correctly:\n";
2459      print_chips_report $chip->{detected};
2460    }
2461    if (@{$$chip{misdetected}}) {
2462      print "  Misdetects:\n";
2463      print_chips_report $chip->{misdetected};
2464    }
2465  }
2466
2467  print "\n\n",
2468        " I will now generate the commands needed to load the I2C modules.\n",
2469        " Sometimes, a chip is available both through the ISA bus and an ",
2470        "I2C bus.\n",
2471        " ISA bus access is faster, but you need to load an additional driver ",
2472        "module\n",
2473        " for it. If you have the choice, do you want to use the ISA bus or ",
2474        "the\n",
2475        " I2C/SMBus (ISA/smbus)? ";
2476  my $use_isa = not <STDIN> =~ /\s*[Ss]/;
2477     
2478  my ($modprobes,$configfile) = generate_modprobes $use_isa;
2479  print "\nWARNING! If you have some things built into your kernel, the \n",
2480        "below list will contain too many modules. Skip the appropriate ones!";
2481  print "\nTo load everything that is needed, add this to some /etc/rc* ",
2482        "file:\n\n";
2483  print "#----cut here----\n";
2484  print $modprobes;
2485  print "#----cut here----\n";
2486  print "\nTo make the sensors modules behave correctly, add these lines to ",
2487        "either\n",
2488        "/etc/modules.conf or /etc/conf.modules:\n\n";
2489  print "#----cut here----\n";
2490  print $configfile;
2491  print "#----cut here----\n";
2492 
2493}
2494
2495main;
Note: See TracBrowser for help on using the browser.