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