-
-
Save galenseitz/f3b2c4a45557eebe992dc637a1db070b to your computer and use it in GitHub Desktop.
Persistent serial port names for USB-serial on OpenWrt hotplugd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ "${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 |
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
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 ofsed
instead ofcat
is taken into account, causing fallback toidVendor
to never happen, resulting in this: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.