]> sigrok.org Git - libsigrok.git/blame - src/hardware/hantek-6xxx/api.c
hantek-6xxx: Initial driver implementation.
[libsigrok.git] / src / hardware / hantek-6xxx / api.c
CommitLineData
6c6bc80a
C
1/*
2 * This file is part of the libsigrok project.
3 *
f2a66a8e 4 * Copyright (C) 2015 Christer Ekholm <christerekholm@gmail.com>
6c6bc80a
C
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 <config.h>
21#include "protocol.h"
22
f2a66a8e
C
23/* Max time in ms before we want to check on USB events */
24#define TICK 200
25
26#define RANGE(ch) (((float)vdivs[devc->voltage[ch]][0] / vdivs[devc->voltage[ch]][1]) * VDIV_MULTIPLIER)
27
28static const uint32_t scanopts[] = {
29 SR_CONF_CONN,
30};
31
32static const uint32_t drvopts[] = {
33 SR_CONF_OSCILLOSCOPE,
34};
35
36static const uint32_t devopts[] = {
37 SR_CONF_CONN | SR_CONF_GET,
38 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_NUM_VDIV | SR_CONF_GET,
40 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
41 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
42};
43
44static const uint32_t devopts_cg[] = {
45 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46};
47
48static const char *channel_names[] = {
49 "CH1", "CH2",
50};
51
52static const struct hantek_6xxx_profile dev_profiles[] = {
53 {
54 0x04b4, 0x6022, 0x04b5, 0x6022,
55 "Hantek", "6022BE", "hantek-6022be.fw",
56 },
57 ALL_ZERO
58};
59
60static const uint64_t samplerates[] = {
61 SAMPLERATE_VALUES
62};
63
64static const uint64_t vdivs[][2] = {
65 VDIV_VALUES
66};
67
6c6bc80a
C
68SR_PRIV struct sr_dev_driver hantek_6xxx_driver_info;
69
f2a66a8e
C
70static int read_channel(const struct sr_dev_inst *sdi, uint32_t amount);
71
72static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
73
74static struct sr_dev_inst *hantek_6xxx_dev_new(const struct hantek_6xxx_profile *prof)
75{
76 struct sr_dev_inst *sdi;
77 struct sr_channel *ch;
78 struct sr_channel_group *cg;
79 struct drv_context *drvc;
80 struct dev_context *devc;
81 unsigned int i;
82
83 sdi = g_malloc0(sizeof(struct sr_dev_inst));
84 sdi->status = SR_ST_INITIALIZING;
85 sdi->vendor = g_strdup(prof->vendor);
86 sdi->model = g_strdup(prof->model);
87 sdi->driver = &hantek_6xxx_driver_info;
88
89 for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
90 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]);
91 cg = g_malloc0(sizeof(struct sr_channel_group));
92 cg->name = g_strdup(channel_names[i]);
93 cg->channels = g_slist_append(cg->channels, ch);
94 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
95 }
96
97 devc = g_malloc0(sizeof(struct dev_context));
98
99 for (i = 0; i < NUM_CHANNELS; i++) {
100 devc->ch_enabled[i] = TRUE;
101 devc->voltage[i] = DEFAULT_VOLTAGE;
102 }
103
104 devc->sample_buf = NULL;
105 devc->sample_buf_write = 0;
106 devc->sample_buf_size = 0;
107
108 devc->profile = prof;
109 devc->dev_state = IDLE;
110 devc->samplerate = DEFAULT_SAMPLERATE;
111
112 sdi->priv = devc;
113 drvc = sdi->driver->context;
114 drvc->instances = g_slist_append(drvc->instances, sdi);
115
116 return sdi;
117}
118
119static int configure_channels(const struct sr_dev_inst *sdi)
120{
121 struct dev_context *devc;
122 const GSList *l;
123 int p;
124 struct sr_channel *ch;
125 devc = sdi->priv;
126
127 g_slist_free(devc->enabled_channels);
128 devc->enabled_channels = NULL;
129 memset(devc->ch_enabled, 0, sizeof(devc->ch_enabled));
130
131 for (l = sdi->channels, p = 0; l; l = l->next, p++) {
132 ch = l->data;
133 if (p < NUM_CHANNELS) {
134 devc->ch_enabled[p] = ch->enabled;
135 devc->enabled_channels = g_slist_append(devc->enabled_channels, ch);
136 }
137 }
138
139 return SR_OK;
140}
141
142static void clear_dev_context(void *priv)
143{
144 struct dev_context *devc;
145
146 devc = priv;
147 g_slist_free(devc->enabled_channels);
148}
149
150static int dev_clear(const struct sr_dev_driver *di)
151{
152 return std_dev_clear(di, clear_dev_context);
153}
154
6c6bc80a
C
155static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
156{
157 return std_init(sr_ctx, di, LOG_PREFIX);
158}
159
160static GSList *scan(struct sr_dev_driver *di, GSList *options)
161{
162 struct drv_context *drvc;
f2a66a8e
C
163 struct dev_context *devc;
164 struct sr_dev_inst *sdi;
165 struct sr_usb_dev_inst *usb;
166 struct sr_config *src;
167 const struct hantek_6xxx_profile *prof;
168 GSList *l, *devices, *conn_devices;
169 struct libusb_device_descriptor des;
170 libusb_device **devlist;
171 int i, j;
172 const char *conn;
173 char connection_id[64];
6c6bc80a 174
6c6bc80a 175 drvc = di->context;
6c6bc80a 176
f2a66a8e
C
177 devices = 0;
178
179 conn = NULL;
180 for (l = options; l; l = l->next) {
181 src = l->data;
182 if (src->key == SR_CONF_CONN) {
183 conn = g_variant_get_string(src->data, NULL);
184 break;
185 }
186 }
187 if (conn)
188 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
189 else
190 conn_devices = NULL;
191
192 /* Find all Hantek 60xx devices and upload firmware to all of them. */
193 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
194 for (i = 0; devlist[i]; i++) {
195 if (conn) {
196 usb = NULL;
197 for (l = conn_devices; l; l = l->next) {
198 usb = l->data;
199 if (usb->bus == libusb_get_bus_number(devlist[i])
200 && usb->address == libusb_get_device_address(devlist[i]))
201 break;
202 }
203 if (!l)
204 /* This device matched none of the ones that
205 * matched the conn specification. */
206 continue;
207 }
208
209 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
210 sr_err("Failed to get device descriptor: %s.",
211 libusb_error_name(ret));
212 continue;
213 }
214
215 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
216
217 prof = NULL;
218 for (j = 0; j < (int)ARRAY_SIZE(dev_profiles); j++) {
219 if (des.idVendor == dev_profiles[j].orig_vid
220 && des.idProduct == dev_profiles[j].orig_pid) {
221 /* Device matches the pre-firmware profile. */
222 prof = &dev_profiles[j];
223 sr_dbg("Found a %s %s.", prof->vendor, prof->model);
224 sdi = hantek_6xxx_dev_new(prof);
225 sdi->connection_id = g_strdup(connection_id);
226 devices = g_slist_append(devices, sdi);
227 devc = sdi->priv;
228 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
229 USB_CONFIGURATION, prof->firmware) == SR_OK)
230 /* Remember when the firmware on this device was updated. */
231 devc->fw_updated = g_get_monotonic_time();
232 else
233 sr_err("Firmware upload failed.");
234 /* Dummy USB address of 0xff will get overwritten later. */
235 sdi->conn = sr_usb_dev_inst_new(
236 libusb_get_bus_number(devlist[i]), 0xff, NULL);
237 break;
238 } else if (des.idVendor == dev_profiles[j].fw_vid
239 && des.idProduct == dev_profiles[j].fw_pid) {
240 /* Device matches the post-firmware profile. */
241 prof = &dev_profiles[j];
242 sr_dbg("Found a %s %s.", prof->vendor, prof->model);
243 sdi = hantek_6xxx_dev_new(prof);
244 sdi->connection_id = g_strdup(connection_id);
245 sdi->status = SR_ST_INACTIVE;
246 devices = g_slist_append(devices, sdi);
247 sdi->inst_type = SR_INST_USB;
248 sdi->conn = sr_usb_dev_inst_new(
249 libusb_get_bus_number(devlist[i]),
250 libusb_get_device_address(devlist[i]), NULL);
251 break;
252 }
253 }
254 if (!prof)
255 /* Not a supported VID/PID. */
256 continue;
257 }
258 libusb_free_device_list(devlist, 1);
6c6bc80a
C
259
260 return devices;
261}
262
263static GSList *dev_list(const struct sr_dev_driver *di)
264{
265 return ((struct drv_context *)(di->context))->instances;
266}
267
6c6bc80a
C
268static int dev_open(struct sr_dev_inst *sdi)
269{
f2a66a8e
C
270 struct dev_context *devc;
271 struct sr_usb_dev_inst *usb;
272 int64_t timediff_us, timediff_ms;
273 int err;
274
275 devc = sdi->priv;
276 usb = sdi->conn;
277
278 /*
279 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
280 * for the FX2 to renumerate.
281 */
282 err = SR_ERR;
283 if (devc->fw_updated > 0) {
284 sr_info("Waiting for device to reset.");
285 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
286 g_usleep(300 * 1000);
287 timediff_ms = 0;
288 while (timediff_ms < MAX_RENUM_DELAY_MS) {
289 if ((err = hantek_6xxx_open(sdi)) == SR_OK)
290 break;
291 g_usleep(100 * 1000);
292 timediff_us = g_get_monotonic_time() - devc->fw_updated;
293 timediff_ms = timediff_us / 1000;
294 sr_spew("Waited %" PRIi64 " ms.", timediff_ms);
295 }
296 if (timediff_ms < MAX_RENUM_DELAY_MS)
297 sr_info("Device came back after %"PRIu64" ms.", timediff_ms);
298 } else {
299 err = hantek_6xxx_open(sdi);
300 }
6c6bc80a 301
f2a66a8e
C
302 if (err != SR_OK) {
303 sr_err("Unable to open device.");
304 return SR_ERR;
305 }
6c6bc80a 306
f2a66a8e
C
307 err = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
308 if (err != 0) {
309 sr_err("Unable to claim interface: %s.",
310 libusb_error_name(err));
311 return SR_ERR;
312 }
6c6bc80a
C
313
314 return SR_OK;
315}
316
317static int dev_close(struct sr_dev_inst *sdi)
318{
f2a66a8e 319 hantek_6xxx_close(sdi);
6c6bc80a
C
320
321 return SR_OK;
322}
323
324static int cleanup(const struct sr_dev_driver *di)
325{
f2a66a8e 326 return dev_clear(di);
6c6bc80a
C
327}
328
329static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
330 const struct sr_channel_group *cg)
331{
f2a66a8e
C
332 struct dev_context *devc;
333 struct sr_usb_dev_inst *usb;
334 char str[128];
335 const uint64_t *vdiv;
336 int ch_idx;
6c6bc80a 337
6c6bc80a 338 switch (key) {
f2a66a8e
C
339 case SR_CONF_NUM_VDIV:
340 *data = g_variant_new_int32(ARRAY_SIZE(vdivs));
341 break;
6c6bc80a
C
342 }
343
f2a66a8e
C
344 if (!sdi)
345 return SR_ERR_ARG;
346
347 devc = sdi->priv;
348 if (!cg) {
349 switch (key) {
350 case SR_CONF_SAMPLERATE:
351 *data = g_variant_new_uint64(devc->samplerate);
352 break;
353 case SR_CONF_LIMIT_MSEC:
354 *data = g_variant_new_uint64(devc->limit_msec);
355 break;
356 case SR_CONF_LIMIT_SAMPLES:
357 *data = g_variant_new_uint64(devc->limit_samples);
358 break;
359 case SR_CONF_CONN:
360 if (!sdi->conn)
361 return SR_ERR_ARG;
362 usb = sdi->conn;
363 if (usb->address == 255)
364 /* Device still needs to re-enumerate after firmware
365 * upload, so we don't know its (future) address. */
366 return SR_ERR;
367 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
368 *data = g_variant_new_string(str);
369 break;
370 default:
371 return SR_ERR_NA;
372 }
373 } else {
374 if (sdi->channel_groups->data == cg)
375 ch_idx = 0;
376 else if (sdi->channel_groups->next->data == cg)
377 ch_idx = 1;
378 else
379 return SR_ERR_ARG;
380 switch (key) {
381 case SR_CONF_VDIV:
382 vdiv = vdivs[devc->voltage[ch_idx]];
383 *data = g_variant_new("(tt)", vdiv[0], vdiv[1]);
384 break;
385 }
386 }
387
388 return SR_OK;
6c6bc80a
C
389}
390
391static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
392 const struct sr_channel_group *cg)
393{
f2a66a8e
C
394 struct dev_context *devc;
395 uint64_t p, q;
396 int tmp_int, ch_idx, ret;
397 unsigned int i;
6c6bc80a
C
398
399 if (sdi->status != SR_ST_ACTIVE)
400 return SR_ERR_DEV_CLOSED;
401
402 ret = SR_OK;
f2a66a8e
C
403 devc = sdi->priv;
404 if (!cg) {
405 switch (key) {
406 case SR_CONF_SAMPLERATE:
407 devc->samplerate = g_variant_get_uint64(data);
408 hantek_6xxx_update_samplerate(sdi);
409 break;
410 case SR_CONF_LIMIT_MSEC:
411 devc->limit_msec = g_variant_get_uint64(data);
412 break;
413 case SR_CONF_LIMIT_SAMPLES:
414 devc->limit_samples = g_variant_get_uint64(data);
415 break;
416 default:
417 ret = SR_ERR_NA;
418 break;
419 }
420 } else {
421 if (sdi->channel_groups->data == cg)
422 ch_idx = 0;
423 else if (sdi->channel_groups->next->data == cg)
424 ch_idx = 1;
425 else
426 return SR_ERR_ARG;
427 switch (key) {
428 case SR_CONF_VDIV:
429 g_variant_get(data, "(tt)", &p, &q);
430 tmp_int = -1;
431 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
432 if (vdivs[i][0] == p && vdivs[i][1] == q) {
433 tmp_int = i;
434 break;
435 }
436 }
437 if (tmp_int >= 0) {
438 devc->voltage[ch_idx] = tmp_int;
439 hantek_6xxx_update_vdiv(sdi);
440 } else
441 ret = SR_ERR_ARG;
442 break;
443 default:
444 ret = SR_ERR_NA;
445 break;
446 }
6c6bc80a
C
447 }
448
449 return ret;
450}
451
452static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
453 const struct sr_channel_group *cg)
454{
f2a66a8e
C
455 GVariant *tuple, *rational[2];
456 GVariantBuilder gvb;
457 unsigned int i;
458 GVariant *gvar;
459
460 if (key == SR_CONF_SCAN_OPTIONS) {
461 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
462 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
463 return SR_OK;
464 } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
465 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
466 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
467 return SR_OK;
468 }
6c6bc80a 469
f2a66a8e
C
470 if (!sdi)
471 return SR_ERR_ARG;
472
473 if (!cg) {
474 switch (key) {
475 case SR_CONF_DEVICE_OPTIONS:
476 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
477 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
478 break;
479 case SR_CONF_SAMPLERATE:
480 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
481 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
482 samplerates, ARRAY_SIZE(samplerates),
483 sizeof(uint64_t));
484 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
485 *data = g_variant_builder_end(&gvb);
486 break;
487 default:
488 return SR_ERR_NA;
489 }
490 } else {
491 switch (key) {
492 case SR_CONF_DEVICE_OPTIONS:
493 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
494 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
495 break;
496 case SR_CONF_VDIV:
497 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
498 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
499 rational[0] = g_variant_new_uint64(vdivs[i][0]);
500 rational[1] = g_variant_new_uint64(vdivs[i][1]);
501 tuple = g_variant_new_tuple(rational, 2);
502 g_variant_builder_add_value(&gvb, tuple);
503 }
504 *data = g_variant_builder_end(&gvb);
505 break;
506 default:
507 return SR_ERR_NA;
508 }
509 }
6c6bc80a 510
f2a66a8e
C
511 return SR_OK;
512}
513
514/* Minimise data amount for limit_samples and limit_msec limits. */
515static uint32_t data_amount(const struct sr_dev_inst *sdi)
516{
517 struct dev_context *devc = sdi->priv;
518 uint32_t data_left;
519 int32_t time_left;
520
521 if (devc->limit_msec) {
522 time_left = devc->limit_msec - (g_get_monotonic_time() - devc->aq_started) / 1000;
523 data_left = devc->samplerate * MAX(time_left, 0) * NUM_CHANNELS / 1000;
524 } else if (devc->limit_samples) {
525 data_left = (devc->limit_samples - devc->samp_received) * NUM_CHANNELS;
526 } else {
527 data_left = devc->samplerate * NUM_CHANNELS;
528 }
529
530 data_left += MIN_PACKET_SIZE; /* Driver does not handle small buffers. */
531
532 sr_spew("data_amount %u", data_left);
533
534 return data_left;
535}
536
537static void send_chunk(struct sr_dev_inst *sdi, unsigned char *buf,
538 int num_samples)
539{
540 struct sr_datafeed_packet packet;
541 struct sr_datafeed_analog_old analog;
542 struct dev_context *devc = sdi->priv;
543 int num_channels, data_offset, i;
544
545 const float ch1_bit = RANGE(0) / 255;
546 const float ch2_bit = RANGE(1) / 255;
547 const float ch1_center = RANGE(0) / 2;
548 const float ch2_center = RANGE(1) / 2;
549
550 const gboolean ch1_ena = !!devc->ch_enabled[0];
551 const gboolean ch2_ena = !!devc->ch_enabled[1];
552
553 num_channels = (ch1_ena && ch2_ena) ? 2 : 1;
554 packet.type = SR_DF_ANALOG_OLD;
555 packet.payload = &analog;
556
557 analog.channels = devc->enabled_channels;
558 analog.num_samples = num_samples;
559 analog.mq = SR_MQ_VOLTAGE;
560 analog.unit = SR_UNIT_VOLT;
561 analog.mqflags = 0;
562
563 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_channels);
564 if (!analog.data) {
565 sr_err("Analog data buffer malloc failed.");
566 devc->dev_state = STOPPING;
567 return;
568 }
569
570 data_offset = 0;
571 for (i = 0; i < num_samples; i++) {
572 /*
573 * The device always sends data for both channels. If a channel
574 * is disabled, it contains a copy of the enabled channel's
575 * data. However, we only send the requested channels to
576 * the bus.
577 *
578 * Voltage values are encoded as a value 0-255, where the
579 * value is a point in the range represented by the vdiv
580 * setting. There are 10 vertical divs, so e.g. 500mV/div
581 * represents 5V peak-to-peak where 0 = -2.5V and 255 = +2.5V.
582 */
583 if (ch1_ena)
584 analog.data[data_offset++] = (ch1_bit * *(buf + i * 2) - ch1_center);
585 if (ch2_ena)
586 analog.data[data_offset++] = (ch2_bit * *(buf + i * 2 + 1) - ch2_center);
587 }
588
589 sr_session_send(devc->cb_data, &packet);
590 g_free(analog.data);
591}
592
593static void send_data(struct sr_dev_inst *sdi, struct libusb_transfer *buf[], uint64_t samples)
594{
595 int i = 0;
596 uint64_t send = 0;
597 uint32_t chunk;
598
599 while (send < samples) {
600 chunk = MIN(samples - send, (uint64_t)(buf[i]->actual_length / NUM_CHANNELS));
601 send += chunk;
602 send_chunk(sdi, buf[i]->buffer, chunk);
603
604 /*
605 * Everything in this transfer was either copied to the buffer
606 * or sent to the session bus.
607 */
608 g_free(buf[i]->buffer);
609 libusb_free_transfer(buf[i]);
610 i++;
611 }
612}
613
614/*
615 * Called by libusb (as triggered by handle_event()) when a transfer comes in.
616 * Only channel data comes in asynchronously, and all transfers for this are
617 * queued up beforehand, so this just needs to chuck the incoming data onto
618 * the libsigrok session bus.
619 */
620static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
621{
622 struct sr_dev_inst *sdi;
623 struct dev_context *devc;
624
625 sdi = transfer->user_data;
626 devc = sdi->priv;
627
628 if (devc->dev_state == FLUSH) {
629 devc->dev_state = CAPTURE;
630 devc->aq_started = g_get_monotonic_time();
631 read_channel(sdi, data_amount(sdi));
632 return;
633 }
634
635 if (devc->dev_state != CAPTURE)
636 return;
637
638 if (!devc->sample_buf) {
639 devc->sample_buf_size = 10;
640 devc->sample_buf = g_try_malloc(devc->sample_buf_size * sizeof(transfer));
641 devc->sample_buf_write = 0;
642 }
643
644 if (devc->sample_buf_write >= devc->sample_buf_size) {
645 devc->sample_buf_size += 10;
646 devc->sample_buf = g_try_realloc(devc->sample_buf,
647 devc->sample_buf_size * sizeof(transfer));
648 if (!devc->sample_buf) {
649 sr_err("Sample buffer malloc failed.");
650 devc->dev_state = STOPPING;
651 return;
652 }
6c6bc80a
C
653 }
654
f2a66a8e
C
655 devc->sample_buf[devc->sample_buf_write++] = transfer;
656 devc->samp_received = transfer->actual_length / NUM_CHANNELS;
657
658 sr_spew("receive_transfer(): calculated samplerate == %" PRIu64 "ks/s",
659 (uint64_t)(transfer->actual_length * 1000 /
660 (g_get_monotonic_time() - devc->read_start_ts + 1) /
661 NUM_CHANNELS));
662
663 sr_spew("receive_transfer(): status %s received %d bytes.",
664 libusb_error_name(transfer->status), transfer->actual_length);
665
666 if (transfer->actual_length == 0)
667 /* Nothing to send to the bus. */
668 return;
669
670 if (devc->limit_samples && devc->samp_received >= devc->limit_samples) {
671 sr_info("Requested number of samples reached, stopping. %"
672 PRIu64 " <= %" PRIu64, devc->limit_samples,
673 devc->samp_received);
674 send_data(sdi, devc->sample_buf, devc->limit_samples);
675 sdi->driver->dev_acquisition_stop(sdi, NULL);
676 } else if (devc->limit_msec && (g_get_monotonic_time() -
677 devc->aq_started) / 1000 >= devc->limit_msec) {
678 sr_info("Requested time limit reached, stopping. %d <= %d",
679 (uint32_t)devc->limit_msec,
680 (uint32_t)(g_get_monotonic_time() - devc->aq_started) / 1000);
681 send_data(sdi, devc->sample_buf, devc->samp_received);
682 g_free(devc->sample_buf);
683 devc->sample_buf = NULL;
684 sdi->driver->dev_acquisition_stop(sdi, NULL);
685 } else {
686 read_channel(sdi, data_amount(sdi));
687 }
688}
689
690static int read_channel(const struct sr_dev_inst *sdi, uint32_t amount)
691{
692 int ret;
693 struct dev_context *devc;
694
695 devc = sdi->priv;
696
697 amount = MIN(amount, MAX_PACKET_SIZE);
698 ret = hantek_6xxx_get_channeldata(sdi, receive_transfer, amount);
699 devc->read_start_ts = g_get_monotonic_time();
700 devc->read_data_amount = amount;
701
6c6bc80a
C
702 return ret;
703}
704
f2a66a8e 705static int handle_event(int fd, int revents, void *cb_data)
6c6bc80a 706{
f2a66a8e
C
707 const struct sr_dev_inst *sdi;
708 struct sr_datafeed_packet packet;
709 struct timeval tv;
710 struct sr_dev_driver *di;
711 struct dev_context *devc;
712 struct drv_context *drvc;
713
714 (void)fd;
715 (void)revents;
716
717 sdi = cb_data;
718 di = sdi->driver;
719 drvc = di->context;
720 devc = sdi->priv;
721
722 /* Always handle pending libusb events. */
723 tv.tv_sec = tv.tv_usec = 0;
724 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
725
726 if (devc->dev_state == STOPPING) {
727 /* We've been told to wind up the acquisition. */
728 sr_dbg("Stopping acquisition.");
729
730 hantek_6xxx_stop_data_collecting(sdi);
731 /*
732 * TODO: Doesn't really cancel pending transfers so they might
733 * come in after SR_DF_END is sent.
734 */
735 usb_source_remove(sdi->session, drvc->sr_ctx);
736
737 packet.type = SR_DF_END;
738 packet.payload = NULL;
739 sr_session_send(sdi, &packet);
740
741 devc->dev_state = IDLE;
742
743 return TRUE;
744 }
745
746 return TRUE;
747}
748
749static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
750{
751 struct dev_context *devc;
752 struct sr_dev_driver *di = sdi->driver;
753 struct drv_context *drvc = di->context;
6c6bc80a
C
754
755 if (sdi->status != SR_ST_ACTIVE)
756 return SR_ERR_DEV_CLOSED;
757
f2a66a8e
C
758 devc = sdi->priv;
759 devc->cb_data = cb_data;
760
761 if (configure_channels(sdi) != SR_OK) {
762 sr_err("Failed to configure channels.");
763 return SR_ERR;
764 }
765
766 if (hantek_6xxx_init(sdi) != SR_OK)
767 return SR_ERR;
768
769 /* Send header packet to the session bus. */
770 std_session_send_df_header(cb_data, LOG_PREFIX);
771
772 devc->samp_received = 0;
773 devc->dev_state = FLUSH;
774
775 usb_source_add(sdi->session, drvc->sr_ctx, TICK,
776 handle_event, (void *)sdi);
777
778 hantek_6xxx_start_data_collecting(sdi);
779
780 read_channel(sdi, FLUSH_PACKET_SIZE);
6c6bc80a
C
781
782 return SR_OK;
783}
784
785static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
786{
f2a66a8e
C
787 struct dev_context *devc;
788
6c6bc80a
C
789 (void)cb_data;
790
791 if (sdi->status != SR_ST_ACTIVE)
f2a66a8e
C
792 return SR_ERR;
793
794 devc = sdi->priv;
795 devc->dev_state = STOPPING;
6c6bc80a 796
f2a66a8e 797 g_free(devc->sample_buf); devc->sample_buf = NULL;
6c6bc80a
C
798
799 return SR_OK;
800}
801
802SR_PRIV struct sr_dev_driver hantek_6xxx_driver_info = {
803 .name = "hantek-6xxx",
804 .longname = "Hantek 6xxx",
805 .api_version = 1,
806 .init = init,
807 .cleanup = cleanup,
808 .scan = scan,
809 .dev_list = dev_list,
810 .dev_clear = dev_clear,
811 .config_get = config_get,
812 .config_set = config_set,
813 .config_list = config_list,
814 .dev_open = dev_open,
815 .dev_close = dev_close,
816 .dev_acquisition_start = dev_acquisition_start,
817 .dev_acquisition_stop = dev_acquisition_stop,
818 .context = NULL,
819};