]> sigrok.org Git - libsigrok.git/blame - src/hardware/victor-dmm/api.c
uni-t-dmm: Use software limit helpers
[libsigrok.git] / src / hardware / victor-dmm / api.c
CommitLineData
ac3898d2
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
6ec6c43b 20#include <config.h>
ac3898d2 21#include <glib.h>
7a360375
BV
22#include <libusb.h>
23#include <stdlib.h>
24#include <string.h>
c1aae900 25#include <libsigrok/libsigrok.h>
fa773062
UH
26#include "libsigrok-internal.h"
27#include "protocol.h"
7a360375
BV
28
29#define VICTOR_VID 0x1244
30#define VICTOR_PID 0xd237
31#define VICTOR_VENDOR "Victor"
32#define VICTOR_INTERFACE 0
1a46cc62 33#define VICTOR_ENDPOINT (LIBUSB_ENDPOINT_IN | 1)
ac3898d2
BV
34
35SR_PRIV struct sr_dev_driver victor_dmm_driver_info;
695dc859 36static int dev_acquisition_stop(struct sr_dev_inst *sdi);
7a360375 37
55333928
BV
38static const uint32_t drvopts[] = {
39 SR_CONF_MULTIMETER,
40};
1beccaed 41
a0e0bb41 42static const uint32_t scanopts[] = {
ac046ef8
BV
43 SR_CONF_CONN,
44};
45
f254bc4b 46static const uint32_t devopts[] = {
1953564a 47 SR_CONF_CONTINUOUS,
5827f61b
BV
48 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
49 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
55333928 50 SR_CONF_CONN | SR_CONF_GET,
7a360375
BV
51};
52
4f840ce9 53static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
ac3898d2 54{
f6beaac5 55 return std_init(sr_ctx, di, LOG_PREFIX);
ac3898d2
BV
56}
57
4f840ce9 58static GSList *scan(struct sr_dev_driver *di, GSList *options)
ac3898d2
BV
59{
60 struct drv_context *drvc;
7a360375
BV
61 struct dev_context *devc;
62 struct sr_dev_inst *sdi;
7a360375
BV
63 struct libusb_device_descriptor des;
64 libusb_device **devlist;
ac3898d2 65 GSList *devices;
2a8f2d41 66 int i;
66c91e25 67 char connection_id[64];
ac3898d2
BV
68
69 (void)options;
70
41812aca 71 drvc = di->context;
7a360375 72
ac3898d2 73 devices = NULL;
d4abb463 74 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
7a360375 75 for (i = 0; devlist[i]; i++) {
2a8f2d41 76 libusb_get_device_descriptor(devlist[i], &des);
ac3898d2 77
7a360375
BV
78 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
79 continue;
80
66c91e25
SA
81 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
82
aac29cc1 83 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
84 sdi->status = SR_ST_INACTIVE;
85 sdi->vendor = g_strdup(VICTOR_VENDOR);
7a360375 86 sdi->driver = di;
66c91e25 87 sdi->connection_id = g_strdup(connection_id);
f57d8ffe 88 devc = g_malloc0(sizeof(struct dev_context));
7a360375
BV
89 sdi->priv = devc;
90
5e23fcab 91 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
91219afc
UH
92 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
93 libusb_get_device_address(devlist[i]), NULL);
ac046ef8 94 sdi->inst_type = SR_INST_USB;
7a360375
BV
95
96 drvc->instances = g_slist_append(drvc->instances, sdi);
97 devices = g_slist_append(devices, sdi);
98 }
99 libusb_free_device_list(devlist, 1);
ac3898d2
BV
100
101 return devices;
102}
103
6078d2c9 104static int dev_open(struct sr_dev_inst *sdi)
ac3898d2 105{
4f840ce9 106 struct sr_dev_driver *di = sdi->driver;
41812aca 107 struct drv_context *drvc = di->context;
ac046ef8 108 struct sr_usb_dev_inst *usb;
7a360375
BV
109 libusb_device **devlist;
110 int ret, i;
66c91e25 111 char connection_id[64];
7a360375 112
ac046ef8
BV
113 usb = sdi->conn;
114
d4abb463 115 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
7a360375 116 for (i = 0; devlist[i]; i++) {
66c91e25
SA
117 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
118 if (strcmp(sdi->connection_id, connection_id))
7a360375 119 continue;
ac046ef8 120 if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
fa773062 121 sr_err("Failed to open device: %s.", libusb_error_name(ret));
7a360375
BV
122 return SR_ERR;
123 }
124 break;
125 }
126 libusb_free_device_list(devlist, 1);
127 if (!devlist[i]) {
128 sr_err("Device not found.");
129 return SR_ERR;
130 }
131
132 /* The device reports as HID class, so the kernel would have
133 * claimed it. */
ac046ef8
BV
134 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
135 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
d4928d71
PS
136 sr_err("Failed to detach kernel driver: %s.",
137 libusb_error_name(ret));
7a360375
BV
138 return SR_ERR;
139 }
140 }
141
ac046ef8 142 if ((ret = libusb_claim_interface(usb->devhdl,
7a360375 143 VICTOR_INTERFACE))) {
fa773062 144 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
7a360375
BV
145 return SR_ERR;
146 }
147 sdi->status = SR_ST_ACTIVE;
ac3898d2
BV
148
149 return SR_OK;
150}
151
6078d2c9 152static int dev_close(struct sr_dev_inst *sdi)
ac3898d2 153{
ac046ef8 154 struct sr_usb_dev_inst *usb;
7a360375 155
ac046ef8
BV
156 usb = sdi->conn;
157
158 if (!usb->devhdl)
7a360375
BV
159 /* Nothing to do. */
160 return SR_OK;
161
ac046ef8
BV
162 libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
163 libusb_close(usb->devhdl);
164 usb->devhdl = NULL;
7a360375 165 sdi->status = SR_ST_INACTIVE;
ac3898d2
BV
166
167 return SR_OK;
168}
169
584560f1 170static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 171 const struct sr_channel_group *cg)
ac046ef8
BV
172{
173 struct sr_usb_dev_inst *usb;
174 char str[128];
175
53b4680f 176 (void)cg;
8f996b89 177
584560f1 178 switch (key) {
ac046ef8
BV
179 case SR_CONF_CONN:
180 if (!sdi || !sdi->conn)
181 return SR_ERR_ARG;
182 usb = sdi->conn;
183 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
184 *data = g_variant_new_string(str);
185 break;
186 default:
187 return SR_ERR_NA;
188 }
189
190 return SR_OK;
191}
192
584560f1 193static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 194 const struct sr_channel_group *cg)
ac3898d2 195{
7a360375
BV
196 struct dev_context *devc;
197 gint64 now;
ac3898d2 198
53b4680f 199 (void)cg;
8f996b89 200
e73ffd42
BV
201 if (sdi->status != SR_ST_ACTIVE)
202 return SR_ERR_DEV_CLOSED;
203
7a360375 204 devc = sdi->priv;
dcd438ee 205
584560f1 206 switch (key) {
1953564a 207 case SR_CONF_LIMIT_MSEC:
a59b4eef 208 devc->limit_msec = g_variant_get_uint64(data);
fa773062
UH
209 now = g_get_monotonic_time() / 1000;
210 devc->end_time = now + devc->limit_msec;
fa773062 211 break;
1953564a 212 case SR_CONF_LIMIT_SAMPLES:
a59b4eef 213 devc->limit_samples = g_variant_get_uint64(data);
fa773062 214 break;
ac3898d2 215 default:
dcd438ee 216 return SR_ERR_NA;
ac3898d2
BV
217 }
218
dcd438ee 219 return SR_OK;
ac3898d2
BV
220}
221
584560f1 222static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 223 const struct sr_channel_group *cg)
a1c743fc 224{
a1c743fc 225 (void)sdi;
53b4680f 226 (void)cg;
a1c743fc
BV
227
228 switch (key) {
ac046ef8 229 case SR_CONF_SCAN_OPTIONS:
584560f1 230 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 231 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
ac046ef8 232 break;
9a6517d1 233 case SR_CONF_DEVICE_OPTIONS:
55333928
BV
234 if (!sdi)
235 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
236 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
237 else
238 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
239 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
9a6517d1 240 break;
a1c743fc 241 default:
bd6fbf62 242 return SR_ERR_NA;
a1c743fc
BV
243 }
244
245 return SR_OK;
246}
247
55462b8b 248static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
7a360375
BV
249{
250 struct dev_context *devc;
251 struct sr_dev_inst *sdi;
252 int ret;
253
254 sdi = transfer->user_data;
255 devc = sdi->priv;
256 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
257 /* USB device was unplugged. */
695dc859 258 dev_acquisition_stop(sdi);
7a360375 259 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
fa773062 260 sr_dbg("Got %d-byte packet.", transfer->actual_length);
ff945683
BV
261 if (transfer->actual_length == DMM_DATA_SIZE) {
262 victor_dmm_receive_data(sdi, transfer->buffer);
7a360375
BV
263 if (devc->limit_samples) {
264 if (devc->num_samples >= devc->limit_samples)
695dc859 265 dev_acquisition_stop(sdi);
7a360375
BV
266 }
267 }
268 }
269 /* Anything else is either an error or a timeout, which is fine:
270 * we were just going to send another transfer request anyway. */
271
272 if (sdi->status == SR_ST_ACTIVE) {
273 /* Send the same request again. */
274 if ((ret = libusb_submit_transfer(transfer) != 0)) {
fa773062
UH
275 sr_err("Unable to resubmit transfer: %s.",
276 libusb_error_name(ret));
7a360375 277 g_free(transfer->buffer);
9ec7ff94 278 libusb_free_transfer(transfer);
695dc859 279 dev_acquisition_stop(sdi);
7a360375
BV
280 }
281 } else {
282 /* This was the last transfer we're going to receive, so
283 * clean up now. */
7a360375 284 g_free(transfer->buffer);
9ec7ff94 285 libusb_free_transfer(transfer);
7a360375 286 }
7a360375
BV
287}
288
289static int handle_events(int fd, int revents, void *cb_data)
290{
291 struct dev_context *devc;
4f840ce9 292 struct drv_context *drvc;
7a360375 293 struct sr_dev_inst *sdi;
4f840ce9 294 struct sr_dev_driver *di;
7a360375
BV
295 struct timeval tv;
296 gint64 now;
7a360375
BV
297
298 (void)fd;
299 (void)revents;
300
301 sdi = cb_data;
302 devc = sdi->priv;
4f840ce9 303 di = sdi->driver;
41812aca 304 drvc = di->context;
7a360375
BV
305
306 if (devc->limit_msec) {
307 now = g_get_monotonic_time() / 1000;
308 if (now > devc->end_time)
695dc859 309 dev_acquisition_stop(sdi);
7a360375
BV
310 }
311
312 if (sdi->status == SR_ST_STOPPING) {
102f1239 313 usb_source_remove(sdi->session, drvc->sr_ctx);
6078d2c9 314 dev_close(sdi);
3be42bc2 315 std_session_send_df_end(sdi, LOG_PREFIX);
7a360375
BV
316 }
317
318 memset(&tv, 0, sizeof(struct timeval));
d4abb463
PS
319 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
320 NULL);
7a360375
BV
321
322 return TRUE;
323}
324
695dc859 325static int dev_acquisition_start(const struct sr_dev_inst *sdi)
ac3898d2 326{
4f840ce9 327 struct sr_dev_driver *di = sdi->driver;
41812aca 328 struct drv_context *drvc = di->context;
ac046ef8 329 struct sr_usb_dev_inst *usb;
7a360375 330 struct libusb_transfer *transfer;
6c60facc 331 int ret;
7a360375
BV
332 unsigned char *buf;
333
e73ffd42
BV
334 if (sdi->status != SR_ST_ACTIVE)
335 return SR_ERR_DEV_CLOSED;
336
ac046ef8 337 usb = sdi->conn;
7a360375 338
695dc859 339 std_session_send_df_header(sdi, LOG_PREFIX);
7a360375 340
102f1239
BV
341 usb_source_add(sdi->session, drvc->sr_ctx, 100,
342 handle_events, (void *)sdi);
7a360375 343
a95f142e 344 buf = g_malloc(DMM_DATA_SIZE);
7a360375
BV
345 transfer = libusb_alloc_transfer(0);
346 /* Each transfer request gets 100ms to arrive before it's restarted.
347 * The device only sends 1 transfer/second no matter how many
348 * times you ask, but we want to keep step with the USB events
349 * handling above. */
ac046ef8 350 libusb_fill_interrupt_transfer(transfer, usb->devhdl,
fa773062 351 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
695dc859 352 (struct sr_dev_inst *)sdi, 100);
7a360375 353 if ((ret = libusb_submit_transfer(transfer) != 0)) {
fa773062 354 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
7a360375
BV
355 libusb_free_transfer(transfer);
356 g_free(buf);
357 return SR_ERR;
358 }
ac3898d2
BV
359
360 return SR_OK;
361}
362
695dc859 363static int dev_acquisition_stop(struct sr_dev_inst *sdi)
ac3898d2 364{
ac3898d2 365 if (sdi->status != SR_ST_ACTIVE) {
7a360375 366 sr_err("Device not active, can't stop acquisition.");
ac3898d2
BV
367 return SR_ERR;
368 }
369
7a360375 370 sdi->status = SR_ST_STOPPING;
ac3898d2
BV
371
372 return SR_OK;
373}
374
375SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
376 .name = "victor-dmm",
7a360375 377 .longname = "Victor DMMs",
ac3898d2 378 .api_version = 1,
6078d2c9 379 .init = init,
700d6b64 380 .cleanup = std_cleanup,
6078d2c9 381 .scan = scan,
c01bf34c 382 .dev_list = std_dev_list,
a6630742 383 .dev_clear = NULL,
ac046ef8 384 .config_get = config_get,
035a1078 385 .config_set = config_set,
a1c743fc 386 .config_list = config_list,
6078d2c9
UH
387 .dev_open = dev_open,
388 .dev_close = dev_close,
389 .dev_acquisition_start = dev_acquisition_start,
390 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 391 .context = NULL,
ac3898d2 392};