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