#!/bin/sh
#
# Simple script to alter kernel parameters for CPUMODE.d
# Andrew Wyatt
#
# Sysctl script for altering parameters when shifting to AC mode.
#
# Add any custom kernel parameters here.
#

if [ -e "/etc/sysconfig/ktune" ]
then
  . /etc/sysconfig/ktune
fi

if [ "${CPUPOWER_AC}" ]
then
  cpupower frequency-set -g ${CPUPOWER_AC}
else
  cpupower frequency-set -g performance
fi

echo 1 > /proc/sys/kernel/nmi_watchdog
echo 0 > /proc/sys/vm/laptop_mode
echo 500 > /proc/sys/vm/dirty_writeback_centisecs


### Automatic device power management management
if [ ! "${DEVICE_PM}" = "disabled" ]
then
  echo 0 > /sys/module/snd_hda_intel/parameters/power_save
  echo high > /sys/class/drm/card0/device/power_profile

  for fdevice in /sys/bus/{pci,spi,i2c}/devices/*; do
    echo on > $fdevice/power/control
  done

  for sdevice in /sys/class/scsi_host/host*/link_power_management_policy; do
    echo max_performance > $sdevice
  done
fi

if [ ! "${USB_PM}" = "disabled" ]
then
  for usb in /sys/bus/usb/devices/*/power/autosuspend; do
    echo 2 > $usb;
  done
fi

### WIFI power management
if [ ! "${WIFI_PM}" = "disabled" ]
then
  WIFI_ADAPTER=$(find /sys/class/net/*/ | grep wireless | head -n 1 | sed -s -e "s#^/sys.*net/##" -e "s#/wireless.*\$##")
  /sbin/iw dev $WIFI_ADAPTER set power_save off
fi

### SHE and GMA overclock support
if [ ! "${EEE_PM}" = "disabled" ]
then
  if [[ $(dmidecode -s processor-version) =~ "Atom" ]]; then
    if [[ $(lspci -s 00:02.0) =~ "945GME" ]]; then
      /sbin/setpci -s 00:02.0 f0.b=00,60
      /sbin/setpci -s 00:02.0 f0.b=33,05
    fi
  fi

  if [ -e "/sys/devices/platform/eeepc/she" ]; then
    SHE_DEVICE="/sys/devices/platform/eeepc/she"
  elif [ -e "/sys/devices/platform/eeepc/cpufv" ]; then
    SHE_DEVICE="/sys/devices/platform/eeepc/cpufv"
  else
    exit 0
  fi

  if [ "$SHE_DEVICE" ]; then
    echo "0" > $SHE_DEVICE
  fi
fi
