slitaz-tools view tinyutils/tazhw @ rev 393

Added tag 3.3 for changeset b27d8925b831
author Christophe Lincoln <pankso@slitaz.org>
date Thu Oct 01 21:57:15 2009 +0200 (2009-10-01)
parents 341674b44feb
children d68593b0db9b
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 GTKdialog, and is
7 # called by args such as all box functions.
8 #
9 # (c) 2009 SliTaz GNU/Linux - GNU gpl v3
10 #
12 usage()
13 {
14 echo -e "\nSliTaz Hardware configuration\n
15 \033[1mUsage: \033[0m `basename $0` [command] [--option]
16 \033[1mCommands: \033[0m\n
17 usage Print this short usage.
18 box Start in graphical mode.
19 init Used at boot time to configure devices.
20 detect-pci Detect all PCI devices.
21 detect-usb Detect all USB devices.
22 detected-modules List all detected Kernel modules.
24 \033[1mOptions: \033[0m\n
25 --get-firmware Get and install non-free firmware (PCI and USB).\n"
26 }
28 # Check if user is root to install, or remove packages.
29 check_root()
30 {
31 if test $(id -u) != 0 ; then
32 echo -e "\nYou must be root to run `basename $0` with this option."
33 echo -e "Please use 'su' and root password to become super-user.\n"
34 exit 0
35 fi
36 }
37 box_check_root()
38 {
39 if test $(id -u) != 0 ; then
40 exec subox tazhw box
41 exit 0
42 fi
43 }
45 check_firmware()
46 {
47 if [ -x /usr/bin/get-$mod-firmware ]; then
48 if [ ! -d /var/lib/tazpkg/installed/$mod-firmware ]; then
49 # We need and active connection to install firmware and we
50 # only install firmware if specified from cmdline.
51 if ifconfig | grep -q "inet addr"; then
52 # Ensure module is not loaded and get files.
53 if [ "$firmware" == "get" ]; then
54 rmmod $mod 2>/dev/null
55 get-$mod-firmware
56 else
57 echo "* Use --get-firmware option to install missing files."
58 fi
59 else
60 echo "* No active connection to get and install firmware."
61 fi
62 else
63 echo "> Firmware in use: $mod-firmware"
64 fi
65 fi
66 }
68 load_module()
69 {
70 if ! lsmod | grep -q "^$mod"; then
71 # lsmod and some modules use underscore, other use dash.
72 # The real modprobe can handle both but not busybox version
73 modprobe $mod 2>/dev/null && \
74 echo "* Loaded module: $mod" || \
75 echo "! Missing module: $mod"
76 else
77 echo "> Module in use: $mod"
78 fi
79 # Add module to rcS.conf and avoid duplication.
80 . /etc/rcS.conf
81 if ! echo "$LOAD_MODULES" | grep -q "$mod"; then
82 sed -i s/"LOAD_MODULES=\"$LOAD_MODULES\""/"LOAD_MODULES=\"$LOAD_MODULES $mod\""/ \
83 /etc/rcS.conf
84 fi
85 }
87 # Detect PCI devices and load kernel module only at first boot,
88 # in LiveCD mode or with the command 'detect-pci'.
89 detect_pci_devices()
90 {
91 if [ ! -s /var/lib/detected-modules ]; then
92 . /etc/rcS.conf
93 # We need module_name to match output of lsmod.
94 list=`lspci -k | grep "modules" | cut -d ":" -f 2 | sed s/-/_/g`
95 echo "$list" > /var/lib/detected-modules
96 for mod in $list
97 do
98 check_firmware
99 load_module
100 done
101 # yenta_socket = laptop
102 if `lsmod | grep -q "yenta_socket"`; then
103 for mod in ac battery
104 do
105 load_module
106 echo " $mod" >> /var/lib/detected-modules
107 done
108 grep -qs batt /etc/lxpanel/default/panels/panel ||
109 sed -i 's/= cpu/= batt\n}\n\nPlugin {\n type = cpu/' \
110 /etc/lxpanel/default/panels/panel 2> /dev/null
111 fi
112 fi
113 }
115 # Detect all USB devices.
116 detect_usb_devices()
117 {
118 if [ ! -s /var/lib/detected-usb-modules ]; then
119 rm -f /var/lib/detected-usb-modules
120 cat /proc/bus/usb/devices | grep "Vendor" | while read line ; do
121 ID=`echo "$line" | awk '{ print $2,$3 }' | sed 's/ / /' | \
122 sed 's/Vendor=/0x/' | sed 's/ProdID=/0x/'`
123 if grep -q "$ID" /lib/modules/*-slitaz/modules.usbmap; then
124 mod=`grep "$ID" /lib/modules/*-slitaz/modules.usbmap | \
125 awk '{ print $1 }'`
126 prod=`grep -A 2 "$line" /proc/bus/usb/devices | grep Product | \
127 cut -d "=" -f 2`
128 echo "$prod"
129 check_firmware
130 load_module
131 echo " $mod" >> /var/lib/detected-usb-modules
132 fi
133 done
134 fi
135 }
137 display_line()
138 {
139 echo "================================================================================"
140 }
142 # Box functions and dialog
144 box_list()
145 {
146 for mod in `cat /var/lib/detected-modules /var/lib/detected-usb-modules 2>/dev/null`
147 do
148 desc=`modinfo $mod | grep ^description | cut -d ":" -f 2`
149 [ -z "$desc" ] && desc="N/A"
150 echo "$mod | $desc"
151 done
152 }
154 box_detect_devices()
155 {
156 if [ $INSTALL_FIRMARE != true ]; then
157 xterm -T "Detect devices" \
158 -geometry 80x24 \
159 -e "$0 detect-pci; $0 detect-usb; \
160 echo -e \"----\nPress ENTER to close...\"; \
161 read i; exit 0"
162 else
163 xterm -T "Detect and get firmware" \
164 -geometry 80x24 \
165 -e "$0 detect-pci --get-firmware; \
166 $0 detect-usb --get-firmware; \
167 echo -e \"----\nPress ENTER to close...\"; \
168 read i; exit 0"
169 fi
170 }
172 box()
173 {
174 export BIN=$0
175 export MAIN_DIALOG='
176 <window title="Tazhw Box" icon-name="computer">
177 <vbox>
179 <text width-chars="54" use-markup="true">
180 <label>"
181 <b>Hardware auto-detection and configuration tool</b>
182 "</label>
183 </text>
184 <tree>
185 <width>520</width><height>180</height>
186 <variable>MODULE</variable>
187 <label>Kernel Module|Description</label>
188 <input>$BIN box_list</input>
189 <action>refresh:MODULE</action>
190 </tree>
191 <hbox>
192 <checkbox>
193 <label>Auto install non-free Firmware</label>
194 <variable>INSTALL_FIRMARE</variable>
195 <default>false</default>
196 </checkbox>
197 <button>
198 <label>Detect PCI/USB devices</label>
199 <input file icon="forward"></input>
200 <action>$BIN box_detect_devices</action>
201 <action>refresh:MODULE</action>
202 </button>
203 <button>
204 <label>Exit</label>
205 <input file icon="exit"></input>
206 <action type="exit">Exit</action>
207 </button>
208 </hbox>
209 </vbox>
210 </window>'
211 gtkdialog --center --program=MAIN_DIALOG >/dev/null
212 }
214 # Get firmware used by check_firmware()
215 if [ "$2" == "--get-firmware" ]; then
216 firmware='get'
217 fi
219 # What to do.
220 case "$1" in
221 init)
222 check_root
223 echo "Detecting PCI devices Kernel modules..."
224 detect_pci_devices
225 echo "Detecting USB devices Kernel modules..."
226 detect_usb_devices ;;
227 detect-pci)
228 check_root
229 echo -e "\nDetected PCI devices Kernel modules" && display_line
230 rm -f /var/lib/detected-modules
231 detect_pci_devices
232 display_line && echo "" ;;
233 detect-usb)
234 check_root
235 echo -e "\nDetected USB devices Kernel modules" && display_line
236 rm -f /var/lib/detected-usb-modules
237 detect_usb_devices
238 display_line && echo "" ;;
239 box*)
240 box_check_root
241 $1 ;;
242 detected-modules)
243 echo -e "\nDetected PCI and USB modules" && display_line
244 cat /var/lib/detected-modules
245 cat /var/lib/detected-usb-modules 2>/dev/null
246 display_line && echo "" ;;
247 *)
248 usage ;;
249 esac
251 exit 0