]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se> | |
5 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
6 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
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 | ||
22 | #include <glib.h> | |
23 | #include <libusb.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <math.h> | |
27 | #include "libsigrok.h" | |
28 | #include "libsigrok-internal.h" | |
29 | #include "protocol.h" | |
30 | ||
31 | #define LOGIC16_VID 0x21a9 | |
32 | #define LOGIC16_PID 0x1001 | |
33 | ||
34 | #define USB_INTERFACE 0 | |
35 | #define USB_CONFIGURATION 1 | |
36 | #define FX2_FIRMWARE FIRMWARE_DIR "/saleae-logic16-fx2.fw" | |
37 | ||
38 | #define MAX_RENUM_DELAY_MS 3000 | |
39 | #define NUM_SIMUL_TRANSFERS 32 | |
40 | ||
41 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info; | |
42 | static struct sr_dev_driver *di = &saleae_logic16_driver_info; | |
43 | ||
44 | static const uint32_t scanopts[] = { | |
45 | SR_CONF_CONN, | |
46 | }; | |
47 | ||
48 | static const uint32_t devopts[] = { | |
49 | SR_CONF_LOGIC_ANALYZER, | |
50 | SR_CONF_CONTINUOUS, | |
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, | |
56 | }; | |
57 | ||
58 | static const int32_t soft_trigger_matches[] = { | |
59 | SR_TRIGGER_ZERO, | |
60 | SR_TRIGGER_ONE, | |
61 | SR_TRIGGER_RISING, | |
62 | SR_TRIGGER_FALLING, | |
63 | SR_TRIGGER_EDGE, | |
64 | }; | |
65 | ||
66 | static const char *channel_names[] = { | |
67 | "0", "1", "2", "3", "4", "5", "6", "7", "8", | |
68 | "9", "10", "11", "12", "13", "14", "15", | |
69 | NULL, | |
70 | }; | |
71 | ||
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 | ||
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 | ||
98 | static int init(struct sr_context *sr_ctx) | |
99 | { | |
100 | return std_init(sr_ctx, di, LOG_PREFIX); | |
101 | } | |
102 | ||
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, | |
127 | des.iProduct, strdesc, sizeof(strdesc)) < 0) | |
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 | ||
141 | static GSList *scan(GSList *options) | |
142 | { | |
143 | struct drv_context *drvc; | |
144 | struct dev_context *devc; | |
145 | struct sr_dev_inst *sdi; | |
146 | struct sr_usb_dev_inst *usb; | |
147 | struct sr_channel *ch; | |
148 | struct sr_config *src; | |
149 | GSList *l, *devices, *conn_devices; | |
150 | struct libusb_device_descriptor des; | |
151 | libusb_device **devlist; | |
152 | int ret, i, j; | |
153 | const char *conn; | |
154 | char connection_id[64]; | |
155 | ||
156 | drvc = di->priv; | |
157 | ||
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. */ | |
173 | devices = NULL; | |
174 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
175 | for (i = 0; devlist[i]; i++) { | |
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 | ||
190 | if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) { | |
191 | sr_warn("Failed to get device descriptor: %s.", | |
192 | libusb_error_name(ret)); | |
193 | continue; | |
194 | } | |
195 | ||
196 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
197 | ||
198 | if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) | |
199 | continue; | |
200 | ||
201 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
202 | sdi->status = SR_ST_INITIALIZING; | |
203 | sdi->vendor = g_strdup("Saleae"); | |
204 | sdi->model = g_strdup("Logic16"); | |
205 | sdi->driver = di; | |
206 | sdi->connection_id = g_strdup(connection_id); | |
207 | ||
208 | for (j = 0; channel_names[j]; j++) { | |
209 | ch = sr_channel_new(j, SR_CHANNEL_LOGIC, TRUE, | |
210 | channel_names[j]); | |
211 | sdi->channels = g_slist_append(sdi->channels, ch); | |
212 | } | |
213 | ||
214 | devc = g_malloc0(sizeof(struct dev_context)); | |
215 | devc->selected_voltage_range = VOLTAGE_RANGE_18_33_V; | |
216 | sdi->priv = devc; | |
217 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
218 | devices = g_slist_append(devices, sdi); | |
219 | ||
220 | if (check_conf_profile(devlist[i])) { | |
221 | /* Already has the firmware, so fix the new address. */ | |
222 | sr_dbg("Found a Logic16 device."); | |
223 | sdi->status = SR_ST_INACTIVE; | |
224 | sdi->inst_type = SR_INST_USB; | |
225 | sdi->conn = sr_usb_dev_inst_new( | |
226 | libusb_get_bus_number(devlist[i]), | |
227 | libusb_get_device_address(devlist[i]), NULL); | |
228 | } else { | |
229 | if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, | |
230 | FX2_FIRMWARE) == SR_OK) | |
231 | /* Store when this device's FW was updated. */ | |
232 | devc->fw_updated = g_get_monotonic_time(); | |
233 | else | |
234 | sr_err("Firmware upload failed."); | |
235 | sdi->inst_type = SR_INST_USB; | |
236 | sdi->conn = sr_usb_dev_inst_new( | |
237 | libusb_get_bus_number(devlist[i]), 0xff, NULL); | |
238 | } | |
239 | } | |
240 | libusb_free_device_list(devlist, 1); | |
241 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); | |
242 | ||
243 | return devices; | |
244 | } | |
245 | ||
246 | static GSList *dev_list(void) | |
247 | { | |
248 | return ((struct drv_context *)(di->priv))->instances; | |
249 | } | |
250 | ||
251 | static int logic16_dev_open(struct sr_dev_inst *sdi) | |
252 | { | |
253 | libusb_device **devlist; | |
254 | struct sr_usb_dev_inst *usb; | |
255 | struct libusb_device_descriptor des; | |
256 | struct drv_context *drvc; | |
257 | int ret, i, device_count; | |
258 | char connection_id[64]; | |
259 | ||
260 | drvc = di->priv; | |
261 | usb = sdi->conn; | |
262 | ||
263 | if (sdi->status == SR_ST_ACTIVE) | |
264 | /* Device is already in use. */ | |
265 | return SR_ERR; | |
266 | ||
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 | ||
281 | if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) | |
282 | continue; | |
283 | ||
284 | if ((sdi->status == SR_ST_INITIALIZING) || | |
285 | (sdi->status == SR_ST_INACTIVE)) { | |
286 | /* | |
287 | * Check device by its physical USB bus/port address. | |
288 | */ | |
289 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
290 | if (strcmp(sdi->connection_id, connection_id)) | |
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 | ||
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 | ||
322 | if ((ret = logic16_init_device(sdi)) != SR_OK) { | |
323 | sr_err("Failed to init device."); | |
324 | break; | |
325 | } | |
326 | ||
327 | sdi->status = SR_ST_ACTIVE; | |
328 | sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.", | |
329 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
330 | ||
331 | break; | |
332 | } | |
333 | libusb_free_device_list(devlist, 1); | |
334 | ||
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 | } | |
341 | return SR_ERR; | |
342 | } | |
343 | ||
344 | return SR_OK; | |
345 | } | |
346 | ||
347 | static int dev_open(struct sr_dev_inst *sdi) | |
348 | { | |
349 | struct dev_context *devc; | |
350 | int ret; | |
351 | int64_t timediff_us, timediff_ms; | |
352 | ||
353 | devc = sdi->priv; | |
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 | ||
389 | if (devc->cur_samplerate == 0) { | |
390 | /* Samplerate hasn't been set; default to the slowest one. */ | |
391 | devc->cur_samplerate = samplerates[0]; | |
392 | } | |
393 | ||
394 | return SR_OK; | |
395 | } | |
396 | ||
397 | static int dev_close(struct sr_dev_inst *sdi) | |
398 | { | |
399 | struct sr_usb_dev_inst *usb; | |
400 | ||
401 | usb = sdi->conn; | |
402 | if (usb->devhdl == NULL) | |
403 | return SR_ERR; | |
404 | ||
405 | sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.", | |
406 | usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); | |
407 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
408 | libusb_close(usb->devhdl); | |
409 | usb->devhdl = NULL; | |
410 | sdi->status = SR_ST_INACTIVE; | |
411 | ||
412 | return SR_OK; | |
413 | } | |
414 | ||
415 | static int cleanup(void) | |
416 | { | |
417 | int ret; | |
418 | struct drv_context *drvc; | |
419 | ||
420 | if (!(drvc = di->priv)) | |
421 | /* Can get called on an unused driver, doesn't matter. */ | |
422 | return SR_OK; | |
423 | ||
424 | ret = std_dev_clear(di, NULL); | |
425 | g_free(drvc); | |
426 | di->priv = NULL; | |
427 | ||
428 | return ret; | |
429 | } | |
430 | ||
431 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
432 | const struct sr_channel_group *cg) | |
433 | { | |
434 | struct dev_context *devc; | |
435 | struct sr_usb_dev_inst *usb; | |
436 | GVariant *range[2]; | |
437 | char str[128]; | |
438 | int ret; | |
439 | unsigned int i; | |
440 | ||
441 | (void)cg; | |
442 | ||
443 | ret = SR_OK; | |
444 | switch (key) { | |
445 | case SR_CONF_CONN: | |
446 | if (!sdi || !sdi->conn) | |
447 | return SR_ERR_ARG; | |
448 | usb = sdi->conn; | |
449 | if (usb->address == 255) | |
450 | /* Device still needs to re-enumerate after firmware | |
451 | * upload, so we don't know its (future) address. */ | |
452 | return SR_ERR; | |
453 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
454 | *data = g_variant_new_string(str); | |
455 | break; | |
456 | case SR_CONF_SAMPLERATE: | |
457 | if (!sdi) | |
458 | return SR_ERR; | |
459 | devc = sdi->priv; | |
460 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
461 | break; | |
462 | case SR_CONF_VOLTAGE_THRESHOLD: | |
463 | if (!sdi) | |
464 | return SR_ERR; | |
465 | devc = sdi->priv; | |
466 | ret = SR_ERR; | |
467 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { | |
468 | if (devc->selected_voltage_range != | |
469 | volt_thresholds[i].range) | |
470 | continue; | |
471 | range[0] = g_variant_new_double(volt_thresholds[i].low); | |
472 | range[1] = g_variant_new_double(volt_thresholds[i].high); | |
473 | *data = g_variant_new_tuple(range, 2); | |
474 | ret = SR_OK; | |
475 | break; | |
476 | } | |
477 | break; | |
478 | default: | |
479 | return SR_ERR_NA; | |
480 | } | |
481 | ||
482 | return ret; | |
483 | } | |
484 | ||
485 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
486 | const struct sr_channel_group *cg) | |
487 | { | |
488 | struct dev_context *devc; | |
489 | gdouble low, high; | |
490 | int ret; | |
491 | unsigned int i; | |
492 | ||
493 | (void)cg; | |
494 | ||
495 | if (sdi->status != SR_ST_ACTIVE) | |
496 | return SR_ERR_DEV_CLOSED; | |
497 | ||
498 | devc = sdi->priv; | |
499 | ||
500 | ret = SR_OK; | |
501 | switch (key) { | |
502 | case SR_CONF_SAMPLERATE: | |
503 | devc->cur_samplerate = g_variant_get_uint64(data); | |
504 | break; | |
505 | case SR_CONF_LIMIT_SAMPLES: | |
506 | devc->limit_samples = g_variant_get_uint64(data); | |
507 | break; | |
508 | case SR_CONF_VOLTAGE_THRESHOLD: | |
509 | g_variant_get(data, "(dd)", &low, &high); | |
510 | ret = SR_ERR_ARG; | |
511 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { | |
512 | if (fabs(volt_thresholds[i].low - low) < 0.1 && | |
513 | fabs(volt_thresholds[i].high - high) < 0.1) { | |
514 | devc->selected_voltage_range = | |
515 | volt_thresholds[i].range; | |
516 | ret = SR_OK; | |
517 | break; | |
518 | } | |
519 | } | |
520 | break; | |
521 | default: | |
522 | ret = SR_ERR_NA; | |
523 | } | |
524 | ||
525 | return ret; | |
526 | } | |
527 | ||
528 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
529 | const struct sr_channel_group *cg) | |
530 | { | |
531 | GVariant *gvar, *range[2]; | |
532 | GVariantBuilder gvb; | |
533 | int ret; | |
534 | unsigned int i; | |
535 | ||
536 | (void)sdi; | |
537 | (void)cg; | |
538 | ||
539 | ret = SR_OK; | |
540 | switch (key) { | |
541 | case SR_CONF_SCAN_OPTIONS: | |
542 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
543 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
544 | break; | |
545 | case SR_CONF_DEVICE_OPTIONS: | |
546 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
547 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
548 | break; | |
549 | case SR_CONF_SAMPLERATE: | |
550 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
551 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
552 | samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
553 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
554 | *data = g_variant_builder_end(&gvb); | |
555 | break; | |
556 | case SR_CONF_VOLTAGE_THRESHOLD: | |
557 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
558 | for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) { | |
559 | range[0] = g_variant_new_double(volt_thresholds[i].low); | |
560 | range[1] = g_variant_new_double(volt_thresholds[i].high); | |
561 | gvar = g_variant_new_tuple(range, 2); | |
562 | g_variant_builder_add_value(&gvb, gvar); | |
563 | } | |
564 | *data = g_variant_builder_end(&gvb); | |
565 | break; | |
566 | case SR_CONF_TRIGGER_MATCH: | |
567 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
568 | soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches), | |
569 | sizeof(int32_t)); | |
570 | break; | |
571 | default: | |
572 | return SR_ERR_NA; | |
573 | } | |
574 | ||
575 | return ret; | |
576 | } | |
577 | ||
578 | static void abort_acquisition(struct dev_context *devc) | |
579 | { | |
580 | int i; | |
581 | ||
582 | devc->sent_samples = -1; | |
583 | ||
584 | for (i = devc->num_transfers - 1; i >= 0; i--) { | |
585 | if (devc->transfers[i]) | |
586 | libusb_cancel_transfer(devc->transfers[i]); | |
587 | } | |
588 | } | |
589 | ||
590 | static unsigned int bytes_per_ms(struct dev_context *devc) | |
591 | { | |
592 | return devc->cur_samplerate * devc->num_channels / 8000; | |
593 | } | |
594 | ||
595 | static size_t get_buffer_size(struct dev_context *devc) | |
596 | { | |
597 | size_t s; | |
598 | ||
599 | /* | |
600 | * The buffer should be large enough to hold 10ms of data and | |
601 | * a multiple of 512. | |
602 | */ | |
603 | s = 10 * bytes_per_ms(devc); | |
604 | return (s + 511) & ~511; | |
605 | } | |
606 | ||
607 | static unsigned int get_number_of_transfers(struct dev_context *devc) | |
608 | { | |
609 | unsigned int n; | |
610 | ||
611 | /* Total buffer size should be able to hold about 500ms of data. */ | |
612 | n = 500 * bytes_per_ms(devc) / get_buffer_size(devc); | |
613 | ||
614 | if (n > NUM_SIMUL_TRANSFERS) | |
615 | return NUM_SIMUL_TRANSFERS; | |
616 | ||
617 | return n; | |
618 | } | |
619 | ||
620 | static unsigned int get_timeout(struct dev_context *devc) | |
621 | { | |
622 | size_t total_size; | |
623 | unsigned int timeout; | |
624 | ||
625 | total_size = get_buffer_size(devc) * get_number_of_transfers(devc); | |
626 | timeout = total_size / bytes_per_ms(devc); | |
627 | return timeout + timeout / 4; /* Leave a headroom of 25% percent. */ | |
628 | } | |
629 | ||
630 | static int configure_channels(const struct sr_dev_inst *sdi) | |
631 | { | |
632 | struct dev_context *devc; | |
633 | struct sr_channel *ch; | |
634 | GSList *l; | |
635 | uint16_t channel_bit; | |
636 | ||
637 | devc = sdi->priv; | |
638 | ||
639 | devc->cur_channels = 0; | |
640 | devc->num_channels = 0; | |
641 | for (l = sdi->channels; l; l = l->next) { | |
642 | ch = (struct sr_channel *)l->data; | |
643 | if (ch->enabled == FALSE) | |
644 | continue; | |
645 | ||
646 | channel_bit = 1 << (ch->index); | |
647 | ||
648 | devc->cur_channels |= channel_bit; | |
649 | ||
650 | #ifdef WORDS_BIGENDIAN | |
651 | /* | |
652 | * Output logic data should be stored in little endian format. | |
653 | * To speed things up during conversion, do the switcharoo | |
654 | * here instead. | |
655 | */ | |
656 | channel_bit = 1 << (ch->index ^ 8); | |
657 | #endif | |
658 | ||
659 | devc->channel_masks[devc->num_channels++] = channel_bit; | |
660 | } | |
661 | ||
662 | return SR_OK; | |
663 | } | |
664 | ||
665 | static int receive_data(int fd, int revents, void *cb_data) | |
666 | { | |
667 | struct timeval tv; | |
668 | struct dev_context *devc; | |
669 | struct drv_context *drvc; | |
670 | const struct sr_dev_inst *sdi; | |
671 | ||
672 | (void)fd; | |
673 | (void)revents; | |
674 | ||
675 | sdi = cb_data; | |
676 | drvc = di->priv; | |
677 | devc = sdi->priv; | |
678 | ||
679 | tv.tv_sec = tv.tv_usec = 0; | |
680 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
681 | ||
682 | if (devc->sent_samples == -2) { | |
683 | logic16_abort_acquisition(sdi); | |
684 | abort_acquisition(devc); | |
685 | } | |
686 | ||
687 | return TRUE; | |
688 | } | |
689 | ||
690 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
691 | { | |
692 | struct dev_context *devc; | |
693 | struct drv_context *drvc; | |
694 | struct sr_usb_dev_inst *usb; | |
695 | struct sr_trigger *trigger; | |
696 | struct libusb_transfer *transfer; | |
697 | unsigned int i, timeout, num_transfers; | |
698 | int ret; | |
699 | unsigned char *buf; | |
700 | size_t size, convsize; | |
701 | ||
702 | if (sdi->status != SR_ST_ACTIVE) | |
703 | return SR_ERR_DEV_CLOSED; | |
704 | ||
705 | drvc = di->priv; | |
706 | devc = sdi->priv; | |
707 | usb = sdi->conn; | |
708 | ||
709 | /* Configures devc->cur_channels. */ | |
710 | if (configure_channels(sdi) != SR_OK) { | |
711 | sr_err("Failed to configure channels."); | |
712 | return SR_ERR; | |
713 | } | |
714 | ||
715 | devc->cb_data = cb_data; | |
716 | devc->sent_samples = 0; | |
717 | devc->empty_transfer_count = 0; | |
718 | devc->cur_channel = 0; | |
719 | memset(devc->channel_data, 0, sizeof(devc->channel_data)); | |
720 | ||
721 | if ((trigger = sr_session_trigger_get(sdi->session))) { | |
722 | devc->stl = soft_trigger_logic_new(sdi, trigger, 0); | |
723 | devc->trigger_fired = FALSE; | |
724 | } else | |
725 | devc->trigger_fired = TRUE; | |
726 | ||
727 | timeout = get_timeout(devc); | |
728 | num_transfers = get_number_of_transfers(devc); | |
729 | size = get_buffer_size(devc); | |
730 | convsize = (size / devc->num_channels + 2) * 16; | |
731 | devc->submitted_transfers = 0; | |
732 | ||
733 | devc->convbuffer_size = convsize; | |
734 | if (!(devc->convbuffer = g_try_malloc(convsize))) { | |
735 | sr_err("Conversion buffer malloc failed."); | |
736 | return SR_ERR_MALLOC; | |
737 | } | |
738 | ||
739 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); | |
740 | if (!devc->transfers) { | |
741 | sr_err("USB transfers malloc failed."); | |
742 | g_free(devc->convbuffer); | |
743 | return SR_ERR_MALLOC; | |
744 | } | |
745 | ||
746 | if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate, | |
747 | devc->cur_channels)) != SR_OK) { | |
748 | g_free(devc->transfers); | |
749 | g_free(devc->convbuffer); | |
750 | return ret; | |
751 | } | |
752 | ||
753 | devc->num_transfers = num_transfers; | |
754 | for (i = 0; i < num_transfers; i++) { | |
755 | if (!(buf = g_try_malloc(size))) { | |
756 | sr_err("USB transfer buffer malloc failed."); | |
757 | if (devc->submitted_transfers) | |
758 | abort_acquisition(devc); | |
759 | else { | |
760 | g_free(devc->transfers); | |
761 | g_free(devc->convbuffer); | |
762 | } | |
763 | return SR_ERR_MALLOC; | |
764 | } | |
765 | transfer = libusb_alloc_transfer(0); | |
766 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
767 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
768 | logic16_receive_transfer, (void *)sdi, timeout); | |
769 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
770 | sr_err("Failed to submit transfer: %s.", | |
771 | libusb_error_name(ret)); | |
772 | libusb_free_transfer(transfer); | |
773 | g_free(buf); | |
774 | abort_acquisition(devc); | |
775 | return SR_ERR; | |
776 | } | |
777 | devc->transfers[i] = transfer; | |
778 | devc->submitted_transfers++; | |
779 | } | |
780 | ||
781 | devc->ctx = drvc->sr_ctx; | |
782 | ||
783 | usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi); | |
784 | ||
785 | /* Send header packet to the session bus. */ | |
786 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
787 | ||
788 | if ((ret = logic16_start_acquisition(sdi)) != SR_OK) { | |
789 | abort_acquisition(devc); | |
790 | return ret; | |
791 | } | |
792 | ||
793 | return SR_OK; | |
794 | } | |
795 | ||
796 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
797 | { | |
798 | int ret; | |
799 | ||
800 | (void)cb_data; | |
801 | ||
802 | if (sdi->status != SR_ST_ACTIVE) | |
803 | return SR_ERR_DEV_CLOSED; | |
804 | ||
805 | ret = logic16_abort_acquisition(sdi); | |
806 | ||
807 | abort_acquisition(sdi->priv); | |
808 | ||
809 | return ret; | |
810 | } | |
811 | ||
812 | SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = { | |
813 | .name = "saleae-logic16", | |
814 | .longname = "Saleae Logic16", | |
815 | .api_version = 1, | |
816 | .init = init, | |
817 | .cleanup = cleanup, | |
818 | .scan = scan, | |
819 | .dev_list = dev_list, | |
820 | .dev_clear = NULL, | |
821 | .config_get = config_get, | |
822 | .config_set = config_set, | |
823 | .config_list = config_list, | |
824 | .dev_open = dev_open, | |
825 | .dev_close = dev_close, | |
826 | .dev_acquisition_start = dev_acquisition_start, | |
827 | .dev_acquisition_stop = dev_acquisition_stop, | |
828 | .priv = NULL, | |
829 | }; |