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