ion.c revision afd912394690cbf7d40b021c62d80ff0f3d40806
1/*
2 *  ion.c
3 *
4 * Memory Allocator functions for ion
5 *
6 *   Copyright 2011 Google, Inc
7 *
8 *  Licensed under the Apache License, Version 2.0 (the "License");
9 *  you may not use this file except in compliance with the License.
10 *  You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 *  Unless required by applicable law or agreed to in writing, software
15 *  distributed under the License is distributed on an "AS IS" BASIS,
16 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 *  See the License for the specific language governing permissions and
18 *  limitations under the License.
19 */
20#define LOG_TAG "ion"
21
22#include <cutils/log.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <sys/ioctl.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29
30#include <linux/ion.h>
31#include <ion/ion.h>
32
33int ion_open()
34{
35        int fd = open("/dev/ion", O_RDWR);
36        if (fd < 0)
37                ALOGE("open /dev/ion failed!\n");
38        return fd;
39}
40
41int ion_close(int fd)
42{
43        return close(fd);
44}
45
46static int ion_ioctl(int fd, int req, void *arg)
47{
48        int ret = ioctl(fd, req, arg);
49        if (ret < 0) {
50                ALOGE("ioctl %x failed with code %d: %s\n", req,
51                       ret, strerror(errno));
52                return -errno;
53        }
54        return ret;
55}
56
57int ion_alloc(int fd, size_t len, size_t align, unsigned int flags,
58              struct ion_handle **handle)
59{
60        int ret;
61        struct ion_allocation_data data = {
62                .len = len,
63                .align = align,
64                .flags = flags,
65        };
66
67        ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
68        if (ret < 0)
69                return ret;
70        *handle = data.handle;
71        return ret;
72}
73
74int ion_free(int fd, struct ion_handle *handle)
75{
76        struct ion_handle_data data = {
77                .handle = handle,
78        };
79        return ion_ioctl(fd, ION_IOC_FREE, &data);
80}
81
82int ion_map(int fd, struct ion_handle *handle, size_t length, int prot,
83            int flags, off_t offset, unsigned char **ptr, int *map_fd)
84{
85        struct ion_fd_data data = {
86                .handle = handle,
87        };
88
89        int ret = ion_ioctl(fd, ION_IOC_MAP, &data);
90        if (ret < 0)
91                return ret;
92        *map_fd = data.fd;
93        if (*map_fd < 0) {
94                ALOGE("map ioctl returned negative fd\n");
95                return -EINVAL;
96        }
97        *ptr = mmap(NULL, length, prot, flags, *map_fd, offset);
98        if (*ptr == MAP_FAILED) {
99                ALOGE("mmap failed: %s\n", strerror(errno));
100                return -errno;
101        }
102        return ret;
103}
104
105int ion_share(int fd, struct ion_handle *handle, int *share_fd)
106{
107        int map_fd;
108        struct ion_fd_data data = {
109                .handle = handle,
110        };
111
112        int ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
113        if (ret < 0)
114                return ret;
115        *share_fd = data.fd;
116        if (*share_fd < 0) {
117                ALOGE("share ioctl returned negative fd\n");
118                return -EINVAL;
119        }
120        return ret;
121}
122
123int ion_import(int fd, int share_fd, struct ion_handle **handle)
124{
125        struct ion_fd_data data = {
126                .fd = share_fd,
127        };
128
129        int ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
130        if (ret < 0)
131                return ret;
132        *handle = data.handle;
133        return ret;
134}
135