Tag Archives: docking station

Thinkpad X220 and Ultrabase 3 Docking Station

As I promised, there will be more posts related to the Fedora 16 configuration on a Thinkpad X220. This one will be about the seamless docking/undocking of the notebook from the dock.

It seems that this is largely still a grey area and is something that rarely seems to work as expected, so I had to hack it away, because this one was one of the biggest problems that I face every day because I take the notebook every day at work and dock it so I’m able to use is as a regular desktop PC and¬† in the afternoon I take it with me at home (one never knows for sure where he’ll end up the next day – at work or at a customer’s site ūüôā

What I was aiming was achieving effortless X setup and display switching under the following conditions:

1. When booting the notebook – should work as expected – show KDM on external display when docked and on local LVDS display when not

2. Proper switching when docking / undocking the notebook and there is active user X session

3. Proper switching when docking / undocking the notebook when KDM is active

4. Proper switching when the notebook is docked / undocked during it was suspended

To achieve this I’ve ended up with a simple bash script that verifies the current configuration (docked / undocked) and switches the display accordingly. What was left was to put it in the all appropriate places, so that it gets executed on all of the above listed conditions. So first about the script – it still a work in progress, but is mostly working properly. I’ll update it here when it becomes more polished. Bare in mind that this was written with KDM in mind, although it shouldn’t be so difficult to be adapted for GDM or other login manager.

The Script (thinkpad-dock.sh)


#!/bin/sh
# wait for the dock state to change
sleep 0.5

export IFS=$"\n"

DOCKED=$(cat /sys/devices/platform/dock.0/docked)

# invoke from XSetup with NO_KDM_REBOOT otherwise you'll end up in a KDM reboot loop
NO_KDM_REBOOT=0
for p in $*; do
case "$p" in
"NO_KDM_REBOOT")
NO_KDM_REBOOT=1
;;
"SWITCH_TO_LOCAL")
DOCKED=0
;;
esac
done

function switch_to_local {
export DISPLAY=$1
export XAUTHORITY=$(find /var/run/kdm -name "A${DISPLAY}-*")

# Turn off all external displays
for output in $(/usr/bin/xrandr --verbose|grep "+"|grep connected| grep -v LVDS|awk '{print $1}'); do
logger -t DOCKING "Switching off $DISPLAY"
/usr/bin/xrandr --output $output --off
done

# Turn on local display
logger -t DOCKING "Switching on LVDS1"
/usr/bin/xrandr --output LVDS1 --auto
}

function switch_to_external {
export DISPLAY=$1
export XAUTHORITY=$(find /var/run/kdm -name "A${DISPLAY}-*")

# The Display port on the docking station is on HDMI2 - lets use it and turn of local display
logger -t UNDOCKING "Switching off LVDS1 and turning on HDMI2"
/usr/bin/xrandr --output HDMI2 --primary --left-of LVDS1
/usr/bin/xrandr --output LVDS1 --off
/usr/bin/xrandr --output HDMI2 --primary --auto
}

case "$DOCKED" in
"0")
#undocked event
CONNECTED_USER=0
# Check for logged users with X sessions
for u in $(w | awk '{print $1" "$2}' | grep ' :[0-9]'); do
CONNECTED_USER=1
switch_to_local $(echo $u | awk '{print $2}')
done
if [ $CONNECTED_USER -eq 0 ] && [ ! $NO_KDM_REBOOT ]; then
# we are at login screen
logger -t KDM_DOCKING "Reloading KDM after X setup"
kill $(cat /var/run/kdm/kdm.pid)
fi
;;
"1")
#docked event
CONNECTED_USER=0
for u in $(w | awk '{print $1" "$2}' | grep ' :[0-9]'); do
CONNECTED_USER=1
switch_to_external $(echo $u | awk '{print $2}')
done
if [ $CONNECTED_USER -eq 0 ] && [ ! $NO_KDM_REBOOT ]; then
logger -t KDM_DOCKING "Reloading KDM after X setup"
kill $(cat /var/run/kdm/kdm.pid)
fi
;;
esac

The Hooks

There are several places where you should put an invoke this script.

1. KDM

Obviously you need it to be run at KDM startup when X is initialized. You need to add this line at the and of /etc/kde/kdm/Xsetup file:

/usr/local/sbin/thinkpad-dock.sh NO_KDM_REBOOT

Where /usr/local/sbin/thinkpad-dock.sh is the location of the above script.

One more thing about the KDM – in order to get proper looking KDM login screen on the external display you’ll need to alter the xorg.conf, otherwise you”ll get a little borked KDM background.

Add this file to /etc/X11/xorg.conf.d/ folder


$ cat /etc/X11/xorg.conf.d/01-external-monitor.conf
Section "Device"
Identifier      "HD3000"
Driver         "intel"
Option          "monitor-LVDS1" "local-display"
Option          "monitor-HDMI2" "external-display"
EndSection

Section "Monitor"
Identifier      "local-display"
Option "Enable" "false"
EndSection

Section "Monitor"
Identifier      "external-display"
Option "PreferredMode" "1680x1050"
Option "Position"       "0 0"
EndSection

Be carefull here as this instructs X to NOT enable by default the local display – it will be “later” activated when KDM is started and thinkpad-dock.sh is invoked.

2. Docking / Undocking events

You’ll need to add this file in /etc/udev/rules.d to catch all dock/undock events through udev:


$ cat 81-thinkpad-dock.rules
KERNEL=="dock.0", ACTION=="change", RUN+="/usr/local/sbin/thinkpad-dock.sh"

3. Suspend / Resume events

Finally you’ll need to ensure and check that the configuration was not changed while the notebook was suspended. To hook the script you’ll need to add this in /etc/pm/sleep.d/

$ cat 00check_dock
#!/bin/bash

case "$1" in
hibernate|suspend)
# If sleeping - pretend undocking
/usr/local/sbin/thinkpad-dock.sh SWITCH_TO_LOCAL
;;
thaw|resume)
# If resuming verify docking station presence
/usr/local/sbin/thinkpad-dock.sh
;;
esac

Only one thing to be noted here – the script needs to be executed only during the resume process, but because of some problems that I had during resuming on local display when system was suspended on the dock I had to add the hook also during suspend state and force the X server to be switched over the local display in case the machine is undocked while suspended.

EDIT:

You’ll also need to disable the KDE display management systems, so it does not ask you every time for display reconfiguration because a new display was detected. The only thing that should be done is to stop the service itself like this:

That’s all – there still some quirks that should be polished and may be the script must be made some more general – like defining some variables at the beginning, but I’ll do that later. In general it just works, is quite simple to implement I think and finally the dock is out of my way ūüôā

Advertisements
Tagged , , , ,