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