2 * This file is part of the libsigrok project.
4 * Copyright (C) 2020 Florian Schmidt <schmidt_florian@gmx.de>
5 * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
6 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * This driver implementation initially was derived from the
25 * src/hardware/saleae-logic16/ source code.
30 #include <libsigrok/libsigrok.h>
33 #include "libsigrok-internal.h"
36 static const uint32_t scanopts[] = {
40 static const uint32_t drvopts[] = {
41 SR_CONF_LOGIC_ANALYZER,
44 static const uint32_t devopts[] = {
45 /* TODO: SR_CONF_CONTINUOUS, */
46 SR_CONF_CONN | SR_CONF_GET,
47 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
48 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_GET | SR_CONF_LIST,
49 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
51 SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
52 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
53 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
56 static const int32_t trigger_matches[] = {
63 static const char *channel_names[] = {
64 "CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7",
65 "CH8", "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15",
68 static const uint64_t samplerates_la2016[] = {
86 static const uint64_t samplerates_la1016[] = {
103 static const float logic_threshold_value[] = {
115 static const char *logic_threshold[] = {
128 #define LOGIC_THRESHOLD_IDX_USER (ARRAY_SIZE(logic_threshold) - 1)
130 static GSList *scan(struct sr_dev_driver *di, GSList *options)
132 struct drv_context *drvc;
133 struct sr_context *ctx;
134 struct dev_context *devc;
135 struct sr_dev_inst *sdi;
136 struct sr_usb_dev_inst *usb;
137 struct sr_config *src;
140 GSList *conn_devices;
141 struct libusb_device_descriptor des;
142 libusb_device **devlist, *dev;
143 size_t dev_count, dev_idx, ch_idx;
147 uint64_t fw_uploaded;
154 for (l = options; l; l = l->next) {
158 conn = g_variant_get_string(src->data, NULL);
163 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
167 /* Find all LA2016 devices, optionally upload firmware to them. */
169 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
171 sr_err("Cannot get device list: %s.", libusb_error_name(ret));
175 for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
176 dev = devlist[dev_idx];
177 bus = libusb_get_bus_number(dev);
178 addr = libusb_get_device_address(dev);
181 for (l = conn_devices; l; l = l->next) {
183 if (usb->bus == bus && usb->address == addr)
188 * A connection parameter was specified and
189 * this device does not match the filter.
195 libusb_get_device_descriptor(dev, &des);
196 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
199 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
202 /* USB identification matches, a device was found. */
203 sr_dbg("Found a device (USB identification).");
204 sdi = g_malloc0(sizeof(*sdi));
205 sdi->status = SR_ST_INITIALIZING;
206 sdi->connection_id = g_strdup(conn_id);
209 if (des.iProduct != LA2016_IPRODUCT_INDEX) {
210 sr_info("Device at '%s' has no firmware loaded.",
213 ret = la2016_upload_firmware(ctx, dev, des.idProduct);
215 sr_err("MCU firmware upload failed.");
216 g_free(sdi->connection_id);
220 fw_uploaded = g_get_monotonic_time();
221 /* Will re-enumerate. Mark as "unknown address yet". */
225 sdi->vendor = g_strdup("Kingst");
226 sdi->model = g_strdup("LA2016");
228 for (ch_idx = 0; ch_idx < ARRAY_SIZE(channel_names); ch_idx++) {
229 sr_channel_new(sdi, ch_idx, SR_CHANNEL_LOGIC,
230 TRUE, channel_names[ch_idx]);
233 devices = g_slist_append(devices, sdi);
235 devc = g_malloc0(sizeof(*devc));
237 devc->fw_uploaded = fw_uploaded;
238 devc->threshold_voltage_idx = 0;
239 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
241 sdi->status = SR_ST_INACTIVE;
242 sdi->inst_type = SR_INST_USB;
244 sdi->conn = sr_usb_dev_inst_new(bus, addr, NULL);
246 libusb_free_device_list(devlist, 1);
247 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
249 return std_scan_complete(di, devices);
252 static int la2016_dev_open(struct sr_dev_inst *sdi)
254 struct sr_dev_driver *di;
255 struct drv_context *drvc;
256 struct sr_context *ctx;
257 libusb_device **devlist, *dev;
258 struct sr_usb_dev_inst *usb;
259 struct libusb_device_descriptor des;
261 size_t device_count, dev_idx;
271 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
273 sr_err("Cannot get device list: %s.", libusb_error_name(ret));
278 sr_warn("Device list is empty. Cannot open.");
281 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
282 dev = devlist[dev_idx];
283 libusb_get_device_descriptor(dev, &des);
285 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
287 if (des.iProduct != LA2016_IPRODUCT_INDEX)
290 check_conn = sdi->status == SR_ST_INITIALIZING;
291 check_conn |= sdi->status == SR_ST_INACTIVE;
293 /* Check physical USB bus/port address. */
294 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
297 if (strcmp(sdi->connection_id, conn_id) != 0) {
298 /* Not the device we looked up before. */
303 ret = libusb_open(dev, &usb->devhdl);
305 sr_err("Cannot open device: %s.",
306 libusb_error_name(ret));
311 if (usb->address == 0xff) {
313 * First encounter after firmware upload.
314 * Grab current address after enumeration.
316 usb->address = libusb_get_device_address(dev);
319 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
320 if (ret == LIBUSB_ERROR_BUSY) {
321 sr_err("Cannot claim USB interface. Another program or driver using it?");
324 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
325 sr_err("Device has been disconnected.");
328 } else if (ret != 0) {
329 sr_err("Cannot claim USB interface: %s.",
330 libusb_error_name(ret));
335 if ((ret = la2016_init_device(sdi)) != SR_OK) {
336 sr_err("Cannot initialize device.");
340 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
341 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
345 libusb_free_device_list(devlist, 1);
349 libusb_release_interface(usb->devhdl, USB_INTERFACE);
350 libusb_close(usb->devhdl);
359 static int dev_open(struct sr_dev_inst *sdi)
361 struct dev_context *devc;
362 uint64_t reset_done, now, elapsed_ms;
368 * When the sigrok driver recently has uploaded MCU firmware,
369 * then wait for the FX2 to re-enumerate. Allow the USB device
370 * to vanish before it reappears. Timeouts are rough estimates
371 * after all, the imprecise time of the last check (potentially
372 * executes after the total check period) simplifies code paths
373 * with optional diagnostics. And increases the probability of
374 * successfully detecting "late/slow" devices.
376 if (devc->fw_uploaded) {
377 sr_info("Waiting for device to reset after firmware upload.");
378 now = g_get_monotonic_time();
379 reset_done = devc->fw_uploaded + RENUM_GONE_DELAY_MS * 1000;
380 if (now < reset_done)
381 g_usleep(reset_done - now);
383 now = g_get_monotonic_time();
384 elapsed_ms = (now - devc->fw_uploaded) / 1000;
385 sr_spew("Waited %" PRIu64 "ms.", elapsed_ms);
386 ret = la2016_dev_open(sdi);
388 devc->fw_uploaded = 0;
391 g_usleep(RENUM_POLL_INTERVAL_MS * 1000);
392 } while (elapsed_ms < RENUM_CHECK_PERIOD_MS);
394 sr_err("Device failed to re-enumerate.");
397 sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms);
399 ret = la2016_dev_open(sdi);
403 sr_err("Cannot open device.");
410 static int dev_close(struct sr_dev_inst *sdi)
412 struct sr_usb_dev_inst *usb;
419 la2016_deinit_device(sdi);
421 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
422 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
423 libusb_release_interface(usb->devhdl, USB_INTERFACE);
424 libusb_close(usb->devhdl);
430 static int config_get(uint32_t key, GVariant **data,
431 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
433 struct dev_context *devc;
434 struct sr_usb_dev_inst *usb;
449 if (usb->address == 0xff) {
451 * Device still needs to re-enumerate after firmware
452 * upload, so we don't know its (future) address.
456 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
458 case SR_CONF_SAMPLERATE:
459 *data = g_variant_new_uint64(devc->cur_samplerate);
461 case SR_CONF_LIMIT_SAMPLES:
462 *data = g_variant_new_uint64(devc->limit_samples);
464 case SR_CONF_CAPTURE_RATIO:
465 *data = g_variant_new_uint64(devc->capture_ratio);
467 case SR_CONF_VOLTAGE_THRESHOLD:
468 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
469 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
471 case SR_CONF_LOGIC_THRESHOLD:
472 label = logic_threshold[devc->threshold_voltage_idx];
473 *data = g_variant_new_string(label);
475 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
476 *data = g_variant_new_double(devc->threshold_voltage);
486 static int config_set(uint32_t key, GVariant *data,
487 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
489 struct dev_context *devc;
498 case SR_CONF_SAMPLERATE:
499 devc->cur_samplerate = g_variant_get_uint64(data);
501 case SR_CONF_LIMIT_SAMPLES:
502 devc->limit_samples = g_variant_get_uint64(data);
504 case SR_CONF_CAPTURE_RATIO:
505 devc->capture_ratio = g_variant_get_uint64(data);
507 case SR_CONF_VOLTAGE_THRESHOLD:
508 g_variant_get(data, "(dd)", &low, &high);
509 devc->threshold_voltage = (low + high) / 2.0;
510 devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_USER;
512 case SR_CONF_LOGIC_THRESHOLD: {
513 idx = std_str_idx(data, ARRAY_AND_SIZE(logic_threshold));
516 if (idx != LOGIC_THRESHOLD_IDX_USER) {
517 devc->threshold_voltage = logic_threshold_value[idx];
519 devc->threshold_voltage_idx = idx;
522 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
523 devc->threshold_voltage = g_variant_get_double(data);
532 static int config_list(uint32_t key, GVariant **data,
533 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
535 struct dev_context *devc;
538 case SR_CONF_SCAN_OPTIONS:
539 case SR_CONF_DEVICE_OPTIONS:
540 return STD_CONFIG_LIST(key, data, sdi, cg,
541 scanopts, drvopts, devopts);
542 case SR_CONF_SAMPLERATE:
546 if (devc->max_samplerate == SR_MHZ(200)) {
547 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016));
549 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016));
552 case SR_CONF_LIMIT_SAMPLES:
553 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN,
554 LA2016_NUM_SAMPLES_MAX);
556 case SR_CONF_VOLTAGE_THRESHOLD:
557 *data = std_gvar_min_max_step_thresholds(
558 LA2016_THR_VOLTAGE_MIN,
559 LA2016_THR_VOLTAGE_MAX, 0.1);
561 case SR_CONF_TRIGGER_MATCH:
562 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
564 case SR_CONF_LOGIC_THRESHOLD:
565 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_threshold));
574 static int configure_channels(const struct sr_dev_inst *sdi)
576 struct dev_context *devc;
578 struct sr_channel *ch;
582 devc->cur_channels = 0;
583 for (l = sdi->channels; l; l = l->next) {
587 devc->cur_channels |= 1UL << ch->index;
593 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
595 struct sr_dev_driver *di;
596 struct drv_context *drvc;
597 struct sr_context *ctx;
598 struct dev_context *devc;
606 if (configure_channels(sdi) != SR_OK) {
607 sr_err("Cannot configure channels.");
611 devc->convbuffer_size = LA2016_CONVBUFFER_SIZE;
612 devc->convbuffer = g_try_malloc(devc->convbuffer_size);
613 if (!devc->convbuffer) {
614 sr_err("Cannot allocate conversion buffer.");
615 return SR_ERR_MALLOC;
618 ret = la2016_setup_acquisition(sdi);
620 g_free(devc->convbuffer);
621 devc->convbuffer = NULL;
625 ret = la2016_start_acquisition(sdi);
627 la2016_abort_acquisition(sdi);
628 g_free(devc->convbuffer);
629 devc->convbuffer = NULL;
633 devc->completion_seen = FALSE;
634 usb_source_add(sdi->session, ctx, 50,
635 la2016_receive_data, (void *)sdi);
637 std_session_send_df_header(sdi);
642 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
646 ret = la2016_abort_acquisition(sdi);
651 static struct sr_dev_driver kingst_la2016_driver_info = {
652 .name = "kingst-la2016",
653 .longname = "Kingst LA2016",
656 .cleanup = std_cleanup,
658 .dev_list = std_dev_list,
659 .dev_clear = std_dev_clear,
660 .config_get = config_get,
661 .config_set = config_set,
662 .config_list = config_list,
663 .dev_open = dev_open,
664 .dev_close = dev_close,
665 .dev_acquisition_start = dev_acquisition_start,
666 .dev_acquisition_stop = dev_acquisition_stop,
669 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);