2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
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.
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.
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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
29 /* Supported device scan options.
31 static const uint32_t scanopts[] = {
35 /* Driver capabilities.
37 static const uint32_t drvopts[] = {
38 SR_CONF_LOGIC_ANALYZER,
41 /* Supported trigger match conditions.
43 static const int32_t trigger_matches[] = {
50 /* Names assigned to available trigger sources.
52 static const char *const trigger_source_names[] = {
53 [TRIGGER_CHANNELS] = "CH",
54 [TRIGGER_EXT_TRG] = "TRG",
57 /* Names assigned to available edge slope choices.
59 static const char *const signal_edge_names[] = {
60 [EDGE_POSITIVE] = "r",
61 [EDGE_NEGATIVE] = "f",
64 /* Initialize the SysClk LWLA driver.
66 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
68 return std_init(sr_ctx, di, LOG_PREFIX);
71 /* Create a new sigrok device instance for the indicated LWLA model.
73 static struct sr_dev_inst *dev_inst_new(const struct model_info *model)
75 struct sr_dev_inst *sdi;
76 struct dev_context *devc;
80 /* Initialize private device context. */
81 devc = g_malloc0(sizeof(struct dev_context));
83 devc->active_fpga_config = FPGA_NOCONF;
85 devc->samplerate = model->samplerates[0];
86 devc->channel_mask = (UINT64_C(1) << model->num_channels) - 1;
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);
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);
105 /* Create a new device instance for a libusb device if it is a SysClk LWLA
106 * device and also matches the connection specification.
108 static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches,
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;
117 unsigned int vid, pid;
120 bus = libusb_get_bus_number(dev);
121 address = libusb_get_device_address(dev);
123 for (node = conn_matches; node != NULL; node = node->next) {
125 if (usb && usb->bus == bus && usb->address == address)
128 if (conn_matches && !node)
129 return NULL; /* no match */
131 ret = libusb_get_device_descriptor(dev, &des);
133 sr_err("Failed to get USB device descriptor: %s.",
134 libusb_error_name(ret));
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;
147 sr_warn("USB device %d.%d (%04x:%04x) is not a"
148 " SysClk LWLA.", bus, address, vid, pid);
151 sdi = dev_inst_new(model);
153 sdi->inst_type = SR_INST_USB;
154 sdi->conn = sr_usb_dev_inst_new(bus, address, NULL);
159 /* Scan for SysClk LWLA devices and create a device instance for each one.
161 static GSList *scan(struct sr_dev_driver *di, GSList *options)
163 GSList *conn_devices, *devices, *node;
164 struct drv_context *drvc;
165 struct sr_dev_inst *sdi;
166 struct sr_config *src;
168 libusb_device **devlist;
176 for (node = options; node != NULL; node = node->next) {
178 if (src->key == SR_CONF_CONN) {
179 conn = g_variant_get_string(src->data, NULL);
184 /* Find devices matching the connection specification. */
185 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
188 /* List all libusb devices. */
189 num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
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);
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]);
202 continue; /* no match */
204 /* Register device instance with driver. */
206 drvc->instances = g_slist_append(drvc->instances, sdi);
207 devices = g_slist_append(devices, sdi);
210 libusb_free_device_list(devlist, 1);
211 g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free);
216 /* Return the list of devices found during scan.
218 static GSList *dev_list(const struct sr_dev_driver *di)
220 struct drv_context *drvc;
224 return drvc->instances;
227 /* Destroy the private device context.
229 static void clear_dev_context(void *priv)
231 struct dev_context *devc;
235 if (devc->acquisition) {
236 sr_err("Cannot clear device context during acquisition!");
237 return; /* leak and pray */
239 sr_dbg("Device context cleared.");
244 /* Destroy all device instances.
246 static int dev_clear(const struct sr_dev_driver *di)
248 return std_dev_clear(di, &clear_dev_context);
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.
255 static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint)
257 int drained, xfer_len;
259 unsigned char buf[512];
261 const unsigned int drain_timeout_ms = 10;
266 ret = libusb_bulk_transfer(usb->devhdl, endpoint,
267 buf, sizeof(buf), &xfer_len,
270 } while (ret == LIBUSB_SUCCESS && xfer_len != 0);
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));
279 sr_warn("Drained %d bytes from USB endpoint %u.",
280 drained, endpoint & (LIBUSB_ENDPOINT_IN - 1));
285 /* Open and initialize device.
287 static int dev_open(struct sr_dev_inst *sdi)
289 struct drv_context *drvc;
290 struct dev_context *devc;
291 struct sr_usb_dev_inst *usb;
294 drvc = sdi->driver->context;
299 sr_err("Driver was not initialized.");
302 if (sdi->status != SR_ST_INACTIVE) {
303 sr_err("Device already open.");
307 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
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));
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));
327 ret = drain_usb(usb, EP_REPLY);
331 sdi->status = SR_ST_ACTIVE;
333 devc->active_fpga_config = FPGA_NOCONF;
334 devc->state = STATE_IDLE;
336 ret = (*devc->model->apply_fpga_config)(sdi);
339 ret = (*devc->model->device_init_check)(sdi);
342 sdi->status = SR_ST_INACTIVE;
348 /* Shutdown and close device.
350 static int dev_close(struct sr_dev_inst *sdi)
352 struct drv_context *drvc;
353 struct dev_context *devc;
354 struct sr_usb_dev_inst *usb;
357 drvc = sdi->driver->context;
362 sr_err("Driver was not initialized.");
365 if (sdi->status == SR_ST_INACTIVE) {
366 sr_dbg("Device already closed.");
369 if (devc->acquisition) {
370 sr_err("Cannot close device during acquisition!");
371 /* Request stop, leak handle, and prepare for the worst. */
372 devc->cancel_requested = TRUE;
376 sdi->status = SR_ST_INACTIVE;
378 /* Download of the shutdown bitstream, if any. */
379 ret = (*devc->model->apply_fpga_config)(sdi);
381 sr_warn("Unable to shut down device.");
383 libusb_release_interface(usb->devhdl, USB_INTERFACE);
389 /* Check whether the device options contain a specific key.
390 * Also match against get/set/list bits if specified.
392 static int has_devopt(const struct model_info *model, uint32_t key)
396 for (i = 0; i < model->num_devopts; i++) {
397 if ((model->devopts[i] & (SR_CONF_MASK | key)) == key)
403 /* Read device configuration setting.
405 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
406 const struct sr_channel_group *cg)
408 struct dev_context *devc;
418 if (!has_devopt(devc->model, key | SR_CONF_GET))
422 case SR_CONF_SAMPLERATE:
423 *data = g_variant_new_uint64(devc->samplerate);
425 case SR_CONF_LIMIT_MSEC:
426 *data = g_variant_new_uint64(devc->limit_msec);
428 case SR_CONF_LIMIT_SAMPLES:
429 *data = g_variant_new_uint64(devc->limit_samples);
432 *data = g_variant_new_boolean(devc->cfg_rle);
434 case SR_CONF_EXTERNAL_CLOCK:
435 *data = g_variant_new_boolean(devc->cfg_clock_source
438 case SR_CONF_CLOCK_EDGE:
439 idx = devc->cfg_clock_edge;
440 if (idx >= ARRAY_SIZE(signal_edge_names))
442 *data = g_variant_new_string(signal_edge_names[idx]);
444 case SR_CONF_TRIGGER_SOURCE:
445 idx = devc->cfg_trigger_source;
446 if (idx >= ARRAY_SIZE(trigger_source_names))
448 *data = g_variant_new_string(trigger_source_names[idx]);
450 case SR_CONF_TRIGGER_SLOPE:
451 idx = devc->cfg_trigger_slope;
452 if (idx >= ARRAY_SIZE(signal_edge_names))
454 *data = g_variant_new_string(signal_edge_names[idx]);
457 /* Must not happen for a key listed in devopts. */
464 /* Helper for mapping a string-typed configuration value to an index
465 * within a table of possible values.
467 static int lookup_index(GVariant *value, const char *const *table, int len)
472 entry = g_variant_get_string(value, NULL);
476 /* Linear search is fine for very small tables. */
477 for (i = 0; i < len; ++i) {
478 if (strcmp(entry, table[i]) == 0)
484 /* Write device configuration setting.
486 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
487 const struct sr_channel_group *cg)
490 struct dev_context *devc;
500 if (!has_devopt(devc->model, key | SR_CONF_SET))
504 case SR_CONF_SAMPLERATE:
505 value = g_variant_get_uint64(data);
506 if (value < devc->model->samplerates[devc->model->num_samplerates - 1]
507 || value > devc->model->samplerates[0])
508 return SR_ERR_SAMPLERATE;
509 devc->samplerate = value;
511 case SR_CONF_LIMIT_MSEC:
512 value = g_variant_get_uint64(data);
513 if (value > MAX_LIMIT_MSEC)
515 devc->limit_msec = value;
517 case SR_CONF_LIMIT_SAMPLES:
518 value = g_variant_get_uint64(data);
519 if (value > MAX_LIMIT_SAMPLES)
521 devc->limit_samples = value;
524 devc->cfg_rle = g_variant_get_boolean(data);
526 case SR_CONF_EXTERNAL_CLOCK:
527 devc->cfg_clock_source = (g_variant_get_boolean(data))
528 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
530 case SR_CONF_CLOCK_EDGE:
531 idx = lookup_index(data, signal_edge_names,
532 ARRAY_SIZE(signal_edge_names));
535 devc->cfg_clock_edge = idx;
537 case SR_CONF_TRIGGER_SOURCE:
538 idx = lookup_index(data, trigger_source_names,
539 ARRAY_SIZE(trigger_source_names));
542 devc->cfg_trigger_source = idx;
544 case SR_CONF_TRIGGER_SLOPE:
545 idx = lookup_index(data, signal_edge_names,
546 ARRAY_SIZE(signal_edge_names));
549 devc->cfg_trigger_slope = idx;
552 /* Must not happen for a key listed in devopts. */
559 /* Apply channel configuration change.
561 static int config_channel_set(const struct sr_dev_inst *sdi,
562 struct sr_channel *ch, unsigned int changes)
564 uint64_t channel_bit;
565 struct dev_context *devc;
572 if (ch->index < 0 || ch->index >= devc->model->num_channels) {
573 sr_err("Channel index %d out of range.", ch->index);
577 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
578 channel_bit = UINT64_C(1) << ch->index;
580 /* Enable or disable logic input for this channel. */
582 devc->channel_mask |= channel_bit;
584 devc->channel_mask &= ~channel_bit;
590 /* Derive trigger masks from the session's trigger configuration.
592 static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
594 uint64_t trigger_mask;
595 uint64_t trigger_values;
596 uint64_t trigger_edge_mask;
597 uint64_t level_bit, type_bit;
598 struct dev_context *devc;
599 struct sr_trigger *trigger;
600 struct sr_trigger_stage *stage;
601 struct sr_trigger_match *match;
604 enum sr_trigger_matches trg;
608 trigger = sr_session_trigger_get(sdi->session);
609 if (!trigger || !trigger->stages)
612 if (trigger->stages->next) {
613 sr_err("This device only supports 1 trigger stage.");
616 stage = trigger->stages->data;
620 trigger_edge_mask = 0;
622 for (node = stage->matches; node; node = node->next) {
625 if (!match->channel->enabled)
626 continue; /* ignore disabled channel */
628 idx = match->channel->index;
631 if (idx < 0 || idx >= devc->model->num_channels) {
632 sr_err("Channel index %d out of range.", idx);
633 return SR_ERR_BUG; /* should not happen */
635 if (trg != SR_TRIGGER_ZERO
636 && trg != SR_TRIGGER_ONE
637 && trg != SR_TRIGGER_RISING
638 && trg != SR_TRIGGER_FALLING) {
639 sr_err("Unsupported trigger match for CH%d.", idx + 1);
642 level_bit = (trg == SR_TRIGGER_ONE
643 || trg == SR_TRIGGER_RISING) ? 1 : 0;
644 type_bit = (trg == SR_TRIGGER_RISING
645 || trg == SR_TRIGGER_FALLING) ? 1 : 0;
647 trigger_mask |= UINT64_C(1) << idx;
648 trigger_values |= level_bit << idx;
649 trigger_edge_mask |= type_bit << idx;
651 devc->trigger_mask = trigger_mask;
652 devc->trigger_values = trigger_values;
653 devc->trigger_edge_mask = trigger_edge_mask;
658 /* Apply current device configuration to the hardware.
660 static int config_commit(const struct sr_dev_inst *sdi)
662 struct dev_context *devc;
667 if (sdi->status != SR_ST_ACTIVE)
668 return SR_ERR_DEV_CLOSED;
670 if (devc->acquisition) {
671 sr_err("Acquisition still in progress?");
675 ret = prepare_trigger_masks(sdi);
679 ret = (*devc->model->apply_fpga_config)(sdi);
681 sr_err("Failed to apply FPGA configuration.");
688 /* List available choices for a configuration setting.
690 static int config_list(uint32_t key, GVariant **data,
691 const struct sr_dev_inst *sdi,
692 const struct sr_channel_group *cg)
694 struct dev_context *devc;
700 if (key == SR_CONF_SCAN_OPTIONS) {
701 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
702 scanopts, ARRAY_SIZE(scanopts),
703 sizeof(scanopts[0]));
707 if (key != SR_CONF_DEVICE_OPTIONS)
710 /* List driver capabilities. */
711 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
712 drvopts, ARRAY_SIZE(drvopts),
719 /* List the model's device options. */
720 if (key == SR_CONF_DEVICE_OPTIONS) {
721 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
722 devc->model->devopts,
723 devc->model->num_devopts,
724 sizeof(devc->model->devopts[0]));
728 if (!has_devopt(devc->model, key | SR_CONF_LIST))
732 case SR_CONF_SAMPLERATE:
733 g_variant_builder_init(&gvb, G_VARIANT_TYPE_VARDICT);
734 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
735 devc->model->samplerates,
736 devc->model->num_samplerates,
737 sizeof(devc->model->samplerates[0]));
738 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
739 *data = g_variant_builder_end(&gvb);
741 case SR_CONF_TRIGGER_MATCH:
742 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
744 ARRAY_SIZE(trigger_matches),
745 sizeof(trigger_matches[0]));
747 case SR_CONF_TRIGGER_SOURCE:
748 *data = g_variant_new_strv(trigger_source_names,
749 ARRAY_SIZE(trigger_source_names));
751 case SR_CONF_TRIGGER_SLOPE:
752 case SR_CONF_CLOCK_EDGE:
753 *data = g_variant_new_strv(signal_edge_names,
754 ARRAY_SIZE(signal_edge_names));
757 /* Must not happen for a key listed in devopts. */
764 /* Set up the device hardware to begin capturing samples as soon as the
765 * configured trigger conditions are met, or immediately if no triggers
768 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
772 if (sdi->status != SR_ST_ACTIVE)
773 return SR_ERR_DEV_CLOSED;
775 sr_info("Starting acquisition.");
777 return lwla_start_acquisition(sdi);
780 /* Request that a running capture operation be stopped.
782 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
784 struct dev_context *devc;
789 if (sdi->status != SR_ST_ACTIVE)
790 return SR_ERR_DEV_CLOSED;
792 if (devc->state != STATE_IDLE && !devc->cancel_requested) {
793 devc->cancel_requested = TRUE;
794 sr_dbg("Stopping acquisition.");
799 /* SysClk LWLA driver descriptor.
801 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
802 .name = "sysclk-lwla",
803 .longname = "SysClk LWLA series",
806 .cleanup = dev_clear,
808 .dev_list = dev_list,
809 .dev_clear = dev_clear,
810 .config_get = config_get,
811 .config_set = config_set,
812 .config_channel_set = config_channel_set,
813 .config_commit = config_commit,
814 .config_list = config_list,
815 .dev_open = dev_open,
816 .dev_close = dev_close,
817 .dev_acquisition_start = dev_acquisition_start,
818 .dev_acquisition_stop = dev_acquisition_stop,