Index: uspace/lib/usbhost/src/ddf_helpers.c
===================================================================
--- uspace/lib/usbhost/src/ddf_helpers.c	(revision 3f03199e0835b11c26a46cd190aeef79835d08f4)
+++ uspace/lib/usbhost/src/ddf_helpers.c	(revision 3f03199e0835b11c26a46cd190aeef79835d08f4)
@@ -0,0 +1,641 @@
+/*
+ * Copyright (c) 2013 Jan Vesely
+ * 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 libusbhost
+ * @{
+ */
+/** @file
+ *
+ */
+
+#include <usb_iface.h>
+#include <usb/classes/classes.h>
+#include <usb/debug.h>
+#include <usb/descriptor.h>
+#include <usb/request.h>
+#include <errno.h>
+#include <str_error.h>
+
+#include "ddf_helpers.h"
+
+#define CTRL_PIPE_MIN_PACKET_SIZE 8
+
+typedef struct usb_dev {
+	link_t link;
+	list_t devices;
+	fibril_mutex_t guard;
+	ddf_fun_t *fun;
+	usb_address_t address;
+	usb_speed_t speed;
+	usb_address_t tt_address;
+	unsigned port;
+} usb_dev_t;
+
+typedef struct hc_dev {
+	ddf_fun_t *ctl_fun;
+	hcd_t hcd;
+	usb_dev_t *root_hub;
+} hc_dev_t;
+
+static hc_dev_t *dev_to_hc_dev(ddf_dev_t *dev)
+{
+	return ddf_dev_data_get(dev);
+}
+
+hcd_t *dev_to_hcd(ddf_dev_t *dev)
+{
+	hc_dev_t *hc_dev = dev_to_hc_dev(dev);
+	if (!hc_dev) {
+		usb_log_error("Invalid HCD device.\n");
+		return NULL;
+	}
+	return &hc_dev->hcd;
+}
+
+
+static int hcd_ddf_new_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port);
+static int hcd_ddf_remove_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port);
+
+
+/* DDF INTERFACE */
+
+/** Register endpoint interface function.
+ * @param fun DDF function.
+ * @param address USB address of the device.
+ * @param endpoint USB endpoint number to be registered.
+ * @param transfer_type Endpoint's transfer type.
+ * @param direction USB communication direction the endpoint is capable of.
+ * @param max_packet_size Maximu size of packets the endpoint accepts.
+ * @param interval Preferred timeout between communication.
+ * @return Error code.
+ */
+static int register_endpoint(
+    ddf_fun_t *fun, usb_endpoint_t endpoint,
+    usb_transfer_type_t transfer_type, usb_direction_t direction,
+    size_t max_packet_size, unsigned interval)
+{
+	assert(fun);
+	hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
+	usb_dev_t *dev = ddf_fun_data_get(fun);
+	assert(hcd);
+	assert(dev);
+	const size_t size = max_packet_size;
+	const usb_target_t target =
+	    {{.address = dev->address, .endpoint = endpoint}};
+
+	usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
+	    dev->address, endpoint, usb_str_transfer_type(transfer_type),
+	    usb_str_direction(direction), max_packet_size, interval);
+
+	return hcd_add_ep(hcd, target, direction, transfer_type,
+	    max_packet_size, size, dev->tt_address, dev->port);
+}
+
+/** Unregister endpoint interface function.
+ * @param fun DDF function.
+ * @param address USB address of the endpoint.
+ * @param endpoint USB endpoint number.
+ * @param direction Communication direction of the enpdoint to unregister.
+ * @return Error code.
+ */
+static int unregister_endpoint(
+    ddf_fun_t *fun, usb_endpoint_t endpoint, usb_direction_t direction)
+{
+	assert(fun);
+	hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
+	usb_dev_t *dev = ddf_fun_data_get(fun);
+	assert(hcd);
+	assert(dev);
+	const usb_target_t target =
+	    {{.address = dev->address, .endpoint = endpoint}};
+	usb_log_debug("Unregister endpoint %d:%d %s.\n",
+	    dev->address, endpoint, usb_str_direction(direction));
+	return hcd_remove_ep(hcd, target, direction);
+}
+
+static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
+{
+	assert(fun);
+	hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
+	usb_dev_t *dev = ddf_fun_data_get(fun);
+	assert(hcd);
+	assert(dev);
+
+	usb_log_debug("Device %d requested default address at %s speed\n",
+	    dev->address, usb_str_speed(speed));
+	return hcd_reserve_default_address(hcd, speed);
+}
+
+static int release_default_address(ddf_fun_t *fun)
+{
+	assert(fun);
+	hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
+	usb_dev_t *dev = ddf_fun_data_get(fun);
+	assert(hcd);
+	assert(dev);
+
+	usb_log_debug("Device %d released default address\n", dev->address);
+	return hcd_release_default_address(hcd);
+}
+
+static int device_enumerate(ddf_fun_t *fun, unsigned port)
+{
+	assert(fun);
+	ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
+	usb_dev_t *dev = ddf_fun_data_get(fun);
+	assert(ddf_dev);
+	assert(dev);
+	usb_log_debug("Hub %d reported a new USB device on port: %u\n",
+	    dev->address, port);
+	return hcd_ddf_new_device(ddf_dev, dev, port);
+}
+
+static int device_remove(ddf_fun_t *fun, unsigned port)
+{
+	assert(fun);
+	ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
+	usb_dev_t *dev = ddf_fun_data_get(fun);
+	assert(ddf_dev);
+	assert(dev);
+	usb_log_debug("Hub `%s' reported removal of device on port %u\n",
+	    ddf_fun_get_name(fun), port);
+	return hcd_ddf_remove_device(ddf_dev, dev, port);
+}
+
+/** Gets handle of the respective device.
+ *
+ * @param[in] fun Device function.
+ * @param[out] handle Place to write the handle.
+ * @return Error code.
+ */
+static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
+{
+	assert(fun);
+	if (handle)
+		*handle = ddf_fun_get_handle(fun);
+	return EOK;
+}
+
+/** Inbound communication interface function.
+ * @param fun DDF function.
+ * @param target Communication target.
+ * @param setup_data Data to use in setup stage (control transfers).
+ * @param data Pointer to data buffer.
+ * @param size Size of the data buffer.
+ * @param callback Function to call on communication end.
+ * @param arg Argument passed to the callback function.
+ * @return Error code.
+ */
+static int dev_read(ddf_fun_t *fun, usb_endpoint_t endpoint,
+    uint64_t setup_data, uint8_t *data, size_t size,
+    usbhc_iface_transfer_in_callback_t callback, void *arg)
+{
+	assert(fun);
+	usb_dev_t *usb_dev = ddf_fun_data_get(fun);
+	assert(usb_dev);
+	const usb_target_t target = {{
+	    .address =  usb_dev->address,
+	    .endpoint = endpoint,
+	}};
+	return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)), target,
+	    USB_DIRECTION_IN, data, size, setup_data, callback, NULL, arg,
+	    "READ");
+}
+
+/** Outbound communication interface function.
+ * @param fun DDF function.
+ * @param target Communication target.
+ * @param setup_data Data to use in setup stage (control transfers).
+ * @param data Pointer to data buffer.
+ * @param size Size of the data buffer.
+ * @param callback Function to call on communication end.
+ * @param arg Argument passed to the callback function.
+ * @return Error code.
+ */
+static int dev_write(ddf_fun_t *fun, usb_endpoint_t endpoint,
+    uint64_t setup_data, const uint8_t *data, size_t size,
+    usbhc_iface_transfer_out_callback_t callback, void *arg)
+{
+	assert(fun);
+	usb_dev_t *usb_dev = ddf_fun_data_get(fun);
+	assert(usb_dev);
+	const usb_target_t target = {{
+	    .address =  usb_dev->address,
+	    .endpoint = endpoint,
+	}};
+	return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)),
+	    target, USB_DIRECTION_OUT, (uint8_t*)data, size, setup_data, NULL,
+	    callback, arg, "WRITE");
+}
+
+/** USB device interface */
+static usb_iface_t usb_iface = {
+	.get_my_device_handle = get_my_device_handle,
+
+	.reserve_default_address = reserve_default_address,
+	.release_default_address = release_default_address,
+
+	.device_enumerate = device_enumerate,
+	.device_remove = device_remove,
+
+	.register_endpoint = register_endpoint,
+	.unregister_endpoint = unregister_endpoint,
+
+	.read = dev_read,
+	.write = dev_write,
+};
+
+/** Standard USB device interface) */
+static ddf_dev_ops_t usb_ops = {
+	.interfaces[USB_DEV_IFACE] = &usb_iface,
+};
+
+
+/* DDF HELPERS */
+
+#define GET_DEVICE_DESC(size) \
+{ \
+	.request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST \
+	    | (USB_REQUEST_TYPE_STANDARD << 5) \
+	    | USB_REQUEST_RECIPIENT_DEVICE, \
+	.request = USB_DEVREQ_GET_DESCRIPTOR, \
+	.value = uint16_host2usb(USB_DESCTYPE_DEVICE << 8), \
+	.index = uint16_host2usb(0), \
+	.length = uint16_host2usb(size), \
+};
+
+#define SET_ADDRESS(address) \
+{ \
+	.request_type = SETUP_REQUEST_TYPE_HOST_TO_DEVICE \
+	    | (USB_REQUEST_TYPE_STANDARD << 5) \
+	    | USB_REQUEST_RECIPIENT_DEVICE, \
+	.request = USB_DEVREQ_SET_ADDRESS, \
+	.value = uint16_host2usb(address), \
+	.index = uint16_host2usb(0), \
+	.length = uint16_host2usb(0), \
+};
+
+static int hcd_ddf_add_device(ddf_dev_t *parent, usb_dev_t *hub_dev,
+    unsigned port, usb_address_t address, usb_speed_t speed, const char *name,
+    const match_id_list_t *mids)
+{
+	assert(parent);
+
+	char default_name[10] = { 0 }; /* usbxyz-ss */
+	if (!name) {
+		snprintf(default_name, sizeof(default_name) - 1,
+		    "usb%u-%cs", address, usb_str_speed(speed)[0]);
+		name = default_name;
+	}
+
+	ddf_fun_t *fun = ddf_fun_create(parent, fun_inner, name);
+	if (!fun)
+		return ENOMEM;
+	usb_dev_t *info = ddf_fun_data_alloc(fun, sizeof(usb_dev_t));
+	if (!info) {
+		ddf_fun_destroy(fun);
+		return ENOMEM;
+	}
+	info->address = address;
+	info->speed = speed;
+	info->fun = fun;
+	info->port = port;
+	info->tt_address = hub_dev ? hub_dev->tt_address : -1;
+	link_initialize(&info->link);
+	list_initialize(&info->devices);
+	fibril_mutex_initialize(&info->guard);
+
+	if (hub_dev && hub_dev->speed == USB_SPEED_HIGH && usb_speed_is_11(speed))
+		info->tt_address = hub_dev->address;
+
+	ddf_fun_set_ops(fun, &usb_ops);
+	list_foreach(mids->ids, link, const match_id_t, mid) {
+		ddf_fun_add_match_id(fun, mid->id, mid->score);
+	}
+
+	int ret = ddf_fun_bind(fun);
+	if (ret != EOK) {
+		ddf_fun_destroy(fun);
+		return ret;
+	}
+
+	if (hub_dev) {
+		fibril_mutex_lock(&hub_dev->guard);
+		list_append(&info->link, &hub_dev->devices);
+		fibril_mutex_unlock(&hub_dev->guard);
+	} else {
+		hc_dev_t *hc_dev = dev_to_hc_dev(parent);
+		assert(hc_dev->root_hub == NULL);
+		hc_dev->root_hub = info;
+	}
+	return EOK;
+}
+
+#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
+do { \
+	match_id_t *mid = malloc(sizeof(match_id_t)); \
+	if (!mid) { \
+		clean_match_ids(list); \
+		return ENOMEM; \
+	} \
+	char *id = NULL; \
+	int ret = asprintf(&id, str, ##__VA_ARGS__); \
+	if (ret < 0) { \
+		clean_match_ids(list); \
+		free(mid); \
+		return ENOMEM; \
+	} \
+	mid->score = sc; \
+	mid->id = id; \
+	add_match_id(list, mid); \
+} while (0)
+
+/* This is a copy of lib/usbdev/src/recognise.c */
+static int create_match_ids(match_id_list_t *l,
+    usb_standard_device_descriptor_t *d)
+{
+	assert(l);
+	assert(d);
+	
+	if (d->vendor_id != 0) {
+		/* First, with release number. */
+		ADD_MATCHID_OR_RETURN(l, 100,
+		    "usb&vendor=%#04x&product=%#04x&release=%x.%x",
+		    d->vendor_id, d->product_id, (d->device_version >> 8),
+		    (d->device_version & 0xff));
+	
+		/* Next, without release number. */
+		ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
+		    d->vendor_id, d->product_id);
+	}
+
+	/* Class match id */
+	ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
+	    usb_str_class(d->device_class));
+
+	/* As a last resort, try fallback driver. */
+	ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
+
+	return EOK;
+
+}
+
+static int hcd_ddf_remove_device(ddf_dev_t *device, usb_dev_t *hub,
+    unsigned port)
+{
+	assert(device);
+
+	hcd_t *hcd = dev_to_hcd(device);
+	assert(hcd);
+
+	hc_dev_t *hc_dev = dev_to_hc_dev(device);
+	assert(hc_dev);
+
+	fibril_mutex_lock(&hub->guard);
+
+	usb_dev_t *victim = NULL;
+
+	list_foreach(hub->devices, link, usb_dev_t, it) {
+		if (it->port == port) {
+			victim = it;
+			break;
+		}
+	}
+	if (victim && victim->port == port) {
+		list_remove(&victim->link);
+		fibril_mutex_unlock(&hub->guard);
+		const int ret = ddf_fun_unbind(victim->fun);
+		if (ret == EOK) {
+			ddf_fun_destroy(victim->fun);
+			hcd_release_address(hcd, victim->address);
+		} else {
+			usb_log_warning("Failed to unbind device `%s': %s\n",
+			    ddf_fun_get_name(victim->fun), str_error(ret));
+		}
+		return EOK;
+	}
+	return ENOENT;
+}
+
+static int hcd_ddf_new_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port)
+{
+	assert(device);
+
+	hcd_t *hcd = dev_to_hcd(device);
+	assert(hcd);
+
+	usb_speed_t speed = USB_SPEED_MAX;
+
+	/* This checks whether the default address is reserved and gets speed */
+	int ret = usb_endpoint_manager_get_speed(&hcd->ep_manager,
+		USB_ADDRESS_DEFAULT, &speed);
+	if (ret != EOK) {
+		return ret;
+	}
+
+	static const usb_target_t default_target = {{
+		.address = USB_ADDRESS_DEFAULT,
+		.endpoint = 0,
+	}};
+
+	const usb_address_t address = hcd_request_address(hcd, speed);
+	if (address < 0)
+		return address;
+
+	const usb_target_t target = {{
+		.address = address,
+		.endpoint = 0,
+	}};
+
+	const usb_address_t tt_address = hub ? hub->tt_address : -1;
+
+	/* Add default pipe on default address */
+	ret = hcd_add_ep(hcd,
+	    default_target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
+	    CTRL_PIPE_MIN_PACKET_SIZE, CTRL_PIPE_MIN_PACKET_SIZE,
+	    tt_address, port);
+
+	if (ret != EOK) {
+		hcd_release_address(hcd, address);
+		return ret;
+	}
+
+	/* Get max packet size for default pipe */
+	usb_standard_device_descriptor_t desc = { 0 };
+	static const usb_device_request_setup_packet_t get_device_desc_8 =
+	    GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
+
+	// TODO CALLBACKS
+	ssize_t got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_IN,
+	    &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
+	    "read first 8 bytes of dev descriptor");
+
+	if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
+		hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
+		hcd_release_address(hcd, address);
+		return got < 0 ? got : EOVERFLOW;
+	}
+
+	/* Register EP on the new address */
+	ret = hcd_add_ep(hcd, target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
+	    desc.max_packet_size, desc.max_packet_size, tt_address, port);
+	if (ret != EOK) {
+		hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
+		hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
+		hcd_release_address(hcd, address);
+		return ret;
+	}
+
+	/* Set new address */
+	const usb_device_request_setup_packet_t set_address =
+	    SET_ADDRESS(target.address);
+
+	got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_OUT,
+	    NULL, 0, *(uint64_t *)&set_address, "set address");
+
+	hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
+
+	if (got != 0) {
+		hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
+		hcd_release_address(hcd, address);
+		return got;
+	}
+	
+	/* Get std device descriptor */
+	static const usb_device_request_setup_packet_t get_device_desc =
+	    GET_DEVICE_DESC(sizeof(desc));
+
+	got = hcd_send_batch_sync(hcd, target, USB_DIRECTION_IN,
+	    &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
+	    "read device descriptor");
+	if (ret != EOK) {
+		hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
+		hcd_release_address(hcd, target.address);
+		return got < 0 ? got : EOVERFLOW;
+	}
+
+	/* Create match ids from the device descriptor */
+	match_id_list_t mids;
+	init_match_ids(&mids);
+
+	ret = create_match_ids(&mids, &desc);
+	if (ret != EOK) {
+		hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
+		hcd_release_address(hcd, target.address);
+		return ret;
+	}
+
+	/* Register device */
+	ret = hcd_ddf_add_device(device, hub, port, address, speed, NULL, &mids);
+	clean_match_ids(&mids);
+	if (ret != EOK) {
+		hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
+		hcd_release_address(hcd, target.address);
+	}
+
+	return ret;
+}
+
+/** Announce root hub to the DDF
+ *
+ * @param[in] device Host controller ddf device
+ * @return Error code
+ */
+int hcd_ddf_setup_root_hub(ddf_dev_t *device)
+{
+	assert(device);
+	hcd_t *hcd = dev_to_hcd(device);
+	assert(hcd);
+
+	hcd_reserve_default_address(hcd, hcd->ep_manager.max_speed);
+	const int ret = hcd_ddf_new_device(device, NULL, 0);
+	hcd_release_default_address(hcd);
+	return ret;
+}
+
+/** Initialize hc structures.
+ *
+ * @param[in] device DDF instance of the device to use.
+ *
+ * This function does all the ddf work for hc driver.
+ */
+int hcd_ddf_setup_hc(ddf_dev_t *device, usb_speed_t max_speed,
+    size_t bw, bw_count_func_t bw_count)
+{
+	assert(device);
+
+	hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
+	if (instance == NULL) {
+		usb_log_error("Failed to allocate HCD ddf structure.\n");
+		return ENOMEM;
+	}
+	instance->root_hub = NULL;
+	hcd_init(&instance->hcd, max_speed, bw, bw_count);
+
+	int ret = ENOMEM;
+	instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
+	if (!instance->ctl_fun) {
+		usb_log_error("Failed to create HCD ddf fun.\n");
+		goto err_destroy_fun;
+	}
+
+	ret = ddf_fun_bind(instance->ctl_fun);
+	if (ret != EOK) {
+		usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
+		goto err_destroy_fun;
+	}
+
+	ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
+	if (ret != EOK) {
+		usb_log_error("Failed to add fun to category: %s.\n",
+		    str_error(ret));
+		ddf_fun_unbind(instance->ctl_fun);
+		goto err_destroy_fun;
+	}
+
+	/* HC should be ok at this point (except it can't do anything) */
+	return EOK;
+
+err_destroy_fun:
+	ddf_fun_destroy(instance->ctl_fun);
+	instance->ctl_fun = NULL;
+	return ret;
+}
+
+void hcd_ddf_clean_hc(ddf_dev_t *device)
+{
+	assert(device);
+	hc_dev_t *hc = dev_to_hc_dev(device);
+	assert(hc);
+	const int ret = ddf_fun_unbind(hc->ctl_fun);
+	if (ret == EOK)
+		ddf_fun_destroy(hc->ctl_fun);
+}
+/**
+ * @}
+ */
Index: uspace/lib/usbhost/src/endpoint.c
===================================================================
--- uspace/lib/usbhost/src/endpoint.c	(revision feeac0d8cd6e0ce736779bc5f1dc701c5cf42ba6)
+++ uspace/lib/usbhost/src/endpoint.c	(revision 3f03199e0835b11c26a46cd190aeef79835d08f4)
@@ -50,5 +50,5 @@
 endpoint_t * endpoint_create(usb_address_t address, usb_endpoint_t endpoint,
     usb_direction_t direction, usb_transfer_type_t type, usb_speed_t speed,
-    size_t max_packet_size, size_t bw)
+    size_t max_packet_size, size_t bw, usb_address_t tt_address, unsigned tt_p)
 {
 	endpoint_t *instance = malloc(sizeof(endpoint_t));
@@ -63,4 +63,6 @@
 		instance->toggle = 0;
 		instance->active = false;
+		instance->tt.address = tt_address;
+		instance->tt.port = tt_p;
 		instance->hc_data.data = NULL;
 		instance->hc_data.toggle_get = NULL;
@@ -109,9 +111,5 @@
 {
 	assert(instance);
-	fibril_mutex_lock(&instance->guard);
-	instance->hc_data.data = NULL;
-	instance->hc_data.toggle_get = NULL;
-	instance->hc_data.toggle_set = NULL;
-	fibril_mutex_unlock(&instance->guard);
+	endpoint_set_hc_data(instance, NULL, NULL, NULL);
 }
 
Index: uspace/lib/usbhost/src/hcd.c
===================================================================
--- uspace/lib/usbhost/src/hcd.c	(revision 3f03199e0835b11c26a46cd190aeef79835d08f4)
+++ uspace/lib/usbhost/src/hcd.c	(revision 3f03199e0835b11c26a46cd190aeef79835d08f4)
@@ -0,0 +1,297 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * 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 libusbhost
+ * @{
+ */
+/** @file
+ *
+ */
+
+#include <errno.h>
+#include <str_error.h>
+#include <usb_iface.h>
+#include <usb/debug.h>
+#include <usb/request.h>
+
+#include "hcd.h"
+
+/** Calls ep_add_hook upon endpoint registration.
+ * @param ep Endpoint to be registered.
+ * @param arg hcd_t in disguise.
+ * @return Error code.
+ */
+static int register_helper(endpoint_t *ep, void *arg)
+{
+	hcd_t *hcd = arg;
+	assert(ep);
+	assert(hcd);
+	if (hcd->ep_add_hook)
+		return hcd->ep_add_hook(hcd, ep);
+	return EOK;
+}
+
+/** Calls ep_remove_hook upon endpoint removal.
+ * @param ep Endpoint to be unregistered.
+ * @param arg hcd_t in disguise.
+ */
+static void unregister_helper(endpoint_t *ep, void *arg)
+{
+	hcd_t *hcd = arg;
+	assert(ep);
+	assert(hcd);
+	if (hcd->ep_remove_hook)
+		hcd->ep_remove_hook(hcd, ep);
+}
+
+/** Calls ep_remove_hook upon endpoint removal. Prints warning.
+ *  * @param ep Endpoint to be unregistered.
+ *   * @param arg hcd_t in disguise.
+ *    */
+static void unregister_helper_warn(endpoint_t *ep, void *arg)
+{
+        assert(ep);
+        usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
+            ep->address, ep->endpoint, usb_str_direction(ep->direction));
+	unregister_helper(ep, arg);
+}
+
+
+/** Initialize hcd_t structure.
+ * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
+ *
+ * @param hcd hcd_t structure to initialize, non-null.
+ * @param max_speed Maximum supported USB speed (full, high).
+ * @param bandwidth Available bandwidth, passed to endpoint manager.
+ * @param bw_count Bandwidth compute function, passed to endpoint manager.
+ */
+void hcd_init(hcd_t *hcd, usb_speed_t max_speed, size_t bandwidth,
+    bw_count_func_t bw_count)
+{
+	assert(hcd);
+	usb_endpoint_manager_init(&hcd->ep_manager, bandwidth, bw_count, max_speed);
+
+	hcd->private_data = NULL;
+	hcd->schedule = NULL;
+	hcd->ep_add_hook = NULL;
+	hcd->ep_remove_hook = NULL;
+}
+
+usb_address_t hcd_request_address(hcd_t *hcd, usb_speed_t speed)
+{
+	assert(hcd);
+	usb_address_t address = 0;
+	const int ret = usb_endpoint_manager_request_address(
+	    &hcd->ep_manager, &address, false, speed);
+	if (ret != EOK)
+		return ret;
+	return address;
+}
+
+int hcd_release_address(hcd_t *hcd, usb_address_t address)
+{
+	assert(hcd);
+	return usb_endpoint_manager_remove_address(&hcd->ep_manager, address,
+	    unregister_helper_warn, hcd);
+}
+
+int hcd_reserve_default_address(hcd_t *hcd, usb_speed_t speed)
+{
+	assert(hcd);
+	usb_address_t address = 0;
+	return usb_endpoint_manager_request_address(
+	    &hcd->ep_manager, &address, true, speed);
+}
+
+int hcd_add_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir,
+    usb_transfer_type_t type, size_t max_packet_size, size_t size,
+    usb_address_t tt_address, unsigned tt_port)
+{
+	assert(hcd);
+	return usb_endpoint_manager_add_ep(&hcd->ep_manager, target.address,
+	    target.endpoint, dir, type, max_packet_size, size, register_helper,
+	    hcd, tt_address, tt_port);
+}
+
+int hcd_remove_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir)
+{
+	assert(hcd);
+	return usb_endpoint_manager_remove_ep(&hcd->ep_manager, target.address,
+	    target.endpoint, dir, unregister_helper, hcd);
+}
+
+
+typedef struct {
+	void *original_data;
+	usbhc_iface_transfer_out_callback_t original_callback;
+	usb_target_t target;
+	hcd_t *hcd;
+} toggle_t;
+
+static void toggle_reset_callback(int retval, void *arg)
+{
+	assert(arg);
+	toggle_t *toggle = arg;
+	if (retval == EOK) {
+		usb_log_debug2("Reseting toggle on %d:%d.\n",
+		    toggle->target.address, toggle->target.endpoint);
+		usb_endpoint_manager_reset_toggle(&toggle->hcd->ep_manager,
+		    toggle->target, toggle->target.endpoint == 0);
+	}
+
+	toggle->original_callback(retval, toggle->original_data);
+}
+
+/** Prepare generic usb_transfer_batch and schedule it.
+ * @param hcd Host controller driver.
+ * @param fun DDF fun
+ * @param target address and endpoint number.
+ * @param setup_data Data to use in setup stage (Control communication type)
+ * @param in Callback for device to host communication.
+ * @param out Callback for host to device communication.
+ * @param arg Callback parameter.
+ * @param name Communication identifier (for nicer output).
+ * @return Error code.
+ */
+int hcd_send_batch(
+    hcd_t *hcd, usb_target_t target, usb_direction_t direction,
+    void *data, size_t size, uint64_t setup_data,
+    usbhc_iface_transfer_in_callback_t in,
+    usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
+{
+	assert(hcd);
+
+	endpoint_t *ep = usb_endpoint_manager_find_ep(&hcd->ep_manager,
+	    target.address, target.endpoint, direction);
+	if (ep == NULL) {
+		usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
+		    target.address, target.endpoint, name);
+		return ENOENT;
+	}
+
+	usb_log_debug2("%s %d:%d %zu(%zu).\n",
+	    name, target.address, target.endpoint, size, ep->max_packet_size);
+
+	const size_t bw = bandwidth_count_usb11(
+	    ep->speed, ep->transfer_type, size, ep->max_packet_size);
+	/* Check if we have enough bandwidth reserved */
+	if (ep->bandwidth < bw) {
+		usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
+		    "but only %zu is reserved.\n",
+		    ep->address, ep->endpoint, name, bw, ep->bandwidth);
+		return ENOSPC;
+	}
+	if (!hcd->schedule) {
+		usb_log_error("HCD does not implement scheduler.\n");
+		return ENOTSUP;
+	}
+
+	/* Check for commands that reset toggle bit */
+	if (ep->transfer_type == USB_TRANSFER_CONTROL) {
+		const int reset_toggle = usb_request_needs_toggle_reset(
+		    (usb_device_request_setup_packet_t *) &setup_data);
+		if (reset_toggle >= 0) {
+			assert(out);
+			toggle_t *toggle = malloc(sizeof(toggle_t));
+			if (!toggle)
+				return ENOMEM;
+			toggle->target.address = target.address;
+			toggle->target.endpoint = reset_toggle;
+			toggle->original_callback = out;
+			toggle->original_data = arg;
+			toggle->hcd = hcd;
+
+			arg = toggle;
+			out = toggle_reset_callback;
+		}
+	}
+
+	usb_transfer_batch_t *batch = usb_transfer_batch_create(
+	    ep, data, size, setup_data, in, out, arg);
+	if (!batch) {
+		usb_log_error("Failed to create transfer batch.\n");
+		return ENOMEM;
+	}
+
+	const int ret = hcd->schedule(hcd, batch);
+	if (ret != EOK)
+		usb_transfer_batch_destroy(batch);
+
+	return ret;
+}
+
+typedef struct {
+	volatile unsigned done;
+	int ret;
+	size_t size;
+} sync_data_t;
+
+static void transfer_in_cb(int ret, size_t size, void* data)
+{
+	sync_data_t *d = data;
+	assert(d);
+	d->ret = ret;
+	d->done = 1;
+	d->size = size;
+}
+
+static void transfer_out_cb(int ret, void* data)
+{
+	sync_data_t *d = data;
+	assert(data);
+	d->ret = ret;
+	d->done = 1;
+}
+
+/** this is really ugly version of sync usb communication */
+ssize_t hcd_send_batch_sync(
+    hcd_t *hcd, usb_target_t target, usb_direction_t dir,
+    void *data, size_t size, uint64_t setup_data, const char* name)
+{
+	assert(hcd);
+	sync_data_t sd = { .done = 0, .ret = EINPROGRESS, .size = size };
+
+	const int ret = hcd_send_batch(hcd, target, dir, data, size, setup_data,
+	    dir == USB_DIRECTION_IN ? transfer_in_cb : NULL,
+	    dir == USB_DIRECTION_OUT ? transfer_out_cb : NULL, &sd, name);
+	if (ret != EOK)
+		return ret;
+
+	while (!sd.done) {
+		async_usleep(1000);
+	}
+
+	if (sd.ret == EOK)
+		return sd.size;
+	return sd.ret;
+}
+
+
+/**
+ * @}
+ */
Index: uspace/lib/usbhost/src/iface.c
===================================================================
--- uspace/lib/usbhost/src/iface.c	(revision feeac0d8cd6e0ce736779bc5f1dc701c5cf42ba6)
+++ 	(revision )
@@ -1,331 +1,0 @@
-/*
- * Copyright (c) 2011 Jan Vesely
- * 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 libusbhost
- * @{
- */
-/** @file
- * @brief HCD DDF interface implementation
- */
-
-#include <ddf/driver.h>
-#include <errno.h>
-
-#include <usb/debug.h>
-#include <usb/host/endpoint.h>
-#include <usb/host/hcd.h>
-
-/** Prepare generic usb_transfer_batch and schedule it.
- * @param fun DDF fun
- * @param target address and endpoint number.
- * @param setup_data Data to use in setup stage (Control communication type)
- * @param in Callback for device to host communication.
- * @param out Callback for host to device communication.
- * @param arg Callback parameter.
- * @param name Communication identifier (for nicer output).
- * @return Error code.
- */
-static inline int send_batch(
-    ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
-    void *data, size_t size, uint64_t setup_data,
-    usbhc_iface_transfer_in_callback_t in,
-    usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
-{
-	assert(fun);
-	hcd_t *hcd = fun_to_hcd(fun);
-	assert(hcd);
-
-	endpoint_t *ep = usb_endpoint_manager_find_ep(&hcd->ep_manager,
-	    target.address, target.endpoint, direction);
-	if (ep == NULL) {
-		usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
-		    target.address, target.endpoint, name);
-		return ENOENT;
-	}
-
-	usb_log_debug2("%s %d:%d %zu(%zu).\n",
-	    name, target.address, target.endpoint, size, ep->max_packet_size);
-
-	const size_t bw = bandwidth_count_usb11(
-	    ep->speed, ep->transfer_type, size, ep->max_packet_size);
-	/* Check if we have enough bandwidth reserved */
-	if (ep->bandwidth < bw) {
-		usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
-		    "but only %zu is reserved.\n",
-		    ep->address, ep->endpoint, name, bw, ep->bandwidth);
-		return ENOSPC;
-	}
-	if (!hcd->schedule) {
-		usb_log_error("HCD does not implement scheduler.\n");
-		return ENOTSUP;
-	}
-
-	/* No private data and no private data dtor */
-	usb_transfer_batch_t *batch =
-	    usb_transfer_batch_create(ep, data, size, setup_data,
-	    in, out, arg, fun, NULL, NULL);
-	if (!batch) {
-		return ENOMEM;
-	}
-
-	const int ret = hcd->schedule(hcd, batch);
-	if (ret != EOK)
-		usb_transfer_batch_destroy(batch);
-
-	return ret;
-}
-
-/** Calls ep_add_hook upon endpoint registration.
- * @param ep Endpoint to be registered.
- * @param arg hcd_t in disguise.
- * @return Error code.
- */
-static int register_helper(endpoint_t *ep, void *arg)
-{
-	hcd_t *hcd = arg;
-	assert(ep);
-	assert(hcd);
-	if (hcd->ep_add_hook)
-		return hcd->ep_add_hook(hcd, ep);
-	return EOK;
-}
-
-/** Calls ep_remove_hook upon endpoint removal.
- * @param ep Endpoint to be unregistered.
- * @param arg hcd_t in disguise.
- */
-static void unregister_helper(endpoint_t *ep, void *arg)
-{
-	hcd_t *hcd = arg;
-	assert(ep);
-	assert(hcd);
-	if (hcd->ep_remove_hook)
-		hcd->ep_remove_hook(hcd, ep);
-}
-
-/** Calls ep_remove_hook upon endpoint removal. Prints warning.
- * @param ep Endpoint to be unregistered.
- * @param arg hcd_t in disguise.
- */
-static void unregister_helper_warn(endpoint_t *ep, void *arg)
-{
-	hcd_t *hcd = arg;
-	assert(ep);
-	assert(hcd);
-	usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
-	    ep->address, ep->endpoint, usb_str_direction(ep->direction));
-	if (hcd->ep_remove_hook)
-		hcd->ep_remove_hook(hcd, ep);
-}
-
-/** Request address interface function.
- *
- * @param[in] fun DDF function that was called.
- * @param[in] address Pointer to preferred USB address.
- * @param[out] address Place to write a new address.
- * @param[in] strict Fail if the preferred address is not available.
- * @param[in] speed Speed to associate with the new default address.
- * @return Error code.
- */
-static int request_address(
-    ddf_fun_t *fun, usb_address_t *address, bool strict, usb_speed_t speed)
-{
-	assert(fun);
-	hcd_t *hcd = fun_to_hcd(fun);
-	assert(hcd);
-	assert(address);
-
-	usb_log_debug("Address request: speed: %s, address: %d, strict: %s.\n",
-	    usb_str_speed(speed), *address, strict ? "YES" : "NO");
-	return usb_device_manager_request_address(
-	    &hcd->dev_manager, address, strict, speed);
-}
-
-/** Bind address interface function.
- *
- * @param[in] fun DDF function that was called.
- * @param[in] address Address of the device
- * @param[in] handle Devman handle of the device driver.
- * @return Error code.
- */
-static int bind_address(
-    ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
-{
-	assert(fun);
-	hcd_t *hcd = fun_to_hcd(fun);
-	assert(hcd);
-
-	usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
-	return usb_device_manager_bind_address(
-	    &hcd->dev_manager, address, handle);
-}
-
-/** Find device handle by address interface function.
- *
- * @param[in] fun DDF function that was called.
- * @param[in] address Address in question.
- * @param[out] handle Where to store device handle if found.
- * @return Error code.
- */
-static int find_by_address(ddf_fun_t *fun, usb_address_t address,
-    devman_handle_t *handle)
-{
-	assert(fun);
-	hcd_t *hcd = fun_to_hcd(fun);
-	assert(hcd);
-	return usb_device_manager_get_info_by_address(
-	    &hcd->dev_manager, address, handle, NULL);
-}
-
-/** Release address interface function.
- *
- * @param[in] fun DDF function that was called.
- * @param[in] address USB address to be released.
- * @return Error code.
- */
-static int release_address(ddf_fun_t *fun, usb_address_t address)
-{
-	assert(fun);
-	hcd_t *hcd = fun_to_hcd(fun);
-	assert(hcd);
-	usb_log_debug("Address release %d.\n", address);
-	usb_device_manager_release_address(&hcd->dev_manager, address);
-	usb_endpoint_manager_remove_address(&hcd->ep_manager, address,
-	    unregister_helper_warn, hcd);
-	return EOK;
-}
-
-/** Register endpoint interface function.
- * @param fun DDF function.
- * @param address USB address of the device.
- * @param endpoint USB endpoint number to be registered.
- * @param transfer_type Endpoint's transfer type.
- * @param direction USB communication direction the endpoint is capable of.
- * @param max_packet_size Maximu size of packets the endpoint accepts.
- * @param interval Preferred timeout between communication.
- * @return Error code.
- */
-static int register_endpoint(
-    ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
-    usb_transfer_type_t transfer_type, usb_direction_t direction,
-    size_t max_packet_size, unsigned interval)
-{
-	assert(fun);
-	hcd_t *hcd = fun_to_hcd(fun);
-	assert(hcd);
-	const size_t size = max_packet_size;
-	usb_speed_t speed = USB_SPEED_MAX;
-	const int ret = usb_device_manager_get_info_by_address(
-	    &hcd->dev_manager, address, NULL, &speed);
-	if (ret != EOK) {
-		return ret;
-	}
-
-	usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
-	    address, endpoint, usb_str_transfer_type(transfer_type),
-	    usb_str_direction(direction), usb_str_speed(speed),
-	    max_packet_size, interval);
-
-	return usb_endpoint_manager_add_ep(&hcd->ep_manager, address, endpoint,
-	    direction, transfer_type, speed, max_packet_size, size,
-	    register_helper, hcd);
-}
-
-/** Unregister endpoint interface function.
- * @param fun DDF function.
- * @param address USB address of the endpoint.
- * @param endpoint USB endpoint number.
- * @param direction Communication direction of the enpdoint to unregister.
- * @return Error code.
- */
-static int unregister_endpoint(
-    ddf_fun_t *fun, usb_address_t address,
-    usb_endpoint_t endpoint, usb_direction_t direction)
-{
-	assert(fun);
-	hcd_t *hcd = fun_to_hcd(fun);
-	assert(hcd);
-	usb_log_debug("Unregister endpoint %d:%d %s.\n",
-	    address, endpoint, usb_str_direction(direction));
-	return usb_endpoint_manager_remove_ep(&hcd->ep_manager, address,
-	    endpoint, direction, unregister_helper, hcd);
-}
-
-/** Inbound communication interface function.
- * @param fun DDF function.
- * @param target Communication target.
- * @param setup_data Data to use in setup stage (control transfers).
- * @param data Pointer to data buffer.
- * @param size Size of the data buffer.
- * @param callback Function to call on communication end.
- * @param arg Argument passed to the callback function.
- * @return Error code.
- */
-static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
-    uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
-    void *arg)
-{
-	return send_batch(fun, target, USB_DIRECTION_IN, data, size,
-	    setup_data, callback, NULL, arg, "READ");
-}
-
-/** Outbound communication interface function.
- * @param fun DDF function.
- * @param target Communication target.
- * @param setup_data Data to use in setup stage (control transfers).
- * @param data Pointer to data buffer.
- * @param size Size of the data buffer.
- * @param callback Function to call on communication end.
- * @param arg Argument passed to the callback function.
- * @return Error code.
- */
-static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
-    const uint8_t *data, size_t size,
-    usbhc_iface_transfer_out_callback_t callback, void *arg)
-{
-	return send_batch(fun, target, USB_DIRECTION_OUT, (uint8_t*)data, size,
-	    setup_data, NULL, callback, arg, "WRITE");
-}
-
-/** usbhc Interface implementation using hcd_t from libusbhost library. */
-usbhc_iface_t hcd_iface = {
-	.request_address = request_address,
-	.bind_address = bind_address,
-	.get_handle = find_by_address,
-	.release_address = release_address,
-
-	.register_endpoint = register_endpoint,
-	.unregister_endpoint = unregister_endpoint,
-
-	.read = usb_read,
-	.write = usb_write,
-};
-
-/**
- * @}
- */
Index: uspace/lib/usbhost/src/usb_device_manager.c
===================================================================
--- uspace/lib/usbhost/src/usb_device_manager.c	(revision feeac0d8cd6e0ce736779bc5f1dc701c5cf42ba6)
+++ 	(revision )
@@ -1,242 +1,0 @@
-/*
- * Copyright (c) 2011 Jan Vesely
- * 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 libusbhost
- * @{
- */
-/** @file
- * Device manager structure and functions (implementation).
- */
-#include <assert.h>
-#include <errno.h>
-#include <usb/debug.h>
-#include <usb/host/usb_device_manager.h>
-
-/** Get a free USB address
- *
- * @param[in] instance Device manager structure to use.
- * @return Free address, or error code.
- */
-static usb_address_t usb_device_manager_get_free_address(
-    usb_device_manager_t *instance)
-{
-
-	usb_address_t new_address = instance->last_address;
-	do {
-		new_address = (new_address + 1) % USB_ADDRESS_COUNT;
-		if (new_address == USB_ADDRESS_DEFAULT)
-			new_address = 1;
-		if (new_address == instance->last_address) {
-			return ENOSPC;
-		}
-	} while (instance->devices[new_address].occupied);
-
-	assert(new_address != USB_ADDRESS_DEFAULT);
-	instance->last_address = new_address;
-
-	return new_address;
-}
-
-/** Initialize device manager structure.
- *
- * @param[in] instance Memory place to initialize.
- * @param[in] max_speed Maximum allowed USB speed of devices (inclusive).
- *
- * Set all values to false/0.
- */
-void usb_device_manager_init(
-    usb_device_manager_t *instance, usb_speed_t max_speed)
-{
-	assert(instance);
-	for (unsigned i = 0; i < USB_ADDRESS_COUNT; ++i) {
-		instance->devices[i].occupied = false;
-		instance->devices[i].handle = 0;
-		instance->devices[i].speed = USB_SPEED_MAX;
-	}
-	instance->last_address = 1;
-	instance->max_speed = max_speed;
-	fibril_mutex_initialize(&instance->guard);
-}
-
-/** Request USB address.
- * @param instance usb_device_manager
- * @param address Pointer to requested address value, place to store new address
- * @parma strict Fail if the requested address is not available.
- * @return Error code.
- * @note Default address is only available in strict mode.
- */
-int usb_device_manager_request_address(usb_device_manager_t *instance,
-    usb_address_t *address, bool strict, usb_speed_t speed)
-{
-	assert(instance);
-	assert(address);
-	if (speed > instance->max_speed)
-		return ENOTSUP;
-
-	if ((*address) < 0 || (*address) >= USB_ADDRESS_COUNT)
-		return EINVAL;
-
-	fibril_mutex_lock(&instance->guard);
-	/* Only grant default address to strict requests */
-	if (( (*address) == USB_ADDRESS_DEFAULT) && !strict) {
-		*address = instance->last_address;
-	}
-
-	if (instance->devices[*address].occupied) {
-		if (strict) {
-			fibril_mutex_unlock(&instance->guard);
-			return ENOENT;
-		}
-		*address = usb_device_manager_get_free_address(instance);
-	}
-	assert(instance->devices[*address].occupied == false);
-	assert(instance->devices[*address].handle == 0);
-	assert(*address != USB_ADDRESS_DEFAULT || strict);
-
-	instance->devices[*address].occupied = true;
-	instance->devices[*address].speed = speed;
-
-	fibril_mutex_unlock(&instance->guard);
-	return EOK;
-}
-
-/** Bind USB address to devman handle.
- *
- * @param[in] instance Device manager structure to use.
- * @param[in] address Device address
- * @param[in] handle Devman handle of the device.
- * @return Error code.
- * @note Won't accept binding for default address.
- */
-int usb_device_manager_bind_address(usb_device_manager_t *instance,
-    usb_address_t address, devman_handle_t handle)
-{
-	if ((address <= 0) || (address >= USB_ADDRESS_COUNT)) {
-		return EINVAL;
-	}
-	assert(instance);
-
-	fibril_mutex_lock(&instance->guard);
-	/* Not reserved */
-	if (!instance->devices[address].occupied) {
-		fibril_mutex_unlock(&instance->guard);
-		return ENOENT;
-	}
-	/* Already bound */
-	if (instance->devices[address].handle != 0) {
-		fibril_mutex_unlock(&instance->guard);
-		return EEXISTS;
-	}
-	instance->devices[address].handle = handle;
-	fibril_mutex_unlock(&instance->guard);
-	return EOK;
-}
-
-/** Release used USB address.
- *
- * @param[in] instance Device manager structure to use.
- * @param[in] address Device address
- * @return Error code.
- */
-int usb_device_manager_release_address(
-    usb_device_manager_t *instance, usb_address_t address)
-{
-	if ((address < 0) || (address >= USB_ADDRESS_COUNT)) {
-		return EINVAL;
-	}
-	assert(instance);
-
-	fibril_mutex_lock(&instance->guard);
-	if (!instance->devices[address].occupied) {
-		fibril_mutex_unlock(&instance->guard);
-		return ENOENT;
-	}
-
-	instance->devices[address].occupied = false;
-	instance->devices[address].handle = 0;
-	fibril_mutex_unlock(&instance->guard);
-	return EOK;
-}
-
-/** Find USB address associated with the device.
- *
- * @param[in] instance Device manager structure to use.
- * @param[in] handle Devman handle of the device seeking its address.
- * @return USB Address, or error code.
- */
-usb_address_t usb_device_manager_find_address(
-    usb_device_manager_t *instance, devman_handle_t handle)
-{
-	assert(instance);
-	fibril_mutex_lock(&instance->guard);
-	for (usb_address_t address = 1; address <= USB11_ADDRESS_MAX; ++address)
-	{
-		if (instance->devices[address].handle == handle) {
-			assert(instance->devices[address].occupied);
-			fibril_mutex_unlock(&instance->guard);
-			return address;
-		}
-	}
-	fibril_mutex_unlock(&instance->guard);
-	return ENOENT;
-}
-
-/** Find devman handle and speed assigned to USB address.
- *
- * @param[in] instance Device manager structure to use.
- * @param[in] address Address the caller wants to find.
- * @param[out] handle Where to store found handle.
- * @param[out] speed Assigned speed.
- * @return Error code.
- */
-int usb_device_manager_get_info_by_address(usb_device_manager_t *instance,
-    usb_address_t address, devman_handle_t *handle, usb_speed_t *speed)
-{
-	assert(instance);
-	if ((address < 0) || (address >= USB_ADDRESS_COUNT)) {
-		return EINVAL;
-	}
-
-	fibril_mutex_lock(&instance->guard);
-	if (!instance->devices[address].occupied) {
-		fibril_mutex_unlock(&instance->guard);
-		return ENOENT;
-	}
-
-	if (handle != NULL) {
-		*handle = instance->devices[address].handle;
-	}
-	if (speed != NULL) {
-		*speed = instance->devices[address].speed;
-	}
-
-	fibril_mutex_unlock(&instance->guard);
-	return EOK;
-}
-/**
- * @}
- */
Index: uspace/lib/usbhost/src/usb_endpoint_manager.c
===================================================================
--- uspace/lib/usbhost/src/usb_endpoint_manager.c	(revision feeac0d8cd6e0ce736779bc5f1dc701c5cf42ba6)
+++ uspace/lib/usbhost/src/usb_endpoint_manager.c	(revision 3f03199e0835b11c26a46cd190aeef79835d08f4)
@@ -36,4 +36,5 @@
 #include <assert.h>
 #include <errno.h>
+#include <macros.h>
 
 #include <usb/debug.h>
@@ -71,5 +72,5 @@
 	assert(instance);
 	assert(addr >= 0);
-	return &instance->endpoint_lists[addr % ENDPOINT_LIST_COUNT];
+	return &instance->devices[addr % ARRAY_SIZE(instance->devices)].endpoint_list;
 }
 
@@ -95,4 +96,28 @@
 	}
 	return NULL;
+}
+
+/** Get a free USB address
+ *
+ * @param[in] instance Device manager structure to use.
+ * @return Free address, or error code.
+ */
+static usb_address_t usb_endpoint_manager_get_free_address(
+    usb_endpoint_manager_t *instance)
+{
+
+	usb_address_t new_address = instance->last_address;
+	do {
+		new_address = (new_address + 1) % USB_ADDRESS_COUNT;
+		if (new_address == USB_ADDRESS_DEFAULT)
+			new_address = 1;
+		if (new_address == instance->last_address)
+			return ENOSPC;
+	} while (instance->devices[new_address].occupied);
+
+	assert(new_address != USB_ADDRESS_DEFAULT);
+	instance->last_address = new_address;
+
+	return new_address;
 }
 
@@ -155,6 +180,5 @@
  */
 int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
-    size_t available_bandwidth,
-    size_t (*bw_count)(usb_speed_t, usb_transfer_type_t, size_t, size_t))
+    size_t available_bandwidth, bw_count_func_t bw_count, usb_speed_t max_speed)
 {
 	assert(instance);
@@ -162,67 +186,12 @@
 	instance->free_bw = available_bandwidth;
 	instance->bw_count = bw_count;
-	for (unsigned i = 0; i < ENDPOINT_LIST_COUNT; ++i) {
-		list_initialize(&instance->endpoint_lists[i]);
+	instance->last_address = 0;
+	instance->max_speed = max_speed;
+	for (unsigned i = 0; i < ARRAY_SIZE(instance->devices); ++i) {
+		list_initialize(&instance->devices[i].endpoint_list);
+		instance->devices[i].speed = USB_SPEED_MAX;
+		instance->devices[i].occupied = false;
 	}
 	return EOK;
-}
-
-/** Check setup packet data for signs of toggle reset.
- *
- * @param[in] instance usb_endpoint_manager structure, non-null.
- * @param[in] target Device to receive setup packet.
- * @param[in] data Setup packet data.
- *
- * Really ugly one. Resets toggle bit on all endpoints that need it.
- * @TODO Use tools from libusbdev requests.h
- */
-void usb_endpoint_manager_reset_eps_if_need(usb_endpoint_manager_t *instance,
-    usb_target_t target, const uint8_t data[8])
-{
-	assert(instance);
-	if (!usb_target_is_valid(target)) {
-		usb_log_error("Invalid data when checking for toggle reset.\n");
-		return;
-	}
-
-	assert(data);
-	switch (data[1])
-	{
-	case 0x01: /* Clear Feature -- resets only cleared ep */
-		/* Recipient is endpoint, value is zero (ENDPOINT_STALL) */
-		// TODO Use macros in libusbdev requests.h
-		if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
-			fibril_mutex_lock(&instance->guard);
-			/* endpoint number is < 16, thus first byte is enough */
-			list_foreach(*get_list(instance, target.address),
-			    link, endpoint_t, ep) {
-				if ((ep->address == target.address)
-				    && (ep->endpoint = data[4])) {
-					endpoint_toggle_set(ep,0);
-				}
-			}
-			fibril_mutex_unlock(&instance->guard);
-		}
-	break;
-
-	case 0x9: /* Set Configuration */
-	case 0x11: /* Set Interface */
-		/* Recipient must be device, this resets all endpoints,
-		 * In fact there should be no endpoints but EP 0 registered
-		 * as different interfaces use different endpoints,
-		 * unless you're changing configuration or alternative
-		 * interface of an already setup device. */
-		if ((data[0] & 0xf) == 0) {
-			fibril_mutex_lock(&instance->guard);
-			list_foreach(*get_list(instance, target.address),
-			    link, endpoint_t, ep) {
-				if (ep->address == target.address) {
-					endpoint_toggle_set(ep,0);
-				}
-			}
-			fibril_mutex_unlock(&instance->guard);
-		}
-	break;
-	}
 }
 
@@ -316,21 +285,20 @@
 int usb_endpoint_manager_add_ep(usb_endpoint_manager_t *instance,
     usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
-    usb_transfer_type_t type, usb_speed_t speed, size_t max_packet_size,
-    size_t data_size, int (*callback)(endpoint_t *, void *), void *arg)
+    usb_transfer_type_t type, size_t max_packet_size, size_t data_size,
+    ep_add_callback_t callback, void *arg, usb_address_t tt_address,
+    unsigned tt_port)
 {
 	assert(instance);
 	if (instance->bw_count == NULL)
 		return ENOTSUP;
-	if (address < 0)
-		return EINVAL;
-
-	const size_t bw =
-	    instance->bw_count(speed, type, data_size, max_packet_size);
-
-	fibril_mutex_lock(&instance->guard);
-	/* Check for available bandwidth */
-	if (bw > instance->free_bw) {
-		fibril_mutex_unlock(&instance->guard);
-		return ENOSPC;
+	if (!usb_address_is_valid(address))
+		return EINVAL;
+
+
+	fibril_mutex_lock(&instance->guard);
+	/* Check for speed and address */
+	if (!instance->devices[address].occupied) {
+		fibril_mutex_unlock(&instance->guard);
+		return ENOENT;
 	}
 
@@ -342,6 +310,16 @@
 	}
 
-	ep = endpoint_create(
-	    address, endpoint, direction, type, speed, max_packet_size, bw);
+	const usb_speed_t speed = instance->devices[address].speed;
+	const size_t bw =
+	    instance->bw_count(speed, type, data_size, max_packet_size);
+
+	/* Check for available bandwidth */
+	if (bw > instance->free_bw) {
+		fibril_mutex_unlock(&instance->guard);
+		return ENOSPC;
+	}
+
+	ep = endpoint_create(address, endpoint, direction, type, speed,
+	    max_packet_size, bw, tt_address, tt_port);
 	if (!ep) {
 		fibril_mutex_unlock(&instance->guard);
@@ -375,5 +353,5 @@
 int usb_endpoint_manager_remove_ep(usb_endpoint_manager_t *instance,
     usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
-    void (*callback)(endpoint_t *, void *), void *arg)
+    ep_remove_callback_t callback, void *arg)
 {
 	assert(instance);
@@ -393,4 +371,25 @@
 	endpoint_destroy(ep);
 	return EOK;
+}
+
+int usb_endpoint_manager_reset_toggle(usb_endpoint_manager_t *instance,
+    usb_target_t target, bool all)
+{
+	assert(instance);
+	if (!usb_target_is_valid(target))
+		return EINVAL;
+
+	int ret = ENOENT;
+
+	fibril_mutex_lock(&instance->guard);
+	list_foreach(*get_list(instance, target.address), link, endpoint_t, ep) {
+		if ((ep->address == target.address)
+		    && (all || ep->endpoint == target.endpoint)) {
+			endpoint_toggle_set(ep, 0);
+			ret = EOK;
+		}
+	}
+	fibril_mutex_unlock(&instance->guard);
+	return ret;
 }
 
@@ -404,21 +403,20 @@
  * @return Error code.
  */
-void usb_endpoint_manager_remove_address(usb_endpoint_manager_t *instance,
-    usb_address_t address, void (*callback)(endpoint_t *, void *), void *arg)
-{
-	list_t *list;
-	link_t *link;
-	link_t *next;
-
-	assert(address >= 0);
-	assert(instance);
-	fibril_mutex_lock(&instance->guard);
-
-	list = get_list(instance, address);
-	link = list_first(list);
-	while (link != NULL) {
+int usb_endpoint_manager_remove_address(usb_endpoint_manager_t *instance,
+    usb_address_t address, ep_remove_callback_t callback, void *arg)
+{
+	assert(instance);
+	if (!usb_address_is_valid(address))
+		return EINVAL;
+
+	fibril_mutex_lock(&instance->guard);
+
+	const int ret = instance->devices[address].occupied ? EOK : ENOENT;
+	instance->devices[address].occupied = false;
+
+	list_t *list = get_list(instance, address);
+	for (link_t *link = list_first(list); link != NULL; ) {
 		endpoint_t *ep = list_get_instance(link, endpoint_t, link);
-		next = list_next(link, list);
-
+		link = list_next(link, list);
 		if (ep->address == address) {
 			list_remove(&ep->link);
@@ -427,7 +425,80 @@
 			endpoint_destroy(ep);
 		}
-		link = next;
-	}
-	fibril_mutex_unlock(&instance->guard);
+	}
+	fibril_mutex_unlock(&instance->guard);
+	return ret;
+}
+
+/** Request USB address.
+ * @param instance usb_device_manager
+ * @param address Pointer to requested address value, place to store new address
+ * @parma strict Fail if the requested address is not available.
+ * @return Error code.
+ * @note Default address is only available in strict mode.
+ */
+int usb_endpoint_manager_request_address(usb_endpoint_manager_t *instance,
+    usb_address_t *address, bool strict, usb_speed_t speed)
+{
+	assert(instance);
+	assert(address);
+	if (speed > instance->max_speed)
+		return ENOTSUP;
+
+	if (!usb_address_is_valid(*address))
+		return EINVAL;
+
+	usb_address_t addr = *address;
+
+	fibril_mutex_lock(&instance->guard);
+	/* Only grant default address to strict requests */
+	if ((addr == USB_ADDRESS_DEFAULT) && !strict) {
+		addr = usb_endpoint_manager_get_free_address(instance);
+	}
+
+	if (instance->devices[addr].occupied) {
+		if (strict) {
+			fibril_mutex_unlock(&instance->guard);
+			return ENOENT;
+		}
+		addr = usb_endpoint_manager_get_free_address(instance);
+	}
+	if (usb_address_is_valid(addr)) {
+		assert(instance->devices[addr].occupied == false);
+		assert(addr != USB_ADDRESS_DEFAULT || strict);
+
+		instance->devices[addr].occupied = true;
+		instance->devices[addr].speed = speed;
+		*address = addr;
+		addr = 0;
+	}
+
+	fibril_mutex_unlock(&instance->guard);
+	return addr;
+}
+
+/** Get speed assigned to USB address.
+ *
+ * @param[in] instance Device manager structure to use.
+ * @param[in] address Address the caller wants to find.
+ * @param[out] speed Assigned speed.
+ * @return Error code.
+ */
+int usb_endpoint_manager_get_speed(usb_endpoint_manager_t *instance,
+    usb_address_t address, usb_speed_t *speed)
+{
+	assert(instance);
+	if (!usb_address_is_valid(address)) {
+		return EINVAL;
+	}
+
+	fibril_mutex_lock(&instance->guard);
+
+	const int ret = instance->devices[address].occupied ? EOK : ENOENT;
+	if (speed && instance->devices[address].occupied) {
+		*speed = instance->devices[address].speed;
+	}
+
+	fibril_mutex_unlock(&instance->guard);
+	return ret;
 }
 /**
Index: uspace/lib/usbhost/src/usb_transfer_batch.c
===================================================================
--- uspace/lib/usbhost/src/usb_transfer_batch.c	(revision feeac0d8cd6e0ce736779bc5f1dc701c5cf42ba6)
+++ uspace/lib/usbhost/src/usb_transfer_batch.c	(revision 3f03199e0835b11c26a46cd190aeef79835d08f4)
@@ -61,8 +61,5 @@
     usbhc_iface_transfer_in_callback_t func_in,
     usbhc_iface_transfer_out_callback_t func_out,
-    void *arg,
-    ddf_fun_t *fun,
-    void *private_data,
-    void (*private_data_dtor)(void *)
+    void *arg
     )
 {
@@ -81,7 +78,4 @@
 		instance->buffer_size = buffer_size;
 		instance->setup_size = 0;
-		instance->fun = fun;
-		instance->private_data = private_data;
-		instance->private_data_dtor = private_data_dtor;
 		instance->transfered_size = 0;
 		instance->error = EOK;
@@ -110,8 +104,4 @@
 		endpoint_release(instance->ep);
 	}
-	if (instance->private_data) {
-		assert(instance->private_data_dtor);
-		instance->private_data_dtor(instance->private_data);
-	}
 	free(instance);
 }
@@ -133,13 +123,5 @@
 	/* NOTE: Only one of these pointers should be set. */
         if (instance->callback_out) {
-		/* Check for commands that reset toggle bit */
-		if (instance->ep->transfer_type == USB_TRANSFER_CONTROL
-		    && error == EOK) {
-			const usb_target_t target =
-			    {{ instance->ep->address, instance->ep->endpoint }};
-			reset_ep_if_need(fun_to_hcd(instance->fun), target,
-			    instance->setup_buffer);
-		}
-		instance->callback_out(instance->fun, error, instance->arg);
+		instance->callback_out(error, instance->arg);
 	}
 
@@ -150,6 +132,5 @@
 	                memcpy(instance->buffer, data, safe_size);
 		}
-		instance->callback_in(instance->fun, error,
-		    safe_size, instance->arg);
+		instance->callback_in(error, safe_size, instance->arg);
 	}
 }
