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