]>
Commit | Line | Data |
---|---|---|
c463dcf0 MC |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se> | |
fec7aa6a MC |
5 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> |
6 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
c463dcf0 MC |
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 | ||
f6a21fa5 MC |
22 | #include <glib.h> |
23 | #include <libusb.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
db11d7d2 | 26 | #include <math.h> |
f6a21fa5 MC |
27 | #include "libsigrok.h" |
28 | #include "libsigrok-internal.h" | |
c463dcf0 MC |
29 | #include "protocol.h" |
30 | ||
96484e22 UH |
31 | #define LOGIC16_VID 0x21a9 |
32 | #define LOGIC16_PID 0x1001 | |
f6a21fa5 | 33 | |
5eea4305 MC |
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 | |
7b5daad4 | 39 | #define NUM_SIMUL_TRANSFERS 32 |
5eea4305 | 40 | |
c463dcf0 MC |
41 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info; |
42 | static struct sr_dev_driver *di = &saleae_logic16_driver_info; | |
43 | ||
a0e0bb41 | 44 | static const uint32_t scanopts[] = { |
b117363a MC |
45 | SR_CONF_CONN, |
46 | }; | |
47 | ||
f254bc4b | 48 | static const uint32_t devopts[] = { |
b117363a | 49 | SR_CONF_LOGIC_ANALYZER, |
b117363a | 50 | SR_CONF_CONTINUOUS, |
5827f61b BV |
51 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, |
52 | SR_CONF_CONN | SR_CONF_GET, | |
53 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
54 | SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
55 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
5a971f66 | 56 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, |
b117363a MC |
57 | }; |
58 | ||
863357fb BV |
59 | static const int32_t soft_trigger_matches[] = { |
60 | SR_TRIGGER_ZERO, | |
61 | SR_TRIGGER_ONE, | |
62 | SR_TRIGGER_RISING, | |
63 | SR_TRIGGER_FALLING, | |
64 | SR_TRIGGER_EDGE, | |
65 | }; | |
66 | ||
ba7dd8bb | 67 | static const char *channel_names[] = { |
f6a21fa5 MC |
68 | "0", "1", "2", "3", "4", "5", "6", "7", "8", |
69 | "9", "10", "11", "12", "13", "14", "15", | |
70 | NULL, | |
71 | }; | |
c463dcf0 | 72 | |
db11d7d2 MC |
73 | static const struct { |
74 | enum voltage_range range; | |
75 | gdouble low; | |
76 | gdouble high; | |
77 | } volt_thresholds[] = { | |
78 | { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 }, | |
79 | { VOLTAGE_RANGE_5_V, 1.4, 3.6 }, | |
80 | }; | |
81 | ||
7b5daad4 MC |
82 | static const uint64_t samplerates[] = { |
83 | SR_KHZ(500), | |
84 | SR_MHZ(1), | |
85 | SR_MHZ(2), | |
86 | SR_MHZ(4), | |
87 | SR_MHZ(5), | |
88 | SR_MHZ(8), | |
89 | SR_MHZ(10), | |
90 | SR_KHZ(12500), | |
91 | SR_MHZ(16), | |
92 | SR_MHZ(25), | |
93 | SR_MHZ(32), | |
94 | SR_MHZ(40), | |
95 | SR_MHZ(80), | |
96 | SR_MHZ(100), | |
97 | }; | |
98 | ||
c463dcf0 MC |
99 | static int init(struct sr_context *sr_ctx) |
100 | { | |
101 | return std_init(sr_ctx, di, LOG_PREFIX); | |
102 | } | |
103 | ||
5eea4305 MC |
104 | static gboolean check_conf_profile(libusb_device *dev) |
105 | { | |
106 | struct libusb_device_descriptor des; | |
107 | struct libusb_device_handle *hdl; | |
108 | gboolean ret; | |
109 | unsigned char strdesc[64]; | |
110 | ||
111 | hdl = NULL; | |
112 | ret = FALSE; | |
113 | while (!ret) { | |
114 | /* Assume the FW has not been loaded, unless proven wrong. */ | |
115 | if (libusb_get_device_descriptor(dev, &des) != 0) | |
116 | break; | |
117 | ||
118 | if (libusb_open(dev, &hdl) != 0) | |
119 | break; | |
120 | ||
121 | if (libusb_get_string_descriptor_ascii(hdl, | |
122 | des.iManufacturer, strdesc, sizeof(strdesc)) < 0) | |
123 | break; | |
124 | if (strcmp((const char *)strdesc, "Saleae LLC")) | |
125 | break; | |
126 | ||
127 | if (libusb_get_string_descriptor_ascii(hdl, | |
96484e22 | 128 | des.iProduct, strdesc, sizeof(strdesc)) < 0) |
5eea4305 MC |
129 | break; |
130 | if (strcmp((const char *)strdesc, "Logic S/16")) | |
131 | break; | |
132 | ||
133 | /* If we made it here, it must be a configured Logic16. */ | |
134 | ret = TRUE; | |
135 | } | |
136 | if (hdl) | |
137 | libusb_close(hdl); | |
138 | ||
139 | return ret; | |
140 | } | |
141 | ||
c463dcf0 MC |
142 | static GSList *scan(GSList *options) |
143 | { | |
144 | struct drv_context *drvc; | |
f6a21fa5 MC |
145 | struct dev_context *devc; |
146 | struct sr_dev_inst *sdi; | |
5eea4305 | 147 | struct sr_usb_dev_inst *usb; |
5eea4305 MC |
148 | struct sr_config *src; |
149 | GSList *l, *devices, *conn_devices; | |
f6a21fa5 MC |
150 | struct libusb_device_descriptor des; |
151 | libusb_device **devlist; | |
ce7d3578 | 152 | int ret, i, j; |
5eea4305 | 153 | const char *conn; |
ce7d3578 | 154 | char connection_id[64]; |
c463dcf0 | 155 | |
c463dcf0 | 156 | drvc = di->priv; |
c463dcf0 | 157 | |
5eea4305 MC |
158 | conn = NULL; |
159 | for (l = options; l; l = l->next) { | |
160 | src = l->data; | |
161 | switch (src->key) { | |
162 | case SR_CONF_CONN: | |
163 | conn = g_variant_get_string(src->data, NULL); | |
164 | break; | |
165 | } | |
166 | } | |
167 | if (conn) | |
168 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
169 | else | |
170 | conn_devices = NULL; | |
171 | ||
172 | /* Find all Logic16 devices and upload firmware to them. */ | |
f6a21fa5 MC |
173 | devices = NULL; |
174 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
175 | for (i = 0; devlist[i]; i++) { | |
5eea4305 MC |
176 | if (conn) { |
177 | usb = NULL; | |
178 | for (l = conn_devices; l; l = l->next) { | |
179 | usb = l->data; | |
180 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
181 | && usb->address == libusb_get_device_address(devlist[i])) | |
182 | break; | |
183 | } | |
184 | if (!l) | |
185 | /* This device matched none of the ones that | |
186 | * matched the conn specification. */ | |
187 | continue; | |
188 | } | |
189 | ||
96484e22 | 190 | if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) { |
5eea4305 MC |
191 | sr_warn("Failed to get device descriptor: %s.", |
192 | libusb_error_name(ret)); | |
f6a21fa5 MC |
193 | continue; |
194 | } | |
195 | ||
ce7d3578 SA |
196 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); |
197 | ||
f6a21fa5 MC |
198 | if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) |
199 | continue; | |
200 | ||
aac29cc1 | 201 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
202 | sdi->status = SR_ST_INITIALIZING; |
203 | sdi->vendor = g_strdup("Saleae"); | |
204 | sdi->model = g_strdup("Logic16"); | |
f6a21fa5 | 205 | sdi->driver = di; |
ce7d3578 | 206 | sdi->connection_id = g_strdup(connection_id); |
f6a21fa5 | 207 | |
5e23fcab ML |
208 | for (j = 0; channel_names[j]; j++) |
209 | sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, | |
c368e6f3 | 210 | channel_names[j]); |
f6a21fa5 | 211 | |
f57d8ffe | 212 | devc = g_malloc0(sizeof(struct dev_context)); |
db11d7d2 | 213 | devc->selected_voltage_range = VOLTAGE_RANGE_18_33_V; |
5eea4305 | 214 | sdi->priv = devc; |
f6a21fa5 MC |
215 | drvc->instances = g_slist_append(drvc->instances, sdi); |
216 | devices = g_slist_append(devices, sdi); | |
5eea4305 MC |
217 | |
218 | if (check_conf_profile(devlist[i])) { | |
219 | /* Already has the firmware, so fix the new address. */ | |
220 | sr_dbg("Found a Logic16 device."); | |
221 | sdi->status = SR_ST_INACTIVE; | |
222 | sdi->inst_type = SR_INST_USB; | |
96484e22 UH |
223 | sdi->conn = sr_usb_dev_inst_new( |
224 | libusb_get_bus_number(devlist[i]), | |
225 | libusb_get_device_address(devlist[i]), NULL); | |
5eea4305 MC |
226 | } else { |
227 | if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, | |
228 | FX2_FIRMWARE) == SR_OK) | |
229 | /* Store when this device's FW was updated. */ | |
230 | devc->fw_updated = g_get_monotonic_time(); | |
231 | else | |
ce7d3578 | 232 | sr_err("Firmware upload failed."); |
5eea4305 | 233 | sdi->inst_type = SR_INST_USB; |
96484e22 UH |
234 | sdi->conn = sr_usb_dev_inst_new( |
235 | libusb_get_bus_number(devlist[i]), 0xff, NULL); | |
5eea4305 | 236 | } |
f6a21fa5 MC |
237 | } |
238 | libusb_free_device_list(devlist, 1); | |
5eea4305 | 239 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); |
c463dcf0 MC |
240 | |
241 | return devices; | |
242 | } | |
243 | ||
244 | static GSList *dev_list(void) | |
245 | { | |
87b537ce | 246 | return ((struct drv_context *)(di->priv))->instances; |
c463dcf0 MC |
247 | } |
248 | ||
5eea4305 MC |
249 | static int logic16_dev_open(struct sr_dev_inst *sdi) |
250 | { | |
251 | libusb_device **devlist; | |
252 | struct sr_usb_dev_inst *usb; | |
253 | struct libusb_device_descriptor des; | |
5eea4305 | 254 | struct drv_context *drvc; |
ce7d3578 SA |
255 | int ret, i, device_count; |
256 | char connection_id[64]; | |
5eea4305 MC |
257 | |
258 | drvc = di->priv; | |
5eea4305 MC |
259 | usb = sdi->conn; |
260 | ||
261 | if (sdi->status == SR_ST_ACTIVE) | |
262 | /* Device is already in use. */ | |
263 | return SR_ERR; | |
264 | ||
5eea4305 MC |
265 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
266 | if (device_count < 0) { | |
267 | sr_err("Failed to get device list: %s.", | |
268 | libusb_error_name(device_count)); | |
269 | return SR_ERR; | |
270 | } | |
271 | ||
272 | for (i = 0; i < device_count; i++) { | |
273 | if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { | |
274 | sr_err("Failed to get device descriptor: %s.", | |
275 | libusb_error_name(ret)); | |
276 | continue; | |
277 | } | |
278 | ||
96484e22 | 279 | if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) |
5eea4305 MC |
280 | continue; |
281 | ||
ce7d3578 SA |
282 | if ((sdi->status == SR_ST_INITIALIZING) || |
283 | (sdi->status == SR_ST_INACTIVE)) { | |
5eea4305 | 284 | /* |
ce7d3578 | 285 | * Check device by its physical USB bus/port address. |
5eea4305 | 286 | */ |
ce7d3578 SA |
287 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); |
288 | if (strcmp(sdi->connection_id, connection_id)) | |
5eea4305 MC |
289 | /* This is not the one. */ |
290 | continue; | |
291 | } | |
292 | ||
293 | if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { | |
294 | if (usb->address == 0xff) | |
295 | /* | |
296 | * First time we touch this device after FW | |
297 | * upload, so we don't know the address yet. | |
298 | */ | |
299 | usb->address = libusb_get_device_address(devlist[i]); | |
300 | } else { | |
301 | sr_err("Failed to open device: %s.", | |
302 | libusb_error_name(ret)); | |
303 | break; | |
304 | } | |
305 | ||
0c30b35f SY |
306 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); |
307 | if (ret == LIBUSB_ERROR_BUSY) { | |
308 | sr_err("Unable to claim USB interface. Another " | |
309 | "program or driver has already claimed it."); | |
310 | break; | |
311 | } else if (ret == LIBUSB_ERROR_NO_DEVICE) { | |
312 | sr_err("Device has been disconnected."); | |
313 | break; | |
314 | } else if (ret != 0) { | |
315 | sr_err("Unable to claim interface: %s.", | |
316 | libusb_error_name(ret)); | |
317 | break; | |
318 | } | |
319 | ||
96484e22 | 320 | if ((ret = logic16_init_device(sdi)) != SR_OK) { |
15abcf0f MC |
321 | sr_err("Failed to init device."); |
322 | break; | |
323 | } | |
324 | ||
5eea4305 | 325 | sdi->status = SR_ST_ACTIVE; |
ce7d3578 SA |
326 | sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.", |
327 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
5eea4305 MC |
328 | |
329 | break; | |
330 | } | |
331 | libusb_free_device_list(devlist, 1); | |
332 | ||
0c30b35f SY |
333 | if (sdi->status != SR_ST_ACTIVE) { |
334 | if (usb->devhdl) { | |
335 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
336 | libusb_close(usb->devhdl); | |
337 | usb->devhdl = NULL; | |
338 | } | |
5eea4305 | 339 | return SR_ERR; |
0c30b35f | 340 | } |
5eea4305 MC |
341 | |
342 | return SR_OK; | |
343 | } | |
344 | ||
c463dcf0 MC |
345 | static int dev_open(struct sr_dev_inst *sdi) |
346 | { | |
5eea4305 MC |
347 | struct dev_context *devc; |
348 | int ret; | |
349 | int64_t timediff_us, timediff_ms; | |
350 | ||
351 | devc = sdi->priv; | |
5eea4305 MC |
352 | |
353 | /* | |
354 | * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS | |
355 | * milliseconds for the FX2 to renumerate. | |
356 | */ | |
357 | ret = SR_ERR; | |
358 | if (devc->fw_updated > 0) { | |
359 | sr_info("Waiting for device to reset."); | |
360 | /* Takes >= 300ms for the FX2 to be gone from the USB bus. */ | |
361 | g_usleep(300 * 1000); | |
362 | timediff_ms = 0; | |
363 | while (timediff_ms < MAX_RENUM_DELAY_MS) { | |
364 | if ((ret = logic16_dev_open(sdi)) == SR_OK) | |
365 | break; | |
366 | g_usleep(100 * 1000); | |
367 | ||
368 | timediff_us = g_get_monotonic_time() - devc->fw_updated; | |
369 | timediff_ms = timediff_us / 1000; | |
370 | sr_spew("Waited %" PRIi64 "ms.", timediff_ms); | |
371 | } | |
372 | if (ret != SR_OK) { | |
373 | sr_err("Device failed to renumerate."); | |
374 | return SR_ERR; | |
375 | } | |
376 | sr_info("Device came back after %" PRIi64 "ms.", timediff_ms); | |
377 | } else { | |
378 | sr_info("Firmware upload was not needed."); | |
379 | ret = logic16_dev_open(sdi); | |
380 | } | |
381 | ||
382 | if (ret != SR_OK) { | |
383 | sr_err("Unable to open device."); | |
384 | return SR_ERR; | |
385 | } | |
386 | ||
5eea4305 MC |
387 | if (devc->cur_samplerate == 0) { |
388 | /* Samplerate hasn't been set; default to the slowest one. */ | |
7b5daad4 | 389 | devc->cur_samplerate = samplerates[0]; |
5eea4305 | 390 | } |
c463dcf0 MC |
391 | |
392 | return SR_OK; | |
393 | } | |
394 | ||
395 | static int dev_close(struct sr_dev_inst *sdi) | |
396 | { | |
5eea4305 | 397 | struct sr_usb_dev_inst *usb; |
c463dcf0 | 398 | |
5eea4305 MC |
399 | usb = sdi->conn; |
400 | if (usb->devhdl == NULL) | |
401 | return SR_ERR; | |
c463dcf0 | 402 | |
ce7d3578 SA |
403 | sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.", |
404 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
5eea4305 MC |
405 | libusb_release_interface(usb->devhdl, USB_INTERFACE); |
406 | libusb_close(usb->devhdl); | |
407 | usb->devhdl = NULL; | |
c463dcf0 MC |
408 | sdi->status = SR_ST_INACTIVE; |
409 | ||
410 | return SR_OK; | |
411 | } | |
412 | ||
413 | static int cleanup(void) | |
414 | { | |
f6a21fa5 MC |
415 | int ret; |
416 | struct drv_context *drvc; | |
c463dcf0 | 417 | |
f6a21fa5 MC |
418 | if (!(drvc = di->priv)) |
419 | /* Can get called on an unused driver, doesn't matter. */ | |
420 | return SR_OK; | |
c463dcf0 | 421 | |
a6630742 | 422 | ret = std_dev_clear(di, NULL); |
f6a21fa5 MC |
423 | g_free(drvc); |
424 | di->priv = NULL; | |
425 | ||
426 | return ret; | |
c463dcf0 MC |
427 | } |
428 | ||
584560f1 | 429 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 430 | const struct sr_channel_group *cg) |
c463dcf0 | 431 | { |
7b5daad4 | 432 | struct dev_context *devc; |
b117363a | 433 | struct sr_usb_dev_inst *usb; |
db11d7d2 | 434 | GVariant *range[2]; |
b117363a | 435 | char str[128]; |
c463dcf0 | 436 | int ret; |
96484e22 | 437 | unsigned int i; |
c463dcf0 | 438 | |
53b4680f | 439 | (void)cg; |
d3c74a6f | 440 | |
c463dcf0 MC |
441 | ret = SR_OK; |
442 | switch (key) { | |
b117363a MC |
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; | |
7b5daad4 MC |
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; | |
5a971f66 AJ |
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; | |
db11d7d2 MC |
466 | case SR_CONF_VOLTAGE_THRESHOLD: |
467 | if (!sdi) | |
468 | return SR_ERR; | |
469 | devc = sdi->priv; | |
470 | ret = SR_ERR; | |
96484e22 UH |
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 | } | |
db11d7d2 | 481 | break; |
c463dcf0 MC |
482 | default: |
483 | return SR_ERR_NA; | |
484 | } | |
485 | ||
486 | return ret; | |
487 | } | |
488 | ||
584560f1 | 489 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 490 | const struct sr_channel_group *cg) |
c463dcf0 | 491 | { |
7b5daad4 | 492 | struct dev_context *devc; |
db11d7d2 | 493 | gdouble low, high; |
c463dcf0 | 494 | int ret; |
96484e22 | 495 | unsigned int i; |
c463dcf0 | 496 | |
53b4680f | 497 | (void)cg; |
d3c74a6f | 498 | |
c463dcf0 MC |
499 | if (sdi->status != SR_ST_ACTIVE) |
500 | return SR_ERR_DEV_CLOSED; | |
501 | ||
7b5daad4 MC |
502 | devc = sdi->priv; |
503 | ||
c463dcf0 MC |
504 | ret = SR_OK; |
505 | switch (key) { | |
7b5daad4 MC |
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; | |
5a971f66 AJ |
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; | |
db11d7d2 MC |
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; | |
c463dcf0 MC |
533 | default: |
534 | ret = SR_ERR_NA; | |
535 | } | |
536 | ||
537 | return ret; | |
538 | } | |
539 | ||
584560f1 | 540 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 541 | const struct sr_channel_group *cg) |
c463dcf0 | 542 | { |
db11d7d2 | 543 | GVariant *gvar, *range[2]; |
7b5daad4 | 544 | GVariantBuilder gvb; |
c463dcf0 | 545 | int ret; |
96484e22 | 546 | unsigned int i; |
c463dcf0 MC |
547 | |
548 | (void)sdi; | |
53b4680f | 549 | (void)cg; |
c463dcf0 MC |
550 | |
551 | ret = SR_OK; | |
552 | switch (key) { | |
b117363a | 553 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 554 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
a0e0bb41 | 555 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
b117363a MC |
556 | break; |
557 | case SR_CONF_DEVICE_OPTIONS: | |
584560f1 | 558 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
f254bc4b | 559 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); |
b117363a | 560 | break; |
7b5daad4 MC |
561 | case SR_CONF_SAMPLERATE: |
562 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
96484e22 UH |
563 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), |
564 | samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
7b5daad4 MC |
565 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); |
566 | *data = g_variant_builder_end(&gvb); | |
db11d7d2 MC |
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); | |
7b5daad4 | 577 | break; |
863357fb BV |
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; | |
c463dcf0 MC |
583 | default: |
584 | return SR_ERR_NA; | |
585 | } | |
586 | ||
587 | return ret; | |
588 | } | |
589 | ||
7b5daad4 MC |
590 | static void abort_acquisition(struct dev_context *devc) |
591 | { | |
592 | int i; | |
593 | ||
863357fb | 594 | devc->sent_samples = -1; |
7b5daad4 MC |
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 | ||
ba7dd8bb | 642 | static int configure_channels(const struct sr_dev_inst *sdi) |
7b5daad4 MC |
643 | { |
644 | struct dev_context *devc; | |
ba7dd8bb | 645 | struct sr_channel *ch; |
7b5daad4 | 646 | GSList *l; |
ba7dd8bb | 647 | uint16_t channel_bit; |
7b5daad4 MC |
648 | |
649 | devc = sdi->priv; | |
650 | ||
651 | devc->cur_channels = 0; | |
652 | devc->num_channels = 0; | |
ba7dd8bb UH |
653 | for (l = sdi->channels; l; l = l->next) { |
654 | ch = (struct sr_channel *)l->data; | |
655 | if (ch->enabled == FALSE) | |
7b5daad4 MC |
656 | continue; |
657 | ||
ba7dd8bb | 658 | channel_bit = 1 << (ch->index); |
7b5daad4 | 659 | |
ba7dd8bb | 660 | devc->cur_channels |= channel_bit; |
7b5daad4 MC |
661 | |
662 | #ifdef WORDS_BIGENDIAN | |
96484e22 UH |
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 | */ | |
ba7dd8bb | 668 | channel_bit = 1 << (ch->index ^ 8); |
7b5daad4 MC |
669 | #endif |
670 | ||
ba7dd8bb | 671 | devc->channel_masks[devc->num_channels++] = channel_bit; |
7b5daad4 MC |
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 | ||
684 | (void)fd; | |
685 | (void)revents; | |
686 | ||
687 | sdi = cb_data; | |
688 | drvc = di->priv; | |
689 | devc = sdi->priv; | |
690 | ||
691 | tv.tv_sec = tv.tv_usec = 0; | |
692 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
693 | ||
863357fb | 694 | if (devc->sent_samples == -2) { |
96484e22 | 695 | logic16_abort_acquisition(sdi); |
7b5daad4 MC |
696 | abort_acquisition(devc); |
697 | } | |
698 | ||
699 | return TRUE; | |
700 | } | |
701 | ||
96484e22 | 702 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
c463dcf0 | 703 | { |
7b5daad4 MC |
704 | struct dev_context *devc; |
705 | struct drv_context *drvc; | |
706 | struct sr_usb_dev_inst *usb; | |
863357fb | 707 | struct sr_trigger *trigger; |
7b5daad4 | 708 | struct libusb_transfer *transfer; |
7b5daad4 MC |
709 | unsigned int i, timeout, num_transfers; |
710 | int ret; | |
711 | unsigned char *buf; | |
712 | size_t size, convsize; | |
c463dcf0 MC |
713 | |
714 | if (sdi->status != SR_ST_ACTIVE) | |
715 | return SR_ERR_DEV_CLOSED; | |
716 | ||
7b5daad4 MC |
717 | drvc = di->priv; |
718 | devc = sdi->priv; | |
719 | usb = sdi->conn; | |
720 | ||
96484e22 | 721 | /* Configures devc->cur_channels. */ |
ba7dd8bb UH |
722 | if (configure_channels(sdi) != SR_OK) { |
723 | sr_err("Failed to configure channels."); | |
7b5daad4 MC |
724 | return SR_ERR; |
725 | } | |
726 | ||
727 | devc->cb_data = cb_data; | |
863357fb | 728 | devc->sent_samples = 0; |
7b5daad4 MC |
729 | devc->empty_transfer_count = 0; |
730 | devc->cur_channel = 0; | |
731 | memset(devc->channel_data, 0, sizeof(devc->channel_data)); | |
732 | ||
0812c40e | 733 | if ((trigger = sr_session_trigger_get(sdi->session))) { |
5a971f66 AJ |
734 | int pre_trigger_samples = 0; |
735 | if (devc->limit_samples > 0) | |
736 | pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100; | |
737 | devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples); | |
738 | if (devc->stl == NULL) | |
739 | return SR_ERR_MALLOC; | |
863357fb BV |
740 | devc->trigger_fired = FALSE; |
741 | } else | |
742 | devc->trigger_fired = TRUE; | |
743 | ||
7b5daad4 MC |
744 | timeout = get_timeout(devc); |
745 | num_transfers = get_number_of_transfers(devc); | |
746 | size = get_buffer_size(devc); | |
747 | convsize = (size / devc->num_channels + 2) * 16; | |
748 | devc->submitted_transfers = 0; | |
7b5daad4 MC |
749 | |
750 | devc->convbuffer_size = convsize; | |
751 | if (!(devc->convbuffer = g_try_malloc(convsize))) { | |
752 | sr_err("Conversion buffer malloc failed."); | |
753 | return SR_ERR_MALLOC; | |
754 | } | |
755 | ||
756 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); | |
757 | if (!devc->transfers) { | |
758 | sr_err("USB transfers malloc failed."); | |
759 | g_free(devc->convbuffer); | |
760 | return SR_ERR_MALLOC; | |
761 | } | |
762 | ||
96484e22 UH |
763 | if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate, |
764 | devc->cur_channels)) != SR_OK) { | |
7b5daad4 MC |
765 | g_free(devc->transfers); |
766 | g_free(devc->convbuffer); | |
767 | return ret; | |
768 | } | |
769 | ||
770 | devc->num_transfers = num_transfers; | |
771 | for (i = 0; i < num_transfers; i++) { | |
772 | if (!(buf = g_try_malloc(size))) { | |
773 | sr_err("USB transfer buffer malloc failed."); | |
774 | if (devc->submitted_transfers) | |
775 | abort_acquisition(devc); | |
776 | else { | |
777 | g_free(devc->transfers); | |
778 | g_free(devc->convbuffer); | |
779 | } | |
780 | return SR_ERR_MALLOC; | |
781 | } | |
782 | transfer = libusb_alloc_transfer(0); | |
783 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
784 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
102f1239 | 785 | logic16_receive_transfer, (void *)sdi, timeout); |
7b5daad4 MC |
786 | if ((ret = libusb_submit_transfer(transfer)) != 0) { |
787 | sr_err("Failed to submit transfer: %s.", | |
788 | libusb_error_name(ret)); | |
789 | libusb_free_transfer(transfer); | |
790 | g_free(buf); | |
791 | abort_acquisition(devc); | |
792 | return SR_ERR; | |
793 | } | |
794 | devc->transfers[i] = transfer; | |
795 | devc->submitted_transfers++; | |
796 | } | |
797 | ||
6c60facc ML |
798 | devc->ctx = drvc->sr_ctx; |
799 | ||
102f1239 | 800 | usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi); |
7b5daad4 MC |
801 | |
802 | /* Send header packet to the session bus. */ | |
803 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
804 | ||
96484e22 | 805 | if ((ret = logic16_start_acquisition(sdi)) != SR_OK) { |
7b5daad4 MC |
806 | abort_acquisition(devc); |
807 | return ret; | |
808 | } | |
c463dcf0 MC |
809 | |
810 | return SR_OK; | |
811 | } | |
812 | ||
813 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
814 | { | |
7b5daad4 MC |
815 | int ret; |
816 | ||
c463dcf0 MC |
817 | (void)cb_data; |
818 | ||
819 | if (sdi->status != SR_ST_ACTIVE) | |
820 | return SR_ERR_DEV_CLOSED; | |
821 | ||
96484e22 | 822 | ret = logic16_abort_acquisition(sdi); |
c463dcf0 | 823 | |
7b5daad4 MC |
824 | abort_acquisition(sdi->priv); |
825 | ||
826 | return ret; | |
c463dcf0 MC |
827 | } |
828 | ||
829 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = { | |
830 | .name = "saleae-logic16", | |
831 | .longname = "Saleae Logic16", | |
832 | .api_version = 1, | |
833 | .init = init, | |
834 | .cleanup = cleanup, | |
835 | .scan = scan, | |
836 | .dev_list = dev_list, | |
a6630742 | 837 | .dev_clear = NULL, |
c463dcf0 MC |
838 | .config_get = config_get, |
839 | .config_set = config_set, | |
840 | .config_list = config_list, | |
841 | .dev_open = dev_open, | |
842 | .dev_close = dev_close, | |
843 | .dev_acquisition_start = dev_acquisition_start, | |
844 | .dev_acquisition_stop = dev_acquisition_stop, | |
845 | .priv = NULL, | |
846 | }; |