]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/chronovu-la/api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / chronovu-la / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2011-2015 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, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
21#include "protocol.h"
22
23#define SCAN_EXPECTED_VENDOR 0x0403
24
25static const uint32_t scanopts[] = {
26 SR_CONF_CONN,
27};
28
29static const uint32_t drvopts[] = {
30 SR_CONF_LOGIC_ANALYZER,
31};
32
33static const uint32_t devopts[] = {
34 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
36 SR_CONF_CONN | SR_CONF_GET,
37 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
39};
40
41static const int32_t trigger_matches[] = {
42 SR_TRIGGER_ZERO,
43 SR_TRIGGER_ONE,
44 SR_TRIGGER_RISING,
45 SR_TRIGGER_FALLING,
46};
47
48static void clear_helper(struct dev_context *devc)
49{
50 ftdi_free(devc->ftdic);
51 g_free(devc->final_buf);
52}
53
54static int dev_clear(const struct sr_dev_driver *di)
55{
56 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
57}
58
59static int add_device(int model, struct libusb_device_descriptor *des,
60 const char *serial_num, const char *connection_id, libusb_device *usbdev,
61 GSList **devices)
62{
63 int ret;
64 unsigned int i;
65 struct sr_dev_inst *sdi;
66 struct dev_context *devc;
67
68 ret = SR_OK;
69
70 devc = g_malloc0(sizeof(struct dev_context));
71
72 /* Set some sane defaults. */
73 devc->prof = &cv_profiles[model];
74 devc->ftdic = NULL; /* Will be set in the open() API call. */
75 devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
76 devc->limit_msec = 0;
77 devc->limit_samples = 0;
78 memset(devc->mangled_buf, 0, BS);
79 devc->final_buf = NULL;
80 devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
81 devc->trigger_mask = 0x0000; /* All channels: "don't care". */
82 devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
83 devc->trigger_found = 0;
84 devc->done = 0;
85 devc->block_counter = 0;
86 devc->divcount = 0;
87 devc->usb_vid = des->idVendor;
88 devc->usb_pid = des->idProduct;
89 memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
90
91 /* Allocate memory where we'll store the de-mangled data. */
92 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
93 sr_err("Failed to allocate memory for sample buffer.");
94 ret = SR_ERR_MALLOC;
95 goto err_free_devc;
96 }
97
98 /* We now know the device, set its max. samplerate as default. */
99 devc->cur_samplerate = devc->prof->max_samplerate;
100
101 sdi = g_malloc0(sizeof(struct sr_dev_inst));
102 sdi->status = SR_ST_INACTIVE;
103 sdi->vendor = g_strdup("ChronoVu");
104 sdi->model = g_strdup(devc->prof->modelname);
105 sdi->serial_num = g_strdup(serial_num);
106 sdi->connection_id = g_strdup(connection_id);
107 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(usbdev),
108 libusb_get_device_address(usbdev), NULL);
109 sdi->priv = devc;
110
111 for (i = 0; i < devc->prof->num_channels; i++)
112 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
113 cv_channel_names[i]);
114
115 *devices = g_slist_append(*devices, sdi);
116
117 if (ret == SR_OK)
118 return SR_OK;
119
120err_free_devc:
121 g_free(devc);
122
123 return ret;
124}
125
126static GSList *scan(struct sr_dev_driver *di, GSList *options)
127{
128 struct drv_context *drvc;
129 GSList *devices;
130 const char *conn;
131 int ret;
132 GSList *conn_devices, *l;
133 size_t i;
134 struct sr_usb_dev_inst *usb;
135 uint8_t bus, addr;
136 struct libusb_device_descriptor des;
137 libusb_device **devlist;
138 struct libusb_device_handle *hdl;
139 char product[64], serial_num[64], connection_id[64];
140 int model;
141
142 drvc = di->context;
143 devices = NULL;
144
145 conn = NULL;
146 (void)sr_serial_extract_options(options, &conn, NULL);
147 conn_devices = NULL;
148 if (conn)
149 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
150
151 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
152 for (i = 0; devlist[i]; i++) {
153 bus = libusb_get_bus_number(devlist[i]);
154 addr = libusb_get_device_address(devlist[i]);
155 if (conn) {
156 /* Check if the connection matches the user spec. */
157 for (l = conn_devices; l; l = l->next) {
158 usb = l->data;
159 if (usb->bus == bus && usb->address == addr)
160 break;
161 }
162 if (!l)
163 continue;
164 }
165
166 libusb_get_device_descriptor(devlist[i], &des);
167
168 /*
169 * In theory we'd accept any USB device with a matching
170 * product string. In practice the enumeration takes a
171 * shortcut and only inspects devices when their USB VID
172 * matches the expectation. This avoids access to flaky
173 * devices which are unrelated to measurement purposes
174 * yet cause trouble when accessed including segfaults,
175 * while libusb won't transparently handle their flaws.
176 *
177 * See https://sigrok.org/bugzilla/show_bug.cgi?id=1115
178 * and https://github.com/sigrokproject/libsigrok/pull/166
179 * for a discussion.
180 */
181 if (des.idVendor != SCAN_EXPECTED_VENDOR)
182 continue;
183
184 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
185 continue;
186
187 if (des.iProduct == 0) {
188 product[0] = '\0';
189 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
190 des.iProduct, (unsigned char *)product,
191 sizeof(product))) < 0) {
192 sr_warn("Failed to get product string descriptor: %s.",
193 libusb_error_name(ret));
194 continue;
195 }
196
197 if (des.iSerialNumber == 0) {
198 serial_num[0] = '\0';
199 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
200 des.iSerialNumber, (unsigned char *)serial_num,
201 sizeof(serial_num))) < 0) {
202 sr_warn("Failed to get serial number string descriptor: %s.",
203 libusb_error_name(ret));
204 continue;
205 }
206
207 libusb_close(hdl);
208
209 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
210 continue;
211
212 if (!strcmp(product, "ChronoVu LA8"))
213 model = 0;
214 else if (!strcmp(product, "ChronoVu LA16"))
215 model = 1;
216 else
217 continue; /* Unknown iProduct string, ignore. */
218
219 sr_dbg("Found %s (%04x:%04x, %d.%d, %s).",
220 product, des.idVendor, des.idProduct,
221 libusb_get_bus_number(devlist[i]),
222 libusb_get_device_address(devlist[i]), connection_id);
223
224 if ((ret = add_device(model, &des, serial_num, connection_id,
225 devlist[i], &devices)) < 0) {
226 sr_dbg("Failed to add device: %d.", ret);
227 }
228 }
229 libusb_free_device_list(devlist, 1);
230 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
231
232 return std_scan_complete(di, devices);
233}
234
235static int dev_open(struct sr_dev_inst *sdi)
236{
237 struct dev_context *devc;
238 int ret;
239
240 devc = sdi->priv;
241
242 if (!(devc->ftdic = ftdi_new())) {
243 sr_err("Failed to initialize libftdi.");
244 return SR_ERR;
245 }
246
247 sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
248 devc->usb_vid, devc->usb_pid);
249
250 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
251 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
252 sr_err("Failed to open FTDI device (%d): %s.",
253 ret, ftdi_get_error_string(devc->ftdic));
254 goto err_ftdi_free;
255 }
256
257 if ((ret = PURGE_FTDI_BOTH(devc->ftdic)) < 0) {
258 sr_err("Failed to purge FTDI buffers (%d): %s.",
259 ret, ftdi_get_error_string(devc->ftdic));
260 goto err_ftdi_free;
261 }
262
263 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
264 sr_err("Failed to enable FTDI flow control (%d): %s.",
265 ret, ftdi_get_error_string(devc->ftdic));
266 goto err_ftdi_free;
267 }
268
269 g_usleep(100 * 1000);
270
271 return SR_OK;
272
273err_ftdi_free:
274 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
275 devc->ftdic = NULL;
276 return SR_ERR;
277}
278
279static int dev_close(struct sr_dev_inst *sdi)
280{
281 int ret;
282 struct dev_context *devc;
283
284 devc = sdi->priv;
285
286 if (!devc->ftdic)
287 return SR_ERR_BUG;
288
289 if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
290 sr_err("Failed to close FTDI device (%d): %s.",
291 ret, ftdi_get_error_string(devc->ftdic));
292
293 return (ret == 0) ? SR_OK : SR_ERR;
294}
295
296static int config_get(uint32_t key, GVariant **data,
297 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
298{
299 struct dev_context *devc;
300 struct sr_usb_dev_inst *usb;
301
302 (void)cg;
303
304 switch (key) {
305 case SR_CONF_CONN:
306 if (!sdi || !(usb = sdi->conn))
307 return SR_ERR_ARG;
308 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
309 break;
310 case SR_CONF_SAMPLERATE:
311 if (!sdi)
312 return SR_ERR_BUG;
313 devc = sdi->priv;
314 *data = g_variant_new_uint64(devc->cur_samplerate);
315 break;
316 default:
317 return SR_ERR_NA;
318 }
319
320 return SR_OK;
321}
322
323static int config_set(uint32_t key, GVariant *data,
324 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
325{
326 struct dev_context *devc;
327
328 (void)cg;
329
330 devc = sdi->priv;
331
332 switch (key) {
333 case SR_CONF_SAMPLERATE:
334 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
335 return SR_ERR;
336 break;
337 case SR_CONF_LIMIT_MSEC:
338 devc->limit_msec = g_variant_get_uint64(data);
339 break;
340 case SR_CONF_LIMIT_SAMPLES:
341 devc->limit_samples = g_variant_get_uint64(data);
342 break;
343 default:
344 return SR_ERR_NA;
345 }
346
347 return SR_OK;
348}
349
350static int config_list(uint32_t key, GVariant **data,
351 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
352{
353 struct dev_context *devc;
354
355 devc = (sdi) ? sdi->priv : NULL;
356
357 switch (key) {
358 case SR_CONF_SCAN_OPTIONS:
359 case SR_CONF_DEVICE_OPTIONS:
360 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
361 case SR_CONF_SAMPLERATE:
362 cv_fill_samplerates_if_needed(sdi);
363 *data = std_gvar_samplerates(ARRAY_AND_SIZE(devc->samplerates));
364 break;
365 case SR_CONF_LIMIT_SAMPLES:
366 if (!devc || !devc->prof)
367 return SR_ERR_BUG;
368 *data = std_gvar_tuple_u64(0, (devc->prof->model == CHRONOVU_LA8) ? MAX_NUM_SAMPLES : MAX_NUM_SAMPLES / 2);
369 break;
370 case SR_CONF_TRIGGER_MATCH:
371 if (!devc || !devc->prof)
372 return SR_ERR_BUG;
373 *data = std_gvar_array_i32(trigger_matches, devc->prof->num_trigger_matches);
374 break;
375 default:
376 return SR_ERR_NA;
377 }
378
379 return SR_OK;
380}
381
382static int receive_data(int fd, int revents, void *cb_data)
383{
384 int i, ret;
385 struct sr_dev_inst *sdi;
386 struct dev_context *devc;
387
388 (void)fd;
389 (void)revents;
390
391 if (!(sdi = cb_data)) {
392 sr_err("cb_data was NULL.");
393 return FALSE;
394 }
395
396 if (!(devc = sdi->priv)) {
397 sr_err("sdi->priv was NULL.");
398 return FALSE;
399 }
400
401 if (!devc->ftdic) {
402 sr_err("devc->ftdic was NULL.");
403 return FALSE;
404 }
405
406 /* Get one block of data. */
407 if ((ret = cv_read_block(devc)) < 0) {
408 sr_err("Failed to read data block: %d.", ret);
409 sr_dev_acquisition_stop(sdi);
410 return FALSE;
411 }
412
413 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
414 if (devc->block_counter != (NUM_BLOCKS - 1)) {
415 devc->block_counter++;
416 return TRUE;
417 }
418
419 sr_dbg("Sampling finished, sending data to session bus now.");
420
421 /*
422 * All data was received and demangled, send it to the session bus.
423 *
424 * Note: Due to the method how data is spread across the 8MByte of
425 * SDRAM, we can _not_ send it to the session bus in a streaming
426 * manner while we receive it. We have to receive and de-mangle the
427 * full 8MByte first, only then the whole buffer contains valid data.
428 */
429 for (i = 0; i < NUM_BLOCKS; i++)
430 cv_send_block_to_session_bus(sdi, i);
431
432 sr_dev_acquisition_stop(sdi);
433
434 return TRUE;
435}
436
437static int dev_acquisition_start(const struct sr_dev_inst *sdi)
438{
439 struct dev_context *devc;
440 uint8_t buf[8];
441 int bytes_to_write, bytes_written;
442
443 devc = sdi->priv;
444
445 if (!devc->ftdic) {
446 sr_err("devc->ftdic was NULL.");
447 return SR_ERR_BUG;
448 }
449
450 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
451 if (devc->divcount == 0xff) {
452 sr_err("Invalid divcount/samplerate.");
453 return SR_ERR;
454 }
455
456 if (cv_convert_trigger(sdi) != SR_OK) {
457 sr_err("Failed to configure trigger.");
458 return SR_ERR;
459 }
460
461 /* Fill acquisition parameters into buf[]. */
462 if (devc->prof->model == CHRONOVU_LA8) {
463 buf[0] = devc->divcount;
464 buf[1] = 0xff; /* This byte must always be 0xff. */
465 buf[2] = devc->trigger_pattern & 0xff;
466 buf[3] = devc->trigger_mask & 0xff;
467 bytes_to_write = 4;
468 } else {
469 buf[0] = devc->divcount;
470 buf[1] = 0xff; /* This byte must always be 0xff. */
471 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
472 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
473 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
474 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
475 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
476 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
477 bytes_to_write = 8;
478 }
479
480 /* Start acquisition. */
481 bytes_written = cv_write(devc, buf, bytes_to_write);
482
483 if (bytes_written < 0 || bytes_written != bytes_to_write) {
484 sr_err("Acquisition failed to start.");
485 return SR_ERR;
486 }
487
488 std_session_send_df_header(sdi);
489
490 /* Time when we should be done (for detecting trigger timeouts). */
491 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
492 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
493 devc->block_counter = 0;
494 devc->trigger_found = 0;
495
496 /* Hook up a dummy handler to receive data from the device. */
497 sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi);
498
499 return SR_OK;
500}
501
502static int dev_acquisition_stop(struct sr_dev_inst *sdi)
503{
504 sr_session_source_remove(sdi->session, -1);
505 std_session_send_df_end(sdi);
506
507 return SR_OK;
508}
509
510static struct sr_dev_driver chronovu_la_driver_info = {
511 .name = "chronovu-la",
512 .longname = "ChronoVu LA8/LA16",
513 .api_version = 1,
514 .init = std_init,
515 .cleanup = std_cleanup,
516 .scan = scan,
517 .dev_list = std_dev_list,
518 .dev_clear = dev_clear,
519 .config_get = config_get,
520 .config_set = config_set,
521 .config_list = config_list,
522 .dev_open = dev_open,
523 .dev_close = dev_close,
524 .dev_acquisition_start = dev_acquisition_start,
525 .dev_acquisition_stop = dev_acquisition_stop,
526 .context = NULL,
527};
528SR_REGISTER_DEV_DRIVER(chronovu_la_driver_info);