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