Index: uspace/lib/usbdev/src/altiface.c
===================================================================
--- uspace/lib/usbdev/src/altiface.c	(revision 1cf26abf038d0db4ca5f4e0a43ca1b41d655b01e)
+++ uspace/lib/usbdev/src/altiface.c	(revision b4b534ac1d1515633baff0d563e9c16938bfe19f)
@@ -34,11 +34,9 @@
  */
 
-#include <usb/dev/driver.h>
-#include <usb/dev/request.h>
-#include <usb/debug.h>
+#include <usb/dev/alternate_ifaces.h>
 #include <usb/dev/dp.h>
+#include <assert.h>
 #include <errno.h>
-#include <str_error.h>
-#include <assert.h>
+#include <stdlib.h>
 
 /** Count number of alternate settings of a interface.
@@ -105,15 +103,14 @@
 	}
 
-	alternates->alternative_count
-	    = usb_interface_count_alternates(config_descr, config_descr_size,
-	        interface_number);
+	const size_t alt_count = usb_interface_count_alternates(config_descr,
+	    config_descr_size, interface_number);
 
-	if (alternates->alternative_count == 0) {
+	if (alt_count == 0) {
 		return ENOENT;
 	}
 
-	alternates->alternatives = calloc(alternates->alternative_count,
+	usb_alternate_interface_descriptors_t *alts = calloc(alt_count,
 	    sizeof(usb_alternate_interface_descriptors_t));
-	if (alternates->alternatives == NULL) {
+	if (alts == NULL) {
 		return ENOMEM;
 	}
@@ -128,14 +125,10 @@
 	};
 
-	usb_alternate_interface_descriptors_t *iterator
-	    = &alternates->alternatives[0];
-
-	const usb_alternate_interface_descriptors_t *end
-	    = &alternates->alternatives[alternates->alternative_count];
 
 	const void *iface_ptr =
 	    usb_dp_get_nested_descriptor(&dp_parser, &dp_data, dp_data.data);
 
-	while (iface_ptr != NULL && iterator < end) {
+	usb_alternate_interface_descriptors_t *iterator = alts;
+	for (; iface_ptr != NULL && iterator < &alts[alt_count]; ++iterator) {
 		const usb_standard_interface_descriptor_t *iface = iface_ptr;
 
@@ -159,9 +152,10 @@
 		    dp_data.data + dp_data.size : iface_ptr;
 
-		iterator->nested_descriptors_size
-		    = next - iterator->nested_descriptors;
+		iterator->nested_descriptors_size =
+		    next - iterator->nested_descriptors;
+	}
 
-		++iterator;
-	}
+	alternates->alternatives = alts;
+	alternates->alternative_count = alt_count;
 
 	return EOK;
Index: uspace/lib/usbdev/src/devdrv.c
===================================================================
--- uspace/lib/usbdev/src/devdrv.c	(revision 1cf26abf038d0db4ca5f4e0a43ca1b41d655b01e)
+++ uspace/lib/usbdev/src/devdrv.c	(revision b4b534ac1d1515633baff0d563e9c16938bfe19f)
@@ -33,45 +33,56 @@
  * USB device driver framework.
  */
-#include <usb/dev/driver.h>
+
+#include <usb_iface.h>
+#include <usb/dev/alternate_ifaces.h>
+#include <usb/dev/device.h>
+#include <usb/dev/pipes.h>
 #include <usb/dev/request.h>
 #include <usb/debug.h>
-#include <usb/dev.h>
+#include <usb/descriptor.h>
+#include <usb/usb.h>
+
+#include <assert.h>
+#include <async.h>
+#include <devman.h>
 #include <errno.h>
-#include <str_error.h>
-#include <assert.h>
-
-static int generic_device_add(ddf_dev_t *);
-static int generic_device_remove(ddf_dev_t *);
-static int generic_device_gone(ddf_dev_t *);
-
-static driver_ops_t generic_driver_ops = {
-	.dev_add = generic_device_add,
-	.dev_remove = generic_device_remove,
-	.dev_gone = generic_device_gone,
-};
-static driver_t generic_driver = {
-	.driver_ops = &generic_driver_ops
-};
-
-static const usb_driver_t *driver = NULL;
-
-/** Main routine of USB device driver.
- *
- * Under normal conditions, this function never returns.
- *
- * @param drv USB device driver structure.
- * @return Task exit status.
- */
-int usb_driver_main(const usb_driver_t *drv)
-{
-	assert(drv != NULL);
-
-	/* Prepare the generic driver. */
-	generic_driver.name = drv->name;
-
-	driver = drv;
-
-	return ddf_driver_main(&generic_driver);
-}
+#include <stdlib.h>
+
+#include <ddf/driver.h>
+
+/** USB device structure. */
+typedef struct usb_device {
+	/** Connection to device on USB bus */
+	usb_dev_session_t *bus_session;
+	/** devman handle */
+	devman_handle_t handle;
+	/** The default control pipe. */
+	usb_pipe_t ctrl_pipe;
+
+	/** Other endpoint pipes.
+	 * This is an array of other endpoint pipes in the same order as
+	 * in usb_driver_t.
+	 */
+	usb_endpoint_mapping_t *pipes;
+	/** Number of other endpoint pipes. */
+	size_t pipes_count;
+	/** Current interface.
+	 * Usually, drivers operate on single interface only.
+	 * This item contains the value of the interface or -1 for any.
+	 */
+	int interface_no;
+	/** Alternative interfaces. */
+	usb_alternate_interfaces_t alternate_interfaces;
+	/** Some useful descriptors for USB device. */
+	usb_device_descriptors_t descriptors;
+	/** Generic DDF device backing this one. DO NOT TOUCH! */
+	ddf_dev_t *ddf_dev;
+	/** Custom driver data.
+	 * Do not use the entry in generic device, that is already used
+	 * by the framework.
+	 */
+	void *driver_data;
+
+} usb_device_t;
 
 /** Count number of pipes the driver expects.
@@ -80,101 +91,9 @@
  * @return Number of pipes (excluding default control pipe).
  */
-static inline size_t count_other_pipes(
-    const usb_endpoint_description_t **endpoints)
+static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
 {
 	size_t count;
 	for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
 	return count;
-}
-
-/** Callback when a new device is supposed to be controlled by this driver.
- *
- * This callback is a wrapper for USB specific version of @c device_add.
- *
- * @param gen_dev Device structure as prepared by DDF.
- * @return Error code.
- */
-int generic_device_add(ddf_dev_t *gen_dev)
-{
-	assert(driver);
-	assert(driver->ops);
-	assert(driver->ops->device_add);
-
-	/* Get place for driver data. */
-	usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t));
-	if (dev == NULL) {
-		usb_log_error("USB device `%s' structure allocation failed.\n",
-		    ddf_dev_get_name(gen_dev));
-		return ENOMEM;
-	}
-
-	/* Initialize generic USB driver data. */
-	const char *err_msg = NULL;
-	int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
-	if (rc != EOK) {
-		usb_log_error("USB device `%s' init failed (%s): %s.\n",
-		    ddf_dev_get_name(gen_dev), err_msg, str_error(rc));
-		return rc;
-	}
-
-	/* Start USB driver specific initialization. */
-	rc = driver->ops->device_add(dev);
-	if (rc != EOK)
-		usb_device_deinit(dev);
-	return rc;
-}
-
-/** Callback when a device is supposed to be removed from the system.
- *
- * This callback is a wrapper for USB specific version of @c device_remove.
- *
- * @param gen_dev Device structure as prepared by DDF.
- * @return Error code.
- */
-int generic_device_remove(ddf_dev_t *gen_dev)
-{
-	assert(driver);
-	assert(driver->ops);
-	if (driver->ops->device_rem == NULL)
-		return ENOTSUP;
-	/* Just tell the driver to stop whatever it is doing */
-	usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
-	const int ret = driver->ops->device_rem(usb_dev);
-	if (ret != EOK)
-		return ret;
-	usb_device_deinit(usb_dev);
-	return EOK;
-}
-
-/** Callback when a device was removed from the system.
- *
- * This callback is a wrapper for USB specific version of @c device_gone.
- *
- * @param gen_dev Device structure as prepared by DDF.
- * @return Error code.
- */
-int generic_device_gone(ddf_dev_t *gen_dev)
-{
-	assert(driver);
-	assert(driver->ops);
-	if (driver->ops->device_gone == NULL)
-		return ENOTSUP;
-	usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
-	const int ret = driver->ops->device_gone(usb_dev);
-	if (ret == EOK)
-		usb_device_deinit(usb_dev);
-
-	return ret;
-}
-
-/** Destroy existing pipes of a USB device.
- *
- * @param dev Device where to destroy the pipes.
- */
-static void destroy_current_pipes(usb_device_t *dev)
-{
-	usb_device_destroy_pipes(dev->pipes, dev->pipes_count);
-	dev->pipes = NULL;
-	dev->pipes_count = 0;
 }
 
@@ -201,26 +120,28 @@
  * @return Error code.
  */
-int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting,
-    const usb_endpoint_description_t **endpoints)
-{
-	if (dev->interface_no < 0) {
+int usb_device_select_interface(usb_device_t *usb_dev,
+    uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
+{
+	assert(usb_dev);
+
+	if (usb_dev->interface_no < 0) {
 		return EINVAL;
 	}
 
-	/* Destroy existing pipes. */
-	destroy_current_pipes(dev);
-
 	/* Change the interface itself. */
-	int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
-	    alternate_setting);
+	int rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
+	    usb_dev->interface_no, alternate_setting);
 	if (rc != EOK) {
 		return rc;
 	}
 
+	/* Change current alternative */
+	usb_dev->alternate_interfaces.current = alternate_setting;
+
+	/* Destroy existing pipes. */
+	usb_device_destroy_pipes(usb_dev);
+
 	/* Create new pipes. */
-	rc = usb_device_create_pipes(&dev->wire, endpoints,
-	    dev->descriptors.configuration, dev->descriptors.configuration_size,
-	    dev->interface_no, (int)alternate_setting,
-	    &dev->pipes, &dev->pipes_count);
+	rc = usb_device_create_pipes(usb_dev, endpoints);
 
 	return rc;
@@ -233,29 +154,22 @@
  * @return Error code.
  */
-int usb_device_retrieve_descriptors(usb_pipe_t *ctrl_pipe,
-    usb_device_descriptors_t *descriptors)
-{
-	assert(descriptors != NULL);
-
-	descriptors->configuration = NULL;
-
-	int rc;
-
-	/* It is worth to start a long transfer. */
-	usb_pipe_start_long_transfer(ctrl_pipe);
+static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	assert(usb_dev->descriptors.full_config == NULL);
 
 	/* Get the device descriptor. */
-	rc = usb_request_get_device_descriptor(ctrl_pipe, &descriptors->device);
+	int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
+	    &usb_dev->descriptors.device);
 	if (rc != EOK) {
-		goto leave;
+		return rc;
 	}
 
 	/* Get the full configuration descriptor. */
 	rc = usb_request_get_full_configuration_descriptor_alloc(
-	    ctrl_pipe, 0, (void **) &descriptors->configuration,
-	    &descriptors->configuration_size);
-
-leave:
-	usb_pipe_end_long_transfer(ctrl_pipe);
+	    &usb_dev->ctrl_pipe, 0,
+	    &usb_dev->descriptors.full_config,
+	    &usb_dev->descriptors.full_config_size);
+
 
 	return rc;
@@ -266,9 +180,10 @@
  * @param[in] descriptors Where to store the descriptors.
  */
-void usb_device_release_descriptors(usb_device_descriptors_t *descriptors)
-{
-	assert(descriptors);
-	free(descriptors->configuration);
-	descriptors->configuration = NULL;
+static void usb_device_release_descriptors(usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	free(usb_dev->descriptors.full_config);
+	usb_dev->descriptors.full_config = NULL;
+	usb_dev->descriptors.full_config_size = 0;
 }
 
@@ -280,5 +195,4 @@
  * - registers endpoints with the host controller
  *
- * @param[in] wire Initialized backing connection to the host controller.
  * @param[in] endpoints Endpoints description, NULL terminated.
  * @param[in] config_descr Configuration descriptor of active configuration.
@@ -292,28 +206,19 @@
  * @return Error code.
  */
-int usb_device_create_pipes(usb_device_connection_t *wire,
-    const usb_endpoint_description_t **endpoints,
-    const uint8_t *config_descr, size_t config_descr_size,
-    int interface_no, int interface_setting,
-    usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
-{
-	assert(wire != NULL);
-	assert(config_descr != NULL);
-	assert(config_descr_size > 0);
-	assert(pipes_ptr != NULL);
-
-	size_t i;
-	int rc;
-
-	const size_t pipe_count = count_other_pipes(endpoints);
+int usb_device_create_pipes(usb_device_t *usb_dev,
+    const usb_endpoint_description_t **endpoints)
+{
+	assert(usb_dev);
+	assert(usb_dev->descriptors.full_config);
+	assert(usb_dev->pipes == NULL);
+	assert(usb_dev->pipes_count == 0);
+
+	size_t pipe_count = count_pipes(endpoints);
 	if (pipe_count == 0) {
-		if (pipes_count_ptr)
-			*pipes_count_ptr = pipe_count;
-		*pipes_ptr = NULL;
 		return EOK;
 	}
 
-	usb_endpoint_mapping_t *pipes
-	    = calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
+	usb_endpoint_mapping_t *pipes =
+	    calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
 	if (pipes == NULL) {
 		return ENOMEM;
@@ -321,13 +226,16 @@
 
 	/* Now initialize. */
-	for (i = 0; i < pipe_count; i++) {
+	for (size_t i = 0; i < pipe_count; i++) {
 		pipes[i].description = endpoints[i];
-		pipes[i].interface_no = interface_no;
-		pipes[i].interface_setting = interface_setting;
+		pipes[i].interface_no = usb_dev->interface_no;
+		pipes[i].interface_setting =
+		    usb_dev->alternate_interfaces.current;
 	}
 
 	/* Find the mapping from configuration descriptor. */
-	rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
-	    config_descr, config_descr_size, wire);
+	int rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
+	    usb_dev->descriptors.full_config,
+	    usb_dev->descriptors.full_config_size,
+	    usb_dev->bus_session);
 	if (rc != EOK) {
 		free(pipes);
@@ -336,5 +244,5 @@
 
 	/* Register created pipes. */
-	for (i = 0; i < pipe_count; i++) {
+	for (size_t i = 0; i < pipe_count; i++) {
 		if (pipes[i].present) {
 			rc = usb_pipe_register(&pipes[i].pipe,
@@ -346,8 +254,6 @@
 	}
 
-	*pipes_ptr = pipes;
-	if (pipes_count_ptr != NULL) {
-		*pipes_count_ptr = pipe_count;
-	}
+	usb_dev->pipes = pipes;
+	usb_dev->pipes_count = pipe_count;
 
 	return EOK;
@@ -360,5 +266,5 @@
 	 */
 rollback_unregister_endpoints:
-	for (i = 0; i < pipe_count; i++) {
+	for (size_t i = 0; i < pipe_count; i++) {
 		if (pipes[i].present) {
 			usb_pipe_unregister(&pipes[i].pipe);
@@ -372,18 +278,94 @@
 /** Destroy pipes previously created by usb_device_create_pipes.
  *
- * @param[in] pipes Endpoint mapping to be destroyed.
- * @param[in] pipes_count Number of endpoints.
- */
-void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count)
-{
+ * @param[in] usb_dev USB device.
+ */
+void usb_device_destroy_pipes(usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	assert(usb_dev->pipes || usb_dev->pipes_count == 0);
 	/* Destroy the pipes. */
-	for (size_t i = 0; i < pipes_count; ++i) {
-		assert(pipes);
+	for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
 		usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
-		    i, pipes[i].present ? "" : "not ");
-		if (pipes[i].present)
-			usb_pipe_unregister(&pipes[i].pipe);
-	}
-	free(pipes);
+		    i, usb_dev->pipes[i].present ? "" : "not ");
+		if (usb_dev->pipes[i].present)
+			usb_pipe_unregister(&usb_dev->pipes[i].pipe);
+	}
+	free(usb_dev->pipes);
+	usb_dev->pipes = NULL;
+	usb_dev->pipes_count = 0;
+}
+
+usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	return &usb_dev->ctrl_pipe;
+}
+
+usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
+    const usb_endpoint_description_t *desc)
+{
+	assert(usb_dev);
+	for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
+		if (usb_dev->pipes[i].description == desc)
+			return &usb_dev->pipes[i];
+	}
+	return NULL;
+}
+
+usb_endpoint_mapping_t * usb_device_get_mapped_ep(
+    usb_device_t *usb_dev, usb_endpoint_t ep)
+{
+	assert(usb_dev);
+	for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
+		if (usb_dev->pipes[i].pipe.endpoint_no == ep)
+			return &usb_dev->pipes[i];
+	}
+	return NULL;
+}
+
+int usb_device_get_iface_number(usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	return usb_dev->interface_no;
+}
+
+devman_handle_t usb_device_get_devman_handle(usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	return usb_dev->handle;
+}
+
+const usb_device_descriptors_t *usb_device_descriptors(usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	return &usb_dev->descriptors;
+}
+
+const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
+    usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	return &usb_dev->alternate_interfaces;
+}
+
+/** Clean instance of a USB device.
+ *
+ * @param dev Device to be de-initialized.
+ *
+ * Does not free/destroy supplied pointer.
+ */
+static void usb_device_fini(usb_device_t *usb_dev)
+{
+	if (usb_dev) {
+		/* Destroy existing pipes. */
+		usb_device_destroy_pipes(usb_dev);
+		/* Ignore errors and hope for the best. */
+		usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
+		usb_device_release_descriptors(usb_dev);
+		free(usb_dev->driver_data);
+		usb_dev->driver_data = NULL;
+		usb_dev_disconnect(usb_dev->bus_session);
+		usb_dev->bus_session = NULL;
+	}
 }
 
@@ -397,62 +379,44 @@
  * @return Error code.
  */
-int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
-    const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
+static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
+    const usb_endpoint_description_t **endpoints, const char **errstr_ptr,
+    devman_handle_t handle, int interface_no)
 {
 	assert(usb_dev != NULL);
-	assert(ddf_dev != NULL);
+	assert(errstr_ptr);
 
 	*errstr_ptr = NULL;
 
 	usb_dev->ddf_dev = ddf_dev;
+	usb_dev->handle = handle;
+	usb_dev->interface_no = interface_no;
 	usb_dev->driver_data = NULL;
-	usb_dev->descriptors.configuration = NULL;
+	usb_dev->descriptors.full_config = NULL;
+	usb_dev->descriptors.full_config_size = 0;
 	usb_dev->pipes_count = 0;
 	usb_dev->pipes = NULL;
 
-	/* Get assigned params */
-	devman_handle_t hc_handle;
-	usb_address_t address;
-
-	int rc = usb_get_info_by_handle(ddf_dev_get_handle(ddf_dev),
-	    &hc_handle, &address, &usb_dev->interface_no);
-	if (rc != EOK) {
-		*errstr_ptr = "device parameters retrieval";
-		return rc;
-	}
-
-	/* Initialize hc connection. */
-	usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
-
-	/* Initialize backing wire and control pipe. */
-	rc = usb_device_connection_initialize(
-	    &usb_dev->wire, &usb_dev->hc_conn, address);
-	if (rc != EOK) {
-		*errstr_ptr = "device connection initialization";
-		return rc;
+	usb_dev->bus_session = usb_dev_connect(handle);
+
+	if (!usb_dev->bus_session) {
+		*errstr_ptr = "device bus session create";
+		return ENOMEM;
 	}
 
 	/* This pipe was registered by the hub driver,
 	 * during device initialization. */
-	rc = usb_pipe_initialize_default_control(
-	    &usb_dev->ctrl_pipe, &usb_dev->wire);
+	int rc = usb_pipe_initialize_default_control(
+	    &usb_dev->ctrl_pipe, usb_dev->bus_session);
 	if (rc != EOK) {
+		usb_dev_disconnect(usb_dev->bus_session);
 		*errstr_ptr = "default control pipe initialization";
 		return rc;
 	}
 
-	/* Open hc connection for pipe registration. */
-	rc = usb_hc_connection_open(&usb_dev->hc_conn);
-	if (rc != EOK) {
-		*errstr_ptr = "hc connection open";
-		return rc;
-	}
-
 	/* Retrieve standard descriptors. */
-	rc = usb_device_retrieve_descriptors(
-	    &usb_dev->ctrl_pipe, &usb_dev->descriptors);
+	rc = usb_device_retrieve_descriptors(usb_dev);
 	if (rc != EOK) {
 		*errstr_ptr = "descriptor retrieval";
-		usb_hc_connection_close(&usb_dev->hc_conn);
+		usb_dev_disconnect(usb_dev->bus_session);
 		return rc;
 	}
@@ -463,47 +427,134 @@
 	 * controlling a device. */
 	rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
-	    usb_dev->descriptors.configuration,
-	    usb_dev->descriptors.configuration_size, usb_dev->interface_no);
-	const int alternate_iface =
-	    (rc == EOK) ? usb_dev->alternate_interfaces.current : 0;
-
-	/* Create and register other pipes than default control (EP 0) */
-	rc = usb_device_create_pipes(&usb_dev->wire, endpoints,
-	    usb_dev->descriptors.configuration,
-	    usb_dev->descriptors.configuration_size,
-	    usb_dev->interface_no, (int)alternate_iface,
-	    &usb_dev->pipes, &usb_dev->pipes_count);
-	if (rc != EOK) {
-		usb_hc_connection_close(&usb_dev->hc_conn);
-		/* Full configuration descriptor is allocated. */
-		usb_device_release_descriptors(&usb_dev->descriptors);
-		/* Alternate interfaces may be allocated */
-		usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
-		*errstr_ptr = "pipes initialization";
-		return rc;
-	}
-
-	usb_hc_connection_close(&usb_dev->hc_conn);
+	    usb_dev->descriptors.full_config,
+	    usb_dev->descriptors.full_config_size, usb_dev->interface_no);
+
+	if (endpoints) {
+		/* Create and register other pipes than default control (EP 0)*/
+		rc = usb_device_create_pipes(usb_dev, endpoints);
+		if (rc != EOK) {
+			usb_device_fini(usb_dev);
+			*errstr_ptr = "pipes initialization";
+			return rc;
+		}
+	}
+
 	return EOK;
 }
 
-/** Clean instance of a USB device.
- *
- * @param dev Device to be de-initialized.
- *
- * Does not free/destroy supplied pointer.
- */
-void usb_device_deinit(usb_device_t *dev)
-{
-	if (dev) {
-		/* Destroy existing pipes. */
-		destroy_current_pipes(dev);
-		/* Ignore errors and hope for the best. */
-		usb_hc_connection_deinitialize(&dev->hc_conn);
-		usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
-		usb_device_release_descriptors(&dev->descriptors);
-		free(dev->driver_data);
-		dev->driver_data = NULL;
-	}
+static int usb_device_get_info(async_sess_t *sess, devman_handle_t *handle,
+	int *iface_no)
+{
+	assert(handle);
+	assert(iface_no);
+	async_exch_t *exch = async_exchange_begin(sess);
+	if (!exch)
+		return EPARTY;
+	int ret = usb_get_my_device_handle(exch, handle);
+	if (ret == EOK) {
+		ret = usb_get_my_interface(exch, iface_no);
+		if (ret == ENOTSUP) {
+			*iface_no = -1;
+			ret = EOK;
+		}
+	}
+	async_exchange_end(exch);
+	return ret;
+}
+
+int usb_device_create_ddf(ddf_dev_t *ddf_dev,
+    const usb_endpoint_description_t **desc, const char **err)
+{
+	assert(ddf_dev);
+	assert(err);
+
+	devman_handle_t h = 0;
+	int iface_no = -1;
+
+	async_sess_t *sess = devman_parent_device_connect(
+	    ddf_dev_get_handle(ddf_dev), IPC_FLAG_BLOCKING);
+	const int ret = usb_device_get_info(sess, &h, &iface_no);
+	async_hangup(sess);
+	if (ret != EOK)
+		return ret;
+
+	usb_device_t *usb_dev =
+	    ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
+	if (usb_dev == NULL) {
+		*err = "DDF data alloc";
+		return ENOMEM;
+	}
+	
+	return usb_device_init(usb_dev, ddf_dev, desc, err, h, iface_no);
+}
+
+void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
+{
+	assert(ddf_dev);
+	usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
+	assert(usb_dev);
+	usb_device_fini(usb_dev);
+	return;
+}
+
+usb_device_t * usb_device_create(devman_handle_t handle)
+{
+	devman_handle_t h = 0;
+	int iface_no = -1;
+
+	async_sess_t *sess = devman_device_connect(handle, IPC_FLAG_BLOCKING);
+	int ret = usb_device_get_info(sess, &h, &iface_no);
+	if (sess)
+		async_hangup(sess);
+	if (ret != EOK)
+		return NULL;
+
+	usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
+	if (!usb_dev)
+		return NULL;
+
+	const char* dummy = NULL;
+	ret = usb_device_init(usb_dev, NULL, NULL, &dummy, handle, iface_no);
+	if (ret != EOK) {
+		free(usb_dev);
+		usb_dev = NULL;
+	}
+	return usb_dev;
+}
+
+void usb_device_destroy(usb_device_t *usb_dev)
+{
+	if (usb_dev) {
+		usb_device_fini(usb_dev);
+		free(usb_dev);
+	}
+}
+
+const char *usb_device_get_name(usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	if (usb_dev->ddf_dev)
+		return ddf_dev_get_name(usb_dev->ddf_dev);
+	return NULL;
+}
+
+ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
+    const char* name)
+{
+	assert(usb_dev);
+	if (usb_dev->ddf_dev)
+		return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
+	return NULL;
+}
+
+async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	return async_exchange_begin(usb_dev->bus_session);
+}
+
+void usb_device_bus_exchange_end(async_exch_t *exch)
+{
+	async_exchange_end(exch);
 }
 
@@ -521,4 +572,10 @@
 }
 
+void * usb_device_data_get(usb_device_t *usb_dev)
+{
+	assert(usb_dev);
+	return usb_dev->driver_data;
+}
+
 /**
  * @}
Index: uspace/lib/usbdev/src/devpoll.c
===================================================================
--- uspace/lib/usbdev/src/devpoll.c	(revision 1cf26abf038d0db4ca5f4e0a43ca1b41d655b01e)
+++ uspace/lib/usbdev/src/devpoll.c	(revision b4b534ac1d1515633baff0d563e9c16938bfe19f)
@@ -33,11 +33,21 @@
  * USB device driver framework - automatic interrupt polling.
  */
+#include <usb/dev/device.h>
+#include <usb/dev/pipes.h>
 #include <usb/dev/poll.h>
 #include <usb/dev/request.h>
+#include <usb/classes/classes.h>
 #include <usb/debug.h>
-#include <usb/classes/classes.h>
+#include <usb/descriptor.h>
+#include <usb/usb.h>
+
+#include <assert.h>
+#include <async.h>
 #include <errno.h>
+#include <fibril.h>
+#include <stdbool.h>
+#include <stdlib.h>
 #include <str_error.h>
-#include <assert.h>
+#include <sys/types.h>
 
 /** Maximum number of failed consecutive requests before announcing failure. */
@@ -51,6 +61,6 @@
 	/** USB device to poll. */
 	usb_device_t *dev;
-	/** Device pipe to use for polling. */
-	size_t pipe_index;
+	/** Device enpoint mapping to use for polling. */
+	usb_endpoint_mapping_t *polling_mapping;
 	/** Size of the recieved data. */
 	size_t request_size;
@@ -72,13 +82,12 @@
 	const usb_device_auto_polling_t *params = &data->auto_polling;
 
-	usb_pipe_t *pipe
-	    = &data->dev->pipes[data->pipe_index].pipe;
+	usb_pipe_t *pipe = &data->polling_mapping->pipe;
 
 	if (params->debug > 0) {
 		const usb_endpoint_mapping_t *mapping
-		    = &data->dev->pipes[data->pipe_index];
-		usb_log_debug("Poll%p: started polling of `%s' - " \
+		    = data->polling_mapping;
+		usb_log_debug("Poll (%p): started polling of `%s' - " \
 		    "interface %d (%s,%d,%d), %zuB/%zu.\n",
-		    data, ddf_dev_get_name(data->dev->ddf_dev),
+		    data, usb_device_get_name(data->dev),
 		    (int) mapping->interface->interface_number,
 		    usb_str_class(mapping->interface->interface_class),
@@ -88,5 +97,4 @@
 	}
 
-	usb_pipe_start_long_transfer(pipe);
 	size_t failed_attempts = 0;
 	while (failed_attempts <= params->max_failures) {
@@ -95,6 +103,6 @@
 		    data->request_size, &actual_size);
 
-		if (params->debug > 1) {
-			if (rc == EOK) {
+		if (rc == EOK) {
+			if (params->debug > 1) {
 				usb_log_debug(
 				    "Poll%p: received: '%s' (%zuB).\n",
@@ -103,9 +111,9 @@
 				        actual_size, 16),
 				    actual_size);
-			} else {
+			}
+		} else {
 				usb_log_debug(
 				    "Poll%p: polling failed: %s.\n",
 				    data, str_error(rc));
-			}
 		}
 
@@ -117,5 +125,6 @@
 			 */
 			usb_request_clear_endpoint_halt(
-			    &data->dev->ctrl_pipe, pipe->endpoint_no);
+			    usb_device_get_default_pipe(data->dev),
+			    pipe->endpoint_no);
 		}
 
@@ -145,8 +154,9 @@
 
 		/* Take a rest before next request. */
+		//TODO: This is broken, the time is in ms not us.
+		// but first we need to fix drivers to actually stop using this,
+		// since polling dealy should be implemented in HC schedule
 		async_usleep(params->delay);
 	}
-
-	usb_pipe_end_long_transfer(pipe);
 
 	const bool failed = failed_attempts > 0;
@@ -159,10 +169,10 @@
 		if (failed) {
 			usb_log_error("Polling of device `%s' terminated: "
-			    "recurring failures.\n", ddf_dev_get_name(
-			    data->dev->ddf_dev));
+			    "recurring failures.\n",
+			    usb_device_get_name(data->dev));
 		} else {
 			usb_log_debug("Polling of device `%s' terminated: "
-			    "driver request.\n", ddf_dev_get_name(
-			    data->dev->ddf_dev));
+			    "driver request.\n",
+			    usb_device_get_name(data->dev));
 		}
 	}
@@ -175,8 +185,9 @@
 }
 
+
 /** Start automatic device polling over interrupt in pipe.
  *
- * @warning It is up to the callback to produce delays between individual
- * requests.
+ * The polling settings is copied thus it is okay to destroy the structure
+ * after this function returns.
  *
  * @warning There is no guarantee when the request to the device
@@ -185,7 +196,101 @@
  *
  * @param dev Device to be periodically polled.
+ * @param epm Endpoint mapping to use.
+ * @param polling Polling settings.
+ * @param request_size How many bytes to ask for in each request.
+ * @param arg Custom argument (passed as is to the callbacks).
+ * @return Error code.
+ * @retval EOK New fibril polling the device was already started.
+ */
+static int usb_device_auto_polling_internal(usb_device_t *dev,
+    usb_endpoint_mapping_t *epm, const usb_device_auto_polling_t *polling,
+    size_t request_size)
+{
+	if ((dev == NULL) || (polling == NULL) || (polling->on_data == NULL)) {
+		return EBADMEM;
+	}
+
+	if (request_size == 0)
+		return EINVAL;
+
+	if (!epm || (epm->pipe.transfer_type != USB_TRANSFER_INTERRUPT) ||
+	    (epm->pipe.direction != USB_DIRECTION_IN))
+		return EINVAL;
+
+
+	polling_data_t *polling_data = malloc(sizeof(polling_data_t));
+	if (polling_data == NULL) {
+		return ENOMEM;
+	}
+
+	/* Fill-in the data. */
+	polling_data->buffer = malloc(sizeof(request_size));
+	if (polling_data->buffer == NULL) {
+		free(polling_data);
+		return ENOMEM;
+	}
+	polling_data->request_size = request_size;
+	polling_data->dev = dev;
+	polling_data->polling_mapping = epm;
+
+	/* Copy provided settings. */
+	polling_data->auto_polling = *polling;
+
+	/* Negative value means use descriptor provided value. */
+	if (polling->delay < 0) {
+		polling_data->auto_polling.delay =
+		    epm->descriptor->poll_interval;
+	}
+
+	fid_t fibril = fibril_create(polling_fibril, polling_data);
+	if (fibril == 0) {
+		free(polling_data->buffer);
+		free(polling_data);
+		return ENOMEM;
+	}
+	fibril_add_ready(fibril);
+
+	/* Fibril launched. That fibril will free the allocated data. */
+
+	return EOK;
+}
+/** Start automatic device polling over interrupt in pipe.
+ *
+ * The polling settings is copied thus it is okay to destroy the structure
+ * after this function returns.
+ *
+ * @warning There is no guarantee when the request to the device
+ * will be sent for the first time (it is possible that this
+ * first request would be executed prior to return from this function).
+ *
+ * @param dev Device to be periodically polled.
  * @param pipe_index Index of the endpoint pipe used for polling.
+ * @param polling Polling settings.
+ * @param req_size How many bytes to ask for in each request.
+ * @param arg Custom argument (passed as is to the callbacks).
+ * @return Error code.
+ * @retval EOK New fibril polling the device was already started.
+ */
+int usb_device_auto_polling(usb_device_t *usb_dev, usb_endpoint_t ep,
+    const usb_device_auto_polling_t *polling, size_t req_size)
+{
+	usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep(usb_dev, ep);
+	return usb_device_auto_polling_internal(usb_dev, epm, polling, req_size);
+}
+
+/** Start automatic device polling over interrupt in pipe.
+ *
+ * @warning It is up to the callback to produce delays between individual
+ * requests.
+ *
+ * @warning There is no guarantee when the request to the device
+ * will be sent for the first time (it is possible that this
+ * first request would be executed prior to return from this function).
+ *
+ * @param dev Device to be periodically polled.
+ * @param ep Endpoint  used for polling.
  * @param callback Callback when data are available.
  * @param request_size How many bytes to ask for in each request.
+ * @param delay NUmber of ms to wait between queries, -1 to use descriptor val.
  * @param terminated_callback Callback when polling is terminated.
  * @param arg Custom argument (passed as is to the callbacks).
@@ -193,6 +298,6 @@
  * @retval EOK New fibril polling the device was already started.
  */
-int usb_device_auto_poll(usb_device_t *dev, size_t pipe_index,
-    usb_polling_callback_t callback, size_t request_size,
+int usb_device_auto_poll(usb_device_t *dev, usb_endpoint_t ep,
+    usb_polling_callback_t callback, size_t request_size, int delay,
     usb_polling_terminted_callback_t terminated_callback, void *arg)
 {
@@ -200,5 +305,5 @@
 		.debug = 1,
 		.auto_clear_halt = true,
-		.delay = 0,
+		.delay = delay,
 		.max_failures = MAX_FAILED_ATTEMPTS,
 		.on_data = callback,
@@ -208,76 +313,38 @@
 	};
 
-	return usb_device_auto_polling(dev, pipe_index, &auto_polling,
-	   request_size);
-}
-
-/** Start automatic device polling over interrupt in pipe.
- *
- * The polling settings is copied thus it is okay to destroy the structure
- * after this function returns.
- *
- * @warning There is no guarantee when the request to the device
- * will be sent for the first time (it is possible that this
- * first request would be executed prior to return from this function).
- *
- * @param dev Device to be periodically polled.
- * @param pipe_index Index of the endpoint pipe used for polling.
- * @param polling Polling settings.
- * @param request_size How many bytes to ask for in each request.
- * @param arg Custom argument (passed as is to the callbacks).
- * @return Error code.
- * @retval EOK New fibril polling the device was already started.
- */
-int usb_device_auto_polling(usb_device_t *dev, size_t pipe_index,
-    const usb_device_auto_polling_t *polling,
-    size_t request_size)
-{
-	if ((dev == NULL) || (polling == NULL) || (polling->on_data == NULL)) {
-		return EBADMEM;
-	}
-
-	if (pipe_index >= dev->pipes_count || request_size == 0) {
-		return EINVAL;
-	}
-	if ((dev->pipes[pipe_index].pipe.transfer_type != USB_TRANSFER_INTERRUPT)
-	    || (dev->pipes[pipe_index].pipe.direction != USB_DIRECTION_IN)) {
-		return EINVAL;
-	}
-
-	polling_data_t *polling_data = malloc(sizeof(polling_data_t));
-	if (polling_data == NULL) {
-		return ENOMEM;
-	}
-
-	/* Fill-in the data. */
-	polling_data->buffer = malloc(sizeof(request_size));
-	if (polling_data->buffer == NULL) {
-		free(polling_data);
-		return ENOMEM;
-	}
-	polling_data->request_size = request_size;
-	polling_data->dev = dev;
-	polling_data->pipe_index = pipe_index;
-
-	/* Copy provided settings. */
-	polling_data->auto_polling = *polling;
-
-	/* Negative value means use descriptor provided value. */
-	if (polling->delay < 0) {
-		polling_data->auto_polling.delay =
-		    (int) dev->pipes[pipe_index].descriptor->poll_interval;
-	}
-
-	fid_t fibril = fibril_create(polling_fibril, polling_data);
-	if (fibril == 0) {
-		free(polling_data->buffer);
-		free(polling_data);
-		return ENOMEM;
-	}
-	fibril_add_ready(fibril);
-
-	/* Fibril launched. That fibril will free the allocated data. */
-
-	return EOK;
+	usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep(dev, ep);
+	return usb_device_auto_polling_internal(
+	    dev, epm, &auto_polling, request_size);
+}
+
+int usb_device_auto_polling_desc(usb_device_t *usb_dev,
+    const usb_endpoint_description_t *desc,
+    const usb_device_auto_polling_t *polling, size_t req_size)
+{
+	usb_endpoint_mapping_t *epm =
+	    usb_device_get_mapped_ep_desc(usb_dev, desc);
+	return usb_device_auto_polling_internal(usb_dev, epm, polling, req_size);
+}
+
+int usb_device_auto_poll_desc(usb_device_t * usb_dev,
+    const usb_endpoint_description_t *desc, usb_polling_callback_t callback,
+    size_t req_size, int delay,
+    usb_polling_terminted_callback_t terminated_callback, void *arg)
+{
+	const usb_device_auto_polling_t auto_polling = {
+		.debug = 1,
+		.auto_clear_halt = true,
+		.delay = delay,
+		.max_failures = MAX_FAILED_ATTEMPTS,
+		.on_data = callback,
+		.on_polling_end = terminated_callback,
+		.on_error = NULL,
+		.arg = arg,
+	};
+
+	usb_endpoint_mapping_t *epm =
+	    usb_device_get_mapped_ep_desc(usb_dev, desc);
+	return usb_device_auto_polling_internal(
+	    usb_dev, epm, &auto_polling, req_size);
 }
 
Index: uspace/lib/usbdev/src/dp.c
===================================================================
--- uspace/lib/usbdev/src/dp.c	(revision 1cf26abf038d0db4ca5f4e0a43ca1b41d655b01e)
+++ uspace/lib/usbdev/src/dp.c	(revision b4b534ac1d1515633baff0d563e9c16938bfe19f)
@@ -41,11 +41,12 @@
  * sibling.
  */
-#include <stdio.h>
-#include <str_error.h>
-#include <errno.h>
-#include <assert.h>
-#include <stdbool.h>
 #include <usb/dev/dp.h>
 #include <usb/descriptor.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <sys/types.h>
 
 #define NESTING(parentname, childname) \
@@ -304,5 +305,5 @@
  * @param arg Custom (user) argument.
  */
-void usb_dp_walk_simple(uint8_t *descriptors, size_t descriptors_size,
+void usb_dp_walk_simple(const uint8_t *descriptors, size_t descriptors_size,
     const usb_dp_descriptor_nesting_t *descriptor_nesting,
     walk_callback_t callback, void *arg)
Index: uspace/lib/usbdev/src/driver.c
===================================================================
--- uspace/lib/usbdev/src/driver.c	(revision b4b534ac1d1515633baff0d563e9c16938bfe19f)
+++ uspace/lib/usbdev/src/driver.c	(revision b4b534ac1d1515633baff0d563e9c16938bfe19f)
@@ -0,0 +1,150 @@
+/*
+ * Copyright (c) 2011 Vojtech Horky
+ * 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 libusbdev
+ * @{
+ */
+/** @file
+ * USB device driver framework.
+ */
+
+#include <usb/dev/driver.h>
+#include <usb/dev/device.h>
+#include <usb/debug.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <str_error.h>
+#include <ddf/driver.h>
+
+static const usb_driver_t *driver = NULL;
+
+/** Callback when a new device is supposed to be controlled by this driver.
+ *
+ * This callback is a wrapper for USB specific version of @c device_add.
+ *
+ * @param gen_dev Device structure as prepared by DDF.
+ * @return Error code.
+ */
+static int generic_device_add(ddf_dev_t *gen_dev)
+{
+	assert(driver);
+	assert(driver->ops);
+	assert(driver->ops->device_add);
+
+	/* Initialize generic USB driver data. */
+	const char *err_msg = NULL;
+	int rc = usb_device_create_ddf(gen_dev, driver->endpoints, &err_msg);
+	if (rc != EOK) {
+		usb_log_error("USB device `%s' init failed (%s): %s.\n",
+		    ddf_dev_get_name(gen_dev), err_msg, str_error(rc));
+		return rc;
+	}
+
+	/* Start USB driver specific initialization. */
+	rc = driver->ops->device_add(ddf_dev_data_get(gen_dev));
+	if (rc != EOK)
+		usb_device_destroy_ddf(gen_dev);
+	return rc;
+}
+
+/** Callback when a device is supposed to be removed from the system.
+ *
+ * This callback is a wrapper for USB specific version of @c device_remove.
+ *
+ * @param gen_dev Device structure as prepared by DDF.
+ * @return Error code.
+ */
+static int generic_device_remove(ddf_dev_t *gen_dev)
+{
+	assert(driver);
+	assert(driver->ops);
+	if (driver->ops->device_rem == NULL)
+		return ENOTSUP;
+	/* Just tell the driver to stop whatever it is doing */
+	usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
+	const int ret = driver->ops->device_rem(usb_dev);
+	if (ret != EOK)
+		return ret;
+	usb_device_destroy_ddf(gen_dev);
+	return EOK;
+}
+
+/** Callback when a device was removed from the system.
+ *
+ * This callback is a wrapper for USB specific version of @c device_gone.
+ *
+ * @param gen_dev Device structure as prepared by DDF.
+ * @return Error code.
+ */
+static int generic_device_gone(ddf_dev_t *gen_dev)
+{
+	assert(driver);
+	assert(driver->ops);
+	if (driver->ops->device_gone == NULL)
+		return ENOTSUP;
+	usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
+	const int ret = driver->ops->device_gone(usb_dev);
+	if (ret == EOK)
+		usb_device_destroy_ddf(gen_dev);
+
+	return ret;
+}
+
+static driver_ops_t generic_driver_ops = {
+	.dev_add = generic_device_add,
+	.dev_remove = generic_device_remove,
+	.dev_gone = generic_device_gone,
+};
+static driver_t generic_driver = {
+	.driver_ops = &generic_driver_ops
+};
+
+
+/** Main routine of USB device driver.
+ *
+ * Under normal conditions, this function never returns.
+ *
+ * @param drv USB device driver structure.
+ * @return Task exit status.
+ */
+int usb_driver_main(const usb_driver_t *drv)
+{
+	assert(drv != NULL);
+
+	/* Prepare the generic driver. */
+	generic_driver.name = drv->name;
+
+	driver = drv;
+
+	return ddf_driver_main(&generic_driver);
+}
+
+/**
+ * @}
+ */
Index: uspace/lib/usbdev/src/hub.c
===================================================================
--- uspace/lib/usbdev/src/hub.c	(revision 1cf26abf038d0db4ca5f4e0a43ca1b41d655b01e)
+++ 	(revision )
@@ -1,319 +1,0 @@
-/*
- * Copyright (c) 2011 Vojtech Horky
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup libusbdev
- * @{
- */
-/** @file
- * Functions needed by hub drivers.
- */
-
-#include <usb/dev/hub.h>
-#include <usb/dev/pipes.h>
-#include <usb/dev/request.h>
-#include <usb/dev/recognise.h>
-#include <usb/debug.h>
-#include <errno.h>
-#include <assert.h>
-#include <time.h>
-#include <async.h>
-
-/** How much time to wait between attempts to get the default address.
- * The value is based on typical value for port reset + some overhead.
- */
-#define DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC (1000 * (10 + 2))
-
-/** Inform host controller about new device.
- *
- * @param connection Opened connection to host controller.
- * @param attached_device Information about the new device.
- * @return Error code.
- */
-int usb_hub_register_device(usb_hc_connection_t *connection,
-    const usb_hub_attached_device_t *attached_device)
-{
-	assert(connection);
-	if (attached_device == NULL || attached_device->fun == NULL)
-		return EBADMEM;
-	return usb_hc_bind_address(connection,
-	    attached_device->address, ddf_fun_get_handle(attached_device->fun));
-}
-
-/** Change address of connected device.
- * This function automatically updates the backing connection to point to
- * the new address. It also unregisterrs the old endpoint and registers
- * a new one.
- * This creates whole bunch of problems:
- *  1. All pipes using this wire are broken because they are not
- *     registered for new address
- *  2. All other pipes for this device are using wrong address,
- *     possibly targeting completely different device
- *
- * @param pipe Control endpoint pipe (session must be already started).
- * @param new_address New USB address to be set (in native endianness).
- * @return Error code.
- */
-static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address)
-{
-	if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
-		return EINVAL;
-	}
-	assert(pipe);
-	assert(pipe->wire != NULL);
-
-	const uint16_t addr = uint16_host2usb((uint16_t)new_address);
-
-	int rc = usb_control_request_set(pipe,
-	    USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
-	    USB_DEVREQ_SET_ADDRESS, addr, 0, NULL, 0);
-
-	if (rc != EOK) {
-		return rc;
-	}
-
-	/* TODO: prevent others from accessing the wire now. */
-	if (usb_pipe_unregister(pipe) != EOK) {
-		usb_log_warning(
-		    "Failed to unregister the old pipe on address change.\n");
-	}
-	/* Address changed. We can release the old one, thus
-	 * allowing other to us it. */
-	usb_hc_release_address(pipe->wire->hc_connection, pipe->wire->address);
-
-	/* The address is already changed so set it in the wire */
-	pipe->wire->address = new_address;
-	rc = usb_pipe_register(pipe, 0);
-	if (rc != EOK)
-		return EADDRNOTAVAIL;
-
-	return EOK;
-}
-
-/** Wrapper for registering attached device to the hub.
- *
- * The @p enable_port function is expected to enable signaling on given
- * port.
- * The argument can have arbitrary meaning and it is not touched at all
- * by this function (it is passed as is to the @p enable_port function).
- *
- * If the @p enable_port fails (i.e. does not return EOK), the device
- * addition is canceled.
- * The return value is then returned (it is good idea to use different
- * error codes than those listed as return codes by this function itself).
- *
- * The @p connection representing connection with host controller does not
- * need to be started.
- * This function duplicates the connection to allow simultaneous calls of
- * this function (i.e. from different fibrils).
- *
- * @param[in] parent Parent device (i.e. the hub device).
- * @param[in] connection Connection to host controller. Must be non-null.
- * @param[in] dev_speed New device speed.
- * @param[in] enable_port Function for enabling signaling through the port the
- *	device is attached to.
- * @param[in] arg Any data argument to @p enable_port.
- * @param[out] assigned_address USB address of the device.
- * @param[in] dev_ops Child device ops. Will use default if not provided.
- * @param[in] new_dev_data Arbitrary pointer to be stored in the child
- *	as @c driver_data. Will allocate and assign usb_hub_attached_device_t
- *	structure if NULL.
- * @param[out] new_fun Storage where pointer to allocated child function
- *	will be written. Must be non-null.
- * @return Error code.
- * @retval EINVAL Either connection or new_fun is a NULL pointer.
- * @retval ENOENT Connection to HC not opened.
- * @retval EADDRNOTAVAIL Failed retrieving free address from host controller.
- * @retval EBUSY Failed reserving default USB address.
- * @retval ENXIO Problem connecting to the host controller via USB pipe.
- * @retval ESTALL Problem communication with device (either SET_ADDRESS
- *	request or requests for descriptors when creating match ids).
- */
-int usb_hc_new_device_wrapper(ddf_dev_t *parent, ddf_fun_t *fun,
-    usb_hc_connection_t *hc_conn, usb_speed_t dev_speed,
-    int (*enable_port)(void *arg), void *arg, usb_address_t *assigned_address,
-    ddf_dev_ops_t *dev_ops)
-{
-	if (hc_conn == NULL)
-		return EINVAL;
-	
-	struct timeval start_time;
-	gettimeofday(&start_time, NULL);
-	
-	/* We are gona do a lot of communication better open it in advance. */
-	int rc = usb_hc_connection_open(hc_conn);
-	if (rc != EOK)
-		return rc;
-	
-	/* Request a new address. */
-	usb_address_t dev_addr =
-	    usb_hc_request_address(hc_conn, 0, false, dev_speed);
-	if (dev_addr < 0) {
-		rc = EADDRNOTAVAIL;
-		goto close_connection;
-	}
-
-	/* Initialize connection to device. */
-	usb_device_connection_t dev_conn;
-	rc = usb_device_connection_initialize(
-	    &dev_conn, hc_conn, USB_ADDRESS_DEFAULT);
-	if (rc != EOK) {
-		rc = ENXIO;
-		goto leave_release_free_address;
-	}
-
-	/* Initialize control pipe on default address. Don't register yet. */
-	usb_pipe_t ctrl_pipe;
-	rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn);
-	if (rc != EOK) {
-		rc = ENXIO;
-		goto leave_release_free_address;
-	}
-
-	/*
-	 * The default address request might fail.
-	 * That means that someone else is already using that address.
-	 * We will simply wait and try again.
-	 * (Someone else already wants to add a new device.)
-	 */
-	do {
-		rc = usb_hc_request_address(hc_conn, USB_ADDRESS_DEFAULT,
-		    true, dev_speed);
-		if (rc == ENOENT) {
-			/* Do not overheat the CPU ;-). */
-			async_usleep(DEFAULT_ADDRESS_ATTEMPT_DELAY_USEC);
-		}
-	} while (rc == ENOENT);
-	if (rc < 0) {
-		goto leave_release_free_address;
-	}
-
-	/* Register control pipe on default address. 0 means no interval. */
-	rc = usb_pipe_register(&ctrl_pipe, 0);
-	if (rc != EOK) {
-		rc = ENXIO;
-		goto leave_release_default_address;
-	}
-	
-	struct timeval end_time;
-	gettimeofday(&end_time, NULL);
-	
-	/* According to the USB spec part 9.1.2 host allows 100ms time for
-	 * the insertion process to complete. According to 7.1.7.1 this is the
-	 * time between attach detected and port reset. However, the setup done
-	 * above might use much of this time so we should only wait to fill
-	 * up the 100ms quota*/
-	const suseconds_t elapsed = tv_sub_diff(&end_time, &start_time);
-	if (elapsed < 100000) {
-		async_usleep(100000 - elapsed);
-	}
-
-	/* Endpoint is registered. We can enable the port and change address. */
-	rc = enable_port(arg);
-	if (rc != EOK) {
-		goto leave_release_default_address;
-	}
-	/* USB spec 7.1.7.1: The USB System Software guarantees a minimum of
-	 * 10ms for reset recovery. Device response to any bus transactions
-	 * addressed to the default device address during the reset recovery
-	 * time is undefined.
-	 */
-	async_usleep(10000);
-
-	/* Get max_packet_size value. */
-	rc = usb_pipe_probe_default_control(&ctrl_pipe);
-	if (rc != EOK) {
-		rc = ESTALL;
-		goto leave_release_default_address;
-	}
-
-	rc = usb_request_set_address(&ctrl_pipe, dev_addr);
-	if (rc != EOK) {
-		rc = ESTALL;
-		goto leave_release_default_address;
-	}
-
-
-	/* Register the device with devman. */
-	/* FIXME: create device_register that will get opened ctrl pipe. */
-	rc = usb_device_register_child_in_devman(&ctrl_pipe,
-	    parent, fun, dev_ops);
-	if (rc != EOK) {
-		goto leave_release_free_address;
-	}
-
-	const usb_hub_attached_device_t new_device = {
-		.address = dev_addr,
-		.fun = fun,
-	};
-
-
-	/* Inform the host controller about the handle. */
-	rc = usb_hub_register_device(hc_conn, &new_device);
-	if (rc != EOK) {
-		/* The child function is already created. */
-		rc = EIO;
-		goto leave_release_free_address;
-	}
-
-	if (assigned_address != NULL) {
-		*assigned_address = dev_addr;
-	}
-
-	rc = EOK;
-	goto close_connection;
-
-	/*
-	 * Error handling (like nested exceptions) starts here.
-	 * Completely ignoring errors here.
-	 */
-leave_release_default_address:
-	if (usb_hc_release_address(hc_conn, USB_ADDRESS_DEFAULT) != EOK)
-		usb_log_warning("%s: Failed to release defaut address.\n",
-		    __FUNCTION__);
-
-leave_release_free_address:
-	/* This might be either 0:0 or dev_addr:0 */
-	if (usb_pipe_unregister(&ctrl_pipe) != EOK)
-		usb_log_warning("%s: Failed to unregister default pipe.\n",
-		    __FUNCTION__);
-
-	if (usb_hc_release_address(hc_conn, dev_addr) != EOK)
-		usb_log_warning("%s: Failed to release address: %d.\n",
-		    __FUNCTION__, dev_addr);
-
-close_connection:
-	if (usb_hc_connection_close(hc_conn) != EOK)
-		usb_log_warning("%s: Failed to close hc connection.\n",
-		    __FUNCTION__);
-
-	return rc;
-}
-
-/**
- * @}
- */
Index: uspace/lib/usbdev/src/pipes.c
===================================================================
--- uspace/lib/usbdev/src/pipes.c	(revision 1cf26abf038d0db4ca5f4e0a43ca1b41d655b01e)
+++ uspace/lib/usbdev/src/pipes.c	(revision b4b534ac1d1515633baff0d563e9c16938bfe19f)
@@ -35,36 +35,11 @@
 #include <usb/dev/pipes.h>
 #include <usb/dev/request.h>
+#include <usb/usb.h>
+#include <usb_iface.h>
+
+#include <assert.h>
+#include <async.h>
 #include <errno.h>
-#include <assert.h>
-
-/** Prepare pipe for a long transfer.
- *
- * Long transfer is transfer consisting of several requests to the HC.
- * Calling this function is optional and it has positive effect of
- * improved performance because IPC session is initiated only once.
- *
- * @param pipe Pipe over which the transfer will happen.
- * @return Error code.
- */
-int usb_pipe_start_long_transfer(usb_pipe_t *pipe)
-{
-	assert(pipe);
-	assert(pipe->wire);
-	assert(pipe->wire->hc_connection);
-	return usb_hc_connection_open(pipe->wire->hc_connection);
-}
-
-/** Terminate a long transfer on a pipe.
- * @param pipe Pipe where to end the long transfer.
- * @return Error code.
- * @see usb_pipe_start_long_transfer
- */
-int usb_pipe_end_long_transfer(usb_pipe_t *pipe)
-{
-	assert(pipe);
-	assert(pipe->wire);
-	assert(pipe->wire->hc_connection);
-	return usb_hc_connection_close(pipe->wire->hc_connection);
-}
+#include <mem.h>
 
 /** Try to clear endpoint halt of default control pipe.
@@ -121,7 +96,9 @@
 	memcpy(&setup_packet, setup_buffer, 8);
 
+	async_exch_t *exch = async_exchange_begin(pipe->bus_session);
 	size_t act_size = 0;
-	const int rc = usb_device_control_read(pipe->wire,
+	const int rc = usb_read(exch,
 	    pipe->endpoint_no, setup_packet, buffer, buffer_size, &act_size);
+	async_exchange_end(exch);
 
 	if (rc == ESTALL) {
@@ -173,6 +150,8 @@
 	memcpy(&setup_packet, setup_buffer, 8);
 
-	const int rc = usb_device_control_write(pipe->wire,
+	async_exch_t *exch = async_exchange_begin(pipe->bus_session);
+	const int rc = usb_write(exch,
 	    pipe->endpoint_no, setup_packet, buffer, buffer_size);
+	async_exchange_end(exch);
 
 	if (rc == ESTALL) {
@@ -217,7 +196,9 @@
 	    return ENOTSUP;
 
+	async_exch_t *exch = async_exchange_begin(pipe->bus_session);
 	size_t act_size = 0;
-	const int rc = usb_device_read(pipe->wire,
-	    pipe->endpoint_no, buffer, size, &act_size);
+	const int rc =
+	    usb_read(exch, pipe->endpoint_no, 0, buffer, size, &act_size);
+	async_exchange_end(exch);
 
 	if (rc == EOK && size_transfered != NULL) {
@@ -256,6 +237,8 @@
 	    return ENOTSUP;
 
-	return usb_device_write(pipe->wire,
-	    pipe->endpoint_no, buffer, size);
+	async_exch_t *exch = async_exchange_begin(pipe->bus_session);
+	const int rc = usb_write(exch, pipe->endpoint_no, 0, buffer, size);
+	async_exchange_end(exch);
+	return rc;
 }
 
@@ -263,5 +246,4 @@
  *
  * @param pipe Endpoint pipe to be initialized.
- * @param connection Connection to the USB device backing this pipe (the wire).
  * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
  * @param transfer_type Transfer type (e.g. interrupt or bulk).
@@ -270,18 +252,17 @@
  * @return Error code.
  */
-int usb_pipe_initialize(usb_pipe_t *pipe,
-    usb_device_connection_t *connection, usb_endpoint_t endpoint_no,
+int usb_pipe_initialize(usb_pipe_t *pipe, usb_endpoint_t endpoint_no,
     usb_transfer_type_t transfer_type, size_t max_packet_size,
-    usb_direction_t direction)
-{
-	assert(pipe);
-	assert(connection);
-
-	pipe->wire = connection;
+    usb_direction_t direction, unsigned packets, usb_dev_session_t *bus_session)
+{
+	assert(pipe);
+
 	pipe->endpoint_no = endpoint_no;
 	pipe->transfer_type = transfer_type;
+	pipe->packets = packets;
 	pipe->max_packet_size = max_packet_size;
 	pipe->direction = direction;
 	pipe->auto_reset_halt = false;
+	pipe->bus_session = bus_session;
 
 	return EOK;
@@ -291,15 +272,13 @@
  *
  * @param pipe Endpoint pipe to be initialized.
- * @param connection Connection to the USB device backing this pipe (the wire).
  * @return Error code.
  */
 int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
-    usb_device_connection_t *connection)
-{
-	assert(pipe);
-	assert(connection);
-
-	int rc = usb_pipe_initialize(pipe, connection, 0, USB_TRANSFER_CONTROL,
-	    CTRL_PIPE_MIN_PACKET_SIZE, USB_DIRECTION_BOTH);
+    usb_dev_session_t *bus_session)
+{
+	assert(pipe);
+
+	const int rc = usb_pipe_initialize(pipe, 0, USB_TRANSFER_CONTROL,
+	    CTRL_PIPE_MIN_PACKET_SIZE, USB_DIRECTION_BOTH, 1, bus_session);
 
 	pipe->auto_reset_halt = true;
@@ -317,9 +296,13 @@
 {
 	assert(pipe);
-	assert(pipe->wire);
-
-	return usb_device_register_endpoint(pipe->wire,
-	   pipe->endpoint_no, pipe->transfer_type,
-	   pipe->direction, pipe->max_packet_size, interval);
+	assert(pipe->bus_session);
+	async_exch_t *exch = async_exchange_begin(pipe->bus_session);
+	if (!exch)
+		return ENOMEM;
+	const int ret = usb_register_endpoint(exch, pipe->endpoint_no,
+	    pipe->transfer_type, pipe->direction, pipe->max_packet_size,
+	    pipe->packets, interval);
+	async_exchange_end(exch);
+	return ret;
 }
 
@@ -332,8 +315,12 @@
 {
 	assert(pipe);
-	assert(pipe->wire);
-
-	return usb_device_unregister_endpoint(pipe->wire,
-	    pipe->endpoint_no, pipe->direction);
+	assert(pipe->bus_session);
+	async_exch_t *exch = async_exchange_begin(pipe->bus_session);
+	if (!exch)
+		return ENOMEM;
+	const int ret = usb_unregister_endpoint(exch, pipe->endpoint_no,
+	    pipe->direction);
+	async_exchange_end(exch);
+	return ret;
 }
 
Index: uspace/lib/usbdev/src/pipesinit.c
===================================================================
--- uspace/lib/usbdev/src/pipesinit.c	(revision 1cf26abf038d0db4ca5f4e0a43ca1b41d655b01e)
+++ uspace/lib/usbdev/src/pipesinit.c	(revision b4b534ac1d1515633baff0d563e9c16938bfe19f)
@@ -34,10 +34,12 @@
  *
  */
-#include <usb/usb.h>
 #include <usb/dev/pipes.h>
 #include <usb/dev/dp.h>
 #include <usb/dev/request.h>
+#include <usb/usb.h>
+#include <usb/descriptor.h>
+
+#include <assert.h>
 #include <errno.h>
-#include <assert.h>
 
 #define DEV_DESCR_MAX_PACKET_SIZE_OFFSET 7
@@ -148,5 +150,4 @@
  * @param interface Interface descriptor under which belongs the @p endpoint.
  * @param endpoint Endpoint descriptor.
- * @param wire Connection backing the endpoint pipes.
  * @return Error code.
  */
@@ -155,5 +156,5 @@
     usb_standard_interface_descriptor_t *interface,
     usb_standard_endpoint_descriptor_t *endpoint_desc,
-    usb_device_connection_t *wire)
+    usb_dev_session_t *bus_session)
 {
 
@@ -193,8 +194,11 @@
 	}
 
-	int rc = usb_pipe_initialize(&ep_mapping->pipe, wire,
+	int rc = usb_pipe_initialize(&ep_mapping->pipe,
 	    ep_no, description.transfer_type,
-	    uint16_usb2host(endpoint_desc->max_packet_size),
-	    description.direction);
+	    ED_MPS_PACKET_SIZE_GET(
+	        uint16_usb2host(endpoint_desc->max_packet_size)),
+	    description.direction,
+	    ED_MPS_TRANS_OPPORTUNITIES_GET(
+	        uint16_usb2host(endpoint_desc->max_packet_size)), bus_session);
 	if (rc != EOK) {
 		return rc;
@@ -220,5 +224,5 @@
     usb_endpoint_mapping_t *mapping, size_t mapping_count,
     const usb_dp_parser_t *parser, const usb_dp_parser_data_t *parser_data,
-    const uint8_t *interface_descriptor)
+    const uint8_t *interface_descriptor, usb_dev_session_t *bus_session)
 {
 	const uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
@@ -236,5 +240,5 @@
 			    (usb_standard_endpoint_descriptor_t *)
 			        descriptor,
-			    (usb_device_connection_t *) parser_data->arg);
+			    bus_session);
 		}
 
@@ -280,7 +284,6 @@
     usb_endpoint_mapping_t *mapping, size_t mapping_count,
     const uint8_t *config_descriptor, size_t config_descriptor_size,
-    usb_device_connection_t *connection)
-{
-	assert(connection);
+    usb_dev_session_t *bus_session)
+{
 
 	if (config_descriptor == NULL) {
@@ -306,5 +309,4 @@
 		.data = config_descriptor,
 		.size = config_descriptor_size,
-		.arg = connection
 	};
 
@@ -319,5 +321,5 @@
 	do {
 		(void) process_interface(mapping, mapping_count,
-		    &dp_parser, &dp_data, interface);
+		    &dp_parser, &dp_data, interface, bus_session);
 		interface = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
 		    config_descriptor, interface);
@@ -347,7 +349,4 @@
 		return EINVAL;
 	}
-
-
-	usb_pipe_start_long_transfer(pipe);
 
 	uint8_t dev_descr_start[CTRL_PIPE_MIN_PACKET_SIZE];
@@ -367,5 +366,4 @@
 		}
 	}
-	usb_pipe_end_long_transfer(pipe);
 	if (rc != EOK) {
 		return rc;
Index: uspace/lib/usbdev/src/recognise.c
===================================================================
--- uspace/lib/usbdev/src/recognise.c	(revision 1cf26abf038d0db4ca5f4e0a43ca1b41d655b01e)
+++ uspace/lib/usbdev/src/recognise.c	(revision b4b534ac1d1515633baff0d563e9c16938bfe19f)
@@ -34,21 +34,13 @@
  */
 
-#include <sys/types.h>
-#include <fibril_synch.h>
-#include <usb/debug.h>
-#include <usb/dev/hub.h>
 #include <usb/dev/pipes.h>
 #include <usb/dev/recognise.h>
-#include <usb/ddfiface.h>
 #include <usb/dev/request.h>
 #include <usb/classes/classes.h>
+
+#include <assert.h>
+#include <errno.h>
 #include <stdio.h>
-#include <errno.h>
-#include <assert.h>
-
-/** DDF operations of child devices. */
-static ddf_dev_ops_t child_ops = {
-	.interfaces[USB_DEV_IFACE] = &usb_iface_hub_child_impl
-};
+#include <sys/types.h>
 
 /** Get integer part from BCD coded number. */
@@ -242,5 +234,5 @@
 		    (int) device_descriptor->product_id,
 		    BCD_ARGS(device_descriptor->device_version));
-		
+
 		/* Next, without release number. */
 		ADD_MATCHID_OR_RETURN(matches, 90,
@@ -248,17 +240,10 @@
 		    (int) device_descriptor->vendor_id,
 		    (int) device_descriptor->product_id);
-	}	
-
-	/*
-	 * If the device class points to interface we skip adding
-	 * class directly but we add a multi interface device.
-	 */
-	if (device_descriptor->device_class != USB_CLASS_USE_INTERFACE) {
-		ADD_MATCHID_OR_RETURN(matches, 50, "usb&class=%s",
-		    usb_str_class(device_descriptor->device_class));
-	} else {
-		ADD_MATCHID_OR_RETURN(matches, 50, "usb&mid");
-	}
-	
+	}
+
+	/* Class match id */
+	ADD_MATCHID_OR_RETURN(matches, 50, "usb&class=%s",
+	    usb_str_class(device_descriptor->device_class));
+
 	/* As a last resort, try fallback driver. */
 	ADD_MATCHID_OR_RETURN(matches, 10, "usb&fallback");
@@ -302,96 +287,4 @@
 }
 
-/** Probe for device kind and register it in devman.
- *
- * @param[in] ctrl_pipe Control pipe to the device.
- * @param[in] parent Parent device.
- * @param[in] dev_ops Child device ops. Default child_ops will be used if NULL.
- * @param[in] dev_data Arbitrary pointer to be stored in the child
- *	as @c driver_data.
- * @param[out] child_fun Storage where pointer to allocated child function
- *	will be written.
- * @return Error code.
- *
- */
-int usb_device_register_child_in_devman(usb_pipe_t *ctrl_pipe,
-    ddf_dev_t *parent, ddf_fun_t *fun, ddf_dev_ops_t *dev_ops)
-{
-	if (ctrl_pipe == NULL)
-		return EINVAL;
-	
-	if (!dev_ops && ddf_fun_data_get(fun) != NULL) {
-		usb_log_warning("Using standard fun ops with arbitrary "
-		    "driver data. This does not have to work.\n");
-	}
-	
-	/** Index to append after device name for uniqueness. */
-	static atomic_t device_name_index = {0};
-	const size_t this_device_name_index =
-	    (size_t) atomic_preinc(&device_name_index);
-	
-	int rc;
-	
-	/*
-	 * TODO: Once the device driver framework support persistent
-	 * naming etc., something more descriptive could be created.
-	 */
-	char child_name[12];  /* The format is: "usbAB_aXYZ", length 11 */
-	rc = snprintf(child_name, sizeof(child_name),
-	    "usb%02zu_a%d", this_device_name_index, ctrl_pipe->wire->address);
-	if (rc < 0) {
-		goto failure;
-	}
-	
-	rc = ddf_fun_set_name(fun, child_name);
-	if (rc != EOK)
-		goto failure;
-	
-	if (dev_ops != NULL)
-		ddf_fun_set_ops(fun, dev_ops);
-	else
-		ddf_fun_set_ops(fun, &child_ops);
-	
-	/*
-	 * Store the attached device in fun
-	 * driver data if there is no other data
-	 */
-	if (ddf_fun_data_get(fun) == NULL) {
-		usb_hub_attached_device_t *new_device = ddf_fun_data_alloc(
-		    fun, sizeof(usb_hub_attached_device_t));
-		if (!new_device) {
-			rc = ENOMEM;
-			goto failure;
-		}
-		
-		new_device->address = ctrl_pipe->wire->address;
-		new_device->fun = fun;
-	}
-	
-	match_id_list_t match_ids;
-	init_match_ids(&match_ids);
-	rc = usb_device_create_match_ids(ctrl_pipe, &match_ids);
-	if (rc != EOK)
-		goto failure;
-	
-	list_foreach(match_ids.ids, link, match_id_t, match_id) {
-		rc = ddf_fun_add_match_id(fun, match_id->id, match_id->score);
-		if (rc != EOK) {
-			clean_match_ids(&match_ids);
-			goto failure;
-		}
-	}
-	
-	clean_match_ids(&match_ids);
-	
-	rc = ddf_fun_bind(fun);
-	if (rc != EOK)
-		goto failure;
-	
-	return EOK;
-	
-failure:
-	return rc;
-}
-
 /**
  * @}
Index: uspace/lib/usbdev/src/request.c
===================================================================
--- uspace/lib/usbdev/src/request.c	(revision 1cf26abf038d0db4ca5f4e0a43ca1b41d655b01e)
+++ uspace/lib/usbdev/src/request.c	(revision b4b534ac1d1515633baff0d563e9c16938bfe19f)
@@ -34,7 +34,11 @@
  */
 #include <usb/dev/request.h>
+#include <usb/request.h>
+#include <usb/usb.h>
+
 #include <errno.h>
-#include <assert.h>
-#include <usb/debug.h>
+#include <mem.h>
+#include <stdlib.h>
+#include <str.h>
 
 #define MAX_DATA_LENGTH ((size_t)(0xFFFF))
@@ -51,9 +55,9 @@
  * @param request Actual request (e.g. GET_DESCRIPTOR).
  * @param value Value of @c wValue field of setup packet
- * 	(must be in USB endianness).
+ *	(must be in USB endianness).
  * @param index Value of @c wIndex field of setup packet
- * 	(must be in USB endianness).
+ *	(must be in USB endianness).
  * @param data Data to be sent during DATA stage
- * 	(expected to be in USB endianness).
+ *	(expected to be in USB endianness).
  * @param data_size Size of the @p data buffer (in native endianness).
  * @return Error code.
@@ -64,7 +68,6 @@
 int usb_control_request_set(usb_pipe_t *pipe,
     usb_request_type_t request_type, usb_request_recipient_t recipient,
-    uint8_t request,
-    uint16_t value, uint16_t index,
-    void *data, size_t data_size)
+    uint8_t request, uint16_t value, uint16_t index,
+    const void *data, size_t data_size)
 {
 	if (pipe == NULL) {
@@ -85,16 +88,14 @@
 	 */
 
-	usb_device_request_setup_packet_t setup_packet;
-	setup_packet.request_type = (request_type << 5) | recipient;
-	setup_packet.request = request;
-	setup_packet.value = value;
-	setup_packet.index = index;
-	setup_packet.length = (uint16_t) data_size;
-
-	int rc = usb_pipe_control_write(pipe,
-	    &setup_packet, sizeof(setup_packet),
-	    data, data_size);
-
-	return rc;
+	const usb_device_request_setup_packet_t setup_packet = {
+		.request_type = (request_type << 5) | recipient,
+		.request = request,
+		.value = value,
+		.index = index,
+		.length = (uint16_t) data_size,
+	};
+
+	return usb_pipe_control_write(pipe,
+	    &setup_packet, sizeof(setup_packet), data, data_size);
 }
 
@@ -108,5 +109,5 @@
   * @param request Actual request (e.g. GET_DESCRIPTOR).
   * @param value Value of @c wValue field of setup packet
-  * 	(must be in USB endianness).
+  *	(must be in USB endianness).
   * @param index Value of @c wIndex field of setup packet
   *	(must be in USB endianness).
@@ -114,7 +115,7 @@
   *	(they will come in USB endianness).
   * @param data_size Size of the @p data buffer
-  * 	(in native endianness).
+  *	(in native endianness).
   * @param actual_data_size Actual size of transfered data
-  *        (in native endianness).
+  *	(in native endianness).
   * @return Error code.
   * @retval EBADMEM @p pipe is NULL.
@@ -124,6 +125,5 @@
 int usb_control_request_get(usb_pipe_t *pipe,
     usb_request_type_t request_type, usb_request_recipient_t recipient,
-    uint8_t request,
-    uint16_t value, uint16_t index,
+    uint8_t request, uint16_t value, uint16_t index,
     void *data, size_t data_size, size_t *actual_data_size)
 {
@@ -209,16 +209,13 @@
 {
 	if (request_type == USB_REQUEST_TYPE_STANDARD) {
-		if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
-		    && (index != 0)) {
+		if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))
+		{
 			return EINVAL;
 		}
 	}
 
-	int rc = usb_control_request_set(pipe, request_type, recipient,
-	    USB_DEVREQ_CLEAR_FEATURE,
-	    uint16_host2usb(feature_selector), uint16_host2usb(index),
-	    NULL, 0);
-
-	return rc;
+	return usb_control_request_set(pipe,
+	    request_type, recipient, USB_DEVREQ_CLEAR_FEATURE,
+	    uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
 }
 
@@ -237,16 +234,13 @@
 {
 	if (request_type == USB_REQUEST_TYPE_STANDARD) {
-		if ((recipient == USB_REQUEST_RECIPIENT_DEVICE)
-		    && (index != 0)) {
+		if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))
+		{
 			return EINVAL;
 		}
 	}
 
-	int rc = usb_control_request_set(pipe, request_type, recipient,
-	    USB_DEVREQ_SET_FEATURE,
-	    uint16_host2usb(feature_selector), uint16_host2usb(index),
-	    NULL, 0);
-
-	return rc;
+	return usb_control_request_set(pipe,
+	    request_type, recipient, USB_DEVREQ_SET_FEATURE,
+	    uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
 }
 
@@ -277,4 +271,7 @@
 	}
 
+	/* The wValue field specifies the descriptor type in the high byte
+	 * and the descriptor index in the low byte. USB 1.1 spec p. 189
+	 */
 	const uint16_t wValue = descriptor_index | (descriptor_type << 8);
 
@@ -313,21 +310,19 @@
 	 * Get only first byte to retrieve descriptor length.
 	 */
-	uint8_t tmp_buffer[1];
+	uint8_t tmp_buffer;
 	size_t bytes_transfered;
 	rc = usb_request_get_descriptor(pipe, request_type, recipient,
 	    descriptor_type, descriptor_index, language,
-	    &tmp_buffer, 1, &bytes_transfered);
+	    &tmp_buffer, sizeof(tmp_buffer), &bytes_transfered);
 	if (rc != EOK) {
 		return rc;
 	}
 	if (bytes_transfered != 1) {
-		/* FIXME: some better error code? */
-		return ESTALL;
-	}
-
-	size_t size = tmp_buffer[0];
+		return ELIMIT;
+	}
+
+	const size_t size = tmp_buffer;
 	if (size == 0) {
-		/* FIXME: some better error code? */
-		return ESTALL;
+		return ELIMIT;
 	}
 
@@ -349,6 +344,5 @@
 	if (bytes_transfered != size) {
 		free(buffer);
-		/* FIXME: some better error code? */
-		return ESTALL;
+		return ELIMIT;
 	}
 
@@ -378,6 +372,5 @@
 	int rc = usb_request_get_descriptor(pipe,
 	    USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
-	    USB_DESCTYPE_DEVICE, 0, 0,
-	    &descriptor_tmp, sizeof(descriptor_tmp),
+	    USB_DESCTYPE_DEVICE, 0, 0, &descriptor_tmp, sizeof(descriptor_tmp),
 	    &actually_transferred);
 
@@ -421,5 +414,5 @@
 	size_t actually_transferred = 0;
 	usb_standard_configuration_descriptor_t descriptor_tmp;
-	int rc = usb_request_get_descriptor(pipe,
+	const int rc = usb_request_get_descriptor(pipe,
 	    USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
 	    USB_DESCTYPE_CONFIGURATION, index, 0,
@@ -479,5 +472,5 @@
 int usb_request_get_full_configuration_descriptor_alloc(
     usb_pipe_t *pipe, int index,
-    void **descriptor_ptr, size_t *descriptor_size)
+    const void **descriptor_ptr, size_t *descriptor_size)
 {
 	int rc;
@@ -546,6 +539,5 @@
     usb_request_type_t request_type, usb_request_recipient_t recipient,
     uint8_t descriptor_type, uint8_t descriptor_index,
-    uint16_t language,
-    void *buffer, size_t size)
+    uint16_t language, const void *buffer, size_t size)
 {
 	if (buffer == NULL) {
@@ -560,8 +552,6 @@
 
 	return usb_control_request_set(pipe,
-	    request_type, recipient,
-	    USB_DEVREQ_SET_DESCRIPTOR,
-	    wValue, language,
-	    buffer, size);
+	    request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR,
+	    wValue, language, buffer, size);
 }
 
@@ -578,9 +568,7 @@
 	size_t actual_size;
 
-	int rc = usb_control_request_get(pipe,
+	const int rc = usb_control_request_get(pipe,
 	    USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
-	    USB_DEVREQ_GET_CONFIGURATION,
-	    0, 0,
-	    &value, 1, &actual_size);
+	    USB_DEVREQ_GET_CONFIGURATION, 0, 0, &value, 1, &actual_size);
 
 	if (rc != EOK) {
@@ -607,5 +595,5 @@
     uint8_t configuration_value)
 {
-	uint16_t config_value
+	const uint16_t config_value
 	    = uint16_host2usb((uint16_t) configuration_value);
 
@@ -629,9 +617,9 @@
 	size_t actual_size;
 
-	int rc = usb_control_request_get(pipe,
+	const int rc = usb_control_request_get(pipe,
 	    USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
 	    USB_DEVREQ_GET_INTERFACE,
 	    0, uint16_host2usb((uint16_t) interface_index),
-	    &value, 1, &actual_size);
+	    &value, sizeof(value), &actual_size);
 
 	if (rc != EOK) {
@@ -678,10 +666,5 @@
     l18_win_locales_t **languages_ptr, size_t *languages_count)
 {
-	int rc;
-
-	if (languages_ptr == NULL) {
-		return EBADMEM;
-	}
-	if (languages_count == NULL) {
+	if (languages_ptr == NULL || languages_count == NULL) {
 		return EBADMEM;
 	}
@@ -689,5 +672,5 @@
 	uint8_t *string_descriptor = NULL;
 	size_t string_descriptor_size = 0;
-	rc = usb_request_get_descriptor_alloc(pipe,
+	const int rc = usb_request_get_descriptor_alloc(pipe,
 	    USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
 	    USB_DESCTYPE_STRING, 0, 0,
@@ -710,7 +693,7 @@
 	}
 
-	size_t langs_count = string_descriptor_size / 2;
-	l18_win_locales_t *langs
-	    = malloc(sizeof(l18_win_locales_t) * langs_count);
+	const size_t langs_count = string_descriptor_size / 2;
+	l18_win_locales_t *langs =
+	    calloc(langs_count, sizeof(l18_win_locales_t));
 	if (langs == NULL) {
 		free(string_descriptor);
@@ -718,9 +701,9 @@
 	}
 
-	size_t i;
-	for (i = 0; i < langs_count; i++) {
+	for (size_t i = 0; i < langs_count; i++) {
 		/* Language code from the descriptor is in USB endianness. */
 		/* FIXME: is this really correct? */
-		uint16_t lang_code = (string_descriptor[2 + 2 * i + 1] << 8)
+		const uint16_t lang_code =
+		    (string_descriptor[2 + 2 * i + 1] << 8)
 		    + string_descriptor[2 + 2 * i];
 		langs[i] = uint16_usb2host(lang_code);
@@ -761,5 +744,5 @@
 	}
 	/* Language is actually two byte value. */
-	if (lang > 0xFFFF) {
+	if (lang > L18N_WIN_LOCALE_MAX) {
 		return ERANGE;
 	}
@@ -795,5 +778,5 @@
 	}
 
-	size_t string_char_count = string_size / 2;
+	const size_t string_char_count = string_size / 2;
 	string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
 	if (string_chars == NULL) {
@@ -807,7 +790,6 @@
 	 * do not have them).
 	 */
-	size_t i;
-	for (i = 0; i < string_char_count; i++) {
-		uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
+	for (size_t i = 0; i < string_char_count; i++) {
+		const uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
 		    + string[2 + 2 * i];
 		string_chars[i] = uni_char;
@@ -827,10 +809,6 @@
 
 leave:
-	if (string != NULL) {
-		free(string);
-	}
-	if (string_chars != NULL) {
-		free(string_chars);
-	}
+	free(string);
+	free(string_chars);
 
 	return rc;
@@ -847,5 +825,5 @@
 	return usb_request_clear_feature(pipe,
 	    USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT,
-	    uint16_host2usb(USB_FEATURE_SELECTOR_ENDPOINT_HALT),
+	    uint16_host2usb(USB_FEATURE_ENDPOINT_HALT),
 	    uint16_host2usb(ep_index));
 }
