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,
56 static const char *probe_names[] = {
62 static struct dso_profile dev_profiles[] = {
67 FIRMWARE_DIR "/hantek-dso-2090.fw" },
68 { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
71 static uint64_t buffersizes[] = {
78 static struct sr_rational timebases[] = {
99 static struct sr_rational vdivs[] = {
114 static char *trigger_sources[] = {
121 static char *filter_targets[] = {
124 /* TODO: "TRIGGER", */
128 static char *coupling[] = {
135 SR_PRIV libusb_context *usb_context = NULL;
136 SR_PRIV GSList *dev_insts = NULL;
139 static struct sr_dev_inst *dso_dev_new(int index, struct dso_profile *prof)
141 struct sr_dev_inst *sdi;
144 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
145 prof->vendor, prof->model, prof->model_version);
149 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
150 sr_err("hantek-dso: ctx malloc failed");
154 ctx->dev_state = IDLE;
155 ctx->timebase = DEFAULT_TIMEBASE;
156 ctx->ch1_enabled = TRUE;
157 ctx->ch2_enabled = TRUE;
158 ctx->voltage_ch1 = DEFAULT_VOLTAGE;
159 ctx->voltage_ch2 = DEFAULT_VOLTAGE;
160 ctx->coupling_ch1 = DEFAULT_COUPLING;
161 ctx->coupling_ch2 = DEFAULT_COUPLING;
162 ctx->voffset_ch1 = DEFAULT_VERT_OFFSET;
163 ctx->voffset_ch2 = DEFAULT_VERT_OFFSET;
164 ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
165 ctx->framesize = DEFAULT_FRAMESIZE;
166 ctx->triggerslope = SLOPE_POSITIVE;
167 ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
168 ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
170 dev_insts = g_slist_append(dev_insts, sdi);
175 static int configure_probes(struct context *ctx, GSList *probes)
177 struct sr_probe *probe;
180 ctx->ch1_enabled = ctx->ch2_enabled = FALSE;
181 for (l = probes; l; l = l->next) {
182 probe = (struct sr_probe *)l->data;
183 if (probe->index == 1)
184 ctx->ch1_enabled = probe->enabled;
185 else if (probe->index == 2)
186 ctx->ch2_enabled = probe->enabled;
192 static int hw_init(const char *devinfo)
194 struct sr_dev_inst *sdi;
195 struct libusb_device_descriptor des;
196 struct dso_profile *prof;
198 libusb_device **devlist;
199 int err, devcnt, i, j;
201 /* Avoid compiler warnings. */
204 if (libusb_init(&usb_context) != 0) {
205 sr_err("hantek-dso: Failed to initialize USB.");
209 /* Find all Hantek DSO devices and upload firmware to all of them. */
211 libusb_get_device_list(usb_context, &devlist);
212 for (i = 0; devlist[i]; i++) {
213 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
214 sr_err("hantek-dso: failed to get device descriptor: %d", err);
219 for (j = 0; dev_profiles[j].orig_vid; j++) {
220 if (des.idVendor == dev_profiles[j].orig_vid
221 && des.idProduct == dev_profiles[j].orig_pid) {
222 /* Device matches the pre-firmware profile. */
223 prof = &dev_profiles[j];
224 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
225 sdi = dso_dev_new(devcnt, prof);
227 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
228 prof->firmware) == SR_OK)
229 /* Remember when the firmware on this device was updated */
230 g_get_current_time(&ctx->fw_updated);
232 sr_err("hantek-dso: firmware upload failed for "
233 "device %d", devcnt);
234 /* Dummy USB address of 0xff will get overwritten later. */
235 ctx->usb = sr_usb_dev_inst_new(
236 libusb_get_bus_number(devlist[i]), 0xff, NULL);
239 } else if (des.idVendor == dev_profiles[j].fw_vid
240 && des.idProduct == dev_profiles[j].fw_pid) {
241 /* Device matches the post-firmware profile. */
242 prof = &dev_profiles[j];
243 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
244 sdi = dso_dev_new(devcnt, prof);
245 sdi->status = SR_ST_INACTIVE;
247 ctx->usb = sr_usb_dev_inst_new(
248 libusb_get_bus_number(devlist[i]),
249 libusb_get_device_address(devlist[i]), NULL);
255 /* not a supported VID/PID */
258 libusb_free_device_list(devlist, 1);
263 static int hw_dev_open(int dev_index)
266 struct sr_dev_inst *sdi;
270 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
275 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
276 * for the FX2 to renumerate
279 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
280 sr_info("hantek-dso: waiting for device to reset");
281 /* takes at least 300ms for the FX2 to be gone from the USB bus */
282 g_usleep(300 * 1000);
284 while (timediff < MAX_RENUM_DELAY) {
285 if ((err = dso_open(dev_index)) == SR_OK)
287 g_usleep(100 * 1000);
288 g_get_current_time(&cur_time);
289 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
291 sr_info("hantek-dso: device came back after %d ms", timediff);
293 err = dso_open(dev_index);
297 sr_err("hantek-dso: unable to open device");
301 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
303 sr_err("hantek-dso: Unable to claim interface: %d", err);
310 static int hw_dev_close(int dev_index)
312 struct sr_dev_inst *sdi;
314 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
322 static int hw_cleanup(void)
325 struct sr_dev_inst *sdi;
328 /* Properly close and free all devices. */
329 for (l = dev_insts; l; l = l->next) {
330 if (!(sdi = l->data)) {
331 /* Log error, but continue cleaning up the rest. */
332 sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
335 if (!(ctx = sdi->priv)) {
336 /* Log error, but continue cleaning up the rest. */
337 sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
341 sr_usb_dev_inst_free(ctx->usb);
342 g_free(ctx->triggersource);
344 sr_dev_inst_free(sdi);
347 g_slist_free(dev_insts);
351 libusb_exit(usb_context);
357 static void *hw_get_device_info(int dev_index, int dev_info_id)
359 struct sr_dev_inst *sdi;
364 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
369 switch (dev_info_id) {
373 case SR_DI_NUM_PROBES:
374 info = GINT_TO_POINTER(ctx->profile->num_probes);
376 case SR_DI_PROBE_NAMES:
379 case SR_DI_BUFFERSIZES:
382 case SR_DI_TIMEBASES:
385 case SR_DI_TRIGGER_SOURCES:
386 info = trigger_sources;
389 info = filter_targets;
394 /* TODO remove this */
395 case SR_DI_CUR_SAMPLERATE:
403 static int hw_get_status(int device_index)
405 struct sr_dev_inst *sdi;
407 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
408 return SR_ST_NOT_FOUND;
413 static int *hwcap_get_all(void)
419 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
421 struct sr_dev_inst *sdi;
423 struct sr_rational tmp_rat;
427 char *tmp_str, **targets;
429 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
432 if (sdi->status != SR_ST_ACTIVE)
438 case SR_HWCAP_LIMIT_FRAMES:
439 ctx->limit_frames = *(uint64_t *)value;
441 case SR_HWCAP_PROBECONFIG:
442 ret = configure_probes(ctx, (GSList *) value);
444 case SR_HWCAP_TRIGGER_SLOPE:
445 tmp_u64 = *(int *)value;
446 if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE)
448 ctx->triggerslope = tmp_u64;
450 case SR_HWCAP_HORIZ_TRIGGERPOS:
451 tmp_float = *(float *)value;
452 if (tmp_float < 0.0 || tmp_float > 1.0) {
453 sr_err("hantek-dso: trigger position should be between 0.0 and 1.0");
456 ctx->triggerposition = tmp_float;
458 case SR_HWCAP_BUFFERSIZE:
459 tmp_u64 = *(int *)value;
460 for (i = 0; buffersizes[i]; i++) {
461 if (buffersizes[i] == tmp_u64) {
462 ctx->framesize = tmp_u64;
466 if (buffersizes[i] == 0)
469 case SR_HWCAP_TIMEBASE:
470 tmp_rat = *(struct sr_rational *)value;
471 for (i = 0; timebases[i].p && timebases[i].q; i++) {
472 if (timebases[i].p == tmp_rat.p
473 && timebases[i].q == tmp_rat.q) {
478 if (timebases[i].p == 0 && timebases[i].q == 0)
481 case SR_HWCAP_TRIGGER_SOURCE:
483 for (i = 0; trigger_sources[i]; i++) {
484 if (!strcmp(tmp_str, trigger_sources[i])) {
485 ctx->triggersource = g_strdup(tmp_str);
489 if (trigger_sources[i] == 0)
492 case SR_HWCAP_FILTER:
493 ctx->filter_ch1 = ctx->filter_ch2 = ctx->filter_trigger = 0;
494 targets = g_strsplit(value, ",", 0);
495 for (i = 0; targets[i]; i++) {
496 if (targets[i] == '\0')
497 /* Empty filter string can be used to clear them all. */
499 else if (!strcmp(targets[i], "CH1"))
500 ctx->filter_ch1 = TRUE;
501 else if (!strcmp(targets[i], "CH2"))
502 ctx->filter_ch2 = TRUE;
503 else if (!strcmp(targets[i], "TRIGGER"))
504 ctx->filter_trigger = TRUE;
506 sr_err("invalid filter target %s", targets[i]);
513 /* TODO not supporting vdiv per channel yet */
514 tmp_rat = *(struct sr_rational *)value;
515 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
516 if (vdivs[i].p == tmp_rat.p
517 && vdivs[i].q == tmp_rat.q) {
518 ctx->voltage_ch1 = i;
519 ctx->voltage_ch2 = i;
523 if (vdivs[i].p == 0 && vdivs[i].q == 0)
526 case SR_HWCAP_COUPLING:
527 /* TODO not supporting coupling per channel yet */
529 for (i = 0; coupling[i]; i++) {
530 if (!strcmp(tmp_str, coupling[i])) {
531 ctx->coupling_ch1 = i;
532 ctx->coupling_ch2 = i;
536 if (coupling[i] == 0)
546 /* Called by libusb (as triggered by handle_event()) when a transfer comes in.
547 * Only channel data comes in asynchronously, and all transfers for this are
548 * queued up beforehand, so this just needs so chuck the incoming data onto
549 * the libsigrok session bus.
551 static void receive_transfer(struct libusb_transfer *transfer)
553 struct sr_datafeed_packet packet;
554 struct sr_datafeed_analog analog;
557 int num_probes, data_offset, i;
559 ctx = transfer->user_data;
560 sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes",
561 transfer->status, transfer->actual_length);
563 if (transfer->actual_length == 0)
564 /* Nothing to send to the bus. */
567 ctx->current_transfer += transfer->actual_length;
568 sr_dbg("hantek-dso: got %d of %d in frame", ctx->current_transfer, ctx->framesize * 2);
570 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
571 packet.type = SR_DF_ANALOG;
572 packet.payload = &analog;
573 /* TODO: support for 5xxx series 9-bit samples */
574 analog.num_samples = transfer->actual_length / 2;
575 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
577 for (i = 0; i < analog.num_samples; i++) {
578 /* The device always sends data for both channels. If a channel
579 * is disabled, it contains a copy of the enabled channel's
580 * data. However, we only send the requested channels to the bus.
582 /* TODO: support for 5xxx series 9-bit samples */
583 if (ctx->ch1_enabled) {
584 ch1 = (*(transfer->buffer + i * 2 + 1) / 255.0);
585 analog.data[data_offset++] = ch1;
587 if (ctx->ch2_enabled) {
588 ch2 = (*(transfer->buffer + i * 2) / 255.0);
589 analog.data[data_offset++] = ch2;
592 g_free(transfer->buffer);
593 libusb_free_transfer(transfer);
594 sr_session_send(ctx->cb_data, &packet);
596 if (ctx->current_transfer >= ctx->framesize * 2) {
597 /* That's the last chunk in this frame. */
598 packet.type = SR_DF_FRAME_END;
599 sr_session_send(ctx->cb_data, &packet);
601 if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) {
602 /* Terminate session */
603 /* TODO: don't leave pending USB transfers hanging */
604 packet.type = SR_DF_END;
605 sr_session_send(ctx->cb_data, &packet);
607 ctx->current_transfer = 0;
608 ctx->dev_state = NEW_CAPTURE;
614 static int handle_event(int fd, int revents, void *cb_data)
616 struct sr_datafeed_packet packet;
621 /* Avoid compiler warnings. */
625 /* Always handle pending libusb events. */
626 tv.tv_sec = tv.tv_usec = 0;
627 libusb_handle_events_timeout(usb_context, &tv);
631 if (ctx->dev_state == NEW_CAPTURE) {
632 if (dso_capture_start(ctx) != SR_OK)
634 if (dso_enable_trigger(ctx) != SR_OK)
636 // if (dso_force_trigger(ctx) != SR_OK)
638 sr_dbg("hantek-dso: successfully requested next chunk");
639 ctx->dev_state = CAPTURE;
642 if (ctx->dev_state != CAPTURE)
645 if ((capturestate = dso_get_capturestate(ctx)) == CAPTURE_UNKNOWN) {
646 /* Generated by the function, not the hardware. */
650 sr_dbg("hantek-dso: capturestate %d", capturestate);
651 switch (capturestate) {
653 if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) {
654 ctx->capture_empty_count = 0;
655 if (dso_capture_start(ctx) != SR_OK)
657 if (dso_enable_trigger(ctx) != SR_OK)
659 // if (dso_force_trigger(ctx) != SR_OK)
661 sr_dbg("hantek-dso: successfully requested next chunk");
664 case CAPTURE_FILLING:
667 case CAPTURE_READY_8BIT:
668 /* Tell the scope to send us the first frame. */
669 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
672 /* Don't hit the state machine again until we're done fetching
673 * the data we just told the scope to send.
675 ctx->dev_state = FETCH_DATA;
677 /* Tell the frontend a new frame is on the way. */
678 packet.type = SR_DF_FRAME_BEGIN;
679 sr_session_send(cb_data, &packet);
681 case CAPTURE_READY_9BIT:
683 sr_err("not yet supported");
685 case CAPTURE_TIMEOUT:
686 /* Doesn't matter, we'll try again next time. */
689 sr_dbg("unknown capture state");
695 static int hw_start_acquisition(int device_index, void *cb_data)
697 const struct libusb_pollfd **lupfd;
698 struct sr_datafeed_packet packet;
699 struct sr_datafeed_header header;
700 struct sr_datafeed_meta_analog meta;
701 struct sr_dev_inst *sdi;
705 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
708 if (sdi->status != SR_ST_ACTIVE)
712 ctx->cb_data = cb_data;
714 if (dso_init(ctx) != SR_OK)
717 if (dso_capture_start(ctx) != SR_OK)
720 ctx->dev_state = CAPTURE;
721 lupfd = libusb_get_pollfds(usb_context);
722 for (i = 0; lupfd[i]; i++)
723 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event,
727 /* Send header packet to the session bus. */
728 packet.type = SR_DF_HEADER;
729 packet.payload = (unsigned char *)&header;
730 header.feed_version = 1;
731 gettimeofday(&header.starttime, NULL);
732 sr_session_send(cb_data, &packet);
734 /* Send metadata about the SR_DF_ANALOG packets to come. */
735 packet.type = SR_DF_META_ANALOG;
736 packet.payload = &meta;
737 meta.num_probes = ctx->profile->num_probes;
738 sr_session_send(cb_data, &packet);
743 /* TODO: doesn't really cancel pending transfers so they might come in after
746 static int hw_stop_acquisition(int device_index, gpointer session_device_id)
748 struct sr_datafeed_packet packet;
749 struct sr_dev_inst *sdi;
752 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
755 if (sdi->status != SR_ST_ACTIVE)
759 ctx->dev_state = IDLE;
761 packet.type = SR_DF_END;
762 sr_session_send(session_device_id, &packet);
767 SR_PRIV struct sr_dev_driver hantek_dso_plugin_info = {
768 .name = "hantek-dso",
769 .longname = "Hantek DSO",
772 .cleanup = hw_cleanup,
773 .dev_open = hw_dev_open,
774 .dev_close = hw_dev_close,
775 .dev_info_get = hw_get_device_info,
776 .dev_status_get = hw_get_status,
777 .hwcap_get_all = hwcap_get_all,
778 .dev_config_set = hw_dev_config_set,
779 .dev_acquisition_start = hw_start_acquisition,
780 .dev_acquisition_stop = hw_stop_acquisition,