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