hid-core.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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/sched.h>
  28. #include <linux/hid.h>
  29. #include <linux/hiddev.h>
  30. #include <linux/hid-debug.h>
  31. #include <linux/hidraw.h>
  32. /*
  33. * Version Information
  34. */
  35. #define DRIVER_VERSION "v2.6"
  36. #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
  37. #define DRIVER_DESC "HID core driver"
  38. #define DRIVER_LICENSE "GPL"
  39. #ifdef CONFIG_HID_DEBUG
  40. int hid_debug = 0;
  41. module_param_named(debug, hid_debug, int, 0600);
  42. MODULE_PARM_DESC(debug, "HID debugging (0=off, 1=probing info, 2=continuous data dumping)");
  43. EXPORT_SYMBOL_GPL(hid_debug);
  44. #endif
  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_hid("too many fields in report\n");
  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 = (s32 *)(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_hid("collection stack overflow\n");
  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_hid("failed to reallocate collection array\n");
  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_hid("collection stack underflow\n");
  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_hid("usage index exceeded\n");
  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("hid_register_report failed\n");
  177. return -1;
  178. }
  179. if (parser->global.logical_maximum < parser->global.logical_minimum) {
  180. dbg_hid("logical range invalid %d %d\n", 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_hid("global enviroment stack overflow\n");
  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_hid("global enviroment stack underflow\n");
  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_hid("invalid report_size %d\n", 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_hid("invalid report_count %d\n", 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_hid("report_id 0 is invalid\n");
  301. return -1;
  302. }
  303. return 0;
  304. default:
  305. dbg_hid("unknown global tag 0x%x\n", 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_hid("item data expected for local item\n");
  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_hid("nested delimiters\n");
  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_hid("bogus close delimiter\n");
  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_hid("alternative usage ignored\n");
  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_hid("alternative usage ignored\n");
  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_hid("alternative usage ignored\n");
  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("hid_add_usage failed\n");
  371. return -1;
  372. }
  373. return 0;
  374. default:
  375. dbg_hid("unknown local item tag 0x%x\n", 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_hid("unknown main item tag 0x%x\n", 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_hid("reserved item type, tag 0x%x\n", 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->collection);
  447. kfree(device);
  448. }
  449. EXPORT_SYMBOL_GPL(hid_free_device);
  450. /*
  451. * Fetch a report description item from the data stream. We support long
  452. * items, though they are not used yet.
  453. */
  454. static u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
  455. {
  456. u8 b;
  457. if ((end - start) <= 0)
  458. return NULL;
  459. b = *start++;
  460. item->type = (b >> 2) & 3;
  461. item->tag = (b >> 4) & 15;
  462. if (item->tag == HID_ITEM_TAG_LONG) {
  463. item->format = HID_ITEM_FORMAT_LONG;
  464. if ((end - start) < 2)
  465. return NULL;
  466. item->size = *start++;
  467. item->tag = *start++;
  468. if ((end - start) < item->size)
  469. return NULL;
  470. item->data.longdata = start;
  471. start += item->size;
  472. return start;
  473. }
  474. item->format = HID_ITEM_FORMAT_SHORT;
  475. item->size = b & 3;
  476. switch (item->size) {
  477. case 0:
  478. return start;
  479. case 1:
  480. if ((end - start) < 1)
  481. return NULL;
  482. item->data.u8 = *start++;
  483. return start;
  484. case 2:
  485. if ((end - start) < 2)
  486. return NULL;
  487. item->data.u16 = get_unaligned_le16(start);
  488. start = (__u8 *)((__le16 *)start + 1);
  489. return start;
  490. case 3:
  491. item->size++;
  492. if ((end - start) < 4)
  493. return NULL;
  494. item->data.u32 = get_unaligned_le32(start);
  495. start = (__u8 *)((__le32 *)start + 1);
  496. return start;
  497. }
  498. return NULL;
  499. }
  500. /*
  501. * Parse a report description into a hid_device structure. Reports are
  502. * enumerated, fields are attached to these reports.
  503. */
  504. struct hid_device *hid_parse_report(__u8 *start, unsigned size)
  505. {
  506. struct hid_device *device;
  507. struct hid_parser *parser;
  508. struct hid_item item;
  509. __u8 *end;
  510. unsigned i;
  511. static int (*dispatch_type[])(struct hid_parser *parser,
  512. struct hid_item *item) = {
  513. hid_parser_main,
  514. hid_parser_global,
  515. hid_parser_local,
  516. hid_parser_reserved
  517. };
  518. if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
  519. return NULL;
  520. if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
  521. HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
  522. kfree(device);
  523. return NULL;
  524. }
  525. device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
  526. for (i = 0; i < HID_REPORT_TYPES; i++)
  527. INIT_LIST_HEAD(&device->report_enum[i].report_list);
  528. if (!(device->rdesc = kmalloc(size, GFP_KERNEL))) {
  529. kfree(device->collection);
  530. kfree(device);
  531. return NULL;
  532. }
  533. memcpy(device->rdesc, start, size);
  534. device->rsize = size;
  535. if (!(parser = vmalloc(sizeof(struct hid_parser)))) {
  536. kfree(device->rdesc);
  537. kfree(device->collection);
  538. kfree(device);
  539. return NULL;
  540. }
  541. memset(parser, 0, sizeof(struct hid_parser));
  542. parser->device = device;
  543. end = start + size;
  544. while ((start = fetch_item(start, end, &item)) != NULL) {
  545. if (item.format != HID_ITEM_FORMAT_SHORT) {
  546. dbg_hid("unexpected long global item\n");
  547. hid_free_device(device);
  548. vfree(parser);
  549. return NULL;
  550. }
  551. if (dispatch_type[item.type](parser, &item)) {
  552. dbg_hid("item %u %u %u %u parsing failed\n",
  553. item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
  554. hid_free_device(device);
  555. vfree(parser);
  556. return NULL;
  557. }
  558. if (start == end) {
  559. if (parser->collection_stack_ptr) {
  560. dbg_hid("unbalanced collection at end of report description\n");
  561. hid_free_device(device);
  562. vfree(parser);
  563. return NULL;
  564. }
  565. if (parser->local.delimiter_depth) {
  566. dbg_hid("unbalanced delimiter at end of report description\n");
  567. hid_free_device(device);
  568. vfree(parser);
  569. return NULL;
  570. }
  571. vfree(parser);
  572. return device;
  573. }
  574. }
  575. dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
  576. hid_free_device(device);
  577. vfree(parser);
  578. return NULL;
  579. }
  580. EXPORT_SYMBOL_GPL(hid_parse_report);
  581. /*
  582. * Convert a signed n-bit integer to signed 32-bit integer. Common
  583. * cases are done through the compiler, the screwed things has to be
  584. * done by hand.
  585. */
  586. static s32 snto32(__u32 value, unsigned n)
  587. {
  588. switch (n) {
  589. case 8: return ((__s8)value);
  590. case 16: return ((__s16)value);
  591. case 32: return ((__s32)value);
  592. }
  593. return value & (1 << (n - 1)) ? value | (-1 << n) : value;
  594. }
  595. /*
  596. * Convert a signed 32-bit integer to a signed n-bit integer.
  597. */
  598. static u32 s32ton(__s32 value, unsigned n)
  599. {
  600. s32 a = value >> (n - 1);
  601. if (a && a != -1)
  602. return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
  603. return value & ((1 << n) - 1);
  604. }
  605. /*
  606. * Extract/implement a data field from/to a little endian report (bit array).
  607. *
  608. * Code sort-of follows HID spec:
  609. * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
  610. *
  611. * While the USB HID spec allows unlimited length bit fields in "report
  612. * descriptors", most devices never use more than 16 bits.
  613. * One model of UPS is claimed to report "LINEV" as a 32-bit field.
  614. * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
  615. */
  616. static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
  617. {
  618. u64 x;
  619. if (n > 32)
  620. printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
  621. n, current->comm);
  622. report += offset >> 3; /* adjust byte index */
  623. offset &= 7; /* now only need bit offset into one byte */
  624. x = get_unaligned_le64(report);
  625. x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
  626. return (u32) x;
  627. }
  628. /*
  629. * "implement" : set bits in a little endian bit stream.
  630. * Same concepts as "extract" (see comments above).
  631. * The data mangled in the bit stream remains in little endian
  632. * order the whole time. It make more sense to talk about
  633. * endianness of register values by considering a register
  634. * a "cached" copy of the little endiad bit stream.
  635. */
  636. static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
  637. {
  638. u64 x;
  639. u64 m = (1ULL << n) - 1;
  640. if (n > 32)
  641. printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
  642. n, current->comm);
  643. if (value > m)
  644. printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
  645. value, current->comm);
  646. WARN_ON(value > m);
  647. value &= m;
  648. report += offset >> 3;
  649. offset &= 7;
  650. x = get_unaligned_le64(report);
  651. x &= ~(m << offset);
  652. x |= ((u64)value) << offset;
  653. put_unaligned_le64(x, report);
  654. }
  655. /*
  656. * Search an array for a value.
  657. */
  658. static __inline__ int search(__s32 *array, __s32 value, unsigned n)
  659. {
  660. while (n--) {
  661. if (*array++ == value)
  662. return 0;
  663. }
  664. return -1;
  665. }
  666. static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, int interrupt)
  667. {
  668. hid_dump_input(usage, value);
  669. if (hid->claimed & HID_CLAIMED_INPUT)
  670. hidinput_hid_event(hid, field, usage, value);
  671. if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
  672. hid->hiddev_hid_event(hid, field, usage, value);
  673. }
  674. /*
  675. * Analyse a received field, and fetch the data from it. The field
  676. * content is stored for next report processing (we do differential
  677. * reporting to the layer).
  678. */
  679. static void hid_input_field(struct hid_device *hid, struct hid_field *field,
  680. __u8 *data, int interrupt)
  681. {
  682. unsigned n;
  683. unsigned count = field->report_count;
  684. unsigned offset = field->report_offset;
  685. unsigned size = field->report_size;
  686. __s32 min = field->logical_minimum;
  687. __s32 max = field->logical_maximum;
  688. __s32 *value;
  689. if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
  690. return;
  691. for (n = 0; n < count; n++) {
  692. value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
  693. extract(data, offset + n * size, size);
  694. if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
  695. && value[n] >= min && value[n] <= max
  696. && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
  697. goto exit;
  698. }
  699. for (n = 0; n < count; n++) {
  700. if (HID_MAIN_ITEM_VARIABLE & field->flags) {
  701. hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
  702. continue;
  703. }
  704. if (field->value[n] >= min && field->value[n] <= max
  705. && field->usage[field->value[n] - min].hid
  706. && search(value, field->value[n], count))
  707. hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
  708. if (value[n] >= min && value[n] <= max
  709. && field->usage[value[n] - min].hid
  710. && search(field->value, value[n], count))
  711. hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
  712. }
  713. memcpy(field->value, value, count * sizeof(__s32));
  714. exit:
  715. kfree(value);
  716. }
  717. /*
  718. * Output the field into the report.
  719. */
  720. static void hid_output_field(struct hid_field *field, __u8 *data)
  721. {
  722. unsigned count = field->report_count;
  723. unsigned offset = field->report_offset;
  724. unsigned size = field->report_size;
  725. unsigned bitsused = offset + count * size;
  726. unsigned n;
  727. /* make sure the unused bits in the last byte are zeros */
  728. if (count > 0 && size > 0 && (bitsused % 8) != 0)
  729. data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
  730. for (n = 0; n < count; n++) {
  731. if (field->logical_minimum < 0) /* signed values */
  732. implement(data, offset + n * size, size, s32ton(field->value[n], size));
  733. else /* unsigned values */
  734. implement(data, offset + n * size, size, field->value[n]);
  735. }
  736. }
  737. /*
  738. * Create a report.
  739. */
  740. void hid_output_report(struct hid_report *report, __u8 *data)
  741. {
  742. unsigned n;
  743. if (report->id > 0)
  744. *data++ = report->id;
  745. for (n = 0; n < report->maxfield; n++)
  746. hid_output_field(report->field[n], data);
  747. }
  748. EXPORT_SYMBOL_GPL(hid_output_report);
  749. /*
  750. * Set a field value. The report this field belongs to has to be
  751. * created and transferred to the device, to set this value in the
  752. * device.
  753. */
  754. int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
  755. {
  756. unsigned size = field->report_size;
  757. hid_dump_input(field->usage + offset, value);
  758. if (offset >= field->report_count) {
  759. dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
  760. hid_dump_field(field, 8);
  761. return -1;
  762. }
  763. if (field->logical_minimum < 0) {
  764. if (value != snto32(s32ton(value, size), size)) {
  765. dbg_hid("value %d is out of range\n", value);
  766. return -1;
  767. }
  768. }
  769. field->value[offset] = value;
  770. return 0;
  771. }
  772. EXPORT_SYMBOL_GPL(hid_set_field);
  773. int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
  774. {
  775. struct hid_report_enum *report_enum = hid->report_enum + type;
  776. struct hid_report *report;
  777. int n, rsize, i;
  778. if (!hid)
  779. return -ENODEV;
  780. if (!size) {
  781. dbg_hid("empty report\n");
  782. return -1;
  783. }
  784. dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
  785. n = 0; /* Normally report number is 0 */
  786. if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
  787. n = *data++;
  788. size--;
  789. }
  790. /* dump the report */
  791. dbg_hid("report %d (size %u) = ", n, size);
  792. for (i = 0; i < size; i++)
  793. dbg_hid_line(" %02x", data[i]);
  794. dbg_hid_line("\n");
  795. if (!(report = report_enum->report_id_hash[n])) {
  796. dbg_hid("undefined report_id %d received\n", n);
  797. return -1;
  798. }
  799. rsize = ((report->size - 1) >> 3) + 1;
  800. if (size < rsize) {
  801. dbg_hid("report %d is too short, (%d < %d)\n", report->id, size, rsize);
  802. memset(data + size, 0, rsize - size);
  803. }
  804. if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
  805. hid->hiddev_report_event(hid, report);
  806. if (hid->claimed & HID_CLAIMED_HIDRAW) {
  807. /* numbered reports need to be passed with the report num */
  808. if (report_enum->numbered)
  809. hidraw_report_event(hid, data - 1, size + 1);
  810. else
  811. hidraw_report_event(hid, data, size);
  812. }
  813. for (n = 0; n < report->maxfield; n++)
  814. hid_input_field(hid, report->field[n], data, interrupt);
  815. if (hid->claimed & HID_CLAIMED_INPUT)
  816. hidinput_report_event(hid, report);
  817. return 0;
  818. }
  819. EXPORT_SYMBOL_GPL(hid_input_report);
  820. static int __init hid_init(void)
  821. {
  822. return hidraw_init();
  823. }
  824. static void __exit hid_exit(void)
  825. {
  826. hidraw_exit();
  827. }
  828. module_init(hid_init);
  829. module_exit(hid_exit);
  830. MODULE_LICENSE(DRIVER_LICENSE);