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 SR_PRIV libusb_context *usb_context = NULL;
129 SR_PRIV GSList *dev_insts = NULL;
132 static struct sr_dev_inst *dso_dev_new(int index, struct dso_profile *prof)
134 struct sr_dev_inst *sdi;
137 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
138 prof->vendor, prof->model, prof->model_version);
142 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
143 sr_err("hantek-dso: ctx malloc failed");
147 ctx->dev_state = IDLE;
148 ctx->timebase = DEFAULT_TIMEBASE;
149 ctx->ch1_enabled = TRUE;
150 ctx->ch2_enabled = TRUE;
151 ctx->voltage_ch1 = DEFAULT_VOLTAGE;
152 ctx->voltage_ch2 = DEFAULT_VOLTAGE;
153 ctx->coupling_ch1 = DEFAULT_COUPLING;
154 ctx->coupling_ch2 = DEFAULT_COUPLING;
155 ctx->voffset_ch1 = DEFAULT_VERT_OFFSET;
156 ctx->voffset_ch2 = DEFAULT_VERT_OFFSET;
157 ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
158 ctx->framesize = DEFAULT_FRAMESIZE;
159 ctx->triggerslope = SLOPE_POSITIVE;
160 ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
161 ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
163 dev_insts = g_slist_append(dev_insts, sdi);
168 static int configure_probes(struct context *ctx, GSList *probes)
170 struct sr_probe *probe;
173 ctx->ch1_enabled = ctx->ch2_enabled = FALSE;
174 for (l = probes; l; l = l->next) {
175 probe = (struct sr_probe *)l->data;
176 if (probe->index == 1)
177 ctx->ch1_enabled = probe->enabled;
178 else if (probe->index == 2)
179 ctx->ch2_enabled = probe->enabled;
185 static int hw_init(const char *devinfo)
187 struct sr_dev_inst *sdi;
188 struct libusb_device_descriptor des;
189 struct dso_profile *prof;
191 libusb_device **devlist;
192 int err, devcnt, i, j;
194 /* Avoid compiler warnings. */
197 if (libusb_init(&usb_context) != 0) {
198 sr_err("hantek-dso: Failed to initialize USB.");
202 /* Find all Hantek DSO devices and upload firmware to all of them. */
204 libusb_get_device_list(usb_context, &devlist);
205 for (i = 0; devlist[i]; i++) {
206 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
207 sr_err("hantek-dso: failed to get device descriptor: %d", err);
212 for (j = 0; dev_profiles[j].orig_vid; j++) {
213 if (des.idVendor == dev_profiles[j].orig_vid
214 && des.idProduct == dev_profiles[j].orig_pid) {
215 /* Device matches the pre-firmware profile. */
216 prof = &dev_profiles[j];
217 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
218 sdi = dso_dev_new(devcnt, prof);
220 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
221 prof->firmware) == SR_OK)
222 /* Remember when the firmware on this device was updated */
223 g_get_current_time(&ctx->fw_updated);
225 sr_err("hantek-dso: firmware upload failed for "
226 "device %d", devcnt);
227 /* Dummy USB address of 0xff will get overwritten later. */
228 ctx->usb = sr_usb_dev_inst_new(
229 libusb_get_bus_number(devlist[i]), 0xff, NULL);
232 } else if (des.idVendor == dev_profiles[j].fw_vid
233 && des.idProduct == dev_profiles[j].fw_pid) {
234 /* Device matches the post-firmware profile. */
235 prof = &dev_profiles[j];
236 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
237 sdi = dso_dev_new(devcnt, prof);
238 sdi->status = SR_ST_INACTIVE;
240 ctx->usb = sr_usb_dev_inst_new(
241 libusb_get_bus_number(devlist[i]),
242 libusb_get_device_address(devlist[i]), NULL);
248 /* not a supported VID/PID */
251 libusb_free_device_list(devlist, 1);
256 static int hw_dev_open(int dev_index)
259 struct sr_dev_inst *sdi;
263 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
268 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
269 * for the FX2 to renumerate
272 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
273 sr_info("hantek-dso: waiting for device to reset");
274 /* takes at least 300ms for the FX2 to be gone from the USB bus */
275 g_usleep(300 * 1000);
277 while (timediff < MAX_RENUM_DELAY) {
278 if ((err = dso_open(dev_index)) == SR_OK)
280 g_usleep(100 * 1000);
281 g_get_current_time(&cur_time);
282 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
284 sr_info("hantek-dso: device came back after %d ms", timediff);
286 err = dso_open(dev_index);
290 sr_err("hantek-dso: unable to open device");
294 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
296 sr_err("hantek-dso: Unable to claim interface: %d", err);
303 static int hw_dev_close(int dev_index)
305 struct sr_dev_inst *sdi;
307 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
315 static int hw_cleanup(void)
318 struct sr_dev_inst *sdi;
321 /* Properly close and free all devices. */
322 for (l = dev_insts; l; l = l->next) {
323 if (!(sdi = l->data)) {
324 /* Log error, but continue cleaning up the rest. */
325 sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
328 if (!(ctx = sdi->priv)) {
329 /* Log error, but continue cleaning up the rest. */
330 sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
334 sr_usb_dev_inst_free(ctx->usb);
335 g_free(ctx->triggersource);
337 sr_dev_inst_free(sdi);
340 g_slist_free(dev_insts);
344 libusb_exit(usb_context);
350 static void *hw_get_device_info(int dev_index, int dev_info_id)
352 struct sr_dev_inst *sdi;
357 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
362 switch (dev_info_id) {
366 case SR_DI_NUM_PROBES:
367 info = GINT_TO_POINTER(ctx->profile->num_probes);
369 case SR_DI_PROBE_NAMES:
372 case SR_DI_BUFFERSIZES:
375 case SR_DI_TIMEBASES:
378 case SR_DI_TRIGGER_SOURCES:
379 info = trigger_sources;
382 info = filter_targets;
387 /* TODO remove this */
388 case SR_DI_CUR_SAMPLERATE:
396 static int hw_get_status(int device_index)
398 struct sr_dev_inst *sdi;
400 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
401 return SR_ST_NOT_FOUND;
406 static int *hwcap_get_all(void)
412 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
414 struct sr_dev_inst *sdi;
416 struct sr_rational tmp_rat;
420 char *tmp_str, **targets;
422 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
425 if (sdi->status != SR_ST_ACTIVE)
431 case SR_HWCAP_LIMIT_FRAMES:
432 ctx->limit_frames = *(uint64_t *)value;
434 case SR_HWCAP_PROBECONFIG:
435 ret = configure_probes(ctx, (GSList *) value);
437 case SR_HWCAP_TRIGGER_SLOPE:
438 tmp_u64 = *(int *)value;
439 if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE)
441 ctx->triggerslope = tmp_u64;
443 case SR_HWCAP_HORIZ_TRIGGERPOS:
444 tmp_float = *(float *)value;
445 if (tmp_float < 0.0 || tmp_float > 1.0) {
446 sr_err("hantek-dso: trigger position should be between 0.0 and 1.0");
449 ctx->triggerposition = tmp_float;
451 case SR_HWCAP_BUFFERSIZE:
452 tmp_u64 = *(int *)value;
453 for (i = 0; buffersizes[i]; i++) {
454 if (buffersizes[i] == tmp_u64) {
455 ctx->framesize = tmp_u64;
459 if (buffersizes[i] == 0)
462 case SR_HWCAP_TIMEBASE:
463 tmp_rat = *(struct sr_rational *)value;
464 for (i = 0; timebases[i].p && timebases[i].q; i++) {
465 if (timebases[i].p == tmp_rat.p
466 && timebases[i].q == tmp_rat.q) {
471 if (timebases[i].p == 0 && timebases[i].q == 0)
474 case SR_HWCAP_TRIGGER_SOURCE:
476 for (i = 0; trigger_sources[i]; i++) {
477 if (!strcmp(tmp_str, trigger_sources[i])) {
478 ctx->triggersource = g_strdup(tmp_str);
482 if (trigger_sources[i] == 0)
485 case SR_HWCAP_FILTER:
486 ctx->filter_ch1 = ctx->filter_ch2 = ctx->filter_trigger = 0;
487 targets = g_strsplit(value, ",", 0);
488 for (i = 0; targets[i]; i++) {
489 if (targets[i] == '\0')
490 /* Empty filter string can be used to clear them all. */
492 else if (!strcmp(targets[i], "CH1"))
493 ctx->filter_ch1 = TRUE;
494 else if (!strcmp(targets[i], "CH2"))
495 ctx->filter_ch2 = TRUE;
496 else if (!strcmp(targets[i], "TRIGGER"))
497 ctx->filter_trigger = TRUE;
499 sr_err("invalid filter target %s", targets[i]);
506 /* TODO not supporting vdiv per channel yet */
507 tmp_rat = *(struct sr_rational *)value;
508 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
509 if (vdivs[i].p == tmp_rat.p
510 && vdivs[i].q == tmp_rat.q) {
511 ctx->voltage_ch1 = i;
512 ctx->voltage_ch2 = i;
516 if (vdivs[i].p == 0 && vdivs[i].q == 0)
526 /* Called by libusb (as triggered by handle_event()) when a transfer comes in.
527 * Only channel data comes in asynchronously, and all transfers for this are
528 * queued up beforehand, so this just needs so chuck the incoming data onto
529 * the libsigrok session bus.
531 static void receive_transfer(struct libusb_transfer *transfer)
533 struct sr_datafeed_packet packet;
534 struct sr_datafeed_analog analog;
537 int num_probes, data_offset, i;
539 ctx = transfer->user_data;
540 sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes",
541 transfer->status, transfer->actual_length);
543 if (transfer->actual_length == 0)
544 /* Nothing to send to the bus. */
547 ctx->current_transfer += transfer->actual_length;
548 sr_dbg("hantek-dso: got %d of %d in frame", ctx->current_transfer, ctx->framesize * 2);
550 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
551 packet.type = SR_DF_ANALOG;
552 packet.payload = &analog;
553 /* TODO: support for 5xxx series 9-bit samples */
554 analog.num_samples = transfer->actual_length / 2;
555 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
557 for (i = 0; i < analog.num_samples; i++) {
558 /* The device always sends data for both channels. If a channel
559 * is disabled, it contains a copy of the enabled channel's
560 * data. However, we only send the requested channels to the bus.
562 /* TODO: support for 5xxx series 9-bit samples */
563 if (ctx->ch1_enabled) {
564 ch1 = (*(transfer->buffer + i * 2 + 1) / 255.0);
565 analog.data[data_offset++] = ch1;
567 if (ctx->ch2_enabled) {
568 ch2 = (*(transfer->buffer + i * 2) / 255.0);
569 analog.data[data_offset++] = ch2;
572 g_free(transfer->buffer);
573 libusb_free_transfer(transfer);
574 sr_session_send(ctx->cb_data, &packet);
576 if (ctx->current_transfer >= ctx->framesize * 2) {
577 /* That's the last chunk in this frame. */
578 packet.type = SR_DF_FRAME_END;
579 sr_session_send(ctx->cb_data, &packet);
581 if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) {
582 /* Terminate session */
583 /* TODO: don't leave pending USB transfers hanging */
584 packet.type = SR_DF_END;
585 sr_session_send(ctx->cb_data, &packet);
587 ctx->current_transfer = 0;
588 ctx->dev_state = NEW_CAPTURE;
594 static int handle_event(int fd, int revents, void *cb_data)
596 struct sr_datafeed_packet packet;
601 /* Avoid compiler warnings. */
605 /* Always handle pending libusb events. */
606 tv.tv_sec = tv.tv_usec = 0;
607 libusb_handle_events_timeout(usb_context, &tv);
611 if (ctx->dev_state == NEW_CAPTURE) {
612 if (dso_capture_start(ctx) != SR_OK)
614 if (dso_enable_trigger(ctx) != SR_OK)
616 // if (dso_force_trigger(ctx) != SR_OK)
618 sr_dbg("hantek-dso: successfully requested next chunk");
619 ctx->dev_state = CAPTURE;
622 if (ctx->dev_state != CAPTURE)
625 if ((capturestate = dso_get_capturestate(ctx)) == CAPTURE_UNKNOWN) {
626 /* Generated by the function, not the hardware. */
630 sr_dbg("hantek-dso: capturestate %d", capturestate);
631 switch (capturestate) {
633 if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) {
634 ctx->capture_empty_count = 0;
635 if (dso_capture_start(ctx) != SR_OK)
637 if (dso_enable_trigger(ctx) != SR_OK)
639 // if (dso_force_trigger(ctx) != SR_OK)
641 sr_dbg("hantek-dso: successfully requested next chunk");
644 case CAPTURE_FILLING:
647 case CAPTURE_READY_8BIT:
648 /* Tell the scope to send us the first frame. */
649 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
652 /* Don't hit the state machine again until we're done fetching
653 * the data we just told the scope to send.
655 ctx->dev_state = FETCH_DATA;
657 /* Tell the frontend a new frame is on the way. */
658 packet.type = SR_DF_FRAME_BEGIN;
659 sr_session_send(cb_data, &packet);
661 case CAPTURE_READY_9BIT:
663 sr_err("not yet supported");
665 case CAPTURE_TIMEOUT:
666 /* Doesn't matter, we'll try again next time. */
669 sr_dbg("unknown capture state");
675 static int hw_start_acquisition(int device_index, void *cb_data)
677 const struct libusb_pollfd **lupfd;
678 struct sr_datafeed_packet packet;
679 struct sr_datafeed_header header;
680 struct sr_datafeed_meta_analog meta;
681 struct sr_dev_inst *sdi;
685 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
688 if (sdi->status != SR_ST_ACTIVE)
692 ctx->cb_data = cb_data;
694 if (dso_init(ctx) != SR_OK)
697 if (dso_capture_start(ctx) != SR_OK)
700 ctx->dev_state = CAPTURE;
701 lupfd = libusb_get_pollfds(usb_context);
702 for (i = 0; lupfd[i]; i++)
703 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event,
707 /* Send header packet to the session bus. */
708 packet.type = SR_DF_HEADER;
709 packet.payload = (unsigned char *)&header;
710 header.feed_version = 1;
711 gettimeofday(&header.starttime, NULL);
712 sr_session_send(cb_data, &packet);
714 /* Send metadata about the SR_DF_ANALOG packets to come. */
715 packet.type = SR_DF_META_ANALOG;
716 packet.payload = &meta;
717 meta.num_probes = ctx->profile->num_probes;
718 sr_session_send(cb_data, &packet);
723 /* TODO: doesn't really cancel pending transfers so they might come in after
726 static int hw_stop_acquisition(int device_index, gpointer session_device_id)
728 struct sr_datafeed_packet packet;
729 struct sr_dev_inst *sdi;
732 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
735 if (sdi->status != SR_ST_ACTIVE)
739 ctx->dev_state = IDLE;
741 packet.type = SR_DF_END;
742 sr_session_send(session_device_id, &packet);
747 SR_PRIV struct sr_dev_driver hantek_dso_plugin_info = {
748 .name = "hantek-dso",
749 .longname = "Hantek DSO",
752 .cleanup = hw_cleanup,
753 .dev_open = hw_dev_open,
754 .dev_close = hw_dev_close,
755 .dev_info_get = hw_get_device_info,
756 .dev_status_get = hw_get_status,
757 .hwcap_get_all = hwcap_get_all,
758 .dev_config_set = hw_dev_config_set,
759 .dev_acquisition_start = hw_start_acquisition,
760 .dev_acquisition_stop = hw_stop_acquisition,