1cca47861SMauro Carvalho Chehab================================================================ 2cca47861SMauro Carvalho ChehabHIDRAW - Raw Access to USB and Bluetooth Human Interface Devices 3cca47861SMauro Carvalho Chehab================================================================ 4cca47861SMauro Carvalho Chehab 5cca47861SMauro Carvalho ChehabThe hidraw driver provides a raw interface to USB and Bluetooth Human 6cca47861SMauro Carvalho ChehabInterface Devices (HIDs). It differs from hiddev in that reports sent and 7cca47861SMauro Carvalho Chehabreceived are not parsed by the HID parser, but are sent to and received from 8cca47861SMauro Carvalho Chehabthe device unmodified. 9cca47861SMauro Carvalho Chehab 10cca47861SMauro Carvalho ChehabHidraw should be used if the userspace application knows exactly how to 11cca47861SMauro Carvalho Chehabcommunicate with the hardware device, and is able to construct the HID 12cca47861SMauro Carvalho Chehabreports manually. This is often the case when making userspace drivers for 13cca47861SMauro Carvalho Chehabcustom HID devices. 14cca47861SMauro Carvalho Chehab 15cca47861SMauro Carvalho ChehabHidraw is also useful for communicating with non-conformant HID devices 16cca47861SMauro Carvalho Chehabwhich send and receive data in a way that is inconsistent with their report 17cca47861SMauro Carvalho Chehabdescriptors. Because hiddev parses reports which are sent and received 18cca47861SMauro Carvalho Chehabthrough it, checking them against the device's report descriptor, such 19cca47861SMauro Carvalho Chehabcommunication with these non-conformant devices is impossible using hiddev. 20cca47861SMauro Carvalho ChehabHidraw is the only alternative, short of writing a custom kernel driver, for 21cca47861SMauro Carvalho Chehabthese non-conformant devices. 22cca47861SMauro Carvalho Chehab 23cca47861SMauro Carvalho ChehabA benefit of hidraw is that its use by userspace applications is independent 241c900363SRandy Dunlapof the underlying hardware type. Currently, hidraw is implemented for USB 25cca47861SMauro Carvalho Chehaband Bluetooth. In the future, as new hardware bus types are developed which 26cca47861SMauro Carvalho Chehabuse the HID specification, hidraw will be expanded to add support for these 27cca47861SMauro Carvalho Chehabnew bus types. 28cca47861SMauro Carvalho Chehab 29cca47861SMauro Carvalho ChehabHidraw uses a dynamic major number, meaning that udev should be relied on to 30cca47861SMauro Carvalho Chehabcreate hidraw device nodes. Udev will typically create the device nodes 31cca47861SMauro Carvalho Chehabdirectly under /dev (eg: /dev/hidraw0). As this location is distribution- 32cca47861SMauro Carvalho Chehaband udev rule-dependent, applications should use libudev to locate hidraw 33cca47861SMauro Carvalho Chehabdevices attached to the system. There is a tutorial on libudev with a 341c900363SRandy Dunlapworking example at:: 35cca47861SMauro Carvalho Chehab 36cca47861SMauro Carvalho Chehab http://www.signal11.us/oss/udev/ 371c900363SRandy Dunlap https://web.archive.org/web/2019*/www.signal11.us 38cca47861SMauro Carvalho Chehab 39cca47861SMauro Carvalho ChehabThe HIDRAW API 40cca47861SMauro Carvalho Chehab--------------- 41cca47861SMauro Carvalho Chehab 42cca47861SMauro Carvalho Chehabread() 43cca47861SMauro Carvalho Chehab------- 44cca47861SMauro Carvalho Chehabread() will read a queued report received from the HID device. On USB 45cca47861SMauro Carvalho Chehabdevices, the reports read using read() are the reports sent from the device 46cca47861SMauro Carvalho Chehabon the INTERRUPT IN endpoint. By default, read() will block until there is 47cca47861SMauro Carvalho Chehaba report available to be read. read() can be made non-blocking, by passing 48cca47861SMauro Carvalho Chehabthe O_NONBLOCK flag to open(), or by setting the O_NONBLOCK flag using 49cca47861SMauro Carvalho Chehabfcntl(). 50cca47861SMauro Carvalho Chehab 51cca47861SMauro Carvalho ChehabOn a device which uses numbered reports, the first byte of the returned data 52cca47861SMauro Carvalho Chehabwill be the report number; the report data follows, beginning in the second 53cca47861SMauro Carvalho Chehabbyte. For devices which do not use numbered reports, the report data 54cca47861SMauro Carvalho Chehabwill begin at the first byte. 55cca47861SMauro Carvalho Chehab 56cca47861SMauro Carvalho Chehabwrite() 57cca47861SMauro Carvalho Chehab------- 58cca47861SMauro Carvalho ChehabThe write() function will write a report to the device. For USB devices, if 59cca47861SMauro Carvalho Chehabthe device has an INTERRUPT OUT endpoint, the report will be sent on that 60cca47861SMauro Carvalho Chehabendpoint. If it does not, the report will be sent over the control endpoint, 61cca47861SMauro Carvalho Chehabusing a SET_REPORT transfer. 62cca47861SMauro Carvalho Chehab 63cca47861SMauro Carvalho ChehabThe first byte of the buffer passed to write() should be set to the report 64cca47861SMauro Carvalho Chehabnumber. If the device does not use numbered reports, the first byte should 65cca47861SMauro Carvalho Chehabbe set to 0. The report data itself should begin at the second byte. 66cca47861SMauro Carvalho Chehab 67cca47861SMauro Carvalho Chehabioctl() 68cca47861SMauro Carvalho Chehab------- 69cca47861SMauro Carvalho ChehabHidraw supports the following ioctls: 70cca47861SMauro Carvalho Chehab 71cca47861SMauro Carvalho ChehabHIDIOCGRDESCSIZE: 72cca47861SMauro Carvalho Chehab Get Report Descriptor Size 73cca47861SMauro Carvalho Chehab 74cca47861SMauro Carvalho ChehabThis ioctl will get the size of the device's report descriptor. 75cca47861SMauro Carvalho Chehab 76cca47861SMauro Carvalho ChehabHIDIOCGRDESC: 77cca47861SMauro Carvalho Chehab Get Report Descriptor 78cca47861SMauro Carvalho Chehab 79cca47861SMauro Carvalho ChehabThis ioctl returns the device's report descriptor using a 80cca47861SMauro Carvalho Chehabhidraw_report_descriptor struct. Make sure to set the size field of the 81cca47861SMauro Carvalho Chehabhidraw_report_descriptor struct to the size returned from HIDIOCGRDESCSIZE. 82cca47861SMauro Carvalho Chehab 83cca47861SMauro Carvalho ChehabHIDIOCGRAWINFO: 84cca47861SMauro Carvalho Chehab Get Raw Info 85cca47861SMauro Carvalho Chehab 86cca47861SMauro Carvalho ChehabThis ioctl will return a hidraw_devinfo struct containing the bus type, the 87cca47861SMauro Carvalho Chehabvendor ID (VID), and product ID (PID) of the device. The bus type can be one 88cca47861SMauro Carvalho Chehabof:: 89cca47861SMauro Carvalho Chehab 90cca47861SMauro Carvalho Chehab - BUS_USB 91cca47861SMauro Carvalho Chehab - BUS_HIL 92cca47861SMauro Carvalho Chehab - BUS_BLUETOOTH 93cca47861SMauro Carvalho Chehab - BUS_VIRTUAL 94cca47861SMauro Carvalho Chehab 95cca47861SMauro Carvalho Chehabwhich are defined in uapi/linux/input.h. 96cca47861SMauro Carvalho Chehab 97cca47861SMauro Carvalho ChehabHIDIOCGRAWNAME(len): 98cca47861SMauro Carvalho Chehab Get Raw Name 99cca47861SMauro Carvalho Chehab 100cca47861SMauro Carvalho ChehabThis ioctl returns a string containing the vendor and product strings of 101cca47861SMauro Carvalho Chehabthe device. The returned string is Unicode, UTF-8 encoded. 102cca47861SMauro Carvalho Chehab 103cca47861SMauro Carvalho ChehabHIDIOCGRAWPHYS(len): 104cca47861SMauro Carvalho Chehab Get Physical Address 105cca47861SMauro Carvalho Chehab 106cca47861SMauro Carvalho ChehabThis ioctl returns a string representing the physical address of the device. 107cca47861SMauro Carvalho ChehabFor USB devices, the string contains the physical path to the device (the 108cca47861SMauro Carvalho ChehabUSB controller, hubs, ports, etc). For Bluetooth devices, the string 109cca47861SMauro Carvalho Chehabcontains the hardware (MAC) address of the device. 110cca47861SMauro Carvalho Chehab 111cca47861SMauro Carvalho ChehabHIDIOCSFEATURE(len): 112cca47861SMauro Carvalho Chehab Send a Feature Report 113cca47861SMauro Carvalho Chehab 114cca47861SMauro Carvalho ChehabThis ioctl will send a feature report to the device. Per the HID 115cca47861SMauro Carvalho Chehabspecification, feature reports are always sent using the control endpoint. 116cca47861SMauro Carvalho ChehabSet the first byte of the supplied buffer to the report number. For devices 117cca47861SMauro Carvalho Chehabwhich do not use numbered reports, set the first byte to 0. The report data 118cca47861SMauro Carvalho Chehabbegins in the second byte. Make sure to set len accordingly, to one more 119cca47861SMauro Carvalho Chehabthan the length of the report (to account for the report number). 120cca47861SMauro Carvalho Chehab 121cca47861SMauro Carvalho ChehabHIDIOCGFEATURE(len): 122cca47861SMauro Carvalho Chehab Get a Feature Report 123cca47861SMauro Carvalho Chehab 124cca47861SMauro Carvalho ChehabThis ioctl will request a feature report from the device using the control 125cca47861SMauro Carvalho Chehabendpoint. The first byte of the supplied buffer should be set to the report 126cca47861SMauro Carvalho Chehabnumber of the requested report. For devices which do not use numbered 127f43d3870SDean Camerareports, set the first byte to 0. The returned report buffer will contain the 128f43d3870SDean Camerareport number in the first byte, followed by the report data read from the 129f43d3870SDean Cameradevice. For devices which do not use numbered reports, the report data will 130f43d3870SDean Camerabegin at the first byte of the returned buffer. 131f43d3870SDean Camera 132f43d3870SDean CameraHIDIOCSINPUT(len): 133f43d3870SDean Camera Send an Input Report 134f43d3870SDean Camera 135f43d3870SDean CameraThis ioctl will send an input report to the device, using the control endpoint. 136f43d3870SDean CameraIn most cases, setting an input HID report on a device is meaningless and has 137f43d3870SDean Camerano effect, but some devices may choose to use this to set or reset an initial 138f43d3870SDean Camerastate of a report. The format of the buffer issued with this report is identical 139f43d3870SDean Camerato that of HIDIOCSFEATURE. 140f43d3870SDean Camera 141f43d3870SDean CameraHIDIOCGINPUT(len): 142f43d3870SDean Camera Get an Input Report 143f43d3870SDean Camera 144f43d3870SDean CameraThis ioctl will request an input report from the device using the control 145f43d3870SDean Cameraendpoint. This is slower on most devices where a dedicated In endpoint exists 146f43d3870SDean Camerafor regular input reports, but allows the host to request the value of a 147f43d3870SDean Cameraspecific report number. Typically, this is used to request the initial states of 148f43d3870SDean Cameraan input report of a device, before an application listens for normal reports via 149f43d3870SDean Camerathe regular device read() interface. The format of the buffer issued with this report 150f43d3870SDean Camerais identical to that of HIDIOCGFEATURE. 151f43d3870SDean Camera 152f43d3870SDean CameraHIDIOCSOUTPUT(len): 153f43d3870SDean Camera Send an Output Report 154f43d3870SDean Camera 155f43d3870SDean CameraThis ioctl will send an output report to the device, using the control endpoint. 156f43d3870SDean CameraThis is slower on most devices where a dedicated Out endpoint exists for regular 157f43d3870SDean Cameraoutput reports, but is added for completeness. Typically, this is used to set 158f43d3870SDean Camerathe initial states of an output report of a device, before an application sends 159f43d3870SDean Cameraupdates via the regular device write() interface. The format of the buffer issued 160f43d3870SDean Camerawith this report is identical to that of HIDIOCSFEATURE. 161f43d3870SDean Camera 162f43d3870SDean CameraHIDIOCGOUTPUT(len): 163f43d3870SDean Camera Get an Output Report 164f43d3870SDean Camera 165f43d3870SDean CameraThis ioctl will request an output report from the device using the control 166*2f7f4efbSRandy Dunlapendpoint. Typically, this is used to retrieve the initial state of 167f43d3870SDean Cameraan output report of a device, before an application updates it as necessary either 168f43d3870SDean Cameravia a HIDIOCSOUTPUT request, or the regular device write() interface. The format 169f43d3870SDean Cameraof the buffer issued with this report is identical to that of HIDIOCGFEATURE. 170cca47861SMauro Carvalho Chehab 171cca47861SMauro Carvalho ChehabExample 172cca47861SMauro Carvalho Chehab------- 173cca47861SMauro Carvalho ChehabIn samples/, find hid-example.c, which shows examples of read(), write(), 174cca47861SMauro Carvalho Chehaband all the ioctls for hidraw. The code may be used by anyone for any 175cca47861SMauro Carvalho Chehabpurpose, and can serve as a starting point for developing applications using 176cca47861SMauro Carvalho Chehabhidraw. 177cca47861SMauro Carvalho Chehab 178cca47861SMauro Carvalho ChehabDocument by: 179cca47861SMauro Carvalho Chehab 180cca47861SMauro Carvalho Chehab Alan Ott <alan@signal11.us>, Signal 11 Software 181