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