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