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