]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se> | |
5 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
6 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
7 | * | |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include <glib.h> | |
23 | #include <libusb.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <math.h> | |
27 | #include "libsigrok.h" | |
28 | #include "libsigrok-internal.h" | |
29 | #include "protocol.h" | |
30 | ||
31 | #define LOGIC16_VID 0x21a9 | |
32 | #define LOGIC16_PID 0x1001 | |
33 | ||
34 | #define USB_INTERFACE 0 | |
35 | #define USB_CONFIGURATION 1 | |
36 | #define FX2_FIRMWARE FIRMWARE_DIR "/saleae-logic16-fx2.fw" | |
37 | ||
38 | #define MAX_RENUM_DELAY_MS 3000 | |
39 | #define NUM_SIMUL_TRANSFERS 32 | |
40 | ||
41 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info; | |
42 | ||
43 | static const uint32_t scanopts[] = { | |
44 | SR_CONF_CONN, | |
45 | }; | |
46 | ||
47 | static const uint32_t devopts[] = { | |
48 | SR_CONF_LOGIC_ANALYZER, | |
49 | SR_CONF_CONTINUOUS, | |
50 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, | |
51 | SR_CONF_CONN | SR_CONF_GET, | |
52 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
53 | SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
54 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
55 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
56 | }; | |
57 | ||
58 | static const int32_t soft_trigger_matches[] = { | |
59 | SR_TRIGGER_ZERO, | |
60 | SR_TRIGGER_ONE, | |
61 | SR_TRIGGER_RISING, | |
62 | SR_TRIGGER_FALLING, | |
63 | SR_TRIGGER_EDGE, | |
64 | }; | |
65 | ||
66 | static const char *channel_names[] = { | |
67 | "0", "1", "2", "3", "4", "5", "6", "7", "8", | |
68 | "9", "10", "11", "12", "13", "14", "15", | |
69 | NULL, | |
70 | }; | |
71 | ||
72 | static const struct { | |
73 | enum voltage_range range; | |
74 | gdouble low; | |
75 | gdouble high; | |
76 | } volt_thresholds[] = { | |
77 | { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 }, | |
78 | { VOLTAGE_RANGE_5_V, 1.4, 3.6 }, | |
79 | }; | |
80 | ||
81 | static const uint64_t samplerates[] = { | |
82 | SR_KHZ(500), | |
83 | SR_MHZ(1), | |
84 | SR_MHZ(2), | |
85 | SR_MHZ(4), | |
86 | SR_MHZ(5), | |
87 | SR_MHZ(8), | |
88 | SR_MHZ(10), | |
89 | SR_KHZ(12500), | |
90 | SR_MHZ(16), | |
91 | SR_MHZ(25), | |
92 | SR_MHZ(32), | |
93 | SR_MHZ(40), | |
94 | SR_MHZ(80), | |
95 | SR_MHZ(100), | |
96 | }; | |
97 | ||
98 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
99 | { | |
100 | return std_init(sr_ctx, di, LOG_PREFIX); | |
101 | } | |
102 | ||
103 | static gboolean check_conf_profile(libusb_device *dev) | |
104 | { | |
105 | struct libusb_device_descriptor des; | |
106 | struct libusb_device_handle *hdl; | |
107 | gboolean ret; | |
108 | unsigned char strdesc[64]; | |
109 | ||
110 | hdl = NULL; | |
111 | ret = FALSE; | |
112 | while (!ret) { | |
113 | /* Assume the FW has not been loaded, unless proven wrong. */ | |
114 | if (libusb_get_device_descriptor(dev, &des) != 0) | |
115 | break; | |
116 | ||
117 | if (libusb_open(dev, &hdl) != 0) | |
118 | break; | |
119 | ||
120 | if (libusb_get_string_descriptor_ascii(hdl, | |
121 | des.iManufacturer, strdesc, sizeof(strdesc)) < 0) | |
122 | break; | |
123 | if (strcmp((const char *)strdesc, "Saleae LLC")) | |
124 | break; | |
125 | ||
126 | if (libusb_get_string_descriptor_ascii(hdl, | |
127 | des.iProduct, strdesc, sizeof(strdesc)) < 0) | |
128 | break; | |
129 | if (strcmp((const char *)strdesc, "Logic S/16")) | |
130 | break; | |
131 | ||
132 | /* If we made it here, it must be a configured Logic16. */ | |
133 | ret = TRUE; | |
134 | } | |
135 | if (hdl) | |
136 | libusb_close(hdl); | |
137 | ||
138 | return ret; | |
139 | } | |
140 | ||
141 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
142 | { | |
143 | struct drv_context *drvc; | |
144 | struct dev_context *devc; | |
145 | struct sr_dev_inst *sdi; | |
146 | struct sr_usb_dev_inst *usb; | |
147 | struct sr_config *src; | |
148 | GSList *l, *devices, *conn_devices; | |
149 | struct libusb_device_descriptor des; | |
150 | libusb_device **devlist; | |
151 | int ret, i, j; | |
152 | const char *conn; | |
153 | char connection_id[64]; | |
154 | ||
155 | drvc = di->priv; | |
156 | ||
157 | conn = NULL; | |
158 | for (l = options; l; l = l->next) { | |
159 | src = l->data; | |
160 | switch (src->key) { | |
161 | case SR_CONF_CONN: | |
162 | conn = g_variant_get_string(src->data, NULL); | |
163 | break; | |
164 | } | |
165 | } | |
166 | if (conn) | |
167 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
168 | else | |
169 | conn_devices = NULL; | |
170 | ||
171 | /* Find all Logic16 devices and upload firmware to them. */ | |
172 | devices = NULL; | |
173 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
174 | for (i = 0; devlist[i]; i++) { | |
175 | if (conn) { | |
176 | usb = NULL; | |
177 | for (l = conn_devices; l; l = l->next) { | |
178 | usb = l->data; | |
179 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
180 | && usb->address == libusb_get_device_address(devlist[i])) | |
181 | break; | |
182 | } | |
183 | if (!l) | |
184 | /* This device matched none of the ones that | |
185 | * matched the conn specification. */ | |
186 | continue; | |
187 | } | |
188 | ||
189 | if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) { | |
190 | sr_warn("Failed to get device 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 | if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) | |
198 | continue; | |
199 | ||
200 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
201 | sdi->status = SR_ST_INITIALIZING; | |
202 | sdi->vendor = g_strdup("Saleae"); | |
203 | sdi->model = g_strdup("Logic16"); | |
204 | sdi->driver = di; | |
205 | sdi->connection_id = g_strdup(connection_id); | |
206 | ||
207 | for (j = 0; channel_names[j]; j++) | |
208 | sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, | |
209 | channel_names[j]); | |
210 | ||
211 | devc = g_malloc0(sizeof(struct dev_context)); | |
212 | devc->selected_voltage_range = VOLTAGE_RANGE_18_33_V; | |
213 | sdi->priv = devc; | |
214 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
215 | devices = g_slist_append(devices, sdi); | |
216 | ||
217 | if (check_conf_profile(devlist[i])) { | |
218 | /* Already has the firmware, so fix the new address. */ | |
219 | sr_dbg("Found a Logic16 device."); | |
220 | sdi->status = SR_ST_INACTIVE; | |
221 | sdi->inst_type = SR_INST_USB; | |
222 | sdi->conn = sr_usb_dev_inst_new( | |
223 | libusb_get_bus_number(devlist[i]), | |
224 | libusb_get_device_address(devlist[i]), NULL); | |
225 | } else { | |
226 | if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, | |
227 | FX2_FIRMWARE) == SR_OK) | |
228 | /* Store when this device's FW was updated. */ | |
229 | devc->fw_updated = g_get_monotonic_time(); | |
230 | else | |
231 | sr_err("Firmware upload failed."); | |
232 | sdi->inst_type = SR_INST_USB; | |
233 | sdi->conn = sr_usb_dev_inst_new( | |
234 | libusb_get_bus_number(devlist[i]), 0xff, NULL); | |
235 | } | |
236 | } | |
237 | libusb_free_device_list(devlist, 1); | |
238 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); | |
239 | ||
240 | return devices; | |
241 | } | |
242 | ||
243 | static GSList *dev_list(const struct sr_dev_driver *di) | |
244 | { | |
245 | return ((struct drv_context *)(di->priv))->instances; | |
246 | } | |
247 | ||
248 | static int logic16_dev_open(struct sr_dev_inst *sdi) | |
249 | { | |
250 | struct sr_dev_driver *di; | |
251 | libusb_device **devlist; | |
252 | struct sr_usb_dev_inst *usb; | |
253 | struct libusb_device_descriptor des; | |
254 | struct drv_context *drvc; | |
255 | int ret, i, device_count; | |
256 | char connection_id[64]; | |
257 | ||
258 | di = sdi->driver; | |
259 | drvc = di->priv; | |
260 | usb = sdi->conn; | |
261 | ||
262 | if (sdi->status == SR_ST_ACTIVE) | |
263 | /* Device is already in use. */ | |
264 | return SR_ERR; | |
265 | ||
266 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
267 | if (device_count < 0) { | |
268 | sr_err("Failed to get device list: %s.", | |
269 | libusb_error_name(device_count)); | |
270 | return SR_ERR; | |
271 | } | |
272 | ||
273 | for (i = 0; i < device_count; i++) { | |
274 | if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { | |
275 | sr_err("Failed to get device descriptor: %s.", | |
276 | libusb_error_name(ret)); | |
277 | continue; | |
278 | } | |
279 | ||
280 | if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) | |
281 | continue; | |
282 | ||
283 | if ((sdi->status == SR_ST_INITIALIZING) || | |
284 | (sdi->status == SR_ST_INACTIVE)) { | |
285 | /* | |
286 | * Check device by its physical USB bus/port address. | |
287 | */ | |
288 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
289 | if (strcmp(sdi->connection_id, connection_id)) | |
290 | /* This is not the one. */ | |
291 | continue; | |
292 | } | |
293 | ||
294 | if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { | |
295 | if (usb->address == 0xff) | |
296 | /* | |
297 | * First time we touch this device after FW | |
298 | * upload, so we don't know the address yet. | |
299 | */ | |
300 | usb->address = libusb_get_device_address(devlist[i]); | |
301 | } else { | |
302 | sr_err("Failed to open device: %s.", | |
303 | libusb_error_name(ret)); | |
304 | break; | |
305 | } | |
306 | ||
307 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); | |
308 | if (ret == LIBUSB_ERROR_BUSY) { | |
309 | sr_err("Unable to claim USB interface. Another " | |
310 | "program or driver has already claimed it."); | |
311 | break; | |
312 | } else if (ret == LIBUSB_ERROR_NO_DEVICE) { | |
313 | sr_err("Device has been disconnected."); | |
314 | break; | |
315 | } else if (ret != 0) { | |
316 | sr_err("Unable to claim interface: %s.", | |
317 | libusb_error_name(ret)); | |
318 | break; | |
319 | } | |
320 | ||
321 | if ((ret = logic16_init_device(sdi)) != SR_OK) { | |
322 | sr_err("Failed to init device."); | |
323 | break; | |
324 | } | |
325 | ||
326 | sdi->status = SR_ST_ACTIVE; | |
327 | sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.", | |
328 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
329 | ||
330 | break; | |
331 | } | |
332 | libusb_free_device_list(devlist, 1); | |
333 | ||
334 | if (sdi->status != SR_ST_ACTIVE) { | |
335 | if (usb->devhdl) { | |
336 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
337 | libusb_close(usb->devhdl); | |
338 | usb->devhdl = NULL; | |
339 | } | |
340 | return SR_ERR; | |
341 | } | |
342 | ||
343 | return SR_OK; | |
344 | } | |
345 | ||
346 | static int dev_open(struct sr_dev_inst *sdi) | |
347 | { | |
348 | struct dev_context *devc; | |
349 | int ret; | |
350 | int64_t timediff_us, timediff_ms; | |
351 | ||
352 | devc = sdi->priv; | |
353 | ||
354 | /* | |
355 | * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS | |
356 | * milliseconds for the FX2 to renumerate. | |
357 | */ | |
358 | ret = SR_ERR; | |
359 | if (devc->fw_updated > 0) { | |
360 | sr_info("Waiting for device to reset."); | |
361 | /* Takes >= 300ms for the FX2 to be gone from the USB bus. */ | |
362 | g_usleep(300 * 1000); | |
363 | timediff_ms = 0; | |
364 | while (timediff_ms < MAX_RENUM_DELAY_MS) { | |
365 | if ((ret = logic16_dev_open(sdi)) == SR_OK) | |
366 | break; | |
367 | g_usleep(100 * 1000); | |
368 | ||
369 | timediff_us = g_get_monotonic_time() - devc->fw_updated; | |
370 | timediff_ms = timediff_us / 1000; | |
371 | sr_spew("Waited %" PRIi64 "ms.", timediff_ms); | |
372 | } | |
373 | if (ret != SR_OK) { | |
374 | sr_err("Device failed to renumerate."); | |
375 | return SR_ERR; | |
376 | } | |
377 | sr_info("Device came back after %" PRIi64 "ms.", timediff_ms); | |
378 | } else { | |
379 | sr_info("Firmware upload was not needed."); | |
380 | ret = logic16_dev_open(sdi); | |
381 | } | |
382 | ||
383 | if (ret != SR_OK) { | |
384 | sr_err("Unable to open device."); | |
385 | return SR_ERR; | |
386 | } | |
387 | ||
388 | if (devc->cur_samplerate == 0) { | |
389 | /* Samplerate hasn't been set; default to the slowest one. */ | |
390 | devc->cur_samplerate = samplerates[0]; | |
391 | } | |
392 | ||
393 | return SR_OK; | |
394 | } | |
395 | ||
396 | static int dev_close(struct sr_dev_inst *sdi) | |
397 | { | |
398 | struct sr_usb_dev_inst *usb; | |
399 | ||
400 | usb = sdi->conn; | |
401 | if (usb->devhdl == NULL) | |
402 | return SR_ERR; | |
403 | ||
404 | sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.", | |
405 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
406 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
407 | libusb_close(usb->devhdl); | |
408 | usb->devhdl = NULL; | |
409 | sdi->status = SR_ST_INACTIVE; | |
410 | ||
411 | return SR_OK; | |
412 | } | |
413 | ||
414 | static int cleanup(const struct sr_dev_driver *di) | |
415 | { | |
416 | int ret; | |
417 | struct drv_context *drvc; | |
418 | ||
419 | if (!(drvc = di->priv)) | |
420 | /* Can get called on an unused driver, doesn't matter. */ | |
421 | return SR_OK; | |
422 | ||
423 | ret = std_dev_clear(di, NULL); | |
424 | g_free(drvc); | |
425 | ||
426 | return ret; | |
427 | } | |
428 | ||
429 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
430 | const struct sr_channel_group *cg) | |
431 | { | |
432 | struct dev_context *devc; | |
433 | struct sr_usb_dev_inst *usb; | |
434 | GVariant *range[2]; | |
435 | char str[128]; | |
436 | int ret; | |
437 | unsigned int i; | |
438 | ||
439 | (void)cg; | |
440 | ||
441 | ret = SR_OK; | |
442 | switch (key) { | |
443 | case SR_CONF_CONN: | |
444 | if (!sdi || !sdi->conn) | |
445 | return SR_ERR_ARG; | |
446 | usb = sdi->conn; | |
447 | if (usb->address == 255) | |
448 | /* Device still needs to re-enumerate after firmware | |
449 | * upload, so we don't know its (future) address. */ | |
450 | return SR_ERR; | |
451 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
452 | *data = g_variant_new_string(str); | |
453 | break; | |
454 | case SR_CONF_SAMPLERATE: | |
455 | if (!sdi) | |
456 | return SR_ERR; | |
457 | devc = sdi->priv; | |
458 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
459 | break; | |
460 | case SR_CONF_CAPTURE_RATIO: | |
461 | if (!sdi) | |
462 | return SR_ERR; | |
463 | devc = sdi->priv; | |
464 | *data = g_variant_new_uint64(devc->capture_ratio); | |
465 | break; | |
466 | case SR_CONF_VOLTAGE_THRESHOLD: | |
467 | if (!sdi) | |
468 | return SR_ERR; | |
469 | devc = sdi->priv; | |
470 | ret = SR_ERR; | |
471 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { | |
472 | if (devc->selected_voltage_range != | |
473 | volt_thresholds[i].range) | |
474 | continue; | |
475 | range[0] = g_variant_new_double(volt_thresholds[i].low); | |
476 | range[1] = g_variant_new_double(volt_thresholds[i].high); | |
477 | *data = g_variant_new_tuple(range, 2); | |
478 | ret = SR_OK; | |
479 | break; | |
480 | } | |
481 | break; | |
482 | default: | |
483 | return SR_ERR_NA; | |
484 | } | |
485 | ||
486 | return ret; | |
487 | } | |
488 | ||
489 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
490 | const struct sr_channel_group *cg) | |
491 | { | |
492 | struct dev_context *devc; | |
493 | gdouble low, high; | |
494 | int ret; | |
495 | unsigned int i; | |
496 | ||
497 | (void)cg; | |
498 | ||
499 | if (sdi->status != SR_ST_ACTIVE) | |
500 | return SR_ERR_DEV_CLOSED; | |
501 | ||
502 | devc = sdi->priv; | |
503 | ||
504 | ret = SR_OK; | |
505 | switch (key) { | |
506 | case SR_CONF_SAMPLERATE: | |
507 | devc->cur_samplerate = g_variant_get_uint64(data); | |
508 | break; | |
509 | case SR_CONF_LIMIT_SAMPLES: | |
510 | devc->limit_samples = g_variant_get_uint64(data); | |
511 | break; | |
512 | case SR_CONF_CAPTURE_RATIO: | |
513 | devc->capture_ratio = g_variant_get_uint64(data); | |
514 | if (devc->capture_ratio > 100) { | |
515 | devc->capture_ratio = 0; | |
516 | ret = SR_ERR; | |
517 | } else | |
518 | ret = SR_OK; | |
519 | break; | |
520 | case SR_CONF_VOLTAGE_THRESHOLD: | |
521 | g_variant_get(data, "(dd)", &low, &high); | |
522 | ret = SR_ERR_ARG; | |
523 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { | |
524 | if (fabs(volt_thresholds[i].low - low) < 0.1 && | |
525 | fabs(volt_thresholds[i].high - high) < 0.1) { | |
526 | devc->selected_voltage_range = | |
527 | volt_thresholds[i].range; | |
528 | ret = SR_OK; | |
529 | break; | |
530 | } | |
531 | } | |
532 | break; | |
533 | default: | |
534 | ret = SR_ERR_NA; | |
535 | } | |
536 | ||
537 | return ret; | |
538 | } | |
539 | ||
540 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
541 | const struct sr_channel_group *cg) | |
542 | { | |
543 | GVariant *gvar, *range[2]; | |
544 | GVariantBuilder gvb; | |
545 | int ret; | |
546 | unsigned int i; | |
547 | ||
548 | (void)sdi; | |
549 | (void)cg; | |
550 | ||
551 | ret = SR_OK; | |
552 | switch (key) { | |
553 | case SR_CONF_SCAN_OPTIONS: | |
554 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
555 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
556 | break; | |
557 | case SR_CONF_DEVICE_OPTIONS: | |
558 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
559 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
560 | break; | |
561 | case SR_CONF_SAMPLERATE: | |
562 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
563 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
564 | samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
565 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
566 | *data = g_variant_builder_end(&gvb); | |
567 | break; | |
568 | case SR_CONF_VOLTAGE_THRESHOLD: | |
569 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
570 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { | |
571 | range[0] = g_variant_new_double(volt_thresholds[i].low); | |
572 | range[1] = g_variant_new_double(volt_thresholds[i].high); | |
573 | gvar = g_variant_new_tuple(range, 2); | |
574 | g_variant_builder_add_value(&gvb, gvar); | |
575 | } | |
576 | *data = g_variant_builder_end(&gvb); | |
577 | break; | |
578 | case SR_CONF_TRIGGER_MATCH: | |
579 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
580 | soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches), | |
581 | sizeof(int32_t)); | |
582 | break; | |
583 | default: | |
584 | return SR_ERR_NA; | |
585 | } | |
586 | ||
587 | return ret; | |
588 | } | |
589 | ||
590 | static void abort_acquisition(struct dev_context *devc) | |
591 | { | |
592 | int i; | |
593 | ||
594 | devc->sent_samples = -1; | |
595 | ||
596 | for (i = devc->num_transfers - 1; i >= 0; i--) { | |
597 | if (devc->transfers[i]) | |
598 | libusb_cancel_transfer(devc->transfers[i]); | |
599 | } | |
600 | } | |
601 | ||
602 | static unsigned int bytes_per_ms(struct dev_context *devc) | |
603 | { | |
604 | return devc->cur_samplerate * devc->num_channels / 8000; | |
605 | } | |
606 | ||
607 | static size_t get_buffer_size(struct dev_context *devc) | |
608 | { | |
609 | size_t s; | |
610 | ||
611 | /* | |
612 | * The buffer should be large enough to hold 10ms of data and | |
613 | * a multiple of 512. | |
614 | */ | |
615 | s = 10 * bytes_per_ms(devc); | |
616 | return (s + 511) & ~511; | |
617 | } | |
618 | ||
619 | static unsigned int get_number_of_transfers(struct dev_context *devc) | |
620 | { | |
621 | unsigned int n; | |
622 | ||
623 | /* Total buffer size should be able to hold about 500ms of data. */ | |
624 | n = 500 * bytes_per_ms(devc) / get_buffer_size(devc); | |
625 | ||
626 | if (n > NUM_SIMUL_TRANSFERS) | |
627 | return NUM_SIMUL_TRANSFERS; | |
628 | ||
629 | return n; | |
630 | } | |
631 | ||
632 | static unsigned int get_timeout(struct dev_context *devc) | |
633 | { | |
634 | size_t total_size; | |
635 | unsigned int timeout; | |
636 | ||
637 | total_size = get_buffer_size(devc) * get_number_of_transfers(devc); | |
638 | timeout = total_size / bytes_per_ms(devc); | |
639 | return timeout + timeout / 4; /* Leave a headroom of 25% percent. */ | |
640 | } | |
641 | ||
642 | static int configure_channels(const struct sr_dev_inst *sdi) | |
643 | { | |
644 | struct dev_context *devc; | |
645 | struct sr_channel *ch; | |
646 | GSList *l; | |
647 | uint16_t channel_bit; | |
648 | ||
649 | devc = sdi->priv; | |
650 | ||
651 | devc->cur_channels = 0; | |
652 | devc->num_channels = 0; | |
653 | for (l = sdi->channels; l; l = l->next) { | |
654 | ch = (struct sr_channel *)l->data; | |
655 | if (ch->enabled == FALSE) | |
656 | continue; | |
657 | ||
658 | channel_bit = 1 << (ch->index); | |
659 | ||
660 | devc->cur_channels |= channel_bit; | |
661 | ||
662 | #ifdef WORDS_BIGENDIAN | |
663 | /* | |
664 | * Output logic data should be stored in little endian format. | |
665 | * To speed things up during conversion, do the switcharoo | |
666 | * here instead. | |
667 | */ | |
668 | channel_bit = 1 << (ch->index ^ 8); | |
669 | #endif | |
670 | ||
671 | devc->channel_masks[devc->num_channels++] = channel_bit; | |
672 | } | |
673 | ||
674 | return SR_OK; | |
675 | } | |
676 | ||
677 | static int receive_data(int fd, int revents, void *cb_data) | |
678 | { | |
679 | struct timeval tv; | |
680 | struct dev_context *devc; | |
681 | struct drv_context *drvc; | |
682 | const struct sr_dev_inst *sdi; | |
683 | struct sr_dev_driver *di; | |
684 | ||
685 | (void)fd; | |
686 | (void)revents; | |
687 | ||
688 | sdi = cb_data; | |
689 | di = sdi->driver; | |
690 | drvc = di->priv; | |
691 | devc = sdi->priv; | |
692 | ||
693 | tv.tv_sec = tv.tv_usec = 0; | |
694 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
695 | ||
696 | if (devc->sent_samples == -2) { | |
697 | logic16_abort_acquisition(sdi); | |
698 | abort_acquisition(devc); | |
699 | } | |
700 | ||
701 | return TRUE; | |
702 | } | |
703 | ||
704 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
705 | { | |
706 | struct sr_dev_driver *di = sdi->driver; | |
707 | struct dev_context *devc; | |
708 | struct drv_context *drvc; | |
709 | struct sr_usb_dev_inst *usb; | |
710 | struct sr_trigger *trigger; | |
711 | struct libusb_transfer *transfer; | |
712 | unsigned int i, timeout, num_transfers; | |
713 | int ret; | |
714 | unsigned char *buf; | |
715 | size_t size, convsize; | |
716 | ||
717 | if (sdi->status != SR_ST_ACTIVE) | |
718 | return SR_ERR_DEV_CLOSED; | |
719 | ||
720 | drvc = di->priv; | |
721 | devc = sdi->priv; | |
722 | usb = sdi->conn; | |
723 | ||
724 | /* Configures devc->cur_channels. */ | |
725 | if (configure_channels(sdi) != SR_OK) { | |
726 | sr_err("Failed to configure channels."); | |
727 | return SR_ERR; | |
728 | } | |
729 | ||
730 | devc->cb_data = cb_data; | |
731 | devc->sent_samples = 0; | |
732 | devc->empty_transfer_count = 0; | |
733 | devc->cur_channel = 0; | |
734 | memset(devc->channel_data, 0, sizeof(devc->channel_data)); | |
735 | ||
736 | if ((trigger = sr_session_trigger_get(sdi->session))) { | |
737 | int pre_trigger_samples = 0; | |
738 | if (devc->limit_samples > 0) | |
739 | pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100; | |
740 | devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples); | |
741 | if (devc->stl == NULL) | |
742 | return SR_ERR_MALLOC; | |
743 | devc->trigger_fired = FALSE; | |
744 | } else | |
745 | devc->trigger_fired = TRUE; | |
746 | ||
747 | timeout = get_timeout(devc); | |
748 | num_transfers = get_number_of_transfers(devc); | |
749 | size = get_buffer_size(devc); | |
750 | convsize = (size / devc->num_channels + 2) * 16; | |
751 | devc->submitted_transfers = 0; | |
752 | ||
753 | devc->convbuffer_size = convsize; | |
754 | if (!(devc->convbuffer = g_try_malloc(convsize))) { | |
755 | sr_err("Conversion buffer malloc failed."); | |
756 | return SR_ERR_MALLOC; | |
757 | } | |
758 | ||
759 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); | |
760 | if (!devc->transfers) { | |
761 | sr_err("USB transfers malloc failed."); | |
762 | g_free(devc->convbuffer); | |
763 | return SR_ERR_MALLOC; | |
764 | } | |
765 | ||
766 | if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate, | |
767 | devc->cur_channels)) != SR_OK) { | |
768 | g_free(devc->transfers); | |
769 | g_free(devc->convbuffer); | |
770 | return ret; | |
771 | } | |
772 | ||
773 | devc->num_transfers = num_transfers; | |
774 | for (i = 0; i < num_transfers; i++) { | |
775 | if (!(buf = g_try_malloc(size))) { | |
776 | sr_err("USB transfer buffer malloc failed."); | |
777 | if (devc->submitted_transfers) | |
778 | abort_acquisition(devc); | |
779 | else { | |
780 | g_free(devc->transfers); | |
781 | g_free(devc->convbuffer); | |
782 | } | |
783 | return SR_ERR_MALLOC; | |
784 | } | |
785 | transfer = libusb_alloc_transfer(0); | |
786 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
787 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
788 | logic16_receive_transfer, (void *)sdi, timeout); | |
789 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
790 | sr_err("Failed to submit transfer: %s.", | |
791 | libusb_error_name(ret)); | |
792 | libusb_free_transfer(transfer); | |
793 | g_free(buf); | |
794 | abort_acquisition(devc); | |
795 | return SR_ERR; | |
796 | } | |
797 | devc->transfers[i] = transfer; | |
798 | devc->submitted_transfers++; | |
799 | } | |
800 | ||
801 | devc->ctx = drvc->sr_ctx; | |
802 | ||
803 | usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi); | |
804 | ||
805 | /* Send header packet to the session bus. */ | |
806 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
807 | ||
808 | if ((ret = logic16_start_acquisition(sdi)) != SR_OK) { | |
809 | abort_acquisition(devc); | |
810 | return ret; | |
811 | } | |
812 | ||
813 | return SR_OK; | |
814 | } | |
815 | ||
816 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
817 | { | |
818 | int ret; | |
819 | ||
820 | (void)cb_data; | |
821 | ||
822 | if (sdi->status != SR_ST_ACTIVE) | |
823 | return SR_ERR_DEV_CLOSED; | |
824 | ||
825 | ret = logic16_abort_acquisition(sdi); | |
826 | ||
827 | abort_acquisition(sdi->priv); | |
828 | ||
829 | return ret; | |
830 | } | |
831 | ||
832 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = { | |
833 | .name = "saleae-logic16", | |
834 | .longname = "Saleae Logic16", | |
835 | .api_version = 1, | |
836 | .init = init, | |
837 | .cleanup = cleanup, | |
838 | .scan = scan, | |
839 | .dev_list = dev_list, | |
840 | .dev_clear = NULL, | |
841 | .config_get = config_get, | |
842 | .config_set = config_set, | |
843 | .config_list = config_list, | |
844 | .dev_open = dev_open, | |
845 | .dev_close = dev_close, | |
846 | .dev_acquisition_start = dev_acquisition_start, | |
847 | .dev_acquisition_stop = dev_acquisition_stop, | |
848 | .priv = NULL, | |
849 | }; |