Android在hid_add_device函数中过滤特定设备
时间: 2024-01-08 10:03:54 浏览: 81
在hid_add_device函数中过滤特定设备,可以使用hid_device_id结构体中的vendor和product字段来指定特定的设备。具体步骤如下:
1. 首先,在hid_device_id结构体中设置vendor和product字段的值,以匹配你要过滤的设备。
```c
static const struct hid_device_id my_hid_devices[] = {
{ HID_USB_DEVICE(0x1234, 0xabcd) }, /* vendor: 1234, product: abcd */
{ },
};
```
2. 在hid_driver结构体中,将driver.name字段设置为你的驱动程序名称,将driver.id_table字段设置为你的hid_device_id数组。
```c
static struct hid_driver my_hid_driver = {
.name = "my_hid_driver",
.id_table = my_hid_devices,
.probe = my_hid_probe,
.disconnect = my_hid_disconnect,
};
```
3. 在hid_add_device函数中,将设备的vendor和product字段与你的hid_device_id数组中的值进行匹配。如果匹配成功,则调用hid_alloc_device函数分配设备,并将设备添加到系统中。
```c
static int my_hid_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
if (id->vendor == 0x1234 && id->product == 0xabcd) {
/* Allocate a new HID device */
struct hid_device *new_dev = hid_alloc_device();
if (!new_dev)
return -ENOMEM;
/* Set the HID device properties */
new_dev->dev.parent = &hdev->dev;
new_dev->bus = hdev->bus;
new_dev->driver = &my_hid_driver;
new_dev->ll_driver = &my_hid_ll_driver;
new_dev->hiddev_rdesc_size = sizeof(struct my_hid_device);
new_dev->hiddev_rdesc = my_hid_device_rdesc;
/* Add the new HID device to the system */
hid_hw_start(new_dev, HID_CONNECT_DEFAULT);
}
return 0;
}
```
通过这种方式,你就可以在hid_add_device函数中过滤特定设备了。
阅读全文
相关推荐


















