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