Index: uspace/lib/c/include/errno.h
===================================================================
--- uspace/lib/c/include/errno.h	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ uspace/lib/c/include/errno.h	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -65,4 +65,7 @@
 #define EEMPTY (-302)
 
+/** Negative acknowledgment. */
+#define ENAK (-303)
+
 /** An API function is called while another blocking function is in progress. */
 #define EINPROGRESS  (-10036)
Index: uspace/lib/usb/include/usb/usb.h
===================================================================
--- uspace/lib/usb/include/usb/usb.h	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ uspace/lib/usb/include/usb/usb.h	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -96,5 +96,6 @@
 	USB_REQUEST_RECIPIENT_DEVICE = 0,
 	USB_REQUEST_RECIPIENT_INTERFACE = 1,
-	USB_REQUEST_RECIPIENT_ENDPOINT = 2
+	USB_REQUEST_RECIPIENT_ENDPOINT = 2,
+	USB_REQUEST_RECIPIENT_OTHER = 3
 } usb_request_recipient_t;
 
Index: uspace/lib/usb/src/debug.c
===================================================================
--- uspace/lib/usb/src/debug.c	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ uspace/lib/usb/src/debug.c	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -158,5 +158,7 @@
 
 /** Fibril local storage for the dumped buffer. */
-static fibril_local char buffer_dump[BUFFER_DUMP_LEN];
+static fibril_local char buffer_dump[2][BUFFER_DUMP_LEN];
+/** Fibril local storage for buffer switching. */
+static fibril_local int buffer_dump_index = 0;
 
 /** Dump buffer into string.
@@ -167,5 +169,6 @@
  * can not do that) and you do not have to guard it against concurrent
  * calls to it.
- * The only limitation is that each call rewrites the buffer again.
+ * The only limitation is that each second call rewrites the buffer again
+ * (internally, two buffer are used in cyclic manner).
  * Thus, it is necessary to copy the buffer elsewhere (that includes printing
  * to screen or writing to file).
@@ -173,5 +176,5 @@
  * that is not a big limitation.
  *
- * @warning You cannot use this function twice in the same printf
+ * @warning You cannot use this function more than twice in the same printf
  * (see detailed explanation).
  *
@@ -185,8 +188,7 @@
 {
 	/*
-	 * Remove previous string (that might also reveal double usage of
-	 * this function).
+	 * Remove previous string.
 	 */
-	bzero(buffer_dump, BUFFER_DUMP_LEN);
+	bzero(buffer_dump[buffer_dump_index], BUFFER_DUMP_LEN);
 
 	if (buffer == NULL) {
@@ -202,5 +204,5 @@
 	/* How many bytes are available in the output buffer. */
 	size_t buffer_remaining_size = BUFFER_DUMP_LEN - 1 - REMAINDER_STR_LEN;
-	char *it = buffer_dump;
+	char *it = buffer_dump[buffer_dump_index];
 
 	size_t index = 0;
@@ -253,5 +255,9 @@
 	}
 
-	return buffer_dump;
+	/* Next time, use the other buffer. */
+	buffer_dump_index = 1 - buffer_dump_index;
+
+	/* Need to take the old one due to previous line. */
+	return buffer_dump[1 - buffer_dump_index];
 }
 
Index: uspace/lib/usbvirt/Makefile
===================================================================
--- uspace/lib/usbvirt/Makefile	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ uspace/lib/usbvirt/Makefile	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -1,4 +1,4 @@
 #
-# Copyright (c) 2010 Vojtech Horky
+# Copyright (c) 2011 Vojtech Horky
 # All rights reserved.
 #
@@ -33,10 +33,8 @@
 
 SOURCES = \
-	src/callback.c \
-	src/ctrlpipe.c \
-	src/debug.c \
-	src/main.c \
+	src/ipc.c \
+	src/ctrltransfer.c \
 	src/stdreq.c \
-	src/transaction.c
+	src/transfer.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/usbvirt/include/usbvirt/device.h
===================================================================
--- uspace/lib/usbvirt/include/usbvirt/device.h	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ uspace/lib/usbvirt/include/usbvirt/device.h	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -1,4 +1,4 @@
 /*
- * Copyright (c) 2010 Vojtech Horky
+ * Copyright (c) 2011 Vojtech Horky
  * All rights reserved.
  *
@@ -38,120 +38,24 @@
 #include <usb/usb.h>
 #include <usb/request.h>
-#include <usb/descriptor.h>
 
-/** Request type of a control transfer. */
-typedef enum {
-	/** Standard USB request. */
-	USBVIRT_REQUEST_TYPE_STANDARD = 0,
-	/** Standard class USB request. */
-	USBVIRT_REQUEST_TYPE_CLASS = 1
-} usbvirt_request_type_t;
-
-/** Recipient of control request. */
-typedef enum {
-	/** Device is the recipient of the control request. */
-	USBVIRT_REQUEST_RECIPIENT_DEVICE = 0,
-	/** Interface is the recipient of the control request. */
-	USBVIRT_REQUEST_RECIPIENT_INTERFACE = 1,
-	/** Endpoint is the recipient of the control request. */
-	USBVIRT_REQUEST_RECIPIENT_ENDPOINT = 2,
-	/** Other part of the device is the recipient of the control request. */
-	USBVIRT_REQUEST_RECIPIENT_OTHER = 3
-} usbvirt_request_recipient_t;
-
-/** Possible states of virtual USB device.
- * Notice that these are not 1:1 mappings to those in USB specification.
- */
-typedef enum {
-	/** Default state, device listens at default address. */
-	USBVIRT_STATE_DEFAULT,
-	/** Device has non-default address assigned. */
-	USBVIRT_STATE_ADDRESS,
-	/** Device is configured. */
-	USBVIRT_STATE_CONFIGURED
-} usbvirt_device_state_t;
+#define USBVIRT_ENDPOINT_MAX 16
 
 typedef struct usbvirt_device usbvirt_device_t;
-struct usbvirt_control_transfer;
 
-typedef int (*usbvirt_on_device_request_t)(usbvirt_device_t *dev,
-	usb_device_request_setup_packet_t *request,
-	uint8_t *data);
+typedef int (*usbvirt_on_data_to_device_t)(usbvirt_device_t *, usb_endpoint_t,
+    usb_transfer_type_t, void *, size_t);
+typedef int (*usbvirt_on_data_from_device_t)(usbvirt_device_t *, usb_endpoint_t,
+    usb_transfer_type_t, void *, size_t, size_t *);
+typedef int (*usbvirt_on_control_t)(usbvirt_device_t *,
+    const usb_device_request_setup_packet_t *, uint8_t *, size_t *);
 
-/** Callback for control request over pipe zero.
- *
- * @param dev Virtual device answering the call.
- * @param request Request setup packet.
- * @param data Data when DATA stage is present.
- * @return Error code.
- */
-typedef int (*usbvirt_control_request_callback_t)(usbvirt_device_t *dev,
-	usb_device_request_setup_packet_t *request,
-	uint8_t *data);
-
-/** Handler for control transfer on endpoint zero. */
 typedef struct {
-	/** Request type bitmap.
-	 * Use USBVIRT_MAKE_CONTROL_REQUEST_TYPE for creating the bitmap.
-	 */
-	uint8_t request_type;
-	/** Request code. */
+	usb_direction_t req_direction;
+	usb_request_recipient_t req_recipient;
+	usb_request_type_t req_type;
 	uint8_t request;
-	/** Request name for debugging. */
 	const char *name;
-	/** Callback for the request.
-	 * NULL value here announces end of a list.
-	 */
-	usbvirt_control_request_callback_t callback;
-} usbvirt_control_transfer_handler_t;
-
-/** Create control request type bitmap.
- *
- * @param direction Transfer direction (use usb_direction_t).
- * @param type Request type (use usbvirt_request_type_t).
- * @param recipient Recipient of the request (use usbvirt_request_recipient_t).
- * @return Request type bitmap.
- */
-#define USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, type, recipient) \
-	((((direction) == USB_DIRECTION_IN) ? 1 : 0) << 7) \
-	| (((type) & 3) << 5) \
-	| (((recipient) & 31))
-
-/** Create last item in an array of control request handlers. */
-#define USBVIRT_CONTROL_TRANSFER_HANDLER_LAST { 0, 0, NULL, NULL }
-
-/** Device operations. */
-typedef struct {
-	/** Callbacks for transfers over control pipe zero. */
-	usbvirt_control_transfer_handler_t *control_transfer_handlers;
-
-	int (*on_control_transfer)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, struct usbvirt_control_transfer *transfer);
-	
-	/** Callback for all other incoming data. */
-	int (*on_data)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size);
-	
-	/** Callback for host request for data. */
-	int (*on_data_request)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size, size_t *actual_size);
-	
-	/** Decides direction of control transfer. */
-	usb_direction_t (*decide_control_transfer_direction)(
-	    usb_endpoint_t endpoint, void *buffer, size_t size);
-
-	/** Callback when device changes its state.
-	 *
-	 * It is correct that this function is called when both states
-	 * are equal (e.g. this function is called during SET_CONFIGURATION
-	 * request done on already configured device).
-	 *
-	 * @warning The value of <code>dev->state</code> before calling
-	 * this function is not specified (i.e. can be @p old_state or
-	 * @p new_state).
-	 */
-	void (*on_state_change)(usbvirt_device_t *dev,
-	    usbvirt_device_state_t old_state, usbvirt_device_state_t new_state);
-} usbvirt_device_ops_t;
+	usbvirt_on_control_t callback;
+} usbvirt_control_request_handler_t;
 
 /** Extra configuration data for GET_CONFIGURATION request. */
@@ -179,104 +83,52 @@
 	 */
 	usb_standard_device_descriptor_t *device;
-	
+
 	/** Configurations. */
 	usbvirt_device_configuration_t *configuration;
 	/** Number of configurations. */
 	size_t configuration_count;
-	/** Index of currently selected configuration. */
-	uint8_t current_configuration;
 } usbvirt_descriptors_t;
 
-/** Information about on-going control transfer.
+/** Possible states of virtual USB device.
+ * Notice that these are not 1:1 mappings to those in USB specification.
  */
-typedef struct usbvirt_control_transfer {
-	/** Transfer direction (read/write control transfer). */
-	usb_direction_t direction;
-	/** Request data. */
-	void *request;
-	/** Size of request data. */
-	size_t request_size;
-	/** Payload. */
-	void *data;
-	/** Size of payload. */
-	size_t data_size;
-} usbvirt_control_transfer_t;
+typedef enum {
+	/** Default state, device listens at default address. */
+	USBVIRT_STATE_DEFAULT,
+	/** Device has non-default address assigned. */
+	USBVIRT_STATE_ADDRESS,
+	/** Device is configured. */
+	USBVIRT_STATE_CONFIGURED
+} usbvirt_device_state_t;
 
-typedef enum {
-	USBVIRT_DEBUGTAG_BASE = 1,
-	USBVIRT_DEBUGTAG_TRANSACTION = 2,
-	USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO = 4,
-	USBVIRT_DEBUGTAG_ALL = 255
-} usbvirt_debug_tags_t;
+typedef struct {
+	usbvirt_on_data_to_device_t data_out[USBVIRT_ENDPOINT_MAX];
+	usbvirt_on_data_from_device_t data_in[USBVIRT_ENDPOINT_MAX];
+	usbvirt_control_request_handler_t *control;
+	void (*state_changed)(usbvirt_device_t *dev,
+	    usbvirt_device_state_t old_state, usbvirt_device_state_t new_state);
+} usbvirt_device_ops_t;
 
-/** Virtual USB device. */
 struct usbvirt_device {
-	/** Callback device operations. */
+	const char *name;
+	void *device_data;
 	usbvirt_device_ops_t *ops;
-	
-	/** Custom device data. */
-	void *device_data;
+	usbvirt_descriptors_t *descriptors;
+	usb_address_t address;
+	usbvirt_device_state_t state;
+};
 
-	/** Reply onto control transfer.
-	 */
-	int (*control_transfer_reply)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size);
-	
-	/** Device name.
-	 * Used in debug prints and sent to virtual host controller.
-	 */
-	const char *name;
-	
-	/** Standard descriptors. */
-	usbvirt_descriptors_t *descriptors;
-	
-	/** Current device state. */
-	usbvirt_device_state_t state;
-	
-	/** Device address. */
-	usb_address_t address;
-	/** New device address.
-	 * This field is used during SET_ADDRESS request.
-	 * On all other occasions, it holds invalid address (e.g. -1).
-	 */
-	usb_address_t new_address;
-	
-	/** Process OUT transaction. */
-	int (*transaction_out)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size);
-	/** Process SETUP transaction. */
-	int (*transaction_setup)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size);
-	/** Process IN transaction. */
-	int (*transaction_in)(usbvirt_device_t *dev,
-	    usb_endpoint_t endpoint, void *buffer, size_t size, size_t *data_size);
-	
-	/** State information on control-transfer endpoints. */
-	usbvirt_control_transfer_t current_control_transfers[USB11_ENDPOINT_MAX];
-	
-	/* User debugging. */
-	
-	/** Debug print. */
-	void (*debug)(usbvirt_device_t *dev, int level, uint8_t tag,
-	    const char *format, ...);
-	
-	/** Current debug level. */
-	int debug_level;
-	
-	/** Bitmap of currently enabled tags. */
-	uint8_t debug_enabled_tags;
-	
-	/* Library debugging. */
-	
-	/** Debug print. */
-	void (*lib_debug)(usbvirt_device_t *dev, int level, uint8_t tag,
-	    const char *format, ...);
-	
-	/** Current debug level. */
-	int lib_debug_level;
-	
-	/** Bitmap of currently enabled tags. */
-	uint8_t lib_debug_enabled_tags;
-};
+int usbvirt_device_plug(usbvirt_device_t *, const char *);
+
+void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *,
+    uint8_t *, size_t *, void *, size_t);
+
+int usbvirt_control_write(usbvirt_device_t *, void *, size_t, void *, size_t);
+int usbvirt_control_read(usbvirt_device_t *, void *, size_t, void *, size_t, size_t *);
+int usbvirt_data_out(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
+    void *, size_t);
+int usbvirt_data_in(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
+    void *, size_t, size_t *);
+
 
 #endif
Index: uspace/lib/usbvirt/include/usbvirt/hub.h
===================================================================
--- uspace/lib/usbvirt/include/usbvirt/hub.h	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ 	(revision )
@@ -1,68 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Virtual USB device.
- */
-#ifndef LIBUSBVIRT_HUB_H_
-#define LIBUSBVIRT_HUB_H_
-
-#include "device.h"
-
-/** USB transaction type.
- * This types does not correspond directly to types in USB specification,
- * as actually DATA transactions are marked with these types to identify
- * their direction (and tag).
- */
-typedef enum {
-	USBVIRT_TRANSACTION_SETUP,
-	USBVIRT_TRANSACTION_IN,
-	USBVIRT_TRANSACTION_OUT
-} usbvirt_transaction_type_t;
-
-const char *usbvirt_str_transaction_type(usbvirt_transaction_type_t type);
-
-/** Telephony methods of virtual devices. */
-typedef enum {
-	IPC_M_USBVIRT_GET_NAME = IPC_FIRST_USER_METHOD,
-	IPC_M_USBVIRT_TRANSACTION_SETUP,
-	IPC_M_USBVIRT_TRANSACTION_OUT,
-	IPC_M_USBVIRT_TRANSACTION_IN,
-} usbvirt_device_method_t;
-
-int usbvirt_connect(usbvirt_device_t *);
-int usbvirt_connect_local(usbvirt_device_t *);
-int usbvirt_disconnect(usbvirt_device_t *dev);
-
-#endif
-/**
- * @}
- */
Index: uspace/lib/usbvirt/include/usbvirt/ipc.h
===================================================================
--- uspace/lib/usbvirt/include/usbvirt/ipc.h	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
+++ uspace/lib/usbvirt/include/usbvirt/ipc.h	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2010 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusbvirt
+ * @{
+ */
+/** @file
+ * @brief Virtual USB device.
+ */
+#ifndef LIBUSBVIRT_IPC_H_
+#define LIBUSBVIRT_IPC_H_
+
+#include <ipc/common.h>
+#include <usb/usb.h>
+#include <bool.h>
+
+typedef enum {
+	IPC_M_USBVIRT_GET_NAME = IPC_FIRST_USER_METHOD + 80,
+	IPC_M_USBVIRT_CONTROL_READ,
+	IPC_M_USBVIRT_CONTROL_WRITE,
+	IPC_M_USBVIRT_INTERRUPT_IN,
+	IPC_M_USBVIRT_INTERRUPT_OUT
+} usbvirt_ipc_t;
+
+int usbvirt_ipc_send_control_read(int, usb_endpoint_t, void *, size_t,
+    void *, size_t, size_t *);
+int usbvirt_ipc_send_control_write(int, usb_endpoint_t, void *, size_t,
+    void *, size_t);
+int usbvirt_ipc_send_data_in(int, usb_endpoint_t, usb_transfer_type_t,
+    void *, size_t, size_t *);
+int usbvirt_ipc_send_data_out(int, usb_endpoint_t, usb_transfer_type_t,
+    void *, size_t);
+
+bool usbvirt_is_usbvirt_method(sysarg_t);
+bool usbvirt_ipc_handle_call(usbvirt_device_t *, ipc_callid_t, ipc_call_t *);
+
+#endif
+/**
+ * @}
+ */
Index: uspace/lib/usbvirt/src/callback.c
===================================================================
--- uspace/lib/usbvirt/src/callback.c	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ 	(revision )
@@ -1,237 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Callback connection handling.
- */
-#include <devmap.h>
-#include <fcntl.h>
-#include <vfs/vfs.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <mem.h>
-
-#include "private.h"
-
-#define NAMESPACE "usb"
-#define USB_MAX_PAYLOAD_SIZE 1020
-
-/** Wrapper for SETUP transaction over telephone. */
-static void handle_setup_transaction(usbvirt_device_t *device,
-    ipc_callid_t iid, ipc_call_t icall)
-{
-	usb_address_t address = IPC_GET_ARG1(icall);
-	usb_endpoint_t endpoint = IPC_GET_ARG2(icall);
-	size_t expected_len = IPC_GET_ARG3(icall);
-	
-	if (address != device->address) {
-		async_answer_0(iid, EADDRNOTAVAIL);
-		return;
-	}
-	
-	if ((endpoint < 0) || (endpoint >= USB11_ENDPOINT_MAX)) {
-		async_answer_0(iid, EINVAL);
-		return;
-	}
-	
-	if (expected_len == 0) {
-		async_answer_0(iid, EINVAL);
-		return;
-	}
-	
-	size_t len = 0;
-	void * buffer = NULL;
-	int rc = async_data_write_accept(&buffer, false,
-	    1, USB_MAX_PAYLOAD_SIZE, 0, &len);
-		
-	if (rc != EOK) {
-		async_answer_0(iid, rc);
-		return;
-	}
-	
-	rc = device->transaction_setup(device, endpoint, buffer, len);
-	
-	async_answer_0(iid, rc);
-}
-
-/** Wrapper for OUT transaction over telephone. */
-static void handle_out_transaction(usbvirt_device_t *device,
-    ipc_callid_t iid, ipc_call_t icall)
-{
-	usb_address_t address = IPC_GET_ARG1(icall);
-	usb_endpoint_t endpoint = IPC_GET_ARG2(icall);
-	size_t expected_len = IPC_GET_ARG3(icall);
-	
-	if (address != device->address) {
-		async_answer_0(iid, EADDRNOTAVAIL);
-		return;
-	}
-	
-	if ((endpoint < 0) || (endpoint >= USB11_ENDPOINT_MAX)) {
-		async_answer_0(iid, EINVAL);
-		return;
-	}
-	
-	int rc = EOK;
-	
-	size_t len = 0;
-	void *buffer = NULL;
-	
-	if (expected_len > 0) {
-		rc = async_data_write_accept(&buffer, false,
-		    1, USB_MAX_PAYLOAD_SIZE, 0, &len);
-			
-		if (rc != EOK) {
-			async_answer_0(iid, rc);
-			return;
-		}
-	}
-	
-	rc = device->transaction_out(device, endpoint, buffer, len);
-	
-	if (buffer != NULL) {
-		free(buffer);
-	}
-	
-	async_answer_0(iid, rc);
-}
-
-
-/** Wrapper for IN transaction over telephone. */
-static void handle_in_transaction(usbvirt_device_t *device,
-    ipc_callid_t iid, ipc_call_t icall)
-{
-	usb_address_t address = IPC_GET_ARG1(icall);
-	usb_endpoint_t endpoint = IPC_GET_ARG2(icall);
-	size_t expected_len = IPC_GET_ARG3(icall);
-	
-	if (address != device->address) {
-		async_answer_0(iid, EADDRNOTAVAIL);
-		return;
-	}
-	
-	if ((endpoint < 0) || (endpoint >= USB11_ENDPOINT_MAX)) {
-		async_answer_0(iid, EINVAL);
-		return;
-	}
-	
-	int rc = EOK;
-	
-	void *buffer = expected_len > 0 ? malloc(expected_len) : NULL;
-	size_t len;
-	
-	rc = device->transaction_in(device, endpoint, buffer, expected_len, &len);
-	/*
-	 * If the request was processed, we will send data back.
-	 */
-	if ((rc == EOK) && (expected_len > 0)) {
-		size_t receive_len;
-		ipc_callid_t callid;
-		if (!async_data_read_receive(&callid, &receive_len)) {
-			async_answer_0(iid, EINVAL);
-			return;
-		}
-		if (len > receive_len) {
-			len = receive_len;
-		}
-		async_data_read_finalize(callid, buffer, len);
-	}
-	
-	async_answer_1(iid, rc, len);
-}
-
-/** Wrapper for getting device name. */
-static void handle_get_name(usbvirt_device_t *device,
-    ipc_callid_t iid, ipc_call_t icall)
-{
-	if (device->name == NULL) {
-		async_answer_0(iid, ENOENT);
-	}
-	
-	size_t size = str_size(device->name);
-	
-	ipc_callid_t callid;
-	size_t accepted_size;
-	if (!async_data_read_receive(&callid, &accepted_size)) {
-		async_answer_0(iid, EINVAL);
-		return;
-	}
-	
-	if (accepted_size > size) {
-		accepted_size = size;
-	}
-	async_data_read_finalize(callid, device->name, accepted_size);
-	
-	async_answer_1(iid, EOK, accepted_size);
-}
-
-/** Callback connection for a given device. */
-void device_callback_connection(usbvirt_device_t *device, ipc_callid_t iid, ipc_call_t *icall)
-{
-	async_answer_0(iid, EOK);
-	
-	while (true) {
-		ipc_callid_t callid; 
-		ipc_call_t call; 
-		
-		callid = async_get_call(&call);
-		switch (IPC_GET_IMETHOD(call)) {
-			case IPC_M_PHONE_HUNGUP:
-				async_answer_0(callid, EOK);
-				return;
-			
-			case IPC_M_USBVIRT_GET_NAME:
-				handle_get_name(device, callid, call);
-				break;
-			
-			case IPC_M_USBVIRT_TRANSACTION_SETUP:
-				handle_setup_transaction(device, callid, call);
-				break;
-			
-			case IPC_M_USBVIRT_TRANSACTION_OUT:
-				handle_out_transaction(device, callid, call);
-				break;
-				
-			case IPC_M_USBVIRT_TRANSACTION_IN:
-				handle_in_transaction(device, callid, call);
-				break;
-			
-			default:
-				async_answer_0(callid, EINVAL);
-				break;
-		}
-	}
-}
-
-
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/ctrlpipe.c
===================================================================
--- uspace/lib/usbvirt/src/ctrlpipe.c	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ 	(revision )
@@ -1,182 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Device control pipe.
- */
-#include <errno.h>
-
-#include "private.h"
-
-/** Compares handler type with request packet type.
- *
- * @param handler Handler.
- * @param request_packet Request packet.
- * @return Whether handler can serve this packet.
- */
-static bool is_suitable_handler(usbvirt_control_transfer_handler_t *handler,
-    usb_device_request_setup_packet_t *request_packet)
-{
-	return (
-	    (handler->request_type == request_packet->request_type)
-	    && (handler->request == request_packet->request));
-
-}
-
-/** Find suitable transfer handler for given request packet.
- *
- * @param handlers Array of available handlers.
- * @param request_packet Request SETUP packet.
- * @return Handler or NULL.
- */
-static usbvirt_control_transfer_handler_t *find_handler(
-    usbvirt_control_transfer_handler_t *handlers,
-    usb_device_request_setup_packet_t *request_packet)
-{
-	if (handlers == NULL) {
-		return NULL;
-	}
-
-	while (handlers->callback != NULL) {
-		if (is_suitable_handler(handlers, request_packet)) {
-			return handlers;
-		}
-		handlers++;
-	}
-
-	return NULL;
-}
-
-#define _GET_BIT(byte, bit) \
-	(((byte) & (1 << (bit))) ? '1' : '0')
-#define _GET_BITS(byte) \
-	_GET_BIT(byte, 7), _GET_BIT(byte, 6), _GET_BIT(byte, 5), \
-	_GET_BIT(byte, 4), _GET_BIT(byte, 3), _GET_BIT(byte, 2), \
-	_GET_BIT(byte, 1), _GET_BIT(byte, 0)
-
-static int find_and_run_handler(usbvirt_device_t *device,
-    usbvirt_control_transfer_handler_t *handlers,
-    usb_device_request_setup_packet_t *setup_packet,
-    uint8_t *data)
-{
-	int rc = EFORWARD;
-	usbvirt_control_transfer_handler_t *suitable_handler
-	    = find_handler(handlers, setup_packet);
-	if (suitable_handler != NULL) {
-		const char *callback_name = "user handler";
-		if (suitable_handler->name != NULL) {
-			callback_name = suitable_handler->name;
-		}
-		device->lib_debug(device, 1, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
-		    "pipe #0 - calling %s " \
-		        "[%c.%c%c.%c%c%c%c%c, R%d, V%d, I%d, L%d]",
-		    callback_name,
-		    _GET_BITS(setup_packet->request_type),
-		    setup_packet->request, setup_packet->value,
-		    setup_packet->index, setup_packet->length);
-		rc = suitable_handler->callback(device, setup_packet, data);
-	}
-
-	return rc;
-}
-#undef _GET_BITS
-#undef _GET_BIT
-
-
-/** Handle communication over control pipe zero.
- */
-int control_pipe(usbvirt_device_t *device, usbvirt_control_transfer_t *transfer)
-{
-	device->lib_debug(device, 2, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
-	    "op on control pipe zero (request_size=%u)", transfer->request_size);
-	
-	if (transfer->request_size < sizeof(usb_device_request_setup_packet_t)) {
-		return ENOMEM;
-	}
-	
-	usb_device_request_setup_packet_t *request
-	    = (usb_device_request_setup_packet_t *) transfer->request;
-
-	/*
-	 * First, see whether user provided its own callback.
-	 */
-	int rc = EFORWARD;
-	if (device->ops) {
-		rc = find_and_run_handler(device,
-		    device->ops->control_transfer_handlers,
-		    request, transfer->data);
-	}
-
-	/*
-	 * If there was no user callback or the callback returned EFORWARD,
-	 * we need to run a local handler.
-	 */
-	if (rc == EFORWARD) {
-		rc = find_and_run_handler(device,
-		    control_pipe_zero_local_handlers,
-		    request, transfer->data);
-	}
-	
-	/*
-	 * Check for SET_ADDRESS finalization.
-	 */
-	if (device->new_address != -1) {
-		/*
-		 * TODO: handle when this request is invalid (e.g.
-		 * setting address when in configured state).
-		 */
-		usbvirt_device_state_t new_state;
-		if (device->new_address == 0) {
-			new_state = USBVIRT_STATE_DEFAULT;
-		} else {
-			new_state = USBVIRT_STATE_ADDRESS;
-		}
-		device->address = device->new_address;
-		
-		device->new_address = -1;
-		
-		if (DEVICE_HAS_OP(device, on_state_change)) {
-			device->ops->on_state_change(device, device->state,
-			    new_state);
-		}
-		device->state = new_state;
-
-		device->lib_debug(device, 2, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
-		    "device address changed to %d (state %s)",
-		    device->address, str_device_state(device->state));
-	}
-	
-	return rc;
-}
-
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/ctrltransfer.c
===================================================================
--- uspace/lib/usbvirt/src/ctrltransfer.c	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
+++ uspace/lib/usbvirt/src/ctrltransfer.c	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -0,0 +1,53 @@
+#include "private.h"
+#include <usb/request.h>
+#include <usb/debug.h>
+#include <assert.h>
+#include <errno.h>
+
+int process_control_transfer(usbvirt_device_t *dev,
+    usbvirt_control_request_handler_t *control_handlers,
+    usb_device_request_setup_packet_t *setup,
+    uint8_t *data, size_t *data_sent_size)
+{
+	assert(dev);
+	assert(setup);
+
+	if (control_handlers == NULL) {
+		return EFORWARD;
+	}
+
+	usb_direction_t direction = setup->request_type & 128 ?
+	    USB_DIRECTION_IN : USB_DIRECTION_OUT;
+	usb_request_recipient_t req_recipient = setup->request_type & 31;
+	usb_request_type_t req_type = (setup->request_type >> 5) & 3;
+
+	usbvirt_control_request_handler_t *handler = control_handlers;
+	while (handler->callback != NULL) {
+		if (handler->req_direction != direction) {
+			goto next;
+		}
+		if (handler->req_recipient != req_recipient) {
+			goto next;
+		}
+		if (handler->req_type != req_type) {
+			goto next;
+		}
+		if (handler->request != setup->request) {
+			goto next;
+		}
+
+		usb_log_debug("Control transfer: %s(%s)\n", handler->name,
+		    usb_debug_str_buffer((uint8_t*) setup, sizeof(*setup), 0));
+		int rc = handler->callback(dev, setup, data, data_sent_size);
+		if (rc == EFORWARD) {
+			goto next;
+		}
+
+		return rc;
+
+next:
+		handler++;
+	}
+
+	return EFORWARD;
+}
Index: uspace/lib/usbvirt/src/debug.c
===================================================================
--- uspace/lib/usbvirt/src/debug.c	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ 	(revision )
@@ -1,103 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Debugging support.
- */
-#include <stdio.h>
-#include <bool.h>
-
-#include "private.h"
-
-
-static void debug_print(int level, uint8_t tag,
-    int current_level, uint8_t enabled_tags,
-    const char *format, va_list args)
-{
-	if (level > current_level) {
-		return;
-	}
-	
-	if ((tag & enabled_tags) == 0) {
-		return;
-	}
-	
-	bool print_prefix = true;
-	
-	if ((format[0] == '%') && (format[1] == 'M')) {
-		format += 2;
-		print_prefix = false;
-	}
-	
-	if (print_prefix) {
-		printf("[vusb]: ");
-		while (--level > 0) {
-			printf(" ");
-		}
-	}
-	
-	vprintf(format, args);
-	
-	if (print_prefix) {
-		printf("\n");
-	}
-}
-
-
-void user_debug(usbvirt_device_t *device, int level, uint8_t tag,
-    const char *format, ...)
-{
-	va_list args;
-	va_start(args, format);
-	
-	debug_print(level, tag,
-	    device->debug_level, device->debug_enabled_tags,
-	    format, args);
-	
-	va_end(args);
-}
-
-void lib_debug(usbvirt_device_t *device, int level, uint8_t tag,
-    const char *format, ...)
-{
-	va_list args;
-	va_start(args, format);
-	
-	debug_print(level, tag,
-	    device->lib_debug_level, device->lib_debug_enabled_tags,
-	    format, args);
-	
-	va_end(args);
-}
-
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/ipc.c
===================================================================
--- uspace/lib/usbvirt/src/ipc.c	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
+++ uspace/lib/usbvirt/src/ipc.c	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -0,0 +1,455 @@
+/*
+ * Copyright (c) 2011 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusbvirt
+ * @{
+ */
+/** @file
+ *
+ */
+#include <errno.h>
+#include <str.h>
+#include <stdio.h>
+#include <assert.h>
+#include <async.h>
+#include <devman.h>
+#include <usbvirt/device.h>
+#include <usbvirt/ipc.h>
+
+#include <usb/debug.h>
+
+static usbvirt_device_t *DEV = NULL;
+
+static void ipc_get_name(usbvirt_device_t *dev,
+    ipc_callid_t iid, ipc_call_t *icall)
+{
+	if (dev->name == NULL) {
+		async_answer_0(iid, ENOENT);
+	}
+
+	size_t size = str_size(dev->name);
+
+	ipc_callid_t callid;
+	size_t accepted_size;
+	if (!async_data_read_receive(&callid, &accepted_size)) {
+		async_answer_0(iid, EINVAL);
+		return;
+	}
+
+	if (accepted_size > size) {
+		accepted_size = size;
+	}
+	async_data_read_finalize(callid, dev->name, accepted_size);
+
+	async_answer_1(iid, EOK, accepted_size);
+}
+
+static void ipc_control_read(usbvirt_device_t *dev,
+    ipc_callid_t iid, ipc_call_t *icall)
+{
+	//usb_endpoint_t endpoint = IPC_GET_ARG1(*icall);
+
+	int rc;
+
+	void *setup_packet = NULL;
+	size_t setup_packet_len = 0;
+	size_t data_len = 0;
+
+	rc = async_data_write_accept(&setup_packet, false,
+	    1, 1024, 0, &setup_packet_len);
+	if (rc != EOK) {
+		async_answer_0(iid, rc);
+		return;
+	}
+
+	ipc_callid_t data_callid;
+	if (!async_data_read_receive(&data_callid, &data_len)) {
+		async_answer_0(iid, EPARTY);
+		free(setup_packet);
+		return;
+	}
+
+	void *buffer = malloc(data_len);
+	if (buffer == NULL) {
+		async_answer_0(iid, ENOMEM);
+		free(setup_packet);
+		return;
+	}
+
+	size_t actual_len;
+	rc = usbvirt_control_read(dev, setup_packet, setup_packet_len,
+	    buffer, data_len, &actual_len);
+
+	if (rc != EOK) {
+		async_answer_0(data_callid, rc);
+		async_answer_0(iid, rc);
+		free(setup_packet);
+		free(buffer);
+		return;
+	}
+
+	async_data_read_finalize(data_callid, buffer, actual_len);
+	async_answer_0(iid, EOK);
+
+	free(setup_packet);
+	free(buffer);
+}
+
+static void ipc_control_write(usbvirt_device_t *dev,
+    ipc_callid_t iid, ipc_call_t *icall)
+{
+	size_t data_buffer_len = IPC_GET_ARG2(*icall);
+	int rc;
+
+	void *setup_packet = NULL;
+	void *data_buffer = NULL;
+	size_t setup_packet_len = 0;
+
+	rc = async_data_write_accept(&setup_packet, false,
+	    1, 1024, 0, &setup_packet_len);
+	if (rc != EOK) {
+		async_answer_0(iid, rc);
+		return;
+	}
+
+	if (data_buffer_len > 0) {
+		rc = async_data_write_accept(&data_buffer, false,
+		    1, 1024, 0, &data_buffer_len);
+		if (rc != EOK) {
+			async_answer_0(iid, rc);
+			free(setup_packet);
+			return;
+		}
+	}
+
+	rc = usbvirt_control_write(dev, setup_packet, setup_packet_len,
+	    data_buffer, data_buffer_len);
+
+	async_answer_0(iid, rc);
+}
+
+static void ipc_interrupt_in(usbvirt_device_t *dev,
+    ipc_callid_t iid, ipc_call_t *icall)
+{
+	usb_endpoint_t endpoint = IPC_GET_ARG1(*icall);
+	usb_transfer_type_t transfer_type = IPC_GET_ARG2(*icall);
+
+	int rc;
+
+	size_t data_len = 0;
+	ipc_callid_t data_callid;
+	if (!async_data_read_receive(&data_callid, &data_len)) {
+		async_answer_0(iid, EPARTY);
+		return;
+	}
+
+	void *buffer = malloc(data_len);
+	if (buffer == NULL) {
+		async_answer_0(iid, ENOMEM);
+		return;
+	}
+
+	size_t actual_len;
+	rc = usbvirt_data_in(dev, transfer_type, endpoint,
+	    buffer, data_len, &actual_len);
+
+	if (rc != EOK) {
+		async_answer_0(data_callid, rc);
+		async_answer_0(iid, rc);
+		free(buffer);
+		return;
+	}
+
+	async_data_read_finalize(data_callid, buffer, actual_len);
+	async_answer_0(iid, EOK);
+
+	free(buffer);
+}
+
+static void ipc_interrupt_out(usbvirt_device_t *dev,
+    ipc_callid_t iid, ipc_call_t *icall)
+{
+	usb_endpoint_t endpoint = IPC_GET_ARG1(*icall);
+	usb_transfer_type_t transfer_type = IPC_GET_ARG2(*icall);
+
+	void *data_buffer = NULL;
+	size_t data_buffer_size = 0;
+
+	int rc = async_data_write_accept(&data_buffer, false,
+	    1, 1024, 0, &data_buffer_size);
+	if (rc != EOK) {
+		async_answer_0(iid, rc);
+		return;
+	}
+
+	rc = usbvirt_data_out(dev, transfer_type, endpoint,
+	    data_buffer, data_buffer_size);
+
+	async_answer_0(iid, rc);
+
+	free(data_buffer);
+}
+
+
+bool usbvirt_ipc_handle_call(usbvirt_device_t *dev,
+    ipc_callid_t callid, ipc_call_t *call)
+{
+	switch (IPC_GET_IMETHOD(*call)) {
+		case IPC_M_USBVIRT_GET_NAME:
+			ipc_get_name(dev, callid, call);
+			break;
+
+		case IPC_M_USBVIRT_CONTROL_READ:
+			ipc_control_read(dev, callid, call);
+			break;
+
+		case IPC_M_USBVIRT_CONTROL_WRITE:
+			ipc_control_write(dev, callid, call);
+			break;
+
+		case IPC_M_USBVIRT_INTERRUPT_IN:
+			ipc_interrupt_in(dev, callid, call);
+			break;
+
+		case IPC_M_USBVIRT_INTERRUPT_OUT:
+			ipc_interrupt_out(dev, callid, call);
+			break;
+
+		default:
+			return false;
+	}
+
+	return true;
+}
+
+static void callback_connection(ipc_callid_t iid, ipc_call_t *icall)
+{
+	assert(DEV != NULL);
+
+	async_answer_0(iid, EOK);
+
+	while (true) {
+		ipc_callid_t callid;
+		ipc_call_t call;
+
+		callid = async_get_call(&call);
+		bool processed = usbvirt_ipc_handle_call(DEV, callid, &call);
+		if (!processed) {
+			switch (IPC_GET_IMETHOD(call)) {
+				case IPC_M_PHONE_HUNGUP:
+					async_answer_0(callid, EOK);
+					return;
+				default:
+					async_answer_0(callid, EINVAL);
+					break;
+			}
+		}
+	}
+}
+
+int usbvirt_device_plug(usbvirt_device_t *dev, const char *vhc_path)
+{
+	int rc;
+	devman_handle_t handle;
+
+	rc = devman_device_get_handle(vhc_path, &handle, 0);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	int hcd_phone = devman_device_connect(handle, 0);
+
+	if (hcd_phone < 0) {
+		return hcd_phone;
+	}
+
+	DEV = dev;
+
+	rc = async_connect_to_me(hcd_phone, 0, 0, 0, callback_connection);
+	if (rc != EOK) {
+		DEV = NULL;
+		return rc;
+	}
+
+
+
+	return EOK;
+}
+
+
+
+int usbvirt_ipc_send_control_read(int phone, usb_endpoint_t ep,
+    void *setup_buffer, size_t setup_buffer_size,
+    void *data_buffer, size_t data_buffer_size, size_t *data_transfered_size)
+{
+	aid_t opening_request = async_send_1(phone,
+	    IPC_M_USBVIRT_CONTROL_READ, ep, NULL);
+	if (opening_request == 0) {
+		return ENOMEM;
+	}
+
+	int rc = async_data_write_start(phone,
+	    setup_buffer, setup_buffer_size);
+	if (rc != EOK) {
+		async_wait_for(opening_request, NULL);
+		return rc;
+	}
+
+	ipc_call_t data_request_call;
+	aid_t data_request = async_data_read(phone,
+	    data_buffer, data_buffer_size,
+	    &data_request_call);
+
+	if (data_request == 0) {
+		async_wait_for(opening_request, NULL);
+		return ENOMEM;
+	}
+
+	sysarg_t data_request_rc;
+	sysarg_t opening_request_rc;
+	async_wait_for(data_request, &data_request_rc);
+	async_wait_for(opening_request, &opening_request_rc);
+
+	if (data_request_rc != EOK) {
+		/* Prefer the return code of the opening request. */
+		if (opening_request_rc != EOK) {
+			return (int) opening_request_rc;
+		} else {
+			return (int) data_request_rc;
+		}
+	}
+	if (opening_request_rc != EOK) {
+		return (int) opening_request_rc;
+	}
+
+	*data_transfered_size = IPC_GET_ARG2(data_request_call);
+
+	return EOK;
+}
+
+int usbvirt_ipc_send_control_write(int phone, usb_endpoint_t ep,
+    void *setup_buffer, size_t setup_buffer_size,
+    void *data_buffer, size_t data_buffer_size)
+{
+	aid_t opening_request = async_send_2(phone,
+	    IPC_M_USBVIRT_CONTROL_WRITE, ep, data_buffer_size,  NULL);
+	if (opening_request == 0) {
+		return ENOMEM;
+	}
+
+	int rc = async_data_write_start(phone,
+	    setup_buffer, setup_buffer_size);
+	if (rc != EOK) {
+		async_wait_for(opening_request, NULL);
+		return rc;
+	}
+
+	if (data_buffer_size > 0) {
+		rc = async_data_write_start(phone,
+		    data_buffer, data_buffer_size);
+
+		if (rc != EOK) {
+			async_wait_for(opening_request, NULL);
+			return rc;
+		}
+	}
+
+	sysarg_t opening_request_rc;
+	async_wait_for(opening_request, &opening_request_rc);
+
+	return (int) opening_request_rc;
+}
+
+int usbvirt_ipc_send_data_in(int phone, usb_endpoint_t ep,
+    usb_transfer_type_t tr_type, void *data, size_t data_size, size_t *act_size)
+{
+	aid_t opening_request = async_send_2(phone,
+	    IPC_M_USBVIRT_INTERRUPT_IN, ep, tr_type, NULL);
+	if (opening_request == 0) {
+		return ENOMEM;
+	}
+
+	ipc_call_t data_request_call;
+	aid_t data_request = async_data_read(phone,
+	    data, data_size,  &data_request_call);
+
+	if (data_request == 0) {
+		async_wait_for(opening_request, NULL);
+		return ENOMEM;
+	}
+
+	sysarg_t data_request_rc;
+	sysarg_t opening_request_rc;
+	async_wait_for(data_request, &data_request_rc);
+	async_wait_for(opening_request, &opening_request_rc);
+
+	if (data_request_rc != EOK) {
+		/* Prefer the return code of the opening request. */
+		if (opening_request_rc != EOK) {
+			return (int) opening_request_rc;
+		} else {
+			return (int) data_request_rc;
+		}
+	}
+	if (opening_request_rc != EOK) {
+		return (int) opening_request_rc;
+	}
+
+	if (act_size != NULL) {
+		*act_size = IPC_GET_ARG2(data_request_call);
+	}
+
+	return EOK;
+}
+
+int usbvirt_ipc_send_data_out(int phone, usb_endpoint_t ep,
+    usb_transfer_type_t tr_type, void *data, size_t data_size)
+{
+	aid_t opening_request = async_send_2(phone,
+	    IPC_M_USBVIRT_INTERRUPT_OUT, ep, tr_type, NULL);
+	if (opening_request == 0) {
+		return ENOMEM;
+	}
+
+	int rc = async_data_write_start(phone,
+	    data, data_size);
+	if (rc != EOK) {
+		async_wait_for(opening_request, NULL);
+		return rc;
+	}
+
+	sysarg_t opening_request_rc;
+	async_wait_for(opening_request, &opening_request_rc);
+
+	return (int) opening_request_rc;
+}
+
+
+/**
+ * @}
+ */
Index: uspace/lib/usbvirt/src/main.c
===================================================================
--- uspace/lib/usbvirt/src/main.c	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ 	(revision )
@@ -1,284 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Device registration with virtual USB framework.
- */
-#include <devman.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <mem.h>
-#include <assert.h>
-
-#include "private.h"
-
-#define NAMESPACE "usb"
-
-/** Virtual device wrapper. */
-typedef struct {
-	/** Actual device. */
-	usbvirt_device_t *device;
-	/** Phone to host controller. */
-	int vhcd_phone;
-	/** Device id. */
-	sysarg_t id;
-	/** Linked-list member. */
-	link_t link;
-} virtual_device_t;
-
-/*** List of known device. */
-static LIST_INITIALIZE(device_list);
-
-/** Find virtual device wrapper based on the contents. */
-static virtual_device_t *find_device(usbvirt_device_t *device)
-{
-	if (list_empty(&device_list)) {
-		return NULL;
-	}
-	
-	link_t *pos;
-	for (pos = device_list.next; pos != &device_list; pos = pos->next) {
-		virtual_device_t *dev
-		    = list_get_instance(pos, virtual_device_t, link);
-		if (dev->device == device) {
-			return dev;
-		}
-	}
-	
-	return NULL;
-}
-
-/** Find virtual device wrapper by its id. */
-static virtual_device_t *find_device_by_id(sysarg_t id)
-{
-	if (list_empty(&device_list)) {
-		return NULL;
-	}
-	
-	link_t *pos;
-	for (pos = device_list.next; pos != &device_list; pos = pos->next) {
-		virtual_device_t *dev
-		    = list_get_instance(pos, virtual_device_t, link);
-		if (dev->id == id) {
-			return dev;
-		}
-	}
-	
-	return NULL;
-}
-
-/** Reply to a control transfer. */
-static int control_transfer_reply(usbvirt_device_t *device,
-	    usb_endpoint_t endpoint, void *buffer, size_t size)
-{
-	usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
-	if (transfer->data != NULL) {
-		free(transfer->data);
-	}
-	transfer->data = malloc(size);
-	memcpy(transfer->data, buffer, size);
-	transfer->data_size = size;
-	
-	return EOK;
-}
-
-/** Initialize virtual device. */
-static void device_init(usbvirt_device_t *dev)
-{
-	dev->transaction_out = transaction_out;
-	dev->transaction_setup = transaction_setup;
-	dev->transaction_in = transaction_in;
-	
-	dev->control_transfer_reply = control_transfer_reply;
-	
-	dev->debug = user_debug;
-	dev->lib_debug = lib_debug;
-	
-	dev->state = USBVIRT_STATE_DEFAULT;
-	dev->address = 0;
-	dev->new_address = -1;
-	
-	size_t i;
-	for (i = 0; i < USB11_ENDPOINT_MAX; i++) {
-		usbvirt_control_transfer_t *transfer = &dev->current_control_transfers[i];
-		transfer->direction = 0;
-		transfer->request = NULL;
-		transfer->request_size = 0;
-		transfer->data = NULL;
-		transfer->data_size = 0;
-	}
-}
-
-/** Add a virtual device.
- * The returned device (if not NULL) shall be destroy via destroy_device().
- */
-static virtual_device_t *add_device(usbvirt_device_t *dev)
-{
-	assert(find_device(dev) == NULL);
-	virtual_device_t *new_device
-	    = (virtual_device_t *) malloc(sizeof(virtual_device_t));
-	
-	new_device->device = dev;
-	link_initialize(&new_device->link);
-	
-	list_append(&new_device->link, &device_list);
-	
-	return new_device;
-}
-
-/** Destroy virtual device. */
-static void destroy_device(virtual_device_t *dev)
-{
-	if (dev->vhcd_phone > 0) {
-		async_hangup(dev->vhcd_phone);
-	}
-	
-	list_remove(&dev->link);
-	
-	free(dev);
-}
-
-/** Callback connection handler. */
-static void callback_connection(ipc_callid_t iid, ipc_call_t *icall)
-{
-	// FIXME - determine which device just called back
-	virtual_device_t *dev = find_device_by_id(0);
-	if (dev == NULL) {
-		async_answer_0(iid, EINVAL);
-		printf("Ooops\n");
-		return;
-	}
-
-	device_callback_connection(dev->device, iid, icall);
-}
-
-/** Create necessary phones for communication with virtual HCD.
- * This function wraps following calls:
- * -# open <code>/dev/devices/\\virt\\usbhc</code> for reading
- * -# access phone of file opened in previous step
- * -# create callback through just opened phone
- * -# create handler for calling on data from host to function
- * -# return the (outgoing) phone
- *
- * @warning This function is wrapper for several actions and therefore
- * it is not possible - in case of error - to determine at which point
- * error occurred.
- *
- * @param dev Device to connect.
- * @return EOK on success or error code from errno.h.
- */
-int usbvirt_connect(usbvirt_device_t *dev)
-{
-	virtual_device_t *virtual_device = find_device(dev);
-	if (virtual_device != NULL) {
-		return EEXISTS;
-	}
-	
-	const char *vhc_path = "/virt/usbhc/hc";
-	int rc;
-	devman_handle_t handle;
-
-	rc = devman_device_get_handle(vhc_path, &handle, 0);
-	if (rc != EOK) {
-		printf("devman_device_get_handle() failed\n");
-		return rc;
-	}
-	
-	int hcd_phone = devman_device_connect(handle, 0);
-	
-	if (hcd_phone < 0) {
-		printf("devman_device_connect() failed\n");
-		return hcd_phone;
-	}
-	
-	rc = async_connect_to_me(hcd_phone, 0, 0, 0, callback_connection);
-	if (rc != EOK) {
-		printf("ipc_connect_to_me() failed\n");
-		return rc;
-	}
-	
-	device_init(dev);
-	
-	virtual_device = add_device(dev);
-	virtual_device->vhcd_phone = hcd_phone;
-	virtual_device->id = 0;
-	
-	return EOK;
-}
-
-/** Prepares device as local.
- * This is useful if you want to have a virtual device in the same task
- * as HCD.
- *
- * @param dev Device to connect.
- * @return Error code.
- * @retval EOK Device connected.
- * @retval EEXISTS This device is already connected.
- */
-int usbvirt_connect_local(usbvirt_device_t *dev)
-{
-	virtual_device_t *virtual_device = find_device(dev);
-	if (virtual_device != NULL) {
-		return EEXISTS;
-	}
-	
-	device_init(dev);
-	
-	virtual_device = add_device(dev);
-	virtual_device->vhcd_phone = -1;
-	virtual_device->id = 0;
-	
-	return EOK;
-}
-
-/** Disconnects device from HCD.
- *
- * @param dev Device to be disconnected.
- * @return Error code.
- * @retval EOK Device connected.
- * @retval ENOENT This device is not connected.
- */
-int usbvirt_disconnect(usbvirt_device_t *dev)
-{
-	virtual_device_t *virtual_device = find_device(dev);
-	if (virtual_device == NULL) {
-		return ENOENT;
-	}
-	
-	destroy_device(virtual_device);
-	
-	return EOK;
-}
-
-
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/private.h
===================================================================
--- uspace/lib/usbvirt/src/private.h	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ uspace/lib/usbvirt/src/private.h	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -1,94 +1,8 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
+#include <usbvirt/device.h>
 
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Virtual USB private header.
- */
-#ifndef LIBUSBVIRT_PRIVATE_H_
-#define LIBUSBVIRT_PRIVATE_H_
+int process_control_transfer(usbvirt_device_t *,
+    usbvirt_control_request_handler_t *,
+    usb_device_request_setup_packet_t *,
+    uint8_t *, size_t *);
 
-#include <usbvirt/device.h>
-#include <usbvirt/hub.h>
-#include <assert.h>
-
-
-#define DEVICE_HAS_OP(dev, op) \
-	( \
-		(  ((dev)->ops) != NULL  ) \
-		&& \
-		(  ((dev)->ops->op) != NULL  ) \
-	)
-
-int usbvirt_data_to_host(struct usbvirt_device *dev,
-    usb_endpoint_t endpoint, void *buffer, size_t size);
-
-int handle_incoming_data(struct usbvirt_device *dev,
-    usb_endpoint_t endpoint, void *buffer, size_t size);
-
-int control_pipe(usbvirt_device_t *device, usbvirt_control_transfer_t *transfer);
-
-int handle_std_request(usbvirt_device_t *device, usb_device_request_setup_packet_t *request, uint8_t *data);
-
-void device_callback_connection(usbvirt_device_t *device, ipc_callid_t iid, ipc_call_t *icall);
-
-int transaction_setup(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size);
-int transaction_out(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size);
-int transaction_in(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size, size_t *data_size);
-
-
-void user_debug(usbvirt_device_t *device, int level, uint8_t tag,
-    const char *format, ...);
-void lib_debug(usbvirt_device_t *device, int level, uint8_t tag,
-    const char *format, ...);
-    
-static inline const char *str_device_state(usbvirt_device_state_t state)
-{
-	switch (state) {
-		case USBVIRT_STATE_DEFAULT:
-			return "default";
-		case USBVIRT_STATE_ADDRESS:
-			return "address";
-		case USBVIRT_STATE_CONFIGURED:
-			return "configured";
-		default:
-			return "unknown";
-	}
-}
-
-extern usbvirt_control_transfer_handler_t control_pipe_zero_local_handlers[];
-
-#endif
-/**
- * @}
- */
+extern usbvirt_control_request_handler_t library_handlers[];
Index: uspace/lib/usbvirt/src/stdreq.c
===================================================================
--- uspace/lib/usbvirt/src/stdreq.c	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ uspace/lib/usbvirt/src/stdreq.c	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -1,70 +1,44 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
+#include "private.h"
+#include <usb/request.h>
+#include <assert.h>
+#include <errno.h>
 
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Preprocessing of standard device requests.
- */
-#include <errno.h>
-#include <stdlib.h>
-#include <mem.h>
-#include <usb/request.h>
+void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *setup_packet,
+    uint8_t *data, size_t *act_size,
+    void *actual_data, size_t actual_data_size)
+{
+	size_t expected_size = setup_packet->length;
+	if (expected_size < actual_data_size) {
+		actual_data_size = expected_size;
+	}
 
-#include "private.h"
+	memcpy(data, actual_data, actual_data_size);
 
-/*
- * All sub handlers must return EFORWARD to inform the caller that
- * they were not able to process the request (yes, it is abuse of
- * this error code but such error code shall not collide with anything
- * else in this context).
- */
- 
+	if (act_size != NULL) {
+		*act_size = actual_data_size;
+	}
+}
+
 /** GET_DESCRIPTOR handler. */
-static int handle_get_descriptor(usbvirt_device_t *device,
-    usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
+static int req_get_descriptor(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
 {
 	uint8_t type = setup_packet->value_high;
 	uint8_t index = setup_packet->value_low;
 
-	/* 
+	/*
 	 * Standard device descriptor.
 	 */
 	if ((type == USB_DESCTYPE_DEVICE) && (index == 0)) {
 		if (device->descriptors && device->descriptors->device) {
-			return device->control_transfer_reply(device, 0,
+			usbvirt_control_reply_helper(setup_packet, data, act_size,
 			    device->descriptors->device,
 			    device->descriptors->device->length);
+			return EOK;
 		} else {
 			return EFORWARD;
 		}
 	}
-	
+
 	/*
 	 * Configuration descriptor together with interface, endpoint and
@@ -85,5 +59,5 @@
 			return ENOMEM;
 		}
-		
+
 		uint8_t *ptr = all_data;
 		memcpy(ptr, config->descriptor, config->descriptor->length);
@@ -96,19 +70,18 @@
 			ptr += extra->length;
 		}
-		
-		int rc = device->control_transfer_reply(device, 0,
+
+		usbvirt_control_reply_helper(setup_packet, data, act_size,
 		    all_data, config->descriptor->total_length);
-		
+
 		free(all_data);
-		
-		return rc;
+
+		return EOK;
 	}
-	
+
 	return EFORWARD;
 }
 
-/** SET_ADDRESS handler. */
-static int handle_set_address(usbvirt_device_t *device,
-    usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
+static int req_set_address(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
 {
 	uint16_t new_address = setup_packet->value;
@@ -119,17 +92,16 @@
 		return EINVAL;
 	}
-	
+
 	if (new_address > 127) {
 		return EINVAL;
 	}
-	
-	device->new_address = new_address;
-	
+
+	device->address = new_address;
+
 	return EOK;
 }
 
-/** SET_CONFIGURATION handler. */
-static int handle_set_configuration(usbvirt_device_t *device,
-    usb_device_request_setup_packet_t *setup_packet, uint8_t *extra_data)
+static int req_set_configuration(usbvirt_device_t *device,
+    const usb_device_request_setup_packet_t *setup_packet, uint8_t *data, size_t *act_size)
 {
 	uint16_t configuration_value = setup_packet->value;
@@ -140,5 +112,5 @@
 		return EINVAL;
 	}
-	
+
 	/*
 	 * Configuration value is 1 byte information.
@@ -147,5 +119,5 @@
 		return EINVAL;
 	}
-	
+
 	/*
 	 * Do nothing when in default state. According to specification,
@@ -155,60 +127,48 @@
 		return EOK;
 	}
-	
+
+	usbvirt_device_state_t new_state;
 	if (configuration_value == 0) {
-		if (DEVICE_HAS_OP(device, on_state_change)) {
-			device->ops->on_state_change(device, device->state,
-			    USBVIRT_STATE_ADDRESS);
-		}
-		device->state = USBVIRT_STATE_ADDRESS;
+		new_state = USBVIRT_STATE_ADDRESS;
 	} else {
-		/*
-		* TODO: browse provided configurations and verify that
-		* user selected existing configuration.
-		*/
-		if (DEVICE_HAS_OP(device, on_state_change)) {
-			device->ops->on_state_change(device, device->state,
-			    USBVIRT_STATE_CONFIGURED);
-		}
-		device->state = USBVIRT_STATE_CONFIGURED;
-		if (device->descriptors) {
-			device->descriptors->current_configuration
-			    = configuration_value;
-		}
+		// FIXME: check that this configuration exists
+		new_state = USBVIRT_STATE_CONFIGURED;
 	}
-		
+
+	if (device->ops && device->ops->state_changed) {
+		device->ops->state_changed(device, device->state, new_state);
+	}
+	device->state = new_state;
+
 	return EOK;
 }
 
-
-#define MAKE_BM_REQUEST(direction, recipient) \
-	USBVIRT_MAKE_CONTROL_REQUEST_TYPE(direction, \
-	    USBVIRT_REQUEST_TYPE_STANDARD, recipient)
-#define MAKE_BM_REQUEST_DEV(direction) \
-	MAKE_BM_REQUEST(direction, USBVIRT_REQUEST_RECIPIENT_DEVICE)
-
-usbvirt_control_transfer_handler_t control_pipe_zero_local_handlers[] = {
+usbvirt_control_request_handler_t library_handlers[] = {
 	{
-		.request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_IN),
-		.request = USB_DEVREQ_GET_DESCRIPTOR,
-		.name = "GetDescriptor()",
-		.callback = handle_get_descriptor
+		.req_direction = USB_DIRECTION_OUT,
+		.req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
+		.req_type = USB_REQUEST_TYPE_STANDARD,
+		.request = USB_DEVREQ_SET_ADDRESS,
+		.name = "SetAddress",
+		.callback = req_set_address
 	},
 	{
-		.request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_OUT),
-		.request = USB_DEVREQ_SET_ADDRESS,
-		.name = "SetAddress()",
-		.callback = handle_set_address
+		.req_direction = USB_DIRECTION_IN,
+		.req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
+		.req_type = USB_REQUEST_TYPE_STANDARD,
+		.request = USB_DEVREQ_GET_DESCRIPTOR,
+		.name = "GetDescriptor",
+		.callback = req_get_descriptor
 	},
 	{
-		.request_type = MAKE_BM_REQUEST_DEV(USB_DIRECTION_OUT),
+		.req_direction = USB_DIRECTION_OUT,
+		.req_recipient = USB_REQUEST_RECIPIENT_DEVICE,
+		.req_type = USB_REQUEST_TYPE_STANDARD,
 		.request = USB_DEVREQ_SET_CONFIGURATION,
-		.name = "SetConfiguration()",
-		.callback = handle_set_configuration
+		.name = "SetConfiguration",
+		.callback = req_set_configuration
 	},
-	USBVIRT_CONTROL_TRANSFER_HANDLER_LAST
+
+	{ .callback = NULL }
 };
 
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/transaction.c
===================================================================
--- uspace/lib/usbvirt/src/transaction.c	(revision 956e8ea602f9ec96e375b369f421330a0630e162)
+++ 	(revision )
@@ -1,268 +1,0 @@
-/*
- * Copyright (c) 2010 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbvirt
- * @{
- */
-/** @file
- * @brief Transaction processing.
- */
-#include <errno.h>
-#include <stdlib.h>
-#include <mem.h>
-
-#include "private.h"
-
-static usb_direction_t setup_transaction_direction(usbvirt_device_t *,
-    usb_endpoint_t, void *, size_t);
-static void process_control_transfer(usbvirt_device_t *,
-    usb_endpoint_t, usbvirt_control_transfer_t *);
-
-/** Convert virtual USB transaction type to string.
- */
-const char *usbvirt_str_transaction_type(usbvirt_transaction_type_t type)
-{
-	switch (type) {
-		case USBVIRT_TRANSACTION_SETUP:
-			return "setup";
-		case USBVIRT_TRANSACTION_IN:
-			return "in";
-		case USBVIRT_TRANSACTION_OUT:
-			return "out";
-		default:
-			return "unknown";
-	}
-}
-
-/** SETUP transaction handling.
- * The setup transaction only prepares control transfer on given endpoint.
- */
-int transaction_setup(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size)
-{
-	device->lib_debug(device, 1, USBVIRT_DEBUGTAG_TRANSACTION,
-	    "setup transaction: endpoint=%d, size=%u", endpoint, size);
-	
-	usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
-	
-	if (transfer->request != NULL) {
-		free(transfer->request);
-	}
-	if (transfer->data != NULL) {
-		free(transfer->data);
-	}
-	
-	transfer->direction = setup_transaction_direction(device, endpoint,
-	    buffer, size);
-	transfer->request = malloc(size);
-	memcpy(transfer->request, buffer, size);
-	transfer->request_size = size;
-	transfer->data = NULL;
-	transfer->data_size = 0;
-	
-	if (transfer->direction == USB_DIRECTION_IN) {
-		process_control_transfer(device, endpoint, transfer);
-	}
-	
-	return EOK;
-}
-
-/** OUT transaction handling.
- * The OUT transaction can trigger processing of a control transfer.
- */
-int transaction_out(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size)
-{
-	device->lib_debug(device, 1, USBVIRT_DEBUGTAG_TRANSACTION,
-	    "out transaction: endpoint=%d, size=%u", endpoint, size);
-	
-	/*
-	 * First check whether it is a transaction over control pipe.
-	 */
-	usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
-	if (transfer->request != NULL) {
-		if (transfer->direction == USB_DIRECTION_OUT) {
-			/*
-			 * For out transactions, append the data to the buffer.
-			 */
-			uint8_t *new_buffer = (uint8_t *) malloc(transfer->data_size + size);
-			if (transfer->data) {
-				memcpy(new_buffer, transfer->data, transfer->data_size);
-			}
-			memcpy(new_buffer + transfer->data_size, buffer, size);
-			
-			if (transfer->data) {
-				free(transfer->data);
-			}
-			transfer->data = new_buffer;
-			transfer->data_size += size;
-		} else {
-			/*
-			 * For in transactions, this means end of the
-			 * transaction.
-			 */
-			free(transfer->request);
-			if (transfer->data) {
-				free(transfer->data);
-			}
-			transfer->request = NULL;
-			transfer->request_size = 0;
-			transfer->data = NULL;
-			transfer->data_size = 0;
-		}
-		
-		return EOK;
-	}
-	
-	/*
-	 * Otherwise, announce that some data has come.
-	 */
-	if (device->ops && device->ops->on_data) {
-		return device->ops->on_data(device, endpoint, buffer, size);
-	} else {
-		return ENOTSUP;
-	}
-}
-
-/** IN transaction handling.
- * The IN transaction can trigger processing of a control transfer.
- */
-int transaction_in(usbvirt_device_t *device, usb_endpoint_t endpoint,
-    void *buffer, size_t size, size_t *data_size)
-{
-	device->lib_debug(device, 1, USBVIRT_DEBUGTAG_TRANSACTION,
-	    "in transaction: endpoint=%d, size=%u", endpoint, size);
-	
-	/*
-	 * First check whether it is a transaction over control pipe.
-	 */
-	usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
-	if (transfer->request != NULL) {
-		if (transfer->direction == USB_DIRECTION_OUT) {
-			/*
-			 * This means end of input data.
-			 */
-			process_control_transfer(device, endpoint, transfer);
-		} else {
-			/*
-			 * For in transactions, this means sending next part
-			 * of the buffer.
-			 */
-			// FIXME: handle when the HC wants the data back
-			// in more chunks
-			size_t actual_size = 0;
-			if (transfer->data) {
-				actual_size = transfer->data_size;
-			}
-			if (actual_size > size) {
-				actual_size = size;
-			}
-			device->lib_debug(device, 1, USBVIRT_DEBUGTAG_TRANSACTION,
-			    "in transaction: will copy %zu bytes", actual_size);
-			if (actual_size > 0) {
-				memcpy(buffer, transfer->data, actual_size);
-				if (data_size) {
-					*data_size = actual_size;
-				}
-			}
-		}
-		
-		return EOK;
-	}
-	
-	if (size == 0) {
-		return EINVAL;
-	}
-	
-	int rc = 1;
-	
-	if (device->ops && device->ops->on_data_request) {
-		rc = device->ops->on_data_request(device, endpoint, buffer, size, data_size);
-	}
-	
-	return rc;
-}
-
-/** Determine direction of control transfer.
- * First, try the user provided callback, otherwise guess, believing that
- * it uses the same format as control pipe 0.
- */
-static usb_direction_t setup_transaction_direction(usbvirt_device_t *device,
-    usb_endpoint_t endpoint,
-    void *data, size_t size)
-{
-	int direction = -1;
-	if (device->ops && device->ops->decide_control_transfer_direction) {
-		direction = device->ops->decide_control_transfer_direction(endpoint,
-		    data, size);
-	}
-	
-	/*
-	 * If the user-supplied routine have not handled the direction
-	 * (or simply was not provided) we will guess, hoping that it 
-	 * uses same format as standard request on control pipe zero.
-	 */
-	if (direction < 0) {
-		if (size > 0) {
-			uint8_t *ptr = (uint8_t *) data;
-			if ((ptr[0] & 128) == 128) {
-				direction = USB_DIRECTION_IN;
-			} else {
-				direction = USB_DIRECTION_OUT;
-			}
-		} else {
-			/* This shall not happen anyway. */
-			direction = USB_DIRECTION_OUT;
-		}
-	}
-	
-	return (usb_direction_t) direction;
-}
-
-/** Process control transfer.
- */
-static void process_control_transfer(usbvirt_device_t *device,
-    usb_endpoint_t endpoint,
-    usbvirt_control_transfer_t *transfer)
-{
-	int rc = EFORWARD;
-	
-	if (device->ops && device->ops->on_control_transfer) {
-		rc = device->ops->on_control_transfer(device, endpoint, transfer);
-	}
-	
-	if (rc == EFORWARD) {
-		if (endpoint == 0) {
-			rc = control_pipe(device, transfer);
-		}
-	}
-}
-
-/**
- * @}
- */
Index: uspace/lib/usbvirt/src/transfer.c
===================================================================
--- uspace/lib/usbvirt/src/transfer.c	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
+++ uspace/lib/usbvirt/src/transfer.c	(revision e1dbcbca6b5c296804e71b581b96149f7f3911b1)
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2011 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libusbvirt
+ * @{
+ */
+/** @file
+ *
+ */
+#include <usbvirt/device.h>
+#include <usb/debug.h>
+#include <errno.h>
+#include <assert.h>
+#include "private.h"
+
+static int usbvirt_control_transfer(usbvirt_device_t *dev,
+    void *setup, size_t setup_size,
+    void *data, size_t data_size, size_t *data_size_sent)
+{
+	assert(dev);
+	assert(dev->ops);
+
+	if (setup_size != sizeof(usb_device_request_setup_packet_t)) {
+		return ESTALL;
+	}
+	usb_device_request_setup_packet_t *setup_packet = setup;
+	if (data_size != setup_packet->length) {
+		return ESTALL;
+	}
+
+	int rc;
+
+	/* Run user handler first. */
+	rc = process_control_transfer(dev, dev->ops->control,
+	    setup_packet, data, data_size_sent);
+	if (rc != EFORWARD) {
+		return rc;
+	}
+
+	/* Run the library handlers afterwards. */
+	rc = process_control_transfer(dev, library_handlers,
+	    setup_packet, data, data_size_sent);
+
+	if (rc == EFORWARD) {
+		usb_log_warning("Control transfer {%s (%s)} not handled.\n",
+		    usb_debug_str_buffer(setup, setup_size, 10),
+		    setup_packet->request_type & 0x80
+		    ? "IN" : usb_debug_str_buffer(data, data_size, 10));
+		rc = EBADCHECKSUM;
+	}
+
+	return rc;
+}
+
+int usbvirt_control_write(usbvirt_device_t *dev, void *setup, size_t setup_size,
+    void *data, size_t data_size)
+{
+	return usbvirt_control_transfer(dev, setup, setup_size,
+	    data, data_size, NULL);
+}
+
+int usbvirt_control_read(usbvirt_device_t *dev, void *setup, size_t setup_size,
+    void *data, size_t data_size, size_t *data_size_sent)
+{
+	return usbvirt_control_transfer(dev, setup, setup_size,
+	    data, data_size, data_size_sent);
+}
+
+int usbvirt_data_out(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
+    usb_endpoint_t endpoint, void *data, size_t data_size)
+{
+	if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
+		return ERANGE;
+	}
+	if ((dev->ops == NULL) || (dev->ops->data_out[endpoint] == NULL)) {
+		return ENOTSUP;
+	}
+
+	int rc = dev->ops->data_out[endpoint](dev, endpoint, transf_type,
+	    data, data_size);
+
+	return rc;
+}
+
+int usbvirt_data_in(usbvirt_device_t *dev, usb_transfer_type_t transf_type,
+    usb_endpoint_t endpoint, void *data, size_t data_size, size_t *data_size_sent)
+{
+	if ((endpoint <= 0) || (endpoint >= USBVIRT_ENDPOINT_MAX)) {
+		return ERANGE;
+	}
+	if ((dev->ops == NULL) || (dev->ops->data_in[endpoint] == NULL)) {
+		return ENOTSUP;
+	}
+
+	size_t data_size_sent_tmp;
+	int rc = dev->ops->data_in[endpoint](dev, endpoint, transf_type,
+	    data, data_size, &data_size_sent_tmp);
+
+	if (rc != EOK) {
+		return rc;
+	}
+
+	if (data_size_sent != NULL) {
+		*data_size_sent = data_size_sent_tmp;
+	}
+
+	return EOK;
+}
+
+/**
+ * @}
+ */
