Gentoo Forums
Gentoo Forums
Quick Search: in
ACPID scripts for thinkpad T42
View unanswered posts
View posts from last 24 hours

rackathon
 
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
Antimatter
Guru
Guru


Joined: 11 Aug 2003
Posts: 450

PostPosted: Mon Nov 21, 2005 5:06 pm    Post subject: ACPID scripts for thinkpad T42 Reply with quote

Here's my current ACIPD scripts, its not complete quite yet, but i'm looking for some critiques on how to improve it and/or hopfully make it more effective, the case statement for detecting the correct events, and a few functions such as susp2ram and susp2disk haven't been written yet because to be frank, i have no idea about susp2ram/disk and at the moment i'm doing research on this.

Code:

# This is a customized script to take care of all the events related
# with ACPI, its designed to be as "modular" as possiable

#-------------------
# Paths to programs
#-------------------

LOGGER="/usr/bin/logger -t ACPID" # Log to syslog & /var/log/acpid

LAPTOPMODE="/sbin/laptop-mode"
HDPARM="/sbin/hdparm"

#XGAMMA="/usr/X11R6/bin/xgamma"
#XSET="/usr/X11R6/bin/xset"

SETTERM="/usr/bin/setterm"
RADEONTOOL="/usr/bin/radeontool"

CPUFREQ_SET="/usr/bin/cpufreq-set"

IWCONFIG="/sbin/iwconfig"

SHUTDOWN="/sbin/shutdown"

#--------------------------
# Paths to system settings
#--------------------------

# This is the file that is echoed into, to set the suspend mode
POWER="/sys/power/state"


#-----------------------
# Path to ACPI "Events"
#-----------------------

BAT_STATE="/proc/acpi/battery/BAT0/state"
BAT_INFO="/proc/acpi/battery/BAT0/info"

AC_STATE="/proc/acpi/ac_adapter/AC/state"

LID_STATE="/proc/acpi/button/lid/LID/state"


#---------------------------
# Path to IBM_ACPI "Events"
#---------------------------

BAY="/proc/acpi/ibm/bay"

BRIGHTNESS="/proc/acpi/ibm/brightness"

VIDEO="/proc/acpi/ibm/video"


#----------
# Settings
#----------

# Wireless Module to modprobe
WMP="ipw2200"

# Wireless Modules to rmmod
WMR="ipw2200 firmware_class ieee80211 ieee80211_crypt"


# Wireless interfance
WIRELESS="eth1"

# Max time before puting card back into power saving mode in seconds
BAT_PEROID_MIN=0
BAT_PEROID_MAX=3

# What packadge to accept, (all, unicast, multicast)
BAT_PACKADGE=unicast


# CPU frequency limiter, in other word max and minium frequency allowed (Khz)
AC_MIN=600000
AC_MAX=1700000

BAT_MIN=600000
BAT_MAX=1700000



# Harddrive spindown settings (consult man hdparm for info)
AC_SPINDOWN=253
BAT_SPINDOWN=12

# Harddrive APM Power management (consult man hdparm for info)
AC_HD_APM=255
BAT_HD_APM=1

# Harddrive Automatic Acoustic Management (consult man hdparm for info)
AC_HD_AAM=254
BAT_HD_AAM=128



# Brightness of laptop screen ( 0 - 7 ) - 0 = dimmest, 7 = brightest
AC_BRIGHTNESS=0
BAT_BRIGHTNESS=0

# Gamma of the screen in X-server ( 0.0 - 1.0 ) - 0.0 = 0%, 1.0 = 100%
AC_GAMMA=1.0
BAT_GAMMA=0.7


# These below i'm not sure because of the x-server dpms?

# Time to blank the display in X-server and the terminal (in seconds)
AC_DISP_BLANK=600
BAT_DISP_BLANK=600


# Time to put the display into standby in X-server
#AC_DISP_STANDBY
#BAT_DISP_STANDBY

# Time to put the display in suspend in X-server
#AC_DISP_SUSPEND
#BAT_DISP_SUSPEND

# Time to shutdown the display in X-server and the terminal
#AC_DISP_OFF
#BAT_DISP_OFF


# Minute from criticaly low that the laptop mode should shutoff so the
# laptop can shutdown faster and so forth.
BAT_LAPTOP_MODE_OFF=10


# Delays in shutdown
SD_DELAY="now"


#-----------
# Functions
#-----------


# Check configations for the system
checkconfig ()
{
    if [ ! -f /proc/sys/vm/laptop_mode ]
    then
        echo "### KERNEL DOES NOT SUPPORT LAPTOP_MODE ###"
   
   # Log this as a critical error in the logs
   $LOGGER -p daemon.crit "### KERNEL DOES NOT SUPPORT LAPTOP_MODE ###"
   return 1
    else
   return 0
    fi
}




# This is called if this script is invoked with "start"
# This is where all of the inital ACPI settings are placed
# such as the governor and so forth.
start_acpi ()
{
    $LOGGER -p daemon.info "Setting inital ACPI settings"

    # Sets the governor to be conservative and sets a few
    # related conservative settings
    $CPUFREQ_SET -g conservative

    # Ignore niced processes in load calucation
    echo 1 > /sys/devices/system/cpu/cpu0/cpufreq/conservative/ignore_nice

    # The governor checks every X micro-second to see if it needs
    # to raise the cpu frequency a step, now it checks every
    # ( sampling_down_factor * X ) to drop the cpu frequency a step.
    echo 2 > /sys/devices/system/cpu/cpu0/cpufreq/conservative/sampling_down_factor


    # Determinite which power mode the system is on: AC or Battery?
    if [[ `awk '/state/ { print $2}' $AC_STATE` == "on-line" ]]
    then
   ac_in
    else
   ac_out
    fi
}




# If this script runs into a unknown event,
# this function is ran.
no_action ()
{
    echo "### NO ACTION FOR EVENT: " $*
    $LOGGER -p daemon.warn "### NO ACTION FOR EVENT: " $*
}




# AC adapter is plugged in
ac_in ()
{
    $LOGGER -p daemon.info "AC Adapter plugged IN"


    # Setting the max and minium allowable cpu frequency
    $CPUFREQ_SET -d $AC_MIN -u $AC_MAX


    # If the X-server is running then set the X-server related settings here
    if ps -A | grep -q X
    then
   # $XGAMMA -d 0:0 -gamma $AC_GAMMA
   # $XSET -d 0:0 dpms 0 0 $AC_DISP_BLANK
    fi

    # Set the terminal screen to blank out after X minute
    $SETTERM -blank ( $AC_DISP_BLANK / 60 )

    # Set the LCD Brightness
    echo level $AC_BRIGHTNESS > $BRIGHTNESS


    # Turning off the power managment for the wireless card
    $IWCONFIG $WIRELESS power off


    # Stops the laptop-mode
    $LAPTOPMODE stop


    # Reset the harddrive spindown time delay back to normal
    $HDPARM -q -S $AC_SPINDOWN /dev/hda

    # Reset the harddrive APM power management to off
    $HDPARM -q -B $AC_HD_APM /dev/hda

    # Reset the Automatic Acoustic Management settings
    $HDPARM -q -M $AC_HD_AAM /dev/hda     
}



# AC adapter is unplugged
ac_out ()
{
    $LOGGER -p daemon.info "AC Adapter plugged OUT"
   
    # Setting the max and minium allowable cpu frequency
    $CPUFREQ_SET -d $BAT_MIN -u $BAT_MAX


    # If the X-server is running then set the X-server related settings here
    if ps -A | grep -q X
    then
   # $XGAMMA -d 0:0 -gamma $BAT_GAMMA
   # $XSET -d 0:0 dpms 0 0 $BAT_DISP_BLANK
    fi


    # Set the terminal screen to blank out after X minute
    $SETTERM -blank ( $BAT_DISP_BLANK / 60 )

    # Set the LCD Brightness
    echo level $BAT_BRIGHTNESS > $BRIGHTNESS


    # Turning on the power managment for the wireless card
    $IWCONFIG $WIRELESS power on power min peroid $BAT_MIN_PEROID \
   max period $BAT_MAX_PEROID $BAT_PACKADGES


    # Starts the laptop-mode
    if [ check == 0 ]
    then
   $LAPTOPMODE auto
    else
   exit 1
    fi


    # Set the harddrive spindown delay
    $HDPARM -q -S $BAT_SPINDOWN /dev/hda

    # Set the harddrive APM power management to on
    $HDPARM -q -B $BAT_HD_APM /dev/hda

    # Set the Automatic Acoustic Management settings
    $HDPARM -q -M $BAT_HD_AAM /dev/hda   
}


# State of the battery has changed
# if the battery is low shut down
battery ()
{
    if grep -q discharging $BAT_STATE
    then
   BAT_REMAIN=`awk '/remaining/ { print $3 }' $BAT_STATE`
   BAT_DISCHARGE=`awk '/rate/ { print $3 }' $BAT_STATE`
   BAT_WARN=`awk '/warning/ { print $4 }' $BAT_INFO`

   BAT_WARN_LAPTOP_MODE=( ( $BAT_DISCHARGE / 60 ) * $BAT_LAPTOP_MODE_OFF )\
       + BAT_WARN

   if (( $BAT_REMAIN < $BAT_WARN_LAPTOP_MODE ))
   then
       # Log this to let the system know that there's only
       # a few minute left of battery before it hit the warning
       # level of the battery pack
       $LOGGER -p daemon.info "Few minute of battery life left before its criticaly low"

       # Battery is below the laptop thershold, so its
       # a good time to shut down the laptop mode so the
       # system can start to flush data to the harddrive
       $LAPTOPMODE stop
   fi

        if (( $BAT_REMAIN < $BAT_WARN ))
   then
       $LOGGER -p daemon.warn "Battery capacity is criticaly low"

       # shutdown the system      
       pwr_btn
   fi
}


# Power button support
# if the power button is pressed and x-server is running
# the user will get a dialog asking what to do
pwr_btn ()
{
  # For the moment, there's not much to be done here


  # flush data to the harddrive then shutdown the system.
  $HDPARM -f /dev/hda

  # Power down the system
  $SHUTDOWN -h $SH_DELAY 
}


# lid switch (closed / opened)
# useful for turning on and off screen and so forth
lid_btn ()
{
  if grep -q open $LID_STATE
  then
     $LOGGER -p daemon.info "Lid opened"

   # X-windows way of turning the display on
   # $XSET dpms force on <- not sure
   
   # Radeon tool way of turning on the display
   $RADEONTOOL light on            
  else
     $LOGGER -p daemon.info "Lid closed"

   # X-windows way of turning the display off
   # $XSET dpms force off
   
   # Radeon tool way of turning off the display
   $RADEONTOOL light off   
  fi
}


# Display blanking (FN + F3)
# used to blank the display, or if already blanked bring it back
blank_btn ()
{
  # Check the BLANK env varriable, if set to an one, then
  # tongle it back to zero and start the screen back up, other
  # wise tongle it to an one and shutdown the screen
  if ( $BLANK == 1 )
  then
   # Tongle it to a zero
   $BLANK = 0
   
   $LOGGER -p daemon.info "Display on" 

   # X-windows way of turning the display on
   # $XSET dpms force on
   
   # Radeon tool way of turning on the display
   # Along with turning on the exterial display
   $RADEONTOOL light on
   $RADEONTOOL dac on   
  else
     # Tongle it to an one
     $BLANK = 1
     
     $LOGGER -p daemon.info "Display off"

   # X-windows way of turning the display off
   # $XSET dpms force off
   
   # Radeon tool way of turning off the display
   # Along with turning off the exterial display
   $RADEONTOOL light off
   $RADEONTOOL dac off   
  fi
}


# Suspend to RAM (FN + F4)
# Used to suspend the system to RAM
susp_btn ()
{

}


# Wireless on and off switch (FN + F5)
# Used to turn the wireless on and off
wireless_btn ()
{
  # Check the RF_KILL env varriable, if set to an one,
  # then tongle it to a zero, other wise tongle it
  # back from a zero to an one
  if ( $RF_KILL == 1 )
  then
     # RF_KILL is a one, so set it to a zero
   $RF_KILL = 0 

   $LOGGER -p daemon.info "Wireless Activated"
   
   # Modprobe the needed modules
   modprobe $WMP
   
   # Re-enable the radio and power up the card
   #echo 0 > /sys/class/net/eth1/power/state
   #echo 0 > /sys/class/net/eth1/device/rf_kill
   # /sys/bus/pci/drivers/ipw2200/0000:02:02.0/* for another
   # source of sys stuff to alter on the wireless card

   
   # Start up the wireless network interfance
   /etc/init.d/net.$WIRELESS start
   
  else
     # RF_KILL is an zero, so set it to an one
     $RF_KILL = 1

   $LOGGER -p daemon.info "Wireless Deactivated"
   
   # Shutdown the wireless network interface
   /etc/init.d/net.$WIRELESS stop

   # de-activate the radio and power down the card
   #echo 3 > /sys/class/net/eth1/power/state
   #echo 1 > /sys/class/net/eth1/device/rf_kill

   
   # Remove the needed modules
   rmmod $WMR
  fi
}


# Screen Switching (FN + F7)
# Used to switch and support LCD, CRT, TV modes
switch_btn ()
{

}


# Suspend to Disk (FN + F12)
# Used to suspend to harddrive
hdd_btn ()
{

}



Any suggestion for improvement and/or things that i may want to add to the scripts.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum