gpt4 book ai didi

linux - 符号链接(symbolic link)上的意外 ioctl 行为

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:00 25 4
gpt4 key购买 nike

我目前正在使用 HID 设备。它有两种设置模式:HID 和 RS232。所以我写了一个小脚本来将他切换到 RS232,只要它作为 HID 设备插入,使用这个 udev 规则:

ENV{ID_VENDOR}=="Vendor", ENV{ID_VENDOR_ID}=="001d", ENV{ID_USB_DRIVER}=="usbhid",\
SYMLINK+="hid_device", RUN+="/path/to/HID_to_serial"

脚本如下:

// HID_to_serial.c
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

//#define DEFAULT_DEVICE_PATH "/dev/hidraw0"
#define DEFAULT_DEVICE_PATH "/dev/hid_device"

int main(int argc, char **argv)
{
int fd = open(DEFAULT_DEVICE_PATH, O_RDWR);

if (fd < 0)
{
perror("Unable to open device");
return 1;
}

// Very specific report descriptor
const char buf[64] = { 0x02, 0x0b, 0x02, 0x04, 0x42, 0x40, 0x10, 0x42,
0x62, 0x10, 0x42, 0x42, 0x03, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

int res = ioctl(fd, _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, 64), buf);
if (res < 0)
perror("ioctl");
else
printf("Device was succesfully switched back to serial mode!\n");

return 0;
}

现在,通常,当我插入设备时,Linux 会为它提供 /dev/hidraw0 文件。当我在 /dev/hidraw0 上使用我的脚本时,它工作得很好。脚本中使用的报告描述符是正确的,一切都按预期工作:HID 设备切换回 RS232 模式。

但是,当我尝试在由 udev 规则创建的 /dev/hid_device 符号链接(symbolic link)上使用我的脚本时,它在 99% 的时间里都不起作用,告诉我 ioctl:无效参数。更奇怪的是它有效,但只有 1% 的时间(可能更少)。

有没有人知道这可能来自哪里,以及如何解决或解决它?提前致谢。

最佳答案

我发现了问题。

它在于我的 udev 规则:它不够精确。

当我插入设备时,linux 在 /dev/ 中创建两个 character special 设备文件:hidraw0input/event15 (在我的例子中)。它们共享许多环境值,例如它们的 ID_VENDOR_ID 或它们的 ID_USB_DRIVER。但是,它们不共享相同的 MAJOR

所以我所做的是在我的 udev 规则中添加 ENV{MAJOR}=="correct_major",现在我的符号链接(symbolic link)链接到正确的设备文件。

这也解释了为什么它有时有效:我猜是因为 udev 规则中缺少详细信息,有时符号链接(symbolic link)链接到正确的设备文件,有时不是。

关于linux - 符号链接(symbolic link)上的意外 ioctl 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41119401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com