xref: /utopia/UTPA2-700.0.x/modules/usb/drv/usb_ecos/newhost/drvTimer.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 
79 //=============================================================================
80 //#include <MsCommon.h> // NUSED
81 
82 //#include "include/drvConfig.h" // NUSED
83 //#include "include/drvCompiler.h" // NUSED
84 //#include "include/drvErrno.h" // NUSED
85 //#include "include/drvPorts.h" // NUSED
86 //#include "include/drvTimer.h" // NUSED
87 #include "include/drvKernel.h"
88 //#include "include/drvList.h" // NUSED
89 
90 U32 volatile jiffies;
91 
92 static void ms_SW_Timer_Tick(U32 u32StTimer, U32 u32TimerID);
93 static struct list_head sw_timer_vec_head;
94 
95 S32 g_Timer_ID;
96 S32 _s32MutexUTimer;
ms_init_timers(void)97 void ms_init_timers(void)
98 {
99     ms_list_init(&sw_timer_vec_head);
100     // Register software timer dispatch program
101     // Base 1 ms, interruptted every 1 ms
102 
103     BOOL err;
104 
105     g_Timer_ID = MsOS_CreateTimer((TimerCb) ms_SW_Timer_Tick,100, USB_SW_TIMER_TICK, FALSE, "USB_TIMER");
106     //err = OS_DefineCyclicHandler(&g_Timer_ID, ms_SW_Timer_Tick, (void*) 0, 100);
107     //USB_ASSERT((err == TRUE), "Define USB timer fail");
108 
109     //err = OS_ActivateCyclicHandler(g_Timer_ID);
110     //
111     err=MsOS_StartTimer(g_Timer_ID);
112     USB_ASSERT((err == TRUE), "Activate USB timer fail");
113     diag_printf("Init Software Timer ok \n");
114     _s32MutexUTimer = MsOS_CreateMutex(E_MSOS_FIFO, "UTimer_MUTEX", MSOS_PROCESS_SHARED);
115 }
116 
ms_remove_timers(void)117 void ms_remove_timers(void)
118 {
119     MsOS_DeleteTimer(g_Timer_ID);
120     diag_printf("Remove Software Timer ok \n");
121     MsOS_DeleteMutex(_s32MutexUTimer);
122 }
123 
124 /*
125  * Timer ISR: It's responsible for dispatching tasks registered to software-timer
126  * task queue. It need to be implemented as Bottom-Half mechanism of Linux
127  * Timer and USB IRQ have the same priority and cannot be interrupted by each other
128  */
ms_SW_Timer_Tick(U32 u32StTimer,U32 u32TimerID)129 static void ms_SW_Timer_Tick(U32 u32StTimer, U32 u32TimerID)
130 {
131     struct timer_list *timer;
132     struct list_head  *tmp;
133 
134     jiffies += USB_SW_TIMER_TICK;
135 
136     //if ( Timer_task_lock == 1 )
137         //goto sw_done;
138 
139     if (!ms_is_empty_list(&sw_timer_vec_head))
140     {
141         tmp = sw_timer_vec_head.next;
142         while (tmp != &sw_timer_vec_head)
143         {
144             timer = entry_to_container (tmp, struct timer_list, entry);
145             tmp = tmp->next;
146             // decrease expire time
147             timer->expires -= USB_SW_TIMER_TICK;
148         }
149     }
150 
151     // Dispath task from task queue of software-timer here
152     if (!ms_is_empty_list(&sw_timer_vec_head))
153     {
154         tmp = sw_timer_vec_head.next;
155         while (tmp != &sw_timer_vec_head)
156         {
157             timer = entry_to_container (tmp, struct timer_list, entry);
158             tmp = tmp->next;
159             //list_entry (tmp, struct timer_list, entry, struct list_head, timer);
160             // Check if time expired ?
161             if ( timer->expires <= 0 )
162             {
163                 void (*pFunc)(U32);
164                 U32 data;
165                 // If lock ==> don't dispatch task
166                 /*
167                                         if ( Timer_task_lock == 1 )
168                                         {
169                                         timer->expires++;
170                                         continue;
171                                         }
172                                         */
173                 pFunc = timer->function;
174                 data = timer->data;
175                 ms_del_timer(timer);
176                 // Call registered function
177                 if ( (U32)pFunc )
178                 {
179                     //lock_usb_core(); // NUSED
180                     pFunc(data);
181                     //unlock_usb_core(); // NUSED
182                 }
183                 else
184                     USB_ASSERT( 0, "ERROR: fun is NULL\n");
185 
186                 //Current version only support dispatch one task each time
187                 //break;
188             }
189         }
190     }
191 
192 //sw_done:
193     return;
194     // End of dispatching
195 }
196 
197 /***
198  * ms_del_timer - deactive a timer.
199  * @timer: the timer to be deactivated
200  *
201  * ms_del_timer() deactivates a timer - this works on both active and inactive
202  * timers.
203  *
204  * The function returns whether it has deactivated a pending timer or not.
205  * (ie. ms_del_timer() of an inactive timer returns 0, del_timer() of an
206  * active timer returns 1.)
207  */
ms_del_timer(struct timer_list * timer)208 int ms_del_timer(struct timer_list *timer)
209 {
210     U32 u32OldIntr;
211 
212     osapi_spin_lock_irqsave(&sw_timer_lock, u32OldIntr);
213     if( timer->entry.next != NULL && timer->entry.prev != NULL )
214     {
215         USB_ASSERT(timer->entry.prev->next == &timer->entry, "timer->entry.prev->next error!!!!\n");
216         USB_ASSERT(timer->entry.next != 0, "timer->entry.next!!!!\n");
217         USB_ASSERT(timer->entry.next->prev == &timer->entry, "timer->entry.next->prev error!!!!\n");
218         USB_ASSERT(timer->entry.prev != 0, "timer->entry.prev error!!!!\n");
219         ms_list_remove_and_init(&timer->entry);
220     }
221 
222     osapi_spin_unlock_irqrestore(&sw_timer_lock, u32OldIntr);
223     return 1;
224 }
225 
226 /***
227  * ms_del_timer_sync - deactive a timer.
228  * @timer: the timer to be deactivated
229  *
230  * ms_del_timer_sync() deactivates a timer - this works on both active and inactive
231  * timers.
232  *
233  * The function will protect synchronization problem before deactivating timer
234  */
ms_del_timer_sync(struct timer_list * timer)235 int ms_del_timer_sync(struct timer_list *timer)
236 {
237     U32 u32OldIntr;
238 
239     osapi_spin_lock_irqsave(&sw_timer_lock, u32OldIntr);
240     if( timer->entry.next != NULL && timer->entry.prev != NULL )
241     {
242         USB_ASSERT(timer->entry.prev->next == &timer->entry, "timer->entry.prev->next error!!!!\n");
243         USB_ASSERT(timer->entry.next != 0, "timer->entry.next!!!!\n");
244         USB_ASSERT(timer->entry.next->prev == &timer->entry, "timer->entry.next->prev error!!!!\n");
245         USB_ASSERT(timer->entry.prev != 0, "timer->entry.prev error!!!!\n");
246         ms_list_remove_and_init(&timer->entry);
247     }
248 
249     osapi_spin_unlock_irqrestore(&sw_timer_lock, u32OldIntr);
250     return 1;
251 }
252 
253 /***
254  * ms_add_timer - start a timer
255  * @timer: the timer to be added
256  *
257  * The kernel will do a ->function(->data) callback from the
258  * timer interrupt at the ->expired point in the future. The
259  * current time is 'jiffies'.
260  *
261  * The timer's ->expired, ->function (and if the handler uses it, ->data)
262  * fields must be set prior calling this function.
263  *
264  * Timers with an ->expired field in the past will be executed in the next
265  * timer tick. It's illegal to add an already pending timer.
266  */
ms_add_timer(struct timer_list * timer)267 void ms_add_timer(struct timer_list *timer)
268 {
269     U32 u32OldIntr;
270 
271     osapi_spin_lock_irqsave(&sw_timer_lock, u32OldIntr);
272     if ( (timer->tmout_step > 0) && (timer->tmout_value > timer->tmout_step) )
273         timer->expires = timer->tmout_step;
274     else
275         timer->expires = timer->tmout_value;
276 
277     ms_insert_list_before(&timer->entry, &sw_timer_vec_head);
278 
279     osapi_spin_unlock_irqrestore(&sw_timer_lock, u32OldIntr);
280 }
281 
ms_update_timer(struct timer_list * timer,U32 expires,U32 steps)282 int ms_update_timer(struct timer_list *timer, U32 expires, U32 steps)
283 {
284     int ret = 0;
285     U32 u32OldIntr;
286 
287     osapi_spin_lock_irqsave(&sw_timer_lock, u32OldIntr);
288     timer->tmout_value = expires;
289     timer->tmout_step = steps;
290 
291     if( timer->entry.next != NULL && timer->entry.prev != NULL )
292     {
293         USB_ASSERT(timer->entry.prev->next == &timer->entry, "timer->entry.prev->next error!!!!\n");
294         USB_ASSERT(timer->entry.next != 0, "timer->entry.next!!!!\n");
295         USB_ASSERT(timer->entry.next->prev == &timer->entry, "timer->entry.next->prev error!!!!\n");
296         USB_ASSERT(timer->entry.prev != 0, "timer->entry.prev error!!!!\n");
297         ms_list_remove_and_init(&timer->entry);
298     }
299 
300     if ( (timer->tmout_step > 0) && (timer->tmout_value > timer->tmout_step) )
301         timer->expires = timer->tmout_step;
302     else
303         timer->expires = timer->tmout_value;
304     ms_insert_list_before(&timer->entry, &sw_timer_vec_head);
305 
306     osapi_spin_unlock_irqrestore(&sw_timer_lock, u32OldIntr);
307     return ret;
308 }
309 
310 #define MS_MSEC_PER_SEC    1000L
ms_jiffies_to_msecs(unsigned long jiffies_n)311 unsigned int ms_jiffies_to_msecs(unsigned long jiffies_n)
312 {
313     unsigned int retval;
314 
315     retval = (MS_MSEC_PER_SEC / HZ) * jiffies_n;
316 
317     return retval;
318 }
319