2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011-2015 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
24 SR_PRIV struct sr_dev_driver chronovu_la_driver_info;
25 static struct sr_dev_driver *di = &chronovu_la_driver_info;
27 static const uint32_t drvopts[] = {
28 SR_CONF_LOGIC_ANALYZER,
31 static const uint32_t scanopts[] = {
35 static const uint32_t devopts[] = {
36 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
37 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
38 SR_CONF_CONN | SR_CONF_GET,
39 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
43 static const int32_t trigger_matches[] = {
50 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
52 static void clear_helper(void *priv)
54 struct dev_context *devc;
58 ftdi_free(devc->ftdic);
59 g_free(devc->final_buf);
62 static int dev_clear(const struct sr_dev_driver *di)
64 return std_dev_clear(di, clear_helper);
67 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
69 return std_init(sr_ctx, di, LOG_PREFIX);
72 static int add_device(int model, struct libusb_device_descriptor *des,
73 const char *serial_num, const char *connection_id,
74 libusb_device *usbdev, GSList **devices)
78 struct sr_dev_inst *sdi;
79 struct drv_context *drvc;
80 struct dev_context *devc;
86 /* Allocate memory for our private device context. */
87 devc = g_malloc0(sizeof(struct dev_context));
89 /* Set some sane defaults. */
90 devc->prof = &cv_profiles[model];
91 devc->ftdic = NULL; /* Will be set in the open() API call. */
92 devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
94 devc->limit_samples = 0;
96 memset(devc->mangled_buf, 0, BS);
97 devc->final_buf = NULL;
98 devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
99 devc->trigger_mask = 0x0000; /* All channels: "don't care". */
100 devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
101 devc->trigger_found = 0;
103 devc->block_counter = 0;
105 devc->usb_vid = des->idVendor;
106 devc->usb_pid = des->idProduct;
107 memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
109 /* Allocate memory where we'll store the de-mangled data. */
110 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
111 sr_err("Failed to allocate memory for sample buffer.");
116 /* We now know the device, set its max. samplerate as default. */
117 devc->cur_samplerate = devc->prof->max_samplerate;
119 /* Register the device with libsigrok. */
120 sdi = g_malloc0(sizeof(struct sr_dev_inst));
121 sdi->status = SR_ST_INITIALIZING;
122 sdi->vendor = g_strdup("ChronoVu");
123 sdi->model = g_strdup(devc->prof->modelname);
124 sdi->serial_num = g_strdup(serial_num);
125 sdi->connection_id = g_strdup(connection_id);
126 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(usbdev),
127 libusb_get_device_address(usbdev), NULL);
131 for (i = 0; i < devc->prof->num_channels; i++)
132 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
133 cv_channel_names[i]);
135 *devices = g_slist_append(*devices, sdi);
136 drvc->instances = g_slist_append(drvc->instances, sdi);
147 static GSList *scan(struct sr_dev_driver *di, GSList *options)
150 struct drv_context *drvc;
151 GSList *devices, *conn_devices, *l;
152 struct sr_usb_dev_inst *usb;
153 struct sr_config *src;
154 struct libusb_device_descriptor des;
155 libusb_device **devlist;
156 struct libusb_device_handle *hdl;
158 char product[64], serial_num[64], connection_id[64];
161 drvc->instances = NULL;
164 for (l = options; l; l = l->next) {
168 conn = g_variant_get_string(src->data, NULL);
173 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
178 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
180 for (i = 0; devlist[i]; i++) {
182 for (l = conn_devices; l; l = l->next) {
184 if (usb->bus == libusb_get_bus_number(devlist[i])
185 && usb->address == libusb_get_device_address(devlist[i]))
189 /* This device matched none of the ones that
190 * matched the conn specification. */
194 libusb_get_device_descriptor(devlist[i], &des);
196 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
199 if (des.iProduct == 0) {
201 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
202 des.iProduct, (unsigned char *)product,
203 sizeof(product))) < 0) {
204 sr_warn("Failed to get product string descriptor: %s.",
205 libusb_error_name(ret));
209 if (des.iSerialNumber == 0) {
210 serial_num[0] = '\0';
211 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
212 des.iSerialNumber, (unsigned char *)serial_num,
213 sizeof(serial_num))) < 0) {
214 sr_warn("Failed to get serial number string descriptor: %s.",
215 libusb_error_name(ret));
219 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
223 if (!strcmp(product, "ChronoVu LA8")) {
225 } else if (!strcmp(product, "ChronoVu LA16")) {
228 sr_spew("Unknown iProduct string '%s'.", product);
232 sr_dbg("Found %s (%04x:%04x, %d.%d, %s).",
233 product, des.idVendor, des.idProduct,
234 libusb_get_bus_number(devlist[i]),
235 libusb_get_device_address(devlist[i]), connection_id);
237 if ((ret = add_device(model, &des, serial_num, connection_id,
238 devlist[i], &devices)) < 0) {
239 sr_dbg("Failed to add device: %d.", ret);
243 libusb_free_device_list(devlist, 1);
244 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
249 static GSList *dev_list(const struct sr_dev_driver *di)
251 return ((struct drv_context *)(di->context))->instances;
254 static int dev_open(struct sr_dev_inst *sdi)
256 struct dev_context *devc;
259 if (!(devc = sdi->priv))
262 /* Allocate memory for the FTDI context and initialize it. */
263 if (!(devc->ftdic = ftdi_new())) {
264 sr_err("Failed to initialize libftdi.");
268 sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
269 devc->usb_vid, devc->usb_pid);
271 /* Open the device. */
272 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
273 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
274 sr_err("Failed to open FTDI device (%d): %s.",
275 ret, ftdi_get_error_string(devc->ftdic));
278 sr_dbg("Device opened successfully.");
280 /* Purge RX/TX buffers in the FTDI chip. */
281 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
282 sr_err("Failed to purge FTDI buffers (%d): %s.",
283 ret, ftdi_get_error_string(devc->ftdic));
286 sr_dbg("FTDI buffers purged successfully.");
288 /* Enable flow control in the FTDI chip. */
289 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
290 sr_err("Failed to enable FTDI flow control (%d): %s.",
291 ret, ftdi_get_error_string(devc->ftdic));
294 sr_dbg("FTDI flow control enabled successfully.");
297 g_usleep(100 * 1000);
299 sdi->status = SR_ST_ACTIVE;
305 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
310 static int dev_close(struct sr_dev_inst *sdi)
313 struct dev_context *devc;
315 if (sdi->status != SR_ST_ACTIVE)
320 if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0)
321 sr_err("Failed to close FTDI device (%d): %s.",
322 ret, ftdi_get_error_string(devc->ftdic));
323 sdi->status = SR_ST_INACTIVE;
328 static int cleanup(const struct sr_dev_driver *di)
330 return dev_clear(di);
333 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
334 const struct sr_channel_group *cg)
336 struct dev_context *devc;
337 struct sr_usb_dev_inst *usb;
344 if (!sdi || !(usb = sdi->conn))
346 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
347 *data = g_variant_new_string(str);
349 case SR_CONF_SAMPLERATE:
350 if (!sdi || !(devc = sdi->priv))
352 *data = g_variant_new_uint64(devc->cur_samplerate);
361 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
362 const struct sr_channel_group *cg)
364 struct dev_context *devc;
368 if (sdi->status != SR_ST_ACTIVE)
369 return SR_ERR_DEV_CLOSED;
371 if (!(devc = sdi->priv))
375 case SR_CONF_SAMPLERATE:
376 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
379 case SR_CONF_LIMIT_MSEC:
380 if (g_variant_get_uint64(data) == 0)
382 devc->limit_msec = g_variant_get_uint64(data);
384 case SR_CONF_LIMIT_SAMPLES:
385 if (g_variant_get_uint64(data) == 0)
387 devc->limit_samples = g_variant_get_uint64(data);
396 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
397 const struct sr_channel_group *cg)
399 GVariant *gvar, *grange[2];
401 struct dev_context *devc;
406 case SR_CONF_SCAN_OPTIONS:
407 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
408 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
410 case SR_CONF_DEVICE_OPTIONS:
412 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
413 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
415 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
416 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
418 case SR_CONF_SAMPLERATE:
419 if (!sdi || !sdi->priv || !(devc = sdi->priv))
421 cv_fill_samplerates_if_needed(sdi);
422 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
423 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
425 ARRAY_SIZE(devc->samplerates),
427 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
428 *data = g_variant_builder_end(&gvb);
430 case SR_CONF_LIMIT_SAMPLES:
431 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
433 grange[0] = g_variant_new_uint64(0);
434 if (devc->prof->model == CHRONOVU_LA8)
435 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES);
437 grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES / 2);
438 *data = g_variant_new_tuple(grange, 2);
440 case SR_CONF_TRIGGER_MATCH:
441 if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof)
443 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
444 trigger_matches, devc->prof->num_trigger_matches,
454 static int receive_data(int fd, int revents, void *cb_data)
457 struct sr_dev_inst *sdi;
458 struct dev_context *devc;
463 if (!(sdi = cb_data)) {
464 sr_err("cb_data was NULL.");
468 if (!(devc = sdi->priv)) {
469 sr_err("sdi->priv was NULL.");
474 sr_err("devc->ftdic was NULL.");
478 /* Get one block of data. */
479 if ((ret = cv_read_block(devc)) < 0) {
480 sr_err("Failed to read data block: %d.", ret);
481 dev_acquisition_stop(sdi, sdi);
485 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
486 if (devc->block_counter != (NUM_BLOCKS - 1)) {
487 devc->block_counter++;
491 sr_dbg("Sampling finished, sending data to session bus now.");
494 * All data was received and demangled, send it to the session bus.
496 * Note: Due to the method how data is spread across the 8MByte of
497 * SDRAM, we can _not_ send it to the session bus in a streaming
498 * manner while we receive it. We have to receive and de-mangle the
499 * full 8MByte first, only then the whole buffer contains valid data.
501 for (i = 0; i < NUM_BLOCKS; i++)
502 cv_send_block_to_session_bus(devc, i);
504 dev_acquisition_stop(sdi, sdi);
509 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
511 struct dev_context *devc;
513 int bytes_to_write, bytes_written;
515 if (sdi->status != SR_ST_ACTIVE)
516 return SR_ERR_DEV_CLOSED;
518 if (!(devc = sdi->priv)) {
519 sr_err("sdi->priv was NULL.");
524 sr_err("devc->ftdic was NULL.");
528 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
529 if (devc->divcount == 0xff) {
530 sr_err("Invalid divcount/samplerate.");
534 if (cv_convert_trigger(sdi) != SR_OK) {
535 sr_err("Failed to configure trigger.");
539 /* Fill acquisition parameters into buf[]. */
540 if (devc->prof->model == CHRONOVU_LA8) {
541 buf[0] = devc->divcount;
542 buf[1] = 0xff; /* This byte must always be 0xff. */
543 buf[2] = devc->trigger_pattern & 0xff;
544 buf[3] = devc->trigger_mask & 0xff;
547 buf[0] = devc->divcount;
548 buf[1] = 0xff; /* This byte must always be 0xff. */
549 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
550 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
551 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
552 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
553 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
554 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
558 /* Start acquisition. */
559 bytes_written = cv_write(devc, buf, bytes_to_write);
561 if (bytes_written < 0 || bytes_written != bytes_to_write) {
562 sr_err("Acquisition failed to start.");
566 sr_dbg("Hardware acquisition started successfully.");
568 devc->cb_data = cb_data;
570 /* Send header packet to the session bus. */
571 std_session_send_df_header(sdi, LOG_PREFIX);
573 /* Time when we should be done (for detecting trigger timeouts). */
574 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
575 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
576 devc->block_counter = 0;
577 devc->trigger_found = 0;
579 /* Hook up a dummy handler to receive data from the device. */
580 sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi);
585 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
587 struct sr_datafeed_packet packet;
591 sr_dbg("Stopping acquisition.");
592 sr_session_source_remove(sdi->session, -1);
594 /* Send end packet to the session bus. */
595 sr_dbg("Sending SR_DF_END.");
596 packet.type = SR_DF_END;
597 sr_session_send(sdi, &packet);
602 SR_PRIV struct sr_dev_driver chronovu_la_driver_info = {
603 .name = "chronovu-la",
604 .longname = "ChronoVu LA8/LA16",
609 .dev_list = dev_list,
610 .dev_clear = dev_clear,
611 .config_get = config_get,
612 .config_set = config_set,
613 .config_list = config_list,
614 .dev_open = dev_open,
615 .dev_close = dev_close,
616 .dev_acquisition_start = dev_acquisition_start,
617 .dev_acquisition_stop = dev_acquisition_stop,