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