]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2011-2015 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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 "protocol.h" | |
22 | ||
23 | static const uint32_t scanopts[] = { | |
24 | SR_CONF_CONN, | |
25 | }; | |
26 | ||
27 | static const uint32_t drvopts[] = { | |
28 | SR_CONF_LOGIC_ANALYZER, | |
29 | }; | |
30 | ||
31 | static const uint32_t devopts[] = { | |
32 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
33 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST, | |
34 | SR_CONF_CONN | SR_CONF_GET, | |
35 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
36 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
37 | }; | |
38 | ||
39 | static const int32_t trigger_matches[] = { | |
40 | SR_TRIGGER_ZERO, | |
41 | SR_TRIGGER_ONE, | |
42 | SR_TRIGGER_RISING, | |
43 | SR_TRIGGER_FALLING, | |
44 | }; | |
45 | ||
46 | static void clear_helper(struct dev_context *devc) | |
47 | { | |
48 | ftdi_free(devc->ftdic); | |
49 | g_free(devc->final_buf); | |
50 | } | |
51 | ||
52 | static int dev_clear(const struct sr_dev_driver *di) | |
53 | { | |
54 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); | |
55 | } | |
56 | ||
57 | static int add_device(int model, struct libusb_device_descriptor *des, | |
58 | const char *serial_num, const char *connection_id, libusb_device *usbdev, | |
59 | GSList **devices) | |
60 | { | |
61 | int ret; | |
62 | unsigned int i; | |
63 | struct sr_dev_inst *sdi; | |
64 | struct dev_context *devc; | |
65 | ||
66 | ret = SR_OK; | |
67 | ||
68 | devc = g_malloc0(sizeof(struct dev_context)); | |
69 | ||
70 | /* Set some sane defaults. */ | |
71 | devc->prof = &cv_profiles[model]; | |
72 | devc->ftdic = NULL; /* Will be set in the open() API call. */ | |
73 | devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */ | |
74 | devc->limit_msec = 0; | |
75 | devc->limit_samples = 0; | |
76 | memset(devc->mangled_buf, 0, BS); | |
77 | devc->final_buf = NULL; | |
78 | devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */ | |
79 | devc->trigger_mask = 0x0000; /* All channels: "don't care". */ | |
80 | devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */ | |
81 | devc->trigger_found = 0; | |
82 | devc->done = 0; | |
83 | devc->block_counter = 0; | |
84 | devc->divcount = 0; | |
85 | devc->usb_vid = des->idVendor; | |
86 | devc->usb_pid = des->idProduct; | |
87 | memset(devc->samplerates, 0, sizeof(uint64_t) * 255); | |
88 | ||
89 | /* Allocate memory where we'll store the de-mangled data. */ | |
90 | if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) { | |
91 | sr_err("Failed to allocate memory for sample buffer."); | |
92 | ret = SR_ERR_MALLOC; | |
93 | goto err_free_devc; | |
94 | } | |
95 | ||
96 | /* We now know the device, set its max. samplerate as default. */ | |
97 | devc->cur_samplerate = devc->prof->max_samplerate; | |
98 | ||
99 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
100 | sdi->status = SR_ST_INACTIVE; | |
101 | sdi->vendor = g_strdup("ChronoVu"); | |
102 | sdi->model = g_strdup(devc->prof->modelname); | |
103 | sdi->serial_num = g_strdup(serial_num); | |
104 | sdi->connection_id = g_strdup(connection_id); | |
105 | sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(usbdev), | |
106 | libusb_get_device_address(usbdev), NULL); | |
107 | sdi->priv = devc; | |
108 | ||
109 | for (i = 0; i < devc->prof->num_channels; i++) | |
110 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, | |
111 | cv_channel_names[i]); | |
112 | ||
113 | *devices = g_slist_append(*devices, sdi); | |
114 | ||
115 | if (ret == SR_OK) | |
116 | return SR_OK; | |
117 | ||
118 | err_free_devc: | |
119 | g_free(devc); | |
120 | ||
121 | return ret; | |
122 | } | |
123 | ||
124 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
125 | { | |
126 | int i, ret, model; | |
127 | struct drv_context *drvc; | |
128 | GSList *devices, *conn_devices, *l; | |
129 | struct sr_usb_dev_inst *usb; | |
130 | struct sr_config *src; | |
131 | struct libusb_device_descriptor des; | |
132 | libusb_device **devlist; | |
133 | struct libusb_device_handle *hdl; | |
134 | const char *conn; | |
135 | char product[64], serial_num[64], connection_id[64]; | |
136 | ||
137 | drvc = di->context; | |
138 | ||
139 | conn = NULL; | |
140 | for (l = options; l; l = l->next) { | |
141 | src = l->data; | |
142 | switch (src->key) { | |
143 | case SR_CONF_CONN: | |
144 | conn = g_variant_get_string(src->data, NULL); | |
145 | break; | |
146 | } | |
147 | } | |
148 | if (conn) | |
149 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
150 | else | |
151 | conn_devices = NULL; | |
152 | ||
153 | devices = NULL; | |
154 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
155 | ||
156 | for (i = 0; devlist[i]; i++) { | |
157 | if (conn) { | |
158 | for (l = conn_devices; l; l = l->next) { | |
159 | usb = l->data; | |
160 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
161 | && usb->address == libusb_get_device_address(devlist[i])) | |
162 | break; | |
163 | } | |
164 | if (!l) | |
165 | /* This device matched none of the ones that | |
166 | * matched the conn specification. */ | |
167 | continue; | |
168 | } | |
169 | ||
170 | libusb_get_device_descriptor(devlist[i], &des); | |
171 | ||
172 | if ((ret = libusb_open(devlist[i], &hdl)) < 0) | |
173 | continue; | |
174 | ||
175 | if (des.iProduct == 0) { | |
176 | product[0] = '\0'; | |
177 | } else if ((ret = libusb_get_string_descriptor_ascii(hdl, | |
178 | des.iProduct, (unsigned char *)product, | |
179 | sizeof(product))) < 0) { | |
180 | sr_warn("Failed to get product string descriptor: %s.", | |
181 | libusb_error_name(ret)); | |
182 | continue; | |
183 | } | |
184 | ||
185 | if (des.iSerialNumber == 0) { | |
186 | serial_num[0] = '\0'; | |
187 | } else if ((ret = libusb_get_string_descriptor_ascii(hdl, | |
188 | des.iSerialNumber, (unsigned char *)serial_num, | |
189 | sizeof(serial_num))) < 0) { | |
190 | sr_warn("Failed to get serial number string descriptor: %s.", | |
191 | libusb_error_name(ret)); | |
192 | continue; | |
193 | } | |
194 | ||
195 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
196 | ||
197 | libusb_close(hdl); | |
198 | ||
199 | if (!strcmp(product, "ChronoVu LA8")) { | |
200 | model = 0; | |
201 | } else if (!strcmp(product, "ChronoVu LA16")) { | |
202 | model = 1; | |
203 | } else { | |
204 | sr_spew("Unknown iProduct string '%s'.", product); | |
205 | continue; | |
206 | } | |
207 | ||
208 | sr_dbg("Found %s (%04x:%04x, %d.%d, %s).", | |
209 | product, des.idVendor, des.idProduct, | |
210 | libusb_get_bus_number(devlist[i]), | |
211 | libusb_get_device_address(devlist[i]), connection_id); | |
212 | ||
213 | if ((ret = add_device(model, &des, serial_num, connection_id, | |
214 | devlist[i], &devices)) < 0) { | |
215 | sr_dbg("Failed to add device: %d.", ret); | |
216 | } | |
217 | } | |
218 | ||
219 | libusb_free_device_list(devlist, 1); | |
220 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); | |
221 | ||
222 | return std_scan_complete(di, devices); | |
223 | } | |
224 | ||
225 | static int dev_open(struct sr_dev_inst *sdi) | |
226 | { | |
227 | struct dev_context *devc; | |
228 | int ret; | |
229 | ||
230 | devc = sdi->priv; | |
231 | ||
232 | if (!(devc->ftdic = ftdi_new())) { | |
233 | sr_err("Failed to initialize libftdi."); | |
234 | return SR_ERR; | |
235 | } | |
236 | ||
237 | sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname, | |
238 | devc->usb_vid, devc->usb_pid); | |
239 | ||
240 | if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid, | |
241 | devc->usb_pid, devc->prof->iproduct, NULL)) < 0) { | |
242 | sr_err("Failed to open FTDI device (%d): %s.", | |
243 | ret, ftdi_get_error_string(devc->ftdic)); | |
244 | goto err_ftdi_free; | |
245 | } | |
246 | ||
247 | if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) { | |
248 | sr_err("Failed to purge FTDI buffers (%d): %s.", | |
249 | ret, ftdi_get_error_string(devc->ftdic)); | |
250 | goto err_ftdi_free; | |
251 | } | |
252 | ||
253 | if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) { | |
254 | sr_err("Failed to enable FTDI flow control (%d): %s.", | |
255 | ret, ftdi_get_error_string(devc->ftdic)); | |
256 | goto err_ftdi_free; | |
257 | } | |
258 | ||
259 | g_usleep(100 * 1000); | |
260 | ||
261 | return SR_OK; | |
262 | ||
263 | err_ftdi_free: | |
264 | ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */ | |
265 | devc->ftdic = NULL; | |
266 | return SR_ERR; | |
267 | } | |
268 | ||
269 | static int dev_close(struct sr_dev_inst *sdi) | |
270 | { | |
271 | int ret; | |
272 | struct dev_context *devc; | |
273 | ||
274 | devc = sdi->priv; | |
275 | ||
276 | if (!devc->ftdic) | |
277 | return SR_ERR_BUG; | |
278 | ||
279 | if ((ret = ftdi_usb_close(devc->ftdic)) < 0) | |
280 | sr_err("Failed to close FTDI device (%d): %s.", | |
281 | ret, ftdi_get_error_string(devc->ftdic)); | |
282 | ||
283 | return (ret == 0) ? SR_OK : SR_ERR; | |
284 | } | |
285 | ||
286 | static int config_get(uint32_t key, GVariant **data, | |
287 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
288 | { | |
289 | struct dev_context *devc; | |
290 | struct sr_usb_dev_inst *usb; | |
291 | ||
292 | (void)cg; | |
293 | ||
294 | switch (key) { | |
295 | case SR_CONF_CONN: | |
296 | if (!sdi || !(usb = sdi->conn)) | |
297 | return SR_ERR_ARG; | |
298 | *data = g_variant_new_printf("%d.%d", usb->bus, usb->address); | |
299 | break; | |
300 | case SR_CONF_SAMPLERATE: | |
301 | if (!sdi) | |
302 | return SR_ERR_BUG; | |
303 | devc = sdi->priv; | |
304 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
305 | break; | |
306 | default: | |
307 | return SR_ERR_NA; | |
308 | } | |
309 | ||
310 | return SR_OK; | |
311 | } | |
312 | ||
313 | static int config_set(uint32_t key, GVariant *data, | |
314 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
315 | { | |
316 | struct dev_context *devc; | |
317 | ||
318 | (void)cg; | |
319 | ||
320 | devc = sdi->priv; | |
321 | ||
322 | switch (key) { | |
323 | case SR_CONF_SAMPLERATE: | |
324 | if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0) | |
325 | return SR_ERR; | |
326 | break; | |
327 | case SR_CONF_LIMIT_MSEC: | |
328 | if (g_variant_get_uint64(data) == 0) | |
329 | return SR_ERR_ARG; | |
330 | devc->limit_msec = g_variant_get_uint64(data); | |
331 | break; | |
332 | case SR_CONF_LIMIT_SAMPLES: | |
333 | if (g_variant_get_uint64(data) == 0) | |
334 | return SR_ERR_ARG; | |
335 | devc->limit_samples = g_variant_get_uint64(data); | |
336 | break; | |
337 | default: | |
338 | return SR_ERR_NA; | |
339 | } | |
340 | ||
341 | return SR_OK; | |
342 | } | |
343 | ||
344 | static int config_list(uint32_t key, GVariant **data, | |
345 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
346 | { | |
347 | struct dev_context *devc; | |
348 | ||
349 | devc = (sdi) ? sdi->priv : NULL; | |
350 | ||
351 | switch (key) { | |
352 | case SR_CONF_SCAN_OPTIONS: | |
353 | case SR_CONF_DEVICE_OPTIONS: | |
354 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
355 | case SR_CONF_SAMPLERATE: | |
356 | cv_fill_samplerates_if_needed(sdi); | |
357 | *data = std_gvar_samplerates(ARRAY_AND_SIZE(devc->samplerates)); | |
358 | break; | |
359 | case SR_CONF_LIMIT_SAMPLES: | |
360 | if (!devc->prof) | |
361 | return SR_ERR_BUG; | |
362 | *data = std_gvar_tuple_u64(0, (devc->prof->model == CHRONOVU_LA8) ? MAX_NUM_SAMPLES : MAX_NUM_SAMPLES / 2); | |
363 | break; | |
364 | case SR_CONF_TRIGGER_MATCH: | |
365 | if (!devc->prof) | |
366 | return SR_ERR_BUG; | |
367 | *data = std_gvar_array_i32(trigger_matches, devc->prof->num_trigger_matches); | |
368 | break; | |
369 | default: | |
370 | return SR_ERR_NA; | |
371 | } | |
372 | ||
373 | return SR_OK; | |
374 | } | |
375 | ||
376 | static int receive_data(int fd, int revents, void *cb_data) | |
377 | { | |
378 | int i, ret; | |
379 | struct sr_dev_inst *sdi; | |
380 | struct dev_context *devc; | |
381 | ||
382 | (void)fd; | |
383 | (void)revents; | |
384 | ||
385 | if (!(sdi = cb_data)) { | |
386 | sr_err("cb_data was NULL."); | |
387 | return FALSE; | |
388 | } | |
389 | ||
390 | if (!(devc = sdi->priv)) { | |
391 | sr_err("sdi->priv was NULL."); | |
392 | return FALSE; | |
393 | } | |
394 | ||
395 | if (!devc->ftdic) { | |
396 | sr_err("devc->ftdic was NULL."); | |
397 | return FALSE; | |
398 | } | |
399 | ||
400 | /* Get one block of data. */ | |
401 | if ((ret = cv_read_block(devc)) < 0) { | |
402 | sr_err("Failed to read data block: %d.", ret); | |
403 | sr_dev_acquisition_stop(sdi); | |
404 | return FALSE; | |
405 | } | |
406 | ||
407 | /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */ | |
408 | if (devc->block_counter != (NUM_BLOCKS - 1)) { | |
409 | devc->block_counter++; | |
410 | return TRUE; | |
411 | } | |
412 | ||
413 | sr_dbg("Sampling finished, sending data to session bus now."); | |
414 | ||
415 | /* | |
416 | * All data was received and demangled, send it to the session bus. | |
417 | * | |
418 | * Note: Due to the method how data is spread across the 8MByte of | |
419 | * SDRAM, we can _not_ send it to the session bus in a streaming | |
420 | * manner while we receive it. We have to receive and de-mangle the | |
421 | * full 8MByte first, only then the whole buffer contains valid data. | |
422 | */ | |
423 | for (i = 0; i < NUM_BLOCKS; i++) | |
424 | cv_send_block_to_session_bus(sdi, i); | |
425 | ||
426 | sr_dev_acquisition_stop(sdi); | |
427 | ||
428 | return TRUE; | |
429 | } | |
430 | ||
431 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
432 | { | |
433 | struct dev_context *devc; | |
434 | uint8_t buf[8]; | |
435 | int bytes_to_write, bytes_written; | |
436 | ||
437 | devc = sdi->priv; | |
438 | ||
439 | if (!devc->ftdic) { | |
440 | sr_err("devc->ftdic was NULL."); | |
441 | return SR_ERR_BUG; | |
442 | } | |
443 | ||
444 | devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate); | |
445 | if (devc->divcount == 0xff) { | |
446 | sr_err("Invalid divcount/samplerate."); | |
447 | return SR_ERR; | |
448 | } | |
449 | ||
450 | if (cv_convert_trigger(sdi) != SR_OK) { | |
451 | sr_err("Failed to configure trigger."); | |
452 | return SR_ERR; | |
453 | } | |
454 | ||
455 | /* Fill acquisition parameters into buf[]. */ | |
456 | if (devc->prof->model == CHRONOVU_LA8) { | |
457 | buf[0] = devc->divcount; | |
458 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
459 | buf[2] = devc->trigger_pattern & 0xff; | |
460 | buf[3] = devc->trigger_mask & 0xff; | |
461 | bytes_to_write = 4; | |
462 | } else { | |
463 | buf[0] = devc->divcount; | |
464 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
465 | buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */ | |
466 | buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */ | |
467 | buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */ | |
468 | buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */ | |
469 | buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */ | |
470 | buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */ | |
471 | bytes_to_write = 8; | |
472 | } | |
473 | ||
474 | /* Start acquisition. */ | |
475 | bytes_written = cv_write(devc, buf, bytes_to_write); | |
476 | ||
477 | if (bytes_written < 0 || bytes_written != bytes_to_write) { | |
478 | sr_err("Acquisition failed to start."); | |
479 | return SR_ERR; | |
480 | } | |
481 | ||
482 | std_session_send_df_header(sdi); | |
483 | ||
484 | /* Time when we should be done (for detecting trigger timeouts). */ | |
485 | devc->done = (devc->divcount + 1) * devc->prof->trigger_constant + | |
486 | g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND); | |
487 | devc->block_counter = 0; | |
488 | devc->trigger_found = 0; | |
489 | ||
490 | /* Hook up a dummy handler to receive data from the device. */ | |
491 | sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi); | |
492 | ||
493 | return SR_OK; | |
494 | } | |
495 | ||
496 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
497 | { | |
498 | sr_session_source_remove(sdi->session, -1); | |
499 | std_session_send_df_end(sdi); | |
500 | ||
501 | return SR_OK; | |
502 | } | |
503 | ||
504 | static struct sr_dev_driver chronovu_la_driver_info = { | |
505 | .name = "chronovu-la", | |
506 | .longname = "ChronoVu LA8/LA16", | |
507 | .api_version = 1, | |
508 | .init = std_init, | |
509 | .cleanup = std_cleanup, | |
510 | .scan = scan, | |
511 | .dev_list = std_dev_list, | |
512 | .dev_clear = dev_clear, | |
513 | .config_get = config_get, | |
514 | .config_set = config_set, | |
515 | .config_list = config_list, | |
516 | .dev_open = dev_open, | |
517 | .dev_close = dev_close, | |
518 | .dev_acquisition_start = dev_acquisition_start, | |
519 | .dev_acquisition_stop = dev_acquisition_stop, | |
520 | .context = NULL, | |
521 | }; | |
522 | SR_REGISTER_DEV_DRIVER(chronovu_la_driver_info); |