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