| 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; |
| | 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; |
| 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; |
| 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; |
| 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]); |
| 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]; |
| 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 | } |
| 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; |