]> sigrok.org Git - libsigrok.git/blob - src/hardware/ftdi-la/api.c
Don't reset instance list in scan() callback
[libsigrok.git] / src / hardware / ftdi-la / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2015 Sergey Alirzaev <zl29ah@gmail.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21 #include <ftdi.h>
22 #include <libusb.h>
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
25 #include "protocol.h"
26
27 static const uint32_t scanopts[] = {
28         SR_CONF_CONN,
29 };
30
31 static const uint32_t devopts[] = {
32         SR_CONF_LOGIC_ANALYZER,
33         SR_CONF_CONTINUOUS,
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,
37 };
38
39 static const uint64_t samplerates[] = {
40         SR_HZ(3600),
41         SR_MHZ(10),
42         SR_HZ(1),
43 };
44
45 static const struct ftdi_chip_desc ft2232h_desc = {
46         .vendor = 0x0403,
47         .product = 0x6010,
48         .samplerate_div = 20,
49         .channel_names = {
50                 "ADBUS0",
51                 "ADBUS1",
52                 "ADBUS2",
53                 "ADBUS3",
54                 "ADBUS4",
55                 "ADBUS5",
56                 "ADBUS6",
57                 "ADBUS7",
58                 /* TODO: BDBUS[0..7] channels. */
59                 NULL
60         }
61 };
62
63 static const struct ftdi_chip_desc ft232r_desc = {
64         .vendor = 0x0403,
65         .product = 0x6001,
66         .samplerate_div = 30,
67         .channel_names = {
68                 "TXD",
69                 "RXD",
70                 "RTS#",
71                 "CTS#",
72                 "DTR#",
73                 "DSR#",
74                 "DCD#",
75                 "RI#",
76                 NULL
77         }
78 };
79
80 static const struct ftdi_chip_desc *chip_descs[] = {
81         &ft2232h_desc,
82         &ft232r_desc,
83 };
84
85 static void scan_device(struct sr_dev_driver *di, struct ftdi_context *ftdic,
86         struct libusb_device *dev, GSList **devices)
87 {
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;
93         struct drv_context *drvc;
94         int rv;
95
96         drvc = di->context;
97         libusb_get_device_descriptor(dev, &usb_desc);
98
99         desc = NULL;
100         for (unsigned long i = 0; i < ARRAY_SIZE(chip_descs); i++) {
101                 desc = chip_descs[i];
102                 if (desc->vendor == usb_desc.idVendor &&
103                         desc->product == usb_desc.idProduct)
104                         break;
105         }
106
107         if (!desc) {
108                 sr_spew("Unsupported FTDI device 0x%4x:0x%4x.",
109                         usb_desc.idVendor, usb_desc.idProduct);
110                 return;
111         }
112
113         /* Allocate memory for our private device context. */
114         devc = g_malloc0(sizeof(struct dev_context));
115
116         /* Allocate memory for the incoming data. */
117         devc->data_buf = g_malloc0(DATA_BUF_SIZE);
118
119         devc->desc = desc;
120
121         vendor = g_malloc(32);
122         model = g_malloc(32);
123         serial_num = g_malloc(32);
124         rv = ftdi_usb_get_strings(ftdic, dev, vendor, 32,
125                              model, 32, serial_num, 32);
126         switch (rv) {
127         case 0:
128                 break;
129         case -9:
130                 sr_dbg("The device lacks a serial number.");
131                 g_free(serial_num);
132                 serial_num = NULL;
133                 break;
134         default:
135                 sr_err("Failed to get the FTDI strings: %d", rv);
136                 goto err_free_strings;
137         }
138         sr_dbg("Found an FTDI device: %s.", model);
139
140         /* Register the device with libsigrok. */
141         sdi = g_malloc0(sizeof(struct sr_dev_inst));
142         sdi->status = SR_ST_INACTIVE;
143         sdi->vendor = vendor;
144         sdi->model = model;
145         sdi->serial_num = serial_num;
146         sdi->driver = di;
147         sdi->priv = devc;
148         sdi->connection_id = g_strdup_printf("d:%u/%u",
149                 libusb_get_bus_number(dev), libusb_get_device_address(dev));
150
151         for (char *const *chan = &(desc->channel_names[0]); *chan; chan++)
152                 sr_channel_new(sdi, chan - &(desc->channel_names[0]),
153                                 SR_CHANNEL_LOGIC, TRUE, *chan);
154
155         *devices = g_slist_append(*devices, sdi);
156         drvc->instances = g_slist_append(drvc->instances, sdi);
157         return;
158
159 err_free_strings:
160         g_free(vendor);
161         g_free(model);
162         g_free(serial_num);
163         g_free(devc->data_buf);
164         g_free(devc);
165 }
166
167 static GSList *scan_all(struct ftdi_context *ftdic, struct sr_dev_driver *di,
168         GSList *options)
169 {
170         GSList *devices;
171         struct ftdi_device_list *devlist = 0;
172         struct ftdi_device_list *curdev;
173         int ret;
174
175         (void)options;
176
177         devices = NULL;
178
179         ret = ftdi_usb_find_all(ftdic, &devlist, 0, 0);
180         if (ret < 0) {
181                 sr_err("Failed to list devices (%d): %s", ret,
182                        ftdi_get_error_string(ftdic));
183                 return NULL;
184         }
185
186         sr_dbg("Number of FTDI devices found: %d", ret);
187
188         curdev = devlist;
189         while (curdev) {
190                 scan_device(di, ftdic, curdev->dev, &devices);
191                 curdev = curdev->next;
192         }
193
194         ftdi_list_free(&devlist);
195
196         return devices;
197 }
198
199 static GSList *scan(struct sr_dev_driver *di, GSList *options)
200 {
201         struct ftdi_context *ftdic;
202         struct sr_config *src;
203         struct sr_usb_dev_inst *usb;
204         const char *conn;
205         GSList *l, *conn_devices;
206         GSList *devices;
207         struct drv_context *drvc;
208         libusb_device **devlist;
209         int i;
210
211         drvc = di->context;
212         conn = NULL;
213         for (l = options; l; l = l->next) {
214                 src = l->data;
215                 if (src->key == SR_CONF_CONN) {
216                         conn = g_variant_get_string(src->data, NULL);
217                         break;
218                 }
219         }
220
221         /* Allocate memory for the FTDI context (ftdic) and initialize it. */
222         ftdic = ftdi_new();
223         if (!ftdic) {
224                 sr_err("Failed to initialize libftdi.");
225                 return NULL;
226         }
227
228         if (conn) {
229                 devices = NULL;
230                 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
231                 for (i = 0; devlist[i]; i++) {
232                         conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
233                         for (l = conn_devices; l; l = l->next) {
234                                 usb = l->data;
235                                 if (usb->bus == libusb_get_bus_number(devlist[i])
236                                         && usb->address == libusb_get_device_address(devlist[i])) {
237                                         scan_device(di, ftdic, devlist[i], &devices);
238                                 }
239                         }
240                 }
241                 libusb_free_device_list(devlist, 1);
242         } else
243                 devices = scan_all(ftdic, di, options);
244
245         ftdi_free(ftdic);
246
247         return devices;
248 }
249
250 static void clear_helper(void *priv)
251 {
252         struct dev_context *devc;
253
254         devc = priv;
255         g_free(devc->data_buf);
256         g_free(devc);
257 }
258
259 static int dev_clear(const struct sr_dev_driver *di)
260 {
261         return std_dev_clear(di, clear_helper);
262 }
263
264 static int dev_open(struct sr_dev_inst *sdi)
265 {
266         struct dev_context *devc;
267         int ret = SR_OK;
268
269         devc = sdi->priv;
270
271         devc->ftdic = ftdi_new();
272         if (!devc->ftdic)
273                 return SR_ERR;
274
275         ret = ftdi_usb_open_string(devc->ftdic, sdi->connection_id);
276         if (ret < 0) {
277                 /* Log errors, except for -3 ("device not found"). */
278                 if (ret != -3)
279                         sr_err("Failed to open device (%d): %s", ret,
280                                ftdi_get_error_string(devc->ftdic));
281                 goto err_ftdi_free;
282         }
283
284         /* Purge RX/TX buffers in the FTDI chip. */
285         ret = ftdi_usb_purge_buffers(devc->ftdic);
286         if (ret < 0) {
287                 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
288                        ret, ftdi_get_error_string(devc->ftdic));
289                 goto err_dev_open_close_ftdic;
290         }
291         sr_dbg("FTDI chip buffers purged successfully.");
292
293         /* Reset the FTDI bitmode. */
294         ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET);
295         if (ret < 0) {
296                 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
297                        ret, ftdi_get_error_string(devc->ftdic));
298                 goto err_dev_open_close_ftdic;
299         }
300         sr_dbg("FTDI chip bitmode reset successfully.");
301
302         ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG);
303         if (ret < 0) {
304                 sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.",
305                        ret, ftdi_get_error_string(devc->ftdic));
306                 goto err_dev_open_close_ftdic;
307         }
308         sr_dbg("FTDI chip bitbang mode entered successfully.");
309
310         sdi->status = SR_ST_ACTIVE;
311
312         return SR_OK;
313 err_dev_open_close_ftdic:
314         ftdi_usb_close(devc->ftdic);
315 err_ftdi_free:
316         ftdi_free(devc->ftdic);
317         return SR_ERR;
318 }
319
320 static int dev_close(struct sr_dev_inst *sdi)
321 {
322         struct dev_context *devc;
323
324         devc = sdi->priv;
325
326         if (devc->ftdic) {
327                 ftdi_usb_close(devc->ftdic);
328                 ftdi_free(devc->ftdic);
329                 devc->ftdic = NULL;
330         }
331
332         sdi->status = SR_ST_INACTIVE;
333
334         return SR_OK;
335 }
336
337 static int config_get(uint32_t key, GVariant **data,
338         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
339 {
340         int ret;
341         struct dev_context *devc;
342         struct sr_usb_dev_inst *usb;
343         char str[128];
344
345         (void)cg;
346
347         devc = sdi->priv;
348
349         ret = SR_OK;
350         switch (key) {
351         case SR_CONF_SAMPLERATE:
352                 *data = g_variant_new_uint64(devc->cur_samplerate);
353                 break;
354         case SR_CONF_CONN:
355                 if (!sdi || !sdi->conn)
356                         return SR_ERR_ARG;
357                 usb = sdi->conn;
358                 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
359                 *data = g_variant_new_string(str);
360                 break;
361         default:
362                 return SR_ERR_NA;
363         }
364
365         return ret;
366 }
367
368 static int config_set(uint32_t key, GVariant *data,
369         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
370 {
371         int ret;
372         struct dev_context *devc;
373         uint64_t value;
374
375         (void)cg;
376
377         if (sdi->status != SR_ST_ACTIVE)
378                 return SR_ERR_DEV_CLOSED;
379
380         devc = sdi->priv;
381
382         ret = SR_OK;
383         switch (key) {
384         case SR_CONF_LIMIT_MSEC:
385                 value = g_variant_get_uint64(data);
386                 /* TODO: Implement. */
387                 ret = SR_ERR_NA;
388                 break;
389         case SR_CONF_LIMIT_SAMPLES:
390                 if (g_variant_get_uint64(data) == 0)
391                         return SR_ERR_ARG;
392                 devc->limit_samples = g_variant_get_uint64(data);
393                 break;
394         case SR_CONF_SAMPLERATE:
395                 value = g_variant_get_uint64(data);
396                 if (value < 3600)
397                         return SR_ERR_SAMPLERATE;
398                 devc->cur_samplerate = value;
399                 return ftdi_la_set_samplerate(devc);
400         default:
401                 ret = SR_ERR_NA;
402         }
403
404         return ret;
405 }
406
407 static int config_list(uint32_t key, GVariant **data,
408         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
409 {
410         int ret;
411         GVariant *gvar;
412         GVariantBuilder gvb;
413
414         (void)sdi;
415         (void)cg;
416
417         ret = SR_OK;
418         switch (key) {
419         case SR_CONF_SCAN_OPTIONS:
420                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
421                         scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
422                 break;
423         case SR_CONF_DEVICE_OPTIONS:
424                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
425                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
426                 break;
427         case SR_CONF_SAMPLERATE:
428                 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
429                 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
430                         samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
431                 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
432                 *data = g_variant_builder_end(&gvb);
433                 break;
434         default:
435                 return SR_ERR_NA;
436         }
437
438         return ret;
439 }
440
441 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
442 {
443         struct dev_context *devc;
444
445         devc = sdi->priv;
446
447         if (sdi->status != SR_ST_ACTIVE)
448                 return SR_ERR_DEV_CLOSED;
449
450         if (!devc->ftdic)
451                 return SR_ERR_BUG;
452
453         ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG);
454
455         /* Properly reset internal variables before every new acquisition. */
456         devc->samples_sent = 0;
457         devc->bytes_received = 0;
458
459         std_session_send_df_header(sdi, LOG_PREFIX);
460
461         /* Hook up a dummy handler to receive data from the device. */
462         sr_session_source_add(sdi->session, -1, G_IO_IN, 0,
463                               ftdi_la_receive_data, (void *)sdi);
464
465         return SR_OK;
466 }
467
468 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
469 {
470         if (sdi->status != SR_ST_ACTIVE)
471                 return SR_ERR_DEV_CLOSED;
472
473         sr_dbg("Stopping acquisition.");
474         sr_session_source_remove(sdi->session, -1);
475
476         std_session_send_df_end(sdi, LOG_PREFIX);
477
478         return SR_OK;
479 }
480
481 static struct sr_dev_driver ftdi_la_driver_info = {
482         .name = "ftdi-la",
483         .longname = "FTDI LA",
484         .api_version = 1,
485         .init = std_init,
486         .cleanup = std_cleanup,
487         .scan = scan,
488         .dev_list = std_dev_list,
489         .dev_clear = dev_clear,
490         .config_get = config_get,
491         .config_set = config_set,
492         .config_list = config_list,
493         .dev_open = dev_open,
494         .dev_close = dev_close,
495         .dev_acquisition_start = dev_acquisition_start,
496         .dev_acquisition_stop = dev_acquisition_stop,
497         .context = NULL,
498 };
499 SR_REGISTER_DEV_DRIVER(ftdi_la_driver_info);