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