]> sigrok.org Git - libsigrok.git/blame - src/hardware/sysclk-lwla/api.c
Introduce standard implementation of the dev_list() callback
[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/* Destroy the private device context.
216 */
5874e88d
DE
217static void clear_dev_context(void *priv)
218{
219 struct dev_context *devc;
220
221 devc = priv;
222
be64f90b
DE
223 if (devc->acquisition) {
224 sr_err("Cannot clear device context during acquisition!");
d64b5f43 225 return; /* Leak and pray. */
be64f90b 226 }
5874e88d
DE
227 sr_dbg("Device context cleared.");
228
5874e88d 229 g_free(devc);
aeaad0b0
DE
230}
231
be64f90b
DE
232/* Destroy all device instances.
233 */
4f840ce9 234static int dev_clear(const struct sr_dev_driver *di)
aeaad0b0 235{
5874e88d 236 return std_dev_clear(di, &clear_dev_context);
aeaad0b0
DE
237}
238
93ed0241
DE
239/* Drain any pending data from the USB transfer buffers on the device.
240 * This may be necessary e.g. after a crash or generally to clean up after
241 * an abnormal condition.
242 */
243static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint)
244{
d64b5f43 245 int drained, xfer_len, ret;
93ed0241 246 unsigned char buf[512];
93ed0241
DE
247 const unsigned int drain_timeout_ms = 10;
248
249 drained = 0;
250 do {
251 xfer_len = 0;
252 ret = libusb_bulk_transfer(usb->devhdl, endpoint,
b3cfc6e9 253 buf, sizeof(buf), &xfer_len,
93ed0241
DE
254 drain_timeout_ms);
255 drained += xfer_len;
256 } while (ret == LIBUSB_SUCCESS && xfer_len != 0);
257
258 if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) {
259 sr_err("Failed to drain USB endpoint %u: %s.",
260 endpoint & (LIBUSB_ENDPOINT_IN - 1),
261 libusb_error_name(ret));
262 return SR_ERR;
263 }
264 if (drained > 0) {
265 sr_warn("Drained %d bytes from USB endpoint %u.",
266 drained, endpoint & (LIBUSB_ENDPOINT_IN - 1));
267 }
d64b5f43 268
93ed0241
DE
269 return SR_OK;
270}
271
be64f90b
DE
272/* Open and initialize device.
273 */
aeaad0b0
DE
274static int dev_open(struct sr_dev_inst *sdi)
275{
5874e88d 276 struct drv_context *drvc;
be64f90b 277 struct dev_context *devc;
5874e88d 278 struct sr_usb_dev_inst *usb;
d64b5f43 279 int i, ret;
aeaad0b0 280
ce19d4c6 281 drvc = sdi->driver->context;
be64f90b
DE
282 devc = sdi->priv;
283 usb = sdi->conn;
aeaad0b0 284
5874e88d
DE
285 if (!drvc) {
286 sr_err("Driver was not initialized.");
287 return SR_ERR;
288 }
c81069b3
DE
289 if (sdi->status != SR_ST_INACTIVE) {
290 sr_err("Device already open.");
291 return SR_ERR;
292 }
5874e88d 293
e35a4592
DE
294 /* Try the whole shebang three times, fingers crossed. */
295 for (i = 0; i < 3; i++) {
296 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
297 if (ret != SR_OK)
298 return ret;
299
300 ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
301 if (ret != LIBUSB_SUCCESS) {
302 sr_err("Failed to set USB configuration: %s.",
303 libusb_error_name(ret));
304 sr_usb_close(usb);
305 return SR_ERR;
306 }
ad6181e2 307
e35a4592
DE
308 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
309 if (ret != LIBUSB_SUCCESS) {
310 sr_err("Failed to claim interface: %s.",
311 libusb_error_name(ret));
312 sr_usb_close(usb);
313 return SR_ERR;
314 }
be64f90b 315
e35a4592
DE
316 ret = drain_usb(usb, EP_REPLY);
317 if (ret != SR_OK) {
318 sr_usb_close(usb);
319 return ret;
320 }
321 /* This delay appears to be necessary for reliable operation. */
322 g_usleep(30 * 1000);
93ed0241 323
e35a4592 324 sdi->status = SR_ST_ACTIVE;
5874e88d 325
e35a4592
DE
326 devc->active_fpga_config = FPGA_NOCONF;
327 devc->short_transfer_quirk = FALSE;
328 devc->state = STATE_IDLE;
be64f90b 329
e35a4592 330 ret = (*devc->model->apply_fpga_config)(sdi);
be64f90b 331
e35a4592
DE
332 if (ret == SR_OK)
333 ret = (*devc->model->device_init_check)(sdi);
334 if (ret == SR_OK)
335 break;
be64f90b 336
e35a4592 337 /* Rinse and repeat. */
c81069b3 338 sdi->status = SR_ST_INACTIVE;
be64f90b 339 sr_usb_close(usb);
c81069b3 340 }
e35a4592
DE
341
342 if (ret == SR_OK && devc->short_transfer_quirk)
78648577
DE
343 sr_warn("Short transfer quirk detected! "
344 "Memory reads will be slow.");
e35a4592 345 return ret;
aeaad0b0
DE
346}
347
be64f90b
DE
348/* Shutdown and close device.
349 */
aeaad0b0
DE
350static int dev_close(struct sr_dev_inst *sdi)
351{
be64f90b 352 struct drv_context *drvc;
c81069b3 353 struct dev_context *devc;
be64f90b 354 struct sr_usb_dev_inst *usb;
c81069b3 355 int ret;
5874e88d 356
be64f90b
DE
357 drvc = sdi->driver->context;
358 devc = sdi->priv;
359 usb = sdi->conn;
360
361 if (!drvc) {
5874e88d
DE
362 sr_err("Driver was not initialized.");
363 return SR_ERR;
364 }
be64f90b
DE
365 if (sdi->status == SR_ST_INACTIVE) {
366 sr_dbg("Device already closed.");
5874e88d 367 return SR_OK;
c81069b3 368 }
be64f90b
DE
369 if (devc->acquisition) {
370 sr_err("Cannot close device during acquisition!");
371 /* Request stop, leak handle, and prepare for the worst. */
372 devc->cancel_requested = TRUE;
373 return SR_ERR_BUG;
374 }
375
6358f0a9 376 sdi->status = SR_ST_INACTIVE;
5874e88d 377
be64f90b
DE
378 /* Download of the shutdown bitstream, if any. */
379 ret = (*devc->model->apply_fpga_config)(sdi);
c81069b3 380 if (ret != SR_OK)
be64f90b 381 sr_warn("Unable to shut down device.");
5874e88d
DE
382
383 libusb_release_interface(usb->devhdl, USB_INTERFACE);
c81069b3 384 sr_usb_close(usb);
5874e88d 385
c81069b3 386 return ret;
aeaad0b0
DE
387}
388
be64f90b
DE
389/* Check whether the device options contain a specific key.
390 * Also match against get/set/list bits if specified.
391 */
392static int has_devopt(const struct model_info *model, uint32_t key)
aeaad0b0 393{
be64f90b
DE
394 unsigned int i;
395
396 for (i = 0; i < model->num_devopts; i++) {
397 if ((model->devopts[i] & (SR_CONF_MASK | key)) == key)
398 return TRUE;
399 }
d64b5f43 400
be64f90b 401 return FALSE;
aeaad0b0
DE
402}
403
be64f90b
DE
404/* Read device configuration setting.
405 */
584560f1 406static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 407 const struct sr_channel_group *cg)
aeaad0b0 408{
5874e88d 409 struct dev_context *devc;
7ed80817 410 unsigned int idx;
aeaad0b0 411
53b4680f 412 (void)cg;
aeaad0b0 413
5874e88d
DE
414 if (!sdi)
415 return SR_ERR_ARG;
416
417 devc = sdi->priv;
418
be64f90b
DE
419 if (!has_devopt(devc->model, key | SR_CONF_GET))
420 return SR_ERR_NA;
421
aeaad0b0 422 switch (key) {
5874e88d
DE
423 case SR_CONF_SAMPLERATE:
424 *data = g_variant_new_uint64(devc->samplerate);
425 break;
29d58767
DE
426 case SR_CONF_LIMIT_MSEC:
427 *data = g_variant_new_uint64(devc->limit_msec);
428 break;
5874e88d
DE
429 case SR_CONF_LIMIT_SAMPLES:
430 *data = g_variant_new_uint64(devc->limit_samples);
431 break;
be64f90b
DE
432 case SR_CONF_RLE:
433 *data = g_variant_new_boolean(devc->cfg_rle);
434 break;
5874e88d 435 case SR_CONF_EXTERNAL_CLOCK:
6358f0a9
DE
436 *data = g_variant_new_boolean(devc->cfg_clock_source
437 == CLOCK_EXT_CLK);
438 break;
439 case SR_CONF_CLOCK_EDGE:
440 idx = devc->cfg_clock_edge;
ce3ecb70 441 if (idx >= ARRAY_SIZE(signal_edge_names))
6358f0a9
DE
442 return SR_ERR_BUG;
443 *data = g_variant_new_string(signal_edge_names[idx]);
5874e88d 444 break;
e6e54bd2
DE
445 case SR_CONF_TRIGGER_SOURCE:
446 idx = devc->cfg_trigger_source;
ce3ecb70 447 if (idx >= ARRAY_SIZE(trigger_source_names))
e6e54bd2
DE
448 return SR_ERR_BUG;
449 *data = g_variant_new_string(trigger_source_names[idx]);
450 break;
451 case SR_CONF_TRIGGER_SLOPE:
452 idx = devc->cfg_trigger_slope;
ce3ecb70 453 if (idx >= ARRAY_SIZE(signal_edge_names))
e6e54bd2 454 return SR_ERR_BUG;
6358f0a9 455 *data = g_variant_new_string(signal_edge_names[idx]);
e6e54bd2 456 break;
aeaad0b0 457 default:
be64f90b
DE
458 /* Must not happen for a key listed in devopts. */
459 return SR_ERR_BUG;
aeaad0b0
DE
460 }
461
5874e88d 462 return SR_OK;
aeaad0b0
DE
463}
464
e6e54bd2
DE
465/* Helper for mapping a string-typed configuration value to an index
466 * within a table of possible values.
467 */
468static int lookup_index(GVariant *value, const char *const *table, int len)
469{
470 const char *entry;
471 int i;
472
473 entry = g_variant_get_string(value, NULL);
474 if (!entry)
475 return -1;
476
477 /* Linear search is fine for very small tables. */
0a1f7b09 478 for (i = 0; i < len; i++) {
e6e54bd2
DE
479 if (strcmp(entry, table[i]) == 0)
480 return i;
481 }
d64b5f43 482
e6e54bd2
DE
483 return -1;
484}
485
be64f90b
DE
486/* Write device configuration setting.
487 */
584560f1 488static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 489 const struct sr_channel_group *cg)
aeaad0b0 490{
29d58767 491 uint64_t value;
5874e88d 492 struct dev_context *devc;
e6e54bd2 493 int idx;
aeaad0b0 494
53b4680f 495 (void)cg;
aeaad0b0 496
be64f90b
DE
497 if (!sdi)
498 return SR_ERR_ARG;
499
5874e88d 500 devc = sdi->priv;
be64f90b
DE
501
502 if (!has_devopt(devc->model, key | SR_CONF_SET))
503 return SR_ERR_NA;
aeaad0b0 504
aeaad0b0 505 switch (key) {
5874e88d 506 case SR_CONF_SAMPLERATE:
29d58767 507 value = g_variant_get_uint64(data);
be64f90b
DE
508 if (value < devc->model->samplerates[devc->model->num_samplerates - 1]
509 || value > devc->model->samplerates[0])
5874e88d 510 return SR_ERR_SAMPLERATE;
29d58767
DE
511 devc->samplerate = value;
512 break;
513 case SR_CONF_LIMIT_MSEC:
514 value = g_variant_get_uint64(data);
515 if (value > MAX_LIMIT_MSEC)
516 return SR_ERR_ARG;
517 devc->limit_msec = value;
5874e88d
DE
518 break;
519 case SR_CONF_LIMIT_SAMPLES:
29d58767
DE
520 value = g_variant_get_uint64(data);
521 if (value > MAX_LIMIT_SAMPLES)
522 return SR_ERR_ARG;
523 devc->limit_samples = value;
5874e88d 524 break;
be64f90b
DE
525 case SR_CONF_RLE:
526 devc->cfg_rle = g_variant_get_boolean(data);
527 break;
5874e88d 528 case SR_CONF_EXTERNAL_CLOCK:
6358f0a9
DE
529 devc->cfg_clock_source = (g_variant_get_boolean(data))
530 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
531 break;
532 case SR_CONF_CLOCK_EDGE:
533 idx = lookup_index(data, signal_edge_names,
ce3ecb70 534 ARRAY_SIZE(signal_edge_names));
6358f0a9
DE
535 if (idx < 0)
536 return SR_ERR_ARG;
537 devc->cfg_clock_edge = idx;
5874e88d 538 break;
e6e54bd2
DE
539 case SR_CONF_TRIGGER_SOURCE:
540 idx = lookup_index(data, trigger_source_names,
ce3ecb70 541 ARRAY_SIZE(trigger_source_names));
e6e54bd2
DE
542 if (idx < 0)
543 return SR_ERR_ARG;
544 devc->cfg_trigger_source = idx;
545 break;
546 case SR_CONF_TRIGGER_SLOPE:
6358f0a9 547 idx = lookup_index(data, signal_edge_names,
ce3ecb70 548 ARRAY_SIZE(signal_edge_names));
e6e54bd2
DE
549 if (idx < 0)
550 return SR_ERR_ARG;
551 devc->cfg_trigger_slope = idx;
552 break;
aeaad0b0 553 default:
be64f90b
DE
554 /* Must not happen for a key listed in devopts. */
555 return SR_ERR_BUG;
aeaad0b0
DE
556 }
557
5874e88d 558 return SR_OK;
aeaad0b0
DE
559}
560
be64f90b
DE
561/* Apply channel configuration change.
562 */
f3ca73ed 563static int config_channel_set(const struct sr_dev_inst *sdi,
be64f90b 564 struct sr_channel *ch, unsigned int changes)
43db3436 565{
ba7dd8bb 566 uint64_t channel_bit;
43db3436
DE
567 struct dev_context *devc;
568
be64f90b
DE
569 if (!sdi)
570 return SR_ERR_ARG;
571
43db3436 572 devc = sdi->priv;
43db3436 573
be64f90b 574 if (ch->index < 0 || ch->index >= devc->model->num_channels) {
ba7dd8bb 575 sr_err("Channel index %d out of range.", ch->index);
43db3436
DE
576 return SR_ERR_BUG;
577 }
43db3436 578
3f239f08 579 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
be64f90b
DE
580 channel_bit = UINT64_C(1) << ch->index;
581
582 /* Enable or disable logic input for this channel. */
ba7dd8bb
UH
583 if (ch->enabled)
584 devc->channel_mask |= channel_bit;
43db3436 585 else
ba7dd8bb 586 devc->channel_mask &= ~channel_bit;
43db3436
DE
587 }
588
43db3436
DE
589 return SR_OK;
590}
591
be64f90b
DE
592/* Derive trigger masks from the session's trigger configuration.
593 */
225d3cb0
DE
594static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
595{
d64b5f43 596 uint64_t trigger_mask, trigger_values, trigger_edge_mask;
09ffac33 597 uint64_t level_bit, type_bit;
225d3cb0
DE
598 struct dev_context *devc;
599 struct sr_trigger *trigger;
600 struct sr_trigger_stage *stage;
601 struct sr_trigger_match *match;
602 const GSList *node;
be64f90b 603 int idx;
09ffac33 604 enum sr_trigger_matches trg;
225d3cb0
DE
605
606 devc = sdi->priv;
607
608 trigger = sr_session_trigger_get(sdi->session);
609 if (!trigger || !trigger->stages)
610 return SR_OK;
611
612 if (trigger->stages->next) {
613 sr_err("This device only supports 1 trigger stage.");
614 return SR_ERR_ARG;
615 }
616 stage = trigger->stages->data;
617
618 trigger_mask = 0;
619 trigger_values = 0;
620 trigger_edge_mask = 0;
621
622 for (node = stage->matches; node; node = node->next) {
623 match = node->data;
624
625 if (!match->channel->enabled)
d64b5f43 626 continue; /* Ignore disabled channel. */
225d3cb0 627
be64f90b 628 idx = match->channel->index;
09ffac33 629 trg = match->match;
be64f90b
DE
630
631 if (idx < 0 || idx >= devc->model->num_channels) {
632 sr_err("Channel index %d out of range.", idx);
d64b5f43 633 return SR_ERR_BUG; /* Should not happen. */
be64f90b 634 }
09ffac33
DE
635 if (trg != SR_TRIGGER_ZERO
636 && trg != SR_TRIGGER_ONE
637 && trg != SR_TRIGGER_RISING
638 && trg != SR_TRIGGER_FALLING) {
be64f90b 639 sr_err("Unsupported trigger match for CH%d.", idx + 1);
225d3cb0
DE
640 return SR_ERR_ARG;
641 }
09ffac33
DE
642 level_bit = (trg == SR_TRIGGER_ONE
643 || trg == SR_TRIGGER_RISING) ? 1 : 0;
644 type_bit = (trg == SR_TRIGGER_RISING
645 || trg == SR_TRIGGER_FALLING) ? 1 : 0;
646
647 trigger_mask |= UINT64_C(1) << idx;
648 trigger_values |= level_bit << idx;
649 trigger_edge_mask |= type_bit << idx;
225d3cb0
DE
650 }
651 devc->trigger_mask = trigger_mask;
652 devc->trigger_values = trigger_values;
653 devc->trigger_edge_mask = trigger_edge_mask;
654
655 return SR_OK;
656}
657
be64f90b
DE
658/* Apply current device configuration to the hardware.
659 */
ee38c8ba
DE
660static int config_commit(const struct sr_dev_inst *sdi)
661{
225d3cb0 662 struct dev_context *devc;
be64f90b
DE
663 int ret;
664
665 devc = sdi->priv;
225d3cb0
DE
666
667 if (sdi->status != SR_ST_ACTIVE)
668 return SR_ERR_DEV_CLOSED;
669
225d3cb0
DE
670 if (devc->acquisition) {
671 sr_err("Acquisition still in progress?");
ee38c8ba
DE
672 return SR_ERR;
673 }
674
be64f90b
DE
675 ret = prepare_trigger_masks(sdi);
676 if (ret != SR_OK)
677 return ret;
678
679 ret = (*devc->model->apply_fpga_config)(sdi);
680 if (ret != SR_OK) {
681 sr_err("Failed to apply FPGA configuration.");
682 return ret;
683 }
684
685 return SR_OK;
ee38c8ba
DE
686}
687
be64f90b
DE
688/* List available choices for a configuration setting.
689 */
690static int config_list(uint32_t key, GVariant **data,
691 const struct sr_dev_inst *sdi,
53b4680f 692 const struct sr_channel_group *cg)
aeaad0b0 693{
be64f90b 694 struct dev_context *devc;
5874e88d
DE
695 GVariant *gvar;
696 GVariantBuilder gvb;
aeaad0b0 697
53b4680f 698 (void)cg;
aeaad0b0 699
be64f90b 700 if (key == SR_CONF_SCAN_OPTIONS) {
584560f1 701 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
d64b5f43 702 scanopts, ARRAY_SIZE(scanopts), sizeof(scanopts[0]));
be64f90b
DE
703 return SR_OK;
704 }
705 if (!sdi) {
706 if (key != SR_CONF_DEVICE_OPTIONS)
707 return SR_ERR_ARG;
708
709 /* List driver capabilities. */
584560f1 710 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
d64b5f43 711 drvopts, ARRAY_SIZE(drvopts), sizeof(drvopts[0]));
be64f90b
DE
712 return SR_OK;
713 }
714
715 devc = sdi->priv;
716
717 /* List the model's device options. */
718 if (key == SR_CONF_DEVICE_OPTIONS) {
719 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
d64b5f43
UH
720 devc->model->devopts, devc->model->num_devopts,
721 sizeof(devc->model->devopts[0]));
be64f90b
DE
722 return SR_OK;
723 }
724
725 if (!has_devopt(devc->model, key | SR_CONF_LIST))
726 return SR_ERR_NA;
727
728 switch (key) {
5874e88d 729 case SR_CONF_SAMPLERATE:
be64f90b
DE
730 g_variant_builder_init(&gvb, G_VARIANT_TYPE_VARDICT);
731 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
d64b5f43
UH
732 devc->model->samplerates, devc->model->num_samplerates,
733 sizeof(devc->model->samplerates[0]));
5874e88d
DE
734 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
735 *data = g_variant_builder_end(&gvb);
736 break;
bbe7e48a
BV
737 case SR_CONF_TRIGGER_MATCH:
738 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
d64b5f43
UH
739 trigger_matches, ARRAY_SIZE(trigger_matches),
740 sizeof(trigger_matches[0]));
5874e88d 741 break;
e6e54bd2
DE
742 case SR_CONF_TRIGGER_SOURCE:
743 *data = g_variant_new_strv(trigger_source_names,
d64b5f43 744 ARRAY_SIZE(trigger_source_names));
e6e54bd2
DE
745 break;
746 case SR_CONF_TRIGGER_SLOPE:
6358f0a9
DE
747 case SR_CONF_CLOCK_EDGE:
748 *data = g_variant_new_strv(signal_edge_names,
d64b5f43 749 ARRAY_SIZE(signal_edge_names));
e6e54bd2 750 break;
aeaad0b0 751 default:
be64f90b
DE
752 /* Must not happen for a key listed in devopts. */
753 return SR_ERR_BUG;
aeaad0b0
DE
754 }
755
5874e88d 756 return SR_OK;
aeaad0b0
DE
757}
758
be64f90b
DE
759/* Set up the device hardware to begin capturing samples as soon as the
760 * configured trigger conditions are met, or immediately if no triggers
761 * are configured.
762 */
695dc859 763static int dev_acquisition_start(const struct sr_dev_inst *sdi)
5874e88d 764{
aeaad0b0
DE
765 if (sdi->status != SR_ST_ACTIVE)
766 return SR_ERR_DEV_CLOSED;
767
5874e88d
DE
768 sr_info("Starting acquisition.");
769
be64f90b 770 return lwla_start_acquisition(sdi);
aeaad0b0
DE
771}
772
be64f90b
DE
773/* Request that a running capture operation be stopped.
774 */
695dc859 775static int dev_acquisition_stop(struct sr_dev_inst *sdi)
aeaad0b0 776{
c81069b3
DE
777 struct dev_context *devc;
778
c81069b3 779 devc = sdi->priv;
aeaad0b0
DE
780
781 if (sdi->status != SR_ST_ACTIVE)
782 return SR_ERR_DEV_CLOSED;
783
be64f90b 784 if (devc->state != STATE_IDLE && !devc->cancel_requested) {
c81069b3
DE
785 devc->cancel_requested = TRUE;
786 sr_dbg("Stopping acquisition.");
787 }
d64b5f43 788
aeaad0b0
DE
789 return SR_OK;
790}
791
be64f90b
DE
792/* SysClk LWLA driver descriptor.
793 */
aeaad0b0
DE
794SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
795 .name = "sysclk-lwla",
5874e88d 796 .longname = "SysClk LWLA series",
aeaad0b0
DE
797 .api_version = 1,
798 .init = init,
700d6b64 799 .cleanup = std_cleanup,
aeaad0b0 800 .scan = scan,
c01bf34c 801 .dev_list = std_dev_list,
aeaad0b0
DE
802 .dev_clear = dev_clear,
803 .config_get = config_get,
804 .config_set = config_set,
f3ca73ed 805 .config_channel_set = config_channel_set,
ee38c8ba 806 .config_commit = config_commit,
aeaad0b0
DE
807 .config_list = config_list,
808 .dev_open = dev_open,
809 .dev_close = dev_close,
810 .dev_acquisition_start = dev_acquisition_start,
811 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 812 .context = NULL,
aeaad0b0 813};