|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * @file |
| 9 | + * @brief USB host stack class instances API |
| 10 | + * |
| 11 | + * This file contains the USB host stack class instances API. |
| 12 | + */ |
| 13 | + |
| 14 | +#ifndef ZEPHYR_INCLUDE_USBH_CLASS_API_H |
| 15 | +#define ZEPHYR_INCLUDE_USBH_CLASS_API_H |
| 16 | + |
| 17 | +#include <zephyr/usb/usbh.h> |
| 18 | + |
| 19 | +/** |
| 20 | + * @brief Initialization of the class implementation |
| 21 | + * |
| 22 | + * This is called for each instance during the initialization phase, |
| 23 | + * for every registered class. |
| 24 | + * It can be used to initialize underlying systems. |
| 25 | + * |
| 26 | + * @param[in] c_data Pointer to USB host class data |
| 27 | + * @param[in] uhs_ctx USB host context to assign to this class |
| 28 | + * @return 0 on success, negative error code on failure. |
| 29 | + */ |
| 30 | +static inline int usbh_class_init(struct usbh_class_data *const c_data, |
| 31 | + struct usbh_context *const uhs_ctx) |
| 32 | +{ |
| 33 | + const struct usbh_class_api *api = c_data->api; |
| 34 | + |
| 35 | + if (api->init != NULL) { |
| 36 | + return api->init(c_data, uhs_ctx); |
| 37 | + } |
| 38 | + |
| 39 | + return -ENOTSUP; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * @brief Request completion event handler |
| 44 | + * |
| 45 | + * Called upon completion of a request made by the host to this class. |
| 46 | + * |
| 47 | + * @param[in] c_data Pointer to USB host class data |
| 48 | + * @param[in] xfer Completed transfer |
| 49 | + * @return 0 on success, negative error code on failure. |
| 50 | + */ |
| 51 | +static inline int usbh_class_completion_cb(struct usbh_class_data *const c_data, |
| 52 | + struct uhc_transfer *const xfer) |
| 53 | +{ |
| 54 | + const struct usbh_class_api *api = c_data->api; |
| 55 | + |
| 56 | + if (api->completion_cb != NULL) { |
| 57 | + return api->completion_cb(c_data, xfer); |
| 58 | + } |
| 59 | + |
| 60 | + return -ENOTSUP; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * @brief Device initialization handler |
| 65 | + * |
| 66 | + * Called when a device is connected to the bus for every device. |
| 67 | + * |
| 68 | + * @param[in] c_data Pointer to USB host class data |
| 69 | + * @param[in] udev USB device connected |
| 70 | + * @param[in] iface The @c bInterfaceNumber or @c bFirstInterface of this class |
| 71 | + * @return 0 on success, negative error code on failure. |
| 72 | + * @return -ENOTSUP if the class is not matching |
| 73 | + */ |
| 74 | +static inline int usbh_class_probe(struct usbh_class_data *const c_data, |
| 75 | + struct usb_device *const udev, |
| 76 | + const uint8_t iface) |
| 77 | +{ |
| 78 | + const struct usbh_class_api *api = c_data->api; |
| 79 | + |
| 80 | + if (api->probe != NULL) { |
| 81 | + return api->probe(c_data, udev, iface); |
| 82 | + } |
| 83 | + |
| 84 | + return -ENOTSUP; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * @brief Device removed handler |
| 89 | + * |
| 90 | + * Called when the device is removed from the bus |
| 91 | + * and it matches the class filters of this instance. |
| 92 | + * |
| 93 | + * @param[in] c_data Pointer to USB host class data |
| 94 | + * @return 0 on success, negative error code on failure. |
| 95 | + */ |
| 96 | +static inline int usbh_class_removed(struct usbh_class_data *const c_data) |
| 97 | +{ |
| 98 | + const struct usbh_class_api *api = c_data->api; |
| 99 | + |
| 100 | + if (api->removed != NULL) { |
| 101 | + return api->removed(c_data); |
| 102 | + } |
| 103 | + |
| 104 | + return -ENOTSUP; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * @brief Bus suspended handler |
| 109 | + * |
| 110 | + * Called when the host has suspended the bus. |
| 111 | + * It can be used to suspend underlying systems. |
| 112 | + * |
| 113 | + * @param[in] c_data Pointer to USB host class data |
| 114 | + * @return 0 on success, negative error code on failure. |
| 115 | + */ |
| 116 | +static inline int usbh_class_suspended(struct usbh_class_data *const c_data) |
| 117 | +{ |
| 118 | + const struct usbh_class_api *api = c_data->api; |
| 119 | + |
| 120 | + if (api->suspended != NULL) { |
| 121 | + return api->suspended(c_data); |
| 122 | + } |
| 123 | + |
| 124 | + return -ENOTSUP; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * @brief Bus resumed handler |
| 129 | + * |
| 130 | + * Called when the host resumes its activity on the bus. |
| 131 | + * It can be used to wake-up underlying systems. |
| 132 | + * |
| 133 | + * @param[in] c_data Pointer to USB host class data |
| 134 | + * @return 0 on success, negative error code on failure. |
| 135 | + */ |
| 136 | +static inline int usbh_class_resumed(struct usbh_class_data *const c_data) |
| 137 | +{ |
| 138 | + const struct usbh_class_api *api = c_data->api; |
| 139 | + |
| 140 | + if (api->resumed != NULL) { |
| 141 | + return api->resumed(c_data); |
| 142 | + } |
| 143 | + |
| 144 | + return -ENOTSUP; |
| 145 | +} |
| 146 | + |
| 147 | +#endif /* ZEPHYR_INCLUDE_USBD_CLASS_API_H */ |
0 commit comments