Index: uspace/lib/usbhost/src/ddf_helpers.c
===================================================================
--- uspace/lib/usbhost/src/ddf_helpers.c	(revision f76d2c2c443491a18c91f9d5b7760dd39b90f5ed)
+++ uspace/lib/usbhost/src/ddf_helpers.c	(revision f76d2c2c443491a18c91f9d5b7760dd39b90f5ed)
@@ -0,0 +1,942 @@
+/*
+ * 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/classes/classes.h>
+#include <usb/debug.h>
+#include <usb/descriptor.h>
+#include <usb/request.h>
+#include <usb/usb.h>
+
+#include <adt/list.h>
+#include <assert.h>
+#include <async.h>
+#include <ddf/driver.h>
+#include <ddf/interrupt.h>
+#include <device/hw_res_parsed.h>
+#include <devman.h>
+#include <errno.h>
+#include <fibril_synch.h>
+#include <macros.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <str_error.h>
+#include <usb_iface.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 packets, 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, packets, 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) {
+		assert(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;
+	}
+	fibril_mutex_unlock(&hub->guard);
+	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_bus_get_speed(&hcd->bus, USB_ADDRESS_DEFAULT, &speed);
+	if (ret != EOK) {
+		usb_log_error("Failed to verify speed: %s.", str_error(ret));
+		return ret;
+	}
+
+	usb_log_debug("Found new %s speed USB device.", usb_str_speed(speed));
+
+	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) {
+		usb_log_error("Failed to reserve new address: %s.",
+		    str_error(address));
+		return address;
+	}
+
+	usb_log_debug("Reserved new address: %d\n", 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 */
+	usb_log_debug("Device(%d): Adding default target(0:0)\n", address);
+	ret = hcd_add_ep(hcd,
+	    default_target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
+	    CTRL_PIPE_MIN_PACKET_SIZE, CTRL_PIPE_MIN_PACKET_SIZE, 1,
+	    tt_address, port);
+	if (ret != EOK) {
+		usb_log_error("Device(%d): Failed to add default target: %s.",
+		    address, str_error(ret));
+		hcd_release_address(hcd, address);
+		return ret;
+	}
+
+	/* Get max packet size for default pipe */
+	usb_standard_device_descriptor_t desc = { 0 };
+	const usb_device_request_setup_packet_t get_device_desc_8 =
+	    GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
+
+	// TODO CALLBACKS
+	usb_log_debug("Device(%d): Requesting first 8B of device descriptor.",
+	    address);
+	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) {
+		ret = got < 0 ? got : EOVERFLOW;
+		usb_log_error("Device(%d): Failed to get 8B of dev descr: %s.",
+		    address, str_error(ret));
+		hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
+		hcd_release_address(hcd, address);
+		return ret;
+	}
+
+	/* Register EP on the new address */
+	usb_log_debug("Device(%d): Registering control EP.", address);
+	ret = hcd_add_ep(hcd, target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
+	    ED_MPS_PACKET_SIZE_GET(uint16_usb2host(desc.max_packet_size)),
+	    ED_MPS_TRANS_OPPORTUNITIES_GET(uint16_usb2host(desc.max_packet_size)),
+	    ED_MPS_PACKET_SIZE_GET(uint16_usb2host(desc.max_packet_size)),
+	    tt_address, port);
+	if (ret != EOK) {
+		usb_log_error("Device(%d): Failed to register EP0: %s",
+		    address, str_error(ret));
+		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);
+
+	usb_log_debug("Device(%d): Setting USB address.", address);
+	got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_OUT,
+	    NULL, 0, *(uint64_t *)&set_address, "set address");
+
+	usb_log_debug("Device(%d): Removing default (0:0) EP.", address);
+	hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
+
+	if (got != 0) {
+		usb_log_error("Device(%d): Failed to set new address: %s.",
+		    address, str_error(got));
+		hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
+		hcd_release_address(hcd, address);
+		return got;
+	}
+
+	/* Get std device descriptor */
+	const usb_device_request_setup_packet_t get_device_desc =
+	    GET_DEVICE_DESC(sizeof(desc));
+
+	usb_log_debug("Device(%d): Requesting full device descriptor.",
+	    address);
+	got = hcd_send_batch_sync(hcd, target, USB_DIRECTION_IN,
+	    &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
+	    "read device descriptor");
+	if (ret != EOK) {
+		usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
+		    address, str_error(ret));
+		hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
+		hcd_release_address(hcd, target.address);
+		return ret;
+	}
+
+	/* Create match ids from the device descriptor */
+	match_id_list_t mids;
+	init_match_ids(&mids);
+
+	usb_log_debug("Device(%d): Creating match IDs.", address);
+	ret = create_match_ids(&mids, &desc);
+	if (ret != EOK) {
+		usb_log_error("Device(%d): Failed to create match ids: %s",
+		    address, str_error(ret));
+		hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
+		hcd_release_address(hcd, target.address);
+		return ret;
+	}
+
+	/* Register device */
+	usb_log_debug("Device(%d): Registering DDF device.", address);
+	ret = hcd_ddf_add_device(device, hub, port, address, speed, NULL, &mids);
+	clean_match_ids(&mids);
+	if (ret != EOK) {
+		usb_log_error("Device(%d): Failed to register: %s.",
+		    address, str_error(ret));
+		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->bus.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.
+ * @param[in] max_speed Maximum supported USB speed.
+ * @param[in] bw available bandwidth.
+ * @param[in] bw_count Function to compute required ep bandwidth.
+ *
+ * @return Error code.
+ * 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);
+}
+
+//TODO: Move this to generic ddf?
+/** Call the parent driver with a request to enable interrupts
+ *
+ * @param[in] device Device asking for interrupts
+ * @return Error code.
+ */
+int hcd_ddf_enable_interrupts(ddf_dev_t *device)
+{
+	assert(device);
+	async_sess_t *parent_sess =
+	    devman_parent_device_connect(ddf_dev_get_handle(device),
+	    IPC_FLAG_BLOCKING);
+	const bool enabled = hw_res_enable_interrupt(parent_sess);
+	async_hangup(parent_sess);
+
+	return enabled ? EOK : EIO;
+}
+
+//TODO: Move this to generic ddf?
+int hcd_ddf_get_registers(ddf_dev_t *device, hw_res_list_parsed_t *hw_res)
+{
+	assert(device);
+	assert(hw_res);
+
+	async_sess_t *parent_sess =
+	    devman_parent_device_connect(ddf_dev_get_handle(device),
+	    IPC_FLAG_BLOCKING);
+	hw_res_list_parsed_init(hw_res);
+	const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
+	async_hangup(parent_sess);
+	if (ret != EOK)
+		hw_res_list_parsed_clean(hw_res);
+	return ret;
+}
+
+// TODO: move this someplace else
+static inline void irq_code_clean(irq_code_t *code)
+{
+	if (code) {
+		free(code->ranges);
+		free(code->cmds);
+		code->ranges = NULL;
+		code->cmds = NULL;
+		code->rangecount = 0;
+		code->cmdcount = 0;
+	}
+}
+
+/** Register interrupt handler
+ *
+ * @param[in] device Host controller DDF device
+ * @param[in] regs Register range
+ * @param[in] irq Interrupt number
+ * @paran[in] handler Interrupt handler
+ * @param[in] gen_irq_code IRQ code generator.
+ *
+ * @return EOK on success or negative error code
+ */
+int hcd_ddf_setup_interrupts(ddf_dev_t *device,
+    const hw_res_list_parsed_t *hw_res,
+    interrupt_handler_t handler,
+    int (*gen_irq_code)(irq_code_t *, const hw_res_list_parsed_t *hw_res))
+{
+
+	assert(device);
+	if (!handler || !gen_irq_code)
+		return ENOTSUP;
+
+	irq_code_t irq_code = {0};
+
+	const int irq = gen_irq_code(&irq_code, hw_res);
+	if (irq < 0) {
+		usb_log_error("Failed to generate IRQ code: %s.\n",
+		    str_error(irq));
+		return irq;
+	}
+
+	/* Register handler to avoid interrupt lockup */
+	int ret = register_interrupt_handler(device, irq, handler, &irq_code);
+	irq_code_clean(&irq_code);
+	if (ret != EOK) {
+		usb_log_error("Failed to register interrupt handler: %s.\n",
+		    str_error(ret));
+		return ret;
+	}
+
+	/* Enable interrupts */
+	ret = hcd_ddf_enable_interrupts(device);
+	if (ret != EOK) {
+		usb_log_error("Failed to register interrupt handler: %s.\n",
+		    str_error(ret));
+		unregister_interrupt_handler(device, irq);
+		return ret;
+	}
+	assert(irq > 0);
+	return irq;
+}
+
+/** IRQ handling callback, forward status from call to diver structure.
+ *
+ * @param[in] dev DDF instance of the device to use.
+ * @param[in] iid (Unused).
+ * @param[in] call Pointer to the call from kernel.
+ */
+void ddf_hcd_gen_irq_handler(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
+{
+	assert(dev);
+	hcd_t *hcd = dev_to_hcd(dev);
+	if (!hcd || !hcd->ops.irq_hook) {
+		usb_log_error("Interrupt on not yet initialized device.\n");
+		return;
+	}
+	const uint32_t status = IPC_GET_ARG1(*call);
+	hcd->ops.irq_hook(hcd, status);
+}
+
+static int interrupt_polling(void *arg)
+{
+	hcd_t *hcd = arg;
+	assert(hcd);
+	if (!hcd->ops.status_hook || !hcd->ops.irq_hook)
+		return ENOTSUP;
+	uint32_t status = 0;
+	while (hcd->ops.status_hook(hcd, &status) == EOK) {
+		hcd->ops.irq_hook(hcd, status);
+		status = 0;
+		/* We should wait 1 frame - 1ms here, but this polling is a
+		 * lame crutch anyway so don't hog the system. 10ms is still
+		 * good enough for emergency mode */
+		async_usleep(10000);
+	}
+	return EOK;
+}
+
+/** Initialize hc and rh DDF structures and their respective drivers.
+ *
+ * @param device DDF instance of the device to use
+ * @param speed Maximum supported speed
+ * @param bw Available bandwidth (arbitrary units)
+ * @param bw_count Bandwidth computing function
+ * @param irq_handler IRQ handling function
+ * @param gen_irq_code Function to generate IRQ pseudocode
+ *                     (it needs to return used irq number)
+ * @param driver_init Function to initialize HC driver
+ * @param driver_fini Function to cleanup HC driver
+ * @return Error code
+ *
+ * This function does all the preparatory work for hc and rh drivers:
+ *  - gets device's hw resources
+ *  - attempts to enable interrupts
+ *  - registers interrupt handler
+ *  - calls driver specific initialization
+ *  - registers root hub
+ */
+int hcd_ddf_add_hc(ddf_dev_t *device, const ddf_hc_driver_t *driver)
+{
+	assert(driver);
+	static const struct { size_t bw; bw_count_func_t bw_count; }bw[] = {
+	    [USB_SPEED_FULL] = { .bw = BANDWIDTH_AVAILABLE_USB11,
+	                         .bw_count = bandwidth_count_usb11 },
+	    [USB_SPEED_HIGH] = { .bw = BANDWIDTH_AVAILABLE_USB11,
+	                         .bw_count = bandwidth_count_usb11 },
+	};
+
+	int ret = EOK;
+	const usb_speed_t speed = driver->hc_speed;
+	if (speed >= ARRAY_SIZE(bw) || bw[speed].bw == 0) {
+		usb_log_error("Driver `%s' reported unsupported speed: %s",
+		    driver->name, usb_str_speed(speed));
+		return ENOTSUP;
+	}
+
+	hw_res_list_parsed_t hw_res;
+	ret = hcd_ddf_get_registers(device, &hw_res);
+	if (ret != EOK) {
+		usb_log_error("Failed to get register memory addresses "
+		    "for `%s': %s.\n", ddf_dev_get_name(device),
+		    str_error(ret));
+		return ret;
+	}
+
+	ret = hcd_ddf_setup_hc(device, speed, bw[speed].bw, bw[speed].bw_count);
+	if (ret != EOK) {
+		usb_log_error("Failed to setup generic HCD.\n");
+		hw_res_list_parsed_clean(&hw_res);
+		return ret;
+	}
+
+	interrupt_handler_t *irq_handler =
+	    driver->irq_handler ? driver->irq_handler : ddf_hcd_gen_irq_handler;
+	const int irq = hcd_ddf_setup_interrupts(device, &hw_res, irq_handler,
+	    driver->irq_code_gen);
+	if (!(irq < 0)) {
+		usb_log_debug("Hw interrupts enabled.\n");
+	}
+
+	if (driver->claim)
+		ret = driver->claim(device);
+	if (ret != EOK) {
+		usb_log_error("Failed to claim `%s' for driver `%s'",
+		    ddf_dev_get_name(device), driver->name);
+		return ret;
+	}
+
+
+	/* Init hw driver */
+	hcd_t *hcd = dev_to_hcd(device);
+	ret = driver->init(hcd, &hw_res, !(irq < 0));
+	hw_res_list_parsed_clean(&hw_res);
+	if (ret != EOK) {
+		usb_log_error("Failed to init HCD: %s.\n", str_error(ret));
+		goto irq_unregister;
+	}
+
+	/* Need working irq replacement to setup root hub */
+	if ((irq < 0) && hcd->ops.status_hook) {
+		hcd->polling_fibril = fibril_create(interrupt_polling, hcd);
+		if (hcd->polling_fibril == 0) {
+			usb_log_error("Failed to create polling fibril\n");
+			ret = ENOMEM;
+			goto irq_unregister;
+		}
+		fibril_add_ready(hcd->polling_fibril);
+		usb_log_warning("Failed to enable interrupts: %s."
+		    " Falling back to polling.\n", str_error(irq));
+	}
+
+	/*
+	 * Creating root hub registers a new USB device so HC
+	 * needs to be ready at this time.
+	 */
+	ret = hcd_ddf_setup_root_hub(device);
+	if (ret != EOK) {
+		usb_log_error("Failed to setup HC root hub: %s.\n",
+		    str_error(ret));
+		driver->fini(dev_to_hcd(device));
+irq_unregister:
+		/* Unregistering non-existent should be ok */
+		unregister_interrupt_handler(device, irq);
+		hcd_ddf_clean_hc(device);
+		return ret;
+	}
+
+	usb_log_info("Controlling new `%s' device `%s'.\n",
+	    driver->name, ddf_dev_get_name(device));
+	return EOK;
+}
+/**
+ * @}
+ */
Index: uspace/lib/usbhost/src/endpoint.c
===================================================================
--- uspace/lib/usbhost/src/endpoint.c	(revision 8a637a47d260c9852eb7bbefc705cba01e8ef051)
+++ uspace/lib/usbhost/src/endpoint.c	(revision f76d2c2c443491a18c91f9d5b7760dd39b90f5ed)
@@ -33,8 +33,8 @@
  */
 
+#include <usb/host/endpoint.h>
+
 #include <assert.h>
 #include <stdlib.h>
-#include <errno.h>
-#include <usb/host/endpoint.h>
 
 /** Allocate ad initialize endpoint_t structure.
@@ -50,5 +50,6 @@
 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, unsigned packets, size_t bw,
+    usb_address_t tt_address, unsigned tt_p)
 {
 	endpoint_t *instance = malloc(sizeof(endpoint_t));
@@ -60,7 +61,10 @@
 		instance->speed = speed;
 		instance->max_packet_size = max_packet_size;
+		instance->packets = packets;
 		instance->bandwidth = bw;
 		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 +113,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 f76d2c2c443491a18c91f9d5b7760dd39b90f5ed)
+++ uspace/lib/usbhost/src/hcd.c	(revision f76d2c2c443491a18c91f9d5b7760dd39b90f5ed)
@@ -0,0 +1,295 @@
+/*
+ * 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 <usb/debug.h>
+#include <usb/request.h>
+
+#include <assert.h>
+#include <async.h>
+#include <errno.h>
+#include <usb_iface.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->ops.ep_add_hook)
+		return hcd->ops.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->ops.ep_remove_hook)
+		hcd->ops.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_bus_init(&hcd->bus, bandwidth, bw_count, max_speed);
+
+	hcd_set_implementation(hcd, NULL, 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_bus_request_address(
+	    &hcd->bus, &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_bus_remove_address(&hcd->bus, 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_bus_request_address(&hcd->bus, &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, unsigned packets,
+    size_t size, usb_address_t tt_address, unsigned tt_port)
+{
+	assert(hcd);
+	return usb_bus_add_ep(&hcd->bus, target.address,
+	    target.endpoint, dir, type, max_packet_size, packets, 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_bus_remove_ep(&hcd->bus, 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_bus_reset_toggle(&toggle->hcd->bus,
+		    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_bus_find_ep(&hcd->bus,
+	    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->ops.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->ops.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 = EBUSY, .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 8a637a47d260c9852eb7bbefc705cba01e8ef051)
+++ 	(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_bus.c
===================================================================
--- uspace/lib/usbhost/src/usb_bus.c	(revision f76d2c2c443491a18c91f9d5b7760dd39b90f5ed)
+++ uspace/lib/usbhost/src/usb_bus.c	(revision f76d2c2c443491a18c91f9d5b7760dd39b90f5ed)
@@ -0,0 +1,528 @@
+/*
+ * Copyright (c) 2011 Jan Vesely
+ * All rights eps.
+ *
+ * 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
+ * HC Endpoint management.
+ */
+
+#include <usb/host/usb_bus.h>
+#include <usb/debug.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <macros.h>
+#include <stdbool.h>
+
+
+/** Endpoint compare helper function.
+ *
+ * USB_DIRECTION_BOTH matches both IN and OUT.
+ * @param ep Endpoint to compare, non-null.
+ * @param address Tested address.
+ * @param endpoint Tested endpoint number.
+ * @param direction Tested direction.
+ * @return True if ep can be used to communicate with given device,
+ * false otherwise.
+ */
+static inline bool ep_match(const endpoint_t *ep,
+    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
+{
+	assert(ep);
+	return
+	    ((direction == ep->direction)
+	        || (ep->direction == USB_DIRECTION_BOTH)
+	        || (direction == USB_DIRECTION_BOTH))
+	    && (endpoint == ep->endpoint)
+	    && (address == ep->address);
+}
+
+/** Get list that holds endpoints for given address.
+ * @param instance usb_bus structure, non-null.
+ * @param addr USB address, must be >= 0.
+ * @return Pointer to the appropriate list.
+ */
+static list_t * get_list(usb_bus_t *instance, usb_address_t addr)
+{
+	assert(instance);
+	assert(addr >= 0);
+	return &instance->devices[addr % ARRAY_SIZE(instance->devices)].endpoint_list;
+}
+
+/** Internal search function, works on locked structure.
+ * @param instance usb_bus structure, non-null.
+ * @param address USB address, must be valid.
+ * @param endpoint USB endpoint number.
+ * @param direction Communication direction.
+ * @return Pointer to endpoint_t structure representing given communication
+ * target, NULL if there is no such endpoint registered.
+ * @note Assumes that the internal mutex is locked.
+ */
+static endpoint_t * find_locked(usb_bus_t *instance,
+    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
+{
+	assert(instance);
+	assert(fibril_mutex_is_locked(&instance->guard));
+	if (address < 0)
+		return NULL;
+	list_foreach(*get_list(instance, address), link, endpoint_t, ep) {
+		if (ep_match(ep, address, endpoint, direction))
+			return ep;
+	}
+	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_bus_get_free_address(usb_bus_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;
+}
+
+/** Calculate bandwidth that needs to be reserved for communication with EP.
+ * Calculation follows USB 1.1 specification.
+ * @param speed Device's speed.
+ * @param type Type of the transfer.
+ * @param size Number of byte to transfer.
+ * @param max_packet_size Maximum bytes in one packet.
+ */
+size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
+    size_t size, size_t max_packet_size)
+{
+	/* We care about bandwidth only for interrupt and isochronous. */
+	if ((type != USB_TRANSFER_INTERRUPT)
+	    && (type != USB_TRANSFER_ISOCHRONOUS)) {
+		return 0;
+	}
+
+	const unsigned packet_count =
+	    (size + max_packet_size - 1) / max_packet_size;
+	/* TODO: It may be that ISO and INT transfers use only one packet per
+	 * transaction, but I did not find text in USB spec to confirm this */
+	/* NOTE: All data packets will be considered to be max_packet_size */
+	switch (speed)
+	{
+	case USB_SPEED_LOW:
+		assert(type == USB_TRANSFER_INTERRUPT);
+		/* Protocol overhead 13B
+		 * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
+		 * CRC bytes, and a 3-byte interpacket delay)
+		 * see USB spec page 45-46. */
+		/* Speed penalty 8: low speed is 8-times slower*/
+		return packet_count * (13 + max_packet_size) * 8;
+	case USB_SPEED_FULL:
+		/* Interrupt transfer overhead see above
+		 * or page 45 of USB spec */
+		if (type == USB_TRANSFER_INTERRUPT)
+			return packet_count * (13 + max_packet_size);
+
+		assert(type == USB_TRANSFER_ISOCHRONOUS);
+		/* Protocol overhead 9B
+		 * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
+		 * bytes, and a 1-byte interpacket delay)
+		 * see USB spec page 42 */
+		return packet_count * (9 + max_packet_size);
+	default:
+		return 0;
+	}
+}
+
+/** Calculate bandwidth that needs to be reserved for communication with EP.
+ * Calculation follows USB 2.0 specification.
+ * @param speed Device's speed.
+ * @param type Type of the transfer.
+ * @param size Number of byte to transfer.
+ * @param max_packet_size Maximum bytes in one packet.
+ */
+size_t bandwidth_count_usb20(usb_speed_t speed, usb_transfer_type_t type,
+    size_t size, size_t max_packet_size)
+{
+	/* We care about bandwidth only for interrupt and isochronous. */
+	if ((type != USB_TRANSFER_INTERRUPT)
+	    && (type != USB_TRANSFER_ISOCHRONOUS)) {
+		return 0;
+	}
+	//TODO Implement
+	return 0;
+}
+
+/** Initialize to default state.
+ * You need to provide valid bw_count function if you plan to use
+ * add_endpoint/remove_endpoint pair.
+ *
+ * @param instance usb_bus structure, non-null.
+ * @param available_bandwidth Size of the bandwidth pool.
+ * @param bw_count function to use to calculate endpoint bw requirements.
+ * @return Error code.
+ */
+int usb_bus_init(usb_bus_t *instance,
+    size_t available_bandwidth, bw_count_func_t bw_count, usb_speed_t max_speed)
+{
+	assert(instance);
+	fibril_mutex_initialize(&instance->guard);
+	instance->free_bw = available_bandwidth;
+	instance->bw_count = bw_count;
+	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;
+}
+
+/** Register endpoint structure.
+ * Checks for duplicates.
+ * @param instance usb_bus, non-null.
+ * @param ep endpoint_t to register.
+ * @param data_size Size of data to transfer.
+ * @return Error code.
+ */
+int usb_bus_register_ep(usb_bus_t *instance, endpoint_t *ep, size_t data_size)
+{
+	assert(instance);
+	if (ep == NULL || ep->address < 0)
+		return EINVAL;
+
+	fibril_mutex_lock(&instance->guard);
+	/* Check for available bandwidth */
+	if (ep->bandwidth > instance->free_bw) {
+		fibril_mutex_unlock(&instance->guard);
+		return ENOSPC;
+	}
+
+	/* Check for existence */
+	const endpoint_t *endpoint =
+	    find_locked(instance, ep->address, ep->endpoint, ep->direction);
+	if (endpoint != NULL) {
+		fibril_mutex_unlock(&instance->guard);
+		return EEXIST;
+	}
+	list_append(&ep->link, get_list(instance, ep->address));
+
+	instance->free_bw -= ep->bandwidth;
+	usb_log_debug("Registered EP(%d:%d:%s:%s)\n", ep->address, ep->endpoint,
+	    usb_str_transfer_type_short(ep->transfer_type),
+	    usb_str_direction(ep->direction));
+	fibril_mutex_unlock(&instance->guard);
+	return EOK;
+}
+
+/** Unregister endpoint structure.
+ * Checks for duplicates.
+ * @param instance usb_bus, non-null.
+ * @param ep endpoint_t to unregister.
+ * @return Error code.
+ */
+int usb_bus_unregister_ep(usb_bus_t *instance, endpoint_t *ep)
+{
+	assert(instance);
+	if (ep == NULL || ep->address < 0)
+		return EINVAL;
+
+	fibril_mutex_lock(&instance->guard);
+	if (!list_member(&ep->link, get_list(instance, ep->address))) {
+		fibril_mutex_unlock(&instance->guard);
+		return ENOENT;
+	}
+	list_remove(&ep->link);
+	instance->free_bw += ep->bandwidth;
+	usb_log_debug("Unregistered EP(%d:%d:%s:%s)\n", ep->address,
+	    ep->endpoint, usb_str_transfer_type_short(ep->transfer_type),
+	    usb_str_direction(ep->direction));
+	fibril_mutex_unlock(&instance->guard);
+	return EOK;
+}
+
+/** Find endpoint_t representing the given communication route.
+ * @param instance usb_bus, non-null.
+ * @param address
+ */
+endpoint_t * usb_bus_find_ep(usb_bus_t *instance,
+    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
+{
+	assert(instance);
+
+	fibril_mutex_lock(&instance->guard);
+	endpoint_t *ep = find_locked(instance, address, endpoint, direction);
+	fibril_mutex_unlock(&instance->guard);
+	return ep;
+}
+
+/** Create and register new endpoint_t structure.
+ * @param instance usb_bus structure, non-null.
+ * @param address USB address.
+ * @param endpoint USB endpoint number.
+ * @param direction Communication direction.
+ * @param type USB transfer type.
+ * @param speed USB Communication speed.
+ * @param max_packet_size Maximum size of data packets.
+ * @param data_size Expected communication size.
+ * @param callback function to call just after registering.
+ * @param arg Argument to pass to the callback function.
+ * @return Error code.
+ */
+int usb_bus_add_ep(usb_bus_t *instance,
+    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
+    usb_transfer_type_t type, size_t max_packet_size, unsigned packets,
+    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 (!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;
+	}
+
+	/* Check for existence */
+	endpoint_t *ep = find_locked(instance, address, endpoint, direction);
+	if (ep != NULL) {
+		fibril_mutex_unlock(&instance->guard);
+		return EEXIST;
+	}
+
+	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, packets, bw, tt_address, tt_port);
+	if (!ep) {
+		fibril_mutex_unlock(&instance->guard);
+		return ENOMEM;
+	}
+
+	if (callback) {
+		const int ret = callback(ep, arg);
+		if (ret != EOK) {
+			fibril_mutex_unlock(&instance->guard);
+			endpoint_destroy(ep);
+			return ret;
+		}
+	}
+	list_append(&ep->link, get_list(instance, ep->address));
+
+	instance->free_bw -= ep->bandwidth;
+	fibril_mutex_unlock(&instance->guard);
+	return EOK;
+}
+
+/** Unregister and destroy endpoint_t structure representing given route.
+ * @param instance usb_bus structure, non-null.
+ * @param address USB address.
+ * @param endpoint USB endpoint number.
+ * @param direction Communication direction.
+ * @param callback Function to call after unregister, before destruction.
+ * @arg Argument to pass to the callback function.
+ * @return Error code.
+ */
+int usb_bus_remove_ep(usb_bus_t *instance,
+    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
+    ep_remove_callback_t callback, void *arg)
+{
+	assert(instance);
+	fibril_mutex_lock(&instance->guard);
+	endpoint_t *ep = find_locked(instance, address, endpoint, direction);
+	if (ep != NULL) {
+		list_remove(&ep->link);
+		instance->free_bw += ep->bandwidth;
+	}
+	fibril_mutex_unlock(&instance->guard);
+	if (ep == NULL)
+		return ENOENT;
+
+	if (callback) {
+		callback(ep, arg);
+	}
+	endpoint_destroy(ep);
+	return EOK;
+}
+
+int usb_bus_reset_toggle(usb_bus_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;
+}
+
+/** Unregister and destroy all endpoints using given address.
+ * @param instance usb_bus structure, non-null.
+ * @param address USB address.
+ * @param endpoint USB endpoint number.
+ * @param direction Communication direction.
+ * @param callback Function to call after unregister, before destruction.
+ * @arg Argument to pass to the callback function.
+ * @return Error code.
+ */
+int usb_bus_remove_address(usb_bus_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);
+		link = list_next(link, list);
+		if (ep->address == address) {
+			list_remove(&ep->link);
+			if (callback)
+				callback(ep, arg);
+			endpoint_destroy(ep);
+		}
+	}
+	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_bus_request_address(usb_bus_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_bus_get_free_address(instance);
+	}
+
+	if (instance->devices[addr].occupied) {
+		if (strict) {
+			fibril_mutex_unlock(&instance->guard);
+			return ENOENT;
+		}
+		addr = usb_bus_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_bus_get_speed(usb_bus_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_device_manager.c
===================================================================
--- uspace/lib/usbhost/src/usb_device_manager.c	(revision 8a637a47d260c9852eb7bbefc705cba01e8ef051)
+++ 	(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 EEXIST;
-	}
-	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 8a637a47d260c9852eb7bbefc705cba01e8ef051)
+++ 	(revision )
@@ -1,435 +1,0 @@
-/*
- * Copyright (c) 2011 Jan Vesely
- * All rights eps.
- *
- * 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
- * HC Endpoint management.
- */
-
-#include <stdbool.h>
-#include <assert.h>
-#include <errno.h>
-
-#include <usb/debug.h>
-#include <usb/host/usb_endpoint_manager.h>
-
-/** Endpoint compare helper function.
- *
- * USB_DIRECTION_BOTH matches both IN and OUT.
- * @param ep Endpoint to compare, non-null.
- * @param address Tested address.
- * @param endpoint Tested endpoint number.
- * @param direction Tested direction.
- * @return True if ep can be used to communicate with given device,
- * false otherwise.
- */
-static inline bool ep_match(const endpoint_t *ep,
-    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
-{
-	assert(ep);
-	return
-	    ((direction == ep->direction)
-	        || (ep->direction == USB_DIRECTION_BOTH)
-	        || (direction == USB_DIRECTION_BOTH))
-	    && (endpoint == ep->endpoint)
-	    && (address == ep->address);
-}
-
-/** Get list that holds endpoints for given address.
- * @param instance usb_endpoint_manager structure, non-null.
- * @param addr USB address, must be >= 0.
- * @return Pointer to the appropriate list.
- */
-static list_t * get_list(usb_endpoint_manager_t *instance, usb_address_t addr)
-{
-	assert(instance);
-	assert(addr >= 0);
-	return &instance->endpoint_lists[addr % ENDPOINT_LIST_COUNT];
-}
-
-/** Internal search function, works on locked structure.
- * @param instance usb_endpoint_manager structure, non-null.
- * @param address USB address, must be valid.
- * @param endpoint USB endpoint number.
- * @param direction Communication direction.
- * @return Pointer to endpoint_t structure representing given communication
- * target, NULL if there is no such endpoint registered.
- * @note Assumes that the internal mutex is locked.
- */
-static endpoint_t * find_locked(usb_endpoint_manager_t *instance,
-    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
-{
-	assert(instance);
-	assert(fibril_mutex_is_locked(&instance->guard));
-	if (address < 0)
-		return NULL;
-	list_foreach(*get_list(instance, address), link, endpoint_t, ep) {
-		if (ep_match(ep, address, endpoint, direction))
-			return ep;
-	}
-	return NULL;
-}
-
-/** Calculate bandwidth that needs to be reserved for communication with EP.
- * Calculation follows USB 1.1 specification.
- * @param speed Device's speed.
- * @param type Type of the transfer.
- * @param size Number of byte to transfer.
- * @param max_packet_size Maximum bytes in one packet.
- */
-size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
-    size_t size, size_t max_packet_size)
-{
-	/* We care about bandwidth only for interrupt and isochronous. */
-	if ((type != USB_TRANSFER_INTERRUPT)
-	    && (type != USB_TRANSFER_ISOCHRONOUS)) {
-		return 0;
-	}
-
-	const unsigned packet_count =
-	    (size + max_packet_size - 1) / max_packet_size;
-	/* TODO: It may be that ISO and INT transfers use only one packet per
-	 * transaction, but I did not find text in USB spec to confirm this */
-	/* NOTE: All data packets will be considered to be max_packet_size */
-	switch (speed)
-	{
-	case USB_SPEED_LOW:
-		assert(type == USB_TRANSFER_INTERRUPT);
-		/* Protocol overhead 13B
-		 * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
-		 * CRC bytes, and a 3-byte interpacket delay)
-		 * see USB spec page 45-46. */
-		/* Speed penalty 8: low speed is 8-times slower*/
-		return packet_count * (13 + max_packet_size) * 8;
-	case USB_SPEED_FULL:
-		/* Interrupt transfer overhead see above
-		 * or page 45 of USB spec */
-		if (type == USB_TRANSFER_INTERRUPT)
-			return packet_count * (13 + max_packet_size);
-
-		assert(type == USB_TRANSFER_ISOCHRONOUS);
-		/* Protocol overhead 9B
-		 * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
-		 * bytes, and a 1-byte interpacket delay)
-		 * see USB spec page 42 */
-		return packet_count * (9 + max_packet_size);
-	default:
-		return 0;
-	}
-}
-
-/** Initialize to default state.
- * You need to provide valid bw_count function if you plan to use
- * add_endpoint/remove_endpoint pair.
- *
- * @param instance usb_endpoint_manager structure, non-null.
- * @param available_bandwidth Size of the bandwidth pool.
- * @param bw_count function to use to calculate endpoint bw requirements.
- * @return Error code.
- */
-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))
-{
-	assert(instance);
-	fibril_mutex_initialize(&instance->guard);
-	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]);
-	}
-	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;
-	}
-}
-
-/** Register endpoint structure.
- * Checks for duplicates.
- * @param instance usb_endpoint_manager, non-null.
- * @param ep endpoint_t to register.
- * @param data_size Size of data to transfer.
- * @return Error code.
- */
-int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
-    endpoint_t *ep, size_t data_size)
-{
-	assert(instance);
-	if (ep == NULL || ep->address < 0)
-		return EINVAL;
-
-	fibril_mutex_lock(&instance->guard);
-	/* Check for available bandwidth */
-	if (ep->bandwidth > instance->free_bw) {
-		fibril_mutex_unlock(&instance->guard);
-		return ENOSPC;
-	}
-
-	/* Check for existence */
-	const endpoint_t *endpoint =
-	    find_locked(instance, ep->address, ep->endpoint, ep->direction);
-	if (endpoint != NULL) {
-		fibril_mutex_unlock(&instance->guard);
-		return EEXIST;
-	}
-	list_append(&ep->link, get_list(instance, ep->address));
-
-	instance->free_bw -= ep->bandwidth;
-	fibril_mutex_unlock(&instance->guard);
-	return EOK;
-}
-
-/** Unregister endpoint structure.
- * Checks for duplicates.
- * @param instance usb_endpoint_manager, non-null.
- * @param ep endpoint_t to unregister.
- * @return Error code.
- */
-int usb_endpoint_manager_unregister_ep(
-    usb_endpoint_manager_t *instance, endpoint_t *ep)
-{
-	assert(instance);
-	if (ep == NULL || ep->address < 0)
-		return EINVAL;
-
-	fibril_mutex_lock(&instance->guard);
-	if (!list_member(&ep->link, get_list(instance, ep->address))) {
-		fibril_mutex_unlock(&instance->guard);
-		return ENOENT;
-	}
-	list_remove(&ep->link);
-	instance->free_bw += ep->bandwidth;
-	fibril_mutex_unlock(&instance->guard);
-	return EOK;
-}
-
-/** Find endpoint_t representing the given communication route.
- * @param instance usb_endpoint_manager, non-null.
- * @param address
- */
-endpoint_t * usb_endpoint_manager_find_ep(usb_endpoint_manager_t *instance,
-    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
-{
-	assert(instance);
-
-	fibril_mutex_lock(&instance->guard);
-	endpoint_t *ep = find_locked(instance, address, endpoint, direction);
-	fibril_mutex_unlock(&instance->guard);
-	return ep;
-}
-
-/** Create and register new endpoint_t structure.
- * @param instance usb_endpoint_manager structure, non-null.
- * @param address USB address.
- * @param endpoint USB endpoint number.
- * @param direction Communication direction.
- * @param type USB transfer type.
- * @param speed USB Communication speed.
- * @param max_packet_size Maximum size of data packets.
- * @param data_size Expected communication size.
- * @param callback function to call just after registering.
- * @param arg Argument to pass to the callback function.
- * @return Error code.
- */
-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)
-{
-	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;
-	}
-
-	/* Check for existence */
-	endpoint_t *ep = find_locked(instance, address, endpoint, direction);
-	if (ep != NULL) {
-		fibril_mutex_unlock(&instance->guard);
-		return EEXIST;
-	}
-
-	ep = endpoint_create(
-	    address, endpoint, direction, type, speed, max_packet_size, bw);
-	if (!ep) {
-		fibril_mutex_unlock(&instance->guard);
-		return ENOMEM;
-	}
-
-	if (callback) {
-		const int ret = callback(ep, arg);
-		if (ret != EOK) {
-			fibril_mutex_unlock(&instance->guard);
-			endpoint_destroy(ep);
-			return ret;
-		}
-	}
-	list_append(&ep->link, get_list(instance, ep->address));
-
-	instance->free_bw -= ep->bandwidth;
-	fibril_mutex_unlock(&instance->guard);
-	return EOK;
-}
-
-/** Unregister and destroy endpoint_t structure representing given route.
- * @param instance usb_endpoint_manager structure, non-null.
- * @param address USB address.
- * @param endpoint USB endpoint number.
- * @param direction Communication direction.
- * @param callback Function to call after unregister, before destruction.
- * @arg Argument to pass to the callback function.
- * @return Error code.
- */
-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)
-{
-	assert(instance);
-	fibril_mutex_lock(&instance->guard);
-	endpoint_t *ep = find_locked(instance, address, endpoint, direction);
-	if (ep != NULL) {
-		list_remove(&ep->link);
-		instance->free_bw += ep->bandwidth;
-	}
-	fibril_mutex_unlock(&instance->guard);
-	if (ep == NULL)
-		return ENOENT;
-
-	if (callback) {
-		callback(ep, arg);
-	}
-	endpoint_destroy(ep);
-	return EOK;
-}
-
-/** Unregister and destroy all endpoints using given address.
- * @param instance usb_endpoint_manager structure, non-null.
- * @param address USB address.
- * @param endpoint USB endpoint number.
- * @param direction Communication direction.
- * @param callback Function to call after unregister, before destruction.
- * @arg Argument to pass to the callback function.
- * @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) {
-		endpoint_t *ep = list_get_instance(link, endpoint_t, link);
-		next = list_next(link, list);
-
-		if (ep->address == address) {
-			list_remove(&ep->link);
-			if (callback)
-				callback(ep, arg);
-			endpoint_destroy(ep);
-		}
-		link = next;
-	}
-	fibril_mutex_unlock(&instance->guard);
-}
-/**
- * @}
- */
Index: uspace/lib/usbhost/src/usb_transfer_batch.c
===================================================================
--- uspace/lib/usbhost/src/usb_transfer_batch.c	(revision 8a637a47d260c9852eb7bbefc705cba01e8ef051)
+++ uspace/lib/usbhost/src/usb_transfer_batch.c	(revision f76d2c2c443491a18c91f9d5b7760dd39b90f5ed)
@@ -32,12 +32,14 @@
  * USB transfer transaction structures (implementation).
  */
+
+#include <usb/host/usb_transfer_batch.h>
+#include <usb/debug.h>
+
+#include <assert.h>
 #include <errno.h>
 #include <macros.h>
-
-#include <usb/usb.h>
-#include <usb/debug.h>
-
-#include <usb/host/usb_transfer_batch.h>
-#include <usb/host/hcd.h>
+#include <mem.h>
+#include <stdlib.h>
+#include <usbhc_iface.h>
 
 /** Allocate and initialize usb_transfer_batch structure.
@@ -61,8 +63,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 +80,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 +106,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 +125,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 +134,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);
 	}
 }
