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