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

Revision 5495, 141.8 KB (checked in by khali, 4 years ago)

Fix calling convention comment.

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