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);
175 ftdi_free(devc->ftdic);
177 g_free(devc->data_buf);
181 static GSList *scan_all(struct sr_dev_driver *di, GSList *options)
184 struct ftdi_device_list *devlist = 0;
185 struct ftdi_device_list *curdev;
186 struct ftdi_context *ftdic;
193 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
196 sr_err("Failed to initialize libftdi.");
200 ret = ftdi_usb_find_all(ftdic, &devlist, 0, 0);
202 sr_err("Failed to list devices (%d): %s", ret,
203 ftdi_get_error_string(ftdic));
207 sr_dbg("Number of FTDI devices found: %d", ret);
211 scan_device(di, curdev->dev, &devices);
212 curdev = curdev->next;
215 ftdi_list_free(&devlist);
221 ftdi_free(ftdic); /* NOT free() or g_free()! */
226 static GSList *scan(struct sr_dev_driver *di, GSList *options)
228 struct sr_config *src;
229 struct sr_usb_dev_inst *usb;
231 GSList *l, *conn_devices;
233 struct drv_context *drvc;
234 libusb_device **devlist;
238 drvc->instances = NULL;
240 for (l = options; l; l = l->next) {
242 if (src->key == SR_CONF_CONN) {
243 conn = g_variant_get_string(src->data, NULL);
249 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
250 for (i = 0; devlist[i]; i++) {
251 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
252 for (l = conn_devices; l; l = l->next) {
254 if (usb->bus == libusb_get_bus_number(devlist[i])
255 && usb->address == libusb_get_device_address(devlist[i])) {
256 scan_device(di, devlist[i], &devices);
260 libusb_free_device_list(devlist, 1);
263 return scan_all(di, options);
266 static GSList *dev_list(const struct sr_dev_driver *di)
268 return ((struct drv_context *)(di->context))->instances;
271 static void clear_helper(void *priv)
273 struct dev_context *devc;
277 ftdi_free(devc->ftdic);
278 g_free(devc->data_buf);
282 static int dev_clear(const struct sr_dev_driver *di)
284 return std_dev_clear(di, clear_helper);
287 static int dev_open(struct sr_dev_inst *sdi)
289 struct dev_context *devc;
294 ret = ftdi_usb_open_dev(devc->ftdic, devc->usbdev);
296 /* Log errors, except for -3 ("device not found"). */
298 sr_err("Failed to open device (%d): %s", ret,
299 ftdi_get_error_string(devc->ftdic));
303 /* Purge RX/TX buffers in the FTDI chip. */
304 ret = ftdi_usb_purge_buffers(devc->ftdic);
306 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
307 ret, ftdi_get_error_string(devc->ftdic));
308 goto err_dev_open_close_ftdic;
310 sr_dbg("FTDI chip buffers purged successfully.");
312 /* Reset the FTDI bitmode. */
313 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET);
315 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
316 ret, ftdi_get_error_string(devc->ftdic));
317 goto err_dev_open_close_ftdic;
319 sr_dbg("FTDI chip bitmode reset successfully.");
321 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG);
323 sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.",
324 ret, ftdi_get_error_string(devc->ftdic));
325 goto err_dev_open_close_ftdic;
327 sr_dbg("FTDI chip bitbang mode entered successfully.");
329 sdi->status = SR_ST_ACTIVE;
333 err_dev_open_close_ftdic:
334 ftdi_usb_close(devc->ftdic);
338 static int dev_close(struct sr_dev_inst *sdi)
340 struct dev_context *devc;
344 ftdi_usb_close(devc->ftdic);
346 sdi->status = SR_ST_INACTIVE;
351 static int cleanup(const struct sr_dev_driver *di)
355 /* TODO: Free other driver resources, if any. */
360 static int config_get(uint32_t key, GVariant **data,
361 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
364 struct dev_context *devc;
365 struct sr_usb_dev_inst *usb;
374 case SR_CONF_SAMPLERATE:
375 *data = g_variant_new_uint64(devc->cur_samplerate);
378 if (!sdi || !sdi->conn)
381 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
382 *data = g_variant_new_string(str);
391 static int config_set(uint32_t key, GVariant *data,
392 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
395 struct dev_context *devc;
400 if (sdi->status != SR_ST_ACTIVE)
401 return SR_ERR_DEV_CLOSED;
407 case SR_CONF_LIMIT_MSEC:
408 value = g_variant_get_uint64(data);
409 /* TODO: Implement. */
412 case SR_CONF_LIMIT_SAMPLES:
413 if (g_variant_get_uint64(data) == 0)
415 devc->limit_samples = g_variant_get_uint64(data);
417 case SR_CONF_SAMPLERATE:
418 value = g_variant_get_uint64(data);
420 return SR_ERR_SAMPLERATE;
421 devc->cur_samplerate = value;
422 return ftdi_la_set_samplerate(devc);
430 static int config_list(uint32_t key, GVariant **data,
431 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
442 case SR_CONF_SCAN_OPTIONS:
443 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
444 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
446 case SR_CONF_DEVICE_OPTIONS:
447 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
448 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
450 case SR_CONF_SAMPLERATE:
451 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
452 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
453 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
454 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
455 *data = g_variant_builder_end(&gvb);
464 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
466 struct dev_context *devc;
470 if (sdi->status != SR_ST_ACTIVE)
471 return SR_ERR_DEV_CLOSED;
476 ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG);
478 /* Properly reset internal variables before every new acquisition. */
479 devc->samples_sent = 0;
480 devc->bytes_received = 0;
482 std_session_send_df_header(sdi, LOG_PREFIX);
484 /* Hook up a dummy handler to receive data from the device. */
485 sr_session_source_add(sdi->session, -1, G_IO_IN, 0,
486 ftdi_la_receive_data, (void *)sdi);
491 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
493 if (sdi->status != SR_ST_ACTIVE)
494 return SR_ERR_DEV_CLOSED;
496 sr_dbg("Stopping acquisition.");
497 sr_session_source_remove(sdi->session, -1);
499 std_session_send_df_end(sdi, LOG_PREFIX);
504 SR_PRIV struct sr_dev_driver ftdi_la_driver_info = {
506 .longname = "FTDI LA",
511 .dev_list = dev_list,
512 .dev_clear = dev_clear,
513 .config_get = config_get,
514 .config_set = config_set,
515 .config_list = config_list,
516 .dev_open = dev_open,
517 .dev_close = dev_close,
518 .dev_acquisition_start = dev_acquisition_start,
519 .dev_acquisition_stop = dev_acquisition_stop,