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