Changeset 240

Show
Ignore:
Timestamp:
02/18/99 00:24:24 (14 years ago)
Author:
frodo
Message:

Now all SMBus access functionality of i2c-dev.h is available in this
perl script too.

Location:
lm-sensors/trunk/prog/detect
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/trunk/prog/detect/detect.pl

    r238 r240  
    185185$IOCTL_I2C_MDELAY = 0x0706; 
    186186 
    187 # General ones 
     187# General ones: 
    188188$IOCTL_I2C_SLAVE = 0x0703; 
    189189$IOCTL_I2C_TENBIT = 0x0704; 
    190190$IOCTL_I2C_SMBUS = 0x0720; 
    191191 
     192 
    192193use vars qw($SMBUS_READ $SMBUS_WRITE $SMBUS_QUICK $SMBUS_BYTE $SMBUS_BYTE_DATA  
    193194            $SMBUS_WORD_DATA $SMBUS_PROC_CALL $SMBUS_BLOCK_DATA); 
     195 
    194196# These are copied from <linux/smbus.h> 
     197 
    195198$SMBUS_READ = 1; 
    196199$SMBUS_WRITE = 0; 
     
    202205$SMBUS_BLOCK_DATA = 5; 
    203206 
     207# Select the device to communicate with through its address. 
     208# $_[0]: Reference to an opened filehandle 
     209# $_[1]: Address to select 
     210# Returns: 0 on failure, 1 on success. 
    204211sub i2c_set_slave_addr 
    205212{ 
     
    210217 
    211218# i2c_smbus_access is based upon the corresponding C function (see  
    212 # <linux/i2c-dev.h>). Note that 'data' is a reference to a list, 
    213 # while 'file' is a reference to a filehandle. Also, it seems that in 
    214 # a struct, chars are encoded as shorts. This is all very tricky and 
    215 # depends on C compiler internals... 
     219# <linux/i2c-dev.h>). You should not need to call this directly. 
     220# Exact calling conventions are intricate; read i2c-dev.c if you really need 
     221# to know. 
     222# $_[0]: Reference to an opened filehandle 
     223# $_[1]: $SMBUS_READ for reading, $SMBUS_WRITE for writing 
     224# $_[2]: Command (usually register number) 
     225# $_[3]: Transaction kind ($SMBUS_BYTE, $SMBUS_BYTE_DATA, etc.) 
     226# $_[4]: Reference to an array used for input/output of data 
     227# Returns: 0 on failure, 1 on success. 
     228# Note the "SS" pack, even though they are declared as chars in the C struct! 
     229# This is very compiler-dependent; I wish there was some other way to do this. 
    216230sub i2c_smbus_access 
    217231{ 
     
    224238} 
    225239 
    226 sub i2c_read_byte_data 
     240# $_[0]: Reference to an opened filehandle 
     241# $_[1]: Either 0 or 1 
     242# Returns: -1 on failure, the 0 on success. 
     243sub i2c_smbus_write_quick 
     244{ 
     245  my ($file,$value) = @_; 
     246  my $data = []; 
     247  i2c_smbus_access $file, $value, 0, $SMBUS_QUICK, $data  
     248         or return -1; 
     249  return 0; 
     250} 
     251 
     252# $_[0]: Reference to an opened filehandle 
     253# Returns: -1 on failure, the read byte on success. 
     254sub i2c_smbus_read_byte 
     255{ 
     256  my ($file) = @_; 
     257  my $data = []; 
     258  i2c_smbus_access $file, $SMBUS_READ, 0, $SMBUS_BYTE, $data  
     259         or return -1; 
     260  return $$data[0]; 
     261} 
     262 
     263# $_[0]: Reference to an opened filehandle 
     264# $_[1]: Byte to write 
     265# Returns: -1 on failure, 0 on success. 
     266sub i2c_smbus_write_byte 
    227267{ 
    228268  my ($file,$command) = @_; 
    229   my @data = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); 
    230   i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_BYTE_DATA, \@data  
    231          or return -1; 
    232   printf "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", @data; 
    233   return $data[0]; 
     269  my $data = [$command]; 
     270  i2c_smbus_access $file, $SMBUS_WRITE, 0, $SMBUS_BYTE, $data  
     271         or return -1; 
     272  return 0; 
     273} 
     274 
     275# $_[0]: Reference to an opened filehandle 
     276# $_[1]: Command byte (usually register number) 
     277# Returns: -1 on failure, the read byte on success. 
     278sub i2c_smbus_read_byte_data 
     279{ 
     280  my ($file,$command) = @_; 
     281  my $data = []; 
     282  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_BYTE_DATA, $data  
     283         or return -1; 
     284  return $$data[0]; 
    234285} 
    235286   
    236 sub i2c_read_word_data 
     287# $_[0]: Reference to an opened filehandle 
     288# $_[1]: Command byte (usually register number) 
     289# $_[2]: Byte to write 
     290# Returns: -1 on failure, 0 on success. 
     291sub i2c_smbus_write_byte_data 
     292{ 
     293  my ($file,$command,$value) = @_; 
     294  my $data = [$value]; 
     295  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_BYTE_DATA, $data  
     296         or return -1; 
     297  return 0; 
     298} 
     299 
     300# $_[0]: Reference to an opened filehandle 
     301# $_[1]: Command byte (usually register number) 
     302# $_[2]: Byte to write 
     303# Returns: -1 on failure, 0 on success. 
     304# Note: some devices use the wrong endiannes; use swap_bytes to correct for  
     305# this. 
     306sub i2c_smbus_write_word_data 
     307{ 
     308  my ($file,$command,$value) = @_; 
     309  my $data = [$value & 0xff, $value >> 8]; 
     310  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_WORD_DATA, $data  
     311         or return -1; 
     312  return 0; 
     313} 
     314 
     315# $_[0]: Reference to an opened filehandle 
     316# $_[1]: Command byte (usually register number) 
     317# Returns: -1 on failure, the read word on success. 
     318# Note: some devices use the wrong endiannes; use swap_bytes to correct for  
     319# this. 
     320sub i2c_smbus_read_word_data 
    237321{ 
    238322  my ($file,$command) = @_; 
    239   my $data = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; 
     323  my $data = []; 
    240324  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_WORD_DATA, $data  
    241325         or return -1; 
    242326  return $$data[0] + 256 * $$data[1]; 
    243327} 
    244    
     328 
     329# $_[0]: Reference to an opened filehandle 
     330# $_[1]: Command byte (usually register number) 
     331# $_[2]: Word to write 
     332# Returns: -1 on failure, read word on success. 
     333# Note: some devices use the wrong endiannes; use swap_bytes to correct for  
     334# this. 
     335sub i2c_smbus_process_call 
     336{ 
     337  my ($file,$command,$value) = @_; 
     338  my $data = [$value & 0xff, $value >> 8]; 
     339  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_PROC_CALL, $data  
     340         or return -1; 
     341  return $$data[0] + 256 * $$data[1]; 
     342} 
     343 
     344# $_[0]: Reference to an opened filehandle 
     345# $_[1]: Command byte (usually register number) 
     346# Returns: Undefined on failure, a list of read bytes on success 
     347# Note: some devices use the wrong endiannes; use swap_bytes to correct for  
     348# this. 
     349sub i2c_smbus_read_block_data 
     350{ 
     351  my ($file,$command) = @_; 
     352  my $data = []; 
     353  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_BLOCK_DATA, $data  
     354         or return; 
     355  shift @$data; 
     356  return @$data; 
     357} 
     358 
     359# $_[0]: Reference to an opened filehandle 
     360# $_[1]: Command byte (usually register number) 
     361# @_[2..]: List of values to write 
     362# Returns: -1 on failure, 0 on success. 
     363# Note: some devices use the wrong endiannes; use swap_bytes to correct for  
     364# this. 
     365sub i2c_smbus_read_block_data 
     366{ 
     367  my ($file,$command,@data) = @_; 
     368  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_BLOCK_DATA, \@data  
     369         or return; 
     370  return 0; 
     371} 
     372 
     373# $_[0]: Reference to an opened filehandle 
     374####################### 
     375# AUXILIARY FUNCTIONS # 
     376####################### 
     377 
     378sub swap_bytes 
     379{ 
     380  return (($_[0] & 0xff00) >> 8) + (($_[0] & 0x00ff) << 7) 
     381} 
     382 
    245383 
    246384################ 
     
    248386################ 
    249387 
     388my @hallo; 
     389 
    250390intialize_proc_pci; 
    251391adapter_pci_detection; 
    252392 
    253393# TEST! 
    254 #open FILE, "+>/dev/i2c-0" or die "Can't open /dev/i2c-0!"; 
    255 #i2c_set_slave_addr \*FILE, 0x49 or die "Couldn't set slave addr!"; 
    256 #print (i2c_read_word_data \*FILE, 0), "\n"; 
     394open FILE, "+>/dev/i2c-0" or die "Can't open /dev/i2c-0!"; 
     395i2c_set_slave_addr \*FILE, 0x49 or die "Couldn't set slave addr!"; 
     396print (i2c_read_word_data \*FILE, 0), "\n"; 
  • lm-sensors/trunk/prog/detect/sensors-detect

    r238 r240  
    185185$IOCTL_I2C_MDELAY = 0x0706; 
    186186 
    187 # General ones 
     187# General ones: 
    188188$IOCTL_I2C_SLAVE = 0x0703; 
    189189$IOCTL_I2C_TENBIT = 0x0704; 
    190190$IOCTL_I2C_SMBUS = 0x0720; 
    191191 
     192 
    192193use vars qw($SMBUS_READ $SMBUS_WRITE $SMBUS_QUICK $SMBUS_BYTE $SMBUS_BYTE_DATA  
    193194            $SMBUS_WORD_DATA $SMBUS_PROC_CALL $SMBUS_BLOCK_DATA); 
     195 
    194196# These are copied from <linux/smbus.h> 
     197 
    195198$SMBUS_READ = 1; 
    196199$SMBUS_WRITE = 0; 
     
    202205$SMBUS_BLOCK_DATA = 5; 
    203206 
     207# Select the device to communicate with through its address. 
     208# $_[0]: Reference to an opened filehandle 
     209# $_[1]: Address to select 
     210# Returns: 0 on failure, 1 on success. 
    204211sub i2c_set_slave_addr 
    205212{ 
     
    210217 
    211218# i2c_smbus_access is based upon the corresponding C function (see  
    212 # <linux/i2c-dev.h>). Note that 'data' is a reference to a list, 
    213 # while 'file' is a reference to a filehandle. Also, it seems that in 
    214 # a struct, chars are encoded as shorts. This is all very tricky and 
    215 # depends on C compiler internals... 
     219# <linux/i2c-dev.h>). You should not need to call this directly. 
     220# Exact calling conventions are intricate; read i2c-dev.c if you really need 
     221# to know. 
     222# $_[0]: Reference to an opened filehandle 
     223# $_[1]: $SMBUS_READ for reading, $SMBUS_WRITE for writing 
     224# $_[2]: Command (usually register number) 
     225# $_[3]: Transaction kind ($SMBUS_BYTE, $SMBUS_BYTE_DATA, etc.) 
     226# $_[4]: Reference to an array used for input/output of data 
     227# Returns: 0 on failure, 1 on success. 
     228# Note the "SS" pack, even though they are declared as chars in the C struct! 
     229# This is very compiler-dependent; I wish there was some other way to do this. 
    216230sub i2c_smbus_access 
    217231{ 
     
    224238} 
    225239 
    226 sub i2c_read_byte_data 
     240# $_[0]: Reference to an opened filehandle 
     241# $_[1]: Either 0 or 1 
     242# Returns: -1 on failure, the 0 on success. 
     243sub i2c_smbus_write_quick 
     244{ 
     245  my ($file,$value) = @_; 
     246  my $data = []; 
     247  i2c_smbus_access $file, $value, 0, $SMBUS_QUICK, $data  
     248         or return -1; 
     249  return 0; 
     250} 
     251 
     252# $_[0]: Reference to an opened filehandle 
     253# Returns: -1 on failure, the read byte on success. 
     254sub i2c_smbus_read_byte 
     255{ 
     256  my ($file) = @_; 
     257  my $data = []; 
     258  i2c_smbus_access $file, $SMBUS_READ, 0, $SMBUS_BYTE, $data  
     259         or return -1; 
     260  return $$data[0]; 
     261} 
     262 
     263# $_[0]: Reference to an opened filehandle 
     264# $_[1]: Byte to write 
     265# Returns: -1 on failure, 0 on success. 
     266sub i2c_smbus_write_byte 
    227267{ 
    228268  my ($file,$command) = @_; 
    229   my @data = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); 
    230   i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_BYTE_DATA, \@data  
    231          or return -1; 
    232   printf "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", @data; 
    233   return $data[0]; 
     269  my $data = [$command]; 
     270  i2c_smbus_access $file, $SMBUS_WRITE, 0, $SMBUS_BYTE, $data  
     271         or return -1; 
     272  return 0; 
     273} 
     274 
     275# $_[0]: Reference to an opened filehandle 
     276# $_[1]: Command byte (usually register number) 
     277# Returns: -1 on failure, the read byte on success. 
     278sub i2c_smbus_read_byte_data 
     279{ 
     280  my ($file,$command) = @_; 
     281  my $data = []; 
     282  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_BYTE_DATA, $data  
     283         or return -1; 
     284  return $$data[0]; 
    234285} 
    235286   
    236 sub i2c_read_word_data 
     287# $_[0]: Reference to an opened filehandle 
     288# $_[1]: Command byte (usually register number) 
     289# $_[2]: Byte to write 
     290# Returns: -1 on failure, 0 on success. 
     291sub i2c_smbus_write_byte_data 
     292{ 
     293  my ($file,$command,$value) = @_; 
     294  my $data = [$value]; 
     295  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_BYTE_DATA, $data  
     296         or return -1; 
     297  return 0; 
     298} 
     299 
     300# $_[0]: Reference to an opened filehandle 
     301# $_[1]: Command byte (usually register number) 
     302# $_[2]: Byte to write 
     303# Returns: -1 on failure, 0 on success. 
     304# Note: some devices use the wrong endiannes; use swap_bytes to correct for  
     305# this. 
     306sub i2c_smbus_write_word_data 
     307{ 
     308  my ($file,$command,$value) = @_; 
     309  my $data = [$value & 0xff, $value >> 8]; 
     310  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_WORD_DATA, $data  
     311         or return -1; 
     312  return 0; 
     313} 
     314 
     315# $_[0]: Reference to an opened filehandle 
     316# $_[1]: Command byte (usually register number) 
     317# Returns: -1 on failure, the read word on success. 
     318# Note: some devices use the wrong endiannes; use swap_bytes to correct for  
     319# this. 
     320sub i2c_smbus_read_word_data 
    237321{ 
    238322  my ($file,$command) = @_; 
    239   my $data = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; 
     323  my $data = []; 
    240324  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_WORD_DATA, $data  
    241325         or return -1; 
    242326  return $$data[0] + 256 * $$data[1]; 
    243327} 
    244    
     328 
     329# $_[0]: Reference to an opened filehandle 
     330# $_[1]: Command byte (usually register number) 
     331# $_[2]: Word to write 
     332# Returns: -1 on failure, read word on success. 
     333# Note: some devices use the wrong endiannes; use swap_bytes to correct for  
     334# this. 
     335sub i2c_smbus_process_call 
     336{ 
     337  my ($file,$command,$value) = @_; 
     338  my $data = [$value & 0xff, $value >> 8]; 
     339  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_PROC_CALL, $data  
     340         or return -1; 
     341  return $$data[0] + 256 * $$data[1]; 
     342} 
     343 
     344# $_[0]: Reference to an opened filehandle 
     345# $_[1]: Command byte (usually register number) 
     346# Returns: Undefined on failure, a list of read bytes on success 
     347# Note: some devices use the wrong endiannes; use swap_bytes to correct for  
     348# this. 
     349sub i2c_smbus_read_block_data 
     350{ 
     351  my ($file,$command) = @_; 
     352  my $data = []; 
     353  i2c_smbus_access $file, $SMBUS_READ, $command, $SMBUS_BLOCK_DATA, $data  
     354         or return; 
     355  shift @$data; 
     356  return @$data; 
     357} 
     358 
     359# $_[0]: Reference to an opened filehandle 
     360# $_[1]: Command byte (usually register number) 
     361# @_[2..]: List of values to write 
     362# Returns: -1 on failure, 0 on success. 
     363# Note: some devices use the wrong endiannes; use swap_bytes to correct for  
     364# this. 
     365sub i2c_smbus_read_block_data 
     366{ 
     367  my ($file,$command,@data) = @_; 
     368  i2c_smbus_access $file, $SMBUS_WRITE, $command, $SMBUS_BLOCK_DATA, \@data  
     369         or return; 
     370  return 0; 
     371} 
     372 
     373# $_[0]: Reference to an opened filehandle 
     374####################### 
     375# AUXILIARY FUNCTIONS # 
     376####################### 
     377 
     378sub swap_bytes 
     379{ 
     380  return (($_[0] & 0xff00) >> 8) + (($_[0] & 0x00ff) << 7) 
     381} 
     382 
    245383 
    246384################ 
     
    248386################ 
    249387 
     388my @hallo; 
     389 
    250390intialize_proc_pci; 
    251391adapter_pci_detection; 
    252392 
    253393# TEST! 
    254 #open FILE, "+>/dev/i2c-0" or die "Can't open /dev/i2c-0!"; 
    255 #i2c_set_slave_addr \*FILE, 0x49 or die "Couldn't set slave addr!"; 
    256 #print (i2c_read_word_data \*FILE, 0), "\n"; 
     394open FILE, "+>/dev/i2c-0" or die "Can't open /dev/i2c-0!"; 
     395i2c_set_slave_addr \*FILE, 0x49 or die "Couldn't set slave addr!"; 
     396print (i2c_read_word_data \*FILE, 0), "\n";