2 * This file is part of the libsigrok project.
4 * Copyright (C) 2022 Gerhard Sittig <gerhard.sittig@gmx.net>
5 * Copyright (C) 2020 Florian Schmidt <schmidt_florian@gmx.de>
6 * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
7 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
8 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * This driver implementation initially was derived from the
26 * src/hardware/saleae-logic16/ source code.
31 #include <libsigrok/libsigrok.h>
34 #include "libsigrok-internal.h"
37 static const uint32_t scanopts[] = {
41 static const uint32_t drvopts[] = {
42 SR_CONF_LOGIC_ANALYZER,
43 SR_CONF_SIGNAL_GENERATOR,
46 static const uint32_t devopts[] = {
47 /* TODO: SR_CONF_CONTINUOUS, */
48 SR_CONF_CONN | SR_CONF_GET,
49 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
51 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
52 #if WITH_THRESHOLD_DEVCFG
53 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
56 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
59 static const uint32_t devopts_cg_logic[] = {
60 #if !WITH_THRESHOLD_DEVCFG
61 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
65 static const uint32_t devopts_cg_pwm[] = {
66 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
67 SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_SET,
68 SR_CONF_DUTY_CYCLE | SR_CONF_GET | SR_CONF_SET,
71 static const int32_t trigger_matches[] = {
78 static const char *channel_names_logic[] = {
79 "CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7",
80 "CH8", "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15",
81 "CH16", "CH17", "CH18", "CH19", "CH20", "CH21", "CH22", "CH23",
82 "CH24", "CH25", "CH26", "CH27", "CH28", "CH29", "CH30", "CH31",
85 static const char *channel_names_pwm[] = {
90 * The hardware uses a 100/200/500MHz base clock (model dependent) and
91 * a 16bit divider (common across all models). The range from 10kHz to
92 * 100/200/500MHz should be applicable to all devices. High rates may
93 * suffer from coarse resolution (e.g. in the "500MHz div 2" case) and
94 * may not provide the desired 1/2/5 steps. Fortunately this exclusively
95 * affects the 500MHz model where 250MHz is used instead of 200MHz and
96 * the 166MHz and 125MHz rates are not presented to users. Deep memory
97 * of these models and hardware compression reduce the necessity to let
98 * users pick from a huge list of possible rates.
102 static const uint64_t rates_500mhz[] = {
120 static const uint64_t rates_200mhz[] = {
137 static const uint64_t rates_100mhz[] = {
154 * Only list a few discrete voltages, to form a useful set which covers
155 * most logic families. Too many choices can make some applications use
156 * a slider again. Which may lack a scale for the current value, and
157 * leave users without feedback what the currently used value might be.
159 static const double threshold_ranges[][2] = {
164 { 1.4, 1.4, }, /* Default, 1.4V, index 4. */
169 #define LOGIC_THRESHOLD_IDX_DFLT 4
171 static double threshold_voltage(const struct sr_dev_inst *sdi, double *high)
173 struct dev_context *devc;
178 idx = devc->threshold_voltage_idx;
179 voltage = threshold_ranges[idx][0];
181 *high = threshold_ranges[idx][1];
186 /* Convenience. Release an allocated devc from error paths. */
187 static void kingst_la2016_free_devc(struct dev_context *devc)
191 g_free(devc->mcu_firmware);
192 g_free(devc->fpga_bitstream);
196 /* Convenience. Release an allocated sdi from error paths. */
197 static void kingst_la2016_free_sdi(struct sr_dev_inst *sdi)
203 g_free(sdi->version);
204 g_free(sdi->serial_num);
205 g_free(sdi->connection_id);
206 sr_usb_dev_inst_free(sdi->conn);
207 kingst_la2016_free_devc(sdi->priv);
210 /* Convenience. Open a USB device (including claiming an interface). */
211 static int la2016_open_usb(struct sr_usb_dev_inst *usb,
212 libusb_device *dev, gboolean show_message)
216 ret = libusb_open(dev, &usb->devhdl);
219 sr_err("Cannot open device: %s.",
220 libusb_error_name(ret));
225 if (usb->address == 0xff) {
227 * First encounter after firmware upload.
228 * Grab current address after enumeration.
230 usb->address = libusb_get_device_address(dev);
233 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
234 if (ret == LIBUSB_ERROR_BUSY) {
235 sr_err("Cannot claim USB interface. Another program or driver using it?");
237 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
238 sr_err("Device has been disconnected.");
240 } else if (ret != 0) {
241 sr_err("Cannot claim USB interface: %s.",
242 libusb_error_name(ret));
249 /* Convenience. Close an opened USB device (and release the interface). */
250 static void la2016_close_usb(struct sr_usb_dev_inst *usb)
257 libusb_release_interface(usb->devhdl, USB_INTERFACE);
258 libusb_close(usb->devhdl);
263 /* Communicate to an USB device to identify the Kingst LA model. */
264 static int la2016_identify_read(struct sr_dev_inst *sdi,
265 struct sr_usb_dev_inst *usb, libusb_device *dev,
266 gboolean show_message)
270 ret = la2016_open_usb(usb, dev, show_message);
273 sr_err("Cannot communicate to MCU firmware.");
278 * Also complete the hardware configuration (FPGA bitstream)
279 * when MCU firmware communication became operational. Either
280 * failure is considered fatal when probing for the device.
282 ret = la2016_identify_device(sdi, show_message);
284 ret = la2016_init_hardware(sdi);
287 la2016_close_usb(usb);
292 /* Find given conn_id in another USB enum. Identify Kingst LA model. */
293 static int la2016_identify_enum(struct sr_dev_inst *sdi)
295 struct sr_dev_driver *di;
296 struct drv_context *drvc;
297 struct sr_context *ctx;
298 libusb_device **devlist, *dev;
299 struct libusb_device_descriptor des;
301 size_t device_count, dev_idx;
308 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
315 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
316 dev = devlist[dev_idx];
317 libusb_get_device_descriptor(dev, &des);
318 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
320 if (des.iProduct != LA2016_IPRODUCT_INDEX)
322 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
325 if (strcmp(sdi->connection_id, conn_id) != 0)
327 id_ret = la2016_identify_read(sdi, sdi->conn, dev, FALSE);
330 libusb_free_device_list(devlist, 1);
335 /* Wait for a device to re-appear after firmware upload. */
336 static int la2016_identify_wait(struct sr_dev_inst *sdi)
338 struct dev_context *devc;
339 uint64_t reset_done, now, elapsed_ms;
344 sr_info("Waiting for device to reset after firmware upload.");
345 now = g_get_monotonic_time();
346 reset_done = devc->fw_uploaded + RENUM_GONE_DELAY_MS * 1000;
347 if (now < reset_done)
348 g_usleep(reset_done - now);
350 now = g_get_monotonic_time();
351 elapsed_ms = (now - devc->fw_uploaded) / 1000;
352 sr_spew("Waited %" PRIu64 "ms.", elapsed_ms);
353 ret = la2016_identify_enum(sdi);
355 devc->fw_uploaded = 0;
358 g_usleep(RENUM_POLL_INTERVAL_MS * 1000);
359 } while (elapsed_ms < RENUM_CHECK_PERIOD_MS);
361 sr_err("Device failed to re-enumerate.");
364 sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms);
370 * Open given conn_id from another USB enum. Used by dev_open(). Similar
371 * to, and should be kept in sync with la2016_identify_enum().
373 static int la2016_open_enum(struct sr_dev_inst *sdi)
375 struct sr_dev_driver *di;
376 struct drv_context *drvc;
377 struct sr_context *ctx;
378 libusb_device **devlist, *dev;
379 struct libusb_device_descriptor des;
381 size_t device_count, dev_idx;
388 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
394 open_ret = SR_ERR_IO;
395 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
396 dev = devlist[dev_idx];
397 libusb_get_device_descriptor(dev, &des);
398 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
400 if (des.iProduct != LA2016_IPRODUCT_INDEX)
402 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
405 if (strcmp(sdi->connection_id, conn_id) != 0)
407 open_ret = la2016_open_usb(sdi->conn, dev, TRUE);
410 libusb_free_device_list(devlist, 1);
415 static GSList *scan(struct sr_dev_driver *di, GSList *options)
417 struct drv_context *drvc;
418 struct sr_context *ctx;
419 struct dev_context *devc;
420 struct sr_dev_inst *sdi;
421 struct sr_usb_dev_inst *usb;
422 struct sr_config *src;
424 GSList *devices, *found_devices, *renum_devices;
425 GSList *conn_devices;
426 struct libusb_device_descriptor des;
427 libusb_device **devlist, *dev;
428 size_t dev_count, dev_idx, ch_idx;
434 size_t ch_off, ch_max;
435 struct sr_channel *ch;
436 struct sr_channel_group *cg;
443 for (l = options; l; l = l->next) {
447 conn = g_variant_get_string(src->data, NULL);
452 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
453 if (conn && !conn_devices) {
454 sr_err("Cannot find the specified connection '%s'.", conn);
459 * Find all LA2016 devices, optionally upload firmware to them.
460 * Defer completion of sdi/devc creation until all (selected)
461 * devices were found in a usable state, and their models got
462 * identified which affect their feature set. It appears that
463 * we cannot communicate to the device within the same USB enum
464 * cycle, needs another USB enumeration after firmware upload.
467 found_devices = NULL;
468 renum_devices = NULL;
469 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
471 sr_err("Cannot get device list: %s.", libusb_error_name(ret));
475 for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
476 dev = devlist[dev_idx];
477 bus = libusb_get_bus_number(dev);
478 addr = libusb_get_device_address(dev);
480 /* Filter by connection when externally specified. */
481 for (l = conn_devices; l; l = l->next) {
483 if (usb->bus == bus && usb->address == addr)
486 if (conn_devices && !l) {
487 sr_spew("Bus %hhu, addr %hhu do not match specified filter.",
492 /* Check USB VID:PID. Get the connection string. */
493 libusb_get_device_descriptor(dev, &des);
494 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
497 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
500 sr_dbg("USB enum found %04x:%04x at path %s, %d.%d.",
501 des.idVendor, des.idProduct, conn_id, bus, addr);
502 usb = sr_usb_dev_inst_new(bus, addr, NULL);
504 sdi = g_malloc0(sizeof(*sdi));
506 sdi->status = SR_ST_INITIALIZING;
507 sdi->inst_type = SR_INST_USB;
508 sdi->connection_id = g_strdup(conn_id);
511 devc = g_malloc0(sizeof(*devc));
515 * Load MCU firmware if it is currently missing. Which
516 * makes the device disappear and renumerate in USB.
517 * We need to come back another time to communicate to
520 devc->fw_uploaded = 0;
522 if (des.iProduct != LA2016_IPRODUCT_INDEX) {
523 sr_info("Uploading MCU firmware to '%s'.", conn_id);
524 ret = la2016_upload_firmware(sdi, ctx, dev, FALSE);
526 sr_err("MCU firmware upload failed.");
527 kingst_la2016_free_sdi(sdi);
530 devc->fw_uploaded = g_get_monotonic_time();
532 renum_devices = g_slist_append(renum_devices, sdi);
535 ret = la2016_upload_firmware(sdi, NULL, NULL, TRUE);
537 sr_err("MCU firmware filename check failed.");
538 kingst_la2016_free_sdi(sdi);
544 * Communicate to the MCU firmware to access EEPROM data
545 * which lets us identify the device type. Then stop, to
546 * share remaining sdi/devc creation with those devices
547 * which had their MCU firmware uploaded above and which
548 * get revisited later.
550 ret = la2016_identify_read(sdi, usb, dev, TRUE);
551 if (ret != SR_OK || !devc->model) {
552 sr_err("Unknown or unsupported device type.");
553 kingst_la2016_free_sdi(sdi);
556 found_devices = g_slist_append(found_devices, sdi);
558 libusb_free_device_list(devlist, 1);
559 g_slist_free_full(conn_devices, sr_usb_dev_inst_free_cb);
562 * Wait for devices to re-appear after firmware upload. Append
563 * the yet unidentified device to the list of found devices, or
564 * release the previously allocated sdi/devc.
566 for (l = renum_devices; l; l = l->next) {
569 ret = la2016_identify_wait(sdi);
570 if (ret != SR_OK || !devc->model) {
571 sr_dbg("Skipping unusable '%s'.", sdi->connection_id);
572 kingst_la2016_free_sdi(sdi);
575 found_devices = g_slist_append(found_devices, sdi);
577 g_slist_free(renum_devices);
580 * All found devices got identified, their type is known here.
581 * Complete the sdi/devc creation. Assign default settings
582 * because the vendor firmware would not let us read back the
583 * previously written configuration.
585 for (l = found_devices; l; l = l->next) {
589 sdi->vendor = g_strdup("Kingst");
590 sdi->model = g_strdup(devc->model->name);
593 /* Create the "Logic" channel group. */
594 ch_max = ARRAY_SIZE(channel_names_logic);
595 if (ch_max > devc->model->channel_count)
596 ch_max = devc->model->channel_count;
597 cg = sr_channel_group_new(sdi, "Logic", NULL);
599 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
600 ch = sr_channel_new(sdi, ch_off,
601 SR_CHANNEL_LOGIC, TRUE,
602 channel_names_logic[ch_idx]);
604 cg->channels = g_slist_append(cg->channels, ch);
607 /* Create the "PWMx" channel groups. */
608 ch_max = ARRAY_SIZE(channel_names_pwm);
609 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
611 name = channel_names_pwm[ch_idx];
612 cg = sr_channel_group_new(sdi, name, NULL);
615 ch = sr_channel_new(sdi, ch_off,
616 SR_CHANNEL_ANALOG, FALSE, name);
618 cg->channels = g_slist_append(cg->channels, ch);
622 * Ideally we'd get the previous configuration from the
623 * hardware, but this device is write-only. So we have
624 * to assign a fixed set of initial configuration values.
626 sr_sw_limits_init(&devc->sw_limits);
627 devc->sw_limits.limit_samples = 0;
628 devc->capture_ratio = 50;
629 devc->samplerate = devc->model->samplerate;
630 devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_DFLT;
631 if (ARRAY_SIZE(devc->pwm_setting) >= 1) {
632 devc->pwm_setting[0].enabled = FALSE;
633 devc->pwm_setting[0].freq = SR_KHZ(1);
634 devc->pwm_setting[0].duty = 50;
636 if (ARRAY_SIZE(devc->pwm_setting) >= 2) {
637 devc->pwm_setting[1].enabled = FALSE;
638 devc->pwm_setting[1].freq = SR_KHZ(100);
639 devc->pwm_setting[1].duty = 50;
642 sdi->status = SR_ST_INACTIVE;
643 devices = g_slist_append(devices, sdi);
645 g_slist_free(found_devices);
647 return std_scan_complete(di, devices);
650 static int dev_open(struct sr_dev_inst *sdi)
652 struct dev_context *devc;
658 ret = la2016_open_enum(sdi);
660 sr_err("Cannot open device.");
664 /* Send most recent PWM configuration to the device. */
665 for (ch = 0; ch < ARRAY_SIZE(devc->pwm_setting); ch++) {
666 ret = la2016_write_pwm_config(sdi, ch);
674 static int dev_close(struct sr_dev_inst *sdi)
676 struct sr_usb_dev_inst *usb;
683 if (WITH_DEINIT_IN_CLOSE)
684 la2016_deinit_hardware(sdi);
686 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
687 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
688 la2016_close_usb(sdi->conn);
693 /* Config API helper. Get type and index of a channel group. */
694 static int get_cg_index(const struct sr_dev_inst *sdi,
695 const struct sr_channel_group *cg,
696 int *type, size_t *logic, size_t *analog)
698 struct dev_context *devc;
702 /* Preset return values. */
710 /* Start categorizing the received cg. */
716 l = sdi->channel_groups;
718 /* First sdi->channelgroups item is "Logic". */
723 *type = SR_CHANNEL_LOGIC;
730 /* Next sdi->channelgroups items are "PWMx". */
732 while (l && l->data != cg) {
736 if (l && idx < ARRAY_SIZE(devc->pwm_setting)) {
738 *type = SR_CHANNEL_ANALOG;
747 static int config_get(uint32_t key, GVariant **data,
748 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
750 struct dev_context *devc;
752 size_t logic_idx, analog_idx;
753 struct pwm_setting *pwm;
754 struct sr_usb_dev_inst *usb;
755 double voltage, rounded;
764 /* Check for types (and index) of channel groups. */
765 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
766 if (cg && ret != SR_OK)
769 /* Handle requests for the "Logic" channel group. */
770 if (cg && cg_type == SR_CHANNEL_LOGIC) {
772 #if !WITH_THRESHOLD_DEVCFG
773 case SR_CONF_VOLTAGE_THRESHOLD:
774 voltage = threshold_voltage(sdi, NULL);
775 *data = std_gvar_tuple_double(voltage, voltage);
777 #endif /* WITH_THRESHOLD_DEVCFG */
784 /* Handle requests for the "PWMx" channel groups. */
785 if (cg && cg_type == SR_CHANNEL_ANALOG) {
786 pwm = &devc->pwm_setting[analog_idx];
788 case SR_CONF_ENABLED:
789 *data = g_variant_new_boolean(pwm->enabled);
791 case SR_CONF_OUTPUT_FREQUENCY:
792 *data = g_variant_new_double(pwm->freq);
794 case SR_CONF_DUTY_CYCLE:
795 *data = g_variant_new_double(pwm->duty);
806 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
808 case SR_CONF_SAMPLERATE:
809 *data = g_variant_new_uint64(devc->samplerate);
811 case SR_CONF_LIMIT_SAMPLES:
812 case SR_CONF_LIMIT_MSEC:
813 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
814 case SR_CONF_CAPTURE_RATIO:
815 *data = g_variant_new_uint64(devc->capture_ratio);
817 #if WITH_THRESHOLD_DEVCFG
818 case SR_CONF_VOLTAGE_THRESHOLD:
819 voltage = threshold_voltage(sdi, NULL);
820 *data = std_gvar_tuple_double(voltage, voltage);
822 #endif /* WITH_THRESHOLD_DEVCFG */
830 static int config_set(uint32_t key, GVariant *data,
831 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
833 struct dev_context *devc;
835 size_t logic_idx, analog_idx;
836 struct pwm_setting *pwm;
842 /* Check for types (and index) of channel groups. */
843 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
844 if (cg && ret != SR_OK)
847 /* Handle requests for the "Logic" channel group. */
848 if (cg && cg_type == SR_CHANNEL_LOGIC) {
850 #if !WITH_THRESHOLD_DEVCFG
851 case SR_CONF_LOGIC_THRESHOLD:
852 idx = std_double_tuple_idx(data,
853 ARRAY_AND_SIZE(threshold_ranges));
856 devc->threshold_voltage_idx = idx;
858 #endif /* WITH_THRESHOLD_DEVCFG */
865 /* Handle requests for the "PWMx" channel groups. */
866 if (cg && cg_type == SR_CHANNEL_ANALOG) {
867 pwm = &devc->pwm_setting[analog_idx];
869 case SR_CONF_ENABLED:
870 pwm->enabled = g_variant_get_boolean(data);
871 ret = la2016_write_pwm_config(sdi, analog_idx);
875 case SR_CONF_OUTPUT_FREQUENCY:
876 value_f = g_variant_get_double(data);
877 if (value_f <= 0.0 || value_f > MAX_PWM_FREQ)
880 ret = la2016_write_pwm_config(sdi, analog_idx);
884 case SR_CONF_DUTY_CYCLE:
885 value_f = g_variant_get_double(data);
886 if (value_f <= 0.0 || value_f > 100.0)
889 ret = la2016_write_pwm_config(sdi, analog_idx);
900 case SR_CONF_SAMPLERATE:
901 devc->samplerate = g_variant_get_uint64(data);
903 case SR_CONF_LIMIT_SAMPLES:
904 case SR_CONF_LIMIT_MSEC:
905 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
906 case SR_CONF_CAPTURE_RATIO:
907 devc->capture_ratio = g_variant_get_uint64(data);
909 #if WITH_THRESHOLD_DEVCFG
910 case SR_CONF_VOLTAGE_THRESHOLD:
911 idx = std_double_tuple_idx(data,
912 ARRAY_AND_SIZE(threshold_ranges));
915 devc->threshold_voltage_idx = idx;
917 #endif /* WITH_THRESHOLD_DEVCFG */
925 static int config_list(uint32_t key, GVariant **data,
926 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
928 struct dev_context *devc;
930 size_t logic_idx, analog_idx;
932 devc = sdi ? sdi->priv : NULL;
934 /* Check for types (and index) of channel groups. */
935 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
936 if (cg && ret != SR_OK)
939 /* Handle requests for the "Logic" channel group. */
940 if (cg && cg_type == SR_CHANNEL_LOGIC) {
942 case SR_CONF_DEVICE_OPTIONS:
943 if (ARRAY_SIZE(devopts_cg_logic) == 0)
945 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
946 devopts_cg_logic, ARRAY_SIZE(devopts_cg_logic),
947 sizeof(devopts_cg_logic[0]));
949 #if !WITH_THRESHOLD_DEVCFG
950 case SR_CONF_VOLTAGE_THRESHOLD:
951 *data = std_gvar_thresholds(ARRAY_AND_SIZE(threshold_ranges));
953 #endif /* WITH_THRESHOLD_DEVCFG */
960 /* Handle requests for the "PWMx" channel groups. */
961 if (cg && cg_type == SR_CHANNEL_ANALOG) {
963 case SR_CONF_DEVICE_OPTIONS:
964 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
965 devopts_cg_pwm, ARRAY_SIZE(devopts_cg_pwm),
966 sizeof(devopts_cg_pwm[0]));
975 case SR_CONF_SCAN_OPTIONS:
976 case SR_CONF_DEVICE_OPTIONS:
977 return STD_CONFIG_LIST(key, data, sdi, cg,
978 scanopts, drvopts, devopts);
979 case SR_CONF_SAMPLERATE:
982 if (devc->model->samplerate == SR_MHZ(500))
983 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_500mhz));
984 else if (devc->model->samplerate == SR_MHZ(200))
985 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_200mhz));
986 else if (devc->model->samplerate == SR_MHZ(100))
987 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_100mhz));
991 case SR_CONF_LIMIT_SAMPLES:
992 *data = std_gvar_tuple_u64(0, LA2016_NUM_SAMPLES_MAX);
994 #if WITH_THRESHOLD_DEVCFG
995 case SR_CONF_VOLTAGE_THRESHOLD:
996 *data = std_gvar_thresholds(ARRAY_AND_SIZE(threshold_ranges));
998 #endif /* WITH_THRESHOLD_DEVCFG */
999 case SR_CONF_TRIGGER_MATCH:
1000 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
1009 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
1011 struct sr_dev_driver *di;
1012 struct drv_context *drvc;
1013 struct sr_context *ctx;
1014 struct dev_context *devc;
1021 ctx = drvc->sr_ctx;;
1024 if (!devc->feed_queue) {
1025 if (devc->model->channel_count == 32)
1026 unitsize = sizeof(uint32_t);
1027 else if (devc->model->channel_count == 16)
1028 unitsize = sizeof(uint16_t);
1031 devc->feed_queue = feed_queue_logic_alloc(sdi,
1032 LA2016_CONVBUFFER_SIZE, unitsize);
1033 if (!devc->feed_queue) {
1034 sr_err("Cannot allocate buffer for session feed.");
1035 return SR_ERR_MALLOC;
1037 devc->packets_per_chunk = TRANSFER_PACKET_LENGTH;
1038 devc->packets_per_chunk--;
1039 devc->packets_per_chunk /= unitsize + sizeof(uint8_t);
1042 sr_sw_limits_acquisition_start(&devc->sw_limits);
1044 voltage = threshold_voltage(sdi, NULL);
1045 ret = la2016_setup_acquisition(sdi, voltage);
1047 feed_queue_logic_free(devc->feed_queue);
1048 devc->feed_queue = NULL;
1052 ret = la2016_start_acquisition(sdi);
1054 la2016_abort_acquisition(sdi);
1055 feed_queue_logic_free(devc->feed_queue);
1056 devc->feed_queue = NULL;
1060 devc->completion_seen = FALSE;
1061 usb_source_add(sdi->session, ctx, 50,
1062 la2016_receive_data, (void *)sdi);
1064 std_session_send_df_header(sdi);
1069 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
1073 ret = la2016_abort_acquisition(sdi);
1078 static struct sr_dev_driver kingst_la2016_driver_info = {
1079 .name = "kingst-la2016",
1080 .longname = "Kingst LA2016",
1083 .cleanup = std_cleanup,
1085 .dev_list = std_dev_list,
1086 .dev_clear = std_dev_clear,
1087 .config_get = config_get,
1088 .config_set = config_set,
1089 .config_list = config_list,
1090 .dev_open = dev_open,
1091 .dev_close = dev_close,
1092 .dev_acquisition_start = dev_acquisition_start,
1093 .dev_acquisition_stop = dev_acquisition_stop,
1096 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);