142 lines
3.6 KiB
C
142 lines
3.6 KiB
C
/****************************************************************************
|
|
* buffered endpoint
|
|
****************************************************************************/
|
|
|
|
/* USB buffer library
|
|
*
|
|
* 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 2 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.
|
|
*/
|
|
|
|
#include "tusb.h"
|
|
#include "device/usbd_pvt.h"
|
|
#include "trace.h"
|
|
#include <libsimtrace/usb_buffered_ep.h>
|
|
#include <errno.h>
|
|
#include <osmocom/core/linuxlist.h>
|
|
#include <osmocom/core/msgb.h>
|
|
|
|
#define USB_MAX_QLEN 3
|
|
#define USB_ALLOC_SIZE 280
|
|
#define USB_HEADROOM_SIZE 8
|
|
|
|
void bep_init(struct usb_buffered_ep *bep, uint8_t ep)
|
|
{
|
|
bep->ep = ep;
|
|
bep->msg_in_progress = NULL;
|
|
INIT_LLIST_HEAD(&bep->queue);
|
|
bep->queue_len = 0;
|
|
}
|
|
|
|
struct msgb *bep_msgb_alloc(struct usb_buffered_ep *bep)
|
|
{
|
|
struct msgb *msg = msgb_alloc_headroom(USB_ALLOC_SIZE, USB_HEADROOM_SIZE, "USB");
|
|
if (!msg)
|
|
return NULL;
|
|
msg->dst = bep;
|
|
return msg;
|
|
}
|
|
|
|
void bep_msgb_free(struct msgb *msg)
|
|
{
|
|
msgb_free(msg);
|
|
}
|
|
|
|
/* IN/IRQ EP: dequeue next pending message and send it to host */
|
|
int bep_refill_to_host(struct usb_buffered_ep *bep, bool is_out_ep)
|
|
{
|
|
unsigned long x;
|
|
const uint8_t rhport = 0;
|
|
unsigned int len;
|
|
|
|
if (usbd_edpt_busy(rhport, bep->ep)) {
|
|
LOGBEP(bep, "skipping: edpt_busy\r\n");
|
|
return 0;
|
|
}
|
|
|
|
local_irq_save(x);
|
|
if (bep->msg_in_progress) {
|
|
local_irq_restore(x);
|
|
LOGBEP(bep, "skipping: msg_in_progress\r\n");
|
|
return 0;
|
|
}
|
|
|
|
if (llist_empty(&bep->queue)) {
|
|
local_irq_restore(x);
|
|
LOGBEP(bep, "skipping: queue empty\r\n");
|
|
return 0;
|
|
}
|
|
|
|
bep->msg_in_progress = msgb_dequeue_count(&bep->queue, &bep->queue_len);
|
|
local_irq_restore(x);
|
|
|
|
if (is_out_ep)
|
|
len = msgb_tailroom(bep->msg_in_progress);
|
|
else
|
|
len = msgb_length(bep->msg_in_progress);
|
|
|
|
TU_VERIFY(usbd_edpt_xfer(rhport, bep->ep, msgb_data(bep->msg_in_progress), len));
|
|
|
|
LOGBEP(bep, "success (msg=%p, data=%p/%u)!\r\n", bep->msg_in_progress, msgb_data(bep->msg_in_progress), len);
|
|
return 1;
|
|
}
|
|
|
|
/* assume the last in-progress msgb has completed + return it */
|
|
struct msgb *bep_get_completed(struct usb_buffered_ep *bep)
|
|
{
|
|
unsigned long x;
|
|
struct msgb *msg;
|
|
|
|
local_irq_save(x);
|
|
msg = bep->msg_in_progress;
|
|
bep->msg_in_progress = NULL;
|
|
local_irq_restore(x);
|
|
LOGBEP(bep, "(msg=%p)\r\n", msg);
|
|
|
|
return msg;
|
|
}
|
|
|
|
/* assume the last in-progress msgb has completed + free it */
|
|
void bep_complete_in_progress(struct usb_buffered_ep *bep)
|
|
{
|
|
struct msgb *msg = bep_get_completed(bep);
|
|
LOGBEP(bep, "(msg=%p)\r\n", msg);
|
|
bep_msgb_free(msg);
|
|
}
|
|
|
|
/* enqueue a USB buffer for transmission to host */
|
|
int bep_enqueue_msgb(struct msgb *msg)
|
|
{
|
|
struct usb_buffered_ep *ep = msg->dst;
|
|
|
|
if (!msg->dst) {
|
|
TRACE_ERROR("%s: msg without dst\r\n", __func__);
|
|
bep_msgb_free(msg);
|
|
return -EINVAL;
|
|
}
|
|
|
|
/* no need for irqsafe operation, as the usb_tx_queue is
|
|
* processed only by the main loop context */
|
|
|
|
if (ep->queue_len >= USB_MAX_QLEN) {
|
|
struct msgb *evict;
|
|
/* free the first pending buffer in the queue */
|
|
TRACE_INFO("EP%02x: dropping first queue element (qlen=%u)\r\n",
|
|
ep->ep, ep->queue_len);
|
|
evict = msgb_dequeue_count(&ep->queue, &ep->queue_len);
|
|
OSMO_ASSERT(evict);
|
|
bep_msgb_free(evict);
|
|
}
|
|
|
|
LOGBEP(ep, "(msg=%p)\r\n", msg);
|
|
msgb_enqueue_count(&ep->queue, msg, &ep->queue_len);
|
|
return 0;
|
|
}
|