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