[dahdi-commits] dahdi/tools.git branch "master" updated.

SVN commits to the DAHDI project dahdi-commits at lists.digium.com
Sun Oct 13 15:31:17 CDT 2013


branch "master" has been updated
       via  5e30a1401f046f651aea221bc9e4852bf942eddf (commit)
       via  2f688f83c4b4be17f1cc160a69e1aa0780fe415b (commit)
       via  680f3e1d1d827ae3e3aad9e501ad30b7c15c1a00 (commit)
       via  60fca920bcfda7843fb6f1339fed856aee568e1e (commit)
       via  c1e016fa3389cc04ae7f4a1ed9329e0d6c7435ca (commit)
       via  dc5c7de9fcd3080fd8ae0a455d22368f4af81f9f (commit)
       via  1e81ed14cf3691547fcfa276bf0cf321fa1771fa (commit)
       via  44a5285454166ad798eccf67f120d545a394b2ea (commit)
       via  7a1e2223094faeb5c44b7ee499fe97fefab409f0 (commit)
       via  52488d66fa209fef71ae58fb6185e2d855769b04 (commit)
       via  00af777a979de1b50f09461bd5645d199682f8ad (commit)
      from  025985d9b7b31a117651b9a29a84043014e9d26b (commit)

Summary of changes:
 Makefile                                         |    5 +-
 dahdi.rules                                      |    1 +
 dahdi_cfg.c                                      |    5 +-
 dahdi_cfg_device_args                            |   32 ----
 dahdi_map                                        |   39 ----
 doc/dahdi_cfg.8                                  |    2 +
 doc/dahdi_map.8                                  |   48 -----
 doc/span_assignments.8                           |  222 ++++++++++++++++++++++
 doc/span_types.8                                 |  146 ++++++++++++++
 handle_device                                    |   48 +++--
 span_assignments                                 |  212 +++++++++++++++++----
 span_config                                      |   84 ++++++++
 span_types                                       |   65 +++++--
 xpp/dahdi_genconf                                |    2 +-
 xpp/dahdi_registration                           |   80 +++++++-
 xpp/perl_modules/Dahdi/Config/Gen/Pinnedspans.pm |   57 ++++++
 xpp/perl_modules/Dahdi/Config/Gen/Spantypes.pm   |   57 ++++++
 17 files changed, 907 insertions(+), 198 deletions(-)
 delete mode 100755 dahdi_cfg_device_args
 delete mode 100755 dahdi_map
 delete mode 100644 doc/dahdi_map.8
 create mode 100644 doc/span_assignments.8
 create mode 100644 doc/span_types.8
 create mode 100755 span_config
 create mode 100644 xpp/perl_modules/Dahdi/Config/Gen/Pinnedspans.pm
 create mode 100644 xpp/perl_modules/Dahdi/Config/Gen/Spantypes.pm


- Log -----------------------------------------------------------------
commit 5e30a1401f046f651aea221bc9e4852bf942eddf
Author: Tzafrir Cohen <tzafrir.cohen at xorcom.com>
Date:   Sun Oct 13 23:28:29 2013 +0300

    Man pages for span_types and span_assignments
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/doc/span_assignments.8 b/doc/span_assignments.8
new file mode 100644
index 0000000..d517a74
--- /dev/null
+++ b/doc/span_assignments.8
@@ -0,0 +1,222 @@
+.TH "SPAN_ASSIGNMENTS" "8" "13 Oct 2013" "" ""
+
+.SH NAME
+span_assignments \- handle DAHDI spans registration
+.SH SYNOPSIS
+
+.B span_assignments [\-v|\-\-verbose] [\-n|\-\-dry\-run] <add|remove> \fIdevpath
+\fB[\fIdevpath \fB...]
+
+.B span_assignments [\-v|\-\-verbose] [\-n|\-\-dry\-run] auto
+
+.B span_assignments [\-v|\-\-verbose] list
+
+.B span_assignments [\-v|\-\-verbose] [\-k|\-\-key \fIkey\fB] dumpconfig
+
+.B span_assignments \-h|\-\-help
+
+.SH DESCRIPTION
+When the kernel module parameter \fBdahdi.auto_assign_span\fR is unset,
+DAHDI devices (such as DAHDI PCI cards) that register with DAHDI don't
+register their spans (e.g.: each digital port is normally a span) with
+DAHDI. This allows user-space to order DAHDI to register them to specific 
+span and channel numbers. This allows registering different spans out of
+order.
+
+.B span_assignments
+is used to register those spans or to help creating the configuration
+file used in their registration:
+.B /etc/dahdi/pinned-spans.conf .
+
+.SH OPTIONS
+
+There are several sub-commands:
+
+.B add \fIdevpath \fB[\fIdevpath \fB...]
+.RS
+Parameters are paths (in SysFS) to DAHDI devices with unregistered
+spans. The command will register with DAHDI according to according to
+configuration in \fBpinned-spans.conf\fR.
+.RE
+
+.B remove \fIdevpath \fB[\fIdevpath \fB...]
+.RS
+Parameters are paths (in SysFS) to DAHDI devices with registered
+spans. The command will unregister with DAHDI.
+.RE
+
+.B auto
+.RS
+Register all non-registered spans. Each span registers to first
+available span number and channel numbers, as if
+\fBdahdi.auto_assign_span\fR was set.
+.RE
+
+.B list
+.RS
+List all spans in the system.
+.RE
+
+.B dumpconfig
+.RS
+List all registered spans in the system in a format fit to be used in
+\fBpinned-spans.conf\fR. Use this to generate a configuration file after
+you have (perhaps manually) registered all existing spans.
+
+.B dahdi_genconf pinnedspans
+uses this command internally.
+.RE
+
+.B \-v \-\-verbose
+.RS
+Verbose output.
+.RE
+
+.B \-n \-\-dry\-run
+.RS
+Don't register / unregister spans. Only print commands used to do so.
+.RE
+
+.B \-k \fIkey
+.RS
+For \fBdumpconfig\fR \- The key by which to identify the hardware in the
+generated configuration. Legal values:
+
+.B hwid
+.RS
+Hardware identifier (e.g.: software-readable serial number). This is the
+default. If the device has no hwid, devpath is used.
+.RE
+
+.B location
+.RS
+The location field (file) in the SysFS device node (directory) for the
+DAHDI device. If not available (typically: DAHDI version <= 2.7.x),
+devpath is used.
+.RE
+
+.B devpath
+.RS
+Path in SysFS to the device node.
+.RE
+.RE
+
+.SH CONFIGURATOIN
+.B pinned-spans.conf
+is a file with lines specifying registration of spans.
+
+Empty lines or lines beginning with '#' are ignored.
+
+Each line is in the format of:
+
+.I ID		spanspec ...
+
+The \fIID\fR field specifies the DAHDI device and the \fIspanspecs\fR
+define how to register its spans. A line may have multiple
+\fIspanspecs\fR in a single line (though dumpconfig generates a
+configuration with one per line).
+
+.SS Span Identifier
+A DAHDI device may be specified either by a hardware identifier (a
+software readable serial number or whatever) or the location in which
+it is installed on the system. The former makes it simpler to change
+connector / slot whereas the latter makes it simpler to replace a unit.
+
+The value in this field is matched (when the commands \fBadd\fR and
+\fBremove\fR) are used) to the following values:
+
+ \fIhwid\fR
+ \fB@\fIlocation\fR
+ \fIdevpath\fR
+
+See above for their descriptions. The value may include shell wildcards:
+*, ? and [], which are used in the match. The values to be matched are
+first cleaned up: '!' is replaced with '/' and any character beyond
+"a-zA-Z0-9/:.-" is removed.
+
+.SS Span Specification
+
+Each line should have one or more span specifications: this is the value
+used to register a span with DAHDI in the SysFS interface. A
+specification has three colon-separated numbers:
+
+.I rel_span_no:span_no:first_chan
+
+for instance, the following are four span specifications for a quad-E1
+device: 1:6:53 2:7:84 3:8:115 4:9:146 occupying spans 6-9 and channels
+53-176.
+
+.B rel_span_no
+.RS
+The relative number of the span in the device. E.g.: port number.
+.RE
+
+.B span_no
+.RS
+The desired DAHDI span number. Must be available.
+.RE
+
+.B first_chan
+.RS
+The desired DAHDI channel number for the first DAHDI channel in the span.
+All channels of the span will be registered following it and hence that
+space must be available.
+.RE
+
+
+.SH ENVIRONMENT
+
+.B DAHDICONFDIR
+.RS
+The directory in which pinned-spans.conf resides. /etc/dahdi if not
+overridden from the environment.
+.RE
+
+.B pinned_spans_conf
+.RS
+The path to pinned-spans.conf resides. /etc/dahdi/pinned-spans.conf if
+not overridden from the environment.
+.RE
+
+.B SPAN_ASSIGNMENTS_KEY
+.RS
+The default value for \-k . Defaults to "hwid" if not overridden from the
+environment.
+.RE
+
+
+.SH FILES
+
+.B /etc/dahdi/pinned-spans.conf
+.RS
+The default location for the configuration file.
+.RE
+
+.B /sys/bus/dahdi_devices/devices/\fIdevice\fR
+.RS
+SysFS node for the device. In this directory reside the following
+files, among others:
+
+.B location
+.RS
+The value of the device's location field.
+.RE
+
+.B assign_span, unassign_span, auto_assign
+.RS
+Write only files for the operations. Used by \fBadd\fR, \fBremove\fR and
+\fBauto\fR, respectively.
+.RE
+
+.RE
+
+.SH SEE ALSO
+span_types(8), dahdi_genconf(8), dahdi_cfg(8)
+
+.SH AUTHOR
+span_assignments was written by Oron Peled.  This manual page was
+written by Tzafrir Cohen. Permission is granted to copy, distribute
+and/or modify this document under the terms of the GNU General Public
+License, Version 2 any  later version published by the Free Software
+Foundation.
+
diff --git a/doc/span_types.8 b/doc/span_types.8
new file mode 100644
index 0000000..4c7dd25
--- /dev/null
+++ b/doc/span_types.8
@@ -0,0 +1,146 @@
+.TH "SPAN_ASSIGNMENTS" "8" "13 Oct 2013" "" ""
+
+.SH NAME
+span_assignments \- set DAHDI spans properties before registration (E1/T1)
+.SH SYNOPSIS
+
+.B span_assignments <list|dumpconfig|set> [\fIdevpath \fB[\fIdevpath \fB...]]
+
+.SH DESCRIPTION
+The span type (E1/T1/J1) must be set to a span before registering it
+with DAHDI, as E1 spans use more channels. \fBspan_types\fR applies the
+span type configuration to an unregistered span.
+
+Using it only makes sense when the kernel module parameter
+\fBdahdi.auto_assign_span\fR is unset, otherwise the DAHDI spans register
+automatically.
+
+.SH OPTIONS
+.B span_types
+takes a command and an optional list of devices. If no device is given,
+the command is applied to all devices.
+
+The device is marked as a path in the SysFS tree.
+
+.B set
+.RS
+Reads settings from \fBspan-types.conf\fR and applies them to the
+device(s) specified in the command line (or all devices, if none
+specified).
+.RE
+
+.B list
+.RS
+List types for all spans in the system which may be set with span_types
+(E1/T1/J1 spans).
+.RE
+
+.B dumpconfig
+.RS
+List types for the spans in a format fit to be used in
+\fBspan-types.conf\fR. Use this to generate a configuration file after
+you have (perhaps manually) set all existing spans.
+
+.B dahdi_genconf spantypes
+uses this command internally.
+.RE
+
+.SH CONFIGURATION
+.B span-types.conf
+is a file with lines specifying registration of spans.
+
+Empty lines or lines beginning with '#' are ignored.
+
+Each line is in the format of:
+
+.I ID		spanspec ...
+
+The \fIID\fR field specifies the DAHDI device and the \fIspanspecs\fR
+define how to register its spans. A line may have multiple
+\fIspanspecs\fR in a single line (though dumpconfig generates a
+configuration with one per line).
+
+.SS Span Identifier
+A DAHDI device may be specified either by a hardware identifier (a
+software readable serial number or whatever) or the location in which
+it is installed on the system. The former makes it simpler to change
+connector / slot whereas the latter makes it simpler to replace a unit.
+
+The value in this field is matched (when the commands \fBadd\fR and
+\fBremove\fR) are used) to the following values:
+
+ \fIhwid\fR
+ \fB@\fIlocation\fR
+ \fIdevpath\fR
+
+See above for their descriptions. The value may include shell wildcards:
+*, ? and [], which are used in the match. The values to be matched are
+first cleaned up: '!' is replaced with '/' and any character beyond
+"a-zA-Z0-9/:.-" is removed.
+
+.SS Span Specification
+
+Each line should have one or more span specifications: this is the value
+used to register a span with DAHDI in the SysFS interface. A
+specification has three colon-separated numbers:
+
+.I rel_span_no:span_type
+
+for instance, the following are four span specifications specify ports 1 and 2 as E1 and ports 3 and 4 as T1: [12]:E1 [34]:T1 .
+
+.B rel_span_no
+.RS
+The relative number of the span in the device. E.g.: port number.
+.RE
+
+.B span_type
+.RS
+E1/T1/J1
+.RE
+
+
+.SH ENVIRONMENT
+
+.B DAHDICONFDIR
+.RS
+The directory in which span-types.conf resides. /etc/dahdi if not
+overridden from the environment.
+.RE
+
+.B span_types_conf
+.RS
+The path to span-types.conf resides. /etc/dahdi/span-types.conf if
+not overridden from the environment.
+.RE
+
+
+.SH FILES
+
+.B /etc/dahdi/span-types.conf
+.RS
+The default location for the configuration file.
+.RE
+
+.B /sys/bus/dahdi_devices/devices/\fIdevice\fR
+.RS
+SysFS node for the device. In this directory reside the following
+files, among others:
+
+.B spantype
+.RS
+read/write file. Reading from it returns current configuration for spans
+of the device. Span-specifications can be written to it to change types
+(but only for a span that is not registered).
+.RE
+
+
+.SH SEE ALSO
+span_assignments(8), dahdi_genconf(8), dahdi_cfg(8)
+
+.SH AUTHOR
+span_assignments was written by Oron Peled.  This manual page was
+written by Tzafrir Cohen. Permission is granted to copy, distribute
+and/or modify this document under the terms of the GNU General Public
+License, Version 2 any  later version published by the Free Software
+Foundation.
+
diff --git a/xpp/dahdi_registration b/xpp/dahdi_registration
index cdd715d..eb1579a 100755
--- a/xpp/dahdi_registration
+++ b/xpp/dahdi_registration
@@ -134,9 +134,10 @@ Span registration should generally always succeed. Span unregistration may
 fail if channels from the span are in use by e.g. asterisk. In such a case
 you'll also see those channels as '(In use)' in the output of lsdahdi(8).
 
-dahdi_registration is intended to be used when the xpp module parameter
-B<dahdi_autoreg> is false (and implicitly: when the dahdi module parameter
-B<auto_assign_span> is true.
+dahdi_registration is intended to be used when the kernel module parameter
+B<xpp.dahdi_autoreg> is false (and implicitly: when the module parameter
+B<dahdi.auto_assign_span> is true). See also the NOTES section regarding
+C<span_assignments>.
 
 If dahdi_autoreg is true, the program will normally do nothing.
 
@@ -209,3 +210,30 @@ This should allow you to register / unregister a specific XPD rather
 than all of them. 
 
 =back
+
+=head1 NOTES
+
+dahdi_registration is intended to be used when the kernel module
+parameter B<xpp.dahdi_autoreg> is false (and implicitly: when the module
+parameter B<dahdi.auto_assign_span> is true), that is, Astribank devices
+as detected by XPP (xbus / xpd) do not register automatically with the
+DAHDI core. This tool is used to register tem in an explicit order. It
+works well, but only if you can arange for all of the Astribanks of the
+system to be available (and not already registered) at a specific point
+in time.
+
+Newer versions of DAHDI added support for registering a span to a
+specific span/channelss numbers specification. This allows registering
+them out of order. To use this capability, the module parameter
+B<dahdi.auto_assign_span> should be unset (set to 0) and thus spans of
+detected DAHDI devices could be registered using C<span_assignments>
+(which may also be run automatically from a udev hook).
+
+In this case there is no point in delaying XPP device registration with
+dahdi and the parameter B<xpp.dahdi_autoreg> should be set.
+dahdi_registration will simply become a no-op.
+
+=head1 SEE ALSO
+
+B<dahdi_cfg>(8), B<span_assignments>(8).
+

commit 2f688f83c4b4be17f1cc160a69e1aa0780fe415b
Author: Tzafrir Cohen <tzafrir.cohen at xorcom.com>
Date:   Wed Oct 2 21:54:58 2013 +0300

    Remove dahdi_map, obsoleted by span_assignments
    
    dahdi_map can be replaced by span_assignments list / dumpconfig
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/Makefile b/Makefile
index 9bfa294..b24f6ef 100644
--- a/Makefile
+++ b/Makefile
@@ -105,7 +105,7 @@ endif
 MAN_PAGES:=$(wildcard $(BINS:%=doc/%.8))
 
 PINNED_DATA_SCRIPTS:=handle_device span_config
-PINNED_UTILS:=dahdi_map span_assignments span_types
+PINNED_UTILS:=span_assignments span_types
 PINNED_CONF:=pinned-spans.conf spantype.conf
 
 TEST_BINS:=patgen pattest patlooptest hdlcstress hdlctest hdlcgen hdlcverify timertest dahdi_maint
diff --git a/dahdi_map b/dahdi_map
deleted file mode 100755
index a95ebae..0000000
--- a/dahdi_map
+++ /dev/null
@@ -1,39 +0,0 @@
-#! /bin/sh
-#
-# Show a map of dahdi devices with the following fields:
-#  - spanno (or '-' if not assigned yet)
-#  - (vendor assigned) name
-#  - local spanno
-#  - hardware_id (or empty if none)
-#  - location (prefixed by '@')
-
-devbase="/sys/bus/dahdi_devices/devices"
-
-[ -d "$devbase" ] || {
-	echo >&2 "$0: Missing '$devbase' (Old driver?)"
-	exit 1
-}
-
-fmt="%-4s %-17s %-3s     %-12s %s\n"
-
-printf "$fmt" 'SPAN' 'NAME' '#' 'HARDWARE_ID' 'LOCATION'
-
-DEVICES=`echo $devbase/*`
-
-for device in $DEVICES
-do
-	hw_id=`cat "$device/hardware_id"`
-	location=`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
-	for local_spanno in `cut -d: -f1 "$device/spantype"`
-	do
-		span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \
-			sed -e 's,/local_spanno:.*,,' -e 's,.*/,,'`
-		if [ "$span" != '' ]; then
-			spanno=`echo $span | sed 's/^.*-//'`
-			name=`cat 2>/dev/null "$device/$span/name"`
-		else
-			spanno='-'
-		fi
-		printf "$fmt" "$spanno" "$name" "($local_spanno)" "[$hw_id]" "@$location"
-	done | sort -n
-done
diff --git a/doc/dahdi_map.8 b/doc/dahdi_map.8
deleted file mode 100644
index 80d5d8f..0000000
--- a/doc/dahdi_map.8
+++ /dev/null
@@ -1,48 +0,0 @@
-.TH dahdi_test 8 "2013-05-24" 
-.SH "NAME" 
-dahdi_map \(em List hardware IDs and locations of DAHDI spans
-.SH "SYNOPSIS" 
-.B dahdi_map
-
-.SH DESCRIPTION 
-.B dahdi_map
-prints a list of hardware IDs and locations of any DAHDI span on the system
-(possibly not yet assigned).
-
-It takes no extra options or parameters.
-
-Example output:
-.EX
-SPAN NAME              #       HARDWARE_ID  LOCATION
-5    XBUS-00/XPD-00    (1)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-6    XBUS-00/XPD-01    (2)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-7    XBUS-00/XPD-02    (3)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-8    XBUS-00/XPD-03    (4)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-9    XBUS-00/XPD-04    (5)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-10   XBUS-00/XPD-05    (6)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-11   XBUS-00/XPD-06    (7)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-12   XBUS-00/XPD-07    (8)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-13   XBUS-00/XPD-10    (9)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-14   XBUS-00/XPD-20    (10)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-15   XBUS-00/XPD-30    (11)     [usb:INT06380] @pci0000:00/0000:00:10.4/usb1/1-3/xbus-00/astribanks:xbus-00
-16   XBUS-01/XPD-00    (1)     []           @pci0000:00/0000:00:10.4/usb1/1-4/xbus-01/astribanks:xbus-01
-1    XBUS-02/XPD-00    (1)     [usb:XILINX_4] @pci0000:00/0000:00:10.4/usb1/1-1/xbus-02/astribanks:xbus-02
-2    XBUS-02/XPD-10    (2)     [usb:XILINX_4] @pci0000:00/0000:00:10.4/usb1/1-1/xbus-02/astribanks:xbus-02
-3    XBUS-02/XPD-20    (3)     [usb:XILINX_4] @pci0000:00/0000:00:10.4/usb1/1-1/xbus-02/astribanks:xbus-02
-4    XBUS-02/XPD-30    (4)     [usb:XILINX_4] @pci0000:00/0000:00:10.4/usb1/1-1/xbus-02/astribanks:xbus-02
-.EE
-
-.SH FILES
-.B /sys/bus/dahdi_devices/devices
-.RS
-Information taken from that area in SysFS.
-.RE
-
-.SH SEE ALSO 
-dahdi_cfg(8)
-
-.SH AUTHOR 
-This manual page was written by Tzafrir Cohen <tzafrir.cohen at xorcom.com> 
-Permission is granted to copy, distribute and/or modify this document under 
-the terms of the GNU General Public License, Version 2 any  
-later version published by the Free Software Foundation. 

commit 680f3e1d1d827ae3e3aad9e501ad30b7c15c1a00
Author: Oron Peled <oron.peled at xorcom.com>
Date:   Sun Sep 29 18:24:08 2013 +0200

    dahdi_registration: adapt to pinned-spans
    
    * If xpp.dahdi_autoreg parameter is 'Y' -- Skip actuall registration.
    * If dahdi.auto_assign_spans is '0' and there's no /etc/dahdi/pinned-spans.conf
      Than use 'span_assignments auto ...' to assign device spans.
    * Since dahdi_registration iterate in correct xpp_order, the span
      assignment logic provides migration path for users who did not
      generate their pinned-spans.conf configuration yet.
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/xpp/dahdi_registration b/xpp/dahdi_registration
index 8b73aea..cdd715d 100755
--- a/xpp/dahdi_registration
+++ b/xpp/dahdi_registration
@@ -19,11 +19,27 @@ use Dahdi::Xpp::Xpd;
 use Getopt::Std;
 
 sub usage {
-	die "Usage: $0 [-s sort_order] [on|off|1|0]\n";
+	die "Usage: $0 [-v] [-R] [-s sort_order] [on|off|1|0]\n";
+}
+
+sub check_param {
+	my $param = shift || die;
+	open(F, $param) || return '';
+	my $val = <F>;
+	close F;
+	chomp $val;
+	return $val;
 }
 
 my %opts;
-getopts('s:', \%opts) || usage;
+getopts('vRs:', \%opts) || usage;
+
+my $dahdi_autoreg = check_param('/sys/module/xpp/parameters/dahdi_autoreg') eq 'Y';
+my $auto_assign_spans = check_param('/sys/module/dahdi/parameters/auto_assign_spans') eq '1';
+my $pinned_spans_config = $ENV{'PINNED_SPANS_CONF_FILE'} || '/etc/dahdi/pinned-spans.conf';
+my $span_types_config = $ENV{'SPAN_TYPES_CONF_FILE'} || '/etc/dahdi/span-types.conf';
+my $have_pinned_spans_config = -f $pinned_spans_config || 0;
+my $have_span_types_config = -f $span_types_config || 0;
 
 my $sorter;
 my $sort_order = $opts{'s'};
@@ -41,9 +57,11 @@ if(defined $sort_order) {
 
 @ARGV == 0 or @ARGV == 1 or usage;
 my $on = shift;
-my $verbose = 0;
+my $verbose = $opts{'v'};
 my $should_output = 1;
 
+#print "dahdi_autoreg=$dahdi_autoreg auto_assign_spans=$auto_assign_spans have_pinned_spans_config='$have_pinned_spans_config' have_span_types_config='$have_span_types_config'\n";
+
 if(defined($on)) {	# Translate to booleans
 	$on = uc($on);
 	$on =~ /^(ON|OFF|1|0)$/ or usage;
@@ -51,6 +69,8 @@ if(defined($on)) {	# Translate to booleans
 	$should_output = 0 unless $verbose;
 }
 
+undef $on if $dahdi_autoreg and not $opts{'R'};
+
 sub state2str($) {
 	return (shift)?"on":"off";
 }
@@ -80,6 +100,15 @@ foreach my $xbus (Dahdi::Xpp::xbuses($sorter)) {
 		}
 		myprintf "%3s ==> %3s\n", state2str($prev), state2str($on);
 	}
+	if (defined($on) && $on && ! $have_pinned_spans_config && ! $auto_assign_spans) {
+		# Emulate /etc/dahdi/pinned-spans.conf:
+		#   - We iterate over $xbus according to /etc/dahdi/xpp_order
+		#   - We "auto" assign all spans of current $xbus
+		my $devpath = sprintf "/sys/bus/dahdi_devices/devices/astribanks:xbus-%02d", $xbus->num;
+		my @cmd = ('span_assignments', 'auto', $devpath);
+		system @cmd;
+		warn "Failed '@cmd' (status=$?)\n" if $?;
+	}
 }
 myprintf "# Sorted: $sort_order\n" if defined $sort_order;
 
@@ -91,7 +120,7 @@ dahdi_registration - Handle registration of Xorcom XPD modules in dahdi.
 
 =head1 SYNOPSIS
 
-dahdi_registration [-s sortorder] [on|off]
+dahdi_registration [-v] [-s sortorder] [-R] [on|off]
 
 =head1 DESCRIPTION
 
@@ -105,6 +134,12 @@ Span registration should generally always succeed. Span unregistration may
 fail if channels from the span are in use by e.g. asterisk. In such a case
 you'll also see those channels as '(In use)' in the output of lsdahdi(8).
 
+dahdi_registration is intended to be used when the xpp module parameter
+B<dahdi_autoreg> is false (and implicitly: when the dahdi module parameter
+B<auto_assign_span> is true.
+
+If dahdi_autoreg is true, the program will normally do nothing.
+
 =head2 Parameters
 
 off -- deregisters all XPD's from dahdi.
@@ -115,6 +150,15 @@ on -- registers all XPD's to dahdi.
 
 =over
 
+=item -v
+
+verbose output.
+
+=item -R
+
+Force operations (on/off) even if the module parameter B<dahdi_autoreg>
+for xpp is enabled (which makes this program unneeded).
+
 =item -s I<sort_order>
 
 The sort order to use. 

commit 60fca920bcfda7843fb6f1339fed856aee568e1e
Author: Oron Peled <oron.peled at xorcom.com>
Date:   Sun Sep 29 10:09:56 2013 +0200

    span_assignments: -k / --keys and more
    
    * New functionality (documented in the script header):
      - Alternative "keys" for device matching
      - Added new command line options: --help, --dry-run, --verbose, --key
    
    * Clean sysfs attribute contents from special characters in every use-case.
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/span_assignments b/span_assignments
index 8f29d39..96e49cd 100755
--- a/span_assignments
+++ b/span_assignments
@@ -22,28 +22,116 @@
 # Without further arguments, it operates on all existing spans
 # With one or more sysfs dahdi_devices it is limited to those.
 #
+# We may use alternative "keys" for device matching:
+# * Available keys:
+#   - "hwid"       - Hardware id attribute from sysfs
+#   - "@location"  - Location attribute from sysfs (embeded inside '<>')
+#   - "/devpath"   - The sysfs absolute devpath
+#
+# * During "dumpconfig", for each device we take the first available key:
+#   - The preference is: "hwid" or else "@location" or else "/devpath"
+#   - This can be overriden via the SPAN_ASSIGNMENTS_KEY environment variable
+#     or the '{-k|--key} key' command line option.
+#
+# * During "add":
+#   - Any key match is valid (hwid/location/devpath)
+#   - Shell globs (wildcards: '*', '?', '[...]') may be optionally used.
+#
+# Command line options:
+#  - The '-h|--help' show a usage message.
+#  - The '-n|--dry-run' affects the "add" and "remove" operations.
+#  - The '-v|--verbose' currently shows device matches during "add" operation.
+#  - The '-k <key>|--key <key>' overrides the SPAN_ASSIGNMENTS_KEY environment
+#    variable.
+#
 # Examples:
 #    span_assignments list
-#    span_assignments add	# all
-#    span_assignments add /sys/bus/dahdi_devices/devices/astribanks:xbus-00
-#    span_assignments remove	# all
+#    span_assignments add	# all unassigned devices
+#    span_assignments add       /sys/bus/dahdi_devices/devices/astribanks:xbus-00
+#    span_assignments remove	# all assigned devices
+#    span_assignments -k location dumpconfig
 #
 
 devbase='/sys/bus/dahdi_devices/devices'
 DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
 pinned_spans_conf="$DAHDICONFDIR/pinned-spans.conf"
+SPAN_ASSIGNMENTS_KEY=${SPAN_ASSIGNMENTS_KEY:-hwid}
+dry_run=
+verbose=
 
 usage() {
-	echo >&2 "Usage: $0 {auto|add|remove|list|dumpconfig} [devpath ...]"
+	echo >&2 "Usage: $0 [options] action [devpath ...]"
+	echo >&2 "       action:"
+	echo >&2 "         auto       - trigger driver auto_assign attribute for given devices"
+	echo >&2 "         add        - assign spans, according to /etc/dahdi/pinned-spans.conf"
+	echo >&2 "         remove     - unassign spans"
+	echo >&2 "         list       - human-readable list of all spans"
+	echo >&2 "         dumpconfig - dump current state as new configuration"
+	echo >&2 ""
+	echo >&2 "       options:"
+	echo >&2 "         -h|--help      - Show this help"
+	echo >&2 "         -n|--dry-run   - For 'add/remove' actions"
+	echo >&2 "         -v|--versbose  - Show matches during 'add' action"
+	echo >&2 "         -k|--key <k>   - Override prefered key during dumpconfig action"
 	exit 1
 }
 
+# Parse command line options
+TEMP=`getopt -o hnvk: --long help,dry-run,verbose,key: -n "$0" -- "$@"`
+if [ $? != 0 ]; then
+	echo >&2 "Bad options"
+	usage
+fi
+
+# Note the quotes around `$TEMP': they are essential!
+eval set -- "$TEMP"
+
+while true ; do
+	case "$1" in
+	-h|--help)
+		usage
+		;;
+	-n|--dry-run)
+		dry_run='true'
+		shift
+		;;
+	-v|--verbose)
+		verbose='true'
+		shift
+		;;
+	-k|--key)
+		SPAN_ASSIGNMENTS_KEY="$2"
+		shift
+		shift
+		;;
+	--)
+		shift
+		break
+		;;
+	*)
+		echo "Internal error!"
+		exit 1
+		;;
+	esac
+done
+
 if [ "$#" -eq 0 ]; then
+	echo >&2 "Missing action argument"
 	usage
 fi
 action="$1"
 shift
 
+# Validate SPAN_ASSIGNMENTS_KEY
+case "$SPAN_ASSIGNMENTS_KEY" in
+hwid|location|devpath)
+	;;
+*)
+	echo >&2 "Bad SPAN_ASSIGNMENTS_KEY='$SPAN_ASSIGNMENTS_KEY' (should be: hwid|location|devpath)"
+	usage
+	;;
+esac
+
 if [ ! -d "$devbase" ]; then
 	echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
 	exit 1
@@ -56,12 +144,18 @@ else
 	DEVICES=`echo $devbase/*`
 fi
 
+# Beware of special characters in attributes
+attr_clean() {
+	cat "$1" 2>/dev/null | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
+}
+
 show_devices() {
 
 	for device in $DEVICES
 	do
-		hw_id=`cat "$device/hardware_id"`
-		location=`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
+		devpath=`cd "$device" && pwd -P`
+		location='@'`attr_clean "$device/location"`
+		hardware_id=`attr_clean "$device/hardware_id"`
 		for local_spanno in `cut -d: -f1 "$device/spantype"`
 		do
 			span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \
@@ -74,7 +168,7 @@ show_devices() {
 				spanno='-'
 				basechan='-'
 			fi
-			printf "%-8s %-14s %s\n" "$local_spanno:$spanno:$basechan" "[$hw_id]" "@$location"
+			printf "%-8s %-14s %s %s\n" "$local_spanno:$spanno:$basechan" "[$hardware_id]" "$location" "$devpath"
 		done | sort -n
 	done
 }
@@ -86,14 +180,17 @@ dump_config() {
 	echo ''
 	for device in $DEVICES
 	do
-		hw_id=`cat "$device/hardware_id"`
-		location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
-		if [ "$hw_id" != '' ]; then
-			id="$hw_id"
+		devpath=`cd "$device" && pwd -P`
+		location=`attr_clean "$device/location"`
+		hardware_id=`attr_clean "$device/hardware_id"`
+		if [ "$SPAN_ASSIGNMENTS_KEY" = 'hwid' -a "$hardware_id" != '' ]; then
+			id="$hardware_id"
+		elif [ "$SPAN_ASSIGNMENTS_KEY" = 'location' -a "$location" != '' ]; then
+			id="@$location"
 		else
-			id="$location"
+			id="$devpath"
 		fi
-		echo "# Device: [$hw_id] $location"
+		echo "# Device: [$hardware_id] @$location $devpath"
 		for local_spanno in `cut -d: -f1 "$device/spantype"`
 		do
 			span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \
@@ -117,6 +214,10 @@ unassign_all_spans() {
 		find "$device" -follow -maxdepth 1 -name 'span-*' -type d | \
 			sort | while read spandir; do
 			local_spanno=`cat "$spandir/local_spanno"`
+			if [ "$dry_run" = true ]; then
+				echo "(dry-run) unassign $device $local_spanno"
+				continue
+			fi
 			echo "unassign $device $local_spanno"
 			if ! echo "$local_spanno" > "$device/unassign_span"; then
 				echo >&2 "$0: failed unassigning '$local_spanno' in '$device'"
@@ -130,11 +231,6 @@ filter_conf() {
 	sed -e 's/#.*//' -e '/^[ \t]*$/d' "$pinned_spans_conf"
 }
 
-# Beware of special characters in attributes
-attr_clean() {
-	cat "$1" | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
-}
-
 assign_device_spans() {
 	device="$1"
 	for s in $spanspecs
@@ -142,12 +238,16 @@ assign_device_spans() {
 		local_spanno=`echo "$s" | cut -d: -f1`
 		spanno=`echo "$s" | cut -d: -f2`
 		span="$device/span-$spanno"
+		if [ "$dry_run" = true ]; then
+			echo "(dry-run) assign $device: $s"
+			continue
+		fi
 		if [ -d "$span" ]; then
 			span_local_spanno=`cat "$span/local_spanno"`
 			if [ "$span_local_spanno" != "$local_spanno" ]; then
 				echo "WARNING: $span_local_spanno != $local_spanno"
 			fi
-			echo "$device [$local_spanno] already assigned to $spanno. Skipping..."
+			echo "$device [$local_spanno] already assigned to span $spanno. Skipping..."
 			continue
 		fi
 		echo "assign $device: $s"
@@ -159,21 +259,29 @@ assign_device_spans() {
 
 match_device() {
 	device="$1"
-	location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
+	devpath=`cd "$device" && pwd -P`
+	location='@'`attr_clean "$device/location"`
 	hardware_id=`attr_clean "$device/hardware_id"`
 	filter_conf | while read id spanspecs
 	do
 		# We use case to enable shell-style globbing in configuration
+		case "$hardware_id" in
+		$id)
+			[ "$verbose" = true ] && echo "match by hwid ($id ~ $hardware_id): $spanspecs"
+			assign_device_spans "$device"
+			;;
+		esac
+		# We use case to enable shell-style globbing in configuration
 		case "$location" in
 		$id)
-			#echo "match location($id ~ $location): $spanspecs"
+			[ "$verbose" = true ] && echo "match by location ($id ~ $location): $spanspecs"
 			assign_device_spans "$device"
 			;;
 		esac
 		# We use case to enable shell-style globbing in configuration
-		case "$hardware_id" in
+		case "$devpath" in
 		$id)
-			#echo "match hardware_id([$id] ~ $hardware_id): $spanspecs"
+			[ "$verbose" = true ] && echo "match by devpath ($id ~ $devpath): $spanspecs"
 			assign_device_spans "$device"
 			;;
 		esac
@@ -196,7 +304,9 @@ auto_assign_devices() {
 	for device in $DEVICES
 	do
 		echo "auto-assign $device"
-		echo 1 > "$device/auto_assign"
+		if [ "$dry_run" != true ]; then
+			echo 1 > "$device/auto_assign"
+		fi
 	done
 }
 
@@ -217,6 +327,7 @@ dumpconfig)
 	dump_config
 	;;
 *)
+	echo >&2 "Bad action='$action'"
 	usage
 	;;
 esac

commit c1e016fa3389cc04ae7f4a1ed9329e0d6c7435ca
Author: Tzafrir Cohen <tzafrir.cohen at xorcom.com>
Date:   Tue Oct 1 22:13:43 2013 +0300

    Do the Right Thing when there's no config
    
    Do the "right thing" (hopefully. At least for a system with a single
    device) if there is are no configuration files:
    
    * No span-types.conf: just ignore it as before. It is optional.
    * No pinned-spans.conf: use span_assignments auto (same as having
      dahdi.auto_assign_spans=1).
    * No system.conf: generate a temporary one with dahdi_genconf.
    
    This will hopefully allow having a partially-working system, and help
    making ut usable with 'span_assignments dumpconfig'. Or maybe just work
    as-is.
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/handle_device b/handle_device
index a25c921..4a95b7b 100755
--- a/handle_device
+++ b/handle_device
@@ -54,6 +54,9 @@ add)
 		fi
 		if [ -r "$DAHDICONFDIR/pinned-spans.conf" ]; then
 			span_assignments add "/sys$DEVPATH"
+		else
+			# No configuration. No order guaranteed
+			span_assignments auto
 		fi
 	) 2>&1 < /dev/null | $LOGGER &
 	;;
diff --git a/span_config b/span_config
index 8500d46..64a6ede 100755
--- a/span_config
+++ b/span_config
@@ -32,22 +32,36 @@ DAHDICONFDIR='/etc/dahdi'
 export DAHDICONFDIR
 
 run_dahdi_cfg() {
+	echo "dahdi_cfg: span $spanno <$basechan-$endchan>"
+	dahdi_cfg -c "$cfg_file" -S "$spanno" -C "$basechan-$endchan"
+}
+
+configure_span() {
 	span_devpath="$1"
 	# Sanity check
 	checkit=`"dahdi_cfg" --help 2>&1 | grep -- '-S' | wc -l`
 	if [ "$checkit" != 1 ]; then
-		$LOGGER "Bad dahdi_cfg (no -S support). Skipping"
+		echo "Bad dahdi_cfg (no -S support). Skipping"
 		exit 0
 	fi
+
+	# Set variables
 	spanno=`echo "$span_devpath" | sed 's,.*/span-,,'`
 	basechan=`cat "$span_devpath/basechan"`
 	channels=`cat "$span_devpath/channels"`
 	endchan=`expr "$basechan" + "$channels" - 1`
-	echo "dahdi_cfg: span $spanno <$basechan-$endchan>"
-	dahdi_cfg \
-		-c "$DAHDICONFDIR/system.conf" \
-		-S "$spanno" \
-		-C "$basechan-$endchan"
+
+	# Configure DAHDI
+	cfg_file="$DAHDICONFDIR/system.conf"
+	if [ -r "$cfg_file" ]; then
+		run_dahdi_cfg
+	else
+		echo "Using auto-generated config for dahdi_cfg"
+		cfg_file='-'
+		DAHDI_CONF_FILE="$cfg_file" dahdi_genconf system | run_dahdi_cfg
+	fi
+
+	# Add to asterisk
 	asterisk -rx "dahdi create channels $basechan $endchan"
 }
 
@@ -57,9 +71,9 @@ add)
 	# Can have alternate dahdi configuration directory for debugging
 	# export DAHDICONFDIR="/tmp/xortel/dahdi"
 
-	run_dahdi_cfg "/sys$DEVPATH" 2>&1 | $LOGGER
+	configure_span "/sys$DEVPATH" 2>&1 | $LOGGER
 	;;
-remove)
+remove|online|offline)
 	# Nothing to do yet...
 	echo "$ACTION: $DEVPATH" | $LOGGER
 	;;

commit dc5c7de9fcd3080fd8ae0a455d22368f4af81f9f
Author: Oron Peled <oron.peled at xorcom.com>
Date:   Sun Oct 6 16:10:48 2013 +0300

    dahdi_cfg: can optionally read config from stdin
    
    Allow for the convention of '-' marking stdin:
    
      dahdi_cfg -c -
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/dahdi_cfg.c b/dahdi_cfg.c
index 3dfaf42..349a0af 100644
--- a/dahdi_cfg.c
+++ b/dahdi_cfg.c
@@ -1500,7 +1500,10 @@ int main(int argc, char *argv[])
 		error("Unable to open master device '%s'\n", MASTER_DEVICE);
 		goto finish;
 	}
-	cf = fopen(filename, "r");
+	if (strcmp(filename, "-") == 0)
+		cf = fdopen(STDIN_FILENO, "r");
+	else
+		cf = fopen(filename, "r");
 	if (cf) {
 		while((buf = readline())) {
 			if (*buf == 10) /* skip new line */
diff --git a/doc/dahdi_cfg.8 b/doc/dahdi_cfg.8
index 5e1eaa2..a5d0595 100644
--- a/doc/dahdi_cfg.8
+++ b/doc/dahdi_cfg.8
@@ -24,6 +24,8 @@ the DAHDI init script.
 .RS
 Use an alternative configuration file instead of
 .I /etc/dahdi/system.conf
+
+If \fICFG_FILE\fR is '\fB\-\fR', it is read from stdin.
 .RE
 
 .B \-C \fICHANNELS

commit 1e81ed14cf3691547fcfa276bf0cf321fa1771fa
Author: Oron Peled <oron.peled at xorcom.com>
Date:   Mon Sep 23 17:11:36 2013 +0200

    Augment dahdi_genconf default generators list
    
    Run Add spantypes and pinnedspans generators by default.
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/xpp/dahdi_genconf b/xpp/dahdi_genconf
index eb55948..bf1a3ca 100755
--- a/xpp/dahdi_genconf
+++ b/xpp/dahdi_genconf
@@ -54,7 +54,7 @@ sub generator_list($) {
 		}
 	} else {
 		# No files given. Use the defaults.
-		@genlist = ('system', 'chandahdi');
+		@genlist = ('spantypes', 'pinnedspans', 'system', 'chandahdi');
 		if($gconfig->{'pri_connection_type'} eq 'R2') {
 			push @genlist, 'unicall';
 		}

commit 44a5285454166ad798eccf67f120d545a394b2ea
Author: Oron Peled <oron.peled at xorcom.com>
Date:   Mon Sep 23 17:08:49 2013 +0200

    add new dahdi_genconf generators
    
    * pinnedspas: For /etc/dahdi/pinned-spans.conf
    * spantypes: For /etc/dahdi/span-types.conf
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/xpp/perl_modules/Dahdi/Config/Gen/Pinnedspans.pm b/xpp/perl_modules/Dahdi/Config/Gen/Pinnedspans.pm
new file mode 100644
index 0000000..8eaf72d
--- /dev/null
+++ b/xpp/perl_modules/Dahdi/Config/Gen/Pinnedspans.pm
@@ -0,0 +1,57 @@
+package Dahdi::Config::Gen::Pinnedspans;
+use strict;
+
+use Dahdi::Config::Gen qw(is_true);
+
+sub new($$$) {
+	my $pack = shift || die;
+	my $gconfig = shift || die;
+	my $genopts = shift || die;
+	my $file = $ENV{PINNED_SPANS_CONF_FILE} || "/etc/dahdi/pinned-spans.conf";
+	my $self = {
+			FILE	=> $file,
+			GCONFIG	=> $gconfig,
+			GENOPTS	=> $genopts,
+		};
+	bless $self, $pack;
+	return $self;
+}
+
+sub generate($$$) {
+	my $self = shift || die;
+	my $file = $self->{FILE};
+	my $gconfig = $self->{GCONFIG};
+	my $genopts = $self->{GENOPTS};
+	my @spans = @_;
+	warn "Empty configuration -- no spans\n" unless @spans;
+	rename "$file", "$file.bak"
+		or $! == 2	# ENOENT (No dependency on Errno.pm)
+		or die "Failed to backup old config: $!\n";
+	#$gconfig->dump;
+	print "Generating $file\n" if $genopts->{verbose};
+	my $cmd = "span_assignments dumpconfig > $file";
+	system $cmd;
+	die "Command failed (status=$?): '$cmd'" if $?;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+dahdi - Generate configuration for dahdi drivers.
+
+=head1 SYNOPSIS
+
+ use Dahdi::Config::Gen::Dahdi;
+
+ my $cfg = new Dahdi::Config::Gen::Dahdi(\%global_config, \%genopts);
+ $cfg->generate(@span_list);
+
+=head1 DESCRIPTION
+
+Generate the F</etc/dahdi/pinned-spans.conf>.
+This is the configuration for span_assignments.
+
+Its location may be overriden via the environment variable F<PINNED_SPANS_CONF_FILE>.
diff --git a/xpp/perl_modules/Dahdi/Config/Gen/Spantypes.pm b/xpp/perl_modules/Dahdi/Config/Gen/Spantypes.pm
new file mode 100644
index 0000000..1a31a62
--- /dev/null
+++ b/xpp/perl_modules/Dahdi/Config/Gen/Spantypes.pm
@@ -0,0 +1,57 @@
+package Dahdi::Config::Gen::Spantypes;
+use strict;
+
+use Dahdi::Config::Gen qw(is_true);
+
+sub new($$$) {
+	my $pack = shift || die;
+	my $gconfig = shift || die;
+	my $genopts = shift || die;
+	my $file = $ENV{SPAN_TYPES_CONF_FILE} || "/etc/dahdi/span-types.conf";
+	my $self = {
+			FILE	=> $file,
+			GCONFIG	=> $gconfig,
+			GENOPTS	=> $genopts,
+		};
+	bless $self, $pack;
+	return $self;
+}
+
+sub generate($$$) {
+	my $self = shift || die;
+	my $file = $self->{FILE};
+	my $gconfig = $self->{GCONFIG};
+	my $genopts = $self->{GENOPTS};
+	my @spans = @_;
+	warn "Empty configuration -- no spans\n" unless @spans;
+	rename "$file", "$file.bak"
+		or $! == 2	# ENOENT (No dependency on Errno.pm)
+		or die "Failed to backup old config: $!\n";
+	#$gconfig->dump;
+	print "Generating $file\n" if $genopts->{verbose};
+	my $cmd = "span_types dumpconfig > $file";
+	system $cmd;
+	die "Command failed (status=$?): '$cmd'" if $?;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+dahdi - Generate configuration for dahdi drivers.
+
+=head1 SYNOPSIS
+
+ use Dahdi::Config::Gen::Dahdi;
+
+ my $cfg = new Dahdi::Config::Gen::Dahdi(\%global_config, \%genopts);
+ $cfg->generate(@span_list);
+
+=head1 DESCRIPTION
+
+Generate the F</etc/dahdi/span-types.conf>.
+This is the configuration for span_types.
+
+Its location may be overriden via the environment variable F<SPAN_TYPES_CONF_FILE>.

commit 7a1e2223094faeb5c44b7ee499fe97fefab409f0
Author: Oron Peled <oron.peled at xorcom.com>
Date:   Mon Sep 23 17:19:03 2013 +0200

    remove unused dahdi_cfg_device_args
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/dahdi_cfg_device_args b/dahdi_cfg_device_args
deleted file mode 100755
index de5d557..0000000
--- a/dahdi_cfg_device_args
+++ /dev/null
@@ -1,32 +0,0 @@
-#! /bin/sh
-
-devbase='/sys/bus/dahdi_devices/devices'
-
-# Use given devices or otherwise, all existing devices
-if [ "$#" -gt 0 ]; then
-	DEVICES="$@"
-else
-	DEVICES=`echo $devbase/*`
-fi
-
-run_action_spans() {
-	device="$1"
-	for span in $device/span-*
-	do
-		spanno=`echo "$span" | sed 's,.*/span-,,'`
-		spantype=`cat "$span/spantype"`
-		basechan=`cat "$span/basechan"`
-		channels=`cat "$span/channels"`
-		endchan=`expr "$basechan" + "$channels" - 1`
-		echo "-S $spanno -C $basechan-$endchan"
-	done
-}
-
-run_action() {
-	for device in $DEVICES
-	do
-		run_action_spans "$device"
-	done
-}
-
-run_action

commit 52488d66fa209fef71ae58fb6185e2d855769b04
Author: Tzafrir Cohen <tzafrir.cohen at xorcom.com>
Date:   Mon Sep 23 20:43:25 2013 +0300

    handle_device: don't fail if no config files
    
    Allow the udev rule to work well even if there's no span-types.conf
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/handle_device b/handle_device
index 498ebf0..a25c921 100755
--- a/handle_device
+++ b/handle_device
@@ -49,8 +49,12 @@ add)
 
 	# Don't block udev for too long
 	(
-		span_types set "/sys$DEVPATH"
-		span_assignments add "/sys$DEVPATH"
+		if [ -r "$DAHDICONFDIR/span-types.conf" ]; then
+			span_types set "/sys$DEVPATH"
+		fi
+		if [ -r "$DAHDICONFDIR/pinned-spans.conf" ]; then
+			span_assignments add "/sys$DEVPATH"
+		fi
 	) 2>&1 < /dev/null | $LOGGER &
 	;;
 remove)

commit 00af777a979de1b50f09461bd5645d199682f8ad
Author: Oron Peled <oron.peled at xorcom.com>
Date:   Mon Sep 23 13:59:14 2013 +0200

    Newer version of DAHDI hotplug scripts
    
    A newer version of the scripts fully adapted to pinned spans:
    * handle_device does not run dahdi_cfg.
    * A separate UDEV rule script for that: span_config. Should also work
      for the non-pinned case.
    * span_assignments, span_types: add actions 'auto' (manually enable all)
      and 'dumpconfig' (dump current status in the format of configuration
      file).
    * Fixed name of span_types and span_assignments (no '-').
    * spantype.conf renamed span-types.conf: configuration files do have a
      dash.
    * Those two are useful programs, insstalled to /usr/sbin.
    
    Signed-off-by: Tzafrir Cohen <tzafrir.cohen at xorcom.com>

diff --git a/Makefile b/Makefile
index 54bdefa..9bfa294 100644
--- a/Makefile
+++ b/Makefile
@@ -104,9 +104,8 @@ ifeq	(1,$(PBX_HDLC))
 endif
 MAN_PAGES:=$(wildcard $(BINS:%=doc/%.8))
 
-PINNED_DATA_SCRIPTS:=dahdi_cfg_device_args handle_device \
-	span_assignments span_types
-PINNED_UTILS:=dahdi_map
+PINNED_DATA_SCRIPTS:=handle_device span_config
+PINNED_UTILS:=dahdi_map span_assignments span_types
 PINNED_CONF:=pinned-spans.conf spantype.conf
 
 TEST_BINS:=patgen pattest patlooptest hdlcstress hdlctest hdlcgen hdlcverify timertest dahdi_maint
diff --git a/dahdi.rules b/dahdi.rules
index ef08fd3..8674cd1 100644
--- a/dahdi.rules
+++ b/dahdi.rules
@@ -1,3 +1,4 @@
 # DAHDI devices with ownership/permissions for running as non-root
 SUBSYSTEM=="dahdi",		OWNER="asterisk", GROUP="asterisk", MODE="0660"
 SUBSYSTEM=="dahdi_devices",	RUN="/usr/share/dahdi/handle_device"
+SUBSYSTEM=="dahdi_spans",	RUN="/usr/share/dahdi/span_config"
diff --git a/handle_device b/handle_device
index 59d0f62..498ebf0 100755
--- a/handle_device
+++ b/handle_device
@@ -2,7 +2,8 @@
 #
 # /usr/share/dahdi/handle_device
 #
-# Called by UDEV when a span goes online/offline to assign spans
+# Called by UDEV when a dahdi device is added/removed
+#
 
 me=`basename $0`
 dir=`dirname $0`
@@ -22,26 +23,38 @@ PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin"
 
 set -e
 
-run_dahdi_cfg() {
-	args="-c $DAHDICONFDIR/system.conf $@"
-	echo "Running dahdi_cfg $args"
-	dahdi_cfg $args
-}
+#echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
+
+# Check if we can safely do our job
+if [ ! -f /sys/module/dahdi/parameters/auto_assign_spans ]; then
+	echo "Old driver (no auto_assign_spans parameter). Skip $DEVPATH" | $LOGGER
+	exit 0
+fi
+if [ `cat /sys/module/dahdi/parameters/auto_assign_spans` -eq 1 ]; then
+	echo "auto_assign_spans=1. Skip $DEVPATH" | $LOGGER
+	exit 0
+fi
 
-echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
+# Can we pass a different value so we can use
+# alternate (testing) configuration?
+# Meanwhile, make it hard-coded.
+DAHDICONFDIR='/etc/dahdi'
+export DAHDICONFDIR
 
 case "$ACTION" in
 add)
 	echo "$ACTION: $DEVPATH" | $LOGGER
-	# FIXME: need a way to add custom environment here:
-	#export DAHDICONFDIR="/tmp/xortel/dahdi"
-	span_types set "/sys/$DEVPATH" 2>&1 | $LOGGER
-	span_assignments add "/sys/$DEVPATH" 2>&1 | $LOGGER
-	dahdi_cfg_device_args | while read args; do
-			run_dahdi_cfg $args 2>&1 | $LOGGER
-		done
+	# Can have alternate dahdi configuration directory for debugging
+	# export DAHDICONFDIR="/tmp/xortel/dahdi"
+
+	# Don't block udev for too long
+	(
+		span_types set "/sys$DEVPATH"
+		span_assignments add "/sys$DEVPATH"
+	) 2>&1 < /dev/null | $LOGGER &
 	;;
 remove)
+	# Nothing to do yet...
 	echo "$ACTION: $DEVPATH" | $LOGGER
 	;;
 *)
diff --git a/span_assignments b/span_assignments
index 0f4e006..8f29d39 100755
--- a/span_assignments
+++ b/span_assignments
@@ -1,15 +1,23 @@
 #! /bin/sh
 #
-# /usr/share/dahdi/span_assignments:
+# /usr/sbin/span_assignments:
 #
 # this script can be used both from udev and
 # from the command line to assign/unassign and list
-# current assignments.
+# current span assignments.
+#
+# It uses a configuration file: $DAHDICONFDIR/pinned-spans.conf
+# (default DAHDICONFDIR=/etc/dahdi)
 #
 # The first argument is an action:
-#   "add" to assign (spans which are not already assigned)
-#   "remove" to unassign (spans which are not already unassigned)
-#   "list" to show all spans (with/without assignments)
+#   "auto"       - trigger driver auto_assign attribute for given devices
+#		   (no configuration file is used)
+#   "add"        - assign (spans which are not already assigned), according
+#                  to /etc/dahdi/pinned-spans.conf configuration file
+#   "remove"     - unassign spans which are not already unassigned
+#   "list"       - human-readable list of all spans (with/without assignments)
+#   "dumpconfig" - dump current assignments in a /etc/dahdi/pinned-spans.conf
+#                  compatible format
 #
 # Without further arguments, it operates on all existing spans
 # With one or more sysfs dahdi_devices it is limited to those.
@@ -26,7 +34,7 @@ DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
 pinned_spans_conf="$DAHDICONFDIR/pinned-spans.conf"
 
 usage() {
-	echo >&2 "Usage: $0 {add|remove|list} [devpath ...]"
+	echo >&2 "Usage: $0 {auto|add|remove|list|dumpconfig} [devpath ...]"
 	exit 1
 }
 
@@ -66,22 +74,26 @@ show_devices() {
 				spanno='-'
 				basechan='-'
 			fi
-			printf "%-4s %-12s %s\n" "$local_spanno:$spanno:$basechan" "[$hw_id]" "@$location"
+			printf "%-8s %-14s %s\n" "$local_spanno:$spanno:$basechan" "[$hw_id]" "@$location"
 		done | sort -n
 	done
 }
 
 dump_config() {
-
+	echo '#'
+	echo "# Autogenerated by $0 on `date`"
+	echo "# Map devices + local spans to span + base channel number"
+	echo ''
 	for device in $DEVICES
 	do
 		hw_id=`cat "$device/hardware_id"`
-		location=`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
+		location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
 		if [ "$hw_id" != '' ]; then
 			id="$hw_id"
 		else
-			id="@$location"
+			id="$location"
 		fi
+		echo "# Device: [$hw_id] $location"
 		for local_spanno in `cut -d: -f1 "$device/spantype"`
 		do
 			span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \
@@ -90,11 +102,12 @@ dump_config() {
 				spanno=`echo $span | sed 's/^.*-//'`
 				name=`cat 2>/dev/null "$device/$span/name"`
 				basechan=`cat 2>/dev/null "$device/$span/basechan"`
+				printf "%-30s %s\n" "$id" "$local_spanno:$spanno:$basechan"
 			else
-				spanno='-'
+				echo "#   Skipped unassigned local span $local_spanno"
 			fi
-			printf "%-30s %s\n" "$id" "$local_spanno:$spanno:$basechan"
-		done | sort -n
+		done | sort
+		echo ''
 	done
 }
 
@@ -168,18 +181,28 @@ match_device() {
 }
 
 assign_devices() {
+	if [ ! -f "$pinned_spans_conf" ]; then
+		echo >&2 "$0: Missing '$pinned_spans_conf'"
+		exit 1
+	fi
+	echo "using '$pinned_spans_conf'"
 	for device in $DEVICES
 	do
 		match_device "$device"
 	done
 }
 
+auto_assign_devices() {
+	for device in $DEVICES
+	do
+		echo "auto-assign $device"
+		echo 1 > "$device/auto_assign"
+	done
+}
+
 case "$action" in
-list)
-	show_devices
-	;;
-dump)
-	dump_config
+auto)
+	auto_assign_devices
 	;;
 add)
 	assign_devices
@@ -187,6 +210,12 @@ add)
 remove)
 	unassign_all_spans
 	;;
+list)
+	show_devices
+	;;
+dumpconfig)
+	dump_config
+	;;
 *)
 	usage
 	;;
diff --git a/span_config b/span_config
new file mode 100755
index 0000000..8500d46
--- /dev/null
+++ b/span_config
@@ -0,0 +1,70 @@
+#! /bin/sh
+#
+# /usr/share/dahdi/span_config
+#
+# Called by UDEV when a dahdi span is added/removed
+#
+
+me=`basename $0`
+dir=`dirname $0`
+LOGGER="logger -i -t '$me'"
+NAME=`basename "$DEVPATH" | tr -c 'A-Za-z0-9-' '_'`
+
+exec 2> /dev/null
+# Always redirect stderr somewhere, otherwise the shell script will die
+# when it tries to do I/O related stuff on closed file descriptor.
+# Our default is to throw it down the bit-bucket.
+#exec 2> /dev/console
+## If you wish to trace this script:
+#exec 2> "/tmp/${me}.$NAME" 1>&2
+
+# Our directory in the beginning, so we can use local lab setup
+PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin"
+
+set -e
+
+#echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
+
+# Can we pass a different value so we can use
+# alternate (testing) configuration?
+# Meanwhile, make it hard-coded.
+DAHDICONFDIR='/etc/dahdi'
+export DAHDICONFDIR
+
+run_dahdi_cfg() {
+	span_devpath="$1"
+	# Sanity check
+	checkit=`"dahdi_cfg" --help 2>&1 | grep -- '-S' | wc -l`
+	if [ "$checkit" != 1 ]; then
+		$LOGGER "Bad dahdi_cfg (no -S support). Skipping"
+		exit 0
+	fi
+	spanno=`echo "$span_devpath" | sed 's,.*/span-,,'`
+	basechan=`cat "$span_devpath/basechan"`
+	channels=`cat "$span_devpath/channels"`
+	endchan=`expr "$basechan" + "$channels" - 1`
+	echo "dahdi_cfg: span $spanno <$basechan-$endchan>"
+	dahdi_cfg \
+		-c "$DAHDICONFDIR/system.conf" \
+		-S "$spanno" \
+		-C "$basechan-$endchan"
+	asterisk -rx "dahdi create channels $basechan $endchan"
+}
+
+case "$ACTION" in
+add)
+	echo "$ACTION: $DEVPATH" | $LOGGER
+	# Can have alternate dahdi configuration directory for debugging
+	# export DAHDICONFDIR="/tmp/xortel/dahdi"
+
+	run_dahdi_cfg "/sys$DEVPATH" 2>&1 | $LOGGER
+	;;
+remove)
+	# Nothing to do yet...
+	echo "$ACTION: $DEVPATH" | $LOGGER
+	;;
+*)
+	echo "UNHANDLED: $ACTION: $DEVPATH" | $LOGGER
+	;;
+esac
+
diff --git a/span_types b/span_types
index cf7c70d..74d18f1 100755
--- a/span_types
+++ b/span_types
@@ -1,34 +1,36 @@
 #! /bin/sh
 #
-# /usr/share/dahdi/span_types:
+# /usr/sbin/span_types
 #
-# this script can be used both from udev and
-# from the command line for spantype management.
+# This script can be used both from udev and
+# from the command line to manage PRI spans
+# type (E1/T1/J1).
 #
-# It use a configuration file /etc/dahdi/spantype.conf
-# (the format is documented inside this file)
+# Span types can be set only *BEFORE* span are assigned.
 #
-# The first argument is an action:
-#   "list" to show existing E1/T1/J1 types
-#   "dump" the same, but in a format (almost) suitable for
-#          the configuration file
-#          FIXME: we currently don't have the base channo in sysfs.
-#   "set"  actually write the setting to the driver
+# It reads a configuration file /etc/dahdi/span-types.conf
+# (the format is documented inside that file)
+#
+# A mandatory first argument is:
+#   list       - to show existing E1/T1/J1 types
+#   dumpconfig - the same, but in a format (almost) suitable
+#                for the configuration file
+#   set        - actually write the setting to the driver
 #
 # Examples:
 #    span_types list
-#    span_types dump
-#    span_types set	# all
+#    span_types dumpconfig
+#    span_types set	     # all devices
 #    span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00
 #
 
 
 devbase='/sys/bus/dahdi_devices/devices'
 DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
-spantype_conf="$DAHDICONFDIR/spantype.conf"
+spantypes_conf="$DAHDICONFDIR/span-types.conf"
 
 usage() {
-	echo >&2 "Usage: $0 {list|dump|set} [devpath ...]"
+	echo >&2 "Usage: $0 {list|dumpconfig|set} [devpath ...]"
 	exit 1
 }
 
@@ -38,6 +40,11 @@ fi
 action="$1"
 shift
 
+if [ ! -d "$devbase" ]; then
+	echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
+	exit 1
+fi
+
 # Use given devices or otherwise, all existing devices
 if [ "$#" -gt 0 ]; then
 	DEVICES="$@"
@@ -46,19 +53,28 @@ else
 fi
 
 show_spantypes() {
+	echo "# PRI span types (E1/T1/J1)"
 	for device in $DEVICES
 	do
 		hw_id=`cat "$device/hardware_id"`
 		location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
 		cat "$device/spantype" | while read st; do
-			printf "%-10s %-20s %s\n" "$st" "[$hw_id]" "$location"
+			case "$st" in
+			*:[ETJ]1)
+				printf "%-10s %-20s %s\n" \
+					"$st" "[$hw_id]" "$location"
+				;;
+			esac
 		done | sort -n
 	done
 }
 
 dump_config() {
+	echo '#'
+	echo "# Autogenerated by $0 on `date`"
+	echo "# Map PRI DAHDI devices to span types for E1/T1/J1"
+	echo ''
 	fmt="%-65s %s\n"
-	echo "# Map of dahdi_devices to span types for E1/T1/J1"
 	printf "$fmt" '# @location/hardware_id' 'span_type'
 	for device in $DEVICES
 	do
@@ -69,19 +85,24 @@ dump_config() {
 		else
 			id="$location"
 		fi
+		#echo "# Device: [$hw_id] $location"
 		cat "$device/spantype" | while read st; do
 			case "$st" in
 			*:[ETJ]1)
 				printf "$fmt" "$id" "$st"
 				;;
+			*)
+				#echo "#    Skipped local span `echo $st | sed 's/:/ -- /'`"
+				;;
 			esac
 		done | sort -n
+		#echo ''
 	done
 }
 
 # Allow comments and empty lines in config file
 filter_conf() {
-	sed -e 's/#.*//' -e '/^[ \t]*$/d' "$spantype_conf"
+	sed -e 's/#.*//' -e '/^[ \t]*$/d' "$spantypes_conf"
 }
 
 conf_spans() {
@@ -121,7 +142,7 @@ device_set_spantype() {
 	hw_id=`attr_clean "$device/hardware_id"`
 	location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
 	spanspecs=`conf_spans "$hw_id" "$location"`
-	echo >&2 "MATCHED($device): $spanspecs"
+	#echo >&2 "MATCHED($device): $spanspecs"
 	cut -d: -f1 "$attr_file" | while read spanno; do
 			for sp in $spanspecs
 			do
@@ -138,6 +159,10 @@ device_set_spantype() {
 }
 
 set_spantypes() {
+	if [ ! -f "$spantypes_conf" ]; then
+		echo >&2 "$0: Missing configuration '$spantypes_conf'"
+		exit 1
+	fi
 	for device in $DEVICES
 	do
 		device_set_spantype "$device"
@@ -148,7 +173,7 @@ case "$action" in
 list)
 	show_spantypes
 	;;
-dump)
+dumpconfig)
 	dump_config
 	;;
 set)

-----------------------------------------------------------------------


-- 
dahdi/tools.git



More information about the dahdi-commits mailing list