2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
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
34 SR_PRIV struct sr_dev_driver victor_dmm_driver_info;
35 static struct sr_dev_driver *di = &victor_dmm_driver_info;
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);
39 static const int hwcaps[] = {
42 SR_CONF_LIMIT_SAMPLES,
47 /* Properly close and free all devices. */
48 static int clear_instances(void)
50 struct sr_dev_inst *sdi;
51 struct drv_context *drvc;
52 struct dev_context *devc;
55 if (!(drvc = di->priv))
56 /* Can get called on an unused driver, doesn't matter. */
59 for (l = drvc->instances; l; l = l->next) {
62 if (!(devc = sdi->priv))
65 sr_usb_dev_inst_free(devc->usb);
66 sr_dev_inst_free(sdi);
69 g_slist_free(drvc->instances);
70 drvc->instances = NULL;
75 static int hw_init(struct sr_context *sr_ctx)
77 struct drv_context *drvc;
79 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
80 sr_err("Driver context malloc failed.");
83 drvc->sr_ctx = sr_ctx;
89 static GSList *hw_scan(GSList *options)
91 struct drv_context *drvc;
92 struct dev_context *devc;
93 struct sr_dev_inst *sdi;
94 struct sr_probe *probe;
95 struct libusb_device_descriptor des;
96 libusb_device **devlist;
102 if (!(drvc = di->priv)) {
103 sr_err("Driver was not initialized.");
107 /* USB scan is always authoritative. */
111 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
112 for (i = 0; devlist[i]; i++) {
113 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
114 sr_warn("Failed to get device descriptor: %s",
115 libusb_error_name(ret));
119 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
122 devcnt = g_slist_length(drvc->instances);
123 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
124 VICTOR_VENDOR, NULL, NULL)))
128 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
132 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
134 sdi->probes = g_slist_append(NULL, probe);
136 if (!(devc->usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
137 libusb_get_device_address(devlist[i]), NULL)))
140 drvc->instances = g_slist_append(drvc->instances, sdi);
141 devices = g_slist_append(devices, sdi);
143 libusb_free_device_list(devlist, 1);
148 static GSList *hw_dev_list(void)
150 struct drv_context *drvc;
152 if (!(drvc = di->priv)) {
153 sr_err("Driver was not initialized.");
157 return drvc->instances;
160 static int hw_dev_open(struct sr_dev_inst *sdi)
162 struct dev_context *devc;
163 struct drv_context *drvc = di->priv;
164 libusb_device **devlist;
168 sr_err("Driver was not initialized.");
173 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
174 for (i = 0; devlist[i]; i++) {
175 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
176 || libusb_get_device_address(devlist[i]) != devc->usb->address)
178 if ((ret = libusb_open(devlist[i], &devc->usb->devhdl))) {
179 sr_err("Failed to open device: %s.", libusb_error_name(ret));
184 libusb_free_device_list(devlist, 1);
186 sr_err("Device not found.");
190 /* The device reports as HID class, so the kernel would have
192 if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) {
193 if ((ret = libusb_detach_kernel_driver(devc->usb->devhdl, 0)) < 0) {
194 sr_err("Failed to detach kernel driver: %s.",
195 libusb_error_name(ret));
200 if ((ret = libusb_claim_interface(devc->usb->devhdl,
201 VICTOR_INTERFACE))) {
202 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
205 sdi->status = SR_ST_ACTIVE;
210 static int hw_dev_close(struct sr_dev_inst *sdi)
212 struct dev_context *devc;
215 sr_err("Driver was not initialized.");
220 if (!devc->usb->devhdl)
224 libusb_release_interface(devc->usb->devhdl, VICTOR_INTERFACE);
225 libusb_close(devc->usb->devhdl);
226 devc->usb->devhdl = NULL;
227 sdi->status = SR_ST_INACTIVE;
232 static int hw_cleanup(void)
234 struct drv_context *drvc;
236 if (!(drvc = di->priv))
237 /* Can get called on an unused driver, doesn't matter. */
247 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
259 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
261 struct dev_context *devc;
266 sr_err("Driver was not initialized.");
270 if (sdi->status != SR_ST_ACTIVE) {
271 sr_err("Device inactive, can't set config options.");
278 case SR_CONF_LIMIT_MSEC:
279 devc->limit_msec = *(const int64_t *)value;
280 now = g_get_monotonic_time() / 1000;
281 devc->end_time = now + devc->limit_msec;
282 sr_dbg("Setting time limit to %" PRIu64 "ms.",
285 case SR_CONF_LIMIT_SAMPLES:
286 devc->limit_samples = *(const uint64_t *)value;
287 sr_dbg("Setting sample limit to %" PRIu64 ".",
288 devc->limit_samples);
291 sr_err("Unknown hardware capability: %d.", id);
298 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
304 case SR_CONF_DEVICE_OPTIONS:
314 static void receive_transfer(struct libusb_transfer *transfer)
316 struct dev_context *devc;
317 struct sr_dev_inst *sdi;
320 sdi = transfer->user_data;
322 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
323 /* USB device was unplugged. */
324 hw_dev_acquisition_stop(sdi, sdi);
325 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
326 sr_dbg("Got %d-byte packet.", transfer->actual_length);
327 if (transfer->actual_length == DMM_DATA_SIZE) {
328 victor_dmm_receive_data(sdi, transfer->buffer);
329 if (devc->limit_samples) {
330 if (devc->num_samples >= devc->limit_samples)
331 hw_dev_acquisition_stop(sdi, sdi);
335 /* Anything else is either an error or a timeout, which is fine:
336 * we were just going to send another transfer request anyway. */
338 if (sdi->status == SR_ST_ACTIVE) {
339 /* Send the same request again. */
340 if ((ret = libusb_submit_transfer(transfer) != 0)) {
341 sr_err("Unable to resubmit transfer: %s.",
342 libusb_error_name(ret));
343 g_free(transfer->buffer);
344 libusb_free_transfer(transfer);
345 hw_dev_acquisition_stop(sdi, sdi);
348 /* This was the last transfer we're going to receive, so
350 g_free(transfer->buffer);
351 libusb_free_transfer(transfer);
355 static int handle_events(int fd, int revents, void *cb_data)
357 struct dev_context *devc;
358 struct drv_context *drvc = di->priv;
359 struct sr_datafeed_packet packet;
360 struct sr_dev_inst *sdi;
371 if (devc->limit_msec) {
372 now = g_get_monotonic_time() / 1000;
373 if (now > devc->end_time)
374 hw_dev_acquisition_stop(sdi, sdi);
377 if (sdi->status == SR_ST_STOPPING) {
378 for (i = 0; devc->usbfd[i] != -1; i++)
379 sr_source_remove(devc->usbfd[i]);
383 packet.type = SR_DF_END;
384 sr_session_send(cb_data, &packet);
387 memset(&tv, 0, sizeof(struct timeval));
388 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
394 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
397 struct sr_datafeed_packet packet;
398 struct sr_datafeed_header header;
399 struct dev_context *devc;
400 struct drv_context *drvc = di->priv;
401 const struct libusb_pollfd **pfd;
402 struct libusb_transfer *transfer;
407 sr_err("Driver was not initialized.");
411 sr_dbg("Starting acquisition.");
414 devc->cb_data = cb_data;
416 /* Send header packet to the session bus. */
417 sr_dbg("Sending SR_DF_HEADER.");
418 packet.type = SR_DF_HEADER;
419 packet.payload = (uint8_t *)&header;
420 header.feed_version = 1;
421 sr_session_send(devc->cb_data, &packet);
423 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
424 for (i = 0; pfd[i]; i++) {
425 /* Handle USB events every 100ms, for decent latency. */
426 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
427 handle_events, (void *)sdi);
428 /* We'll need to remove this fd later. */
429 devc->usbfd[i] = pfd[i]->fd;
433 buf = g_try_malloc(DMM_DATA_SIZE);
434 transfer = libusb_alloc_transfer(0);
435 /* Each transfer request gets 100ms to arrive before it's restarted.
436 * The device only sends 1 transfer/second no matter how many
437 * times you ask, but we want to keep step with the USB events
439 libusb_fill_interrupt_transfer(transfer, devc->usb->devhdl,
440 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
442 if ((ret = libusb_submit_transfer(transfer) != 0)) {
443 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
444 libusb_free_transfer(transfer);
452 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
457 sr_err("Driver was not initialized.");
461 if (sdi->status != SR_ST_ACTIVE) {
462 sr_err("Device not active, can't stop acquisition.");
466 sdi->status = SR_ST_STOPPING;
471 SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
472 .name = "victor-dmm",
473 .longname = "Victor DMMs",
476 .cleanup = hw_cleanup,
478 .dev_list = hw_dev_list,
479 .dev_clear = clear_instances,
480 .config_get = config_get,
481 .config_set = config_set,
482 .config_list = config_list,
483 .dev_open = hw_dev_open,
484 .dev_close = hw_dev_close,
485 .dev_acquisition_start = hw_dev_acquisition_start,
486 .dev_acquisition_stop = hw_dev_acquisition_stop,