blob: 9ad3f71a9cc882bc9cfbe1c5a7a0f5777bb1e1b8 [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
Harald Welteda432cd2019-12-15 19:13:26 +010020
21#include <libusb.h>
22
Harald Welted462e3f2019-12-15 20:04:51 +010023#define USB_MAX_PATH_LEN 20
24
25struct dev_id {
26 uint16_t vendor_id;
27 uint16_t product_id;
28};
29
30/* structure describing a single matching interface found */
31struct usb_interface_match {
32 /* libusb device E*/
33 libusb_device *usb_dev;
34 /* Vendor ID of the device running matching interface */
35 uint16_t vendor;
36 /* Product ID of the device running matching interface */
37 uint16_t product;
38 /* USB Bus Address */
39 uint8_t addr;
40 /* physical path */
41 char path[USB_MAX_PATH_LEN];
42 /* configuration of matching interface */
43 uint8_t configuration;
44 /* interface number of matching interface */
45 uint8_t interface;
46 /* altsetting of matching interface */
47 uint8_t altsetting;
48 /* bInterfaceClass of matching interface */
49 uint8_t class;
50 /* bInterfaceSubClass of matching interface */
51 uint8_t sub_class;
52 /* bInterfaceProtocol of matching interface */
53 uint8_t protocol;
54 /* index of string descriptor of matching interface */
55 uint8_t string_idx;
56};
57
Harald Welte3edd70c2020-01-12 23:45:42 +010058/*! Description of the USB device+interface we're looking for */
59struct osmo_usb_matchspec {
60 /*! specify the USB device */
61 struct {
62 int vendor_id; /*!< typically -1 for compile time defaults */
63 int product_id; /*!< typically -1 for compile time defaults */
64 char *path; /*!< used for disambiguation when multiple matches; can be NULL */
65 } dev;
66
67 /*! specify the USB configuration */
68 int config_id; /*!< typically -1 unless user selects specific configuration */
69
70 /*! specify the USB interface */
71 struct {
72 /* typically those three are set to application defaults */
73 int class; /*!< -1 or a user-specified class */
74 int subclass; /*!< -1 or a user-specified subclass */
75 int proto; /*!< -1 or a user-specified protocol */
76
77 /* typically those two are -1; but user can override them */
78 int num;
79 int altsetting;
80 } intf;
81};
82
Harald Welted462e3f2019-12-15 20:04:51 +010083
84char *osmo_libusb_dev_get_path_buf(char *buf, size_t bufsize, libusb_device *dev);
85char *osmo_libusb_dev_get_path_c(void *ctx, libusb_device *dev);
86
87libusb_device **osmo_libusb_find_matching_usb_devs(void *ctx, struct libusb_context *luctx,
88 const struct dev_id *dev_ids);
89
Harald Weltec45787b2019-12-24 12:20:07 +010090libusb_device *osmo_libusb_find_matching_dev_path(struct libusb_context *luctx,
91 const struct dev_id *dev_ids,
92 const char *path);
93
94libusb_device *osmo_libusb_find_matching_dev_serial(struct libusb_context *luctx,
95 const struct dev_id *dev_ids,
96 const char *serial);
97
Harald Welted462e3f2019-12-15 20:04:51 +010098int osmo_libusb_dev_find_matching_interfaces(libusb_device *dev, int class, int sub_class,
99 int protocol, struct usb_interface_match *out,
100 unsigned int out_len);
101
102int osmo_libusb_find_matching_interfaces(libusb_context *luctx, const struct dev_id *dev_ids,
103 int class, int sub_class, int protocol,
104 struct usb_interface_match *out, unsigned int out_len);
105
106libusb_device_handle *osmo_libusb_open_claim_interface(void *ctx, libusb_context *luctx,
107 const struct usb_interface_match *ifm);
108
Harald Welte3edd70c2020-01-12 23:45:42 +0100109void osmo_libusb_match_init(struct osmo_usb_matchspec *cfg, int if_class, int if_subclass, int if_proto);
110
111libusb_device_handle *osmo_libusb_find_open_claim(const struct osmo_usb_matchspec *cfg,
112 const struct dev_id *default_dev_ids);
113
Harald Welted462e3f2019-12-15 20:04:51 +0100114int osmo_libusb_get_ep_addrs(libusb_device_handle *devh, unsigned int if_num,
115 uint8_t *out, uint8_t *in, uint8_t *irq);
116
117
Harald Welteda432cd2019-12-15 19:13:26 +0100118int osmo_libusb_init(libusb_context **luctx);
119void osmo_libusb_exit(libusb_context *luctx);