trackpoint.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/delay.h>
  11. #include <linux/serio.h>
  12. #include <linux/module.h>
  13. #include <linux/input.h>
  14. #include <linux/libps2.h>
  15. #include <linux/proc_fs.h>
  16. #include <asm/uaccess.h>
  17. #include "psmouse.h"
  18. #include "trackpoint.h"
  19. /*
  20. * Device IO: read, write and toggle bit
  21. */
  22. static int trackpoint_read(struct ps2dev *ps2dev, unsigned char loc, unsigned char *results)
  23. {
  24. if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
  25. ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 1, loc))) {
  26. return -1;
  27. }
  28. return 0;
  29. }
  30. static int trackpoint_write(struct ps2dev *ps2dev, unsigned char loc, unsigned char val)
  31. {
  32. if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
  33. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_WRITE_MEM)) ||
  34. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
  35. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, val))) {
  36. return -1;
  37. }
  38. return 0;
  39. }
  40. static int trackpoint_toggle_bit(struct ps2dev *ps2dev, unsigned char loc, unsigned char mask)
  41. {
  42. /* Bad things will happen if the loc param isn't in this range */
  43. if (loc < 0x20 || loc >= 0x2F)
  44. return -1;
  45. if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
  46. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_TOGGLE)) ||
  47. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
  48. ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, mask))) {
  49. return -1;
  50. }
  51. return 0;
  52. }
  53. /*
  54. * Trackpoint-specific attributes
  55. */
  56. struct trackpoint_attr_data {
  57. size_t field_offset;
  58. unsigned char command;
  59. unsigned char mask;
  60. unsigned char inverted;
  61. };
  62. static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse, void *data, char *buf)
  63. {
  64. struct trackpoint_data *tp = psmouse->private;
  65. struct trackpoint_attr_data *attr = data;
  66. unsigned char value = *(unsigned char *)((char *)tp + attr->field_offset);
  67. if (attr->inverted)
  68. value = !value;
  69. return sprintf(buf, "%u\n", value);
  70. }
  71. static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
  72. const char *buf, size_t count)
  73. {
  74. struct trackpoint_data *tp = psmouse->private;
  75. struct trackpoint_attr_data *attr = data;
  76. unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
  77. unsigned long value;
  78. if (strict_strtoul(buf, 10, &value) || value > 255)
  79. return -EINVAL;
  80. *field = value;
  81. trackpoint_write(&psmouse->ps2dev, attr->command, value);
  82. return count;
  83. }
  84. #define TRACKPOINT_INT_ATTR(_name, _command) \
  85. static struct trackpoint_attr_data trackpoint_attr_##_name = { \
  86. .field_offset = offsetof(struct trackpoint_data, _name), \
  87. .command = _command, \
  88. }; \
  89. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  90. &trackpoint_attr_##_name, \
  91. trackpoint_show_int_attr, trackpoint_set_int_attr)
  92. static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
  93. const char *buf, size_t count)
  94. {
  95. struct trackpoint_data *tp = psmouse->private;
  96. struct trackpoint_attr_data *attr = data;
  97. unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
  98. unsigned long value;
  99. if (strict_strtoul(buf, 10, &value) || value > 1)
  100. return -EINVAL;
  101. if (attr->inverted)
  102. value = !value;
  103. if (*field != value) {
  104. *field = value;
  105. trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask);
  106. }
  107. return count;
  108. }
  109. #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv) \
  110. static struct trackpoint_attr_data trackpoint_attr_##_name = { \
  111. .field_offset = offsetof(struct trackpoint_data, _name), \
  112. .command = _command, \
  113. .mask = _mask, \
  114. .inverted = _inv, \
  115. }; \
  116. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  117. &trackpoint_attr_##_name, \
  118. trackpoint_show_int_attr, trackpoint_set_bit_attr)
  119. TRACKPOINT_INT_ATTR(sensitivity, TP_SENS);
  120. TRACKPOINT_INT_ATTR(speed, TP_SPEED);
  121. TRACKPOINT_INT_ATTR(inertia, TP_INERTIA);
  122. TRACKPOINT_INT_ATTR(reach, TP_REACH);
  123. TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS);
  124. TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG);
  125. TRACKPOINT_INT_ATTR(thresh, TP_THRESH);
  126. TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH);
  127. TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME);
  128. TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV);
  129. TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, 0);
  130. TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, 0);
  131. TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, 1);
  132. static struct attribute *trackpoint_attrs[] = {
  133. &psmouse_attr_sensitivity.dattr.attr,
  134. &psmouse_attr_speed.dattr.attr,
  135. &psmouse_attr_inertia.dattr.attr,
  136. &psmouse_attr_reach.dattr.attr,
  137. &psmouse_attr_draghys.dattr.attr,
  138. &psmouse_attr_mindrag.dattr.attr,
  139. &psmouse_attr_thresh.dattr.attr,
  140. &psmouse_attr_upthresh.dattr.attr,
  141. &psmouse_attr_ztime.dattr.attr,
  142. &psmouse_attr_jenks.dattr.attr,
  143. &psmouse_attr_press_to_select.dattr.attr,
  144. &psmouse_attr_skipback.dattr.attr,
  145. &psmouse_attr_ext_dev.dattr.attr,
  146. NULL
  147. };
  148. static struct attribute_group trackpoint_attr_group = {
  149. .attrs = trackpoint_attrs,
  150. };
  151. static int trackpoint_start_protocol(struct psmouse *psmouse, unsigned char *firmware_id)
  152. {
  153. unsigned char param[2] = { 0 };
  154. if (ps2_command(&psmouse->ps2dev, param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
  155. return -1;
  156. if (param[0] != TP_MAGIC_IDENT)
  157. return -1;
  158. if (firmware_id)
  159. *firmware_id = param[1];
  160. return 0;
  161. }
  162. static int trackpoint_sync(struct psmouse *psmouse)
  163. {
  164. struct trackpoint_data *tp = psmouse->private;
  165. unsigned char toggle;
  166. /* Disable features that may make device unusable with this driver */
  167. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, &toggle);
  168. if (toggle & TP_MASK_TWOHAND)
  169. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, TP_MASK_TWOHAND);
  170. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, &toggle);
  171. if (toggle & TP_MASK_SOURCE_TAG)
  172. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, TP_MASK_SOURCE_TAG);
  173. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_MB, &toggle);
  174. if (toggle & TP_MASK_MB)
  175. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_MB, TP_MASK_MB);
  176. /* Push the config to the device */
  177. trackpoint_write(&psmouse->ps2dev, TP_SENS, tp->sensitivity);
  178. trackpoint_write(&psmouse->ps2dev, TP_INERTIA, tp->inertia);
  179. trackpoint_write(&psmouse->ps2dev, TP_SPEED, tp->speed);
  180. trackpoint_write(&psmouse->ps2dev, TP_REACH, tp->reach);
  181. trackpoint_write(&psmouse->ps2dev, TP_DRAGHYS, tp->draghys);
  182. trackpoint_write(&psmouse->ps2dev, TP_MINDRAG, tp->mindrag);
  183. trackpoint_write(&psmouse->ps2dev, TP_THRESH, tp->thresh);
  184. trackpoint_write(&psmouse->ps2dev, TP_UP_THRESH, tp->upthresh);
  185. trackpoint_write(&psmouse->ps2dev, TP_Z_TIME, tp->ztime);
  186. trackpoint_write(&psmouse->ps2dev, TP_JENKS_CURV, tp->jenks);
  187. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_PTSON, &toggle);
  188. if (((toggle & TP_MASK_PTSON) == TP_MASK_PTSON) != tp->press_to_select)
  189. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_PTSON, TP_MASK_PTSON);
  190. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, &toggle);
  191. if (((toggle & TP_MASK_SKIPBACK) == TP_MASK_SKIPBACK) != tp->skipback)
  192. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK);
  193. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, &toggle);
  194. if (((toggle & TP_MASK_EXT_DEV) == TP_MASK_EXT_DEV) != tp->ext_dev)
  195. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV);
  196. return 0;
  197. }
  198. static void trackpoint_defaults(struct trackpoint_data *tp)
  199. {
  200. tp->press_to_select = TP_DEF_PTSON;
  201. tp->sensitivity = TP_DEF_SENS;
  202. tp->speed = TP_DEF_SPEED;
  203. tp->reach = TP_DEF_REACH;
  204. tp->draghys = TP_DEF_DRAGHYS;
  205. tp->mindrag = TP_DEF_MINDRAG;
  206. tp->thresh = TP_DEF_THRESH;
  207. tp->upthresh = TP_DEF_UP_THRESH;
  208. tp->ztime = TP_DEF_Z_TIME;
  209. tp->jenks = TP_DEF_JENKS_CURV;
  210. tp->inertia = TP_DEF_INERTIA;
  211. tp->skipback = TP_DEF_SKIPBACK;
  212. tp->ext_dev = TP_DEF_EXT_DEV;
  213. }
  214. static void trackpoint_disconnect(struct psmouse *psmouse)
  215. {
  216. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj, &trackpoint_attr_group);
  217. kfree(psmouse->private);
  218. psmouse->private = NULL;
  219. }
  220. static int trackpoint_reconnect(struct psmouse *psmouse)
  221. {
  222. if (trackpoint_start_protocol(psmouse, NULL))
  223. return -1;
  224. if (trackpoint_sync(psmouse))
  225. return -1;
  226. return 0;
  227. }
  228. int trackpoint_detect(struct psmouse *psmouse, int set_properties)
  229. {
  230. struct trackpoint_data *priv;
  231. struct ps2dev *ps2dev = &psmouse->ps2dev;
  232. unsigned char firmware_id;
  233. unsigned char button_info;
  234. int error;
  235. if (trackpoint_start_protocol(psmouse, &firmware_id))
  236. return -1;
  237. if (!set_properties)
  238. return 0;
  239. if (trackpoint_read(&psmouse->ps2dev, TP_EXT_BTN, &button_info)) {
  240. printk(KERN_WARNING "trackpoint.c: failed to get extended button data\n");
  241. button_info = 0;
  242. }
  243. psmouse->private = priv = kzalloc(sizeof(struct trackpoint_data), GFP_KERNEL);
  244. if (!priv)
  245. return -1;
  246. psmouse->vendor = "IBM";
  247. psmouse->name = "TrackPoint";
  248. psmouse->reconnect = trackpoint_reconnect;
  249. psmouse->disconnect = trackpoint_disconnect;
  250. trackpoint_defaults(priv);
  251. trackpoint_sync(psmouse);
  252. error = sysfs_create_group(&ps2dev->serio->dev.kobj, &trackpoint_attr_group);
  253. if (error) {
  254. printk(KERN_ERR
  255. "trackpoint.c: failed to create sysfs attributes, error: %d\n",
  256. error);
  257. kfree(priv);
  258. return -1;
  259. }
  260. printk(KERN_INFO "IBM TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
  261. firmware_id, (button_info & 0xf0) >> 4, button_info & 0x0f);
  262. return 0;
  263. }