162306a36Sopenharmony_ci================================================
262306a36Sopenharmony_ciCare and feeding of your Human Interface Devices
362306a36Sopenharmony_ci================================================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciIntroduction
662306a36Sopenharmony_ci============
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciIn addition to the normal input type HID devices, USB also uses the
962306a36Sopenharmony_cihuman interface device protocols for things that are not really human
1062306a36Sopenharmony_ciinterfaces, but have similar sorts of communication needs. The two big
1162306a36Sopenharmony_ciexamples for this are power devices (especially uninterruptible power
1262306a36Sopenharmony_cisupplies) and monitor control on higher end monitors.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciTo support these disparate requirements, the Linux USB system provides
1562306a36Sopenharmony_ciHID events to two separate interfaces:
1662306a36Sopenharmony_ci* the input subsystem, which converts HID events into normal input
1762306a36Sopenharmony_cidevice interfaces (such as keyboard, mouse and joystick) and a
1862306a36Sopenharmony_cinormalised event interface - see Documentation/input/input.rst
1962306a36Sopenharmony_ci* the hiddev interface, which provides fairly raw HID events
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciThe data flow for a HID event produced by a device is something like
2262306a36Sopenharmony_cithe following::
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci usb.c ---> hid-core.c  ----> hid-input.c ----> [keyboard/mouse/joystick/event]
2562306a36Sopenharmony_ci                         |
2662306a36Sopenharmony_ci                         |
2762306a36Sopenharmony_ci                          --> hiddev.c ----> POWER / MONITOR CONTROL
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciIn addition, other subsystems (apart from USB) can potentially feed
3062306a36Sopenharmony_cievents into the input subsystem, but these have no effect on the HID
3162306a36Sopenharmony_cidevice interface.
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciUsing the HID Device Interface
3462306a36Sopenharmony_ci==============================
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciThe hiddev interface is a char interface using the normal USB major,
3762306a36Sopenharmony_ciwith the minor numbers starting at 96 and finishing at 111. Therefore,
3862306a36Sopenharmony_ciyou need the following commands::
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	mknod /dev/usb/hiddev0 c 180 96
4162306a36Sopenharmony_ci	mknod /dev/usb/hiddev1 c 180 97
4262306a36Sopenharmony_ci	mknod /dev/usb/hiddev2 c 180 98
4362306a36Sopenharmony_ci	mknod /dev/usb/hiddev3 c 180 99
4462306a36Sopenharmony_ci	mknod /dev/usb/hiddev4 c 180 100
4562306a36Sopenharmony_ci	mknod /dev/usb/hiddev5 c 180 101
4662306a36Sopenharmony_ci	mknod /dev/usb/hiddev6 c 180 102
4762306a36Sopenharmony_ci	mknod /dev/usb/hiddev7 c 180 103
4862306a36Sopenharmony_ci	mknod /dev/usb/hiddev8 c 180 104
4962306a36Sopenharmony_ci	mknod /dev/usb/hiddev9 c 180 105
5062306a36Sopenharmony_ci	mknod /dev/usb/hiddev10 c 180 106
5162306a36Sopenharmony_ci	mknod /dev/usb/hiddev11 c 180 107
5262306a36Sopenharmony_ci	mknod /dev/usb/hiddev12 c 180 108
5362306a36Sopenharmony_ci	mknod /dev/usb/hiddev13 c 180 109
5462306a36Sopenharmony_ci	mknod /dev/usb/hiddev14 c 180 110
5562306a36Sopenharmony_ci	mknod /dev/usb/hiddev15 c 180 111
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ciSo you point your hiddev compliant user-space program at the correct
5862306a36Sopenharmony_ciinterface for your device, and it all just works.
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciAssuming that you have a hiddev compliant user-space program, of
6162306a36Sopenharmony_cicourse. If you need to write one, read on.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ciThe HIDDEV API
6562306a36Sopenharmony_ci==============
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ciThis description should be read in conjunction with the HID
6862306a36Sopenharmony_cispecification, freely available from https://www.usb.org, and
6962306a36Sopenharmony_ciconveniently linked of http://www.linux-usb.org.
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciThe hiddev API uses a read() interface, and a set of ioctl() calls.
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ciHID devices exchange data with the host computer using data
7462306a36Sopenharmony_cibundles called "reports".  Each report is divided into "fields",
7562306a36Sopenharmony_cieach of which can have one or more "usages".  In the hid-core,
7662306a36Sopenharmony_cieach one of these usages has a single signed 32-bit value.
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ciread():
7962306a36Sopenharmony_ci-------
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciThis is the event interface.  When the HID device's state changes,
8262306a36Sopenharmony_ciit performs an interrupt transfer containing a report which contains
8362306a36Sopenharmony_cithe changed value.  The hid-core.c module parses the report, and
8462306a36Sopenharmony_cireturns to hiddev.c the individual usages that have changed within
8562306a36Sopenharmony_cithe report.  In its basic mode, the hiddev will make these individual
8662306a36Sopenharmony_ciusage changes available to the reader using a struct hiddev_event::
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci       struct hiddev_event {
8962306a36Sopenharmony_ci           unsigned hid;
9062306a36Sopenharmony_ci           signed int value;
9162306a36Sopenharmony_ci       };
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cicontaining the HID usage identifier for the status that changed, and
9462306a36Sopenharmony_cithe value that it was changed to. Note that the structure is defined
9562306a36Sopenharmony_ciwithin <linux/hiddev.h>, along with some other useful #defines and
9662306a36Sopenharmony_cistructures.  The HID usage identifier is a composite of the HID usage
9762306a36Sopenharmony_cipage shifted to the 16 high order bits ORed with the usage code.  The
9862306a36Sopenharmony_cibehavior of the read() function can be modified using the HIDIOCSFLAG
9962306a36Sopenharmony_ciioctl() described below.
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciioctl():
10362306a36Sopenharmony_ci--------
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ciThis is the control interface. There are a number of controls:
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciHIDIOCGVERSION
10862306a36Sopenharmony_ci  - int (read)
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci Gets the version code out of the hiddev driver.
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciHIDIOCAPPLICATION
11362306a36Sopenharmony_ci  - (none)
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciThis ioctl call returns the HID application usage associated with the
11662306a36Sopenharmony_ciHID device. The third argument to ioctl() specifies which application
11762306a36Sopenharmony_ciindex to get. This is useful when the device has more than one
11862306a36Sopenharmony_ciapplication collection. If the index is invalid (greater or equal to
11962306a36Sopenharmony_cithe number of application collections this device has) the ioctl
12062306a36Sopenharmony_cireturns -1. You can find out beforehand how many application
12162306a36Sopenharmony_cicollections the device has from the num_applications field from the
12262306a36Sopenharmony_cihiddev_devinfo structure.
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ciHIDIOCGCOLLECTIONINFO
12562306a36Sopenharmony_ci  - struct hiddev_collection_info (read/write)
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciThis returns a superset of the information above, providing not only
12862306a36Sopenharmony_ciapplication collections, but all the collections the device has.  It
12962306a36Sopenharmony_cialso returns the level the collection lives in the hierarchy.
13062306a36Sopenharmony_ciThe user passes in a hiddev_collection_info struct with the index
13162306a36Sopenharmony_cifield set to the index that should be returned.  The ioctl fills in
13262306a36Sopenharmony_cithe other fields.  If the index is larger than the last collection
13362306a36Sopenharmony_ciindex, the ioctl returns -1 and sets errno to -EINVAL.
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciHIDIOCGDEVINFO
13662306a36Sopenharmony_ci  - struct hiddev_devinfo (read)
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ciGets a hiddev_devinfo structure which describes the device.
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ciHIDIOCGSTRING
14162306a36Sopenharmony_ci  - struct hiddev_string_descriptor (read/write)
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciGets a string descriptor from the device. The caller must fill in the
14462306a36Sopenharmony_ci"index" field to indicate which descriptor should be returned.
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ciHIDIOCINITREPORT
14762306a36Sopenharmony_ci  - (none)
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ciInstructs the kernel to retrieve all input and feature report values
15062306a36Sopenharmony_cifrom the device. At this point, all the usage structures will contain
15162306a36Sopenharmony_cicurrent values for the device, and will maintain it as the device
15262306a36Sopenharmony_cichanges.  Note that the use of this ioctl is unnecessary in general,
15362306a36Sopenharmony_cisince later kernels automatically initialize the reports from the
15462306a36Sopenharmony_cidevice at attach time.
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ciHIDIOCGNAME
15762306a36Sopenharmony_ci  - string (variable length)
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ciGets the device name
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ciHIDIOCGREPORT
16262306a36Sopenharmony_ci  - struct hiddev_report_info (write)
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ciInstructs the kernel to get a feature or input report from the device,
16562306a36Sopenharmony_ciin order to selectively update the usage structures (in contrast to
16662306a36Sopenharmony_ciINITREPORT).
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ciHIDIOCSREPORT
16962306a36Sopenharmony_ci  - struct hiddev_report_info (write)
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ciInstructs the kernel to send a report to the device. This report can
17262306a36Sopenharmony_cibe filled in by the user through HIDIOCSUSAGE calls (below) to fill in
17362306a36Sopenharmony_ciindividual usage values in the report before sending the report in full
17462306a36Sopenharmony_cito the device.
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ciHIDIOCGREPORTINFO
17762306a36Sopenharmony_ci  - struct hiddev_report_info (read/write)
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ciFills in a hiddev_report_info structure for the user. The report is
18062306a36Sopenharmony_cilooked up by type (input, output or feature) and id, so these fields
18162306a36Sopenharmony_cimust be filled in by the user. The ID can be absolute -- the actual
18262306a36Sopenharmony_cireport id as reported by the device -- or relative --
18362306a36Sopenharmony_ciHID_REPORT_ID_FIRST for the first report, and (HID_REPORT_ID_NEXT |
18462306a36Sopenharmony_cireport_id) for the next report after report_id. Without a priori
18562306a36Sopenharmony_ciinformation about report ids, the right way to use this ioctl is to
18662306a36Sopenharmony_ciuse the relative IDs above to enumerate the valid IDs. The ioctl
18762306a36Sopenharmony_cireturns non-zero when there is no more next ID. The real report ID is
18862306a36Sopenharmony_cifilled into the returned hiddev_report_info structure.
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ciHIDIOCGFIELDINFO
19162306a36Sopenharmony_ci  - struct hiddev_field_info (read/write)
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ciReturns the field information associated with a report in a
19462306a36Sopenharmony_cihiddev_field_info structure. The user must fill in report_id and
19562306a36Sopenharmony_cireport_type in this structure, as above. The field_index should also
19662306a36Sopenharmony_cibe filled in, which should be a number from 0 and maxfield-1, as
19762306a36Sopenharmony_cireturned from a previous HIDIOCGREPORTINFO call.
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ciHIDIOCGUCODE
20062306a36Sopenharmony_ci  - struct hiddev_usage_ref (read/write)
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ciReturns the usage_code in a hiddev_usage_ref structure, given that
20362306a36Sopenharmony_ciits report type, report id, field index, and index within the
20462306a36Sopenharmony_cifield have already been filled into the structure.
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ciHIDIOCGUSAGE
20762306a36Sopenharmony_ci  - struct hiddev_usage_ref (read/write)
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ciReturns the value of a usage in a hiddev_usage_ref structure. The
21062306a36Sopenharmony_ciusage to be retrieved can be specified as above, or the user can
21162306a36Sopenharmony_cichoose to fill in the report_type field and specify the report_id as
21262306a36Sopenharmony_ciHID_REPORT_ID_UNKNOWN. In this case, the hiddev_usage_ref will be
21362306a36Sopenharmony_cifilled in with the report and field information associated with this
21462306a36Sopenharmony_ciusage if it is found.
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ciHIDIOCSUSAGE
21762306a36Sopenharmony_ci  - struct hiddev_usage_ref (write)
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ciSets the value of a usage in an output report.  The user fills in
22062306a36Sopenharmony_cithe hiddev_usage_ref structure as above, but additionally fills in
22162306a36Sopenharmony_cithe value field.
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ciHIDIOGCOLLECTIONINDEX
22462306a36Sopenharmony_ci  - struct hiddev_usage_ref (write)
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ciReturns the collection index associated with this usage.  This
22762306a36Sopenharmony_ciindicates where in the collection hierarchy this usage sits.
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ciHIDIOCGFLAG
23062306a36Sopenharmony_ci  - int (read)
23162306a36Sopenharmony_ciHIDIOCSFLAG
23262306a36Sopenharmony_ci  - int (write)
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ciThese operations respectively inspect and replace the mode flags
23562306a36Sopenharmony_cithat influence the read() call above.  The flags are as follows:
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci    HIDDEV_FLAG_UREF
23862306a36Sopenharmony_ci      - read() calls will now return
23962306a36Sopenharmony_ci        struct hiddev_usage_ref instead of struct hiddev_event.
24062306a36Sopenharmony_ci        This is a larger structure, but in situations where the
24162306a36Sopenharmony_ci        device has more than one usage in its reports with the
24262306a36Sopenharmony_ci        same usage code, this mode serves to resolve such
24362306a36Sopenharmony_ci        ambiguity.
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci    HIDDEV_FLAG_REPORT
24662306a36Sopenharmony_ci      - This flag can only be used in conjunction
24762306a36Sopenharmony_ci        with HIDDEV_FLAG_UREF.  With this flag set, when the device
24862306a36Sopenharmony_ci        sends a report, a struct hiddev_usage_ref will be returned
24962306a36Sopenharmony_ci        to read() filled in with the report_type and report_id, but
25062306a36Sopenharmony_ci        with field_index set to FIELD_INDEX_NONE.  This serves as
25162306a36Sopenharmony_ci        additional notification when the device has sent a report.
252