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