PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/hid/hid-multitouch.c

https://github.com/genesi/linux-shortbus
C | 516 lines | 395 code | 71 blank | 50 comment | 49 complexity | 7a1e9ca03079c3c8f046bca4fba30afb MD5 | raw file
  1. /*
  2. * HID driver for multitouch panels
  3. *
  4. * Copyright (c) 2010-2011 Stephane Chatty <chatty@enac.fr>
  5. * Copyright (c) 2010-2011 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  6. * Copyright (c) 2010-2011 Ecole Nationale de l'Aviation Civile, France
  7. *
  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/device.h>
  16. #include <linux/hid.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #include <linux/input/mt.h>
  21. #include "usbhid/usbhid.h"
  22. MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
  23. MODULE_DESCRIPTION("HID multitouch panels");
  24. MODULE_LICENSE("GPL");
  25. #include "hid-ids.h"
  26. /* quirks to control the device */
  27. #define MT_QUIRK_NOT_SEEN_MEANS_UP (1 << 0)
  28. #define MT_QUIRK_SLOT_IS_CONTACTID (1 << 1)
  29. #define MT_QUIRK_CYPRESS (1 << 2)
  30. #define MT_QUIRK_SLOT_IS_CONTACTNUMBER (1 << 3)
  31. #define MT_QUIRK_VALID_IS_INRANGE (1 << 4)
  32. #define MT_QUIRK_VALID_IS_CONFIDENCE (1 << 5)
  33. struct mt_slot {
  34. __s32 x, y, p, w, h;
  35. __s32 contactid; /* the device ContactID assigned to this slot */
  36. bool touch_state; /* is the touch valid? */
  37. bool seen_in_this_frame;/* has this slot been updated */
  38. };
  39. struct mt_device {
  40. struct mt_slot curdata; /* placeholder of incoming data */
  41. struct mt_class *mtclass; /* our mt device class */
  42. unsigned last_field_index; /* last field index of the report */
  43. unsigned last_slot_field; /* the last field of a slot */
  44. __s8 inputmode; /* InputMode HID feature, -1 if non-existent */
  45. __u8 num_received; /* how many contacts we received */
  46. __u8 num_expected; /* expected last contact index */
  47. bool curvalid; /* is the current contact valid? */
  48. struct mt_slot slots[0]; /* first slot */
  49. };
  50. struct mt_class {
  51. __s32 name; /* MT_CLS */
  52. __s32 quirks;
  53. __s32 sn_move; /* Signal/noise ratio for move events */
  54. __s32 sn_pressure; /* Signal/noise ratio for pressure events */
  55. __u8 maxcontacts;
  56. };
  57. /* classes of device behavior */
  58. #define MT_CLS_DEFAULT 1
  59. #define MT_CLS_DUAL1 2
  60. #define MT_CLS_DUAL2 3
  61. #define MT_CLS_CYPRESS 4
  62. /*
  63. * these device-dependent functions determine what slot corresponds
  64. * to a valid contact that was just read.
  65. */
  66. static int cypress_compute_slot(struct mt_device *td)
  67. {
  68. if (td->curdata.contactid != 0 || td->num_received == 0)
  69. return td->curdata.contactid;
  70. else
  71. return -1;
  72. }
  73. static int find_slot_from_contactid(struct mt_device *td)
  74. {
  75. int i;
  76. for (i = 0; i < td->mtclass->maxcontacts; ++i) {
  77. if (td->slots[i].contactid == td->curdata.contactid &&
  78. td->slots[i].touch_state)
  79. return i;
  80. }
  81. for (i = 0; i < td->mtclass->maxcontacts; ++i) {
  82. if (!td->slots[i].seen_in_this_frame &&
  83. !td->slots[i].touch_state)
  84. return i;
  85. }
  86. /* should not occurs. If this happens that means
  87. * that the device sent more touches that it says
  88. * in the report descriptor. It is ignored then. */
  89. return -1;
  90. }
  91. struct mt_class mt_classes[] = {
  92. { .name = MT_CLS_DEFAULT,
  93. .quirks = MT_QUIRK_VALID_IS_INRANGE,
  94. .maxcontacts = 10 },
  95. { .name = MT_CLS_DUAL1,
  96. .quirks = MT_QUIRK_VALID_IS_INRANGE |
  97. MT_QUIRK_SLOT_IS_CONTACTID,
  98. .maxcontacts = 2 },
  99. { .name = MT_CLS_DUAL2,
  100. .quirks = MT_QUIRK_VALID_IS_INRANGE |
  101. MT_QUIRK_SLOT_IS_CONTACTNUMBER,
  102. .maxcontacts = 2 },
  103. { .name = MT_CLS_CYPRESS,
  104. .quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
  105. MT_QUIRK_CYPRESS,
  106. .maxcontacts = 10 },
  107. { }
  108. };
  109. static void mt_feature_mapping(struct hid_device *hdev,
  110. struct hid_field *field, struct hid_usage *usage)
  111. {
  112. if (usage->hid == HID_DG_INPUTMODE) {
  113. struct mt_device *td = hid_get_drvdata(hdev);
  114. td->inputmode = field->report->id;
  115. }
  116. }
  117. static void set_abs(struct input_dev *input, unsigned int code,
  118. struct hid_field *field, int snratio)
  119. {
  120. int fmin = field->logical_minimum;
  121. int fmax = field->logical_maximum;
  122. int fuzz = snratio ? (fmax - fmin) / snratio : 0;
  123. input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
  124. }
  125. static int mt_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  126. struct hid_field *field, struct hid_usage *usage,
  127. unsigned long **bit, int *max)
  128. {
  129. struct mt_device *td = hid_get_drvdata(hdev);
  130. struct mt_class *cls = td->mtclass;
  131. switch (usage->hid & HID_USAGE_PAGE) {
  132. case HID_UP_GENDESK:
  133. switch (usage->hid) {
  134. case HID_GD_X:
  135. hid_map_usage(hi, usage, bit, max,
  136. EV_ABS, ABS_MT_POSITION_X);
  137. set_abs(hi->input, ABS_MT_POSITION_X, field,
  138. cls->sn_move);
  139. /* touchscreen emulation */
  140. set_abs(hi->input, ABS_X, field, cls->sn_move);
  141. td->last_slot_field = usage->hid;
  142. return 1;
  143. case HID_GD_Y:
  144. hid_map_usage(hi, usage, bit, max,
  145. EV_ABS, ABS_MT_POSITION_Y);
  146. set_abs(hi->input, ABS_MT_POSITION_Y, field,
  147. cls->sn_move);
  148. /* touchscreen emulation */
  149. set_abs(hi->input, ABS_Y, field, cls->sn_move);
  150. td->last_slot_field = usage->hid;
  151. return 1;
  152. }
  153. return 0;
  154. case HID_UP_DIGITIZER:
  155. switch (usage->hid) {
  156. case HID_DG_INRANGE:
  157. td->last_slot_field = usage->hid;
  158. return 1;
  159. case HID_DG_CONFIDENCE:
  160. td->last_slot_field = usage->hid;
  161. return 1;
  162. case HID_DG_TIPSWITCH:
  163. hid_map_usage(hi, usage, bit, max, EV_KEY, BTN_TOUCH);
  164. input_set_capability(hi->input, EV_KEY, BTN_TOUCH);
  165. td->last_slot_field = usage->hid;
  166. return 1;
  167. case HID_DG_CONTACTID:
  168. input_mt_init_slots(hi->input,
  169. td->mtclass->maxcontacts);
  170. td->last_slot_field = usage->hid;
  171. return 1;
  172. case HID_DG_WIDTH:
  173. hid_map_usage(hi, usage, bit, max,
  174. EV_ABS, ABS_MT_TOUCH_MAJOR);
  175. td->last_slot_field = usage->hid;
  176. return 1;
  177. case HID_DG_HEIGHT:
  178. hid_map_usage(hi, usage, bit, max,
  179. EV_ABS, ABS_MT_TOUCH_MINOR);
  180. field->logical_maximum = 1;
  181. field->logical_minimum = 0;
  182. set_abs(hi->input, ABS_MT_ORIENTATION, field, 0);
  183. td->last_slot_field = usage->hid;
  184. return 1;
  185. case HID_DG_TIPPRESSURE:
  186. hid_map_usage(hi, usage, bit, max,
  187. EV_ABS, ABS_MT_PRESSURE);
  188. set_abs(hi->input, ABS_MT_PRESSURE, field,
  189. cls->sn_pressure);
  190. /* touchscreen emulation */
  191. set_abs(hi->input, ABS_PRESSURE, field,
  192. cls->sn_pressure);
  193. td->last_slot_field = usage->hid;
  194. return 1;
  195. case HID_DG_CONTACTCOUNT:
  196. td->last_field_index = field->report->maxfield - 1;
  197. return 1;
  198. case HID_DG_CONTACTMAX:
  199. /* we don't set td->last_slot_field as contactcount and
  200. * contact max are global to the report */
  201. return -1;
  202. }
  203. /* let hid-input decide for the others */
  204. return 0;
  205. case 0xff000000:
  206. /* we do not want to map these: no input-oriented meaning */
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. static int mt_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  212. struct hid_field *field, struct hid_usage *usage,
  213. unsigned long **bit, int *max)
  214. {
  215. if (usage->type == EV_KEY || usage->type == EV_ABS)
  216. set_bit(usage->type, hi->input->evbit);
  217. return -1;
  218. }
  219. static int mt_compute_slot(struct mt_device *td)
  220. {
  221. __s32 quirks = td->mtclass->quirks;
  222. if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
  223. return td->curdata.contactid;
  224. if (quirks & MT_QUIRK_CYPRESS)
  225. return cypress_compute_slot(td);
  226. if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
  227. return td->num_received;
  228. return find_slot_from_contactid(td);
  229. }
  230. /*
  231. * this function is called when a whole contact has been processed,
  232. * so that it can assign it to a slot and store the data there
  233. */
  234. static void mt_complete_slot(struct mt_device *td)
  235. {
  236. td->curdata.seen_in_this_frame = true;
  237. if (td->curvalid) {
  238. int slotnum = mt_compute_slot(td);
  239. if (slotnum >= 0 && slotnum < td->mtclass->maxcontacts)
  240. td->slots[slotnum] = td->curdata;
  241. }
  242. td->num_received++;
  243. }
  244. /*
  245. * this function is called when a whole packet has been received and processed,
  246. * so that it can decide what to send to the input layer.
  247. */
  248. static void mt_emit_event(struct mt_device *td, struct input_dev *input)
  249. {
  250. int i;
  251. for (i = 0; i < td->mtclass->maxcontacts; ++i) {
  252. struct mt_slot *s = &(td->slots[i]);
  253. if ((td->mtclass->quirks & MT_QUIRK_NOT_SEEN_MEANS_UP) &&
  254. !s->seen_in_this_frame) {
  255. s->touch_state = false;
  256. }
  257. input_mt_slot(input, i);
  258. input_mt_report_slot_state(input, MT_TOOL_FINGER,
  259. s->touch_state);
  260. if (s->touch_state) {
  261. input_event(input, EV_ABS, ABS_MT_POSITION_X, s->x);
  262. input_event(input, EV_ABS, ABS_MT_POSITION_Y, s->y);
  263. input_event(input, EV_ABS, ABS_MT_PRESSURE, s->p);
  264. input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, s->w);
  265. input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, s->h);
  266. }
  267. s->seen_in_this_frame = false;
  268. }
  269. input_mt_report_pointer_emulation(input, true);
  270. input_sync(input);
  271. td->num_received = 0;
  272. }
  273. static int mt_event(struct hid_device *hid, struct hid_field *field,
  274. struct hid_usage *usage, __s32 value)
  275. {
  276. struct mt_device *td = hid_get_drvdata(hid);
  277. __s32 quirks = td->mtclass->quirks;
  278. if (hid->claimed & HID_CLAIMED_INPUT) {
  279. switch (usage->hid) {
  280. case HID_DG_INRANGE:
  281. if (quirks & MT_QUIRK_VALID_IS_INRANGE)
  282. td->curvalid = value;
  283. break;
  284. case HID_DG_TIPSWITCH:
  285. if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
  286. td->curvalid = value;
  287. td->curdata.touch_state = value;
  288. break;
  289. case HID_DG_CONFIDENCE:
  290. if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
  291. td->curvalid = value;
  292. break;
  293. case HID_DG_CONTACTID:
  294. td->curdata.contactid = value;
  295. break;
  296. case HID_DG_TIPPRESSURE:
  297. td->curdata.p = value;
  298. break;
  299. case HID_GD_X:
  300. td->curdata.x = value;
  301. break;
  302. case HID_GD_Y:
  303. td->curdata.y = value;
  304. break;
  305. case HID_DG_WIDTH:
  306. td->curdata.w = value;
  307. break;
  308. case HID_DG_HEIGHT:
  309. td->curdata.h = value;
  310. break;
  311. case HID_DG_CONTACTCOUNT:
  312. /*
  313. * Includes multi-packet support where subsequent
  314. * packets are sent with zero contactcount.
  315. */
  316. if (value)
  317. td->num_expected = value;
  318. break;
  319. default:
  320. /* fallback to the generic hidinput handling */
  321. return 0;
  322. }
  323. if (usage->hid == td->last_slot_field)
  324. mt_complete_slot(td);
  325. if (field->index == td->last_field_index
  326. && td->num_received >= td->num_expected)
  327. mt_emit_event(td, field->hidinput->input);
  328. }
  329. /* we have handled the hidinput part, now remains hiddev */
  330. if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
  331. hid->hiddev_hid_event(hid, field, usage, value);
  332. return 1;
  333. }
  334. static void mt_set_input_mode(struct hid_device *hdev)
  335. {
  336. struct mt_device *td = hid_get_drvdata(hdev);
  337. struct hid_report *r;
  338. struct hid_report_enum *re;
  339. if (td->inputmode < 0)
  340. return;
  341. re = &(hdev->report_enum[HID_FEATURE_REPORT]);
  342. r = re->report_id_hash[td->inputmode];
  343. if (r) {
  344. r->field[0]->value[0] = 0x02;
  345. usbhid_submit_report(hdev, r, USB_DIR_OUT);
  346. }
  347. }
  348. static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
  349. {
  350. int ret, i;
  351. struct mt_device *td;
  352. struct mt_class *mtclass = mt_classes; /* MT_CLS_DEFAULT */
  353. for (i = 0; mt_classes[i].name ; i++) {
  354. if (id->driver_data == mt_classes[i].name) {
  355. mtclass = &(mt_classes[i]);
  356. break;
  357. }
  358. }
  359. /* This allows the driver to correctly support devices
  360. * that emit events over several HID messages.
  361. */
  362. hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
  363. td = kzalloc(sizeof(struct mt_device) +
  364. mtclass->maxcontacts * sizeof(struct mt_slot),
  365. GFP_KERNEL);
  366. if (!td) {
  367. dev_err(&hdev->dev, "cannot allocate multitouch data\n");
  368. return -ENOMEM;
  369. }
  370. td->mtclass = mtclass;
  371. td->inputmode = -1;
  372. hid_set_drvdata(hdev, td);
  373. ret = hid_parse(hdev);
  374. if (ret != 0)
  375. goto fail;
  376. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  377. if (ret)
  378. goto fail;
  379. mt_set_input_mode(hdev);
  380. return 0;
  381. fail:
  382. kfree(td);
  383. return ret;
  384. }
  385. #ifdef CONFIG_PM
  386. static int mt_reset_resume(struct hid_device *hdev)
  387. {
  388. mt_set_input_mode(hdev);
  389. return 0;
  390. }
  391. #endif
  392. static void mt_remove(struct hid_device *hdev)
  393. {
  394. struct mt_device *td = hid_get_drvdata(hdev);
  395. hid_hw_stop(hdev);
  396. kfree(td);
  397. hid_set_drvdata(hdev, NULL);
  398. }
  399. static const struct hid_device_id mt_devices[] = {
  400. /* Cypress panel */
  401. { .driver_data = MT_CLS_CYPRESS,
  402. HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS,
  403. USB_DEVICE_ID_CYPRESS_TRUETOUCH) },
  404. /* GeneralTouch panel */
  405. { .driver_data = MT_CLS_DUAL2,
  406. HID_USB_DEVICE(USB_VENDOR_ID_GENERAL_TOUCH,
  407. USB_DEVICE_ID_GENERAL_TOUCH_WIN7_TWOFINGERS) },
  408. /* PixCir-based panels */
  409. { .driver_data = MT_CLS_DUAL1,
  410. HID_USB_DEVICE(USB_VENDOR_ID_HANVON,
  411. USB_DEVICE_ID_HANVON_MULTITOUCH) },
  412. { .driver_data = MT_CLS_DUAL1,
  413. HID_USB_DEVICE(USB_VENDOR_ID_CANDO,
  414. USB_DEVICE_ID_CANDO_PIXCIR_MULTI_TOUCH) },
  415. { }
  416. };
  417. MODULE_DEVICE_TABLE(hid, mt_devices);
  418. static const struct hid_usage_id mt_grabbed_usages[] = {
  419. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  420. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
  421. };
  422. static struct hid_driver mt_driver = {
  423. .name = "hid-multitouch",
  424. .id_table = mt_devices,
  425. .probe = mt_probe,
  426. .remove = mt_remove,
  427. .input_mapping = mt_input_mapping,
  428. .input_mapped = mt_input_mapped,
  429. .feature_mapping = mt_feature_mapping,
  430. .usage_table = mt_grabbed_usages,
  431. .event = mt_event,
  432. #ifdef CONFIG_PM
  433. .reset_resume = mt_reset_resume,
  434. #endif
  435. };
  436. static int __init mt_init(void)
  437. {
  438. return hid_register_driver(&mt_driver);
  439. }
  440. static void __exit mt_exit(void)
  441. {
  442. hid_unregister_driver(&mt_driver);
  443. }
  444. module_init(mt_init);
  445. module_exit(mt_exit);