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