hid-ntrig.c 27 KB

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