2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 #include <sys/types.h>
30 #include <arpa/inet.h>
34 #include "sigrok-internal.h"
39 /* Max time in ms before we want to check on events */
42 static int capabilities[] = {
43 SR_HWCAP_OSCILLOSCOPE,
44 SR_HWCAP_LIMIT_SAMPLES,
48 SR_HWCAP_TRIGGER_SOURCE,
49 SR_HWCAP_TRIGGER_SLOPE,
50 SR_HWCAP_HORIZ_TRIGGERPOS,
55 static const char *probe_names[] = {
61 static struct dso_profile dev_profiles[] = {
66 FIRMWARE_DIR "/hantek-dso-2090.fw" },
67 { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
70 static uint64_t buffersizes[] = {
77 static struct sr_rational timebases[] = {
98 static char *trigger_sources[] = {
105 static char *filter_targets[] = {
108 /* TODO: "TRIGGER", */
112 SR_PRIV libusb_context *usb_context = NULL;
113 SR_PRIV GSList *dev_insts = NULL;
116 static struct sr_dev_inst *dso_dev_new(int index, struct dso_profile *prof)
118 struct sr_dev_inst *sdi;
121 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
122 prof->vendor, prof->model, prof->model_version);
126 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
127 sr_err("hantek-dso: ctx malloc failed");
131 ctx->dev_state = IDLE;
132 ctx->timebase = DEFAULT_TIMEBASE;
133 ctx->ch1_enabled = TRUE;
134 ctx->ch2_enabled = TRUE;
135 ctx->voltage_ch1 = DEFAULT_VOLTAGE;
136 ctx->voltage_ch2 = DEFAULT_VOLTAGE;
137 ctx->coupling_ch1 = DEFAULT_COUPLING;
138 ctx->coupling_ch2 = DEFAULT_COUPLING;
139 ctx->voffset_ch1 = DEFAULT_VERT_OFFSET;
140 ctx->voffset_ch2 = DEFAULT_VERT_OFFSET;
141 ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
142 ctx->framesize = DEFAULT_FRAMESIZE;
143 ctx->triggerslope = SLOPE_POSITIVE;
144 ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
145 ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
147 dev_insts = g_slist_append(dev_insts, sdi);
152 static int configure_probes(struct context *ctx, GSList *probes)
154 struct sr_probe *probe;
157 ctx->ch1_enabled = ctx->ch2_enabled = FALSE;
158 for (l = probes; l; l = l->next) {
159 probe = (struct sr_probe *)l->data;
160 if (probe->index == 1)
161 ctx->ch1_enabled = probe->enabled;
162 else if (probe->index == 2)
163 ctx->ch2_enabled = probe->enabled;
169 static int hw_init(const char *devinfo)
171 struct sr_dev_inst *sdi;
172 struct libusb_device_descriptor des;
173 struct dso_profile *prof;
175 libusb_device **devlist;
176 int err, devcnt, i, j;
178 /* Avoid compiler warnings. */
181 if (libusb_init(&usb_context) != 0) {
182 sr_err("hantek-dso: Failed to initialize USB.");
186 /* Find all Hantek DSO devices and upload firmware to all of them. */
188 libusb_get_device_list(usb_context, &devlist);
189 for (i = 0; devlist[i]; i++) {
190 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
191 sr_err("hantek-dso: failed to get device descriptor: %d", err);
196 for (j = 0; dev_profiles[j].orig_vid; j++) {
197 if (des.idVendor == dev_profiles[j].orig_vid
198 && des.idProduct == dev_profiles[j].orig_pid) {
199 /* Device matches the pre-firmware profile. */
200 prof = &dev_profiles[j];
201 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
202 sdi = dso_dev_new(devcnt, prof);
204 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
205 prof->firmware) == SR_OK)
206 /* Remember when the firmware on this device was updated */
207 g_get_current_time(&ctx->fw_updated);
209 sr_err("hantek-dso: firmware upload failed for "
210 "device %d", devcnt);
211 /* Dummy USB address of 0xff will get overwritten later. */
212 ctx->usb = sr_usb_dev_inst_new(
213 libusb_get_bus_number(devlist[i]), 0xff, NULL);
216 } else if (des.idVendor == dev_profiles[j].fw_vid
217 && des.idProduct == dev_profiles[j].fw_pid) {
218 /* Device matches the post-firmware profile. */
219 prof = &dev_profiles[j];
220 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
221 sdi = dso_dev_new(devcnt, prof);
222 sdi->status = SR_ST_INACTIVE;
224 ctx->usb = sr_usb_dev_inst_new(
225 libusb_get_bus_number(devlist[i]),
226 libusb_get_device_address(devlist[i]), NULL);
232 /* not a supported VID/PID */
235 libusb_free_device_list(devlist, 1);
240 static int hw_dev_open(int dev_index)
243 struct sr_dev_inst *sdi;
247 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
252 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
253 * for the FX2 to renumerate
256 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
257 sr_info("hantek-dso: waiting for device to reset");
258 /* takes at least 300ms for the FX2 to be gone from the USB bus */
259 g_usleep(300 * 1000);
261 while (timediff < MAX_RENUM_DELAY) {
262 if ((err = dso_open(dev_index)) == SR_OK)
264 g_usleep(100 * 1000);
265 g_get_current_time(&cur_time);
266 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
268 sr_info("hantek-dso: device came back after %d ms", timediff);
270 err = dso_open(dev_index);
274 sr_err("hantek-dso: unable to open device");
278 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
280 sr_err("hantek-dso: Unable to claim interface: %d", err);
287 static int hw_dev_close(int dev_index)
289 struct sr_dev_inst *sdi;
291 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
299 static int hw_cleanup(void)
302 struct sr_dev_inst *sdi;
305 /* Properly close and free all devices. */
306 for (l = dev_insts; l; l = l->next) {
307 if (!(sdi = l->data)) {
308 /* Log error, but continue cleaning up the rest. */
309 sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
312 if (!(ctx = sdi->priv)) {
313 /* Log error, but continue cleaning up the rest. */
314 sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
318 sr_usb_dev_inst_free(ctx->usb);
319 g_free(ctx->triggersource);
321 sr_dev_inst_free(sdi);
324 g_slist_free(dev_insts);
328 libusb_exit(usb_context);
334 static void *hw_get_device_info(int dev_index, int dev_info_id)
336 struct sr_dev_inst *sdi;
341 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
346 switch (dev_info_id) {
350 case SR_DI_NUM_PROBES:
351 info = GINT_TO_POINTER(ctx->profile->num_probes);
353 case SR_DI_PROBE_NAMES:
356 case SR_DI_BUFFERSIZES:
359 case SR_DI_TIMEBASES:
362 case SR_DI_TRIGGER_SOURCES:
363 info = trigger_sources;
366 info = filter_targets;
368 /* TODO remove this */
369 case SR_DI_CUR_SAMPLERATE:
377 static int hw_get_status(int device_index)
379 struct sr_dev_inst *sdi;
381 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
382 return SR_ST_NOT_FOUND;
387 static int *hwcap_get_all(void)
393 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
395 struct sr_dev_inst *sdi;
397 struct sr_rational tmp_rat;
401 char *tmp_str, **targets;
403 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
406 if (sdi->status != SR_ST_ACTIVE)
412 case SR_HWCAP_LIMIT_FRAMES:
413 ctx->limit_frames = *(uint64_t *)value;
415 case SR_HWCAP_PROBECONFIG:
416 ret = configure_probes(ctx, (GSList *) value);
418 case SR_HWCAP_TRIGGER_SLOPE:
419 tmp_u64 = *(int *)value;
420 if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE)
422 ctx->triggerslope = tmp_u64;
424 case SR_HWCAP_HORIZ_TRIGGERPOS:
425 tmp_float = *(float *)value;
426 if (tmp_float < 0.0 || tmp_float > 1.0) {
427 sr_err("hantek-dso: trigger position should be between 0.0 and 1.0");
430 ctx->triggerposition = tmp_float;
432 case SR_HWCAP_BUFFERSIZE:
433 tmp_u64 = *(int *)value;
434 for (i = 0; buffersizes[i]; i++) {
435 if (buffersizes[i] == tmp_u64) {
436 ctx->framesize = tmp_u64;
440 if (buffersizes[i] == 0)
443 case SR_HWCAP_TIMEBASE:
444 tmp_rat = *(struct sr_rational *)value;
445 for (i = 0; timebases[i].p && timebases[i].q; i++) {
446 if (timebases[i].p == tmp_rat.p
447 && timebases[i].q == tmp_rat.q) {
452 if (timebases[i].p == 0 && timebases[i].q == 0)
455 case SR_HWCAP_TRIGGER_SOURCE:
457 for (i = 0; trigger_sources[i]; i++) {
458 if (!strcmp(tmp_str, trigger_sources[i])) {
459 ctx->triggersource = g_strdup(tmp_str);
463 if (trigger_sources[i] == 0)
466 case SR_HWCAP_FILTER:
467 ctx->filter_ch1 = ctx->filter_ch2 = ctx->filter_trigger = 0;
468 targets = g_strsplit(value, ",", 0);
469 for (i = 0; targets[i]; i++) {
470 if (targets[i] == '\0')
471 /* Empty filter string can be used to clear them all. */
473 else if (!strcmp(targets[i], "CH1"))
474 ctx->filter_ch1 = TRUE;
475 else if (!strcmp(targets[i], "CH2"))
476 ctx->filter_ch2 = TRUE;
477 else if (!strcmp(targets[i], "TRIGGER"))
478 ctx->filter_trigger = TRUE;
480 sr_err("invalid filter target %s", targets[i]);
493 /* Called by libusb (as triggered by handle_event()) when a transfer comes in.
494 * Only channel data comes in asynchronously, and all transfers for this are
495 * queued up beforehand, so this just needs so chuck the incoming data onto
496 * the libsigrok session bus.
498 static void receive_transfer(struct libusb_transfer *transfer)
500 struct sr_datafeed_packet packet;
501 struct sr_datafeed_analog analog;
504 int num_probes, data_offset, i;
506 ctx = transfer->user_data;
507 sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes",
508 transfer->status, transfer->actual_length);
510 if (transfer->actual_length == 0)
511 /* Nothing to send to the bus. */
514 ctx->current_transfer += transfer->actual_length;
515 sr_dbg("hantek-dso: got %d of %d in frame", ctx->current_transfer, ctx->framesize * 2);
517 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
518 packet.type = SR_DF_ANALOG;
519 packet.payload = &analog;
520 /* TODO: support for 5xxx series 9-bit samples */
521 analog.num_samples = transfer->actual_length / 2;
522 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
524 for (i = 0; i < analog.num_samples; i++) {
525 /* The device always sends data for both channels. If a channel
526 * is disabled, it contains a copy of the enabled channel's
527 * data. However, we only send the requested channels to the bus.
529 /* TODO: support for 5xxx series 9-bit samples */
530 if (ctx->ch1_enabled) {
531 ch1 = (*(transfer->buffer + i * 2 + 1) / 255.0);
532 analog.data[data_offset++] = ch1;
534 if (ctx->ch2_enabled) {
535 ch2 = (*(transfer->buffer + i * 2) / 255.0);
536 analog.data[data_offset++] = ch2;
539 g_free(transfer->buffer);
540 libusb_free_transfer(transfer);
541 sr_session_send(ctx->cb_data, &packet);
543 if (ctx->current_transfer >= ctx->framesize * 2) {
544 /* That's the last chunk in this frame. */
545 packet.type = SR_DF_FRAME_END;
546 sr_session_send(ctx->cb_data, &packet);
548 if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) {
549 /* Terminate session */
550 /* TODO: don't leave pending USB transfers hanging */
551 packet.type = SR_DF_END;
552 sr_session_send(ctx->cb_data, &packet);
554 ctx->current_transfer = 0;
555 ctx->dev_state = NEW_CAPTURE;
561 static int handle_event(int fd, int revents, void *cb_data)
563 struct sr_datafeed_packet packet;
568 /* Avoid compiler warnings. */
572 /* Always handle pending libusb events. */
573 tv.tv_sec = tv.tv_usec = 0;
574 libusb_handle_events_timeout(usb_context, &tv);
578 if (ctx->dev_state == NEW_CAPTURE) {
579 if (dso_capture_start(ctx) != SR_OK)
581 if (dso_enable_trigger(ctx) != SR_OK)
583 // if (dso_force_trigger(ctx) != SR_OK)
585 sr_dbg("hantek-dso: successfully requested next chunk");
586 ctx->dev_state = CAPTURE;
589 if (ctx->dev_state != CAPTURE)
592 if ((capturestate = dso_get_capturestate(ctx)) == CAPTURE_UNKNOWN) {
593 /* Generated by the function, not the hardware. */
597 sr_dbg("hantek-dso: capturestate %d", capturestate);
598 switch (capturestate) {
600 if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) {
601 ctx->capture_empty_count = 0;
602 if (dso_capture_start(ctx) != SR_OK)
604 if (dso_enable_trigger(ctx) != SR_OK)
606 // if (dso_force_trigger(ctx) != SR_OK)
608 sr_dbg("hantek-dso: successfully requested next chunk");
611 case CAPTURE_FILLING:
614 case CAPTURE_READY_8BIT:
615 /* Tell the scope to send us the first frame. */
616 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
619 /* Don't hit the state machine again until we're done fetching
620 * the data we just told the scope to send.
622 ctx->dev_state = FETCH_DATA;
624 /* Tell the frontend a new frame is on the way. */
625 packet.type = SR_DF_FRAME_BEGIN;
626 sr_session_send(cb_data, &packet);
628 case CAPTURE_READY_9BIT:
630 sr_err("not yet supported");
632 case CAPTURE_TIMEOUT:
633 /* Doesn't matter, we'll try again next time. */
636 sr_dbg("unknown capture state");
642 static int hw_start_acquisition(int device_index, void *cb_data)
644 const struct libusb_pollfd **lupfd;
645 struct sr_datafeed_packet packet;
646 struct sr_datafeed_header header;
647 struct sr_datafeed_meta_analog meta;
648 struct sr_dev_inst *sdi;
652 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
655 if (sdi->status != SR_ST_ACTIVE)
659 ctx->cb_data = cb_data;
661 if (dso_init(ctx) != SR_OK)
664 if (dso_capture_start(ctx) != SR_OK)
667 ctx->dev_state = CAPTURE;
668 lupfd = libusb_get_pollfds(usb_context);
669 for (i = 0; lupfd[i]; i++)
670 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event,
674 /* Send header packet to the session bus. */
675 packet.type = SR_DF_HEADER;
676 packet.payload = (unsigned char *)&header;
677 header.feed_version = 1;
678 gettimeofday(&header.starttime, NULL);
679 sr_session_send(cb_data, &packet);
681 /* Send metadata about the SR_DF_ANALOG packets to come. */
682 packet.type = SR_DF_META_ANALOG;
683 packet.payload = &meta;
684 meta.num_probes = ctx->profile->num_probes;
685 sr_session_send(cb_data, &packet);
690 /* TODO: doesn't really cancel pending transfers so they might come in after
693 static int hw_stop_acquisition(int device_index, gpointer session_device_id)
695 struct sr_datafeed_packet packet;
696 struct sr_dev_inst *sdi;
699 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
702 if (sdi->status != SR_ST_ACTIVE)
706 ctx->dev_state = IDLE;
708 packet.type = SR_DF_END;
709 sr_session_send(session_device_id, &packet);
714 SR_PRIV struct sr_dev_driver hantek_dso_plugin_info = {
715 .name = "hantek-dso",
716 .longname = "Hantek DSO",
719 .cleanup = hw_cleanup,
720 .dev_open = hw_dev_open,
721 .dev_close = hw_dev_close,
722 .dev_info_get = hw_get_device_info,
723 .dev_status_get = hw_get_status,
724 .hwcap_get_all = hwcap_get_all,
725 .dev_config_set = hw_dev_config_set,
726 .dev_acquisition_start = hw_start_acquisition,
727 .dev_acquisition_stop = hw_stop_acquisition,