Index: uspace/drv/bus/usb/xhci/bus.c
===================================================================
--- uspace/drv/bus/usb/xhci/bus.c	(revision 10cd715240ae1db3090d2fb49f7185790adc313a)
+++ uspace/drv/bus/usb/xhci/bus.c	(revision a8723748f11aced50ec8c8b3ca56ba447f743ba0)
@@ -58,50 +58,17 @@
 };
 
-static int prepare_endpoint(xhci_endpoint_t *ep, const usb_endpoint_desc_t *desc)
-{
-	/* Extract information from endpoint_desc */
-	ep->base.endpoint = desc->endpoint_no;
-	ep->base.direction = desc->direction;
-	ep->base.transfer_type = desc->transfer_type;
-	ep->base.max_packet_size = desc->max_packet_size;
-	ep->base.packets = desc->packets;
-	ep->max_streams = desc->usb3.max_streams;
-	ep->max_burst = desc->usb3.max_burst;
-	ep->mult = desc->usb3.mult;
-
-	if (ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
-		if (ep->base.device->speed <= USB_SPEED_HIGH) {
-			ep->isoch_max_size = desc->max_packet_size * (desc->packets + 1);
-		}
-		else if (ep->base.device->speed == USB_SPEED_SUPER) {
-			ep->isoch_max_size = desc->usb3.bytes_per_interval;
-		}
-		/* Technically there could be superspeed plus too. */
-
-		/* Allocate and setup isochronous-specific structures. */
-		ep->isoch_enqueue = 0;
-		ep->isoch_dequeue = XHCI_ISOCH_BUFFER_COUNT - 1;
-		ep->isoch_started = false;
-
-		fibril_mutex_initialize(&ep->isoch_guard);
-		fibril_condvar_initialize(&ep->isoch_avail);
-	}
-
-	return xhci_endpoint_alloc_transfer_ds(ep);
-}
-
-static endpoint_t *create_endpoint(bus_t *base);
-
-static int address_device(xhci_hc_t *hc, xhci_device_t *dev)
+static endpoint_t *endpoint_create(device_t *, const usb_endpoint_desc_t *);
+
+static int address_device(xhci_bus_t *bus, xhci_device_t *dev)
 {
 	int err;
 
 	/* Enable new slot. */
-	if ((err = hc_enable_slot(hc, &dev->slot_id)) != EOK)
+	if ((err = hc_enable_slot(bus->hc, &dev->slot_id)) != EOK)
 		return err;
 	usb_log_debug2("Obtained slot ID: %u.\n", dev->slot_id);
 
 	/* Create and configure control endpoint. */
-	endpoint_t *ep0_base = create_endpoint(&hc->bus.base);
+	endpoint_t *ep0_base = endpoint_create(&dev->base, &ep0_initial_desc);
 	if (!ep0_base)
 		goto err_slot;
@@ -112,5 +79,5 @@
 	xhci_endpoint_t *ep0 = xhci_endpoint_get(ep0_base);
 
-	if ((err = prepare_endpoint(ep0, &ep0_initial_desc)))
+	if ((err = xhci_endpoint_alloc_transfer_ds(ep0)))
 		goto err_ep;
 
@@ -120,5 +87,5 @@
 
 	/* Address device */
-	if ((err = hc_address_device(hc, dev, ep0)))
+	if ((err = hc_address_device(bus->hc, dev, ep0)))
 		goto err_added;
 
@@ -135,5 +102,5 @@
 	endpoint_del_ref(ep0_base);
 err_slot:
-	hc_disable_slot(hc, dev);
+	hc_disable_slot(bus->hc, dev);
 	return err;
 }
@@ -164,5 +131,5 @@
 }
 
-int xhci_bus_enumerate_device(xhci_bus_t *bus, xhci_hc_t *hc, device_t *dev)
+int xhci_bus_enumerate_device(xhci_bus_t *bus, device_t *dev)
 {
 	int err;
@@ -184,5 +151,5 @@
 
 	/* Assign an address to the device */
-	if ((err = address_device(hc, xhci_dev))) {
+	if ((err = address_device(bus, xhci_dev))) {
 		usb_log_error("Failed to setup address of the new device: %s", str_error(err));
 		return err;
@@ -195,5 +162,5 @@
 	fibril_mutex_unlock(&bus->base.guard);
 
-	if ((err = setup_ep0_packet_size(hc, xhci_dev))) {
+	if ((err = setup_ep0_packet_size(bus->hc, xhci_dev))) {
 		usb_log_error("Failed to setup control endpoint of the new device: %s", str_error(err));
 		goto err_address;
@@ -201,5 +168,5 @@
 
 	/* Read the device descriptor, derive the match ids */
-	if ((err = hcd_ddf_device_explore(hc->hcd, dev))) {
+	if ((err = hcd_ddf_device_explore(dev))) {
 		usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
 		goto err_address;
@@ -213,7 +180,7 @@
 }
 
-static int unregister_endpoint(bus_t *, endpoint_t *);
-
-int xhci_bus_remove_device(xhci_bus_t *bus, xhci_hc_t *hc, device_t *dev)
+static int endpoint_unregister(endpoint_t *);
+
+int xhci_bus_remove_device(xhci_bus_t *bus, device_t *dev)
 {
 	int err;
@@ -246,5 +213,5 @@
 	/* Disable the slot, dropping all endpoints. */
 	const uint32_t slot_id = xhci_dev->slot_id;
-	if ((err = hc_disable_slot(hc, xhci_dev))) {
+	if ((err = hc_disable_slot(bus->hc, xhci_dev))) {
 		usb_log_warning("Failed to disable slot of device " XHCI_DEV_FMT ": %s",
 		    XHCI_DEV_ARGS(*xhci_dev), str_error(err));
@@ -258,5 +225,5 @@
 			continue;
 
-		if ((err = unregister_endpoint(&bus->base, &xhci_dev->endpoints[i]->base))) {
+		if ((err = endpoint_unregister(&xhci_dev->endpoints[i]->base))) {
 			usb_log_warning("Failed to unregister endpoint " XHCI_EP_FMT ": %s",
 			    XHCI_EP_ARGS(*xhci_dev->endpoints[i]), str_error(err));
@@ -278,34 +245,21 @@
 }
 
-static int enumerate_device(bus_t *bus_base, hcd_t *hcd, device_t *dev)
-{
-	xhci_hc_t *hc = hcd_get_driver_data(hcd);
-	assert(hc);
-
-	xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
-	assert(bus);
-
-	return xhci_bus_enumerate_device(bus, hc, dev);
-}
-
-static int remove_device(bus_t *bus_base, hcd_t *hcd, device_t *dev)
-{
-	xhci_hc_t *hc = hcd_get_driver_data(hcd);
-	assert(hc);
-
-	xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
-	assert(bus);
-
-	return xhci_bus_remove_device(bus, hc, dev);
-}
-
-static int online_device(bus_t *bus_base, hcd_t *hcd, device_t *dev_base)
-{
-	int err;
-
-	xhci_hc_t *hc = hcd_get_driver_data(hcd);
-	assert(hc);
-
-	xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
+static int device_enumerate(device_t *dev)
+{
+	xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
+	return xhci_bus_enumerate_device(bus, dev);
+}
+
+static int device_remove(device_t *dev)
+{
+	xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
+	return xhci_bus_remove_device(bus, dev);
+}
+
+static int device_online(device_t *dev_base)
+{
+	int err;
+
+	xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
 	assert(bus);
 
@@ -314,5 +268,5 @@
 
 	/* Transition the device from the Addressed to the Configured state. */
-	if ((err = hc_configure_device(hc, dev->slot_id))) {
+	if ((err = hc_configure_device(bus->hc, dev->slot_id))) {
 		usb_log_warning("Failed to configure device " XHCI_DEV_FMT ".", XHCI_DEV_ARGS(*dev));
 	}
@@ -331,12 +285,9 @@
 }
 
-static int offline_device(bus_t *bus_base, hcd_t *hcd, device_t *dev_base)
-{
-	int err;
-
-	xhci_hc_t *hc = hcd_get_driver_data(hcd);
-	assert(hc);
-
-	xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
+static int device_offline(device_t *dev_base)
+{
+	int err;
+
+	xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
 	assert(bus);
 
@@ -370,5 +321,5 @@
 
 	/* Issue one HC command to simultaneously drop all endpoints except zero. */
-	if ((err = hc_deconfigure_device(hc, dev->slot_id))) {
+	if ((err = hc_deconfigure_device(bus->hc, dev->slot_id))) {
 		usb_log_warning("Failed to deconfigure device " XHCI_DEV_FMT ".",
 		    XHCI_DEV_ARGS(*dev));
@@ -388,13 +339,11 @@
 }
 
-static endpoint_t *create_endpoint(bus_t *base)
-{
-	xhci_bus_t *bus = bus_to_xhci_bus(base);
-
+static endpoint_t *endpoint_create(device_t *dev, const usb_endpoint_desc_t *desc)
+{
 	xhci_endpoint_t *ep = calloc(1, sizeof(xhci_endpoint_t));
 	if (!ep)
 		return NULL;
 
-	if (xhci_endpoint_init(ep, bus)) {
+	if (xhci_endpoint_init(ep, dev, desc)) {
 		free(ep);
 		return NULL;
@@ -404,5 +353,5 @@
 }
 
-static void destroy_endpoint(endpoint_t *ep)
+static void endpoint_destroy(endpoint_t *ep)
 {
 	xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
@@ -412,13 +361,13 @@
 }
 
-static int register_endpoint(bus_t *bus_base, device_t *device, endpoint_t *ep_base, const usb_endpoint_desc_t *desc)
-{
-	int err;
-	xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
+static int endpoint_register(endpoint_t *ep_base)
+{
+	int err;
+	xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep_base));
 	xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
 
-	xhci_device_t *dev = xhci_device_get(device);
-
-	if ((err = prepare_endpoint(ep, desc)))
+	xhci_device_t *dev = xhci_device_get(ep_base->device);
+
+	if ((err = xhci_endpoint_alloc_transfer_ds(ep)))
 		return err;
 
@@ -443,8 +392,8 @@
 }
 
-static int unregister_endpoint(bus_t *bus_base, endpoint_t *ep_base)
-{
-	int err;
-	xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
+static int endpoint_unregister(endpoint_t *ep_base)
+{
+	int err;
+	xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep_base));
 	xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
 	xhci_device_t *dev = xhci_device_get(ep_base->device);
@@ -470,5 +419,5 @@
 }
 
-static endpoint_t* find_endpoint(bus_t *bus_base, device_t *dev_base, usb_target_t target, usb_direction_t direction)
+static endpoint_t* device_find_endpoint(device_t *dev_base, usb_target_t target, usb_direction_t direction)
 {
 	xhci_device_t *dev = xhci_device_get(dev_base);
@@ -487,10 +436,4 @@
 }
 
-static size_t count_bw(endpoint_t *ep, size_t size)
-{
-	// TODO: Implement me!
-	return 0;
-}
-
 /* Endpoint ops, optional (have generic fallback) */
 static bool endpoint_get_toggle(endpoint_t *ep)
@@ -525,5 +468,5 @@
 }
 
-static usb_transfer_batch_t *create_batch(bus_t *bus, endpoint_t *ep)
+static usb_transfer_batch_t *batch_create(endpoint_t *ep)
 {
 	xhci_transfer_t *transfer = xhci_transfer_create(ep);
@@ -531,5 +474,5 @@
 }
 
-static void destroy_batch(usb_transfer_batch_t *batch)
+static void batch_destroy(usb_transfer_batch_t *batch)
 {
 	xhci_transfer_destroy(xhci_transfer_from_batch(batch));
@@ -538,28 +481,23 @@
 static const bus_ops_t xhci_bus_ops = {
 #define BIND_OP(op) .op = op,
-	BIND_OP(enumerate_device)
-	BIND_OP(remove_device)
-
-	BIND_OP(online_device)
-	BIND_OP(offline_device)
-
-	BIND_OP(create_endpoint)
-	BIND_OP(destroy_endpoint)
-
-	BIND_OP(register_endpoint)
-	BIND_OP(unregister_endpoint)
-	BIND_OP(find_endpoint)
-
 	BIND_OP(reserve_default_address)
 	BIND_OP(release_default_address)
-
 	BIND_OP(reset_toggle)
-	BIND_OP(count_bw)
-
+
+	BIND_OP(device_enumerate)
+	BIND_OP(device_remove)
+	BIND_OP(device_online)
+	BIND_OP(device_offline)
+	BIND_OP(device_find_endpoint)
+
+	BIND_OP(endpoint_create)
+	BIND_OP(endpoint_destroy)
+	BIND_OP(endpoint_register)
+	BIND_OP(endpoint_unregister)
 	BIND_OP(endpoint_get_toggle)
 	BIND_OP(endpoint_set_toggle)
 
-	BIND_OP(create_batch)
-	BIND_OP(destroy_batch)
+	BIND_OP(batch_create)
+	BIND_OP(batch_destroy)
 #undef BIND_OP
 };
@@ -569,5 +507,5 @@
 	assert(bus);
 
-	bus_init(&bus->base, sizeof(xhci_device_t));
+	bus_init(&bus->base, hc->hcd, sizeof(xhci_device_t));
 
 	bus->devices_by_slot = calloc(hc->max_slots, sizeof(xhci_device_t *));
@@ -576,5 +514,5 @@
 
 	bus->hc = hc;
-	bus->base.ops = xhci_bus_ops;
+	bus->base.ops = &xhci_bus_ops;
 	bus->default_address_speed = USB_SPEED_MAX;
 	return EOK;
