Show
Ignore:
Timestamp:
03/08/10 14:59:20 (5 years ago)
Author:
khali
Message:

Unload i2c-stub automatically if needed.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • i2c-tools/trunk/stub/i2c-stub-from-dump

    r5533 r5828  
    6262} 
    6363 
     64# Unload i2c-stub if we need an address it doesn't offer 
     65sub check_chip_addr { 
     66        my $chip_addr_file = shift; 
     67        my $addr = shift; 
     68        local $_; 
     69 
     70        open(CHIP_ADDR, $chip_addr_file) || return; 
     71        $_ = <CHIP_ADDR>; 
     72        chomp; 
     73        my %stub_addr = map { $_ => 1 } split ','; 
     74        close(CHIP_ADDR); 
     75 
     76        unless (exists $stub_addr{$addr}) { 
     77                print STDERR "Cycling i2c-stub to get the address we need\n"; 
     78                system("/sbin/rmmod", "i2c-stub"); 
     79        } 
     80} 
     81 
    6482# Load the required kernel drivers if needed 
    6583sub load_kernel_drivers 
     
    6987        my $nr; 
    7088 
     89        # i2c-stub may be loaded without the address we want 
     90        check_chip_addr("/sys/module/i2c_stub/parameters/chip_addr", $addr); 
     91 
    7192        # Maybe everything is already loaded 
    7293        $nr = get_i2c_stub_bus_number(); 
    73         if (defined $nr) { 
    74                 if (kernel_version_at_least(2, 6, 19)) { 
    75                         # Check if the chip address we need is there 
    76                         open(CHIP_ADDR, "/sys/module/i2c_stub/parameters/chip_addr"); 
    77                         $_ = <CHIP_ADDR>; 
    78                         chomp; 
    79                         my @stub_addr = split ','; 
    80                         close(CHIP_ADDR); 
    81  
    82                         foreach (@stub_addr) { 
    83                                 return $nr if $addr == $_; 
    84                         } 
    85                         printf STDERR "i2c-stub already loaded without support for address 0x%02x\n", $addr; 
    86                         exit 2; 
    87                 } 
    88                 return $nr; 
    89         } 
     94        return $nr if defined $nr; 
    9095 
    9196        system("/sbin/modprobe", "i2c-dev") == 0 || exit 1;