trackpoint.c 9.0 KB

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