4c16c5b335fb9cb2b28ffdb3f76f65dff7d8a63d
[cascardo/linux.git] / drivers / input / mouse / trackpoint.c
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/slab.h>
12 #include <linux/delay.h>
13 #include <linux/serio.h>
14 #include <linux/module.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  * Power-on Reset: Resets all trackpoint parameters, including RAM values,
24  * to defaults.
25  * Returns zero on success, non-zero on failure.
26  */
27 static int trackpoint_power_on_reset(struct ps2dev *ps2dev)
28 {
29         unsigned char results[2];
30
31         if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
32             ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 2, TP_POR))) {
33                 return -1;
34         }
35
36         /* POR response should be 0xAA00 or 0xFC00 */
37         if ((results[0] != 0xAA && results[0] != 0xFC) || results[1] != 0x00)
38                 return -1;
39         return 0;
40 }
41
42 /*
43  * Device IO: read, write and toggle bit
44  */
45 static int trackpoint_read(struct ps2dev *ps2dev, unsigned char loc, unsigned char *results)
46 {
47         if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
48             ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 1, loc))) {
49                 return -1;
50         }
51
52         return 0;
53 }
54
55 static int trackpoint_write(struct ps2dev *ps2dev, unsigned char loc, unsigned char val)
56 {
57         if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
58             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_WRITE_MEM)) ||
59             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
60             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, val))) {
61                 return -1;
62         }
63
64         return 0;
65 }
66
67 static int trackpoint_toggle_bit(struct ps2dev *ps2dev, unsigned char loc, unsigned char mask)
68 {
69         /* Bad things will happen if the loc param isn't in this range */
70         if (loc < 0x20 || loc >= 0x2F)
71                 return -1;
72
73         if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
74             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_TOGGLE)) ||
75             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
76             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, mask))) {
77                 return -1;
78         }
79
80         return 0;
81 }
82
83
84 /*
85  * Trackpoint-specific attributes
86  */
87 struct trackpoint_attr_data {
88         size_t field_offset;
89         unsigned char command;
90         unsigned char mask;
91         unsigned char inverted;
92         unsigned char power_on_default;
93 };
94
95 static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse, void *data, char *buf)
96 {
97         struct trackpoint_data *tp = psmouse->private;
98         struct trackpoint_attr_data *attr = data;
99         unsigned char value = *(unsigned char *)((char *)tp + attr->field_offset);
100
101         if (attr->inverted)
102                 value = !value;
103
104         return sprintf(buf, "%u\n", value);
105 }
106
107 static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
108                                         const char *buf, size_t count)
109 {
110         struct trackpoint_data *tp = psmouse->private;
111         struct trackpoint_attr_data *attr = data;
112         unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
113         unsigned char value;
114         int err;
115
116         err = kstrtou8(buf, 10, &value);
117         if (err)
118                 return err;
119
120         *field = value;
121         trackpoint_write(&psmouse->ps2dev, attr->command, value);
122
123         return count;
124 }
125
126 #define TRACKPOINT_INT_ATTR(_name, _command, _default)                          \
127         static struct trackpoint_attr_data trackpoint_attr_##_name = {          \
128                 .field_offset = offsetof(struct trackpoint_data, _name),        \
129                 .command = _command,                                            \
130                 .power_on_default = _default,                                   \
131         };                                                                      \
132         PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                           \
133                             &trackpoint_attr_##_name,                           \
134                             trackpoint_show_int_attr, trackpoint_set_int_attr)
135
136 static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
137                                         const char *buf, size_t count)
138 {
139         struct trackpoint_data *tp = psmouse->private;
140         struct trackpoint_attr_data *attr = data;
141         unsigned char *field = (unsigned char *)((char *)tp + attr->field_offset);
142         unsigned int value;
143         int err;
144
145         err = kstrtouint(buf, 10, &value);
146         if (err)
147                 return err;
148
149         if (value > 1)
150                 return -EINVAL;
151
152         if (attr->inverted)
153                 value = !value;
154
155         if (*field != value) {
156                 *field = value;
157                 trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask);
158         }
159
160         return count;
161 }
162
163
164 #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv, _default)             \
165         static struct trackpoint_attr_data trackpoint_attr_##_name = {          \
166                 .field_offset           = offsetof(struct trackpoint_data,      \
167                                                    _name),                      \
168                 .command                = _command,                             \
169                 .mask                   = _mask,                                \
170                 .inverted               = _inv,                                 \
171                 .power_on_default       = _default,                             \
172         };                                                                      \
173         PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                           \
174                             &trackpoint_attr_##_name,                           \
175                             trackpoint_show_int_attr, trackpoint_set_bit_attr)
176
177 #define TRACKPOINT_UPDATE_BIT(_psmouse, _tp, _name)                             \
178         {                                                                       \
179                 unsigned char _toggle;                                          \
180                 struct trackpoint_attr_data *_attr = &trackpoint_attr_##_name;  \
181                 trackpoint_read(&_psmouse->ps2dev, _attr->command, &_toggle);   \
182                 if (((_toggle & _attr->mask) == _attr->mask) !=                 \
183                       _tp->_name)                                               \
184                         trackpoint_toggle_bit(&_psmouse->ps2dev,                \
185                                               _attr->command,                   \
186                                               _attr->mask);                     \
187         }
188
189 #define TRACKPOINT_UPDATE(_power_on, _psmouse, _tp, _name)                      \
190         if (!_power_on ||                                                       \
191             _tp->_name != trackpoint_attr_##_name.power_on_default) {           \
192                 if (!trackpoint_attr_##_name.mask)                              \
193                         trackpoint_write(&_psmouse->ps2dev,                     \
194                                          trackpoint_attr_##_name.command,       \
195                                          _tp->_name);                           \
196                 else                                                            \
197                         TRACKPOINT_UPDATE_BIT(_psmouse, _tp, _name);            \
198         }
199
200 #define TRACKPOINT_SET_POWER_ON_DEFAULT(_tp, _name)                             \
201         (_tp->_name = trackpoint_attr_##_name.power_on_default)
202
203 TRACKPOINT_INT_ATTR(sensitivity, TP_SENS, TP_DEF_SENS);
204 TRACKPOINT_INT_ATTR(speed, TP_SPEED, TP_DEF_SPEED);
205 TRACKPOINT_INT_ATTR(inertia, TP_INERTIA, TP_DEF_INERTIA);
206 TRACKPOINT_INT_ATTR(reach, TP_REACH, TP_DEF_REACH);
207 TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS, TP_DEF_DRAGHYS);
208 TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG, TP_DEF_MINDRAG);
209 TRACKPOINT_INT_ATTR(thresh, TP_THRESH, TP_DEF_THRESH);
210 TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH, TP_DEF_UP_THRESH);
211 TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME, TP_DEF_Z_TIME);
212 TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV, TP_DEF_JENKS_CURV);
213
214 TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, 0,
215                     TP_DEF_PTSON);
216 TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, 0,
217                     TP_DEF_SKIPBACK);
218 TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, 1,
219                     TP_DEF_EXT_DEV);
220 TRACKPOINT_BIT_ATTR(twohand, TP_TOGGLE_TWOHAND, TP_MASK_TWOHAND, 0,
221                     TP_DEF_TWOHAND);
222 TRACKPOINT_BIT_ATTR(source_tag, TP_TOGGLE_SOURCE_TAG, TP_MASK_SOURCE_TAG, 0,
223                     TP_DEF_SOURCE_TAG);
224 TRACKPOINT_BIT_ATTR(mb, TP_TOGGLE_MB, TP_MASK_MB, 0,
225                     TP_DEF_MB);
226
227 static struct attribute *trackpoint_attrs[] = {
228         &psmouse_attr_sensitivity.dattr.attr,
229         &psmouse_attr_speed.dattr.attr,
230         &psmouse_attr_inertia.dattr.attr,
231         &psmouse_attr_reach.dattr.attr,
232         &psmouse_attr_draghys.dattr.attr,
233         &psmouse_attr_mindrag.dattr.attr,
234         &psmouse_attr_thresh.dattr.attr,
235         &psmouse_attr_upthresh.dattr.attr,
236         &psmouse_attr_ztime.dattr.attr,
237         &psmouse_attr_jenks.dattr.attr,
238         &psmouse_attr_press_to_select.dattr.attr,
239         &psmouse_attr_skipback.dattr.attr,
240         &psmouse_attr_ext_dev.dattr.attr,
241         &psmouse_attr_twohand.dattr.attr,
242         &psmouse_attr_source_tag.dattr.attr,
243         &psmouse_attr_mb.dattr.attr,
244         NULL
245 };
246
247 static struct attribute_group trackpoint_attr_group = {
248         .attrs = trackpoint_attrs,
249 };
250
251 static int trackpoint_start_protocol(struct psmouse *psmouse, unsigned char *firmware_id)
252 {
253         unsigned char param[2] = { 0 };
254
255         if (ps2_command(&psmouse->ps2dev, param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
256                 return -1;
257
258         if (param[0] != TP_MAGIC_IDENT)
259                 return -1;
260
261         if (firmware_id)
262                 *firmware_id = param[1];
263
264         return 0;
265 }
266
267 /*
268  * Write parameters to trackpad.
269  * in_power_on_state: Set to true if TP is in default / power-on state (ex. if
270  *                    power-on reset was run). If so, values will only be
271  *                    written to TP if they differ from power-on default.
272  */
273 static int trackpoint_sync(struct psmouse *psmouse, bool in_power_on_state)
274 {
275         struct trackpoint_data *tp = psmouse->private;
276
277         /*
278         * These properties can be changed in this driver. Only
279         * configure them if the values are non-default or if the TP is in
280         * an unknown state.
281         */
282         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, sensitivity);
283         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, inertia);
284         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, speed);
285         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, reach);
286         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, draghys);
287         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, mindrag);
288         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, thresh);
289         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, upthresh);
290         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ztime);
291         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, jenks);
292
293         /* toggles */
294         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, press_to_select);
295         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, skipback);
296         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ext_dev);
297         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, twohand);
298         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, source_tag);
299         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, mb);
300
301         return 0;
302 }
303
304 static void trackpoint_defaults(struct trackpoint_data *tp)
305 {
306         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, sensitivity);
307         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, speed);
308         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, reach);
309         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, draghys);
310         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, mindrag);
311         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, thresh);
312         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, upthresh);
313         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ztime);
314         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, jenks);
315         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, inertia);
316
317         /* toggles */
318         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, press_to_select);
319         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, skipback);
320         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ext_dev);
321         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, twohand);
322         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, source_tag);
323         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, mb);
324 }
325
326 static void trackpoint_disconnect(struct psmouse *psmouse)
327 {
328         sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj, &trackpoint_attr_group);
329
330         kfree(psmouse->private);
331         psmouse->private = NULL;
332 }
333
334 static int trackpoint_reconnect(struct psmouse *psmouse)
335 {
336         int reset_fail;
337
338         if (trackpoint_start_protocol(psmouse, NULL))
339                 return -1;
340
341         reset_fail = trackpoint_power_on_reset(&psmouse->ps2dev);
342         if (trackpoint_sync(psmouse, !reset_fail))
343                 return -1;
344
345         return 0;
346 }
347
348 int trackpoint_detect(struct psmouse *psmouse, bool set_properties)
349 {
350         struct ps2dev *ps2dev = &psmouse->ps2dev;
351         unsigned char firmware_id;
352         unsigned char button_info;
353         int error;
354
355         if (trackpoint_start_protocol(psmouse, &firmware_id))
356                 return -1;
357
358         if (!set_properties)
359                 return 0;
360
361         if (trackpoint_read(&psmouse->ps2dev, TP_EXT_BTN, &button_info)) {
362                 psmouse_warn(psmouse, "failed to get extended button data\n");
363                 button_info = 0;
364         }
365
366         psmouse->private = kzalloc(sizeof(struct trackpoint_data), GFP_KERNEL);
367         if (!psmouse->private)
368                 return -ENOMEM;
369
370         psmouse->vendor = "IBM";
371         psmouse->name = "TrackPoint";
372
373         psmouse->reconnect = trackpoint_reconnect;
374         psmouse->disconnect = trackpoint_disconnect;
375
376         if ((button_info & 0x0f) >= 3)
377                 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
378
379         trackpoint_defaults(psmouse->private);
380
381         error = trackpoint_power_on_reset(&psmouse->ps2dev);
382
383         /* Write defaults to TP only if reset fails. */
384         if (error)
385                 trackpoint_sync(psmouse, false);
386
387         error = sysfs_create_group(&ps2dev->serio->dev.kobj, &trackpoint_attr_group);
388         if (error) {
389                 psmouse_err(psmouse,
390                             "failed to create sysfs attributes, error: %d\n",
391                             error);
392                 kfree(psmouse->private);
393                 psmouse->private = NULL;
394                 return -1;
395         }
396
397         psmouse_info(psmouse,
398                      "IBM TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
399                      firmware_id,
400                      (button_info & 0xf0) >> 4, button_info & 0x0f);
401
402         return 0;
403 }
404