Debian Codegraph @

usb-devices

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