Skip to content

Instantly share code, notes, and snippets.

@Leo-PL
Last active May 21, 2024 14:25
Show Gist options
  • Save Leo-PL/b5ee737e49b34c1551dba6c182707c8e to your computer and use it in GitHub Desktop.
Save Leo-PL/b5ee737e49b34c1551dba6c182707c8e to your computer and use it in GitHub Desktop.
[DEPRECATED - see https://github.com/Leo-PL/my-openwrt-packages/tree/master/utils/persistent-serial} Persistent serial port names for USB-serial on OpenWrt hotplugd
[ "${ACTION}" = "add" -o "${ACTION}" = "remove" ] || exit 0
[ "${SUBSYSTEM}" = "tty" ] || exit 0
[ -n "${DEVNAME}" -a -n "${DEVPATH}" ] || exit 1
if [ "${ACTION}" = "add" ]; then
subsystem="$(basename $(readlink /sys${DEVPATH}/../../../subsystem))"
[ "$subsystem" = "usb" ] || exit 0
manufacturer="$(cat /sys${DEVPATH}/../../../../manufacturer)" || manufacturer="$(cat /sys${DEVPATH}/../../../../idVendor)"
product="$(cat /sys${DEVPATH}/../../../../product)" || product="$(cat /sys${DEVPATH}/../../../../idProduct)"
serial="$(cat /sys${DEVPATH}/../../../../serial)"
interface="$(cat /sys${DEVPATH}/../../../bInterfaceNumber)"
port="$(cat /sys${DEVPATH}/device/port_number)"
id_link=$(echo "${subsystem}"-"${manufacturer}"_"${product}${serial:+_}${serial}"-if"${interface}${port:+-port}${port}" | s/[^\.\:0-9A-Za-z-]/_/g)
path_link=$(echo "${DEVPATH}${port:+-port}${port}" | sed s%/devices/%% | sed s%/${DEVNAME}/tty/${DEVNAME}%%g | sed s/[^\.\:0-9A-Za-z-]/_/g)
mkdir -p /dev/serial/by-id /dev/serial/by-path
ln -sf "/dev/${DEVNAME}" "/dev/serial/by-id/${id_link}"
ln -sf "/dev/${DEVNAME}" "/dev/serial/by-path/${path_link}"
elif [ "${ACTION}" = "remove" ]; then
for link in $(find /dev/serial -type l); do
[ -L ${link} -a "$(readlink ${link})" = "/dev/$DEVNAME" ] && rm ${link}
done
fi
@Leo-PL
Copy link
Author

Leo-PL commented May 21, 2024

Do you have links under /dev/serial/by-path? Symlinks for by-id won't work with CH340, because it lacks iSerialNumber attribute in USB descriptor. Also it's possible there is some copy-paste error, check if you can execute the script manually by passing required environment variables. Run set -x to enable shell debug.

@naseef Also use the version from the linked repository. One posted here is deprecated.

@naseef
Copy link

naseef commented May 21, 2024

Seems like I have narrowed down the issue. The devices are directly soldered in to the board and not hotplugged, so openwrt won't initiate any hotplug events. I am still new this, correct me if I am wrong

And yes, I was using the code from the linked repo.

@Leo-PL
Copy link
Author

Leo-PL commented May 21, 2024

Still, bind events should work for coldplugged devices. Unless there is a bug in hotplugd which prevents this somehow.

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