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"
37 * Default device configuration. Must be applicable to any of the
38 * supported devices (no model specific default values yet). Specific
39 * firmware implementation details unfortunately won't let us detect
40 * and keep using previously configured values.
42 #define LA2016_DFLT_SAMPLERATE SR_MHZ(100)
43 #define LA2016_DFLT_SAMPLEDEPTH (5 * 1000 * 1000)
44 #define LA2016_DFLT_CAPT_RATIO 5 /* Capture ratio, in percent. */
46 static const uint32_t scanopts[] = {
50 static const uint32_t drvopts[] = {
51 SR_CONF_LOGIC_ANALYZER,
54 static const uint32_t devopts[] = {
55 /* TODO: SR_CONF_CONTINUOUS, */
56 SR_CONF_CONN | SR_CONF_GET,
57 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
58 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
59 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
60 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
61 SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
62 SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
63 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
64 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
67 static const int32_t trigger_matches[] = {
74 static const char *channel_names_logic[] = {
75 "CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7",
76 "CH8", "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15",
77 "CH16", "CH17", "CH18", "CH19", "CH20", "CH21", "CH22", "CH23",
78 "CH24", "CH25", "CH26", "CH27", "CH28", "CH29", "CH30", "CH31",
82 * The hardware uses a 100/200/500MHz base clock (model dependent) and
83 * a 16bit divider (common across all models). The range from 10kHz to
84 * 100/200/500MHz should be applicable to all devices. High rates may
85 * suffer from coarse resolution (e.g. in the "500MHz div 2" case) and
86 * may not provide the desired 1/2/5 steps. Fortunately this exclusively
87 * affects the 500MHz model where 250MHz is used instead of 200MHz and
88 * the 166MHz and 125MHz rates are not presented to users. Deep memory
89 * of these models and hardware compression reduce the necessity to let
90 * users pick from a huge list of possible rates.
94 static const uint64_t rates_500mhz[] = {
112 static const uint64_t rates_200mhz[] = {
129 static const uint64_t rates_100mhz[] = {
145 static const float logic_threshold_value[] = {
157 static const char *logic_threshold[] = {
170 #define LOGIC_THRESHOLD_IDX_USER (ARRAY_SIZE(logic_threshold) - 1)
172 /* Convenience. Release an allocated devc from error paths. */
173 static void kingst_la2016_free_devc(struct dev_context *devc)
177 g_free(devc->mcu_firmware);
178 g_free(devc->fpga_bitstream);
182 /* Convenience. Release an allocated sdi from error paths. */
183 static void kingst_la2016_free_sdi(struct sr_dev_inst *sdi)
189 g_free(sdi->version);
190 g_free(sdi->serial_num);
191 g_free(sdi->connection_id);
192 sr_usb_dev_inst_free(sdi->conn);
193 kingst_la2016_free_devc(sdi->priv);
196 /* Convenience. Open a USB device (including claiming an interface). */
197 static int la2016_open_usb(struct sr_usb_dev_inst *usb,
198 libusb_device *dev, gboolean show_message)
202 ret = libusb_open(dev, &usb->devhdl);
205 sr_err("Cannot open device: %s.",
206 libusb_error_name(ret));
211 if (usb->address == 0xff) {
213 * First encounter after firmware upload.
214 * Grab current address after enumeration.
216 usb->address = libusb_get_device_address(dev);
219 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
220 if (ret == LIBUSB_ERROR_BUSY) {
221 sr_err("Cannot claim USB interface. Another program or driver using it?");
223 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
224 sr_err("Device has been disconnected.");
226 } else if (ret != 0) {
227 sr_err("Cannot claim USB interface: %s.",
228 libusb_error_name(ret));
235 /* Convenience. Close an opened USB device (and release the interface). */
236 static void la2016_close_usb(struct sr_usb_dev_inst *usb)
243 libusb_release_interface(usb->devhdl, USB_INTERFACE);
244 libusb_close(usb->devhdl);
249 /* Communicate to an USB device to identify the Kingst LA model. */
250 static int la2016_identify_read(struct sr_dev_inst *sdi,
251 struct sr_usb_dev_inst *usb, libusb_device *dev,
252 gboolean show_message)
256 ret = la2016_open_usb(usb, dev, show_message);
259 sr_err("Cannot communicate to MCU firmware.");
262 ret = la2016_identify_device(sdi, show_message);
263 la2016_close_usb(usb);
268 /* Find given conn_id in another USB enum. Identify Kingst LA model. */
269 static int la2016_identify_enum(struct sr_dev_inst *sdi)
271 struct sr_dev_driver *di;
272 struct drv_context *drvc;
273 struct sr_context *ctx;
274 libusb_device **devlist, *dev;
275 struct libusb_device_descriptor des;
277 size_t device_count, dev_idx;
284 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
291 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
292 dev = devlist[dev_idx];
293 libusb_get_device_descriptor(dev, &des);
294 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
296 if (des.iProduct != LA2016_IPRODUCT_INDEX)
298 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
301 if (strcmp(sdi->connection_id, conn_id) != 0)
303 id_ret = la2016_identify_read(sdi, sdi->conn, dev, FALSE);
306 libusb_free_device_list(devlist, 1);
311 /* Wait for a device to re-appear after firmware upload. */
312 static int la2016_identify_wait(struct sr_dev_inst *sdi)
314 struct dev_context *devc;
315 uint64_t reset_done, now, elapsed_ms;
320 sr_info("Waiting for device to reset after firmware upload.");
321 now = g_get_monotonic_time();
322 reset_done = devc->fw_uploaded + RENUM_GONE_DELAY_MS * 1000;
323 if (now < reset_done)
324 g_usleep(reset_done - now);
326 now = g_get_monotonic_time();
327 elapsed_ms = (now - devc->fw_uploaded) / 1000;
328 sr_spew("Waited %" PRIu64 "ms.", elapsed_ms);
329 ret = la2016_identify_enum(sdi);
331 devc->fw_uploaded = 0;
334 g_usleep(RENUM_POLL_INTERVAL_MS * 1000);
335 } while (elapsed_ms < RENUM_CHECK_PERIOD_MS);
337 sr_err("Device failed to re-enumerate.");
340 sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms);
346 * Open given conn_id from another USB enum. Used by dev_open(). Similar
347 * to, and should be kept in sync with la2016_identify_enum().
349 static int la2016_open_enum(struct sr_dev_inst *sdi)
351 struct sr_dev_driver *di;
352 struct drv_context *drvc;
353 struct sr_context *ctx;
354 libusb_device **devlist, *dev;
355 struct libusb_device_descriptor des;
357 size_t device_count, dev_idx;
364 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
370 open_ret = SR_ERR_IO;
371 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
372 dev = devlist[dev_idx];
373 libusb_get_device_descriptor(dev, &des);
374 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
376 if (des.iProduct != LA2016_IPRODUCT_INDEX)
378 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
381 if (strcmp(sdi->connection_id, conn_id) != 0)
383 open_ret = la2016_open_usb(sdi->conn, dev, TRUE);
386 libusb_free_device_list(devlist, 1);
391 static GSList *scan(struct sr_dev_driver *di, GSList *options)
393 struct drv_context *drvc;
394 struct sr_context *ctx;
395 struct dev_context *devc;
396 struct sr_dev_inst *sdi;
397 struct sr_usb_dev_inst *usb;
398 struct sr_config *src;
400 GSList *devices, *found_devices, *renum_devices;
401 GSList *conn_devices;
402 struct libusb_device_descriptor des;
403 libusb_device **devlist, *dev;
404 size_t dev_count, dev_idx, ch_idx;
410 size_t ch_off, ch_max;
417 for (l = options; l; l = l->next) {
421 conn = g_variant_get_string(src->data, NULL);
426 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
427 if (conn && !conn_devices) {
428 sr_err("Cannot find the specified connection '%s'.", conn);
433 * Find all LA2016 devices, optionally upload firmware to them.
434 * Defer completion of sdi/devc creation until all (selected)
435 * devices were found in a usable state, and their models got
436 * identified which affect their feature set. It appears that
437 * we cannot communicate to the device within the same USB enum
438 * cycle, needs another USB enumeration after firmware upload.
441 found_devices = NULL;
442 renum_devices = NULL;
443 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
445 sr_err("Cannot get device list: %s.", libusb_error_name(ret));
449 for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
450 dev = devlist[dev_idx];
451 bus = libusb_get_bus_number(dev);
452 addr = libusb_get_device_address(dev);
454 /* Filter by connection when externally specified. */
455 for (l = conn_devices; l; l = l->next) {
457 if (usb->bus == bus && usb->address == addr)
460 if (conn_devices && !l) {
461 sr_spew("Bus %hhu, addr %hhu do not match specified filter.",
466 /* Check USB VID:PID. Get the connection string. */
467 libusb_get_device_descriptor(dev, &des);
468 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
471 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
474 sr_dbg("USB enum found %04x:%04x at path %s, %d.%d.",
475 des.idVendor, des.idProduct, conn_id, bus, addr);
476 usb = sr_usb_dev_inst_new(bus, addr, NULL);
478 sdi = g_malloc0(sizeof(*sdi));
480 sdi->status = SR_ST_INITIALIZING;
481 sdi->inst_type = SR_INST_USB;
482 sdi->connection_id = g_strdup(conn_id);
485 devc = g_malloc0(sizeof(*devc));
489 * Load MCU firmware if it is currently missing. Which
490 * makes the device disappear and renumerate in USB.
491 * We need to come back another time to communicate to
494 devc->fw_uploaded = 0;
495 if (des.iProduct != LA2016_IPRODUCT_INDEX) {
496 sr_info("Uploading MCU firmware to '%s'.", conn_id);
497 ret = la2016_upload_firmware(sdi, ctx, dev, pid);
499 sr_err("MCU firmware upload failed.");
500 kingst_la2016_free_sdi(sdi);
503 devc->fw_uploaded = g_get_monotonic_time();
505 renum_devices = g_slist_append(renum_devices, sdi);
510 * Communicate to the MCU firmware to access EEPROM data
511 * which lets us identify the device type. Then stop, to
512 * share remaining sdi/devc creation with those devices
513 * which had their MCU firmware uploaded above and which
514 * get revisited later.
516 ret = la2016_identify_read(sdi, usb, dev, TRUE);
517 if (ret != SR_OK || !devc->model) {
518 sr_err("Unknown or unsupported device type.");
519 kingst_la2016_free_sdi(sdi);
522 found_devices = g_slist_append(found_devices, sdi);
524 libusb_free_device_list(devlist, 1);
525 g_slist_free_full(conn_devices, sr_usb_dev_inst_free_cb);
528 * Wait for devices to re-appear after firmware upload. Append
529 * the yet unidentified device to the list of found devices, or
530 * release the previously allocated sdi/devc.
532 for (l = renum_devices; l; l = l->next) {
535 ret = la2016_identify_wait(sdi);
536 if (ret != SR_OK || !devc->model) {
537 sr_dbg("Skipping unusable '%s'.", sdi->connection_id);
538 kingst_la2016_free_sdi(sdi);
541 found_devices = g_slist_append(found_devices, sdi);
543 g_slist_free(renum_devices);
546 * All found devices got identified, their type is known here.
547 * Complete the sdi/devc creation. Assign default settings
548 * because the vendor firmware would not let us read back the
549 * previously written configuration.
551 for (l = found_devices; l; l = l->next) {
555 sdi->vendor = g_strdup("Kingst");
556 sdi->model = g_strdup(devc->model->name);
559 /* Create the logic channels. */
560 ch_max = ARRAY_SIZE(channel_names_logic);
561 if (ch_max > devc->model->channel_count)
562 ch_max = devc->model->channel_count;
563 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
564 sr_channel_new(sdi, ch_off,
565 SR_CHANNEL_LOGIC, TRUE,
566 channel_names_logic[ch_idx]);
570 sr_sw_limits_init(&devc->sw_limits);
571 devc->sw_limits.limit_samples = LA2016_DFLT_SAMPLEDEPTH;
572 devc->capture_ratio = LA2016_DFLT_CAPT_RATIO;
573 devc->cur_samplerate = LA2016_DFLT_SAMPLERATE;
574 devc->threshold_voltage_idx = 0;
575 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
577 sdi->status = SR_ST_INACTIVE;
578 devices = g_slist_append(devices, sdi);
580 g_slist_free(found_devices);
582 return std_scan_complete(di, devices);
585 static int dev_open(struct sr_dev_inst *sdi)
589 ret = la2016_open_enum(sdi);
591 sr_err("Cannot open device.");
598 static int dev_close(struct sr_dev_inst *sdi)
600 struct sr_usb_dev_inst *usb;
607 la2016_deinit_device(sdi);
609 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
610 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
611 la2016_close_usb(sdi->conn);
616 static int config_get(uint32_t key, GVariant **data,
617 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
619 struct dev_context *devc;
620 struct sr_usb_dev_inst *usb;
635 if (usb->address == 0xff) {
637 * Device still needs to re-enumerate after firmware
638 * upload, so we don't know its (future) address.
642 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
644 case SR_CONF_SAMPLERATE:
645 *data = g_variant_new_uint64(devc->cur_samplerate);
647 case SR_CONF_LIMIT_SAMPLES:
648 case SR_CONF_LIMIT_MSEC:
649 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
650 case SR_CONF_CAPTURE_RATIO:
651 *data = g_variant_new_uint64(devc->capture_ratio);
653 case SR_CONF_VOLTAGE_THRESHOLD:
654 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
655 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
657 case SR_CONF_LOGIC_THRESHOLD:
658 label = logic_threshold[devc->threshold_voltage_idx];
659 *data = g_variant_new_string(label);
661 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
662 *data = g_variant_new_double(devc->threshold_voltage);
672 static int config_set(uint32_t key, GVariant *data,
673 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
675 struct dev_context *devc;
684 case SR_CONF_SAMPLERATE:
685 devc->cur_samplerate = g_variant_get_uint64(data);
687 case SR_CONF_LIMIT_SAMPLES:
688 case SR_CONF_LIMIT_MSEC:
689 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
690 case SR_CONF_CAPTURE_RATIO:
691 devc->capture_ratio = g_variant_get_uint64(data);
693 case SR_CONF_VOLTAGE_THRESHOLD:
694 g_variant_get(data, "(dd)", &low, &high);
695 devc->threshold_voltage = (low + high) / 2.0;
696 devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_USER;
698 case SR_CONF_LOGIC_THRESHOLD: {
699 idx = std_str_idx(data, ARRAY_AND_SIZE(logic_threshold));
702 if (idx != LOGIC_THRESHOLD_IDX_USER) {
703 devc->threshold_voltage = logic_threshold_value[idx];
705 devc->threshold_voltage_idx = idx;
708 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
709 devc->threshold_voltage = g_variant_get_double(data);
718 static int config_list(uint32_t key, GVariant **data,
719 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
721 struct dev_context *devc;
723 devc = sdi ? sdi->priv : NULL;
726 case SR_CONF_SCAN_OPTIONS:
727 case SR_CONF_DEVICE_OPTIONS:
728 return STD_CONFIG_LIST(key, data, sdi, cg,
729 scanopts, drvopts, devopts);
730 case SR_CONF_SAMPLERATE:
733 if (devc->model->samplerate == SR_MHZ(500))
734 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_500mhz));
735 else if (devc->model->samplerate == SR_MHZ(200))
736 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_200mhz));
738 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_100mhz));
740 case SR_CONF_LIMIT_SAMPLES:
741 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN,
742 LA2016_NUM_SAMPLES_MAX);
744 case SR_CONF_VOLTAGE_THRESHOLD:
745 *data = std_gvar_min_max_step_thresholds(
746 LA2016_THR_VOLTAGE_MIN,
747 LA2016_THR_VOLTAGE_MAX, 0.1);
749 case SR_CONF_TRIGGER_MATCH:
750 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
752 case SR_CONF_LOGIC_THRESHOLD:
753 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_threshold));
762 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
764 struct sr_dev_driver *di;
765 struct drv_context *drvc;
766 struct sr_context *ctx;
767 struct dev_context *devc;
775 if (!devc->feed_queue) {
776 devc->feed_queue = feed_queue_logic_alloc(sdi,
777 LA2016_CONVBUFFER_SIZE, sizeof(uint16_t));
778 if (!devc->feed_queue) {
779 sr_err("Cannot allocate buffer for session feed.");
780 return SR_ERR_MALLOC;
784 sr_sw_limits_acquisition_start(&devc->sw_limits);
786 ret = la2016_setup_acquisition(sdi);
788 feed_queue_logic_free(devc->feed_queue);
789 devc->feed_queue = NULL;
793 ret = la2016_start_acquisition(sdi);
795 la2016_abort_acquisition(sdi);
796 feed_queue_logic_free(devc->feed_queue);
797 devc->feed_queue = NULL;
801 devc->completion_seen = FALSE;
802 usb_source_add(sdi->session, ctx, 50,
803 la2016_receive_data, (void *)sdi);
805 std_session_send_df_header(sdi);
810 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
814 ret = la2016_abort_acquisition(sdi);
819 static struct sr_dev_driver kingst_la2016_driver_info = {
820 .name = "kingst-la2016",
821 .longname = "Kingst LA2016",
824 .cleanup = std_cleanup,
826 .dev_list = std_dev_list,
827 .dev_clear = std_dev_clear,
828 .config_get = config_get,
829 .config_set = config_set,
830 .config_list = config_list,
831 .dev_open = dev_open,
832 .dev_close = dev_close,
833 .dev_acquisition_start = dev_acquisition_start,
834 .dev_acquisition_stop = dev_acquisition_stop,
837 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);