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