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