1 /* 2 * Copyright (c) 2022 Nordic Semiconductor ASA 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #include <errno.h> 8 #include <zephyr/sys/util.h> 9 #include "usbh_internal.h" 10 11 #include <zephyr/logging/log.h> 12 LOG_MODULE_REGISTER(uhs_api, CONFIG_USBH_LOG_LEVEL); 13 usbh_init(struct usbh_contex * uhs_ctx)14int usbh_init(struct usbh_contex *uhs_ctx) 15 { 16 int ret; 17 18 k_mutex_lock(&uhs_ctx->mutex, K_FOREVER); 19 20 if (!device_is_ready(uhs_ctx->dev)) { 21 LOG_ERR("USB host controller is not ready"); 22 ret = -ENODEV; 23 goto init_exit; 24 } 25 26 if (uhc_is_initialized(uhs_ctx->dev)) { 27 LOG_WRN("USB host controller is already initialized"); 28 ret = -EALREADY; 29 goto init_exit; 30 } 31 32 ret = usbh_init_device_intl(uhs_ctx); 33 34 init_exit: 35 k_mutex_unlock(&uhs_ctx->mutex); 36 return ret; 37 } 38 usbh_enable(struct usbh_contex * uhs_ctx)39int usbh_enable(struct usbh_contex *uhs_ctx) 40 { 41 int ret; 42 43 k_mutex_lock(&uhs_ctx->mutex, K_FOREVER); 44 45 if (!uhc_is_initialized(uhs_ctx->dev)) { 46 LOG_WRN("USB host controller is not initialized"); 47 ret = -EPERM; 48 goto enable_exit; 49 } 50 51 if (uhc_is_enabled(uhs_ctx->dev)) { 52 LOG_WRN("USB host controller is already enabled"); 53 ret = -EALREADY; 54 goto enable_exit; 55 } 56 57 ret = uhc_enable(uhs_ctx->dev); 58 if (ret != 0) { 59 LOG_ERR("Failed to enable controller"); 60 goto enable_exit; 61 } 62 63 enable_exit: 64 k_mutex_unlock(&uhs_ctx->mutex); 65 return ret; 66 } 67 usbh_disable(struct usbh_contex * uhs_ctx)68int usbh_disable(struct usbh_contex *uhs_ctx) 69 { 70 int ret; 71 72 if (!uhc_is_enabled(uhs_ctx->dev)) { 73 LOG_WRN("USB host controller is already disabled"); 74 return 0; 75 } 76 77 k_mutex_lock(&uhs_ctx->mutex, K_FOREVER); 78 79 ret = uhc_disable(uhs_ctx->dev); 80 if (ret) { 81 LOG_ERR("Failed to disable USB controller"); 82 } 83 84 k_mutex_unlock(&uhs_ctx->mutex); 85 86 return 0; 87 } 88 usbh_shutdown(struct usbh_contex * const uhs_ctx)89int usbh_shutdown(struct usbh_contex *const uhs_ctx) 90 { 91 int ret; 92 93 k_mutex_lock(&uhs_ctx->mutex, K_FOREVER); 94 95 ret = uhc_shutdown(uhs_ctx->dev); 96 if (ret) { 97 LOG_ERR("Failed to shutdown USB device"); 98 } 99 100 k_mutex_unlock(&uhs_ctx->mutex); 101 102 return ret; 103 } 104