Linux USB HID gadget driverВ¶
IntroductionВ¶
The HID Gadget driver provides emulation of USB Human Interface Devices (HID). The basic HID handling is done in the kernel, and HID reports can be sent/received through I/O on the /dev/hidgX character devices.
For more details about HID, see the developer page on https://www.usb.org/developers/hidpage/
ConfigurationВ¶
g_hid is a platform driver, so to use it you need to add struct platform_device(s) to your platform code defining the HID function descriptors you want to use — E.G. something like:
You can add as many HID functions as you want, only limited by the amount of interrupt endpoints your gadget driver supports.
Configuration with configfsВ¶
Instead of adding fake platform devices and drivers in order to pass some data to the kernel, if HID is a part of a gadget composed with configfs the hidg_func_descriptor.report_desc is passed to the kernel by writing the appropriate stream of bytes to a configfs attribute.
Send and receive HID reportsВ¶
HID reports can be sent/received using read/write on the /dev/hidgX character devices. See below for an example program to do this.
hid_gadget_test is a small interactive program to test the HID gadget driver. To use, point it at a hidg device and set the device type (keyboard / mouse / joystick) — E.G.:
You are now in the prompt of hid_gadget_test. You can type any combination of options and values. Available options and values are listed at program start. In keyboard mode you can send up to six values.
For example type: g i s t r –left-shift
Hit return and the corresponding report will be sent by the HID gadget.
Another interesting example is the caps lock test. Type –caps-lock and hit return. A report is then sent by the gadget and you should receive the host answer, corresponding to the caps lock LED status:
With this command:
You can test the mouse emulation. Values are two signed numbers.
Источник
Linux usb hid terminal
HIDAPI library for Windows, Linux, FreeBSD and macOS
CI instance | Status |
---|---|
Linux/macOS/Windows master | |
Windows master | |
Linux/BSD, last build (branch/PR) | Linux usb hid terminalThere are two options for using a USB mouse or a USB keyboard — the standalone Boot Protocol (HIDBP) way and the full featured HID driver way. The Boot Protocol way is generally inferior, and this document describes the full featured way. The Boot Protocol way may be appropriate for embedded systems and other systems with resource constraints and no real need for the full keyboard and mouse capabilities. In the kernel configuration stage, you need to turn on USB Human Interface Device (HID) support in the USB support and Mouse Support in the Input core support . You don’t need to worry about the screen resolution entries for a normal mouse — these are for mouse-like devices such as a graphics tablet. Do not turn on USB HIDBP Mouse support . Perform the normal kernel rebuild and installation steps. If you are installing as modules, you need to load the input.o , hid.o and mousedev.o modules. Plug in a USB mouse and check that your mouse has been correctly sensed by the kernel. If you don’t have a kernel message, look for the changes to /proc/bus/usb/devices . Since USB supports multiple identical devices, you can have multiple mice plugged in. You can get each mouse seperately, or you can get them all mixed together. You almost always want the mixed version, and that is what will be used in this example. You need to set up a device node entry for the mixed mice. It is customary to create the entries for this device in the /dev/input/ directory. Use the following commands: You also need to add an entry to each applicable ServerLayout Section. These are normally at the end of the configuration file. If you only have a USB mouse (or USB mice), then replace the line with the «CorePointer» entry with the following line: |