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 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;
95 libusb_get_device_descriptor(dev, &usb_desc);
98 for (unsigned long i = 0; i < ARRAY_SIZE(chip_descs); i++) {
100 if (desc->vendor == usb_desc.idVendor &&
101 desc->product == usb_desc.idProduct)
106 sr_spew("Unsupported FTDI device 0x%4x:0x%4x.",
107 usb_desc.idVendor, usb_desc.idProduct);
111 /* Allocate memory for our private device context. */
112 devc = g_malloc0(sizeof(struct dev_context));
114 /* Allocate memory for the incoming data. */
115 devc->data_buf = g_malloc0(DATA_BUF_SIZE);
119 vendor = g_malloc(32);
120 model = g_malloc(32);
121 serial_num = g_malloc(32);
122 rv = ftdi_usb_get_strings(ftdic, dev, vendor, 32,
123 model, 32, serial_num, 32);
128 sr_dbg("The device lacks a serial number.");
133 sr_err("Failed to get the FTDI strings: %d", rv);
134 goto err_free_strings;
136 sr_dbg("Found an FTDI device: %s.", model);
138 /* Register the device with libsigrok. */
139 sdi = g_malloc0(sizeof(struct sr_dev_inst));
140 sdi->status = SR_ST_INACTIVE;
141 sdi->vendor = vendor;
143 sdi->serial_num = serial_num;
145 sdi->connection_id = g_strdup_printf("d:%u/%u",
146 libusb_get_bus_number(dev), libusb_get_device_address(dev));
148 for (char *const *chan = &(desc->channel_names[0]); *chan; chan++)
149 sr_channel_new(sdi, chan - &(desc->channel_names[0]),
150 SR_CHANNEL_LOGIC, TRUE, *chan);
152 *devices = g_slist_append(*devices, sdi);
159 g_free(devc->data_buf);
163 static GSList *scan_all(struct ftdi_context *ftdic, GSList *options)
166 struct ftdi_device_list *devlist = 0;
167 struct ftdi_device_list *curdev;
174 ret = ftdi_usb_find_all(ftdic, &devlist, 0, 0);
176 sr_err("Failed to list devices (%d): %s", ret,
177 ftdi_get_error_string(ftdic));
183 scan_device(ftdic, curdev->dev, &devices);
184 curdev = curdev->next;
187 ftdi_list_free(&devlist);
192 static GSList *scan(struct sr_dev_driver *di, GSList *options)
194 struct ftdi_context *ftdic;
195 struct sr_config *src;
196 struct sr_usb_dev_inst *usb;
198 GSList *l, *conn_devices;
200 struct drv_context *drvc;
201 libusb_device **devlist;
206 for (l = options; l; l = l->next) {
208 if (src->key == SR_CONF_CONN) {
209 conn = g_variant_get_string(src->data, NULL);
214 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
217 sr_err("Failed to initialize libftdi.");
223 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
224 for (i = 0; devlist[i]; i++) {
225 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
226 for (l = conn_devices; l; l = l->next) {
228 if (usb->bus == libusb_get_bus_number(devlist[i])
229 && usb->address == libusb_get_device_address(devlist[i])) {
230 scan_device(ftdic, devlist[i], &devices);
234 libusb_free_device_list(devlist, 1);
236 devices = scan_all(ftdic, options);
240 return std_scan_complete(di, devices);
243 static void clear_helper(void *priv)
245 struct dev_context *devc;
248 g_free(devc->data_buf);
251 static int dev_clear(const struct sr_dev_driver *di)
253 return std_dev_clear_with_callback(di, clear_helper);
256 static int dev_open(struct sr_dev_inst *sdi)
258 struct dev_context *devc;
263 devc->ftdic = ftdi_new();
267 ret = ftdi_usb_open_string(devc->ftdic, sdi->connection_id);
269 /* Log errors, except for -3 ("device not found"). */
271 sr_err("Failed to open device (%d): %s", ret,
272 ftdi_get_error_string(devc->ftdic));
276 /* Purge RX/TX buffers in the FTDI chip. */
277 ret = ftdi_usb_purge_buffers(devc->ftdic);
279 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
280 ret, ftdi_get_error_string(devc->ftdic));
281 goto err_dev_open_close_ftdic;
284 /* Reset the FTDI bitmode. */
285 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET);
287 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
288 ret, ftdi_get_error_string(devc->ftdic));
289 goto err_dev_open_close_ftdic;
292 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG);
294 sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.",
295 ret, ftdi_get_error_string(devc->ftdic));
296 goto err_dev_open_close_ftdic;
301 err_dev_open_close_ftdic:
302 ftdi_usb_close(devc->ftdic);
305 ftdi_free(devc->ftdic);
310 static int dev_close(struct sr_dev_inst *sdi)
312 struct dev_context *devc;
319 ftdi_usb_close(devc->ftdic);
320 ftdi_free(devc->ftdic);
326 static int config_get(uint32_t key, GVariant **data,
327 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
330 struct dev_context *devc;
331 struct sr_usb_dev_inst *usb;
340 case SR_CONF_SAMPLERATE:
341 *data = g_variant_new_uint64(devc->cur_samplerate);
344 if (!sdi || !sdi->conn)
347 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
348 *data = g_variant_new_string(str);
357 static int config_set(uint32_t key, GVariant *data,
358 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
361 struct dev_context *devc;
370 case SR_CONF_LIMIT_MSEC:
371 value = g_variant_get_uint64(data);
372 /* TODO: Implement. */
375 case SR_CONF_LIMIT_SAMPLES:
376 if (g_variant_get_uint64(data) == 0)
378 devc->limit_samples = g_variant_get_uint64(data);
380 case SR_CONF_SAMPLERATE:
381 value = g_variant_get_uint64(data);
383 return SR_ERR_SAMPLERATE;
384 devc->cur_samplerate = value;
385 return ftdi_la_set_samplerate(devc);
393 static int config_list(uint32_t key, GVariant **data,
394 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
405 case SR_CONF_SCAN_OPTIONS:
406 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
407 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
409 case SR_CONF_DEVICE_OPTIONS:
410 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
411 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
413 case SR_CONF_SAMPLERATE:
414 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
415 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
416 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
417 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
418 *data = g_variant_builder_end(&gvb);
427 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
429 struct dev_context *devc;
436 ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG);
438 /* Properly reset internal variables before every new acquisition. */
439 devc->samples_sent = 0;
440 devc->bytes_received = 0;
442 std_session_send_df_header(sdi);
444 /* Hook up a dummy handler to receive data from the device. */
445 sr_session_source_add(sdi->session, -1, G_IO_IN, 0,
446 ftdi_la_receive_data, (void *)sdi);
451 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
453 sr_session_source_remove(sdi->session, -1);
455 std_session_send_df_end(sdi);
460 static struct sr_dev_driver ftdi_la_driver_info = {
462 .longname = "FTDI LA",
465 .cleanup = std_cleanup,
467 .dev_list = std_dev_list,
468 .dev_clear = dev_clear,
469 .config_get = config_get,
470 .config_set = config_set,
471 .config_list = config_list,
472 .dev_open = dev_open,
473 .dev_close = dev_close,
474 .dev_acquisition_start = dev_acquisition_start,
475 .dev_acquisition_stop = dev_acquisition_stop,
478 SR_REGISTER_DEV_DRIVER(ftdi_la_driver_info);