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