]> sigrok.org Git - libsigrok.git/blame - src/hardware/kingst-la2016/api.c
kingst-la2016: implement alternative simpler threshold voltage config
[libsigrok.git] / src / hardware / kingst-la2016 / api.c
CommitLineData
f2cd2deb
FS
1/*
2 * This file is part of the libsigrok project.
3 *
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>
8 *
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.
13 *
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.
18 *
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/>.
21 */
22
96dc954e
GS
23/*
24 * This driver implementation initially was derived from the
25 * src/hardware/saleae-logic16/ source code.
26 */
f2cd2deb
FS
27
28#include <config.h>
a7740b06 29
a7740b06 30#include <libsigrok/libsigrok.h>
f2cd2deb 31#include <string.h>
a7740b06 32
f2cd2deb
FS
33#include "libsigrok-internal.h"
34#include "protocol.h"
35
36static const uint32_t scanopts[] = {
37 SR_CONF_CONN,
38};
39
40static const uint32_t drvopts[] = {
41 SR_CONF_LOGIC_ANALYZER,
331277e0 42 SR_CONF_SIGNAL_GENERATOR,
f2cd2deb
FS
43};
44
45static 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,
a38f0f5e
GS
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,
9270f8f4 51#if WITH_THRESHOLD_DEVCFG
f2cd2deb 52 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
9270f8f4 53#if !WITH_THRESHOLD_SIMPLE
f2cd2deb
FS
54 SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
9270f8f4
GS
56#endif
57#endif
f2cd2deb
FS
58 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
59 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
60};
61
9270f8f4
GS
62static const uint32_t devopts_cg_logic[] = {
63#if !WITH_THRESHOLD_DEVCFG
64 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
65#endif
66};
67
331277e0
GS
68static const uint32_t devopts_cg_pwm[] = {
69 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
70 SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_SET,
71 SR_CONF_DUTY_CYCLE | SR_CONF_GET | SR_CONF_SET,
72};
73
f2cd2deb
FS
74static const int32_t trigger_matches[] = {
75 SR_TRIGGER_ZERO,
76 SR_TRIGGER_ONE,
77 SR_TRIGGER_RISING,
78 SR_TRIGGER_FALLING,
79};
80
d466f61c 81static const char *channel_names_logic[] = {
da25c287
GS
82 "CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7",
83 "CH8", "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15",
d466f61c
GS
84 "CH16", "CH17", "CH18", "CH19", "CH20", "CH21", "CH22", "CH23",
85 "CH24", "CH25", "CH26", "CH27", "CH28", "CH29", "CH30", "CH31",
f2cd2deb
FS
86};
87
331277e0
GS
88static const char *channel_names_pwm[] = {
89 "PWM1", "PWM2",
90};
91
ea436ba7 92/*
330853ba 93 * The hardware uses a 100/200/500MHz base clock (model dependent) and
ea436ba7
GS
94 * a 16bit divider (common across all models). The range from 10kHz to
95 * 100/200/500MHz should be applicable to all devices. High rates may
96 * suffer from coarse resolution (e.g. in the "500MHz div 2" case) and
330853ba
GS
97 * may not provide the desired 1/2/5 steps. Fortunately this exclusively
98 * affects the 500MHz model where 250MHz is used instead of 200MHz and
99 * the 166MHz and 125MHz rates are not presented to users. Deep memory
100 * of these models and hardware compression reduce the necessity to let
101 * users pick from a huge list of possible rates.
102 *
ea436ba7
GS
103 */
104
330853ba
GS
105static const uint64_t rates_500mhz[] = {
106 SR_KHZ(10),
107 SR_KHZ(20),
108 SR_KHZ(50),
109 SR_KHZ(100),
110 SR_KHZ(200),
111 SR_KHZ(500),
112 SR_MHZ(1),
113 SR_MHZ(2),
114 SR_MHZ(5),
115 SR_MHZ(10),
116 SR_MHZ(20),
117 SR_MHZ(50),
118 SR_MHZ(100),
119 SR_MHZ(250),
120 SR_MHZ(500),
121};
122
123static const uint64_t rates_200mhz[] = {
ea436ba7 124 SR_KHZ(10),
f2cd2deb
FS
125 SR_KHZ(20),
126 SR_KHZ(50),
127 SR_KHZ(100),
128 SR_KHZ(200),
129 SR_KHZ(500),
130 SR_MHZ(1),
131 SR_MHZ(2),
f2cd2deb 132 SR_MHZ(5),
f2cd2deb
FS
133 SR_MHZ(10),
134 SR_MHZ(20),
135 SR_MHZ(50),
136 SR_MHZ(100),
137 SR_MHZ(200),
138};
139
330853ba 140static const uint64_t rates_100mhz[] = {
ea436ba7 141 SR_KHZ(10),
8b172e78
KG
142 SR_KHZ(20),
143 SR_KHZ(50),
144 SR_KHZ(100),
145 SR_KHZ(200),
146 SR_KHZ(500),
147 SR_MHZ(1),
148 SR_MHZ(2),
8b172e78 149 SR_MHZ(5),
8b172e78
KG
150 SR_MHZ(10),
151 SR_MHZ(20),
152 SR_MHZ(50),
153 SR_MHZ(100),
154};
155
9270f8f4
GS
156#if WITH_THRESHOLD_SIMPLE
157
158/*
159 * Only list a few discrete voltages, to form a useful set which covers
160 * most logic families. Too many choices can make some applications use
161 * a slider again. Which may lack a scale for the current value, and
162 * leave users without feedback what the currently used value might be.
163 */
164static const double threshold_ranges[][2] = {
165 { 0.4, 0.4, },
166 { 0.6, 0.6, },
167 { 0.9, 0.9, },
168 { 1.2, 1.2, },
169 { 1.4, 1.4, }, /* Default, 1.4V, index 4. */
170 { 2.0, 2.0, },
171 { 2.5, 2.5, },
172 { 4.0, 4.0, },
173};
174#define LOGIC_THRESHOLD_IDX_DFLT 4
175
176static double threshold_voltage(const struct sr_dev_inst *sdi, double *high)
177{
178 struct dev_context *devc;
179 size_t idx;
180 double voltage;
181
182 devc = sdi->priv;
183 idx = devc->threshold_voltage_idx;
184 voltage = threshold_ranges[idx][0];
185 if (high)
186 *high = threshold_ranges[idx][1];
187
188 return voltage;
189}
190
191#else /* WITH_THRESHOLD_SIMPLE */
192
f2cd2deb
FS
193static const float logic_threshold_value[] = {
194 1.58,
195 2.5,
196 1.165,
197 1.5,
198 1.25,
199 0.9,
200 0.75,
201 0.60,
202 0.45,
203};
204
205static const char *logic_threshold[] = {
206 "TTL 5V",
207 "CMOS 5V",
208 "CMOS 3.3V",
209 "CMOS 3.0V",
210 "CMOS 2.5V",
211 "CMOS 1.8V",
212 "CMOS 1.5V",
213 "CMOS 1.2V",
214 "CMOS 0.9V",
215 "USER",
216};
217
da2cb50d 218#define LOGIC_THRESHOLD_IDX_USER (ARRAY_SIZE(logic_threshold) - 1)
f2cd2deb 219
9270f8f4
GS
220static double threshold_voltage(const struct sr_dev_inst *sdi, double *high)
221{
222 struct dev_context *devc;
223 size_t idx;
224 double voltage;
225
226 devc = sdi->priv;
227 idx = devc->threshold_voltage_idx;
228 if (idx == LOGIC_THRESHOLD_IDX_USER)
229 voltage = devc->threshold_voltage;
230 else
231 voltage = logic_threshold_value[idx];
232 if (high)
233 *high = voltage;
234
235 return voltage;
236}
237
238#endif /* WITH_THRESHOLD_SIMPLE */
239
d466f61c
GS
240/* Convenience. Release an allocated devc from error paths. */
241static void kingst_la2016_free_devc(struct dev_context *devc)
242{
243 if (!devc)
244 return;
245 g_free(devc->mcu_firmware);
246 g_free(devc->fpga_bitstream);
247 g_free(devc);
248}
249
250/* Convenience. Release an allocated sdi from error paths. */
251static void kingst_la2016_free_sdi(struct sr_dev_inst *sdi)
252{
253 if (!sdi)
254 return;
255 g_free(sdi->vendor);
256 g_free(sdi->model);
257 g_free(sdi->version);
258 g_free(sdi->serial_num);
259 g_free(sdi->connection_id);
260 sr_usb_dev_inst_free(sdi->conn);
261 kingst_la2016_free_devc(sdi->priv);
262}
263
264/* Convenience. Open a USB device (including claiming an interface). */
265static int la2016_open_usb(struct sr_usb_dev_inst *usb,
266 libusb_device *dev, gboolean show_message)
267{
268 int ret;
269
270 ret = libusb_open(dev, &usb->devhdl);
271 if (ret != 0) {
272 if (show_message) {
273 sr_err("Cannot open device: %s.",
274 libusb_error_name(ret));
275 }
276 return SR_ERR_IO;
277 }
278
279 if (usb->address == 0xff) {
280 /*
281 * First encounter after firmware upload.
282 * Grab current address after enumeration.
283 */
284 usb->address = libusb_get_device_address(dev);
285 }
286
287 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
288 if (ret == LIBUSB_ERROR_BUSY) {
289 sr_err("Cannot claim USB interface. Another program or driver using it?");
290 return SR_ERR_IO;
291 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
292 sr_err("Device has been disconnected.");
293 return SR_ERR_IO;
294 } else if (ret != 0) {
295 sr_err("Cannot claim USB interface: %s.",
296 libusb_error_name(ret));
297 return SR_ERR_IO;
298 }
299
300 return SR_OK;
301}
302
303/* Convenience. Close an opened USB device (and release the interface). */
304static void la2016_close_usb(struct sr_usb_dev_inst *usb)
305{
306
307 if (!usb)
308 return;
309
310 if (usb->devhdl) {
311 libusb_release_interface(usb->devhdl, USB_INTERFACE);
312 libusb_close(usb->devhdl);
313 usb->devhdl = NULL;
314 }
315}
316
317/* Communicate to an USB device to identify the Kingst LA model. */
318static int la2016_identify_read(struct sr_dev_inst *sdi,
319 struct sr_usb_dev_inst *usb, libusb_device *dev,
320 gboolean show_message)
321{
322 int ret;
323
324 ret = la2016_open_usb(usb, dev, show_message);
325 if (ret != SR_OK) {
326 if (show_message)
327 sr_err("Cannot communicate to MCU firmware.");
328 return ret;
329 }
6d53e949
GS
330
331 /*
332 * Also complete the hardware configuration (FPGA bitstream)
333 * when MCU firmware communication became operational. Either
334 * failure is considered fatal when probing for the device.
335 */
d466f61c 336 ret = la2016_identify_device(sdi, show_message);
6d53e949
GS
337 if (ret == SR_OK) {
338 ret = la2016_init_hardware(sdi);
339 }
340
d466f61c
GS
341 la2016_close_usb(usb);
342
343 return ret;
344}
345
346/* Find given conn_id in another USB enum. Identify Kingst LA model. */
347static int la2016_identify_enum(struct sr_dev_inst *sdi)
348{
349 struct sr_dev_driver *di;
350 struct drv_context *drvc;
351 struct sr_context *ctx;
352 libusb_device **devlist, *dev;
353 struct libusb_device_descriptor des;
354 int ret, id_ret;
355 size_t device_count, dev_idx;
356 char conn_id[64];
357
358 di = sdi->driver;
359 drvc = di->context;
360 ctx = drvc->sr_ctx;;
361
362 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
363 if (ret < 0)
364 return SR_ERR_IO;
365 device_count = ret;
366 if (!device_count)
367 return SR_ERR_IO;
368 id_ret = SR_ERR_IO;
369 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
370 dev = devlist[dev_idx];
371 libusb_get_device_descriptor(dev, &des);
372 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
373 continue;
374 if (des.iProduct != LA2016_IPRODUCT_INDEX)
375 continue;
376 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
377 if (ret < 0)
378 continue;
379 if (strcmp(sdi->connection_id, conn_id) != 0)
380 continue;
381 id_ret = la2016_identify_read(sdi, sdi->conn, dev, FALSE);
382 break;
383 }
384 libusb_free_device_list(devlist, 1);
385
386 return id_ret;
387}
388
389/* Wait for a device to re-appear after firmware upload. */
390static int la2016_identify_wait(struct sr_dev_inst *sdi)
391{
392 struct dev_context *devc;
393 uint64_t reset_done, now, elapsed_ms;
394 int ret;
395
396 devc = sdi->priv;
397
398 sr_info("Waiting for device to reset after firmware upload.");
399 now = g_get_monotonic_time();
400 reset_done = devc->fw_uploaded + RENUM_GONE_DELAY_MS * 1000;
401 if (now < reset_done)
402 g_usleep(reset_done - now);
403 do {
404 now = g_get_monotonic_time();
405 elapsed_ms = (now - devc->fw_uploaded) / 1000;
406 sr_spew("Waited %" PRIu64 "ms.", elapsed_ms);
407 ret = la2016_identify_enum(sdi);
408 if (ret == SR_OK) {
409 devc->fw_uploaded = 0;
410 break;
411 }
412 g_usleep(RENUM_POLL_INTERVAL_MS * 1000);
413 } while (elapsed_ms < RENUM_CHECK_PERIOD_MS);
414 if (ret != SR_OK) {
415 sr_err("Device failed to re-enumerate.");
416 return ret;
417 }
418 sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms);
419
420 return SR_OK;
421}
422
423/*
424 * Open given conn_id from another USB enum. Used by dev_open(). Similar
425 * to, and should be kept in sync with la2016_identify_enum().
426 */
427static int la2016_open_enum(struct sr_dev_inst *sdi)
428{
429 struct sr_dev_driver *di;
430 struct drv_context *drvc;
431 struct sr_context *ctx;
432 libusb_device **devlist, *dev;
433 struct libusb_device_descriptor des;
434 int ret, open_ret;
435 size_t device_count, dev_idx;
436 char conn_id[64];
437
438 di = sdi->driver;
439 drvc = di->context;
440 ctx = drvc->sr_ctx;;
441
442 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
443 if (ret < 0)
444 return SR_ERR_IO;
445 device_count = ret;
446 if (!device_count)
447 return SR_ERR_IO;
448 open_ret = SR_ERR_IO;
449 for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
450 dev = devlist[dev_idx];
451 libusb_get_device_descriptor(dev, &des);
452 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
453 continue;
454 if (des.iProduct != LA2016_IPRODUCT_INDEX)
455 continue;
456 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
457 if (ret < 0)
458 continue;
459 if (strcmp(sdi->connection_id, conn_id) != 0)
460 continue;
461 open_ret = la2016_open_usb(sdi->conn, dev, TRUE);
462 break;
463 }
464 libusb_free_device_list(devlist, 1);
465
466 return open_ret;
467}
468
f2cd2deb
FS
469static GSList *scan(struct sr_dev_driver *di, GSList *options)
470{
471 struct drv_context *drvc;
520a20e9 472 struct sr_context *ctx;
f2cd2deb
FS
473 struct dev_context *devc;
474 struct sr_dev_inst *sdi;
475 struct sr_usb_dev_inst *usb;
476 struct sr_config *src;
477 GSList *l;
d466f61c 478 GSList *devices, *found_devices, *renum_devices;
f2cd2deb
FS
479 GSList *conn_devices;
480 struct libusb_device_descriptor des;
520a20e9
GS
481 libusb_device **devlist, *dev;
482 size_t dev_count, dev_idx, ch_idx;
483 uint8_t bus, addr;
d466f61c 484 uint16_t pid;
f2cd2deb 485 const char *conn;
520a20e9 486 char conn_id[64];
520a20e9 487 int ret;
d466f61c 488 size_t ch_off, ch_max;
331277e0
GS
489 struct sr_channel *ch;
490 struct sr_channel_group *cg;
f2cd2deb
FS
491
492 drvc = di->context;
520a20e9 493 ctx = drvc->sr_ctx;;
f2cd2deb
FS
494
495 conn = NULL;
d466f61c 496 conn_devices = NULL;
f2cd2deb
FS
497 for (l = options; l; l = l->next) {
498 src = l->data;
499 switch (src->key) {
500 case SR_CONF_CONN:
501 conn = g_variant_get_string(src->data, NULL);
502 break;
503 }
504 }
505 if (conn)
520a20e9 506 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
d466f61c
GS
507 if (conn && !conn_devices) {
508 sr_err("Cannot find the specified connection '%s'.", conn);
509 return NULL;
510 }
f2cd2deb 511
d466f61c
GS
512 /*
513 * Find all LA2016 devices, optionally upload firmware to them.
514 * Defer completion of sdi/devc creation until all (selected)
515 * devices were found in a usable state, and their models got
516 * identified which affect their feature set. It appears that
517 * we cannot communicate to the device within the same USB enum
518 * cycle, needs another USB enumeration after firmware upload.
519 */
f2cd2deb 520 devices = NULL;
d466f61c
GS
521 found_devices = NULL;
522 renum_devices = NULL;
520a20e9
GS
523 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
524 if (ret < 0) {
525 sr_err("Cannot get device list: %s.", libusb_error_name(ret));
526 return devices;
527 }
528 dev_count = ret;
529 for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
530 dev = devlist[dev_idx];
531 bus = libusb_get_bus_number(dev);
532 addr = libusb_get_device_address(dev);
d466f61c
GS
533
534 /* Filter by connection when externally specified. */
535 for (l = conn_devices; l; l = l->next) {
536 usb = l->data;
537 if (usb->bus == bus && usb->address == addr)
538 break;
539 }
540 if (conn_devices && !l) {
541 sr_spew("Bus %hhu, addr %hhu do not match specified filter.",
542 bus, addr);
543 continue;
f2cd2deb
FS
544 }
545
d466f61c 546 /* Check USB VID:PID. Get the connection string. */
520a20e9 547 libusb_get_device_descriptor(dev, &des);
d466f61c
GS
548 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
549 continue;
550 pid = des.idProduct;
520a20e9
GS
551 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
552 if (ret < 0)
f2cd2deb 553 continue;
d466f61c
GS
554 sr_dbg("USB enum found %04x:%04x at path %s, %d.%d.",
555 des.idVendor, des.idProduct, conn_id, bus, addr);
556 usb = sr_usb_dev_inst_new(bus, addr, NULL);
f2cd2deb 557
520a20e9 558 sdi = g_malloc0(sizeof(*sdi));
d466f61c 559 sdi->driver = di;
f2cd2deb 560 sdi->status = SR_ST_INITIALIZING;
d466f61c 561 sdi->inst_type = SR_INST_USB;
520a20e9 562 sdi->connection_id = g_strdup(conn_id);
d466f61c 563 sdi->conn = usb;
f2cd2deb 564
d466f61c
GS
565 devc = g_malloc0(sizeof(*devc));
566 sdi->priv = devc;
f2cd2deb 567
d466f61c
GS
568 /*
569 * Load MCU firmware if it is currently missing. Which
570 * makes the device disappear and renumerate in USB.
571 * We need to come back another time to communicate to
572 * this device.
573 */
574 devc->fw_uploaded = 0;
575 if (des.iProduct != LA2016_IPRODUCT_INDEX) {
576 sr_info("Uploading MCU firmware to '%s'.", conn_id);
577 ret = la2016_upload_firmware(sdi, ctx, dev, pid);
520a20e9 578 if (ret != SR_OK) {
91f73872 579 sr_err("MCU firmware upload failed.");
d466f61c 580 kingst_la2016_free_sdi(sdi);
f2cd2deb
FS
581 continue;
582 }
d466f61c
GS
583 devc->fw_uploaded = g_get_monotonic_time();
584 usb->address = 0xff;
585 renum_devices = g_slist_append(renum_devices, sdi);
586 continue;
f2cd2deb
FS
587 }
588
d466f61c
GS
589 /*
590 * Communicate to the MCU firmware to access EEPROM data
591 * which lets us identify the device type. Then stop, to
592 * share remaining sdi/devc creation with those devices
593 * which had their MCU firmware uploaded above and which
594 * get revisited later.
595 */
596 ret = la2016_identify_read(sdi, usb, dev, TRUE);
597 if (ret != SR_OK || !devc->model) {
598 sr_err("Unknown or unsupported device type.");
599 kingst_la2016_free_sdi(sdi);
600 continue;
601 }
602 found_devices = g_slist_append(found_devices, sdi);
603 }
604 libusb_free_device_list(devlist, 1);
605 g_slist_free_full(conn_devices, sr_usb_dev_inst_free_cb);
f2cd2deb 606
d466f61c
GS
607 /*
608 * Wait for devices to re-appear after firmware upload. Append
609 * the yet unidentified device to the list of found devices, or
610 * release the previously allocated sdi/devc.
611 */
612 for (l = renum_devices; l; l = l->next) {
613 sdi = l->data;
614 devc = sdi->priv;
615 ret = la2016_identify_wait(sdi);
616 if (ret != SR_OK || !devc->model) {
617 sr_dbg("Skipping unusable '%s'.", sdi->connection_id);
618 kingst_la2016_free_sdi(sdi);
619 continue;
520a20e9 620 }
d466f61c
GS
621 found_devices = g_slist_append(found_devices, sdi);
622 }
623 g_slist_free(renum_devices);
f2cd2deb 624
d466f61c
GS
625 /*
626 * All found devices got identified, their type is known here.
627 * Complete the sdi/devc creation. Assign default settings
628 * because the vendor firmware would not let us read back the
629 * previously written configuration.
630 */
631 for (l = found_devices; l; l = l->next) {
632 sdi = l->data;
633 devc = sdi->priv;
634
635 sdi->vendor = g_strdup("Kingst");
636 sdi->model = g_strdup(devc->model->name);
637 ch_off = 0;
638
331277e0 639 /* Create the "Logic" channel group. */
d466f61c
GS
640 ch_max = ARRAY_SIZE(channel_names_logic);
641 if (ch_max > devc->model->channel_count)
642 ch_max = devc->model->channel_count;
331277e0
GS
643 cg = sr_channel_group_new(sdi, "Logic", NULL);
644 devc->cg_logic = cg;
d466f61c 645 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
331277e0 646 ch = sr_channel_new(sdi, ch_off,
d466f61c
GS
647 SR_CHANNEL_LOGIC, TRUE,
648 channel_names_logic[ch_idx]);
649 ch_off++;
331277e0
GS
650 cg->channels = g_slist_append(cg->channels, ch);
651 }
652
653 /* Create the "PWMx" channel groups. */
654 ch_max = ARRAY_SIZE(channel_names_pwm);
655 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
656 const char *name;
657 name = channel_names_pwm[ch_idx];
658 cg = sr_channel_group_new(sdi, name, NULL);
659 if (!devc->cg_pwm)
660 devc->cg_pwm = cg;
661 ch = sr_channel_new(sdi, ch_off,
662 SR_CHANNEL_ANALOG, FALSE, name);
663 ch_off++;
664 cg->channels = g_slist_append(cg->channels, ch);
d466f61c 665 }
f2cd2deb 666
08a49848
GS
667 /*
668 * Ideally we'd get the previous configuration from the
669 * hardware, but this device is write-only. So we have
670 * to assign a fixed set of initial configuration values.
671 */
a38f0f5e 672 sr_sw_limits_init(&devc->sw_limits);
d8fbfcd9
GS
673 devc->sw_limits.limit_samples = 0;
674 devc->capture_ratio = 50;
675 devc->cur_samplerate = devc->model->samplerate;
9270f8f4
GS
676#if WITH_THRESHOLD_SIMPLE
677 devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_DFLT;
678#else /* WITH_THRESHOLD_SIMPLE */
f2cd2deb
FS
679 devc->threshold_voltage_idx = 0;
680 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
9270f8f4 681#endif /* WITH_THRESHOLD_SIMPLE */
08a49848
GS
682 if (ARRAY_SIZE(devc->pwm_setting) >= 1) {
683 devc->pwm_setting[0].enabled = FALSE;
684 devc->pwm_setting[0].freq = SR_KHZ(1);
685 devc->pwm_setting[0].duty = 50;
686 }
687 if (ARRAY_SIZE(devc->pwm_setting) >= 2) {
688 devc->pwm_setting[1].enabled = FALSE;
689 devc->pwm_setting[1].freq = SR_KHZ(100);
690 devc->pwm_setting[1].duty = 50;
691 }
f2cd2deb
FS
692
693 sdi->status = SR_ST_INACTIVE;
d466f61c 694 devices = g_slist_append(devices, sdi);
f2cd2deb 695 }
d466f61c 696 g_slist_free(found_devices);
f2cd2deb
FS
697
698 return std_scan_complete(di, devices);
699}
700
f2cd2deb
FS
701static int dev_open(struct sr_dev_inst *sdi)
702{
08a49848 703 struct dev_context *devc;
f2cd2deb 704 int ret;
08a49848
GS
705 size_t ch;
706
707 devc = sdi->priv;
f2cd2deb 708
d466f61c 709 ret = la2016_open_enum(sdi);
f2cd2deb 710 if (ret != SR_OK) {
91f73872 711 sr_err("Cannot open device.");
520a20e9 712 return ret;
f2cd2deb
FS
713 }
714
08a49848
GS
715 /* Send most recent PWM configuration to the device. */
716 for (ch = 0; ch < ARRAY_SIZE(devc->pwm_setting); ch++) {
717 ret = la2016_write_pwm_config(sdi, ch);
718 if (ret != SR_OK)
719 return ret;
6d53e949
GS
720 }
721
f2cd2deb
FS
722 return SR_OK;
723}
724
725static int dev_close(struct sr_dev_inst *sdi)
726{
727 struct sr_usb_dev_inst *usb;
728
729 usb = sdi->conn;
730
731 if (!usb->devhdl)
732 return SR_ERR_BUG;
733
6d53e949 734 la2016_deinit_hardware(sdi);
f2cd2deb
FS
735
736 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
737 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
d466f61c 738 la2016_close_usb(sdi->conn);
f2cd2deb
FS
739
740 return SR_OK;
741}
742
331277e0
GS
743/* Config API helper. Get type and index of a channel group. */
744static int get_cg_index(const struct sr_dev_inst *sdi,
745 const struct sr_channel_group *cg,
746 int *type, size_t *logic, size_t *analog)
747{
748 struct dev_context *devc;
749 GSList *l;
750 size_t idx;
751
752 /* Preset return values. */
753 if (type)
754 *type = 0;
755 if (logic)
756 *logic = 0;
757 if (analog)
758 *analog = 0;
759
760 /* Start categorizing the received cg. */
761 if (!sdi)
762 return SR_ERR_ARG;
763 devc = sdi->priv;
764 if (!cg)
765 return SR_OK;
766 l = sdi->channel_groups;
767
768 /* First sdi->channelgroups item is "Logic". */
769 if (!l)
770 return SR_ERR_BUG;
771 if (cg == l->data) {
772 if (type)
773 *type = SR_CHANNEL_LOGIC;
774 if (logic)
775 *logic = 0;
776 return SR_OK;
777 }
778 l = l->next;
779
780 /* Next sdi->channelgroups items are "PWMx". */
781 idx = 0;
782 while (l && l->data != cg) {
783 idx++;
784 l = l->next;
785 }
786 if (l && idx < ARRAY_SIZE(devc->pwm_setting)) {
787 if (type)
788 *type = SR_CHANNEL_ANALOG;
789 if (analog)
790 *analog = idx;
791 return SR_OK;
792 }
793
794 return SR_ERR_ARG;
795}
796
955ab604
GS
797static int config_get(uint32_t key, GVariant **data,
798 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
f2cd2deb
FS
799{
800 struct dev_context *devc;
331277e0
GS
801 int ret, cg_type;
802 size_t logic_idx, analog_idx;
803 struct pwm_setting *pwm;
f2cd2deb 804 struct sr_usb_dev_inst *usb;
9270f8f4 805 double voltage, rounded;
da2cb50d 806 const char *label;
f2cd2deb 807
9270f8f4
GS
808 (void)rounded;
809 (void)voltage;
810
f2cd2deb
FS
811 if (!sdi)
812 return SR_ERR_ARG;
813 devc = sdi->priv;
814
331277e0
GS
815 /* Check for types (and index) of channel groups. */
816 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
817 if (cg && ret != SR_OK)
818 return SR_ERR_ARG;
819
820 /* Handle requests for the "Logic" channel group. */
821 if (cg && cg_type == SR_CHANNEL_LOGIC) {
9270f8f4
GS
822 switch (key) {
823#if !WITH_THRESHOLD_DEVCFG
824#if WITH_THRESHOLD_SIMPLE
825 case SR_CONF_VOLTAGE_THRESHOLD:
826 voltage = threshold_voltage(sdi, NULL);
827 *data = std_gvar_tuple_double(voltage, voltage);
828 break;
829#endif /* WITH_THRESHOLD_SIMPLE */
830#endif /* WITH_THRESHOLD_DEVCFG */
831 default:
832 return SR_ERR_NA;
833 }
834 return SR_OK;
331277e0
GS
835 }
836
837 /* Handle requests for the "PWMx" channel groups. */
838 if (cg && cg_type == SR_CHANNEL_ANALOG) {
839 pwm = &devc->pwm_setting[analog_idx];
840 switch (key) {
841 case SR_CONF_ENABLED:
842 *data = g_variant_new_boolean(pwm->enabled);
843 break;
844 case SR_CONF_OUTPUT_FREQUENCY:
845 *data = g_variant_new_double(pwm->freq);
846 break;
847 case SR_CONF_DUTY_CYCLE:
848 *data = g_variant_new_double(pwm->duty);
849 break;
850 default:
851 return SR_ERR_NA;
852 }
853 return SR_OK;
854 }
855
f2cd2deb
FS
856 switch (key) {
857 case SR_CONF_CONN:
f2cd2deb 858 usb = sdi->conn;
f2cd2deb
FS
859 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
860 break;
861 case SR_CONF_SAMPLERATE:
862 *data = g_variant_new_uint64(devc->cur_samplerate);
863 break;
864 case SR_CONF_LIMIT_SAMPLES:
a38f0f5e
GS
865 case SR_CONF_LIMIT_MSEC:
866 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
f2cd2deb
FS
867 case SR_CONF_CAPTURE_RATIO:
868 *data = g_variant_new_uint64(devc->capture_ratio);
869 break;
9270f8f4
GS
870#if WITH_THRESHOLD_DEVCFG
871#if WITH_THRESHOLD_SIMPLE
872 case SR_CONF_VOLTAGE_THRESHOLD:
873 voltage = threshold_voltage(sdi, NULL);
874 *data = std_gvar_tuple_double(voltage, voltage);
875 break;
876#else /* WITH_THRESHOLD_SIMPLE */
f2cd2deb
FS
877 case SR_CONF_VOLTAGE_THRESHOLD:
878 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
879 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
9270f8f4 880 break;
f2cd2deb 881 case SR_CONF_LOGIC_THRESHOLD:
da2cb50d
GS
882 label = logic_threshold[devc->threshold_voltage_idx];
883 *data = g_variant_new_string(label);
f2cd2deb
FS
884 break;
885 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
886 *data = g_variant_new_double(devc->threshold_voltage);
887 break;
9270f8f4
GS
888#endif /* WITH_THRESHOLD_SIMPLE */
889#endif /* WITH_THRESHOLD_DEVCFG */
f2cd2deb
FS
890 default:
891 return SR_ERR_NA;
892 }
893
894 return SR_OK;
895}
896
955ab604
GS
897static int config_set(uint32_t key, GVariant *data,
898 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
f2cd2deb
FS
899{
900 struct dev_context *devc;
331277e0
GS
901 int ret, cg_type;
902 size_t logic_idx, analog_idx;
903 struct pwm_setting *pwm;
c35baf6e 904 double value_f;
9270f8f4 905 double low, high, voltage;
f2cd2deb
FS
906 int idx;
907
f2cd2deb
FS
908 devc = sdi->priv;
909
331277e0
GS
910 /* Check for types (and index) of channel groups. */
911 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
912 if (cg && ret != SR_OK)
913 return SR_ERR_ARG;
914
915 /* Handle requests for the "Logic" channel group. */
916 if (cg && cg_type == SR_CHANNEL_LOGIC) {
9270f8f4
GS
917 switch (key) {
918#if !WITH_THRESHOLD_DEVCFG
919#if WITH_THRESHOLD_SIMPLE
920 case SR_CONF_LOGIC_THRESHOLD:
921 idx = std_double_tuple_idx(data,
922 ARRAY_AND_SIZE(threshold_ranges));
923 if (idx < 0)
924 return SR_ERR_ARG;
925 devc->threshold_voltage_idx = idx;
926 break;
927#endif /* WITH_THRESHOLD_SIMPLE */
928#endif /* WITH_THRESHOLD_DEVCFG */
929 default:
930 return SR_ERR_NA;
931 }
932 return SR_OK;
331277e0
GS
933 }
934
935 /* Handle requests for the "PWMx" channel groups. */
936 if (cg && cg_type == SR_CHANNEL_ANALOG) {
937 pwm = &devc->pwm_setting[analog_idx];
938 switch (key) {
939 case SR_CONF_ENABLED:
940 pwm->enabled = g_variant_get_boolean(data);
941 ret = la2016_write_pwm_config(sdi, analog_idx);
942 if (ret != SR_OK)
943 return ret;
944 break;
945 case SR_CONF_OUTPUT_FREQUENCY:
c35baf6e
GS
946 value_f = g_variant_get_double(data);
947 if (value_f <= 0.0 || value_f > MAX_PWM_FREQ)
948 return SR_ERR_ARG;
949 pwm->freq = value_f;
331277e0
GS
950 ret = la2016_write_pwm_config(sdi, analog_idx);
951 if (ret != SR_OK)
952 return ret;
953 break;
954 case SR_CONF_DUTY_CYCLE:
c35baf6e
GS
955 value_f = g_variant_get_double(data);
956 if (value_f <= 0.0 || value_f > 100.0)
957 return SR_ERR_ARG;
958 pwm->duty = value_f;
331277e0
GS
959 ret = la2016_write_pwm_config(sdi, analog_idx);
960 if (ret != SR_OK)
961 return ret;
962 break;
963 default:
964 return SR_ERR_NA;
965 }
966 return SR_OK;
967 }
968
f2cd2deb
FS
969 switch (key) {
970 case SR_CONF_SAMPLERATE:
971 devc->cur_samplerate = g_variant_get_uint64(data);
972 break;
973 case SR_CONF_LIMIT_SAMPLES:
a38f0f5e
GS
974 case SR_CONF_LIMIT_MSEC:
975 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
f2cd2deb
FS
976 case SR_CONF_CAPTURE_RATIO:
977 devc->capture_ratio = g_variant_get_uint64(data);
978 break;
9270f8f4
GS
979#if WITH_THRESHOLD_DEVCFG
980#if WITH_THRESHOLD_SIMPLE
981 case SR_CONF_VOLTAGE_THRESHOLD:
982 idx = std_double_tuple_idx(data,
983 ARRAY_AND_SIZE(threshold_ranges));
984 if (idx < 0)
985 return SR_ERR_ARG;
986 devc->threshold_voltage_idx = idx;
987 break;
988#else /* WITH_THRESHOLD_SIMPLE */
f2cd2deb
FS
989 case SR_CONF_VOLTAGE_THRESHOLD:
990 g_variant_get(data, "(dd)", &low, &high);
991 devc->threshold_voltage = (low + high) / 2.0;
da2cb50d 992 devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_USER;
f2cd2deb
FS
993 break;
994 case SR_CONF_LOGIC_THRESHOLD: {
411ad77c
GS
995 idx = std_str_idx(data, ARRAY_AND_SIZE(logic_threshold));
996 if (idx < 0)
f2cd2deb 997 return SR_ERR_ARG;
da2cb50d 998 if (idx != LOGIC_THRESHOLD_IDX_USER) {
f2cd2deb
FS
999 devc->threshold_voltage = logic_threshold_value[idx];
1000 }
1001 devc->threshold_voltage_idx = idx;
1002 break;
1003 }
1004 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
1005 devc->threshold_voltage = g_variant_get_double(data);
1006 break;
9270f8f4
GS
1007#endif /* WITH_THRESHOLD_SIMPLE */
1008#endif /* WITH_THRESHOLD_DEVCFG */
f2cd2deb
FS
1009 default:
1010 return SR_ERR_NA;
1011 }
1012
1013 return SR_OK;
1014}
1015
955ab604
GS
1016static int config_list(uint32_t key, GVariant **data,
1017 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
f2cd2deb 1018{
8b172e78 1019 struct dev_context *devc;
331277e0
GS
1020 int ret, cg_type;
1021 size_t logic_idx, analog_idx;
8b172e78 1022
a38f0f5e
GS
1023 devc = sdi ? sdi->priv : NULL;
1024
331277e0
GS
1025 /* Check for types (and index) of channel groups. */
1026 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
1027 if (cg && ret != SR_OK)
1028 return SR_ERR_ARG;
1029
1030 /* Handle requests for the "Logic" channel group. */
1031 if (cg && cg_type == SR_CHANNEL_LOGIC) {
9270f8f4
GS
1032 switch (key) {
1033 case SR_CONF_DEVICE_OPTIONS:
1034 if (ARRAY_SIZE(devopts_cg_logic) == 0)
1035 return SR_ERR_NA;
1036 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
1037 devopts_cg_logic, ARRAY_SIZE(devopts_cg_logic),
1038 sizeof(devopts_cg_logic[0]));
1039 break;
1040#if !WITH_THRESHOLD_DEVCFG
1041#if WITH_THRESHOLD_SIMPLE
1042 case SR_CONF_VOLTAGE_THRESHOLD:
1043 *data = std_gvar_thresholds(ARRAY_AND_SIZE(threshold_ranges));
1044 break;
1045#endif /* WITH_THRESHOLD_SIMPLE */
1046#endif /* WITH_THRESHOLD_DEVCFG */
1047 default:
1048 return SR_ERR_NA;
1049 }
1050 return SR_OK;
331277e0
GS
1051 }
1052
1053 /* Handle requests for the "PWMx" channel groups. */
1054 if (cg && cg_type == SR_CHANNEL_ANALOG) {
1055 switch (key) {
1056 case SR_CONF_DEVICE_OPTIONS:
1057 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
1058 devopts_cg_pwm, ARRAY_SIZE(devopts_cg_pwm),
1059 sizeof(devopts_cg_pwm[0]));
1060 break;
1061 default:
1062 return SR_ERR_NA;
1063 }
1064 return SR_OK;
1065 }
1066
f2cd2deb
FS
1067 switch (key) {
1068 case SR_CONF_SCAN_OPTIONS:
1069 case SR_CONF_DEVICE_OPTIONS:
411ad77c
GS
1070 return STD_CONFIG_LIST(key, data, sdi, cg,
1071 scanopts, drvopts, devopts);
f2cd2deb 1072 case SR_CONF_SAMPLERATE:
fb28e72d
MW
1073 if (!sdi)
1074 return SR_ERR_ARG;
d466f61c 1075 if (devc->model->samplerate == SR_MHZ(500))
330853ba 1076 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_500mhz));
d466f61c 1077 else if (devc->model->samplerate == SR_MHZ(200))
330853ba
GS
1078 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_200mhz));
1079 else
1080 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_100mhz));
f2cd2deb
FS
1081 break;
1082 case SR_CONF_LIMIT_SAMPLES:
d8fbfcd9 1083 *data = std_gvar_tuple_u64(0, LA2016_NUM_SAMPLES_MAX);
f2cd2deb 1084 break;
9270f8f4
GS
1085#if WITH_THRESHOLD_DEVCFG
1086#if WITH_THRESHOLD_SIMPLE
1087 case SR_CONF_VOLTAGE_THRESHOLD:
1088 *data = std_gvar_thresholds(ARRAY_AND_SIZE(threshold_ranges));
1089 break;
1090#else /* WITH_THRESHOLD_SIMPLE */
f2cd2deb
FS
1091 case SR_CONF_VOLTAGE_THRESHOLD:
1092 *data = std_gvar_min_max_step_thresholds(
1093 LA2016_THR_VOLTAGE_MIN,
1094 LA2016_THR_VOLTAGE_MAX, 0.1);
1095 break;
9270f8f4
GS
1096#endif /* WITH_THRESHOLD_SIMPLE */
1097#endif /* WITH_THRESHOLD_DEVCFG */
f2cd2deb
FS
1098 case SR_CONF_TRIGGER_MATCH:
1099 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
1100 break;
9270f8f4 1101#if WITH_THRESHOLD_DEVCFG && !WITH_THRESHOLD_SIMPLE
f2cd2deb 1102 case SR_CONF_LOGIC_THRESHOLD:
da2cb50d 1103 *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_threshold));
f2cd2deb 1104 break;
9270f8f4 1105#endif
f2cd2deb
FS
1106 default:
1107 return SR_ERR_NA;
1108 }
1109
1110 return SR_OK;
1111}
1112
f2cd2deb
FS
1113static int dev_acquisition_start(const struct sr_dev_inst *sdi)
1114{
1115 struct sr_dev_driver *di;
1116 struct drv_context *drvc;
520a20e9 1117 struct sr_context *ctx;
f2cd2deb 1118 struct dev_context *devc;
9270f8f4 1119 double voltage;
f2cd2deb
FS
1120 int ret;
1121
1122 di = sdi->driver;
1123 drvc = di->context;
520a20e9 1124 ctx = drvc->sr_ctx;;
f2cd2deb
FS
1125 devc = sdi->priv;
1126
a38f0f5e
GS
1127 if (!devc->feed_queue) {
1128 devc->feed_queue = feed_queue_logic_alloc(sdi,
1129 LA2016_CONVBUFFER_SIZE, sizeof(uint16_t));
1130 if (!devc->feed_queue) {
1131 sr_err("Cannot allocate buffer for session feed.");
1132 return SR_ERR_MALLOC;
1133 }
f2cd2deb
FS
1134 }
1135
a38f0f5e
GS
1136 sr_sw_limits_acquisition_start(&devc->sw_limits);
1137
9270f8f4
GS
1138 voltage = threshold_voltage(sdi, NULL);
1139 ret = la2016_setup_acquisition(sdi, voltage);
411ad77c 1140 if (ret != SR_OK) {
a38f0f5e
GS
1141 feed_queue_logic_free(devc->feed_queue);
1142 devc->feed_queue = NULL;
f2cd2deb
FS
1143 return ret;
1144 }
1145
411ad77c
GS
1146 ret = la2016_start_acquisition(sdi);
1147 if (ret != SR_OK) {
3ebc1cb2 1148 la2016_abort_acquisition(sdi);
a38f0f5e
GS
1149 feed_queue_logic_free(devc->feed_queue);
1150 devc->feed_queue = NULL;
f2cd2deb
FS
1151 return ret;
1152 }
1153
cf057ac4 1154 devc->completion_seen = FALSE;
520a20e9 1155 usb_source_add(sdi->session, ctx, 50,
388438e4 1156 la2016_receive_data, (void *)sdi);
f2cd2deb
FS
1157
1158 std_session_send_df_header(sdi);
1159
1160 return SR_OK;
1161}
1162
1163static int dev_acquisition_stop(struct sr_dev_inst *sdi)
1164{
1165 int ret;
1166
1167 ret = la2016_abort_acquisition(sdi);
f2cd2deb
FS
1168
1169 return ret;
1170}
1171
1172static struct sr_dev_driver kingst_la2016_driver_info = {
1173 .name = "kingst-la2016",
1174 .longname = "Kingst LA2016",
1175 .api_version = 1,
1176 .init = std_init,
1177 .cleanup = std_cleanup,
1178 .scan = scan,
1179 .dev_list = std_dev_list,
1180 .dev_clear = std_dev_clear,
1181 .config_get = config_get,
1182 .config_set = config_set,
1183 .config_list = config_list,
1184 .dev_open = dev_open,
1185 .dev_close = dev_close,
1186 .dev_acquisition_start = dev_acquisition_start,
1187 .dev_acquisition_stop = dev_acquisition_stop,
1188 .context = NULL,
1189};
1190SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);