Write something to linux hid device?

liunx picture liunx · Jul 27, 2011 · Viewed 9.9k times · Source

I am learning linux hid driver programming, i know how to read a message from hid device
but, i am puzzled how to write something to the device ? such as usb hid keyboard, i can use xset or some other program to control the leds of the keyboard, how to archive that? Any tips please!
thanks advance.

Answer

rodrigo picture rodrigo · Jul 28, 2011

USB HID devices are mostly input devices, so they don't usually provide an OUT endpoint (they are allowed by the HID specification but I've never seen one). If an OUT endpoint is not provided, then output reports are sent through the control endpoint (EP0). The URB should be something like this:

bmRequestType = 0x21     (To class interface)
bRequest = 0x09          (SET_REPORT)
wValue = 0x02 <report_code>
wIndex = <interface>     (Usually 0x0001)
wLength = <Data length>
Data = <report_code> <data>...

Naturally, there are functions that do just that. From kernel, you may look at hiddev_lookup_report/usbhid_submit_report. From userland, if you use /dev/usb/hiddev? you may try the HIDIOCSREPORT ioctl, and if you use /dev/hidraw? you simply write() into it.

HID also defines "Features", that are an output control mechanism, but I've never used them.