1 /*
2 * Copyright (c) 2020-2021 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)16 int32_t UartHostRequest(struct UartHost *host)
17 {
18 int32_t ret;
19
20 if (host == NULL) {
21 HDF_LOGE("%s: host is NULL", __func__);
22 return HDF_ERR_INVALID_PARAM;
23 }
24
25 if (host->method == NULL || host->method->Init == NULL) {
26 HDF_LOGE("%s: method or init is NULL", __func__);
27 return HDF_ERR_NOT_SUPPORT;
28 }
29
30 if (OsalAtomicIncReturn(&host->atom) > 1) {
31 HDF_LOGE("%s: uart device is busy", __func__);
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("%s: host init fail", __func__);
39 OsalAtomicDec(&host->atom);
40 return ret;
41 }
42
43 return HDF_SUCCESS;
44 }
45
UartHostRelease(struct UartHost * host)46 int32_t UartHostRelease(struct UartHost *host)
47 {
48 int32_t ret;
49
50 if (host == NULL) {
51 HDF_LOGE("%s: host or method is NULL", __func__);
52 return HDF_ERR_INVALID_PARAM;
53 }
54
55 if (host->method == NULL || host->method->Deinit == NULL) {
56 HDF_LOGE("%s: method or Deinit is NULL", __func__);
57 return HDF_ERR_NOT_SUPPORT;
58 }
59
60 ret = host->method->Deinit(host);
61 if (ret != HDF_SUCCESS) {
62 HDF_LOGE("%s: host deinit fail", __func__);
63 return ret;
64 }
65
66 OsalAtomicDec(&host->atom);
67 return HDF_SUCCESS;
68 }
69
UartHostDestroy(struct UartHost * host)70 void UartHostDestroy(struct UartHost *host)
71 {
72 if (host == NULL) {
73 return;
74 }
75 OsalMemFree(host);
76 }
77
UartHostCreate(struct HdfDeviceObject * device)78 struct UartHost *UartHostCreate(struct HdfDeviceObject *device)
79 {
80 struct UartHost *host = NULL;
81
82 if (device == NULL) {
83 HDF_LOGE("%s: invalid parameter", __func__);
84 return NULL;
85 }
86
87 host = (struct UartHost *)OsalMemCalloc(sizeof(*host));
88 if (host == NULL) {
89 HDF_LOGE("%s: OsalMemCalloc error", __func__);
90 return NULL;
91 }
92
93 host->device = device;
94 device->service = &(host->service);
95 host->device->service->Dispatch = UartIoDispatch;
96 OsalAtomicSet(&host->atom, 0);
97 host->priv = NULL;
98 host->method = NULL;
99 return host;
100 }
101