]> sigrok.org Git - libsigrok.git/blame - hardware/hantek-dso/api.c
sr: remove unused argument from hardware driver function init()
[libsigrok.git] / hardware / hantek-dso / api.c
CommitLineData
3b533202
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdio.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <unistd.h>
27#include <string.h>
28#include <sys/time.h>
29#include <inttypes.h>
3b533202
BV
30#include <glib.h>
31#include <libusb.h>
45c59c8b
BV
32#include "libsigrok.h"
33#include "libsigrok-internal.h"
3b533202
BV
34#include "config.h"
35#include "dso.h"
36
37
fc8fe3e3
BV
38/* Max time in ms before we want to check on USB events */
39/* TODO tune this properly */
3b533202
BV
40#define TICK 1
41
62bb8840 42static const int hwcaps[] = {
3b533202 43 SR_HWCAP_OSCILLOSCOPE,
ae88b97b 44 SR_HWCAP_LIMIT_SAMPLES,
3b533202 45 SR_HWCAP_CONTINUOUS,
a370ef19
BV
46 SR_HWCAP_TIMEBASE,
47 SR_HWCAP_BUFFERSIZE,
48 SR_HWCAP_TRIGGER_SOURCE,
49 SR_HWCAP_TRIGGER_SLOPE,
50 SR_HWCAP_HORIZ_TRIGGERPOS,
ebb781a6 51 SR_HWCAP_FILTER,
313deed2 52 SR_HWCAP_VDIV,
4a090d72 53 SR_HWCAP_COUPLING,
3b533202
BV
54 0,
55};
56
57static const char *probe_names[] = {
58 "CH1",
59 "CH2",
60 NULL,
61};
62
62bb8840 63static const struct dso_profile dev_profiles[] = {
88a13f30 64 { 0x04b4, 0x2090, 0x04b5, 0x2090,
3b533202 65 "Hantek", "DSO-2090",
88a13f30
BV
66 FIRMWARE_DIR "/hantek-dso-2xxx.fw" },
67 { 0x04b4, 0x2150, 0x04b5, 0x2150,
68 "Hantek", "DSO-2150",
69 FIRMWARE_DIR "/hantek-dso-2xxx.fw" },
70 { 0x04b4, 0x2250, 0x04b5, 0x2250,
71 "Hantek", "DSO-2250",
72 FIRMWARE_DIR "/hantek-dso-2xxx.fw" },
73 { 0x04b4, 0x5200, 0x04b5, 0x5200,
74 "Hantek", "DSO-5200",
75 FIRMWARE_DIR "/hantek-dso-5xxx.fw" },
76 { 0x04b4, 0x520a, 0x04b5, 0x520a,
77 "Hantek", "DSO-5200A",
78 FIRMWARE_DIR "/hantek-dso-5xxx.fw" },
79 { 0, 0, 0, 0, 0, 0, 0 },
3b533202
BV
80};
81
62bb8840 82static const uint64_t buffersizes[] = {
a370ef19
BV
83 10240,
84 32768,
85 /* TODO: 65535 */
62bb8840 86 0,
a370ef19
BV
87};
88
62bb8840 89static const struct sr_rational timebases[] = {
a370ef19
BV
90 /* microseconds */
91 { 10, 1000000 },
92 { 20, 1000000 },
93 { 40, 1000000 },
94 { 100, 1000000 },
95 { 200, 1000000 },
96 { 400, 1000000 },
97 /* milliseconds */
98 { 1, 1000 },
99 { 2, 1000 },
100 { 4, 1000 },
101 { 10, 1000 },
102 { 20, 1000 },
103 { 40, 1000 },
104 { 100, 1000 },
105 { 200, 1000 },
106 { 400, 1000 },
62bb8840 107 { 0, 0},
a370ef19
BV
108};
109
62bb8840 110static const struct sr_rational vdivs[] = {
313deed2
BV
111 /* millivolts */
112 { 10, 1000 },
113 { 20, 1000 },
114 { 50, 1000 },
115 { 100, 1000 },
116 { 200, 1000 },
117 { 500, 1000 },
118 /* volts */
119 { 1, 1 },
120 { 2, 1 },
121 { 5, 1 },
62bb8840 122 { 0, 0 },
313deed2
BV
123};
124
62bb8840 125static const char *trigger_sources[] = {
a370ef19
BV
126 "CH1",
127 "CH2",
128 "EXT",
88a13f30 129 /* TODO: forced */
62bb8840 130 NULL,
a370ef19 131};
3b533202 132
62bb8840 133static const char *filter_targets[] = {
ebb781a6
BV
134 "CH1",
135 "CH2",
136 /* TODO: "TRIGGER", */
62bb8840 137 NULL,
ebb781a6
BV
138};
139
62bb8840 140static const char *coupling[] = {
b58fbd99
BV
141 "AC",
142 "DC",
143 "GND",
62bb8840 144 NULL,
b58fbd99
BV
145};
146
3b533202
BV
147SR_PRIV libusb_context *usb_context = NULL;
148SR_PRIV GSList *dev_insts = NULL;
149
62bb8840 150static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof)
3b533202
BV
151{
152 struct sr_dev_inst *sdi;
153 struct context *ctx;
154
155 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
88a13f30 156 prof->vendor, prof->model, NULL);
3b533202
BV
157 if (!sdi)
158 return NULL;
159
160 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
161 sr_err("hantek-dso: ctx malloc failed");
162 return NULL;
163 }
164 ctx->profile = prof;
165 ctx->dev_state = IDLE;
166 ctx->timebase = DEFAULT_TIMEBASE;
167 ctx->ch1_enabled = TRUE;
168 ctx->ch2_enabled = TRUE;
169 ctx->voltage_ch1 = DEFAULT_VOLTAGE;
170 ctx->voltage_ch2 = DEFAULT_VOLTAGE;
171 ctx->coupling_ch1 = DEFAULT_COUPLING;
172 ctx->coupling_ch2 = DEFAULT_COUPLING;
173 ctx->voffset_ch1 = DEFAULT_VERT_OFFSET;
174 ctx->voffset_ch2 = DEFAULT_VERT_OFFSET;
175 ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
3b533202
BV
176 ctx->framesize = DEFAULT_FRAMESIZE;
177 ctx->triggerslope = SLOPE_POSITIVE;
a370ef19 178 ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
3b533202
BV
179 ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
180 sdi->priv = ctx;
181 dev_insts = g_slist_append(dev_insts, sdi);
182
183 return sdi;
184}
185
62bb8840 186static int configure_probes(struct context *ctx, const GSList *probes)
3b533202 187{
62bb8840
UH
188 const struct sr_probe *probe;
189 const GSList *l;
3b533202 190
6e71ef3b 191 ctx->ch1_enabled = ctx->ch2_enabled = FALSE;
3b533202
BV
192 for (l = probes; l; l = l->next) {
193 probe = (struct sr_probe *)l->data;
6e71ef3b 194 if (probe->index == 1)
3b533202 195 ctx->ch1_enabled = probe->enabled;
6e71ef3b 196 else if (probe->index == 2)
3b533202
BV
197 ctx->ch2_enabled = probe->enabled;
198 }
199
200 return SR_OK;
201}
202
40dda2c3 203static int hw_init(void)
3b533202
BV
204{
205 struct sr_dev_inst *sdi;
206 struct libusb_device_descriptor des;
62bb8840 207 const struct dso_profile *prof;
3b533202
BV
208 struct context *ctx;
209 libusb_device **devlist;
210 int err, devcnt, i, j;
211
3b533202
BV
212 if (libusb_init(&usb_context) != 0) {
213 sr_err("hantek-dso: Failed to initialize USB.");
214 return 0;
215 }
216
217 /* Find all Hantek DSO devices and upload firmware to all of them. */
218 devcnt = 0;
219 libusb_get_device_list(usb_context, &devlist);
220 for (i = 0; devlist[i]; i++) {
221 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
222 sr_err("hantek-dso: failed to get device descriptor: %d", err);
223 continue;
224 }
225
226 prof = NULL;
227 for (j = 0; dev_profiles[j].orig_vid; j++) {
228 if (des.idVendor == dev_profiles[j].orig_vid
229 && des.idProduct == dev_profiles[j].orig_pid) {
230 /* Device matches the pre-firmware profile. */
231 prof = &dev_profiles[j];
232 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
233 sdi = dso_dev_new(devcnt, prof);
234 ctx = sdi->priv;
235 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
236 prof->firmware) == SR_OK)
237 /* Remember when the firmware on this device was updated */
fc8fe3e3 238 ctx->fw_updated = g_get_monotonic_time();
3b533202
BV
239 else
240 sr_err("hantek-dso: firmware upload failed for "
241 "device %d", devcnt);
242 /* Dummy USB address of 0xff will get overwritten later. */
243 ctx->usb = sr_usb_dev_inst_new(
244 libusb_get_bus_number(devlist[i]), 0xff, NULL);
245 devcnt++;
246 break;
247 } else if (des.idVendor == dev_profiles[j].fw_vid
248 && des.idProduct == dev_profiles[j].fw_pid) {
249 /* Device matches the post-firmware profile. */
250 prof = &dev_profiles[j];
251 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
252 sdi = dso_dev_new(devcnt, prof);
253 sdi->status = SR_ST_INACTIVE;
254 ctx = sdi->priv;
255 ctx->usb = sr_usb_dev_inst_new(
256 libusb_get_bus_number(devlist[i]),
257 libusb_get_device_address(devlist[i]), NULL);
258 devcnt++;
259 break;
260 }
261 }
262 if (!prof)
263 /* not a supported VID/PID */
264 continue;
265 }
266 libusb_free_device_list(devlist, 1);
267
268 return devcnt;
269}
270
271static int hw_dev_open(int dev_index)
272{
3b533202
BV
273 struct sr_dev_inst *sdi;
274 struct context *ctx;
fc8fe3e3
BV
275 int64_t timediff_us, timediff_ms;
276 int err;
3b533202
BV
277
278 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
279 return SR_ERR_ARG;
280 ctx = sdi->priv;
281
282 /*
fc8fe3e3 283 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
3b533202
BV
284 * for the FX2 to renumerate
285 */
fc8fe3e3
BV
286 err = SR_ERR;
287 if (ctx->fw_updated > 0) {
3b533202
BV
288 sr_info("hantek-dso: waiting for device to reset");
289 /* takes at least 300ms for the FX2 to be gone from the USB bus */
290 g_usleep(300 * 1000);
fc8fe3e3
BV
291 timediff_ms = 0;
292 while (timediff_ms < MAX_RENUM_DELAY_MS) {
3b533202
BV
293 if ((err = dso_open(dev_index)) == SR_OK)
294 break;
295 g_usleep(100 * 1000);
fc8fe3e3
BV
296 timediff_us = g_get_monotonic_time() - ctx->fw_updated;
297 timediff_ms = timediff_us / 1000;
99f5d45e 298 sr_spew("hantek-dso: waited %" PRIi64 " ms", timediff_ms);
3b533202 299 }
fc8fe3e3 300 sr_info("hantek-dso: device came back after %d ms", timediff_ms);
3b533202
BV
301 } else {
302 err = dso_open(dev_index);
303 }
304
305 if (err != SR_OK) {
306 sr_err("hantek-dso: unable to open device");
307 return SR_ERR;
308 }
309
310 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
311 if (err != 0) {
312 sr_err("hantek-dso: Unable to claim interface: %d", err);
313 return SR_ERR;
314 }
315
316 return SR_OK;
317}
318
319static int hw_dev_close(int dev_index)
320{
321 struct sr_dev_inst *sdi;
322
323 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
324 return SR_ERR_ARG;
325
326 dso_close(sdi);
327
328 return SR_OK;
329}
330
331static int hw_cleanup(void)
332{
333 GSList *l;
334 struct sr_dev_inst *sdi;
335 struct context *ctx;
336
337 /* Properly close and free all devices. */
338 for (l = dev_insts; l; l = l->next) {
339 if (!(sdi = l->data)) {
340 /* Log error, but continue cleaning up the rest. */
341 sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
342 continue;
343 }
344 if (!(ctx = sdi->priv)) {
345 /* Log error, but continue cleaning up the rest. */
346 sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
347 continue;
348 }
349 dso_close(sdi);
350 sr_usb_dev_inst_free(ctx->usb);
a370ef19
BV
351 g_free(ctx->triggersource);
352
3b533202
BV
353 sr_dev_inst_free(sdi);
354 }
355
356 g_slist_free(dev_insts);
357 dev_insts = NULL;
358
359 if (usb_context)
360 libusb_exit(usb_context);
361 usb_context = NULL;
362
363 return SR_OK;
364}
365
62bb8840 366static const void *hw_dev_info_get(int dev_index, int dev_info_id)
3b533202
BV
367{
368 struct sr_dev_inst *sdi;
62bb8840 369 const void *info;
3b533202
BV
370 uint64_t tmp;
371
372 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
373 return NULL;
3b533202
BV
374
375 info = NULL;
376 switch (dev_info_id) {
377 case SR_DI_INST:
378 info = sdi;
379 break;
380 case SR_DI_NUM_PROBES:
88a13f30 381 info = GINT_TO_POINTER(NUM_PROBES);
3b533202
BV
382 break;
383 case SR_DI_PROBE_NAMES:
384 info = probe_names;
385 break;
a370ef19
BV
386 case SR_DI_BUFFERSIZES:
387 info = buffersizes;
388 break;
389 case SR_DI_TIMEBASES:
390 info = timebases;
391 break;
392 case SR_DI_TRIGGER_SOURCES:
393 info = trigger_sources;
394 break;
ebb781a6
BV
395 case SR_DI_FILTERS:
396 info = filter_targets;
397 break;
313deed2
BV
398 case SR_DI_VDIVS:
399 info = vdivs;
400 break;
4a090d72
BV
401 case SR_DI_COUPLING:
402 info = coupling;
403 break;
3b533202
BV
404 /* TODO remove this */
405 case SR_DI_CUR_SAMPLERATE:
406 info = &tmp;
407 break;
408 }
409
410 return info;
411}
412
62bb8840 413static int hw_dev_status_get(int dev_index)
3b533202
BV
414{
415 struct sr_dev_inst *sdi;
416
62bb8840 417 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
3b533202
BV
418 return SR_ST_NOT_FOUND;
419
420 return sdi->status;
421}
422
62bb8840 423static const int *hw_hwcap_get_all(void)
3b533202 424{
62bb8840 425 return hwcaps;
3b533202
BV
426}
427
af36b809 428static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
3b533202
BV
429{
430 struct sr_dev_inst *sdi;
431 struct context *ctx;
a370ef19
BV
432 struct sr_rational tmp_rat;
433 float tmp_float;
434 uint64_t tmp_u64;
435 int ret, i;
4a090d72 436 char **targets;
3b533202
BV
437
438 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
439 return SR_ERR;
440
441 if (sdi->status != SR_ST_ACTIVE)
442 return SR_ERR;
443
a370ef19 444 ret = SR_OK;
3b533202
BV
445 ctx = sdi->priv;
446 switch (hwcap) {
ae88b97b 447 case SR_HWCAP_LIMIT_FRAMES:
62bb8840 448 ctx->limit_frames = *(const uint64_t *)value;
ae88b97b 449 break;
3b533202 450 case SR_HWCAP_PROBECONFIG:
62bb8840 451 ret = configure_probes(ctx, (const GSList *)value);
3b533202 452 break;
a370ef19 453 case SR_HWCAP_TRIGGER_SLOPE:
62bb8840 454 tmp_u64 = *(const int *)value;
a370ef19
BV
455 if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE)
456 ret = SR_ERR_ARG;
457 ctx->triggerslope = tmp_u64;
458 break;
459 case SR_HWCAP_HORIZ_TRIGGERPOS:
62bb8840 460 tmp_float = *(const float *)value;
a370ef19
BV
461 if (tmp_float < 0.0 || tmp_float > 1.0) {
462 sr_err("hantek-dso: trigger position should be between 0.0 and 1.0");
3b533202 463 ret = SR_ERR_ARG;
a370ef19
BV
464 } else
465 ctx->triggerposition = tmp_float;
466 break;
467 case SR_HWCAP_BUFFERSIZE:
62bb8840 468 tmp_u64 = *(const int *)value;
a370ef19
BV
469 for (i = 0; buffersizes[i]; i++) {
470 if (buffersizes[i] == tmp_u64) {
471 ctx->framesize = tmp_u64;
472 break;
473 }
474 }
475 if (buffersizes[i] == 0)
476 ret = SR_ERR_ARG;
477 break;
478 case SR_HWCAP_TIMEBASE:
62bb8840 479 tmp_rat = *(const struct sr_rational *)value;
a370ef19
BV
480 for (i = 0; timebases[i].p && timebases[i].q; i++) {
481 if (timebases[i].p == tmp_rat.p
482 && timebases[i].q == tmp_rat.q) {
483 ctx->timebase = i;
484 break;
485 }
486 }
487 if (timebases[i].p == 0 && timebases[i].q == 0)
488 ret = SR_ERR_ARG;
489 break;
490 case SR_HWCAP_TRIGGER_SOURCE:
a370ef19 491 for (i = 0; trigger_sources[i]; i++) {
4a090d72
BV
492 if (!strcmp(value, trigger_sources[i])) {
493 ctx->triggersource = g_strdup(value);
a370ef19
BV
494 break;
495 }
496 }
497 if (trigger_sources[i] == 0)
498 ret = SR_ERR_ARG;
499 break;
ebb781a6
BV
500 case SR_HWCAP_FILTER:
501 ctx->filter_ch1 = ctx->filter_ch2 = ctx->filter_trigger = 0;
502 targets = g_strsplit(value, ",", 0);
503 for (i = 0; targets[i]; i++) {
504 if (targets[i] == '\0')
505 /* Empty filter string can be used to clear them all. */
506 ;
507 else if (!strcmp(targets[i], "CH1"))
508 ctx->filter_ch1 = TRUE;
509 else if (!strcmp(targets[i], "CH2"))
510 ctx->filter_ch2 = TRUE;
511 else if (!strcmp(targets[i], "TRIGGER"))
512 ctx->filter_trigger = TRUE;
513 else {
514 sr_err("invalid filter target %s", targets[i]);
515 ret = SR_ERR_ARG;
516 }
517 }
518 g_strfreev(targets);
519 break;
313deed2
BV
520 case SR_HWCAP_VDIV:
521 /* TODO not supporting vdiv per channel yet */
62bb8840 522 tmp_rat = *(const struct sr_rational *)value;
313deed2
BV
523 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
524 if (vdivs[i].p == tmp_rat.p
525 && vdivs[i].q == tmp_rat.q) {
526 ctx->voltage_ch1 = i;
527 ctx->voltage_ch2 = i;
528 break;
529 }
530 }
531 if (vdivs[i].p == 0 && vdivs[i].q == 0)
532 ret = SR_ERR_ARG;
533 break;
b58fbd99
BV
534 case SR_HWCAP_COUPLING:
535 /* TODO not supporting coupling per channel yet */
b58fbd99 536 for (i = 0; coupling[i]; i++) {
4a090d72 537 if (!strcmp(value, coupling[i])) {
b58fbd99
BV
538 ctx->coupling_ch1 = i;
539 ctx->coupling_ch2 = i;
540 break;
541 }
542 }
543 if (coupling[i] == 0)
544 ret = SR_ERR_ARG;
545 break;
3b533202
BV
546 default:
547 ret = SR_ERR_ARG;
548 }
549
550 return ret;
551}
552
e749a8cb
BV
553static void send_chunk(struct context *ctx, unsigned char *buf,
554 int num_samples)
3b533202
BV
555{
556 struct sr_datafeed_packet packet;
557 struct sr_datafeed_analog analog;
c5841b28 558 float ch1, ch2, range;
6e71ef3b 559 int num_probes, data_offset, i;
3b533202 560
6e71ef3b 561 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
3b533202
BV
562 packet.type = SR_DF_ANALOG;
563 packet.payload = &analog;
6e71ef3b 564 /* TODO: support for 5xxx series 9-bit samples */
e749a8cb 565 analog.num_samples = num_samples;
9956f285
UH
566 analog.mq = SR_MQ_VOLTAGE;
567 analog.unit = SR_UNIT_VOLT;
6e71ef3b
BV
568 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
569 data_offset = 0;
3b533202 570 for (i = 0; i < analog.num_samples; i++) {
6e71ef3b
BV
571 /* The device always sends data for both channels. If a channel
572 * is disabled, it contains a copy of the enabled channel's
573 * data. However, we only send the requested channels to the bus.
c5841b28
BV
574 *
575 * Voltage values are encoded as a value 0-255 (0-512 on the 5200*),
576 * where the value is a point in the range represented by the vdiv
577 * setting. There are 8 vertical divs, so e.g. 500mV/div represents
578 * 4V peak-to-peak where 0 = -2V and 255 = +2V.
6e71ef3b 579 */
3b533202 580 /* TODO: support for 5xxx series 9-bit samples */
6e71ef3b 581 if (ctx->ch1_enabled) {
c5841b28 582 range = ((float)vdivs[ctx->voltage_ch1].p / vdivs[ctx->voltage_ch1].q) * 8;
e749a8cb 583 ch1 = range / 255 * *(buf + i * 2 + 1);
c5841b28
BV
584 /* Value is centered around 0V. */
585 ch1 -= range / 2;
6e71ef3b
BV
586 analog.data[data_offset++] = ch1;
587 }
588 if (ctx->ch2_enabled) {
c5841b28 589 range = ((float)vdivs[ctx->voltage_ch2].p / vdivs[ctx->voltage_ch2].q) * 8;
e749a8cb 590 ch2 = range / 255 * *(buf + i * 2);
c5841b28 591 ch2 -= range / 2;
6e71ef3b
BV
592 analog.data[data_offset++] = ch2;
593 }
3b533202 594 }
e749a8cb
BV
595 sr_session_send(ctx->cb_data, &packet);
596
597}
598
599/* Called by libusb (as triggered by handle_event()) when a transfer comes in.
600 * Only channel data comes in asynchronously, and all transfers for this are
601 * queued up beforehand, so this just needs so chuck the incoming data onto
602 * the libsigrok session bus.
603 */
604static void receive_transfer(struct libusb_transfer *transfer)
605{
606 struct sr_datafeed_packet packet;
607 struct context *ctx;
608 int num_samples, pre;
609
610 ctx = transfer->user_data;
611 sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes",
612 transfer->status, transfer->actual_length);
613
614 if (transfer->actual_length == 0)
615 /* Nothing to send to the bus. */
616 return;
617
618 num_samples = transfer->actual_length / 2;
619
620 sr_dbg("hantek-dso: got %d-%d/%d samples in frame", ctx->samp_received + 1,
621 ctx->samp_received + num_samples, ctx->framesize);
622
623 /* The device always sends a full frame, but the beginning of the frame
624 * doesn't represent the trigger point. The offset at which the trigger
625 * happened came in with the capture state, so we need to start sending
626 * from there up the session bus. The samples in the frame buffer before
627 * that trigger point came after the end of the device's frame buffer was
628 * reached, and it wrapped around to overwrite up until the trigger point.
629 */
630 if (ctx->samp_received < ctx->trigger_offset) {
631 /* Trigger point not yet reached. */
632 if (ctx->samp_received + num_samples < ctx->trigger_offset) {
633 /* The entire chunk is before the trigger point. */
634 memcpy(ctx->framebuf + ctx->samp_buffered * 2,
635 transfer->buffer, num_samples * 2);
636 ctx->samp_buffered += num_samples;
637 } else {
638 /* This chunk hits or overruns the trigger point.
639 * Store the part before the trigger fired, and
640 * send the rest up to the session bus. */
641 pre = ctx->trigger_offset - ctx->samp_received;
642 memcpy(ctx->framebuf + ctx->samp_buffered * 2,
643 transfer->buffer, pre * 2);
644 ctx->samp_buffered += pre;
645
646 /* The rest of this chunk starts with the trigger point. */
647 sr_dbg("hantek-dso: reached trigger point, %d samples buffered",
648 ctx->samp_buffered);
649
650 /* Avoid the corner case where the chunk ended at
651 * exactly the trigger point. */
652 if (num_samples > pre)
653 send_chunk(ctx, transfer->buffer + pre * 2,
654 num_samples - pre);
655 }
656 } else {
657 /* Already past the trigger point, just send it all out. */
658 send_chunk(ctx, transfer->buffer,
659 num_samples);
660 }
661
662 ctx->samp_received += num_samples;
663
664 /* Everything in this transfer was either copied to the buffer or
665 * sent to the session bus. */
3b533202
BV
666 g_free(transfer->buffer);
667 libusb_free_transfer(transfer);
3b533202 668
e749a8cb
BV
669 if (ctx->samp_received >= ctx->framesize) {
670 /* That was the last chunk in this frame. Send the buffered
671 * pre-trigger samples out now, in one big chunk. */
672 sr_dbg("hantek-dso: end of frame, sending %d pre-trigger buffered samples",
673 ctx->samp_buffered);
674 send_chunk(ctx, ctx->framebuf, ctx->samp_buffered);
675
676 /* Mark the end of this frame. */
ae88b97b
BV
677 packet.type = SR_DF_FRAME_END;
678 sr_session_send(ctx->cb_data, &packet);
679
680 if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) {
681 /* Terminate session */
6e71ef3b 682 /* TODO: don't leave pending USB transfers hanging */
ae88b97b
BV
683 packet.type = SR_DF_END;
684 sr_session_send(ctx->cb_data, &packet);
685 } else {
ae88b97b
BV
686 ctx->dev_state = NEW_CAPTURE;
687 }
688 }
689
3b533202
BV
690}
691
692static int handle_event(int fd, int revents, void *cb_data)
693{
ae88b97b 694 struct sr_datafeed_packet packet;
3b533202
BV
695 struct timeval tv;
696 struct context *ctx;
e749a8cb 697 int num_probes;
6e6eeff4
BV
698 uint32_t trigger_offset;
699 uint8_t capturestate;
3b533202
BV
700
701 /* Avoid compiler warnings. */
702 (void)fd;
703 (void)revents;
704
705 /* Always handle pending libusb events. */
706 tv.tv_sec = tv.tv_usec = 0;
707 libusb_handle_events_timeout(usb_context, &tv);
708
709 ctx = cb_data;
710 /* TODO: ugh */
711 if (ctx->dev_state == NEW_CAPTURE) {
712 if (dso_capture_start(ctx) != SR_OK)
713 return TRUE;
714 if (dso_enable_trigger(ctx) != SR_OK)
715 return TRUE;
a370ef19
BV
716// if (dso_force_trigger(ctx) != SR_OK)
717// return TRUE;
3b533202
BV
718 sr_dbg("hantek-dso: successfully requested next chunk");
719 ctx->dev_state = CAPTURE;
720 return TRUE;
721 }
722 if (ctx->dev_state != CAPTURE)
723 return TRUE;
724
6e6eeff4 725 if ((dso_get_capturestate(ctx, &capturestate, &trigger_offset)) != SR_OK)
3b533202 726 return TRUE;
3b533202
BV
727
728 sr_dbg("hantek-dso: capturestate %d", capturestate);
6e6eeff4 729 sr_dbg("hantek-dso: trigger offset 0x%.6x", trigger_offset);
3b533202
BV
730 switch (capturestate) {
731 case CAPTURE_EMPTY:
732 if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) {
733 ctx->capture_empty_count = 0;
734 if (dso_capture_start(ctx) != SR_OK)
735 break;
736 if (dso_enable_trigger(ctx) != SR_OK)
737 break;
a370ef19
BV
738// if (dso_force_trigger(ctx) != SR_OK)
739// break;
3b533202
BV
740 sr_dbg("hantek-dso: successfully requested next chunk");
741 }
742 break;
743 case CAPTURE_FILLING:
744 /* no data yet */
745 break;
746 case CAPTURE_READY_8BIT:
e749a8cb
BV
747 /* Remember where in the captured frame the trigger is. */
748 ctx->trigger_offset = trigger_offset;
749
750 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
751 ctx->framebuf = g_try_malloc(ctx->framesize * num_probes * 2);
752 ctx->samp_buffered = ctx->samp_received = 0;
753
3b533202
BV
754 /* Tell the scope to send us the first frame. */
755 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
756 break;
ae88b97b
BV
757
758 /* Don't hit the state machine again until we're done fetching
759 * the data we just told the scope to send.
760 */
3b533202 761 ctx->dev_state = FETCH_DATA;
ae88b97b
BV
762
763 /* Tell the frontend a new frame is on the way. */
764 packet.type = SR_DF_FRAME_BEGIN;
765 sr_session_send(cb_data, &packet);
3b533202
BV
766 break;
767 case CAPTURE_READY_9BIT:
768 /* TODO */
769 sr_err("not yet supported");
770 break;
771 case CAPTURE_TIMEOUT:
772 /* Doesn't matter, we'll try again next time. */
773 break;
774 default:
775 sr_dbg("unknown capture state");
776 }
777
778 return TRUE;
779}
780
62bb8840 781static int hw_dev_acquisition_start(int dev_index, void *cb_data)
3b533202
BV
782{
783 const struct libusb_pollfd **lupfd;
784 struct sr_datafeed_packet packet;
785 struct sr_datafeed_header header;
786 struct sr_datafeed_meta_analog meta;
787 struct sr_dev_inst *sdi;
788 struct context *ctx;
789 int i;
790
62bb8840 791 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
3b533202
BV
792 return SR_ERR;
793
794 if (sdi->status != SR_ST_ACTIVE)
795 return SR_ERR;
796
797 ctx = sdi->priv;
798 ctx->cb_data = cb_data;
799
800 if (dso_init(ctx) != SR_OK)
801 return SR_ERR;
802
803 if (dso_capture_start(ctx) != SR_OK)
804 return SR_ERR;
805
806 ctx->dev_state = CAPTURE;
807 lupfd = libusb_get_pollfds(usb_context);
808 for (i = 0; lupfd[i]; i++)
809 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event,
810 ctx);
811 free(lupfd);
812
813 /* Send header packet to the session bus. */
814 packet.type = SR_DF_HEADER;
815 packet.payload = (unsigned char *)&header;
816 header.feed_version = 1;
817 gettimeofday(&header.starttime, NULL);
818 sr_session_send(cb_data, &packet);
819
820 /* Send metadata about the SR_DF_ANALOG packets to come. */
821 packet.type = SR_DF_META_ANALOG;
822 packet.payload = &meta;
88a13f30 823 meta.num_probes = NUM_PROBES;
3b533202
BV
824 sr_session_send(cb_data, &packet);
825
826 return SR_OK;
827}
828
829/* TODO: doesn't really cancel pending transfers so they might come in after
830 * SR_DF_END is sent.
831 */
62bb8840 832static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
3b533202
BV
833{
834 struct sr_datafeed_packet packet;
835 struct sr_dev_inst *sdi;
836 struct context *ctx;
837
62bb8840 838 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
3b533202
BV
839 return SR_ERR;
840
841 if (sdi->status != SR_ST_ACTIVE)
842 return SR_ERR;
843
844 ctx = sdi->priv;
845 ctx->dev_state = IDLE;
846
847 packet.type = SR_DF_END;
62bb8840 848 sr_session_send(cb_data, &packet);
3b533202
BV
849
850 return SR_OK;
851}
852
62bb8840 853SR_PRIV struct sr_dev_driver hantek_dso_driver_info = {
3b533202
BV
854 .name = "hantek-dso",
855 .longname = "Hantek DSO",
856 .api_version = 1,
857 .init = hw_init,
858 .cleanup = hw_cleanup,
859 .dev_open = hw_dev_open,
860 .dev_close = hw_dev_close,
62bb8840
UH
861 .dev_info_get = hw_dev_info_get,
862 .dev_status_get = hw_dev_status_get,
863 .hwcap_get_all = hw_hwcap_get_all,
3b533202 864 .dev_config_set = hw_dev_config_set,
62bb8840
UH
865 .dev_acquisition_start = hw_dev_acquisition_start,
866 .dev_acquisition_stop = hw_dev_acquisition_stop,
3b533202 867};