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)
34 static int dev_acquisition_stop(struct sr_dev_inst *sdi);
36 static const uint32_t drvopts[] = {
40 static const uint32_t scanopts[] = {
44 static const uint32_t devopts[] = {
46 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
47 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
48 SR_CONF_CONN | SR_CONF_GET,
51 static GSList *scan(struct sr_dev_driver *di, GSList *options)
53 struct drv_context *drvc;
54 struct dev_context *devc;
55 struct sr_dev_inst *sdi;
56 struct libusb_device_descriptor des;
57 libusb_device **devlist;
60 char connection_id[64];
67 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
68 for (i = 0; devlist[i]; i++) {
69 libusb_get_device_descriptor(devlist[i], &des);
71 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
74 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
76 sdi = g_malloc0(sizeof(struct sr_dev_inst));
77 sdi->status = SR_ST_INACTIVE;
78 sdi->vendor = g_strdup(VICTOR_VENDOR);
79 sdi->connection_id = g_strdup(connection_id);
80 devc = g_malloc0(sizeof(struct dev_context));
81 sr_sw_limits_init(&devc->limits);
84 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
85 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
86 libusb_get_device_address(devlist[i]), NULL);
87 sdi->inst_type = SR_INST_USB;
89 devices = g_slist_append(devices, sdi);
91 libusb_free_device_list(devlist, 1);
93 return std_scan_complete(di, devices);
96 static int dev_open(struct sr_dev_inst *sdi)
98 struct sr_dev_driver *di = sdi->driver;
99 struct drv_context *drvc = di->context;
100 struct sr_usb_dev_inst *usb;
105 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
109 /* The device reports as HID class, so the kernel would have
111 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
112 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
113 sr_err("Failed to detach kernel driver: %s.",
114 libusb_error_name(ret));
119 if ((ret = libusb_claim_interface(usb->devhdl,
120 VICTOR_INTERFACE))) {
121 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
124 sdi->status = SR_ST_ACTIVE;
129 static int dev_close(struct sr_dev_inst *sdi)
131 struct sr_usb_dev_inst *usb;
139 libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
140 libusb_close(usb->devhdl);
142 sdi->status = SR_ST_INACTIVE;
147 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
148 const struct sr_channel_group *cg)
150 struct dev_context *devc = sdi->priv;
151 struct sr_usb_dev_inst *usb;
158 if (!sdi || !sdi->conn)
161 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
162 *data = g_variant_new_string(str);
164 case SR_CONF_LIMIT_SAMPLES:
165 case SR_CONF_LIMIT_MSEC:
166 return sr_sw_limits_config_get(&devc->limits, key, data);
174 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
175 const struct sr_channel_group *cg)
177 struct dev_context *devc;
181 if (sdi->status != SR_ST_ACTIVE)
182 return SR_ERR_DEV_CLOSED;
186 return sr_sw_limits_config_set(&devc->limits, key, data);
189 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
190 const struct sr_channel_group *cg)
196 case SR_CONF_SCAN_OPTIONS:
197 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
198 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
200 case SR_CONF_DEVICE_OPTIONS:
202 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
203 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
205 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
206 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
215 static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
217 struct dev_context *devc;
218 struct sr_dev_inst *sdi;
221 sdi = transfer->user_data;
223 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
224 /* USB device was unplugged. */
225 dev_acquisition_stop(sdi);
226 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
227 sr_dbg("Got %d-byte packet.", transfer->actual_length);
228 if (transfer->actual_length == DMM_DATA_SIZE) {
229 victor_dmm_receive_data(sdi, transfer->buffer);
230 if (sr_sw_limits_check(&devc->limits))
231 dev_acquisition_stop(sdi);
234 /* Anything else is either an error or a timeout, which is fine:
235 * we were just going to send another transfer request anyway. */
237 if (sdi->status == SR_ST_ACTIVE) {
238 /* Send the same request again. */
239 if ((ret = libusb_submit_transfer(transfer) != 0)) {
240 sr_err("Unable to resubmit transfer: %s.",
241 libusb_error_name(ret));
242 g_free(transfer->buffer);
243 libusb_free_transfer(transfer);
244 dev_acquisition_stop(sdi);
247 /* This was the last transfer we're going to receive, so
249 g_free(transfer->buffer);
250 libusb_free_transfer(transfer);
254 static int handle_events(int fd, int revents, void *cb_data)
256 struct dev_context *devc;
257 struct drv_context *drvc;
258 struct sr_dev_inst *sdi;
259 struct sr_dev_driver *di;
270 if (sr_sw_limits_check(&devc->limits))
271 dev_acquisition_stop(sdi);
273 if (sdi->status == SR_ST_STOPPING) {
274 usb_source_remove(sdi->session, drvc->sr_ctx);
276 std_session_send_df_end(sdi);
279 memset(&tv, 0, sizeof(struct timeval));
280 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
286 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
288 struct sr_dev_driver *di = sdi->driver;
289 struct drv_context *drvc = di->context;
290 struct sr_usb_dev_inst *usb;
291 struct libusb_transfer *transfer;
295 if (sdi->status != SR_ST_ACTIVE)
296 return SR_ERR_DEV_CLOSED;
300 std_session_send_df_header(sdi);
302 usb_source_add(sdi->session, drvc->sr_ctx, 100,
303 handle_events, (void *)sdi);
305 buf = g_malloc(DMM_DATA_SIZE);
306 transfer = libusb_alloc_transfer(0);
307 /* Each transfer request gets 100ms to arrive before it's restarted.
308 * The device only sends 1 transfer/second no matter how many
309 * times you ask, but we want to keep step with the USB events
311 libusb_fill_interrupt_transfer(transfer, usb->devhdl,
312 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
313 (struct sr_dev_inst *)sdi, 100);
314 if ((ret = libusb_submit_transfer(transfer) != 0)) {
315 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
316 libusb_free_transfer(transfer);
324 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
326 if (sdi->status != SR_ST_ACTIVE) {
327 sr_err("Device not active, can't stop acquisition.");
331 sdi->status = SR_ST_STOPPING;
336 static struct sr_dev_driver victor_dmm_driver_info = {
337 .name = "victor-dmm",
338 .longname = "Victor DMMs",
341 .cleanup = std_cleanup,
343 .dev_list = std_dev_list,
345 .config_get = config_get,
346 .config_set = config_set,
347 .config_list = config_list,
348 .dev_open = dev_open,
349 .dev_close = dev_close,
350 .dev_acquisition_start = dev_acquisition_start,
351 .dev_acquisition_stop = dev_acquisition_stop,
354 SR_REGISTER_DEV_DRIVER(victor_dmm_driver_info);