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