xref: /freebsd/lib/libusbhid/usbhid.3 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
1b425e319SNick Hibma.\"	$NetBSD: usb.3,v 1.13 2000/09/24 02:17:52 augustss Exp $
29e2046dfSNick Hibma.\"
3cf0e07e5SMatthew N. Dodd.\" Copyright (c) 1999, 2001 Lennart Augustsson <augustss@NetBSD.org>
49e2046dfSNick Hibma.\" All rights reserved.
59e2046dfSNick Hibma.\"
69e2046dfSNick Hibma.\" Redistribution and use in source and binary forms, with or without
79e2046dfSNick Hibma.\" modification, are permitted provided that the following conditions
89e2046dfSNick Hibma.\" are met:
99e2046dfSNick Hibma.\" 1. Redistributions of source code must retain the above copyright
109e2046dfSNick Hibma.\"    notice, this list of conditions and the following disclaimer.
119e2046dfSNick Hibma.\" 2. Redistributions in binary form must reproduce the above copyright
129e2046dfSNick Hibma.\"    notice, this list of conditions and the following disclaimer in the
139e2046dfSNick Hibma.\"    documentation and/or other materials provided with the distribution.
149e2046dfSNick Hibma.\"
159e2046dfSNick Hibma.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
169e2046dfSNick Hibma.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
179e2046dfSNick Hibma.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
189e2046dfSNick Hibma.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199e2046dfSNick Hibma.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
209e2046dfSNick Hibma.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
219e2046dfSNick Hibma.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
229e2046dfSNick Hibma.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
239e2046dfSNick Hibma.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
249e2046dfSNick Hibma.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
259e2046dfSNick Hibma.\" SUCH DAMAGE.
269e2046dfSNick Hibma.\"
277e474656SAlfred Perlstein.Dd January 27, 2009
287793e755SJosef Karthauser.Dt USBHID 3
299e2046dfSNick Hibma.Os
309e2046dfSNick Hibma.Sh NAME
317793e755SJosef Karthauser.Nm usbhid ,
329e2046dfSNick Hibma.Nm hid_get_report_desc ,
337e474656SAlfred Perlstein.Nm hid_get_report_id ,
34b425e319SNick Hibma.Nm hid_use_report_desc ,
359e2046dfSNick Hibma.Nm hid_dispose_report_desc ,
369e2046dfSNick Hibma.Nm hid_start_parse ,
379e2046dfSNick Hibma.Nm hid_end_parse ,
389e2046dfSNick Hibma.Nm hid_get_item ,
399e2046dfSNick Hibma.Nm hid_report_size ,
409e2046dfSNick Hibma.Nm hid_locate ,
419e2046dfSNick Hibma.Nm hid_usage_page ,
429e2046dfSNick Hibma.Nm hid_usage_in_page ,
439e2046dfSNick Hibma.Nm hid_init ,
449e2046dfSNick Hibma.Nm hid_get_data ,
457778ab7eSAlexander Motin.Nm hid_set_data ,
467778ab7eSAlexander Motin.Nm hid_get_report ,
477778ab7eSAlexander Motin.Nm hid_set_report
489e2046dfSNick Hibma.Nd USB HID access routines
499e2046dfSNick Hibma.Sh LIBRARY
507b8cfc52SJosef Karthauser.Lb libusbhid
519e2046dfSNick Hibma.Sh SYNOPSIS
52cf0e07e5SMatthew N. Dodd.In usbhid.h
539e2046dfSNick Hibma.Ft report_desc_t
549e2046dfSNick Hibma.Fn hid_get_report_desc "int file"
557e474656SAlfred Perlstein.Ft int
567e474656SAlfred Perlstein.Fn hid_get_report_id "int file"
577e474656SAlfred Perlstein.Ft int
587e474656SAlfred Perlstein.Fn hid_set_immed "int fd" "int enable"
59b425e319SNick Hibma.Ft report_desc_t
60b425e319SNick Hibma.Fn hid_use_report_desc "unsigned char *data" "unsigned int size"
619e2046dfSNick Hibma.Ft void
629e2046dfSNick Hibma.Fn hid_dispose_report_desc "report_desc_t d"
639e2046dfSNick Hibma.Ft hid_data_t
64cf0e07e5SMatthew N. Dodd.Fn hid_start_parse "report_desc_t d" "int kindset" "int id"
659e2046dfSNick Hibma.Ft void
669e2046dfSNick Hibma.Fn hid_end_parse "hid_data_t s"
679e2046dfSNick Hibma.Ft int
689e2046dfSNick Hibma.Fn hid_get_item "hid_data_t s" "hid_item_t *h"
699e2046dfSNick Hibma.Ft int
70cf0e07e5SMatthew N. Dodd.Fn hid_report_size "report_desc_t d" "hid_kind_t k" "int id"
719e2046dfSNick Hibma.Ft int
72cf0e07e5SMatthew N. Dodd.Fn hid_locate "report_desc_t d" "u_int usage" "hid_kind_t k" "hid_item_t *h" "int id"
734226a8bfSRuslan Ermilov.Ft "const char *"
749e2046dfSNick Hibma.Fn hid_usage_page "int i"
754226a8bfSRuslan Ermilov.Ft "const char *"
76cf0e07e5SMatthew N. Dodd.Fn hid_usage_in_page "u_int u"
77b425e319SNick Hibma.Ft int
78cf0e07e5SMatthew N. Dodd.Fn hid_parse_usage_page "const char *"
794226a8bfSRuslan Ermilov.Ft int
80cf0e07e5SMatthew N. Dodd.Fn hid_parse_usage_in_page "const char *"
819e2046dfSNick Hibma.Ft void
824226a8bfSRuslan Ermilov.Fn hid_init "const char *file"
831a7d8d06SBruce Evans.Ft int
844226a8bfSRuslan Ermilov.Fn hid_get_data "const void *data" "const hid_item_t *h"
851a7d8d06SBruce Evans.Ft void
864226a8bfSRuslan Ermilov.Fn hid_set_data "void *buf" "const hid_item_t *h" "int data"
877778ab7eSAlexander Motin.Ft int
887778ab7eSAlexander Motin.Fn hid_get_report "int fd" "enum hid_kind k" "unsigned char *data" "unsigned int size"
897778ab7eSAlexander Motin.Ft int
907778ab7eSAlexander Motin.Fn hid_set_report "int fd" "enum hid_kind k" "unsigned char *data" "unsigned int size"
919e2046dfSNick Hibma.Sh DESCRIPTION
929e2046dfSNick HibmaThe
939e2046dfSNick Hibma.Nm
949e2046dfSNick Hibmalibrary provides routines to extract data from USB Human Interface Devices.
953a5146d9SRuslan Ermilov.Ss Introduction
96*9b15c51bSGordon BerglingUSB HID devices send and receive data laid out in a device dependent way.
97af41ab4cSRuslan ErmilovThe
989e2046dfSNick Hibma.Nm
999e2046dfSNick Hibmalibrary contains routines to extract the
100af41ab4cSRuslan Ermilov.Em "report descriptor"
1019e2046dfSNick Hibmawhich contains the data layout information and then use this information.
1029e2046dfSNick Hibma.Pp
1039e2046dfSNick HibmaThe routines can be divided into four parts: extraction of the descriptor,
1049e2046dfSNick Hibmaparsing of the descriptor, translating to/from symbolic names, and
1059e2046dfSNick Hibmadata manipulation.
1067e474656SAlfred Perlstein.Ss Synchronous HID operation
1077e474656SAlfred PerlsteinSynchronous HID operation can be enabled or disabled by a call to
1087e474656SAlfred Perlstein.Fn hid_set_immed .
1097e474656SAlfred PerlsteinIf the second argument is zero synchronous HID operation is disabled.
1107e474656SAlfred PerlsteinElse synchronous HID operation is enabled.
1117e474656SAlfred PerlsteinThe function returns a negative value on failure.
1127778ab7eSAlexander Motin.Pp
1137778ab7eSAlexander Motin.Fn hid_get_report
1147778ab7eSAlexander Motinand
1157778ab7eSAlexander Motin.Fn hid_set_report
1167778ab7eSAlexander Motinfunctions allow to synchronously get and set specific report if device
1177778ab7eSAlexander Motinsupports it.
1187778ab7eSAlexander MotinFor devices with multiple report IDs, wanted ID should be provided in the
1197778ab7eSAlexander Motinfirst byte of the buffer for both get and set.
1203a5146d9SRuslan Ermilov.Ss Descriptor Functions
1217e474656SAlfred PerlsteinThe report descriptor ID can be obtained by calling
1227e474656SAlfred Perlstein.Fn hid_get_report_id .
1239e2046dfSNick HibmaA report descriptor can be obtained by calling
1249e2046dfSNick Hibma.Fn hid_get_report_desc
1259e2046dfSNick Hibmawith a file descriptor obtained by opening a
1269e2046dfSNick Hibma.Xr uhid 4
127af41ab4cSRuslan Ermilovdevice.
128af41ab4cSRuslan ErmilovAlternatively a data buffer containing the report descriptor can be
129b425e319SNick Hibmapassed into
130b425e319SNick Hibma.Fn hid_use_report_desc .
131af41ab4cSRuslan ErmilovThe data is copied into an internal structure.
132af41ab4cSRuslan ErmilovWhen the report descriptor
133b425e319SNick Hibmais no longer needed it should be freed by calling
1349e2046dfSNick Hibma.Fn hid_dispose_report_desc .
1359e2046dfSNick HibmaThe type
136af41ab4cSRuslan Ermilov.Vt report_desc_t
1379e2046dfSNick Hibmais opaque and should be used when calling the parsing functions.
138b425e319SNick HibmaIf
139b425e319SNick Hibma.Fn hid_dispose_report_desc
140b425e319SNick Hibmafails it will return
141af41ab4cSRuslan Ermilov.Dv NULL .
1423a5146d9SRuslan Ermilov.Ss Descriptor Parsing Functions
1439e2046dfSNick HibmaTo parse the report descriptor the
1449e2046dfSNick Hibma.Fn hid_start_parse
145ec9b9fbdSAlexander Motinfunction should be called with a report descriptor, a set that
146ec9b9fbdSAlexander Motindescribes which items that are interesting, and the desired report
147ec9b9fbdSAlexander MotinID (or -1 to obtain items of all report IDs).
148cf0e07e5SMatthew N. DoddThe set is obtained by OR-ing together values
1499e2046dfSNick Hibma.Fa "(1 << k)"
1509e2046dfSNick Hibmawhere
1519e2046dfSNick Hibma.Fa k
1529e2046dfSNick Hibmais an item of type
153af41ab4cSRuslan Ermilov.Vt hid_kind_t .
1549e2046dfSNick HibmaThe function returns
155af41ab4cSRuslan Ermilov.Dv NULL
1569e2046dfSNick Hibmaif the initialization fails, otherwise an opaque value to be used
1579e2046dfSNick Hibmain subsequent calls.
1589e2046dfSNick HibmaAfter parsing the
1599e2046dfSNick Hibma.Fn hid_end_parse
1609e2046dfSNick Hibmafunction should be called to free internal data structures.
1619e2046dfSNick Hibma.Pp
1629e2046dfSNick HibmaTo iterate through all the items in the report descriptor
1639e2046dfSNick Hibma.Fn hid_get_item
1649e2046dfSNick Hibmashould be called while it returns a value greater than 0.
1659e2046dfSNick HibmaWhen the report descriptor ends it will returns 0; a syntax
1669e2046dfSNick Hibmaerror within the report descriptor will cause a return value less
1679e2046dfSNick Hibmathan 0.
1689e2046dfSNick HibmaThe struct pointed to by
1699e2046dfSNick Hibma.Fa h
1709e2046dfSNick Hibmawill be filled with the relevant data for the item.
1719e2046dfSNick HibmaThe definition of
172af41ab4cSRuslan Ermilov.Vt hid_item_t
1739e2046dfSNick Hibmacan be found in
174fe08efe6SRuslan Ermilov.In usbhid.h
1759e2046dfSNick Hibmaand the meaning of the components in the USB HID documentation.
1769e2046dfSNick Hibma.Pp
1779e2046dfSNick HibmaData should be read/written to the device in the size of
178af41ab4cSRuslan Ermilovthe report.
179cf0e07e5SMatthew N. DoddThe size of a report (of a certain kind) can be computed by the
1809e2046dfSNick Hibma.Fn hid_report_size
181b425e319SNick Hibmafunction.
182cf0e07e5SMatthew N. DoddIf the report is prefixed by an ID byte it is given by
183cf0e07e5SMatthew N. Dodd.Fa id .
1849e2046dfSNick Hibma.Pp
1859e2046dfSNick HibmaTo locate a single item the
1869e2046dfSNick Hibma.Fn hid_locate
187af41ab4cSRuslan Ermilovfunction can be used.
188af41ab4cSRuslan ErmilovIt should be given the usage code of
1899e2046dfSNick Hibmathe item and its kind and it will fill the item and return
1909e2046dfSNick Hibmanon-zero if the item was found.
1913a5146d9SRuslan Ermilov.Ss Name Translation Functions
1929e2046dfSNick HibmaThe function
1939e2046dfSNick Hibma.Fn hid_usage_page
1949e2046dfSNick Hibmawill return the symbolic name of a usage page, and the function
1959e2046dfSNick Hibma.Fn hid_usage_in_page
1969e2046dfSNick Hibmawill return the symbolic name of the usage within the page.
1979e2046dfSNick HibmaBoth these functions may return a pointer to static data.
198b425e319SNick Hibma.Pp
199b425e319SNick HibmaThe functions
200b425e319SNick Hibma.Fn hid_parse_usage_page
201b425e319SNick Hibmaand
202b425e319SNick Hibma.Fn hid_parse_usage_in_page
203b425e319SNick Hibmaare the inverses of
204b425e319SNick Hibma.Fn hid_usage_page
205b425e319SNick Hibmaand
206b425e319SNick Hibma.Fn hid_usage_in_page .
207af41ab4cSRuslan ErmilovThey take a usage string and return the number of the usage, or \-1
208b425e319SNick Hibmaif it cannot be found.
209b425e319SNick Hibma.Pp
210b425e319SNick HibmaBefore any of these functions can be called the usage table
2119e2046dfSNick Hibmamust be parsed, this is done by calling
2129e2046dfSNick Hibma.Fn hid_init
213af41ab4cSRuslan Ermilovwith the name of the table.
214af41ab4cSRuslan ErmilovPassing
215af41ab4cSRuslan Ermilov.Dv NULL
2169e2046dfSNick Hibmato this function will cause it to use the default table.
2173a5146d9SRuslan Ermilov.Ss Data Extraction Functions
2189e2046dfSNick HibmaGiven the data obtained from a HID device and an item in the
2199e2046dfSNick Hibmareport descriptor the
2209e2046dfSNick Hibma.Fn hid_get_data
2219e2046dfSNick Hibmafunction extracts the value of the item.
2229e2046dfSNick HibmaConversely
2239e2046dfSNick Hibma.Fn hid_set_data
2249e2046dfSNick Hibmacan be used to put data into a report (which must be zeroed first).
2259e2046dfSNick Hibma.Sh FILES
226af41ab4cSRuslan Ermilov.Bl -tag -width ".Pa /usr/share/misc/usb_hid_usages"
227af41ab4cSRuslan Ermilov.It Pa /usr/share/misc/usb_hid_usages
2289e2046dfSNick HibmaThe default HID usage table.
229af41ab4cSRuslan Ermilov.El
23024a0682cSRuslan Ermilov.Sh EXAMPLES
23124a0682cSRuslan ErmilovNot yet.
2329e2046dfSNick Hibma.Sh SEE ALSO
2339e2046dfSNick HibmaThe
2349e2046dfSNick Hibma.Tn USB
2359e2046dfSNick Hibmaspecifications can be found at
236c2e41e91SHideyuki KURASHINA.Pa http://www.usb.org/developers/docs/ .
2379e2046dfSNick Hibma.Pp
238265c01dfSRuslan Ermilov.Xr uhid 4 ,
239cb17760dSBen Smithurst.Xr usb 4
2409e2046dfSNick Hibma.Sh HISTORY
2419e2046dfSNick HibmaThe
2429e2046dfSNick Hibma.Nm
2439e2046dfSNick Hibmalibrary first appeared in
2449e2046dfSNick Hibma.Nx 1.5 .
24524a0682cSRuslan Ermilov.Sh BUGS
24624a0682cSRuslan ErmilovThis man page is woefully incomplete.
247