Changeset 5471

Show
Ignore:
Timestamp:
11/30/08 14:05:38 (4 years ago)
Author:
khali
Message:

Reindent I2C access functions.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/branches/lm-sensors-3.0.0/prog/detect/sensors-detect

    r5470 r5471  
    22662266# These are copied from <linux/i2c-dev.h> 
    22672267 
    2268 use constant IOCTL_I2C_SLAVE    => 0x0703; 
    2269 use constant IOCTL_I2C_FUNCS    => 0x0705; 
    2270 use constant IOCTL_I2C_SMBUS    => 0x0720; 
    2271  
    2272 use constant SMBUS_READ         => 1; 
    2273 use constant SMBUS_WRITE        => 0; 
    2274  
    2275 use constant SMBUS_QUICK        => 0; 
    2276 use constant SMBUS_BYTE         => 1; 
    2277 use constant SMBUS_BYTE_DATA    => 2; 
    2278 use constant SMBUS_WORD_DATA    => 3; 
     2268use constant IOCTL_I2C_SLAVE    => 0x0703; 
     2269use constant IOCTL_I2C_FUNCS    => 0x0705; 
     2270use constant IOCTL_I2C_SMBUS    => 0x0720; 
     2271 
     2272use constant SMBUS_READ         => 1; 
     2273use constant SMBUS_WRITE        => 0; 
     2274 
     2275use constant SMBUS_QUICK        => 0; 
     2276use constant SMBUS_BYTE         => 1; 
     2277use constant SMBUS_BYTE_DATA    => 2; 
     2278use constant SMBUS_WORD_DATA    => 3; 
    22792279 
    22802280use constant I2C_FUNC_SMBUS_QUICK       => 0x00010000; 
     
    22862286sub i2c_get_funcs 
    22872287{ 
    2288   my $file = shift; 
    2289   my $funcs = pack("L", 0); # Allocate space 
    2290  
    2291   ioctl($file, IOCTL_I2C_FUNCS, $funcs) or return -1; 
    2292   $funcs = unpack("L", $funcs); 
    2293  
    2294   return $funcs; 
     2288        my $file = shift; 
     2289        my $funcs = pack("L", 0); # Allocate space 
     2290 
     2291        ioctl($file, IOCTL_I2C_FUNCS, $funcs) or return -1; 
     2292        $funcs = unpack("L", $funcs); 
     2293 
     2294        return $funcs; 
    22952295} 
    22962296 
     
    23012301sub i2c_set_slave_addr 
    23022302{ 
    2303   my ($file, $addr) = @_; 
    2304  
    2305   # Reset register data cache 
    2306   @i2c_byte_cache = (); 
    2307  
    2308   $addr += 0; # Make sure it's a number not a string 
    2309   ioctl($file, IOCTL_I2C_SLAVE, $addr) or return 0; 
    2310   return 1; 
     2303        my ($file, $addr) = @_; 
     2304 
     2305        # Reset register data cache 
     2306        @i2c_byte_cache = (); 
     2307 
     2308        $addr += 0; # Make sure it's a number not a string 
     2309        ioctl($file, IOCTL_I2C_SLAVE, $addr) or return 0; 
     2310        return 1; 
    23112311} 
    23122312 
    23132313# i2c_smbus_access is based upon the corresponding C function (see 
    23142314# <linux/i2c-dev.h>). You should not need to call this directly. 
    2315 # Exact calling conventions are intricate; read i2c-dev.c if you really need 
    2316 # to know. 
    23172315# $_[0]: Reference to an opened filehandle 
    23182316# $_[1]: SMBUS_READ for reading, SMBUS_WRITE for writing 
     
    23262324sub i2c_smbus_access 
    23272325{ 
    2328   my ($file, $read_write, $command, $size, $data) = @_; 
    2329   my $data_array = pack("C32", @$data); 
    2330   my $ioctl_data = pack("C2x2Ip", $read_write, $command, $size, $data_array); 
    2331   ioctl($file, IOCTL_I2C_SMBUS, $ioctl_data) or return 0; 
    2332   @{$_[4]} = unpack("C32", $data_array); 
    2333   return 1; 
     2326        my ($file, $read_write, $command, $size, $data) = @_; 
     2327        my $data_array = pack("C32", @$data); 
     2328        my $ioctl_data = pack("C2x2Ip", $read_write, $command, $size, 
     2329                              $data_array); 
     2330 
     2331        ioctl($file, IOCTL_I2C_SMBUS, $ioctl_data) or return 0; 
     2332        @{$_[4]} = unpack("C32", $data_array); 
     2333        return 1; 
    23342334} 
    23352335 
     
    23382338sub i2c_smbus_read_byte 
    23392339{ 
    2340   my ($file) = @_; 
    2341   my @data; 
    2342   i2c_smbus_access($file, SMBUS_READ, 0, SMBUS_BYTE, \@data) 
    2343          or return -1; 
    2344   return $data[0]; 
     2340        my ($file) = @_; 
     2341        my @data; 
     2342 
     2343        i2c_smbus_access($file, SMBUS_READ, 0, SMBUS_BYTE, \@data) 
     2344                or return -1; 
     2345        return $data[0]; 
    23452346} 
    23462347 
     
    23532354sub i2c_smbus_read_byte_data 
    23542355{ 
    2355   my ($file, $command, $nocache) = @_; 
    2356   my @data; 
    2357   if (!$nocache && exists $i2c_byte_cache[$command]) { 
    2358     return $i2c_byte_cache[$command]; 
    2359   } 
    2360   i2c_smbus_access($file, SMBUS_READ, $command, SMBUS_BYTE_DATA, \@data) 
    2361          or return -1; 
    2362   return ($i2c_byte_cache[$command] = $data[0]); 
     2356        my ($file, $command, $nocache) = @_; 
     2357        my @data; 
     2358 
     2359        return $i2c_byte_cache[$command] 
     2360                if !$nocache && exists $i2c_byte_cache[$command]; 
     2361 
     2362        i2c_smbus_access($file, SMBUS_READ, $command, SMBUS_BYTE_DATA, \@data) 
     2363                or return -1; 
     2364        return ($i2c_byte_cache[$command] = $data[0]); 
    23632365} 
    23642366 
     
    23732375sub i2c_smbus_read_word_data 
    23742376{ 
    2375   my ($file, $command) = @_; 
    2376   my @data; 
    2377   i2c_smbus_access($file, SMBUS_READ, $command, SMBUS_WORD_DATA, \@data) 
    2378          or return -1; 
    2379   return $data[0] + 256 * $data[1]; 
     2377        my ($file, $command) = @_; 
     2378        my @data; 
     2379        i2c_smbus_access($file, SMBUS_READ, $command, SMBUS_WORD_DATA, \@data) 
     2380                or return -1; 
     2381        return $data[0] + 256 * $data[1]; 
    23802382} 
    23812383 
     
    23892391sub i2c_probe 
    23902392{ 
    2391   my ($file, $addr, $funcs) = @_; 
    2392   my $data = []; 
    2393   if (($addr >= 0x50 && $addr <= 0x5F) 
    2394    || ($addr >= 0x30 && $addr <= 0x37)) { 
    2395     # This covers all EEPROMs we know of, including page protection addresses. 
    2396     # Note that some page protection addresses will not reveal themselves with 
    2397     # this, because they ack on write only, but this is probably better since 
    2398     # some EEPROMs write-protect themselves permanently on almost any write to 
    2399     # their page protection address. 
    2400     return 0 unless ($funcs & I2C_FUNC_SMBUS_READ_BYTE); 
    2401     return i2c_smbus_access($file, SMBUS_READ, 0, SMBUS_BYTE, $data); 
    2402   } else { 
    2403     return 0 unless ($funcs & I2C_FUNC_SMBUS_QUICK); 
    2404     return i2c_smbus_access($file, SMBUS_WRITE, 0, SMBUS_QUICK, $data); 
    2405   } 
     2393        my ($file, $addr, $funcs) = @_; 
     2394        my $data = []; 
     2395 
     2396        if (($addr >= 0x50 && $addr <= 0x5F) 
     2397         || ($addr >= 0x30 && $addr <= 0x37)) { 
     2398                # This covers all EEPROMs we know of, including page protection 
     2399                # addresses. Note that some page protection addresses will not 
     2400                # reveal themselves with this, because they ack on write only, 
     2401                # but this is probably better since some EEPROMs write-protect 
     2402                # themselves permanently on almost any write to their page 
     2403                # protection address. 
     2404                return 0 unless ($funcs & I2C_FUNC_SMBUS_READ_BYTE); 
     2405                return i2c_smbus_access($file, SMBUS_READ, 0, SMBUS_BYTE, $data); 
     2406        } else { 
     2407                return 0 unless ($funcs & I2C_FUNC_SMBUS_QUICK); 
     2408                return i2c_smbus_access($file, SMBUS_WRITE, 0, SMBUS_QUICK, $data); 
     2409        } 
    24062410} 
    24072411 
     
    24182422sub i2c_safety_check 
    24192423{ 
    2420   my ($file) = @_; 
    2421   my $data; 
    2422  
    2423   # First we receive a byte from the chip, and remember it. 
    2424   $data = i2c_smbus_read_byte($file); 
    2425   return 1 if ($data < 0); 
    2426  
    2427   # We receive a byte again; very likely to be the same for 
    2428   # 1-register-only devices. 
    2429   return 1 if (i2c_smbus_read_byte($file) != $data); 
    2430  
    2431   # Then we try a standard byte read, with a register offset equal to 
    2432   # the byte we received; we should receive the same byte value in return. 
    2433   return 1 if (i2c_smbus_read_byte_data($file, $data) != $data); 
    2434  
    2435   # Then we try a standard byte read, with a slightly different register 
    2436   # offset; we should again receive the same byte value in return. 
    2437   return 1 if (i2c_smbus_read_byte_data($file, $data ^ 1) != ($data ^ 1)); 
    2438  
    2439   # Apprently this is a 1-register-only device, restore the original register 
    2440   # value and leave it alone. 
    2441   i2c_smbus_read_byte_data($file, $data); 
    2442   return 0; 
     2424        my ($file) = @_; 
     2425        my $data; 
     2426 
     2427        # First we receive a byte from the chip, and remember it. 
     2428        $data = i2c_smbus_read_byte($file); 
     2429        return 1 if ($data < 0); 
     2430 
     2431        # We receive a byte again; very likely to be the same for 
     2432        # 1-register-only devices. 
     2433        return 1 if (i2c_smbus_read_byte($file) != $data); 
     2434 
     2435        # Then we try a standard byte read, with a register offset equal to 
     2436        # the byte we received; we should receive the same byte value in return. 
     2437        return 1 if (i2c_smbus_read_byte_data($file, $data) != $data); 
     2438 
     2439        # Then we try a standard byte read, with a slightly different register 
     2440        # offset; we should again receive the same byte value in return. 
     2441        return 1 if (i2c_smbus_read_byte_data($file, $data ^ 1) != ($data ^ 1)); 
     2442 
     2443        # Apprently this is a 1-register-only device, restore the original 
     2444        # register value and leave it alone. 
     2445        i2c_smbus_read_byte_data($file, $data); 
     2446        return 0; 
    24432447} 
    24442448