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