Zephyr API Documentation 4.5.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
uhc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
11
12#ifndef ZEPHYR_INCLUDE_UHC_H
13#define ZEPHYR_INCLUDE_UHC_H
14
15#include <zephyr/kernel.h>
16#include <zephyr/device.h>
17#include <zephyr/net_buf.h>
19#include <zephyr/usb/usb_ch9.h>
20#include <zephyr/sys/dlist.h>
21#include <zephyr/sys/ref.h>
22
31
39
55
56#define UHC_INTERFACES_MAX 32
57
66
70};
71
109
118
166
195
204struct uhc_event {
209 union {
214 };
216 const struct device *dev;
217};
218
229typedef int (*uhc_event_cb_t)(const struct device *dev,
230 const struct uhc_event *const event);
231
240};
241
245#define UHC_STATUS_INITIALIZED 0
249#define UHC_STATUS_ENABLED 1
250
275
283static inline bool uhc_is_initialized(const struct device *dev)
284{
285 struct uhc_data *data = dev->data;
286
288}
289
297static inline bool uhc_is_enabled(const struct device *dev)
298{
299 struct uhc_data *data = dev->data;
300
302}
303
307__subsystem struct uhc_driver_api {
308 int (*lock)(const struct device *dev);
309 int (*unlock)(const struct device *dev);
310
311 int (*init)(const struct device *dev);
312 int (*enable)(const struct device *dev);
313 int (*disable)(const struct device *dev);
314 int (*shutdown)(const struct device *dev);
315
316 int (*bus_reset)(const struct device *dev);
317 int (*sof_enable)(const struct device *dev);
318 int (*bus_suspend)(const struct device *dev);
319 int (*bus_resume)(const struct device *dev);
320
321 int (*ep_enqueue)(const struct device *dev,
322 struct uhc_transfer *const xfer);
323 int (*ep_dequeue)(const struct device *dev,
324 struct uhc_transfer *const xfer);
325};
329
341static inline int uhc_bus_reset(const struct device *dev)
342{
343 const struct uhc_driver_api *api = DEVICE_API_GET(uhc, dev);
344 int ret;
345
346 api->lock(dev);
347 ret = api->bus_reset(dev);
348 api->unlock(dev);
349
350 return ret;
351}
352
363static inline int uhc_sof_enable(const struct device *dev)
364{
365 const struct uhc_driver_api *api = DEVICE_API_GET(uhc, dev);
366 int ret;
367
368 api->lock(dev);
369 ret = api->sof_enable(dev);
370 api->unlock(dev);
371
372 return ret;
373}
374
386static inline int uhc_bus_suspend(const struct device *dev)
387{
388 const struct uhc_driver_api *api = DEVICE_API_GET(uhc, dev);
389 int ret;
390
391 api->lock(dev);
392 ret = api->bus_suspend(dev);
393 api->unlock(dev);
394
395 return ret;
396}
397
409static inline int uhc_bus_resume(const struct device *dev)
410{
411 const struct uhc_driver_api *api = DEVICE_API_GET(uhc, dev);
412 int ret;
413
414 api->lock(dev);
415 ret = api->bus_resume(dev);
416 api->unlock(dev);
417
418 return ret;
419}
420
436struct uhc_transfer *uhc_xfer_alloc(const struct device *dev,
437 const uint8_t ep,
438 struct usb_device *const udev,
439 void *const cb,
440 void *const cb_priv);
441
457 const uint8_t ep,
458 struct usb_device *const udev,
459 void *const cb,
460 void *const cb_priv,
461 size_t size);
462
468void uhc_xfer_ref(struct uhc_transfer *const xfer);
469
480bool uhc_xfer_unref(struct uhc_transfer *const xfer);
481
492int uhc_xfer_free(const struct device *dev,
493 struct uhc_transfer *const xfer);
494
506int uhc_xfer_buf_add(const struct device *dev,
507 struct uhc_transfer *const xfer,
508 struct net_buf *buf);
520struct net_buf *uhc_xfer_buf_alloc(const struct device *dev,
521 const size_t size);
522
531void uhc_xfer_buf_free(const struct device *dev, struct net_buf *const buf);
532
545int uhc_ep_enqueue(const struct device *dev, struct uhc_transfer *const xfer);
546
558int uhc_ep_dequeue(const struct device *dev, struct uhc_transfer *const xfer);
559
573int uhc_init(const struct device *dev,
574 uhc_event_cb_t event_cb, const void *const event_ctx);
575
588int uhc_enable(const struct device *dev);
589
600int uhc_disable(const struct device *dev);
601
613int uhc_shutdown(const struct device *dev);
614
625static inline struct uhc_device_caps uhc_caps(const struct device *dev)
626{
627 struct uhc_data *data = dev->data;
628
629 return data->caps;
630}
631
642static inline const void *uhc_get_event_ctx(const struct device *dev)
643{
644 struct uhc_data *data = dev->data;
645
646 return data->event_ctx;
647}
648
652
653#endif /* ZEPHYR_INCLUDE_UHC_H */
APIs and macros for the Zephyr device model.
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1486
Header file for the doubly-linked list API.
long atomic_t
Atomic integer variable.
Definition atomic_types.h:34
static _Bool atomic_test_bit(const atomic_t *target, int bit)
Atomically get and test a bit.
Definition atomic.h:138
struct _dnode sys_dnode_t
Doubly-linked list node structure.
Definition dlist.h:59
struct _dnode sys_dlist_t
Doubly-linked list structure.
Definition dlist.h:55
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:44
#define UHC_STATUS_INITIALIZED
Controller is initialized by uhc_init().
Definition uhc.h:245
static bool uhc_is_enabled(const struct device *dev)
Checks whether the controller is enabled.
Definition uhc.h:297
#define UHC_INTERFACES_MAX
Definition uhc.h:56
#define UHC_STATUS_ENABLED
Controller is enabled and all API functions are available.
Definition uhc.h:249
static bool uhc_is_initialized(const struct device *dev)
Checks whether the controller is initialized.
Definition uhc.h:283
int(* uhc_event_cb_t)(const struct device *dev, const struct uhc_event *const event)
Callback to submit UHC event to higher layer.
Definition uhc.h:229
static const void * uhc_get_event_ctx(const struct device *dev)
Get pointer to higher layer context.
Definition uhc.h:642
static int uhc_bus_reset(const struct device *dev)
Reset USB bus.
Definition uhc.h:341
usb_device_speed
USB device operating speed.
Definition uhc.h:43
static int uhc_bus_resume(const struct device *dev)
Resume USB bus.
Definition uhc.h:409
void uhc_xfer_ref(struct uhc_transfer *const xfer)
Increment the UHC transfer reference.
usb_device_state
USB device state.
Definition uhc.h:33
struct uhc_transfer * uhc_xfer_alloc_with_buf(const struct device *dev, const uint8_t ep, struct usb_device *const udev, void *const cb, void *const cb_priv, size_t size)
Allocate UHC transfer with buffer.
int uhc_xfer_buf_add(const struct device *dev, struct uhc_transfer *const xfer, struct net_buf *buf)
Add UHC transfer buffer.
int uhc_ep_enqueue(const struct device *dev, struct uhc_transfer *const xfer)
Queue USB host controller transfer.
int uhc_ep_dequeue(const struct device *dev, struct uhc_transfer *const xfer)
Remove a USB host controller transfers from queue.
static struct uhc_device_caps uhc_caps(const struct device *dev)
Get USB host controller capabilities.
Definition uhc.h:625
static int uhc_bus_suspend(const struct device *dev)
Suspend USB bus.
Definition uhc.h:386
int uhc_init(const struct device *dev, uhc_event_cb_t event_cb, const void *const event_ctx)
Initialize USB host controller.
int uhc_disable(const struct device *dev)
Disable USB host controller.
int uhc_xfer_free(const struct device *dev, struct uhc_transfer *const xfer)
Free UHC transfer and any buffers.
bool uhc_xfer_unref(struct uhc_transfer *const xfer)
Decrement the UHC transfer reference.
struct net_buf * uhc_xfer_buf_alloc(const struct device *dev, const size_t size)
Allocate UHC transfer buffer.
struct uhc_transfer * uhc_xfer_alloc(const struct device *dev, const uint8_t ep, struct usb_device *const udev, void *const cb, void *const cb_priv)
Allocate UHC transfer.
uhc_event_type
USB host controller event types.
Definition uhc.h:170
void uhc_xfer_buf_free(const struct device *dev, struct net_buf *const buf)
Free UHC request buffer.
int uhc_shutdown(const struct device *dev)
Poweroff USB host controller.
uhc_control_stage
USB control transfer stage.
Definition uhc.h:113
static int uhc_sof_enable(const struct device *dev)
Enable Start of Frame generator.
Definition uhc.h:363
int uhc_enable(const struct device *dev)
Enable USB host controller.
@ USB_SPEED_SPEED_SS
Super speed.
Definition uhc.h:53
@ USB_SPEED_SPEED_LS
Low speed.
Definition uhc.h:47
@ USB_SPEED_SPEED_FS
Full speed.
Definition uhc.h:49
@ USB_SPEED_SPEED_HS
High speed.
Definition uhc.h:51
@ USB_SPEED_UNKNOWN
Device is probably not connected.
Definition uhc.h:45
@ USB_STATE_CONFIGURED
Definition uhc.h:37
@ USB_STATE_NOTCONNECTED
Definition uhc.h:34
@ USB_STATE_DEFAULT
Definition uhc.h:35
@ USB_STATE_ADDRESSED
Definition uhc.h:36
@ UHC_EVT_EP_REQUEST
Endpoint request result event.
Definition uhc.h:188
@ UHC_EVT_DEV_CONNECTED_HS
High speed device connected.
Definition uhc.h:176
@ UHC_EVT_DEV_CONNECTED_FS
Full speed device connected.
Definition uhc.h:174
@ UHC_EVT_RWUP
Remote wakeup signal.
Definition uhc.h:186
@ UHC_EVT_SUSPENDED
Bus suspend operation finished.
Definition uhc.h:182
@ UHC_EVT_DEV_REMOVED
Device (peripheral) removed.
Definition uhc.h:178
@ UHC_EVT_RESUMED
Bus resume operation finished.
Definition uhc.h:184
@ UHC_EVT_DEV_CONNECTED_LS
Low speed device connected.
Definition uhc.h:172
@ UHC_EVT_ERROR
Non-correctable error event, requires attention from higher levels or application.
Definition uhc.h:193
@ UHC_EVT_RESETED
Bus reset operation finished.
Definition uhc.h:180
@ UHC_CONTROL_STAGE_DATA
Definition uhc.h:115
@ UHC_CONTROL_STAGE_SETUP
Definition uhc.h:114
@ UHC_CONTROL_STAGE_STATUS
Definition uhc.h:116
#define USB_BUF_ROUND_UP(size)
Round up to the granularity required by the UDC driver.
Definition usb_buf.h:73
#define USB_BUF_ALIGN
Buffer alignment required by the UDC driver.
Definition usb_buf.h:67
Public kernel APIs.
Buffer management.
int shutdown(int sock, int how)
Header file for reference counting.
__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:543
void * data
Address of the device instance private data.
Definition device.h:553
Kernel mutex structure.
Definition kernel.h:3500
Network buffer representation.
Definition net_buf.h:1054
uint16_t size
Amount of data that this buffer can store.
Definition net_buf.h:1149
Reference of an object.
Definition ref.h:147
Common UHC driver data structure.
Definition uhc.h:257
struct k_mutex mutex
Driver access mutex.
Definition uhc.h:261
atomic_t status
USB host controller status.
Definition uhc.h:271
void * priv
Driver private data.
Definition uhc.h:273
sys_dlist_t ctrl_xfers
dlist for control transfers
Definition uhc.h:263
const void * event_ctx
Opaque pointer to store higher layer context.
Definition uhc.h:269
struct uhc_device_caps caps
Controller capabilities.
Definition uhc.h:259
sys_dlist_t bulk_xfers
dlist for bulk transfers
Definition uhc.h:265
uhc_event_cb_t event_cb
Callback to submit an UHC event to upper layer.
Definition uhc.h:267
USB host controller capabilities.
Definition uhc.h:237
uint32_t hs
USB high speed capable controller.
Definition uhc.h:239
USB host controller event.
Definition uhc.h:204
struct uhc_transfer * xfer
Pointer to request used only for UHC_EVT_EP_REQUEST.
Definition uhc.h:213
const struct device * dev
Pointer to controller's device struct.
Definition uhc.h:216
enum uhc_event_type type
Event type.
Definition uhc.h:208
int status
Event status value, if any.
Definition uhc.h:211
sys_snode_t node
slist node for the message queue
Definition uhc.h:206
UHC endpoint buffer info.
Definition uhc.h:129
void * priv
Pointer to completion callback private data.
Definition uhc.h:162
struct sys_ref ref
Reference count.
Definition uhc.h:133
int err
Transfer result, 0 on success, other values on error.
Definition uhc.h:164
struct net_buf * buf
Transfer data buffer.
Definition uhc.h:137
uint16_t start_frame
Start frame, used for periodic transfers only.
Definition uhc.h:147
unsigned int no_status
The status stage of the control transfer will be omitted.
Definition uhc.h:156
uint16_t mps
Maximum packet size.
Definition uhc.h:143
struct usb_device * udev
Pointer to USB device.
Definition uhc.h:158
unsigned int stage
Control stage status, up to the driver to use it or not.
Definition uhc.h:151
uint8_t ep
Endpoint to which request is associated.
Definition uhc.h:139
unsigned int queued
Flag marks request buffer is queued.
Definition uhc.h:149
void * cb
Pointer to transfer completion callback (opaque for the UHC).
Definition uhc.h:160
uint8_t type
Endpoint type.
Definition uhc.h:141
sys_dnode_t node
dlist node
Definition uhc.h:131
uint8_t setup_pkt[ROUND_UP(8, USB_PRIV_BUFGRANULARITY)]
Control transfer setup packet.
Definition uhc.h:135
uint16_t interval
Interval, used for periodic transfers only.
Definition uhc.h:145
USB Association Descriptor defined in USB 3 spec.
Definition usb_ch9.h:465
Header of a USB descriptor.
Definition usb_ch9.h:284
USB Standard Device Descriptor.
Definition usb_ch9.h:304
Host representation of a USB device.
Definition uhc.h:75
sys_dnode_t node
dlist node
Definition uhc.h:77
uint8_t actual_cfg
Actual active device configuration.
Definition uhc.h:89
enum usb_device_speed speed
Device speed.
Definition uhc.h:87
struct usb_device_descriptor dev_desc
USB device descriptor.
Definition uhc.h:83
struct k_mutex mutex
Device mutex.
Definition uhc.h:81
struct usb_device * hub
Pointer to the hub to which this device is connected.
Definition uhc.h:101
uint8_t hub_port
Device's hub port.
Definition uhc.h:105
struct usb_host_interface ifaces[32+1]
Pointers to device interfaces.
Definition uhc.h:95
struct usb_host_ep ep_out[16]
Pointers to device OUT endpoints.
Definition uhc.h:97
void * ctx
An opaque pointer to the host context to which this device belongs.
Definition uhc.h:79
enum usb_device_state state
Device state.
Definition uhc.h:85
uint8_t addr
Device address.
Definition uhc.h:91
void * cfg_desc
Pointer to actual device configuration descriptor.
Definition uhc.h:93
struct usb_host_ep ep_in[16]
Pointers to device IN endpoints.
Definition uhc.h:99
uint8_t level
Device's level (root device = 1).
Definition uhc.h:107
uint16_t tt
Device's hub Think Time.
Definition uhc.h:103
USB Standard Endpoint Descriptor.
Definition usb_ch9.h:431
Definition uhc.h:67
struct usb_ep_descriptor * desc
Pointer to the endpoint descriptor.
Definition uhc.h:69
Definition uhc.h:58
uint8_t alternate
Alternate setting selected for this interface.
Definition uhc.h:64
struct usb_association_descriptor * iad
Pointer to the association interface descriptor, if any.
Definition uhc.h:62
struct usb_desc_header * dhp
Pointer to the interface descriptor.
Definition uhc.h:60
Buffers for USB device support.
USB Chapter 9 structures and definitions.