内核注册驱动程序后如何触发 udev 规则?

内核注册驱动程序后如何触发 udev 规则?

我有一个 MIDI 键盘“Impulse”和一个 raspi3。我想将键盘连接到raspi,并且在不接触任何东西的情况下发出声音。

所以我使用fluidsynthwithjack作为音频驱动程序。 Fluidsynth 由 systemd 服务启动。没有问题。

所以我制作了一个脚本,其中我“jack_connect”端口,并以这种方式“aconnect”我键盘的midi端口到合成器fluidsynth:

impulseport=$(aconnect -i|grep -i "IMPULSE" | cut -d ' ' -f 2)0
synthport=$(aconnect -o |grep -i "FLUID" | cut -d ' ' -f 2)0

# some verifications of existence and exit if one port is missing
aconnect ${impulseport} ${synthport}

问题是它并没有真正按照我想要的方式工作。我的 udev 规则是在加载“snd-usb-audio”接口驱动程序之前触发此脚本。因此,该变量$impulseport为空(实际上=0,最后连接)。

这是我的 udev 规则:

ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="1235", ATTRS{idProduct}=="001a", RUN+="/bin/su -c /home/pi/piano_connect - pi"
ACTION=="remove", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="1235", ATTRS{idProduct}=="001a", RUN+="/usr/bin/aconnect -x"

在系统日志中:

1 systemd[410]: Started Sound Service.
2 fluidsynth[423]: fluidsynth: Jack sample rate mismatch, adjusting. (synth.sample-rate=44100, jackd=48000)
3 kernel: [162.772916] usb 1-1.2: new full-speed USB device number 4 using dwc_otg
4 kernel: [162.905473] usb 1-1.2: New USB device found, idVendor=1235, idProduct=001a, bcdDevice= 0.00
5 kernel: [162.905491] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
6 kernel: [162.905500] usb 1-1.2: Product: Impulse
7 kernel: [162.905509] usb 1-1.2: Manufacturer: Focusrite A.E. Ltd
8 mtp-probe: checking bus 1, device 4: "/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2"
9 mtp-probe: bus: 1, device: 4 was not an MTP device
10 systemd[1]: Started Session c2 of user pi.
11 pi: [Piano] Jackd : connecting system to fluidsynth ports
12 pi: [Piano] Error: Port Impulse unknown, exiting
13 systemd-udevd[473]: Process '/bin/su -c /home/pi/piano_connect - pi' failed with exit code 1.
14 systemd[1]: session-c2.scope: Succeeded.15 
15 kernel: [165.876060] usbcore: registered new interface driver snd-usb-audio
16 mtp-probe: checking bus 1, device 4: "/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2"
17 mtp-probe: bus: 1, device: 4 was not an MTP device

问题出在第 12 行:$impulseport为空!因此它使脚本退出(第 13 行)。你可以看到第 15 行驱动程序是在之后加载的。所以在我的脚本中我做了一个肮脏的事情,比如一个 while 循环,我用 a 检查目录是否/sys/bus/usb/drivers/snd-usb-audio存在,sleep但它挂起系统并且模块永远不会加载。你知道吗?伙计们有什么出路吗?

注意:如果我拔下 USB MIDI 键盘,然后再次插入它,它可以正常工作,但我希望它能够在启动后的第一个插头上正常工作...或者甚至在键盘已插入时在启动后可以正常工作: D

答案1

好吧,经过一番研究,我找到了出路。一、使用udevadm monitor前插拔并插好。这是输出:

KERNEL[126.555200] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
  KERNEL[126.555888] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
  KERNEL[126.556508] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.1 (usb)
  KERNEL[126.557829] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.2 (usb)
  KERNEL[126.558188] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.3 (usb)
  KERNEL[126.558548] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
  KERNEL[129.399848] add      /module/snd_seq_dummy (module)
  UDEV  [129.406959] add      /module/snd_seq_dummy (module)
  UDEV  [129.493966] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
  UDEV  [129.501782] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.2 (usb)
  UDEV  [129.505609] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.3 (usb)
  KERNEL[129.508606] add      /module/snd_rawmidi (module)
  UDEV  [129.510396] add      /module/snd_rawmidi (module)
  KERNEL[129.513459] add      /module/snd_usbmidi_lib (module)
  UDEV  [129.515605] add      /module/snd_usbmidi_lib (module)
  KERNEL[129.516631] add      /module/snd_hwdep (module)
  UDEV  [129.518617] add      /module/snd_hwdep (module)
  KERNEL[129.535977] add      /module/snd_usb_audio (module)
  UDEV  [129.537881] add      /module/snd_usb_audio (module)
  KERNEL[129.538452] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.1 (usb)
  KERNEL[129.538547] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1 (sound)
  KERNEL[129.539135] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/midiC1D0 (sound)
  KERNEL[129.539260] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0 (snd_seq)
  KERNEL[129.539985] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/controlC1 (sound)
  KERNEL[129.541172] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
  UDEV  [129.541345] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.1 (usb)
  KERNEL[129.541428] add      /bus/usb/drivers/snd-usb-audio (drivers)
  UDEV  [129.542776] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
  UDEV  [129.542917] add      /bus/usb/drivers/snd-usb-audio (drivers)
  KERNEL[129.549489] add      /module/snd_seq_midi_event (module)
  UDEV  [129.551648] add      /module/snd_seq_midi_event (module)
  KERNEL[129.552118] add      /module/snd_seq_midi (module)
  KERNEL[129.552213] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0 (snd_seq)
  KERNEL[129.552288] add      /bus/snd_seq/drivers/snd_seq_midi (drivers)
  UDEV  [129.554430] add      /module/snd_seq_midi (module)
  UDEV  [129.554572] add      /bus/snd_seq/drivers/snd_seq_midi (drivers)
  UDEV  [129.561871] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
  UDEV  [129.564936] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.1 (usb)
  UDEV  [129.566125] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1 (sound)
  UDEV  [129.568261] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0 (snd_seq)
  UDEV  [129.570243] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/midiC1D0 (sound)
  KERNEL[129.573057] change   /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1 (sound)
  UDEV  [129.607147] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/controlC1 (sound)
  UDEV  [129.609881] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
  UDEV  [129.611644] bind     /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0 (snd_seq)
  UDEV  [129.618931] change   /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1 (sound)

注意到最后一行后UDEV [129.611644] bind /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0 (snd_seq),我插入了 USB MIDI 键盘,然后执行了以下操作:

devadm info -a /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2\:1.0/sound/card1/seq-midi-1-0/> /tmp/udev`

在输出中,第一行有:

  looking at device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1/seq-midi-1-0':
    KERNEL=="seq-midi-1-0"
    SUBSYSTEM=="snd_seq"
    DRIVER=="snd_seq_midi"

  looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/sound/card1':
    KERNELS=="card1"
    SUBSYSTEMS=="sound"
    DRIVERS==""
    ATTRS{number}=="1"
    ATTRS{id}=="Impulse"

  looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0':
    KERNELS=="1-1.2:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="snd-usb-audio"
    ATTRS{bInterfaceProtocol}=="00"
    ATTRS{bInterfaceSubClass}=="01"
    ATTRS{interface}=="Novation Impulse"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{supports_autosuspend}=="1"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{authorized}=="1"
    ATTRS{bNumEndpoints}=="00"
    ATTRS{bInterfaceClass}=="01"

有趣的信息是SUBSYSTEMDRIVERS。所以我将 udev 规则更改为:

ACTION=="bind", SUBSYSTEM=="snd_seq", DRIVERS=="snd-usb-audio", RUN+="/bin/su -c /home/pi/piano_connect - pi"
ACTION=="remove", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}=="1235", ATTRS{idProduct}=="001a", RUN+="/usr/bin/aconnect -x"

在这里,我将 ACTION 更改为“bind”,因为第一个命令 ( ) 并与和udevadm monitor匹配,而不是产品的 id。它就像一个魅力!希望有帮助。 :)SUBSYSTEMDRIVERS

相关内容