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 GSList *scan(struct sr_dev_driver *di, GSList *options)
55 struct drv_context *drvc;
56 struct dev_context *devc;
57 struct sr_dev_inst *sdi;
58 struct libusb_device_descriptor des;
59 libusb_device **devlist;
62 char connection_id[64];
69 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
70 for (i = 0; devlist[i]; i++) {
71 libusb_get_device_descriptor(devlist[i], &des);
73 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
76 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
78 sdi = g_malloc0(sizeof(struct sr_dev_inst));
79 sdi->status = SR_ST_INACTIVE;
80 sdi->vendor = g_strdup(VICTOR_VENDOR);
82 sdi->connection_id = g_strdup(connection_id);
83 devc = g_malloc0(sizeof(struct dev_context));
84 sr_sw_limits_init(&devc->limits);
87 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
88 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
89 libusb_get_device_address(devlist[i]), NULL);
90 sdi->inst_type = SR_INST_USB;
92 drvc->instances = g_slist_append(drvc->instances, sdi);
93 devices = g_slist_append(devices, sdi);
95 libusb_free_device_list(devlist, 1);
100 static int dev_open(struct sr_dev_inst *sdi)
102 struct sr_dev_driver *di = sdi->driver;
103 struct drv_context *drvc = di->context;
104 struct sr_usb_dev_inst *usb;
109 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
113 /* The device reports as HID class, so the kernel would have
115 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
116 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
117 sr_err("Failed to detach kernel driver: %s.",
118 libusb_error_name(ret));
123 if ((ret = libusb_claim_interface(usb->devhdl,
124 VICTOR_INTERFACE))) {
125 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
128 sdi->status = SR_ST_ACTIVE;
133 static int dev_close(struct sr_dev_inst *sdi)
135 struct sr_usb_dev_inst *usb;
143 libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
144 libusb_close(usb->devhdl);
146 sdi->status = SR_ST_INACTIVE;
151 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
152 const struct sr_channel_group *cg)
154 struct dev_context *devc = sdi->priv;
155 struct sr_usb_dev_inst *usb;
162 if (!sdi || !sdi->conn)
165 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
166 *data = g_variant_new_string(str);
168 case SR_CONF_LIMIT_SAMPLES:
169 case SR_CONF_LIMIT_MSEC:
170 return sr_sw_limits_config_get(&devc->limits, key, data);
178 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
179 const struct sr_channel_group *cg)
181 struct dev_context *devc;
185 if (sdi->status != SR_ST_ACTIVE)
186 return SR_ERR_DEV_CLOSED;
190 return sr_sw_limits_config_set(&devc->limits, key, data);
193 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
194 const struct sr_channel_group *cg)
200 case SR_CONF_SCAN_OPTIONS:
201 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
202 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
204 case SR_CONF_DEVICE_OPTIONS:
206 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
207 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
209 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
210 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
219 static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
221 struct dev_context *devc;
222 struct sr_dev_inst *sdi;
225 sdi = transfer->user_data;
227 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
228 /* USB device was unplugged. */
229 dev_acquisition_stop(sdi);
230 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
231 sr_dbg("Got %d-byte packet.", transfer->actual_length);
232 if (transfer->actual_length == DMM_DATA_SIZE) {
233 victor_dmm_receive_data(sdi, transfer->buffer);
234 if (sr_sw_limits_check(&devc->limits))
235 dev_acquisition_stop(sdi);
238 /* Anything else is either an error or a timeout, which is fine:
239 * we were just going to send another transfer request anyway. */
241 if (sdi->status == SR_ST_ACTIVE) {
242 /* Send the same request again. */
243 if ((ret = libusb_submit_transfer(transfer) != 0)) {
244 sr_err("Unable to resubmit transfer: %s.",
245 libusb_error_name(ret));
246 g_free(transfer->buffer);
247 libusb_free_transfer(transfer);
248 dev_acquisition_stop(sdi);
251 /* This was the last transfer we're going to receive, so
253 g_free(transfer->buffer);
254 libusb_free_transfer(transfer);
258 static int handle_events(int fd, int revents, void *cb_data)
260 struct dev_context *devc;
261 struct drv_context *drvc;
262 struct sr_dev_inst *sdi;
263 struct sr_dev_driver *di;
274 if (sr_sw_limits_check(&devc->limits))
275 dev_acquisition_stop(sdi);
277 if (sdi->status == SR_ST_STOPPING) {
278 usb_source_remove(sdi->session, drvc->sr_ctx);
280 std_session_send_df_end(sdi, LOG_PREFIX);
283 memset(&tv, 0, sizeof(struct timeval));
284 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
290 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
292 struct sr_dev_driver *di = sdi->driver;
293 struct drv_context *drvc = di->context;
294 struct sr_usb_dev_inst *usb;
295 struct libusb_transfer *transfer;
299 if (sdi->status != SR_ST_ACTIVE)
300 return SR_ERR_DEV_CLOSED;
304 std_session_send_df_header(sdi, LOG_PREFIX);
306 usb_source_add(sdi->session, drvc->sr_ctx, 100,
307 handle_events, (void *)sdi);
309 buf = g_malloc(DMM_DATA_SIZE);
310 transfer = libusb_alloc_transfer(0);
311 /* Each transfer request gets 100ms to arrive before it's restarted.
312 * The device only sends 1 transfer/second no matter how many
313 * times you ask, but we want to keep step with the USB events
315 libusb_fill_interrupt_transfer(transfer, usb->devhdl,
316 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
317 (struct sr_dev_inst *)sdi, 100);
318 if ((ret = libusb_submit_transfer(transfer) != 0)) {
319 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
320 libusb_free_transfer(transfer);
328 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
330 if (sdi->status != SR_ST_ACTIVE) {
331 sr_err("Device not active, can't stop acquisition.");
335 sdi->status = SR_ST_STOPPING;
340 SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
341 .name = "victor-dmm",
342 .longname = "Victor DMMs",
345 .cleanup = std_cleanup,
347 .dev_list = std_dev_list,
349 .config_get = config_get,
350 .config_set = config_set,
351 .config_list = config_list,
352 .dev_open = dev_open,
353 .dev_close = dev_close,
354 .dev_acquisition_start = dev_acquisition_start,
355 .dev_acquisition_stop = dev_acquisition_stop,