Index: uspace/lib/usbhid/src/hiddescriptor.c
===================================================================
--- uspace/lib/usbhid/src/hiddescriptor.c	(revision ef90ffb36a666fc9c6a9a42b1148e05fdc9c271e)
+++ uspace/lib/usbhid/src/hiddescriptor.c	(revision f3b39b431bddc818b72efc239b05c1caad7fd5c4)
@@ -41,8 +41,25 @@
 #include <assert.h>
 
-
+/*---------------------------------------------------------------------------*/
+/*
+ * Constants defining current parsing mode for correct parsing of the set of
+ * local tags (usage) enclosed in delimter tags.
+ */
+/**
+ * Second delimiter tag was read. The set of local items (usage) ended.
+ */
 #define OUTSIDE_DELIMITER_SET	0
+
+/**
+ * First delimiter tag was read. The set of local items (usage) started.
+ */
 #define START_DELIMITER_SET	1
+
+/**
+ * Parser is in the set of local items.
+ */
 #define INSIDE_DELIMITER_SET	2
+
+/*---------------------------------------------------------------------------*/
 	
 /** The new report item flag. Used to determine when the item is completly
@@ -61,16 +78,30 @@
 #define USB_HID_UNKNOWN_TAG		-99
 
-usb_hid_report_path_t *usb_hid_report_path_try_insert(usb_hid_report_t *report, usb_hid_report_path_t *cmp_path)
-{
-	/* find or append current collection path to the list */
-	//link_t *path_it = report->collection_paths.next;
+/*---------------------------------------------------------------------------*/
+/**
+ * Checks if given collection path is already present in report structure and
+ * inserts it if not.
+ *
+ * @param report Report structure 
+ * @param cmp_path The collection path 
+ * @return Pointer to the result collection path in report structure.
+ * @retval NULL If some error occurs
+ */
+usb_hid_report_path_t *usb_hid_report_path_try_insert(
+		usb_hid_report_t *report, usb_hid_report_path_t *cmp_path) {
+	
 	link_t *path_it = report->collection_paths.prev->next;
 	usb_hid_report_path_t *path = NULL;
 	
+	if((report == NULL) || (cmp_path == NULL)) {
+		return NULL;
+	}
 	
 	while(path_it != &report->collection_paths) {
-		path = list_get_instance(path_it, usb_hid_report_path_t, link);
-		
-		if(usb_hid_report_compare_usage_path(path, cmp_path, USB_HID_PATH_COMPARE_STRICT) == EOK){
+		path = list_get_instance(path_it, usb_hid_report_path_t,
+				link);
+		
+		if(usb_hid_report_compare_usage_path(path, cmp_path,
+					USB_HID_PATH_COMPARE_STRICT) == EOK){
 			break;
 		}			
@@ -78,5 +109,8 @@
 	}
 	if(path_it == &report->collection_paths) {
-		path = usb_hid_report_path_clone(cmp_path);			
+		path = usb_hid_report_path_clone(cmp_path);
+		if(path == NULL) {
+			return NULL;
+		}
 		list_append(&path->link, &report->collection_paths);					
 		report->collection_paths_count++;
@@ -85,8 +119,10 @@
 	}
 	else {
-		return list_get_instance(path_it, usb_hid_report_path_t, link);
-	}
-}
-
+		return list_get_instance(path_it, usb_hid_report_path_t,
+				link); 
+	}
+}
+
+/*---------------------------------------------------------------------------*/
 /**
  * Initialize the report descriptor parser structure
@@ -94,4 +130,6 @@
  * @param parser Report descriptor parser structure
  * @return Error code
+ * @retval EINVAL If no report structure was given
+ * @retval EOK If report structure was successfully initialized
  */
 int usb_hid_report_init(usb_hid_report_t *report)
@@ -109,11 +147,21 @@
 }
 
-
-/*
- *
- *
- */
-int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item)
-{
+/*---------------------------------------------------------------------------*/
+
+/**
+ *
+ *
+ * @param report Report structure in which the new report items should be
+ * 		 stored
+ * @param report_item Current report descriptor's parsing state table 
+ * @return Error code
+ * @retval EOK If all fields were successfully append to report
+ * @retval EINVAL If invalid parameters (NULL) was given
+ * @retval ENOMEM If there is no memmory to store new report description
+ *
+ */
+int usb_hid_report_append_fields(usb_hid_report_t *report,
+		usb_hid_report_item_t *report_item) {
+
 	usb_hid_report_field_t *field;
 	int i;
@@ -121,7 +169,13 @@
 	uint32_t *usages;
 	int usages_used=0;
+
+	if((report == NULL) || (report_item == NULL)) {
+		return EINVAL;
+	}
+
 	if(report_item->usages_count > 0){
 		usages = malloc(sizeof(int32_t) * report_item->usages_count);
-		memcpy(usages, report_item->usages, sizeof(int32_t) * report_item->usages_count);
+		memcpy(usages, report_item->usages, sizeof(int32_t) *
+				report_item->usages_count); 
 	}
 	else {
@@ -144,6 +198,6 @@
 		if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0){
 			/* 
-			 	Store usage array. The Correct Usage Page and Usage is depending 
-			 	on data in report and will be filled later
+			Store usage array. The Correct Usage Page and Usage is
+			depending on data in report and will be filled later
 			*/
 			field->usage = 0;
@@ -162,10 +216,12 @@
 			}
 			else {
-				usage = report_item->usages[report_item->usages_count - 1];
+				usage =	report_item->usages[
+					report_item->usages_count- 1]; 
 			}
 
 			if(USB_HID_IS_EXTENDED_USAGE(usage)){
 				field->usage = USB_HID_EXTENDED_USAGE(usage);
-				field->usage_page = USB_HID_EXTENDED_USAGE_PAGE(usage);
+				field->usage_page = 
+					USB_HID_EXTENDED_USAGE_PAGE(usage);
 			}
 			else {
@@ -176,13 +232,19 @@
 		}
 		
-		usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_GLOBAL, field->usage_page);
-		usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_LOCAL, field->usage);
-
-		field->collection_path = usb_hid_report_path_try_insert(report, path);
+		usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_GLOBAL,
+				field->usage_page);
+		usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_LOCAL,
+				field->usage);
+
+		field->collection_path =
+			usb_hid_report_path_try_insert(report, path);
 
 		field->size = report_item->size;
 		
-		size_t offset_byte = (report_item->offset + (i * report_item->size)) / 8;
-		size_t offset_bit = 8 - ((report_item->offset + (i * report_item->size)) % 8) - report_item->size;
+		size_t offset_byte = (report_item->offset + (i *
+			report_item->size)) / 8; 
+
+		size_t offset_bit = 8 - ((report_item->offset + (i *
+			report_item->size)) % 8) - report_item->size;
 
 		field->offset = 8 * offset_byte + offset_bit;
@@ -195,8 +257,16 @@
 		/* find the right report list*/
 		usb_hid_report_description_t *report_des;
-		report_des = usb_hid_report_find_description(report, report_item->id, report_item->type);
+		report_des = usb_hid_report_find_description(report,
+			report_item->id, report_item->type);
+		
 		if(report_des == NULL){
-			report_des = malloc(sizeof(usb_hid_report_description_t));
-			memset(report_des, 0, sizeof(usb_hid_report_description_t));
+			report_des = malloc(
+				sizeof(usb_hid_report_description_t));
+			if(report_des == NULL) {
+				return ENOMEM;
+			}
+
+			memset(report_des, 0,
+				sizeof(usb_hid_report_description_t));
 
 			report_des->type = report_item->type;
@@ -225,14 +295,28 @@
 	return EOK;
 }
-
-usb_hid_report_description_t * usb_hid_report_find_description(const usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
-{
+/*---------------------------------------------------------------------------*/
+/**
+ * Finds description of report with given report_id and of given type in
+ * opaque report structure.
+ *
+ * @param report Opaque structure containing the parsed report descriptor
+ * @param report_id ReportId of report we are searching
+ * @param type Type of report we are searching
+ * @return Pointer to the particular report description
+ * @retval NULL If no description is founded
+ */
+usb_hid_report_description_t * usb_hid_report_find_description(
+		const usb_hid_report_t *report, uint8_t report_id,
+		usb_hid_report_type_t type) {
+
 	link_t *report_it = report->reports.next;
 	usb_hid_report_description_t *report_des = NULL;
 	
 	while(report_it != &report->reports) {
-		report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
-
-		if((report_des->report_id == report_id) && (report_des->type == type)){
+		report_des = list_get_instance(report_it,
+				usb_hid_report_description_t, link);
+
+		if((report_des->report_id == report_id) && 
+		   (report_des->type == type)) { 
 			return report_des;
 		}
@@ -243,4 +327,5 @@
 	return NULL;
 }
+/*---------------------------------------------------------------------------*/
 
 /** Parse HID report descriptor.
@@ -249,4 +334,7 @@
  * @param data Data describing the report.
  * @return Error code.
+ * @retval ENOMEM If no more memmory is available
+ * @retval EINVAL If invalid data are founded
+ * @retval EOK If report descriptor is successfully parsed
  */
 int usb_hid_parse_report_descriptor(usb_hid_report_t *report, 
@@ -299,88 +387,114 @@
 			
 			ret = usb_hid_report_parse_tag(tag,class,data+i+1,
-			                               item_size,report_item, usage_path);
+				item_size,report_item, usage_path);
+
 			switch(ret){
-				case USB_HID_NEW_REPORT_ITEM:
-					// store report item to report and create the new one
-					// store current collection path
-					report_item->usage_path = usage_path;
+			case USB_HID_NEW_REPORT_ITEM:
+				/* store report item to report and create the
+				 * new one store current collection path
+				 */
+				report_item->usage_path = usage_path;
 					
-					usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id);	
-					if(report_item->id != 0){
-						report->use_report_ids = 1;
-					}
+				usb_hid_report_path_set_report_id(
+				     report_item->usage_path, report_item->id);
+				
+				if(report_item->id != 0){
+					report->use_report_ids = 1;
+				}
 					
-					switch(tag) {
-						case USB_HID_REPORT_TAG_INPUT:
-							report_item->type = USB_HID_REPORT_TYPE_INPUT;
-							report_item->offset = offset_input;
-							offset_input += report_item->count * report_item->size;
-							break;
-						case USB_HID_REPORT_TAG_OUTPUT:
-							report_item->type = USB_HID_REPORT_TYPE_OUTPUT;
-							report_item->offset = offset_output;
-							offset_output += report_item->count * report_item->size;
-
-							break;
-						case USB_HID_REPORT_TAG_FEATURE:
-							report_item->type = USB_HID_REPORT_TYPE_FEATURE;
-							report_item->offset = offset_feature;
-							offset_feature += report_item->count * report_item->size;
-							break;
-						default:
-						    usb_log_debug("\tjump over - tag %X\n", tag);
-						    break;
-					}
+				switch(tag) {
+				case USB_HID_REPORT_TAG_INPUT:
+					report_item->type = 
+					    USB_HID_REPORT_TYPE_INPUT;
+
+					report_item->offset = offset_input;
+					offset_input += report_item->count * 
+					    report_item->size;
+					break;
+	
+				case USB_HID_REPORT_TAG_OUTPUT:
+					report_item->type = 
+					    USB_HID_REPORT_TYPE_OUTPUT;
 					
-					/* 
-					 * append new fields to the report
-					 * structure 					 
-					 */
-					usb_hid_report_append_fields(report, report_item);
-
-					/* reset local items */
-					usb_hid_report_reset_local_items (report_item);
-
+					report_item->offset = offset_output;
+					offset_output += report_item->count * 
+					    report_item->size;
 					break;
-
-				case USB_HID_RESET_OFFSET:
-					offset_input = 0;
-					offset_output = 0;
-					offset_feature = 0;
-					usb_hid_report_path_set_report_id (usage_path, report_item->id);
+	
+				case USB_HID_REPORT_TAG_FEATURE:
+					report_item->type = 
+					    USB_HID_REPORT_TYPE_FEATURE;
+
+					report_item->offset = offset_feature;
+					offset_feature += report_item->count * 
+						report_item->size;
 					break;
-
-				case USB_HID_REPORT_TAG_PUSH:
-					// push current state to stack
-					new_report_item = usb_hid_report_item_clone(report_item);
-					usb_hid_report_path_t *tmp_path = usb_hid_report_path_clone(usage_path);
-					new_report_item->usage_path = tmp_path; 
-
-					list_prepend (&new_report_item->link, &stack);
-					break;
-				case USB_HID_REPORT_TAG_POP:
-					// restore current state from stack
-					if(list_empty (&stack)) {
-						return EINVAL;
-					}
-					free(report_item);
+	
+				default:
+					usb_log_debug2(
+					    "\tjump over - tag %X\n", tag);
+				    	break;
+				}
+					
+				/* 
+				 * append new fields to the report structure 					 
+				 */
+				usb_hid_report_append_fields(report, 
+				    report_item);
+
+				/* reset local items */
+				usb_hid_report_reset_local_items (report_item);
+				break;
+
+			case USB_HID_RESET_OFFSET:
+				offset_input = 0;
+				offset_output = 0;
+				offset_feature = 0;
+				usb_hid_report_path_set_report_id (usage_path, 
+				    report_item->id);
+				break;
+
+			case USB_HID_REPORT_TAG_PUSH:
+				// push current state to stack
+				new_report_item = usb_hid_report_item_clone(
+				    report_item);
+				
+				usb_hid_report_path_t *tmp_path = 
+				    usb_hid_report_path_clone(usage_path);
+
+				new_report_item->usage_path = tmp_path; 
+
+				list_prepend (&new_report_item->link, &stack);
+				break;
+			case USB_HID_REPORT_TAG_POP:
+				// restore current state from stack
+				if(list_empty (&stack)) {
+					return EINVAL;
+				}
+				free(report_item);
 						
-					report_item = list_get_instance(stack.next, usb_hid_report_item_t, link);
+				report_item = list_get_instance(stack.next, 
+				    usb_hid_report_item_t, link);
 					
-					usb_hid_report_usage_path_t *tmp_usage_path;
-					tmp_usage_path = list_get_instance(report_item->usage_path->link.prev, usb_hid_report_usage_path_t, link);
+				usb_hid_report_usage_path_t *tmp_usage_path;
+				tmp_usage_path = list_get_instance(
+				    report_item->usage_path->link.prev, 
+				    usb_hid_report_usage_path_t, link);
 					
-					usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_GLOBAL, tmp_usage_path->usage_page);
-					usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_LOCAL, tmp_usage_path->usage);
-
-					usb_hid_report_path_free(report_item->usage_path);
-					list_initialize(&report_item->usage_path->link);
-					list_remove (stack.next);
+				usb_hid_report_set_last_item(usage_path, 
+				    USB_HID_TAG_CLASS_GLOBAL, tmp_usage_path->usage_page);
+				
+				usb_hid_report_set_last_item(usage_path, 
+				    USB_HID_TAG_CLASS_LOCAL, tmp_usage_path->usage);
+
+				usb_hid_report_path_free(report_item->usage_path);
+				list_initialize(&report_item->usage_path->link);
+				list_remove (stack.next);
 					
-					break;
+				break;
 					
-				default:
-					// nothing special to do					
-					break;
+			default:
+				// nothing special to do					
+				break;
 			}
 
@@ -399,4 +513,5 @@
 }
 
+/*---------------------------------------------------------------------------*/
 
 /**
@@ -409,30 +524,35 @@
  * @return Code of action to be done next
  */
-int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
-{	
+int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data,
+	size_t item_size, usb_hid_report_item_t *report_item,
+	usb_hid_report_path_t *usage_path) {	
+	
 	int ret;
 	
 	switch(class){
-		case USB_HID_TAG_CLASS_MAIN:
-
-			if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
-				return USB_HID_NEW_REPORT_ITEM;
-			}
-			else {
-				/*TODO process the error */
-				return ret;
-			   }
-			break;
-
-		case USB_HID_TAG_CLASS_GLOBAL:	
-			return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
-			break;
-
-		case USB_HID_TAG_CLASS_LOCAL:			
-			return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
-			break;
-		default:
-			return USB_HID_NO_ACTION;
+	case USB_HID_TAG_CLASS_MAIN:
+
+		if((ret=usb_hid_report_parse_main_tag(tag, data, item_size,
+			report_item, usage_path)) == EOK) {
+
+			return USB_HID_NEW_REPORT_ITEM;
+		}
+		else {
+			return ret;
+		}
+		break;
+
+	case USB_HID_TAG_CLASS_GLOBAL:	
+		return usb_hid_report_parse_global_tag(tag, data, item_size,
+			report_item, usage_path);
+		break;
+
+	case USB_HID_TAG_CLASS_LOCAL:			
+		return usb_hid_report_parse_local_tag(tag, data, item_size,
+			report_item, usage_path);
+		break;
+	
+	default:
+		return USB_HID_NO_ACTION;
 	}
 }
@@ -448,6 +568,7 @@
  */
 
-int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
+int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, 
+	size_t item_size, usb_hid_report_item_t *report_item,
+	usb_hid_report_path_t *usage_path)
 {
 	usb_hid_report_usage_path_t *path_item;
@@ -455,38 +576,46 @@
 	switch(tag)
 	{
-		case USB_HID_REPORT_TAG_INPUT:
-		case USB_HID_REPORT_TAG_OUTPUT:
-		case USB_HID_REPORT_TAG_FEATURE:
-			report_item->item_flags = *data;			
-			return EOK;			
-			break;
+	case USB_HID_REPORT_TAG_INPUT:
+	case USB_HID_REPORT_TAG_OUTPUT:
+	case USB_HID_REPORT_TAG_FEATURE:
+		report_item->item_flags = *data;			
+		return EOK;			
+		break;
 			
-		case USB_HID_REPORT_TAG_COLLECTION:
-
-			// store collection atributes
-			path_item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
-			path_item->flags = *data;	
+	case USB_HID_REPORT_TAG_COLLECTION:
+
+		/* store collection atributes */
+		path_item = list_get_instance(usage_path->head.prev, 
+			usb_hid_report_usage_path_t, link);
+		path_item->flags = *data;	
 			
-			// set last item
-			usb_hid_report_set_last_item(usage_path, 
-			                             USB_HID_TAG_CLASS_GLOBAL, 
-			                             USB_HID_EXTENDED_USAGE_PAGE(report_item->usages[report_item->usages_count-1]));
-			usb_hid_report_set_last_item(usage_path, 
-			                             USB_HID_TAG_CLASS_LOCAL, 
-			                             USB_HID_EXTENDED_USAGE(report_item->usages[report_item->usages_count-1]));
+		/* set last item */
+		usb_hid_report_set_last_item(usage_path, 
+			USB_HID_TAG_CLASS_GLOBAL, 
+			USB_HID_EXTENDED_USAGE_PAGE(report_item->usages[
+				report_item->usages_count-1]));
+
+		usb_hid_report_set_last_item(usage_path, 
+			USB_HID_TAG_CLASS_LOCAL, 
+			USB_HID_EXTENDED_USAGE(report_item->usages[
+				report_item->usages_count-1]));
 			
-			// append the new one which will be set by common
-			// usage/usage page
-			usb_hid_report_path_append_item(usage_path, report_item->usage_page, report_item->usages[report_item->usages_count-1]);	
-			usb_hid_report_reset_local_items (report_item);
-			return USB_HID_NO_ACTION;
-			break;
+		/* append the new one which will be set by common usage/usage
+		 * page */
+		usb_hid_report_path_append_item(usage_path, 
+			report_item->usage_page, 
+			report_item->usages[report_item->usages_count-1]);
+
+		usb_hid_report_reset_local_items (report_item);
+		return USB_HID_NO_ACTION;
+		break;
 			
-		case USB_HID_REPORT_TAG_END_COLLECTION:
-			usb_hid_report_remove_last_item(usage_path);
-			return USB_HID_NO_ACTION;
-			break;
-		default:
-			return USB_HID_NO_ACTION;
+	case USB_HID_REPORT_TAG_END_COLLECTION:
+		usb_hid_report_remove_last_item(usage_path);
+		return USB_HID_NO_ACTION;
+		break;
+
+	default:
+		return USB_HID_NO_ACTION;
 	}
 
@@ -503,53 +632,76 @@
  * @return Error code
  */
-int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
-{
-	// TODO take care about the bit length of data
+int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, 
+	size_t item_size, usb_hid_report_item_t *report_item, 
+	usb_hid_report_path_t *usage_path) { 
+	
 	switch(tag)
 	{
-		case USB_HID_REPORT_TAG_USAGE_PAGE:
-			report_item->usage_page = usb_hid_report_tag_data_uint32(data, item_size);
-			break;
-		case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
-			report_item->logical_minimum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
-			break;
-		case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
-			report_item->logical_maximum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
-			break;
-		case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
-			report_item->physical_minimum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
-			break;			
-		case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
-			report_item->physical_maximum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
-
-			break;
-		case USB_HID_REPORT_TAG_UNIT_EXPONENT:
-			report_item->unit_exponent = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_UNIT:
-			report_item->unit = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_REPORT_SIZE:
-			report_item->size = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_REPORT_COUNT:
-			report_item->count = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_REPORT_ID:
-			report_item->id = usb_hid_report_tag_data_uint32(data,item_size);
-			return USB_HID_RESET_OFFSET;
-			break;
-		case USB_HID_REPORT_TAG_PUSH:
-		case USB_HID_REPORT_TAG_POP:
-			/* 
-			 * stack operations are done in top level parsing
-			 * function
-			 */
-			return tag;
-			break;
+	case USB_HID_REPORT_TAG_USAGE_PAGE:
+		report_item->usage_page = 
+			usb_hid_report_tag_data_uint32(data, item_size);
+		break;
+
+	case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
+		report_item->logical_minimum = USB_HID_UINT32_TO_INT32(
+			usb_hid_report_tag_data_uint32(data,item_size),
+		       	item_size * 8);
+		break;
+
+	case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
+		report_item->logical_maximum = USB_HID_UINT32_TO_INT32(
+			usb_hid_report_tag_data_uint32(data,item_size), 
+			item_size * 8);
+		break;
+
+	case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
+		report_item->physical_minimum = USB_HID_UINT32_TO_INT32(
+			usb_hid_report_tag_data_uint32(data,item_size), 
+			item_size * 8);
+		break;			
+
+	case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
+		report_item->physical_maximum = USB_HID_UINT32_TO_INT32(
+			usb_hid_report_tag_data_uint32(data,item_size), 
+			item_size * 8);
+		break;
+
+	case USB_HID_REPORT_TAG_UNIT_EXPONENT:
+		report_item->unit_exponent = usb_hid_report_tag_data_uint32(
+			data,item_size);
+		break;
+
+	case USB_HID_REPORT_TAG_UNIT:
+		report_item->unit = usb_hid_report_tag_data_uint32(
+			data,item_size);
+		break;
+
+	case USB_HID_REPORT_TAG_REPORT_SIZE:
+		report_item->size = usb_hid_report_tag_data_uint32(
+			data,item_size);
+		break;
+
+	case USB_HID_REPORT_TAG_REPORT_COUNT:
+		report_item->count = usb_hid_report_tag_data_uint32(
+			data,item_size);
+		break;
+
+	case USB_HID_REPORT_TAG_REPORT_ID:
+		report_item->id = usb_hid_report_tag_data_uint32(data, 
+			item_size);
+		return USB_HID_RESET_OFFSET;
+		break;
+	
+	case USB_HID_REPORT_TAG_PUSH:
+	case USB_HID_REPORT_TAG_POP:
+		/* 
+		 * stack operations are done in top level parsing
+		 * function
+		 */
+		return tag;
+		break;
 			
-		default:
-			return USB_HID_NO_ACTION;
+	default:
+		return USB_HID_NO_ACTION;
 	}
 
@@ -566,92 +718,135 @@
  * @return Error code
  */
-int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
-                             usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
+int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, 
+	size_t item_size, usb_hid_report_item_t *report_item, 
+	usb_hid_report_path_t *usage_path)
 {
 	int32_t extended_usage;
 	
 	switch(tag) {
-		case USB_HID_REPORT_TAG_USAGE:
-			switch(report_item->in_delimiter) {
-				case INSIDE_DELIMITER_SET:
-					// nothing to do
-					break;
-				case START_DELIMITER_SET:
-					report_item->in_delimiter = INSIDE_DELIMITER_SET;
-				case OUTSIDE_DELIMITER_SET:
-					extended_usage = ((report_item->usage_page) << 16);
-					extended_usage += usb_hid_report_tag_data_uint32(data,item_size);
-					report_item->usages[report_item->usages_count] = extended_usage;
-					report_item->usages_count++;
-					break;
+	case USB_HID_REPORT_TAG_USAGE:
+		switch(report_item->in_delimiter) {
+		case INSIDE_DELIMITER_SET:
+			/* nothing to do
+			 * we catch only the first one
+			 */
+			break;
+	
+		case START_DELIMITER_SET:
+			report_item->in_delimiter = INSIDE_DELIMITER_SET;
+		case OUTSIDE_DELIMITER_SET:
+			extended_usage = ((report_item->usage_page) << 16);
+			extended_usage += usb_hid_report_tag_data_uint32(
+				data,item_size);
+
+			report_item->usages[report_item->usages_count] = 
+				extended_usage;
+
+			report_item->usages_count++;
+			break;
+		}
+		break;
+		
+	case USB_HID_REPORT_TAG_USAGE_MINIMUM:			
+		if (item_size == 3) {
+			// usage extended usages
+			report_item->extended_usage_page = 
+			    USB_HID_EXTENDED_USAGE_PAGE(
+			    usb_hid_report_tag_data_uint32(data,item_size));
+			   
+
+			report_item->usage_minimum = 
+			    USB_HID_EXTENDED_USAGE(
+			    usb_hid_report_tag_data_uint32(data,item_size));
+		}
+		else {
+			report_item->usage_minimum = 
+			    usb_hid_report_tag_data_uint32(data,item_size);
+		}
+		break;
+	
+	case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
+		if (item_size == 3) {
+			if(report_item->extended_usage_page != 
+			    USB_HID_EXTENDED_USAGE_PAGE(	
+			    usb_hid_report_tag_data_uint32(data,item_size))) {
+				
+				return EINVAL;
 			}
-			break;
-		case USB_HID_REPORT_TAG_USAGE_MINIMUM:			
-			if (item_size == 3) {
-				// usage extended usages
-				report_item->extended_usage_page = (usb_hid_report_tag_data_uint32(data,item_size) & 0xFF00) >> 16; 
-				report_item->usage_minimum = usb_hid_report_tag_data_uint32(data,item_size) & 0xFF;
+				
+			// usage extended usages
+			report_item->extended_usage_page = 
+				USB_HID_EXTENDED_USAGE_PAGE(
+				usb_hid_report_tag_data_uint32(data,item_size));
+
+			report_item->usage_maximum = 
+				USB_HID_EXTENDED_USAGE(
+				usb_hid_report_tag_data_uint32(data,item_size));
+		}
+		else {
+			report_item->usage_maximum = 
+				usb_hid_report_tag_data_uint32(data,item_size);
+		}
+
+		// vlozit zaznamy do pole usages
+		int32_t i;
+		for(i = report_item->usage_minimum; 
+		    i <= report_item->usage_maximum; i++) {
+
+			if(report_item->extended_usage_page) {
+			    report_item->usages[report_item->usages_count++] = 
+				(report_item->extended_usage_page << 16) + i;
 			}
-			else {
-				report_item->usage_minimum = usb_hid_report_tag_data_uint32(data,item_size);
+			else {			
+			    report_item->usages[report_item->usages_count++] = 
+				(report_item->usage_page << 16) + i;
 			}
-			break;
-		case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
-			if (item_size == 3) {
-
-				if(report_item->extended_usage_page != ((usb_hid_report_tag_data_uint32(data,item_size) & 0xFF00) >> 16)) {
-					return EINVAL;
-				}
-				
-				// usage extended usages
-				report_item->extended_usage_page = (usb_hid_report_tag_data_uint32(data,item_size) & 0xFF00) >> 16; 
-				report_item->usage_maximum = usb_hid_report_tag_data_uint32(data,item_size) & 0xFF;
-			}
-			else {
-				report_item->usage_maximum = usb_hid_report_tag_data_uint32(data,item_size);
-			}
-
-			// vlozit zaznamy do pole usages
-			int32_t i;
-			for(i=report_item->usage_minimum; i<=report_item->usage_maximum; i++) {
-				if(report_item->extended_usage_page) {
-					report_item->usages[report_item->usages_count++] = (report_item->extended_usage_page << 16) + i;
-				}
-				else {
-					
-					report_item->usages[report_item->usages_count++] = (report_item->usage_page << 16) + i;
-				}
-			}
-			report_item->extended_usage_page = 0;
+		}
+		report_item->extended_usage_page = 0;
 			
-			break;
-		case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
-			report_item->designator_index = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
-			report_item->designator_minimum = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
-			report_item->designator_maximum = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_STRING_INDEX:
-			report_item->string_index = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_STRING_MINIMUM:
-			report_item->string_minimum = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-		case USB_HID_REPORT_TAG_STRING_MAXIMUM:
-			report_item->string_maximum = usb_hid_report_tag_data_uint32(data,item_size);
-			break;			
-		case USB_HID_REPORT_TAG_DELIMITER:
-			report_item->in_delimiter = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-
-		default:
-			return USB_HID_NO_ACTION;
+		break;
+		
+	case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
+		report_item->designator_index = 
+			usb_hid_report_tag_data_uint32(data,item_size);
+		break;
+	
+	case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
+		report_item->designator_minimum = 
+			usb_hid_report_tag_data_uint32(data,item_size);
+		break;
+
+	case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
+		report_item->designator_maximum = 
+			usb_hid_report_tag_data_uint32(data,item_size);
+		break;
+
+	case USB_HID_REPORT_TAG_STRING_INDEX:
+		report_item->string_index = 
+			usb_hid_report_tag_data_uint32(data,item_size);
+		break;
+
+	case USB_HID_REPORT_TAG_STRING_MINIMUM:
+		report_item->string_minimum = 
+			usb_hid_report_tag_data_uint32(data,item_size);
+		break;
+
+	case USB_HID_REPORT_TAG_STRING_MAXIMUM:
+		report_item->string_maximum = 
+			usb_hid_report_tag_data_uint32(data,item_size);
+		break;			
+
+	case USB_HID_REPORT_TAG_DELIMITER:
+		report_item->in_delimiter = 
+			usb_hid_report_tag_data_uint32(data,item_size);
+		break;
+
+	default:
+		return USB_HID_NO_ACTION;
 	}
 
 	return EOK;
 }
+/*---------------------------------------------------------------------------*/
 
 /**
@@ -674,4 +869,5 @@
 	return result;
 }
+/*---------------------------------------------------------------------------*/
 
 /**
@@ -694,15 +890,23 @@
 	for(item = head->next; item != head; item = item->next) {
                 
-		report_item = list_get_instance(item, usb_hid_report_field_t, link);
+		report_item = list_get_instance(item, usb_hid_report_field_t, 
+				link);
 
 		usb_log_debug("\t\tOFFSET: %X\n", report_item->offset);
-		usb_log_debug("\t\tSIZE: %zu\n", report_item->size);				
-		usb_log_debug("\t\tLOGMIN: %d\n", report_item->logical_minimum);
-		usb_log_debug("\t\tLOGMAX: %d\n", report_item->logical_maximum);		
-		usb_log_debug("\t\tPHYMIN: %d\n", report_item->physical_minimum);		
-		usb_log_debug("\t\tPHYMAX: %d\n", report_item->physical_maximum);				
-		usb_log_debug("\t\ttUSAGEMIN: %X\n", report_item->usage_minimum);
-		usb_log_debug("\t\tUSAGEMAX: %X\n", report_item->usage_maximum);
-		usb_log_debug("\t\tUSAGES COUNT: %zu\n", report_item->usages_count);
+		usb_log_debug("\t\tSIZE: %zu\n", report_item->size);
+		usb_log_debug("\t\tLOGMIN: %d\n", 
+			report_item->logical_minimum);
+		usb_log_debug("\t\tLOGMAX: %d\n", 
+			report_item->logical_maximum);		
+		usb_log_debug("\t\tPHYMIN: %d\n", 
+			report_item->physical_minimum);		
+		usb_log_debug("\t\tPHYMAX: %d\n", 
+			report_item->physical_maximum);				
+		usb_log_debug("\t\ttUSAGEMIN: %X\n", 
+			report_item->usage_minimum);
+		usb_log_debug("\t\tUSAGEMAX: %X\n",
+			       report_item->usage_maximum);
+		usb_log_debug("\t\tUSAGES COUNT: %zu\n", 
+			report_item->usages_count);
 
 		usb_log_debug("\t\tVALUE: %X\n", report_item->value);
@@ -716,6 +920,7 @@
 	}
 
-
-}
+}
+/*---------------------------------------------------------------------------*/
+
 /**
  * Prints content of given report descriptor in human readable format.
@@ -734,5 +939,6 @@
 
 	while(report_it != &report->reports) {
-		report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
+		report_des = list_get_instance(report_it, 
+			usb_hid_report_description_t, link);
 		usb_log_debug("Report ID: %d\n", report_des->report_id);
 		usb_log_debug("\tType: %d\n", report_des->type);
@@ -742,14 +948,8 @@
 		usb_hid_descriptor_print_list(&report_des->report_items);
 
-/*
-		link_t *path_it = report->collection_paths.next;
-		while(path_it != &report->collection_paths) {
-			usb_hid_print_usage_path (list_get_instance(path_it, usb_hid_report_path_t, link));
-			path_it = path_it->next;
-		}
-*/		
 		report_it = report_it->next;
 	}
 }
+/*---------------------------------------------------------------------------*/
 
 /**
@@ -776,5 +976,5 @@
 
 		while(!list_empty(&report_item->usage_path->link)) {
-			usb_hid_report_remove_last_item(report_item->usage_path);
+		    usb_hid_report_remove_last_item(report_item->usage_path);
 		}
 
@@ -788,4 +988,5 @@
 	
 }
+/*---------------------------------------------------------------------------*/
 
 /** Frees the HID report descriptor parser structure 
@@ -803,5 +1004,7 @@
 	usb_hid_report_path_t *path;
 	while(!list_empty(&report->collection_paths)) {
-		path = list_get_instance(report->collection_paths.next, usb_hid_report_path_t, link);
+		path = list_get_instance(report->collection_paths.next, 
+				usb_hid_report_path_t, link);
+
 		usb_hid_report_path_free(path);		
 	}
@@ -811,9 +1014,14 @@
 	usb_hid_report_field_t *field;
 	while(!list_empty(&report->reports)) {
-		report_des = list_get_instance(report->reports.next, usb_hid_report_description_t, link);
+		report_des = list_get_instance(report->reports.next, 
+				usb_hid_report_description_t, link);
+
 		list_remove(&report_des->link);
 		
 		while(!list_empty(&report_des->report_items)) {
-			field = list_get_instance(report_des->report_items.next, usb_hid_report_field_t, link);
+			field = list_get_instance(
+				report_des->report_items.next, 
+				usb_hid_report_field_t, link);
+
 			list_remove(&field->link);
 
@@ -826,4 +1034,5 @@
 	return;
 }
+/*---------------------------------------------------------------------------*/
 
 /**
