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 int hwcaps[] = {
42 SR_HWCAP_LIMIT_SAMPLES,
47 static const char *probe_names[] = {
52 /* Properly close and free all devices. */
53 static int clear_instances(void)
55 struct sr_dev_inst *sdi;
56 struct drv_context *drvc;
57 struct dev_context *devc;
60 if (!(drvc = di->priv))
61 /* Can get called on an unused driver, doesn't matter. */
64 for (l = drvc->instances; l; l = l->next) {
67 if (!(devc = sdi->priv))
70 sr_usb_dev_inst_free(devc->usb);
71 sr_dev_inst_free(sdi);
74 g_slist_free(drvc->instances);
75 drvc->instances = NULL;
80 static int hw_init(struct sr_context *sr_ctx)
82 struct drv_context *drvc;
84 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
85 sr_err("Driver context malloc failed.");
88 drvc->sr_ctx = sr_ctx;
94 static GSList *hw_scan(GSList *options)
96 struct drv_context *drvc;
97 struct dev_context *devc;
98 struct sr_dev_inst *sdi;
99 struct sr_probe *probe;
100 struct libusb_device_descriptor des;
101 libusb_device **devlist;
107 if (!(drvc = di->priv)) {
108 sr_err("Driver was not initialized.");
112 /* USB scan is always authoritative. */
116 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
117 for (i = 0; devlist[i]; i++) {
118 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
119 sr_warn("Failed to get device descriptor: %s",
120 libusb_error_name(ret));
124 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
127 devcnt = g_slist_length(drvc->instances);
128 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
129 VICTOR_VENDOR, NULL, NULL)))
133 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
137 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
139 sdi->probes = g_slist_append(NULL, probe);
141 if (!(devc->usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
142 libusb_get_device_address(devlist[i]), NULL)))
145 drvc->instances = g_slist_append(drvc->instances, sdi);
146 devices = g_slist_append(devices, sdi);
148 libusb_free_device_list(devlist, 1);
153 static GSList *hw_dev_list(void)
155 struct drv_context *drvc;
157 if (!(drvc = di->priv)) {
158 sr_err("Driver was not initialized.");
162 return drvc->instances;
165 static int hw_dev_open(struct sr_dev_inst *sdi)
167 struct dev_context *devc;
168 struct drv_context *drvc = di->priv;
169 libusb_device **devlist;
173 sr_err("Driver was not initialized.");
178 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
179 for (i = 0; devlist[i]; i++) {
180 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
181 || libusb_get_device_address(devlist[i]) != devc->usb->address)
183 if ((ret = libusb_open(devlist[i], &devc->usb->devhdl))) {
184 sr_err("Failed to open device: %s.", libusb_error_name(ret));
189 libusb_free_device_list(devlist, 1);
191 sr_err("Device not found.");
195 /* The device reports as HID class, so the kernel would have
197 if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) {
198 if ((ret = libusb_detach_kernel_driver(devc->usb->devhdl, 0)) < 0) {
199 sr_err("Failed to detach kernel driver: %s.",
200 libusb_error_name(ret));
205 if ((ret = libusb_claim_interface(devc->usb->devhdl,
206 VICTOR_INTERFACE))) {
207 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
210 sdi->status = SR_ST_ACTIVE;
215 static int hw_dev_close(struct sr_dev_inst *sdi)
217 struct dev_context *devc;
220 sr_err("Driver was not initialized.");
225 if (!devc->usb->devhdl)
229 libusb_release_interface(devc->usb->devhdl, VICTOR_INTERFACE);
230 libusb_close(devc->usb->devhdl);
231 devc->usb->devhdl = NULL;
232 sdi->status = SR_ST_INACTIVE;
237 static int hw_cleanup(void)
239 struct drv_context *drvc;
241 if (!(drvc = di->priv))
242 /* Can get called on an unused driver, doesn't matter. */
252 static int hw_info_get(int info_id, const void **data,
253 const struct sr_dev_inst *sdi)
261 case SR_DI_NUM_PROBES:
262 *data = GINT_TO_POINTER(1);
264 case SR_DI_PROBE_NAMES:
268 sr_err("Unknown info_id: %d.", info_id);
275 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
278 struct dev_context *devc;
283 sr_err("Driver was not initialized.");
287 if (sdi->status != SR_ST_ACTIVE) {
288 sr_err("Device inactive, can't set config options.");
295 case SR_HWCAP_LIMIT_MSEC:
296 devc->limit_msec = *(const int64_t *)value;
297 now = g_get_monotonic_time() / 1000;
298 devc->end_time = now + devc->limit_msec;
299 sr_dbg("Setting time limit to %" PRIu64 "ms.",
302 case SR_HWCAP_LIMIT_SAMPLES:
303 devc->limit_samples = *(const uint64_t *)value;
304 sr_dbg("Setting sample limit to %" PRIu64 ".",
305 devc->limit_samples);
308 sr_err("Unknown hardware capability: %d.", hwcap);
315 static void receive_transfer(struct libusb_transfer *transfer)
317 struct dev_context *devc;
318 struct sr_dev_inst *sdi;
321 sdi = transfer->user_data;
323 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
324 /* USB device was unplugged. */
325 hw_dev_acquisition_stop(sdi, sdi);
326 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
327 sr_dbg("Got %d-byte packet.", transfer->actual_length);
328 if (transfer->actual_length == DMM_DATA_SIZE) {
329 victor_dmm_receive_data(sdi, transfer->buffer);
330 if (devc->limit_samples) {
331 if (devc->num_samples >= devc->limit_samples)
332 hw_dev_acquisition_stop(sdi, sdi);
336 /* Anything else is either an error or a timeout, which is fine:
337 * we were just going to send another transfer request anyway. */
339 if (sdi->status == SR_ST_ACTIVE) {
340 /* Send the same request again. */
341 if ((ret = libusb_submit_transfer(transfer) != 0)) {
342 sr_err("Unable to resubmit transfer: %s.",
343 libusb_error_name(ret));
344 g_free(transfer->buffer);
345 libusb_free_transfer(transfer);
346 hw_dev_acquisition_stop(sdi, sdi);
349 /* This was the last transfer we're going to receive, so
351 g_free(transfer->buffer);
352 libusb_free_transfer(transfer);
356 static int handle_events(int fd, int revents, void *cb_data)
358 struct dev_context *devc;
359 struct drv_context *drvc = di->priv;
360 struct sr_datafeed_packet packet;
361 struct sr_dev_inst *sdi;
372 if (devc->limit_msec) {
373 now = g_get_monotonic_time() / 1000;
374 if (now > devc->end_time)
375 hw_dev_acquisition_stop(sdi, sdi);
378 if (sdi->status == SR_ST_STOPPING) {
379 for (i = 0; devc->usbfd[i] != -1; i++)
380 sr_source_remove(devc->usbfd[i]);
384 packet.type = SR_DF_END;
385 sr_session_send(cb_data, &packet);
388 memset(&tv, 0, sizeof(struct timeval));
389 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
395 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
398 struct sr_datafeed_packet packet;
399 struct sr_datafeed_header header;
400 struct sr_datafeed_meta_analog meta;
401 struct dev_context *devc;
402 struct drv_context *drvc = di->priv;
403 const struct libusb_pollfd **pfd;
404 struct libusb_transfer *transfer;
409 sr_err("Driver was not initialized.");
413 sr_dbg("Starting acquisition.");
416 devc->cb_data = cb_data;
418 /* Send header packet to the session bus. */
419 sr_dbg("Sending SR_DF_HEADER.");
420 packet.type = SR_DF_HEADER;
421 packet.payload = (uint8_t *)&header;
422 header.feed_version = 1;
423 sr_session_send(devc->cb_data, &packet);
425 /* Send metadata about the SR_DF_ANALOG packets to come. */
426 packet.type = SR_DF_META_ANALOG;
427 packet.payload = &meta;
429 sr_session_send(devc->cb_data, &packet);
431 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
432 for (i = 0; pfd[i]; i++) {
433 /* Handle USB events every 100ms, for decent latency. */
434 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
435 handle_events, (void *)sdi);
436 /* We'll need to remove this fd later. */
437 devc->usbfd[i] = pfd[i]->fd;
441 buf = g_try_malloc(DMM_DATA_SIZE);
442 transfer = libusb_alloc_transfer(0);
443 /* Each transfer request gets 100ms to arrive before it's restarted.
444 * The device only sends 1 transfer/second no matter how many
445 * times you ask, but we want to keep step with the USB events
447 libusb_fill_interrupt_transfer(transfer, devc->usb->devhdl,
448 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
450 if ((ret = libusb_submit_transfer(transfer) != 0)) {
451 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
452 libusb_free_transfer(transfer);
460 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
465 sr_err("Driver was not initialized.");
469 if (sdi->status != SR_ST_ACTIVE) {
470 sr_err("Device not active, can't stop acquisition.");
474 sdi->status = SR_ST_STOPPING;
479 SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
480 .name = "victor-dmm",
481 .longname = "Victor DMMs",
484 .cleanup = hw_cleanup,
486 .dev_list = hw_dev_list,
487 .dev_clear = clear_instances,
488 .dev_open = hw_dev_open,
489 .dev_close = hw_dev_close,
490 .info_get = hw_info_get,
491 .dev_config_set = hw_dev_config_set,
492 .dev_acquisition_start = hw_dev_acquisition_start,
493 .dev_acquisition_stop = hw_dev_acquisition_stop,