trackpoint.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. };
  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 *field = (unsigned char *)((char *)tp + attr->field_offset);
  67. return sprintf(buf, "%u\n", *field);
  68. }
  69. static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
  70. const char *buf, size_t count)
  71. {
  72. struct trackpoint_data *tp = psmouse->private;
  73. struct trackpoint_attr_data *attr = data;
  74. unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
  75. unsigned long value;
  76. char *rest;
  77. value = simple_strtoul(buf, &rest, 10);
  78. if (*rest || 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. char *rest;
  100. value = simple_strtoul(buf, &rest, 10);
  101. if (*rest || value > 1)
  102. return -EINVAL;
  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) \
  110. static struct trackpoint_attr_data trackpoint_attr_##_name = { \
  111. .field_offset = offsetof(struct trackpoint_data, _name), \
  112. .command = _command, \
  113. .mask = _mask, \
  114. }; \
  115. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  116. &trackpoint_attr_##_name, \
  117. trackpoint_show_int_attr, trackpoint_set_bit_attr)
  118. TRACKPOINT_INT_ATTR(sensitivity, TP_SENS);
  119. TRACKPOINT_INT_ATTR(speed, TP_SPEED);
  120. TRACKPOINT_INT_ATTR(inertia, TP_INERTIA);
  121. TRACKPOINT_INT_ATTR(reach, TP_REACH);
  122. TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS);
  123. TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG);
  124. TRACKPOINT_INT_ATTR(thresh, TP_THRESH);
  125. TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH);
  126. TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME);
  127. TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV);
  128. TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON);
  129. TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK);
  130. TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV);
  131. static struct attribute *trackpoint_attrs[] = {
  132. &psmouse_attr_sensitivity.dattr.attr,
  133. &psmouse_attr_speed.dattr.attr,
  134. &psmouse_attr_inertia.dattr.attr,
  135. &psmouse_attr_reach.dattr.attr,
  136. &psmouse_attr_draghys.dattr.attr,
  137. &psmouse_attr_mindrag.dattr.attr,
  138. &psmouse_attr_thresh.dattr.attr,
  139. &psmouse_attr_upthresh.dattr.attr,
  140. &psmouse_attr_ztime.dattr.attr,
  141. &psmouse_attr_jenks.dattr.attr,
  142. &psmouse_attr_press_to_select.dattr.attr,
  143. &psmouse_attr_skipback.dattr.attr,
  144. &psmouse_attr_ext_dev.dattr.attr,
  145. NULL
  146. };
  147. static struct attribute_group trackpoint_attr_group = {
  148. .attrs = trackpoint_attrs,
  149. };
  150. static void trackpoint_disconnect(struct psmouse *psmouse)
  151. {
  152. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj, &trackpoint_attr_group);
  153. kfree(psmouse->private);
  154. psmouse->private = NULL;
  155. }
  156. static int trackpoint_sync(struct psmouse *psmouse)
  157. {
  158. unsigned char toggle;
  159. struct trackpoint_data *tp = psmouse->private;
  160. if (!tp)
  161. return -1;
  162. /* Disable features that may make device unusable with this driver */
  163. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, &toggle);
  164. if (toggle & TP_MASK_TWOHAND)
  165. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, TP_MASK_TWOHAND);
  166. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, &toggle);
  167. if (toggle & TP_MASK_SOURCE_TAG)
  168. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, TP_MASK_SOURCE_TAG);
  169. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_MB, &toggle);
  170. if (toggle & TP_MASK_MB)
  171. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_MB, TP_MASK_MB);
  172. /* Push the config to the device */
  173. trackpoint_write(&psmouse->ps2dev, TP_SENS, tp->sensitivity);
  174. trackpoint_write(&psmouse->ps2dev, TP_INERTIA, tp->inertia);
  175. trackpoint_write(&psmouse->ps2dev, TP_SPEED, tp->speed);
  176. trackpoint_write(&psmouse->ps2dev, TP_REACH, tp->reach);
  177. trackpoint_write(&psmouse->ps2dev, TP_DRAGHYS, tp->draghys);
  178. trackpoint_write(&psmouse->ps2dev, TP_MINDRAG, tp->mindrag);
  179. trackpoint_write(&psmouse->ps2dev, TP_THRESH, tp->thresh);
  180. trackpoint_write(&psmouse->ps2dev, TP_UP_THRESH, tp->upthresh);
  181. trackpoint_write(&psmouse->ps2dev, TP_Z_TIME, tp->ztime);
  182. trackpoint_write(&psmouse->ps2dev, TP_JENKS_CURV, tp->jenks);
  183. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_PTSON, &toggle);
  184. if (((toggle & TP_MASK_PTSON) == TP_MASK_PTSON) != tp->press_to_select)
  185. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_PTSON, TP_MASK_PTSON);
  186. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, &toggle);
  187. if (((toggle & TP_MASK_SKIPBACK) == TP_MASK_SKIPBACK) != tp->skipback)
  188. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK);
  189. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, &toggle);
  190. if (((toggle & TP_MASK_EXT_DEV) == TP_MASK_EXT_DEV) != tp->ext_dev)
  191. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV);
  192. return 0;
  193. }
  194. static void trackpoint_defaults(struct trackpoint_data *tp)
  195. {
  196. tp->press_to_select = TP_DEF_PTSON;
  197. tp->sensitivity = TP_DEF_SENS;
  198. tp->speed = TP_DEF_SPEED;
  199. tp->reach = TP_DEF_REACH;
  200. tp->draghys = TP_DEF_DRAGHYS;
  201. tp->mindrag = TP_DEF_MINDRAG;
  202. tp->thresh = TP_DEF_THRESH;
  203. tp->upthresh = TP_DEF_UP_THRESH;
  204. tp->ztime = TP_DEF_Z_TIME;
  205. tp->jenks = TP_DEF_JENKS_CURV;
  206. tp->inertia = TP_DEF_INERTIA;
  207. tp->skipback = TP_DEF_SKIPBACK;
  208. tp->ext_dev = TP_DEF_EXT_DEV;
  209. }
  210. int trackpoint_detect(struct psmouse *psmouse, int set_properties)
  211. {
  212. struct trackpoint_data *priv;
  213. struct ps2dev *ps2dev = &psmouse->ps2dev;
  214. unsigned char firmware_id;
  215. unsigned char button_info;
  216. unsigned char param[2];
  217. param[0] = param[1] = 0;
  218. if (ps2_command(ps2dev, param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
  219. return -1;
  220. if (param[0] != TP_MAGIC_IDENT)
  221. return -1;
  222. if (!set_properties)
  223. return 0;
  224. firmware_id = param[1];
  225. if (trackpoint_read(&psmouse->ps2dev, TP_EXT_BTN, &button_info)) {
  226. printk(KERN_WARNING "trackpoint.c: failed to get extended button data\n");
  227. button_info = 0;
  228. }
  229. psmouse->private = priv = kcalloc(1, sizeof(struct trackpoint_data), GFP_KERNEL);
  230. if (!priv)
  231. return -1;
  232. psmouse->vendor = "IBM";
  233. psmouse->name = "TrackPoint";
  234. psmouse->reconnect = trackpoint_sync;
  235. psmouse->disconnect = trackpoint_disconnect;
  236. trackpoint_defaults(priv);
  237. trackpoint_sync(psmouse);
  238. sysfs_create_group(&ps2dev->serio->dev.kobj, &trackpoint_attr_group);
  239. printk(KERN_INFO "IBM TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
  240. firmware_id, (button_info & 0xf0) >> 4, button_info & 0x0f);
  241. return 0;
  242. }