]> sigrok.org Git - libsigrok.git/blame - src/hardware/kingst-la2016/api.c
kingst-la2016: add support for streaming mode, works for 16 channels
[libsigrok.git] / src / hardware / kingst-la2016 / api.c
CommitLineData
f2cd2deb
FS
1/*
2 * This file is part of the libsigrok project.
3 *
7047acc8 4 * Copyright (C) 2022 Gerhard Sittig <gerhard.sittig@gmx.net>
f2cd2deb
FS
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>
9 *
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.
14 *
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.
19 *
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/>.
22 */
23
96dc954e
GS
24/*
25 * This driver implementation initially was derived from the
26 * src/hardware/saleae-logic16/ source code.
27 */
f2cd2deb
FS
28
29#include <config.h>
a7740b06 30
a7740b06 31#include <libsigrok/libsigrok.h>
f2cd2deb 32#include <string.h>
a7740b06 33
f2cd2deb
FS
34#include "libsigrok-internal.h"
35#include "protocol.h"
36
37static const uint32_t scanopts[] = {
38 SR_CONF_CONN,
39};
40
41static const uint32_t drvopts[] = {
42 SR_CONF_LOGIC_ANALYZER,
331277e0 43 SR_CONF_SIGNAL_GENERATOR,
f2cd2deb
FS
44};
45
46static const uint32_t devopts[] = {
f2cd2deb
FS
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#endif
f2cd2deb
FS
54 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
55 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
0fbb464b 56 SR_CONF_CONTINUOUS | SR_CONF_GET | SR_CONF_SET,
f2cd2deb
FS
57};
58
9270f8f4
GS
59static 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,
62#endif
63};
64
331277e0
GS
65static 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,
69};
70
f2cd2deb
FS
71static const int32_t trigger_matches[] = {
72 SR_TRIGGER_ZERO,
73 SR_TRIGGER_ONE,
74 SR_TRIGGER_RISING,
75 SR_TRIGGER_FALLING,
76};
77
d466f61c 78static const char *channel_names_logic[] = {
da25c287
GS
79 "CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7",
80 "CH8", "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15",
d466f61c
GS
81 "CH16", "CH17", "CH18", "CH19", "CH20", "CH21", "CH22", "CH23",
82 "CH24", "CH25", "CH26", "CH27", "CH28", "CH29", "CH30", "CH31",
f2cd2deb
FS
83};
84
331277e0
GS
85static const char *channel_names_pwm[] = {
86 "PWM1", "PWM2",
87};
88
ea436ba7 89/*
330853ba 90 * The hardware uses a 100/200/500MHz base clock (model dependent) and
ea436ba7
GS
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
330853ba
GS
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.
99 *
ea436ba7
GS
100 */
101
330853ba
GS
102static const uint64_t rates_500mhz[] = {
103 SR_KHZ(10),
104 SR_KHZ(20),
105 SR_KHZ(50),
106 SR_KHZ(100),
107 SR_KHZ(200),
108 SR_KHZ(500),
109 SR_MHZ(1),
110 SR_MHZ(2),
111 SR_MHZ(5),
112 SR_MHZ(10),
113 SR_MHZ(20),
114 SR_MHZ(50),
115 SR_MHZ(100),
116 SR_MHZ(250),
117 SR_MHZ(500),
118};
119
120static const uint64_t rates_200mhz[] = {
ea436ba7 121 SR_KHZ(10),
f2cd2deb
FS
122 SR_KHZ(20),
123 SR_KHZ(50),
124 SR_KHZ(100),
125 SR_KHZ(200),
126 SR_KHZ(500),
127 SR_MHZ(1),
128 SR_MHZ(2),
f2cd2deb 129 SR_MHZ(5),
f2cd2deb
FS
130 SR_MHZ(10),
131 SR_MHZ(20),
132 SR_MHZ(50),
133 SR_MHZ(100),
134 SR_MHZ(200),
135};
136
330853ba 137static const uint64_t rates_100mhz[] = {
ea436ba7 138 SR_KHZ(10),
8b172e78
KG
139 SR_KHZ(20),
140 SR_KHZ(50),
141 SR_KHZ(100),
142 SR_KHZ(200),
143 SR_KHZ(500),
144 SR_MHZ(1),
145 SR_MHZ(2),
8b172e78 146 SR_MHZ(5),
8b172e78
KG
147 SR_MHZ(10),
148 SR_MHZ(20),
149 SR_MHZ(50),
150 SR_MHZ(100),
151};
152
9270f8f4
GS
153/*
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.
158 */
159static const double threshold_ranges[][2] = {
160 { 0.4, 0.4, },
161 { 0.6, 0.6, },
162 { 0.9, 0.9, },
163 { 1.2, 1.2, },
164 { 1.4, 1.4, }, /* Default, 1.4V, index 4. */
165 { 2.0, 2.0, },
166 { 2.5, 2.5, },
167 { 4.0, 4.0, },
168};
169#define LOGIC_THRESHOLD_IDX_DFLT 4
170
171static double threshold_voltage(const struct sr_dev_inst *sdi, double *high)
172{
173 struct dev_context *devc;
174 size_t idx;
175 double voltage;
176
177 devc = sdi->priv;
178 idx = devc->threshold_voltage_idx;
179 voltage = threshold_ranges[idx][0];
180 if (high)
181 *high = threshold_ranges[idx][1];
182
183 return voltage;
184}
185
d466f61c
GS
186/* Convenience. Release an allocated devc from error paths. */
187static void kingst_la2016_free_devc(struct dev_context *devc)
188{
189 if (!devc)
190 return;
191 g_free(devc->mcu_firmware);
192 g_free(devc->fpga_bitstream);
193 g_free(devc);
194}
195
196/* Convenience. Release an allocated sdi from error paths. */
197static void kingst_la2016_free_sdi(struct sr_dev_inst *sdi)
198{
199 if (!sdi)
200 return;
201 g_free(sdi->vendor);
202 g_free(sdi->model);
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);
208}
209
210/* Convenience. Open a USB device (including claiming an interface). */
211static int la2016_open_usb(struct sr_usb_dev_inst *usb,
212 libusb_device *dev, gboolean show_message)
213{
214 int ret;
215
216 ret = libusb_open(dev, &usb->devhdl);
217 if (ret != 0) {
218 if (show_message) {
219 sr_err("Cannot open device: %s.",
220 libusb_error_name(ret));
221 }
222 return SR_ERR_IO;
223 }
224
225 if (usb->address == 0xff) {
226 /*
227 * First encounter after firmware upload.
228 * Grab current address after enumeration.
229 */
230 usb->address = libusb_get_device_address(dev);
231 }
232
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?");
236 return SR_ERR_IO;
237 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
238 sr_err("Device has been disconnected.");
239 return SR_ERR_IO;
240 } else if (ret != 0) {
241 sr_err("Cannot claim USB interface: %s.",
242 libusb_error_name(ret));
243 return SR_ERR_IO;
244 }
245
246 return SR_OK;
247}
248
249/* Convenience. Close an opened USB device (and release the interface). */
250static void la2016_close_usb(struct sr_usb_dev_inst *usb)
251{
252
253 if (!usb)
254 return;
255
256 if (usb->devhdl) {
257 libusb_release_interface(usb->devhdl, USB_INTERFACE);
258 libusb_close(usb->devhdl);
259 usb->devhdl = NULL;
260 }
261}
262
263/* Communicate to an USB device to identify the Kingst LA model. */
264static int la2016_identify_read(struct sr_dev_inst *sdi,
265 struct sr_usb_dev_inst *usb, libusb_device *dev,
266 gboolean show_message)
267{
268 int ret;
269
270 ret = la2016_open_usb(usb, dev, show_message);
271 if (ret != SR_OK) {
272 if (show_message)
273 sr_err("Cannot communicate to MCU firmware.");
274 return ret;
275 }
6d53e949
GS
276
277 /*
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.
281 */
d466f61c 282 ret = la2016_identify_device(sdi, show_message);
6d53e949
GS
283 if (ret == SR_OK) {
284 ret = la2016_init_hardware(sdi);
285 }
286
d466f61c
GS
287 la2016_close_usb(usb);
288
289 return ret;
290}
291
292/* Find given conn_id in another USB enum. Identify Kingst LA model. */
293static int la2016_identify_enum(struct sr_dev_inst *sdi)
294{
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;
300 int ret, id_ret;
301 size_t device_count, dev_idx;
302 char conn_id[64];
303
304 di = sdi->driver;
305 drvc = di->context;
306 ctx = drvc->sr_ctx;;
307
308 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
309 if (ret < 0)
310 return SR_ERR_IO;
311 device_count = ret;
312 if (!device_count)
313 return SR_ERR_IO;
314 id_ret = SR_ERR_IO;
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)
319 continue;
320 if (des.iProduct != LA2016_IPRODUCT_INDEX)
321 continue;
322 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
323 if (ret < 0)
324 continue;
325 if (strcmp(sdi->connection_id, conn_id) != 0)
326 continue;
327 id_ret = la2016_identify_read(sdi, sdi->conn, dev, FALSE);
328 break;
329 }
330 libusb_free_device_list(devlist, 1);
331
332 return id_ret;
333}
334
335/* Wait for a device to re-appear after firmware upload. */
336static int la2016_identify_wait(struct sr_dev_inst *sdi)
337{
338 struct dev_context *devc;
339 uint64_t reset_done, now, elapsed_ms;
340 int ret;
341
342 devc = sdi->priv;
343
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);
349 do {
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);
354 if (ret == SR_OK) {
355 devc->fw_uploaded = 0;
356 break;
357 }
358 g_usleep(RENUM_POLL_INTERVAL_MS * 1000);
359 } while (elapsed_ms < RENUM_CHECK_PERIOD_MS);
360 if (ret != SR_OK) {
361 sr_err("Device failed to re-enumerate.");
362 return ret;
363 }
364 sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms);
365
366 return SR_OK;
367}
368
369/*
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().
372 */
373static int la2016_open_enum(struct sr_dev_inst *sdi)
374{
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;
380 int ret, open_ret;
381 size_t device_count, dev_idx;
382 char conn_id[64];
383
384 di = sdi->driver;
385 drvc = di->context;
386 ctx = drvc->sr_ctx;;
387
388 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
389 if (ret < 0)
390 return SR_ERR_IO;
391 device_count = ret;
392 if (!device_count)
393 return SR_ERR_IO;
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)
399 continue;
400 if (des.iProduct != LA2016_IPRODUCT_INDEX)
401 continue;
402 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
403 if (ret < 0)
404 continue;
405 if (strcmp(sdi->connection_id, conn_id) != 0)
406 continue;
407 open_ret = la2016_open_usb(sdi->conn, dev, TRUE);
408 break;
409 }
410 libusb_free_device_list(devlist, 1);
411
412 return open_ret;
413}
414
f2cd2deb
FS
415static GSList *scan(struct sr_dev_driver *di, GSList *options)
416{
417 struct drv_context *drvc;
520a20e9 418 struct sr_context *ctx;
f2cd2deb
FS
419 struct dev_context *devc;
420 struct sr_dev_inst *sdi;
421 struct sr_usb_dev_inst *usb;
422 struct sr_config *src;
423 GSList *l;
d466f61c 424 GSList *devices, *found_devices, *renum_devices;
f2cd2deb
FS
425 GSList *conn_devices;
426 struct libusb_device_descriptor des;
520a20e9
GS
427 libusb_device **devlist, *dev;
428 size_t dev_count, dev_idx, ch_idx;
429 uint8_t bus, addr;
d466f61c 430 uint16_t pid;
f2cd2deb 431 const char *conn;
520a20e9 432 char conn_id[64];
520a20e9 433 int ret;
d466f61c 434 size_t ch_off, ch_max;
331277e0
GS
435 struct sr_channel *ch;
436 struct sr_channel_group *cg;
f2cd2deb
FS
437
438 drvc = di->context;
520a20e9 439 ctx = drvc->sr_ctx;;
f2cd2deb
FS
440
441 conn = NULL;
d466f61c 442 conn_devices = NULL;
f2cd2deb
FS
443 for (l = options; l; l = l->next) {
444 src = l->data;
445 switch (src->key) {
446 case SR_CONF_CONN:
447 conn = g_variant_get_string(src->data, NULL);
448 break;
449 }
450 }
451 if (conn)
520a20e9 452 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
d466f61c
GS
453 if (conn && !conn_devices) {
454 sr_err("Cannot find the specified connection '%s'.", conn);
455 return NULL;
456 }
f2cd2deb 457
d466f61c
GS
458 /*
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.
465 */
f2cd2deb 466 devices = NULL;
d466f61c
GS
467 found_devices = NULL;
468 renum_devices = NULL;
520a20e9
GS
469 ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
470 if (ret < 0) {
471 sr_err("Cannot get device list: %s.", libusb_error_name(ret));
472 return devices;
473 }
474 dev_count = 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);
d466f61c
GS
479
480 /* Filter by connection when externally specified. */
481 for (l = conn_devices; l; l = l->next) {
482 usb = l->data;
483 if (usb->bus == bus && usb->address == addr)
484 break;
485 }
486 if (conn_devices && !l) {
487 sr_spew("Bus %hhu, addr %hhu do not match specified filter.",
488 bus, addr);
489 continue;
f2cd2deb
FS
490 }
491
d466f61c 492 /* Check USB VID:PID. Get the connection string. */
520a20e9 493 libusb_get_device_descriptor(dev, &des);
d466f61c
GS
494 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
495 continue;
496 pid = des.idProduct;
520a20e9
GS
497 ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
498 if (ret < 0)
f2cd2deb 499 continue;
d466f61c
GS
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);
f2cd2deb 503
520a20e9 504 sdi = g_malloc0(sizeof(*sdi));
d466f61c 505 sdi->driver = di;
f2cd2deb 506 sdi->status = SR_ST_INITIALIZING;
d466f61c 507 sdi->inst_type = SR_INST_USB;
520a20e9 508 sdi->connection_id = g_strdup(conn_id);
d466f61c 509 sdi->conn = usb;
f2cd2deb 510
d466f61c
GS
511 devc = g_malloc0(sizeof(*devc));
512 sdi->priv = devc;
f2cd2deb 513
d466f61c
GS
514 /*
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
518 * this device.
519 */
520 devc->fw_uploaded = 0;
91aa0f04 521 devc->usb_pid = pid;
d466f61c
GS
522 if (des.iProduct != LA2016_IPRODUCT_INDEX) {
523 sr_info("Uploading MCU firmware to '%s'.", conn_id);
91aa0f04 524 ret = la2016_upload_firmware(sdi, ctx, dev, FALSE);
520a20e9 525 if (ret != SR_OK) {
91f73872 526 sr_err("MCU firmware upload failed.");
d466f61c 527 kingst_la2016_free_sdi(sdi);
f2cd2deb
FS
528 continue;
529 }
d466f61c
GS
530 devc->fw_uploaded = g_get_monotonic_time();
531 usb->address = 0xff;
532 renum_devices = g_slist_append(renum_devices, sdi);
533 continue;
91aa0f04
GS
534 } else {
535 ret = la2016_upload_firmware(sdi, NULL, NULL, TRUE);
536 if (ret != SR_OK) {
537 sr_err("MCU firmware filename check failed.");
538 kingst_la2016_free_sdi(sdi);
539 continue;
540 }
f2cd2deb
FS
541 }
542
d466f61c
GS
543 /*
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.
549 */
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);
554 continue;
555 }
556 found_devices = g_slist_append(found_devices, sdi);
557 }
558 libusb_free_device_list(devlist, 1);
559 g_slist_free_full(conn_devices, sr_usb_dev_inst_free_cb);
f2cd2deb 560
d466f61c
GS
561 /*
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.
565 */
566 for (l = renum_devices; l; l = l->next) {
567 sdi = l->data;
568 devc = sdi->priv;
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);
573 continue;
520a20e9 574 }
d466f61c
GS
575 found_devices = g_slist_append(found_devices, sdi);
576 }
577 g_slist_free(renum_devices);
f2cd2deb 578
d466f61c
GS
579 /*
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.
584 */
585 for (l = found_devices; l; l = l->next) {
586 sdi = l->data;
587 devc = sdi->priv;
588
589 sdi->vendor = g_strdup("Kingst");
590 sdi->model = g_strdup(devc->model->name);
591 ch_off = 0;
592
331277e0 593 /* Create the "Logic" channel group. */
d466f61c
GS
594 ch_max = ARRAY_SIZE(channel_names_logic);
595 if (ch_max > devc->model->channel_count)
596 ch_max = devc->model->channel_count;
331277e0
GS
597 cg = sr_channel_group_new(sdi, "Logic", NULL);
598 devc->cg_logic = cg;
d466f61c 599 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
331277e0 600 ch = sr_channel_new(sdi, ch_off,
d466f61c
GS
601 SR_CHANNEL_LOGIC, TRUE,
602 channel_names_logic[ch_idx]);
603 ch_off++;
331277e0
GS
604 cg->channels = g_slist_append(cg->channels, ch);
605 }
606
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++) {
610 const char *name;
611 name = channel_names_pwm[ch_idx];
612 cg = sr_channel_group_new(sdi, name, NULL);
613 if (!devc->cg_pwm)
614 devc->cg_pwm = cg;
615 ch = sr_channel_new(sdi, ch_off,
616 SR_CHANNEL_ANALOG, FALSE, name);
617 ch_off++;
618 cg->channels = g_slist_append(cg->channels, ch);
d466f61c 619 }
f2cd2deb 620
08a49848
GS
621 /*
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.
625 */
a38f0f5e 626 sr_sw_limits_init(&devc->sw_limits);
d8fbfcd9
GS
627 devc->sw_limits.limit_samples = 0;
628 devc->capture_ratio = 50;
edc0b015 629 devc->samplerate = devc->model->samplerate;
0fbb464b
GS
630 if (!devc->model->memory_bits)
631 devc->continuous = TRUE;
9270f8f4 632 devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_DFLT;
08a49848
GS
633 if (ARRAY_SIZE(devc->pwm_setting) >= 1) {
634 devc->pwm_setting[0].enabled = FALSE;
635 devc->pwm_setting[0].freq = SR_KHZ(1);
636 devc->pwm_setting[0].duty = 50;
637 }
638 if (ARRAY_SIZE(devc->pwm_setting) >= 2) {
639 devc->pwm_setting[1].enabled = FALSE;
640 devc->pwm_setting[1].freq = SR_KHZ(100);
641 devc->pwm_setting[1].duty = 50;
642 }
f2cd2deb
FS
643
644 sdi->status = SR_ST_INACTIVE;
d466f61c 645 devices = g_slist_append(devices, sdi);
f2cd2deb 646 }
d466f61c 647 g_slist_free(found_devices);
f2cd2deb
FS
648
649 return std_scan_complete(di, devices);
650}
651
f2cd2deb
FS
652static int dev_open(struct sr_dev_inst *sdi)
653{
08a49848 654 struct dev_context *devc;
f2cd2deb 655 int ret;
08a49848
GS
656 size_t ch;
657
658 devc = sdi->priv;
f2cd2deb 659
d466f61c 660 ret = la2016_open_enum(sdi);
f2cd2deb 661 if (ret != SR_OK) {
91f73872 662 sr_err("Cannot open device.");
520a20e9 663 return ret;
f2cd2deb
FS
664 }
665
08a49848
GS
666 /* Send most recent PWM configuration to the device. */
667 for (ch = 0; ch < ARRAY_SIZE(devc->pwm_setting); ch++) {
668 ret = la2016_write_pwm_config(sdi, ch);
669 if (ret != SR_OK)
670 return ret;
6d53e949
GS
671 }
672
f2cd2deb
FS
673 return SR_OK;
674}
675
676static int dev_close(struct sr_dev_inst *sdi)
677{
678 struct sr_usb_dev_inst *usb;
679
680 usb = sdi->conn;
681
682 if (!usb->devhdl)
683 return SR_ERR_BUG;
684
1291ea43
GS
685 la2016_release_resources(sdi);
686
66a24ab5
GS
687 if (WITH_DEINIT_IN_CLOSE)
688 la2016_deinit_hardware(sdi);
f2cd2deb
FS
689
690 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
691 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
d466f61c 692 la2016_close_usb(sdi->conn);
f2cd2deb
FS
693
694 return SR_OK;
695}
696
331277e0
GS
697/* Config API helper. Get type and index of a channel group. */
698static int get_cg_index(const struct sr_dev_inst *sdi,
699 const struct sr_channel_group *cg,
700 int *type, size_t *logic, size_t *analog)
701{
702 struct dev_context *devc;
703 GSList *l;
704 size_t idx;
705
706 /* Preset return values. */
707 if (type)
708 *type = 0;
709 if (logic)
710 *logic = 0;
711 if (analog)
712 *analog = 0;
713
714 /* Start categorizing the received cg. */
715 if (!sdi)
716 return SR_ERR_ARG;
717 devc = sdi->priv;
718 if (!cg)
719 return SR_OK;
720 l = sdi->channel_groups;
721
722 /* First sdi->channelgroups item is "Logic". */
723 if (!l)
724 return SR_ERR_BUG;
725 if (cg == l->data) {
726 if (type)
727 *type = SR_CHANNEL_LOGIC;
728 if (logic)
729 *logic = 0;
730 return SR_OK;
731 }
732 l = l->next;
733
734 /* Next sdi->channelgroups items are "PWMx". */
735 idx = 0;
736 while (l && l->data != cg) {
737 idx++;
738 l = l->next;
739 }
740 if (l && idx < ARRAY_SIZE(devc->pwm_setting)) {
741 if (type)
742 *type = SR_CHANNEL_ANALOG;
743 if (analog)
744 *analog = idx;
745 return SR_OK;
746 }
747
748 return SR_ERR_ARG;
749}
750
955ab604
GS
751static int config_get(uint32_t key, GVariant **data,
752 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
f2cd2deb
FS
753{
754 struct dev_context *devc;
331277e0
GS
755 int ret, cg_type;
756 size_t logic_idx, analog_idx;
757 struct pwm_setting *pwm;
f2cd2deb 758 struct sr_usb_dev_inst *usb;
9270f8f4 759 double voltage, rounded;
f2cd2deb 760
9270f8f4
GS
761 (void)rounded;
762 (void)voltage;
763
f2cd2deb
FS
764 if (!sdi)
765 return SR_ERR_ARG;
766 devc = sdi->priv;
767
331277e0
GS
768 /* Check for types (and index) of channel groups. */
769 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
770 if (cg && ret != SR_OK)
771 return SR_ERR_ARG;
772
773 /* Handle requests for the "Logic" channel group. */
774 if (cg && cg_type == SR_CHANNEL_LOGIC) {
9270f8f4
GS
775 switch (key) {
776#if !WITH_THRESHOLD_DEVCFG
9270f8f4
GS
777 case SR_CONF_VOLTAGE_THRESHOLD:
778 voltage = threshold_voltage(sdi, NULL);
779 *data = std_gvar_tuple_double(voltage, voltage);
780 break;
9270f8f4
GS
781#endif /* WITH_THRESHOLD_DEVCFG */
782 default:
783 return SR_ERR_NA;
784 }
785 return SR_OK;
331277e0
GS
786 }
787
788 /* Handle requests for the "PWMx" channel groups. */
789 if (cg && cg_type == SR_CHANNEL_ANALOG) {
790 pwm = &devc->pwm_setting[analog_idx];
791 switch (key) {
792 case SR_CONF_ENABLED:
793 *data = g_variant_new_boolean(pwm->enabled);
794 break;
795 case SR_CONF_OUTPUT_FREQUENCY:
796 *data = g_variant_new_double(pwm->freq);
797 break;
798 case SR_CONF_DUTY_CYCLE:
799 *data = g_variant_new_double(pwm->duty);
800 break;
801 default:
802 return SR_ERR_NA;
803 }
804 return SR_OK;
805 }
806
f2cd2deb
FS
807 switch (key) {
808 case SR_CONF_CONN:
f2cd2deb 809 usb = sdi->conn;
f2cd2deb
FS
810 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
811 break;
812 case SR_CONF_SAMPLERATE:
edc0b015 813 *data = g_variant_new_uint64(devc->samplerate);
f2cd2deb
FS
814 break;
815 case SR_CONF_LIMIT_SAMPLES:
a38f0f5e
GS
816 case SR_CONF_LIMIT_MSEC:
817 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
f2cd2deb
FS
818 case SR_CONF_CAPTURE_RATIO:
819 *data = g_variant_new_uint64(devc->capture_ratio);
820 break;
9270f8f4 821#if WITH_THRESHOLD_DEVCFG
9270f8f4
GS
822 case SR_CONF_VOLTAGE_THRESHOLD:
823 voltage = threshold_voltage(sdi, NULL);
824 *data = std_gvar_tuple_double(voltage, voltage);
825 break;
9270f8f4 826#endif /* WITH_THRESHOLD_DEVCFG */
0fbb464b
GS
827 case SR_CONF_CONTINUOUS:
828 *data = g_variant_new_boolean(devc->continuous);
829 break;
f2cd2deb
FS
830 default:
831 return SR_ERR_NA;
832 }
833
834 return SR_OK;
835}
836
955ab604
GS
837static int config_set(uint32_t key, GVariant *data,
838 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
f2cd2deb
FS
839{
840 struct dev_context *devc;
331277e0
GS
841 int ret, cg_type;
842 size_t logic_idx, analog_idx;
843 struct pwm_setting *pwm;
c35baf6e 844 double value_f;
f2cd2deb 845 int idx;
0fbb464b 846 gboolean on;
f2cd2deb 847
f2cd2deb
FS
848 devc = sdi->priv;
849
331277e0
GS
850 /* Check for types (and index) of channel groups. */
851 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
852 if (cg && ret != SR_OK)
853 return SR_ERR_ARG;
854
855 /* Handle requests for the "Logic" channel group. */
856 if (cg && cg_type == SR_CHANNEL_LOGIC) {
9270f8f4
GS
857 switch (key) {
858#if !WITH_THRESHOLD_DEVCFG
9270f8f4
GS
859 case SR_CONF_LOGIC_THRESHOLD:
860 idx = std_double_tuple_idx(data,
861 ARRAY_AND_SIZE(threshold_ranges));
862 if (idx < 0)
863 return SR_ERR_ARG;
864 devc->threshold_voltage_idx = idx;
865 break;
9270f8f4
GS
866#endif /* WITH_THRESHOLD_DEVCFG */
867 default:
868 return SR_ERR_NA;
869 }
870 return SR_OK;
331277e0
GS
871 }
872
873 /* Handle requests for the "PWMx" channel groups. */
874 if (cg && cg_type == SR_CHANNEL_ANALOG) {
875 pwm = &devc->pwm_setting[analog_idx];
876 switch (key) {
877 case SR_CONF_ENABLED:
878 pwm->enabled = g_variant_get_boolean(data);
879 ret = la2016_write_pwm_config(sdi, analog_idx);
880 if (ret != SR_OK)
881 return ret;
882 break;
883 case SR_CONF_OUTPUT_FREQUENCY:
c35baf6e
GS
884 value_f = g_variant_get_double(data);
885 if (value_f <= 0.0 || value_f > MAX_PWM_FREQ)
886 return SR_ERR_ARG;
887 pwm->freq = value_f;
331277e0
GS
888 ret = la2016_write_pwm_config(sdi, analog_idx);
889 if (ret != SR_OK)
890 return ret;
891 break;
892 case SR_CONF_DUTY_CYCLE:
c35baf6e
GS
893 value_f = g_variant_get_double(data);
894 if (value_f <= 0.0 || value_f > 100.0)
895 return SR_ERR_ARG;
896 pwm->duty = value_f;
331277e0
GS
897 ret = la2016_write_pwm_config(sdi, analog_idx);
898 if (ret != SR_OK)
899 return ret;
900 break;
901 default:
902 return SR_ERR_NA;
903 }
904 return SR_OK;
905 }
906
f2cd2deb
FS
907 switch (key) {
908 case SR_CONF_SAMPLERATE:
edc0b015 909 devc->samplerate = g_variant_get_uint64(data);
f2cd2deb
FS
910 break;
911 case SR_CONF_LIMIT_SAMPLES:
a38f0f5e
GS
912 case SR_CONF_LIMIT_MSEC:
913 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
f2cd2deb
FS
914 case SR_CONF_CAPTURE_RATIO:
915 devc->capture_ratio = g_variant_get_uint64(data);
916 break;
9270f8f4 917#if WITH_THRESHOLD_DEVCFG
9270f8f4
GS
918 case SR_CONF_VOLTAGE_THRESHOLD:
919 idx = std_double_tuple_idx(data,
920 ARRAY_AND_SIZE(threshold_ranges));
921 if (idx < 0)
922 return SR_ERR_ARG;
923 devc->threshold_voltage_idx = idx;
924 break;
9270f8f4 925#endif /* WITH_THRESHOLD_DEVCFG */
0fbb464b
GS
926 case SR_CONF_CONTINUOUS:
927 on = g_variant_get_boolean(data);
928 if (!devc->model->memory_bits && !on)
929 return SR_ERR_ARG;
930 devc->continuous = on;
931 break;
f2cd2deb
FS
932 default:
933 return SR_ERR_NA;
934 }
935
936 return SR_OK;
937}
938
955ab604
GS
939static int config_list(uint32_t key, GVariant **data,
940 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
f2cd2deb 941{
8b172e78 942 struct dev_context *devc;
331277e0
GS
943 int ret, cg_type;
944 size_t logic_idx, analog_idx;
8b172e78 945
a38f0f5e
GS
946 devc = sdi ? sdi->priv : NULL;
947
331277e0
GS
948 /* Check for types (and index) of channel groups. */
949 ret = get_cg_index(sdi, cg, &cg_type, &logic_idx, &analog_idx);
950 if (cg && ret != SR_OK)
951 return SR_ERR_ARG;
952
953 /* Handle requests for the "Logic" channel group. */
954 if (cg && cg_type == SR_CHANNEL_LOGIC) {
9270f8f4
GS
955 switch (key) {
956 case SR_CONF_DEVICE_OPTIONS:
957 if (ARRAY_SIZE(devopts_cg_logic) == 0)
958 return SR_ERR_NA;
959 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
960 devopts_cg_logic, ARRAY_SIZE(devopts_cg_logic),
961 sizeof(devopts_cg_logic[0]));
962 break;
963#if !WITH_THRESHOLD_DEVCFG
9270f8f4
GS
964 case SR_CONF_VOLTAGE_THRESHOLD:
965 *data = std_gvar_thresholds(ARRAY_AND_SIZE(threshold_ranges));
966 break;
9270f8f4
GS
967#endif /* WITH_THRESHOLD_DEVCFG */
968 default:
969 return SR_ERR_NA;
970 }
971 return SR_OK;
331277e0
GS
972 }
973
974 /* Handle requests for the "PWMx" channel groups. */
975 if (cg && cg_type == SR_CHANNEL_ANALOG) {
976 switch (key) {
977 case SR_CONF_DEVICE_OPTIONS:
978 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
979 devopts_cg_pwm, ARRAY_SIZE(devopts_cg_pwm),
980 sizeof(devopts_cg_pwm[0]));
981 break;
982 default:
983 return SR_ERR_NA;
984 }
985 return SR_OK;
986 }
987
f2cd2deb
FS
988 switch (key) {
989 case SR_CONF_SCAN_OPTIONS:
990 case SR_CONF_DEVICE_OPTIONS:
411ad77c
GS
991 return STD_CONFIG_LIST(key, data, sdi, cg,
992 scanopts, drvopts, devopts);
f2cd2deb 993 case SR_CONF_SAMPLERATE:
fb28e72d
MW
994 if (!sdi)
995 return SR_ERR_ARG;
d466f61c 996 if (devc->model->samplerate == SR_MHZ(500))
330853ba 997 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_500mhz));
d466f61c 998 else if (devc->model->samplerate == SR_MHZ(200))
330853ba 999 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_200mhz));
4276ca94 1000 else if (devc->model->samplerate == SR_MHZ(100))
330853ba 1001 *data = std_gvar_samplerates(ARRAY_AND_SIZE(rates_100mhz));
4276ca94
GS
1002 else
1003 return SR_ERR_BUG;
f2cd2deb
FS
1004 break;
1005 case SR_CONF_LIMIT_SAMPLES:
d8fbfcd9 1006 *data = std_gvar_tuple_u64(0, LA2016_NUM_SAMPLES_MAX);
f2cd2deb 1007 break;
9270f8f4 1008#if WITH_THRESHOLD_DEVCFG
9270f8f4
GS
1009 case SR_CONF_VOLTAGE_THRESHOLD:
1010 *data = std_gvar_thresholds(ARRAY_AND_SIZE(threshold_ranges));
1011 break;
9270f8f4 1012#endif /* WITH_THRESHOLD_DEVCFG */
f2cd2deb
FS
1013 case SR_CONF_TRIGGER_MATCH:
1014 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
1015 break;
f2cd2deb
FS
1016 default:
1017 return SR_ERR_NA;
1018 }
1019
1020 return SR_OK;
1021}
1022
f2cd2deb
FS
1023static int dev_acquisition_start(const struct sr_dev_inst *sdi)
1024{
1025 struct sr_dev_driver *di;
1026 struct drv_context *drvc;
520a20e9 1027 struct sr_context *ctx;
f2cd2deb 1028 struct dev_context *devc;
4276ca94 1029 size_t unitsize;
9270f8f4 1030 double voltage;
f2cd2deb
FS
1031 int ret;
1032
1033 di = sdi->driver;
1034 drvc = di->context;
520a20e9 1035 ctx = drvc->sr_ctx;;
f2cd2deb
FS
1036 devc = sdi->priv;
1037
a38f0f5e 1038 if (!devc->feed_queue) {
4276ca94
GS
1039 if (devc->model->channel_count == 32)
1040 unitsize = sizeof(uint32_t);
1041 else if (devc->model->channel_count == 16)
1042 unitsize = sizeof(uint16_t);
1043 else
1044 return SR_ERR_ARG;
a38f0f5e 1045 devc->feed_queue = feed_queue_logic_alloc(sdi,
4276ca94 1046 LA2016_CONVBUFFER_SIZE, unitsize);
a38f0f5e
GS
1047 if (!devc->feed_queue) {
1048 sr_err("Cannot allocate buffer for session feed.");
1049 return SR_ERR_MALLOC;
1050 }
038e65c1
GS
1051 devc->packets_per_chunk = TRANSFER_PACKET_LENGTH;
1052 devc->packets_per_chunk--;
1053 devc->packets_per_chunk /= unitsize + sizeof(uint8_t);
f2cd2deb
FS
1054 }
1055
a38f0f5e
GS
1056 sr_sw_limits_acquisition_start(&devc->sw_limits);
1057
9270f8f4
GS
1058 voltage = threshold_voltage(sdi, NULL);
1059 ret = la2016_setup_acquisition(sdi, voltage);
411ad77c 1060 if (ret != SR_OK) {
a38f0f5e
GS
1061 feed_queue_logic_free(devc->feed_queue);
1062 devc->feed_queue = NULL;
f2cd2deb
FS
1063 return ret;
1064 }
1065
411ad77c
GS
1066 ret = la2016_start_acquisition(sdi);
1067 if (ret != SR_OK) {
3ebc1cb2 1068 la2016_abort_acquisition(sdi);
a38f0f5e
GS
1069 feed_queue_logic_free(devc->feed_queue);
1070 devc->feed_queue = NULL;
f2cd2deb
FS
1071 return ret;
1072 }
1073
cf057ac4 1074 devc->completion_seen = FALSE;
520a20e9 1075 usb_source_add(sdi->session, ctx, 50,
388438e4 1076 la2016_receive_data, (void *)sdi);
f2cd2deb
FS
1077
1078 std_session_send_df_header(sdi);
1079
1080 return SR_OK;
1081}
1082
1083static int dev_acquisition_stop(struct sr_dev_inst *sdi)
1084{
1085 int ret;
1086
1087 ret = la2016_abort_acquisition(sdi);
f2cd2deb
FS
1088
1089 return ret;
1090}
1091
1092static struct sr_dev_driver kingst_la2016_driver_info = {
1093 .name = "kingst-la2016",
1094 .longname = "Kingst LA2016",
1095 .api_version = 1,
1096 .init = std_init,
1097 .cleanup = std_cleanup,
1098 .scan = scan,
1099 .dev_list = std_dev_list,
1100 .dev_clear = std_dev_clear,
1101 .config_get = config_get,
1102 .config_set = config_set,
1103 .config_list = config_list,
1104 .dev_open = dev_open,
1105 .dev_close = dev_close,
1106 .dev_acquisition_start = dev_acquisition_start,
1107 .dev_acquisition_stop = dev_acquisition_stop,
1108 .context = NULL,
1109};
1110SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);