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 const int hwcaps[] = {
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 const struct dso_profile dev_profiles[] = {
70 FIRMWARE_DIR "/hantek-dso-2090.fw" },
71 { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
74 static const uint64_t buffersizes[] = {
81 static const struct sr_rational timebases[] = {
102 static const struct sr_rational vdivs[] = {
117 static const char *trigger_sources[] = {
124 static const char *filter_targets[] = {
127 /* TODO: "TRIGGER", */
131 static const char *coupling[] = {
138 SR_PRIV libusb_context *usb_context = NULL;
139 SR_PRIV GSList *dev_insts = NULL;
141 static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof)
143 struct sr_dev_inst *sdi;
146 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
147 prof->vendor, prof->model, prof->model_version);
151 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
152 sr_err("hantek-dso: ctx malloc failed");
156 ctx->dev_state = IDLE;
157 ctx->timebase = DEFAULT_TIMEBASE;
158 ctx->ch1_enabled = TRUE;
159 ctx->ch2_enabled = TRUE;
160 ctx->voltage_ch1 = DEFAULT_VOLTAGE;
161 ctx->voltage_ch2 = DEFAULT_VOLTAGE;
162 ctx->coupling_ch1 = DEFAULT_COUPLING;
163 ctx->coupling_ch2 = DEFAULT_COUPLING;
164 ctx->voffset_ch1 = DEFAULT_VERT_OFFSET;
165 ctx->voffset_ch2 = DEFAULT_VERT_OFFSET;
166 ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
167 ctx->framesize = DEFAULT_FRAMESIZE;
168 ctx->triggerslope = SLOPE_POSITIVE;
169 ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
170 ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
172 dev_insts = g_slist_append(dev_insts, sdi);
177 static int configure_probes(struct context *ctx, const GSList *probes)
179 const struct sr_probe *probe;
182 ctx->ch1_enabled = ctx->ch2_enabled = FALSE;
183 for (l = probes; l; l = l->next) {
184 probe = (struct sr_probe *)l->data;
185 if (probe->index == 1)
186 ctx->ch1_enabled = probe->enabled;
187 else if (probe->index == 2)
188 ctx->ch2_enabled = probe->enabled;
194 static int hw_init(const char *devinfo)
196 struct sr_dev_inst *sdi;
197 struct libusb_device_descriptor des;
198 const struct dso_profile *prof;
200 libusb_device **devlist;
201 int err, devcnt, i, j;
203 /* Avoid compiler warnings. */
206 if (libusb_init(&usb_context) != 0) {
207 sr_err("hantek-dso: Failed to initialize USB.");
211 /* Find all Hantek DSO devices and upload firmware to all of them. */
213 libusb_get_device_list(usb_context, &devlist);
214 for (i = 0; devlist[i]; i++) {
215 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
216 sr_err("hantek-dso: failed to get device descriptor: %d", err);
221 for (j = 0; dev_profiles[j].orig_vid; j++) {
222 if (des.idVendor == dev_profiles[j].orig_vid
223 && des.idProduct == dev_profiles[j].orig_pid) {
224 /* Device matches the pre-firmware profile. */
225 prof = &dev_profiles[j];
226 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
227 sdi = dso_dev_new(devcnt, prof);
229 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
230 prof->firmware) == SR_OK)
231 /* Remember when the firmware on this device was updated */
232 g_get_current_time(&ctx->fw_updated);
234 sr_err("hantek-dso: firmware upload failed for "
235 "device %d", devcnt);
236 /* Dummy USB address of 0xff will get overwritten later. */
237 ctx->usb = sr_usb_dev_inst_new(
238 libusb_get_bus_number(devlist[i]), 0xff, NULL);
241 } else if (des.idVendor == dev_profiles[j].fw_vid
242 && des.idProduct == dev_profiles[j].fw_pid) {
243 /* Device matches the post-firmware profile. */
244 prof = &dev_profiles[j];
245 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
246 sdi = dso_dev_new(devcnt, prof);
247 sdi->status = SR_ST_INACTIVE;
249 ctx->usb = sr_usb_dev_inst_new(
250 libusb_get_bus_number(devlist[i]),
251 libusb_get_device_address(devlist[i]), NULL);
257 /* not a supported VID/PID */
260 libusb_free_device_list(devlist, 1);
265 static int hw_dev_open(int dev_index)
268 struct sr_dev_inst *sdi;
272 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
277 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
278 * for the FX2 to renumerate
281 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
282 sr_info("hantek-dso: waiting for device to reset");
283 /* takes at least 300ms for the FX2 to be gone from the USB bus */
284 g_usleep(300 * 1000);
286 while (timediff < MAX_RENUM_DELAY) {
287 if ((err = dso_open(dev_index)) == SR_OK)
289 g_usleep(100 * 1000);
290 g_get_current_time(&cur_time);
291 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
293 sr_info("hantek-dso: device came back after %d ms", timediff);
295 err = dso_open(dev_index);
299 sr_err("hantek-dso: unable to open device");
303 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
305 sr_err("hantek-dso: Unable to claim interface: %d", err);
312 static int hw_dev_close(int dev_index)
314 struct sr_dev_inst *sdi;
316 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
324 static int hw_cleanup(void)
327 struct sr_dev_inst *sdi;
330 /* Properly close and free all devices. */
331 for (l = dev_insts; l; l = l->next) {
332 if (!(sdi = l->data)) {
333 /* Log error, but continue cleaning up the rest. */
334 sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
337 if (!(ctx = sdi->priv)) {
338 /* Log error, but continue cleaning up the rest. */
339 sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
343 sr_usb_dev_inst_free(ctx->usb);
344 g_free(ctx->triggersource);
346 sr_dev_inst_free(sdi);
349 g_slist_free(dev_insts);
353 libusb_exit(usb_context);
359 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
361 struct sr_dev_inst *sdi;
366 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
371 switch (dev_info_id) {
375 case SR_DI_NUM_PROBES:
376 info = GINT_TO_POINTER(ctx->profile->num_probes);
378 case SR_DI_PROBE_NAMES:
381 case SR_DI_BUFFERSIZES:
384 case SR_DI_TIMEBASES:
387 case SR_DI_TRIGGER_SOURCES:
388 info = trigger_sources;
391 info = filter_targets;
399 /* TODO remove this */
400 case SR_DI_CUR_SAMPLERATE:
408 static int hw_dev_status_get(int dev_index)
410 struct sr_dev_inst *sdi;
412 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
413 return SR_ST_NOT_FOUND;
418 static const int *hw_hwcap_get_all(void)
423 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
425 struct sr_dev_inst *sdi;
427 struct sr_rational tmp_rat;
433 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
436 if (sdi->status != SR_ST_ACTIVE)
442 case SR_HWCAP_LIMIT_FRAMES:
443 ctx->limit_frames = *(const uint64_t *)value;
445 case SR_HWCAP_PROBECONFIG:
446 ret = configure_probes(ctx, (const GSList *)value);
448 case SR_HWCAP_TRIGGER_SLOPE:
449 tmp_u64 = *(const int *)value;
450 if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE)
452 ctx->triggerslope = tmp_u64;
454 case SR_HWCAP_HORIZ_TRIGGERPOS:
455 tmp_float = *(const float *)value;
456 if (tmp_float < 0.0 || tmp_float > 1.0) {
457 sr_err("hantek-dso: trigger position should be between 0.0 and 1.0");
460 ctx->triggerposition = tmp_float;
462 case SR_HWCAP_BUFFERSIZE:
463 tmp_u64 = *(const int *)value;
464 for (i = 0; buffersizes[i]; i++) {
465 if (buffersizes[i] == tmp_u64) {
466 ctx->framesize = tmp_u64;
470 if (buffersizes[i] == 0)
473 case SR_HWCAP_TIMEBASE:
474 tmp_rat = *(const struct sr_rational *)value;
475 for (i = 0; timebases[i].p && timebases[i].q; i++) {
476 if (timebases[i].p == tmp_rat.p
477 && timebases[i].q == tmp_rat.q) {
482 if (timebases[i].p == 0 && timebases[i].q == 0)
485 case SR_HWCAP_TRIGGER_SOURCE:
486 for (i = 0; trigger_sources[i]; i++) {
487 if (!strcmp(value, trigger_sources[i])) {
488 ctx->triggersource = g_strdup(value);
492 if (trigger_sources[i] == 0)
495 case SR_HWCAP_FILTER:
496 ctx->filter_ch1 = ctx->filter_ch2 = ctx->filter_trigger = 0;
497 targets = g_strsplit(value, ",", 0);
498 for (i = 0; targets[i]; i++) {
499 if (targets[i] == '\0')
500 /* Empty filter string can be used to clear them all. */
502 else if (!strcmp(targets[i], "CH1"))
503 ctx->filter_ch1 = TRUE;
504 else if (!strcmp(targets[i], "CH2"))
505 ctx->filter_ch2 = TRUE;
506 else if (!strcmp(targets[i], "TRIGGER"))
507 ctx->filter_trigger = TRUE;
509 sr_err("invalid filter target %s", targets[i]);
516 /* TODO not supporting vdiv per channel yet */
517 tmp_rat = *(const struct sr_rational *)value;
518 for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
519 if (vdivs[i].p == tmp_rat.p
520 && vdivs[i].q == tmp_rat.q) {
521 ctx->voltage_ch1 = i;
522 ctx->voltage_ch2 = i;
526 if (vdivs[i].p == 0 && vdivs[i].q == 0)
529 case SR_HWCAP_COUPLING:
530 /* TODO not supporting coupling per channel yet */
531 for (i = 0; coupling[i]; i++) {
532 if (!strcmp(value, coupling[i])) {
533 ctx->coupling_ch1 = i;
534 ctx->coupling_ch2 = i;
538 if (coupling[i] == 0)
548 /* Called by libusb (as triggered by handle_event()) when a transfer comes in.
549 * Only channel data comes in asynchronously, and all transfers for this are
550 * queued up beforehand, so this just needs so chuck the incoming data onto
551 * the libsigrok session bus.
553 static void receive_transfer(struct libusb_transfer *transfer)
555 struct sr_datafeed_packet packet;
556 struct sr_datafeed_analog analog;
559 int num_probes, data_offset, i;
561 ctx = transfer->user_data;
562 sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes",
563 transfer->status, transfer->actual_length);
565 if (transfer->actual_length == 0)
566 /* Nothing to send to the bus. */
569 ctx->current_transfer += transfer->actual_length;
570 sr_dbg("hantek-dso: got %d of %d in frame", ctx->current_transfer, ctx->framesize * 2);
572 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
573 packet.type = SR_DF_ANALOG;
574 packet.payload = &analog;
575 /* TODO: support for 5xxx series 9-bit samples */
576 analog.num_samples = transfer->actual_length / 2;
577 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
579 for (i = 0; i < analog.num_samples; i++) {
580 /* The device always sends data for both channels. If a channel
581 * is disabled, it contains a copy of the enabled channel's
582 * data. However, we only send the requested channels to the bus.
584 /* TODO: support for 5xxx series 9-bit samples */
585 if (ctx->ch1_enabled) {
586 ch1 = (*(transfer->buffer + i * 2 + 1) / 255.0);
587 analog.data[data_offset++] = ch1;
589 if (ctx->ch2_enabled) {
590 ch2 = (*(transfer->buffer + i * 2) / 255.0);
591 analog.data[data_offset++] = ch2;
594 g_free(transfer->buffer);
595 libusb_free_transfer(transfer);
596 sr_session_send(ctx->cb_data, &packet);
598 if (ctx->current_transfer >= ctx->framesize * 2) {
599 /* That's the last chunk in this frame. */
600 packet.type = SR_DF_FRAME_END;
601 sr_session_send(ctx->cb_data, &packet);
603 if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) {
604 /* Terminate session */
605 /* TODO: don't leave pending USB transfers hanging */
606 packet.type = SR_DF_END;
607 sr_session_send(ctx->cb_data, &packet);
609 ctx->current_transfer = 0;
610 ctx->dev_state = NEW_CAPTURE;
616 static int handle_event(int fd, int revents, void *cb_data)
618 struct sr_datafeed_packet packet;
623 /* Avoid compiler warnings. */
627 /* Always handle pending libusb events. */
628 tv.tv_sec = tv.tv_usec = 0;
629 libusb_handle_events_timeout(usb_context, &tv);
633 if (ctx->dev_state == NEW_CAPTURE) {
634 if (dso_capture_start(ctx) != SR_OK)
636 if (dso_enable_trigger(ctx) != SR_OK)
638 // if (dso_force_trigger(ctx) != SR_OK)
640 sr_dbg("hantek-dso: successfully requested next chunk");
641 ctx->dev_state = CAPTURE;
644 if (ctx->dev_state != CAPTURE)
647 if ((capturestate = dso_get_capturestate(ctx)) == CAPTURE_UNKNOWN) {
648 /* Generated by the function, not the hardware. */
652 sr_dbg("hantek-dso: capturestate %d", capturestate);
653 switch (capturestate) {
655 if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) {
656 ctx->capture_empty_count = 0;
657 if (dso_capture_start(ctx) != SR_OK)
659 if (dso_enable_trigger(ctx) != SR_OK)
661 // if (dso_force_trigger(ctx) != SR_OK)
663 sr_dbg("hantek-dso: successfully requested next chunk");
666 case CAPTURE_FILLING:
669 case CAPTURE_READY_8BIT:
670 /* Tell the scope to send us the first frame. */
671 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
674 /* Don't hit the state machine again until we're done fetching
675 * the data we just told the scope to send.
677 ctx->dev_state = FETCH_DATA;
679 /* Tell the frontend a new frame is on the way. */
680 packet.type = SR_DF_FRAME_BEGIN;
681 sr_session_send(cb_data, &packet);
683 case CAPTURE_READY_9BIT:
685 sr_err("not yet supported");
687 case CAPTURE_TIMEOUT:
688 /* Doesn't matter, we'll try again next time. */
691 sr_dbg("unknown capture state");
697 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
699 const struct libusb_pollfd **lupfd;
700 struct sr_datafeed_packet packet;
701 struct sr_datafeed_header header;
702 struct sr_datafeed_meta_analog meta;
703 struct sr_dev_inst *sdi;
707 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
710 if (sdi->status != SR_ST_ACTIVE)
714 ctx->cb_data = cb_data;
716 if (dso_init(ctx) != SR_OK)
719 if (dso_capture_start(ctx) != SR_OK)
722 ctx->dev_state = CAPTURE;
723 lupfd = libusb_get_pollfds(usb_context);
724 for (i = 0; lupfd[i]; i++)
725 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event,
729 /* Send header packet to the session bus. */
730 packet.type = SR_DF_HEADER;
731 packet.payload = (unsigned char *)&header;
732 header.feed_version = 1;
733 gettimeofday(&header.starttime, NULL);
734 sr_session_send(cb_data, &packet);
736 /* Send metadata about the SR_DF_ANALOG packets to come. */
737 packet.type = SR_DF_META_ANALOG;
738 packet.payload = &meta;
739 meta.num_probes = ctx->profile->num_probes;
740 sr_session_send(cb_data, &packet);
745 /* TODO: doesn't really cancel pending transfers so they might come in after
748 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
750 struct sr_datafeed_packet packet;
751 struct sr_dev_inst *sdi;
754 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
757 if (sdi->status != SR_ST_ACTIVE)
761 ctx->dev_state = IDLE;
763 packet.type = SR_DF_END;
764 sr_session_send(cb_data, &packet);
769 SR_PRIV struct sr_dev_driver hantek_dso_driver_info = {
770 .name = "hantek-dso",
771 .longname = "Hantek DSO",
774 .cleanup = hw_cleanup,
775 .dev_open = hw_dev_open,
776 .dev_close = hw_dev_close,
777 .dev_info_get = hw_dev_info_get,
778 .dev_status_get = hw_dev_status_get,
779 .hwcap_get_all = hw_hwcap_get_all,
780 .dev_config_set = hw_dev_config_set,
781 .dev_acquisition_start = hw_dev_acquisition_start,
782 .dev_acquisition_stop = hw_dev_acquisition_stop,