build: cleanup src/jtag/drivers directory
[openocd.git] / src / jtag / drivers / libusb1_common.c
blobdcca8d1c7d74696aec108ca4e0a0e2ba17647867
1 /***************************************************************************
2 * Copyright (C) 2009 by Zachary T Welch <zw@superlucidity.net> *
3 * *
4 * Copyright (C) 2011 by Mauro Gamba <maurillo71@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (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 *
18 * Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "log.h"
26 #include "libusb1_common.h"
28 static struct libusb_context *jtag_libusb_context; /**< Libusb context **/
29 static libusb_device **devs; /**< The usb device list **/
31 static bool jtag_libusb_match(struct jtag_libusb_device *dev,
32 const uint16_t vids[], const uint16_t pids[])
34 struct libusb_device_descriptor dev_desc;
36 for (unsigned i = 0; vids[i] && pids[i]; i++) {
37 if (libusb_get_device_descriptor(dev, &dev_desc) == 0) {
38 if (dev_desc.idVendor == vids[i] &&
39 dev_desc.idProduct == pids[i])
40 return true;
43 return false;
46 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
47 struct jtag_libusb_device_handle **out)
49 int cnt, idx, errCode;
51 if (libusb_init(&jtag_libusb_context) < 0)
52 return -ENODEV;
54 libusb_set_debug(jtag_libusb_context, 3);
56 cnt = libusb_get_device_list(jtag_libusb_context, &devs);
58 for (idx = 0; idx < cnt; idx++) {
59 if (!jtag_libusb_match(devs[idx], vids, pids))
60 continue;
62 errCode = libusb_open(devs[idx], out);
64 /** Free the device list **/
65 libusb_free_device_list(devs, 1);
67 if (errCode < 0)
68 return errCode;
69 return 0;
71 return -ENODEV;
74 void jtag_libusb_close(jtag_libusb_device_handle *dev)
76 /* Close device */
77 jtag_libusb_close(dev);
79 libusb_exit(jtag_libusb_context);
82 int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
83 int size, int timeout)
85 int transferred = 0;
87 libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
88 &transferred, timeout);
89 return transferred;
92 int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
93 int size, int timeout)
95 int transferred = 0;
97 libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
98 &transferred, timeout);
99 return transferred;
102 int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
103 int configuration)
105 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
106 int retCode = -99;
108 struct libusb_config_descriptor *config = NULL;
110 libusb_get_config_descriptor(udev, configuration, &config);
111 retCode = libusb_set_configuration(devh, config->bConfigurationValue);
113 libusb_free_config_descriptor(config);
115 return retCode;
118 int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev,
119 unsigned int *usb_read_ep,
120 unsigned int *usb_write_ep)
122 const struct libusb_interface *inter;
123 const struct libusb_interface_descriptor *interdesc;
124 const struct libusb_endpoint_descriptor *epdesc;
125 struct libusb_config_descriptor *config;
127 libusb_get_config_descriptor(udev, 0, &config);
128 for (int i = 0; i < (int)config->bNumInterfaces; i++) {
129 inter = &config->interface[i];
131 for (int j = 0; j < inter->num_altsetting; j++) {
132 interdesc = &inter->altsetting[j];
133 for (int k = 0;
134 k < (int)interdesc->bNumEndpoints; k++) {
135 epdesc = &interdesc->endpoint[k];
137 uint8_t epnum = epdesc->bEndpointAddress;
138 bool is_input = epnum & 0x80;
139 LOG_DEBUG("usb ep %s %02x",
140 is_input ? "in" : "out", epnum);
142 if (is_input)
143 *usb_read_ep = epnum;
144 else
145 *usb_write_ep = epnum;
149 libusb_free_config_descriptor(config);
151 return 0;