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