1 /*
2 * Copyright (c) 2022-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 "hdf_log.h"
10 #include "osal_mem.h"
11 #include "watchdog/watchdog_core.h"
12
13 #define HDF_LOG_TAG watchdog_virtual
14
15 struct VirtualWatchdogCntlr {
16 struct WatchdogCntlr wdt;
17 int32_t seconds;
18 int32_t status;
19 };
20
21 enum VirtualWatchdogStatus {
22 WATCHDOG_STOP, /**< Stopped */
23 WATCHDOG_START, /**< Started */
24 };
25
VirtualWatchdogGetStatus(struct WatchdogCntlr * wdt,int32_t * status)26 static int32_t VirtualWatchdogGetStatus(struct WatchdogCntlr *wdt, int32_t *status)
27 {
28 struct VirtualWatchdogCntlr *virtual = NULL;
29
30 if (wdt == NULL) {
31 HDF_LOGE("VirtualWatchdogGetStatus: wdt is null!");
32 return HDF_ERR_INVALID_OBJECT;
33 }
34 virtual = (struct VirtualWatchdogCntlr *)wdt;
35
36 if (status == NULL) {
37 HDF_LOGE("VirtualWatchdogGetStatus: status is null!");
38 return HDF_ERR_INVALID_PARAM;
39 }
40 *status = virtual->status;
41
42 return HDF_SUCCESS;
43 }
44
VirtualWatchdogStart(struct WatchdogCntlr * wdt)45 static int32_t VirtualWatchdogStart(struct WatchdogCntlr *wdt)
46 {
47 struct VirtualWatchdogCntlr *virtual = NULL;
48
49 if (wdt == NULL) {
50 HDF_LOGE("VirtualWatchdogStart: wdt is null!");
51 return HDF_ERR_INVALID_OBJECT;
52 }
53 virtual = (struct VirtualWatchdogCntlr *)wdt;
54 virtual->status = WATCHDOG_START;
55
56 return HDF_SUCCESS;
57 }
58
VirtualWatchdogStop(struct WatchdogCntlr * wdt)59 static int32_t VirtualWatchdogStop(struct WatchdogCntlr *wdt)
60 {
61 struct VirtualWatchdogCntlr *virtual = NULL;
62
63 if (wdt == NULL) {
64 HDF_LOGE("VirtualWatchdogStop: wdt is null!");
65 return HDF_ERR_INVALID_OBJECT;
66 }
67 virtual = (struct VirtualWatchdogCntlr *)wdt;
68 virtual->status = WATCHDOG_STOP;
69
70 return HDF_SUCCESS;
71 }
72
VirtualWatchdogSetTimeout(struct WatchdogCntlr * wdt,uint32_t seconds)73 static int32_t VirtualWatchdogSetTimeout(struct WatchdogCntlr *wdt, uint32_t seconds)
74 {
75 struct VirtualWatchdogCntlr *virtual = NULL;
76
77 if (wdt == NULL) {
78 HDF_LOGE("VirtualWatchdogSetTimeout: wdt is null!");
79 return HDF_ERR_INVALID_OBJECT;
80 }
81 virtual = (struct VirtualWatchdogCntlr *)wdt;
82 virtual->seconds = seconds;
83
84 return HDF_SUCCESS;
85 }
86
VirtualWatchdogGetTimeout(struct WatchdogCntlr * wdt,uint32_t * seconds)87 static int32_t VirtualWatchdogGetTimeout(struct WatchdogCntlr *wdt, uint32_t *seconds)
88 {
89 struct VirtualWatchdogCntlr *virtual = NULL;
90
91 if (wdt == NULL || seconds == NULL) {
92 HDF_LOGE("VirtualWatchdogGetTimeout: wdt or seconds is null!");
93 return HDF_ERR_INVALID_OBJECT;
94 }
95 virtual = (struct VirtualWatchdogCntlr *)wdt;
96 *seconds = virtual->seconds;
97
98 return HDF_SUCCESS;
99 }
100
VirtualWatchdogFeed(struct WatchdogCntlr * wdt)101 static int32_t VirtualWatchdogFeed(struct WatchdogCntlr *wdt)
102 {
103 (void)wdt;
104 return HDF_SUCCESS;
105 }
106
107 static struct WatchdogMethod g_method = {
108 .getStatus = VirtualWatchdogGetStatus,
109 .start = VirtualWatchdogStart,
110 .stop = VirtualWatchdogStop,
111 .setTimeout = VirtualWatchdogSetTimeout,
112 .getTimeout = VirtualWatchdogGetTimeout,
113 .feed = VirtualWatchdogFeed,
114 };
115
VirtualWatchdogBind(struct HdfDeviceObject * device)116 static int32_t VirtualWatchdogBind(struct HdfDeviceObject *device)
117 {
118 int32_t ret;
119 struct WatchdogCntlr *wdt = NULL;
120
121 HDF_LOGI("VirtualWatchdogBind: enter!");
122 if (device == NULL || device->property == NULL) {
123 HDF_LOGE("VirtualWatchdogBind: device or property is null!");
124 return HDF_ERR_INVALID_OBJECT;
125 }
126
127 wdt = (struct WatchdogCntlr *)OsalMemCalloc(sizeof(*wdt));
128 if (wdt == NULL) {
129 HDF_LOGE("VirtualWatchdogBind: malloc wdt fail!");
130 return HDF_ERR_MALLOC_FAIL;
131 }
132
133 wdt->priv = (void *)device->property;
134 wdt->ops = &g_method;
135 wdt->device = device;
136 ret = WatchdogCntlrAdd(wdt);
137 if (ret != HDF_SUCCESS) {
138 HDF_LOGE("VirtualWatchdogBind: err add watchdog, ret: %d!", ret);
139 OsalMemFree(wdt);
140 return ret;
141 }
142 HDF_LOGI("VirtualWatchdogBind: dev service %s bind success!", HdfDeviceGetServiceName(device));
143 return HDF_SUCCESS;
144 }
145
VirtualWatchdogInit(struct HdfDeviceObject * device)146 static int32_t VirtualWatchdogInit(struct HdfDeviceObject *device)
147 {
148 HDF_LOGI("VirtualWatchdogInit: enter");
149 (void)device;
150 return HDF_SUCCESS;
151 }
152
VirtualWatchdogRelease(struct HdfDeviceObject * device)153 static void VirtualWatchdogRelease(struct HdfDeviceObject *device)
154 {
155 struct WatchdogCntlr *wdt = NULL;
156
157 HDF_LOGI("VirtualWatchdogRelease: enter!");
158 if (device == NULL) {
159 HDF_LOGE("VirtualWatchdogRelease: device is null!");
160 return;
161 }
162
163 wdt = WatchdogCntlrFromDevice(device);
164 if (wdt == NULL) {
165 HDF_LOGE("VirtualWatchdogRelease: wdt is null!");
166 return;
167 }
168 WatchdogCntlrRemove(wdt);
169 OsalMemFree(wdt);
170 }
171
172 struct HdfDriverEntry g_virtualWatchdogDriverEntry = {
173 .moduleVersion = 1,
174 .Bind = VirtualWatchdogBind,
175 .Init = VirtualWatchdogInit,
176 .Release = VirtualWatchdogRelease,
177 .moduleName = "virtual_watchdog_driver",
178 };
179 HDF_INIT(g_virtualWatchdogDriverEntry);
180