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