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(di, sr_ctx, 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;
116 int bus, address, ret;
117 unsigned int vid, pid;
119 bus = libusb_get_bus_number(dev);
120 address = libusb_get_device_address(dev);
122 for (node = conn_matches; node != NULL; node = node->next) {
124 if (usb && usb->bus == bus && usb->address == address)
127 if (conn_matches && !node)
128 return NULL; /* no match */
130 ret = libusb_get_device_descriptor(dev, &des);
132 sr_err("Failed to get USB device descriptor: %s.",
133 libusb_error_name(ret));
139 /* Create sigrok device instance. */
140 if (vid == USB_VID_SYSCLK && pid == USB_PID_LWLA1016) {
141 model = &lwla1016_info;
142 } else if (vid == USB_VID_SYSCLK && pid == USB_PID_LWLA1034) {
143 model = &lwla1034_info;
146 sr_warn("USB device %d.%d (%04x:%04x) is not a"
147 " SysClk LWLA.", bus, address, vid, pid);
150 sdi = dev_inst_new(model);
152 sdi->inst_type = SR_INST_USB;
153 sdi->conn = sr_usb_dev_inst_new(bus, address, NULL);
158 /* Scan for SysClk LWLA devices and create a device instance for each one.
160 static GSList *scan(struct sr_dev_driver *di, GSList *options)
162 GSList *conn_devices, *devices, *node;
163 struct drv_context *drvc;
164 struct sr_dev_inst *sdi;
165 struct sr_config *src;
167 libusb_device **devlist;
175 for (node = options; node != NULL; node = node->next) {
177 if (src->key == SR_CONF_CONN) {
178 conn = g_variant_get_string(src->data, NULL);
183 /* Find devices matching the connection specification. */
184 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
187 /* List all libusb devices. */
188 num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
190 sr_err("Failed to list USB devices: %s.",
191 libusb_error_name(num_devs));
192 g_slist_free_full(conn_devices,
193 (GDestroyNotify)&sr_usb_dev_inst_free);
197 /* Scan the USB device list for matching LWLA devices. */
198 for (i = 0; i < num_devs; i++) {
199 sdi = dev_inst_new_matching(conn_devices, devlist[i]);
201 continue; /* no match */
203 /* Register device instance with driver. */
205 drvc->instances = g_slist_append(drvc->instances, sdi);
206 devices = g_slist_append(devices, sdi);
209 libusb_free_device_list(devlist, 1);
210 g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free);
215 /* Destroy the private device context.
217 static void clear_dev_context(void *priv)
219 struct dev_context *devc;
223 if (devc->acquisition) {
224 sr_err("Cannot clear device context during acquisition!");
225 return; /* Leak and pray. */
227 sr_dbg("Device context cleared.");
232 /* Destroy all device instances.
234 static int dev_clear(const struct sr_dev_driver *di)
236 return std_dev_clear(di, &clear_dev_context);
239 /* Drain any pending data from the USB transfer buffers on the device.
240 * This may be necessary e.g. after a crash or generally to clean up after
241 * an abnormal condition.
243 static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint)
245 int drained, xfer_len, ret;
246 unsigned char buf[512];
247 const unsigned int drain_timeout_ms = 10;
252 ret = libusb_bulk_transfer(usb->devhdl, endpoint,
253 buf, sizeof(buf), &xfer_len,
256 } while (ret == LIBUSB_SUCCESS && xfer_len != 0);
258 if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) {
259 sr_err("Failed to drain USB endpoint %u: %s.",
260 endpoint & (LIBUSB_ENDPOINT_IN - 1),
261 libusb_error_name(ret));
265 sr_warn("Drained %d bytes from USB endpoint %u.",
266 drained, endpoint & (LIBUSB_ENDPOINT_IN - 1));
272 /* Open and initialize device.
274 static int dev_open(struct sr_dev_inst *sdi)
276 struct drv_context *drvc;
277 struct dev_context *devc;
278 struct sr_usb_dev_inst *usb;
281 drvc = sdi->driver->context;
285 if (sdi->status != SR_ST_INACTIVE) {
286 sr_err("Device already open.");
290 /* Try the whole shebang three times, fingers crossed. */
291 for (i = 0; i < 3; i++) {
292 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
296 ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
297 if (ret != LIBUSB_SUCCESS) {
298 sr_err("Failed to set USB configuration: %s.",
299 libusb_error_name(ret));
304 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
305 if (ret != LIBUSB_SUCCESS) {
306 sr_err("Failed to claim interface: %s.",
307 libusb_error_name(ret));
312 ret = drain_usb(usb, EP_REPLY);
317 /* This delay appears to be necessary for reliable operation. */
320 sdi->status = SR_ST_ACTIVE;
322 devc->active_fpga_config = FPGA_NOCONF;
323 devc->short_transfer_quirk = FALSE;
324 devc->state = STATE_IDLE;
326 ret = (*devc->model->apply_fpga_config)(sdi);
329 ret = (*devc->model->device_init_check)(sdi);
333 /* Rinse and repeat. */
334 sdi->status = SR_ST_INACTIVE;
338 if (ret == SR_OK && devc->short_transfer_quirk)
339 sr_warn("Short transfer quirk detected! "
340 "Memory reads will be slow.");
344 /* Shutdown and close device.
346 static int dev_close(struct sr_dev_inst *sdi)
348 struct dev_context *devc;
349 struct sr_usb_dev_inst *usb;
355 if (sdi->status == SR_ST_INACTIVE) {
356 sr_dbg("Device already closed.");
359 if (devc->acquisition) {
360 sr_err("Cannot close device during acquisition!");
361 /* Request stop, leak handle, and prepare for the worst. */
362 devc->cancel_requested = TRUE;
366 sdi->status = SR_ST_INACTIVE;
368 /* Download of the shutdown bitstream, if any. */
369 ret = (*devc->model->apply_fpga_config)(sdi);
371 sr_warn("Unable to shut down device.");
373 libusb_release_interface(usb->devhdl, USB_INTERFACE);
379 /* Check whether the device options contain a specific key.
380 * Also match against get/set/list bits if specified.
382 static int has_devopt(const struct model_info *model, uint32_t key)
386 for (i = 0; i < model->num_devopts; i++) {
387 if ((model->devopts[i] & (SR_CONF_MASK | key)) == key)
394 /* Read device configuration setting.
396 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
397 const struct sr_channel_group *cg)
399 struct dev_context *devc;
409 if (!has_devopt(devc->model, key | SR_CONF_GET))
413 case SR_CONF_SAMPLERATE:
414 *data = g_variant_new_uint64(devc->samplerate);
416 case SR_CONF_LIMIT_MSEC:
417 *data = g_variant_new_uint64(devc->limit_msec);
419 case SR_CONF_LIMIT_SAMPLES:
420 *data = g_variant_new_uint64(devc->limit_samples);
423 *data = g_variant_new_boolean(devc->cfg_rle);
425 case SR_CONF_EXTERNAL_CLOCK:
426 *data = g_variant_new_boolean(devc->cfg_clock_source
429 case SR_CONF_CLOCK_EDGE:
430 idx = devc->cfg_clock_edge;
431 if (idx >= ARRAY_SIZE(signal_edge_names))
433 *data = g_variant_new_string(signal_edge_names[idx]);
435 case SR_CONF_TRIGGER_SOURCE:
436 idx = devc->cfg_trigger_source;
437 if (idx >= ARRAY_SIZE(trigger_source_names))
439 *data = g_variant_new_string(trigger_source_names[idx]);
441 case SR_CONF_TRIGGER_SLOPE:
442 idx = devc->cfg_trigger_slope;
443 if (idx >= ARRAY_SIZE(signal_edge_names))
445 *data = g_variant_new_string(signal_edge_names[idx]);
448 /* Must not happen for a key listed in devopts. */
455 /* Helper for mapping a string-typed configuration value to an index
456 * within a table of possible values.
458 static int lookup_index(GVariant *value, const char *const *table, int len)
463 entry = g_variant_get_string(value, NULL);
467 /* Linear search is fine for very small tables. */
468 for (i = 0; i < len; i++) {
469 if (strcmp(entry, table[i]) == 0)
476 /* Write device configuration setting.
478 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
479 const struct sr_channel_group *cg)
482 struct dev_context *devc;
492 if (!has_devopt(devc->model, key | SR_CONF_SET))
496 case SR_CONF_SAMPLERATE:
497 value = g_variant_get_uint64(data);
498 if (value < devc->model->samplerates[devc->model->num_samplerates - 1]
499 || value > devc->model->samplerates[0])
500 return SR_ERR_SAMPLERATE;
501 devc->samplerate = value;
503 case SR_CONF_LIMIT_MSEC:
504 value = g_variant_get_uint64(data);
505 if (value > MAX_LIMIT_MSEC)
507 devc->limit_msec = value;
509 case SR_CONF_LIMIT_SAMPLES:
510 value = g_variant_get_uint64(data);
511 if (value > MAX_LIMIT_SAMPLES)
513 devc->limit_samples = value;
516 devc->cfg_rle = g_variant_get_boolean(data);
518 case SR_CONF_EXTERNAL_CLOCK:
519 devc->cfg_clock_source = (g_variant_get_boolean(data))
520 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
522 case SR_CONF_CLOCK_EDGE:
523 idx = lookup_index(data, signal_edge_names,
524 ARRAY_SIZE(signal_edge_names));
527 devc->cfg_clock_edge = idx;
529 case SR_CONF_TRIGGER_SOURCE:
530 idx = lookup_index(data, trigger_source_names,
531 ARRAY_SIZE(trigger_source_names));
534 devc->cfg_trigger_source = idx;
536 case SR_CONF_TRIGGER_SLOPE:
537 idx = lookup_index(data, signal_edge_names,
538 ARRAY_SIZE(signal_edge_names));
541 devc->cfg_trigger_slope = idx;
544 /* Must not happen for a key listed in devopts. */
551 /* Apply channel configuration change.
553 static int config_channel_set(const struct sr_dev_inst *sdi,
554 struct sr_channel *ch, unsigned int changes)
556 uint64_t channel_bit;
557 struct dev_context *devc;
564 if (ch->index < 0 || ch->index >= devc->model->num_channels) {
565 sr_err("Channel index %d out of range.", ch->index);
569 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
570 channel_bit = UINT64_C(1) << ch->index;
572 /* Enable or disable logic input for this channel. */
574 devc->channel_mask |= channel_bit;
576 devc->channel_mask &= ~channel_bit;
582 /* Derive trigger masks from the session's trigger configuration.
584 static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
586 uint64_t trigger_mask, trigger_values, trigger_edge_mask;
587 uint64_t level_bit, type_bit;
588 struct dev_context *devc;
589 struct sr_trigger *trigger;
590 struct sr_trigger_stage *stage;
591 struct sr_trigger_match *match;
594 enum sr_trigger_matches trg;
598 trigger = sr_session_trigger_get(sdi->session);
599 if (!trigger || !trigger->stages)
602 if (trigger->stages->next) {
603 sr_err("This device only supports 1 trigger stage.");
606 stage = trigger->stages->data;
610 trigger_edge_mask = 0;
612 for (node = stage->matches; node; node = node->next) {
615 if (!match->channel->enabled)
616 continue; /* Ignore disabled channel. */
618 idx = match->channel->index;
621 if (idx < 0 || idx >= devc->model->num_channels) {
622 sr_err("Channel index %d out of range.", idx);
623 return SR_ERR_BUG; /* Should not happen. */
625 if (trg != SR_TRIGGER_ZERO
626 && trg != SR_TRIGGER_ONE
627 && trg != SR_TRIGGER_RISING
628 && trg != SR_TRIGGER_FALLING) {
629 sr_err("Unsupported trigger match for CH%d.", idx + 1);
632 level_bit = (trg == SR_TRIGGER_ONE
633 || trg == SR_TRIGGER_RISING) ? 1 : 0;
634 type_bit = (trg == SR_TRIGGER_RISING
635 || trg == SR_TRIGGER_FALLING) ? 1 : 0;
637 trigger_mask |= UINT64_C(1) << idx;
638 trigger_values |= level_bit << idx;
639 trigger_edge_mask |= type_bit << idx;
641 devc->trigger_mask = trigger_mask;
642 devc->trigger_values = trigger_values;
643 devc->trigger_edge_mask = trigger_edge_mask;
648 /* Apply current device configuration to the hardware.
650 static int config_commit(const struct sr_dev_inst *sdi)
652 struct dev_context *devc;
657 if (sdi->status != SR_ST_ACTIVE)
658 return SR_ERR_DEV_CLOSED;
660 if (devc->acquisition) {
661 sr_err("Acquisition still in progress?");
665 ret = prepare_trigger_masks(sdi);
669 ret = (*devc->model->apply_fpga_config)(sdi);
671 sr_err("Failed to apply FPGA configuration.");
678 /* List available choices for a configuration setting.
680 static int config_list(uint32_t key, GVariant **data,
681 const struct sr_dev_inst *sdi,
682 const struct sr_channel_group *cg)
684 struct dev_context *devc;
690 if (key == SR_CONF_SCAN_OPTIONS) {
691 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
692 scanopts, ARRAY_SIZE(scanopts), sizeof(scanopts[0]));
696 if (key != SR_CONF_DEVICE_OPTIONS)
699 /* List driver capabilities. */
700 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
701 drvopts, ARRAY_SIZE(drvopts), sizeof(drvopts[0]));
707 /* List the model's device options. */
708 if (key == SR_CONF_DEVICE_OPTIONS) {
709 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
710 devc->model->devopts, devc->model->num_devopts,
711 sizeof(devc->model->devopts[0]));
715 if (!has_devopt(devc->model, key | SR_CONF_LIST))
719 case SR_CONF_SAMPLERATE:
720 g_variant_builder_init(&gvb, G_VARIANT_TYPE_VARDICT);
721 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
722 devc->model->samplerates, devc->model->num_samplerates,
723 sizeof(devc->model->samplerates[0]));
724 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
725 *data = g_variant_builder_end(&gvb);
727 case SR_CONF_TRIGGER_MATCH:
728 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
729 trigger_matches, ARRAY_SIZE(trigger_matches),
730 sizeof(trigger_matches[0]));
732 case SR_CONF_TRIGGER_SOURCE:
733 *data = g_variant_new_strv(trigger_source_names,
734 ARRAY_SIZE(trigger_source_names));
736 case SR_CONF_TRIGGER_SLOPE:
737 case SR_CONF_CLOCK_EDGE:
738 *data = g_variant_new_strv(signal_edge_names,
739 ARRAY_SIZE(signal_edge_names));
742 /* Must not happen for a key listed in devopts. */
749 /* Set up the device hardware to begin capturing samples as soon as the
750 * configured trigger conditions are met, or immediately if no triggers
753 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
755 if (sdi->status != SR_ST_ACTIVE)
756 return SR_ERR_DEV_CLOSED;
758 sr_info("Starting acquisition.");
760 return lwla_start_acquisition(sdi);
763 /* Request that a running capture operation be stopped.
765 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
767 struct dev_context *devc;
771 if (sdi->status != SR_ST_ACTIVE)
772 return SR_ERR_DEV_CLOSED;
774 if (devc->state != STATE_IDLE && !devc->cancel_requested) {
775 devc->cancel_requested = TRUE;
776 sr_dbg("Stopping acquisition.");
782 /* SysClk LWLA driver descriptor.
784 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
785 .name = "sysclk-lwla",
786 .longname = "SysClk LWLA series",
789 .cleanup = std_cleanup,
791 .dev_list = std_dev_list,
792 .dev_clear = dev_clear,
793 .config_get = config_get,
794 .config_set = config_set,
795 .config_channel_set = config_channel_set,
796 .config_commit = config_commit,
797 .config_list = config_list,
798 .dev_open = dev_open,
799 .dev_close = dev_close,
800 .dev_acquisition_start = dev_acquisition_start,
801 .dev_acquisition_stop = dev_acquisition_stop,