From 0f3ba45c0b1eb233a66ebd4191ecd39c0c62adad Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 25 May 2017 21:29:03 +0100 Subject: usb/txhandler: generate transmission handling --- usb/txhandler.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 usb/txhandler.c (limited to 'usb/txhandler.c') diff --git a/usb/txhandler.c b/usb/txhandler.c new file mode 100644 index 0000000..89cd0cf --- /dev/null +++ b/usb/txhandler.c @@ -0,0 +1,82 @@ +/* + * usb/txhandler.c -- USB Transmission Handling + * + * Copyright (C) 2017 Tomasz Kramkowski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include + +#include "bdt.h" +#include "txhandler.h" + +/* puttx: place data in the current buffer descriptor */ +static bool puttx(struct tx_ctx *tx, void *data, size_t size) +{ + if (GET_BIT(tx->bdt[tx->odd].desc, BD_OWN)) + return false; + + tx->bdt[tx->odd].addr = data; + tx->bdt[tx->odd].desc = USB0_BD_INIT(size, tx->data01); + tx->odd = !tx->odd; + tx->data01 = !tx->data01; + + return true; +} + +/* tx_push: attempt to push rest of the current transmission into a BD */ +bool tx_push(struct tx_ctx *tx) +{ + size_t size = tx->size; + + if (tx->data == NULL) + return false; + + if (size > tx->max) + size = tx->max; + + if (!puttx(tx, tx->data, size)) + return false; + + tx->data = (char *)tx->data + size; + tx->size -= size; + + if (tx->size == 0 && size < tx->max) + tx->data = NULL; + + return true; +} + +/* tx_que: enqueue a transmission */ +void tx_que(struct tx_ctx *tx, void *data, size_t size) +{ + if (data == NULL || size == 0) { + puttx(tx, NULL, 0); + return; + } + + tx->data = data; + tx->size = size; + + while (tx_push(tx)) + ; +} + +/* tx_isempty: check if there's nothing left to transmit */ +bool tx_isempty(struct tx_ctx *tx) +{ + return tx->data == NULL; +} -- cgit v1.2.3-54-g00ecf