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