1、Linux下HID 设备Linux下HID 设备,如果非标准的输入设备(Keypad/MOUSE/JoyStick/input event device).将会把信息转入hid device的设备结点。这一点可以参见内核的关于hiddev的文档 http:/lxr.free- 它在 Documentation/usb/hiddev.txt 目录下,大致有这样一个流程, usb.c - hid-core.c - hid-input.c - keyboard/mouse/joystick/event | | - hiddev.c - POWER / MONITOR CONTROL 关于操作非标准的
2、HID设备,可以参见如下文档. 在设备文件系统中,会产生的/dev/hiddev0这样的结点,这类设备主设备号为 180,从设备号最低为96Cat /proc/device | grep hiddevmknod /dev/hiddev0 c 180 96mknod /dev/hiddev1 c 180 97mknod /dev/hiddev2 c 180 98mknod /dev/hiddev3 c 180 99 .mknod /dev/hiddev15 c 180 111对此设备结点的处理有两种接口,一种是read(),另一种是ioctl(); read(): This is the eve
3、nt interface. When the HID device performs an interrupt transfer, indicating a change of state, data will be made available at the associated hiddev device with the content of a struct hiddev_event:struct hiddev_event unsigned hid; signed int value; ; containing the HID usage identifier for the stat
4、us that changed, and the value that it was changed to. ioctl(): This is the control interface. There are a number of controls:HIDIOCGVERSIONint (read)Gets the version code out of the hiddev driver.HIDIOCAPPLICATION(none)This ioctl call returns the HID application usage associated with the hid device
5、. The third argument to ioctl() specifies which application index to get. This is useful when the device has more than one application collection. If the index is invalid (greater or equal to the number of application collections this device has) the ioctl returns -1. You can find out beforehand how
6、 many application collections the device has from the num_applications field from the hiddev_devinfo structure.HIDIOCGDEVINFOstruct hiddev_devinfo (read)Gets a hiddev_devinfo structure which describes the device.HIDIOCGSTRINGstruct struct hiddev_string_descriptor (read/write)Gets a string descriptor
7、 from the device. The caller must fill in the index field to indicate which descriptor should be returned.HIDIOCINITREPORT Instructs the kernel to retrieve all input and feature report values from the device. At this point, all the usage structures will contain current values for the device, and wil
8、l maintain it as the device changes.HIDIOCGNAMEstring (variable length)Gets the device nameHIDIOCGREPORTstruct hiddev_report_info (write)Instructs the kernel to get a feature or input report from the device, in order to selectively update the usage structures (in contrast to INITREPORT).HIDIOCSREPOR
9、Tstruct hiddev_report_info (write)Instructs the kernel to send a report to the device. This report can be filled in by the user throughHIDIOCSUSAGE calls (below) to fill in individual usage values in the report before sending the report in full to the device.HIDIOCGREPORTINFOstruct hiddev_report_inf
10、o (read/write)Fills in a hiddev_report_info structure for the user. The report is looked up by type (input, output or feature) and id, so these fields must be filled in by the user. The ID can be absolute - the actual report id as reported by the device - or relative - HID_REPORT_ID_FIRST for the fi
11、rst report, and (HID_REPORT_ID_NEXT | report_id) for the next report after report_id. Without a-priori information about report ids, the right way to use this ioctl is to use the relative IDs above to enumerate the valid IDs. The ioctl returns non-zero when there is no more next ID. The real report
12、ID is filled into the returned hiddev_report_info structure.HIDIOCGFIELDINFOstruct hiddev_field_info (read/write)Returns the field information associated with a report in a hiddev_field_info structure. The user must fill in report_id and report_type in this structure, as above. The field_index shoul
13、d also be filled in, which should be a number from 0 and maxfield-1, as returned from a previous HIDIOCGREPORTINFO call.HIDIOCGUCODEstruct hiddev_usage_ref (read/write)Returns the usage_code in a hiddev_usage_ref structure, given that given its report type, report id, field index, and index within t
14、he field have already been filled into the structure.HIDIOCGUSAGEstruct hiddev_usage_ref (read/write)Returns the value of a usage in a hiddev_usage_ref structure. The usage to be retrieved can be specified as above, or the user can choose to fill in the report_type field and specify the report_id as
15、HID_REPORT_ID_UNKNOWN. In this case, the hiddev_usage_ref will be filled in with the report and field infomation associated with this usage if it is found.HIDIOCSUSAGEstruct hiddev_usage_ref (write)Sets the value of a usage in an output report.1 测试代码/*2 Author : Andrew Huang 3 4 http:/lxr.free-5 6 *
16、/7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 17 static void showReports(int fd, unsigned report_type);18 static void show_all_report(int fd);19 static int read_event(int fd);20 21 int main()22 23 char dev_name64 = /dev/hiddev0;24 int fd
17、 = -1;25 int version;26 char name100;27 struct hiddev_devinfo dinfo;28 29 30 fd = open(dev_name,O_RDWR);31 if(fd = -1)32 33 fprintf(stderr,open %s failuren,dev_name);34 return -1;35 36 37 38 printf(%s inforn,dev_name); 39 40 if (ioctl(fd, HIDIOCGVERSION, &version) 16) & 0xFFFF, version & 0xFFFF);45
18、if (version != HID_VERSION)46 printf(WARNING: version does not match compile-time versionn);47 48 49 if (ioctl(fd, HIDIOCGDEVINFO, &dinfo) 0)50 perror(HIDIOCGDEVINFO);51 else52 53 printf(HIDIOCGDEVINFO: bustype=%d busnum=%d devnum=%d ifnum=%dn54 tvendor=0x%04hx product=0x%04hx version=0x%04hxn55 tnu
19、m_applications=%dn,56 dinfo.bustype, dinfo.busnum, dinfo.devnum, dinfo.ifnum,57 dinfo.vendor, dinfo.product, dinfo.version, dinfo.num_applications);58 59 60 if (ioctl(fd, HIDIOCGNAME(99), name) = 0)97 98 printf(HIDIOCGREPORTINFO: report_id=0x%X (%u fields)n,99 rinfo.report_id, rinfo.num_fields);100
20、for (i = 0; i rinfo.num_fields; i+)101 102 finfo.report_type = rinfo.report_type;103 finfo.report_id = rinfo.report_id;104 finfo.field_index = i;105 ioctl(fd, HIDIOCGFIELDINFO, &finfo);106 107 printf(HIDIOCGFIELDINFO: field_index=%u maxusage=%u flags=0x%Xn108 tphysical=0x%X logical=0x%X application=
21、0x%Xn109 tlogical_minimum=%d,maximum=%d physical_minimum=%d,maximum=%dn,110 finfo.field_index, finfo.maxusage, finfo.flags,111 finfo.physical, finfo.logical, finfo.application,112 finfo.logical_minimum, finfo.logical_maximum,113 finfo.physical_minimum, finfo.physical_maximum);114 115 for (j = 0; j u
22、sage_index=%u usage_code=0x%X () value=%dn,125 uref.usage_index,126 uref.usage_code,127 / controlName(uref.usage_code),128 uref.value);129 130 131 132 printf(n);133 134 rinfo.report_id |= HID_REPORT_ID_NEXT;135 ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);136 137 138 139 140 void show_all_report(int f
23、d)141 142 143 struct hiddev_report_info rinfo;144 struct hiddev_field_info finfo;145 struct hiddev_usage_ref uref;146 int rtype, i, j;147 char *rtype_str;148 149 for (rtype = HID_REPORT_TYPE_MIN; rtype = 0) 161 fprintf(stdout, Report id: %d (%d fields)n,162 rinfo.report_id, rinfo.num_fields);163 for
24、 (i = 0; i rinfo.num_fields; i+) 164 memset(&finfo, 0, sizeof(finfo);165 finfo.report_type = rinfo.report_type;166 finfo.report_id = rinfo.report_id;167 finfo.field_index = i;168 ioctl(fd, HIDIOCGFIELDINFO, &finfo);169 fprintf(stdout, Field: %d: app: %04x phys %04x 170 flags %x (%d usages) unit %x exp %dn, 171 i, finfo.application, finfo.physical, finfo.flags,172 finfo.maxusage, finfo.unit, finfo.unit_exponent);173 memset(&uref, 0, sizeof(uref);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1