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