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