]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/hantek-dso/api.c
sr: remove unused argument from hardware driver function init()
[libsigrok.git] / hardware / hantek-dso / api.c
... / ...
CommitLineData
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 <glib.h>
31#include <libusb.h>
32#include "libsigrok.h"
33#include "libsigrok-internal.h"
34#include "config.h"
35#include "dso.h"
36
37
38/* Max time in ms before we want to check on USB events */
39/* TODO tune this properly */
40#define TICK 1
41
42static const int hwcaps[] = {
43 SR_HWCAP_OSCILLOSCOPE,
44 SR_HWCAP_LIMIT_SAMPLES,
45 SR_HWCAP_CONTINUOUS,
46 SR_HWCAP_TIMEBASE,
47 SR_HWCAP_BUFFERSIZE,
48 SR_HWCAP_TRIGGER_SOURCE,
49 SR_HWCAP_TRIGGER_SLOPE,
50 SR_HWCAP_HORIZ_TRIGGERPOS,
51 SR_HWCAP_FILTER,
52 SR_HWCAP_VDIV,
53 SR_HWCAP_COUPLING,
54 0,
55};
56
57static const char *probe_names[] = {
58 "CH1",
59 "CH2",
60 NULL,
61};
62
63static const struct dso_profile dev_profiles[] = {
64 { 0x04b4, 0x2090, 0x04b5, 0x2090,
65 "Hantek", "DSO-2090",
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 },
80};
81
82static const uint64_t buffersizes[] = {
83 10240,
84 32768,
85 /* TODO: 65535 */
86 0,
87};
88
89static const struct sr_rational timebases[] = {
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 },
107 { 0, 0},
108};
109
110static const struct sr_rational vdivs[] = {
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 },
122 { 0, 0 },
123};
124
125static const char *trigger_sources[] = {
126 "CH1",
127 "CH2",
128 "EXT",
129 /* TODO: forced */
130 NULL,
131};
132
133static const char *filter_targets[] = {
134 "CH1",
135 "CH2",
136 /* TODO: "TRIGGER", */
137 NULL,
138};
139
140static const char *coupling[] = {
141 "AC",
142 "DC",
143 "GND",
144 NULL,
145};
146
147SR_PRIV libusb_context *usb_context = NULL;
148SR_PRIV GSList *dev_insts = NULL;
149
150static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof)
151{
152 struct sr_dev_inst *sdi;
153 struct context *ctx;
154
155 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
156 prof->vendor, prof->model, NULL);
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;
176 ctx->framesize = DEFAULT_FRAMESIZE;
177 ctx->triggerslope = SLOPE_POSITIVE;
178 ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
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
186static int configure_probes(struct context *ctx, const GSList *probes)
187{
188 const struct sr_probe *probe;
189 const GSList *l;
190
191 ctx->ch1_enabled = ctx->ch2_enabled = FALSE;
192 for (l = probes; l; l = l->next) {
193 probe = (struct sr_probe *)l->data;
194 if (probe->index == 1)
195 ctx->ch1_enabled = probe->enabled;
196 else if (probe->index == 2)
197 ctx->ch2_enabled = probe->enabled;
198 }
199
200 return SR_OK;
201}
202
203static int hw_init(void)
204{
205 struct sr_dev_inst *sdi;
206 struct libusb_device_descriptor des;
207 const struct dso_profile *prof;
208 struct context *ctx;
209 libusb_device **devlist;
210 int err, devcnt, i, j;
211
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 */
238 ctx->fw_updated = g_get_monotonic_time();
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{
273 struct sr_dev_inst *sdi;
274 struct context *ctx;
275 int64_t timediff_us, timediff_ms;
276 int err;
277
278 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
279 return SR_ERR_ARG;
280 ctx = sdi->priv;
281
282 /*
283 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
284 * for the FX2 to renumerate
285 */
286 err = SR_ERR;
287 if (ctx->fw_updated > 0) {
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);
291 timediff_ms = 0;
292 while (timediff_ms < MAX_RENUM_DELAY_MS) {
293 if ((err = dso_open(dev_index)) == SR_OK)
294 break;
295 g_usleep(100 * 1000);
296 timediff_us = g_get_monotonic_time() - ctx->fw_updated;
297 timediff_ms = timediff_us / 1000;
298 sr_spew("hantek-dso: waited %" PRIi64 " ms", timediff_ms);
299 }
300 sr_info("hantek-dso: device came back after %d ms", timediff_ms);
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);
351 g_free(ctx->triggersource);
352
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
366static const void *hw_dev_info_get(int dev_index, int dev_info_id)
367{
368 struct sr_dev_inst *sdi;
369 const void *info;
370 uint64_t tmp;
371
372 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
373 return NULL;
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:
381 info = GINT_TO_POINTER(NUM_PROBES);
382 break;
383 case SR_DI_PROBE_NAMES:
384 info = probe_names;
385 break;
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;
395 case SR_DI_FILTERS:
396 info = filter_targets;
397 break;
398 case SR_DI_VDIVS:
399 info = vdivs;
400 break;
401 case SR_DI_COUPLING:
402 info = coupling;
403 break;
404 /* TODO remove this */
405 case SR_DI_CUR_SAMPLERATE:
406 info = &tmp;
407 break;
408 }
409
410 return info;
411}
412
413static int hw_dev_status_get(int dev_index)
414{
415 struct sr_dev_inst *sdi;
416
417 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
418 return SR_ST_NOT_FOUND;
419
420 return sdi->status;
421}
422
423static const int *hw_hwcap_get_all(void)
424{
425 return hwcaps;
426}
427
428static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
429{
430 struct sr_dev_inst *sdi;
431 struct context *ctx;
432 struct sr_rational tmp_rat;
433 float tmp_float;
434 uint64_t tmp_u64;
435 int ret, i;
436 char **targets;
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
444 ret = SR_OK;
445 ctx = sdi->priv;
446 switch (hwcap) {
447 case SR_HWCAP_LIMIT_FRAMES:
448 ctx->limit_frames = *(const uint64_t *)value;
449 break;
450 case SR_HWCAP_PROBECONFIG:
451 ret = configure_probes(ctx, (const GSList *)value);
452 break;
453 case SR_HWCAP_TRIGGER_SLOPE:
454 tmp_u64 = *(const int *)value;
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:
460 tmp_float = *(const float *)value;
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");
463 ret = SR_ERR_ARG;
464 } else
465 ctx->triggerposition = tmp_float;
466 break;
467 case SR_HWCAP_BUFFERSIZE:
468 tmp_u64 = *(const int *)value;
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:
479 tmp_rat = *(const struct sr_rational *)value;
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:
491 for (i = 0; trigger_sources[i]; i++) {
492 if (!strcmp(value, trigger_sources[i])) {
493 ctx->triggersource = g_strdup(value);
494 break;
495 }
496 }
497 if (trigger_sources[i] == 0)
498 ret = SR_ERR_ARG;
499 break;
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;
520 case SR_HWCAP_VDIV:
521 /* TODO not supporting vdiv per channel yet */
522 tmp_rat = *(const struct sr_rational *)value;
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;
534 case SR_HWCAP_COUPLING:
535 /* TODO not supporting coupling per channel yet */
536 for (i = 0; coupling[i]; i++) {
537 if (!strcmp(value, coupling[i])) {
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;
546 default:
547 ret = SR_ERR_ARG;
548 }
549
550 return ret;
551}
552
553static void send_chunk(struct context *ctx, unsigned char *buf,
554 int num_samples)
555{
556 struct sr_datafeed_packet packet;
557 struct sr_datafeed_analog analog;
558 float ch1, ch2, range;
559 int num_probes, data_offset, i;
560
561 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
562 packet.type = SR_DF_ANALOG;
563 packet.payload = &analog;
564 /* TODO: support for 5xxx series 9-bit samples */
565 analog.num_samples = num_samples;
566 analog.mq = SR_MQ_VOLTAGE;
567 analog.unit = SR_UNIT_VOLT;
568 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
569 data_offset = 0;
570 for (i = 0; i < analog.num_samples; i++) {
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.
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.
579 */
580 /* TODO: support for 5xxx series 9-bit samples */
581 if (ctx->ch1_enabled) {
582 range = ((float)vdivs[ctx->voltage_ch1].p / vdivs[ctx->voltage_ch1].q) * 8;
583 ch1 = range / 255 * *(buf + i * 2 + 1);
584 /* Value is centered around 0V. */
585 ch1 -= range / 2;
586 analog.data[data_offset++] = ch1;
587 }
588 if (ctx->ch2_enabled) {
589 range = ((float)vdivs[ctx->voltage_ch2].p / vdivs[ctx->voltage_ch2].q) * 8;
590 ch2 = range / 255 * *(buf + i * 2);
591 ch2 -= range / 2;
592 analog.data[data_offset++] = ch2;
593 }
594 }
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. */
666 g_free(transfer->buffer);
667 libusb_free_transfer(transfer);
668
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. */
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 */
682 /* TODO: don't leave pending USB transfers hanging */
683 packet.type = SR_DF_END;
684 sr_session_send(ctx->cb_data, &packet);
685 } else {
686 ctx->dev_state = NEW_CAPTURE;
687 }
688 }
689
690}
691
692static int handle_event(int fd, int revents, void *cb_data)
693{
694 struct sr_datafeed_packet packet;
695 struct timeval tv;
696 struct context *ctx;
697 int num_probes;
698 uint32_t trigger_offset;
699 uint8_t capturestate;
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;
716// if (dso_force_trigger(ctx) != SR_OK)
717// return TRUE;
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
725 if ((dso_get_capturestate(ctx, &capturestate, &trigger_offset)) != SR_OK)
726 return TRUE;
727
728 sr_dbg("hantek-dso: capturestate %d", capturestate);
729 sr_dbg("hantek-dso: trigger offset 0x%.6x", trigger_offset);
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;
738// if (dso_force_trigger(ctx) != SR_OK)
739// break;
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:
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
754 /* Tell the scope to send us the first frame. */
755 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
756 break;
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 */
761 ctx->dev_state = FETCH_DATA;
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);
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
781static int hw_dev_acquisition_start(int dev_index, void *cb_data)
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
791 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
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;
823 meta.num_probes = NUM_PROBES;
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 */
832static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
833{
834 struct sr_datafeed_packet packet;
835 struct sr_dev_inst *sdi;
836 struct context *ctx;
837
838 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
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;
848 sr_session_send(cb_data, &packet);
849
850 return SR_OK;
851}
852
853SR_PRIV struct sr_dev_driver hantek_dso_driver_info = {
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,
861 .dev_info_get = hw_dev_info_get,
862 .dev_status_get = hw_dev_status_get,
863 .hwcap_get_all = hw_hwcap_get_all,
864 .dev_config_set = hw_dev_config_set,
865 .dev_acquisition_start = hw_dev_acquisition_start,
866 .dev_acquisition_stop = hw_dev_acquisition_stop,
867};