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