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

Revision 5460, 151.3 KB (checked in by khali, 4 years ago)

Move non-hwmon I2C device definitions to a separate array.

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