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