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