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