hid-core.c 24 KB

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