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