]> sigrok.org Git - libsigrok.git/blame - hardware/chronovu-la8/api.c
sr/drivers: use sr_dev_inst instead of device index for dev_config_set()
[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
171static int hw_dev_open(int dev_index)
172{
173 int ret;
174 struct sr_dev_inst *sdi;
175 struct context *ctx;
176
765ef2f7 177 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
b908f067
UH
178 sr_err("la8: %s: sdi was NULL", __func__);
179 return SR_ERR_BUG;
180 }
181
182 if (!(ctx = sdi->priv)) {
183 sr_err("la8: %s: sdi->priv was NULL", __func__);
184 return SR_ERR_BUG;
185 }
186
187 sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
74e5f12d 188 ctx->usb_pid);
b908f067
UH
189
190 /* Open the device. */
191 if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
74e5f12d 192 ctx->usb_pid, USB_DESCRIPTION, NULL)) < 0) {
b908f067
UH
193 sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s",
194 __func__, ret, ftdi_get_error_string(ctx->ftdic));
195 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
196 return SR_ERR;
197 }
198 sr_dbg("la8: Device opened successfully.");
199
200 /* Purge RX/TX buffers in the FTDI chip. */
201 if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
202 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
203 __func__, ret, ftdi_get_error_string(ctx->ftdic));
204 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
205 goto err_dev_open_close_ftdic;
206 }
207 sr_dbg("la8: FTDI buffers purged successfully.");
208
209 /* Enable flow control in the FTDI chip. */
210 if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
211 sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s",
212 __func__, ret, ftdi_get_error_string(ctx->ftdic));
213 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
214 goto err_dev_open_close_ftdic;
215 }
216 sr_dbg("la8: FTDI flow control enabled successfully.");
217
218 /* Wait 100ms. */
219 g_usleep(100 * 1000);
220
221 sdi->status = SR_ST_ACTIVE;
222
223 return SR_OK;
224
225err_dev_open_close_ftdic:
226 (void) la8_close(ctx); /* Log, but ignore errors. */
227 return SR_ERR;
228}
229
230static int hw_dev_close(int dev_index)
231{
232 struct sr_dev_inst *sdi;
233 struct context *ctx;
234
765ef2f7 235 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
b908f067
UH
236 sr_err("la8: %s: sdi was NULL", __func__);
237 return SR_ERR_BUG;
238 }
239
240 if (!(ctx = sdi->priv)) {
241 sr_err("la8: %s: sdi->priv was NULL", __func__);
242 return SR_ERR_BUG;
243 }
244
245 sr_dbg("la8: Closing device.");
246
247 if (sdi->status == SR_ST_ACTIVE) {
248 sr_dbg("la8: Status ACTIVE, closing device.");
249 /* TODO: Really ignore errors here, or return SR_ERR? */
250 (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
251 } else {
252 sr_spew("la8: Status not ACTIVE, nothing to do.");
253 }
254
255 sdi->status = SR_ST_INACTIVE;
256
257 sr_dbg("la8: Freeing sample buffer.");
258 g_free(ctx->final_buf);
259
260 return SR_OK;
261}
262
263static int hw_cleanup(void)
264{
b908f067 265
c4f3ed4b 266 clear_instances();
b908f067 267
c4f3ed4b 268 return SR_OK;
b908f067
UH
269}
270
6a2761fd
BV
271static int hw_info_get(int info_id, const void **data,
272 const struct sr_dev_inst *sdi)
b908f067 273{
b908f067 274 struct context *ctx;
b908f067 275
6a2761fd 276 switch (info_id) {
b908f067 277 case SR_DI_INST:
6a2761fd 278 *data = sdi;
b908f067
UH
279 sr_spew("la8: %s: Returning sdi.", __func__);
280 break;
7566601c
BV
281 case SR_DI_HWCAPS:
282 *data = hwcaps;
283 break;
b908f067 284 case SR_DI_NUM_PROBES:
6a2761fd 285 *data = GINT_TO_POINTER(NUM_PROBES);
b908f067
UH
286 sr_spew("la8: %s: Returning number of probes: %d.", __func__,
287 NUM_PROBES);
288 break;
289 case SR_DI_PROBE_NAMES:
6a2761fd 290 *data = probe_names;
b908f067
UH
291 sr_spew("la8: %s: Returning probenames.", __func__);
292 break;
293 case SR_DI_SAMPLERATES:
294 fill_supported_samplerates_if_needed();
6a2761fd 295 *data = &samplerates;
b908f067
UH
296 sr_spew("la8: %s: Returning samplerates.", __func__);
297 break;
298 case SR_DI_TRIGGER_TYPES:
6a2761fd 299 *data = (char *)TRIGGER_TYPES;
b908f067
UH
300 sr_spew("la8: %s: Returning trigger types: %s.", __func__,
301 TRIGGER_TYPES);
302 break;
303 case SR_DI_CUR_SAMPLERATE:
6a2761fd
BV
304 if (sdi) {
305 ctx = sdi->priv;
306 *data = &ctx->cur_samplerate;
307 sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
308 __func__, ctx->cur_samplerate);
309 } else
310 return SR_ERR;
b908f067 311 break;
cfe8a84d
BV
312 default:
313 return SR_ERR_ARG;
b908f067
UH
314 }
315
6a2761fd 316 return SR_OK;
b908f067
UH
317}
318
319static int hw_dev_status_get(int dev_index)
320{
321 struct sr_dev_inst *sdi;
322
765ef2f7 323 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
b908f067
UH
324 sr_err("la8: %s: sdi was NULL, device not found", __func__);
325 return SR_ST_NOT_FOUND;
326 }
327
328 sr_dbg("la8: Returning status: %d.", sdi->status);
329
330 return sdi->status;
331}
332
6f4b1868
BV
333static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
334 const void *value)
b908f067 335{
b908f067
UH
336 struct context *ctx;
337
b908f067
UH
338 if (!(ctx = sdi->priv)) {
339 sr_err("la8: %s: sdi->priv was NULL", __func__);
340 return SR_ERR_BUG;
341 }
342
b908f067
UH
343 switch (hwcap) {
344 case SR_HWCAP_SAMPLERATE:
345 if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) {
346 sr_err("la8: %s: setting samplerate failed.", __func__);
347 return SR_ERR;
348 }
349 sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
350 break;
351 case SR_HWCAP_PROBECONFIG:
352 if (configure_probes(ctx, (const GSList *)value) != SR_OK) {
353 sr_err("la8: %s: probe config failed.", __func__);
354 return SR_ERR;
355 }
356 break;
357 case SR_HWCAP_LIMIT_MSEC:
358 if (*(const uint64_t *)value == 0) {
359 sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
360 return SR_ERR;
361 }
362 ctx->limit_msec = *(const uint64_t *)value;
363 sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec);
364 break;
365 case SR_HWCAP_LIMIT_SAMPLES:
366 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
367 sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
368 return SR_ERR;
369 }
370 ctx->limit_samples = *(const uint64_t *)value;
371 sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples);
372 break;
373 default:
374 /* Unknown capability, return SR_ERR. */
375 sr_err("la8: %s: Unknown capability.", __func__);
376 return SR_ERR;
377 break;
378 }
379
380 return SR_OK;
381}
382
383static int receive_data(int fd, int revents, void *cb_data)
384{
385 int i, ret;
386 struct sr_dev_inst *sdi;
387 struct context *ctx;
388
389 /* Avoid compiler errors. */
390 (void)fd;
391 (void)revents;
392
393 if (!(sdi = cb_data)) {
394 sr_err("la8: %s: cb_data was NULL", __func__);
395 return FALSE;
396 }
397
398 if (!(ctx = sdi->priv)) {
399 sr_err("la8: %s: sdi->priv was NULL", __func__);
400 return FALSE;
401 }
402
403 if (!ctx->ftdic) {
404 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
405 return FALSE;
406 }
407
408 /* Get one block of data. */
409 if ((ret = la8_read_block(ctx)) < 0) {
410 sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
411 hw_dev_acquisition_stop(sdi->index, sdi);
412 return FALSE;
413 }
414
415 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
416 if (ctx->block_counter != (NUM_BLOCKS - 1)) {
417 ctx->block_counter++;
418 return TRUE;
419 }
420
421 sr_dbg("la8: Sampling finished, sending data to session bus now.");
422
423 /* All data was received and demangled, send it to the session bus. */
424 for (i = 0; i < NUM_BLOCKS; i++)
425 send_block_to_session_bus(ctx, i);
426
427 hw_dev_acquisition_stop(sdi->index, sdi);
428
429 // return FALSE; /* FIXME? */
430 return TRUE;
431}
432
433static int hw_dev_acquisition_start(int dev_index, void *cb_data)
434{
435 struct sr_dev_inst *sdi;
436 struct context *ctx;
437 struct sr_datafeed_packet packet;
438 struct sr_datafeed_header header;
439 struct sr_datafeed_meta_logic meta;
440 uint8_t buf[4];
441 int bytes_written;
442
765ef2f7 443 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
b908f067
UH
444 sr_err("la8: %s: sdi was NULL", __func__);
445 return SR_ERR_BUG;
446 }
447
448 if (!(ctx = sdi->priv)) {
449 sr_err("la8: %s: sdi->priv was NULL", __func__);
450 return SR_ERR_BUG;
451 }
452
453 if (!ctx->ftdic) {
454 sr_err("la8: %s: ctx->ftdic was NULL", __func__);
455 return SR_ERR_BUG;
456 }
457
458 ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate);
459 if (ctx->divcount == 0xff) {
460 sr_err("la8: %s: invalid divcount/samplerate", __func__);
461 return SR_ERR;
462 }
463
464 sr_dbg("la8: Starting acquisition.");
465
466 /* Fill acquisition parameters into buf[]. */
467 buf[0] = ctx->divcount;
468 buf[1] = 0xff; /* This byte must always be 0xff. */
469 buf[2] = ctx->trigger_pattern;
470 buf[3] = ctx->trigger_mask;
471
472 /* Start acquisition. */
473 bytes_written = la8_write(ctx, buf, 4);
474
475 if (bytes_written < 0) {
476 sr_err("la8: Acquisition failed to start.");
477 return SR_ERR;
478 } else if (bytes_written != 4) {
479 sr_err("la8: Acquisition failed to start.");
480 return SR_ERR; /* TODO: Other error and return code? */
481 }
482
483 sr_dbg("la8: Acquisition started successfully.");
484
485 ctx->session_dev_id = cb_data;
486
487 /* Send header packet to the session bus. */
488 sr_dbg("la8: Sending SR_DF_HEADER.");
489 packet.type = SR_DF_HEADER;
490 packet.payload = &header;
491 header.feed_version = 1;
492 gettimeofday(&header.starttime, NULL);
493 sr_session_send(ctx->session_dev_id, &packet);
494
495 /* Send metadata about the SR_DF_LOGIC packets to come. */
496 packet.type = SR_DF_META_LOGIC;
497 packet.payload = &meta;
498 meta.samplerate = ctx->cur_samplerate;
499 meta.num_probes = NUM_PROBES;
500 sr_session_send(ctx->session_dev_id, &packet);
501
502 /* Time when we should be done (for detecting trigger timeouts). */
503 ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL)
504 + ctx->trigger_timeout;
505 ctx->block_counter = 0;
506 ctx->trigger_found = 0;
507
508 /* Hook up a dummy handler to receive data from the LA8. */
509 sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
510
511 return SR_OK;
512}
513
514static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
515{
516 struct sr_dev_inst *sdi;
517 struct context *ctx;
518 struct sr_datafeed_packet packet;
519
520 sr_dbg("la8: Stopping acquisition.");
521
765ef2f7 522 if (!(sdi = sr_dev_inst_get(cdi->instances, dev_index))) {
b908f067
UH
523 sr_err("la8: %s: sdi was NULL", __func__);
524 return SR_ERR_BUG;
525 }
526
527 if (!(ctx = sdi->priv)) {
528 sr_err("la8: %s: sdi->priv was NULL", __func__);
529 return SR_ERR_BUG;
530 }
531
532 /* Send end packet to the session bus. */
533 sr_dbg("la8: Sending SR_DF_END.");
534 packet.type = SR_DF_END;
535 sr_session_send(cb_data, &packet);
536
537 return SR_OK;
538}
539
540SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = {
541 .name = "chronovu-la8",
542 .longname = "ChronoVu LA8",
543 .api_version = 1,
544 .init = hw_init,
545 .cleanup = hw_cleanup,
61136ea6 546 .scan = hw_scan,
b908f067
UH
547 .dev_open = hw_dev_open,
548 .dev_close = hw_dev_close,
6a2761fd 549 .info_get = hw_info_get,
b908f067 550 .dev_status_get = hw_dev_status_get,
b908f067
UH
551 .dev_config_set = hw_dev_config_set,
552 .dev_acquisition_start = hw_dev_acquisition_start,
553 .dev_acquisition_stop = hw_dev_acquisition_stop,
765ef2f7 554 .instances = NULL,
b908f067 555};