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,
54 static const char *probe_names[] = {
60 static struct dso_profile dev_profiles[] = {
65 FIRMWARE_DIR "/hantek-dso-2090.fw" },
66 { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
69 static uint64_t buffersizes[] = {
76 static struct sr_rational timebases[] = {
97 static char *trigger_sources[] = {
104 SR_PRIV libusb_context *usb_context = NULL;
105 SR_PRIV GSList *dev_insts = NULL;
108 static struct sr_dev_inst *dso_dev_new(int index, struct dso_profile *prof)
110 struct sr_dev_inst *sdi;
113 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
114 prof->vendor, prof->model, prof->model_version);
118 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
119 sr_err("hantek-dso: ctx malloc failed");
123 ctx->dev_state = IDLE;
124 ctx->timebase = DEFAULT_TIMEBASE;
125 ctx->ch1_enabled = TRUE;
126 ctx->ch2_enabled = TRUE;
127 ctx->voltage_ch1 = DEFAULT_VOLTAGE;
128 ctx->voltage_ch2 = DEFAULT_VOLTAGE;
129 ctx->coupling_ch1 = DEFAULT_COUPLING;
130 ctx->coupling_ch2 = DEFAULT_COUPLING;
131 ctx->voffset_ch1 = DEFAULT_VERT_OFFSET;
132 ctx->voffset_ch2 = DEFAULT_VERT_OFFSET;
133 ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
134 ctx->framesize = DEFAULT_FRAMESIZE;
135 ctx->triggerslope = SLOPE_POSITIVE;
136 ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
137 ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
139 dev_insts = g_slist_append(dev_insts, sdi);
144 static int configure_probes(struct context *ctx, GSList *probes)
146 struct sr_probe *probe;
149 ctx->ch1_enabled = ctx->ch2_enabled = FALSE;
150 for (l = probes; l; l = l->next) {
151 probe = (struct sr_probe *)l->data;
152 if (probe->index == 1)
153 ctx->ch1_enabled = probe->enabled;
154 else if (probe->index == 2)
155 ctx->ch2_enabled = probe->enabled;
161 static int hw_init(const char *devinfo)
163 struct sr_dev_inst *sdi;
164 struct libusb_device_descriptor des;
165 struct dso_profile *prof;
167 libusb_device **devlist;
168 int err, devcnt, i, j;
170 /* Avoid compiler warnings. */
173 if (libusb_init(&usb_context) != 0) {
174 sr_err("hantek-dso: Failed to initialize USB.");
178 /* Find all Hantek DSO devices and upload firmware to all of them. */
180 libusb_get_device_list(usb_context, &devlist);
181 for (i = 0; devlist[i]; i++) {
182 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
183 sr_err("hantek-dso: failed to get device descriptor: %d", err);
188 for (j = 0; dev_profiles[j].orig_vid; j++) {
189 if (des.idVendor == dev_profiles[j].orig_vid
190 && des.idProduct == dev_profiles[j].orig_pid) {
191 /* Device matches the pre-firmware profile. */
192 prof = &dev_profiles[j];
193 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
194 sdi = dso_dev_new(devcnt, prof);
196 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
197 prof->firmware) == SR_OK)
198 /* Remember when the firmware on this device was updated */
199 g_get_current_time(&ctx->fw_updated);
201 sr_err("hantek-dso: firmware upload failed for "
202 "device %d", devcnt);
203 /* Dummy USB address of 0xff will get overwritten later. */
204 ctx->usb = sr_usb_dev_inst_new(
205 libusb_get_bus_number(devlist[i]), 0xff, NULL);
208 } else if (des.idVendor == dev_profiles[j].fw_vid
209 && des.idProduct == dev_profiles[j].fw_pid) {
210 /* Device matches the post-firmware profile. */
211 prof = &dev_profiles[j];
212 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
213 sdi = dso_dev_new(devcnt, prof);
214 sdi->status = SR_ST_INACTIVE;
216 ctx->usb = sr_usb_dev_inst_new(
217 libusb_get_bus_number(devlist[i]),
218 libusb_get_device_address(devlist[i]), NULL);
224 /* not a supported VID/PID */
227 libusb_free_device_list(devlist, 1);
232 static int hw_dev_open(int dev_index)
235 struct sr_dev_inst *sdi;
239 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
244 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
245 * for the FX2 to renumerate
248 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
249 sr_info("hantek-dso: waiting for device to reset");
250 /* takes at least 300ms for the FX2 to be gone from the USB bus */
251 g_usleep(300 * 1000);
253 while (timediff < MAX_RENUM_DELAY) {
254 if ((err = dso_open(dev_index)) == SR_OK)
256 g_usleep(100 * 1000);
257 g_get_current_time(&cur_time);
258 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
260 sr_info("hantek-dso: device came back after %d ms", timediff);
262 err = dso_open(dev_index);
266 sr_err("hantek-dso: unable to open device");
270 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
272 sr_err("hantek-dso: Unable to claim interface: %d", err);
279 static int hw_dev_close(int dev_index)
281 struct sr_dev_inst *sdi;
283 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
291 static int hw_cleanup(void)
294 struct sr_dev_inst *sdi;
297 /* Properly close and free all devices. */
298 for (l = dev_insts; l; l = l->next) {
299 if (!(sdi = l->data)) {
300 /* Log error, but continue cleaning up the rest. */
301 sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
304 if (!(ctx = sdi->priv)) {
305 /* Log error, but continue cleaning up the rest. */
306 sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
310 sr_usb_dev_inst_free(ctx->usb);
311 g_free(ctx->triggersource);
313 sr_dev_inst_free(sdi);
316 g_slist_free(dev_insts);
320 libusb_exit(usb_context);
326 static void *hw_get_device_info(int dev_index, int dev_info_id)
328 struct sr_dev_inst *sdi;
333 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
338 switch (dev_info_id) {
342 case SR_DI_NUM_PROBES:
343 info = GINT_TO_POINTER(ctx->profile->num_probes);
345 case SR_DI_PROBE_NAMES:
348 case SR_DI_BUFFERSIZES:
351 case SR_DI_TIMEBASES:
354 case SR_DI_TRIGGER_SOURCES:
355 info = trigger_sources;
357 /* TODO remove this */
358 case SR_DI_CUR_SAMPLERATE:
366 static int hw_get_status(int device_index)
368 struct sr_dev_inst *sdi;
370 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
371 return SR_ST_NOT_FOUND;
376 static int *hwcap_get_all(void)
382 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
384 struct sr_dev_inst *sdi;
386 struct sr_rational tmp_rat;
392 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
395 if (sdi->status != SR_ST_ACTIVE)
401 case SR_HWCAP_LIMIT_FRAMES:
402 ctx->limit_frames = *(uint64_t *)value;
404 case SR_HWCAP_PROBECONFIG:
405 ret = configure_probes(ctx, (GSList *) value);
407 case SR_HWCAP_TRIGGER_SLOPE:
408 tmp_u64 = *(int *)value;
409 if (tmp_u64 != SLOPE_NEGATIVE && tmp_u64 != SLOPE_POSITIVE)
411 ctx->triggerslope = tmp_u64;
413 case SR_HWCAP_HORIZ_TRIGGERPOS:
414 tmp_float = *(float *)value;
415 if (tmp_float < 0.0 || tmp_float > 1.0) {
416 sr_err("hantek-dso: trigger position should be between 0.0 and 1.0");
419 ctx->triggerposition = tmp_float;
421 case SR_HWCAP_BUFFERSIZE:
422 tmp_u64 = *(int *)value;
423 for (i = 0; buffersizes[i]; i++) {
424 if (buffersizes[i] == tmp_u64) {
425 ctx->framesize = tmp_u64;
429 if (buffersizes[i] == 0)
432 case SR_HWCAP_TIMEBASE:
433 tmp_rat = *(struct sr_rational *)value;
434 for (i = 0; timebases[i].p && timebases[i].q; i++) {
435 if (timebases[i].p == tmp_rat.p
436 && timebases[i].q == tmp_rat.q) {
441 if (timebases[i].p == 0 && timebases[i].q == 0)
444 case SR_HWCAP_TRIGGER_SOURCE:
446 for (i = 0; trigger_sources[i]; i++) {
447 if (!strcmp(tmp_str, trigger_sources[i])) {
448 ctx->triggersource = g_strdup(tmp_str);
452 if (trigger_sources[i] == 0)
462 /* Called by libusb (as triggered by handle_event()) when a transfer comes in.
463 * Only channel data comes in asynchronously, and all transfers for this are
464 * queued up beforehand, so this just needs so chuck the incoming data onto
465 * the libsigrok session bus.
467 static void receive_transfer(struct libusb_transfer *transfer)
469 struct sr_datafeed_packet packet;
470 struct sr_datafeed_analog analog;
473 int num_probes, data_offset, i;
475 ctx = transfer->user_data;
476 sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes",
477 transfer->status, transfer->actual_length);
479 if (transfer->actual_length == 0)
480 /* Nothing to send to the bus. */
483 ctx->current_transfer += transfer->actual_length;
484 sr_dbg("hantek-dso: got %d of %d in frame", ctx->current_transfer, ctx->framesize * 2);
486 num_probes = (ctx->ch1_enabled && ctx->ch2_enabled) ? 2 : 1;
487 packet.type = SR_DF_ANALOG;
488 packet.payload = &analog;
489 /* TODO: support for 5xxx series 9-bit samples */
490 analog.num_samples = transfer->actual_length / 2;
491 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
493 for (i = 0; i < analog.num_samples; i++) {
494 /* The device always sends data for both channels. If a channel
495 * is disabled, it contains a copy of the enabled channel's
496 * data. However, we only send the requested channels to the bus.
498 /* TODO: support for 5xxx series 9-bit samples */
499 if (ctx->ch1_enabled) {
500 ch1 = (*(transfer->buffer + i * 2 + 1) / 255.0);
501 analog.data[data_offset++] = ch1;
503 if (ctx->ch2_enabled) {
504 ch2 = (*(transfer->buffer + i * 2) / 255.0);
505 analog.data[data_offset++] = ch2;
508 g_free(transfer->buffer);
509 libusb_free_transfer(transfer);
510 sr_session_send(ctx->cb_data, &packet);
512 if (ctx->current_transfer >= ctx->framesize * 2) {
513 /* That's the last chunk in this frame. */
514 packet.type = SR_DF_FRAME_END;
515 sr_session_send(ctx->cb_data, &packet);
517 if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) {
518 /* Terminate session */
519 /* TODO: don't leave pending USB transfers hanging */
520 packet.type = SR_DF_END;
521 sr_session_send(ctx->cb_data, &packet);
523 ctx->current_transfer = 0;
524 ctx->dev_state = NEW_CAPTURE;
530 static int handle_event(int fd, int revents, void *cb_data)
532 struct sr_datafeed_packet packet;
537 /* Avoid compiler warnings. */
541 /* Always handle pending libusb events. */
542 tv.tv_sec = tv.tv_usec = 0;
543 libusb_handle_events_timeout(usb_context, &tv);
547 if (ctx->dev_state == NEW_CAPTURE) {
548 if (dso_capture_start(ctx) != SR_OK)
550 if (dso_enable_trigger(ctx) != SR_OK)
552 // if (dso_force_trigger(ctx) != SR_OK)
554 sr_dbg("hantek-dso: successfully requested next chunk");
555 ctx->dev_state = CAPTURE;
558 if (ctx->dev_state != CAPTURE)
561 if ((capturestate = dso_get_capturestate(ctx)) == CAPTURE_UNKNOWN) {
562 /* Generated by the function, not the hardware. */
566 sr_dbg("hantek-dso: capturestate %d", capturestate);
567 switch (capturestate) {
569 if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) {
570 ctx->capture_empty_count = 0;
571 if (dso_capture_start(ctx) != SR_OK)
573 if (dso_enable_trigger(ctx) != SR_OK)
575 // if (dso_force_trigger(ctx) != SR_OK)
577 sr_dbg("hantek-dso: successfully requested next chunk");
580 case CAPTURE_FILLING:
583 case CAPTURE_READY_8BIT:
584 /* Tell the scope to send us the first frame. */
585 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
588 /* Don't hit the state machine again until we're done fetching
589 * the data we just told the scope to send.
591 ctx->dev_state = FETCH_DATA;
593 /* Tell the frontend a new frame is on the way. */
594 packet.type = SR_DF_FRAME_BEGIN;
595 sr_session_send(cb_data, &packet);
597 case CAPTURE_READY_9BIT:
599 sr_err("not yet supported");
601 case CAPTURE_TIMEOUT:
602 /* Doesn't matter, we'll try again next time. */
605 sr_dbg("unknown capture state");
611 static int hw_start_acquisition(int device_index, void *cb_data)
613 const struct libusb_pollfd **lupfd;
614 struct sr_datafeed_packet packet;
615 struct sr_datafeed_header header;
616 struct sr_datafeed_meta_analog meta;
617 struct sr_dev_inst *sdi;
621 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
624 if (sdi->status != SR_ST_ACTIVE)
628 ctx->cb_data = cb_data;
630 if (dso_init(ctx) != SR_OK)
633 if (dso_capture_start(ctx) != SR_OK)
636 ctx->dev_state = CAPTURE;
637 lupfd = libusb_get_pollfds(usb_context);
638 for (i = 0; lupfd[i]; i++)
639 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event,
643 /* Send header packet to the session bus. */
644 packet.type = SR_DF_HEADER;
645 packet.payload = (unsigned char *)&header;
646 header.feed_version = 1;
647 gettimeofday(&header.starttime, NULL);
648 sr_session_send(cb_data, &packet);
650 /* Send metadata about the SR_DF_ANALOG packets to come. */
651 packet.type = SR_DF_META_ANALOG;
652 packet.payload = &meta;
653 meta.num_probes = ctx->profile->num_probes;
654 sr_session_send(cb_data, &packet);
659 /* TODO: doesn't really cancel pending transfers so they might come in after
662 static int hw_stop_acquisition(int device_index, gpointer session_device_id)
664 struct sr_datafeed_packet packet;
665 struct sr_dev_inst *sdi;
668 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
671 if (sdi->status != SR_ST_ACTIVE)
675 ctx->dev_state = IDLE;
677 packet.type = SR_DF_END;
678 sr_session_send(session_device_id, &packet);
683 SR_PRIV struct sr_dev_driver hantek_dso_plugin_info = {
684 .name = "hantek-dso",
685 .longname = "Hantek DSO",
688 .cleanup = hw_cleanup,
689 .dev_open = hw_dev_open,
690 .dev_close = hw_dev_close,
691 .dev_info_get = hw_get_device_info,
692 .dev_status_get = hw_get_status,
693 .hwcap_get_all = hwcap_get_all,
694 .dev_config_set = hw_dev_config_set,
695 .dev_acquisition_start = hw_start_acquisition,
696 .dev_acquisition_stop = hw_stop_acquisition,