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