xref: /OK3568_Linux_fs/kernel/Documentation/hid/hiddev.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun================================================
2*4882a593SmuzhiyunCare and feeding of your Human Interface Devices
3*4882a593Smuzhiyun================================================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunIntroduction
6*4882a593Smuzhiyun============
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunIn addition to the normal input type HID devices, USB also uses the
9*4882a593Smuzhiyunhuman interface device protocols for things that are not really human
10*4882a593Smuzhiyuninterfaces, but have similar sorts of communication needs. The two big
11*4882a593Smuzhiyunexamples for this are power devices (especially uninterruptable power
12*4882a593Smuzhiyunsupplies) and monitor control on higher end monitors.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunTo support these disparate requirements, the Linux USB system provides
15*4882a593SmuzhiyunHID events to two separate interfaces:
16*4882a593Smuzhiyun* the input subsystem, which converts HID events into normal input
17*4882a593Smuzhiyundevice interfaces (such as keyboard, mouse and joystick) and a
18*4882a593Smuzhiyunnormalised event interface - see Documentation/input/input.rst
19*4882a593Smuzhiyun* the hiddev interface, which provides fairly raw HID events
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunThe data flow for a HID event produced by a device is something like
22*4882a593Smuzhiyunthe following::
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun usb.c ---> hid-core.c  ----> hid-input.c ----> [keyboard/mouse/joystick/event]
25*4882a593Smuzhiyun                         |
26*4882a593Smuzhiyun                         |
27*4882a593Smuzhiyun                          --> hiddev.c ----> POWER / MONITOR CONTROL
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunIn addition, other subsystems (apart from USB) can potentially feed
30*4882a593Smuzhiyunevents into the input subsystem, but these have no effect on the hid
31*4882a593Smuzhiyundevice interface.
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunUsing the HID Device Interface
34*4882a593Smuzhiyun==============================
35*4882a593Smuzhiyun
36*4882a593SmuzhiyunThe hiddev interface is a char interface using the normal USB major,
37*4882a593Smuzhiyunwith the minor numbers starting at 96 and finishing at 111. Therefore,
38*4882a593Smuzhiyunyou need the following commands::
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun	mknod /dev/usb/hiddev0 c 180 96
41*4882a593Smuzhiyun	mknod /dev/usb/hiddev1 c 180 97
42*4882a593Smuzhiyun	mknod /dev/usb/hiddev2 c 180 98
43*4882a593Smuzhiyun	mknod /dev/usb/hiddev3 c 180 99
44*4882a593Smuzhiyun	mknod /dev/usb/hiddev4 c 180 100
45*4882a593Smuzhiyun	mknod /dev/usb/hiddev5 c 180 101
46*4882a593Smuzhiyun	mknod /dev/usb/hiddev6 c 180 102
47*4882a593Smuzhiyun	mknod /dev/usb/hiddev7 c 180 103
48*4882a593Smuzhiyun	mknod /dev/usb/hiddev8 c 180 104
49*4882a593Smuzhiyun	mknod /dev/usb/hiddev9 c 180 105
50*4882a593Smuzhiyun	mknod /dev/usb/hiddev10 c 180 106
51*4882a593Smuzhiyun	mknod /dev/usb/hiddev11 c 180 107
52*4882a593Smuzhiyun	mknod /dev/usb/hiddev12 c 180 108
53*4882a593Smuzhiyun	mknod /dev/usb/hiddev13 c 180 109
54*4882a593Smuzhiyun	mknod /dev/usb/hiddev14 c 180 110
55*4882a593Smuzhiyun	mknod /dev/usb/hiddev15 c 180 111
56*4882a593Smuzhiyun
57*4882a593SmuzhiyunSo you point your hiddev compliant user-space program at the correct
58*4882a593Smuzhiyuninterface for your device, and it all just works.
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunAssuming that you have a hiddev compliant user-space program, of
61*4882a593Smuzhiyuncourse. If you need to write one, read on.
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun
64*4882a593SmuzhiyunThe HIDDEV API
65*4882a593Smuzhiyun==============
66*4882a593Smuzhiyun
67*4882a593SmuzhiyunThis description should be read in conjunction with the HID
68*4882a593Smuzhiyunspecification, freely available from https://www.usb.org, and
69*4882a593Smuzhiyunconveniently linked of http://www.linux-usb.org.
70*4882a593Smuzhiyun
71*4882a593SmuzhiyunThe hiddev API uses a read() interface, and a set of ioctl() calls.
72*4882a593Smuzhiyun
73*4882a593SmuzhiyunHID devices exchange data with the host computer using data
74*4882a593Smuzhiyunbundles called "reports".  Each report is divided into "fields",
75*4882a593Smuzhiyuneach of which can have one or more "usages".  In the hid-core,
76*4882a593Smuzhiyuneach one of these usages has a single signed 32 bit value.
77*4882a593Smuzhiyun
78*4882a593Smuzhiyunread():
79*4882a593Smuzhiyun-------
80*4882a593Smuzhiyun
81*4882a593SmuzhiyunThis is the event interface.  When the HID device's state changes,
82*4882a593Smuzhiyunit performs an interrupt transfer containing a report which contains
83*4882a593Smuzhiyunthe changed value.  The hid-core.c module parses the report, and
84*4882a593Smuzhiyunreturns to hiddev.c the individual usages that have changed within
85*4882a593Smuzhiyunthe report.  In its basic mode, the hiddev will make these individual
86*4882a593Smuzhiyunusage changes available to the reader using a struct hiddev_event::
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun       struct hiddev_event {
89*4882a593Smuzhiyun           unsigned hid;
90*4882a593Smuzhiyun           signed int value;
91*4882a593Smuzhiyun       };
92*4882a593Smuzhiyun
93*4882a593Smuzhiyuncontaining the HID usage identifier for the status that changed, and
94*4882a593Smuzhiyunthe value that it was changed to. Note that the structure is defined
95*4882a593Smuzhiyunwithin <linux/hiddev.h>, along with some other useful #defines and
96*4882a593Smuzhiyunstructures.  The HID usage identifier is a composite of the HID usage
97*4882a593Smuzhiyunpage shifted to the 16 high order bits ORed with the usage code.  The
98*4882a593Smuzhiyunbehavior of the read() function can be modified using the HIDIOCSFLAG
99*4882a593Smuzhiyunioctl() described below.
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun
102*4882a593Smuzhiyunioctl():
103*4882a593Smuzhiyun--------
104*4882a593Smuzhiyun
105*4882a593SmuzhiyunThis is the control interface. There are a number of controls:
106*4882a593Smuzhiyun
107*4882a593SmuzhiyunHIDIOCGVERSION
108*4882a593Smuzhiyun  - int (read)
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun Gets the version code out of the hiddev driver.
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunHIDIOCAPPLICATION
113*4882a593Smuzhiyun  - (none)
114*4882a593Smuzhiyun
115*4882a593SmuzhiyunThis ioctl call returns the HID application usage associated with the
116*4882a593Smuzhiyunhid device. The third argument to ioctl() specifies which application
117*4882a593Smuzhiyunindex to get. This is useful when the device has more than one
118*4882a593Smuzhiyunapplication collection. If the index is invalid (greater or equal to
119*4882a593Smuzhiyunthe number of application collections this device has) the ioctl
120*4882a593Smuzhiyunreturns -1. You can find out beforehand how many application
121*4882a593Smuzhiyuncollections the device has from the num_applications field from the
122*4882a593Smuzhiyunhiddev_devinfo structure.
123*4882a593Smuzhiyun
124*4882a593SmuzhiyunHIDIOCGCOLLECTIONINFO
125*4882a593Smuzhiyun  - struct hiddev_collection_info (read/write)
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunThis returns a superset of the information above, providing not only
128*4882a593Smuzhiyunapplication collections, but all the collections the device has.  It
129*4882a593Smuzhiyunalso returns the level the collection lives in the hierarchy.
130*4882a593SmuzhiyunThe user passes in a hiddev_collection_info struct with the index
131*4882a593Smuzhiyunfield set to the index that should be returned.  The ioctl fills in
132*4882a593Smuzhiyunthe other fields.  If the index is larger than the last collection
133*4882a593Smuzhiyunindex, the ioctl returns -1 and sets errno to -EINVAL.
134*4882a593Smuzhiyun
135*4882a593SmuzhiyunHIDIOCGDEVINFO
136*4882a593Smuzhiyun  - struct hiddev_devinfo (read)
137*4882a593Smuzhiyun
138*4882a593SmuzhiyunGets a hiddev_devinfo structure which describes the device.
139*4882a593Smuzhiyun
140*4882a593SmuzhiyunHIDIOCGSTRING
141*4882a593Smuzhiyun  - struct hiddev_string_descriptor (read/write)
142*4882a593Smuzhiyun
143*4882a593SmuzhiyunGets a string descriptor from the device. The caller must fill in the
144*4882a593Smuzhiyun"index" field to indicate which descriptor should be returned.
145*4882a593Smuzhiyun
146*4882a593SmuzhiyunHIDIOCINITREPORT
147*4882a593Smuzhiyun  - (none)
148*4882a593Smuzhiyun
149*4882a593SmuzhiyunInstructs the kernel to retrieve all input and feature report values
150*4882a593Smuzhiyunfrom the device. At this point, all the usage structures will contain
151*4882a593Smuzhiyuncurrent values for the device, and will maintain it as the device
152*4882a593Smuzhiyunchanges.  Note that the use of this ioctl is unnecessary in general,
153*4882a593Smuzhiyunsince later kernels automatically initialize the reports from the
154*4882a593Smuzhiyundevice at attach time.
155*4882a593Smuzhiyun
156*4882a593SmuzhiyunHIDIOCGNAME
157*4882a593Smuzhiyun  - string (variable length)
158*4882a593Smuzhiyun
159*4882a593SmuzhiyunGets the device name
160*4882a593Smuzhiyun
161*4882a593SmuzhiyunHIDIOCGREPORT
162*4882a593Smuzhiyun  - struct hiddev_report_info (write)
163*4882a593Smuzhiyun
164*4882a593SmuzhiyunInstructs the kernel to get a feature or input report from the device,
165*4882a593Smuzhiyunin order to selectively update the usage structures (in contrast to
166*4882a593SmuzhiyunINITREPORT).
167*4882a593Smuzhiyun
168*4882a593SmuzhiyunHIDIOCSREPORT
169*4882a593Smuzhiyun  - struct hiddev_report_info (write)
170*4882a593Smuzhiyun
171*4882a593SmuzhiyunInstructs the kernel to send a report to the device. This report can
172*4882a593Smuzhiyunbe filled in by the user through HIDIOCSUSAGE calls (below) to fill in
173*4882a593Smuzhiyunindividual usage values in the report before sending the report in full
174*4882a593Smuzhiyunto the device.
175*4882a593Smuzhiyun
176*4882a593SmuzhiyunHIDIOCGREPORTINFO
177*4882a593Smuzhiyun  - struct hiddev_report_info (read/write)
178*4882a593Smuzhiyun
179*4882a593SmuzhiyunFills in a hiddev_report_info structure for the user. The report is
180*4882a593Smuzhiyunlooked up by type (input, output or feature) and id, so these fields
181*4882a593Smuzhiyunmust be filled in by the user. The ID can be absolute -- the actual
182*4882a593Smuzhiyunreport id as reported by the device -- or relative --
183*4882a593SmuzhiyunHID_REPORT_ID_FIRST for the first report, and (HID_REPORT_ID_NEXT |
184*4882a593Smuzhiyunreport_id) for the next report after report_id. Without a-priori
185*4882a593Smuzhiyuninformation about report ids, the right way to use this ioctl is to
186*4882a593Smuzhiyunuse the relative IDs above to enumerate the valid IDs. The ioctl
187*4882a593Smuzhiyunreturns non-zero when there is no more next ID. The real report ID is
188*4882a593Smuzhiyunfilled into the returned hiddev_report_info structure.
189*4882a593Smuzhiyun
190*4882a593SmuzhiyunHIDIOCGFIELDINFO
191*4882a593Smuzhiyun  - struct hiddev_field_info (read/write)
192*4882a593Smuzhiyun
193*4882a593SmuzhiyunReturns the field information associated with a report in a
194*4882a593Smuzhiyunhiddev_field_info structure. The user must fill in report_id and
195*4882a593Smuzhiyunreport_type in this structure, as above. The field_index should also
196*4882a593Smuzhiyunbe filled in, which should be a number from 0 and maxfield-1, as
197*4882a593Smuzhiyunreturned from a previous HIDIOCGREPORTINFO call.
198*4882a593Smuzhiyun
199*4882a593SmuzhiyunHIDIOCGUCODE
200*4882a593Smuzhiyun  - struct hiddev_usage_ref (read/write)
201*4882a593Smuzhiyun
202*4882a593SmuzhiyunReturns the usage_code in a hiddev_usage_ref structure, given that
203*4882a593Smuzhiyungiven its report type, report id, field index, and index within the
204*4882a593Smuzhiyunfield have already been filled into the structure.
205*4882a593Smuzhiyun
206*4882a593SmuzhiyunHIDIOCGUSAGE
207*4882a593Smuzhiyun  - struct hiddev_usage_ref (read/write)
208*4882a593Smuzhiyun
209*4882a593SmuzhiyunReturns the value of a usage in a hiddev_usage_ref structure. The
210*4882a593Smuzhiyunusage to be retrieved can be specified as above, or the user can
211*4882a593Smuzhiyunchoose to fill in the report_type field and specify the report_id as
212*4882a593SmuzhiyunHID_REPORT_ID_UNKNOWN. In this case, the hiddev_usage_ref will be
213*4882a593Smuzhiyunfilled in with the report and field information associated with this
214*4882a593Smuzhiyunusage if it is found.
215*4882a593Smuzhiyun
216*4882a593SmuzhiyunHIDIOCSUSAGE
217*4882a593Smuzhiyun  - struct hiddev_usage_ref (write)
218*4882a593Smuzhiyun
219*4882a593SmuzhiyunSets the value of a usage in an output report.  The user fills in
220*4882a593Smuzhiyunthe hiddev_usage_ref structure as above, but additionally fills in
221*4882a593Smuzhiyunthe value field.
222*4882a593Smuzhiyun
223*4882a593SmuzhiyunHIDIOGCOLLECTIONINDEX
224*4882a593Smuzhiyun  - struct hiddev_usage_ref (write)
225*4882a593Smuzhiyun
226*4882a593SmuzhiyunReturns the collection index associated with this usage.  This
227*4882a593Smuzhiyunindicates where in the collection hierarchy this usage sits.
228*4882a593Smuzhiyun
229*4882a593SmuzhiyunHIDIOCGFLAG
230*4882a593Smuzhiyun  - int (read)
231*4882a593SmuzhiyunHIDIOCSFLAG
232*4882a593Smuzhiyun  - int (write)
233*4882a593Smuzhiyun
234*4882a593SmuzhiyunThese operations respectively inspect and replace the mode flags
235*4882a593Smuzhiyunthat influence the read() call above.  The flags are as follows:
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun    HIDDEV_FLAG_UREF
238*4882a593Smuzhiyun      - read() calls will now return
239*4882a593Smuzhiyun        struct hiddev_usage_ref instead of struct hiddev_event.
240*4882a593Smuzhiyun        This is a larger structure, but in situations where the
241*4882a593Smuzhiyun        device has more than one usage in its reports with the
242*4882a593Smuzhiyun        same usage code, this mode serves to resolve such
243*4882a593Smuzhiyun        ambiguity.
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun    HIDDEV_FLAG_REPORT
246*4882a593Smuzhiyun      - This flag can only be used in conjunction
247*4882a593Smuzhiyun        with HIDDEV_FLAG_UREF.  With this flag set, when the device
248*4882a593Smuzhiyun        sends a report, a struct hiddev_usage_ref will be returned
249*4882a593Smuzhiyun        to read() filled in with the report_type and report_id, but
250*4882a593Smuzhiyun        with field_index set to FIELD_INDEX_NONE.  This serves as
251*4882a593Smuzhiyun        additional notification when the device has sent a report.
252