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