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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
29 #define VICTOR_VID 0x1244
30 #define VICTOR_PID 0xd237
31 #define VICTOR_VENDOR "Victor"
32 #define VICTOR_INTERFACE 0
33 #define VICTOR_ENDPOINT (LIBUSB_ENDPOINT_IN | 1)
35 SR_PRIV struct sr_dev_driver victor_dmm_driver_info;
36 static int dev_acquisition_stop(struct sr_dev_inst *sdi);
38 static const uint32_t drvopts[] = {
42 static const uint32_t scanopts[] = {
46 static const uint32_t devopts[] = {
48 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
49 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
50 SR_CONF_CONN | SR_CONF_GET,
53 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
55 return std_init(sr_ctx, di, LOG_PREFIX);
58 static GSList *scan(struct sr_dev_driver *di, GSList *options)
60 struct drv_context *drvc;
61 struct dev_context *devc;
62 struct sr_dev_inst *sdi;
63 struct libusb_device_descriptor des;
64 libusb_device **devlist;
67 char connection_id[64];
74 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
75 for (i = 0; devlist[i]; i++) {
76 libusb_get_device_descriptor(devlist[i], &des);
78 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
81 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
83 sdi = g_malloc0(sizeof(struct sr_dev_inst));
84 sdi->status = SR_ST_INACTIVE;
85 sdi->vendor = g_strdup(VICTOR_VENDOR);
87 sdi->connection_id = g_strdup(connection_id);
88 devc = g_malloc0(sizeof(struct dev_context));
91 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
92 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
93 libusb_get_device_address(devlist[i]), NULL);
94 sdi->inst_type = SR_INST_USB;
96 drvc->instances = g_slist_append(drvc->instances, sdi);
97 devices = g_slist_append(devices, sdi);
99 libusb_free_device_list(devlist, 1);
104 static int dev_open(struct sr_dev_inst *sdi)
106 struct sr_dev_driver *di = sdi->driver;
107 struct drv_context *drvc = di->context;
108 struct sr_usb_dev_inst *usb;
109 libusb_device **devlist;
111 char connection_id[64];
115 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
116 for (i = 0; devlist[i]; i++) {
117 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
118 if (strcmp(sdi->connection_id, connection_id))
120 if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
121 sr_err("Failed to open device: %s.", libusb_error_name(ret));
126 libusb_free_device_list(devlist, 1);
128 sr_err("Device not found.");
132 /* The device reports as HID class, so the kernel would have
134 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
135 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
136 sr_err("Failed to detach kernel driver: %s.",
137 libusb_error_name(ret));
142 if ((ret = libusb_claim_interface(usb->devhdl,
143 VICTOR_INTERFACE))) {
144 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
147 sdi->status = SR_ST_ACTIVE;
152 static int dev_close(struct sr_dev_inst *sdi)
154 struct sr_usb_dev_inst *usb;
162 libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
163 libusb_close(usb->devhdl);
165 sdi->status = SR_ST_INACTIVE;
170 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
171 const struct sr_channel_group *cg)
173 struct sr_usb_dev_inst *usb;
180 if (!sdi || !sdi->conn)
183 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
184 *data = g_variant_new_string(str);
193 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
194 const struct sr_channel_group *cg)
196 struct dev_context *devc;
201 if (sdi->status != SR_ST_ACTIVE)
202 return SR_ERR_DEV_CLOSED;
207 case SR_CONF_LIMIT_MSEC:
208 devc->limit_msec = g_variant_get_uint64(data);
209 now = g_get_monotonic_time() / 1000;
210 devc->end_time = now + devc->limit_msec;
212 case SR_CONF_LIMIT_SAMPLES:
213 devc->limit_samples = g_variant_get_uint64(data);
222 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
223 const struct sr_channel_group *cg)
229 case SR_CONF_SCAN_OPTIONS:
230 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
231 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
233 case SR_CONF_DEVICE_OPTIONS:
235 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
236 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
238 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
239 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
248 static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
250 struct dev_context *devc;
251 struct sr_dev_inst *sdi;
254 sdi = transfer->user_data;
256 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
257 /* USB device was unplugged. */
258 dev_acquisition_stop(sdi);
259 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
260 sr_dbg("Got %d-byte packet.", transfer->actual_length);
261 if (transfer->actual_length == DMM_DATA_SIZE) {
262 victor_dmm_receive_data(sdi, transfer->buffer);
263 if (devc->limit_samples) {
264 if (devc->num_samples >= devc->limit_samples)
265 dev_acquisition_stop(sdi);
269 /* Anything else is either an error or a timeout, which is fine:
270 * we were just going to send another transfer request anyway. */
272 if (sdi->status == SR_ST_ACTIVE) {
273 /* Send the same request again. */
274 if ((ret = libusb_submit_transfer(transfer) != 0)) {
275 sr_err("Unable to resubmit transfer: %s.",
276 libusb_error_name(ret));
277 g_free(transfer->buffer);
278 libusb_free_transfer(transfer);
279 dev_acquisition_stop(sdi);
282 /* This was the last transfer we're going to receive, so
284 g_free(transfer->buffer);
285 libusb_free_transfer(transfer);
289 static int handle_events(int fd, int revents, void *cb_data)
291 struct dev_context *devc;
292 struct drv_context *drvc;
293 struct sr_dev_inst *sdi;
294 struct sr_dev_driver *di;
306 if (devc->limit_msec) {
307 now = g_get_monotonic_time() / 1000;
308 if (now > devc->end_time)
309 dev_acquisition_stop(sdi);
312 if (sdi->status == SR_ST_STOPPING) {
313 usb_source_remove(sdi->session, drvc->sr_ctx);
315 std_session_send_df_end(sdi, LOG_PREFIX);
318 memset(&tv, 0, sizeof(struct timeval));
319 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
325 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
327 struct sr_dev_driver *di = sdi->driver;
328 struct drv_context *drvc = di->context;
329 struct sr_usb_dev_inst *usb;
330 struct libusb_transfer *transfer;
334 if (sdi->status != SR_ST_ACTIVE)
335 return SR_ERR_DEV_CLOSED;
339 std_session_send_df_header(sdi, LOG_PREFIX);
341 usb_source_add(sdi->session, drvc->sr_ctx, 100,
342 handle_events, (void *)sdi);
344 buf = g_malloc(DMM_DATA_SIZE);
345 transfer = libusb_alloc_transfer(0);
346 /* Each transfer request gets 100ms to arrive before it's restarted.
347 * The device only sends 1 transfer/second no matter how many
348 * times you ask, but we want to keep step with the USB events
350 libusb_fill_interrupt_transfer(transfer, usb->devhdl,
351 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
352 (struct sr_dev_inst *)sdi, 100);
353 if ((ret = libusb_submit_transfer(transfer) != 0)) {
354 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
355 libusb_free_transfer(transfer);
363 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
365 if (sdi->status != SR_ST_ACTIVE) {
366 sr_err("Device not active, can't stop acquisition.");
370 sdi->status = SR_ST_STOPPING;
375 SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
376 .name = "victor-dmm",
377 .longname = "Victor DMMs",
380 .cleanup = std_cleanup,
382 .dev_list = std_dev_list,
384 .config_get = config_get,
385 .config_set = config_set,
386 .config_list = config_list,
387 .dev_open = dev_open,
388 .dev_close = dev_close,
389 .dev_acquisition_start = dev_acquisition_start,
390 .dev_acquisition_stop = dev_acquisition_stop,