libosmousb 1.13.1.1-afb4 Osmocom USB library

Macros | Functions
osmo_libusb.c File Reference
`#include <errno.h>`
`#include <unistd.h>`
`#include <stdlib.h>`
`#include <stdint.h>`
`#include <string.h>`
`#include <poll.h>`
`#include <osmocom/core/utils.h>`
`#include <osmocom/core/logging.h>`
`#include <osmocom/core/select.h>`
`#include <osmocom/core/talloc.h>`
`#include <libusb.h>`

## Macros
#define | DLUSB DLINP

## Functions
static int | osmo_usb_fd_cb (struct osmo_fd *ofd, unsigned int what)
static void | osmo_usb_added_cb (int fd, short events, void *user_data)
static void | osmo_usb_removed_cb (int fd, void *user_data)
char * | osmo_libusb_dev_get_path_buf (char *buf, size_t bufsize, libusb_device *dev)
| obtain the string representation of the USB device path of given device. More...
char * | osmo_libusb_dev_get_path_c (void *ctx, libusb_device *dev)
| obtain the string representation of the USB device path of given device. More...
static int | match_dev_id (const struct libusb_device_descriptor *desc, const struct dev_id *id)
static int | match_dev_ids (const struct libusb_device_descriptor *desc, const struct dev_id *ids)
libusb_device ** | osmo_libusb_find_matching_usb_devs (void *ctx, struct libusb_context *luctx, const struct dev_id *dev_ids)
| Find USB devices matching the specified list of USB VendorID/ProductIDs. More...
libusb_device * | osmo_libusb_find_matching_dev_path (struct libusb_context *luctx, const struct dev_id *dev_ids, const char *path)
| Find a USB device of matching VendorID/ProductID at given path. More...
libusb_device * | osmo_libusb_find_matching_dev_serial (struct libusb_context *luctx, const struct dev_id *dev_ids, const char *serial)
| Find a USB device of matching VendorID/ProductID and given iSerial string. More...
int | osmo_libusb_dev_find_matching_interfaces (libusb_device *dev, int class, int sub_class, int protocol, struct usb_interface_match *out, unsigned int out_len)
| find a matching interface among all interfaces of the given USB device. More...
int | osmo_libusb_find_matching_interfaces (libusb_context *luctx, const struct dev_id *dev_ids, int class, int sub_class, int protocol, struct usb_interface_match *out, unsigned int out_len)
| find matching interfaces among a list devices of specified VendorId/ProductID tuples. More...
libusb_device_handle * | osmo_libusb_open_claim_interface (void *ctx, libusb_context *luctx, const struct usb_interface_match *ifm)
| open matching USB device and claim interface More...
void | osmo_libusb_match_init (struct osmo_usb_matchspec *cfg, int if_class, int if_subclass, int if_proto)
libusb_device_handle * | osmo_libusb_find_open_claim (const struct osmo_usb_matchspec *cfg, const struct dev_id *default_dev_ids)
| high-level all-in-one function for USB device, config + interface matching + opening. More...
int | osmo_libusb_get_ep_addrs (libusb_device_handle *devh, unsigned int if_num, uint8_t *out, uint8_t *in, uint8_t *irq)
| obtain the endpoint addresses for a given USB interface. More...
int | osmo_libusb_init (libusb_context **pluctx)
void | osmo_libusb_exit (libusb_context *luctx)

## Macro Definition Documentation

## ◆ DLUSB

#define DLUSB DLINP

## Function Documentation

## ◆ match_dev_id()

static int match_dev_id | ( | const struct libusb_device_descriptor * | desc,
|  | const struct dev_id * | id
| ) |  |
| static

References dev_id::product_id, and dev_id::vendor_id.

Referenced by match_dev_ids().

## ◆ match_dev_ids()

static int match_dev_ids | ( | const struct libusb_device_descriptor * | desc,
|  | const struct dev_id * | ids
| ) |  |
| static

References match_dev_id(), and dev_id::vendor_id.

Referenced by osmo_libusb_find_matching_dev_path(), osmo_libusb_find_matching_dev_serial(), and osmo_libusb_find_matching_usb_devs().

## ◆ osmo_libusb_dev_find_matching_interfaces()

int osmo_libusb_dev_find_matching_interfaces | ( | libusb_device * | dev,
|  | int | class,
|  | int | sub_class,
|  | int | protocol,
|  | struct usb_interface_match * | out,
|  | unsigned int | out_len
| ) |  |

find a matching interface among all interfaces of the given USB device.

Parameters

  [in] | dev | USB device in which we shall search
  [in] | class | USB Interface Class to look for
  [in] | sub_class | USB Interface Subclass to look for
  [in] | protocol | USB Interface Protocol to look for
  [out] | out | User-allocated array for storing matches
  [in] | out_len | Length of out array

Returns
  number of matching interfaces; negative in case of error

References usb_interface_match::addr, usb_interface_match::altsetting, usb_interface_match::class, usb_interface_match::configuration, usb_interface_match::interface, osmo_libusb_dev_get_path_buf(), OSMO_STRLCPY_ARRAY, usb_interface_match::path, usb_interface_match::product, usb_interface_match::protocol, usb_interface_match::string_idx, usb_interface_match::sub_class, usb_interface_match::usb_dev, USB_MAX_PATH_LEN, and usb_interface_match::vendor.

Referenced by osmo_libusb_find_matching_interfaces(), and osmo_libusb_find_open_claim().

## ◆ osmo_libusb_dev_get_path_buf()

char * osmo_libusb_dev_get_path_buf | ( | char * | buf,
|  | size_t | bufsize,
|  | libusb_device * | dev
| ) |  |

obtain the string representation of the USB device path of given device.

Parameters

  [out] | buf | Output string buffer
  [in] | bufsize | Size of output string buffer in bytes
  [in] | dev | USB device whose bus path we want to obtain

Returns
  pointer to 'buf' in case of success; NULL in case of error

References osmo_strbuf::buf, and OSMO_STRBUF_PRINTF.

Referenced by osmo_libusb_dev_find_matching_interfaces(), osmo_libusb_dev_get_path_c(), osmo_libusb_find_matching_dev_path(), and osmo_libusb_open_claim_interface().

## ◆ osmo_libusb_dev_get_path_c()

char * osmo_libusb_dev_get_path_c | ( | void * | ctx,
|  | libusb_device * | dev
| ) |  |

obtain the string representation of the USB device path of given device.

Parameters

  [in] | talloc | context from which to dynamically allocate output string buffer
  [in] | dev | USB device whose bus path we want to obtain

Returns
  pointer to 'buf' in case of success; NULL in case of error

References osmo_strbuf::buf, osmo_libusb_dev_get_path_buf(), and USB_MAX_PATH_LEN.

## ◆ osmo_libusb_exit()

void osmo_libusb_exit | ( | libusb_context * | luctx | ) |

## ◆ osmo_libusb_find_matching_dev_path()

libusb_device * osmo_libusb_find_matching_dev_path | ( | struct libusb_context * | luctx,
|  | const struct dev_id * | dev_ids,
|  | const char * | path
| ) |  |

Find a USB device of matching VendorID/ProductID at given path.

Parameters

  [in] | luctx | libusb context on which to operate
  [in] | dev_ids | zer-oterminated array of VendorId/ProductId tuples
  [in] | path | string representation of USB path

Returns
  libusb_device if there was exactly one match; NULL otherwise

References DLUSB, LOGL_ERROR, LOGP, match_dev_ids(), and osmo_libusb_dev_get_path_buf().

Referenced by osmo_libusb_find_open_claim().

## ◆ osmo_libusb_find_matching_dev_serial()

libusb_device * osmo_libusb_find_matching_dev_serial | ( | struct libusb_context * | luctx,
|  | const struct dev_id * | dev_ids,
|  | const char * | serial
| ) |  |

Find a USB device of matching VendorID/ProductID and given iSerial string.

Parameters

  [in] | luctx | libusb context on which to operate
  [in] | dev_ids | zer-oterminated array of VendorId/ProductId tuples
  [in] | serial | string representation of serial number

Returns
  libusb_device if there was exactly one match; NULL otherwise

References DLUSB, LOGL_ERROR, LOGP, and match_dev_ids().

## ◆ osmo_libusb_find_matching_interfaces()

int osmo_libusb_find_matching_interfaces | ( | libusb_context * | luctx,
|  | const struct dev_id * | dev_ids,
|  | int | class,
|  | int | sub_class,
|  | int | protocol,
|  | struct usb_interface_match * | out,
|  | unsigned int | out_len
| ) |  |

find matching interfaces among a list devices of specified VendorId/ProductID tuples.

Parameters

  [in] | luctx | libusb context on which to operate
  [in] | dev_ids | zero-terminated array of VendorId/ProductId tuples
  [in] | class | USB Interface Class to look for
  [in] | sub_class | USB Interface Subclass to look for
  [in] | protocol | USB Interface Protocol to look for
  [out] | out | User-allocated array for storing matches
  [in] | out_len | Length of out array

Returns
  number of matching interfaces; negative in case of error

References osmo_libusb_dev_find_matching_interfaces(), and osmo_libusb_find_matching_usb_devs().

## ◆ osmo_libusb_find_matching_usb_devs()

libusb_device ** osmo_libusb_find_matching_usb_devs | ( | void * | ctx,
|  | struct libusb_context * | luctx,
|  | const struct dev_id * | dev_ids
| ) |  |

Find USB devices matching the specified list of USB VendorID/ProductIDs.

Parameters

  [in] | ctx | talloc context from which to allocate output data
  [in] | luctx | libusb context on which to operate
  [in] | dev_ids | zero-terminated array of VendorId/ProductId tuples

Returns
  array of up to 256 libusb_device pointers; NULL in case of error

References match_dev_ids().

Referenced by osmo_libusb_find_matching_interfaces(), and osmo_libusb_open_claim_interface().

## ◆ osmo_libusb_find_open_claim()

libusb_device_handle * osmo_libusb_find_open_claim | ( | const struct osmo_usb_matchspec * | cfg,
|  | const struct dev_id * | default_dev_ids
| ) |  |

high-level all-in-one function for USB device, config + interface matching + opening.

This function offers the highest level of API among all libosmousb helper functions. It is intended as a one-stop shop for everything related to grabbing an interface.

1) looks for a device matching either the VID/PID from 'cfg' or 'default_dev_ids', if more than one is found, the user is expected to fill in cfg->dev.path to disambiguate. 2) find any interfaces on the device that match the specification in 'cfg'. The match could be done based on any of (class, subclass, proto, interface number). If there are multiple matches, the caller must disambiguate by specifying the interface number. 3) open the USB device; set the configuration (if needed); claim the interface and set the altsetting

Parameters

  [in] | cfg | user-supplied match configuration (from command line or config file)
  [in] | default_dev_ids | Default list of supported VendorId/ProductIds

Returns
  libusb_device_handle on success, NULL on error

References osmo_usb_matchspec::class, osmo_usb_matchspec::dev, DLUSB, osmo_usb_matchspec::intf, LOGL_ERROR, LOGL_NOTICE, LOGP, osmo_usb_matchspec::num, osmo_libusb_dev_find_matching_interfaces(), osmo_libusb_find_matching_dev_path(), osmo_libusb_open_claim_interface(), osmo_usb_matchspec::path, osmo_usb_matchspec::product_id, osmo_usb_matchspec::proto, osmo_usb_matchspec::subclass, and osmo_usb_matchspec::vendor_id.

## ◆ osmo_libusb_get_ep_addrs()

int osmo_libusb_get_ep_addrs | ( | libusb_device_handle * | devh,
|  | unsigned int | if_num,
|  | uint8_t * | out,
|  | uint8_t * | in,
|  | uint8_t * | irq
| ) |  |

obtain the endpoint addresses for a given USB interface.

Parameters

  [in] | devh | USB device handle on which to operate
  [in] | if_num | USB Interface number on which to operate
  [out] | out | user-provided storage for OUT endpoint number
  [out] | in | user-provided storage for IN endpoint number
  [out] | irq | user-provided storage for IRQ endpoint number

Returns
  0 in case of success; negative in case of error

## ◆ osmo_libusb_init()

int osmo_libusb_init | ( | libusb_context ** | pluctx | ) |

References DLUSB, LOGL_ERROR, LOGP, osmo_usb_added_cb(), and osmo_usb_removed_cb().

## ◆ osmo_libusb_match_init()

void osmo_libusb_match_init | ( | struct osmo_usb_matchspec * | cfg,
|  | int | if_class,
|  | int | if_subclass,
|  | int | if_proto
| ) |  |

References osmo_usb_matchspec::altsetting, osmo_usb_matchspec::class, osmo_usb_matchspec::config_id, osmo_usb_matchspec::dev, osmo_usb_matchspec::intf, osmo_usb_matchspec::num, osmo_usb_matchspec::path, osmo_usb_matchspec::product_id, osmo_usb_matchspec::proto, osmo_usb_matchspec::subclass, and osmo_usb_matchspec::vendor_id.

## ◆ osmo_libusb_open_claim_interface()

libusb_device_handle * osmo_libusb_open_claim_interface | ( | void * | ctx,
|  | libusb_context * | luctx,
|  | const struct usb_interface_match * | ifm
| ) |  |

open matching USB device and claim interface

Parameters

  [in] | ctx | talloc context to use for related allocations
  [in] | luctx | libusb context on which to operate
  [in] | ifm | interface match describing interface to claim

Returns
  libusb device chandle on success; NULL on error

References usb_interface_match::addr, usb_interface_match::altsetting, config, usb_interface_match::configuration, usb_interface_match::interface, osmo_libusb_dev_get_path_buf(), osmo_libusb_find_matching_usb_devs(), usb_interface_match::path, usb_interface_match::product, USB_MAX_PATH_LEN, and usb_interface_match::vendor.

Referenced by osmo_libusb_find_open_claim().

## ◆ osmo_usb_added_cb()

static void osmo_usb_added_cb | ( | int | fd,
|  | short | events,
|  | void * | user_data
| ) |  |
| static

References DLUSB, osmo_fd::fd, LOGL_ERROR, LOGP, OSMO_FD_READ, osmo_fd_register(), osmo_fd_setup(), OSMO_FD_WRITE, osmo_usb_fd_cb(), OTC_GLOBAL, and osmo_fd::when.

Referenced by osmo_libusb_init().

## ◆ osmo_usb_fd_cb()

static int osmo_usb_fd_cb | ( | struct osmo_fd * | ofd,
|  | unsigned int | what
| ) |  |
| static

References osmo_fd::data.

Referenced by osmo_usb_added_cb().

## ◆ osmo_usb_removed_cb()

static void osmo_usb_removed_cb | ( | int | fd,
|  | void * | user_data
| ) |  |
| static

References osmo_fd::fd, osmo_fd_get_by_fd(), and osmo_fd_unregister().

Referenced by osmo_libusb_init().