1 /*
2  *  Routines to access hardware
3  *
4  *  Copyright (c) 2013 Realtek Semiconductor Corp.
5  *
6  *  This module is a confidential and proprietary property of RealTek and
7  *  possession or use of this module requires written permission of RealTek.
8  */
9 
10 #include "osdep_service.h"
11 #include "device_lock.h"
12 
13 //------------------------------------------------------
14 #define DEVICE_MUTEX_IS_INIT(device)	(mutex_init & (1<<device))
15 #define DEVICE_MUTEX_SET_INIT(device)	(mutex_init |= (1<<device))
16 #define DEVICE_MUTEX_CLR_INIT(device)	(mutex_init &= (~(1<<device)))
17 
18 static u32 mutex_init = 0;
19 static _mutex device_mutex[RT_DEV_LOCK_MAX];
20 
21 //======================================================
device_mutex_init(RT_DEV_LOCK_E device)22 static void device_mutex_init(RT_DEV_LOCK_E device)
23 {
24 	if(!DEVICE_MUTEX_IS_INIT(device)){
25 		_lock lock;
26 		_irqL irqL;
27 		rtw_enter_critical(&lock, &irqL);
28 		if(!DEVICE_MUTEX_IS_INIT(device)){
29 			rtw_mutex_init(&device_mutex[device]);
30 			DEVICE_MUTEX_SET_INIT(device);
31 		}
32 		rtw_exit_critical(&lock, &irqL);
33 	}
34 }
35 
36 //======================================================
device_mutex_free(RT_DEV_LOCK_E device)37 void device_mutex_free(RT_DEV_LOCK_E device)
38 {
39 	if(DEVICE_MUTEX_IS_INIT(device)){
40 		_lock lock;
41 		_irqL irqL;
42 		rtw_enter_critical(&lock, &irqL);
43 		if(!DEVICE_MUTEX_IS_INIT(device)){
44 			rtw_mutex_free(&device_mutex[device]);
45 			DEVICE_MUTEX_CLR_INIT(device);
46 		}
47 		rtw_exit_critical(&lock, &irqL);
48 	}
49 }
50 
51 //======================================================
device_mutex_lock(RT_DEV_LOCK_E device)52 void device_mutex_lock(RT_DEV_LOCK_E device)
53 {
54 	device_mutex_init(device);
55 	while(rtw_mutex_get_timeout(&device_mutex[device], 10000)<0)
56 		printf("device lock timeout: %d\n", (int)device);
57 }
58 
59 //======================================================
device_mutex_unlock(RT_DEV_LOCK_E device)60 void device_mutex_unlock(RT_DEV_LOCK_E device)
61 {
62 	device_mutex_init(device);
63 	rtw_mutex_put(&device_mutex[device]);
64 }
65