Commit | Line | Data |
---|---|---|
541e316a SE |
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 | ||
5a0e3ad6 | 11 | #include <linux/slab.h> |
541e316a SE |
12 | #include <linux/delay.h> |
13 | #include <linux/serio.h> | |
14 | #include <linux/module.h> | |
541e316a SE |
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 | ||
541e316a SE |
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 | ||
541e316a | 63 | |
cfe9e888 DT |
64 | /* |
65 | * Trackpoint-specific attributes | |
66 | */ | |
67 | struct trackpoint_attr_data { | |
68 | size_t field_offset; | |
69 | unsigned char command; | |
70 | unsigned char mask; | |
b8044c74 | 71 | unsigned char inverted; |
cfe9e888 | 72 | }; |
541e316a | 73 | |
cfe9e888 DT |
74 | static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse, void *data, char *buf) |
75 | { | |
76 | struct trackpoint_data *tp = psmouse->private; | |
77 | struct trackpoint_attr_data *attr = data; | |
b8044c74 DT |
78 | unsigned char value = *(unsigned char *)((char *)tp + attr->field_offset); |
79 | ||
80 | if (attr->inverted) | |
81 | value = !value; | |
541e316a | 82 | |
b8044c74 | 83 | return sprintf(buf, "%u\n", value); |
cfe9e888 | 84 | } |
541e316a | 85 | |
cfe9e888 DT |
86 | static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data, |
87 | const char *buf, size_t count) | |
88 | { | |
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); | |
76496e7a JD |
92 | unsigned char value; |
93 | int err; | |
541e316a | 94 | |
76496e7a JD |
95 | err = kstrtou8(buf, 10, &value); |
96 | if (err) | |
97 | return err; | |
541e316a | 98 | |
cfe9e888 DT |
99 | *field = value; |
100 | trackpoint_write(&psmouse->ps2dev, attr->command, value); | |
541e316a | 101 | |
cfe9e888 DT |
102 | return count; |
103 | } | |
541e316a | 104 | |
cfe9e888 DT |
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, \ | |
109 | }; \ | |
110 | PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \ | |
111 | &trackpoint_attr_##_name, \ | |
112 | trackpoint_show_int_attr, trackpoint_set_int_attr) | |
113 | ||
114 | static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data, | |
115 | const char *buf, size_t count) | |
116 | { | |
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); | |
76496e7a JD |
120 | unsigned int value; |
121 | int err; | |
122 | ||
123 | err = kstrtouint(buf, 10, &value); | |
124 | if (err) | |
125 | return err; | |
cfe9e888 | 126 | |
76496e7a | 127 | if (value > 1) |
cfe9e888 DT |
128 | return -EINVAL; |
129 | ||
b8044c74 DT |
130 | if (attr->inverted) |
131 | value = !value; | |
132 | ||
cfe9e888 DT |
133 | if (*field != value) { |
134 | *field = value; | |
135 | trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask); | |
136 | } | |
541e316a | 137 | |
cfe9e888 DT |
138 | return count; |
139 | } | |
541e316a | 140 | |
541e316a | 141 | |
b8044c74 | 142 | #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv) \ |
cfe9e888 DT |
143 | static struct trackpoint_attr_data trackpoint_attr_##_name = { \ |
144 | .field_offset = offsetof(struct trackpoint_data, _name), \ | |
145 | .command = _command, \ | |
146 | .mask = _mask, \ | |
b8044c74 | 147 | .inverted = _inv, \ |
cfe9e888 DT |
148 | }; \ |
149 | PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \ | |
150 | &trackpoint_attr_##_name, \ | |
151 | trackpoint_show_int_attr, trackpoint_set_bit_attr) | |
152 | ||
153 | TRACKPOINT_INT_ATTR(sensitivity, TP_SENS); | |
154 | TRACKPOINT_INT_ATTR(speed, TP_SPEED); | |
155 | TRACKPOINT_INT_ATTR(inertia, TP_INERTIA); | |
156 | TRACKPOINT_INT_ATTR(reach, TP_REACH); | |
157 | TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS); | |
158 | TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG); | |
159 | TRACKPOINT_INT_ATTR(thresh, TP_THRESH); | |
160 | TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH); | |
161 | TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME); | |
162 | TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV); | |
163 | ||
b8044c74 DT |
164 | TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, 0); |
165 | TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, 0); | |
166 | TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, 1); | |
541e316a SE |
167 | |
168 | static struct attribute *trackpoint_attrs[] = { | |
cfe9e888 DT |
169 | &psmouse_attr_sensitivity.dattr.attr, |
170 | &psmouse_attr_speed.dattr.attr, | |
171 | &psmouse_attr_inertia.dattr.attr, | |
172 | &psmouse_attr_reach.dattr.attr, | |
173 | &psmouse_attr_draghys.dattr.attr, | |
174 | &psmouse_attr_mindrag.dattr.attr, | |
175 | &psmouse_attr_thresh.dattr.attr, | |
176 | &psmouse_attr_upthresh.dattr.attr, | |
177 | &psmouse_attr_ztime.dattr.attr, | |
178 | &psmouse_attr_jenks.dattr.attr, | |
179 | &psmouse_attr_press_to_select.dattr.attr, | |
180 | &psmouse_attr_skipback.dattr.attr, | |
181 | &psmouse_attr_ext_dev.dattr.attr, | |
541e316a SE |
182 | NULL |
183 | }; | |
184 | ||
185 | static struct attribute_group trackpoint_attr_group = { | |
186 | .attrs = trackpoint_attrs, | |
187 | }; | |
188 | ||
184dd275 | 189 | static int trackpoint_start_protocol(struct psmouse *psmouse, unsigned char *firmware_id) |
541e316a | 190 | { |
184dd275 | 191 | unsigned char param[2] = { 0 }; |
541e316a | 192 | |
184dd275 DT |
193 | if (ps2_command(&psmouse->ps2dev, param, MAKE_PS2_CMD(0, 2, TP_READ_ID))) |
194 | return -1; | |
195 | ||
196 | if (param[0] != TP_MAGIC_IDENT) | |
197 | return -1; | |
198 | ||
199 | if (firmware_id) | |
200 | *firmware_id = param[1]; | |
201 | ||
202 | return 0; | |
541e316a SE |
203 | } |
204 | ||
205 | static int trackpoint_sync(struct psmouse *psmouse) | |
206 | { | |
541e316a | 207 | struct trackpoint_data *tp = psmouse->private; |
184dd275 | 208 | unsigned char toggle; |
541e316a SE |
209 | |
210 | /* Disable features that may make device unusable with this driver */ | |
211 | trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, &toggle); | |
212 | if (toggle & TP_MASK_TWOHAND) | |
213 | trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, TP_MASK_TWOHAND); | |
214 | ||
215 | trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, &toggle); | |
216 | if (toggle & TP_MASK_SOURCE_TAG) | |
217 | trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, TP_MASK_SOURCE_TAG); | |
218 | ||
219 | trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_MB, &toggle); | |
220 | if (toggle & TP_MASK_MB) | |
221 | trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_MB, TP_MASK_MB); | |
222 | ||
223 | /* Push the config to the device */ | |
224 | trackpoint_write(&psmouse->ps2dev, TP_SENS, tp->sensitivity); | |
225 | trackpoint_write(&psmouse->ps2dev, TP_INERTIA, tp->inertia); | |
226 | trackpoint_write(&psmouse->ps2dev, TP_SPEED, tp->speed); | |
227 | ||
228 | trackpoint_write(&psmouse->ps2dev, TP_REACH, tp->reach); | |
229 | trackpoint_write(&psmouse->ps2dev, TP_DRAGHYS, tp->draghys); | |
230 | trackpoint_write(&psmouse->ps2dev, TP_MINDRAG, tp->mindrag); | |
231 | ||
232 | trackpoint_write(&psmouse->ps2dev, TP_THRESH, tp->thresh); | |
233 | trackpoint_write(&psmouse->ps2dev, TP_UP_THRESH, tp->upthresh); | |
234 | ||
235 | trackpoint_write(&psmouse->ps2dev, TP_Z_TIME, tp->ztime); | |
236 | trackpoint_write(&psmouse->ps2dev, TP_JENKS_CURV, tp->jenks); | |
237 | ||
238 | trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_PTSON, &toggle); | |
239 | if (((toggle & TP_MASK_PTSON) == TP_MASK_PTSON) != tp->press_to_select) | |
240 | trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_PTSON, TP_MASK_PTSON); | |
241 | ||
242 | trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, &toggle); | |
243 | if (((toggle & TP_MASK_SKIPBACK) == TP_MASK_SKIPBACK) != tp->skipback) | |
244 | trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK); | |
245 | ||
246 | trackpoint_read(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, &toggle); | |
247 | if (((toggle & TP_MASK_EXT_DEV) == TP_MASK_EXT_DEV) != tp->ext_dev) | |
248 | trackpoint_toggle_bit(&psmouse->ps2dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV); | |
249 | ||
250 | return 0; | |
251 | } | |
252 | ||
253 | static void trackpoint_defaults(struct trackpoint_data *tp) | |
254 | { | |
255 | tp->press_to_select = TP_DEF_PTSON; | |
256 | tp->sensitivity = TP_DEF_SENS; | |
257 | tp->speed = TP_DEF_SPEED; | |
258 | tp->reach = TP_DEF_REACH; | |
259 | ||
260 | tp->draghys = TP_DEF_DRAGHYS; | |
261 | tp->mindrag = TP_DEF_MINDRAG; | |
262 | ||
263 | tp->thresh = TP_DEF_THRESH; | |
264 | tp->upthresh = TP_DEF_UP_THRESH; | |
265 | ||
266 | tp->ztime = TP_DEF_Z_TIME; | |
267 | tp->jenks = TP_DEF_JENKS_CURV; | |
268 | ||
269 | tp->inertia = TP_DEF_INERTIA; | |
270 | tp->skipback = TP_DEF_SKIPBACK; | |
271 | tp->ext_dev = TP_DEF_EXT_DEV; | |
272 | } | |
273 | ||
184dd275 DT |
274 | static void trackpoint_disconnect(struct psmouse *psmouse) |
275 | { | |
276 | sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj, &trackpoint_attr_group); | |
277 | ||
278 | kfree(psmouse->private); | |
279 | psmouse->private = NULL; | |
280 | } | |
281 | ||
282 | static int trackpoint_reconnect(struct psmouse *psmouse) | |
283 | { | |
284 | if (trackpoint_start_protocol(psmouse, NULL)) | |
285 | return -1; | |
286 | ||
287 | if (trackpoint_sync(psmouse)) | |
288 | return -1; | |
289 | ||
290 | return 0; | |
291 | } | |
292 | ||
b7802c5c | 293 | int trackpoint_detect(struct psmouse *psmouse, bool set_properties) |
541e316a | 294 | { |
541e316a SE |
295 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
296 | unsigned char firmware_id; | |
297 | unsigned char button_info; | |
8ff22ea7 | 298 | int error; |
541e316a | 299 | |
184dd275 | 300 | if (trackpoint_start_protocol(psmouse, &firmware_id)) |
541e316a SE |
301 | return -1; |
302 | ||
303 | if (!set_properties) | |
304 | return 0; | |
305 | ||
541e316a SE |
306 | if (trackpoint_read(&psmouse->ps2dev, TP_EXT_BTN, &button_info)) { |
307 | printk(KERN_WARNING "trackpoint.c: failed to get extended button data\n"); | |
308 | button_info = 0; | |
309 | } | |
310 | ||
315eb996 DT |
311 | psmouse->private = kzalloc(sizeof(struct trackpoint_data), GFP_KERNEL); |
312 | if (!psmouse->private) | |
6792cbbb | 313 | return -ENOMEM; |
541e316a SE |
314 | |
315 | psmouse->vendor = "IBM"; | |
316 | psmouse->name = "TrackPoint"; | |
317 | ||
184dd275 | 318 | psmouse->reconnect = trackpoint_reconnect; |
541e316a SE |
319 | psmouse->disconnect = trackpoint_disconnect; |
320 | ||
315eb996 DT |
321 | if ((button_info & 0x0f) >= 3) |
322 | __set_bit(BTN_MIDDLE, psmouse->dev->keybit); | |
323 | ||
324 | trackpoint_defaults(psmouse->private); | |
541e316a SE |
325 | trackpoint_sync(psmouse); |
326 | ||
8ff22ea7 JG |
327 | error = sysfs_create_group(&ps2dev->serio->dev.kobj, &trackpoint_attr_group); |
328 | if (error) { | |
329 | printk(KERN_ERR | |
330 | "trackpoint.c: failed to create sysfs attributes, error: %d\n", | |
331 | error); | |
315eb996 DT |
332 | kfree(psmouse->private); |
333 | psmouse->private = NULL; | |
8ff22ea7 JG |
334 | return -1; |
335 | } | |
541e316a SE |
336 | |
337 | printk(KERN_INFO "IBM TrackPoint firmware: 0x%02x, buttons: %d/%d\n", | |
338 | firmware_id, (button_info & 0xf0) >> 4, button_info & 0x0f); | |
339 | ||
340 | return 0; | |
341 | } | |
342 |