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 ft2232h_tumpa_desc = {
64 /* 20 pin JTAG header */
66 "TCK", "TDI", "TDO", "TMS", "RST", "nTRST", "DBGRQ", "RTCK",
71 static const struct ftdi_chip_desc ft4232h_desc = {
76 "ADBUS0", "ADBUS1", "ADBUS2", "ADBUS3", "ADBUS4", "ADBUS5", "ADBUS6", "ADBUS7",
77 /* TODO: BDBUS[0..7], CDBUS[0..7], DDBUS[0..7] channels. */
82 static const struct ftdi_chip_desc ft232r_desc = {
87 "TXD", "RXD", "RTS#", "CTS#", "DTR#", "DSR#", "DCD#", "RI#",
92 static const struct ftdi_chip_desc ft232h_desc = {
97 "ADBUS0", "ADBUS1", "ADBUS2", "ADBUS3", "ADBUS4", "ADBUS5", "ADBUS6", "ADBUS7",
102 static const struct ftdi_chip_desc *chip_descs[] = {
111 static void scan_device(struct ftdi_context *ftdic,
112 struct libusb_device *dev, GSList **devices)
114 struct libusb_device_descriptor usb_desc;
115 const struct ftdi_chip_desc *desc;
116 struct dev_context *devc;
117 char *vendor, *model, *serial_num;
118 struct sr_dev_inst *sdi;
121 libusb_get_device_descriptor(dev, &usb_desc);
124 for (unsigned long i = 0; i < ARRAY_SIZE(chip_descs); i++) {
125 desc = chip_descs[i];
128 if (desc->vendor == usb_desc.idVendor &&
129 desc->product == usb_desc.idProduct)
134 sr_spew("Unsupported FTDI device 0x%04x:0x%04x.",
135 usb_desc.idVendor, usb_desc.idProduct);
139 devc = g_malloc0(sizeof(struct dev_context));
141 /* Allocate memory for the incoming data. */
142 devc->data_buf = g_malloc0(DATA_BUF_SIZE);
146 vendor = g_malloc(32);
147 model = g_malloc(32);
148 serial_num = g_malloc(32);
149 rv = ftdi_usb_get_strings(ftdic, dev, vendor, 32,
150 model, 32, serial_num, 32);
155 sr_dbg("The device lacks a serial number.");
160 sr_err("Failed to get the FTDI strings: %d", rv);
161 goto err_free_strings;
163 sr_dbg("Found an FTDI device: %s.", model);
165 sdi = g_malloc0(sizeof(struct sr_dev_inst));
166 sdi->status = SR_ST_INACTIVE;
167 sdi->vendor = vendor;
169 sdi->serial_num = serial_num;
171 sdi->connection_id = g_strdup_printf("d:%u/%u",
172 libusb_get_bus_number(dev), libusb_get_device_address(dev));
174 for (char *const *chan = &(desc->channel_names[0]); *chan; chan++)
175 sr_channel_new(sdi, chan - &(desc->channel_names[0]),
176 SR_CHANNEL_LOGIC, TRUE, *chan);
178 *devices = g_slist_append(*devices, sdi);
185 g_free(devc->data_buf);
189 static GSList *scan_all(struct ftdi_context *ftdic, GSList *options)
192 struct ftdi_device_list *devlist = 0;
193 struct ftdi_device_list *curdev;
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));
209 scan_device(ftdic, curdev->dev, &devices);
210 curdev = curdev->next;
213 ftdi_list_free(&devlist);
218 static GSList *scan(struct sr_dev_driver *di, GSList *options)
220 struct ftdi_context *ftdic;
221 struct sr_config *src;
222 struct sr_usb_dev_inst *usb;
224 GSList *l, *conn_devices;
226 struct drv_context *drvc;
227 libusb_device **devlist;
232 for (l = options; l; l = l->next) {
234 if (src->key == SR_CONF_CONN) {
235 conn = g_variant_get_string(src->data, NULL);
242 sr_err("Failed to initialize libftdi.");
248 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
249 for (i = 0; devlist[i]; i++) {
250 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
251 for (l = conn_devices; l; l = l->next) {
253 if (usb->bus == libusb_get_bus_number(devlist[i])
254 && usb->address == libusb_get_device_address(devlist[i])) {
255 scan_device(ftdic, devlist[i], &devices);
259 libusb_free_device_list(devlist, 1);
261 devices = scan_all(ftdic, options);
265 return std_scan_complete(di, devices);
268 static void clear_helper(struct dev_context *devc)
270 g_free(devc->data_buf);
273 static int dev_clear(const struct sr_dev_driver *di)
275 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
278 static int dev_open(struct sr_dev_inst *sdi)
280 struct dev_context *devc;
285 devc->ftdic = ftdi_new();
289 ret = ftdi_usb_open_string(devc->ftdic, sdi->connection_id);
291 /* Log errors, except for -3 ("device not found"). */
293 sr_err("Failed to open device (%d): %s", ret,
294 ftdi_get_error_string(devc->ftdic));
298 ret = ftdi_usb_purge_buffers(devc->ftdic);
300 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
301 ret, ftdi_get_error_string(devc->ftdic));
302 goto err_dev_open_close_ftdic;
305 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET);
307 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
308 ret, ftdi_get_error_string(devc->ftdic));
309 goto err_dev_open_close_ftdic;
312 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG);
314 sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.",
315 ret, ftdi_get_error_string(devc->ftdic));
316 goto err_dev_open_close_ftdic;
321 err_dev_open_close_ftdic:
322 ftdi_usb_close(devc->ftdic);
325 ftdi_free(devc->ftdic);
330 static int dev_close(struct sr_dev_inst *sdi)
332 struct dev_context *devc;
339 ftdi_usb_close(devc->ftdic);
340 ftdi_free(devc->ftdic);
346 static int config_get(uint32_t key, GVariant **data,
347 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
349 struct dev_context *devc;
350 struct sr_usb_dev_inst *usb;
357 case SR_CONF_SAMPLERATE:
358 *data = g_variant_new_uint64(devc->cur_samplerate);
361 if (!sdi || !sdi->conn)
364 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
373 static int config_set(uint32_t key, GVariant *data,
374 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
376 struct dev_context *devc;
384 case SR_CONF_LIMIT_MSEC:
385 value = g_variant_get_uint64(data);
386 /* TODO: Implement. */
389 case SR_CONF_LIMIT_SAMPLES:
390 devc->limit_samples = g_variant_get_uint64(data);
392 case SR_CONF_SAMPLERATE:
393 value = g_variant_get_uint64(data);
395 return SR_ERR_SAMPLERATE;
396 devc->cur_samplerate = value;
397 return ftdi_la_set_samplerate(devc);
405 static int config_list(uint32_t key, GVariant **data,
406 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
409 case SR_CONF_SCAN_OPTIONS:
410 case SR_CONF_DEVICE_OPTIONS:
411 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
412 case SR_CONF_SAMPLERATE:
413 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
422 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
424 struct dev_context *devc;
431 ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG);
433 /* Properly reset internal variables before every new acquisition. */
434 devc->samples_sent = 0;
435 devc->bytes_received = 0;
437 std_session_send_df_header(sdi);
439 /* Hook up a dummy handler to receive data from the device. */
440 sr_session_source_add(sdi->session, -1, G_IO_IN, 0,
441 ftdi_la_receive_data, (void *)sdi);
446 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
448 sr_session_source_remove(sdi->session, -1);
450 std_session_send_df_end(sdi);
455 static struct sr_dev_driver ftdi_la_driver_info = {
457 .longname = "FTDI LA",
460 .cleanup = std_cleanup,
462 .dev_list = std_dev_list,
463 .dev_clear = dev_clear,
464 .config_get = config_get,
465 .config_set = config_set,
466 .config_list = config_list,
467 .dev_open = dev_open,
468 .dev_close = dev_close,
469 .dev_acquisition_start = dev_acquisition_start,
470 .dev_acquisition_stop = dev_acquisition_stop,
473 SR_REGISTER_DEV_DRIVER(ftdi_la_driver_info);