slitaz-tools rev 299

Add Tazhw - Hardware auto detection (PCI, PCMCIA, USB) tool
author Christophe Lincoln <pankso@slitaz.org>
date Sun Feb 22 22:06:52 2009 +0100 (2009-02-22)
parents 0a481e5dd003
children 20f08a94af98
files rootfs/usr/share/applications/tazhw-box.desktop tinyutils/tazhw
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rootfs/usr/share/applications/tazhw-box.desktop	Sun Feb 22 22:06:52 2009 +0100
     1.3 @@ -0,0 +1,8 @@
     1.4 +[Desktop Entry]
     1.5 +Encoding=UTF-8
     1.6 +Name=Hardware detection and drivers
     1.7 +Name[fr]=Détection du matériel et drivers
     1.8 +Icon=computer
     1.9 +Exec=subox 'tazhw box'
    1.10 +Type=Application
    1.11 +Categories=System;Application;
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/tinyutils/tazhw	Sun Feb 22 22:06:52 2009 +0100
     2.3 @@ -0,0 +1,263 @@
     2.4 +#!/bin/sh
     2.5 +#
     2.6 +# SliTaz Hardware configuration tool. Auto-detect and configure in a
     2.7 +# simple way all PCI, PCMCIA and USB devices. Some Wireless Adapaters 
     2.8 +# need non-free firmware not installed by default, users must use the
     2.9 +# option --get-firmware to force installation. GUI use GTKdialog, is
    2.10 +# called by arg such as all box functions.
    2.11 +#
    2.12 +# (c) 2009 SliTaz GNU/Linux - GNU gpl v3
    2.13 +#
    2.14 +
    2.15 +usage()
    2.16 +{
    2.17 +	echo -e "\nSliTaz Hardware configuration\n
    2.18 +\033[1mUsage: \033[0m `basename $0` [command] [--option]
    2.19 +\033[1mCommands: \033[0m\n
    2.20 +  usage             Print this short usage.
    2.21 +  box               Start in graphical mode.
    2.22 +  init              Used at boot time to configure devices.
    2.23 +  detect-pci        Detect all PCI devices.
    2.24 +  detect-usb        Detect all USB devices.
    2.25 +  detected-modules  List all detected Kernel modules.
    2.26 +  
    2.27 +\033[1mOptions: \033[0m\n
    2.28 +  --get-firmware    Get and install non-free firmware (PCI and USB).\n"
    2.29 +}
    2.30 +
    2.31 +# Check if user is root to install, or remove packages.
    2.32 +check_root()
    2.33 +{
    2.34 +	if test $(id -u) != 0 ; then
    2.35 +		echo -e "\nYou must be root to run `basename $0` with this option."
    2.36 +		echo -e "Please use 'su' and root password to become super-user.\n"
    2.37 +		exit 0
    2.38 +	fi
    2.39 +}
    2.40 +box_check_root()
    2.41 +{
    2.42 +	if test $(id -u) != 0 ; then
    2.43 +		exec subox tazhw box
    2.44 +		exit 0
    2.45 +	fi
    2.46 +}
    2.47 +
    2.48 +check_firmware()
    2.49 +{
    2.50 +	if [ -x /usr/bin/get-$mod-firmware ]; then
    2.51 +		if [ ! -d /var/lib/tazpkg/installed/$mod-firmware ]; then
    2.52 +			# We need and active connection to install firmware and we
    2.53 +			# only install firmware if specified from cmdline.
    2.54 +			if ifconfig | grep -q "inet addr"; then
    2.55 +				# Ensure module is not loaded and get files.				
    2.56 +				if [ "$firmware" == "get" ]; then
    2.57 +					rmmod $mod 2>/dev/null
    2.58 +					get-$mod-firmware
    2.59 +				else
    2.60 +					echo "* Use --get-firmware option to install missing files."
    2.61 +				fi
    2.62 +			else
    2.63 +				echo "* No active connection to get and install firmware."
    2.64 +			fi
    2.65 +		else
    2.66 +			echo "> Firmware in use: $mod-firmware"
    2.67 +		fi
    2.68 +	fi
    2.69 +}
    2.70 +
    2.71 +load_module()
    2.72 +{
    2.73 +	if ! lsmod | grep -q "^$mod"; then
    2.74 +		if [ -f "$(modprobe -l $mod)" ]; then
    2.75 +			echo " Loading Kernel module:  $mod"
    2.76 +			/sbin/modprobe $mod
    2.77 +		else
    2.78 +			echo "! Missing module:  $mod"
    2.79 +		fi
    2.80 +	else
    2.81 +		echo "> Found module:    $mod"
    2.82 +	fi
    2.83 +	# Add module to rcS.conf and avoid duplication.
    2.84 +	. /etc/rcS.conf
    2.85 +	if ! echo "$LOAD_MODULES" | grep -q "$mod"; then
    2.86 +		sed -i s/"LOAD_MODULES=\"$LOAD_MODULES\""/"LOAD_MODULES=\"$LOAD_MODULES $mod\""/ \
    2.87 +			/etc/rcS.conf
    2.88 +	fi
    2.89 +}
    2.90 +
    2.91 +# Retry a Ethernet connection with DHCP after detection (used at boot).
    2.92 +# TODO: try a wireless connection but stop eth0 to avoid conflict.
    2.93 +start_network()
    2.94 +{
    2.95 +	if ifconfig -a | grep -q "eth0"; then
    2.96 +		if [ ! -f /var/run/udhcpc.eth0.pid ]; then
    2.97 +			echo "Starting udhcpc client on: eth0... "
    2.98 +			/sbin/udhcpc -b -i eth0 -p /var/run/udhcpc.eth0.pid
    2.99 +		fi
   2.100 +	fi
   2.101 +}
   2.102 +
   2.103 +# Detect PCI devices and load kernel module only at first boot,
   2.104 +# in LiveCD mode or with the command 'detect-pci'.
   2.105 +detect_pci_devices()
   2.106 +{
   2.107 +	if [ ! -s /var/lib/detected-modules ]; then
   2.108 +		. /etc/rcS.conf
   2.109 +		# We need module_name to match output of lsmod.
   2.110 +		list=`lspci -k | grep "modules" | cut -d ":" -f 2 | sed s/-/_/g`
   2.111 +		echo "$list" > /var/lib/detected-modules
   2.112 +		for mod in $list
   2.113 +		do
   2.114 +			check_firmware
   2.115 +			load_module
   2.116 +		done
   2.117 +		# yenta_socket = laptop
   2.118 +		if `lsmod | grep -q "yenta_socket"`; then
   2.119 +			for mod in ac battery
   2.120 +			do
   2.121 +				load_module
   2.122 +				echo " $mod" >> /var/lib/detected-modules
   2.123 +			done
   2.124 +			sed -i 's/= cpu/= batt\n}\n\nPlugin {\n    type = cpu/' \
   2.125 +				/etc/lxpanel/default/panels/panel 2> /dev/null
   2.126 +		fi
   2.127 +	fi
   2.128 +}
   2.129 +
   2.130 +# Detect all USB devices.
   2.131 +detect_usb_devices()
   2.132 +{
   2.133 +	if [ ! -s /var/lib/detected-usb-modules ]; then
   2.134 +		rm -f /var/lib/detected-usb-modules
   2.135 +		cat /proc/bus/usb/devices | grep "Vendor" | while read line ; do
   2.136 +			ID=`echo "$line" | awk '{ print $2,$3 }' | sed 's/ /   /' | \
   2.137 +				sed 's/Vendor=/0x/' | sed 's/ProdID=/0x/'`
   2.138 +			if grep -q "$ID" /lib/modules/*-slitaz/modules.usbmap; then
   2.139 +				mod=`grep "$ID" /lib/modules/*-slitaz/modules.usbmap | \
   2.140 +					awk '{ print $1 }'`
   2.141 +				prod=`grep -A 2 "$line" /proc/bus/usb/devices | grep Product | \
   2.142 +					cut -d "=" -f 2`
   2.143 +				echo "$prod"
   2.144 +				check_firmware
   2.145 +				load_module
   2.146 +				echo " $mod" >> /var/lib/detected-usb-modules
   2.147 +			fi
   2.148 +		done
   2.149 +	fi
   2.150 +}
   2.151 +
   2.152 +display_line()
   2.153 +{
   2.154 +	echo "================================================================================"
   2.155 +}
   2.156 +
   2.157 +# Box functions and dialog
   2.158 +
   2.159 +box_list()
   2.160 +{
   2.161 +	for mod in `cat /var/lib/detected-modules /var/lib/detected-usb-modules 2>/dev/null`
   2.162 +	do
   2.163 +		desc=`modinfo $mod | grep ^description | cut -d ":" -f 2`
   2.164 +		[ -z "$desc" ] && desc="N/A"
   2.165 +		echo "$mod | $desc"
   2.166 +	done
   2.167 +}
   2.168 +
   2.169 +box_detect_devices()
   2.170 +{
   2.171 +	if [ $INSTALL_FIRMARE != true ]; then
   2.172 +		xterm -T "Detect devices" \
   2.173 +			-geometry 80x24 \
   2.174 +			-e "$0 detect-pci; $0 detect-usb; \
   2.175 +			echo -e \"----\nPress ENTER to close...\"; \
   2.176 +			read i; exit 0"
   2.177 +	else
   2.178 +		xterm -T "Detect and install firmware" \
   2.179 +			-geometry 80x24 \
   2.180 +			-e "$0 detect-pci --install-firmware; $0 detect-usb \
   2.181 +			--install-firmware; echo -e \"----\nPress ENTER to close...\"; \
   2.182 +			read i; exit 0"
   2.183 +	fi
   2.184 +}
   2.185 +
   2.186 +box()
   2.187 +{
   2.188 +	export BIN=$0
   2.189 +	export MAIN_DIALOG='
   2.190 +<window title="Tazhw Box" icon-name="computer">
   2.191 +<vbox>
   2.192 +	
   2.193 +	<text width-chars="54" use-markup="true">
   2.194 +		<label>"
   2.195 +<b>Hardware auto-detection and configuration tool</b>
   2.196 +		"</label>
   2.197 +	</text>
   2.198 +	<tree>
   2.199 +		<width>520</width><height>180</height>
   2.200 +		<variable>MODULE</variable>
   2.201 +		<label>Kernel Module|Description</label>
   2.202 +		<input>$BIN box_list</input>
   2.203 +		<action>refresh:MODULE</action>
   2.204 +	</tree>
   2.205 +	<hbox>
   2.206 +		<checkbox>
   2.207 +			<label>Auto install non-free Firmware</label>
   2.208 +			<variable>INSTALL_FIRMARE</variable>
   2.209 +			<default>false</default>
   2.210 +		</checkbox>
   2.211 +		<button>
   2.212 +			<label>Detect PCI/USB devices</label>
   2.213 +			<input file icon="forward"></input>
   2.214 +			<action>$BIN box_detect_devices</action>
   2.215 +			<action>refresh:MODULE</action>
   2.216 +		</button>
   2.217 +		<button>
   2.218 +			<label>Exit</label>
   2.219 +			<input file icon="exit"></input>
   2.220 +			<action type="exit">Exit</action>
   2.221 +		</button>
   2.222 +	</hbox>
   2.223 +</vbox>
   2.224 +</window>'
   2.225 +	gtkdialog --center --program=MAIN_DIALOG >/dev/null
   2.226 +}
   2.227 +
   2.228 +# Get firmware used by check_firmware()
   2.229 +if [ "$2" == "--get-firmware" ]; then
   2.230 +	firmware='get'
   2.231 +fi
   2.232 +	
   2.233 +# What to do.
   2.234 +case "$1" in
   2.235 +	init)
   2.236 +		check_root
   2.237 +		echo "Detecting PCI devices Kernel modules..."
   2.238 +		detect_pci_devices
   2.239 +		echo "Detecting USB devices Kernel modules..."
   2.240 +		detect_usb_devices 
   2.241 +		start_network ;;
   2.242 +	detect-pci)
   2.243 +		check_root
   2.244 +		echo -e "\nDetected PCI devices Kernel modules" && display_line
   2.245 +		rm -f /var/lib/detected-modules
   2.246 +		detect_pci_devices
   2.247 +		display_line && echo "" ;;
   2.248 +	detect-usb)
   2.249 +		check_root
   2.250 +		echo -e "\nDetected USB devices Kernel modules" && display_line
   2.251 +		rm -f /var/lib/detected-usb-modules
   2.252 +		detect_usb_devices
   2.253 +		display_line && echo "" ;;
   2.254 +	box*)
   2.255 +		box_check_root
   2.256 +		$1 ;;
   2.257 +	detected-modules)
   2.258 +		echo -e "\nDetected PCI and USB modules" && display_line
   2.259 +		cat /var/lib/detected-modules
   2.260 +		cat /var/lib/detected-usb-modules 2>dev/null
   2.261 +		display_line && echo "" ;;
   2.262 +	*)
   2.263 +		usage ;;
   2.264 +esac
   2.265 +
   2.266 +exit 0