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/wakelock.h" 33f9f848faSopenharmony_ci#include "los_spinlock.h" 34f9f848faSopenharmony_ci 35f9f848faSopenharmony_ci 36f9f848faSopenharmony_ci#define SET_BIT(bitmap, pos) ((bitmap) |= (1u << (pos))) 37f9f848faSopenharmony_ci#define CLEAR_BIT(bitmap, pos) ((bitmap) &= ~(1u << (pos))) 38f9f848faSopenharmony_ci#define CHECK_BIT(bitmap, pos) (((bitmap) & (1u << (pos))) ? 1 : 0) 39f9f848faSopenharmony_ci 40f9f848faSopenharmony_ci#define WAKELOCK_BITMAP_CNT 32 41f9f848faSopenharmony_ci 42f9f848faSopenharmony_ci/* spinlock for wake lock module only available on SMP mode */ 43f9f848faSopenharmony_ciLITE_OS_SEC_BSS SPIN_LOCK_INIT(g_wakeLockSpin); 44f9f848faSopenharmony_ci 45f9f848faSopenharmony_citypedef struct { 46f9f848faSopenharmony_ci UINT32 lock; /* Indicate which bit is locked */ 47f9f848faSopenharmony_ci UINT32 used; /* Indicate used bit */ 48f9f848faSopenharmony_ci} WakeLockBitmap; 49f9f848faSopenharmony_ci 50f9f848faSopenharmony_ciSTATIC WakeLockBitmap g_wlBitmap; 51f9f848faSopenharmony_ci 52f9f848faSopenharmony_ciSTATIC INT32 FindEmptyBit(UINT32 bitmap) 53f9f848faSopenharmony_ci{ 54f9f848faSopenharmony_ci UINT32 t = 1; 55f9f848faSopenharmony_ci INT32 i; 56f9f848faSopenharmony_ci for (i = 0; i < WAKELOCK_BITMAP_CNT; i++) { 57f9f848faSopenharmony_ci if (!(t & bitmap)) { 58f9f848faSopenharmony_ci break; 59f9f848faSopenharmony_ci } else { 60f9f848faSopenharmony_ci t <<= 1; 61f9f848faSopenharmony_ci } 62f9f848faSopenharmony_ci } 63f9f848faSopenharmony_ci 64f9f848faSopenharmony_ci if (i == WAKELOCK_BITMAP_CNT) { 65f9f848faSopenharmony_ci return -1; 66f9f848faSopenharmony_ci } 67f9f848faSopenharmony_ci return i; 68f9f848faSopenharmony_ci} 69f9f848faSopenharmony_ci 70f9f848faSopenharmony_ci/* 71f9f848faSopenharmony_ci * Initial a wakelock 72f9f848faSopenharmony_ci * Attention: DO NOT init more than 32 wakelock! 73f9f848faSopenharmony_ci */ 74f9f848faSopenharmony_civoid linux_wake_lock_init(struct wake_lock *lock, int type, const char *name) 75f9f848faSopenharmony_ci{ 76f9f848faSopenharmony_ci UINT32 intSave; 77f9f848faSopenharmony_ci INT32 pos; 78f9f848faSopenharmony_ci (VOID)type; 79f9f848faSopenharmony_ci 80f9f848faSopenharmony_ci if (lock == NULL) { 81f9f848faSopenharmony_ci PRINT_ERR("%s : invalid parameter!\n", __FUNCTION__); 82f9f848faSopenharmony_ci return; 83f9f848faSopenharmony_ci } 84f9f848faSopenharmony_ci 85f9f848faSopenharmony_ci lock->ws.name = name; 86f9f848faSopenharmony_ci 87f9f848faSopenharmony_ci LOS_SpinLockSave(&g_wakeLockSpin, &intSave); 88f9f848faSopenharmony_ci pos = FindEmptyBit(g_wlBitmap.used); 89f9f848faSopenharmony_ci if (pos != -1) { 90f9f848faSopenharmony_ci lock->bitmap_pos = (UINT32)pos; 91f9f848faSopenharmony_ci SET_BIT(g_wlBitmap.used, lock->bitmap_pos); 92f9f848faSopenharmony_ci CLEAR_BIT(g_wlBitmap.lock, lock->bitmap_pos); 93f9f848faSopenharmony_ci } else { 94f9f848faSopenharmony_ci LOS_SpinUnlockRestore(&g_wakeLockSpin, intSave); 95f9f848faSopenharmony_ci PRINT_ERR("%s : wakelock count exceeds 32!\n", __FUNCTION__); 96f9f848faSopenharmony_ci return; 97f9f848faSopenharmony_ci } 98f9f848faSopenharmony_ci 99f9f848faSopenharmony_ci LOS_SpinUnlockRestore(&g_wakeLockSpin, intSave); 100f9f848faSopenharmony_ci} 101f9f848faSopenharmony_ci 102f9f848faSopenharmony_civoid linux_wake_lock(struct wake_lock *lock) 103f9f848faSopenharmony_ci{ 104f9f848faSopenharmony_ci UINT32 intSave; 105f9f848faSopenharmony_ci if (lock == NULL) { 106f9f848faSopenharmony_ci return; 107f9f848faSopenharmony_ci } 108f9f848faSopenharmony_ci 109f9f848faSopenharmony_ci LOS_SpinLockSave(&g_wakeLockSpin, &intSave); 110f9f848faSopenharmony_ci SET_BIT(g_wlBitmap.lock, lock->bitmap_pos); 111f9f848faSopenharmony_ci LOS_SpinUnlockRestore(&g_wakeLockSpin, intSave); 112f9f848faSopenharmony_ci} 113f9f848faSopenharmony_ci 114f9f848faSopenharmony_civoid linux_wake_unlock(struct wake_lock *lock) 115f9f848faSopenharmony_ci{ 116f9f848faSopenharmony_ci UINT32 intSave; 117f9f848faSopenharmony_ci if (lock == NULL) { 118f9f848faSopenharmony_ci return; 119f9f848faSopenharmony_ci } 120f9f848faSopenharmony_ci 121f9f848faSopenharmony_ci LOS_SpinLockSave(&g_wakeLockSpin, &intSave); 122f9f848faSopenharmony_ci CLEAR_BIT(g_wlBitmap.lock, lock->bitmap_pos); 123f9f848faSopenharmony_ci LOS_SpinUnlockRestore(&g_wakeLockSpin, intSave); 124f9f848faSopenharmony_ci} 125f9f848faSopenharmony_ci 126f9f848faSopenharmony_ci/* Judge whether the wake lock is active or not */ 127f9f848faSopenharmony_ciint linux_wake_lock_active(struct wake_lock *lock) 128f9f848faSopenharmony_ci{ 129f9f848faSopenharmony_ci INT32 ret; 130f9f848faSopenharmony_ci UINT32 intSave; 131f9f848faSopenharmony_ci if (lock == NULL) { 132f9f848faSopenharmony_ci return -1; 133f9f848faSopenharmony_ci } 134f9f848faSopenharmony_ci LOS_SpinLockSave(&g_wakeLockSpin, &intSave); 135f9f848faSopenharmony_ci ret = CHECK_BIT(g_wlBitmap.lock, lock->bitmap_pos); 136f9f848faSopenharmony_ci LOS_SpinUnlockRestore(&g_wakeLockSpin, intSave); 137f9f848faSopenharmony_ci return ret; 138f9f848faSopenharmony_ci} 139f9f848faSopenharmony_ci 140f9f848faSopenharmony_civoid linux_wake_lock_destroy(struct wake_lock *lock) 141f9f848faSopenharmony_ci{ 142f9f848faSopenharmony_ci UINT32 intSave; 143f9f848faSopenharmony_ci if (lock == NULL) { 144f9f848faSopenharmony_ci return; 145f9f848faSopenharmony_ci } 146f9f848faSopenharmony_ci LOS_SpinLockSave(&g_wakeLockSpin, &intSave); 147f9f848faSopenharmony_ci CLEAR_BIT(g_wlBitmap.used, lock->bitmap_pos); 148f9f848faSopenharmony_ci CLEAR_BIT(g_wlBitmap.lock, lock->bitmap_pos); 149f9f848faSopenharmony_ci LOS_SpinUnlockRestore(&g_wakeLockSpin, intSave); 150f9f848faSopenharmony_ci} 151f9f848faSopenharmony_ci 152