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 int32_t hwopts[] = {
43 static const int32_t hwcaps[] = {
46 SR_CONF_LIMIT_SAMPLES,
50 /* Properly close and free all devices. */
51 static int clear_instances(void)
53 struct sr_dev_inst *sdi;
54 struct drv_context *drvc;
55 struct dev_context *devc;
58 if (!(drvc = di->priv))
59 /* Can get called on an unused driver, doesn't matter. */
62 for (l = drvc->instances; l; l = l->next) {
65 if (!(devc = sdi->priv))
68 sr_usb_dev_inst_free(sdi->conn);
69 sr_dev_inst_free(sdi);
72 g_slist_free(drvc->instances);
73 drvc->instances = NULL;
78 static int hw_init(struct sr_context *sr_ctx)
80 return std_hw_init(sr_ctx, di, LOG_PREFIX);
83 static GSList *hw_scan(GSList *options)
85 struct drv_context *drvc;
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;
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 (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
125 libusb_get_device_address(devlist[i]), NULL)))
127 sdi->inst_type = SR_INST_USB;
129 drvc->instances = g_slist_append(drvc->instances, sdi);
130 devices = g_slist_append(devices, sdi);
132 libusb_free_device_list(devlist, 1);
137 static GSList *hw_dev_list(void)
139 return ((struct drv_context *)(di->priv))->instances;
142 static int hw_dev_open(struct sr_dev_inst *sdi)
144 struct drv_context *drvc = di->priv;
145 struct sr_usb_dev_inst *usb;
146 libusb_device **devlist;
150 sr_err("Driver was not initialized.");
156 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
157 for (i = 0; devlist[i]; i++) {
158 if (libusb_get_bus_number(devlist[i]) != usb->bus
159 || libusb_get_device_address(devlist[i]) != usb->address)
161 if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
162 sr_err("Failed to open device: %s.", libusb_error_name(ret));
167 libusb_free_device_list(devlist, 1);
169 sr_err("Device not found.");
173 /* The device reports as HID class, so the kernel would have
175 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
176 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
177 sr_err("Failed to detach kernel driver: %s.",
178 libusb_error_name(ret));
183 if ((ret = libusb_claim_interface(usb->devhdl,
184 VICTOR_INTERFACE))) {
185 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
188 sdi->status = SR_ST_ACTIVE;
193 static int hw_dev_close(struct sr_dev_inst *sdi)
195 struct sr_usb_dev_inst *usb;
198 sr_err("Driver was not initialized.");
208 libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
209 libusb_close(usb->devhdl);
211 sdi->status = SR_ST_INACTIVE;
216 static int hw_cleanup(void)
218 struct drv_context *drvc;
220 if (!(drvc = di->priv))
221 /* Can get called on an unused driver, doesn't matter. */
231 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
233 struct sr_usb_dev_inst *usb;
238 if (!sdi || !sdi->conn)
241 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
242 *data = g_variant_new_string(str);
251 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
253 struct dev_context *devc;
257 if (sdi->status != SR_ST_ACTIVE)
258 return SR_ERR_DEV_CLOSED;
261 sr_err("Driver was not initialized.");
268 case SR_CONF_LIMIT_MSEC:
269 devc->limit_msec = g_variant_get_uint64(data);
270 now = g_get_monotonic_time() / 1000;
271 devc->end_time = now + devc->limit_msec;
272 sr_dbg("Setting time limit to %" PRIu64 "ms.",
275 case SR_CONF_LIMIT_SAMPLES:
276 devc->limit_samples = g_variant_get_uint64(data);
277 sr_dbg("Setting sample limit to %" PRIu64 ".",
278 devc->limit_samples);
287 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
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));
297 case SR_CONF_DEVICE_OPTIONS:
298 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
299 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
308 static void receive_transfer(struct libusb_transfer *transfer)
310 struct dev_context *devc;
311 struct sr_dev_inst *sdi;
314 sdi = transfer->user_data;
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) {
320 sr_dbg("Got %d-byte packet.", transfer->actual_length);
321 if (transfer->actual_length == DMM_DATA_SIZE) {
322 victor_dmm_receive_data(sdi, transfer->buffer);
323 if (devc->limit_samples) {
324 if (devc->num_samples >= devc->limit_samples)
325 hw_dev_acquisition_stop(sdi, sdi);
329 /* Anything else is either an error or a timeout, which is fine:
330 * we were just going to send another transfer request anyway. */
332 if (sdi->status == SR_ST_ACTIVE) {
333 /* Send the same request again. */
334 if ((ret = libusb_submit_transfer(transfer) != 0)) {
335 sr_err("Unable to resubmit transfer: %s.",
336 libusb_error_name(ret));
337 g_free(transfer->buffer);
338 libusb_free_transfer(transfer);
339 hw_dev_acquisition_stop(sdi, sdi);
342 /* This was the last transfer we're going to receive, so
344 g_free(transfer->buffer);
345 libusb_free_transfer(transfer);
349 static int handle_events(int fd, int revents, void *cb_data)
351 struct dev_context *devc;
352 struct drv_context *drvc = di->priv;
353 struct sr_datafeed_packet packet;
354 struct sr_dev_inst *sdi;
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);
371 if (sdi->status == SR_ST_STOPPING) {
372 for (i = 0; devc->usbfd[i] != -1; i++)
373 sr_source_remove(devc->usbfd[i]);
377 packet.type = SR_DF_END;
378 sr_session_send(cb_data, &packet);
381 memset(&tv, 0, sizeof(struct timeval));
382 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
388 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
391 struct dev_context *devc;
392 struct drv_context *drvc = di->priv;
393 const struct libusb_pollfd **pfd;
394 struct sr_usb_dev_inst *usb;
395 struct libusb_transfer *transfer;
399 if (sdi->status != SR_ST_ACTIVE)
400 return SR_ERR_DEV_CLOSED;
403 sr_err("Driver was not initialized.");
409 devc->cb_data = cb_data;
411 /* Send header packet to the session bus. */
412 std_session_send_df_header(cb_data, LOG_PREFIX);
414 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
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;
424 buf = g_try_malloc(DMM_DATA_SIZE);
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
430 libusb_fill_interrupt_transfer(transfer, usb->devhdl,
431 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
433 if ((ret = libusb_submit_transfer(transfer) != 0)) {
434 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
435 libusb_free_transfer(transfer);
443 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
448 sr_err("Driver was not initialized.");
452 if (sdi->status != SR_ST_ACTIVE) {
453 sr_err("Device not active, can't stop acquisition.");
457 sdi->status = SR_ST_STOPPING;
462 SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
463 .name = "victor-dmm",
464 .longname = "Victor DMMs",
467 .cleanup = hw_cleanup,
469 .dev_list = hw_dev_list,
470 .dev_clear = clear_instances,
471 .config_get = config_get,
472 .config_set = config_set,
473 .config_list = config_list,
474 .dev_open = hw_dev_open,
475 .dev_close = hw_dev_close,
476 .dev_acquisition_start = hw_dev_acquisition_start,
477 .dev_acquisition_stop = hw_dev_acquisition_stop,