1123626Snjl#!/bin/sh
2123626Snjl#
3123626Snjl# Modify the power profile based on AC line state.  This script is
4123626Snjl# usually called from devd(8).
5123626Snjl#
6123626Snjl# Arguments: 0x00 (AC offline, economy) or 0x01 (AC online, performance)
7123626Snjl#
8123626Snjl# $FreeBSD$
9123626Snjl#
10123626Snjl
11123626Snjl# PROVIDE: power_profile
12168283Sdes# REQUIRE: FILESYSTEMS syslogd
13136224Smtm# KEYWORD: nojail nostart
14123626Snjl
15123626Snjl. /etc/rc.subr
16123626Snjl
17123626Snjlname="power_profile"
18174464Sdougbstop_cmd=':'
19123626SnjlLOGGER="logger -t power_profile -p daemon.notice"
20123626Snjl
21123626Snjl# Set a given sysctl node to a value.
22123626Snjl#
23123626Snjl# Variables:
24123626Snjl# $node: sysctl node to set with the new value
25123626Snjl# $value: HIGH for the highest performance value, LOW for the best
26123626Snjl#	  economy value, or the value itself.
27123626Snjl# $highest_value: maximum value for this sysctl, when $value is "HIGH"
28123626Snjl# $lowest_value: minimum value for this sysctl, when $value is "LOW"
29123626Snjl#
30238416Skevlosysctl_set()
31123626Snjl{
32123626Snjl	# Check if the node exists
33123626Snjl	if [ -z "$(sysctl -n ${node} 2> /dev/null)" ]; then
34123626Snjl		return
35123626Snjl	fi
36123626Snjl
37123626Snjl	# Get the new value, checking for special types HIGH or LOW
38123626Snjl	case ${value} in
39123626Snjl	[Hh][Ii][Gg][Hh])
40123626Snjl		value=${highest_value}
41123626Snjl		;;
42123626Snjl	[Ll][Oo][Ww])
43123626Snjl		value=${lowest_value}
44123626Snjl		;;
45142572Snjl	[Nn][Oo][Nn][Ee])
46142572Snjl		return
47142572Snjl		;;
48123626Snjl	*)
49123626Snjl		;;
50123626Snjl	esac
51123626Snjl
52123626Snjl	# Set the desired value
53179965Smtm	if [ -n "${value}" ]; then
54179965Smtm		if ! sysctl ${node}=${value} > /dev/null 2>&1; then
55179965Smtm			warn "unable to set ${node}=${value}"
56179965Smtm		fi
57179965Smtm	fi
58123626Snjl}
59123626Snjl
60123626Snjlif [ $# -ne 1 ]; then
61123626Snjl	err 1 "Usage: $0 [0x00|0x01]"
62123626Snjlfi
63123626Snjlload_rc_config $name
64123626Snjl
65123626Snjl# Find the next state (performance or economy).
66123626Snjlstate=$1
67123626Snjlcase ${state} in
68123626Snjl0x01 | '')
69123626Snjl	${LOGGER} "changed to 'performance'"
70123626Snjl	profile="performance"
71123626Snjl	;;
72123626Snjl0x00)
73123626Snjl	${LOGGER} "changed to 'economy'"
74123626Snjl	profile="economy"
75123626Snjl	;;
76123626Snjl*)
77123626Snjl	echo "Usage: $0 [0x00|0x01]"
78123626Snjl	exit 1
79123626Snjlesac
80123626Snjl
81123626Snjl# Set the various sysctls based on the profile's values.
82123626Snjlnode="hw.acpi.cpu.cx_lowest"
83129021Snjlhighest_value="C1"
84240343Savglowest_value="Cmax"
85123626Snjleval value=\$${profile}_cx_lowest
86123626Snjlsysctl_set
87123626Snjl
88141417Snjlnode="dev.cpu.0.freq"
89142523Snjlhighest_value="`(sysctl -n dev.cpu.0.freq_levels | \
90142523Snjl	awk '{ split($0, a, "[/ ]"); print a[1] }' -) 2> /dev/null`"
91142523Snjllowest_value="`(sysctl -n dev.cpu.0.freq_levels | \
92238009Ssbruno	awk '{ split($0, a, "[/ ]"); print a[length(a) - 1] }' -) 2> /dev/null`"
93141417Snjleval value=\$${profile}_cpu_freq
94123626Snjlsysctl_set
95123626Snjl
96123626Snjlexit 0
97