GNU Linux-libre 4.14.266-gnu1
[releases.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 <linux/uaccess.h>
19 #include "psmouse.h"
20 #include "trackpoint.h"
21
22 static const char * const trackpoint_variants[] = {
23         [TP_VARIANT_IBM]                = "IBM",
24         [TP_VARIANT_ALPS]               = "ALPS",
25         [TP_VARIANT_ELAN]               = "Elan",
26         [TP_VARIANT_NXP]                = "NXP",
27         [TP_VARIANT_JYT_SYNAPTICS]      = "JYT_Synaptics",
28         [TP_VARIANT_SYNAPTICS]          = "Synaptics",
29 };
30
31 /*
32  * Power-on Reset: Resets all trackpoint parameters, including RAM values,
33  * to defaults.
34  * Returns zero on success, non-zero on failure.
35  */
36 static int trackpoint_power_on_reset(struct ps2dev *ps2dev)
37 {
38         u8 results[2];
39         int tries = 0;
40
41         /* Issue POR command, and repeat up to once if 0xFC00 received */
42         do {
43                 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
44                     ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 2, TP_POR)))
45                         return -1;
46         } while (results[0] == 0xFC && results[1] == 0x00 && ++tries < 2);
47
48         /* Check for success response -- 0xAA00 */
49         if (results[0] != 0xAA || results[1] != 0x00)
50                 return -ENODEV;
51
52         return 0;
53 }
54
55 /*
56  * Device IO: read, write and toggle bit
57  */
58 static int trackpoint_read(struct ps2dev *ps2dev, u8 loc, u8 *results)
59 {
60         if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
61             ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 1, loc))) {
62                 return -1;
63         }
64
65         return 0;
66 }
67
68 static int trackpoint_write(struct ps2dev *ps2dev, u8 loc, u8 val)
69 {
70         if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
71             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_WRITE_MEM)) ||
72             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
73             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, val))) {
74                 return -1;
75         }
76
77         return 0;
78 }
79
80 static int trackpoint_toggle_bit(struct ps2dev *ps2dev, u8 loc, u8 mask)
81 {
82         /* Bad things will happen if the loc param isn't in this range */
83         if (loc < 0x20 || loc >= 0x2F)
84                 return -1;
85
86         if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
87             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_TOGGLE)) ||
88             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
89             ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, mask))) {
90                 return -1;
91         }
92
93         return 0;
94 }
95
96 static int trackpoint_update_bit(struct ps2dev *ps2dev,
97                                  u8 loc, u8 mask, u8 value)
98 {
99         int retval = 0;
100         u8 data;
101
102         trackpoint_read(ps2dev, loc, &data);
103         if (((data & mask) == mask) != !!value)
104                 retval = trackpoint_toggle_bit(ps2dev, loc, mask);
105
106         return retval;
107 }
108
109 /*
110  * Trackpoint-specific attributes
111  */
112 struct trackpoint_attr_data {
113         size_t field_offset;
114         u8 command;
115         u8 mask;
116         bool inverted;
117         u8 power_on_default;
118 };
119
120 static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse,
121                                         void *data, char *buf)
122 {
123         struct trackpoint_data *tp = psmouse->private;
124         struct trackpoint_attr_data *attr = data;
125         u8 value = *(u8 *)((void *)tp + attr->field_offset);
126
127         if (attr->inverted)
128                 value = !value;
129
130         return sprintf(buf, "%u\n", value);
131 }
132
133 static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
134                                         const char *buf, size_t count)
135 {
136         struct trackpoint_data *tp = psmouse->private;
137         struct trackpoint_attr_data *attr = data;
138         u8 *field = (void *)tp + attr->field_offset;
139         u8 value;
140         int err;
141
142         err = kstrtou8(buf, 10, &value);
143         if (err)
144                 return err;
145
146         *field = value;
147         trackpoint_write(&psmouse->ps2dev, attr->command, value);
148
149         return count;
150 }
151
152 #define TRACKPOINT_INT_ATTR(_name, _command, _default)                          \
153         static struct trackpoint_attr_data trackpoint_attr_##_name = {          \
154                 .field_offset = offsetof(struct trackpoint_data, _name),        \
155                 .command = _command,                                            \
156                 .power_on_default = _default,                                   \
157         };                                                                      \
158         PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                           \
159                             &trackpoint_attr_##_name,                           \
160                             trackpoint_show_int_attr, trackpoint_set_int_attr)
161
162 static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
163                                         const char *buf, size_t count)
164 {
165         struct trackpoint_data *tp = psmouse->private;
166         struct trackpoint_attr_data *attr = data;
167         bool *field = (void *)tp + attr->field_offset;
168         bool value;
169         int err;
170
171         err = kstrtobool(buf, &value);
172         if (err)
173                 return err;
174
175         if (attr->inverted)
176                 value = !value;
177
178         if (*field != value) {
179                 *field = value;
180                 trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask);
181         }
182
183         return count;
184 }
185
186
187 #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv, _default)     \
188 static struct trackpoint_attr_data trackpoint_attr_##_name = {          \
189         .field_offset           = offsetof(struct trackpoint_data,      \
190                                            _name),                      \
191         .command                = _command,                             \
192         .mask                   = _mask,                                \
193         .inverted               = _inv,                                 \
194         .power_on_default       = _default,                             \
195         };                                                              \
196 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO,                           \
197                     &trackpoint_attr_##_name,                           \
198                     trackpoint_show_int_attr, trackpoint_set_bit_attr)
199
200 TRACKPOINT_INT_ATTR(sensitivity, TP_SENS, TP_DEF_SENS);
201 TRACKPOINT_INT_ATTR(speed, TP_SPEED, TP_DEF_SPEED);
202 TRACKPOINT_INT_ATTR(inertia, TP_INERTIA, TP_DEF_INERTIA);
203 TRACKPOINT_INT_ATTR(reach, TP_REACH, TP_DEF_REACH);
204 TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS, TP_DEF_DRAGHYS);
205 TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG, TP_DEF_MINDRAG);
206 TRACKPOINT_INT_ATTR(thresh, TP_THRESH, TP_DEF_THRESH);
207 TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH, TP_DEF_UP_THRESH);
208 TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME, TP_DEF_Z_TIME);
209 TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV, TP_DEF_JENKS_CURV);
210 TRACKPOINT_INT_ATTR(drift_time, TP_DRIFT_TIME, TP_DEF_DRIFT_TIME);
211
212 TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, false,
213                     TP_DEF_PTSON);
214 TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, false,
215                     TP_DEF_SKIPBACK);
216 TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, true,
217                     TP_DEF_EXT_DEV);
218
219 static bool trackpoint_is_attr_available(struct psmouse *psmouse,
220                                          struct attribute *attr)
221 {
222         struct trackpoint_data *tp = psmouse->private;
223
224         return tp->variant_id == TP_VARIANT_IBM ||
225                 attr == &psmouse_attr_sensitivity.dattr.attr ||
226                 attr == &psmouse_attr_press_to_select.dattr.attr;
227 }
228
229 static umode_t trackpoint_is_attr_visible(struct kobject *kobj,
230                                           struct attribute *attr, int n)
231 {
232         struct device *dev = container_of(kobj, struct device, kobj);
233         struct serio *serio = to_serio_port(dev);
234         struct psmouse *psmouse = serio_get_drvdata(serio);
235
236         return trackpoint_is_attr_available(psmouse, attr) ? attr->mode : 0;
237 }
238
239 static struct attribute *trackpoint_attrs[] = {
240         &psmouse_attr_sensitivity.dattr.attr,
241         &psmouse_attr_speed.dattr.attr,
242         &psmouse_attr_inertia.dattr.attr,
243         &psmouse_attr_reach.dattr.attr,
244         &psmouse_attr_draghys.dattr.attr,
245         &psmouse_attr_mindrag.dattr.attr,
246         &psmouse_attr_thresh.dattr.attr,
247         &psmouse_attr_upthresh.dattr.attr,
248         &psmouse_attr_ztime.dattr.attr,
249         &psmouse_attr_jenks.dattr.attr,
250         &psmouse_attr_drift_time.dattr.attr,
251         &psmouse_attr_press_to_select.dattr.attr,
252         &psmouse_attr_skipback.dattr.attr,
253         &psmouse_attr_ext_dev.dattr.attr,
254         NULL
255 };
256
257 static struct attribute_group trackpoint_attr_group = {
258         .is_visible     = trackpoint_is_attr_visible,
259         .attrs          = trackpoint_attrs,
260 };
261
262 #define TRACKPOINT_UPDATE(_power_on, _psmouse, _tp, _name)              \
263 do {                                                                    \
264         struct trackpoint_attr_data *_attr = &trackpoint_attr_##_name;  \
265                                                                         \
266         if ((!_power_on || _tp->_name != _attr->power_on_default) &&    \
267             trackpoint_is_attr_available(_psmouse,                      \
268                                 &psmouse_attr_##_name.dattr.attr)) {    \
269                 if (!_attr->mask)                                       \
270                         trackpoint_write(&_psmouse->ps2dev,             \
271                                          _attr->command, _tp->_name);   \
272                 else                                                    \
273                         trackpoint_update_bit(&_psmouse->ps2dev,        \
274                                         _attr->command, _attr->mask,    \
275                                         _tp->_name);                    \
276         }                                                               \
277 } while (0)
278
279 #define TRACKPOINT_SET_POWER_ON_DEFAULT(_tp, _name)                     \
280 do {                                                                    \
281         _tp->_name = trackpoint_attr_##_name.power_on_default;          \
282 } while (0)
283
284 static int trackpoint_start_protocol(struct psmouse *psmouse,
285                                      u8 *variant_id, u8 *firmware_id)
286 {
287         u8 param[2] = { 0 };
288         int error;
289
290         error = ps2_command(&psmouse->ps2dev,
291                             param, MAKE_PS2_CMD(0, 2, TP_READ_ID));
292         if (error)
293                 return error;
294
295         switch (param[0]) {
296         case TP_VARIANT_IBM:
297         case TP_VARIANT_ALPS:
298         case TP_VARIANT_ELAN:
299         case TP_VARIANT_NXP:
300         case TP_VARIANT_JYT_SYNAPTICS:
301         case TP_VARIANT_SYNAPTICS:
302                 if (variant_id)
303                         *variant_id = param[0];
304                 if (firmware_id)
305                         *firmware_id = param[1];
306                 return 0;
307         }
308
309         return -ENODEV;
310 }
311
312 /*
313  * Write parameters to trackpad.
314  * in_power_on_state: Set to true if TP is in default / power-on state (ex. if
315  *                    power-on reset was run). If so, values will only be
316  *                    written to TP if they differ from power-on default.
317  */
318 static int trackpoint_sync(struct psmouse *psmouse, bool in_power_on_state)
319 {
320         struct trackpoint_data *tp = psmouse->private;
321
322         if (!in_power_on_state && tp->variant_id == TP_VARIANT_IBM) {
323                 /*
324                  * Disable features that may make device unusable
325                  * with this driver.
326                  */
327                 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND,
328                                       TP_MASK_TWOHAND, TP_DEF_TWOHAND);
329
330                 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG,
331                                       TP_MASK_SOURCE_TAG, TP_DEF_SOURCE_TAG);
332
333                 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_MB,
334                                       TP_MASK_MB, TP_DEF_MB);
335         }
336
337         /*
338          * These properties can be changed in this driver. Only
339          * configure them if the values are non-default or if the TP is in
340          * an unknown state.
341          */
342         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, sensitivity);
343         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, inertia);
344         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, speed);
345         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, reach);
346         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, draghys);
347         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, mindrag);
348         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, thresh);
349         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, upthresh);
350         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ztime);
351         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, jenks);
352         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, drift_time);
353
354         /* toggles */
355         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, press_to_select);
356         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, skipback);
357         TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ext_dev);
358
359         return 0;
360 }
361
362 static void trackpoint_defaults(struct trackpoint_data *tp)
363 {
364         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, sensitivity);
365         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, speed);
366         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, reach);
367         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, draghys);
368         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, mindrag);
369         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, thresh);
370         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, upthresh);
371         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ztime);
372         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, jenks);
373         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, drift_time);
374         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, inertia);
375
376         /* toggles */
377         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, press_to_select);
378         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, skipback);
379         TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ext_dev);
380 }
381
382 static void trackpoint_disconnect(struct psmouse *psmouse)
383 {
384         device_remove_group(&psmouse->ps2dev.serio->dev,
385                             &trackpoint_attr_group);
386
387         kfree(psmouse->private);
388         psmouse->private = NULL;
389 }
390
391 static int trackpoint_reconnect(struct psmouse *psmouse)
392 {
393         struct trackpoint_data *tp = psmouse->private;
394         int error;
395         bool was_reset;
396
397         error = trackpoint_start_protocol(psmouse, NULL, NULL);
398         if (error)
399                 return error;
400
401         was_reset = tp->variant_id == TP_VARIANT_IBM &&
402                     trackpoint_power_on_reset(&psmouse->ps2dev) == 0;
403
404         error = trackpoint_sync(psmouse, was_reset);
405         if (error)
406                 return error;
407
408         return 0;
409 }
410
411 int trackpoint_detect(struct psmouse *psmouse, bool set_properties)
412 {
413         struct ps2dev *ps2dev = &psmouse->ps2dev;
414         struct trackpoint_data *tp;
415         u8 variant_id;
416         u8 firmware_id;
417         u8 button_info;
418         int error;
419
420         error = trackpoint_start_protocol(psmouse, &variant_id, &firmware_id);
421         if (error)
422                 return error;
423
424         if (!set_properties)
425                 return 0;
426
427         tp = kzalloc(sizeof(*tp), GFP_KERNEL);
428         if (!tp)
429                 return -ENOMEM;
430
431         trackpoint_defaults(tp);
432         tp->variant_id = variant_id;
433         tp->firmware_id = firmware_id;
434
435         psmouse->private = tp;
436
437         psmouse->vendor = trackpoint_variants[variant_id];
438         psmouse->name = "TrackPoint";
439
440         psmouse->reconnect = trackpoint_reconnect;
441         psmouse->disconnect = trackpoint_disconnect;
442
443         if (variant_id != TP_VARIANT_IBM) {
444                 /* Newer variants do not support extended button query. */
445                 button_info = 0x33;
446         } else {
447                 error = trackpoint_read(ps2dev, TP_EXT_BTN, &button_info);
448                 if (error) {
449                         psmouse_warn(psmouse,
450                                      "failed to get extended button data, assuming 3 buttons\n");
451                         button_info = 0x33;
452                 } else if (!button_info) {
453                         psmouse_warn(psmouse,
454                                      "got 0 in extended button data, assuming 3 buttons\n");
455                         button_info = 0x33;
456                 }
457         }
458
459         if ((button_info & 0x0f) >= 3)
460                 input_set_capability(psmouse->dev, EV_KEY, BTN_MIDDLE);
461
462         __set_bit(INPUT_PROP_POINTER, psmouse->dev->propbit);
463         __set_bit(INPUT_PROP_POINTING_STICK, psmouse->dev->propbit);
464
465         if (variant_id != TP_VARIANT_IBM ||
466             trackpoint_power_on_reset(ps2dev) != 0) {
467                 /*
468                  * Write defaults to TP if we did not reset the trackpoint.
469                  */
470                 trackpoint_sync(psmouse, false);
471         }
472
473         error = device_add_group(&ps2dev->serio->dev, &trackpoint_attr_group);
474         if (error) {
475                 psmouse_err(psmouse,
476                             "failed to create sysfs attributes, error: %d\n",
477                             error);
478                 kfree(psmouse->private);
479                 psmouse->private = NULL;
480                 return -1;
481         }
482
483         psmouse_info(psmouse,
484                      "%s TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
485                      psmouse->vendor, firmware_id,
486                      (button_info & 0xf0) >> 4, button_info & 0x0f);
487
488         return 0;
489 }
490