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