trackpoint.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. * 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. if (strict_strtoul(buf, 10, &value) || value > 255)
  80. return -EINVAL;
  81. *field = value;
  82. trackpoint_write(&psmouse->ps2dev, attr->command, value);
  83. return count;
  84. }
  85. #define TRACKPOINT_INT_ATTR(_name, _command) \
  86. static struct trackpoint_attr_data trackpoint_attr_##_name = { \
  87. .field_offset = offsetof(struct trackpoint_data, _name), \
  88. .command = _command, \
  89. }; \
  90. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  91. &trackpoint_attr_##_name, \
  92. trackpoint_show_int_attr, trackpoint_set_int_attr)
  93. static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
  94. const char *buf, size_t count)
  95. {
  96. struct trackpoint_data *tp = psmouse->private;
  97. struct trackpoint_attr_data *attr = data;
  98. unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
  99. unsigned long value;
  100. if (strict_strtoul(buf, 10, &value) || value > 1)
  101. return -EINVAL;
  102. if (attr->inverted)
  103. value = !value;
  104. if (*field != value) {
  105. *field = value;
  106. trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask);
  107. }
  108. return count;
  109. }
  110. #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv) \
  111. static struct trackpoint_attr_data trackpoint_attr_##_name = { \
  112. .field_offset = offsetof(struct trackpoint_data, _name), \
  113. .command = _command, \
  114. .mask = _mask, \
  115. .inverted = _inv, \
  116. }; \
  117. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  118. &trackpoint_attr_##_name, \
  119. trackpoint_show_int_attr, trackpoint_set_bit_attr)
  120. TRACKPOINT_INT_ATTR(sensitivity, TP_SENS);
  121. TRACKPOINT_INT_ATTR(speed, TP_SPEED);
  122. TRACKPOINT_INT_ATTR(inertia, TP_INERTIA);
  123. TRACKPOINT_INT_ATTR(reach, TP_REACH);
  124. TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS);
  125. TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG);
  126. TRACKPOINT_INT_ATTR(thresh, TP_THRESH);
  127. TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH);
  128. TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME);
  129. TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV);
  130. TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, 0);
  131. TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, 0);
  132. TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, 1);
  133. static struct attribute *trackpoint_attrs[] = {
  134. &psmouse_attr_sensitivity.dattr.attr,
  135. &psmouse_attr_speed.dattr.attr,
  136. &psmouse_attr_inertia.dattr.attr,
  137. &psmouse_attr_reach.dattr.attr,
  138. &psmouse_attr_draghys.dattr.attr,
  139. &psmouse_attr_mindrag.dattr.attr,
  140. &psmouse_attr_thresh.dattr.attr,
  141. &psmouse_attr_upthresh.dattr.attr,
  142. &psmouse_attr_ztime.dattr.attr,
  143. &psmouse_attr_jenks.dattr.attr,
  144. &psmouse_attr_press_to_select.dattr.attr,
  145. &psmouse_attr_skipback.dattr.attr,
  146. &psmouse_attr_ext_dev.dattr.attr,
  147. NULL
  148. };
  149. static struct attribute_group trackpoint_attr_group = {
  150. .attrs = trackpoint_attrs,
  151. };
  152. static int trackpoint_start_protocol(struct psmouse *psmouse, unsigned char *firmware_id)
  153. {
  154. unsigned char param[2] = { 0 };
  155. if (ps2_command(&psmouse->ps2dev, param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
  156. return -1;
  157. if (param[0] != TP_MAGIC_IDENT)
  158. return -1;
  159. if (firmware_id)
  160. *firmware_id = param[1];
  161. return 0;
  162. }
  163. static int trackpoint_sync(struct psmouse *psmouse)
  164. {
  165. struct trackpoint_data *tp = psmouse->private;
  166. unsigned char toggle;
  167. /* Disable features that may make device unusable with this driver */
  168. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, &toggle);
  169. if (toggle & TP_MASK_TWOHAND)
  170. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, TP_MASK_TWOHAND);
  171. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, &toggle);
  172. if (toggle & TP_MASK_SOURCE_TAG)
  173. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, TP_MASK_SOURCE_TAG);
  174. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_MB, &toggle);
  175. if (toggle & TP_MASK_MB)
  176. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_MB, TP_MASK_MB);
  177. /* Push the config to the device */
  178. trackpoint_write(&psmouse->ps2dev, TP_SENS, tp->sensitivity);
  179. trackpoint_write(&psmouse->ps2dev, TP_INERTIA, tp->inertia);
  180. trackpoint_write(&psmouse->ps2dev, TP_SPEED, tp->speed);
  181. trackpoint_write(&psmouse->ps2dev, TP_REACH, tp->reach);
  182. trackpoint_write(&psmouse->ps2dev, TP_DRAGHYS, tp->draghys);
  183. trackpoint_write(&psmouse->ps2dev, TP_MINDRAG, tp->mindrag);
  184. trackpoint_write(&psmouse->ps2dev, TP_THRESH, tp->thresh);
  185. trackpoint_write(&psmouse->ps2dev, TP_UP_THRESH, tp->upthresh);
  186. trackpoint_write(&psmouse->ps2dev, TP_Z_TIME, tp->ztime);
  187. trackpoint_write(&psmouse->ps2dev, TP_JENKS_CURV, tp->jenks);
  188. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_PTSON, &toggle);
  189. if (((toggle & TP_MASK_PTSON) == TP_MASK_PTSON) != tp->press_to_select)
  190. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_PTSON, TP_MASK_PTSON);
  191. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, &toggle);
  192. if (((toggle & TP_MASK_SKIPBACK) == TP_MASK_SKIPBACK) != tp->skipback)
  193. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK);
  194. trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, &toggle);
  195. if (((toggle & TP_MASK_EXT_DEV) == TP_MASK_EXT_DEV) != tp->ext_dev)
  196. trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV);
  197. return 0;
  198. }
  199. static void trackpoint_defaults(struct trackpoint_data *tp)
  200. {
  201. tp->press_to_select = TP_DEF_PTSON;
  202. tp->sensitivity = TP_DEF_SENS;
  203. tp->speed = TP_DEF_SPEED;
  204. tp->reach = TP_DEF_REACH;
  205. tp->draghys = TP_DEF_DRAGHYS;
  206. tp->mindrag = TP_DEF_MINDRAG;
  207. tp->thresh = TP_DEF_THRESH;
  208. tp->upthresh = TP_DEF_UP_THRESH;
  209. tp->ztime = TP_DEF_Z_TIME;
  210. tp->jenks = TP_DEF_JENKS_CURV;
  211. tp->inertia = TP_DEF_INERTIA;
  212. tp->skipback = TP_DEF_SKIPBACK;
  213. tp->ext_dev = TP_DEF_EXT_DEV;
  214. }
  215. static void trackpoint_disconnect(struct psmouse *psmouse)
  216. {
  217. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj, &trackpoint_attr_group);
  218. kfree(psmouse->private);
  219. psmouse->private = NULL;
  220. }
  221. static int trackpoint_reconnect(struct psmouse *psmouse)
  222. {
  223. if (trackpoint_start_protocol(psmouse, NULL))
  224. return -1;
  225. if (trackpoint_sync(psmouse))
  226. return -1;
  227. return 0;
  228. }
  229. int trackpoint_detect(struct psmouse *psmouse, bool set_properties)
  230. {
  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 = kzalloc(sizeof(struct trackpoint_data), GFP_KERNEL);
  244. if (!psmouse->private)
  245. return -ENOMEM;
  246. psmouse->vendor = "IBM";
  247. psmouse->name = "TrackPoint";
  248. psmouse->reconnect = trackpoint_reconnect;
  249. psmouse->disconnect = trackpoint_disconnect;
  250. if ((button_info & 0x0f) >= 3)
  251. __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
  252. trackpoint_defaults(psmouse->private);
  253. trackpoint_sync(psmouse);
  254. error = sysfs_create_group(&ps2dev->serio->dev.kobj, &trackpoint_attr_group);
  255. if (error) {
  256. printk(KERN_ERR
  257. "trackpoint.c: failed to create sysfs attributes, error: %d\n",
  258. error);
  259. kfree(psmouse->private);
  260. psmouse->private = NULL;
  261. return -1;
  262. }
  263. printk(KERN_INFO "IBM TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
  264. firmware_id, (button_info & 0xf0) >> 4, button_info & 0x0f);
  265. return 0;
  266. }