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

Revision 5407, 175.3 KB (checked in by khali, 5 years ago)

Get I2C adapter driver names from sysfs. The old method (matching against
regexp for known I2C adapters) is still used as a fallback, as early 2.6
kernels don't support the new method, and some drivers (i2c-viapro for
example) do not bind to their device so the new method doesn't work for
them.

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