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