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