]> sigrok.org Git - libsigrok.git/blob - src/hardware/saleae-logic16/api.c
Drop unneeded std_session_send_df_header() comments.
[libsigrok.git] / src / hardware / saleae-logic16 / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
5  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
6  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <glib.h>
24 #include <libusb.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
30 #include "protocol.h"
31
32 #define LOGIC16_VID             0x21a9
33 #define LOGIC16_PID             0x1001
34
35 #define USB_INTERFACE           0
36 #define USB_CONFIGURATION       1
37 #define FX2_FIRMWARE            "saleae-logic16-fx2.fw"
38
39 #define MAX_RENUM_DELAY_MS      3000
40 #define NUM_SIMUL_TRANSFERS     32
41
42 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info;
43
44 static const uint32_t scanopts[] = {
45         SR_CONF_CONN,
46 };
47
48 static const uint32_t devopts[] = {
49         SR_CONF_LOGIC_ANALYZER,
50         SR_CONF_CONTINUOUS,
51         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
52         SR_CONF_CONN | SR_CONF_GET,
53         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
54         SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55         SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
56         SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
57 };
58
59 static const int32_t soft_trigger_matches[] = {
60         SR_TRIGGER_ZERO,
61         SR_TRIGGER_ONE,
62         SR_TRIGGER_RISING,
63         SR_TRIGGER_FALLING,
64         SR_TRIGGER_EDGE,
65 };
66
67 static const char *channel_names[] = {
68         "0", "1", "2", "3", "4", "5", "6", "7", "8",
69         "9", "10", "11", "12", "13", "14", "15",
70 };
71
72 static const struct {
73         enum voltage_range range;
74         gdouble low;
75         gdouble high;
76 } volt_thresholds[] = {
77         { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 },
78         { VOLTAGE_RANGE_5_V,     1.4, 3.6 },
79 };
80
81 static const uint64_t samplerates[] = {
82         SR_KHZ(500),
83         SR_MHZ(1),
84         SR_MHZ(2),
85         SR_MHZ(4),
86         SR_MHZ(5),
87         SR_MHZ(8),
88         SR_MHZ(10),
89         SR_KHZ(12500),
90         SR_MHZ(16),
91         SR_MHZ(25),
92         SR_MHZ(32),
93         SR_MHZ(40),
94         SR_MHZ(80),
95         SR_MHZ(100),
96 };
97
98 static int init(struct sr_dev_driver *di, 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                 libusb_get_device_descriptor(dev, &des);
115
116                 if (libusb_open(dev, &hdl) != 0)
117                         break;
118
119                 if (libusb_get_string_descriptor_ascii(hdl,
120                     des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
121                         break;
122                 if (strcmp((const char *)strdesc, "Saleae LLC"))
123                         break;
124
125                 if (libusb_get_string_descriptor_ascii(hdl,
126                     des.iProduct, strdesc, sizeof(strdesc)) < 0)
127                         break;
128                 if (strcmp((const char *)strdesc, "Logic S/16"))
129                         break;
130
131                 /* If we made it here, it must be a configured Logic16. */
132                 ret = TRUE;
133         }
134         if (hdl)
135                 libusb_close(hdl);
136
137         return ret;
138 }
139
140 static GSList *scan(struct sr_dev_driver *di, GSList *options)
141 {
142         struct drv_context *drvc;
143         struct dev_context *devc;
144         struct sr_dev_inst *sdi;
145         struct sr_usb_dev_inst *usb;
146         struct sr_config *src;
147         GSList *l, *devices, *conn_devices;
148         struct libusb_device_descriptor des;
149         libusb_device **devlist;
150         unsigned int i, j;
151         const char *conn;
152         char connection_id[64];
153
154         drvc = di->context;
155
156         conn = NULL;
157         for (l = options; l; l = l->next) {
158                 src = l->data;
159                 switch (src->key) {
160                 case SR_CONF_CONN:
161                         conn = g_variant_get_string(src->data, NULL);
162                         break;
163                 }
164         }
165         if (conn)
166                 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
167         else
168                 conn_devices = NULL;
169
170         /* Find all Logic16 devices and upload firmware to them. */
171         devices = NULL;
172         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
173         for (i = 0; devlist[i]; i++) {
174                 if (conn) {
175                         usb = NULL;
176                         for (l = conn_devices; l; l = l->next) {
177                                 usb = l->data;
178                                 if (usb->bus == libusb_get_bus_number(devlist[i])
179                                     && usb->address == libusb_get_device_address(devlist[i]))
180                                         break;
181                         }
182                         if (!l)
183                                 /* This device matched none of the ones that
184                                  * matched the conn specification. */
185                                 continue;
186                 }
187
188                 libusb_get_device_descriptor(devlist[i], &des);
189
190                 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
191
192                 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
193                         continue;
194
195                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
196                 sdi->status = SR_ST_INITIALIZING;
197                 sdi->vendor = g_strdup("Saleae");
198                 sdi->model = g_strdup("Logic16");
199                 sdi->driver = di;
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                 drvc->instances = g_slist_append(drvc->instances, sdi);
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 devices;
236 }
237
238 static GSList *dev_list(const struct sr_dev_driver *di)
239 {
240         return ((struct drv_context *)(di->context))->instances;
241 }
242
243 static int logic16_dev_open(struct sr_dev_inst *sdi)
244 {
245         struct sr_dev_driver *di;
246         libusb_device **devlist;
247         struct sr_usb_dev_inst *usb;
248         struct libusb_device_descriptor des;
249         struct drv_context *drvc;
250         int ret, i, device_count;
251         char connection_id[64];
252
253         di = sdi->driver;
254         drvc = di->context;
255         usb = sdi->conn;
256
257         if (sdi->status == SR_ST_ACTIVE)
258                 /* Device is already in use. */
259                 return SR_ERR;
260
261         device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
262         if (device_count < 0) {
263                 sr_err("Failed to get device list: %s.",
264                        libusb_error_name(device_count));
265                 return SR_ERR;
266         }
267
268         for (i = 0; i < device_count; i++) {
269                 libusb_get_device_descriptor(devlist[i], &des);
270
271                 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
272                         continue;
273
274                 if ((sdi->status == SR_ST_INITIALIZING) ||
275                                 (sdi->status == SR_ST_INACTIVE)) {
276                         /*
277                          * Check device by its physical USB bus/port address.
278                          */
279                         usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
280                         if (strcmp(sdi->connection_id, connection_id))
281                                 /* This is not the one. */
282                                 continue;
283                 }
284
285                 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
286                         if (usb->address == 0xff)
287                                 /*
288                                  * First time we touch this device after FW
289                                  * upload, so we don't know the address yet.
290                                  */
291                                 usb->address = libusb_get_device_address(devlist[i]);
292                 } else {
293                         sr_err("Failed to open device: %s.",
294                                libusb_error_name(ret));
295                         break;
296                 }
297
298                 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
299                 if (ret == LIBUSB_ERROR_BUSY) {
300                         sr_err("Unable to claim USB interface. Another "
301                                "program or driver has already claimed it.");
302                         break;
303                 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
304                         sr_err("Device has been disconnected.");
305                         break;
306                 } else if (ret != 0) {
307                         sr_err("Unable to claim interface: %s.",
308                                libusb_error_name(ret));
309                         break;
310                 }
311
312                 if ((ret = logic16_init_device(sdi)) != SR_OK) {
313                         sr_err("Failed to init device.");
314                         break;
315                 }
316
317                 sdi->status = SR_ST_ACTIVE;
318                 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
319                         usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
320
321                 break;
322         }
323         libusb_free_device_list(devlist, 1);
324
325         if (sdi->status != SR_ST_ACTIVE) {
326                 if (usb->devhdl) {
327                         libusb_release_interface(usb->devhdl, USB_INTERFACE);
328                         libusb_close(usb->devhdl);
329                         usb->devhdl = NULL;
330                 }
331                 return SR_ERR;
332         }
333
334         return SR_OK;
335 }
336
337 static int dev_open(struct sr_dev_inst *sdi)
338 {
339         struct dev_context *devc;
340         int ret;
341         int64_t timediff_us, timediff_ms;
342
343         devc = sdi->priv;
344
345         /*
346          * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
347          * milliseconds for the FX2 to renumerate.
348          */
349         ret = SR_ERR;
350         if (devc->fw_updated > 0) {
351                 sr_info("Waiting for device to reset.");
352                 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
353                 g_usleep(300 * 1000);
354                 timediff_ms = 0;
355                 while (timediff_ms < MAX_RENUM_DELAY_MS) {
356                         if ((ret = logic16_dev_open(sdi)) == SR_OK)
357                                 break;
358                         g_usleep(100 * 1000);
359
360                         timediff_us = g_get_monotonic_time() - devc->fw_updated;
361                         timediff_ms = timediff_us / 1000;
362                         sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
363                 }
364                 if (ret != SR_OK) {
365                         sr_err("Device failed to renumerate.");
366                         return SR_ERR;
367                 }
368                 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
369         } else {
370                 sr_info("Firmware upload was not needed.");
371                 ret = logic16_dev_open(sdi);
372         }
373
374         if (ret != SR_OK) {
375                 sr_err("Unable to open device.");
376                 return SR_ERR;
377         }
378
379         if (devc->cur_samplerate == 0) {
380                 /* Samplerate hasn't been set; default to the slowest one. */
381                 devc->cur_samplerate = samplerates[0];
382         }
383
384         return SR_OK;
385 }
386
387 static int dev_close(struct sr_dev_inst *sdi)
388 {
389         struct sr_usb_dev_inst *usb;
390
391         usb = sdi->conn;
392         if (!usb->devhdl)
393                 return SR_ERR;
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         sdi->status = SR_ST_INACTIVE;
401
402         return SR_OK;
403 }
404
405 static int cleanup(const struct sr_dev_driver *di)
406 {
407         int ret;
408         struct drv_context *drvc;
409
410         if (!(drvc = di->context))
411                 /* Can get called on an unused driver, doesn't matter. */
412                 return SR_OK;
413
414         ret = std_dev_clear(di, NULL);
415         g_free(drvc);
416
417         return ret;
418 }
419
420 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
421                 const struct sr_channel_group *cg)
422 {
423         struct dev_context *devc;
424         struct sr_usb_dev_inst *usb;
425         GVariant *range[2];
426         char str[128];
427         int ret;
428         unsigned int i;
429
430         (void)cg;
431
432         ret = SR_OK;
433         switch (key) {
434         case SR_CONF_CONN:
435                 if (!sdi || !sdi->conn)
436                         return SR_ERR_ARG;
437                 usb = sdi->conn;
438                 if (usb->address == 255)
439                         /* Device still needs to re-enumerate after firmware
440                          * upload, so we don't know its (future) address. */
441                         return SR_ERR;
442                 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
443                 *data = g_variant_new_string(str);
444                 break;
445         case SR_CONF_SAMPLERATE:
446                 if (!sdi)
447                         return SR_ERR;
448                 devc = sdi->priv;
449                 *data = g_variant_new_uint64(devc->cur_samplerate);
450                 break;
451         case SR_CONF_CAPTURE_RATIO:
452                 if (!sdi)
453                         return SR_ERR;
454                 devc = sdi->priv;
455                 *data = g_variant_new_uint64(devc->capture_ratio);
456                 break;
457         case SR_CONF_VOLTAGE_THRESHOLD:
458                 if (!sdi)
459                         return SR_ERR;
460                 devc = sdi->priv;
461                 ret = SR_ERR;
462                 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
463                         if (devc->selected_voltage_range !=
464                             volt_thresholds[i].range)
465                                 continue;
466                         range[0] = g_variant_new_double(volt_thresholds[i].low);
467                         range[1] = g_variant_new_double(volt_thresholds[i].high);
468                         *data = g_variant_new_tuple(range, 2);
469                         ret = SR_OK;
470                         break;
471                 }
472                 break;
473         default:
474                 return SR_ERR_NA;
475         }
476
477         return ret;
478 }
479
480 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
481                 const struct sr_channel_group *cg)
482 {
483         struct dev_context *devc;
484         gdouble low, high;
485         int ret;
486         unsigned int i;
487
488         (void)cg;
489
490         if (sdi->status != SR_ST_ACTIVE)
491                 return SR_ERR_DEV_CLOSED;
492
493         devc = sdi->priv;
494
495         ret = SR_OK;
496         switch (key) {
497         case SR_CONF_SAMPLERATE:
498                 devc->cur_samplerate = g_variant_get_uint64(data);
499                 break;
500         case SR_CONF_LIMIT_SAMPLES:
501                 devc->limit_samples = g_variant_get_uint64(data);
502                 break;
503         case SR_CONF_CAPTURE_RATIO:
504                 devc->capture_ratio = g_variant_get_uint64(data);
505                 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
506                 break;
507         case SR_CONF_VOLTAGE_THRESHOLD:
508                 g_variant_get(data, "(dd)", &low, &high);
509                 ret = SR_ERR_ARG;
510                 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
511                         if (fabs(volt_thresholds[i].low - low) < 0.1 &&
512                             fabs(volt_thresholds[i].high - high) < 0.1) {
513                                 devc->selected_voltage_range =
514                                         volt_thresholds[i].range;
515                                 ret = SR_OK;
516                                 break;
517                         }
518                 }
519                 break;
520         default:
521                 ret = SR_ERR_NA;
522         }
523
524         return ret;
525 }
526
527 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
528                 const struct sr_channel_group *cg)
529 {
530         GVariant *gvar, *range[2];
531         GVariantBuilder gvb;
532         int ret;
533         unsigned int i;
534
535         (void)sdi;
536         (void)cg;
537
538         ret = SR_OK;
539         switch (key) {
540         case SR_CONF_SCAN_OPTIONS:
541                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
542                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
543                 break;
544         case SR_CONF_DEVICE_OPTIONS:
545                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
546                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
547                 break;
548         case SR_CONF_SAMPLERATE:
549                 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
550                 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
551                         samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
552                 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
553                 *data = g_variant_builder_end(&gvb);
554                 break;
555         case SR_CONF_VOLTAGE_THRESHOLD:
556                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
557                 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
558                         range[0] = g_variant_new_double(volt_thresholds[i].low);
559                         range[1] = g_variant_new_double(volt_thresholds[i].high);
560                         gvar = g_variant_new_tuple(range, 2);
561                         g_variant_builder_add_value(&gvb, gvar);
562                 }
563                 *data = g_variant_builder_end(&gvb);
564                 break;
565         case SR_CONF_TRIGGER_MATCH:
566                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
567                                 soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches),
568                                 sizeof(int32_t));
569                 break;
570         default:
571                 return SR_ERR_NA;
572         }
573
574         return ret;
575 }
576
577 static void abort_acquisition(struct dev_context *devc)
578 {
579         int i;
580
581         devc->sent_samples = -1;
582
583         for (i = devc->num_transfers - 1; i >= 0; i--) {
584                 if (devc->transfers[i])
585                         libusb_cancel_transfer(devc->transfers[i]);
586         }
587 }
588
589 static unsigned int bytes_per_ms(struct dev_context *devc)
590 {
591         return devc->cur_samplerate * devc->num_channels / 8000;
592 }
593
594 static size_t get_buffer_size(struct dev_context *devc)
595 {
596         size_t s;
597
598         /*
599          * The buffer should be large enough to hold 10ms of data and
600          * a multiple of 512.
601          */
602         s = 10 * bytes_per_ms(devc);
603         return (s + 511) & ~511;
604 }
605
606 static unsigned int get_number_of_transfers(struct dev_context *devc)
607 {
608         unsigned int n;
609
610         /* Total buffer size should be able to hold about 500ms of data. */
611         n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
612
613         if (n > NUM_SIMUL_TRANSFERS)
614                 return NUM_SIMUL_TRANSFERS;
615
616         return n;
617 }
618
619 static unsigned int get_timeout(struct dev_context *devc)
620 {
621         size_t total_size;
622         unsigned int timeout;
623
624         total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
625         timeout = total_size / bytes_per_ms(devc);
626         return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
627 }
628
629 static int configure_channels(const struct sr_dev_inst *sdi)
630 {
631         struct dev_context *devc;
632         struct sr_channel *ch;
633         GSList *l;
634         uint16_t channel_bit;
635
636         devc = sdi->priv;
637
638         devc->cur_channels = 0;
639         devc->num_channels = 0;
640         for (l = sdi->channels; l; l = l->next) {
641                 ch = (struct sr_channel *)l->data;
642                 if (ch->enabled == FALSE)
643                         continue;
644
645                 channel_bit = 1 << (ch->index);
646
647                 devc->cur_channels |= channel_bit;
648
649 #ifdef WORDS_BIGENDIAN
650                 /*
651                  * Output logic data should be stored in little endian format.
652                  * To speed things up during conversion, do the switcharoo
653                  * here instead.
654                  */
655                 channel_bit = 1 << (ch->index ^ 8);
656 #endif
657
658                 devc->channel_masks[devc->num_channels++] = channel_bit;
659         }
660
661         return SR_OK;
662 }
663
664 static int receive_data(int fd, int revents, void *cb_data)
665 {
666         struct timeval tv;
667         struct dev_context *devc;
668         struct drv_context *drvc;
669         const struct sr_dev_inst *sdi;
670         struct sr_dev_driver *di;
671
672         (void)fd;
673         (void)revents;
674
675         sdi = cb_data;
676         di = sdi->driver;
677         drvc = di->context;
678         devc = sdi->priv;
679
680         tv.tv_sec = tv.tv_usec = 0;
681         libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
682
683         if (devc->sent_samples == -2) {
684                 logic16_abort_acquisition(sdi);
685                 abort_acquisition(devc);
686         }
687
688         return TRUE;
689 }
690
691 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
692 {
693         struct sr_dev_driver *di = sdi->driver;
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->context;
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                 int pre_trigger_samples = 0;
725                 if (devc->limit_samples > 0)
726                         pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
727                 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
728                 if (!devc->stl)
729                         return SR_ERR_MALLOC;
730                 devc->trigger_fired = FALSE;
731         } else
732                 devc->trigger_fired = TRUE;
733
734         timeout = get_timeout(devc);
735         num_transfers = get_number_of_transfers(devc);
736         size = get_buffer_size(devc);
737         convsize = (size / devc->num_channels + 2) * 16;
738         devc->submitted_transfers = 0;
739
740         devc->convbuffer_size = convsize;
741         if (!(devc->convbuffer = g_try_malloc(convsize))) {
742                 sr_err("Conversion buffer malloc failed.");
743                 return SR_ERR_MALLOC;
744         }
745
746         devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
747         if (!devc->transfers) {
748                 sr_err("USB transfers malloc failed.");
749                 g_free(devc->convbuffer);
750                 return SR_ERR_MALLOC;
751         }
752
753         if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
754                                              devc->cur_channels)) != SR_OK) {
755                 g_free(devc->transfers);
756                 g_free(devc->convbuffer);
757                 return ret;
758         }
759
760         devc->num_transfers = num_transfers;
761         for (i = 0; i < num_transfers; i++) {
762                 if (!(buf = g_try_malloc(size))) {
763                         sr_err("USB transfer buffer malloc failed.");
764                         if (devc->submitted_transfers)
765                                 abort_acquisition(devc);
766                         else {
767                                 g_free(devc->transfers);
768                                 g_free(devc->convbuffer);
769                         }
770                         return SR_ERR_MALLOC;
771                 }
772                 transfer = libusb_alloc_transfer(0);
773                 libusb_fill_bulk_transfer(transfer, usb->devhdl,
774                                 2 | LIBUSB_ENDPOINT_IN, buf, size,
775                                 logic16_receive_transfer, (void *)sdi, timeout);
776                 if ((ret = libusb_submit_transfer(transfer)) != 0) {
777                         sr_err("Failed to submit transfer: %s.",
778                                libusb_error_name(ret));
779                         libusb_free_transfer(transfer);
780                         g_free(buf);
781                         abort_acquisition(devc);
782                         return SR_ERR;
783                 }
784                 devc->transfers[i] = transfer;
785                 devc->submitted_transfers++;
786         }
787
788         devc->ctx = drvc->sr_ctx;
789
790         usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi);
791
792         std_session_send_df_header(cb_data, LOG_PREFIX);
793
794         if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
795                 abort_acquisition(devc);
796                 return ret;
797         }
798
799         return SR_OK;
800 }
801
802 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
803 {
804         int ret;
805
806         (void)cb_data;
807
808         if (sdi->status != SR_ST_ACTIVE)
809                 return SR_ERR_DEV_CLOSED;
810
811         ret = logic16_abort_acquisition(sdi);
812
813         abort_acquisition(sdi->priv);
814
815         return ret;
816 }
817
818 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
819         .name = "saleae-logic16",
820         .longname = "Saleae Logic16",
821         .api_version = 1,
822         .init = init,
823         .cleanup = cleanup,
824         .scan = scan,
825         .dev_list = dev_list,
826         .dev_clear = NULL,
827         .config_get = config_get,
828         .config_set = config_set,
829         .config_list = config_list,
830         .dev_open = dev_open,
831         .dev_close = dev_close,
832         .dev_acquisition_start = dev_acquisition_start,
833         .dev_acquisition_stop = dev_acquisition_stop,
834         .context = NULL,
835 };