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