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