]>
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; | |
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 | */ | |
161 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
162 | { | |
163 | GSList *conn_devices, *devices, *node; | |
164 | struct drv_context *drvc; | |
165 | struct sr_dev_inst *sdi; | |
166 | struct sr_config *src; | |
167 | const char *conn; | |
168 | libusb_device **devlist; | |
169 | ssize_t num_devs, i; | |
170 | ||
171 | drvc = di->context; | |
172 | conn = NULL; | |
173 | conn_devices = NULL; | |
174 | devices = NULL; | |
175 | ||
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 | } | |
183 | if (conn) { | |
184 | /* Find devices matching the connection specification. */ | |
185 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
186 | } | |
187 | ||
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 | } | |
197 | ||
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 */ | |
203 | ||
204 | /* Register device instance with driver. */ | |
205 | sdi->driver = di; | |
206 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
207 | devices = g_slist_append(devices, sdi); | |
208 | } | |
209 | ||
210 | libusb_free_device_list(devlist, 1); | |
211 | g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free); | |
212 | ||
213 | return devices; | |
214 | } | |
215 | ||
216 | /* Return the list of devices found during scan. | |
217 | */ | |
218 | static GSList *dev_list(const struct sr_dev_driver *di) | |
219 | { | |
220 | struct drv_context *drvc; | |
221 | ||
222 | drvc = di->context; | |
223 | ||
224 | return drvc->instances; | |
225 | } | |
226 | ||
227 | /* Destroy the private device context. | |
228 | */ | |
229 | static void clear_dev_context(void *priv) | |
230 | { | |
231 | struct dev_context *devc; | |
232 | ||
233 | devc = priv; | |
234 | ||
235 | if (devc->acquisition) { | |
236 | sr_err("Cannot clear device context during acquisition!"); | |
237 | return; /* leak and pray */ | |
238 | } | |
239 | sr_dbg("Device context cleared."); | |
240 | ||
241 | g_free(devc); | |
242 | } | |
243 | ||
244 | /* Destroy all device instances. | |
245 | */ | |
246 | static int dev_clear(const struct sr_dev_driver *di) | |
247 | { | |
248 | return std_dev_clear(di, &clear_dev_context); | |
249 | } | |
250 | ||
251 | /* Drain any pending data from the USB transfer buffers on the device. | |
252 | * This may be necessary e.g. after a crash or generally to clean up after | |
253 | * an abnormal condition. | |
254 | */ | |
255 | static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint) | |
256 | { | |
257 | int drained, xfer_len; | |
258 | int ret; | |
259 | unsigned char buf[512]; | |
260 | ||
261 | const unsigned int drain_timeout_ms = 10; | |
262 | ||
263 | drained = 0; | |
264 | do { | |
265 | xfer_len = 0; | |
266 | ret = libusb_bulk_transfer(usb->devhdl, endpoint, | |
267 | buf, sizeof(buf), &xfer_len, | |
268 | drain_timeout_ms); | |
269 | drained += xfer_len; | |
270 | } while (ret == LIBUSB_SUCCESS && xfer_len != 0); | |
271 | ||
272 | if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) { | |
273 | sr_err("Failed to drain USB endpoint %u: %s.", | |
274 | endpoint & (LIBUSB_ENDPOINT_IN - 1), | |
275 | libusb_error_name(ret)); | |
276 | return SR_ERR; | |
277 | } | |
278 | if (drained > 0) { | |
279 | sr_warn("Drained %d bytes from USB endpoint %u.", | |
280 | drained, endpoint & (LIBUSB_ENDPOINT_IN - 1)); | |
281 | } | |
282 | return SR_OK; | |
283 | } | |
284 | ||
285 | /* Open and initialize device. | |
286 | */ | |
287 | static int dev_open(struct sr_dev_inst *sdi) | |
288 | { | |
289 | struct drv_context *drvc; | |
290 | struct dev_context *devc; | |
291 | struct sr_usb_dev_inst *usb; | |
292 | int ret; | |
293 | ||
294 | drvc = sdi->driver->context; | |
295 | devc = sdi->priv; | |
296 | usb = sdi->conn; | |
297 | ||
298 | if (!drvc) { | |
299 | sr_err("Driver was not initialized."); | |
300 | return SR_ERR; | |
301 | } | |
302 | if (sdi->status != SR_ST_INACTIVE) { | |
303 | sr_err("Device already open."); | |
304 | return SR_ERR; | |
305 | } | |
306 | ||
307 | ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb); | |
308 | if (ret != SR_OK) | |
309 | return ret; | |
310 | ||
311 | ret = libusb_set_configuration(usb->devhdl, USB_CONFIG); | |
312 | if (ret != LIBUSB_SUCCESS) { | |
313 | sr_err("Failed to set USB configuration: %s.", | |
314 | libusb_error_name(ret)); | |
315 | sr_usb_close(usb); | |
316 | return SR_ERR; | |
317 | } | |
318 | ||
319 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); | |
320 | if (ret != LIBUSB_SUCCESS) { | |
321 | sr_err("Failed to claim interface: %s.", | |
322 | libusb_error_name(ret)); | |
323 | sr_usb_close(usb); | |
324 | return SR_ERR; | |
325 | } | |
326 | ||
327 | ret = drain_usb(usb, EP_REPLY); | |
328 | if (ret != SR_OK) { | |
329 | sr_usb_close(usb); | |
330 | return ret; | |
331 | } | |
332 | /* This delay appears to be necessary for reliable operation. */ | |
333 | g_usleep(30 * 1000); | |
334 | ||
335 | sdi->status = SR_ST_ACTIVE; | |
336 | ||
337 | devc->active_fpga_config = FPGA_NOCONF; | |
338 | devc->short_transfer_quirk = FALSE; | |
339 | devc->state = STATE_IDLE; | |
340 | ||
341 | ret = (*devc->model->apply_fpga_config)(sdi); | |
342 | ||
343 | if (ret == SR_OK) | |
344 | ret = (*devc->model->device_init_check)(sdi); | |
345 | ||
346 | if (ret != SR_OK) { | |
347 | sdi->status = SR_ST_INACTIVE; | |
348 | sr_usb_close(usb); | |
349 | return ret; | |
350 | } | |
351 | if (devc->short_transfer_quirk) | |
352 | sr_warn("Short transfer quirk detected! " | |
353 | "Memory reads will be slow."); | |
354 | return SR_OK; | |
355 | } | |
356 | ||
357 | /* Shutdown and close device. | |
358 | */ | |
359 | static int dev_close(struct sr_dev_inst *sdi) | |
360 | { | |
361 | struct drv_context *drvc; | |
362 | struct dev_context *devc; | |
363 | struct sr_usb_dev_inst *usb; | |
364 | int ret; | |
365 | ||
366 | drvc = sdi->driver->context; | |
367 | devc = sdi->priv; | |
368 | usb = sdi->conn; | |
369 | ||
370 | if (!drvc) { | |
371 | sr_err("Driver was not initialized."); | |
372 | return SR_ERR; | |
373 | } | |
374 | if (sdi->status == SR_ST_INACTIVE) { | |
375 | sr_dbg("Device already closed."); | |
376 | return SR_OK; | |
377 | } | |
378 | if (devc->acquisition) { | |
379 | sr_err("Cannot close device during acquisition!"); | |
380 | /* Request stop, leak handle, and prepare for the worst. */ | |
381 | devc->cancel_requested = TRUE; | |
382 | return SR_ERR_BUG; | |
383 | } | |
384 | ||
385 | sdi->status = SR_ST_INACTIVE; | |
386 | ||
387 | /* Download of the shutdown bitstream, if any. */ | |
388 | ret = (*devc->model->apply_fpga_config)(sdi); | |
389 | if (ret != SR_OK) | |
390 | sr_warn("Unable to shut down device."); | |
391 | ||
392 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
393 | sr_usb_close(usb); | |
394 | ||
395 | return ret; | |
396 | } | |
397 | ||
398 | /* Check whether the device options contain a specific key. | |
399 | * Also match against get/set/list bits if specified. | |
400 | */ | |
401 | static int has_devopt(const struct model_info *model, uint32_t key) | |
402 | { | |
403 | unsigned int i; | |
404 | ||
405 | for (i = 0; i < model->num_devopts; i++) { | |
406 | if ((model->devopts[i] & (SR_CONF_MASK | key)) == key) | |
407 | return TRUE; | |
408 | } | |
409 | return FALSE; | |
410 | } | |
411 | ||
412 | /* Read device configuration setting. | |
413 | */ | |
414 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
415 | const struct sr_channel_group *cg) | |
416 | { | |
417 | struct dev_context *devc; | |
418 | unsigned int idx; | |
419 | ||
420 | (void)cg; | |
421 | ||
422 | if (!sdi) | |
423 | return SR_ERR_ARG; | |
424 | ||
425 | devc = sdi->priv; | |
426 | ||
427 | if (!has_devopt(devc->model, key | SR_CONF_GET)) | |
428 | return SR_ERR_NA; | |
429 | ||
430 | switch (key) { | |
431 | case SR_CONF_SAMPLERATE: | |
432 | *data = g_variant_new_uint64(devc->samplerate); | |
433 | break; | |
434 | case SR_CONF_LIMIT_MSEC: | |
435 | *data = g_variant_new_uint64(devc->limit_msec); | |
436 | break; | |
437 | case SR_CONF_LIMIT_SAMPLES: | |
438 | *data = g_variant_new_uint64(devc->limit_samples); | |
439 | break; | |
440 | case SR_CONF_RLE: | |
441 | *data = g_variant_new_boolean(devc->cfg_rle); | |
442 | break; | |
443 | case SR_CONF_EXTERNAL_CLOCK: | |
444 | *data = g_variant_new_boolean(devc->cfg_clock_source | |
445 | == CLOCK_EXT_CLK); | |
446 | break; | |
447 | case SR_CONF_CLOCK_EDGE: | |
448 | idx = devc->cfg_clock_edge; | |
449 | if (idx >= ARRAY_SIZE(signal_edge_names)) | |
450 | return SR_ERR_BUG; | |
451 | *data = g_variant_new_string(signal_edge_names[idx]); | |
452 | break; | |
453 | case SR_CONF_TRIGGER_SOURCE: | |
454 | idx = devc->cfg_trigger_source; | |
455 | if (idx >= ARRAY_SIZE(trigger_source_names)) | |
456 | return SR_ERR_BUG; | |
457 | *data = g_variant_new_string(trigger_source_names[idx]); | |
458 | break; | |
459 | case SR_CONF_TRIGGER_SLOPE: | |
460 | idx = devc->cfg_trigger_slope; | |
461 | if (idx >= ARRAY_SIZE(signal_edge_names)) | |
462 | return SR_ERR_BUG; | |
463 | *data = g_variant_new_string(signal_edge_names[idx]); | |
464 | break; | |
465 | default: | |
466 | /* Must not happen for a key listed in devopts. */ | |
467 | return SR_ERR_BUG; | |
468 | } | |
469 | ||
470 | return SR_OK; | |
471 | } | |
472 | ||
473 | /* Helper for mapping a string-typed configuration value to an index | |
474 | * within a table of possible values. | |
475 | */ | |
476 | static int lookup_index(GVariant *value, const char *const *table, int len) | |
477 | { | |
478 | const char *entry; | |
479 | int i; | |
480 | ||
481 | entry = g_variant_get_string(value, NULL); | |
482 | if (!entry) | |
483 | return -1; | |
484 | ||
485 | /* Linear search is fine for very small tables. */ | |
486 | for (i = 0; i < len; ++i) { | |
487 | if (strcmp(entry, table[i]) == 0) | |
488 | return i; | |
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; | |
604 | uint64_t trigger_values; | |
605 | uint64_t trigger_edge_mask; | |
606 | uint64_t level_bit, type_bit; | |
607 | struct dev_context *devc; | |
608 | struct sr_trigger *trigger; | |
609 | struct sr_trigger_stage *stage; | |
610 | struct sr_trigger_match *match; | |
611 | const GSList *node; | |
612 | int idx; | |
613 | enum sr_trigger_matches trg; | |
614 | ||
615 | devc = sdi->priv; | |
616 | ||
617 | trigger = sr_session_trigger_get(sdi->session); | |
618 | if (!trigger || !trigger->stages) | |
619 | return SR_OK; | |
620 | ||
621 | if (trigger->stages->next) { | |
622 | sr_err("This device only supports 1 trigger stage."); | |
623 | return SR_ERR_ARG; | |
624 | } | |
625 | stage = trigger->stages->data; | |
626 | ||
627 | trigger_mask = 0; | |
628 | trigger_values = 0; | |
629 | trigger_edge_mask = 0; | |
630 | ||
631 | for (node = stage->matches; node; node = node->next) { | |
632 | match = node->data; | |
633 | ||
634 | if (!match->channel->enabled) | |
635 | continue; /* ignore disabled channel */ | |
636 | ||
637 | idx = match->channel->index; | |
638 | trg = match->match; | |
639 | ||
640 | if (idx < 0 || idx >= devc->model->num_channels) { | |
641 | sr_err("Channel index %d out of range.", idx); | |
642 | return SR_ERR_BUG; /* should not happen */ | |
643 | } | |
644 | if (trg != SR_TRIGGER_ZERO | |
645 | && trg != SR_TRIGGER_ONE | |
646 | && trg != SR_TRIGGER_RISING | |
647 | && trg != SR_TRIGGER_FALLING) { | |
648 | sr_err("Unsupported trigger match for CH%d.", idx + 1); | |
649 | return SR_ERR_ARG; | |
650 | } | |
651 | level_bit = (trg == SR_TRIGGER_ONE | |
652 | || trg == SR_TRIGGER_RISING) ? 1 : 0; | |
653 | type_bit = (trg == SR_TRIGGER_RISING | |
654 | || trg == SR_TRIGGER_FALLING) ? 1 : 0; | |
655 | ||
656 | trigger_mask |= UINT64_C(1) << idx; | |
657 | trigger_values |= level_bit << idx; | |
658 | trigger_edge_mask |= type_bit << idx; | |
659 | } | |
660 | devc->trigger_mask = trigger_mask; | |
661 | devc->trigger_values = trigger_values; | |
662 | devc->trigger_edge_mask = trigger_edge_mask; | |
663 | ||
664 | return SR_OK; | |
665 | } | |
666 | ||
667 | /* Apply current device configuration to the hardware. | |
668 | */ | |
669 | static int config_commit(const struct sr_dev_inst *sdi) | |
670 | { | |
671 | struct dev_context *devc; | |
672 | int ret; | |
673 | ||
674 | devc = sdi->priv; | |
675 | ||
676 | if (sdi->status != SR_ST_ACTIVE) | |
677 | return SR_ERR_DEV_CLOSED; | |
678 | ||
679 | if (devc->acquisition) { | |
680 | sr_err("Acquisition still in progress?"); | |
681 | return SR_ERR; | |
682 | } | |
683 | ||
684 | ret = prepare_trigger_masks(sdi); | |
685 | if (ret != SR_OK) | |
686 | return ret; | |
687 | ||
688 | ret = (*devc->model->apply_fpga_config)(sdi); | |
689 | if (ret != SR_OK) { | |
690 | sr_err("Failed to apply FPGA configuration."); | |
691 | return ret; | |
692 | } | |
693 | ||
694 | return SR_OK; | |
695 | } | |
696 | ||
697 | /* List available choices for a configuration setting. | |
698 | */ | |
699 | static int config_list(uint32_t key, GVariant **data, | |
700 | const struct sr_dev_inst *sdi, | |
701 | const struct sr_channel_group *cg) | |
702 | { | |
703 | struct dev_context *devc; | |
704 | GVariant *gvar; | |
705 | GVariantBuilder gvb; | |
706 | ||
707 | (void)cg; | |
708 | ||
709 | if (key == SR_CONF_SCAN_OPTIONS) { | |
710 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
711 | scanopts, ARRAY_SIZE(scanopts), | |
712 | sizeof(scanopts[0])); | |
713 | return SR_OK; | |
714 | } | |
715 | if (!sdi) { | |
716 | if (key != SR_CONF_DEVICE_OPTIONS) | |
717 | return SR_ERR_ARG; | |
718 | ||
719 | /* List driver capabilities. */ | |
720 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
721 | drvopts, ARRAY_SIZE(drvopts), | |
722 | sizeof(drvopts[0])); | |
723 | return SR_OK; | |
724 | } | |
725 | ||
726 | devc = sdi->priv; | |
727 | ||
728 | /* List the model's device options. */ | |
729 | if (key == SR_CONF_DEVICE_OPTIONS) { | |
730 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
731 | devc->model->devopts, | |
732 | devc->model->num_devopts, | |
733 | sizeof(devc->model->devopts[0])); | |
734 | return SR_OK; | |
735 | } | |
736 | ||
737 | if (!has_devopt(devc->model, key | SR_CONF_LIST)) | |
738 | return SR_ERR_NA; | |
739 | ||
740 | switch (key) { | |
741 | case SR_CONF_SAMPLERATE: | |
742 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_VARDICT); | |
743 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64, | |
744 | devc->model->samplerates, | |
745 | devc->model->num_samplerates, | |
746 | sizeof(devc->model->samplerates[0])); | |
747 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
748 | *data = g_variant_builder_end(&gvb); | |
749 | break; | |
750 | case SR_CONF_TRIGGER_MATCH: | |
751 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
752 | trigger_matches, | |
753 | ARRAY_SIZE(trigger_matches), | |
754 | sizeof(trigger_matches[0])); | |
755 | break; | |
756 | case SR_CONF_TRIGGER_SOURCE: | |
757 | *data = g_variant_new_strv(trigger_source_names, | |
758 | ARRAY_SIZE(trigger_source_names)); | |
759 | break; | |
760 | case SR_CONF_TRIGGER_SLOPE: | |
761 | case SR_CONF_CLOCK_EDGE: | |
762 | *data = g_variant_new_strv(signal_edge_names, | |
763 | ARRAY_SIZE(signal_edge_names)); | |
764 | break; | |
765 | default: | |
766 | /* Must not happen for a key listed in devopts. */ | |
767 | return SR_ERR_BUG; | |
768 | } | |
769 | ||
770 | return SR_OK; | |
771 | } | |
772 | ||
773 | /* Set up the device hardware to begin capturing samples as soon as the | |
774 | * configured trigger conditions are met, or immediately if no triggers | |
775 | * are configured. | |
776 | */ | |
777 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
778 | { | |
779 | (void)cb_data; | |
780 | ||
781 | if (sdi->status != SR_ST_ACTIVE) | |
782 | return SR_ERR_DEV_CLOSED; | |
783 | ||
784 | sr_info("Starting acquisition."); | |
785 | ||
786 | return lwla_start_acquisition(sdi); | |
787 | } | |
788 | ||
789 | /* Request that a running capture operation be stopped. | |
790 | */ | |
791 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
792 | { | |
793 | struct dev_context *devc; | |
794 | ||
795 | (void)cb_data; | |
796 | devc = sdi->priv; | |
797 | ||
798 | if (sdi->status != SR_ST_ACTIVE) | |
799 | return SR_ERR_DEV_CLOSED; | |
800 | ||
801 | if (devc->state != STATE_IDLE && !devc->cancel_requested) { | |
802 | devc->cancel_requested = TRUE; | |
803 | sr_dbg("Stopping acquisition."); | |
804 | } | |
805 | return SR_OK; | |
806 | } | |
807 | ||
808 | /* SysClk LWLA driver descriptor. | |
809 | */ | |
810 | SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = { | |
811 | .name = "sysclk-lwla", | |
812 | .longname = "SysClk LWLA series", | |
813 | .api_version = 1, | |
814 | .init = init, | |
815 | .cleanup = dev_clear, | |
816 | .scan = scan, | |
817 | .dev_list = dev_list, | |
818 | .dev_clear = dev_clear, | |
819 | .config_get = config_get, | |
820 | .config_set = config_set, | |
821 | .config_channel_set = config_channel_set, | |
822 | .config_commit = config_commit, | |
823 | .config_list = config_list, | |
824 | .dev_open = dev_open, | |
825 | .dev_close = dev_close, | |
826 | .dev_acquisition_start = dev_acquisition_start, | |
827 | .dev_acquisition_stop = dev_acquisition_stop, | |
828 | .context = NULL, | |
829 | }; |