]>
Commit | Line | Data |
---|---|---|
ac3898d2 BV |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * | |
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 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
20 | #include <glib.h> | |
7a360375 BV |
21 | #include <libusb.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
fa773062 UH |
24 | #include "libsigrok.h" |
25 | #include "libsigrok-internal.h" | |
26 | #include "protocol.h" | |
7a360375 BV |
27 | |
28 | #define VICTOR_VID 0x1244 | |
29 | #define VICTOR_PID 0xd237 | |
30 | #define VICTOR_VENDOR "Victor" | |
31 | #define VICTOR_INTERFACE 0 | |
32 | #define VICTOR_ENDPOINT LIBUSB_ENDPOINT_IN | 1 | |
ac3898d2 BV |
33 | |
34 | SR_PRIV struct sr_dev_driver victor_dmm_driver_info; | |
35 | static struct sr_dev_driver *di = &victor_dmm_driver_info; | |
6078d2c9 | 36 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); |
7a360375 | 37 | |
ac046ef8 BV |
38 | static const int32_t hwopts[] = { |
39 | SR_CONF_CONN, | |
40 | }; | |
41 | ||
a59b4eef | 42 | static const int32_t hwcaps[] = { |
1953564a BV |
43 | SR_CONF_MULTIMETER, |
44 | SR_CONF_LIMIT_MSEC, | |
45 | SR_CONF_LIMIT_SAMPLES, | |
46 | SR_CONF_CONTINUOUS, | |
7a360375 BV |
47 | }; |
48 | ||
3b412e3a | 49 | static int dev_clear(void) |
ac3898d2 | 50 | { |
8d18d266 | 51 | return std_dev_clear(di, NULL); |
ac3898d2 BV |
52 | } |
53 | ||
6078d2c9 | 54 | static int init(struct sr_context *sr_ctx) |
ac3898d2 | 55 | { |
f6beaac5 | 56 | return std_init(sr_ctx, di, LOG_PREFIX); |
ac3898d2 BV |
57 | } |
58 | ||
6078d2c9 | 59 | static GSList *scan(GSList *options) |
ac3898d2 BV |
60 | { |
61 | struct drv_context *drvc; | |
7a360375 BV |
62 | struct dev_context *devc; |
63 | struct sr_dev_inst *sdi; | |
64 | struct sr_probe *probe; | |
65 | struct libusb_device_descriptor des; | |
66 | libusb_device **devlist; | |
ac3898d2 | 67 | GSList *devices; |
7a360375 | 68 | int ret, devcnt, i; |
ac3898d2 BV |
69 | |
70 | (void)options; | |
71 | ||
4b97c74e | 72 | drvc = di->priv; |
7a360375 | 73 | |
ac3898d2 | 74 | devices = NULL; |
d4abb463 | 75 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
7a360375 BV |
76 | for (i = 0; devlist[i]; i++) { |
77 | if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) { | |
78 | sr_warn("Failed to get device descriptor: %s", | |
79 | libusb_error_name(ret)); | |
80 | continue; | |
81 | } | |
ac3898d2 | 82 | |
7a360375 BV |
83 | if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID) |
84 | continue; | |
85 | ||
86 | devcnt = g_slist_length(drvc->instances); | |
fa773062 UH |
87 | if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, |
88 | VICTOR_VENDOR, NULL, NULL))) | |
7a360375 BV |
89 | return NULL; |
90 | sdi->driver = di; | |
91 | ||
92 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) | |
93 | return NULL; | |
94 | sdi->priv = devc; | |
95 | ||
96 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
97 | return NULL; | |
98 | sdi->probes = g_slist_append(NULL, probe); | |
99 | ||
ac046ef8 | 100 | if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), |
7a360375 BV |
101 | libusb_get_device_address(devlist[i]), NULL))) |
102 | return NULL; | |
ac046ef8 | 103 | sdi->inst_type = SR_INST_USB; |
7a360375 BV |
104 | |
105 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
106 | devices = g_slist_append(devices, sdi); | |
107 | } | |
108 | libusb_free_device_list(devlist, 1); | |
ac3898d2 BV |
109 | |
110 | return devices; | |
111 | } | |
112 | ||
6078d2c9 | 113 | static GSList *dev_list(void) |
ac3898d2 | 114 | { |
0e94d524 | 115 | return ((struct drv_context *)(di->priv))->instances; |
ac3898d2 BV |
116 | } |
117 | ||
6078d2c9 | 118 | static int dev_open(struct sr_dev_inst *sdi) |
ac3898d2 | 119 | { |
d4abb463 | 120 | struct drv_context *drvc = di->priv; |
ac046ef8 | 121 | struct sr_usb_dev_inst *usb; |
7a360375 BV |
122 | libusb_device **devlist; |
123 | int ret, i; | |
124 | ||
125 | if (!di->priv) { | |
126 | sr_err("Driver was not initialized."); | |
127 | return SR_ERR; | |
128 | } | |
129 | ||
ac046ef8 BV |
130 | usb = sdi->conn; |
131 | ||
d4abb463 | 132 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
7a360375 | 133 | for (i = 0; devlist[i]; i++) { |
ac046ef8 BV |
134 | if (libusb_get_bus_number(devlist[i]) != usb->bus |
135 | || libusb_get_device_address(devlist[i]) != usb->address) | |
7a360375 | 136 | continue; |
ac046ef8 | 137 | if ((ret = libusb_open(devlist[i], &usb->devhdl))) { |
fa773062 | 138 | sr_err("Failed to open device: %s.", libusb_error_name(ret)); |
7a360375 BV |
139 | return SR_ERR; |
140 | } | |
141 | break; | |
142 | } | |
143 | libusb_free_device_list(devlist, 1); | |
144 | if (!devlist[i]) { | |
145 | sr_err("Device not found."); | |
146 | return SR_ERR; | |
147 | } | |
148 | ||
149 | /* The device reports as HID class, so the kernel would have | |
150 | * claimed it. */ | |
ac046ef8 BV |
151 | if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { |
152 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { | |
d4928d71 PS |
153 | sr_err("Failed to detach kernel driver: %s.", |
154 | libusb_error_name(ret)); | |
7a360375 BV |
155 | return SR_ERR; |
156 | } | |
157 | } | |
158 | ||
ac046ef8 | 159 | if ((ret = libusb_claim_interface(usb->devhdl, |
7a360375 | 160 | VICTOR_INTERFACE))) { |
fa773062 | 161 | sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); |
7a360375 BV |
162 | return SR_ERR; |
163 | } | |
164 | sdi->status = SR_ST_ACTIVE; | |
ac3898d2 BV |
165 | |
166 | return SR_OK; | |
167 | } | |
168 | ||
6078d2c9 | 169 | static int dev_close(struct sr_dev_inst *sdi) |
ac3898d2 | 170 | { |
ac046ef8 | 171 | struct sr_usb_dev_inst *usb; |
7a360375 BV |
172 | |
173 | if (!di->priv) { | |
174 | sr_err("Driver was not initialized."); | |
175 | return SR_ERR; | |
176 | } | |
177 | ||
ac046ef8 BV |
178 | usb = sdi->conn; |
179 | ||
180 | if (!usb->devhdl) | |
7a360375 BV |
181 | /* Nothing to do. */ |
182 | return SR_OK; | |
183 | ||
ac046ef8 BV |
184 | libusb_release_interface(usb->devhdl, VICTOR_INTERFACE); |
185 | libusb_close(usb->devhdl); | |
186 | usb->devhdl = NULL; | |
7a360375 | 187 | sdi->status = SR_ST_INACTIVE; |
ac3898d2 BV |
188 | |
189 | return SR_OK; | |
190 | } | |
191 | ||
6078d2c9 | 192 | static int cleanup(void) |
ac3898d2 | 193 | { |
8d18d266 | 194 | int ret; |
7a360375 BV |
195 | struct drv_context *drvc; |
196 | ||
197 | if (!(drvc = di->priv)) | |
198 | /* Can get called on an unused driver, doesn't matter. */ | |
199 | return SR_OK; | |
ac3898d2 | 200 | |
3b412e3a | 201 | ret = dev_clear(); |
7a360375 BV |
202 | g_free(drvc); |
203 | di->priv = NULL; | |
ac3898d2 | 204 | |
8d18d266 | 205 | return ret; |
ac3898d2 BV |
206 | } |
207 | ||
8f996b89 ML |
208 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, |
209 | const struct sr_probe_group *probe_group) | |
ac046ef8 BV |
210 | { |
211 | struct sr_usb_dev_inst *usb; | |
212 | char str[128]; | |
213 | ||
8f996b89 ML |
214 | (void)probe_group; |
215 | ||
ac046ef8 BV |
216 | switch (id) { |
217 | case SR_CONF_CONN: | |
218 | if (!sdi || !sdi->conn) | |
219 | return SR_ERR_ARG; | |
220 | usb = sdi->conn; | |
221 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
222 | *data = g_variant_new_string(str); | |
223 | break; | |
224 | default: | |
225 | return SR_ERR_NA; | |
226 | } | |
227 | ||
228 | return SR_OK; | |
229 | } | |
230 | ||
8f996b89 ML |
231 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, |
232 | const struct sr_probe_group *probe_group) | |
ac3898d2 | 233 | { |
7a360375 BV |
234 | struct dev_context *devc; |
235 | gint64 now; | |
ac3898d2 BV |
236 | int ret; |
237 | ||
8f996b89 ML |
238 | (void)probe_group; |
239 | ||
e73ffd42 BV |
240 | if (sdi->status != SR_ST_ACTIVE) |
241 | return SR_ERR_DEV_CLOSED; | |
242 | ||
7a360375 BV |
243 | if (!di->priv) { |
244 | sr_err("Driver was not initialized."); | |
245 | return SR_ERR; | |
246 | } | |
247 | ||
7a360375 | 248 | devc = sdi->priv; |
ac3898d2 | 249 | ret = SR_OK; |
035a1078 | 250 | switch (id) { |
1953564a | 251 | case SR_CONF_LIMIT_MSEC: |
a59b4eef | 252 | devc->limit_msec = g_variant_get_uint64(data); |
fa773062 UH |
253 | now = g_get_monotonic_time() / 1000; |
254 | devc->end_time = now + devc->limit_msec; | |
255 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
256 | devc->limit_msec); | |
257 | break; | |
1953564a | 258 | case SR_CONF_LIMIT_SAMPLES: |
a59b4eef | 259 | devc->limit_samples = g_variant_get_uint64(data); |
fa773062 UH |
260 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
261 | devc->limit_samples); | |
262 | break; | |
ac3898d2 | 263 | default: |
bd6fbf62 | 264 | ret = SR_ERR_NA; |
ac3898d2 BV |
265 | } |
266 | ||
267 | return ret; | |
268 | } | |
269 | ||
8f996b89 ML |
270 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
271 | const struct sr_probe_group *probe_group) | |
a1c743fc | 272 | { |
a1c743fc | 273 | (void)sdi; |
8f996b89 | 274 | (void)probe_group; |
a1c743fc BV |
275 | |
276 | switch (key) { | |
ac046ef8 BV |
277 | case SR_CONF_SCAN_OPTIONS: |
278 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
279 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
280 | break; | |
9a6517d1 | 281 | case SR_CONF_DEVICE_OPTIONS: |
a59b4eef BV |
282 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
283 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 284 | break; |
a1c743fc | 285 | default: |
bd6fbf62 | 286 | return SR_ERR_NA; |
a1c743fc BV |
287 | } |
288 | ||
289 | return SR_OK; | |
290 | } | |
291 | ||
7a360375 BV |
292 | static void receive_transfer(struct libusb_transfer *transfer) |
293 | { | |
294 | struct dev_context *devc; | |
295 | struct sr_dev_inst *sdi; | |
296 | int ret; | |
297 | ||
298 | sdi = transfer->user_data; | |
299 | devc = sdi->priv; | |
300 | if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) { | |
301 | /* USB device was unplugged. */ | |
6078d2c9 | 302 | dev_acquisition_stop(sdi, sdi); |
7a360375 | 303 | } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { |
fa773062 | 304 | sr_dbg("Got %d-byte packet.", transfer->actual_length); |
ff945683 BV |
305 | if (transfer->actual_length == DMM_DATA_SIZE) { |
306 | victor_dmm_receive_data(sdi, transfer->buffer); | |
7a360375 BV |
307 | if (devc->limit_samples) { |
308 | if (devc->num_samples >= devc->limit_samples) | |
6078d2c9 | 309 | dev_acquisition_stop(sdi, sdi); |
7a360375 BV |
310 | } |
311 | } | |
312 | } | |
313 | /* Anything else is either an error or a timeout, which is fine: | |
314 | * we were just going to send another transfer request anyway. */ | |
315 | ||
316 | if (sdi->status == SR_ST_ACTIVE) { | |
317 | /* Send the same request again. */ | |
318 | if ((ret = libusb_submit_transfer(transfer) != 0)) { | |
fa773062 UH |
319 | sr_err("Unable to resubmit transfer: %s.", |
320 | libusb_error_name(ret)); | |
7a360375 | 321 | g_free(transfer->buffer); |
9ec7ff94 | 322 | libusb_free_transfer(transfer); |
6078d2c9 | 323 | dev_acquisition_stop(sdi, sdi); |
7a360375 BV |
324 | } |
325 | } else { | |
326 | /* This was the last transfer we're going to receive, so | |
327 | * clean up now. */ | |
7a360375 | 328 | g_free(transfer->buffer); |
9ec7ff94 | 329 | libusb_free_transfer(transfer); |
7a360375 | 330 | } |
7a360375 BV |
331 | } |
332 | ||
333 | static int handle_events(int fd, int revents, void *cb_data) | |
334 | { | |
335 | struct dev_context *devc; | |
d4abb463 | 336 | struct drv_context *drvc = di->priv; |
7a360375 BV |
337 | struct sr_datafeed_packet packet; |
338 | struct sr_dev_inst *sdi; | |
339 | struct timeval tv; | |
340 | gint64 now; | |
341 | int i; | |
342 | ||
343 | (void)fd; | |
344 | (void)revents; | |
345 | ||
346 | sdi = cb_data; | |
347 | devc = sdi->priv; | |
348 | ||
349 | if (devc->limit_msec) { | |
350 | now = g_get_monotonic_time() / 1000; | |
351 | if (now > devc->end_time) | |
6078d2c9 | 352 | dev_acquisition_stop(sdi, sdi); |
7a360375 BV |
353 | } |
354 | ||
355 | if (sdi->status == SR_ST_STOPPING) { | |
356 | for (i = 0; devc->usbfd[i] != -1; i++) | |
357 | sr_source_remove(devc->usbfd[i]); | |
358 | ||
6078d2c9 | 359 | dev_close(sdi); |
7a360375 BV |
360 | |
361 | packet.type = SR_DF_END; | |
362 | sr_session_send(cb_data, &packet); | |
363 | } | |
364 | ||
365 | memset(&tv, 0, sizeof(struct timeval)); | |
d4abb463 PS |
366 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, |
367 | NULL); | |
7a360375 BV |
368 | |
369 | return TRUE; | |
370 | } | |
371 | ||
6078d2c9 | 372 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
ac3898d2 | 373 | { |
7a360375 | 374 | struct dev_context *devc; |
d4abb463 | 375 | struct drv_context *drvc = di->priv; |
7a360375 | 376 | const struct libusb_pollfd **pfd; |
ac046ef8 | 377 | struct sr_usb_dev_inst *usb; |
7a360375 BV |
378 | struct libusb_transfer *transfer; |
379 | int ret, i; | |
380 | unsigned char *buf; | |
381 | ||
e73ffd42 BV |
382 | if (sdi->status != SR_ST_ACTIVE) |
383 | return SR_ERR_DEV_CLOSED; | |
384 | ||
7a360375 BV |
385 | if (!di->priv) { |
386 | sr_err("Driver was not initialized."); | |
387 | return SR_ERR; | |
388 | } | |
389 | ||
7a360375 | 390 | devc = sdi->priv; |
ac046ef8 | 391 | usb = sdi->conn; |
7a360375 BV |
392 | devc->cb_data = cb_data; |
393 | ||
394 | /* Send header packet to the session bus. */ | |
29a27196 | 395 | std_session_send_df_header(cb_data, LOG_PREFIX); |
7a360375 | 396 | |
d4abb463 | 397 | pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx); |
7a360375 BV |
398 | for (i = 0; pfd[i]; i++) { |
399 | /* Handle USB events every 100ms, for decent latency. */ | |
400 | sr_source_add(pfd[i]->fd, pfd[i]->events, 100, | |
401 | handle_events, (void *)sdi); | |
402 | /* We'll need to remove this fd later. */ | |
403 | devc->usbfd[i] = pfd[i]->fd; | |
404 | } | |
405 | devc->usbfd[i] = -1; | |
406 | ||
ff945683 | 407 | buf = g_try_malloc(DMM_DATA_SIZE); |
7a360375 BV |
408 | transfer = libusb_alloc_transfer(0); |
409 | /* Each transfer request gets 100ms to arrive before it's restarted. | |
410 | * The device only sends 1 transfer/second no matter how many | |
411 | * times you ask, but we want to keep step with the USB events | |
412 | * handling above. */ | |
ac046ef8 | 413 | libusb_fill_interrupt_transfer(transfer, usb->devhdl, |
fa773062 UH |
414 | VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer, |
415 | cb_data, 100); | |
7a360375 | 416 | if ((ret = libusb_submit_transfer(transfer) != 0)) { |
fa773062 | 417 | sr_err("Unable to submit transfer: %s.", libusb_error_name(ret)); |
7a360375 BV |
418 | libusb_free_transfer(transfer); |
419 | g_free(buf); | |
420 | return SR_ERR; | |
421 | } | |
ac3898d2 BV |
422 | |
423 | return SR_OK; | |
424 | } | |
425 | ||
6078d2c9 | 426 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
ac3898d2 BV |
427 | { |
428 | (void)cb_data; | |
429 | ||
7a360375 BV |
430 | if (!di->priv) { |
431 | sr_err("Driver was not initialized."); | |
432 | return SR_ERR; | |
433 | } | |
434 | ||
ac3898d2 | 435 | if (sdi->status != SR_ST_ACTIVE) { |
7a360375 | 436 | sr_err("Device not active, can't stop acquisition."); |
ac3898d2 BV |
437 | return SR_ERR; |
438 | } | |
439 | ||
7a360375 | 440 | sdi->status = SR_ST_STOPPING; |
ac3898d2 BV |
441 | |
442 | return SR_OK; | |
443 | } | |
444 | ||
445 | SR_PRIV struct sr_dev_driver victor_dmm_driver_info = { | |
446 | .name = "victor-dmm", | |
7a360375 | 447 | .longname = "Victor DMMs", |
ac3898d2 | 448 | .api_version = 1, |
6078d2c9 UH |
449 | .init = init, |
450 | .cleanup = cleanup, | |
451 | .scan = scan, | |
452 | .dev_list = dev_list, | |
3b412e3a | 453 | .dev_clear = dev_clear, |
ac046ef8 | 454 | .config_get = config_get, |
035a1078 | 455 | .config_set = config_set, |
a1c743fc | 456 | .config_list = config_list, |
6078d2c9 UH |
457 | .dev_open = dev_open, |
458 | .dev_close = dev_close, | |
459 | .dev_acquisition_start = dev_acquisition_start, | |
460 | .dev_acquisition_stop = dev_acquisition_stop, | |
ac3898d2 BV |
461 | .priv = NULL, |
462 | }; |