]> sigrok.org Git - libsigrok.git/blame - src/hardware/kingst-la2016/api.c
kingst-la2016: tested with idVendor=77a1, idProduct=01a2
[libsigrok.git] / src / hardware / kingst-la2016 / api.c
CommitLineData
f2cd2deb
FS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2020 Florian Schmidt <schmidt_florian@gmx.de>
5 * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
6 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/* mostly stolen from src/hardware/saleae-logic16/ */
24
25#include <config.h>
26#include <glib.h>
27#include <libusb.h>
28#include <stdlib.h>
29#include <string.h>
30#include <math.h>
31#include <libsigrok/libsigrok.h>
32#include "libsigrok-internal.h"
33#include "protocol.h"
34
35static const uint32_t scanopts[] = {
36 SR_CONF_CONN,
37};
38
39static const uint32_t drvopts[] = {
40 SR_CONF_LOGIC_ANALYZER,
41};
42
43static const uint32_t devopts[] = {
44 /* TODO: SR_CONF_CONTINUOUS, */
45 SR_CONF_CONN | SR_CONF_GET,
46 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_GET | SR_CONF_LIST,
48 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
49 SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
51 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
52 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
53};
54
55static const int32_t trigger_matches[] = {
56 SR_TRIGGER_ZERO,
57 SR_TRIGGER_ONE,
58 SR_TRIGGER_RISING,
59 SR_TRIGGER_FALLING,
60};
61
62static const char *channel_names[] = {
63 "0", "1", "2", "3", "4", "5", "6", "7", "8",
64 "9", "10", "11", "12", "13", "14", "15",
65};
66
67static const uint64_t samplerates[] = {
68 SR_KHZ(20),
69 SR_KHZ(50),
70 SR_KHZ(100),
71 SR_KHZ(200),
72 SR_KHZ(500),
73 SR_MHZ(1),
74 SR_MHZ(2),
75 SR_MHZ(4),
76 SR_MHZ(5),
77 SR_MHZ(8),
78 SR_MHZ(10),
79 SR_MHZ(20),
80 SR_MHZ(50),
81 SR_MHZ(100),
82 SR_MHZ(200),
83};
84
85static const float logic_threshold_value[] = {
86 1.58,
87 2.5,
88 1.165,
89 1.5,
90 1.25,
91 0.9,
92 0.75,
93 0.60,
94 0.45,
95};
96
97static const char *logic_threshold[] = {
98 "TTL 5V",
99 "CMOS 5V",
100 "CMOS 3.3V",
101 "CMOS 3.0V",
102 "CMOS 2.5V",
103 "CMOS 1.8V",
104 "CMOS 1.5V",
105 "CMOS 1.2V",
106 "CMOS 0.9V",
107 "USER",
108};
109
110#define MAX_NUM_LOGIC_THRESHOLD_ENTRIES ARRAY_SIZE(logic_threshold)
111
112#define array_length(a) (sizeof(a) / sizeof((a)[0]))
113
114static GSList *scan(struct sr_dev_driver *di, GSList *options)
115{
116 struct drv_context *drvc;
117 struct dev_context *devc;
118 struct sr_dev_inst *sdi;
119 struct sr_usb_dev_inst *usb;
120 struct sr_config *src;
121 GSList *l;
122 GSList *devices;
123 GSList *conn_devices;
124 struct libusb_device_descriptor des;
125 libusb_device **devlist;
126 unsigned int i, j;
127 const char *conn;
128 char connection_id[64];
129 int64_t fw_updated;
130 unsigned int dev_addr;
131
132 drvc = di->context;
133
134 conn = NULL;
135 for (l = options; l; l = l->next) {
136 src = l->data;
137 switch (src->key) {
138 case SR_CONF_CONN:
139 conn = g_variant_get_string(src->data, NULL);
140 break;
141 }
142 }
143 if (conn)
144 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
145 else
146 conn_devices = NULL;
147
148 /* Find all LA2016 devices and upload firmware to them. */
149 devices = NULL;
150 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
151 for (i = 0; devlist[i]; i++) {
152 if (conn) {
153 usb = NULL;
154 for (l = conn_devices; l; l = l->next) {
155 usb = l->data;
156 if (usb->bus == libusb_get_bus_number(devlist[i])
157 && usb->address == libusb_get_device_address(devlist[i]))
158 break;
159 }
160 if (!l)
161 /* This device matched none of the ones that
162 * matched the conn specification. */
163 continue;
164 }
165
166 libusb_get_device_descriptor(devlist[i], &des);
167
168 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
169 continue;
170
171 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
172 continue;
173
174 /* Already has the firmware */
175 sr_dbg("Found a LA2016 device.");
176 sdi = g_malloc0(sizeof(struct sr_dev_inst));
177 sdi->status = SR_ST_INITIALIZING;
178 sdi->connection_id = g_strdup(connection_id);
179
180 fw_updated = 0;
181 dev_addr = libusb_get_device_address(devlist[i]);
182 if (des.iProduct != 2) {
183 sr_info("device at '%s' has no firmware loaded!", connection_id);
184
185 if (la2016_upload_firmware(drvc->sr_ctx, devlist[i], des.idProduct) != SR_OK) {
186 sr_err("uC firmware upload failed!");
187 g_free(sdi->connection_id);
188 g_free(sdi);
189 continue;
190 }
191 fw_updated = g_get_monotonic_time();
192 dev_addr = 0xff; /* to mark that we don't know address yet... ugly */
193 }
194
195 sdi->vendor = g_strdup("Kingst");
196 sdi->model = g_strdup("LA2016");
197
198 for (j = 0; j < ARRAY_SIZE(channel_names); j++)
199 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_names[j]);
200
201 devices = g_slist_append(devices, sdi);
202
203 devc = g_malloc0(sizeof(struct dev_context));
204 sdi->priv = devc;
205 devc->fw_updated = fw_updated;
206 devc->threshold_voltage_idx = 0;
207 devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
208
209 sdi->status = SR_ST_INACTIVE;
210 sdi->inst_type = SR_INST_USB;
211
212 sdi->conn = sr_usb_dev_inst_new(
213 libusb_get_bus_number(devlist[i]),
214 dev_addr, NULL);
215 }
216 libusb_free_device_list(devlist, 1);
217 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
218
219 return std_scan_complete(di, devices);
220}
221
222static int la2016_dev_open(struct sr_dev_inst *sdi)
223{
224 struct sr_dev_driver *di;
225 libusb_device **devlist;
226 struct sr_usb_dev_inst *usb;
227 struct libusb_device_descriptor des;
228 struct drv_context *drvc;
229 int ret, i, device_count;
230 char connection_id[64];
231
232 di = sdi->driver;
233 drvc = di->context;
234 usb = sdi->conn;
235 ret = SR_ERR;
236
237 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
238 if (device_count < 0) {
239 sr_err("Failed to get device list: %s.", libusb_error_name(device_count));
240 return SR_ERR;
241 }
242
243 for (i = 0; i < device_count; i++) {
244 libusb_get_device_descriptor(devlist[i], &des);
245
246 if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID || des.iProduct != 2)
247 continue;
248
249 if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) {
250 /*
251 * Check device by its physical USB bus/port address.
252 */
253 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
254 continue;
255
256 if (strcmp(sdi->connection_id, connection_id))
257 /* This is not the one. */
258 continue;
259 }
260
261 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
262 if (usb->address == 0xff)
263 /*
264 * First time we touch this device after FW
265 * upload, so we don't know the address yet.
266 */
267 usb->address = libusb_get_device_address(devlist[i]);
268 } else {
269 sr_err("Failed to open device: %s.", libusb_error_name(ret));
270 ret = SR_ERR;
271 break;
272 }
273
274 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
275 if (ret == LIBUSB_ERROR_BUSY) {
276 sr_err("Unable to claim USB interface. Another "
277 "program or driver has already claimed it.");
278 ret = SR_ERR;
279 break;
280 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
281 sr_err("Device has been disconnected.");
282 ret = SR_ERR;
283 break;
284 } else if (ret != 0) {
285 sr_err("Unable to claim interface: %s.", libusb_error_name(ret));
286 ret = SR_ERR;
287 break;
288 }
289
290 if ((ret = la2016_init_device(sdi)) != SR_OK) {
291 sr_err("Failed to init device.");
292 break;
293 }
294
295 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
296 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
297
298 ret = SR_OK;
299
300 break;
301 }
302
303 libusb_free_device_list(devlist, 1);
304
305 if (ret != SR_OK) {
306 if (usb->devhdl) {
307 libusb_release_interface(usb->devhdl, USB_INTERFACE);
308 libusb_close(usb->devhdl);
309 usb->devhdl = NULL;
310 }
311 return SR_ERR;
312 }
313
314 return SR_OK;
315}
316
317static int dev_open(struct sr_dev_inst *sdi)
318{
319 struct dev_context *devc;
320 int64_t timediff_us, timediff_ms;
321 uint64_t reset_done;
322 uint64_t now;
323 int ret;
324
325 devc = sdi->priv;
326
327 /*
328 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
329 * milliseconds for the FX2 to renumerate.
330 */
331 ret = SR_ERR;
332 if (devc->fw_updated > 0) {
333 sr_info("Waiting for device to reset after firmware upload.");
334 /* Takes >= 2000ms for the uC to be gone from the USB bus. */
335 reset_done = devc->fw_updated + 18 * (uint64_t)1e5; /* 1.8 seconds */
336 now = g_get_monotonic_time();
337 if (reset_done > now)
338 g_usleep(reset_done - now);
339 timediff_ms = 0;
340 while (timediff_ms < MAX_RENUM_DELAY_MS) {
341 g_usleep(200 * 1000);
342
343 timediff_us = g_get_monotonic_time() - devc->fw_updated;
344 timediff_ms = timediff_us / 1000;
345
346 if ((ret = la2016_dev_open(sdi)) == SR_OK)
347 break;
348 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
349 }
350 if (ret != SR_OK) {
351 sr_err("Device failed to re-enumerate.");
352 return SR_ERR;
353 }
354 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
355 } else
356 ret = la2016_dev_open(sdi);
357
358 if (ret != SR_OK) {
359 sr_err("Unable to open device.");
360 return SR_ERR;
361 }
362
363 return SR_OK;
364}
365
366static int dev_close(struct sr_dev_inst *sdi)
367{
368 struct sr_usb_dev_inst *usb;
369
370 usb = sdi->conn;
371
372 if (!usb->devhdl)
373 return SR_ERR_BUG;
374
375 la2016_deinit_device(sdi);
376
377 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
378 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
379 libusb_release_interface(usb->devhdl, USB_INTERFACE);
380 libusb_close(usb->devhdl);
381 usb->devhdl = NULL;
382
383 return SR_OK;
384}
385
386static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
387{
388 struct dev_context *devc;
389 struct sr_usb_dev_inst *usb;
390 double rounded;
391
392 (void)cg;
393
394 if (!sdi)
395 return SR_ERR_ARG;
396 devc = sdi->priv;
397
398 switch (key) {
399 case SR_CONF_CONN:
400 if (!sdi->conn)
401 return SR_ERR_ARG;
402 usb = sdi->conn;
403 if (usb->address == 255)
404 /* Device still needs to re-enumerate after firmware
405 * upload, so we don't know its (future) address. */
406 return SR_ERR;
407 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
408 break;
409 case SR_CONF_SAMPLERATE:
410 *data = g_variant_new_uint64(devc->cur_samplerate);
411 break;
412 case SR_CONF_LIMIT_SAMPLES:
413 *data = g_variant_new_uint64(devc->limit_samples);
414 break;
415 case SR_CONF_CAPTURE_RATIO:
416 *data = g_variant_new_uint64(devc->capture_ratio);
417 break;
418 case SR_CONF_VOLTAGE_THRESHOLD:
419 rounded = (int)(devc->threshold_voltage / 0.1) * 0.1;
420 *data = std_gvar_tuple_double(rounded, rounded + 0.1);
421 return SR_OK;
422 case SR_CONF_LOGIC_THRESHOLD:
423 *data = g_variant_new_string(logic_threshold[devc->threshold_voltage_idx]);
424 break;
425 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
426 *data = g_variant_new_double(devc->threshold_voltage);
427 break;
428
429 default:
430 return SR_ERR_NA;
431 }
432
433 return SR_OK;
434}
435
436static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
437{
438 struct dev_context *devc;
439 double low, high;
440 int idx;
441
442 (void)cg;
443
444 devc = sdi->priv;
445
446 switch (key) {
447 case SR_CONF_SAMPLERATE:
448 devc->cur_samplerate = g_variant_get_uint64(data);
449 break;
450 case SR_CONF_LIMIT_SAMPLES:
451 devc->limit_samples = g_variant_get_uint64(data);
452 break;
453 case SR_CONF_CAPTURE_RATIO:
454 devc->capture_ratio = g_variant_get_uint64(data);
455 break;
456 case SR_CONF_VOLTAGE_THRESHOLD:
457 g_variant_get(data, "(dd)", &low, &high);
458 devc->threshold_voltage = (low + high) / 2.0;
459 devc->threshold_voltage_idx = MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1; /* USER */
460 break;
461 case SR_CONF_LOGIC_THRESHOLD: {
462 if ((idx = std_str_idx(data, logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES)) < 0)
463 return SR_ERR_ARG;
464 if (idx == MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1) {
465 /* user threshold */
466 } else {
467 devc->threshold_voltage = logic_threshold_value[idx];
468 }
469 devc->threshold_voltage_idx = idx;
470 break;
471 }
472 case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
473 devc->threshold_voltage = g_variant_get_double(data);
474 break;
475 default:
476 return SR_ERR_NA;
477 }
478
479 return SR_OK;
480}
481
482static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
483{
484 switch (key) {
485 case SR_CONF_SCAN_OPTIONS:
486 case SR_CONF_DEVICE_OPTIONS:
487 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
488 case SR_CONF_SAMPLERATE:
489 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
490 break;
491 case SR_CONF_LIMIT_SAMPLES:
492 *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX);
493 break;
494 case SR_CONF_VOLTAGE_THRESHOLD:
495 *data = std_gvar_min_max_step_thresholds(
496 LA2016_THR_VOLTAGE_MIN,
497 LA2016_THR_VOLTAGE_MAX, 0.1);
498 break;
499 case SR_CONF_TRIGGER_MATCH:
500 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
501 break;
502 case SR_CONF_LOGIC_THRESHOLD:
503 *data = g_variant_new_strv(logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES);
504 break;
505 default:
506 return SR_ERR_NA;
507 }
508
509 return SR_OK;
510}
511
512static void send_chunk(struct sr_dev_inst *sdi, transfer_packet_t *packets, unsigned int num_tfers)
513{
514 struct dev_context *devc;
515 struct sr_datafeed_logic logic;
516 struct sr_datafeed_packet sr_packet;
517 transfer_packet_t *packet;
518 acq_packet_t *p;
519 unsigned int max_samples, n_samples, total_samples, free_n_samples, ptotal;
520 unsigned int i, j, k;
521 int do_signal_trigger;
522 uint16_t *wp;
523
524 devc = sdi->priv;
525
526 logic.unitsize = 2;
527 logic.data = devc->convbuffer;
528
529 sr_packet.type = SR_DF_LOGIC;
530 sr_packet.payload = &logic;
531
532 max_samples = devc->convbuffer_size / 2;
533 n_samples = 0;
534 wp = (uint16_t*)devc->convbuffer;
535 total_samples = 0;
536 do_signal_trigger = 0;
537
538 if (devc->had_triggers_configured && devc->reading_behind_trigger == 0 && devc->info.n_rep_packets_before_trigger == 0) {
539 sr_packet.type = SR_DF_TRIGGER;
540 sr_packet.payload = NULL;
541 sr_session_send(sdi, &sr_packet);
542 devc->reading_behind_trigger = 1;
543
544 do_signal_trigger = 0;
545 sr_packet.type = SR_DF_LOGIC;
546 sr_packet.payload = &logic;
547 }
548
549 for (i = 0; i < num_tfers; i++) {
550 transfer_packet_host(packets[i]);
551 packet = packets + i;
552 ptotal = 0;
553 for (k = 0; k < array_length(packet->packet); k++) {
554 free_n_samples = max_samples - n_samples;
555 if (free_n_samples < 256 || do_signal_trigger) {
556 logic.length = n_samples * 2;
557 sr_session_send(sdi, &sr_packet);
558 n_samples = 0;
559 wp = (uint16_t*)devc->convbuffer;
560 if (do_signal_trigger) {
561 sr_packet.type = SR_DF_TRIGGER;
562 sr_packet.payload = NULL;
563 sr_session_send(sdi, &sr_packet);
564
565 do_signal_trigger = 0;
566 sr_packet.type = SR_DF_LOGIC;
567 sr_packet.payload = &logic;
568 }
569 }
570 p = packet->packet + k;
571 for (j = 0; j < p->repetitions; j++)
572 *(wp++) = p->state;
573 n_samples += p->repetitions;
574 total_samples += p->repetitions;
575 ptotal += p->repetitions;
576 devc->total_samples += p->repetitions;
577 if (!devc->reading_behind_trigger) {
578 devc->n_reps_until_trigger --;
579 if (devc->n_reps_until_trigger == 0) {
580 devc->reading_behind_trigger = 1;
581 do_signal_trigger = 1;
582 sr_dbg(" here is trigger position after %" PRIu64 " samples, %.6fms",
583 devc->total_samples,
584 (double)devc->total_samples / devc->cur_samplerate * 1e3);
585 }
586 }
587 }
588 }
589 if (n_samples) {
590 logic.length = n_samples * 2;
591 sr_session_send(sdi, &sr_packet);
592 if (do_signal_trigger) {
593 sr_packet.type = SR_DF_TRIGGER;
594 sr_packet.payload = NULL;
595 sr_session_send(sdi, &sr_packet);
596 }
597 }
598 sr_dbg("send_chunk done after %d samples", total_samples);
599}
600
601static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
602{
603 struct sr_dev_inst *sdi;
604 struct dev_context *devc;
605 struct sr_usb_dev_inst *usb;
606 int ret;
607
608 sdi = transfer->user_data;
609 devc = sdi->priv;
610 usb = sdi->conn;
611
612 sr_dbg("receive_transfer(): status %s received %d bytes.",
613 libusb_error_name(transfer->status), transfer->actual_length);
614
615 if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) {
616 sr_err("bulk transfer timeout!");
617 devc->transfer_finished = 1;
618 }
619 send_chunk(sdi, (transfer_packet_t*)transfer->buffer, transfer->actual_length / sizeof(transfer_packet_t));
620
621 devc->n_bytes_to_read -= transfer->actual_length;
622 if (devc->n_bytes_to_read) {
623 uint32_t to_read = devc->n_bytes_to_read;
624 if (to_read > LA2016_BULK_MAX)
625 to_read = LA2016_BULK_MAX;
626 libusb_fill_bulk_transfer(
627 transfer, usb->devhdl,
628 0x86, transfer->buffer, to_read,
629 receive_transfer, (void*)sdi, DEFAULT_TIMEOUT_MS);
630
631 if ((ret = libusb_submit_transfer(transfer)) == 0)
632 return;
633 sr_err("Failed to submit further transfer: %s.", libusb_error_name(ret));
634 }
635
636 g_free(transfer->buffer);
637 libusb_free_transfer(transfer);
638 devc->transfer_finished = 1;
639}
640
641static int handle_event(int fd, int revents, void *cb_data)
642{
643 const struct sr_dev_inst *sdi;
644 struct dev_context *devc;
645 struct drv_context *drvc;
646 struct sr_datafeed_packet packet;
647 struct timeval tv;
648
649 (void)fd;
650 (void)revents;
651
652 sdi = cb_data;
653 devc = sdi->priv;
654 drvc = sdi->driver->context;
655
656 if (devc->have_trigger == 0) {
657 if (la2016_has_triggered(sdi) == 0) {
658 sr_dbg("not yet ready for download...");
659 return TRUE;
660 }
661 devc->have_trigger = 1;
662 devc->transfer_finished = 0;
663 devc->reading_behind_trigger = 0;
664 devc->total_samples = 0;
665 /* we can start retrieving data! */
666 if (la2016_start_retrieval(sdi, receive_transfer) != SR_OK) {
667 sr_err("failed to start retrieval!");
668 return FALSE;
669 }
670 sr_dbg("retrieval is started...");
671 packet.type = SR_DF_FRAME_BEGIN;
672 sr_session_send(sdi, &packet);
673
674 return TRUE;
675 }
676
677 tv.tv_sec = tv.tv_usec = 0;
678 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
679
680 if (devc->transfer_finished) {
681 sr_dbg("transfer is finished!");
682 packet.type = SR_DF_FRAME_END;
683 sr_session_send(sdi, &packet);
684
685 usb_source_remove(sdi->session, drvc->sr_ctx);
686 std_session_send_df_end(sdi);
687
688 la2016_stop_acquisition(sdi);
689
690 g_free(devc->convbuffer);
691 devc->convbuffer = NULL;
692
693 sr_dbg("transfer is now finished");
694 }
695
696 return TRUE;
697}
698
699static void abort_acquisition(struct dev_context *devc)
700{
701 if (devc->transfer)
702 libusb_cancel_transfer(devc->transfer);
703}
704
705static int configure_channels(const struct sr_dev_inst *sdi)
706{
707 struct dev_context *devc;
708
709 devc = sdi->priv;
710 devc->cur_channels = 0;
711 devc->num_channels = 0;
712
713 for (GSList *l = sdi->channels; l; l = l->next) {
714 struct sr_channel *ch = (struct sr_channel*)l->data;
715 if (ch->enabled == FALSE)
716 continue;
717 devc->cur_channels |= 1 << (ch->index);
718 devc->num_channels++;
719 }
720
721 return SR_OK;
722}
723
724static int dev_acquisition_start(const struct sr_dev_inst *sdi)
725{
726 struct sr_dev_driver *di;
727 struct drv_context *drvc;
728 struct dev_context *devc;
729 int ret;
730
731 di = sdi->driver;
732 drvc = di->context;
733 devc = sdi->priv;
734
735 if (configure_channels(sdi) != SR_OK) {
736 sr_err("Failed to configure channels.");
737 return SR_ERR;
738 }
739
740 devc->convbuffer_size = 4 * 1024 * 1024;
741 if (!(devc->convbuffer = g_try_malloc(devc->convbuffer_size))) {
742 sr_err("Conversion buffer malloc failed.");
743 return SR_ERR_MALLOC;
744 }
745
746 if ((ret = la2016_setup_acquisition(sdi)) != SR_OK) {
747 g_free(devc->convbuffer);
748 devc->convbuffer = NULL;
749 return ret;
750 }
751
752 devc->ctx = drvc->sr_ctx;
753
754 if ((ret = la2016_start_acquisition(sdi)) != SR_OK) {
755 abort_acquisition(devc);
756 return ret;
757 }
758
759 devc->have_trigger = 0;
760 usb_source_add(sdi->session, drvc->sr_ctx, 50, handle_event, (void*)sdi);
761
762 std_session_send_df_header(sdi);
763
764 return SR_OK;
765}
766
767static int dev_acquisition_stop(struct sr_dev_inst *sdi)
768{
769 int ret;
770
771 ret = la2016_abort_acquisition(sdi);
772 abort_acquisition(sdi->priv);
773
774 return ret;
775}
776
777static struct sr_dev_driver kingst_la2016_driver_info = {
778 .name = "kingst-la2016",
779 .longname = "Kingst LA2016",
780 .api_version = 1,
781 .init = std_init,
782 .cleanup = std_cleanup,
783 .scan = scan,
784 .dev_list = std_dev_list,
785 .dev_clear = std_dev_clear,
786 .config_get = config_get,
787 .config_set = config_set,
788 .config_list = config_list,
789 .dev_open = dev_open,
790 .dev_close = dev_close,
791 .dev_acquisition_start = dev_acquisition_start,
792 .dev_acquisition_stop = dev_acquisition_stop,
793 .context = NULL,
794};
795SR_REGISTER_DEV_DRIVER(kingst_la2016_driver_info);