hid-ntrig.c 26 KB

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