Skip to content

Instantly share code, notes, and snippets.

@madchap
Last active February 5, 2019 22:13
Show Gist options
  • Select an option

  • Save madchap/b5e6f9baf489d127b5ca2dbcd71adf63 to your computer and use it in GitHub Desktop.

Select an option

Save madchap/b5e6f9baf489d127b5ca2dbcd71adf63 to your computer and use it in GitHub Desktop.
Dell TB16 Thunderbolt udev

When plugging my Dell XPS13 (9360) to my TB16, which is connected to 2x 4k monitors, I want to turn off my laptop monitor. When unplugging the laptop from the docking, I want to turn the laptop monitor back-on.

system info

4.20.2-1-default

NAME="openSUSE Tumbleweed"
# VERSION="20190126"
ID="opensuse-tumbleweed"
ID_LIKE="opensuse suse"
VERSION_ID="20190126"
PRETTY_NAME="openSUSE Tumbleweed"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:tumbleweed:20190126"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"

udev

info - when docking is plugged in

# udevadm info -ap /devices/pci0000:00/0000:00:1c.0/0000:01:00.0/0000:02:00.0/0000:03:00.0/domain0/0-0/0-1/0-301

  looking at device '/devices/pci0000:00/0000:00:1c.0/0000:01:00.0/0000:02:00.0/0000:03:00.0/domain0/0-0/0-1/0-301':
    KERNEL=="0-301"
    SUBSYSTEM=="thunderbolt"
    DRIVER==""
    ATTR{authorized}=="1"
    ATTR{boot}=="0"
    ATTR{device}=="0xb054"
    ATTR{device_name}=="Dell Thunderbolt Dock"
    ATTR{nvm_authenticate}=="0x0"
    ATTR{nvm_version}=="16.0"
    ATTR{unique_id}=="d3010000-00a2-8d18-a218-f4d4a2c2aa18"
    ATTR{vendor}=="0xd4"
    ATTR{vendor_name}=="Dell"

info - When dock is not plugged in

This will be important to notice in the remove rule.

# udevadm monitor --environment --udev

UDEV  [36689.512928] remove   /devices/pci0000:00/0000:00:1c.0/0000:01:00.0/0000:02:00.0/0000:03:00.0/domain0/0-0/0-1/0-301 (thunderbolt)
ACTION=remove               
DEVPATH=/devices/pci0000:00/0000:00:1c.0/0000:01:00.0/0000:02:00.0/0000:03:00.0/domain0/0-0/0-1/0-301
DEVTYPE=thunderbolt_device  
SEQNUM=12594                
SUBSYSTEM=thunderbolt       
USEC_INITIALIZED=36689419102

udev rules

Using at (enable atd through systemctl)

ACTION=="add", SUBSYSTEM=="thunderbolt", ATTR{vendor}=="0xd4", ATTR{device}=="0xb054", RUN="/usr/bin/at -M -f /home/fblaise/bin/udev_plug_dock.sh now"

ACTION=="remove", SUBSYSTEM=="thunderbolt", ENV{DEVPATH}=="/devices/pci0000:00/0000:00:1c.0/0000:01:00.0/0000:02:00.0/0000:03:00.0/domain0/0-0/0-1/0-301", RUN="/usr/bin/at -M -f /home/fblaise/bin/udev_unplug_dock.sh now"

# https://www.linuxquestions.org/questions/linux-desktop-74/udev-not-doing-remove-rules-841733/
# no access to ATTR upon remove and not much ENV variables available, so cannot re-use the same.

tests

udevadm test --action=add /devices/pci0000:00/0000:00:1c.0/0000:01:00.0/0000:02:00.0/0000:03:00.0/domain0/0-0/0-1/0-301
udevadm test --action=remove /devices/pci0000:00/0000:00:1c.0/0000:01:00.0/0000:02:00.0/0000:03:00.0/domain0/0-0/0-1/0-301

reload udev

# udevadm control --reload

Rules should be reloaded via inotify.

tips for scripts running xrandr through udev

  • export DISPLAY=:0
  • export XAUTHORITY=/home/username/.Xauthority
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment