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