2.27. usb_host — Universal Serial Bus - Host

A USB host powers the bus and enumerates connected USB devices.


Class driver modules:


Source code: src/drivers/usb_host.h, src/drivers/usb_host.c


Defines

USB_HOST_DEVICE_STATE_NONE
USB_HOST_DEVICE_STATE_ATTACHED
USB_PIPE_TYPE_CONTROL
USB_PIPE_TYPE_INTERRUPT
USB_PIPE_TYPE_ISOCHRONOUS
USB_PIPE_TYPE_BULK

Functions

int usb_host_module_init(void)

Initialize the USB host module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

int usb_host_init(struct usb_host_driver_t *self_p, struct usb_device_t *dev_p, struct usb_host_device_t *devices_p, size_t length)

Initialize the USB host driver object from given configuration.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to be initialized.
  • dev_p: USB device to use.
  • devices_p: An array of devices. One entry in this array is allocated for each USB device that is connected to the host.
  • length: Length of the devices array.

int usb_host_start(struct usb_host_driver_t *self_p)

Start the USB host device using given driver object.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

int usb_host_stop(struct usb_host_driver_t *self_p)

Stop the USB host device referenced by driver object.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

int usb_host_driver_add(struct usb_host_driver_t *self_p, struct usb_host_device_driver_t *driver_p, void *arg_p)

Add given class/vendor driver to the USB host driver.

When a USB device is plugged in, its class and vendor information is read by the host. Those values are used to find the device driver for this particular device. If there is no driver, the device cannot be configured and will not work.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.
  • driver_p: USB device driver to add.

int usb_host_driver_remove(struct usb_host_driver_t *self_p, struct usb_host_device_driver_t *driver_p)

Remove given class/vendor driver from the USB host driver.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.
  • driver_p: USB device driver to remove.

struct usb_host_device_t *usb_host_device_open(struct usb_host_driver_t *self_p, int device)

Open given device in given driver. Open a device before reading and writing data to it with usb_host_device_read() or usb_host_device_write().

Return
Opened device or NULL on failure.
Parameters
  • self_p: Initialized driver.
  • device: Device to open.

int usb_host_device_close(struct usb_host_driver_t *self_p, int device)

Close given device in given driver.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver.
  • device: Device to close.

ssize_t usb_host_device_read(struct usb_host_device_t *device_p, int endpoint, void *buf_p, size_t size)

Read data from given endpoint for given device.

Return
Number of bytes read or negative error code.
Parameters
  • device_p: Device to read from.
  • endpoint: Endpoint to read data from.
  • buf_p: Buffer to read into.
  • size: Number of bytes to read.

ssize_t usb_host_device_write(struct usb_host_device_t *device_p, int endpoint, const void *buf_p, size_t size)

Write data to given endpoint for given device.

Return
Number of bytes written or negative error code.
Parameters
  • device_p: Device to write to.
  • endpoint: Endpoint to write to.
  • buf_p: Buffer to write.
  • size: Number of bytes to write.

ssize_t usb_host_device_control_transfer(struct usb_host_device_t *device_p, struct usb_setup_t *setup_p, void *buf_p, size_t size)

Perform a control transfer on endpoint zero(0).

A control transfer can have up to three stages. First the setup stage, then an optional data stage, and at last a status stage.

Return
Number of bytes read/written or negative error code.
Parameters
  • device_p: Device to write to.
  • setup_p: Setup packet to write.
  • buf_p: Buffer to read/write. May be NULL if no data shall be transferred.
  • size: Number of bytes to read/write.

int usb_host_device_set_configuration(struct usb_host_device_t *device_p, uint8_t configuration)

Set configuration for given device.

Return
zero(0) or negative error code.
Parameters
  • device_p: Device to use.
  • configuration: Configuration to set.

struct usb_host_device_t
#include <usb_host.h>

An USB device as seen by the host.

Public Members

int id
int state
int address
int vid
int pid
char *description_p
size_t max_packet_size
uint8_t configuration
struct usb_descriptor_device_t *dev_p
struct usb_descriptor_configuration_t *conf_p
struct usb_host_device_t::@22::@24 usb_host_device_t::descriptor
struct usb_host_device_t::@22 usb_host_device_t::current
struct usb_host_driver_t *self_p
struct usb_pipe_t *pipes[32]
size_t size
uint8_t buf[128]
struct usb_host_device_t::@23 usb_host_device_t::descriptors
struct usb_host_device_driver_t
#include <usb_host.h>

Used to find a device driver.

Public Members

int (*supports)(struct usb_host_device_t *)
int (*enumerate)(struct usb_host_device_t *)
struct usb_host_device_driver_t *next_p