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