40#include "usb_private.h"
50 usbd_dev->control_state.state = IDLE;
62static bool needs_zlp(uint16_t len, uint16_t wLength, uint8_t ep_size)
65 if (len && (len % ep_size == 0)) {
79 for (i = 0; i < MAX_USER_CONTROL_CALLBACK; i++) {
80 if (usbd_dev->user_control_callback[i].cb) {
84 usbd_dev->user_control_callback[i].type = type;
85 usbd_dev->user_control_callback[i].type_mask = type_mask;
86 usbd_dev->user_control_callback[i].cb = callback;
95 if (usbd_dev->desc->bMaxPacketSize0 <
96 usbd_dev->control_state.ctrl_len) {
99 usbd_dev->control_state.ctrl_buf,
100 usbd_dev->desc->bMaxPacketSize0);
101 usbd_dev->control_state.state = DATA_IN;
102 usbd_dev->control_state.ctrl_buf +=
103 usbd_dev->desc->bMaxPacketSize0;
104 usbd_dev->control_state.ctrl_len -=
105 usbd_dev->desc->bMaxPacketSize0;
109 usbd_dev->control_state.ctrl_buf,
110 usbd_dev->control_state.ctrl_len);
112 usbd_dev->control_state.state =
113 usbd_dev->control_state.needs_zlp ?
114 DATA_IN : LAST_DATA_IN;
115 usbd_dev->control_state.needs_zlp =
false;
116 usbd_dev->control_state.ctrl_len = 0;
117 usbd_dev->control_state.ctrl_buf = NULL;
123 uint16_t packetsize = MIN(usbd_dev->desc->bMaxPacketSize0,
124 usbd_dev->control_state.req.wLength -
125 usbd_dev->control_state.ctrl_len);
127 usbd_dev->control_state.ctrl_buf +
128 usbd_dev->control_state.ctrl_len,
131 if (size != packetsize) {
136 usbd_dev->control_state.ctrl_len += size;
146 struct user_control_callback *cb = usbd_dev->user_control_callback;
149 for (i = 0; i < MAX_USER_CONTROL_CALLBACK; i++) {
150 if (cb[i].cb == NULL) {
155 result = cb[i].cb(usbd_dev, req,
156 &(usbd_dev->control_state.ctrl_buf),
157 &(usbd_dev->control_state.ctrl_len),
158 &(usbd_dev->control_state.complete));
168 &(usbd_dev->control_state.ctrl_buf),
169 &(usbd_dev->control_state.ctrl_len));
176 usbd_dev->control_state.ctrl_buf = usbd_dev->ctrl_buf;
177 usbd_dev->control_state.ctrl_len = req->
wLength;
181 usbd_dev->control_state.needs_zlp =
182 needs_zlp(usbd_dev->control_state.ctrl_len,
184 usbd_dev->desc->bMaxPacketSize0);
190 usbd_dev->control_state.state = STATUS_IN;
201 if (req->
wLength > usbd_dev->ctrl_buf_len) {
207 usbd_dev->control_state.ctrl_buf = usbd_dev->ctrl_buf;
208 usbd_dev->control_state.ctrl_len = 0;
210 if (req->
wLength > usbd_dev->desc->bMaxPacketSize0) {
211 usbd_dev->control_state.state = DATA_OUT;
213 usbd_dev->control_state.state = LAST_DATA_OUT;
227 usbd_dev->control_state.complete = NULL;
244 switch (usbd_dev->control_state.state) {
249 if ((usbd_dev->control_state.req.wLength -
250 usbd_dev->control_state.ctrl_len) <=
251 usbd_dev->desc->bMaxPacketSize0) {
252 usbd_dev->control_state.state = LAST_DATA_OUT;
264 &(usbd_dev->control_state.req))) {
267 usbd_dev->control_state.state = STATUS_IN;
274 usbd_dev->control_state.state = IDLE;
275 if (usbd_dev->control_state.complete) {
276 usbd_dev->control_state.complete(usbd_dev,
277 &(usbd_dev->control_state.req));
279 usbd_dev->control_state.complete = NULL;
291 switch (usbd_dev->control_state.state) {
296 usbd_dev->control_state.state = STATUS_OUT;
300 if (usbd_dev->control_state.complete) {
301 usbd_dev->control_state.complete(usbd_dev,
302 &(usbd_dev->control_state.req));
308 usbd_dev->driver->set_address(usbd_dev, req->
wValue);
310 usbd_dev->control_state.state = IDLE;
static void usb_control_setup_read(usbd_device *usbd_dev, struct usb_setup_data *req)
static bool needs_zlp(uint16_t len, uint16_t wLength, uint8_t ep_size)
If we're replying with some data, but less than the host is expecting, then we normally just do a sho...
static void usb_control_send_chunk(usbd_device *usbd_dev)
int usbd_register_control_callback(usbd_device *usbd_dev, uint8_t type, uint8_t type_mask, usbd_control_callback callback)
Registers a control callback.
static void usb_control_setup_write(usbd_device *usbd_dev, struct usb_setup_data *req)
static enum usbd_request_return_codes usb_control_request_dispatch(usbd_device *usbd_dev, struct usb_setup_data *req)
static int usb_control_recv_chunk(usbd_device *usbd_dev)
static void stall_transaction(usbd_device *usbd_dev)
void usbd_ep_nak_set(usbd_device *usbd_dev, uint8_t addr, uint8_t nak)
Set an Out endpoint to NAK.
struct _usbd_device usbd_device
void usbd_ep_stall_set(usbd_device *usbd_dev, uint8_t addr, uint8_t stall)
Set/clear STALL condition on an endpoint.
uint16_t usbd_ep_write_packet(usbd_device *usbd_dev, uint8_t addr, const void *buf, uint16_t len)
Write a packet.
uint16_t usbd_ep_read_packet(usbd_device *usbd_dev, uint8_t addr, void *buf, uint16_t len)
Read a packet.
enum usbd_request_return_codes(* usbd_control_callback)(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, usbd_control_complete_callback *complete)
usbd_request_return_codes
#define USB_REQ_SET_ADDRESS
void _usbd_control_out(usbd_device *usbd_dev, uint8_t ea)
void _usbd_control_in(usbd_device *usbd_dev, uint8_t ea)
void _usbd_control_setup(usbd_device *usbd_dev, uint8_t ea)
enum usbd_request_return_codes _usbd_standard_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len)