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