1 /* 2 * Copyright (c) 2022 Nordic Semiconductor ASA 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** 8 * @file 9 * @brief New experimental USB device stack APIs and structures 10 * 11 * This file contains the USB device stack APIs and structures. 12 */ 13 14 #ifndef ZEPHYR_INCLUDE_USBH_H_ 15 #define ZEPHYR_INCLUDE_USBH_H_ 16 17 #include <stdint.h> 18 #include <zephyr/device.h> 19 #include <zephyr/net_buf.h> 20 #include <zephyr/sys/dlist.h> 21 #include <zephyr/sys/bitarray.h> 22 #include <zephyr/drivers/usb/uhc.h> 23 #include <zephyr/sys/iterable_sections.h> 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 /** 30 * @brief USB HOST Core Layer API 31 * @defgroup usb_host_core_api USB Host Core API 32 * @ingroup usb 33 * @{ 34 */ 35 36 /** 37 * USB host support runtime context 38 */ 39 struct usbh_contex { 40 /** Name of the USB device */ 41 const char *name; 42 /** Access mutex */ 43 struct k_mutex mutex; 44 /** Pointer to UHC device struct */ 45 const struct device *dev; 46 /** USB device list */ 47 sys_dlist_t udevs; 48 /** USB root device */ 49 struct usb_device *root; 50 /** Allocated device addresses bit array */ 51 struct sys_bitarray *addr_ba; 52 }; 53 54 #define USBH_CONTROLLER_DEFINE(device_name, uhc_dev) \ 55 SYS_BITARRAY_DEFINE_STATIC(ba_##device_name, 128); \ 56 static STRUCT_SECTION_ITERABLE(usbh_contex, device_name) = { \ 57 .name = STRINGIFY(device_name), \ 58 .mutex = Z_MUTEX_INITIALIZER(device_name.mutex), \ 59 .dev = uhc_dev, \ 60 .addr_ba = &ba_##device_name, \ 61 } 62 63 /** 64 * @brief USB Class Code triple 65 */ 66 struct usbh_code_triple { 67 /** Device Class Code */ 68 uint8_t dclass; 69 /** Class Subclass Code */ 70 uint8_t sub; 71 /** Class Protocol Code */ 72 uint8_t proto; 73 }; 74 75 /** 76 * @brief USB host class data and class instance API 77 */ 78 struct usbh_class_data { 79 /** Class code supported by this instance */ 80 struct usbh_code_triple code; 81 82 /** Initialization of the class implementation */ 83 /* int (*init)(struct usbh_contex *const uhs_ctx); */ 84 /** Request completion event handler */ 85 int (*request)(struct usbh_contex *const uhs_ctx, 86 struct uhc_transfer *const xfer, int err); 87 /** Device connected handler */ 88 int (*connected)(struct usbh_contex *const uhs_ctx); 89 /** Device removed handler */ 90 int (*removed)(struct usbh_contex *const uhs_ctx); 91 /** Bus remote wakeup handler */ 92 int (*rwup)(struct usbh_contex *const uhs_ctx); 93 /** Bus suspended handler */ 94 int (*suspended)(struct usbh_contex *const uhs_ctx); 95 /** Bus resumed handler */ 96 int (*resumed)(struct usbh_contex *const uhs_ctx); 97 }; 98 99 /** 100 */ 101 #define USBH_DEFINE_CLASS(name) \ 102 static STRUCT_SECTION_ITERABLE(usbh_class_data, name) 103 104 105 /** 106 * @brief Initialize the USB host support; 107 * 108 * @param[in] uhs_ctx Pointer to USB host support context 109 * 110 * @return 0 on success, other values on fail. 111 */ 112 int usbh_init(struct usbh_contex *uhs_ctx); 113 114 /** 115 * @brief Enable the USB host support and class instances 116 * 117 * This function enables the USB host support. 118 * 119 * @param[in] uhs_ctx Pointer to USB host support context 120 * 121 * @return 0 on success, other values on fail. 122 */ 123 int usbh_enable(struct usbh_contex *uhs_ctx); 124 125 /** 126 * @brief Disable the USB host support 127 * 128 * This function disables the USB host support. 129 * 130 * @param[in] uhs_ctx Pointer to USB host support context 131 * 132 * @return 0 on success, other values on fail. 133 */ 134 int usbh_disable(struct usbh_contex *uhs_ctx); 135 136 /** 137 * @brief Shutdown the USB host support 138 * 139 * This function completely disables the USB host support. 140 * 141 * @param[in] uhs_ctx Pointer to USB host support context 142 * 143 * @return 0 on success, other values on fail. 144 */ 145 int usbh_shutdown(struct usbh_contex *const uhs_ctx); 146 147 /** 148 * @} 149 */ 150 151 #ifdef __cplusplus 152 } 153 #endif 154 155 #endif /* ZEPHYR_INCLUDE_USBH_H_ */ 156