Index: uspace/drv/ohci/root_hub.c
===================================================================
--- uspace/drv/ohci/root_hub.c	(revision cd1cec3bc8deaa31b192d8430abb26ebf75ce6f3)
+++ uspace/drv/ohci/root_hub.c	(revision 8b74997f71d062c07d27d7b964e1b778df3c993b)
@@ -47,20 +47,19 @@
  *	standart device descriptor for ohci root hub
  */
-static const usb_standard_device_descriptor_t ohci_rh_device_descriptor =
-{
-		.configuration_count = 1,
-		.descriptor_type = USB_DESCTYPE_DEVICE,
-		.device_class = USB_CLASS_HUB,
-		.device_protocol = 0,
-		.device_subclass = 0,
-		.device_version = 0,
-		.length = sizeof(usb_standard_device_descriptor_t),
-		/// \TODO this value is guessed
-		.max_packet_size = 8,
-		.vendor_id = 0x16db,
-		.product_id = 0x0001,
-		/// \TODO these values migt be different
-		.str_serial_number = 0,
-		.usb_spec_version = 0x110,
+static const usb_standard_device_descriptor_t ohci_rh_device_descriptor = {
+	.configuration_count = 1,
+	.descriptor_type = USB_DESCTYPE_DEVICE,
+	.device_class = USB_CLASS_HUB,
+	.device_protocol = 0,
+	.device_subclass = 0,
+	.device_version = 0,
+	.length = sizeof (usb_standard_device_descriptor_t),
+	/// \TODO this value is guessed
+	.max_packet_size = 8,
+	.vendor_id = 0x16db,
+	.product_id = 0x0001,
+	/// \TODO these values migt be different
+	.str_serial_number = 0,
+	.usb_spec_version = 0x110,
 };
 
@@ -69,12 +68,11 @@
  * for ohci root hubs
  */
-static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor =
-{
+static const usb_standard_configuration_descriptor_t ohci_rh_conf_descriptor = {
 	/// \TODO some values are default or guessed
-	.attributes = 1<<7,
+	.attributes = 1 << 7,
 	.configuration_number = 1,
 	.descriptor_type = USB_DESCTYPE_CONFIGURATION,
 	.interface_count = 1,
-	.length = sizeof(usb_standard_configuration_descriptor_t),
+	.length = sizeof (usb_standard_configuration_descriptor_t),
 	.max_power = 100,
 	.str_configuration = 0,
@@ -84,6 +82,5 @@
  * standart ohci root hub interface descriptor
  */
-static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor =
-{
+static const usb_standard_interface_descriptor_t ohci_rh_iface_descriptor = {
 	.alternate_setting = 0,
 	.descriptor_type = USB_DESCTYPE_INTERFACE,
@@ -94,5 +91,5 @@
 	.interface_protocol = 0,
 	.interface_subclass = 0,
-	.length = sizeof(usb_standard_interface_descriptor_t),
+	.length = sizeof (usb_standard_interface_descriptor_t),
 	.str_interface = 0,
 };
@@ -101,10 +98,9 @@
  * standart ohci root hub endpoint descriptor
  */
-static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor =
-{
+static const usb_standard_endpoint_descriptor_t ohci_rh_ep_descriptor = {
 	.attributes = USB_TRANSFER_INTERRUPT,
 	.descriptor_type = USB_DESCTYPE_ENDPOINT,
-	.endpoint_address = 1 + (1<<7),
-	.length = sizeof(usb_standard_endpoint_descriptor_t),
+	.endpoint_address = 1 + (1 << 7),
+	.length = sizeof (usb_standard_endpoint_descriptor_t),
 	.max_packet_size = 8,
 	.poll_interval = 255,
@@ -112,6 +108,6 @@
 
 static const uint32_t hub_clear_feature_valid_mask =
-	(1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER) +
-	(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
+	(1 << USB_HUB_FEATURE_C_HUB_LOCAL_POWER) |
+(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
 
 static const uint32_t hub_clear_feature_by_writing_one_mask =
@@ -121,28 +117,149 @@
 	(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
 
-	
+
 static const uint32_t hub_set_feature_direct_mask =
 	(1 << USB_HUB_FEATURE_C_HUB_OVER_CURRENT);
 
 static const uint32_t port_set_feature_valid_mask =
-	(1 << USB_HUB_FEATURE_PORT_ENABLE) +
-	(1 << USB_HUB_FEATURE_PORT_SUSPEND) +
-	(1 << USB_HUB_FEATURE_PORT_RESET) +
-	(1 << USB_HUB_FEATURE_PORT_POWER);
+	(1 << USB_HUB_FEATURE_PORT_ENABLE) |
+(1 << USB_HUB_FEATURE_PORT_SUSPEND) |
+(1 << USB_HUB_FEATURE_PORT_RESET) |
+(1 << USB_HUB_FEATURE_PORT_POWER);
 
 static const uint32_t port_clear_feature_valid_mask =
-	(1 << USB_HUB_FEATURE_PORT_CONNECTION) +
-	(1 << USB_HUB_FEATURE_PORT_SUSPEND) +
-	(1 << USB_HUB_FEATURE_PORT_OVER_CURRENT) +
-	(1 << USB_HUB_FEATURE_PORT_POWER) +
-	(1 << USB_HUB_FEATURE_C_PORT_CONNECTION) +
-	(1 << USB_HUB_FEATURE_C_PORT_ENABLE) +
-	(1 << USB_HUB_FEATURE_C_PORT_SUSPEND) +
-	(1 << USB_HUB_FEATURE_C_PORT_OVER_CURRENT) +
-	(1 << USB_HUB_FEATURE_C_PORT_RESET);
-//note that USB_HUB_FEATURE_PORT_POWER bit is translated into USB_HUB_FEATURE_PORT_LOW_SPEED
-
-
-
+	(1 << USB_HUB_FEATURE_PORT_CONNECTION) |
+(1 << USB_HUB_FEATURE_PORT_SUSPEND) |
+(1 << USB_HUB_FEATURE_PORT_OVER_CURRENT) |
+(1 << USB_HUB_FEATURE_PORT_POWER) |
+(1 << USB_HUB_FEATURE_C_PORT_CONNECTION) |
+(1 << USB_HUB_FEATURE_C_PORT_ENABLE) |
+(1 << USB_HUB_FEATURE_C_PORT_SUSPEND) |
+(1 << USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
+(1 << USB_HUB_FEATURE_C_PORT_RESET);
+//note that USB_HUB_FEATURE_PORT_POWER bit is translated into
+//USB_HUB_FEATURE_PORT_LOW_SPEED
+
+static const uint32_t port_status_change_mask =
+(1<< USB_HUB_FEATURE_C_PORT_CONNECTION) |
+(1<< USB_HUB_FEATURE_C_PORT_ENABLE) |
+(1<< USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
+(1<< USB_HUB_FEATURE_C_PORT_RESET) |
+(1<< USB_HUB_FEATURE_C_PORT_SUSPEND);
+
+
+static void usb_create_serialized_hub_descriptor(rh_t *instance,
+	uint8_t ** out_result,
+	size_t * out_size);
+
+static void rh_init_descriptors(rh_t *instance);
+
+static int process_get_port_status_request(rh_t *instance, uint16_t port,
+	usb_transfer_batch_t * request);
+
+static int process_get_hub_status_request(rh_t *instance,
+	usb_transfer_batch_t * request);
+
+static int process_get_status_request(rh_t *instance,
+	usb_transfer_batch_t * request);
+
+static void create_interrupt_mask(rh_t *instance, void ** buffer,
+	size_t * buffer_size);
+
+static int process_get_descriptor_request(rh_t *instance,
+	usb_transfer_batch_t *request);
+
+static int process_get_configuration_request(rh_t *instance,
+	usb_transfer_batch_t *request);
+
+static int process_hub_feature_set_request(rh_t *instance, uint16_t feature);
+
+static int process_hub_feature_clear_request(rh_t *instance,
+	uint16_t feature);
+
+static int process_port_feature_set_request(rh_t *instance,
+	uint16_t feature, uint16_t port);
+
+static int process_port_feature_clear_request(rh_t *instance,
+	uint16_t feature, uint16_t port);
+
+static int process_address_set_request(rh_t *instance,
+	uint16_t address);
+
+static int process_request_with_output(rh_t *instance,
+	usb_transfer_batch_t *request);
+
+static int process_request_with_input(rh_t *instance,
+	usb_transfer_batch_t *request);
+
+static int process_request_without_data(rh_t *instance,
+	usb_transfer_batch_t *request);
+
+static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request);
+
+
+
+
+
+/** Root hub initialization
+ * @return Error code.
+ */
+int rh_init(rh_t *instance, ddf_dev_t *dev, ohci_regs_t *regs) {
+	assert(instance);
+	//instance->address = -1;
+	instance->registers = regs;
+	instance->device = dev;
+	instance->port_count = instance->registers->rh_desc_a & 0xff;
+	rh_init_descriptors(instance);
+	// set port power mode to no-power-switching
+	instance->registers->rh_desc_a =
+		instance->registers->rh_desc_a | (1<<9);
+
+	usb_log_info("OHCI root hub with %d ports.\n", instance->port_count);
+
+	//start generic usb hub driver
+
+	/* TODO: implement */
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
+
+/**
+ * process root hub request
+ *
+ * @param instance root hub instance
+ * @param request structure containing both request and response information
+ * @return error code
+ */
+int rh_request(rh_t *instance, usb_transfer_batch_t *request) {
+	assert(instance);
+	assert(request);
+	int opResult;
+	if (request->transfer_type == USB_TRANSFER_CONTROL) {
+		usb_log_info("Root hub got CONTROL packet\n");
+		opResult = process_ctrl_request(instance, request);
+	} else if (request->transfer_type == USB_TRANSFER_INTERRUPT) {
+		usb_log_info("Root hub got INTERRUPT packet\n");
+		void * buffer;
+		create_interrupt_mask(instance, &buffer,
+			&(request->transfered_size));
+		memcpy(request->transport_buffer, buffer,
+			request->transfered_size);
+		opResult = EOK;
+	} else {
+		opResult = EINVAL;
+	}
+	usb_transfer_batch_finish(request, opResult);
+	return EOK;
+}
+
+/*----------------------------------------------------------------------------*/
+
+
+void rh_interrupt(rh_t *instance) {
+	usb_log_info("Whoa whoa wait, I`m not supposed to receive any "
+		"interrupts, am I?\n");
+	/* TODO: implement? */
+}
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -157,14 +274,14 @@
  */
 static void usb_create_serialized_hub_descriptor(rh_t *instance,
-		uint8_t ** out_result,
-		size_t * out_size) {
+	uint8_t ** out_result,
+	size_t * out_size) {
 	//base size
 	size_t size = 7;
 	//variable size according to port count
 	size_t var_size = instance->port_count / 8 +
-			((instance->port_count % 8 > 0) ? 1 : 0);
+		((instance->port_count % 8 > 0) ? 1 : 0);
 	size += 2 * var_size;
 	uint8_t * result = (uint8_t*) malloc(size);
-	bzero(result,size);
+	bzero(result, size);
 	//size
 	result[0] = size;
@@ -174,9 +291,9 @@
 	uint32_t hub_desc_reg = instance->registers->rh_desc_a;
 	result[3] =
-			((hub_desc_reg >> 8) %2) +
-			(((hub_desc_reg >> 9) %2) << 1) +
-			(((hub_desc_reg >> 10) %2) << 2) +
-			(((hub_desc_reg >> 11) %2) << 3) +
-			(((hub_desc_reg >> 12) %2) << 4);
+		((hub_desc_reg >> 8) % 2) +
+		(((hub_desc_reg >> 9) % 2) << 1) +
+		(((hub_desc_reg >> 10) % 2) << 2) +
+		(((hub_desc_reg >> 11) % 2) << 3) +
+		(((hub_desc_reg >> 12) % 2) << 4);
 	result[4] = 0;
 	result[5] = /*descriptor->pwr_on_2_good_time*/ 50;
@@ -185,6 +302,8 @@
 	int port;
 	for (port = 1; port <= instance->port_count; ++port) {
-		result[7 + port/8] +=
-				((instance->registers->rh_desc_b >> port)%2) << (port%8);
+		uint8_t is_non_removable =
+			instance->registers->rh_desc_b >> port % 2;
+		result[7 + port / 8] +=
+			is_non_removable << (port % 8);
 	}
 	size_t i;
@@ -195,5 +314,5 @@
 	(*out_size) = size;
 }
-
+/*----------------------------------------------------------------------------*/
 
 /** initialize hub descriptors
@@ -203,60 +322,38 @@
  * @instance root hub instance
  */
-static void rh_init_descriptors(rh_t *instance){
+static void rh_init_descriptors(rh_t *instance) {
 	memcpy(&instance->descriptors.device, &ohci_rh_device_descriptor,
-		sizeof(ohci_rh_device_descriptor)
-	);
+		sizeof (ohci_rh_device_descriptor)
+		);
 	usb_standard_configuration_descriptor_t descriptor;
-	memcpy(&descriptor,&ohci_rh_conf_descriptor,
-			sizeof(ohci_rh_conf_descriptor));
+	memcpy(&descriptor, &ohci_rh_conf_descriptor,
+		sizeof (ohci_rh_conf_descriptor));
 	uint8_t * hub_descriptor;
 	size_t hub_desc_size;
 	usb_create_serialized_hub_descriptor(instance, &hub_descriptor,
-			&hub_desc_size);
+		&hub_desc_size);
 
 	descriptor.total_length =
-			sizeof(usb_standard_configuration_descriptor_t)+
-			sizeof(usb_standard_endpoint_descriptor_t)+
-			sizeof(usb_standard_interface_descriptor_t)+
-			hub_desc_size;
-	
+		sizeof (usb_standard_configuration_descriptor_t) +
+		sizeof (usb_standard_endpoint_descriptor_t) +
+		sizeof (usb_standard_interface_descriptor_t) +
+		hub_desc_size;
+
 	uint8_t * full_config_descriptor =
-			(uint8_t*) malloc(descriptor.total_length);
-	memcpy(full_config_descriptor, &descriptor, sizeof(descriptor));
-	memcpy(full_config_descriptor + sizeof(descriptor),
-			&ohci_rh_iface_descriptor, sizeof(ohci_rh_iface_descriptor));
-	memcpy(full_config_descriptor + sizeof(descriptor) +
-				sizeof(ohci_rh_iface_descriptor),
-			&ohci_rh_ep_descriptor, sizeof(ohci_rh_ep_descriptor));
-	memcpy(full_config_descriptor + sizeof(descriptor) +
-				sizeof(ohci_rh_iface_descriptor) +
-				sizeof(ohci_rh_ep_descriptor),
-			hub_descriptor, hub_desc_size);
-	
+		(uint8_t*) malloc(descriptor.total_length);
+	memcpy(full_config_descriptor, &descriptor, sizeof (descriptor));
+	memcpy(full_config_descriptor + sizeof (descriptor),
+		&ohci_rh_iface_descriptor, sizeof (ohci_rh_iface_descriptor));
+	memcpy(full_config_descriptor + sizeof (descriptor) +
+		sizeof (ohci_rh_iface_descriptor),
+		&ohci_rh_ep_descriptor, sizeof (ohci_rh_ep_descriptor));
+	memcpy(full_config_descriptor + sizeof (descriptor) +
+		sizeof (ohci_rh_iface_descriptor) +
+		sizeof (ohci_rh_ep_descriptor),
+		hub_descriptor, hub_desc_size);
+
 	instance->descriptors.configuration = full_config_descriptor;
 	instance->descriptors.configuration_size = descriptor.total_length;
 }
-
-/** Root hub initialization
- * @return Error code.
- */
-int rh_init(rh_t *instance, ddf_dev_t *dev, ohci_regs_t *regs)
-{
-	assert(instance);
-	instance->address = -1;
-	instance->registers = regs;
-	instance->device = dev;
-	instance->port_count = instance->registers->rh_desc_a & 0xff;
-	rh_init_descriptors(instance);
-	/// \TODO set port power mode
-
-
-	usb_log_info("OHCI root hub with %d ports.\n", instance->port_count);
-
-	//start generic usb hub driver
-	
-	/* TODO: implement */
-	return EOK;
-}
 /*----------------------------------------------------------------------------*/
 
@@ -272,12 +369,21 @@
  */
 static int process_get_port_status_request(rh_t *instance, uint16_t port,
-		usb_transfer_batch_t * request){
-	if(port<1 || port>instance->port_count)
-		return EINVAL;
-	uint32_t * uint32_buffer = (uint32_t*)request->transport_buffer;
+	usb_transfer_batch_t * request) {
+	if (port < 1 || port > instance->port_count)
+		return EINVAL;
+	uint32_t * uint32_buffer = (uint32_t*) request->transport_buffer;
 	request->transfered_size = 4;
-	uint32_buffer[0] = instance->registers->rh_port_status[port -1];
-	return EOK;
-}
+	uint32_buffer[0] = instance->registers->rh_port_status[port - 1];
+#if 0
+	int i;
+	for (i = 0; i < instance->port_count; ++i) {
+		usb_log_debug("port status %d,x%x\n",
+			instance->registers->rh_port_status[i],
+			instance->registers->rh_port_status[i]);
+	}
+#endif
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -291,15 +397,13 @@
  */
 static int process_get_hub_status_request(rh_t *instance,
-		usb_transfer_batch_t * request){
-	uint32_t * uint32_buffer = (uint32_t*)request->transport_buffer;
+	usb_transfer_batch_t * request) {
+	uint32_t * uint32_buffer = (uint32_t*) request->transport_buffer;
+	request->transfered_size = 4;
 	//bits, 0,1,16,17
-	request->transfered_size = 4;
-	uint32_t mask = 1 & (1<<1) & (1<<16) & (1<<17);
+	uint32_t mask = 1 | (1 << 1) | (1 << 16) | (1 << 17);
 	uint32_buffer[0] = mask & instance->registers->rh_status;
 	return EOK;
-
-}
-
-
+}
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -313,24 +417,25 @@
  */
 static int process_get_status_request(rh_t *instance,
-		usb_transfer_batch_t * request)
-{
+	usb_transfer_batch_t * request) {
 	size_t buffer_size = request->buffer_size;
 	usb_device_request_setup_packet_t * request_packet =
-			(usb_device_request_setup_packet_t*)
-			request->setup_buffer;
+		(usb_device_request_setup_packet_t*)
+		request->setup_buffer;
 
 	usb_hub_bm_request_type_t request_type = request_packet->request_type;
-	if(buffer_size<4/*request_packet->length*/){///\TODO
+	if (buffer_size < 4/*request_packet->length*/) {///\TODO
 		usb_log_warning("requested more data than buffer size\n");
 		return EINVAL;
 	}
 
-	if(request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
+	if (request_type == USB_HUB_REQ_TYPE_GET_HUB_STATUS)
 		return process_get_hub_status_request(instance, request);
-	if(request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
-		return process_get_port_status_request(instance, request_packet->index,
-				request);
+	if (request_type == USB_HUB_REQ_TYPE_GET_PORT_STATUS)
+		return process_get_port_status_request(instance,
+		request_packet->index,
+		request);
 	return ENOTSUP;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -339,5 +444,6 @@
  * Result contains bitmap where bit 0 indicates change on hub and
  * bit i indicates change on i`th port (i>0). For more info see
- * Hub and Port status bitmap specification in USB specification.
+ * Hub and Port status bitmap specification in USB specification
+ * (chapter 11.13.4)
  * @param instance root hub instance
  * @param@out buffer pointer to created interrupt mas
@@ -345,26 +451,26 @@
  */
 static void create_interrupt_mask(rh_t *instance, void ** buffer,
-		size_t * buffer_size){
+	size_t * buffer_size) {
 	int bit_count = instance->port_count + 1;
-	(*buffer_size) = (bit_count / 8) + (bit_count%8==0)?0:1;
+	(*buffer_size) = (bit_count / 8) + ((bit_count % 8 == 0) ? 0 : 1);
+
 	(*buffer) = malloc(*buffer_size);
-	uint8_t * bitmap = (uint8_t*)(*buffer);
-	uint32_t mask = (1<<16) + (1<<17);
-	bzero(bitmap,(*buffer_size));
-	if(instance->registers->rh_status & mask){
+	uint8_t * bitmap = (uint8_t*) (*buffer);
+	uint32_t mask = (1 << (USB_HUB_FEATURE_C_HUB_LOCAL_POWER + 16))
+		| (1 << (USB_HUB_FEATURE_C_HUB_OVER_CURRENT + 16));
+	bzero(bitmap, (*buffer_size));
+	if (instance->registers->rh_status & mask) {
 		bitmap[0] = 1;
 	}
 	int port;
-	mask = 0;
-	int i;
-	for(i=16;i<=20;++i)
-		mask += 1<<i;
-	for(port = 1; port<=instance->port_count;++port){
-		if(mask & instance->registers->rh_port_status[port-1]){
-			bitmap[(port+1)/8] += 1<<(port%8);
-		}
-	}
-}
- 
+	mask = port_status_change_mask;
+	for (port = 1; port <= instance->port_count; ++port) {
+		if (mask & instance->registers->rh_port_status[port - 1]) {
+			bitmap[(port) / 8] += 1 << (port % 8);
+		}
+	}
+}
+/*----------------------------------------------------------------------------*/
+
 /**
  * create answer to a descriptor request
@@ -377,29 +483,31 @@
  */
 static int process_get_descriptor_request(rh_t *instance,
-		usb_transfer_batch_t *request){
+	usb_transfer_batch_t *request) {
 	usb_device_request_setup_packet_t * setup_request =
-			(usb_device_request_setup_packet_t*)request->setup_buffer;
+		(usb_device_request_setup_packet_t*) request->setup_buffer;
 	size_t size;
 	const void * result_descriptor = NULL;
 	const uint16_t setup_request_value = setup_request->value_high;
-			//(setup_request->value_low << 8);
+	//(setup_request->value_low << 8);
 	bool del = false;
-	switch (setup_request_value)
-	{
-		case USB_DESCTYPE_HUB: {
+	switch (setup_request_value) {
+		case USB_DESCTYPE_HUB:
+		{
 			uint8_t * descriptor;
 			usb_create_serialized_hub_descriptor(
 				instance, &descriptor, &size);
 			result_descriptor = descriptor;
-			if(result_descriptor) del = true;
+			if (result_descriptor) del = true;
 			break;
 		}
-		case USB_DESCTYPE_DEVICE: {
+		case USB_DESCTYPE_DEVICE:
+		{
 			usb_log_debug("USB_DESCTYPE_DEVICE\n");
 			result_descriptor = &ohci_rh_device_descriptor;
-			size = sizeof(ohci_rh_device_descriptor);
+			size = sizeof (ohci_rh_device_descriptor);
 			break;
 		}
-		case USB_DESCTYPE_CONFIGURATION: {
+		case USB_DESCTYPE_CONFIGURATION:
+		{
 			usb_log_debug("USB_DESCTYPE_CONFIGURATION\n");
 			result_descriptor = instance->descriptors.configuration;
@@ -407,39 +515,43 @@
 			break;
 		}
-		case USB_DESCTYPE_INTERFACE: {
+		case USB_DESCTYPE_INTERFACE:
+		{
 			usb_log_debug("USB_DESCTYPE_INTERFACE\n");
 			result_descriptor = &ohci_rh_iface_descriptor;
-			size = sizeof(ohci_rh_iface_descriptor);
+			size = sizeof (ohci_rh_iface_descriptor);
 			break;
 		}
-		case USB_DESCTYPE_ENDPOINT: {
+		case USB_DESCTYPE_ENDPOINT:
+		{
 			usb_log_debug("USB_DESCTYPE_ENDPOINT\n");
 			result_descriptor = &ohci_rh_ep_descriptor;
-			size = sizeof(ohci_rh_ep_descriptor);
+			size = sizeof (ohci_rh_ep_descriptor);
 			break;
 		}
-		default: {
-			usb_log_debug("USB_DESCTYPE_EINVAL %d \n",setup_request->value);
-			usb_log_debug("\ttype %d\n\trequest %d\n\tvalue %d\n\tindex %d\n\tlen %d\n ",
-					setup_request->request_type,
-					setup_request->request,
-					setup_request_value,
-					setup_request->index,
-					setup_request->length
-					);
+		default:
+		{
+			usb_log_debug("USB_DESCTYPE_EINVAL %d \n",
+				setup_request->value);
+			usb_log_debug("\ttype %d\n\trequest %d\n\tvalue "
+				"%d\n\tindex %d\n\tlen %d\n ",
+				setup_request->request_type,
+				setup_request->request,
+				setup_request_value,
+				setup_request->index,
+				setup_request->length
+				);
 			return EINVAL;
 		}
 	}
-	if(request->buffer_size < size){
+	if (request->buffer_size < size) {
 		size = request->buffer_size;
 	}
 	request->transfered_size = size;
-	memcpy(request->transport_buffer,result_descriptor,size);
-	usb_log_debug("sent desctiptor: %s\n",
-			usb_debug_str_buffer((uint8_t*)request->transport_buffer,size,size));
+	memcpy(request->transport_buffer, result_descriptor, size);
 	if (del)
 		free(result_descriptor);
 	return EOK;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -451,9 +563,9 @@
  * @return error code
  */
-static int process_get_configuration_request(rh_t *instance, 
-		usb_transfer_batch_t *request){
+static int process_get_configuration_request(rh_t *instance,
+	usb_transfer_batch_t *request) {
 	//set and get configuration requests do not have any meaning, only dummy
 	//values are returned
-	if(request->buffer_size != 1)
+	if (request->buffer_size != 1)
 		return EINVAL;
 	request->transport_buffer[0] = 1;
@@ -461,8 +573,9 @@
 	return EOK;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
  * process feature-enabling request on hub
- * 
+ *
  * @param instance root hub instance
  * @param feature feature selector
@@ -470,12 +583,13 @@
  */
 static int process_hub_feature_set_request(rh_t *instance,
-		uint16_t feature){
-	if(! ((1<<feature) & hub_set_feature_valid_mask))
+	uint16_t feature) {
+	if (!((1 << feature) & hub_set_feature_valid_mask))
 		return EINVAL;
 	instance->registers->rh_status =
-			(instance->registers->rh_status | (1<<feature))
-			& (~ hub_clear_feature_by_writing_one_mask);
-	return EOK;
-}
+		(instance->registers->rh_status | (1 << feature))
+		& (~hub_clear_feature_by_writing_one_mask);
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -487,26 +601,25 @@
  */
 static int process_hub_feature_clear_request(rh_t *instance,
-		uint16_t feature){
-	if(! ((1<<feature) & hub_clear_feature_valid_mask))
+	uint16_t feature) {
+	if (!((1 << feature) & hub_clear_feature_valid_mask))
 		return EINVAL;
 	//is the feature cleared directly?
-	if ((1<<feature) & hub_set_feature_direct_mask){
+	if ((1 << feature) & hub_set_feature_direct_mask) {
 		instance->registers->rh_status =
-			(instance->registers->rh_status & (~(1<<feature)))
-			& (~ hub_clear_feature_by_writing_one_mask);
-	}else{//the feature is cleared by writing '1'
+			(instance->registers->rh_status & (~(1 << feature)))
+			& (~hub_clear_feature_by_writing_one_mask);
+	} else {//the feature is cleared by writing '1'
 		instance->registers->rh_status =
-				(instance->registers->rh_status
-				& (~ hub_clear_feature_by_writing_one_mask))
-				| (1<<feature);
-	}
-	return EOK;
-}
-
-
+			(instance->registers->rh_status
+			& (~hub_clear_feature_by_writing_one_mask))
+			| (1 << feature);
+	}
+	return EOK;
+}
+/*----------------------------------------------------------------------------*/
 
 /**
  * process feature-enabling request on hub
- * 
+ *
  * @param instance root hub instance
  * @param feature feature selector
@@ -516,15 +629,16 @@
  */
 static int process_port_feature_set_request(rh_t *instance,
-		uint16_t feature, uint16_t port){
-	if(!((1<<feature) & port_set_feature_valid_mask))
-		return EINVAL;
-	if(port<1 || port>instance->port_count)
+	uint16_t feature, uint16_t port) {
+	if (!((1 << feature) & port_set_feature_valid_mask))
+		return EINVAL;
+	if (port < 1 || port > instance->port_count)
 		return EINVAL;
 	instance->registers->rh_port_status[port - 1] =
-			(instance->registers->rh_port_status[port - 1] | (1<<feature))
-			& (~port_clear_feature_valid_mask);
+		(instance->registers->rh_port_status[port - 1] | (1 << feature))
+		& (~port_clear_feature_valid_mask);
 	/// \TODO any error?
 	return EOK;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -538,25 +652,25 @@
  */
 static int process_port_feature_clear_request(rh_t *instance,
-		uint16_t feature, uint16_t port){
-	if(!((1<<feature) & port_clear_feature_valid_mask))
-		return EINVAL;
-	if(port<1 || port>instance->port_count)
-		return EINVAL;
-	if(feature == USB_HUB_FEATURE_PORT_POWER)
+	uint16_t feature, uint16_t port) {
+	if (!((1 << feature) & port_clear_feature_valid_mask))
+		return EINVAL;
+	if (port < 1 || port > instance->port_count)
+		return EINVAL;
+	if (feature == USB_HUB_FEATURE_PORT_POWER)
 		feature = USB_HUB_FEATURE_PORT_LOW_SPEED;
-	if(feature == USB_HUB_FEATURE_PORT_SUSPEND)
+	if (feature == USB_HUB_FEATURE_PORT_SUSPEND)
 		feature = USB_HUB_FEATURE_PORT_OVER_CURRENT;
 	instance->registers->rh_port_status[port - 1] =
-			(instance->registers->rh_port_status[port - 1] 
-			& (~port_clear_feature_valid_mask))
-			| (1<<feature);
+		(instance->registers->rh_port_status[port - 1]
+		& (~port_clear_feature_valid_mask))
+		| (1 << feature);
 	/// \TODO any error?
 	return EOK;
 }
-
+/*----------------------------------------------------------------------------*/
 
 /**
  * register address to this device
- * 
+ *
  * @param instance root hub instance
  * @param address new address
@@ -564,8 +678,9 @@
  */
 static int process_address_set_request(rh_t *instance,
-		uint16_t address){
+	uint16_t address) {
 	instance->address = address;
 	return EOK;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -579,16 +694,16 @@
  */
 static int process_request_with_output(rh_t *instance,
-		usb_transfer_batch_t *request){
+	usb_transfer_batch_t *request) {
 	usb_device_request_setup_packet_t * setup_request =
-			(usb_device_request_setup_packet_t*)request->setup_buffer;
-	if(setup_request->request == USB_DEVREQ_GET_STATUS){
+		(usb_device_request_setup_packet_t*) request->setup_buffer;
+	if (setup_request->request == USB_DEVREQ_GET_STATUS) {
 		usb_log_debug("USB_DEVREQ_GET_STATUS\n");
 		return process_get_status_request(instance, request);
 	}
-	if(setup_request->request == USB_DEVREQ_GET_DESCRIPTOR){
+	if (setup_request->request == USB_DEVREQ_GET_DESCRIPTOR) {
 		usb_log_debug("USB_DEVREQ_GET_DESCRIPTOR\n");
 		return process_get_descriptor_request(instance, request);
 	}
-	if(setup_request->request == USB_DEVREQ_GET_CONFIGURATION){
+	if (setup_request->request == USB_DEVREQ_GET_CONFIGURATION) {
 		usb_log_debug("USB_DEVREQ_GET_CONFIGURATION\n");
 		return process_get_configuration_request(instance, request);
@@ -596,4 +711,5 @@
 	return ENOTSUP;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -607,12 +723,12 @@
  */
 static int process_request_with_input(rh_t *instance,
-		usb_transfer_batch_t *request){
+	usb_transfer_batch_t *request) {
 	usb_device_request_setup_packet_t * setup_request =
-			(usb_device_request_setup_packet_t*)request->setup_buffer;
+		(usb_device_request_setup_packet_t*) request->setup_buffer;
 	request->transfered_size = 0;
-	if(setup_request->request == USB_DEVREQ_SET_DESCRIPTOR){
+	if (setup_request->request == USB_DEVREQ_SET_DESCRIPTOR) {
 		return ENOTSUP;
 	}
-	if(setup_request->request == USB_DEVREQ_SET_CONFIGURATION){
+	if (setup_request->request == USB_DEVREQ_SET_CONFIGURATION) {
 		//set and get configuration requests do not have any meaning,
 		//only dummy values are returned
@@ -621,4 +737,5 @@
 	return ENOTSUP;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -632,46 +749,50 @@
  */
 static int process_request_without_data(rh_t *instance,
-		usb_transfer_batch_t *request){
+	usb_transfer_batch_t *request) {
 	usb_device_request_setup_packet_t * setup_request =
-			(usb_device_request_setup_packet_t*)request->setup_buffer;
+		(usb_device_request_setup_packet_t*) request->setup_buffer;
 	request->transfered_size = 0;
-	if(setup_request->request == USB_DEVREQ_CLEAR_FEATURE){
-		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE){
+	if (setup_request->request == USB_DEVREQ_CLEAR_FEATURE) {
+		if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
 			usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
 			return process_hub_feature_clear_request(instance,
-					setup_request->value);
-		}
-		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE){
+				setup_request->value);
+		}
+		if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
 			usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
 			return process_port_feature_clear_request(instance,
-					setup_request->value,
-					setup_request->index);
+				setup_request->value,
+				setup_request->index);
 		}
 		usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
-				setup_request->request_type);
-		return EINVAL;
-	}
-	if(setup_request->request == USB_DEVREQ_SET_FEATURE){
-		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE){
+			setup_request->request_type);
+		return EINVAL;
+	}
+	if (setup_request->request == USB_DEVREQ_SET_FEATURE) {
+		if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_HUB_FEATURE) {
 			usb_log_debug("USB_HUB_REQ_TYPE_SET_HUB_FEATURE\n");
 			return process_hub_feature_set_request(instance,
-					setup_request->value);
-		}
-		if(setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE){
+				setup_request->value);
+		}
+		if (setup_request->request_type == USB_HUB_REQ_TYPE_SET_PORT_FEATURE) {
 			usb_log_debug("USB_HUB_REQ_TYPE_SET_PORT_FEATURE\n");
 			return process_port_feature_set_request(instance,
-					setup_request->value,
-					setup_request->index);
-		}
-		usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",setup_request->request_type);
-		return EINVAL;
-	}
-	if(setup_request->request == USB_DEVREQ_SET_ADDRESS){
+				setup_request->value,
+				setup_request->index);
+		}
+		usb_log_debug("USB_HUB_REQ_TYPE_INVALID %d\n",
+			setup_request->request_type);
+		return EINVAL;
+	}
+	if (setup_request->request == USB_DEVREQ_SET_ADDRESS) {
 		usb_log_debug("USB_DEVREQ_SET_ADDRESS\n");
-		return process_address_set_request(instance, setup_request->value);
-	}
-	usb_log_debug("USB_DEVREQ_SET_ENOTSUP %d\n",setup_request->request_type);
+		return process_address_set_request(instance,
+			setup_request->value);
+	}
+	usb_log_debug("USB_DEVREQ_SET_ENOTSUP %d\n",
+		setup_request->request_type);
 	return ENOTSUP;
 }
+/*----------------------------------------------------------------------------*/
 
 /**
@@ -693,83 +814,56 @@
  * @return error code
  */
-static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request){
+static int process_ctrl_request(rh_t *instance, usb_transfer_batch_t *request) {
+	if (!request->setup_buffer) {
+		usb_log_error("root hub received empty transaction?");
+		return EINVAL;
+	}
 	int opResult;
-	if (request->setup_buffer) {
-		if(sizeof(usb_device_request_setup_packet_t)>request->setup_size){
-			usb_log_error("setup packet too small\n");
-			return EINVAL;
-		}
-		usb_log_info("CTRL packet: %s.\n",
-			usb_debug_str_buffer((const uint8_t *)request->setup_buffer, 8, 8));
-		usb_device_request_setup_packet_t * setup_request =
-				(usb_device_request_setup_packet_t*)request->setup_buffer;
-		if(
-			setup_request->request == USB_DEVREQ_GET_STATUS
-			|| setup_request->request == USB_DEVREQ_GET_DESCRIPTOR
-			|| setup_request->request == USB_DEVREQ_GET_CONFIGURATION
-		){
+	if (sizeof (usb_device_request_setup_packet_t) > request->setup_size) {
+		usb_log_error("setup packet too small\n");
+		return EINVAL;
+	}
+	usb_log_info("CTRL packet: %s.\n",
+		usb_debug_str_buffer(
+		(const uint8_t *) request->setup_buffer, 8, 8));
+	usb_device_request_setup_packet_t * setup_request =
+		(usb_device_request_setup_packet_t*)
+		request->setup_buffer;
+	switch (setup_request->request) {
+		case USB_DEVREQ_GET_STATUS:
+		case USB_DEVREQ_GET_DESCRIPTOR:
+		case USB_DEVREQ_GET_CONFIGURATION:
 			usb_log_debug("processing request with output\n");
-			opResult = process_request_with_output(instance,request);
-		}else if(
-			setup_request->request == USB_DEVREQ_CLEAR_FEATURE
-			|| setup_request->request == USB_DEVREQ_SET_FEATURE
-			|| setup_request->request == USB_DEVREQ_SET_ADDRESS
-		){
-			usb_log_debug("processing request without additional data\n");
-			opResult = process_request_without_data(instance,request);
-		}else if(setup_request->request == USB_DEVREQ_SET_DESCRIPTOR
-				|| setup_request->request == USB_DEVREQ_SET_CONFIGURATION
-		){
-			usb_log_debug("processing request with input\n");
-			opResult = process_request_with_input(instance,request);
-		}else{
-			usb_log_warning("received unsuported request: %d\n",
-					setup_request->request
-					);
+			opResult = process_request_with_output(
+				instance, request);
+			break;
+		case USB_DEVREQ_CLEAR_FEATURE:
+		case USB_DEVREQ_SET_FEATURE:
+		case USB_DEVREQ_SET_ADDRESS:
+			usb_log_debug("processing request without "
+				"additional data\n");
+			opResult = process_request_without_data(
+				instance, request);
+			break;
+		case USB_DEVREQ_SET_DESCRIPTOR:
+		case USB_DEVREQ_SET_CONFIGURATION:
+			usb_log_debug("processing request with "
+				"input\n");
+			opResult = process_request_with_input(
+				instance, request);
+			break;
+		default:
+			usb_log_warning("received unsuported request: "
+				"%d\n",
+				setup_request->request
+				);
 			opResult = ENOTSUP;
-		}
-	}else{
-		usb_log_error("root hub received empty transaction?");
-		opResult = EINVAL;
 	}
 	return opResult;
 }
 
-/**
- * process root hub request
- *
- * @param instance root hub instance
- * @param request structure containing both request and response information
- * @return error code
- */
-int rh_request(rh_t *instance, usb_transfer_batch_t *request)
-{
-	assert(instance);
-	assert(request);
-	int opResult;
-	if(request->transfer_type == USB_TRANSFER_CONTROL){
-		usb_log_info("Root hub got CONTROL packet\n");
-		opResult = process_ctrl_request(instance,request);
-	}else if(request->transfer_type == USB_TRANSFER_INTERRUPT){
-		usb_log_info("Root hub got INTERRUPT packet\n");
-		void * buffer;
-		create_interrupt_mask(instance, &buffer,
-			&(request->transfered_size));
-		memcpy(request->transport_buffer,buffer, request->transfered_size);
-		opResult = EOK;
-	}else{
-		opResult = EINVAL;
-	}
-	usb_transfer_batch_finish_error(request, opResult);
-	return EOK;
-}
-/*----------------------------------------------------------------------------*/
-
-
-void rh_interrupt(rh_t *instance)
-{
-	usb_log_info("Whoa whoa wait, I`m not supposed to receive any interrupts, am I?\n");
-	/* TODO: implement? */
-}
+
+
+
 /**
  * @}
