slitaz-tools view tinyutils/tazhw @ rev 763

Tiny edits
author Paul Issott <paul@slitaz.org>
date Sun May 13 13:44:10 2012 +0100 (2012-05-13)
parents e8a02a9a135f
children 72c2ef5c57e7
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 #
11 # Authors : Christophe Lincoln <pankso@slitaz.org>
12 # Rohit Joshi <jozee@slitaz.org>
13 #
15 usage()
16 {
17 echo -e "\nSliTaz Hardware configuration\n
18 \033[1mUsage: \033[0m `basename $0` [command] [--option]
19 \033[1mCommands: \033[0m\n
20 usage Print this short usage.
21 box Start in graphical mode.
22 init Used at boot time to configure devices.
23 setup Setup hardware devices.
24 detect-pci Detect all PCI devices.
25 detect-usb Detect all USB devices.
26 detected-modules List all detected Kernel modules.
28 \033[1mOptions: \033[0m\n
29 --get-firmware Get and install non-free firmware (PCI and USB).\n"
30 }
32 # Check if user is root to install, or remove packages.
33 check_root()
34 {
35 if test $(id -u) != 0 ; then
36 echo -e "\nYou must be root to run `basename $0` with this option."
37 echo -e "Please use 'su' and root password to become super-user.\n"
38 exit 0
39 fi
40 }
41 box_check_root()
42 {
43 if test $(id -u) != 0 ; then
44 exec subox tazhw box
45 exit 0
46 fi
47 }
49 check_firmware()
50 {
51 if [ -x /usr/bin/get-$mod-firmware ]; then
52 if [ ! -d /var/lib/tazpkg/installed/$mod-firmware ]; then
53 # We need and active connection to install firmware and we
54 # only install firmware if specified from cmdline.
55 if ifconfig | grep -q "inet addr"; then
56 # Ensure module is not loaded and get files.
57 if [ "$firmware" == "get" ]; then
58 rmmod $mod 2>/dev/null
59 get-$mod-firmware
60 else
61 echo "* Use --get-firmware option to install missing files."
62 fi
63 else
64 echo "* No active connection to get and install firmware."
65 fi
66 else
67 echo "> Firmware in use: $mod-firmware"
68 fi
69 fi
70 }
72 load_module()
73 {
74 if ! lsmod | grep -q "^$mod"; then
76 # lsmod and some modules use underscore, others use dash.
77 # The real modprobe can handle both but not busybox version
78 modprobe $mod 2>/dev/null && \
79 echo "* Loaded module: $mod" || \
80 echo "! Missing module: $mod"
82 # Add it to load automatically at next boot.
83 if ! echo "$LOAD_MODULES" | grep -q "$mod"; then
84 sed -i s/"LOAD_MODULES=\"$LOAD_MODULES\""/"LOAD_MODULES=\"$LOAD_MODULES $mod\""/ \
85 /etc/rcS.conf
86 fi
87 . /etc/rcS.conf
89 else
90 echo "> Module in use: $mod"
91 fi
92 }
94 # Detect PCI devices and load kernel module only at first boot,
95 # in LiveCD mode or with the command 'detect-pci'.
96 detect_pci_devices()
97 {
98 if [ ! -s /var/lib/detected-modules ]; then
99 . /etc/rcS.conf
100 # We need module_name to match output of lsmod.
101 list=`lspci -k | grep "modules" | cut -d ":" -f 2 | sed s/-/_/g`
102 echo "$list" > /var/lib/detected-modules
103 for mod in $list
104 do
105 check_firmware
106 load_module
107 done
108 # yenta_socket = laptop
109 if `lsmod | grep -q "yenta_socket"`; then
110 grep -qs batt /etc/lxpanel/default/panels/panel ||
111 sed -i 's/= cpu/= batt\n}\n\nPlugin {\n type = cpu/' \
112 /etc/lxpanel/default/panels/panel 2> /dev/null
113 fi
114 fi
115 }
117 # Detect all USB devices.
118 detect_usb_devices()
119 {
120 if [ -f /proc/bus/usb/devices -a ! -s /var/lib/detected-usb-modules ]; then
121 rm -f /var/lib/detected-usb-modules
122 cat /proc/bus/usb/devices | grep "Vendor" | while read line ; do
123 ID=`echo "$line" | awk '{ print $2,$3 }' | sed 's/ / /' | \
124 sed 's/Vendor=/0x/' | sed 's/ProdID=/0x/'`
125 if grep -q "$ID" /lib/modules/`uname -r`/modules.usbmap; then
126 mod=`grep "$ID" /lib/modules/$(uname -r)/modules.usbmap | \
127 awk '{ print $1 }'`
128 prod=`grep -A 2 "$line" /proc/bus/usb/devices | grep Product | \
129 cut -d "=" -f 2`
130 echo "$prod"
131 check_firmware
132 load_module
133 echo " $mod" >> /var/lib/detected-usb-modules
134 fi
135 done
136 fi
137 }
139 display_line()
140 {
141 echo "================================================================================"
142 }
144 # Box functions and dialog
146 box_list()
147 {
148 for mod in `cat /var/lib/detected-modules /var/lib/detected-usb-modules 2>/dev/null`
149 do
150 desc=`modinfo $mod | grep ^description | cut -d ":" -f 2`
151 [ -z "$desc" ] && desc="N/A"
152 echo "$mod | $desc"
153 done
154 }
156 box_detect_devices()
157 {
158 if [ $INSTALL_FIRMARE != true ]; then
159 xterm -T "Detect devices" \
160 -geometry 80x24 \
161 -e "$0 detect-pci; $0 detect-usb; \
162 echo -e \"----\nPress ENTER to close...\"; \
163 read i; exit 0"
164 else
165 xterm -T "Detect and get firmware" \
166 -geometry 80x24 \
167 -e "$0 detect-pci --get-firmware; \
168 $0 detect-usb --get-firmware; \
169 echo -e \"----\nPress ENTER to close...\"; \
170 read i; exit 0"
171 fi
172 }
174 box_setup_devices()
175 {
176 SETUP_OPTIONS=""
177 if [ $INSTALL_NON_FREE == true ]; then
178 SETUP_OPTIONS="$SETUP_OPTIONS --non-free"
179 fi
180 if [ $AUTO_INSTALL_SUGGESTED == true ]; then
181 SETUP_OPTIONS="$SETUP_OPTIONS --suggested"
182 elif [ $CONFIRM_INSTALL_SUGGESTED == true ]; then
183 SETUP_OPTIONS="$SETUP_OPTIONS --confirm"
184 fi
185 xterm -geometry 80x24 -T "$DEVICE Setup" \
186 -e "$0 setup $DEVICE $SETUP_OPTIONS; \
187 echo -e \"----\nPress ENTER to close...\"; \
188 read i; exit 0"
189 }
191 box()
192 {
193 export BIN=$0
194 export MAIN_DIALOG='
195 <window title="Tazhw box" icon-name="computer">
196 <vbox>
197 <text width-chars="54" use-markup="true">
198 <label>"
199 <b>Hardware auto-detection and configuration tool</b>
200 "</label>
201 </text>
202 <frame Loaded Kernel Modules>
203 <vbox>
204 <tree>
205 <width>470</width><height>180</height>
206 <variable>MODULE</variable>
207 <label>Module|Description</label>
208 <input>$BIN box_list</input>
209 <action>refresh:MODULE</action>
210 </tree>
211 <hbox>
212 <checkbox>
213 <label>Auto install non-free Firmware </label>
214 <variable>INSTALL_FIRMARE</variable>
215 <default>false</default>
216 </checkbox>
217 <button>
218 <label>Discover PCI/USB devices</label>
219 <input file icon="folder-saved-search"></input>
220 <action>$BIN box_detect_devices</action>
221 <action>refresh:MODULE</action>
222 </button>
223 </hbox>
224 </vbox>
225 </frame>
226 <frame Hardware Setup>
227 <hbox>
228 <button>
229 <label>Printer</label>
230 <input file icon="printer"></input>
231 <action>export DEVICE="printer";$BIN box_setup_devices</action>
232 </button>
233 <button>
234 <label>Scanner</label>
235 <input file icon="x-office-document"></input>
236 <action>export DEVICE="scanner";$BIN box_setup_devices</action>
237 </button>
238 <button>
239 <label>3G-Modem</label>
240 <input file icon="network-transmit-receive"></input>
241 <action>export DEVICE="3g-modem";$BIN box_setup_devices</action>
242 </button>
243 <button>
244 <label>Nvidia</label>
245 <input file icon="video-display"></input>
246 <action>export DEVICE="nvidia";$BIN box_setup_devices</action>
247 </button>
248 <button>
249 <label>ATI Radeon</label>
250 <input file icon="video-display"></input>
251 <action>export DEVICE="ati";$BIN box_setup_devices</action>
252 </button>
254 </hbox>
255 <hbox>
256 <button>
257 <label>Webcam </label>
258 <input file icon="camera-video"></input>
259 <action>export DEVICE="webcam";$BIN box_setup_devices</action>
260 </button>
262 <button>
263 <label>Digital Camera </label>
264 <input file icon="camera-photo"></input>
265 <action>export DEVICE="camera";$BIN box_setup_devices</action>
266 </button>
267 <button>
268 <label>Bluetooth </label>
269 <input file icon="preferences-desktop-multimedia"></input>
270 <action>export DEVICE="bluetooth";$BIN box_setup_devices</action>
271 </button>
273 <button>
274 <label>Firewall </label>
275 <input file icon="gtk-network"></input>
276 <action>export DEVICE="firewall";$BIN box_setup_devices</action>
277 </button>
279 </hbox>
280 <hbox>
281 <checkbox>
282 <label>Install non-free packages </label>
283 <variable>INSTALL_NON_FREE</variable>
284 <default>true</default>
285 </checkbox>
286 <checkbox>
287 <label>Auto-install all optional packages </label>
288 <variable>AUTO_INSTALL_SUGGESTED</variable>
289 <default>false</default>
290 </checkbox>
292 </hbox>
293 <hbox>
294 <checkbox>
295 <label>Choose optional packages to install</label>
296 <variable>CONFIRM_INSTALL_SUGGESTED</variable>
297 <default>false</default>
298 </checkbox>
299 </hbox>
301 </frame>
302 <hbox>
303 <button>
304 <label>Exit</label>
305 <input file icon="exit"></input>
306 <action type="exit">Exit</action>
307 </button>
308 </hbox>
310 </vbox>
311 </window>'
313 gtkdialog --center --program=MAIN_DIALOG >/dev/null
314 }
316 # Get firmware used by check_firmware()
317 if [ "$2" == "--get-firmware" ]; then
318 firmware='get'
319 fi
321 # What to do.
322 case "$1" in
323 init)
324 check_root
325 echo "Detecting PCI devices Kernel modules..."
326 detect_pci_devices
327 echo "Detecting USB devices Kernel modules..."
328 detect_usb_devices ;;
329 detect-pci)
330 check_root
331 echo -e "\nDetected PCI devices Kernel modules" && display_line
332 rm -f /var/lib/detected-modules
333 detect_pci_devices
334 display_line && echo "" ;;
335 detect-usb)
336 check_root
337 echo -e "\nDetected USB devices Kernel modules" && display_line
338 rm -f /var/lib/detected-usb-modules
339 detect_usb_devices
340 display_line && echo "" ;;
341 setup)
342 SETUP_OPTIONS=`echo "$@" | sed 's/setup//' `
343 hwsetup $SETUP_OPTIONS ;;
344 box*)
345 box_check_root
346 $1 ;;
347 detected-modules)
348 echo -e "\nDetected PCI and USB modules" && display_line
349 cat /var/lib/detected-modules
350 cat /var/lib/detected-usb-modules 2>/dev/null
351 display_line && echo "" ;;
352 *)
353 usage ;;
354 esac
356 exit 0