trackpoint.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Stephen Evanchik <evanchsa@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * Trademarks are the property of their respective owners.
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/delay.h>
  12. #include <linux/serio.h>
  13. #include <linux/module.h>
  14. #include <linux/input.h>
  15. #include <linux/libps2.h>
  16. #include <linux/proc_fs.h>
  17. #include <asm/uaccess.h>
  18. #include "psmouse.h"
  19. #include "trackpoint.h"
  20. /*
  21. * Power-on Reset: Resets all trackpoint parameters, including RAM values,
  22. * to defaults.
  23. * Returns zero on success, non-zero on failure.
  24. */
  25. static int trackpoint_power_on_reset(struct ps2dev *ps2dev)
  26. {
  27. unsigned char results[2];
  28. int tries = 0;
  29. /* Issue POR command, and repeat up to once if 0xFC00 received */
  30. do {
  31. if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
  32. ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 2, TP_POR)))
  33. return -1;
  34. } while (results[0] == 0xFC && results[1] == 0x00 && ++tries < 2);
  35. /* Check for success response -- 0xAA00 */
  36. if (results[0] != 0xAA || results[1] != 0x00)
  37. return -1;
  38. return 0;
  39. }
  40. /*
  41. * Device IO: read, write and toggle bit
  42. */
  43. static int trackpoint_read(struct ps2dev *ps2dev,
  44. unsigned char loc, unsigned char *results)
  45. {
  46. if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
  47. ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 1, loc))) {
  48. return -1;
  49. }
  50. return 0;
  51. }
  52. static int trackpoint_write(struct ps2dev *ps2dev,
  53. unsigned char loc, unsigned char val)
  54. {
  55. if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
  56. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_WRITE_MEM)) ||
  57. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
  58. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, val))) {
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. static int trackpoint_toggle_bit(struct ps2dev *ps2dev,
  64. unsigned char loc, unsigned char mask)
  65. {
  66. /* Bad things will happen if the loc param isn't in this range */
  67. if (loc < 0x20 || loc >= 0x2F)
  68. return -1;
  69. if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
  70. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_TOGGLE)) ||
  71. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
  72. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, mask))) {
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. static int trackpoint_update_bit(struct ps2dev *ps2dev, unsigned char loc,
  78. unsigned char mask, unsigned char value)
  79. {
  80. int retval = 0;
  81. unsigned char data;
  82. trackpoint_read(ps2dev, loc, &data);
  83. if (((data & mask) == mask) != !!value)
  84. retval = trackpoint_toggle_bit(ps2dev, loc, mask);
  85. return retval;
  86. }
  87. /*
  88. * Trackpoint-specific attributes
  89. */
  90. struct trackpoint_attr_data {
  91. size_t field_offset;
  92. unsigned char command;
  93. unsigned char mask;
  94. unsigned char inverted;
  95. unsigned char power_on_default;
  96. };
  97. static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse, void *data, char *buf)
  98. {
  99. struct trackpoint_data *tp = psmouse->private;
  100. struct trackpoint_attr_data *attr = data;
  101. unsigned char value = *(unsigned char *)((char *)tp + attr->field_offset);
  102. if (attr->inverted)
  103. value = !value;
  104. return sprintf(buf, "%u\n", value);
  105. }
  106. static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
  107. const char *buf, size_t count)
  108. {
  109. struct trackpoint_data *tp = psmouse->private;
  110. struct trackpoint_attr_data *attr = data;
  111. unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
  112. unsigned char value;
  113. int err;
  114. err = kstrtou8(buf, 10, &value);
  115. if (err)
  116. return err;
  117. *field = value;
  118. trackpoint_write(&psmouse->ps2dev, attr->command, value);
  119. return count;
  120. }
  121. #define TRACKPOINT_INT_ATTR(_name, _command, _default) \
  122. static struct trackpoint_attr_data trackpoint_attr_##_name = { \
  123. .field_offset = offsetof(struct trackpoint_data, _name), \
  124. .command = _command, \
  125. .power_on_default = _default, \
  126. }; \
  127. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  128. &trackpoint_attr_##_name, \
  129. trackpoint_show_int_attr, trackpoint_set_int_attr)
  130. static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
  131. const char *buf, size_t count)
  132. {
  133. struct trackpoint_data *tp = psmouse->private;
  134. struct trackpoint_attr_data *attr = data;
  135. unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
  136. unsigned int value;
  137. int err;
  138. err = kstrtouint(buf, 10, &value);
  139. if (err)
  140. return err;
  141. if (value > 1)
  142. return -EINVAL;
  143. if (attr->inverted)
  144. value = !value;
  145. if (*field != value) {
  146. *field = value;
  147. trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask);
  148. }
  149. return count;
  150. }
  151. #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv, _default) \
  152. static struct trackpoint_attr_data trackpoint_attr_##_name = { \
  153. .field_offset = offsetof(struct trackpoint_data, \
  154. _name), \
  155. .command = _command, \
  156. .mask = _mask, \
  157. .inverted = _inv, \
  158. .power_on_default = _default, \
  159. }; \
  160. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  161. &trackpoint_attr_##_name, \
  162. trackpoint_show_int_attr, trackpoint_set_bit_attr)
  163. #define TRACKPOINT_UPDATE_BIT(_psmouse, _tp, _name) \
  164. do { \
  165. struct trackpoint_attr_data *_attr = &trackpoint_attr_##_name; \
  166. \
  167. trackpoint_update_bit(&_psmouse->ps2dev, \
  168. _attr->command, _attr->mask, _tp->_name); \
  169. } while (0)
  170. #define TRACKPOINT_UPDATE(_power_on, _psmouse, _tp, _name) \
  171. do { \
  172. if (!_power_on || \
  173. _tp->_name != trackpoint_attr_##_name.power_on_default) { \
  174. if (!trackpoint_attr_##_name.mask) \
  175. trackpoint_write(&_psmouse->ps2dev, \
  176. trackpoint_attr_##_name.command, \
  177. _tp->_name); \
  178. else \
  179. TRACKPOINT_UPDATE_BIT(_psmouse, _tp, _name); \
  180. } \
  181. } while (0)
  182. #define TRACKPOINT_SET_POWER_ON_DEFAULT(_tp, _name) \
  183. (_tp->_name = trackpoint_attr_##_name.power_on_default)
  184. TRACKPOINT_INT_ATTR(sensitivity, TP_SENS, TP_DEF_SENS);
  185. TRACKPOINT_INT_ATTR(speed, TP_SPEED, TP_DEF_SPEED);
  186. TRACKPOINT_INT_ATTR(inertia, TP_INERTIA, TP_DEF_INERTIA);
  187. TRACKPOINT_INT_ATTR(reach, TP_REACH, TP_DEF_REACH);
  188. TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS, TP_DEF_DRAGHYS);
  189. TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG, TP_DEF_MINDRAG);
  190. TRACKPOINT_INT_ATTR(thresh, TP_THRESH, TP_DEF_THRESH);
  191. TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH, TP_DEF_UP_THRESH);
  192. TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME, TP_DEF_Z_TIME);
  193. TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV, TP_DEF_JENKS_CURV);
  194. TRACKPOINT_INT_ATTR(drift_time, TP_DRIFT_TIME, TP_DEF_DRIFT_TIME);
  195. TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, 0,
  196. TP_DEF_PTSON);
  197. TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, 0,
  198. TP_DEF_SKIPBACK);
  199. TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, 1,
  200. TP_DEF_EXT_DEV);
  201. static struct attribute *trackpoint_attrs[] = {
  202. &psmouse_attr_sensitivity.dattr.attr,
  203. &psmouse_attr_speed.dattr.attr,
  204. &psmouse_attr_inertia.dattr.attr,
  205. &psmouse_attr_reach.dattr.attr,
  206. &psmouse_attr_draghys.dattr.attr,
  207. &psmouse_attr_mindrag.dattr.attr,
  208. &psmouse_attr_thresh.dattr.attr,
  209. &psmouse_attr_upthresh.dattr.attr,
  210. &psmouse_attr_ztime.dattr.attr,
  211. &psmouse_attr_jenks.dattr.attr,
  212. &psmouse_attr_drift_time.dattr.attr,
  213. &psmouse_attr_press_to_select.dattr.attr,
  214. &psmouse_attr_skipback.dattr.attr,
  215. &psmouse_attr_ext_dev.dattr.attr,
  216. NULL
  217. };
  218. static struct attribute_group trackpoint_attr_group = {
  219. .attrs = trackpoint_attrs,
  220. };
  221. static int trackpoint_start_protocol(struct psmouse *psmouse, unsigned char *firmware_id)
  222. {
  223. unsigned char param[2] = { 0 };
  224. if (ps2_command(&psmouse->ps2dev, param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
  225. return -1;
  226. /* add new TP ID. */
  227. if (!(param[0] & TP_MAGIC_IDENT))
  228. return -1;
  229. if (firmware_id)
  230. *firmware_id = param[1];
  231. return 0;
  232. }
  233. /*
  234. * Write parameters to trackpad.
  235. * in_power_on_state: Set to true if TP is in default / power-on state (ex. if
  236. * power-on reset was run). If so, values will only be
  237. * written to TP if they differ from power-on default.
  238. */
  239. static int trackpoint_sync(struct psmouse *psmouse, bool in_power_on_state)
  240. {
  241. struct trackpoint_data *tp = psmouse->private;
  242. if (!in_power_on_state) {
  243. /*
  244. * Disable features that may make device unusable
  245. * with this driver.
  246. */
  247. trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND,
  248. TP_MASK_TWOHAND, TP_DEF_TWOHAND);
  249. trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG,
  250. TP_MASK_SOURCE_TAG, TP_DEF_SOURCE_TAG);
  251. trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_MB,
  252. TP_MASK_MB, TP_DEF_MB);
  253. }
  254. /*
  255. * These properties can be changed in this driver. Only
  256. * configure them if the values are non-default or if the TP is in
  257. * an unknown state.
  258. */
  259. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, sensitivity);
  260. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, inertia);
  261. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, speed);
  262. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, reach);
  263. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, draghys);
  264. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, mindrag);
  265. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, thresh);
  266. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, upthresh);
  267. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ztime);
  268. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, jenks);
  269. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, drift_time);
  270. /* toggles */
  271. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, press_to_select);
  272. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, skipback);
  273. TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ext_dev);
  274. return 0;
  275. }
  276. static void trackpoint_defaults(struct trackpoint_data *tp)
  277. {
  278. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, sensitivity);
  279. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, speed);
  280. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, reach);
  281. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, draghys);
  282. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, mindrag);
  283. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, thresh);
  284. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, upthresh);
  285. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ztime);
  286. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, jenks);
  287. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, drift_time);
  288. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, inertia);
  289. /* toggles */
  290. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, press_to_select);
  291. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, skipback);
  292. TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ext_dev);
  293. }
  294. static void trackpoint_disconnect(struct psmouse *psmouse)
  295. {
  296. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj, &trackpoint_attr_group);
  297. kfree(psmouse->private);
  298. psmouse->private = NULL;
  299. }
  300. static int trackpoint_reconnect(struct psmouse *psmouse)
  301. {
  302. int reset_fail;
  303. if (trackpoint_start_protocol(psmouse, NULL))
  304. return -1;
  305. reset_fail = trackpoint_power_on_reset(&psmouse->ps2dev);
  306. if (trackpoint_sync(psmouse, !reset_fail))
  307. return -1;
  308. return 0;
  309. }
  310. int trackpoint_detect(struct psmouse *psmouse, bool set_properties)
  311. {
  312. struct ps2dev *ps2dev = &psmouse->ps2dev;
  313. unsigned char firmware_id;
  314. unsigned char button_info;
  315. int error;
  316. if (trackpoint_start_protocol(psmouse, &firmware_id))
  317. return -1;
  318. if (!set_properties)
  319. return 0;
  320. if (trackpoint_read(&psmouse->ps2dev, TP_EXT_BTN, &button_info)) {
  321. psmouse_warn(psmouse, "failed to get extended button data, assuming 3 buttons\n");
  322. button_info = 0x33;
  323. } else if (!button_info) {
  324. psmouse_warn(psmouse, "got 0 in extended button data, assuming 3 buttons\n");
  325. button_info = 0x33;
  326. }
  327. psmouse->private = kzalloc(sizeof(struct trackpoint_data), GFP_KERNEL);
  328. if (!psmouse->private)
  329. return -ENOMEM;
  330. psmouse->vendor = "IBM";
  331. psmouse->name = "TrackPoint";
  332. psmouse->reconnect = trackpoint_reconnect;
  333. psmouse->disconnect = trackpoint_disconnect;
  334. if ((button_info & 0x0f) >= 3)
  335. __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
  336. __set_bit(INPUT_PROP_POINTER, psmouse->dev->propbit);
  337. __set_bit(INPUT_PROP_POINTING_STICK, psmouse->dev->propbit);
  338. trackpoint_defaults(psmouse->private);
  339. error = trackpoint_power_on_reset(&psmouse->ps2dev);
  340. /* Write defaults to TP only if reset fails. */
  341. if (error)
  342. trackpoint_sync(psmouse, false);
  343. error = sysfs_create_group(&ps2dev->serio->dev.kobj, &trackpoint_attr_group);
  344. if (error) {
  345. psmouse_err(psmouse,
  346. "failed to create sysfs attributes, error: %d\n",
  347. error);
  348. kfree(psmouse->private);
  349. psmouse->private = NULL;
  350. return -1;
  351. }
  352. psmouse_info(psmouse,
  353. "IBM TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
  354. firmware_id,
  355. (button_info & 0xf0) >> 4, button_info & 0x0f);
  356. return 0;
  357. }