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