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