1.\" $OpenBSD: usbhid.3,v 1.20 2020/05/12 13:03:52 schwarze Exp $ 2.\" $NetBSD: usbhid.3,v 1.5 2002/02/07 07:00:52 ross Exp $ 3.\" 4.\" Copyright (c) 1999, 2001 Lennart Augustsson <augustss@netbsd.org> 5.\" All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd $Mdocdate: May 12 2020 $ 29.Dt USBHID 3 30.Os 31.Sh NAME 32.Nm usbhid , 33.Nm hid_get_report_desc , 34.Nm hid_use_report_desc , 35.Nm hid_dispose_report_desc , 36.Nm hid_start_parse , 37.Nm hid_end_parse , 38.Nm hid_get_item , 39.Nm hid_report_size , 40.Nm hid_locate , 41.Nm hid_usage_page , 42.Nm hid_usage_in_page , 43.Nm hid_parse_usage_page , 44.Nm hid_parse_usage_in_page , 45.Nm hid_start , 46.Nm hid_init , 47.Nm hid_get_data , 48.Nm hid_set_data 49.Nd USB HID access routines 50.Sh SYNOPSIS 51.In usbhid.h 52.Ft report_desc_t 53.Fn hid_get_report_desc "int file" 54.Ft report_desc_t 55.Fn hid_use_report_desc "unsigned char *data" "unsigned int size" 56.Ft void 57.Fn hid_dispose_report_desc "report_desc_t d" 58.Ft hid_data_t 59.Fn hid_start_parse "report_desc_t d" "int kindset" "int id" 60.Ft void 61.Fn hid_end_parse "hid_data_t s" 62.Ft int 63.Fn hid_get_item "hid_data_t s" "hid_item_t *h" 64.Ft int 65.Fn hid_report_size "report_desc_t d" "hid_kind_t k" "int id" 66.Ft int 67.Fn hid_locate "report_desc_t d" "u_int usage" "hid_kind_t k" "hid_item_t *h" "int id" 68.Ft const char * 69.Fn hid_usage_page "int i" 70.Ft const char * 71.Fn hid_usage_in_page "u_int u" 72.Ft int 73.Fn hid_parse_usage_page "const char *" 74.Ft int 75.Fn hid_parse_usage_in_page "const char *" 76.Ft void 77.Fn hid_init "char *file" 78.Ft int 79.Fn hid_start "char *file" 80.Ft int32_t 81.Fn hid_get_data "void *data" "hid_item_t *h" 82.Ft void 83.Fn hid_set_data "void *data" "hid_item_t *h" "int32_t data" 84.Sh DESCRIPTION 85The 86.Nm 87library provides routines to extract data from USB Human Interface Devices. 88.Ss INTRODUCTION 89USB HID devices send and receive data laid out in a device dependent way. 90The 91.Nm 92library contains routines to extract the 93.Em report descriptor 94which contains the data layout information and then use this information. 95.Pp 96The routines can be divided into four parts: extraction of the descriptor, 97parsing of the descriptor, translating to/from symbolic names, and 98data manipulation. 99.Ss DESCRIPTOR FUNCTIONS 100A report descriptor can be obtained by calling 101.Fn hid_get_report_desc 102with a file descriptor obtained by opening a 103.Xr uhid 4 104device. 105Alternatively a data buffer containing the report descriptor can be passed into 106.Fn hid_use_report_desc . 107The data is copied into an internal structure. 108When the report descriptor is no longer needed it should be freed by calling 109.Fn hid_dispose_report_desc . 110The type 111.Fa report_desc_t 112is opaque and should be used when calling the parsing functions. 113.Fn hid_get_report_desc 114and 115.Fn hid_use_report_desc 116return 117.Fa NULL 118on failure. 119.Ss DESCRIPTOR PARSING FUNCTIONS 120To parse the report descriptor the 121.Fn hid_start_parse 122function should be called with a report descriptor and a set that 123describes which items that are interesting. 124The set is obtained by or-ing together values 125.Fa (1 << k) 126where 127.Fa k 128is an item of type 129.Fa hid_kind_t . 130The desired report ID, or \-1 131to obtain items of all report IDs, 132is given by 133.Fa id . 134The function returns 135.Fa NULL 136if the initialization fails, otherwise an opaque value to be used 137in subsequent calls. 138After parsing the 139.Fn hid_end_parse 140function should be called to free internal data structures. 141.Pp 142To iterate through all the items in the report descriptor, 143.Fn hid_get_item 144should be called while it returns a value greater than 0. 145When the report descriptor ends it will return 0; a syntax 146error within the report descriptor will cause a return value less 147than 0. 148The struct pointed to by 149.Fa h 150will be filled with the relevant data for the item. 151The definition of 152.Fa hid_item_t 153can be found in 154.In usbhid.h 155and the meaning of the components in the USB HID documentation. 156.Pp 157Data should be read/written to the device in the size of 158the report. 159The size of a report (of a certain kind) can be computed by the 160.Fn hid_report_size 161function. 162If the report is prefixed by an ID byte it is given by 163.Fa id . 164.Pp 165To locate a single item the 166.Fn hid_locate 167function can be used. 168It should be given the usage code of 169the item and its kind and it will fill the item and return 170non-zero if the item was found. 171.Ss NAME TRANSLATION FUNCTIONS 172The function 173.Fn hid_usage_page 174will return the symbolic name of a usage page, and the function 175.Fn hid_usage_in_page 176will return the symbolic name of the usage within the page. 177Both these functions may return a pointer to static data. 178.Pp 179The functions 180.Fn hid_parse_usage_page 181and 182.Fn hid_parse_usage_in_page 183are the inverses of 184.Fn hid_usage_page 185and 186.Fn hid_usage_in_page . 187They take a usage string and return the number of the usage, or \-1 188if it cannot be found. 189.Pp 190Before any of these functions can be called the usage table 191must be parsed, this is done by calling 192.Fn hid_start 193with the name of the table. 194Passing 195.Fa NULL 196to this function will cause it to use the default table. 197A return value of \-1 indicates that an error has occurred, and 198.Va errno 199is set. 200.Ss DATA EXTRACTION FUNCTIONS 201Given the data obtained from a HID device and an item in the 202report descriptor the 203.Fn hid_get_data 204function extracts the value of the item. 205Conversely 206.Fn hid_set_data 207can be used to put data into a report (which must be zeroed first). 208.Sh FILES 209.Pa /usr/share/misc/usb_hid_usages 210The default HID usage table. 211.\" .Sh EXAMPLES 212.Sh SEE ALSO 213The USB specifications can be found at: 214.Lk https://www.usb.org/documents/ 215.Pp 216.Xr uhid 4 , 217.Xr usb 4 218.Sh HISTORY 219The 220.Nm 221library first appeared in 222.Ox 3.0 . 223.Sh BUGS 224This man page is woefully incomplete. 225