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 "timer_core.h"
10 #include "hdf_device_object.h"
11 #include "hdf_log.h"
12 #include "osal_mem.h"
13 #include "osal_time.h"
14 #include "platform_listener_common.h"
15 #include "platform_trace.h"
16 #include "securec.h"
17
18 #define HDF_LOG_TAG timer_core
19
20 struct TimerManager {
21 struct IDeviceIoService service;
22 struct HdfDeviceObject *device;
23 struct DListHead timerListHead;
24 struct OsalMutex lock;
25 };
26
27 static struct TimerManager *g_timerManager = NULL;
28 #define TIMER_HANDLE_SHIFT ((uintptr_t)(-1) << 16)
29 #define TIMER_TRACE_PARAM_NUM 5
30 #define TIMER_TRACE_BASIC_PARAM_NUM 3
31 #define TIMER_TRACE_PARAM_STOP_NUM 3
32
TimerCntrlOpen(const uint32_t number)33 struct TimerCntrl *TimerCntrlOpen(const uint32_t number)
34 {
35 struct TimerCntrl *pos = NULL;
36 struct TimerManager *manager = g_timerManager;
37 CHECK_NULL_PTR_RETURN_VALUE(manager, NULL);
38
39 if (OsalMutexLock(&manager->lock) != HDF_SUCCESS) {
40 HDF_LOGE("TimerCntrlOpen: OsalMutexLock fail!");
41 return NULL;
42 }
43
44 DLIST_FOR_EACH_ENTRY(pos, &manager->timerListHead, struct TimerCntrl, node) {
45 if (number == pos->info.number) {
46 (void)OsalMutexUnlock(&manager->lock);
47 return pos;
48 }
49 }
50
51 (void)OsalMutexUnlock(&manager->lock);
52 HDF_LOGE("TimerCntrlOpen: open %u fail!", number);
53 return NULL;
54 }
55
TimerCntrlClose(struct TimerCntrl * cntrl)56 int32_t TimerCntrlClose(struct TimerCntrl *cntrl)
57 {
58 CHECK_NULL_PTR_RETURN_VALUE(cntrl, HDF_ERR_INVALID_OBJECT);
59 if (OsalMutexLock(&cntrl->lock) != HDF_SUCCESS) {
60 HDF_LOGE("TimerCntrlClose: OsalMutexLock %u fail!", cntrl->info.number);
61 return HDF_ERR_DEVICE_BUSY;
62 }
63 if ((cntrl->ops->Close != NULL) && (cntrl->ops->Close(cntrl) != HDF_SUCCESS)) {
64 HDF_LOGE("TimerCntrlClose: close %u fail!", cntrl->info.number);
65 (void)OsalMutexUnlock(&cntrl->lock);
66 return HDF_FAILURE;
67 }
68 (void)OsalMutexUnlock(&cntrl->lock);
69 return HDF_SUCCESS;
70 }
71
TimerCntrlSet(struct TimerCntrl * cntrl,uint32_t useconds,TimerHandleCb cb)72 int32_t TimerCntrlSet(struct TimerCntrl *cntrl, uint32_t useconds, TimerHandleCb cb)
73 {
74 CHECK_NULL_PTR_RETURN_VALUE(cntrl, HDF_ERR_INVALID_OBJECT);
75 CHECK_NULL_PTR_RETURN_VALUE(cb, HDF_ERR_INVALID_OBJECT);
76 if (OsalMutexLock(&cntrl->lock) != HDF_SUCCESS) {
77 HDF_LOGE("TimerCntrlSet: OsalMutexLock %u fail!", cntrl->info.number);
78 return HDF_ERR_DEVICE_BUSY;
79 }
80 if ((cntrl->ops->Set != NULL) && (cntrl->ops->Set(cntrl, useconds, cb) != HDF_SUCCESS)) {
81 HDF_LOGE("TimerCntrlSet: set %u fail!", cntrl->info.number);
82 (void)OsalMutexUnlock(&cntrl->lock);
83 return HDF_FAILURE;
84 }
85
86 (void)OsalMutexUnlock(&cntrl->lock);
87 return HDF_SUCCESS;
88 }
89
TimerCntrlSetOnce(struct TimerCntrl * cntrl,uint32_t useconds,TimerHandleCb cb)90 int32_t TimerCntrlSetOnce(struct TimerCntrl *cntrl, uint32_t useconds, TimerHandleCb cb)
91 {
92 CHECK_NULL_PTR_RETURN_VALUE(cntrl, HDF_ERR_INVALID_OBJECT);
93 CHECK_NULL_PTR_RETURN_VALUE(cb, HDF_ERR_INVALID_OBJECT);
94
95 if (OsalMutexLock(&cntrl->lock) != HDF_SUCCESS) {
96 HDF_LOGE("TimerCntrlSetOnce: OsalMutexLock %u fail!", cntrl->info.number);
97 return HDF_ERR_DEVICE_BUSY;
98 }
99 if ((cntrl->ops->SetOnce != NULL) && (cntrl->ops->SetOnce(cntrl, useconds, cb) != HDF_SUCCESS)) {
100 HDF_LOGE("TimerCntrlSetOnce: setOnce %u fail!", cntrl->info.number);
101 (void)OsalMutexUnlock(&cntrl->lock);
102 return HDF_FAILURE;
103 }
104
105 (void)OsalMutexUnlock(&cntrl->lock);
106 return HDF_SUCCESS;
107 }
108
TimerCntrlGet(struct TimerCntrl * cntrl,uint32_t * useconds,bool * isPeriod)109 int32_t TimerCntrlGet(struct TimerCntrl *cntrl, uint32_t *useconds, bool *isPeriod)
110 {
111 CHECK_NULL_PTR_RETURN_VALUE(cntrl, HDF_ERR_INVALID_OBJECT);
112 CHECK_NULL_PTR_RETURN_VALUE(useconds, HDF_ERR_INVALID_OBJECT);
113 CHECK_NULL_PTR_RETURN_VALUE(isPeriod, HDF_ERR_INVALID_OBJECT);
114
115 if (OsalMutexLock(&cntrl->lock) != HDF_SUCCESS) {
116 HDF_LOGE("TimerCntrlGet: OsalMutexLock %u fail!", cntrl->info.number);
117 return HDF_ERR_DEVICE_BUSY;
118 }
119 *useconds = cntrl->info.useconds;
120 *isPeriod = cntrl->info.isPeriod;
121 (void)OsalMutexUnlock(&cntrl->lock);
122 return HDF_SUCCESS;
123 }
124
TimerCntrlStart(struct TimerCntrl * cntrl)125 int32_t TimerCntrlStart(struct TimerCntrl *cntrl)
126 {
127 CHECK_NULL_PTR_RETURN_VALUE(cntrl, HDF_ERR_INVALID_OBJECT);
128
129 if (OsalMutexLock(&cntrl->lock) != HDF_SUCCESS) {
130 HDF_LOGE("TimerCntrlStart: OsalMutexLock %u fail!", cntrl->info.number);
131 return HDF_ERR_DEVICE_BUSY;
132 }
133 if ((cntrl->ops->Start != NULL) && (cntrl->ops->Start(cntrl) != HDF_SUCCESS)) {
134 HDF_LOGE("TimerCntrlStart: start %u fail!", cntrl->info.number);
135 (void)OsalMutexUnlock(&cntrl->lock);
136 return HDF_FAILURE;
137 }
138
139 int32_t ret = PlatformTraceStart();
140 if (ret == HDF_SUCCESS) {
141 unsigned int infos[TIMER_TRACE_BASIC_PARAM_NUM];
142 infos[PLATFORM_TRACE_UINT_PARAM_SIZE_1 - 1] = cntrl->info.number;
143 infos[PLATFORM_TRACE_UINT_PARAM_SIZE_2 - 1] = cntrl->info.useconds;
144 infos[PLATFORM_TRACE_UINT_PARAM_SIZE_3 - 1] = cntrl->info.isPeriod;
145 PlatformTraceAddUintMsg(
146 PLATFORM_TRACE_MODULE_TIMER, PLATFORM_TRACE_MODULE_TIMER_FUN_START, infos, TIMER_TRACE_BASIC_PARAM_NUM);
147 PlatformTraceStop();
148 PlatformTraceInfoDump();
149 }
150
151 (void)OsalMutexUnlock(&cntrl->lock);
152 return HDF_SUCCESS;
153 }
154
TimerCntrlStop(struct TimerCntrl * cntrl)155 int32_t TimerCntrlStop(struct TimerCntrl *cntrl)
156 {
157 CHECK_NULL_PTR_RETURN_VALUE(cntrl, HDF_ERR_INVALID_OBJECT);
158
159 if (OsalMutexLock(&cntrl->lock) != HDF_SUCCESS) {
160 HDF_LOGE("TimerCntrlStop: OsalMutexLock %u fail!", cntrl->info.number);
161 return HDF_ERR_DEVICE_BUSY;
162 }
163 if ((cntrl->ops->Stop != NULL) && (cntrl->ops->Stop(cntrl) != HDF_SUCCESS)) {
164 HDF_LOGE("TimerCntrlStop: stop %u fail!", cntrl->info.number);
165 (void)OsalMutexUnlock(&cntrl->lock);
166 return HDF_FAILURE;
167 }
168
169 int32_t ret = PlatformTraceStart();
170 if (ret == HDF_SUCCESS) {
171 unsigned int infos[TIMER_TRACE_PARAM_STOP_NUM];
172 infos[0] = cntrl->info.number;
173 PlatformTraceAddUintMsg(
174 PLATFORM_TRACE_MODULE_TIMER, PLATFORM_TRACE_MODULE_TIMER_FUN_STOP, infos, TIMER_TRACE_PARAM_STOP_NUM);
175 PlatformTraceStop();
176 PlatformTraceInfoDump();
177 }
178 (void)OsalMutexUnlock(&cntrl->lock);
179 return HDF_SUCCESS;
180 }
181
TimerIoOpen(struct HdfSBuf * data,struct HdfSBuf * reply)182 static int32_t TimerIoOpen(struct HdfSBuf *data, struct HdfSBuf *reply)
183 {
184 int16_t number;
185 uint32_t handle;
186
187 if ((data == NULL) || (reply == NULL)) {
188 HDF_LOGE("TimerIoOpen: param invalid!");
189 return HDF_ERR_INVALID_PARAM;
190 }
191 if (!HdfSbufReadUint16(data, (uint16_t *)&number)) {
192 HDF_LOGE("TimerIoOpen: read number fail!");
193 return HDF_ERR_IO;
194 }
195
196 if (number < 0) {
197 HDF_LOGE("TimerIoOpen: info read fail!");
198 return HDF_ERR_INVALID_PARAM;
199 }
200
201 if (TimerCntrlOpen(number) == NULL) {
202 HDF_LOGE("TimerIoOpen: timer cntlr open %d fail!", number);
203 return HDF_FAILURE;
204 }
205
206 handle = (uint32_t)(number + TIMER_HANDLE_SHIFT);
207 if (!HdfSbufWriteUint32(reply, handle)) {
208 HDF_LOGE("TimerIoOpen: write handle fail!");
209 return HDF_ERR_IO;
210 }
211 return HDF_SUCCESS;
212 }
213
TimerIoClose(struct HdfSBuf * data,struct HdfSBuf * reply)214 static int32_t TimerIoClose(struct HdfSBuf *data, struct HdfSBuf *reply)
215 {
216 uint32_t handle;
217 int16_t number;
218
219 (void)reply;
220 if (data == NULL) {
221 HDF_LOGE("TimerIoClose: data is null!");
222 return HDF_ERR_INVALID_PARAM;
223 }
224
225 if (!HdfSbufReadUint32(data, &handle)) {
226 HDF_LOGE("TimerIoClose: read handle fail!");
227 return HDF_ERR_IO;
228 }
229
230 number = (int16_t)(handle - TIMER_HANDLE_SHIFT);
231 if (number < 0) {
232 HDF_LOGE("TimerIoClose: number[%d] invalid!", number);
233 return HDF_ERR_INVALID_PARAM;
234 }
235 return TimerCntrlClose(TimerCntrlOpen(number));
236 }
237
TimerIoStart(struct HdfSBuf * data,struct HdfSBuf * reply)238 static int32_t TimerIoStart(struct HdfSBuf *data, struct HdfSBuf *reply)
239 {
240 uint32_t handle;
241 int16_t number;
242
243 (void)reply;
244 if (data == NULL) {
245 HDF_LOGE("TimerIoStart: data is null!");
246 return HDF_ERR_INVALID_PARAM;
247 }
248 if (!HdfSbufReadUint32(data, &handle)) {
249 HDF_LOGE("TimerIoStart: read handle fail!");
250 return HDF_ERR_IO;
251 }
252
253 number = (int16_t)(handle - TIMER_HANDLE_SHIFT);
254 if (number < 0) {
255 HDF_LOGE("TimerIoStart: number[%d] is invalid!", number);
256 return HDF_ERR_INVALID_PARAM;
257 }
258 return TimerCntrlStart(TimerCntrlOpen(number));
259 }
260
TimerIoStop(struct HdfSBuf * data,struct HdfSBuf * reply)261 static int32_t TimerIoStop(struct HdfSBuf *data, struct HdfSBuf *reply)
262 {
263 uint32_t handle;
264 int16_t number;
265
266 (void)reply;
267 if (data == NULL) {
268 HDF_LOGE("TimerIoStop: reply is null!");
269 return HDF_ERR_INVALID_PARAM;
270 }
271 if (!HdfSbufReadUint32(data, &handle)) {
272 HDF_LOGE("TimerIoStop: read handle fail!");
273 return HDF_ERR_IO;
274 }
275
276 number = (int16_t)(handle - TIMER_HANDLE_SHIFT);
277 if (number < 0) {
278 HDF_LOGE("TimerIoStop: number[%d] is invalid!", number);
279 return HDF_ERR_INVALID_PARAM;
280 }
281 return TimerCntrlStop(TimerCntrlOpen(number));
282 }
283
TimerIoCb(uint32_t number)284 static int32_t TimerIoCb(uint32_t number)
285 {
286 uint32_t handle = number + TIMER_HANDLE_SHIFT;
287 int32_t ret;
288 struct HdfSBuf *data = NULL;
289
290 data = HdfSbufObtainDefaultSize();
291 if (data == NULL) {
292 HDF_LOGE("TimerIoCb: fail to obtain data!");
293 return HDF_ERR_IO;
294 }
295 if (!HdfSbufWriteUint32(data, handle)) {
296 HDF_LOGE("TimerIoCb: write handle fail!");
297 HdfSbufRecycle(data);
298 return HDF_ERR_IO;
299 }
300 ret = HdfDeviceSendEvent(g_timerManager->device, PLATFORM_LISTENER_EVENT_TIMER_NOTIFY, data);
301 HdfSbufRecycle(data);
302 HDF_LOGD("TimerIoCb: set service info done, ret = %d %d", ret, handle);
303 return HDF_SUCCESS;
304 }
305
TimerIoSet(struct HdfSBuf * data,struct HdfSBuf * reply)306 static int32_t TimerIoSet(struct HdfSBuf *data, struct HdfSBuf *reply)
307 {
308 uint32_t len;
309 uint32_t handle;
310 int16_t number;
311 struct TimerConfig *cfg = NULL;
312
313 (void)reply;
314 if (data == NULL) {
315 HDF_LOGE("TimerIoSet: data is null!");
316 return HDF_ERR_INVALID_PARAM;
317 }
318 if (!HdfSbufReadUint32(data, &handle)) {
319 HDF_LOGE("TimerIoSet: read handle fail!");
320 return HDF_ERR_IO;
321 }
322
323 if (!HdfSbufReadBuffer(data, (const void **)&cfg, &len) || cfg == NULL) {
324 HDF_LOGE("TimerIoSet: read buffer fail!");
325 return HDF_ERR_IO;
326 }
327
328 number = (int16_t)(handle - TIMER_HANDLE_SHIFT);
329 if (number < 0) {
330 HDF_LOGE("TimerIoSet: number[%d] is invalid!", number);
331 return HDF_ERR_INVALID_PARAM;
332 }
333 return TimerCntrlSet(TimerCntrlOpen(number), cfg->useconds, TimerIoCb);
334 }
335
TimerIoSetOnce(struct HdfSBuf * data,struct HdfSBuf * reply)336 static int32_t TimerIoSetOnce(struct HdfSBuf *data, struct HdfSBuf *reply)
337 {
338 uint32_t len;
339 uint32_t handle;
340 int16_t number;
341 struct TimerConfig *cfg = NULL;
342
343 (void)reply;
344 if (data == NULL) {
345 HDF_LOGE("TimerIoSetOnce: data is null!");
346 return HDF_ERR_INVALID_PARAM;
347 }
348 if (!HdfSbufReadUint32(data, &handle)) {
349 HDF_LOGE("TimerIoSetOnce: read handle fail!");
350 return HDF_ERR_IO;
351 }
352
353 if (!HdfSbufReadBuffer(data, (const void **)&cfg, &len) || cfg == NULL) {
354 HDF_LOGE("TimerIoSetOnce: read buffer fail!");
355 return HDF_ERR_IO;
356 }
357
358 number = (int16_t)(handle - TIMER_HANDLE_SHIFT);
359 if (number < 0) {
360 HDF_LOGE("TimerIoSetOnce: number[%d] is invalid!", number);
361 return HDF_ERR_INVALID_PARAM;
362 }
363 return TimerCntrlSetOnce(TimerCntrlOpen(number), cfg->useconds, TimerIoCb);
364 }
365
TimerIoGet(struct HdfSBuf * data,struct HdfSBuf * reply)366 static int32_t TimerIoGet(struct HdfSBuf *data, struct HdfSBuf *reply)
367 {
368 int32_t ret = HDF_SUCCESS;
369 struct TimerConfig cfg;
370 uint32_t handle;
371 int16_t number;
372
373 if ((data == NULL) || (reply == NULL)) {
374 HDF_LOGE("TimerIoGet: data or reply is null!");
375 return HDF_ERR_INVALID_PARAM;
376 }
377
378 if (!HdfSbufReadUint32(data, &handle)) {
379 HDF_LOGE("TimerIoGet: read handle fail!");
380 return HDF_ERR_IO;
381 }
382
383 number = (int16_t)(handle - TIMER_HANDLE_SHIFT);
384 if (number < 0) {
385 HDF_LOGE("TimerIoGet: number[%d] is invalid!", number);
386 return HDF_ERR_INVALID_PARAM;
387 }
388 cfg.number = number;
389 ret = TimerCntrlGet(TimerCntrlOpen(number), &cfg.useconds, &cfg.isPeriod);
390 if (ret != HDF_SUCCESS) {
391 HDF_LOGE("TimerIoGet: get timer cntlr fail!");
392 return ret;
393 }
394
395 if (!HdfSbufWriteBuffer(reply, &cfg, sizeof(cfg))) {
396 HDF_LOGE("TimerIoGet: write buffer fail!");
397 return HDF_FAILURE;
398 }
399
400 return HDF_SUCCESS;
401 }
402
TimerIoDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)403 static int32_t TimerIoDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
404 {
405 int32_t ret;
406
407 (void)client;
408 switch (cmd) {
409 case TIMER_IO_OPEN:
410 ret = TimerIoOpen(data, reply);
411 break;
412 case TIMER_IO_CLOSE:
413 ret = TimerIoClose(data, reply);
414 break;
415 case TIMER_IO_START:
416 ret = TimerIoStart(data, reply);
417 break;
418 case TIMER_IO_STOP:
419 ret = TimerIoStop(data, reply);
420 break;
421 case TIMER_IO_SET:
422 ret = TimerIoSet(data, reply);
423 break;
424 case TIMER_IO_SETONCE:
425 ret = TimerIoSetOnce(data, reply);
426 break;
427 case TIMER_IO_GET:
428 ret = TimerIoGet(data, reply);
429 break;
430 default:
431 ret = HDF_ERR_NOT_SUPPORT;
432 HDF_LOGE("TimerIoDispatch: cmd[%d] is not support!", cmd);
433 break;
434 }
435 return ret;
436 }
437
TimerListRemoveAll(void)438 int32_t TimerListRemoveAll(void)
439 {
440 struct TimerCntrl *pos = NULL;
441 struct TimerCntrl *tmp = NULL;
442 struct TimerManager *manager = g_timerManager;
443
444 if (OsalMutexLock(&manager->lock) != HDF_SUCCESS) {
445 HDF_LOGE("TimerListRemoveAll: lock regulator manager fail!");
446 return HDF_ERR_DEVICE_BUSY;
447 }
448
449 DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &manager->timerListHead, struct TimerCntrl, node) {
450 if ((pos->ops->Remove != NULL) && (pos->ops->Remove(pos) != HDF_SUCCESS)) {
451 HDF_LOGE("TimerListRemoveAll: remove %u fail!", pos->info.number);
452 }
453 DListRemove(&pos->node);
454 (void)OsalMutexDestroy(&pos->lock);
455 OsalMemFree(pos);
456 }
457
458 (void)OsalMutexUnlock(&manager->lock);
459 HDF_LOGI("TimerListRemoveAll: remove all regulator success!");
460 return HDF_SUCCESS;
461 }
462
TimerCntrlAdd(struct TimerCntrl * cntrl)463 int32_t TimerCntrlAdd(struct TimerCntrl *cntrl)
464 {
465 CHECK_NULL_PTR_RETURN_VALUE(cntrl, HDF_ERR_INVALID_PARAM);
466 CHECK_NULL_PTR_RETURN_VALUE(cntrl->ops, HDF_ERR_INVALID_PARAM);
467 struct TimerCntrl *pos = NULL;
468 struct TimerCntrl *tmp = NULL;
469 struct TimerManager *manager = g_timerManager;
470 CHECK_NULL_PTR_RETURN_VALUE(manager, HDF_FAILURE);
471
472 DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &manager->timerListHead, struct TimerCntrl, node) {
473 if (cntrl->info.number == pos->info.number) {
474 HDF_LOGE("TimerCntrlAdd: timer[%u] existed!", cntrl->info.number);
475 return HDF_FAILURE;
476 }
477 }
478
479 // init info
480 if (OsalMutexInit(&cntrl->lock) != HDF_SUCCESS) {
481 HDF_LOGE("TimerCntrlAdd: OsalMutexInit %u fail!", cntrl->info.number);
482 return HDF_FAILURE;
483 }
484
485 if (OsalMutexLock(&manager->lock) != HDF_SUCCESS) {
486 HDF_LOGE("TimerCntrlAdd: OsalMutexLock %u fail!", cntrl->info.number);
487 return HDF_ERR_DEVICE_BUSY;
488 }
489 DListInsertTail(&cntrl->node, &manager->timerListHead);
490 (void)OsalMutexUnlock(&manager->lock);
491 HDF_LOGI("TimerCntrlAdd: add timer number[%u] success!", cntrl->info.number);
492
493 return HDF_SUCCESS;
494 }
495
TimerCntrlRemoveByNumber(const uint32_t number)496 int32_t TimerCntrlRemoveByNumber(const uint32_t number)
497 {
498 struct TimerCntrl *pos = NULL;
499 struct TimerCntrl *tmp = NULL;
500 struct TimerManager *manager = g_timerManager;
501 CHECK_NULL_PTR_RETURN_VALUE(manager, HDF_FAILURE);
502
503 if (OsalMutexLock(&manager->lock) != HDF_SUCCESS) {
504 HDF_LOGE("TimerCntrlRemoveByNumber: OsalMutexLock fail!");
505 return HDF_ERR_DEVICE_BUSY;
506 }
507
508 DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &manager->timerListHead, struct TimerCntrl, node) {
509 if (number == pos->info.number) {
510 if ((pos->ops->Remove != NULL) && (pos->ops->Remove(pos) != HDF_SUCCESS)) {
511 HDF_LOGE("TimerCntrlRemoveByNumber: remove %u fail!", pos->info.number);
512 }
513 (void)OsalMutexDestroy(&pos->lock);
514 DListRemove(&pos->node);
515 OsalMemFree(pos);
516 break;
517 }
518 }
519
520 (void)OsalMutexUnlock(&manager->lock);
521 HDF_LOGI("TimerCntrlRemoveByNumber: remove timer %u success!", number);
522 return HDF_SUCCESS;
523 }
524
TimerManagerBind(struct HdfDeviceObject * device)525 static int32_t TimerManagerBind(struct HdfDeviceObject *device)
526 {
527 int32_t ret;
528 struct TimerManager *manager = NULL;
529
530 CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_OBJECT);
531
532 manager = (struct TimerManager *)OsalMemCalloc(sizeof(*manager));
533 if (manager == NULL) {
534 HDF_LOGE("TimerManagerBind: malloc manager fail!");
535 return HDF_ERR_MALLOC_FAIL;
536 }
537
538 ret = OsalMutexInit(&manager->lock);
539 if (ret != HDF_SUCCESS) {
540 HDF_LOGE("TimerManagerBind: mutex init fail, ret: %d", ret);
541 OsalMemFree(manager);
542 manager = NULL;
543 return HDF_FAILURE;
544 }
545
546 manager->device = device;
547 device->service = &manager->service;
548 device->service->Dispatch = TimerIoDispatch;
549 DListHeadInit(&manager->timerListHead);
550 g_timerManager = manager;
551
552 HDF_LOGI("TimerManagerBind: success!");
553 return HDF_SUCCESS;
554 }
555
TimerManagerInit(struct HdfDeviceObject * device)556 static int32_t TimerManagerInit(struct HdfDeviceObject *device)
557 {
558 HdfDeviceSetClass(device, DEVICE_CLASS_PLAT);
559 HDF_LOGI("TimerManagerInit: success!");
560 return HDF_SUCCESS;
561 }
562
TimerManagerRelease(struct HdfDeviceObject * device)563 static void TimerManagerRelease(struct HdfDeviceObject *device)
564 {
565 HDF_LOGI("TimerManagerRelease: enter!");
566 CHECK_NULL_PTR_RETURN(device);
567
568 if (TimerListRemoveAll() != HDF_SUCCESS) {
569 HDF_LOGE("TimerManagerRelease: timer list remove all fail!");
570 }
571
572 struct TimerManager *manager = (struct TimerManager *)device->service;
573 CHECK_NULL_PTR_RETURN(manager);
574 OsalMutexDestroy(&manager->lock);
575 OsalMemFree(manager);
576 g_timerManager = NULL;
577 }
578
579 struct HdfDriverEntry g_timerManagerEntry = {
580 .moduleVersion = 1,
581 .Bind = TimerManagerBind,
582 .Init = TimerManagerInit,
583 .Release = TimerManagerRelease,
584 .moduleName = "HDF_PLATFORM_TIMER_MANAGER",
585 };
586
587 HDF_INIT(g_timerManagerEntry);
588