]> sigrok.org Git - libsigrok.git/blame - hardware/saleae-logic16/api.c
rigol-ds1xx2: fix bitrot in device cleanup code
[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>
fec7aa6a
MC
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
6 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
c463dcf0
MC
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
f6a21fa5
MC
22#include <glib.h>
23#include <libusb.h>
24#include <stdlib.h>
25#include <string.h>
db11d7d2 26#include <math.h>
f6a21fa5
MC
27#include "libsigrok.h"
28#include "libsigrok-internal.h"
c463dcf0
MC
29#include "protocol.h"
30
96484e22
UH
31#define LOGIC16_VID 0x21a9
32#define LOGIC16_PID 0x1001
f6a21fa5 33
5eea4305
MC
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
7b5daad4 39#define NUM_SIMUL_TRANSFERS 32
5eea4305 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
d0107565 58static const char *probe_names[] = {
f6a21fa5
MC
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,
96484e22 119 des.iProduct, strdesc, sizeof(strdesc)) < 0)
5eea4305
MC
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
96484e22 181 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
5eea4305
MC
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;
96484e22
UH
216 sdi->conn = sr_usb_dev_inst_new(
217 libusb_get_bus_number(devlist[i]),
218 libusb_get_device_address(devlist[i]), NULL);
5eea4305
MC
219 } else {
220 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
221 FX2_FIRMWARE) == SR_OK)
222 /* Store when this device's FW was updated. */
223 devc->fw_updated = g_get_monotonic_time();
224 else
225 sr_err("Firmware upload failed for "
226 "device %d.", devcnt);
227 sdi->inst_type = SR_INST_USB;
96484e22
UH
228 sdi->conn = sr_usb_dev_inst_new(
229 libusb_get_bus_number(devlist[i]), 0xff, NULL);
5eea4305 230 }
f6a21fa5
MC
231 }
232 libusb_free_device_list(devlist, 1);
5eea4305 233 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
c463dcf0
MC
234
235 return devices;
236}
237
238static GSList *dev_list(void)
239{
87b537ce 240 return ((struct drv_context *)(di->priv))->instances;
c463dcf0
MC
241}
242
243static int dev_clear(void)
244{
245 return std_dev_clear(di, NULL);
246}
247
5eea4305
MC
248static int logic16_dev_open(struct sr_dev_inst *sdi)
249{
250 libusb_device **devlist;
251 struct sr_usb_dev_inst *usb;
252 struct libusb_device_descriptor des;
5eea4305
MC
253 struct drv_context *drvc;
254 int ret, skip, i, device_count;
255
256 drvc = di->priv;
5eea4305
MC
257 usb = sdi->conn;
258
259 if (sdi->status == SR_ST_ACTIVE)
260 /* Device is already in use. */
261 return SR_ERR;
262
263 skip = 0;
264 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
265 if (device_count < 0) {
266 sr_err("Failed to get device list: %s.",
267 libusb_error_name(device_count));
268 return SR_ERR;
269 }
270
271 for (i = 0; i < device_count; i++) {
272 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
273 sr_err("Failed to get device descriptor: %s.",
274 libusb_error_name(ret));
275 continue;
276 }
277
96484e22 278 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
5eea4305
MC
279 continue;
280
281 if (sdi->status == SR_ST_INITIALIZING) {
282 if (skip != sdi->index) {
283 /* Skip devices of this type that aren't the one we want. */
284 skip += 1;
285 continue;
286 }
287 } else if (sdi->status == SR_ST_INACTIVE) {
288 /*
289 * This device is fully enumerated, so we need to find
290 * this device by vendor, product, bus and address.
291 */
292 if (libusb_get_bus_number(devlist[i]) != usb->bus
96484e22 293 || libusb_get_device_address(devlist[i]) != usb->address)
5eea4305
MC
294 /* This is not the one. */
295 continue;
296 }
297
298 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
299 if (usb->address == 0xff)
300 /*
301 * First time we touch this device after FW
302 * upload, so we don't know the address yet.
303 */
304 usb->address = libusb_get_device_address(devlist[i]);
305 } else {
306 sr_err("Failed to open device: %s.",
307 libusb_error_name(ret));
308 break;
309 }
310
0c30b35f
SY
311 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
312 if (ret == LIBUSB_ERROR_BUSY) {
313 sr_err("Unable to claim USB interface. Another "
314 "program or driver has already claimed it.");
315 break;
316 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
317 sr_err("Device has been disconnected.");
318 break;
319 } else if (ret != 0) {
320 sr_err("Unable to claim interface: %s.",
321 libusb_error_name(ret));
322 break;
323 }
324
96484e22 325 if ((ret = logic16_init_device(sdi)) != SR_OK) {
15abcf0f
MC
326 sr_err("Failed to init device.");
327 break;
328 }
329
5eea4305 330 sdi->status = SR_ST_ACTIVE;
96484e22
UH
331 sr_info("Opened device %d on %d.%d, interface %d.",
332 sdi->index, usb->bus, usb->address, USB_INTERFACE);
5eea4305
MC
333
334 break;
335 }
336 libusb_free_device_list(devlist, 1);
337
0c30b35f
SY
338 if (sdi->status != SR_ST_ACTIVE) {
339 if (usb->devhdl) {
340 libusb_release_interface(usb->devhdl, USB_INTERFACE);
341 libusb_close(usb->devhdl);
342 usb->devhdl = NULL;
343 }
5eea4305 344 return SR_ERR;
0c30b35f 345 }
5eea4305
MC
346
347 return SR_OK;
348}
349
c463dcf0
MC
350static int dev_open(struct sr_dev_inst *sdi)
351{
5eea4305
MC
352 struct dev_context *devc;
353 int ret;
354 int64_t timediff_us, timediff_ms;
355
356 devc = sdi->priv;
5eea4305
MC
357
358 /*
359 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
360 * milliseconds for the FX2 to renumerate.
361 */
362 ret = SR_ERR;
363 if (devc->fw_updated > 0) {
364 sr_info("Waiting for device to reset.");
365 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
366 g_usleep(300 * 1000);
367 timediff_ms = 0;
368 while (timediff_ms < MAX_RENUM_DELAY_MS) {
369 if ((ret = logic16_dev_open(sdi)) == SR_OK)
370 break;
371 g_usleep(100 * 1000);
372
373 timediff_us = g_get_monotonic_time() - devc->fw_updated;
374 timediff_ms = timediff_us / 1000;
375 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
376 }
377 if (ret != SR_OK) {
378 sr_err("Device failed to renumerate.");
379 return SR_ERR;
380 }
381 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
382 } else {
383 sr_info("Firmware upload was not needed.");
384 ret = logic16_dev_open(sdi);
385 }
386
387 if (ret != SR_OK) {
388 sr_err("Unable to open device.");
389 return SR_ERR;
390 }
391
5eea4305
MC
392 if (devc->cur_samplerate == 0) {
393 /* Samplerate hasn't been set; default to the slowest one. */
7b5daad4 394 devc->cur_samplerate = samplerates[0];
5eea4305 395 }
c463dcf0
MC
396
397 return SR_OK;
398}
399
400static int dev_close(struct sr_dev_inst *sdi)
401{
5eea4305 402 struct sr_usb_dev_inst *usb;
c463dcf0 403
5eea4305
MC
404 usb = sdi->conn;
405 if (usb->devhdl == NULL)
406 return SR_ERR;
c463dcf0 407
5eea4305
MC
408 sr_info("Closing device %d on %d.%d interface %d.",
409 sdi->index, usb->bus, usb->address, USB_INTERFACE);
410 libusb_release_interface(usb->devhdl, USB_INTERFACE);
411 libusb_close(usb->devhdl);
412 usb->devhdl = NULL;
c463dcf0
MC
413 sdi->status = SR_ST_INACTIVE;
414
415 return SR_OK;
416}
417
418static int cleanup(void)
419{
f6a21fa5
MC
420 int ret;
421 struct drv_context *drvc;
c463dcf0 422
f6a21fa5
MC
423 if (!(drvc = di->priv))
424 /* Can get called on an unused driver, doesn't matter. */
425 return SR_OK;
c463dcf0 426
f6a21fa5
MC
427 ret = dev_clear();
428 g_free(drvc);
429 di->priv = NULL;
430
431 return ret;
c463dcf0
MC
432}
433
434static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
435{
7b5daad4 436 struct dev_context *devc;
b117363a 437 struct sr_usb_dev_inst *usb;
db11d7d2 438 GVariant *range[2];
b117363a 439 char str[128];
c463dcf0 440 int ret;
96484e22 441 unsigned int i;
c463dcf0 442
c463dcf0
MC
443 ret = SR_OK;
444 switch (key) {
b117363a
MC
445 case SR_CONF_CONN:
446 if (!sdi || !sdi->conn)
447 return SR_ERR_ARG;
448 usb = sdi->conn;
449 if (usb->address == 255)
450 /* Device still needs to re-enumerate after firmware
451 * upload, so we don't know its (future) address. */
452 return SR_ERR;
453 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
454 *data = g_variant_new_string(str);
455 break;
7b5daad4
MC
456 case SR_CONF_SAMPLERATE:
457 if (!sdi)
458 return SR_ERR;
459 devc = sdi->priv;
460 *data = g_variant_new_uint64(devc->cur_samplerate);
461 break;
db11d7d2
MC
462 case SR_CONF_VOLTAGE_THRESHOLD:
463 if (!sdi)
464 return SR_ERR;
465 devc = sdi->priv;
466 ret = SR_ERR;
96484e22
UH
467 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
468 if (devc->selected_voltage_range !=
469 volt_thresholds[i].range)
470 continue;
471 range[0] = g_variant_new_double(volt_thresholds[i].low);
472 range[1] = g_variant_new_double(volt_thresholds[i].high);
473 *data = g_variant_new_tuple(range, 2);
474 ret = SR_OK;
475 break;
476 }
db11d7d2 477 break;
c463dcf0
MC
478 default:
479 return SR_ERR_NA;
480 }
481
482 return ret;
483}
484
485static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
486{
7b5daad4 487 struct dev_context *devc;
db11d7d2 488 gdouble low, high;
c463dcf0 489 int ret;
96484e22 490 unsigned int i;
c463dcf0 491
c463dcf0
MC
492 if (sdi->status != SR_ST_ACTIVE)
493 return SR_ERR_DEV_CLOSED;
494
7b5daad4
MC
495 devc = sdi->priv;
496
c463dcf0
MC
497 ret = SR_OK;
498 switch (key) {
7b5daad4
MC
499 case SR_CONF_SAMPLERATE:
500 devc->cur_samplerate = g_variant_get_uint64(data);
501 break;
502 case SR_CONF_LIMIT_SAMPLES:
503 devc->limit_samples = g_variant_get_uint64(data);
504 break;
db11d7d2
MC
505 case SR_CONF_VOLTAGE_THRESHOLD:
506 g_variant_get(data, "(dd)", &low, &high);
507 ret = SR_ERR_ARG;
508 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
509 if (fabs(volt_thresholds[i].low - low) < 0.1 &&
510 fabs(volt_thresholds[i].high - high) < 0.1) {
511 devc->selected_voltage_range =
512 volt_thresholds[i].range;
513 ret = SR_OK;
514 break;
515 }
516 }
517 break;
c463dcf0
MC
518 default:
519 ret = SR_ERR_NA;
520 }
521
522 return ret;
523}
524
525static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
526{
db11d7d2 527 GVariant *gvar, *range[2];
7b5daad4 528 GVariantBuilder gvb;
c463dcf0 529 int ret;
96484e22 530 unsigned int i;
c463dcf0
MC
531
532 (void)sdi;
c463dcf0
MC
533
534 ret = SR_OK;
535 switch (key) {
b117363a
MC
536 case SR_CONF_SCAN_OPTIONS:
537 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
538 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
539 break;
540 case SR_CONF_DEVICE_OPTIONS:
541 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
542 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
543 break;
7b5daad4
MC
544 case SR_CONF_SAMPLERATE:
545 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
96484e22
UH
546 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
547 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
7b5daad4
MC
548 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
549 *data = g_variant_builder_end(&gvb);
db11d7d2
MC
550 break;
551 case SR_CONF_VOLTAGE_THRESHOLD:
552 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
553 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
554 range[0] = g_variant_new_double(volt_thresholds[i].low);
555 range[1] = g_variant_new_double(volt_thresholds[i].high);
556 gvar = g_variant_new_tuple(range, 2);
557 g_variant_builder_add_value(&gvb, gvar);
558 }
559 *data = g_variant_builder_end(&gvb);
7b5daad4 560 break;
c463dcf0
MC
561 default:
562 return SR_ERR_NA;
563 }
564
565 return ret;
566}
567
7b5daad4
MC
568static void abort_acquisition(struct dev_context *devc)
569{
570 int i;
571
572 devc->num_samples = -1;
573
574 for (i = devc->num_transfers - 1; i >= 0; i--) {
575 if (devc->transfers[i])
576 libusb_cancel_transfer(devc->transfers[i]);
577 }
578}
579
580static unsigned int bytes_per_ms(struct dev_context *devc)
581{
582 return devc->cur_samplerate * devc->num_channels / 8000;
583}
584
585static size_t get_buffer_size(struct dev_context *devc)
586{
587 size_t s;
588
589 /*
590 * The buffer should be large enough to hold 10ms of data and
591 * a multiple of 512.
592 */
593 s = 10 * bytes_per_ms(devc);
594 return (s + 511) & ~511;
595}
596
597static unsigned int get_number_of_transfers(struct dev_context *devc)
598{
599 unsigned int n;
600
601 /* Total buffer size should be able to hold about 500ms of data. */
602 n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
603
604 if (n > NUM_SIMUL_TRANSFERS)
605 return NUM_SIMUL_TRANSFERS;
606
607 return n;
608}
609
610static unsigned int get_timeout(struct dev_context *devc)
611{
612 size_t total_size;
613 unsigned int timeout;
614
615 total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
616 timeout = total_size / bytes_per_ms(devc);
617 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
618}
619
620static int configure_probes(const struct sr_dev_inst *sdi)
621{
622 struct dev_context *devc;
623 struct sr_probe *probe;
624 GSList *l;
625 uint16_t probe_bit;
626
627 devc = sdi->priv;
628
629 devc->cur_channels = 0;
630 devc->num_channels = 0;
631 for (l = sdi->probes; l; l = l->next) {
632 probe = (struct sr_probe *)l->data;
633 if (probe->enabled == FALSE)
634 continue;
635
636 probe_bit = 1 << (probe->index);
637
638 devc->cur_channels |= probe_bit;
639
640#ifdef WORDS_BIGENDIAN
96484e22
UH
641 /*
642 * Output logic data should be stored in little endian format.
643 * To speed things up during conversion, do the switcharoo
644 * here instead.
645 */
7b5daad4
MC
646 probe_bit = 1 << (probe->index ^ 8);
647#endif
648
96484e22 649 devc->channel_masks[devc->num_channels++] = probe_bit;
7b5daad4
MC
650 }
651
652 return SR_OK;
653}
654
655static int receive_data(int fd, int revents, void *cb_data)
656{
657 struct timeval tv;
658 struct dev_context *devc;
659 struct drv_context *drvc;
660 const struct sr_dev_inst *sdi;
661
662 (void)fd;
663 (void)revents;
664
665 sdi = cb_data;
666 drvc = di->priv;
667 devc = sdi->priv;
668
669 tv.tv_sec = tv.tv_usec = 0;
670 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
671
672 if (devc->num_samples == -2) {
96484e22 673 logic16_abort_acquisition(sdi);
7b5daad4
MC
674 abort_acquisition(devc);
675 }
676
677 return TRUE;
678}
679
96484e22 680static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
c463dcf0 681{
7b5daad4
MC
682 struct dev_context *devc;
683 struct drv_context *drvc;
684 struct sr_usb_dev_inst *usb;
685 struct libusb_transfer *transfer;
686 const struct libusb_pollfd **lupfd;
687 unsigned int i, timeout, num_transfers;
688 int ret;
689 unsigned char *buf;
690 size_t size, convsize;
c463dcf0
MC
691
692 if (sdi->status != SR_ST_ACTIVE)
693 return SR_ERR_DEV_CLOSED;
694
7b5daad4
MC
695 drvc = di->priv;
696 devc = sdi->priv;
697 usb = sdi->conn;
698
96484e22 699 /* Configures devc->cur_channels. */
7b5daad4
MC
700 if (configure_probes(sdi) != SR_OK) {
701 sr_err("Failed to configure probes.");
702 return SR_ERR;
703 }
704
705 devc->cb_data = cb_data;
706 devc->num_samples = 0;
707 devc->empty_transfer_count = 0;
708 devc->cur_channel = 0;
709 memset(devc->channel_data, 0, sizeof(devc->channel_data));
710
711 timeout = get_timeout(devc);
712 num_transfers = get_number_of_transfers(devc);
713 size = get_buffer_size(devc);
714 convsize = (size / devc->num_channels + 2) * 16;
715 devc->submitted_transfers = 0;
716 devc->usbfd = NULL;
717
718 devc->convbuffer_size = convsize;
719 if (!(devc->convbuffer = g_try_malloc(convsize))) {
720 sr_err("Conversion buffer malloc failed.");
721 return SR_ERR_MALLOC;
722 }
723
724 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
725 if (!devc->transfers) {
726 sr_err("USB transfers malloc failed.");
727 g_free(devc->convbuffer);
728 return SR_ERR_MALLOC;
729 }
730
96484e22
UH
731 if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
732 devc->cur_channels)) != SR_OK) {
7b5daad4
MC
733 g_free(devc->transfers);
734 g_free(devc->convbuffer);
735 return ret;
736 }
737
738 devc->num_transfers = num_transfers;
739 for (i = 0; i < num_transfers; i++) {
740 if (!(buf = g_try_malloc(size))) {
741 sr_err("USB transfer buffer malloc failed.");
742 if (devc->submitted_transfers)
743 abort_acquisition(devc);
744 else {
745 g_free(devc->transfers);
746 g_free(devc->convbuffer);
747 }
748 return SR_ERR_MALLOC;
749 }
750 transfer = libusb_alloc_transfer(0);
751 libusb_fill_bulk_transfer(transfer, usb->devhdl,
752 2 | LIBUSB_ENDPOINT_IN, buf, size,
96484e22 753 logic16_receive_transfer, devc, timeout);
7b5daad4
MC
754 if ((ret = libusb_submit_transfer(transfer)) != 0) {
755 sr_err("Failed to submit transfer: %s.",
756 libusb_error_name(ret));
757 libusb_free_transfer(transfer);
758 g_free(buf);
759 abort_acquisition(devc);
760 return SR_ERR;
761 }
762 devc->transfers[i] = transfer;
763 devc->submitted_transfers++;
764 }
765
766 lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
767 for (i = 0; lupfd[i]; i++);
96484e22
UH
768 devc->usbfd = g_try_malloc(sizeof(struct libusb_pollfd) * (i + 1));
769 if (!devc->usbfd) {
7b5daad4
MC
770 abort_acquisition(devc);
771 free(lupfd);
772 return SR_ERR;
773 }
774 for (i = 0; lupfd[i]; i++) {
775 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
776 timeout, receive_data, (void *)sdi);
777 devc->usbfd[i] = lupfd[i]->fd;
778 }
779 devc->usbfd[i] = -1;
780 free(lupfd);
781
782 /* Send header packet to the session bus. */
783 std_session_send_df_header(cb_data, LOG_PREFIX);
784
96484e22 785 if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
7b5daad4
MC
786 abort_acquisition(devc);
787 return ret;
788 }
c463dcf0
MC
789
790 return SR_OK;
791}
792
793static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
794{
7b5daad4
MC
795 int ret;
796
c463dcf0
MC
797 (void)cb_data;
798
799 if (sdi->status != SR_ST_ACTIVE)
800 return SR_ERR_DEV_CLOSED;
801
96484e22 802 ret = logic16_abort_acquisition(sdi);
c463dcf0 803
7b5daad4
MC
804 abort_acquisition(sdi->priv);
805
806 return ret;
c463dcf0
MC
807}
808
809SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
810 .name = "saleae-logic16",
811 .longname = "Saleae Logic16",
812 .api_version = 1,
813 .init = init,
814 .cleanup = cleanup,
815 .scan = scan,
816 .dev_list = dev_list,
817 .dev_clear = dev_clear,
818 .config_get = config_get,
819 .config_set = config_set,
820 .config_list = config_list,
821 .dev_open = dev_open,
822 .dev_close = dev_close,
823 .dev_acquisition_start = dev_acquisition_start,
824 .dev_acquisition_stop = dev_acquisition_stop,
825 .priv = NULL,
826};