---
name: "usbhid"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/hid_dispose_report_desc"
---

# usbhid(3)

USB HID access routines

Part of [hid\_dispose\_report\_desc(3)](/hid_dispose_report_desc/3.md).

## NAME

**usbhid**, **hid\_get\_report\_desc**, **hid\_get\_report\_id**, **hid\_use\_report\_desc**, **hid\_dispose\_report\_desc**, **hid\_start\_parse**, **hid\_end\_parse**, **hid\_get\_item**, **hid\_report\_size**, **hid\_locate**, **hid\_usage\_page**, **hid\_usage\_in\_page**, **hid\_init**, **hid\_get\_data**, **hid\_set\_data**, **hid\_get\_report**, **hid\_set\_report** – USB HID access routines

## LIBRARY

library “libusbhid”

## SYNOPSIS

**#include \<usbhid.h\>**

*report\_desc\_t*\

**hid\_get\_report\_desc**(*int file*);

*int*\

**hid\_get\_report\_id**(*int file*);

*int*\

**hid\_set\_immed**(*int fd*, *int enable*);

*report\_desc\_t*\

**hid\_use\_report\_desc**(*unsigned char \*data*, *unsigned int size*);

*void*\

**hid\_dispose\_report\_desc**(*report\_desc\_t d*);

*hid\_data\_t*\

**hid\_start\_parse**(*report\_desc\_t d*, *int kindset*, *int id*);

*void*\

**hid\_end\_parse**(*hid\_data\_t s*);

*int*\

**hid\_get\_item**(*hid\_data\_t s*, *hid\_item\_t \*h*);

*int*\

**hid\_report\_size**(*report\_desc\_t d*, *hid\_kind\_t k*, *int id*);

*int*\

**hid\_locate**(*report\_desc\_t d*, *u\_int usage*, *hid\_kind\_t k*, *hid\_item\_t \*h*, *int id*);

*const char \**\

**hid\_usage\_page**(*int i*);

*const char \**\

**hid\_usage\_in\_page**(*u\_int u*);

*int*\

**hid\_parse\_usage\_page**(*const char \**);

*int*\

**hid\_parse\_usage\_in\_page**(*const char \**);

*void*\

**hid\_init**(*const char \*file*);

*int*\

**hid\_get\_data**(*const void \*data*, *const hid\_item\_t \*h*);

*void*\

**hid\_set\_data**(*void \*buf*, *const hid\_item\_t \*h*, *int data*);

*int*\

**hid\_get\_report**(*int fd*, *enum hid\_kind k*, *unsigned char \*data*, *unsigned int size*);

*int*\

**hid\_set\_report**(*int fd*, *enum hid\_kind k*, *unsigned char \*data*, *unsigned int size*);

## DESCRIPTION

The **usbhid** library provides routines to extract data from USB Human Interface Devices.

### Introduction

USB HID devices send and receive data laid out in a device dependent way. The **usbhid** library contains routines to extract the *report descriptor* which contains the data layout information and then use this information.

The routines can be divided into four parts: extraction of the descriptor, parsing of the descriptor, translating to/from symbolic names, and data manipulation.

### Synchronous HID operation

Synchronous HID operation can be enabled or disabled by a call to **hid\_set\_immed**(). If the second argument is zero synchronous HID operation is disabled. Else synchronous HID operation is enabled. The function returns a negative value on failure.

**hid\_get\_report**() and **hid\_set\_report**() functions allow to synchronously get and set specific report if device supports it. For devices with multiple report IDs, wanted ID should be provided in the first byte of the buffer for both get and set.

### Descriptor Functions

The report descriptor ID can be obtained by calling **hid\_get\_report\_id**(). A report descriptor can be obtained by calling **hid\_get\_report\_desc**() with a file descriptor obtained by opening a [uhid(4)](/uhid/4.md) device. Alternatively a data buffer containing the report descriptor can be passed into **hid\_use\_report\_desc**(). The data is copied into an internal structure. When the report descriptor is no longer needed it should be freed by calling **hid\_dispose\_report\_desc**(). The type *report\_desc\_t* is opaque and should be used when calling the parsing functions. If **hid\_dispose\_report\_desc**() fails it will return NULL.

### Descriptor Parsing Functions

To parse the report descriptor the **hid\_start\_parse**() function should be called with a report descriptor, a set that describes which items that are interesting, and the desired report ID (or -1 to obtain items of all report IDs). The set is obtained by OR-ing together values *(1 \<\< k)* where *k* is an item of type *hid\_kind\_t*. The function returns NULL if the initialization fails, otherwise an opaque value to be used in subsequent calls. After parsing the **hid\_end\_parse**() function should be called to free internal data structures.

To iterate through all the items in the report descriptor **hid\_get\_item**() should be called while it returns a value greater than 0. When the report descriptor ends it will returns 0; a syntax error within the report descriptor will cause a return value less than 0. The struct pointed to by *h* will be filled with the relevant data for the item. The definition of *hid\_item\_t* can be found in ⟨usbhid.h⟩ and the meaning of the components in the USB HID documentation.

Data should be read/written to the device in the size of the report. The size of a report (of a certain kind) can be computed by the **hid\_report\_size**() function. If the report is prefixed by an ID byte it is given by *id*.

To locate a single item the **hid\_locate**() function can be used. It should be given the usage code of the item and its kind and it will fill the item and return non-zero if the item was found.

### Name Translation Functions

The function **hid\_usage\_page**() will return the symbolic name of a usage page, and the function **hid\_usage\_in\_page**() will return the symbolic name of the usage within the page. Both these functions may return a pointer to static data.

The functions **hid\_parse\_usage\_page**() and **hid\_parse\_usage\_in\_page**() are the inverses of **hid\_usage\_page**() and **hid\_usage\_in\_page**(). They take a usage string and return the number of the usage, or -1 if it cannot be found.

Before any of these functions can be called the usage table must be parsed, this is done by calling **hid\_init**() with the name of the table. Passing NULL to this function will cause it to use the default table.

### Data Extraction Functions

Given the data obtained from a HID device and an item in the report descriptor the **hid\_get\_data**() function extracts the value of the item. Conversely **hid\_set\_data**() can be used to put data into a report (which must be zeroed first).

## FILES

- */usr/share/misc/usb\_hid\_usages*

  The default HID usage table.

## EXAMPLES

Not yet.

## SEE ALSO

The USB specifications can be found at *http://www.usb.org/developers/docs/*.

[uhid(4)](/uhid/4.md), [usb(4)](/usb/4.md)

## HISTORY

The **usbhid** library first appeared in NetBSD 1.5.

## BUGS

This man page is woefully incomplete.
