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