]> sigrok.org Git - libsigrok.git/blame - src/usb.c
serial: On Windows, include <windows.h> for HANDLE
[libsigrok.git] / src / usb.c
CommitLineData
0c632d36 1/*
50985c20 2 * This file is part of the libsigrok project.
0c632d36
UH
3 *
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdlib.h>
db156e54 23#include <memory.h>
0c632d36
UH
24#include <glib.h>
25#include <libusb.h>
c1aae900 26#include <libsigrok/libsigrok.h>
0c632d36
UH
27#include "libsigrok-internal.h"
28
1953564a 29/* SR_CONF_CONN takes one of these: */
1eb0a0df 30#define CONN_USB_VIDPID "^([0-9a-z]{4})\\.([0-9a-z]{4})$"
0c632d36
UH
31#define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$"
32
3544f848 33#define LOG_PREFIX "usb"
0c632d36 34
c1adbb1e
DE
35#if !HAVE_LIBUSB_OS_HANDLE
36typedef int libusb_os_handle;
37#endif
38
7ae6a758
BV
39/**
40 * Find USB devices according to a connection string.
41 *
42 * @param usb_ctx libusb context to use while scanning.
43 * @param conn Connection string specifying the device(s) to match. This
44 * can be of the form "<bus>.<address>", or "<vendorid>.<productid>".
45 *
46 * @return A GSList of struct sr_usb_dev_inst, with bus and address fields
47 * matching the device that matched the connection string. The GSList and
48 * its contents must be freed by the caller.
49 */
50SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn)
0c632d36 51{
7ae6a758 52 struct sr_usb_dev_inst *usb;
0c632d36
UH
53 struct libusb_device **devlist;
54 struct libusb_device_descriptor des;
55 GSList *devices;
56 GRegex *reg;
57 GMatchInfo *match;
58 int vid, pid, bus, addr, b, a, ret, i;
59 char *mstr;
60
61 vid = pid = bus = addr = 0;
62 reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL);
63 if (g_regex_match(reg, conn, 0, &match)) {
e7f378fd 64 if ((mstr = g_match_info_fetch(match, 1)))
0c632d36 65 vid = strtoul(mstr, NULL, 16);
0c632d36
UH
66 g_free(mstr);
67
e7f378fd 68 if ((mstr = g_match_info_fetch(match, 2)))
0c632d36 69 pid = strtoul(mstr, NULL, 16);
0c632d36
UH
70 g_free(mstr);
71 sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.",
72 vid, pid);
73 } else {
74 g_match_info_unref(match);
75 g_regex_unref(reg);
76 reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL);
77 if (g_regex_match(reg, conn, 0, &match)) {
e7f378fd 78 if ((mstr = g_match_info_fetch(match, 1)))
1eb0a0df 79 bus = strtoul(mstr, NULL, 10);
0c632d36
UH
80 g_free(mstr);
81
e7f378fd 82 if ((mstr = g_match_info_fetch(match, 2)))
1eb0a0df 83 addr = strtoul(mstr, NULL, 10);
0c632d36
UH
84 g_free(mstr);
85 sr_dbg("Trying to find USB device with bus.address = "
86 "%d.%d.", bus, addr);
87 }
88 }
89 g_match_info_unref(match);
90 g_regex_unref(reg);
91
92 if (vid + pid + bus + addr == 0) {
e7f378fd 93 sr_err("Neither VID:PID nor bus.address was specified.");
0c632d36
UH
94 return NULL;
95 }
96
97 if (bus > 64) {
98 sr_err("Invalid bus specified: %d.", bus);
99 return NULL;
100 }
101
102 if (addr > 127) {
103 sr_err("Invalid address specified: %d.", addr);
104 return NULL;
105 }
106
107 /* Looks like a valid USB device specification, but is it connected? */
108 devices = NULL;
109 libusb_get_device_list(usb_ctx, &devlist);
110 for (i = 0; devlist[i]; i++) {
111 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
d4928d71
PS
112 sr_err("Failed to get device descriptor: %s.",
113 libusb_error_name(ret));
0c632d36
UH
114 continue;
115 }
116
7ae6a758 117 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
0c632d36 118 continue;
0c632d36 119
7ae6a758
BV
120 b = libusb_get_bus_number(devlist[i]);
121 a = libusb_get_device_address(devlist[i]);
122 if (bus + addr && (b != bus || a != addr))
0c632d36 123 continue;
0c632d36
UH
124
125 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
126 "%d.%d).", des.idVendor, des.idProduct, b, a);
127
7ae6a758
BV
128 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
129 libusb_get_device_address(devlist[i]), NULL);
130 devices = g_slist_append(devices, usb);
0c632d36
UH
131 }
132 libusb_free_device_list(devlist, 1);
133
134 sr_dbg("Found %d device(s).", g_slist_length(devices));
135
136 return devices;
137}
138
139SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
140{
141 struct libusb_device **devlist;
142 struct libusb_device_descriptor des;
143 int ret, r, cnt, i, a, b;
144
c5f1a021 145 sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address);
0c632d36
UH
146
147 if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
148 sr_err("Failed to retrieve device list: %s.",
149 libusb_error_name(cnt));
150 return SR_ERR;
151 }
152
153 ret = SR_ERR;
154 for (i = 0; i < cnt; i++) {
155 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
156 sr_err("Failed to get device descriptor: %s.",
157 libusb_error_name(r));
158 continue;
159 }
160
161 b = libusb_get_bus_number(devlist[i]);
162 a = libusb_get_device_address(devlist[i]);
c5f1a021 163 if (b != usb->bus || a != usb->address)
0c632d36 164 continue;
0c632d36
UH
165
166 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
167 sr_err("Failed to open device: %s.",
168 libusb_error_name(r));
169 break;
170 }
171
172 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
173 "%d.%d).", des.idVendor, des.idProduct, b, a);
174
175 ret = SR_OK;
176 break;
177 }
178
179 libusb_free_device_list(devlist, 1);
180
181 return ret;
182}
6c60facc 183
67e95ed3
BV
184SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb)
185{
186 libusb_close(usb->devhdl);
187 usb->devhdl = NULL;
188 sr_dbg("Closed USB device %d.%d.", usb->bus, usb->address);
189}
190
534b634c
DE
191static LIBUSB_CALL void usb_pollfd_added(libusb_os_handle fd,
192 short events, void *user_data)
193{
194 struct sr_session *session;
195 gintptr tag;
196
197 session = user_data;
198 tag = (gintptr)session->ctx->libusb_ctx;
199#ifdef G_OS_WIN32
200 events = G_IO_IN;
201#endif
202 sr_session_source_poll_add(session, tag, (gintptr)fd, events);
203}
204
205static LIBUSB_CALL void usb_pollfd_removed(libusb_os_handle fd, void *user_data)
206{
207 struct sr_session *session;
208 gintptr tag;
209
210 session = user_data;
211 tag = (gintptr)session->ctx->libusb_ctx;
212
213 sr_session_source_poll_remove(session, tag, (gintptr)fd);
214}
215
102f1239
BV
216SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
217 int timeout, sr_receive_data_callback cb, void *cb_data)
6c60facc 218{
534b634c
DE
219 const struct libusb_pollfd **pollfds;
220 gintptr tag;
4b9e2532 221 int i;
92248e78 222 int ret;
534b634c 223 int events;
92248e78 224
6640324f
ML
225 if (ctx->usb_source_present) {
226 sr_err("A USB event source is already present.");
227 return SR_ERR;
228 }
534b634c
DE
229 pollfds = libusb_get_pollfds(ctx->libusb_ctx);
230 if (!pollfds) {
92248e78
DE
231 sr_err("Failed to get libusb file descriptors.");
232 return SR_ERR;
233 }
534b634c
DE
234 tag = (gintptr)ctx->libusb_ctx;
235 ret = sr_session_source_add_internal(session,
236 timeout, cb, cb_data, tag);
237
238 ctx->usb_source_present = (ret == SR_OK);
239
240 for (i = 0; ret == SR_OK && pollfds[i]; ++i) {
1b0c6b6d 241#ifdef G_OS_WIN32
534b634c 242 events = G_IO_IN;
1b0c6b6d 243#else
534b634c 244 events = pollfds[i]->events;
1b0c6b6d 245#endif
534b634c
DE
246 ret = sr_session_source_poll_add(session, tag,
247 (gintptr)pollfds[i]->fd, events);
62d7945f 248 }
534b634c
DE
249#if (LIBUSB_API_VERSION >= 0x01000104)
250 libusb_free_pollfds(pollfds);
251#else
252 free(pollfds);
253#endif
254 if (ret != SR_OK)
255 return ret;
4b9e2532 256
534b634c
DE
257 libusb_set_pollfd_notifiers(ctx->libusb_ctx,
258 &usb_pollfd_added, &usb_pollfd_removed, session);
6c60facc 259
534b634c 260 return SR_OK;
6c60facc
ML
261}
262
102f1239 263SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx)
6c60facc 264{
534b634c
DE
265 ctx->usb_source_present = FALSE;
266
267 libusb_set_pollfd_notifiers(ctx->libusb_ctx, NULL, NULL, NULL);
268
5a7d8dac 269 return sr_session_source_remove_internal(session,
92248e78 270 (gintptr)ctx->libusb_ctx);
6c60facc 271}
db156e54 272
76bc5f63 273SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len)
db156e54 274{
76bc5f63
AJ
275 uint8_t port_numbers[8];
276 int i, n, len;
277
19643b96
UJ
278/*
279 * FreeBSD requires that devices prior to calling libusb_get_port_numbers()
280 * have been opened with libusb_open().
281 */
282#ifdef __FreeBSD__
283 struct libusb_device_handle *devh;
284 if (libusb_open(dev, &devh) != 0)
285 return SR_ERR;
286#endif
76bc5f63 287 n = libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers));
19643b96
UJ
288#ifdef __FreeBSD__
289 libusb_close(devh);
290#endif
76bc5f63 291
19643b96
UJ
292/* Workaround FreeBSD libusb_get_port_numbers() returning 0. */
293#ifdef __FreeBSD__
294 if (n == 0) {
295 port_numbers[0] = libusb_get_device_address(dev);
296 n = 1;
297 }
298#endif
2f004b4b
VP
299 if (n < 1)
300 return SR_ERR;
301
76bc5f63
AJ
302 len = snprintf(path, path_len, "usb/%d-%d",
303 libusb_get_bus_number(dev), port_numbers[0]);
db156e54 304
76bc5f63
AJ
305 for (i = 1; i < n; i++)
306 len += snprintf(path+len, path_len-len, ".%d", port_numbers[i]);
db156e54
SA
307
308 return SR_OK;
309}