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