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