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

Revision 5094, 174.1 KB (checked in by khali, 7 years ago)

Add detection of the SMSC SCH5514 Super-IO. The chip doesn't contain
HW monitoring features. Patch from Juerg Haefliger.

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