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

View File

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