2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2016 Marc Schink <jaylink-dev@marcschink.de>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
26 #include "libjaylink.h"
27 #include "libjaylink-internal.h"
30 * libusb.h includes windows.h and therefore must be included after anything
31 * that includes winsock2.h.
38 * Device discovery (USB).
42 /** USB Vendor ID (VID) of SEGGER products. */
43 #define USB_VENDOR_ID 0x1366
45 /* USB Product IDs (PID) and their corresponding USB addresses. */
46 static const uint16_t pids
[][2] = {
67 /** Maximum length of the USB string descriptor for the serial number. */
68 #define USB_SERIAL_NUMBER_LENGTH 12
71 * Maximum number of digits in a serial number
73 * The serial number of a device consists of at most 9 digits but user defined
74 * serial numbers are allowed with up to 10 digits.
76 #define MAX_SERIAL_NUMBER_DIGITS 10
79 static bool parse_serial_number(const char *str
, uint32_t *serial_number
)
86 * Skip the first digits which are not part of a valid serial number.
87 * This is necessary because some devices erroneously use random digits
88 * instead of zeros for padding.
90 if (length
> MAX_SERIAL_NUMBER_DIGITS
)
91 str
= str
+ (length
- MAX_SERIAL_NUMBER_DIGITS
);
93 if (jaylink_parse_serial_number(str
, serial_number
) != JAYLINK_OK
)
99 static bool compare_devices(const void *a
, const void *b
)
101 const struct jaylink_device
*dev
;
102 const struct libusb_device
*usb_dev
;
107 if (dev
->iface
!= JAYLINK_HIF_USB
)
110 if (dev
->usb_dev
== usb_dev
)
116 static struct jaylink_device
*find_device(const struct jaylink_context
*ctx
,
117 const struct libusb_device
*usb_dev
)
121 item
= list_find_custom(ctx
->devs
, &compare_devices
, usb_dev
);
129 static struct jaylink_device
*probe_device(struct jaylink_context
*ctx
,
130 struct libusb_device
*usb_dev
)
133 struct libusb_device_descriptor desc
;
134 struct libusb_device_handle
*usb_devh
;
135 struct jaylink_device
*dev
;
136 char buf
[USB_SERIAL_NUMBER_LENGTH
+ 1];
138 uint32_t serial_number
;
139 bool valid_serial_number
;
143 ret
= libusb_get_device_descriptor(usb_dev
, &desc
);
145 if (ret
!= LIBUSB_SUCCESS
) {
146 log_warn(ctx
, "Failed to get device descriptor: %s.",
147 libusb_error_name(ret
));
151 if (desc
.idVendor
!= USB_VENDOR_ID
)
154 found_device
= false;
156 for (i
= 0; i
< sizeof(pids
) / sizeof(pids
[0]); i
++) {
157 if (pids
[i
][0] == desc
.idProduct
) {
159 usb_address
= pids
[i
][1];
167 log_dbg(ctx
, "Found device (VID:PID = %04x:%04x, bus:address = "
168 "%03u:%03u).", desc
.idVendor
, desc
.idProduct
,
169 libusb_get_bus_number(usb_dev
),
170 libusb_get_device_address(usb_dev
));
173 * Search for an already allocated device instance for this device and
174 * if found return a reference to it.
176 dev
= find_device(ctx
, usb_dev
);
179 log_dbg(ctx
, "Device: USB address = %u.", dev
->usb_address
);
181 if (dev
->valid_serial_number
)
182 log_dbg(ctx
, "Device: Serial number = %u.",
185 log_dbg(ctx
, "Device: Serial number = N/A.");
187 log_dbg(ctx
, "Using existing device instance.");
188 return jaylink_ref_device(dev
);
191 /* Open the device to be able to retrieve its serial number. */
192 ret
= libusb_open(usb_dev
, &usb_devh
);
194 if (ret
!= LIBUSB_SUCCESS
) {
195 log_warn(ctx
, "Failed to open device: %s.",
196 libusb_error_name(ret
));
201 valid_serial_number
= true;
203 ret
= libusb_get_string_descriptor_ascii(usb_devh
, desc
.iSerialNumber
,
204 (unsigned char *)buf
, USB_SERIAL_NUMBER_LENGTH
+ 1);
206 libusb_close(usb_devh
);
209 log_warn(ctx
, "Failed to retrieve serial number: %s.",
210 libusb_error_name(ret
));
211 valid_serial_number
= false;
214 if (valid_serial_number
) {
215 if (!parse_serial_number(buf
, &serial_number
)) {
216 log_warn(ctx
, "Failed to parse serial number.");
221 log_dbg(ctx
, "Device: USB address = %u.", usb_address
);
223 if (valid_serial_number
)
224 log_dbg(ctx
, "Device: Serial number = %u.", serial_number
);
226 log_dbg(ctx
, "Device: Serial number = N/A.");
228 log_dbg(ctx
, "Allocating new device instance.");
230 dev
= device_allocate(ctx
);
233 log_warn(ctx
, "Device instance malloc failed.");
237 dev
->iface
= JAYLINK_HIF_USB
;
238 dev
->usb_dev
= libusb_ref_device(usb_dev
);
239 dev
->usb_address
= usb_address
;
240 dev
->serial_number
= serial_number
;
241 dev
->valid_serial_number
= valid_serial_number
;
246 JAYLINK_PRIV
int discovery_usb_scan(struct jaylink_context
*ctx
)
249 struct libusb_device
**devs
;
250 struct jaylink_device
*dev
;
254 ret
= libusb_get_device_list(ctx
->usb_ctx
, &devs
);
256 if (ret
== LIBUSB_ERROR_IO
) {
257 log_err(ctx
, "Failed to retrieve device list: input/output "
259 return JAYLINK_ERR_IO
;
260 } else if (ret
< 0) {
261 log_err(ctx
, "Failed to retrieve device list: %s.",
262 libusb_error_name(ret
));
268 for (i
= 0; devs
[i
]; i
++) {
269 dev
= probe_device(ctx
, devs
[i
]);
274 ctx
->discovered_devs
= list_prepend(ctx
->discovered_devs
, dev
);
278 libusb_free_device_list(devs
, true);
279 log_dbg(ctx
, "Found %zu USB device(s).", num
);