]> sigrok.org Git - libsigrok.git/blob - src/usb.c
Reorganize project tree.
[libsigrok.git] / src / usb.c
1 /*
2  * This file is part of the libsigrok project.
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>
23 #include <glib.h>
24 #include <libusb.h>
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
27
28 /* SR_CONF_CONN takes one of these: */
29 #define CONN_USB_VIDPID  "^([0-9a-z]{4})\\.([0-9a-z]{4})$"
30 #define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$"
31
32 #define LOG_PREFIX "usb"
33
34 /**
35  * Find USB devices according to a connection string.
36  *
37  * @param usb_ctx libusb context to use while scanning.
38  * @param conn Connection string specifying the device(s) to match. This
39  * can be of the form "<bus>.<address>", or "<vendorid>.<productid>".
40  *
41  * @return A GSList of struct sr_usb_dev_inst, with bus and address fields
42  * matching the device that matched the connection string. The GSList and
43  * its contents must be freed by the caller.
44  */
45 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn)
46 {
47         struct sr_usb_dev_inst *usb;
48         struct libusb_device **devlist;
49         struct libusb_device_descriptor des;
50         GSList *devices;
51         GRegex *reg;
52         GMatchInfo *match;
53         int vid, pid, bus, addr, b, a, ret, i;
54         char *mstr;
55
56         vid = pid = bus = addr = 0;
57         reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL);
58         if (g_regex_match(reg, conn, 0, &match)) {
59                 if ((mstr = g_match_info_fetch(match, 1)))
60                         vid = strtoul(mstr, NULL, 16);
61                 g_free(mstr);
62
63                 if ((mstr = g_match_info_fetch(match, 2)))
64                         pid = strtoul(mstr, NULL, 16);
65                 g_free(mstr);
66                 sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.",
67                        vid, pid);
68         } else {
69                 g_match_info_unref(match);
70                 g_regex_unref(reg);
71                 reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL);
72                 if (g_regex_match(reg, conn, 0, &match)) {
73                         if ((mstr = g_match_info_fetch(match, 1)))
74                                 bus = strtoul(mstr, NULL, 10);
75                         g_free(mstr);
76
77                         if ((mstr = g_match_info_fetch(match, 2)))
78                                 addr = strtoul(mstr, NULL, 10);
79                         g_free(mstr);
80                         sr_dbg("Trying to find USB device with bus.address = "
81                                "%d.%d.", bus, addr);
82                 }
83         }
84         g_match_info_unref(match);
85         g_regex_unref(reg);
86
87         if (vid + pid + bus + addr == 0) {
88                 sr_err("Neither VID:PID nor bus.address was specified.");
89                 return NULL;
90         }
91
92         if (bus > 64) {
93                 sr_err("Invalid bus specified: %d.", bus);
94                 return NULL;
95         }
96
97         if (addr > 127) {
98                 sr_err("Invalid address specified: %d.", addr);
99                 return NULL;
100         }
101
102         /* Looks like a valid USB device specification, but is it connected? */
103         devices = NULL;
104         libusb_get_device_list(usb_ctx, &devlist);
105         for (i = 0; devlist[i]; i++) {
106                 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
107                         sr_err("Failed to get device descriptor: %s.",
108                                libusb_error_name(ret));
109                         continue;
110                 }
111
112                 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
113                         continue;
114
115                 b = libusb_get_bus_number(devlist[i]);
116                 a = libusb_get_device_address(devlist[i]);
117                 if (bus + addr && (b != bus || a != addr))
118                         continue;
119
120                 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
121                        "%d.%d).", des.idVendor, des.idProduct, b, a);
122
123                 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
124                                 libusb_get_device_address(devlist[i]), NULL);
125                 devices = g_slist_append(devices, usb);
126         }
127         libusb_free_device_list(devlist, 1);
128
129         sr_dbg("Found %d device(s).", g_slist_length(devices));
130         
131         return devices;
132 }
133
134 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
135 {
136         struct libusb_device **devlist;
137         struct libusb_device_descriptor des;
138         int ret, r, cnt, i, a, b;
139
140         sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address);
141
142         if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
143                 sr_err("Failed to retrieve device list: %s.",
144                        libusb_error_name(cnt));
145                 return SR_ERR;
146         }
147
148         ret = SR_ERR;
149         for (i = 0; i < cnt; i++) {
150                 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
151                         sr_err("Failed to get device descriptor: %s.",
152                                libusb_error_name(r));
153                         continue;
154                 }
155
156                 b = libusb_get_bus_number(devlist[i]);
157                 a = libusb_get_device_address(devlist[i]);
158                 if (b != usb->bus || a != usb->address)
159                         continue;
160
161                 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
162                         sr_err("Failed to open device: %s.",
163                                libusb_error_name(r));
164                         break;
165                 }
166
167                 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
168                        "%d.%d).", des.idVendor, des.idProduct, b, a);
169
170                 ret = SR_OK;
171                 break;
172         }
173
174         libusb_free_device_list(devlist, 1);
175
176         return ret;
177 }
178
179 #ifdef _WIN32
180 static gpointer usb_thread(gpointer data)
181 {
182         struct sr_context *ctx = data;
183
184         while (ctx->usb_thread_running) {
185                 g_mutex_lock(&ctx->usb_mutex);
186                 libusb_wait_for_event(ctx->libusb_ctx, NULL);
187                 SetEvent(ctx->usb_event);
188                 g_mutex_unlock(&ctx->usb_mutex);
189                 g_thread_yield();
190         }
191
192         return NULL;
193 }
194
195 static int usb_callback(int fd, int revents, void *cb_data)
196 {
197         struct sr_context *ctx = cb_data;
198         int ret;
199
200         g_mutex_lock(&ctx->usb_mutex);
201         ret = ctx->usb_cb(fd, revents, ctx->usb_cb_data);
202
203         if (ctx->usb_thread_running) {
204                 ResetEvent(ctx->usb_event);
205                 g_mutex_unlock(&ctx->usb_mutex);
206         }
207
208         return ret;
209 }
210 #endif
211
212 SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
213                 int timeout, sr_receive_data_callback cb, void *cb_data)
214 {
215         if (ctx->usb_source_present) {
216                 sr_err("A USB event source is already present.");
217                 return SR_ERR;
218         }
219
220 #ifdef _WIN32
221         ctx->usb_event = CreateEvent(NULL, TRUE, FALSE, NULL);
222         g_mutex_init(&ctx->usb_mutex);
223         ctx->usb_thread_running = TRUE;
224         ctx->usb_thread = g_thread_new("usb", usb_thread, ctx);
225         ctx->usb_pollfd.fd = ctx->usb_event;
226         ctx->usb_pollfd.events = G_IO_IN;
227         ctx->usb_cb = cb;
228         ctx->usb_cb_data = cb_data;
229         sr_session_source_add_pollfd(session, &ctx->usb_pollfd, timeout,
230                         usb_callback, ctx);
231 #else
232         const struct libusb_pollfd **lupfd;
233         unsigned int i;
234
235         lupfd = libusb_get_pollfds(ctx->libusb_ctx);
236         for (i = 0; lupfd[i]; i++)
237                 sr_session_source_add(session, lupfd[i]->fd, lupfd[i]->events,
238                                 timeout, cb, cb_data);
239         free(lupfd);
240 #endif
241         ctx->usb_source_present = TRUE;
242
243         return SR_OK;
244 }
245
246 SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx)
247 {
248         if (!ctx->usb_source_present)
249                 return SR_OK;
250
251 #ifdef _WIN32
252         ctx->usb_thread_running = FALSE;
253         g_mutex_unlock(&ctx->usb_mutex);
254         libusb_unlock_events(ctx->libusb_ctx);
255         g_thread_join(ctx->usb_thread);
256         g_mutex_clear(&ctx->usb_mutex);
257         sr_session_source_remove_pollfd(session, &ctx->usb_pollfd);
258         CloseHandle(ctx->usb_event);
259 #else
260         const struct libusb_pollfd **lupfd;
261         unsigned int i;
262
263         lupfd = libusb_get_pollfds(ctx->libusb_ctx);
264         for (i = 0; lupfd[i]; i++)
265                 sr_session_source_remove(session, lupfd[i]->fd);
266         free(lupfd);
267 #endif
268         ctx->usb_source_present = FALSE;
269
270         return SR_OK;
271 }