root/lm-sensors/trunk/prog/detect/sensors-detect @ 5564

Revision 5564, 178.4 KB (checked in by khali, 6 years ago)

Add Intel Core I7 support. Patch from Reinhard Nissl.

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