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