blob: a7306ce869bbf39cf3502341668a56d7b5f06fc1 [file] [log] [blame]
Kévin Redon26a66092018-10-10 00:30:23 +02001/* simtrace2-discovery - host PC library to scan for matching USB
2 * devices
3 *
4 * (C) 2016 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 */
20#include <stdint.h>
21
22#include <libusb.h>
23
24/*! \brief obtain the endpoint addresses for a given USB interface */
25int get_usb_ep_addrs(libusb_device_handle *devh, unsigned int if_num,
26 uint8_t *out, uint8_t *in, uint8_t *irq)
27{
28 libusb_device *dev = libusb_get_device(devh);
29 struct libusb_config_descriptor *cdesc;
30 const struct libusb_interface_descriptor *idesc;
31 const struct libusb_interface *iface;
32 int rc, l;
33
34 rc = libusb_get_active_config_descriptor(dev, &cdesc);
35 if (rc < 0)
36 return rc;
37
38 iface = &cdesc->interface[if_num];
39 /* FIXME: we assume there's no altsetting */
40 idesc = &iface->altsetting[0];
41
42 for (l = 0; l < idesc->bNumEndpoints; l++) {
43 const struct libusb_endpoint_descriptor *edesc = &idesc->endpoint[l];
44 switch (edesc->bmAttributes & 3) {
45 case LIBUSB_TRANSFER_TYPE_BULK:
46 if (edesc->bEndpointAddress & 0x80) {
47 if (in)
48 *in = edesc->bEndpointAddress;
49 } else {
50 if (out)
51 *out = edesc->bEndpointAddress;
52 }
53 break;
54 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
55 if (irq)
56 *irq = edesc->bEndpointAddress;
57 break;
58 default:
59 break;
60 }
61 }
62 return 0;
63}
64
65#if 0
66 struct libusb_device_descriptor ddesc;
67 int rc, i, j, k;
68
69 rc = libusb_get_device_descriptor(devh, &ddesc);
70 if (rc < 0)
71 return;
72
73 for (i = 0; i < ddesc.bNumConfigurations; i++) {
74 struct libusb_config_descriptor *cdesc;
75 rc = libusb_get_config_descriptor(devh, i, &cdesc);
76 if (rc < 0)
77 return;
78
79 for (j = 0; j < cdesc->bNumInterfaces; j++) {
80 const struct libusb_interface *iface = cdesc->interface[j];
81 for (k = 0; k < iface->num_altsetting; k++) {
82 const struct libusb_interface_descriptor *idesc = iface->altsetting[k];
83 /* make sure this is the interface we're looking for */
84 if (idesc->bInterfaceClass != 0xFF ||
85 idesc->bInterfaceSubClass != if_class ||
86 idsec->bInterfaceProtocol != if_proto)
87 continue;
88 /* FIXME */
89 }
90 }
91
92 libusb_free_config_descriptor(cdesc);
93 }
94#endif