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