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