hid-core.c 24 KB

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