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