Zephyr API Documentation 4.0.0-rc2
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>
18#include <zephyr/sys/atomic.h>
19#include <zephyr/usb/usb_ch9.h>
20
30
50
64
86
102
126
127
152
161struct udc_event {
164 union {
170 struct net_buf *buf;
171 };
173 const struct device *dev;
174};
175
187 unsigned int setup : 1;
189 unsigned int data : 1;
191 unsigned int status : 1;
193 unsigned int zlp : 1;
195 unsigned int claimed : 1;
197 unsigned int queued : 1;
199 void *owner;
201 int err;
202} __packed;
203
217typedef int (*udc_event_cb_t)(const struct device *dev,
218 const struct udc_event *const event);
219
226struct udc_api {
227 enum udc_bus_speed (*device_speed)(const struct device *dev);
228 int (*ep_enqueue)(const struct device *dev,
229 struct udc_ep_config *const cfg,
230 struct net_buf *const buf);
231 int (*ep_dequeue)(const struct device *dev,
232 struct udc_ep_config *const cfg);
233 int (*ep_set_halt)(const struct device *dev,
234 struct udc_ep_config *const cfg);
235 int (*ep_clear_halt)(const struct device *dev,
236 struct udc_ep_config *const cfg);
237 int (*ep_try_config)(const struct device *dev,
238 struct udc_ep_config *const cfg);
239 int (*ep_enable)(const struct device *dev,
240 struct udc_ep_config *const cfg);
241 int (*ep_disable)(const struct device *dev,
242 struct udc_ep_config *const cfg);
243 int (*host_wakeup)(const struct device *dev);
244 int (*set_address)(const struct device *dev,
245 const uint8_t addr);
246 int (*test_mode)(const struct device *dev,
247 const uint8_t mode, const bool dryrun);
248 int (*enable)(const struct device *dev);
249 int (*disable)(const struct device *dev);
250 int (*init)(const struct device *dev);
251 int (*shutdown)(const struct device *dev);
252 int (*lock)(const struct device *dev);
253 int (*unlock)(const struct device *dev);
254};
255
260#define UDC_STATUS_INITIALIZED 0
265#define UDC_STATUS_ENABLED 1
267#define UDC_STATUS_SUSPENDED 2
268
295
312static inline bool udc_is_initialized(const struct device *dev)
313{
314 struct udc_data *data = dev->data;
315
317}
318
326static inline bool udc_is_enabled(const struct device *dev)
327{
328 struct udc_data *data = dev->data;
329
331}
332
340static inline bool udc_is_suspended(const struct device *dev)
341{
342 struct udc_data *data = dev->data;
343
345}
346
362int udc_init(const struct device *dev,
363 udc_event_cb_t event_cb, const void *const event_ctx);
364
378int udc_enable(const struct device *dev);
379
391int udc_disable(const struct device *dev);
392
404int udc_shutdown(const struct device *dev);
405
416static inline struct udc_device_caps udc_caps(const struct device *dev)
417{
418 struct udc_data *data = dev->data;
419
420 return data->caps;
421}
422
433enum udc_bus_speed udc_device_speed(const struct device *dev);
434
446static inline int udc_set_address(const struct device *dev, const uint8_t addr)
447{
448 const struct udc_api *api = dev->api;
449 int ret;
450
451 if (!udc_is_enabled(dev)) {
452 return -EPERM;
453 }
454
455 api->lock(dev);
456 ret = api->set_address(dev, addr);
457 api->unlock(dev);
458
459 return ret;
460}
461
477static inline int udc_test_mode(const struct device *dev,
478 const uint8_t mode, const bool dryrun)
479{
480 const struct udc_api *api = dev->api;
481 int ret;
482
483 if (!udc_is_enabled(dev)) {
484 return -EPERM;
485 }
486
487 if (api->test_mode != NULL) {
488 api->lock(dev);
489 ret = api->test_mode(dev, mode, dryrun);
490 api->unlock(dev);
491 } else {
492 ret = -ENOTSUP;
493 }
494
495 return ret;
496}
497
508static inline int udc_host_wakeup(const struct device *dev)
509{
510 const struct udc_api *api = dev->api;
511 int ret;
512
513 if (!udc_is_enabled(dev)) {
514 return -EPERM;
515 }
516
517 api->lock(dev);
518 ret = api->host_wakeup(dev);
519 api->unlock(dev);
520
521 return ret;
522}
523
545int udc_ep_try_config(const struct device *dev,
546 const uint8_t ep,
547 const uint8_t attributes,
548 uint16_t *const mps,
549 const uint8_t interval);
550
569int udc_ep_enable(const struct device *dev,
570 const uint8_t ep,
571 const uint8_t attributes,
572 const uint16_t mps,
573 const uint8_t interval);
574
589int udc_ep_disable(const struct device *dev, const uint8_t ep);
590
604int udc_ep_set_halt(const struct device *dev, const uint8_t ep);
605
619int udc_ep_clear_halt(const struct device *dev, const uint8_t ep);
620
636int udc_ep_enqueue(const struct device *dev, struct net_buf *const buf);
637
654int udc_ep_dequeue(const struct device *dev, const uint8_t ep);
655
667struct net_buf *udc_ep_buf_alloc(const struct device *dev,
668 const uint8_t ep,
669 const size_t size);
670
681int udc_ep_buf_free(const struct device *dev, struct net_buf *const buf);
682
690static inline void udc_ep_buf_set_zlp(struct net_buf *const buf)
691{
692 struct udc_buf_info *bi;
693
694 __ASSERT_NO_MSG(buf);
695 bi = (struct udc_buf_info *)net_buf_user_data(buf);
696 if (USB_EP_DIR_IS_IN(bi->ep)) {
697 bi->zlp = 1;
698 }
699}
700
708static inline struct udc_buf_info *udc_get_buf_info(const struct net_buf *const buf)
709{
710 __ASSERT_NO_MSG(buf);
711 return (struct udc_buf_info *)net_buf_user_data(buf);
712}
713
714
725static inline const void *udc_get_event_ctx(const struct device *dev)
726{
727 struct udc_data *data = dev->data;
728
729 return data->event_ctx;
730}
731
739static inline uint16_t udc_mps_ep_size(const struct udc_ep_config *const cfg)
740{
741 return USB_MPS_EP_SIZE(cfg->mps);
742}
743
748#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 get and 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 net_buf.h:1576
#define ENOTSUP
Unsupported value.
Definition errno.h:114
#define EPERM
Not owner.
Definition errno.h:39
int udc_init(const struct device *dev, udc_event_cb_t event_cb, const void *const event_ctx)
Initialize USB device controller.
static struct udc_buf_info * udc_get_buf_info(const struct net_buf *const buf)
Get requests metadata.
Definition udc.h:708
static int udc_host_wakeup(const struct device *dev)
Initiate host wakeup procedure.
Definition udc.h:508
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:326
static const void * udc_get_event_ctx(const struct device *dev)
Get pointer to higher layer context.
Definition udc.h:725
static struct udc_device_caps udc_caps(const struct device *dev)
Get USB device controller capabilities.
Definition udc.h:416
static bool udc_is_initialized(const struct device *dev)
Checks whether the controller is initialized.
Definition udc.h:312
static void udc_ep_buf_set_zlp(struct net_buf *const buf)
Set ZLP flag in requests metadata.
Definition udc.h:690
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.
static uint16_t udc_mps_ep_size(const struct udc_ep_config *const cfg)
Get endpoint size from UDC endpoint configuration.
Definition udc.h:739
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:446
static int udc_test_mode(const struct device *dev, const uint8_t mode, const bool dryrun)
Enable Test Mode.
Definition udc.h:477
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:340
int udc_disable(const struct device *dev)
Disable 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.
__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:412
void * data
Address of the device instance private data.
Definition device.h:422
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:418
Definition kernel.h:2468
Mutex Structure.
Definition kernel.h:2994
Network buffer representation.
Definition net_buf.h:1006
uint16_t size
Amount of data that this buffer can store.
Definition net_buf.h:1038
Definition stat.h:57
UDC driver API This is the mandatory API any USB device controller driver needs to expose with except...
Definition udc.h:226
int(* init)(const struct device *dev)
Definition udc.h:250
int(* ep_dequeue)(const struct device *dev, struct udc_ep_config *const cfg)
Definition udc.h:231
int(* ep_disable)(const struct device *dev, struct udc_ep_config *const cfg)
Definition udc.h:241
int(* unlock)(const struct device *dev)
Definition udc.h:253
int(* ep_enable)(const struct device *dev, struct udc_ep_config *const cfg)
Definition udc.h:239
int(* set_address)(const struct device *dev, const uint8_t addr)
Definition udc.h:244
int(* ep_try_config)(const struct device *dev, struct udc_ep_config *const cfg)
Definition udc.h:237
int(* disable)(const struct device *dev)
Definition udc.h:249
int(* ep_clear_halt)(const struct device *dev, struct udc_ep_config *const cfg)
Definition udc.h:235
int(* ep_enqueue)(const struct device *dev, struct udc_ep_config *const cfg, struct net_buf *const buf)
Definition udc.h:228
int(* test_mode)(const struct device *dev, const uint8_t mode, const bool dryrun)
Definition udc.h:246
int(* lock)(const struct device *dev)
Definition udc.h:252
int(* shutdown)(const struct device *dev)
Definition udc.h:251
int(* ep_set_halt)(const struct device *dev, struct udc_ep_config *const cfg)
Definition udc.h:233
enum udc_bus_speed(* device_speed)(const struct device *dev)
Definition udc.h:227
int(* host_wakeup)(const struct device *dev)
Definition udc.h:243
int(* enable)(const struct device *dev)
Definition udc.h:248
UDC endpoint buffer info.
Definition udc.h:183
uint8_t ep
Endpoint to which request is associated.
Definition udc.h:185
void * owner
Transfer owner (usually pointer to a class instance)
Definition udc.h:199
unsigned int queued
Flag marks request buffer is queued (TBD)
Definition udc.h:197
unsigned int status
Flag marks status stage of setup transfer.
Definition udc.h:191
unsigned int setup
Flag marks setup transfer.
Definition udc.h:187
unsigned int claimed
Flag marks request buffer claimed by the controller (TBD)
Definition udc.h:195
unsigned int data
Flag marks data stage of setup transfer.
Definition udc.h:189
int err
Transfer result, 0 on success, other values on error.
Definition udc.h:201
unsigned int zlp
Flag marks ZLP at the end of a transfer.
Definition udc.h:193
Common UDC driver data structure.
Definition udc.h:275
struct k_mutex mutex
Driver access mutex.
Definition udc.h:281
struct net_buf * setup
Pointer to buffer containing setup packet.
Definition udc.h:291
udc_event_cb_t event_cb
Callback to submit an UDC event to higher layer.
Definition udc.h:283
atomic_t status
USB device controller status.
Definition udc.h:287
struct udc_device_caps caps
Controller capabilities.
Definition udc.h:279
const void * event_ctx
Opaque pointer to store higher layer context.
Definition udc.h:285
int stage
Internal used Control Sequence Stage.
Definition udc.h:289
void * priv
Driver private data.
Definition udc.h:293
struct udc_ep_config * ep_lut[32]
LUT for endpoint management.
Definition udc.h:277
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:82
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 high_bandwidth
High-Bandwidth (interrupt or iso) capable endpoint.
Definition udc.h:80
uint32_t out
OUT transfer capable endpoint.
Definition udc.h:84
USB device controller endpoint configuration.
Definition udc.h:110
uint8_t interval
Polling interval.
Definition udc.h:124
struct udc_ep_caps caps
Endpoint capabilities.
Definition udc.h:114
struct k_fifo fifo
Endpoint requests FIFO.
Definition udc.h:112
uint8_t addr
Endpoint address.
Definition udc.h:118
uint8_t attributes
Endpoint attributes.
Definition udc.h:120
uint16_t mps
Maximum packet size.
Definition udc.h:122
USB device controller endpoint status.
Definition udc.h:90
uint32_t odd
If double buffering is supported, last used buffer is odd.
Definition udc.h:98
uint32_t data1
Last submitted PID is DATA1.
Definition udc.h:96
uint32_t enabled
Endpoint is enabled.
Definition udc.h:92
uint32_t busy
Endpoint is busy.
Definition udc.h:100
uint32_t halted
Endpoint is halted (returning STALL PID)
Definition udc.h:94
USB device controller event.
Definition udc.h:161
int status
Event status value, if any.
Definition udc.h:168
enum udc_event_type type
Event type.
Definition udc.h:163
const struct device * dev
Pointer to device struct.
Definition udc.h:173
struct net_buf * buf
Pointer to request used only for UDC_EVT_EP_REQUEST.
Definition udc.h:170
uint32_t value
Event value.
Definition udc.h:166
udc_event_type
USB device controller event types.
Definition udc.h:131
@ UDC_EVT_VBUS_READY
VBUS ready event.
Definition udc.h:133
@ UDC_EVT_RESUME
Device resume event.
Definition udc.h:137
@ UDC_EVT_SUSPEND
Device suspended event.
Definition udc.h:139
@ UDC_EVT_VBUS_REMOVED
VBUS removed event.
Definition udc.h:135
@ UDC_EVT_EP_REQUEST
Endpoint request result event.
Definition udc.h:145
@ UDC_EVT_ERROR
Non-correctable error event, requires attention from higher levels or application.
Definition udc.h:150
@ UDC_EVT_RESET
Port reset detected.
Definition udc.h:141
@ UDC_EVT_SOF
Start of Frame event.
Definition udc.h:143
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:260
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:217
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:267
#define UDC_STATUS_ENABLED
Controller is enabled and all API functions are available, controller is recognizable by host.
Definition udc.h:265
Buffers for USB device support.
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:318
#define USB_MPS_EP_SIZE(mps)
Get endpoint size field from Max Packet Size value.
Definition usb_ch9.h:357