1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/sys/util.h>
8 #include <zephyr/drivers/usb/udc.h>
9 #include <zephyr/usb/usbd.h>
10
11 #include "usbd_device.h"
12 #include "usbd_class.h"
13 #include "usbd_ch9.h"
14 #include "usbd_desc.h"
15 #include "usbd_endpoint.h"
16
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(usbd_ep, CONFIG_USBD_LOG_LEVEL);
19
usbd_ep_enable(const struct device * dev,const struct usb_ep_descriptor * const ed,uint32_t * const ep_bm)20 int usbd_ep_enable(const struct device *dev,
21 const struct usb_ep_descriptor *const ed,
22 uint32_t *const ep_bm)
23 {
24 int ret;
25
26 ret = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
27 ed->wMaxPacketSize, ed->bInterval);
28 if (ret == 0) {
29 usbd_ep_bm_set(ep_bm, ed->bEndpointAddress);
30 }
31
32 return ret;
33 }
34
usbd_ep_disable(const struct device * dev,const uint8_t ep,uint32_t * const ep_bm)35 int usbd_ep_disable(const struct device *dev,
36 const uint8_t ep,
37 uint32_t *const ep_bm)
38 {
39 int ret;
40
41 ret = udc_ep_disable(dev, ep);
42 if (ret) {
43 return ret;
44 }
45
46 usbd_ep_bm_clear(ep_bm, ep);
47
48 ret = udc_ep_dequeue(dev, ep);
49 if (ret) {
50 return ret;
51 }
52
53 k_yield();
54
55 return ret;
56 }
57
usbd_ep_ctrl_set_zlp(struct usbd_context * const uds_ctx,struct net_buf * const buf)58 static void usbd_ep_ctrl_set_zlp(struct usbd_context *const uds_ctx,
59 struct net_buf *const buf)
60 {
61 struct usb_setup_packet *setup = usbd_get_setup_pkt(uds_ctx);
62 size_t min_len = MIN(setup->wLength, buf->len);
63
64 if (buf->len == 0) {
65 return;
66 }
67
68 /*
69 * Set ZLP flag when host asks for a bigger length and the
70 * last chunk is wMaxPacketSize long, to indicate the last
71 * packet.
72 */
73 if (setup->wLength > min_len && !(min_len % USB_CONTROL_EP_MPS)) {
74 /*
75 * Transfer length is less as requested by wLength and
76 * is multiple of wMaxPacketSize.
77 */
78 LOG_DBG("add ZLP, wLength %u buf length %zu",
79 setup->wLength, min_len);
80 udc_ep_buf_set_zlp(buf);
81
82 }
83 }
84
85 /*
86 * All the functions below are part of public USB device support API.
87 */
88
usbd_ep_ctrl_enqueue(struct usbd_context * const uds_ctx,struct net_buf * const buf)89 int usbd_ep_ctrl_enqueue(struct usbd_context *const uds_ctx,
90 struct net_buf *const buf)
91 {
92 struct udc_buf_info *bi;
93
94 bi = udc_get_buf_info(buf);
95 if (USB_EP_GET_IDX(bi->ep)) {
96 /* Not a control endpoint */
97 return -EINVAL;
98 }
99
100 if (USB_EP_DIR_IS_IN(bi->ep)) {
101 if (usbd_is_suspended(uds_ctx)) {
102 LOG_ERR("device is suspended");
103 return -EPERM;
104 }
105
106 usbd_ep_ctrl_set_zlp(uds_ctx, buf);
107 }
108
109 return udc_ep_enqueue(uds_ctx->dev, buf);
110 }
111
usbd_ep_buf_alloc(const struct usbd_class_data * const c_data,const uint8_t ep,const size_t size)112 struct net_buf *usbd_ep_buf_alloc(const struct usbd_class_data *const c_data,
113 const uint8_t ep, const size_t size)
114 {
115 struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
116
117 return udc_ep_buf_alloc(uds_ctx->dev, ep, size);
118 }
119
usbd_ep_enqueue(const struct usbd_class_data * const c_data,struct net_buf * const buf)120 int usbd_ep_enqueue(const struct usbd_class_data *const c_data,
121 struct net_buf *const buf)
122 {
123 struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
124 struct udc_buf_info *bi = udc_get_buf_info(buf);
125
126 if (USB_EP_DIR_IS_IN(bi->ep)) {
127 if (usbd_is_suspended(uds_ctx)) {
128 return -EPERM;
129 }
130 }
131
132 bi->owner = (void *)c_data;
133
134 return udc_ep_enqueue(uds_ctx->dev, buf);
135 }
136
usbd_ep_buf_free(struct usbd_context * const uds_ctx,struct net_buf * buf)137 int usbd_ep_buf_free(struct usbd_context *const uds_ctx, struct net_buf *buf)
138 {
139 return udc_ep_buf_free(uds_ctx->dev, buf);
140 }
141
usbd_ep_dequeue(struct usbd_context * const uds_ctx,const uint8_t ep)142 int usbd_ep_dequeue(struct usbd_context *const uds_ctx, const uint8_t ep)
143 {
144 return udc_ep_dequeue(uds_ctx->dev, ep);
145 }
146
usbd_ep_set_halt(struct usbd_context * const uds_ctx,const uint8_t ep)147 int usbd_ep_set_halt(struct usbd_context *const uds_ctx, const uint8_t ep)
148 {
149 struct usbd_ch9_data *ch9_data = &uds_ctx->ch9_data;
150 int ret;
151
152 ret = udc_ep_set_halt(uds_ctx->dev, ep);
153 if (ret) {
154 LOG_WRN("Set halt 0x%02x failed", ep);
155 return ret;
156 }
157
158 usbd_ep_bm_set(&ch9_data->ep_halt, ep);
159
160 return ret;
161 }
162
usbd_ep_clear_halt(struct usbd_context * const uds_ctx,const uint8_t ep)163 int usbd_ep_clear_halt(struct usbd_context *const uds_ctx, const uint8_t ep)
164 {
165 struct usbd_ch9_data *ch9_data = &uds_ctx->ch9_data;
166 int ret;
167
168 ret = udc_ep_clear_halt(uds_ctx->dev, ep);
169 if (ret) {
170 LOG_WRN("Clear halt 0x%02x failed", ep);
171 return ret;
172 }
173
174 usbd_ep_bm_clear(&ch9_data->ep_halt, ep);
175
176 return ret;
177 }
178
usbd_ep_is_halted(struct usbd_context * const uds_ctx,const uint8_t ep)179 bool usbd_ep_is_halted(struct usbd_context *const uds_ctx, const uint8_t ep)
180 {
181 struct usbd_ch9_data *ch9_data = &uds_ctx->ch9_data;
182
183 return usbd_ep_bm_is_set(&ch9_data->ep_halt, ep);
184 }
185