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"
38 /* FIXME: Temporary build fix, this will be removed later. */
39 #define GTV_TO_MSEC(gtv) (gtv.tv_sec * 1000 + gtv.tv_usec / 1000)
41 /* Max time in ms before we want to check on events */
44 static int capabilities[] = {
45 SR_HWCAP_OSCILLOSCOPE,
46 SR_HWCAP_LIMIT_SAMPLES,
50 SR_HWCAP_TRIGGER_SOURCE,
51 SR_HWCAP_TRIGGER_SLOPE,
52 SR_HWCAP_HORIZ_TRIGGERPOS,
59 static const char *probe_names[] = {
65 static struct dso_profile dev_profiles[] = {
70 FIRMWARE_DIR "/hantek-dso-2090.fw" },
71 { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
74 static uint64_t buffersizes[] = {
81 static struct sr_rational timebases[] = {
102 static struct sr_rational vdivs[] = {
117 static char *trigger_sources[] = {
124 static char *filter_targets[] = {
127 /* TODO: "TRIGGER", */
131 static char *coupling[] = {
138 SR_PRIV libusb_context *usb_context = NULL;
139 SR_PRIV GSList *dev_insts = NULL;
142 static struct sr_dev_inst *dso_dev_new(int index, struct dso_profile *prof)
144 struct sr_dev_inst *sdi;
147 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
148 prof->vendor, prof->model, prof->model_version);
152 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
153 sr_err("hantek-dso: ctx malloc failed");
157 ctx->dev_state = IDLE;
158 ctx->timebase = DEFAULT_TIMEBASE;
159 ctx->ch1_enabled = TRUE;
160 ctx->ch2_enabled = TRUE;
161 ctx->voltage_ch1 = DEFAULT_VOLTAGE;
162 ctx->voltage_ch2 = DEFAULT_VOLTAGE;
163 ctx->coupling_ch1 = DEFAULT_COUPLING;
164 ctx->coupling_ch2 = DEFAULT_COUPLING;
165 ctx->voffset_ch1 = DEFAULT_VERT_OFFSET;
166 ctx->voffset_ch2 = DEFAULT_VERT_OFFSET;
167 ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
168 ctx->framesize = DEFAULT_FRAMESIZE;
169 ctx->triggerslope = SLOPE_POSITIVE;
170 ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
171 ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
173 dev_insts = g_slist_append(dev_insts, sdi);
178 static int configure_probes(struct context *ctx, GSList *probes)
180 struct sr_probe *probe;
183 ctx->ch1_enabled = ctx->ch2_enabled = FALSE;
184 for (l = probes; l; l = l->next) {
185 probe = (struct sr_probe *)l->data;
186 if (probe->index == 1)
187 ctx->ch1_enabled = probe->enabled;
188 else if (probe->index == 2)
189 ctx->ch2_enabled = probe->enabled;
195 static int hw_init(const char *devinfo)
197 struct sr_dev_inst *sdi;
198 struct libusb_device_descriptor des;
199 struct dso_profile *prof;
201 libusb_device **devlist;
202 int err, devcnt, i, j;
204 /* Avoid compiler warnings. */
207 if (libusb_init(&usb_context) != 0) {
208 sr_err("hantek-dso: Failed to initialize USB.");
212 /* Find all Hantek DSO devices and upload firmware to all of them. */
214 libusb_get_device_list(usb_context, &devlist);
215 for (i = 0; devlist[i]; i++) {
216 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
217 sr_err("hantek-dso: failed to get device descriptor: %d", err);
222 for (j = 0; dev_profiles[j].orig_vid; j++) {
223 if (des.idVendor == dev_profiles[j].orig_vid
224 && des.idProduct == dev_profiles[j].orig_pid) {
225 /* Device matches the pre-firmware profile. */
226 prof = &dev_profiles[j];
227 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
228 sdi = dso_dev_new(devcnt, prof);
230 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
231 prof->firmware) == SR_OK)
232 /* Remember when the firmware on this device was updated */
233 g_get_current_time(&ctx->fw_updated);
235 sr_err("hantek-dso: firmware upload failed for "
236 "device %d", devcnt);
237 /* Dummy USB address of 0xff will get overwritten later. */
238 ctx->usb = sr_usb_dev_inst_new(
239 libusb_get_bus_number(devlist[i]), 0xff, NULL);
242 } else if (des.idVendor == dev_profiles[j].fw_vid
243 && des.idProduct == dev_profiles[j].fw_pid) {
244 /* Device matches the post-firmware profile. */
245 prof = &dev_profiles[j];
246 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
247 sdi = dso_dev_new(devcnt, prof);
248 sdi->status = SR_ST_INACTIVE;
250 ctx->usb = sr_usb_dev_inst_new(
251 libusb_get_bus_number(devlist[i]),
252 libusb_get_device_address(devlist[i]), NULL);
258 /* not a supported VID/PID */
261 libusb_free_device_list(devlist, 1);
266 static int hw_dev_open(int dev_index)
269 struct sr_dev_inst *sdi;
273 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
278 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
279 * for the FX2 to renumerate
282 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
283 sr_info("hantek-dso: waiting for device to reset");
284 /* takes at least 300ms for the FX2 to be gone from the USB bus */
285 g_usleep(300 * 1000);
287 while (timediff < MAX_RENUM_DELAY) {
288 if ((err = dso_open(dev_index)) == SR_OK)
290 g_usleep(100 * 1000);
291 g_get_current_time(&cur_time);
292 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
294 sr_info("hantek-dso: device came back after %d ms", timediff);
296 err = dso_open(dev_index);
300 sr_err("hantek-dso: unable to open device");
304 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
306 sr_err("hantek-dso: Unable to claim interface: %d", err);
313 static int hw_dev_close(int dev_index)
315 struct sr_dev_inst *sdi;
317 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
325 static int hw_cleanup(void)
328 struct sr_dev_inst *sdi;
331 /* Properly close and free all devices. */
332 for (l = dev_insts; l; l = l->next) {
333 if (!(sdi = l->data)) {
334 /* Log error, but continue cleaning up the rest. */
335 sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
338 if (!(ctx = sdi->priv)) {
339 /* Log error, but continue cleaning up the rest. */
340 sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
344 sr_usb_dev_inst_free(ctx->usb);
345 g_free(ctx->triggersource);
347 sr_dev_inst_free(sdi);
350 g_slist_free(dev_insts);
354 libusb_exit(usb_context);
360 static const void *hw_get_device_info(int dev_index, int dev_info_id)
362 struct sr_dev_inst *sdi;
367 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
372 switch (dev_info_id) {
376 case SR_DI_NUM_PROBES:
377 info = GINT_TO_POINTER(ctx->profile->num_probes);
379 case SR_DI_PROBE_NAMES:
382 case SR_DI_BUFFERSIZES:
385 case SR_DI_TIMEBASES:
388 case SR_DI_TRIGGER_SOURCES:
389 info = trigger_sources;
392 info = filter_targets;
400 /* TODO remove this */
401 case SR_DI_CUR_SAMPLERATE:
409 static int hw_get_status(int device_index)
411 struct sr_dev_inst *sdi;
413 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
414 return SR_ST_NOT_FOUND;
419 static const int *hwcap_get_all(void)
424 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
426 struct sr_dev_inst *sdi;
428 struct sr_rational tmp_rat;
434 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
437 if (sdi->status != SR_ST_ACTIVE)
443 case SR_HWCAP_LIMIT_FRAMES:
444 ctx->limit_frames = *(uint64_t *)value;
446 case SR_HWCAP_PROBECONFIG:
447 ret = configure_probes(ctx, (GSList *) value);
449 case SR_HWCAP_TRIGGER_SLOPE:
450 tmp_u64 = *(int *)value;
451 if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE)
453 ctx->triggerslope = tmp_u64;
455 case SR_HWCAP_HORIZ_TRIGGERPOS:
456 tmp_float = *(float *)value;
457 if (tmp_float < 0.0 || tmp_float > 1.0) {
458 sr_err("hantek-dso: trigger position should be between 0.0 and 1.0");
461 ctx->triggerposition = tmp_float;
463 case SR_HWCAP_BUFFERSIZE:
464 tmp_u64 = *(int *)value;
465 for (i = 0; buffersizes[i]; i++) {
466 if (buffersizes[i] == tmp_u64) {
467 ctx->framesize = tmp_u64;
471 if (buffersizes[i] == 0)
474 case SR_HWCAP_TIMEBASE:
475 tmp_rat = *(struct sr_rational *)value;
476 for (i = 0; timebases[i].p && timebases[i].q; i++) {
477 if (timebases[i].p == tmp_rat.p
478 && timebases[i].q == tmp_rat.q) {
483 if (timebases[i].p == 0 && timebases[i].q == 0)
486 case SR_HWCAP_TRIGGER_SOURCE:
487 for (i = 0; trigger_sources[i]; i++) {
488 if (!strcmp(value, trigger_sources[i])) {
489 ctx->triggersource = g_strdup(value);
493 if (trigger_sources[i] == 0)
496 case SR_HWCAP_FILTER:
497 ctx->filter_ch1 = ctx->filter_ch2 = ctx->filter_trigger = 0;
498 targets = g_strsplit(value, ",", 0);
499 for (i = 0; targets[i]; i++) {
500 if (targets[i] == '\0')
501 /* Empty filter string can be used to clear them all. */
503 else if (!strcmp(targets[i], "CH1"))
504 ctx->filter_ch1 = TRUE;
505 else if (!strcmp(targets[i], "CH2"))
506 ctx->filter_ch2 = TRUE;
507 else if (!strcmp(targets[i], "TRIGGER"))
508 ctx->filter_trigger = TRUE;
510 sr_err("invalid filter target %s", targets[i]);
517 /* TODO not supporting vdiv per channel yet */
518 tmp_rat = *(struct sr_rational *)value;
519 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
520 if (vdivs[i].p == tmp_rat.p
521 && vdivs[i].q == tmp_rat.q) {
522 ctx->voltage_ch1 = i;
523 ctx->voltage_ch2 = i;
527 if (vdivs[i].p == 0 && vdivs[i].q == 0)
530 case SR_HWCAP_COUPLING:
531 /* TODO not supporting coupling per channel yet */
532 for (i = 0; coupling[i]; i++) {
533 if (!strcmp(value, coupling[i])) {
534 ctx->coupling_ch1 = i;
535 ctx->coupling_ch2 = i;
539 if (coupling[i] == 0)
549 /* Called by libusb (as triggered by handle_event()) when a transfer comes in.
550 * Only channel data comes in asynchronously, and all transfers for this are
551 * queued up beforehand, so this just needs so chuck the incoming data onto
552 * the libsigrok session bus.
554 static void receive_transfer(struct libusb_transfer *transfer)
556 struct sr_datafeed_packet packet;
557 struct sr_datafeed_analog analog;
560 int num_probes, data_offset, i;
562 ctx = transfer->user_data;
563 sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes",
564 transfer->status, transfer->actual_length);
566 if (transfer->actual_length == 0)
567 /* Nothing to send to the bus. */
570 ctx->current_transfer += transfer->actual_length;
571 sr_dbg("hantek-dso: got %d of %d in frame", ctx->current_transfer, ctx->framesize * 2);
573 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
574 packet.type = SR_DF_ANALOG;
575 packet.payload = &analog;
576 /* TODO: support for 5xxx series 9-bit samples */
577 analog.num_samples = transfer->actual_length / 2;
578 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
580 for (i = 0; i < analog.num_samples; i++) {
581 /* The device always sends data for both channels. If a channel
582 * is disabled, it contains a copy of the enabled channel's
583 * data. However, we only send the requested channels to the bus.
585 /* TODO: support for 5xxx series 9-bit samples */
586 if (ctx->ch1_enabled) {
587 ch1 = (*(transfer->buffer + i * 2 + 1) / 255.0);
588 analog.data[data_offset++] = ch1;
590 if (ctx->ch2_enabled) {
591 ch2 = (*(transfer->buffer + i * 2) / 255.0);
592 analog.data[data_offset++] = ch2;
595 g_free(transfer->buffer);
596 libusb_free_transfer(transfer);
597 sr_session_send(ctx->cb_data, &packet);
599 if (ctx->current_transfer >= ctx->framesize * 2) {
600 /* That's the last chunk in this frame. */
601 packet.type = SR_DF_FRAME_END;
602 sr_session_send(ctx->cb_data, &packet);
604 if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) {
605 /* Terminate session */
606 /* TODO: don't leave pending USB transfers hanging */
607 packet.type = SR_DF_END;
608 sr_session_send(ctx->cb_data, &packet);
610 ctx->current_transfer = 0;
611 ctx->dev_state = NEW_CAPTURE;
617 static int handle_event(int fd, int revents, void *cb_data)
619 struct sr_datafeed_packet packet;
624 /* Avoid compiler warnings. */
628 /* Always handle pending libusb events. */
629 tv.tv_sec = tv.tv_usec = 0;
630 libusb_handle_events_timeout(usb_context, &tv);
634 if (ctx->dev_state == NEW_CAPTURE) {
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");
642 ctx->dev_state = CAPTURE;
645 if (ctx->dev_state != CAPTURE)
648 if ((capturestate = dso_get_capturestate(ctx)) == CAPTURE_UNKNOWN) {
649 /* Generated by the function, not the hardware. */
653 sr_dbg("hantek-dso: capturestate %d", capturestate);
654 switch (capturestate) {
656 if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) {
657 ctx->capture_empty_count = 0;
658 if (dso_capture_start(ctx) != SR_OK)
660 if (dso_enable_trigger(ctx) != SR_OK)
662 // if (dso_force_trigger(ctx) != SR_OK)
664 sr_dbg("hantek-dso: successfully requested next chunk");
667 case CAPTURE_FILLING:
670 case CAPTURE_READY_8BIT:
671 /* Tell the scope to send us the first frame. */
672 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
675 /* Don't hit the state machine again until we're done fetching
676 * the data we just told the scope to send.
678 ctx->dev_state = FETCH_DATA;
680 /* Tell the frontend a new frame is on the way. */
681 packet.type = SR_DF_FRAME_BEGIN;
682 sr_session_send(cb_data, &packet);
684 case CAPTURE_READY_9BIT:
686 sr_err("not yet supported");
688 case CAPTURE_TIMEOUT:
689 /* Doesn't matter, we'll try again next time. */
692 sr_dbg("unknown capture state");
698 static int hw_start_acquisition(int device_index, void *cb_data)
700 const struct libusb_pollfd **lupfd;
701 struct sr_datafeed_packet packet;
702 struct sr_datafeed_header header;
703 struct sr_datafeed_meta_analog meta;
704 struct sr_dev_inst *sdi;
708 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
711 if (sdi->status != SR_ST_ACTIVE)
715 ctx->cb_data = cb_data;
717 if (dso_init(ctx) != SR_OK)
720 if (dso_capture_start(ctx) != SR_OK)
723 ctx->dev_state = CAPTURE;
724 lupfd = libusb_get_pollfds(usb_context);
725 for (i = 0; lupfd[i]; i++)
726 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event,
730 /* Send header packet to the session bus. */
731 packet.type = SR_DF_HEADER;
732 packet.payload = (unsigned char *)&header;
733 header.feed_version = 1;
734 gettimeofday(&header.starttime, NULL);
735 sr_session_send(cb_data, &packet);
737 /* Send metadata about the SR_DF_ANALOG packets to come. */
738 packet.type = SR_DF_META_ANALOG;
739 packet.payload = &meta;
740 meta.num_probes = ctx->profile->num_probes;
741 sr_session_send(cb_data, &packet);
746 /* TODO: doesn't really cancel pending transfers so they might come in after
749 static int hw_stop_acquisition(int device_index, gpointer session_device_id)
751 struct sr_datafeed_packet packet;
752 struct sr_dev_inst *sdi;
755 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
758 if (sdi->status != SR_ST_ACTIVE)
762 ctx->dev_state = IDLE;
764 packet.type = SR_DF_END;
765 sr_session_send(session_device_id, &packet);
770 SR_PRIV struct sr_dev_driver hantek_dso_plugin_info = {
771 .name = "hantek-dso",
772 .longname = "Hantek DSO",
775 .cleanup = hw_cleanup,
776 .dev_open = hw_dev_open,
777 .dev_close = hw_dev_close,
778 .dev_info_get = hw_get_device_info,
779 .dev_status_get = hw_get_status,
780 .hwcap_get_all = hwcap_get_all,
781 .dev_config_set = hw_dev_config_set,
782 .dev_acquisition_start = hw_start_acquisition,
783 .dev_acquisition_stop = hw_stop_acquisition,