Sophie

Sophie

distrib > Mandriva > 10.2 > x86_64 > by-pkgid > a6f769c34cf8fed361fcb46cdf6be9ca > files > 1

cpufreq-1.0-14mdk.src.rpm

#!/bin/bash
#
# Init file for cpufreq
#
# chkconfig: 2345 65 35
# description: cpufreq - probing CPU frequency scaling modulesi



# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0
KERNEL_TYPE=`uname -r | sed -e 's/\(2\.[2-6]\).*/\1/g'`

#Some hardware informations
family=`LC_ALL=C grep "cpu family" /proc/cpuinfo | awk '{print $4}' | head -1`
vendor=`LC_ALL=C grep "vendor_id" /proc/cpuinfo | awk '{print $3}' | head -1`
model=`LC_ALL=C grep "model" /proc/cpuinfo | awk '{print $3}' | head -1`
flags=`grep "flags" /proc/cpuinfo | cut -d ":" -f 2 | head -1 | sed -e 's/ //'`
power_management=`sed -n '/^power management *: \(.*\)/s//\1/p' /proc/cpuinfo`
INTEL="GenuineIntel"
AMD="AuthenticAMD"
CYRIX="CyrixInstead"
NSC="Geode by NSC"
TRANSMETA="TransmetaCPU"
TRANSMETA1="GenuineTMx86"
CENTAUR="CentaurHauls"
PPC="PowerBook3,4 PowerBook3,5 PowerBook4,1 PowerBook3,2 MacRISC3"

# The module to probe
MODULE=""

isFlag() {
for flag in $flags ; do
	if [ "$flag" = "$1" ]; then
		return 0;
	fi
done
return -1

}

isPciDevice() {
for pcidevice in $(lspci -n | awk '{print $4}'); do
	if [ "$pcidevice" == "$1" ]; then
		return 0;
	fi
done
return -1
}

probe_P4() {
	if [ "$vendor" = "$INTEL" ]; then
		if [ "$family" = "15" ]; then return 0 ; fi
	fi
	return -1
}

probe_centrino() {
	if [ "$vendor" = "$INTEL" ]; then
		if isFlag est &&  [ "$family" = "6" ]; then 
			if [ "$model" = "9" -o "$model" = "13" -o "$model" = "15" ]; then return 0; fi
		fi
	fi
	return -1
}

probe_ich() {
	if isPciDevice 8086:24cc || isPciDevice 8086:248c || isPciDevice 8086:244c ; then return 0; fi
	return -1;
}

probe_smi() {
	if isPciDevice 8086:7190 ; then return 0; fi
	return -1;
}

probe_powernow_k6() {
	if [ "$vendor" = "$AMD" ]; then
		if [ "$family" = "5" ]; then 
			if [ "$model" = "12" -o "$model" = "13" ]; then return 0; fi
		fi
	fi
	return -1
}

probe_powernow_k7() {
	if [ "$vendor" = "$AMD" ]; then
		if [ "$family" = "6" ]; then return 0; fi
	fi
	return -1
}

probe_powernow_k8() {
    if [[ "$vendor" = "$AMD" ]]; then
	if [ "$family" = "15" ]; then return 0; fi
	case " $power_management " in
	*" fid "*) return 0;; # frequency ID control
	esac
    fi
    return -1
}

probe_gsx() {
	if [ "$vendor" = "$CYRIX" -o "$vendor" = "$NSC" ]; then
		if isPciDevice 1078::0100 || isPciDevice 1078::0002 || isPciDevice 1078::0000; then return 0; fi
	fi
	return -1
}

probe_longhaul() {
	if [ "$vendor" = "$CENTAUR" ]; then
		if [ "$family" = "6" ]; then 
			if [ "$model" = "6" -o "$model" = "7" -o "$model" = "8" -o "$model" = "9" ]; then return 0; fi
		fi

	fi
return -1
}

probe_longrun() {
	if [ "$vendor" = "$TRANSMETA" -o "$vendor" = "$TRANSMETA1" ]; then
		if isFlag longrun; then return 0; fi; 
	fi
return -1
}

probe_nforce2() {
	if isPciDevice 10de:01e0; then return 0; else return -1; fi
}

probe_powerpc() {
	mobo=`LC_ALL=C grep "motherboard" /proc/cpuinfo | cut -d ":" -f 2 | head -1 | sed -e 's/ //'`
	mobo=" "$mobo" "
	#I hope we get always pvr like this:
	pvr=`LC_ALL=C grep "revision" /proc/cpuinfo | awk '{print $5}' | head -1`
	
	for board in $PPC; do
		#the spaces around $board make sure only whole words match:
		if [ "${mobo#* $board *}" != "$mobo" ]; then return 0; 
		fi
	done
	
	#Kernel contains a special case for the supported 750FX, 
	#not sure if we can use the cpu value so I use same test as kernel:
	if [ "$pvr" = "7000" ]; then 
		return 0; 
	fi
	
return -1
}

probe_chipset() {
	if probe_ich; then MODULE=speedstep-ich; return 0; fi;
	if probe_smi; then MODULE=speedstep-smi; return 0; fi;
	if [ "$KERNEL_TYPE" = "2.6" ]; then
		if probe_nforce2; then MODULE=cpufreq-nforce2; return 0; fi;
		if probe_gsx; then MODULE=gsx-suspmod; return 0; fi;
	fi
	if [ "`uname -m`" = "ppc" ]; then
		if probe_powerpc; then return 0; fi;
	fi
return -1
}

probe_cpu() {
	if probe_centrino; then MODULE=speedstep-centrino; return 0; fi;
	if probe_P4; then MODULE=p4-clockmod; return 0; fi;
	if probe_powernow_k6; then MODULE=powernow-k6; return 0; fi;
	if probe_powernow_k7; then MODULE=powernow-k7; return 0; fi;
	if probe_powernow_k8; then MODULE=powernow-k8; return 0; fi;
	if probe_longhaul; then MODULE=longhaul; return 0; fi;
	if probe_longrun; then MODULE=longrun; return 0; fi;
return -1
}

find_module() {
# First we try find a cpufreq compilant northbridge
	if probe_chipset; then
		# check whether we have centrino, by chance
		# otherwise it will get detected on ICH chipset and 
		# speedstep-ich doesn't work with it
		if probe_centrino; then 
			MODULE=speedstep-centrino
		fi
		return 0;
	else
		# Then we try to find a cpufreq compliant processor
		if probe_cpu; then
			return 0;
		fi
	fi

#No cpufreq device was found	
return -1		
}

unprobe_modules() {
	MODULE=""
	if find_module; then
		if [ "$KERNEL_TYPE" = "2.6" ]; then 
			rmmod cpufreq_performance 2>/dev/null
			rmmod cpufreq_ondemand 2>/dev/null
			rmmod cpufreq_userspace 2>/dev/null
			rmmod cpufreq_powersave 2>/dev/null
		fi
		if [ -z $MODULE ]; then return 0; fi
		
		rmmod $MODULE 2>/dev/null
		RETVAL=$?
		if [ $RETVAL -ne 0 ]; then return $RETVAL; fi
		if [ "$KERNEL_TYPE" = "2.6" ]; then 
			rmmod speedstep-lib 2>/dev/null
		fi
	fi
}
probe_modules() {
	MODULE="";
	if find_module; then
		#sometimes, no extra module might be needed:
		if [ -z $MODULE ]; then return 0; fi
		modprobe $MODULE 2>/dev/null
		return $?
	else
		return -1
	fi	
}

start() {
	gprintf "Probing cpufreq modules : "
	probe_modules
	RETVAL=$?	
	if [ $RETVAL -eq 0 ];  then
		if [ "$KERNEL_TYPE" = "2.6" ]; then
			modprobe cpufreq_performance 2>/dev/null 
			modprobe cpufreq_powersave 2>/dev/null 
			modprobe cpufreq_userspace 2>/dev/null 
			modprobe cpufreq_ondemand 2>/dev/null 
		fi
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/cpufreq
		success
		echo
	else
		RETVAL=-1
		failure
		echo 
	fi
	return $RETVAL
}
	
stop() {
	gprintf "Unloading cpufreq modules : "
	if unprobe_modules; then
		rm -f /var/lock/subsys/cpufreq
		success
		echo
		return 0 
	else
		failure
		echo
		return -1;
	fi
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		$0 stop
		$0 start	
		;;
	condrestart)
		if [ -f /var/log/subsys/cpufreq ]; then
			restart
		fi
		;;	
	status)
		
		;;
	*)
		gprintf "Usage: %s {start|stop|restart|condrestart|status}\n" `basename $0`
		RETVAL=1
		;;

esac

exit $RETVAL