Debian Codegraph @

prog/debug/find-driver

raw
1 #!/bin/bash
2 #
3 # find-driver - find the driver exporting a chip as reported by 'sensors'
4 # Copyright (C) 2018 Ondřej Lysoněk
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 shopt -s nullglob
21
22 function usage() {
23 echo "Usage: $0 <chip name>" >&2
24 echo >&2
25 echo 'Chip name is a string in the format <chip>-<bus>-<address>' >&2
26 echo 'as reported by the "sensors" program. For example the chip' >&2
27 echo 'name can be "coretemp-isa-0000" or "acpitz-virtual-0".' >&2
28 }
29
30 function get_chip() {
31 echo "$1" | sed -r -e 's/^(.+)-[^-]+-[^-]+$/\1/'
32 }
33
34 if [ "$#" -lt 1 ]; then
35 usage
36 exit 1
37 fi
38 ARG=$1; shift
39 if [ "$ARG" = "-h" ] || [ "$ARG" = "--help" ]; then
40 usage
41 test "$#" -eq 0
42 exit
43 elif [ "$ARG" = "-d" ] || [ "$ARG" = "--debug" ]; then
44 test "$#" -eq 1 || { usage; exit 1; }
45 set -x
46 CHIP=$(get_chip "$1")
47 else
48 test "$#" -eq 0 || { usage; exit 1; }
49 CHIP=$(get_chip "$ARG")
50 fi
51
52 DEVICE=
53 for i in /sys/class/hwmon/hwmon*; do
54 NAME=
55 if [ -f "$i/name" ]; then
56 NAME=$(cat "$i/name")
57 elif [ -L "$i/device" ]; then
58 TARGET=$(readlink -f "$i/device")
59 if [ -f "$TARGET/name" ]; then
60 NAME=$(cat "$TARGET/name")
61 fi
62 fi
63 if [ "$NAME" = "$CHIP" ]; then
64 DEVICE="$i"
65 break
66 fi
67 done
68 test -n "$DEVICE" || { echo 'Chip not found.' >&2; exit 1; }
69
70 DRIVER=unknown
71 MODULE=unknown
72 while true; do
73 if [ -L "$DEVICE/driver" ]; then
74 DRIVER_DIR=$(readlink -f "$DEVICE/driver")
75 DRIVER=$(basename "$DRIVER_DIR")
76 if [ -L "$DRIVER_DIR/module" ]; then
77 MODULE=$(basename $(readlink "$DRIVER_DIR/module"))
78 fi
79 break
80 elif [ -L "$DEVICE/device" ]; then
81 DEVICE=$(readlink -f "$DEVICE/device")
82 else
83 break
84 fi
85 done
86
87 echo "Driver: $DRIVER"
88 echo "Module: $MODULE"