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