Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / input / mouse / trackpoint.c
blobbbd29220dbe99838ba69a48dc3dfb595ec0dae0f
1 /*
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.
9 */
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"
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",
30 * Power-on Reset: Resets all trackpoint parameters, including RAM values,
31 * to defaults.
32 * Returns zero on success, non-zero on failure.
34 static int trackpoint_power_on_reset(struct ps2dev *ps2dev)
36 u8 results[2];
37 int tries = 0;
39 /* Issue POR command, and repeat up to once if 0xFC00 received */
40 do {
41 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
42 ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 2, TP_POR)))
43 return -1;
44 } while (results[0] == 0xFC && results[1] == 0x00 && ++tries < 2);
46 /* Check for success response -- 0xAA00 */
47 if (results[0] != 0xAA || results[1] != 0x00)
48 return -ENODEV;
50 return 0;
54 * Device IO: read, write and toggle bit
56 static int trackpoint_read(struct ps2dev *ps2dev, u8 loc, u8 *results)
58 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
59 ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 1, loc))) {
60 return -1;
63 return 0;
66 static int trackpoint_write(struct ps2dev *ps2dev, u8 loc, u8 val)
68 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
69 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_WRITE_MEM)) ||
70 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
71 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, val))) {
72 return -1;
75 return 0;
78 static int trackpoint_toggle_bit(struct ps2dev *ps2dev, u8 loc, u8 mask)
80 /* Bad things will happen if the loc param isn't in this range */
81 if (loc < 0x20 || loc >= 0x2F)
82 return -1;
84 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
85 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_TOGGLE)) ||
86 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, loc)) ||
87 ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, mask))) {
88 return -1;
91 return 0;
94 static int trackpoint_update_bit(struct ps2dev *ps2dev,
95 u8 loc, u8 mask, u8 value)
97 int retval = 0;
98 u8 data;
100 trackpoint_read(ps2dev, loc, &data);
101 if (((data & mask) == mask) != !!value)
102 retval = trackpoint_toggle_bit(ps2dev, loc, mask);
104 return retval;
108 * Trackpoint-specific attributes
110 struct trackpoint_attr_data {
111 size_t field_offset;
112 u8 command;
113 u8 mask;
114 bool inverted;
115 u8 power_on_default;
118 static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse,
119 void *data, char *buf)
121 struct trackpoint_data *tp = psmouse->private;
122 struct trackpoint_attr_data *attr = data;
123 u8 value = *(u8 *)((void *)tp + attr->field_offset);
125 if (attr->inverted)
126 value = !value;
128 return sprintf(buf, "%u\n", value);
131 static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
132 const char *buf, size_t count)
134 struct trackpoint_data *tp = psmouse->private;
135 struct trackpoint_attr_data *attr = data;
136 u8 *field = (void *)tp + attr->field_offset;
137 u8 value;
138 int err;
140 err = kstrtou8(buf, 10, &value);
141 if (err)
142 return err;
144 *field = value;
145 trackpoint_write(&psmouse->ps2dev, attr->command, value);
147 return count;
150 #define TRACKPOINT_INT_ATTR(_name, _command, _default) \
151 static struct trackpoint_attr_data trackpoint_attr_##_name = { \
152 .field_offset = offsetof(struct trackpoint_data, _name), \
153 .command = _command, \
154 .power_on_default = _default, \
155 }; \
156 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
157 &trackpoint_attr_##_name, \
158 trackpoint_show_int_attr, trackpoint_set_int_attr)
160 static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
161 const char *buf, size_t count)
163 struct trackpoint_data *tp = psmouse->private;
164 struct trackpoint_attr_data *attr = data;
165 bool *field = (void *)tp + attr->field_offset;
166 bool value;
167 int err;
169 err = kstrtobool(buf, &value);
170 if (err)
171 return err;
173 if (attr->inverted)
174 value = !value;
176 if (*field != value) {
177 *field = value;
178 trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask);
181 return count;
185 #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv, _default) \
186 static struct trackpoint_attr_data trackpoint_attr_##_name = { \
187 .field_offset = offsetof(struct trackpoint_data, \
188 _name), \
189 .command = _command, \
190 .mask = _mask, \
191 .inverted = _inv, \
192 .power_on_default = _default, \
193 }; \
194 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
195 &trackpoint_attr_##_name, \
196 trackpoint_show_int_attr, trackpoint_set_bit_attr)
198 TRACKPOINT_INT_ATTR(sensitivity, TP_SENS, TP_DEF_SENS);
199 TRACKPOINT_INT_ATTR(speed, TP_SPEED, TP_DEF_SPEED);
200 TRACKPOINT_INT_ATTR(inertia, TP_INERTIA, TP_DEF_INERTIA);
201 TRACKPOINT_INT_ATTR(reach, TP_REACH, TP_DEF_REACH);
202 TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS, TP_DEF_DRAGHYS);
203 TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG, TP_DEF_MINDRAG);
204 TRACKPOINT_INT_ATTR(thresh, TP_THRESH, TP_DEF_THRESH);
205 TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH, TP_DEF_UP_THRESH);
206 TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME, TP_DEF_Z_TIME);
207 TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV, TP_DEF_JENKS_CURV);
208 TRACKPOINT_INT_ATTR(drift_time, TP_DRIFT_TIME, TP_DEF_DRIFT_TIME);
210 TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, false,
211 TP_DEF_PTSON);
212 TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, false,
213 TP_DEF_SKIPBACK);
214 TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, true,
215 TP_DEF_EXT_DEV);
217 static bool trackpoint_is_attr_available(struct psmouse *psmouse,
218 struct attribute *attr)
220 struct trackpoint_data *tp = psmouse->private;
222 return tp->variant_id == TP_VARIANT_IBM ||
223 attr == &psmouse_attr_sensitivity.dattr.attr ||
224 attr == &psmouse_attr_press_to_select.dattr.attr;
227 static umode_t trackpoint_is_attr_visible(struct kobject *kobj,
228 struct attribute *attr, int n)
230 struct device *dev = container_of(kobj, struct device, kobj);
231 struct serio *serio = to_serio_port(dev);
232 struct psmouse *psmouse = serio_get_drvdata(serio);
234 return trackpoint_is_attr_available(psmouse, attr) ? attr->mode : 0;
237 static struct attribute *trackpoint_attrs[] = {
238 &psmouse_attr_sensitivity.dattr.attr,
239 &psmouse_attr_speed.dattr.attr,
240 &psmouse_attr_inertia.dattr.attr,
241 &psmouse_attr_reach.dattr.attr,
242 &psmouse_attr_draghys.dattr.attr,
243 &psmouse_attr_mindrag.dattr.attr,
244 &psmouse_attr_thresh.dattr.attr,
245 &psmouse_attr_upthresh.dattr.attr,
246 &psmouse_attr_ztime.dattr.attr,
247 &psmouse_attr_jenks.dattr.attr,
248 &psmouse_attr_drift_time.dattr.attr,
249 &psmouse_attr_press_to_select.dattr.attr,
250 &psmouse_attr_skipback.dattr.attr,
251 &psmouse_attr_ext_dev.dattr.attr,
252 NULL
255 static struct attribute_group trackpoint_attr_group = {
256 .is_visible = trackpoint_is_attr_visible,
257 .attrs = trackpoint_attrs,
260 #define TRACKPOINT_UPDATE(_power_on, _psmouse, _tp, _name) \
261 do { \
262 struct trackpoint_attr_data *_attr = &trackpoint_attr_##_name; \
264 if ((!_power_on || _tp->_name != _attr->power_on_default) && \
265 trackpoint_is_attr_available(_psmouse, \
266 &psmouse_attr_##_name.dattr.attr)) { \
267 if (!_attr->mask) \
268 trackpoint_write(&_psmouse->ps2dev, \
269 _attr->command, _tp->_name); \
270 else \
271 trackpoint_update_bit(&_psmouse->ps2dev, \
272 _attr->command, _attr->mask, \
273 _tp->_name); \
275 } while (0)
277 #define TRACKPOINT_SET_POWER_ON_DEFAULT(_tp, _name) \
278 do { \
279 _tp->_name = trackpoint_attr_##_name.power_on_default; \
280 } while (0)
282 static int trackpoint_start_protocol(struct psmouse *psmouse,
283 u8 *variant_id, u8 *firmware_id)
285 u8 param[2] = { 0 };
286 int error;
288 error = ps2_command(&psmouse->ps2dev,
289 param, MAKE_PS2_CMD(0, 2, TP_READ_ID));
290 if (error)
291 return error;
293 switch (param[0]) {
294 case TP_VARIANT_IBM:
295 case TP_VARIANT_ALPS:
296 case TP_VARIANT_ELAN:
297 case TP_VARIANT_NXP:
298 if (variant_id)
299 *variant_id = param[0];
300 if (firmware_id)
301 *firmware_id = param[1];
302 return 0;
305 return -ENODEV;
309 * Write parameters to trackpad.
310 * in_power_on_state: Set to true if TP is in default / power-on state (ex. if
311 * power-on reset was run). If so, values will only be
312 * written to TP if they differ from power-on default.
314 static int trackpoint_sync(struct psmouse *psmouse, bool in_power_on_state)
316 struct trackpoint_data *tp = psmouse->private;
318 if (!in_power_on_state && tp->variant_id == TP_VARIANT_IBM) {
320 * Disable features that may make device unusable
321 * with this driver.
323 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND,
324 TP_MASK_TWOHAND, TP_DEF_TWOHAND);
326 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG,
327 TP_MASK_SOURCE_TAG, TP_DEF_SOURCE_TAG);
329 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_MB,
330 TP_MASK_MB, TP_DEF_MB);
334 * These properties can be changed in this driver. Only
335 * configure them if the values are non-default or if the TP is in
336 * an unknown state.
338 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, sensitivity);
339 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, inertia);
340 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, speed);
341 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, reach);
342 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, draghys);
343 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, mindrag);
344 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, thresh);
345 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, upthresh);
346 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ztime);
347 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, jenks);
348 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, drift_time);
350 /* toggles */
351 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, press_to_select);
352 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, skipback);
353 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ext_dev);
355 return 0;
358 static void trackpoint_defaults(struct trackpoint_data *tp)
360 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, sensitivity);
361 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, speed);
362 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, reach);
363 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, draghys);
364 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, mindrag);
365 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, thresh);
366 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, upthresh);
367 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ztime);
368 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, jenks);
369 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, drift_time);
370 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, inertia);
372 /* toggles */
373 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, press_to_select);
374 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, skipback);
375 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ext_dev);
378 static void trackpoint_disconnect(struct psmouse *psmouse)
380 device_remove_group(&psmouse->ps2dev.serio->dev,
381 &trackpoint_attr_group);
383 kfree(psmouse->private);
384 psmouse->private = NULL;
387 static int trackpoint_reconnect(struct psmouse *psmouse)
389 struct trackpoint_data *tp = psmouse->private;
390 int error;
391 bool was_reset;
393 error = trackpoint_start_protocol(psmouse, NULL, NULL);
394 if (error)
395 return error;
397 was_reset = tp->variant_id == TP_VARIANT_IBM &&
398 trackpoint_power_on_reset(&psmouse->ps2dev) == 0;
400 error = trackpoint_sync(psmouse, was_reset);
401 if (error)
402 return error;
404 return 0;
407 int trackpoint_detect(struct psmouse *psmouse, bool set_properties)
409 struct ps2dev *ps2dev = &psmouse->ps2dev;
410 struct trackpoint_data *tp;
411 u8 variant_id;
412 u8 firmware_id;
413 u8 button_info;
414 int error;
416 error = trackpoint_start_protocol(psmouse, &variant_id, &firmware_id);
417 if (error)
418 return error;
420 if (!set_properties)
421 return 0;
423 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
424 if (!tp)
425 return -ENOMEM;
427 trackpoint_defaults(tp);
428 tp->variant_id = variant_id;
429 tp->firmware_id = firmware_id;
431 psmouse->private = tp;
433 psmouse->vendor = trackpoint_variants[variant_id];
434 psmouse->name = "TrackPoint";
436 psmouse->reconnect = trackpoint_reconnect;
437 psmouse->disconnect = trackpoint_disconnect;
439 if (variant_id != TP_VARIANT_IBM) {
440 /* Newer variants do not support extended button query. */
441 button_info = 0x33;
442 } else {
443 error = trackpoint_read(ps2dev, TP_EXT_BTN, &button_info);
444 if (error) {
445 psmouse_warn(psmouse,
446 "failed to get extended button data, assuming 3 buttons\n");
447 button_info = 0x33;
448 } else if (!button_info) {
449 psmouse_warn(psmouse,
450 "got 0 in extended button data, assuming 3 buttons\n");
451 button_info = 0x33;
455 if ((button_info & 0x0f) >= 3)
456 input_set_capability(psmouse->dev, EV_KEY, BTN_MIDDLE);
458 __set_bit(INPUT_PROP_POINTER, psmouse->dev->propbit);
459 __set_bit(INPUT_PROP_POINTING_STICK, psmouse->dev->propbit);
461 if (variant_id != TP_VARIANT_IBM ||
462 trackpoint_power_on_reset(ps2dev) != 0) {
464 * Write defaults to TP if we did not reset the trackpoint.
466 trackpoint_sync(psmouse, false);
469 error = device_add_group(&ps2dev->serio->dev, &trackpoint_attr_group);
470 if (error) {
471 psmouse_err(psmouse,
472 "failed to create sysfs attributes, error: %d\n",
473 error);
474 kfree(psmouse->private);
475 psmouse->private = NULL;
476 return -1;
479 psmouse_info(psmouse,
480 "%s TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
481 psmouse->vendor, firmware_id,
482 (button_info & 0xf0) >> 4, button_info & 0x0f);
484 return 0;