From 3d8b5e9b613770c716c2c317f206839102667abb Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Mon, 8 Mar 2010 14:00:52 +0000 Subject: [PATCH] Add support for multiple dumps. This makes it easier to setup test environments with more than one I2C chip. git-svn-id: http://lm-sensors.org/svn/i2c-tools/trunk@5830 7894878c-1315-0410-8ee3-d5d059ff63e0 --- CHANGES | 1 + stub/i2c-stub-from-dump | 56 +++++++++++++++++++++++++++++++++-------------- stub/i2c-stub-from-dump.8 | 14 ++++++------ 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/CHANGES b/CHANGES index 2159dad..ef25032 100644 --- a/CHANGES +++ b/CHANGES @@ -17,6 +17,7 @@ SVN i2cset: Add support for short writes with PEC i2c-stub-from-dump: Use udev settle to speed up initialization Unload i2c-stub automatically if needed + Add support for multiple dumps 3.0.2 (2008-11-29) i2c-dev.h: Drop I2C_FUNC_SMBUS_*I2C_BLOCK_2 defines diff --git a/stub/i2c-stub-from-dump b/stub/i2c-stub-from-dump index 3b6fbfc..7eb7ff8 100755 --- a/stub/i2c-stub-from-dump +++ b/stub/i2c-stub-from-dump @@ -22,7 +22,7 @@ # a device you do not have access to, but of which you have a dump. use strict; -use vars qw($bus_nr $addr $err); +use vars qw($bus_nr @addr $err); # Kernel version detection code by Mark M. Hoffman, # copied from sensors-detect. @@ -64,7 +64,7 @@ sub get_i2c_stub_bus_number # Unload i2c-stub if we need an address it doesn't offer sub check_chip_addr { my $chip_addr_file = shift; - my $addr = shift; + my @addr = @{shift()}; local $_; open(CHIP_ADDR, $chip_addr_file) || return; @@ -73,9 +73,12 @@ sub check_chip_addr { my %stub_addr = map { $_ => 1 } split ','; close(CHIP_ADDR); - unless (exists $stub_addr{$addr}) { - print STDERR "Cycling i2c-stub to get the address we need\n"; - system("/sbin/rmmod", "i2c-stub"); + foreach my $addr (@addr) { + unless (exists $stub_addr{$addr}) { + print STDERR "Cycling i2c-stub to get the address we need\n"; + system("/sbin/rmmod", "i2c-stub"); + return; + } } } @@ -83,11 +86,11 @@ sub check_chip_addr { sub load_kernel_drivers { local $_; - my $addr = oct shift; + my @addr = @{shift()}; my $nr; # i2c-stub may be loaded without the address we want - check_chip_addr("/sys/module/i2c_stub/parameters/chip_addr", $addr); + check_chip_addr("/sys/module/i2c_stub/parameters/chip_addr", \@addr); # Maybe everything is already loaded $nr = get_i2c_stub_bus_number(); @@ -95,7 +98,8 @@ sub load_kernel_drivers system("/sbin/modprobe", "i2c-dev") == 0 || exit 1; if (kernel_version_at_least(2, 6, 19)) { - system("/sbin/modprobe", "i2c-stub", "chip_addr=$addr") == 0 || exit 1; + system("/sbin/modprobe", "i2c-stub", + "chip_addr=".join(',', @addr)) == 0 || exit 1; } else { system("/sbin/modprobe", "i2c-stub") == 0 || exit 1; } @@ -180,26 +184,46 @@ if ($>) { exit 1; } -if (@ARGV != 2) { - print STDERR "Usage: i2c-stub-from-dump \n"; +if (@ARGV < 2) { + print STDERR "Usage: i2c-stub-from-dump [,,...] [ ...]\n"; exit 1; } # Check the parameters -$addr = $ARGV[0]; -if ($addr !~ m/^0x[0-7][0-9a-f]$/i) { - print STDERR "Invalid address $addr\n"; - exit 1; +@addr = split(/,/, shift @ARGV); +foreach (@addr) { + unless (m/^0x[0-7][0-9a-f]$/i) { + print STDERR "Invalid address $_\n"; + exit 1; + } + $_ = oct $_; +} + +if (@addr < @ARGV) { + print STDERR "Fewer addresses than dumps provided\n"; + exit 4; } initialize_kernel_version(); +if (@addr > 1 && !kernel_version_at_least(2, 6, 24)) { + print STDERR "Multiple addresses not supported by this kernel version\n"; + exit 5; +} -$bus_nr = load_kernel_drivers($addr); +$bus_nr = load_kernel_drivers(\@addr); # We don't want to see the output of 256 i2cset open(SAVEOUT, ">&STDOUT"); open(STDOUT, ">/dev/null"); -$err = process_dump(oct $addr, $ARGV[1]); +foreach (@addr) { + if (!@ARGV) { + printf STDERR "Skipping \%d-\%04x, no dump file privided\n", + $bus_nr, $_; + next; + } + $err = process_dump($_, shift @ARGV); + last if $err; +} close(STDOUT); exit($err); diff --git a/stub/i2c-stub-from-dump.8 b/stub/i2c-stub-from-dump.8 index b594839..b1550ed 100644 --- a/stub/i2c-stub-from-dump.8 +++ b/stub/i2c-stub-from-dump.8 @@ -1,20 +1,20 @@ -.TH I2C-STUB-FROM-DUMP 8 "April 2008" +.TH I2C-STUB-FROM-DUMP 8 "March 2010" .SH NAME -i2c-stub-from-dump \- feed i2c-stub with a dump file +i2c-stub-from-dump \- feed i2c-stub with dump files .SH SYNOPSIS .B i2c-stub-from-dump -.I address -.I dump-file +.IR address [, address ,...] +.IR dump-file " [" dump-file " ...]" .SH DESCRIPTION i2c-stub-from-dump is a small helper script for the i2c-stub kernel driver. -It lets you setup a fake I2C chip on the i2c-stub bus based on a dump of -the chip you want to emulate. +It lets you setup one or more fake I2C chips on the i2c-stub bus based on +dumps of the chips you want to emulate. i2c-stub-from-dump requires i2cdetect and i2cset to be installed and reachable through the user's PATH. The former is used to find out the i2c-stub -bus number, while the latter is used to write to the fake I2C chip. +bus number, while the latter is used to write to the fake I2C chips. .SH EXAMPLE You have an I2C chip on system A. You would like to do some development on its