hid-core.c 23 KB

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