]> sigrok.org Git - libsigrok.git/blame - src/hardware/sysclk-lwla/api.c
Introduce standard cleanup helper
[libsigrok.git] / src / hardware / sysclk-lwla / api.c
CommitLineData
aeaad0b0
DE
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.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
6ec6c43b 20#include <config.h>
5874e88d
DE
21#include <glib.h>
22#include <libusb.h>
23#include <stdlib.h>
24#include <string.h>
c1aae900 25#include <libsigrok/libsigrok.h>
407b6e2c 26#include <libsigrok-internal.h>
515ab088 27#include "protocol.h"
5874e88d 28
be64f90b
DE
29/* Supported device scan options.
30 */
a0e0bb41 31static const uint32_t scanopts[] = {
99c76642
DE
32 SR_CONF_CONN,
33};
34
be64f90b
DE
35/* Driver capabilities.
36 */
37static const uint32_t drvopts[] = {
5874e88d 38 SR_CONF_LOGIC_ANALYZER,
5874e88d
DE
39};
40
be64f90b
DE
41/* Supported trigger match conditions.
42 */
bbe7e48a
BV
43static const int32_t trigger_matches[] = {
44 SR_TRIGGER_ZERO,
45 SR_TRIGGER_ONE,
46 SR_TRIGGER_RISING,
47 SR_TRIGGER_FALLING,
48};
49
be64f90b 50/* Names assigned to available trigger sources.
5874e88d 51 */
be64f90b
DE
52static const char *const trigger_source_names[] = {
53 [TRIGGER_CHANNELS] = "CH",
54 [TRIGGER_EXT_TRG] = "TRG",
5874e88d 55};
aeaad0b0 56
be64f90b 57/* Names assigned to available edge slope choices.
e6e54bd2 58 */
be64f90b
DE
59static const char *const signal_edge_names[] = {
60 [EDGE_POSITIVE] = "r",
61 [EDGE_NEGATIVE] = "f",
62};
e6e54bd2 63
be64f90b 64/* Initialize the SysClk LWLA driver.
e6e54bd2 65 */
4f840ce9 66static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
aeaad0b0
DE
67{
68 return std_init(sr_ctx, di, LOG_PREFIX);
69}
70
be64f90b
DE
71/* Create a new sigrok device instance for the indicated LWLA model.
72 */
73static struct sr_dev_inst *dev_inst_new(const struct model_info *model)
43db3436
DE
74{
75 struct sr_dev_inst *sdi;
76 struct dev_context *devc;
5e23fcab
ML
77 int i;
78 char name[8];
43db3436 79
be64f90b 80 /* Initialize private device context. */
f57d8ffe 81 devc = g_malloc0(sizeof(struct dev_context));
be64f90b
DE
82 devc->model = model;
83 devc->active_fpga_config = FPGA_NOCONF;
84 devc->cfg_rle = TRUE;
85 devc->samplerate = model->samplerates[0];
86 devc->channel_mask = (UINT64_C(1) << model->num_channels) - 1;
43db3436 87
be64f90b 88 /* Create sigrok device instance. */
aac29cc1 89 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
90 sdi->status = SR_ST_INACTIVE;
91 sdi->vendor = g_strdup(VENDOR_NAME);
be64f90b 92 sdi->model = g_strdup(model->name);
43db3436 93 sdi->priv = devc;
be64f90b
DE
94
95 /* Generate list of logic channels. */
96 for (i = 0; i < model->num_channels; i++) {
5e23fcab 97 /* The LWLA series simply number channels from CH1 to CHxx. */
fe473123
DE
98 g_snprintf(name, sizeof(name), "CH%d", i + 1);
99 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
5e23fcab 100 }
43db3436
DE
101
102 return sdi;
103}
104
be64f90b
DE
105/* Create a new device instance for a libusb device if it is a SysClk LWLA
106 * device and also matches the connection specification.
107 */
108static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches,
109 libusb_device *dev)
110{
111 GSList *node;
112 struct sr_usb_dev_inst *usb;
113 const struct model_info *model;
114 struct sr_dev_inst *sdi;
115 struct libusb_device_descriptor des;
d64b5f43 116 int bus, address, ret;
be64f90b 117 unsigned int vid, pid;
be64f90b
DE
118
119 bus = libusb_get_bus_number(dev);
120 address = libusb_get_device_address(dev);
121
122 for (node = conn_matches; node != NULL; node = node->next) {
123 usb = node->data;
124 if (usb && usb->bus == bus && usb->address == address)
125 break; /* found */
126 }
127 if (conn_matches && !node)
128 return NULL; /* no match */
129
130 ret = libusb_get_device_descriptor(dev, &des);
131 if (ret != 0) {
132 sr_err("Failed to get USB device descriptor: %s.",
133 libusb_error_name(ret));
134 return NULL;
135 }
136 vid = des.idVendor;
137 pid = des.idProduct;
138
139 /* Create sigrok device instance. */
140 if (vid == USB_VID_SYSCLK && pid == USB_PID_LWLA1016) {
141 model = &lwla1016_info;
142 } else if (vid == USB_VID_SYSCLK && pid == USB_PID_LWLA1034) {
143 model = &lwla1034_info;
144 } else {
145 if (conn_matches)
146 sr_warn("USB device %d.%d (%04x:%04x) is not a"
147 " SysClk LWLA.", bus, address, vid, pid);
148 return NULL;
149 }
150 sdi = dev_inst_new(model);
151
152 sdi->inst_type = SR_INST_USB;
153 sdi->conn = sr_usb_dev_inst_new(bus, address, NULL);
154
155 return sdi;
156}
157
158/* Scan for SysClk LWLA devices and create a device instance for each one.
159 */
4f840ce9 160static GSList *scan(struct sr_dev_driver *di, GSList *options)
aeaad0b0 161{
be64f90b 162 GSList *conn_devices, *devices, *node;
aeaad0b0 163 struct drv_context *drvc;
5874e88d 164 struct sr_dev_inst *sdi;
7ebe9b9e
DE
165 struct sr_config *src;
166 const char *conn;
be64f90b
DE
167 libusb_device **devlist;
168 ssize_t num_devs, i;
aeaad0b0 169
41812aca 170 drvc = di->context;
be64f90b
DE
171 conn = NULL;
172 conn_devices = NULL;
173 devices = NULL;
5874e88d 174
7ebe9b9e
DE
175 for (node = options; node != NULL; node = node->next) {
176 src = node->data;
177 if (src->key == SR_CONF_CONN) {
178 conn = g_variant_get_string(src->data, NULL);
179 break;
180 }
181 }
be64f90b
DE
182 if (conn) {
183 /* Find devices matching the connection specification. */
184 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
185 }
5874e88d 186
be64f90b
DE
187 /* List all libusb devices. */
188 num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
189 if (num_devs < 0) {
190 sr_err("Failed to list USB devices: %s.",
191 libusb_error_name(num_devs));
192 g_slist_free_full(conn_devices,
193 (GDestroyNotify)&sr_usb_dev_inst_free);
194 return NULL;
195 }
5874e88d 196
be64f90b
DE
197 /* Scan the USB device list for matching LWLA devices. */
198 for (i = 0; i < num_devs; i++) {
199 sdi = dev_inst_new_matching(conn_devices, devlist[i]);
200 if (!sdi)
201 continue; /* no match */
5874e88d 202
43db3436 203 /* Register device instance with driver. */
be64f90b 204 sdi->driver = di;
5874e88d
DE
205 drvc->instances = g_slist_append(drvc->instances, sdi);
206 devices = g_slist_append(devices, sdi);
207 }
aeaad0b0 208
be64f90b
DE
209 libusb_free_device_list(devlist, 1);
210 g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free);
aeaad0b0
DE
211
212 return devices;
213}
214
be64f90b
DE
215/* Return the list of devices found during scan.
216 */
4f840ce9 217static GSList *dev_list(const struct sr_dev_driver *di)
aeaad0b0 218{
d64b5f43 219 return ((struct drv_context *)(di->context))->instances;
5874e88d
DE
220}
221
be64f90b
DE
222/* Destroy the private device context.
223 */
5874e88d
DE
224static void clear_dev_context(void *priv)
225{
226 struct dev_context *devc;
227
228 devc = priv;
229
be64f90b
DE
230 if (devc->acquisition) {
231 sr_err("Cannot clear device context during acquisition!");
d64b5f43 232 return; /* Leak and pray. */
be64f90b 233 }
5874e88d
DE
234 sr_dbg("Device context cleared.");
235
5874e88d 236 g_free(devc);
aeaad0b0
DE
237}
238
be64f90b
DE
239/* Destroy all device instances.
240 */
4f840ce9 241static int dev_clear(const struct sr_dev_driver *di)
aeaad0b0 242{
5874e88d 243 return std_dev_clear(di, &clear_dev_context);
aeaad0b0
DE
244}
245
93ed0241
DE
246/* Drain any pending data from the USB transfer buffers on the device.
247 * This may be necessary e.g. after a crash or generally to clean up after
248 * an abnormal condition.
249 */
250static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint)
251{
d64b5f43 252 int drained, xfer_len, ret;
93ed0241 253 unsigned char buf[512];
93ed0241
DE
254 const unsigned int drain_timeout_ms = 10;
255
256 drained = 0;
257 do {
258 xfer_len = 0;
259 ret = libusb_bulk_transfer(usb->devhdl, endpoint,
b3cfc6e9 260 buf, sizeof(buf), &xfer_len,
93ed0241
DE
261 drain_timeout_ms);
262 drained += xfer_len;
263 } while (ret == LIBUSB_SUCCESS && xfer_len != 0);
264
265 if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) {
266 sr_err("Failed to drain USB endpoint %u: %s.",
267 endpoint & (LIBUSB_ENDPOINT_IN - 1),
268 libusb_error_name(ret));
269 return SR_ERR;
270 }
271 if (drained > 0) {
272 sr_warn("Drained %d bytes from USB endpoint %u.",
273 drained, endpoint & (LIBUSB_ENDPOINT_IN - 1));
274 }
d64b5f43 275
93ed0241
DE
276 return SR_OK;
277}
278
be64f90b
DE
279/* Open and initialize device.
280 */
aeaad0b0
DE
281static int dev_open(struct sr_dev_inst *sdi)
282{
5874e88d 283 struct drv_context *drvc;
be64f90b 284 struct dev_context *devc;
5874e88d 285 struct sr_usb_dev_inst *usb;
d64b5f43 286 int i, ret;
aeaad0b0 287
ce19d4c6 288 drvc = sdi->driver->context;
be64f90b
DE
289 devc = sdi->priv;
290 usb = sdi->conn;
aeaad0b0 291
5874e88d
DE
292 if (!drvc) {
293 sr_err("Driver was not initialized.");
294 return SR_ERR;
295 }
c81069b3
DE
296 if (sdi->status != SR_ST_INACTIVE) {
297 sr_err("Device already open.");
298 return SR_ERR;
299 }
5874e88d 300
e35a4592
DE
301 /* Try the whole shebang three times, fingers crossed. */
302 for (i = 0; i < 3; i++) {
303 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
304 if (ret != SR_OK)
305 return ret;
306
307 ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
308 if (ret != LIBUSB_SUCCESS) {
309 sr_err("Failed to set USB configuration: %s.",
310 libusb_error_name(ret));
311 sr_usb_close(usb);
312 return SR_ERR;
313 }
ad6181e2 314
e35a4592
DE
315 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
316 if (ret != LIBUSB_SUCCESS) {
317 sr_err("Failed to claim interface: %s.",
318 libusb_error_name(ret));
319 sr_usb_close(usb);
320 return SR_ERR;
321 }
be64f90b 322
e35a4592
DE
323 ret = drain_usb(usb, EP_REPLY);
324 if (ret != SR_OK) {
325 sr_usb_close(usb);
326 return ret;
327 }
328 /* This delay appears to be necessary for reliable operation. */
329 g_usleep(30 * 1000);
93ed0241 330
e35a4592 331 sdi->status = SR_ST_ACTIVE;
5874e88d 332
e35a4592
DE
333 devc->active_fpga_config = FPGA_NOCONF;
334 devc->short_transfer_quirk = FALSE;
335 devc->state = STATE_IDLE;
be64f90b 336
e35a4592 337 ret = (*devc->model->apply_fpga_config)(sdi);
be64f90b 338
e35a4592
DE
339 if (ret == SR_OK)
340 ret = (*devc->model->device_init_check)(sdi);
341 if (ret == SR_OK)
342 break;
be64f90b 343
e35a4592 344 /* Rinse and repeat. */
c81069b3 345 sdi->status = SR_ST_INACTIVE;
be64f90b 346 sr_usb_close(usb);
c81069b3 347 }
e35a4592
DE
348
349 if (ret == SR_OK && devc->short_transfer_quirk)
78648577
DE
350 sr_warn("Short transfer quirk detected! "
351 "Memory reads will be slow.");
e35a4592 352 return ret;
aeaad0b0
DE
353}
354
be64f90b
DE
355/* Shutdown and close device.
356 */
aeaad0b0
DE
357static int dev_close(struct sr_dev_inst *sdi)
358{
be64f90b 359 struct drv_context *drvc;
c81069b3 360 struct dev_context *devc;
be64f90b 361 struct sr_usb_dev_inst *usb;
c81069b3 362 int ret;
5874e88d 363
be64f90b
DE
364 drvc = sdi->driver->context;
365 devc = sdi->priv;
366 usb = sdi->conn;
367
368 if (!drvc) {
5874e88d
DE
369 sr_err("Driver was not initialized.");
370 return SR_ERR;
371 }
be64f90b
DE
372 if (sdi->status == SR_ST_INACTIVE) {
373 sr_dbg("Device already closed.");
5874e88d 374 return SR_OK;
c81069b3 375 }
be64f90b
DE
376 if (devc->acquisition) {
377 sr_err("Cannot close device during acquisition!");
378 /* Request stop, leak handle, and prepare for the worst. */
379 devc->cancel_requested = TRUE;
380 return SR_ERR_BUG;
381 }
382
6358f0a9 383 sdi->status = SR_ST_INACTIVE;
5874e88d 384
be64f90b
DE
385 /* Download of the shutdown bitstream, if any. */
386 ret = (*devc->model->apply_fpga_config)(sdi);
c81069b3 387 if (ret != SR_OK)
be64f90b 388 sr_warn("Unable to shut down device.");
5874e88d
DE
389
390 libusb_release_interface(usb->devhdl, USB_INTERFACE);
c81069b3 391 sr_usb_close(usb);
5874e88d 392
c81069b3 393 return ret;
aeaad0b0
DE
394}
395
be64f90b
DE
396/* Check whether the device options contain a specific key.
397 * Also match against get/set/list bits if specified.
398 */
399static int has_devopt(const struct model_info *model, uint32_t key)
aeaad0b0 400{
be64f90b
DE
401 unsigned int i;
402
403 for (i = 0; i < model->num_devopts; i++) {
404 if ((model->devopts[i] & (SR_CONF_MASK | key)) == key)
405 return TRUE;
406 }
d64b5f43 407
be64f90b 408 return FALSE;
aeaad0b0
DE
409}
410
be64f90b
DE
411/* Read device configuration setting.
412 */
584560f1 413static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 414 const struct sr_channel_group *cg)
aeaad0b0 415{
5874e88d 416 struct dev_context *devc;
7ed80817 417 unsigned int idx;
aeaad0b0 418
53b4680f 419 (void)cg;
aeaad0b0 420
5874e88d
DE
421 if (!sdi)
422 return SR_ERR_ARG;
423
424 devc = sdi->priv;
425
be64f90b
DE
426 if (!has_devopt(devc->model, key | SR_CONF_GET))
427 return SR_ERR_NA;
428
aeaad0b0 429 switch (key) {
5874e88d
DE
430 case SR_CONF_SAMPLERATE:
431 *data = g_variant_new_uint64(devc->samplerate);
432 break;
29d58767
DE
433 case SR_CONF_LIMIT_MSEC:
434 *data = g_variant_new_uint64(devc->limit_msec);
435 break;
5874e88d
DE
436 case SR_CONF_LIMIT_SAMPLES:
437 *data = g_variant_new_uint64(devc->limit_samples);
438 break;
be64f90b
DE
439 case SR_CONF_RLE:
440 *data = g_variant_new_boolean(devc->cfg_rle);
441 break;
5874e88d 442 case SR_CONF_EXTERNAL_CLOCK:
6358f0a9
DE
443 *data = g_variant_new_boolean(devc->cfg_clock_source
444 == CLOCK_EXT_CLK);
445 break;
446 case SR_CONF_CLOCK_EDGE:
447 idx = devc->cfg_clock_edge;
ce3ecb70 448 if (idx >= ARRAY_SIZE(signal_edge_names))
6358f0a9
DE
449 return SR_ERR_BUG;
450 *data = g_variant_new_string(signal_edge_names[idx]);
5874e88d 451 break;
e6e54bd2
DE
452 case SR_CONF_TRIGGER_SOURCE:
453 idx = devc->cfg_trigger_source;
ce3ecb70 454 if (idx >= ARRAY_SIZE(trigger_source_names))
e6e54bd2
DE
455 return SR_ERR_BUG;
456 *data = g_variant_new_string(trigger_source_names[idx]);
457 break;
458 case SR_CONF_TRIGGER_SLOPE:
459 idx = devc->cfg_trigger_slope;
ce3ecb70 460 if (idx >= ARRAY_SIZE(signal_edge_names))
e6e54bd2 461 return SR_ERR_BUG;
6358f0a9 462 *data = g_variant_new_string(signal_edge_names[idx]);
e6e54bd2 463 break;
aeaad0b0 464 default:
be64f90b
DE
465 /* Must not happen for a key listed in devopts. */
466 return SR_ERR_BUG;
aeaad0b0
DE
467 }
468
5874e88d 469 return SR_OK;
aeaad0b0
DE
470}
471
e6e54bd2
DE
472/* Helper for mapping a string-typed configuration value to an index
473 * within a table of possible values.
474 */
475static int lookup_index(GVariant *value, const char *const *table, int len)
476{
477 const char *entry;
478 int i;
479
480 entry = g_variant_get_string(value, NULL);
481 if (!entry)
482 return -1;
483
484 /* Linear search is fine for very small tables. */
0a1f7b09 485 for (i = 0; i < len; i++) {
e6e54bd2
DE
486 if (strcmp(entry, table[i]) == 0)
487 return i;
488 }
d64b5f43 489
e6e54bd2
DE
490 return -1;
491}
492
be64f90b
DE
493/* Write device configuration setting.
494 */
584560f1 495static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 496 const struct sr_channel_group *cg)
aeaad0b0 497{
29d58767 498 uint64_t value;
5874e88d 499 struct dev_context *devc;
e6e54bd2 500 int idx;
aeaad0b0 501
53b4680f 502 (void)cg;
aeaad0b0 503
be64f90b
DE
504 if (!sdi)
505 return SR_ERR_ARG;
506
5874e88d 507 devc = sdi->priv;
be64f90b
DE
508
509 if (!has_devopt(devc->model, key | SR_CONF_SET))
510 return SR_ERR_NA;
aeaad0b0 511
aeaad0b0 512 switch (key) {
5874e88d 513 case SR_CONF_SAMPLERATE:
29d58767 514 value = g_variant_get_uint64(data);
be64f90b
DE
515 if (value < devc->model->samplerates[devc->model->num_samplerates - 1]
516 || value > devc->model->samplerates[0])
5874e88d 517 return SR_ERR_SAMPLERATE;
29d58767
DE
518 devc->samplerate = value;
519 break;
520 case SR_CONF_LIMIT_MSEC:
521 value = g_variant_get_uint64(data);
522 if (value > MAX_LIMIT_MSEC)
523 return SR_ERR_ARG;
524 devc->limit_msec = value;
5874e88d
DE
525 break;
526 case SR_CONF_LIMIT_SAMPLES:
29d58767
DE
527 value = g_variant_get_uint64(data);
528 if (value > MAX_LIMIT_SAMPLES)
529 return SR_ERR_ARG;
530 devc->limit_samples = value;
5874e88d 531 break;
be64f90b
DE
532 case SR_CONF_RLE:
533 devc->cfg_rle = g_variant_get_boolean(data);
534 break;
5874e88d 535 case SR_CONF_EXTERNAL_CLOCK:
6358f0a9
DE
536 devc->cfg_clock_source = (g_variant_get_boolean(data))
537 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
538 break;
539 case SR_CONF_CLOCK_EDGE:
540 idx = lookup_index(data, signal_edge_names,
ce3ecb70 541 ARRAY_SIZE(signal_edge_names));
6358f0a9
DE
542 if (idx < 0)
543 return SR_ERR_ARG;
544 devc->cfg_clock_edge = idx;
5874e88d 545 break;
e6e54bd2
DE
546 case SR_CONF_TRIGGER_SOURCE:
547 idx = lookup_index(data, trigger_source_names,
ce3ecb70 548 ARRAY_SIZE(trigger_source_names));
e6e54bd2
DE
549 if (idx < 0)
550 return SR_ERR_ARG;
551 devc->cfg_trigger_source = idx;
552 break;
553 case SR_CONF_TRIGGER_SLOPE:
6358f0a9 554 idx = lookup_index(data, signal_edge_names,
ce3ecb70 555 ARRAY_SIZE(signal_edge_names));
e6e54bd2
DE
556 if (idx < 0)
557 return SR_ERR_ARG;
558 devc->cfg_trigger_slope = idx;
559 break;
aeaad0b0 560 default:
be64f90b
DE
561 /* Must not happen for a key listed in devopts. */
562 return SR_ERR_BUG;
aeaad0b0
DE
563 }
564
5874e88d 565 return SR_OK;
aeaad0b0
DE
566}
567
be64f90b
DE
568/* Apply channel configuration change.
569 */
f3ca73ed 570static int config_channel_set(const struct sr_dev_inst *sdi,
be64f90b 571 struct sr_channel *ch, unsigned int changes)
43db3436 572{
ba7dd8bb 573 uint64_t channel_bit;
43db3436
DE
574 struct dev_context *devc;
575
be64f90b
DE
576 if (!sdi)
577 return SR_ERR_ARG;
578
43db3436 579 devc = sdi->priv;
43db3436 580
be64f90b 581 if (ch->index < 0 || ch->index >= devc->model->num_channels) {
ba7dd8bb 582 sr_err("Channel index %d out of range.", ch->index);
43db3436
DE
583 return SR_ERR_BUG;
584 }
43db3436 585
3f239f08 586 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
be64f90b
DE
587 channel_bit = UINT64_C(1) << ch->index;
588
589 /* Enable or disable logic input for this channel. */
ba7dd8bb
UH
590 if (ch->enabled)
591 devc->channel_mask |= channel_bit;
43db3436 592 else
ba7dd8bb 593 devc->channel_mask &= ~channel_bit;
43db3436
DE
594 }
595
43db3436
DE
596 return SR_OK;
597}
598
be64f90b
DE
599/* Derive trigger masks from the session's trigger configuration.
600 */
225d3cb0
DE
601static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
602{
d64b5f43 603 uint64_t trigger_mask, trigger_values, trigger_edge_mask;
09ffac33 604 uint64_t level_bit, type_bit;
225d3cb0
DE
605 struct dev_context *devc;
606 struct sr_trigger *trigger;
607 struct sr_trigger_stage *stage;
608 struct sr_trigger_match *match;
609 const GSList *node;
be64f90b 610 int idx;
09ffac33 611 enum sr_trigger_matches trg;
225d3cb0
DE
612
613 devc = sdi->priv;
614
615 trigger = sr_session_trigger_get(sdi->session);
616 if (!trigger || !trigger->stages)
617 return SR_OK;
618
619 if (trigger->stages->next) {
620 sr_err("This device only supports 1 trigger stage.");
621 return SR_ERR_ARG;
622 }
623 stage = trigger->stages->data;
624
625 trigger_mask = 0;
626 trigger_values = 0;
627 trigger_edge_mask = 0;
628
629 for (node = stage->matches; node; node = node->next) {
630 match = node->data;
631
632 if (!match->channel->enabled)
d64b5f43 633 continue; /* Ignore disabled channel. */
225d3cb0 634
be64f90b 635 idx = match->channel->index;
09ffac33 636 trg = match->match;
be64f90b
DE
637
638 if (idx < 0 || idx >= devc->model->num_channels) {
639 sr_err("Channel index %d out of range.", idx);
d64b5f43 640 return SR_ERR_BUG; /* Should not happen. */
be64f90b 641 }
09ffac33
DE
642 if (trg != SR_TRIGGER_ZERO
643 && trg != SR_TRIGGER_ONE
644 && trg != SR_TRIGGER_RISING
645 && trg != SR_TRIGGER_FALLING) {
be64f90b 646 sr_err("Unsupported trigger match for CH%d.", idx + 1);
225d3cb0
DE
647 return SR_ERR_ARG;
648 }
09ffac33
DE
649 level_bit = (trg == SR_TRIGGER_ONE
650 || trg == SR_TRIGGER_RISING) ? 1 : 0;
651 type_bit = (trg == SR_TRIGGER_RISING
652 || trg == SR_TRIGGER_FALLING) ? 1 : 0;
653
654 trigger_mask |= UINT64_C(1) << idx;
655 trigger_values |= level_bit << idx;
656 trigger_edge_mask |= type_bit << idx;
225d3cb0
DE
657 }
658 devc->trigger_mask = trigger_mask;
659 devc->trigger_values = trigger_values;
660 devc->trigger_edge_mask = trigger_edge_mask;
661
662 return SR_OK;
663}
664
be64f90b
DE
665/* Apply current device configuration to the hardware.
666 */
ee38c8ba
DE
667static int config_commit(const struct sr_dev_inst *sdi)
668{
225d3cb0 669 struct dev_context *devc;
be64f90b
DE
670 int ret;
671
672 devc = sdi->priv;
225d3cb0
DE
673
674 if (sdi->status != SR_ST_ACTIVE)
675 return SR_ERR_DEV_CLOSED;
676
225d3cb0
DE
677 if (devc->acquisition) {
678 sr_err("Acquisition still in progress?");
ee38c8ba
DE
679 return SR_ERR;
680 }
681
be64f90b
DE
682 ret = prepare_trigger_masks(sdi);
683 if (ret != SR_OK)
684 return ret;
685
686 ret = (*devc->model->apply_fpga_config)(sdi);
687 if (ret != SR_OK) {
688 sr_err("Failed to apply FPGA configuration.");
689 return ret;
690 }
691
692 return SR_OK;
ee38c8ba
DE
693}
694
be64f90b
DE
695/* List available choices for a configuration setting.
696 */
697static int config_list(uint32_t key, GVariant **data,
698 const struct sr_dev_inst *sdi,
53b4680f 699 const struct sr_channel_group *cg)
aeaad0b0 700{
be64f90b 701 struct dev_context *devc;
5874e88d
DE
702 GVariant *gvar;
703 GVariantBuilder gvb;
aeaad0b0 704
53b4680f 705 (void)cg;
aeaad0b0 706
be64f90b 707 if (key == SR_CONF_SCAN_OPTIONS) {
584560f1 708 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
d64b5f43 709 scanopts, ARRAY_SIZE(scanopts), sizeof(scanopts[0]));
be64f90b
DE
710 return SR_OK;
711 }
712 if (!sdi) {
713 if (key != SR_CONF_DEVICE_OPTIONS)
714 return SR_ERR_ARG;
715
716 /* List driver capabilities. */
584560f1 717 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
d64b5f43 718 drvopts, ARRAY_SIZE(drvopts), sizeof(drvopts[0]));
be64f90b
DE
719 return SR_OK;
720 }
721
722 devc = sdi->priv;
723
724 /* List the model's device options. */
725 if (key == SR_CONF_DEVICE_OPTIONS) {
726 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
d64b5f43
UH
727 devc->model->devopts, devc->model->num_devopts,
728 sizeof(devc->model->devopts[0]));
be64f90b
DE
729 return SR_OK;
730 }
731
732 if (!has_devopt(devc->model, key | SR_CONF_LIST))
733 return SR_ERR_NA;
734
735 switch (key) {
5874e88d 736 case SR_CONF_SAMPLERATE:
be64f90b
DE
737 g_variant_builder_init(&gvb, G_VARIANT_TYPE_VARDICT);
738 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
d64b5f43
UH
739 devc->model->samplerates, devc->model->num_samplerates,
740 sizeof(devc->model->samplerates[0]));
5874e88d
DE
741 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
742 *data = g_variant_builder_end(&gvb);
743 break;
bbe7e48a
BV
744 case SR_CONF_TRIGGER_MATCH:
745 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
d64b5f43
UH
746 trigger_matches, ARRAY_SIZE(trigger_matches),
747 sizeof(trigger_matches[0]));
5874e88d 748 break;
e6e54bd2
DE
749 case SR_CONF_TRIGGER_SOURCE:
750 *data = g_variant_new_strv(trigger_source_names,
d64b5f43 751 ARRAY_SIZE(trigger_source_names));
e6e54bd2
DE
752 break;
753 case SR_CONF_TRIGGER_SLOPE:
6358f0a9
DE
754 case SR_CONF_CLOCK_EDGE:
755 *data = g_variant_new_strv(signal_edge_names,
d64b5f43 756 ARRAY_SIZE(signal_edge_names));
e6e54bd2 757 break;
aeaad0b0 758 default:
be64f90b
DE
759 /* Must not happen for a key listed in devopts. */
760 return SR_ERR_BUG;
aeaad0b0
DE
761 }
762
5874e88d 763 return SR_OK;
aeaad0b0
DE
764}
765
be64f90b
DE
766/* Set up the device hardware to begin capturing samples as soon as the
767 * configured trigger conditions are met, or immediately if no triggers
768 * are configured.
769 */
695dc859 770static int dev_acquisition_start(const struct sr_dev_inst *sdi)
5874e88d 771{
aeaad0b0
DE
772 if (sdi->status != SR_ST_ACTIVE)
773 return SR_ERR_DEV_CLOSED;
774
5874e88d
DE
775 sr_info("Starting acquisition.");
776
be64f90b 777 return lwla_start_acquisition(sdi);
aeaad0b0
DE
778}
779
be64f90b
DE
780/* Request that a running capture operation be stopped.
781 */
695dc859 782static int dev_acquisition_stop(struct sr_dev_inst *sdi)
aeaad0b0 783{
c81069b3
DE
784 struct dev_context *devc;
785
c81069b3 786 devc = sdi->priv;
aeaad0b0
DE
787
788 if (sdi->status != SR_ST_ACTIVE)
789 return SR_ERR_DEV_CLOSED;
790
be64f90b 791 if (devc->state != STATE_IDLE && !devc->cancel_requested) {
c81069b3
DE
792 devc->cancel_requested = TRUE;
793 sr_dbg("Stopping acquisition.");
794 }
d64b5f43 795
aeaad0b0
DE
796 return SR_OK;
797}
798
be64f90b
DE
799/* SysClk LWLA driver descriptor.
800 */
aeaad0b0
DE
801SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
802 .name = "sysclk-lwla",
5874e88d 803 .longname = "SysClk LWLA series",
aeaad0b0
DE
804 .api_version = 1,
805 .init = init,
700d6b64 806 .cleanup = std_cleanup,
aeaad0b0
DE
807 .scan = scan,
808 .dev_list = dev_list,
809 .dev_clear = dev_clear,
810 .config_get = config_get,
811 .config_set = config_set,
f3ca73ed 812 .config_channel_set = config_channel_set,
ee38c8ba 813 .config_commit = config_commit,
aeaad0b0
DE
814 .config_list = config_list,
815 .dev_open = dev_open,
816 .dev_close = dev_close,
817 .dev_acquisition_start = dev_acquisition_start,
818 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 819 .context = NULL,
aeaad0b0 820};