summaryrefslogtreecommitdiff
blob: dfd89b7f307b69e300d217448b43822ed65336f0 (plain)
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
#!/bin/sh

usage() {
	cat <<-EOF
	List relevant kernel modules for USB devices currently plugged in.  The
	module list is taken from the modules installed in /lib/modules/<ver>/.

	Usage: usbmodules [options] [kernel version]

	Options:
	  -m, --map <map>   Specify usbmap (default: /lib/modules/<ver>/modules.usbmap)
	  -h, --help        This help screen
	EOF
	if [ -n "$*" ] ; then
		echo
		echo "Error: $*" 1>&2
		exit 1
	else
		exit 0
	fi
}

map=""

while [ -n "$1" ] ; do
	case $1 in
		-m|--map)  map=$2; shift;;
		-h|--help) usage;;
		--)        break;;
		-*)        usage "unknown option '$1'";;
		*)         break;;
	esac
	shift
done

ver=${1:-$(uname -r)}
map=${map:-/lib/modules/${ver}/modules.usbmap}

for dev in $(lsusb | awk '{print $6}') ; do
	[ "${dev}" = "0000:0000" ] && continue

	IFS=:
	set -- ${dev}
	vendor=$1
	product=$2
	unset IFS

	awk \
		-v vendor="0x${vendor}" \
		-v product="0x${product}" \
		'vendor == $3 && product == $4 {print $1}' \
		${map}
done

exit 0