blob: b8d4dd703a4af535592e5ae95c7ce7856ffab5fb [file] [log] [blame]
Harald Welte8e7fca32017-05-07 16:14:33 +02001#include "board.h"
2#include "trace.h"
3#include "usb_buf.h"
Kévin Redona1012b12018-07-01 18:11:01 +02004#include "simtrace_usb.h"
Harald Welte8e7fca32017-05-07 16:14:33 +02005
Harald Welte9d90d282018-06-29 22:25:42 +02006#include <osmocom/core/linuxlist.h>
7#include <osmocom/core/msgb.h>
Harald Welte8e7fca32017-05-07 16:14:33 +02008#include <errno.h>
9
10#define USB_ALLOC_SIZE 280
11
12static struct usb_buffered_ep usb_buffered_ep[BOARD_USB_NUMENDPOINTS];
13
14struct usb_buffered_ep *usb_get_buf_ep(uint8_t ep)
15{
16 if (ep >= ARRAY_SIZE(usb_buffered_ep))
17 return NULL;
18 return &usb_buffered_ep[ep];
19}
20
21
22/***********************************************************************
23 * User API
24 ***********************************************************************/
25
26struct llist_head *usb_get_queue(uint8_t ep)
27{
28 struct usb_buffered_ep *bep = usb_get_buf_ep(ep);
29 if (!bep)
30 return NULL;
31 return &bep->queue;
32}
33
34/* allocate a USB buffer for use with given end-point */
35struct msgb *usb_buf_alloc(uint8_t ep)
36{
37 struct msgb *msg;
38
39 msg = msgb_alloc(USB_ALLOC_SIZE, "USB");
40 if (!msg)
41 return NULL;
42 msg->dst = usb_get_buf_ep(ep);
43 return msg;
44}
45
46/* release/return the USB buffer to the pool */
47void usb_buf_free(struct msgb *msg)
48{
49 msgb_free(msg);
50}
51
52/* submit a USB buffer for transmission to host */
53int usb_buf_submit(struct msgb *msg)
54{
55 struct usb_buffered_ep *ep = msg->dst;
56
57 if (!msg->dst) {
58 TRACE_ERROR("%s: msg without dst\r\n", __func__);
59 usb_buf_free(msg);
60 return -EINVAL;
61 }
62
63 /* no need for irqsafe operation, as the usb_tx_queue is
64 * processed only by the main loop context */
65 msgb_enqueue(&ep->queue, msg);
66 return 0;
67}
68
69void usb_buf_init(void)
70{
71 unsigned int i;
72
73 for (i = 0; i < ARRAY_SIZE(usb_buffered_ep); i++) {
74 struct usb_buffered_ep *ep = &usb_buffered_ep[i];
75 INIT_LLIST_HEAD(&ep->queue);
76 }
77}