Skip to content

Setting up script for ds4 when connected

So I'm trying to edit the udev rule from ds4drv so it runs when its plugged in. I'm looking to see if udev can also stop the script once the controller is no longer paired.

My udev and script that is run:

cat /etc/udev/rules.d/50-ds4drv.rules
KERNEL=="uinput", MODE="0666"
#KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="05c4", MODE="0666"
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="05c4", MODE="0666" RUN+="/home/andrew/.config/ds4.sh"
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", KERNELS=="0005:054C:05C4.*", MODE="0666"
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="09cc", MODE="0666"
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", KERNELS=="0005:054C:09CC.*", MODE="0666"

cat /home/andrew/.config/ds4.sh
#!/usr/bin/env bash

# Terminate already running ds4drv instances
killall -q ds4drv

# Wait until the processes have been shut down
while pgrep -u $UID -x ds4drv >/dev/null; do sleep 1; done

ds4drv --hidraw --udp

I take it I just need a udev rule to check if the ds4 has been removed but I'm not seeing/googling successfully if udev has a disconnected rule.

Edited 24 Jan 2020. We only hold this version.

6 points4 comments
Open on Reddit
About this post
Posted
Archived
Edited
Score
6 · 100% up
Kind
Text
ID
etdkbs

Author

18
posts
103
comments
3
media
View profile

4 comments

Have you tried delegating to systemd? This is how I do in on my LibreELEC box:

udev.rules.d/10-sony_ds4.rules

ACTION=="add", SUBSYSTEM=="input", ATTRS{uniq}=="28:c1:xx:xx:xx:xx", SYMLINK+="sony_ds4", TAG+="systemd"

system.d/sony_ds4.service

[Unit]
BindsTo=dev-sony_ds4.device
After=dev-sony_ds4.device

[Install]
WantedBy=dev-sony_ds4.device

[Service]
Type=oneshot

ExecStart=ds4_systemd.sh

I only run a script on connect - so you'd have to adapt it to your use case (ExecStop etc), but systemd units are less of a pain in the ass than udev rules imo.

3 points

No I haven't sounds great but I am still confused after read the wiki bit on systemd units.

I made /etc/systemd/system/sony_ds4.service

[Unit]
BindsTo=dev-sony_ds4.device
After=dev-sony_ds4.device

[Install]
WantedBy=dev-sony_ds4.device

[Service]
Type=oneshot
User=andrew

ExecStart=/home/andrew/ds4.sh

Edited /etc/udev/rules.d/50-ds4drv.rules

KERNEL=="uinput", MODE="0666"
#KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="05c4", MODE="0666"
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="05c4", ATTRS{address}=="40:1b:5f:85:bc:42", SYMLINK+="sony_ds4", TAG+="systemd", MODE="0666"
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", KERNELS=="0005:054C:05C4.*", MODE="0666"
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="09cc", MODE="0666"
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", KERNELS=="0005:054C:09CC.*", MODE="0666"

Testing:

sudo systemctl start /etc/systemd/system/sony_ds4.service
Failed to start etc-systemd-system-sony_ds4.service.mount: Unit etc-systemd-system-sony_ds4.service.mount not found.

The User= would be me wanting to eventually have this run as a non root user, but I'm far from fully grasping this yet. Is systemd looking for the wrong mount just because my bindsto= and after= are wrong?

2 points

It's been a while since I set this up (it took me hours of reading udev/systemd man pages, apparently I immediately suppressed the memories of what I did exactly). Once enabled, it should start/stop automatically on connect/disconnect (tracking the device state via sysfs).

I find stuff like this hard to debug remotely, but first I'd check that the udev rule works correctly and systemd correctly tracks the device.

When you pair the ds4, udev should create the symlink /dev/sony_ds4 (that's the SYMLINK+= part of the udev rule) and

systemctl status dev-sony_ds4.device

should get you something like this:

dev-sony_ds4.device - /dev/sony_ds4
Follow: unit currently follows state of sys-devices-pci0000:00-0000:00:14.0-us...
 Loaded: loaded
 Active: active (plugged) since Fri 2020-01-24 23:34:53 CET; 6s ago          
 Device: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-4/1...

If that's working, then set up the systemd service unit file.

I'm afraid these are all the pointers I can come up with at the moment, but please do document if/how you get it working. Maybe someone else can help.

Edit: Oh, and don't use the full path to service unit files with systemctl. Just systemctl enable sony_ds4.service. that's what the "not found" error is about when you're running systemctl.

Also note that my setup only has one controller hardcoded because that's all I needed.

1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.