]> sigrok.org Git - libsigrok.git/blame - hardware/fx2lafw/fx2lafw.c
fx2lafw: Imported software triggering from fx2lafw
[libsigrok.git] / hardware / fx2lafw / fx2lafw.c
CommitLineData
f302a082
JH
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>
8b35f474 22#include <inttypes.h>
187b3582
JH
23#include <glib.h>
24#include <libusb.h>
f302a082
JH
25#include "config.h"
26#include "sigrok.h"
27#include "sigrok-internal.h"
28#include "fx2lafw.h"
f6582cd7 29#include "command.h"
f302a082 30
4679d14d 31static const struct fx2lafw_profile supported_fx2[] = {
17b6c75a
JH
32 /* CWAV USBee AX
33 * EE Electronics ESLA201A
34 */
f8b07fc6
JH
35 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
36 FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw", 8 },
93a9f3da 37
4502e869
JH
38 /* CWAV USBee SX
39 */
40 { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
41 FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw", 8 },
42
93a9f3da
JH
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
f8b07fc6 50 { 0, 0, 0, 0, 0, 0, 0 }
187b3582
JH
51};
52
8b35f474
JH
53static 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,
772a0e61 60 0,
8b35f474
JH
61};
62
63static const char *fx2lafw_probe_names[] = {
64 "D0",
65 "D1",
66 "D2",
67 "D3",
68 "D4",
69 "D5",
70 "D6",
71 "D7",
772a0e61 72 NULL,
8b35f474
JH
73};
74
75static 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),
772a0e61 84 SR_MHZ(24),
8b35f474
JH
85};
86
87static struct sr_samplerates fx2lafw_samplerates = {
88 SR_MHZ(1),
89 SR_MHZ(24),
90 SR_HZ(0),
772a0e61 91 fx2lafw_supported_samplerates,
8b35f474
JH
92};
93
cac0bbaa 94static GSList *dev_insts = NULL;
187b3582
JH
95static libusb_context *usb_context = NULL;
96
f92994fd 97static int hw_dev_config_set(int dev_index, int hwcap, void *value);
1f9813eb 98static int hw_dev_acquisition_stop(int dev_index, void *session_dev_id);
610dbb70 99
b1eeb67e
JH
100/**
101 * Check the USB configuration to determine if this is an fx2lafw device.
102 *
f9a69557
UH
103 * @return TRUE if the device's configuration profile match fx2lafw
104 * configuration, FALSE otherwise.
b1eeb67e 105 */
f9a69557 106static gboolean check_conf_profile(libusb_device *dev)
b1eeb67e
JH
107{
108 struct libusb_device_descriptor des;
109 struct libusb_config_descriptor *conf_dsc = NULL;
110 const struct libusb_interface_descriptor *intf_dsc;
f9a69557 111 gboolean ret = FALSE;
b1eeb67e
JH
112
113 while (!ret) {
0ca21631 114 /* Assume the firmware has not been loaded, unless proven wrong. */
b1eeb67e
JH
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]);
9031ce63
JH
136 if (intf_dsc->bNumEndpoints != 2)
137 /* Need exactly 2 end points. */
b1eeb67e
JH
138 break;
139
140 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
9031ce63
JH
141 (2 | LIBUSB_ENDPOINT_IN)) // 0x82
142 /* The first endpoint should be 2 (inbound). */
b1eeb67e
JH
143 break;
144
145 /* TODO: Check the debug channel... */
146
147 /* If we made it here, it must be an fx2lafw. */
f9a69557 148 ret = TRUE;
b1eeb67e
JH
149 }
150
151 if (conf_dsc)
152 libusb_free_config_descriptor(conf_dsc);
153
154 return ret;
155}
156
43125c69
JH
157static int fx2lafw_open_dev(int dev_index)
158{
159 libusb_device **devlist;
160 struct libusb_device_descriptor des;
161 struct sr_dev_inst *sdi;
772a0e61 162 struct context *ctx;
ebc34738 163 int ret, skip, i;
43125c69
JH
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++) {
ebc34738
UH
176 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
177 sr_err("fx2lafw: failed to get device descriptor: %d", ret);
43125c69
JH
178 continue;
179 }
180
74fcfb80
JH
181 if (des.idVendor != ctx->profile->vid
182 || des.idProduct != ctx->profile->pid)
43125c69
JH
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
ebc34738 202 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
43125c69
JH
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 {
ebc34738 215 sr_err("fx2lafw: failed to open device: %d", ret);
43125c69
JH
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
f1898235
JH
229static void close_dev(struct sr_dev_inst *sdi)
230{
772a0e61 231 struct context *ctx;
f1898235
JH
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
6c6781b6
JH
246static 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
772a0e61 290static struct context *fx2lafw_device_new(void)
187b3582 291{
772a0e61 292 struct context *ctx;
187b3582 293
772a0e61
UH
294 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
295 sr_err("fx2lafw: %s: ctx malloc failed", __func__);
187b3582
JH
296 return NULL;
297 }
298
6c6781b6
JH
299 ctx->trigger_stage = TRIGGER_FIRED;
300
772a0e61 301 return ctx;
187b3582 302}
f302a082
JH
303
304/*
305 * API callbacks
306 */
307
308static int hw_init(const char *deviceinfo)
309{
187b3582
JH
310 struct sr_dev_inst *sdi;
311 struct libusb_device_descriptor des;
4679d14d 312 const struct fx2lafw_profile *fx2lafw_prof;
772a0e61 313 struct context *ctx;
187b3582 314 libusb_device **devlist;
ebc34738 315 int ret;
187b3582
JH
316 int devcnt = 0;
317 int i, j;
318
319 /* Avoid compiler warnings. */
f302a082 320 (void)deviceinfo;
187b3582
JH
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
ebc34738 331 if ((ret = libusb_get_device_descriptor(
187b3582 332 devlist[i], &des)) != 0) {
ebc34738 333 sr_warn("failed to get device descriptor: %d", ret);
187b3582
JH
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
90282c82
JH
355 ctx = fx2lafw_device_new();
356 ctx->profile = fx2lafw_prof;
357 sdi->priv = ctx;
be4b99e8 358 dev_insts = g_slist_append(dev_insts, sdi);
187b3582 359
b1eeb67e
JH
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 {
f8b07fc6
JH
368 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
369 fx2lafw_prof->firmware) == SR_OK)
b1eeb67e
JH
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
187b3582
JH
379 devcnt++;
380 }
381 libusb_free_device_list(devlist, 1);
382
383 return devcnt;
f302a082
JH
384}
385
772a0e61 386static int hw_dev_open(int dev_index)
f302a082 387{
43125c69
JH
388 GTimeVal cur_time;
389 struct sr_dev_inst *sdi;
772a0e61 390 struct context *ctx;
ebc34738 391 int timediff, ret;
43125c69 392
772a0e61 393 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
43125c69
JH
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 */
ebc34738 401 ret = 0;
43125c69
JH
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) {
ebc34738 408 if ((ret = fx2lafw_open_dev(dev_index)) == SR_OK)
43125c69
JH
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 {
ebc34738 416 ret = fx2lafw_open_dev(dev_index);
43125c69
JH
417 }
418
ebc34738 419 if (ret != SR_OK) {
43125c69
JH
420 sr_err("fx2lafw: unable to open device");
421 return SR_ERR;
422 }
423 ctx = sdi->priv;
424
ebc34738
UH
425 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
426 if (ret != 0) {
427 sr_err("fx2lafw: Unable to claim interface: %d", ret);
43125c69
JH
428 return SR_ERR;
429 }
430
f92994fd
JH
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
f302a082
JH
438 return SR_OK;
439}
440
f1898235 441static int hw_dev_close(int dev_index)
f302a082 442{
f1898235
JH
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__);
0abee507 447 return SR_ERR_BUG;
f1898235
JH
448 }
449
450 /* TODO */
451 close_dev(sdi);
452
f302a082
JH
453 return SR_OK;
454}
455
456static int hw_cleanup(void)
457{
187b3582
JH
458 GSList *l;
459 struct sr_dev_inst *sdi;
772a0e61 460 struct context *ctx;
62bc70e4 461 int ret = SR_OK;
187b3582 462
62bc70e4
JH
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);
187b3582
JH
478 sdi = l->data;
479 sr_dev_inst_free(sdi);
480 }
481
62bc70e4
JH
482 g_slist_free(dev_insts);
483 dev_insts = NULL;
187b3582
JH
484
485 if(usb_context)
486 libusb_exit(usb_context);
487 usb_context = NULL;
488
62bc70e4 489 return ret;
f302a082
JH
490}
491
772a0e61 492static void *hw_dev_info_get(int dev_index, int dev_info_id)
f302a082 493{
8b35f474 494 struct sr_dev_inst *sdi;
772a0e61 495 struct context *ctx;
8b35f474 496
772a0e61 497 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
8b35f474 498 return NULL;
cdfdd711 499 ctx = sdi->priv;
8b35f474 500
772a0e61 501 switch (dev_info_id) {
8b35f474
JH
502 case SR_DI_INST:
503 return sdi;
504 case SR_DI_NUM_PROBES:
cdfdd711 505 return GINT_TO_POINTER(ctx->profile->num_probes);
8b35f474
JH
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;
e3186647
JH
512 case SR_DI_CUR_SAMPLERATE:
513 return &ctx->cur_samplerate;
8b35f474
JH
514 }
515
f302a082
JH
516 return NULL;
517}
518
772a0e61 519static int hw_dev_status_get(int dev_index)
f302a082 520{
aae2fed6 521 const struct sr_dev_inst *const sdi =
772a0e61 522 sr_dev_inst_get(dev_insts, dev_index);
aae2fed6
JH
523
524 if (!sdi)
525 return SR_ST_NOT_FOUND;
526
527 return sdi->status;
f302a082
JH
528}
529
530static int *hw_hwcap_get_all(void)
531{
8b35f474 532 return fx2lafw_capabilities;
f302a082
JH
533}
534
7cb621d4 535static int hw_dev_config_set(int dev_index, int hwcap, void *value)
f302a082 536{
7cb621d4 537 struct sr_dev_inst *sdi;
772a0e61 538 struct context *ctx;
7cb621d4
JH
539 int ret;
540
541 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
542 return SR_ERR;
543 ctx = sdi->priv;
544
e3186647
JH
545 if (hwcap == SR_HWCAP_SAMPLERATE) {
546 ctx->cur_samplerate = *(uint64_t *)value;
547 ret = SR_OK;
548 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
6c6781b6 549 ret = configure_probes(ctx, (GSList *) value);
e3186647 550 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
7cb621d4
JH
551 ctx->limit_samples = *(uint64_t *)value;
552 ret = SR_OK;
553 } else {
554 ret = SR_ERR;
555 }
556
557 return ret;
f302a082
JH
558}
559
1f9813eb 560static int receive_data(int fd, int revents, void *cb_data)
610dbb70
JH
561{
562 struct timeval tv;
563
564 /* Avoid compiler warnings. */
565 (void)fd;
566 (void)revents;
1f9813eb 567 (void)cb_data;
610dbb70
JH
568
569 tv.tv_sec = tv.tv_usec = 0;
570 libusb_handle_events_timeout(usb_context, &tv);
571
572 return TRUE;
573}
574
2e526f4a
JH
575static 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
610dbb70
JH
587static void receive_transfer(struct libusb_transfer *transfer)
588{
589 /* TODO: These statics have to move to the ctx struct. */
610dbb70
JH
590 static int empty_transfer_count = 0;
591 struct sr_datafeed_packet packet;
592 struct sr_datafeed_logic logic;
2e526f4a 593 struct context *ctx = transfer->user_data;
6c6781b6 594 int cur_buflen, trigger_offset, i;
610dbb70
JH
595 unsigned char *cur_buf, *new_buf;
596
610dbb70
JH
597 /*
598 * If acquisition has already ended, just free any queued up
599 * transfer that come in.
600 */
2e526f4a 601 if (ctx->num_samples == -1) {
610dbb70
JH
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 */
2e526f4a 636 abort_acquisition(ctx);
610dbb70
JH
637 }
638 return;
639 } else {
640 empty_transfer_count = 0;
641 }
642
6c6781b6
JH
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 }
610dbb70 697
6c6781b6
JH
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 */
610dbb70
JH
718 }
719}
720
3cd3a20b 721static int hw_dev_acquisition_start(int dev_index, void *cb_data)
f302a082 722{
610dbb70
JH
723 struct sr_dev_inst *sdi;
724 struct sr_datafeed_packet *packet;
725 struct sr_datafeed_header *header;
772a0e61 726 struct context *ctx;
610dbb70
JH
727 struct libusb_transfer *transfer;
728 const struct libusb_pollfd **lupfd;
ebc34738 729 int ret, size, i;
610dbb70
JH
730 unsigned char *buf;
731
732 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
733 return SR_ERR;
734 ctx = sdi->priv;
3cd3a20b 735 ctx->session_dev_id = cb_data;
2e526f4a 736 ctx->num_samples = 0;
610dbb70
JH
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);
e3186647 778 header->samplerate = ctx->cur_samplerate;
610dbb70 779 header->num_logic_probes = ctx->profile->num_probes;
c8f2c9dd 780 sr_session_send(cb_data, packet);
610dbb70
JH
781 g_free(header);
782 g_free(packet);
783
ebc34738 784 if ((ret = command_start_acquisition (ctx->usb->devhdl,
017375d1 785 ctx->cur_samplerate)) != SR_OK) {
ebc34738 786 return ret;
017375d1
JH
787 }
788
f302a082
JH
789 return SR_OK;
790}
791
3cd3a20b 792/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
c8f2c9dd 793static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
f302a082 794{
2e526f4a 795 struct sr_dev_inst *sdi;
5da93902 796
2e526f4a
JH
797 /* unused parameter */
798 (void)cb_data;
5da93902 799
2e526f4a
JH
800 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
801 return SR_ERR;
802
803 abort_acquisition(sdi->priv);
5da93902 804
f302a082
JH
805 return SR_OK;
806}
807
c09f0b57 808SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
f302a082 809 .name = "fx2lafw",
2e7cb004 810 .longname = "fx2lafw (generic driver for FX2 based LAs)",
f302a082
JH
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};