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 static const uint32_t scanopts[] = {
33 static const uint32_t drvopts[] = {
34 SR_CONF_LOGIC_ANALYZER,
37 static const int32_t trigger_matches[] = {
44 static const char *const trigger_source_names[] = {
45 [TRIGGER_CHANNELS] = "CH",
46 [TRIGGER_EXT_TRG] = "TRG",
49 static const char *const signal_edge_names[] = {
50 [EDGE_POSITIVE] = "r",
51 [EDGE_NEGATIVE] = "f",
54 static struct sr_dev_inst *dev_inst_new(const struct model_info *model)
56 struct sr_dev_inst *sdi;
57 struct dev_context *devc;
61 devc = g_malloc0(sizeof(struct dev_context));
63 devc->active_fpga_config = FPGA_NOCONF;
65 devc->samplerate = model->samplerates[0];
66 devc->channel_mask = (UINT64_C(1) << model->num_channels) - 1;
68 sdi = g_malloc0(sizeof(struct sr_dev_inst));
69 sdi->status = SR_ST_INACTIVE;
70 sdi->vendor = g_strdup(VENDOR_NAME);
71 sdi->model = g_strdup(model->name);
74 for (i = 0; i < model->num_channels; i++) {
75 g_snprintf(name, sizeof(name), "CH%d", i + 1);
76 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
82 /* Create a new device instance for a libusb device if it is a SysClk LWLA
83 * device and also matches the connection specification.
85 static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches,
89 struct sr_usb_dev_inst *usb;
90 const struct model_info *model;
91 struct sr_dev_inst *sdi;
92 struct libusb_device_descriptor des;
93 int bus, address, ret;
94 unsigned int vid, pid;
96 bus = libusb_get_bus_number(dev);
97 address = libusb_get_device_address(dev);
99 for (node = conn_matches; node != NULL; node = node->next) {
101 if (usb && usb->bus == bus && usb->address == address)
104 if (conn_matches && !node)
105 return NULL; /* no match */
107 ret = libusb_get_device_descriptor(dev, &des);
109 sr_err("Failed to get USB device descriptor: %s.",
110 libusb_error_name(ret));
116 /* Create sigrok device instance. */
117 if (vid == USB_VID_SYSCLK && pid == USB_PID_LWLA1016) {
118 model = &lwla1016_info;
119 } else if (vid == USB_VID_SYSCLK && pid == USB_PID_LWLA1034) {
120 model = &lwla1034_info;
123 sr_warn("USB device %d.%d (%04x:%04x) is not a"
124 " SysClk LWLA.", bus, address, vid, pid);
127 sdi = dev_inst_new(model);
129 sdi->inst_type = SR_INST_USB;
130 sdi->conn = sr_usb_dev_inst_new(bus, address, NULL);
135 static GSList *scan(struct sr_dev_driver *di, GSList *options)
137 GSList *conn_devices, *devices, *node;
138 struct drv_context *drvc;
139 struct sr_dev_inst *sdi;
140 struct sr_config *src;
142 libusb_device **devlist;
150 for (node = options; node != NULL; node = node->next) {
152 if (src->key == SR_CONF_CONN) {
153 conn = g_variant_get_string(src->data, NULL);
158 /* Find devices matching the connection specification. */
159 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
162 /* List all libusb devices. */
163 num_devs = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
165 sr_err("Failed to list USB devices: %s.",
166 libusb_error_name(num_devs));
167 g_slist_free_full(conn_devices,
168 (GDestroyNotify)&sr_usb_dev_inst_free);
172 /* Scan the USB device list for matching LWLA devices. */
173 for (i = 0; i < num_devs; i++) {
174 sdi = dev_inst_new_matching(conn_devices, devlist[i]);
176 continue; /* no match */
178 /* Register device instance with driver. */
179 devices = g_slist_append(devices, sdi);
182 libusb_free_device_list(devlist, 1);
183 g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free);
185 return std_scan_complete(di, devices);
188 /* Drain any pending data from the USB transfer buffers on the device.
189 * This may be necessary e.g. after a crash or generally to clean up after
190 * an abnormal condition.
192 static int drain_usb(struct sr_usb_dev_inst *usb, unsigned int endpoint)
194 int drained, xfer_len, ret;
195 unsigned char buf[512];
196 const unsigned int drain_timeout_ms = 10;
201 ret = libusb_bulk_transfer(usb->devhdl, endpoint,
202 buf, sizeof(buf), &xfer_len,
205 } while (ret == LIBUSB_SUCCESS && xfer_len != 0);
207 if (ret != LIBUSB_SUCCESS && ret != LIBUSB_ERROR_TIMEOUT) {
208 sr_err("Failed to drain USB endpoint %u: %s.",
209 endpoint & (LIBUSB_ENDPOINT_IN - 1),
210 libusb_error_name(ret));
214 sr_warn("Drained %d bytes from USB endpoint %u.",
215 drained, endpoint & (LIBUSB_ENDPOINT_IN - 1));
221 static int dev_open(struct sr_dev_inst *sdi)
223 struct drv_context *drvc;
224 struct dev_context *devc;
225 struct sr_usb_dev_inst *usb;
228 drvc = sdi->driver->context;
232 /* Try the whole shebang three times, fingers crossed. */
233 for (i = 0; i < 3; i++) {
234 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
238 ret = libusb_set_configuration(usb->devhdl, USB_CONFIG);
239 if (ret != LIBUSB_SUCCESS) {
240 sr_err("Failed to set USB configuration: %s.",
241 libusb_error_name(ret));
246 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
247 if (ret != LIBUSB_SUCCESS) {
248 sr_err("Failed to claim interface: %s.",
249 libusb_error_name(ret));
254 ret = drain_usb(usb, EP_REPLY);
259 /* This delay appears to be necessary for reliable operation. */
262 devc->active_fpga_config = FPGA_NOCONF;
263 devc->short_transfer_quirk = FALSE;
264 devc->state = STATE_IDLE;
266 ret = (*devc->model->apply_fpga_config)(sdi);
269 ret = (*devc->model->device_init_check)(sdi);
273 /* Rinse and repeat. */
277 if (ret == SR_OK && devc->short_transfer_quirk)
278 sr_warn("Short transfer quirk detected! "
279 "Memory reads will be slow.");
283 static int dev_close(struct sr_dev_inst *sdi)
285 struct dev_context *devc;
286 struct sr_usb_dev_inst *usb;
292 if (devc->acquisition) {
293 sr_err("Cannot close device during acquisition!");
294 /* Request stop, leak handle, and prepare for the worst. */
295 devc->cancel_requested = TRUE;
299 /* Download of the shutdown bitstream, if any. */
300 ret = (*devc->model->apply_fpga_config)(sdi);
302 sr_warn("Unable to shut down device.");
304 libusb_release_interface(usb->devhdl, USB_INTERFACE);
311 /* Check whether the device options contain a specific key.
312 * Also match against get/set/list bits if specified.
314 static int has_devopt(const struct model_info *model, uint32_t key)
318 for (i = 0; i < model->num_devopts; i++) {
319 if ((model->devopts[i] & (SR_CONF_MASK | key)) == key)
326 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
327 const struct sr_channel_group *cg)
329 struct dev_context *devc;
339 if (!has_devopt(devc->model, key | SR_CONF_GET))
343 case SR_CONF_SAMPLERATE:
344 *data = g_variant_new_uint64(devc->samplerate);
346 case SR_CONF_LIMIT_MSEC:
347 *data = g_variant_new_uint64(devc->limit_msec);
349 case SR_CONF_LIMIT_SAMPLES:
350 *data = g_variant_new_uint64(devc->limit_samples);
353 *data = g_variant_new_boolean(devc->cfg_rle);
355 case SR_CONF_EXTERNAL_CLOCK:
356 *data = g_variant_new_boolean(devc->cfg_clock_source
359 case SR_CONF_CLOCK_EDGE:
360 idx = devc->cfg_clock_edge;
361 if (idx >= ARRAY_SIZE(signal_edge_names))
363 *data = g_variant_new_string(signal_edge_names[idx]);
365 case SR_CONF_TRIGGER_SOURCE:
366 idx = devc->cfg_trigger_source;
367 if (idx >= ARRAY_SIZE(trigger_source_names))
369 *data = g_variant_new_string(trigger_source_names[idx]);
371 case SR_CONF_TRIGGER_SLOPE:
372 idx = devc->cfg_trigger_slope;
373 if (idx >= ARRAY_SIZE(signal_edge_names))
375 *data = g_variant_new_string(signal_edge_names[idx]);
378 /* Must not happen for a key listed in devopts. */
385 /* Helper for mapping a string-typed configuration value to an index
386 * within a table of possible values.
388 static int lookup_index(GVariant *value, const char *const *table, int len)
393 entry = g_variant_get_string(value, NULL);
397 /* Linear search is fine for very small tables. */
398 for (i = 0; i < len; i++) {
399 if (strcmp(entry, table[i]) == 0)
406 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
407 const struct sr_channel_group *cg)
410 struct dev_context *devc;
420 if (!has_devopt(devc->model, key | SR_CONF_SET))
424 case SR_CONF_SAMPLERATE:
425 value = g_variant_get_uint64(data);
426 if (value < devc->model->samplerates[devc->model->num_samplerates - 1]
427 || value > devc->model->samplerates[0])
428 return SR_ERR_SAMPLERATE;
429 devc->samplerate = value;
431 case SR_CONF_LIMIT_MSEC:
432 value = g_variant_get_uint64(data);
433 if (value > MAX_LIMIT_MSEC)
435 devc->limit_msec = value;
437 case SR_CONF_LIMIT_SAMPLES:
438 value = g_variant_get_uint64(data);
439 if (value > MAX_LIMIT_SAMPLES)
441 devc->limit_samples = value;
444 devc->cfg_rle = g_variant_get_boolean(data);
446 case SR_CONF_EXTERNAL_CLOCK:
447 devc->cfg_clock_source = (g_variant_get_boolean(data))
448 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
450 case SR_CONF_CLOCK_EDGE:
451 idx = lookup_index(data, signal_edge_names,
452 ARRAY_SIZE(signal_edge_names));
455 devc->cfg_clock_edge = idx;
457 case SR_CONF_TRIGGER_SOURCE:
458 idx = lookup_index(data, trigger_source_names,
459 ARRAY_SIZE(trigger_source_names));
462 devc->cfg_trigger_source = idx;
464 case SR_CONF_TRIGGER_SLOPE:
465 idx = lookup_index(data, signal_edge_names,
466 ARRAY_SIZE(signal_edge_names));
469 devc->cfg_trigger_slope = idx;
472 /* Must not happen for a key listed in devopts. */
479 static int config_channel_set(const struct sr_dev_inst *sdi,
480 struct sr_channel *ch, unsigned int changes)
482 uint64_t channel_bit;
483 struct dev_context *devc;
490 if (ch->index < 0 || ch->index >= devc->model->num_channels) {
491 sr_err("Channel index %d out of range.", ch->index);
495 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
496 channel_bit = UINT64_C(1) << ch->index;
498 /* Enable or disable logic input for this channel. */
500 devc->channel_mask |= channel_bit;
502 devc->channel_mask &= ~channel_bit;
508 /* Derive trigger masks from the session's trigger configuration. */
509 static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
511 uint64_t trigger_mask, trigger_values, trigger_edge_mask;
512 uint64_t level_bit, type_bit;
513 struct dev_context *devc;
514 struct sr_trigger *trigger;
515 struct sr_trigger_stage *stage;
516 struct sr_trigger_match *match;
519 enum sr_trigger_matches trg;
523 trigger = sr_session_trigger_get(sdi->session);
524 if (!trigger || !trigger->stages)
527 if (trigger->stages->next) {
528 sr_err("This device only supports 1 trigger stage.");
531 stage = trigger->stages->data;
535 trigger_edge_mask = 0;
537 for (node = stage->matches; node; node = node->next) {
540 if (!match->channel->enabled)
541 continue; /* Ignore disabled channel. */
543 idx = match->channel->index;
546 if (idx < 0 || idx >= devc->model->num_channels) {
547 sr_err("Channel index %d out of range.", idx);
548 return SR_ERR_BUG; /* Should not happen. */
550 if (trg != SR_TRIGGER_ZERO
551 && trg != SR_TRIGGER_ONE
552 && trg != SR_TRIGGER_RISING
553 && trg != SR_TRIGGER_FALLING) {
554 sr_err("Unsupported trigger match for CH%d.", idx + 1);
557 level_bit = (trg == SR_TRIGGER_ONE
558 || trg == SR_TRIGGER_RISING) ? 1 : 0;
559 type_bit = (trg == SR_TRIGGER_RISING
560 || trg == SR_TRIGGER_FALLING) ? 1 : 0;
562 trigger_mask |= UINT64_C(1) << idx;
563 trigger_values |= level_bit << idx;
564 trigger_edge_mask |= type_bit << idx;
566 devc->trigger_mask = trigger_mask;
567 devc->trigger_values = trigger_values;
568 devc->trigger_edge_mask = trigger_edge_mask;
573 static int config_commit(const struct sr_dev_inst *sdi)
575 struct dev_context *devc;
580 if (devc->acquisition) {
581 sr_err("Acquisition still in progress?");
585 ret = prepare_trigger_masks(sdi);
589 ret = (*devc->model->apply_fpga_config)(sdi);
591 sr_err("Failed to apply FPGA configuration.");
598 static int config_list(uint32_t key, GVariant **data,
599 const struct sr_dev_inst *sdi,
600 const struct sr_channel_group *cg)
602 struct dev_context *devc;
604 devc = (sdi) ? sdi->priv : NULL;
607 case SR_CONF_SCAN_OPTIONS:
608 case SR_CONF_DEVICE_OPTIONS:
609 return std_opts_config_list(key, data, sdi, cg,
610 scanopts, ARRAY_SIZE(scanopts),
611 drvopts, ARRAY_SIZE(drvopts),
612 (devc) ? devc->model->devopts : NULL,
613 (devc) ? devc->model->num_devopts : 0);
616 if (!has_devopt(devc->model, key | SR_CONF_LIST))
620 case SR_CONF_SAMPLERATE:
621 *data = std_gvar_samplerates(devc->model->samplerates, devc->model->num_samplerates);
623 case SR_CONF_TRIGGER_MATCH:
624 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
625 trigger_matches, ARRAY_SIZE(trigger_matches),
626 sizeof(trigger_matches[0]));
628 case SR_CONF_TRIGGER_SOURCE:
629 *data = g_variant_new_strv(trigger_source_names,
630 ARRAY_SIZE(trigger_source_names));
632 case SR_CONF_TRIGGER_SLOPE:
633 case SR_CONF_CLOCK_EDGE:
634 *data = g_variant_new_strv(signal_edge_names,
635 ARRAY_SIZE(signal_edge_names));
638 /* Must not happen for a key listed in devopts. */
645 /* Set up the device hardware to begin capturing samples as soon as the
646 * configured trigger conditions are met, or immediately if no triggers
649 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
651 return lwla_start_acquisition(sdi);
654 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
656 struct dev_context *devc;
660 if (devc->state != STATE_IDLE && !devc->cancel_requested) {
661 devc->cancel_requested = TRUE;
662 sr_dbg("Requesting cancel.");
668 static struct sr_dev_driver sysclk_lwla_driver_info = {
669 .name = "sysclk-lwla",
670 .longname = "SysClk LWLA series",
673 .cleanup = std_cleanup,
675 .dev_list = std_dev_list,
676 .dev_clear = std_dev_clear,
677 .config_get = config_get,
678 .config_set = config_set,
679 .config_channel_set = config_channel_set,
680 .config_commit = config_commit,
681 .config_list = config_list,
682 .dev_open = dev_open,
683 .dev_close = dev_close,
684 .dev_acquisition_start = dev_acquisition_start,
685 .dev_acquisition_stop = dev_acquisition_stop,
688 SR_REGISTER_DEV_DRIVER(sysclk_lwla_driver_info);