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