You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
hid_get_input_report returns one byte more than hid_read, also how to deal with hid_get_feature_report #229
The code of hid_get_input_report contains:
/* bytes_returned does not include the first byte which contains the
report ID. The data buffer actually contains one more byte than
bytes_returned. */
bytes_returned++;
/** @brief Read an Input report from a HID device.
Input reports are returned
to the host through the INTERRUPT IN endpoint. The first byte will
contain the Report number if the device uses numbered reports.
Addendum
That is at least a reasonable interpretation of the documentation for hid_get_input_report. However, the same text appears in hid_get_feature_report which, in turn, seems to have conflicting implementations:
the libusb implementation appears to follow the interpretation above;
but the hidraw implementation simply wraps HIDIOCGFEATURE, which is documented to have a different behavior: "For devices which do not use numbered reports, the report data will begin at the first byte of the returned buffer."
changed the title [-]hid_get_input_report returns one byte more than hid_read [/-][+]hid_get_input_report returns one byte more than hid_read, also how to deal with hid_get_feature_report[/+]on Jun 13, 2021
List: libusb-devel
Subject: Re: [Libusb-devel] Returned HID feature report size on windows
From: Alan Ott <alan () signal11 ! us>
Date: 2010-09-06 15:35:11
Message-ID: 4C850A2F.5090509 () signal11 ! us
[Download RAW message or body]
On 09/04/2010 10:18 PM, Xiaofan Chen wrote:
ReadFile() is generic and should be able to be used for
input/output/feature reports.
ReadFile() will only give input reports which came back on the
Interrupt-IN endpoint. To get a feature report and also its length, you
have to use the DeviceIoControl() call instead of HidD_GetFeature(). The
problem is, the definitions of the IOCTLs are only in the DDK, and
they're not even in the same place as the hid.dll headers. Further,
there are two sets of IOCTLs, one for kernel-space, and one for
user-space, which do many of the same things.
If I understand your results correctly, hid_get_input_report actually works fine, but hid_get_feature_report doesn't. This makes #232 invalid.
UPD: comments below.
@JoergAtGithub something I've failed to ask you earlier:
Does you device uses numbered reports?
So based on my test result hid_read_timeout() is correct for input report. hid_get_feature_report() is a bit wrong for feature report in terms of the bytes reading. hidapitester does not test the correctness of hid_get_input_report().
Does you device/FW support sending syncronous input reports?
I know that with the devices I use, if a report can be hid_read (basically the device sends me interruptions on a specific events), I cannot get_input_report it - only those that are specifically designed/implemented that way.
And I got the same results with feature report that one extra byte is read using hidpytoy and hidapitester, so there is an issue with Windows hid_get_feature_report. I will not be surprised that hid_get_input_report has the same issue.
FYI on the device FW and libusb test program. For this paticular device FW, libusb HID backend seems to work. But there are other cases it does not work. You can see this report ID is causing lots of problem for Windows HID related codes, due to the unclear documentation from Microsoft. libusb/libusb#902
I need to have a minor fix to build under MSYS2 MinGW-w64.
$ git diff
diff --git a/lvr_genhid_mod/libusb1_lvrhid8.c b/lvr_genhid_mod/libusb1_lvrhid8.c
index 6616c4b..ddd1372 100644
--- a/lvr_genhid_mod/libusb1_lvrhid8.c
+++ b/lvr_genhid_mod/libusb1_lvrhid8.c
@@ -25,7 +25,7 @@
const static int PACKET_CTRL_LEN=3;
const static int PACKET_INT_LEN=3;
-const static int INTERFACE=0;
+const static int MYINTERFACE=0;
const static int ENDPOINT_INT_IN=0x81; /* endpoint 0x81 address for IN */
const static int ENDPOINT_INT_OUT=0x01; /* endpoint 1 address for OUT */
const static int TIMEOUT=5000; /* timeout in ms */
@@ -166,7 +166,7 @@ int main(void)
goto out;
}
printf("Successfully set usb configuration 1\n");
- r = libusb_claim_interface(devh, 0);
+ r = libusb_claim_interface(devh, MYINTERFACE);
if (r < 0) {
fprintf(stderr, "libusb_claim_interface error %d\n", r);
goto out;
$ cd lvr_genhid_mod/
$ ./libusb1_lvrhid8.exe
Successfully find the LVR Generic HID device
Successfully set usb configuration 1
Successfully claimed interface
Testing control transfer using loop back test of feature report
03, 04; 21, 21; 22, 22; 23, 00;
Testing control transfer using loop back test of input/output report
02, 01; 31, 31; 32, 32; 33, 00;
Testing interrupt transfer using loop back test of input/output report
02, 01; 41, 41; 42, 42; 43, 00;
I guess I've trusted (and didn't check so far) the original commit too much.
My anlaysis below and I believe hidapi current codes are wrong. I have asked in the libusb-devel mailing list to confirm.
The following article is from the Windows Hardware Developer site.
https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/obtaining-hid-reports
This topic discusses the obtaining of HID input reports or HID feature reports,
by user-mode applications using ReadFile or the HidD_GetXxx routines.
However, an application should only use the HidD_GetXxx routines to obtain the current state of a device.
If an application attempts to use HidD_GetInputReport to continuously obtain input reports, the reports
can be lost. In addition, some devices might not support HidD_GetInputReport, and will become
unresponsive if this routine is used.
Using IOCTL_HID_GET_Xxx Requests
A driver can use the following I/O requests to obtain the most current input and feature reports from a
HID collection:
IOCTL_HID_GET_INPUT_REPORT Returns an input report from a HID collection (Windows XP and later versions).
IOCTL_HID_GET_FEATURE Returns a feature report from a HID collection.
So you can see that HIDAPI is using ReadFile for the fetching of HID reports continuously with the
hid_read API. And then it does not use HidD_GetXxx routines to obtain the most current input report
and feature report, rather it uses IOCTLs to get the input report and feature report.
Historical reasons why Alan Ott chose to use DeviceIoControl() call and not HidD_GetXxx. #229
So it seems to me that the Microsoft documentation may be a bit confusing to say the driver can use
the two IOCTLs. Usually driver means kernel space in this context even though there are user mode
drivers as well.
https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/hidclass/ni-hidclass-ioctl_hid_get_input_report
https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/hidclass/ni-hidclass-ioctl_hid_get_feature
Output buffer
The Irp->MdlAddress member points to the requester-allocated output buffer that the HID class driver
uses to return the input report. The first byte of the buffer, which the requester uses to input a report ID
or zero, is unchanged. The input report -- excluding its report ID, if report IDs are used -- is returned at
((PUCHAR)Irp->MdlAddress + 1).
Output buffer length
The size of the input report.
diff --git a/windows/hid.c b/windows/hid.c
index f0013a5..74a05bc 100644
--- a/windows/hid.c
+++ b/windows/hid.c
@@ -876,11 +876,6 @@ int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned
return -1;
}
- /* bytes_returned does not include the first byte which contains the
- report ID. The data buffer actually contains one more byte than
- bytes_returned. */
- bytes_returned++;
-
return bytes_returned;
#endif
}
@@ -925,11 +920,6 @@ int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned c
return -1;
}
- /* bytes_returned does not include the first byte which contains the
- report ID. The data buffer actually contains one more byte than
- bytes_returned. */
- bytes_returned++;
-
return bytes_returned;
#endif
}
With the above patch, the results are the same as macOS and Linux. So unfortunately my device is not suitable to test hid_get_input_report(). But at least it confirms the issue on hid_get_feature_report.
@JoergAtGithub Did you test the device without a report ID? If that is tested, then I think #232 is good to go.
No, I tested this only with devices with Report ID. I have to check for a device without Report ID, where hid_get_input_report works. Not sure if I have such a device.
Answers from Tim Roberts (a top Windows driver expert in the wordd and USB expert) in libusb-devel mailing list.
On Wed, Jun 16, 2021 at 12:21 AM Tim Roberts wrote:
Xiaofan Chen wrote:
So you can see that HIDAPI is using ReadFile for the fetching of HID reports
continuously with the hid_read API. And then it does not use HidD_GetXxx routines
to obtain the most current input report and feature report, rather it
uses IOCTLs to get the input report and feature report.
Historical reasons why Alan Ott chose to use DeviceIoControl() call and not HidD_GetXxx. #229
The ioctls are equivalent to the HidD calls. The HidD calls do almost
nothing other than format and send a DeviceIoControl request.
There are essentially 8 ways to talk to a HID device.
GetFeature/SetFeature read and write feature reports. These get an
immediate response, and include a report ID byte (if the descriptor has
multiple reports).
SetOutputReport/WriteReport/WriteFile all write output reports and do
the same thing. These all happen immediately and include a report ID byte.
GetInputReport gets an input report immediately, and includes a report
ID byte in both directions.
ReadReport/ReadFile are the strange ones. These are long term calls.
This says "send me the next report that changes, whatever it may be."
It will get queued up for later. When it returns, the buffer will
include a report ID, but you don't ask for one to begin with.
So it seems to me that the Microsoft documentation may be a bit confusing to
say the driver can use the two IOCTLs. Usually driver means kernel space in
this context even though there are user mode drivers as well.
Well, sort of. A HID driver lives in kernel space. It is NOW possible
to write a UMDF HID driver, but only because Microsoft created a
kernel-mode shim driver that shuffles the requests out to UMDF. The HID
subsystem is really a kernel component.
This code is wrong. The "bytes_returned" value from a HID driver
definitely does include the report ID byte. This happens to be fresh in
my mind because I just finished writing a fake HID driver that had to
deal with this. I wonder if they did their testing on a faulty driver?
Activity
JoergAtGithub commentedon Jan 7, 2021
The code of hid_get_input_report contains:
/* bytes_returned does not include the first byte which contains the
report ID. The data buffer actually contains one more byte than
bytes_returned. */
bytes_returned++;
I compared this with the implementation in Google Chrome (WebHID backend), and they read the data using the same API, but do not increment the byte count:
https://chromium.googlesource.com/chromium/src/+/cebcd836b83e1d48db1000b2a5878aa90bc7e88b/services/device/hid/hid_connection_win.cc
hid_get_input_report: Correct number of bytes_returned
Removed workaround for libusb/hidapi#229
jonasmalacofilho commentedon Mar 31, 2021
(Arriving here from a reference in another issue...)
That is the documented behavior of
hid_get_input_reportwhen report IDs are not used, which could be the case here, since there is a single report.hidapi/hidapi/hidapi.h
Lines 370 to 376 in 6a01f3b
hid_read, indeed, follows different semantics.hidapi/hidapi/hidapi.h
Lines 271 to 275 in 6a01f3b
Addendum
That is at least a reasonable interpretation of the documentation for
hid_get_input_report. However, the same text appears inhid_get_feature_reportwhich, in turn, seems to have conflicting implementations:HIDIOCGFEATURE, which is documented to have a different behavior: "For devices which do not use numbered reports, the report data will begin at the first byte of the returned buffer."mcuee commentedon Jun 13, 2021
http://janaxelson.com/hidpage.htm
Jan Axelson's page may be of some interests. Including FW codes to be used for test device.
[-]hid_get_input_report returns one byte more than hid_read [/-][+]hid_get_input_report returns one byte more than hid_read, also how to deal with hid_get_feature_report[/+]mcuee commentedon Jun 13, 2021
Historical info about feature report.
https://marc.info/?l=libusb-devel&m=128378733122118&w=2
List: libusb-devel
Subject: Re: [Libusb-devel] Returned HID feature report size on windows
From: Alan Ott <alan () signal11 ! us>
Date: 2010-09-06 15:35:11
Message-ID: 4C850A2F.5090509 () signal11 ! us
[Download RAW message or body]
On 09/04/2010 10:18 PM, Xiaofan Chen wrote:
...
ReadFile() will only give input reports which came back on the
Interrupt-IN endpoint. To get a feature report and also its length, you
have to use the DeviceIoControl() call instead of HidD_GetFeature(). The
problem is, the definitions of the IOCTLs are only in the DDK, and
they're not even in the same place as the hid.dll headers. Further,
there are two sets of IOCTLs, one for kernel-space, and one for
user-space, which do many of the same things.
I was eventually able to figure it out, and if you look here:
http://github.com/signal11/hidapi/blob/master/windows/hid.cpp
at the hid_get_feature_report() function, you'll see what works.
... skipped ...
mcuee commentedon Jun 13, 2021
Ref from Microsoft, which is said not to be so clear.
https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/obtaining-hid-reports
mcuee commentedon Jun 14, 2021
Please check my test result also.
#232
Test FW and libusb code
https://github.com/mcuee/picusb/tree/master/lvr_genhid_mod
hidapitester results: it seems input report is correct but feature report has an issue as it reads back 4 bytes.
mcuee commentedon Jun 14, 2021
I just purposefully send one more byte and here is the test result. No issues with the input report and feature report.
Youw commentedon Jun 14, 2021
If I understand your results correctly,hid_get_input_reportactually works fine, buthid_get_feature_reportdoesn't.This makes #232 invalid.UPD: comments below.
@JoergAtGithub something I've failed to ask you earlier:
Does you device uses numbered reports?
mcuee commentedon Jun 14, 2021
I am using hidapitester.
https://github.com/todbot/hidapitester
Report Descriptor of the device:
mcuee commentedon Jun 14, 2021
Form the code of hidapitester.c, it is using hid_read_timeout() for input report and hid_get_feature_report() for feature report read.
https://github.com/todbot/hidapitester/blob/master/hidapitester.c
So based on my test result hid_read_timeout() is correct for input report. hid_get_feature_report() is a bit wrong for feature report in terms of the bytes reading. hidapitester does not test the correctness of hid_get_input_report().
Youw commentedon Jun 14, 2021
That's unfortunate. Do you have other way to test it? (maybe modify
hidapitester?)7 remaining items
mcuee commentedon Jun 14, 2021
Looks like my quick fix is not correct. I got the same results under Windows.
Youw commentedon Jun 14, 2021
Does you device/FW support sending syncronous input reports?
I know that with the devices I use, if a report can be
hid_read(basically the device sends me interruptions on a specific events), I cannotget_input_reportit - only those that are specifically designed/implemented that way.mcuee commentedon Jun 14, 2021
The device supports reading input report using either interrupt transfer or control transfer. I have tested with libusb Windows HID backend.
I got the same results using hidpytoy, it also use hid_read for input reports.
https://github.com/todbot/hidpytoy/blob/master/src/main/python/main.py
And I got the same results with feature report that one extra byte is read using hidpytoy and hidapitester, so there is an issue with Windows hid_get_feature_report. I will not be surprised that hid_get_input_report has the same issue.
Youw commentedon Jun 14, 2021
Yeah, no surprise 1, 2 lol.
I guess I've trusted (and didn't check so far) the original commit too much.
mcuee commentedon Jun 14, 2021
FYI on the device FW and libusb test program. For this paticular device FW, libusb HID backend seems to work. But there are other cases it does not work. You can see this report ID is causing lots of problem for Windows HID related codes, due to the unclear documentation from Microsoft.
libusb/libusb#902
My picusb repo:
https://github.com/mcuee/picusb/tree/master/lvr_genhid_mod
I need to have a minor fix to build under MSYS2 MinGW-w64.
mcuee commentedon Jun 15, 2021
My anlaysis below and I believe hidapi current codes are wrong. I have asked in the libusb-devel mailing list to confirm.
The following article is from the Windows Hardware Developer site.
So you can see that HIDAPI is using ReadFile for the fetching of HID reports continuously with the
hid_read API. And then it does not use HidD_GetXxx routines to obtain the most current input report
and feature report, rather it uses IOCTLs to get the input report and feature report.
https://github.com/libusb/hidapi/blob/master/windows/hid.c#L56
#define IOCTL_HID_GET_FEATURE HID_OUT_CTL_CODE(100)
#define IOCTL_HID_GET_INPUT_REPORT HID_OUT_CTL_CODE(104)
Historical reasons why Alan Ott chose to use DeviceIoControl() call and not HidD_GetXxx.
#229
So it seems to me that the Microsoft documentation may be a bit confusing to say the driver can use
the two IOCTLs. Usually driver means kernel space in this context even though there are user mode
drivers as well.
Now the question is the return data of the two IOCTLs (output buffer). From what I read, I believe
HIDAPI codes are wrong in both cases, as Microsoft documentation says that the first byte of
the buffer is unchanged (report ID or 0 if no report ID).
https://github.com/libusb/hidapi/blob/master/windows/hid.c#L933
https://github.com/libusb/hidapi/blob/master/windows/hid.c#L884
mcuee commentedon Jun 15, 2021
More proper fix for hidapitester:
And proposed fix to hidapi (including #232)
mcuee commentedon Jun 15, 2021
With the above patch, the results are the same as macOS and Linux. So unfortunately my device is not suitable to test hid_get_input_report(). But at least it confirms the issue on hid_get_feature_report.
mcuee commentedon Jun 15, 2021
@JoergAtGithub Did you test the device without a report ID? If that is tested, then I think #232 is good to go.
JoergAtGithub commentedon Jun 15, 2021
No, I tested this only with devices with Report ID. I have to check for a device without Report ID, where hid_get_input_report works. Not sure if I have such a device.
mcuee commentedon Jun 16, 2021
Answers from Tim Roberts (a top Windows driver expert in the wordd and USB expert) in libusb-devel mailing list.
On Wed, Jun 16, 2021 at 12:21 AM Tim Roberts wrote:
mcuee commentedon Jun 16, 2021
@JoergAtGithub So #232 is good to go. Is it possible for you to create another pull request for the feature report? Thanks.
win: hid_get_input_report: Correct number of bytes_returned (#232)