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

Revision 5248, 176.2 KB (checked in by khali, 5 years ago)

Rework all device detection routines which use SMBus read word
transactions in address range 0x40-0x48. Some devices don't like word
transactions, so we now try to do as much of the detection as possible
using byte transactions, and only use word transactions when there is
a good chance that the detection will succeed.

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