Debian Codegraph @

contrib/parsers/usb_parse/parse_usb.pl

raw
1 #!/usr/bin/perl
2 use strict;
3
4 # Copyright (C) 2010 Mauro Carvalho Chehab
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, version 2 of the License.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # This small script parses USB dumps generated by several drivers,
16 # decoding USB bits.
17 #
18 # To use it, do:
19 # dmesg | ./parse_usb.pl
20 #
21 # Also, there are other utilities that produce similar outputs, and it
22 # is not hard to parse some USB analyzers log into the expected format.
23 #
24
25 sub type_req($)
26 {
27 my $reqtype = shift;
28 my $s;
29
30 if ($reqtype & 0x80) {
31 $s = "RD ";
32 } else {
33 $s = "WR ";
34 }
35 if (($reqtype & 0x60) == 0x20) {
36 $s .= "CLAS ";
37 } elsif (($reqtype & 0x60) == 0x40) {
38 $s .= "VEND ";
39 } elsif (($reqtype & 0x60) == 0x60) {
40 $s .= "RSVD ";
41 }
42
43 if (($reqtype & 0x1f) == 0x00) {
44 $s .= "DEV ";
45 } elsif (($reqtype & 0x1f) == 0x01) {
46 $s .= "INT ";
47 } elsif (($reqtype & 0x1f) == 0x02) {
48 $s .= "EP ";
49 } elsif (($reqtype & 0x1f) == 0x03) {
50 $s .= "OTHER ";
51 } elsif (($reqtype & 0x1f) == 0x04) {
52 $s .= "PORT ";
53 } elsif (($reqtype & 0x1f) == 0x05) {
54 $s .= "RPIPE ";
55 } else {
56 $s .= sprintf "RECIP 0x%02x ", $reqtype & 0x1f;
57 }
58
59 $s =~ s/\s+$//;
60 return $s;
61 }
62
63 while (<>) {
64 if (m/(.*)([0-9a-f].) ([0-9a-f].) ([0-9a-f].) ([0-9a-f].) ([0-9a-f].) ([0-9a-f].) ([0-9a-f].) ([0-9a-f].)[\<\>\s]+(.*)/) {
65 my $timestamp = $1;
66 my $reqtype = hex($2);
67 my $req = hex($3);
68 my $wvalue = hex("$5$4");
69 my $windex = hex("$7$6");
70 my $wlen = hex("$9$8");
71 my $payload = $10;
72
73 $timestamp =~ s/^\s+//;
74 $timestamp =~ s/\s+$//;
75
76 printf("%s %s(0x%02x), Req 0x%02x, wValue: 0x%04x, wIndex 0x%04x, wlen %d: %s\n",
77 $timestamp, type_req($reqtype), $reqtype, $req, $wvalue, $windex, $wlen, $payload);
78 }
79 }