hid-core.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:33k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * $Id: hid-core.c,v 1.8 2001/05/23 12:02:18 vojtech Exp $
  3.  *
  4.  *  Copyright (c) 1999 Andreas Gal
  5.  *  Copyright (c) 2000-2001 Vojtech Pavlik
  6.  *
  7.  *  USB HID support for Linux
  8.  *
  9.  *  Sponsored by SuSE
  10.  */
  11. /*
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25.  *
  26.  * Should you need to contact me, the author, you can do so either by
  27.  * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
  28.  * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
  29.  */
  30. #include <linux/module.h>
  31. #include <linux/slab.h>
  32. #include <linux/init.h>
  33. #include <linux/kernel.h>
  34. #include <linux/sched.h>
  35. #include <linux/list.h>
  36. #include <linux/mm.h>
  37. #include <linux/smp_lock.h>
  38. #include <linux/spinlock.h>
  39. #include <asm/unaligned.h>
  40. #include <linux/input.h>
  41. #undef DEBUG
  42. #undef DEBUG_DATA
  43. #include <linux/usb.h>
  44. #include "hid.h"
  45. #include <linux/hiddev.h>
  46. /*
  47.  * Version Information
  48.  */
  49. #define DRIVER_VERSION "v1.8.1"
  50. #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik <vojtech@suse.cz>"
  51. #define DRIVER_DESC "USB HID support drivers"
  52. static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
  53. "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
  54. /*
  55.  * Register a new report for a device.
  56.  */
  57. static struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id)
  58. {
  59. struct hid_report_enum *report_enum = device->report_enum + type;
  60. struct hid_report *report;
  61. if (report_enum->report_id_hash[id])
  62. return report_enum->report_id_hash[id];
  63. if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL)))
  64. return NULL;
  65. memset(report, 0, sizeof(struct hid_report));
  66. if (id != 0) report_enum->numbered = 1;
  67. report->id = id;
  68. report->type = type;
  69. report->size = 0;
  70. report->device = device;
  71. report_enum->report_id_hash[id] = report;
  72. list_add_tail(&report->list, &report_enum->report_list);
  73. return report;
  74. }
  75. /*
  76.  * Register a new field for this report.
  77.  */
  78. static struct hid_field *hid_register_field(struct hid_report *report, unsigned usages, unsigned values)
  79. {
  80. struct hid_field *field;
  81. if (report->maxfield == HID_MAX_FIELDS) {
  82. dbg("too many fields in report");
  83. return NULL;
  84. }
  85. if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
  86. + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
  87. memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
  88. + values * sizeof(unsigned));
  89. report->field[report->maxfield++] = field;
  90. field->usage = (struct hid_usage *)(field + 1);
  91. field->value = (unsigned *)(field->usage + usages);
  92. field->report = report;
  93. return field;
  94. }
  95. /*
  96.  * Open a collection. The type/usage is pushed on the stack.
  97.  */
  98. static int open_collection(struct hid_parser *parser, unsigned type)
  99. {
  100. struct hid_collection *collection;
  101. unsigned usage;
  102. usage = parser->local.usage[0];
  103. if (type == HID_COLLECTION_APPLICATION
  104. && parser->device->maxapplication < HID_MAX_APPLICATIONS)
  105. parser->device->application[parser->device->maxapplication++] = usage;
  106. if (parser->collection_stack_ptr == HID_COLLECTION_STACK_SIZE) {
  107. dbg("collection stack overflow");
  108. return -1;
  109. }
  110. collection = parser->collection_stack + parser->collection_stack_ptr++;
  111. collection->type = type;
  112. collection->usage = usage;
  113. return 0;
  114. }
  115. /*
  116.  * Close a collection.
  117.  */
  118. static int close_collection(struct hid_parser *parser)
  119. {
  120. if (!parser->collection_stack_ptr) {
  121. dbg("collection stack underflow");
  122. return -1;
  123. }
  124. parser->collection_stack_ptr--;
  125. return 0;
  126. }
  127. /*
  128.  * Climb up the stack, search for the specified collection type
  129.  * and return the usage.
  130.  */
  131. static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
  132. {
  133. int n;
  134. for (n = parser->collection_stack_ptr - 1; n >= 0; n--)
  135. if (parser->collection_stack[n].type == type)
  136. return parser->collection_stack[n].usage;
  137. return 0; /* we know nothing about this usage type */
  138. }
  139. /*
  140.  * Add a usage to the temporary parser table.
  141.  */
  142. static int hid_add_usage(struct hid_parser *parser, unsigned usage)
  143. {
  144. if (parser->local.usage_index >= HID_MAX_USAGES) {
  145. dbg("usage index exceeded");
  146. return -1;
  147. }
  148. parser->local.usage[parser->local.usage_index++] = usage;
  149. return 0;
  150. }
  151. /*
  152.  * Register a new field for this report.
  153.  */
  154. static int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
  155. {
  156. struct hid_report *report;
  157. struct hid_field *field;
  158. int usages;
  159. unsigned offset;
  160. int i;
  161. if (!(report = hid_register_report(parser->device, report_type, parser->global.report_id))) {
  162. dbg("hid_register_report failed");
  163. return -1;
  164. }
  165. if (parser->global.logical_maximum <= parser->global.logical_minimum) {
  166. dbg("logical range invalid %d %d", parser->global.logical_minimum, parser->global.logical_maximum);
  167. return -1;
  168. }
  169. usages = parser->local.usage_index;
  170. offset = report->size;
  171. report->size += parser->global.report_size * parser->global.report_count;
  172. if (usages == 0)
  173. return 0; /* ignore padding fields */
  174. if ((field = hid_register_field(report, usages, parser->global.report_count)) == NULL)
  175. return 0;
  176. field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
  177. field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
  178. field->application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
  179. for (i = 0; i < usages; i++)
  180. field->usage[i].hid = parser->local.usage[i];
  181. field->maxusage = usages;
  182. field->flags = flags;
  183. field->report_offset = offset;
  184. field->report_type = report_type;
  185. field->report_size = parser->global.report_size;
  186. field->report_count = parser->global.report_count;
  187. field->logical_minimum = parser->global.logical_minimum;
  188. field->logical_maximum = parser->global.logical_maximum;
  189. field->physical_minimum = parser->global.physical_minimum;
  190. field->physical_maximum = parser->global.physical_maximum;
  191. field->unit_exponent = parser->global.unit_exponent;
  192. field->unit = parser->global.unit;
  193. return 0;
  194. }
  195. /*
  196.  * Read data value from item.
  197.  */
  198. static __inline__ __u32 item_udata(struct hid_item *item)
  199. {
  200. switch (item->size) {
  201. case 1: return item->data.u8;
  202. case 2: return item->data.u16;
  203. case 4: return item->data.u32;
  204. }
  205. return 0;
  206. }
  207. static __inline__ __s32 item_sdata(struct hid_item *item)
  208. {
  209. switch (item->size) {
  210. case 1: return item->data.s8;
  211. case 2: return item->data.s16;
  212. case 4: return item->data.s32;
  213. }
  214. return 0;
  215. }
  216. /*
  217.  * Process a global item.
  218.  */
  219. static int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
  220. {
  221. switch (item->tag) {
  222. case HID_GLOBAL_ITEM_TAG_PUSH:
  223. if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
  224. dbg("global enviroment stack overflow");
  225. return -1;
  226. }
  227. memcpy(parser->global_stack + parser->global_stack_ptr++,
  228. &parser->global, sizeof(struct hid_global));
  229. return 0;
  230. case HID_GLOBAL_ITEM_TAG_POP:
  231. if (!parser->global_stack_ptr) {
  232. dbg("global enviroment stack underflow");
  233. return -1;
  234. }
  235. memcpy(&parser->global, parser->global_stack + --parser->global_stack_ptr,
  236. sizeof(struct hid_global));
  237. return 0;
  238. case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
  239. parser->global.usage_page = item_udata(item);
  240. return 0;
  241. case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
  242. parser->global.logical_minimum = item_sdata(item);
  243. return 0;
  244. case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
  245. if (parser->global.logical_minimum < 0)
  246. parser->global.logical_maximum = item_sdata(item);
  247. else
  248. parser->global.logical_maximum = item_udata(item);
  249. return 0;
  250. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
  251. parser->global.physical_minimum = item_sdata(item);
  252. return 0;
  253. case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
  254. if (parser->global.physical_minimum < 0)
  255. parser->global.physical_maximum = item_sdata(item);
  256. else
  257. parser->global.physical_maximum = item_udata(item);
  258. return 0;
  259. case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
  260. parser->global.unit_exponent = item_udata(item);
  261. return 0;
  262. case HID_GLOBAL_ITEM_TAG_UNIT:
  263. parser->global.unit = item_udata(item);
  264. return 0;
  265. case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
  266. if ((parser->global.report_size = item_udata(item)) > 32) {
  267. dbg("invalid report_size %d", parser->global.report_size);
  268. return -1;
  269. }
  270. return 0;
  271. case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
  272. if ((parser->global.report_count = item_udata(item)) > HID_MAX_USAGES) {
  273. dbg("invalid report_count %d", parser->global.report_count);
  274. return -1;
  275. }
  276. return 0;
  277. case HID_GLOBAL_ITEM_TAG_REPORT_ID:
  278. if ((parser->global.report_id = item_udata(item)) == 0) {
  279. dbg("report_id 0 is invalid");
  280. return -1;
  281. }
  282. return 0;
  283. default:
  284. dbg("unknown global tag 0x%x", item->tag);
  285. return -1;
  286. }
  287. }
  288. /*
  289.  * Process a local item.
  290.  */
  291. static int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
  292. {
  293. __u32 data;
  294. unsigned n;
  295. if (item->size == 0) {
  296. dbg("item data expected for local item");
  297. return -1;
  298. }
  299. data = item_udata(item);
  300. switch (item->tag) {
  301. case HID_LOCAL_ITEM_TAG_DELIMITER:
  302. if (data) {
  303. /*
  304.  * We treat items before the first delimiter
  305.  * as global to all usage sets (branch 0).
  306.  * In the moment we process only these global
  307.  * items and the first delimiter set.
  308.  */
  309. if (parser->local.delimiter_depth != 0) {
  310. dbg("nested delimiters");
  311. return -1;
  312. }
  313. parser->local.delimiter_depth++;
  314. parser->local.delimiter_branch++;
  315. } else {
  316. if (parser->local.delimiter_depth < 1) {
  317. dbg("bogus close delimiter");
  318. return -1;
  319. }
  320. parser->local.delimiter_depth--;
  321. }
  322. return 1;
  323. case HID_LOCAL_ITEM_TAG_USAGE:
  324. if (parser->local.delimiter_branch > 1) {
  325. dbg("alternative usage ignored");
  326. return 0;
  327. }
  328. if (item->size <= 2)
  329. data = (parser->global.usage_page << 16) + data;
  330. return hid_add_usage(parser, data);
  331. case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
  332. if (parser->local.delimiter_branch > 1) {
  333. dbg("alternative usage ignored");
  334. return 0;
  335. }
  336. if (item->size <= 2)
  337. data = (parser->global.usage_page << 16) + data;
  338. parser->local.usage_minimum = data;
  339. return 0;
  340. case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
  341. if (parser->local.delimiter_branch > 1) {
  342. dbg("alternative usage ignored");
  343. return 0;
  344. }
  345. if (item->size <= 2)
  346. data = (parser->global.usage_page << 16) + data;
  347. for (n = parser->local.usage_minimum; n <= data; n++)
  348. if (hid_add_usage(parser, n)) {
  349. dbg("hid_add_usage failedn");
  350. return -1;
  351. }
  352. return 0;
  353. default:
  354. dbg("unknown local item tag 0x%x", item->tag);
  355. return 0;
  356. }
  357. return 0;
  358. }
  359. /*
  360.  * Process a main item.
  361.  */
  362. static int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
  363. {
  364. __u32 data;
  365. int ret;
  366. data = item_udata(item);
  367. switch (item->tag) {
  368. case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
  369. ret = open_collection(parser, data & 3);
  370. break;
  371. case HID_MAIN_ITEM_TAG_END_COLLECTION:
  372. ret = close_collection(parser);
  373. break;
  374. case HID_MAIN_ITEM_TAG_INPUT:
  375. ret = hid_add_field(parser, HID_INPUT_REPORT, data);
  376. break;
  377. case HID_MAIN_ITEM_TAG_OUTPUT:
  378. ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
  379. break;
  380. case HID_MAIN_ITEM_TAG_FEATURE:
  381. ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
  382. break;
  383. default:
  384. dbg("unknown main item tag 0x%x", item->tag);
  385. ret = 0;
  386. }
  387. memset(&parser->local, 0, sizeof(parser->local)); /* Reset the local parser environment */
  388. return ret;
  389. }
  390. /*
  391.  * Process a reserved item.
  392.  */
  393. static int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
  394. {
  395. dbg("reserved item type, tag 0x%x", item->tag);
  396. return 0;
  397. }
  398. /*
  399.  * Free a report and all registered fields. The field->usage and
  400.  * field->value table's are allocated behind the field, so we need
  401.  * only to free(field) itself.
  402.  */
  403. static void hid_free_report(struct hid_report *report)
  404. {
  405. unsigned n;
  406. for (n = 0; n < report->maxfield; n++)
  407. kfree(report->field[n]);
  408. if (report->data)
  409. kfree(report->data);
  410. kfree(report);
  411. }
  412. /*
  413.  * Free a device structure, all reports, and all fields.
  414.  */
  415. static void hid_free_device(struct hid_device *device)
  416. {
  417. unsigned i,j;
  418. for (i = 0; i < HID_REPORT_TYPES; i++) {
  419. struct hid_report_enum *report_enum = device->report_enum + i;
  420. for (j = 0; j < 256; j++) {
  421. struct hid_report *report = report_enum->report_id_hash[j];
  422. if (report) hid_free_report(report);
  423. }
  424. }
  425. if (device->rdesc) kfree(device->rdesc);
  426. }
  427. /*
  428.  * Fetch a report description item from the data stream. We support long
  429.  * items, though they are not used yet.
  430.  */
  431. static __u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
  432. {
  433. if ((end - start) > 0) {
  434. __u8 b = *start++;
  435. item->type = (b >> 2) & 3;
  436. item->tag  = (b >> 4) & 15;
  437. if (item->tag == HID_ITEM_TAG_LONG) {
  438. item->format = HID_ITEM_FORMAT_LONG;
  439. if ((end - start) >= 2) {
  440. item->size = *start++;
  441. item->tag  = *start++;
  442. if ((end - start) >= item->size) {
  443. item->data.longdata = start;
  444. start += item->size;
  445. return start;
  446. }
  447. }
  448. } else {
  449. item->format = HID_ITEM_FORMAT_SHORT;
  450. item->size = b & 3;
  451. switch (item->size) {
  452. case 0:
  453. return start;
  454. case 1:
  455. if ((end - start) >= 1) {
  456. item->data.u8 = *start++;
  457. return start;
  458. }
  459. break;
  460. case 2:
  461. if ((end - start) >= 2) {
  462. item->data.u16 = le16_to_cpu( get_unaligned(((__u16*)start)++));
  463. return start;
  464. }
  465. case 3:
  466. item->size++;
  467. if ((end - start) >= 4) {
  468. item->data.u32 = le32_to_cpu( get_unaligned(((__u32*)start)++));
  469. return start;
  470. }
  471. }
  472. }
  473. }
  474. return NULL;
  475. }
  476. /*
  477.  * Parse a report description into a hid_device structure. Reports are
  478.  * enumerated, fields are attached to these reports.
  479.  */
  480. static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
  481. {
  482. struct hid_device *device;
  483. struct hid_parser *parser;
  484. struct hid_item item;
  485. __u8 *end;
  486. unsigned i;
  487. static int (*dispatch_type[])(struct hid_parser *parser,
  488.       struct hid_item *item) = {
  489. hid_parser_main,
  490. hid_parser_global,
  491. hid_parser_local,
  492. hid_parser_reserved
  493. };
  494. if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL)))
  495. return NULL;
  496. memset(device, 0, sizeof(struct hid_device));
  497. for (i = 0; i < HID_REPORT_TYPES; i++)
  498. INIT_LIST_HEAD(&device->report_enum[i].report_list);
  499. if (!(device->rdesc = (__u8 *)kmalloc(size, GFP_KERNEL))) {
  500. kfree(device);
  501. return NULL;
  502. }
  503. memcpy(device->rdesc, start, size);
  504. if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
  505. kfree(device->rdesc);
  506. kfree(device);
  507. return NULL;
  508. }
  509. memset(parser, 0, sizeof(struct hid_parser));
  510. parser->device = device;
  511. end = start + size;
  512. while ((start = fetch_item(start, end, &item)) != 0) {
  513. if (item.format != HID_ITEM_FORMAT_SHORT) {
  514. dbg("unexpected long global item");
  515. hid_free_device(device);
  516. kfree(parser);
  517. return NULL;
  518. }
  519. if (dispatch_type[item.type](parser, &item)) {
  520. dbg("item %u %u %u %u parsing failedn",
  521. item.format, (unsigned)item.size, (unsigned)item.type, (unsigned)item.tag);
  522. hid_free_device(device);
  523. kfree(parser);
  524. return NULL;
  525. }
  526. if (start == end) {
  527. if (parser->collection_stack_ptr) {
  528. dbg("unbalanced collection at end of report description");
  529. hid_free_device(device);
  530. kfree(parser);
  531. return NULL;
  532. }
  533. if (parser->local.delimiter_depth) {
  534. dbg("unbalanced delimiter at end of report description");
  535. hid_free_device(device);
  536. kfree(parser);
  537. return NULL;
  538. }
  539. kfree(parser);
  540. return device;
  541. }
  542. }
  543. dbg("item fetching failed at offset %dn", (int)(end - start));
  544. hid_free_device(device);
  545. kfree(parser);
  546. return NULL;
  547. }
  548. /*
  549.  * Convert a signed n-bit integer to signed 32-bit integer. Common
  550.  * cases are done through the compiler, the screwed things has to be
  551.  * done by hand.
  552.  */
  553. static __inline__ __s32 snto32(__u32 value, unsigned n)
  554. {
  555. switch (n) {
  556. case 8:  return ((__s8)value);
  557. case 16: return ((__s16)value);
  558. case 32: return ((__s32)value);
  559. }
  560. return value & (1 << (n - 1)) ? value | (-1 << n) : value;
  561. }
  562. /*
  563.  * Convert a signed 32-bit integer to a signed n-bit integer.
  564.  */
  565. static __inline__ __u32 s32ton(__s32 value, unsigned n)
  566. {
  567. __s32 a = value >> (n - 1);
  568. if (a && a != -1) return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
  569. return value & ((1 << n) - 1);
  570. }
  571. /*
  572.  * Extract/implement a data field from/to a report.
  573.  */
  574. static __inline__ __u32 extract(__u8 *report, unsigned offset, unsigned n)
  575. {
  576. report += (offset >> 5) << 2; offset &= 31;
  577. return (le64_to_cpu(get_unaligned((__u64*)report)) >> offset) & ((1 << n) - 1);
  578. }
  579. static __inline__ void implement(__u8 *report, unsigned offset, unsigned n, __u32 value)
  580. {
  581. report += (offset >> 5) << 2; offset &= 31;
  582. put_unaligned((get_unaligned((__u64*)report)
  583. & cpu_to_le64(~((((__u64) 1 << n) - 1) << offset)))
  584. | cpu_to_le64((__u64)value << offset), (__u64*)report);
  585. }
  586. /*
  587.  * Search an array for a value.
  588.  */
  589. static __inline__ int search(__s32 *array, __s32 value, unsigned n)
  590. {
  591. while (n--) if (*array++ == value) return 0;
  592. return -1;
  593. }
  594. static void hid_process_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
  595. {
  596. hid_dump_input(usage, value);
  597. if (hid->claimed & HID_CLAIMED_INPUT)
  598. hidinput_hid_event(hid, field, usage, value);
  599. if (hid->claimed & HID_CLAIMED_HIDDEV)
  600. hiddev_hid_event(hid, usage->hid, value);
  601. }
  602. /*
  603.  * Analyse a received field, and fetch the data from it. The field
  604.  * content is stored for next report processing (we do differential
  605.  * reporting to the layer).
  606.  */
  607. static void hid_input_field(struct hid_device *hid, struct hid_field *field, __u8 *data)
  608. {
  609. unsigned n;
  610. unsigned count = field->report_count;
  611. unsigned offset = field->report_offset;
  612. unsigned size = field->report_size;
  613. __s32 min = field->logical_minimum;
  614. __s32 max = field->logical_maximum;
  615. __s32 value[count]; /* WARNING: gcc specific */
  616. for (n = 0; n < count; n++) {
  617. value[n] = min < 0 ? snto32(extract(data, offset + n * size, size), size) :
  618.     extract(data, offset + n * size, size);
  619. if (!(field->flags & HID_MAIN_ITEM_VARIABLE) /* Ignore report if ErrorRollOver */
  620.     && value[n] >= min && value[n] <= max
  621.     && field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
  622. return;
  623. }
  624. for (n = 0; n < count; n++) {
  625. if (HID_MAIN_ITEM_VARIABLE & field->flags) {
  626. if (field->flags & HID_MAIN_ITEM_RELATIVE) {
  627. if (!value[n]) continue;
  628. } else {
  629. if (value[n] == field->value[n]) continue;
  630. }
  631. hid_process_event(hid, field, &field->usage[n], value[n]);
  632. continue;
  633. }
  634. if (field->value[n] >= min && field->value[n] <= max
  635. && field->usage[field->value[n] - min].hid
  636. && search(value, field->value[n], count))
  637. hid_process_event(hid, field, &field->usage[field->value[n] - min], 0);
  638. if (value[n] >= min && value[n] <= max
  639. && field->usage[value[n] - min].hid
  640. && search(field->value, value[n], count))
  641. hid_process_event(hid, field, &field->usage[value[n] - min], 1);
  642. }
  643. memcpy(field->value, value, count * sizeof(__s32));
  644. }
  645. static int hid_input_report(int type, u8 *data, int len, struct hid_device *hid)
  646. {
  647. struct hid_report_enum *report_enum = hid->report_enum + type;
  648. struct hid_report *report;
  649. int n, size;
  650. if (!len) {
  651. dbg("empty report");
  652. return -1;
  653. }
  654. #ifdef DEBUG_DATA
  655. printk(KERN_DEBUG __FILE__ ": report (size %u) (%snumbered)n", len, report_enum->numbered ? "" : "un");
  656. #endif
  657. n = 0; /* Normally report number is 0 */
  658. if (report_enum->numbered) { /* Device uses numbered reports, data[0] is report number */
  659. n = *data++;
  660. len--;
  661. }
  662. if (!(report = report_enum->report_id_hash[n])) {
  663. dbg("undefined report_id %d received", n);
  664. #ifdef DEBUG
  665. printk(KERN_DEBUG __FILE__ ": report (size %u) = ", len);
  666. for (n = 0; n < len; n++)
  667. printk(" %02x", data[n]);
  668. printk("n");
  669. #endif
  670. return -1;
  671. }
  672. size = ((report->size - 1) >> 3) + 1;
  673. if (len < size) {
  674. if (size <= 8) {
  675. dbg("report %d is too short, (%d < %d)", report->id, len, size);
  676. return -1;
  677. }
  678. /*
  679.  * Some low-speed devices have large reports and maxpacketsize 8.
  680.  * We buffer the data in that case and parse it when we got it all.
  681.  * Works only for unnumbered reports. Doesn't make sense for numbered
  682.  * reports anyway - then they don't need to be large.
  683.  */
  684. if (!report->data)
  685. if (!(report->data = kmalloc(size, GFP_ATOMIC))) {
  686. dbg("couldn't allocate report buffer");
  687. return -1;
  688. }
  689. if (report->idx + len > size) {
  690. dbg("report data buffer overflow");
  691. report->idx = 0;
  692. return -1;
  693. }
  694. memcpy(report->data + report->idx, data, len);
  695. report->idx += len;
  696. if (report->idx < size)
  697. return 0;
  698. data = report->data;
  699. }
  700. for (n = 0; n < report->maxfield; n++)
  701. hid_input_field(hid, report->field[n], data);
  702. report->idx = 0;
  703. return 0;
  704. }
  705. /*
  706.  * Interrupt input handler.
  707.  */
  708. static void hid_irq(struct urb *urb)
  709. {
  710. if (urb->status) {
  711. dbg("nonzero status in irq %d", urb->status);
  712. return;
  713. }
  714. hid_input_report(HID_INPUT_REPORT, urb->transfer_buffer, urb->actual_length, urb->context);
  715. }
  716. /*
  717.  * hid_read_report() reads in report values without waiting for an irq urb.
  718.  */
  719. void hid_read_report(struct hid_device *hid, struct hid_report *report)
  720. {
  721. int len = ((report->size - 1) >> 3) + 1 + hid->report_enum[report->type].numbered;
  722. u8 data[len];
  723. int read;
  724. if (hid->quirks & HID_QUIRK_NOGET)
  725. return;
  726. if ((read = usb_get_report(hid->dev, hid->ifnum, report->type + 1, report->id, data, len)) != len) {
  727. dbg("reading report type %d id %d failed len %d read %d", report->type + 1, report->id, len, read);
  728. return;
  729. }
  730. hid_input_report(report->type, data, len, hid);
  731. }
  732. /*
  733.  * Output the field into the report.
  734.  */
  735. static void hid_output_field(struct hid_field *field, __u8 *data)
  736. {
  737. unsigned count = field->report_count;
  738. unsigned offset = field->report_offset;
  739. unsigned size = field->report_size;
  740. unsigned n;
  741. for (n = 0; n < count; n++) {
  742. if (field->logical_minimum < 0) /* signed values */
  743. implement(data, offset + n * size, size, s32ton(field->value[n], size));
  744.  else /* unsigned values */
  745. implement(data, offset + n * size, size, field->value[n]);
  746. }
  747. }
  748. /*
  749.  * Create a report.
  750.  */
  751. void hid_output_report(struct hid_report *report, __u8 *data)
  752. {
  753. unsigned n;
  754. for (n = 0; n < report->maxfield; n++)
  755. hid_output_field(report->field[n], data);
  756. }
  757. /*
  758.  * Set a field value. The report this field belongs to has to be
  759.  * created and transfered to the device, to set this value in the
  760.  * device.
  761.  */
  762. int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
  763. {
  764. unsigned size = field->report_size;
  765. hid_dump_input(field->usage + offset, value);
  766. if (offset >= field->report_count) {
  767. dbg("offset exceeds report_count");
  768. return -1;
  769. }
  770. if (field->logical_minimum < 0) {
  771. if (value != snto32(s32ton(value, size), size)) {
  772. dbg("value %d is out of range", value);
  773. return -1;
  774. }
  775. }
  776. if (   (value > field->logical_maximum)
  777.     || (value < field->logical_minimum)) {
  778. dbg("value %d is invalid", value);
  779. return -1;
  780. }
  781. field->value[offset] = value;
  782. return 0;
  783. }
  784. int hid_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
  785. {
  786. struct hid_report_enum *report_enum = hid->report_enum + HID_OUTPUT_REPORT;
  787. struct list_head *list = report_enum->report_list.next;
  788. int i, j;
  789. while (list != &report_enum->report_list) {
  790. struct hid_report *report = (struct hid_report *) list;
  791. list = list->next;
  792. for (i = 0; i < report->maxfield; i++) {
  793. *field = report->field[i];
  794. for (j = 0; j < (*field)->maxusage; j++)
  795. if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
  796. return j;
  797. }
  798. }
  799. return -1;
  800. }
  801. static int hid_submit_out(struct hid_device *hid)
  802. {
  803. hid->urbout.transfer_buffer_length = le16_to_cpup(&hid->out[hid->outtail].dr.wLength);
  804. hid->urbout.transfer_buffer = hid->out[hid->outtail].buffer;
  805. hid->urbout.setup_packet = (void *) &(hid->out[hid->outtail].dr);
  806. hid->urbout.dev = hid->dev;
  807. if (usb_submit_urb(&hid->urbout)) {
  808. err("usb_submit_urb(out) failed");
  809. return -1;
  810. }
  811. return 0;
  812. }
  813. static void hid_ctrl(struct urb *urb)
  814. {
  815. struct hid_device *hid = urb->context;
  816. if (urb->status)
  817. warn("ctrl urb status %d received", urb->status);
  818. hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  819. if (hid->outhead != hid->outtail)
  820. hid_submit_out(hid);
  821. }
  822. void hid_write_report(struct hid_device *hid, struct hid_report *report)
  823. {
  824. hid_output_report(report, hid->out[hid->outhead].buffer);
  825. hid->out[hid->outhead].dr.wValue = cpu_to_le16(0x200 | report->id);
  826. hid->out[hid->outhead].dr.wLength = cpu_to_le16((report->size + 7) >> 3);
  827. hid->outhead = (hid->outhead + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  828. if (hid->outhead == hid->outtail)
  829. hid->outtail = (hid->outtail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
  830. if (hid->urbout.status != -EINPROGRESS)
  831. hid_submit_out(hid);
  832. }
  833. int hid_open(struct hid_device *hid)
  834. {
  835. if (hid->open++)
  836. return 0;
  837. hid->urb.dev = hid->dev;
  838. if (usb_submit_urb(&hid->urb))
  839. return -EIO;
  840. return 0;
  841. }
  842. void hid_close(struct hid_device *hid)
  843. {
  844. if (!--hid->open)
  845. usb_unlink_urb(&hid->urb);
  846. }
  847. /*
  848.  * Initialize all readable reports
  849.  */
  850. void hid_init_reports(struct hid_device *hid)
  851. {
  852. int i;
  853. struct hid_report *report;
  854. struct hid_report_enum *report_enum;
  855. struct list_head *list;
  856. for (i = 0; i < HID_REPORT_TYPES; i++) {
  857. if (i == HID_FEATURE_REPORT || i == HID_INPUT_REPORT) {
  858. report_enum = hid->report_enum + i;
  859. list = report_enum->report_list.next;
  860. while (list != &report_enum->report_list) {
  861. report = (struct hid_report *) list;
  862. hid_read_report(hid, report);
  863. usb_set_idle(hid->dev, hid->ifnum, 0, report->id);
  864. list = list->next;
  865. }
  866. }
  867. }
  868. }
  869. #define USB_VENDOR_ID_WACOM 0x056a
  870. #define USB_DEVICE_ID_WACOM_PENPARTNER 0x0000
  871. #define USB_DEVICE_ID_WACOM_GRAPHIRE 0x0010
  872. #define USB_DEVICE_ID_WACOM_INTUOS 0x0020
  873. #define USB_DEVICE_ID_WACOM_PL 0x0030
  874. #define USB_DEVICE_ID_WACOM_INTUOS2 0x0041
  875. #define USB_VENDOR_ID_ATEN 0x0557
  876. #define USB_DEVICE_ID_ATEN_UC100KM 0x2004
  877. #define USB_DEVICE_ID_ATEN_CS124U 0x2202
  878. #define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204
  879. #define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
  880. struct hid_blacklist {
  881. __u16 idVendor;
  882. __u16 idProduct;
  883. unsigned quirks;
  884. } hid_blacklist[] = {
  885. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PENPARTNER, HID_QUIRK_IGNORE },
  886. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE, HID_QUIRK_IGNORE },
  887. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 1, HID_QUIRK_IGNORE },
  888. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE + 2, HID_QUIRK_IGNORE },
  889. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS, HID_QUIRK_IGNORE },
  890. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 1, HID_QUIRK_IGNORE },
  891. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 2, HID_QUIRK_IGNORE },
  892. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 3, HID_QUIRK_IGNORE },
  893. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS + 4, HID_QUIRK_IGNORE },
  894. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL, HID_QUIRK_IGNORE },
  895. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 1, HID_QUIRK_IGNORE },
  896. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 2, HID_QUIRK_IGNORE },
  897. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 3, HID_QUIRK_IGNORE },
  898. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 4, HID_QUIRK_IGNORE },
  899. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_PL + 5, HID_QUIRK_IGNORE },
  900. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2, HID_QUIRK_IGNORE },
  901. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 1, HID_QUIRK_IGNORE },
  902. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 2, HID_QUIRK_IGNORE },
  903. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 3, HID_QUIRK_IGNORE },
  904. { USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS2 + 4, HID_QUIRK_IGNORE },
  905. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_UC100KM, HID_QUIRK_NOGET },
  906. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_CS124U, HID_QUIRK_NOGET },
  907. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_2PORTKVM, HID_QUIRK_NOGET },
  908. { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVM, HID_QUIRK_NOGET },
  909. { 0, 0 }
  910. };
  911. static struct hid_device *usb_hid_configure(struct usb_device *dev, int ifnum)
  912. {
  913. struct usb_interface_descriptor *interface = dev->actconfig->interface[ifnum].altsetting + 0;
  914. struct hid_descriptor *hdesc;
  915. struct hid_device *hid;
  916. unsigned quirks = 0, rsize = 0;
  917. char *buf;
  918. int n;
  919. for (n = 0; hid_blacklist[n].idVendor; n++)
  920. if ((hid_blacklist[n].idVendor == dev->descriptor.idVendor) &&
  921. (hid_blacklist[n].idProduct == dev->descriptor.idProduct))
  922. quirks = hid_blacklist[n].quirks;
  923. if (quirks & HID_QUIRK_IGNORE)
  924. return NULL;
  925. if (usb_get_extra_descriptor(interface, USB_DT_HID, &hdesc) && ((!interface->bNumEndpoints) ||
  926. usb_get_extra_descriptor(&interface->endpoint[0], USB_DT_HID, &hdesc))) {
  927. dbg("class descriptor not presentn");
  928. return NULL;
  929. }
  930. for (n = 0; n < hdesc->bNumDescriptors; n++)
  931. if (hdesc->desc[n].bDescriptorType == USB_DT_REPORT)
  932. rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
  933. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  934. dbg("weird size of report descriptor (%u)", rsize);
  935. return NULL;
  936. }
  937. {
  938. __u8 rdesc[rsize];
  939. if ((n = usb_get_class_descriptor(dev, interface->bInterfaceNumber, USB_DT_REPORT, 0, rdesc, rsize)) < 0) {
  940. dbg("reading report descriptor failed");
  941. return NULL;
  942. }
  943. #ifdef DEBUG_DATA
  944. printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
  945. for (n = 0; n < rsize; n++)
  946. printk(" %02x", (unsigned) rdesc[n]);
  947. printk("n");
  948. #endif
  949. if (!(hid = hid_parse_report(rdesc, rsize))) {
  950. dbg("parsing report descriptor failed");
  951. return NULL;
  952. }
  953. }
  954. hid->quirks = quirks;
  955. for (n = 0; n < interface->bNumEndpoints; n++) {
  956. struct usb_endpoint_descriptor *endpoint = &interface->endpoint[n];
  957. int pipe, maxp;
  958. if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
  959. continue;
  960. if (!(endpoint->bEndpointAddress & 0x80)) /* Not an input endpoint */
  961. continue;
  962. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  963. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  964. FILL_INT_URB(&hid->urb, dev, pipe, hid->buffer, maxp > 32 ? 32 : maxp, hid_irq, hid, endpoint->bInterval);
  965. break;
  966. }
  967. if (n == interface->bNumEndpoints) {
  968. dbg("couldn't find an input interrupt endpoint");
  969. hid_free_device(hid);
  970. return NULL;
  971. }
  972. hid->version = hdesc->bcdHID;
  973. hid->country = hdesc->bCountryCode;
  974. hid->dev = dev;
  975. hid->ifnum = interface->bInterfaceNumber;
  976. for (n = 0; n < HID_CONTROL_FIFO_SIZE; n++) {
  977. hid->out[n].dr.bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  978. hid->out[n].dr.bRequest = USB_REQ_SET_REPORT;
  979. hid->out[n].dr.wIndex = cpu_to_le16(hid->ifnum);
  980. }
  981. hid->name[0] = 0;
  982. if (!(buf = kmalloc(63, GFP_KERNEL)))
  983. return NULL;
  984. if (usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0) {
  985. strcat(hid->name, buf);
  986. if (usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
  987. sprintf(hid->name, "%s %s", hid->name, buf);
  988. } else
  989. sprintf(hid->name, "%04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
  990. kfree(buf);
  991. FILL_CONTROL_URB(&hid->urbout, dev, usb_sndctrlpipe(dev, 0),
  992. (void*) &hid->out[0].dr, hid->out[0].buffer, 1, hid_ctrl, hid);
  993. /*
  994.  * Some devices don't like this and crash. I don't know of any devices
  995.  * needing this, so it is disabled for now.
  996.  */
  997. #if 0
  998. if (interface->bInterfaceSubClass == 1)
  999. usb_set_protocol(dev, hid->ifnum, 1);
  1000. #endif
  1001. return hid;
  1002. }
  1003. static void* hid_probe(struct usb_device *dev, unsigned int ifnum,
  1004.        const struct usb_device_id *id)
  1005. {
  1006. struct hid_device *hid;
  1007. int i;
  1008. char *c;
  1009. dbg("HID probe called for ifnum %d", ifnum);
  1010. if (!(hid = usb_hid_configure(dev, ifnum)))
  1011. return NULL;
  1012. hid_init_reports(hid);
  1013. hid_dump_device(hid);
  1014. if (!hidinput_connect(hid))
  1015. hid->claimed |= HID_CLAIMED_INPUT;
  1016. if (!hiddev_connect(hid))
  1017. hid->claimed |= HID_CLAIMED_HIDDEV;
  1018. printk(KERN_INFO);
  1019. if (hid->claimed & HID_CLAIMED_INPUT)
  1020. printk("input%d", hid->input.number);
  1021. if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
  1022. printk(",");
  1023. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1024. printk("hiddev%d", hid->minor);
  1025. c = "Device";
  1026. for (i = 0; i < hid->maxapplication; i++)
  1027. if ((hid->application[i] & 0xffff) < ARRAY_SIZE(hid_types)) {
  1028. c = hid_types[hid->application[i] & 0xffff];
  1029. break;
  1030. }
  1031. printk(": USB HID v%x.%02x %s [%s] on usb%d:%d.%dn",
  1032. hid->version >> 8, hid->version & 0xff, c, hid->name,
  1033. dev->bus->busnum, dev->devnum, ifnum);
  1034. return hid;
  1035. }
  1036. static void hid_disconnect(struct usb_device *dev, void *ptr)
  1037. {
  1038. struct hid_device *hid = ptr;
  1039. dbg("cleanup called");
  1040. usb_unlink_urb(&hid->urb);
  1041. if (hid->claimed & HID_CLAIMED_INPUT)
  1042. hidinput_disconnect(hid);
  1043. if (hid->claimed & HID_CLAIMED_HIDDEV)
  1044. hiddev_disconnect(hid);
  1045. hid_free_device(hid);
  1046. }
  1047. static struct usb_device_id hid_usb_ids [] = {
  1048. { match_flags: USB_DEVICE_ID_MATCH_INT_CLASS,
  1049.     bInterfaceClass: USB_INTERFACE_CLASS_HID },
  1050. { } /* Terminating entry */
  1051. };
  1052. MODULE_DEVICE_TABLE (usb, hid_usb_ids);
  1053. static struct usb_driver hid_driver = {
  1054. name: "hid",
  1055. probe: hid_probe,
  1056. disconnect: hid_disconnect,
  1057. id_table: hid_usb_ids,
  1058. };
  1059. static int __init hid_init(void)
  1060. {
  1061. hiddev_init();
  1062. usb_register(&hid_driver);
  1063. info(DRIVER_VERSION " " DRIVER_AUTHOR);
  1064. info(DRIVER_DESC);
  1065. return 0;
  1066. }
  1067. static void __exit hid_exit(void)
  1068. {
  1069. hiddev_exit();
  1070. usb_deregister(&hid_driver);
  1071. }
  1072. module_init(hid_init);
  1073. module_exit(hid_exit);
  1074. MODULE_AUTHOR( DRIVER_AUTHOR );
  1075. MODULE_DESCRIPTION( DRIVER_DESC );
  1076. MODULE_LICENSE("GPL");