Debian Codegraph @

xpp/perl_modules/Dahdi/Hardware.pm

raw
1 package Dahdi::Hardware;
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
12 =head1 NAME
13
14 Dahdi::Hardware - Perl interface to a Dahdi devices listing
15
16
17 use Dahdi::Hardware;
18
19 my $hardware = Dahdi::Hardware->scan;
20
21 # mini dahdi_hardware:
22 foreach my $device ($hardware->device_list) {
23 print "Vendor: device->{VENDOR}, Product: $device->{PRODUCT}\n"
24 }
25
26 # let's see if there are devices without loaded drivers, and sugggest
27 # drivers to load:
28 my @to_load = ();
29 foreach my $device ($hardware->device_list) {
30 if (! $device->{LOADED} ) {
31 push @to_load, ($device->${DRIVER});
32 }
33 }
34 if (@to_load) {
35 print "To support the extra devices you probably need to run:\n"
36 print " modprobe ". (join ' ', @to_load). "\n";
37 }
38
39
40 This module provides information about available Dahdi devices on the
41 system. It identifies devices by (USB/PCI) bus IDs.
42
43
44 =head1 Device Attributes
45
46 As usual, object attributes can be used in either upp-case or
47 lower-case, or lower-case functions.
48
49 =head2 bus_type
50
51 'PCI' or 'USB'.
52
53
54 =head2 description
55
56 A one-line description of the device.
57
58
59 =head2 driver
60
61 Name of a Dahdi device driver that should handle this device. This is
62 based on a pre-made list.
63
64
65 =head2 vendor, product, subvendor, subproduct
66
67 The PCI and USB vendor ID, product ID, sub-vendor ID and sub-product ID.
68 (The standard short lspci and lsusb listings show only vendor and
69 product IDs).
70
71
72 =head2 loaded
73
74 If the device is handled by a module - the name of the module. Else -
75 undef.
76
77
78 =head2 priv_device_name
79
80 A string that shows the "location" of that device on the bus.
81
82
83 =head2 is_astribank
84
85 True if the device is a Xorcom Astribank (which may provide some extra
86 attributes).
87
88 =head2 serial
89
90 (Astribank-specific attrribute) - the serial number string of the
91 Astribank.
92
93 =cut
94 #
95 # A global hardware handle
96 #
97
98 my %hardware_list = (
99 'PCI' => [],
100 'USB' => [],
101 );
102
103
104 sub new($$) {
105 my $pack = shift || die "Wasn't called as a class method\n";
106 my $name = shift || die "$0: Missing device name";
107 my $type = shift || die "$0: Missing device type";
108 my $dev = {};
109 $dev->{'BUS_TYPE'} = $type;
110 $dev->{IS_ASTRIBANK} = 0 unless defined $dev->{'IS_ASTRIBANK'};
111 $dev->{'HARDWARE_NAME'} = $name;
112 return $dev;
113 }
114
115 =head1 device_list()
116
117 Returns a list of the hardware devices on the system.
118
119 You must run scan() first for this function to run meaningful output.
120
121 =cut
122
123 sub device_list($) {
124 my $pack = shift || die;
125 my @types = @_;
126 my @list;
127
128 @types = qw(USB PCI) unless @types;
129 foreach my $t (@types) {
130 my $lst = $hardware_list{$t};
131 @list = ( @list, @{$lst} );
132 }
133 return @list;
134 }
135
136 sub device_by_hwname($$) {
137 my $pack = shift || die;
138 my $name = shift || die;
139 my @list = device_list('localcall');
140
141 my @good = grep { $_->hardware_name eq $name } @list;
142 return undef unless @good;
143 @good > 1 && die "$pack: Multiple matches for '$name': @good";
144 return $good[0];
145 }
146
147 =head1 drivers()
148
149 Returns a list of drivers (currently sorted by name) that are used by
150 the devices in the current system (regardless to whether or not they are
151 loaded.
152
153 =cut
154
155 sub drivers($) {
156 my $self = shift || die;
157 my @devs = device_list('localcall');
158 my @drvs = map { $_->{DRIVER} } @devs;
159 # Make unique
160 my %drivers;
161 @drivers{@drvs} = 1;
162 return sort keys %drivers;
163 }
164
165
166 =head1 scan()
167
168 Scan the system for Dahdi devices (PCI and USB). Returns nothing but
169 must be run to initialize the module.
170
171 =cut
172
173 my $hardware_scanned;
174
175 sub scan($) {
176 my $pack = shift || die;
177
178 return if $hardware_scanned++;
179 foreach my $type (qw(PCI USB)) {
180 eval "use Dahdi::Hardware::$type";
181 die $@ if $@;
182 $hardware_list{$type} = [ "Dahdi::Hardware::$type"->scan_devices ];
183 }
184 }
185
186 =head1 rescan
187
188 Rescan for devices. In case new devices became available since the script
189 has started.
190
191 =cut
192
193 sub rescan($) {
194 my $pack = shift || die;
195
196 $hardware_scanned = 0;
197 $pack->scan();
198 }
199
200 sub import {
201 Dahdi::Hardware->scan unless grep(/\bnoscan\b/i, @_);
202 }
203
204 sub showall {
205 my $pack = shift || die;
206 my @devs;
207
208 my $printer = sub {
209 my $title = shift;
210 my @devs = @_;
211
212 return unless @devs;
213 printf "%s:\n", $title;
214 foreach my $dev (@devs) {
215 printf "\t%s\n", $dev->hardware_name;
216 foreach my $k (sort keys %{$dev}) {
217 my $v = $dev->{$k};
218 if($k eq 'MPPINFO') {
219 printf "\t\tMPPINFO:\n";
220 eval "use Dahdi::Xpp::Mpp";
221 die $@ if $@;
222 $v->showinfo("\t\t ");
223 } else {
224 printf "\t\t%-20s %s\n", $k, $v;
225 }
226 }
227 }
228 };
229 foreach my $type (qw(USB PCI)) {
230 my $lst = $hardware_list{$type};
231 &$printer("$type devices", @{$lst});
232 }
233 }
234
235 1;