1 /* 2 * Copyright (c) 2020-2023 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #include "uart_core.h" 10 #include "hdf_log.h" 11 #include "osal_mem.h" 12 #include "uart_if.h" 13 14 #define HDF_LOG_TAG uart_core_c 15 UartHostRequest(struct UartHost * host)16int32_t UartHostRequest(struct UartHost *host) 17 { 18 int32_t ret; 19 20 if (host == NULL) { 21 HDF_LOGE("UartHostRequest: host is null!"); 22 return HDF_ERR_INVALID_PARAM; 23 } 24 25 if (host->method == NULL || host->method->Init == NULL) { 26 HDF_LOGE("UartHostRequest: method or init is null!"); 27 return HDF_ERR_NOT_SUPPORT; 28 } 29 30 if (OsalAtomicIncReturn(&host->atom) > 1) { 31 HDF_LOGE("UartHostRequest: uart device is busy!"); 32 OsalAtomicDec(&host->atom); 33 return HDF_ERR_DEVICE_BUSY; 34 } 35 36 ret = host->method->Init(host); 37 if (ret != HDF_SUCCESS) { 38 HDF_LOGE("UartHostRequest: host init fail!"); 39 OsalAtomicDec(&host->atom); 40 return ret; 41 } 42 43 return HDF_SUCCESS; 44 } 45 UartHostRelease(struct UartHost * host)46int32_t UartHostRelease(struct UartHost *host) 47 { 48 int32_t ret; 49 50 if (host == NULL) { 51 HDF_LOGE("UartHostRelease: host or method is null!"); 52 return HDF_ERR_INVALID_PARAM; 53 } 54 55 if (host->method == NULL || host->method->Deinit == NULL) { 56 HDF_LOGE("UartHostRelease: method or Deinit is null!"); 57 return HDF_ERR_NOT_SUPPORT; 58 } 59 60 ret = host->method->Deinit(host); 61 if (ret != HDF_SUCCESS) { 62 HDF_LOGE("UartHostRelease: host deinit fail!"); 63 return ret; 64 } 65 66 OsalAtomicDec(&host->atom); 67 return HDF_SUCCESS; 68 } 69 UartHostDestroy(struct UartHost * host)70void UartHostDestroy(struct UartHost *host) 71 { 72 if (host == NULL) { 73 HDF_LOGE("UartHostDestroy: host is null!"); 74 return; 75 } 76 OsalMemFree(host); 77 } 78 UartHostCreate(struct HdfDeviceObject * device)79struct UartHost *UartHostCreate(struct HdfDeviceObject *device) 80 { 81 struct UartHost *host = NULL; 82 83 if (device == NULL) { 84 HDF_LOGE("UartHostCreate: device is null!"); 85 return NULL; 86 } 87 88 host = (struct UartHost *)OsalMemCalloc(sizeof(*host)); 89 if (host == NULL) { 90 HDF_LOGE("UartHostCreate: memcalloc error!"); 91 return NULL; 92 } 93 94 host->device = device; 95 device->service = &(host->service); 96 host->device->service->Dispatch = UartIoDispatch; 97 OsalAtomicSet(&host->atom, 0); 98 host->priv = NULL; 99 host->method = NULL; 100 return host; 101 } 102