]>
Commit | Line | Data |
---|---|---|
c463dcf0 MC |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se> | |
fec7aa6a MC |
5 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> |
6 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
c463dcf0 MC |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
f6a21fa5 MC |
22 | #include <glib.h> |
23 | #include <libusb.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
db11d7d2 | 26 | #include <math.h> |
f6a21fa5 MC |
27 | #include "libsigrok.h" |
28 | #include "libsigrok-internal.h" | |
c463dcf0 MC |
29 | #include "protocol.h" |
30 | ||
f6a21fa5 MC |
31 | #define LOGIC16_VID 0x21a9 |
32 | #define LOGIC16_PID 0x1001 | |
33 | #define NUM_PROBES 16 | |
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 MC |
41 | |
42 | ||
c463dcf0 MC |
43 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info; |
44 | static struct sr_dev_driver *di = &saleae_logic16_driver_info; | |
45 | ||
b117363a MC |
46 | static const int32_t hwopts[] = { |
47 | SR_CONF_CONN, | |
48 | }; | |
49 | ||
50 | static const int32_t hwcaps[] = { | |
51 | SR_CONF_LOGIC_ANALYZER, | |
52 | SR_CONF_SAMPLERATE, | |
db11d7d2 | 53 | SR_CONF_VOLTAGE_THRESHOLD, |
b117363a MC |
54 | |
55 | /* These are really implemented in the driver, not the hardware. */ | |
56 | SR_CONF_LIMIT_SAMPLES, | |
57 | SR_CONF_CONTINUOUS, | |
58 | }; | |
59 | ||
f6a21fa5 MC |
60 | static const char *probe_names[NUM_PROBES + 1] = { |
61 | "0", "1", "2", "3", "4", "5", "6", "7", "8", | |
62 | "9", "10", "11", "12", "13", "14", "15", | |
63 | NULL, | |
64 | }; | |
c463dcf0 | 65 | |
db11d7d2 MC |
66 | static const struct { |
67 | enum voltage_range range; | |
68 | gdouble low; | |
69 | gdouble high; | |
70 | } volt_thresholds[] = { | |
71 | { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 }, | |
72 | { VOLTAGE_RANGE_5_V, 1.4, 3.6 }, | |
73 | }; | |
74 | ||
7b5daad4 MC |
75 | static const uint64_t samplerates[] = { |
76 | SR_KHZ(500), | |
77 | SR_MHZ(1), | |
78 | SR_MHZ(2), | |
79 | SR_MHZ(4), | |
80 | SR_MHZ(5), | |
81 | SR_MHZ(8), | |
82 | SR_MHZ(10), | |
83 | SR_KHZ(12500), | |
84 | SR_MHZ(16), | |
85 | SR_MHZ(25), | |
86 | SR_MHZ(32), | |
87 | SR_MHZ(40), | |
88 | SR_MHZ(80), | |
89 | SR_MHZ(100), | |
90 | }; | |
91 | ||
c463dcf0 MC |
92 | static int init(struct sr_context *sr_ctx) |
93 | { | |
94 | return std_init(sr_ctx, di, LOG_PREFIX); | |
95 | } | |
96 | ||
5eea4305 MC |
97 | static gboolean check_conf_profile(libusb_device *dev) |
98 | { | |
99 | struct libusb_device_descriptor des; | |
100 | struct libusb_device_handle *hdl; | |
101 | gboolean ret; | |
102 | unsigned char strdesc[64]; | |
103 | ||
104 | hdl = NULL; | |
105 | ret = FALSE; | |
106 | while (!ret) { | |
107 | /* Assume the FW has not been loaded, unless proven wrong. */ | |
108 | if (libusb_get_device_descriptor(dev, &des) != 0) | |
109 | break; | |
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, | |
121 | des.iProduct, strdesc, sizeof(strdesc)) < 0) | |
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 | ||
c463dcf0 MC |
135 | static GSList *scan(GSList *options) |
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; |
f6a21fa5 | 141 | struct sr_probe *probe; |
5eea4305 MC |
142 | struct sr_config *src; |
143 | GSList *l, *devices, *conn_devices; | |
f6a21fa5 MC |
144 | struct libusb_device_descriptor des; |
145 | libusb_device **devlist; | |
5eea4305 MC |
146 | int devcnt, ret, i, j; |
147 | const char *conn; | |
c463dcf0 | 148 | |
c463dcf0 | 149 | drvc = di->priv; |
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 | ||
183 | if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) { | |
184 | sr_warn("Failed to get device descriptor: %s.", | |
185 | libusb_error_name(ret)); | |
f6a21fa5 MC |
186 | continue; |
187 | } | |
188 | ||
189 | if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) | |
190 | continue; | |
191 | ||
192 | devcnt = g_slist_length(drvc->instances); | |
5eea4305 MC |
193 | sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING, |
194 | "Saleae", "Logic16", NULL); | |
195 | if (!sdi) | |
f6a21fa5 MC |
196 | return NULL; |
197 | sdi->driver = di; | |
198 | ||
f6a21fa5 MC |
199 | for (j = 0; probe_names[j]; j++) { |
200 | if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE, | |
201 | probe_names[j]))) | |
202 | return NULL; | |
203 | sdi->probes = g_slist_append(sdi->probes, probe); | |
204 | } | |
205 | ||
5eea4305 | 206 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) |
f6a21fa5 | 207 | return NULL; |
db11d7d2 | 208 | devc->selected_voltage_range = VOLTAGE_RANGE_18_33_V; |
5eea4305 | 209 | sdi->priv = devc; |
f6a21fa5 MC |
210 | drvc->instances = g_slist_append(drvc->instances, sdi); |
211 | devices = g_slist_append(devices, sdi); | |
5eea4305 MC |
212 | |
213 | if (check_conf_profile(devlist[i])) { | |
214 | /* Already has the firmware, so fix the new address. */ | |
215 | sr_dbg("Found a Logic16 device."); | |
216 | sdi->status = SR_ST_INACTIVE; | |
217 | sdi->inst_type = SR_INST_USB; | |
218 | sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), | |
219 | libusb_get_device_address(devlist[i]), NULL); | |
220 | } else { | |
221 | if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, | |
222 | FX2_FIRMWARE) == SR_OK) | |
223 | /* Store when this device's FW was updated. */ | |
224 | devc->fw_updated = g_get_monotonic_time(); | |
225 | else | |
226 | sr_err("Firmware upload failed for " | |
227 | "device %d.", devcnt); | |
228 | sdi->inst_type = SR_INST_USB; | |
229 | sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]), | |
230 | 0xff, NULL); | |
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 MC |
235 | |
236 | return devices; | |
237 | } | |
238 | ||
239 | static GSList *dev_list(void) | |
240 | { | |
241 | struct drv_context *drvc; | |
242 | ||
243 | drvc = di->priv; | |
244 | ||
245 | return drvc->instances; | |
246 | } | |
247 | ||
248 | static int dev_clear(void) | |
249 | { | |
250 | return std_dev_clear(di, NULL); | |
251 | } | |
252 | ||
5eea4305 MC |
253 | static int logic16_dev_open(struct sr_dev_inst *sdi) |
254 | { | |
255 | libusb_device **devlist; | |
256 | struct sr_usb_dev_inst *usb; | |
257 | struct libusb_device_descriptor des; | |
5eea4305 MC |
258 | struct drv_context *drvc; |
259 | int ret, skip, i, device_count; | |
260 | ||
261 | drvc = di->priv; | |
5eea4305 MC |
262 | usb = sdi->conn; |
263 | ||
264 | if (sdi->status == SR_ST_ACTIVE) | |
265 | /* Device is already in use. */ | |
266 | return SR_ERR; | |
267 | ||
268 | skip = 0; | |
269 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
270 | if (device_count < 0) { | |
271 | sr_err("Failed to get device list: %s.", | |
272 | libusb_error_name(device_count)); | |
273 | return SR_ERR; | |
274 | } | |
275 | ||
276 | for (i = 0; i < device_count; i++) { | |
277 | if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { | |
278 | sr_err("Failed to get device descriptor: %s.", | |
279 | libusb_error_name(ret)); | |
280 | continue; | |
281 | } | |
282 | ||
283 | if (des.idVendor != LOGIC16_VID | |
284 | || des.idProduct != LOGIC16_PID) | |
285 | continue; | |
286 | ||
287 | if (sdi->status == SR_ST_INITIALIZING) { | |
288 | if (skip != sdi->index) { | |
289 | /* Skip devices of this type that aren't the one we want. */ | |
290 | skip += 1; | |
291 | continue; | |
292 | } | |
293 | } else if (sdi->status == SR_ST_INACTIVE) { | |
294 | /* | |
295 | * This device is fully enumerated, so we need to find | |
296 | * this device by vendor, product, bus and address. | |
297 | */ | |
298 | if (libusb_get_bus_number(devlist[i]) != usb->bus | |
299 | || libusb_get_device_address(devlist[i]) != usb->address) | |
300 | /* This is not the one. */ | |
301 | continue; | |
302 | } | |
303 | ||
304 | if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { | |
305 | if (usb->address == 0xff) | |
306 | /* | |
307 | * First time we touch this device after FW | |
308 | * upload, so we don't know the address yet. | |
309 | */ | |
310 | usb->address = libusb_get_device_address(devlist[i]); | |
311 | } else { | |
312 | sr_err("Failed to open device: %s.", | |
313 | libusb_error_name(ret)); | |
314 | break; | |
315 | } | |
316 | ||
15abcf0f MC |
317 | if ((ret = saleae_logic16_init_device(sdi)) != SR_OK) { |
318 | sr_err("Failed to init device."); | |
319 | break; | |
320 | } | |
321 | ||
5eea4305 MC |
322 | sdi->status = SR_ST_ACTIVE; |
323 | sr_info("Opened device %d on %d.%d, " | |
324 | "interface %d.", | |
325 | sdi->index, usb->bus, usb->address, | |
326 | USB_INTERFACE); | |
327 | ||
328 | break; | |
329 | } | |
330 | libusb_free_device_list(devlist, 1); | |
331 | ||
332 | if (sdi->status != SR_ST_ACTIVE) | |
333 | return SR_ERR; | |
334 | ||
335 | return SR_OK; | |
336 | } | |
337 | ||
c463dcf0 MC |
338 | static int dev_open(struct sr_dev_inst *sdi) |
339 | { | |
5eea4305 MC |
340 | struct sr_usb_dev_inst *usb; |
341 | struct dev_context *devc; | |
342 | int ret; | |
343 | int64_t timediff_us, timediff_ms; | |
344 | ||
345 | devc = sdi->priv; | |
346 | usb = sdi->conn; | |
347 | ||
348 | /* | |
349 | * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS | |
350 | * milliseconds for the FX2 to renumerate. | |
351 | */ | |
352 | ret = SR_ERR; | |
353 | if (devc->fw_updated > 0) { | |
354 | sr_info("Waiting for device to reset."); | |
355 | /* Takes >= 300ms for the FX2 to be gone from the USB bus. */ | |
356 | g_usleep(300 * 1000); | |
357 | timediff_ms = 0; | |
358 | while (timediff_ms < MAX_RENUM_DELAY_MS) { | |
359 | if ((ret = logic16_dev_open(sdi)) == SR_OK) | |
360 | break; | |
361 | g_usleep(100 * 1000); | |
362 | ||
363 | timediff_us = g_get_monotonic_time() - devc->fw_updated; | |
364 | timediff_ms = timediff_us / 1000; | |
365 | sr_spew("Waited %" PRIi64 "ms.", timediff_ms); | |
366 | } | |
367 | if (ret != SR_OK) { | |
368 | sr_err("Device failed to renumerate."); | |
369 | return SR_ERR; | |
370 | } | |
371 | sr_info("Device came back after %" PRIi64 "ms.", timediff_ms); | |
372 | } else { | |
373 | sr_info("Firmware upload was not needed."); | |
374 | ret = logic16_dev_open(sdi); | |
375 | } | |
376 | ||
377 | if (ret != SR_OK) { | |
378 | sr_err("Unable to open device."); | |
379 | return SR_ERR; | |
380 | } | |
381 | ||
382 | ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); | |
383 | if (ret != 0) { | |
384 | switch(ret) { | |
385 | case LIBUSB_ERROR_BUSY: | |
386 | sr_err("Unable to claim USB interface. Another " | |
387 | "program or driver has already claimed it."); | |
388 | break; | |
389 | case LIBUSB_ERROR_NO_DEVICE: | |
390 | sr_err("Device has been disconnected."); | |
391 | break; | |
392 | default: | |
393 | sr_err("Unable to claim interface: %s.", | |
394 | libusb_error_name(ret)); | |
395 | break; | |
396 | } | |
c463dcf0 | 397 | |
5eea4305 MC |
398 | return SR_ERR; |
399 | } | |
c463dcf0 | 400 | |
5eea4305 MC |
401 | if (devc->cur_samplerate == 0) { |
402 | /* Samplerate hasn't been set; default to the slowest one. */ | |
7b5daad4 | 403 | devc->cur_samplerate = samplerates[0]; |
5eea4305 | 404 | } |
c463dcf0 MC |
405 | |
406 | return SR_OK; | |
407 | } | |
408 | ||
409 | static int dev_close(struct sr_dev_inst *sdi) | |
410 | { | |
5eea4305 | 411 | struct sr_usb_dev_inst *usb; |
c463dcf0 | 412 | |
5eea4305 MC |
413 | usb = sdi->conn; |
414 | if (usb->devhdl == NULL) | |
415 | return SR_ERR; | |
c463dcf0 | 416 | |
5eea4305 MC |
417 | sr_info("Closing device %d on %d.%d interface %d.", |
418 | sdi->index, usb->bus, usb->address, USB_INTERFACE); | |
419 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
420 | libusb_close(usb->devhdl); | |
421 | usb->devhdl = NULL; | |
c463dcf0 MC |
422 | sdi->status = SR_ST_INACTIVE; |
423 | ||
424 | return SR_OK; | |
425 | } | |
426 | ||
427 | static int cleanup(void) | |
428 | { | |
f6a21fa5 MC |
429 | int ret; |
430 | struct drv_context *drvc; | |
c463dcf0 | 431 | |
f6a21fa5 MC |
432 | if (!(drvc = di->priv)) |
433 | /* Can get called on an unused driver, doesn't matter. */ | |
434 | return SR_OK; | |
c463dcf0 | 435 | |
f6a21fa5 MC |
436 | ret = dev_clear(); |
437 | g_free(drvc); | |
438 | di->priv = NULL; | |
439 | ||
440 | return ret; | |
c463dcf0 MC |
441 | } |
442 | ||
443 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi) | |
444 | { | |
7b5daad4 | 445 | struct dev_context *devc; |
b117363a | 446 | struct sr_usb_dev_inst *usb; |
db11d7d2 | 447 | GVariant *range[2]; |
b117363a | 448 | char str[128]; |
c463dcf0 | 449 | int ret; |
db11d7d2 | 450 | unsigned i; |
c463dcf0 | 451 | |
c463dcf0 MC |
452 | ret = SR_OK; |
453 | switch (key) { | |
b117363a MC |
454 | case SR_CONF_CONN: |
455 | if (!sdi || !sdi->conn) | |
456 | return SR_ERR_ARG; | |
457 | usb = sdi->conn; | |
458 | if (usb->address == 255) | |
459 | /* Device still needs to re-enumerate after firmware | |
460 | * upload, so we don't know its (future) address. */ | |
461 | return SR_ERR; | |
462 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
463 | *data = g_variant_new_string(str); | |
464 | break; | |
7b5daad4 MC |
465 | case SR_CONF_SAMPLERATE: |
466 | if (!sdi) | |
467 | return SR_ERR; | |
468 | devc = sdi->priv; | |
469 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
470 | break; | |
db11d7d2 MC |
471 | case SR_CONF_VOLTAGE_THRESHOLD: |
472 | if (!sdi) | |
473 | return SR_ERR; | |
474 | devc = sdi->priv; | |
475 | ret = SR_ERR; | |
476 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) | |
477 | if (devc->selected_voltage_range == | |
478 | volt_thresholds[i].range) { | |
479 | range[0] = g_variant_new_double(volt_thresholds[i].low); | |
480 | range[1] = g_variant_new_double(volt_thresholds[i].high); | |
481 | *data = g_variant_new_tuple(range, 2); | |
482 | ret = SR_OK; | |
483 | break; | |
484 | } | |
485 | break; | |
c463dcf0 MC |
486 | default: |
487 | return SR_ERR_NA; | |
488 | } | |
489 | ||
490 | return ret; | |
491 | } | |
492 | ||
493 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi) | |
494 | { | |
7b5daad4 | 495 | struct dev_context *devc; |
db11d7d2 | 496 | gdouble low, high; |
c463dcf0 | 497 | int ret; |
db11d7d2 | 498 | unsigned i; |
c463dcf0 | 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; | |
db11d7d2 MC |
513 | case SR_CONF_VOLTAGE_THRESHOLD: |
514 | g_variant_get(data, "(dd)", &low, &high); | |
515 | ret = SR_ERR_ARG; | |
516 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { | |
517 | if (fabs(volt_thresholds[i].low - low) < 0.1 && | |
518 | fabs(volt_thresholds[i].high - high) < 0.1) { | |
519 | devc->selected_voltage_range = | |
520 | volt_thresholds[i].range; | |
521 | ret = SR_OK; | |
522 | break; | |
523 | } | |
524 | } | |
525 | break; | |
c463dcf0 MC |
526 | default: |
527 | ret = SR_ERR_NA; | |
528 | } | |
529 | ||
530 | return ret; | |
531 | } | |
532 | ||
533 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) | |
534 | { | |
db11d7d2 | 535 | GVariant *gvar, *range[2]; |
7b5daad4 | 536 | GVariantBuilder gvb; |
c463dcf0 | 537 | int ret; |
db11d7d2 | 538 | unsigned i; |
c463dcf0 MC |
539 | |
540 | (void)sdi; | |
c463dcf0 MC |
541 | |
542 | ret = SR_OK; | |
543 | switch (key) { | |
b117363a MC |
544 | case SR_CONF_SCAN_OPTIONS: |
545 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
546 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
547 | break; | |
548 | case SR_CONF_DEVICE_OPTIONS: | |
549 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
550 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
551 | break; | |
7b5daad4 MC |
552 | case SR_CONF_SAMPLERATE: |
553 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
554 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates, | |
555 | ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
556 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
557 | *data = g_variant_builder_end(&gvb); | |
db11d7d2 MC |
558 | break; |
559 | case SR_CONF_VOLTAGE_THRESHOLD: | |
560 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
561 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { | |
562 | range[0] = g_variant_new_double(volt_thresholds[i].low); | |
563 | range[1] = g_variant_new_double(volt_thresholds[i].high); | |
564 | gvar = g_variant_new_tuple(range, 2); | |
565 | g_variant_builder_add_value(&gvb, gvar); | |
566 | } | |
567 | *data = g_variant_builder_end(&gvb); | |
7b5daad4 | 568 | break; |
c463dcf0 MC |
569 | default: |
570 | return SR_ERR_NA; | |
571 | } | |
572 | ||
573 | return ret; | |
574 | } | |
575 | ||
7b5daad4 MC |
576 | static void abort_acquisition(struct dev_context *devc) |
577 | { | |
578 | int i; | |
579 | ||
580 | devc->num_samples = -1; | |
581 | ||
582 | for (i = devc->num_transfers - 1; i >= 0; i--) { | |
583 | if (devc->transfers[i]) | |
584 | libusb_cancel_transfer(devc->transfers[i]); | |
585 | } | |
586 | } | |
587 | ||
588 | static unsigned int bytes_per_ms(struct dev_context *devc) | |
589 | { | |
590 | return devc->cur_samplerate * devc->num_channels / 8000; | |
591 | } | |
592 | ||
593 | static size_t get_buffer_size(struct dev_context *devc) | |
594 | { | |
595 | size_t s; | |
596 | ||
597 | /* | |
598 | * The buffer should be large enough to hold 10ms of data and | |
599 | * a multiple of 512. | |
600 | */ | |
601 | s = 10 * bytes_per_ms(devc); | |
602 | return (s + 511) & ~511; | |
603 | } | |
604 | ||
605 | static unsigned int get_number_of_transfers(struct dev_context *devc) | |
606 | { | |
607 | unsigned int n; | |
608 | ||
609 | /* Total buffer size should be able to hold about 500ms of data. */ | |
610 | n = 500 * bytes_per_ms(devc) / get_buffer_size(devc); | |
611 | ||
612 | if (n > NUM_SIMUL_TRANSFERS) | |
613 | return NUM_SIMUL_TRANSFERS; | |
614 | ||
615 | return n; | |
616 | } | |
617 | ||
618 | static unsigned int get_timeout(struct dev_context *devc) | |
619 | { | |
620 | size_t total_size; | |
621 | unsigned int timeout; | |
622 | ||
623 | total_size = get_buffer_size(devc) * get_number_of_transfers(devc); | |
624 | timeout = total_size / bytes_per_ms(devc); | |
625 | return timeout + timeout / 4; /* Leave a headroom of 25% percent. */ | |
626 | } | |
627 | ||
628 | static int configure_probes(const struct sr_dev_inst *sdi) | |
629 | { | |
630 | struct dev_context *devc; | |
631 | struct sr_probe *probe; | |
632 | GSList *l; | |
633 | uint16_t probe_bit; | |
634 | ||
635 | devc = sdi->priv; | |
636 | ||
637 | devc->cur_channels = 0; | |
638 | devc->num_channels = 0; | |
639 | for (l = sdi->probes; l; l = l->next) { | |
640 | probe = (struct sr_probe *)l->data; | |
641 | if (probe->enabled == FALSE) | |
642 | continue; | |
643 | ||
644 | probe_bit = 1 << (probe->index); | |
645 | ||
646 | devc->cur_channels |= probe_bit; | |
647 | ||
648 | #ifdef WORDS_BIGENDIAN | |
649 | /* Output logic data should be stored in little endian | |
650 | format. To speed things up during conversion, do the | |
651 | switcharoo here instead. */ | |
652 | ||
653 | probe_bit = 1 << (probe->index ^ 8); | |
654 | #endif | |
655 | ||
656 | devc->channel_masks[devc->num_channels ++] = probe_bit; | |
657 | } | |
658 | ||
659 | return SR_OK; | |
660 | } | |
661 | ||
662 | static int receive_data(int fd, int revents, void *cb_data) | |
663 | { | |
664 | struct timeval tv; | |
665 | struct dev_context *devc; | |
666 | struct drv_context *drvc; | |
667 | const struct sr_dev_inst *sdi; | |
668 | ||
669 | (void)fd; | |
670 | (void)revents; | |
671 | ||
672 | sdi = cb_data; | |
673 | drvc = di->priv; | |
674 | devc = sdi->priv; | |
675 | ||
676 | tv.tv_sec = tv.tv_usec = 0; | |
677 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
678 | ||
679 | if (devc->num_samples == -2) { | |
680 | saleae_logic16_abort_acquisition(sdi); | |
681 | abort_acquisition(devc); | |
682 | } | |
683 | ||
684 | return TRUE; | |
685 | } | |
686 | ||
c463dcf0 | 687 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, |
7b5daad4 | 688 | void *cb_data) |
c463dcf0 | 689 | { |
7b5daad4 MC |
690 | struct dev_context *devc; |
691 | struct drv_context *drvc; | |
692 | struct sr_usb_dev_inst *usb; | |
693 | struct libusb_transfer *transfer; | |
694 | const struct libusb_pollfd **lupfd; | |
695 | unsigned int i, timeout, num_transfers; | |
696 | int ret; | |
697 | unsigned char *buf; | |
698 | size_t size, convsize; | |
c463dcf0 MC |
699 | |
700 | if (sdi->status != SR_ST_ACTIVE) | |
701 | return SR_ERR_DEV_CLOSED; | |
702 | ||
7b5daad4 MC |
703 | drvc = di->priv; |
704 | devc = sdi->priv; | |
705 | usb = sdi->conn; | |
706 | ||
707 | /* Configures devc->cur_channels */ | |
708 | if (configure_probes(sdi) != SR_OK) { | |
709 | sr_err("Failed to configure probes."); | |
710 | return SR_ERR; | |
711 | } | |
712 | ||
713 | devc->cb_data = cb_data; | |
714 | devc->num_samples = 0; | |
715 | devc->empty_transfer_count = 0; | |
716 | devc->cur_channel = 0; | |
717 | memset(devc->channel_data, 0, sizeof(devc->channel_data)); | |
718 | ||
719 | timeout = get_timeout(devc); | |
720 | num_transfers = get_number_of_transfers(devc); | |
721 | size = get_buffer_size(devc); | |
722 | convsize = (size / devc->num_channels + 2) * 16; | |
723 | devc->submitted_transfers = 0; | |
724 | devc->usbfd = NULL; | |
725 | ||
726 | devc->convbuffer_size = convsize; | |
727 | if (!(devc->convbuffer = g_try_malloc(convsize))) { | |
728 | sr_err("Conversion buffer malloc failed."); | |
729 | return SR_ERR_MALLOC; | |
730 | } | |
731 | ||
732 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); | |
733 | if (!devc->transfers) { | |
734 | sr_err("USB transfers malloc failed."); | |
735 | g_free(devc->convbuffer); | |
736 | return SR_ERR_MALLOC; | |
737 | } | |
738 | ||
739 | if ((ret = saleae_logic16_setup_acquisition(sdi, devc->cur_samplerate, | |
740 | devc->cur_channels)) != SR_OK) { | |
741 | g_free(devc->transfers); | |
742 | g_free(devc->convbuffer); | |
743 | return ret; | |
744 | } | |
745 | ||
746 | devc->num_transfers = num_transfers; | |
747 | for (i = 0; i < num_transfers; i++) { | |
748 | if (!(buf = g_try_malloc(size))) { | |
749 | sr_err("USB transfer buffer malloc failed."); | |
750 | if (devc->submitted_transfers) | |
751 | abort_acquisition(devc); | |
752 | else { | |
753 | g_free(devc->transfers); | |
754 | g_free(devc->convbuffer); | |
755 | } | |
756 | return SR_ERR_MALLOC; | |
757 | } | |
758 | transfer = libusb_alloc_transfer(0); | |
759 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
760 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
761 | saleae_logic16_receive_transfer, devc, timeout); | |
762 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
763 | sr_err("Failed to submit transfer: %s.", | |
764 | libusb_error_name(ret)); | |
765 | libusb_free_transfer(transfer); | |
766 | g_free(buf); | |
767 | abort_acquisition(devc); | |
768 | return SR_ERR; | |
769 | } | |
770 | devc->transfers[i] = transfer; | |
771 | devc->submitted_transfers++; | |
772 | } | |
773 | ||
774 | lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx); | |
775 | for (i = 0; lupfd[i]; i++); | |
776 | if (!(devc->usbfd = g_try_malloc(sizeof(struct libusb_pollfd) * (i + 1)))) { | |
777 | abort_acquisition(devc); | |
778 | free(lupfd); | |
779 | return SR_ERR; | |
780 | } | |
781 | for (i = 0; lupfd[i]; i++) { | |
782 | sr_source_add(lupfd[i]->fd, lupfd[i]->events, | |
783 | timeout, receive_data, (void *)sdi); | |
784 | devc->usbfd[i] = lupfd[i]->fd; | |
785 | } | |
786 | devc->usbfd[i] = -1; | |
787 | free(lupfd); | |
788 | ||
789 | /* Send header packet to the session bus. */ | |
790 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
791 | ||
792 | if ((ret = saleae_logic16_start_acquisition(sdi)) != SR_OK) { | |
793 | abort_acquisition(devc); | |
794 | return ret; | |
795 | } | |
c463dcf0 MC |
796 | |
797 | return SR_OK; | |
798 | } | |
799 | ||
800 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
801 | { | |
7b5daad4 MC |
802 | int ret; |
803 | ||
c463dcf0 MC |
804 | (void)cb_data; |
805 | ||
806 | if (sdi->status != SR_ST_ACTIVE) | |
807 | return SR_ERR_DEV_CLOSED; | |
808 | ||
7b5daad4 | 809 | ret = saleae_logic16_abort_acquisition(sdi); |
c463dcf0 | 810 | |
7b5daad4 MC |
811 | abort_acquisition(sdi->priv); |
812 | ||
813 | return ret; | |
c463dcf0 MC |
814 | } |
815 | ||
816 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = { | |
817 | .name = "saleae-logic16", | |
818 | .longname = "Saleae Logic16", | |
819 | .api_version = 1, | |
820 | .init = init, | |
821 | .cleanup = cleanup, | |
822 | .scan = scan, | |
823 | .dev_list = dev_list, | |
824 | .dev_clear = dev_clear, | |
825 | .config_get = config_get, | |
826 | .config_set = config_set, | |
827 | .config_list = config_list, | |
828 | .dev_open = dev_open, | |
829 | .dev_close = dev_close, | |
830 | .dev_acquisition_start = dev_acquisition_start, | |
831 | .dev_acquisition_stop = dev_acquisition_stop, | |
832 | .priv = NULL, | |
833 | }; |