| 1 |
#! /usr/bin/perl -w |
| 2 |
# |
| 3 |
# Written by Oron Peled <[email protected]> |
| 4 |
# Copyright (C) 2007, Xorcom |
| 5 |
# This program is free software; you can redistribute and/or |
| 6 |
# modify it under the same terms as Perl itself. |
| 7 |
# |
| 8 |
# $Id$ |
| 9 |
# |
| 10 |
use strict; |
| 11 |
use File::Basename; |
| 12 |
BEGIN { my $dir = dirname($0); unshift(@INC, "$dir", "$dir/perl_modules"); } |
| 13 |
|
| 14 |
use Dahdi; |
| 15 |
use Dahdi::Span; |
| 16 |
use Dahdi::Xpp; |
| 17 |
use Dahdi::Xpp::Xbus; |
| 18 |
use Dahdi::Xpp::Xpd; |
| 19 |
use Getopt::Std; |
| 20 |
|
| 21 |
sub usage { |
| 22 |
die "Usage: $0 [-v] [-R] [-s sort_order] [on|off|1|0]\n"; |
| 23 |
} |
| 24 |
|
| 25 |
sub check_param { |
| 26 |
my $param = shift || die; |
| 27 |
open(F, $param) || return ''; |
| 28 |
my $val = <F>; |
| 29 |
close F; |
| 30 |
chomp $val; |
| 31 |
return $val; |
| 32 |
} |
| 33 |
|
| 34 |
my %opts; |
| 35 |
getopts('vRs:', \%opts) || usage; |
| 36 |
|
| 37 |
my $dahdi_autoreg = check_param('/sys/module/xpp/parameters/dahdi_autoreg') eq 'Y'; |
| 38 |
my $auto_assign_spans = check_param('/sys/module/dahdi/parameters/auto_assign_spans') ne '0'; |
| 39 |
my $assigned_spans_config = $ENV{'ASSIGNED_SPANS_CONF_FILE'} || '/etc/dahdi/assigned-spans.conf'; |
| 40 |
my $span_types_config = $ENV{'SPAN_TYPES_CONF_FILE'} || '/etc/dahdi/span-types.conf'; |
| 41 |
my $have_assigned_spans_config = -f $assigned_spans_config || 0; |
| 42 |
my $have_span_types_config = -f $span_types_config || 0; |
| 43 |
|
| 44 |
# Spans will be auto-assigned by default if either: |
| 45 |
# - Driver $auto_assign_spans them or |
| 46 |
# - Udev script see that we $have_span_types_config and it "add" them |
| 47 |
my $default_auto_assign = $auto_assign_spans || $have_assigned_spans_config; |
| 48 |
|
| 49 |
my $sorter; |
| 50 |
my $sort_order = $opts{'s'}; |
| 51 |
if(defined $sort_order) { |
| 52 |
my $sorter = Dahdi::Xpp::sorters($sort_order); |
| 53 |
|
| 54 |
if(!defined $sorter) { |
| 55 |
my @sorter_names = Dahdi::Xpp::sorters; |
| 56 |
print STDERR "Unknown sort order $sort_order. Select from:\n\t"; |
| 57 |
print STDERR join("\n\t", @sorter_names); |
| 58 |
print STDERR "\n"; |
| 59 |
exit 1; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@ARGV == 0 or @ARGV == 1 or usage; |
| 64 |
my $on = shift; |
| 65 |
my $verbose = $opts{'v'}; |
| 66 |
my $should_output = 1; |
| 67 |
|
| 68 |
#print "dahdi_autoreg=$dahdi_autoreg auto_assign_spans=$auto_assign_spans have_assigned_spans_config='$have_assigned_spans_config' have_span_types_config='$have_span_types_config'\n"; |
| 69 |
|
| 70 |
if(defined($on)) { # Translate to booleans |
| 71 |
$on = uc($on); |
| 72 |
$on =~ /^(ON|OFF|1|0)$/ or usage; |
| 73 |
$on = ($on eq 'ON') ? 1 : 0; |
| 74 |
$should_output = 0 unless $verbose; |
| 75 |
} |
| 76 |
|
| 77 |
sub state2str($) { |
| 78 |
return (shift)?"on":"off"; |
| 79 |
} |
| 80 |
|
| 81 |
sub myprintf { |
| 82 |
printf @_ if $should_output; |
| 83 |
} |
| 84 |
|
| 85 |
foreach my $xbus (Dahdi::Xpp::xbuses($sorter)) { |
| 86 |
my $prev = $xbus->dahdi_registration($on); |
| 87 |
if(!defined($prev)) { # Failure |
| 88 |
printf STDERR "%s: Failed %s\n", $xbus->name, $!; |
| 89 |
next; |
| 90 |
} |
| 91 |
my $reg_str; |
| 92 |
if (defined $on) { |
| 93 |
$reg_str = ($on) ? "registering" : "unregistering"; |
| 94 |
} else { |
| 95 |
$reg_str = ($prev) ? "registered" : "unregistered"; |
| 96 |
} |
| 97 |
myprintf "%-10s\t%3s-%s\t%s (%s)\n", |
| 98 |
$xbus->name, $xbus->xpporder, $xbus->label, $xbus->connector, |
| 99 |
$reg_str; |
| 100 |
next unless $xbus->status eq 'CONNECTED'; |
| 101 |
# Only assign if no default assignment, or forced by '-R' option |
| 102 |
if (defined($on) && $on) { |
| 103 |
if ($opts{'R'} || ! $default_auto_assign) { |
| 104 |
# Emulate /etc/dahdi/assigned-spans.conf: |
| 105 |
# - We iterate over $xbus according to /etc/dahdi/xpp_order |
| 106 |
# - We "auto" assign all spans of current $xbus |
| 107 |
my $devpath = sprintf "/sys/bus/dahdi_devices/devices/astribanks:xbus-%02d", $xbus->num; |
| 108 |
my @cmd = ('dahdi_span_assignments', 'auto', $devpath); |
| 109 |
system @cmd; |
| 110 |
warn "Failed '@cmd' (status=$?)\n" if $?; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
if (defined($on) && $on) { |
| 115 |
if ($opts{'R'} || ! $default_auto_assign) { |
| 116 |
# wait for UDEV to do its stuff |
| 117 |
system "dahdi_waitfor_span_assignments assigned"; |
| 118 |
} |
| 119 |
} |
| 120 |
foreach my $xbus (Dahdi::Xpp::xbuses($sorter)) { |
| 121 |
foreach my $xpd (Dahdi::Xpp::Xpd::telephony_devs($xbus->xpds())) { |
| 122 |
my $spanno = $xpd->xpd_getattr('span'); |
| 123 |
myprintf "\t%-10s: ", $xpd->fqn; |
| 124 |
my $spanstr = ($spanno) ? ("Span " . $spanno) : "unassigned"; |
| 125 |
myprintf "%s\n", $spanstr ; |
| 126 |
} |
| 127 |
} |
| 128 |
myprintf "# Sorted: $sort_order\n" if defined $sort_order; |
| 129 |
|
| 130 |
__END__ |
| 131 |
|
| 132 |
=head1 NAME |
| 133 |
|
| 134 |
dahdi_registration - Handle registration of Xorcom XPD modules in dahdi. |
| 135 |
|
| 136 |
=head1 SYNOPSIS |
| 137 |
|
| 138 |
dahdi_registration [-v] [-s sortorder] [-R] [on|off] |
| 139 |
|
| 140 |
=head1 DESCRIPTION |
| 141 |
|
| 142 |
Without parameters, show all connected XPDs sorted by physical connector order. |
| 143 |
Each one is show to be unregistered (off), or registered to a specific dahdi |
| 144 |
span (the span number is shown). |
| 145 |
|
| 146 |
All registerations/deregisterations are sorted by physical connector string. |
| 147 |
|
| 148 |
Span registration should generally always succeed. Span unregistration may |
| 149 |
fail if channels from the span are in use by e.g. asterisk. In such a case |
| 150 |
you'll also see those channels as '(In use)' in the output of lsdahdi(8). |
| 151 |
|
| 152 |
dahdi_registration is intended to be used when the kernel module parameter |
| 153 |
B<xpp.dahdi_autoreg> is false (and implicitly: when the module parameter |
| 154 |
B<dahdi.auto_assign_span> is true). See also the NOTES section regarding |
| 155 |
C<dahdi_span_assignments>. |
| 156 |
|
| 157 |
If dahdi_autoreg is true, the program will normally do nothing. |
| 158 |
|
| 159 |
=head2 Parameters |
| 160 |
|
| 161 |
off -- deregisters all XPD's from dahdi. |
| 162 |
|
| 163 |
on -- registers all XPD's to dahdi. |
| 164 |
|
| 165 |
=head2 Options |
| 166 |
|
| 167 |
=over |
| 168 |
|
| 169 |
=item -v |
| 170 |
|
| 171 |
verbose output. |
| 172 |
|
| 173 |
=item -R |
| 174 |
|
| 175 |
Force operations (on/off) even if the module parameter B<dahdi_autoreg> |
| 176 |
for xpp is enabled (which makes this program unneeded). |
| 177 |
|
| 178 |
=item -s I<sort_order> |
| 179 |
|
| 180 |
The sort order to use. |
| 181 |
|
| 182 |
=back |
| 183 |
|
| 184 |
If the option is not used, the sort order is taken from the environment |
| 185 |
variable XBUS_SORT and failing that: the hard-coded default of |
| 186 |
SORT_XPPORDER. |
| 187 |
|
| 188 |
The available sorting orders are documented in Dahdi::Xpp manual. |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
=head2 Sample Output |
| 193 |
|
| 194 |
An example of the output of dahdi_registration for some registered |
| 195 |
Astribanks: |
| 196 |
|
| 197 |
$ dahdi_registration -s type |
| 198 |
XBUS-01 usb:0000153 usb-0000:00:10.4-2 |
| 199 |
XBUS-01/XPD-00: on Span 1 |
| 200 |
XBUS-01/XPD-01: on Span 2 |
| 201 |
XBUS-00 usb:0000157 usb-0000:00:10.4-4 |
| 202 |
XBUS-00/XPD-00: on Span 3 |
| 203 |
XBUS-00/XPD-01: on Span 4 |
| 204 |
XBUS-00/XPD-02: on Span 5 |
| 205 |
XBUS-00/XPD-03: on Span 6 |
| 206 |
XBUS-00/XPD-04: on Span 7 |
| 207 |
XBUS-00/XPD-05: on Span 8 |
| 208 |
XBUS-00/XPD-06: on Span 9 |
| 209 |
XBUS-00/XPD-07: on Span 10 |
| 210 |
XBUS-02 usb-0000:00:10.4-1 |
| 211 |
XBUS-02/XPD-00: on Span 11 |
| 212 |
XBUS-02/XPD-10: on Span 12 |
| 213 |
# Sorted: type |
| 214 |
|
| 215 |
=head1 FILES |
| 216 |
|
| 217 |
=over |
| 218 |
|
| 219 |
=item /proc/xpp/XBUS-nn/XPD-mm/dahdi_registration |
| 220 |
|
| 221 |
Reading from this file shows if if the if the specific XPD is |
| 222 |
registered. Writing to it 0 or 1 registers / unregisters the device. |
| 223 |
|
| 224 |
This should allow you to register / unregister a specific XPD rather |
| 225 |
than all of them. |
| 226 |
|
| 227 |
=back |
| 228 |
|
| 229 |
=head1 NOTES |
| 230 |
|
| 231 |
dahdi_registration is intended to be used when the kernel module |
| 232 |
parameter B<xpp.dahdi_autoreg> is false (and implicitly: when the module |
| 233 |
parameter B<dahdi.auto_assign_span> is true), that is, Astribank devices |
| 234 |
as detected by XPP (xbus / xpd) do not register automatically with the |
| 235 |
DAHDI core. This tool is used to register tem in an explicit order. It |
| 236 |
works well, but only if you can arange for all of the Astribanks of the |
| 237 |
system to be available (and not already registered) at a specific point |
| 238 |
in time. |
| 239 |
|
| 240 |
Newer versions of DAHDI added support for registering a span to a |
| 241 |
specific span/channelss numbers specification. This allows registering |
| 242 |
them out of order. To use this capability, the module parameter |
| 243 |
B<dahdi.auto_assign_span> should be unset (set to 0) and thus spans of |
| 244 |
detected DAHDI devices could be registered using C<dahdi_span_assignments> |
| 245 |
(which may also be run automatically from a udev hook). |
| 246 |
|
| 247 |
In this case there is no point in delaying XPP device registration with |
| 248 |
dahdi and the parameter B<xpp.dahdi_autoreg> should be set. |
| 249 |
dahdi_registration will simply become a no-op. |
| 250 |
|
| 251 |
=head1 SEE ALSO |
| 252 |
|
| 253 |
B<dahdi_cfg>(8), B<dahdi_span_assignments>(8). |
| 254 |
|