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/>.
21 #include "libsigrok.h"
22 #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(void)
82 struct drv_context *drvc;
84 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
85 sr_err("Driver context malloc failed.");
93 static GSList *hw_scan(GSList *options)
95 struct drv_context *drvc;
96 struct dev_context *devc;
97 struct sr_dev_inst *sdi;
98 struct sr_probe *probe;
99 struct libusb_device_descriptor des;
100 libusb_device **devlist;
106 if (!(drvc = di->priv)) {
107 sr_err("Driver was not initialized.");
111 /* USB scan is always authoritative. */
115 libusb_get_device_list(NULL, &devlist);
116 for (i = 0; devlist[i]; i++) {
117 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
118 sr_warn("Failed to get device descriptor: %s",
119 libusb_error_name(ret));
123 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
126 devcnt = g_slist_length(drvc->instances);
127 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, VICTOR_VENDOR,
132 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
136 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
138 sdi->probes = g_slist_append(NULL, probe);
140 if (!(devc->usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
141 libusb_get_device_address(devlist[i]), NULL)))
144 drvc->instances = g_slist_append(drvc->instances, sdi);
145 devices = g_slist_append(devices, sdi);
147 libusb_free_device_list(devlist, 1);
152 static GSList *hw_dev_list(void)
154 struct drv_context *drvc;
156 if (!(drvc = di->priv)) {
157 sr_err("Driver was not initialized.");
161 return drvc->instances;
164 static int hw_dev_open(struct sr_dev_inst *sdi)
166 struct dev_context *devc;
167 libusb_device **devlist;
171 sr_err("Driver was not initialized.");
176 libusb_get_device_list(NULL, &devlist);
177 for (i = 0; devlist[i]; i++) {
178 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
179 || libusb_get_device_address(devlist[i]) != devc->usb->address)
181 if ((ret = libusb_open(devlist[i], &devc->usb->devhdl))) {
182 sr_err("Failed to open device: %s", libusb_error_name(ret));
187 libusb_free_device_list(devlist, 1);
189 sr_err("Device not found.");
193 /* The device reports as HID class, so the kernel would have
195 if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) {
196 if (libusb_detach_kernel_driver(devc->usb->devhdl, 0) < 0) {
197 sr_err("Failed to detach kernel driver.");
202 if ((ret = libusb_claim_interface(devc->usb->devhdl,
203 VICTOR_INTERFACE))) {
204 sr_err("Failed to claim interface: %s", libusb_error_name(ret));
207 sdi->status = SR_ST_ACTIVE;
212 static int hw_dev_close(struct sr_dev_inst *sdi)
214 struct dev_context *devc;
217 sr_err("Driver was not initialized.");
222 if (!devc->usb->devhdl)
226 libusb_release_interface(devc->usb->devhdl, VICTOR_INTERFACE);
227 libusb_close(devc->usb->devhdl);
228 devc->usb->devhdl = NULL;
229 sdi->status = SR_ST_INACTIVE;
234 static int hw_cleanup(void)
236 struct drv_context *drvc;
238 if (!(drvc = di->priv))
239 /* Can get called on an unused driver, doesn't matter. */
249 static int hw_info_get(int info_id, const void **data,
250 const struct sr_dev_inst *sdi)
259 case SR_DI_NUM_PROBES:
260 *data = GINT_TO_POINTER(1);
262 case SR_DI_PROBE_NAMES:
272 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
275 struct dev_context *devc;
280 sr_err("Driver was not initialized.");
284 if (sdi->status != SR_ST_ACTIVE) {
285 sr_err("Device inactive, can't set config options.");
292 case SR_HWCAP_LIMIT_MSEC:
293 devc->limit_msec = *(const int64_t *)value;
294 now = g_get_monotonic_time() / 1000;
295 devc->end_time = now + devc->limit_msec;
296 sr_dbg("setting time limit to %" PRIu64 "ms.",
299 case SR_HWCAP_LIMIT_SAMPLES:
300 devc->limit_samples = *(const uint64_t *)value;
301 sr_dbg("setting sample limit to %" PRIu64 ".",
302 devc->limit_samples);
305 sr_err("Unknown hardware capability: %d.", hwcap);
312 static void receive_transfer(struct libusb_transfer *transfer)
314 struct dev_context *devc;
315 struct sr_dev_inst *sdi;
318 sdi = transfer->user_data;
320 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
321 /* USB device was unplugged. */
322 hw_dev_acquisition_stop(sdi, sdi);
323 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
324 sr_dbg("got %d-byte packet", transfer->actual_length);
325 if (transfer->actual_length == 14) {
329 if (devc->limit_samples) {
330 if (devc->num_samples >= devc->limit_samples)
331 hw_dev_acquisition_stop(sdi, sdi);
335 /* Anything else is either an error or a timeout, which is fine:
336 * we were just going to send another transfer request anyway. */
338 if (sdi->status == SR_ST_ACTIVE) {
339 /* Send the same request again. */
340 if ((ret = libusb_submit_transfer(transfer) != 0)) {
341 sr_err("unable to resubmit transfer: %s", libusb_error_name(ret));
342 libusb_free_transfer(transfer);
343 g_free(transfer->buffer);
344 hw_dev_acquisition_stop(sdi, sdi);
347 /* This was the last transfer we're going to receive, so
349 libusb_free_transfer(transfer);
350 g_free(transfer->buffer);
355 static int handle_events(int fd, int revents, void *cb_data)
357 struct dev_context *devc;
358 struct sr_datafeed_packet packet;
359 struct sr_dev_inst *sdi;
370 if (devc->limit_msec) {
371 now = g_get_monotonic_time() / 1000;
372 if (now > devc->end_time)
373 hw_dev_acquisition_stop(sdi, sdi);
376 if (sdi->status == SR_ST_STOPPING) {
377 for (i = 0; devc->usbfd[i] != -1; i++)
378 sr_source_remove(devc->usbfd[i]);
382 packet.type = SR_DF_END;
383 sr_session_send(cb_data, &packet);
386 memset(&tv, 0, sizeof(struct timeval));
387 libusb_handle_events_timeout_completed(NULL, &tv, NULL);
392 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
395 struct sr_datafeed_packet packet;
396 struct sr_datafeed_header header;
397 struct sr_datafeed_meta_analog meta;
398 struct dev_context *devc;
399 const struct libusb_pollfd **pfd;
400 struct libusb_transfer *transfer;
405 sr_err("Driver was not initialized.");
409 sr_dbg("Starting acquisition.");
412 devc->cb_data = cb_data;
414 /* Send header packet to the session bus. */
415 sr_dbg("Sending SR_DF_HEADER.");
416 packet.type = SR_DF_HEADER;
417 packet.payload = (uint8_t *)&header;
418 header.feed_version = 1;
419 sr_session_send(devc->cb_data, &packet);
421 /* Send metadata about the SR_DF_ANALOG packets to come. */
422 packet.type = SR_DF_META_ANALOG;
423 packet.payload = &meta;
425 sr_session_send(devc->cb_data, &packet);
427 pfd = libusb_get_pollfds(NULL);
428 for (i = 0; pfd[i]; i++) {
429 /* Handle USB events every 100ms, for decent latency. */
430 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
431 handle_events, (void *)sdi);
432 /* We'll need to remove this fd later. */
433 devc->usbfd[i] = pfd[i]->fd;
437 buf = g_try_malloc(14);
438 transfer = libusb_alloc_transfer(0);
439 /* Each transfer request gets 100ms to arrive before it's restarted.
440 * The device only sends 1 transfer/second no matter how many
441 * times you ask, but we want to keep step with the USB events
443 libusb_fill_interrupt_transfer(transfer, devc->usb->devhdl,
444 VICTOR_ENDPOINT, buf, 14, receive_transfer, cb_data, 100);
445 if ((ret = libusb_submit_transfer(transfer) != 0)) {
446 sr_err("unable to submit transfer: %s", libusb_error_name(ret));
447 libusb_free_transfer(transfer);
455 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
461 sr_err("Driver was not initialized.");
465 if (sdi->status != SR_ST_ACTIVE) {
466 sr_err("Device not active, can't stop acquisition.");
470 sdi->status = SR_ST_STOPPING;
475 SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
476 .name = "victor-dmm",
477 .longname = "Victor DMMs",
480 .cleanup = hw_cleanup,
482 .dev_list = hw_dev_list,
483 .dev_clear = clear_instances,
484 .dev_open = hw_dev_open,
485 .dev_close = hw_dev_close,
486 .info_get = hw_info_get,
487 .dev_config_set = hw_dev_config_set,
488 .dev_acquisition_start = hw_dev_acquisition_start,
489 .dev_acquisition_stop = hw_dev_acquisition_stop,