1.\" $NetBSD: usb.3,v 1.13 2000/09/24 02:17:52 augustss 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 January 27, 2009 28.Dt USBHID 3 29.Os 30.Sh NAME 31.Nm usbhid , 32.Nm hid_get_report_desc , 33.Nm hid_get_report_id , 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_init , 44.Nm hid_get_data , 45.Nm hid_set_data , 46.Nm hid_get_report , 47.Nm hid_set_report 48.Nd USB HID access routines 49.Sh LIBRARY 50.Lb libusbhid 51.Sh SYNOPSIS 52.In usbhid.h 53.Ft report_desc_t 54.Fn hid_get_report_desc "int file" 55.Ft int 56.Fn hid_get_report_id "int file" 57.Ft int 58.Fn hid_set_immed "int fd" "int enable" 59.Ft report_desc_t 60.Fn hid_use_report_desc "unsigned char *data" "unsigned int size" 61.Ft void 62.Fn hid_dispose_report_desc "report_desc_t d" 63.Ft hid_data_t 64.Fn hid_start_parse "report_desc_t d" "int kindset" "int id" 65.Ft void 66.Fn hid_end_parse "hid_data_t s" 67.Ft int 68.Fn hid_get_item "hid_data_t s" "hid_item_t *h" 69.Ft int 70.Fn hid_report_size "report_desc_t d" "hid_kind_t k" "int id" 71.Ft int 72.Fn hid_locate "report_desc_t d" "u_int usage" "hid_kind_t k" "hid_item_t *h" "int id" 73.Ft "const char *" 74.Fn hid_usage_page "int i" 75.Ft "const char *" 76.Fn hid_usage_in_page "u_int u" 77.Ft int 78.Fn hid_parse_usage_page "const char *" 79.Ft int 80.Fn hid_parse_usage_in_page "const char *" 81.Ft void 82.Fn hid_init "const char *file" 83.Ft int 84.Fn hid_get_data "const void *data" "const hid_item_t *h" 85.Ft void 86.Fn hid_set_data "void *buf" "const hid_item_t *h" "int data" 87.Ft int 88.Fn hid_get_report "int fd" "enum hid_kind k" "unsigned char *data" "unsigned int size" 89.Ft int 90.Fn hid_set_report "int fd" "enum hid_kind k" "unsigned char *data" "unsigned int size" 91.Sh DESCRIPTION 92The 93.Nm 94library provides routines to extract data from USB Human Interface Devices. 95.Ss Introduction 96USB HID devices send and receive data laid out in a device dependent way. 97The 98.Nm 99library contains routines to extract the 100.Em "report descriptor" 101which contains the data layout information and then use this information. 102.Pp 103The routines can be divided into four parts: extraction of the descriptor, 104parsing of the descriptor, translating to/from symbolic names, and 105data manipulation. 106.Ss Synchronous HID operation 107Synchronous HID operation can be enabled or disabled by a call to 108.Fn hid_set_immed . 109If the second argument is zero synchronous HID operation is disabled. 110Else synchronous HID operation is enabled. 111The function returns a negative value on failure. 112.Pp 113.Fn hid_get_report 114and 115.Fn hid_set_report 116functions allow to synchronously get and set specific report if device 117supports it. 118For devices with multiple report IDs, wanted ID should be provided in the 119first byte of the buffer for both get and set. 120.Ss Descriptor Functions 121The report descriptor ID can be obtained by calling 122.Fn hid_get_report_id . 123A report descriptor can be obtained by calling 124.Fn hid_get_report_desc 125with a file descriptor obtained by opening a 126.Xr uhid 4 127device. 128Alternatively a data buffer containing the report descriptor can be 129passed into 130.Fn hid_use_report_desc . 131The data is copied into an internal structure. 132When the report descriptor 133is no longer needed it should be freed by calling 134.Fn hid_dispose_report_desc . 135The type 136.Vt report_desc_t 137is opaque and should be used when calling the parsing functions. 138If 139.Fn hid_dispose_report_desc 140fails it will return 141.Dv NULL . 142.Ss Descriptor Parsing Functions 143To parse the report descriptor the 144.Fn hid_start_parse 145function should be called with a report descriptor, a set that 146describes which items that are interesting, and the desired report 147ID (or -1 to obtain items of all report IDs). 148The set is obtained by OR-ing together values 149.Fa "(1 << k)" 150where 151.Fa k 152is an item of type 153.Vt hid_kind_t . 154The function returns 155.Dv NULL 156if the initialization fails, otherwise an opaque value to be used 157in subsequent calls. 158After parsing the 159.Fn hid_end_parse 160function should be called to free internal data structures. 161.Pp 162To iterate through all the items in the report descriptor 163.Fn hid_get_item 164should be called while it returns a value greater than 0. 165When the report descriptor ends it will returns 0; a syntax 166error within the report descriptor will cause a return value less 167than 0. 168The struct pointed to by 169.Fa h 170will be filled with the relevant data for the item. 171The definition of 172.Vt hid_item_t 173can be found in 174.In usbhid.h 175and the meaning of the components in the USB HID documentation. 176.Pp 177Data should be read/written to the device in the size of 178the report. 179The size of a report (of a certain kind) can be computed by the 180.Fn hid_report_size 181function. 182If the report is prefixed by an ID byte it is given by 183.Fa id . 184.Pp 185To locate a single item the 186.Fn hid_locate 187function can be used. 188It should be given the usage code of 189the item and its kind and it will fill the item and return 190non-zero if the item was found. 191.Ss Name Translation Functions 192The function 193.Fn hid_usage_page 194will return the symbolic name of a usage page, and the function 195.Fn hid_usage_in_page 196will return the symbolic name of the usage within the page. 197Both these functions may return a pointer to static data. 198.Pp 199The functions 200.Fn hid_parse_usage_page 201and 202.Fn hid_parse_usage_in_page 203are the inverses of 204.Fn hid_usage_page 205and 206.Fn hid_usage_in_page . 207They take a usage string and return the number of the usage, or \-1 208if it cannot be found. 209.Pp 210Before any of these functions can be called the usage table 211must be parsed, this is done by calling 212.Fn hid_init 213with the name of the table. 214Passing 215.Dv NULL 216to this function will cause it to use the default table. 217.Ss Data Extraction Functions 218Given the data obtained from a HID device and an item in the 219report descriptor the 220.Fn hid_get_data 221function extracts the value of the item. 222Conversely 223.Fn hid_set_data 224can be used to put data into a report (which must be zeroed first). 225.Sh FILES 226.Bl -tag -width ".Pa /usr/share/misc/usb_hid_usages" 227.It Pa /usr/share/misc/usb_hid_usages 228The default HID usage table. 229.El 230.Sh EXAMPLES 231Not yet. 232.Sh SEE ALSO 233The 234.Tn USB 235specifications can be found at 236.Pa http://www.usb.org/developers/docs/ . 237.Pp 238.Xr uhid 4 , 239.Xr usb 4 240.Sh HISTORY 241The 242.Nm 243library first appeared in 244.Nx 1.5 . 245.Sh BUGS 246This man page is woefully incomplete. 247