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 Mar 30, 2024

Copying the URL manually worked. It isn't as complicated as I envisioned after all, I'll try it out. Looks cool!
I plan to package the script in https://github.com/Leo-PL/my-openwrt-packages soon-ish. I mean - I created the package, but I have yet to at least build-test it.

@Leo-PL
Copy link
Author

Leo-PL commented Apr 6, 2024

@galenseitz I updated the wiki for TTY events as well, and hell - even put the script above as an example, already in your improved form.

@naseef
Copy link

naseef commented May 21, 2024

Hi. Thank you for the script. I have an issue. My ch341 UART converter is not triggering an event in hotplug.d. Can you please help me to troubleshoot this issue?

As mentioned in wiki, I have created a file /etc/hotplug.d/tty/99-logger as mentioned in openwrt wiki with

logger -t hotplug $(env)

output of logread -e hotplug which does not contain my device ttyUSB0

root@OpenWrt:/# logread -e hotplug
Tue May 21 07:32:31 2024 user.notice hotplug: USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=513 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.4/ttyUSB3 LOGNAME=root DEVICENAME=ttyUSB3 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin PWD=/
Tue May 21 07:32:31 2024 user.notice hotplug: DEVNAME=ttyUSB3 USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=514 MAJOR=188 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.4/ttyUSB3/tty/ttyUSB3 LOGNAME=root DEVICENAME=ttyUSB3 TERM=linux SUBSYSTEM=tty PATH=/usr/sbin:/usr/bin:/sbin:/bin MINOR=3 PWD=/
Tue May 21 07:32:31 2024 user.notice hotplug: USER=root ACTION=bind SHLVL=1 HOME=/ SEQNUM=515 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.4/ttyUSB3 LOGNAME=root DEVICENAME=ttyUSB3 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin DRIVER=option1 PWD=/
Tue May 21 07:32:32 2024 user.notice hotplug: USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=517 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.5/ttyUSB4 LOGNAME=root DEVICENAME=ttyUSB4 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin PWD=/
Tue May 21 07:32:32 2024 user.notice hotplug: DEVNAME=ttyUSB4 USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=518 MAJOR=188 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.5/ttyUSB4/tty/ttyUSB4 LOGNAME=root DEVICENAME=ttyUSB4 TERM=linux SUBSYSTEM=tty PATH=/usr/sbin:/usr/bin:/sbin:/bin MINOR=4 PWD=/
Tue May 21 07:32:33 2024 user.notice hotplug: USER=root ACTION=bind SHLVL=1 HOME=/ SEQNUM=519 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.5/ttyUSB4 LOGNAME=root DEVICENAME=ttyUSB4 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin DRIVER=option1 PWD=/
Tue May 21 07:32:33 2024 user.notice hotplug: USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=521 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.6/ttyUSB5 LOGNAME=root DEVICENAME=ttyUSB5 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin PWD=/
Tue May 21 07:32:33 2024 user.notice hotplug: DEVNAME=ttyUSB5 USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=522 MAJOR=188 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.6/ttyUSB5/tty/ttyUSB5 LOGNAME=root DEVICENAME=ttyUSB5 TERM=linux SUBSYSTEM=tty PATH=/usr/sbin:/usr/bin:/sbin:/bin MINOR=5 PWD=/
Tue May 21 07:32:34 2024 user.notice hotplug: USER=root ACTION=bind SHLVL=1 HOME=/ SEQNUM=523 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.6/ttyUSB5 LOGNAME=root DEVICENAME=ttyUSB5 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin DRIVER=option1 PWD=/
Tue May 21 07:32:34 2024 user.notice hotplug: USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=525 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.7/ttyUSB6 LOGNAME=root DEVICENAME=ttyUSB6 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin PWD=/
Tue May 21 07:32:34 2024 user.notice hotplug: DEVNAME=ttyUSB6 USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=526 MAJOR=188 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.7/ttyUSB6/tty/ttyUSB6 LOGNAME=root DEVICENAME=ttyUSB6 TERM=linux SUBSYSTEM=tty PATH=/usr/sbin:/usr/bin:/sbin:/bin MINOR=6 PWD=/
Tue May 21 07:32:35 2024 user.notice hotplug: USER=root ACTION=bind SHLVL=1 HOME=/ SEQNUM=527 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.7/ttyUSB6 LOGNAME=root DEVICENAME=ttyUSB6 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin DRIVER=option1 PWD=/
Tue May 21 07:32:35 2024 user.notice hotplug: USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=529 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.8/ttyUSB7 LOGNAME=root DEVICENAME=ttyUSB7 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin PWD=/
Tue May 21 07:32:35 2024 user.notice hotplug: DEVNAME=ttyUSB7 USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=530 MAJOR=188 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.8/ttyUSB7/tty/ttyUSB7 LOGNAME=root DEVICENAME=ttyUSB7 TERM=linux SUBSYSTEM=tty PATH=/usr/sbin:/usr/bin:/sbin:/bin MINOR=7 PWD=/
Tue May 21 07:32:36 2024 user.notice hotplug: USER=root ACTION=bind SHLVL=1 HOME=/ SEQNUM=531 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.8/ttyUSB7 LOGNAME=root DEVICENAME=ttyUSB7 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin DRIVER=option1 PWD=/

dmesg output

[   11.279005] usb 1-1.1: ch341-uart converter now attached to ttyUSB0
[   11.752141] mt76_wmac 10300000.wmac: ASIC revision: 76280001
[   12.820404] mt76_wmac 10300000.wmac: Firmware Version: 20151201
[   12.826451] mt76_wmac 10300000.wmac: Build Time: 20151201183641
[   12.868042] mt76_wmac 10300000.wmac: firmware init done
[   13.344992] usbcore: registered new interface driver pl2303
[   13.350870] usbserial: USB Serial support registered for pl2303
[   13.421730] PPP generic driver version 2.4.2
[   13.449429] NET: Registered PF_PPPOX protocol family
[   13.505415] usbcore: registered new interface driver option
[   13.511305] usbserial: USB Serial support registered for GSM modem (1-port)
[   13.519259] option 1-1.2:1.2: GSM modem (1-port) converter detected
[   13.526040] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB1
[   13.533575] option 1-1.2:1.3: GSM modem (1-port) converter detected
[   13.540443] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB2
[   13.547894] option 1-1.2:1.4: GSM modem (1-port) converter detected
[   13.554751] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB3
[   13.562273] option 1-1.2:1.5: GSM modem (1-port) converter detected
[   13.569142] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB4
[   13.576643] option 1-1.2:1.6: GSM modem (1-port) converter detected
[   13.583542] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB5
[   13.591133] option 1-1.2:1.7: GSM modem (1-port) converter detected
[   13.597890] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB6
[   13.605524] option 1-1.2:1.8: GSM modem (1-port) converter detected
[   13.612398] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB7

ttyUSB1 also not triggering the event

root@OpenWrt:/# logread |grep USB0
Tue May 21 07:51:59 2024 kern.info kernel: [   11.309613] usb 1-1.1: ch341-uart converter now attached to ttyUSB0
root@OpenWrt:/# logread |grep USB1
Tue May 21 07:51:59 2024 kern.info kernel: [   13.568606] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB1
root@OpenWrt:/# logread |grep USB2
Tue May 21 07:51:59 2024 kern.info kernel: [   13.582983] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB2
Tue May 21 07:51:59 2024 user.notice hotplug: USER=root ACTION=bind SHLVL=1 HOME=/ SEQNUM=511 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.3/ttyUSB2 LOGNAME=root DEVICENAME=ttyUSB2 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin DRIVER=option1 PWD=/
root@OpenWrt:/# logread |grep USB3
Tue May 21 07:51:59 2024 kern.info kernel: [   13.597271] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB3
Tue May 21 07:52:00 2024 user.notice hotplug: USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=513 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.4/ttyUSB3 LOGNAME=root DEVICENAME=ttyUSB3 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin PWD=/
Tue May 21 07:52:00 2024 user.notice hotplug: DEVNAME=ttyUSB3 USER=root ACTION=add SHLVL=1 HOME=/ SEQNUM=514 MAJOR=188 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.4/ttyUSB3/tty/ttyUSB3 LOGNAME=root DEVICENAME=ttyUSB3 TERM=linux SUBSYSTEM=tty PATH=/usr/sbin:/usr/bin:/sbin:/bin MINOR=3 PWD=/
Tue May 21 07:52:01 2024 user.notice hotplug: USER=root ACTION=bind SHLVL=1 HOME=/ SEQNUM=515 HOTPLUG_TYPE=tty DEVPATH=/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.4/ttyUSB3 LOGNAME=root DEVICENAME=ttyUSB3 TERM=linux SUBSYSTEM=usb-serial PATH=/usr/sbin:/usr/bin:/sbin:/bin DRIVER=option1 PWD=/

@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