xpp: sysfs access cleanups

With /proc/xpp code removed, some cleanup can be done.

* No need to search alternative attribute names, so xbus_attr_path() is
  redundant.

* Don't compose xbus sysfs directory in multiple code locations.
  Do it once in Xbus->new() and use the result ($xbus->sysfs_dir)

* Pass XPD directory strings directly to Xpd->new()
  - So we parse it for unit/subunit only in Xpd->new()

* Don't parse xbus sysfs name:
  - Toplevel scan, pass it as is to Xbus->new()
  - Xbus->new() deduce the xbus->num from the naming
    of Xpd's inside (nnⓂ️k) -- this is a hack until
    we add xbus number as an xbus sysfs attribute

Signed-off-by: Oron Peled <oron.peled@xorcom.com>
Acked-by: Tzafrir Cohen <tzafrir.cohen@xorcom.com>

git-svn-id: http://svn.astersk.org/svn/dahdi/tools/trunk@10494 17933a7a-c749-41c5-a318-cba88f637d49
This commit is contained in:
Oron Peled 2012-03-15 20:32:27 +00:00 committed by Tzafrir Cohen
parent 6027d2d1d1
commit 3572bb5a13
3 changed files with 35 additions and 38 deletions

View File

@ -50,8 +50,8 @@ sub scan($) {
opendir(D, $sysfs_astribanks) || return();
while(my $entry = readdir D) {
next unless $entry =~ /xbus-(\d+)/;
my $xbus = Dahdi::Xpp::Xbus->new($1);
next if $entry eq '.' or $entry eq '..';
my $xbus = Dahdi::Xpp::Xbus->new($sysfs_astribanks, $entry);
push(@xbuses, $xbus);
}
closedir D;

View File

@ -45,23 +45,11 @@ sub get_xpd_by_number($$) {
return $wanted;
}
my %file_warned; # Prevent duplicate warnings about same file.
sub xbus_attr_path($$) {
my ($busnum, @attr) = @_;
foreach my $attr (@attr) {
my $file = sprintf "$Dahdi::Xpp::sysfs_astribanks/xbus-%02d/$attr", $busnum;
next unless -f $file;
return $file;
}
return undef;
}
sub xbus_getattr($$) {
my $xbus = shift || die;
my $attr = shift || die;
$attr = lc($attr);
my $file = xbus_attr_path($xbus->num, lc($attr));
my $file = sprintf "%s/%s", $xbus->sysfs_dir, $attr;
open(F, $file) || die "Failed opening '$file': $!";
my $val = <F>;
@ -104,12 +92,11 @@ sub transport_type($$) {
}
sub read_xpdnames($) {
my $xbus_num = shift || die;
my $xbus_dir = "$Dahdi::Xpp::sysfs_astribanks/xbus-$xbus_num";
my $pat = sprintf "%s/xbus-%02d/[0-9][0-9]:[0-9]:[0-9]", $Dahdi::Xpp::sysfs_astribanks, $xbus_num;
my $xbus_dir = shift or die;
my $pat = sprintf "%s/[0-9][0-9]:[0-9]:[0-9]", $xbus_dir;
my @xpdnames;
#print STDERR "read_xpdnames($xbus_num): $pat\n";
#printf STDERR "read_xpdnames(%s): $pat\n", $xbus_dir;
foreach (glob $pat) {
die "Bad /sys entry: '$_'" unless m/^.*\/([0-9][0-9]):([0-9]):([0-9])$/;
my ($busnum, $unit, $subunit) = ($1, $2, $3);
@ -120,19 +107,30 @@ sub read_xpdnames($) {
return @xpdnames;
}
sub read_num($) {
my $self = shift or die;
my $xbus_dir = $self->sysfs_dir;
my @xpdnames = read_xpdnames($xbus_dir);
my $first = shift @xpdnames or die "No XPDs for '$xbus_dir'\n";
$first =~ /^(\d+\d+).*/;
return $1;
}
sub new($$) {
my $pack = shift or die "Wasn't called as a class method\n";
my $num = shift;
my $xbus_dir = "$Dahdi::Xpp::sysfs_astribanks/xbus-$num";
my $self = {
NUM => $num,
NAME => "XBUS-$num",
SYSFS_DIR => $xbus_dir,
};
my $parent_dir = shift or die;
my $entry_dir = shift or die;
my $xbus_dir = "$parent_dir/$entry_dir";
my $self = {};
bless $self, $pack;
$self->{SYSFS_DIR} = $xbus_dir;
my $num = $self->read_num;
$self->{NUM} = $num;
$self->{NAME} = "XBUS-$num";
$self->read_attrs;
# Get transport related info
my $transport = "$xbus_dir/transport";
die "OLD DRIVER: missing '$transport'\n" unless -e $transport;
my $transport_type = $self->transport_type($xbus_dir);
if(defined $transport_type) {
my $tt = "Dahdi::Hardware::$transport_type";
@ -141,11 +139,9 @@ sub new($$) {
}
my @xpdnames;
my @xpds;
die "OLD DRIVER: missing '$transport'\n" unless -e $transport;
@xpdnames = read_xpdnames($num);
@xpdnames = read_xpdnames($self->sysfs_dir);
foreach my $xpdstr (@xpdnames) {
my ($busnum, $unit, $subunit) = split(/:/, $xpdstr);
my $xpd = Dahdi::Xpp::Xpd->new($self, $unit, $subunit, "$xbus_dir/$xpdstr");
my $xpd = Dahdi::Xpp::Xpd->new($self, $xpdstr);
push(@xpds, $xpd);
}
@{$self->{XPDS}} = sort { $a->id <=> $b->id } @xpds;

View File

@ -99,14 +99,15 @@ my %file_warned; # Prevent duplicate warnings about same file.
sub xpd_attr_path($@) {
my $self = shift || die;
my $xbus = $self->xbus;
my ($busnum, $unitnum, $subunitnum, @attr) = (
$self->xbus->num,
$xbus->num,
$self->unit,
$self->subunit,
@_);
foreach my $attr (@attr) {
my $file = sprintf "$Dahdi::Xpp::sysfs_xpds/%02d:%1d:%1d/$attr",
$busnum, $unitnum, $subunitnum;
my $file = sprintf "%s/%02d:%1d:%1d/$attr",
$xbus->sysfs_dir, $busnum, $unitnum, $subunitnum;
next unless -f $file;
return $file;
}
@ -217,12 +218,12 @@ sub xpds_by_spanno() {
return @idx;
}
sub new($$$$$) {
sub new($$$) {
my $pack = shift or die "Wasn't called as a class method\n";
my $xbus = shift || die;
my $unit = shift; # May be zero
my $subunit = shift; # May be zero
my $sysfsdir = shift || die;
my $xbus = shift or die;
my $xpdstr = shift or die;
my $sysfsdir = sprintf "%s/%s", $xbus->sysfs_dir, $xpdstr;
my ($busnum, $unit, $subunit) = split(/:/, $xpdstr);
my $self = {
XBUS => $xbus,
ID => sprintf("%1d%1d", $unit, $subunit),