for callback invoke when resolution is changed
[platform/adaptation/ap_samsung/libomxil-e3250-v4l2.git] / exynos4 / libion_exynos / libion.c
1 /*
2  * Copyright (C) 2012 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <ion.h>
18 #include <fcntl.h>
19 #include <sys/mman.h>
20 #include <sys/ioctl.h>
21 #ifndef TIZEN_FEATURE_E3250 /* build env */
22 #include <cutils/log.h>
23 #endif
24
25 typedef unsigned long ion_handle;
26
27 struct ion_allocation_data {
28     size_t len;
29     size_t align;
30     unsigned int heap_mask;
31     unsigned int flags;
32     ion_handle handle;
33 };
34
35 struct ion_fd_data {
36     ion_handle handle;
37     int fd;
38 };
39
40 struct ion_handle_data {
41     ion_handle handle;
42 };
43
44 struct ion_custom_data {
45     unsigned int cmd;
46     unsigned long arg;
47 };
48
49 #define ION_IOC_MAGIC   'I'
50 #define ION_IOC_ALLOC   _IOWR(ION_IOC_MAGIC, 0, struct ion_allocation_data)
51 #define ION_IOC_FREE    _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
52 #define ION_IOC_MAP     _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
53 #define ION_IOC_SHARE   _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
54 #define ION_IOC_IMPORT  _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
55 #define ION_IOC_CUSTOM  _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
56 #define ION_IOC_SYNC    _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
57
58 struct ion_msync_data {
59     long flags;
60     ion_buffer buf;
61     size_t size;
62     off_t offset;
63 };
64
65 enum ION_EXYNOS_CUSTOM_CMD {
66     ION_EXYNOS_CUSTOM_MSYNC
67 };
68
69 ion_client ion_client_create(void)
70 {
71     return open("/dev/ion", O_RDWR);
72 }
73
74 void ion_client_destroy(ion_client client)
75 {
76     close(client);
77 }
78
79 ion_buffer ion_alloc(ion_client client, size_t len, size_t align,
80                      unsigned int heap_mask, unsigned int flags)
81 {
82     int ret;
83     struct ion_handle_data arg_free;
84     struct ion_fd_data arg_share;
85     struct ion_allocation_data arg_alloc;
86
87     arg_alloc.len = len;
88     arg_alloc.align = align;
89     arg_alloc.heap_mask = heap_mask;
90     arg_alloc.flags = flags;
91
92     ret = ioctl(client, ION_IOC_ALLOC, &arg_alloc);
93     if (ret < 0)
94         return ret;
95
96     arg_share.handle = arg_alloc.handle;
97     ret = ioctl(client, ION_IOC_SHARE, &arg_share);
98
99     arg_free.handle = arg_alloc.handle;
100     ioctl(client, ION_IOC_FREE, &arg_free);
101
102     if (ret < 0)
103         return ret;
104
105     return arg_share.fd;
106 }
107
108 void ion_free(ion_buffer buffer)
109 {
110     close(buffer);
111 }
112
113 void *ion_map(ion_buffer buffer, size_t len, off_t offset)
114 {
115     return mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
116                 buffer, offset);
117 }
118
119 int ion_unmap(void *addr, size_t len)
120 {
121     return munmap(addr, len);
122 }
123
124 int ion_sync(ion_client client, ion_buffer buffer)
125 {
126     struct ion_fd_data data;
127
128     data.fd = buffer;
129
130     return ioctl(client, ION_IOC_SYNC, &data);
131 }