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/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 int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
37 static const uint32_t drvopts[] = {
41 static const uint32_t scanopts[] = {
45 static const uint32_t devopts[] = {
47 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
48 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
49 SR_CONF_CONN | SR_CONF_GET,
52 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
54 return std_init(sr_ctx, di, LOG_PREFIX);
57 static GSList *scan(struct sr_dev_driver *di, GSList *options)
59 struct drv_context *drvc;
60 struct dev_context *devc;
61 struct sr_dev_inst *sdi;
62 struct libusb_device_descriptor des;
63 libusb_device **devlist;
66 char connection_id[64];
73 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
74 for (i = 0; devlist[i]; i++) {
75 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
76 sr_warn("Failed to get device descriptor: %s",
77 libusb_error_name(ret));
81 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
84 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
86 sdi = g_malloc0(sizeof(struct sr_dev_inst));
87 sdi->status = SR_ST_INACTIVE;
88 sdi->vendor = g_strdup(VICTOR_VENDOR);
90 sdi->connection_id = g_strdup(connection_id);
91 devc = g_malloc0(sizeof(struct dev_context));
94 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
96 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
97 libusb_get_device_address(devlist[i]), NULL);
98 sdi->inst_type = SR_INST_USB;
100 drvc->instances = g_slist_append(drvc->instances, sdi);
101 devices = g_slist_append(devices, sdi);
103 libusb_free_device_list(devlist, 1);
108 static GSList *dev_list(const struct sr_dev_driver *di)
110 return ((struct drv_context *)(di->context))->instances;
113 static int dev_open(struct sr_dev_inst *sdi)
115 struct sr_dev_driver *di = sdi->driver;
116 struct drv_context *drvc = di->context;
117 struct sr_usb_dev_inst *usb;
118 libusb_device **devlist;
120 char connection_id[64];
123 sr_err("Driver was not initialized.");
129 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
130 for (i = 0; devlist[i]; i++) {
131 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
132 if (strcmp(sdi->connection_id, connection_id))
134 if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
135 sr_err("Failed to open device: %s.", libusb_error_name(ret));
140 libusb_free_device_list(devlist, 1);
142 sr_err("Device not found.");
146 /* The device reports as HID class, so the kernel would have
148 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
149 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
150 sr_err("Failed to detach kernel driver: %s.",
151 libusb_error_name(ret));
156 if ((ret = libusb_claim_interface(usb->devhdl,
157 VICTOR_INTERFACE))) {
158 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
161 sdi->status = SR_ST_ACTIVE;
166 static int dev_close(struct sr_dev_inst *sdi)
168 struct sr_dev_driver *di = sdi->driver;
169 struct sr_usb_dev_inst *usb;
172 sr_err("Driver was not initialized.");
182 libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
183 libusb_close(usb->devhdl);
185 sdi->status = SR_ST_INACTIVE;
190 static int cleanup(const struct sr_dev_driver *di)
193 struct drv_context *drvc;
195 if (!(drvc = di->context))
196 /* Can get called on an unused driver, doesn't matter. */
199 ret = std_dev_clear(di, NULL);
205 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
206 const struct sr_channel_group *cg)
208 struct sr_usb_dev_inst *usb;
215 if (!sdi || !sdi->conn)
218 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
219 *data = g_variant_new_string(str);
228 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
229 const struct sr_channel_group *cg)
231 struct sr_dev_driver *di = sdi->driver;
232 struct dev_context *devc;
237 if (sdi->status != SR_ST_ACTIVE)
238 return SR_ERR_DEV_CLOSED;
241 sr_err("Driver was not initialized.");
248 case SR_CONF_LIMIT_MSEC:
249 devc->limit_msec = g_variant_get_uint64(data);
250 now = g_get_monotonic_time() / 1000;
251 devc->end_time = now + devc->limit_msec;
253 case SR_CONF_LIMIT_SAMPLES:
254 devc->limit_samples = g_variant_get_uint64(data);
263 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
264 const struct sr_channel_group *cg)
270 case SR_CONF_SCAN_OPTIONS:
271 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
272 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
274 case SR_CONF_DEVICE_OPTIONS:
276 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
277 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
279 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
280 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
289 static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
291 struct dev_context *devc;
292 struct sr_dev_inst *sdi;
295 sdi = transfer->user_data;
297 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
298 /* USB device was unplugged. */
299 dev_acquisition_stop(sdi, sdi);
300 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
301 sr_dbg("Got %d-byte packet.", transfer->actual_length);
302 if (transfer->actual_length == DMM_DATA_SIZE) {
303 victor_dmm_receive_data(sdi, transfer->buffer);
304 if (devc->limit_samples) {
305 if (devc->num_samples >= devc->limit_samples)
306 dev_acquisition_stop(sdi, sdi);
310 /* Anything else is either an error or a timeout, which is fine:
311 * we were just going to send another transfer request anyway. */
313 if (sdi->status == SR_ST_ACTIVE) {
314 /* Send the same request again. */
315 if ((ret = libusb_submit_transfer(transfer) != 0)) {
316 sr_err("Unable to resubmit transfer: %s.",
317 libusb_error_name(ret));
318 g_free(transfer->buffer);
319 libusb_free_transfer(transfer);
320 dev_acquisition_stop(sdi, sdi);
323 /* This was the last transfer we're going to receive, so
325 g_free(transfer->buffer);
326 libusb_free_transfer(transfer);
330 static int handle_events(int fd, int revents, void *cb_data)
332 struct dev_context *devc;
333 struct drv_context *drvc;
334 struct sr_datafeed_packet packet;
335 struct sr_dev_inst *sdi;
336 struct sr_dev_driver *di;
348 if (devc->limit_msec) {
349 now = g_get_monotonic_time() / 1000;
350 if (now > devc->end_time)
351 dev_acquisition_stop(sdi, sdi);
354 if (sdi->status == SR_ST_STOPPING) {
355 usb_source_remove(sdi->session, drvc->sr_ctx);
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 dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
372 struct sr_dev_driver *di = sdi->driver;
373 struct dev_context *devc;
374 struct drv_context *drvc = di->context;
375 struct sr_usb_dev_inst *usb;
376 struct libusb_transfer *transfer;
380 if (sdi->status != SR_ST_ACTIVE)
381 return SR_ERR_DEV_CLOSED;
384 sr_err("Driver was not initialized.");
390 devc->cb_data = cb_data;
392 /* Send header packet to the session bus. */
393 std_session_send_df_header(cb_data, LOG_PREFIX);
395 usb_source_add(sdi->session, drvc->sr_ctx, 100,
396 handle_events, (void *)sdi);
398 buf = g_malloc(DMM_DATA_SIZE);
399 transfer = libusb_alloc_transfer(0);
400 /* Each transfer request gets 100ms to arrive before it's restarted.
401 * The device only sends 1 transfer/second no matter how many
402 * times you ask, but we want to keep step with the USB events
404 libusb_fill_interrupt_transfer(transfer, usb->devhdl,
405 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
407 if ((ret = libusb_submit_transfer(transfer) != 0)) {
408 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
409 libusb_free_transfer(transfer);
417 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
419 struct sr_dev_driver *di = sdi->driver;
423 sr_err("Driver was not initialized.");
427 if (sdi->status != SR_ST_ACTIVE) {
428 sr_err("Device not active, can't stop acquisition.");
432 sdi->status = SR_ST_STOPPING;
437 SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
438 .name = "victor-dmm",
439 .longname = "Victor DMMs",
444 .dev_list = dev_list,
446 .config_get = config_get,
447 .config_set = config_set,
448 .config_list = config_list,
449 .dev_open = dev_open,
450 .dev_close = dev_close,
451 .dev_acquisition_start = dev_acquisition_start,
452 .dev_acquisition_stop = dev_acquisition_stop,