hid-ntrig.c 26 KB

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