1.\" $OpenBSD: usbhid.3,v 1.22 2021/05/30 19:54:52 jcs 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 30 2021 $ 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_get_report_desc_data , 37.Nm hid_start_parse , 38.Nm hid_end_parse , 39.Nm hid_get_item , 40.Nm hid_report_size , 41.Nm hid_locate , 42.Nm hid_usage_page , 43.Nm hid_usage_in_page , 44.Nm hid_parse_usage_page , 45.Nm hid_parse_usage_in_page , 46.Nm hid_start , 47.Nm hid_init , 48.Nm hid_get_data , 49.Nm hid_set_data 50.Nd USB HID access routines 51.Sh SYNOPSIS 52.In usbhid.h 53.Ft report_desc_t 54.Fn hid_get_report_desc "int file" 55.Ft report_desc_t 56.Fn hid_use_report_desc "unsigned char *data" "unsigned int size" 57.Ft void 58.Fn hid_dispose_report_desc "report_desc_t d" 59.Ft void 60.Fn hid_get_report_desc_data "report_desc_t d" "uint8_t **data" "uint32_t *size" 61.Ft hid_data_t 62.Fn hid_start_parse "report_desc_t d" "int kindset" "int id" 63.Ft void 64.Fn hid_end_parse "hid_data_t s" 65.Ft int 66.Fn hid_get_item "hid_data_t s" "hid_item_t *h" 67.Ft int 68.Fn hid_report_size "report_desc_t d" "hid_kind_t k" "int id" 69.Ft int 70.Fn hid_locate "report_desc_t d" "u_int usage" "hid_kind_t k" "hid_item_t *h" "int id" 71.Ft const char * 72.Fn hid_usage_page "int i" 73.Ft const char * 74.Fn hid_usage_in_page "u_int u" 75.Ft int 76.Fn hid_parse_usage_page "const char *" 77.Ft int 78.Fn hid_parse_usage_in_page "const char *" 79.Ft void 80.Fn hid_init "char *file" 81.Ft int 82.Fn hid_start "char *file" 83.Ft int32_t 84.Fn hid_get_data "void *data" "hid_item_t *h" 85.Ft void 86.Fn hid_set_data "void *data" "hid_item_t *h" "int32_t data" 87.Sh DESCRIPTION 88The 89.Nm 90library provides routines to extract data from USB Human Interface Devices. 91.Ss INTRODUCTION 92USB HIDs send and receive data laid out in a device dependent way. 93The 94.Nm 95library contains routines to extract the 96.Em report descriptor 97which contains the data layout information and then use this information. 98.Pp 99The routines can be divided into four parts: extraction of the descriptor, 100parsing of the descriptor, translating to/from symbolic names, and 101data manipulation. 102.Ss DESCRIPTOR FUNCTIONS 103A report descriptor can be obtained by calling 104.Fn hid_get_report_desc 105with a file descriptor obtained by opening a 106.Xr uhid 4 107device. 108Alternatively a data buffer containing the report descriptor can be passed into 109.Fn hid_use_report_desc . 110The data is copied into an internal structure which can be accessed with 111.Fn hid_get_report_desc_data . 112When the report descriptor is no longer needed it should be freed by calling 113.Fn hid_dispose_report_desc . 114The type 115.Fa report_desc_t 116is opaque and should be used when calling the parsing functions. 117.Fn hid_get_report_desc 118and 119.Fn hid_use_report_desc 120return 121.Fa NULL 122on failure. 123.Ss DESCRIPTOR PARSING FUNCTIONS 124To parse the report descriptor, the 125.Fn hid_start_parse 126function should be called with a report descriptor and a set that 127describes which items are interesting. 128The set is obtained by or-ing together values 129.Fa (1 << k) 130where 131.Fa k 132is an item of type 133.Fa hid_kind_t . 134The desired report ID, or \-1 135to obtain items of all report IDs, 136is given by 137.Fa id . 138The function returns 139.Fa NULL 140if the initialization fails, otherwise an opaque value to be used 141in subsequent calls. 142After parsing the 143.Fn hid_end_parse 144function should be called to free internal data structures. 145.Pp 146To iterate through all the items in the report descriptor, 147.Fn hid_get_item 148should be called while it returns a value greater than 0. 149When the report descriptor ends it will return 0; a syntax 150error within the report descriptor will cause a return value less 151than 0. 152The struct pointed to by 153.Fa h 154will be filled with the relevant data for the item. 155The definition of 156.Fa hid_item_t 157can be found in 158.In usbhid.h 159and the meaning of the components in the USB HID documentation. 160.Pp 161Data should be read/written to the device in the size of 162the report. 163The size of a report (of a certain kind) can be computed by the 164.Fn hid_report_size 165function. 166If the report is prefixed by an ID byte it is given by 167.Fa id . 168.Pp 169To locate a single item the 170.Fn hid_locate 171function can be used. 172It should be given the usage code of 173the item and its kind and it will fill the item and return 174non-zero if the item was found. 175.Ss NAME TRANSLATION FUNCTIONS 176The function 177.Fn hid_usage_page 178will return the symbolic name of a usage page, and the function 179.Fn hid_usage_in_page 180will return the symbolic name of the usage within the page. 181Both these functions may return a pointer to static data. 182.Pp 183The functions 184.Fn hid_parse_usage_page 185and 186.Fn hid_parse_usage_in_page 187are the inverses of 188.Fn hid_usage_page 189and 190.Fn hid_usage_in_page . 191They take a usage string and return the number of the usage, or \-1 192if it cannot be found. 193.Pp 194Before any of these functions can be called the usage table 195must be parsed, this is done by calling 196.Fn hid_start 197with the name of the table. 198Passing 199.Fa NULL 200to this function will cause it to use the default table. 201A return value of \-1 indicates that an error has occurred, and 202.Va errno 203is set. 204.Ss DATA EXTRACTION FUNCTIONS 205Given the data obtained from a HID and an item in the 206report descriptor, the 207.Fn hid_get_data 208function extracts the value of the item. 209Conversely 210.Fn hid_set_data 211can be used to put data into a report (which must be zeroed first). 212.Sh FILES 213.Pa /usr/share/misc/usb_hid_usages 214The default HID usage table. 215.\" .Sh EXAMPLES 216.Sh SEE ALSO 217The USB specifications can be found at: 218.Lk https://www.usb.org/documents/ 219.Pp 220.Xr uhid 4 , 221.Xr usb 4 222.Sh HISTORY 223The 224.Nm 225library first appeared in 226.Ox 3.0 . 227.Sh BUGS 228This man page is woefully incomplete. 229