xref: /linux/include/uapi/linux/android/binder.h (revision 4949009eb8d40a441dcddcd96e101e77d31cf1b2)
1 /*
2  * Copyright (C) 2008 Google, Inc.
3  *
4  * Based on, but no longer compatible with, the original
5  * OpenBinder.org binder driver interface, which is:
6  *
7  * Copyright (c) 2005 Palmsource, Inc.
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef _UAPI_LINUX_BINDER_H
21 #define _UAPI_LINUX_BINDER_H
22 
23 #include <linux/types.h>
24 #include <linux/ioctl.h>
25 
26 #define B_PACK_CHARS(c1, c2, c3, c4) \
27 	((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
28 #define B_TYPE_LARGE 0x85
29 
30 enum {
31 	BINDER_TYPE_BINDER	= B_PACK_CHARS('s', 'b', '*', B_TYPE_LARGE),
32 	BINDER_TYPE_WEAK_BINDER	= B_PACK_CHARS('w', 'b', '*', B_TYPE_LARGE),
33 	BINDER_TYPE_HANDLE	= B_PACK_CHARS('s', 'h', '*', B_TYPE_LARGE),
34 	BINDER_TYPE_WEAK_HANDLE	= B_PACK_CHARS('w', 'h', '*', B_TYPE_LARGE),
35 	BINDER_TYPE_FD		= B_PACK_CHARS('f', 'd', '*', B_TYPE_LARGE),
36 };
37 
38 enum {
39 	FLAT_BINDER_FLAG_PRIORITY_MASK = 0xff,
40 	FLAT_BINDER_FLAG_ACCEPTS_FDS = 0x100,
41 };
42 
43 #ifdef BINDER_IPC_32BIT
44 typedef __u32 binder_size_t;
45 typedef __u32 binder_uintptr_t;
46 #else
47 typedef __u64 binder_size_t;
48 typedef __u64 binder_uintptr_t;
49 #endif
50 
51 /*
52  * This is the flattened representation of a Binder object for transfer
53  * between processes.  The 'offsets' supplied as part of a binder transaction
54  * contains offsets into the data where these structures occur.  The Binder
55  * driver takes care of re-writing the structure type and data as it moves
56  * between processes.
57  */
58 struct flat_binder_object {
59 	/* 8 bytes for large_flat_header. */
60 	__u32		type;
61 	__u32		flags;
62 
63 	/* 8 bytes of data. */
64 	union {
65 		binder_uintptr_t	binder;	/* local object */
66 		__u32			handle;	/* remote object */
67 	};
68 
69 	/* extra data associated with local object */
70 	binder_uintptr_t	cookie;
71 };
72 
73 /*
74  * On 64-bit platforms where user code may run in 32-bits the driver must
75  * translate the buffer (and local binder) addresses appropriately.
76  */
77 
78 struct binder_write_read {
79 	binder_size_t		write_size;	/* bytes to write */
80 	binder_size_t		write_consumed;	/* bytes consumed by driver */
81 	binder_uintptr_t	write_buffer;
82 	binder_size_t		read_size;	/* bytes to read */
83 	binder_size_t		read_consumed;	/* bytes consumed by driver */
84 	binder_uintptr_t	read_buffer;
85 };
86 
87 /* Use with BINDER_VERSION, driver fills in fields. */
88 struct binder_version {
89 	/* driver protocol version -- increment with incompatible change */
90 	__s32       protocol_version;
91 };
92 
93 /* This is the current protocol version. */
94 #ifdef BINDER_IPC_32BIT
95 #define BINDER_CURRENT_PROTOCOL_VERSION 7
96 #else
97 #define BINDER_CURRENT_PROTOCOL_VERSION 8
98 #endif
99 
100 #define BINDER_WRITE_READ		_IOWR('b', 1, struct binder_write_read)
101 #define BINDER_SET_IDLE_TIMEOUT		_IOW('b', 3, __s64)
102 #define BINDER_SET_MAX_THREADS		_IOW('b', 5, __u32)
103 #define BINDER_SET_IDLE_PRIORITY	_IOW('b', 6, __s32)
104 #define BINDER_SET_CONTEXT_MGR		_IOW('b', 7, __s32)
105 #define BINDER_THREAD_EXIT		_IOW('b', 8, __s32)
106 #define BINDER_VERSION			_IOWR('b', 9, struct binder_version)
107 
108 /*
109  * NOTE: Two special error codes you should check for when calling
110  * in to the driver are:
111  *
112  * EINTR -- The operation has been interupted.  This should be
113  * handled by retrying the ioctl() until a different error code
114  * is returned.
115  *
116  * ECONNREFUSED -- The driver is no longer accepting operations
117  * from your process.  That is, the process is being destroyed.
118  * You should handle this by exiting from your process.  Note
119  * that once this error code is returned, all further calls to
120  * the driver from any thread will return this same code.
121  */
122 
123 enum transaction_flags {
124 	TF_ONE_WAY	= 0x01,	/* this is a one-way call: async, no return */
125 	TF_ROOT_OBJECT	= 0x04,	/* contents are the component's root object */
126 	TF_STATUS_CODE	= 0x08,	/* contents are a 32-bit status code */
127 	TF_ACCEPT_FDS	= 0x10,	/* allow replies with file descriptors */
128 };
129 
130 struct binder_transaction_data {
131 	/* The first two are only used for bcTRANSACTION and brTRANSACTION,
132 	 * identifying the target and contents of the transaction.
133 	 */
134 	union {
135 		/* target descriptor of command transaction */
136 		__u32	handle;
137 		/* target descriptor of return transaction */
138 		binder_uintptr_t ptr;
139 	} target;
140 	binder_uintptr_t	cookie;	/* target object cookie */
141 	__u32		code;		/* transaction command */
142 
143 	/* General information about the transaction. */
144 	__u32	        flags;
145 	pid_t		sender_pid;
146 	uid_t		sender_euid;
147 	binder_size_t	data_size;	/* number of bytes of data */
148 	binder_size_t	offsets_size;	/* number of bytes of offsets */
149 
150 	/* If this transaction is inline, the data immediately
151 	 * follows here; otherwise, it ends with a pointer to
152 	 * the data buffer.
153 	 */
154 	union {
155 		struct {
156 			/* transaction data */
157 			binder_uintptr_t	buffer;
158 			/* offsets from buffer to flat_binder_object structs */
159 			binder_uintptr_t	offsets;
160 		} ptr;
161 		__u8	buf[8];
162 	} data;
163 };
164 
165 struct binder_ptr_cookie {
166 	binder_uintptr_t ptr;
167 	binder_uintptr_t cookie;
168 };
169 
170 struct binder_handle_cookie {
171 	__u32 handle;
172 	binder_uintptr_t cookie;
173 } __packed;
174 
175 struct binder_pri_desc {
176 	__s32 priority;
177 	__u32 desc;
178 };
179 
180 struct binder_pri_ptr_cookie {
181 	__s32 priority;
182 	binder_uintptr_t ptr;
183 	binder_uintptr_t cookie;
184 };
185 
186 enum binder_driver_return_protocol {
187 	BR_ERROR = _IOR('r', 0, __s32),
188 	/*
189 	 * int: error code
190 	 */
191 
192 	BR_OK = _IO('r', 1),
193 	/* No parameters! */
194 
195 	BR_TRANSACTION = _IOR('r', 2, struct binder_transaction_data),
196 	BR_REPLY = _IOR('r', 3, struct binder_transaction_data),
197 	/*
198 	 * binder_transaction_data: the received command.
199 	 */
200 
201 	BR_ACQUIRE_RESULT = _IOR('r', 4, __s32),
202 	/*
203 	 * not currently supported
204 	 * int: 0 if the last bcATTEMPT_ACQUIRE was not successful.
205 	 * Else the remote object has acquired a primary reference.
206 	 */
207 
208 	BR_DEAD_REPLY = _IO('r', 5),
209 	/*
210 	 * The target of the last transaction (either a bcTRANSACTION or
211 	 * a bcATTEMPT_ACQUIRE) is no longer with us.  No parameters.
212 	 */
213 
214 	BR_TRANSACTION_COMPLETE = _IO('r', 6),
215 	/*
216 	 * No parameters... always refers to the last transaction requested
217 	 * (including replies).  Note that this will be sent even for
218 	 * asynchronous transactions.
219 	 */
220 
221 	BR_INCREFS = _IOR('r', 7, struct binder_ptr_cookie),
222 	BR_ACQUIRE = _IOR('r', 8, struct binder_ptr_cookie),
223 	BR_RELEASE = _IOR('r', 9, struct binder_ptr_cookie),
224 	BR_DECREFS = _IOR('r', 10, struct binder_ptr_cookie),
225 	/*
226 	 * void *:	ptr to binder
227 	 * void *: cookie for binder
228 	 */
229 
230 	BR_ATTEMPT_ACQUIRE = _IOR('r', 11, struct binder_pri_ptr_cookie),
231 	/*
232 	 * not currently supported
233 	 * int:	priority
234 	 * void *: ptr to binder
235 	 * void *: cookie for binder
236 	 */
237 
238 	BR_NOOP = _IO('r', 12),
239 	/*
240 	 * No parameters.  Do nothing and examine the next command.  It exists
241 	 * primarily so that we can replace it with a BR_SPAWN_LOOPER command.
242 	 */
243 
244 	BR_SPAWN_LOOPER = _IO('r', 13),
245 	/*
246 	 * No parameters.  The driver has determined that a process has no
247 	 * threads waiting to service incoming transactions.  When a process
248 	 * receives this command, it must spawn a new service thread and
249 	 * register it via bcENTER_LOOPER.
250 	 */
251 
252 	BR_FINISHED = _IO('r', 14),
253 	/*
254 	 * not currently supported
255 	 * stop threadpool thread
256 	 */
257 
258 	BR_DEAD_BINDER = _IOR('r', 15, binder_uintptr_t),
259 	/*
260 	 * void *: cookie
261 	 */
262 	BR_CLEAR_DEATH_NOTIFICATION_DONE = _IOR('r', 16, binder_uintptr_t),
263 	/*
264 	 * void *: cookie
265 	 */
266 
267 	BR_FAILED_REPLY = _IO('r', 17),
268 	/*
269 	 * The the last transaction (either a bcTRANSACTION or
270 	 * a bcATTEMPT_ACQUIRE) failed (e.g. out of memory).  No parameters.
271 	 */
272 };
273 
274 enum binder_driver_command_protocol {
275 	BC_TRANSACTION = _IOW('c', 0, struct binder_transaction_data),
276 	BC_REPLY = _IOW('c', 1, struct binder_transaction_data),
277 	/*
278 	 * binder_transaction_data: the sent command.
279 	 */
280 
281 	BC_ACQUIRE_RESULT = _IOW('c', 2, __s32),
282 	/*
283 	 * not currently supported
284 	 * int:  0 if the last BR_ATTEMPT_ACQUIRE was not successful.
285 	 * Else you have acquired a primary reference on the object.
286 	 */
287 
288 	BC_FREE_BUFFER = _IOW('c', 3, binder_uintptr_t),
289 	/*
290 	 * void *: ptr to transaction data received on a read
291 	 */
292 
293 	BC_INCREFS = _IOW('c', 4, __u32),
294 	BC_ACQUIRE = _IOW('c', 5, __u32),
295 	BC_RELEASE = _IOW('c', 6, __u32),
296 	BC_DECREFS = _IOW('c', 7, __u32),
297 	/*
298 	 * int:	descriptor
299 	 */
300 
301 	BC_INCREFS_DONE = _IOW('c', 8, struct binder_ptr_cookie),
302 	BC_ACQUIRE_DONE = _IOW('c', 9, struct binder_ptr_cookie),
303 	/*
304 	 * void *: ptr to binder
305 	 * void *: cookie for binder
306 	 */
307 
308 	BC_ATTEMPT_ACQUIRE = _IOW('c', 10, struct binder_pri_desc),
309 	/*
310 	 * not currently supported
311 	 * int: priority
312 	 * int: descriptor
313 	 */
314 
315 	BC_REGISTER_LOOPER = _IO('c', 11),
316 	/*
317 	 * No parameters.
318 	 * Register a spawned looper thread with the device.
319 	 */
320 
321 	BC_ENTER_LOOPER = _IO('c', 12),
322 	BC_EXIT_LOOPER = _IO('c', 13),
323 	/*
324 	 * No parameters.
325 	 * These two commands are sent as an application-level thread
326 	 * enters and exits the binder loop, respectively.  They are
327 	 * used so the binder can have an accurate count of the number
328 	 * of looping threads it has available.
329 	 */
330 
331 	BC_REQUEST_DEATH_NOTIFICATION = _IOW('c', 14,
332 						struct binder_handle_cookie),
333 	/*
334 	 * int: handle
335 	 * void *: cookie
336 	 */
337 
338 	BC_CLEAR_DEATH_NOTIFICATION = _IOW('c', 15,
339 						struct binder_handle_cookie),
340 	/*
341 	 * int: handle
342 	 * void *: cookie
343 	 */
344 
345 	BC_DEAD_BINDER_DONE = _IOW('c', 16, binder_uintptr_t),
346 	/*
347 	 * void *: cookie
348 	 */
349 };
350 
351 #endif /* _UAPI_LINUX_BINDER_H */
352 
353