1dde5845aSJiri Kosina /* 2229695e5SJiri Kosina * HID support for Linux 3dde5845aSJiri Kosina * 4dde5845aSJiri Kosina * Copyright (c) 1999 Andreas Gal 5dde5845aSJiri Kosina * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz> 6dde5845aSJiri Kosina * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc 7dde5845aSJiri Kosina * Copyright (c) 2006 Jiri Kosina 8dde5845aSJiri Kosina */ 9dde5845aSJiri Kosina 10dde5845aSJiri Kosina /* 11dde5845aSJiri Kosina * This program is free software; you can redistribute it and/or modify it 12dde5845aSJiri Kosina * under the terms of the GNU General Public License as published by the Free 13dde5845aSJiri Kosina * Software Foundation; either version 2 of the License, or (at your option) 14dde5845aSJiri Kosina * any later version. 15dde5845aSJiri Kosina */ 16dde5845aSJiri Kosina 17dde5845aSJiri Kosina #include <linux/module.h> 18dde5845aSJiri Kosina #include <linux/slab.h> 19dde5845aSJiri Kosina #include <linux/init.h> 20dde5845aSJiri Kosina #include <linux/kernel.h> 21dde5845aSJiri Kosina #include <linux/list.h> 22dde5845aSJiri Kosina #include <linux/mm.h> 23dde5845aSJiri Kosina #include <linux/smp_lock.h> 24dde5845aSJiri Kosina #include <linux/spinlock.h> 25dde5845aSJiri Kosina #include <asm/unaligned.h> 26dde5845aSJiri Kosina #include <asm/byteorder.h> 27dde5845aSJiri Kosina #include <linux/input.h> 28dde5845aSJiri Kosina #include <linux/wait.h> 2947a80edbSJiri Kosina #include <linux/vmalloc.h> 30dde5845aSJiri Kosina 31dde5845aSJiri Kosina #include <linux/hid.h> 32dde5845aSJiri Kosina #include <linux/hiddev.h> 33c080d89aSJiri Kosina #include <linux/hid-debug.h> 34dde5845aSJiri Kosina 35dde5845aSJiri Kosina /* 36dde5845aSJiri Kosina * Version Information 37dde5845aSJiri Kosina */ 38dde5845aSJiri Kosina 39dde5845aSJiri Kosina #define DRIVER_VERSION "v2.6" 40dde5845aSJiri Kosina #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik" 4153149801SJiri Kosina #define DRIVER_DESC "HID core driver" 42dde5845aSJiri Kosina #define DRIVER_LICENSE "GPL" 43dde5845aSJiri Kosina 44dde5845aSJiri Kosina /* 45dde5845aSJiri Kosina * Register a new report for a device. 46dde5845aSJiri Kosina */ 47dde5845aSJiri Kosina 48dde5845aSJiri Kosina static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id) 49dde5845aSJiri Kosina { 50dde5845aSJiri Kosina struct hid_report_enum *report_enum = device->report_enum + type; 51dde5845aSJiri Kosina struct hid_report *report; 52dde5845aSJiri Kosina 53dde5845aSJiri Kosina if (report_enum->report_id_hash[id]) 54dde5845aSJiri Kosina return report_enum->report_id_hash[id]; 55dde5845aSJiri Kosina 56dde5845aSJiri Kosina if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL))) 57dde5845aSJiri Kosina return NULL; 58dde5845aSJiri Kosina 59dde5845aSJiri Kosina if (id != 0) 60dde5845aSJiri Kosina report_enum->numbered = 1; 61dde5845aSJiri Kosina 62dde5845aSJiri Kosina report->id = id; 63dde5845aSJiri Kosina report->type = type; 64dde5845aSJiri Kosina report->size = 0; 65dde5845aSJiri Kosina report->device = device; 66dde5845aSJiri Kosina report_enum->report_id_hash[id] = report; 67dde5845aSJiri Kosina 68dde5845aSJiri Kosina list_add_tail(&report->list, &report_enum->report_list); 69dde5845aSJiri Kosina 70dde5845aSJiri Kosina return report; 71dde5845aSJiri Kosina } 72dde5845aSJiri Kosina 73dde5845aSJiri Kosina /* 74dde5845aSJiri Kosina * Register a new field for this report. 75dde5845aSJiri Kosina */ 76dde5845aSJiri Kosina 77dde5845aSJiri Kosina static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values) 78dde5845aSJiri Kosina { 79dde5845aSJiri Kosina struct hid_field *field; 80dde5845aSJiri Kosina 81dde5845aSJiri Kosina if (report->maxfield == HID_MAX_FIELDS) { 82dde5845aSJiri Kosina dbg("too many fields in report"); 83dde5845aSJiri Kosina return NULL; 84dde5845aSJiri Kosina } 85dde5845aSJiri Kosina 86dde5845aSJiri Kosina if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage) 87dde5845aSJiri Kosina + values * sizeof(unsigned), GFP_KERNEL))) return NULL; 88dde5845aSJiri Kosina 89dde5845aSJiri Kosina field->index = report->maxfield++; 90dde5845aSJiri Kosina report->field[field->index] = field; 91dde5845aSJiri Kosina field->usage = (struct hid_usage *)(field + 1); 92dde5845aSJiri Kosina field->value = (unsigned *)(field->usage + usages); 93dde5845aSJiri Kosina field->report = report; 94dde5845aSJiri Kosina 95dde5845aSJiri Kosina return field; 96dde5845aSJiri Kosina } 97dde5845aSJiri Kosina 98dde5845aSJiri Kosina /* 99dde5845aSJiri Kosina * Open a collection. The type/usage is pushed on the stack. 100dde5845aSJiri Kosina */ 101dde5845aSJiri Kosina 102dde5845aSJiri Kosina static int open_collection(struct hid_parser *parser, unsigned type) 103dde5845aSJiri Kosina { 104dde5845aSJiri Kosina struct hid_collection *collection; 105dde5845aSJiri Kosina unsigned usage; 106dde5845aSJiri Kosina 107dde5845aSJiri Kosina usage = parser->local.usage[0]; 108dde5845aSJiri Kosina 109dde5845aSJiri Kosina if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) { 110dde5845aSJiri Kosina dbg("collection stack overflow"); 111dde5845aSJiri Kosina return -1; 112dde5845aSJiri Kosina } 113dde5845aSJiri Kosina 114dde5845aSJiri Kosina if (parser->device->maxcollection == parser->device->collection_size) { 115dde5845aSJiri Kosina collection = kmalloc(sizeof(struct hid_collection) * 116dde5845aSJiri Kosina parser->device->collection_size * 2, GFP_KERNEL); 117dde5845aSJiri Kosina if (collection == NULL) { 118dde5845aSJiri Kosina dbg("failed to reallocate collection array"); 119dde5845aSJiri Kosina return -1; 120dde5845aSJiri Kosina } 121dde5845aSJiri Kosina memcpy(collection, parser->device->collection, 122dde5845aSJiri Kosina sizeof(struct hid_collection) * 123dde5845aSJiri Kosina parser->device->collection_size); 124dde5845aSJiri Kosina memset(collection + parser->device->collection_size, 0, 125dde5845aSJiri Kosina sizeof(struct hid_collection) * 126dde5845aSJiri Kosina parser->device->collection_size); 127dde5845aSJiri Kosina kfree(parser->device->collection); 128dde5845aSJiri Kosina parser->device->collection = collection; 129dde5845aSJiri Kosina parser->device->collection_size *= 2; 130dde5845aSJiri Kosina } 131dde5845aSJiri Kosina 132dde5845aSJiri Kosina parser->collection_stack[parser->collection_stack_ptr++] = 133dde5845aSJiri Kosina parser->device->maxcollection; 134dde5845aSJiri Kosina 135dde5845aSJiri Kosina collection = parser->device->collection + 136dde5845aSJiri Kosina parser->device->maxcollection++; 137dde5845aSJiri Kosina collection->type = type; 138dde5845aSJiri Kosina collection->usage = usage; 139dde5845aSJiri Kosina collection->level = parser->collection_stack_ptr - 1; 140dde5845aSJiri Kosina 141dde5845aSJiri Kosina if (type == HID_COLLECTION_APPLICATION) 142dde5845aSJiri Kosina parser->device->maxapplication++; 143dde5845aSJiri Kosina 144dde5845aSJiri Kosina return 0; 145dde5845aSJiri Kosina } 146dde5845aSJiri Kosina 147dde5845aSJiri Kosina /* 148dde5845aSJiri Kosina * Close a collection. 149dde5845aSJiri Kosina */ 150dde5845aSJiri Kosina 151dde5845aSJiri Kosina static int close_collection(struct hid_parser *parser) 152dde5845aSJiri Kosina { 153dde5845aSJiri Kosina if (!parser->collection_stack_ptr) { 154dde5845aSJiri Kosina dbg("collection stack underflow"); 155dde5845aSJiri Kosina return -1; 156dde5845aSJiri Kosina } 157dde5845aSJiri Kosina parser->collection_stack_ptr--; 158dde5845aSJiri Kosina return 0; 159dde5845aSJiri Kosina } 160dde5845aSJiri Kosina 161dde5845aSJiri Kosina /* 162dde5845aSJiri Kosina * Climb up the stack, search for the specified collection type 163dde5845aSJiri Kosina * and return the usage. 164dde5845aSJiri Kosina */ 165dde5845aSJiri Kosina 166dde5845aSJiri Kosina static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type) 167dde5845aSJiri Kosina { 168dde5845aSJiri Kosina int n; 169dde5845aSJiri Kosina for (n = parser->collection_stack_ptr - 1; n >= 0; n--) 170dde5845aSJiri Kosina if (parser->device->collection[parser->collection_stack[n]].type == type) 171dde5845aSJiri Kosina return parser->device->collection[parser->collection_stack[n]].usage; 172dde5845aSJiri Kosina return 0; /* we know nothing about this usage type */ 173dde5845aSJiri Kosina } 174dde5845aSJiri Kosina 175dde5845aSJiri Kosina /* 176dde5845aSJiri Kosina * Add a usage to the temporary parser table. 177dde5845aSJiri Kosina */ 178dde5845aSJiri Kosina 179dde5845aSJiri Kosina static int hid_add_usage(struct hid_parser *parser, unsigned usage) 180dde5845aSJiri Kosina { 181dde5845aSJiri Kosina if (parser->local.usage_index >= HID_MAX_USAGES) { 182dde5845aSJiri Kosina dbg("usage index exceeded"); 183dde5845aSJiri Kosina return -1; 184dde5845aSJiri Kosina } 185dde5845aSJiri Kosina parser->local.usage[parser->local.usage_index] = usage; 186dde5845aSJiri Kosina parser->local.collection_index[parser->local.usage_index] = 187dde5845aSJiri Kosina parser->collection_stack_ptr ? 188dde5845aSJiri Kosina parser->collection_stack[parser->collection_stack_ptr - 1] : 0; 189dde5845aSJiri Kosina parser->local.usage_index++; 190dde5845aSJiri Kosina return 0; 191dde5845aSJiri Kosina } 192dde5845aSJiri Kosina 193dde5845aSJiri Kosina /* 194dde5845aSJiri Kosina * Register a new field for this report. 195dde5845aSJiri Kosina */ 196dde5845aSJiri Kosina 197dde5845aSJiri Kosina static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags) 198dde5845aSJiri Kosina { 199dde5845aSJiri Kosina struct hid_report *report; 200dde5845aSJiri Kosina struct hid_field *field; 201dde5845aSJiri Kosina int usages; 202dde5845aSJiri Kosina unsigned offset; 203dde5845aSJiri Kosina int i; 204dde5845aSJiri Kosina 205dde5845aSJiri Kosina if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) { 206dde5845aSJiri Kosina dbg("hid_register_report failed"); 207dde5845aSJiri Kosina return -1; 208dde5845aSJiri Kosina } 209dde5845aSJiri Kosina 210dde5845aSJiri Kosina if (parser->global.logical_maximum < parser->global.logical_minimum) { 211dde5845aSJiri Kosina dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum); 212dde5845aSJiri Kosina return -1; 213dde5845aSJiri Kosina } 214dde5845aSJiri Kosina 215dde5845aSJiri Kosina offset = report->size; 216dde5845aSJiri Kosina report->size += parser->global.report_size * parser->global.report_count; 217dde5845aSJiri Kosina 218dde5845aSJiri Kosina if (!parser->local.usage_index) /* Ignore padding fields */ 219dde5845aSJiri Kosina return 0; 220dde5845aSJiri Kosina 221dde5845aSJiri Kosina usages = max_t(int, parser->local.usage_index, parser->global.report_count); 222dde5845aSJiri Kosina 223dde5845aSJiri Kosina if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL) 224dde5845aSJiri Kosina return 0; 225dde5845aSJiri Kosina 226dde5845aSJiri Kosina field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL); 227dde5845aSJiri Kosina field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL); 228dde5845aSJiri Kosina field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION); 229dde5845aSJiri Kosina 230dde5845aSJiri Kosina for (i = 0; i < usages; i++) { 231dde5845aSJiri Kosina int j = i; 232dde5845aSJiri Kosina /* Duplicate the last usage we parsed if we have excess values */ 233dde5845aSJiri Kosina if (i >= parser->local.usage_index) 234dde5845aSJiri Kosina j = parser->local.usage_index - 1; 235dde5845aSJiri Kosina field->usage[i].hid = parser->local.usage[j]; 236dde5845aSJiri Kosina field->usage[i].collection_index = 237dde5845aSJiri Kosina parser->local.collection_index[j]; 238dde5845aSJiri Kosina } 239dde5845aSJiri Kosina 240dde5845aSJiri Kosina field->maxusage = usages; 241dde5845aSJiri Kosina field->flags = flags; 242dde5845aSJiri Kosina field->report_offset = offset; 243dde5845aSJiri Kosina field->report_type = report_type; 244dde5845aSJiri Kosina field->report_size = parser->global.report_size; 245dde5845aSJiri Kosina field->report_count = parser->global.report_count; 246dde5845aSJiri Kosina field->logical_minimum = parser->global.logical_minimum; 247dde5845aSJiri Kosina field->logical_maximum = parser->global.logical_maximum; 248dde5845aSJiri Kosina field->physical_minimum = parser->global.physical_minimum; 249dde5845aSJiri Kosina field->physical_maximum = parser->global.physical_maximum; 250dde5845aSJiri Kosina field->unit_exponent = parser->global.unit_exponent; 251dde5845aSJiri Kosina field->unit = parser->global.unit; 252dde5845aSJiri Kosina 253dde5845aSJiri Kosina return 0; 254dde5845aSJiri Kosina } 255dde5845aSJiri Kosina 256dde5845aSJiri Kosina /* 257dde5845aSJiri Kosina * Read data value from item. 258dde5845aSJiri Kosina */ 259dde5845aSJiri Kosina 260dde5845aSJiri Kosina static u32 item_udata(struct hid_item *item) 261dde5845aSJiri Kosina { 262dde5845aSJiri Kosina switch (item->size) { 263dde5845aSJiri Kosina case 1: return item->data.u8; 264dde5845aSJiri Kosina case 2: return item->data.u16; 265dde5845aSJiri Kosina case 4: return item->data.u32; 266dde5845aSJiri Kosina } 267dde5845aSJiri Kosina return 0; 268dde5845aSJiri Kosina } 269dde5845aSJiri Kosina 270dde5845aSJiri Kosina static s32 item_sdata(struct hid_item *item) 271dde5845aSJiri Kosina { 272dde5845aSJiri Kosina switch (item->size) { 273dde5845aSJiri Kosina case 1: return item->data.s8; 274dde5845aSJiri Kosina case 2: return item->data.s16; 275dde5845aSJiri Kosina case 4: return item->data.s32; 276dde5845aSJiri Kosina } 277dde5845aSJiri Kosina return 0; 278dde5845aSJiri Kosina } 279dde5845aSJiri Kosina 280dde5845aSJiri Kosina /* 281dde5845aSJiri Kosina * Process a global item. 282dde5845aSJiri Kosina */ 283dde5845aSJiri Kosina 284dde5845aSJiri Kosina static int hid_parser_global(struct hid_parser *parser, struct hid_item *item) 285dde5845aSJiri Kosina { 286dde5845aSJiri Kosina switch (item->tag) { 287dde5845aSJiri Kosina 288dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_PUSH: 289dde5845aSJiri Kosina 290dde5845aSJiri Kosina if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) { 291dde5845aSJiri Kosina dbg("global enviroment stack overflow"); 292dde5845aSJiri Kosina return -1; 293dde5845aSJiri Kosina } 294dde5845aSJiri Kosina 295dde5845aSJiri Kosina memcpy(parser->global_stack + parser->global_stack_ptr++, 296dde5845aSJiri Kosina &parser->global, sizeof(struct hid_global)); 297dde5845aSJiri Kosina return 0; 298dde5845aSJiri Kosina 299dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_POP: 300dde5845aSJiri Kosina 301dde5845aSJiri Kosina if (!parser->global_stack_ptr) { 302dde5845aSJiri Kosina dbg("global enviroment stack underflow"); 303dde5845aSJiri Kosina return -1; 304dde5845aSJiri Kosina } 305dde5845aSJiri Kosina 306dde5845aSJiri Kosina memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr, 307dde5845aSJiri Kosina sizeof(struct hid_global)); 308dde5845aSJiri Kosina return 0; 309dde5845aSJiri Kosina 310dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_USAGE_PAGE: 311dde5845aSJiri Kosina parser->global.usage_page = item_udata(item); 312dde5845aSJiri Kosina return 0; 313dde5845aSJiri Kosina 314dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM: 315dde5845aSJiri Kosina parser->global.logical_minimum = item_sdata(item); 316dde5845aSJiri Kosina return 0; 317dde5845aSJiri Kosina 318dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM: 319dde5845aSJiri Kosina if (parser->global.logical_minimum < 0) 320dde5845aSJiri Kosina parser->global.logical_maximum = item_sdata(item); 321dde5845aSJiri Kosina else 322dde5845aSJiri Kosina parser->global.logical_maximum = item_udata(item); 323dde5845aSJiri Kosina return 0; 324dde5845aSJiri Kosina 325dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM: 326dde5845aSJiri Kosina parser->global.physical_minimum = item_sdata(item); 327dde5845aSJiri Kosina return 0; 328dde5845aSJiri Kosina 329dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM: 330dde5845aSJiri Kosina if (parser->global.physical_minimum < 0) 331dde5845aSJiri Kosina parser->global.physical_maximum = item_sdata(item); 332dde5845aSJiri Kosina else 333dde5845aSJiri Kosina parser->global.physical_maximum = item_udata(item); 334dde5845aSJiri Kosina return 0; 335dde5845aSJiri Kosina 336dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT: 337dde5845aSJiri Kosina parser->global.unit_exponent = item_sdata(item); 338dde5845aSJiri Kosina return 0; 339dde5845aSJiri Kosina 340dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_UNIT: 341dde5845aSJiri Kosina parser->global.unit = item_udata(item); 342dde5845aSJiri Kosina return 0; 343dde5845aSJiri Kosina 344dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_REPORT_SIZE: 345dde5845aSJiri Kosina if ((parser->global.report_size = item_udata(item)) > 32) { 346dde5845aSJiri Kosina dbg("invalid report_size %d", parser->global.report_size); 347dde5845aSJiri Kosina return -1; 348dde5845aSJiri Kosina } 349dde5845aSJiri Kosina return 0; 350dde5845aSJiri Kosina 351dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_REPORT_COUNT: 352dde5845aSJiri Kosina if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) { 353dde5845aSJiri Kosina dbg("invalid report_count %d", parser->global.report_count); 354dde5845aSJiri Kosina return -1; 355dde5845aSJiri Kosina } 356dde5845aSJiri Kosina return 0; 357dde5845aSJiri Kosina 358dde5845aSJiri Kosina case HID_GLOBAL_ITEM_TAG_REPORT_ID: 359dde5845aSJiri Kosina if ((parser->global.report_id = item_udata(item)) == 0) { 360dde5845aSJiri Kosina dbg("report_id 0 is invalid"); 361dde5845aSJiri Kosina return -1; 362dde5845aSJiri Kosina } 363dde5845aSJiri Kosina return 0; 364dde5845aSJiri Kosina 365dde5845aSJiri Kosina default: 366dde5845aSJiri Kosina dbg("unknown global tag 0x%x", item->tag); 367dde5845aSJiri Kosina return -1; 368dde5845aSJiri Kosina } 369dde5845aSJiri Kosina } 370dde5845aSJiri Kosina 371dde5845aSJiri Kosina /* 372dde5845aSJiri Kosina * Process a local item. 373dde5845aSJiri Kosina */ 374dde5845aSJiri Kosina 375dde5845aSJiri Kosina static int hid_parser_local(struct hid_parser *parser, struct hid_item *item) 376dde5845aSJiri Kosina { 377dde5845aSJiri Kosina __u32 data; 378dde5845aSJiri Kosina unsigned n; 379dde5845aSJiri Kosina 380dde5845aSJiri Kosina if (item->size == 0) { 381dde5845aSJiri Kosina dbg("item data expected for local item"); 382dde5845aSJiri Kosina return -1; 383dde5845aSJiri Kosina } 384dde5845aSJiri Kosina 385dde5845aSJiri Kosina data = item_udata(item); 386dde5845aSJiri Kosina 387dde5845aSJiri Kosina switch (item->tag) { 388dde5845aSJiri Kosina 389dde5845aSJiri Kosina case HID_LOCAL_ITEM_TAG_DELIMITER: 390dde5845aSJiri Kosina 391dde5845aSJiri Kosina if (data) { 392dde5845aSJiri Kosina /* 393dde5845aSJiri Kosina * We treat items before the first delimiter 394dde5845aSJiri Kosina * as global to all usage sets (branch 0). 395dde5845aSJiri Kosina * In the moment we process only these global 396dde5845aSJiri Kosina * items and the first delimiter set. 397dde5845aSJiri Kosina */ 398dde5845aSJiri Kosina if (parser->local.delimiter_depth != 0) { 399dde5845aSJiri Kosina dbg("nested delimiters"); 400dde5845aSJiri Kosina return -1; 401dde5845aSJiri Kosina } 402dde5845aSJiri Kosina parser->local.delimiter_depth++; 403dde5845aSJiri Kosina parser->local.delimiter_branch++; 404dde5845aSJiri Kosina } else { 405dde5845aSJiri Kosina if (parser->local.delimiter_depth < 1) { 406dde5845aSJiri Kosina dbg("bogus close delimiter"); 407dde5845aSJiri Kosina return -1; 408dde5845aSJiri Kosina } 409dde5845aSJiri Kosina parser->local.delimiter_depth--; 410dde5845aSJiri Kosina } 411dde5845aSJiri Kosina return 1; 412dde5845aSJiri Kosina 413dde5845aSJiri Kosina case HID_LOCAL_ITEM_TAG_USAGE: 414dde5845aSJiri Kosina 415dde5845aSJiri Kosina if (parser->local.delimiter_branch > 1) { 416dde5845aSJiri Kosina dbg("alternative usage ignored"); 417dde5845aSJiri Kosina return 0; 418dde5845aSJiri Kosina } 419dde5845aSJiri Kosina 420dde5845aSJiri Kosina if (item->size <= 2) 421dde5845aSJiri Kosina data = (parser->global.usage_page << 16) + data; 422dde5845aSJiri Kosina 423dde5845aSJiri Kosina return hid_add_usage(parser, data); 424dde5845aSJiri Kosina 425dde5845aSJiri Kosina case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM: 426dde5845aSJiri Kosina 427dde5845aSJiri Kosina if (parser->local.delimiter_branch > 1) { 428dde5845aSJiri Kosina dbg("alternative usage ignored"); 429dde5845aSJiri Kosina return 0; 430dde5845aSJiri Kosina } 431dde5845aSJiri Kosina 432dde5845aSJiri Kosina if (item->size <= 2) 433dde5845aSJiri Kosina data = (parser->global.usage_page << 16) + data; 434dde5845aSJiri Kosina 435dde5845aSJiri Kosina parser->local.usage_minimum = data; 436dde5845aSJiri Kosina return 0; 437dde5845aSJiri Kosina 438dde5845aSJiri Kosina case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM: 439dde5845aSJiri Kosina 440dde5845aSJiri Kosina if (parser->local.delimiter_branch > 1) { 441dde5845aSJiri Kosina dbg("alternative usage ignored"); 442dde5845aSJiri Kosina return 0; 443dde5845aSJiri Kosina } 444dde5845aSJiri Kosina 445dde5845aSJiri Kosina if (item->size <= 2) 446dde5845aSJiri Kosina data = (parser->global.usage_page << 16) + data; 447dde5845aSJiri Kosina 448dde5845aSJiri Kosina for (n = parser->local.usage_minimum; n <= data; n++) 449dde5845aSJiri Kosina if (hid_add_usage(parser, n)) { 450dde5845aSJiri Kosina dbg("hid_add_usage failed\n"); 451dde5845aSJiri Kosina return -1; 452dde5845aSJiri Kosina } 453dde5845aSJiri Kosina return 0; 454dde5845aSJiri Kosina 455dde5845aSJiri Kosina default: 456dde5845aSJiri Kosina 457dde5845aSJiri Kosina dbg("unknown local item tag 0x%x", item->tag); 458dde5845aSJiri Kosina return 0; 459dde5845aSJiri Kosina } 460dde5845aSJiri Kosina return 0; 461dde5845aSJiri Kosina } 462dde5845aSJiri Kosina 463dde5845aSJiri Kosina /* 464dde5845aSJiri Kosina * Process a main item. 465dde5845aSJiri Kosina */ 466dde5845aSJiri Kosina 467dde5845aSJiri Kosina static int hid_parser_main(struct hid_parser *parser, struct hid_item *item) 468dde5845aSJiri Kosina { 469dde5845aSJiri Kosina __u32 data; 470dde5845aSJiri Kosina int ret; 471dde5845aSJiri Kosina 472dde5845aSJiri Kosina data = item_udata(item); 473dde5845aSJiri Kosina 474dde5845aSJiri Kosina switch (item->tag) { 475dde5845aSJiri Kosina case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION: 476dde5845aSJiri Kosina ret = open_collection(parser, data & 0xff); 477dde5845aSJiri Kosina break; 478dde5845aSJiri Kosina case HID_MAIN_ITEM_TAG_END_COLLECTION: 479dde5845aSJiri Kosina ret = close_collection(parser); 480dde5845aSJiri Kosina break; 481dde5845aSJiri Kosina case HID_MAIN_ITEM_TAG_INPUT: 482dde5845aSJiri Kosina ret = hid_add_field(parser, HID_INPUT_REPORT, data); 483dde5845aSJiri Kosina break; 484dde5845aSJiri Kosina case HID_MAIN_ITEM_TAG_OUTPUT: 485dde5845aSJiri Kosina ret = hid_add_field(parser, HID_OUTPUT_REPORT, data); 486dde5845aSJiri Kosina break; 487dde5845aSJiri Kosina case HID_MAIN_ITEM_TAG_FEATURE: 488dde5845aSJiri Kosina ret = hid_add_field(parser, HID_FEATURE_REPORT, data); 489dde5845aSJiri Kosina break; 490dde5845aSJiri Kosina default: 491dde5845aSJiri Kosina dbg("unknown main item tag 0x%x", item->tag); 492dde5845aSJiri Kosina ret = 0; 493dde5845aSJiri Kosina } 494dde5845aSJiri Kosina 495dde5845aSJiri Kosina memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */ 496dde5845aSJiri Kosina 497dde5845aSJiri Kosina return ret; 498dde5845aSJiri Kosina } 499dde5845aSJiri Kosina 500dde5845aSJiri Kosina /* 501dde5845aSJiri Kosina * Process a reserved item. 502dde5845aSJiri Kosina */ 503dde5845aSJiri Kosina 504dde5845aSJiri Kosina static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item) 505dde5845aSJiri Kosina { 506dde5845aSJiri Kosina dbg("reserved item type, tag 0x%x", item->tag); 507dde5845aSJiri Kosina return 0; 508dde5845aSJiri Kosina } 509dde5845aSJiri Kosina 510dde5845aSJiri Kosina /* 511dde5845aSJiri Kosina * Free a report and all registered fields. The field->usage and 512dde5845aSJiri Kosina * field->value table's are allocated behind the field, so we need 513dde5845aSJiri Kosina * only to free(field) itself. 514dde5845aSJiri Kosina */ 515dde5845aSJiri Kosina 516dde5845aSJiri Kosina static void hid_free_report(struct hid_report *report) 517dde5845aSJiri Kosina { 518dde5845aSJiri Kosina unsigned n; 519dde5845aSJiri Kosina 520dde5845aSJiri Kosina for (n = 0; n < report->maxfield; n++) 521dde5845aSJiri Kosina kfree(report->field[n]); 522dde5845aSJiri Kosina kfree(report); 523dde5845aSJiri Kosina } 524dde5845aSJiri Kosina 525dde5845aSJiri Kosina /* 526dde5845aSJiri Kosina * Free a device structure, all reports, and all fields. 527dde5845aSJiri Kosina */ 528dde5845aSJiri Kosina 529229695e5SJiri Kosina void hid_free_device(struct hid_device *device) 530dde5845aSJiri Kosina { 531dde5845aSJiri Kosina unsigned i,j; 532dde5845aSJiri Kosina 533dde5845aSJiri Kosina for (i = 0; i < HID_REPORT_TYPES; i++) { 534dde5845aSJiri Kosina struct hid_report_enum *report_enum = device->report_enum + i; 535dde5845aSJiri Kosina 536dde5845aSJiri Kosina for (j = 0; j < 256; j++) { 537dde5845aSJiri Kosina struct hid_report *report = report_enum->report_id_hash[j]; 538dde5845aSJiri Kosina if (report) 539dde5845aSJiri Kosina hid_free_report(report); 540dde5845aSJiri Kosina } 541dde5845aSJiri Kosina } 542dde5845aSJiri Kosina 543dde5845aSJiri Kosina kfree(device->rdesc); 544767fe787SJiri Kosina kfree(device->collection); 545dde5845aSJiri Kosina kfree(device); 546dde5845aSJiri Kosina } 547229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_free_device); 548dde5845aSJiri Kosina 549dde5845aSJiri Kosina /* 550dde5845aSJiri Kosina * Fetch a report description item from the data stream. We support long 551dde5845aSJiri Kosina * items, though they are not used yet. 552dde5845aSJiri Kosina */ 553dde5845aSJiri Kosina 554dde5845aSJiri Kosina static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item) 555dde5845aSJiri Kosina { 556dde5845aSJiri Kosina u8 b; 557dde5845aSJiri Kosina 558dde5845aSJiri Kosina if ((end - start) <= 0) 559dde5845aSJiri Kosina return NULL; 560dde5845aSJiri Kosina 561dde5845aSJiri Kosina b = *start++; 562dde5845aSJiri Kosina 563dde5845aSJiri Kosina item->type = (b >> 2) & 3; 564dde5845aSJiri Kosina item->tag = (b >> 4) & 15; 565dde5845aSJiri Kosina 566dde5845aSJiri Kosina if (item->tag == HID_ITEM_TAG_LONG) { 567dde5845aSJiri Kosina 568dde5845aSJiri Kosina item->format = HID_ITEM_FORMAT_LONG; 569dde5845aSJiri Kosina 570dde5845aSJiri Kosina if ((end - start) < 2) 571dde5845aSJiri Kosina return NULL; 572dde5845aSJiri Kosina 573dde5845aSJiri Kosina item->size = *start++; 574dde5845aSJiri Kosina item->tag = *start++; 575dde5845aSJiri Kosina 576dde5845aSJiri Kosina if ((end - start) < item->size) 577dde5845aSJiri Kosina return NULL; 578dde5845aSJiri Kosina 579dde5845aSJiri Kosina item->data.longdata = start; 580dde5845aSJiri Kosina start += item->size; 581dde5845aSJiri Kosina return start; 582dde5845aSJiri Kosina } 583dde5845aSJiri Kosina 584dde5845aSJiri Kosina item->format = HID_ITEM_FORMAT_SHORT; 585dde5845aSJiri Kosina item->size = b & 3; 586dde5845aSJiri Kosina 587dde5845aSJiri Kosina switch (item->size) { 588dde5845aSJiri Kosina 589dde5845aSJiri Kosina case 0: 590dde5845aSJiri Kosina return start; 591dde5845aSJiri Kosina 592dde5845aSJiri Kosina case 1: 593dde5845aSJiri Kosina if ((end - start) < 1) 594dde5845aSJiri Kosina return NULL; 595dde5845aSJiri Kosina item->data.u8 = *start++; 596dde5845aSJiri Kosina return start; 597dde5845aSJiri Kosina 598dde5845aSJiri Kosina case 2: 599dde5845aSJiri Kosina if ((end - start) < 2) 600dde5845aSJiri Kosina return NULL; 601dde5845aSJiri Kosina item->data.u16 = le16_to_cpu(get_unaligned((__le16*)start)); 602dde5845aSJiri Kosina start = (__u8 *)((__le16 *)start + 1); 603dde5845aSJiri Kosina return start; 604dde5845aSJiri Kosina 605dde5845aSJiri Kosina case 3: 606dde5845aSJiri Kosina item->size++; 607dde5845aSJiri Kosina if ((end - start) < 4) 608dde5845aSJiri Kosina return NULL; 609dde5845aSJiri Kosina item->data.u32 = le32_to_cpu(get_unaligned((__le32*)start)); 610dde5845aSJiri Kosina start = (__u8 *)((__le32 *)start + 1); 611dde5845aSJiri Kosina return start; 612dde5845aSJiri Kosina } 613dde5845aSJiri Kosina 614dde5845aSJiri Kosina return NULL; 615dde5845aSJiri Kosina } 616dde5845aSJiri Kosina 617dde5845aSJiri Kosina /* 618dde5845aSJiri Kosina * Parse a report description into a hid_device structure. Reports are 619dde5845aSJiri Kosina * enumerated, fields are attached to these reports. 620dde5845aSJiri Kosina */ 621dde5845aSJiri Kosina 622229695e5SJiri Kosina struct hid_device *hid_parse_report(__u8 *start, unsigned size) 623dde5845aSJiri Kosina { 624dde5845aSJiri Kosina struct hid_device *device; 625dde5845aSJiri Kosina struct hid_parser *parser; 626dde5845aSJiri Kosina struct hid_item item; 627dde5845aSJiri Kosina __u8 *end; 628dde5845aSJiri Kosina unsigned i; 629dde5845aSJiri Kosina static int (*dispatch_type[])(struct hid_parser *parser, 630dde5845aSJiri Kosina struct hid_item *item) = { 631dde5845aSJiri Kosina hid_parser_main, 632dde5845aSJiri Kosina hid_parser_global, 633dde5845aSJiri Kosina hid_parser_local, 634dde5845aSJiri Kosina hid_parser_reserved 635dde5845aSJiri Kosina }; 636dde5845aSJiri Kosina 637dde5845aSJiri Kosina if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL))) 638dde5845aSJiri Kosina return NULL; 639dde5845aSJiri Kosina 640dde5845aSJiri Kosina if (!(device->collection = kzalloc(sizeof(struct hid_collection) * 641dde5845aSJiri Kosina HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) { 642dde5845aSJiri Kosina kfree(device); 643dde5845aSJiri Kosina return NULL; 644dde5845aSJiri Kosina } 645dde5845aSJiri Kosina device->collection_size = HID_DEFAULT_NUM_COLLECTIONS; 646dde5845aSJiri Kosina 647dde5845aSJiri Kosina for (i = 0; i < HID_REPORT_TYPES; i++) 648dde5845aSJiri Kosina INIT_LIST_HEAD(&device->report_enum[i].report_list); 649dde5845aSJiri Kosina 650d6509c36SAhmed S. Darwish if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) { 651dde5845aSJiri Kosina kfree(device->collection); 652dde5845aSJiri Kosina kfree(device); 653dde5845aSJiri Kosina return NULL; 654dde5845aSJiri Kosina } 655dde5845aSJiri Kosina memcpy(device->rdesc, start, size); 656dde5845aSJiri Kosina device->rsize = size; 657dde5845aSJiri Kosina 65847a80edbSJiri Kosina if (!(parser = vmalloc(sizeof(struct hid_parser)))) { 659dde5845aSJiri Kosina kfree(device->rdesc); 660dde5845aSJiri Kosina kfree(device->collection); 661dde5845aSJiri Kosina kfree(device); 662dde5845aSJiri Kosina return NULL; 663dde5845aSJiri Kosina } 66447a80edbSJiri Kosina memset(parser, 0, sizeof(struct hid_parser)); 665dde5845aSJiri Kosina parser->device = device; 666dde5845aSJiri Kosina 667dde5845aSJiri Kosina end = start + size; 668dde5845aSJiri Kosina while ((start = fetch_item(start, end, &item)) != NULL) { 669dde5845aSJiri Kosina 670dde5845aSJiri Kosina if (item.format != HID_ITEM_FORMAT_SHORT) { 671dde5845aSJiri Kosina dbg("unexpected long global item"); 672dde5845aSJiri Kosina hid_free_device(device); 67347a80edbSJiri Kosina vfree(parser); 674dde5845aSJiri Kosina return NULL; 675dde5845aSJiri Kosina } 676dde5845aSJiri Kosina 677dde5845aSJiri Kosina if (dispatch_type[item.type](parser, &item)) { 678dde5845aSJiri Kosina dbg("item %u %u %u %u parsing failed\n", 679dde5845aSJiri Kosina item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag); 680dde5845aSJiri Kosina hid_free_device(device); 68147a80edbSJiri Kosina vfree(parser); 682dde5845aSJiri Kosina return NULL; 683dde5845aSJiri Kosina } 684dde5845aSJiri Kosina 685dde5845aSJiri Kosina if (start == end) { 686dde5845aSJiri Kosina if (parser->collection_stack_ptr) { 687dde5845aSJiri Kosina dbg("unbalanced collection at end of report description"); 688dde5845aSJiri Kosina hid_free_device(device); 68947a80edbSJiri Kosina vfree(parser); 690dde5845aSJiri Kosina return NULL; 691dde5845aSJiri Kosina } 692dde5845aSJiri Kosina if (parser->local.delimiter_depth) { 693dde5845aSJiri Kosina dbg("unbalanced delimiter at end of report description"); 694dde5845aSJiri Kosina hid_free_device(device); 69547a80edbSJiri Kosina vfree(parser); 696dde5845aSJiri Kosina return NULL; 697dde5845aSJiri Kosina } 69847a80edbSJiri Kosina vfree(parser); 699dde5845aSJiri Kosina return device; 700dde5845aSJiri Kosina } 701dde5845aSJiri Kosina } 702dde5845aSJiri Kosina 703dde5845aSJiri Kosina dbg("item fetching failed at offset %d\n", (int)(end - start)); 704dde5845aSJiri Kosina hid_free_device(device); 70547a80edbSJiri Kosina vfree(parser); 706dde5845aSJiri Kosina return NULL; 707dde5845aSJiri Kosina } 708229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_parse_report); 709dde5845aSJiri Kosina 710dde5845aSJiri Kosina /* 711dde5845aSJiri Kosina * Convert a signed n-bit integer to signed 32-bit integer. Common 712dde5845aSJiri Kosina * cases are done through the compiler, the screwed things has to be 713dde5845aSJiri Kosina * done by hand. 714dde5845aSJiri Kosina */ 715dde5845aSJiri Kosina 716dde5845aSJiri Kosina static s32 snto32(__u32 value, unsigned n) 717dde5845aSJiri Kosina { 718dde5845aSJiri Kosina switch (n) { 719dde5845aSJiri Kosina case 8: return ((__s8)value); 720dde5845aSJiri Kosina case 16: return ((__s16)value); 721dde5845aSJiri Kosina case 32: return ((__s32)value); 722dde5845aSJiri Kosina } 723dde5845aSJiri Kosina return value & (1 << (n - 1)) ? value | (-1 << n) : value; 724dde5845aSJiri Kosina } 725dde5845aSJiri Kosina 726dde5845aSJiri Kosina /* 727dde5845aSJiri Kosina * Convert a signed 32-bit integer to a signed n-bit integer. 728dde5845aSJiri Kosina */ 729dde5845aSJiri Kosina 730dde5845aSJiri Kosina static u32 s32ton(__s32 value, unsigned n) 731dde5845aSJiri Kosina { 732dde5845aSJiri Kosina s32 a = value >> (n - 1); 733dde5845aSJiri Kosina if (a && a != -1) 734dde5845aSJiri Kosina return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1; 735dde5845aSJiri Kosina return value & ((1 << n) - 1); 736dde5845aSJiri Kosina } 737dde5845aSJiri Kosina 738dde5845aSJiri Kosina /* 739dde5845aSJiri Kosina * Extract/implement a data field from/to a little endian report (bit array). 740dde5845aSJiri Kosina * 741dde5845aSJiri Kosina * Code sort-of follows HID spec: 742dde5845aSJiri Kosina * http://www.usb.org/developers/devclass_docs/HID1_11.pdf 743dde5845aSJiri Kosina * 744dde5845aSJiri Kosina * While the USB HID spec allows unlimited length bit fields in "report 745dde5845aSJiri Kosina * descriptors", most devices never use more than 16 bits. 746dde5845aSJiri Kosina * One model of UPS is claimed to report "LINEV" as a 32-bit field. 747dde5845aSJiri Kosina * Search linux-kernel and linux-usb-devel archives for "hid-core extract". 748dde5845aSJiri Kosina */ 749dde5845aSJiri Kosina 750dde5845aSJiri Kosina static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n) 751dde5845aSJiri Kosina { 752dde5845aSJiri Kosina u64 x; 753dde5845aSJiri Kosina 754dde5845aSJiri Kosina WARN_ON(n > 32); 755dde5845aSJiri Kosina 756dde5845aSJiri Kosina report += offset >> 3; /* adjust byte index */ 757dde5845aSJiri Kosina offset &= 7; /* now only need bit offset into one byte */ 758b87496aaSAl Viro x = le64_to_cpu(get_unaligned((__le64 *) report)); 759dde5845aSJiri Kosina x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */ 760dde5845aSJiri Kosina return (u32) x; 761dde5845aSJiri Kosina } 762dde5845aSJiri Kosina 763dde5845aSJiri Kosina /* 764dde5845aSJiri Kosina * "implement" : set bits in a little endian bit stream. 765dde5845aSJiri Kosina * Same concepts as "extract" (see comments above). 766dde5845aSJiri Kosina * The data mangled in the bit stream remains in little endian 767dde5845aSJiri Kosina * order the whole time. It make more sense to talk about 768dde5845aSJiri Kosina * endianness of register values by considering a register 769dde5845aSJiri Kosina * a "cached" copy of the little endiad bit stream. 770dde5845aSJiri Kosina */ 771dde5845aSJiri Kosina static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value) 772dde5845aSJiri Kosina { 773b87496aaSAl Viro __le64 x; 774dde5845aSJiri Kosina u64 m = (1ULL << n) - 1; 775dde5845aSJiri Kosina 776dde5845aSJiri Kosina WARN_ON(n > 32); 777dde5845aSJiri Kosina 778dde5845aSJiri Kosina WARN_ON(value > m); 779dde5845aSJiri Kosina value &= m; 780dde5845aSJiri Kosina 781dde5845aSJiri Kosina report += offset >> 3; 782dde5845aSJiri Kosina offset &= 7; 783dde5845aSJiri Kosina 784b87496aaSAl Viro x = get_unaligned((__le64 *)report); 785dde5845aSJiri Kosina x &= cpu_to_le64(~(m << offset)); 786dde5845aSJiri Kosina x |= cpu_to_le64(((u64) value) << offset); 787b87496aaSAl Viro put_unaligned(x, (__le64 *) report); 788dde5845aSJiri Kosina } 789dde5845aSJiri Kosina 790dde5845aSJiri Kosina /* 791dde5845aSJiri Kosina * Search an array for a value. 792dde5845aSJiri Kosina */ 793dde5845aSJiri Kosina 794dde5845aSJiri Kosina static __inline__ int search(__s32 *array, __s32 value, unsigned n) 795dde5845aSJiri Kosina { 796dde5845aSJiri Kosina while (n--) { 797dde5845aSJiri Kosina if (*array++ == value) 798dde5845aSJiri Kosina return 0; 799dde5845aSJiri Kosina } 800dde5845aSJiri Kosina return -1; 801dde5845aSJiri Kosina } 802dde5845aSJiri Kosina 803dde5845aSJiri Kosina static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt) 804dde5845aSJiri Kosina { 805dde5845aSJiri Kosina hid_dump_input(usage, value); 806dde5845aSJiri Kosina if (hid->claimed & HID_CLAIMED_INPUT) 807dde5845aSJiri Kosina hidinput_hid_event(hid, field, usage, value); 808aa938f79SJiri Kosina if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event) 809aa938f79SJiri Kosina hid->hiddev_hid_event(hid, field, usage, value); 810dde5845aSJiri Kosina } 811dde5845aSJiri Kosina 812dde5845aSJiri Kosina /* 813dde5845aSJiri Kosina * Analyse a received field, and fetch the data from it. The field 814dde5845aSJiri Kosina * content is stored for next report processing (we do differential 815dde5845aSJiri Kosina * reporting to the layer). 816dde5845aSJiri Kosina */ 817dde5845aSJiri Kosina 818229695e5SJiri Kosina void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data, int interrupt) 819dde5845aSJiri Kosina { 820dde5845aSJiri Kosina unsigned n; 821dde5845aSJiri Kosina unsigned count = field->report_count; 822dde5845aSJiri Kosina unsigned offset = field->report_offset; 823dde5845aSJiri Kosina unsigned size = field->report_size; 824dde5845aSJiri Kosina __s32 min = field->logical_minimum; 825dde5845aSJiri Kosina __s32 max = field->logical_maximum; 826dde5845aSJiri Kosina __s32 *value; 827dde5845aSJiri Kosina 828dde5845aSJiri Kosina if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC))) 829dde5845aSJiri Kosina return; 830dde5845aSJiri Kosina 831dde5845aSJiri Kosina for (n = 0; n < count; n++) { 832dde5845aSJiri Kosina 833dde5845aSJiri Kosina value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) : 834dde5845aSJiri Kosina extract(data, offset + n * size, size); 835dde5845aSJiri Kosina 836dde5845aSJiri Kosina if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */ 837dde5845aSJiri Kosina && value[n] >= min && value[n] <= max 838dde5845aSJiri Kosina && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1) 839dde5845aSJiri Kosina goto exit; 840dde5845aSJiri Kosina } 841dde5845aSJiri Kosina 842dde5845aSJiri Kosina for (n = 0; n < count; n++) { 843dde5845aSJiri Kosina 844dde5845aSJiri Kosina if (HID_MAIN_ITEM_VARIABLE & field->flags) { 845dde5845aSJiri Kosina hid_process_event(hid, field, &field->usage[n], value[n], interrupt); 846dde5845aSJiri Kosina continue; 847dde5845aSJiri Kosina } 848dde5845aSJiri Kosina 849dde5845aSJiri Kosina if (field->value[n] >= min && field->value[n] <= max 850dde5845aSJiri Kosina && field->usage[field->value[n] - min].hid 851dde5845aSJiri Kosina && search(value, field->value[n], count)) 852dde5845aSJiri Kosina hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt); 853dde5845aSJiri Kosina 854dde5845aSJiri Kosina if (value[n] >= min && value[n] <= max 855dde5845aSJiri Kosina && field->usage[value[n] - min].hid 856dde5845aSJiri Kosina && search(field->value, value[n], count)) 857dde5845aSJiri Kosina hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt); 858dde5845aSJiri Kosina } 859dde5845aSJiri Kosina 860dde5845aSJiri Kosina memcpy(field->value, value, count * sizeof(__s32)); 861dde5845aSJiri Kosina exit: 862dde5845aSJiri Kosina kfree(value); 863dde5845aSJiri Kosina } 864229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_input_field); 865dde5845aSJiri Kosina 866dde5845aSJiri Kosina /* 867dde5845aSJiri Kosina * Output the field into the report. 868dde5845aSJiri Kosina */ 869dde5845aSJiri Kosina 870dde5845aSJiri Kosina static void hid_output_field(struct hid_field *field, __u8 *data) 871dde5845aSJiri Kosina { 872dde5845aSJiri Kosina unsigned count = field->report_count; 873dde5845aSJiri Kosina unsigned offset = field->report_offset; 874dde5845aSJiri Kosina unsigned size = field->report_size; 875*46386b58SSimon Budig unsigned bitsused = offset + count * size; 876dde5845aSJiri Kosina unsigned n; 877dde5845aSJiri Kosina 878*46386b58SSimon Budig /* make sure the unused bits in the last byte are zeros */ 879*46386b58SSimon Budig if (count > 0 && size > 0 && (bitsused % 8) != 0) 880*46386b58SSimon Budig data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1; 881*46386b58SSimon Budig 882dde5845aSJiri Kosina for (n = 0; n < count; n++) { 883dde5845aSJiri Kosina if (field->logical_minimum < 0) /* signed values */ 884dde5845aSJiri Kosina implement(data, offset + n * size, size, s32ton(field->value[n], size)); 885dde5845aSJiri Kosina else /* unsigned values */ 886dde5845aSJiri Kosina implement(data, offset + n * size, size, field->value[n]); 887dde5845aSJiri Kosina } 888dde5845aSJiri Kosina } 889dde5845aSJiri Kosina 890dde5845aSJiri Kosina /* 891dde5845aSJiri Kosina * Create a report. 892dde5845aSJiri Kosina */ 893dde5845aSJiri Kosina 894229695e5SJiri Kosina void hid_output_report(struct hid_report *report, __u8 *data) 895dde5845aSJiri Kosina { 896dde5845aSJiri Kosina unsigned n; 897dde5845aSJiri Kosina 898dde5845aSJiri Kosina if (report->id > 0) 899dde5845aSJiri Kosina *data++ = report->id; 900dde5845aSJiri Kosina 901dde5845aSJiri Kosina for (n = 0; n < report->maxfield; n++) 902dde5845aSJiri Kosina hid_output_field(report->field[n], data); 903dde5845aSJiri Kosina } 904229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_output_report); 905dde5845aSJiri Kosina 906dde5845aSJiri Kosina /* 907dde5845aSJiri Kosina * Set a field value. The report this field belongs to has to be 908dde5845aSJiri Kosina * created and transferred to the device, to set this value in the 909dde5845aSJiri Kosina * device. 910dde5845aSJiri Kosina */ 911dde5845aSJiri Kosina 912dde5845aSJiri Kosina int hid_set_field(struct hid_field *field, unsigned offset, __s32 value) 913dde5845aSJiri Kosina { 914dde5845aSJiri Kosina unsigned size = field->report_size; 915dde5845aSJiri Kosina 916dde5845aSJiri Kosina hid_dump_input(field->usage + offset, value); 917dde5845aSJiri Kosina 918dde5845aSJiri Kosina if (offset >= field->report_count) { 919dde5845aSJiri Kosina dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count); 920dde5845aSJiri Kosina hid_dump_field(field, 8); 921dde5845aSJiri Kosina return -1; 922dde5845aSJiri Kosina } 923dde5845aSJiri Kosina if (field->logical_minimum < 0) { 924dde5845aSJiri Kosina if (value != snto32(s32ton(value, size), size)) { 925dde5845aSJiri Kosina dbg("value %d is out of range", value); 926dde5845aSJiri Kosina return -1; 927dde5845aSJiri Kosina } 928dde5845aSJiri Kosina } 929dde5845aSJiri Kosina field->value[offset] = value; 930dde5845aSJiri Kosina return 0; 931dde5845aSJiri Kosina } 932229695e5SJiri Kosina EXPORT_SYMBOL_GPL(hid_set_field); 933dde5845aSJiri Kosina 934aa8de2f0SJiri Kosina int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt) 935aa8de2f0SJiri Kosina { 936aa8de2f0SJiri Kosina struct hid_report_enum *report_enum = hid->report_enum + type; 937aa8de2f0SJiri Kosina struct hid_report *report; 938aa8de2f0SJiri Kosina int n, rsize; 939aa8de2f0SJiri Kosina 940aa8de2f0SJiri Kosina if (!hid) 941aa8de2f0SJiri Kosina return -ENODEV; 942aa8de2f0SJiri Kosina 943aa8de2f0SJiri Kosina if (!size) { 944aa8de2f0SJiri Kosina dbg("empty report"); 945aa8de2f0SJiri Kosina return -1; 946aa8de2f0SJiri Kosina } 947aa8de2f0SJiri Kosina 948dd64c151SJiri Kosina #ifdef CONFIG_HID_DEBUG 949e54dea69SJiri Kosina printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un"); 950aa8de2f0SJiri Kosina #endif 951aa8de2f0SJiri Kosina 952aa8de2f0SJiri Kosina n = 0; /* Normally report number is 0 */ 953aa8de2f0SJiri Kosina if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */ 954aa8de2f0SJiri Kosina n = *data++; 955aa8de2f0SJiri Kosina size--; 956aa8de2f0SJiri Kosina } 957aa8de2f0SJiri Kosina 958dd64c151SJiri Kosina #ifdef CONFIG_HID_DEBUG 959aa8de2f0SJiri Kosina { 960aa8de2f0SJiri Kosina int i; 961aa8de2f0SJiri Kosina printk(KERN_DEBUG __FILE__ ": report %d (size %u) = ", n, size); 962aa8de2f0SJiri Kosina for (i = 0; i < size; i++) 963aa8de2f0SJiri Kosina printk(" %02x", data[i]); 964aa8de2f0SJiri Kosina printk("\n"); 965aa8de2f0SJiri Kosina } 966aa8de2f0SJiri Kosina #endif 967aa8de2f0SJiri Kosina 968aa8de2f0SJiri Kosina if (!(report = report_enum->report_id_hash[n])) { 969aa8de2f0SJiri Kosina dbg("undefined report_id %d received", n); 970aa8de2f0SJiri Kosina return -1; 971aa8de2f0SJiri Kosina } 972aa8de2f0SJiri Kosina 973aa8de2f0SJiri Kosina rsize = ((report->size - 1) >> 3) + 1; 974aa8de2f0SJiri Kosina 975aa8de2f0SJiri Kosina if (size < rsize) { 976aa8de2f0SJiri Kosina dbg("report %d is too short, (%d < %d)", report->id, size, rsize); 977aa8de2f0SJiri Kosina return -1; 978aa8de2f0SJiri Kosina } 979aa8de2f0SJiri Kosina 980aa8de2f0SJiri Kosina if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event) 981aa8de2f0SJiri Kosina hid->hiddev_report_event(hid, report); 982aa8de2f0SJiri Kosina 983aa8de2f0SJiri Kosina for (n = 0; n < report->maxfield; n++) 984aa8de2f0SJiri Kosina hid_input_field(hid, report->field[n], data, interrupt); 985aa8de2f0SJiri Kosina 986aa8de2f0SJiri Kosina if (hid->claimed & HID_CLAIMED_INPUT) 987aa8de2f0SJiri Kosina hidinput_report_event(hid, report); 988aa8de2f0SJiri Kosina 989aa8de2f0SJiri Kosina return 0; 990aa8de2f0SJiri Kosina } 991aa8de2f0SJiri Kosina EXPORT_SYMBOL_GPL(hid_input_report); 992aa8de2f0SJiri Kosina 993aa938f79SJiri Kosina MODULE_LICENSE(DRIVER_LICENSE); 994aa938f79SJiri Kosina 995