hid-ntrig.c 26 KB

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