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

Revision 5279, 179.8 KB (checked in by khali, 6 years ago)

Lower confidence of the mtp008 detection.

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