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