2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Sergey Alirzaev <zl29ah@gmail.com>
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
27 static const uint32_t scanopts[] = {
31 static const uint32_t devopts[] = {
32 SR_CONF_LOGIC_ANALYZER,
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
35 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
36 SR_CONF_CONN | SR_CONF_GET,
39 static const uint64_t samplerates[] = {
45 static const struct ftdi_chip_desc ft2232h_desc = {
58 /* TODO: BDBUS[0..7] channels. */
63 static const struct ftdi_chip_desc ft232r_desc = {
80 static const struct ftdi_chip_desc *chip_descs[] = {
85 static void scan_device(struct sr_dev_driver *di, struct ftdi_context *ftdic,
86 struct libusb_device *dev, GSList **devices)
88 struct libusb_device_descriptor usb_desc;
89 const struct ftdi_chip_desc *desc;
90 struct dev_context *devc;
91 char *vendor, *model, *serial_num;
92 struct sr_dev_inst *sdi;
93 struct drv_context *drvc;
97 libusb_get_device_descriptor(dev, &usb_desc);
100 for (unsigned long i = 0; i < ARRAY_SIZE(chip_descs); i++) {
101 desc = chip_descs[i];
102 if (desc->vendor == usb_desc.idVendor &&
103 desc->product == usb_desc.idProduct)
108 sr_spew("Unsupported FTDI device 0x%4x:0x%4x.",
109 usb_desc.idVendor, usb_desc.idProduct);
113 /* Allocate memory for our private device context. */
114 devc = g_malloc0(sizeof(struct dev_context));
116 /* Allocate memory for the incoming data. */
117 devc->data_buf = g_malloc0(DATA_BUF_SIZE);
121 vendor = g_malloc(32);
122 model = g_malloc(32);
123 serial_num = g_malloc(32);
124 rv = ftdi_usb_get_strings(ftdic, dev, vendor, 32,
125 model, 32, serial_num, 32);
130 sr_dbg("The device lacks a serial number.");
135 sr_err("Failed to get the FTDI strings: %d", rv);
136 goto err_free_strings;
138 sr_dbg("Found an FTDI device: %s.", model);
140 /* Register the device with libsigrok. */
141 sdi = g_malloc0(sizeof(struct sr_dev_inst));
142 sdi->status = SR_ST_INACTIVE;
143 sdi->vendor = vendor;
145 sdi->serial_num = serial_num;
148 sdi->connection_id = g_strdup_printf("d:%u/%u",
149 libusb_get_bus_number(dev), libusb_get_device_address(dev));
151 for (char *const *chan = &(desc->channel_names[0]); *chan; chan++)
152 sr_channel_new(sdi, chan - &(desc->channel_names[0]),
153 SR_CHANNEL_LOGIC, TRUE, *chan);
155 *devices = g_slist_append(*devices, sdi);
156 drvc->instances = g_slist_append(drvc->instances, sdi);
163 g_free(devc->data_buf);
167 static GSList *scan_all(struct ftdi_context *ftdic, struct sr_dev_driver *di,
171 struct ftdi_device_list *devlist = 0;
172 struct ftdi_device_list *curdev;
179 ret = ftdi_usb_find_all(ftdic, &devlist, 0, 0);
181 sr_err("Failed to list devices (%d): %s", ret,
182 ftdi_get_error_string(ftdic));
186 sr_dbg("Number of FTDI devices found: %d", ret);
190 scan_device(di, ftdic, curdev->dev, &devices);
191 curdev = curdev->next;
194 ftdi_list_free(&devlist);
199 static GSList *scan(struct sr_dev_driver *di, GSList *options)
201 struct ftdi_context *ftdic;
202 struct sr_config *src;
203 struct sr_usb_dev_inst *usb;
205 GSList *l, *conn_devices;
207 struct drv_context *drvc;
208 libusb_device **devlist;
212 drvc->instances = NULL;
214 for (l = options; l; l = l->next) {
216 if (src->key == SR_CONF_CONN) {
217 conn = g_variant_get_string(src->data, NULL);
222 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
225 sr_err("Failed to initialize libftdi.");
231 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
232 for (i = 0; devlist[i]; i++) {
233 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
234 for (l = conn_devices; l; l = l->next) {
236 if (usb->bus == libusb_get_bus_number(devlist[i])
237 && usb->address == libusb_get_device_address(devlist[i])) {
238 scan_device(di, ftdic, devlist[i], &devices);
242 libusb_free_device_list(devlist, 1);
244 devices = scan_all(ftdic, di, options);
251 static void clear_helper(void *priv)
253 struct dev_context *devc;
256 g_free(devc->data_buf);
260 static int dev_clear(const struct sr_dev_driver *di)
262 return std_dev_clear(di, clear_helper);
265 static int dev_open(struct sr_dev_inst *sdi)
267 struct dev_context *devc;
272 devc->ftdic = ftdi_new();
276 ret = ftdi_usb_open_string(devc->ftdic, sdi->connection_id);
278 /* Log errors, except for -3 ("device not found"). */
280 sr_err("Failed to open device (%d): %s", ret,
281 ftdi_get_error_string(devc->ftdic));
285 /* Purge RX/TX buffers in the FTDI chip. */
286 ret = ftdi_usb_purge_buffers(devc->ftdic);
288 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
289 ret, ftdi_get_error_string(devc->ftdic));
290 goto err_dev_open_close_ftdic;
292 sr_dbg("FTDI chip buffers purged successfully.");
294 /* Reset the FTDI bitmode. */
295 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET);
297 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
298 ret, ftdi_get_error_string(devc->ftdic));
299 goto err_dev_open_close_ftdic;
301 sr_dbg("FTDI chip bitmode reset successfully.");
303 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG);
305 sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.",
306 ret, ftdi_get_error_string(devc->ftdic));
307 goto err_dev_open_close_ftdic;
309 sr_dbg("FTDI chip bitbang mode entered successfully.");
311 sdi->status = SR_ST_ACTIVE;
314 err_dev_open_close_ftdic:
315 ftdi_usb_close(devc->ftdic);
317 ftdi_free(devc->ftdic);
321 static int dev_close(struct sr_dev_inst *sdi)
323 struct dev_context *devc;
328 ftdi_usb_close(devc->ftdic);
329 ftdi_free(devc->ftdic);
333 sdi->status = SR_ST_INACTIVE;
338 static int config_get(uint32_t key, GVariant **data,
339 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
342 struct dev_context *devc;
343 struct sr_usb_dev_inst *usb;
352 case SR_CONF_SAMPLERATE:
353 *data = g_variant_new_uint64(devc->cur_samplerate);
356 if (!sdi || !sdi->conn)
359 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
360 *data = g_variant_new_string(str);
369 static int config_set(uint32_t key, GVariant *data,
370 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
373 struct dev_context *devc;
378 if (sdi->status != SR_ST_ACTIVE)
379 return SR_ERR_DEV_CLOSED;
385 case SR_CONF_LIMIT_MSEC:
386 value = g_variant_get_uint64(data);
387 /* TODO: Implement. */
390 case SR_CONF_LIMIT_SAMPLES:
391 if (g_variant_get_uint64(data) == 0)
393 devc->limit_samples = g_variant_get_uint64(data);
395 case SR_CONF_SAMPLERATE:
396 value = g_variant_get_uint64(data);
398 return SR_ERR_SAMPLERATE;
399 devc->cur_samplerate = value;
400 return ftdi_la_set_samplerate(devc);
408 static int config_list(uint32_t key, GVariant **data,
409 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
420 case SR_CONF_SCAN_OPTIONS:
421 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
422 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
424 case SR_CONF_DEVICE_OPTIONS:
425 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
426 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
428 case SR_CONF_SAMPLERATE:
429 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
430 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
431 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
432 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
433 *data = g_variant_builder_end(&gvb);
442 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
444 struct dev_context *devc;
448 if (sdi->status != SR_ST_ACTIVE)
449 return SR_ERR_DEV_CLOSED;
454 ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG);
456 /* Properly reset internal variables before every new acquisition. */
457 devc->samples_sent = 0;
458 devc->bytes_received = 0;
460 std_session_send_df_header(sdi, LOG_PREFIX);
462 /* Hook up a dummy handler to receive data from the device. */
463 sr_session_source_add(sdi->session, -1, G_IO_IN, 0,
464 ftdi_la_receive_data, (void *)sdi);
469 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
471 if (sdi->status != SR_ST_ACTIVE)
472 return SR_ERR_DEV_CLOSED;
474 sr_dbg("Stopping acquisition.");
475 sr_session_source_remove(sdi->session, -1);
477 std_session_send_df_end(sdi, LOG_PREFIX);
482 static struct sr_dev_driver ftdi_la_driver_info = {
484 .longname = "FTDI LA",
487 .cleanup = std_cleanup,
489 .dev_list = std_dev_list,
490 .dev_clear = dev_clear,
491 .config_get = config_get,
492 .config_set = config_set,
493 .config_list = config_list,
494 .dev_open = dev_open,
495 .dev_close = dev_close,
496 .dev_acquisition_start = dev_acquisition_start,
497 .dev_acquisition_stop = dev_acquisition_stop,
500 SR_REGISTER_DEV_DRIVER(ftdi_la_driver_info);