binder_module.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _BINDER_MODULE_H_
00014 #define _BINDER_MODULE_H_
00015 
00016 #include <support/TypeConstants.h>
00017 
00018 #if TARGET_HOST == TARGET_HOST_PALMOS
00019 #include <linux/types.h>
00020 #include <asm/ioctl.h>
00021 #endif
00022 
00023 #ifdef __cplusplus
00024 #if _SUPPORTS_NAMESPACE
00025 namespace palmos {
00026 namespace support {
00027 #endif
00028 #endif
00029 
00030 // These are pre-packed type constants for the object type codes.
00031 enum {
00032     kPackedLargeBinderType = B_PACK_LARGE_TYPE(B_BINDER_TYPE),
00033     kPackedLargeBinderWeakType = B_PACK_LARGE_TYPE(B_BINDER_WEAK_TYPE),
00034     kPackedLargeBinderHandleType = B_PACK_LARGE_TYPE(B_BINDER_HANDLE_TYPE),
00035     kPackedLargeBinderWeakHandleType = B_PACK_LARGE_TYPE(B_BINDER_WEAK_HANDLE_TYPE),
00036     kPackedLargeBinderNodeType = B_PACK_LARGE_TYPE(B_BINDER_NODE_TYPE),
00037     kPackedLargeBinderWeakNodeType = B_PACK_LARGE_TYPE(B_BINDER_WEAK_NODE_TYPE),
00038 };
00039 
00040 // Internal data structure used by driver.
00041 struct binder_node;
00042 
00043 // This is the flattened representation of a Binder object for transfer
00044 // between processes.  The 'offsets' supplied as part of a binder transaction
00045 // contains offsets into the data where these structures occur.  The Binder
00046 // driver takes care of re-writing the structure type and data as it moves
00047 // between processes.
00048 //
00049 // Note that this is very intentionally designed to be the same as a user-space
00050 // large_flat_data structure holding 8 bytes.  The IPC mechanism requires that
00051 // this structure be at least 8 bytes large.
00052 typedef struct flat_binder_object
00053 {
00054     // 8 bytes for large_flat_header.
00055     unsigned long       type;
00056     unsigned long       length;
00057     
00058     // 8 bytes of data.
00059     union {
00060         void*               binder;     // local object
00061         signed long         handle;     // remote object
00062         struct binder_node* node;       // driver node
00063     };
00064     void*               cookie;         // extra data associated with local object
00065 } flat_binder_object_t;
00066 
00067 /*
00068  * On 64-bit platforms where user code may run in 32-bits the driver must
00069  * translate the buffer (and local binder) addresses apropriately.
00070  */
00071 
00072 typedef struct binder_write_read {
00073     signed long     write_size;     // bytes to write
00074     signed long     write_consumed; // bytes consumed by driver (for ERESTARTSYS)
00075     unsigned long   write_buffer;
00076     signed long     read_size;      // bytes to read
00077     signed long     read_consumed;  // bytes consumed by driver (for ERESTARTSYS)
00078     unsigned long   read_buffer;
00079 } binder_write_read_t;
00080 
00081 // Use with BINDER_VERSION, driver fills in fields.
00082 typedef struct binder_version {
00083     signed long     protocol_version;   // driver protocol version -- increment with incompatible change
00084 } binder_version_t;
00085 
00086 // This is the current protocol version.
00087 #define BINDER_CURRENT_PROTOCOL_VERSION 4
00088 
00089 #define BINDER_IOC_MAGIC 'b'
00090 #define BINDER_WRITE_READ _IOWR(BINDER_IOC_MAGIC, 1, binder_write_read_t)
00091 #define BINDER_SET_WAKEUP_TIME  _IOW(BINDER_IOC_MAGIC, 2, binder_wakeup_time_t)
00092 #define BINDER_SET_IDLE_TIMEOUT _IOW(BINDER_IOC_MAGIC, 3, bigtime_t)
00093 #define BINDER_SET_REPLY_TIMEOUT    _IOW(BINDER_IOC_MAGIC, 4, bigtime_t)
00094 #define BINDER_SET_MAX_THREADS  _IOW(BINDER_IOC_MAGIC, 5, size_t)
00095 #define BINDER_SET_IDLE_PRIORITY    _IOW(BINDER_IOC_MAGIC, 6, int)
00096 #define BINDER_SET_CONTEXT_MGR  _IOW(BINDER_IOC_MAGIC, 7, int)
00097 #define BINDER_THREAD_EXIT  _IOW(BINDER_IOC_MAGIC, 8, int)
00098 #define BINDER_VERSION _IOWR(BINDER_IOC_MAGIC, 9, binder_version_t)
00099 #define BINDER_IOC_MAXNR 9
00100 
00101 // NOTE: Two special error codes you should check for when calling
00102 // in to the driver are:
00103 //
00104 // EINTR -- The operation has been interupted.  This should be
00105 // handled by retrying the ioctl() until a different error code
00106 // is returned.
00107 //
00108 // ECONNREFUSED -- The driver is no longer accepting operations
00109 // from your process.  That is, the process is being destroyed.
00110 // You should handle this by exiting from your process.  Note
00111 // that once this error code is returned, all further calls to
00112 // the driver from any thread will return this same code.
00113 
00114 typedef int64_t bigtime_t;
00115 
00116 enum transaction_flags {
00117     tfInline = 0x01,            // not yet implemented
00118     tfSynchronous = 0x02,       // obsolete
00119     tfRootObject = 0x04,        // contents are the component's root object
00120     tfStatusCode = 0x08         // contents are a 32-bit status code
00121 };
00122 
00123 typedef struct binder_transaction_data
00124 {
00125     // The first two are only used for bcTRANSACTION and brTRANSACTION,
00126     // identifying the target and contents of the transaction.
00127     union {
00128         unsigned long   handle;     // target descriptor of command transaction
00129         void            *ptr;       // target descriptor of return transaction
00130     } target;
00131     unsigned int    code;           // transaction command
00132     
00133     // General information about the transaction.
00134     unsigned int    flags;
00135     int priority;                   // requested/current thread priority
00136     size_t  data_size;              // number of bytes of data
00137     size_t  offsets_size;           // number of bytes of flat_binder_object offsets
00138     
00139     // If this transaction is inline, the data immediately
00140     // follows here; otherwise, it ends with a pointer to
00141     // the data buffer.
00142     union {
00143         struct {
00144             const void  *buffer;    // transaction data
00145             const void  *offsets;   // offsets to flat_binder_object structs
00146         } ptr;
00147         uint8_t buf[8];
00148     } data;
00149 } binder_transaction_data_t;
00150 
00151 typedef struct binder_wakeup_time
00152 {
00153     bigtime_t time;
00154     int priority;
00155 } binder_wakeup_time_t;
00156 
00157 enum BinderDriverReturnProtocol {
00158     brERROR = -1,
00159     /*
00160         int: error code
00161     */
00162     
00163     brOK = 0,
00164     brTIMEOUT,
00165     brWAKEUP,
00166     /*  No parameters! */
00167     
00168     brTRANSACTION,
00169     brREPLY,
00170     /*
00171         binder_transaction_data: the received command.
00172     */
00173     
00174     brACQUIRE_RESULT,
00175     /*
00176         int: 0 if the last bcATTEMPT_ACQUIRE was not successful.
00177         Else the remote object has acquired a primary reference.
00178     */
00179     
00180     brDEAD_REPLY,
00181     /*
00182         The target of the last transaction (either a bcTRANSACTION or
00183         a bcATTEMPT_ACQUIRE) is no longer with us.  No parameters.
00184     */
00185     
00186     brTRANSACTION_COMPLETE,
00187     /*
00188         No parameters... always refers to the last transaction requested
00189         (including replies).  Note that this will be sent even for asynchronous
00190         transactions.
00191     */
00192     
00193     brINCREFS,
00194     brACQUIRE,
00195     brRELEASE,
00196     brDECREFS,
00197     /*
00198         void *: ptr to binder
00199         void *: cookie for binder
00200     */
00201     
00202     brATTEMPT_ACQUIRE,
00203     /*
00204         int:    priority
00205         void *: ptr to binder
00206         void *: cookie for binder
00207     */
00208     
00209     brEVENT_OCCURRED,
00210     /*
00211         This is returned when the bcSET_NEXT_EVENT_TIME has elapsed.
00212         At this point the next event time is set to B_INFINITE_TIMEOUT,
00213         so you must send another bcSET_NEXT_EVENT_TIME command if you
00214         have another event pending.
00215     */
00216 
00217     brNOOP,
00218     /*
00219      * No parameters.  Do nothing and examine the next command.  It exists
00220      * primarily so that we can replace it with a brSPAWN_LOOPER command.
00221      */
00222 
00223     brSPAWN_LOOPER,
00224     /*
00225      * No parameters.  The driver has determined that a process has no threads
00226      * waiting to service incomming transactions.  When a process receives this
00227      * command, it must spawn a new service thread and register it via
00228      * bcENTER_LOOPER.
00229      */
00230 
00231     brFINISHED
00232 };
00233 
00234 enum BinderDriverCommandProtocol {
00235     bcNOOP = 0,
00236     /*  No parameters! */
00237 
00238     bcTRANSACTION,
00239     bcREPLY,
00240     /*
00241         binder_transaction_data: the sent command.
00242     */
00243     
00244     bcACQUIRE_RESULT,
00245     /*
00246         int:  0 if the last brATTEMPT_ACQUIRE was not successful.
00247         Else you have acquired a primary reference on the object.
00248     */
00249     
00250     bcFREE_BUFFER,
00251     /*
00252         void *: ptr to transaction data received on a read
00253     */
00254     
00255     bcTRANSACTION_COMPLETE,
00256     /*
00257         No parameters... send when finishing an asynchronous transaction.
00258     */
00259     
00260     bcINCREFS,
00261     bcACQUIRE,
00262     bcRELEASE,
00263     bcDECREFS,
00264     /*
00265         int:    descriptor
00266     */
00267     
00268     bcINCREFS_DONE,
00269     bcACQUIRE_DONE,
00270     /*
00271         void *: ptr to binder
00272         void *: cookie for binder
00273     */
00274     
00275     bcATTEMPT_ACQUIRE,
00276     /*
00277         int:    priority
00278         int:    descriptor
00279     */
00280     
00281     bcRETRIEVE_ROOT_OBJECT,
00282     /*
00283         int:    process ID
00284     */
00285     
00286     bcSET_THREAD_ENTRY,
00287     /*
00288         void *: thread entry function for new threads created to handle tasks
00289         void *: argument passed to those threads
00290     */
00291     
00292     bcREGISTER_LOOPER,
00293     /*
00294         No parameters.
00295         Register a spawned looper thread with the device.  This must be
00296         called by the function that is supplied in bcSET_THREAD_ENTRY as
00297         part of its initialization with the binder.
00298     */
00299     
00300     bcENTER_LOOPER,
00301     bcEXIT_LOOPER,
00302     /*
00303         No parameters.
00304         These two commands are sent as an application-level thread
00305         enters and exits the binder loop, respectively.  They are
00306         used so the binder can have an accurate count of the number
00307         of looping threads it has available.
00308     */
00309     
00310     bcSYNC,
00311     /*
00312         No parameters.
00313         Upon receiving this command, the driver waits until all
00314         pending asynchronous transactions have completed.
00315     */
00316     
00317 #if 0
00318     bcCATCH_ROOT_OBJECTS,
00319     /*
00320         No parameters.
00321         Call this to have your team start catching root objects
00322         published by other teams that are spawned outside of the binder.
00323         When this happens, you will receive a brTRANSACTION with the
00324         tfRootObject flag set.  (Note that this is distinct from receiving
00325         normal root objects, which are a brREPLY.)
00326     */
00327 #endif
00328     
00329     bcSTOP_PROCESS,
00330     /*
00331         int: descriptor of process's root object
00332         int: 1 to stop immediately, 0 when root object is released
00333     */
00334 
00335     bcSTOP_SELF
00336     /*
00337         int: 1 to stop immediately, 0 when root object is released
00338     */
00339 };
00340 
00341 #if 0
00342 /*  Parameters for BINDER_READ_WRITE ioctl. */
00343 #if BINDER_DEBUG_LIB
00344 
00345 struct binder_write_read
00346 {
00347     ssize_t     write_size;
00348     const void* write_buffer;
00349     ssize_t     read_size;
00350     void*       read_buffer;
00351 };
00352 
00353 
00354 /*  Below are calls to access the binder when debugging the driver from
00355     user space by compiling it as libbinderdbg and linking libbe2 with it. */
00356 
00357 extern int          open_binder(int teamID=0);
00358 extern status_t     close_binder(int desc);
00359 extern status_t     ioctl_binder(int desc, int cmd, void *data, int len);
00360 extern ssize_t      read_binder(int desc, void *data, size_t numBytes);
00361 extern ssize_t      write_binder(int desc, void *data, size_t numBytes);
00362 
00363 #else
00364 
00365 #include <unistd.h>
00366 inline int          open_binder(int ) { return open("/dev/misc/binder2",O_RDWR|O_CLOEXEC); };
00367 inline status_t     close_binder(int desc) { return close(desc); };
00368 inline status_t     ioctl_binder(int desc, int cmd, void *data, int len) { return ioctl(desc,cmd,data,len); };
00369 inline ssize_t      read_binder(int desc, void *data, size_t numBytes) { return read(desc,data,numBytes); };
00370 inline ssize_t      write_binder(int desc, void *data, size_t numBytes) { return write(desc,data,numBytes); };
00371 
00372 #endif
00373 #endif
00374 
00375 #ifdef __cplusplus
00376 #if _SUPPORTS_NAMESPACE
00377 } } // namespace palmos::support
00378 #endif
00379 #endif
00380 
00381 #endif // _BINDER_MODULE_H_