0001 ================================================================
0002 HIDRAW - Raw Access to USB and Bluetooth Human Interface Devices
0003 ================================================================
0004
0005 The hidraw driver provides a raw interface to USB and Bluetooth Human
0006 Interface Devices (HIDs). It differs from hiddev in that reports sent and
0007 received are not parsed by the HID parser, but are sent to and received from
0008 the device unmodified.
0009
0010 Hidraw should be used if the userspace application knows exactly how to
0011 communicate with the hardware device, and is able to construct the HID
0012 reports manually. This is often the case when making userspace drivers for
0013 custom HID devices.
0014
0015 Hidraw is also useful for communicating with non-conformant HID devices
0016 which send and receive data in a way that is inconsistent with their report
0017 descriptors. Because hiddev parses reports which are sent and received
0018 through it, checking them against the device's report descriptor, such
0019 communication with these non-conformant devices is impossible using hiddev.
0020 Hidraw is the only alternative, short of writing a custom kernel driver, for
0021 these non-conformant devices.
0022
0023 A benefit of hidraw is that its use by userspace applications is independent
0024 of the underlying hardware type. Currently, hidraw is implemented for USB
0025 and Bluetooth. In the future, as new hardware bus types are developed which
0026 use the HID specification, hidraw will be expanded to add support for these
0027 new bus types.
0028
0029 Hidraw uses a dynamic major number, meaning that udev should be relied on to
0030 create hidraw device nodes. Udev will typically create the device nodes
0031 directly under /dev (eg: /dev/hidraw0). As this location is distribution-
0032 and udev rule-dependent, applications should use libudev to locate hidraw
0033 devices attached to the system. There is a tutorial on libudev with a
0034 working example at::
0035
0036 http://www.signal11.us/oss/udev/
0037 https://web.archive.org/web/2019*/www.signal11.us
0038
0039 The HIDRAW API
0040 ---------------
0041
0042 read()
0043 -------
0044 read() will read a queued report received from the HID device. On USB
0045 devices, the reports read using read() are the reports sent from the device
0046 on the INTERRUPT IN endpoint. By default, read() will block until there is
0047 a report available to be read. read() can be made non-blocking, by passing
0048 the O_NONBLOCK flag to open(), or by setting the O_NONBLOCK flag using
0049 fcntl().
0050
0051 On a device which uses numbered reports, the first byte of the returned data
0052 will be the report number; the report data follows, beginning in the second
0053 byte. For devices which do not use numbered reports, the report data
0054 will begin at the first byte.
0055
0056 write()
0057 -------
0058 The write() function will write a report to the device. For USB devices, if
0059 the device has an INTERRUPT OUT endpoint, the report will be sent on that
0060 endpoint. If it does not, the report will be sent over the control endpoint,
0061 using a SET_REPORT transfer.
0062
0063 The first byte of the buffer passed to write() should be set to the report
0064 number. If the device does not use numbered reports, the first byte should
0065 be set to 0. The report data itself should begin at the second byte.
0066
0067 ioctl()
0068 -------
0069 Hidraw supports the following ioctls:
0070
0071 HIDIOCGRDESCSIZE:
0072 Get Report Descriptor Size
0073
0074 This ioctl will get the size of the device's report descriptor.
0075
0076 HIDIOCGRDESC:
0077 Get Report Descriptor
0078
0079 This ioctl returns the device's report descriptor using a
0080 hidraw_report_descriptor struct. Make sure to set the size field of the
0081 hidraw_report_descriptor struct to the size returned from HIDIOCGRDESCSIZE.
0082
0083 HIDIOCGRAWINFO:
0084 Get Raw Info
0085
0086 This ioctl will return a hidraw_devinfo struct containing the bus type, the
0087 vendor ID (VID), and product ID (PID) of the device. The bus type can be one
0088 of::
0089
0090 - BUS_USB
0091 - BUS_HIL
0092 - BUS_BLUETOOTH
0093 - BUS_VIRTUAL
0094
0095 which are defined in uapi/linux/input.h.
0096
0097 HIDIOCGRAWNAME(len):
0098 Get Raw Name
0099
0100 This ioctl returns a string containing the vendor and product strings of
0101 the device. The returned string is Unicode, UTF-8 encoded.
0102
0103 HIDIOCGRAWPHYS(len):
0104 Get Physical Address
0105
0106 This ioctl returns a string representing the physical address of the device.
0107 For USB devices, the string contains the physical path to the device (the
0108 USB controller, hubs, ports, etc). For Bluetooth devices, the string
0109 contains the hardware (MAC) address of the device.
0110
0111 HIDIOCSFEATURE(len):
0112 Send a Feature Report
0113
0114 This ioctl will send a feature report to the device. Per the HID
0115 specification, feature reports are always sent using the control endpoint.
0116 Set the first byte of the supplied buffer to the report number. For devices
0117 which do not use numbered reports, set the first byte to 0. The report data
0118 begins in the second byte. Make sure to set len accordingly, to one more
0119 than the length of the report (to account for the report number).
0120
0121 HIDIOCGFEATURE(len):
0122 Get a Feature Report
0123
0124 This ioctl will request a feature report from the device using the control
0125 endpoint. The first byte of the supplied buffer should be set to the report
0126 number of the requested report. For devices which do not use numbered
0127 reports, set the first byte to 0. The returned report buffer will contain the
0128 report number in the first byte, followed by the report data read from the
0129 device. For devices which do not use numbered reports, the report data will
0130 begin at the first byte of the returned buffer.
0131
0132 HIDIOCSINPUT(len):
0133 Send an Input Report
0134
0135 This ioctl will send an input report to the device, using the control endpoint.
0136 In most cases, setting an input HID report on a device is meaningless and has
0137 no effect, but some devices may choose to use this to set or reset an initial
0138 state of a report. The format of the buffer issued with this report is identical
0139 to that of HIDIOCSFEATURE.
0140
0141 HIDIOCGINPUT(len):
0142 Get an Input Report
0143
0144 This ioctl will request an input report from the device using the control
0145 endpoint. This is slower on most devices where a dedicated In endpoint exists
0146 for regular input reports, but allows the host to request the value of a
0147 specific report number. Typically, this is used to request the initial states of
0148 an input report of a device, before an application listens for normal reports via
0149 the regular device read() interface. The format of the buffer issued with this report
0150 is identical to that of HIDIOCGFEATURE.
0151
0152 HIDIOCSOUTPUT(len):
0153 Send an Output Report
0154
0155 This ioctl will send an output report to the device, using the control endpoint.
0156 This is slower on most devices where a dedicated Out endpoint exists for regular
0157 output reports, but is added for completeness. Typically, this is used to set
0158 the initial states of an output report of a device, before an application sends
0159 updates via the regular device write() interface. The format of the buffer issued
0160 with this report is identical to that of HIDIOCSFEATURE.
0161
0162 HIDIOCGOUTPUT(len):
0163 Get an Output Report
0164
0165 This ioctl will request an output report from the device using the control
0166 endpoint. Typically, this is used to retrive the initial state of
0167 an output report of a device, before an application updates it as necessary either
0168 via a HIDIOCSOUTPUT request, or the regular device write() interface. The format
0169 of the buffer issued with this report is identical to that of HIDIOCGFEATURE.
0170
0171 Example
0172 -------
0173 In samples/, find hid-example.c, which shows examples of read(), write(),
0174 and all the ioctls for hidraw. The code may be used by anyone for any
0175 purpose, and can serve as a starting point for developing applications using
0176 hidraw.
0177
0178 Document by:
0179
0180 Alan Ott <alan@signal11.us>, Signal 11 Software