hid-ntrig.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*
  2. * HID driver for N-Trig touchscreens
  3. *
  4. * Copyright (c) 2008-2010 Rafi Rubin
  5. * Copyright (c) 2009-2010 Stephane Chatty
  6. *
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/hid.h>
  16. #include <linux/usb.h>
  17. #include "usbhid/usbhid.h"
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include "hid-ids.h"
  21. #define NTRIG_DUPLICATE_USAGES 0x001
  22. static unsigned int min_width;
  23. static unsigned int min_height;
  24. static unsigned int activate_slack = 1;
  25. static unsigned int deactivate_slack = 4;
  26. static unsigned int activation_width = 64;
  27. static unsigned int activation_height = 32;
  28. struct ntrig_data {
  29. /* Incoming raw values for a single contact */
  30. __u16 x, y, w, h;
  31. __u16 id;
  32. bool tipswitch;
  33. bool confidence;
  34. bool first_contact_touch;
  35. bool reading_mt;
  36. __u8 mt_footer[4];
  37. __u8 mt_foot_count;
  38. /* The current activation state. */
  39. __s8 act_state;
  40. /* Empty frames to ignore before recognizing the end of activity */
  41. __s8 deactivate_slack;
  42. /* Frames to ignore before acknowledging the start of activity */
  43. __s8 activate_slack;
  44. /* Minimum size contact to accept */
  45. __u16 min_width;
  46. __u16 min_height;
  47. /* Threshold to override activation slack */
  48. __u16 activation_width;
  49. __u16 activation_height;
  50. __u16 sensor_logical_width;
  51. __u16 sensor_logical_height;
  52. __u16 sensor_physical_width;
  53. __u16 sensor_physical_height;
  54. };
  55. static ssize_t show_phys_width(struct device *dev,
  56. struct device_attribute *attr,
  57. char *buf)
  58. {
  59. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  60. struct ntrig_data *nd = hid_get_drvdata(hdev);
  61. return sprintf(buf, "%d\n", nd->sensor_physical_width);
  62. }
  63. static DEVICE_ATTR(sensor_physical_width, S_IRUGO, show_phys_width, NULL);
  64. static ssize_t show_phys_height(struct device *dev,
  65. struct device_attribute *attr,
  66. char *buf)
  67. {
  68. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  69. struct ntrig_data *nd = hid_get_drvdata(hdev);
  70. return sprintf(buf, "%d\n", nd->sensor_physical_height);
  71. }
  72. static DEVICE_ATTR(sensor_physical_height, S_IRUGO, show_phys_height, NULL);
  73. static ssize_t show_log_width(struct device *dev,
  74. struct device_attribute *attr,
  75. char *buf)
  76. {
  77. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  78. struct ntrig_data *nd = hid_get_drvdata(hdev);
  79. return sprintf(buf, "%d\n", nd->sensor_logical_width);
  80. }
  81. static DEVICE_ATTR(sensor_logical_width, S_IRUGO, show_log_width, NULL);
  82. static ssize_t show_log_height(struct device *dev,
  83. struct device_attribute *attr,
  84. char *buf)
  85. {
  86. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  87. struct ntrig_data *nd = hid_get_drvdata(hdev);
  88. return sprintf(buf, "%d\n", nd->sensor_logical_height);
  89. }
  90. static DEVICE_ATTR(sensor_logical_height, S_IRUGO, show_log_height, NULL);
  91. static ssize_t show_min_width(struct device *dev,
  92. struct device_attribute *attr,
  93. char *buf)
  94. {
  95. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  96. struct ntrig_data *nd = hid_get_drvdata(hdev);
  97. return sprintf(buf, "%d\n", nd->min_width *
  98. nd->sensor_physical_width /
  99. nd->sensor_logical_width);
  100. }
  101. static ssize_t set_min_width(struct device *dev,
  102. struct device_attribute *attr,
  103. const char *buf, size_t count)
  104. {
  105. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  106. struct ntrig_data *nd = hid_get_drvdata(hdev);
  107. unsigned long val;
  108. if (strict_strtoul(buf, 0, &val))
  109. return -EINVAL;
  110. if (val > nd->sensor_physical_width)
  111. return -EINVAL;
  112. nd->min_width = val * nd->sensor_logical_width /
  113. nd->sensor_physical_width;
  114. return count;
  115. }
  116. static DEVICE_ATTR(min_width, S_IWUSR | S_IRUGO, show_min_width, set_min_width);
  117. static ssize_t show_min_height(struct device *dev,
  118. struct device_attribute *attr,
  119. char *buf)
  120. {
  121. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  122. struct ntrig_data *nd = hid_get_drvdata(hdev);
  123. return sprintf(buf, "%d\n", nd->min_height *
  124. nd->sensor_physical_height /
  125. nd->sensor_logical_height);
  126. }
  127. static ssize_t set_min_height(struct device *dev,
  128. struct device_attribute *attr,
  129. const char *buf, size_t count)
  130. {
  131. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  132. struct ntrig_data *nd = hid_get_drvdata(hdev);
  133. unsigned long val;
  134. if (strict_strtoul(buf, 0, &val))
  135. return -EINVAL;
  136. if (val > nd->sensor_physical_height)
  137. return -EINVAL;
  138. nd->min_height = val * nd->sensor_logical_height /
  139. nd->sensor_physical_height;
  140. return count;
  141. }
  142. static DEVICE_ATTR(min_height, S_IWUSR | S_IRUGO, show_min_height,
  143. set_min_height);
  144. static ssize_t show_activate_slack(struct device *dev,
  145. struct device_attribute *attr,
  146. char *buf)
  147. {
  148. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  149. struct ntrig_data *nd = hid_get_drvdata(hdev);
  150. return sprintf(buf, "%d\n", nd->activate_slack);
  151. }
  152. static ssize_t set_activate_slack(struct device *dev,
  153. struct device_attribute *attr,
  154. const char *buf, size_t count)
  155. {
  156. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  157. struct ntrig_data *nd = hid_get_drvdata(hdev);
  158. unsigned long val;
  159. if (strict_strtoul(buf, 0, &val))
  160. return -EINVAL;
  161. if (val > 0x7f)
  162. return -EINVAL;
  163. nd->activate_slack = val;
  164. return count;
  165. }
  166. static DEVICE_ATTR(activate_slack, S_IWUSR | S_IRUGO, show_activate_slack,
  167. set_activate_slack);
  168. static ssize_t show_activation_width(struct device *dev,
  169. struct device_attribute *attr,
  170. char *buf)
  171. {
  172. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  173. struct ntrig_data *nd = hid_get_drvdata(hdev);
  174. return sprintf(buf, "%d\n", nd->activation_width *
  175. nd->sensor_physical_width /
  176. nd->sensor_logical_width);
  177. }
  178. static ssize_t set_activation_width(struct device *dev,
  179. struct device_attribute *attr,
  180. const char *buf, size_t count)
  181. {
  182. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  183. struct ntrig_data *nd = hid_get_drvdata(hdev);
  184. unsigned long val;
  185. if (strict_strtoul(buf, 0, &val))
  186. return -EINVAL;
  187. if (val > nd->sensor_physical_width)
  188. return -EINVAL;
  189. nd->activation_width = val * nd->sensor_logical_width /
  190. nd->sensor_physical_width;
  191. return count;
  192. }
  193. static DEVICE_ATTR(activation_width, S_IWUSR | S_IRUGO, show_activation_width,
  194. set_activation_width);
  195. static ssize_t show_activation_height(struct device *dev,
  196. struct device_attribute *attr,
  197. char *buf)
  198. {
  199. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  200. struct ntrig_data *nd = hid_get_drvdata(hdev);
  201. return sprintf(buf, "%d\n", nd->activation_height *
  202. nd->sensor_physical_height /
  203. nd->sensor_logical_height);
  204. }
  205. static ssize_t set_activation_height(struct device *dev,
  206. struct device_attribute *attr,
  207. const char *buf, size_t count)
  208. {
  209. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  210. struct ntrig_data *nd = hid_get_drvdata(hdev);
  211. unsigned long val;
  212. if (strict_strtoul(buf, 0, &val))
  213. return -EINVAL;
  214. if (val > nd->sensor_physical_height)
  215. return -EINVAL;
  216. nd->activation_height = val * nd->sensor_logical_height /
  217. nd->sensor_physical_height;
  218. return count;
  219. }
  220. static DEVICE_ATTR(activation_height, S_IWUSR | S_IRUGO,
  221. show_activation_height, set_activation_height);
  222. static ssize_t show_deactivate_slack(struct device *dev,
  223. struct device_attribute *attr,
  224. char *buf)
  225. {
  226. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  227. struct ntrig_data *nd = hid_get_drvdata(hdev);
  228. return sprintf(buf, "%d\n", -nd->deactivate_slack);
  229. }
  230. static ssize_t set_deactivate_slack(struct device *dev,
  231. struct device_attribute *attr,
  232. const char *buf, size_t count)
  233. {
  234. struct hid_device *hdev = container_of(dev, struct hid_device, dev);
  235. struct ntrig_data *nd = hid_get_drvdata(hdev);
  236. unsigned long val;
  237. if (strict_strtoul(buf, 0, &val))
  238. return -EINVAL;
  239. /*
  240. * No more than 8 terminal frames have been observed so far
  241. * and higher slack is highly likely to leave the single
  242. * touch emulation stuck down.
  243. */
  244. if (val > 7)
  245. return -EINVAL;
  246. nd->deactivate_slack = -val;
  247. return count;
  248. }
  249. static DEVICE_ATTR(deactivate_slack, S_IWUSR | S_IRUGO, show_deactivate_slack,
  250. set_deactivate_slack);
  251. static struct attribute *sysfs_attrs[] = {
  252. &dev_attr_sensor_physical_width.attr,
  253. &dev_attr_sensor_physical_height.attr,
  254. &dev_attr_sensor_logical_width.attr,
  255. &dev_attr_sensor_logical_height.attr,
  256. &dev_attr_min_height.attr,
  257. &dev_attr_min_width.attr,
  258. &dev_attr_activate_slack.attr,
  259. &dev_attr_activation_width.attr,
  260. &dev_attr_activation_height.attr,
  261. &dev_attr_deactivate_slack.attr,
  262. NULL
  263. };
  264. static struct attribute_group ntrig_attribute_group = {
  265. .attrs = sysfs_attrs
  266. };
  267. /*
  268. * this driver is aimed at two firmware versions in circulation:
  269. * - dual pen/finger single touch
  270. * - finger multitouch, pen not working
  271. */
  272. static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  273. struct hid_field *field, struct hid_usage *usage,
  274. unsigned long **bit, int *max)
  275. {
  276. struct ntrig_data *nd = hid_get_drvdata(hdev);
  277. /* No special mappings needed for the pen and single touch */
  278. if (field->physical)
  279. return 0;
  280. switch (usage->hid & HID_USAGE_PAGE) {
  281. case HID_UP_GENDESK:
  282. switch (usage->hid) {
  283. case HID_GD_X:
  284. hid_map_usage(hi, usage, bit, max,
  285. EV_ABS, ABS_MT_POSITION_X);
  286. input_set_abs_params(hi->input, ABS_X,
  287. field->logical_minimum,
  288. field->logical_maximum, 0, 0);
  289. if (!nd->sensor_logical_width) {
  290. nd->sensor_logical_width =
  291. field->logical_maximum -
  292. field->logical_minimum;
  293. nd->sensor_physical_width =
  294. field->physical_maximum -
  295. field->physical_minimum;
  296. nd->activation_width = activation_width *
  297. nd->sensor_logical_width /
  298. nd->sensor_physical_width;
  299. nd->min_width = min_width *
  300. nd->sensor_logical_width /
  301. nd->sensor_physical_width;
  302. }
  303. return 1;
  304. case HID_GD_Y:
  305. hid_map_usage(hi, usage, bit, max,
  306. EV_ABS, ABS_MT_POSITION_Y);
  307. input_set_abs_params(hi->input, ABS_Y,
  308. field->logical_minimum,
  309. field->logical_maximum, 0, 0);
  310. if (!nd->sensor_logical_height) {
  311. nd->sensor_logical_height =
  312. field->logical_maximum -
  313. field->logical_minimum;
  314. nd->sensor_physical_height =
  315. field->physical_maximum -
  316. field->physical_minimum;
  317. nd->activation_height = activation_height *
  318. nd->sensor_logical_height /
  319. nd->sensor_physical_height;
  320. nd->min_height = min_height *
  321. nd->sensor_logical_height /
  322. nd->sensor_physical_height;
  323. }
  324. return 1;
  325. }
  326. return 0;
  327. case HID_UP_DIGITIZER:
  328. switch (usage->hid) {
  329. /* we do not want to map these for now */
  330. case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
  331. case HID_DG_INPUTMODE:
  332. case HID_DG_DEVICEINDEX:
  333. case HID_DG_CONTACTMAX:
  334. return -1;
  335. /* width/height mapped on TouchMajor/TouchMinor/Orientation */
  336. case HID_DG_WIDTH:
  337. hid_map_usage(hi, usage, bit, max,
  338. EV_ABS, ABS_MT_TOUCH_MAJOR);
  339. return 1;
  340. case HID_DG_HEIGHT:
  341. hid_map_usage(hi, usage, bit, max,
  342. EV_ABS, ABS_MT_TOUCH_MINOR);
  343. input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
  344. 0, 1, 0, 0);
  345. return 1;
  346. }
  347. return 0;
  348. case 0xff000000:
  349. /* we do not want to map these: no input-oriented meaning */
  350. return -1;
  351. }
  352. return 0;
  353. }
  354. static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  355. struct hid_field *field, struct hid_usage *usage,
  356. unsigned long **bit, int *max)
  357. {
  358. /* No special mappings needed for the pen and single touch */
  359. if (field->physical)
  360. return 0;
  361. if (usage->type == EV_KEY || usage->type == EV_REL
  362. || usage->type == EV_ABS)
  363. clear_bit(usage->code, *bit);
  364. return 0;
  365. }
  366. /*
  367. * this function is called upon all reports
  368. * so that we can filter contact point information,
  369. * decide whether we are in multi or single touch mode
  370. * and call input_mt_sync after each point if necessary
  371. */
  372. static int ntrig_event (struct hid_device *hid, struct hid_field *field,
  373. struct hid_usage *usage, __s32 value)
  374. {
  375. struct input_dev *input = field->hidinput->input;
  376. struct ntrig_data *nd = hid_get_drvdata(hid);
  377. /* No special handling needed for the pen */
  378. if (field->application == HID_DG_PEN)
  379. return 0;
  380. if (hid->claimed & HID_CLAIMED_INPUT) {
  381. switch (usage->hid) {
  382. case 0xff000001:
  383. /* Tag indicating the start of a multitouch group */
  384. nd->reading_mt = 1;
  385. nd->first_contact_touch = 0;
  386. break;
  387. case HID_DG_TIPSWITCH:
  388. nd->tipswitch = value;
  389. /* Prevent emission of touch until validated */
  390. return 1;
  391. case HID_DG_CONFIDENCE:
  392. nd->confidence = value;
  393. break;
  394. case HID_GD_X:
  395. nd->x = value;
  396. /* Clear the contact footer */
  397. nd->mt_foot_count = 0;
  398. break;
  399. case HID_GD_Y:
  400. nd->y = value;
  401. break;
  402. case HID_DG_CONTACTID:
  403. nd->id = value;
  404. break;
  405. case HID_DG_WIDTH:
  406. nd->w = value;
  407. break;
  408. case HID_DG_HEIGHT:
  409. nd->h = value;
  410. /*
  411. * when in single touch mode, this is the last
  412. * report received in a finger event. We want
  413. * to emit a normal (X, Y) position
  414. */
  415. if (!nd->reading_mt) {
  416. /*
  417. * TipSwitch indicates the presence of a
  418. * finger in single touch mode.
  419. */
  420. input_report_key(input, BTN_TOUCH,
  421. nd->tipswitch);
  422. input_report_key(input, BTN_TOOL_DOUBLETAP,
  423. nd->tipswitch);
  424. input_event(input, EV_ABS, ABS_X, nd->x);
  425. input_event(input, EV_ABS, ABS_Y, nd->y);
  426. }
  427. break;
  428. case 0xff000002:
  429. /*
  430. * we receive this when the device is in multitouch
  431. * mode. The first of the three values tagged with
  432. * this usage tells if the contact point is real
  433. * or a placeholder
  434. */
  435. /* Shouldn't get more than 4 footer packets, so skip */
  436. if (nd->mt_foot_count >= 4)
  437. break;
  438. nd->mt_footer[nd->mt_foot_count++] = value;
  439. /* if the footer isn't complete break */
  440. if (nd->mt_foot_count != 4)
  441. break;
  442. /* Pen activity signal. */
  443. if (nd->mt_footer[2]) {
  444. /*
  445. * When the pen deactivates touch, we see a
  446. * bogus frame with ContactCount > 0.
  447. * We can
  448. * save a bit of work by ensuring act_state < 0
  449. * even if deactivation slack is turned off.
  450. */
  451. nd->act_state = deactivate_slack - 1;
  452. nd->confidence = 0;
  453. break;
  454. }
  455. /*
  456. * The first footer value indicates the presence of a
  457. * finger.
  458. */
  459. if (nd->mt_footer[0]) {
  460. /*
  461. * We do not want to process contacts under
  462. * the size threshold, but do not want to
  463. * ignore them for activation state
  464. */
  465. if (nd->w < nd->min_width ||
  466. nd->h < nd->min_height)
  467. nd->confidence = 0;
  468. } else
  469. break;
  470. if (nd->act_state > 0) {
  471. /*
  472. * Contact meets the activation size threshold
  473. */
  474. if (nd->w >= nd->activation_width &&
  475. nd->h >= nd->activation_height) {
  476. if (nd->id)
  477. /*
  478. * first contact, activate now
  479. */
  480. nd->act_state = 0;
  481. else {
  482. /*
  483. * avoid corrupting this frame
  484. * but ensure next frame will
  485. * be active
  486. */
  487. nd->act_state = 1;
  488. break;
  489. }
  490. } else
  491. /*
  492. * Defer adjusting the activation state
  493. * until the end of the frame.
  494. */
  495. break;
  496. }
  497. /* Discarding this contact */
  498. if (!nd->confidence)
  499. break;
  500. /* emit a normal (X, Y) for the first point only */
  501. if (nd->id == 0) {
  502. /*
  503. * TipSwitch is superfluous in multitouch
  504. * mode. The footer events tell us
  505. * if there is a finger on the screen or
  506. * not.
  507. */
  508. nd->first_contact_touch = nd->confidence;
  509. input_event(input, EV_ABS, ABS_X, nd->x);
  510. input_event(input, EV_ABS, ABS_Y, nd->y);
  511. }
  512. /* Emit MT events */
  513. input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
  514. input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
  515. /*
  516. * Translate from height and width to size
  517. * and orientation.
  518. */
  519. if (nd->w > nd->h) {
  520. input_event(input, EV_ABS,
  521. ABS_MT_ORIENTATION, 1);
  522. input_event(input, EV_ABS,
  523. ABS_MT_TOUCH_MAJOR, nd->w);
  524. input_event(input, EV_ABS,
  525. ABS_MT_TOUCH_MINOR, nd->h);
  526. } else {
  527. input_event(input, EV_ABS,
  528. ABS_MT_ORIENTATION, 0);
  529. input_event(input, EV_ABS,
  530. ABS_MT_TOUCH_MAJOR, nd->h);
  531. input_event(input, EV_ABS,
  532. ABS_MT_TOUCH_MINOR, nd->w);
  533. }
  534. input_mt_sync(field->hidinput->input);
  535. break;
  536. case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
  537. if (!nd->reading_mt) /* Just to be sure */
  538. break;
  539. nd->reading_mt = 0;
  540. /*
  541. * Activation state machine logic:
  542. *
  543. * Fundamental states:
  544. * state > 0: Inactive
  545. * state <= 0: Active
  546. * state < -deactivate_slack:
  547. * Pen termination of touch
  548. *
  549. * Specific values of interest
  550. * state == activate_slack
  551. * no valid input since the last reset
  552. *
  553. * state == 0
  554. * general operational state
  555. *
  556. * state == -deactivate_slack
  557. * read sufficient empty frames to accept
  558. * the end of input and reset
  559. */
  560. if (nd->act_state > 0) { /* Currently inactive */
  561. if (value)
  562. /*
  563. * Consider each live contact as
  564. * evidence of intentional activity.
  565. */
  566. nd->act_state = (nd->act_state > value)
  567. ? nd->act_state - value
  568. : 0;
  569. else
  570. /*
  571. * Empty frame before we hit the
  572. * activity threshold, reset.
  573. */
  574. nd->act_state = nd->activate_slack;
  575. /*
  576. * Entered this block inactive and no
  577. * coordinates sent this frame, so hold off
  578. * on button state.
  579. */
  580. break;
  581. } else { /* Currently active */
  582. if (value && nd->act_state >=
  583. nd->deactivate_slack)
  584. /*
  585. * Live point: clear accumulated
  586. * deactivation count.
  587. */
  588. nd->act_state = 0;
  589. else if (nd->act_state <= nd->deactivate_slack)
  590. /*
  591. * We've consumed the deactivation
  592. * slack, time to deactivate and reset.
  593. */
  594. nd->act_state =
  595. nd->activate_slack;
  596. else { /* Move towards deactivation */
  597. nd->act_state--;
  598. break;
  599. }
  600. }
  601. if (nd->first_contact_touch && nd->act_state <= 0) {
  602. /*
  603. * Check to see if we're ready to start
  604. * emitting touch events.
  605. *
  606. * Note: activation slack will decrease over
  607. * the course of the frame, and it will be
  608. * inconsistent from the start to the end of
  609. * the frame. However if the frame starts
  610. * with slack, first_contact_touch will still
  611. * be 0 and we will not get to this point.
  612. */
  613. input_report_key(input, BTN_TOOL_DOUBLETAP, 1);
  614. input_report_key(input, BTN_TOUCH, 1);
  615. } else {
  616. input_report_key(input, BTN_TOOL_DOUBLETAP, 0);
  617. input_report_key(input, BTN_TOUCH, 0);
  618. }
  619. break;
  620. default:
  621. /* fall-back to the generic hidinput handling */
  622. return 0;
  623. }
  624. }
  625. /* we have handled the hidinput part, now remains hiddev */
  626. if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
  627. hid->hiddev_hid_event(hid, field, usage, value);
  628. return 1;
  629. }
  630. static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
  631. {
  632. int ret;
  633. struct ntrig_data *nd;
  634. struct hid_input *hidinput;
  635. struct input_dev *input;
  636. struct hid_report *report;
  637. if (id->driver_data)
  638. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  639. nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
  640. if (!nd) {
  641. dev_err(&hdev->dev, "cannot allocate N-Trig data\n");
  642. return -ENOMEM;
  643. }
  644. nd->reading_mt = 0;
  645. nd->min_width = 0;
  646. nd->min_height = 0;
  647. nd->activate_slack = activate_slack;
  648. nd->act_state = activate_slack;
  649. nd->deactivate_slack = -deactivate_slack;
  650. nd->sensor_logical_width = 0;
  651. nd->sensor_logical_height = 0;
  652. nd->sensor_physical_width = 0;
  653. nd->sensor_physical_height = 0;
  654. hid_set_drvdata(hdev, nd);
  655. ret = hid_parse(hdev);
  656. if (ret) {
  657. dev_err(&hdev->dev, "parse failed\n");
  658. goto err_free;
  659. }
  660. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  661. if (ret) {
  662. dev_err(&hdev->dev, "hw start failed\n");
  663. goto err_free;
  664. }
  665. list_for_each_entry(hidinput, &hdev->inputs, list) {
  666. if (hidinput->report->maxfield < 1)
  667. continue;
  668. input = hidinput->input;
  669. switch (hidinput->report->field[0]->application) {
  670. case HID_DG_PEN:
  671. input->name = "N-Trig Pen";
  672. break;
  673. case HID_DG_TOUCHSCREEN:
  674. /* These keys are redundant for fingers, clear them
  675. * to prevent incorrect identification */
  676. __clear_bit(BTN_TOOL_PEN, input->keybit);
  677. __clear_bit(BTN_TOOL_FINGER, input->keybit);
  678. __clear_bit(BTN_0, input->keybit);
  679. __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
  680. /*
  681. * The physical touchscreen (single touch)
  682. * input has a value for physical, whereas
  683. * the multitouch only has logical input
  684. * fields.
  685. */
  686. input->name =
  687. (hidinput->report->field[0]
  688. ->physical) ?
  689. "N-Trig Touchscreen" :
  690. "N-Trig MultiTouch";
  691. break;
  692. }
  693. }
  694. /* This is needed for devices with more recent firmware versions */
  695. report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
  696. if (report)
  697. usbhid_submit_report(hdev, report, USB_DIR_OUT);
  698. ret = sysfs_create_group(&hdev->dev.kobj,
  699. &ntrig_attribute_group);
  700. return 0;
  701. err_free:
  702. kfree(nd);
  703. return ret;
  704. }
  705. static void ntrig_remove(struct hid_device *hdev)
  706. {
  707. sysfs_remove_group(&hdev->dev.kobj,
  708. &ntrig_attribute_group);
  709. hid_hw_stop(hdev);
  710. kfree(hid_get_drvdata(hdev));
  711. }
  712. static const struct hid_device_id ntrig_devices[] = {
  713. { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
  714. .driver_data = NTRIG_DUPLICATE_USAGES },
  715. { }
  716. };
  717. MODULE_DEVICE_TABLE(hid, ntrig_devices);
  718. static const struct hid_usage_id ntrig_grabbed_usages[] = {
  719. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  720. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
  721. };
  722. static struct hid_driver ntrig_driver = {
  723. .name = "ntrig",
  724. .id_table = ntrig_devices,
  725. .probe = ntrig_probe,
  726. .remove = ntrig_remove,
  727. .input_mapping = ntrig_input_mapping,
  728. .input_mapped = ntrig_input_mapped,
  729. .usage_table = ntrig_grabbed_usages,
  730. .event = ntrig_event,
  731. };
  732. static int __init ntrig_init(void)
  733. {
  734. return hid_register_driver(&ntrig_driver);
  735. }
  736. static void __exit ntrig_exit(void)
  737. {
  738. hid_unregister_driver(&ntrig_driver);
  739. }
  740. module_init(ntrig_init);
  741. module_exit(ntrig_exit);
  742. MODULE_LICENSE("GPL");