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