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