Index: uspace/lib/c/generic/async.c
===================================================================
--- uspace/lib/c/generic/async.c	(revision a35b458e9db1ca95e679799dc7c1b12c83359ca3)
+++ uspace/lib/c/generic/async.c	(revision eadaeae8dacc68cf558028f23cc96b90a08b7361)
@@ -133,5 +133,5 @@
 
 	/** Session identification */
-	int phone;
+	cap_phone_handle_t phone;
 
 	/** First clone connection argument */
@@ -169,5 +169,5 @@
 
 	/** Exchange identification */
-	int phone;
+	cap_phone_handle_t phone;
 };
 
@@ -185,5 +185,5 @@
 	link_t link;
 
-	cap_handle_t chandle;
+	cap_call_handle_t chandle;
 	ipc_call_t call;
 } msg_t;
@@ -237,5 +237,5 @@
 
 	/** Identification of the opening call. */
-	cap_handle_t chandle;
+	cap_call_handle_t chandle;
 
 	/** Call data of the opening call. */
@@ -243,5 +243,5 @@
 
 	/** Identification of the closing call. */
-	cap_handle_t close_chandle;
+	cap_call_handle_t close_chandle;
 
 	/** Fibril function that will be used to handle the connection. */
@@ -382,5 +382,5 @@
  *
  */
-static void default_fallback_port_handler(cap_handle_t chandle,
+static void default_fallback_port_handler(cap_call_handle_t chandle,
     ipc_call_t *call, void *arg)
 {
@@ -786,5 +786,5 @@
  */
 static fid_t async_new_connection(task_id_t in_task_id, sysarg_t in_phone_hash,
-    cap_handle_t chandle, ipc_call_t *call, async_port_handler_t handler,
+    cap_call_handle_t chandle, ipc_call_t *call, async_port_handler_t handler,
     void *data)
 {
@@ -968,5 +968,5 @@
  *
  */
-static bool route_call(cap_handle_t chandle, ipc_call_t *call)
+static bool route_call(cap_call_handle_t chandle, ipc_call_t *call)
 {
 	assert(call);
@@ -1057,5 +1057,5 @@
  */
 errno_t async_irq_subscribe(int inr, async_notification_handler_t handler,
-    void *data, const irq_code_t *ucode, cap_handle_t *handle)
+    void *data, const irq_code_t *ucode, cap_irq_handle_t *handle)
 {
 	notification_t *notification =
@@ -1077,8 +1077,8 @@
 	futex_up(&async_futex);
 
-	cap_handle_t cap;
-	errno_t rc = ipc_irq_subscribe(inr, imethod, ucode, &cap);
+	cap_irq_handle_t ihandle;
+	errno_t rc = ipc_irq_subscribe(inr, imethod, ucode, &ihandle);
 	if (rc == EOK && handle != NULL) {
-		*handle = cap;
+		*handle = ihandle;
 	}
 	return rc;
@@ -1087,15 +1087,15 @@
 /** Unsubscribe from IRQ notification.
  *
- * @param cap     IRQ capability handle.
+ * @param handle  IRQ capability handle.
  *
  * @return Zero on success or an error code.
  *
  */
-errno_t async_irq_unsubscribe(int cap)
+errno_t async_irq_unsubscribe(cap_irq_handle_t ihandle)
 {
 	// TODO: Remove entry from hash table
 	//       to avoid memory leak
 
-	return ipc_irq_unsubscribe(cap);
+	return ipc_irq_unsubscribe(ihandle);
 }
 
@@ -1200,5 +1200,5 @@
  *          message. In that case zero CAP_NIL is returned.
  */
-cap_handle_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
+cap_call_handle_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
 {
 	assert(call);
@@ -1267,5 +1267,5 @@
 	list_remove(&msg->link);
 
-	cap_handle_t chandle = msg->chandle;
+	cap_call_handle_t chandle = msg->chandle;
 	*call = msg->call;
 	free(msg);
@@ -1339,5 +1339,5 @@
  *
  */
-static void handle_call(cap_handle_t chandle, ipc_call_t *call)
+static void handle_call(cap_call_handle_t chandle, ipc_call_t *call)
 {
 	assert(call);
@@ -2042,15 +2042,15 @@
 }
 
-errno_t async_answer_0(cap_handle_t chandle, errno_t retval)
+errno_t async_answer_0(cap_call_handle_t chandle, errno_t retval)
 {
 	return ipc_answer_0(chandle, retval);
 }
 
-errno_t async_answer_1(cap_handle_t chandle, errno_t retval, sysarg_t arg1)
+errno_t async_answer_1(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1)
 {
 	return ipc_answer_1(chandle, retval, arg1);
 }
 
-errno_t async_answer_2(cap_handle_t chandle, errno_t retval, sysarg_t arg1,
+errno_t async_answer_2(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
     sysarg_t arg2)
 {
@@ -2058,5 +2058,5 @@
 }
 
-errno_t async_answer_3(cap_handle_t chandle, errno_t retval, sysarg_t arg1,
+errno_t async_answer_3(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
     sysarg_t arg2, sysarg_t arg3)
 {
@@ -2064,5 +2064,5 @@
 }
 
-errno_t async_answer_4(cap_handle_t chandle, errno_t retval, sysarg_t arg1,
+errno_t async_answer_4(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
     sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
 {
@@ -2070,5 +2070,5 @@
 }
 
-errno_t async_answer_5(cap_handle_t chandle, errno_t retval, sysarg_t arg1,
+errno_t async_answer_5(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
     sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
 {
@@ -2076,5 +2076,5 @@
 }
 
-errno_t async_forward_fast(cap_handle_t chandle, async_exch_t *exch,
+errno_t async_forward_fast(cap_call_handle_t chandle, async_exch_t *exch,
     sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
 {
@@ -2085,5 +2085,5 @@
 }
 
-errno_t async_forward_slow(cap_handle_t chandle, async_exch_t *exch,
+errno_t async_forward_slow(cap_call_handle_t chandle, async_exch_t *exch,
     sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
     sysarg_t arg4, sysarg_t arg5, unsigned int mode)
@@ -2126,6 +2126,7 @@
 }
 
-static errno_t async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
-    sysarg_t arg3, sysarg_t arg4, int *out_phone)
+static errno_t async_connect_me_to_internal(cap_phone_handle_t phone,
+    sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
+    cap_phone_handle_t *out_phone)
 {
 	ipc_call_t result;
@@ -2133,5 +2134,5 @@
 	// XXX: Workaround for GCC's inability to infer association between
 	// rc == EOK and *out_phone being assigned.
-	*out_phone = -1;
+	*out_phone = CAP_NIL;
 
 	amsg_t *msg = amsg_create();
@@ -2151,5 +2152,5 @@
 		return rc;
 
-	*out_phone = (int) IPC_GET_ARG5(result);
+	*out_phone = (cap_phone_handle_t) IPC_GET_ARG5(result);
 	return EOK;
 }
@@ -2182,5 +2183,5 @@
 	}
 
-	int phone;
+	cap_phone_handle_t phone;
 	errno_t rc = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
 	    0, &phone);
@@ -2235,5 +2236,5 @@
 	}
 
-	int phone;
+	cap_phone_handle_t phone;
 	errno_t rc = async_connect_me_to_internal(exch->phone, iface, arg2,
 	    arg3, 0, &phone);
@@ -2306,5 +2307,5 @@
 	}
 
-	int phone;
+	cap_phone_handle_t phone;
 	errno_t rc = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
 	    IPC_FLAG_BLOCKING, &phone);
@@ -2360,5 +2361,5 @@
 	}
 
-	int phone;
+	cap_phone_handle_t phone;
 	errno_t rc = async_connect_me_to_internal(exch->phone, iface, arg2,
 	    arg3, IPC_FLAG_BLOCKING, &phone);
@@ -2396,5 +2397,5 @@
 	}
 
-	cap_handle_t phone;
+	cap_phone_handle_t phone;
 	errno_t rc = ipc_connect_kbox(id, &phone);
 	if (rc != EOK) {
@@ -2421,5 +2422,5 @@
 }
 
-static errno_t async_hangup_internal(int phone)
+static errno_t async_hangup_internal(cap_phone_handle_t phone)
 {
 	return ipc_hangup(phone);
@@ -2515,5 +2516,5 @@
 			}
 		} else if (mgmt == EXCHANGE_PARALLEL) {
-			int phone;
+			cap_phone_handle_t phone;
 			errno_t rc;
 
@@ -2645,5 +2646,5 @@
  *
  */
-bool async_share_in_receive(cap_handle_t *chandle, size_t *size)
+bool async_share_in_receive(cap_call_handle_t *chandle, size_t *size)
 {
 	assert(chandle);
@@ -2673,5 +2674,6 @@
  *
  */
-errno_t async_share_in_finalize(cap_handle_t chandle, void *src, unsigned int flags)
+errno_t async_share_in_finalize(cap_call_handle_t chandle, void *src,
+    unsigned int flags)
 {
 	return ipc_answer_3(chandle, EOK, (sysarg_t) src, (sysarg_t) flags,
@@ -2712,5 +2714,5 @@
  *
  */
-bool async_share_out_receive(cap_handle_t *chandle, size_t *size,
+bool async_share_out_receive(cap_call_handle_t *chandle, size_t *size,
     unsigned int *flags)
 {
@@ -2743,5 +2745,5 @@
  *
  */
-errno_t async_share_out_finalize(cap_handle_t chandle, void **dst)
+errno_t async_share_out_finalize(cap_call_handle_t chandle, void **dst)
 {
 	return ipc_answer_2(chandle, EOK, (sysarg_t) __entry, (sysarg_t) dst);
@@ -2797,5 +2799,5 @@
  *
  */
-bool async_data_read_receive(cap_handle_t *chandle, size_t *size)
+bool async_data_read_receive(cap_call_handle_t *chandle, size_t *size)
 {
 	ipc_call_t data;
@@ -2817,5 +2819,5 @@
  *
  */
-bool async_data_read_receive_call(cap_handle_t *chandle, ipc_call_t *data,
+bool async_data_read_receive_call(cap_call_handle_t *chandle, ipc_call_t *data,
     size_t *size)
 {
@@ -2848,5 +2850,6 @@
  *
  */
-errno_t async_data_read_finalize(cap_handle_t chandle, const void *src, size_t size)
+errno_t async_data_read_finalize(cap_call_handle_t chandle, const void *src,
+    size_t size)
 {
 	return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) size);
@@ -2863,5 +2866,5 @@
 		return ENOENT;
 
-	cap_handle_t chandle;
+	cap_call_handle_t chandle;
 	if (!async_data_read_receive(&chandle, NULL)) {
 		ipc_answer_0(chandle, EINVAL);
@@ -2922,5 +2925,5 @@
  *
  */
-bool async_data_write_receive(cap_handle_t *chandle, size_t *size)
+bool async_data_write_receive(cap_call_handle_t *chandle, size_t *size)
 {
 	ipc_call_t data;
@@ -2943,5 +2946,5 @@
  *
  */
-bool async_data_write_receive_call(cap_handle_t *chandle, ipc_call_t *data,
+bool async_data_write_receive_call(cap_call_handle_t *chandle, ipc_call_t *data,
     size_t *size)
 {
@@ -2973,5 +2976,6 @@
  *
  */
-errno_t async_data_write_finalize(cap_handle_t chandle, void *dst, size_t size)
+errno_t async_data_write_finalize(cap_call_handle_t chandle, void *dst,
+    size_t size)
 {
 	return ipc_answer_2(chandle, EOK, (sysarg_t) dst, (sysarg_t) size);
@@ -3005,5 +3009,5 @@
 	assert(data);
 
-	cap_handle_t chandle;
+	cap_call_handle_t chandle;
 	size_t size;
 	if (!async_data_write_receive(&chandle, &size)) {
@@ -3064,5 +3068,5 @@
 void async_data_write_void(errno_t retval)
 {
-	cap_handle_t chandle;
+	cap_call_handle_t chandle;
 	async_data_write_receive(&chandle, NULL);
 	ipc_answer_0(chandle, retval);
@@ -3079,5 +3083,5 @@
 		return ENOENT;
 
-	cap_handle_t chandle;
+	cap_call_handle_t chandle;
 	if (!async_data_write_receive(&chandle, NULL)) {
 		ipc_answer_0(chandle, EINVAL);
@@ -3121,8 +3125,9 @@
 	/* Accept the phone */
 	ipc_call_t call;
-	cap_handle_t chandle = async_get_call(&call);
-	cap_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(call);
-
-	if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) || (phandle < 0)) {
+	cap_call_handle_t chandle = async_get_call(&call);
+	cap_phone_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(call);
+
+	if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
+	    !CAP_HANDLE_VALID((phandle))) {
 		async_answer_0(chandle, EINVAL);
 		return NULL;
@@ -3171,7 +3176,8 @@
 async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
 {
-	cap_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(*call);
-
-	if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) || (phandle < 0))
+	cap_phone_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(*call);
+
+	if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
+	    !CAP_HANDLE_VALID((phandle)))
 		return NULL;
 
@@ -3201,8 +3207,8 @@
 {
 	return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
-	    arg1, arg2, arg3, 0, other_exch->phone);
-}
-
-bool async_state_change_receive(cap_handle_t *chandle, sysarg_t *arg1,
+	    arg1, arg2, arg3, 0, CAP_HANDLE_RAW(other_exch->phone));
+}
+
+bool async_state_change_receive(cap_call_handle_t *chandle, sysarg_t *arg1,
     sysarg_t *arg2, sysarg_t *arg3)
 {
@@ -3225,7 +3231,8 @@
 }
 
-errno_t async_state_change_finalize(cap_handle_t chandle, async_exch_t *other_exch)
-{
-	return ipc_answer_1(chandle, EOK, other_exch->phone);
+errno_t async_state_change_finalize(cap_call_handle_t chandle,
+    async_exch_t *other_exch)
+{
+	return ipc_answer_1(chandle, EOK, CAP_HANDLE_RAW(other_exch->phone));
 }
 
