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