Git init
[framework/system/usbutils.git] / usb-devices
1 #!/bin/bash
2
3 # Copyright: 2009 Greg Kroah-Hartman <greg@kroah.com>
4 #            2009 Randy Dunlap <rdunlap@xenotime.net>
5 #            2009 Frans Pop <elendil@planet.nl>
6 #
7 # This software may be used and distributed according to the terms of
8 # the GNU General Public License (GPL), version 2, or at your option
9 # any later version.
10
11 print_string() {
12         file=$1
13         name=$2
14         if [ -f $file ]; then
15                 echo "S:  $name=`cat $file`"
16         fi
17 }
18
19 class_decode() {
20         local class=$1          # v4: in hex
21
22         case $class in
23         "00") echo ">ifc " ;;
24         "01") echo "audio" ;;
25         "02") echo "commc" ;;
26         "03") echo "HID  " ;;
27         "05") echo "PID  " ;;
28         "06") echo "still" ;;
29         "07") echo "print" ;;
30         "08") echo "stor." ;;
31         "09") echo "hub  " ;;
32         "0a") echo "data " ;;
33         "0b") echo "scard" ;;
34         "0d") echo "c-sec" ;;
35         "0e") echo "video" ;;
36         "0f") echo "perhc" ;;
37         "dc") echo "diagd" ;;
38         "e0") echo "wlcon" ;;
39         "ef") echo "misc " ;;
40         "fe") echo "app. " ;;
41         "ff") echo "vend." ;;
42         "*")  echo "unk. " ;;
43         esac
44 }
45
46 print_endpoint() {
47         local eppath=$1
48
49         addr=`cat $eppath/bEndpointAddress`
50         attr=`cat $eppath/bmAttributes`
51         dir=`cat $eppath/direction`
52         eptype=`cat $eppath/type`
53         maxps_hex="0x`cat $eppath/wMaxPacketSize`"
54         # Extract MaxPS size (bits 0-10) and multiplicity values (bits 11-12)
55         maxps=`printf "%4i*%s\n" $(($maxps_hex & 0x7ff)) \
56                                 $((1 + (($maxps_hex >> 11) & 0x3)))`
57         interval=`cat $eppath/interval`
58
59         printf "E:  Ad=%s(%s) Atr=%s(%s) MxPS=%s Ivl=%s\n" \
60                 $addr $dir $attr $eptype "$maxps" $interval
61 }
62
63 print_interface() {
64         local ifpath=$1
65
66         ifnum=`cat $ifpath/bInterfaceNumber`
67         altset=`cat $ifpath/bAlternateSetting`
68         numeps=`cat $ifpath/bNumEndpoints`
69         class=`cat $ifpath/bInterfaceClass`
70         subclass=`cat $ifpath/bInterfaceSubClass`
71         protocol=`cat $ifpath/bInterfaceProtocol`
72         if [ -L $ifpath/driver ]; then          # v4: allow for no driver
73                 driver=`readlink $ifpath/driver`
74                 driver=`basename $driver`
75         else
76                 driver="(none)"
77         fi
78         classname=`class_decode $class`
79         printf "I:  If#=%2i Alt=%2i #EPs=%2i Cls=%s(%s) Sub=%s Prot=%s Driver=%s\n" \
80                 $ifnum $altset $numeps $class "$classname" $subclass \
81                 $protocol $driver
82
83         for endpoint in $ifpath/ep_??
84         do
85                 if [ -L $endpoint ]; then       # v4: verify endpoint exists
86                         print_endpoint $endpoint
87                 fi
88         done
89 }
90
91 print_device() {
92         local devpath=$1
93         local parent=$2
94         local level=$3
95         local count=$4
96
97         [ -d $devpath ] || return
98         cd $devpath
99
100         local busnum=`cat busnum`
101         local devnum=`cat devnum`
102
103         if [ $level -gt 0 ]; then
104                 port=$((${devpath##*[-.]} - 1))
105         else
106                 port=0
107         fi
108         speed=`cat speed`
109         maxchild=`cat maxchild`
110         printf "\nT:  Bus=%02i Lev=%02i Prnt=%02i Port=%02i Cnt=%02i Dev#=%3i Spd=%-3s MxCh=%2i\n" \
111                 $busnum $level $parent $port $count $devnum $speed $maxchild
112
113         ver=`cat version`
114         devclass=`cat bDeviceClass`
115         devsubclass=`cat bDeviceSubClass`
116         devprotocol=`cat bDeviceProtocol`
117         maxps0=`cat bMaxPacketSize0`
118         numconfigs=`cat bNumConfigurations`
119         classname=`class_decode $devclass`
120         printf "D:  Ver=%5s Cls=%s(%s) Sub=%s Prot=%s MxPS=%2i #Cfgs=%3i\n" \
121                 $ver $devclass "$classname" $devsubclass $devprotocol \
122                 $maxps0 $numconfigs
123
124         vendid=`cat idVendor`
125         prodid=`cat idProduct`
126         revmajor=`cat bcdDevice | cut -c 1-2`
127         revminor=`cat bcdDevice | cut -c 3-4`
128         printf "P:  Vendor=%s ProdID=%s Rev=%s.%s\n" \
129                 $vendid $prodid $revmajor $revminor
130
131         print_string manufacturer "Manufacturer"
132         print_string product Product
133         print_string serial SerialNumber
134
135         numifs=`cat bNumInterfaces`
136         cfgnum=`cat bConfigurationValue`
137         attr=`cat bmAttributes`
138         maxpower=`cat bMaxPower`
139         printf "C:  #Ifs=%2i Cfg#=%2i Atr=%s MxPwr=%s\n" \
140                 $numifs $cfgnum $attr $maxpower
141
142         # There's not really any useful info in endpoint 00
143         #print_endpoint $devpath/ep_00
144
145         for interface in $busnum-*:?.*
146         do
147                 print_interface $devpath/$interface
148         done
149
150         local devcount=0
151         for subdev in $busnum-*
152         do
153                 echo "$subdev" | grep -Eq "^$busnum-[0-9]+(\.[0-9]+)*$" \
154                         || continue
155
156                 devcount=$(($devcount + 1))
157                 if [ -d $devpath/$subdev ]; then
158                         print_device $devpath/$subdev \
159                                 $devnum $(($level +1)) $devcount
160                 fi
161         done
162 }
163
164 if [ ! -d /sys/bus ]; then
165         echo "Error: directory /sys/bus does not exist; is sysfs mounted?" >&2
166         exit 1
167 fi
168
169 for device in /sys/bus/usb/devices/usb*
170 do
171         print_device $device 0 0 0
172 done