]> git.sur5r.net Git - bacula/rescue/blob - rescue/linux/cdrom/yaird-0.0.5/perl/UsbTab.pm
Initial revision
[bacula/rescue] / rescue / linux / cdrom / yaird-0.0.5 / perl / UsbTab.pm
1 #!perl -w
2 #
3 # UsbTab -- encapsulate modules.usbmap
4 #   Copyright (C) 2005  Erik van Konijnenburg
5 #
6 #   This program is free software; you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation; either version 2 of the License, or
9 #   (at your option) any later version.
10 #
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
15 #
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program; if not, write to the Free Software
18 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 #
20
21 use strict;
22 use warnings;
23 use Base;
24 use UsbMapEntry;
25 package UsbTab;
26
27 my $usbList = undef;
28
29 # Parse this:
30 # # usb module         match_flags idVendor idProduct bcdDevice_lo bcdDevice_hi bDeviceClass bDeviceSubClass bDeviceProtocol bInterfaceClass bInterfaceSubClass bInterfaceProtocol driver_info
31 # bfusb                0x0003      0x057c   0x2200    0x0000       0x0000       0x00         0x00            0x00            0x00            0x00               0x00               0x0
32 # bcm203x              0x0003      0x0a5c   0x2033    0x0000       0x0000       0x00         0x00            0x00            0x00            0x00               0x00               0x0
33
34 sub init () {
35         if (defined ($usbList)) {
36                 return;
37         }
38         $usbList = [];
39         my $name = Conf::get('usbMap');
40         if (! open (IN, "<", "$name")) {
41                 Base::fatal ("can't open usb module list $name");
42         }
43         while (defined (my $line = <IN>)) {
44                 chomp $line;
45                 $line =~ s/#.*//;
46                 $line =~ s/^\s+//;
47                 $line =~ s/\s+$//;
48                 next if ($line eq "");
49                 my @fields = split (/\s+/, $line, 999);
50                 if ($#fields != 12) {
51                         Base::fatal ("malformed line in usb module list $name");
52                 }
53                 push @{$usbList}, UsbMapEntry->new (
54                         module => $fields[0],
55                         match_flags => hex ($fields[1]),
56                         idVendor => hex ($fields[2]),
57                         idProduct => hex ($fields[3]),
58                         bcdDevice_lo => hex ($fields[4]),
59                         bcdDevice_hi => hex ($fields[5]),
60                         bDeviceClass => hex ($fields[6]),
61                         bDeviceSubClass => hex ($fields[7]),
62                         bDeviceProtocol => hex ($fields[8]),
63                         bInterfaceClass => hex ($fields[9]),
64                         bInterfaceSubClass => hex ($fields[10]),
65                         bInterfaceProtocol => hex ($fields[11]),
66                         driver_info => hex ($fields[12]),
67                         );
68         }
69         if (! close (IN)) {
70                 Base::fatal ("could not read usb module list $name");
71         }
72 }
73
74 sub all () {
75         init;
76         return $usbList;
77 }
78
79 # given pathname in devices tree, return list of matching modules.
80 sub find ($) {
81         my ($dev) = @_;
82         my @result = ();
83         for my $ume (@{UsbTab::all()}) {
84                 if ($ume->matches ($dev)) {
85                         push @result, $ume->module;
86                 }
87         }
88         return [@result];
89 }
90
91
92 1;