📜 ⬆️ ⬇️

ACPI fine tuning on the example of the Thinkpad X220

Lyrics


Over the past couple of months, I often had to enter into discussions about all sorts of laptop computers: breaking the spears in disputes which manufacturer is better and why, discuss compatibility with GNU / Linux and how to configure this or that function in this system and, periodically, me asked to share configs. Under the influence of these very discussions, this article was born.

Most of my computer experience I use so-called “business series” laptops: IBM ThinkPad 600, HP-Compaq nc2400, Lenovo ThinkPad X61T, Lenovo ThinkPad X220.


')
There were short breaks, when you had to have a hard time anyhow, I mean computers of the consumer segment: Apple ibook G4 and Acer aspire 5112, and it was at these moments that an understanding of how much obvious and familiar things like trackpoint and docking station came.



What it is? Support for a laptop with a power connector and all sorts of additional ports. In some models, you can install an additional hard drive or battery. The docking station eliminates the need to connect the entire set of cables every time, coming to the workplace and disconnecting when leaving.

This configuration is especially convenient under GNU / Linux (in my case, Debian stable), where you can describe the behavior of a computer in several scripts.

Description of the scenario


At work, I use a configuration with an external monitor and a bluetooth Lenovo ThinkPad kt1255 keyboard. The laptop almost never turns off, instead it uses suspend to ram. I will describe handler scripts:

- Wake-up in the dock with the lid closed: turn on the external monitor, activate bluetooth and connect the keyboard, cpu performance
- Opening the lid in the dock: extinguish the external monitor and transfer the image to the internal
- Removing with the lid open: additionally disable the keyboard and bt, cpu on-demand
- Removing with the lid closed: lock the screen, transfer the image to the internal screen by turning off the external one, cut down the keyboard and bt-module, cpu in on-demand, switch to pm-suspend
- Closing the lid off the dock: screen lock, pm-suspend

Directly setting


I don’t use any DE, and I don’t see any reason to assign the function of power management to them. Therefore, we will configure by means of acpid.

We start the setup, as usual, with the installation of the necessary programs:
#apt-get install acpid acpi-support cpufrequtils 


Next, we look at what events we have generated when disconnecting a laptop:
 #acpi_listen ac_adapter ACPI0003:00 00000080 00000000 ibm/hotkey LEN0068:00 00000080 00006030 thermal_zone LNXTHERM:00 00000081 00000000 battery PNP0C0A:00 00000080 00000001 ibm/hotkey LEN0068:00 00000080 00004011 


When docked:
 #acpi_listen ibm/hotkey LEN0068:00 00000080 00004010 ac_adapter ACPI0003:00 00000080 00000001 ibm/hotkey LEN0068:00 00000080 00006030 thermal_zone LNXTHERM:00 00000081 00000000 battery PNP0C0A:00 00000080 00000001 button/lid LID close 


It is worth noting that the ThinkPad Mini Dock Series 3 does not provide a proc interface.

Let's write the rules for events:
 #vim /etc/acpi/events/thinkpad-dock event=ibm/hotkey LEN0068:00 00000080 00004010 action=/etc/acpi/thinkpad-dock.sh #vim /etc/acpi/events/thinkpad-undock event=ibm/hotkey LEN0068:00 00000080 00004011 action=/etc/acpi/thinkpad-undock.sh 


The rule for closing the cover comes with the acpi-support package. We will bring it to the proper form, indicating the path to the script handler:
 #vim /etc/acpi/events/lidbtn event=button[ /]lid action=/etc/acpi/lid.sh 


And now we come to the most interesting: scripts that describe the reaction to events under the conditions described above.

 #vim /etc/acpi/lid.sh #!/bin/bash #,     - grep -q on /etc/tp_dock_state; if [ $? = 0 ] then # ,     grep -q open /proc/acpi/button/lid/*/state if [ $? = 0 ] then # ,   ,   DISPLAY=:0.0 su user -c 'xrandr --output LVDS1 --auto' DISPLAY=:0.0 su user -c 'xrandr --output HDMI3 --off' else # ,  ,   DISPLAY=:0.0 su user -c 'xrandr --output HDMI3 --auto' DISPLAY=:0.0 su user -c 'xrandr --output LVDS1 --off' # bluetooth-    echo enabled> /proc/acpi/ibm/bluetooth /etc/init.d/bluetooth start # bluetooth . echo 'connect 90:7F:61:10:A3:BC'|bluetoothctl #    alt_gr,    DISPLAY=:0.0 su user -c 'setxkbmap -option grp:toggle us,ru' DISPLAY=:0.0 su user -c 'xmodmap ~/.xmodmaprc' fi else #   grep -q closed /proc/acpi/button/lid/*/state if [ $? = 0 ] then #  ,     bt- echo 'disconnect 90:7F:61:10:A3:BC'|bluetoothctl /etc/init.d/bluetooth stop echo disabled> /proc/acpi/ibm/bluetooth #    user   DISPLAY=:0.0 su user -c /home/user/bin/lock #     DISPLAY=:0.0 su user -c 'xrandr --output LVDS1 --auto' #  DISPLAY=:0.0 su user -c 'xrandr --output HDMI3 --off' #   pm-suspend #  ,    thinkpad: grep -q closed /proc/acpi/button/lid/*/state if [ $? = 0 ] then # , ,   : echo on >/etc/tp_dock_state #      DISPLAY=:0.0 su user -c 'xrandr --output HDMI3 --auto' DISPLAY=:0.0 su user -c 'xrandr --output LVDS1 --off' # bt-,  ,  bt- echo enabled> /proc/acpi/ibm/bluetooth /etc/init.d/bluetooth start echo 'connect 90:7F:61:10:A3:BC'|bluetoothctl #    DISPLAY=:0.0 su user -c 'setxkbmap -option grp:toggle us,ru' DISPLAY=:0.0 su user -c 'xmodmap ~/.xmodmaprc &' fi else # ,      DISPLAY=:0.0 su user -c 'xrandr --output LVDS1 --auto' fi 


Let me explain, just in case, that 90: 7F: 61: 10: A3: BC is the address of my keyboard. Bluetooth devices are configured quite simply:
 #apt-get install bluez #bluetoothctl [NEW] Controller 40:2C:F4:BB:3C:FC nethack [default] [bluetooth]# agent on Agent registered [bluetooth]# default-agent Default agent request successful [bluetooth]# scan on Discovery started [CHG] Controller 40:2C:F4:BB:3C:FC nethack [default] [NEW] Device 90:7F:61:10:A3:BC ThinkPad Compact Bluetooth Keyboard with TrackPoint [bluetooth]# pair 90:7F:61:10:A3:BC Attempting to pair with 90:7F:61:10:A3:BC [CHG] Device 90:7F:61:10:A3:BC Connected: yes [agent] PIN code: 12345 #   PIN [bluetooth]# connect 90:7F:61:10:A3:BC Attempting to connect to 90:7F:61:10:A3:BC Connection successful 


Further, when the devices are connected, it is enough to transmit
 #echo 'connect 90:7F:61:10:A3:BC'| bluetoothctl 

What we use in scripts.

We describe the docking handler:
 #vim /etc/acpi/thinkpad-dock.sh #!/bin/sh #   echo on >/etc/tp_dock_state #    performance cpufreq-set -c 0 -g performance cpufreq-set -c 1 -g performance cpufreq-set -c 2 -g performance cpufreq-set -c 3 -g performance # ,  ,   -   echo 'connect 90:7F:61:10:A3:BC'|bluetoothctl 


And undock:
 #vim /etc/acpi/thinkpad-undock.sh #!/bin/bash #   echo off >/etc/tp_dock_state #     cpufreq-set -c 0 -g powersave cpufreq-set -c 1 -g powersave cpufreq-set -c 2 -g powersave cpufreq-set -c 3 -g powersave #,   : grep -q closed /proc/acpi/button/lid/*/state if [ $? = 0 ] then # ,    DISPLAY=:0.0 su user -c /home/user/bin/lock # bt-,  ,   echo 'disconnect 90:7F:61:10:A3:BC'|bluetoothctl /etc/init.d/bluetooth stop echo disabled> /proc/acpi/ibm/bluetooth #     DISPLAY=:0.0 su user -c 'xrandr --output LVDS1 --auto' DISPLAY=:0.0 su user -c 'xrandr --output HDMI3 --off' #  pm-suspend #   ,    grep -q closed /proc/acpi/button/lid/*/state if [ $? = 0 ] then # ,        DISPLAY=:0.0 su user -c 'xrandr --output HDMI3 --auto' DISPLAY=:0.0 su user -c 'xrandr --output LVDS1 --off' # bt-,  ,   echo enabled> /proc/acpi/ibm/bluetooth /etc/init.d/bluetooth start echo 'connect 90:7F:61:10:A3:BC'|bluetoothctl #   echo on >/etc/tp_dock_state fi fi 


Underwater rocks


After upgrading to debian 8, where systemd appeared in the system, I encountered the following problem: when closing the lid, pm-suspend somehow worked twice. That is, the lid.sh script was executed, but in parallel with it, the pm-suspend action was performed without any script bindings.

The main suspect, of course, was the new initialization harvester. And for good reason: after ten minutes of googling, it turned out that, it turns out, systemd is trying, until the heap, to take over the acpi event management function, but it is not doing so well for the time being: at the moment it cannot handle the connection events of the power adapter and battery .

There was also no desire to rewrite the scripts for new fashion trends, therefore the following was done:
 #sed -i 's/HandleLidSwitch=yes/HandleLidSwitch=ignore/' /etc/systemd/logind.conf 

Thus, we have disaccustomed systemd to do what is not being asked and everything fell into place. In principle, the same should be done with HandleSuspendKey, HandleHibernateKey and HandlePowerKey, if there is a desire to handle clicks via acpid.

Conclusion


Thus, we get a convenient configuration that does not require any extra routine manipulations. Coming to work in the morning, I take the laptop out of my backpack, insert it into the docking station and press the power button on this docking station, after which I work as with a stationary computer. Leaving, just press the undock button and put the computer in the backpack.

The disadvantage of this approach is exactly one: it is highly addictive.

Source: https://habr.com/ru/post/272167/


All Articles