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