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