hid-core.c 24 KB

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