1 // SPDX-License-Identifier: GPL-2.0-only
3 * Stephen Evanchik <evanchsa@gmail.com>
5 * Trademarks are the property of their respective owners.
8 #include <linux/slab.h>
9 #include <linux/delay.h>
10 #include <linux/serio.h>
11 #include <linux/module.h>
12 #include <linux/input.h>
13 #include <linux/libps2.h>
14 #include <linux/proc_fs.h>
15 #include <linux/uaccess.h>
17 #include "trackpoint.h"
19 static const char * const trackpoint_variants
[] = {
20 [TP_VARIANT_IBM
] = "IBM",
21 [TP_VARIANT_ALPS
] = "ALPS",
22 [TP_VARIANT_ELAN
] = "Elan",
23 [TP_VARIANT_NXP
] = "NXP",
24 [TP_VARIANT_JYT_SYNAPTICS
] = "JYT_Synaptics",
25 [TP_VARIANT_SYNAPTICS
] = "Synaptics",
29 * Power-on Reset: Resets all trackpoint parameters, including RAM values,
31 * Returns zero on success, non-zero on failure.
33 static int trackpoint_power_on_reset(struct ps2dev
*ps2dev
)
35 u8 param
[2] = { TP_POR
};
38 err
= ps2_command(ps2dev
, param
, MAKE_PS2_CMD(1, 2, TP_COMMAND
));
42 /* Check for success response -- 0xAA00 */
43 if (param
[0] != 0xAA || param
[1] != 0x00)
50 * Device IO: read, write and toggle bit
52 static int trackpoint_read(struct ps2dev
*ps2dev
, u8 loc
, u8
*results
)
56 return ps2_command(ps2dev
, results
, MAKE_PS2_CMD(1, 1, TP_COMMAND
));
59 static int trackpoint_write(struct ps2dev
*ps2dev
, u8 loc
, u8 val
)
61 u8 param
[3] = { TP_WRITE_MEM
, loc
, val
};
63 return ps2_command(ps2dev
, param
, MAKE_PS2_CMD(3, 0, TP_COMMAND
));
66 static int trackpoint_toggle_bit(struct ps2dev
*ps2dev
, u8 loc
, u8 mask
)
68 u8 param
[3] = { TP_TOGGLE
, loc
, mask
};
70 /* Bad things will happen if the loc param isn't in this range */
71 if (loc
< 0x20 || loc
>= 0x2F)
74 return ps2_command(ps2dev
, param
, MAKE_PS2_CMD(3, 0, TP_COMMAND
));
77 static int trackpoint_update_bit(struct ps2dev
*ps2dev
,
78 u8 loc
, u8 mask
, u8 value
)
83 retval
= trackpoint_read(ps2dev
, loc
, &data
);
87 if (((data
& mask
) == mask
) != !!value
)
88 retval
= trackpoint_toggle_bit(ps2dev
, loc
, mask
);
94 * Trackpoint-specific attributes
96 struct trackpoint_attr_data
{
104 static ssize_t
trackpoint_show_int_attr(struct psmouse
*psmouse
,
105 void *data
, char *buf
)
107 struct trackpoint_data
*tp
= psmouse
->private;
108 struct trackpoint_attr_data
*attr
= data
;
109 u8 value
= *(u8
*)((void *)tp
+ attr
->field_offset
);
114 return sprintf(buf
, "%u\n", value
);
117 static ssize_t
trackpoint_set_int_attr(struct psmouse
*psmouse
, void *data
,
118 const char *buf
, size_t count
)
120 struct trackpoint_data
*tp
= psmouse
->private;
121 struct trackpoint_attr_data
*attr
= data
;
122 u8
*field
= (void *)tp
+ attr
->field_offset
;
126 err
= kstrtou8(buf
, 10, &value
);
131 err
= trackpoint_write(&psmouse
->ps2dev
, attr
->command
, value
);
136 #define TRACKPOINT_INT_ATTR(_name, _command, _default) \
137 static struct trackpoint_attr_data trackpoint_attr_##_name = { \
138 .field_offset = offsetof(struct trackpoint_data, _name), \
139 .command = _command, \
140 .power_on_default = _default, \
142 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
143 &trackpoint_attr_##_name, \
144 trackpoint_show_int_attr, trackpoint_set_int_attr)
146 static ssize_t
trackpoint_set_bit_attr(struct psmouse
*psmouse
, void *data
,
147 const char *buf
, size_t count
)
149 struct trackpoint_data
*tp
= psmouse
->private;
150 struct trackpoint_attr_data
*attr
= data
;
151 bool *field
= (void *)tp
+ attr
->field_offset
;
155 err
= kstrtobool(buf
, &value
);
162 if (*field
!= value
) {
164 err
= trackpoint_toggle_bit(&psmouse
->ps2dev
,
165 attr
->command
, attr
->mask
);
172 #define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv, _default) \
173 static struct trackpoint_attr_data trackpoint_attr_##_name = { \
174 .field_offset = offsetof(struct trackpoint_data, \
176 .command = _command, \
179 .power_on_default = _default, \
181 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
182 &trackpoint_attr_##_name, \
183 trackpoint_show_int_attr, trackpoint_set_bit_attr)
185 TRACKPOINT_INT_ATTR(sensitivity
, TP_SENS
, TP_DEF_SENS
);
186 TRACKPOINT_INT_ATTR(speed
, TP_SPEED
, TP_DEF_SPEED
);
187 TRACKPOINT_INT_ATTR(inertia
, TP_INERTIA
, TP_DEF_INERTIA
);
188 TRACKPOINT_INT_ATTR(reach
, TP_REACH
, TP_DEF_REACH
);
189 TRACKPOINT_INT_ATTR(draghys
, TP_DRAGHYS
, TP_DEF_DRAGHYS
);
190 TRACKPOINT_INT_ATTR(mindrag
, TP_MINDRAG
, TP_DEF_MINDRAG
);
191 TRACKPOINT_INT_ATTR(thresh
, TP_THRESH
, TP_DEF_THRESH
);
192 TRACKPOINT_INT_ATTR(upthresh
, TP_UP_THRESH
, TP_DEF_UP_THRESH
);
193 TRACKPOINT_INT_ATTR(ztime
, TP_Z_TIME
, TP_DEF_Z_TIME
);
194 TRACKPOINT_INT_ATTR(jenks
, TP_JENKS_CURV
, TP_DEF_JENKS_CURV
);
195 TRACKPOINT_INT_ATTR(drift_time
, TP_DRIFT_TIME
, TP_DEF_DRIFT_TIME
);
197 TRACKPOINT_BIT_ATTR(press_to_select
, TP_TOGGLE_PTSON
, TP_MASK_PTSON
, false,
199 TRACKPOINT_BIT_ATTR(skipback
, TP_TOGGLE_SKIPBACK
, TP_MASK_SKIPBACK
, false,
201 TRACKPOINT_BIT_ATTR(ext_dev
, TP_TOGGLE_EXT_DEV
, TP_MASK_EXT_DEV
, true,
204 static bool trackpoint_is_attr_available(struct psmouse
*psmouse
,
205 struct attribute
*attr
)
207 struct trackpoint_data
*tp
= psmouse
->private;
209 return tp
->variant_id
== TP_VARIANT_IBM
||
210 attr
== &psmouse_attr_sensitivity
.dattr
.attr
||
211 attr
== &psmouse_attr_press_to_select
.dattr
.attr
;
214 static umode_t
trackpoint_is_attr_visible(struct kobject
*kobj
,
215 struct attribute
*attr
, int n
)
217 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
218 struct serio
*serio
= to_serio_port(dev
);
219 struct psmouse
*psmouse
= serio_get_drvdata(serio
);
221 return trackpoint_is_attr_available(psmouse
, attr
) ? attr
->mode
: 0;
224 static struct attribute
*trackpoint_attrs
[] = {
225 &psmouse_attr_sensitivity
.dattr
.attr
,
226 &psmouse_attr_speed
.dattr
.attr
,
227 &psmouse_attr_inertia
.dattr
.attr
,
228 &psmouse_attr_reach
.dattr
.attr
,
229 &psmouse_attr_draghys
.dattr
.attr
,
230 &psmouse_attr_mindrag
.dattr
.attr
,
231 &psmouse_attr_thresh
.dattr
.attr
,
232 &psmouse_attr_upthresh
.dattr
.attr
,
233 &psmouse_attr_ztime
.dattr
.attr
,
234 &psmouse_attr_jenks
.dattr
.attr
,
235 &psmouse_attr_drift_time
.dattr
.attr
,
236 &psmouse_attr_press_to_select
.dattr
.attr
,
237 &psmouse_attr_skipback
.dattr
.attr
,
238 &psmouse_attr_ext_dev
.dattr
.attr
,
242 static struct attribute_group trackpoint_attr_group
= {
243 .is_visible
= trackpoint_is_attr_visible
,
244 .attrs
= trackpoint_attrs
,
247 #define TRACKPOINT_UPDATE(_power_on, _psmouse, _tp, _name) \
249 struct trackpoint_attr_data *_attr = &trackpoint_attr_##_name; \
251 if ((!_power_on || _tp->_name != _attr->power_on_default) && \
252 trackpoint_is_attr_available(_psmouse, \
253 &psmouse_attr_##_name.dattr.attr)) { \
255 trackpoint_write(&_psmouse->ps2dev, \
256 _attr->command, _tp->_name); \
258 trackpoint_update_bit(&_psmouse->ps2dev, \
259 _attr->command, _attr->mask, \
264 #define TRACKPOINT_SET_POWER_ON_DEFAULT(_tp, _name) \
266 _tp->_name = trackpoint_attr_##_name.power_on_default; \
269 static int trackpoint_start_protocol(struct psmouse
*psmouse
,
270 u8
*variant_id
, u8
*firmware_id
)
275 error
= ps2_command(&psmouse
->ps2dev
,
276 param
, MAKE_PS2_CMD(0, 2, TP_READ_ID
));
282 case TP_VARIANT_ALPS
:
283 case TP_VARIANT_ELAN
:
285 case TP_VARIANT_JYT_SYNAPTICS
:
286 case TP_VARIANT_SYNAPTICS
:
288 *variant_id
= param
[0];
290 *firmware_id
= param
[1];
298 * Write parameters to trackpad.
299 * in_power_on_state: Set to true if TP is in default / power-on state (ex. if
300 * power-on reset was run). If so, values will only be
301 * written to TP if they differ from power-on default.
303 static int trackpoint_sync(struct psmouse
*psmouse
, bool in_power_on_state
)
305 struct trackpoint_data
*tp
= psmouse
->private;
307 if (!in_power_on_state
&& tp
->variant_id
== TP_VARIANT_IBM
) {
309 * Disable features that may make device unusable
312 trackpoint_update_bit(&psmouse
->ps2dev
, TP_TOGGLE_TWOHAND
,
313 TP_MASK_TWOHAND
, TP_DEF_TWOHAND
);
315 trackpoint_update_bit(&psmouse
->ps2dev
, TP_TOGGLE_SOURCE_TAG
,
316 TP_MASK_SOURCE_TAG
, TP_DEF_SOURCE_TAG
);
318 trackpoint_update_bit(&psmouse
->ps2dev
, TP_TOGGLE_MB
,
319 TP_MASK_MB
, TP_DEF_MB
);
323 * These properties can be changed in this driver. Only
324 * configure them if the values are non-default or if the TP is in
327 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, sensitivity
);
328 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, inertia
);
329 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, speed
);
330 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, reach
);
331 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, draghys
);
332 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, mindrag
);
333 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, thresh
);
334 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, upthresh
);
335 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, ztime
);
336 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, jenks
);
337 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, drift_time
);
340 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, press_to_select
);
341 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, skipback
);
342 TRACKPOINT_UPDATE(in_power_on_state
, psmouse
, tp
, ext_dev
);
347 static void trackpoint_defaults(struct trackpoint_data
*tp
)
349 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, sensitivity
);
350 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, speed
);
351 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, reach
);
352 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, draghys
);
353 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, mindrag
);
354 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, thresh
);
355 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, upthresh
);
356 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, ztime
);
357 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, jenks
);
358 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, drift_time
);
359 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, inertia
);
362 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, press_to_select
);
363 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, skipback
);
364 TRACKPOINT_SET_POWER_ON_DEFAULT(tp
, ext_dev
);
367 static void trackpoint_disconnect(struct psmouse
*psmouse
)
369 device_remove_group(&psmouse
->ps2dev
.serio
->dev
,
370 &trackpoint_attr_group
);
372 kfree(psmouse
->private);
373 psmouse
->private = NULL
;
376 static int trackpoint_reconnect(struct psmouse
*psmouse
)
378 struct trackpoint_data
*tp
= psmouse
->private;
382 error
= trackpoint_start_protocol(psmouse
, NULL
, NULL
);
386 was_reset
= tp
->variant_id
== TP_VARIANT_IBM
&&
387 trackpoint_power_on_reset(&psmouse
->ps2dev
) == 0;
389 error
= trackpoint_sync(psmouse
, was_reset
);
396 int trackpoint_detect(struct psmouse
*psmouse
, bool set_properties
)
398 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
399 struct trackpoint_data
*tp
;
405 error
= trackpoint_start_protocol(psmouse
, &variant_id
, &firmware_id
);
412 tp
= kzalloc(sizeof(*tp
), GFP_KERNEL
);
416 trackpoint_defaults(tp
);
417 tp
->variant_id
= variant_id
;
418 tp
->firmware_id
= firmware_id
;
420 psmouse
->private = tp
;
422 psmouse
->vendor
= trackpoint_variants
[variant_id
];
423 psmouse
->name
= "TrackPoint";
425 psmouse
->reconnect
= trackpoint_reconnect
;
426 psmouse
->disconnect
= trackpoint_disconnect
;
428 if (variant_id
!= TP_VARIANT_IBM
) {
429 /* Newer variants do not support extended button query. */
432 error
= trackpoint_read(ps2dev
, TP_EXT_BTN
, &button_info
);
434 psmouse_warn(psmouse
,
435 "failed to get extended button data, assuming 3 buttons\n");
437 } else if (!button_info
) {
438 psmouse_warn(psmouse
,
439 "got 0 in extended button data, assuming 3 buttons\n");
444 if ((button_info
& 0x0f) >= 3)
445 input_set_capability(psmouse
->dev
, EV_KEY
, BTN_MIDDLE
);
447 __set_bit(INPUT_PROP_POINTER
, psmouse
->dev
->propbit
);
448 __set_bit(INPUT_PROP_POINTING_STICK
, psmouse
->dev
->propbit
);
450 if (variant_id
!= TP_VARIANT_IBM
||
451 trackpoint_power_on_reset(ps2dev
) != 0) {
453 * Write defaults to TP if we did not reset the trackpoint.
455 trackpoint_sync(psmouse
, false);
458 error
= device_add_group(&ps2dev
->serio
->dev
, &trackpoint_attr_group
);
461 "failed to create sysfs attributes, error: %d\n",
463 kfree(psmouse
->private);
464 psmouse
->private = NULL
;
468 psmouse_info(psmouse
,
469 "%s TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
470 psmouse
->vendor
, firmware_id
,
471 (button_info
& 0xf0) >> 4, button_info
& 0x0f);