]> sigrok.org Git - libsigrok.git/blob - hardware/chronovu-la/api.c
chronovu-la: Show errors other than "device not found".
[libsigrok.git] / hardware / chronovu-la / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2011-2014 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 "protocol.h"
22
23 SR_PRIV struct sr_dev_driver chronovu_la_driver_info;
24 static struct sr_dev_driver *di = &chronovu_la_driver_info;
25
26 static const int32_t hwcaps[] = {
27         SR_CONF_LOGIC_ANALYZER,
28         SR_CONF_SAMPLERATE,
29         SR_CONF_TRIGGER_TYPE,
30         SR_CONF_LIMIT_MSEC, /* TODO: Not yet implemented. */
31         SR_CONF_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
32 };
33
34 /* The ChronoVu LA8/LA16 can have multiple VID/PID pairs. */
35 static struct {
36         uint16_t vid;
37         uint16_t pid;
38         int model;
39         const char *iproduct;
40 } vid_pid[] = {
41         { 0x0403, 0x6001, CHRONOVU_LA8,  "ChronoVu LA8"  },
42         { 0x0403, 0x8867, CHRONOVU_LA8,  "ChronoVu LA8"  },
43         { 0x0403, 0x6001, CHRONOVU_LA16, "ChronoVu LA16" },
44         { 0x0403, 0x8867, CHRONOVU_LA16, "ChronoVu LA16" },
45 };
46
47 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
48
49 static void clear_helper(void *priv)
50 {
51         struct dev_context *devc;
52
53         devc = priv;
54
55         ftdi_free(devc->ftdic);
56         g_free(devc->final_buf);
57 }
58
59 static int dev_clear(void)
60 {
61         return std_dev_clear(di, clear_helper);
62 }
63
64 static int init(struct sr_context *sr_ctx)
65 {
66         return std_init(sr_ctx, di, LOG_PREFIX);
67 }
68
69 static int add_device(int idx, int model, GSList **devices)
70 {
71         int ret;
72         unsigned int i;
73         struct sr_dev_inst *sdi;
74         struct drv_context *drvc;
75         struct dev_context *devc;
76         struct sr_channel *ch;
77
78         ret = SR_OK;
79
80         drvc = di->priv;
81
82         /* Allocate memory for our private device context. */
83         devc = g_try_malloc(sizeof(struct dev_context));
84
85         /* Set some sane defaults. */
86         devc->prof = &cv_profiles[model];
87         devc->ftdic = NULL; /* Will be set in the open() API call. */
88         devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
89         devc->limit_msec = 0;
90         devc->limit_samples = 0;
91         devc->cb_data = NULL;
92         memset(devc->mangled_buf, 0, BS);
93         devc->final_buf = NULL;
94         devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
95         devc->trigger_mask = 0x0000; /* All channels: "don't care". */
96         devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
97         devc->trigger_found = 0;
98         devc->done = 0;
99         devc->block_counter = 0;
100         devc->divcount = 0;
101         devc->usb_vid = vid_pid[idx].vid;
102         devc->usb_pid = vid_pid[idx].pid;
103         memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
104
105         /* Allocate memory where we'll store the de-mangled data. */
106         if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
107                 sr_err("Failed to allocate memory for sample buffer.");
108                 ret = SR_ERR_MALLOC;
109                 goto err_free_devc;
110         }
111
112         /* We now know the device, set its max. samplerate as default. */
113         devc->cur_samplerate = devc->prof->max_samplerate;
114
115         /* Register the device with libsigrok. */
116         sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
117                               "ChronoVu", devc->prof->modelname, NULL);
118         if (!sdi) {
119                 sr_err("Failed to create device instance.");
120                 ret = SR_ERR;
121                 goto err_free_final_buf;
122         }
123         sdi->driver = di;
124         sdi->priv = devc;
125
126         for (i = 0; i < devc->prof->num_channels; i++) {
127                 if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
128                                           cv_channel_names[i]))) {
129                         ret = SR_ERR;
130                         goto err_free_dev_inst;
131                 }
132                 sdi->channels = g_slist_append(sdi->channels, ch);
133         }
134
135         *devices = g_slist_append(*devices, sdi);
136         drvc->instances = g_slist_append(drvc->instances, sdi);
137
138         return SR_OK;
139
140 err_free_dev_inst:
141         sr_dev_inst_free(sdi);
142 err_free_final_buf:
143         g_free(devc->final_buf);
144 err_free_devc:
145         g_free(devc);
146
147         return ret;
148 }
149
150 static GSList *scan(GSList *options)
151 {
152         int ret;
153         unsigned int i;
154         GSList *devices;
155         struct ftdi_context *ftdic;
156
157         (void)options;
158
159         devices = NULL;
160
161         /* Allocate memory for the FTDI context and initialize it. */
162         if (!(ftdic = ftdi_new())) {
163                 sr_err("Failed to initialize libftdi.");
164                 return NULL;
165         }
166
167         /* Check for LA8 and/or LA16 devices with various VID/PIDs. */
168         for (i = 0; i < ARRAY_SIZE(vid_pid); i++) {
169                 ret = ftdi_usb_open_desc(ftdic, vid_pid[i].vid,
170                         vid_pid[i].pid, vid_pid[i].iproduct, NULL);
171                 /* Show errors other than "device not found". */
172                 if (ret < 0 && ret != -3)
173                         sr_dbg("Error finding/opening device (%d): %s.",
174                                ret, ftdi_get_error_string(ftdic));
175                 if (ret < 0)
176                         continue; /* No device found, or not usable. */
177
178                 sr_dbg("Found %s device (%04x:%04x).",
179                        vid_pid[i].iproduct, vid_pid[i].vid, vid_pid[i].pid);
180
181                 if ((ret = add_device(i, vid_pid[i].model, &devices)) < 0)
182                         sr_dbg("Failed to add device: %d.", ret);
183
184                 if ((ret = ftdi_usb_close(ftdic)) < 0)
185                         sr_dbg("Failed to close FTDI device (%d): %s.",
186                                ret, ftdi_get_error_string(ftdic));
187         }
188
189         /* Close USB device, deinitialize and free the FTDI context. */
190         ftdi_free(ftdic);
191         ftdic = NULL;
192
193         return devices;
194 }
195
196 static GSList *dev_list(void)
197 {
198         return ((struct drv_context *)(di->priv))->instances;
199 }
200
201 static int dev_open(struct sr_dev_inst *sdi)
202 {
203         struct dev_context *devc;
204         int ret;
205
206         ret = SR_ERR;
207
208         if (!(devc = sdi->priv))
209                 return SR_ERR_BUG;
210
211         /* Allocate memory for the FTDI context and initialize it. */
212         if (!(devc->ftdic = ftdi_new())) {
213                 sr_err("Failed to initialize libftdi.");
214                 return SR_ERR;
215         }
216
217         sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
218                devc->usb_vid, devc->usb_pid);
219
220         /* Open the device. */
221         if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
222                         devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
223                 sr_err("Failed to open FTDI device (%d): %s.",
224                        ret, ftdi_get_error_string(devc->ftdic));
225                 goto err_ftdi_free;
226         }
227         sr_dbg("Device opened successfully.");
228
229         /* Purge RX/TX buffers in the FTDI chip. */
230         if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
231                 sr_err("Failed to purge FTDI buffers (%d): %s.",
232                        ret, ftdi_get_error_string(devc->ftdic));
233                 goto err_ftdi_free;
234         }
235         sr_dbg("FTDI buffers purged successfully.");
236
237         /* Enable flow control in the FTDI chip. */
238         if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
239                 sr_err("Failed to enable FTDI flow control (%d): %s.",
240                        ret, ftdi_get_error_string(devc->ftdic));
241                 goto err_ftdi_free;
242         }
243         sr_dbg("FTDI flow control enabled successfully.");
244
245         /* Wait 100ms. */
246         g_usleep(100 * 1000);
247
248         sdi->status = SR_ST_ACTIVE;
249
250         return SR_OK;
251
252 err_ftdi_free:
253         ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
254         devc->ftdic = NULL;
255         return ret;
256 }
257
258 static int dev_close(struct sr_dev_inst *sdi)
259 {
260         int ret;
261         struct dev_context *devc;
262
263         if (sdi->status != SR_ST_ACTIVE)
264                 return SR_OK;
265
266         devc = sdi->priv;
267
268         if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0)
269                 sr_err("Failed to close FTDI device (%d): %s.",
270                        ret, ftdi_get_error_string(devc->ftdic));
271         sdi->status = SR_ST_INACTIVE;
272
273         return SR_OK;
274 }
275
276 static int cleanup(void)
277 {
278         return dev_clear();
279 }
280
281 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
282                 const struct sr_channel_group *cg)
283 {
284         struct dev_context *devc;
285
286         (void)cg;
287
288         switch (id) {
289         case SR_CONF_SAMPLERATE:
290                 if (!sdi || !(devc = sdi->priv))
291                         return SR_ERR_BUG;
292                 *data = g_variant_new_uint64(devc->cur_samplerate);
293                 break;
294         default:
295                 return SR_ERR_NA;
296         }
297
298         return SR_OK;
299 }
300
301 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
302                 const struct sr_channel_group *cg)
303 {
304         struct dev_context *devc;
305
306         (void)cg;
307
308         if (sdi->status != SR_ST_ACTIVE)
309                 return SR_ERR_DEV_CLOSED;
310
311         if (!(devc = sdi->priv))
312                 return SR_ERR_BUG;
313
314         switch (id) {
315         case SR_CONF_SAMPLERATE:
316                 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
317                         return SR_ERR;
318                 break;
319         case SR_CONF_LIMIT_MSEC:
320                 if (g_variant_get_uint64(data) == 0)
321                         return SR_ERR_ARG;
322                 devc->limit_msec = g_variant_get_uint64(data);
323                 break;
324         case SR_CONF_LIMIT_SAMPLES:
325                 if (g_variant_get_uint64(data) == 0)
326                         return SR_ERR_ARG;
327                 devc->limit_samples = g_variant_get_uint64(data);
328                 break;
329         default:
330                 return SR_ERR_NA;
331         }
332
333         return SR_OK;
334 }
335
336 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
337                 const struct sr_channel_group *cg)
338 {
339         GVariant *gvar, *grange[2];
340         GVariantBuilder gvb;
341         struct dev_context *devc;
342
343         (void)cg;
344
345         switch (key) {
346         case SR_CONF_DEVICE_OPTIONS:
347                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
348                                 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
349                 break;
350         case SR_CONF_SAMPLERATE:
351                 if (!sdi || !sdi->priv || !(devc = sdi->priv))
352                         return SR_ERR_BUG;
353                 cv_fill_samplerates_if_needed(sdi);
354                 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
355                 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
356                                 devc->samplerates,
357                                 ARRAY_SIZE(devc->samplerates),
358                                 sizeof(uint64_t));
359                 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
360                 *data = g_variant_builder_end(&gvb);
361                 break;
362         case SR_CONF_LIMIT_SAMPLES:
363                 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
364                         return SR_ERR_BUG;
365                 grange[0] = g_variant_new_uint64(0);
366                 if (devc->prof->model == CHRONOVU_LA8)
367                         grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES);
368                 else
369                         grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES / 2);
370                 *data = g_variant_new_tuple(grange, 2);
371                 break;
372         case SR_CONF_TRIGGER_TYPE:
373                 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
374                         return SR_ERR_BUG;
375                 *data = g_variant_new_string(devc->prof->trigger_type);
376                 break;
377         default:
378                 return SR_ERR_NA;
379         }
380
381         return SR_OK;
382 }
383
384 static int receive_data(int fd, int revents, void *cb_data)
385 {
386         int i, ret;
387         struct sr_dev_inst *sdi;
388         struct dev_context *devc;
389
390         (void)fd;
391         (void)revents;
392
393         if (!(sdi = cb_data)) {
394                 sr_err("cb_data was NULL.");
395                 return FALSE;
396         }
397
398         if (!(devc = sdi->priv)) {
399                 sr_err("sdi->priv was NULL.");
400                 return FALSE;
401         }
402
403         if (!devc->ftdic) {
404                 sr_err("devc->ftdic was NULL.");
405                 return FALSE;
406         }
407
408         /* Get one block of data. */
409         if ((ret = cv_read_block(devc)) < 0) {
410                 sr_err("Failed to read data block: %d.", ret);
411                 dev_acquisition_stop(sdi, sdi);
412                 return FALSE;
413         }
414
415         /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
416         if (devc->block_counter != (NUM_BLOCKS - 1)) {
417                 devc->block_counter++;
418                 return TRUE;
419         }
420
421         sr_dbg("Sampling finished, sending data to session bus now.");
422
423         /*
424          * All data was received and demangled, send it to the session bus.
425          *
426          * Note: Due to the method how data is spread across the 8MByte of
427          * SDRAM, we can _not_ send it to the session bus in a streaming
428          * manner while we receive it. We have to receive and de-mangle the
429          * full 8MByte first, only then the whole buffer contains valid data.
430          */
431         for (i = 0; i < NUM_BLOCKS; i++)
432                 cv_send_block_to_session_bus(devc, i);
433
434         dev_acquisition_stop(sdi, sdi);
435
436         return TRUE;
437 }
438
439 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
440 {
441         struct dev_context *devc;
442         uint8_t buf[8];
443         int bytes_to_write, bytes_written;
444
445         if (sdi->status != SR_ST_ACTIVE)
446                 return SR_ERR_DEV_CLOSED;
447
448         if (!(devc = sdi->priv)) {
449                 sr_err("sdi->priv was NULL.");
450                 return SR_ERR_BUG;
451         }
452
453         if (!devc->ftdic) {
454                 sr_err("devc->ftdic was NULL.");
455                 return SR_ERR_BUG;
456         }
457
458         devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
459         if (devc->divcount == 0xff) {
460                 sr_err("Invalid divcount/samplerate.");
461                 return SR_ERR;
462         }
463
464         if (cv_configure_channels(sdi) != SR_OK) {
465                 sr_err("Failed to configure channels.");
466                 return SR_ERR;
467         }
468
469         /* Fill acquisition parameters into buf[]. */
470         if (devc->prof->model == CHRONOVU_LA8) {
471                 buf[0] = devc->divcount;
472                 buf[1] = 0xff; /* This byte must always be 0xff. */
473                 buf[2] = devc->trigger_pattern & 0xff;
474                 buf[3] = devc->trigger_mask & 0xff;
475                 bytes_to_write = 4;
476         } else {
477                 buf[0] = devc->divcount;
478                 buf[1] = 0xff; /* This byte must always be 0xff. */
479                 buf[2] = (devc->trigger_pattern & 0xff00) >> 8;  /* LSB */
480                 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0;  /* MSB */
481                 buf[4] = (devc->trigger_mask & 0xff00) >> 8;     /* LSB */
482                 buf[5] = (devc->trigger_mask & 0x00ff) >> 0;     /* MSB */
483                 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
484                 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
485                 bytes_to_write = 8;
486         }
487
488         /* Start acquisition. */
489         bytes_written = cv_write(devc, buf, bytes_to_write);
490
491         if (bytes_written < 0 || bytes_written != bytes_to_write) {
492                 sr_err("Acquisition failed to start.");
493                 return SR_ERR;
494         }
495
496         sr_dbg("Hardware acquisition started successfully.");
497
498         devc->cb_data = cb_data;
499
500         /* Send header packet to the session bus. */
501         std_session_send_df_header(cb_data, LOG_PREFIX);
502
503         /* Time when we should be done (for detecting trigger timeouts). */
504         devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
505                         g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
506         devc->block_counter = 0;
507         devc->trigger_found = 0;
508
509         /* Hook up a dummy handler to receive data from the device. */
510         sr_source_add(-1, G_IO_IN, 0, receive_data, (void *)sdi);
511
512         return SR_OK;
513 }
514
515 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
516 {
517         struct sr_datafeed_packet packet;
518
519         (void)sdi;
520
521         sr_dbg("Stopping acquisition.");
522         sr_source_remove(-1);
523
524         /* Send end packet to the session bus. */
525         sr_dbg("Sending SR_DF_END.");
526         packet.type = SR_DF_END;
527         sr_session_send(cb_data, &packet);
528
529         return SR_OK;
530 }
531
532 SR_PRIV struct sr_dev_driver chronovu_la_driver_info = {
533         .name = "chronovu-la",
534         .longname = "ChronoVu LA8/LA16",
535         .api_version = 1,
536         .init = init,
537         .cleanup = cleanup,
538         .scan = scan,
539         .dev_list = dev_list,
540         .dev_clear = dev_clear,
541         .config_get = config_get,
542         .config_set = config_set,
543         .config_list = config_list,
544         .dev_open = dev_open,
545         .dev_close = dev_close,
546         .dev_acquisition_start = dev_acquisition_start,
547         .dev_acquisition_stop = dev_acquisition_stop,
548         .priv = NULL,
549 };