Skip to content

Instantly share code, notes, and snippets.

@galenseitz
Forked from Leo-PL/10-persistent-serial.sh
Last active May 11, 2024 18:43
Show Gist options
  • Save galenseitz/f3b2c4a45557eebe992dc637a1db070b to your computer and use it in GitHub Desktop.
Save galenseitz/f3b2c4a45557eebe992dc637a1db070b to your computer and use it in GitHub Desktop.
Persistent serial port names for USB-serial on OpenWrt hotplugd
[ "${ACTION}" = "bind" -o "${ACTION}" = "unbind" ] || exit 0
[ "${SUBSYSTEM}" = "usb-serial" ] || exit 0
[ -n "${DEVICENAME}" -a -n "${DEVPATH}" ] || exit 1
if [ "${ACTION}" = "bind" ]; then
subsystem="$(basename $(readlink /sys${DEVPATH}/../subsystem))"
[ "$subsystem" = "usb" ] || exit 0
replace_whitespace="s/^[ \t]*|[ \t]*$//g; s/[ \t]+/_/g"
manufacturer="$(sed -E "${replace_whitespace}" /sys${DEVPATH}/../../manufacturer)"
# If manufacturer is blank, use vendor ID.
: ${manufacturer:=$(cat /sys${DEVPATH}/../../idVendor)}
product="$(sed -E "${replace_whitespace}" /sys${DEVPATH}/../../product)"
# If product is blank, use product ID.
: ${product:=$(cat /sys${DEVPATH}/../../idProduct)}
serial="$(sed -E "${replace_whitespace}" /sys${DEVPATH}/../../serial)"
interface="$(cat /sys${DEVPATH}/../bInterfaceNumber)"
port="$(cat /sys${DEVPATH}/port_number)"
replace_chars="s/[^0-9A-Za-z#+.:=@-]/_/g"
id_link=$(echo "${subsystem}-${manufacturer}_${product}${serial:+_${serial}}-if${interface}${port:+-port${port}}" | \
sed "${replace_chars}")
path_link=$(echo "${DEVPATH}${port:+-${port}}" | \
sed "s%/devices/%%; s%/${DEVICENAME}%%g; ${replace_chars}")
mkdir -p /dev/serial/by-id /dev/serial/by-path
ln -sf "/dev/${DEVICENAME}" "/dev/serial/by-id/${id_link}"
ln -sf "/dev/${DEVICENAME}" "/dev/serial/by-path/${path_link}"
elif [ "${ACTION}" = "unbind" ]; then
for link in $(find /dev/serial -type l); do
[ -L ${link} -a "$(readlink ${link})" = "/dev/$DEVICENAME" ] && rm ${link}
done
fi
@Leo-PL
Copy link

Leo-PL commented Mar 30, 2024

I had a chance to test this against my CH340-based adapter which lacks manufacturer string - and I think the call to sed in line 11 breaks this detection, because result code of sed instead of cat is taken into account, causing fallback to idVendor to never happen, resulting in this:

lrwxrwxrwx    1 root     root            12 Mar 30 14:58 usb-_USB2.0-Ser_-if00-port0 -> /dev/ttyUSB3

Adding set -o pipefail at the start fixes this.
I noticed, that with bind/unbind events, hotplug reacts faster to changes in the system, btw.

@galenseitz
Copy link
Author

Thanks for pointing this out. I've made a change that I believe fixes the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment