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

Revision 5421, 163.5 KB (checked in by khali, 4 years ago)

Harmonize the order of the parameters of all I2C detection functions.
The purpose of such an admittedly intrusive change is that we then no
longer need to document the calling convention of each function
individually, which spares a huge amount of comments.

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