18c2ecf20Sopenharmony_ci================================================
28c2ecf20Sopenharmony_ciCare and feeding of your Human Interface Devices
38c2ecf20Sopenharmony_ci================================================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciIntroduction
68c2ecf20Sopenharmony_ci============
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ciIn addition to the normal input type HID devices, USB also uses the
98c2ecf20Sopenharmony_cihuman interface device protocols for things that are not really human
108c2ecf20Sopenharmony_ciinterfaces, but have similar sorts of communication needs. The two big
118c2ecf20Sopenharmony_ciexamples for this are power devices (especially uninterruptable power
128c2ecf20Sopenharmony_cisupplies) and monitor control on higher end monitors.
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciTo support these disparate requirements, the Linux USB system provides
158c2ecf20Sopenharmony_ciHID events to two separate interfaces:
168c2ecf20Sopenharmony_ci* the input subsystem, which converts HID events into normal input
178c2ecf20Sopenharmony_cidevice interfaces (such as keyboard, mouse and joystick) and a
188c2ecf20Sopenharmony_cinormalised event interface - see Documentation/input/input.rst
198c2ecf20Sopenharmony_ci* the hiddev interface, which provides fairly raw HID events
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciThe data flow for a HID event produced by a device is something like
228c2ecf20Sopenharmony_cithe following::
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci usb.c ---> hid-core.c  ----> hid-input.c ----> [keyboard/mouse/joystick/event]
258c2ecf20Sopenharmony_ci                         |
268c2ecf20Sopenharmony_ci                         |
278c2ecf20Sopenharmony_ci                          --> hiddev.c ----> POWER / MONITOR CONTROL
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciIn addition, other subsystems (apart from USB) can potentially feed
308c2ecf20Sopenharmony_cievents into the input subsystem, but these have no effect on the hid
318c2ecf20Sopenharmony_cidevice interface.
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ciUsing the HID Device Interface
348c2ecf20Sopenharmony_ci==============================
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciThe hiddev interface is a char interface using the normal USB major,
378c2ecf20Sopenharmony_ciwith the minor numbers starting at 96 and finishing at 111. Therefore,
388c2ecf20Sopenharmony_ciyou need the following commands::
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev0 c 180 96
418c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev1 c 180 97
428c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev2 c 180 98
438c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev3 c 180 99
448c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev4 c 180 100
458c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev5 c 180 101
468c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev6 c 180 102
478c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev7 c 180 103
488c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev8 c 180 104
498c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev9 c 180 105
508c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev10 c 180 106
518c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev11 c 180 107
528c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev12 c 180 108
538c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev13 c 180 109
548c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev14 c 180 110
558c2ecf20Sopenharmony_ci	mknod /dev/usb/hiddev15 c 180 111
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ciSo you point your hiddev compliant user-space program at the correct
588c2ecf20Sopenharmony_ciinterface for your device, and it all just works.
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciAssuming that you have a hiddev compliant user-space program, of
618c2ecf20Sopenharmony_cicourse. If you need to write one, read on.
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ciThe HIDDEV API
658c2ecf20Sopenharmony_ci==============
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciThis description should be read in conjunction with the HID
688c2ecf20Sopenharmony_cispecification, freely available from https://www.usb.org, and
698c2ecf20Sopenharmony_ciconveniently linked of http://www.linux-usb.org.
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ciThe hiddev API uses a read() interface, and a set of ioctl() calls.
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciHID devices exchange data with the host computer using data
748c2ecf20Sopenharmony_cibundles called "reports".  Each report is divided into "fields",
758c2ecf20Sopenharmony_cieach of which can have one or more "usages".  In the hid-core,
768c2ecf20Sopenharmony_cieach one of these usages has a single signed 32 bit value.
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ciread():
798c2ecf20Sopenharmony_ci-------
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ciThis is the event interface.  When the HID device's state changes,
828c2ecf20Sopenharmony_ciit performs an interrupt transfer containing a report which contains
838c2ecf20Sopenharmony_cithe changed value.  The hid-core.c module parses the report, and
848c2ecf20Sopenharmony_cireturns to hiddev.c the individual usages that have changed within
858c2ecf20Sopenharmony_cithe report.  In its basic mode, the hiddev will make these individual
868c2ecf20Sopenharmony_ciusage changes available to the reader using a struct hiddev_event::
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci       struct hiddev_event {
898c2ecf20Sopenharmony_ci           unsigned hid;
908c2ecf20Sopenharmony_ci           signed int value;
918c2ecf20Sopenharmony_ci       };
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cicontaining the HID usage identifier for the status that changed, and
948c2ecf20Sopenharmony_cithe value that it was changed to. Note that the structure is defined
958c2ecf20Sopenharmony_ciwithin <linux/hiddev.h>, along with some other useful #defines and
968c2ecf20Sopenharmony_cistructures.  The HID usage identifier is a composite of the HID usage
978c2ecf20Sopenharmony_cipage shifted to the 16 high order bits ORed with the usage code.  The
988c2ecf20Sopenharmony_cibehavior of the read() function can be modified using the HIDIOCSFLAG
998c2ecf20Sopenharmony_ciioctl() described below.
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciioctl():
1038c2ecf20Sopenharmony_ci--------
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ciThis is the control interface. There are a number of controls:
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciHIDIOCGVERSION
1088c2ecf20Sopenharmony_ci  - int (read)
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci Gets the version code out of the hiddev driver.
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ciHIDIOCAPPLICATION
1138c2ecf20Sopenharmony_ci  - (none)
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciThis ioctl call returns the HID application usage associated with the
1168c2ecf20Sopenharmony_cihid device. The third argument to ioctl() specifies which application
1178c2ecf20Sopenharmony_ciindex to get. This is useful when the device has more than one
1188c2ecf20Sopenharmony_ciapplication collection. If the index is invalid (greater or equal to
1198c2ecf20Sopenharmony_cithe number of application collections this device has) the ioctl
1208c2ecf20Sopenharmony_cireturns -1. You can find out beforehand how many application
1218c2ecf20Sopenharmony_cicollections the device has from the num_applications field from the
1228c2ecf20Sopenharmony_cihiddev_devinfo structure.
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ciHIDIOCGCOLLECTIONINFO
1258c2ecf20Sopenharmony_ci  - struct hiddev_collection_info (read/write)
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ciThis returns a superset of the information above, providing not only
1288c2ecf20Sopenharmony_ciapplication collections, but all the collections the device has.  It
1298c2ecf20Sopenharmony_cialso returns the level the collection lives in the hierarchy.
1308c2ecf20Sopenharmony_ciThe user passes in a hiddev_collection_info struct with the index
1318c2ecf20Sopenharmony_cifield set to the index that should be returned.  The ioctl fills in
1328c2ecf20Sopenharmony_cithe other fields.  If the index is larger than the last collection
1338c2ecf20Sopenharmony_ciindex, the ioctl returns -1 and sets errno to -EINVAL.
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciHIDIOCGDEVINFO
1368c2ecf20Sopenharmony_ci  - struct hiddev_devinfo (read)
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ciGets a hiddev_devinfo structure which describes the device.
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciHIDIOCGSTRING
1418c2ecf20Sopenharmony_ci  - struct hiddev_string_descriptor (read/write)
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciGets a string descriptor from the device. The caller must fill in the
1448c2ecf20Sopenharmony_ci"index" field to indicate which descriptor should be returned.
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ciHIDIOCINITREPORT
1478c2ecf20Sopenharmony_ci  - (none)
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciInstructs the kernel to retrieve all input and feature report values
1508c2ecf20Sopenharmony_cifrom the device. At this point, all the usage structures will contain
1518c2ecf20Sopenharmony_cicurrent values for the device, and will maintain it as the device
1528c2ecf20Sopenharmony_cichanges.  Note that the use of this ioctl is unnecessary in general,
1538c2ecf20Sopenharmony_cisince later kernels automatically initialize the reports from the
1548c2ecf20Sopenharmony_cidevice at attach time.
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciHIDIOCGNAME
1578c2ecf20Sopenharmony_ci  - string (variable length)
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ciGets the device name
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ciHIDIOCGREPORT
1628c2ecf20Sopenharmony_ci  - struct hiddev_report_info (write)
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ciInstructs the kernel to get a feature or input report from the device,
1658c2ecf20Sopenharmony_ciin order to selectively update the usage structures (in contrast to
1668c2ecf20Sopenharmony_ciINITREPORT).
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ciHIDIOCSREPORT
1698c2ecf20Sopenharmony_ci  - struct hiddev_report_info (write)
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ciInstructs the kernel to send a report to the device. This report can
1728c2ecf20Sopenharmony_cibe filled in by the user through HIDIOCSUSAGE calls (below) to fill in
1738c2ecf20Sopenharmony_ciindividual usage values in the report before sending the report in full
1748c2ecf20Sopenharmony_cito the device.
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ciHIDIOCGREPORTINFO
1778c2ecf20Sopenharmony_ci  - struct hiddev_report_info (read/write)
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ciFills in a hiddev_report_info structure for the user. The report is
1808c2ecf20Sopenharmony_cilooked up by type (input, output or feature) and id, so these fields
1818c2ecf20Sopenharmony_cimust be filled in by the user. The ID can be absolute -- the actual
1828c2ecf20Sopenharmony_cireport id as reported by the device -- or relative --
1838c2ecf20Sopenharmony_ciHID_REPORT_ID_FIRST for the first report, and (HID_REPORT_ID_NEXT |
1848c2ecf20Sopenharmony_cireport_id) for the next report after report_id. Without a-priori
1858c2ecf20Sopenharmony_ciinformation about report ids, the right way to use this ioctl is to
1868c2ecf20Sopenharmony_ciuse the relative IDs above to enumerate the valid IDs. The ioctl
1878c2ecf20Sopenharmony_cireturns non-zero when there is no more next ID. The real report ID is
1888c2ecf20Sopenharmony_cifilled into the returned hiddev_report_info structure.
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ciHIDIOCGFIELDINFO
1918c2ecf20Sopenharmony_ci  - struct hiddev_field_info (read/write)
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ciReturns the field information associated with a report in a
1948c2ecf20Sopenharmony_cihiddev_field_info structure. The user must fill in report_id and
1958c2ecf20Sopenharmony_cireport_type in this structure, as above. The field_index should also
1968c2ecf20Sopenharmony_cibe filled in, which should be a number from 0 and maxfield-1, as
1978c2ecf20Sopenharmony_cireturned from a previous HIDIOCGREPORTINFO call.
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ciHIDIOCGUCODE
2008c2ecf20Sopenharmony_ci  - struct hiddev_usage_ref (read/write)
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ciReturns the usage_code in a hiddev_usage_ref structure, given that
2038c2ecf20Sopenharmony_cigiven its report type, report id, field index, and index within the
2048c2ecf20Sopenharmony_cifield have already been filled into the structure.
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciHIDIOCGUSAGE
2078c2ecf20Sopenharmony_ci  - struct hiddev_usage_ref (read/write)
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ciReturns the value of a usage in a hiddev_usage_ref structure. The
2108c2ecf20Sopenharmony_ciusage to be retrieved can be specified as above, or the user can
2118c2ecf20Sopenharmony_cichoose to fill in the report_type field and specify the report_id as
2128c2ecf20Sopenharmony_ciHID_REPORT_ID_UNKNOWN. In this case, the hiddev_usage_ref will be
2138c2ecf20Sopenharmony_cifilled in with the report and field information associated with this
2148c2ecf20Sopenharmony_ciusage if it is found.
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ciHIDIOCSUSAGE
2178c2ecf20Sopenharmony_ci  - struct hiddev_usage_ref (write)
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ciSets the value of a usage in an output report.  The user fills in
2208c2ecf20Sopenharmony_cithe hiddev_usage_ref structure as above, but additionally fills in
2218c2ecf20Sopenharmony_cithe value field.
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ciHIDIOGCOLLECTIONINDEX
2248c2ecf20Sopenharmony_ci  - struct hiddev_usage_ref (write)
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ciReturns the collection index associated with this usage.  This
2278c2ecf20Sopenharmony_ciindicates where in the collection hierarchy this usage sits.
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ciHIDIOCGFLAG
2308c2ecf20Sopenharmony_ci  - int (read)
2318c2ecf20Sopenharmony_ciHIDIOCSFLAG
2328c2ecf20Sopenharmony_ci  - int (write)
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ciThese operations respectively inspect and replace the mode flags
2358c2ecf20Sopenharmony_cithat influence the read() call above.  The flags are as follows:
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci    HIDDEV_FLAG_UREF
2388c2ecf20Sopenharmony_ci      - read() calls will now return
2398c2ecf20Sopenharmony_ci        struct hiddev_usage_ref instead of struct hiddev_event.
2408c2ecf20Sopenharmony_ci        This is a larger structure, but in situations where the
2418c2ecf20Sopenharmony_ci        device has more than one usage in its reports with the
2428c2ecf20Sopenharmony_ci        same usage code, this mode serves to resolve such
2438c2ecf20Sopenharmony_ci        ambiguity.
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci    HIDDEV_FLAG_REPORT
2468c2ecf20Sopenharmony_ci      - This flag can only be used in conjunction
2478c2ecf20Sopenharmony_ci        with HIDDEV_FLAG_UREF.  With this flag set, when the device
2488c2ecf20Sopenharmony_ci        sends a report, a struct hiddev_usage_ref will be returned
2498c2ecf20Sopenharmony_ci        to read() filled in with the report_type and report_id, but
2508c2ecf20Sopenharmony_ci        with field_index set to FIELD_INDEX_NONE.  This serves as
2518c2ecf20Sopenharmony_ci        additional notification when the device has sent a report.
252