Harald Welte | da432cd | 2019-12-15 19:13:26 +0100 | [diff] [blame] | 1 | #pragma once |
Harald Welte | d462e3f | 2019-12-15 20:04:51 +0100 | [diff] [blame] | 2 | /* 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 Welte | d462e3f | 2019-12-15 20:04:51 +0100 | [diff] [blame] | 15 | */ |
Harald Welte | da432cd | 2019-12-15 19:13:26 +0100 | [diff] [blame] | 16 | |
| 17 | #include <libusb.h> |
| 18 | |
Harald Welte | d462e3f | 2019-12-15 20:04:51 +0100 | [diff] [blame] | 19 | #define USB_MAX_PATH_LEN 20 |
| 20 | |
| 21 | struct dev_id { |
| 22 | uint16_t vendor_id; |
| 23 | uint16_t product_id; |
| 24 | }; |
| 25 | |
| 26 | /* structure describing a single matching interface found */ |
| 27 | struct 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 Welte | 3edd70c | 2020-01-12 23:45:42 +0100 | [diff] [blame] | 54 | /*! Description of the USB device+interface we're looking for */ |
| 55 | struct 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 Welte | d462e3f | 2019-12-15 20:04:51 +0100 | [diff] [blame] | 79 | |
| 80 | char *osmo_libusb_dev_get_path_buf(char *buf, size_t bufsize, libusb_device *dev); |
| 81 | char *osmo_libusb_dev_get_path_c(void *ctx, libusb_device *dev); |
| 82 | |
| 83 | libusb_device **osmo_libusb_find_matching_usb_devs(void *ctx, struct libusb_context *luctx, |
| 84 | const struct dev_id *dev_ids); |
| 85 | |
Harald Welte | c45787b | 2019-12-24 12:20:07 +0100 | [diff] [blame] | 86 | libusb_device *osmo_libusb_find_matching_dev_path(struct libusb_context *luctx, |
| 87 | const struct dev_id *dev_ids, |
| 88 | const char *path); |
| 89 | |
| 90 | libusb_device *osmo_libusb_find_matching_dev_serial(struct libusb_context *luctx, |
| 91 | const struct dev_id *dev_ids, |
| 92 | const char *serial); |
| 93 | |
Harald Welte | d462e3f | 2019-12-15 20:04:51 +0100 | [diff] [blame] | 94 | int 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 | |
| 98 | int 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 | |
| 102 | libusb_device_handle *osmo_libusb_open_claim_interface(void *ctx, libusb_context *luctx, |
| 103 | const struct usb_interface_match *ifm); |
| 104 | |
Harald Welte | 3edd70c | 2020-01-12 23:45:42 +0100 | [diff] [blame] | 105 | void osmo_libusb_match_init(struct osmo_usb_matchspec *cfg, int if_class, int if_subclass, int if_proto); |
| 106 | |
| 107 | libusb_device_handle *osmo_libusb_find_open_claim(const struct osmo_usb_matchspec *cfg, |
| 108 | const struct dev_id *default_dev_ids); |
| 109 | |
Harald Welte | d462e3f | 2019-12-15 20:04:51 +0100 | [diff] [blame] | 110 | int 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 Welte | da432cd | 2019-12-15 19:13:26 +0100 | [diff] [blame] | 114 | int osmo_libusb_init(libusb_context **luctx); |
| 115 | void osmo_libusb_exit(libusb_context *luctx); |