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/>.
24 SR_PRIV struct sr_dev_driver ftdi_la_driver_info;
26 static const uint32_t scanopts[] = {
27 /* TODO: SR_CONF_CONN to be able to specify the USB address. */
30 static const uint32_t devopts[] = {
31 SR_CONF_LOGIC_ANALYZER,
32 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
33 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
36 static const uint64_t samplerates[] = {
42 static const struct ftdi_chip_desc ft2232h_desc = {
55 /* TODO: BDBUS[0..7] channels. */
60 static const struct ftdi_chip_desc ft232r_desc = {
77 static const struct ftdi_chip_desc *chip_descs[] = {
82 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
84 return std_init(sr_ctx, di, LOG_PREFIX);
87 static GSList *scan(struct sr_dev_driver *di, GSList *options)
89 struct drv_context *drvc;
90 struct dev_context *devc;
91 struct sr_dev_inst *sdi;
93 struct ftdi_device_list *devlist = 0;
94 struct ftdi_device_list *curdev;
95 struct libusb_device_descriptor usb_desc;
96 const struct ftdi_chip_desc *desc;
97 char *vendor, *model, *serial_num;
104 drvc->instances = NULL;
106 /* Allocate memory for our private device context. */
107 devc = g_malloc0(sizeof(struct dev_context));
109 /* Allocate memory for the incoming data. */
110 devc->data_buf = g_malloc0(DATA_BUF_SIZE);
112 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
113 devc->ftdic = ftdi_new();
115 sr_err("Failed to initialize libftdi.");
116 goto err_free_data_buf;
119 ret = ftdi_usb_find_all(devc->ftdic, &devlist, 0, 0);
121 sr_err("Failed to list devices (%d): %s", ret,
122 ftdi_get_error_string(devc->ftdic));
126 sr_dbg("Number of FTDI devices found: %d", ret);
130 libusb_get_device_descriptor(curdev->dev, &usb_desc);
133 for (unsigned long i = 0; i < ARRAY_SIZE(chip_descs); i++) {
134 desc = chip_descs[i];
135 if (desc->vendor == usb_desc.idVendor &&
136 desc->product == usb_desc.idProduct)
141 sr_spew("Unsupported FTDI device 0x%4x:0x%4x.",
142 usb_desc.idVendor, usb_desc.idProduct);
145 devc->usbdev = curdev->dev;
148 vendor = g_malloc(32);
149 model = g_malloc(32);
150 serial_num = g_malloc(32);
151 ftdi_usb_get_strings(devc->ftdic, curdev->dev, vendor, 32,
152 model, 32, serial_num, 32);
153 sr_dbg("Found an FTDI device: %s.", model);
155 /* Register the device with libsigrok. */
156 sdi = g_malloc0(sizeof(struct sr_dev_inst));
157 sdi->status = SR_ST_INITIALIZING;
158 sdi->vendor = vendor;
160 sdi->serial_num = serial_num;
164 for (char *const *chan = &(desc->channel_names[0]); *chan; chan++)
165 sr_channel_new(sdi, &(desc->channel_names[0]) - chan,
166 SR_CHANNEL_LOGIC, TRUE, *chan);
168 devices = g_slist_append(devices, sdi);
169 drvc->instances = g_slist_append(drvc->instances, sdi);
171 curdev = curdev->next;
177 ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
179 g_free(devc->data_buf);
185 static GSList *dev_list(const struct sr_dev_driver *di)
187 return ((struct drv_context *)(di->context))->instances;
190 static void clear_helper(void *priv)
192 struct dev_context *devc;
196 ftdi_free(devc->ftdic);
197 g_free(devc->data_buf);
201 static int dev_clear(const struct sr_dev_driver *di)
203 return std_dev_clear(di, clear_helper);
206 static int dev_open(struct sr_dev_inst *sdi)
208 struct dev_context *devc;
213 ret = ftdi_usb_open_dev(devc->ftdic, devc->usbdev);
215 /* Log errors, except for -3 ("device not found"). */
217 sr_err("Failed to open device (%d): %s", ret,
218 ftdi_get_error_string(devc->ftdic));
222 /* Purge RX/TX buffers in the FTDI chip. */
223 ret = ftdi_usb_purge_buffers(devc->ftdic);
225 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
226 ret, ftdi_get_error_string(devc->ftdic));
227 goto err_dev_open_close_ftdic;
229 sr_dbg("FTDI chip buffers purged successfully.");
231 /* Reset the FTDI bitmode. */
232 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET);
234 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
235 ret, ftdi_get_error_string(devc->ftdic));
236 goto err_dev_open_close_ftdic;
238 sr_dbg("FTDI chip bitmode reset successfully.");
240 ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG);
242 sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.",
243 ret, ftdi_get_error_string(devc->ftdic));
244 goto err_dev_open_close_ftdic;
246 sr_dbg("FTDI chip bitbang mode entered successfully.");
248 sdi->status = SR_ST_ACTIVE;
252 err_dev_open_close_ftdic:
253 ftdi_usb_close(devc->ftdic);
257 static int dev_close(struct sr_dev_inst *sdi)
259 struct dev_context *devc;
263 ftdi_usb_close(devc->ftdic);
265 sdi->status = SR_ST_INACTIVE;
270 static int cleanup(const struct sr_dev_driver *di)
274 /* TODO: Free other driver resources, if any. */
279 static int config_get(uint32_t key, GVariant **data,
280 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
283 struct dev_context *devc;
291 case SR_CONF_SAMPLERATE:
292 *data = g_variant_new_uint64(devc->cur_samplerate);
301 static int config_set(uint32_t key, GVariant *data,
302 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
305 struct dev_context *devc;
310 if (sdi->status != SR_ST_ACTIVE)
311 return SR_ERR_DEV_CLOSED;
317 case SR_CONF_LIMIT_MSEC:
318 value = g_variant_get_uint64(data);
319 /* TODO: Implement. */
322 case SR_CONF_LIMIT_SAMPLES:
323 if (g_variant_get_uint64(data) == 0)
325 devc->limit_samples = g_variant_get_uint64(data);
327 case SR_CONF_SAMPLERATE:
328 value = g_variant_get_uint64(data);
330 return SR_ERR_SAMPLERATE;
331 devc->cur_samplerate = value;
332 return ftdi_la_set_samplerate(devc);
340 static int config_list(uint32_t key, GVariant **data,
341 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
352 case SR_CONF_SCAN_OPTIONS:
353 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
354 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
356 case SR_CONF_DEVICE_OPTIONS:
357 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
358 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
360 case SR_CONF_SAMPLERATE:
361 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
362 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
363 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
364 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
365 *data = g_variant_builder_end(&gvb);
374 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
376 struct dev_context *devc;
382 if (sdi->status != SR_ST_ACTIVE)
383 return SR_ERR_DEV_CLOSED;
388 ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG);
390 devc->cb_data = cb_data;
392 /* Properly reset internal variables before every new acquisition. */
393 devc->samples_sent = 0;
394 devc->bytes_received = 0;
396 /* Send header packet to the session bus. */
397 std_session_send_df_header(sdi, LOG_PREFIX);
399 /* Hook up a dummy handler to receive data from the device. */
400 sr_session_source_add(sdi->session, -1, G_IO_IN, 0,
401 ftdi_la_receive_data, (void *)sdi);
406 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
408 struct sr_datafeed_packet packet;
412 if (sdi->status != SR_ST_ACTIVE)
413 return SR_ERR_DEV_CLOSED;
415 sr_dbg("Stopping acquisition.");
416 sr_session_source_remove(sdi->session, -1);
418 /* Send end packet to the session bus. */
419 sr_dbg("Sending SR_DF_END.");
420 packet.type = SR_DF_END;
421 sr_session_send(sdi, &packet);
426 SR_PRIV struct sr_dev_driver ftdi_la_driver_info = {
428 .longname = "FTDI LA",
433 .dev_list = dev_list,
434 .dev_clear = dev_clear,
435 .config_get = config_get,
436 .config_set = config_set,
437 .config_list = config_list,
438 .dev_open = dev_open,
439 .dev_close = dev_close,
440 .dev_acquisition_start = dev_acquisition_start,
441 .dev_acquisition_stop = dev_acquisition_stop,