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