+ struct sr_dev_inst *sdi;
+ struct context *ctx;
+ int ret;
+
+ if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
+ return SR_ERR;
+ ctx = sdi->priv;
+
+ if (hwcap == SR_HWCAP_SAMPLERATE) {
+ ctx->cur_samplerate = *(uint64_t *)value;
+ ret = SR_OK;
+ } else if (hwcap == SR_HWCAP_PROBECONFIG) {
+ ret = SR_OK;
+ } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
+ ctx->limit_samples = *(uint64_t *)value;
+ ret = SR_OK;
+ } else {
+ ret = SR_ERR;
+ }
+
+ return ret;
+}
+
+static int receive_data(int fd, int revents, void *cb_data)
+{
+ struct timeval tv;
+
+ /* Avoid compiler warnings. */
+ (void)fd;
+ (void)revents;
+ (void)cb_data;
+
+ tv.tv_sec = tv.tv_usec = 0;
+ libusb_handle_events_timeout(usb_context, &tv);
+
+ return TRUE;
+}
+
+static void abort_acquisition(struct context *ctx)
+{
+ struct sr_datafeed_packet packet;
+
+ packet.type = SR_DF_END;
+ sr_session_send(ctx->session_dev_id, &packet);
+
+ ctx->num_samples = -1;
+
+ /* TODO: Need to cancel and free any queued up transfers. */
+}
+
+static void receive_transfer(struct libusb_transfer *transfer)
+{
+ /* TODO: These statics have to move to the ctx struct. */
+ static int empty_transfer_count = 0;
+ struct sr_datafeed_packet packet;
+ struct sr_datafeed_logic logic;
+ struct context *ctx = transfer->user_data;
+ int cur_buflen;
+ unsigned char *cur_buf, *new_buf;
+
+ /*
+ * If acquisition has already ended, just free any queued up
+ * transfer that come in.
+ */
+ if (ctx->num_samples == -1) {
+ if (transfer)
+ libusb_free_transfer(transfer);
+ return;
+ }
+
+ sr_info("fx2lafw: receive_transfer(): status %d received %d bytes",
+ transfer->status, transfer->actual_length);
+
+ /* Save incoming transfer before reusing the transfer struct. */
+ cur_buf = transfer->buffer;
+ cur_buflen = transfer->actual_length;
+ ctx = transfer->user_data;
+
+ /* Fire off a new request. */
+ if (!(new_buf = g_try_malloc(4096))) {
+ sr_err("fx2lafw: %s: new_buf malloc failed", __func__);
+ return; /* TODO: SR_ERR_MALLOC */
+ }
+
+ transfer->buffer = new_buf;
+ transfer->length = 4096;
+ if (libusb_submit_transfer(transfer) != 0) {
+ /* TODO: Stop session? */
+ /* TODO: Better error message. */
+ sr_err("fx2lafw: %s: libusb_submit_transfer error", __func__);
+ }
+
+ if (cur_buflen == 0) {
+ empty_transfer_count++;
+ if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
+ /*
+ * The FX2 gave up. End the acquisition, the frontend
+ * will work out that the samplecount is short.
+ */
+ abort_acquisition(ctx);
+ }
+ return;
+ } else {
+ empty_transfer_count = 0;
+ }
+
+ /* Send the incoming transfer to the session bus. */
+ packet.type = SR_DF_LOGIC;
+ packet.payload = &logic;
+ logic.length = cur_buflen;
+ logic.unitsize = 1;
+ logic.data = cur_buf;
+ sr_session_send(ctx->session_dev_id, &packet);
+ g_free(cur_buf);
+
+ ctx->num_samples += cur_buflen;
+ if (ctx->limit_samples &&
+ (unsigned int) ctx->num_samples > ctx->limit_samples) {
+ abort_acquisition(ctx);
+ }