]>
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 | |
37 | #define FX2_FIRMWARE FIRMWARE_DIR "/saleae-logic16-fx2.fw" | |
38 | ||
39 | #define MAX_RENUM_DELAY_MS 3000 | |
7b5daad4 | 40 | #define NUM_SIMUL_TRANSFERS 32 |
5eea4305 | 41 | |
c463dcf0 | 42 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info; |
c463dcf0 | 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", | |
f6a21fa5 | 70 | }; |
c463dcf0 | 71 | |
db11d7d2 MC |
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 | ||
7b5daad4 MC |
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 | ||
4f840ce9 | 98 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) |
c463dcf0 MC |
99 | { |
100 | return std_init(sr_ctx, di, LOG_PREFIX); | |
101 | } | |
102 | ||
5eea4305 MC |
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, | |
96484e22 | 127 | des.iProduct, strdesc, sizeof(strdesc)) < 0) |
5eea4305 MC |
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 | ||
4f840ce9 | 141 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
c463dcf0 MC |
142 | { |
143 | struct drv_context *drvc; | |
f6a21fa5 MC |
144 | struct dev_context *devc; |
145 | struct sr_dev_inst *sdi; | |
5eea4305 | 146 | struct sr_usb_dev_inst *usb; |
5eea4305 MC |
147 | struct sr_config *src; |
148 | GSList *l, *devices, *conn_devices; | |
f6a21fa5 MC |
149 | struct libusb_device_descriptor des; |
150 | libusb_device **devlist; | |
2cca921d UH |
151 | int ret; |
152 | unsigned int i, j; | |
5eea4305 | 153 | const char *conn; |
ce7d3578 | 154 | char connection_id[64]; |
c463dcf0 | 155 | |
41812aca | 156 | drvc = di->context; |
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 | |
2cca921d | 208 | for (j = 0; j < ARRAY_SIZE(channel_names); j++) |
5e23fcab | 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 | ||
4f840ce9 | 244 | static GSList *dev_list(const struct sr_dev_driver *di) |
c463dcf0 | 245 | { |
41812aca | 246 | return ((struct drv_context *)(di->context))->instances; |
c463dcf0 MC |
247 | } |
248 | ||
5eea4305 MC |
249 | static int logic16_dev_open(struct sr_dev_inst *sdi) |
250 | { | |
4f840ce9 | 251 | struct sr_dev_driver *di; |
5eea4305 MC |
252 | libusb_device **devlist; |
253 | struct sr_usb_dev_inst *usb; | |
254 | struct libusb_device_descriptor des; | |
5eea4305 | 255 | struct drv_context *drvc; |
ce7d3578 SA |
256 | int ret, i, device_count; |
257 | char connection_id[64]; | |
5eea4305 | 258 | |
4f840ce9 | 259 | di = sdi->driver; |
41812aca | 260 | drvc = di->context; |
5eea4305 MC |
261 | usb = sdi->conn; |
262 | ||
263 | if (sdi->status == SR_ST_ACTIVE) | |
264 | /* Device is already in use. */ | |
265 | return SR_ERR; | |
266 | ||
5eea4305 MC |
267 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
268 | if (device_count < 0) { | |
269 | sr_err("Failed to get device list: %s.", | |
270 | libusb_error_name(device_count)); | |
271 | return SR_ERR; | |
272 | } | |
273 | ||
274 | for (i = 0; i < device_count; i++) { | |
275 | if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { | |
276 | sr_err("Failed to get device descriptor: %s.", | |
277 | libusb_error_name(ret)); | |
278 | continue; | |
279 | } | |
280 | ||
96484e22 | 281 | if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) |
5eea4305 MC |
282 | continue; |
283 | ||
ce7d3578 SA |
284 | if ((sdi->status == SR_ST_INITIALIZING) || |
285 | (sdi->status == SR_ST_INACTIVE)) { | |
5eea4305 | 286 | /* |
ce7d3578 | 287 | * Check device by its physical USB bus/port address. |
5eea4305 | 288 | */ |
ce7d3578 SA |
289 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); |
290 | if (strcmp(sdi->connection_id, connection_id)) | |
5eea4305 MC |
291 | /* This is not the one. */ |
292 | continue; | |
293 | } | |
294 | ||
295 | if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { | |
296 | if (usb->address == 0xff) | |
297 | /* | |
298 | * First time we touch this device after FW | |
299 | * upload, so we don't know the address yet. | |
300 | */ | |
301 | usb->address = libusb_get_device_address(devlist[i]); | |
302 | } else { | |
303 | sr_err("Failed to open device: %s.", | |
304 | libusb_error_name(ret)); | |
305 | break; | |
306 | } | |
307 | ||
0c30b35f SY |
308 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); |
309 | if (ret == LIBUSB_ERROR_BUSY) { | |
310 | sr_err("Unable to claim USB interface. Another " | |
311 | "program or driver has already claimed it."); | |
312 | break; | |
313 | } else if (ret == LIBUSB_ERROR_NO_DEVICE) { | |
314 | sr_err("Device has been disconnected."); | |
315 | break; | |
316 | } else if (ret != 0) { | |
317 | sr_err("Unable to claim interface: %s.", | |
318 | libusb_error_name(ret)); | |
319 | break; | |
320 | } | |
321 | ||
96484e22 | 322 | if ((ret = logic16_init_device(sdi)) != SR_OK) { |
15abcf0f MC |
323 | sr_err("Failed to init device."); |
324 | break; | |
325 | } | |
326 | ||
5eea4305 | 327 | sdi->status = SR_ST_ACTIVE; |
ce7d3578 SA |
328 | sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.", |
329 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
5eea4305 MC |
330 | |
331 | break; | |
332 | } | |
333 | libusb_free_device_list(devlist, 1); | |
334 | ||
0c30b35f SY |
335 | if (sdi->status != SR_ST_ACTIVE) { |
336 | if (usb->devhdl) { | |
337 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
338 | libusb_close(usb->devhdl); | |
339 | usb->devhdl = NULL; | |
340 | } | |
5eea4305 | 341 | return SR_ERR; |
0c30b35f | 342 | } |
5eea4305 MC |
343 | |
344 | return SR_OK; | |
345 | } | |
346 | ||
c463dcf0 MC |
347 | static int dev_open(struct sr_dev_inst *sdi) |
348 | { | |
5eea4305 MC |
349 | struct dev_context *devc; |
350 | int ret; | |
351 | int64_t timediff_us, timediff_ms; | |
352 | ||
353 | devc = sdi->priv; | |
5eea4305 MC |
354 | |
355 | /* | |
356 | * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS | |
357 | * milliseconds for the FX2 to renumerate. | |
358 | */ | |
359 | ret = SR_ERR; | |
360 | if (devc->fw_updated > 0) { | |
361 | sr_info("Waiting for device to reset."); | |
362 | /* Takes >= 300ms for the FX2 to be gone from the USB bus. */ | |
363 | g_usleep(300 * 1000); | |
364 | timediff_ms = 0; | |
365 | while (timediff_ms < MAX_RENUM_DELAY_MS) { | |
366 | if ((ret = logic16_dev_open(sdi)) == SR_OK) | |
367 | break; | |
368 | g_usleep(100 * 1000); | |
369 | ||
370 | timediff_us = g_get_monotonic_time() - devc->fw_updated; | |
371 | timediff_ms = timediff_us / 1000; | |
372 | sr_spew("Waited %" PRIi64 "ms.", timediff_ms); | |
373 | } | |
374 | if (ret != SR_OK) { | |
375 | sr_err("Device failed to renumerate."); | |
376 | return SR_ERR; | |
377 | } | |
378 | sr_info("Device came back after %" PRIi64 "ms.", timediff_ms); | |
379 | } else { | |
380 | sr_info("Firmware upload was not needed."); | |
381 | ret = logic16_dev_open(sdi); | |
382 | } | |
383 | ||
384 | if (ret != SR_OK) { | |
385 | sr_err("Unable to open device."); | |
386 | return SR_ERR; | |
387 | } | |
388 | ||
5eea4305 MC |
389 | if (devc->cur_samplerate == 0) { |
390 | /* Samplerate hasn't been set; default to the slowest one. */ | |
7b5daad4 | 391 | devc->cur_samplerate = samplerates[0]; |
5eea4305 | 392 | } |
c463dcf0 MC |
393 | |
394 | return SR_OK; | |
395 | } | |
396 | ||
397 | static int dev_close(struct sr_dev_inst *sdi) | |
398 | { | |
5eea4305 | 399 | struct sr_usb_dev_inst *usb; |
c463dcf0 | 400 | |
5eea4305 | 401 | usb = sdi->conn; |
98fec29e | 402 | if (!usb->devhdl) |
5eea4305 | 403 | return SR_ERR; |
c463dcf0 | 404 | |
ce7d3578 SA |
405 | sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.", |
406 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
5eea4305 MC |
407 | libusb_release_interface(usb->devhdl, USB_INTERFACE); |
408 | libusb_close(usb->devhdl); | |
409 | usb->devhdl = NULL; | |
c463dcf0 MC |
410 | sdi->status = SR_ST_INACTIVE; |
411 | ||
412 | return SR_OK; | |
413 | } | |
414 | ||
4f840ce9 | 415 | static int cleanup(const struct sr_dev_driver *di) |
c463dcf0 | 416 | { |
f6a21fa5 MC |
417 | int ret; |
418 | struct drv_context *drvc; | |
c463dcf0 | 419 | |
41812aca | 420 | if (!(drvc = di->context)) |
f6a21fa5 MC |
421 | /* Can get called on an unused driver, doesn't matter. */ |
422 | return SR_OK; | |
c463dcf0 | 423 | |
a6630742 | 424 | ret = std_dev_clear(di, NULL); |
f6a21fa5 | 425 | g_free(drvc); |
f6a21fa5 MC |
426 | |
427 | return ret; | |
c463dcf0 MC |
428 | } |
429 | ||
584560f1 | 430 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 431 | const struct sr_channel_group *cg) |
c463dcf0 | 432 | { |
7b5daad4 | 433 | struct dev_context *devc; |
b117363a | 434 | struct sr_usb_dev_inst *usb; |
db11d7d2 | 435 | GVariant *range[2]; |
b117363a | 436 | char str[128]; |
c463dcf0 | 437 | int ret; |
96484e22 | 438 | unsigned int i; |
c463dcf0 | 439 | |
53b4680f | 440 | (void)cg; |
d3c74a6f | 441 | |
c463dcf0 MC |
442 | ret = SR_OK; |
443 | switch (key) { | |
b117363a MC |
444 | case SR_CONF_CONN: |
445 | if (!sdi || !sdi->conn) | |
446 | return SR_ERR_ARG; | |
447 | usb = sdi->conn; | |
448 | if (usb->address == 255) | |
449 | /* Device still needs to re-enumerate after firmware | |
450 | * upload, so we don't know its (future) address. */ | |
451 | return SR_ERR; | |
452 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
453 | *data = g_variant_new_string(str); | |
454 | break; | |
7b5daad4 MC |
455 | case SR_CONF_SAMPLERATE: |
456 | if (!sdi) | |
457 | return SR_ERR; | |
458 | devc = sdi->priv; | |
459 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
460 | break; | |
5a971f66 AJ |
461 | case SR_CONF_CAPTURE_RATIO: |
462 | if (!sdi) | |
463 | return SR_ERR; | |
464 | devc = sdi->priv; | |
465 | *data = g_variant_new_uint64(devc->capture_ratio); | |
466 | break; | |
db11d7d2 MC |
467 | case SR_CONF_VOLTAGE_THRESHOLD: |
468 | if (!sdi) | |
469 | return SR_ERR; | |
470 | devc = sdi->priv; | |
471 | ret = SR_ERR; | |
96484e22 UH |
472 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { |
473 | if (devc->selected_voltage_range != | |
474 | volt_thresholds[i].range) | |
475 | continue; | |
476 | range[0] = g_variant_new_double(volt_thresholds[i].low); | |
477 | range[1] = g_variant_new_double(volt_thresholds[i].high); | |
478 | *data = g_variant_new_tuple(range, 2); | |
479 | ret = SR_OK; | |
480 | break; | |
481 | } | |
db11d7d2 | 482 | break; |
c463dcf0 MC |
483 | default: |
484 | return SR_ERR_NA; | |
485 | } | |
486 | ||
487 | return ret; | |
488 | } | |
489 | ||
584560f1 | 490 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 491 | const struct sr_channel_group *cg) |
c463dcf0 | 492 | { |
7b5daad4 | 493 | struct dev_context *devc; |
db11d7d2 | 494 | gdouble low, high; |
c463dcf0 | 495 | int ret; |
96484e22 | 496 | unsigned int i; |
c463dcf0 | 497 | |
53b4680f | 498 | (void)cg; |
d3c74a6f | 499 | |
c463dcf0 MC |
500 | if (sdi->status != SR_ST_ACTIVE) |
501 | return SR_ERR_DEV_CLOSED; | |
502 | ||
7b5daad4 MC |
503 | devc = sdi->priv; |
504 | ||
c463dcf0 MC |
505 | ret = SR_OK; |
506 | switch (key) { | |
7b5daad4 MC |
507 | case SR_CONF_SAMPLERATE: |
508 | devc->cur_samplerate = g_variant_get_uint64(data); | |
509 | break; | |
510 | case SR_CONF_LIMIT_SAMPLES: | |
511 | devc->limit_samples = g_variant_get_uint64(data); | |
512 | break; | |
5a971f66 AJ |
513 | case SR_CONF_CAPTURE_RATIO: |
514 | devc->capture_ratio = g_variant_get_uint64(data); | |
515 | if (devc->capture_ratio > 100) { | |
516 | devc->capture_ratio = 0; | |
517 | ret = SR_ERR; | |
518 | } else | |
519 | ret = SR_OK; | |
520 | break; | |
db11d7d2 MC |
521 | case SR_CONF_VOLTAGE_THRESHOLD: |
522 | g_variant_get(data, "(dd)", &low, &high); | |
523 | ret = SR_ERR_ARG; | |
524 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { | |
525 | if (fabs(volt_thresholds[i].low - low) < 0.1 && | |
526 | fabs(volt_thresholds[i].high - high) < 0.1) { | |
527 | devc->selected_voltage_range = | |
528 | volt_thresholds[i].range; | |
529 | ret = SR_OK; | |
530 | break; | |
531 | } | |
532 | } | |
533 | break; | |
c463dcf0 MC |
534 | default: |
535 | ret = SR_ERR_NA; | |
536 | } | |
537 | ||
538 | return ret; | |
539 | } | |
540 | ||
584560f1 | 541 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 542 | const struct sr_channel_group *cg) |
c463dcf0 | 543 | { |
db11d7d2 | 544 | GVariant *gvar, *range[2]; |
7b5daad4 | 545 | GVariantBuilder gvb; |
c463dcf0 | 546 | int ret; |
96484e22 | 547 | unsigned int i; |
c463dcf0 MC |
548 | |
549 | (void)sdi; | |
53b4680f | 550 | (void)cg; |
c463dcf0 MC |
551 | |
552 | ret = SR_OK; | |
553 | switch (key) { | |
b117363a | 554 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 555 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
a0e0bb41 | 556 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
b117363a MC |
557 | break; |
558 | case SR_CONF_DEVICE_OPTIONS: | |
584560f1 | 559 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
f254bc4b | 560 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); |
b117363a | 561 | break; |
7b5daad4 MC |
562 | case SR_CONF_SAMPLERATE: |
563 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
96484e22 UH |
564 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), |
565 | samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
7b5daad4 MC |
566 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); |
567 | *data = g_variant_builder_end(&gvb); | |
db11d7d2 MC |
568 | break; |
569 | case SR_CONF_VOLTAGE_THRESHOLD: | |
570 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
571 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { | |
572 | range[0] = g_variant_new_double(volt_thresholds[i].low); | |
573 | range[1] = g_variant_new_double(volt_thresholds[i].high); | |
574 | gvar = g_variant_new_tuple(range, 2); | |
575 | g_variant_builder_add_value(&gvb, gvar); | |
576 | } | |
577 | *data = g_variant_builder_end(&gvb); | |
7b5daad4 | 578 | break; |
863357fb BV |
579 | case SR_CONF_TRIGGER_MATCH: |
580 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
581 | soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches), | |
582 | sizeof(int32_t)); | |
583 | break; | |
c463dcf0 MC |
584 | default: |
585 | return SR_ERR_NA; | |
586 | } | |
587 | ||
588 | return ret; | |
589 | } | |
590 | ||
7b5daad4 MC |
591 | static void abort_acquisition(struct dev_context *devc) |
592 | { | |
593 | int i; | |
594 | ||
863357fb | 595 | devc->sent_samples = -1; |
7b5daad4 MC |
596 | |
597 | for (i = devc->num_transfers - 1; i >= 0; i--) { | |
598 | if (devc->transfers[i]) | |
599 | libusb_cancel_transfer(devc->transfers[i]); | |
600 | } | |
601 | } | |
602 | ||
603 | static unsigned int bytes_per_ms(struct dev_context *devc) | |
604 | { | |
605 | return devc->cur_samplerate * devc->num_channels / 8000; | |
606 | } | |
607 | ||
608 | static size_t get_buffer_size(struct dev_context *devc) | |
609 | { | |
610 | size_t s; | |
611 | ||
612 | /* | |
613 | * The buffer should be large enough to hold 10ms of data and | |
614 | * a multiple of 512. | |
615 | */ | |
616 | s = 10 * bytes_per_ms(devc); | |
617 | return (s + 511) & ~511; | |
618 | } | |
619 | ||
620 | static unsigned int get_number_of_transfers(struct dev_context *devc) | |
621 | { | |
622 | unsigned int n; | |
623 | ||
624 | /* Total buffer size should be able to hold about 500ms of data. */ | |
625 | n = 500 * bytes_per_ms(devc) / get_buffer_size(devc); | |
626 | ||
627 | if (n > NUM_SIMUL_TRANSFERS) | |
628 | return NUM_SIMUL_TRANSFERS; | |
629 | ||
630 | return n; | |
631 | } | |
632 | ||
633 | static unsigned int get_timeout(struct dev_context *devc) | |
634 | { | |
635 | size_t total_size; | |
636 | unsigned int timeout; | |
637 | ||
638 | total_size = get_buffer_size(devc) * get_number_of_transfers(devc); | |
639 | timeout = total_size / bytes_per_ms(devc); | |
640 | return timeout + timeout / 4; /* Leave a headroom of 25% percent. */ | |
641 | } | |
642 | ||
ba7dd8bb | 643 | static int configure_channels(const struct sr_dev_inst *sdi) |
7b5daad4 MC |
644 | { |
645 | struct dev_context *devc; | |
ba7dd8bb | 646 | struct sr_channel *ch; |
7b5daad4 | 647 | GSList *l; |
ba7dd8bb | 648 | uint16_t channel_bit; |
7b5daad4 MC |
649 | |
650 | devc = sdi->priv; | |
651 | ||
652 | devc->cur_channels = 0; | |
653 | devc->num_channels = 0; | |
ba7dd8bb UH |
654 | for (l = sdi->channels; l; l = l->next) { |
655 | ch = (struct sr_channel *)l->data; | |
656 | if (ch->enabled == FALSE) | |
7b5daad4 MC |
657 | continue; |
658 | ||
ba7dd8bb | 659 | channel_bit = 1 << (ch->index); |
7b5daad4 | 660 | |
ba7dd8bb | 661 | devc->cur_channels |= channel_bit; |
7b5daad4 MC |
662 | |
663 | #ifdef WORDS_BIGENDIAN | |
96484e22 UH |
664 | /* |
665 | * Output logic data should be stored in little endian format. | |
666 | * To speed things up during conversion, do the switcharoo | |
667 | * here instead. | |
668 | */ | |
ba7dd8bb | 669 | channel_bit = 1 << (ch->index ^ 8); |
7b5daad4 MC |
670 | #endif |
671 | ||
ba7dd8bb | 672 | devc->channel_masks[devc->num_channels++] = channel_bit; |
7b5daad4 MC |
673 | } |
674 | ||
675 | return SR_OK; | |
676 | } | |
677 | ||
678 | static int receive_data(int fd, int revents, void *cb_data) | |
679 | { | |
680 | struct timeval tv; | |
681 | struct dev_context *devc; | |
682 | struct drv_context *drvc; | |
683 | const struct sr_dev_inst *sdi; | |
4f840ce9 | 684 | struct sr_dev_driver *di; |
7b5daad4 MC |
685 | |
686 | (void)fd; | |
687 | (void)revents; | |
688 | ||
689 | sdi = cb_data; | |
4f840ce9 | 690 | di = sdi->driver; |
41812aca | 691 | drvc = di->context; |
7b5daad4 MC |
692 | devc = sdi->priv; |
693 | ||
694 | tv.tv_sec = tv.tv_usec = 0; | |
695 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
696 | ||
863357fb | 697 | if (devc->sent_samples == -2) { |
96484e22 | 698 | logic16_abort_acquisition(sdi); |
7b5daad4 MC |
699 | abort_acquisition(devc); |
700 | } | |
701 | ||
702 | return TRUE; | |
703 | } | |
704 | ||
96484e22 | 705 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
c463dcf0 | 706 | { |
4f840ce9 | 707 | struct sr_dev_driver *di = sdi->driver; |
7b5daad4 MC |
708 | struct dev_context *devc; |
709 | struct drv_context *drvc; | |
710 | struct sr_usb_dev_inst *usb; | |
863357fb | 711 | struct sr_trigger *trigger; |
7b5daad4 | 712 | struct libusb_transfer *transfer; |
7b5daad4 MC |
713 | unsigned int i, timeout, num_transfers; |
714 | int ret; | |
715 | unsigned char *buf; | |
716 | size_t size, convsize; | |
c463dcf0 MC |
717 | |
718 | if (sdi->status != SR_ST_ACTIVE) | |
719 | return SR_ERR_DEV_CLOSED; | |
720 | ||
41812aca | 721 | drvc = di->context; |
7b5daad4 MC |
722 | devc = sdi->priv; |
723 | usb = sdi->conn; | |
724 | ||
96484e22 | 725 | /* Configures devc->cur_channels. */ |
ba7dd8bb UH |
726 | if (configure_channels(sdi) != SR_OK) { |
727 | sr_err("Failed to configure channels."); | |
7b5daad4 MC |
728 | return SR_ERR; |
729 | } | |
730 | ||
731 | devc->cb_data = cb_data; | |
863357fb | 732 | devc->sent_samples = 0; |
7b5daad4 MC |
733 | devc->empty_transfer_count = 0; |
734 | devc->cur_channel = 0; | |
735 | memset(devc->channel_data, 0, sizeof(devc->channel_data)); | |
736 | ||
0812c40e | 737 | if ((trigger = sr_session_trigger_get(sdi->session))) { |
5a971f66 AJ |
738 | int pre_trigger_samples = 0; |
739 | if (devc->limit_samples > 0) | |
740 | pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100; | |
741 | devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples); | |
98fec29e | 742 | if (!devc->stl) |
5a971f66 | 743 | return SR_ERR_MALLOC; |
863357fb BV |
744 | devc->trigger_fired = FALSE; |
745 | } else | |
746 | devc->trigger_fired = TRUE; | |
747 | ||
7b5daad4 MC |
748 | timeout = get_timeout(devc); |
749 | num_transfers = get_number_of_transfers(devc); | |
750 | size = get_buffer_size(devc); | |
751 | convsize = (size / devc->num_channels + 2) * 16; | |
752 | devc->submitted_transfers = 0; | |
7b5daad4 MC |
753 | |
754 | devc->convbuffer_size = convsize; | |
755 | if (!(devc->convbuffer = g_try_malloc(convsize))) { | |
756 | sr_err("Conversion buffer malloc failed."); | |
757 | return SR_ERR_MALLOC; | |
758 | } | |
759 | ||
760 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); | |
761 | if (!devc->transfers) { | |
762 | sr_err("USB transfers malloc failed."); | |
763 | g_free(devc->convbuffer); | |
764 | return SR_ERR_MALLOC; | |
765 | } | |
766 | ||
96484e22 UH |
767 | if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate, |
768 | devc->cur_channels)) != SR_OK) { | |
7b5daad4 MC |
769 | g_free(devc->transfers); |
770 | g_free(devc->convbuffer); | |
771 | return ret; | |
772 | } | |
773 | ||
774 | devc->num_transfers = num_transfers; | |
775 | for (i = 0; i < num_transfers; i++) { | |
776 | if (!(buf = g_try_malloc(size))) { | |
777 | sr_err("USB transfer buffer malloc failed."); | |
778 | if (devc->submitted_transfers) | |
779 | abort_acquisition(devc); | |
780 | else { | |
781 | g_free(devc->transfers); | |
782 | g_free(devc->convbuffer); | |
783 | } | |
784 | return SR_ERR_MALLOC; | |
785 | } | |
786 | transfer = libusb_alloc_transfer(0); | |
787 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
788 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
102f1239 | 789 | logic16_receive_transfer, (void *)sdi, timeout); |
7b5daad4 MC |
790 | if ((ret = libusb_submit_transfer(transfer)) != 0) { |
791 | sr_err("Failed to submit transfer: %s.", | |
792 | libusb_error_name(ret)); | |
793 | libusb_free_transfer(transfer); | |
794 | g_free(buf); | |
795 | abort_acquisition(devc); | |
796 | return SR_ERR; | |
797 | } | |
798 | devc->transfers[i] = transfer; | |
799 | devc->submitted_transfers++; | |
800 | } | |
801 | ||
6c60facc ML |
802 | devc->ctx = drvc->sr_ctx; |
803 | ||
102f1239 | 804 | usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi); |
7b5daad4 MC |
805 | |
806 | /* Send header packet to the session bus. */ | |
807 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
808 | ||
96484e22 | 809 | if ((ret = logic16_start_acquisition(sdi)) != SR_OK) { |
7b5daad4 MC |
810 | abort_acquisition(devc); |
811 | return ret; | |
812 | } | |
c463dcf0 MC |
813 | |
814 | return SR_OK; | |
815 | } | |
816 | ||
817 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
818 | { | |
7b5daad4 MC |
819 | int ret; |
820 | ||
c463dcf0 MC |
821 | (void)cb_data; |
822 | ||
823 | if (sdi->status != SR_ST_ACTIVE) | |
824 | return SR_ERR_DEV_CLOSED; | |
825 | ||
96484e22 | 826 | ret = logic16_abort_acquisition(sdi); |
c463dcf0 | 827 | |
7b5daad4 MC |
828 | abort_acquisition(sdi->priv); |
829 | ||
830 | return ret; | |
c463dcf0 MC |
831 | } |
832 | ||
833 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = { | |
834 | .name = "saleae-logic16", | |
835 | .longname = "Saleae Logic16", | |
836 | .api_version = 1, | |
837 | .init = init, | |
838 | .cleanup = cleanup, | |
839 | .scan = scan, | |
840 | .dev_list = dev_list, | |
a6630742 | 841 | .dev_clear = NULL, |
c463dcf0 MC |
842 | .config_get = config_get, |
843 | .config_set = config_set, | |
844 | .config_list = config_list, | |
845 | .dev_open = dev_open, | |
846 | .dev_close = dev_close, | |
847 | .dev_acquisition_start = dev_acquisition_start, | |
848 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 849 | .context = NULL, |
c463dcf0 | 850 | }; |