]>
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 | static const uint32_t scanopts[] = { | |
28 | SR_CONF_CONN, | |
29 | }; | |
30 | ||
31 | static const uint32_t drvopts[] = { | |
32 | SR_CONF_LOGIC_ANALYZER, | |
33 | }; | |
34 | ||
35 | static const uint32_t devopts[] = { | |
36 | SR_CONF_CONTINUOUS, | |
37 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, | |
38 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
39 | SR_CONF_CONN | SR_CONF_GET, | |
40 | }; | |
41 | ||
42 | static const uint64_t samplerates[] = { | |
43 | SR_HZ(3600), | |
44 | SR_MHZ(10), | |
45 | SR_HZ(1), | |
46 | }; | |
47 | ||
48 | static const struct ftdi_chip_desc ft2232h_desc = { | |
49 | .vendor = 0x0403, | |
50 | .product = 0x6010, | |
51 | .samplerate_div = 20, | |
52 | .channel_names = { | |
53 | "ADBUS0", "ADBUS1", "ADBUS2", "ADBUS3", | |
54 | "ADBUS4", "ADBUS5", "ADBUS6", "ADBUS7", | |
55 | /* TODO: BDBUS[0..7] channels. */ | |
56 | NULL | |
57 | } | |
58 | }; | |
59 | ||
60 | static const struct ftdi_chip_desc ft2232h_tumpa_desc = { | |
61 | .vendor = 0x0403, | |
62 | .product = 0x8a98, | |
63 | .samplerate_div = 20, | |
64 | /* 20 pin JTAG header */ | |
65 | .channel_names = { | |
66 | "TCK", "TDI", "TDO", "TMS", "RST", "nTRST", "DBGRQ", "RTCK", | |
67 | NULL | |
68 | } | |
69 | }; | |
70 | ||
71 | static const struct ftdi_chip_desc ft232r_desc = { | |
72 | .vendor = 0x0403, | |
73 | .product = 0x6001, | |
74 | .samplerate_div = 30, | |
75 | .channel_names = { | |
76 | "TXD", "RXD", "RTS#", "CTS#", "DTR#", "DSR#", "DCD#", "RI#", | |
77 | NULL | |
78 | } | |
79 | }; | |
80 | ||
81 | static const struct ftdi_chip_desc ft232h_desc = { | |
82 | .vendor = 0x0403, | |
83 | .product = 0x6014, | |
84 | .samplerate_div = 30, | |
85 | .channel_names = { | |
86 | "ADBUS0", "ADBUS1", "ADBUS2", "ADBUS3", "ADBUS4", "ADBUS5", "ADBUS6", "ADBUS7", | |
87 | NULL | |
88 | } | |
89 | }; | |
90 | ||
91 | static const struct ftdi_chip_desc *chip_descs[] = { | |
92 | &ft2232h_desc, | |
93 | &ft2232h_tumpa_desc, | |
94 | &ft232r_desc, | |
95 | &ft232h_desc, | |
96 | NULL, | |
97 | }; | |
98 | ||
99 | static void scan_device(struct ftdi_context *ftdic, | |
100 | struct libusb_device *dev, GSList **devices) | |
101 | { | |
102 | struct libusb_device_descriptor usb_desc; | |
103 | const struct ftdi_chip_desc *desc; | |
104 | struct dev_context *devc; | |
105 | char *vendor, *model, *serial_num; | |
106 | struct sr_dev_inst *sdi; | |
107 | int rv; | |
108 | ||
109 | libusb_get_device_descriptor(dev, &usb_desc); | |
110 | ||
111 | desc = NULL; | |
112 | for (unsigned long i = 0; i < ARRAY_SIZE(chip_descs); i++) { | |
113 | desc = chip_descs[i]; | |
114 | if (!desc) | |
115 | break; | |
116 | if (desc->vendor == usb_desc.idVendor && | |
117 | desc->product == usb_desc.idProduct) | |
118 | break; | |
119 | } | |
120 | ||
121 | if (!desc) { | |
122 | sr_spew("Unsupported FTDI device 0x%04x:0x%04x.", | |
123 | usb_desc.idVendor, usb_desc.idProduct); | |
124 | return; | |
125 | } | |
126 | ||
127 | devc = g_malloc0(sizeof(struct dev_context)); | |
128 | ||
129 | /* Allocate memory for the incoming data. */ | |
130 | devc->data_buf = g_malloc0(DATA_BUF_SIZE); | |
131 | ||
132 | devc->desc = desc; | |
133 | ||
134 | vendor = g_malloc(32); | |
135 | model = g_malloc(32); | |
136 | serial_num = g_malloc(32); | |
137 | rv = ftdi_usb_get_strings(ftdic, dev, vendor, 32, | |
138 | model, 32, serial_num, 32); | |
139 | switch (rv) { | |
140 | case 0: | |
141 | break; | |
142 | case -9: | |
143 | sr_dbg("The device lacks a serial number."); | |
144 | g_free(serial_num); | |
145 | serial_num = NULL; | |
146 | break; | |
147 | default: | |
148 | sr_err("Failed to get the FTDI strings: %d", rv); | |
149 | goto err_free_strings; | |
150 | } | |
151 | sr_dbg("Found an FTDI device: %s.", model); | |
152 | ||
153 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
154 | sdi->status = SR_ST_INACTIVE; | |
155 | sdi->vendor = vendor; | |
156 | sdi->model = model; | |
157 | sdi->serial_num = serial_num; | |
158 | sdi->priv = devc; | |
159 | sdi->connection_id = g_strdup_printf("d:%u/%u", | |
160 | libusb_get_bus_number(dev), libusb_get_device_address(dev)); | |
161 | ||
162 | for (char *const *chan = &(desc->channel_names[0]); *chan; chan++) | |
163 | sr_channel_new(sdi, chan - &(desc->channel_names[0]), | |
164 | SR_CHANNEL_LOGIC, TRUE, *chan); | |
165 | ||
166 | *devices = g_slist_append(*devices, sdi); | |
167 | return; | |
168 | ||
169 | err_free_strings: | |
170 | g_free(vendor); | |
171 | g_free(model); | |
172 | g_free(serial_num); | |
173 | g_free(devc->data_buf); | |
174 | g_free(devc); | |
175 | } | |
176 | ||
177 | static GSList *scan_all(struct ftdi_context *ftdic, GSList *options) | |
178 | { | |
179 | GSList *devices; | |
180 | struct ftdi_device_list *devlist = 0; | |
181 | struct ftdi_device_list *curdev; | |
182 | int ret; | |
183 | ||
184 | (void)options; | |
185 | ||
186 | devices = NULL; | |
187 | ||
188 | ret = ftdi_usb_find_all(ftdic, &devlist, 0, 0); | |
189 | if (ret < 0) { | |
190 | sr_err("Failed to list devices (%d): %s", ret, | |
191 | ftdi_get_error_string(ftdic)); | |
192 | return NULL; | |
193 | } | |
194 | ||
195 | curdev = devlist; | |
196 | while (curdev) { | |
197 | scan_device(ftdic, curdev->dev, &devices); | |
198 | curdev = curdev->next; | |
199 | } | |
200 | ||
201 | ftdi_list_free(&devlist); | |
202 | ||
203 | return devices; | |
204 | } | |
205 | ||
206 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
207 | { | |
208 | struct ftdi_context *ftdic; | |
209 | struct sr_config *src; | |
210 | struct sr_usb_dev_inst *usb; | |
211 | const char *conn; | |
212 | GSList *l, *conn_devices; | |
213 | GSList *devices; | |
214 | struct drv_context *drvc; | |
215 | libusb_device **devlist; | |
216 | int i; | |
217 | ||
218 | drvc = di->context; | |
219 | conn = NULL; | |
220 | for (l = options; l; l = l->next) { | |
221 | src = l->data; | |
222 | if (src->key == SR_CONF_CONN) { | |
223 | conn = g_variant_get_string(src->data, NULL); | |
224 | break; | |
225 | } | |
226 | } | |
227 | ||
228 | ftdic = ftdi_new(); | |
229 | if (!ftdic) { | |
230 | sr_err("Failed to initialize libftdi."); | |
231 | return NULL; | |
232 | } | |
233 | ||
234 | if (conn) { | |
235 | devices = NULL; | |
236 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
237 | for (i = 0; devlist[i]; i++) { | |
238 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
239 | for (l = conn_devices; l; l = l->next) { | |
240 | usb = l->data; | |
241 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
242 | && usb->address == libusb_get_device_address(devlist[i])) { | |
243 | scan_device(ftdic, devlist[i], &devices); | |
244 | } | |
245 | } | |
246 | } | |
247 | libusb_free_device_list(devlist, 1); | |
248 | } else | |
249 | devices = scan_all(ftdic, options); | |
250 | ||
251 | ftdi_free(ftdic); | |
252 | ||
253 | return std_scan_complete(di, devices); | |
254 | } | |
255 | ||
256 | static void clear_helper(struct dev_context *devc) | |
257 | { | |
258 | g_free(devc->data_buf); | |
259 | } | |
260 | ||
261 | static int dev_clear(const struct sr_dev_driver *di) | |
262 | { | |
263 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)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 | ||
273 | devc->ftdic = ftdi_new(); | |
274 | if (!devc->ftdic) | |
275 | return SR_ERR; | |
276 | ||
277 | ret = ftdi_usb_open_string(devc->ftdic, sdi->connection_id); | |
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)); | |
283 | goto err_ftdi_free; | |
284 | } | |
285 | ||
286 | ret = ftdi_usb_purge_buffers(devc->ftdic); | |
287 | if (ret < 0) { | |
288 | sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.", | |
289 | ret, ftdi_get_error_string(devc->ftdic)); | |
290 | goto err_dev_open_close_ftdic; | |
291 | } | |
292 | ||
293 | ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET); | |
294 | if (ret < 0) { | |
295 | sr_err("Failed to reset the FTDI chip bitmode (%d): %s.", | |
296 | ret, ftdi_get_error_string(devc->ftdic)); | |
297 | goto err_dev_open_close_ftdic; | |
298 | } | |
299 | ||
300 | ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_BITBANG); | |
301 | if (ret < 0) { | |
302 | sr_err("Failed to put FTDI chip into bitbang mode (%d): %s.", | |
303 | ret, ftdi_get_error_string(devc->ftdic)); | |
304 | goto err_dev_open_close_ftdic; | |
305 | } | |
306 | ||
307 | return SR_OK; | |
308 | ||
309 | err_dev_open_close_ftdic: | |
310 | ftdi_usb_close(devc->ftdic); | |
311 | ||
312 | err_ftdi_free: | |
313 | ftdi_free(devc->ftdic); | |
314 | ||
315 | return SR_ERR; | |
316 | } | |
317 | ||
318 | static int dev_close(struct sr_dev_inst *sdi) | |
319 | { | |
320 | struct dev_context *devc; | |
321 | ||
322 | devc = sdi->priv; | |
323 | ||
324 | if (!devc->ftdic) | |
325 | return SR_ERR_BUG; | |
326 | ||
327 | ftdi_usb_close(devc->ftdic); | |
328 | ftdi_free(devc->ftdic); | |
329 | devc->ftdic = NULL; | |
330 | ||
331 | return SR_OK; | |
332 | } | |
333 | ||
334 | static int config_get(uint32_t key, GVariant **data, | |
335 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
336 | { | |
337 | struct dev_context *devc; | |
338 | struct sr_usb_dev_inst *usb; | |
339 | ||
340 | (void)cg; | |
341 | ||
342 | devc = sdi->priv; | |
343 | ||
344 | switch (key) { | |
345 | case SR_CONF_SAMPLERATE: | |
346 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
347 | break; | |
348 | case SR_CONF_CONN: | |
349 | if (!sdi || !sdi->conn) | |
350 | return SR_ERR_ARG; | |
351 | usb = sdi->conn; | |
352 | *data = g_variant_new_printf("%d.%d", usb->bus, usb->address); | |
353 | break; | |
354 | default: | |
355 | return SR_ERR_NA; | |
356 | } | |
357 | ||
358 | return SR_OK; | |
359 | } | |
360 | ||
361 | static int config_set(uint32_t key, GVariant *data, | |
362 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
363 | { | |
364 | struct dev_context *devc; | |
365 | uint64_t value; | |
366 | ||
367 | (void)cg; | |
368 | ||
369 | devc = sdi->priv; | |
370 | ||
371 | switch (key) { | |
372 | case SR_CONF_LIMIT_MSEC: | |
373 | value = g_variant_get_uint64(data); | |
374 | /* TODO: Implement. */ | |
375 | (void)value; | |
376 | return SR_ERR_NA; | |
377 | case SR_CONF_LIMIT_SAMPLES: | |
378 | devc->limit_samples = g_variant_get_uint64(data); | |
379 | break; | |
380 | case SR_CONF_SAMPLERATE: | |
381 | value = g_variant_get_uint64(data); | |
382 | if (value < 3600) | |
383 | return SR_ERR_SAMPLERATE; | |
384 | devc->cur_samplerate = value; | |
385 | return ftdi_la_set_samplerate(devc); | |
386 | default: | |
387 | return SR_ERR_NA; | |
388 | } | |
389 | ||
390 | return SR_OK; | |
391 | } | |
392 | ||
393 | static int config_list(uint32_t key, GVariant **data, | |
394 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
395 | { | |
396 | switch (key) { | |
397 | case SR_CONF_SCAN_OPTIONS: | |
398 | case SR_CONF_DEVICE_OPTIONS: | |
399 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
400 | case SR_CONF_SAMPLERATE: | |
401 | *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); | |
402 | break; | |
403 | default: | |
404 | return SR_ERR_NA; | |
405 | } | |
406 | ||
407 | return SR_OK; | |
408 | } | |
409 | ||
410 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
411 | { | |
412 | struct dev_context *devc; | |
413 | ||
414 | devc = sdi->priv; | |
415 | ||
416 | if (!devc->ftdic) | |
417 | return SR_ERR_BUG; | |
418 | ||
419 | ftdi_set_bitmode(devc->ftdic, 0, BITMODE_BITBANG); | |
420 | ||
421 | /* Properly reset internal variables before every new acquisition. */ | |
422 | devc->samples_sent = 0; | |
423 | devc->bytes_received = 0; | |
424 | ||
425 | std_session_send_df_header(sdi); | |
426 | ||
427 | /* Hook up a dummy handler to receive data from the device. */ | |
428 | sr_session_source_add(sdi->session, -1, G_IO_IN, 0, | |
429 | ftdi_la_receive_data, (void *)sdi); | |
430 | ||
431 | return SR_OK; | |
432 | } | |
433 | ||
434 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
435 | { | |
436 | sr_session_source_remove(sdi->session, -1); | |
437 | ||
438 | std_session_send_df_end(sdi); | |
439 | ||
440 | return SR_OK; | |
441 | } | |
442 | ||
443 | static struct sr_dev_driver ftdi_la_driver_info = { | |
444 | .name = "ftdi-la", | |
445 | .longname = "FTDI LA", | |
446 | .api_version = 1, | |
447 | .init = std_init, | |
448 | .cleanup = std_cleanup, | |
449 | .scan = scan, | |
450 | .dev_list = std_dev_list, | |
451 | .dev_clear = dev_clear, | |
452 | .config_get = config_get, | |
453 | .config_set = config_set, | |
454 | .config_list = config_list, | |
455 | .dev_open = dev_open, | |
456 | .dev_close = dev_close, | |
457 | .dev_acquisition_start = dev_acquisition_start, | |
458 | .dev_acquisition_stop = dev_acquisition_stop, | |
459 | .context = NULL, | |
460 | }; | |
461 | SR_REGISTER_DEV_DRIVER(ftdi_la_driver_info); |