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 /* Create a new sigrok device instance for the indicated LWLA model.
66 static struct sr_dev_inst *dev_inst_new(const struct model_info *model)
68 struct sr_dev_inst *sdi;
69 struct dev_context *devc;
73 /* Initialize private device context. */
74 devc = g_malloc0(sizeof(struct dev_context));
76 devc->active_fpga_config = FPGA_NOCONF;
78 devc->samplerate = model->samplerates[0];
79 devc->channel_mask = (UINT64_C(1) << model->num_channels) - 1;
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);
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);
98 /* Create a new device instance for a libusb device if it is a SysClk LWLA
99 * device and also matches the connection specification.
101 static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches,
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;
112 bus = libusb_get_bus_number(dev);
113 address = libusb_get_device_address(dev);
115 for (node = conn_matches; node != NULL; node = node->next) {
117 if (usb && usb->bus == bus && usb->address == address)
120 if (conn_matches && !node)
121 return NULL; /* no match */
123 ret = libusb_get_device_descriptor(dev, &des);
125 sr_err("Failed to get USB device descriptor: %s.",
126 libusb_error_name(ret));
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;
139 sr_warn("USB device %d.%d (%04x:%04x) is not a"
140 " SysClk LWLA.", bus, address, vid, pid);
143 sdi = dev_inst_new(model);
145 sdi->inst_type = SR_INST_USB;
146 sdi->conn = sr_usb_dev_inst_new(bus, address, NULL);
151 /* Scan for SysClk LWLA devices and create a device instance for each one.
153 static GSList *scan(struct sr_dev_driver *di, GSList *options)
155 GSList *conn_devices, *devices, *node;
156 struct drv_context *drvc;
157 struct sr_dev_inst *sdi;
158 struct sr_config *src;
160 libusb_device **devlist;
168 for (node = options; node != NULL; node = node->next) {
170 if (src->key == SR_CONF_CONN) {
171 conn = g_variant_get_string(src->data, NULL);
176 /* Find devices matching the connection specification. */
177 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
180 /* List all libusb devices. */
181 num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
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);
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]);
194 continue; /* no match */
196 /* Register device instance with driver. */
197 devices = g_slist_append(devices, sdi);
200 libusb_free_device_list(devlist, 1);
201 g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free);
203 return std_scan_complete(di, devices);
206 /* Destroy the private device context.
208 static void clear_dev_context(void *priv)
210 struct dev_context *devc;
214 if (devc->acquisition) {
215 sr_err("Cannot clear device context during acquisition!");
216 return; /* Leak and pray. */
218 sr_dbg("Device context cleared.");
223 /* Destroy all device instances.
225 static int dev_clear(const struct sr_dev_driver *di)
227 return std_dev_clear(di, &clear_dev_context);
230 /* Drain any pending data from the USB transfer buffers on the device.
231 * This may be necessary e.g. after a crash or generally to clean up after
232 * an abnormal condition.
234 static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint)
236 int drained, xfer_len, ret;
237 unsigned char buf[512];
238 const unsigned int drain_timeout_ms = 10;
243 ret = libusb_bulk_transfer(usb->devhdl, endpoint,
244 buf, sizeof(buf), &xfer_len,
247 } while (ret == LIBUSB_SUCCESS && xfer_len != 0);
249 if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) {
250 sr_err("Failed to drain USB endpoint %u: %s.",
251 endpoint & (LIBUSB_ENDPOINT_IN - 1),
252 libusb_error_name(ret));
256 sr_warn("Drained %d bytes from USB endpoint %u.",
257 drained, endpoint & (LIBUSB_ENDPOINT_IN - 1));
263 /* Open and initialize device.
265 static int dev_open(struct sr_dev_inst *sdi)
267 struct drv_context *drvc;
268 struct dev_context *devc;
269 struct sr_usb_dev_inst *usb;
272 drvc = sdi->driver->context;
276 /* Try the whole shebang three times, fingers crossed. */
277 for (i = 0; i < 3; i++) {
278 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
282 ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
283 if (ret != LIBUSB_SUCCESS) {
284 sr_err("Failed to set USB configuration: %s.",
285 libusb_error_name(ret));
290 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
291 if (ret != LIBUSB_SUCCESS) {
292 sr_err("Failed to claim interface: %s.",
293 libusb_error_name(ret));
298 ret = drain_usb(usb, EP_REPLY);
303 /* This delay appears to be necessary for reliable operation. */
306 devc->active_fpga_config = FPGA_NOCONF;
307 devc->short_transfer_quirk = FALSE;
308 devc->state = STATE_IDLE;
310 ret = (*devc->model->apply_fpga_config)(sdi);
313 ret = (*devc->model->device_init_check)(sdi);
317 /* Rinse and repeat. */
321 if (ret == SR_OK && devc->short_transfer_quirk)
322 sr_warn("Short transfer quirk detected! "
323 "Memory reads will be slow.");
327 /* Shutdown and close device.
329 static int dev_close(struct sr_dev_inst *sdi)
331 struct dev_context *devc;
332 struct sr_usb_dev_inst *usb;
338 if (devc->acquisition) {
339 sr_err("Cannot close device during acquisition!");
340 /* Request stop, leak handle, and prepare for the worst. */
341 devc->cancel_requested = TRUE;
345 sdi->status = SR_ST_INACTIVE;
347 /* Download of the shutdown bitstream, if any. */
348 ret = (*devc->model->apply_fpga_config)(sdi);
350 sr_warn("Unable to shut down device.");
352 libusb_release_interface(usb->devhdl, USB_INTERFACE);
358 /* Check whether the device options contain a specific key.
359 * Also match against get/set/list bits if specified.
361 static int has_devopt(const struct model_info *model, uint32_t key)
365 for (i = 0; i < model->num_devopts; i++) {
366 if ((model->devopts[i] & (SR_CONF_MASK | key)) == key)
373 /* Read device configuration setting.
375 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
376 const struct sr_channel_group *cg)
378 struct dev_context *devc;
388 if (!has_devopt(devc->model, key | SR_CONF_GET))
392 case SR_CONF_SAMPLERATE:
393 *data = g_variant_new_uint64(devc->samplerate);
395 case SR_CONF_LIMIT_MSEC:
396 *data = g_variant_new_uint64(devc->limit_msec);
398 case SR_CONF_LIMIT_SAMPLES:
399 *data = g_variant_new_uint64(devc->limit_samples);
402 *data = g_variant_new_boolean(devc->cfg_rle);
404 case SR_CONF_EXTERNAL_CLOCK:
405 *data = g_variant_new_boolean(devc->cfg_clock_source
408 case SR_CONF_CLOCK_EDGE:
409 idx = devc->cfg_clock_edge;
410 if (idx >= ARRAY_SIZE(signal_edge_names))
412 *data = g_variant_new_string(signal_edge_names[idx]);
414 case SR_CONF_TRIGGER_SOURCE:
415 idx = devc->cfg_trigger_source;
416 if (idx >= ARRAY_SIZE(trigger_source_names))
418 *data = g_variant_new_string(trigger_source_names[idx]);
420 case SR_CONF_TRIGGER_SLOPE:
421 idx = devc->cfg_trigger_slope;
422 if (idx >= ARRAY_SIZE(signal_edge_names))
424 *data = g_variant_new_string(signal_edge_names[idx]);
427 /* Must not happen for a key listed in devopts. */
434 /* Helper for mapping a string-typed configuration value to an index
435 * within a table of possible values.
437 static int lookup_index(GVariant *value, const char *const *table, int len)
442 entry = g_variant_get_string(value, NULL);
446 /* Linear search is fine for very small tables. */
447 for (i = 0; i < len; i++) {
448 if (strcmp(entry, table[i]) == 0)
455 /* Write device configuration setting.
457 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
458 const struct sr_channel_group *cg)
461 struct dev_context *devc;
471 if (!has_devopt(devc->model, key | SR_CONF_SET))
475 case SR_CONF_SAMPLERATE:
476 value = g_variant_get_uint64(data);
477 if (value < devc->model->samplerates[devc->model->num_samplerates - 1]
478 || value > devc->model->samplerates[0])
479 return SR_ERR_SAMPLERATE;
480 devc->samplerate = value;
482 case SR_CONF_LIMIT_MSEC:
483 value = g_variant_get_uint64(data);
484 if (value > MAX_LIMIT_MSEC)
486 devc->limit_msec = value;
488 case SR_CONF_LIMIT_SAMPLES:
489 value = g_variant_get_uint64(data);
490 if (value > MAX_LIMIT_SAMPLES)
492 devc->limit_samples = value;
495 devc->cfg_rle = g_variant_get_boolean(data);
497 case SR_CONF_EXTERNAL_CLOCK:
498 devc->cfg_clock_source = (g_variant_get_boolean(data))
499 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
501 case SR_CONF_CLOCK_EDGE:
502 idx = lookup_index(data, signal_edge_names,
503 ARRAY_SIZE(signal_edge_names));
506 devc->cfg_clock_edge = idx;
508 case SR_CONF_TRIGGER_SOURCE:
509 idx = lookup_index(data, trigger_source_names,
510 ARRAY_SIZE(trigger_source_names));
513 devc->cfg_trigger_source = idx;
515 case SR_CONF_TRIGGER_SLOPE:
516 idx = lookup_index(data, signal_edge_names,
517 ARRAY_SIZE(signal_edge_names));
520 devc->cfg_trigger_slope = idx;
523 /* Must not happen for a key listed in devopts. */
530 /* Apply channel configuration change.
532 static int config_channel_set(const struct sr_dev_inst *sdi,
533 struct sr_channel *ch, unsigned int changes)
535 uint64_t channel_bit;
536 struct dev_context *devc;
543 if (ch->index < 0 || ch->index >= devc->model->num_channels) {
544 sr_err("Channel index %d out of range.", ch->index);
548 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
549 channel_bit = UINT64_C(1) << ch->index;
551 /* Enable or disable logic input for this channel. */
553 devc->channel_mask |= channel_bit;
555 devc->channel_mask &= ~channel_bit;
561 /* Derive trigger masks from the session's trigger configuration.
563 static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
565 uint64_t trigger_mask, trigger_values, trigger_edge_mask;
566 uint64_t level_bit, type_bit;
567 struct dev_context *devc;
568 struct sr_trigger *trigger;
569 struct sr_trigger_stage *stage;
570 struct sr_trigger_match *match;
573 enum sr_trigger_matches trg;
577 trigger = sr_session_trigger_get(sdi->session);
578 if (!trigger || !trigger->stages)
581 if (trigger->stages->next) {
582 sr_err("This device only supports 1 trigger stage.");
585 stage = trigger->stages->data;
589 trigger_edge_mask = 0;
591 for (node = stage->matches; node; node = node->next) {
594 if (!match->channel->enabled)
595 continue; /* Ignore disabled channel. */
597 idx = match->channel->index;
600 if (idx < 0 || idx >= devc->model->num_channels) {
601 sr_err("Channel index %d out of range.", idx);
602 return SR_ERR_BUG; /* Should not happen. */
604 if (trg != SR_TRIGGER_ZERO
605 && trg != SR_TRIGGER_ONE
606 && trg != SR_TRIGGER_RISING
607 && trg != SR_TRIGGER_FALLING) {
608 sr_err("Unsupported trigger match for CH%d.", idx + 1);
611 level_bit = (trg == SR_TRIGGER_ONE
612 || trg == SR_TRIGGER_RISING) ? 1 : 0;
613 type_bit = (trg == SR_TRIGGER_RISING
614 || trg == SR_TRIGGER_FALLING) ? 1 : 0;
616 trigger_mask |= UINT64_C(1) << idx;
617 trigger_values |= level_bit << idx;
618 trigger_edge_mask |= type_bit << idx;
620 devc->trigger_mask = trigger_mask;
621 devc->trigger_values = trigger_values;
622 devc->trigger_edge_mask = trigger_edge_mask;
627 /* Apply current device configuration to the hardware.
629 static int config_commit(const struct sr_dev_inst *sdi)
631 struct dev_context *devc;
636 if (devc->acquisition) {
637 sr_err("Acquisition still in progress?");
641 ret = prepare_trigger_masks(sdi);
645 ret = (*devc->model->apply_fpga_config)(sdi);
647 sr_err("Failed to apply FPGA configuration.");
654 /* List available choices for a configuration setting.
656 static int config_list(uint32_t key, GVariant **data,
657 const struct sr_dev_inst *sdi,
658 const struct sr_channel_group *cg)
660 struct dev_context *devc;
666 if (key == SR_CONF_SCAN_OPTIONS) {
667 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
668 scanopts, ARRAY_SIZE(scanopts), sizeof(scanopts[0]));
672 if (key != SR_CONF_DEVICE_OPTIONS)
675 /* List driver capabilities. */
676 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
677 drvopts, ARRAY_SIZE(drvopts), sizeof(drvopts[0]));
683 /* List the model's device options. */
684 if (key == SR_CONF_DEVICE_OPTIONS) {
685 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
686 devc->model->devopts, devc->model->num_devopts,
687 sizeof(devc->model->devopts[0]));
691 if (!has_devopt(devc->model, key | SR_CONF_LIST))
695 case SR_CONF_SAMPLERATE:
696 g_variant_builder_init(&gvb, G_VARIANT_TYPE_VARDICT);
697 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
698 devc->model->samplerates, devc->model->num_samplerates,
699 sizeof(devc->model->samplerates[0]));
700 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
701 *data = g_variant_builder_end(&gvb);
703 case SR_CONF_TRIGGER_MATCH:
704 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
705 trigger_matches, ARRAY_SIZE(trigger_matches),
706 sizeof(trigger_matches[0]));
708 case SR_CONF_TRIGGER_SOURCE:
709 *data = g_variant_new_strv(trigger_source_names,
710 ARRAY_SIZE(trigger_source_names));
712 case SR_CONF_TRIGGER_SLOPE:
713 case SR_CONF_CLOCK_EDGE:
714 *data = g_variant_new_strv(signal_edge_names,
715 ARRAY_SIZE(signal_edge_names));
718 /* Must not happen for a key listed in devopts. */
725 /* Set up the device hardware to begin capturing samples as soon as the
726 * configured trigger conditions are met, or immediately if no triggers
729 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
731 return lwla_start_acquisition(sdi);
734 /* Request that a running capture operation be stopped.
736 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
738 struct dev_context *devc;
742 if (devc->state != STATE_IDLE && !devc->cancel_requested) {
743 devc->cancel_requested = TRUE;
744 sr_dbg("Requesting cancel.");
750 /* SysClk LWLA driver descriptor.
752 static struct sr_dev_driver sysclk_lwla_driver_info = {
753 .name = "sysclk-lwla",
754 .longname = "SysClk LWLA series",
757 .cleanup = std_cleanup,
759 .dev_list = std_dev_list,
760 .dev_clear = dev_clear,
761 .config_get = config_get,
762 .config_set = config_set,
763 .config_channel_set = config_channel_set,
764 .config_commit = config_commit,
765 .config_list = config_list,
766 .dev_open = dev_open,
767 .dev_close = dev_close,
768 .dev_acquisition_start = dev_acquisition_start,
769 .dev_acquisition_stop = dev_acquisition_stop,
772 SR_REGISTER_DEV_DRIVER(sysclk_lwla_driver_info);