1 /*
2  *  sync.c
3  *
4  *   Copyright 2012 Google, Inc
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 #include <fcntl.h>
20 #include <malloc.h>
21 #include <stdint.h>
22 #include <string.h>
23 
24 #include <linux/sync.h>
25 #include <linux/sw_sync.h>
26 
27 #include <sys/ioctl.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 
sync_wait(int fd,int timeout)31 int sync_wait(int fd, int timeout)
32 {
33     __s32 to = timeout;
34 
35     return ioctl(fd, SYNC_IOC_WAIT, &to);
36 }
37 
sync_merge(const char * name,int fd1,int fd2)38 int sync_merge(const char *name, int fd1, int fd2)
39 {
40     struct sync_merge_data data;
41     int err;
42 
43     data.fd2 = fd2;
44     strlcpy(data.name, name, sizeof(data.name));
45 
46     err = ioctl(fd1, SYNC_IOC_MERGE, &data);
47     if (err < 0)
48         return err;
49 
50     return data.fence;
51 }
52 
sync_fence_info(int fd)53 struct sync_fence_info_data *sync_fence_info(int fd)
54 {
55     struct sync_fence_info_data *info;
56     int err;
57 
58     info = malloc(4096);
59     if (info == NULL)
60         return NULL;
61 
62     info->len = 4096;
63     err = ioctl(fd, SYNC_IOC_FENCE_INFO, info);
64     if (err < 0) {
65         free(info);
66         return NULL;
67     }
68 
69     return info;
70 }
71 
sync_pt_info(struct sync_fence_info_data * info,struct sync_pt_info * itr)72 struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
73                                   struct sync_pt_info *itr)
74 {
75     if (itr == NULL)
76         itr = (struct sync_pt_info *) info->pt_info;
77     else
78         itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len);
79 
80     if ((__u8 *)itr - (__u8 *)info >= (int)info->len)
81         return NULL;
82 
83     return itr;
84 }
85 
sync_fence_info_free(struct sync_fence_info_data * info)86 void sync_fence_info_free(struct sync_fence_info_data *info)
87 {
88     free(info);
89 }
90 
91 
sw_sync_timeline_create(void)92 int sw_sync_timeline_create(void)
93 {
94     return open("/dev/sw_sync", O_RDWR);
95 }
96 
sw_sync_timeline_inc(int fd,unsigned count)97 int sw_sync_timeline_inc(int fd, unsigned count)
98 {
99     __u32 arg = count;
100 
101     return ioctl(fd, SW_SYNC_IOC_INC, &arg);
102 }
103 
sw_sync_fence_create(int fd,const char * name,unsigned value)104 int sw_sync_fence_create(int fd, const char *name, unsigned value)
105 {
106     struct sw_sync_create_fence_data data;
107     int err;
108 
109     data.value = value;
110     strlcpy(data.name, name, sizeof(data.name));
111 
112     err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
113     if (err < 0)
114         return err;
115 
116     return data.fence;
117 }
118