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 dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
38 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_context *sr_ctx)
54 return std_init(sr_ctx, di, LOG_PREFIX);
57 static GSList *scan(GSList *options)
59 struct drv_context *drvc;
60 struct dev_context *devc;
61 struct sr_dev_inst *sdi;
62 struct sr_channel *ch;
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 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
77 sr_warn("Failed to get device descriptor: %s",
78 libusb_error_name(ret));
82 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
85 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
87 sdi = g_malloc0(sizeof(struct sr_dev_inst));
88 sdi->status = SR_ST_INACTIVE;
89 sdi->vendor = g_strdup(VICTOR_VENDOR);
91 sdi->connection_id = g_strdup(connection_id);
92 devc = g_malloc0(sizeof(struct dev_context));
95 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
97 sdi->channels = g_slist_append(NULL, ch);
99 if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
100 libusb_get_device_address(devlist[i]), NULL)))
102 sdi->inst_type = SR_INST_USB;
104 drvc->instances = g_slist_append(drvc->instances, sdi);
105 devices = g_slist_append(devices, sdi);
107 libusb_free_device_list(devlist, 1);
112 static GSList *dev_list(void)
114 return ((struct drv_context *)(di->priv))->instances;
117 static int dev_open(struct sr_dev_inst *sdi)
119 struct drv_context *drvc = di->priv;
120 struct sr_usb_dev_inst *usb;
121 libusb_device **devlist;
123 char connection_id[64];
126 sr_err("Driver was not initialized.");
132 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
133 for (i = 0; devlist[i]; i++) {
134 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
135 if (strcmp(sdi->connection_id, connection_id))
137 if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
138 sr_err("Failed to open device: %s.", libusb_error_name(ret));
143 libusb_free_device_list(devlist, 1);
145 sr_err("Device not found.");
149 /* The device reports as HID class, so the kernel would have
151 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
152 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
153 sr_err("Failed to detach kernel driver: %s.",
154 libusb_error_name(ret));
159 if ((ret = libusb_claim_interface(usb->devhdl,
160 VICTOR_INTERFACE))) {
161 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
164 sdi->status = SR_ST_ACTIVE;
169 static int dev_close(struct sr_dev_inst *sdi)
171 struct sr_usb_dev_inst *usb;
174 sr_err("Driver was not initialized.");
184 libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
185 libusb_close(usb->devhdl);
187 sdi->status = SR_ST_INACTIVE;
192 static int cleanup(void)
195 struct drv_context *drvc;
197 if (!(drvc = di->priv))
198 /* Can get called on an unused driver, doesn't matter. */
202 ret = std_dev_clear(di, NULL);
209 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
210 const struct sr_channel_group *cg)
212 struct sr_usb_dev_inst *usb;
219 if (!sdi || !sdi->conn)
222 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
223 *data = g_variant_new_string(str);
232 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
233 const struct sr_channel_group *cg)
235 struct dev_context *devc;
241 if (sdi->status != SR_ST_ACTIVE)
242 return SR_ERR_DEV_CLOSED;
245 sr_err("Driver was not initialized.");
252 case SR_CONF_LIMIT_MSEC:
253 devc->limit_msec = g_variant_get_uint64(data);
254 now = g_get_monotonic_time() / 1000;
255 devc->end_time = now + devc->limit_msec;
256 sr_dbg("Setting time limit to %" PRIu64 "ms.",
259 case SR_CONF_LIMIT_SAMPLES:
260 devc->limit_samples = g_variant_get_uint64(data);
261 sr_dbg("Setting sample limit to %" PRIu64 ".",
262 devc->limit_samples);
271 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
272 const struct sr_channel_group *cg)
278 case SR_CONF_SCAN_OPTIONS:
279 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
280 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
282 case SR_CONF_DEVICE_OPTIONS:
284 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
285 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
287 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
288 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
297 static void receive_transfer(struct libusb_transfer *transfer)
299 struct dev_context *devc;
300 struct sr_dev_inst *sdi;
303 sdi = transfer->user_data;
305 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
306 /* USB device was unplugged. */
307 dev_acquisition_stop(sdi, sdi);
308 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
309 sr_dbg("Got %d-byte packet.", transfer->actual_length);
310 if (transfer->actual_length == DMM_DATA_SIZE) {
311 victor_dmm_receive_data(sdi, transfer->buffer);
312 if (devc->limit_samples) {
313 if (devc->num_samples >= devc->limit_samples)
314 dev_acquisition_stop(sdi, sdi);
318 /* Anything else is either an error or a timeout, which is fine:
319 * we were just going to send another transfer request anyway. */
321 if (sdi->status == SR_ST_ACTIVE) {
322 /* Send the same request again. */
323 if ((ret = libusb_submit_transfer(transfer) != 0)) {
324 sr_err("Unable to resubmit transfer: %s.",
325 libusb_error_name(ret));
326 g_free(transfer->buffer);
327 libusb_free_transfer(transfer);
328 dev_acquisition_stop(sdi, sdi);
331 /* This was the last transfer we're going to receive, so
333 g_free(transfer->buffer);
334 libusb_free_transfer(transfer);
338 static int handle_events(int fd, int revents, void *cb_data)
340 struct dev_context *devc;
341 struct drv_context *drvc = di->priv;
342 struct sr_datafeed_packet packet;
343 struct sr_dev_inst *sdi;
353 if (devc->limit_msec) {
354 now = g_get_monotonic_time() / 1000;
355 if (now > devc->end_time)
356 dev_acquisition_stop(sdi, sdi);
359 if (sdi->status == SR_ST_STOPPING) {
360 usb_source_remove(sdi->session, drvc->sr_ctx);
364 packet.type = SR_DF_END;
365 sr_session_send(cb_data, &packet);
368 memset(&tv, 0, sizeof(struct timeval));
369 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
375 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
377 struct dev_context *devc;
378 struct drv_context *drvc = di->priv;
379 struct sr_usb_dev_inst *usb;
380 struct libusb_transfer *transfer;
384 if (sdi->status != SR_ST_ACTIVE)
385 return SR_ERR_DEV_CLOSED;
388 sr_err("Driver was not initialized.");
394 devc->cb_data = cb_data;
396 /* Send header packet to the session bus. */
397 std_session_send_df_header(cb_data, LOG_PREFIX);
399 usb_source_add(sdi->session, drvc->sr_ctx, 100,
400 handle_events, (void *)sdi);
402 buf = g_try_malloc(DMM_DATA_SIZE);
403 transfer = libusb_alloc_transfer(0);
404 /* Each transfer request gets 100ms to arrive before it's restarted.
405 * The device only sends 1 transfer/second no matter how many
406 * times you ask, but we want to keep step with the USB events
408 libusb_fill_interrupt_transfer(transfer, usb->devhdl,
409 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
411 if ((ret = libusb_submit_transfer(transfer) != 0)) {
412 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
413 libusb_free_transfer(transfer);
421 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
426 sr_err("Driver was not initialized.");
430 if (sdi->status != SR_ST_ACTIVE) {
431 sr_err("Device not active, can't stop acquisition.");
435 sdi->status = SR_ST_STOPPING;
440 SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
441 .name = "victor-dmm",
442 .longname = "Victor DMMs",
447 .dev_list = dev_list,
449 .config_get = config_get,
450 .config_set = config_set,
451 .config_list = config_list,
452 .dev_open = dev_open,
453 .dev_close = dev_close,
454 .dev_acquisition_start = dev_acquisition_start,
455 .dev_acquisition_stop = dev_acquisition_stop,