]> sigrok.org Git - libsigrok.git/blob - src/hardware/saleae-logic16/api.c
drivers: SR_CONF_CAPTURE_RATIO fixes.
[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 } volt_thresholds_ranges[] = {
76         { VOLTAGE_RANGE_18_33_V, },
77         { VOLTAGE_RANGE_5_V, },
78 };
79
80 static const double volt_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                 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
192
193                 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
194                         continue;
195
196                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
197                 sdi->status = SR_ST_INITIALIZING;
198                 sdi->vendor = g_strdup("Saleae");
199                 sdi->model = g_strdup("Logic16");
200                 sdi->connection_id = g_strdup(connection_id);
201
202                 for (j = 0; j < ARRAY_SIZE(channel_names); j++)
203                         sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
204                                         channel_names[j]);
205
206                 devc = g_malloc0(sizeof(struct dev_context));
207                 devc->selected_voltage_range = VOLTAGE_RANGE_18_33_V;
208                 sdi->priv = devc;
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(drvc->sr_ctx, devlist[i],
221                                         USB_CONFIGURATION, 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.");
226                         sdi->inst_type = SR_INST_USB;
227                         sdi->conn = sr_usb_dev_inst_new(
228                                 libusb_get_bus_number(devlist[i]), 0xff, NULL);
229                 }
230         }
231         libusb_free_device_list(devlist, 1);
232         g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
233
234         return std_scan_complete(di, devices);
235 }
236
237 static int logic16_dev_open(struct sr_dev_inst *sdi)
238 {
239         struct sr_dev_driver *di;
240         libusb_device **devlist;
241         struct sr_usb_dev_inst *usb;
242         struct libusb_device_descriptor des;
243         struct drv_context *drvc;
244         int ret = SR_ERR, i, device_count;
245         char connection_id[64];
246
247         di = sdi->driver;
248         drvc = di->context;
249         usb = sdi->conn;
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                         ret = SR_ERR;
286                         break;
287                 }
288
289                 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
290                 if (ret == LIBUSB_ERROR_BUSY) {
291                         sr_err("Unable to claim USB interface. Another "
292                                "program or driver has already claimed it.");
293                         ret = SR_ERR;
294                         break;
295                 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
296                         sr_err("Device has been disconnected.");
297                         ret = SR_ERR;
298                         break;
299                 } else if (ret != 0) {
300                         sr_err("Unable to claim interface: %s.",
301                                libusb_error_name(ret));
302                         ret = SR_ERR;
303                         break;
304                 }
305
306                 if ((ret = logic16_init_device(sdi)) != SR_OK) {
307                         sr_err("Failed to init device.");
308                         break;
309                 }
310
311                 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
312                         usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
313
314                 ret = SR_OK;
315
316                 break;
317         }
318
319         libusb_free_device_list(devlist, 1);
320
321         if (ret != SR_OK) {
322                 if (usb->devhdl) {
323                         libusb_release_interface(usb->devhdl, USB_INTERFACE);
324                         libusb_close(usb->devhdl);
325                         usb->devhdl = NULL;
326                 }
327                 return SR_ERR;
328         }
329
330         return SR_OK;
331 }
332
333 static int dev_open(struct sr_dev_inst *sdi)
334 {
335         struct dev_context *devc;
336         int ret;
337         int64_t timediff_us, timediff_ms;
338
339         devc = sdi->priv;
340
341         /*
342          * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
343          * milliseconds for the FX2 to renumerate.
344          */
345         ret = SR_ERR;
346         if (devc->fw_updated > 0) {
347                 sr_info("Waiting for device to reset.");
348                 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
349                 g_usleep(300 * 1000);
350                 timediff_ms = 0;
351                 while (timediff_ms < MAX_RENUM_DELAY_MS) {
352                         if ((ret = logic16_dev_open(sdi)) == SR_OK)
353                                 break;
354                         g_usleep(100 * 1000);
355
356                         timediff_us = g_get_monotonic_time() - devc->fw_updated;
357                         timediff_ms = timediff_us / 1000;
358                         sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
359                 }
360                 if (ret != SR_OK) {
361                         sr_err("Device failed to renumerate.");
362                         return SR_ERR;
363                 }
364                 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
365         } else {
366                 sr_info("Firmware upload was not needed.");
367                 ret = logic16_dev_open(sdi);
368         }
369
370         if (ret != SR_OK) {
371                 sr_err("Unable to open device.");
372                 return SR_ERR;
373         }
374
375         if (devc->cur_samplerate == 0) {
376                 /* Samplerate hasn't been set; default to the slowest one. */
377                 devc->cur_samplerate = samplerates[0];
378         }
379
380         return SR_OK;
381 }
382
383 static int dev_close(struct sr_dev_inst *sdi)
384 {
385         struct sr_usb_dev_inst *usb;
386
387         usb = sdi->conn;
388
389         if (!usb->devhdl)
390                 return SR_ERR_BUG;
391
392         sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
393                 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
394         libusb_release_interface(usb->devhdl, USB_INTERFACE);
395         libusb_close(usb->devhdl);
396         usb->devhdl = NULL;
397
398         return SR_OK;
399 }
400
401 static int config_get(uint32_t key, GVariant **data,
402         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
403 {
404         struct dev_context *devc;
405         struct sr_usb_dev_inst *usb;
406         unsigned int i;
407
408         (void)cg;
409
410         switch (key) {
411         case SR_CONF_CONN:
412                 if (!sdi || !sdi->conn)
413                         return SR_ERR_ARG;
414                 usb = sdi->conn;
415                 if (usb->address == 255)
416                         /* Device still needs to re-enumerate after firmware
417                          * upload, so we don't know its (future) address. */
418                         return SR_ERR;
419                 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
420                 break;
421         case SR_CONF_SAMPLERATE:
422                 if (!sdi)
423                         return SR_ERR;
424                 devc = sdi->priv;
425                 *data = g_variant_new_uint64(devc->cur_samplerate);
426                 break;
427         case SR_CONF_CAPTURE_RATIO:
428                 if (!sdi)
429                         return SR_ERR;
430                 devc = sdi->priv;
431                 *data = g_variant_new_uint64(devc->capture_ratio);
432                 break;
433         case SR_CONF_VOLTAGE_THRESHOLD:
434                 if (!sdi)
435                         return SR_ERR;
436                 devc = sdi->priv;
437                 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
438                         if (devc->selected_voltage_range !=
439                             volt_thresholds_ranges[i].range)
440                                 continue;
441                         *data = std_gvar_tuple_double(volt_thresholds[i][0], volt_thresholds[i][1]);
442                         return SR_OK;
443                 }
444                 return SR_ERR;
445         default:
446                 return SR_ERR_NA;
447         }
448
449         return SR_OK;
450 }
451
452 static int config_set(uint32_t key, GVariant *data,
453         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
454 {
455         struct dev_context *devc;
456         int idx;
457
458         (void)cg;
459
460         devc = sdi->priv;
461
462         switch (key) {
463         case SR_CONF_SAMPLERATE:
464                 devc->cur_samplerate = g_variant_get_uint64(data);
465                 break;
466         case SR_CONF_LIMIT_SAMPLES:
467                 devc->limit_samples = g_variant_get_uint64(data);
468                 break;
469         case SR_CONF_CAPTURE_RATIO:
470                 devc->capture_ratio = g_variant_get_uint64(data);
471                 break;
472         case SR_CONF_VOLTAGE_THRESHOLD:
473                 if ((idx = std_double_tuple_idx(data, ARRAY_AND_SIZE(volt_thresholds))) < 0)
474                         return SR_ERR_ARG;
475                 devc->selected_voltage_range = volt_thresholds_ranges[idx].range;
476                 break;
477         default:
478                 return SR_ERR_NA;
479         }
480
481         return SR_OK;
482 }
483
484 static int config_list(uint32_t key, GVariant **data,
485         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
486 {
487         switch (key) {
488         case SR_CONF_SCAN_OPTIONS:
489         case SR_CONF_DEVICE_OPTIONS:
490                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
491         case SR_CONF_SAMPLERATE:
492                 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
493                 break;
494         case SR_CONF_VOLTAGE_THRESHOLD:
495                 *data = std_gvar_thresholds(ARRAY_AND_SIZE(volt_thresholds));
496                 break;
497         case SR_CONF_TRIGGER_MATCH:
498                 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
499                 break;
500         default:
501                 return SR_ERR_NA;
502         }
503
504         return SR_OK;
505 }
506
507 static void abort_acquisition(struct dev_context *devc)
508 {
509         int i;
510
511         devc->sent_samples = -1;
512
513         for (i = devc->num_transfers - 1; i >= 0; i--) {
514                 if (devc->transfers[i])
515                         libusb_cancel_transfer(devc->transfers[i]);
516         }
517 }
518
519 static unsigned int bytes_per_ms(struct dev_context *devc)
520 {
521         return devc->cur_samplerate * devc->num_channels / 8000;
522 }
523
524 static size_t get_buffer_size(struct dev_context *devc)
525 {
526         size_t s;
527
528         /*
529          * The buffer should be large enough to hold 10ms of data and
530          * a multiple of 512.
531          */
532         s = 10 * bytes_per_ms(devc);
533         return (s + 511) & ~511;
534 }
535
536 static unsigned int get_number_of_transfers(struct dev_context *devc)
537 {
538         unsigned int n;
539
540         /* Total buffer size should be able to hold about 500ms of data. */
541         n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
542
543         if (n > NUM_SIMUL_TRANSFERS)
544                 return NUM_SIMUL_TRANSFERS;
545
546         return n;
547 }
548
549 static unsigned int get_timeout(struct dev_context *devc)
550 {
551         size_t total_size;
552         unsigned int timeout;
553
554         total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
555         timeout = total_size / bytes_per_ms(devc);
556         return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
557 }
558
559 static int configure_channels(const struct sr_dev_inst *sdi)
560 {
561         struct dev_context *devc;
562         struct sr_channel *ch;
563         GSList *l;
564         uint16_t channel_bit;
565
566         devc = sdi->priv;
567
568         devc->cur_channels = 0;
569         devc->num_channels = 0;
570         for (l = sdi->channels; l; l = l->next) {
571                 ch = (struct sr_channel *)l->data;
572                 if (ch->enabled == FALSE)
573                         continue;
574
575                 channel_bit = 1 << (ch->index);
576
577                 devc->cur_channels |= channel_bit;
578
579 #ifdef WORDS_BIGENDIAN
580                 /*
581                  * Output logic data should be stored in little endian format.
582                  * To speed things up during conversion, do the switcharoo
583                  * here instead.
584                  */
585                 channel_bit = 1 << (ch->index ^ 8);
586 #endif
587
588                 devc->channel_masks[devc->num_channels++] = channel_bit;
589         }
590
591         return SR_OK;
592 }
593
594 static int receive_data(int fd, int revents, void *cb_data)
595 {
596         struct timeval tv;
597         struct dev_context *devc;
598         struct drv_context *drvc;
599         const struct sr_dev_inst *sdi;
600         struct sr_dev_driver *di;
601
602         (void)fd;
603         (void)revents;
604
605         sdi = cb_data;
606         di = sdi->driver;
607         drvc = di->context;
608         devc = sdi->priv;
609
610         tv.tv_sec = tv.tv_usec = 0;
611         libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
612
613         if (devc->sent_samples == -2) {
614                 logic16_abort_acquisition(sdi);
615                 abort_acquisition(devc);
616         }
617
618         return TRUE;
619 }
620
621 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
622 {
623         struct sr_dev_driver *di = sdi->driver;
624         struct dev_context *devc;
625         struct drv_context *drvc;
626         struct sr_usb_dev_inst *usb;
627         struct sr_trigger *trigger;
628         struct libusb_transfer *transfer;
629         unsigned int i, timeout, num_transfers;
630         int ret;
631         unsigned char *buf;
632         size_t size, convsize;
633
634         drvc = di->context;
635         devc = sdi->priv;
636         usb = sdi->conn;
637
638         /* Configures devc->cur_channels. */
639         if (configure_channels(sdi) != SR_OK) {
640                 sr_err("Failed to configure channels.");
641                 return SR_ERR;
642         }
643
644         devc->sent_samples = 0;
645         devc->empty_transfer_count = 0;
646         devc->cur_channel = 0;
647         memset(devc->channel_data, 0, sizeof(devc->channel_data));
648
649         if ((trigger = sr_session_trigger_get(sdi->session))) {
650                 int pre_trigger_samples = 0;
651                 if (devc->limit_samples > 0)
652                         pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
653                 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
654                 if (!devc->stl)
655                         return SR_ERR_MALLOC;
656                 devc->trigger_fired = FALSE;
657         } else
658                 devc->trigger_fired = TRUE;
659
660         timeout = get_timeout(devc);
661         num_transfers = get_number_of_transfers(devc);
662         size = get_buffer_size(devc);
663         convsize = (size / devc->num_channels + 2) * 16;
664         devc->submitted_transfers = 0;
665
666         devc->convbuffer_size = convsize;
667         if (!(devc->convbuffer = g_try_malloc(convsize))) {
668                 sr_err("Conversion buffer malloc failed.");
669                 return SR_ERR_MALLOC;
670         }
671
672         devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
673         if (!devc->transfers) {
674                 sr_err("USB transfers malloc failed.");
675                 g_free(devc->convbuffer);
676                 return SR_ERR_MALLOC;
677         }
678
679         if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
680                                              devc->cur_channels)) != SR_OK) {
681                 g_free(devc->transfers);
682                 g_free(devc->convbuffer);
683                 return ret;
684         }
685
686         devc->num_transfers = num_transfers;
687         for (i = 0; i < num_transfers; i++) {
688                 if (!(buf = g_try_malloc(size))) {
689                         sr_err("USB transfer buffer malloc failed.");
690                         if (devc->submitted_transfers)
691                                 abort_acquisition(devc);
692                         else {
693                                 g_free(devc->transfers);
694                                 g_free(devc->convbuffer);
695                         }
696                         return SR_ERR_MALLOC;
697                 }
698                 transfer = libusb_alloc_transfer(0);
699                 libusb_fill_bulk_transfer(transfer, usb->devhdl,
700                                 2 | LIBUSB_ENDPOINT_IN, buf, size,
701                                 logic16_receive_transfer, (void *)sdi, timeout);
702                 if ((ret = libusb_submit_transfer(transfer)) != 0) {
703                         sr_err("Failed to submit transfer: %s.",
704                                libusb_error_name(ret));
705                         libusb_free_transfer(transfer);
706                         g_free(buf);
707                         abort_acquisition(devc);
708                         return SR_ERR;
709                 }
710                 devc->transfers[i] = transfer;
711                 devc->submitted_transfers++;
712         }
713
714         devc->ctx = drvc->sr_ctx;
715
716         usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi);
717
718         std_session_send_df_header(sdi);
719
720         if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
721                 abort_acquisition(devc);
722                 return ret;
723         }
724
725         return SR_OK;
726 }
727
728 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
729 {
730         int ret;
731
732         ret = logic16_abort_acquisition(sdi);
733
734         abort_acquisition(sdi->priv);
735
736         return ret;
737 }
738
739 static struct sr_dev_driver saleae_logic16_driver_info = {
740         .name = "saleae-logic16",
741         .longname = "Saleae Logic16",
742         .api_version = 1,
743         .init = std_init,
744         .cleanup = std_cleanup,
745         .scan = scan,
746         .dev_list = std_dev_list,
747         .dev_clear = std_dev_clear,
748         .config_get = config_get,
749         .config_set = config_set,
750         .config_list = config_list,
751         .dev_open = dev_open,
752         .dev_close = dev_close,
753         .dev_acquisition_start = dev_acquisition_start,
754         .dev_acquisition_stop = dev_acquisition_stop,
755         .context = NULL,
756 };
757 SR_REGISTER_DEV_DRIVER(saleae_logic16_driver_info);