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