]>
Commit | Line | Data |
---|---|---|
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> | |
23 | #include <glib.h> | |
24 | #include <libusb.h> | |
25 | #include "libsigrok.h" | |
26 | #include "libsigrok-internal.h" | |
27 | ||
1953564a | 28 | /* SR_CONF_CONN takes one of these: */ |
1eb0a0df | 29 | #define CONN_USB_VIDPID "^([0-9a-z]{4})\\.([0-9a-z]{4})$" |
0c632d36 UH |
30 | #define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$" |
31 | ||
3544f848 | 32 | #define LOG_PREFIX "usb" |
0c632d36 | 33 | |
7ae6a758 BV |
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) | |
0c632d36 | 46 | { |
7ae6a758 | 47 | struct sr_usb_dev_inst *usb; |
0c632d36 UH |
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)) { | |
e7f378fd | 59 | if ((mstr = g_match_info_fetch(match, 1))) |
0c632d36 | 60 | vid = strtoul(mstr, NULL, 16); |
0c632d36 UH |
61 | g_free(mstr); |
62 | ||
e7f378fd | 63 | if ((mstr = g_match_info_fetch(match, 2))) |
0c632d36 | 64 | pid = strtoul(mstr, NULL, 16); |
0c632d36 UH |
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)) { | |
e7f378fd | 73 | if ((mstr = g_match_info_fetch(match, 1))) |
1eb0a0df | 74 | bus = strtoul(mstr, NULL, 10); |
0c632d36 UH |
75 | g_free(mstr); |
76 | ||
e7f378fd | 77 | if ((mstr = g_match_info_fetch(match, 2))) |
1eb0a0df | 78 | addr = strtoul(mstr, NULL, 10); |
0c632d36 UH |
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) { | |
e7f378fd | 88 | sr_err("Neither VID:PID nor bus.address was specified."); |
0c632d36 UH |
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))) { | |
d4928d71 PS |
107 | sr_err("Failed to get device descriptor: %s.", |
108 | libusb_error_name(ret)); | |
0c632d36 UH |
109 | continue; |
110 | } | |
111 | ||
7ae6a758 | 112 | if (vid + pid && (des.idVendor != vid || des.idProduct != pid)) |
0c632d36 | 113 | continue; |
0c632d36 | 114 | |
7ae6a758 BV |
115 | b = libusb_get_bus_number(devlist[i]); |
116 | a = libusb_get_device_address(devlist[i]); | |
117 | if (bus + addr && (b != bus || a != addr)) | |
0c632d36 | 118 | continue; |
0c632d36 UH |
119 | |
120 | sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = " | |
121 | "%d.%d).", des.idVendor, des.idProduct, b, a); | |
122 | ||
7ae6a758 BV |
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); | |
0c632d36 UH |
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 | ||
c5f1a021 | 140 | sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address); |
0c632d36 UH |
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]); | |
c5f1a021 | 158 | if (b != usb->bus || a != usb->address) |
0c632d36 | 159 | continue; |
0c632d36 UH |
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 | } | |
6c60facc | 178 | |
5321ac6b | 179 | #ifdef _WIN32 |
dcc94340 | 180 | static gpointer usb_thread(gpointer data) |
5321ac6b ML |
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 | ||
dcc94340 | 195 | static int usb_callback(int fd, int revents, void *cb_data) |
5321ac6b ML |
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); | |
34ea7f96 ML |
202 | |
203 | if (ctx->usb_thread_running) { | |
204 | ResetEvent(ctx->usb_event); | |
205 | g_mutex_unlock(&ctx->usb_mutex); | |
206 | } | |
5321ac6b ML |
207 | |
208 | return ret; | |
209 | } | |
210 | #endif | |
211 | ||
6c60facc | 212 | SR_PRIV int usb_source_add(struct sr_context *ctx, int timeout, |
144f6660 | 213 | sr_receive_data_callback cb, void *cb_data) |
6c60facc | 214 | { |
6640324f ML |
215 | if (ctx->usb_source_present) { |
216 | sr_err("A USB event source is already present."); | |
217 | return SR_ERR; | |
218 | } | |
219 | ||
6c60facc | 220 | #ifdef _WIN32 |
5321ac6b ML |
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(&ctx->usb_pollfd, timeout, usb_callback, ctx); | |
6c60facc ML |
230 | #else |
231 | const struct libusb_pollfd **lupfd; | |
232 | unsigned int i; | |
233 | ||
234 | lupfd = libusb_get_pollfds(ctx->libusb_ctx); | |
235 | for (i = 0; lupfd[i]; i++) | |
236 | sr_source_add(lupfd[i]->fd, lupfd[i]->events, timeout, cb, cb_data); | |
237 | free(lupfd); | |
5321ac6b | 238 | #endif |
6640324f | 239 | ctx->usb_source_present = TRUE; |
6c60facc ML |
240 | |
241 | return SR_OK; | |
6c60facc ML |
242 | } |
243 | ||
244 | SR_PRIV int usb_source_remove(struct sr_context *ctx) | |
245 | { | |
6640324f ML |
246 | if (!ctx->usb_source_present) |
247 | return SR_OK; | |
248 | ||
6c60facc | 249 | #ifdef _WIN32 |
5321ac6b | 250 | ctx->usb_thread_running = FALSE; |
b5328e1d | 251 | g_mutex_unlock(&ctx->usb_mutex); |
5321ac6b ML |
252 | libusb_unlock_events(ctx->libusb_ctx); |
253 | g_thread_join(ctx->usb_thread); | |
254 | g_mutex_clear(&ctx->usb_mutex); | |
255 | sr_session_source_remove_pollfd(&ctx->usb_pollfd); | |
256 | CloseHandle(ctx->usb_event); | |
6c60facc ML |
257 | #else |
258 | const struct libusb_pollfd **lupfd; | |
259 | unsigned int i; | |
260 | ||
261 | lupfd = libusb_get_pollfds(ctx->libusb_ctx); | |
262 | for (i = 0; lupfd[i]; i++) | |
263 | sr_source_remove(lupfd[i]->fd); | |
264 | free(lupfd); | |
5321ac6b | 265 | #endif |
6640324f | 266 | ctx->usb_source_present = FALSE; |
6c60facc ML |
267 | |
268 | return SR_OK; | |
6c60facc | 269 | } |