]> sigrok.org Git - libsigrok.git/blame - hardware/victor-dmm/api.c
hardware: Call libusb_error_name() in all USB-related error messages
[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 }
1ebe4b4e 87 drvc->sr_ctx = sr_ctx;
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;
d4abb463 115 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
7a360375
BV
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);
fa773062
UH
127 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
128 VICTOR_VENDOR, NULL, NULL)))
7a360375
BV
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 166 struct dev_context *devc;
d4abb463 167 struct drv_context *drvc = di->priv;
7a360375
BV
168 libusb_device **devlist;
169 int ret, i;
170
171 if (!di->priv) {
172 sr_err("Driver was not initialized.");
173 return SR_ERR;
174 }
175
176 devc = sdi->priv;
d4abb463 177 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
7a360375
BV
178 for (i = 0; devlist[i]; i++) {
179 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
180 || libusb_get_device_address(devlist[i]) != devc->usb->address)
181 continue;
182 if ((ret = libusb_open(devlist[i], &devc->usb->devhdl))) {
fa773062 183 sr_err("Failed to open device: %s.", libusb_error_name(ret));
7a360375
BV
184 return SR_ERR;
185 }
186 break;
187 }
188 libusb_free_device_list(devlist, 1);
189 if (!devlist[i]) {
190 sr_err("Device not found.");
191 return SR_ERR;
192 }
193
194 /* The device reports as HID class, so the kernel would have
195 * claimed it. */
196 if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) {
d4928d71
PS
197 if ((ret = libusb_detach_kernel_driver(devc->usb->devhdl, 0)) < 0) {
198 sr_err("Failed to detach kernel driver: %s.",
199 libusb_error_name(ret));
7a360375
BV
200 return SR_ERR;
201 }
202 }
203
204 if ((ret = libusb_claim_interface(devc->usb->devhdl,
205 VICTOR_INTERFACE))) {
fa773062 206 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
7a360375
BV
207 return SR_ERR;
208 }
209 sdi->status = SR_ST_ACTIVE;
ac3898d2
BV
210
211 return SR_OK;
212}
213
214static int hw_dev_close(struct sr_dev_inst *sdi)
215{
7a360375
BV
216 struct dev_context *devc;
217
218 if (!di->priv) {
219 sr_err("Driver was not initialized.");
220 return SR_ERR;
221 }
222
223 devc = sdi->priv;
224 if (!devc->usb->devhdl)
225 /* Nothing to do. */
226 return SR_OK;
227
228 libusb_release_interface(devc->usb->devhdl, VICTOR_INTERFACE);
229 libusb_close(devc->usb->devhdl);
230 devc->usb->devhdl = NULL;
231 sdi->status = SR_ST_INACTIVE;
ac3898d2
BV
232
233 return SR_OK;
234}
235
236static int hw_cleanup(void)
237{
7a360375
BV
238 struct drv_context *drvc;
239
240 if (!(drvc = di->priv))
241 /* Can get called on an unused driver, doesn't matter. */
242 return SR_OK;
ac3898d2 243
7a360375
BV
244 clear_instances();
245 g_free(drvc);
246 di->priv = NULL;
ac3898d2
BV
247
248 return SR_OK;
249}
250
251static int hw_info_get(int info_id, const void **data,
252 const struct sr_dev_inst *sdi)
253{
7a360375
BV
254 (void)sdi;
255
ac3898d2 256 switch (info_id) {
fa773062
UH
257 case SR_DI_HWCAPS:
258 *data = hwcaps;
259 break;
260 case SR_DI_NUM_PROBES:
261 *data = GINT_TO_POINTER(1);
262 break;
263 case SR_DI_PROBE_NAMES:
264 *data = probe_names;
265 break;
266 default:
267 sr_err("Unknown info_id: %d.", info_id);
268 return SR_ERR_ARG;
ac3898d2
BV
269 }
270
271 return SR_OK;
272}
273
274static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
275 const void *value)
276{
7a360375
BV
277 struct dev_context *devc;
278 gint64 now;
ac3898d2
BV
279 int ret;
280
7a360375
BV
281 if (!di->priv) {
282 sr_err("Driver was not initialized.");
283 return SR_ERR;
284 }
285
ac3898d2
BV
286 if (sdi->status != SR_ST_ACTIVE) {
287 sr_err("Device inactive, can't set config options.");
288 return SR_ERR;
289 }
290
7a360375 291 devc = sdi->priv;
ac3898d2
BV
292 ret = SR_OK;
293 switch (hwcap) {
fa773062
UH
294 case SR_HWCAP_LIMIT_MSEC:
295 devc->limit_msec = *(const int64_t *)value;
296 now = g_get_monotonic_time() / 1000;
297 devc->end_time = now + devc->limit_msec;
298 sr_dbg("Setting time limit to %" PRIu64 "ms.",
299 devc->limit_msec);
300 break;
301 case SR_HWCAP_LIMIT_SAMPLES:
302 devc->limit_samples = *(const uint64_t *)value;
303 sr_dbg("Setting sample limit to %" PRIu64 ".",
304 devc->limit_samples);
305 break;
ac3898d2
BV
306 default:
307 sr_err("Unknown hardware capability: %d.", hwcap);
308 ret = SR_ERR_ARG;
309 }
310
311 return ret;
312}
313
7a360375
BV
314static void receive_transfer(struct libusb_transfer *transfer)
315{
316 struct dev_context *devc;
317 struct sr_dev_inst *sdi;
318 int ret;
319
320 sdi = transfer->user_data;
321 devc = sdi->priv;
322 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
323 /* USB device was unplugged. */
324 hw_dev_acquisition_stop(sdi, sdi);
325 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
fa773062 326 sr_dbg("Got %d-byte packet.", transfer->actual_length);
ff945683
BV
327 if (transfer->actual_length == DMM_DATA_SIZE) {
328 victor_dmm_receive_data(sdi, transfer->buffer);
7a360375
BV
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)) {
fa773062
UH
341 sr_err("Unable to resubmit transfer: %s.",
342 libusb_error_name(ret));
7a360375
BV
343 libusb_free_transfer(transfer);
344 g_free(transfer->buffer);
345 hw_dev_acquisition_stop(sdi, sdi);
346 }
347 } else {
348 /* This was the last transfer we're going to receive, so
349 * clean up now. */
350 libusb_free_transfer(transfer);
351 g_free(transfer->buffer);
352 }
7a360375
BV
353}
354
355static int handle_events(int fd, int revents, void *cb_data)
356{
357 struct dev_context *devc;
d4abb463 358 struct drv_context *drvc = di->priv;
7a360375
BV
359 struct sr_datafeed_packet packet;
360 struct sr_dev_inst *sdi;
361 struct timeval tv;
362 gint64 now;
363 int i;
364
365 (void)fd;
366 (void)revents;
367
368 sdi = cb_data;
369 devc = sdi->priv;
370
371 if (devc->limit_msec) {
372 now = g_get_monotonic_time() / 1000;
373 if (now > devc->end_time)
374 hw_dev_acquisition_stop(sdi, sdi);
375 }
376
377 if (sdi->status == SR_ST_STOPPING) {
378 for (i = 0; devc->usbfd[i] != -1; i++)
379 sr_source_remove(devc->usbfd[i]);
380
381 hw_dev_close(sdi);
382
383 packet.type = SR_DF_END;
384 sr_session_send(cb_data, &packet);
385 }
386
387 memset(&tv, 0, sizeof(struct timeval));
d4abb463
PS
388 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
389 NULL);
7a360375
BV
390
391 return TRUE;
392}
393
ac3898d2
BV
394static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
395 void *cb_data)
396{
7a360375
BV
397 struct sr_datafeed_packet packet;
398 struct sr_datafeed_header header;
399 struct sr_datafeed_meta_analog meta;
400 struct dev_context *devc;
d4abb463 401 struct drv_context *drvc = di->priv;
7a360375
BV
402 const struct libusb_pollfd **pfd;
403 struct libusb_transfer *transfer;
404 int ret, i;
405 unsigned char *buf;
406
407 if (!di->priv) {
408 sr_err("Driver was not initialized.");
409 return SR_ERR;
410 }
411
412 sr_dbg("Starting acquisition.");
413
414 devc = sdi->priv;
415 devc->cb_data = cb_data;
416
417 /* Send header packet to the session bus. */
418 sr_dbg("Sending SR_DF_HEADER.");
419 packet.type = SR_DF_HEADER;
420 packet.payload = (uint8_t *)&header;
421 header.feed_version = 1;
422 sr_session_send(devc->cb_data, &packet);
423
424 /* Send metadata about the SR_DF_ANALOG packets to come. */
425 packet.type = SR_DF_META_ANALOG;
426 packet.payload = &meta;
427 meta.num_probes = 1;
428 sr_session_send(devc->cb_data, &packet);
429
d4abb463 430 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
7a360375
BV
431 for (i = 0; pfd[i]; i++) {
432 /* Handle USB events every 100ms, for decent latency. */
433 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
434 handle_events, (void *)sdi);
435 /* We'll need to remove this fd later. */
436 devc->usbfd[i] = pfd[i]->fd;
437 }
438 devc->usbfd[i] = -1;
439
ff945683 440 buf = g_try_malloc(DMM_DATA_SIZE);
7a360375
BV
441 transfer = libusb_alloc_transfer(0);
442 /* Each transfer request gets 100ms to arrive before it's restarted.
443 * The device only sends 1 transfer/second no matter how many
444 * times you ask, but we want to keep step with the USB events
445 * handling above. */
446 libusb_fill_interrupt_transfer(transfer, devc->usb->devhdl,
fa773062
UH
447 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
448 cb_data, 100);
7a360375 449 if ((ret = libusb_submit_transfer(transfer) != 0)) {
fa773062 450 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
7a360375
BV
451 libusb_free_transfer(transfer);
452 g_free(buf);
453 return SR_ERR;
454 }
ac3898d2
BV
455
456 return SR_OK;
457}
458
7a360375 459static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
ac3898d2
BV
460{
461 (void)cb_data;
462
7a360375
BV
463 if (!di->priv) {
464 sr_err("Driver was not initialized.");
465 return SR_ERR;
466 }
467
ac3898d2 468 if (sdi->status != SR_ST_ACTIVE) {
7a360375 469 sr_err("Device not active, can't stop acquisition.");
ac3898d2
BV
470 return SR_ERR;
471 }
472
7a360375 473 sdi->status = SR_ST_STOPPING;
ac3898d2
BV
474
475 return SR_OK;
476}
477
478SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
479 .name = "victor-dmm",
7a360375 480 .longname = "Victor DMMs",
ac3898d2
BV
481 .api_version = 1,
482 .init = hw_init,
483 .cleanup = hw_cleanup,
484 .scan = hw_scan,
485 .dev_list = hw_dev_list,
486 .dev_clear = clear_instances,
487 .dev_open = hw_dev_open,
488 .dev_close = hw_dev_close,
489 .info_get = hw_info_get,
490 .dev_config_set = hw_dev_config_set,
491 .dev_acquisition_start = hw_dev_acquisition_start,
492 .dev_acquisition_stop = hw_dev_acquisition_stop,
493 .priv = NULL,
494};