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