hid-core.c 25 KB

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