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,
49 static const char *probe_names[] = {
55 static struct dso_profile dev_profiles[] = {
60 FIRMWARE_DIR "/hantek-dso-2090.fw" },
61 { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
65 SR_PRIV libusb_context *usb_context = NULL;
66 SR_PRIV GSList *dev_insts = NULL;
69 static struct sr_dev_inst *dso_dev_new(int index, struct dso_profile *prof)
71 struct sr_dev_inst *sdi;
74 sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
75 prof->vendor, prof->model, prof->model_version);
79 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
80 sr_err("hantek-dso: ctx malloc failed");
84 ctx->dev_state = IDLE;
85 ctx->timebase = DEFAULT_TIMEBASE;
86 ctx->ch1_enabled = TRUE;
87 ctx->ch2_enabled = TRUE;
88 ctx->voltage_ch1 = DEFAULT_VOLTAGE;
89 ctx->voltage_ch2 = DEFAULT_VOLTAGE;
90 ctx->coupling_ch1 = DEFAULT_COUPLING;
91 ctx->coupling_ch2 = DEFAULT_COUPLING;
92 ctx->voffset_ch1 = DEFAULT_VERT_OFFSET;
93 ctx->voffset_ch2 = DEFAULT_VERT_OFFSET;
94 ctx->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
95 ctx->selected_channel = DEFAULT_SELECTED_CHANNEL;
96 ctx->framesize = DEFAULT_FRAMESIZE;
97 ctx->triggerslope = SLOPE_POSITIVE;
98 ctx->triggersource = DEFAULT_TRIGGER_SOURCE;
99 ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
101 dev_insts = g_slist_append(dev_insts, sdi);
106 static int configure_probes(struct context *ctx, GSList *probes)
108 struct sr_probe *probe;
111 for (l = probes; l; l = l->next) {
112 probe = (struct sr_probe *)l->data;
113 if (probe->index == 0)
114 ctx->ch1_enabled = probe->enabled;
115 else if (probe->index == 1)
116 ctx->ch2_enabled = probe->enabled;
122 static int hw_init(const char *devinfo)
124 struct sr_dev_inst *sdi;
125 struct libusb_device_descriptor des;
126 struct dso_profile *prof;
128 libusb_device **devlist;
129 int err, devcnt, i, j;
131 /* Avoid compiler warnings. */
134 if (libusb_init(&usb_context) != 0) {
135 sr_err("hantek-dso: Failed to initialize USB.");
139 /* Find all Hantek DSO devices and upload firmware to all of them. */
141 libusb_get_device_list(usb_context, &devlist);
142 for (i = 0; devlist[i]; i++) {
143 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
144 sr_err("hantek-dso: failed to get device descriptor: %d", err);
149 for (j = 0; dev_profiles[j].orig_vid; j++) {
150 if (des.idVendor == dev_profiles[j].orig_vid
151 && des.idProduct == dev_profiles[j].orig_pid) {
152 /* Device matches the pre-firmware profile. */
153 prof = &dev_profiles[j];
154 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
155 sdi = dso_dev_new(devcnt, prof);
157 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
158 prof->firmware) == SR_OK)
159 /* Remember when the firmware on this device was updated */
160 g_get_current_time(&ctx->fw_updated);
162 sr_err("hantek-dso: firmware upload failed for "
163 "device %d", devcnt);
164 /* Dummy USB address of 0xff will get overwritten later. */
165 ctx->usb = sr_usb_dev_inst_new(
166 libusb_get_bus_number(devlist[i]), 0xff, NULL);
169 } else if (des.idVendor == dev_profiles[j].fw_vid
170 && des.idProduct == dev_profiles[j].fw_pid) {
171 /* Device matches the post-firmware profile. */
172 prof = &dev_profiles[j];
173 sr_dbg("hantek-dso: Found a %s %s.", prof->vendor, prof->model);
174 sdi = dso_dev_new(devcnt, prof);
175 sdi->status = SR_ST_INACTIVE;
177 ctx->usb = sr_usb_dev_inst_new(
178 libusb_get_bus_number(devlist[i]),
179 libusb_get_device_address(devlist[i]), NULL);
185 /* not a supported VID/PID */
188 libusb_free_device_list(devlist, 1);
193 static int hw_dev_open(int dev_index)
196 struct sr_dev_inst *sdi;
200 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
205 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
206 * for the FX2 to renumerate
209 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
210 sr_info("hantek-dso: waiting for device to reset");
211 /* takes at least 300ms for the FX2 to be gone from the USB bus */
212 g_usleep(300 * 1000);
214 while (timediff < MAX_RENUM_DELAY) {
215 if ((err = dso_open(dev_index)) == SR_OK)
217 g_usleep(100 * 1000);
218 g_get_current_time(&cur_time);
219 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
221 sr_info("hantek-dso: device came back after %d ms", timediff);
223 err = dso_open(dev_index);
227 sr_err("hantek-dso: unable to open device");
231 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
233 sr_err("hantek-dso: Unable to claim interface: %d", err);
240 static int hw_dev_close(int dev_index)
242 struct sr_dev_inst *sdi;
244 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
252 static int hw_cleanup(void)
255 struct sr_dev_inst *sdi;
258 /* Properly close and free all devices. */
259 for (l = dev_insts; l; l = l->next) {
260 if (!(sdi = l->data)) {
261 /* Log error, but continue cleaning up the rest. */
262 sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
265 if (!(ctx = sdi->priv)) {
266 /* Log error, but continue cleaning up the rest. */
267 sr_err("hantek-dso: %s: sdi->priv was NULL, continuing", __func__);
271 sr_usb_dev_inst_free(ctx->usb);
272 sr_dev_inst_free(sdi);
275 g_slist_free(dev_insts);
279 libusb_exit(usb_context);
285 static void *hw_get_device_info(int dev_index, int dev_info_id)
287 struct sr_dev_inst *sdi;
292 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
297 switch (dev_info_id) {
301 case SR_DI_NUM_PROBES:
302 info = GINT_TO_POINTER(ctx->profile->num_probes);
304 case SR_DI_PROBE_NAMES:
307 /* TODO remove this */
308 case SR_DI_CUR_SAMPLERATE:
316 static int hw_get_status(int device_index)
318 struct sr_dev_inst *sdi;
320 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
321 return SR_ST_NOT_FOUND;
326 static int *hwcap_get_all(void)
332 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
334 struct sr_dev_inst *sdi;
338 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
341 if (sdi->status != SR_ST_ACTIVE)
346 case SR_HWCAP_LIMIT_FRAMES:
347 ctx->limit_frames = *(uint64_t *)value;
349 case SR_HWCAP_PROBECONFIG:
350 ret = configure_probes(ctx, (GSList *) value);
352 case SR_HWCAP_TRIGGERSLOPE:
354 if (tmp != SLOPE_NEGATIVE && tmp != SLOPE_POSITIVE)
356 ctx->triggerslope = tmp;
364 /* Called by libusb (as triggered by handle_event()) when a transfer comes in.
365 * Only channel data comes in asynchronously, and all transfers for this are
366 * queued up beforehand, so this just needs so chuck the incoming data onto
367 * the libsigrok session bus.
369 static void receive_transfer(struct libusb_transfer *transfer)
371 struct sr_datafeed_packet packet;
372 struct sr_datafeed_analog analog;
377 ctx = transfer->user_data;
378 sr_dbg("hantek-dso: receive_transfer(): status %d received %d bytes",
379 transfer->status, transfer->actual_length);
381 if (transfer->actual_length == 0)
382 /* Nothing to send to the bus. */
385 ctx->current_transfer += transfer->actual_length;
386 sr_dbg("hantek-dso: got %d of %d in frame", ctx->current_transfer, ctx->framesize * 2);
388 packet.type = SR_DF_ANALOG;
389 packet.payload = &analog;
390 analog.num_samples = transfer->actual_length / 2;
391 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * ctx->profile->num_probes);
392 for (i = 0; i < analog.num_samples; i++) {
393 /* Hardcoded for two channels, since the order/encoding is specific. */
394 /* TODO: support for 5xxx series 9-bit samples */
395 ch2 = (*(transfer->buffer + i * 2) / 255.0);
396 ch1 = (*(transfer->buffer + i * 2 + 1) / 255.0);
397 analog.data[i * ctx->profile->num_probes] = ch1;
398 analog.data[i * ctx->profile->num_probes + 1] = ch2;
400 g_free(transfer->buffer);
401 libusb_free_transfer(transfer);
402 sr_session_send(ctx->cb_data, &packet);
404 if (ctx->current_transfer >= ctx->framesize * 2) {
405 /* That's the last chunk in this frame. */
406 packet.type = SR_DF_FRAME_END;
407 sr_session_send(ctx->cb_data, &packet);
409 if (ctx->limit_frames && ++ctx->num_frames == ctx->limit_frames) {
410 /* Terminate session */
411 packet.type = SR_DF_END;
412 sr_session_send(ctx->cb_data, &packet);
414 ctx->current_transfer = 0;
415 ctx->dev_state = NEW_CAPTURE;
421 static int handle_event(int fd, int revents, void *cb_data)
423 struct sr_datafeed_packet packet;
428 /* Avoid compiler warnings. */
432 /* Always handle pending libusb events. */
433 tv.tv_sec = tv.tv_usec = 0;
434 libusb_handle_events_timeout(usb_context, &tv);
438 if (ctx->dev_state == NEW_CAPTURE) {
439 if (dso_capture_start(ctx) != SR_OK)
441 if (dso_enable_trigger(ctx) != SR_OK)
443 if (dso_force_trigger(ctx) != SR_OK)
445 sr_dbg("hantek-dso: successfully requested next chunk");
446 ctx->dev_state = CAPTURE;
449 if (ctx->dev_state != CAPTURE)
452 if ((capturestate = dso_get_capturestate(ctx)) == CAPTURE_UNKNOWN) {
453 /* Generated by the function, not the hardware. */
457 sr_dbg("hantek-dso: capturestate %d", capturestate);
458 switch (capturestate) {
460 if (++ctx->capture_empty_count >= MAX_CAPTURE_EMPTY) {
461 ctx->capture_empty_count = 0;
462 if (dso_capture_start(ctx) != SR_OK)
464 if (dso_enable_trigger(ctx) != SR_OK)
466 if (dso_force_trigger(ctx) != SR_OK)
468 sr_dbg("hantek-dso: successfully requested next chunk");
471 case CAPTURE_FILLING:
474 case CAPTURE_READY_8BIT:
475 /* Tell the scope to send us the first frame. */
476 if (dso_get_channeldata(ctx, receive_transfer) != SR_OK)
479 /* Don't hit the state machine again until we're done fetching
480 * the data we just told the scope to send.
482 ctx->dev_state = FETCH_DATA;
484 /* Tell the frontend a new frame is on the way. */
485 packet.type = SR_DF_FRAME_BEGIN;
486 sr_session_send(cb_data, &packet);
488 case CAPTURE_READY_9BIT:
490 sr_err("not yet supported");
492 case CAPTURE_TIMEOUT:
493 /* Doesn't matter, we'll try again next time. */
496 sr_dbg("unknown capture state");
502 static int hw_start_acquisition(int device_index, void *cb_data)
504 const struct libusb_pollfd **lupfd;
505 struct sr_datafeed_packet packet;
506 struct sr_datafeed_header header;
507 struct sr_datafeed_meta_analog meta;
508 struct sr_dev_inst *sdi;
512 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
515 if (sdi->status != SR_ST_ACTIVE)
519 ctx->cb_data = cb_data;
521 if (dso_init(ctx) != SR_OK)
524 if (dso_capture_start(ctx) != SR_OK)
527 ctx->dev_state = CAPTURE;
528 lupfd = libusb_get_pollfds(usb_context);
529 for (i = 0; lupfd[i]; i++)
530 sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK, handle_event,
534 /* Send header packet to the session bus. */
535 packet.type = SR_DF_HEADER;
536 packet.payload = (unsigned char *)&header;
537 header.feed_version = 1;
538 gettimeofday(&header.starttime, NULL);
539 sr_session_send(cb_data, &packet);
541 /* Send metadata about the SR_DF_ANALOG packets to come. */
542 packet.type = SR_DF_META_ANALOG;
543 packet.payload = &meta;
544 meta.num_probes = ctx->profile->num_probes;
545 sr_session_send(cb_data, &packet);
550 /* TODO: doesn't really cancel pending transfers so they might come in after
553 static int hw_stop_acquisition(int device_index, gpointer session_device_id)
555 struct sr_datafeed_packet packet;
556 struct sr_dev_inst *sdi;
559 if (!(sdi = sr_dev_inst_get(dev_insts, device_index)))
562 if (sdi->status != SR_ST_ACTIVE)
566 ctx->dev_state = IDLE;
568 packet.type = SR_DF_END;
569 sr_session_send(session_device_id, &packet);
574 SR_PRIV struct sr_dev_driver hantek_dso_plugin_info = {
575 .name = "hantek-dso",
576 .longname = "Hantek DSO",
579 .cleanup = hw_cleanup,
580 .dev_open = hw_dev_open,
581 .dev_close = hw_dev_close,
582 .dev_info_get = hw_get_device_info,
583 .dev_status_get = hw_get_status,
584 .hwcap_get_all = hwcap_get_all,
585 .dev_config_set = hw_dev_config_set,
586 .dev_acquisition_start = hw_start_acquisition,
587 .dev_acquisition_stop = hw_stop_acquisition,