Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

HID: hidraw: Add additional hidraw input/output report ioctls.

Currently the hidraw module can only read and write feature HID reports on
demand, via dedicated ioctls. Input reports are read from the device through
the read() interface, while output reports are written through the write
interface().

This is insufficient; it is desirable in many situations to be able to read and
write input and output reports through the control interface to cover
additional scenarios:

- Reading an input report by its report ID, to get initial state
- Writing an input report, to set initial input state in the device
- Reading an output report by its report ID, to obtain current state
- Writing an output report by its report ID, out of band

This patch adds these missing ioctl requests to read and write the remaining
HID report types. Note that not all HID backends will neccesarily support this
(e.g. while the USB link layer supports setting Input reports, others may not).

Also included are documentation and example updates. The current hidraw
documentation states that feature reports read from the device does *not*
include the report ID, however this is not the case and the returned report
will have its report ID prepended by conforming HID devices, as the report data
sent from the device over the control endpoint must be indentical in format to
those sent over the regular transport.

Signed-off-by: Dean Camera <dean@fourwalledcubicle.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>

authored by

Dean Camera and committed by
Jiri Kosina
f43d3870 6a0eaf51

+73 -4
+43 -2
Documentation/hid/hidraw.rst
··· 123 123 This ioctl will request a feature report from the device using the control 124 124 endpoint. The first byte of the supplied buffer should be set to the report 125 125 number of the requested report. For devices which do not use numbered 126 - reports, set the first byte to 0. The report will be returned starting at 127 - the first byte of the buffer (ie: the report number is not returned). 126 + reports, set the first byte to 0. The returned report buffer will contain the 127 + report number in the first byte, followed by the report data read from the 128 + device. For devices which do not use numbered reports, the report data will 129 + begin at the first byte of the returned buffer. 130 + 131 + HIDIOCSINPUT(len): 132 + Send an Input Report 133 + 134 + This ioctl will send an input report to the device, using the control endpoint. 135 + In most cases, setting an input HID report on a device is meaningless and has 136 + no effect, but some devices may choose to use this to set or reset an initial 137 + state of a report. The format of the buffer issued with this report is identical 138 + to that of HIDIOCSFEATURE. 139 + 140 + HIDIOCGINPUT(len): 141 + Get an Input Report 142 + 143 + This ioctl will request an input report from the device using the control 144 + endpoint. This is slower on most devices where a dedicated In endpoint exists 145 + for regular input reports, but allows the host to request the value of a 146 + specific report number. Typically, this is used to request the initial states of 147 + an input report of a device, before an application listens for normal reports via 148 + the regular device read() interface. The format of the buffer issued with this report 149 + is identical to that of HIDIOCGFEATURE. 150 + 151 + HIDIOCSOUTPUT(len): 152 + Send an Output Report 153 + 154 + This ioctl will send an output report to the device, using the control endpoint. 155 + This is slower on most devices where a dedicated Out endpoint exists for regular 156 + output reports, but is added for completeness. Typically, this is used to set 157 + the initial states of an output report of a device, before an application sends 158 + updates via the regular device write() interface. The format of the buffer issued 159 + with this report is identical to that of HIDIOCSFEATURE. 160 + 161 + HIDIOCGOUTPUT(len): 162 + Get an Output Report 163 + 164 + This ioctl will request an output report from the device using the control 165 + endpoint. Typically, this is used to retrive the initial state of 166 + an output report of a device, before an application updates it as necessary either 167 + via a HIDIOCSOUTPUT request, or the regular device write() interface. The format 168 + of the buffer issued with this report is identical to that of HIDIOCGFEATURE. 128 169 129 170 Example 130 171 -------
+23 -1
drivers/hid/hidraw.c
··· 170 170 /* 171 171 * This function performs a Get_Report transfer over the control endpoint 172 172 * per section 7.2.1 of the HID specification, version 1.1. The first byte 173 - * of buffer is the report number to request, or 0x0 if the defice does not 173 + * of buffer is the report number to request, or 0x0 if the device does not 174 174 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT 175 175 * or HID_INPUT_REPORT. 176 176 */ ··· 425 425 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) { 426 426 int len = _IOC_SIZE(cmd); 427 427 ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT); 428 + break; 429 + } 430 + 431 + if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSINPUT(0))) { 432 + int len = _IOC_SIZE(cmd); 433 + ret = hidraw_send_report(file, user_arg, len, HID_INPUT_REPORT); 434 + break; 435 + } 436 + if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGINPUT(0))) { 437 + int len = _IOC_SIZE(cmd); 438 + ret = hidraw_get_report(file, user_arg, len, HID_INPUT_REPORT); 439 + break; 440 + } 441 + 442 + if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSOUTPUT(0))) { 443 + int len = _IOC_SIZE(cmd); 444 + ret = hidraw_send_report(file, user_arg, len, HID_OUTPUT_REPORT); 445 + break; 446 + } 447 + if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGOUTPUT(0))) { 448 + int len = _IOC_SIZE(cmd); 449 + ret = hidraw_get_report(file, user_arg, len, HID_OUTPUT_REPORT); 428 450 break; 429 451 } 430 452
+6
include/uapi/linux/hidraw.h
··· 40 40 #define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len) 41 41 #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len) 42 42 #define HIDIOCGRAWUNIQ(len) _IOC(_IOC_READ, 'H', 0x08, len) 43 + /* The first byte of SINPUT and GINPUT is the report number */ 44 + #define HIDIOCSINPUT(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x09, len) 45 + #define HIDIOCGINPUT(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x0A, len) 46 + /* The first byte of SOUTPUT and GOUTPUT is the report number */ 47 + #define HIDIOCSOUTPUT(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x0B, len) 48 + #define HIDIOCGOUTPUT(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x0C, len) 43 49 44 50 #define HIDRAW_FIRST_MINOR 0 45 51 #define HIDRAW_MAX_DEVICES 64
+1 -1
samples/hidraw/hid-example.c
··· 128 128 perror("HIDIOCGFEATURE"); 129 129 } else { 130 130 printf("ioctl HIDIOCGFEATURE returned: %d\n", res); 131 - printf("Report data (not containing the report number):\n\t"); 131 + printf("Report data:\n\t"); 132 132 for (i = 0; i < res; i++) 133 133 printf("%hhx ", buf[i]); 134 134 puts("\n");