blob: 33caa86287ac4fb0f5ab2fe63c4f0504bba7645f [file] [log] [blame]
Harald Welteda432cd2019-12-15 19:13:26 +01001#pragma once
Harald Welted462e3f2019-12-15 20:04:51 +01002/* libusb utilities
3 *
4 * (C) 2010-2019 by Harald Welte <hwelte@hmw-consulting.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Harald Welted462e3f2019-12-15 20:04:51 +010015 */
Harald Welteda432cd2019-12-15 19:13:26 +010016
17#include <libusb.h>
18
Harald Welted462e3f2019-12-15 20:04:51 +010019#define USB_MAX_PATH_LEN 20
20
21struct dev_id {
22 uint16_t vendor_id;
23 uint16_t product_id;
24};
25
26/* structure describing a single matching interface found */
27struct usb_interface_match {
28 /* libusb device E*/
29 libusb_device *usb_dev;
30 /* Vendor ID of the device running matching interface */
31 uint16_t vendor;
32 /* Product ID of the device running matching interface */
33 uint16_t product;
34 /* USB Bus Address */
35 uint8_t addr;
36 /* physical path */
37 char path[USB_MAX_PATH_LEN];
38 /* configuration of matching interface */
39 uint8_t configuration;
40 /* interface number of matching interface */
41 uint8_t interface;
42 /* altsetting of matching interface */
43 uint8_t altsetting;
44 /* bInterfaceClass of matching interface */
45 uint8_t class;
46 /* bInterfaceSubClass of matching interface */
47 uint8_t sub_class;
48 /* bInterfaceProtocol of matching interface */
49 uint8_t protocol;
50 /* index of string descriptor of matching interface */
51 uint8_t string_idx;
52};
53
Harald Welte3edd70c2020-01-12 23:45:42 +010054/*! Description of the USB device+interface we're looking for */
55struct osmo_usb_matchspec {
56 /*! specify the USB device */
57 struct {
58 int vendor_id; /*!< typically -1 for compile time defaults */
59 int product_id; /*!< typically -1 for compile time defaults */
60 char *path; /*!< used for disambiguation when multiple matches; can be NULL */
61 } dev;
62
63 /*! specify the USB configuration */
64 int config_id; /*!< typically -1 unless user selects specific configuration */
65
66 /*! specify the USB interface */
67 struct {
68 /* typically those three are set to application defaults */
69 int class; /*!< -1 or a user-specified class */
70 int subclass; /*!< -1 or a user-specified subclass */
71 int proto; /*!< -1 or a user-specified protocol */
72
73 /* typically those two are -1; but user can override them */
74 int num;
75 int altsetting;
76 } intf;
77};
78
Harald Welted462e3f2019-12-15 20:04:51 +010079
80char *osmo_libusb_dev_get_path_buf(char *buf, size_t bufsize, libusb_device *dev);
81char *osmo_libusb_dev_get_path_c(void *ctx, libusb_device *dev);
82
83libusb_device **osmo_libusb_find_matching_usb_devs(void *ctx, struct libusb_context *luctx,
84 const struct dev_id *dev_ids);
85
Harald Weltec45787b2019-12-24 12:20:07 +010086libusb_device *osmo_libusb_find_matching_dev_path(struct libusb_context *luctx,
87 const struct dev_id *dev_ids,
88 const char *path);
89
90libusb_device *osmo_libusb_find_matching_dev_serial(struct libusb_context *luctx,
91 const struct dev_id *dev_ids,
92 const char *serial);
93
Harald Welted462e3f2019-12-15 20:04:51 +010094int osmo_libusb_dev_find_matching_interfaces(libusb_device *dev, int class, int sub_class,
95 int protocol, struct usb_interface_match *out,
96 unsigned int out_len);
97
98int osmo_libusb_find_matching_interfaces(libusb_context *luctx, const struct dev_id *dev_ids,
99 int class, int sub_class, int protocol,
100 struct usb_interface_match *out, unsigned int out_len);
101
102libusb_device_handle *osmo_libusb_open_claim_interface(void *ctx, libusb_context *luctx,
103 const struct usb_interface_match *ifm);
104
Harald Welte3edd70c2020-01-12 23:45:42 +0100105void osmo_libusb_match_init(struct osmo_usb_matchspec *cfg, int if_class, int if_subclass, int if_proto);
106
107libusb_device_handle *osmo_libusb_find_open_claim(const struct osmo_usb_matchspec *cfg,
108 const struct dev_id *default_dev_ids);
109
Harald Welted462e3f2019-12-15 20:04:51 +0100110int osmo_libusb_get_ep_addrs(libusb_device_handle *devh, unsigned int if_num,
111 uint8_t *out, uint8_t *in, uint8_t *irq);
112
113
Harald Welteda432cd2019-12-15 19:13:26 +0100114int osmo_libusb_init(libusb_context **luctx);
115void osmo_libusb_exit(libusb_context *luctx);