]>
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 | /* 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) | |
67 | { | |
68 | struct sr_dev_inst *sdi; | |
69 | struct dev_context *devc; | |
70 | int i; | |
71 | char name[8]; | |
72 | ||
73 | /* Initialize private device context. */ | |
74 | devc = g_malloc0(sizeof(struct dev_context)); | |
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; | |
80 | ||
81 | /* Create sigrok device instance. */ | |
82 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
83 | sdi->status = SR_ST_INACTIVE; | |
84 | sdi->vendor = g_strdup(VENDOR_NAME); | |
85 | sdi->model = g_strdup(model->name); | |
86 | sdi->priv = devc; | |
87 | ||
88 | /* Generate list of logic channels. */ | |
89 | for (i = 0; i < model->num_channels; i++) { | |
90 | /* The LWLA series simply number channels from CH1 to CHxx. */ | |
91 | g_snprintf(name, sizeof(name), "CH%d", i + 1); | |
92 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name); | |
93 | } | |
94 | ||
95 | return sdi; | |
96 | } | |
97 | ||
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; | |
109 | int bus, address, ret; | |
110 | unsigned int vid, pid; | |
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 | */ | |
153 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
154 | { | |
155 | GSList *conn_devices, *devices, *node; | |
156 | struct drv_context *drvc; | |
157 | struct sr_dev_inst *sdi; | |
158 | struct sr_config *src; | |
159 | const char *conn; | |
160 | libusb_device **devlist; | |
161 | ssize_t num_devs, i; | |
162 | ||
163 | drvc = di->context; | |
164 | conn = NULL; | |
165 | conn_devices = NULL; | |
166 | devices = NULL; | |
167 | ||
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 | } | |
175 | if (conn) { | |
176 | /* Find devices matching the connection specification. */ | |
177 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
178 | } | |
179 | ||
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 | } | |
189 | ||
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 */ | |
195 | ||
196 | /* Register device instance with driver. */ | |
197 | devices = g_slist_append(devices, sdi); | |
198 | } | |
199 | ||
200 | libusb_free_device_list(devlist, 1); | |
201 | g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free); | |
202 | ||
203 | return std_scan_complete(di, devices); | |
204 | } | |
205 | ||
206 | /* Drain any pending data from the USB transfer buffers on the device. | |
207 | * This may be necessary e.g. after a crash or generally to clean up after | |
208 | * an abnormal condition. | |
209 | */ | |
210 | static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint) | |
211 | { | |
212 | int drained, xfer_len, ret; | |
213 | unsigned char buf[512]; | |
214 | const unsigned int drain_timeout_ms = 10; | |
215 | ||
216 | drained = 0; | |
217 | do { | |
218 | xfer_len = 0; | |
219 | ret = libusb_bulk_transfer(usb->devhdl, endpoint, | |
220 | buf, sizeof(buf), &xfer_len, | |
221 | drain_timeout_ms); | |
222 | drained += xfer_len; | |
223 | } while (ret == LIBUSB_SUCCESS && xfer_len != 0); | |
224 | ||
225 | if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) { | |
226 | sr_err("Failed to drain USB endpoint %u: %s.", | |
227 | endpoint & (LIBUSB_ENDPOINT_IN - 1), | |
228 | libusb_error_name(ret)); | |
229 | return SR_ERR; | |
230 | } | |
231 | if (drained > 0) { | |
232 | sr_warn("Drained %d bytes from USB endpoint %u.", | |
233 | drained, endpoint & (LIBUSB_ENDPOINT_IN - 1)); | |
234 | } | |
235 | ||
236 | return SR_OK; | |
237 | } | |
238 | ||
239 | /* Open and initialize device. | |
240 | */ | |
241 | static int dev_open(struct sr_dev_inst *sdi) | |
242 | { | |
243 | struct drv_context *drvc; | |
244 | struct dev_context *devc; | |
245 | struct sr_usb_dev_inst *usb; | |
246 | int i, ret; | |
247 | ||
248 | drvc = sdi->driver->context; | |
249 | devc = sdi->priv; | |
250 | usb = sdi->conn; | |
251 | ||
252 | /* Try the whole shebang three times, fingers crossed. */ | |
253 | for (i = 0; i < 3; i++) { | |
254 | ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb); | |
255 | if (ret != SR_OK) | |
256 | return ret; | |
257 | ||
258 | ret = libusb_set_configuration(usb->devhdl, USB_CONFIG); | |
259 | if (ret != LIBUSB_SUCCESS) { | |
260 | sr_err("Failed to set USB configuration: %s.", | |
261 | libusb_error_name(ret)); | |
262 | sr_usb_close(usb); | |
263 | return SR_ERR; | |
264 | } | |
265 | ||
266 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); | |
267 | if (ret != LIBUSB_SUCCESS) { | |
268 | sr_err("Failed to claim interface: %s.", | |
269 | libusb_error_name(ret)); | |
270 | sr_usb_close(usb); | |
271 | return SR_ERR; | |
272 | } | |
273 | ||
274 | ret = drain_usb(usb, EP_REPLY); | |
275 | if (ret != SR_OK) { | |
276 | sr_usb_close(usb); | |
277 | return ret; | |
278 | } | |
279 | /* This delay appears to be necessary for reliable operation. */ | |
280 | g_usleep(30 * 1000); | |
281 | ||
282 | devc->active_fpga_config = FPGA_NOCONF; | |
283 | devc->short_transfer_quirk = FALSE; | |
284 | devc->state = STATE_IDLE; | |
285 | ||
286 | ret = (*devc->model->apply_fpga_config)(sdi); | |
287 | ||
288 | if (ret == SR_OK) | |
289 | ret = (*devc->model->device_init_check)(sdi); | |
290 | if (ret == SR_OK) | |
291 | break; | |
292 | ||
293 | /* Rinse and repeat. */ | |
294 | sr_usb_close(usb); | |
295 | } | |
296 | ||
297 | if (ret == SR_OK && devc->short_transfer_quirk) | |
298 | sr_warn("Short transfer quirk detected! " | |
299 | "Memory reads will be slow."); | |
300 | return ret; | |
301 | } | |
302 | ||
303 | /* Shutdown and close device. */ | |
304 | static int dev_close(struct sr_dev_inst *sdi) | |
305 | { | |
306 | struct dev_context *devc; | |
307 | struct sr_usb_dev_inst *usb; | |
308 | int ret; | |
309 | ||
310 | devc = sdi->priv; | |
311 | usb = sdi->conn; | |
312 | ||
313 | if (devc->acquisition) { | |
314 | sr_err("Cannot close device during acquisition!"); | |
315 | /* Request stop, leak handle, and prepare for the worst. */ | |
316 | devc->cancel_requested = TRUE; | |
317 | return SR_ERR_BUG; | |
318 | } | |
319 | ||
320 | /* Download of the shutdown bitstream, if any. */ | |
321 | ret = (*devc->model->apply_fpga_config)(sdi); | |
322 | if (ret != SR_OK) | |
323 | sr_warn("Unable to shut down device."); | |
324 | ||
325 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
326 | ||
327 | sr_usb_close(usb); | |
328 | ||
329 | return SR_OK; | |
330 | } | |
331 | ||
332 | /* Check whether the device options contain a specific key. | |
333 | * Also match against get/set/list bits if specified. | |
334 | */ | |
335 | static int has_devopt(const struct model_info *model, uint32_t key) | |
336 | { | |
337 | unsigned int i; | |
338 | ||
339 | for (i = 0; i < model->num_devopts; i++) { | |
340 | if ((model->devopts[i] & (SR_CONF_MASK | key)) == key) | |
341 | return TRUE; | |
342 | } | |
343 | ||
344 | return FALSE; | |
345 | } | |
346 | ||
347 | /* Read device configuration setting. | |
348 | */ | |
349 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
350 | const struct sr_channel_group *cg) | |
351 | { | |
352 | struct dev_context *devc; | |
353 | unsigned int idx; | |
354 | ||
355 | (void)cg; | |
356 | ||
357 | if (!sdi) | |
358 | return SR_ERR_ARG; | |
359 | ||
360 | devc = sdi->priv; | |
361 | ||
362 | if (!has_devopt(devc->model, key | SR_CONF_GET)) | |
363 | return SR_ERR_NA; | |
364 | ||
365 | switch (key) { | |
366 | case SR_CONF_SAMPLERATE: | |
367 | *data = g_variant_new_uint64(devc->samplerate); | |
368 | break; | |
369 | case SR_CONF_LIMIT_MSEC: | |
370 | *data = g_variant_new_uint64(devc->limit_msec); | |
371 | break; | |
372 | case SR_CONF_LIMIT_SAMPLES: | |
373 | *data = g_variant_new_uint64(devc->limit_samples); | |
374 | break; | |
375 | case SR_CONF_RLE: | |
376 | *data = g_variant_new_boolean(devc->cfg_rle); | |
377 | break; | |
378 | case SR_CONF_EXTERNAL_CLOCK: | |
379 | *data = g_variant_new_boolean(devc->cfg_clock_source | |
380 | == CLOCK_EXT_CLK); | |
381 | break; | |
382 | case SR_CONF_CLOCK_EDGE: | |
383 | idx = devc->cfg_clock_edge; | |
384 | if (idx >= ARRAY_SIZE(signal_edge_names)) | |
385 | return SR_ERR_BUG; | |
386 | *data = g_variant_new_string(signal_edge_names[idx]); | |
387 | break; | |
388 | case SR_CONF_TRIGGER_SOURCE: | |
389 | idx = devc->cfg_trigger_source; | |
390 | if (idx >= ARRAY_SIZE(trigger_source_names)) | |
391 | return SR_ERR_BUG; | |
392 | *data = g_variant_new_string(trigger_source_names[idx]); | |
393 | break; | |
394 | case SR_CONF_TRIGGER_SLOPE: | |
395 | idx = devc->cfg_trigger_slope; | |
396 | if (idx >= ARRAY_SIZE(signal_edge_names)) | |
397 | return SR_ERR_BUG; | |
398 | *data = g_variant_new_string(signal_edge_names[idx]); | |
399 | break; | |
400 | default: | |
401 | /* Must not happen for a key listed in devopts. */ | |
402 | return SR_ERR_BUG; | |
403 | } | |
404 | ||
405 | return SR_OK; | |
406 | } | |
407 | ||
408 | /* Helper for mapping a string-typed configuration value to an index | |
409 | * within a table of possible values. | |
410 | */ | |
411 | static int lookup_index(GVariant *value, const char *const *table, int len) | |
412 | { | |
413 | const char *entry; | |
414 | int i; | |
415 | ||
416 | entry = g_variant_get_string(value, NULL); | |
417 | if (!entry) | |
418 | return -1; | |
419 | ||
420 | /* Linear search is fine for very small tables. */ | |
421 | for (i = 0; i < len; i++) { | |
422 | if (strcmp(entry, table[i]) == 0) | |
423 | return i; | |
424 | } | |
425 | ||
426 | return -1; | |
427 | } | |
428 | ||
429 | /* Write device configuration setting. | |
430 | */ | |
431 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
432 | const struct sr_channel_group *cg) | |
433 | { | |
434 | uint64_t value; | |
435 | struct dev_context *devc; | |
436 | int idx; | |
437 | ||
438 | (void)cg; | |
439 | ||
440 | if (!sdi) | |
441 | return SR_ERR_ARG; | |
442 | ||
443 | devc = sdi->priv; | |
444 | ||
445 | if (!has_devopt(devc->model, key | SR_CONF_SET)) | |
446 | return SR_ERR_NA; | |
447 | ||
448 | switch (key) { | |
449 | case SR_CONF_SAMPLERATE: | |
450 | value = g_variant_get_uint64(data); | |
451 | if (value < devc->model->samplerates[devc->model->num_samplerates - 1] | |
452 | || value > devc->model->samplerates[0]) | |
453 | return SR_ERR_SAMPLERATE; | |
454 | devc->samplerate = value; | |
455 | break; | |
456 | case SR_CONF_LIMIT_MSEC: | |
457 | value = g_variant_get_uint64(data); | |
458 | if (value > MAX_LIMIT_MSEC) | |
459 | return SR_ERR_ARG; | |
460 | devc->limit_msec = value; | |
461 | break; | |
462 | case SR_CONF_LIMIT_SAMPLES: | |
463 | value = g_variant_get_uint64(data); | |
464 | if (value > MAX_LIMIT_SAMPLES) | |
465 | return SR_ERR_ARG; | |
466 | devc->limit_samples = value; | |
467 | break; | |
468 | case SR_CONF_RLE: | |
469 | devc->cfg_rle = g_variant_get_boolean(data); | |
470 | break; | |
471 | case SR_CONF_EXTERNAL_CLOCK: | |
472 | devc->cfg_clock_source = (g_variant_get_boolean(data)) | |
473 | ? CLOCK_EXT_CLK : CLOCK_INTERNAL; | |
474 | break; | |
475 | case SR_CONF_CLOCK_EDGE: | |
476 | idx = lookup_index(data, signal_edge_names, | |
477 | ARRAY_SIZE(signal_edge_names)); | |
478 | if (idx < 0) | |
479 | return SR_ERR_ARG; | |
480 | devc->cfg_clock_edge = idx; | |
481 | break; | |
482 | case SR_CONF_TRIGGER_SOURCE: | |
483 | idx = lookup_index(data, trigger_source_names, | |
484 | ARRAY_SIZE(trigger_source_names)); | |
485 | if (idx < 0) | |
486 | return SR_ERR_ARG; | |
487 | devc->cfg_trigger_source = idx; | |
488 | break; | |
489 | case SR_CONF_TRIGGER_SLOPE: | |
490 | idx = lookup_index(data, signal_edge_names, | |
491 | ARRAY_SIZE(signal_edge_names)); | |
492 | if (idx < 0) | |
493 | return SR_ERR_ARG; | |
494 | devc->cfg_trigger_slope = idx; | |
495 | break; | |
496 | default: | |
497 | /* Must not happen for a key listed in devopts. */ | |
498 | return SR_ERR_BUG; | |
499 | } | |
500 | ||
501 | return SR_OK; | |
502 | } | |
503 | ||
504 | /* Apply channel configuration change. | |
505 | */ | |
506 | static int config_channel_set(const struct sr_dev_inst *sdi, | |
507 | struct sr_channel *ch, unsigned int changes) | |
508 | { | |
509 | uint64_t channel_bit; | |
510 | struct dev_context *devc; | |
511 | ||
512 | if (!sdi) | |
513 | return SR_ERR_ARG; | |
514 | ||
515 | devc = sdi->priv; | |
516 | ||
517 | if (ch->index < 0 || ch->index >= devc->model->num_channels) { | |
518 | sr_err("Channel index %d out of range.", ch->index); | |
519 | return SR_ERR_BUG; | |
520 | } | |
521 | ||
522 | if ((changes & SR_CHANNEL_SET_ENABLED) != 0) { | |
523 | channel_bit = UINT64_C(1) << ch->index; | |
524 | ||
525 | /* Enable or disable logic input for this channel. */ | |
526 | if (ch->enabled) | |
527 | devc->channel_mask |= channel_bit; | |
528 | else | |
529 | devc->channel_mask &= ~channel_bit; | |
530 | } | |
531 | ||
532 | return SR_OK; | |
533 | } | |
534 | ||
535 | /* Derive trigger masks from the session's trigger configuration. | |
536 | */ | |
537 | static int prepare_trigger_masks(const struct sr_dev_inst *sdi) | |
538 | { | |
539 | uint64_t trigger_mask, trigger_values, trigger_edge_mask; | |
540 | uint64_t level_bit, type_bit; | |
541 | struct dev_context *devc; | |
542 | struct sr_trigger *trigger; | |
543 | struct sr_trigger_stage *stage; | |
544 | struct sr_trigger_match *match; | |
545 | const GSList *node; | |
546 | int idx; | |
547 | enum sr_trigger_matches trg; | |
548 | ||
549 | devc = sdi->priv; | |
550 | ||
551 | trigger = sr_session_trigger_get(sdi->session); | |
552 | if (!trigger || !trigger->stages) | |
553 | return SR_OK; | |
554 | ||
555 | if (trigger->stages->next) { | |
556 | sr_err("This device only supports 1 trigger stage."); | |
557 | return SR_ERR_ARG; | |
558 | } | |
559 | stage = trigger->stages->data; | |
560 | ||
561 | trigger_mask = 0; | |
562 | trigger_values = 0; | |
563 | trigger_edge_mask = 0; | |
564 | ||
565 | for (node = stage->matches; node; node = node->next) { | |
566 | match = node->data; | |
567 | ||
568 | if (!match->channel->enabled) | |
569 | continue; /* Ignore disabled channel. */ | |
570 | ||
571 | idx = match->channel->index; | |
572 | trg = match->match; | |
573 | ||
574 | if (idx < 0 || idx >= devc->model->num_channels) { | |
575 | sr_err("Channel index %d out of range.", idx); | |
576 | return SR_ERR_BUG; /* Should not happen. */ | |
577 | } | |
578 | if (trg != SR_TRIGGER_ZERO | |
579 | && trg != SR_TRIGGER_ONE | |
580 | && trg != SR_TRIGGER_RISING | |
581 | && trg != SR_TRIGGER_FALLING) { | |
582 | sr_err("Unsupported trigger match for CH%d.", idx + 1); | |
583 | return SR_ERR_ARG; | |
584 | } | |
585 | level_bit = (trg == SR_TRIGGER_ONE | |
586 | || trg == SR_TRIGGER_RISING) ? 1 : 0; | |
587 | type_bit = (trg == SR_TRIGGER_RISING | |
588 | || trg == SR_TRIGGER_FALLING) ? 1 : 0; | |
589 | ||
590 | trigger_mask |= UINT64_C(1) << idx; | |
591 | trigger_values |= level_bit << idx; | |
592 | trigger_edge_mask |= type_bit << idx; | |
593 | } | |
594 | devc->trigger_mask = trigger_mask; | |
595 | devc->trigger_values = trigger_values; | |
596 | devc->trigger_edge_mask = trigger_edge_mask; | |
597 | ||
598 | return SR_OK; | |
599 | } | |
600 | ||
601 | /* Apply current device configuration to the hardware. | |
602 | */ | |
603 | static int config_commit(const struct sr_dev_inst *sdi) | |
604 | { | |
605 | struct dev_context *devc; | |
606 | int ret; | |
607 | ||
608 | devc = sdi->priv; | |
609 | ||
610 | if (devc->acquisition) { | |
611 | sr_err("Acquisition still in progress?"); | |
612 | return SR_ERR; | |
613 | } | |
614 | ||
615 | ret = prepare_trigger_masks(sdi); | |
616 | if (ret != SR_OK) | |
617 | return ret; | |
618 | ||
619 | ret = (*devc->model->apply_fpga_config)(sdi); | |
620 | if (ret != SR_OK) { | |
621 | sr_err("Failed to apply FPGA configuration."); | |
622 | return ret; | |
623 | } | |
624 | ||
625 | return SR_OK; | |
626 | } | |
627 | ||
628 | static int config_list(uint32_t key, GVariant **data, | |
629 | const struct sr_dev_inst *sdi, | |
630 | const struct sr_channel_group *cg) | |
631 | { | |
632 | struct dev_context *devc; | |
633 | GVariant *gvar; | |
634 | GVariantBuilder gvb; | |
635 | ||
636 | devc = (sdi) ? sdi->priv : NULL; | |
637 | ||
638 | switch (key) { | |
639 | case SR_CONF_SCAN_OPTIONS: | |
640 | case SR_CONF_DEVICE_OPTIONS: | |
641 | return std_opts_config_list(key, data, sdi, cg, | |
642 | scanopts, ARRAY_SIZE(scanopts), | |
643 | drvopts, ARRAY_SIZE(drvopts), | |
644 | (devc) ? devc->model->devopts : NULL, | |
645 | (devc) ? devc->model->num_devopts : 0); | |
646 | } | |
647 | ||
648 | if (!has_devopt(devc->model, key | SR_CONF_LIST)) | |
649 | return SR_ERR_NA; | |
650 | ||
651 | switch (key) { | |
652 | case SR_CONF_SAMPLERATE: | |
653 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_VARDICT); | |
654 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64, | |
655 | devc->model->samplerates, devc->model->num_samplerates, | |
656 | sizeof(devc->model->samplerates[0])); | |
657 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
658 | *data = g_variant_builder_end(&gvb); | |
659 | break; | |
660 | case SR_CONF_TRIGGER_MATCH: | |
661 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
662 | trigger_matches, ARRAY_SIZE(trigger_matches), | |
663 | sizeof(trigger_matches[0])); | |
664 | break; | |
665 | case SR_CONF_TRIGGER_SOURCE: | |
666 | *data = g_variant_new_strv(trigger_source_names, | |
667 | ARRAY_SIZE(trigger_source_names)); | |
668 | break; | |
669 | case SR_CONF_TRIGGER_SLOPE: | |
670 | case SR_CONF_CLOCK_EDGE: | |
671 | *data = g_variant_new_strv(signal_edge_names, | |
672 | ARRAY_SIZE(signal_edge_names)); | |
673 | break; | |
674 | default: | |
675 | /* Must not happen for a key listed in devopts. */ | |
676 | return SR_ERR_BUG; | |
677 | } | |
678 | ||
679 | return SR_OK; | |
680 | } | |
681 | ||
682 | /* Set up the device hardware to begin capturing samples as soon as the | |
683 | * configured trigger conditions are met, or immediately if no triggers | |
684 | * are configured. | |
685 | */ | |
686 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
687 | { | |
688 | return lwla_start_acquisition(sdi); | |
689 | } | |
690 | ||
691 | /* Request that a running capture operation be stopped. | |
692 | */ | |
693 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
694 | { | |
695 | struct dev_context *devc; | |
696 | ||
697 | devc = sdi->priv; | |
698 | ||
699 | if (devc->state != STATE_IDLE && !devc->cancel_requested) { | |
700 | devc->cancel_requested = TRUE; | |
701 | sr_dbg("Requesting cancel."); | |
702 | } | |
703 | ||
704 | return SR_OK; | |
705 | } | |
706 | ||
707 | /* SysClk LWLA driver descriptor. | |
708 | */ | |
709 | static struct sr_dev_driver sysclk_lwla_driver_info = { | |
710 | .name = "sysclk-lwla", | |
711 | .longname = "SysClk LWLA series", | |
712 | .api_version = 1, | |
713 | .init = std_init, | |
714 | .cleanup = std_cleanup, | |
715 | .scan = scan, | |
716 | .dev_list = std_dev_list, | |
717 | .dev_clear = std_dev_clear, | |
718 | .config_get = config_get, | |
719 | .config_set = config_set, | |
720 | .config_channel_set = config_channel_set, | |
721 | .config_commit = config_commit, | |
722 | .config_list = config_list, | |
723 | .dev_open = dev_open, | |
724 | .dev_close = dev_close, | |
725 | .dev_acquisition_start = dev_acquisition_start, | |
726 | .dev_acquisition_stop = dev_acquisition_stop, | |
727 | .context = NULL, | |
728 | }; | |
729 | SR_REGISTER_DEV_DRIVER(sysclk_lwla_driver_info); |