slitaz-tools view tinyutils/tazhw @ rev 1037

tazbox: lxpolkit support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Oct 23 10:41:01 2021 +0000 (2021-10-23)
parents d91890c642cf
children
line source
1 #!/bin/sh
2 #
3 # SliTaz Hardware configuration tool. Auto-detect and configure in a
4 # simple way all PCI, PCMCIA and USB devices. Some Wireless Adapters
5 # need non-free firmware not installed by default. Users must use the
6 # option --get-firmware to force installation. GUI uses Yad and is
7 # called by args such as all box functions.
8 #
9 # (c) 2009-2015 SliTaz GNU/Linux - GNU GPL v3
10 #
11 # Authors: Christophe Lincoln <pankso@slitaz.org>
12 # Rohit Joshi <jozee@slitaz.org>
13 #
15 . /lib/libtaz.sh
16 export TEXTDOMAIN='slitaz-tools' # i18n text mode
18 usage() {
19 cat << EOT
21 $(_n 'SliTaz Hardware configuration')
23 $(boldify "$(_n 'Usage:')") $(basename $0) [$(_n 'command')] [--$(_n 'option')]
25 $(boldify "$(_n 'Commands:')")
26 usage $(_n 'Print this short usage.')
27 init $(_n 'Used at boot time to configure devices.')
28 setup $(_n 'Setup hardware devices.')
29 detect-pci $(_n 'Detect all PCI devices.')
30 detect-usb $(_n 'Detect all USB devices.')
31 detected-modules $(_n 'List all detected Kernel modules.')
33 $(boldify "$(_n 'Options:')")
34 --get-firmware $(_n 'Get and install non-free firmware (PCI and USB).')
36 EOT
37 }
40 check_firmware() {
41 if [ -x /usr/bin/get-$mod-firmware ]; then
42 if [ ! -d /var/lib/tazpkg/installed/$mod-firmware ]; then
43 # We need an active connection to install firmware and we
44 # only install firmware if specified from cmdline.
45 if ifconfig | grep -q "inet addr"; then
46 # Ensure module is not loaded and get files.
47 if [ "$firmware" = "get" ]; then
48 rmmod $mod 2>/dev/null
49 get-$mod-firmware
50 else
51 _ '* Use --get-firmware option to install missing files.'
52 fi
53 else
54 _ '* No active connection to get and install firmware.'
55 fi
56 else
57 _ '> Firmware in use: %s' "$mod-firmware"
58 fi
59 fi
60 }
63 load_module() {
64 if ! lsmod | grep -q "^$mod"; then
65 # Check if builtin, loaded or missing
66 if zcat /proc/config.gz | fgrep -i $mod | fgrep -q '=y'; then
67 _ '* Builtin module : %s' "$mod"
68 unset mod
69 elif modprobe $mod 2>/dev/null; then
70 _ '* Loaded module : %s' "$mod"
71 else
72 _ '! Missing module : %s' "$mod"
73 fi
74 else
75 _ '> Module in use : %s' "$mod"
76 fi
77 # Add it to load automatically at next boot.
78 if ! echo "$LOAD_MODULES" | grep -q "$mod"; then
79 sed -i s/"LOAD_MODULES=\"$LOAD_MODULES\""/"LOAD_MODULES=\"$LOAD_MODULES $mod\""/ \
80 /etc/rcS.conf
81 fi
82 . /etc/rcS.conf
83 }
86 module_name()
87 {
88 local mod mod2
89 while read mod; do
90 if [ -d /sys/module -a ! -d /sys/module/${mod//-/_} ]; then
91 mod2=$(ls -d /sys/module/*/drivers/*:$mod 2>/dev/null | \
92 sed 's|/sys/module/\(.*\)/drivers/.*|\1|')
93 [ "$mod2" ] && mod=$mod2
94 fi
95 echo ${mod//-/_}
96 done
97 }
100 # Detect PCI devices and load kernel module only at first boot,
101 # in LiveCD mode or with the command 'detect-pci'.
103 detect_pci_devices() {
104 if [ ! -s /var/lib/detected-modules ]; then
105 . /etc/rcS.conf
106 # We need module_name to match output of lsmod.
107 list=$(sed '/^DRIVER=/!d;s/.*=/ /' /sys/bus/pci/devices/*/uevent| module_name)
108 echo "$list" > /var/lib/detected-modules
109 for mod in $(sort < /var/lib/detected-modules | uniq)
110 do
111 check_firmware
112 load_module
113 done
114 fi
115 }
118 # Detect all USB devices.
120 detect_usb_devices() {
121 if [ -e /sys/bus/usb/devices/usb1 ]; then
122 for product in /sys/bus/usb/devices/*/product
123 do
124 path=$(dirname $product)
125 product=$(cat $product)
126 config=$(cat $path/configuration)
127 debug "$path"
128 . $path/[0-9]*/uevent
129 [ ! "$DRIVER" ] && DRIVER="(none)"
130 echo "$product $config $(indent 40 $DRIVER)"
131 unset DRIVER
132 done
133 fi
134 }
137 # Get firmware used by check_firmware()
139 if [ "$2" = "--get-firmware" ]; then
140 firmware='get'
141 fi
144 # What to do.
146 case "$1" in
147 -i|init)
148 check_root
149 _ 'Detecting PCI devices Kernel modules...'
150 detect_pci_devices
151 _ 'Detecting USB devices Kernel modules...'
152 detect_usb_devices
153 [ -e /sys/class/thermal/thermal_zone0/mode ] &&
154 _ 'Enable fan control...' &&
155 for zone in /sys/class/thermal/thermal_zone*/mode; do
156 echo -n enabled > $zone
157 done ;;
158 -dp|detect-pci)
159 check_root
160 title 'Detected PCI devices Kernel modules'
161 rm -f /var/lib/detected-modules
162 detect_pci_devices
163 footer ;;
164 -du|detect-usb)
165 check_root
166 title 'Detected USB devices Kernel modules'
167 rm -f /var/lib/detected-usb-modules
168 detect_usb_devices
169 footer ;;
170 -s|setup)
171 SETUP_OPTIONS=$(echo "$@" | sed 's/setup//')
172 check_root
173 hwsetup $SETUP_OPTIONS ;;
174 -dm|detected-modules)
175 title 'Detected PCI and USB modules'
176 cat /var/lib/detected-modules /var/lib/detected-usb-modules 2>/dev/null
177 footer ;;
178 *)
179 usage ;;
180 esac
182 exit 0