]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/fx2lafw/fx2lafw.c
sr/drivers: change driver dev_acquisition_start/_stop calls to use sdi
[libsigrok.git] / hardware / fx2lafw / fx2lafw.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <inttypes.h>
25#include <libusb.h>
26#include "config.h"
27#include "libsigrok.h"
28#include "libsigrok-internal.h"
29#include "fx2lafw.h"
30#include "command.h"
31
32static const struct fx2lafw_profile supported_fx2[] = {
33 /*
34 * CWAV USBee AX
35 * EE Electronics ESLA201A
36 * ARMFLY AX-Pro
37 */
38 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
39 FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw",
40 0 },
41 /*
42 * CWAV USBee DX
43 * XZL-Studio DX
44 */
45 { 0x08a9, 0x0015, "CWAV", "USBee DX", NULL,
46 FIRMWARE_DIR "/fx2lafw-cwav-usbeedx.fw",
47 DEV_CAPS_16BIT },
48
49 /*
50 * CWAV USBee SX
51 */
52 { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
53 FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw",
54 0 },
55
56 /*
57 * Saleae Logic
58 * EE Electronics ESLA100
59 * Robomotic MiniLogic
60 * Robomotic BugLogic 3
61 */
62 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
63 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw",
64 0 },
65
66 /*
67 * Default Cypress FX2 without EEPROM, e.g.:
68 * Lcsoft Mini Board
69 * Braintechnology USB Interface V2.x
70 */
71 { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
72 FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw",
73 DEV_CAPS_16BIT },
74
75 /*
76 * Braintechnology USB-LPS
77 */
78 { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
79 FIRMWARE_DIR "/fx2lafw-braintechnology-usb-lps.fw",
80 DEV_CAPS_16BIT },
81
82 { 0, 0, 0, 0, 0, 0, 0 }
83};
84
85static const int hwcaps[] = {
86 SR_HWCAP_LOGIC_ANALYZER,
87 SR_HWCAP_SAMPLERATE,
88
89 /* These are really implemented in the driver, not the hardware. */
90 SR_HWCAP_LIMIT_SAMPLES,
91 SR_HWCAP_CONTINUOUS,
92 0,
93};
94
95static const char *probe_names[] = {
96 "0",
97 "1",
98 "2",
99 "3",
100 "4",
101 "5",
102 "6",
103 "7",
104 "8",
105 "9",
106 "10",
107 "11",
108 "12",
109 "13",
110 "14",
111 "15",
112 NULL,
113};
114
115static const uint64_t supported_samplerates[] = {
116 SR_KHZ(20),
117 SR_KHZ(25),
118 SR_KHZ(50),
119 SR_KHZ(100),
120 SR_KHZ(200),
121 SR_KHZ(250),
122 SR_KHZ(500),
123 SR_MHZ(1),
124 SR_MHZ(2),
125 SR_MHZ(3),
126 SR_MHZ(4),
127 SR_MHZ(6),
128 SR_MHZ(8),
129 SR_MHZ(12),
130 SR_MHZ(16),
131 SR_MHZ(24),
132 0,
133};
134
135static const struct sr_samplerates samplerates = {
136 0,
137 0,
138 0,
139 supported_samplerates,
140};
141
142static libusb_context *usb_context = NULL;
143
144SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
145static struct sr_dev_driver *fdi = &fx2lafw_driver_info;
146static int hw_dev_close(struct sr_dev_inst *sdi);
147static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
148 const void *value);
149static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
150 void *cb_data);
151
152/**
153 * Check the USB configuration to determine if this is an fx2lafw device.
154 *
155 * @return TRUE if the device's configuration profile match fx2lafw
156 * configuration, FALSE otherwise.
157 */
158static gboolean check_conf_profile(libusb_device *dev)
159{
160 struct libusb_device_descriptor des;
161 struct libusb_device_handle *hdl;
162 gboolean ret;
163 unsigned char strdesc[64];
164
165 hdl = NULL;
166 ret = FALSE;
167 while (!ret) {
168 /* Assume the FW has not been loaded, unless proven wrong. */
169 if (libusb_get_device_descriptor(dev, &des) != 0)
170 break;
171
172 if (libusb_open(dev, &hdl) != 0)
173 break;
174
175 if (libusb_get_string_descriptor_ascii(hdl,
176 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
177 break;
178 if (strncmp((const char *)strdesc, "sigrok", 6))
179 break;
180
181 if (libusb_get_string_descriptor_ascii(hdl,
182 des.iProduct, strdesc, sizeof(strdesc)) < 0)
183 break;
184 if (strncmp((const char *)strdesc, "fx2lafw", 7))
185 break;
186
187 /* If we made it here, it must be an fx2lafw. */
188 ret = TRUE;
189 }
190 if (hdl)
191 libusb_close(hdl);
192
193 return ret;
194}
195
196static int fx2lafw_dev_open(struct sr_dev_inst *sdi)
197{
198 libusb_device **devlist;
199 struct libusb_device_descriptor des;
200 struct context *ctx;
201 struct version_info vi;
202 int ret, skip, i;
203 uint8_t revid;
204
205 ctx = sdi->priv;
206
207 if (sdi->status == SR_ST_ACTIVE)
208 /* already in use */
209 return SR_ERR;
210
211 skip = 0;
212 const int device_count = libusb_get_device_list(usb_context, &devlist);
213 if (device_count < 0) {
214 sr_err("fx2lafw: Failed to retrieve device list (%d)",
215 device_count);
216 return SR_ERR;
217 }
218
219 for (i = 0; i < device_count; i++) {
220 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
221 sr_err("fx2lafw: Failed to get device descriptor: %d.",
222 ret);
223 continue;
224 }
225
226 if (des.idVendor != ctx->profile->vid
227 || des.idProduct != ctx->profile->pid)
228 continue;
229
230 if (sdi->status == SR_ST_INITIALIZING) {
231 if (skip != sdi->index) {
232 /* Skip devices of this type that aren't the one we want. */
233 skip += 1;
234 continue;
235 }
236 } else if (sdi->status == SR_ST_INACTIVE) {
237 /*
238 * This device is fully enumerated, so we need to find
239 * this device by vendor, product, bus and address.
240 */
241 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
242 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
243 /* this is not the one */
244 continue;
245 }
246
247 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
248 if (ctx->usb->address == 0xff)
249 /*
250 * first time we touch this device after firmware upload,
251 * so we don't know the address yet.
252 */
253 ctx->usb->address = libusb_get_device_address(devlist[i]);
254 } else {
255 sr_err("fx2lafw: Failed to open device: %d.", ret);
256 break;
257 }
258
259 ret = command_get_fw_version(ctx->usb->devhdl, &vi);
260 if (ret != SR_OK) {
261 sr_err("fx2lafw: Failed to retrieve "
262 "firmware version information.");
263 break;
264 }
265
266 ret = command_get_revid_version(ctx->usb->devhdl, &revid);
267 if (ret != SR_OK) {
268 sr_err("fx2lafw: Failed to retrieve REVID.");
269 break;
270 }
271
272 /*
273 * Changes in major version mean incompatible/API changes, so
274 * bail out if we encounter an incompatible version.
275 * Different minor versions are OK, they should be compatible.
276 */
277 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
278 sr_err("fx2lafw: Expected firmware version %d.x, "
279 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
280 vi.major, vi.minor);
281 break;
282 }
283
284 sdi->status = SR_ST_ACTIVE;
285 sr_info("fx2lafw: Opened device %d on %d.%d "
286 "interface %d, firmware %d.%d, REVID %d.",
287 sdi->index, ctx->usb->bus, ctx->usb->address,
288 USB_INTERFACE, vi.major, vi.minor, revid);
289
290 break;
291 }
292 libusb_free_device_list(devlist, 1);
293
294 if (sdi->status != SR_ST_ACTIVE)
295 return SR_ERR;
296
297 return SR_OK;
298}
299
300static int configure_probes(struct context *ctx, GSList *probes)
301{
302 struct sr_probe *probe;
303 GSList *l;
304 int probe_bit, stage, i;
305 char *tc;
306
307 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
308 ctx->trigger_mask[i] = 0;
309 ctx->trigger_value[i] = 0;
310 }
311
312 stage = -1;
313 for (l = probes; l; l = l->next) {
314 probe = (struct sr_probe *)l->data;
315 if (probe->enabled == FALSE)
316 continue;
317
318 if (probe->index > 8)
319 ctx->sample_wide = TRUE;
320
321 probe_bit = 1 << (probe->index - 1);
322 if (!(probe->trigger))
323 continue;
324
325 stage = 0;
326 for (tc = probe->trigger; *tc; tc++) {
327 ctx->trigger_mask[stage] |= probe_bit;
328 if (*tc == '1')
329 ctx->trigger_value[stage] |= probe_bit;
330 stage++;
331 if (stage > NUM_TRIGGER_STAGES)
332 return SR_ERR;
333 }
334 }
335
336 if (stage == -1)
337 /*
338 * We didn't configure any triggers, make sure acquisition
339 * doesn't wait for any.
340 */
341 ctx->trigger_stage = TRIGGER_FIRED;
342 else
343 ctx->trigger_stage = 0;
344
345 return SR_OK;
346}
347
348static struct context *fx2lafw_dev_new(void)
349{
350 struct context *ctx;
351
352 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
353 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
354 return NULL;
355 }
356
357 ctx->trigger_stage = TRIGGER_FIRED;
358
359 return ctx;
360}
361
362static int clear_instances(void)
363{
364 GSList *l;
365 struct sr_dev_inst *sdi;
366 struct context *ctx;
367 int ret;
368
369 ret = SR_OK;
370 for (l = fdi->instances; l; l = l->next) {
371 if (!(sdi = l->data)) {
372 /* Log error, but continue cleaning up the rest. */
373 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
374 __func__);
375 ret = SR_ERR_BUG;
376 continue;
377 }
378 if (!(ctx = sdi->priv)) {
379 /* Log error, but continue cleaning up the rest. */
380 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
381 __func__);
382 ret = SR_ERR_BUG;
383 continue;
384 }
385 hw_dev_close(sdi);
386 sdi = l->data;
387 sr_dev_inst_free(sdi);
388 }
389
390 g_slist_free(fdi->instances);
391 fdi->instances = NULL;
392
393 return ret;
394}
395
396
397/*
398 * API callbacks
399 */
400
401static int hw_init(void)
402{
403
404 if (libusb_init(&usb_context) != 0) {
405 sr_warn("fx2lafw: Failed to initialize libusb.");
406 return SR_ERR;
407 }
408
409 return SR_OK;
410}
411
412static GSList *hw_scan(GSList *options)
413{
414 GSList *devices;
415 struct libusb_device_descriptor des;
416 struct sr_dev_inst *sdi;
417 const struct fx2lafw_profile *prof;
418 struct context *ctx;
419 struct sr_probe *probe;
420 libusb_device **devlist;
421 int devcnt, num_logic_probes, ret, i, j;
422
423 /* Avoid compiler warnings. */
424 (void)options;
425
426 /* This scan always invalidates any previous scans. */
427 clear_instances();
428
429 /* Find all fx2lafw compatible devices and upload firmware to them. */
430 devices = NULL;
431 libusb_get_device_list(usb_context, &devlist);
432 for (i = 0; devlist[i]; i++) {
433
434 if ((ret = libusb_get_device_descriptor(
435 devlist[i], &des)) != 0) {
436 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
437 continue;
438 }
439
440 prof = NULL;
441 for (j = 0; supported_fx2[j].vid; j++) {
442 if (des.idVendor == supported_fx2[j].vid &&
443 des.idProduct == supported_fx2[j].pid) {
444 prof = &supported_fx2[j];
445 }
446 }
447
448 /* Skip if the device was not found */
449 if (!prof)
450 continue;
451
452 devcnt = g_slist_length(fdi->instances);
453 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
454 prof->vendor, prof->model, prof->model_version);
455 if (!sdi)
456 return NULL;
457 sdi->driver = fdi;
458
459 /* Fill in probelist according to this device's profile. */
460 num_logic_probes = prof->dev_caps & DEV_CAPS_16BIT ? 16 : 8;
461 for (j = 0; j < num_logic_probes; j++) {
462 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
463 probe_names[j])))
464 return NULL;
465 sdi->probes = g_slist_append(sdi->probes, probe);
466 }
467
468 ctx = fx2lafw_dev_new();
469 ctx->profile = prof;
470 sdi->priv = ctx;
471 fdi->instances = g_slist_append(fdi->instances, sdi);
472 devices = g_slist_append(devices, sdi);
473
474 if (check_conf_profile(devlist[i])) {
475 /* Already has the firmware, so fix the new address. */
476 sr_dbg("fx2lafw: Found an fx2lafw device.");
477 sdi->status = SR_ST_INACTIVE;
478 ctx->usb = sr_usb_dev_inst_new
479 (libusb_get_bus_number(devlist[i]),
480 libusb_get_device_address(devlist[i]), NULL);
481 } else {
482 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
483 prof->firmware) == SR_OK)
484 /* Remember when the firmware on this device was updated */
485 ctx->fw_updated = g_get_monotonic_time();
486 else
487 sr_err("fx2lafw: Firmware upload failed for "
488 "device %d.", devcnt);
489 ctx->usb = sr_usb_dev_inst_new
490 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
491 }
492 }
493 libusb_free_device_list(devlist, 1);
494
495 return devices;
496}
497
498static int hw_dev_open(struct sr_dev_inst *sdi)
499{
500 struct context *ctx;
501 int ret;
502 int64_t timediff_us, timediff_ms;
503
504 ctx = sdi->priv;
505
506 /*
507 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
508 * milliseconds for the FX2 to renumerate.
509 */
510 ret = SR_ERR;
511 if (ctx->fw_updated > 0) {
512 sr_info("fx2lafw: Waiting for device to reset.");
513 /* takes at least 300ms for the FX2 to be gone from the USB bus */
514 g_usleep(300 * 1000);
515 timediff_ms = 0;
516 while (timediff_ms < MAX_RENUM_DELAY_MS) {
517 if ((ret = fx2lafw_dev_open(sdi)) == SR_OK)
518 break;
519 g_usleep(100 * 1000);
520
521 timediff_us = g_get_monotonic_time() - ctx->fw_updated;
522 timediff_ms = timediff_us / 1000;
523 sr_spew("fx2lafw: waited %" PRIi64 " ms", timediff_ms);
524 }
525 sr_info("fx2lafw: Device came back after %d ms.", timediff_ms);
526 } else {
527 ret = fx2lafw_dev_open(sdi);
528 }
529
530 if (ret != SR_OK) {
531 sr_err("fx2lafw: Unable to open device.");
532 return SR_ERR;
533 }
534 ctx = sdi->priv;
535
536 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
537 if (ret != 0) {
538 switch(ret) {
539 case LIBUSB_ERROR_BUSY:
540 sr_err("fx2lafw: Unable to claim USB interface. Another "
541 "program or driver has already claimed it.");
542 break;
543
544 case LIBUSB_ERROR_NO_DEVICE:
545 sr_err("fx2lafw: Device has been disconnected.");
546 break;
547
548 default:
549 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
550 break;
551 }
552
553 return SR_ERR;
554 }
555
556 if (ctx->cur_samplerate == 0) {
557 /* Samplerate hasn't been set; default to the slowest one. */
558 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
559 &supported_samplerates[0]) == SR_ERR)
560 return SR_ERR;
561 }
562
563 return SR_OK;
564}
565
566static int hw_dev_close(struct sr_dev_inst *sdi)
567{
568 struct context *ctx;
569
570 ctx = sdi->priv;
571 if (ctx->usb->devhdl == NULL)
572 return SR_ERR;
573
574 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
575 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
576 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
577 libusb_close(ctx->usb->devhdl);
578 ctx->usb->devhdl = NULL;
579 sdi->status = SR_ST_INACTIVE;
580
581 return SR_OK;
582}
583
584static int hw_cleanup(void)
585{
586 int ret;
587
588 ret = clear_instances();
589
590 if (usb_context)
591 libusb_exit(usb_context);
592 usb_context = NULL;
593
594 return ret;
595}
596
597static int hw_info_get(int info_id, const void **data,
598 const struct sr_dev_inst *sdi)
599{
600 struct context *ctx;
601
602 switch (info_id) {
603 case SR_DI_INST:
604 *data = sdi;
605 break;
606 case SR_DI_HWCAPS:
607 *data = hwcaps;
608 break;
609 case SR_DI_NUM_PROBES:
610 if (sdi) {
611 ctx = sdi->priv;
612 *data = GINT_TO_POINTER(
613 (ctx->profile->dev_caps & DEV_CAPS_16BIT) ?
614 16 : 8);
615 } else
616 return SR_ERR;
617 break;
618 case SR_DI_PROBE_NAMES:
619 *data = probe_names;
620 break;
621 case SR_DI_SAMPLERATES:
622 *data = &samplerates;
623 break;
624 case SR_DI_TRIGGER_TYPES:
625 *data = TRIGGER_TYPES;
626 break;
627 case SR_DI_CUR_SAMPLERATE:
628 if (sdi) {
629 ctx = sdi->priv;
630 *data = &ctx->cur_samplerate;
631 } else
632 return SR_ERR;
633 break;
634 default:
635 return SR_ERR_ARG;
636 }
637
638 return SR_OK;
639}
640
641static int hw_dev_status_get(int dev_index)
642{
643 const struct sr_dev_inst *const sdi =
644 sr_dev_inst_get(fdi->instances, dev_index);
645
646 if (!sdi)
647 return SR_ST_NOT_FOUND;
648
649 return sdi->status;
650}
651
652static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
653 const void *value)
654{
655 struct context *ctx;
656 int ret;
657
658 ctx = sdi->priv;
659
660 if (hwcap == SR_HWCAP_SAMPLERATE) {
661 ctx->cur_samplerate = *(const uint64_t *)value;
662 ret = SR_OK;
663 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
664 ret = configure_probes(ctx, (GSList *) value);
665 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
666 ctx->limit_samples = *(const uint64_t *)value;
667 ret = SR_OK;
668 } else {
669 ret = SR_ERR;
670 }
671
672 return ret;
673}
674
675static int receive_data(int fd, int revents, void *cb_data)
676{
677 struct timeval tv;
678
679 /* Avoid compiler warnings. */
680 (void)fd;
681 (void)revents;
682 (void)cb_data;
683
684 tv.tv_sec = tv.tv_usec = 0;
685 libusb_handle_events_timeout(usb_context, &tv);
686
687 return TRUE;
688}
689
690static void abort_acquisition(struct context *ctx)
691{
692 int i;
693
694 ctx->num_samples = -1;
695
696 for (i = ctx->num_transfers - 1; i >= 0; i--) {
697 if (ctx->transfers[i])
698 libusb_cancel_transfer(ctx->transfers[i]);
699 }
700}
701
702static void finish_acquisition(struct context *ctx)
703{
704 struct sr_datafeed_packet packet;
705 int i;
706
707 /* Terminate session */
708 packet.type = SR_DF_END;
709 sr_session_send(ctx->session_dev_id, &packet);
710
711 /* Remove fds from polling */
712 const struct libusb_pollfd **const lupfd =
713 libusb_get_pollfds(usb_context);
714 for (i = 0; lupfd[i]; i++)
715 sr_source_remove(lupfd[i]->fd);
716 free(lupfd); /* NOT g_free()! */
717
718 ctx->num_transfers = 0;
719 g_free(ctx->transfers);
720}
721
722static void free_transfer(struct libusb_transfer *transfer)
723{
724 struct context *ctx = transfer->user_data;
725 unsigned int i;
726
727 g_free(transfer->buffer);
728 transfer->buffer = NULL;
729 libusb_free_transfer(transfer);
730
731 for (i = 0; i < ctx->num_transfers; i++) {
732 if (ctx->transfers[i] == transfer) {
733 ctx->transfers[i] = NULL;
734 break;
735 }
736 }
737
738 ctx->submitted_transfers--;
739 if (ctx->submitted_transfers == 0)
740 finish_acquisition(ctx);
741
742}
743
744static void resubmit_transfer(struct libusb_transfer *transfer)
745{
746 if (libusb_submit_transfer(transfer) != 0) {
747 free_transfer(transfer);
748 /* TODO: Stop session? */
749 /* TODO: Better error message. */
750 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
751 }
752}
753
754static void receive_transfer(struct libusb_transfer *transfer)
755{
756 gboolean packet_has_error = FALSE;
757 struct sr_datafeed_packet packet;
758 struct sr_datafeed_logic logic;
759 struct context *ctx = transfer->user_data;
760 int trigger_offset, i;
761
762 /*
763 * If acquisition has already ended, just free any queued up
764 * transfer that come in.
765 */
766 if (ctx->num_samples == -1) {
767 free_transfer(transfer);
768 return;
769 }
770
771 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
772 transfer->status, transfer->actual_length);
773
774 /* Save incoming transfer before reusing the transfer struct. */
775 uint8_t *const cur_buf = transfer->buffer;
776 const int sample_width = ctx->sample_wide ? 2 : 1;
777 const int cur_sample_count = transfer->actual_length / sample_width;
778
779 switch (transfer->status) {
780 case LIBUSB_TRANSFER_NO_DEVICE:
781 abort_acquisition(ctx);
782 free_transfer(transfer);
783 return;
784 case LIBUSB_TRANSFER_COMPLETED:
785 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
786 break;
787 default:
788 packet_has_error = TRUE;
789 break;
790 }
791
792 if (transfer->actual_length == 0 || packet_has_error) {
793 ctx->empty_transfer_count++;
794 if (ctx->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
795 /*
796 * The FX2 gave up. End the acquisition, the frontend
797 * will work out that the samplecount is short.
798 */
799 abort_acquisition(ctx);
800 free_transfer(transfer);
801 } else {
802 resubmit_transfer(transfer);
803 }
804 return;
805 } else {
806 ctx->empty_transfer_count = 0;
807 }
808
809 trigger_offset = 0;
810 if (ctx->trigger_stage >= 0) {
811 for (i = 0; i < cur_sample_count; i++) {
812
813 const uint16_t cur_sample = ctx->sample_wide ?
814 *((const uint16_t*)cur_buf + i) :
815 *((const uint8_t*)cur_buf + i);
816
817 if ((cur_sample & ctx->trigger_mask[ctx->trigger_stage]) ==
818 ctx->trigger_value[ctx->trigger_stage]) {
819 /* Match on this trigger stage. */
820 ctx->trigger_buffer[ctx->trigger_stage] = cur_sample;
821 ctx->trigger_stage++;
822
823 if (ctx->trigger_stage == NUM_TRIGGER_STAGES ||
824 ctx->trigger_mask[ctx->trigger_stage] == 0) {
825 /* Match on all trigger stages, we're done. */
826 trigger_offset = i + 1;
827
828 /*
829 * TODO: Send pre-trigger buffer to session bus.
830 * Tell the frontend we hit the trigger here.
831 */
832 packet.type = SR_DF_TRIGGER;
833 packet.payload = NULL;
834 sr_session_send(ctx->session_dev_id, &packet);
835
836 /*
837 * Send the samples that triggered it, since we're
838 * skipping past them.
839 */
840 packet.type = SR_DF_LOGIC;
841 packet.payload = &logic;
842 logic.unitsize = sizeof(*ctx->trigger_buffer);
843 logic.length = ctx->trigger_stage * logic.unitsize;
844 logic.data = ctx->trigger_buffer;
845 sr_session_send(ctx->session_dev_id, &packet);
846
847 ctx->trigger_stage = TRIGGER_FIRED;
848 break;
849 }
850 } else if (ctx->trigger_stage > 0) {
851 /*
852 * We had a match before, but not in the next sample. However, we may
853 * have a match on this stage in the next bit -- trigger on 0001 will
854 * fail on seeing 00001, so we need to go back to stage 0 -- but at
855 * the next sample from the one that matched originally, which the
856 * counter increment at the end of the loop takes care of.
857 */
858 i -= ctx->trigger_stage;
859 if (i < -1)
860 i = -1; /* Oops, went back past this buffer. */
861 /* Reset trigger stage. */
862 ctx->trigger_stage = 0;
863 }
864 }
865 }
866
867 if (ctx->trigger_stage == TRIGGER_FIRED) {
868 /* Send the incoming transfer to the session bus. */
869 const int trigger_offset_bytes = trigger_offset * sample_width;
870 packet.type = SR_DF_LOGIC;
871 packet.payload = &logic;
872 logic.length = transfer->actual_length - trigger_offset_bytes;
873 logic.unitsize = sample_width;
874 logic.data = cur_buf + trigger_offset_bytes;
875 sr_session_send(ctx->session_dev_id, &packet);
876
877 ctx->num_samples += cur_sample_count;
878 if (ctx->limit_samples &&
879 (unsigned int)ctx->num_samples > ctx->limit_samples) {
880 abort_acquisition(ctx);
881 free_transfer(transfer);
882 return;
883 }
884 } else {
885 /*
886 * TODO: Buffer pre-trigger data in capture
887 * ratio-sized buffer.
888 */
889 }
890
891 resubmit_transfer(transfer);
892}
893
894static unsigned int to_bytes_per_ms(unsigned int samplerate)
895{
896 return samplerate / 1000;
897}
898
899static size_t get_buffer_size(struct context *ctx)
900{
901 size_t s;
902
903 /* The buffer should be large enough to hold 10ms of data and a multiple
904 * of 512. */
905 s = 10 * to_bytes_per_ms(ctx->cur_samplerate);
906 return (s + 511) & ~511;
907}
908
909static unsigned int get_number_of_transfers(struct context *ctx)
910{
911 unsigned int n;
912
913 /* Total buffer size should be able to hold about 500ms of data */
914 n = 500 * to_bytes_per_ms(ctx->cur_samplerate) / get_buffer_size(ctx);
915
916 if (n > NUM_SIMUL_TRANSFERS)
917 return NUM_SIMUL_TRANSFERS;
918
919 return n;
920}
921
922static unsigned int get_timeout(struct context *ctx)
923{
924 size_t total_size;
925 unsigned int timeout;
926
927 total_size = get_buffer_size(ctx) * get_number_of_transfers(ctx);
928 timeout = total_size / to_bytes_per_ms(ctx->cur_samplerate);
929 return timeout + timeout / 4; /* Leave a headroom of 25% percent */
930}
931
932static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
933 void *cb_data)
934{
935 struct sr_datafeed_packet packet;
936 struct sr_datafeed_header header;
937 struct sr_datafeed_meta_logic meta;
938 struct context *ctx;
939 struct libusb_transfer *transfer;
940 const struct libusb_pollfd **lupfd;
941 unsigned int i;
942 int ret;
943 unsigned char *buf;
944
945 ctx = sdi->priv;
946 if (ctx->submitted_transfers != 0)
947 return SR_ERR;
948
949 ctx->session_dev_id = cb_data;
950 ctx->num_samples = 0;
951 ctx->empty_transfer_count = 0;
952
953 const unsigned int timeout = get_timeout(ctx);
954 const unsigned int num_transfers = get_number_of_transfers(ctx);
955 const size_t size = get_buffer_size(ctx);
956
957 ctx->transfers = g_try_malloc0(sizeof(*ctx->transfers) * num_transfers);
958 if (!ctx->transfers)
959 return SR_ERR;
960
961 ctx->num_transfers = num_transfers;
962
963 for (i = 0; i < num_transfers; i++) {
964 if (!(buf = g_try_malloc(size))) {
965 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
966 return SR_ERR_MALLOC;
967 }
968 transfer = libusb_alloc_transfer(0);
969 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
970 2 | LIBUSB_ENDPOINT_IN, buf, size,
971 receive_transfer, ctx, timeout);
972 if (libusb_submit_transfer(transfer) != 0) {
973 libusb_free_transfer(transfer);
974 g_free(buf);
975 abort_acquisition(ctx);
976 return SR_ERR;
977 }
978 ctx->transfers[i] = transfer;
979 ctx->submitted_transfers++;
980 }
981
982 lupfd = libusb_get_pollfds(usb_context);
983 for (i = 0; lupfd[i]; i++)
984 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
985 timeout, receive_data, NULL);
986 free(lupfd); /* NOT g_free()! */
987
988 packet.type = SR_DF_HEADER;
989 packet.payload = &header;
990 header.feed_version = 1;
991 gettimeofday(&header.starttime, NULL);
992 sr_session_send(cb_data, &packet);
993
994 /* Send metadata about the SR_DF_LOGIC packets to come. */
995 packet.type = SR_DF_META_LOGIC;
996 packet.payload = &meta;
997 meta.samplerate = ctx->cur_samplerate;
998 meta.num_probes = ctx->sample_wide ? 16 : 8;
999 sr_session_send(cb_data, &packet);
1000
1001 if ((ret = command_start_acquisition (ctx->usb->devhdl,
1002 ctx->cur_samplerate, ctx->sample_wide)) != SR_OK) {
1003 abort_acquisition(ctx);
1004 return ret;
1005 }
1006
1007 return SR_OK;
1008}
1009
1010/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
1011static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
1012 void *cb_data)
1013{
1014
1015 /* Avoid compiler warnings. */
1016 (void)cb_data;
1017
1018 abort_acquisition(sdi->priv);
1019
1020 return SR_OK;
1021}
1022
1023SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
1024 .name = "fx2lafw",
1025 .longname = "fx2lafw (generic driver for FX2 based LAs)",
1026 .api_version = 1,
1027 .init = hw_init,
1028 .cleanup = hw_cleanup,
1029 .scan = hw_scan,
1030 .dev_open = hw_dev_open,
1031 .dev_close = hw_dev_close,
1032 .info_get = hw_info_get,
1033 .dev_status_get = hw_dev_status_get,
1034 .dev_config_set = hw_dev_config_set,
1035 .dev_acquisition_start = hw_dev_acquisition_start,
1036 .dev_acquisition_stop = hw_dev_acquisition_stop,
1037 .instances = NULL,
1038};