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 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
80 static GSList *hw_scan(GSList *options)
82 struct drv_context *drvc;
83 struct dev_context *devc;
84 struct sr_dev_inst *sdi;
85 struct sr_probe *probe;
86 struct libusb_device_descriptor des;
87 libusb_device **devlist;
95 /* USB scan is always authoritative. */
99 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
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));
107 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
110 devcnt = g_slist_length(drvc->instances);
111 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
112 VICTOR_VENDOR, NULL, NULL)))
116 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
120 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
122 sdi->probes = g_slist_append(NULL, probe);
124 if (!(devc->usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
125 libusb_get_device_address(devlist[i]), NULL)))
128 drvc->instances = g_slist_append(drvc->instances, sdi);
129 devices = g_slist_append(devices, sdi);
131 libusb_free_device_list(devlist, 1);
136 static GSList *hw_dev_list(void)
138 struct drv_context *drvc;
140 if (!(drvc = di->priv)) {
141 sr_err("Driver was not initialized.");
145 return drvc->instances;
148 static int hw_dev_open(struct sr_dev_inst *sdi)
150 struct dev_context *devc;
151 struct drv_context *drvc = di->priv;
152 libusb_device **devlist;
156 sr_err("Driver was not initialized.");
161 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
162 for (i = 0; devlist[i]; i++) {
163 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
164 || libusb_get_device_address(devlist[i]) != devc->usb->address)
166 if ((ret = libusb_open(devlist[i], &devc->usb->devhdl))) {
167 sr_err("Failed to open device: %s.", libusb_error_name(ret));
172 libusb_free_device_list(devlist, 1);
174 sr_err("Device not found.");
178 /* The device reports as HID class, so the kernel would have
180 if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) {
181 if ((ret = libusb_detach_kernel_driver(devc->usb->devhdl, 0)) < 0) {
182 sr_err("Failed to detach kernel driver: %s.",
183 libusb_error_name(ret));
188 if ((ret = libusb_claim_interface(devc->usb->devhdl,
189 VICTOR_INTERFACE))) {
190 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
193 sdi->status = SR_ST_ACTIVE;
198 static int hw_dev_close(struct sr_dev_inst *sdi)
200 struct dev_context *devc;
203 sr_err("Driver was not initialized.");
208 if (!devc->usb->devhdl)
212 libusb_release_interface(devc->usb->devhdl, VICTOR_INTERFACE);
213 libusb_close(devc->usb->devhdl);
214 devc->usb->devhdl = NULL;
215 sdi->status = SR_ST_INACTIVE;
220 static int hw_cleanup(void)
222 struct drv_context *drvc;
224 if (!(drvc = di->priv))
225 /* Can get called on an unused driver, doesn't matter. */
235 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
237 struct dev_context *devc;
242 sr_err("Driver was not initialized.");
246 if (sdi->status != SR_ST_ACTIVE) {
247 sr_err("Device inactive, can't set config options.");
254 case SR_CONF_LIMIT_MSEC:
255 devc->limit_msec = *(const int64_t *)value;
256 now = g_get_monotonic_time() / 1000;
257 devc->end_time = now + devc->limit_msec;
258 sr_dbg("Setting time limit to %" PRIu64 "ms.",
261 case SR_CONF_LIMIT_SAMPLES:
262 devc->limit_samples = *(const uint64_t *)value;
263 sr_dbg("Setting sample limit to %" PRIu64 ".",
264 devc->limit_samples);
267 sr_err("Unknown hardware capability: %d.", id);
274 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
280 case SR_CONF_DEVICE_OPTIONS:
290 static void receive_transfer(struct libusb_transfer *transfer)
292 struct dev_context *devc;
293 struct sr_dev_inst *sdi;
296 sdi = transfer->user_data;
298 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
299 /* USB device was unplugged. */
300 hw_dev_acquisition_stop(sdi, sdi);
301 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
302 sr_dbg("Got %d-byte packet.", transfer->actual_length);
303 if (transfer->actual_length == DMM_DATA_SIZE) {
304 victor_dmm_receive_data(sdi, transfer->buffer);
305 if (devc->limit_samples) {
306 if (devc->num_samples >= devc->limit_samples)
307 hw_dev_acquisition_stop(sdi, sdi);
311 /* Anything else is either an error or a timeout, which is fine:
312 * we were just going to send another transfer request anyway. */
314 if (sdi->status == SR_ST_ACTIVE) {
315 /* Send the same request again. */
316 if ((ret = libusb_submit_transfer(transfer) != 0)) {
317 sr_err("Unable to resubmit transfer: %s.",
318 libusb_error_name(ret));
319 g_free(transfer->buffer);
320 libusb_free_transfer(transfer);
321 hw_dev_acquisition_stop(sdi, sdi);
324 /* This was the last transfer we're going to receive, so
326 g_free(transfer->buffer);
327 libusb_free_transfer(transfer);
331 static int handle_events(int fd, int revents, void *cb_data)
333 struct dev_context *devc;
334 struct drv_context *drvc = di->priv;
335 struct sr_datafeed_packet packet;
336 struct sr_dev_inst *sdi;
347 if (devc->limit_msec) {
348 now = g_get_monotonic_time() / 1000;
349 if (now > devc->end_time)
350 hw_dev_acquisition_stop(sdi, sdi);
353 if (sdi->status == SR_ST_STOPPING) {
354 for (i = 0; devc->usbfd[i] != -1; i++)
355 sr_source_remove(devc->usbfd[i]);
359 packet.type = SR_DF_END;
360 sr_session_send(cb_data, &packet);
363 memset(&tv, 0, sizeof(struct timeval));
364 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
370 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
373 struct sr_datafeed_packet packet;
374 struct sr_datafeed_header header;
375 struct dev_context *devc;
376 struct drv_context *drvc = di->priv;
377 const struct libusb_pollfd **pfd;
378 struct libusb_transfer *transfer;
383 sr_err("Driver was not initialized.");
387 sr_dbg("Starting acquisition.");
390 devc->cb_data = cb_data;
392 /* Send header packet to the session bus. */
393 sr_dbg("Sending SR_DF_HEADER.");
394 packet.type = SR_DF_HEADER;
395 packet.payload = (uint8_t *)&header;
396 header.feed_version = 1;
397 sr_session_send(devc->cb_data, &packet);
399 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
400 for (i = 0; pfd[i]; i++) {
401 /* Handle USB events every 100ms, for decent latency. */
402 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
403 handle_events, (void *)sdi);
404 /* We'll need to remove this fd later. */
405 devc->usbfd[i] = pfd[i]->fd;
409 buf = g_try_malloc(DMM_DATA_SIZE);
410 transfer = libusb_alloc_transfer(0);
411 /* Each transfer request gets 100ms to arrive before it's restarted.
412 * The device only sends 1 transfer/second no matter how many
413 * times you ask, but we want to keep step with the USB events
415 libusb_fill_interrupt_transfer(transfer, devc->usb->devhdl,
416 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
418 if ((ret = libusb_submit_transfer(transfer) != 0)) {
419 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
420 libusb_free_transfer(transfer);
428 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
433 sr_err("Driver was not initialized.");
437 if (sdi->status != SR_ST_ACTIVE) {
438 sr_err("Device not active, can't stop acquisition.");
442 sdi->status = SR_ST_STOPPING;
447 SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
448 .name = "victor-dmm",
449 .longname = "Victor DMMs",
452 .cleanup = hw_cleanup,
454 .dev_list = hw_dev_list,
455 .dev_clear = clear_instances,
456 .config_set = config_set,
457 .config_list = config_list,
458 .dev_open = hw_dev_open,
459 .dev_close = hw_dev_close,
460 .dev_acquisition_start = hw_dev_acquisition_start,
461 .dev_acquisition_stop = hw_dev_acquisition_stop,