Udev

udev:使用 USB 程式器,無需 sudo

  • June 21, 2020

我正在使用mspdebug程式器連接到設備。

mspdebug只會在我使用時連接到設備sudo mspdebug。如果我不使用sudomspdebug找不到設備。

如何在不需要的情況下連接到設備sudo

我特別想連接的設備在這裡列出:

$ lsusb
Bus 001 Device 018: ID 2047:0013 Texas Instruments MSP Tools Driver

我有這些udev規則/etc/udev/rules.d/46-TI_launchpad.rules

ATTR{idVendor}=="0451", ATTR{idProduct}=="f432", MODE="0660", GROUP="plugdev"
ATTR{idVendor}=="2047", ATTR{idProduct}=="0013", MODE="0660", GROUP="plugdev"

我在plugdev群裡:

$ id $USER
uid=1000(earthishome) gid=1000(earthishome) groups=1000(earthishome),10(wheel),977(pkg-build),1001(plugdev),974(vboxusers),1003(docker)

編輯 1

追踪設備

插入設備時,會創建兩個 tty:ttyACM0ttyACM1

$ udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[21580.524390] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2 (usb)
KERNEL[21580.527712] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0 (usb)
KERNEL[21580.528138] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/tty/ttyACM0 (tty)
KERNEL[21580.528301] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0 (usb)
KERNEL[21580.528453] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.1 (usb)
KERNEL[21580.528623] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.1 (usb)
KERNEL[21580.528892] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2 (usb)
KERNEL[21580.529451] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/tty/ttyACM1 (tty)
KERNEL[21580.529600] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2 (usb)
KERNEL[21580.529746] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.3 (usb)
KERNEL[21580.529892] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.3 (usb)
KERNEL[21580.530075] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-2 (usb)
UDEV  [21580.563378] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2 (usb)
UDEV  [21580.570495] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0 (usb)
UDEV  [21580.572104] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.1 (usb)
UDEV  [21580.573582] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.3 (usb)
UDEV  [21580.573663] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2 (usb)
UDEV  [21580.577042] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.1 (usb)
UDEV  [21580.578987] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.3 (usb)
UDEV  [21580.579357] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/tty/ttyACM0 (tty)
UDEV  [21580.580712] add      /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2/tty/ttyACM1 (tty)
UDEV  [21580.585516] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0 (usb)
UDEV  [21580.585592] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.2 (usb)
UDEV  [21580.596329] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-2 (usb)

使用 strace

嘗試打開時出現“權限被拒絕”錯誤/dev/ttyACM0,但在我執行時不會發生strace sudo mspdebug tilib

$ strace mspdebug tilib
openat(AT_FDCWD, "/dev/ttyACM0", O_RDWR|O_NOCTTY|O_NONBLOCK) = -1 EACCES (Permission denied)
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=5000000}, NULL) = 0
openat(AT_FDCWD, "/dev/ttyACM0", O_RDWR|O_NOCTTY|O_NONBLOCK) = -1 EACCES (Permission denied)
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=5000000}, NULL) = 0
openat(AT_FDCWD, "/dev/ttyACM0", O_RDWR|O_NOCTTY|O_NONBLOCK) = -1 EACCES (Permission denied)
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=5000000}, NULL) = 0
openat(AT_FDCWD, "/dev/ttyACM0", O_RDWR|O_NOCTTY|O_NONBLOCK) = -1 EACCES (Permission denied)
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=5000000}, NULL) = 0

使用 udevadm 查找設備及其父設備

$ udevadm info --attribute-walk /dev/ttyACM0

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

 looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/tty/ttyACM0':
   KERNEL=="ttyACM0"
   SUBSYSTEM=="tty"
   DRIVER==""

 looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0':
   KERNELS=="1-2:1.0"
   SUBSYSTEMS=="usb"
   DRIVERS=="cdc_acm"
   ATTRS{iad_bFunctionSubClass}=="02"
   ATTRS{bmCapabilities}=="2"
   ATTRS{interface}=="MSP Debug Interface"
   ATTRS{bInterfaceClass}=="02"
   ATTRS{authorized}=="1"
   ATTRS{bInterfaceNumber}=="00"
   ATTRS{iad_bFunctionClass}=="02"
   ATTRS{iad_bFunctionProtocol}=="01"
   ATTRS{bAlternateSetting}==" 0"
   ATTRS{supports_autosuspend}=="1"
   ATTRS{iad_bFirstInterface}=="00"
   ATTRS{bInterfaceSubClass}=="02"
   ATTRS{bNumEndpoints}=="01"
   ATTRS{bInterfaceProtocol}=="01"
   ATTRS{iad_bInterfaceCount}=="02"

 looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-2':
   KERNELS=="1-2"
   SUBSYSTEMS=="usb"
   DRIVERS=="usb"
   ATTRS{busnum}=="1"
   ATTRS{authorized}=="1"
   ATTRS{bMaxPacketSize0}=="8"
   ATTRS{devnum}=="14"
   ATTRS{bmAttributes}=="80"
   ATTRS{rx_lanes}=="1"
   ATTRS{serial}=="B1443A5106001800"
   ATTRS{bcdDevice}=="0200"
   ATTRS{bMaxPower}=="100mA"
   ATTRS{product}=="MSP Tools Driver"
   ATTRS{idVendor}=="2047"
   ATTRS{bDeviceSubClass}=="02"
   ATTRS{speed}=="12"
   ATTRS{removable}=="removable"
   ATTRS{urbnum}=="140"
   ATTRS{devpath}=="2"
   ATTRS{bDeviceClass}=="ef"
   ATTRS{bNumInterfaces}==" 4"
   ATTRS{tx_lanes}=="1"
   ATTRS{ltm_capable}=="no"
   ATTRS{idProduct}=="0013"
   ATTRS{configuration}=="MSP430 USB"
   ATTRS{maxchild}=="0"
   ATTRS{bDeviceProtocol}=="01"
   ATTRS{quirks}=="0x0"
   ATTRS{bNumConfigurations}=="1"
   ATTRS{manufacturer}=="Texas Instruments"
   ATTRS{avoid_reset_quirk}=="0"
   ATTRS{version}==" 2.00"
   ATTRS{bConfigurationValue}=="1"

 looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1':
   KERNELS=="usb1"
   SUBSYSTEMS=="usb"
   DRIVERS=="usb"
   ATTRS{authorized_default}=="1"
   ATTRS{idVendor}=="1d6b"
   ATTRS{speed}=="480"
   ATTRS{bDeviceProtocol}=="01"
   ATTRS{serial}=="0000:00:14.0"
   ATTRS{devnum}=="1"
   ATTRS{devpath}=="0"
   ATTRS{bMaxPower}=="0mA"
   ATTRS{ltm_capable}=="no"
   ATTRS{version}==" 2.00"
   ATTRS{rx_lanes}=="1"
   ATTRS{busnum}=="1"
   ATTRS{avoid_reset_quirk}=="0"
   ATTRS{urbnum}=="268"
   ATTRS{bmAttributes}=="e0"
   ATTRS{bDeviceClass}=="09"
   ATTRS{bcdDevice}=="0506"
   ATTRS{interface_authorized_default}=="1"
   ATTRS{configuration}==""
   ATTRS{bConfigurationValue}=="1"
   ATTRS{bNumConfigurations}=="1"
   ATTRS{quirks}=="0x0"
   ATTRS{manufacturer}=="Linux 5.6.19-300.fc32.x86_64 xhci-hcd"
   ATTRS{bNumInterfaces}==" 1"
   ATTRS{idProduct}=="0002"
   ATTRS{bDeviceSubClass}=="00"
   ATTRS{bMaxPacketSize0}=="64"
   ATTRS{authorized}=="1"
   ATTRS{maxchild}=="12"
   ATTRS{removable}=="unknown"
   ATTRS{tx_lanes}=="1"
   ATTRS{product}=="xHCI Host Controller"

 looking at parent device '/devices/pci0000:00/0000:00:14.0':
   KERNELS=="0000:00:14.0"
   SUBSYSTEMS=="pci"
   DRIVERS=="xhci_hcd"
   ATTRS{device}=="0x9d2f"
   ATTRS{dbc}=="disabled"
   ATTRS{irq}=="126"
   ATTRS{consistent_dma_mask_bits}=="64"
   ATTRS{driver_override}=="(null)"
   ATTRS{subsystem_device}=="0x082a"
   ATTRS{dma_mask_bits}=="64"
   ATTRS{class}=="0x0c0330"
   ATTRS{subsystem_vendor}=="0x1028"
   ATTRS{vendor}=="0x8086"
   ATTRS{enable}=="1"
   ATTRS{local_cpulist}=="0-7"
   ATTRS{ari_enabled}=="0"
   ATTRS{d3cold_allowed}=="1"
   ATTRS{revision}=="0x21"
   ATTRS{broken_parity_status}=="0"
   ATTRS{local_cpus}=="ff"
   ATTRS{msi_bus}=="1"
   ATTRS{numa_node}=="-1"

 looking at parent device '/devices/pci0000:00':
   KERNELS=="pci0000:00"
   SUBSYSTEMS==""
   DRIVERS==""

編輯 2

由於評論中的故障排除提示,我能夠解決這個問題。

TLDR

有兩件事需要改變:

  1. 將文件從 移動46-TI_launchpad.rules94-TI_launchpad.rules
  2. 在規則中更改ATTRATTRS如圖所示:

ATTRS{idVendor}==“2047”, ATTRS{idProduct}==“0013”, MODE=“0660”, GROUP=“plugdev”

細節

正如 Artem S. Tashkinov 和 meuh 所指出的,這條規則正在被後來的規則所覆蓋。在 90 年代重命名文件可確保它將執行到最後,並且不會被早期規則覆蓋。

使用udevadm info -adirkt 建議,列印整個設備鏈。查看設備鏈,實際設備本身沒有屬性idVendoridProduct定義;這些屬性在父設備中定義。為了匹配那些屬性,ATTR必須更改ATTRS為匹配父設備中的屬性。

由於問題評論中的故障排除提示,我能夠解決這個問題。

TLDR

有兩件事需要改變:

  1. 將文件從 移動46-TI_launchpad.rules94-TI_launchpad.rules
  2. 在規則中更改ATTRATTRS如圖所示:
ATTRS{idVendor}=="2047", ATTRS{idProduct}=="0013", MODE="0660", GROUP="plugdev"

細節

正如 Artem S. Tashkinov 和 meuh 所指出的,46 規則正在被後來的規則所覆蓋。在 90 年代重命名文件可確保它將執行到最後,並且不會被早期規則覆蓋。

使用udevadm info -adirkt 建議,列印整個設備鏈。查看設備鏈,實際設備本身沒有屬性idVendoridProduct定義;這些屬性在父設備中定義。為了匹配父設備中的屬性,ATTR必須更改為ATTRS.

引用自:https://unix.stackexchange.com/questions/594042