]> sigrok.org Git - libsigrok.git/blob - hardware/fx2lafw/fx2lafw.c
sr: ARMFLY AX-Pro is supported by fx2lafw (LA only).
[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          * 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
72 static 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  */
85 static const char *probe_names[] = {
86         "0",
87         "1",
88         "2",
89         "3",
90         "4",
91         "5",
92         "6",
93         "7",
94         NULL,
95 };
96
97 static 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
117 static struct sr_samplerates samplerates = {
118         0,
119         0,
120         0,
121         supported_samplerates,
122 };
123
124 static GSList *dev_insts = NULL;
125 static libusb_context *usb_context = NULL;
126
127 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
128 static 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  */
136 static 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
174 static 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
277 static 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
294 static 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
338 static 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
356 static 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                                 g_get_current_time(&ctx->fw_updated);
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
433 static int hw_dev_open(int dev_index)
434 {
435         GTimeVal cur_time;
436         struct sr_dev_inst *sdi;
437         struct context *ctx;
438         int timediff, ret;
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         if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
450                 sr_info("fx2lafw: Waiting for device to reset.");
451                 /* takes at least 300ms for the FX2 to be gone from the USB bus */
452                 g_usleep(300 * 1000);
453                 timediff = 0;
454                 while (timediff < MAX_RENUM_DELAY) {
455                         if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
456                                 break;
457                         g_usleep(100 * 1000);
458                         g_get_current_time(&cur_time);
459                         timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
460                 }
461                 sr_info("fx2lafw: Device came back after %d ms.", timediff);
462         } else {
463                 ret = fx2lafw_dev_open(dev_index);
464         }
465
466         if (ret != SR_OK) {
467                 sr_err("fx2lafw: Unable to open device.");
468                 return SR_ERR;
469         }
470         ctx = sdi->priv;
471
472         ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
473         if (ret != 0) {
474                 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
475                 return SR_ERR;
476         }
477
478         if (ctx->cur_samplerate == 0) {
479                 /* Samplerate hasn't been set; default to the slowest one. */
480                 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
481                     &supported_samplerates[0]) == SR_ERR)
482                         return SR_ERR;
483         }
484
485         return SR_OK;
486 }
487
488 static int hw_dev_close(int dev_index)
489 {
490         struct sr_dev_inst *sdi;
491
492         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
493                 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
494                 return SR_ERR_BUG;
495         }
496
497         /* TODO */
498         close_dev(sdi);
499
500         return SR_OK;
501 }
502
503 static int hw_cleanup(void)
504 {
505         GSList *l;
506         struct sr_dev_inst *sdi;
507         struct context *ctx;
508         int ret = SR_OK;
509
510         for (l = dev_insts; l; l = l->next) {
511                 if (!(sdi = l->data)) {
512                         /* Log error, but continue cleaning up the rest. */
513                         sr_err("fx2lafw: %s: sdi was NULL, continuing.",
514                                __func__);
515                         ret = SR_ERR_BUG;
516                         continue;
517                 }
518                 if (!(ctx = sdi->priv)) {
519                         /* Log error, but continue cleaning up the rest. */
520                         sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
521                                __func__);
522                         ret = SR_ERR_BUG;
523                         continue;
524                 }
525                 close_dev(sdi);
526                 sdi = l->data;
527                 sr_dev_inst_free(sdi);
528         }
529
530         g_slist_free(dev_insts);
531         dev_insts = NULL;
532
533         if (usb_context)
534                 libusb_exit(usb_context);
535         usb_context = NULL;
536
537         return ret;
538 }
539
540 static void *hw_dev_info_get(int dev_index, int dev_info_id)
541 {
542         struct sr_dev_inst *sdi;
543         struct context *ctx;
544
545         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
546                 return NULL;
547         ctx = sdi->priv;
548
549         switch (dev_info_id) {
550         case SR_DI_INST:
551                 return sdi;
552         case SR_DI_NUM_PROBES:
553                 return GINT_TO_POINTER(ctx->profile->num_probes);
554         case SR_DI_PROBE_NAMES:
555                 return probe_names;
556         case SR_DI_SAMPLERATES:
557                 return &samplerates;
558         case SR_DI_TRIGGER_TYPES:
559                 return TRIGGER_TYPES;
560         case SR_DI_CUR_SAMPLERATE:
561                 return &ctx->cur_samplerate;
562         }
563
564         return NULL;
565 }
566
567 static int hw_dev_status_get(int dev_index)
568 {
569         const struct sr_dev_inst *const sdi =
570                 sr_dev_inst_get(dev_insts, dev_index);
571
572         if (!sdi)
573                 return SR_ST_NOT_FOUND;
574
575         return sdi->status;
576 }
577
578 static int *hw_hwcap_get_all(void)
579 {
580         return hwcaps;
581 }
582
583 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
584 {
585         struct sr_dev_inst *sdi;
586         struct context *ctx;
587         int ret;
588
589         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
590                 return SR_ERR;
591         ctx = sdi->priv;
592
593         if (hwcap == SR_HWCAP_SAMPLERATE) {
594                 ctx->cur_samplerate = *(uint64_t *)value;
595                 ret = SR_OK;
596         } else if (hwcap == SR_HWCAP_PROBECONFIG) {
597                 ret = configure_probes(ctx, (GSList *) value);
598         } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
599                 ctx->limit_samples = *(uint64_t *)value;
600                 ret = SR_OK;
601         } else {
602                 ret = SR_ERR;
603         }
604
605         return ret;
606 }
607
608 static int receive_data(int fd, int revents, void *cb_data)
609 {
610         struct timeval tv;
611
612         /* Avoid compiler warnings. */
613         (void)fd;
614         (void)revents;
615         (void)cb_data;
616
617         tv.tv_sec = tv.tv_usec = 0;
618         libusb_handle_events_timeout(usb_context, &tv);
619
620         return TRUE;
621 }
622
623 static void abort_acquisition(struct context *ctx)
624 {
625         ctx->num_samples = -1;
626 }
627
628 static void finish_acquisition(struct context *ctx)
629 {
630         struct sr_datafeed_packet packet;
631         int i;
632
633         /* Terminate session */
634         packet.type = SR_DF_END;
635         sr_session_send(ctx->session_dev_id, &packet);
636
637         /* Remove fds from polling */
638         const struct libusb_pollfd **const lupfd =
639                 libusb_get_pollfds(usb_context);
640         for (i = 0; lupfd[i]; i++)
641                 sr_source_remove(lupfd[i]->fd);
642         free(lupfd); /* NOT g_free()! */
643 }
644
645 static void receive_transfer(struct libusb_transfer *transfer)
646 {
647         /* TODO: These statics have to move to the ctx struct. */
648         static int empty_transfer_count = 0;
649         struct sr_datafeed_packet packet;
650         struct sr_datafeed_logic logic;
651         struct context *ctx = transfer->user_data;
652         int cur_buflen, trigger_offset, i;
653         unsigned char *cur_buf, *new_buf;
654
655         /*
656          * If acquisition has already ended, just free any queued up
657          * transfer that come in.
658          */
659         if (ctx->num_samples == -1) {
660                 if (transfer)
661                         libusb_free_transfer(transfer);
662
663                 ctx->submitted_transfers--;
664                 if (ctx->submitted_transfers == 0)
665                         finish_acquisition(ctx);
666
667                 return;
668         }
669
670         sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
671                 transfer->status, transfer->actual_length);
672
673         /* Save incoming transfer before reusing the transfer struct. */
674         cur_buf = transfer->buffer;
675         cur_buflen = transfer->actual_length;
676
677         /* Fire off a new request. */
678         if (!(new_buf = g_try_malloc(4096))) {
679                 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
680                 return; /* TODO: SR_ERR_MALLOC */
681         }
682
683         transfer->buffer = new_buf;
684         transfer->length = 4096;
685         if (libusb_submit_transfer(transfer) != 0) {
686                 /* TODO: Stop session? */
687                 /* TODO: Better error message. */
688                 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
689         }
690
691         if (cur_buflen == 0) {
692                 empty_transfer_count++;
693                 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
694                         /*
695                          * The FX2 gave up. End the acquisition, the frontend
696                          * will work out that the samplecount is short.
697                          */
698                         abort_acquisition(ctx);
699                 }
700                 return;
701         } else {
702                 empty_transfer_count = 0;
703         }
704
705         trigger_offset = 0;
706         if (ctx->trigger_stage >= 0) {
707                 for (i = 0; i < cur_buflen; i++) {
708
709                         if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
710                                 /* Match on this trigger stage. */
711                                 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
712                                 ctx->trigger_stage++;
713
714                                 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
715                                         /* Match on all trigger stages, we're done. */
716                                         trigger_offset = i + 1;
717
718                                         /*
719                                          * TODO: Send pre-trigger buffer to session bus.
720                                          * Tell the frontend we hit the trigger here.
721                                          */
722                                         packet.type = SR_DF_TRIGGER;
723                                         packet.payload = NULL;
724                                         sr_session_send(ctx->session_dev_id, &packet);
725
726                                         /*
727                                          * Send the samples that triggered it, since we're
728                                          * skipping past them.
729                                          */
730                                         packet.type = SR_DF_LOGIC;
731                                         packet.payload = &logic;
732                                         logic.length = ctx->trigger_stage;
733                                         logic.unitsize = 1;
734                                         logic.data = ctx->trigger_buffer;
735                                         sr_session_send(ctx->session_dev_id, &packet);
736
737                                         ctx->trigger_stage = TRIGGER_FIRED;
738                                         break;
739                                 }
740                                 return;
741                         }
742
743                         /*
744                          * We had a match before, but not in the next sample. However, we may
745                          * have a match on this stage in the next bit -- trigger on 0001 will
746                          * fail on seeing 00001, so we need to go back to stage 0 -- but at
747                          * the next sample from the one that matched originally, which the
748                          * counter increment at the end of the loop takes care of.
749                          */
750                         if (ctx->trigger_stage > 0) {
751                                 i -= ctx->trigger_stage;
752                                 if (i < -1)
753                                         i = -1; /* Oops, went back past this buffer. */
754                                 /* Reset trigger stage. */
755                                 ctx->trigger_stage = 0;
756                         }
757                 }
758         }
759
760         if (ctx->trigger_stage == TRIGGER_FIRED) {
761                 /* Send the incoming transfer to the session bus. */
762                 packet.type = SR_DF_LOGIC;
763                 packet.payload = &logic;
764                 logic.length = cur_buflen - trigger_offset;
765                 logic.unitsize = 1;
766                 logic.data = cur_buf + trigger_offset;
767                 sr_session_send(ctx->session_dev_id, &packet);
768                 g_free(cur_buf);
769
770                 ctx->num_samples += cur_buflen;
771                 if (ctx->limit_samples &&
772                         (unsigned int)ctx->num_samples > ctx->limit_samples) {
773                         abort_acquisition(ctx);
774                 }
775         } else {
776                 /*
777                  * TODO: Buffer pre-trigger data in capture
778                  * ratio-sized buffer.
779                  */
780         }
781 }
782
783 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
784 {
785         struct sr_dev_inst *sdi;
786         struct sr_datafeed_packet *packet;
787         struct sr_datafeed_header *header;
788         struct context *ctx;
789         struct libusb_transfer *transfer;
790         const struct libusb_pollfd **lupfd;
791         int ret, size, i;
792         unsigned char *buf;
793
794         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
795                 return SR_ERR;
796         ctx = sdi->priv;
797         ctx->session_dev_id = cb_data;
798         ctx->num_samples = 0;
799
800         if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
801                 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
802                 return SR_ERR_MALLOC;
803         }
804
805         if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
806                 sr_err("fx2lafw: %s: header malloc failed.", __func__);
807                 return SR_ERR_MALLOC;
808         }
809
810         /* Start with 2K transfer, subsequently increased to 4K. */
811         size = 2048;
812         for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
813                 if (!(buf = g_try_malloc(size))) {
814                         sr_err("fx2lafw: %s: buf malloc failed.", __func__);
815                         return SR_ERR_MALLOC;
816                 }
817                 transfer = libusb_alloc_transfer(0);
818                 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
819                                 2 | LIBUSB_ENDPOINT_IN, buf, size,
820                                 receive_transfer, ctx, 40);
821                 if (libusb_submit_transfer(transfer) != 0) {
822                         /* TODO: Free them all. */
823                         libusb_free_transfer(transfer);
824                         g_free(buf);
825                         return SR_ERR;
826                 }
827
828                 ctx->submitted_transfers++;
829                 size = 4096;
830         }
831
832         lupfd = libusb_get_pollfds(usb_context);
833         for (i = 0; lupfd[i]; i++)
834                 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
835                               40, receive_data, NULL);
836         free(lupfd); /* NOT g_free()! */
837
838         packet->type = SR_DF_HEADER;
839         packet->payload = header;
840         header->feed_version = 1;
841         gettimeofday(&header->starttime, NULL);
842         header->samplerate = ctx->cur_samplerate;
843         header->num_logic_probes = ctx->profile->num_probes;
844         sr_session_send(cb_data, packet);
845         g_free(header);
846         g_free(packet);
847
848         if ((ret = command_start_acquisition (ctx->usb->devhdl,
849                 ctx->cur_samplerate)) != SR_OK) {
850                 return ret;
851         }
852
853         return SR_OK;
854 }
855
856 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
857 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
858 {
859         struct sr_dev_inst *sdi;
860
861         /* Avoid compiler warnings. */
862         (void)cb_data;
863
864         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
865                 return SR_ERR;
866  
867         abort_acquisition(sdi->priv);
868
869         return SR_OK;
870 }
871
872 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
873         .name = "fx2lafw",
874         .longname = "fx2lafw (generic driver for FX2 based LAs)",
875         .api_version = 1,
876         .init = hw_init,
877         .cleanup = hw_cleanup,
878         .dev_open = hw_dev_open,
879         .dev_close = hw_dev_close,
880         .dev_info_get = hw_dev_info_get,
881         .dev_status_get = hw_dev_status_get,
882         .hwcap_get_all = hw_hwcap_get_all,
883         .dev_config_set = hw_dev_config_set,
884         .dev_acquisition_start = hw_dev_acquisition_start,
885         .dev_acquisition_stop = hw_dev_acquisition_stop,
886 };