]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/chronovu-la8/chronovu-la8.c
sr: No need for dynamic hardware driver registration.
[libsigrok.git] / hardware / chronovu-la8 / chronovu-la8.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <ftdi.h>
22#include <glib.h>
23#include <string.h>
24#include <stdlib.h>
25#include "sigrok.h"
26#include "sigrok-internal.h"
27
28#define USB_VENDOR_ID 0x0403
29#define USB_PRODUCT_ID 0x6001
30#define USB_DESCRIPTION "ChronoVu LA8"
31#define USB_VENDOR_NAME "ChronoVu"
32#define USB_MODEL_NAME "LA8"
33#define USB_MODEL_VERSION ""
34
35#define NUM_PROBES 8
36#define TRIGGER_TYPES "01"
37#define SDRAM_SIZE (8 * 1024 * 1024)
38#define MIN_NUM_SAMPLES 1
39
40#define BS 4096 /* Block size */
41#define NUM_BLOCKS 2048 /* Number of blocks */
42
43static GSList *dev_insts = NULL;
44
45static const char *probe_names[NUM_PROBES + 1] = {
46 "0",
47 "1",
48 "2",
49 "3",
50 "4",
51 "5",
52 "6",
53 "7",
54 NULL,
55};
56
57/* Private, per-device-instance driver context. */
58struct context {
59 /** FTDI device context (used by libftdi). */
60 struct ftdi_context *ftdic;
61
62 /** The currently configured samplerate of the device. */
63 uint64_t cur_samplerate;
64
65 /** The current sampling limit (in ms). */
66 uint64_t limit_msec;
67
68 /** The current sampling limit (in number of samples). */
69 uint64_t limit_samples;
70
71 /** TODO */
72 gpointer session_id;
73
74 /**
75 * A buffer containing some (mangled) samples from the device.
76 * Format: Pretty mangled-up (due to hardware reasons), see code.
77 */
78 uint8_t mangled_buf[BS];
79
80 /**
81 * An 8MB buffer where we'll store the de-mangled samples.
82 * Format: Each sample is 1 byte, MSB is channel 7, LSB is channel 0.
83 */
84 uint8_t *final_buf;
85
86 /**
87 * Trigger pattern (MSB = channel 7, LSB = channel 0).
88 * A 1 bit matches a high signal, 0 matches a low signal on a probe.
89 * Only low/high triggers (but not e.g. rising/falling) are supported.
90 */
91 uint8_t trigger_pattern;
92
93 /**
94 * Trigger mask (MSB = channel 7, LSB = channel 0).
95 * A 1 bit means "must match trigger_pattern", 0 means "don't care".
96 */
97 uint8_t trigger_mask;
98
99 /** Time (in seconds) before the trigger times out. */
100 uint64_t trigger_timeout;
101
102 /** Tells us whether an SR_DF_TRIGGER packet was already sent. */
103 int trigger_found;
104
105 /** TODO */
106 time_t done;
107
108 /** Counter/index for the data block to be read. */
109 int block_counter;
110
111 /** The divcount value (determines the sample period) for the LA8. */
112 uint8_t divcount;
113};
114
115/* This will be initialized via hw_dev_info_get()/SR_DI_SAMPLERATES. */
116static uint64_t supported_samplerates[255 + 1] = { 0 };
117
118/*
119 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
120 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
121 */
122static struct sr_samplerates samplerates = {
123 .low = 0,
124 .high = 0,
125 .step = 0,
126 .list = supported_samplerates,
127};
128
129/* Note: Continuous sampling is not supported by the hardware. */
130static int hwcaps[] = {
131 SR_HWCAP_LOGIC_ANALYZER,
132 SR_HWCAP_SAMPLERATE,
133 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
134 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
135 0,
136};
137
138/* Function prototypes. */
139static int la8_close_usb_reset_sequencer(struct context *ctx);
140static int hw_dev_acquisition_stop(int dev_index, gpointer session_data);
141static int la8_reset(struct context *ctx);
142
143static void fill_supported_samplerates_if_needed(void)
144{
145 int i;
146
147 /* Do nothing if supported_samplerates[] is already filled. */
148 if (supported_samplerates[0] != 0)
149 return;
150
151 /* Fill supported_samplerates[] with the proper values. */
152 for (i = 0; i < 255; i++)
153 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
154 supported_samplerates[255] = 0;
155}
156
157/**
158 * Check if the given samplerate is supported by the LA8 hardware.
159 *
160 * @param samplerate The samplerate (in Hz) to check.
161 * @return 1 if the samplerate is supported/valid, 0 otherwise.
162 */
163static int is_valid_samplerate(uint64_t samplerate)
164{
165 int i;
166
167 fill_supported_samplerates_if_needed();
168
169 for (i = 0; i < 255; i++) {
170 if (supported_samplerates[i] == samplerate)
171 return 1;
172 }
173
174 sr_err("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
175 __func__, samplerate);
176
177 return 0;
178}
179
180/**
181 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
182 *
183 * LA8 hardware: sample period = (divcount + 1) * 10ns.
184 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
185 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
186 *
187 * @param samplerate The samplerate in Hz.
188 * @return The divcount value as needed by the hardware, or 0xff upon errors.
189 */
190static uint8_t samplerate_to_divcount(uint64_t samplerate)
191{
192 if (samplerate == 0) {
193 sr_err("la8: %s: samplerate was 0", __func__);
194 return 0xff;
195 }
196
197 if (!is_valid_samplerate(samplerate)) {
198 sr_err("la8: %s: can't get divcount, samplerate invalid",
199 __func__);
200 return 0xff;
201 }
202
203 return (SR_MHZ(100) / samplerate) - 1;
204}
205
206/**
207 * Write data of a certain length to the LA8's FTDI device.
208 *
209 * @param ctx The struct containing private per-device-instance data. Must not
210 * be NULL. ctx->ftdic must not be NULL either.
211 * @param buf The buffer containing the data to write. Must not be NULL.
212 * @param size The number of bytes to write. Must be >= 0.
213 * @return The number of bytes written, or a negative value upon errors.
214 */
215static int la8_write(struct context *ctx, uint8_t *buf, int size)
216{
217 int bytes_written;
218
219 /* Note: Caller checked that ctx and ctx->ftdic != NULL. */
220
221 if (!buf) {
222 sr_err("la8: %s: buf was NULL", __func__);
223 return SR_ERR_ARG;
224 }
225
226 if (size < 0) {
227 sr_err("la8: %s: size was < 0", __func__);
228 return SR_ERR_ARG;
229 }
230
231 bytes_written = ftdi_write_data(ctx->ftdic, buf, size);
232
233 if (bytes_written < 0) {
234 sr_err("la8: %s: ftdi_write_data: (%d) %s", __func__,
235 bytes_written, ftdi_get_error_string(ctx->ftdic));
236 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
237 } else if (bytes_written != size) {
238 sr_err("la8: %s: bytes to write: %d, bytes written: %d",
239 __func__, size, bytes_written);
240 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
241 }
242
243 return bytes_written;
244}
245
246/**
247 * Read a certain amount of bytes from the LA8's FTDI device.
248 *
249 * @param ctx The struct containing private per-device-instance data. Must not
250 * be NULL. ctx->ftdic must not be NULL either.
251 * @param buf The buffer where the received data will be stored. Must not
252 * be NULL.
253 * @param size The number of bytes to read. Must be >= 1.
254 * @return The number of bytes read, or a negative value upon errors.
255 */
256static int la8_read(struct context *ctx, uint8_t *buf, int size)
257{
258 int bytes_read;
259
260 /* Note: Caller checked that ctx and ctx->ftdic != NULL. */
261
262 if (!buf) {
263 sr_err("la8: %s: buf was NULL", __func__);
264 return SR_ERR_ARG;
265 }
266
267 if (size <= 0) {
268 sr_err("la8: %s: size was <= 0", __func__);
269 return SR_ERR_ARG;
270 }
271
272 bytes_read = ftdi_read_data(ctx->ftdic, buf, size);
273
274 if (bytes_read < 0) {
275 sr_err("la8: %s: ftdi_read_data: (%d) %s", __func__,
276 bytes_read, ftdi_get_error_string(ctx->ftdic));
277 } else if (bytes_read != size) {
278 // sr_err("la8: %s: bytes to read: %d, bytes read: %d",
279 // __func__, size, bytes_read);
280 }
281
282 return bytes_read;
283}
284
285static int la8_close(struct context *ctx)
286{
287 int ret;
288
289 if (!ctx) {
290 sr_err("la8: %s: ctx was NULL", __func__);
291 return SR_ERR_ARG;
292 }
293
294 if (!ctx->ftdic) {
295 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
296 return SR_ERR_ARG;
297 }
298
299 if ((ret = ftdi_usb_close(ctx->ftdic)) < 0) {
300 sr_err("la8: %s: ftdi_usb_close: (%d) %s",
301 __func__, ret, ftdi_get_error_string(ctx->ftdic));
302 }
303
304 return ret;
305}
306
307/**
308 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
309 *
310 * @param ctx The struct containing private per-device-instance data.
311 * @return SR_OK upon success, SR_ERR upon failure.
312 */
313static int la8_close_usb_reset_sequencer(struct context *ctx)
314{
315 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
316 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
317 int ret;
318
319 if (!ctx) {
320 sr_err("la8: %s: ctx was NULL", __func__);
321 return SR_ERR_ARG;
322 }
323
324 if (!ctx->ftdic) {
325 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
326 return SR_ERR_ARG;
327 }
328
329 if (ctx->ftdic->usb_dev) {
330 /* Reset the LA8 sequencer logic, then wait 100ms. */
331 sr_dbg("la8: resetting sequencer logic");
332 (void) la8_write(ctx, buf, 8); /* Ignore errors. */
333 g_usleep(100 * 1000);
334
335 /* Purge FTDI buffers, then reset and close the FTDI device. */
336 sr_dbg("la8: purging buffers, resetting+closing FTDI device");
337
338 /* Log errors, but ignore them (i.e., don't abort). */
339 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0)
340 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
341 __func__, ret, ftdi_get_error_string(ctx->ftdic));
342 if ((ret = ftdi_usb_reset(ctx->ftdic)) < 0)
343 sr_err("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
344 ret, ftdi_get_error_string(ctx->ftdic));
345 if ((ret = ftdi_usb_close(ctx->ftdic)) < 0)
346 sr_err("la8: %s: ftdi_usb_close: (%d) %s", __func__,
347 ret, ftdi_get_error_string(ctx->ftdic));
348 }
349
350 ftdi_free(ctx->ftdic); /* Returns void. */
351 ctx->ftdic = NULL;
352
353 return SR_OK;
354}
355
356/**
357 * Reset the ChronoVu LA8.
358 *
359 * The LA8 must be reset after a failed read/write operation or upon timeouts.
360 *
361 * @param ctx The struct containing private per-device-instance data.
362 * @return SR_OK upon success, SR_ERR upon failure.
363 */
364static int la8_reset(struct context *ctx)
365{
366 uint8_t buf[BS];
367 time_t done, now;
368 int bytes_read;
369
370 if (!ctx) {
371 sr_err("la8: %s: ctx was NULL", __func__);
372 return SR_ERR_ARG;
373 }
374
375 if (!ctx->ftdic) {
376 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
377 return SR_ERR_ARG;
378 }
379
380 sr_dbg("la8: resetting the device");
381
382 /*
383 * Purge pending read data from the FTDI hardware FIFO until
384 * no more data is left, or a timeout occurs (after 20s).
385 */
386 done = 20 + time(NULL);
387 do {
388 /* TODO: Ignore errors? Check for < 0 at least! */
389 bytes_read = la8_read(ctx, (uint8_t *)&buf, BS);
390 now = time(NULL);
391 } while ((done > now) && (bytes_read > 0));
392
393 /* Reset the LA8 sequencer logic and close the USB port. */
394 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
395
396 sr_dbg("la8: device reset finished");
397
398 return SR_OK;
399}
400
401static int configure_probes(struct context *ctx, GSList *probes)
402{
403 struct sr_probe *probe;
404 GSList *l;
405 uint8_t probe_bit;
406 char *tc;
407
408 /* Note: Caller checked that ctx != NULL. */
409
410 ctx->trigger_pattern = 0;
411 ctx->trigger_mask = 0; /* Default to "don't care" for all probes. */
412
413 for (l = probes; l; l = l->next) {
414 probe = (struct sr_probe *)l->data;
415
416 if (!probe) {
417 sr_err("la8: %s: probe was NULL", __func__);
418 return SR_ERR;
419 }
420
421 /* Skip disabled probes. */
422 if (!probe->enabled)
423 continue;
424
425 /* Skip (enabled) probes with no configured trigger. */
426 if (!probe->trigger)
427 continue;
428
429 /* Note: Must only be run if probe->trigger != NULL. */
430 if (probe->index < 0 || probe->index > 7) {
431 sr_err("la8: %s: invalid probe index %d, must be "
432 "between 0 and 7", __func__, probe->index);
433 return SR_ERR;
434 }
435
436 probe_bit = (1 << (probe->index - 1));
437
438 /* Configure the probe's trigger mask and trigger pattern. */
439 for (tc = probe->trigger; tc && *tc; tc++) {
440 ctx->trigger_mask |= probe_bit;
441
442 /* Sanity check, LA8 only supports low/high trigger. */
443 if (*tc != '0' && *tc != '1') {
444 sr_err("la8: %s: invalid trigger '%c', only "
445 "'0'/'1' supported", __func__, *tc);
446 return SR_ERR;
447 }
448
449 if (*tc == '1')
450 ctx->trigger_pattern |= probe_bit;
451 }
452 }
453
454 sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
455 __func__, ctx->trigger_mask, ctx->trigger_pattern);
456
457 return SR_OK;
458}
459
460static int hw_init(const char *devinfo)
461{
462 int ret;
463 struct sr_dev_inst *sdi;
464 struct context *ctx;
465
466 /* Avoid compiler errors. */
467 (void)devinfo;
468
469 /* Allocate memory for our private driver context. */
470 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
471 sr_err("la8: %s: struct context malloc failed", __func__);
472 goto err_free_nothing;
473 }
474
475 /* Set some sane defaults. */
476 ctx->ftdic = NULL;
477 ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
478 ctx->limit_msec = 0;
479 ctx->limit_samples = 0;
480 ctx->session_id = NULL;
481 memset(ctx->mangled_buf, 0, BS);
482 ctx->final_buf = NULL;
483 ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
484 ctx->trigger_mask = 0x00; /* All probes are "don't care". */
485 ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */
486 ctx->trigger_found = 0;
487 ctx->done = 0;
488 ctx->block_counter = 0;
489 ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */
490
491 /* Allocate memory where we'll store the de-mangled data. */
492 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
493 sr_err("la8: %s: final_buf malloc failed", __func__);
494 goto err_free_ctx;
495 }
496
497 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
498 if (!(ctx->ftdic = ftdi_new())) {
499 sr_err("la8: %s: ftdi_new failed", __func__);
500 goto err_free_final_buf;
501 }
502
503 /* Check for the device and temporarily open it. */
504 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
505 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
506 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
507 goto err_free_ftdic;
508 }
509 sr_dbg("la8: found device");
510
511 /* Register the device with libsigrok. */
512 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
513 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
514 if (!sdi) {
515 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
516 goto err_close_ftdic;
517 }
518
519 sdi->priv = ctx;
520
521 dev_insts = g_slist_append(dev_insts, sdi);
522
523 sr_spew("la8: %s finished successfully", __func__);
524
525 /* Close device. We'll reopen it again when we need it. */
526 (void) la8_close(ctx); /* Log, but ignore errors. */
527
528 return 1;
529
530err_close_ftdic:
531 (void) la8_close(ctx); /* Log, but ignore errors. */
532err_free_ftdic:
533 free(ctx->ftdic); /* NOT g_free()! */
534err_free_final_buf:
535 g_free(ctx->final_buf);
536err_free_ctx:
537 g_free(ctx);
538err_free_nothing:
539
540 return 0;
541}
542
543static int hw_dev_open(int dev_index)
544{
545 int ret;
546 struct sr_dev_inst *sdi;
547 struct context *ctx;
548
549 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
550 sr_err("la8: %s: sdi was NULL", __func__);
551 return SR_ERR; /* TODO: SR_ERR_ARG? */
552 }
553
554 if (!(ctx = sdi->priv)) {
555 sr_err("la8: %s: sdi->priv was NULL", __func__);
556 return SR_ERR; /* TODO: SR_ERR_ARG? */
557 }
558
559 sr_dbg("la8: opening device");
560
561 /* Open the device. */
562 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
563 USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) {
564 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
565 __func__, ret, ftdi_get_error_string(ctx->ftdic));
566 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
567 return SR_ERR;
568 }
569 sr_dbg("la8: device opened successfully");
570
571 /* Purge RX/TX buffers in the FTDI chip. */
572 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
573 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
574 __func__, ret, ftdi_get_error_string(ctx->ftdic));
575 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
576 goto err_dev_open_close_ftdic;
577 }
578 sr_dbg("la8: FTDI buffers purged successfully");
579
580 /* Enable flow control in the FTDI chip. */
581 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
582 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
583 __func__, ret, ftdi_get_error_string(ctx->ftdic));
584 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
585 goto err_dev_open_close_ftdic;
586 }
587 sr_dbg("la8: FTDI flow control enabled successfully");
588
589 /* Wait 100ms. */
590 g_usleep(100 * 1000);
591
592 sdi->status = SR_ST_ACTIVE;
593
594 return SR_OK;
595
596err_dev_open_close_ftdic:
597 (void) la8_close(ctx); /* Log, but ignore errors. */
598 return SR_ERR;
599}
600
601static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
602{
603 struct context *ctx;
604
605 /* Note: Caller checked that sdi and sdi->priv != NULL. */
606
607 ctx = sdi->priv;
608
609 sr_spew("la8: setting samplerate");
610
611 fill_supported_samplerates_if_needed();
612
613 /* Check if this is a samplerate supported by the hardware. */
614 if (!is_valid_samplerate(samplerate))
615 return SR_ERR;
616
617 /* Set the new samplerate. */
618 ctx->cur_samplerate = samplerate;
619
620 sr_dbg("la8: samplerate set to %" PRIu64 "Hz", ctx->cur_samplerate);
621
622 return SR_OK;
623}
624
625static int hw_dev_close(int dev_index)
626{
627 struct sr_dev_inst *sdi;
628 struct context *ctx;
629
630 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
631 sr_err("la8: %s: sdi was NULL", __func__);
632 return SR_ERR; /* TODO: SR_ERR_ARG? */
633 }
634
635 if (!(ctx = sdi->priv)) {
636 sr_err("la8: %s: sdi->priv was NULL", __func__);
637 return SR_ERR; /* TODO: SR_ERR_ARG? */
638 }
639
640 sr_dbg("la8: closing device");
641
642 if (sdi->status == SR_ST_ACTIVE) {
643 sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
644 /* TODO: Really ignore errors here, or return SR_ERR? */
645 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
646 } else {
647 sr_spew("la8: %s: status not ACTIVE, nothing to do", __func__);
648 }
649
650 sdi->status = SR_ST_INACTIVE;
651
652 sr_dbg("la8: %s: freeing sample buffers", __func__);
653 g_free(ctx->final_buf);
654
655 return SR_OK;
656}
657
658static int hw_cleanup(void)
659{
660 GSList *l;
661 struct sr_dev_inst *sdi;
662 int ret = SR_OK;
663
664 /* Properly close all devices. */
665 for (l = dev_insts; l; l = l->next) {
666 if (!(sdi = l->data)) {
667 /* Log error, but continue cleaning up the rest. */
668 sr_err("la8: %s: sdi was NULL, continuing", __func__);
669 ret = SR_ERR_BUG;
670 continue;
671 }
672 sr_dev_inst_free(sdi); /* Returns void. */
673 }
674 g_slist_free(dev_insts); /* Returns void. */
675 dev_insts = NULL;
676
677 return ret;
678}
679
680static void *hw_dev_info_get(int dev_index, int dev_info_id)
681{
682 struct sr_dev_inst *sdi;
683 struct context *ctx;
684 void *info;
685
686 sr_spew("la8: entering %s", __func__);
687
688 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
689 sr_err("la8: %s: sdi was NULL", __func__);
690 return NULL;
691 }
692
693 if (!(ctx = sdi->priv)) {
694 sr_err("la8: %s: sdi->priv was NULL", __func__);
695 return NULL;
696 }
697
698 switch (dev_info_id) {
699 case SR_DI_INST:
700 info = sdi;
701 break;
702 case SR_DI_NUM_PROBES:
703 info = GINT_TO_POINTER(NUM_PROBES);
704 break;
705 case SR_DI_PROBE_NAMES:
706 info = probe_names;
707 break;
708 case SR_DI_SAMPLERATES:
709 fill_supported_samplerates_if_needed();
710 info = &samplerates;
711 break;
712 case SR_DI_TRIGGER_TYPES:
713 info = (char *)TRIGGER_TYPES;
714 break;
715 case SR_DI_CUR_SAMPLERATE:
716 info = &ctx->cur_samplerate;
717 break;
718 default:
719 /* Unknown device info ID, return NULL. */
720 sr_err("la8: %s: Unknown device info ID", __func__);
721 info = NULL;
722 break;
723 }
724
725 return info;
726}
727
728static int hw_dev_status_get(int dev_index)
729{
730 struct sr_dev_inst *sdi;
731
732 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
733 sr_err("la8: %s: sdi was NULL, device not found", __func__);
734 return SR_ST_NOT_FOUND;
735 }
736
737 sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
738
739 return sdi->status;
740}
741
742static int *hw_hwcap_get_all(void)
743{
744 sr_spew("la8: entering %s", __func__);
745
746 return hwcaps;
747}
748
749static int hw_dev_config_set(int dev_index, int hwcap, void *value)
750{
751 struct sr_dev_inst *sdi;
752 struct context *ctx;
753
754 sr_spew("la8: entering %s", __func__);
755
756 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
757 sr_err("la8: %s: sdi was NULL", __func__);
758 return SR_ERR; /* TODO: SR_ERR_ARG? */
759 }
760
761 if (!(ctx = sdi->priv)) {
762 sr_err("la8: %s: sdi->priv was NULL", __func__);
763 return SR_ERR; /* TODO: SR_ERR_ARG? */
764 }
765
766 switch (hwcap) {
767 case SR_HWCAP_SAMPLERATE:
768 if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
769 return SR_ERR;
770 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
771 break;
772 case SR_HWCAP_PROBECONFIG:
773 if (configure_probes(ctx, (GSList *)value) != SR_OK) {
774 sr_err("la8: %s: probe config failed", __func__);
775 return SR_ERR;
776 }
777 break;
778 case SR_HWCAP_LIMIT_MSEC:
779 if (*(uint64_t *)value == 0) {
780 sr_err("la8: %s: LIMIT_MSEC can't be 0", __func__);
781 return SR_ERR;
782 }
783 ctx->limit_msec = *(uint64_t *)value;
784 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
785 break;
786 case SR_HWCAP_LIMIT_SAMPLES:
787 if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
788 sr_err("la8: %s: LIMIT_SAMPLES too small", __func__);
789 return SR_ERR;
790 }
791 ctx->limit_samples = *(uint64_t *)value;
792 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
793 break;
794 default:
795 /* Unknown capability, return SR_ERR. */
796 sr_err("la8: %s: Unknown capability", __func__);
797 return SR_ERR;
798 break;
799 }
800
801 return SR_OK;
802}
803
804/**
805 * Get a block of data from the LA8.
806 *
807 * @param ctx The struct containing private per-device-instance data.
808 * @return SR_OK upon success, or SR_ERR upon errors.
809 */
810static int la8_read_block(struct context *ctx)
811{
812 int i, byte_offset, m, mi, p, index, bytes_read;
813 time_t now;
814
815 /* Note: Caller checked that ctx and ctx->ftdic != NULL. */
816
817 sr_spew("la8: %s: reading block %d", __func__, ctx->block_counter);
818
819 bytes_read = la8_read(ctx, ctx->mangled_buf, BS);
820
821 /* If first block read got 0 bytes, retry until success or timeout. */
822 if ((bytes_read == 0) && (ctx->block_counter == 0)) {
823 do {
824 sr_spew("la8: %s: reading block 0 again", __func__);
825 bytes_read = la8_read(ctx, ctx->mangled_buf, BS);
826 /* TODO: How to handle read errors here? */
827 now = time(NULL);
828 } while ((ctx->done > now) && (bytes_read == 0));
829 }
830
831 /* Check if block read was successful or a timeout occured. */
832 if (bytes_read != BS) {
833 sr_err("la8: %s: trigger timed out", __func__);
834 (void) la8_reset(ctx); /* Ignore errors. */
835 return SR_ERR;
836 }
837
838 /* De-mangle the data. */
839 sr_spew("la8: de-mangling samples of block %d", ctx->block_counter);
840 byte_offset = ctx->block_counter * BS;
841 m = byte_offset / (1024 * 1024);
842 mi = m * (1024 * 1024);
843 for (i = 0; i < BS; i++) {
844 p = i & (1 << 0);
845 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
846 index += (ctx->divcount == 0) ? p : (1 - p);
847 ctx->final_buf[index] = ctx->mangled_buf[i];
848 }
849
850 return SR_OK;
851}
852
853static void send_block_to_session_bus(struct context *ctx, int block)
854{
855 int i;
856 uint8_t sample, expected_sample;
857 struct sr_datafeed_packet packet;
858 struct sr_datafeed_logic logic;
859 int trigger_point; /* Relative trigger point (in this block). */
860
861 /* Note: No sanity checks on ctx/block, caller is responsible. */
862
863 /* Check if we can find the trigger condition in this block. */
864 trigger_point = -1;
865 expected_sample = ctx->trigger_pattern & ctx->trigger_mask;
866 for (i = 0; i < BS; i++) {
867 /* Don't continue if the trigger was found previously. */
868 if (ctx->trigger_found)
869 break;
870
871 /*
872 * Also, don't continue if triggers are "don't care", i.e. if
873 * no trigger conditions were specified by the user. In that
874 * case we don't want to send an SR_DF_TRIGGER packet at all.
875 */
876 if (ctx->trigger_mask == 0x00)
877 break;
878
879 sample = *(ctx->final_buf + (block * BS) + i);
880
881 if ((sample & ctx->trigger_mask) == expected_sample) {
882 trigger_point = i;
883 ctx->trigger_found = 1;
884 break;
885 }
886 }
887
888 /* If no trigger was found, send one SR_DF_LOGIC packet. */
889 if (trigger_point == -1) {
890 /* Send an SR_DF_LOGIC packet to the session bus. */
891 sr_spew("la8: sending SR_DF_LOGIC packet (%d bytes) for "
892 "block %d", BS, block);
893 packet.type = SR_DF_LOGIC;
894 packet.payload = &logic;
895 logic.length = BS;
896 logic.unitsize = 1;
897 logic.data = ctx->final_buf + (block * BS);
898 sr_session_bus(ctx->session_id, &packet);
899 return;
900 }
901
902 /*
903 * We found the trigger, so some special handling is needed. We have
904 * to send an SR_DF_LOGIC packet with the samples before the trigger
905 * (if any), then the SD_DF_TRIGGER packet itself, then another
906 * SR_DF_LOGIC packet with the samples after the trigger (if any).
907 */
908
909 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
910
911 /* If at least one sample is located before the trigger... */
912 if (trigger_point > 0) {
913 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
914 sr_spew("la8: sending pre-trigger SR_DF_LOGIC packet, "
915 "start = %d, length = %d", block * BS, trigger_point);
916 packet.type = SR_DF_LOGIC;
917 packet.payload = &logic;
918 logic.length = trigger_point;
919 logic.unitsize = 1;
920 logic.data = ctx->final_buf + (block * BS);
921 sr_session_bus(ctx->session_id, &packet);
922 }
923
924 /* Send the SR_DF_TRIGGER packet to the session bus. */
925 sr_spew("la8: sending SR_DF_TRIGGER packet, sample = %d",
926 (block * BS) + trigger_point);
927 packet.type = SR_DF_TRIGGER;
928 packet.payload = NULL;
929 sr_session_bus(ctx->session_id, &packet);
930
931 /* If at least one sample is located after the trigger... */
932 if (trigger_point < (BS - 1)) {
933 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
934 sr_spew("la8: sending post-trigger SR_DF_LOGIC packet, "
935 "start = %d, length = %d",
936 (block * BS) + trigger_point, BS - trigger_point);
937 packet.type = SR_DF_LOGIC;
938 packet.payload = &logic;
939 logic.length = BS - trigger_point;
940 logic.unitsize = 1;
941 logic.data = ctx->final_buf + (block * BS) + trigger_point;
942 sr_session_bus(ctx->session_id, &packet);
943 }
944}
945
946static int receive_data(int fd, int revents, void *session_data)
947{
948 int i, ret;
949 struct sr_dev_inst *sdi;
950 struct context *ctx;
951
952 /* Avoid compiler errors. */
953 (void)fd;
954 (void)revents;
955
956 if (!(sdi = session_data)) {
957 sr_err("la8: %s: session_data was NULL", __func__);
958 return FALSE;
959 }
960
961 if (!(ctx = sdi->priv)) {
962 sr_err("la8: %s: sdi->priv was NULL", __func__);
963 return FALSE;
964 }
965
966 if (!ctx->ftdic) {
967 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
968 return FALSE;
969 }
970
971 /* Get one block of data. */
972 if ((ret = la8_read_block(ctx)) < 0) {
973 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
974 hw_dev_acquisition_stop(sdi->index, session_data);
975 return FALSE;
976 }
977
978 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
979 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
980 ctx->block_counter++;
981 return TRUE;
982 }
983
984 sr_dbg("la8: sampling finished, sending data to session bus now");
985
986 /* All data was received and demangled, send it to the session bus. */
987 for (i = 0; i < NUM_BLOCKS; i++)
988 send_block_to_session_bus(ctx, i);
989
990 hw_dev_acquisition_stop(sdi->index, session_data);
991
992 // return FALSE; /* FIXME? */
993 return TRUE;
994}
995
996static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
997{
998 struct sr_dev_inst *sdi;
999 struct context *ctx;
1000 struct sr_datafeed_packet packet;
1001 struct sr_datafeed_header header;
1002 uint8_t buf[4];
1003 int bytes_written;
1004
1005 sr_spew("la8: entering %s", __func__);
1006
1007 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
1008 sr_err("la8: %s: sdi was NULL", __func__);
1009 return SR_ERR; /* TODO: SR_ERR_ARG? */
1010 }
1011
1012 if (!(ctx = sdi->priv)) {
1013 sr_err("la8: %s: sdi->priv was NULL", __func__);
1014 return SR_ERR; /* TODO: SR_ERR_ARG? */
1015 }
1016
1017 if (!ctx->ftdic) {
1018 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
1019 return SR_ERR_ARG;
1020 }
1021
1022 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
1023 if (ctx->divcount == 0xff) {
1024 sr_err("la8: %s: invalid divcount/samplerate", __func__);
1025 return SR_ERR;
1026 }
1027
1028 /* Fill acquisition parameters into buf[]. */
1029 buf[0] = ctx->divcount;
1030 buf[1] = 0xff; /* This byte must always be 0xff. */
1031 buf[2] = ctx->trigger_pattern;
1032 buf[3] = ctx->trigger_mask;
1033
1034 /* Start acquisition. */
1035 bytes_written = la8_write(ctx, buf, 4);
1036
1037 if (bytes_written < 0) {
1038 sr_err("la8: acquisition failed to start");
1039 return SR_ERR;
1040 } else if (bytes_written != 4) {
1041 sr_err("la8: acquisition failed to start");
1042 return SR_ERR; /* TODO: Other error and return code? */
1043 }
1044
1045 sr_dbg("la8: acquisition started successfully");
1046
1047 ctx->session_id = session_data;
1048
1049 /* Send header packet to the session bus. */
1050 sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
1051 packet.type = SR_DF_HEADER;
1052 packet.payload = &header;
1053 header.feed_version = 1;
1054 gettimeofday(&header.starttime, NULL);
1055 header.samplerate = ctx->cur_samplerate;
1056 header.num_logic_probes = NUM_PROBES;
1057 sr_session_bus(session_data, &packet);
1058
1059 /* Time when we should be done (for detecting trigger timeouts). */
1060 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
1061 + ctx->trigger_timeout;
1062 ctx->block_counter = 0;
1063 ctx->trigger_found = 0;
1064
1065 /* Hook up a dummy handler to receive data from the LA8. */
1066 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
1067
1068 return SR_OK;
1069}
1070
1071static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
1072{
1073 struct sr_dev_inst *sdi;
1074 struct context *ctx;
1075 struct sr_datafeed_packet packet;
1076
1077 sr_dbg("la8: stopping acquisition");
1078
1079 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
1080 sr_err("la8: %s: sdi was NULL", __func__);
1081 return SR_ERR_BUG;
1082 }
1083
1084 if (!(ctx = sdi->priv)) {
1085 sr_err("la8: %s: sdi->priv was NULL", __func__);
1086 return SR_ERR_BUG;
1087 }
1088
1089 /* Send end packet to the session bus. */
1090 sr_dbg("la8: %s: sending SR_DF_END", __func__);
1091 packet.type = SR_DF_END;
1092 sr_session_bus(session_data, &packet);
1093
1094 return SR_OK;
1095}
1096
1097SR_PRIV struct sr_dev_plugin chronovu_la8_plugin_info = {
1098 .name = "chronovu-la8",
1099 .longname = "ChronoVu LA8",
1100 .api_version = 1,
1101 .init = hw_init,
1102 .cleanup = hw_cleanup,
1103 .dev_open = hw_dev_open,
1104 .dev_close = hw_dev_close,
1105 .dev_info_get = hw_dev_info_get,
1106 .dev_status_get = hw_dev_status_get,
1107 .hwcap_get_all = hw_hwcap_get_all,
1108 .dev_config_set = hw_dev_config_set,
1109 .dev_acquisition_start = hw_dev_acquisition_start,
1110 .dev_acquisition_stop = hw_dev_acquisition_stop,
1111};