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