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

Revision 5450, 156.8 KB (checked in by khali, 4 years ago)

Reindent several functions.

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