]> sigrok.org Git - libsigrok.git/blame - src/hardware/victor-dmm/api.c
fx2lafw: Replace obsoleted strncmp() calls.
[libsigrok.git] / src / 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;
6078d2c9 36static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
7a360375 37
55333928
BV
38static const uint32_t drvopts[] = {
39 SR_CONF_MULTIMETER,
40};
a0e0bb41 41static const uint32_t scanopts[] = {
ac046ef8
BV
42 SR_CONF_CONN,
43};
44
f254bc4b 45static const uint32_t devopts[] = {
1953564a 46 SR_CONF_CONTINUOUS,
5827f61b
BV
47 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
48 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
55333928 49 SR_CONF_CONN | SR_CONF_GET,
7a360375
BV
50};
51
6078d2c9 52static int init(struct sr_context *sr_ctx)
ac3898d2 53{
f6beaac5 54 return std_init(sr_ctx, di, LOG_PREFIX);
ac3898d2
BV
55}
56
6078d2c9 57static GSList *scan(GSList *options)
ac3898d2
BV
58{
59 struct drv_context *drvc;
7a360375
BV
60 struct dev_context *devc;
61 struct sr_dev_inst *sdi;
ba7dd8bb 62 struct sr_channel *ch;
7a360375
BV
63 struct libusb_device_descriptor des;
64 libusb_device **devlist;
ac3898d2 65 GSList *devices;
66c91e25
SA
66 int ret, i;
67 char connection_id[64];
ac3898d2
BV
68
69 (void)options;
70
4b97c74e 71 drvc = di->priv;
7a360375 72
ac3898d2 73 devices = NULL;
d4abb463 74 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
7a360375
BV
75 for (i = 0; devlist[i]; i++) {
76 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
77 sr_warn("Failed to get device descriptor: %s",
78 libusb_error_name(ret));
79 continue;
80 }
ac3898d2 81
7a360375
BV
82 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
83 continue;
84
66c91e25
SA
85 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
86
aac29cc1 87 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
88 sdi->status = SR_ST_INACTIVE;
89 sdi->vendor = g_strdup(VICTOR_VENDOR);
7a360375 90 sdi->driver = di;
66c91e25 91 sdi->connection_id = g_strdup(connection_id);
f57d8ffe 92 devc = g_malloc0(sizeof(struct dev_context));
7a360375
BV
93 sdi->priv = devc;
94
c368e6f3 95 ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1");
ba7dd8bb 96 sdi->channels = g_slist_append(NULL, ch);
7a360375 97
91219afc
UH
98 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
99 libusb_get_device_address(devlist[i]), NULL);
ac046ef8 100 sdi->inst_type = SR_INST_USB;
7a360375
BV
101
102 drvc->instances = g_slist_append(drvc->instances, sdi);
103 devices = g_slist_append(devices, sdi);
104 }
105 libusb_free_device_list(devlist, 1);
ac3898d2
BV
106
107 return devices;
108}
109
6078d2c9 110static GSList *dev_list(void)
ac3898d2 111{
0e94d524 112 return ((struct drv_context *)(di->priv))->instances;
ac3898d2
BV
113}
114
6078d2c9 115static int dev_open(struct sr_dev_inst *sdi)
ac3898d2 116{
d4abb463 117 struct drv_context *drvc = di->priv;
ac046ef8 118 struct sr_usb_dev_inst *usb;
7a360375
BV
119 libusb_device **devlist;
120 int ret, i;
66c91e25 121 char connection_id[64];
7a360375
BV
122
123 if (!di->priv) {
124 sr_err("Driver was not initialized.");
125 return SR_ERR;
126 }
127
ac046ef8
BV
128 usb = sdi->conn;
129
d4abb463 130 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
7a360375 131 for (i = 0; devlist[i]; i++) {
66c91e25
SA
132 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
133 if (strcmp(sdi->connection_id, connection_id))
7a360375 134 continue;
ac046ef8 135 if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
fa773062 136 sr_err("Failed to open device: %s.", libusb_error_name(ret));
7a360375
BV
137 return SR_ERR;
138 }
139 break;
140 }
141 libusb_free_device_list(devlist, 1);
142 if (!devlist[i]) {
143 sr_err("Device not found.");
144 return SR_ERR;
145 }
146
147 /* The device reports as HID class, so the kernel would have
148 * claimed it. */
ac046ef8
BV
149 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
150 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
d4928d71
PS
151 sr_err("Failed to detach kernel driver: %s.",
152 libusb_error_name(ret));
7a360375
BV
153 return SR_ERR;
154 }
155 }
156
ac046ef8 157 if ((ret = libusb_claim_interface(usb->devhdl,
7a360375 158 VICTOR_INTERFACE))) {
fa773062 159 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
7a360375
BV
160 return SR_ERR;
161 }
162 sdi->status = SR_ST_ACTIVE;
ac3898d2
BV
163
164 return SR_OK;
165}
166
6078d2c9 167static int dev_close(struct sr_dev_inst *sdi)
ac3898d2 168{
ac046ef8 169 struct sr_usb_dev_inst *usb;
7a360375
BV
170
171 if (!di->priv) {
172 sr_err("Driver was not initialized.");
173 return SR_ERR;
174 }
175
ac046ef8
BV
176 usb = sdi->conn;
177
178 if (!usb->devhdl)
7a360375
BV
179 /* Nothing to do. */
180 return SR_OK;
181
ac046ef8
BV
182 libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
183 libusb_close(usb->devhdl);
184 usb->devhdl = NULL;
7a360375 185 sdi->status = SR_ST_INACTIVE;
ac3898d2
BV
186
187 return SR_OK;
188}
189
6078d2c9 190static int cleanup(void)
ac3898d2 191{
8d18d266 192 int ret;
7a360375
BV
193 struct drv_context *drvc;
194
195 if (!(drvc = di->priv))
196 /* Can get called on an unused driver, doesn't matter. */
197 return SR_OK;
ac3898d2 198
a6630742
BV
199
200 ret = std_dev_clear(di, NULL);
7a360375
BV
201 g_free(drvc);
202 di->priv = NULL;
ac3898d2 203
8d18d266 204 return ret;
ac3898d2
BV
205}
206
584560f1 207static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 208 const struct sr_channel_group *cg)
ac046ef8
BV
209{
210 struct sr_usb_dev_inst *usb;
211 char str[128];
212
53b4680f 213 (void)cg;
8f996b89 214
584560f1 215 switch (key) {
ac046ef8
BV
216 case SR_CONF_CONN:
217 if (!sdi || !sdi->conn)
218 return SR_ERR_ARG;
219 usb = sdi->conn;
220 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
221 *data = g_variant_new_string(str);
222 break;
223 default:
224 return SR_ERR_NA;
225 }
226
227 return SR_OK;
228}
229
584560f1 230static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 231 const struct sr_channel_group *cg)
ac3898d2 232{
7a360375
BV
233 struct dev_context *devc;
234 gint64 now;
ac3898d2
BV
235 int ret;
236
53b4680f 237 (void)cg;
8f996b89 238
e73ffd42
BV
239 if (sdi->status != SR_ST_ACTIVE)
240 return SR_ERR_DEV_CLOSED;
241
7a360375
BV
242 if (!di->priv) {
243 sr_err("Driver was not initialized.");
244 return SR_ERR;
245 }
246
7a360375 247 devc = sdi->priv;
ac3898d2 248 ret = SR_OK;
584560f1 249 switch (key) {
1953564a 250 case SR_CONF_LIMIT_MSEC:
a59b4eef 251 devc->limit_msec = g_variant_get_uint64(data);
fa773062
UH
252 now = g_get_monotonic_time() / 1000;
253 devc->end_time = now + devc->limit_msec;
254 sr_dbg("Setting time limit to %" PRIu64 "ms.",
255 devc->limit_msec);
256 break;
1953564a 257 case SR_CONF_LIMIT_SAMPLES:
a59b4eef 258 devc->limit_samples = g_variant_get_uint64(data);
fa773062
UH
259 sr_dbg("Setting sample limit to %" PRIu64 ".",
260 devc->limit_samples);
261 break;
ac3898d2 262 default:
bd6fbf62 263 ret = SR_ERR_NA;
ac3898d2
BV
264 }
265
266 return ret;
267}
268
584560f1 269static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 270 const struct sr_channel_group *cg)
a1c743fc 271{
a1c743fc 272 (void)sdi;
53b4680f 273 (void)cg;
a1c743fc
BV
274
275 switch (key) {
ac046ef8 276 case SR_CONF_SCAN_OPTIONS:
584560f1 277 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 278 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
ac046ef8 279 break;
9a6517d1 280 case SR_CONF_DEVICE_OPTIONS:
55333928
BV
281 if (!sdi)
282 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
283 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
284 else
285 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
286 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
9a6517d1 287 break;
a1c743fc 288 default:
bd6fbf62 289 return SR_ERR_NA;
a1c743fc
BV
290 }
291
292 return SR_OK;
293}
294
7a360375
BV
295static void receive_transfer(struct libusb_transfer *transfer)
296{
297 struct dev_context *devc;
298 struct sr_dev_inst *sdi;
299 int ret;
300
301 sdi = transfer->user_data;
302 devc = sdi->priv;
303 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
304 /* USB device was unplugged. */
6078d2c9 305 dev_acquisition_stop(sdi, sdi);
7a360375 306 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
fa773062 307 sr_dbg("Got %d-byte packet.", transfer->actual_length);
ff945683
BV
308 if (transfer->actual_length == DMM_DATA_SIZE) {
309 victor_dmm_receive_data(sdi, transfer->buffer);
7a360375
BV
310 if (devc->limit_samples) {
311 if (devc->num_samples >= devc->limit_samples)
6078d2c9 312 dev_acquisition_stop(sdi, sdi);
7a360375
BV
313 }
314 }
315 }
316 /* Anything else is either an error or a timeout, which is fine:
317 * we were just going to send another transfer request anyway. */
318
319 if (sdi->status == SR_ST_ACTIVE) {
320 /* Send the same request again. */
321 if ((ret = libusb_submit_transfer(transfer) != 0)) {
fa773062
UH
322 sr_err("Unable to resubmit transfer: %s.",
323 libusb_error_name(ret));
7a360375 324 g_free(transfer->buffer);
9ec7ff94 325 libusb_free_transfer(transfer);
6078d2c9 326 dev_acquisition_stop(sdi, sdi);
7a360375
BV
327 }
328 } else {
329 /* This was the last transfer we're going to receive, so
330 * clean up now. */
7a360375 331 g_free(transfer->buffer);
9ec7ff94 332 libusb_free_transfer(transfer);
7a360375 333 }
7a360375
BV
334}
335
336static int handle_events(int fd, int revents, void *cb_data)
337{
338 struct dev_context *devc;
d4abb463 339 struct drv_context *drvc = di->priv;
7a360375
BV
340 struct sr_datafeed_packet packet;
341 struct sr_dev_inst *sdi;
342 struct timeval tv;
343 gint64 now;
7a360375
BV
344
345 (void)fd;
346 (void)revents;
347
348 sdi = cb_data;
349 devc = sdi->priv;
350
351 if (devc->limit_msec) {
352 now = g_get_monotonic_time() / 1000;
353 if (now > devc->end_time)
6078d2c9 354 dev_acquisition_stop(sdi, sdi);
7a360375
BV
355 }
356
357 if (sdi->status == SR_ST_STOPPING) {
102f1239 358 usb_source_remove(sdi->session, drvc->sr_ctx);
7a360375 359
6078d2c9 360 dev_close(sdi);
7a360375
BV
361
362 packet.type = SR_DF_END;
363 sr_session_send(cb_data, &packet);
364 }
365
366 memset(&tv, 0, sizeof(struct timeval));
d4abb463
PS
367 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
368 NULL);
7a360375
BV
369
370 return TRUE;
371}
372
6078d2c9 373static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
ac3898d2 374{
7a360375 375 struct dev_context *devc;
d4abb463 376 struct drv_context *drvc = di->priv;
ac046ef8 377 struct sr_usb_dev_inst *usb;
7a360375 378 struct libusb_transfer *transfer;
6c60facc 379 int ret;
7a360375
BV
380 unsigned char *buf;
381
e73ffd42
BV
382 if (sdi->status != SR_ST_ACTIVE)
383 return SR_ERR_DEV_CLOSED;
384
7a360375
BV
385 if (!di->priv) {
386 sr_err("Driver was not initialized.");
387 return SR_ERR;
388 }
389
7a360375 390 devc = sdi->priv;
ac046ef8 391 usb = sdi->conn;
7a360375
BV
392 devc->cb_data = cb_data;
393
394 /* Send header packet to the session bus. */
29a27196 395 std_session_send_df_header(cb_data, LOG_PREFIX);
7a360375 396
102f1239
BV
397 usb_source_add(sdi->session, drvc->sr_ctx, 100,
398 handle_events, (void *)sdi);
7a360375 399
ff945683 400 buf = g_try_malloc(DMM_DATA_SIZE);
7a360375
BV
401 transfer = libusb_alloc_transfer(0);
402 /* Each transfer request gets 100ms to arrive before it's restarted.
403 * The device only sends 1 transfer/second no matter how many
404 * times you ask, but we want to keep step with the USB events
405 * handling above. */
ac046ef8 406 libusb_fill_interrupt_transfer(transfer, usb->devhdl,
fa773062
UH
407 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
408 cb_data, 100);
7a360375 409 if ((ret = libusb_submit_transfer(transfer) != 0)) {
fa773062 410 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
7a360375
BV
411 libusb_free_transfer(transfer);
412 g_free(buf);
413 return SR_ERR;
414 }
ac3898d2
BV
415
416 return SR_OK;
417}
418
6078d2c9 419static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
ac3898d2
BV
420{
421 (void)cb_data;
422
7a360375
BV
423 if (!di->priv) {
424 sr_err("Driver was not initialized.");
425 return SR_ERR;
426 }
427
ac3898d2 428 if (sdi->status != SR_ST_ACTIVE) {
7a360375 429 sr_err("Device not active, can't stop acquisition.");
ac3898d2
BV
430 return SR_ERR;
431 }
432
7a360375 433 sdi->status = SR_ST_STOPPING;
ac3898d2
BV
434
435 return SR_OK;
436}
437
438SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
439 .name = "victor-dmm",
7a360375 440 .longname = "Victor DMMs",
ac3898d2 441 .api_version = 1,
6078d2c9
UH
442 .init = init,
443 .cleanup = cleanup,
444 .scan = scan,
445 .dev_list = dev_list,
a6630742 446 .dev_clear = NULL,
ac046ef8 447 .config_get = config_get,
035a1078 448 .config_set = config_set,
a1c743fc 449 .config_list = config_list,
6078d2c9
UH
450 .dev_open = dev_open,
451 .dev_close = dev_close,
452 .dev_acquisition_start = dev_acquisition_start,
453 .dev_acquisition_stop = dev_acquisition_stop,
ac3898d2
BV
454 .priv = NULL,
455};