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