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