核心註冊驅動程序後如何觸發 udev 規則?
我有一個 MIDI 鍵盤“Impulse”和一個 raspi3。我想將鍵盤連接到 raspi,並在不接觸任何東西的情況下發出聲音。
所以我用
fluidsynth
withjack
作為音頻驅動程序。Fluidsynth 由 systemd 服務啟動。沒有問題。所以我製作了一個腳本,在其中我“jack_connect”埠,並以這種方式將鍵盤的 midi 埠“aconnect”到合成器流體合成器:
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 行驅動程序是在 AFTERWARDS 之後載入的。所以在我的腳本中,我做了一個臟東西,比如 while 循環,我/sys/bus/usb/drivers/snd-usb-audio
用 a 檢查目錄是否存在,sleep
但它會掛起系統並且模組永遠不會載入。你伙計們對出路有什麼想法嗎?注意:如果我拔下 USB MIDI 鍵盤,然後重新插入它就可以正常工作,但我希望它在啟動後的第一個插頭上正常工作……或者在鍵盤已經插入時甚至在啟動後工作更多: D
好的,經過一些研究,我找到了出路。一、
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"
有趣的資訊是
SUBSYSTEM
和DRIVERS
。所以我將我的 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"
udevadm monitor
在這裡SUBSYSTEM
,由於第一個命令DRIVERS
(它就像一個魅力!希望它有所幫助。:)