プロポについて(その3)〜 udevの設定 〜

lsusbを使った接続確認

udevのルールを追加せずにT8SGをDFUモードで接続すると、
DeviationUploaderで認識されず、ファームウェアの書き換えができない。
 lsusbを使用して接続状況を確認してみる。

$ sudo lsusb

Bus 003 Device 008: ID 05ac:8501 Apple, Inc. Built-in iSight [Micron]
Bus 003 Device 040: ID 0483:df11 STMicroelectronics STM Device in DFU Mode
Bus 003 Device 002: ID 1a40:0101 Terminus Technology Inc. Hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 007 Device 003: ID 05ac:022b Apple, Inc. Internal Keyboard/Trackpad (MacBook Pro) (JIS)
Bus 007 Device 002: ID 05ac:8242 Apple, Inc. Built-in IR Receiver
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 005: ID 05ac:0251 Apple, Inc. Apple Keyboard
Bus 001 Device 004: ID 056e:0132 Elecom Co., Ltd TrackBall Mouse M-DPT1MR Wireless
Bus 001 Device 003: ID 05ac:1006 Apple, Inc. Hub in Aluminum Keyboard
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 009: ID 05ac:8205 Apple, Inc. Bluetooth HCI
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

ツリー形式で表示すると、

$ sudo lsusb -t

/:  Bus 07.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
    |__ Port 1: Dev 2, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
    |__ Port 2: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 2: Dev 3, If 1, Class=Human Interface Device, Driver=appletouch, 12M
    |__ Port 2: Dev 3, If 2, Class=Human Interface Device, Driver=usbhid, 12M
/:  Bus 06.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 05.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/6p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 4: Dev 40, If 0, Class=Application Specific Interface, Driver=, 12M
    |__ Port 4: Dev 8, If 0, Class=Video, Driver=uvcvideo, 480M
    |__ Port 4: Dev 8, If 1, Class=Video, Driver=uvcvideo, 480M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
    |__ Port 1: Dev 9, If 0, Class=Wireless, Driver=btusb, 12M
    |__ Port 1: Dev 9, If 1, Class=Wireless, Driver=btusb, 12M
    |__ Port 1: Dev 9, If 2, Class=Application Specific Interface, Driver=, 12M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/4p, 480M
    |__ Port 2: Dev 3, If 0, Class=Hub, Driver=hub/3p, 480M
        |__ Port 1: Dev 4, If 0, Class=Human Interface Device, Driver=usbhid, 12M
        |__ Port 1: Dev 4, If 1, Class=Human Interface Device, Driver=usbhid, 12M
        |__ Port 1: Dev 4, If 2, Class=Human Interface Device, Driver=usbhid, 12M
        |__ Port 1: Dev 4, If 3, Class=Human Interface Device, Driver=usbhid, 12M
        |__ Port 2: Dev 5, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 2: Dev 5, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M

T8SGである”STMicroelectronics STM Device in DFU Mode”は
Bus 003にDevice 40として、接続されている。
vendorとproductはそれぞれ、0483とdf11。
このvendorとproductを指定してさらに詳細を表示する。

$ sudo lsusb -v -d 0483:df11

Bus 003 Device 040: ID 0483:df11 STMicroelectronics STM Device in DFU Mode
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.00
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x0483 STMicroelectronics
  idProduct          0xdf11 STM Device in DFU Mode
  bcdDevice            2.00
  iManufacturer           1 STMicroelectronics
  iProduct                2 STM32 DFU
  iSerial                 3 DEVO-7E ㌀⡂䣿ጴ々W
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x002d
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0x80
      (Bus Powered)
    MaxPower               64mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           0
      bInterfaceClass       254 Application Specific Interface
      bInterfaceSubClass      1 Device Firmware Update
      bInterfaceProtocol      2 
      iInterface              4 @Internal Flash  /0x08003000/00*001Ka,244*001Kg
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       1
      bNumEndpoints           0
      bInterfaceClass       254 Application Specific Interface
      bInterfaceSubClass      1 Device Firmware Update
      bInterfaceProtocol      2 
      iInterface              5 @SPI Flash: Config/0x00002000/030*04Kg
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       2
      bNumEndpoints           0
      bInterfaceClass       254 Application Specific Interface
      bInterfaceSubClass      1 Device Firmware Update
      bInterfaceProtocol      2 
      iInterface              6 @SPI Flash: Library/0x00020000/030*064Kg
      Device Firmware Upgrade Interface Descriptor:
        bLength                             9
        bDescriptorType                    33
        bmAttributes                       11
          Will Detach
          Manifestation Intolerant
          Upload Supported
          Download Supported
        wDetachTimeout                    255 milliseconds
        wTransferSize                    1024 bytes
        bcdDFUVersion                   1.1a
Device Status:     0x0000
  (Bus Powered)

Devo-7E”などの表示もあり、特に問題は見つからないように思う。

udevadmの出力結果

T8SGを接続する前にudevadmを起動しておく。

$ sudo udevadm monitor -p

-uオプションと-kオプションがどちらも指定されていなければ、
接続や取り外し時にカーネルとudevの両方のメッセージが表示される。

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

KERNEL[8118569.017950] add      /devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/003/040
DEVTYPE=usb_device
PRODUCT=483/df11/200
TYPE=0/0/0
BUSNUM=003
DEVNUM=040
SEQNUM=3499
MAJOR=189
MINOR=295

KERNEL[8118569.020321] add      /devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4/3-1.4:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4/3-1.4:1.0
SUBSYSTEM=usb
DEVTYPE=usb_interface
PRODUCT=483/df11/200
TYPE=0/0/0
INTERFACE=254/1/2
MODALIAS=usb:v0483pDF11d0200dc00dsc00dp00icFEisc01ip02in00
SEQNUM=3500

KERNEL[8118569.020793] bind     /devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4 (usb)
ACTION=bind
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/003/040
DEVTYPE=usb_device
DRIVER=usb
PRODUCT=483/df11/200
TYPE=0/0/0
BUSNUM=003
DEVNUM=040
SEQNUM=3501
MAJOR=189
MINOR=295

UDEV  [8118569.043236] add      /devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/003/040
DEVTYPE=usb_device
PRODUCT=483/df11/200
TYPE=0/0/0
BUSNUM=003
DEVNUM=040
SEQNUM=3499
USEC_INITIALIZED=8118569042741
ID_VENDOR=STMicroelectronics
ID_VENDOR_ENC=STMicroelectronics
ID_VENDOR_ID=0483
ID_MODEL=STM32_DFU
ID_MODEL_ENC=STM32\x20DFU
ID_MODEL_ID=df11
ID_REVISION=0200
ID_SERIAL=STMicroelectronics_STM32_DFU
ID_BUS=usb
ID_USB_INTERFACES=:fe0102:
ID_VENDOR_FROM_DATABASE=STMicroelectronics
ID_MODEL_FROM_DATABASE=STM Device in DFU Mode
ID_PATH=pci-0000:00:1d.7-usb-0:1.4
ID_PATH_TAG=pci-0000_00_1d_7-usb-0_1_4
DRIVER=usb
ID_FOR_SEAT=usb-pci-0000_00_1d_7-usb-0_1_4
MAJOR=189
MINOR=295
TAGS=:seat:
CURRENT_TAGS=:seat:

UDEV  [8118569.051684] add      /devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4/3-1.4:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4/3-1.4:1.0
SUBSYSTEM=usb
DEVTYPE=usb_interface
PRODUCT=483/df11/200
TYPE=0/0/0
INTERFACE=254/1/2
MODALIAS=usb:v0483pDF11d0200dc00dsc00dp00icFEisc01ip02in00
SEQNUM=3500
USEC_INITIALIZED=8118569051210
ID_VENDOR_FROM_DATABASE=STMicroelectronics
ID_MODEL_FROM_DATABASE=STM Device in DFU Mode
ID_PATH=pci-0000:00:1d.7-usb-0:1.4:1.0
ID_PATH_TAG=pci-0000_00_1d_7-usb-0_1_4_1_0

UDEV  [8118569.073052] bind     /devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4 (usb)
ACTION=bind
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb3/3-1/3-1.4
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/003/040
DEVTYPE=usb_device
DRIVER=usb
PRODUCT=483/df11/200
TYPE=0/0/0
BUSNUM=003
DEVNUM=040
SEQNUM=3501
USEC_INITIALIZED=8118569042741
ID_VENDOR=STMicroelectronics
ID_VENDOR_ENC=STMicroelectronics
ID_VENDOR_ID=0483
ID_MODEL=STM32_DFU
ID_MODEL_ENC=STM32\x20DFU
ID_MODEL_ID=df11
ID_REVISION=0200
ID_SERIAL=STMicroelectronics_STM32_DFU
ID_BUS=usb
ID_USB_INTERFACES=:fe0102:
ID_VENDOR_FROM_DATABASE=STMicroelectronics
ID_MODEL_FROM_DATABASE=STM Device in DFU Mode
ID_PATH=pci-0000:00:1d.7-usb-0:1.4
ID_PATH_TAG=pci-0000_00_1d_7-usb-0_1_4
ID_FOR_SEAT=usb-pci-0000_00_1d_7-usb-0_1_4
MAJOR=189
MINOR=295
TAGS=:seat:
CURRENT_TAGS=:seat:

バイス名は /dev/bus/usb/003/040となっている。
lsを使ってデバイスファイルの状態を確認する。

$ ls -l -d /dev/bus/usb/003/040

crw-rw-r-- 1 root root 189, 295  5月  1 19:50 /dev/bus/usb/003/040

ユーザー、グループの両方がrootになっていて、
otherのパーミッションは”r”しか設定されていないため、
root以外のユーザーでは書き込みができず、コマンドも送れない。

パーミッションの設定

直接/dev/bus/used/003/040などのデバイスファイルのパーミッションを変更しても、
次回接続時には反映されない。
USB接続のデバイスに所属グループやパーミッションを設定するためには
適切な識別情報を与えてudevルールを追加する。
今回の場合は、idVendorとidProductを指定して、グループをplugdevに、
パーミッション0664に設定するルールを追加している。

$ cat /etc/udev/rules.d/45-stdfu-permissions.rules 
# DFU (Internal bootloader for STM32 MCUs)
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="df11",  MODE="0664", GROUP="plugdev"

ルールファイルを追加したのち、T8SGを再接続すると、パーミッションとグループが反映され、
plugdevに所属しているユーザーで起動したDeviationUploaderに認識されるようになる。
ということらしい・・・

$ lsusb
Bus 003 Device 008: ID 05ac:8501 Apple, Inc. Built-in iSight [Micron]
Bus 003 Device 041: ID 0483:df11 STMicroelectronics STM Device in DFU Mode
Bus 003 Device 002: ID 1a40:0101 Terminus Technology Inc. Hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 007 Device 003: ID 05ac:022b Apple, Inc. Internal Keyboard/Trackpad (MacBook Pro) (JIS)
Bus 007 Device 002: ID 05ac:8242 Apple, Inc. Built-in IR Receiver
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 005: ID 05ac:0251 Apple, Inc. Apple Keyboard
Bus 001 Device 004: ID 056e:0132 Elecom Co., Ltd TrackBall Mouse M-DPT1MR Wireless
Bus 001 Device 003: ID 05ac:1006 Apple, Inc. Hub in Aluminum Keyboard
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 009: ID 05ac:8205 Apple, Inc. Bluetooth HCI
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

$ ls -l -d /dev/bus/usb/003/041
crw-rw-r-- 1 root plugdev 189, 296  5月  1 19:54 /dev/bus/usb/003/041