root/lm-sensors/branches/lm-sensors-3.0.0/prog/detect/sensors-detect @ 5410

Revision 5410, 172.5 KB (checked in by khali, 5 years ago)

Update an obsolete comment.

  • 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#    sensors-detect - Detect hardware monitoring chips
4#    Copyright (C) 1998 - 2002  Frodo Looijaard <frodol@dds.nl>
5#    Copyright (C) 2004 - 2008  Jean Delvare <khali@linux-fr.org>
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., 51 Franklin Street, Fifth Floor, Boston,
20#    MA 02110-1301 USA.
21#
22
23require 5.004;
24
25use strict;
26use Fcntl;
27use POSIX;
28use File::Basename;
29
30# We will call modprobe, which typically lives in either /sbin,
31# /usr/sbin or /usr/local/bin. So make sure these are all in the PATH.
32foreach ('/usr/sbin', '/usr/local/sbin', '/sbin') {
33        $ENV{PATH} = "$_:".$ENV{PATH}
34                unless $ENV{PATH} =~ m/(^|:)$_\/?(:|$)/;
35}
36
37#########################
38# CONSTANT DECLARATIONS #
39#########################
40
41use constant NO_CACHE => 1;
42use vars qw(@pci_adapters @chip_ids $i2c_addresses_to_scan @superio_ids
43            @cpu_ids $revision @i2c_byte_cache);
44
45$revision = '$Revision$ ($Date$)';
46$revision =~ s/\$\w+: (.*?) \$/$1/g;
47$revision =~ s/ \([^()]*\)//;
48
49# This is the list of SMBus or I2C adapters we recognize by their PCI
50# signature. This is an easy and fast way to determine which SMBus or I2C
51# adapters should be present.
52# Each entry must have a vendid (Vendor ID), devid (Device ID) and
53# procid (Device name) and driver (Device driver).
54@pci_adapters = (
55     {
56       vendid => 0x8086,
57       devid  => 0x7113,
58       procid => "Intel 82371AB PIIX4 ACPI",
59       driver => "i2c-piix4",
60     },
61     {
62       vendid => 0x8086,
63       devid  => 0x7603,
64       procid => "Intel 82372FB PIIX5 ACPI",
65       driver => "to-be-tested",
66     },
67     {
68       vendid => 0x8086,
69       devid  => 0x719b,
70       procid => "Intel 82443MX Mobile",
71       driver => "i2c-piix4",
72     },
73     {
74       vendid => 0x8086,
75       devid  => 0x2413,
76       procid => "Intel 82801AA ICH",
77       driver => "i2c-i801",
78     },
79     {
80       vendid => 0x8086,
81       devid  => 0x2423,
82       procid => "Intel 82801AB ICH0",
83       driver => "i2c-i801",
84     },
85     {
86       vendid => 0x8086,
87       devid  => 0x2443,
88       procid => "Intel 82801BA ICH2",
89       driver => "i2c-i801",
90     },
91     {
92       vendid => 0x8086,
93       devid  => 0x2483,
94       procid => "Intel 82801CA/CAM ICH3",
95       driver => "i2c-i801",
96     },
97     {
98       vendid => 0x8086,
99       devid  => 0x24C3,
100       procid => "Intel 82801DB ICH4",
101       driver => "i2c-i801",
102     },
103     {
104       vendid => 0x8086,
105       devid  => 0x24D3,
106       procid => "Intel 82801EB ICH5",
107       driver => "i2c-i801",
108     },
109     {
110       vendid => 0x8086,
111       devid  => 0x25A4,
112       procid => "Intel 6300ESB",
113       driver => "i2c-i801",
114     },
115     {
116       vendid => 0x8086,
117       devid  => 0x269B,
118       procid => "Intel Enterprise Southbridge - ESB2",
119       driver => "i2c-i801",
120     },
121     {
122       vendid => 0x8086,
123       devid  => 0x266A,
124       procid => "Intel 82801FB ICH6",
125       driver => "i2c-i801",
126     },
127     {
128       vendid => 0x8086,
129       devid  => 0x27DA,
130       procid => "Intel 82801G ICH7",
131       driver => "i2c-i801",
132     },
133     {
134       vendid => 0x8086,
135       devid  => 0x283E,
136       procid => "Intel 82801H ICH8",
137       driver => "i2c-i801",
138     },
139     {
140       vendid => 0x8086,
141       devid  => 0x2930,
142       procid => "Intel ICH9",
143       driver => "i2c-i801",
144     },
145     {
146       vendid => 0x8086,
147       devid  => 0x5032,
148       procid => "Intel Tolapai",
149       driver => "i2c-i801",
150     },
151     {
152       vendid => 0x8086,
153       devid  => 0x3A30,
154       procid => "Intel ICH10",
155       driver => "i2c-i801",
156     },
157     {
158       vendid => 0x8086,
159       devid  => 0x3A60,
160       procid => "Intel ICH10",
161       driver => "i2c-i801",
162     },
163     {
164       vendid => 0x8086,
165       devid  => 0x8119,
166       procid => "Intel SCH",
167       driver => "i2c-isch",
168     },
169     {
170       vendid => 0x1106,
171       devid  => 0x3040,
172       procid => "VIA Technologies VT82C586B Apollo ACPI",
173       driver => "i2c-via",
174     },
175     {
176       vendid => 0x1106,
177       devid  => 0x3050,
178       procid => "VIA Technologies VT82C596 Apollo ACPI",
179       driver => "i2c-viapro",
180     },
181     {
182       vendid => 0x1106,
183       devid  => 0x3051,
184       procid => "VIA Technologies VT82C596B ACPI",
185       driver => "i2c-viapro",
186     },
187     {
188       vendid => 0x1106,
189       devid  => 0x3057,
190       procid => "VIA Technologies VT82C686 Apollo ACPI",
191       driver => "i2c-viapro",
192     },
193     {
194       vendid => 0x1106,
195       devid  => 0x3074,
196       procid => "VIA Technologies VT8233 VLink South Bridge",
197       driver => "i2c-viapro",
198     },
199     {
200       vendid => 0x1106,
201       devid  => 0x3147,
202       procid => "VIA Technologies VT8233A South Bridge",
203       driver => "i2c-viapro",
204     },
205     {
206       vendid => 0x1106,
207       devid  => 0x3177,
208       procid => "VIA Technologies VT8233A/8235 South Bridge",
209       driver => "i2c-viapro",
210     },
211     {
212       vendid => 0x1106,
213       devid  => 0x3227,
214       procid => "VIA Technologies VT8237 South Bridge",
215       driver => "i2c-viapro",
216     },
217     {
218       vendid => 0x1106,
219       devid  => 0x3337,
220       procid => "VIA Technologies VT8237A South Bridge",
221       driver => "i2c-viapro",
222     },
223     {
224       vendid => 0x1106,
225       devid  => 0x8235,
226       procid => "VIA Technologies VT8231 South Bridge",
227       driver => "i2c-viapro",
228     },
229     {
230       vendid => 0x1106,
231       devid  => 0x3287,
232       procid => "VIA Technologies VT8251 South Bridge",
233       driver => "i2c-viapro",
234     },
235     {
236       vendid => 0x1106,
237       devid  => 0x8324,
238       procid => "VIA Technologies CX700 South Bridge",
239       driver => "i2c-viapro",
240     },
241     {
242       vendid => 0x1106,
243       devid  => 0x8353,
244       procid => "VIA Technologies VX800/VX820 South Bridge",
245       driver => "i2c-viapro",
246     },
247     {
248       vendid => 0x1039,
249       devid  => 0x0630,
250       procid => "Silicon Integrated Systems SIS630",
251       driver => "i2c-sis630",
252     },
253     {
254       vendid => 0x1039,
255       devid  => 0x0730,
256       procid => "Silicon Integrated Systems SIS730",
257       driver => "i2c-sis630",
258     },
259#
260# Both Ali chips below have same PCI ID. Can't be helped. Only one should load.
261#
262     {
263       vendid => 0x10b9,
264       devid => 0x7101,
265       procid => "Acer Labs 1533/1543",
266       driver => "i2c-ali15x3",
267     },
268     {
269       vendid => 0x10b9,
270       devid => 0x7101,
271       procid => "Acer Labs 1535",
272       driver => "i2c-ali1535",
273     },
274     {
275       vendid => 0x10b9,
276       devid => 0x1563,
277       procid => "Acer Labs 1563",
278       driver => "i2c-ali1563",
279     },
280     {
281       vendid => 0x1022,
282       devid  => 0x740b,
283       procid => "AMD-756 Athlon ACPI",
284       driver => "i2c-amd756",
285     },
286     {
287       vendid => 0x1022,
288       devid  => 0x7413,
289       procid => "AMD-766 Athlon ACPI",
290       driver => "i2c-amd756",
291     },
292     {
293       vendid => 0x1022,
294       devid  => 0x7443,
295       procid => "AMD-768 System Management",
296       driver => "i2c-amd756",
297     },
298     {
299       vendid => 0x1022,
300       devid  => 0x746b,
301       procid => "AMD-8111 ACPI",
302       driver => "i2c-amd756",
303     },
304     {
305       vendid => 0x1022,
306       devid  => 0x746a,
307       procid => "AMD-8111 SMBus 2.0",
308       driver => "i2c-amd8111",
309     },
310     {
311       vendid => 0x10de,
312       devid  => 0x01b4,
313       procid => "nVidia nForce SMBus",
314       driver => "i2c-amd756",
315     },
316     {
317       vendid => 0x10de,
318       devid  => 0x0064,
319       procid => "nVidia Corporation nForce2 SMBus (MCP)",
320       driver => "i2c-nforce2",
321     },
322     {
323       vendid => 0x10de,
324       devid  => 0x0084,
325       procid => "nVidia Corporation nForce2 Ultra 400 SMBus (MCP)",
326       driver => "i2c-nforce2",
327     },
328     {
329       vendid => 0x10de,
330       devid  => 0x00D4,
331       procid => "nVidia Corporation nForce3 Pro150 SMBus (MCP)",
332       driver => "i2c-nforce2",
333     },
334     {
335       vendid => 0x10de,
336       devid  => 0x00E4,
337       procid => "nVidia Corporation nForce3 250Gb SMBus (MCP)",
338       driver => "i2c-nforce2",
339     },
340     {
341       vendid => 0x10de,
342       devid  => 0x0052,
343       procid => "nVidia Corporation nForce4 SMBus (MCP)",
344       driver => "i2c-nforce2",
345     },
346     {
347       vendid => 0x10de,
348       devid => 0x0034,
349       procid => "nVidia Corporation nForce4 SMBus (MCP-04)",
350       driver => "i2c-nforce2",
351     },
352     {
353       vendid => 0x10de,
354       devid => 0x0264,
355       procid => "nVidia Corporation nForce4 SMBus (MCP51)",
356       driver => "i2c-nforce2",
357     },
358     {
359       vendid => 0x10de,
360       devid => 0x0368,
361       procid => "nVidia Corporation nForce4 SMBus (MCP55)",
362       driver => "i2c-nforce2",
363     },
364     {
365       vendid => 0x10de,
366       devid => 0x03eb,
367       procid => "nVidia Corporation nForce4 SMBus (MCP61)",
368       driver => "i2c-nforce2",
369     },
370     {
371       vendid => 0x10de,
372       devid => 0x0446,
373       procid => "nVidia Corporation nForce4 SMBus (MCP65)",
374       driver => "i2c-nforce2",
375     },
376     {
377       vendid => 0x1166,
378       devid  => 0x0200,
379       procid => "ServerWorks OSB4 South Bridge",
380       driver => "i2c-piix4",
381     },
382     {
383       vendid => 0x1055,
384       devid  => 0x9463,
385       procid => "SMSC Victory66 South Bridge",
386       driver => "i2c-piix4",
387     },
388     {
389       vendid => 0x1166,
390       devid  => 0x0201,
391       procid => "ServerWorks CSB5 South Bridge",
392       driver => "i2c-piix4",
393     },
394     {
395       vendid => 0x1166,
396       devid  => 0x0203,
397       procid => "ServerWorks CSB6 South Bridge",
398       driver => "i2c-piix4",
399     },
400     {
401       vendid => 0x1166,
402       devid  => 0x0205,
403       procid => "ServerWorks HT-1000 South Bridge",
404       driver => "i2c-piix4",
405     },
406     {
407       vendid => 0x1002,
408       devid  => 0x4353,
409       procid => "ATI Technologies Inc ATI SMBus",
410       driver => "i2c-piix4",
411     },
412     {
413       vendid => 0x1002,
414       devid  => 0x4363,
415       procid => "ATI Technologies Inc ATI SMBus",
416       driver => "i2c-piix4",
417     },
418     {
419       vendid => 0x1002,
420       devid  => 0x4372,
421       procid => "ATI Technologies Inc IXP SB400 SMBus Controller",
422       driver => "i2c-piix4",
423     },
424     {
425       vendid => 0x1002,
426       devid  => 0x4385,
427       procid => "ATI Technologies Inc SB600 SMBus",
428       driver => "i2c-piix4",
429     },
430     {
431       vendid => 0x100B,
432       devid => 0x0500,
433       procid => "SCx200 Bridge",
434       driver => "scx200_acb",
435     },
436     {
437       vendid => 0x100B,
438       devid => 0x0510,
439       procid => "SC1100 Bridge",
440       driver => "scx200_acb",
441     },
442     {
443       vendid => 0x100B,
444       devid => 0x002B,
445       procid => "CS5535 ISA bridge",
446       driver => "scx200_acb",
447     },
448     {
449       vendid => 0x1022,
450       devid => 0x2090,
451       procid => "CS5536 [Geode companion] ISA",
452       driver => "scx200_acb",
453     },
454);
455
456# The following entries used to appear directly in @pci_adapters.
457# Because of the tendency of SiS chipsets to have their real PCI
458# IDs obscured, we have to qualify these with a custom detection
459# routine before we add them to the @pci_adapters list.
460#
461use vars qw(@pci_adapters_sis5595 @pci_adapters_sis96x);
462@pci_adapters_sis5595 = (
463     {
464       vendid => 0x1039,
465       devid  => 0x0008,
466       procid => "Silicon Integrated Systems SIS5595",
467       driver => "i2c-sis5595",
468     },
469);
470
471@pci_adapters_sis96x = (
472     {
473       vendid => 0x1039,
474       devid  => 0x0016,
475       procid => "Silicon Integrated Systems SMBus Controller",
476       driver => "i2c-sis96x",
477     },
478);
479
480# Look-up table to find out an I2C bus' driver based on the bus name.
481# The match field should contain a regular expression matching the I2C
482# bus name as it would appear in /sys/class/i2c-adapter.
483# Note that new drivers probably don't need to be added to this table
484# if they bind to their device, as we will be able to get the driver name
485# from sysfs directly.
486use vars qw(@i2c_adapter_names);
487@i2c_adapter_names = (
488        { driver => "i2c-piix4",        match => qr/^SMBus PIIX4 adapter at / },
489        { driver => "i2c-i801",         match => qr/^SMBus I801 adapter at / },
490        { driver => "i2c-via",          match => qr/^VIA i2c/ },
491        { driver => "i2c-viapro",       match => qr/^SMBus V(IA|ia) Pro adapter at / },
492        { driver => "i2c-sis5595",      match => qr/^SMBus SIS5595 adapter at / },
493        { driver => "i2c-sis630",       match => qr/^SMBus SIS630 adapter at / },
494        { driver => "i2c-sis96x",       match => qr/^SiS96x SMBus adapter at / },
495        { driver => "i2c-ali15x3",      match => qr/^SMBus ALI15X3 adapter at / },
496        { driver => "i2c-ali1535",      match => qr/^SMBus ALI1535 adapter at/ },
497        { driver => "i2c-ali1563",      match => qr/^SMBus ALi 1563 Adapter @ / },
498        { driver => "i2c-amd756",       match => qr/^SMBus (AMD756|AMD766|AMD768|AMD8111|nVidia nForce) adapter at / },
499        { driver => "i2c-amd8111",      match => qr/^SMBus2 AMD8111 adapter at / },
500        { driver => "i2c-nforce2",      match => qr/^SMBus nForce2 adapter at / },
501        { driver => "scx200_acb",       match => qr/^(NatSemi SCx200 ACCESS\.bus|SCx200 ACB\d+|CS553[56] ACB\d+) / },
502);
503
504# This is a list of all recognized chips.
505# Each entry must have the following fields:
506#  name: The full chip name
507#  driver: The driver name (without .o extension). Put in exactly
508#      "to-be-written" if it is not yet available. Put in exactly
509#      "not-a-sensor" if it is not a hardware monitoring chip.
510#      Put in exactly "use-isa-instead" if no i2c driver will be written.
511#  i2c_addrs (optional): For I2C chips, the range of valid I2C addresses to
512#      probe. Recommend avoiding 0x69 because of clock chips.
513#  i2c_detect (optional): For I2C chips, the function to call to detect
514#      this chip. The function should take two parameters: an open file
515#      descriptor to access the bus, and the I2C address to probe.
516#  isa_addrs (optional): For ISA chips, the range of valid port addresses to
517#      probe.
518#  isa_detect (optional): For ISA chips, the function to call to detect
519#      this chip. The function should take one parameter: the ISA address
520#      to probe.
521#  alias_detect (optional): For chips which can be both on the ISA and the
522#      I2C bus, a function which detectes whether two entries are the same.
523#      The function should take three parameters: The ISA address, the
524#      I2C bus number, and the I2C address.
525@chip_ids = (
526     {
527       name => "Myson MTP008",
528       driver => "mtp008",
529       i2c_addrs => [0x2c..0x2e],
530       i2c_detect => sub { mtp008_detect(@_); },
531     },
532     {
533       name => "National Semiconductor LM78",
534       driver => "lm78",
535       i2c_addrs => [0x28..0x2f],
536       i2c_detect => sub { lm78_detect(0, @_); },
537       isa_addrs => [0x290],
538       isa_detect => sub { lm78_isa_detect(0, @_); },
539       alias_detect => sub { winbond_alias_detect(0x2b, 0x3d, @_); },
540     },
541     {
542       name => "National Semiconductor LM78-J",
543       driver => "lm78",
544       i2c_addrs => [0x28..0x2f],
545       i2c_detect => sub { lm78_detect(1, @_); },
546       isa_addrs => [0x290],
547       isa_detect => sub { lm78_isa_detect(1, @_); },
548       alias_detect => sub { winbond_alias_detect(0x2b, 0x3d, @_); },
549     },
550     {
551       name => "National Semiconductor LM79",
552       driver => "lm78",
553       i2c_addrs => [0x28..0x2f],
554       i2c_detect => sub { lm78_detect(2, @_); },
555       isa_addrs => [0x290],
556       isa_detect => sub { lm78_isa_detect(2, @_); },
557       alias_detect => sub { winbond_alias_detect(0x2b, 0x3d, @_); },
558     },
559     {
560       name => "National Semiconductor LM75",
561       driver => "lm75",
562       i2c_addrs => [0x48..0x4f],
563       i2c_detect => sub { lm75_detect(0, @_); },
564     },
565     {
566       name => "Dallas Semiconductor DS75",
567       driver => "lm75",
568       i2c_addrs => [0x48..0x4f],
569       i2c_detect => sub { lm75_detect(1, @_); },
570     },
571     {
572       name => "National Semiconductor LM77",
573       driver => "lm77",
574       i2c_addrs => [0x48..0x4b],
575       i2c_detect => sub { lm77_detect(@_); },
576     },
577     {
578       name => "National Semiconductor LM80",
579       driver => "lm80",
580       i2c_addrs => [0x28..0x2f],
581       i2c_detect => sub { lm80_detect(@_); },
582     },
583     {
584       name => "National Semiconductor LM85",
585       driver => "lm85",
586       i2c_addrs => [0x2c..0x2e],
587       i2c_detect => sub { lm85_detect(0, @_); },
588     },
589     {
590       name => "National Semiconductor LM96000 or PC8374L",
591       driver => "lm85",
592       i2c_addrs => [0x2c..0x2e],
593       i2c_detect => sub { lm85_detect(1, @_); },
594     },
595     {
596       name => "Analog Devices ADM1027",
597       driver => "lm85",
598       i2c_addrs => [0x2c..0x2e],
599       i2c_detect => sub { lm85_detect(2, @_); },
600     },
601     {
602       name => "Analog Devices ADT7460 or ADT7463",
603       driver => "lm85",
604       i2c_addrs => [0x2c..0x2e],
605       i2c_detect => sub { lm85_detect(3, @_); },
606     },
607     {
608       name => "SMSC EMC6D100 or EMC6D101",
609       driver => "lm85",
610       i2c_addrs => [0x2c..0x2e],
611       i2c_detect => sub { lm85_detect(4, @_); },
612     },
613     {
614       name => "SMSC EMC6D102",
615       driver => "lm85",
616       i2c_addrs => [0x2c..0x2e],
617       i2c_detect => sub { lm85_detect(5, @_); },
618     },
619     {
620       name => "SMSC EMC6D103",
621       driver => "lm85",
622       i2c_addrs => [0x2c..0x2e],
623       i2c_detect => sub { lm85_detect(6, @_); },
624     },
625     {
626       name => "Analog Devices ADT7462",
627       driver => "adt7462",
628       i2c_addrs => [0x5c, 0x58],
629       i2c_detect => sub { adt7467_detect(2, @_); },
630     },
631     {
632       name => "Analog Devices ADT7466",
633       driver => "to-be-written",
634       i2c_addrs => [0x4c],
635       i2c_detect => sub { adt7467_detect(3, @_); },
636     },
637     {
638       name => "Analog Devices ADT7467 or ADT7468",
639       driver => "to-be-written",
640       i2c_addrs => [0x2e],
641       i2c_detect => sub { adt7467_detect(0, @_); },
642     },
643     {
644       name => "Analog Devices ADT7470",
645       driver => "adt7470",
646       i2c_addrs => [0x2c, 0x2e, 0x2f],
647       i2c_detect => sub { adt7467_detect(4, @_); },
648     },
649     {
650       name => "Analog Devices ADT7473",
651       driver => "adt7473",
652       i2c_addrs => [0x2e],
653       i2c_detect => sub { adt7473_detect(0, @_); },
654     },
655     {
656       name => "Analog Devices ADT7475",
657       driver => "to-be-written",
658       i2c_addrs => [0x2e],
659       i2c_detect => sub { adt7473_detect(1, @_); },
660     },
661     {
662       name => "Analog Devices ADT7476",
663       driver => "to-be-written",
664       i2c_addrs => [0x2c..0x2e],
665       i2c_detect => sub { adt7467_detect(1, @_); },
666     },
667     {
668       name => "Andigilog aSC7511",
669       driver => "to-be-written",
670       i2c_addrs => [0x4c],
671       i2c_detect => sub { andigilog_aSC7511_detect(0, @_); },
672     },
673     {
674       name => "Andigilog aSC7512",
675       driver => "to-be-written",
676       i2c_addrs => [0x58],
677       i2c_detect => sub { andigilog_detect(0, @_); },
678     },
679     {
680       name => "Andigilog aSC7611",
681       driver => "to-be-written",
682       i2c_addrs => [0x2c, 0x2d, 0x2e],
683       i2c_detect => sub { andigilog_detect(1, @_); },
684     },
685     {
686       name => "Andigilog aSC7621",
687       driver => "to-be-written",
688       i2c_addrs => [0x2c, 0x2d, 0x2e],
689       i2c_detect => sub { andigilog_detect(2, @_); },
690     },
691     {
692       name => "National Semiconductor LM87",
693       driver => "lm87",
694       i2c_addrs => [0x2c..0x2e],
695       i2c_detect => sub { lm87_detect(0, @_); },
696     },
697     {
698       name => "Analog Devices ADM1024",
699       driver => "lm87",
700       i2c_addrs => [0x2c..0x2e],
701       i2c_detect => sub { lm87_detect(1, @_); },
702     },
703     {
704       name => "National Semiconductor LM93",
705       driver => "lm93",
706       i2c_addrs => [0x2c..0x2e],
707       i2c_detect => sub { lm93_detect(@_); },
708     },
709     {
710       name => "Winbond W83781D",
711       driver => "w83781d",
712       i2c_detect => sub { w83781d_detect(0, @_); },
713       i2c_addrs => [0x28..0x2f],
714       isa_addrs => [0x290],
715       isa_detect => sub { w83781d_isa_detect(0, @_); },
716       alias_detect => sub { winbond_alias_detect(0x2b, 0x3d, @_); },
717     },
718     {
719       name => "Winbond W83782D",
720       driver => "w83781d",
721       i2c_addrs => [0x28..0x2f],
722       i2c_detect => sub { w83781d_detect(1, @_); },
723       isa_addrs => [0x290],
724       isa_detect => sub { w83781d_isa_detect(1, @_); },
725       alias_detect => sub { winbond_alias_detect(0x2b, 0x3d, @_); },
726     },
727     {
728       name => "Winbond W83783S",
729       driver => "w83781d",
730       i2c_addrs => [0x2d],
731       i2c_detect => sub { w83781d_detect(2, @_); },
732     },
733     {
734       name => "Winbond W83791D",
735       driver => "w83791d",
736       i2c_addrs => [0x2c..0x2f],
737       i2c_detect => sub { w83781d_detect(7, @_); },
738     },
739     {
740       name => "Winbond W83792D",
741       driver => "w83792d",
742       i2c_addrs => [0x2c..0x2f],
743       i2c_detect => sub { w83781d_detect(8, @_); },
744     },
745     {
746       name => "Winbond W83793R/G",
747       driver => "w83793",
748       i2c_addrs => [0x2c..0x2f],
749       i2c_detect => sub { w83793_detect(0, @_); },
750     },
751     {
752       name => "Winbond W83791SD",
753       driver => "not-a-sensor",
754       i2c_addrs => [0x2c..0x2f],
755       i2c_detect => sub { w83791sd_detect(@_); },
756     },
757     {
758       name => "Winbond W83627HF",
759       driver => "use-isa-instead",
760       i2c_addrs => [0x28..0x2f],
761       i2c_detect => sub { w83781d_detect(3, @_); },
762     },
763     {
764       name => "Winbond W83627EHF",
765       driver => "use-isa-instead",
766       i2c_addrs => [0x28..0x2f],
767       i2c_detect => sub { w83781d_detect(9, @_); },
768     },
769     {
770       name => "Winbond W83627DHG",
771       driver => "use-isa-instead",
772       i2c_addrs => [0x28..0x2f],
773       i2c_detect => sub { w83781d_detect(10, @_); },
774     },
775     {
776       name => "Asus AS99127F (rev.1)",
777       driver => "w83781d",
778       i2c_addrs => [0x28..0x2f],
779       i2c_detect => sub { w83781d_detect(4, @_); },
780     },
781     {
782       name => "Asus AS99127F (rev.2)",
783       driver => "w83781d",
784       i2c_addrs => [0x28..0x2f],
785       i2c_detect => sub { w83781d_detect(5, @_); },
786     },
787     {
788       name => "Asus ASB100 Bach",
789       driver => "asb100",
790       i2c_addrs => [0x28..0x2f],
791       i2c_detect => sub { w83781d_detect(6, @_); },
792     },
793     {
794       name => "Asus ASM58 Mozart-2",
795       driver => "to-be-written",
796       i2c_addrs => [0x77],
797       i2c_detect => sub { mozart_detect(0, @_); },
798     },
799     {
800       name => "Asus AS2K129R Mozart-2",
801       driver => "to-be-written",
802       i2c_addrs => [0x77],
803       i2c_detect => sub { mozart_detect(1, @_); },
804     },
805     {
806       name => "Asus Mozart-2",
807       driver => "to-be-written",
808       i2c_addrs => [0x77],
809       i2c_detect => sub { mozart_detect(2, @_); },
810     },
811     {
812       name => "Winbond W83L784R/AR/G",
813       driver => "to-be-written",
814       i2c_addrs => [0x2d],
815       i2c_detect => sub { w83l784r_detect(0, @_); },
816     },
817     {
818       name => "Winbond W83L785R/G",
819       driver => "to-be-written",
820       i2c_addrs => [0x2d],
821       i2c_detect => sub { w83l784r_detect(1, @_); },
822     },
823     {
824       name => "Winbond W83L786NR/NG/R/G",
825       driver => "w83l786ng",
826       i2c_addrs => [0x2e, 0x2f],
827       i2c_detect => sub { w83l784r_detect(2, @_); },
828     },
829     {
830       name => "Winbond W83L785TS-S",
831       driver => "w83l785ts",
832       i2c_addrs => [0x2e],
833       i2c_detect => sub { w83l785ts_detect(0, @_); },
834     },
835     {
836       name => "Genesys Logic GL518SM Revision 0x00",
837       driver => "gl518sm",
838       i2c_addrs => [0x2c, 0x2d],
839       i2c_detect => sub { gl518sm_detect(0, @_); },
840     },
841     {
842       name => "Genesys Logic GL518SM Revision 0x80",
843       driver => "gl518sm",
844       i2c_addrs => [0x2c, 0x2d],
845       i2c_detect => sub { gl518sm_detect(1, @_); },
846     },
847     {
848       name => "Genesys Logic GL520SM",
849       driver => "gl520sm",
850       i2c_addrs => [0x2c, 0x2d],
851       i2c_detect => sub { gl520sm_detect(@_); },
852     },
853     {
854       name => "Genesys Logic GL525SM",
855       driver => "Unwritten (GL525SM)",
856       i2c_addrs => [0x2d],
857       i2c_detect => sub { gl525sm_detect(@_); },
858     },
859     {
860       name => "Analog Devices ADM9240",
861       driver => "adm9240",
862       i2c_addrs => [0x2c..0x2f],
863       i2c_detect => sub { adm9240_detect(0, @_); },
864     },
865     {
866       name => "Dallas Semiconductor DS1621/DS1631",
867       driver => "ds1621",
868       i2c_addrs => [0x48..0x4f],
869       i2c_detect => sub { ds1621_detect(@_); },
870     },
871     {
872       name => "Dallas Semiconductor DS1780",
873       driver => "adm9240",
874       i2c_addrs => [0x2c..0x2f],
875       i2c_detect => sub { adm9240_detect(1, @_); },
876     },
877     {
878       name => "National Semiconductor LM81",
879       driver => "adm9240",
880       i2c_addrs => [0x2c..0x2f],
881       i2c_detect => sub { adm9240_detect(2, @_); },
882     },
883     {
884       name => "Analog Devices ADM1026",
885       driver => "adm1026",
886       i2c_addrs => [0x2c, 0x2d, 0x2e],
887       i2c_detect => sub { adm1026_detect(0, @_); },
888     },
889     {
890       name => "Analog Devices ADM1025",
891       driver => "adm1025",
892       i2c_addrs => [0x2c..0x2e],
893       i2c_detect => sub { adm1025_detect(0, @_); },
894     },
895     {
896       name => "Philips NE1619",
897       driver => "adm1025",
898       i2c_addrs => [0x2c..0x2d],
899       i2c_detect => sub { adm1025_detect(1, @_); },
900     },
901     {
902       name => "Analog Devices ADM1021",
903       driver => "adm1021",
904       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
905       i2c_detect => sub { adm1021_detect(0, @_); },
906     },
907     {
908       name => "Analog Devices ADM1021A/ADM1023",
909       driver => "adm1021",
910       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
911       i2c_detect => sub { adm1021_detect(1, @_); },
912     },
913     {
914       name => "Maxim MAX1617",
915       driver => "adm1021",
916       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
917       i2c_detect => sub { adm1021_detect(2, @_); },
918     },
919     {
920       name => "Maxim MAX1617A",
921       driver => "adm1021",
922       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
923       i2c_detect => sub { adm1021_detect(3, @_); },
924     },
925     {
926       name => "Maxim MAX1668",
927       driver => "max1668",
928       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
929       i2c_detect => sub { max1668_detect(0, @_); },
930     },
931     {
932       name => "Maxim MAX1805",
933       driver => "max1668",
934       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
935       i2c_detect => sub { max1668_detect(1, @_); },
936     },
937     {
938       name => "Maxim MAX1989",
939       driver => "max1668",
940       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
941       i2c_detect => sub { max1668_detect(2, @_); },
942     },
943     {
944       name => "Maxim MAX6650/MAX6651",
945       driver => "max6650",
946       i2c_addrs => [0x1b, 0x1f, 0x48, 0x4b],
947       i2c_detect => sub { max6650_detect(0, @_); },
948     },
949     {
950       name => "Maxim MAX6655/MAX6656",
951       driver => "max6655",
952       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
953       i2c_detect => sub { max6655_detect(0, @_); },
954     },
955     {
956       name => "TI THMC10",
957       driver => "adm1021",
958       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
959       i2c_detect => sub { adm1021_detect(4, @_); },
960     },
961     {
962       name => "National Semiconductor LM84",
963       driver => "adm1021",
964       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
965       i2c_detect => sub { adm1021_detect(5, @_); },
966     },
967     {
968       name => "Genesys Logic GL523SM",
969       driver => "adm1021",
970       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
971       i2c_detect => sub { adm1021_detect(6, @_); },
972     },
973     {
974       name => "Onsemi MC1066",
975       driver => "adm1021",
976       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
977       i2c_detect => sub { adm1021_detect(7, @_); },
978     },
979     {
980       name => "Maxim MAX1618",
981       driver => "max1619",
982       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
983       i2c_detect => sub { max1619_detect(1, @_); },
984     },
985     {
986       name => "Maxim MAX1619",
987       driver => "max1619",
988       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
989       i2c_detect => sub { max1619_detect(0, @_); },
990     },
991     {
992       name => "National Semiconductor LM82/LM83",
993       driver => "lm83",
994       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
995       i2c_detect => sub { lm83_detect(0, @_); },
996     },
997     {
998       name => "National Semiconductor LM90",
999       driver => "lm90",
1000       i2c_addrs => [0x4c],
1001       i2c_detect => sub { lm90_detect(0, @_); },
1002     },
1003     {
1004       name => "National Semiconductor LM89/LM99",
1005       driver => "lm90",
1006       i2c_addrs => [0x4c..0x4d],
1007       i2c_detect => sub { lm90_detect(1, @_); },
1008     },
1009     {
1010       name => "National Semiconductor LM86",
1011       driver => "lm90",
1012       i2c_addrs => [0x4c],
1013       i2c_detect => sub { lm90_detect(2, @_); },
1014     },
1015     {
1016       name => "Analog Devices ADM1032",
1017       driver => "lm90",
1018       i2c_addrs => [0x4c..0x4d],
1019       i2c_detect => sub { lm90_detect(3, @_); },
1020     },
1021     {
1022       name => "Maxim MAX6654/MAX6690",
1023       driver => "to-be-written", # probably lm90
1024       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
1025       i2c_detect => sub { lm90_detect(4, @_); },
1026     },
1027     {
1028       name => "Maxim MAX6657/MAX6658/MAX6659",
1029       driver => "lm90",
1030       i2c_addrs => [0x4c],
1031       i2c_detect => sub { max6657_detect(@_); },
1032     },
1033     {
1034       name => "Maxim MAX6659",
1035       driver => "lm90",
1036       i2c_addrs => [0x4d..0x4e], # 0x4c is handled above
1037       i2c_detect => sub { max6657_detect(@_); },
1038     },
1039     {
1040       name => "Maxim MAX6646",
1041       driver => "lm90",
1042       i2c_addrs => [0x4d],
1043       i2c_detect => sub { lm90_detect(6, @_); },
1044     },
1045     {
1046       name => "Maxim MAX6647",
1047       driver => "lm90",
1048       i2c_addrs => [0x4e],
1049       i2c_detect => sub { lm90_detect(6, @_); },
1050     },
1051     {
1052       name => "Maxim MAX6648/MAX6649/MAX6692",
1053       driver => "lm90",
1054       i2c_addrs => [0x4c],
1055       i2c_detect => sub { lm90_detect(6, @_); },
1056     },
1057     {
1058       name => "Maxim MAX6680/MAX6681",
1059       driver => "lm90",
1060       i2c_addrs => [0x18..0x1a, 0x29..0x2b, 0x4c..0x4e],
1061       i2c_detect => sub { lm90_detect(7, @_); },
1062     },
1063     {
1064       name => "Winbond W83L771W/G",
1065       driver => "lm90",
1066       i2c_addrs => [0x4c],
1067       i2c_detect => sub { lm90_detect(8, @_); },
1068     },
1069     {
1070       name => "Texas Instruments TMP401",
1071       driver => "tmp401",
1072       i2c_addrs => [0x4c],
1073       i2c_detect => sub { lm90_detect(9, @_); },
1074     },
1075     {
1076       name => "Texas Instruments TMP411",
1077       driver => "to-be-written",
1078       i2c_addrs => [0x4c..0x4e],
1079       i2c_detect => sub { lm90_detect(10, @_); },
1080     },
1081     {
1082       name => "National Semiconductor LM95231",
1083       driver => "to-be-written",
1084       i2c_addrs => [0x2b, 0x19, 0x2a],
1085       i2c_detect => sub { lm95231_detect(@_); },
1086     },
1087     {
1088       name => "National Semiconductor LM63",
1089       driver => "lm63",
1090       i2c_addrs => [0x4c],
1091       i2c_detect => sub { lm63_detect(1, @_); },
1092     },
1093     {
1094       name => "National Semiconductor LM64",
1095       driver => "to-be-written", # lm63
1096       i2c_addrs => [0x18, 0x4e],
1097       i2c_detect => sub { lm63_detect(3, @_); },
1098     },
1099     {
1100       name => "Fintek F75363SG",
1101       driver => "lm63", # Not yet
1102       i2c_addrs => [0x4c],
1103       i2c_detect => sub { lm63_detect(2, @_); },
1104     },
1105     {
1106       name => "National Semiconductor LM92",
1107       driver => "lm92",
1108       i2c_addrs => [0x48..0x4b],
1109       i2c_detect => sub { lm92_detect(0, @_); },
1110     },
1111     {
1112       name => "National Semiconductor LM76",
1113       driver => "lm92",
1114       i2c_addrs => [0x48..0x4b],
1115       i2c_detect => sub { lm92_detect(1, @_); },
1116     },
1117     {
1118       name => "Maxim MAX6633/MAX6634/MAX6635",
1119       driver => "lm92",
1120       i2c_addrs => [0x48..0x4f], # The MAX6633 can also use 0x40-0x47 but we
1121                                  # don't want to probe these addresses, it's
1122                                  # dangerous.
1123       i2c_detect => sub { lm92_detect(2, @_); },
1124     },
1125     {
1126       name => "Analog Devices ADT7461",
1127       driver => "lm90",
1128       i2c_addrs => [0x4c..0x4d],
1129       i2c_detect => sub { lm90_detect(5, @_); },
1130     },
1131     {
1132       name => "Analog Devices ADT7481",
1133       driver => "to-be-written",
1134       i2c_addrs => [0x4c, 0x4b],
1135       i2c_detect => sub { adt7481_detect(@_); },
1136     },
1137     {
1138       name => "Analog Devices ADM1029",
1139       driver => "adm1029",
1140       i2c_addrs => [0x28..0x2f],
1141       i2c_detect => sub { adm1029_detect(0, @_); },
1142     },
1143     {
1144       name => "Analog Devices ADM1030",
1145       driver => "adm1031",
1146       i2c_addrs => [0x2c..0x2e],
1147       i2c_detect => sub { adm1031_detect(0, @_); },
1148     },
1149     {
1150       name => "Analog Devices ADM1031",
1151       driver => "adm1031",
1152       i2c_addrs => [0x2c..0x2e],
1153       i2c_detect => sub { adm1031_detect(1, @_); },
1154     },
1155     {
1156       name => "Analog Devices ADM1033",
1157       driver => "to-be-written",
1158       i2c_addrs => [0x50..0x53],
1159       i2c_detect => sub { adm1034_detect(0, @_); },
1160     },
1161     {
1162       name => "Analog Devices ADM1034",
1163       driver => "to-be-written",
1164       i2c_addrs => [0x50..0x53],
1165       i2c_detect => sub { adm1034_detect(1, @_); },
1166     },
1167     {
1168       name => "Analog Devices ADM1022",
1169       driver => "thmc50",
1170       i2c_addrs => [0x2c..0x2e],
1171       i2c_detect => sub { adm1022_detect(0, @_); },
1172     },
1173     {
1174       name => "Texas Instruments THMC50",
1175       driver => "thmc50",
1176       i2c_addrs => [0x2c..0x2e],
1177       i2c_detect => sub { adm1022_detect(1, @_); },
1178     },
1179     {
1180       name => "Analog Devices ADM1028",
1181       driver => "thmc50",
1182       i2c_addrs => [0x2e],
1183       i2c_detect => sub { adm1022_detect(2, @_); },
1184     },
1185     {
1186       name => "Texas Instruments THMC51",
1187       driver => "to-be-written", # thmc50
1188       i2c_addrs => [0x2e], # At least (no datasheet)
1189       i2c_detect => sub { adm1022_detect(3, @_); },
1190     },
1191     {
1192       name => "VIA VT1211 (I2C)",
1193       driver => "use-isa-instead",
1194       i2c_addrs => [0x2d],
1195       i2c_detect => sub { vt1211_i2c_detect(0, @_); },
1196     },
1197     {
1198       name => "ITE IT8712F",
1199       driver => "it87",
1200       i2c_addrs => [0x28..0x2f],
1201       i2c_detect => sub { ite_detect(0, @_); },
1202     },
1203     {
1204       name => "ITE IT8201R/IT8203R/IT8206R/IT8266R",
1205       driver => "not-a-sensor",
1206       i2c_addrs => [0x4e],
1207       i2c_detect => sub { ite_overclock_detect(@_); },
1208     },
1209     {
1210       name => "SPD EEPROM",
1211       driver => "not-a-sensor",
1212       # Can also live at 0x54-0x57, but we don't care: we only check
1213       # for SPD and EDID EEPROMs because some hardware monitoring chips
1214       # can live at 0x50-0x53.
1215       i2c_addrs => [0x50..0x53],
1216       i2c_detect => sub { eeprom_detect(@_); },
1217     },
1218     {
1219       name => "EDID EEPROM",
1220       driver => "not-a-sensor",
1221       i2c_addrs => [0x50..0x53],
1222       i2c_detect => sub { ddcmonitor_detect(@_); },
1223     },
1224     {
1225       name => "FSC Poseidon I",
1226       driver => sub { kernel_version_at_least(2, 6, 24) ? "fschmd" : "fscpos" },
1227       i2c_addrs => [0x73],
1228       i2c_detect => sub { fsc_detect(0, @_); },
1229     },
1230     {
1231       name => "FSC Poseidon II",
1232       driver => "to-be-written",
1233       i2c_addrs => [0x73],
1234       i2c_detect => sub { fsc_detect(1, @_); },
1235     },
1236     {
1237       name => "FSC Scylla",
1238       driver => "fschmd",
1239       i2c_addrs => [0x73],
1240       i2c_detect => sub { fsc_detect(2, @_); },
1241     },
1242     {
1243       name => "FSC Hermes",
1244       driver => sub { kernel_version_at_least(2, 6, 24) ? "fschmd" : "fscher" },
1245       i2c_addrs => [0x73],
1246       i2c_detect => sub { fsc_detect(3, @_); },
1247     },
1248     {
1249       name => "FSC Heimdal",
1250       driver => "fschmd",
1251       i2c_addrs => [0x73],
1252       i2c_detect => sub { fsc_detect(4, @_); },
1253     },
1254     {
1255       name => "FSC Heracles",
1256       driver => "fschmd",
1257       i2c_addrs => [0x73],
1258       i2c_detect => sub { fsc_detect(5, @_); },
1259     },
1260     {
1261       name => "ALi M5879",
1262       driver => "to-be-written",
1263       i2c_addrs => [0x2c..0x2d],
1264       i2c_detect => sub { m5879_detect(@_); },
1265     },
1266     {
1267       name => "SMSC LPC47M15x/192/292/997",
1268       driver => "smsc47m192",
1269       i2c_addrs => [0x2c..0x2d],
1270       i2c_detect => sub { smsc47m192_detect(@_); },
1271     },
1272     {
1273       name => "SMSC DME1737",
1274       driver => "dme1737",
1275       i2c_addrs => [0x2c..0x2e],
1276       i2c_detect => sub { dme1737_detect(1, @_); },
1277     },
1278     {
1279       name => "SMSC SCH5027D-NW",
1280       driver => "dme1737",
1281       i2c_addrs => [0x2c..0x2e],
1282       i2c_detect => sub { dme1737_detect(2, @_); },
1283     },
1284     {
1285       name => "Fintek F75111R/RG/N (GPIO)",
1286       driver => "not-a-sensor",
1287       i2c_addrs => [0x4e], # 0x37 not probed
1288       i2c_detect => sub { fintek_detect(1, @_); },
1289     },
1290     {
1291       name => "Fintek F75121R/F75122R/RG (VID+GPIO)",
1292       driver => "to-be-written",
1293       i2c_addrs => [0x4e], # 0x37 not probed
1294       i2c_detect => sub { fintek_detect(2, @_); },
1295     },
1296     {
1297       name => "Fintek F75373S/SG",
1298       driver => "f75375s",
1299       i2c_addrs => [0x2d..0x2e],
1300       i2c_detect => sub { fintek_detect(3, @_); },
1301     },
1302     {
1303       name => "Fintek F75375S/SP",
1304       driver => "f75375s",
1305       i2c_addrs => [0x2d..0x2e],
1306       i2c_detect => sub { fintek_detect(4, @_); },
1307     },
1308     {
1309       name => "Fintek F75387SG/RG",
1310       driver => "to-be-written",
1311       i2c_addrs => [0x2d..0x2e],
1312       i2c_detect => sub { fintek_detect(5, @_); },
1313     },
1314     {
1315       name => "Fintek F75383S/M",
1316       driver => "to-be-written",
1317       i2c_addrs => [0x4c],
1318       i2c_detect => sub { fintek_detect(6, @_); },
1319     },
1320     {
1321       name => "Fintek F75384S/M",
1322       driver => "to-be-written",
1323       i2c_addrs => [0x4d],
1324       i2c_detect => sub { fintek_detect(6, @_); },
1325     },
1326     {
1327       name => "Fintek custom power control IC",
1328       driver => "to-be-written",
1329       i2c_addrs => [0x2f],
1330       i2c_detect => sub { fintek_detect(7, @_); },
1331     },
1332     {
1333       name => "Smart Battery",
1334       driver => "smartbatt",
1335       i2c_addrs => [0x0b],
1336       i2c_detect => sub { smartbatt_detect(@_); },
1337     },
1338     {
1339       name => "IPMI BMC KCS",
1340       driver => "ipmisensors",
1341       isa_addrs => [0x0ca0],
1342       isa_detect => sub { ipmi_kcs_detect(@_); },
1343     },
1344     {
1345       name => "IPMI BMC SMIC",
1346       driver => "ipmisensors",
1347       isa_addrs => [0x0ca8],
1348       isa_detect => sub { ipmi_smic_detect(@_); },
1349     },
1350);
1351
1352
1353# This is a list of all recognized superio chips.
1354# Each entry must have the following fields:
1355#  name: The full chip name
1356#  driver: The driver name (without .o extension). Put in
1357#      "to-be-written" if it is not yet available.
1358#      Put in "not-a-sensor" if the chip doesn't have hardware monitoring
1359#      capabilities (listing such chips here removes the need of manual
1360#      lookup when people report them).
1361#      Put in exactly "via-smbus-only" if this is a Super-I/O chip whose
1362#      hardware monitoring registers can only be accessed via the SMBus.
1363#  devid: The device ID(s) we have to match (base device)
1364#  devid_mask (optional): Bitmask to apply before checking the device ID
1365#  logdev: The logical device containing the sensors
1366#  alias_detect (optional): For chips which can be both on the ISA and the
1367#      I2C bus, a function which detectes whether two entries are the same.
1368#      The function should take three parameters: The ISA address, the
1369#      I2C bus number, and the I2C address.
1370# Entries are grouped by family. Each family entry has the following fields:
1371#  family: The family name
1372#  guess (optional): Typical logical device address. This lets us do
1373#      generic probing if we fail to recognize the chip.
1374#  enter: The password sequence to write to the address register
1375#  chips: Array of chips
1376# The order of families matters, because we stop as soon as one family
1377# succeeds. So we have to list families with shorter password sequences
1378# first.
1379@superio_ids = (
1380  {
1381    family => "National Semiconductor",
1382    enter =>
1383    {
1384      0x2e => [],
1385      0x4e => [],
1386    },
1387    chips =>
1388    [
1389      {
1390        name => "Nat. Semi. PC8374L Super IO Sensors",
1391        driver => "to-be-written",
1392        devid => 0xf1,
1393        logdev => 0x08,
1394      },
1395      {
1396        name => "Nat. Semi. PC87351 Super IO Fan Sensors",
1397        driver => "to-be-written",
1398        devid => 0xe2,
1399        logdev => 0x08,
1400      },
1401      {
1402        name => "Nat. Semi. PC87360 Super IO Fan Sensors",
1403        driver => "pc87360",
1404        devid => 0xe1,
1405        logdev => 0x09,
1406      },
1407      {
1408        name => "Nat. Semi. PC87363 Super IO Fan Sensors",
1409        driver => "pc87360",
1410        devid => 0xe8,
1411        logdev => 0x09,
1412      },
1413      {
1414        name => "Nat. Semi. PC87364 Super IO Fan Sensors",
1415        driver => "pc87360",
1416        devid => 0xe4,
1417        logdev => 0x09,
1418      },
1419      {
1420        name => "Nat. Semi. PC87365 Super IO Fan Sensors",
1421        driver => "pc87360",
1422        devid => 0xe5,
1423        logdev => 0x09,
1424      },
1425      {
1426        name => "Nat. Semi. PC87365 Super IO Voltage Sensors",
1427        driver => "pc87360",
1428        devid => 0xe5,
1429        logdev => 0x0d,
1430      },
1431      {
1432        name => "Nat. Semi. PC87365 Super IO Thermal Sensors",
1433        driver => "pc87360",
1434        devid => 0xe5,
1435        logdev => 0x0e,
1436      },
1437      {
1438        name => "Nat. Semi. PC87366 Super IO Fan Sensors",
1439        driver => "pc87360",
1440        devid => 0xe9,
1441        logdev => 0x09,
1442      },
1443      {
1444        name => "Nat. Semi. PC87366 Super IO Voltage Sensors",
1445        driver => "pc87360",
1446        devid => 0xe9,
1447        logdev => 0x0d,
1448      },
1449      {
1450        name => "Nat. Semi. PC87366 Super IO Thermal Sensors",
1451        driver => "pc87360",
1452        devid => 0xe9,
1453        logdev => 0x0e,
1454      },
1455      {
1456        name => "Nat. Semi. PC87372 Super IO Fan Sensors",
1457        driver => "to-be-written",
1458        devid => 0xf0,
1459        logdev => 0x09,
1460      },
1461      {
1462        name => "Nat. Semi. PC87373 Super IO Fan Sensors",
1463        driver => "to-be-written",
1464        devid => 0xf3,
1465        logdev => 0x09,
1466      },
1467      {
1468        name => "Nat. Semi. PC87591 Super IO",
1469        driver => "to-be-written",
1470        devid => 0xec,
1471        logdev => 0x0f,
1472      },
1473      {
1474        name => "Nat. Semi. PC87317 Super IO",
1475        driver => "not-a-sensor",
1476        devid => 0xd0,
1477      },
1478      {
1479        name => "Nat. Semi. PC97317 Super IO",
1480        driver => "not-a-sensor",
1481        devid => 0xdf,
1482      },
1483      {
1484        name => "Nat. Semi. PC8739x Super IO",
1485        driver => "not-a-sensor",
1486        devid => 0xea,
1487      },
1488      {
1489        name => "Nat. Semi. PC8741x Super IO",
1490        driver => "not-a-sensor",
1491        devid => 0xee,
1492      },
1493      {
1494        name => "Nat. Semi. PC87427 Super IO Fan Sensors",
1495        driver => "pc87427",
1496        devid => 0xf2,
1497        logdev => 0x09,
1498      },
1499      {
1500        name => "Nat. Semi. PC87427 Super IO Health Sensors",
1501        driver => "to-be-written",
1502        devid => 0xf2,
1503        logdev => 0x14,
1504      },
1505    ],
1506  },
1507  {
1508    family => "SMSC",
1509    enter =>
1510    {
1511      0x2e => [0x55],
1512      0x4e => [0x55],
1513    },
1514    chips =>
1515    [
1516      {
1517        name => "SMSC DME1737 Super IO",
1518        # Hardware monitoring features are accessed on the SMBus
1519        driver => "via-smbus-only",
1520        devid => 0x78,
1521      },
1522      {
1523        name => "SMSC DME1737 Super IO",
1524        # The DME1737 shows up twice in this list because it can return either
1525        # 0x78 or 0x77 as its device ID.
1526        # Hardware monitoring features are accessed on the SMBus
1527        driver => "via-smbus-only",
1528        devid => 0x77,
1529      },
1530      {
1531        name => "SMSC EMC2700LPC Super IO",
1532        # no datasheet
1533        devid => 0x67,
1534      },
1535      {
1536        name => "SMSC FDC37B72x Super IO",
1537        driver => "not-a-sensor",
1538        devid => 0x4c,
1539      },
1540      {
1541        name => "SMSC FDC37B78x Super IO",
1542        driver => "not-a-sensor",
1543        devid => 0x44,
1544      },
1545      {
1546        name => "SMSC FDC37C672 Super IO",
1547        driver => "not-a-sensor",
1548        devid => 0x40,
1549      },
1550      {
1551        name => "SMSC FDC37M707 Super IO",
1552        driver => "not-a-sensor",
1553        devid => 0x42,
1554      },
1555      {
1556        name => "SMSC FDC37M81x Super IO",
1557        driver => "not-a-sensor",
1558        devid => 0x4d,
1559      },
1560      {
1561        name => "SMSC LPC47B27x Super IO Fan Sensors",
1562        driver => "smsc47m1",
1563        devid => 0x51,
1564        logdev => 0x0a,
1565      },
1566      {
1567        name => "SMSC LPC47B34x Super IO",
1568        driver => "not-a-sensor",
1569        devid => 0x56,
1570      },
1571      {
1572        name => "SMSC LPC47B357/M967 Super IO",
1573        driver => "not-a-sensor",
1574        devid => 0x5d,
1575      },
1576      {
1577        name => "SMSC LPC47B367-NC Super IO",
1578        driver => "not-a-sensor",
1579        devid => 0x6d,
1580      },
1581      {
1582        name => "SMSC LPC47B37x Super IO Fan Sensors",
1583        driver => "to-be-written",
1584        devid => 0x52,
1585        logdev => 0x0a,
1586      },
1587      {
1588        name => "SMSC LPC47B397-NC Super IO",
1589        driver => "smsc47b397",
1590        devid => 0x6f,
1591        logdev => 0x08,
1592      },
1593      {
1594        name => "SMSC LPC47M10x/112/13x Super IO Fan Sensors",
1595        driver => "smsc47m1",
1596        devid => 0x59,
1597        logdev => 0x0a,
1598      },
1599      {
1600        name => "SMSC LPC47M14x Super IO Fan Sensors",
1601        driver => "smsc47m1",
1602        devid => 0x5f,
1603        logdev => 0x0a,
1604      },
1605      {
1606        name => "SMSC LPC47M15x/192/997 Super IO Fan Sensors",
1607        driver => "smsc47m1",
1608        devid => 0x60,
1609        logdev => 0x0a,
1610      },
1611      {
1612        name => "SMSC LPC47M172 Super IO Fan Sensors",
1613        driver => "to-be-written",
1614        devid => 0x14,
1615        logdev => 0x0a,
1616      },
1617      {
1618        name => "SMSC LPC47M182 Super IO Fan Sensors",
1619        driver => "to-be-written",
1620        devid => 0x74,
1621        logdev => 0x0a,
1622      },
1623      {
1624        name => "SMSC LPC47M233 Super IO Sensors",
1625        driver => "smsc47m1",
1626        devid => 0x6b80,
1627        devid_mask => 0xff80,
1628        logdev => 0x0a,
1629      },
1630      {
1631        name => "SMSC LPC47M292 Super IO Fan Sensors",
1632        driver => "smsc47m1",
1633        devid => 0x6b00,
1634        devid_mask => 0xff80,
1635        logdev => 0x0a,
1636      },
1637      {
1638        name => "SMSC LPC47M584-NC Super IO",
1639        # No datasheet
1640        devid => 0x76,
1641      },
1642      {
1643        name => "SMSC LPC47N252 Super IO Fan Sensors",
1644        driver => "to-be-written",
1645        devid => 0x0e,
1646        logdev => 0x09,
1647      },
1648      {
1649        name => "SMSC LPC47S42x Super IO Fan Sensors",
1650        driver => "to-be-written",
1651        devid => 0x57,
1652        logdev => 0x0a,
1653      },
1654      {
1655        name => "SMSC LPC47S45x Super IO Fan Sensors",
1656        driver => "to-be-written",
1657        devid => 0x62,
1658        logdev => 0x0a,
1659      },
1660      {
1661        name => "SMSC LPC47U33x Super IO Fan Sensors",
1662        driver => "to-be-written",
1663        devid => 0x54,
1664        logdev => 0x0a,
1665      },
1666      {
1667        name => "SMSC SCH3112 Super IO",
1668        driver => "dme1737",
1669        devid => 0x7c,
1670        logdev => 0x0a,
1671      },
1672      {
1673        name => "SMSC SCH3114 Super IO",
1674        driver => "dme1737",
1675        devid => 0x7d,
1676        logdev => 0x0a,
1677      },
1678      {
1679        name => "SMSC SCH3116 Super IO",
1680        driver => "dme1737",
1681        devid => 0x7f,
1682        logdev => 0x0a,
1683      },
1684      {
1685        name => "SMSC SCH4307 Super IO Fan Sensors",
1686        driver => "to-be-written",
1687        devid => 0x90,
1688        logdev => 0x08,
1689      },
1690      {
1691        name => "SMSC SCH5027D-NW Super IO",
1692        # Hardware monitoring features are accessed on the SMBus
1693        driver => "via-smbus-only",
1694        devid => 0x89,
1695      },
1696      {
1697        name => "SMSC SCH5127 Super IO",
1698        driver => "dme1737",
1699        devid => 0x86,
1700        logdev => 0x0a,
1701      },
1702      {
1703        name => "SMSC SCH5307-NS Super IO",
1704        driver => "smsc47b397",
1705        devid => 0x81,
1706        logdev => 0x08,
1707      },
1708      {
1709        name => "SMSC SCH5317 Super IO",
1710        driver => "smsc47b397",
1711        devid => 0x85,
1712        logdev => 0x08,
1713      },
1714      {
1715        name => "SMSC SCH5317 Super IO",
1716        # The SCH5317 shows up twice in this list because it can return either
1717        # 0x85 or 0x8c as its device ID.
1718        driver => "smsc47b397",
1719        devid => 0x8c,
1720        logdev => 0x08,
1721      },
1722      {
1723        name => "SMSC SCH5504-NS Super IO",
1724        # No datasheet
1725        driver => "not-a-sensor",
1726        devid => 0x79,
1727      },
1728      {
1729        name => "SMSC SCH5514D-NS Super IO",
1730        # No datasheet
1731        driver => "not-a-sensor",
1732        devid => 0x83,
1733      },
1734    ],
1735    # Non-standard SMSC detection callback and chip list. These chips differ
1736    # from the standard ones listed above in that the device ID register
1737    # address is 0x0d instead of 0x20 (as specified by the ISA PNP spec).
1738    ns_detect => \&smsc_ns_detect_superio,
1739    ns_chips =>
1740    [
1741      {
1742        name => "SMSC FDC37C665 Super IO",
1743        driver => "not-a-sensor",
1744        devid => 0x65,
1745      },
1746      {
1747        name => "SMSC FDC37C666 Super IO",
1748        driver => "not-a-sensor",
1749        devid => 0x66,
1750      },
1751      {
1752        name => "SMSC FDC37C669 Super IO",
1753        driver => "not-a-sensor",
1754        devid => 0x03,
1755      },
1756      {
1757        name => "SMSC FDC37N769 Super IO",
1758        driver => "not-a-sensor",
1759        devid => 0x28,
1760      },
1761      {
1762        name => "SMSC LPC47N227 Super IO",
1763        driver => "not-a-sensor",
1764        devid => 0x5a,
1765      },
1766    ],
1767  },
1768  {
1769    family => "VIA/Winbond/Fintek",
1770    guess => 0x290,
1771    enter =>
1772    {
1773      0x2e => [0x87, 0x87],
1774      0x4e => [0x87, 0x87],
1775    },
1776    chips =>
1777    [
1778      {
1779        name => "VIA VT1211 Super IO Sensors",
1780        driver => "vt1211",
1781        devid => 0x3c,
1782        logdev => 0x0b,
1783        alias_detect => sub { vt1211_alias_detect(@_); },
1784      },
1785      {
1786        name => "VIA VT1212 Super IO Lite",   # in 100 pin TQFP package
1787        driver => "not-a-sensor",
1788        devid => 0x3e,
1789      },
1790      {
1791        name => "VIA VT1212 Super IO Lite",   # in 48 pin LQFP package
1792        driver => "not-a-sensor",
1793        devid => 0x3f,
1794      },
1795      {
1796        name => "Winbond W83627HF/F/HG/G Super IO Sensors",
1797        driver => "w83627hf",
1798        devid => 0x52,
1799        logdev => 0x0b,
1800        alias_detect => sub { winbond_alias_detect(0x2b, 0x3d, @_); },
1801      },
1802      {
1803        name => "Winbond W83627THF/THG Super IO Sensors",
1804        driver => "w83627hf",
1805        devid => 0x82,
1806        logdev => 0x0b,
1807      },
1808      {
1809        name => "Winbond W83637HF/HG Super IO Sensors",
1810        driver => "w83627hf",
1811        devid => 0x70,
1812        logdev => 0x0b,
1813      },
1814      {
1815        name => "Winbond W83687THF Super IO Sensors",
1816        driver => "w83627hf",
1817        devid => 0x85,
1818        logdev => 0x0b,
1819      },
1820      {
1821        name => "Winbond W83697HF/F/HG Super IO Sensors",
1822        driver => "w83627hf",
1823        devid => 0x60,
1824        logdev => 0x0b,
1825      },
1826      {
1827        name => "Winbond W83697SF/UF/UG Super IO PWM",
1828        driver => "to-be-written",
1829        devid => 0x68,
1830        logdev => 0x0b,
1831      },
1832      {
1833        name => "Winbond W83627EHF/EF/EHG/EG Super IO Sensors",
1834        driver => "w83627ehf",
1835        # W83627EHF datasheet says 0x886x but 0x8853 was seen, thus the
1836        # broader mask. W83627EHG was seen with ID 0x8863.
1837        devid => 0x8840,
1838        devid_mask => 0xFFC0,
1839        logdev => 0x0b,
1840        alias_detect => sub { winbond_alias_detect(0x2b, 0x3e, @_); },
1841      },
1842      {
1843        name => "Winbond W83627DHG Super IO Sensors",
1844        driver => "w83627ehf",
1845        devid => 0xA020,
1846        devid_mask => 0xFFF0,
1847        logdev => 0x0b,
1848        alias_detect => sub { winbond_alias_detect(0x2b, 0x3e, @_); },
1849      },
1850      {
1851        name => "Winbond W83L517D Super IO",
1852        driver => "not-a-sensor",
1853        devid => 0x61,
1854      },
1855      {
1856        name => "Fintek F71805F/FG Super IO Sensors",
1857        driver => "f71805f",
1858        devid => 0x0406,
1859        logdev => 0x04,
1860      },
1861      {
1862        name => "Fintek F71862FG Super IO Sensors",
1863        driver => "to-be-written",
1864        devid => 0x0601,
1865        logdev => 0x04,
1866      },
1867      {
1868        name => "Fintek F71806FG/F71872FG Super IO Sensors",
1869        driver => "f71805f",
1870        devid => 0x0341,
1871        logdev => 0x04,
1872      },
1873      {
1874        name => "Fintek F71858DG Super IO Sensors",
1875        driver => "to-be-written",
1876        devid => 0x0507,
1877        logdev => 0x02,
1878      },
1879      {
1880        name => "Fintek F71882FG/F71883FG Super IO Sensors",
1881        driver => "f71882fg",
1882        devid => 0x0541,
1883        logdev => 0x04,
1884      },
1885      {
1886        name => "Fintek F81216D Super IO",
1887        driver => "not-a-sensor",
1888        devid => 0x0208,
1889      },
1890      {
1891        name => "Fintek F81218D Super IO",
1892        driver => "not-a-sensor",
1893        devid => 0x0206,
1894      },
1895      {
1896        name => "Asus F8000 Super IO",
1897        driver => "f8000",
1898        devid => 0x0581,
1899        logdev => 0x04,
1900      },
1901      {
1902        # Shouldn't be in this family, but seems to be still.
1903        name => "ITE IT8708F Super IO",
1904        driver => "not-a-sensor",
1905        devid => 0x8708,
1906      },
1907    ],
1908  },
1909  {
1910    family => "ITE",
1911    guess => 0x290,
1912    enter =>
1913    {
1914      0x2e => [0x87, 0x01, 0x55, 0x55],
1915      0x4e => [0x87, 0x01, 0x55, 0xaa],
1916    },
1917    chips =>
1918    [
1919      {
1920        name => "ITE IT8702F Super IO Sensors",
1921        driver => "to-be-written",
1922        devid => 0x8702,
1923        logdev => 0x04,
1924      },
1925      {
1926        name => "ITE IT8705F Super IO Sensors",
1927        driver => "it87",
1928        devid => 0x8705,
1929        logdev => 0x04,
1930      },
1931      {
1932        name => "ITE IT8712F Super IO Sensors",
1933        driver => "it87",
1934        devid => 0x8712,
1935        logdev => 0x04,
1936        alias_detect => sub { winbond_alias_detect(0x30, 0x45, @_); },
1937      },
1938      {
1939        name => "ITE IT8716F Super IO Sensors",
1940        driver => "it87",
1941        devid => 0x8716,
1942        logdev => 0x04,
1943      },
1944      {
1945        name => "ITE IT8718F Super IO Sensors",
1946        driver => "it87",
1947        devid => 0x8718,
1948        logdev => 0x04,
1949      },
1950      {
1951        name => "ITE IT8720F Super IO Sensors",
1952        driver => "it87",
1953        devid => 0x8720,
1954        logdev => 0x04,
1955      },
1956      {
1957        name => "ITE IT8726F Super IO Sensors",
1958        driver => "it87",
1959        devid => 0x8726,
1960        logdev => 0x04,
1961      },
1962    ],
1963  },
1964);
1965
1966# Drivers for CPU embedded sensors
1967# Each entry must have the following fields:
1968#  name: The CPU family name
1969#  driver: The driver name. Put "to-be-written" if no driver is available.
1970#  detect: Detection callback function. No parameter will be passed to
1971#          this function, it must use global lists of PCI devices, CPU,
1972#          etc. It must return a confidence value, undef if no supported
1973#          CPU is found.
1974@cpu_ids = (
1975  {
1976    name => "Silicon Integrated Systems SIS5595",
1977    driver => "sis5595",
1978    detect => sub { sis5595_pci_detect(); },
1979  },
1980  {
1981    name => "VIA VT82C686 Integrated Sensors",
1982    driver => "via686a",
1983    detect => sub { via686a_pci_detect(); },
1984  },
1985  {
1986    name => "VIA VT8231 Integrated Sensors",
1987    driver => "vt8231",
1988    detect => sub { via8231_pci_detect(); },
1989  },
1990  {
1991    name => "AMD K8 thermal sensors",
1992    driver => "k8temp",
1993    detect => sub { k8temp_pci_detect(); },
1994  },
1995  {
1996    name => "AMD K10 thermal sensors",
1997    driver => "to-be-written",
1998    detect => sub { k10temp_pci_detect(); },
1999  },
2000  {
2001    name => "Intel Core family thermal sensor",
2002    driver => "coretemp",
2003    detect => sub { coretemp_detect(); },
2004  },
2005  {
2006    name => "Intel AMB FB-DIMM thermal sensor",
2007    driver => "i5k_amb",
2008    detect => sub { intel_amb_detect(); },
2009  },
2010  {
2011    name => "VIA C7 thermal and voltage sensors",
2012    driver => "c7temp",
2013    detect => sub { c7temp_detect(); },
2014  },
2015);
2016
2017#######################
2018# AUXILIARY FUNCTIONS #
2019#######################
2020
2021sub swap_bytes
2022{
2023  return (($_[0] & 0xff00) >> 8) + (($_[0] & 0x00ff) << 8)
2024}
2025
2026# $_[0] is the sought value
2027# @_[1..] is the list to seek in
2028# Returns: 0 on failure, 1 if found.
2029# Note: Every use of this sub probably indicates the use of the wrong
2030#       datastructure
2031sub contains
2032{
2033  my $sought = shift;
2034  foreach (@_) {
2035    return 1 if $sought eq $_;
2036  }
2037  return 0;
2038}
2039
2040# Address can be decimal or hexadecimal
2041sub valid_address
2042{
2043  my $value = shift;
2044
2045  if ($value !~ m/^(0x[0-9a-f]+|[0-9]+)$/i) {
2046    print "$value is not a valid address, sorry.\n";
2047    exit -1;
2048  }
2049  $value = oct($value) if $value =~ /^0x/i;
2050
2051  return $value;
2052}
2053
2054sub parse_not_to_scan
2055{
2056  my ($min, $max, $to_parse) = @_;
2057  my @ranges = split /\s*, \s*/, $to_parse;
2058  my @res;
2059  my $range;
2060  foreach $range (@ranges) {
2061    my ($start, $end) = split /\s*-\s*/, $range;
2062    $start = valid_address($start);
2063    if (defined $end) {
2064      $end = valid_address($end);
2065      if ($end <= $start) {
2066        print "$start-$end is not a valid range, sorry.\n";
2067        exit -1;
2068      }
2069      $start = $min if $start < $min;
2070      $end = $max if $end > $max;
2071      push @res, ($start..$end);
2072    } else {
2073      push @res, $start if $start >= $min and $start <= $max;
2074    }
2075  }
2076  return sort { $a <=> $b } @res;
2077}
2078
2079# @_[0]: Reference to list 1
2080# @_[1]: Reference to list 2
2081# Result: 0 if they have no elements in common, 1 if they have
2082# Elements must be numeric.
2083sub any_list_match
2084{
2085  my ($list1, $list2) = @_;
2086  my ($el1, $el2);
2087  foreach $el1 (@$list1) {
2088    foreach $el2 (@$list2) {
2089      return 1 if $el1 == $el2;
2090    }
2091  }
2092  return 0;
2093}
2094
2095###################
2096# I/O port access #
2097###################
2098
2099sub initialize_ioports
2100{
2101  sysopen(IOPORTS, "/dev/port", O_RDWR)
2102    or die "/dev/port: $!\n";
2103  binmode(IOPORTS);
2104}
2105
2106sub close_ioports
2107{
2108  close(IOPORTS)
2109    or print "Warning: $!\n";
2110}
2111
2112# $_[0]: port to read
2113# Returns: -1 on failure, read value on success.
2114sub inb
2115{
2116  my ($res, $nrchars);
2117  sysseek(IOPORTS, $_[0], 0) or return -1;
2118  $nrchars = sysread(IOPORTS, $res, 1);
2119  return -1 if not defined $nrchars or $nrchars != 1;
2120  $res = unpack("C", $res);
2121  return $res;
2122}
2123
2124# $_[0]: port to write
2125# $_[1]: value to write
2126# Returns: -1 on failure, 0 on success.
2127sub outb
2128{
2129  my $towrite = pack("C", $_[1]);
2130  sysseek(IOPORTS, $_[0], 0) or return -1;
2131  my $nrchars = syswrite(IOPORTS, $towrite, 1);
2132  return -1 if not defined $nrchars or $nrchars != 1;
2133  return 0;
2134}
2135
2136# $_[0]: Address register
2137# $_[1]: Data register
2138# $_[2]: Register to read
2139# Returns: read value
2140sub isa_read_byte
2141{
2142  outb($_[0], $_[2]);
2143  return inb($_[1]);
2144}
2145
2146# $_[0]: Base address
2147# $_[1]: Register to read
2148# Returns: read value
2149# This one can be used for any ISA chip with index register at
2150# offset 5 and data register at offset 6.
2151sub isa_read_i5d6
2152{
2153  my ($addr, $reg) = @_;
2154  return isa_read_byte($addr + 5, $addr + 6, $reg);
2155}
2156
2157#################
2158# AUTODETECTION #
2159#################
2160
2161use vars qw($dev_i2c $sysfs_root);
2162
2163sub initialize_conf
2164{
2165  my $use_devfs = 0;
2166  open(local *INPUTFILE, "/proc/mounts") or die "Can't access /proc/mounts!";
2167  local $_;
2168  while (<INPUTFILE>) {
2169    if (m@^\w+ /dev devfs @) {
2170      $use_devfs = 1;
2171      $dev_i2c = '/dev/i2c/';
2172    }
2173    if (m@^\S+ (/\w+) sysfs @) {
2174      $sysfs_root = $1;
2175    }
2176  }
2177  close(INPUTFILE);
2178
2179  # We need sysfs for many things
2180  if (!defined $sysfs_root) {
2181      print "Sysfs not mounted?\n";
2182      exit -1;
2183  }
2184
2185  my $use_udev = 0;
2186  if (open(*INPUTFILE, '/etc/udev/udev.conf')) {
2187    while (<INPUTFILE>) {
2188      if (m/^\s*udev_db\s*=\s*\"([^"]*)\"/ || m/^\s*udev_db\s*=\s*(\S+)/) {
2189        if (-e $1) {
2190          $use_udev = 1;
2191          $dev_i2c = '/dev/i2c-';
2192        }
2193        last;
2194      }
2195    }
2196    close(INPUTFILE);
2197  }
2198
2199  if (!$use_udev) {
2200    # Try some known default udev db locations, just in case
2201    if (-e '/dev/.udev.tdb' || -e '/dev/.udev'
2202     || -e '/dev/.udevdb') {
2203      $use_udev = 1;
2204      $dev_i2c = '/dev/i2c-';
2205    }
2206  }
2207
2208  if (!($use_devfs || $use_udev)) {
2209    if (! -c '/dev/i2c-0' && -x '/sbin/MAKEDEV') {
2210      system("/sbin/MAKEDEV i2c");
2211    }
2212    if (! -c '/dev/i2c-0' && -x '/dev/MAKEDEV') {
2213      system("/dev/MAKEDEV i2c");
2214    }
2215    if (-c '/dev/i2c-0') {
2216      $dev_i2c = '/dev/i2c-';
2217    } else { # default
2218      print "No i2c device files found.\n";
2219      exit -1;
2220    }
2221  }
2222}
2223
2224# [0] -> VERSION
2225# [1] -> PATCHLEVEL
2226# [2] -> SUBLEVEL
2227# [3] -> EXTRAVERSION
2228#
2229use vars qw(@kernel_version $kernel_arch);
2230
2231sub initialize_kernel_version
2232{
2233  `uname -r` =~ /(\d+)\.(\d+)\.(\d+)(.*)/;
2234  @kernel_version = ($1, $2, $3, $4);
2235  chomp($kernel_arch = `uname -m`);
2236
2237  # We only support kernels >= 2.6.0
2238  if (!kernel_version_at_least(2, 6, 0)) {
2239      print "Kernel version is unsupported (too old, >= 2.6.0 needed)\n";
2240      exit -1;
2241  }
2242}
2243
2244sub kernel_version_at_least
2245{
2246  my ($vers, $plvl, $slvl) = @_;
2247  return 1 if ($kernel_version[0]  > $vers ||
2248                ($kernel_version[0] == $vers &&
2249                  ($kernel_version[1]  > $plvl ||
2250                    ($kernel_version[1] == $plvl &&
2251                      ($kernel_version[2] >= $slvl)))));
2252  return 0;
2253}
2254
2255# @cpu is a list of reference to hashes, one hash per CPU.
2256# Each entry has the following keys: vendor_id, cpu family, model,
2257# model name and stepping, directly taken from /proc/cpuinfo.
2258use vars qw(@cpu);
2259
2260sub initialize_cpu_list
2261{
2262  open(local *INPUTFILE, "/proc/cpuinfo") or die "Can't access /proc/cpuinfo!";
2263  local $_;
2264  my $entry;
2265
2266  while (<INPUTFILE>) {
2267    if (m/^processor\s*:\s*(\d+)/) {
2268      push @cpu, $entry if scalar keys(%{$entry}); # Previous entry
2269      $entry = {}; # New entry
2270      next;
2271    }
2272    if (m/^(vendor_id|cpu family|model|model name|stepping)\s*:\s*(.+)$/) {
2273      my $k = $1;
2274      my $v = $2;
2275      $v =~ s/\s+/ /g;  # Merge multiple spaces
2276      $v =~ s/ $//;     # Trim trailing space
2277      $entry->{$k} = $v;
2278      next;
2279    }
2280  }
2281  push @cpu, $entry if scalar keys(%{$entry}); # Last entry
2282  close(INPUTFILE);
2283}
2284
2285# @i2c_adapters is a list of references to hashes, one hash per I2C/SMBus
2286# adapter present on the system. Each entry has the following keys: name
2287# (directly taken from /sys/class/i2c-adapter) and driver.
2288use vars qw(@i2c_adapters);
2289
2290sub initialize_i2c_adapters_list
2291{
2292  my $entry;
2293  local $_;
2294
2295  my $class_dir = "${sysfs_root}/class/i2c-adapter";
2296  opendir(local *ADAPTERS, $class_dir) or return;
2297
2298  while (defined($_ = readdir(ADAPTERS))) {
2299    next unless m/^i2c-(\d+)$/;
2300    $entry = {}; # New entry
2301    $entry->{'name'} = sysfs_device_attribute("${class_dir}/i2c-$1", "name")
2302                    || sysfs_device_attribute("${class_dir}/i2c-$1/device", "name");
2303    next if $entry->{'name'} eq "ISA main adapter";
2304
2305    # First try to get the I2C adapter driver name from sysfs, and if it
2306    # fails, fall back to searching our list of known I2C adapters.
2307    $entry->{'driver'} = sysfs_device_driver("${class_dir}/i2c-$1/device")
2308                      || find_i2c_adapter_driver($entry->{'name'})
2309                      || 'UNKNOWN';
2310    print $entry->{'driver'}, "\n";
2311    $i2c_adapters[$1] = $entry;
2312  }
2313  closedir(ADAPTERS);
2314}
2315
2316###########
2317# MODULES #
2318###########
2319
2320use vars qw(%modules_list %modules_supported);
2321
2322sub initialize_modules_list
2323{
2324  open(local *INPUTFILE, "/proc/modules") or return;
2325  local $_;
2326  while (<INPUTFILE>) {
2327    tr/_/-/;
2328    $modules_list{$1} = 1 if m/^(\S*)/;
2329  }
2330}
2331
2332sub initialize_modules_supported
2333{
2334  foreach my $chip (@chip_ids) {
2335    $modules_supported{$chip->{driver}}++;
2336  }
2337}
2338
2339#################
2340# SYSFS HELPERS #
2341#################
2342
2343# From a sysfs device path, return the driver (module) name, or undef
2344sub sysfs_device_driver($)
2345{
2346  my $device = shift;
2347
2348  my $link = readlink("$device/driver/module");
2349  return unless defined $link;
2350  return basename($link);
2351}
2352
2353# From a sysfs device path and an attribute name, return the attribute
2354# value, or undef
2355sub sysfs_device_attribute($$)
2356{
2357  my ($device, $attr) = @_;
2358  my $value;
2359
2360  open(local *FILE, "$device/$attr") or return;
2361  return unless defined($value = <FILE>);
2362  close(FILE);
2363
2364  chomp($value);
2365  return $value;
2366}
2367
2368##############
2369# PCI ACCESS #
2370##############
2371
2372use vars qw(%pci_list);
2373
2374# This function returns a list of hashes. Each hash has some PCI information:
2375# 'domain', 'bus', 'slot' and 'func' uniquely identify a PCI device in a
2376# computer; 'vendid' and 'devid' uniquely identify a type of device.
2377# 'class' lets us spot unknown SMBus adapters.
2378sub read_sys_dev_pci($)
2379{
2380  my $devices = shift;
2381  my ($dev, @pci_list);
2382
2383  opendir(local *DEVICES, "$devices")
2384    or die "$devices: $!";
2385
2386  while (defined($dev = readdir(DEVICES))) {
2387    my %record;
2388    next unless $dev =~
2389      m/^(?:([\da-f]+):)?([\da-f]+):([\da-f]+)\.([\da-f]+)$/;
2390
2391    $record{domain} = hex $1;
2392    $record{bus} = hex $2;
2393    $record{slot} = hex $3;
2394    $record{func} = hex $4;
2395
2396    $record{vendid} = oct sysfs_device_attribute("$devices/$dev", "vendor");
2397    $record{devid} = oct sysfs_device_attribute("$devices/$dev", "device");
2398    $record{class} = (oct sysfs_device_attribute("$devices/$dev", "class"))
2399                     >> 8;
2400
2401    push @pci_list, \%record;
2402  }
2403
2404  return \@pci_list;
2405}
2406
2407sub initialize_pci
2408{
2409  my $pci_list;
2410
2411  $pci_list = read_sys_dev_pci("$sysfs_root/bus/pci/devices");
2412
2413  # Note that we lose duplicate devices at this point, but we don't
2414  # really care. What matters to us is which unique devices are present,
2415  # not how many of each.
2416  %pci_list = map {
2417    sprintf("%04x:%04x", $_->{vendid}, $_->{devid}) => $_
2418  } @{$pci_list};
2419}
2420
2421#####################
2422# ADAPTER DETECTION #
2423#####################
2424
2425sub adapter_pci_detection_sis_96x
2426{
2427  my $driver = "";
2428
2429  # first, determine which driver if any...
2430  if (exists $pci_list{"1039:0016"}) {
2431    $driver = "i2c-sis96x";
2432  } elsif (exists $pci_list{"1039:0008"}) {
2433    $driver = "i2c-sis5595";
2434  }
2435
2436  # then, add the appropriate entries to @pci_adapters
2437  if ($driver eq "i2c-sis5595") {
2438    push @pci_adapters, @pci_adapters_sis5595;
2439  } elsif ($driver eq "i2c-sis96x") {
2440    push @pci_adapters, @pci_adapters_sis96x;
2441  }
2442}
2443
2444# Build and return a PCI device's bus ID
2445sub pci_busid($)
2446{
2447  my $device = shift;
2448  my $busid;
2449
2450  $busid = sprintf("\%02x:\%02x.\%x",
2451                   $device->{bus}, $device->{slot}, $device->{func});
2452  $busid = sprintf("\%04x:", $device->{domain}) . $busid
2453    if defined $device->{domain};
2454
2455  return $busid;
2456}
2457
2458sub adapter_pci_detection
2459{
2460  my ($key, $device, $try, @res, %smbus);
2461  print "Probing for PCI bus adapters...\n";
2462
2463  # Custom detection routine for some SiS chipsets
2464  adapter_pci_detection_sis_96x();
2465
2466  # Build a list of detected SMBus devices
2467  foreach $key (keys %pci_list) {
2468    $device = $pci_list{$key};
2469    $smbus{$key}++
2470      if exists $device->{class} && $device->{class} == 0x0c05; # SMBus
2471  }
2472
2473  # Loop over the known I2C/SMBus adapters
2474  foreach $try (@pci_adapters) {
2475    $key = sprintf("%04x:%04x", $try->{vendid}, $try->{devid});
2476    if (exists $pci_list{$key}) {
2477        $device = $pci_list{$key};
2478        if ($try->{driver} =~ m/^to-be-/) {
2479          printf "No known driver for device \%s: \%s\n",
2480                 pci_busid($device), $try->{procid};
2481
2482          if ($try->{driver} eq "to-be-tested") {
2483            print "\nWe are currently looking for testers for this adapter!\n".
2484                  "Please check http://www.lm-sensors.org/wiki/Devices\n".
2485                  "and/or contact us if you want to help.\n\n";
2486            print "Continue... ";
2487            <STDIN>;
2488            print "\n";
2489          }
2490        } else {
2491          printf "Use driver `\%s' for device \%s: \%s\n",
2492                 $try->{driver}, pci_busid($device), $try->{procid};
2493          push @res, $try->{driver};
2494        }
2495
2496        # Delete from detected SMBus device list
2497        delete $smbus{$key};
2498    }
2499  }
2500
2501  # Now see if there are unknown SMBus devices left
2502  foreach $key (keys %smbus) {
2503    $device = $pci_list{$key};
2504    printf "Found unknown SMBus adapter \%04x:\%04x at \%s.\n",
2505           $device->{vendid}, $device->{devid}, pci_busid($device);
2506  }
2507
2508  if (! @res) {
2509    print "Sorry, no supported PCI bus adapters found.\n";
2510  }
2511  return @res;
2512}
2513
2514# $_[0]: Adapter description as found in /sys/class/i2c-adapter
2515sub find_i2c_adapter_driver
2516{
2517        my $name = shift;
2518        my $entry;
2519
2520        foreach $entry (@i2c_adapter_names) {
2521                return $entry->{driver}
2522                        if $name =~ $entry->{match};
2523        }
2524}
2525
2526#############################
2527# I2C AND SMBUS /DEV ACCESS #
2528#############################
2529
2530# This should really go into a separate module/package.
2531
2532# These are copied from <linux/i2c-dev.h>
2533
2534use constant IOCTL_I2C_SLAVE    => 0x0703;
2535use constant IOCTL_I2C_FUNCS    => 0x0705;
2536use constant IOCTL_I2C_SMBUS    => 0x0720;
2537
2538use constant SMBUS_READ         => 1;
2539use constant SMBUS_WRITE        => 0;
2540
2541use constant SMBUS_QUICK        => 0;
2542use constant SMBUS_BYTE         => 1;
2543use constant SMBUS_BYTE_DATA    => 2;
2544use constant SMBUS_WORD_DATA    => 3;
2545
2546use constant I2C_FUNC_SMBUS_QUICK       => 0x00010000;
2547use constant I2C_FUNC_SMBUS_READ_BYTE   => 0x00020000;
2548
2549# Get the i2c adapter's functionalities
2550# $_[0]: Reference to an opened filehandle
2551# Returns: -1 on failure, functionality bitfield on success.
2552sub i2c_get_funcs($)
2553{
2554  my $file = shift;
2555  my $funcs = pack("L", 0); # Allocate space
2556
2557  ioctl($file, IOCTL_I2C_FUNCS, $funcs) or return -1;
2558  $funcs = unpack("L", $funcs);
2559
2560  return $funcs;
2561}
2562
2563# Select the device to communicate with through its address.
2564# $_[0]: Reference to an opened filehandle
2565# $_[1]: Address to select
2566# Returns: 0 on failure, 1 on success.
2567sub i2c_set_slave_addr
2568{
2569  my ($file, $addr) = @_;
2570
2571  # Reset register data cache
2572  @i2c_byte_cache = ();
2573
2574  $addr += 0; # Make sure it's a number not a string
2575  ioctl($file, IOCTL_I2C_SLAVE, $addr) or return 0;
2576  return 1;
2577}
2578
2579# i2c_smbus_access is based upon the corresponding C function (see
2580# <linux/i2c-dev.h>). You should not need to call this directly.
2581# Exact calling conventions are intricate; read i2c-dev.c if you really need
2582# to know.
2583# $_[0]: Reference to an opened filehandle
2584# $_[1]: SMBUS_READ for reading, SMBUS_WRITE for writing
2585# $_[2]: Command (usually register number)
2586# $_[3]: Transaction kind (SMBUS_BYTE, SMBUS_BYTE_DATA, etc.)
2587# $_[4]: Reference to an array used for input/output of data
2588# Returns: 0 on failure, 1 on success.
2589# Note that we need to get back to Integer boundaries through the 'x2'
2590# in the pack. This is very compiler-dependent; I wish there was some other
2591# way to do this.
2592sub i2c_smbus_access
2593{
2594  my ($file, $read_write, $command, $size, $data) = @_;
2595  my $data_array = pack("C32", @$data);
2596  my $ioctl_data = pack("C2x2Ip", $read_write, $command, $size, $data_array);
2597  ioctl($file, IOCTL_I2C_SMBUS, $ioctl_data) or return 0;
2598  @{$_[4]} = unpack("C32", $data_array);
2599  return 1;
2600}
2601
2602# $_[0]: Reference to an opened filehandle
2603# Returns: -1 on failure, the read byte on success.
2604sub i2c_smbus_read_byte
2605{
2606  my ($file) = @_;
2607  my @data;
2608  i2c_smbus_access($file, SMBUS_READ, 0, SMBUS_BYTE, \@data)
2609         or return -1;
2610  return $data[0];
2611}
2612
2613# $_[0]: Reference to an opened filehandle
2614# $_[1]: Command byte (usually register number)
2615# Returns: -1 on failure, the read byte on success.
2616# Read byte data values are cached by default. As we keep reading the
2617# same registers over and over again in the detection functions, and
2618# SMBus can be slow, caching results in a big performance boost.
2619sub i2c_smbus_read_byte_data
2620{
2621  my ($file, $command, $nocache) = @_;
2622  my @data;
2623  if (!$nocache && exists $i2c_byte_cache[$command]) {
2624    return $i2c_byte_cache[$command];
2625  }
2626  i2c_smbus_access($file, SMBUS_READ, $command, SMBUS_BYTE_DATA, \@data)
2627         or return -1;
2628  return ($i2c_byte_cache[$command] = $data[0]);
2629}
2630
2631# $_[0]: Reference to an opened filehandle
2632# $_[1]: Command byte (usually register number)
2633# Returns: -1 on failure, the read word on success.
2634# Use this function with care, some devices don't like word reads,
2635# so you should do as much of the detection as possible using byte reads,
2636# and only start using word reads when there is a good chance that
2637# the detection will succeed.
2638# Note: some devices use the wrong endiannes; use swap_bytes to correct for
2639# this.
2640sub i2c_smbus_read_word_data
2641{
2642  my ($file, $command) = @_;
2643  my @data;
2644  i2c_smbus_access($file, SMBUS_READ, $command, SMBUS_WORD_DATA, \@data)
2645         or return -1;
2646  return $data[0] + 256 * $data[1];
2647}
2648
2649# $_[0]: Reference to an opened filehandle
2650# $_[1]: Address
2651# $_[2]: Functionalities of this i2c adapter
2652# Returns: 1 on successful probing, 0 else.
2653# This function is meant to prevent AT24RF08 corruption and write-only
2654# chips locks. This is done by choosing the best probing method depending
2655# on the address range.
2656sub i2c_probe($$$)
2657{
2658  my ($file, $addr, $funcs) = @_;
2659  my $data = [];
2660  if (($addr >= 0x50 && $addr <= 0x5F)
2661   || ($addr >= 0x30 && $addr <= 0x37)) {
2662    # This covers all EEPROMs we know of, including page protection addresses.
2663    # Note that some page protection addresses will not reveal themselves with
2664    # this, because they ack on write only, but this is probably better since
2665    # some EEPROMs write-protect themselves permanently on almost any write to
2666    # their page protection address.
2667    return 0 unless ($funcs & I2C_FUNC_SMBUS_READ_BYTE);
2668    return i2c_smbus_access($file, SMBUS_READ, 0, SMBUS_BYTE, $data);
2669  } else {
2670    return 0 unless ($funcs & I2C_FUNC_SMBUS_QUICK);
2671    return i2c_smbus_access($file, SMBUS_WRITE, 0, SMBUS_QUICK, $data);
2672  }
2673}
2674
2675# $_[0]: Reference to an opened file handle
2676# Returns: 1 if the device is safe to access, 0 else.
2677# This function is meant to prevent access to 1-register-only devices,
2678# which are designed to be accessed with SMBus receive byte and SMBus send
2679# byte transactions (i.e. short reads and short writes) and treat SMBus
2680# read byte as a real write followed by a read. The device detection
2681# routines would write random values to the chip with possibly very nasty
2682# results for the hardware. Note that this function won't catch all such
2683# chips, as it assumes that reads and writes relate to the same register,
2684# but that's the best we can do.
2685sub i2c_safety_check
2686{
2687  my ($file) = @_;
2688  my $data;
2689
2690  # First we receive a byte from the chip, and remember it.
2691  $data = i2c_smbus_read_byte($file);
2692  return 1 if ($data < 0);
2693
2694  # We receive a byte again; very likely to be the same for
2695  # 1-register-only devices.
2696  return 1 if (i2c_smbus_read_byte($file) != $data);
2697
2698  # Then we try a standard byte read, with a register offset equal to
2699  # the byte we received; we should receive the same byte value in return.
2700  return 1 if (i2c_smbus_read_byte_data($file, $data) != $data);
2701
2702  # Then we try a standard byte read, with a slightly different register
2703  # offset; we should again receive the same byte value in return.
2704  return 1 if (i2c_smbus_read_byte_data($file, $data ^ 1) != ($data ^ 1));
2705
2706  # Apprently this is a 1-register-only device, restore the original register
2707  # value and leave it alone.
2708  i2c_smbus_read_byte_data($file, $data);
2709  return 0;
2710}
2711
2712####################
2713# ADAPTER SCANNING #
2714####################
2715
2716use vars qw(@chips_detected);
2717
2718# We will build a complicated structure @chips_detected here, being:
2719# A list of
2720#  references to hashes
2721#    with field 'driver', being a string with the driver name for this chip;
2722#    with field 'detected'
2723#      being a reference to a list of
2724#        references to hashes of type 'detect_data';
2725#    with field 'misdetected'
2726#      being a reference to a list of
2727#        references to hashes of type 'detect_data'
2728
2729# Type detect_data:
2730# A hash
2731#   with field 'i2c_adap' containing an adapter string as appearing
2732#        in /sys/class/i2c-adapter (if this is an I2C detection)
2733#  with field 'i2c_devnr', contianing the /dev/i2c-* number of this
2734#       adapter (if this is an I2C detection)
2735#  with field 'i2c_driver', containing the driver name for this adapter
2736#       (if this is an I2C detection)
2737#  with field 'i2c_addr', containing the I2C address of the detection;
2738#       (if this is an I2C detection)
2739#  with field 'i2c_sub_addrs', containing a reference to a list of
2740#       other I2C addresses (if this is an I2C detection)
2741#  with field 'isa_addr' containing the ISA address this chip is on
2742#       (if this is an ISA detection)
2743#  with field 'conf', containing the confidence level of this detection
2744#  with field 'chipname', containing the chip name
2745
2746# This adds a detection to the above structure. We do no alias detection
2747# here; so you should do ISA detections *after* all I2C detections.
2748# Not all possibilities of i2c_addr and i2c_sub_addrs are exhausted.
2749# In all normal cases, it should be all right.
2750# $_[0]: chip driver
2751# $_[1]: reference to data hash
2752# Returns: Nothing
2753sub add_i2c_to_chips_detected
2754{
2755  my ($chipdriver, $datahash) = @_;
2756  my ($i, $new_detected_ref, $new_misdetected_ref, $detected_ref, $misdetected_ref,
2757      $main_entry, $detected_entry, $put_in_detected, @hash_addrs, @entry_addrs,
2758      $do_not_add);
2759
2760  # First determine where the hash has to be added.
2761  for ($i = 0; $i < @chips_detected; $i++) {
2762    last if ($chips_detected[$i]->{driver} eq $chipdriver);
2763  }
2764  if ($i == @chips_detected) {
2765    push @chips_detected, { driver => $chipdriver,
2766                            detected => [],
2767                            misdetected => [] };
2768  }
2769  $new_detected_ref = $chips_detected[$i]->{detected};
2770  $new_misdetected_ref = $chips_detected[$i]->{misdetected};
2771
2772  # Find out whether our new entry should go into the detected or the
2773  # misdetected list. We compare all i2c addresses; if at least one matches,
2774  # but our conf value is lower, we assume this is a misdetect.
2775  @hash_addrs = ($datahash->{i2c_addr});
2776  push @hash_addrs, @{$datahash->{i2c_sub_addrs}}
2777       if exists $datahash->{i2c_sub_addrs};
2778  $put_in_detected = 1;
2779  $do_not_add = 0;
2780  FIND_LOOP:
2781  foreach $main_entry (@chips_detected) {
2782    foreach $detected_entry (@{$main_entry->{detected}}) {
2783      @entry_addrs = ($detected_entry->{i2c_addr});
2784      push @entry_addrs, @{$detected_entry->{i2c_sub_addrs}}
2785               if exists $detected_entry->{i2c_sub_addrs};
2786      if ($detected_entry->{i2c_devnr} == $datahash->{i2c_devnr} and
2787          any_list_match(\@entry_addrs, \@hash_addrs)) {
2788        if ($detected_entry->{conf} >= $datahash->{conf}) {
2789          $put_in_detected = 0;
2790        }
2791        if ($chipdriver eq $main_entry->{driver}) {
2792          $do_not_add = 1;
2793        }
2794        last FIND_LOOP;
2795      }
2796    }
2797  }
2798
2799  if ($put_in_detected) {
2800    # Here, we move all entries from detected to misdetected which
2801    # match at least in one main or sub address. This may not be the
2802    # best idea to do, as it may remove detections without replacing
2803    # them with second-best ones. Too bad.
2804    # (Khali 2003-09-13) If the driver is the same, the "misdetected"
2805    # entry is simply deleted; failing to do so cause the configuration
2806    # lines generated later to look very confusing (the driver will
2807    # be told to ignore valid addresses).
2808    @hash_addrs = ($datahash->{i2c_addr});
2809    push @hash_addrs, @{$datahash->{i2c_sub_addrs}}
2810         if exists $datahash->{i2c_sub_addrs};
2811    foreach $main_entry (@chips_detected) {
2812      $detected_ref = $main_entry->{detected};
2813      $misdetected_ref = $main_entry->{misdetected};
2814      for ($i = @$detected_ref-1; $i >=0; $i--) {
2815        @entry_addrs = ($detected_ref->[$i]->{i2c_addr});
2816        push @entry_addrs, @{$detected_ref->[$i]->{i2c_sub_addrs}}
2817             if exists $detected_ref->[$i]->{i2c_sub_addrs};
2818        if ($detected_ref->[$i]->{i2c_devnr} == $datahash->{i2c_devnr} and
2819            any_list_match(\@entry_addrs, \@hash_addrs)) {
2820          push @$misdetected_ref, $detected_ref->[$i]
2821            unless $chipdriver eq $main_entry->{driver};
2822          splice @$detected_ref, $i, 1;
2823        }
2824      }
2825    }
2826
2827    # Now add the new entry to detected
2828    push @$new_detected_ref, $datahash;
2829  } else {
2830    # No hard work here
2831    push @$new_misdetected_ref, $datahash
2832      unless $do_not_add;
2833  }
2834}
2835
2836# This adds a detection to the above structure. We also do alias detection
2837# here; so you should do ISA detections *after* all I2C detections.
2838# $_[0]: alias detection function
2839# $_[1]: chip driver
2840# $_[2]: reference to data hash
2841# Returns: 0 if it is not an alias, datahash reference if it is.
2842sub add_isa_to_chips_detected
2843{
2844  my ($alias_detect, $chipdriver, $datahash) = @_;
2845  my ($i, $new_detected_ref, $new_misdetected_ref, $detected_ref, $misdetected_ref,
2846      $main_entry, $isalias);
2847
2848  # First determine where the hash has to be added.
2849  $isalias = 0;
2850  for ($i = 0; $i < @chips_detected; $i++) {
2851    last if ($chips_detected[$i]->{driver} eq $chipdriver);
2852  }
2853  if ($i == @chips_detected) {
2854    push @chips_detected, { driver => $chipdriver,
2855                            detected => [],
2856                            misdetected => [] };
2857  }
2858  $new_detected_ref = $chips_detected[$i]->{detected};
2859  $new_misdetected_ref = $chips_detected[$i]->{misdetected};
2860
2861  # Now, we are looking for aliases. An alias can only be the same chiptype.
2862  # If an alias is found in the misdetected list, we add the new information
2863  # and terminate this function. If it is found in the detected list, we
2864  # still have to check whether another chip has claimed this ISA address.
2865  # So we remove the old entry from the detected list and put it in datahash.
2866
2867  # Misdetected alias detection:
2868  for ($i = 0; $i < @$new_misdetected_ref; $i++) {
2869    if (exists $new_misdetected_ref->[$i]->{i2c_addr} and
2870        not exists $new_misdetected_ref->[$i]->{isa_addr} and
2871        defined $alias_detect and
2872        $new_misdetected_ref->[$i]->{chipname} eq $datahash->{chipname}) {
2873      open(local *FILE, "$dev_i2c$new_misdetected_ref->[$i]->{i2c_devnr}") or
2874        print("Can't open $dev_i2c$new_misdetected_ref->[$i]->{i2c_devnr}?!?\n"),
2875        next;
2876      binmode(FILE);
2877      i2c_set_slave_addr(\*FILE, $new_misdetected_ref->[$i]->{i2c_addr}) or
2878           print("Can't set I2C address for ",
2879                 "$dev_i2c$new_misdetected_ref->[$i]->{i2c_devnr}?!?\n"),
2880           next;
2881      if (&$alias_detect ($datahash->{isa_addr}, \*FILE,
2882                          $new_misdetected_ref->[$i]->{i2c_addr})) {
2883        $new_misdetected_ref->[$i]->{isa_addr} = $datahash->{isa_addr};
2884        return $new_misdetected_ref->[$i];
2885      }
2886    }
2887  }
2888
2889  # Detected alias detection:
2890  for ($i = 0; $i < @$new_detected_ref; $i++) {
2891    if (exists $new_detected_ref->[$i]->{i2c_addr} and
2892        not exists $new_detected_ref->[$i]->{isa_addr} and
2893        defined $alias_detect and
2894        $new_detected_ref->[$i]->{chipname} eq $datahash->{chipname}) {
2895      open(local *FILE, "$dev_i2c$new_detected_ref->[$i]->{i2c_devnr}") or
2896        print("Can't open $dev_i2c$new_detected_ref->[$i]->{i2c_devnr}?!?\n"),
2897        next;
2898      binmode(FILE);
2899      i2c_set_slave_addr(\*FILE, $new_detected_ref->[$i]->{i2c_addr}) or
2900           print("Can't set I2C address for ",
2901                 "$dev_i2c$new_detected_ref->[$i]->{i2c_devnr}?!?\n"),
2902           next;
2903      if (&$alias_detect ($datahash->{isa_addr}, \*FILE,
2904                          $new_detected_ref->[$i]->{i2c_addr})) {
2905        $new_detected_ref->[$i]->{isa_addr} = $datahash->{isa_addr};
2906        ($datahash) = splice (@$new_detected_ref, $i, 1);
2907        $isalias = 1;
2908        last;
2909      }
2910    }
2911  }
2912
2913
2914  # Find out whether our new entry should go into the detected or the
2915  # misdetected list. We only compare main isa_addr here, of course.
2916  # (Khali 2004-05-12) If the driver is the same, the "misdetected"
2917  # entry is simply deleted; same we do for I2C chips.
2918  foreach $main_entry (@chips_detected) {
2919    $detected_ref = $main_entry->{detected};
2920    $misdetected_ref = $main_entry->{misdetected};
2921    for ($i = 0; $i < @{$main_entry->{detected}}; $i++) {
2922      if (exists $detected_ref->[$i]->{isa_addr} and
2923          exists $datahash->{isa_addr} and
2924          $detected_ref->[$i]->{isa_addr} == $datahash->{isa_addr}) {
2925        if ($detected_ref->[$i]->{conf} >= $datahash->{conf}) {
2926          push @$new_misdetected_ref, $datahash
2927            unless $main_entry->{driver} eq $chipdriver;
2928        } else {
2929          push @$misdetected_ref, $detected_ref->[$i]
2930            unless $main_entry->{driver} eq $chipdriver;
2931          splice @$detected_ref, $i, 1;
2932          push @$new_detected_ref, $datahash;
2933        }
2934        if ($isalias) {
2935          return $datahash;
2936        } else {
2937          return 0;
2938        }
2939      }
2940    }
2941  }
2942
2943  # Not found? OK, put it in the detected list
2944  push @$new_detected_ref, $datahash;
2945  if ($isalias) {
2946    return $datahash;
2947  } else {
2948    return 0;
2949  }
2950}
2951
2952# From the list of known I2C/SMBus devices, build a list of I2C addresses
2953# which are worth probing. There's no point in probing an address for which
2954# we don't know a single device, and probing some addresses has caused
2955# random trouble in the past.
2956sub i2c_addresses_to_scan()
2957{
2958  my @used;
2959  my @addresses;
2960  my $addr;
2961
2962  foreach my $chip (@chip_ids) {
2963    next unless defined $chip->{'i2c_addrs'};
2964    next if $chip->{'driver'} eq 'not-a-sensor';
2965    foreach $addr (@{$chip->{'i2c_addrs'}}) {
2966      $used[$addr]++;
2967    }
2968  }
2969
2970  for ($addr = 0x03; $addr <= 0x77; $addr++) {
2971    push @addresses, $addr if $used[$addr];
2972  }
2973  return \@addresses;
2974}
2975
2976# $_[0]: The number of the adapter to scan
2977# $_[1]: The name of the adapter, as appearing in /sys/class/i2c-adapter
2978# $_[2]: The driver of the adapter
2979# @_[3]: Addresses not to scan (array reference)
2980sub scan_adapter
2981{
2982  my ($adapter_nr, $adapter_name, $adapter_driver, $not_to_scan) = @_;
2983  my ($funcs, $chip, $addr, $conf, @chips, $new_hash, $other_addr);
2984
2985  # As we modify it, we need a copy
2986  my @not_to_scan = @$not_to_scan;
2987
2988  open(local *FILE, "$dev_i2c$adapter_nr") or
2989    (print "Can't open $dev_i2c$adapter_nr\n"), return;
2990  binmode(FILE);
2991
2992  # Can we probe this adapter?
2993  $funcs = i2c_get_funcs(\*FILE);
2994  if ($funcs < 0) {
2995    print "Adapter failed to provide its functionalities, skipping.\n";
2996    return;
2997  }
2998  if (!($funcs & (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_READ_BYTE))) {
2999    print "Adapter cannot be probed, skipping.\n";
3000    return;
3001  }
3002  if (~$funcs & (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_READ_BYTE)) {
3003    print "Adapter doesn't support all probing functions.\n",
3004          "Some addresses won't be probed.\n";
3005  }
3006
3007  # Now scan each address in turn
3008  foreach $addr (@{$i2c_addresses_to_scan}) {
3009    # As the not_to_scan list is sorted, we can check it fast
3010    shift @not_to_scan # User skipped an address which we didn't intend to probe anyway
3011      while (@not_to_scan and $not_to_scan[0] < $addr);
3012    if (@not_to_scan and $not_to_scan[0] == $addr) {
3013      shift @not_to_scan;
3014      next;
3015    }
3016
3017    if (!i2c_set_slave_addr(\*FILE, $addr)) {
3018      # If the address is busy, we can normally find out which driver
3019      # requested it (if the kernel is recent enough, at least 2.6.16
3020      # and later are known to work), and we assume it is the right one.
3021      my ($device, $driver);
3022
3023      $device = sprintf("$sysfs_root/bus/i2c/devices/\%d-\%04x",
3024                             $adapter_nr, $addr);
3025      $driver = sysfs_device_driver($device);
3026
3027      if (defined($driver)) {
3028        $new_hash = {
3029          conf => 6, # Arbitrary confidence
3030          i2c_addr => $addr,
3031          chipname => sysfs_device_attribute($device, "name") || "unknown",
3032          i2c_adap => $adapter_name,
3033          i2c_driver => $adapter_driver,
3034          i2c_devnr => $adapter_nr,
3035        };
3036
3037        printf "Client found at address 0x\%02x\n", $addr;
3038        printf "Handled by driver `\%s' (already loaded), chip type `\%s'\n",
3039               $driver, $new_hash->{chipname};
3040
3041        # Only add it to the list if this is something we would have
3042        # detected, else we end up with random i2c chip drivers listed
3043        # (for example media/video drivers.)
3044        if (exists $modules_supported{$driver}) {
3045          add_i2c_to_chips_detected($driver, $new_hash);
3046        } else {
3047          print "    (note: this is probably NOT a sensor chip!)\n";
3048        }
3049      } else {
3050        printf("Client at address 0x%02x can not be probed - ".
3051               "unload all client drivers first!\n", $addr);
3052      }
3053      next;
3054    }
3055
3056    next unless i2c_probe(\*FILE, $addr, $funcs);
3057    printf "Client found at address 0x%02x\n", $addr;
3058    if (!i2c_safety_check(\*FILE)) {
3059      print "Seems to be a 1-register-only device, skipping.\n";
3060      next;
3061    }
3062
3063    $| = 1;
3064    foreach $chip (@chip_ids) {
3065      if (exists $chip->{i2c_addrs} and contains($addr, @{$chip->{i2c_addrs}})) {
3066        printf("\%-60s", sprintf("Probing for `\%s'... ", $chip->{name}));
3067        if (($conf, @chips) = &{$chip->{i2c_detect}} (\*FILE, $addr)) {
3068          if ($chip->{driver} eq "not-a-sensor") {
3069            print "Yes\n",
3070                  "    (confidence $conf, not a hardware monitoring chip";
3071          } else {
3072            print "Success!\n",
3073                  "    (confidence $conf, driver `$chip->{driver}'";
3074          }
3075          if (@chips) {
3076            print ", other addresses:";
3077            @chips = sort @chips;
3078            foreach $other_addr (@chips) {
3079              printf(" 0x%02x", $other_addr);
3080            }
3081          }
3082          printf ")\n";
3083
3084          next if ($chip->{driver} eq "not-a-sensor"
3085                || $chip->{driver} eq "use-isa-instead");
3086
3087          $new_hash = { conf => $conf,
3088                        i2c_addr => $addr,
3089                        chipname => $chip->{name},
3090                        i2c_adap => $adapter_name,
3091                        i2c_driver => $adapter_driver,
3092                        i2c_devnr => $adapter_nr,
3093                      };
3094          if (@chips) {
3095            my @chips_copy = @chips;
3096            $new_hash->{i2c_sub_addrs} = \@chips_copy;
3097          }
3098          add_i2c_to_chips_detected($chip->{driver}, $new_hash);
3099        } else {
3100          print "No\n";
3101        }
3102      }
3103    }
3104    $| = 0;
3105  }
3106}
3107
3108sub scan_isa_bus
3109{
3110  my ($chip, $addr, $conf);
3111  $| = 1;
3112  foreach $chip (@chip_ids) {
3113    next if not exists $chip->{isa_addrs} or not exists $chip->{isa_detect};
3114    foreach $addr (@{$chip->{isa_addrs}}) {
3115      printf("\%-60s", sprintf("Probing for `\%s'\ at 0x\%x... ", $chip->{name},
3116                               $addr));
3117      $conf = &{$chip->{isa_detect}} ($addr);
3118      print("No\n"), next if not defined $conf;
3119      print "Success!\n";
3120      printf "    (confidence %d, driver `%s')\n", $conf, $chip->{driver};
3121      my $new_hash = { conf => $conf,
3122                       isa_addr => $addr,
3123                       chipname => $chip->{name}
3124                     };
3125      $new_hash = add_isa_to_chips_detected($chip->{alias_detect}, $chip->{driver},
3126                                            $new_hash);
3127      if ($new_hash) {
3128        printf "    Alias of the chip on I2C bus `%s', address 0x%04x\n",
3129                        $new_hash->{i2c_adap}, $new_hash->{i2c_addr};
3130      }
3131    }
3132  }
3133  $| = 0;
3134}
3135
3136use vars qw(%superio);
3137
3138# The following are taken from the PNP ISA spec (so it's supposed
3139# to be common to all Super I/O chips):
3140#  devidreg: The device ID register(s)
3141#  logdevreg: The logical device register
3142#  actreg: The activation register within the logical device
3143#  actmask: The activation bit in the activation register
3144#  basereg: The I/O base register within the logical device
3145%superio = (
3146  devidreg => 0x20,
3147  logdevreg => 0x07,
3148  actreg => 0x30,
3149  actmask => 0x01,
3150  basereg => 0x60,
3151);
3152
3153sub exit_superio
3154{
3155  my ($addrreg, $datareg) = @_;
3156
3157  # Some chips (SMSC, Winbond) want this
3158  outb($addrreg, 0xaa);
3159
3160  # Return to "Wait For Key" state (PNP-ISA spec)
3161  outb($addrreg, 0x02);
3162  outb($datareg, 0x02);
3163}
3164
3165# Guess if an unknown Super-I/O chip has sensors
3166sub guess_superio_ld($$$)
3167{
3168  my ($addrreg, $datareg, $typical_addr) = @_;
3169  my ($oldldn, $ldn, $addr);
3170
3171  # Save logical device number
3172  outb($addrreg, $superio{logdevreg});
3173  $oldldn = inb($datareg);
3174
3175  for ($ldn = 0; $ldn < 16; $ldn++) {
3176    # Select logical device
3177    outb($addrreg, $superio{logdevreg});
3178    outb($datareg, $ldn);
3179
3180    # Read base I/O address
3181    outb($addrreg, $superio{basereg});
3182    $addr = inb($datareg) << 8;
3183    outb($addrreg, $superio{basereg} + 1);
3184    $addr |= inb($datareg);
3185    next unless ($addr & 0xfff8) == $typical_addr;
3186
3187    printf "    (logical device \%X has address 0x\%x, could be sensors)\n",
3188           $ldn, $addr;
3189    last;
3190  }
3191
3192  # Be nice, restore original logical device
3193  outb($addrreg, $superio{logdevreg});
3194  outb($datareg, $oldldn);
3195}
3196
3197sub probe_superio($$$)
3198{
3199  my ($addrreg, $datareg, $chip) = @_;
3200  my ($val, $addr);
3201
3202  printf "\%-60s",  "Found `$chip->{name}'";
3203
3204  # Does it have hardware monitoring capabilities?
3205  if (!exists $chip->{driver}) {
3206    print "\n    (no information available)\n";
3207    return;
3208  }
3209  if ($chip->{driver} eq "not-a-sensor") {
3210    print "\n    (no hardware monitoring capabilities)\n";
3211    return;
3212  }
3213  if ($chip->{driver} eq "via-smbus-only") {
3214    print "\n    (hardware monitoring capabilities accessible via SMBus only)\n";
3215    return;
3216  }
3217
3218  # Switch to the sensor logical device
3219  outb($addrreg, $superio{logdevreg});
3220  outb($datareg, $chip->{logdev});
3221
3222  # Check the activation register
3223  outb($addrreg, $superio{actreg});
3224  $val = inb($datareg);
3225  if (!($val & $superio{actmask})) {
3226    print "\n    (but not activated)\n";
3227    return;
3228  }
3229
3230  # Get the IO base register
3231  outb($addrreg, $superio{basereg});
3232  $addr = inb($datareg);
3233  outb($addrreg, $superio{basereg} + 1);
3234  $addr = ($addr << 8) | inb($datareg);
3235  if ($addr == 0) {
3236    print "\n    (but no address specified)\n";
3237    return;
3238  }
3239  print "Success!\n";
3240  printf "    (address 0x\%x, driver `%s')\n", $addr, $chip->{driver};
3241  my $new_hash = { conf => 9,
3242                   isa_addr => $addr,
3243                   chipname => $chip->{name}
3244                 };
3245  add_isa_to_chips_detected($chip->{alias_detect}, $chip->{driver},
3246                                        $new_hash);
3247}
3248
3249# Detection routine for non-standard SMSC Super I/O chips
3250# $_[0]: Super I/O LPC config/index port
3251# $_[1]: Super I/O LPC data port
3252# $_[2]: Reference to array of non-standard chips
3253# Return values: 1 if non-standard chip found, 0 otherwise
3254sub smsc_ns_detect_superio
3255{
3256    my ($addrreg, $datareg, $ns_chips) = @_;
3257    my ($val, $chip);
3258
3259    # read alternate device ID register
3260    outb($addrreg, 0x0d);
3261    $val = inb($datareg);
3262    if ($val == 0x00 || $val == 0xff) {
3263        return 0;
3264    }
3265
3266    print "Yes\n";
3267
3268    foreach $chip (@{$ns_chips}) {
3269        if ($chip->{devid} == $val) {
3270            probe_superio($addrreg, $datareg, $chip);
3271            return 1;
3272        }
3273    }
3274
3275    printf("Found unknown non-standard chip with ID 0x%02x\n", $val);
3276    return 1;
3277}
3278
3279sub scan_superio
3280{
3281  my ($addrreg, $datareg) = @_;
3282  my ($val, $found);
3283
3284  printf("Probing for Super-I/O at 0x\%x/0x\%x\n", $addrreg, $datareg);
3285
3286  $| = 1;
3287# reset state to avoid false positives
3288  exit_superio($addrreg, $datareg);
3289  FAMILY:
3290  foreach my $family (@superio_ids) {
3291    printf("\%-60s", "Trying family `$family->{family}'... ");
3292# write the password
3293    foreach $val (@{$family->{enter}->{$addrreg}}) {
3294      outb($addrreg, $val);
3295    }
3296# call the non-standard detection routine first if it exists
3297    if (defined($family->{ns_detect}) &&
3298        &{$family->{ns_detect}}($addrreg, $datareg, $family->{ns_chips})) {
3299      exit_superio($addrreg, $datareg);
3300      last FAMILY;
3301    }
3302# did it work?
3303    outb($addrreg, $superio{devidreg});
3304    $val = inb($datareg);
3305    outb($addrreg, $superio{devidreg} + 1);
3306    $val = ($val << 8) | inb($datareg);
3307    if ($val == 0x0000 || $val == 0xffff) {
3308      print "No\n";
3309      next FAMILY;
3310    }
3311    print "Yes\n";
3312
3313    $found = 0;
3314    foreach my $chip (@{$family->{chips}}) {
3315      if (($chip->{devid} > 0xff && ($val & ($chip->{devid_mask} || 0xffff)) == $chip->{devid})
3316       || ($chip->{devid} <= 0xff && ($val >> 8) == $chip->{devid})) {
3317        probe_superio($addrreg, $datareg, $chip);
3318        $found++;
3319      }
3320    }
3321
3322    if (!$found) {
3323      printf("Found unknown chip with ID 0x%04x\n", $val);
3324      # Guess if a logical device could correspond to sensors
3325      guess_superio_ld($addrreg, $datareg, $family->{guess})
3326        if defined $family->{guess};
3327    }
3328
3329    exit_superio($addrreg, $datareg);
3330    last FAMILY;
3331  }
3332  $| = 0;
3333}
3334
3335
3336sub scan_cpu($)
3337{
3338  my $entry = shift;
3339  my $confidence;
3340
3341  printf("\%-60s", "$entry->{name}... ");
3342  if (defined ($confidence = $entry->{detect}())) {
3343    print "Success!\n";
3344    printf "    (driver `%s')\n", $entry->{driver};
3345    my $new_hash = {
3346      conf => $confidence,
3347      chipname => $entry->{name},
3348    };
3349    add_isa_to_chips_detected(undef, $entry->{driver}, $new_hash);
3350  } else {
3351    print "No\n";
3352  }
3353}
3354
3355
3356##################
3357# CHIP DETECTION #
3358##################
3359
3360# This routine allows you to dynamically update the chip detection list.
3361# The most common use is to allow for different chip to driver mappings
3362# based on different linux kernels
3363sub chip_special_cases
3364{
3365        # Some chip to driver mappings depend on the environment
3366        foreach my $chip (@chip_ids) {
3367                if (ref($chip->{driver}) eq 'CODE') {
3368                        $chip->{driver} = $chip->{driver}->();
3369                }
3370        }
3371}
3372
3373# Each function returns a confidence value. The higher this value, the more
3374# sure we are about this chip. A Winbond W83781D, for example, will be
3375# detected as a LM78 too; but as the Winbond detection has a higher confidence
3376# factor, you should identify it as a Winbond.
3377
3378# Each function returns a list. The first element is the confidence value;
3379# Each element after it is an SMBus address. In this way, we can detect
3380# chips with several SMBus addresses. The SMBus address for which the
3381# function was called is never returned.
3382
3383# If there are devices which get confused if they are only read from, then
3384# this program will surely confuse them. But we guarantee never to write to
3385# any of these devices.
3386
3387
3388# $_[0]: A reference to the file descriptor to access this chip.
3389# $_[1]: Address
3390# Returns: undef if not detected, (3) if detected.
3391# Registers used: 0x58
3392sub mtp008_detect
3393{
3394  my ($file, $addr) = @_;
3395  return if i2c_smbus_read_byte_data($file, 0x58) != 0xac;
3396  return (3);
3397}
3398
3399# $_[0]: Chip to detect (0 = LM78, 1 = LM78-J, 2 = LM79)
3400# $_[1]: A reference to the file descriptor to access this chip.
3401# $_[2]: Address
3402# Returns: undef if not detected, (6) if detected.
3403# Registers used:
3404#   0x40: Configuration
3405#   0x48: Full I2C Address
3406#   0x49: Device ID
3407sub lm78_detect
3408{
3409  my $reg;
3410  my ($chip, $file, $addr) = @_;
3411  return unless i2c_smbus_read_byte_data($file, 0x48) == $addr;
3412  return unless (i2c_smbus_read_byte_data($file, 0x40) & 0x80) == 0x00;
3413  $reg = i2c_smbus_read_byte_data($file, 0x49);
3414  return unless ($chip == 0 and ($reg == 0x00 or $reg == 0x20)) or
3415                    ($chip == 1 and $reg == 0x40) or
3416                    ($chip == 2 and ($reg & 0xfe) == 0xc0);
3417
3418  # Explicitly prevent misdetection of Winbond chips
3419  $reg = i2c_smbus_read_byte_data($file, 0x4f);
3420  return if $reg == 0xa3 || $reg == 0x5c;
3421
3422  return (6);
3423}
3424
3425# $_[0]: Chip to detect (0 = LM78, 1 = LM78-J, 2 = LM79)
3426# $_[1]: Address
3427# Returns: undef if not detected, 6 if detected.
3428# Note: Only address 0x290 is scanned at this moment.
3429sub lm78_isa_detect
3430{
3431  my ($chip, $addr) = @_;
3432  my $val = inb($addr + 1);
3433  return if inb($addr + 2) != $val or inb($addr + 3) != $val or
3434            inb($addr + 7) != $val;
3435
3436  $val = inb($addr + 5);
3437  outb($addr + 5, ~$val & 0x7f);
3438  if ((inb($addr+5) & 0x7f) != (~ $val & 0x7f)) {
3439    outb($addr+5, $val);
3440    return;
3441  }
3442
3443  return unless (isa_read_i5d6($addr, 0x40) & 0x80) == 0x00;
3444  my $reg = isa_read_i5d6($addr, 0x49);
3445  return unless ($chip == 0 and ($reg == 0x00 or $reg == 0x20)) or
3446                ($chip == 1 and $reg == 0x40) or
3447                ($chip == 2 and ($reg & 0xfe) == 0xc0);
3448
3449  # Explicitly prevent misdetection of Winbond chips
3450  $reg = isa_read_i5d6($addr, 0x4f);
3451  return if $reg == 0xa3 || $reg == 0x5c;
3452
3453  # Explicitly prevent misdetection of ITE chips
3454  $reg = isa_read_i5d6($addr, 0x58);
3455  return if $reg == 0x90;
3456
3457  return 6;
3458}
3459
3460# $_[0]: Chip to detect (0 = LM75, 1 = DS75)
3461# $_[1]: A reference to the file descriptor to access this chip.
3462# $_[2]: Address (unused)
3463# Returns: undef if not detected, 3 or 6 if detected;
3464#   6 means that the temperatures make sense;
3465#   3 means that the temperatures look strange;
3466# Registers used:
3467#   0x00: Temperature
3468#   0x01: Configuration
3469#   0x02: Hysteresis
3470#   0x03: Overtemperature Shutdown
3471#   0x04-0x07: No registers
3472# The first detection step is based on the fact that the LM75 has only
3473# four registers, and cycles addresses over 8-byte boundaries. We use the
3474# 0x04-0x07 addresses (unused) to improve the reliability. These are not
3475# real registers and will always return the last returned value. This isn't
3476# documented.
3477# Note that register 0x00 may change, so we can't use the modulo trick on it.
3478# The DS75 is a bit different, it doesn't cycle over 8-byte boundaries, and
3479# all register addresses from 0x04 to 0x0f behave like 0x04-0x07 do for
3480# the LM75.
3481# Not all devices enjoy SMBus read word transactions, so we use read byte
3482# transactions even for the 16-bit registers. The low bits aren't very
3483# useful for detection anyway.
3484sub lm75_detect
3485{
3486  my $i;
3487  my ($chip, $file, $addr) = @_;
3488  my $cur = i2c_smbus_read_byte_data($file, 0x00);
3489  my $conf = i2c_smbus_read_byte_data($file, 0x01);
3490
3491  my $hyst = i2c_smbus_read_byte_data($file, 0x02, NO_CACHE);
3492  my $maxreg = $chip == 1 ? 0x0f : 0x07;
3493  for $i (0x04 .. $maxreg) {
3494    return if i2c_smbus_read_byte_data($file, $i, NO_CACHE) != $hyst;
3495  }
3496
3497  my $os = i2c_smbus_read_byte_data($file, 0x03, NO_CACHE);
3498  for $i (0x04 .. $maxreg) {
3499    return if i2c_smbus_read_byte_data($file, $i, NO_CACHE) != $os;
3500  }
3501
3502  if ($chip == 0) {
3503    for ($i = 8; $i <= 248; $i += 40) {
3504      return if i2c_smbus_read_byte_data($file, $i + 0x01) != $conf
3505             or i2c_smbus_read_byte_data($file, $i + 0x02) != $hyst
3506             or i2c_smbus_read_byte_data($file, $i + 0x03) != $os;
3507    }
3508  }
3509
3510  # All registers hold the same value, obviously a misdetection
3511  return if $conf == $cur and $cur == $hyst and $cur == $os;
3512
3513  # Unused bits
3514  return if $chip == 0 and ($conf & 0xe0);
3515  return if $chip == 1 and ($conf & 0x80);
3516
3517  # Most probable value ranges
3518  return 6 if $cur <= 100 and ($hyst >= 10 && $hyst <= 125)
3519    and ($os >= 20 && $os <= 127) and $hyst < $os;
3520  return 3;
3521}
3522
3523# $_[0]: A reference to the file descriptor to access this chip.
3524# $_[1]: Address
3525# Returns: undef if not detected, 3 or 6 if detected;
3526#   6 means that the temperatures make sense;
3527#   3 means that the temperatures look strange;
3528# Registers used:
3529#   0x00: Temperature
3530#   0x01: Configuration
3531#   0x02: Hysteresis
3532#   0x03: Overtemperature Shutdown
3533#   0x04: Low limit
3534#   0x05: High limit
3535#   0x06-0x07: No registers
3536# The first detection step is based on the fact that the LM77 has only
3537# six registers, and cycles addresses over 8-byte boundaries. We use the
3538# 0x06-0x07 addresses (unused) to improve the reliability. These are not
3539# real registers and will always return the last returned value. This isn't
3540# documented.
3541# Note that register 0x00 may change, so we can't use the modulo trick on it.
3542# Not all devices enjoy SMBus read word transactions, so we use read byte
3543# transactions even for the 16-bit registers at first. We only use read word
3544# transactions in the end when we are already almost certain that we have an
3545# LM77 chip.
3546sub lm77_detect
3547{
3548  my $i;
3549  my ($file, $addr) = @_;
3550  my $cur = i2c_smbus_read_byte_data($file, 0x00);
3551  my $conf = i2c_smbus_read_byte_data($file, 0x01);
3552  my $hyst = i2c_smbus_read_byte_data($file, 0x02);
3553  my $os = i2c_smbus_read_byte_data($file, 0x03);
3554
3555  my $low = i2c_smbus_read_byte_data($file, 0x04, NO_CACHE);
3556  return if i2c_smbus_read_byte_data($file, 0x06, NO_CACHE) != $low;
3557  return if i2c_smbus_read_byte_data($file, 0x07, NO_CACHE) != $low;
3558
3559  my $high = i2c_smbus_read_byte_data($file, 0x05, NO_CACHE);
3560  return if i2c_smbus_read_byte_data($file, 0x06, NO_CACHE) != $high;
3561  return if i2c_smbus_read_byte_data($file, 0x07, NO_CACHE) != $high;
3562
3563  for ($i = 8; $i <= 248; $i += 40) {
3564    return if i2c_smbus_read_byte_data($file, $i + 0x01) != $conf;
3565    return if i2c_smbus_read_byte_data($file, $i + 0x02) != $hyst;
3566    return if i2c_smbus_read_byte_data($file, $i + 0x03) != $os;
3567    return if i2c_smbus_read_byte_data($file, $i + 0x04) != $low;
3568    return if i2c_smbus_read_byte_data($file, $i + 0x05) != $high;
3569  }
3570
3571  # All registers hold the same value, obviously a misdetection
3572  return if $conf == $cur and $cur == $hyst
3573    and $cur == $os and $cur == $low and $cur == $high;
3574
3575  # Unused bits
3576  return if ($conf & 0xe0)
3577    or (($cur >> 4) != 0 && ($cur >> 4) != 0xf)
3578    or (($hyst >> 4) != 0 && ($hyst >> 4) != 0xf)
3579    or (($os >> 4) != 0 && ($os >> 4) != 0xf)
3580    or (($low >> 4) != 0 && ($low >> 4) != 0xf)
3581    or (($high >> 4) != 0 && ($high >> 4) != 0xf);
3582
3583  # Make sure the chip supports SMBus read word transactions
3584  $cur = i2c_smbus_read_word_data($file, 0x00);
3585  return if $cur < 0;
3586  $hyst = i2c_smbus_read_word_data($file, 0x02);
3587  return if $hyst < 0;
3588  $os = i2c_smbus_read_word_data($file, 0x03);
3589  return if $os < 0;
3590  $low = i2c_smbus_read_word_data($file, 0x04);
3591  return if $low < 0;
3592  $high = i2c_smbus_read_word_data($file, 0x05);
3593  return if $high < 0;
3594
3595  $cur /= 16;
3596  $hyst /= 16;
3597  $os /= 16;
3598  $high /= 16;
3599  $low /= 16;
3600
3601  # Most probable value ranges
3602  return 6 if $cur <= 100 and $hyst <= 40
3603    and ($os >= 20 && $os <= 127) and ($high >= 20 && $high <= 127);
3604  return 3;
3605}
3606
3607# $_[0]: Chip to detect (0 = LM92, 1 = LM76, 2 = MAX6633/MAX6634/MAX6635)
3608# $_[1]: A reference to the file descriptor to access this chip.
3609# $_[2]: Address
3610# Returns: undef if not detected, 2 or 4 if detected;
3611# Registers used:
3612#   0x01: Configuration (National Semiconductor only)
3613#   0x02: Hysteresis
3614#   0x03: Critical Temp
3615#   0x04: Low Limit
3616#   0x05: High Limit
3617#   0x07: Manufacturer ID (LM92 only)
3618# One detection step is based on the fact that the LM92 and clones have a
3619# limited number of registers, which cycle modulo 16 address values.
3620# Note that register 0x00 may change, so we can't use the modulo trick on it.
3621# Not all devices enjoy SMBus read word transactions, so we use read byte
3622# transactions even for the 16-bit registers at first. We only use read
3623# word transactions in the end when we are already almost certain that we
3624# have an LM92 chip or compatible.
3625sub lm92_detect
3626{
3627  my ($chip, $file, $addr) = @_;
3628
3629  my $conf = i2c_smbus_read_byte_data($file, 0x01);
3630  my $hyst = i2c_smbus_read_byte_data($file, 0x02);
3631  my $crit = i2c_smbus_read_byte_data($file, 0x03);
3632  my $low = i2c_smbus_read_byte_data($file, 0x04);
3633  my $high = i2c_smbus_read_byte_data($file, 0x05);
3634
3635  return if $conf == 0 and $hyst == 0 and $crit == 0
3636        and $low == 0 and $high == 0;
3637
3638  # Unused bits
3639  return if ($chip == 0 || $chip == 1)
3640        and ($conf & 0xE0);
3641
3642  for (my $i = 0; $i <= 240; $i += 16) {
3643    return if i2c_smbus_read_byte_data($file, $i + 0x01) != $conf;
3644    return if i2c_smbus_read_byte_data($file, $i + 0x02) != $hyst;
3645    return if i2c_smbus_read_byte_data($file, $i + 0x03) != $crit;
3646    return if i2c_smbus_read_byte_data($file, $i + 0x04) != $low;
3647    return if i2c_smbus_read_byte_data($file, $i + 0x05) != $high;
3648  }
3649
3650  return if $chip == 0
3651        and i2c_smbus_read_word_data($file, 0x07) != 0x0180;
3652
3653  # Make sure the chip supports SMBus read word transactions
3654  $hyst = i2c_smbus_read_word_data($file, 0x02);
3655  return if $hyst < 0;
3656  $crit = i2c_smbus_read_word_data($file, 0x03);
3657  return if $crit < 0;
3658  $low = i2c_smbus_read_word_data($file, 0x04);
3659  return if $low < 0;
3660  $high = i2c_smbus_read_word_data($file, 0x05);
3661  return if $high < 0;
3662
3663  foreach my $temp ($hyst, $crit, $low, $high) {
3664    return if $chip == 2 and ($temp & 0x7F00);
3665    return if $chip != 2 and ($temp & 0x0700);
3666  }
3667
3668  return 4 if $chip == 0;
3669  return 2;
3670}
3671
3672# $_[0]: A reference to the file descriptor to access this chip.
3673# $_[1]: Address
3674# Returns: undef if not detected, 3 if detected
3675# Registers used:
3676#   0xAA: Temperature
3677#   0xA1: High limit
3678#   0xA2: Low limit
3679#   0xA8: Counter
3680#   0xA9: Slope
3681#   0xAC: Configuration
3682# Detection is weak. We check if bit 4 (NVB) is clear, because it is
3683# unlikely to be set (would mean that EEPROM is currently being accessed).
3684# We also check the value of the counter and slope registers, the datasheet
3685# doesn't mention the possible values but the conversion formula together
3686# with experimental evidence suggest possible sanity checks.
3687# Not all devices enjoy SMBus read word transactions, so we do as much as
3688# possible with read byte transactions first, and only use read word
3689# transactions second.
3690sub ds1621_detect
3691{
3692  my ($file, $addr) = @_;
3693
3694  my $conf = i2c_smbus_read_byte_data($file, 0xAC);
3695  return if ($conf & 0x10);
3696
3697  my $counter = i2c_smbus_read_byte_data($file, 0xA8);
3698  my $slope = i2c_smbus_read_byte_data($file, 0xA9);
3699  return if ($slope != 0x10 || $counter > $slope);
3700
3701  my $temp = i2c_smbus_read_word_data($file, 0xAA);
3702  return if $temp < 0 || ($temp & 0x0f00);
3703  # On the DS1631, the following two checks are too strict in theory,
3704  # but in practice I very much doubt that anyone will set temperature
3705  # limits not a multiple of 0.5 degrees C.
3706  my $high = i2c_smbus_read_word_data($file, 0xA1);
3707  return if $high < 0 || ($high & 0x7f00);
3708  my $low = i2c_smbus_read_word_data($file, 0xA2);
3709  return if $low < 0 || ($low & 0x7f00);
3710
3711  return if ($temp == 0 && $high == 0 && $low == 0 && $conf == 0);
3712
3713  return 3;
3714}
3715
3716# $_[0]: A reference to the file descriptor to access this chip.
3717# $_[1]: Address
3718# Returns: undef if not detected, 1 to 3 if detected.
3719# Registers used:
3720#   0x00: Configuration register
3721#   0x02: Interrupt state register
3722#   0x2a-0x3d: Limits registers
3723# This one is easily misdetected since it doesn't provide identification
3724# registers. So we have to use some tricks:
3725#   - 6-bit addressing, so limits readings modulo 0x40 should be unchanged
3726#   - positive temperature limits
3727#   - limits order correctness
3728# Hopefully this should limit the rate of false positives, without increasing
3729# the rate of false negatives.
3730# Thanks to Lennard Klein for testing on a non-LM80 chip, which was
3731# previously misdetected, and isn't anymore. For reference, it scored
3732# a final confidence of 0, and changing from strict limit comparisons
3733# to loose comparisons did not change the score.
3734sub lm80_detect
3735{
3736  my ($i, $reg);
3737  my ($file, $addr) = @_;
3738
3739  return if (i2c_smbus_read_byte_data($file, 0x00) & 0x80) != 0;
3740  return if (i2c_smbus_read_byte_data($file, 0x02) & 0xc0) != 0;
3741
3742  for ($i = 0x2a; $i <= 0x3d; $i++) {
3743    $reg = i2c_smbus_read_byte_data($file, $i);
3744    return if i2c_smbus_read_byte_data($file, $i+0x40) != $reg;
3745    return if i2c_smbus_read_byte_data($file, $i+0x80) != $reg;
3746    return if i2c_smbus_read_byte_data($file, $i+0xc0) != $reg;
3747  }
3748
3749  # Refine a bit by checking wether limits are in the correct order
3750  # (min<max for voltages, hyst<max for temperature). Since it is still
3751  # possible that the chip is an LM80 with limits not properly set,
3752  # a few "errors" are tolerated.
3753  my $confidence = 0;
3754  for ($i = 0x2a; $i <= 0x3a; $i++) {
3755    $confidence++
3756      if i2c_smbus_read_byte_data($file, $i) < i2c_smbus_read_byte_data($file, $i+1);
3757  }
3758  # hot temp<OS temp
3759  $confidence++
3760    if i2c_smbus_read_byte_data($file, 0x38) < i2c_smbus_read_byte_data($file, 0x3a);
3761
3762  # Negative temperature limits are unlikely.
3763  for ($i = 0x3a; $i <= 0x3d; $i++) {
3764    $confidence++ if (i2c_smbus_read_byte_data($file, $i) & 0x80) == 0;
3765  }
3766
3767  # $confidence is between 0 and 14
3768  $confidence = ($confidence >> 1) - 4;
3769  # $confidence is now between -4 and 3
3770
3771  return unless $confidence > 0;
3772
3773  return $confidence;
3774}
3775
3776# $_[0]: Chip to detect
3777#   (0 = LM82/LM83)
3778# $_[1]: A reference to the file descriptor to access this chip.
3779# $_[2]: Address
3780# Returns: undef if not detected, 4 to 8 if detected.
3781# Registers used:
3782#   0x02: Status 1
3783#   0x03: Configuration
3784#   0x04: Company ID of LM84
3785#   0x35: Status 2
3786#   0xfe: Manufacturer ID
3787#   0xff: Chip ID / die revision
3788# We can use the LM84 Company ID register because the LM83 and the LM82 are
3789# compatible with the LM84.
3790# The LM83 chip ID is missing from the datasheet and was contributed by
3791# Magnus Forsstrom: 0x03.
3792# At least some revisions of the LM82 seem to be repackaged LM83, so they
3793# have the same chip ID, and temp2/temp4 will be stuck in "OPEN" state.
3794# For this reason, we don't even try to distinguish between both chips.
3795# Thanks to Ben Gardner for reporting.
3796sub lm83_detect
3797{
3798  my ($chip, $file) = @_;
3799  return if i2c_smbus_read_byte_data($file, 0xfe) != 0x01;
3800  my $chipid = i2c_smbus_read_byte_data($file, 0xff);
3801  return if $chipid != 0x01 && $chipid != 0x03;
3802
3803  my $confidence = 4;
3804  $confidence++
3805    if (i2c_smbus_read_byte_data($file, 0x02) & 0xa8) == 0x00;
3806  $confidence++
3807    if (i2c_smbus_read_byte_data($file, 0x03) & 0x41) == 0x00;
3808  $confidence++
3809    if i2c_smbus_read_byte_data($file, 0x04) == 0x00;
3810  $confidence++
3811    if (i2c_smbus_read_byte_data($file, 0x35) & 0x48) == 0x00;
3812
3813  return $confidence;
3814}
3815
3816# $_[0]: Chip to detect
3817#   (0 = LM90, 1 = LM89/LM99, 2 = LM86, 3 = ADM1032, 4 = MAX6654/MAX6690,
3818#    5 = ADT7461, 6 = MAX6646/MAX6647/MAX6648/MAX6649/MAX6692,
3819#    7 = MAX6680/MAX6681, 8 = W83L771W/G, 9 = TMP401, 10 = TMP411)
3820# $_[1]: A reference to the file descriptor to access this chip.
3821# $_[2]: Address
3822# Returns: undef if not detected, 6 or 8 if detected.
3823# Registers used:
3824#   0x03: Configuration
3825#   0x04: Conversion rate
3826#   0xfe: Manufacturer ID
3827#   0xff: Chip ID / die revision
3828sub lm90_detect
3829{
3830  my ($chip, $file, $addr) = @_;
3831  my $mid = i2c_smbus_read_byte_data($file, 0xfe);
3832  my $cid = i2c_smbus_read_byte_data($file, 0xff);
3833  my $conf = i2c_smbus_read_byte_data($file, 0x03);
3834  my $rate = i2c_smbus_read_byte_data($file, 0x04);
3835
3836  if ($chip == 0) {
3837    return if ($conf & 0x2a) != 0;
3838    return if $rate > 0x09;
3839    return if $mid != 0x01;     # National Semiconductor
3840    return 8 if $cid == 0x21;   # LM90
3841    return 6 if ($cid & 0x0f) == 0x20;
3842  }
3843  if ($chip == 1) {
3844    return if ($conf & 0x2a) != 0;
3845    return if $rate > 0x09;
3846    return if $mid != 0x01;     # National Semiconductor
3847    return 8 if $addr == 0x4c and $cid == 0x31; # LM89/LM99
3848    return 8 if $addr == 0x4d and $cid == 0x34; # LM89-1/LM99-1
3849    return 6 if ($cid & 0x0f) == 0x30;
3850  }
3851  if ($chip == 2) {
3852    return if ($conf & 0x2a) != 0;
3853    return if $rate > 0x09;
3854    return if $mid != 0x01;     # National Semiconductor
3855    return 8 if $cid == 0x11;   # LM86
3856    return 6 if ($cid & 0xf0) == 0x10;
3857  }
3858  if ($chip == 3) {
3859    return if ($conf & 0x3f) != 0;
3860    return if $rate > 0x0a;
3861    return if $mid != 0x41;     # Analog Devices
3862    return 6 if ($cid & 0xf0) == 0x40; # ADM1032
3863  }
3864  if ($chip == 4) {
3865    return if ($conf & 0x07) != 0;
3866    return if $rate > 0x07;
3867    return if $mid != 0x4d;     # Maxim
3868    return if $cid != 0x08;     # MAX6654/MAX6690
3869    return 8;
3870  }
3871  if ($chip == 5) {
3872    return if ($conf & 0x1b) != 0;
3873    return if $rate > 0x0a;
3874    return if $mid != 0x41;     # Analog Devices
3875    return 8 if $cid == 0x51;   # ADT7461
3876  }
3877  if ($chip == 6) {
3878    return if ($conf & 0x3f) != 0;
3879    return if $rate > 0x07;
3880    return if $mid != 0x4d;     # Maxim
3881    return if $cid != 0x59;     # MAX6648/MAX6692
3882    return 8;
3883  }
3884  if ($chip == 7) {
3885    return if ($conf & 0x03) != 0;
3886    return if $rate > 0x07;
3887    return if $mid != 0x4d;     # Maxim
3888    return if $cid != 0x01;     # MAX6680/MAX6681
3889    return 8;
3890  }
3891  if ($chip == 8) {
3892    return if ($conf & 0x2a) != 0;
3893    return if $rate > 0x09;
3894    return if $mid != 0x5c;     # Winbond
3895    return if $cid != 0x00;     # W83L771W/G
3896    return 6;
3897  }
3898  if ($chip == 9) {
3899    return if ($conf & 0x1B) != 0;
3900    return if $rate > 0x0F;
3901    return if $mid != 0x55;     # Texas Instruments
3902    return if $cid != 0x11;     # TMP401
3903    return 8;
3904  }
3905  if ($chip == 10) {
3906    return if ($conf & 0x1B) != 0;
3907    return if $rate > 0x0F;
3908    return if $mid != 0x55;     # Texas Instruments
3909    return 6 if ($addr == 0x4c && $cid == 0x12); # TMP411A
3910    return 6 if ($addr == 0x4d && $cid == 0x13); # TMP411B
3911    return 6 if ($addr == 0x4e && $cid == 0x10); # TMP411C
3912    return;
3913  }
3914  return;
3915}
3916
3917# $_[0]: A reference to the file descriptor to access this chip.
3918# $_[1]: Address
3919# Returns: undef if not detected, 5 if detected.
3920# Registers used:
3921#   0x03: Configuration (no low nibble)
3922#   0x04: Conversion rate
3923#   0xfe: Manufacturer ID
3924#   0xff: no register
3925sub max6657_detect
3926{
3927  my ($file, $addr) = @_;
3928  my $mid = i2c_smbus_read_byte_data($file, 0xfe, NO_CACHE);
3929  my $cid = i2c_smbus_read_byte_data($file, 0xff, NO_CACHE);
3930  my $conf = i2c_smbus_read_byte_data($file, 0x03, NO_CACHE);
3931
3932  return if $mid != 0x4d;     # Maxim
3933  return if ($conf & 0x1f) != 0x0d; # No low nibble,
3934                                    # returns previous low nibble
3935  return if $cid != 0x4d;     # No register, returns previous value
3936
3937  my $rate = i2c_smbus_read_byte_data($file, 0x04, NO_CACHE);
3938  return if $rate > 0x09;
3939
3940  $cid = i2c_smbus_read_byte_data($file, 0xff, NO_CACHE);
3941  $conf = i2c_smbus_read_byte_data($file, 0x03, NO_CACHE);
3942  return if ($conf & 0x0f) != $rate; # No low nibble,
3943                                     # returns previous low nibble
3944  return if $cid != $rate;    # No register, returns previous value
3945
3946  return 5;
3947}
3948
3949# $_[0]: A reference to the file descriptor to access this chip.
3950# $_[1]: Address
3951# Returns: undef if not detected, 6 if detected.
3952# Registers used:
3953#   0x03: Configuration
3954#   0xfe: Manufacturer ID
3955#   0xff: Revision ID
3956sub lm95231_detect
3957{
3958  my ($file, $addr) = @_;
3959  my $mid = i2c_smbus_read_byte_data($file, 0xfe);
3960  my $cid = i2c_smbus_read_byte_data($file, 0xff);
3961  my $conf = i2c_smbus_read_byte_data($file, 0x03);
3962
3963  return if ($conf & 0x89) != 0;
3964  return if $mid != 0x01;     # National Semiconductor
3965  return if $cid != 0xa1;     # LM95231
3966
3967  return 6;
3968}
3969
3970# $_[0]: A reference to the file descriptor to access this chip.
3971# $_[1]: Address
3972# Returns: undef if not detected, 6 if detected.
3973# Registers used:
3974#   0x03: Configuration 1
3975#   0x24: Configuration 2
3976#   0x3d: Manufacturer ID
3977#   0x3e: Device ID
3978sub adt7481_detect
3979{
3980  my ($file, $addr) = @_;
3981  my $mid = i2c_smbus_read_byte_data($file, 0x3d);
3982  my $cid = i2c_smbus_read_byte_data($file, 0x3e);
3983  my $conf1 = i2c_smbus_read_byte_data($file, 0x03);
3984  my $conf2 = i2c_smbus_read_byte_data($file, 0x24);
3985
3986  return if ($conf1 & 0x10) != 0;
3987  return if ($conf2 & 0x7f) != 0;
3988  return if $mid != 0x41;     # Analog Devices
3989  return if $cid != 0x81;     # ADT7481
3990
3991  return 6;
3992}
3993
3994# $_[0]: Chip to detect
3995#   (1 = LM63, 2 = F75363SG, 3 = LM64)
3996# $_[1]: A reference to the file descriptor to access this chip.
3997# $_[2]: Address (unused)
3998# Returns: undef if not detected, 6 if detected.
3999# Registers used:
4000#   0xfe: Manufacturer ID
4001#   0xff: Chip ID / die revision
4002#   0x03: Configuration (two or three unused bits)
4003#   0x16: Alert mask (two or three unused bits)
4004sub lm63_detect
4005{
4006  my ($chip, $file, $addr) = @_;
4007
4008  my $mid = i2c_smbus_read_byte_data($file, 0xfe);
4009  my $cid = i2c_smbus_read_byte_data($file, 0xff);
4010  my $conf = i2c_smbus_read_byte_data($file, 0x03);
4011  my $mask = i2c_smbus_read_byte_data($file, 0x16);
4012
4013  if ($chip == 1) {
4014    return if $mid != 0x01    # National Semiconductor
4015           || $cid != 0x41;   # LM63
4016    return if ($conf & 0x18) != 0x00
4017           || ($mask & 0xa4) != 0xa4;
4018  } elsif ($chip == 2) {
4019    return if $mid != 0x23    # Fintek
4020           || $cid != 0x20;   # F75363SG
4021    return if ($conf & 0x1a) != 0x00
4022           || ($mask & 0x84) != 0x00;
4023  } elsif ($chip == 3) {
4024    return if $mid != 0x01    # National Semiconductor
4025           || $cid != 0x51;   # LM64
4026    return if ($conf & 0x18) != 0x00
4027           || ($mask & 0xa4) != 0xa4;
4028  }
4029
4030  return 6;
4031}
4032
4033# $_[0]: Chip to detect
4034#   (0 = ADM1029)
4035# $_[1]: A reference to the file descriptor to access this chip.
4036# $_[2]: Address (unused)
4037# Returns: undef if not detected, 6 if detected.
4038# Registers used:
4039#   0x02, 0x03: Fan support
4040#   0x06: Temperature support
4041#   0x07, 0x08, 0x09: Fan config
4042#   0x0d: Manufacturer ID
4043#   0x0e: Chip ID / die revision
4044sub adm1029_detect
4045{
4046  my ($chip, $file, $addr) = @_;
4047  my $mid = i2c_smbus_read_byte_data($file, 0x0d);
4048  my $cid = i2c_smbus_read_byte_data($file, 0x0e);
4049  my $cfg;
4050
4051  if ($chip == 0) {
4052    return unless $mid == 0x41;             # Analog Devices
4053    return unless ($cid & 0xF0) == 0x00;    # ADM1029
4054
4055    # Extra check on unused bits
4056    $cfg = i2c_smbus_read_byte_data($file, 0x02);
4057    return unless $cfg == 0x03;
4058    $cfg = i2c_smbus_read_byte_data($file, 0x06);
4059    return unless ($cfg & 0xF9) == 0x01;
4060    foreach my $reg (0x03, 0x07, 0x08, 0x09) {
4061      $cfg = i2c_smbus_read_byte_data($file, $reg);
4062      return unless ($cfg & 0xFC) == 0x00;
4063    }
4064
4065    return 7;
4066  }
4067  return;
4068}
4069
4070# $_[0]: Chip to detect
4071#   (0 = ADM1030, 1=ADM1031)
4072# $_[1]: A reference to the file descriptor to access this chip.
4073# $_[2]: Address
4074# Returns: undef if not detected, 3 to 7 (ADM1031) or 9 (ADM1030)
4075#          if detected.
4076# Registers used:
4077#   0x01: Config 2
4078#   0x03: Status 2
4079#   0x0d, 0x0e, 0x0f: Temperature offsets
4080#   0x22: Fan speed config
4081#   0x3d: Chip ID
4082#   0x3e: Manufacturer ID
4083#   0x3f: Die revision
4084sub adm1031_detect
4085{
4086  my ($chip, $file, $addr) = @_;
4087  my $mid = i2c_smbus_read_byte_data($file, 0x3e);
4088  my $cid = i2c_smbus_read_byte_data($file, 0x3d);
4089  my $drev = i2c_smbus_read_byte_data($file, 0x3f);
4090  my $conf2 = i2c_smbus_read_byte_data($file, 0x01);
4091  my $stat2 = i2c_smbus_read_byte_data($file, 0x03);
4092  my $fsc = i2c_smbus_read_byte_data($file, 0x22);
4093  my $lto = i2c_smbus_read_byte_data($file, 0x0d);
4094  my $r1to = i2c_smbus_read_byte_data($file, 0x0e);
4095  my $r2to = i2c_smbus_read_byte_data($file, 0x0f);
4096  my $confidence = 3;
4097
4098  if ($chip == 0) {
4099    return if $mid != 0x41;     # Analog Devices
4100    return if $cid != 0x30;     # ADM1030
4101    $confidence++ if ($drev & 0x70) == 0x00;
4102    $confidence++ if ($conf2 & 0x4A) == 0x00;
4103    $confidence++ if ($stat2 & 0x3F) == 0x00;
4104    $confidence++ if ($fsc & 0xF0) == 0x00;
4105    $confidence++ if ($lto & 0x70) == 0x00;
4106    $confidence++ if ($r1to & 0x70) == 0x00;
4107    return $confidence;
4108  }
4109  if ($chip == 1) {
4110    return if $mid != 0x41;     # Analog Devices
4111    return if $cid != 0x31;     # ADM1031
4112    $confidence++ if ($drev & 0x70) == 0x00;
4113    $confidence++ if ($lto & 0x70) == 0x00;
4114    $confidence++ if ($r1to & 0x70) == 0x00;
4115    $confidence++ if ($r2to & 0x70) == 0x00;
4116    return $confidence;
4117  }
4118  return;
4119}
4120
4121# $_[0]: Chip to detect
4122#   (0 = ADM1033, 1 = ADM1034)
4123# $_[1]: A reference to the file descriptor to access this chip.
4124# $_[2]: Address (unused)
4125# Returns: undef if not detected, 4 or 6 if detected.
4126# Registers used:
4127#   0x3d: Chip ID
4128#   0x3e: Manufacturer ID
4129#   0x3f: Die revision
4130sub adm1034_detect
4131{
4132  my ($chip, $file, $addr) = @_;
4133  my $mid = i2c_smbus_read_byte_data($file, 0x3e);
4134  my $cid = i2c_smbus_read_byte_data($file, 0x3d);
4135  my $drev = i2c_smbus_read_byte_data($file, 0x3f);
4136
4137  if ($chip == 0) {
4138    return if $mid != 0x41;     # Analog Devices
4139    return if $cid != 0x33;     # ADM1033
4140    return if ($drev & 0xf8) != 0x00;
4141    return 6 if $drev == 0x02;
4142    return 4;
4143  }
4144  if ($chip == 1) {
4145    return if $mid != 0x41;     # Analog Devices
4146    return if $cid != 0x34;     # ADM1034
4147    return if ($drev & 0xf8) != 0x00;
4148    return 6 if $drev == 0x02;
4149    return 4;
4150  }
4151  return
4152}
4153
4154# $_[0]: Chip to detect
4155#   (0 = ADT7467/ADT7468, 1 = ADT7476, 2 = ADT7462, 3 = ADT7466,
4156#    4 = ADT7470)
4157# $_[1]: A reference to the file descriptor to access this chip.
4158# $_[2]: Address
4159# Returns: undef if not detected, 5 or 7 if detected.
4160# Registers used:
4161#   0x3d: Chip ID
4162#   0x3e: Manufacturer ID
4163#   0x3f: Die revision
4164sub adt7467_detect
4165{
4166  my ($chip, $file, $addr) = @_;
4167  my $mid = i2c_smbus_read_byte_data($file, 0x3e);
4168  my $cid = i2c_smbus_read_byte_data($file, 0x3d);
4169  my $drev = i2c_smbus_read_byte_data($file, 0x3f);
4170
4171  if ($chip == 0) {
4172    return if $mid != 0x41;     # Analog Devices
4173    return if $cid != 0x68;     # ADT7467
4174    return if ($drev & 0xf0) != 0x70;
4175    return 7 if ($drev == 0x71 || $drev == 0x72);
4176    return 5;
4177  }
4178  if ($chip == 1) {
4179    return if $mid != 0x41;     # Analog Devices
4180    return if $cid != 0x76;     # ADT7476
4181    return if ($drev & 0xf0) != 0x60;
4182    return 7 if ($drev == 0x69);
4183    return 5;
4184  }
4185  if ($chip == 2) {
4186    return if $mid != 0x41;     # Analog Devices
4187    return if $cid != 0x62;     # ADT7462
4188    return if ($drev & 0xf0) != 0x00;
4189    return 7 if ($drev == 0x04);
4190    return 5;
4191  }
4192  if ($chip == 3) {
4193    return if $mid != 0x41;     # Analog Devices
4194    return if $cid != 0x66;     # ADT7466
4195    return if ($drev & 0xf0) != 0x00;
4196    return 7 if ($drev == 0x02);
4197    return 5;
4198  }
4199  if ($chip == 4) {
4200    return if $mid != 0x41;     # Analog Devices
4201    return if $cid != 0x70;     # ADT7470
4202    return if ($drev & 0xf0) != 0x00;
4203    return 7 if ($drev == 0x00);
4204    return 5;
4205  }
4206  return
4207}
4208
4209# $_[0]: Chip to detect
4210#   (0 = ADT7473, 1 = ADT7475)
4211# $_[1]: A reference to the file descriptor to access this chip.
4212# $_[2]: Address (unused)
4213# Returns: undef if not detected, 5 if detected.
4214# Registers used:
4215#   0x3d: Chip ID
4216#   0x3e: Manufacturer ID
4217sub adt7473_detect
4218{
4219  my ($chip, $file, $addr) = @_;
4220  my $mid = i2c_smbus_read_byte_data($file, 0x3e);
4221  my $cid = i2c_smbus_read_byte_data($file, 0x3d);
4222
4223  if ($chip == 0) {
4224    return if $mid != 0x41;     # Analog Devices
4225    return if $cid != 0x73;     # ADT7473
4226    return 5;
4227  }
4228  if ($chip == 1) {
4229    return if $mid != 0x41;     # Analog Devices
4230    return if $cid != 0x75;     # ADT7475
4231    return 5;
4232  }
4233  return
4234}
4235
4236# $_[0]: Chip to detect
4237#   (0 = aSC7512, 1 = aSC7611, 2 = aSC7621)
4238# $_[1]: A reference to the file descriptor to access this chip.
4239# $_[2]: Address (unused)
4240# Returns: undef if not detected, 1 if detected.
4241# Registers used:
4242#   0x3e: Manufacturer ID (0x61)
4243#   0x3f: Version
4244
4245sub andigilog_detect
4246{
4247  my ($chip, $file, $addr) = @_;
4248  my $mid = i2c_smbus_read_byte_data($file, 0x3e);
4249  my $cid = i2c_smbus_read_byte_data($file, 0x3f);
4250
4251  return if ($mid != 0x61);
4252
4253  if ($chip == 0) {
4254    return if $cid != 0x62;
4255    return 5;
4256  }
4257
4258  if ($chip == 1) {
4259    return if $cid != 0x69;
4260    return 5;
4261  }
4262
4263  if ($chip == 2) {
4264    return if ($cid != 0x6C && $cid != 0x6D);
4265    return 5;
4266  }
4267
4268  return;
4269}
4270
4271# $_[0]: Chip to detect
4272#   (0 = aSC7511)
4273# $_[1]: A reference to the file descriptor to access this chip.
4274# $_[2]: Address (unused)
4275# Returns: undef if not detected, 1 if detected.
4276# Registers used:
4277#   0xfe: Manufacturer ID
4278#   0xff: Die Code
4279sub andigilog_aSC7511_detect
4280{
4281  my ($chip, $file, $addr) = @_;
4282  my $mid = i2c_smbus_read_byte_data($file, 0xfe);
4283  my $die = i2c_smbus_read_byte_data($file, 0xff);
4284
4285  if ($chip == 0) {
4286    return if $mid != 0x61;     # Andigilog
4287    if ($die == 0x0) {
4288        return 3;
4289    } else {
4290        return 1;
4291    }
4292  }
4293  return;
4294}
4295
4296# $_[0]: Chip to detect
4297#   (0 = LM85, 1 = LM96000, 2 = ADM1027, 3 = ADT7463,
4298#    4 = EMC6D100/101, 5 = EMC6D102, 6 = EMC6D103)
4299# $_[1]: A reference to the file descriptor to access this chip.
4300# #_[2]: Base address.
4301# Returns: undef if not detected, (7) or (8) if detected.
4302# Registers used: 0x3e == Vendor register.
4303#                 0x3d == Device ID register (Analog Devices only).
4304#                 0x3f == Version/Stepping register.
4305# Constants used: 0x01 == National Semiconductor Vendor Id.
4306#                 0x41 == Analog Devices Vendor Id.
4307#                 0x5c == SMSC Vendor Id.
4308sub lm85_detect
4309{
4310  my ($chip, $file, $addr) = @_;
4311  my $vendor = i2c_smbus_read_byte_data($file, 0x3e);
4312  my $verstep = i2c_smbus_read_byte_data($file, 0x3f);
4313
4314  if ($chip == 0) {
4315    return if $vendor != 0x01;  # National Semiconductor
4316    return if $verstep != 0x60  # LM85 C
4317           && $verstep != 0x62; # LM85 B
4318  } elsif ($chip == 1) {
4319    return if $vendor != 0x01;  # National Semiconductor
4320    return if $verstep != 0x68  # LM96000
4321           && $verstep != 0x69; # LM96000
4322  } elsif ($chip == 2) {
4323    return if $vendor != 0x41;  # Analog Devices
4324    return if $verstep != 0x60; # ADM1027
4325  } elsif ($chip == 3) {
4326    return if $vendor != 0x41;  # Analog Devices
4327    return if $verstep != 0x62  # ADT7463
4328           && $verstep != 0x6a; # ADT7463 C
4329  } elsif ($chip == 4) {
4330    return if $vendor != 0x5c;  # SMSC
4331    return if $verstep != 0x60  # EMC6D100/101 A0
4332           && $verstep != 0x61; # EMC6D100/101 A1
4333  } elsif ($chip == 5) {
4334    return if $vendor != 0x5c;  # SMSC
4335    return if $verstep != 0x65; # EMC6D102
4336  } elsif ($chip == 6) {
4337    return if $vendor != 0x5c;  # SMSC
4338    return if $verstep != 0x68; # EMC6D103
4339  }
4340
4341  if ($vendor == 0x41) { # Analog Devices
4342    return if i2c_smbus_read_byte_data($file, 0x3d) != 0x27;
4343    return (8);
4344  }
4345
4346  return (7);
4347}
4348
4349# $_[0]: Chip to detect (0 = LM87, 1 = ADM1024)
4350# $_[1]: A reference to the file descriptor to access this chip.
4351# $_[2]: Address
4352# Returns: undef if not detected, (7) if detected.
4353# Registers used:
4354#   0x3e: Company ID
4355#   0x3f: Revision
4356#   0x40: Configuration
4357sub lm87_detect
4358{
4359  my ($chip, $file, $addr) = @_;
4360  my $cid = i2c_smbus_read_byte_data($file, 0x3e);
4361  my $rev = i2c_smbus_read_byte_data($file, 0x3f);
4362
4363  if ($chip == 0) {
4364    return if $cid != 0x02;     # National Semiconductor
4365    return if ($rev & 0xfc) != 0x04;
4366  }
4367  if ($chip == 1) {
4368    return if $cid != 0x41;     # Analog Devices
4369    return if ($rev & 0xf0) != 0x10;
4370  }
4371
4372  my $cfg = i2c_smbus_read_byte_data($file, 0x40);
4373  return if ($cfg & 0x80) != 0x00;
4374
4375  return (7);
4376}
4377
4378# $_[0]: Chip to detect (0 = W83781D, 1 = W83782D, 2 = W83783S,
4379#                        3 = W83627HF, 4 = AS99127F (rev.1),
4380#                        5 = AS99127F (rev.2), 6 = ASB100, 7 = W83791D,
4381#                        8 = W83792D, 9 = W83627EHF, 10 = W83627DHG)
4382# $_[1]: A reference to the file descriptor to access this chip.
4383# $_[2]: Address
4384# Returns: undef if not detected, (8, addr1, addr2) if detected, but only
4385#          if the LM75 chip emulation is enabled.
4386# Registers used:
4387#   0x48: Full I2C Address
4388#   0x4a: I2C addresses of emulated LM75 chips
4389#   0x4e: Vendor ID byte selection, and bank selection
4390#   0x4f: Vendor ID
4391#   0x58: Device ID (only when in bank 0)
4392# Note: Fails if the W8378xD is not in bank 0!
4393# Note: Detection overrules a previous LM78 detection
4394# Note: Asus chips do not have their I2C address at register 0x48?
4395#       AS99127F rev.1 and ASB100 have 0x00, confirmation wanted for
4396#       AS99127F rev.2.
4397sub w83781d_detect
4398{
4399  my ($reg1, $reg2, @res);
4400  my ($chip, $file, $addr) = @_;
4401
4402  return unless (i2c_smbus_read_byte_data($file, 0x48) == $addr)
4403    or ($chip >= 4 && $chip <= 6);
4404
4405  $reg1 = i2c_smbus_read_byte_data($file, 0x4e);
4406  $reg2 = i2c_smbus_read_byte_data($file, 0x4f);
4407  if ($chip == 4) { # Asus AS99127F (rev.1)
4408    return unless (($reg1 & 0x80) == 0x00 and $reg2 == 0xc3) or
4409                  (($reg1 & 0x80) == 0x80 and $reg2 == 0x12);
4410  } elsif ($chip == 6) { # Asus ASB100
4411    return unless (($reg1 & 0x80) == 0x00 and $reg2 == 0x94) or
4412                  (($reg1 & 0x80) == 0x80 and $reg2 == 0x06);
4413  } else { # Winbond and Asus AS99127F (rev.2)
4414    return unless (($reg1 & 0x80) == 0x00 and $reg2 == 0xa3) or
4415                  (($reg1 & 0x80) == 0x80 and $reg2 == 0x5c);
4416  }
4417
4418  return unless ($reg1 & 0x07) == 0x00;
4419
4420  $reg1 = i2c_smbus_read_byte_data($file, 0x58);
4421  return if $chip == 0 and ($reg1 != 0x10 && $reg1 != 0x11);
4422  return if $chip == 1 and  $reg1 != 0x30;
4423  return if $chip == 2 and  $reg1 != 0x40;
4424  return if $chip == 3 and  $reg1 != 0x21;
4425  return if $chip == 4 and  $reg1 != 0x31;
4426  return if $chip == 5 and  $reg1 != 0x31;
4427  return if $chip == 6 and  $reg1 != 0x31;
4428  return if $chip == 7 and  $reg1 != 0x71;
4429  return if $chip == 8 and  $reg1 != 0x7a;
4430  return if $chip == 9 and ($reg1 != 0x88 && $reg1 != 0xa1);
4431  return if $chip == 10 and  $reg1 != 0xc1;
4432  # Default address is 0x2d
4433  @res = ($addr != 0x2d) ? (7) : (8);
4434  return @res if $chip >= 9; # No subclients
4435
4436  $reg1 = i2c_smbus_read_byte_data($file, 0x4a);
4437  push @res, ($reg1 & 0x07) + 0x48 unless $reg1 & 0x08;
4438  push @res, (($reg1 & 0x70) >> 4) + 0x48 unless ($reg1 & 0x80 or $chip == 2);
4439  return @res;
4440}
4441
4442# $_[0]: Chip to detect (0 = W83793)
4443# $_[1]: A reference to the file descriptor to access this chip.
4444# $_[2]: Address
4445# Returns: undef if not detected
4446#          6 if detected and bank different from 0
4447#          (8, addr1, addr2) if detected, bank is 0 and LM75 chip emulation
4448#          is enabled
4449# Registers used:
4450#   0x0b: Full I2C Address
4451#   0x0c: I2C addresses of emulated LM75 chips
4452#   0x00: Vendor ID byte selection, and bank selection(Bank 0, 1, 2)
4453#   0x0d: Vendor ID(Bank 0, 1, 2)
4454#   0x0e: Device ID(Bank 0, 1, 2)
4455sub w83793_detect
4456{
4457  my ($bank, $reg, @res);
4458  my ($chip, $file, $addr) = @_;
4459
4460  $bank = i2c_smbus_read_byte_data($file, 0x00);
4461  $reg = i2c_smbus_read_byte_data($file, 0x0d);
4462
4463  return unless (($bank & 0x80) == 0x00 and $reg == 0xa3) or
4464                (($bank & 0x80) == 0x80 and $reg == 0x5c);
4465
4466  $reg = i2c_smbus_read_byte_data($file, 0x0e);
4467  return if $chip == 0 and $reg != 0x7b;
4468
4469# If bank 0 is selected, we can do more checks
4470  return 6 unless ($bank & 0x07) == 0;
4471  $reg = i2c_smbus_read_byte_data($file, 0x0b);
4472  return unless ($reg == ($addr << 1));
4473
4474  $reg = i2c_smbus_read_byte_data($file, 0x0c);
4475  @res = (8);
4476  push @res, ($reg & 0x07) + 0x48 unless $reg & 0x08;
4477  push @res, (($reg & 0x70) >> 4) + 0x48 unless $reg & 0x80;
4478  return @res;
4479}
4480
4481# $_[0]: A reference to the file descriptor to access this chip.
4482# $_[1]: Address
4483# Returns: undef if not detected, 3 if detected
4484# Registers used:
4485#   0x48: Full I2C Address
4486#   0x4e: Vendor ID byte selection
4487#   0x4f: Vendor ID
4488#   0x58: Device ID
4489# Note that the datasheet was useless and this detection routine
4490# is based on dumps we received from users. Also, the W83781SD is *NOT*
4491# a hardware monitoring chip as far as we know, but we still want to
4492# detect it so that people won't keep reporting it as an unknown chip
4493# we should investigate about.
4494sub w83791sd_detect
4495{
4496  my ($file, $addr) = @_;
4497  my ($reg1, $reg2);
4498
4499  return unless (i2c_smbus_read_byte_data($file, 0x48) == $addr);
4500
4501  $reg1 = i2c_smbus_read_byte_data($file, 0x4e);
4502  $reg2 = i2c_smbus_read_byte_data($file, 0x4f);
4503  return unless (!($reg1 & 0x80) && $reg2 == 0xa3)
4504             || (($reg1 & 0x80) && $reg2 == 0x5c);
4505
4506  $reg1 = i2c_smbus_read_byte_data($file, 0x58);
4507  return unless $reg1 == 0x72;
4508
4509  return 3;
4510}
4511
4512# $_[0]: Chip to detect (0 = ASM58, 1 = AS2K129R, 2 = ???)
4513# $_[1]: A reference to the file descriptor to access this chip
4514# $_[2]: Address (unused)
4515# Returns: undef if not detected, 5 if detected
4516# Registers used:
4517#   0x4e: Vendor ID high byte
4518#   0x4f: Vendor ID low byte
4519#   0x58: Device ID
4520# Note: The values were given by Alex van Kaam, we don't have datasheets
4521#       to confirm.
4522sub mozart_detect
4523{
4524  my ($vid, $dev);
4525  my ($chip, $file, $addr) = @_;
4526
4527  $vid = (i2c_smbus_read_byte_data($file, 0x4e) << 8)
4528       +  i2c_smbus_read_byte_data($file, 0x4f);
4529  $dev = i2c_smbus_read_byte_data($file, 0x58);
4530
4531  return if ($chip == 0) and ($dev != 0x56 || $vid != 0x9436);
4532  return if ($chip == 1) and ($dev != 0x56 || $vid != 0x9406);
4533  return if ($chip == 2) and ($dev != 0x10 || $vid != 0x5ca3);
4534
4535  return 5;
4536}
4537
4538# $_[0]: First limit register to compare
4539# $_[1]: Last limit register to compare
4540# $_[2]: ISA address
4541# $_[3]: I2C file handle
4542# $_[4]: I2C address
4543sub winbond_alias_detect
4544{
4545  my ($first, $last, $isa_addr, $file, $i2c_addr) = @_;
4546  my $i;
4547
4548  return 0 unless isa_read_i5d6($isa_addr, 0x48) == $i2c_addr;
4549  for ($i = $first; $i <= $last; $i++) {
4550    return 0 unless isa_read_i5d6($isa_addr, $i) == i2c_smbus_read_byte_data($file, $i);
4551  }
4552  return 1;
4553}
4554
4555# $_[0]: Chip to detect (0 = W83781D, 1 = W83782D)
4556# $_[1]: Address
4557# Returns: undef if not detected, (8) if detected.
4558sub w83781d_isa_detect
4559{
4560  my ($chip, $addr) = @_;
4561  my ($reg1, $reg2);
4562  my $val = inb($addr + 1);
4563  return if inb($addr + 2) != $val or inb($addr + 3) != $val or
4564            inb($addr + 7) != $val;
4565
4566  $val = inb($addr + 5);
4567  outb($addr+5, ~$val & 0x7f);
4568  if ((inb($addr+5) & 0x7f) != (~ $val & 0x7f)) {
4569    outb($addr+5, $val);
4570    return;
4571  }
4572
4573  $reg1 = isa_read_i5d6($addr, 0x4e);
4574  $reg2 = isa_read_i5d6($addr, 0x4f);
4575  return unless (($reg1 & 0x80) == 0x00 and $reg2 == 0xa3) or
4576                (($reg1 & 0x80) == 0x80 and $reg2 == 0x5c);
4577  return unless ($reg1 & 0x07) == 0x00;
4578  $reg1 = isa_read_i5d6($addr, 0x58);
4579  return if $chip == 0 and  ($reg1 & 0xfe) != 0x10;
4580  return if $chip == 1 and  ($reg1 & 0xfe) != 0x30;
4581
4582  return 8;
4583}
4584
4585# $_[0]: Chip to detect (0 = Revision 0x00, 1 = Revision 0x80)
4586# $_[1]: A reference to the file descriptor to access this chip.
4587# $_[2]: Address
4588# Returns: undef if not detected, (6) if detected.
4589# Registers used:
4590#   0x00: Device ID
4591#   0x01: Revision ID
4592#   0x03: Configuration
4593# Mediocre detection
4594sub gl518sm_detect
4595{
4596  my $reg;
4597  my ($chip, $file, $addr) = @_;
4598  return unless i2c_smbus_read_byte_data($file, 0x00) == 0x80;
4599  return unless (i2c_smbus_read_byte_data($file, 0x03) & 0x80) == 0x00;
4600  $reg = i2c_smbus_read_byte_data($file, 0x01);
4601  return unless ($chip == 0 and $reg == 0x00) or
4602                ($chip == 1 and $reg == 0x80);
4603  return (6);
4604}
4605
4606# $_[0]: A reference to the file descriptor to access this chip.
4607# $_[1]: Address
4608# Returns: undef if not detected, (5) if detected.
4609# Registers used:
4610#   0x00: Device ID
4611#   0x01: Revision ID
4612#   0x03: Configuration
4613# Mediocre detection
4614sub gl520sm_detect
4615{
4616  my ($file, $addr) = @_;
4617  return unless i2c_smbus_read_byte_data($file, 0x00) == 0x20;
4618  return unless (i2c_smbus_read_byte_data($file, 0x03) & 0x80) == 0x00;
4619  # The line below must be better checked before I dare to use it.
4620  # return unless i2c_smbus_read_byte_data($file, 0x01) == 0x00;
4621  return (5);
4622}
4623
4624# $_[0]: A reference to the file descriptor to access this chip.
4625# $_[1]: Address
4626# Returns: undef if not detected, (5) if detected.
4627# Registers used:
4628#   0x00: Device ID
4629# Mediocre detection
4630sub gl525sm_detect
4631{
4632  my ($file, $addr) = @_;
4633  return unless i2c_smbus_read_byte_data($file, 0x00) == 0x25;
4634  return (5);
4635}
4636
4637# $_[0]: Chip to detect (0 = ADM9240, 1 = DS1780, 2 = LM81)
4638# $_[1]: A reference to the file descriptor to access this chip.
4639# $_[2]: Address
4640# Returns: undef if not detected, (7) if detected.
4641# Registers used:
4642#   0x3e: Company ID
4643#   0x40: Configuration
4644#   0x48: Full I2C Address
4645# Note: Detection overrules a previous LM78 detection
4646sub adm9240_detect
4647{
4648  my $reg;
4649  my ($chip, $file, $addr) = @_;
4650  $reg = i2c_smbus_read_byte_data($file, 0x3e);
4651  return unless ($chip == 0 and $reg == 0x23) or
4652                ($chip == 1 and $reg == 0xda) or
4653                ($chip == 2 and $reg == 0x01);
4654  return unless (i2c_smbus_read_byte_data($file, 0x40) & 0x80) == 0x00;
4655  return unless i2c_smbus_read_byte_data($file, 0x48) == $addr;
4656
4657  return (7);
4658}
4659
4660# $_[0]: Chip to detect (0 = ADM1022, 1 = THMC50, 2 = ADM1028,
4661#                        3 = THMC51)
4662# $_[1]: A reference to the file descriptor to access this chip.
4663# $_[2]: Address
4664# Returns: undef if not detected, (8) if detected.
4665# Registers used:
4666#   0x3e: Company ID
4667#   0x3f: Revision
4668#   0x40: Configuration
4669# Note: Detection overrules a previous LM78 or ADM9240 detection
4670sub adm1022_detect
4671{
4672  my $reg;
4673  my ($chip, $file, $addr) = @_;
4674  $reg = i2c_smbus_read_byte_data($file, 0x3e);
4675  return unless ($chip == 0 and $reg == 0x41) or
4676                ($chip == 1 and $reg == 0x49) or
4677                ($chip == 2 and $reg == 0x41) or
4678                ($chip == 3 and $reg == 0x49);
4679  $reg = i2c_smbus_read_byte_data($file, 0x40);
4680  return if ($reg & 0x10);                      # Soft Reset always reads 0
4681  return if ($chip != 0 and ($reg & 0x80));     # Reserved on THMC50 and ADM1028
4682  $reg = i2c_smbus_read_byte_data($file, 0x3f) & 0xf0;
4683  return unless ($chip == 0 and $reg == 0xc0) or
4684                ($chip == 1 and $reg == 0xc0) or
4685                ($chip == 2 and $reg == 0xd0) or
4686                ($chip == 3 and $reg == 0xd0);
4687  return (8);
4688}
4689
4690# $_[0]: Chip to detect (0 = ADM1025, 1 = NE1619)
4691# $_[1]: A reference to the file descriptor to access this chip.
4692# $_[2]: Address
4693# Returns: undef if not detected, (8) if detected.
4694# Registers used:
4695#   0x3e: Company ID
4696#   0x3f: Revision
4697#   0x40: Configuration
4698#   0x41: Status 1
4699#   0x42: Status 2
4700# Note: Detection overrules a previous LM78 or ADM9240 detection
4701sub adm1025_detect
4702{
4703  my $reg;
4704  my ($chip, $file, $addr) = @_;
4705
4706  $reg = i2c_smbus_read_byte_data($file, 0x3e);
4707  return if ($chip == 0) and ($reg != 0x41);
4708  return if ($chip == 1) and ($reg != 0xA1);
4709
4710  return unless (i2c_smbus_read_byte_data($file, 0x40) & 0x80) == 0x00;
4711  return unless (i2c_smbus_read_byte_data($file, 0x41) & 0xC0) == 0x00;
4712  return unless (i2c_smbus_read_byte_data($file, 0x42) & 0xBC) == 0x00;
4713  return unless (i2c_smbus_read_byte_data($file, 0x3f) & 0xf0) == 0x20;
4714
4715  return (8);
4716}
4717
4718# $_[0]: Chip to detect (0 = ADM1026)
4719# $_[1]: A reference to the file descriptor to access this chip.
4720# $_[2]: Address
4721# Returns: undef if not detected, (8) if detected.
4722# Registers used:
4723#   0x16: Company ID
4724#   0x17: Revision
4725sub adm1026_detect
4726{
4727  my $reg;
4728  my ($chip, $file, $addr) = @_;
4729  $reg = i2c_smbus_read_byte_data($file, 0x16);
4730  return unless ($reg == 0x41);
4731  return unless (i2c_smbus_read_byte_data($file, 0x17) & 0xf0) == 0x40;
4732  return (8);
4733}
4734
4735# $_[0]: Chip to detect
4736#   (0 = ADM1021, 1 = ADM1021A/ADM1023, 2 = MAX1617, 3 = MAX1617A, 4 = THMC10,
4737#    5 = LM84, 6 = GL523, 7 = MC1066)
4738# $_[1]: A reference to the file descriptor to access this chip.
4739# $_[2]: Address
4740# Returns: undef if not detected, 3 if simply detected, 5 if detected and
4741#          manufacturer ID matches, 7 if detected and manufacturer ID and
4742#          revision match
4743# Registers used:
4744#   0x04: Company ID (LM84 only)
4745#   0xfe: Company ID (all but LM84 and MAX1617)
4746#   0xff: Revision (ADM1021, ADM1021A/ADM1023 and MAX1617A)
4747#   0x02: Status
4748#   0x03: Configuration
4749#   0x04: Conversion rate
4750#   0x00-0x01, 0x05-0x08: Temperatures (MAX1617 and LM84)
4751# Note: Especially the MAX1617 has very bad detection; we give it a low
4752# confidence value.
4753sub adm1021_detect
4754{
4755  my ($chip, $file, $addr) = @_;
4756  my $man_id = i2c_smbus_read_byte_data($file, 0xfe);
4757  my $rev = i2c_smbus_read_byte_data($file, 0xff);
4758  my $conf = i2c_smbus_read_byte_data($file, 0x03);
4759  my $status = i2c_smbus_read_byte_data($file, 0x02);
4760  my $convrate = i2c_smbus_read_byte_data($file, 0x04);
4761
4762  # Check manufacturer IDs and product revisions when available
4763  return if $chip == 0 and $man_id != 0x41 ||
4764                          ($rev & 0xf0) != 0x00;
4765  return if $chip == 1 and $man_id != 0x41 ||
4766                          ($rev & 0xf0) != 0x30;
4767  return if $chip == 3 and $man_id != 0x4d ||
4768                           $rev != 0x01;
4769  return if $chip == 4 and $man_id != 0x49;
4770  return if $chip == 5 and $convrate != 0x00;
4771  return if $chip == 6 and $man_id != 0x23;
4772  return if $chip == 7 and $man_id != 0x54;
4773
4774  # Check unused bits
4775  if ($chip == 5) { # LM84
4776    return if ($status & 0xab) != 0;
4777    return if ($conf & 0x7f) != 0;
4778  } else {
4779    return if ($status & 0x03) != 0;
4780    return if ($conf & 0x3f) != 0;
4781    return if ($convrate & 0xf8) != 0;
4782  }
4783
4784  # Extra checks for MAX1617 and LM84, since those are often misdetected
4785  # We verify several assertions (6 for the MAX1617, 4 for the LM84) and
4786  # discard the chip if any fail. Note that these checks are not done
4787  # by the adm1021 driver.
4788  if ($chip == 2 || $chip == 5) {
4789    my $lte = i2c_smbus_read_byte_data($file, 0x00);
4790    my $rte = i2c_smbus_read_byte_data($file, 0x01);
4791    my $lhi = i2c_smbus_read_byte_data($file, 0x05);
4792    my $rhi = i2c_smbus_read_byte_data($file, 0x07);
4793    my $llo = i2c_smbus_read_byte_data($file, 0x06);
4794    my $rlo = i2c_smbus_read_byte_data($file, 0x08);
4795
4796    # If all registers hold the same value, it has to be a misdetection
4797    return if $lte == $rte and $lte == $lhi and $lte == $rhi
4798           and $lte == $llo and $lte == $rlo;
4799
4800    # Negative temperatures
4801    return if ($lte & 0x80) or ($rte & 0x80);
4802    # Negative high limits
4803    return if ($lhi & 0x80) or ($rhi & 0x80);
4804    # Low limits over high limits
4805    if ($chip != 5) { # LM84 doesn't have low limits
4806      $llo -= 256 if ($llo & 0x80);
4807      $rlo -= 256 if ($rlo & 0x80);
4808      return if ($llo > $lhi) or ($rlo > $rhi);
4809    }
4810  }
4811
4812  return 3 if ($chip == 2) or ($chip == 5);
4813  return 7 if $chip <= 3;
4814  return 5;
4815}
4816
4817# $_[0]: Chip to detect
4818#   (0 = MAX1668, 1 = MAX1805, 2 = MAX1989)
4819# $_[1]: A reference to the file descriptor to access this chip.
4820#        We may assume an i2c_set_slave_addr was already done.
4821# $_[2]: Address
4822# Returns: undef if not detected, 7 if detected
4823# Registers used:
4824#   0xfe: Company ID
4825#   0xff: Device ID
4826sub max1668_detect
4827{
4828  my ($chip, $file, $addr) = @_;
4829  my $man_id = i2c_smbus_read_byte_data($file, 0xfe);
4830  my $dev_id = i2c_smbus_read_byte_data($file, 0xff);
4831
4832  return if $man_id != 0x4d;
4833  return if $chip == 0 and $dev_id != 0x03;
4834  return if $chip == 1 and $dev_id != 0x05;
4835  return if $chip == 2 and $dev_id != 0x0b;
4836
4837  return 7;
4838}
4839
4840# $_[0]: Chip to detect
4841#   (0 = MAX1619, 1 = MAX1618)
4842# $_[1]: A reference to the file descriptor to access this chip.
4843# $_[2]: Address
4844# Returns: undef if not detected, 7 if detected
4845# Registers used:
4846#   0xfe: Company ID
4847#   0xff: Device ID
4848#   0x02: Status
4849#   0x03: Configuration
4850#   0x04: Conversion rate
4851sub max1619_detect
4852{
4853  my ($chip, $file, $addr) = @_;
4854  my $man_id = i2c_smbus_read_byte_data($file, 0xfe);
4855  my $dev_id = i2c_smbus_read_byte_data($file, 0xff);
4856  my $conf = i2c_smbus_read_byte_data($file, 0x03);
4857  my $status = i2c_smbus_read_byte_data($file, 0x02);
4858  my $convrate = i2c_smbus_read_byte_data($file, 0x04);
4859
4860  if ($chip == 0) {     # MAX1619
4861    return if $man_id != 0x4D
4862      or $dev_id != 0x04
4863      or ($conf & 0x03)
4864      or ($status & 0x61)
4865      or $convrate >= 8;
4866  }
4867  if ($chip == 1) {     # MAX1618
4868    return if $man_id != 0x4D
4869      or $dev_id != 0x02
4870      or ($conf & 0x07)
4871      or ($status & 0x63);
4872  }
4873
4874  return 7;
4875}
4876
4877# $_[0]: A reference to the file descriptor to access this chip.
4878# $_[1]: Address (unused)
4879# Returns: undef if not detected, 6 if detected.
4880# Registers used:
4881#   0x28: User ID
4882#   0x29: User ID2
4883#   0x2A: Version ID
4884
4885sub ite_overclock_detect
4886{
4887  my ($file, $addr) = @_;
4888
4889  my $uid1 = i2c_smbus_read_byte_data($file, 0x28);
4890  my $uid2 = i2c_smbus_read_byte_data($file, 0x29);
4891  return if $uid1 != 0x83
4892         || $uid2 != 0x12;
4893
4894  return 6;
4895}
4896
4897# $_[0]: Chip to detect (0 = IT8712F)
4898# $_[1]: A reference to the file descriptor to access this chip.
4899# $_[2]: Address
4900# Returns: undef if not detected, 7 or 8 if detected (tops LM78).
4901# Registers used:
4902#   0x00: Configuration
4903#   0x48: Full I2C Address
4904#   0x58: Mfr ID
4905#   0x5b: Device ID (not on IT8705)
4906sub ite_detect
4907{
4908  my $reg;
4909  my ($chip, $file, $addr) = @_;
4910  return unless i2c_smbus_read_byte_data($file, 0x48) == $addr;
4911  return unless (i2c_smbus_read_byte_data($file, 0x00) & 0x90) == 0x10;
4912  return unless i2c_smbus_read_byte_data($file, 0x58) == 0x90;
4913  return if $chip == 0 and i2c_smbus_read_byte_data($file, 0x5b) != 0x12;
4914  return (7 + ($addr == 0x2d));
4915}
4916
4917# $_[0]: A reference to the file descriptor to access this chip
4918# $_[1]: Address
4919# Returns: 8 for a memory eeprom
4920# Registers used:
4921#   0-63: SPD Data and Checksum
4922sub eeprom_detect
4923{
4924  my ($file, $addr) = @_;
4925  my $checksum = 0;
4926
4927  # Check the checksum for validity (works for most DIMMs and RIMMs)
4928  for (my $i = 0; $i <= 62; $i++) {
4929    $checksum += i2c_smbus_read_byte_data($file, $i);
4930  }
4931  $checksum &= 255;
4932
4933  return 8
4934    if $checksum == i2c_smbus_read_byte_data($file, 63);
4935
4936  return;
4937}
4938
4939# $_[0]: A reference to the file descriptor to access this chip.
4940# $_[1]: Address
4941# Returns: undef if not detected, 8 if detected.
4942# Registers used:
4943#   0x00..0x07: DDC signature
4944sub ddcmonitor_detect
4945{
4946  my ($file, $addr) = @_;
4947
4948  return unless
4949    i2c_smbus_read_byte_data($file, 0x00) == 0x00 and
4950    i2c_smbus_read_byte_data($file, 0x01) == 0xFF and
4951    i2c_smbus_read_byte_data($file, 0x02) == 0xFF and
4952    i2c_smbus_read_byte_data($file, 0x03) == 0xFF and
4953    i2c_smbus_read_byte_data($file, 0x04) == 0xFF and
4954    i2c_smbus_read_byte_data($file, 0x05) == 0xFF and
4955    i2c_smbus_read_byte_data($file, 0x06) == 0xFF and
4956    i2c_smbus_read_byte_data($file, 0x07) == 0x00;
4957
4958  return 8;
4959}
4960
4961# $_[0]: Chip to detect (0 = Poseidon I, 1 = Poseidon II, 2 = Scylla,
4962#                        3 = Hermes, 4 = Heimdal, 5 = Heracles)
4963# $_[1]: A reference to the file descriptor to access this chip.
4964# $_[2]: Address
4965# Returns: undef if not detected, (8) if detected.
4966# Registers used:
4967#   0x00-0x02: Identification (3 capital ASCII letters)
4968sub fsc_detect
4969{
4970  my ($chip, $file, $addr) = @_;
4971  my $id;
4972
4973  $id = chr(i2c_smbus_read_byte_data($file, 0x00))
4974      . chr(i2c_smbus_read_byte_data($file, 0x01))
4975      . chr(i2c_smbus_read_byte_data($file, 0x02));
4976
4977  return if $chip == 0 and $id ne 'PEG';  # Pegasus? aka Poseidon I
4978  return if $chip == 1 and $id ne 'POS';  # Poseidon II
4979  return if $chip == 2 and $id ne 'SCY';  # Scylla
4980  return if $chip == 3 and $id ne 'HER';  # Hermes
4981  return if $chip == 4 and $id ne 'HMD';  # Heimdal
4982  return if $chip == 5 and $id ne 'HRC';  # Heracles
4983
4984  return (8);
4985}
4986
4987# $_[0]: A reference to the file descriptor to access this chip.
4988# $_[1]: Address (unused)
4989# Returns: undef if not detected, 5 if detected.
4990# Registers used:
4991#   0x3E: Manufacturer ID
4992#   0x3F: Version/Stepping
4993sub lm93_detect
4994{
4995  my $file = shift;
4996  return unless i2c_smbus_read_byte_data($file, 0x3E) == 0x01
4997            and i2c_smbus_read_byte_data($file, 0x3F) == 0x73;
4998  return 5;
4999}
5000
5001# $_[0]: A reference to the file descriptor to access this chip.
5002# $_[1]: Address
5003# Returns: undef if not detected, (7) if detected.
5004# Registers used:
5005#   0x3F: Revision ID
5006#   0x48: Address
5007#   0x4A, 0x4B, 0x4F, 0x57, 0x58: Reserved bits.
5008# We do not use 0x49's reserved bits on purpose. The register is named
5009# "VID4/Device ID" so it is doubtful bits 7-1 are really unused.
5010sub m5879_detect
5011{
5012  my ($file, $addr) = @_;
5013
5014  return
5015    unless i2c_smbus_read_byte_data($file, 0x3F) == 0x01;
5016
5017  return
5018    unless i2c_smbus_read_byte_data($file, 0x48) == $addr;
5019
5020  return
5021    unless (i2c_smbus_read_byte_data($file, 0x4A) & 0x06) == 0
5022       and (i2c_smbus_read_byte_data($file, 0x4B) & 0xFC) == 0
5023       and (i2c_smbus_read_byte_data($file, 0x4F) & 0xFC) == 0
5024       and (i2c_smbus_read_byte_data($file, 0x57) & 0xFE) == 0
5025       and (i2c_smbus_read_byte_data($file, 0x58) & 0xEF) == 0;
5026
5027  return (7);
5028}
5029
5030# $_[0]: A reference to the file descriptor to access this chip.
5031# $_[1]: Address
5032# Returns: undef if not detected, 5 or 6 if detected.
5033# Registers used:
5034#   0x3E: Manufacturer ID
5035#   0x3F: Version/Stepping
5036#   0x47: VID (3 reserved bits)
5037#   0x49: VID4 (7 reserved bits)
5038sub smsc47m192_detect
5039{
5040  my ($file, $addr) = @_;
5041  return unless i2c_smbus_read_byte_data($file, 0x3E) == 0x55
5042           and (i2c_smbus_read_byte_data($file, 0x3F) & 0xF0) == 0x20
5043           and (i2c_smbus_read_byte_data($file, 0x47) & 0x70) == 0x00
5044           and (i2c_smbus_read_byte_data($file, 0x49) & 0xFE) == 0x80;
5045  return ($addr == 0x2d ? 6 : 5);
5046}
5047
5048# $_[0]: Chip to detect
5049#        (1 = DME1737, 2 = SCH5027)
5050# $_[1]: A reference to the file descriptor to access this chip.
5051# $_[2]: Address
5052# Returns: undef if not detected, 5 or 6 if detected.
5053# Registers used:
5054#   0x3E: Manufacturer ID
5055#   0x3F: Version/Stepping
5056#   0x73: Read-only test register (4 test bits)
5057#   0x8A: Read-only test register (7 test bits)
5058#   0xBA: Read-only test register (8 test bits)
5059sub dme1737_detect
5060{
5061  my ($chip, $file, $addr) = @_;
5062  my $vendor = i2c_smbus_read_byte_data($file, 0x3E);
5063  my $verstep = i2c_smbus_read_byte_data($file, 0x3F);
5064
5065  return unless $vendor == 0x5C; # SMSC
5066
5067  if ($chip == 1) { # DME1737
5068      return unless ($verstep & 0xF8) == 0x88 and
5069          (i2c_smbus_read_byte_data($file, 0x73) & 0x0F) == 0x09 and
5070          (i2c_smbus_read_byte_data($file, 0x8A) & 0x7F) == 0x4D;
5071  } elsif ($chip == 2) { # SCH5027
5072      return unless $verstep >= 0x69 and $verstep <= 0x6F and
5073          i2c_smbus_read_byte_data($file, 0xBA) == 0x0F;
5074  }
5075
5076  return ($addr == 0x2e ? 6 : 5);
5077}
5078
5079# $_[0]: Chip to detect
5080#   (1 = F75111R/RG/N, 2 = F75121R/F75122R/RG, 3 = F75373S/SG,
5081#    4 = F75375S/SP, 5 = F75387SG/RG, 6 = F75383M/S/F75384M/S,
5082#    7 = custom power control IC)
5083# $_[1]: A reference to the file descriptor to access this chip.
5084# $_[2]: Address (unused)
5085# Returns: undef if not detected, 7 if detected.
5086# Registers used:
5087#   0x5A-0x5B: Chip ID
5088#   0x5D-0x5E: Vendor ID
5089sub fintek_detect
5090{
5091  my ($chip, $file, $addr) = @_;
5092  my $chipid = (i2c_smbus_read_byte_data($file, 0x5A) << 8)
5093             | i2c_smbus_read_byte_data($file, 0x5B);
5094  my $vendid = (i2c_smbus_read_byte_data($file, 0x5D) << 8)
5095             | i2c_smbus_read_byte_data($file, 0x5E);
5096
5097  return unless $vendid == 0x1934; # Fintek ID
5098
5099  if ($chip == 1) { # F75111R/RG/N
5100    return unless $chipid == 0x0300;
5101  } elsif ($chip == 2) { # F75121R/F75122R/RG
5102    return unless $chipid == 0x0301;
5103  } elsif ($chip == 3) { # F75373S/SG
5104    return unless $chipid == 0x0204;
5105  } elsif ($chip == 4) { # F75375S/SP
5106    return unless $chipid == 0x0306;
5107  } elsif ($chip == 5) { # F75387SG/RG
5108    return unless $chipid == 0x0410;
5109  } elsif ($chip == 6) { # F75383M/S/F75384M/S
5110    # The datasheet has 0x0303, but Fintek say 0x0413 is also possible
5111    return unless $chipid == 0x0303 || $chipid == 0x0413;
5112  } elsif ($chip == 7) { # custom power control IC
5113    return unless $chipid == 0x0302;
5114  }
5115
5116  return 7;
5117}
5118
5119# This checks for non-FFFF values for temperature, voltage, and current.
5120# The address (0x0b) is specified by the SMBus standard so it's likely
5121# that this really is a smart battery.
5122# $_[0]: A reference to the file descriptor to access this chip.
5123# $_[1]: Address
5124# Returns: 5
5125sub smartbatt_detect
5126{
5127  my ($file, $addr) = @_;
5128  # check some registers
5129  if (i2c_smbus_read_word_data($file, 0x08) == 0xffff) {
5130        return;
5131  }
5132  if (i2c_smbus_read_word_data($file, 0x09) == 0xffff) {
5133        return;
5134  }
5135  if (i2c_smbus_read_word_data($file, 0x0a) == 0xffff) {
5136        return;
5137  }
5138  return (5);
5139}
5140
5141# Returns: 4
5142# These are simple detectors that only look for a register at the
5143# standard location. No writes are performed.
5144# For KCS, use the STATUS register. For SMIC, use the FLAGS register.
5145sub ipmi_kcs_detect
5146{
5147  return if inb(0x0ca3) == 0xff;
5148  return (4);
5149}
5150
5151sub ipmi_smic_detect
5152{
5153  return if inb(0x0cab) == 0xff;
5154  return (4);
5155}
5156
5157# $_[0]: Chip to detect (0 = W83L784R/AR/G, 1 = W83L785R/G,
5158#                        2 = W83L786NR/NG/R/G)
5159# $_[1]: A reference to the file descriptor to access this chip.
5160# $_[2]: Address
5161# Returns: undef if not detected, 6 or 8 if detected
5162# Registers used:
5163#   0x40: Configuration
5164#   0x4a: Full I2C Address (W83L784R only)
5165#   0x4b: I2C addresses of emulated LM75 chips (W83L784R only)
5166#   0x4c: Winbond Vendor ID (Low Byte)
5167#   0x4d: Winbond Vendor ID (High Byte)
5168#   0x4e: Chip ID
5169sub w83l784r_detect
5170{
5171  my ($reg, @res);
5172  my ($chip, $file, $addr) = @_;
5173
5174  return unless (i2c_smbus_read_byte_data($file, 0x40) & 0x80) == 0x00;
5175  return if $chip == 0
5176    and i2c_smbus_read_byte_data($file, 0x4a) != $addr;
5177  return unless i2c_smbus_read_byte_data($file, 0x4c) == 0xa3;
5178  return unless i2c_smbus_read_byte_data($file, 0x4d) == 0x5c;
5179
5180  $reg = i2c_smbus_read_byte_data($file, 0x4e);
5181  return if $chip == 0 and $reg != 0x50;
5182  return if $chip == 1 and $reg != 0x60;
5183  return if $chip == 2 and $reg != 0x80;
5184
5185  return 6 if $chip != 0; # No subclients
5186
5187  @res = (8);
5188  $reg = i2c_smbus_read_byte_data($file, 0x4b);
5189  push @res, ($reg & 0x07) + 0x48 unless $reg & 0x08;
5190  push @res, (($reg & 0x70) >> 4) + 0x48 unless $reg & 0x80;
5191  return @res;
5192}
5193
5194# $_[0]: Chip to detect (0 = W83L785TS-S)
5195# $_[1]: A reference to the file descriptor to access this chip.
5196# $_[2]: Address
5197# Returns: undef if not detected, 8 if detected
5198# Registers used:
5199#   0x4C-4E: Mfr and Chip ID
5200sub w83l785ts_detect
5201{
5202  my ($chip, $file, $addr) = @_;
5203  return unless i2c_smbus_read_byte_data($file, 0x4c) == 0xa3;
5204  return unless i2c_smbus_read_byte_data($file, 0x4d) == 0x5c;
5205  return unless i2c_smbus_read_byte_data($file, 0x4e) == 0x70;
5206  return (8);
5207}
5208
5209# $_[0]: Chip to detect. Always zero for now, but available for future use
5210#        if somebody finds a way to distinguish MAX6650 and MAX6651.
5211# $_[1]: A reference to the file descriptor to access this chip.
5212# $_[2]: Address
5213# Returns: undef if not detected, 3 if detected.
5214#
5215# The max6650 has no device ID register. However, a few registers have
5216# spare bits, which are documented as being always zero on read. We read
5217# all of these registers check the spare bits. Any non-zero means this
5218# is not a max6650/1.
5219#
5220# The always zero bits are:
5221#   configuration byte register (0x02) - top 2 bits
5222#   gpio status register (0x14) - top 3 bits
5223#   alarm enable register (0x08) - top 3 bits
5224#   alarm status register (0x0A) - top 3 bits
5225#   tachometer count time register (0x16) - top 6 bits
5226# Additionally, not all values are possible for lower 3 bits of
5227# the configuration register.
5228sub max6650_detect
5229{
5230  my ($chip, $file) = @_;
5231
5232  my $conf = i2c_smbus_read_byte_data($file, 0x02);
5233
5234  return if i2c_smbus_read_byte_data($file, 0x16) & 0xFC;
5235  return if i2c_smbus_read_byte_data($file, 0x0A) & 0xE0;
5236  return if i2c_smbus_read_byte_data($file, 0x08) & 0xE0;
5237  return if i2c_smbus_read_byte_data($file, 0x14) & 0xE0;
5238  return if ($conf & 0xC0) or ($conf & 0x07) > 4;
5239
5240  return 3;
5241}
5242
5243# $_[0]: Chip to detect. Always zero.
5244# $_[1]: A reference to the file descriptor to access this chip.
5245# $_[2]: Address.
5246#
5247# Returns: undef if not detected, 6 if detected.
5248sub max6655_detect
5249{
5250  my ($chip, $file, $addr) = @_;
5251
5252  # checking RDID (Device ID)
5253  return unless i2c_smbus_read_byte_data($file, 0xfe) == 0x0a;
5254  # checking RDRV (Manufacturer ID)
5255  return unless i2c_smbus_read_byte_data($file, 0xff) == 0x4d;
5256  # checking unused bits (conversion rate, extended temperature)
5257  return unless i2c_smbus_read_byte_data($file, 0x04) & 0xf8;
5258  return unless i2c_smbus_read_byte_data($file, 0x10) & 0x1f;
5259  return unless i2c_smbus_read_byte_data($file, 0x11) & 0x1f;
5260  return unless i2c_smbus_read_byte_data($file, 0x12) & 0x1f;
5261
5262  return 6;
5263}
5264
5265# $_[0]: Chip to detect (0 = VT1211)
5266# $_[1]: A reference to the file descriptor to access this chip.
5267# $_[2]: Address
5268#
5269# This isn't very good detection.
5270# Verify the i2c address, and the stepping ID (which is 0xb0 on
5271# my chip but could be different for others...
5272#
5273sub vt1211_i2c_detect
5274{
5275  my ($chip, $file, $addr) = @_;
5276  return unless (i2c_smbus_read_byte_data($file, 0x48) & 0x7f) == $addr;
5277  return unless i2c_smbus_read_byte_data($file, 0x3f) == 0xb0;
5278  return 2;
5279}
5280
5281# $_[0]: ISA address
5282# $_[1]: I2C file handle
5283# $_[2]: I2C address
5284sub vt1211_alias_detect
5285{
5286  my ($isa_addr, $file, $i2c_addr) = @_;
5287  my $i;
5288  return 0 unless (inb($isa_addr + 0x48) & 0x7f) == $i2c_addr;
5289  for ($i = 0x2b; $i <= 0x3d; $i++) {
5290    return 0 unless inb($isa_addr + $i) == i2c_smbus_read_byte_data($file, $i);
5291  }
5292  return 1;
5293}
5294
5295
5296######################
5297# PCI CHIP DETECTION #
5298######################
5299
5300# Returns: undef if not detected, (9) if detected.
5301# The address is encoded in PCI space. We could decode it and print it.
5302sub sis5595_pci_detect
5303{
5304        return unless exists $pci_list{'1039:0008'};
5305        return 9;
5306}
5307
5308# Returns: undef if not detected, (9) if detected.
5309# The address is encoded in PCI space. We could decode it and print it.
5310sub via686a_pci_detect
5311{
5312        return unless exists $pci_list{'1106:3057'};
5313        return 9;
5314}
5315
5316# Returns: undef if not detected, (9) if detected.
5317# The address is encoded in PCI space. We could decode it and print it.
5318sub via8231_pci_detect
5319{
5320        return unless exists $pci_list{'1106:8235'};
5321        return 9;
5322}
5323
5324# Returns: undef if not detected, (9) if detected.
5325sub k8temp_pci_detect
5326{
5327        return unless exists $pci_list{'1022:1103'};
5328        return 9;
5329}
5330
5331sub k10temp_pci_detect
5332{
5333        return unless exists $pci_list{'1022:1203'};
5334        return 9;
5335}
5336
5337# Returns: undef if not detected, (9) if detected.
5338sub intel_amb_detect
5339{
5340        if ((exists $pci_list{'8086:25f0'}) ||  # Intel 5000
5341            (exists $pci_list{'8086:4030'})) {  # Intel 5400
5342                return 9;
5343        }
5344        return;
5345}
5346
5347# Returns: undef if not detected, (9) if detected.
5348sub coretemp_detect
5349{
5350        my $probecpu;
5351        foreach $probecpu (@cpu) {
5352                if ($probecpu->{'vendor_id'} eq 'GenuineIntel' &&
5353                                $probecpu->{'cpu family'} == 6 &&
5354                                ($probecpu->{'model'} == 14 ||
5355                                 $probecpu->{'model'} == 15 ||
5356                                 $probecpu->{'model'} == 0x16 ||
5357                                 $probecpu->{'model'} == 0x17)) {
5358                        return 9;
5359                }
5360        }
5361        return;
5362}
5363
5364# Returns: undef if not detected, (9) if detected.
5365sub c7temp_detect
5366{
5367        my $probecpu;
5368        foreach $probecpu (@cpu) {
5369                if ($probecpu->{'vendor_id'} eq 'CentaurHauls' &&
5370                                $probecpu->{'cpu family'} == 6 &&
5371                                ($probecpu->{'model'} == 0xa ||
5372                                 $probecpu->{'model'} == 0xd)) {
5373                        return 9;
5374                }
5375        }
5376        return;
5377}
5378
5379################
5380# MAIN PROGRAM #
5381################
5382
5383# $_[0]: reference to a list of chip hashes
5384sub print_chips_report
5385{
5386  my ($listref) = @_;
5387  my $data;
5388
5389  foreach $data (@$listref) {
5390    my $is_i2c = exists $data->{i2c_addr};
5391    my $is_isa = exists $data->{isa_addr};
5392    print "  * ";
5393    if ($is_i2c) {
5394      printf "Bus `%s'\n", $data->{i2c_adap};
5395      printf "    Busdriver `%s', I2C address 0x%02x",
5396             $data->{i2c_driver}, $data->{i2c_addr};
5397      if (exists $data->{i2c_sub_addrs}) {
5398        print " (and";
5399        my $sub_addr;
5400        foreach $sub_addr (@{$data->{i2c_sub_addrs}}) {
5401          printf " 0x%02x", $sub_addr;
5402        }
5403        print ")"
5404      }
5405      print "\n    ";
5406    }
5407    if ($is_isa) {
5408      print "ISA bus";
5409      if ($data->{isa_addr}) {
5410        printf ", address 0x%x", $data->{isa_addr};
5411      }
5412      print " (Busdriver `i2c-isa')"
5413        unless kernel_version_at_least(2, 6, 18);
5414      print "\n    ";
5415    }
5416    printf "Chip `%s' (confidence: %d)\n",
5417           $data->{chipname},  $data->{conf};
5418  }
5419}
5420
5421# We build here an array adapters, indexed on the number the adapter has
5422# at this moment (we assume only loaded adapters are interesting at all;
5423# everything that got scanned also got loaded). Each entry is a reference
5424# to a hash containing:
5425#  driver: Name of the adapter driver
5426#  nr_now: Number of the bus now
5427#  nr_later: Number of the bus when the modprobes are done (not included if the
5428#        driver should not be loaded)
5429# A second array, called
5430sub generate_modprobes
5431{
5432  my ($chip, $detection, $nr, $i, @optionlist, @probelist, $driver, $isa, $adap);
5433  my $ipmi = 0;
5434  my $modprobes = "";
5435  my $configfile;
5436
5437  # Collect all adapters used
5438  $nr = 0;
5439  $isa = 0;
5440  foreach $chip (@chips_detected) {
5441    foreach $detection (@{$chip->{detected}}) {
5442      # If there is more than one bus detected by a driver, they are
5443      # still all added. So we number them in the correct order
5444      if (exists $detection->{i2c_driver} and
5445          not exists $i2c_adapters[$detection->{i2c_devnr}]->{nr_later} and
5446          not exists $detection->{isa_addr}) { # Always use ISA access if possible
5447         foreach $adap (@i2c_adapters) {
5448           next unless exists $adap->{driver};
5449           $adap->{nr_later} = $nr++ if $adap->{driver} eq $detection->{i2c_driver};
5450         }
5451      }
5452      if (exists $detection->{isa_addr}) {
5453           $isa = 1;
5454      }
5455      if ($chip->{driver} eq "bmcsensors" ||
5456          $chip->{driver} eq "ipmisensors") {
5457           $ipmi = 1;
5458      }
5459    }
5460  }
5461
5462  $modprobes .= "# I2C adapter drivers\n" if $nr;
5463  for ($i = 0; $i < $nr; $i++) {
5464    foreach $adap (@i2c_adapters) {
5465      next unless exists $adap->{nr_later} and $adap->{nr_later} == $i;
5466      if ($adap->{driver} eq "UNKNOWN") {
5467        $modprobes .= "# modprobe unknown adapter ".$adap->{name}."\n";
5468      } else {
5469        $modprobes .= "modprobe $adap->{driver}\n"
5470          unless $modprobes =~ /modprobe $adap->{driver}\n/;
5471      }
5472      last;
5473    }
5474  }
5475  # i2c-isa is loaded automatically (as a dependency) since 2.6.14,
5476  # and will soon be gone.
5477  $modprobes .= "modprobe i2c-isa\n" if ($isa && !kernel_version_at_least(2, 6, 18));
5478  if ($ipmi) {
5479    $modprobes .= "# You must also install and load the IPMI modules\n";
5480    $modprobes .= "modprobe ipmi-si\n";
5481  }
5482
5483  # Now determine the chip probe lines
5484  $modprobes .= "# Chip drivers\n";
5485  foreach $chip (@chips_detected) {
5486    next if not @{$chip->{detected}};
5487    if ($chip->{driver} eq "to-be-written") {
5488      $modprobes .= "# no driver for $chip->{detected}[0]{chipname} yet\n";
5489    } else {
5490       open(local *INPUTFILE, "modprobe -l $chip->{driver} 2>/dev/null |");
5491       local $_;
5492       my $modulefound = 0;
5493       while (<INPUTFILE>) {
5494         if (m@/@) {
5495           $modulefound = 1;
5496           last;
5497         }
5498       }
5499       close(INPUTFILE);
5500       #check return value from modprobe in case modprobe -l isn't supported
5501       if ((($? >> 8) == 0) && ! $modulefound) {
5502         $modprobes .= "# Warning: the required module $chip->{driver} is not currently installed\n".
5503                       "# on your system. For status of 2.6 kernel ports check\n".
5504                       "# http://www.lm-sensors.org/wiki/Devices. If driver is built\n".
5505                       "# into the kernel, or unavailable, comment out the following line.\n";
5506       }
5507       $modprobes .= "modprobe $chip->{driver}\n";
5508    }
5509
5510    # Handle misdetects
5511    foreach $detection (@{$chip->{misdetected}}) {
5512      push @optionlist, $i2c_adapters[$detection->{i2c_devnr}]->{nr_later},
5513                       $detection->{i2c_addr}
5514           if exists $detection->{i2c_addr} and
5515              exists $i2c_adapters[$detection->{i2c_devnr}]->{nr_later};
5516      push @optionlist, -1, $detection->{isa_addr}
5517           if exists $detection->{isa_addr};
5518    }
5519
5520    # Handle aliases
5521    foreach $detection (@{$chip->{detected}}) {
5522      if (exists $detection->{i2c_driver} and
5523          exists $detection->{isa_addr} and
5524          exists $i2c_adapters[$detection->{i2c_devnr}]->{nr_later}) {
5525        push @optionlist, $i2c_adapters[$detection->{i2c_devnr}]->{nr_later},
5526                          $detection->{i2c_addr};
5527      }
5528    }
5529
5530    next if not (@probelist or @optionlist);
5531    $configfile = "# hwmon module options\n" unless defined $configfile;
5532    $configfile .= "options $chip->{driver}";
5533    $configfile .= sprintf(" ignore=%d,0x%02x", shift @optionlist,
5534                                               shift @optionlist)
5535                  if @optionlist;
5536    $configfile .= sprintf(",%d,0x%02x", shift @optionlist, shift @optionlist)
5537                  while @optionlist;
5538    $configfile .= sprintf(" probe=%d,0x%02x", shift @probelist,
5539                                              shift @probelist)
5540                  if @probelist;
5541    $configfile .= sprintf(",%d,0x%02x", shift @probelist, shift @probelist)
5542                  while @probelist;
5543    $configfile .= "\n";
5544  }
5545
5546  return ($modprobes, $configfile);
5547
5548}
5549
5550sub main
5551{
5552  my (@adapters, $res, $did_adapter_detection, $adapter);
5553
5554  # We won't go very far if not root
5555  unless ($> == 0) {
5556    print "You need to be root to run this script.\n";
5557    exit -1;
5558  }
5559
5560  if (-x "/sbin/service" && -f "/etc/init.d/lm_sensors" &&
5561      -f "/var/lock/subsys/lm_sensors") {
5562    system("/sbin/service", "lm_sensors", "stop");
5563  }
5564
5565  initialize_kernel_version();
5566  initialize_conf();
5567  initialize_pci();
5568  initialize_modules_list();
5569  # make sure any special case chips are added to the chip_ids list before
5570  # making the support modules list
5571  chip_special_cases();
5572  initialize_modules_supported();
5573  initialize_cpu_list();
5574
5575  print "# sensors-detect revision $revision\n\n";
5576
5577  print "This program will help you determine which kernel modules you need\n",
5578        "to load to use lm_sensors most effectively. It is generally safe\n",
5579        "and recommended to accept the default answers to all questions,\n",
5580        "unless you know what you're doing.\n";
5581  print "\n";
5582
5583  print "We can start with probing for (PCI) I2C or SMBus adapters.\n";
5584  print "Do you want to probe now? (YES/no): ";
5585  @adapters = adapter_pci_detection()
5586                        if ($did_adapter_detection = not <STDIN> =~ /\s*[Nn]/);
5587  print "\n";
5588
5589  if (not $did_adapter_detection) {
5590    print "As you skipped adapter detection, we will only scan already loaded\n".
5591          "adapter modules.\n";
5592  } else {
5593    print "We will now try to load each adapter module in turn.\n"
5594      if (@adapters);
5595    foreach $adapter (@adapters) {
5596      if (exists($modules_list{$adapter})) {
5597        print "Module `$adapter' already loaded.\n";
5598      } else {
5599        print "Load `$adapter' (say NO if built into your kernel)? (YES/no): ";
5600        unless (<STDIN> =~ /^\s*[Nn]/) {
5601          if (system("modprobe", $adapter)) {
5602            print "Loading failed... skipping.\n";
5603          } else {
5604            print "Module loaded successfully.\n";
5605          }
5606        }
5607      }
5608    }
5609  }
5610
5611  print "If you have undetectable or unsupported I2C/SMBus adapters, you can have\n".
5612        "them scanned by manually loading the modules before running this script.\n\n";
5613  initialize_i2c_adapters_list();
5614
5615  if (! -e "$sysfs_root/class/i2c-dev") {
5616    print "To continue, we need module `i2c-dev' to be loaded.\n";
5617    print "Do you want to load `i2c-dev' now? (YES/no): ";
5618    if (<STDIN> =~ /^\s*n/i) {
5619      print "Well, you will know best.\n";
5620    } elsif (system("modprobe", "i2c-dev")) {
5621      print "Loading failed, expect problems later on.\n";
5622    } else {
5623      print "Module loaded successfully.\n";
5624    }
5625    print "\n";
5626  }
5627
5628  $i2c_addresses_to_scan = i2c_addresses_to_scan();
5629
5630  print "We are now going to do the I2C/SMBus adapter probings. Some chips may\n",
5631        "be double detected; we choose the one with the highest confidence\n",
5632        "value in that case.\n",
5633        "If you found that the adapter hung after probing a certain address,\n",
5634        "you can specify that address to remain unprobed.\n";
5635
5636  my ($inp, @not_to_scan, $inp2);
5637  for (my $dev_nr = 0; $dev_nr < @i2c_adapters; $dev_nr++) {
5638    next unless exists $i2c_adapters[$dev_nr];
5639    my $adap = $i2c_adapters[$dev_nr]->{'name'};
5640    print "\n";
5641    print "Next adapter: $adap (i2c-$dev_nr)\n";
5642    print "Do you want to scan it? (YES/no/selectively): ";
5643
5644    $inp = <STDIN>;
5645    if ($inp =~ /^\s*[Ss]/) {
5646      print "Please enter one or more addresses not to scan. Separate them ",
5647            "with comma's.\n",
5648            "You can specify a range by using dashes. Addresses may be ",
5649            "decimal (like 54)\n",
5650            "or hexadecimal (like 0x33).\n",
5651            "Addresses: ";
5652      $inp2 = <STDIN>;
5653      chop($inp2);
5654      @not_to_scan = parse_not_to_scan(0x03, 0x77, $inp2);
5655    }
5656    scan_adapter($dev_nr, $adap, $i2c_adapters[$dev_nr]->{'driver'},
5657                 \@not_to_scan) unless $inp =~ /^\s*[Nn]/;
5658  }
5659  print "\n";
5660
5661  # Skip "random" I/O port probing on PPC
5662  if ($kernel_arch ne 'ppc'
5663   && $kernel_arch ne 'ppc64') {
5664    print "Some chips are also accessible through the ISA I/O ports. We have to\n".
5665          "write to arbitrary I/O ports to probe them. This is usually safe though.\n".
5666          "Yes, you do have ISA I/O ports even if you do not have any ISA slots!\n";
5667    print "Do you want to scan the ISA I/O ports? (YES/no): ";
5668    unless (<STDIN> =~ /^\s*n/i) {
5669      initialize_ioports();
5670      scan_isa_bus();
5671      close_ioports();
5672    }
5673    print "\n";
5674
5675    print "Some Super I/O chips may also contain sensors. We have to write to\n".
5676          "standard I/O ports to probe them. This is usually safe.\n";
5677    print "Do you want to scan for Super I/O sensors? (YES/no): ";
5678    unless (<STDIN> =~ /^\s*n/i) {
5679      initialize_ioports();
5680      scan_superio(0x2e, 0x2f);
5681      scan_superio(0x4e, 0x4f);
5682      close_ioports();
5683    }
5684    print "\n";
5685  }
5686
5687  print "Some south bridges, CPUs or memory controllers may also contain\n";
5688  print "embedded sensors. Do you want to scan for them? (YES/no): ";
5689  unless (<STDIN> =~ /^\s*n/i) {
5690    $| = 1;
5691    foreach my $entry (@cpu_ids) {
5692      scan_cpu($entry);
5693    }
5694    $| = 0;
5695  }
5696  print "\n";
5697
5698  if (! @chips_detected) {
5699    print "Sorry, no sensors were detected.\n",
5700          "Either your sensors are not supported, or they are connected to an\n",
5701          "I2C or SMBus adapter that is not supported. See\n",
5702          "http://www.lm-sensors.org/wiki/FAQ/Chapter3 for further information.\n",
5703          "If you find out what chips are on your board, check\n",
5704          "http://www.lm-sensors.org/wiki/Devices for driver status.\n";
5705    exit;
5706  }
5707
5708  print "Now follows a summary of the probes I have just done.\n".
5709        "Just press ENTER to continue: ";
5710  <STDIN>;
5711
5712  my ($chip, $data);
5713  foreach $chip (@chips_detected) {
5714    print "\nDriver `$chip->{driver}' ";
5715    if (@{$chip->{detected}}) {
5716      if (@{$chip->{misdetected}}) {
5717        print "(should be inserted but causes problems):\n";
5718      } else {
5719        print "(should be inserted):\n";
5720      }
5721    } else {
5722      if (@{$chip->{misdetected}}) {
5723        print "(may not be inserted):\n";
5724      } else {
5725        print "(should not be inserted, but is harmless):\n";
5726      }
5727    }
5728    if (@{$chip->{detected}}) {
5729      print "  Detects correctly:\n";
5730      print_chips_report($chip->{detected});
5731    }
5732    if (@{$chip->{misdetected}}) {
5733      print "  Misdetects:\n";
5734      print_chips_report($chip->{misdetected});
5735    }
5736  }
5737  print "\n";
5738
5739  my ($modprobes, $configfile) = generate_modprobes();
5740
5741  if (defined $configfile) {
5742    my $have_modprobe_d = -d '/etc/modprobe.d';
5743    printf "Do you want to \%s /etc/modprobe.d/lm_sensors? (\%s): ",
5744           (-e '/etc/modprobe.d/lm_sensors' ? 'overwrite' : 'generate'),
5745           ($have_modprobe_d ? 'YES/no' : 'yes/NO');
5746    $_ = <STDIN>;
5747    if (($have_modprobe_d and not m/^\s*n/i) or m/^\s*y/i) {
5748      unless ($have_modprobe_d) {
5749        mkdir('/etc/modprobe.d', 0777)
5750          or die "Sorry, can't create /etc/modprobe.d ($!)";
5751      }
5752      open(local *MODPROBE_D, ">/etc/modprobe.d/lm_sensors")
5753        or die "Sorry, can't create /etc/modprobe.d/lm_sensors ($!)";
5754      print MODPROBE_D
5755        "# Generated by sensors-detect on " . scalar localtime() . "\n";
5756      print MODPROBE_D $configfile;
5757      close(MODPROBE_D);
5758    } else {
5759      print "To make the sensors modules behave correctly, add these lines to\n".
5760            "/etc/modprobe.conf:\n\n";
5761      print "#----cut here----\n".
5762            $configfile.
5763            "#----cut here----\n\n";
5764    }
5765  }
5766
5767  my $have_sysconfig = -d '/etc/sysconfig';
5768  printf "Do you want to \%s /etc/sysconfig/lm_sensors? (\%s): ",
5769         (-e '/etc/sysconfig/lm_sensors' ? 'overwrite' : 'generate'),
5770         ($have_sysconfig ? 'YES/no' : 'yes/NO');
5771  $_ = <STDIN>;
5772  if (($have_sysconfig and not m/^\s*n/i) or m/^