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