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