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

Revision 5281, 180.1 KB (checked in by khali, 7 years ago)

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