Skip to content

Commit 8f1815e

Browse files
committed
Improve serial symlink (/dev/serial<n>) creation
The kernel change that sets UART indexes based on Device Tree aliases gives predictable UART /dev/tty* names but has the potential to break our downstream use for determining which UART is the console ("serial0") and which is for Blueooth ("serial1"). Instead we use the "console" and "bluetooth" aliases respectively. As a bonus, the improved console matching (based on /sys/class/tty entries) also allows us to handle all UARTs with one rule.
1 parent 5ce3ef2 commit 8f1815e

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

etc.armhf/udev/rules.d/99-com.rules

+18-33
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,24 @@ SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chgrp -R gpio /sys%p && c
1414
# PWM export results in a "change" action on the pwmchip device (not "add" of a new device), so match actions other than "remove".
1515
SUBSYSTEM=="pwm", ACTION!="remove", PROGRAM="/bin/sh -c 'chgrp -R gpio /sys%p && chmod -R g=u /sys%p'"
1616

17-
KERNEL=="ttyAMA0", PROGRAM="/bin/sh -c '\
18-
ALIASES=/proc/device-tree/aliases; \
19-
if cmp -s $$ALIASES/uart0 $$ALIASES/serial0; then \
20-
echo 0;\
21-
elif cmp -s $$ALIASES/uart0 $$ALIASES/serial1; then \
22-
echo 1; \
23-
else \
24-
exit 1; \
25-
fi\
26-
'", SYMLINK+="serial%c"
27-
28-
KERNEL=="ttyAMA1", PROGRAM="/bin/sh -c '\
29-
ALIASES=/proc/device-tree/aliases; \
30-
if [ -e /dev/ttyAMA0 ]; then \
31-
exit 1; \
32-
elif cmp -s $$ALIASES/uart0 $$ALIASES/serial0; then \
33-
echo 0;\
34-
elif cmp -s $$ALIASES/uart0 $$ALIASES/serial1; then \
35-
echo 1; \
36-
else \
37-
exit 1; \
38-
fi\
39-
'", SYMLINK+="serial%c"
40-
41-
KERNEL=="ttyS0", PROGRAM="/bin/sh -c '\
42-
ALIASES=/proc/device-tree/aliases; \
43-
if cmp -s $$ALIASES/uart1 $$ALIASES/serial0; then \
44-
echo 0; \
45-
elif cmp -s $$ALIASES/uart1 $$ALIASES/serial1; then \
46-
echo 1; \
47-
else \
48-
exit 1; \
49-
fi \
17+
KERNEL=="ttyAMA[0-9]*|ttyS[0-9]*", PROGRAM="/bin/sh -c '\
18+
ALIASES=/proc/device-tree/aliases; \
19+
TTYNODE=$$(readlink /sys/class/tty/%k/device/of_node | sed 's/base/:/' | cut -d: -f2); \
20+
if [ -e $$ALIASES/bluetooth ] && [ $$TTYNODE/bluetooth = $$(strings $$ALIASES/bluetooth) ]; then \
21+
echo 1; \
22+
elif [ -e $$ALIASES/console ]; then \
23+
if [ $$TTYNODE = $$(strings $$ALIASES/console) ]; then \
24+
echo 0;\
25+
else \
26+
exit 1; \
27+
fi \
28+
elif [ $$TTYNODE = $$(strings $$ALIASES/serial0) ]; then \
29+
echo 0; \
30+
elif [ $$TTYNODE = $$(strings $$ALIASES/serial1) ]; then \
31+
echo 1; \
32+
else \
33+
exit 1; \
34+
fi \
5035
'", SYMLINK+="serial%c"
5136

5237
ACTION=="add", SUBSYSTEM=="vtconsole", KERNEL=="vtcon1", RUN+="/bin/sh -c '\

0 commit comments

Comments
 (0)