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