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 static const uint32_t scanopts[] = {
39 static const uint32_t drvopts[] = {
43 static const uint32_t devopts[] = {
45 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
46 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
47 SR_CONF_CONN | SR_CONF_GET,
50 static GSList *scan(struct sr_dev_driver *di, GSList *options)
52 struct drv_context *drvc;
53 struct dev_context *devc;
54 struct sr_dev_inst *sdi;
55 struct libusb_device_descriptor des;
56 libusb_device **devlist;
59 char connection_id[64];
66 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
67 for (i = 0; devlist[i]; i++) {
68 libusb_get_device_descriptor(devlist[i], &des);
70 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
73 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
75 sdi = g_malloc0(sizeof(struct sr_dev_inst));
76 sdi->status = SR_ST_INACTIVE;
77 sdi->vendor = g_strdup(VICTOR_VENDOR);
78 sdi->connection_id = g_strdup(connection_id);
79 devc = g_malloc0(sizeof(struct dev_context));
80 sr_sw_limits_init(&devc->limits);
83 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
84 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
85 libusb_get_device_address(devlist[i]), NULL);
86 sdi->inst_type = SR_INST_USB;
88 devices = g_slist_append(devices, sdi);
90 libusb_free_device_list(devlist, 1);
92 return std_scan_complete(di, devices);
95 static int dev_open(struct sr_dev_inst *sdi)
97 struct sr_dev_driver *di = sdi->driver;
98 struct drv_context *drvc = di->context;
99 struct sr_usb_dev_inst *usb;
104 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
108 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
109 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
110 sr_err("Failed to detach kernel driver: %s.",
111 libusb_error_name(ret));
116 if ((ret = libusb_claim_interface(usb->devhdl,
117 VICTOR_INTERFACE))) {
118 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
125 static int dev_close(struct sr_dev_inst *sdi)
127 struct sr_usb_dev_inst *usb;
134 libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
135 libusb_close(usb->devhdl);
141 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
142 const struct sr_channel_group *cg)
144 struct dev_context *devc = sdi->priv;
145 struct sr_usb_dev_inst *usb;
152 if (!sdi || !sdi->conn)
155 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
156 *data = g_variant_new_string(str);
158 case SR_CONF_LIMIT_SAMPLES:
159 case SR_CONF_LIMIT_MSEC:
160 return sr_sw_limits_config_get(&devc->limits, key, data);
168 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
169 const struct sr_channel_group *cg)
171 struct dev_context *devc;
177 return sr_sw_limits_config_set(&devc->limits, key, data);
180 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
181 const struct sr_channel_group *cg)
183 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
186 static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
188 struct dev_context *devc;
189 struct sr_dev_inst *sdi;
192 sdi = transfer->user_data;
194 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
195 /* USB device was unplugged. */
196 sr_dev_acquisition_stop(sdi);
197 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
198 sr_dbg("Got %d-byte packet.", transfer->actual_length);
199 if (transfer->actual_length == DMM_DATA_SIZE) {
200 victor_dmm_receive_data(sdi, transfer->buffer);
201 if (sr_sw_limits_check(&devc->limits))
202 sr_dev_acquisition_stop(sdi);
205 /* Anything else is either an error or a timeout, which is fine:
206 * we were just going to send another transfer request anyway. */
208 if (sdi->status == SR_ST_ACTIVE) {
209 /* Send the same request again. */
210 if ((ret = libusb_submit_transfer(transfer) != 0)) {
211 sr_err("Unable to resubmit transfer: %s.",
212 libusb_error_name(ret));
213 g_free(transfer->buffer);
214 libusb_free_transfer(transfer);
215 sr_dev_acquisition_stop(sdi);
218 /* This was the last transfer we're going to receive, so
220 g_free(transfer->buffer);
221 libusb_free_transfer(transfer);
225 static int handle_events(int fd, int revents, void *cb_data)
227 struct dev_context *devc;
228 struct drv_context *drvc;
229 struct sr_dev_inst *sdi;
230 struct sr_dev_driver *di;
241 if (sr_sw_limits_check(&devc->limits))
242 sr_dev_acquisition_stop(sdi);
244 if (sdi->status == SR_ST_STOPPING) {
245 usb_source_remove(sdi->session, drvc->sr_ctx);
247 std_session_send_df_end(sdi);
250 memset(&tv, 0, sizeof(struct timeval));
251 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
257 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
259 struct sr_dev_driver *di = sdi->driver;
260 struct drv_context *drvc = di->context;
261 struct sr_usb_dev_inst *usb;
262 struct libusb_transfer *transfer;
268 std_session_send_df_header(sdi);
270 usb_source_add(sdi->session, drvc->sr_ctx, 100,
271 handle_events, (void *)sdi);
273 buf = g_malloc(DMM_DATA_SIZE);
274 transfer = libusb_alloc_transfer(0);
275 /* Each transfer request gets 100ms to arrive before it's restarted.
276 * The device only sends 1 transfer/second no matter how many
277 * times you ask, but we want to keep step with the USB events
279 libusb_fill_interrupt_transfer(transfer, usb->devhdl,
280 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
281 (struct sr_dev_inst *)sdi, 100);
282 if ((ret = libusb_submit_transfer(transfer) != 0)) {
283 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
284 libusb_free_transfer(transfer);
292 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
294 sdi->status = SR_ST_STOPPING;
299 static struct sr_dev_driver victor_dmm_driver_info = {
300 .name = "victor-dmm",
301 .longname = "Victor DMMs",
304 .cleanup = std_cleanup,
306 .dev_list = std_dev_list,
307 .dev_clear = std_dev_clear,
308 .config_get = config_get,
309 .config_set = config_set,
310 .config_list = config_list,
311 .dev_open = dev_open,
312 .dev_close = dev_close,
313 .dev_acquisition_start = dev_acquisition_start,
314 .dev_acquisition_stop = dev_acquisition_stop,
317 SR_REGISTER_DEV_DRIVER(victor_dmm_driver_info);