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,
42 SR_CONF_SIGNAL_GENERATOR,
45 static const uint32_t devopts[] = {
46 /* TODO: SR_CONF_CONTINUOUS, */
47 SR_CONF_CONN | SR_CONF_GET,
48 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
49 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
51 #if WITH_THRESHOLD_DEVCFG
52 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
54 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
55 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
58 static const uint32_t devopts_cg_logic[] = {
59 #if !WITH_THRESHOLD_DEVCFG
60 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
64 static const uint32_t devopts_cg_pwm[] = {
65 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
66 SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_SET,
67 SR_CONF_DUTY_CYCLE | SR_CONF_GET | SR_CONF_SET,
70 static const int32_t trigger_matches[] = {
77 static const char *channel_names_logic[] = {
78 "CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7",
79 "CH8", "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15",
80 "CH16", "CH17", "CH18", "CH19", "CH20", "CH21", "CH22", "CH23",
81 "CH24", "CH25", "CH26", "CH27", "CH28", "CH29", "CH30", "CH31",
84 static const char *channel_names_pwm[] = {
89 * The hardware uses a 100/200/500MHz base clock (model dependent) and
90 * a 16bit divider (common across all models). The range from 10kHz to
91 * 100/200/500MHz should be applicable to all devices. High rates may
92 * suffer from coarse resolution (e.g. in the "500MHz div 2" case) and
93 * may not provide the desired 1/2/5 steps. Fortunately this exclusively
94 * affects the 500MHz model where 250MHz is used instead of 200MHz and
95 * the 166MHz and 125MHz rates are not presented to users. Deep memory
96 * of these models and hardware compression reduce the necessity to let
97 * users pick from a huge list of possible rates.
101 static const uint64_t rates_500mhz[] = {
119 static const uint64_t rates_200mhz[] = {
136 static const uint64_t rates_100mhz[] = {
153 * Only list a few discrete voltages, to form a useful set which covers
154 * most logic families. Too many choices can make some applications use
155 * a slider again. Which may lack a scale for the current value, and
156 * leave users without feedback what the currently used value might be.
158 static const double threshold_ranges[][2] = {
163 { 1.4, 1.4, }, /* Default, 1.4V, index 4. */
168 #define LOGIC_THRESHOLD_IDX_DFLT 4
170 static double threshold_voltage(const struct sr_dev_inst *sdi, double *high)
172 struct dev_context *devc;
177 idx = devc->threshold_voltage_idx;
178 voltage = threshold_ranges[idx][0];
180 *high = threshold_ranges[idx][1];
185 /* Convenience. Release an allocated devc from error paths. */
186 static void kingst_la2016_free_devc(struct dev_context *devc)
190 g_free(devc->mcu_firmware);
191 g_free(devc->fpga_bitstream);
195 /* Convenience. Release an allocated sdi from error paths. */
196 static void kingst_la2016_free_sdi(struct sr_dev_inst *sdi)
202 g_free(sdi->version);
203 g_free(sdi->serial_num);
204 g_free(sdi->connection_id);
205 sr_usb_dev_inst_free(sdi->conn);
206 kingst_la2016_free_devc(sdi->priv);
209 /* Convenience. Open a USB device (including claiming an interface). */
210 static int la2016_open_usb(struct sr_usb_dev_inst *usb,
211 libusb_device *dev, gboolean show_message)
215 ret = libusb_open(dev, &usb->devhdl);
218 sr_err("Cannot open device: %s.",
219 libusb_error_name(ret));
224 if (usb->address == 0xff) {
226 * First encounter after firmware upload.
227 * Grab current address after enumeration.
229 usb->address = libusb_get_device_address(dev);
232 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
233 if (ret == LIBUSB_ERROR_BUSY) {
234 sr_err("Cannot claim USB interface. Another program or driver using it?");
236 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
237 sr_err("Device has been disconnected.");
239 } else if (ret != 0) {
240 sr_err("Cannot claim USB interface: %s.",
241 libusb_error_name(ret));
248 /* Convenience. Close an opened USB device (and release the interface). */
249 static void la2016_close_usb(struct sr_usb_dev_inst *usb)
256 libusb_release_interface(usb->devhdl, USB_INTERFACE);
257 libusb_close(usb->devhdl);
262 /* Communicate to an USB device to identify the Kingst LA model. */
263 static int la2016_identify_read(struct sr_dev_inst *sdi,
264 struct sr_usb_dev_inst *usb, libusb_device *dev,
265 gboolean show_message)
269 ret = la2016_open_usb(usb, dev, show_message);
272 sr_err("Cannot communicate to MCU firmware.");
277 * Also complete the hardware configuration (FPGA bitstream)
278 * when MCU firmware communication became operational. Either
279 * failure is considered fatal when probing for the device.
281 ret = la2016_identify_device(sdi, show_message);
283 ret = la2016_init_hardware(sdi);
286 la2016_close_usb(usb);
291 /* Find given conn_id in another USB enum. Identify Kingst LA model. */
292 static int la2016_identify_enum(struct sr_dev_inst *sdi)
294 struct sr_dev_driver *di;
295 struct drv_context *drvc;
296 struct sr_context *ctx;
297 libusb_device **devlist, *dev;
298 struct libusb_device_descriptor des;
300 size_t device_count, dev_idx;
307 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
314 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
315 dev = devlist[dev_idx];
316 libusb_get_device_descriptor(dev, &des);
317 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
319 if (des.iProduct != LA2016_IPRODUCT_INDEX)
321 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
324 if (strcmp(sdi->connection_id, conn_id) != 0)
326 id_ret = la2016_identify_read(sdi, sdi->conn, dev, FALSE);
329 libusb_free_device_list(devlist, 1);
334 /* Wait for a device to re-appear after firmware upload. */
335 static int la2016_identify_wait(struct sr_dev_inst *sdi)
337 struct dev_context *devc;
338 uint64_t reset_done, now, elapsed_ms;
343 sr_info("Waiting for device to reset after firmware upload.");
344 now = g_get_monotonic_time();
345 reset_done = devc->fw_uploaded + RENUM_GONE_DELAY_MS * 1000;
346 if (now < reset_done)
347 g_usleep(reset_done - now);
349 now = g_get_monotonic_time();
350 elapsed_ms = (now - devc->fw_uploaded) / 1000;
351 sr_spew("Waited %" PRIu64 "ms.", elapsed_ms);
352 ret = la2016_identify_enum(sdi);
354 devc->fw_uploaded = 0;
357 g_usleep(RENUM_POLL_INTERVAL_MS * 1000);
358 } while (elapsed_ms < RENUM_CHECK_PERIOD_MS);
360 sr_err("Device failed to re-enumerate.");
363 sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms);
369 * Open given conn_id from another USB enum. Used by dev_open(). Similar
370 * to, and should be kept in sync with la2016_identify_enum().
372 static int la2016_open_enum(struct sr_dev_inst *sdi)
374 struct sr_dev_driver *di;
375 struct drv_context *drvc;
376 struct sr_context *ctx;
377 libusb_device **devlist, *dev;
378 struct libusb_device_descriptor des;
380 size_t device_count, dev_idx;
387 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
393 open_ret = SR_ERR_IO;
394 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
395 dev = devlist[dev_idx];
396 libusb_get_device_descriptor(dev, &des);
397 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
399 if (des.iProduct != LA2016_IPRODUCT_INDEX)
401 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
404 if (strcmp(sdi->connection_id, conn_id) != 0)
406 open_ret = la2016_open_usb(sdi->conn, dev, TRUE);
409 libusb_free_device_list(devlist, 1);
414 static GSList *scan(struct sr_dev_driver *di, GSList *options)
416 struct drv_context *drvc;
417 struct sr_context *ctx;
418 struct dev_context *devc;
419 struct sr_dev_inst *sdi;
420 struct sr_usb_dev_inst *usb;
421 struct sr_config *src;
423 GSList *devices, *found_devices, *renum_devices;
424 GSList *conn_devices;
425 struct libusb_device_descriptor des;
426 libusb_device **devlist, *dev;
427 size_t dev_count, dev_idx, ch_idx;
433 size_t ch_off, ch_max;
434 struct sr_channel *ch;
435 struct sr_channel_group *cg;
442 for (l = options; l; l = l->next) {
446 conn = g_variant_get_string(src->data, NULL);
451 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
452 if (conn && !conn_devices) {
453 sr_err("Cannot find the specified connection '%s'.", conn);
458 * Find all LA2016 devices, optionally upload firmware to them.
459 * Defer completion of sdi/devc creation until all (selected)
460 * devices were found in a usable state, and their models got
461 * identified which affect their feature set. It appears that
462 * we cannot communicate to the device within the same USB enum
463 * cycle, needs another USB enumeration after firmware upload.
466 found_devices = NULL;
467 renum_devices = NULL;
468 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
470 sr_err("Cannot get device list: %s.", libusb_error_name(ret));
474 for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
475 dev = devlist[dev_idx];
476 bus = libusb_get_bus_number(dev);
477 addr = libusb_get_device_address(dev);
479 /* Filter by connection when externally specified. */
480 for (l = conn_devices; l; l = l->next) {
482 if (usb->bus == bus && usb->address == addr)
485 if (conn_devices && !l) {
486 sr_spew("Bus %hhu, addr %hhu do not match specified filter.",
491 /* Check USB VID:PID. Get the connection string. */
492 libusb_get_device_descriptor(dev, &des);
493 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
496 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
499 sr_dbg("USB enum found %04x:%04x at path %s, %d.%d.",
500 des.idVendor, des.idProduct, conn_id, bus, addr);
501 usb = sr_usb_dev_inst_new(bus, addr, NULL);
503 sdi = g_malloc0(sizeof(*sdi));
505 sdi->status = SR_ST_INITIALIZING;
506 sdi->inst_type = SR_INST_USB;
507 sdi->connection_id = g_strdup(conn_id);
510 devc = g_malloc0(sizeof(*devc));
514 * Load MCU firmware if it is currently missing. Which
515 * makes the device disappear and renumerate in USB.
516 * We need to come back another time to communicate to
519 devc->fw_uploaded = 0;
521 if (des.iProduct != LA2016_IPRODUCT_INDEX) {
522 sr_info("Uploading MCU firmware to '%s'.", conn_id);
523 ret = la2016_upload_firmware(sdi, ctx, dev, FALSE);
525 sr_err("MCU firmware upload failed.");
526 kingst_la2016_free_sdi(sdi);
529 devc->fw_uploaded = g_get_monotonic_time();
531 renum_devices = g_slist_append(renum_devices, sdi);
534 ret = la2016_upload_firmware(sdi, NULL, NULL, TRUE);
536 sr_err("MCU firmware filename check failed.");
537 kingst_la2016_free_sdi(sdi);
543 * Communicate to the MCU firmware to access EEPROM data
544 * which lets us identify the device type. Then stop, to
545 * share remaining sdi/devc creation with those devices
546 * which had their MCU firmware uploaded above and which
547 * get revisited later.
549 ret = la2016_identify_read(sdi, usb, dev, TRUE);
550 if (ret != SR_OK || !devc->model) {
551 sr_err("Unknown or unsupported device type.");
552 kingst_la2016_free_sdi(sdi);
555 found_devices = g_slist_append(found_devices, sdi);
557 libusb_free_device_list(devlist, 1);
558 g_slist_free_full(conn_devices, sr_usb_dev_inst_free_cb);
561 * Wait for devices to re-appear after firmware upload. Append
562 * the yet unidentified device to the list of found devices, or
563 * release the previously allocated sdi/devc.
565 for (l = renum_devices; l; l = l->next) {
568 ret = la2016_identify_wait(sdi);
569 if (ret != SR_OK || !devc->model) {
570 sr_dbg("Skipping unusable '%s'.", sdi->connection_id);
571 kingst_la2016_free_sdi(sdi);
574 found_devices = g_slist_append(found_devices, sdi);
576 g_slist_free(renum_devices);
579 * All found devices got identified, their type is known here.
580 * Complete the sdi/devc creation. Assign default settings
581 * because the vendor firmware would not let us read back the
582 * previously written configuration.
584 for (l = found_devices; l; l = l->next) {
588 sdi->vendor = g_strdup("Kingst");
589 sdi->model = g_strdup(devc->model->name);
592 /* Create the "Logic" channel group. */
593 ch_max = ARRAY_SIZE(channel_names_logic);
594 if (ch_max > devc->model->channel_count)
595 ch_max = devc->model->channel_count;
596 cg = sr_channel_group_new(sdi, "Logic", NULL);
598 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
599 ch = sr_channel_new(sdi, ch_off,
600 SR_CHANNEL_LOGIC, TRUE,
601 channel_names_logic[ch_idx]);
603 cg->channels = g_slist_append(cg->channels, ch);
606 /* Create the "PWMx" channel groups. */
607 ch_max = ARRAY_SIZE(channel_names_pwm);
608 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
610 name = channel_names_pwm[ch_idx];
611 cg = sr_channel_group_new(sdi, name, NULL);
614 ch = sr_channel_new(sdi, ch_off,
615 SR_CHANNEL_ANALOG, FALSE, name);
617 cg->channels = g_slist_append(cg->channels, ch);
621 * Ideally we'd get the previous configuration from the
622 * hardware, but this device is write-only. So we have
623 * to assign a fixed set of initial configuration values.
625 sr_sw_limits_init(&devc->sw_limits);
626 devc->sw_limits.limit_samples = 0;
627 devc->capture_ratio = 50;
628 devc->samplerate = devc->model->samplerate;
629 devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_DFLT;
630 if (ARRAY_SIZE(devc->pwm_setting) >= 1) {
631 devc->pwm_setting[0].enabled = FALSE;
632 devc->pwm_setting[0].freq = SR_KHZ(1);
633 devc->pwm_setting[0].duty = 50;
635 if (ARRAY_SIZE(devc->pwm_setting) >= 2) {
636 devc->pwm_setting[1].enabled = FALSE;
637 devc->pwm_setting[1].freq = SR_KHZ(100);
638 devc->pwm_setting[1].duty = 50;
641 sdi->status = SR_ST_INACTIVE;
642 devices = g_slist_append(devices, sdi);
644 g_slist_free(found_devices);
646 return std_scan_complete(di, devices);
649 static int dev_open(struct sr_dev_inst *sdi)
651 struct dev_context *devc;
657 ret = la2016_open_enum(sdi);
659 sr_err("Cannot open device.");
663 /* Send most recent PWM configuration to the device. */
664 for (ch = 0; ch < ARRAY_SIZE(devc->pwm_setting); ch++) {
665 ret = la2016_write_pwm_config(sdi, ch);
673 static int dev_close(struct sr_dev_inst *sdi)
675 struct sr_usb_dev_inst *usb;
682 if (WITH_DEINIT_IN_CLOSE)
683 la2016_deinit_hardware(sdi);
685 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
686 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
687 la2016_close_usb(sdi->conn);
692 /* Config API helper. Get type and index of a channel group. */
693 static int get_cg_index(const struct sr_dev_inst *sdi,
694 const struct sr_channel_group *cg,
695 int *type, size_t *logic, size_t *analog)
697 struct dev_context *devc;
701 /* Preset return values. */
709 /* Start categorizing the received cg. */
715 l = sdi->channel_groups;
717 /* First sdi->channelgroups item is "Logic". */
722 *type = SR_CHANNEL_LOGIC;
729 /* Next sdi->channelgroups items are "PWMx". */
731 while (l && l->data != cg) {
735 if (l && idx < ARRAY_SIZE(devc->pwm_setting)) {
737 *type = SR_CHANNEL_ANALOG;
746 static int config_get(uint32_t key, GVariant **data,
747 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
749 struct dev_context *devc;
751 size_t logic_idx, analog_idx;
752 struct pwm_setting *pwm;
753 struct sr_usb_dev_inst *usb;
754 double voltage, rounded;
763 /* Check for types (and index) of channel groups. */
764 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
765 if (cg && ret != SR_OK)
768 /* Handle requests for the "Logic" channel group. */
769 if (cg && cg_type == SR_CHANNEL_LOGIC) {
771 #if !WITH_THRESHOLD_DEVCFG
772 case SR_CONF_VOLTAGE_THRESHOLD:
773 voltage = threshold_voltage(sdi, NULL);
774 *data = std_gvar_tuple_double(voltage, voltage);
776 #endif /* WITH_THRESHOLD_DEVCFG */
783 /* Handle requests for the "PWMx" channel groups. */
784 if (cg && cg_type == SR_CHANNEL_ANALOG) {
785 pwm = &devc->pwm_setting[analog_idx];
787 case SR_CONF_ENABLED:
788 *data = g_variant_new_boolean(pwm->enabled);
790 case SR_CONF_OUTPUT_FREQUENCY:
791 *data = g_variant_new_double(pwm->freq);
793 case SR_CONF_DUTY_CYCLE:
794 *data = g_variant_new_double(pwm->duty);
805 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
807 case SR_CONF_SAMPLERATE:
808 *data = g_variant_new_uint64(devc->samplerate);
810 case SR_CONF_LIMIT_SAMPLES:
811 case SR_CONF_LIMIT_MSEC:
812 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
813 case SR_CONF_CAPTURE_RATIO:
814 *data = g_variant_new_uint64(devc->capture_ratio);
816 #if WITH_THRESHOLD_DEVCFG
817 case SR_CONF_VOLTAGE_THRESHOLD:
818 voltage = threshold_voltage(sdi, NULL);
819 *data = std_gvar_tuple_double(voltage, voltage);
821 #endif /* WITH_THRESHOLD_DEVCFG */
829 static int config_set(uint32_t key, GVariant *data,
830 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
832 struct dev_context *devc;
834 size_t logic_idx, analog_idx;
835 struct pwm_setting *pwm;
841 /* Check for types (and index) of channel groups. */
842 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
843 if (cg && ret != SR_OK)
846 /* Handle requests for the "Logic" channel group. */
847 if (cg && cg_type == SR_CHANNEL_LOGIC) {
849 #if !WITH_THRESHOLD_DEVCFG
850 case SR_CONF_LOGIC_THRESHOLD:
851 idx = std_double_tuple_idx(data,
852 ARRAY_AND_SIZE(threshold_ranges));
855 devc->threshold_voltage_idx = idx;
857 #endif /* WITH_THRESHOLD_DEVCFG */
864 /* Handle requests for the "PWMx" channel groups. */
865 if (cg && cg_type == SR_CHANNEL_ANALOG) {
866 pwm = &devc->pwm_setting[analog_idx];
868 case SR_CONF_ENABLED:
869 pwm->enabled = g_variant_get_boolean(data);
870 ret = la2016_write_pwm_config(sdi, analog_idx);
874 case SR_CONF_OUTPUT_FREQUENCY:
875 value_f = g_variant_get_double(data);
876 if (value_f <= 0.0 || value_f > MAX_PWM_FREQ)
879 ret = la2016_write_pwm_config(sdi, analog_idx);
883 case SR_CONF_DUTY_CYCLE:
884 value_f = g_variant_get_double(data);
885 if (value_f <= 0.0 || value_f > 100.0)
888 ret = la2016_write_pwm_config(sdi, analog_idx);
899 case SR_CONF_SAMPLERATE:
900 devc->samplerate = g_variant_get_uint64(data);
902 case SR_CONF_LIMIT_SAMPLES:
903 case SR_CONF_LIMIT_MSEC:
904 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
905 case SR_CONF_CAPTURE_RATIO:
906 devc->capture_ratio = g_variant_get_uint64(data);
908 #if WITH_THRESHOLD_DEVCFG
909 case SR_CONF_VOLTAGE_THRESHOLD:
910 idx = std_double_tuple_idx(data,
911 ARRAY_AND_SIZE(threshold_ranges));
914 devc->threshold_voltage_idx = idx;
916 #endif /* WITH_THRESHOLD_DEVCFG */
924 static int config_list(uint32_t key, GVariant **data,
925 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
927 struct dev_context *devc;
929 size_t logic_idx, analog_idx;
931 devc = sdi ? sdi->priv : NULL;
933 /* Check for types (and index) of channel groups. */
934 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
935 if (cg && ret != SR_OK)
938 /* Handle requests for the "Logic" channel group. */
939 if (cg && cg_type == SR_CHANNEL_LOGIC) {
941 case SR_CONF_DEVICE_OPTIONS:
942 if (ARRAY_SIZE(devopts_cg_logic) == 0)
944 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
945 devopts_cg_logic, ARRAY_SIZE(devopts_cg_logic),
946 sizeof(devopts_cg_logic[0]));
948 #if !WITH_THRESHOLD_DEVCFG
949 case SR_CONF_VOLTAGE_THRESHOLD:
950 *data = std_gvar_thresholds(ARRAY_AND_SIZE(threshold_ranges));
952 #endif /* WITH_THRESHOLD_DEVCFG */
959 /* Handle requests for the "PWMx" channel groups. */
960 if (cg && cg_type == SR_CHANNEL_ANALOG) {
962 case SR_CONF_DEVICE_OPTIONS:
963 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
964 devopts_cg_pwm, ARRAY_SIZE(devopts_cg_pwm),
965 sizeof(devopts_cg_pwm[0]));
974 case SR_CONF_SCAN_OPTIONS:
975 case SR_CONF_DEVICE_OPTIONS:
976 return STD_CONFIG_LIST(key, data, sdi, cg,
977 scanopts, drvopts, devopts);
978 case SR_CONF_SAMPLERATE:
981 if (devc->model->samplerate == SR_MHZ(500))
982 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_500mhz));
983 else if (devc->model->samplerate == SR_MHZ(200))
984 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_200mhz));
985 else if (devc->model->samplerate == SR_MHZ(100))
986 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_100mhz));
990 case SR_CONF_LIMIT_SAMPLES:
991 *data = std_gvar_tuple_u64(0, LA2016_NUM_SAMPLES_MAX);
993 #if WITH_THRESHOLD_DEVCFG
994 case SR_CONF_VOLTAGE_THRESHOLD:
995 *data = std_gvar_thresholds(ARRAY_AND_SIZE(threshold_ranges));
997 #endif /* WITH_THRESHOLD_DEVCFG */
998 case SR_CONF_TRIGGER_MATCH:
999 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
1008 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
1010 struct sr_dev_driver *di;
1011 struct drv_context *drvc;
1012 struct sr_context *ctx;
1013 struct dev_context *devc;
1020 ctx = drvc->sr_ctx;;
1023 if (!devc->feed_queue) {
1024 if (devc->model->channel_count == 32)
1025 unitsize = sizeof(uint32_t);
1026 else if (devc->model->channel_count == 16)
1027 unitsize = sizeof(uint16_t);
1030 devc->feed_queue = feed_queue_logic_alloc(sdi,
1031 LA2016_CONVBUFFER_SIZE, unitsize);
1032 if (!devc->feed_queue) {
1033 sr_err("Cannot allocate buffer for session feed.");
1034 return SR_ERR_MALLOC;
1036 devc->packets_per_chunk = TRANSFER_PACKET_LENGTH;
1037 devc->packets_per_chunk--;
1038 devc->packets_per_chunk /= unitsize + sizeof(uint8_t);
1041 sr_sw_limits_acquisition_start(&devc->sw_limits);
1043 voltage = threshold_voltage(sdi, NULL);
1044 ret = la2016_setup_acquisition(sdi, voltage);
1046 feed_queue_logic_free(devc->feed_queue);
1047 devc->feed_queue = NULL;
1051 ret = la2016_start_acquisition(sdi);
1053 la2016_abort_acquisition(sdi);
1054 feed_queue_logic_free(devc->feed_queue);
1055 devc->feed_queue = NULL;
1059 devc->completion_seen = FALSE;
1060 usb_source_add(sdi->session, ctx, 50,
1061 la2016_receive_data, (void *)sdi);
1063 std_session_send_df_header(sdi);
1068 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
1072 ret = la2016_abort_acquisition(sdi);
1077 static struct sr_dev_driver kingst_la2016_driver_info = {
1078 .name = "kingst-la2016",
1079 .longname = "Kingst LA2016",
1082 .cleanup = std_cleanup,
1084 .dev_list = std_dev_list,
1085 .dev_clear = std_dev_clear,
1086 .config_get = config_get,
1087 .config_set = config_set,
1088 .config_list = config_list,
1089 .dev_open = dev_open,
1090 .dev_close = dev_close,
1091 .dev_acquisition_start = dev_acquisition_start,
1092 .dev_acquisition_stop = dev_acquisition_stop,
1095 SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);