---
name: "usbd_abort_pipe"
section: "9freebsd"
platform: "linux"
package: "freebsd-manpages"
version: "7.0-2"
release: "Debian 5.0 (Lenny)"
platforms: ["linux"]
url: "https://new.carta.tech/usbd_abort_pipe"
---

# usbd\_abort\_pipe(9freebsd)

Universal Serial Bus driver programming interface

## NAME

**usb\_detach\_wait**, **usb\_detach\_wakeup**, **usb\_find\_desc**, **usbd\_abort\_default\_pipe**, **usbd\_abort\_pipe**, **usbd\_alloc\_buffer**, **usbd\_alloc\_xfer**, **usbd\_bulk\_transfer**, **usbd\_clear\_endpoint\_stall**, **usbd\_clear\_endpoint\_stall\_async**, **usbd\_clear\_endpoint\_toggle**, **usbd\_close\_pipe**, **usbd\_device2interface\_handle**, **usbd\_devinfo**, **usbd\_do\_request**, **usbd\_do\_request\_async**, **usbd\_do\_request\_flags**, **usbd\_do\_request\_flags\_pipe**, **usbd\_dopoll**, **usbd\_endpoint\_count**, **usbd\_errstr**, **usbd\_fill\_deviceinfo**, **usbd\_find\_edesc**, **usbd\_find\_idesc**, **usbd\_free\_buffer**, **usbd\_free\_xfer**, **usbd\_get\_buffer**, **usbd\_get\_config**, **usbd\_get\_config\_desc**, **usbd\_get\_config\_desc\_full**, **usbd\_get\_config\_descriptor**, **usbd\_get\_device\_descriptor**, **usbd\_get\_endpoint\_descriptor**, **usbd\_get\_interface\_altindex**, **usbd\_get\_interface\_descriptor**, **usbd\_get\_no\_alts**, **usbd\_get\_quirks**, **usbd\_get\_speed**, **usbd\_get\_string**, **usbd\_get\_string\_desc**, **usbd\_get\_xfer\_status**, **usbd\_interface2device\_handle**, **usbd\_interface2endpoint\_descriptor**, **usbd\_interface\_count**, **usbd\_intr\_transfer**, **usbd\_open\_pipe**, **usbd\_open\_pipe\_intr**, **usbd\_pipe2device\_handle**, **usbd\_ratecheck**, **usbd\_set\_config\_index**, **usbd\_set\_config\_no**, **usbd\_set\_interface**, **usbd\_set\_polling**, **usbd\_setup\_default\_xfer**, **usbd\_setup\_isoc\_xfer**, **usbd\_setup\_xfer**, **usbd\_sync\_transfer**, **usbd\_transfer** – Universal Serial Bus driver programming interface

## SYNOPSIS

**#include \<dev/usb/usb.h\>**

**#include \<dev/usb/usbdi.h\>**

**#include \<dev/usb/usbdi\_util.h\>**

*void*\

**usb\_detach\_wait**(*device\_ptr\_t dv*);

*void*\

**usb\_detach\_wakeup**(*device\_ptr\_t dv*);

*const usb\_descriptor\_t \**\

**usb\_find\_desc**(*usbd\_device\_handle dev*, *int type*, *int subtype*);

*usbd\_status*\

**usbd\_abort\_default\_pipe**(*usbd\_device\_handle dev*);

*usbd\_status*\

**usbd\_abort\_pipe**(*usbd\_pipe\_handle pipe*);

*void \**\

**usbd\_alloc\_buffer**(*usbd\_xfer\_handle xfer*, *u\_int32\_t size*);

*usbd\_xfer\_handle*\

**usbd\_alloc\_xfer**(*usbd\_device\_handle dev*);

*usbd\_status*\

**usbd\_bulk\_transfer**(*usbd\_xfer\_handle xfer*, *usbd\_pipe\_handle pipe*, *u\_int16\_t flags*, *u\_int32\_t timeout*, *void \*buf*, *u\_int32\_t \*size*, *char \*lbl*);

*usbd\_status*\

**usbd\_clear\_endpoint\_stall**(*usbd\_pipe\_handle pipe*);

*usbd\_status*\

**usbd\_clear\_endpoint\_stall\_async**(*usbd\_pipe\_handle*);

*void*\

**usbd\_clear\_endpoint\_toggle**(*usbd\_pipe\_handle pipe*);

*usbd\_status*\

**usbd\_close\_pipe**(*usbd\_pipe\_handle pipe*);

*usbd\_status*\

**usbd\_device2interface\_handle**(*usbd\_device\_handle dev*, *u\_int8\_t ifaceno*, *usbd\_interface\_handle \*iface*);

*void*\

**usbd\_devinfo**(*usbd\_device\_handle dev*, *int showclass*, *char \*cp*);

*usbd\_status*\

**usbd\_do\_request**(*usbd\_device\_handle dev*, *usb\_device\_request\_t \*req*, *void \*data*);

*usbd\_status*\

**usbd\_do\_request\_async**(*usbd\_device\_handle dev*, *usb\_device\_request\_t \*req*, *void \*data*);

*usbd\_status*\

**usbd\_do\_request\_flags**(*usbd\_device\_handle dev*, *usb\_device\_request\_t \*req*, *void \*data*, *u\_int16\_t flags*, *int \*actlen*, *u\_int32\_t timo*);

*usbd\_status*\

**usbd\_do\_request\_flags\_pipe**(*usbd\_device\_handle dev*, *usbd\_pipe\_handle pipe*, *usb\_device\_request\_t \*req*, *void \*data*, *u\_int16\_t flags*, *int \*actlen*, *u\_int32\_t timeout*);

*void*\

**usbd\_dopoll**(*usbd\_interface\_handle iface*);

*usbd\_status*\

**usbd\_endpoint\_count**(*usbd\_interface\_handle iface*, *u\_int8\_t \*count*);

*const char \**\

**usbd\_errstr**(*usbd\_status err*);

*void*\

**usbd\_fill\_deviceinfo**(*usbd\_device\_handle dev*, *struct usb\_device\_info \*di*, *int usedev*);

*usb\_endpoint\_descriptor\_t \**\

**usbd\_find\_edesc**(*usb\_config\_descriptor\_t \*cd*, *int ifaceidx*, *int altidx*, *int endptidx*);

*usb\_interface\_descriptor\_t \**\

**usbd\_find\_idesc**(*usb\_config\_descriptor\_t \*cd*, *int ifaceidx*, *int altidx*);

*void*\

**usbd\_free\_buffer**(*usbd\_xfer\_handle xfer*);

*usbd\_status*\

**usbd\_free\_xfer**(*usbd\_xfer\_handle xfer*);

*void \**\

**usbd\_get\_buffer**(*usbd\_xfer\_handle xfer*);

*usbd\_status*\

**usbd\_get\_config**(*usbd\_device\_handle dev*, *u\_int8\_t \*conf*);

*usbd\_status*\

**usbd\_get\_config\_desc**(*usbd\_device\_handle dev*, *int confidx*, *usb\_config\_descriptor\_t \*d*);

*usbd\_status*\

**usbd\_get\_config\_desc\_full**(*usbd\_device\_handle dev*, *int conf*, *void \*d*, *int size*);

*usb\_config\_descriptor\_t \**\

**usbd\_get\_config\_descriptor**(*usbd\_device\_handle dev*);

*usb\_device\_descriptor\_t \**\

**usbd\_get\_device\_descriptor**(*usbd\_device\_handle dev*);

*usb\_endpoint\_descriptor\_t \**\

**usbd\_get\_endpoint\_descriptor**(*usbd\_interface\_handle iface*, *u\_int8\_t address*);

*int*\

**usbd\_get\_interface\_altindex**(*usbd\_interface\_handle iface*);

*usb\_interface\_descriptor\_t \**\

**usbd\_get\_interface\_descriptor**(*usbd\_interface\_handle iface*);

*int*\

**usbd\_get\_no\_alts**(*usb\_config\_descriptor\_t \*cdesc*, *int ifaceno*);

*const struct usbd\_quirks \**\

**usbd\_get\_quirks**(*usbd\_device\_handle dev*);

*int*\

**usbd\_get\_speed**(*usbd\_device\_handle dev*);

*usbd\_status*\

**usbd\_get\_string**(*usbd\_device\_handle dev*, *int si*, *char \*buf*);

*usbd\_status*\

**usbd\_get\_string\_desc**(*usbd\_device\_handle dev*, *int sindex*, *int langid*, *usb\_string\_descriptor\_t \*sdesc*, *int \*sizep*);

*void*\

**usbd\_get\_xfer\_status**(*usbd\_xfer\_handle xfer*, *usbd\_private\_handle \*priv*, *void \*\*buffer*, *u\_int32\_t \*count*, *usbd\_status \*status*);

*void*\

**usbd\_interface2device\_handle**(*usbd\_interface\_handle iface*, *usbd\_device\_handle \*dev*);

*usb\_endpoint\_descriptor\_t \**\

**usbd\_interface2endpoint\_descriptor**(*usbd\_interface\_handle iface*, *u\_int8\_t index*);

*usbd\_status*\

**usbd\_interface\_count**(*usbd\_device\_handle dev*, *u\_int8\_t \*count*);

*usbd\_status*\

**usbd\_intr\_transfer**(*usbd\_xfer\_handle xfer*, *usbd\_pipe\_handle pipe*, *u\_int16\_t flags*, *u\_int32\_t timeout*, *void \*buf*, *u\_int32\_t \*size*, *char \*lbl*);

*usbd\_status*\

**usbd\_open\_pipe**(*usbd\_interface\_handle iface*, *u\_int8\_t address*, *u\_int8\_t flags*, *usbd\_pipe\_handle \*pipe*);

*usbd\_status*\

**usbd\_open\_pipe\_intr**(*usbd\_interface\_handle iface*, *u\_int8\_t address*, *u\_int8\_t flags*, *usbd\_pipe\_handle \*pipe*, *usbd\_private\_handle priv*, *void \*buffer*, *u\_int32\_t len*, *usbd\_callback cb*, *int ival*);

*usbd\_device\_handle*\

**usbd\_pipe2device\_handle**(*usbd\_pipe\_handle pipe*);

*int*\

**usbd\_ratecheck**(*struct timeval \*last*);

*usbd\_status*\

**usbd\_set\_config\_index**(*usbd\_device\_handle dev*, *int index*, *int msg*);

*usbd\_status*\

**usbd\_set\_config\_no**(*usbd\_device\_handle dev*, *int no*, *int msg*);

*usbd\_status*\

**usbd\_set\_interface**(*usbd\_interface\_handle iface*, *int altidx*);

*void*\

**usbd\_set\_polling**(*usbd\_device\_handle dev*, *int on*);

*void*\

**usbd\_setup\_default\_xfer**(*usbd\_xfer\_handle xfer*, *usbd\_device\_handle dev*, *usbd\_private\_handle priv*, *u\_int32\_t timeout*, *usb\_device\_request\_t \*req*, *void \*buffer*, *u\_int32\_t length*, *u\_int16\_t flags*, *usbd\_callback callback*);

*void*\

**usbd\_setup\_isoc\_xfer**(*usbd\_xfer\_handle xfer*, *usbd\_pipe\_handle pipe*, *usbd\_private\_handle priv*, *u\_int16\_t \*frlengths*, *u\_int32\_t nframes*, *u\_int16\_t flags*, *usbd\_callback callback*);

*void*\

**usbd\_setup\_xfer**(*usbd\_xfer\_handle xfer*, *usbd\_pipe\_handle pipe*, *usbd\_private\_handle priv*, *void \*buffer*, *u\_int32\_t length*, *u\_int16\_t flags*, *u\_int32\_t timeout*, *usbd\_callback callback*);

*usbd\_status*\

**usbd\_sync\_transfer**(*usbd\_xfer\_handle xfer*);

*usbd\_status*\

**usbd\_transfer**(*usbd\_xfer\_handle xfer*);

## DESCRIPTION

The Universal Serial Bus (USB) driver programming interface provides USB peripheral drivers with a host controller independent API for controlling and communicating with USB peripherals.

Typically, drivers will first use some combination of the functions **usbd\_set\_config\_no**(), **usbd\_get\_config\_descriptor**(), **usbd\_set\_interface**(), **usbd\_get\_interface\_descriptor**(), **usbd\_device2interface\_handle**(), **usbd\_endpoint\_count**() and **usbd\_interface2endpoint\_descriptor**() to query the device's properties and prepare it for use. Drivers can then perform requests on the USB control pipe using **usbd\_do\_request**(), they can open pipes using the functions **usbd\_open\_pipe**() and **usbd\_open\_pipe\_intr**(), and perform transfers over these pipes using **usbd\_alloc\_xfer**(), **usbd\_setup\_xfer**() and **usbd\_transfer**(). Finally, the functions **usbd\_abort\_pipe**(), **usbd\_close\_pipe**() and **usbd\_free\_xfer**() are used to cancel outstanding transfers, close open pipes and deallocate transfer structures.

The **usbd\_get\_device\_descriptor**() function returns a pointer to the USB device descriptor for *dev*. See USB Descriptors below for information about the USB device descriptor.

The **usbd\_get\_config\_desc**() function retrieves the specified configuration descriptor from the device. The *confidx* parameter specifies the configuration descriptor index, which must be less than the *bNumConfigurations* value in the device descriptor. The function **usbd\_get\_config\_desc\_full**() retrieves a full configuration descriptor, which has all related interface and endpoint descriptors appended to a normal configuration descriptor. The parameter *d* should point to memory that is at least *size* bytes in length, and this should be at least as long as the *wTotalLength* value from the configuration descriptor. See USB Descriptors below for information about the USB configuration descriptor.

The **usbd\_get\_config**() function retrieves the current configuration number from the device, i.e. the *bConfigurationValue* value from the configuration that is active. If the device is unconfigured then USB\_UNCONFIG\_NO is returned. The current configuration can be changed by calling either **usbd\_set\_config\_index**() or **usbd\_set\_config\_no**(). The difference between these functions is that **usbd\_set\_config\_index**() accepts a configuration index number that is less than the *bNumConfigurations* value from the device descriptor, whereas **usbd\_set\_config\_no**() requires the *bConfigurationValue* value of the desired configuration to be provided instead. To unconfigure the device, supply a configuration index of USB\_UNCONFIG\_INDEX to **usbd\_set\_config\_index**(), or else specify a configuration number of USB\_UNCONFIG\_NO to **usbd\_set\_config\_no**().

The **usbd\_get\_config\_descriptor**() function returns a pointer to an in-memory copy of the full configuration descriptor of the configuration that is currently active. The returned pointer remains valid until the device configuration is changed using **usbd\_set\_config\_index**() or **usbd\_set\_config\_no**(). If the device is unconfigured then NULL is returned instead.

The function **usbd\_interface\_count**() returns the number of interfaces available in the current device configuration. The **usbd\_get\_no\_alts**() function determines the number of alternate interfaces in a full configuration descriptor by counting the interface descriptors with *bInterfaceNumber* equal to *ifaceno* (the count includes alternate index zero). The **usbd\_find\_idesc**() function locates an interface descriptor within a full configuration descriptor. The *ifaceidx* parameter specifies the interface index number, which should be less than the number of interfaces in the configuration descriptor (i.e. the value returned by **usbd\_interface\_count**() or the *bNumInterface* field from the configuration descriptor). An alternate interface can be specified using a non-zero *altidx*, which should be less than the value returned by **usbd\_get\_no\_alts**(). The return value is a pointer to the requested interface descriptor within the full configuration descriptor, or NULL if the specified interface descriptor does not exist. Note that the *altidx* parameter specifies the alternate setting by index number starting at zero; it is not the alternate setting number defined in the interface descriptor.

The function **usbd\_find\_edesc**() locates an endpoint descriptor within a full configuration descriptor. The *ifaceidx* and *altidx* parameters are the same as described for **usbd\_find\_idesc**(), and the *endptidx* parameter is an endpoint index number that should be less than the *bNumEndpoints* field in the interface descriptor. The return value is a pointer to the requested endpoint descriptor within the full configuration descriptor, or NULL if the specified endpoint descriptor does not exist. Note that the *altidx* and *endptidx* parameters are index numbers starting at zero; they are not the alternate setting and endpoint address defined in the descriptors.

The **usbd\_get\_speed**() function returns the device speed. This can be USB\_SPEED\_LOW, USB\_SPEED\_FULL or USB\_SPEED\_HIGH.

USB devices optionally support string descriptors, which can be retrieved using the **usbd\_get\_string**() or **usbd\_get\_string\_desc**() functions. Device, configuration and interface descriptors reference strings by an index number that can be supplied to these functions. The **usbd\_get\_string**() function should be used unless a non-default language is required. It requires that *buf* points to a buffer of at least USB\_MAX\_STRING\_LEN bytes in size. The *si* parameter specified which string to retrieve.

The **usb\_find\_desc**() function searches through the in-memory full configuration descriptor for the active configuration and finds the first descriptor that has a *bDescriptorType* equal to *type*, and if *subtype* is not equal to USBD\_SUBTYPE\_ANY, the descriptor must also have a *bDescriptorSubtype* equal to *subtype*. If found, then a pointer to the descriptor is returned. Otherwise, **usb\_find\_desc**() returns NULL. The returned pointer is valid until the device configuration is changed using **usbd\_set\_config\_index**() or **usbd\_set\_config\_no**().

The USB driver interface uses opaque interface handles to refer to configuration interfaces. These handles remain valid until the device configuration is changed using **usbd\_set\_config\_index**() or **usbd\_set\_config\_no**(). The **usbd\_device2interface\_handle**() function retrieves an interface handle. The *ifaceno* parameter is an interface index number starting at zero. If the device is configured and the specified interface exists, then USBD\_NORMAL\_COMPLETION is returned and the interface handle is stored in *\*iface*. Otherwise an error code is returned and *\*iface* is not changed. The **usbd\_interface2device\_handle**() function retrieves the device handle from an interface handle. This is just for convenience to save passing around the device handle as well as the interface handle. The **usbd\_set\_interface**() function changes the alternate setting number for an interface to the alternate setting identified by the zero-based index number *altidx*. This operation invalidates any existing endpoints on this interface and their descriptors. The **usbd\_get\_interface\_altindex**() function returns the current alternative setting index as was specified when calling **usbd\_set\_interface**(). The **usbd\_endpoint\_count**() function retrieves the number of endpoints associated with the specified interface. The **usbd\_interface2endpoint\_descriptor**() function returns a pointer to an in-memory endpoint descriptor for the endpoint that has an index number of *index*. This pointer remains valid until the configuration or alternate setting number are changed. The function **usbd\_get\_endpoint\_descriptor**() is like **usbd\_interface2endpoint\_descriptor**() but it accepts a *bEndpointAddress* address value instead of an index.

The **usbd\_fill\_deviceinfo**() function fills out a *usb\_device\_info* structure with information about the device. The vendor and product names come from the device itself, falling back to a table lookup or just providing the IDs in hexadecimal. If *usedev* is zero then **usbd\_fill\_deviceinfo**() will not attempt to retrieve the vendor and product names from the device. The usb\_device\_info structure is defined in ⟨dev/usb/usb.h⟩ as follows:

```
struct usb_device_info {
	u_int8_t	udi_bus;
	u_int8_t	udi_addr;	/* device address */
	usb_event_cookie_t udi_cookie;
	char		udi_product[USB_MAX_STRING_LEN];
	char		udi_vendor[USB_MAX_STRING_LEN];
	char		udi_release[8];
	u_int16_t	udi_productNo;
	u_int16_t	udi_vendorNo;
	u_int16_t	udi_releaseNo;
	u_int8_t	udi_class;
	u_int8_t	udi_subclass;
	u_int8_t	udi_protocol;
	u_int8_t	udi_config;
	u_int8_t	udi_speed;
#define USB_SPEED_LOW  1
#define USB_SPEED_FULL 2
#define USB_SPEED_HIGH 3
	int		udi_power;	/* power consumption in mA */
	int		udi_nports;
	char		udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
	/* hub only: addresses of devices on ports */
	u_int8_t	udi_ports[16];
#define USB_PORT_ENABLED 0xff
#define USB_PORT_SUSPENDED 0xfe
#define USB_PORT_POWERED 0xfd
}
```

The **usbd\_devinfo**() function generates a string description of the USB device. The *cp* argument should point to a 1024-byte buffer (XXX the maximum length is approximately 320 chars, but there is no sanity checking and everything uses 1024-character buffers). Device class information is included if the *showclass* parameter is non-zero.

The **usbd\_get\_quirks**() function returns information from a table of devices that require special workarounds in order to function correctly. The returned structure is defined in ⟨dev/usb/usb\_quirks.h⟩ as follows:

```
struct usbd_quirks {
	u_int32_t uq_flags;	/* Device problems */
};
```

See ⟨dev/usb/usb\_quirks.h⟩ for a list of all currently defined quirks.

USB control requests are performed via *usb\_device\_request\_t* structures, defined in ⟨dev/usb/usb.h⟩ as follows:

```
typedef struct {
	uByte		bmRequestType;
	uByte		bRequest;
	uWord		wValue;
	uWord		wIndex;
	uWord		wLength;
} UPACKED usb_device_request_t;
```

The **usbd\_do\_request**() function performs a single request synchronously. The *req* parameter should point to a properly initialized *usb\_device\_request\_t*, and when the *wLength* field is non-zero, *data* should point at a buffer that is at least *wLength* bytes in length. The request timeout is set to 5 seconds, so the operation will fail with USBD\_TIMEOUT if the device does not respond within that time. The **usbd\_do\_request\_async**() function is like **usbd\_do\_request**(), but it does not wait for the request to complete before returning. This routine does not block so it can be used from contexts where sleeping is not allowed. Note that there is no notification mechanism to report when the operation completed nor is there a way to determine whether the request succeeded, so this function is of limited use. See **usbd\_setup\_default\_xfer**() and **usbd\_transfer**() for a way to invoke an asynchronous callback upon completion of a control request. The **usbd\_do\_request\_flags**() function is like **usbd\_do\_request**(), but additional flags can be specified, the timeout is configurable, and the actual number of bytes transferred is made available to the caller. The **usbd\_do\_request\_flags\_pipe**() function uses a specified pipe instead of the default pipe.

The function **usbd\_open\_pipe**() creates a pipe connected to a specified endpoint on a specified interface. The parameter *address* should be the *bEndpointAddress* value from one of this interface's endpoint descriptors. If *flags* contains USBD\_EXCLUSIVE\_USE then the operation will only succeed if there are no open pipes already connected to the specified endpoint. The **usbd\_open\_pipe\_intr**() function creates an interrupt pipe connected to the specified endpoint. The parameter *address* should be the *bEndpointAddress* value from one of this interface's endpoint descriptors. The *flags* parameter is passed to **usbd\_setup\_xfer**(). The *buffer* and *len* parameters define a buffer that is to be used for the interrupt transfers. The callback to be invoked each time a transfer completes is specified by *cb*, and *priv* is an argument to be passed to the callback function. The *ival* parameter specifies the maximum acceptable interval between transfers; in practice the transfers may occur more frequently. The function **usbd\_pipe2device\_handle**() returns the device associated with the specified *pipe*.

The **usbd\_abort\_pipe**() function aborts all active or waiting transfers on the specified pipe. Each transfer is aborted with a USBD\_CANCELLED status; callback routines must detect this error code to ensure that they do not attempt to initiate a new transfer in response to one being aborted. This routine blocks while it is waiting for the hardware to complete processing of aborted transfers, so it is only safe to call it in contexts where sleeping is allowed. The function **usbd\_abort\_default\_pipe**() aborts all active or waiting transfers on the default pipe. Like **usbd\_abort\_pipe**(), it blocks waiting for the hardware processing to complete.

When a pipe has no active or waiting transfers, the pipe may be closed using the **usbd\_close\_pipe**() function. Once a pipe is closed, its pipe handle becomes invalid and may no longer be used.

USB transfer handles are allocated using the function **usbd\_alloc\_xfer**() and may be freed using **usbd\_free\_xfer**().

The function **usbd\_setup\_xfer**() initializes a transfer handle with the details of a transfer to or from a USB device. The *xfer* parameter specifies the transfer handle to initialize, *pipe* specifies the pipe on which the transfer is to take place, and *priv* is an argument that will be passed to callback function. The arguments *buffer* and *length* define the data buffer for the transfer. If *length* is zero then the *buffer* may be NULL. The *flags* parameter may contain the following flags:

- USBD\_NO\_COPY

  This is used in association with **usbd\_alloc\_buffer**() and **usbd\_free\_buffer**() to use a dedicated DMA-capable buffer for the transfer.

- USBD\_SYNCHRONOUS

  Wait for the transfer to compete in **usbd\_transfer**().

- USBD\_SHORT\_XFER\_OK

  Permit transfers shorter than the requested data length.

- USBD\_FORCE\_SHORT\_XFER

  Force a short transfer at the end of a write operation to let the device know that the transfer has ended.

The *timeout* parameter specifies a timeout for the transfer in milliseconds. A value of USBD\_NO\_TIMEOUT indicates that no timeout should be configured. The parameter *callback* specifies the function to call when the transfer completes. Note that **usbd\_setup\_xfer**() does not actually initiate the transfer. The **usbd\_setup\_default\_xfer**() initializes a control transfer for the default pipe. The *req* parameter should point at a completed *usb\_device\_request\_t* structure. The function *usbd\_setup\_isoc\_xfer* initializes a transfer for an isochronous pipe.

The function **usbd\_transfer**() initiates a transfer. Normally it returns USBD\_IN\_PROGRESS to indicate that the transfer has been queued. If the USB stack is operating in polling mode, or if the transfer is synchronous, then USBD\_NORMAL\_COMPLETION may be returned. Other return values indicate that the transfer could not be initiated due to an error. The **usbd\_sync\_transfer**() function executes a transfer synchronously. It will sleep waiting for the transfer to complete and then return the transfer status. Note that if the transfer has a callback routine, this will be invoked before **usbd\_sync\_transfer**() returns.

The **usbd\_intr\_transfer**() and **usbd\_bulk\_transfer**() functions set up a transfer and wait synchronously for it to complete but they allows signals to interrupt the wait. They returns USBD\_INTERRUPTED if the transfer was interrupted by a signal. XXX these two functions are identical apart from their names.

The function **usbd\_get\_xfer\_status**() retrieves various information from a completed transfer. If the *priv* parameter is not NULL then the callback private argument is stored in *\*priv*. If *buffer* is not NULL then the transfer buffer pointer is stored in *\*buffer*. The actual number of bytes transferred is stored in *\*count* if *count*, *is*, *not*, *NULL.* Finally, the transfer status is stored in *\*status* if *status* is not NULL.

The **usbd\_clear\_endpoint\_stall**() function clears an endpoint stall condition synchronously, i.e. it sleeps waiting for the stall clear request to complete. The function **usbd\_clear\_endpoint\_stall\_async**() performs the same function asynchronously, but it provides no way to determine when the request completed, or whether it was successful. The **usbd\_clear\_endpoint\_toggle**() function instructs the host controller driver to reset the toggle bit on a pipe. This is used when manually clearing an endpoint stall using a control pipe request, in order to ensure that the host controller driver and the USB device restart with the same toggle value.

Normally the USB subsystem maps and copies data to and from DMA-capable memory each time a transfer is performed. The function **usbd\_alloc\_buffer**() allocates a permanent DMA-capable buffer associated with the transfer to avoid this overhead. The return value is the virtual address of the buffer. Any time that **usbd\_setup\_xfer**() is called on the transfer with the USBD\_NO\_COPY flag enabled, the allocated buffer will be used directly and the *buffer* argument passed to **usbd\_setup\_xfer**() will be ignored. The **usbd\_get\_buffer**() function returns a pointer to the virtual address of a buffer previously allocated by **usbd\_alloc\_buffer**(). Finally, **usbd\_free\_buffer**() deallocates the buffer.

The **usbd\_errstr**() function converts a status code into a string for display.

The function **usbd\_set\_polling**() enables or disables polling mode. In polling mode, all operations will busy-wait for the device to respond, so its use is effectively limited to boot time and kernel debuggers. It is important to match up calls that enable and disable polling mode, because the implementation just increments a polling reference count when *on* is non-zero and decrements it when *on* is zero. The **usbd\_dopoll**() causes the host controller driver to poll for any activity. This should only be used when polling mode is enabled.

The **usbd\_ratecheck**() function is used to limit the rate at which error messages are printed to approximately once per second. The *last* argument should point at a persistent *struct timeval*. A value of 1 will be returned if a message should be printed, but if **usbd\_ratecheck**() has already been called with the same *struct timeval* parameter in the last second then 0 is returned and the error message should be suppressed.

The functions **usb\_detach\_wait**() and **usb\_detach\_wakeup**() are used to wait for references to drain before completing the detachment of a device. The **usb\_detach\_wait**() function will wait up to 60 seconds to receive a signal from **usb\_detach\_wait**().

### USB Descriptors

The USB specification defines a number of standard descriptors by which USB devices report their attributes. These descriptors are fixed-format structures that all USB devices make available through USB control pipe requests.

Every USB device has exactly one USB device descriptor. The USB subsystem retrieves this automatically when a device is attached, and a copy of the descriptor is kept in memory. The **usbd\_get\_device\_descriptor**() function returns a pointer to the descriptor. The device descriptor structure is defined in ⟨dev/usb/usb.h⟩ as follows:

```
typedef struct {
	uByte		bLength;
	uByte		bDescriptorType;
	uWord		bcdUSB;
#define UD_USB_2_0		0x0200
#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
	uByte		bDeviceClass;
	uByte		bDeviceSubClass;
	uByte		bDeviceProtocol;
	uByte		bMaxPacketSize;
	/* The fields below are not part of the initial descriptor. */
	uWord		idVendor;
	uWord		idProduct;
	uWord		bcdDevice;
	uByte		iManufacturer;
	uByte		iProduct;
	uByte		iSerialNumber;
	uByte		bNumConfigurations;
} UPACKED usb_device_descriptor_t;
#define USB_DEVICE_DESCRIPTOR_SIZE 18
```

USB devices have at least one configuration descriptor. The *bNumConfigurations* field of the device descriptor specifies the number of configuration descriptors that a device supports. The **usbd\_get\_config\_desc**() function retrieves a particular configuration descriptor from the device and the **usbd\_get\_config\_desc\_full**() function retrieves a full *wTotalLength* length configuration descriptor, which includes all related interface and endpoint descriptors. Only one configuration may be active at a time. The **usbd\_set\_config\_index**() function activates a specified configuration. The configuration descriptor structure is defined in ⟨dev/usb/usb.h⟩ as follows:

```
typedef struct {
	uByte		bLength;
	uByte		bDescriptorType;
	uWord		wTotalLength;
	uByte		bNumInterface;
	uByte		bConfigurationValue;
	uByte		iConfiguration;
	uByte		bmAttributes;
#define UC_BUS_POWERED		0x80
#define UC_SELF_POWERED		0x40
#define UC_REMOTE_WAKEUP	0x20
	uByte		bMaxPower; /* max current in 2 mA units */
#define UC_POWER_FACTOR 2
} UPACKED usb_config_descriptor_t;
#define USB_CONFIG_DESCRIPTOR_SIZE 9
```

Each device configuration provides one or more interfaces. The *bNumInterface* field of the configuration descriptor specifies the number of interfaces associated with a device configuration. Interfaces are described by an interface descriptor, which is defined in ⟨dev/usb/usb.h⟩ as follows:

```
typedef struct {
	uByte		bLength;
	uByte		bDescriptorType;
	uByte		bInterfaceNumber;
	uByte		bAlternateSetting;
	uByte		bNumEndpoints;
	uByte		bInterfaceClass;
	uByte		bInterfaceSubClass;
	uByte		bInterfaceProtocol;
	uByte		iInterface;
} UPACKED usb_interface_descriptor_t;
#define USB_INTERFACE_DESCRIPTOR_SIZE 9
```

Configurations may also have alternate interfaces with the same *bInterfaceNumber* but different *bAlternateSetting* values. These alternate interface settings may be selected by passing a non-zero *altidx* parameter to **usbd\_set\_interface**().

Interfaces have zero or more endpoints, and each endpoint has an endpoint descriptor. Note that endpoint zero, which is always present, does not have an endpoint descriptor, and it is never included in the *bNumEndpoints* count of endpoints. The endpoint descriptor is defined in ⟨dev/usb/usb.h⟩ as follows:

```
typedef struct {
	uByte		bLength;
	uByte		bDescriptorType;
	uByte		bEndpointAddress;
#define UE_GET_DIR(a)	((a) & 0x80)
#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
#define UE_DIR_IN	0x80
#define UE_DIR_OUT	0x00
#define UE_ADDR		0x0f
#define UE_GET_ADDR(a)	((a) & UE_ADDR)
	uByte		bmAttributes;
#define UE_XFERTYPE	0x03
#define	 UE_CONTROL	0x00
#define	 UE_ISOCHRONOUS 0x01
#define	 UE_BULK	0x02
#define	 UE_INTERRUPT	0x03
#define UE_GET_XFERTYPE(a)	((a) & UE_XFERTYPE)
#define UE_ISO_TYPE	0x0c
#define	 UE_ISO_ASYNC	0x04
#define	 UE_ISO_ADAPT	0x08
#define	 UE_ISO_SYNC	0x0c
#define UE_GET_ISO_TYPE(a)	((a) & UE_ISO_TYPE)
	uWord		wMaxPacketSize;
	uByte		bInterval;
} UPACKED usb_endpoint_descriptor_t;
#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
```

## RETURN VALUES

Many functions return a *usbd\_status* type to indicate the outcome of the operation. If the operation completed successfully then USBD\_NORMAL\_COMPLETION is returned. Operations that have been started but not yet completed will return USBD\_IN\_PROGRESS. Other errors usually indicate a problem. Error codes can be converted to strings using **usbd\_errstr**().

## ERRORS

- \[USBD\_PENDING\_REQUESTS\]

  A pipe could not be closed because there are active requests.

- \[USBD\_NOT\_STARTED\]

  The transfer has not yet been started.

- \[USBD\_INVAL\]

  An invalid value was supplied.

- \[USBD\_NOMEM\]

  An attempt to allocate memory failed.

- \[USBD\_CANCELLED\]

  The transfer was aborted.

- \[USBD\_BAD\_ADDRESS\]

  The specified endpoint address was not found.

- \[USBD\_IN\_USE\]

  The endpoint is already in use, or the configuration cannot be changed because some of its endpoints are in use.

- \[USBD\_NO\_ADDR\]

  No free USB devices addresses were found to assign to the device.

- \[USBD\_SET\_ADDR\_FAILED\]

  The device address could not be set.

- \[USBD\_NO\_POWER\]

  Insufficient power was available for the device.

- \[USBD\_TOO\_DEEP\]

  Too many levels of chained hubs were found.

- \[USBD\_IOERROR\]

  There was an error communicating with the device.

- \[USBD\_NOT\_CONFIGURED\]

  An operation that requires an active configuration was attempted while the device was in an unconfigured state.

- \[USBD\_TIMEOUT\]

  A transfer timed out.

- \[USBD\_SHORT\_XFER\]

  A transfer that disallowed short data lengths completed with less than the requested length transferred.

- \[USBD\_STALLED\]

  A transfer failed because the pipe is stalled.

- \[USBD\_INTERRUPTED\]

  An interruptible operation caught a signal.

## SEE ALSO

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

## HISTORY

The USB driver interface first appeared in FreeBSD 3.0.

## AUTHORS

The USB driver was written by Lennart Augustsson for the NetBSD project.

This manual page was written by Ian Dowse ⟨iedowse@FreeBSD.org⟩.
