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.
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>
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",
27 [TP_VARIANT_JYT_SYNAPTICS] = "JYT_Synaptics",
28 [TP_VARIANT_SYNAPTICS] = "Synaptics",
32 * Power-on Reset: Resets all trackpoint parameters, including RAM values,
34 * Returns zero on success, non-zero on failure.
36 static int trackpoint_power_on_reset(struct ps2dev *ps2dev)
41 /* Issue POR command, and repeat up to once if 0xFC00 received */
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)))
46 } while (results[0] == 0xFC && results[1] == 0x00 && ++tries < 2);
48 /* Check for success response -- 0xAA00 */
49 if (results[0] != 0xAA || results[1] != 0x00)
56 * Device IO: read, write and toggle bit
58 static int trackpoint_read(struct ps2dev *ps2dev, u8 loc, u8 *results)
60 if (ps2_command(ps2dev, NULL, MAKE_PS2_CMD(0, 0, TP_COMMAND)) ||
61 ps2_command(ps2dev, results, MAKE_PS2_CMD(0, 1, loc))) {
68 static int trackpoint_write(struct ps2dev *ps2dev, u8 loc, u8 val)
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))) {
80 static int trackpoint_toggle_bit(struct ps2dev *ps2dev, u8 loc, u8 mask)
82 /* Bad things will happen if the loc param isn't in this range */
83 if (loc < 0x20 || loc >= 0x2F)
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))) {
96 static int trackpoint_update_bit(struct ps2dev *ps2dev,
97 u8 loc, u8 mask, u8 value)
102 trackpoint_read(ps2dev, loc, &data);
103 if (((data & mask) == mask) != !!value)
104 retval = trackpoint_toggle_bit(ps2dev, loc, mask);
110 * Trackpoint-specific attributes
112 struct trackpoint_attr_data {
120 static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse,
121 void *data, char *buf)
123 struct trackpoint_data *tp = psmouse->private;
124 struct trackpoint_attr_data *attr = data;
125 u8 value = *(u8 *)((void *)tp + attr->field_offset);
130 return sprintf(buf, "%u\n", value);
133 static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
134 const char *buf, size_t count)
136 struct trackpoint_data *tp = psmouse->private;
137 struct trackpoint_attr_data *attr = data;
138 u8 *field = (void *)tp + attr->field_offset;
142 err = kstrtou8(buf, 10, &value);
147 trackpoint_write(&psmouse->ps2dev, attr->command, value);
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, \
158 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
159 &trackpoint_attr_##_name, \
160 trackpoint_show_int_attr, trackpoint_set_int_attr)
162 static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
163 const char *buf, size_t count)
165 struct trackpoint_data *tp = psmouse->private;
166 struct trackpoint_attr_data *attr = data;
167 bool *field = (void *)tp + attr->field_offset;
171 err = kstrtobool(buf, &value);
178 if (*field != value) {
180 trackpoint_toggle_bit(&psmouse->ps2dev, attr->command, attr->mask);
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, \
191 .command = _command, \
194 .power_on_default = _default, \
196 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
197 &trackpoint_attr_##_name, \
198 trackpoint_show_int_attr, trackpoint_set_bit_attr)
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);
212 TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, false,
214 TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, false,
216 TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, true,
219 static bool trackpoint_is_attr_available(struct psmouse *psmouse,
220 struct attribute *attr)
222 struct trackpoint_data *tp = psmouse->private;
224 return tp->variant_id == TP_VARIANT_IBM ||
225 attr == &psmouse_attr_sensitivity.dattr.attr ||
226 attr == &psmouse_attr_press_to_select.dattr.attr;
229 static umode_t trackpoint_is_attr_visible(struct kobject *kobj,
230 struct attribute *attr, int n)
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);
236 return trackpoint_is_attr_available(psmouse, attr) ? attr->mode : 0;
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,
257 static struct attribute_group trackpoint_attr_group = {
258 .is_visible = trackpoint_is_attr_visible,
259 .attrs = trackpoint_attrs,
262 #define TRACKPOINT_UPDATE(_power_on, _psmouse, _tp, _name) \
264 struct trackpoint_attr_data *_attr = &trackpoint_attr_##_name; \
266 if ((!_power_on || _tp->_name != _attr->power_on_default) && \
267 trackpoint_is_attr_available(_psmouse, \
268 &psmouse_attr_##_name.dattr.attr)) { \
270 trackpoint_write(&_psmouse->ps2dev, \
271 _attr->command, _tp->_name); \
273 trackpoint_update_bit(&_psmouse->ps2dev, \
274 _attr->command, _attr->mask, \
279 #define TRACKPOINT_SET_POWER_ON_DEFAULT(_tp, _name) \
281 _tp->_name = trackpoint_attr_##_name.power_on_default; \
284 static int trackpoint_start_protocol(struct psmouse *psmouse,
285 u8 *variant_id, u8 *firmware_id)
290 error = ps2_command(&psmouse->ps2dev,
291 param, MAKE_PS2_CMD(0, 2, TP_READ_ID));
297 case TP_VARIANT_ALPS:
298 case TP_VARIANT_ELAN:
300 case TP_VARIANT_JYT_SYNAPTICS:
301 case TP_VARIANT_SYNAPTICS:
303 *variant_id = param[0];
305 *firmware_id = param[1];
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.
318 static int trackpoint_sync(struct psmouse *psmouse, bool in_power_on_state)
320 struct trackpoint_data *tp = psmouse->private;
322 if (!in_power_on_state && tp->variant_id == TP_VARIANT_IBM) {
324 * Disable features that may make device unusable
327 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND,
328 TP_MASK_TWOHAND, TP_DEF_TWOHAND);
330 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG,
331 TP_MASK_SOURCE_TAG, TP_DEF_SOURCE_TAG);
333 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_MB,
334 TP_MASK_MB, TP_DEF_MB);
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
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);
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);
362 static void trackpoint_defaults(struct trackpoint_data *tp)
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);
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);
382 static void trackpoint_disconnect(struct psmouse *psmouse)
384 device_remove_group(&psmouse->ps2dev.serio->dev,
385 &trackpoint_attr_group);
387 kfree(psmouse->private);
388 psmouse->private = NULL;
391 static int trackpoint_reconnect(struct psmouse *psmouse)
393 struct trackpoint_data *tp = psmouse->private;
397 error = trackpoint_start_protocol(psmouse, NULL, NULL);
401 was_reset = tp->variant_id == TP_VARIANT_IBM &&
402 trackpoint_power_on_reset(&psmouse->ps2dev) == 0;
404 error = trackpoint_sync(psmouse, was_reset);
411 int trackpoint_detect(struct psmouse *psmouse, bool set_properties)
413 struct ps2dev *ps2dev = &psmouse->ps2dev;
414 struct trackpoint_data *tp;
420 error = trackpoint_start_protocol(psmouse, &variant_id, &firmware_id);
427 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
431 trackpoint_defaults(tp);
432 tp->variant_id = variant_id;
433 tp->firmware_id = firmware_id;
435 psmouse->private = tp;
437 psmouse->vendor = trackpoint_variants[variant_id];
438 psmouse->name = "TrackPoint";
440 psmouse->reconnect = trackpoint_reconnect;
441 psmouse->disconnect = trackpoint_disconnect;
443 if (variant_id != TP_VARIANT_IBM) {
444 /* Newer variants do not support extended button query. */
447 error = trackpoint_read(ps2dev, TP_EXT_BTN, &button_info);
449 psmouse_warn(psmouse,
450 "failed to get extended button data, assuming 3 buttons\n");
452 } else if (!button_info) {
453 psmouse_warn(psmouse,
454 "got 0 in extended button data, assuming 3 buttons\n");
459 if ((button_info & 0x0f) >= 3)
460 input_set_capability(psmouse->dev, EV_KEY, BTN_MIDDLE);
462 __set_bit(INPUT_PROP_POINTER, psmouse->dev->propbit);
463 __set_bit(INPUT_PROP_POINTING_STICK, psmouse->dev->propbit);
465 if (variant_id != TP_VARIANT_IBM ||
466 trackpoint_power_on_reset(ps2dev) != 0) {
468 * Write defaults to TP if we did not reset the trackpoint.
470 trackpoint_sync(psmouse, false);
473 error = device_add_group(&ps2dev->serio->dev, &trackpoint_attr_group);
476 "failed to create sysfs attributes, error: %d\n",
478 kfree(psmouse->private);
479 psmouse->private = NULL;
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);