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