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