]>
Commit | Line | Data |
---|---|---|
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 | err_free_data_buf: | |
176 | g_free(devc->data_buf); | |
177 | g_free(devc); | |
178 | } | |
179 | ||
180 | static GSList *scan_all(struct sr_dev_driver *di, GSList *options) | |
181 | { | |
182 | GSList *devices; | |
183 | struct ftdi_device_list *devlist = 0; | |
184 | struct ftdi_device_list *curdev; | |
185 | struct ftdi_context *ftdic; | |
186 | int ret; | |
187 | ||
188 | (void)options; | |
189 | ||
190 | devices = NULL; | |
191 | ||
192 | /* Allocate memory for the FTDI context (ftdic) and initialize it. */ | |
193 | ftdic = ftdi_new(); | |
194 | if (!ftdic) { | |
195 | sr_err("Failed to initialize libftdi."); | |
196 | return NULL; | |
197 | } | |
198 | ||
199 | ret = ftdi_usb_find_all(ftdic, &devlist, 0, 0); | |
200 | if (ret < 0) { | |
201 | sr_err("Failed to list devices (%d): %s", ret, | |
202 | ftdi_get_error_string(ftdic)); | |
203 | goto err_free_ftdic; | |
204 | } | |
205 | ||
206 | sr_dbg("Number of FTDI devices found: %d", ret); | |
207 | ||
208 | curdev = devlist; | |
209 | while (curdev) { | |
210 | scan_device(di, curdev->dev, &devices); | |
211 | curdev = curdev->next; | |
212 | } | |
213 | ||
214 | return devices; | |
215 | ||
216 | err_free_ftdic: | |
217 | ftdi_free(ftdic); /* NOT free() or g_free()! */ | |
218 | ||
219 | return NULL; | |
220 | } | |
221 | ||
222 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
223 | { | |
224 | struct sr_config *src; | |
225 | struct sr_usb_dev_inst *usb; | |
226 | const char *conn; | |
227 | GSList *l, *conn_devices; | |
228 | GSList *devices; | |
229 | struct drv_context *drvc; | |
230 | libusb_device **devlist; | |
231 | int i; | |
232 | ||
233 | drvc = di->context; | |
234 | drvc->instances = NULL; | |
235 | conn = NULL; | |
236 | for (l = options; l; l = l->next) { | |
237 | src = l->data; | |
238 | if (src->key == SR_CONF_CONN) { | |
239 | conn = g_variant_get_string(src->data, NULL); | |
240 | break; | |
241 | } | |
242 | } | |
243 | if (conn) { | |
244 | devices = NULL; | |
245 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
246 | for (i = 0; devlist[i]; i++) { | |
247 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
248 | for (l = conn_devices; l; l = l->next) { | |
249 | usb = l->data; | |
250 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
251 | && usb->address == libusb_get_device_address(devlist[i])) { | |
252 | scan_device(di, devlist[i], &devices); | |
253 | } | |
254 | } | |
255 | } | |
256 | libusb_free_device_list(devlist, 1); | |
257 | return devices; | |
258 | } else | |
259 | return scan_all(di, options); | |
260 | } | |
261 | ||
262 | static GSList *dev_list(const struct sr_dev_driver *di) | |
263 | { | |
264 | return ((struct drv_context *)(di->context))->instances; | |
265 | } | |
266 | ||
267 | static void clear_helper(void *priv) | |
268 | { | |
269 | struct dev_context *devc; | |
270 | ||
271 | devc = priv; | |
272 | ||
273 | ftdi_free(devc->ftdic); | |
274 | g_free(devc->data_buf); | |
275 | g_free(devc); | |
276 | } | |
277 | ||
278 | static int dev_clear(const struct sr_dev_driver *di) | |
279 | { | |
280 | return std_dev_clear(di, clear_helper); | |
281 | } | |
282 | ||
283 | static int dev_open(struct sr_dev_inst *sdi) | |
284 | { | |
285 | struct dev_context *devc; | |
286 | int ret = SR_OK; | |
287 | ||
288 | devc = sdi->priv; | |
289 | ||
290 | ret = ftdi_usb_open_dev(devc->ftdic, devc->usbdev); | |
291 | if (ret < 0) { | |
292 | /* Log errors, except for -3 ("device not found"). */ | |
293 | if (ret != -3) | |
294 | sr_err("Failed to open device (%d): %s", ret, | |
295 | ftdi_get_error_string(devc->ftdic)); | |
296 | return SR_ERR; | |
297 | } | |
298 | ||
299 | /* Purge RX/TX buffers in the FTDI chip. */ | |
300 | ret = ftdi_usb_purge_buffers(devc->ftdic); | |
301 | if (ret < 0) { | |
302 | sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.", | |
303 | ret, ftdi_get_error_string(devc->ftdic)); | |
304 | goto err_dev_open_close_ftdic; | |
305 | } | |
306 | sr_dbg("FTDI chip buffers purged successfully."); | |
307 | ||
308 | /* Reset the FTDI bitmode. */ | |
309 | ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET); | |
310 | if (ret < 0) { | |
311 | sr_err("Failed to reset the FTDI chip bitmode (%d): %s.", | |
312 | ret, ftdi_get_error_string(devc->ftdic)); | |
313 | goto err_dev_open_close_ftdic; | |
314 | } | |
315 | sr_dbg("FTDI chip bitmode reset successfully."); | |
316 | ||
317 | ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG); | |
318 | if (ret < 0) { | |
319 | sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.", | |
320 | ret, ftdi_get_error_string(devc->ftdic)); | |
321 | goto err_dev_open_close_ftdic; | |
322 | } | |
323 | sr_dbg("FTDI chip bitbang mode entered successfully."); | |
324 | ||
325 | sdi->status = SR_ST_ACTIVE; | |
326 | ||
327 | return SR_OK; | |
328 | ||
329 | err_dev_open_close_ftdic: | |
330 | ftdi_usb_close(devc->ftdic); | |
331 | return SR_ERR; | |
332 | } | |
333 | ||
334 | static int dev_close(struct sr_dev_inst *sdi) | |
335 | { | |
336 | struct dev_context *devc; | |
337 | ||
338 | devc = sdi->priv; | |
339 | ||
340 | ftdi_usb_close(devc->ftdic); | |
341 | ||
342 | sdi->status = SR_ST_INACTIVE; | |
343 | ||
344 | return SR_OK; | |
345 | } | |
346 | ||
347 | static int cleanup(const struct sr_dev_driver *di) | |
348 | { | |
349 | dev_clear(di); | |
350 | ||
351 | /* TODO: Free other driver resources, if any. */ | |
352 | ||
353 | return SR_OK; | |
354 | } | |
355 | ||
356 | static int config_get(uint32_t key, GVariant **data, | |
357 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
358 | { | |
359 | int ret; | |
360 | struct dev_context *devc; | |
361 | struct sr_usb_dev_inst *usb; | |
362 | char str[128]; | |
363 | ||
364 | (void)cg; | |
365 | ||
366 | devc = sdi->priv; | |
367 | ||
368 | ret = SR_OK; | |
369 | switch (key) { | |
370 | case SR_CONF_SAMPLERATE: | |
371 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
372 | break; | |
373 | case SR_CONF_CONN: | |
374 | if (!sdi || !sdi->conn) | |
375 | return SR_ERR_ARG; | |
376 | usb = sdi->conn; | |
377 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
378 | *data = g_variant_new_string(str); | |
379 | break; | |
380 | default: | |
381 | return SR_ERR_NA; | |
382 | } | |
383 | ||
384 | return ret; | |
385 | } | |
386 | ||
387 | static int config_set(uint32_t key, GVariant *data, | |
388 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
389 | { | |
390 | int ret; | |
391 | struct dev_context *devc; | |
392 | uint64_t value; | |
393 | ||
394 | (void)cg; | |
395 | ||
396 | if (sdi->status != SR_ST_ACTIVE) | |
397 | return SR_ERR_DEV_CLOSED; | |
398 | ||
399 | devc = sdi->priv; | |
400 | ||
401 | ret = SR_OK; | |
402 | switch (key) { | |
403 | case SR_CONF_LIMIT_MSEC: | |
404 | value = g_variant_get_uint64(data); | |
405 | /* TODO: Implement. */ | |
406 | ret = SR_ERR_NA; | |
407 | break; | |
408 | case SR_CONF_LIMIT_SAMPLES: | |
409 | if (g_variant_get_uint64(data) == 0) | |
410 | return SR_ERR_ARG; | |
411 | devc->limit_samples = g_variant_get_uint64(data); | |
412 | break; | |
413 | case SR_CONF_SAMPLERATE: | |
414 | value = g_variant_get_uint64(data); | |
415 | if (value < 3600) | |
416 | return SR_ERR_SAMPLERATE; | |
417 | devc->cur_samplerate = value; | |
418 | return ftdi_la_set_samplerate(devc); | |
419 | default: | |
420 | ret = SR_ERR_NA; | |
421 | } | |
422 | ||
423 | return ret; | |
424 | } | |
425 | ||
426 | static int config_list(uint32_t key, GVariant **data, | |
427 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
428 | { | |
429 | int ret; | |
430 | GVariant *gvar; | |
431 | GVariantBuilder gvb; | |
432 | ||
433 | (void)sdi; | |
434 | (void)cg; | |
435 | ||
436 | ret = SR_OK; | |
437 | switch (key) { | |
438 | case SR_CONF_SCAN_OPTIONS: | |
439 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
440 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
441 | break; | |
442 | case SR_CONF_DEVICE_OPTIONS: | |
443 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
444 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
445 | break; | |
446 | case SR_CONF_SAMPLERATE: | |
447 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
448 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
449 | samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
450 | g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar); | |
451 | *data = g_variant_builder_end(&gvb); | |
452 | break; | |
453 | default: | |
454 | return SR_ERR_NA; | |
455 | } | |
456 | ||
457 | return ret; | |
458 | } | |
459 | ||
460 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
461 | { | |
462 | struct dev_context *devc; | |
463 | ||
464 | devc = sdi->priv; | |
465 | ||
466 | if (sdi->status != SR_ST_ACTIVE) | |
467 | return SR_ERR_DEV_CLOSED; | |
468 | ||
469 | if (!devc->ftdic) | |
470 | return SR_ERR_BUG; | |
471 | ||
472 | ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG); | |
473 | ||
474 | /* Properly reset internal variables before every new acquisition. */ | |
475 | devc->samples_sent = 0; | |
476 | devc->bytes_received = 0; | |
477 | ||
478 | std_session_send_df_header(sdi, LOG_PREFIX); | |
479 | ||
480 | /* Hook up a dummy handler to receive data from the device. */ | |
481 | sr_session_source_add(sdi->session, -1, G_IO_IN, 0, | |
482 | ftdi_la_receive_data, (void *)sdi); | |
483 | ||
484 | return SR_OK; | |
485 | } | |
486 | ||
487 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
488 | { | |
489 | if (sdi->status != SR_ST_ACTIVE) | |
490 | return SR_ERR_DEV_CLOSED; | |
491 | ||
492 | sr_dbg("Stopping acquisition."); | |
493 | sr_session_source_remove(sdi->session, -1); | |
494 | ||
495 | std_session_send_df_end(sdi, LOG_PREFIX); | |
496 | ||
497 | return SR_OK; | |
498 | } | |
499 | ||
500 | SR_PRIV struct sr_dev_driver ftdi_la_driver_info = { | |
501 | .name = "ftdi-la", | |
502 | .longname = "FTDI LA", | |
503 | .api_version = 1, | |
504 | .init = init, | |
505 | .cleanup = cleanup, | |
506 | .scan = scan, | |
507 | .dev_list = dev_list, | |
508 | .dev_clear = dev_clear, | |
509 | .config_get = config_get, | |
510 | .config_set = config_set, | |
511 | .config_list = config_list, | |
512 | .dev_open = dev_open, | |
513 | .dev_close = dev_close, | |
514 | .dev_acquisition_start = dev_acquisition_start, | |
515 | .dev_acquisition_stop = dev_acquisition_stop, | |
516 | .context = NULL, | |
517 | }; |