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