Zephyr API Documentation  3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
udc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021-2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12#ifndef ZEPHYR_INCLUDE_UDC_H
13#define ZEPHYR_INCLUDE_UDC_H
14
15#include <zephyr/kernel.h>
16#include <zephyr/device.h>
17#include <zephyr/net/buf.h>
18#include <zephyr/sys/atomic.h>
19#include <zephyr/usb/usb_ch9.h>
20
29};
30
48 enum udc_mps0 mps0 : 2;
49};
50
63};
64
83};
84
99};
100
110 struct k_fifo fifo;
123};
124
125
149};
150
159struct udc_event {
162 union {
168 struct net_buf *buf;
169 };
171 const struct device *dev;
172};
173
185 unsigned int setup : 1;
187 unsigned int data : 1;
189 unsigned int status : 1;
191 unsigned int zlp : 1;
193 unsigned int claimed : 1;
195 unsigned int queued : 1;
197 void *owner;
199 int err;
200} __packed;
201
215typedef int (*udc_event_cb_t)(const struct device *dev,
216 const struct udc_event *const event);
217
224struct udc_api {
225 enum udc_bus_speed (*device_speed)(const struct device *dev);
226 int (*ep_enqueue)(const struct device *dev,
227 struct udc_ep_config *const cfg,
228 struct net_buf *const buf);
229 int (*ep_dequeue)(const struct device *dev,
230 struct udc_ep_config *const cfg);
231 int (*ep_set_halt)(const struct device *dev,
232 struct udc_ep_config *const cfg);
233 int (*ep_clear_halt)(const struct device *dev,
234 struct udc_ep_config *const cfg);
235 int (*ep_try_config)(const struct device *dev,
236 struct udc_ep_config *const cfg);
237 int (*ep_enable)(const struct device *dev,
238 struct udc_ep_config *const cfg);
239 int (*ep_disable)(const struct device *dev,
240 struct udc_ep_config *const cfg);
241 int (*host_wakeup)(const struct device *dev);
242 int (*set_address)(const struct device *dev,
243 const uint8_t addr);
244 int (*test_mode)(const struct device *dev,
245 const uint8_t mode, const bool dryrun);
246 int (*enable)(const struct device *dev);
247 int (*disable)(const struct device *dev);
248 int (*init)(const struct device *dev);
249 int (*shutdown)(const struct device *dev);
250 int (*lock)(const struct device *dev);
251 int (*unlock)(const struct device *dev);
252};
253
258#define UDC_STATUS_INITIALIZED 0
263#define UDC_STATUS_ENABLED 1
265#define UDC_STATUS_SUSPENDED 2
266
273struct udc_data {
285 int stage;
287 struct net_buf *setup;
289 void *priv;
290};
291
306static inline bool udc_is_initialized(const struct device *dev)
307{
308 struct udc_data *data = dev->data;
309
311}
312
320static inline bool udc_is_enabled(const struct device *dev)
321{
322 struct udc_data *data = dev->data;
323
325}
326
334static inline bool udc_is_suspended(const struct device *dev)
335{
336 struct udc_data *data = dev->data;
337
339}
340
355int udc_init(const struct device *dev, udc_event_cb_t event_cb);
356
369int udc_enable(const struct device *dev);
370
382int udc_disable(const struct device *dev);
383
395int udc_shutdown(const struct device *dev);
396
407static inline struct udc_device_caps udc_caps(const struct device *dev)
408{
409 struct udc_data *data = dev->data;
410
411 return data->caps;
412}
413
424enum udc_bus_speed udc_device_speed(const struct device *dev);
425
437static inline int udc_set_address(const struct device *dev, const uint8_t addr)
438{
439 const struct udc_api *api = dev->api;
440 int ret;
441
442 if (!udc_is_enabled(dev)) {
443 return -EPERM;
444 }
445
446 api->lock(dev);
447 ret = api->set_address(dev, addr);
448 api->unlock(dev);
449
450 return ret;
451}
452
468static inline int udc_test_mode(const struct device *dev,
469 const uint8_t mode, const bool dryrun)
470{
471 const struct udc_api *api = dev->api;
472 int ret;
473
474 if (!udc_is_enabled(dev)) {
475 return -EPERM;
476 }
477
478 if (api->test_mode != NULL) {
479 api->lock(dev);
480 ret = api->test_mode(dev, mode, dryrun);
481 api->unlock(dev);
482 } else {
483 ret = -ENOTSUP;
484 }
485
486 return ret;
487}
488
499static inline int udc_host_wakeup(const struct device *dev)
500{
501 const struct udc_api *api = dev->api;
502 int ret;
503
504 if (!udc_is_enabled(dev)) {
505 return -EPERM;
506 }
507
508 api->lock(dev);
509 ret = api->host_wakeup(dev);
510 api->unlock(dev);
511
512 return ret;
513}
514
536int udc_ep_try_config(const struct device *dev,
537 const uint8_t ep,
538 const uint8_t attributes,
539 uint16_t *const mps,
540 const uint8_t interval);
541
560int udc_ep_enable(const struct device *dev,
561 const uint8_t ep,
562 const uint8_t attributes,
563 const uint16_t mps,
564 const uint8_t interval);
565
580int udc_ep_disable(const struct device *dev, const uint8_t ep);
581
595int udc_ep_set_halt(const struct device *dev, const uint8_t ep);
596
610int udc_ep_clear_halt(const struct device *dev, const uint8_t ep);
611
627int udc_ep_enqueue(const struct device *dev, struct net_buf *const buf);
628
645int udc_ep_dequeue(const struct device *dev, const uint8_t ep);
646
658struct net_buf *udc_ep_buf_alloc(const struct device *dev,
659 const uint8_t ep,
660 const size_t size);
661
672int udc_ep_buf_free(const struct device *dev, struct net_buf *const buf);
673
681static inline void udc_ep_buf_set_zlp(struct net_buf *const buf)
682{
683 struct udc_buf_info *bi;
684
685 __ASSERT_NO_MSG(buf);
686 bi = (struct udc_buf_info *)net_buf_user_data(buf);
687 if (USB_EP_DIR_IS_IN(bi->ep)) {
688 bi->zlp = 1;
689 }
690}
691
699static inline struct udc_buf_info *udc_get_buf_info(const struct net_buf *const buf)
700{
701 __ASSERT_NO_MSG(buf);
702 return (struct udc_buf_info *)net_buf_user_data(buf);
703}
704
709#endif /* ZEPHYR_INCLUDE_UDC_H */
long atomic_t
Definition: atomic_types.h:15
static bool atomic_test_bit(const atomic_t *target, int bit)
Atomically test a bit.
Definition: atomic.h:127
static void * net_buf_user_data(const struct net_buf *buf)
Get a pointer to the user data of a buffer.
Definition: buf.h:1582
#define ENOTSUP
Unsupported value.
Definition: errno.h:114
#define EPERM
Not owner.
Definition: errno.h:39
static struct udc_buf_info * udc_get_buf_info(const struct net_buf *const buf)
Get requests metadata.
Definition: udc.h:699
static int udc_host_wakeup(const struct device *dev)
Initiate host wakeup procedure.
Definition: udc.h:499
int udc_ep_set_halt(const struct device *dev, const uint8_t ep)
Halt endpoint.
static bool udc_is_enabled(const struct device *dev)
Checks whether the controller is enabled.
Definition: udc.h:320
static struct udc_device_caps udc_caps(const struct device *dev)
Get USB device controller capabilities.
Definition: udc.h:407
static bool udc_is_initialized(const struct device *dev)
Checks whether the controller is initialized.
Definition: udc.h:306
static void udc_ep_buf_set_zlp(struct net_buf *const buf)
Set ZLP flag in requests metadata.
Definition: udc.h:681
int udc_ep_disable(const struct device *dev, const uint8_t ep)
Disable endpoint.
int udc_shutdown(const struct device *dev)
Poweroff USB device controller.
int udc_ep_dequeue(const struct device *dev, const uint8_t ep)
Remove all USB device controller requests from endpoint queue.
static int udc_set_address(const struct device *dev, const uint8_t addr)
Set USB device address.
Definition: udc.h:437
static int udc_test_mode(const struct device *dev, const uint8_t mode, const bool dryrun)
Enable Test Mode.
Definition: udc.h:468
int udc_ep_try_config(const struct device *dev, const uint8_t ep, const uint8_t attributes, uint16_t *const mps, const uint8_t interval)
Try an endpoint configuration.
static bool udc_is_suspended(const struct device *dev)
Checks whether the controller is suspended.
Definition: udc.h:334
int udc_disable(const struct device *dev)
Disable USB device controller.
int udc_init(const struct device *dev, udc_event_cb_t event_cb)
Initialize USB device controller.
enum udc_bus_speed udc_device_speed(const struct device *dev)
Get actual USB device speed.
int udc_ep_enqueue(const struct device *dev, struct net_buf *const buf)
Queue USB device controller request.
int udc_ep_enable(const struct device *dev, const uint8_t ep, const uint8_t attributes, const uint16_t mps, const uint8_t interval)
Configure and enable endpoint.
struct net_buf * udc_ep_buf_alloc(const struct device *dev, const uint8_t ep, const size_t size)
Allocate UDC request buffer.
int udc_ep_clear_halt(const struct device *dev, const uint8_t ep)
Clear endpoint halt.
int udc_ep_buf_free(const struct device *dev, struct net_buf *const buf)
Free UDC request buffer.
int udc_enable(const struct device *dev)
Enable USB device controller.
Public kernel APIs.
Buffer management.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition: device.h:403
void * data
Address of the device instance private data.
Definition: device.h:413
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:409
Definition: kernel.h:2391
Mutex Structure.
Definition: kernel.h:2917
Network buffer representation.
Definition: buf.h:1004
uint16_t size
Amount of data that this buffer can store.
Definition: buf.h:1036
Definition: stat.h:92
UDC driver API This is the mandatory API any USB device controller driver needs to expose with except...
Definition: udc.h:224
int(* init)(const struct device *dev)
Definition: udc.h:248
int(* ep_dequeue)(const struct device *dev, struct udc_ep_config *const cfg)
Definition: udc.h:229
int(* ep_disable)(const struct device *dev, struct udc_ep_config *const cfg)
Definition: udc.h:239
int(* unlock)(const struct device *dev)
Definition: udc.h:251
int(* ep_enable)(const struct device *dev, struct udc_ep_config *const cfg)
Definition: udc.h:237
int(* set_address)(const struct device *dev, const uint8_t addr)
Definition: udc.h:242
int(* ep_try_config)(const struct device *dev, struct udc_ep_config *const cfg)
Definition: udc.h:235
int(* disable)(const struct device *dev)
Definition: udc.h:247
int(* ep_clear_halt)(const struct device *dev, struct udc_ep_config *const cfg)
Definition: udc.h:233
int(* ep_enqueue)(const struct device *dev, struct udc_ep_config *const cfg, struct net_buf *const buf)
Definition: udc.h:226
int(* test_mode)(const struct device *dev, const uint8_t mode, const bool dryrun)
Definition: udc.h:244
int(* lock)(const struct device *dev)
Definition: udc.h:250
int(* shutdown)(const struct device *dev)
Definition: udc.h:249
int(* ep_set_halt)(const struct device *dev, struct udc_ep_config *const cfg)
Definition: udc.h:231
enum udc_bus_speed(* device_speed)(const struct device *dev)
Definition: udc.h:225
int(* host_wakeup)(const struct device *dev)
Definition: udc.h:241
int(* enable)(const struct device *dev)
Definition: udc.h:246
UDC endpoint buffer info.
Definition: udc.h:181
uint8_t ep
Endpoint to which request is associated.
Definition: udc.h:183
void * owner
Transfer owner (usually pointer to a class instance)
Definition: udc.h:197
unsigned int queued
Flag marks request buffer is queued (TBD)
Definition: udc.h:195
unsigned int status
Flag marks status stage of setup transfer.
Definition: udc.h:189
unsigned int setup
Flag marks setup transfer.
Definition: udc.h:185
unsigned int claimed
Flag marks request buffer claimed by the controller (TBD)
Definition: udc.h:193
unsigned int data
Flag marks data stage of setup transfer.
Definition: udc.h:187
int err
Transfer result, 0 on success, other values on error.
Definition: udc.h:199
unsigned int zlp
Flag marks ZLP at the end of a transfer.
Definition: udc.h:191
Common UDC driver data structure.
Definition: udc.h:273
struct k_mutex mutex
Driver access mutex.
Definition: udc.h:279
struct net_buf * setup
Pointer to buffer containing setup packet.
Definition: udc.h:287
udc_event_cb_t event_cb
Callback to submit an UDC event to upper layer.
Definition: udc.h:281
atomic_t status
USB device controller status.
Definition: udc.h:283
struct udc_device_caps caps
Controller capabilities.
Definition: udc.h:277
int stage
Internal used Control Sequence Stage.
Definition: udc.h:285
void * priv
Driver private data.
Definition: udc.h:289
struct udc_ep_config * ep_lut[32]
LUT for endpoint management.
Definition: udc.h:275
USB device controller capabilities.
Definition: udc.h:36
uint32_t out_ack
Controller performs status OUT stage automatically.
Definition: udc.h:42
enum udc_mps0 mps0
Maximum packet size for control endpoint.
Definition: udc.h:48
uint32_t can_detect_vbus
Controller can detect the state change of USB supply VBUS.
Definition: udc.h:46
uint32_t rwup
Controller supports USB remote wakeup.
Definition: udc.h:40
uint32_t hs
USB high speed capable controller.
Definition: udc.h:38
uint32_t addr_before_status
Controller expects device address to be set before status stage.
Definition: udc.h:44
USB device controller endpoint capabilities.
Definition: udc.h:68
uint32_t iso
ISO transfer capable endpoint.
Definition: udc.h:78
uint32_t in
IN transfer capable endpoint.
Definition: udc.h:80
uint32_t mps
Maximum packet size of the endpoint buffer.
Definition: udc.h:70
uint32_t bulk
Bulk transfer capable endpoint.
Definition: udc.h:76
uint32_t interrupt
Interrupt transfer capable endpoint.
Definition: udc.h:74
uint32_t control
Control transfer capable endpoint (for completeness)
Definition: udc.h:72
uint32_t out
OUT transfer capable endpoint.
Definition: udc.h:82
USB device controller endpoint configuration.
Definition: udc.h:108
uint8_t interval
Polling interval.
Definition: udc.h:122
struct udc_ep_caps caps
Endpoint capabilities.
Definition: udc.h:112
struct k_fifo fifo
Endpoint requests FIFO.
Definition: udc.h:110
uint8_t addr
Endpoint address.
Definition: udc.h:116
uint8_t attributes
Endpoint attributes.
Definition: udc.h:118
uint16_t mps
Maximum packet size.
Definition: udc.h:120
USB device controller endpoint status.
Definition: udc.h:88
uint32_t odd
If double buffering is supported, last used buffer is odd.
Definition: udc.h:96
uint32_t data1
Last submitted PID is DATA1.
Definition: udc.h:94
uint32_t enabled
Endpoint is enabled.
Definition: udc.h:90
uint32_t busy
Endpoint is busy.
Definition: udc.h:98
uint32_t halted
Endpoint is halted (returning STALL PID)
Definition: udc.h:92
USB device controller event.
Definition: udc.h:159
int status
Event status value, if any.
Definition: udc.h:166
enum udc_event_type type
Event type.
Definition: udc.h:161
const struct device * dev
Pointer to device struct.
Definition: udc.h:171
struct net_buf * buf
Pointer to request used only for UDC_EVT_EP_REQUEST.
Definition: udc.h:168
uint32_t value
Event value.
Definition: udc.h:164
int(* udc_event_cb_t)(const struct device *dev, const struct udc_event *const event)
Callback to submit UDC event to higher layer.
Definition: udc.h:215
udc_event_type
USB device controller event types.
Definition: udc.h:129
@ UDC_EVT_VBUS_READY
VBUS ready event.
Definition: udc.h:131
@ UDC_EVT_RESUME
Device resume event.
Definition: udc.h:135
@ UDC_EVT_SUSPEND
Device suspended event.
Definition: udc.h:137
@ UDC_EVT_VBUS_REMOVED
VBUS removed event.
Definition: udc.h:133
@ UDC_EVT_EP_REQUEST
Endpoint request result event.
Definition: udc.h:143
@ UDC_EVT_ERROR
Non-correctable error event, requires attention from higher levels or application.
Definition: udc.h:148
@ UDC_EVT_RESET
Port reset detected.
Definition: udc.h:139
@ UDC_EVT_SOF
Start of Frame event.
Definition: udc.h:141
udc_bus_speed
USB device actual speed.
Definition: udc.h:54
@ UDC_BUS_SPEED_SS
Device is connected to a super speed bus.
Definition: udc.h:62
@ UDC_BUS_SPEED_FS
Device is connected to a full speed bus.
Definition: udc.h:58
@ UDC_BUS_UNKNOWN
Device is probably not connected.
Definition: udc.h:56
@ UDC_BUS_SPEED_HS
Device is connected to a high speed bus
Definition: udc.h:60
#define UDC_STATUS_INITIALIZED
Controller is initialized by udc_init() and can generate the VBUS events, if capable,...
Definition: udc.h:258
udc_mps0
Maximum packet size of control endpoint supported by the controller.
Definition: udc.h:24
@ UDC_MPS0_32
Definition: udc.h:27
@ UDC_MPS0_16
Definition: udc.h:26
@ UDC_MPS0_64
Definition: udc.h:28
@ UDC_MPS0_8
Definition: udc.h:25
#define UDC_STATUS_SUSPENDED
Controller is suspended by the host.
Definition: udc.h:265
#define UDC_STATUS_ENABLED
Controller is enabled and all API functions are available, controller is recognizable by host.
Definition: udc.h:263
USB Chapter 9 structures and definitions.
#define USB_EP_DIR_IS_IN(ep)
True if the endpoint is an IN endpoint.
Definition: usb_ch9.h:313