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 drvopts[] = {
32 SR_CONF_LOGIC_ANALYZER,
35 static const uint32_t devopts[] = {
37 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
38 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_CONN | SR_CONF_GET,
42 static const uint64_t samplerates[] = {
48 static const struct ftdi_chip_desc ft2232h_desc = {
53 "ADBUS0", "ADBUS1", "ADBUS2", "ADBUS3",
54 "ADBUS4", "ADBUS5", "ADBUS6", "ADBUS7",
55 /* TODO: BDBUS[0..7] channels. */
60 static const struct ftdi_chip_desc ft232r_desc = {
65 "TXD", "RXD", "RTS#", "CTS#", "DTR#", "DSR#", "DCD#", "RI#",
70 static const struct ftdi_chip_desc ft232h_desc = {
75 "ADBUS0", "ADBUS1", "ADBUS2", "ADBUS3", "ADBUS4", "ADBUS5", "ADBUS6", "ADBUS7",
80 static const struct ftdi_chip_desc *chip_descs[] = {
86 static void scan_device(struct ftdi_context *ftdic,
87 struct libusb_device *dev, GSList **devices)
89 struct libusb_device_descriptor usb_desc;
90 const struct ftdi_chip_desc *desc;
91 struct dev_context *devc;
92 char *vendor, *model, *serial_num;
93 struct sr_dev_inst *sdi;
96 libusb_get_device_descriptor(dev, &usb_desc);
99 for (unsigned long i = 0; i < ARRAY_SIZE(chip_descs); i++) {
100 desc = chip_descs[i];
101 if (desc->vendor == usb_desc.idVendor &&
102 desc->product == usb_desc.idProduct)
107 sr_spew("Unsupported FTDI device 0x%4x:0x%4x.",
108 usb_desc.idVendor, usb_desc.idProduct);
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 sdi = g_malloc0(sizeof(struct sr_dev_inst));
139 sdi->status = SR_ST_INACTIVE;
140 sdi->vendor = vendor;
142 sdi->serial_num = serial_num;
144 sdi->connection_id = g_strdup_printf("d:%u/%u",
145 libusb_get_bus_number(dev), libusb_get_device_address(dev));
147 for (char *const *chan = &(desc->channel_names[0]); *chan; chan++)
148 sr_channel_new(sdi, chan - &(desc->channel_names[0]),
149 SR_CHANNEL_LOGIC, TRUE, *chan);
151 *devices = g_slist_append(*devices, sdi);
158 g_free(devc->data_buf);
162 static GSList *scan_all(struct ftdi_context *ftdic, GSList *options)
165 struct ftdi_device_list *devlist = 0;
166 struct ftdi_device_list *curdev;
173 ret = ftdi_usb_find_all(ftdic, &devlist, 0, 0);
175 sr_err("Failed to list devices (%d): %s", ret,
176 ftdi_get_error_string(ftdic));
182 scan_device(ftdic, curdev->dev, &devices);
183 curdev = curdev->next;
186 ftdi_list_free(&devlist);
191 static GSList *scan(struct sr_dev_driver *di, GSList *options)
193 struct ftdi_context *ftdic;
194 struct sr_config *src;
195 struct sr_usb_dev_inst *usb;
197 GSList *l, *conn_devices;
199 struct drv_context *drvc;
200 libusb_device **devlist;
205 for (l = options; l; l = l->next) {
207 if (src->key == SR_CONF_CONN) {
208 conn = g_variant_get_string(src->data, NULL);
215 sr_err("Failed to initialize libftdi.");
221 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
222 for (i = 0; devlist[i]; i++) {
223 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
224 for (l = conn_devices; l; l = l->next) {
226 if (usb->bus == libusb_get_bus_number(devlist[i])
227 && usb->address == libusb_get_device_address(devlist[i])) {
228 scan_device(ftdic, devlist[i], &devices);
232 libusb_free_device_list(devlist, 1);
234 devices = scan_all(ftdic, options);
238 return std_scan_complete(di, devices);
241 static void clear_helper(struct dev_context *devc)
243 g_free(devc->data_buf);
246 static int dev_clear(const struct sr_dev_driver *di)
248 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
251 static int dev_open(struct sr_dev_inst *sdi)
253 struct dev_context *devc;
258 devc->ftdic = ftdi_new();
262 ret = ftdi_usb_open_string(devc->ftdic, sdi->connection_id);
264 /* Log errors, except for -3 ("device not found"). */
266 sr_err("Failed to open device (%d): %s", ret,
267 ftdi_get_error_string(devc->ftdic));
271 ret = ftdi_usb_purge_buffers(devc->ftdic);
273 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
274 ret, ftdi_get_error_string(devc->ftdic));
275 goto err_dev_open_close_ftdic;
278 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET);
280 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
281 ret, ftdi_get_error_string(devc->ftdic));
282 goto err_dev_open_close_ftdic;
285 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG);
287 sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.",
288 ret, ftdi_get_error_string(devc->ftdic));
289 goto err_dev_open_close_ftdic;
294 err_dev_open_close_ftdic:
295 ftdi_usb_close(devc->ftdic);
298 ftdi_free(devc->ftdic);
303 static int dev_close(struct sr_dev_inst *sdi)
305 struct dev_context *devc;
312 ftdi_usb_close(devc->ftdic);
313 ftdi_free(devc->ftdic);
319 static int config_get(uint32_t key, GVariant **data,
320 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
322 struct dev_context *devc;
323 struct sr_usb_dev_inst *usb;
330 case SR_CONF_SAMPLERATE:
331 *data = g_variant_new_uint64(devc->cur_samplerate);
334 if (!sdi || !sdi->conn)
337 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
346 static int config_set(uint32_t key, GVariant *data,
347 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
349 struct dev_context *devc;
357 case SR_CONF_LIMIT_MSEC:
358 value = g_variant_get_uint64(data);
359 /* TODO: Implement. */
362 case SR_CONF_LIMIT_SAMPLES:
363 devc->limit_samples = g_variant_get_uint64(data);
365 case SR_CONF_SAMPLERATE:
366 value = g_variant_get_uint64(data);
368 return SR_ERR_SAMPLERATE;
369 devc->cur_samplerate = value;
370 return ftdi_la_set_samplerate(devc);
378 static int config_list(uint32_t key, GVariant **data,
379 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
382 case SR_CONF_SCAN_OPTIONS:
383 case SR_CONF_DEVICE_OPTIONS:
384 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
385 case SR_CONF_SAMPLERATE:
386 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
395 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
397 struct dev_context *devc;
404 ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG);
406 /* Properly reset internal variables before every new acquisition. */
407 devc->samples_sent = 0;
408 devc->bytes_received = 0;
410 std_session_send_df_header(sdi);
412 /* Hook up a dummy handler to receive data from the device. */
413 sr_session_source_add(sdi->session, -1, G_IO_IN, 0,
414 ftdi_la_receive_data, (void *)sdi);
419 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
421 sr_session_source_remove(sdi->session, -1);
423 std_session_send_df_end(sdi);
428 static struct sr_dev_driver ftdi_la_driver_info = {
430 .longname = "FTDI LA",
433 .cleanup = std_cleanup,
435 .dev_list = std_dev_list,
436 .dev_clear = dev_clear,
437 .config_get = config_get,
438 .config_set = config_set,
439 .config_list = config_list,
440 .dev_open = dev_open,
441 .dev_close = dev_close,
442 .dev_acquisition_start = dev_acquisition_start,
443 .dev_acquisition_stop = dev_acquisition_stop,
446 SR_REGISTER_DEV_DRIVER(ftdi_la_driver_info);