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