hid-core.c 32 KB

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