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