• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/printk.h>
22 #include <linux/timer.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/version.h>
26 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
27 #include <linux/sched/clock.h>
28 #else
29 #include <linux/sched.h>
30 #endif
31 #include <linux/rtc.h>
32 #include "hi_osal.h"
33 
osal_hrtimer_create(osal_hrtimer * hrtimer)34 int osal_hrtimer_create(osal_hrtimer *hrtimer)
35 {
36     return -1;
37 }
osal_hrtimer_start(osal_hrtimer * hrtimer)38 int osal_hrtimer_start(osal_hrtimer *hrtimer)
39 {
40     return -1;
41 }
osal_hrtimer_destory(osal_hrtimer * hrtimer)42 int osal_hrtimer_destory(osal_hrtimer *hrtimer)
43 {
44     return -1;
45 }
46 
osal_timer_init(osal_timer * timer)47 int osal_timer_init(osal_timer *timer)
48 {
49     struct timer_list *t = NULL;
50 
51     if (timer == NULL) {
52         osal_printk("%s - parameter invalid!\n", __FUNCTION__);
53         return -1;
54     }
55 
56     t = (struct timer_list *)kmalloc(sizeof(struct timer_list), GFP_KERNEL);
57     if (t == NULL) {
58         osal_printk("%s - kmalloc error!\n", __FUNCTION__);
59         return -1;
60     }
61 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
62     timer_setup(t, NULL, 0);
63 #else
64     init_timer(t);
65 #endif
66     timer->timer = t;
67     return 0;
68 }
69 EXPORT_SYMBOL(osal_timer_init);
70 
osal_timer_set(osal_timer * timer,unsigned long interval)71 int osal_timer_set(osal_timer *timer, unsigned long interval)
72 {
73     struct timer_list *t = NULL;
74     if ((timer == NULL) || (timer->timer == NULL) || (timer->handler == NULL) || (interval == 0)) {
75         osal_printk("%s - parameter invalid!\n", __FUNCTION__);
76         return -1;
77     }
78     t = timer->timer;
79 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
80     t->function = (void (*)(struct timer_list *))timer->handler;
81 #else
82     t->function = timer->handler;
83     t->data = timer->data;
84 #endif
85     return mod_timer(t, jiffies + msecs_to_jiffies(interval) - 1);
86 }
87 EXPORT_SYMBOL(osal_timer_set);
88 
osal_timer_del(osal_timer * timer)89 int osal_timer_del(osal_timer *timer)
90 {
91     struct timer_list *t = NULL;
92 
93     if ((timer == NULL) || (timer->timer == NULL) || (timer->handler == NULL)) {
94         osal_printk("%s - parameter invalid!\n", __FUNCTION__);
95         return -1;
96     }
97 
98     t = timer->timer;
99     return del_timer(t);
100 }
101 EXPORT_SYMBOL(osal_timer_del);
102 
osal_timer_destory(osal_timer * timer)103 int osal_timer_destory(osal_timer *timer)
104 {
105     struct timer_list *t = NULL;
106 
107     if ((timer == NULL) || (timer->timer == NULL)) {
108         osal_printk("%s - parameter invalid!\n", __FUNCTION__);
109         return -1;
110     }
111 
112     t = timer->timer;
113     del_timer(t);
114     kfree(t);
115     timer->timer = NULL;
116     return 0;
117 }
118 EXPORT_SYMBOL(osal_timer_destory);
119 
osal_msleep(unsigned int msecs)120 unsigned long osal_msleep(unsigned int msecs)
121 {
122     return msleep_interruptible(msecs);
123 }
124 EXPORT_SYMBOL(osal_msleep);
125 
osal_msleep_uninterruptible(unsigned int msecs)126 void osal_msleep_uninterruptible(unsigned int msecs)
127 {
128     msleep(msecs);
129 }
130 EXPORT_SYMBOL(osal_msleep_uninterruptible);
131 
osal_udelay(unsigned int usecs)132 void osal_udelay(unsigned int usecs)
133 {
134     udelay(usecs);
135 }
136 EXPORT_SYMBOL(osal_udelay);
137 
osal_mdelay(unsigned int msecs)138 void osal_mdelay(unsigned int msecs)
139 {
140     mdelay(msecs);
141 }
142 EXPORT_SYMBOL(osal_mdelay);
143 
osal_get_tickcount()144 unsigned int osal_get_tickcount()
145 {
146     return jiffies_to_msecs(jiffies);
147 }
148 EXPORT_SYMBOL(osal_get_tickcount);
149 
osal_sched_clock()150 unsigned long long osal_sched_clock()
151 {
152     return sched_clock();
153 }
154 EXPORT_SYMBOL(osal_sched_clock);
155 
156 #define S_TO_NS 1000000000ULL
157 #define US_TO_NS 1000
osal_get_timeofday(osal_timeval * tv)158 void osal_get_timeofday(osal_timeval *tv)
159 {
160 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
161     uint64_t ns = ktime_to_ns(ktime_get());
162     tv->tv_sec = osal_div64_u64(ns, S_TO_NS);
163     tv->tv_usec = osal_div_u64(osal_div64_u64_rem(ns, S_TO_NS), US_TO_NS);
164 #else
165     struct timeval t;
166 
167     if (tv == NULL) {
168         osal_printk("%s - parameter invalid!\n", __FUNCTION__);
169         return;
170     }
171     do_gettimeofday(&t);
172 
173     tv->tv_sec = t.tv_sec;
174     tv->tv_usec = t.tv_usec;
175 #endif
176 }
177 EXPORT_SYMBOL(osal_get_timeofday);
178 
osal_get_jiffies(void)179 unsigned long long osal_get_jiffies(void)
180 {
181     return jiffies;
182 }
183 EXPORT_SYMBOL(osal_get_jiffies);
184 
osal_time_after(unsigned long a,unsigned long b)185 int osal_time_after(unsigned long a, unsigned long b)
186 {
187     return (a > b);
188 }
189 EXPORT_SYMBOL(osal_time_after);
190 
osal_time_before(unsigned long a,unsigned long b)191 int osal_time_before(unsigned long a, unsigned long b)
192 {
193     return (b > a);
194 }
195 EXPORT_SYMBOL(osal_time_before);
196 
osal_get_pid(void)197 int osal_get_pid(void)
198 {
199     return (int)task_tgid_nr(current);
200 }
201 EXPORT_SYMBOL(osal_get_pid);
202 
203 
osal_rtc_time_to_tm(unsigned long time,osal_rtc_time * tm)204 void osal_rtc_time_to_tm(unsigned long time, osal_rtc_time *tm)
205 {
206     struct rtc_time _tm = { 0 };
207 
208 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
209     rtc_time64_to_tm(time, &_tm);
210 #else
211     rtc_time_to_tm(time, &_tm);
212 #endif
213 
214     tm->tm_sec = _tm.tm_sec;
215     tm->tm_min = _tm.tm_min;
216     tm->tm_hour = _tm.tm_hour;
217     tm->tm_mday = _tm.tm_mday;
218     tm->tm_mon = _tm.tm_mon;
219     tm->tm_year = _tm.tm_year;
220     tm->tm_wday = _tm.tm_wday;
221     tm->tm_yday = _tm.tm_yday;
222     tm->tm_isdst = _tm.tm_isdst;
223 }
224 EXPORT_SYMBOL(osal_rtc_time_to_tm);
225 
osal_rtc_tm_to_time(osal_rtc_time * tm,unsigned long * time)226 void osal_rtc_tm_to_time(osal_rtc_time *tm, unsigned long *time)
227 {
228     struct rtc_time _tm;
229     _tm.tm_sec = tm->tm_sec;
230     _tm.tm_min = tm->tm_min;
231     _tm.tm_hour = tm->tm_hour;
232     _tm.tm_mday = tm->tm_mday;
233     _tm.tm_mon = tm->tm_mon;
234     _tm.tm_year = tm->tm_year;
235     _tm.tm_wday = tm->tm_wday;
236     _tm.tm_yday = tm->tm_yday;
237     _tm.tm_isdst = tm->tm_isdst;
238 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
239     *time = rtc_tm_to_time64(&_tm);
240 #else
241     rtc_tm_to_time(&_tm, time);
242 #endif
243 }
244 EXPORT_SYMBOL(osal_rtc_tm_to_time);
245 
246 
osal_rtc_valid_tm(osal_rtc_time * tm)247 int osal_rtc_valid_tm(osal_rtc_time *tm)
248 {
249     struct rtc_time _tm;
250     _tm.tm_sec = tm->tm_sec;
251     _tm.tm_min = tm->tm_min;
252     _tm.tm_hour = tm->tm_hour;
253     _tm.tm_mday = tm->tm_mday;
254     _tm.tm_mon = tm->tm_mon;
255     _tm.tm_year = tm->tm_year;
256     _tm.tm_wday = tm->tm_wday;
257     _tm.tm_yday = tm->tm_yday;
258     _tm.tm_isdst = tm->tm_isdst;
259 
260     return rtc_valid_tm(&_tm);
261 }
262 EXPORT_SYMBOL(osal_rtc_valid_tm);
263