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