]> sigrok.org Git - libsigrok.git/blame - src/hardware/saleae-logic16/api.c
Introduce standard cleanup helper
[libsigrok.git] / src / hardware / saleae-logic16 / api.c
CommitLineData
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
c463dcf0 42SR_PRIV struct sr_dev_driver saleae_logic16_driver_info;
c463dcf0 43
a0e0bb41 44static const uint32_t scanopts[] = {
b117363a
MC
45 SR_CONF_CONN,
46};
47
f254bc4b 48static const uint32_t devopts[] = {
b117363a 49 SR_CONF_LOGIC_ANALYZER,
b117363a 50 SR_CONF_CONTINUOUS,
5827f61b
BV
51 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
52 SR_CONF_CONN | SR_CONF_GET,
53 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
54 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
5a971f66 56 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
b117363a
MC
57};
58
863357fb
BV
59static const int32_t soft_trigger_matches[] = {
60 SR_TRIGGER_ZERO,
61 SR_TRIGGER_ONE,
62 SR_TRIGGER_RISING,
63 SR_TRIGGER_FALLING,
64 SR_TRIGGER_EDGE,
65};
66
ba7dd8bb 67static const char *channel_names[] = {
f6a21fa5
MC
68 "0", "1", "2", "3", "4", "5", "6", "7", "8",
69 "9", "10", "11", "12", "13", "14", "15",
f6a21fa5 70};
c463dcf0 71
db11d7d2
MC
72static const struct {
73 enum voltage_range range;
74 gdouble low;
75 gdouble high;
76} volt_thresholds[] = {
77 { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 },
78 { VOLTAGE_RANGE_5_V, 1.4, 3.6 },
79};
80
7b5daad4
MC
81static const uint64_t samplerates[] = {
82 SR_KHZ(500),
83 SR_MHZ(1),
84 SR_MHZ(2),
85 SR_MHZ(4),
86 SR_MHZ(5),
87 SR_MHZ(8),
88 SR_MHZ(10),
89 SR_KHZ(12500),
90 SR_MHZ(16),
91 SR_MHZ(25),
92 SR_MHZ(32),
93 SR_MHZ(40),
94 SR_MHZ(80),
95 SR_MHZ(100),
96};
97
4f840ce9 98static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
c463dcf0
MC
99{
100 return std_init(sr_ctx, di, LOG_PREFIX);
101}
102
5eea4305
MC
103static 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. */
2a8f2d41 114 libusb_get_device_descriptor(dev, &des);
5eea4305
MC
115
116 if (libusb_open(dev, &hdl) != 0)
117 break;
118
119 if (libusb_get_string_descriptor_ascii(hdl,
120 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
121 break;
122 if (strcmp((const char *)strdesc, "Saleae LLC"))
123 break;
124
125 if (libusb_get_string_descriptor_ascii(hdl,
96484e22 126 des.iProduct, strdesc, sizeof(strdesc)) < 0)
5eea4305
MC
127 break;
128 if (strcmp((const char *)strdesc, "Logic S/16"))
129 break;
130
131 /* If we made it here, it must be a configured Logic16. */
132 ret = TRUE;
133 }
134 if (hdl)
135 libusb_close(hdl);
136
137 return ret;
138}
139
4f840ce9 140static GSList *scan(struct sr_dev_driver *di, GSList *options)
c463dcf0
MC
141{
142 struct drv_context *drvc;
f6a21fa5
MC
143 struct dev_context *devc;
144 struct sr_dev_inst *sdi;
5eea4305 145 struct sr_usb_dev_inst *usb;
5eea4305
MC
146 struct sr_config *src;
147 GSList *l, *devices, *conn_devices;
f6a21fa5
MC
148 struct libusb_device_descriptor des;
149 libusb_device **devlist;
2cca921d 150 unsigned int i, j;
5eea4305 151 const char *conn;
ce7d3578 152 char connection_id[64];
c463dcf0 153
41812aca 154 drvc = di->context;
c463dcf0 155
5eea4305
MC
156 conn = NULL;
157 for (l = options; l; l = l->next) {
158 src = l->data;
159 switch (src->key) {
160 case SR_CONF_CONN:
161 conn = g_variant_get_string(src->data, NULL);
162 break;
163 }
164 }
165 if (conn)
166 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
167 else
168 conn_devices = NULL;
169
170 /* Find all Logic16 devices and upload firmware to them. */
f6a21fa5
MC
171 devices = NULL;
172 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
173 for (i = 0; devlist[i]; i++) {
5eea4305
MC
174 if (conn) {
175 usb = NULL;
176 for (l = conn_devices; l; l = l->next) {
177 usb = l->data;
178 if (usb->bus == libusb_get_bus_number(devlist[i])
179 && usb->address == libusb_get_device_address(devlist[i]))
180 break;
181 }
182 if (!l)
183 /* This device matched none of the ones that
184 * matched the conn specification. */
185 continue;
186 }
187
2a8f2d41 188 libusb_get_device_descriptor(devlist[i], &des);
f6a21fa5 189
ce7d3578
SA
190 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
191
f6a21fa5
MC
192 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
193 continue;
194
aac29cc1 195 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
196 sdi->status = SR_ST_INITIALIZING;
197 sdi->vendor = g_strdup("Saleae");
198 sdi->model = g_strdup("Logic16");
f6a21fa5 199 sdi->driver = di;
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,
c368e6f3 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
MC
209 drvc->instances = g_slist_append(drvc->instances, sdi);
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
DE
221 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
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();
225 else
ce7d3578 226 sr_err("Firmware upload failed.");
5eea4305 227 sdi->inst_type = SR_INST_USB;
96484e22
UH
228 sdi->conn = sr_usb_dev_inst_new(
229 libusb_get_bus_number(devlist[i]), 0xff, NULL);
5eea4305 230 }
f6a21fa5
MC
231 }
232 libusb_free_device_list(devlist, 1);
5eea4305 233 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
c463dcf0
MC
234
235 return devices;
236}
237
4f840ce9 238static GSList *dev_list(const struct sr_dev_driver *di)
c463dcf0 239{
41812aca 240 return ((struct drv_context *)(di->context))->instances;
c463dcf0
MC
241}
242
5eea4305
MC
243static int logic16_dev_open(struct sr_dev_inst *sdi)
244{
4f840ce9 245 struct sr_dev_driver *di;
5eea4305
MC
246 libusb_device **devlist;
247 struct sr_usb_dev_inst *usb;
248 struct libusb_device_descriptor des;
5eea4305 249 struct drv_context *drvc;
ce7d3578
SA
250 int ret, i, device_count;
251 char connection_id[64];
5eea4305 252
4f840ce9 253 di = sdi->driver;
41812aca 254 drvc = di->context;
5eea4305
MC
255 usb = sdi->conn;
256
257 if (sdi->status == SR_ST_ACTIVE)
258 /* Device is already in use. */
259 return SR_ERR;
260
5eea4305
MC
261 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
262 if (device_count < 0) {
263 sr_err("Failed to get device list: %s.",
264 libusb_error_name(device_count));
265 return SR_ERR;
266 }
267
268 for (i = 0; i < device_count; i++) {
2a8f2d41 269 libusb_get_device_descriptor(devlist[i], &des);
5eea4305 270
96484e22 271 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
5eea4305
MC
272 continue;
273
ce7d3578
SA
274 if ((sdi->status == SR_ST_INITIALIZING) ||
275 (sdi->status == SR_ST_INACTIVE)) {
5eea4305 276 /*
ce7d3578 277 * Check device by its physical USB bus/port address.
5eea4305 278 */
ce7d3578
SA
279 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
280 if (strcmp(sdi->connection_id, connection_id))
5eea4305
MC
281 /* This is not the one. */
282 continue;
283 }
284
285 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
286 if (usb->address == 0xff)
287 /*
288 * First time we touch this device after FW
289 * upload, so we don't know the address yet.
290 */
291 usb->address = libusb_get_device_address(devlist[i]);
292 } else {
293 sr_err("Failed to open device: %s.",
294 libusb_error_name(ret));
295 break;
296 }
297
0c30b35f
SY
298 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
299 if (ret == LIBUSB_ERROR_BUSY) {
300 sr_err("Unable to claim USB interface. Another "
301 "program or driver has already claimed it.");
302 break;
303 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
304 sr_err("Device has been disconnected.");
305 break;
306 } else if (ret != 0) {
307 sr_err("Unable to claim interface: %s.",
308 libusb_error_name(ret));
309 break;
310 }
311
96484e22 312 if ((ret = logic16_init_device(sdi)) != SR_OK) {
15abcf0f
MC
313 sr_err("Failed to init device.");
314 break;
315 }
316
5eea4305 317 sdi->status = SR_ST_ACTIVE;
ce7d3578
SA
318 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
319 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
5eea4305
MC
320
321 break;
322 }
323 libusb_free_device_list(devlist, 1);
324
0c30b35f
SY
325 if (sdi->status != SR_ST_ACTIVE) {
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
337static 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
387static int dev_close(struct sr_dev_inst *sdi)
388{
5eea4305 389 struct sr_usb_dev_inst *usb;
c463dcf0 390
5eea4305 391 usb = sdi->conn;
98fec29e 392 if (!usb->devhdl)
5eea4305 393 return SR_ERR;
c463dcf0 394
ce7d3578
SA
395 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
396 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
5eea4305
MC
397 libusb_release_interface(usb->devhdl, USB_INTERFACE);
398 libusb_close(usb->devhdl);
399 usb->devhdl = NULL;
c463dcf0
MC
400 sdi->status = SR_ST_INACTIVE;
401
402 return SR_OK;
403}
404
584560f1 405static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 406 const struct sr_channel_group *cg)
c463dcf0 407{
7b5daad4 408 struct dev_context *devc;
b117363a 409 struct sr_usb_dev_inst *usb;
db11d7d2 410 GVariant *range[2];
b117363a 411 char str[128];
c463dcf0 412 int ret;
96484e22 413 unsigned int i;
c463dcf0 414
53b4680f 415 (void)cg;
d3c74a6f 416
c463dcf0
MC
417 ret = SR_OK;
418 switch (key) {
b117363a
MC
419 case SR_CONF_CONN:
420 if (!sdi || !sdi->conn)
421 return SR_ERR_ARG;
422 usb = sdi->conn;
423 if (usb->address == 255)
424 /* Device still needs to re-enumerate after firmware
425 * upload, so we don't know its (future) address. */
426 return SR_ERR;
427 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
428 *data = g_variant_new_string(str);
429 break;
7b5daad4
MC
430 case SR_CONF_SAMPLERATE:
431 if (!sdi)
432 return SR_ERR;
433 devc = sdi->priv;
434 *data = g_variant_new_uint64(devc->cur_samplerate);
435 break;
5a971f66
AJ
436 case SR_CONF_CAPTURE_RATIO:
437 if (!sdi)
438 return SR_ERR;
439 devc = sdi->priv;
440 *data = g_variant_new_uint64(devc->capture_ratio);
441 break;
db11d7d2
MC
442 case SR_CONF_VOLTAGE_THRESHOLD:
443 if (!sdi)
444 return SR_ERR;
445 devc = sdi->priv;
446 ret = SR_ERR;
96484e22
UH
447 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
448 if (devc->selected_voltage_range !=
449 volt_thresholds[i].range)
450 continue;
451 range[0] = g_variant_new_double(volt_thresholds[i].low);
452 range[1] = g_variant_new_double(volt_thresholds[i].high);
453 *data = g_variant_new_tuple(range, 2);
454 ret = SR_OK;
455 break;
456 }
db11d7d2 457 break;
c463dcf0
MC
458 default:
459 return SR_ERR_NA;
460 }
461
462 return ret;
463}
464
584560f1 465static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 466 const struct sr_channel_group *cg)
c463dcf0 467{
7b5daad4 468 struct dev_context *devc;
db11d7d2 469 gdouble low, high;
c463dcf0 470 int ret;
96484e22 471 unsigned int i;
c463dcf0 472
53b4680f 473 (void)cg;
d3c74a6f 474
c463dcf0
MC
475 if (sdi->status != SR_ST_ACTIVE)
476 return SR_ERR_DEV_CLOSED;
477
7b5daad4
MC
478 devc = sdi->priv;
479
c463dcf0
MC
480 ret = SR_OK;
481 switch (key) {
7b5daad4
MC
482 case SR_CONF_SAMPLERATE:
483 devc->cur_samplerate = g_variant_get_uint64(data);
484 break;
485 case SR_CONF_LIMIT_SAMPLES:
486 devc->limit_samples = g_variant_get_uint64(data);
487 break;
5a971f66
AJ
488 case SR_CONF_CAPTURE_RATIO:
489 devc->capture_ratio = g_variant_get_uint64(data);
a5c38703 490 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
5a971f66 491 break;
db11d7d2
MC
492 case SR_CONF_VOLTAGE_THRESHOLD:
493 g_variant_get(data, "(dd)", &low, &high);
494 ret = SR_ERR_ARG;
495 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
496 if (fabs(volt_thresholds[i].low - low) < 0.1 &&
497 fabs(volt_thresholds[i].high - high) < 0.1) {
498 devc->selected_voltage_range =
499 volt_thresholds[i].range;
500 ret = SR_OK;
501 break;
502 }
503 }
504 break;
c463dcf0
MC
505 default:
506 ret = SR_ERR_NA;
507 }
508
509 return ret;
510}
511
584560f1 512static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 513 const struct sr_channel_group *cg)
c463dcf0 514{
db11d7d2 515 GVariant *gvar, *range[2];
7b5daad4 516 GVariantBuilder gvb;
c463dcf0 517 int ret;
96484e22 518 unsigned int i;
c463dcf0
MC
519
520 (void)sdi;
53b4680f 521 (void)cg;
c463dcf0
MC
522
523 ret = SR_OK;
524 switch (key) {
b117363a 525 case SR_CONF_SCAN_OPTIONS:
584560f1 526 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 527 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
b117363a
MC
528 break;
529 case SR_CONF_DEVICE_OPTIONS:
584560f1 530 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 531 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
b117363a 532 break;
7b5daad4
MC
533 case SR_CONF_SAMPLERATE:
534 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
96484e22
UH
535 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
536 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
7b5daad4
MC
537 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
538 *data = g_variant_builder_end(&gvb);
db11d7d2
MC
539 break;
540 case SR_CONF_VOLTAGE_THRESHOLD:
541 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
542 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
543 range[0] = g_variant_new_double(volt_thresholds[i].low);
544 range[1] = g_variant_new_double(volt_thresholds[i].high);
545 gvar = g_variant_new_tuple(range, 2);
546 g_variant_builder_add_value(&gvb, gvar);
547 }
548 *data = g_variant_builder_end(&gvb);
7b5daad4 549 break;
863357fb
BV
550 case SR_CONF_TRIGGER_MATCH:
551 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
552 soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches),
553 sizeof(int32_t));
554 break;
c463dcf0
MC
555 default:
556 return SR_ERR_NA;
557 }
558
559 return ret;
560}
561
7b5daad4
MC
562static void abort_acquisition(struct dev_context *devc)
563{
564 int i;
565
863357fb 566 devc->sent_samples = -1;
7b5daad4
MC
567
568 for (i = devc->num_transfers - 1; i >= 0; i--) {
569 if (devc->transfers[i])
570 libusb_cancel_transfer(devc->transfers[i]);
571 }
572}
573
574static unsigned int bytes_per_ms(struct dev_context *devc)
575{
576 return devc->cur_samplerate * devc->num_channels / 8000;
577}
578
579static size_t get_buffer_size(struct dev_context *devc)
580{
581 size_t s;
582
583 /*
584 * The buffer should be large enough to hold 10ms of data and
585 * a multiple of 512.
586 */
587 s = 10 * bytes_per_ms(devc);
588 return (s + 511) & ~511;
589}
590
591static unsigned int get_number_of_transfers(struct dev_context *devc)
592{
593 unsigned int n;
594
595 /* Total buffer size should be able to hold about 500ms of data. */
596 n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
597
598 if (n > NUM_SIMUL_TRANSFERS)
599 return NUM_SIMUL_TRANSFERS;
600
601 return n;
602}
603
604static unsigned int get_timeout(struct dev_context *devc)
605{
606 size_t total_size;
607 unsigned int timeout;
608
609 total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
610 timeout = total_size / bytes_per_ms(devc);
611 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
612}
613
ba7dd8bb 614static int configure_channels(const struct sr_dev_inst *sdi)
7b5daad4
MC
615{
616 struct dev_context *devc;
ba7dd8bb 617 struct sr_channel *ch;
7b5daad4 618 GSList *l;
ba7dd8bb 619 uint16_t channel_bit;
7b5daad4
MC
620
621 devc = sdi->priv;
622
623 devc->cur_channels = 0;
624 devc->num_channels = 0;
ba7dd8bb
UH
625 for (l = sdi->channels; l; l = l->next) {
626 ch = (struct sr_channel *)l->data;
627 if (ch->enabled == FALSE)
7b5daad4
MC
628 continue;
629
ba7dd8bb 630 channel_bit = 1 << (ch->index);
7b5daad4 631
ba7dd8bb 632 devc->cur_channels |= channel_bit;
7b5daad4
MC
633
634#ifdef WORDS_BIGENDIAN
96484e22
UH
635 /*
636 * Output logic data should be stored in little endian format.
637 * To speed things up during conversion, do the switcharoo
638 * here instead.
639 */
ba7dd8bb 640 channel_bit = 1 << (ch->index ^ 8);
7b5daad4
MC
641#endif
642
ba7dd8bb 643 devc->channel_masks[devc->num_channels++] = channel_bit;
7b5daad4
MC
644 }
645
646 return SR_OK;
647}
648
649static int receive_data(int fd, int revents, void *cb_data)
650{
651 struct timeval tv;
652 struct dev_context *devc;
653 struct drv_context *drvc;
654 const struct sr_dev_inst *sdi;
4f840ce9 655 struct sr_dev_driver *di;
7b5daad4
MC
656
657 (void)fd;
658 (void)revents;
659
660 sdi = cb_data;
4f840ce9 661 di = sdi->driver;
41812aca 662 drvc = di->context;
7b5daad4
MC
663 devc = sdi->priv;
664
665 tv.tv_sec = tv.tv_usec = 0;
666 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
667
863357fb 668 if (devc->sent_samples == -2) {
96484e22 669 logic16_abort_acquisition(sdi);
7b5daad4
MC
670 abort_acquisition(devc);
671 }
672
673 return TRUE;
674}
675
695dc859 676static int dev_acquisition_start(const struct sr_dev_inst *sdi)
c463dcf0 677{
4f840ce9 678 struct sr_dev_driver *di = sdi->driver;
7b5daad4
MC
679 struct dev_context *devc;
680 struct drv_context *drvc;
681 struct sr_usb_dev_inst *usb;
863357fb 682 struct sr_trigger *trigger;
7b5daad4 683 struct libusb_transfer *transfer;
7b5daad4
MC
684 unsigned int i, timeout, num_transfers;
685 int ret;
686 unsigned char *buf;
687 size_t size, convsize;
c463dcf0
MC
688
689 if (sdi->status != SR_ST_ACTIVE)
690 return SR_ERR_DEV_CLOSED;
691
41812aca 692 drvc = di->context;
7b5daad4
MC
693 devc = sdi->priv;
694 usb = sdi->conn;
695
96484e22 696 /* Configures devc->cur_channels. */
ba7dd8bb
UH
697 if (configure_channels(sdi) != SR_OK) {
698 sr_err("Failed to configure channels.");
7b5daad4
MC
699 return SR_ERR;
700 }
701
863357fb 702 devc->sent_samples = 0;
7b5daad4
MC
703 devc->empty_transfer_count = 0;
704 devc->cur_channel = 0;
705 memset(devc->channel_data, 0, sizeof(devc->channel_data));
706
0812c40e 707 if ((trigger = sr_session_trigger_get(sdi->session))) {
5a971f66
AJ
708 int pre_trigger_samples = 0;
709 if (devc->limit_samples > 0)
710 pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
711 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
98fec29e 712 if (!devc->stl)
5a971f66 713 return SR_ERR_MALLOC;
863357fb
BV
714 devc->trigger_fired = FALSE;
715 } else
716 devc->trigger_fired = TRUE;
717
7b5daad4
MC
718 timeout = get_timeout(devc);
719 num_transfers = get_number_of_transfers(devc);
720 size = get_buffer_size(devc);
721 convsize = (size / devc->num_channels + 2) * 16;
722 devc->submitted_transfers = 0;
7b5daad4
MC
723
724 devc->convbuffer_size = convsize;
725 if (!(devc->convbuffer = g_try_malloc(convsize))) {
726 sr_err("Conversion buffer malloc failed.");
727 return SR_ERR_MALLOC;
728 }
729
730 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
731 if (!devc->transfers) {
732 sr_err("USB transfers malloc failed.");
733 g_free(devc->convbuffer);
734 return SR_ERR_MALLOC;
735 }
736
96484e22
UH
737 if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
738 devc->cur_channels)) != SR_OK) {
7b5daad4
MC
739 g_free(devc->transfers);
740 g_free(devc->convbuffer);
741 return ret;
742 }
743
744 devc->num_transfers = num_transfers;
745 for (i = 0; i < num_transfers; i++) {
746 if (!(buf = g_try_malloc(size))) {
747 sr_err("USB transfer buffer malloc failed.");
748 if (devc->submitted_transfers)
749 abort_acquisition(devc);
750 else {
751 g_free(devc->transfers);
752 g_free(devc->convbuffer);
753 }
754 return SR_ERR_MALLOC;
755 }
756 transfer = libusb_alloc_transfer(0);
757 libusb_fill_bulk_transfer(transfer, usb->devhdl,
758 2 | LIBUSB_ENDPOINT_IN, buf, size,
102f1239 759 logic16_receive_transfer, (void *)sdi, timeout);
7b5daad4
MC
760 if ((ret = libusb_submit_transfer(transfer)) != 0) {
761 sr_err("Failed to submit transfer: %s.",
762 libusb_error_name(ret));
763 libusb_free_transfer(transfer);
764 g_free(buf);
765 abort_acquisition(devc);
766 return SR_ERR;
767 }
768 devc->transfers[i] = transfer;
769 devc->submitted_transfers++;
770 }
771
6c60facc
ML
772 devc->ctx = drvc->sr_ctx;
773
102f1239 774 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi);
7b5daad4 775
695dc859 776 std_session_send_df_header(sdi, LOG_PREFIX);
7b5daad4 777
96484e22 778 if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
7b5daad4
MC
779 abort_acquisition(devc);
780 return ret;
781 }
c463dcf0
MC
782
783 return SR_OK;
784}
785
695dc859 786static int dev_acquisition_stop(struct sr_dev_inst *sdi)
c463dcf0 787{
7b5daad4
MC
788 int ret;
789
c463dcf0
MC
790 if (sdi->status != SR_ST_ACTIVE)
791 return SR_ERR_DEV_CLOSED;
792
96484e22 793 ret = logic16_abort_acquisition(sdi);
c463dcf0 794
7b5daad4
MC
795 abort_acquisition(sdi->priv);
796
797 return ret;
c463dcf0
MC
798}
799
800SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
801 .name = "saleae-logic16",
802 .longname = "Saleae Logic16",
803 .api_version = 1,
804 .init = init,
700d6b64 805 .cleanup = std_cleanup,
c463dcf0
MC
806 .scan = scan,
807 .dev_list = dev_list,
a6630742 808 .dev_clear = NULL,
c463dcf0
MC
809 .config_get = config_get,
810 .config_set = config_set,
811 .config_list = config_list,
812 .dev_open = dev_open,
813 .dev_close = dev_close,
814 .dev_acquisition_start = dev_acquisition_start,
815 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 816 .context = NULL,
c463dcf0 817};