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;
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 /* Return the list of devices found during scan.
217 static GSList *dev_list(const struct sr_dev_driver *di)
219 return ((struct drv_context *)(di->context))->instances;
222 /* Destroy the private device context.
224 static void clear_dev_context(void *priv)
226 struct dev_context *devc;
230 if (devc->acquisition) {
231 sr_err("Cannot clear device context during acquisition!");
232 return; /* Leak and pray. */
234 sr_dbg("Device context cleared.");
239 /* Destroy all device instances.
241 static int dev_clear(const struct sr_dev_driver *di)
243 return std_dev_clear(di, &clear_dev_context);
246 /* Drain any pending data from the USB transfer buffers on the device.
247 * This may be necessary e.g. after a crash or generally to clean up after
248 * an abnormal condition.
250 static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint)
252 int drained, xfer_len, ret;
253 unsigned char buf[512];
254 const unsigned int drain_timeout_ms = 10;
259 ret = libusb_bulk_transfer(usb->devhdl, endpoint,
260 buf, sizeof(buf), &xfer_len,
263 } while (ret == LIBUSB_SUCCESS && xfer_len != 0);
265 if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) {
266 sr_err("Failed to drain USB endpoint %u: %s.",
267 endpoint & (LIBUSB_ENDPOINT_IN - 1),
268 libusb_error_name(ret));
272 sr_warn("Drained %d bytes from USB endpoint %u.",
273 drained, endpoint & (LIBUSB_ENDPOINT_IN - 1));
279 /* Open and initialize device.
281 static int dev_open(struct sr_dev_inst *sdi)
283 struct drv_context *drvc;
284 struct dev_context *devc;
285 struct sr_usb_dev_inst *usb;
288 drvc = sdi->driver->context;
293 sr_err("Driver was not initialized.");
296 if (sdi->status != SR_ST_INACTIVE) {
297 sr_err("Device already open.");
301 /* Try the whole shebang three times, fingers crossed. */
302 for (i = 0; i < 3; i++) {
303 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
307 ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
308 if (ret != LIBUSB_SUCCESS) {
309 sr_err("Failed to set USB configuration: %s.",
310 libusb_error_name(ret));
315 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
316 if (ret != LIBUSB_SUCCESS) {
317 sr_err("Failed to claim interface: %s.",
318 libusb_error_name(ret));
323 ret = drain_usb(usb, EP_REPLY);
328 /* This delay appears to be necessary for reliable operation. */
331 sdi->status = SR_ST_ACTIVE;
333 devc->active_fpga_config = FPGA_NOCONF;
334 devc->short_transfer_quirk = FALSE;
335 devc->state = STATE_IDLE;
337 ret = (*devc->model->apply_fpga_config)(sdi);
340 ret = (*devc->model->device_init_check)(sdi);
344 /* Rinse and repeat. */
345 sdi->status = SR_ST_INACTIVE;
349 if (ret == SR_OK && devc->short_transfer_quirk)
350 sr_warn("Short transfer quirk detected! "
351 "Memory reads will be slow.");
355 /* Shutdown and close device.
357 static int dev_close(struct sr_dev_inst *sdi)
359 struct drv_context *drvc;
360 struct dev_context *devc;
361 struct sr_usb_dev_inst *usb;
364 drvc = sdi->driver->context;
369 sr_err("Driver was not initialized.");
372 if (sdi->status == SR_ST_INACTIVE) {
373 sr_dbg("Device already closed.");
376 if (devc->acquisition) {
377 sr_err("Cannot close device during acquisition!");
378 /* Request stop, leak handle, and prepare for the worst. */
379 devc->cancel_requested = TRUE;
383 sdi->status = SR_ST_INACTIVE;
385 /* Download of the shutdown bitstream, if any. */
386 ret = (*devc->model->apply_fpga_config)(sdi);
388 sr_warn("Unable to shut down device.");
390 libusb_release_interface(usb->devhdl, USB_INTERFACE);
396 /* Check whether the device options contain a specific key.
397 * Also match against get/set/list bits if specified.
399 static int has_devopt(const struct model_info *model, uint32_t key)
403 for (i = 0; i < model->num_devopts; i++) {
404 if ((model->devopts[i] & (SR_CONF_MASK | key)) == key)
411 /* Read device configuration setting.
413 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
414 const struct sr_channel_group *cg)
416 struct dev_context *devc;
426 if (!has_devopt(devc->model, key | SR_CONF_GET))
430 case SR_CONF_SAMPLERATE:
431 *data = g_variant_new_uint64(devc->samplerate);
433 case SR_CONF_LIMIT_MSEC:
434 *data = g_variant_new_uint64(devc->limit_msec);
436 case SR_CONF_LIMIT_SAMPLES:
437 *data = g_variant_new_uint64(devc->limit_samples);
440 *data = g_variant_new_boolean(devc->cfg_rle);
442 case SR_CONF_EXTERNAL_CLOCK:
443 *data = g_variant_new_boolean(devc->cfg_clock_source
446 case SR_CONF_CLOCK_EDGE:
447 idx = devc->cfg_clock_edge;
448 if (idx >= ARRAY_SIZE(signal_edge_names))
450 *data = g_variant_new_string(signal_edge_names[idx]);
452 case SR_CONF_TRIGGER_SOURCE:
453 idx = devc->cfg_trigger_source;
454 if (idx >= ARRAY_SIZE(trigger_source_names))
456 *data = g_variant_new_string(trigger_source_names[idx]);
458 case SR_CONF_TRIGGER_SLOPE:
459 idx = devc->cfg_trigger_slope;
460 if (idx >= ARRAY_SIZE(signal_edge_names))
462 *data = g_variant_new_string(signal_edge_names[idx]);
465 /* Must not happen for a key listed in devopts. */
472 /* Helper for mapping a string-typed configuration value to an index
473 * within a table of possible values.
475 static int lookup_index(GVariant *value, const char *const *table, int len)
480 entry = g_variant_get_string(value, NULL);
484 /* Linear search is fine for very small tables. */
485 for (i = 0; i < len; i++) {
486 if (strcmp(entry, table[i]) == 0)
493 /* Write device configuration setting.
495 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
496 const struct sr_channel_group *cg)
499 struct dev_context *devc;
509 if (!has_devopt(devc->model, key | SR_CONF_SET))
513 case SR_CONF_SAMPLERATE:
514 value = g_variant_get_uint64(data);
515 if (value < devc->model->samplerates[devc->model->num_samplerates - 1]
516 || value > devc->model->samplerates[0])
517 return SR_ERR_SAMPLERATE;
518 devc->samplerate = value;
520 case SR_CONF_LIMIT_MSEC:
521 value = g_variant_get_uint64(data);
522 if (value > MAX_LIMIT_MSEC)
524 devc->limit_msec = value;
526 case SR_CONF_LIMIT_SAMPLES:
527 value = g_variant_get_uint64(data);
528 if (value > MAX_LIMIT_SAMPLES)
530 devc->limit_samples = value;
533 devc->cfg_rle = g_variant_get_boolean(data);
535 case SR_CONF_EXTERNAL_CLOCK:
536 devc->cfg_clock_source = (g_variant_get_boolean(data))
537 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
539 case SR_CONF_CLOCK_EDGE:
540 idx = lookup_index(data, signal_edge_names,
541 ARRAY_SIZE(signal_edge_names));
544 devc->cfg_clock_edge = idx;
546 case SR_CONF_TRIGGER_SOURCE:
547 idx = lookup_index(data, trigger_source_names,
548 ARRAY_SIZE(trigger_source_names));
551 devc->cfg_trigger_source = idx;
553 case SR_CONF_TRIGGER_SLOPE:
554 idx = lookup_index(data, signal_edge_names,
555 ARRAY_SIZE(signal_edge_names));
558 devc->cfg_trigger_slope = idx;
561 /* Must not happen for a key listed in devopts. */
568 /* Apply channel configuration change.
570 static int config_channel_set(const struct sr_dev_inst *sdi,
571 struct sr_channel *ch, unsigned int changes)
573 uint64_t channel_bit;
574 struct dev_context *devc;
581 if (ch->index < 0 || ch->index >= devc->model->num_channels) {
582 sr_err("Channel index %d out of range.", ch->index);
586 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
587 channel_bit = UINT64_C(1) << ch->index;
589 /* Enable or disable logic input for this channel. */
591 devc->channel_mask |= channel_bit;
593 devc->channel_mask &= ~channel_bit;
599 /* Derive trigger masks from the session's trigger configuration.
601 static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
603 uint64_t trigger_mask, trigger_values, trigger_edge_mask;
604 uint64_t level_bit, type_bit;
605 struct dev_context *devc;
606 struct sr_trigger *trigger;
607 struct sr_trigger_stage *stage;
608 struct sr_trigger_match *match;
611 enum sr_trigger_matches trg;
615 trigger = sr_session_trigger_get(sdi->session);
616 if (!trigger || !trigger->stages)
619 if (trigger->stages->next) {
620 sr_err("This device only supports 1 trigger stage.");
623 stage = trigger->stages->data;
627 trigger_edge_mask = 0;
629 for (node = stage->matches; node; node = node->next) {
632 if (!match->channel->enabled)
633 continue; /* Ignore disabled channel. */
635 idx = match->channel->index;
638 if (idx < 0 || idx >= devc->model->num_channels) {
639 sr_err("Channel index %d out of range.", idx);
640 return SR_ERR_BUG; /* Should not happen. */
642 if (trg != SR_TRIGGER_ZERO
643 && trg != SR_TRIGGER_ONE
644 && trg != SR_TRIGGER_RISING
645 && trg != SR_TRIGGER_FALLING) {
646 sr_err("Unsupported trigger match for CH%d.", idx + 1);
649 level_bit = (trg == SR_TRIGGER_ONE
650 || trg == SR_TRIGGER_RISING) ? 1 : 0;
651 type_bit = (trg == SR_TRIGGER_RISING
652 || trg == SR_TRIGGER_FALLING) ? 1 : 0;
654 trigger_mask |= UINT64_C(1) << idx;
655 trigger_values |= level_bit << idx;
656 trigger_edge_mask |= type_bit << idx;
658 devc->trigger_mask = trigger_mask;
659 devc->trigger_values = trigger_values;
660 devc->trigger_edge_mask = trigger_edge_mask;
665 /* Apply current device configuration to the hardware.
667 static int config_commit(const struct sr_dev_inst *sdi)
669 struct dev_context *devc;
674 if (sdi->status != SR_ST_ACTIVE)
675 return SR_ERR_DEV_CLOSED;
677 if (devc->acquisition) {
678 sr_err("Acquisition still in progress?");
682 ret = prepare_trigger_masks(sdi);
686 ret = (*devc->model->apply_fpga_config)(sdi);
688 sr_err("Failed to apply FPGA configuration.");
695 /* List available choices for a configuration setting.
697 static int config_list(uint32_t key, GVariant **data,
698 const struct sr_dev_inst *sdi,
699 const struct sr_channel_group *cg)
701 struct dev_context *devc;
707 if (key == SR_CONF_SCAN_OPTIONS) {
708 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
709 scanopts, ARRAY_SIZE(scanopts), sizeof(scanopts[0]));
713 if (key != SR_CONF_DEVICE_OPTIONS)
716 /* List driver capabilities. */
717 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
718 drvopts, ARRAY_SIZE(drvopts), sizeof(drvopts[0]));
724 /* List the model's device options. */
725 if (key == SR_CONF_DEVICE_OPTIONS) {
726 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
727 devc->model->devopts, devc->model->num_devopts,
728 sizeof(devc->model->devopts[0]));
732 if (!has_devopt(devc->model, key | SR_CONF_LIST))
736 case SR_CONF_SAMPLERATE:
737 g_variant_builder_init(&gvb, G_VARIANT_TYPE_VARDICT);
738 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
739 devc->model->samplerates, devc->model->num_samplerates,
740 sizeof(devc->model->samplerates[0]));
741 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
742 *data = g_variant_builder_end(&gvb);
744 case SR_CONF_TRIGGER_MATCH:
745 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
746 trigger_matches, ARRAY_SIZE(trigger_matches),
747 sizeof(trigger_matches[0]));
749 case SR_CONF_TRIGGER_SOURCE:
750 *data = g_variant_new_strv(trigger_source_names,
751 ARRAY_SIZE(trigger_source_names));
753 case SR_CONF_TRIGGER_SLOPE:
754 case SR_CONF_CLOCK_EDGE:
755 *data = g_variant_new_strv(signal_edge_names,
756 ARRAY_SIZE(signal_edge_names));
759 /* Must not happen for a key listed in devopts. */
766 /* Set up the device hardware to begin capturing samples as soon as the
767 * configured trigger conditions are met, or immediately if no triggers
770 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
774 if (sdi->status != SR_ST_ACTIVE)
775 return SR_ERR_DEV_CLOSED;
777 sr_info("Starting acquisition.");
779 return lwla_start_acquisition(sdi);
782 /* Request that a running capture operation be stopped.
784 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
786 struct dev_context *devc;
792 if (sdi->status != SR_ST_ACTIVE)
793 return SR_ERR_DEV_CLOSED;
795 if (devc->state != STATE_IDLE && !devc->cancel_requested) {
796 devc->cancel_requested = TRUE;
797 sr_dbg("Stopping acquisition.");
803 /* SysClk LWLA driver descriptor.
805 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
806 .name = "sysclk-lwla",
807 .longname = "SysClk LWLA series",
810 .cleanup = dev_clear,
812 .dev_list = dev_list,
813 .dev_clear = dev_clear,
814 .config_get = config_get,
815 .config_set = config_set,
816 .config_channel_set = config_channel_set,
817 .config_commit = config_commit,
818 .config_list = config_list,
819 .dev_open = dev_open,
820 .dev_close = dev_close,
821 .dev_acquisition_start = dev_acquisition_start,
822 .dev_acquisition_stop = dev_acquisition_stop,