]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | #include <config.h> | |
21 | #include <glib.h> | |
22 | #include <libusb.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <libsigrok/libsigrok.h> | |
26 | #include <libsigrok-internal.h> | |
27 | #include "protocol.h" | |
28 | ||
29 | /* Supported device scan options. | |
30 | */ | |
31 | static const uint32_t scanopts[] = { | |
32 | SR_CONF_CONN, | |
33 | }; | |
34 | ||
35 | /* Driver capabilities. | |
36 | */ | |
37 | static const uint32_t drvopts[] = { | |
38 | SR_CONF_LOGIC_ANALYZER, | |
39 | }; | |
40 | ||
41 | /* Supported trigger match conditions. | |
42 | */ | |
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 | ||
50 | /* Names assigned to available trigger sources. | |
51 | */ | |
52 | static const char *const trigger_source_names[] = { | |
53 | [TRIGGER_CHANNELS] = "CH", | |
54 | [TRIGGER_EXT_TRG] = "TRG", | |
55 | }; | |
56 | ||
57 | /* Names assigned to available edge slope choices. | |
58 | */ | |
59 | static const char *const signal_edge_names[] = { | |
60 | [EDGE_POSITIVE] = "r", | |
61 | [EDGE_NEGATIVE] = "f", | |
62 | }; | |
63 | ||
64 | /* Initialize the SysClk LWLA driver. | |
65 | */ | |
66 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
67 | { | |
68 | return std_init(sr_ctx, di, LOG_PREFIX); | |
69 | } | |
70 | ||
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) | |
74 | { | |
75 | struct sr_dev_inst *sdi; | |
76 | struct dev_context *devc; | |
77 | int i; | |
78 | char name[8]; | |
79 | ||
80 | /* Initialize private device context. */ | |
81 | devc = g_malloc0(sizeof(struct dev_context)); | |
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; | |
87 | ||
88 | /* Create sigrok device instance. */ | |
89 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
90 | sdi->status = SR_ST_INACTIVE; | |
91 | sdi->vendor = g_strdup(VENDOR_NAME); | |
92 | sdi->model = g_strdup(model->name); | |
93 | sdi->priv = devc; | |
94 | ||
95 | /* Generate list of logic channels. */ | |
96 | for (i = 0; i < model->num_channels; i++) { | |
97 | /* The LWLA series simply number channels from CH1 to CHxx. */ | |
98 | g_snprintf(name, sizeof(name), "CH%d", i + 1); | |
99 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name); | |
100 | } | |
101 | ||
102 | return sdi; | |
103 | } | |
104 | ||
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, ret; | |
117 | unsigned int vid, pid; | |
118 | ||
119 | bus = libusb_get_bus_number(dev); | |
120 | address = libusb_get_device_address(dev); | |
121 | ||
122 | for (node = conn_matches; node != NULL; node = node->next) { | |
123 | usb = node->data; | |
124 | if (usb && usb->bus == bus && usb->address == address) | |
125 | break; /* found */ | |
126 | } | |
127 | if (conn_matches && !node) | |
128 | return NULL; /* no match */ | |
129 | ||
130 | ret = libusb_get_device_descriptor(dev, &des); | |
131 | if (ret != 0) { | |
132 | sr_err("Failed to get USB device descriptor: %s.", | |
133 | libusb_error_name(ret)); | |
134 | return NULL; | |
135 | } | |
136 | vid = des.idVendor; | |
137 | pid = des.idProduct; | |
138 | ||
139 | /* Create sigrok device instance. */ | |
140 | if (vid == USB_VID_SYSCLK && pid == USB_PID_LWLA1016) { | |
141 | model = &lwla1016_info; | |
142 | } else if (vid == USB_VID_SYSCLK && pid == USB_PID_LWLA1034) { | |
143 | model = &lwla1034_info; | |
144 | } else { | |
145 | if (conn_matches) | |
146 | sr_warn("USB device %d.%d (%04x:%04x) is not a" | |
147 | " SysClk LWLA.", bus, address, vid, pid); | |
148 | return NULL; | |
149 | } | |
150 | sdi = dev_inst_new(model); | |
151 | ||
152 | sdi->inst_type = SR_INST_USB; | |
153 | sdi->conn = sr_usb_dev_inst_new(bus, address, NULL); | |
154 | ||
155 | return sdi; | |
156 | } | |
157 | ||
158 | /* Scan for SysClk LWLA devices and create a device instance for each one. | |
159 | */ | |
160 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
161 | { | |
162 | GSList *conn_devices, *devices, *node; | |
163 | struct drv_context *drvc; | |
164 | struct sr_dev_inst *sdi; | |
165 | struct sr_config *src; | |
166 | const char *conn; | |
167 | libusb_device **devlist; | |
168 | ssize_t num_devs, i; | |
169 | ||
170 | drvc = di->context; | |
171 | conn = NULL; | |
172 | conn_devices = NULL; | |
173 | devices = NULL; | |
174 | ||
175 | for (node = options; node != NULL; node = node->next) { | |
176 | src = node->data; | |
177 | if (src->key == SR_CONF_CONN) { | |
178 | conn = g_variant_get_string(src->data, NULL); | |
179 | break; | |
180 | } | |
181 | } | |
182 | if (conn) { | |
183 | /* Find devices matching the connection specification. */ | |
184 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
185 | } | |
186 | ||
187 | /* List all libusb devices. */ | |
188 | num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
189 | if (num_devs < 0) { | |
190 | sr_err("Failed to list USB devices: %s.", | |
191 | libusb_error_name(num_devs)); | |
192 | g_slist_free_full(conn_devices, | |
193 | (GDestroyNotify)&sr_usb_dev_inst_free); | |
194 | return NULL; | |
195 | } | |
196 | ||
197 | /* Scan the USB device list for matching LWLA devices. */ | |
198 | for (i = 0; i < num_devs; i++) { | |
199 | sdi = dev_inst_new_matching(conn_devices, devlist[i]); | |
200 | if (!sdi) | |
201 | continue; /* no match */ | |
202 | ||
203 | /* Register device instance with driver. */ | |
204 | sdi->driver = di; | |
205 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
206 | devices = g_slist_append(devices, sdi); | |
207 | } | |
208 | ||
209 | libusb_free_device_list(devlist, 1); | |
210 | g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free); | |
211 | ||
212 | return devices; | |
213 | } | |
214 | ||
215 | /* Return the list of devices found during scan. | |
216 | */ | |
217 | static GSList *dev_list(const struct sr_dev_driver *di) | |
218 | { | |
219 | return ((struct drv_context *)(di->context))->instances; | |
220 | } | |
221 | ||
222 | /* Destroy the private device context. | |
223 | */ | |
224 | static void clear_dev_context(void *priv) | |
225 | { | |
226 | struct dev_context *devc; | |
227 | ||
228 | devc = priv; | |
229 | ||
230 | if (devc->acquisition) { | |
231 | sr_err("Cannot clear device context during acquisition!"); | |
232 | return; /* Leak and pray. */ | |
233 | } | |
234 | sr_dbg("Device context cleared."); | |
235 | ||
236 | g_free(devc); | |
237 | } | |
238 | ||
239 | /* Destroy all device instances. | |
240 | */ | |
241 | static int dev_clear(const struct sr_dev_driver *di) | |
242 | { | |
243 | return std_dev_clear(di, &clear_dev_context); | |
244 | } | |
245 | ||
246 | /* Drain any pending data from the USB transfer buffers on the device. | |
247 | * This may be necessary e.g. after a crash or generally to clean up after | |
248 | * an abnormal condition. | |
249 | */ | |
250 | static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint) | |
251 | { | |
252 | int drained, xfer_len, ret; | |
253 | unsigned char buf[512]; | |
254 | const unsigned int drain_timeout_ms = 10; | |
255 | ||
256 | drained = 0; | |
257 | do { | |
258 | xfer_len = 0; | |
259 | ret = libusb_bulk_transfer(usb->devhdl, endpoint, | |
260 | buf, sizeof(buf), &xfer_len, | |
261 | drain_timeout_ms); | |
262 | drained += xfer_len; | |
263 | } while (ret == LIBUSB_SUCCESS && xfer_len != 0); | |
264 | ||
265 | if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) { | |
266 | sr_err("Failed to drain USB endpoint %u: %s.", | |
267 | endpoint & (LIBUSB_ENDPOINT_IN - 1), | |
268 | libusb_error_name(ret)); | |
269 | return SR_ERR; | |
270 | } | |
271 | if (drained > 0) { | |
272 | sr_warn("Drained %d bytes from USB endpoint %u.", | |
273 | drained, endpoint & (LIBUSB_ENDPOINT_IN - 1)); | |
274 | } | |
275 | ||
276 | return SR_OK; | |
277 | } | |
278 | ||
279 | /* Open and initialize device. | |
280 | */ | |
281 | static int dev_open(struct sr_dev_inst *sdi) | |
282 | { | |
283 | struct drv_context *drvc; | |
284 | struct dev_context *devc; | |
285 | struct sr_usb_dev_inst *usb; | |
286 | int i, ret; | |
287 | ||
288 | drvc = sdi->driver->context; | |
289 | devc = sdi->priv; | |
290 | usb = sdi->conn; | |
291 | ||
292 | if (!drvc) { | |
293 | sr_err("Driver was not initialized."); | |
294 | return SR_ERR; | |
295 | } | |
296 | if (sdi->status != SR_ST_INACTIVE) { | |
297 | sr_err("Device already open."); | |
298 | return SR_ERR; | |
299 | } | |
300 | ||
301 | /* Try the whole shebang three times, fingers crossed. */ | |
302 | for (i = 0; i < 3; i++) { | |
303 | ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb); | |
304 | if (ret != SR_OK) | |
305 | return ret; | |
306 | ||
307 | ret = libusb_set_configuration(usb->devhdl, USB_CONFIG); | |
308 | if (ret != LIBUSB_SUCCESS) { | |
309 | sr_err("Failed to set USB configuration: %s.", | |
310 | libusb_error_name(ret)); | |
311 | sr_usb_close(usb); | |
312 | return SR_ERR; | |
313 | } | |
314 | ||
315 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); | |
316 | if (ret != LIBUSB_SUCCESS) { | |
317 | sr_err("Failed to claim interface: %s.", | |
318 | libusb_error_name(ret)); | |
319 | sr_usb_close(usb); | |
320 | return SR_ERR; | |
321 | } | |
322 | ||
323 | ret = drain_usb(usb, EP_REPLY); | |
324 | if (ret != SR_OK) { | |
325 | sr_usb_close(usb); | |
326 | return ret; | |
327 | } | |
328 | /* This delay appears to be necessary for reliable operation. */ | |
329 | g_usleep(30 * 1000); | |
330 | ||
331 | sdi->status = SR_ST_ACTIVE; | |
332 | ||
333 | devc->active_fpga_config = FPGA_NOCONF; | |
334 | devc->short_transfer_quirk = FALSE; | |
335 | devc->state = STATE_IDLE; | |
336 | ||
337 | ret = (*devc->model->apply_fpga_config)(sdi); | |
338 | ||
339 | if (ret == SR_OK) | |
340 | ret = (*devc->model->device_init_check)(sdi); | |
341 | if (ret == SR_OK) | |
342 | break; | |
343 | ||
344 | /* Rinse and repeat. */ | |
345 | sdi->status = SR_ST_INACTIVE; | |
346 | sr_usb_close(usb); | |
347 | } | |
348 | ||
349 | if (ret == SR_OK && devc->short_transfer_quirk) | |
350 | sr_warn("Short transfer quirk detected! " | |
351 | "Memory reads will be slow."); | |
352 | return ret; | |
353 | } | |
354 | ||
355 | /* Shutdown and close device. | |
356 | */ | |
357 | static int dev_close(struct sr_dev_inst *sdi) | |
358 | { | |
359 | struct drv_context *drvc; | |
360 | struct dev_context *devc; | |
361 | struct sr_usb_dev_inst *usb; | |
362 | int ret; | |
363 | ||
364 | drvc = sdi->driver->context; | |
365 | devc = sdi->priv; | |
366 | usb = sdi->conn; | |
367 | ||
368 | if (!drvc) { | |
369 | sr_err("Driver was not initialized."); | |
370 | return SR_ERR; | |
371 | } | |
372 | if (sdi->status == SR_ST_INACTIVE) { | |
373 | sr_dbg("Device already closed."); | |
374 | return SR_OK; | |
375 | } | |
376 | if (devc->acquisition) { | |
377 | sr_err("Cannot close device during acquisition!"); | |
378 | /* Request stop, leak handle, and prepare for the worst. */ | |
379 | devc->cancel_requested = TRUE; | |
380 | return SR_ERR_BUG; | |
381 | } | |
382 | ||
383 | sdi->status = SR_ST_INACTIVE; | |
384 | ||
385 | /* Download of the shutdown bitstream, if any. */ | |
386 | ret = (*devc->model->apply_fpga_config)(sdi); | |
387 | if (ret != SR_OK) | |
388 | sr_warn("Unable to shut down device."); | |
389 | ||
390 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
391 | sr_usb_close(usb); | |
392 | ||
393 | return ret; | |
394 | } | |
395 | ||
396 | /* Check whether the device options contain a specific key. | |
397 | * Also match against get/set/list bits if specified. | |
398 | */ | |
399 | static int has_devopt(const struct model_info *model, uint32_t key) | |
400 | { | |
401 | unsigned int i; | |
402 | ||
403 | for (i = 0; i < model->num_devopts; i++) { | |
404 | if ((model->devopts[i] & (SR_CONF_MASK | key)) == key) | |
405 | return TRUE; | |
406 | } | |
407 | ||
408 | return FALSE; | |
409 | } | |
410 | ||
411 | /* Read device configuration setting. | |
412 | */ | |
413 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
414 | const struct sr_channel_group *cg) | |
415 | { | |
416 | struct dev_context *devc; | |
417 | unsigned int idx; | |
418 | ||
419 | (void)cg; | |
420 | ||
421 | if (!sdi) | |
422 | return SR_ERR_ARG; | |
423 | ||
424 | devc = sdi->priv; | |
425 | ||
426 | if (!has_devopt(devc->model, key | SR_CONF_GET)) | |
427 | return SR_ERR_NA; | |
428 | ||
429 | switch (key) { | |
430 | case SR_CONF_SAMPLERATE: | |
431 | *data = g_variant_new_uint64(devc->samplerate); | |
432 | break; | |
433 | case SR_CONF_LIMIT_MSEC: | |
434 | *data = g_variant_new_uint64(devc->limit_msec); | |
435 | break; | |
436 | case SR_CONF_LIMIT_SAMPLES: | |
437 | *data = g_variant_new_uint64(devc->limit_samples); | |
438 | break; | |
439 | case SR_CONF_RLE: | |
440 | *data = g_variant_new_boolean(devc->cfg_rle); | |
441 | break; | |
442 | case SR_CONF_EXTERNAL_CLOCK: | |
443 | *data = g_variant_new_boolean(devc->cfg_clock_source | |
444 | == CLOCK_EXT_CLK); | |
445 | break; | |
446 | case SR_CONF_CLOCK_EDGE: | |
447 | idx = devc->cfg_clock_edge; | |
448 | if (idx >= ARRAY_SIZE(signal_edge_names)) | |
449 | return SR_ERR_BUG; | |
450 | *data = g_variant_new_string(signal_edge_names[idx]); | |
451 | break; | |
452 | case SR_CONF_TRIGGER_SOURCE: | |
453 | idx = devc->cfg_trigger_source; | |
454 | if (idx >= ARRAY_SIZE(trigger_source_names)) | |
455 | return SR_ERR_BUG; | |
456 | *data = g_variant_new_string(trigger_source_names[idx]); | |
457 | break; | |
458 | case SR_CONF_TRIGGER_SLOPE: | |
459 | idx = devc->cfg_trigger_slope; | |
460 | if (idx >= ARRAY_SIZE(signal_edge_names)) | |
461 | return SR_ERR_BUG; | |
462 | *data = g_variant_new_string(signal_edge_names[idx]); | |
463 | break; | |
464 | default: | |
465 | /* Must not happen for a key listed in devopts. */ | |
466 | return SR_ERR_BUG; | |
467 | } | |
468 | ||
469 | return SR_OK; | |
470 | } | |
471 | ||
472 | /* Helper for mapping a string-typed configuration value to an index | |
473 | * within a table of possible values. | |
474 | */ | |
475 | static int lookup_index(GVariant *value, const char *const *table, int len) | |
476 | { | |
477 | const char *entry; | |
478 | int i; | |
479 | ||
480 | entry = g_variant_get_string(value, NULL); | |
481 | if (!entry) | |
482 | return -1; | |
483 | ||
484 | /* Linear search is fine for very small tables. */ | |
485 | for (i = 0; i < len; i++) { | |
486 | if (strcmp(entry, table[i]) == 0) | |
487 | return i; | |
488 | } | |
489 | ||
490 | return -1; | |
491 | } | |
492 | ||
493 | /* Write device configuration setting. | |
494 | */ | |
495 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
496 | const struct sr_channel_group *cg) | |
497 | { | |
498 | uint64_t value; | |
499 | struct dev_context *devc; | |
500 | int idx; | |
501 | ||
502 | (void)cg; | |
503 | ||
504 | if (!sdi) | |
505 | return SR_ERR_ARG; | |
506 | ||
507 | devc = sdi->priv; | |
508 | ||
509 | if (!has_devopt(devc->model, key | SR_CONF_SET)) | |
510 | return SR_ERR_NA; | |
511 | ||
512 | switch (key) { | |
513 | case SR_CONF_SAMPLERATE: | |
514 | value = g_variant_get_uint64(data); | |
515 | if (value < devc->model->samplerates[devc->model->num_samplerates - 1] | |
516 | || value > devc->model->samplerates[0]) | |
517 | return SR_ERR_SAMPLERATE; | |
518 | devc->samplerate = value; | |
519 | break; | |
520 | case SR_CONF_LIMIT_MSEC: | |
521 | value = g_variant_get_uint64(data); | |
522 | if (value > MAX_LIMIT_MSEC) | |
523 | return SR_ERR_ARG; | |
524 | devc->limit_msec = value; | |
525 | break; | |
526 | case SR_CONF_LIMIT_SAMPLES: | |
527 | value = g_variant_get_uint64(data); | |
528 | if (value > MAX_LIMIT_SAMPLES) | |
529 | return SR_ERR_ARG; | |
530 | devc->limit_samples = value; | |
531 | break; | |
532 | case SR_CONF_RLE: | |
533 | devc->cfg_rle = g_variant_get_boolean(data); | |
534 | break; | |
535 | case SR_CONF_EXTERNAL_CLOCK: | |
536 | devc->cfg_clock_source = (g_variant_get_boolean(data)) | |
537 | ? CLOCK_EXT_CLK : CLOCK_INTERNAL; | |
538 | break; | |
539 | case SR_CONF_CLOCK_EDGE: | |
540 | idx = lookup_index(data, signal_edge_names, | |
541 | ARRAY_SIZE(signal_edge_names)); | |
542 | if (idx < 0) | |
543 | return SR_ERR_ARG; | |
544 | devc->cfg_clock_edge = idx; | |
545 | break; | |
546 | case SR_CONF_TRIGGER_SOURCE: | |
547 | idx = lookup_index(data, trigger_source_names, | |
548 | ARRAY_SIZE(trigger_source_names)); | |
549 | if (idx < 0) | |
550 | return SR_ERR_ARG; | |
551 | devc->cfg_trigger_source = idx; | |
552 | break; | |
553 | case SR_CONF_TRIGGER_SLOPE: | |
554 | idx = lookup_index(data, signal_edge_names, | |
555 | ARRAY_SIZE(signal_edge_names)); | |
556 | if (idx < 0) | |
557 | return SR_ERR_ARG; | |
558 | devc->cfg_trigger_slope = idx; | |
559 | break; | |
560 | default: | |
561 | /* Must not happen for a key listed in devopts. */ | |
562 | return SR_ERR_BUG; | |
563 | } | |
564 | ||
565 | return SR_OK; | |
566 | } | |
567 | ||
568 | /* Apply channel configuration change. | |
569 | */ | |
570 | static int config_channel_set(const struct sr_dev_inst *sdi, | |
571 | struct sr_channel *ch, unsigned int changes) | |
572 | { | |
573 | uint64_t channel_bit; | |
574 | struct dev_context *devc; | |
575 | ||
576 | if (!sdi) | |
577 | return SR_ERR_ARG; | |
578 | ||
579 | devc = sdi->priv; | |
580 | ||
581 | if (ch->index < 0 || ch->index >= devc->model->num_channels) { | |
582 | sr_err("Channel index %d out of range.", ch->index); | |
583 | return SR_ERR_BUG; | |
584 | } | |
585 | ||
586 | if ((changes & SR_CHANNEL_SET_ENABLED) != 0) { | |
587 | channel_bit = UINT64_C(1) << ch->index; | |
588 | ||
589 | /* Enable or disable logic input for this channel. */ | |
590 | if (ch->enabled) | |
591 | devc->channel_mask |= channel_bit; | |
592 | else | |
593 | devc->channel_mask &= ~channel_bit; | |
594 | } | |
595 | ||
596 | return SR_OK; | |
597 | } | |
598 | ||
599 | /* Derive trigger masks from the session's trigger configuration. | |
600 | */ | |
601 | static int prepare_trigger_masks(const struct sr_dev_inst *sdi) | |
602 | { | |
603 | uint64_t trigger_mask, trigger_values, trigger_edge_mask; | |
604 | uint64_t level_bit, type_bit; | |
605 | struct dev_context *devc; | |
606 | struct sr_trigger *trigger; | |
607 | struct sr_trigger_stage *stage; | |
608 | struct sr_trigger_match *match; | |
609 | const GSList *node; | |
610 | int idx; | |
611 | enum sr_trigger_matches trg; | |
612 | ||
613 | devc = sdi->priv; | |
614 | ||
615 | trigger = sr_session_trigger_get(sdi->session); | |
616 | if (!trigger || !trigger->stages) | |
617 | return SR_OK; | |
618 | ||
619 | if (trigger->stages->next) { | |
620 | sr_err("This device only supports 1 trigger stage."); | |
621 | return SR_ERR_ARG; | |
622 | } | |
623 | stage = trigger->stages->data; | |
624 | ||
625 | trigger_mask = 0; | |
626 | trigger_values = 0; | |
627 | trigger_edge_mask = 0; | |
628 | ||
629 | for (node = stage->matches; node; node = node->next) { | |
630 | match = node->data; | |
631 | ||
632 | if (!match->channel->enabled) | |
633 | continue; /* Ignore disabled channel. */ | |
634 | ||
635 | idx = match->channel->index; | |
636 | trg = match->match; | |
637 | ||
638 | if (idx < 0 || idx >= devc->model->num_channels) { | |
639 | sr_err("Channel index %d out of range.", idx); | |
640 | return SR_ERR_BUG; /* Should not happen. */ | |
641 | } | |
642 | if (trg != SR_TRIGGER_ZERO | |
643 | && trg != SR_TRIGGER_ONE | |
644 | && trg != SR_TRIGGER_RISING | |
645 | && trg != SR_TRIGGER_FALLING) { | |
646 | sr_err("Unsupported trigger match for CH%d.", idx + 1); | |
647 | return SR_ERR_ARG; | |
648 | } | |
649 | level_bit = (trg == SR_TRIGGER_ONE | |
650 | || trg == SR_TRIGGER_RISING) ? 1 : 0; | |
651 | type_bit = (trg == SR_TRIGGER_RISING | |
652 | || trg == SR_TRIGGER_FALLING) ? 1 : 0; | |
653 | ||
654 | trigger_mask |= UINT64_C(1) << idx; | |
655 | trigger_values |= level_bit << idx; | |
656 | trigger_edge_mask |= type_bit << idx; | |
657 | } | |
658 | devc->trigger_mask = trigger_mask; | |
659 | devc->trigger_values = trigger_values; | |
660 | devc->trigger_edge_mask = trigger_edge_mask; | |
661 | ||
662 | return SR_OK; | |
663 | } | |
664 | ||
665 | /* Apply current device configuration to the hardware. | |
666 | */ | |
667 | static int config_commit(const struct sr_dev_inst *sdi) | |
668 | { | |
669 | struct dev_context *devc; | |
670 | int ret; | |
671 | ||
672 | devc = sdi->priv; | |
673 | ||
674 | if (sdi->status != SR_ST_ACTIVE) | |
675 | return SR_ERR_DEV_CLOSED; | |
676 | ||
677 | if (devc->acquisition) { | |
678 | sr_err("Acquisition still in progress?"); | |
679 | return SR_ERR; | |
680 | } | |
681 | ||
682 | ret = prepare_trigger_masks(sdi); | |
683 | if (ret != SR_OK) | |
684 | return ret; | |
685 | ||
686 | ret = (*devc->model->apply_fpga_config)(sdi); | |
687 | if (ret != SR_OK) { | |
688 | sr_err("Failed to apply FPGA configuration."); | |
689 | return ret; | |
690 | } | |
691 | ||
692 | return SR_OK; | |
693 | } | |
694 | ||
695 | /* List available choices for a configuration setting. | |
696 | */ | |
697 | static int config_list(uint32_t key, GVariant **data, | |
698 | const struct sr_dev_inst *sdi, | |
699 | const struct sr_channel_group *cg) | |
700 | { | |
701 | struct dev_context *devc; | |
702 | GVariant *gvar; | |
703 | GVariantBuilder gvb; | |
704 | ||
705 | (void)cg; | |
706 | ||
707 | if (key == SR_CONF_SCAN_OPTIONS) { | |
708 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
709 | scanopts, ARRAY_SIZE(scanopts), sizeof(scanopts[0])); | |
710 | return SR_OK; | |
711 | } | |
712 | if (!sdi) { | |
713 | if (key != SR_CONF_DEVICE_OPTIONS) | |
714 | return SR_ERR_ARG; | |
715 | ||
716 | /* List driver capabilities. */ | |
717 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
718 | drvopts, ARRAY_SIZE(drvopts), sizeof(drvopts[0])); | |
719 | return SR_OK; | |
720 | } | |
721 | ||
722 | devc = sdi->priv; | |
723 | ||
724 | /* List the model's device options. */ | |
725 | if (key == SR_CONF_DEVICE_OPTIONS) { | |
726 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
727 | devc->model->devopts, devc->model->num_devopts, | |
728 | sizeof(devc->model->devopts[0])); | |
729 | return SR_OK; | |
730 | } | |
731 | ||
732 | if (!has_devopt(devc->model, key | SR_CONF_LIST)) | |
733 | return SR_ERR_NA; | |
734 | ||
735 | switch (key) { | |
736 | case SR_CONF_SAMPLERATE: | |
737 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_VARDICT); | |
738 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64, | |
739 | devc->model->samplerates, devc->model->num_samplerates, | |
740 | sizeof(devc->model->samplerates[0])); | |
741 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
742 | *data = g_variant_builder_end(&gvb); | |
743 | break; | |
744 | case SR_CONF_TRIGGER_MATCH: | |
745 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
746 | trigger_matches, ARRAY_SIZE(trigger_matches), | |
747 | sizeof(trigger_matches[0])); | |
748 | break; | |
749 | case SR_CONF_TRIGGER_SOURCE: | |
750 | *data = g_variant_new_strv(trigger_source_names, | |
751 | ARRAY_SIZE(trigger_source_names)); | |
752 | break; | |
753 | case SR_CONF_TRIGGER_SLOPE: | |
754 | case SR_CONF_CLOCK_EDGE: | |
755 | *data = g_variant_new_strv(signal_edge_names, | |
756 | ARRAY_SIZE(signal_edge_names)); | |
757 | break; | |
758 | default: | |
759 | /* Must not happen for a key listed in devopts. */ | |
760 | return SR_ERR_BUG; | |
761 | } | |
762 | ||
763 | return SR_OK; | |
764 | } | |
765 | ||
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 | */ | |
770 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
771 | { | |
772 | (void)cb_data; | |
773 | ||
774 | if (sdi->status != SR_ST_ACTIVE) | |
775 | return SR_ERR_DEV_CLOSED; | |
776 | ||
777 | sr_info("Starting acquisition."); | |
778 | ||
779 | return lwla_start_acquisition(sdi); | |
780 | } | |
781 | ||
782 | /* Request that a running capture operation be stopped. | |
783 | */ | |
784 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
785 | { | |
786 | struct dev_context *devc; | |
787 | ||
788 | (void)cb_data; | |
789 | ||
790 | devc = sdi->priv; | |
791 | ||
792 | if (sdi->status != SR_ST_ACTIVE) | |
793 | return SR_ERR_DEV_CLOSED; | |
794 | ||
795 | if (devc->state != STATE_IDLE && !devc->cancel_requested) { | |
796 | devc->cancel_requested = TRUE; | |
797 | sr_dbg("Stopping acquisition."); | |
798 | } | |
799 | ||
800 | return SR_OK; | |
801 | } | |
802 | ||
803 | /* SysClk LWLA driver descriptor. | |
804 | */ | |
805 | SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = { | |
806 | .name = "sysclk-lwla", | |
807 | .longname = "SysClk LWLA series", | |
808 | .api_version = 1, | |
809 | .init = init, | |
810 | .cleanup = dev_clear, | |
811 | .scan = scan, | |
812 | .dev_list = dev_list, | |
813 | .dev_clear = dev_clear, | |
814 | .config_get = config_get, | |
815 | .config_set = config_set, | |
816 | .config_channel_set = config_channel_set, | |
817 | .config_commit = config_commit, | |
818 | .config_list = config_list, | |
819 | .dev_open = dev_open, | |
820 | .dev_close = dev_close, | |
821 | .dev_acquisition_start = dev_acquisition_start, | |
822 | .dev_acquisition_stop = dev_acquisition_stop, | |
823 | .context = NULL, | |
824 | }; |