1 /*
2 * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "linux/timer.h"
33 #include "los_swtmr.h"
34
35
linux_init_timer(struct timer_list * timer)36 void linux_init_timer(struct timer_list *timer)
37 {
38 UINT32 intSave;
39
40 if (timer == NULL) {
41 PRINTK("init_timer<error> timer is NULL\n");
42 return;
43 }
44 LOS_SpinInit(&timer->lock);
45 LOS_SpinLockSave(&timer->lock, &intSave);
46 timer->flag = TIMER_UNVALID;
47 LOS_SpinUnlockRestore(&timer->lock, intSave);
48 }
49
DoDeleteTimer(struct timer_list * timer)50 STATIC UINT32 DoDeleteTimer(struct timer_list *timer)
51 {
52 UINT32 ret;
53
54 ret = LOS_SwtmrDelete(timer->timerid);
55 if (ret == LOS_OK) {
56 timer->flag = TIMER_UNVALID;
57 }
58 return ret;
59 }
60
DoAddTimer(struct timer_list * timer)61 STATIC UINT32 DoAddTimer(struct timer_list *timer)
62 {
63 UINT32 ret;
64
65 ret = LOS_SwtmrCreate(timer->expires, LOS_SWTMR_MODE_NO_SELFDELETE, (SWTMR_PROC_FUNC)timer->function,
66 &timer->timerid, timer->data);
67 if (ret != LOS_OK) {
68 PRINTK("add_timer<error> timer create failed: %u \n", ret);
69 return ret;
70 }
71 ret = LOS_SwtmrStart(timer->timerid);
72 if (ret != LOS_OK) {
73 PRINTK("add_timer<error> timer start failed: %u \n", ret);
74 }
75 return ret;
76 }
77
linux_add_timer(struct timer_list * timer)78 void linux_add_timer(struct timer_list *timer)
79 {
80 UINT32 intSave;
81
82 if (timer == NULL) {
83 PRINTK("add_timer<error> timer is NULL\n");
84 return;
85 }
86
87 LOS_SpinLockSave(&timer->lock, &intSave);
88 if (timer->flag == TIMER_VALID) {
89 if (DoDeleteTimer(timer) != LOS_OK) {
90 goto ERROUT;
91 }
92 }
93
94 if (DoAddTimer(timer) != LOS_OK) {
95 goto ERROUT;
96 }
97 timer->flag = TIMER_VALID;
98 ERROUT:
99 LOS_SpinUnlockRestore(&timer->lock, intSave);
100 return;
101 }
102
linux_del_timer(struct timer_list * timer)103 int linux_del_timer(struct timer_list *timer)
104 {
105 UINT32 intSave;
106 INT32 ret = 0;
107
108 if (timer == NULL) {
109 PRINTK("del_timer<error> timer is NULL\n");
110 return ret;
111 }
112
113 LOS_SpinLockSave(&timer->lock, &intSave);
114 if (timer->flag == TIMER_VALID) {
115 ret = (DoDeleteTimer(timer) == LOS_OK) ? 1 : 0;
116 }
117 LOS_SpinUnlockRestore(&timer->lock, intSave);
118
119 return ret;
120 }
121
linux_mod_timer(struct timer_list * timer,unsigned long expires)122 int linux_mod_timer(struct timer_list *timer, unsigned long expires)
123 {
124 INT32 ret = 0;
125 UINT32 intSave;
126
127 if (timer == NULL) {
128 return ret;
129 }
130
131 LOS_SpinLockSave(&timer->lock, &intSave);
132 if (timer->flag == TIMER_VALID) {
133 if (DoDeleteTimer(timer) != LOS_OK) {
134 goto ERROUT;
135 }
136 timer->expires = expires;
137 if (DoAddTimer(timer) == LOS_OK) {
138 timer->flag = TIMER_VALID;
139 ret = 1;
140 }
141 }
142 ERROUT:
143 LOS_SpinUnlockRestore(&timer->lock, intSave);
144 return ret;
145 }
146
147