Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 1 | #include "board.h" |
| 2 | #include "trace.h" |
| 3 | #include "usb_buf.h" |
Kévin Redon | a1012b1 | 2018-07-01 18:11:01 +0200 | [diff] [blame] | 4 | #include "simtrace_usb.h" |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 5 | |
Harald Welte | 9d90d28 | 2018-06-29 22:25:42 +0200 | [diff] [blame] | 6 | #include <osmocom/core/linuxlist.h> |
| 7 | #include <osmocom/core/msgb.h> |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 8 | #include <errno.h> |
| 9 | |
| 10 | #define USB_ALLOC_SIZE 280 |
| 11 | |
| 12 | static struct usb_buffered_ep usb_buffered_ep[BOARD_USB_NUMENDPOINTS]; |
| 13 | |
| 14 | struct 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 | |
| 26 | struct 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 */ |
| 35 | struct 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 */ |
| 47 | void usb_buf_free(struct msgb *msg) |
| 48 | { |
| 49 | msgb_free(msg); |
| 50 | } |
| 51 | |
| 52 | /* submit a USB buffer for transmission to host */ |
| 53 | int 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 | |
| 69 | void 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 | } |