forked from jlhonora/lsusb
/
lsusb
executable file
·370 lines (319 loc) · 9.88 KB
/
lsusb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env bash
# lsusb (list USB devices) utility for macOS
# Author: JLH, Sep 2013
# Author: LanikSJ, Oct 2023
#
# Disclaimer: Usage info and functionality from lsusb under Linux
# Turn on extended patterns
shopt -s extglob
verbose() {
system_profiler SPUSBDataType 2>/dev/null
# Restore the IFS and exit
cleanup
}
version() {
echo "lsusb for macOS 1.1.3"
# Restore the IFS and exit
cleanup
}
help() {
cat >&2 <<EOM
$(usage)
List USB devices
-v Increase verbosity (show output of "system_profiler SPUSBDataType")
-s [[bus]:][devnum]
Show only devices with specified device and/or
bus numbers (in decimal)
-d [vendor]:[product]
Show only devices with the specified vendor and
product ID numbers (in hexadecimal)
-p Display manufacturer names in parentheses
-t Dump the physical USB device hierarchy as a tree
-V Show version of program
-h Show usage and help
EOM
# Restore the IFS and exit
cleanup
}
usage() {
echo "Usage: $(basename "$0") [options]..."
}
unknown_dev_num=127
parse() {
# Get the name of the device, it is the first line that ends with a ':'
# Trim the string at the end
name=$(echo "$device" | head -2 | grep ":$" | sed -e 's/^ *//g' -e 's/ *:$//g')
# Get the speed
speed=$(echo "$device" | grep "Speed: " | awk -F':' '{print $2}' | sed 's/^ *[^0-9]*//g; s/ *$//; s/b\/sec$//; s/ //g')
# Get the PID, trim at the end
PID=$(echo "$device" | grep "Product ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g')
# Get the VID string, trim at the end
VID_all=$(echo "$device" | grep "Vendor ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g')
# Get the VID
VID=$(echo "$VID_all" | awk -F ' ' '{print $1}' | sed 's/^ *//; s/ *$//')
# Get the manufacturer string
manufacturer=$(echo "$VID_all" | cut -d ' ' -f 2- | sed 's/^ *//; s/ *$//')
# Get the Location Id
location=$(echo "$device" | grep "Location ID" | sed -e 's/Location ID://; s/^ *//g; s/ *$//g;')
# Get the bus number. It's the first two hex digits of the Location ID.
# We'll convert to decimal later
# shellcheck disable=SC2001
bus_num=$(echo "$location" | sed -e 's/^..\(..\).*/\1/;')
if [[ -z $bus_num ]]; then
bus_num="00"
fi
# Get the device number. It's after the '/' in the Location ID, already decimal.
device_num=$(echo "$location" | awk -F'/' '{print $2}')
if [[ -z $device_num ]]; then
device_num=$((unknown_dev_num--))
fi
device_num=$(printf "%03d" "$device_num")
# Strip off the excess from LocationID for sorting tree.
locationID=$(echo "$location" | awk -F'/' '{print $1}')
# Special case for "root hub" - follow the (faked) Linux standard.
if [ -z "$PID" ]; then
if [ -z "$name" ]; then
return 1
fi
# Get the bus number, in hexadecimal. We'll convert to decimal later.
bus_num=$(echo "$device" | grep "Bus Number" | sed -e 's/Bus Number://' -e 's/0x//; s/^ *//g' -e 's/ *$//g')
if [[ -z $bus_num ]]; then
bus_num="00"
fi
# Create a fake Location ID for sorting purposes, following the model.
locationID="0x${bus_num}000000"
# Device Number is always 1.
device_num="001"
# Vendor ID and Manufacturer are always as follows.
VID="05ac"
manufacturer="(Apple Inc.)"
# The PID depends on the speed of the hub, which we deduce from the controller driver.
PID=$(echo "$device" | grep "Controller Driver" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g; s/.*\(....\)$/\1/')
case "$PID" in
OHCI)
PID="8005"
name="OHCI Root Hub Simulation"
speed="12M"
;;
EHCI)
PID="8006"
name="EHCI Root Hub Simulation"
speed="480M"
;;
XHCI)
PID="8007"
name="XHCI Root Hub USB 2.0 Simulation"
speed="5000M"
;;
esac
fi
if [ ${#bus_num} != 0 ]; then
# Convert bus number from hexadecimal to decimal.
bus_num=$(printf "%03d" "0x0$bus_num")
else
bus_num='---'
fi
# Strip the parentheses from manufacturer name unless so specified.
if [ -z "$parens" ]; then
manufacturer=$(echo "$manufacturer" | sed 's/(//; s/)//')
fi
# Include serial number only if available.
serial_str=""
serial_number=$(echo "$device" | grep "Serial Number" | sed 's/Serial Number: //; s/^ *//g; s/ *$//g')
if [ -n "$serial_number" ]; then
serial_str=" Serial: ""$serial_number"
fi
# Don't filter if we're building the tree.
if [ "$treeflag" == "yes" ]; then
exitcode=0
return
fi
# Filter by VID/PID if given as input argument.
if [ -n "$vid_pid" ]; then
# Convert input vid to lower case.
# shellcheck disable=SC2143
if [ -n "$(echo "$vid_pid" | grep ':')" ]; then
arg_vid=$(echo "$vid_pid" | awk -F':' '{print $1}' | sed 's/^0x//' | tr '[:upper:]' '[:lower:]')
if [ -n "$arg_vid" ]; then
if [ $((16#$arg_vid)) != $((16#$VID)) ]; then
return 1
fi
fi
arg_pid=$(echo "$vid_pid" | awk -F':' '{print $2}' | sed 's/^0x//' | tr '[:upper:]' '[:lower:]')
if [ -n "$arg_pid" ]; then
if [ $((16#$arg_pid)) != $((16#$PID)) ]; then
return 1
fi
fi
else
# Missing a colon is a syntax error in -d option
help
# shellcheck disable=SC2317
exit 1
fi
fi
# Filter by BUS/DEV if given as input argument.
if [ -n "$bus_dev" ]; then
# shellcheck disable=SC2143
if [ -n "$(echo "$bus_dev" | grep ':')" ]; then
# Convert input bus to lower case
arg_bus=$(echo "$bus_dev" | awk -F':' '{print $1}' | sed 's/^ *//; s/ *$//')
if [ -n "$arg_bus" ]; then
if [ "$arg_bus" != "$bus_num" ]; then
return 1
fi
fi
# Strip leading and trailing spaces from argument.
arg_dev=$(echo "$bus_dev" | awk -F':' '{print $2}' | sed 's/^ *//; s/ *$//')
if [ -n "$arg_dev" ]; then
if [ "$arg_dev" != "$device_num" ]; then
return 1
fi
fi
fi
fi
# Special case: no colon after -s means device-only.
# shellcheck disable=SC2143
if [ -z "$(echo "$bus_dev" | grep ':')" ]; then
# Strip leading and trailing spaces from argument.
arg_dev=$(echo "$bus_dev" | sed 's/^ *//; s/ *$//')
if [ -n "$arg_dev" ]; then
if [ "$arg_dev" != "$device_num" ]; then
return 1
fi
fi
fi
# If we got this far, we found a match, so set exit code to 0 before returning
exitcode=0
}
setup() {
# Save the data (if we haven't already).
if [ -z "$rawlog" ]; then
rawlog=$(verbose)
fi
# Change the IFS to #, backup the current one.
OIFS=$IFS
IFS="#"
# Default exit code is failure, but if we match anything this will change to success.
exitcode=1
# Flag to know if -t option was set. We ignore filters if so.
treeflag="no"
}
cleanup() {
IFS=$OIFS
# shellcheck disable=SC2086
exit $exitcode
}
buildtreeline() {
# Build a formatted line to be sorted for the tree.
# The LocationID has the tree structure (0xbbdddddd):
# 0x -- always
# bb -- bus number in hexadecimal
# dddddd -- up to six levels for the tree, each digit represents its
# position on that level
#
# So we start each line with the LocationID for sorting purposes, then append the rest of the line as we want it
# Later, we'll do the sort, then strip off the LocationID for display purposes
spaces=$(echo "$locationID" | sed 's/^0x...//; s/0//g; s/./ /g; s/.*/&&&&/')
if [ ${#spaces} -eq 0 ]; then
spaces=" /: "
else
spaces="${spaces}|__ "
fi
treeline="${locationID}${spaces}Bus ""${bus_num}"".Dev ""${device_num}"": ""${name}"", ""${speed}"
}
get_devices() {
sed <<<"$rawlog" -e '/:$/i\
#'
}
get_buses() {
awk <<<"$rawlog" -v first=1 '/Bus:$/ { flag = 1; if (first == 1) { first = 0 } else { print "#" }; print; next }; /:$/ && flag == 1 { flag = 0 }; flag'
}
tree() {
setup
treeflag="yes"
for device in $(get_devices); do
# Skip null device lines
if [ "${#device}" != 1 ]; then
parse
buildtreeline
treedata="$treedata""${treeline}"$'\n'
fi
done
for device in $(get_buses); do
parse
buildtreeline
treedata="${treedata}""${treeline}"$'\n'
done
# Strip off final \n.
treedata=$(echo "${treedata}" | awk '{ prev_line = line; line = $0; } NR > 1 { print prev_line; } END { ORS = ""; print line; }')
# Sort by Location ID.
treedata=$(echo "${treedata}" | sort)
# Now strip off leading Location ID and print to stdout.
for line in $treedata; do
echo -e "$line"
done
# Restore the IFS
cleanup
}
# Parse options
while getopts ":hvpd:s:Vt" opt; do
case "$opt" in
h)
help
;; # help
v)
verbose
;; # verbose
t)
tree
;; # tree
V)
version
;; # version
d) vid_pid=$OPTARG ;; # filter
s) bus_dev=$OPTARG ;; # filter
p) parens="yes" ;; # formatter
*)
echo >&2 "lsusb: invalid option -- '$OPTARG'"
help
;;
esac
done
setup
# Get all sets of lines that contain the "Product ID" string.
# Also include other relevant lines. Here's an entry sample:
#
# BRCM2046 Hub:
#
# Product ID: 0x4500
# Vendor ID: 0x0a5c (Broadcom Corp.)
# Version: 1.00
# Speed: Up to 12 Mb/sec
# Manufacturer: Apple Inc.
# Location ID: 0x06100000 / 2
# Current Available (mA): 500
# Current Required (mA): 0
#
# Each entry will be separated by two '--' lines. We replace them
# with the '#' symbol in case other parameters that contain a dash
# will not be interfered
# Iterate over each entry
if [ -z "$get_devices" ]; then
for device in $(get_devices); do
if parse; then
# Print the formatted entry
echo "Bus ""$bus_num"" Device ""$device_num"": ID ""$VID"":""$PID"" ""$manufacturer"" ""$name"" ""$serial_str"
fi
done
fi
if [ -n "$get_buses" ]; then
for device in $(get_buses); do
if parse; then
# Print the formatted entry
echo "Buses:" && echo "Bus ""$bus_num"" Device ""$device_num"": ID ""$VID"":""$PID"" ""$manufacturer"" ""$name"" ""$serial_str"
fi
done
fi
# Restore the IFS and exit
cleanup