]> sigrok.org Git - libsigrok.git/blame - hardware/hantek-dso/api.c
ols: Get rid of gcc warnings
[libsigrok.git] / hardware / hantek-dso / api.c
CommitLineData
3b533202
BV
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>
3b533202
BV
30#include <glib.h>
31#include <libusb.h>
45c59c8b
BV
32#include "libsigrok.h"
33#include "libsigrok-internal.h"
3b533202
BV
34#include "dso.h"
35
fc8fe3e3
BV
36/* Max time in ms before we want to check on USB events */
37/* TODO tune this properly */
e98b7f1b 38#define TICK 1
3b533202 39
f627afd6 40static const int32_t devopts[] = {
1953564a 41 SR_CONF_OSCILLOSCOPE,
be6db330 42 SR_CONF_LIMIT_FRAMES,
1953564a
BV
43 SR_CONF_CONTINUOUS,
44 SR_CONF_TIMEBASE,
45 SR_CONF_BUFFERSIZE,
46 SR_CONF_TRIGGER_SOURCE,
47 SR_CONF_TRIGGER_SLOPE,
48 SR_CONF_HORIZ_TRIGGERPOS,
49 SR_CONF_FILTER,
50 SR_CONF_VDIV,
51 SR_CONF_COUPLING,
3b533202
BV
52};
53
54static const char *probe_names[] = {
78693401 55 "CH1", "CH2",
3b533202
BV
56 NULL,
57};
58
62bb8840 59static const struct dso_profile dev_profiles[] = {
88a13f30 60 { 0x04b4, 0x2090, 0x04b5, 0x2090,
3b533202 61 "Hantek", "DSO-2090",
7b78b2f7 62 FIRMWARE_DIR "/hantek-dso-2090.fw" },
88a13f30
BV
63 { 0x04b4, 0x2150, 0x04b5, 0x2150,
64 "Hantek", "DSO-2150",
7b78b2f7 65 FIRMWARE_DIR "/hantek-dso-2150.fw" },
88a13f30
BV
66 { 0x04b4, 0x2250, 0x04b5, 0x2250,
67 "Hantek", "DSO-2250",
7b78b2f7 68 FIRMWARE_DIR "/hantek-dso-2250.fw" },
88a13f30
BV
69 { 0x04b4, 0x5200, 0x04b5, 0x5200,
70 "Hantek", "DSO-5200",
7b78b2f7 71 FIRMWARE_DIR "/hantek-dso-5200.fw" },
88a13f30
BV
72 { 0x04b4, 0x520a, 0x04b5, 0x520a,
73 "Hantek", "DSO-5200A",
7b78b2f7 74 FIRMWARE_DIR "/hantek-dso-5200A.fw" },
88a13f30 75 { 0, 0, 0, 0, 0, 0, 0 },
3b533202
BV
76};
77
62bb8840 78static const uint64_t buffersizes[] = {
a370ef19
BV
79 10240,
80 32768,
81 /* TODO: 65535 */
a370ef19
BV
82};
83
86bb3f4a 84static const uint64_t timebases[][2] = {
a370ef19
BV
85 /* microseconds */
86 { 10, 1000000 },
87 { 20, 1000000 },
88 { 40, 1000000 },
89 { 100, 1000000 },
90 { 200, 1000000 },
91 { 400, 1000000 },
92 /* milliseconds */
93 { 1, 1000 },
94 { 2, 1000 },
95 { 4, 1000 },
96 { 10, 1000 },
97 { 20, 1000 },
98 { 40, 1000 },
99 { 100, 1000 },
100 { 200, 1000 },
101 { 400, 1000 },
a370ef19
BV
102};
103
86bb3f4a 104static const uint64_t vdivs[][2] = {
313deed2
BV
105 /* millivolts */
106 { 10, 1000 },
107 { 20, 1000 },
108 { 50, 1000 },
109 { 100, 1000 },
110 { 200, 1000 },
111 { 500, 1000 },
112 /* volts */
113 { 1, 1 },
114 { 2, 1 },
115 { 5, 1 },
313deed2
BV
116};
117
62bb8840 118static const char *trigger_sources[] = {
a370ef19
BV
119 "CH1",
120 "CH2",
121 "EXT",
88a13f30 122 /* TODO: forced */
a370ef19 123};
3b533202 124
62bb8840 125static const char *filter_targets[] = {
ebb781a6
BV
126 "CH1",
127 "CH2",
128 /* TODO: "TRIGGER", */
ebb781a6
BV
129};
130
62bb8840 131static const char *coupling[] = {
b58fbd99
BV
132 "AC",
133 "DC",
134 "GND",
b58fbd99
BV
135};
136
982947f7 137SR_PRIV struct sr_dev_driver hantek_dso_driver_info;
a873c594 138static struct sr_dev_driver *di = &hantek_dso_driver_info;
e98b7f1b 139
69b07d14 140static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
3b533202 141
62bb8840 142static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof)
3b533202
BV
143{
144 struct sr_dev_inst *sdi;
87ca93c5 145 struct sr_probe *probe;
269971dd
BV
146 struct drv_context *drvc;
147 struct dev_context *devc;
87ca93c5 148 int i;
3b533202
BV
149
150 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
88a13f30 151 prof->vendor, prof->model, NULL);
3b533202
BV
152 if (!sdi)
153 return NULL;
a873c594 154 sdi->driver = di;
3b533202 155
e98b7f1b
UH
156 /*
157 * Add only the real probes -- EXT isn't a source of data, only
87ca93c5
BV
158 * a trigger source internal to the device.
159 */
160 for (i = 0; probe_names[i]; i++) {
161 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
162 probe_names[i])))
163 return NULL;
164 sdi->probes = g_slist_append(sdi->probes, probe);
165 }
166
269971dd 167 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
e98b7f1b 168 sr_err("Device context malloc failed.");
3b533202
BV
169 return NULL;
170 }
e98b7f1b 171
269971dd
BV
172 devc->profile = prof;
173 devc->dev_state = IDLE;
174 devc->timebase = DEFAULT_TIMEBASE;
175 devc->ch1_enabled = TRUE;
176 devc->ch2_enabled = TRUE;
177 devc->voltage_ch1 = DEFAULT_VOLTAGE;
178 devc->voltage_ch2 = DEFAULT_VOLTAGE;
179 devc->coupling_ch1 = DEFAULT_COUPLING;
180 devc->coupling_ch2 = DEFAULT_COUPLING;
181 devc->voffset_ch1 = DEFAULT_VERT_OFFSET;
182 devc->voffset_ch2 = DEFAULT_VERT_OFFSET;
183 devc->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
184 devc->framesize = DEFAULT_FRAMESIZE;
185 devc->triggerslope = SLOPE_POSITIVE;
186 devc->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
187 devc->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
188 sdi->priv = devc;
a873c594 189 drvc = di->priv;
269971dd 190 drvc->instances = g_slist_append(drvc->instances, sdi);
3b533202
BV
191
192 return sdi;
193}
194
014359e3 195static int configure_probes(const struct sr_dev_inst *sdi)
3b533202 196{
014359e3 197 struct dev_context *devc;
69e19dd7 198 struct sr_probe *probe;
62bb8840 199 const GSList *l;
69e19dd7 200 int p;
3b533202 201
014359e3
BV
202 devc = sdi->priv;
203
69e19dd7 204 g_slist_free(devc->enabled_probes);
269971dd 205 devc->ch1_enabled = devc->ch2_enabled = FALSE;
69e19dd7
BV
206 for (l = sdi->probes, p = 0; l; l = l->next, p++) {
207 probe = l->data;
208 if (p == 0)
269971dd 209 devc->ch1_enabled = probe->enabled;
69e19dd7 210 else
269971dd 211 devc->ch2_enabled = probe->enabled;
69e19dd7
BV
212 if (probe->enabled)
213 devc->enabled_probes = g_slist_append(devc->enabled_probes, probe);
3b533202
BV
214 }
215
216 return SR_OK;
217}
218
39cfdd75 219/* Properly close and free all devices. */
811deee4 220static int clear_instances(void)
39cfdd75
BV
221{
222 struct sr_dev_inst *sdi;
269971dd
BV
223 struct drv_context *drvc;
224 struct dev_context *devc;
39cfdd75
BV
225 GSList *l;
226
a873c594 227 drvc = di->priv;
269971dd 228 for (l = drvc->instances; l; l = l->next) {
39cfdd75
BV
229 if (!(sdi = l->data)) {
230 /* Log error, but continue cleaning up the rest. */
e98b7f1b 231 sr_err("%s: sdi was NULL, continuing", __func__);
39cfdd75
BV
232 continue;
233 }
269971dd 234 if (!(devc = sdi->priv)) {
39cfdd75 235 /* Log error, but continue cleaning up the rest. */
e98b7f1b 236 sr_err("%s: sdi->priv was NULL, continuing", __func__);
39cfdd75
BV
237 continue;
238 }
239 dso_close(sdi);
269971dd
BV
240 sr_usb_dev_inst_free(devc->usb);
241 g_free(devc->triggersource);
69e19dd7 242 g_slist_free(devc->enabled_probes);
39cfdd75
BV
243
244 sr_dev_inst_free(sdi);
245 }
246
269971dd
BV
247 g_slist_free(drvc->instances);
248 drvc->instances = NULL;
39cfdd75 249
811deee4 250 return SR_OK;
39cfdd75
BV
251}
252
34f06b90 253static int hw_init(struct sr_context *sr_ctx)
61136ea6 254{
063e7aef 255 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
61136ea6
BV
256}
257
39cfdd75 258static GSList *hw_scan(GSList *options)
3b533202
BV
259{
260 struct sr_dev_inst *sdi;
62bb8840 261 const struct dso_profile *prof;
269971dd
BV
262 struct drv_context *drvc;
263 struct dev_context *devc;
39cfdd75
BV
264 GSList *devices;
265 struct libusb_device_descriptor des;
3b533202 266 libusb_device **devlist;
61136ea6 267 int devcnt, ret, i, j;
3b533202 268
39cfdd75 269 (void)options;
e98b7f1b 270
a873c594 271 drvc = di->priv;
269971dd 272 drvc->instances = NULL;
39cfdd75 273
4b97c74e
UH
274 devcnt = 0;
275 devices = 0;
276
39cfdd75
BV
277 clear_instances();
278
279 /* Find all Hantek DSO devices and upload firmware to all of them. */
d4abb463 280 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
3b533202 281 for (i = 0; devlist[i]; i++) {
61136ea6 282 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
d4928d71
PS
283 sr_err("Failed to get device descriptor: %s.",
284 libusb_error_name(ret));
3b533202
BV
285 continue;
286 }
287
288 prof = NULL;
289 for (j = 0; dev_profiles[j].orig_vid; j++) {
290 if (des.idVendor == dev_profiles[j].orig_vid
291 && des.idProduct == dev_profiles[j].orig_pid) {
292 /* Device matches the pre-firmware profile. */
293 prof = &dev_profiles[j];
e98b7f1b 294 sr_dbg("Found a %s %s.", prof->vendor, prof->model);
3b533202 295 sdi = dso_dev_new(devcnt, prof);
39cfdd75 296 devices = g_slist_append(devices, sdi);
269971dd 297 devc = sdi->priv;
3b533202
BV
298 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
299 prof->firmware) == SR_OK)
300 /* Remember when the firmware on this device was updated */
269971dd 301 devc->fw_updated = g_get_monotonic_time();
3b533202 302 else
e98b7f1b
UH
303 sr_err("Firmware upload failed for "
304 "device %d.", devcnt);
3b533202 305 /* Dummy USB address of 0xff will get overwritten later. */
269971dd 306 devc->usb = sr_usb_dev_inst_new(
3b533202
BV
307 libusb_get_bus_number(devlist[i]), 0xff, NULL);
308 devcnt++;
309 break;
310 } else if (des.idVendor == dev_profiles[j].fw_vid
311 && des.idProduct == dev_profiles[j].fw_pid) {
312 /* Device matches the post-firmware profile. */
313 prof = &dev_profiles[j];
e98b7f1b 314 sr_dbg("Found a %s %s.", prof->vendor, prof->model);
3b533202
BV
315 sdi = dso_dev_new(devcnt, prof);
316 sdi->status = SR_ST_INACTIVE;
39cfdd75 317 devices = g_slist_append(devices, sdi);
269971dd
BV
318 devc = sdi->priv;
319 devc->usb = sr_usb_dev_inst_new(
3b533202
BV
320 libusb_get_bus_number(devlist[i]),
321 libusb_get_device_address(devlist[i]), NULL);
322 devcnt++;
323 break;
324 }
325 }
326 if (!prof)
327 /* not a supported VID/PID */
328 continue;
329 }
330 libusb_free_device_list(devlist, 1);
331
39cfdd75 332 return devices;
3b533202
BV
333}
334
811deee4
BV
335static GSList *hw_dev_list(void)
336{
0e94d524 337 return ((struct drv_context *)(di->priv))->instances;
811deee4
BV
338}
339
25a0f108 340static int hw_dev_open(struct sr_dev_inst *sdi)
3b533202 341{
269971dd 342 struct dev_context *devc;
fc8fe3e3
BV
343 int64_t timediff_us, timediff_ms;
344 int err;
3b533202 345
269971dd 346 devc = sdi->priv;
3b533202
BV
347
348 /*
e98b7f1b
UH
349 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
350 * for the FX2 to renumerate.
3b533202 351 */
fc8fe3e3 352 err = SR_ERR;
269971dd 353 if (devc->fw_updated > 0) {
e98b7f1b
UH
354 sr_info("Waiting for device to reset.");
355 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
3b533202 356 g_usleep(300 * 1000);
fc8fe3e3
BV
357 timediff_ms = 0;
358 while (timediff_ms < MAX_RENUM_DELAY_MS) {
25a0f108 359 if ((err = dso_open(sdi)) == SR_OK)
3b533202
BV
360 break;
361 g_usleep(100 * 1000);
269971dd 362 timediff_us = g_get_monotonic_time() - devc->fw_updated;
fc8fe3e3 363 timediff_ms = timediff_us / 1000;
e98b7f1b 364 sr_spew("Waited %" PRIi64 " ms.", timediff_ms);
3b533202 365 }
e98b7f1b 366 sr_info("Device came back after %d ms.", timediff_ms);
3b533202 367 } else {
25a0f108 368 err = dso_open(sdi);
3b533202
BV
369 }
370
371 if (err != SR_OK) {
e98b7f1b 372 sr_err("Unable to open device.");
3b533202
BV
373 return SR_ERR;
374 }
375
269971dd 376 err = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
3b533202 377 if (err != 0) {
d4928d71
PS
378 sr_err("Unable to claim interface: %s.",
379 libusb_error_name(err));
3b533202
BV
380 return SR_ERR;
381 }
382
383 return SR_OK;
384}
385
25a0f108 386static int hw_dev_close(struct sr_dev_inst *sdi)
3b533202 387{
3b533202
BV
388 dso_close(sdi);
389
390 return SR_OK;
391}
392
393static int hw_cleanup(void)
394{
269971dd
BV
395 struct drv_context *drvc;
396
a873c594 397 if (!(drvc = di->priv))
269971dd 398 return SR_OK;
3b533202 399
39cfdd75 400 clear_instances();
3b533202 401
3b533202
BV
402 return SR_OK;
403}
404
f627afd6 405static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
3b533202 406{
269971dd 407 struct dev_context *devc;
f627afd6 408 double tmp_double;
86bb3f4a 409 uint64_t tmp_u64, p, q;
f627afd6
BV
410 int tmp_int, ret;
411 unsigned int i;
412 const char *tmp_str;
4a090d72 413 char **targets;
3b533202 414
3b533202
BV
415 if (sdi->status != SR_ST_ACTIVE)
416 return SR_ERR;
417
a370ef19 418 ret = SR_OK;
269971dd 419 devc = sdi->priv;
035a1078 420 switch (id) {
1953564a 421 case SR_CONF_LIMIT_FRAMES:
f627afd6 422 devc->limit_frames = g_variant_get_uint64(data);
ae88b97b 423 break;
1953564a 424 case SR_CONF_TRIGGER_SLOPE:
f627afd6 425 tmp_u64 = g_variant_get_uint64(data);
a370ef19
BV
426 if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE)
427 ret = SR_ERR_ARG;
269971dd 428 devc->triggerslope = tmp_u64;
a370ef19 429 break;
1953564a 430 case SR_CONF_HORIZ_TRIGGERPOS:
f627afd6
BV
431 tmp_double = g_variant_get_double(data);
432 if (tmp_double < 0.0 || tmp_double > 1.0) {
e98b7f1b 433 sr_err("Trigger position should be between 0.0 and 1.0.");
3b533202 434 ret = SR_ERR_ARG;
a370ef19 435 } else
f627afd6 436 devc->triggerposition = tmp_double;
a370ef19 437 break;
1953564a 438 case SR_CONF_BUFFERSIZE:
f627afd6 439 tmp_u64 = g_variant_get_uint64(data);
a370ef19
BV
440 for (i = 0; buffersizes[i]; i++) {
441 if (buffersizes[i] == tmp_u64) {
269971dd 442 devc->framesize = tmp_u64;
a370ef19
BV
443 break;
444 }
445 }
446 if (buffersizes[i] == 0)
447 ret = SR_ERR_ARG;
448 break;
1953564a 449 case SR_CONF_TIMEBASE:
86bb3f4a 450 g_variant_get(data, "(tt)", &p, &q);
f627afd6
BV
451 tmp_int = -1;
452 for (i = 0; i < ARRAY_SIZE(timebases); i++) {
453 if (timebases[i][0] == p && timebases[i][1] == q) {
454 tmp_int = i;
a370ef19
BV
455 break;
456 }
457 }
f627afd6
BV
458 if (tmp_int >= 0)
459 devc->timebase = tmp_int;
460 else
a370ef19
BV
461 ret = SR_ERR_ARG;
462 break;
1953564a 463 case SR_CONF_TRIGGER_SOURCE:
f627afd6 464 tmp_str = g_variant_get_string(data, NULL);
a370ef19 465 for (i = 0; trigger_sources[i]; i++) {
f627afd6
BV
466 if (!strcmp(tmp_str, trigger_sources[i])) {
467 devc->triggersource = g_strdup(tmp_str);
a370ef19
BV
468 break;
469 }
470 }
471 if (trigger_sources[i] == 0)
472 ret = SR_ERR_ARG;
473 break;
1953564a 474 case SR_CONF_FILTER:
f627afd6 475 tmp_str = g_variant_get_string(data, NULL);
269971dd 476 devc->filter_ch1 = devc->filter_ch2 = devc->filter_trigger = 0;
f627afd6 477 targets = g_strsplit(tmp_str, ",", 0);
ebb781a6
BV
478 for (i = 0; targets[i]; i++) {
479 if (targets[i] == '\0')
480 /* Empty filter string can be used to clear them all. */
481 ;
482 else if (!strcmp(targets[i], "CH1"))
269971dd 483 devc->filter_ch1 = TRUE;
ebb781a6 484 else if (!strcmp(targets[i], "CH2"))
269971dd 485 devc->filter_ch2 = TRUE;
ebb781a6 486 else if (!strcmp(targets[i], "TRIGGER"))
269971dd 487 devc->filter_trigger = TRUE;
ebb781a6 488 else {
e98b7f1b 489 sr_err("Invalid filter target %s.", targets[i]);
ebb781a6
BV
490 ret = SR_ERR_ARG;
491 }
492 }
493 g_strfreev(targets);
494 break;
1953564a 495 case SR_CONF_VDIV:
e98b7f1b 496 /* TODO: Not supporting vdiv per channel yet. */
86bb3f4a 497 g_variant_get(data, "(tt)", &p, &q);
f627afd6
BV
498 tmp_int = -1;
499 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
500 if (vdivs[i][0] == p && vdivs[i][1] == q) {
501 tmp_int = i;
313deed2
BV
502 break;
503 }
504 }
f627afd6
BV
505 if (tmp_int >= 0) {
506 devc->voltage_ch1 = tmp_int;
507 devc->voltage_ch2 = tmp_int;
508 } else
313deed2
BV
509 ret = SR_ERR_ARG;
510 break;
1953564a 511 case SR_CONF_COUPLING:
f627afd6 512 tmp_str = g_variant_get_string(data, NULL);
e98b7f1b 513 /* TODO: Not supporting coupling per channel yet. */
b58fbd99 514 for (i = 0; coupling[i]; i++) {
f627afd6 515 if (!strcmp(tmp_str, coupling[i])) {
269971dd
BV
516 devc->coupling_ch1 = i;
517 devc->coupling_ch2 = i;
b58fbd99
BV
518 break;
519 }
520 }
521 if (coupling[i] == 0)
522 ret = SR_ERR_ARG;
523 break;
3b533202
BV
524 default:
525 ret = SR_ERR_ARG;
e98b7f1b 526 break;
3b533202
BV
527 }
528
529 return ret;
530}
531
f627afd6 532static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
a1c743fc
BV
533{
534
535 (void)sdi;
536
537 switch (key) {
9a6517d1 538 case SR_CONF_DEVICE_OPTIONS:
f627afd6
BV
539 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
540 devopts, ARRAY_SIZE(devopts), sizeof(int32_t));
9a6517d1 541 break;
6d1ceffa 542 case SR_CONF_BUFFERSIZE:
f627afd6
BV
543 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
544 buffersizes, ARRAY_SIZE(buffersizes), sizeof(uint64_t));
6d1ceffa 545 break;
2a7b113d 546 case SR_CONF_COUPLING:
f627afd6 547 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
2a7b113d 548 break;
e4f2b2ad 549 case SR_CONF_VDIV:
86bb3f4a
BV
550 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
551 vdivs, ARRAY_SIZE(vdivs) * 2, sizeof(uint64_t));
e4f2b2ad 552 break;
6e1fbcc4 553 case SR_CONF_FILTER:
f627afd6
BV
554 *data = g_variant_new_strv(filter_targets,
555 ARRAY_SIZE(filter_targets));
6e1fbcc4 556 break;
41f5bd09 557 case SR_CONF_TIMEBASE:
86bb3f4a
BV
558 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
559 timebases, ARRAY_SIZE(timebases) * 2, sizeof(uint64_t));
41f5bd09 560 break;
328bafab 561 case SR_CONF_TRIGGER_SOURCE:
f627afd6
BV
562 *data = g_variant_new_strv(trigger_sources,
563 ARRAY_SIZE(trigger_sources));
328bafab 564 break;
a1c743fc
BV
565 default:
566 return SR_ERR_ARG;
567 }
568
569 return SR_OK;
570}
571
69e19dd7 572static void send_chunk(struct sr_dev_inst *sdi, unsigned char *buf,
e749a8cb 573 int num_samples)
3b533202
BV
574{
575 struct sr_datafeed_packet packet;
576 struct sr_datafeed_analog analog;
69e19dd7 577 struct dev_context *devc;
c5841b28 578 float ch1, ch2, range;
6e71ef3b 579 int num_probes, data_offset, i;
3b533202 580
69e19dd7 581 devc = sdi->priv;
269971dd 582 num_probes = (devc->ch1_enabled && devc->ch2_enabled) ? 2 : 1;
3b533202
BV
583 packet.type = SR_DF_ANALOG;
584 packet.payload = &analog;
6e71ef3b 585 /* TODO: support for 5xxx series 9-bit samples */
69e19dd7 586 analog.probes = devc->enabled_probes;
e749a8cb 587 analog.num_samples = num_samples;
9956f285
UH
588 analog.mq = SR_MQ_VOLTAGE;
589 analog.unit = SR_UNIT_VOLT;
886a52b6 590 /* TODO: Check malloc return value. */
6e71ef3b
BV
591 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
592 data_offset = 0;
3b533202 593 for (i = 0; i < analog.num_samples; i++) {
e98b7f1b
UH
594 /*
595 * The device always sends data for both channels. If a channel
6e71ef3b 596 * is disabled, it contains a copy of the enabled channel's
e98b7f1b
UH
597 * data. However, we only send the requested channels to
598 * the bus.
c5841b28 599 *
e98b7f1b
UH
600 * Voltage values are encoded as a value 0-255 (0-512 on the
601 * DSO-5200*), where the value is a point in the range
602 * represented by the vdiv setting. There are 8 vertical divs,
603 * so e.g. 500mV/div represents 4V peak-to-peak where 0 = -2V
604 * and 255 = +2V.
6e71ef3b 605 */
e98b7f1b 606 /* TODO: Support for DSO-5xxx series 9-bit samples. */
269971dd 607 if (devc->ch1_enabled) {
f627afd6 608 range = ((float)vdivs[devc->voltage_ch1][0] / vdivs[devc->voltage_ch1][1]) * 8;
e749a8cb 609 ch1 = range / 255 * *(buf + i * 2 + 1);
c5841b28
BV
610 /* Value is centered around 0V. */
611 ch1 -= range / 2;
6e71ef3b
BV
612 analog.data[data_offset++] = ch1;
613 }
269971dd 614 if (devc->ch2_enabled) {
f627afd6 615 range = ((float)vdivs[devc->voltage_ch2][0] / vdivs[devc->voltage_ch2][1]) * 8;
e749a8cb 616 ch2 = range / 255 * *(buf + i * 2);
c5841b28 617 ch2 -= range / 2;
6e71ef3b
BV
618 analog.data[data_offset++] = ch2;
619 }
3b533202 620 }
269971dd 621 sr_session_send(devc->cb_data, &packet);
e749a8cb
BV
622}
623
e98b7f1b
UH
624/*
625 * Called by libusb (as triggered by handle_event()) when a transfer comes in.
e749a8cb 626 * Only channel data comes in asynchronously, and all transfers for this are
e98b7f1b 627 * queued up beforehand, so this just needs to chuck the incoming data onto
e749a8cb
BV
628 * the libsigrok session bus.
629 */
630static void receive_transfer(struct libusb_transfer *transfer)
631{
632 struct sr_datafeed_packet packet;
69e19dd7 633 struct sr_dev_inst *sdi;
269971dd 634 struct dev_context *devc;
e749a8cb
BV
635 int num_samples, pre;
636
69e19dd7
BV
637 sdi = transfer->user_data;
638 devc = sdi->priv;
d4007311 639 sr_spew("receive_transfer(): status %d received %d bytes.",
e98b7f1b 640 transfer->status, transfer->actual_length);
e749a8cb
BV
641
642 if (transfer->actual_length == 0)
643 /* Nothing to send to the bus. */
644 return;
645
646 num_samples = transfer->actual_length / 2;
647
d4007311 648 sr_spew("Got %d-%d/%d samples in frame.", devc->samp_received + 1,
e98b7f1b 649 devc->samp_received + num_samples, devc->framesize);
e749a8cb 650
e98b7f1b
UH
651 /*
652 * The device always sends a full frame, but the beginning of the frame
e749a8cb
BV
653 * doesn't represent the trigger point. The offset at which the trigger
654 * happened came in with the capture state, so we need to start sending
e98b7f1b
UH
655 * from there up the session bus. The samples in the frame buffer
656 * before that trigger point came after the end of the device's frame
657 * buffer was reached, and it wrapped around to overwrite up until the
658 * trigger point.
e749a8cb 659 */
269971dd 660 if (devc->samp_received < devc->trigger_offset) {
e749a8cb 661 /* Trigger point not yet reached. */
269971dd 662 if (devc->samp_received + num_samples < devc->trigger_offset) {
e749a8cb 663 /* The entire chunk is before the trigger point. */
269971dd 664 memcpy(devc->framebuf + devc->samp_buffered * 2,
e749a8cb 665 transfer->buffer, num_samples * 2);
269971dd 666 devc->samp_buffered += num_samples;
e749a8cb 667 } else {
e98b7f1b
UH
668 /*
669 * This chunk hits or overruns the trigger point.
e749a8cb 670 * Store the part before the trigger fired, and
e98b7f1b
UH
671 * send the rest up to the session bus.
672 */
269971dd
BV
673 pre = devc->trigger_offset - devc->samp_received;
674 memcpy(devc->framebuf + devc->samp_buffered * 2,
e749a8cb 675 transfer->buffer, pre * 2);
269971dd 676 devc->samp_buffered += pre;
e749a8cb
BV
677
678 /* The rest of this chunk starts with the trigger point. */
e98b7f1b
UH
679 sr_dbg("Reached trigger point, %d samples buffered.",
680 devc->samp_buffered);
e749a8cb
BV
681
682 /* Avoid the corner case where the chunk ended at
683 * exactly the trigger point. */
684 if (num_samples > pre)
69e19dd7 685 send_chunk(sdi, transfer->buffer + pre * 2,
e749a8cb
BV
686 num_samples - pre);
687 }
688 } else {
689 /* Already past the trigger point, just send it all out. */
69e19dd7 690 send_chunk(sdi, transfer->buffer,
e749a8cb
BV
691 num_samples);
692 }
693
269971dd 694 devc->samp_received += num_samples;
e749a8cb
BV
695
696 /* Everything in this transfer was either copied to the buffer or
697 * sent to the session bus. */
3b533202
BV
698 g_free(transfer->buffer);
699 libusb_free_transfer(transfer);
3b533202 700
269971dd 701 if (devc->samp_received >= devc->framesize) {
e749a8cb
BV
702 /* That was the last chunk in this frame. Send the buffered
703 * pre-trigger samples out now, in one big chunk. */
e98b7f1b
UH
704 sr_dbg("End of frame, sending %d pre-trigger buffered samples.",
705 devc->samp_buffered);
69e19dd7 706 send_chunk(sdi, devc->framebuf, devc->samp_buffered);
e749a8cb
BV
707
708 /* Mark the end of this frame. */
ae88b97b 709 packet.type = SR_DF_FRAME_END;
269971dd 710 sr_session_send(devc->cb_data, &packet);
ae88b97b 711
269971dd 712 if (devc->limit_frames && ++devc->num_frames == devc->limit_frames) {
ae88b97b 713 /* Terminate session */
a3508e33 714 devc->dev_state = STOPPING;
ae88b97b 715 } else {
269971dd 716 devc->dev_state = NEW_CAPTURE;
ae88b97b
BV
717 }
718 }
3b533202
BV
719}
720
721static int handle_event(int fd, int revents, void *cb_data)
722{
a3508e33 723 const struct sr_dev_inst *sdi;
ae88b97b 724 struct sr_datafeed_packet packet;
3b533202 725 struct timeval tv;
269971dd 726 struct dev_context *devc;
a873c594 727 struct drv_context *drvc = di->priv;
a3508e33
BV
728 const struct libusb_pollfd **lupfd;
729 int num_probes, i;
6e6eeff4
BV
730 uint32_t trigger_offset;
731 uint8_t capturestate;
3b533202 732
3b533202
BV
733 (void)fd;
734 (void)revents;
735
269971dd
BV
736 sdi = cb_data;
737 devc = sdi->priv;
a3508e33
BV
738 if (devc->dev_state == STOPPING) {
739 /* We've been told to wind up the acquisition. */
e98b7f1b
UH
740 sr_dbg("Stopping acquisition.");
741 /*
742 * TODO: Doesn't really cancel pending transfers so they might
743 * come in after SR_DF_END is sent.
744 */
d4abb463 745 lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
a3508e33
BV
746 for (i = 0; lupfd[i]; i++)
747 sr_source_remove(lupfd[i]->fd);
748 free(lupfd);
749
750 packet.type = SR_DF_END;
751 sr_session_send(sdi, &packet);
752
753 devc->dev_state = IDLE;
754
755 return TRUE;
756 }
757
3b533202
BV
758 /* Always handle pending libusb events. */
759 tv.tv_sec = tv.tv_usec = 0;
d4abb463 760 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
3b533202 761
3b533202 762 /* TODO: ugh */
269971dd
BV
763 if (devc->dev_state == NEW_CAPTURE) {
764 if (dso_capture_start(devc) != SR_OK)
3b533202 765 return TRUE;
269971dd 766 if (dso_enable_trigger(devc) != SR_OK)
3b533202 767 return TRUE;
269971dd 768// if (dso_force_trigger(devc) != SR_OK)
a370ef19 769// return TRUE;
e98b7f1b 770 sr_dbg("Successfully requested next chunk.");
269971dd 771 devc->dev_state = CAPTURE;
3b533202
BV
772 return TRUE;
773 }
269971dd 774 if (devc->dev_state != CAPTURE)
3b533202
BV
775 return TRUE;
776
269971dd 777 if ((dso_get_capturestate(devc, &capturestate, &trigger_offset)) != SR_OK)
3b533202 778 return TRUE;
3b533202 779
e98b7f1b
UH
780 sr_dbg("Capturestate %d.", capturestate);
781 sr_dbg("Trigger offset 0x%.6x.", trigger_offset);
3b533202
BV
782 switch (capturestate) {
783 case CAPTURE_EMPTY:
269971dd
BV
784 if (++devc->capture_empty_count >= MAX_CAPTURE_EMPTY) {
785 devc->capture_empty_count = 0;
786 if (dso_capture_start(devc) != SR_OK)
3b533202 787 break;
269971dd 788 if (dso_enable_trigger(devc) != SR_OK)
3b533202 789 break;
269971dd 790// if (dso_force_trigger(devc) != SR_OK)
a370ef19 791// break;
e98b7f1b 792 sr_dbg("Successfully requested next chunk.");
3b533202
BV
793 }
794 break;
795 case CAPTURE_FILLING:
e98b7f1b 796 /* No data yet. */
3b533202
BV
797 break;
798 case CAPTURE_READY_8BIT:
e749a8cb 799 /* Remember where in the captured frame the trigger is. */
269971dd 800 devc->trigger_offset = trigger_offset;
e749a8cb 801
269971dd 802 num_probes = (devc->ch1_enabled && devc->ch2_enabled) ? 2 : 1;
886a52b6 803 /* TODO: Check malloc return value. */
269971dd
BV
804 devc->framebuf = g_try_malloc(devc->framesize * num_probes * 2);
805 devc->samp_buffered = devc->samp_received = 0;
e749a8cb 806
3b533202 807 /* Tell the scope to send us the first frame. */
69e19dd7 808 if (dso_get_channeldata(sdi, receive_transfer) != SR_OK)
3b533202 809 break;
ae88b97b 810
e98b7f1b
UH
811 /*
812 * Don't hit the state machine again until we're done fetching
ae88b97b
BV
813 * the data we just told the scope to send.
814 */
269971dd 815 devc->dev_state = FETCH_DATA;
ae88b97b
BV
816
817 /* Tell the frontend a new frame is on the way. */
818 packet.type = SR_DF_FRAME_BEGIN;
269971dd 819 sr_session_send(sdi, &packet);
3b533202
BV
820 break;
821 case CAPTURE_READY_9BIT:
822 /* TODO */
e98b7f1b 823 sr_err("Not yet supported.");
3b533202
BV
824 break;
825 case CAPTURE_TIMEOUT:
826 /* Doesn't matter, we'll try again next time. */
827 break;
828 default:
e98b7f1b
UH
829 sr_dbg("Unknown capture state: %d.", capturestate);
830 break;
3b533202
BV
831 }
832
833 return TRUE;
834}
835
3ffb6964 836static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
e98b7f1b 837 void *cb_data)
3b533202
BV
838{
839 const struct libusb_pollfd **lupfd;
269971dd 840 struct dev_context *devc;
a873c594 841 struct drv_context *drvc = di->priv;
3b533202
BV
842 int i;
843
3b533202
BV
844 if (sdi->status != SR_ST_ACTIVE)
845 return SR_ERR;
846
269971dd
BV
847 devc = sdi->priv;
848 devc->cb_data = cb_data;
3b533202 849
014359e3 850 if (configure_probes(sdi) != SR_OK) {
e98b7f1b 851 sr_err("Failed to configure probes.");
014359e3
BV
852 return SR_ERR;
853 }
854
269971dd 855 if (dso_init(devc) != SR_OK)
3b533202
BV
856 return SR_ERR;
857
269971dd 858 if (dso_capture_start(devc) != SR_OK)
3b533202
BV
859 return SR_ERR;
860
269971dd 861 devc->dev_state = CAPTURE;
d4abb463 862 lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
3b533202 863 for (i = 0; lupfd[i]; i++)
e98b7f1b
UH
864 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK,
865 handle_event, (void *)sdi);
3b533202
BV
866 free(lupfd);
867
868 /* Send header packet to the session bus. */
4afdfd46 869 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
3b533202 870
3b533202
BV
871 return SR_OK;
872}
873
69b07d14 874static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
3b533202 875{
269971dd
BV
876 struct dev_context *devc;
877
878 (void)cb_data;
3b533202 879
3b533202
BV
880 if (sdi->status != SR_ST_ACTIVE)
881 return SR_ERR;
882
a3508e33
BV
883 devc = sdi->priv;
884 devc->dev_state = STOPPING;
3b533202
BV
885
886 return SR_OK;
887}
888
62bb8840 889SR_PRIV struct sr_dev_driver hantek_dso_driver_info = {
3b533202
BV
890 .name = "hantek-dso",
891 .longname = "Hantek DSO",
892 .api_version = 1,
893 .init = hw_init,
894 .cleanup = hw_cleanup,
61136ea6 895 .scan = hw_scan,
811deee4
BV
896 .dev_list = hw_dev_list,
897 .dev_clear = clear_instances,
6fab7b8f 898 .config_get = NULL,
035a1078 899 .config_set = config_set,
a1c743fc 900 .config_list = config_list,
3b533202
BV
901 .dev_open = hw_dev_open,
902 .dev_close = hw_dev_close,
62bb8840
UH
903 .dev_acquisition_start = hw_dev_acquisition_start,
904 .dev_acquisition_stop = hw_dev_acquisition_stop,
269971dd 905 .priv = NULL,
3b533202 906};