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