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