hid-core.c 24 KB

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