Source
26
26
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27
27
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28
28
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29
29
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30
30
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
31
*/
32
32
33
33
#include <stdlib.h>
34
34
#include <string.h>
35
35
#include <stdio.h>
36
-
37
36
#include <pthread.h>
38
37
39
38
#include <xf86drm.h>
40
39
#include <omap_drm.h>
41
40
#include <omap_drmif.h>
42
41
43
42
#include <ti/ipc/mm/MmRpc.h>
44
43
#include "dce_priv.h"
45
44
#include "libdce.h"
46
45
#include "dce_rpc.h"
47
46
#include "memplugin.h"
48
47
49
48
#define INVALID_DRM_FD (-1)
50
49
51
-
int OmapDrm_FD = INVALID_DRM_FD;
52
-
int bDrmOpenedByDce = FALSE;
50
+
static int OmapDrm_FD = INVALID_DRM_FD;
51
+
static int dce_init_count = 0;
53
52
struct omap_device *OmapDev = NULL;
54
53
extern MmRpc_Handle MmRpcHandle[];
55
-
56
54
extern pthread_mutex_t ipc_mutex;
57
55
58
56
void *dce_init(void)
59
57
{
60
58
dce_error_status eError = DCE_EOK;
59
+
pthread_mutexattr_t attr;
61
60
62
61
DEBUG(" >> dce_init");
63
62
64
-
pthread_mutexattr_t attr;
65
-
pthread_mutexattr_init(&attr);
66
-
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
67
-
pthread_mutex_init(&ipc_mutex, &attr);
63
+
/* Use this for refcount */
64
+
dce_init_count++;
68
65
69
-
/* Open omapdrm device */
70
-
if( OmapDrm_FD == INVALID_DRM_FD ) {
71
-
DEBUG("Open omapdrm device");
66
+
/* Open omapdrm device only for the first dce_init call */
67
+
if( dce_init_count == 1 ) {
68
+
DEBUG("Open omapdrm device and initializing the mutex...");
72
69
OmapDrm_FD = drmOpen("omapdrm", "platform:omapdrm:00");
73
70
_ASSERT(OmapDrm_FD > 0, DCE_EOMAPDRM_FAIL);
74
-
bDrmOpenedByDce = TRUE;
71
+
72
+
pthread_mutexattr_init(&attr);
73
+
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
74
+
pthread_mutex_init(&ipc_mutex, &attr);
75
75
}
76
+
76
77
OmapDev = omap_device_new(OmapDrm_FD);
77
78
_ASSERT(OmapDev != NULL, DCE_EOMAPDRM_FAIL);
78
79
79
80
EXIT:
80
81
return ((void *)OmapDev);
81
82
}
82
83
83
84
void dce_deinit(void *dev)
84
85
{
85
86
omap_device_del(dev);
86
87
dev = NULL;
87
-
if (bDrmOpenedByDce == TRUE) {
88
+
if (--dce_init_count == 0) {
89
+
DEBUG("Closing omapdrm device...");
88
90
close(OmapDrm_FD);
89
-
bDrmOpenedByDce = FALSE;
91
+
OmapDrm_FD = INVALID_DRM_FD;
90
92
}
91
-
OmapDrm_FD = INVALID_DRM_FD;
92
-
93
93
return;
94
94
}
95
95
96
96
int dce_buf_lock(int num, size_t *handle)
97
97
{
98
98
int i;
99
99
MmRpc_BufDesc *desc = NULL;
100
100
dce_error_status eError = DCE_EOK;
101
101
102
102
pthread_mutex_lock(&ipc_mutex);