How to write things to USB HID in Linux?

710 Views Asked by At

I'm working with a AMOLED screen. The vendor exposes the screen's brightness control to USB HID, and only give me an example webpage that uses Chrome's navigator.hid. I read the JavaScript code and find I need to do following things:

  • find a USB HID device whose vendor ID is 1810 and product ID is 10;
  • at each time I need to set brightness, I have to send a report with report ID of 9 and four bytes of data containing formatted brightness value.

Although it looks really clear and simple, I still don't know how to do these with Linux and C. Especially:

  • When I plugged the control USB port, I found there are two new device files: /dev/hidraw0 and /dev/usb/hiddev0. What are difference between them and which one should I use?
  • How to do the USB-specific things i.e. fetch vendor ID and product ID, send the report with report ID? Is there a specific header file for ioctl flags?
1

There are 1 best solutions below

0
On

After some study I found the /dev/hidrawX device should be used, and the /dev/usb/hiddevX is probably for the screen touch input. The API is described in linux kernel documentation and no library is needed.

Actually all things are sample and direct. Firstly I should open the hidraw device. The vendor ID and product ID can be retrieved using ioctl with HIDIOCGRAWINFO and struct hidraw_devinfo:

struct hidraw_devinfo info;
ioctl(fd, HIDIOCGRAWINFO, &info);

After identified correct hidraw device, I should write them 5-byte array using write. The first byte contains report ID of 9, and the latter four bytes contains the message that make the device happy.