23 #define MAX_USB_PORTS 7
26 static struct libusb_device **
devs;
33 case LIBUSB_ERROR_TIMEOUT:
36 case LIBUSB_ERROR_INVALID_PARAM:
37 case LIBUSB_ERROR_ACCESS:
38 case LIBUSB_ERROR_NO_DEVICE:
39 case LIBUSB_ERROR_NOT_FOUND:
40 case LIBUSB_ERROR_BUSY:
41 case LIBUSB_ERROR_OVERFLOW:
42 case LIBUSB_ERROR_PIPE:
43 case LIBUSB_ERROR_INTERRUPTED:
44 case LIBUSB_ERROR_NO_MEM:
45 case LIBUSB_ERROR_NOT_SUPPORTED:
46 case LIBUSB_ERROR_OTHER:
54 const uint16_t vids[],
const uint16_t pids[])
56 for (
unsigned int i = 0; vids[i]; i++) {
57 if (dev_desc->idVendor == vids[i] &&
58 dev_desc->idProduct == pids[i]) {
65 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
73 if (path_len == LIBUSB_ERROR_OVERFLOW) {
74 LOG_WARNING(
"cannot determine path to usb device! (more than %i ports in path)\n",
78 dev_bus = libusb_get_bus_number(
device);
96 char desc_string[256+1];
101 retval = libusb_get_string_descriptor_ascii(
device, str_index,
102 (
unsigned char *)desc_string,
sizeof(desc_string)-1);
104 LOG_ERROR(
"libusb_get_string_descriptor_ascii() failed with %d", retval);
109 desc_string[
sizeof(desc_string)-1] =
'\0';
111 matched = strncmp(
string, desc_string,
sizeof(desc_string)) == 0;
113 LOG_DEBUG(
"Device serial number '%s' doesn't match requested serial '%s'",
114 desc_string,
string);
119 struct libusb_device_descriptor *dev_desc,
const char *
serial,
126 if (!adapter_get_alternate_serial)
130 char *alternate_serial = adapter_get_alternate_serial(
device, dev_desc);
133 if (!alternate_serial)
138 if (strcmp(
serial, alternate_serial) == 0)
141 LOG_DEBUG(
"Device alternate serial number '%s' doesn't match requested serial '%s'",
142 alternate_serial,
serial);
144 free(alternate_serial);
149 const char *product,
struct libusb_device_handle **out,
152 int cnt, idx, err_code;
154 bool serial_mismatch =
false;
155 bool product_mismatch =
false;
156 struct libusb_device_handle *libusb_handle =
NULL;
164 for (idx = 0; idx < cnt; idx++) {
165 struct libusb_device_descriptor dev_desc;
167 if (libusb_get_device_descriptor(
devs[idx], &dev_desc) != 0)
176 err_code = libusb_open(
devs[idx], &libusb_handle);
179 LOG_ERROR(
"libusb_open() failed with %s",
180 libusb_error_name(err_code));
187 serial_mismatch =
true;
188 libusb_close(libusb_handle);
194 product_mismatch =
true;
195 libusb_close(libusb_handle);
200 *out = libusb_handle;
202 serial_mismatch =
false;
203 product_mismatch =
false;
207 libusb_free_device_list(
devs, 1);
210 LOG_INFO(
"No device matches the serial string");
212 if (product_mismatch)
213 LOG_INFO(
"No device matches the product string");
230 uint8_t request, uint16_t value, uint16_t index,
char *bytes,
231 uint16_t
size,
unsigned int timeout,
int *transferred)
233 int retval = libusb_control_transfer(dev, request_type, request, value, index,
237 LOG_ERROR(
"libusb_control_transfer error: %s", libusb_error_name(retval));
244 *transferred = retval;
256 ret = libusb_bulk_transfer(dev, ep, (
unsigned char *)bytes,
size,
258 if (ret != LIBUSB_SUCCESS) {
259 LOG_ERROR(
"libusb_bulk_write error: %s", libusb_error_name(ret));
273 ret = libusb_bulk_transfer(dev, ep, (
unsigned char *)bytes,
size,
275 if (ret != LIBUSB_SUCCESS) {
276 LOG_ERROR(
"libusb_bulk_read error: %s", libusb_error_name(ret));
286 struct libusb_device *udev = libusb_get_device(
devh);
289 struct libusb_config_descriptor *
config =
NULL;
290 int current_config = -1;
292 retval = libusb_get_configuration(
devh, ¤t_config);
296 retval = libusb_get_config_descriptor(udev, configuration, &
config);
297 if (retval != 0 || !
config)
303 if (current_config !=
config->bConfigurationValue)
304 retval = libusb_set_configuration(
devh,
config->bConfigurationValue);
306 libusb_free_config_descriptor(
config);
312 unsigned int *usb_read_ep,
313 unsigned int *usb_write_ep,
314 int bclass,
int subclass,
int protocol,
int trans_type)
316 struct libusb_device *udev = libusb_get_device(
devh);
317 const struct libusb_interface *inter;
318 const struct libusb_interface_descriptor *interdesc;
319 const struct libusb_endpoint_descriptor *epdesc;
320 struct libusb_config_descriptor *
config;
322 *usb_read_ep = *usb_write_ep = 0;
324 libusb_get_config_descriptor(udev, 0, &
config);
325 for (
int i = 0; i < (int)
config->bNumInterfaces; i++) {
326 inter = &
config->interface[i];
328 interdesc = &inter->altsetting[0];
330 k < (int)interdesc->bNumEndpoints; k++) {
331 if ((bclass > 0 && interdesc->bInterfaceClass != bclass) ||
332 (subclass > 0 && interdesc->bInterfaceSubClass != subclass) ||
333 (protocol > 0 && interdesc->bInterfaceProtocol != protocol))
336 epdesc = &interdesc->endpoint[k];
337 if (trans_type > 0 && (epdesc->bmAttributes & 0x3) != trans_type)
340 uint8_t epnum = epdesc->bEndpointAddress;
341 bool is_input = epnum & 0x80;
343 is_input ?
"in" :
"out", epnum);
346 *usb_read_ep = epnum;
348 *usb_write_ep = epnum;
350 if (*usb_read_ep && *usb_write_ep) {
351 LOG_DEBUG(
"Claiming interface %d", (
int)interdesc->bInterfaceNumber);
352 libusb_claim_interface(
devh, (
int)interdesc->bInterfaceNumber);
353 libusb_free_config_descriptor(
config);
358 libusb_free_config_descriptor(
config);
365 struct libusb_device_descriptor dev_desc;
367 if (libusb_get_device_descriptor(dev, &dev_desc) == 0) {
368 *pid = dev_desc.idProduct;
388 #if !defined(LIBUSB_API_VERSION) || (LIBUSB_API_VERSION < 0x01000105)
397 return LIBUSB_ERROR_NOT_SUPPORTED;
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len)
const char * adapter_usb_get_location(void)
static const struct device_t * device
static struct jaylink_device_handle * devh
static struct device_config config
static bool jtag_libusb_match_serial(struct libusb_device_handle *device, struct libusb_device_descriptor *dev_desc, const char *serial, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], const char *product, struct libusb_device_handle **out, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
static bool jtag_libusb_location_equal(struct libusb_device *device)
int jtag_libusb_bulk_write(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_handle_events_completed(int *completed)
int jtag_libusb_get_pid(struct libusb_device *dev, uint16_t *pid)
@ DEV_MEM_NOT_YET_DECIDED
@ DEV_MEM_FALLBACK_MALLOC
static bool string_descriptor_equal(struct libusb_device_handle *device, uint8_t str_index, const char *string)
uint8_t * oocd_libusb_dev_mem_alloc(libusb_device_handle *devh, size_t length)
Attempts to allocate a block of persistent DMA memory suitable for transfers against the USB device.
int jtag_libusb_control_transfer(struct libusb_device_handle *dev, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, char *bytes, uint16_t size, unsigned int timeout, int *transferred)
static struct libusb_device ** devs
The usb device list.
static int jtag_libusb_error(int err)
static int libusb_dev_mem_free(libusb_device_handle *devh, uint8_t *buffer, size_t length)
int jtag_libusb_set_configuration(struct libusb_device_handle *devh, int configuration)
static enum @28 dev_mem_allocation
void jtag_libusb_close(struct libusb_device_handle *dev)
int jtag_libusb_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int oocd_libusb_dev_mem_free(libusb_device_handle *devh, uint8_t *buffer, size_t length)
Free device memory allocated with oocd_libusb_dev_mem_alloc().
bool jtag_libusb_match_ids(struct libusb_device_descriptor *dev_desc, const uint16_t vids[], const uint16_t pids[])
static uint8_t * libusb_dev_mem_alloc(libusb_device_handle *devh, size_t length)
static struct libusb_context * jtag_libusb_context
Libusb context.
int jtag_libusb_choose_interface(struct libusb_device_handle *devh, unsigned int *usb_read_ep, unsigned int *usb_write_ep, int bclass, int subclass, int protocol, int trans_type)
Find the first interface optionally matching class, subclass and protocol and claim it.
char *(* adapter_get_alternate_serial_fn)(struct libusb_device_handle *device, struct libusb_device_descriptor *dev_desc)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define ERROR_TIMEOUT_REACHED
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
size_t size
Size of the control block search area.