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