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