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