2 * This file is part of the sigrok project.
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
28 static GSList *dev_insts = NULL;
30 /* Function prototypes. */
31 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
33 static int hw_init(const char *devinfo)
36 struct sr_dev_inst *sdi;
39 /* Avoid compiler errors. */
42 /* Allocate memory for our private driver context. */
43 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
44 sr_err("la8: %s: struct context malloc failed", __func__);
45 goto err_free_nothing;
48 /* Set some sane defaults. */
50 ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
52 ctx->limit_samples = 0;
53 ctx->session_dev_id = NULL;
54 memset(ctx->mangled_buf, 0, BS);
55 ctx->final_buf = NULL;
56 ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
57 ctx->trigger_mask = 0x00; /* All probes are "don't care". */
58 ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */
59 ctx->trigger_found = 0;
61 ctx->block_counter = 0;
62 ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */
64 /* Allocate memory where we'll store the de-mangled data. */
65 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
66 sr_err("la8: %s: final_buf malloc failed", __func__);
70 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
71 if (!(ctx->ftdic = ftdi_new())) {
72 sr_err("la8: %s: ftdi_new failed", __func__);
73 goto err_free_final_buf;
76 /* Check for the device and temporarily open it. */
77 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
78 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
79 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
82 sr_dbg("la8: Found LA8 device (%04x:%04x).", USB_VENDOR_ID,
85 /* Register the device with libsigrok. */
86 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
87 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
89 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
95 dev_insts = g_slist_append(dev_insts, sdi);
97 sr_spew("la8: Device init successful.");
99 /* Close device. We'll reopen it again when we need it. */
100 (void) la8_close(ctx); /* Log, but ignore errors. */
105 (void) la8_close(ctx); /* Log, but ignore errors. */
107 free(ctx->ftdic); /* NOT g_free()! */
109 g_free(ctx->final_buf);
117 static int hw_dev_open(int dev_index)
120 struct sr_dev_inst *sdi;
123 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
124 sr_err("la8: %s: sdi was NULL", __func__);
128 if (!(ctx = sdi->priv)) {
129 sr_err("la8: %s: sdi->priv was NULL", __func__);
133 sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
136 /* Open the device. */
137 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
138 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
139 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
140 __func__, ret, ftdi_get_error_string(ctx->ftdic));
141 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
144 sr_dbg("la8: Device opened successfully.");
146 /* Purge RX/TX buffers in the FTDI chip. */
147 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
148 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
149 __func__, ret, ftdi_get_error_string(ctx->ftdic));
150 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
151 goto err_dev_open_close_ftdic;
153 sr_dbg("la8: FTDI buffers purged successfully.");
155 /* Enable flow control in the FTDI chip. */
156 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
157 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
158 __func__, ret, ftdi_get_error_string(ctx->ftdic));
159 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
160 goto err_dev_open_close_ftdic;
162 sr_dbg("la8: FTDI flow control enabled successfully.");
165 g_usleep(100 * 1000);
167 sdi->status = SR_ST_ACTIVE;
171 err_dev_open_close_ftdic:
172 (void) la8_close(ctx); /* Log, but ignore errors. */
176 static int hw_dev_close(int dev_index)
178 struct sr_dev_inst *sdi;
181 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
182 sr_err("la8: %s: sdi was NULL", __func__);
186 if (!(ctx = sdi->priv)) {
187 sr_err("la8: %s: sdi->priv was NULL", __func__);
191 sr_dbg("la8: Closing device.");
193 if (sdi->status == SR_ST_ACTIVE) {
194 sr_dbg("la8: Status ACTIVE, closing device.");
195 /* TODO: Really ignore errors here, or return SR_ERR? */
196 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
198 sr_spew("la8: Status not ACTIVE, nothing to do.");
201 sdi->status = SR_ST_INACTIVE;
203 sr_dbg("la8: Freeing sample buffer.");
204 g_free(ctx->final_buf);
209 static int hw_cleanup(void)
212 struct sr_dev_inst *sdi;
215 /* Properly close all devices. */
216 for (l = dev_insts; l; l = l->next) {
217 if (!(sdi = l->data)) {
218 /* Log error, but continue cleaning up the rest. */
219 sr_err("la8: %s: sdi was NULL, continuing", __func__);
223 sr_dev_inst_free(sdi); /* Returns void. */
225 g_slist_free(dev_insts); /* Returns void. */
231 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
233 struct sr_dev_inst *sdi;
237 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
238 sr_err("la8: %s: sdi was NULL", __func__);
242 if (!(ctx = sdi->priv)) {
243 sr_err("la8: %s: sdi->priv was NULL", __func__);
247 sr_spew("la8: %s: dev_index %d, dev_info_id %d.", __func__,
248 dev_index, dev_info_id);
250 switch (dev_info_id) {
253 sr_spew("la8: %s: Returning sdi.", __func__);
255 case SR_DI_NUM_PROBES:
256 info = GINT_TO_POINTER(NUM_PROBES);
257 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
260 case SR_DI_PROBE_NAMES:
262 sr_spew("la8: %s: Returning probenames.", __func__);
264 case SR_DI_SAMPLERATES:
265 fill_supported_samplerates_if_needed();
267 sr_spew("la8: %s: Returning samplerates.", __func__);
269 case SR_DI_TRIGGER_TYPES:
270 info = TRIGGER_TYPES;
271 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
274 case SR_DI_CUR_SAMPLERATE:
275 info = &ctx->cur_samplerate;
276 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
277 __func__, ctx->cur_samplerate);
280 /* Unknown device info ID, return NULL. */
281 sr_err("la8: %s: Unknown device info ID", __func__);
289 static int hw_dev_status_get(int dev_index)
291 struct sr_dev_inst *sdi;
293 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
294 sr_err("la8: %s: sdi was NULL, device not found", __func__);
295 return SR_ST_NOT_FOUND;
298 sr_dbg("la8: Returning status: %d.", sdi->status);
303 static const int *hw_hwcap_get_all(void)
305 sr_spew("la8: Returning list of device capabilities.");
310 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
312 struct sr_dev_inst *sdi;
315 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
316 sr_err("la8: %s: sdi was NULL", __func__);
320 if (!(ctx = sdi->priv)) {
321 sr_err("la8: %s: sdi->priv was NULL", __func__);
325 sr_spew("la8: %s: dev_index %d, hwcap %d", __func__, dev_index, hwcap);
328 case SR_HWCAP_SAMPLERATE:
329 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
330 sr_err("la8: %s: setting samplerate failed.", __func__);
333 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
335 case SR_HWCAP_PROBECONFIG:
336 if (configure_probes(ctx, (const GSList *)value) != SR_OK) {
337 sr_err("la8: %s: probe config failed.", __func__);
341 case SR_HWCAP_LIMIT_MSEC:
342 if (*(const uint64_t *)value == 0) {
343 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
346 ctx->limit_msec = *(const uint64_t *)value;
347 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
349 case SR_HWCAP_LIMIT_SAMPLES:
350 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
351 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
354 ctx->limit_samples = *(const uint64_t *)value;
355 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
358 /* Unknown capability, return SR_ERR. */
359 sr_err("la8: %s: Unknown capability.", __func__);
367 static int receive_data(int fd, int revents, void *cb_data)
370 struct sr_dev_inst *sdi;
373 /* Avoid compiler errors. */
377 if (!(sdi = cb_data)) {
378 sr_err("la8: %s: cb_data was NULL", __func__);
382 if (!(ctx = sdi->priv)) {
383 sr_err("la8: %s: sdi->priv was NULL", __func__);
388 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
392 /* Get one block of data. */
393 if ((ret = la8_read_block(ctx)) < 0) {
394 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
395 hw_dev_acquisition_stop(sdi->index, sdi);
399 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
400 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
401 ctx->block_counter++;
405 sr_dbg("la8: Sampling finished, sending data to session bus now.");
407 /* All data was received and demangled, send it to the session bus. */
408 for (i = 0; i < NUM_BLOCKS; i++)
409 send_block_to_session_bus(ctx, i);
411 hw_dev_acquisition_stop(sdi->index, sdi);
413 // return FALSE; /* FIXME? */
417 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
419 struct sr_dev_inst *sdi;
421 struct sr_datafeed_packet packet;
422 struct sr_datafeed_header header;
423 struct sr_datafeed_meta_logic meta;
427 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
428 sr_err("la8: %s: sdi was NULL", __func__);
432 if (!(ctx = sdi->priv)) {
433 sr_err("la8: %s: sdi->priv was NULL", __func__);
438 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
442 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
443 if (ctx->divcount == 0xff) {
444 sr_err("la8: %s: invalid divcount/samplerate", __func__);
448 sr_dbg("la8: Starting acquisition.");
450 /* Fill acquisition parameters into buf[]. */
451 buf[0] = ctx->divcount;
452 buf[1] = 0xff; /* This byte must always be 0xff. */
453 buf[2] = ctx->trigger_pattern;
454 buf[3] = ctx->trigger_mask;
456 /* Start acquisition. */
457 bytes_written = la8_write(ctx, buf, 4);
459 if (bytes_written < 0) {
460 sr_err("la8: Acquisition failed to start.");
462 } else if (bytes_written != 4) {
463 sr_err("la8: Acquisition failed to start.");
464 return SR_ERR; /* TODO: Other error and return code? */
467 sr_dbg("la8: Acquisition started successfully.");
469 ctx->session_dev_id = cb_data;
471 /* Send header packet to the session bus. */
472 sr_dbg("la8: Sending SR_DF_HEADER.");
473 packet.type = SR_DF_HEADER;
474 packet.payload = &header;
475 header.feed_version = 1;
476 gettimeofday(&header.starttime, NULL);
477 sr_session_send(ctx->session_dev_id, &packet);
479 /* Send metadata about the SR_DF_LOGIC packets to come. */
480 packet.type = SR_DF_META_LOGIC;
481 packet.payload = &meta;
482 meta.samplerate = ctx->cur_samplerate;
483 meta.num_probes = NUM_PROBES;
484 sr_session_send(ctx->session_dev_id, &packet);
486 /* Time when we should be done (for detecting trigger timeouts). */
487 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
488 + ctx->trigger_timeout;
489 ctx->block_counter = 0;
490 ctx->trigger_found = 0;
492 /* Hook up a dummy handler to receive data from the LA8. */
493 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
498 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
500 struct sr_dev_inst *sdi;
502 struct sr_datafeed_packet packet;
504 sr_dbg("la8: Stopping acquisition.");
506 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
507 sr_err("la8: %s: sdi was NULL", __func__);
511 if (!(ctx = sdi->priv)) {
512 sr_err("la8: %s: sdi->priv was NULL", __func__);
516 /* Send end packet to the session bus. */
517 sr_dbg("la8: Sending SR_DF_END.");
518 packet.type = SR_DF_END;
519 sr_session_send(cb_data, &packet);
524 SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
525 .name = "chronovu-la8",
526 .longname = "ChronoVu LA8",
529 .cleanup = hw_cleanup,
530 .dev_open = hw_dev_open,
531 .dev_close = hw_dev_close,
532 .dev_info_get = hw_dev_info_get,
533 .dev_status_get = hw_dev_status_get,
534 .hwcap_get_all = hw_hwcap_get_all,
535 .dev_config_set = hw_dev_config_set,
536 .dev_acquisition_start = hw_dev_acquisition_start,
537 .dev_acquisition_stop = hw_dev_acquisition_stop,