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