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