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