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 SR_PRIV struct sr_dev_driver ftdi_la_driver_info;
29 static const uint32_t scanopts[] = {
33 static const uint32_t devopts[] = {
34 SR_CONF_LOGIC_ANALYZER,
35 SR_CONF_CONTINUOUS | SR_CONF_SET,
36 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
37 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38 SR_CONF_CONN | SR_CONF_GET,
41 static const uint64_t samplerates[] = {
47 static const struct ftdi_chip_desc ft2232h_desc = {
60 /* TODO: BDBUS[0..7] channels. */
65 static const struct ftdi_chip_desc ft232r_desc = {
82 static const struct ftdi_chip_desc *chip_descs[] = {
87 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
89 return std_init(sr_ctx, di, LOG_PREFIX);
92 static void scan_device(struct sr_dev_driver *di, struct libusb_device *dev, GSList **devices)
94 struct libusb_device_descriptor usb_desc;
95 const struct ftdi_chip_desc *desc;
96 struct dev_context *devc;
97 char *vendor, *model, *serial_num;
98 struct sr_dev_inst *sdi;
99 struct drv_context *drvc;
103 libusb_get_device_descriptor(dev, &usb_desc);
106 for (unsigned long i = 0; i < ARRAY_SIZE(chip_descs); i++) {
107 desc = chip_descs[i];
108 if (desc->vendor == usb_desc.idVendor &&
109 desc->product == usb_desc.idProduct)
114 sr_spew("Unsupported FTDI device 0x%4x:0x%4x.",
115 usb_desc.idVendor, usb_desc.idProduct);
119 /* Allocate memory for our private device context. */
120 devc = g_malloc0(sizeof(struct dev_context));
122 /* Allocate memory for the incoming data. */
123 devc->data_buf = g_malloc0(DATA_BUF_SIZE);
125 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
126 devc->ftdic = ftdi_new();
128 sr_err("Failed to initialize libftdi.");
129 goto err_free_data_buf;
135 vendor = g_malloc(32);
136 model = g_malloc(32);
137 serial_num = g_malloc(32);
138 rv = ftdi_usb_get_strings(devc->ftdic, dev, vendor, 32,
139 model, 32, serial_num, 32);
144 sr_dbg("The device lacks a serial number.");
149 sr_err("Failed to get the FTDI strings: %d", rv);
150 goto err_free_strings;
152 sr_dbg("Found an FTDI device: %s.", model);
154 /* Register the device with libsigrok. */
155 sdi = g_malloc0(sizeof(struct sr_dev_inst));
156 sdi->status = SR_ST_INITIALIZING;
157 sdi->vendor = vendor;
159 sdi->serial_num = serial_num;
163 for (char *const *chan = &(desc->channel_names[0]); *chan; chan++)
164 sr_channel_new(sdi, chan - &(desc->channel_names[0]),
165 SR_CHANNEL_LOGIC, TRUE, *chan);
167 *devices = g_slist_append(*devices, sdi);
168 drvc->instances = g_slist_append(drvc->instances, sdi);
176 g_free(devc->data_buf);
180 static GSList *scan_all(struct sr_dev_driver *di, GSList *options)
183 struct ftdi_device_list *devlist = 0;
184 struct ftdi_device_list *curdev;
185 struct ftdi_context *ftdic;
192 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
195 sr_err("Failed to initialize libftdi.");
199 ret = ftdi_usb_find_all(ftdic, &devlist, 0, 0);
201 sr_err("Failed to list devices (%d): %s", ret,
202 ftdi_get_error_string(ftdic));
206 sr_dbg("Number of FTDI devices found: %d", ret);
210 scan_device(di, curdev->dev, &devices);
211 curdev = curdev->next;
217 ftdi_free(ftdic); /* NOT free() or g_free()! */
222 static GSList *scan(struct sr_dev_driver *di, GSList *options)
224 struct sr_config *src;
225 struct sr_usb_dev_inst *usb;
227 GSList *l, *conn_devices;
229 struct drv_context *drvc;
230 libusb_device **devlist;
234 drvc->instances = NULL;
236 for (l = options; l; l = l->next) {
238 if (src->key == SR_CONF_CONN) {
239 conn = g_variant_get_string(src->data, NULL);
245 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
246 for (i = 0; devlist[i]; i++) {
247 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
248 for (l = conn_devices; l; l = l->next) {
250 if (usb->bus == libusb_get_bus_number(devlist[i])
251 && usb->address == libusb_get_device_address(devlist[i])) {
252 scan_device(di, devlist[i], &devices);
256 libusb_free_device_list(devlist, 1);
259 return scan_all(di, options);
262 static GSList *dev_list(const struct sr_dev_driver *di)
264 return ((struct drv_context *)(di->context))->instances;
267 static void clear_helper(void *priv)
269 struct dev_context *devc;
273 ftdi_free(devc->ftdic);
274 g_free(devc->data_buf);
278 static int dev_clear(const struct sr_dev_driver *di)
280 return std_dev_clear(di, clear_helper);
283 static int dev_open(struct sr_dev_inst *sdi)
285 struct dev_context *devc;
290 ret = ftdi_usb_open_dev(devc->ftdic, devc->usbdev);
292 /* Log errors, except for -3 ("device not found"). */
294 sr_err("Failed to open device (%d): %s", ret,
295 ftdi_get_error_string(devc->ftdic));
299 /* Purge RX/TX buffers in the FTDI chip. */
300 ret = ftdi_usb_purge_buffers(devc->ftdic);
302 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
303 ret, ftdi_get_error_string(devc->ftdic));
304 goto err_dev_open_close_ftdic;
306 sr_dbg("FTDI chip buffers purged successfully.");
308 /* Reset the FTDI bitmode. */
309 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET);
311 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
312 ret, ftdi_get_error_string(devc->ftdic));
313 goto err_dev_open_close_ftdic;
315 sr_dbg("FTDI chip bitmode reset successfully.");
317 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG);
319 sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.",
320 ret, ftdi_get_error_string(devc->ftdic));
321 goto err_dev_open_close_ftdic;
323 sr_dbg("FTDI chip bitbang mode entered successfully.");
325 sdi->status = SR_ST_ACTIVE;
329 err_dev_open_close_ftdic:
330 ftdi_usb_close(devc->ftdic);
334 static int dev_close(struct sr_dev_inst *sdi)
336 struct dev_context *devc;
340 ftdi_usb_close(devc->ftdic);
342 sdi->status = SR_ST_INACTIVE;
347 static int cleanup(const struct sr_dev_driver *di)
351 /* TODO: Free other driver resources, if any. */
356 static int config_get(uint32_t key, GVariant **data,
357 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
360 struct dev_context *devc;
361 struct sr_usb_dev_inst *usb;
370 case SR_CONF_SAMPLERATE:
371 *data = g_variant_new_uint64(devc->cur_samplerate);
374 if (!sdi || !sdi->conn)
377 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
378 *data = g_variant_new_string(str);
387 static int config_set(uint32_t key, GVariant *data,
388 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
391 struct dev_context *devc;
396 if (sdi->status != SR_ST_ACTIVE)
397 return SR_ERR_DEV_CLOSED;
403 case SR_CONF_LIMIT_MSEC:
404 value = g_variant_get_uint64(data);
405 /* TODO: Implement. */
408 case SR_CONF_LIMIT_SAMPLES:
409 if (g_variant_get_uint64(data) == 0)
411 devc->limit_samples = g_variant_get_uint64(data);
413 case SR_CONF_SAMPLERATE:
414 value = g_variant_get_uint64(data);
416 return SR_ERR_SAMPLERATE;
417 devc->cur_samplerate = value;
418 return ftdi_la_set_samplerate(devc);
426 static int config_list(uint32_t key, GVariant **data,
427 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
438 case SR_CONF_SCAN_OPTIONS:
439 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
440 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
442 case SR_CONF_DEVICE_OPTIONS:
443 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
444 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
446 case SR_CONF_SAMPLERATE:
447 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
448 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
449 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
450 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
451 *data = g_variant_builder_end(&gvb);
460 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
462 struct dev_context *devc;
468 if (sdi->status != SR_ST_ACTIVE)
469 return SR_ERR_DEV_CLOSED;
474 ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG);
476 devc->cb_data = cb_data;
478 /* Properly reset internal variables before every new acquisition. */
479 devc->samples_sent = 0;
480 devc->bytes_received = 0;
482 /* Send header packet to the session bus. */
483 std_session_send_df_header(sdi, LOG_PREFIX);
485 /* Hook up a dummy handler to receive data from the device. */
486 sr_session_source_add(sdi->session, -1, G_IO_IN, 0,
487 ftdi_la_receive_data, (void *)sdi);
492 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
494 struct sr_datafeed_packet packet;
498 if (sdi->status != SR_ST_ACTIVE)
499 return SR_ERR_DEV_CLOSED;
501 sr_dbg("Stopping acquisition.");
502 sr_session_source_remove(sdi->session, -1);
504 /* Send end packet to the session bus. */
505 sr_dbg("Sending SR_DF_END.");
506 packet.type = SR_DF_END;
507 sr_session_send(sdi, &packet);
512 SR_PRIV struct sr_dev_driver ftdi_la_driver_info = {
514 .longname = "FTDI LA",
519 .dev_list = dev_list,
520 .dev_clear = dev_clear,
521 .config_get = config_get,
522 .config_set = config_set,
523 .config_list = config_list,
524 .dev_open = dev_open,
525 .dev_close = dev_close,
526 .dev_acquisition_start = dev_acquisition_start,
527 .dev_acquisition_stop = dev_acquisition_stop,