Index: uspace/lib/usb/src/hiddescriptor.c
===================================================================
--- uspace/lib/usb/src/hiddescriptor.c	(revision f90c0d6c1710d4e0841deffb9389c9f882f27afd)
+++ uspace/lib/usb/src/hiddescriptor.c	(revision 0a494fe397fd717d3deccc0927261dd80e6ee6da)
@@ -41,9 +41,4 @@
 #include <assert.h>
 
-
-#define OUTSIDE_DELIMITER_SET	0
-#define START_DELIMITER_SET	1
-#define INSIDE_DELIMITER_SET	2
-	
 /** The new report item flag. Used to determine when the item is completly
  * configured and should be added to the report structure
@@ -61,6 +56,31 @@
 #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)
-{
+
+/**
+ * Initialize the report descriptor parser structure
+ *
+ * @param parser Report descriptor parser structure
+ * @return Error code
+ */
+int usb_hid_report_init(usb_hid_report_t *report)
+{
+	if(report == NULL) {
+		return EINVAL;
+	}
+
+	memset(report, 0, sizeof(usb_hid_report_t));
+	list_initialize(&report->reports);
+	list_initialize(&report->collection_paths);
+
+	report->use_report_ids = 0;
+    return EOK;   
+}
+
+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;
+
+
 	/* find or append current collection path to the list */
 	link_t *path_it = report->collection_paths.next;
@@ -69,5 +89,5 @@
 		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){
+		if(usb_hid_report_compare_usage_path(path, report_item->usage_path, USB_HID_PATH_COMPARE_STRICT) == EOK){
 			break;
 		}			
@@ -75,44 +95,8 @@
 	}
 	if(path_it == &report->collection_paths) {
-		path = usb_hid_report_path_clone(cmp_path);			
+		path = usb_hid_report_path_clone(report_item->usage_path);			
 		list_append(&path->link, &report->collection_paths);					
 		report->collection_paths_count++;
-
-		return path;
-	}
-	else {
-		return list_get_instance(path_it, usb_hid_report_path_t, link);
-	}
-}
-
-/**
- * Initialize the report descriptor parser structure
- *
- * @param parser Report descriptor parser structure
- * @return Error code
- */
-int usb_hid_report_init(usb_hid_report_t *report)
-{
-	if(report == NULL) {
-		return EINVAL;
-	}
-
-	memset(report, 0, sizeof(usb_hid_report_t));
-	list_initialize(&report->reports);
-	list_initialize(&report->collection_paths);
-
-	report->use_report_ids = 0;
-    return EOK;   
-}
-
-
-/*
- *
- *
- */
-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;
+	}
 
 	for(i=0; i<report_item->usages_count; i++){
@@ -120,5 +104,5 @@
 	}
 
-	usb_hid_report_path_t *path = report_item->usage_path;	
+	
 	for(i=0; i<report_item->count; i++){
 
@@ -128,4 +112,5 @@
 
 		/* fill the attributes */		
+		field->collection_path = path;
 		field->logical_minimum = report_item->logical_minimum;
 		field->logical_maximum = report_item->logical_maximum;
@@ -144,9 +129,19 @@
 		if(report_item->usages_count > 0 && ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
 			uint32_t usage;
-			if(i < report_item->usages_count){
-				usage = report_item->usages[i];
+			if(report_item->type != USB_HID_REPORT_TYPE_OUTPUT) {
+				if(i < report_item->usages_count){
+					usage = report_item->usages[i];
+				}
+				else {
+					usage = report_item->usages[report_item->usages_count - 1];
+				}
 			}
 			else {
-				usage = report_item->usages[report_item->usages_count - 1];
+				if((report_item->count - i - 1) < report_item->usages_count){
+					usage = report_item->usages[(report_item->count - i - 1)];
+				}
+				else {
+					usage = report_item->usages[report_item->usages_count - 1];
+				}
 			}
 
@@ -164,18 +159,15 @@
 
 		if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) != 0) && (!((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0)))) {
-			field->usage = report_item->usage_minimum + i;					
-		}
-		
-		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);
-
+			if(report_item->type == USB_HID_REPORT_TYPE_INPUT) {
+				field->usage = report_item->usage_maximum - i;
+			}
+			else {
+				field->usage = report_item->usage_minimum + i;					
+			}
+
+		}
+		
 		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;
-
-		field->offset = 8 * offset_byte + offset_bit;
+		field->offset = report_item->offset + (i * report_item->size);
 		if(report_item->id != 0) {
 			field->offset += 8;
@@ -272,5 +264,4 @@
 		return ENOMEM;
 	}
-	usb_hid_report_path_append_item(usage_path, 0, 0);	
 	
 	while(i<size){	
@@ -358,6 +349,5 @@
 					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_set_last_item(usage_path, tmp_usage_path->usage_page, tmp_usage_path->usage);
 
 					usb_hid_report_path_free(report_item->usage_path);
@@ -437,7 +427,5 @@
 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;
-	
+{		
 	switch(tag)
 	{
@@ -450,15 +438,6 @@
 			
 		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, report_item->usage_page);
-			usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_LOCAL, 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]);	
+			// TODO usage_path->flags = *data;
+			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;
@@ -551,17 +530,9 @@
                              usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
 {
-	switch(tag) {
+	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:
-					report_item->usages[report_item->usages_count] = usb_hid_report_tag_data_uint32(data,item_size);
-					report_item->usages_count++;
-					break;
-			}
+			report_item->usages[report_item->usages_count] = usb_hid_report_tag_data_uint32(data,item_size);
+			report_item->usages_count++;
 			break;
 		case USB_HID_REPORT_TAG_USAGE_MINIMUM:
@@ -604,11 +575,13 @@
 			break;			
 		case USB_HID_REPORT_TAG_DELIMITER:
-			report_item->in_delimiter = usb_hid_report_tag_data_uint32(data,item_size);
-			break;
-
+			//report_item->delimiter = usb_hid_report_tag_data_uint32(data,item_size);
+			//TODO: 
+			//	DELIMITER STUFF
+			break;
+		
 		default:
 			return USB_HID_NO_ACTION;
 	}
-
+	
 	return EOK;
 }
@@ -656,5 +629,5 @@
 
 		usb_log_debug("\t\tOFFSET: %X\n", report_item->offset);
-		usb_log_debug("\t\tSIZE: %zu\n", report_item->size);				
+		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);		
@@ -667,8 +640,6 @@
 		usb_log_debug("\t\ttUSAGE: %X\n", report_item->usage);
 		usb_log_debug("\t\tUSAGE PAGE: %X\n", report_item->usage_page);
-		
-		//usb_hid_print_usage_path(report_item->collection_path);
-
-		usb_log_debug("\n");		
+						
+//		usb_log_debug("\n");		
 
 	}
@@ -695,6 +666,6 @@
 		usb_log_debug("Report ID: %d\n", report_des->report_id);
 		usb_log_debug("\tType: %d\n", report_des->type);
-		usb_log_debug("\tLength: %zu\n", report_des->bit_length);		
-		usb_log_debug("\tItems: %zu\n", report_des->item_length);		
+		usb_log_debug("\tLength: %zu\n", report_des->bit_length);
+		usb_log_debug("\tItems: %zu\n", report_des->item_length);
 
 		usb_hid_descriptor_print_list(&report_des->report_items);
