]>
Commit | Line | Data |
---|---|---|
4f7fdcdd SA |
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 | ||
cee6d3fa | 20 | #include <config.h> |
4f7fdcdd SA |
21 | #include <ftdi.h> |
22 | #include <libusb.h> | |
cee6d3fa SA |
23 | #include <libsigrok/libsigrok.h> |
24 | #include "libsigrok-internal.h" | |
4f7fdcdd SA |
25 | #include "protocol.h" |
26 | ||
4f7fdcdd | 27 | static const uint32_t scanopts[] = { |
cee6d3fa | 28 | SR_CONF_CONN, |
4f7fdcdd SA |
29 | }; |
30 | ||
31 | static const uint32_t devopts[] = { | |
32 | SR_CONF_LOGIC_ANALYZER, | |
e91bb0a6 | 33 | SR_CONF_CONTINUOUS, |
4f7fdcdd SA |
34 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, |
35 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
cee6d3fa | 36 | SR_CONF_CONN | SR_CONF_GET, |
4f7fdcdd SA |
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", | |
4f7fdcdd | 69 | "RXD", |
cee6d3fa SA |
70 | "RTS#", |
71 | "CTS#", | |
72 | "DTR#", | |
73 | "DSR#", | |
74 | "DCD#", | |
75 | "RI#", | |
4f7fdcdd SA |
76 | NULL |
77 | } | |
78 | }; | |
79 | ||
80 | static const struct ftdi_chip_desc *chip_descs[] = { | |
81 | &ft2232h_desc, | |
82 | &ft232r_desc, | |
83 | }; | |
84 | ||
07a91752 LPC |
85 | static void scan_device(struct sr_dev_driver *di, struct ftdi_context *ftdic, |
86 | struct libusb_device *dev, GSList **devices) | |
4f7fdcdd | 87 | { |
4f7fdcdd SA |
88 | struct libusb_device_descriptor usb_desc; |
89 | const struct ftdi_chip_desc *desc; | |
cee6d3fa | 90 | struct dev_context *devc; |
4f7fdcdd | 91 | char *vendor, *model, *serial_num; |
cee6d3fa SA |
92 | struct sr_dev_inst *sdi; |
93 | struct drv_context *drvc; | |
94 | int rv; | |
4f7fdcdd | 95 | |
4f7fdcdd | 96 | drvc = di->context; |
cee6d3fa SA |
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 | } | |
4f7fdcdd SA |
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 | ||
07a91752 LPC |
119 | snprintf(devc->address, sizeof(devc->address), "d:%u/%u", |
120 | libusb_get_bus_number(dev), libusb_get_device_address(dev)); | |
4f7fdcdd | 121 | |
cee6d3fa SA |
122 | devc->desc = desc; |
123 | ||
124 | vendor = g_malloc(32); | |
125 | model = g_malloc(32); | |
126 | serial_num = g_malloc(32); | |
07a91752 | 127 | rv = ftdi_usb_get_strings(ftdic, dev, vendor, 32, |
cee6d3fa SA |
128 | model, 32, serial_num, 32); |
129 | switch (rv) { | |
130 | case 0: | |
131 | break; | |
132 | case -9: | |
133 | sr_dbg("The device lacks a serial number."); | |
134 | g_free(serial_num); | |
135 | serial_num = NULL; | |
136 | break; | |
137 | default: | |
138 | sr_err("Failed to get the FTDI strings: %d", rv); | |
139 | goto err_free_strings; | |
140 | } | |
141 | sr_dbg("Found an FTDI device: %s.", model); | |
142 | ||
143 | /* Register the device with libsigrok. */ | |
144 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
45884333 | 145 | sdi->status = SR_ST_INACTIVE; |
cee6d3fa SA |
146 | sdi->vendor = vendor; |
147 | sdi->model = model; | |
148 | sdi->serial_num = serial_num; | |
149 | sdi->driver = di; | |
150 | sdi->priv = devc; | |
151 | ||
152 | for (char *const *chan = &(desc->channel_names[0]); *chan; chan++) | |
153 | sr_channel_new(sdi, chan - &(desc->channel_names[0]), | |
154 | SR_CHANNEL_LOGIC, TRUE, *chan); | |
155 | ||
156 | *devices = g_slist_append(*devices, sdi); | |
157 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
158 | return; | |
159 | ||
160 | err_free_strings: | |
161 | g_free(vendor); | |
162 | g_free(model); | |
163 | g_free(serial_num); | |
cee6d3fa SA |
164 | g_free(devc->data_buf); |
165 | g_free(devc); | |
166 | } | |
167 | ||
07a91752 LPC |
168 | static GSList *scan_all(struct ftdi_context *ftdic, struct sr_dev_driver *di, |
169 | GSList *options) | |
cee6d3fa SA |
170 | { |
171 | GSList *devices; | |
172 | struct ftdi_device_list *devlist = 0; | |
173 | struct ftdi_device_list *curdev; | |
cee6d3fa SA |
174 | int ret; |
175 | ||
176 | (void)options; | |
177 | ||
178 | devices = NULL; | |
179 | ||
cee6d3fa | 180 | ret = ftdi_usb_find_all(ftdic, &devlist, 0, 0); |
4f7fdcdd SA |
181 | if (ret < 0) { |
182 | sr_err("Failed to list devices (%d): %s", ret, | |
cee6d3fa | 183 | ftdi_get_error_string(ftdic)); |
07a91752 | 184 | return NULL; |
4f7fdcdd SA |
185 | } |
186 | ||
187 | sr_dbg("Number of FTDI devices found: %d", ret); | |
188 | ||
189 | curdev = devlist; | |
190 | while (curdev) { | |
07a91752 | 191 | scan_device(di, ftdic, curdev->dev, &devices); |
4f7fdcdd SA |
192 | curdev = curdev->next; |
193 | } | |
194 | ||
7b6072d3 | 195 | ftdi_list_free(&devlist); |
7b6072d3 | 196 | |
4f7fdcdd | 197 | return devices; |
4f7fdcdd SA |
198 | } |
199 | ||
cee6d3fa SA |
200 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
201 | { | |
07a91752 | 202 | struct ftdi_context *ftdic; |
cee6d3fa SA |
203 | struct sr_config *src; |
204 | struct sr_usb_dev_inst *usb; | |
205 | const char *conn; | |
206 | GSList *l, *conn_devices; | |
207 | GSList *devices; | |
208 | struct drv_context *drvc; | |
209 | libusb_device **devlist; | |
210 | int i; | |
211 | ||
212 | drvc = di->context; | |
213 | drvc->instances = NULL; | |
214 | conn = NULL; | |
215 | for (l = options; l; l = l->next) { | |
216 | src = l->data; | |
217 | if (src->key == SR_CONF_CONN) { | |
218 | conn = g_variant_get_string(src->data, NULL); | |
219 | break; | |
220 | } | |
221 | } | |
07a91752 LPC |
222 | |
223 | /* Allocate memory for the FTDI context (ftdic) and initialize it. */ | |
224 | ftdic = ftdi_new(); | |
225 | if (!ftdic) { | |
226 | sr_err("Failed to initialize libftdi."); | |
227 | return NULL; | |
228 | } | |
229 | ||
cee6d3fa SA |
230 | if (conn) { |
231 | devices = NULL; | |
232 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
233 | for (i = 0; devlist[i]; i++) { | |
234 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
235 | for (l = conn_devices; l; l = l->next) { | |
236 | usb = l->data; | |
237 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
238 | && usb->address == libusb_get_device_address(devlist[i])) { | |
07a91752 | 239 | scan_device(di, ftdic, devlist[i], &devices); |
cee6d3fa SA |
240 | } |
241 | } | |
242 | } | |
243 | libusb_free_device_list(devlist, 1); | |
cee6d3fa | 244 | } else |
07a91752 LPC |
245 | devices = scan_all(ftdic, di, options); |
246 | ||
247 | ftdi_free(ftdic); | |
248 | ||
249 | return devices; | |
cee6d3fa SA |
250 | } |
251 | ||
4f7fdcdd SA |
252 | static void clear_helper(void *priv) |
253 | { | |
254 | struct dev_context *devc; | |
255 | ||
256 | devc = priv; | |
4f7fdcdd SA |
257 | g_free(devc->data_buf); |
258 | g_free(devc); | |
259 | } | |
260 | ||
261 | static int dev_clear(const struct sr_dev_driver *di) | |
262 | { | |
263 | return std_dev_clear(di, clear_helper); | |
264 | } | |
265 | ||
266 | static int dev_open(struct sr_dev_inst *sdi) | |
267 | { | |
268 | struct dev_context *devc; | |
269 | int ret = SR_OK; | |
270 | ||
271 | devc = sdi->priv; | |
272 | ||
07a91752 LPC |
273 | devc->ftdic = ftdi_new(); |
274 | if (!devc->ftdic) | |
275 | return SR_ERR; | |
276 | ||
277 | ret = ftdi_usb_open_string(devc->ftdic, devc->address); | |
4f7fdcdd SA |
278 | if (ret < 0) { |
279 | /* Log errors, except for -3 ("device not found"). */ | |
280 | if (ret != -3) | |
281 | sr_err("Failed to open device (%d): %s", ret, | |
282 | ftdi_get_error_string(devc->ftdic)); | |
07a91752 | 283 | goto err_ftdi_free; |
4f7fdcdd SA |
284 | } |
285 | ||
286 | /* Purge RX/TX buffers in the FTDI chip. */ | |
287 | ret = ftdi_usb_purge_buffers(devc->ftdic); | |
288 | if (ret < 0) { | |
289 | sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.", | |
290 | ret, ftdi_get_error_string(devc->ftdic)); | |
291 | goto err_dev_open_close_ftdic; | |
292 | } | |
293 | sr_dbg("FTDI chip buffers purged successfully."); | |
294 | ||
295 | /* Reset the FTDI bitmode. */ | |
296 | ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET); | |
297 | if (ret < 0) { | |
298 | sr_err("Failed to reset the FTDI chip bitmode (%d): %s.", | |
299 | ret, ftdi_get_error_string(devc->ftdic)); | |
300 | goto err_dev_open_close_ftdic; | |
301 | } | |
302 | sr_dbg("FTDI chip bitmode reset successfully."); | |
303 | ||
304 | ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG); | |
305 | if (ret < 0) { | |
306 | sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.", | |
307 | ret, ftdi_get_error_string(devc->ftdic)); | |
308 | goto err_dev_open_close_ftdic; | |
309 | } | |
310 | sr_dbg("FTDI chip bitbang mode entered successfully."); | |
311 | ||
312 | sdi->status = SR_ST_ACTIVE; | |
313 | ||
314 | return SR_OK; | |
4f7fdcdd SA |
315 | err_dev_open_close_ftdic: |
316 | ftdi_usb_close(devc->ftdic); | |
07a91752 LPC |
317 | err_ftdi_free: |
318 | ftdi_free(devc->ftdic); | |
4f7fdcdd SA |
319 | return SR_ERR; |
320 | } | |
321 | ||
322 | static int dev_close(struct sr_dev_inst *sdi) | |
323 | { | |
324 | struct dev_context *devc; | |
325 | ||
326 | devc = sdi->priv; | |
327 | ||
07a91752 LPC |
328 | if (devc->ftdic) { |
329 | ftdi_usb_close(devc->ftdic); | |
330 | ftdi_free(devc->ftdic); | |
331 | devc->ftdic = NULL; | |
332 | } | |
4f7fdcdd SA |
333 | |
334 | sdi->status = SR_ST_INACTIVE; | |
335 | ||
336 | return SR_OK; | |
337 | } | |
338 | ||
4f7fdcdd SA |
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; | |
cee6d3fa SA |
344 | struct sr_usb_dev_inst *usb; |
345 | char str[128]; | |
4f7fdcdd SA |
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; | |
cee6d3fa SA |
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; | |
4f7fdcdd SA |
363 | default: |
364 | return SR_ERR_NA; | |
365 | } | |
366 | ||
367 | return ret; | |
368 | } | |
369 | ||
4f7fdcdd SA |
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; | |
f2273382 | 401 | return ftdi_la_set_samplerate(devc); |
4f7fdcdd SA |
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 | ||
695dc859 | 443 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
4f7fdcdd SA |
444 | { |
445 | struct dev_context *devc; | |
446 | ||
4f7fdcdd SA |
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 | ||
4f7fdcdd SA |
457 | /* Properly reset internal variables before every new acquisition. */ |
458 | devc->samples_sent = 0; | |
459 | devc->bytes_received = 0; | |
460 | ||
4f7fdcdd SA |
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, | |
f2273382 | 465 | ftdi_la_receive_data, (void *)sdi); |
4f7fdcdd SA |
466 | |
467 | return SR_OK; | |
468 | } | |
469 | ||
695dc859 | 470 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
4f7fdcdd | 471 | { |
4f7fdcdd SA |
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 | ||
3be42bc2 | 478 | std_session_send_df_end(sdi, LOG_PREFIX); |
4f7fdcdd SA |
479 | |
480 | return SR_OK; | |
481 | } | |
482 | ||
dd5c48a6 | 483 | static struct sr_dev_driver ftdi_la_driver_info = { |
f2273382 UH |
484 | .name = "ftdi-la", |
485 | .longname = "FTDI LA", | |
4f7fdcdd | 486 | .api_version = 1, |
c2fdcc25 | 487 | .init = std_init, |
700d6b64 | 488 | .cleanup = std_cleanup, |
4f7fdcdd | 489 | .scan = scan, |
c01bf34c | 490 | .dev_list = std_dev_list, |
4f7fdcdd SA |
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 | }; | |
dd5c48a6 | 501 | SR_REGISTER_DEV_DRIVER(ftdi_la_driver_info); |