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