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