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

Revision 668, 63.7 KB (checked in by frodo, 13 years ago)

Adrian's cosmetic newline

  • 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
2
3#
4#    sensors-detect - Detect PCI bus and chips
5#    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
6#
7#    This program is free software; you can redistribute it and/or modify
8#    it under the terms of the GNU General Public License as published by
9#    the Free Software Foundation; either version 2 of the License, or
10#    (at your option) any later version.
11#
12#    This program is distributed in the hope that it will be useful,
13#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#    GNU General Public License for more details.
16#
17#    You should have received a copy of the GNU General Public License
18#    along with this program; if not, write to the Free Software
19#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20#
21
22# TODO: Better handling of chips with several addresses
23
24# A Perl wizard really ought to look upon this; the PCI and I2C stuff should
25# each be put in a separate file, using modules and packages. That is beyond
26# me.
27
28require 5.004;
29
30use strict;
31
32#########################
33# CONSTANT DECLARATIONS #
34#########################
35
36use vars qw(@pci_adapters @chip_ids @undetectable_adapters);
37
38@undetectable_adapters = ( "i2c-elektor","i2c-elv", "i2c-philips-par",
39                           "i2c-velleman" );
40
41# This is the list of SMBus or I2C adapters we recognize by their PCI
42# signature. This is an easy and fast way to determine which SMBus or I2C
43# adapters should be present.
44# Each entry must have a vindid (Vendor ID), devid (Device ID), func (PCI
45# Function) and procid (string as appears in /proc/pci; see linux/driver/pci,
46# either pci.c or oldproc.c). If no driver is written yet, omit the
47# driver (Driver Name) field. The match (Match Description) field should
48# contain a function which returns zero if its two parameter matches
49# the text as it would appear in /proc/bus/i2c.
50@pci_adapters = ( 
51     { 
52       vendid => 0x8086,
53       devid  => 0x7113,
54       func => 3,
55       procid => "Intel 82371AB PIIX4 ACPI",
56       driver => "i2c-piix4",
57       match => sub { $_[0] =~ /^SMBus PIIX4 adapter at [0-9,a-f]{4}/ },
58     } , 
59     { 
60       vendid => 0x8086,
61       devid  => 0x2413,
62       func => 3,
63       procid => "Intel 82801AA ICH",
64       driver => "i2c-i801",
65       match => sub { $_[0] =~ /^SMBus i801AA adapter at [0-9,a-f]{4}/ },
66     } , 
67     { 
68       vendid => 0x8086,
69       devid  => 0x2423,
70       func => 3,
71       procid => "Intel 82801AB ICH0",
72       driver => "i2c-i801",
73       match => sub { $_[0] =~ /^SMBus i801AB adapter at [0-9,a-f]{4}/ },
74     } , 
75     { 
76       vendid => 0x1106,
77       devid  => 0x3040,
78       func => 3,
79       procid => "VIA Technologies VT 82C586B Apollo ACPI",
80       driver => "i2c-via",
81       match => sub { $_[0] =~ /^VIA i2c/ },
82     } ,
83     { 
84       vendid => 0x1106,
85       devid  => 0x3050,
86       func => 3,
87       procid => "VIA Technologies VT 82C596 Apollo ACPI",
88       driver => "i2c-viapro",
89       match => sub { $_[0] =~ /^SMBus vt82c596 adapter at [0-9,a-f]{4}/ },
90     } ,
91     { 
92       vendid => 0x1106,
93       devid  => 0x3057,
94       func => 4,
95       procid => "VIA Technologies VT 82C686 Apollo ACPI",
96       driver => "i2c-viapro",
97       match => sub { $_[0] =~ /^SMBus vt82c596 adapter at [0-9,a-f]{4}/ },
98     } ,
99     {
100       vendid => 0x1039,
101       devid  => 0x0008,
102       func => 0,
103       procid => "Silicon Integrated Systems SIS5595",
104       driver => "i2c-sis5595",
105       match => sub {  $_[0] =~ /^SMBus SIS5595 adapter at [0-9,a-f]{4}/ },
106     } ,
107     {
108       vendid => 0x10b9,
109       devid => 0x7101,
110       funcid => 0,
111       procid => "Acer Labs M7101",
112       driver => "i2c-ali15x3",
113       match => sub { $_[0] =~ /^SMBus ALI15X3 adapter at [0-9,a-f]{4}/ },
114     },
115     { 
116       vendid => 0x106b,
117       devid  => 0x000e,
118       func => 0,
119       procid => "Apple Computer Inc. Hydra Mac I/O",
120       driver => "i2c-hydra",
121       match => sub { $_[0] =~ /^Hydra i2c/ },
122     },
123     { 
124       vendid => 0x1022,
125       devid  => 0x740b,
126       func => 3,
127       procid => "AMD-756 Athlon ACPI",
128       driver => "i2c-amd756",
129       match => sub { $_[0] =~ /^SMBus AMD756 adapter at [0-9,a-f]{4}/ },
130     },
131);
132
133use subs qw(lm78_detect lm78_isa_detect lm78_alias_detect lm75_detect
134            lm80_detect w83781d_detect w83781d_alias_detect
135            w83781d_isa_detect gl518sm_detect gl520sm_detect adm9240_detect
136            adm1021_detect sis5595_isa_detect eeprom_detect
137            adm1022_detect ltc1710_detect gl525sm_detect);
138
139# This is a list of all recognized chips.
140# Each entry must have the following fields:
141#  name: The full chip name
142#  driver (optional): The driver name (without .o extension). Omit if none is
143#      written yet.
144#  i2c_addrs (optional): For I2C chips, the range of valid I2C addresses to
145#      probe. Recommend avoiding 0x69 because of clock chips.
146#  i2c_driver_addrs (optional): For I2C chips, the range of valid I2C
147#      addresses probed by the kernel driver. Strictly optional.
148#  i2c_detect (optional): For I2C chips, the function to call to detect
149#      this chip. The function should take two parameters: an open file
150#      descriptor to access the bus, and the I2C address to probe.
151#  isa_addrs (optional): For ISA chips, the range of valid port addresses to
152#      probe.
153#  isa_driver_addrs (optional): For ISA chips, the range of valid ISA
154#      addresses probed by the kernel driver. Strictly optional.
155#  isa_detect (optional): For ISA chips, the function to call to detect
156#      this chip. The function should take one parameter: the ISA address
157#      to probe.
158#  alias_detect (optional): For chips which can be both on the ISA and the
159#      I2C bus, a function which detectes whether two entries are the same.
160#      The function should take three parameters: The ISA address, the
161#      I2C bus number, and the I2C address.
162@chip_ids = (
163     {
164       name => "National Semiconductor LM78",
165       driver => "lm78",
166       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
167       i2c_driver_addrs => [0x20..0x2f], 
168       i2c_detect => sub { lm78_detect 0, @_},
169       isa_addrs => [0x290],
170       isa_detect => sub { lm78_isa_detect 0, @_ },
171       alias_detect => sub { lm78_alias_detect 0, @_ },
172     } ,
173     {
174       name => "National Semiconductor LM78-J",
175       driver => "lm78",
176       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
177       i2c_driver_addrs => [0x20..0x2f], 
178       i2c_detect => sub { lm78_detect 1, @_ },
179       isa_addrs => [0x290],
180       isa_detect => sub { lm78_isa_detect 1, @_ },
181       alias_detect => sub { lm78_alias_detect 1, @_ },
182     } ,
183     {
184       name => "National Semiconductor LM79",
185       driver => "lm78",
186       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
187       i2c_driver_addrs => [0x20..0x2f], 
188       i2c_detect => sub { lm78_detect 2, @_ },
189       isa_addrs => [0x290],
190       isa_detect => sub { lm78_isa_detect 2, @_ },
191       alias_detect => sub { lm78_alias_detect 2, @_ },
192     } ,
193     {
194       name => "National Semiconductor LM75",
195       driver => "lm75",
196       i2c_addrs => [0x48..0x4f],
197       i2c_detect => sub { lm75_detect @_},
198     } ,
199     {
200       name => "National Semiconductor LM80",
201       driver => "lm80",
202       i2c_addrs => [0x28..0x2f],
203       i2c_detect => sub { lm80_detect @_} ,
204     },
205     {
206       name => "Winbond W83781D",
207       driver => "w83781d",
208       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
209       i2c_detect => sub { w83781d_detect 0, @_},
210       i2c_driver_addrs => [0x20..0x2f], 
211       isa_addrs => [0x290],
212       isa_detect => sub { w83781d_isa_detect 0, @_ },
213       alias_detect => sub { w83781d_alias_detect 0, @_ },
214     } ,
215     {
216       name => "Winbond W83782D",
217       driver => "w83781d",
218       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
219       i2c_driver_addrs => [0x20..0x2f], 
220       i2c_detect => sub { w83781d_detect 1, @_},
221       isa_addrs => [0x290],
222       isa_detect => sub { w83781d_isa_detect 1, @_ },
223       alias_detect => sub { w83781d_alias_detect 1, @_ },
224     } ,
225     {
226       name => "Winbond W83783S",
227       driver => "w83781d",
228       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
229       i2c_driver_addrs => [0x20..0x2f], 
230       i2c_detect => sub { w83781d_detect 2, @_},
231     } ,
232     {
233       name => "Winbond W83627HF",
234       driver => "w83781d",
235       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
236       i2c_driver_addrs => [0x20..0x2f], 
237       i2c_detect => sub { w83781d_detect 3, @_},
238       isa_addrs => [0x290],
239       isa_detect => sub { w83781d_isa_detect 3, @_ },
240       alias_detect => sub { w83781d_alias_detect 3, @_ },
241     } ,
242     {
243       name => "Asus AS99127F",
244       driver => "w83781d",
245       i2c_addrs => [0x00..0x68,0x6a..0x7f], 
246       i2c_driver_addrs => [0x20..0x2f], 
247       i2c_detect => sub { w83781d_detect 4, @_},
248     } ,
249     {
250       name => "Genesys Logic GL518SM Revision 0x00",
251       driver => "gl518sm",
252       i2c_addrs => [0x2c, 0x2d],
253       i2c_detect => sub { gl518sm_detect 0, @_} ,
254     },
255     {
256       name => "Genesys Logic GL518SM Revision 0x80",
257       driver => "gl518sm",
258       i2c_addrs => [0x2c, 0x2d],
259       i2c_detect => sub { gl518sm_detect 1, @_} ,
260     },
261     {
262       name => "Genesys Logic GL520SM",
263       driver => "gl520sm",
264       i2c_addrs => [0x2c, 0x2d],
265       i2c_detect => sub { gl520sm_detect @_} ,
266     },
267     {
268       name => "Genesys Logic GL525SM",
269#        driver => "xxxxxxx",
270       i2c_addrs => [0x2d],
271       i2c_detect => sub { gl525sm_detect @_} ,
272     },
273     {
274       name => "Analog Devices ADM9240",
275       driver => "adm9240",
276       i2c_addrs => [0x2c..0x2f],
277       i2c_detect => sub { adm9240_detect 0, @_ }
278     },
279     {
280       name => "Dallas Semiconductor DS1780",
281       driver => "adm9240",
282       i2c_addrs => [0x2c..0x2f],
283       i2c_detect => sub { adm9240_detect 1, @_ }
284     },
285     {
286       name => "National Semiconductor LM81",
287       driver => "adm9240",
288       i2c_addrs => [0x2c..0x2f],
289       i2c_detect => sub { adm9240_detect 2, @_ }
290     },
291     {
292       name => "Analog Devices ADM1021",
293       driver => "adm1021",
294       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
295       i2c_detect => sub { adm1021_detect 0, @_ },
296     },
297     {
298       name => "Maxim MAX1617",
299       driver => "adm1021",
300       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
301       i2c_detect => sub { adm1021_detect 1, @_ },
302     },
303     {
304       name => "Maxim MAX1617A",
305       driver => "adm1021",
306       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
307       i2c_detect => sub { adm1021_detect 2, @_ },
308     },
309     {
310       name => "TI THMC10",
311       driver => "adm1021",
312       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
313       i2c_detect => sub { adm1021_detect 3, @_ },
314     },
315     {
316       name => "National Semiconductor LM84",
317       driver => "adm1021",
318       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
319       i2c_detect => sub { adm1021_detect 4, @_ },
320     },
321     {
322       name => "Genesys Logic GL523SM",
323       driver => "adm1021",
324       i2c_addrs => [0x18..0x1a,0x29..0x2b,0x4c..0x4e],
325       i2c_detect => sub { adm1021_detect 5, @_ },
326     },
327     {
328       name => "Analog Devices ADM1022",
329       driver => "thmc50",
330       i2c_addrs => [0x2c..0x2f],
331       i2c_detect => sub { adm1022_detect 0, @_ },
332     },
333     {
334       name => "Texas Instruments THMC50",
335       driver => "thmc50",
336       i2c_addrs => [0x2c..0x2f],
337       i2c_detect => sub { adm1022_detect 1, @_ },
338     },
339     {
340       name => "Silicon Integrated Systems SIS5595",
341       driver => "sis5595",
342       isa_addrs => [ 0 ],
343       isa_detect => sub { sis5595_isa_detect @_ },
344     },
345     {
346       name => "Serial EEPROM (PC-100 DIMM)",
347       driver => "eeprom",
348       i2c_addrs => [0x50..0x57],
349       i2c_detect => sub { eeprom_detect @_ },
350     },
351     {
352       name => "LTC1710",
353       driver => "ltc1710",
354       i2c_addrs => [0x58..0x5a],
355       i2c_detect => sub { ltc1710_detect @_ },
356     },
357);
358
359
360#######################
361# AUXILIARY FUNCTIONS #
362#######################
363
364sub swap_bytes
365{
366  return (($_[0] & 0xff00) >> 8) + (($_[0] & 0x00ff) << 8)
367}
368
369# $_[0] is the sought value
370# @_[1..] is the list to seek in
371# Returns: 0 on failure, 1 if found.
372sub contains
373{
374  my $sought = shift;
375  foreach (@_) {
376    return 1 if $sought eq $_;
377  }
378  return 0;
379}
380
381sub parse_not_to_scan
382{
383  my ($min,$max,$to_parse) = @_;
384  my @ranges = split /\s*,\s*/, $to_parse;
385  my @res = ();
386  my $range;
387  foreach $range (@ranges) {
388    my ($start,$end) = split /\s*-s*/, $range;
389    $start = oct $start if $start =~ /^0/;
390    if (defined $end) {
391      $end = oct $end if $end =~ /^0/;
392      $start = $min if $start < $min;
393      $end = $max if $end > $max;
394      push @res, ($start+0..$end+0);
395    } else {
396      push @res, $start+0 if $start >= $min and $start <= $max;
397    }
398  }
399  return sort { $a <=> $b } @res;
400}
401
402# @_[0]: Reference to list 1
403# @_[1]: Reference to list 2
404# Result: 0 if they have no elements in common, 1 if they have
405# Elements must be numeric.
406sub any_list_match
407{
408  my ($list1,$list2) = @_;
409  my ($el1,$el2);
410  foreach $el1 (@$list1) {
411    foreach $el2 (@$list2) {
412      return 1 if $el1 == $el2;
413    }
414  }
415  return 0;
416}
417
418###################
419# I/O port access #
420###################
421
422sub initialize_ioports
423{
424  sysopen IOPORTS, "/dev/port", 2;
425}
426
427# $_[0]: port to read
428# Returns: -1 on failure, read value on success.
429sub inb
430{
431  my ($res,$nrchars);
432  sysseek IOPORTS, $_[0], 0 or return -1;
433  $nrchars = sysread IOPORTS, $res, 1;
434  return -1 if not defined $nrchars or $nrchars != 1;
435  $res = unpack "C",$res ;
436  return $res;
437}
438
439# $_[0]: port to write
440# $_[1]: value to write
441# Returns: -1 on failure, 0 on success.
442sub outb
443{
444  my $towrite = pack "C", $_[1];
445  sysseek IOPORTS, $_[0], 0 or return -1;
446  my $nrchars = syswrite IOPORTS, $towrite, 1;
447  return -1 if not defined $nrchars or $nrchars != 1;
448  return 0;
449}
450
451# $_[0]: Address register
452# $_[1]: Data register
453# $_[2]: Register to read
454# Returns: read value
455sub isa_read_byte
456{
457  outb $_[0],$_[2];
458  return inb $_[1];
459}
460
461# $_[0]: Address register
462# $_[1]: Data register
463# $_[2]: Register to write
464# $_[3}: Value to write
465# Returns: nothing
466sub isa_write_byte
467{
468  outb $_[0],$_[2];
469  outb $_[1],$_[3];
470}
471
472###########
473# MODULES #
474###########
475
476use vars qw(@modules_list);
477
478sub initialize_modules_list
479{
480  open INPUTFILE, "/proc/modules" or die "Can't access /proc/modules!";
481  while (<INPUTFILE>) {
482    push @modules_list, /^(\S*)/ ;
483  }
484  close INPUTFILE;
485}
486
487##############
488# PCI ACCESS #
489##############
490
491use vars qw(@pci_list);
492
493# This function returns a list of hashes. Each hash has some PCI information
494# (more than we will ever need, probably). The most important
495# fields are 'bus', 'slot', 'func' (they uniquely identify a PCI device in
496# a computer) and 'vendid','devid' (they uniquely identify a type of device).
497# /proc/bus/pci/devices is only available on late 2.1 and 2.2 kernels.
498sub read_proc_dev_pci
499{
500  my ($dfn,$vend,@pci_list);
501  open INPUTFILE, "/proc/bus/pci/devices" or return;
502  while (<INPUTFILE>) {
503    my $record = {};
504    ($dfn,$vend,$record->{irq},$record->{base_addr0},$record->{base_addr1},
505          $record->{base_addr2},$record->{base_addr3},$record->{base_addr4},
506          $record->{base_addr5},$record->{rom_base_addr}) = 
507          map { oct "0x$_" } split;
508    $record->{bus} = $dfn >> 8;
509    $record->{slot} = ($dfn & 0xf8) >> 3;
510    $record->{func} = $dfn & 0x07;
511    $record->{vendid} = $vend >> 16;
512    $record->{devid} = $vend & 0xffff;
513  push @pci_list,$record;
514  }
515  close INPUTFILE or return;
516  return @pci_list;
517}
518
519# This function returns a list of hashes. Each hash has some PCI
520# information. The important fields here are 'bus', 'slot', 'func' (they
521# uniquely identify a PCI device in a computer) and 'desc' (a functional
522# description of the PCI device). If this is an 'unknown device', the
523# vendid and devid fields are set instead.
524sub read_proc_pci
525{
526  my @pci_list;
527  open INPUTFILE, "/proc/pci" or return;
528  while (<INPUTFILE>) {
529    my $record = {};
530    if (($record->{bus},$record->{slot},$record->{func}) = 
531        /^\s*Bus\s*(\S)+\s*,\s*device\s*(\S+)\s*,\s*function\s*(\S+)\s*:\s*$/) {
532      my $desc = <INPUTFILE>;
533      unless (($desc =~ /Unknown device/) and
534              (($record->{vendid},$record->{devid}) = 
535                         /^\s*Vendor id=(\S+)\.\s*Device id=(\S+)\.$/)) {
536        $record->{desc} = $desc;
537      }
538      push @pci_list,$record;
539    }
540  }
541  close INPUTFILE or return;
542  return @pci_list;
543}
544
545sub initialize_proc_pci
546{
547  @pci_list = read_proc_dev_pci;
548  @pci_list = read_proc_pci     if not defined @pci_list;
549  die "Can't access either /proc/bus/pci/ or /proc/pci!" 
550                                    if not defined @pci_list;
551}
552
553#####################
554# ADAPTER DETECTION #
555#####################
556
557sub all_available_adapters
558{
559  my @res = ();
560  my ($module,$adapter);
561  MODULES:
562  foreach $module (@modules_list) {
563    foreach $adapter (@pci_adapters) {
564      if (exists $adapter->{driver} and $module eq $adapter->{driver}) {
565        push @res, $module;
566        next MODULES;
567      }
568    }
569  }
570  return @res;
571}
572
573sub adapter_pci_detection
574{
575  my ($device,$try,@res);
576  print "Probing for PCI bus adapters...\n";
577
578  foreach $device (@pci_list) {
579    foreach $try (@pci_adapters) {
580      if ((defined($device->{vendid}) and 
581           $try->{vendid} == $device->{vendid} and
582           $try->{devid} == $device->{devid} and
583           $try->{func} == $device->{func}) or
584          (! defined($device->{vendid}) and
585           $device->{desc} =~ /$try->{procid}/ and
586           $try->{func} == $device->{func})) {
587        printf "Use driver `%s' for device %02x:%02x.%x: %s\n",
588               $try->{driver}?$try->{driver}:"<To Be Written>",
589               $device->{bus},$device->{slot},$device->{func},$try->{procid};
590        push @res,$try->{driver};
591      }
592    }
593  }
594  if (! defined @res) {
595    print ("Sorry, no PCI bus adapters found.\n");
596  } else {
597    printf ("Probe succesfully concluded.\n");
598  }
599  return @res;
600}
601
602# $_[0]: Adapter description as found in /proc/bus/i2c
603# $_[1]: Algorithm description as found in /proc/bus/i2c
604sub find_adapter_driver
605{
606  my $adapter;
607  for $adapter (@pci_adapters) {
608    return $adapter->{driver} if &{$adapter->{match}} ($_[0],$_[1]);
609  }
610  return "?????";
611}
612
613#############################
614# I2C AND SMBUS /DEV ACCESS #
615#############################
616
617# This should really go into a separate module/package.
618
619# To do: support i2c-level access (through sysread/syswrite, probably).
620# I can't test this at all (PIIX4 does not support this), so I have not
621# included it.
622
623use vars qw($IOCTL_I2C_RETRIES $IOCTL_I2C_TIMEOUT $IOCTL_I2C_UDELAY
624            $IOCTL_I2C_MDELAY $IOCTL_I2C_SLAVE $IOCTL_I2C_TENBIT
625            $IOCTL_I2C_SMBUS);
626
627# These are copied from <linux/i2c.h> and <linux/smbus.h>
628
629# For bit-adapters:
630$IOCTL_I2C_RETRIES = 0x0701;
631$IOCTL_I2C_TIMEOUT = 0x0702;
632$IOCTL_I2C_UDELAY = 0x0705;
633$IOCTL_I2C_MDELAY = 0x0706;
634
635# General ones:
636$IOCTL_I2C_SLAVE = 0x0703;
637$IOCTL_I2C_TENBIT = 0x0704;
638$IOCTL_I2C_SMBUS = 0x0720;
639
640
641
642use vars qw($SMBUS_READ $SMBUS_WRITE $SMBUS_QUICK $SMBUS_BYTE $SMBUS_BYTE_DATA
643            $SMBUS_WORD_DATA $SMBUS_PROC_CALL $SMBUS_BLOCK_DATA);
644
645# These are copied from <linux/smbus.h>
646
647$SMBUS_READ = 1;
648$SMBUS_WRITE = 0;
649$SMBUS_QUICK = 0;
650$SMBUS_BYTE = 1;
651$SMBUS_BYTE_DATA  = 2;
652$SMBUS_WORD_DATA  = 3;
653$SMBUS_PROC_CALL = 4;
654$SMBUS_BLOCK_DATA = 5;
655
656# Select the device to communicate with through its address.
657# $_[0]: Reference to an opened filehandle
658# $_[1]: Address to select
659# Returns: 0 on failure, 1 on success.
660sub i2c_set_slave_addr
661{
662  my ($file,$addr) = @_;
663  ioctl $file, $IOCTL_I2C_SLAVE, $addr or return 0;
664  return 1;
665}
666
667# i2c_smbus_access is based upon the corresponding C function (see
668# <linux/i2c-dev.h>). You should not need to call this directly.
669# Exact calling conventions are intricate; read i2c-dev.c if you really need
670# to know.
671# $_[0]: Reference to an opened filehandle
672# $_[1]: $SMBUS_READ for reading, $SMBUS_WRITE for writing
673# $_[2]: Command (usually register number)
674# $_[3]: Transaction kind ($SMBUS_BYTE, $SMBUS_BYTE_DATA, etc.)
675# $_[4]: Reference to an array used for input/output of data
676# Returns: 0 on failure, 1 on success.
677# Note that we need to get back to Integer boundaries through the 'x2'
678# in the pack. This is very compiler-dependent; I wish there was some other
679# way to do this.
680sub i2c_smbus_access
681{
682  my ($file,$read_write,$command,$size,$data) = @_;
683  my $data_array = pack "C32", @$data;
684  my $ioctl_data = pack "C2x2Ip", ($read_write,$command,$size,$data_array);
685  ioctl $file, $IOCTL_I2C_SMBUS, $ioctl_data or return 0;
686  $_[4] = [ unpack "C32",$data_array ];
687  return 1;
688}
689
690# $_[0]: Reference to an opened filehandle
691# $_[1]: Either 0 or 1
692# Returns: -1 on failure, the 0 on success.
693sub i2c_smbus_write_quick
694{
695  my ($file,$value) = @_;
696  my $data = [];
697  i2c_smbus_access $file, $value, 0, $SMBUS_QUICK, $data 
698         or return -1;
699  return 0;
700}
701
702# $_[0]: Reference to an opened filehandle
703# Returns: -1 on failure, the read byte on success.
704sub i2c_smbus_read_byte
705{
706  my ($file) = @_;
707  my $data = [];
708  i2c_smbus_access $file, $SMBUS_READ, 0, $SMBUS_BYTE, $data 
709         or return -1;
710  return $$data[0];
711}
712
713# $_[0]: Reference to an opened filehandle
714# $_[1]: Byte to write
715# Returns: -1 on failure, 0 on success.
716sub i2c_smbus_write_byte
717{
718  my ($file,$command) = @_;
719  my $data = [$command];
720  i2c_smbus_access $file, $SMBUS_WRITE, 0, $SMBUS_BYTE, $data 
721         or return -1;
722  return 0;
723}
724
725# $_[0]: Reference to an opened filehandle
726# $_[1]: Command byte (usually register number)
727# Returns: -1 on failure, the read byte on success.
728sub i2c_smbus_read_byte_data
729{
730  my ($file,$command) = @_;
731  my $data = [];
732  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_BYTE_DATA, $data 
733         or return -1;
734  return $$data[0];
735}
736 
737# $_[0]: Reference to an opened filehandle
738# $_[1]: Command byte (usually register number)
739# $_[2]: Byte to write
740# Returns: -1 on failure, 0 on success.
741sub i2c_smbus_write_byte_data
742{
743  my ($file,$command,$value) = @_;
744  my $data = [$value];
745  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_BYTE_DATA, $data 
746         or return -1;
747  return 0;
748}
749
750# $_[0]: Reference to an opened filehandle
751# $_[1]: Command byte (usually register number)
752# Returns: -1 on failure, the read word on success.
753# Note: some devices use the wrong endiannes; use swap_bytes to correct for
754# this.
755sub i2c_smbus_read_word_data
756{
757  my ($file,$command) = @_;
758  my $data = [];
759  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_WORD_DATA, $data 
760         or return -1;
761  return $$data[0] + 256 * $$data[1];
762}
763
764# $_[0]: Reference to an opened filehandle
765# $_[1]: Command byte (usually register number)
766# $_[2]: Byte to write
767# Returns: -1 on failure, 0 on success.
768# Note: some devices use the wrong endiannes; use swap_bytes to correct for
769# this.
770sub i2c_smbus_write_word_data
771{
772  my ($file,$command,$value) = @_;
773  my $data = [$value & 0xff, $value >> 8];
774  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_WORD_DATA, $data 
775         or return -1;
776  return 0;
777}
778
779# $_[0]: Reference to an opened filehandle
780# $_[1]: Command byte (usually register number)
781# $_[2]: Word to write
782# Returns: -1 on failure, read word on success.
783# Note: some devices use the wrong endiannes; use swap_bytes to correct for
784# this.
785sub i2c_smbus_process_call
786{
787  my ($file,$command,$value) = @_;
788  my $data = [$value & 0xff, $value >> 8];
789  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_PROC_CALL, $data 
790         or return -1;
791  return $$data[0] + 256 * $$data[1];
792}
793
794# $_[0]: Reference to an opened filehandle
795# $_[1]: Command byte (usually register number)
796# Returns: Undefined on failure, a list of read bytes on success
797# Note: some devices use the wrong endiannes; use swap_bytes to correct for
798# this.
799sub i2c_smbus_read_block_data
800{
801  my ($file,$command) = @_;
802  my $data = [];
803  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_BLOCK_DATA, $data 
804         or return;
805  shift @$data;
806  return @$data;
807}
808
809# $_[0]: Reference to an opened filehandle
810# $_[1]: Command byte (usually register number)
811# @_[2..]: List of values to write
812# Returns: -1 on failure, 0 on success.
813# Note: some devices use the wrong endiannes; use swap_bytes to correct for
814# this.
815sub i2c_smbus_write_block_data
816{
817  my ($file,$command,@data) = @_;
818  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_BLOCK_DATA, \@data 
819         or return;
820  return 0;
821}
822
823####################
824# ADAPTER SCANNING #
825####################
826
827use vars qw(@chips_detected);
828
829# We will build a complicated structure @chips_detected here, being:
830# A list of
831#  references to hashes
832#    with field 'driver', being a string with the driver name for this chip;
833#    with field 'detected'
834#      being a reference to a list of
835#        references to hashes of type 'detect_data';
836#    with field 'misdetected'
837#      being a reference to a list of
838#        references to hashes of type 'detect_data'
839
840# Type detect_data:
841# A hash
842#   with field 'i2c_adap' containing an adapter string as appearing
843#        in /proc/bus/i2c (if this is an I2C detection)
844#  with field 'i2c_algo' containing an algorithm string as appearing
845#       in /proc/bus/i2c (if this is an I2C detection)
846#  with field 'i2c_devnr', contianing the /dev/i2c-* number of this
847#       adapter (if this is an I2C detection)
848#  with field 'i2c_driver', containing the driver name for this adapter
849#       (if this is an I2C detection)
850#  with field 'i2c_addr', containing the I2C address of the detection;
851#       (if this is an I2C detection)
852#  with field 'i2c_sub_addrs', containing a reference to a list of
853#       other I2C addresses (if this is an I2C detection)
854#  with field 'i2c_extra' if this is an I2C detection and the address
855#       is not normally probed by the kernel driver
856#  with field 'isa_addr' containing the ISA address this chip is on
857#       (if this is an ISA detection)
858#  with field 'isa_extra' if this is an ISA detection and the address
859#       is not normally probed by the kernel driver
860#  with field 'conf', containing the confidence level of this detection
861#  with field 'chipname', containing the chip name
862
863# This adds a detection to the above structure. We do no alias detection
864# here; so you should do ISA detections *after* all I2C detections.
865# Not all possibilities of i2c_addr and i2c_sub_addrs are exhausted.
866# In all normal cases, it should be all right.
867# $_[0]: chip driver
868# $_[1]: reference to data hash
869# Returns: Nothing
870sub add_i2c_to_chips_detected
871{
872  my ($chipdriver,$datahash) = @_;
873  my ($i,$new_detected_ref,$new_misdetected_ref,$detected_ref,$misdetected_ref,
874      $main_entry,$detected_entry,$put_in_detected,@hash_addrs,@entry_addrs);
875
876  # First determine where the hash has to be added.
877  for ($i = 0; $i < @chips_detected; $i++) {
878    last if ($chips_detected[$i]->{driver} eq $chipdriver);
879  }
880  if ($i == @chips_detected) {
881    push @chips_detected, { driver => $chipdriver,
882                            detected => [],
883                            misdetected => [] };
884  }
885  $new_detected_ref = $chips_detected[$i]->{detected};
886  $new_misdetected_ref = $chips_detected[$i]->{misdetected};
887
888  # Find out whether our new entry should go into the detected or the
889  # misdetected list. We compare all i2c addresses; if at least one matches,
890  # but our conf value is lower, we assume this is a misdetect.
891  @hash_addrs = ($datahash->{i2c_addr});
892  push @hash_addrs, @{$datahash->{i2c_sub_addrs}}
893       if exists $datahash->{i2c_sub_addrs};
894  $put_in_detected = 1;
895  FIND_LOOP:
896  foreach $main_entry (@chips_detected) {
897    foreach $detected_entry (@{$main_entry->{detected}}) {
898      @entry_addrs = ($detected_entry->{i2c_addr});
899      push @entry_addrs, @{$detected_entry->{i2c_sub_addrs}}
900               if exists $detected_entry->{i2c_sub_addrs};
901      if ($detected_entry->{i2c_devnr} == $datahash->{i2c_devnr} and
902          any_list_match \@entry_addrs, \@hash_addrs) {
903        if ($detected_entry->{conf} >= $datahash->{conf}) {
904          $put_in_detected = 0;
905        }
906        last FIND_LOOP;
907      }
908    }
909  }
910
911  if ($put_in_detected) {
912    # Here, we move all entries from detected to misdetected which
913    # match at least in one main or sub address. This may not be the
914    # best idea to do, as it may remove detections without replacing
915    # them with second-best ones. Too bad.
916    @hash_addrs = ($datahash->{i2c_addr});
917    push @hash_addrs, @{$datahash->{i2c_sub_addrs}} 
918         if exists $datahash->{i2c_sub_addrs};
919    foreach $main_entry (@chips_detected) {
920      $detected_ref = $main_entry->{detected};
921      $misdetected_ref = $main_entry->{misdetected};
922      for ($i = @$detected_ref-1; $i >=0; $i--) {
923        @entry_addrs = ($detected_ref->[$i]->{i2c_addr});
924        push @entry_addrs, @{$detected_ref->[$i]->{i2c_sub_addrs}}
925             if exists $detected_ref->[$i]->{i2c_sub_addrs};
926        if (any_list_match \@entry_addrs, \@hash_addrs) {
927          push @$misdetected_ref,$detected_ref->[$i];
928          splice @$detected_ref, $i, 1;
929        }
930      }
931    }
932
933    # Now add the new entry to detected
934    push @$new_detected_ref, $datahash;
935  } else {
936    # No hard work here
937    push @$new_misdetected_ref, $datahash;
938  }
939}
940
941# This adds a detection to the above structure. We also do alias detection
942# here; so you should do ISA detections *after* all I2C detections.
943# $_[0]: alias detection function
944# $_[1]: chip driver
945# $_[2]: reference to data hash
946# Returns: 0 if it is not an alias, datahash reference if it is.
947sub add_isa_to_chips_detected
948{
949  my ($alias_detect,$chipdriver,$datahash) = @_;
950  my ($i,$new_detected_ref,$new_misdetected_ref,$detected_ref,$misdetected_ref,
951      $main_entry,$isalias);
952
953  # First determine where the hash has to be added.
954  $isalias=0;
955  for ($i = 0; $i < @chips_detected; $i++) {
956    last if ($chips_detected[$i]->{driver} eq $chipdriver);
957  }
958  if ($i == @chips_detected) {
959    push @chips_detected, { driver => $chipdriver,
960                            detected => [],
961                            misdetected => [] };
962  }
963  $new_detected_ref = $chips_detected[$i]->{detected};
964  $new_misdetected_ref = $chips_detected[$i]->{misdetected};
965
966  # Now, we are looking for aliases. An alias can only be the same chiptype.
967  # If an alias is found in the misdetected list, we add the new information
968  # and terminate this function. If it is found in the detected list, we
969  # still have to check whether another chip has claimed this ISA address.
970  # So we remove the old entry from the detected list and put it in datahash.
971
972  # Misdetected alias detection:
973  for ($i = 0; $i < @$new_misdetected_ref; $i++) {
974    if (exists $new_misdetected_ref->[$i]->{i2c_addr} and
975        not exists $new_misdetected_ref->[$i]->{isa_addr} and
976        defined $alias_detect and
977        $new_misdetected_ref->[$i]->{chipname} eq $datahash->{chipname}) {
978      open FILE,"/dev/i2c-$new_misdetected_ref->[$i]->{i2c_devnr}" or
979           print("Can't open ",
980                 "/dev/i2c-$new_misdetected_ref->[$i]->{i2c_devnr}?!?\n"),
981           next;
982      i2c_set_slave_addr \*FILE,$new_misdetected_ref->[$i]->{i2c_addr} or
983           print("Can't set I2C address for ",
984                 "/dev/i2c-$new_misdetected_ref->[$i]->{i2c_devnr}?!?\n"),
985           next;
986      if (&$alias_detect ($datahash->{isa_addr},\*FILE,
987                          $new_misdetected_ref->[$i]->{i2c_addr})) {
988        $new_misdetected_ref->[$i]->{isa_addr} = $datahash->{isa_addr};
989        $new_misdetected_ref->[$i]->{isa_extra} = $datahash->{isa_extra} 
990               if exists $datahash->{isa_extra};
991        close FILE;
992        return $new_misdetected_ref->[$i]; 
993      }
994      close FILE;
995    }
996  }
997
998  # Detected alias detection:
999  for ($i = 0; $i < @$new_detected_ref; $i++) {
1000    if (exists $new_detected_ref->[$i]->{i2c_addr} and
1001        not exists $new_detected_ref->[$i]->{isa_addr} and
1002        defined $alias_detect and
1003        $new_detected_ref->[$i]->{chipname} eq $datahash->{chipname}) {
1004      open FILE,"/dev/i2c-$new_detected_ref->[$i]->{i2c_devnr}" or
1005           print("Can't open ",
1006                 "/dev/i2c-$new_detected_ref->[$i]->{i2c_devnr}?!?\n"),
1007           next;
1008      i2c_set_slave_addr \*FILE,$new_detected_ref->[$i]->{i2c_addr} or
1009           print("Can't set I2C address for ",
1010                 "/dev/i2c-$new_detected_ref->[$i]->{i2c_devnr}?!?\n"),
1011           next;
1012      if (&$alias_detect ($datahash->{isa_addr},\*FILE,
1013                          $new_detected_ref->[$i]->{i2c_addr})) {
1014        $new_detected_ref->[$i]->{isa_addr} = $datahash->{isa_addr};
1015        $new_detected_ref->[$i]->{isa_extra} = $datahash->{isa_extra} 
1016               if exists $datahash->{isa_extra};
1017        ($datahash) = splice (@$new_detected_ref, $i, 1);
1018        close FILE;
1019        $isalias=1;
1020        last;
1021      }
1022      close FILE;
1023    }
1024  }
1025
1026
1027  # Find out whether our new entry should go into the detected or the
1028  # misdetected list. We only compare main isa_addr here, of course.
1029  foreach $main_entry (@chips_detected) {
1030    $detected_ref = $main_entry->{detected};
1031    $misdetected_ref = $main_entry->{misdetected};
1032    for ($i = 0; $i < @{$main_entry->{detected}}; $i++) {
1033      if (exists $detected_ref->[$i]->{isa_addr} and
1034          $detected_ref->[$i]->{isa_addr} == $datahash->{isa_addr}) {
1035        if ($detected_ref->[$i]->{conf} >= $datahash->{conf}) {
1036          push @$new_misdetected_ref, $datahash;
1037        } else {
1038          push @$misdetected_ref,$detected_ref->[$i];
1039          splice @$detected_ref, $i,1;
1040          push @$new_detected_ref, $datahash;
1041        }
1042        if ($isalias) {
1043          return $datahash;
1044        } else {
1045          return 0;
1046        }
1047      }
1048    }
1049  }
1050
1051  # Not found? OK, put it in the detected list
1052  push @$new_detected_ref, $datahash;
1053  if ($isalias) {
1054    return $datahash;
1055  } else {
1056    return 0;
1057  }
1058}
1059
1060# $_[0]: The number of the adapter to scan
1061# $_[1]: The name of the adapter, as appearing in /proc/bus/i2c
1062# $_[2]: The name of the algorithm, as appearing in /proc/bus/i2c
1063# $_[3]: The driver of the adapter
1064# @_[4..]: Addresses not to scan
1065sub scan_adapter
1066{
1067  my ( $adapter_nr,$adapter_name,$algorithm_name,$adapter_driver, 
1068       $not_to_scan) = @_;
1069  my ($chip, $addr, $conf,@chips,$new_hash,$other_addr);
1070
1071  # As we modify it, we need a copy
1072  my @not_to_scan = @$not_to_scan;
1073
1074  open FILE,"/dev/i2c-$adapter_nr" or 
1075       (print "Can't open /dev/i2c-$adapter_nr ($!)\n"), return;
1076
1077  # Now scan each address in turn
1078  foreach $addr (0..0x7f) {
1079    # As the not_to_scan list is sorted, we can check it fast
1080    if (@not_to_scan and $not_to_scan[0] == $addr) {
1081      shift @not_to_scan;
1082      next;
1083    }
1084
1085    i2c_set_slave_addr(\*FILE,$addr) or 
1086        printf("Client at address 0x%02x can not be probed - unload all client drivers first!\n",$addr), next;
1087
1088    next unless i2c_smbus_write_quick(\*FILE,$SMBUS_WRITE) >= 0;
1089    printf "Client found at address 0x%02x\n",$addr;
1090
1091    foreach $chip (@chip_ids) {
1092      if (exists $$chip{i2c_addrs} and contains $addr, @{$$chip{i2c_addrs}}) {
1093        print "Probing for `$$chip{name}'... ";
1094        if (($conf,@chips) = &{$$chip{i2c_detect}} (\*FILE ,$addr)) {
1095          print "Success!\n",
1096                "    (confidence $conf, driver `$$chip{driver}'";
1097          if (@chips) {
1098            print ", other addresses:";
1099            @chips = sort @chips;
1100            foreach $other_addr (sort @chips) {
1101              printf(" 0x%02x",$other_addr);
1102            }
1103          }
1104          printf "\n";
1105          $new_hash = { conf => $conf,
1106                        i2c_addr => $addr,
1107                        chipname =>  $$chip{name},
1108                        i2c_adap => $adapter_name,
1109                        i2c_algo => $algorithm_name,
1110                        i2c_driver => $adapter_driver,
1111                        i2c_devnr => $adapter_nr,
1112                      };
1113          if (@chips) {
1114            my @chips_copy = @chips;
1115            $new_hash->{i2c_sub_addrs} = \@chips_copy;
1116          }
1117          $new_hash->{i2c_extra} = 0 
1118                 if exists $chip->{i2c_driver_addrs} and
1119                    not contains( $addr , @{$chip->{i2c_driver_addrs}});
1120          add_i2c_to_chips_detected $$chip{driver}, $new_hash;
1121        } else {
1122          print "Failed!\n";
1123        }
1124      }
1125    }
1126  }
1127}
1128
1129sub scan_isa_bus
1130{
1131  my ($chip,$addr,$conf);
1132  foreach $chip (@chip_ids) {
1133    next if not exists $$chip{isa_addrs} or not exists $$chip{isa_detect};
1134    print "Probing for `$$chip{name}'\n";
1135    foreach $addr (@{$$chip{isa_addrs}}) {
1136      if ($addr) {
1137        printf "  Trying address 0x%04x... ", $addr;
1138      } else {
1139        print "  Trying general detect... ";
1140      }
1141      $conf = &{$$chip{isa_detect}} ($addr);
1142      print("Failed!\n"), next if not defined $conf;
1143      print "Success!\n";
1144      printf "    (confidence %d, driver `%s')\n", $conf, $$chip{driver};
1145      my $new_hash = { conf => $conf,
1146                       isa_addr => $addr,
1147                       chipname =>  $$chip{name}
1148                     };
1149      $new_hash->{isa_extra} = 0 
1150             if exists $chip->{isa_driver_addrs} and
1151                not contains ($addr, @{$chip->{isa_driver_addrs}});
1152      $new_hash = add_isa_to_chips_detected $$chip{alias_detect},$$chip{driver},
1153                                            $new_hash;
1154      if ($new_hash) {
1155        printf "    Alias of the chip on I2C bus `%s', address 0x%04x\n",
1156                        $new_hash->{i2c_adap},$new_hash->{i2c_addr};
1157      }
1158    }
1159  }
1160}
1161
1162
1163##################
1164# CHIP DETECTION #
1165##################
1166
1167# Each function returns a confidence value. The higher this value, the more
1168# sure we are about this chip. A Winbond W83781D, for example, will be
1169# detected as a LM78 too; but as the Winbond detection has a higher confidence
1170# factor, you should identify it as a Winbond.
1171
1172# Each function returns a list. The first element is the confidence value;
1173# Each element after it is an SMBus address. In this way, we can detect
1174# chips with several SMBus addresses. The SMBus address for which the
1175# function was called is never returned.
1176
1177# If there are devices which get confused if they are only read from, then
1178# this program will surely confuse them. But we guarantee never to write to
1179# any of these devices.
1180
1181
1182# $_[0]: Chip to detect (0 = LM78, 1 = LM78-J, 2 = LM79)
1183# $_[1]: A reference to the file descriptor to access this chip.
1184#        We may assume an i2c_set_slave_addr was already done.
1185# $_[2]: Address
1186# Returns: undef if not detected, (7) if detected.
1187# Registers used:
1188#   0x40: Configuration
1189#   0x48: Full I2C Address
1190#   0x49: Device ID
1191# Note that this function is always called through a closure, so the
1192# arguments are shifted by one place.
1193sub lm78_detect
1194{
1195  my $reg;
1196  my ($chip,$file,$addr) = @_;
1197  return unless i2c_smbus_read_byte_data($file,0x48) == $addr;
1198  return unless (i2c_smbus_read_byte_data($file,0x40) & 0x80) == 0x00;
1199  $reg = i2c_smbus_read_byte_data($file,0x49);
1200  return unless ($chip == 0 and $reg == 0x00) or
1201                    ($chip == 1 and $reg == 0x40) or
1202                    ($chip == 2 and ($reg & 0xfe) == 0xc0);
1203  return (7);
1204}
1205
1206# $_[0]: Chip to detect (0 = LM78, 1 = LM78-J, 2 = LM79)
1207# $_[1]: Address
1208# Returns: undef if not detected, 7 if detected.
1209# Note: Only address 0x290 is scanned at this moment.
1210sub lm78_isa_detect
1211{
1212  my ($chip,$addr) = @_ ;
1213  my $val = inb ($addr + 1);
1214  return if inb ($addr + 2) != $val or inb ($addr + 3) != $val or 
1215            inb ($addr + 7) != $val;
1216
1217  $val = inb($addr + 5) & 0x7f;
1218  outb($addr+5,~ $val);
1219  if ((inb ($addr+5) & 0x7f) != (~ $val & 0x7f)) {
1220    outb($addr+5,$val);
1221    return;
1222  }
1223  my $readproc = sub { isa_read_byte $addr + 5, $addr + 6, @_ };
1224  return unless (&$readproc(0x40) & 0x80) == 0x00;
1225  my $reg = &$readproc(0x49);
1226  return unless ($chip == 0 and $reg == 0x00) or
1227                ($chip == 1 and $reg == 0x40) or
1228                ($chip == 2 and ($reg & 0xfe) == 0xc0);
1229  return 7;
1230}
1231
1232
1233# $_[0]: Chip to detect (0 = LM78, 1 = LM78-J, 2 = LM79)
1234# $_[1]: ISA address
1235# $_[2]: I2C file handle
1236# $_[3]: I2C address
1237sub lm78_alias_detect
1238{
1239  my ($chip,$isa_addr,$file,$i2c_addr) = @_;
1240  my $i;
1241  my $readproc = sub { isa_read_byte $isa_addr + 5, $isa_addr + 6, @_ };
1242  return 0 unless &$readproc(0x48) == $i2c_addr;
1243  for ($i = 0x2b; $i <= 0x3d; $i ++) {
1244    return 0 unless &$readproc($i) == i2c_smbus_read_byte_data($file,$i);
1245  }
1246  return 1;
1247}
1248
1249# $_[0]: A reference to the file descriptor to access this chip.
1250#        We may assume an i2c_set_slave_addr was already done.
1251# $_[1]: Address
1252# Returns: undef if not detected, (3) if detected.
1253# Registers used:
1254#   0x01: Configuration
1255#   0x02: Hysteresis
1256#   0x03: Overtemperature Shutdown
1257# Detection really sucks! It is only based on the fact that the LM75 has only
1258# four registers. Any other chip in the valid address range with only four
1259# registers will be detected too.
1260# Note that register $00 may change, so we can't use the modulo trick on it.
1261sub lm75_detect
1262{
1263  my $i;
1264  my ($file,$addr) = @_;
1265  my $cur = i2c_smbus_read_word_data($file,0x00);
1266  my $conf = i2c_smbus_read_byte_data($file,0x01);
1267  my $hyst = i2c_smbus_read_word_data($file,0x02);
1268  my $os = i2c_smbus_read_word_data($file,0x03);
1269  for ($i = 0x00; $i <= 0x1f; $i += 1) {
1270    return if i2c_smbus_read_byte_data($file,($i * 0x08) + 0x01) != $conf;
1271    return if i2c_smbus_read_word_data($file,($i * 0x08) + 0x02) != $hyst;
1272    return if i2c_smbus_read_word_data($file,($i * 0x08) + 0x03) != $os;
1273  }
1274  return (3);
1275}
1276 
1277
1278# $_[0]: A reference to the file descriptor to access this chip.
1279#        We may assume an i2c_set_slave_addr was already done.
1280# $_[1]: Address
1281# Returns: undef if not detected, (3) if detected.
1282# Registers used:
1283# Registers used:
1284#   0x02: Interrupt state register
1285# How to detect this beast?
1286sub lm80_detect
1287{
1288  my $i;
1289  my ($file,$addr) = @_;
1290  return if (i2c_smbus_read_byte_data($file,$0x02) & 0xc0) != 0;
1291  for ($i = 0x2a; $i <= 0x3d; $i++) {
1292    my $reg = i2c_smbus_read_byte_data($file,$i);
1293    return if i2c_smbus_read_byte_data($file,$i+0x40) != $reg;
1294    return if i2c_smbus_read_byte_data($file,$i+0x80) != $reg;
1295    return if i2c_smbus_read_byte_data($file,$i+0xc0) != $reg;
1296  }
1297  return (3);
1298}
1299 
1300# $_[0]: Chip to detect (0 = W83781D, 1 = W83782D, 2 = W83783S,
1301#                        3 = W83627HF, 4 = AS99127F)
1302# $_[1]: A reference to the file descriptor to access this chip.
1303#        We may assume an i2c_set_slave_addr was already done.
1304# $_[2]: Address
1305# Returns: undef if not detected, (8,addr1,addr2) if detected, but only
1306#          if the LM75 chip emulation is enabled.
1307# Registers used:
1308#   0x48: Full I2C Address
1309#   0x4a: I2C addresses of emulated LM75 chips
1310#   0x4e: Vendor ID byte selection, and bank selection
1311#   0x4f: Vendor ID
1312#   0x58: Device ID (only when in bank 0); ignore LSB.
1313# Note: Fails if the W8378xD is not in bank 0!
1314# Note: Detection overrules a previous LM78 detection
1315# Note: AS99127F address register 0x48 not supported?
1316sub w83781d_detect
1317{
1318  my ($reg1,$reg2,@res);
1319  my ($chip,$file,$addr) = @_;
1320  return unless (i2c_smbus_read_byte_data($file,0x48) == $addr)
1321    or ($chip == 4);
1322  $reg1 = i2c_smbus_read_byte_data($file,0x4e);
1323  $reg2 = i2c_smbus_read_byte_data($file,0x4f);
1324  if ($chip < 4) {
1325    return unless (($reg1 & 0x80) == 0x00 and $reg2 == 0xa3) or 
1326                  (($reg1 & 0x80) == 0x80 and $reg2 == 0x5c);
1327  }
1328  if ($chip == 4) {
1329    return unless (($reg1 & 0x80) == 0x00 and $reg2 == 0xc3) or 
1330                  (($reg1 & 0x80) == 0x80 and $reg2 == 0x12);
1331  }
1332  return unless ($reg1 & 0x07) == 0x00;
1333  $reg1 = i2c_smbus_read_byte_data($file,0x58) & 0xfe;
1334  return if $chip == 0 and  $reg1 != 0x10;
1335  return if $chip == 1 and  $reg1 != 0x30;
1336  return if $chip == 2 and  $reg1 != 0x40;
1337  return if $chip == 3 and  $reg1 != 0x20;
1338  return if $chip == 4 and  $reg1 != 0x30;
1339  $reg1 = i2c_smbus_read_byte_data($file,0x4a);
1340  @res = (8);
1341  push @res, ($reg1 & 0x07) + 0x48 unless $reg1 & 0x08;
1342  push @res, (($reg1 & 0x80) >> 4) + 0x48 unless $reg1 & 0x80;
1343  return @res;
1344}
1345
1346# $_[0]: Chip to detect (0 = W83781D, 1 = W83782D, 2 = W83783S, 3 = W83627HF)
1347# $_[1]: ISA address
1348# $_[2]: I2C file handle
1349# $_[3]: I2C address
1350sub w83781d_alias_detect
1351{
1352  my ($chip,$isa_addr,$file,$i2c_addr) = @_;
1353  my $i;
1354  my $readproc = sub { isa_read_byte $isa_addr + 5, $isa_addr + 6, @_ };
1355  return 0 unless &$readproc(0x48) == $i2c_addr;
1356  for ($i = 0x2b; $i <= 0x3d; $i ++) {
1357    return 0 unless &$readproc($i) == i2c_smbus_read_byte_data($file,$i);
1358  }
1359  return 1;
1360}
1361
1362# $_[0]: Chip to detect (0 = W83781D, 1 = W83782D, 3 = W83627HF)
1363#        W83783S not on ISA bus.
1364# $_[1]: Address
1365# Returns: undef if not detected, (8) if detected.
1366sub w83781d_isa_detect
1367{
1368  my ($chip,$addr) = @_ ;
1369  my ($reg1,$reg2);
1370  my $val = inb ($addr + 1);
1371  return if inb ($addr + 2) != $val or inb ($addr + 3) != $val or
1372            inb ($addr + 7) != $val;
1373
1374  $val = inb($addr + 5) & 0x7f;
1375  outb($addr+5,~ $val);
1376  if ((inb ($addr+5) & 0x7f) != (~ $val & 0x7f)) {
1377    outb($addr+5,$val);
1378    return;
1379  }
1380
1381  my $read_proc = sub { isa_read_byte $addr + 5, $addr + 6, @_ };
1382  $reg1 = &$read_proc(0x4e);
1383  $reg2 = &$read_proc(0x4f);
1384  return unless (($reg1 & 0x80) == 0x00 and $reg2 == 0xa3) or 
1385                (($reg1 & 0x80) == 0x80 and $reg2 == 0x5c);
1386  return unless ($reg1 & 0x07) == 0x00;
1387  $reg1 = &$read_proc(0x58) & 0xfe;
1388  return if $chip == 0 and  $reg1 != 0x10;
1389  return if $chip == 1 and  $reg1 != 0x30;
1390  return if $chip == 3 and  $reg1 != 0x20;
1391  return 8;
1392}
1393
1394# $_[0]: Chip to detect (0 = Revision 0x00, 1 = Revision 0x80)
1395# $_[1]: A reference to the file descriptor to access this chip.
1396#        We may assume an i2c_set_slave_addr was already done.
1397# $_[2]: Address
1398# Returns: undef if not detected, (6) if detected.
1399# Registers used:
1400#   0x00: Device ID
1401#   0x01: Revision ID
1402#   0x03: Configuration
1403# Mediocre detection
1404sub gl518sm_detect
1405{
1406  my $reg;
1407  my ($chip,$file,$addr) = @_;
1408  return unless i2c_smbus_read_byte_data($file,0x00) == 0x80;
1409  return unless (i2c_smbus_read_byte_data($file,0x03) & 0x80) == 0x00;
1410  $reg = i2c_smbus_read_byte_data($file,0x01);
1411  return unless ($chip == 0 and $reg == 0x00) or
1412                ($chip == 1 and $reg == 0x80);
1413  return (6);
1414}
1415
1416# $_[0]: A reference to the file descriptor to access this chip.
1417#        We may assume an i2c_set_slave_addr was already done.
1418# $_[1]: Address
1419# Returns: undef if not detected, (5) if detected.
1420# Registers used:
1421#   0x00: Device ID
1422#   0x01: Revision ID
1423#   0x03: Configuration
1424# Mediocre detection
1425sub gl520sm_detect
1426{
1427  my ($file,$addr) = @_;
1428  return unless i2c_smbus_read_byte_data($file,0x00) == 0x20;
1429  return unless (i2c_smbus_read_byte_data($file,0x03) & 0x80) == 0x00;
1430  # The line below must be better checked before I dare to use it.
1431  # return unless i2c_smbus_read_byte_data($file,0x01) == 0x00;
1432  return (5);
1433}
1434
1435# $_[0]: A reference to the file descriptor to access this chip.
1436#        We may assume an i2c_set_slave_addr was already done.
1437# $_[1]: Address
1438# Returns: undef if not detected, (5) if detected.
1439# Registers used:
1440#   0x00: Device ID
1441# Mediocre detection
1442sub gl525sm_detect
1443{
1444  my ($file,$addr) = @_;
1445  return unless i2c_smbus_read_byte_data($file,0x00) == 0x25;
1446  return (5);
1447}
1448
1449# $_[0]: Chip to detect (0 = ADM9240, 1 = DS1780, 2 = LM81)
1450# $_[1]: A reference to the file descriptor to access this chip.
1451#        We may assume an i2c_set_slave_addr was already done.
1452# $_[2]: Address
1453# Returns: undef if not detected, (7) if detected.
1454# Registers used:
1455#   0x3e: Company ID
1456#   0x40: Configuration
1457#   0x48: Full I2C Address
1458# Note: Detection overrules a previous LM78 detection
1459sub adm9240_detect
1460{
1461  my $reg;
1462  my ($chip, $file,$addr) = @_;
1463  $reg = i2c_smbus_read_byte_data($file,0x3e);
1464  return unless ($chip == 0 and $reg == 0x23) or
1465                ($chip == 1 and $reg == 0xda) or
1466                ($chip == 2 and $reg == 0x01);
1467  return unless (i2c_smbus_read_byte_data($file,0x40) & 0x80) == 0x00;
1468  return unless i2c_smbus_read_byte_data($file,0x48) == $addr;
1469 
1470  return (7);
1471}
1472
1473# $_[0]: Chip to detect (0 = ADM1022, 1 = THMC50)
1474# $_[1]: A reference to the file descriptor to access this chip.
1475#        We may assume an i2c_set_slave_addr was already done.
1476# $_[2]: Address
1477# Returns: undef if not detected, (8) if detected.
1478# Registers used:
1479#   0x3e: Company ID
1480#   0x3f: Revision
1481#   0x40: Configuration
1482# Note: Detection overrules a previous LM78 or ADM9240 detection
1483sub adm1022_detect
1484{
1485  my $reg;
1486  my ($chip, $file,$addr) = @_;
1487  $reg = i2c_smbus_read_byte_data($file,0x3e);
1488  return unless ($chip == 0 and $reg == 0x41) or
1489                ($chip == 1 and $reg == 0x49);
1490  return unless (i2c_smbus_read_byte_data($file,0x40) & 0x80) == 0x00;
1491  return unless (i2c_smbus_read_byte_data($file,0x3f) & 0xc0) == 0xc0;
1492 
1493  return (8);
1494}
1495
1496# $_[0]: Chip to detect
1497#   (0 = ADM1021, 1 = MAX1617, 2 = MAX1617A, 3 = THMC10, 4 = LM84, 5 = GL523)
1498# $_[1]: A reference to the file descriptor to access this chip.
1499#        We may assume an i2c_set_slave_addr was already done.
1500# $_[2]: Address
1501# Returns: undef if not detected, (6) or (3) if detected.
1502# Registers used:
1503#   0x04: Company ID (LM84 only)
1504#   0xfe: Company ID
1505#   0xff: Revision (Maxim only)
1506#   0x02: Status
1507# Note: Especially the Maxim has very bad detection; we give it a low
1508# confidence value.
1509sub adm1021_detect
1510{
1511  my $reg;
1512  my ($chip, $file,$addr) = @_;
1513  return if $chip == 0 and i2c_smbus_read_byte_data($file,0xfe) != 0x41;
1514  return if $chip == 3 and i2c_smbus_read_byte_data($file,0xfe) != 0x49;
1515  return if $chip == 4 and i2c_smbus_read_byte_data($file,0x04) != 0x00;
1516  return if $chip == 5 and i2c_smbus_read_byte_data($file,0xfe) != 0x23;
1517  return if $chip == 2 and i2c_smbus_read_byte_data($file,0xfe) != 0x4d and
1518                           i2c_smbus_read_byte_data($file,0xff) != 0x01;
1519  # The remaining things are flaky at best. Perhaps something can be done
1520  # with the fact that some registers are unreadable?
1521  return if (i2c_smbus_read_byte_data($file,0x02) & 0x03) != 0;
1522  if ($chip == 1) {
1523    return (3);
1524  } else {
1525    return (6);
1526  }
1527}
1528
1529# $_[0]: Address
1530# Returns: undef if not detected, (9) if detected.
1531# Note: It is already 99% certain this chip exists if we find the PCI
1532# entry. The exact address is encoded in PCI space.
1533sub sis5595_isa_detect
1534{
1535  my ($addr) = @_;
1536  my ($adapter,$try,$local_try);
1537  my $found = 0;
1538  foreach $local_try (@pci_adapters) {
1539    if ($local_try->{procid} eq "Silicon Integrated Systems 85C503") {
1540      $try = $local_try;
1541      $found = 1;
1542      last;
1543    }
1544  }
1545  return if not $found;
1546
1547  $found = 0;
1548  foreach $adapter (@pci_list) {
1549    if ((defined($adapter->{vendid}) and 
1550         $try->{vendid} == $adapter->{vendid} and
1551         $try->{devid} == $adapter->{devid} and
1552         $try->{func} == $adapter->{func}) or
1553        (! defined($adapter->{vendid}) and
1554         $adapter->{desc} =~ /$try->{procid}/ and
1555         $try->{func} == $adapter->{func})) {
1556      $found = 1;
1557      last;
1558    }
1559  }
1560  return if not $found;
1561
1562  return 9;
1563}
1564
1565# $_[0]: A reference to the file descriptor to access this chip.
1566#        We may assume an i2c_set_slave_addr was already done.
1567# $_[1]: Address
1568# Returns: undef if not detected, (5) if detected.
1569# Registers used:
1570#   0x00-0x63: PC-100 Data and Checksum
1571sub eeprom_detect
1572{
1573  my ($file,$addr) = @_;
1574  # Check the checksum for validity (only works for PC-100 DIMMs)
1575  my $checksum = 0;
1576  for (my $i = 0; $i <= 62; $i = $i + 1) {
1577    $checksum = $checksum + i2c_smbus_read_byte_data($file,$i);
1578  }
1579  $checksum=$checksum & 255;
1580  if (i2c_smbus_read_byte_data($file,63) == $checksum) {
1581        return (8);
1582  }
1583  # Even if checksum test fails, it still may be an eeprom
1584  return (1);
1585}
1586
1587# $_[0]: A reference to the file descriptor to access this chip.
1588#        We may assume an i2c_set_slave_addr was already done.
1589# $_[1]: Address
1590# Returns: undef if not detected, (1) if detected.
1591# Detection is impossible!
1592sub ltc1710_detect
1593{
1594  return (1);
1595}
1596 
1597
1598################
1599# MAIN PROGRAM #
1600################
1601
1602# $_[0]: reference to a list of chip hashes
1603sub print_chips_report 
1604{
1605  my ($listref) = @_;
1606  my $data;
1607 
1608  foreach $data (@$listref) {
1609    my $is_i2c = exists $data->{i2c_addr};
1610    my $is_isa = exists $data->{isa_addr};
1611    print "  * ";
1612    if ($is_i2c) {
1613      printf "Bus `%s' (%s)\n", $data->{i2c_adap}, $data->{i2c_algo};
1614      printf "    Busdriver `%s', I2C address 0x%02x", 
1615             $data->{i2c_driver}, $data->{i2c_addr};
1616      if (exists $data->{i2c_sub_addrs}) {
1617        print " (and";
1618        my $sub_addr;
1619        foreach $sub_addr (@{$data->{i2c_sub_addrs}}) {
1620          printf " 0x%02x",$sub_addr;
1621        }
1622        print ")"
1623      }
1624      print "\n";
1625    }
1626    if ($is_isa) {
1627      print "    " if  $is_i2c;
1628      if ($data->{isa_addr}) {
1629        printf "ISA bus address 0x%04x (Busdriver `i2c-isa')\n", 
1630               $data->{isa_addr};
1631      } else {
1632        printf "ISA bus, undetermined address (Busdriver `i2c-isa')\n"
1633      }
1634    }
1635    printf "    Chip `%s' (confidence: %d)\n",
1636           $data->{chipname},  $data->{conf};
1637  }
1638}
1639
1640# $_[0]: 1 if ISA bus is prefered, 0 for SMBus
1641sub generate_modprobes
1642{
1643  my ($prefer_isa) = @_;
1644
1645  my ($chip,$detection,%adapters,$nr,$i,@optionlist,@probelist);
1646  my $modprobes = "";
1647  my $configfile = "";
1648
1649  # These are always needed
1650  $configfile .= "# I2C module options\n";
1651  $configfile .= "alias char-major-89 i2c-dev\n";
1652
1653  # Collect all adapters
1654  $nr = 0;
1655  $modprobes .= "# I2C adapter drivers\n";
1656  foreach $chip (@chips_detected) {
1657    foreach $detection (@{$chip->{detected}}) {
1658      %adapters->{$detection->{i2c_driver}} = $nr ++
1659            if exists $detection->{i2c_driver} and 
1660               not exists %adapters->{$detection->{i2c_driver}} and
1661               not (exists $detection->{isa_addr} and $prefer_isa);
1662      %adapters->{"i2c-isa"} = $nr ++ 
1663            if exists $detection->{isa_addr} and 
1664               not exists %adapters->{"i2c-isa"} and
1665               not (exists $detection->{i2c_driver} and not $prefer_isa);
1666    }
1667  }
1668  for ($i = 0; $i < $nr; $i ++) {
1669    foreach $detection (keys %adapters) {
1670      $modprobes .= "modprobe $detection\n", last 
1671                 if $adapters{$detection} == $i;
1672    }
1673  }
1674
1675  # Now determine the chip probe lines
1676  $modprobes .= "# I2C chip drivers\n";
1677  foreach $chip (@chips_detected) {
1678    next if not @{$chip->{detected}};
1679    $modprobes .= "modprobe $chip->{driver}\n";
1680    @optionlist = ();
1681    @probelist = ();
1682
1683    # Handle detects out-of-range
1684    foreach $detection (@{$chip->{detected}}) {
1685      push @probelist, %adapters->{$detection->{i2c_driver}},
1686                       $detection->{i2c_addr}
1687           if exists $detection->{i2c_driver} and
1688              exists %adapters->{$detection->{i2c_driver}} and
1689              exists $detection->{i2c_extra};
1690      push @probelist, -1, $detection->{isa_addr}
1691           if exists $detection->{isa_addr} and
1692              exists %adapters->{"i2c-isa"} and
1693              exists $detection->{isa_extra};
1694    }
1695
1696    # Handle misdetects
1697    foreach $detection (@{$chip->{misdetected}}) {
1698      push @optionlist, %adapters->{$detection->{i2c_driver}},
1699                       $detection->{i2c_addr}
1700           if exists $detection->{i2c_driver} and
1701              exists %adapters->{$detection->{i2c_driver}};
1702      push @optionlist, -1, $detection->{isa_addr}
1703           if exists $detection->{isa_addr} and
1704              exists %adapters->{"i2c-isa"};
1705    }
1706
1707    # Handle aliases
1708    foreach $detection (@{$chip->{detected}}) {
1709      if (exists $detection->{i2c_driver} and 
1710          exists $detection->{isa_addr} and
1711          exists %adapters->{$detection->{i2c_driver}} and
1712          exists %adapters->{"i2c-isa"}) {
1713        if ($prefer_isa) {
1714          push @optionlist,%adapters->{$detection->{i2c_driver}},
1715                           $detection->{i2c_addr};
1716        } else {
1717          push @optionlist, -1, $detection->{isa_addr}
1718        }
1719      }
1720    }
1721
1722    next if not (@probelist or @optionlist);
1723    $configfile .= "options $chip->{driver}";
1724    $configfile .= sprintf " ignore=%d,0x%02x",shift @optionlist, 
1725                                               shift @optionlist
1726                  if @optionlist;
1727    $configfile .= sprintf ",%d,0x%02x",shift @optionlist, shift @optionlist
1728                  while @optionlist;
1729    $configfile .= sprintf " probe=%d,0x%02x",shift @probelist,
1730                                              shift @probelist
1731                  if @probelist;
1732    $configfile .= sprintf ",%d,0x%02x",shift @probelist, shift @probelist
1733                  while @probelist;
1734    $configfile .= "\n";
1735  }
1736
1737  return ($modprobes,$configfile);
1738 
1739}
1740
1741sub main
1742{
1743  my (@adapters,$res,$did_adapter_detection,$detect_others,$adapter);
1744
1745  initialize_proc_pci;
1746  initialize_modules_list;
1747
1748  print " This program will help you to determine which I2C/SMBus modules you ",
1749        "need to\n",
1750        " load to use lm_sensors most effectively.\n";
1751  print " You need to have done a `make install', issued a `depmod -a' and ",
1752        "made sure\n",
1753        " `/etc/conf.modules' (or `/etc/modules.conf') contains the ",
1754        "appropriate\n",
1755        " module path before you can use some functions of this utility. ",
1756        "Read\n",
1757        " doc/modules for more information.\n";
1758  print " Also, you need to be `root', or at least have access to the ",
1759        "/dev/i2c-* files\n",
1760        " for some things. You can use prog/mkdev/mkdev.sh to create these ",
1761        "/dev files\n",
1762        " if you do not have them already.\n";
1763  print " If you have patched your kernel and have some drivers built-in ",
1764        "you can\n",
1765        " safely answer NO if asked to load some modules. In this case, ",
1766        "things may\n",
1767        " seem a bit confusing, but they will still work.\n\n";
1768
1769  print " We can start with probing for (PCI) I2C or SMBus adapters.\n";
1770  print " You do not need any special privileges for this.\n";
1771  print " Do you want to probe now? (YES/no): ";
1772  @adapters = adapter_pci_detection
1773                        if ($did_adapter_detection = not <STDIN> =~ /\s*[Nn]/);
1774
1775  print "\n";
1776
1777  if (not $did_adapter_detection) {
1778    print " As you skipped adapter detection, we will only scan already ",
1779          "loaded adapter\n",
1780          " modules. You can still be prompted for non-detectable adapters.\n",
1781          " Do you want to? (yes/NO): ";
1782    $detect_others = <STDIN> =~ /^\s*[Yy]/;
1783  } elsif ($> != 0) {
1784    print " As you are not root, we can't load adapter modules. We will only ",
1785          "scan\n",
1786          " already loaded adapters.\n";
1787    $detect_others = 0;
1788  } else {
1789    print " We will now try to load each adapter module in turn.\n";
1790    foreach $adapter (@adapters) {
1791      if (contains $adapter, @modules_list) {
1792        print "Module `$adapter' already loaded.\n";
1793      } else {
1794        print "Load `$adapter' (say NO if built into your kernel)? (YES/no): ";
1795        unless (<STDIN> =~ /^\s*[Nn]/) {
1796          if (system ("modprobe", $adapter)) {
1797            print "Loading failed ($!)... skipping.\n";
1798          } else {
1799            print "Module loaded succesfully.\n";
1800          }
1801        }
1802      }
1803    }
1804    print " Do you now want to be prompted for non-detectable adapters? ",
1805          "(yes/NO): ";
1806    $detect_others = <STDIN> =~ /^\s*[Yy]/ ;
1807  }
1808
1809  if ($detect_others) {
1810    foreach $adapter (@undetectable_adapters) {
1811      print "Load `$adapter' (say NO if built into your kernel)? (YES/no): ";
1812      unless (<STDIN> =~ /^\s*[Nn]/) {
1813        if (system ("modprobe", $adapter)) {
1814          print "Loading failed ($!)... skipping.\n";
1815        } else {
1816          print "Module loaded succesfully.\n";
1817        }
1818      }
1819    }
1820  }
1821
1822  print " To continue, we need module `i2c-dev' to be loaded.\n";
1823  print " If it is built-in into your kernel, you can safely skip this.\n";
1824  if (contains "i2c-dev", @modules_list) {
1825    print "i2c-dev is already loaded.\n";
1826  } else {
1827    if ($> != 0) {
1828      print " i2c-dev is not loaded. As you are not root, we will just hope ",
1829            "you edited\n",
1830            " `/etc/conf.modules' (or `/etc/modules.conf') for automatic ",
1831            "loading of\n",
1832            " this module. If not, you won't be able to open any /dev/i2c-* ",
1833            "file.\n";
1834    } else {
1835      print " i2c-dev is not loaded. Do you want to load it now? (YES/no): ";
1836      if (<STDIN> =~ /^\s*[Nn]/) {
1837        print " Well, you will know best. We will just hope you edited ",
1838              "`/etc/conf.modules'\n",
1839              " (or `/etc/modules.conf') for automatic loading of this ",
1840              "module. If not,\n",
1841              " you won't be able to open any /dev/i2c-* file (unless you",
1842              "have it built-in\n",
1843              " into your kernel)\n";
1844      } elsif (system "modprobe","i2c-dev") {
1845        print " Loading failed ($!), expect problems later on.\n";
1846      } else {
1847        print " Module loaded succesfully.\n";
1848      }
1849    }
1850  }
1851
1852  print "\n We are now going to do the adapter probings. Some adapters may ",
1853        "hang halfway\n",
1854        " through; we can't really help that. Also, some chips will be double ",
1855        "detected;\n",
1856        " we choose the one with the highest confidence value in that case.\n",
1857        " If you found that the adapter hung after probing a certain address, ",
1858        "you can\n",
1859        " specify that address to remain unprobed. If you have a PIIX4, that ",
1860        "often\n",
1861        " includes addresses 0x69 and/or 0x6a.\n";
1862
1863  my ($inp,@not_to_scan,$inp2);
1864  open INPUTFILE,"/proc/bus/i2c" or die "Couldn't open /proc/bus/i2c?!?";
1865  while (<INPUTFILE>) {
1866    my ($dev_nr,$type,$adap,$algo) = /^i2c-(\S+)\s+(\S+)\s+(.*?)\s*\t\s*(.*?)\s+$/;
1867    next if ($type eq "dummy");
1868    print "\n";
1869    print "Next adapter: $adap ($algo)\n";
1870    print "Do you want to scan it? (YES/no/selectively): ";
1871   
1872    $inp = <STDIN>;
1873    @not_to_scan=();
1874    if ($inp =~ /^\s*[Ss]/) {
1875      print "Please enter one or more addresses not to scan. Separate them ",
1876            "with comma's.\n",
1877            "You can specify a range by using dashes. Addresses may be ",
1878            "decimal (like 54)\n",
1879            "or hexadecimal (like 0x33).\n",
1880            "Addresses: ";
1881      $inp2 = <STDIN>;
1882      chop $inp2;
1883      @not_to_scan = parse_not_to_scan 0,0x7f,$inp2;
1884    }
1885    scan_adapter $dev_nr, $adap, $algo, find_adapter_driver($adap,$algo),
1886                 \@not_to_scan   unless $inp =~ /^\s*[Nn]/;
1887  }
1888
1889  print "\n Some chips are also accessible through the ISA bus. ISA probes ",
1890        "are\n",
1891        " typically a bit more dangerous, as we have to write to I/O ports ",
1892        "to do\n",
1893        " this. ";
1894  if ($> != 0) {
1895    print "As you are not root, we shall skip this step.\n";
1896  } else {
1897    print " Do you want to scan the ISA bus? (YES/no): ";
1898    if (not <STDIN> =~ /^\s*[Nn]/) {
1899      initialize_ioports or die "Sorry, can't access /dev/port ($!)?!?";
1900      scan_isa_bus;
1901    }
1902  }
1903
1904  print "\n Now follows a summary of the probes I have just done.\n";
1905  print " Just press ENTER to continue: ";
1906  <STDIN>;
1907
1908  my ($chip,$data);
1909  foreach $chip (@chips_detected) {
1910    print "\nDriver `$$chip{driver}' ";
1911    if (@{$$chip{detected}}) {
1912      if (@{$$chip{misdetected}}) {
1913        print "(should be inserted but causes problems):\n";
1914      } else {
1915        print "(should be inserted):\n";
1916      }
1917    } else {
1918      if (@{$$chip{misdetected}}) {
1919        print "(may not be inserted):\n";
1920      } else {
1921        print "(should not be inserted, but is harmless):\n";
1922      }
1923    }
1924    if (@{$$chip{detected}}) {
1925      print "  Detects correctly:\n";
1926      print_chips_report $chip->{detected};
1927    }
1928    if (@{$$chip{misdetected}}) {
1929      print "  Misdetects:\n";
1930      print_chips_report $chip->{misdetected};
1931    }
1932  }
1933
1934  print "\n\n",
1935        " I will now generate the commands needed to load the I2C modules.\n",
1936        " Sometimes, a chip is available both through the ISA bus and an ",
1937        "I2C bus.\n",
1938        " ISA bus access is faster, but you need to load an additional driver ",
1939        "module\n",
1940        " for it. If you have the choice, do you want to use the ISA bus or ",
1941        "the\n",
1942        " I2C/SMBus (ISA/smbus)? ";
1943  my $use_isa = not <STDIN> =~ /\s*[Ss]/;
1944     
1945  my ($modprobes,$configfile) = generate_modprobes $use_isa;
1946  print "\nWARNING! If you have some things built into your kernel, the \n",
1947        "below list will contain too many modules. Skip the appropriate ones!";
1948  print "\nTo load everything that is needed, add this to some /etc/rc* ",
1949        "file:\n\n";
1950  print "#----cut here----\n";
1951  print $modprobes;
1952  print "#----cut here----\n";
1953  print "\nTo make the sensors modules behave correctly, add these lines to ",
1954        "either\n",
1955        "/etc/modules.conf or /etc/conf.modules:\n\n";
1956  print "#----cut here----\n";
1957  print $configfile;
1958  print "#----cut here----\n";
1959 
1960}
1961
1962main;
Note: See TracBrowser for help on using the browser.