ALSA: wss_lib: fix opti93x capture formats limitations
[safe/jmp/linux-2.6] / drivers / hid / hid-core.c
index 689ae16..426ac5a 100644 (file)
@@ -1,10 +1,10 @@
 /*
- *  USB HID support for Linux
+ *  HID support for Linux
  *
  *  Copyright (c) 1999 Andreas Gal
  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
- *  Copyright (c) 2006 Jiri Kosina
+ *  Copyright (c) 2006-2007 Jiri Kosina
  */
 
 /*
 #include <linux/slab.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
-#include <linux/sched.h>
 #include <linux/list.h>
 #include <linux/mm.h>
-#include <linux/smp_lock.h>
 #include <linux/spinlock.h>
 #include <asm/unaligned.h>
 #include <asm/byteorder.h>
 #include <linux/input.h>
 #include <linux/wait.h>
-
-#undef DEBUG
-#undef DEBUG_DATA
-
-#include <linux/usb.h>
+#include <linux/vmalloc.h>
+#include <linux/sched.h>
 
 #include <linux/hid.h>
 #include <linux/hiddev.h>
+#include <linux/hid-debug.h>
+#include <linux/hidraw.h>
 
 /*
  * Version Information
  */
 
 #define DRIVER_VERSION "v2.6"
-#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik"
-#define DRIVER_DESC "USB HID core driver"
+#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
+#define DRIVER_DESC "HID core driver"
 #define DRIVER_LICENSE "GPL"
 
-/*
- * Module parameters.
- */
-
-static unsigned int hid_mousepoll_interval;
-module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
-MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
+#ifdef CONFIG_HID_DEBUG
+int hid_debug = 0;
+module_param_named(debug, hid_debug, int, 0600);
+MODULE_PARM_DESC(debug, "HID debugging (0=off, 1=probing info, 2=continuous data dumping)");
+EXPORT_SYMBOL_GPL(hid_debug);
+#endif
 
 /*
  * Register a new report for a device.
@@ -91,7 +87,7 @@ static struct hid_field *hid_register_field(struct hid_report *report, unsigned
        struct hid_field *field;
 
        if (report->maxfield == HID_MAX_FIELDS) {
-               dbg("too many fields in report");
+               dbg_hid("too many fields in report\n");
                return NULL;
        }
 
@@ -101,7 +97,7 @@ static struct hid_field *hid_register_field(struct hid_report *report, unsigned
        field->index = report->maxfield++;
        report->field[field->index] = field;
        field->usage = (struct hid_usage *)(field + 1);
-       field->value = (unsigned *)(field->usage + usages);
+       field->value = (s32 *)(field->usage + usages);
        field->report = report;
 
        return field;
@@ -119,7 +115,7 @@ static int open_collection(struct hid_parser *parser, unsigned type)
        usage = parser->local.usage[0];
 
        if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
-               dbg("collection stack overflow");
+               dbg_hid("collection stack overflow\n");
                return -1;
        }
 
@@ -127,7 +123,7 @@ static int open_collection(struct hid_parser *parser, unsigned type)
                collection = kmalloc(sizeof(struct hid_collection) *
                                parser->device->collection_size * 2, GFP_KERNEL);
                if (collection == NULL) {
-                       dbg("failed to reallocate collection array");
+                       dbg_hid("failed to reallocate collection array\n");
                        return -1;
                }
                memcpy(collection, parser->device->collection,
@@ -163,7 +159,7 @@ static int open_collection(struct hid_parser *parser, unsigned type)
 static int close_collection(struct hid_parser *parser)
 {
        if (!parser->collection_stack_ptr) {
-               dbg("collection stack underflow");
+               dbg_hid("collection stack underflow\n");
                return -1;
        }
        parser->collection_stack_ptr--;
@@ -191,7 +187,7 @@ static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
 static int hid_add_usage(struct hid_parser *parser, unsigned usage)
 {
        if (parser->local.usage_index >= HID_MAX_USAGES) {
-               dbg("usage index exceeded");
+               dbg_hid("usage index exceeded\n");
                return -1;
        }
        parser->local.usage[parser->local.usage_index] = usage;
@@ -215,12 +211,12 @@ static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsign
        int i;
 
        if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
-               dbg("hid_register_report failed");
+               dbg_hid("hid_register_report failed\n");
                return -1;
        }
 
        if (parser->global.logical_maximum < parser->global.logical_minimum) {
-               dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
+               dbg_hid("logical range invalid %d %d\n", parser->global.logical_minimum, parser->global.logical_maximum);
                return -1;
        }
 
@@ -300,7 +296,7 @@ static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
                case HID_GLOBAL_ITEM_TAG_PUSH:
 
                        if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
-                               dbg("global enviroment stack overflow");
+                               dbg_hid("global enviroment stack overflow\n");
                                return -1;
                        }
 
@@ -311,7 +307,7 @@ static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
                case HID_GLOBAL_ITEM_TAG_POP:
 
                        if (!parser->global_stack_ptr) {
-                               dbg("global enviroment stack underflow");
+                               dbg_hid("global enviroment stack underflow\n");
                                return -1;
                        }
 
@@ -355,27 +351,27 @@ static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
 
                case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
                        if ((parser->global.report_size = item_udata(item)) > 32) {
-                               dbg("invalid report_size %d", parser->global.report_size);
+                               dbg_hid("invalid report_size %d\n", parser->global.report_size);
                                return -1;
                        }
                        return 0;
 
                case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
                        if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
-                               dbg("invalid report_count %d", parser->global.report_count);
+                               dbg_hid("invalid report_count %d\n", parser->global.report_count);
                                return -1;
                        }
                        return 0;
 
                case HID_GLOBAL_ITEM_TAG_REPORT_ID:
                        if ((parser->global.report_id = item_udata(item)) == 0) {
-                               dbg("report_id 0 is invalid");
+                               dbg_hid("report_id 0 is invalid\n");
                                return -1;
                        }
                        return 0;
 
                default:
-                       dbg("unknown global tag 0x%x", item->tag);
+                       dbg_hid("unknown global tag 0x%x\n", item->tag);
                        return -1;
        }
 }
@@ -390,7 +386,7 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
        unsigned n;
 
        if (item->size == 0) {
-               dbg("item data expected for local item");
+               dbg_hid("item data expected for local item\n");
                return -1;
        }
 
@@ -408,14 +404,14 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
                                 * items and the first delimiter set.
                                 */
                                if (parser->local.delimiter_depth != 0) {
-                                       dbg("nested delimiters");
+                                       dbg_hid("nested delimiters\n");
                                        return -1;
                                }
                                parser->local.delimiter_depth++;
                                parser->local.delimiter_branch++;
                        } else {
                                if (parser->local.delimiter_depth < 1) {
-                                       dbg("bogus close delimiter");
+                                       dbg_hid("bogus close delimiter\n");
                                        return -1;
                                }
                                parser->local.delimiter_depth--;
@@ -425,7 +421,7 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
                case HID_LOCAL_ITEM_TAG_USAGE:
 
                        if (parser->local.delimiter_branch > 1) {
-                               dbg("alternative usage ignored");
+                               dbg_hid("alternative usage ignored\n");
                                return 0;
                        }
 
@@ -437,7 +433,7 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
                case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
 
                        if (parser->local.delimiter_branch > 1) {
-                               dbg("alternative usage ignored");
+                               dbg_hid("alternative usage ignored\n");
                                return 0;
                        }
 
@@ -450,7 +446,7 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
                case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
 
                        if (parser->local.delimiter_branch > 1) {
-                               dbg("alternative usage ignored");
+                               dbg_hid("alternative usage ignored\n");
                                return 0;
                        }
 
@@ -459,14 +455,14 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
 
                        for (n = parser->local.usage_minimum; n <= data; n++)
                                if (hid_add_usage(parser, n)) {
-                                       dbg("hid_add_usage failed\n");
+                                       dbg_hid("hid_add_usage failed\n");
                                        return -1;
                                }
                        return 0;
 
                default:
 
-                       dbg("unknown local item tag 0x%x", item->tag);
+                       dbg_hid("unknown local item tag 0x%x\n", item->tag);
                        return 0;
        }
        return 0;
@@ -500,7 +496,7 @@ static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
                        ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
                        break;
                default:
-                       dbg("unknown main item tag 0x%x", item->tag);
+                       dbg_hid("unknown main item tag 0x%x\n", item->tag);
                        ret = 0;
        }
 
@@ -515,7 +511,7 @@ static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
 
 static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
 {
-       dbg("reserved item type, tag 0x%x", item->tag);
+       dbg_hid("reserved item type, tag 0x%x\n", item->tag);
        return 0;
 }
 
@@ -538,7 +534,7 @@ static void hid_free_report(struct hid_report *report)
  * Free a device structure, all reports, and all fields.
  */
 
-static void hid_free_device(struct hid_device *device)
+void hid_free_device(struct hid_device *device)
 {
        unsigned i,j;
 
@@ -553,8 +549,10 @@ static void hid_free_device(struct hid_device *device)
        }
 
        kfree(device->rdesc);
+       kfree(device->collection);
        kfree(device);
 }
+EXPORT_SYMBOL_GPL(hid_free_device);
 
 /*
  * Fetch a report description item from the data stream. We support long
@@ -608,7 +606,7 @@ static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
                case 2:
                        if ((end - start) < 2)
                                return NULL;
-                       item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start));
+                       item->data.u16 = get_unaligned_le16(start);
                        start = (__u8 *)((__le16 *)start + 1);
                        return start;
 
@@ -616,7 +614,7 @@ static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
                        item->size++;
                        if ((end - start) < 4)
                                return NULL;
-                       item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start));
+                       item->data.u32 = get_unaligned_le32(start);
                        start = (__u8 *)((__le32 *)start + 1);
                        return start;
        }
@@ -629,7 +627,7 @@ static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
  * enumerated, fields are attached to these reports.
  */
 
-static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
+struct hid_device *hid_parse_report(__u8 *start, unsigned size)
 {
        struct hid_device *device;
        struct hid_parser *parser;
@@ -657,7 +655,7 @@ static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
        for (i = 0; i < HID_REPORT_TYPES; i++)
                INIT_LIST_HEAD(&device->report_enum[i].report_list);
 
-       if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
+       if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) {
                kfree(device->collection);
                kfree(device);
                return NULL;
@@ -665,60 +663,57 @@ static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
        memcpy(device->rdesc, start, size);
        device->rsize = size;
 
-       if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
+       if (!(parser = vmalloc(sizeof(struct hid_parser)))) {
                kfree(device->rdesc);
                kfree(device->collection);
                kfree(device);
                return NULL;
        }
+       memset(parser, 0, sizeof(struct hid_parser));
        parser->device = device;
 
        end = start + size;
        while ((start = fetch_item(start, end, &item)) != NULL) {
 
                if (item.format != HID_ITEM_FORMAT_SHORT) {
-                       dbg("unexpected long global item");
-                       kfree(device->collection);
+                       dbg_hid("unexpected long global item\n");
                        hid_free_device(device);
-                       kfree(parser);
+                       vfree(parser);
                        return NULL;
                }
 
                if (dispatch_type[item.type](parser, &item)) {
-                       dbg("item %u %u %u %u parsing failed\n",
+                       dbg_hid("item %u %u %u %u parsing failed\n",
                                item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
-                       kfree(device->collection);
                        hid_free_device(device);
-                       kfree(parser);
+                       vfree(parser);
                        return NULL;
                }
 
                if (start == end) {
                        if (parser->collection_stack_ptr) {
-                               dbg("unbalanced collection at end of report description");
-                               kfree(device->collection);
+                               dbg_hid("unbalanced collection at end of report description\n");
                                hid_free_device(device);
-                               kfree(parser);
+                               vfree(parser);
                                return NULL;
                        }
                        if (parser->local.delimiter_depth) {
-                               dbg("unbalanced delimiter at end of report description");
-                               kfree(device->collection);
+                               dbg_hid("unbalanced delimiter at end of report description\n");
                                hid_free_device(device);
-                               kfree(parser);
+                               vfree(parser);
                                return NULL;
                        }
-                       kfree(parser);
+                       vfree(parser);
                        return device;
                }
        }
 
-       dbg("item fetching failed at offset %d\n", (int)(end - start));
-       kfree(device->collection);
+       dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
        hid_free_device(device);
-       kfree(parser);
+       vfree(parser);
        return NULL;
 }
+EXPORT_SYMBOL_GPL(hid_parse_report);
 
 /*
  * Convert a signed n-bit integer to signed 32-bit integer. Common
@@ -764,13 +759,14 @@ static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
 {
        u64 x;
 
-       WARN_ON(n > 32);
+       if (n > 32)
+               printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
+                               n, current->comm);
 
        report += offset >> 3;  /* adjust byte index */
-       offset &= 7;            /* now only need bit offset into one byte */
-       x = get_unaligned((u64 *) report);
-       x = le64_to_cpu(x);
-       x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
+       offset &= 7;            /* now only need bit offset into one byte */
+       x = get_unaligned_le64(report);
+       x = (x >> offset) & ((1ULL << n) - 1);  /* extract bit field */
        return (u32) x;
 }
 
@@ -787,18 +783,23 @@ static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u3
        u64 x;
        u64 m = (1ULL << n) - 1;
 
-       WARN_ON(n > 32);
+       if (n > 32)
+               printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
+                               n, current->comm);
 
+       if (value > m)
+               printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
+                               value, current->comm);
        WARN_ON(value > m);
        value &= m;
 
        report += offset >> 3;
        offset &= 7;
 
-       x = get_unaligned((u64 *)report);
-       x &= cpu_to_le64(~(m << offset));
-       x |= cpu_to_le64(((u64) value) << offset);
-       put_unaligned(x, (u64 *) report);
+       x = get_unaligned_le64(report);
+       x &= ~(m << offset);
+       x |= ((u64)value) << offset;
+       put_unaligned_le64(x, report);
 }
 
 /*
@@ -819,8 +820,8 @@ static void hid_process_event(struct hid_device *hid, struct hid_field *field, s
        hid_dump_input(usage, value);
        if (hid->claimed & HID_CLAIMED_INPUT)
                hidinput_hid_event(hid, field, usage, value);
-       if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt)
-               hiddev_hid_event(hid, field, usage, value);
+       if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
+               hid->hiddev_hid_event(hid, field, usage, value);
 }
 
 /*
@@ -829,7 +830,8 @@ static void hid_process_event(struct hid_device *hid, struct hid_field *field, s
  * reporting to the layer).
  */
 
-static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt)
+static void hid_input_field(struct hid_device *hid, struct hid_field *field,
+                           __u8 *data, int interrupt)
 {
        unsigned n;
        unsigned count = field->report_count;
@@ -876,7 +878,6 @@ exit:
        kfree(value);
 }
 
-
 /*
  * Output the field into the report.
  */
@@ -886,8 +887,13 @@ static void hid_output_field(struct hid_field *field, __u8 *data)
        unsigned count = field->report_count;
        unsigned offset = field->report_offset;
        unsigned size = field->report_size;
+       unsigned bitsused = offset + count * size;
        unsigned n;
 
+       /* make sure the unused bits in the last byte are zeros */
+       if (count > 0 && size > 0 && (bitsused % 8) != 0)
+               data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
+
        for (n = 0; n < count; n++) {
                if (field->logical_minimum < 0) /* signed values */
                        implement(data, offset + n * size, size, s32ton(field->value[n], size));
@@ -900,7 +906,7 @@ static void hid_output_field(struct hid_field *field, __u8 *data)
  * Create a report.
  */
 
-static void hid_output_report(struct hid_report *report, __u8 *data)
+void hid_output_report(struct hid_report *report, __u8 *data)
 {
        unsigned n;
 
@@ -910,6 +916,7 @@ static void hid_output_report(struct hid_report *report, __u8 *data)
        for (n = 0; n < report->maxfield; n++)
                hid_output_field(report->field[n], data);
 }
+EXPORT_SYMBOL_GPL(hid_output_report);
 
 /*
  * Set a field value. The report this field belongs to has to be
@@ -924,17 +931,93 @@ int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
        hid_dump_input(field->usage + offset, value);
 
        if (offset >= field->report_count) {
-               dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count);
+               dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
                hid_dump_field(field, 8);
                return -1;
        }
        if (field->logical_minimum < 0) {
                if (value != snto32(s32ton(value, size), size)) {
-                       dbg("value %d is out of range", value);
+                       dbg_hid("value %d is out of range\n", value);
                        return -1;
                }
        }
        field->value[offset] = value;
        return 0;
 }
+EXPORT_SYMBOL_GPL(hid_set_field);
+
+int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
+{
+       struct hid_report_enum *report_enum = hid->report_enum + type;
+       struct hid_report *report;
+       int n, rsize, i;
+
+       if (!hid)
+               return -ENODEV;
+
+       if (!size) {
+               dbg_hid("empty report\n");
+               return -1;
+       }
+
+       dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
+
+       n = 0;                          /* Normally report number is 0 */
+       if (report_enum->numbered) {    /* Device uses numbered reports, data[0] is report number */
+               n = *data++;
+               size--;
+       }
+
+       /* dump the report */
+       dbg_hid("report %d (size %u) = ", n, size);
+       for (i = 0; i < size; i++)
+               dbg_hid_line(" %02x", data[i]);
+       dbg_hid_line("\n");
+
+       if (!(report = report_enum->report_id_hash[n])) {
+               dbg_hid("undefined report_id %d received\n", n);
+               return -1;
+       }
+
+       rsize = ((report->size - 1) >> 3) + 1;
+
+       if (size < rsize) {
+               dbg_hid("report %d is too short, (%d < %d)\n", report->id, size, rsize);
+               memset(data + size, 0, rsize - size);
+       }
+
+       if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
+               hid->hiddev_report_event(hid, report);
+       if (hid->claimed & HID_CLAIMED_HIDRAW) {
+               /* numbered reports need to be passed with the report num */
+               if (report_enum->numbered)
+                       hidraw_report_event(hid, data - 1, size + 1);
+               else
+                       hidraw_report_event(hid, data, size);
+       }
+
+       for (n = 0; n < report->maxfield; n++)
+               hid_input_field(hid, report->field[n], data, interrupt);
+
+       if (hid->claimed & HID_CLAIMED_INPUT)
+               hidinput_report_event(hid, report);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(hid_input_report);
+
+static int __init hid_init(void)
+{
+       return hidraw_init();
+}
+
+static void __exit hid_exit(void)
+{
+       hidraw_exit();
+}
+
+module_init(hid_init);
+module_exit(hid_exit);
+
+MODULE_LICENSE(DRIVER_LICENSE);