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