]> sigrok.org Git - libsigrok.git/blame - hardware/chronovu-la8/api.c
sr: remove obsolete SR_DI_INST
[libsigrok.git] / hardware / chronovu-la8 / api.c
CommitLineData
b908f067
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <ftdi.h>
22#include <glib.h>
23#include <string.h>
45c59c8b
BV
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
b908f067
UH
26#include "driver.h"
27
765ef2f7
BV
28SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
29static struct sr_dev_driver *cdi = &chronovu_la8_driver_info;
b908f067 30
74e5f12d
UH
31/*
32 * The ChronoVu LA8 can have multiple PIDs. Older versions shipped with
33 * a standard FTDI USB VID/PID of 0403:6001, newer ones have 0403:8867.
34 */
35static const uint16_t usb_pids[] = {
36 0x6001,
37 0x8867,
38};
39
b908f067 40/* Function prototypes. */
3ffb6964
BV
41static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
42 void *cb_data);
b908f067 43
c4f3ed4b
BV
44static void clear_instances(void)
45{
46 GSList *l;
47 struct sr_dev_inst *sdi;
48 struct context *ctx;
49
50 /* Properly close all devices. */
51 for (l = cdi->instances; l; l = l->next) {
52 if (!(sdi = l->data)) {
53 /* Log error, but continue cleaning up the rest. */
54 sr_err("la8: %s: sdi was NULL, continuing", __func__);
55 continue;
56 }
57 if (sdi->priv) {
58 ctx = sdi->priv;
59 ftdi_free(ctx->ftdic);
60 g_free(ctx);
61 }
62 sr_dev_inst_free(sdi);
63 }
64 g_slist_free(cdi->instances);
65 cdi->instances = NULL;
66
67}
68
40dda2c3 69static int hw_init(void)
61136ea6
BV
70{
71
72 /* Nothing to do. */
73
74 return SR_OK;
75}
76
c4f3ed4b 77static GSList *hw_scan(GSList *options)
b908f067 78{
b908f067 79 struct sr_dev_inst *sdi;
87ca93c5 80 struct sr_probe *probe;
b908f067 81 struct context *ctx;
c4f3ed4b 82 GSList *devices;
9c4311c5
BV
83 unsigned int i;
84 int ret;
c4f3ed4b
BV
85
86 (void)options;
87 devices = NULL;
b908f067 88
b908f067
UH
89 /* Allocate memory for our private driver context. */
90 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
91 sr_err("la8: %s: struct context malloc failed", __func__);
92 goto err_free_nothing;
93 }
94
95 /* Set some sane defaults. */
96 ctx->ftdic = NULL;
97 ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */
98 ctx->limit_msec = 0;
99 ctx->limit_samples = 0;
100 ctx->session_dev_id = NULL;
101 memset(ctx->mangled_buf, 0, BS);
102 ctx->final_buf = NULL;
103 ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */
104 ctx->trigger_mask = 0x00; /* All probes are "don't care". */
105 ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */
106 ctx->trigger_found = 0;
107 ctx->done = 0;
108 ctx->block_counter = 0;
109 ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */
d67b663e 110 ctx->usb_pid = 0;
b908f067
UH
111
112 /* Allocate memory where we'll store the de-mangled data. */
113 if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) {
114 sr_err("la8: %s: final_buf malloc failed", __func__);
115 goto err_free_ctx;
116 }
117
118 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
119 if (!(ctx->ftdic = ftdi_new())) {
120 sr_err("la8: %s: ftdi_new failed", __func__);
121 goto err_free_final_buf;
122 }
123
124 /* Check for the device and temporarily open it. */
74e5f12d
UH
125 for (i = 0; i < ARRAY_SIZE(usb_pids); i++) {
126 sr_dbg("la8: Probing for VID/PID %04x:%04x.", USB_VENDOR_ID,
127 usb_pids[i]);
128 ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
129 usb_pids[i], USB_DESCRIPTION, NULL);
130 if (ret == 0) {
131 sr_dbg("la8: Found LA8 device (%04x:%04x).",
132 USB_VENDOR_ID, usb_pids[i]);
133 ctx->usb_pid = usb_pids[i];
134 }
b908f067 135 }
74e5f12d
UH
136
137 if (ctx->usb_pid == 0)
138 goto err_free_ftdic;
b908f067
UH
139
140 /* Register the device with libsigrok. */
141 sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
142 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
143 if (!sdi) {
144 sr_err("la8: %s: sr_dev_inst_new failed", __func__);
145 goto err_close_ftdic;
146 }
cfe8a84d 147 sdi->driver = cdi;
b908f067
UH
148 sdi->priv = ctx;
149
87ca93c5
BV
150 for (i = 0; probe_names[i]; i++) {
151 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
152 probe_names[i])))
153 return NULL;
154 sdi->probes = g_slist_append(sdi->probes, probe);
155 }
156
c4f3ed4b 157 devices = g_slist_append(devices, sdi);
765ef2f7 158 cdi->instances = g_slist_append(cdi->instances, sdi);
b908f067
UH
159
160 sr_spew("la8: Device init successful.");
161
162 /* Close device. We'll reopen it again when we need it. */
163 (void) la8_close(ctx); /* Log, but ignore errors. */
164
c4f3ed4b 165 return devices;
b908f067
UH
166
167err_close_ftdic:
168 (void) la8_close(ctx); /* Log, but ignore errors. */
169err_free_ftdic:
170 free(ctx->ftdic); /* NOT g_free()! */
171err_free_final_buf:
172 g_free(ctx->final_buf);
173err_free_ctx:
174 g_free(ctx);
175err_free_nothing:
176
c4f3ed4b 177 return NULL;
b908f067
UH
178}
179
25a0f108 180static int hw_dev_open(struct sr_dev_inst *sdi)
b908f067 181{
b908f067 182 struct context *ctx;
25a0f108 183 int ret;
b908f067
UH
184
185 if (!(ctx = sdi->priv)) {
186 sr_err("la8: %s: sdi->priv was NULL", __func__);
187 return SR_ERR_BUG;
188 }
189
190 sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
74e5f12d 191 ctx->usb_pid);
b908f067
UH
192
193 /* Open the device. */
194 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
74e5f12d 195 ctx->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
b908f067
UH
196 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
197 __func__, ret, ftdi_get_error_string(ctx->ftdic));
198 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
199 return SR_ERR;
200 }
201 sr_dbg("la8: Device opened successfully.");
202
203 /* Purge RX/TX buffers in the FTDI chip. */
204 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
205 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
206 __func__, ret, ftdi_get_error_string(ctx->ftdic));
207 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
208 goto err_dev_open_close_ftdic;
209 }
210 sr_dbg("la8: FTDI buffers purged successfully.");
211
212 /* Enable flow control in the FTDI chip. */
213 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
214 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
215 __func__, ret, ftdi_get_error_string(ctx->ftdic));
216 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
217 goto err_dev_open_close_ftdic;
218 }
219 sr_dbg("la8: FTDI flow control enabled successfully.");
220
221 /* Wait 100ms. */
222 g_usleep(100 * 1000);
223
224 sdi->status = SR_ST_ACTIVE;
225
226 return SR_OK;
227
228err_dev_open_close_ftdic:
229 (void) la8_close(ctx); /* Log, but ignore errors. */
230 return SR_ERR;
231}
232
25a0f108 233static int hw_dev_close(struct sr_dev_inst *sdi)
b908f067 234{
b908f067
UH
235 struct context *ctx;
236
b908f067
UH
237 if (!(ctx = sdi->priv)) {
238 sr_err("la8: %s: sdi->priv was NULL", __func__);
239 return SR_ERR_BUG;
240 }
241
242 sr_dbg("la8: Closing device.");
243
244 if (sdi->status == SR_ST_ACTIVE) {
245 sr_dbg("la8: Status ACTIVE, closing device.");
246 /* TODO: Really ignore errors here, or return SR_ERR? */
247 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
248 } else {
249 sr_spew("la8: Status not ACTIVE, nothing to do.");
250 }
251
252 sdi->status = SR_ST_INACTIVE;
253
254 sr_dbg("la8: Freeing sample buffer.");
255 g_free(ctx->final_buf);
256
257 return SR_OK;
258}
259
260static int hw_cleanup(void)
261{
b908f067 262
c4f3ed4b 263 clear_instances();
b908f067 264
c4f3ed4b 265 return SR_OK;
b908f067
UH
266}
267
6a2761fd
BV
268static int hw_info_get(int info_id, const void **data,
269 const struct sr_dev_inst *sdi)
b908f067 270{
b908f067 271 struct context *ctx;
b908f067 272
6a2761fd 273 switch (info_id) {
7566601c
BV
274 case SR_DI_HWCAPS:
275 *data = hwcaps;
276 break;
b908f067 277 case SR_DI_NUM_PROBES:
6a2761fd 278 *data = GINT_TO_POINTER(NUM_PROBES);
b908f067
UH
279 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
280 NUM_PROBES);
281 break;
282 case SR_DI_PROBE_NAMES:
6a2761fd 283 *data = probe_names;
b908f067
UH
284 sr_spew("la8: %s: Returning probenames.", __func__);
285 break;
286 case SR_DI_SAMPLERATES:
287 fill_supported_samplerates_if_needed();
6a2761fd 288 *data = &samplerates;
b908f067
UH
289 sr_spew("la8: %s: Returning samplerates.", __func__);
290 break;
291 case SR_DI_TRIGGER_TYPES:
6a2761fd 292 *data = (char *)TRIGGER_TYPES;
b908f067
UH
293 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
294 TRIGGER_TYPES);
295 break;
296 case SR_DI_CUR_SAMPLERATE:
6a2761fd
BV
297 if (sdi) {
298 ctx = sdi->priv;
299 *data = &ctx->cur_samplerate;
300 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
301 __func__, ctx->cur_samplerate);
302 } else
303 return SR_ERR;
b908f067 304 break;
cfe8a84d
BV
305 default:
306 return SR_ERR_ARG;
b908f067
UH
307 }
308
6a2761fd 309 return SR_OK;
b908f067
UH
310}
311
6f4b1868
BV
312static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
313 const void *value)
b908f067 314{
b908f067
UH
315 struct context *ctx;
316
b908f067
UH
317 if (!(ctx = sdi->priv)) {
318 sr_err("la8: %s: sdi->priv was NULL", __func__);
319 return SR_ERR_BUG;
320 }
321
b908f067
UH
322 switch (hwcap) {
323 case SR_HWCAP_SAMPLERATE:
324 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
325 sr_err("la8: %s: setting samplerate failed.", __func__);
326 return SR_ERR;
327 }
328 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
329 break;
330 case SR_HWCAP_PROBECONFIG:
331 if (configure_probes(ctx, (const GSList *)value) != SR_OK) {
332 sr_err("la8: %s: probe config failed.", __func__);
333 return SR_ERR;
334 }
335 break;
336 case SR_HWCAP_LIMIT_MSEC:
337 if (*(const uint64_t *)value == 0) {
338 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
339 return SR_ERR;
340 }
341 ctx->limit_msec = *(const uint64_t *)value;
342 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
343 break;
344 case SR_HWCAP_LIMIT_SAMPLES:
345 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
346 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
347 return SR_ERR;
348 }
349 ctx->limit_samples = *(const uint64_t *)value;
350 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
351 break;
352 default:
353 /* Unknown capability, return SR_ERR. */
354 sr_err("la8: %s: Unknown capability.", __func__);
355 return SR_ERR;
356 break;
357 }
358
359 return SR_OK;
360}
361
362static int receive_data(int fd, int revents, void *cb_data)
363{
364 int i, ret;
365 struct sr_dev_inst *sdi;
366 struct context *ctx;
367
368 /* Avoid compiler errors. */
369 (void)fd;
370 (void)revents;
371
372 if (!(sdi = cb_data)) {
373 sr_err("la8: %s: cb_data was NULL", __func__);
374 return FALSE;
375 }
376
377 if (!(ctx = sdi->priv)) {
378 sr_err("la8: %s: sdi->priv was NULL", __func__);
379 return FALSE;
380 }
381
382 if (!ctx->ftdic) {
383 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
384 return FALSE;
385 }
386
387 /* Get one block of data. */
388 if ((ret = la8_read_block(ctx)) < 0) {
389 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
3ffb6964 390 hw_dev_acquisition_stop(sdi, sdi);
b908f067
UH
391 return FALSE;
392 }
393
394 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
395 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
396 ctx->block_counter++;
397 return TRUE;
398 }
399
400 sr_dbg("la8: Sampling finished, sending data to session bus now.");
401
402 /* All data was received and demangled, send it to the session bus. */
403 for (i = 0; i < NUM_BLOCKS; i++)
404 send_block_to_session_bus(ctx, i);
405
3ffb6964 406 hw_dev_acquisition_stop(sdi, sdi);
b908f067
UH
407
408 // return FALSE; /* FIXME? */
409 return TRUE;
410}
411
3ffb6964
BV
412static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
413 void *cb_data)
b908f067 414{
b908f067
UH
415 struct context *ctx;
416 struct sr_datafeed_packet packet;
417 struct sr_datafeed_header header;
418 struct sr_datafeed_meta_logic meta;
419 uint8_t buf[4];
420 int bytes_written;
421
b908f067
UH
422 if (!(ctx = sdi->priv)) {
423 sr_err("la8: %s: sdi->priv was NULL", __func__);
424 return SR_ERR_BUG;
425 }
426
427 if (!ctx->ftdic) {
428 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
429 return SR_ERR_BUG;
430 }
431
432 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
433 if (ctx->divcount == 0xff) {
434 sr_err("la8: %s: invalid divcount/samplerate", __func__);
435 return SR_ERR;
436 }
437
438 sr_dbg("la8: Starting acquisition.");
439
440 /* Fill acquisition parameters into buf[]. */
441 buf[0] = ctx->divcount;
442 buf[1] = 0xff; /* This byte must always be 0xff. */
443 buf[2] = ctx->trigger_pattern;
444 buf[3] = ctx->trigger_mask;
445
446 /* Start acquisition. */
447 bytes_written = la8_write(ctx, buf, 4);
448
449 if (bytes_written < 0) {
450 sr_err("la8: Acquisition failed to start.");
451 return SR_ERR;
452 } else if (bytes_written != 4) {
453 sr_err("la8: Acquisition failed to start.");
454 return SR_ERR; /* TODO: Other error and return code? */
455 }
456
457 sr_dbg("la8: Acquisition started successfully.");
458
459 ctx->session_dev_id = cb_data;
460
461 /* Send header packet to the session bus. */
462 sr_dbg("la8: Sending SR_DF_HEADER.");
463 packet.type = SR_DF_HEADER;
464 packet.payload = &header;
465 header.feed_version = 1;
466 gettimeofday(&header.starttime, NULL);
467 sr_session_send(ctx->session_dev_id, &packet);
468
469 /* Send metadata about the SR_DF_LOGIC packets to come. */
470 packet.type = SR_DF_META_LOGIC;
471 packet.payload = &meta;
472 meta.samplerate = ctx->cur_samplerate;
473 meta.num_probes = NUM_PROBES;
474 sr_session_send(ctx->session_dev_id, &packet);
475
476 /* Time when we should be done (for detecting trigger timeouts). */
477 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
478 + ctx->trigger_timeout;
479 ctx->block_counter = 0;
480 ctx->trigger_found = 0;
481
482 /* Hook up a dummy handler to receive data from the LA8. */
3ffb6964 483 sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
b908f067
UH
484
485 return SR_OK;
486}
487
3ffb6964
BV
488static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
489 void *cb_data)
b908f067 490{
b908f067
UH
491 struct context *ctx;
492 struct sr_datafeed_packet packet;
493
494 sr_dbg("la8: Stopping acquisition.");
495
b908f067
UH
496 if (!(ctx = sdi->priv)) {
497 sr_err("la8: %s: sdi->priv was NULL", __func__);
498 return SR_ERR_BUG;
499 }
500
501 /* Send end packet to the session bus. */
502 sr_dbg("la8: Sending SR_DF_END.");
503 packet.type = SR_DF_END;
504 sr_session_send(cb_data, &packet);
505
506 return SR_OK;
507}
508
509SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
510 .name = "chronovu-la8",
511 .longname = "ChronoVu LA8",
512 .api_version = 1,
513 .init = hw_init,
514 .cleanup = hw_cleanup,
61136ea6 515 .scan = hw_scan,
b908f067
UH
516 .dev_open = hw_dev_open,
517 .dev_close = hw_dev_close,
6a2761fd 518 .info_get = hw_info_get,
b908f067
UH
519 .dev_config_set = hw_dev_config_set,
520 .dev_acquisition_start = hw_dev_acquisition_start,
521 .dev_acquisition_stop = hw_dev_acquisition_stop,
765ef2f7 522 .instances = NULL,
b908f067 523};