2 * Stephen Evanchik <evanchsa@gmail.com>
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.
8 * Trademarks are the property of their respective owners.
11 #include <linux/delay.h>
12 #include <linux/serio.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/input.h>
16 #include <linux/libps2.h>
17 #include <linux/proc_fs.h>
18 #include <asm/uaccess.h>
20 #include "trackpoint.h"
23 * Device IO: read, write and toggle bit
25 static int trackpoint_read(struct ps2dev *ps2dev, unsigned char loc, unsigned char *results)
27 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
28 ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 1, loc))) {
35 static int trackpoint_write(struct ps2dev *ps2dev, unsigned char loc, unsigned char val)
37 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
38 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_WRITE_MEM)) ||
39 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
40 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, val))) {
47 static int trackpoint_toggle_bit(struct ps2dev *ps2dev, unsigned char loc, unsigned char mask)
49 /* Bad things will happen if the loc param isn't in this range */
50 if (loc < 0x20 || loc >= 0x2F)
53 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
54 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_TOGGLE)) ||
55 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
56 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, mask))) {
65 * Trackpoint-specific attributes
67 struct trackpoint_attr_data {
69 unsigned char command;
71 unsigned char inverted;
74 static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse, void *data, char *buf)
76 struct trackpoint_data *tp = psmouse->private;
77 struct trackpoint_attr_data *attr = data;
78 unsigned char value = *(unsigned char *)((char *)tp + attr->field_offset);
83 return sprintf(buf, "%u\n", value);
86 static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
87 const char *buf, size_t count)
89 struct trackpoint_data *tp = psmouse->private;
90 struct trackpoint_attr_data *attr = data;
91 unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
95 value = simple_strtoul(buf, &rest, 10);
96 if (*rest || value > 255)
100 trackpoint_write(&psmouse->ps2dev, attr->command, value);
105 #define TRACKPOINT_INT_ATTR(_name, _command) \
106 static struct trackpoint_attr_data trackpoint_attr_##_name = { \
107 .field_offset = offsetof(struct trackpoint_data, _name), \
108 .command = _command, \
110 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
111 &trackpoint_attr_##_name, \
112 trackpoint_show_int_attr, trackpoint_set_int_attr)
114 static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
115 const char *buf, size_t count)
117 struct trackpoint_data *tp = psmouse->private;
118 struct trackpoint_attr_data *attr = data;
119 unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
123 value = simple_strtoul(buf, &rest, 10);
124 if (*rest || value > 1)
130 if (*field != value) {
132 trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask);
139 #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv) \
140 static struct trackpoint_attr_data trackpoint_attr_##_name = { \
141 .field_offset = offsetof(struct trackpoint_data, _name), \
142 .command = _command, \
146 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
147 &trackpoint_attr_##_name, \
148 trackpoint_show_int_attr, trackpoint_set_bit_attr)
150 TRACKPOINT_INT_ATTR(sensitivity, TP_SENS);
151 TRACKPOINT_INT_ATTR(speed, TP_SPEED);
152 TRACKPOINT_INT_ATTR(inertia, TP_INERTIA);
153 TRACKPOINT_INT_ATTR(reach, TP_REACH);
154 TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS);
155 TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG);
156 TRACKPOINT_INT_ATTR(thresh, TP_THRESH);
157 TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH);
158 TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME);
159 TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV);
161 TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, 0);
162 TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, 0);
163 TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, 1);
165 static struct attribute *trackpoint_attrs[] = {
166 &psmouse_attr_sensitivity.dattr.attr,
167 &psmouse_attr_speed.dattr.attr,
168 &psmouse_attr_inertia.dattr.attr,
169 &psmouse_attr_reach.dattr.attr,
170 &psmouse_attr_draghys.dattr.attr,
171 &psmouse_attr_mindrag.dattr.attr,
172 &psmouse_attr_thresh.dattr.attr,
173 &psmouse_attr_upthresh.dattr.attr,
174 &psmouse_attr_ztime.dattr.attr,
175 &psmouse_attr_jenks.dattr.attr,
176 &psmouse_attr_press_to_select.dattr.attr,
177 &psmouse_attr_skipback.dattr.attr,
178 &psmouse_attr_ext_dev.dattr.attr,
182 static struct attribute_group trackpoint_attr_group = {
183 .attrs = trackpoint_attrs,
186 static void trackpoint_disconnect(struct psmouse *psmouse)
188 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj, &trackpoint_attr_group);
190 kfree(psmouse->private);
191 psmouse->private = NULL;
194 static int trackpoint_sync(struct psmouse *psmouse)
196 unsigned char toggle;
197 struct trackpoint_data *tp = psmouse->private;
202 /* Disable features that may make device unusable with this driver */
203 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, &toggle);
204 if (toggle & TP_MASK_TWOHAND)
205 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, TP_MASK_TWOHAND);
207 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, &toggle);
208 if (toggle & TP_MASK_SOURCE_TAG)
209 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, TP_MASK_SOURCE_TAG);
211 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_MB, &toggle);
212 if (toggle & TP_MASK_MB)
213 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_MB, TP_MASK_MB);
215 /* Push the config to the device */
216 trackpoint_write(&psmouse->ps2dev, TP_SENS, tp->sensitivity);
217 trackpoint_write(&psmouse->ps2dev, TP_INERTIA, tp->inertia);
218 trackpoint_write(&psmouse->ps2dev, TP_SPEED, tp->speed);
220 trackpoint_write(&psmouse->ps2dev, TP_REACH, tp->reach);
221 trackpoint_write(&psmouse->ps2dev, TP_DRAGHYS, tp->draghys);
222 trackpoint_write(&psmouse->ps2dev, TP_MINDRAG, tp->mindrag);
224 trackpoint_write(&psmouse->ps2dev, TP_THRESH, tp->thresh);
225 trackpoint_write(&psmouse->ps2dev, TP_UP_THRESH, tp->upthresh);
227 trackpoint_write(&psmouse->ps2dev, TP_Z_TIME, tp->ztime);
228 trackpoint_write(&psmouse->ps2dev, TP_JENKS_CURV, tp->jenks);
230 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_PTSON, &toggle);
231 if (((toggle & TP_MASK_PTSON) == TP_MASK_PTSON) != tp->press_to_select)
232 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_PTSON, TP_MASK_PTSON);
234 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, &toggle);
235 if (((toggle & TP_MASK_SKIPBACK) == TP_MASK_SKIPBACK) != tp->skipback)
236 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK);
238 trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, &toggle);
239 if (((toggle & TP_MASK_EXT_DEV) == TP_MASK_EXT_DEV) != tp->ext_dev)
240 trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV);
245 static void trackpoint_defaults(struct trackpoint_data *tp)
247 tp->press_to_select = TP_DEF_PTSON;
248 tp->sensitivity = TP_DEF_SENS;
249 tp->speed = TP_DEF_SPEED;
250 tp->reach = TP_DEF_REACH;
252 tp->draghys = TP_DEF_DRAGHYS;
253 tp->mindrag = TP_DEF_MINDRAG;
255 tp->thresh = TP_DEF_THRESH;
256 tp->upthresh = TP_DEF_UP_THRESH;
258 tp->ztime = TP_DEF_Z_TIME;
259 tp->jenks = TP_DEF_JENKS_CURV;
261 tp->inertia = TP_DEF_INERTIA;
262 tp->skipback = TP_DEF_SKIPBACK;
263 tp->ext_dev = TP_DEF_EXT_DEV;
266 int trackpoint_detect(struct psmouse *psmouse, int set_properties)
268 struct trackpoint_data *priv;
269 struct ps2dev *ps2dev = &psmouse->ps2dev;
270 unsigned char firmware_id;
271 unsigned char button_info;
272 unsigned char param[2];
274 param[0] = param[1] = 0;
276 if (ps2_command(ps2dev, param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
279 if (param[0] != TP_MAGIC_IDENT)
285 firmware_id = param[1];
287 if (trackpoint_read(&psmouse->ps2dev, TP_EXT_BTN, &button_info)) {
288 printk(KERN_WARNING "trackpoint.c: failed to get extended button data\n");
292 psmouse->private = priv = kcalloc(1, sizeof(struct trackpoint_data), GFP_KERNEL);
296 psmouse->vendor = "IBM";
297 psmouse->name = "TrackPoint";
299 psmouse->reconnect = trackpoint_sync;
300 psmouse->disconnect = trackpoint_disconnect;
302 trackpoint_defaults(priv);
303 trackpoint_sync(psmouse);
305 sysfs_create_group(&ps2dev->serio->dev.kobj, &trackpoint_attr_group);
307 printk(KERN_INFO "IBM TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
308 firmware_id, (button_info & 0xf0) >> 4, button_info & 0x0f);