当我插入 USB 转串口设备时,设备需要一些时间才能切换到“dialout”组(它从“root”开始)组。这会对某些客户端软件造成严重破坏,这些软件希望在重置后立即具有读取设备的权限。
我尝试了两个 USB 转串口设备,它们都有相同的行为 - atmega32u4 和 ch340。
ls -l /dev/tty*
如果我快速循环打印,会发生以下情况:
第一次插入设备时:
> while ((1)); do ls -l /dev/ttyACM* 2> /dev/null; sleep 0.1; done;
# it does not print anything until I plug in the device
crw-rw-rw- 1 root dialout 166, 0 Aug 19 17:23 /dev/ttyACM0
crw-rw-rw- 1 root dialout 166, 0 Aug 19 17:23 /dev/ttyACM0
crw-rw-rw- 1 root dialout 166, 0 Aug 19 17:23 /dev/ttyACM0
这看起来很棒,但是当我再次拔下并插入设备时会发生这种情况:
> while ((1)); do ls -l /dev/ttyACM* 2> /dev/null; sleep 0.1; done;
# Nothing is printed out until I plug the device in for a second time
crw------- 1 root root 166, 0 Aug 19 17:23 /dev/ttyACM0
crw------- 1 root root 166, 0 Aug 19 17:23 /dev/ttyACM0
crw------- 1 root root 166, 0 Aug 19 17:23 /dev/ttyACM0
crw------- 1 root root 166, 0 Aug 19 17:23 /dev/ttyACM0
crw------- 1 root root 166, 0 Aug 19 17:23 /dev/ttyACM0
crw-rw-rw- 1 root dialout 166, 0 Aug 19 17:23 /dev/ttyACM0
crw-rw-rw- 1 root dialout 166, 0 Aug 19 17:23 /dev/ttyACM0
crw-rw-rw- 1 root dialout 166, 0 Aug 19 17:23 /dev/ttyACM0
第一次插入设备后,在所有连续尝试插入设备时,我让设备在“root”组中出现半秒,然后将其自身移动到“dialout”组。这可以瞬间实现吗?
第一次和连续插件的 dmesg 消息是相同的。
这种情况在没有任何自定义 udev 规则的情况下都会发生,和udev 规则要求将此特定设备放入“拨出”组中 - 第一次插入后总会有半秒的延迟。
这是 2018 年 8 月发布的 Debian Unstable 版本。
这是udevadm monitor
显示的内容:
sudo udevadm monitor & (while ((1)); do ls -l /dev/ttyACM*; ls -l /dev/ttyUSB*; sleep 0.1; done;) 2> /dev/null
[1] 18788
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
KERNEL[8916.515599] add /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
KERNEL[8916.516088] add /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
KERNEL[8916.516768] add /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/tty/ttyACM0 (tty)
KERNEL[8916.516835] bind /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
KERNEL[8916.516886] add /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.1 (usb)
KERNEL[8916.516944] bind /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.1 (usb)
KERNEL[8916.516999] bind /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
crw------- 1 root root 166, 0 Aug 19 17:54 /dev/ttyACM0
crw------- 1 root root 166, 0 Aug 19 17:54 /dev/ttyACM0
crw------- 1 root root 166, 0 Aug 19 17:54 /dev/ttyACM0
crw------- 1 root root 166, 0 Aug 19 17:54 /dev/ttyACM0
crw------- 1 root root 166, 0 Aug 19 17:54 /dev/ttyACM0
UDEV [8917.063021] add /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
UDEV [8917.064210] add /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
UDEV [8917.068240] add /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.1 (usb)
UDEV [8917.068290] bind /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.1 (usb)
UDEV [8917.068359] add /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/tty/ttyACM0 (tty)
UDEV [8917.080062] bind /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0 (usb)
UDEV [8917.080134] bind /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
crw-rw-rw- 1 root dialout 166, 0 Aug 19 17:54 /dev/ttyACM0
crw-rw-rw- 1 root dialout 166, 0 Aug 19 17:54 /dev/ttyACM0
crw-rw-rw- 1 root dialout 166, 0 Aug 19 17:54 /dev/ttyACM0