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