hid-core.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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. static void hid_device_release(struct device *dev)
  435. {
  436. struct hid_device *device = container_of(dev, struct hid_device, dev);
  437. unsigned i, j;
  438. for (i = 0; i < HID_REPORT_TYPES; i++) {
  439. struct hid_report_enum *report_enum = device->report_enum + i;
  440. for (j = 0; j < 256; j++) {
  441. struct hid_report *report = report_enum->report_id_hash[j];
  442. if (report)
  443. hid_free_report(report);
  444. }
  445. }
  446. kfree(device->rdesc);
  447. kfree(device->collection);
  448. kfree(device);
  449. }
  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. * hid_parse_report - parse device report
  502. *
  503. * @device: hid device
  504. * @start: report start
  505. * @size: report size
  506. *
  507. * Parse a report description into a hid_device structure. Reports are
  508. * enumerated, fields are attached to these reports.
  509. * 0 returned on success, otherwise nonzero error value.
  510. */
  511. int hid_parse_report(struct hid_device *device, __u8 *start,
  512. unsigned size)
  513. {
  514. struct hid_parser *parser;
  515. struct hid_item item;
  516. __u8 *end;
  517. int ret;
  518. static int (*dispatch_type[])(struct hid_parser *parser,
  519. struct hid_item *item) = {
  520. hid_parser_main,
  521. hid_parser_global,
  522. hid_parser_local,
  523. hid_parser_reserved
  524. };
  525. device->rdesc = kmalloc(size, GFP_KERNEL);
  526. if (device->rdesc == NULL)
  527. return -ENOMEM;
  528. memcpy(device->rdesc, start, size);
  529. device->rsize = size;
  530. parser = vmalloc(sizeof(struct hid_parser));
  531. if (!parser) {
  532. ret = -ENOMEM;
  533. goto err;
  534. }
  535. memset(parser, 0, sizeof(struct hid_parser));
  536. parser->device = device;
  537. end = start + size;
  538. ret = -EINVAL;
  539. while ((start = fetch_item(start, end, &item)) != NULL) {
  540. if (item.format != HID_ITEM_FORMAT_SHORT) {
  541. dbg_hid("unexpected long global item\n");
  542. goto err;
  543. }
  544. if (dispatch_type[item.type](parser, &item)) {
  545. dbg_hid("item %u %u %u %u parsing failed\n",
  546. item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
  547. goto err;
  548. }
  549. if (start == end) {
  550. if (parser->collection_stack_ptr) {
  551. dbg_hid("unbalanced collection at end of report description\n");
  552. goto err;
  553. }
  554. if (parser->local.delimiter_depth) {
  555. dbg_hid("unbalanced delimiter at end of report description\n");
  556. goto err;
  557. }
  558. vfree(parser);
  559. return 0;
  560. }
  561. }
  562. dbg_hid("item fetching failed at offset %d\n", (int)(end - start));
  563. err:
  564. vfree(parser);
  565. return ret;
  566. }
  567. EXPORT_SYMBOL_GPL(hid_parse_report);
  568. /*
  569. * Convert a signed n-bit integer to signed 32-bit integer. Common
  570. * cases are done through the compiler, the screwed things has to be
  571. * done by hand.
  572. */
  573. static s32 snto32(__u32 value, unsigned n)
  574. {
  575. switch (n) {
  576. case 8: return ((__s8)value);
  577. case 16: return ((__s16)value);
  578. case 32: return ((__s32)value);
  579. }
  580. return value & (1 << (n - 1)) ? value | (-1 << n) : value;
  581. }
  582. /*
  583. * Convert a signed 32-bit integer to a signed n-bit integer.
  584. */
  585. static u32 s32ton(__s32 value, unsigned n)
  586. {
  587. s32 a = value >> (n - 1);
  588. if (a && a != -1)
  589. return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
  590. return value & ((1 << n) - 1);
  591. }
  592. /*
  593. * Extract/implement a data field from/to a little endian report (bit array).
  594. *
  595. * Code sort-of follows HID spec:
  596. * http://www.usb.org/developers/devclass_docs/HID1_11.pdf
  597. *
  598. * While the USB HID spec allows unlimited length bit fields in "report
  599. * descriptors", most devices never use more than 16 bits.
  600. * One model of UPS is claimed to report "LINEV" as a 32-bit field.
  601. * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
  602. */
  603. static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
  604. {
  605. u64 x;
  606. if (n > 32)
  607. printk(KERN_WARNING "HID: extract() called with n (%d) > 32! (%s)\n",
  608. n, current->comm);
  609. report += offset >> 3; /* adjust byte index */
  610. offset &= 7; /* now only need bit offset into one byte */
  611. x = get_unaligned_le64(report);
  612. x = (x >> offset) & ((1ULL << n) - 1); /* extract bit field */
  613. return (u32) x;
  614. }
  615. /*
  616. * "implement" : set bits in a little endian bit stream.
  617. * Same concepts as "extract" (see comments above).
  618. * The data mangled in the bit stream remains in little endian
  619. * order the whole time. It make more sense to talk about
  620. * endianness of register values by considering a register
  621. * a "cached" copy of the little endiad bit stream.
  622. */
  623. static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
  624. {
  625. u64 x;
  626. u64 m = (1ULL << n) - 1;
  627. if (n > 32)
  628. printk(KERN_WARNING "HID: implement() called with n (%d) > 32! (%s)\n",
  629. n, current->comm);
  630. if (value > m)
  631. printk(KERN_WARNING "HID: implement() called with too large value %d! (%s)\n",
  632. value, current->comm);
  633. WARN_ON(value > m);
  634. value &= m;
  635. report += offset >> 3;
  636. offset &= 7;
  637. x = get_unaligned_le64(report);
  638. x &= ~(m << offset);
  639. x |= ((u64)value) << offset;
  640. put_unaligned_le64(x, report);
  641. }
  642. /*
  643. * Search an array for a value.
  644. */
  645. static __inline__ int search(__s32 *array, __s32 value, unsigned n)
  646. {
  647. while (n--) {
  648. if (*array++ == value)
  649. return 0;
  650. }
  651. return -1;
  652. }
  653. /**
  654. * hid_match_report - check if driver's raw_event should be called
  655. *
  656. * @hid: hid device
  657. * @report_type: type to match against
  658. *
  659. * compare hid->driver->report_table->report_type to report->type
  660. */
  661. static int hid_match_report(struct hid_device *hid, struct hid_report *report)
  662. {
  663. const struct hid_report_id *id = hid->driver->report_table;
  664. if (!id) /* NULL means all */
  665. return 1;
  666. for (; id->report_type != HID_TERMINATOR; id++)
  667. if (id->report_type == HID_ANY_ID ||
  668. id->report_type == report->type)
  669. return 1;
  670. return 0;
  671. }
  672. /**
  673. * hid_match_usage - check if driver's event should be called
  674. *
  675. * @hid: hid device
  676. * @usage: usage to match against
  677. *
  678. * compare hid->driver->usage_table->usage_{type,code} to
  679. * usage->usage_{type,code}
  680. */
  681. static int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
  682. {
  683. const struct hid_usage_id *id = hid->driver->usage_table;
  684. if (!id) /* NULL means all */
  685. return 1;
  686. for (; id->usage_type != HID_ANY_ID - 1; id++)
  687. if ((id->usage_hid == HID_ANY_ID ||
  688. id->usage_hid == usage->hid) &&
  689. (id->usage_type == HID_ANY_ID ||
  690. id->usage_type == usage->type) &&
  691. (id->usage_code == HID_ANY_ID ||
  692. id->usage_code == usage->code))
  693. return 1;
  694. return 0;
  695. }
  696. static void hid_process_event(struct hid_device *hid, struct hid_field *field,
  697. struct hid_usage *usage, __s32 value, int interrupt)
  698. {
  699. struct hid_driver *hdrv = hid->driver;
  700. int ret;
  701. hid_dump_input(usage, value);
  702. if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
  703. ret = hdrv->event(hid, field, usage, value);
  704. if (ret != 0) {
  705. if (ret < 0)
  706. dbg_hid("%s's event failed with %d\n",
  707. hdrv->name, ret);
  708. return;
  709. }
  710. }
  711. if (hid->claimed & HID_CLAIMED_INPUT)
  712. hidinput_hid_event(hid, field, usage, value);
  713. if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
  714. hid->hiddev_hid_event(hid, field, usage, value);
  715. }
  716. /*
  717. * Analyse a received field, and fetch the data from it. The field
  718. * content is stored for next report processing (we do differential
  719. * reporting to the layer).
  720. */
  721. static void hid_input_field(struct hid_device *hid, struct hid_field *field,
  722. __u8 *data, int interrupt)
  723. {
  724. unsigned n;
  725. unsigned count = field->report_count;
  726. unsigned offset = field->report_offset;
  727. unsigned size = field->report_size;
  728. __s32 min = field->logical_minimum;
  729. __s32 max = field->logical_maximum;
  730. __s32 *value;
  731. if (!(value = kmalloc(sizeof(__s32) * count, GFP_ATOMIC)))
  732. return;
  733. for (n = 0; n < count; n++) {
  734. value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
  735. extract(data, offset + n * size, size);
  736. if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
  737. && value[n] >= min && value[n] <= max
  738. && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
  739. goto exit;
  740. }
  741. for (n = 0; n < count; n++) {
  742. if (HID_MAIN_ITEM_VARIABLE & field->flags) {
  743. hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
  744. continue;
  745. }
  746. if (field->value[n] >= min && field->value[n] <= max
  747. && field->usage[field->value[n] - min].hid
  748. && search(value, field->value[n], count))
  749. hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
  750. if (value[n] >= min && value[n] <= max
  751. && field->usage[value[n] - min].hid
  752. && search(field->value, value[n], count))
  753. hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
  754. }
  755. memcpy(field->value, value, count * sizeof(__s32));
  756. exit:
  757. kfree(value);
  758. }
  759. /*
  760. * Output the field into the report.
  761. */
  762. static void hid_output_field(struct hid_field *field, __u8 *data)
  763. {
  764. unsigned count = field->report_count;
  765. unsigned offset = field->report_offset;
  766. unsigned size = field->report_size;
  767. unsigned bitsused = offset + count * size;
  768. unsigned n;
  769. /* make sure the unused bits in the last byte are zeros */
  770. if (count > 0 && size > 0 && (bitsused % 8) != 0)
  771. data[(bitsused-1)/8] &= (1 << (bitsused % 8)) - 1;
  772. for (n = 0; n < count; n++) {
  773. if (field->logical_minimum < 0) /* signed values */
  774. implement(data, offset + n * size, size, s32ton(field->value[n], size));
  775. else /* unsigned values */
  776. implement(data, offset + n * size, size, field->value[n]);
  777. }
  778. }
  779. /*
  780. * Create a report.
  781. */
  782. void hid_output_report(struct hid_report *report, __u8 *data)
  783. {
  784. unsigned n;
  785. if (report->id > 0)
  786. *data++ = report->id;
  787. for (n = 0; n < report->maxfield; n++)
  788. hid_output_field(report->field[n], data);
  789. }
  790. EXPORT_SYMBOL_GPL(hid_output_report);
  791. /*
  792. * Set a field value. The report this field belongs to has to be
  793. * created and transferred to the device, to set this value in the
  794. * device.
  795. */
  796. int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
  797. {
  798. unsigned size = field->report_size;
  799. hid_dump_input(field->usage + offset, value);
  800. if (offset >= field->report_count) {
  801. dbg_hid("offset (%d) exceeds report_count (%d)\n", offset, field->report_count);
  802. hid_dump_field(field, 8);
  803. return -1;
  804. }
  805. if (field->logical_minimum < 0) {
  806. if (value != snto32(s32ton(value, size), size)) {
  807. dbg_hid("value %d is out of range\n", value);
  808. return -1;
  809. }
  810. }
  811. field->value[offset] = value;
  812. return 0;
  813. }
  814. EXPORT_SYMBOL_GPL(hid_set_field);
  815. static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
  816. const u8 *data)
  817. {
  818. struct hid_report *report;
  819. unsigned int n = 0; /* Normally report number is 0 */
  820. /* Device uses numbered reports, data[0] is report number */
  821. if (report_enum->numbered)
  822. n = *data;
  823. report = report_enum->report_id_hash[n];
  824. if (report == NULL)
  825. dbg_hid("undefined report_id %u received\n", n);
  826. return report;
  827. }
  828. void hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
  829. int interrupt)
  830. {
  831. struct hid_report_enum *report_enum = hid->report_enum + type;
  832. struct hid_report *report;
  833. unsigned int a;
  834. int rsize, csize = size;
  835. u8 *cdata = data;
  836. report = hid_get_report(report_enum, data);
  837. if (!report)
  838. return;
  839. if (report_enum->numbered) {
  840. cdata++;
  841. csize--;
  842. }
  843. rsize = ((report->size - 1) >> 3) + 1;
  844. if (csize < rsize) {
  845. dbg_hid("report %d is too short, (%d < %d)\n", report->id,
  846. csize, rsize);
  847. memset(cdata + csize, 0, rsize - csize);
  848. }
  849. if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
  850. hid->hiddev_report_event(hid, report);
  851. if (hid->claimed & HID_CLAIMED_HIDRAW) {
  852. /* numbered reports need to be passed with the report num */
  853. if (report_enum->numbered)
  854. hidraw_report_event(hid, data - 1, size + 1);
  855. else
  856. hidraw_report_event(hid, data, size);
  857. }
  858. for (a = 0; a < report->maxfield; a++)
  859. hid_input_field(hid, report->field[a], cdata, interrupt);
  860. if (hid->claimed & HID_CLAIMED_INPUT)
  861. hidinput_report_event(hid, report);
  862. }
  863. EXPORT_SYMBOL_GPL(hid_report_raw_event);
  864. /**
  865. * hid_input_report - report data from lower layer (usb, bt...)
  866. *
  867. * @hid: hid device
  868. * @type: HID report type (HID_*_REPORT)
  869. * @data: report contents
  870. * @size: size of data parameter
  871. * @interrupt: called from atomic?
  872. *
  873. * This is data entry for lower layers.
  874. */
  875. int hid_input_report(struct hid_device *hid, int type, u8 *data, int size, int interrupt)
  876. {
  877. struct hid_report_enum *report_enum = hid->report_enum + type;
  878. struct hid_driver *hdrv = hid->driver;
  879. struct hid_report *report;
  880. unsigned int i;
  881. int ret;
  882. if (!hid || !hid->driver)
  883. return -ENODEV;
  884. if (!size) {
  885. dbg_hid("empty report\n");
  886. return -1;
  887. }
  888. dbg_hid("report (size %u) (%snumbered)\n", size, report_enum->numbered ? "" : "un");
  889. report = hid_get_report(report_enum, data);
  890. if (!report)
  891. return -1;
  892. /* dump the report */
  893. dbg_hid("report %d (size %u) = ", report->id, size);
  894. for (i = 0; i < size; i++)
  895. dbg_hid_line(" %02x", data[i]);
  896. dbg_hid_line("\n");
  897. if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
  898. ret = hdrv->raw_event(hid, report, data, size);
  899. if (ret != 0)
  900. return ret < 0 ? ret : 0;
  901. }
  902. hid_report_raw_event(hid, type, data, size, interrupt);
  903. return 0;
  904. }
  905. EXPORT_SYMBOL_GPL(hid_input_report);
  906. static bool hid_match_one_id(struct hid_device *hdev,
  907. const struct hid_device_id *id)
  908. {
  909. return id->bus == hdev->bus &&
  910. (id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
  911. (id->product == HID_ANY_ID || id->product == hdev->product);
  912. }
  913. static const struct hid_device_id *hid_match_id(struct hid_device *hdev,
  914. const struct hid_device_id *id)
  915. {
  916. for (; id->bus; id++)
  917. if (hid_match_one_id(hdev, id))
  918. return id;
  919. return NULL;
  920. }
  921. static const struct hid_device_id hid_blacklist[] = {
  922. { }
  923. };
  924. static int hid_bus_match(struct device *dev, struct device_driver *drv)
  925. {
  926. struct hid_driver *hdrv = container_of(drv, struct hid_driver, driver);
  927. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  928. if (!hid_match_id(hdev, hdrv->id_table))
  929. return 0;
  930. /* generic wants all non-blacklisted */
  931. if (!strncmp(hdrv->name, "generic-", 8))
  932. return !hid_match_id(hdev, hid_blacklist);
  933. return 1;
  934. }
  935. static int hid_device_probe(struct device *dev)
  936. {
  937. struct hid_driver *hdrv = container_of(dev->driver,
  938. struct hid_driver, driver);
  939. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  940. const struct hid_device_id *id;
  941. int ret = 0;
  942. if (!hdev->driver) {
  943. if (hdrv->probe) {
  944. ret = -ENODEV;
  945. id = hid_match_id(hdev, hdrv->id_table);
  946. if (id)
  947. ret = hdrv->probe(hdev, id);
  948. }
  949. if (!ret)
  950. hdev->driver = hdrv;
  951. }
  952. return ret;
  953. }
  954. static int hid_device_remove(struct device *dev)
  955. {
  956. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  957. struct hid_driver *hdrv = hdev->driver;
  958. if (hdrv) {
  959. if (hdrv->remove)
  960. hdrv->remove(hdev);
  961. hdev->driver = NULL;
  962. }
  963. return 0;
  964. }
  965. static int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
  966. {
  967. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  968. if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
  969. hdev->bus, hdev->vendor, hdev->product))
  970. return -ENOMEM;
  971. if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
  972. return -ENOMEM;
  973. if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
  974. return -ENOMEM;
  975. if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
  976. return -ENOMEM;
  977. if (add_uevent_var(env, "MODALIAS=hid:b%04Xv%08Xp%08X",
  978. hdev->bus, hdev->vendor, hdev->product))
  979. return -ENOMEM;
  980. return 0;
  981. }
  982. static struct bus_type hid_bus_type = {
  983. .name = "hid",
  984. .match = hid_bus_match,
  985. .probe = hid_device_probe,
  986. .remove = hid_device_remove,
  987. .uevent = hid_uevent,
  988. };
  989. int hid_add_device(struct hid_device *hdev)
  990. {
  991. static atomic_t id = ATOMIC_INIT(0);
  992. int ret;
  993. if (WARN_ON(hdev->status & HID_STAT_ADDED))
  994. return -EBUSY;
  995. /* XXX hack, any other cleaner solution < 20 bus_id bytes? */
  996. sprintf(hdev->dev.bus_id, "%04X:%04X:%04X.%04X", hdev->bus,
  997. hdev->vendor, hdev->product, atomic_inc_return(&id));
  998. ret = device_add(&hdev->dev);
  999. if (!ret)
  1000. hdev->status |= HID_STAT_ADDED;
  1001. return ret;
  1002. }
  1003. EXPORT_SYMBOL_GPL(hid_add_device);
  1004. /**
  1005. * hid_allocate_device - allocate new hid device descriptor
  1006. *
  1007. * Allocate and initialize hid device, so that hid_destroy_device might be
  1008. * used to free it.
  1009. *
  1010. * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
  1011. * error value.
  1012. */
  1013. struct hid_device *hid_allocate_device(void)
  1014. {
  1015. struct hid_device *hdev;
  1016. unsigned int i;
  1017. int ret = -ENOMEM;
  1018. hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
  1019. if (hdev == NULL)
  1020. return ERR_PTR(ret);
  1021. device_initialize(&hdev->dev);
  1022. hdev->dev.release = hid_device_release;
  1023. hdev->dev.bus = &hid_bus_type;
  1024. hdev->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
  1025. sizeof(struct hid_collection), GFP_KERNEL);
  1026. if (hdev->collection == NULL)
  1027. goto err;
  1028. hdev->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
  1029. for (i = 0; i < HID_REPORT_TYPES; i++)
  1030. INIT_LIST_HEAD(&hdev->report_enum[i].report_list);
  1031. return hdev;
  1032. err:
  1033. put_device(&hdev->dev);
  1034. return ERR_PTR(ret);
  1035. }
  1036. EXPORT_SYMBOL_GPL(hid_allocate_device);
  1037. static void hid_remove_device(struct hid_device *hdev)
  1038. {
  1039. if (hdev->status & HID_STAT_ADDED) {
  1040. device_del(&hdev->dev);
  1041. hdev->status &= ~HID_STAT_ADDED;
  1042. }
  1043. }
  1044. /**
  1045. * hid_destroy_device - free previously allocated device
  1046. *
  1047. * @hdev: hid device
  1048. *
  1049. * If you allocate hid_device through hid_allocate_device, you should ever
  1050. * free by this function.
  1051. */
  1052. void hid_destroy_device(struct hid_device *hdev)
  1053. {
  1054. hid_remove_device(hdev);
  1055. put_device(&hdev->dev);
  1056. }
  1057. EXPORT_SYMBOL_GPL(hid_destroy_device);
  1058. int __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
  1059. const char *mod_name)
  1060. {
  1061. hdrv->driver.name = hdrv->name;
  1062. hdrv->driver.bus = &hid_bus_type;
  1063. hdrv->driver.owner = owner;
  1064. hdrv->driver.mod_name = mod_name;
  1065. return driver_register(&hdrv->driver);
  1066. }
  1067. EXPORT_SYMBOL_GPL(__hid_register_driver);
  1068. void hid_unregister_driver(struct hid_driver *hdrv)
  1069. {
  1070. driver_unregister(&hdrv->driver);
  1071. }
  1072. EXPORT_SYMBOL_GPL(hid_unregister_driver);
  1073. static int __init hid_init(void)
  1074. {
  1075. int ret;
  1076. ret = bus_register(&hid_bus_type);
  1077. if (ret) {
  1078. printk(KERN_ERR "HID: can't register hid bus\n");
  1079. goto err;
  1080. }
  1081. ret = hidraw_init();
  1082. if (ret)
  1083. goto err_bus;
  1084. return 0;
  1085. err_bus:
  1086. bus_unregister(&hid_bus_type);
  1087. err:
  1088. return ret;
  1089. }
  1090. static void __exit hid_exit(void)
  1091. {
  1092. hidraw_exit();
  1093. bus_unregister(&hid_bus_type);
  1094. }
  1095. module_init(hid_init);
  1096. module_exit(hid_exit);
  1097. MODULE_LICENSE(DRIVER_LICENSE);