USB接口HID类设备——2)Linux源码

一、源码目录
它在 Linux-5.2/Documentation/hid/hiddev.txt 目录下,大致有这样一个流程

如上图所示,Linux USB子系统提供两种HID events 接口:

1)* the input subsystem, which converts HID events into normal input device interfaces (such as keyboard, mouse and joystick) and a normalised event interface - see Documentation/input/input.rst
2)* the hiddev interface, which provides fairly raw HID events, In addition to the normal input type HID devices, USB also uses the human interface device protocols for things that are not really human interfaces, but have similar sorts of communication needs. The two big examples for this are power devices (especially uninterruptable power supplies) and monitor control on higher end monitors.

二、代码应用

因项目需求,采用the hiddev interface(即上图中的红色部分),用HID类设备传输数据来做控制应用,而不是传统的人机交互

Linux内核已经实现了底层驱动,并提供了API(open,read等),这样就可以在应用层直接调用,降低了开发难度

1.打开HID设备(内核中了实现open函数)

fd = open("/dev/usb/hiddev0", O_TRUNC | O_RDWR | O_NONBLOCK, 777);

The hiddev interface is a char interface using the normal USB major,
with the minor numbers starting at 96 and finishing at 111. Therefore,
you need the following commands:
mknod /dev/usb/hiddev0 c 180 96
mknod /dev/usb/hiddev1 c 180 97
mknod /dev/usb/hiddev2 c 180 98
mknod /dev/usb/hiddev3 c 180 99
mknod /dev/usb/hiddev4 c 180 100
mknod /dev/usb/hiddev5 c 180 101
mknod /dev/usb/hiddev6 c 180 102
mknod /dev/usb/hiddev7 c 180 103
mknod /dev/usb/hiddev8 c 180 104
mknod /dev/usb/hiddev9 c 180 105
mknod /dev/usb/hiddev10 c 180 106
mknod /dev/usb/hiddev11 c 180 107
mknod /dev/usb/hiddev12 c 180 108
mknod /dev/usb/hiddev13 c 180 109
mknod /dev/usb/hiddev14 c 180 110
mknod /dev/usb/hiddev15 c 180 111

2.读取HID设备信息(控制输入,ioctl+HIDIOCGVERSION 等)

struct hiddev_report_info rinfo;    //存放报表信息的结构体
struct hiddev_field_info finfo;     //存放字段信息的结构体
struct hiddev_usage_ref uref;       //存放已使用的参考信息

ioctl(fd, HIDIOCGDEVINFO, &device_info)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值