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