blob: d68aba9c5dd62f008f938783838e433b30c87e6b [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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
22static 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 */
36static 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 */
58static 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
68static 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
80static 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
96static 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 */
112struct trackpoint_attr_data {
113 size_t field_offset;
114 u8 command;
115 u8 mask;
116 bool inverted;
117 u8 power_on_default;
118};
119
120static 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
133static 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
162static 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) \
188static 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 }; \
196PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
197 &trackpoint_attr_##_name, \
198 trackpoint_show_int_attr, trackpoint_set_bit_attr)
199
200TRACKPOINT_INT_ATTR(sensitivity, TP_SENS, TP_DEF_SENS);
201TRACKPOINT_INT_ATTR(speed, TP_SPEED, TP_DEF_SPEED);
202TRACKPOINT_INT_ATTR(inertia, TP_INERTIA, TP_DEF_INERTIA);
203TRACKPOINT_INT_ATTR(reach, TP_REACH, TP_DEF_REACH);
204TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS, TP_DEF_DRAGHYS);
205TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG, TP_DEF_MINDRAG);
206TRACKPOINT_INT_ATTR(thresh, TP_THRESH, TP_DEF_THRESH);
207TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH, TP_DEF_UP_THRESH);
208TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME, TP_DEF_Z_TIME);
209TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV, TP_DEF_JENKS_CURV);
210TRACKPOINT_INT_ATTR(drift_time, TP_DRIFT_TIME, TP_DEF_DRIFT_TIME);
211
212TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, false,
213 TP_DEF_PTSON);
214TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, false,
215 TP_DEF_SKIPBACK);
216TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, true,
217 TP_DEF_EXT_DEV);
218
219static 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
229static 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
239static 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
257static 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) \
263do { \
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) \
280do { \
281 _tp->_name = trackpoint_attr_##_name.power_on_default; \
282} while (0)
283
284static 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 if (variant_id)
301 *variant_id = param[0];
302 if (firmware_id)
303 *firmware_id = param[1];
304 return 0;
305 }
306
307 return -ENODEV;
308}
309
310/*
311 * Write parameters to trackpad.
312 * in_power_on_state: Set to true if TP is in default / power-on state (ex. if
313 * power-on reset was run). If so, values will only be
314 * written to TP if they differ from power-on default.
315 */
316static int trackpoint_sync(struct psmouse *psmouse, bool in_power_on_state)
317{
318 struct trackpoint_data *tp = psmouse->private;
319
320 if (!in_power_on_state && tp->variant_id == TP_VARIANT_IBM) {
321 /*
322 * Disable features that may make device unusable
323 * with this driver.
324 */
325 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND,
326 TP_MASK_TWOHAND, TP_DEF_TWOHAND);
327
328 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG,
329 TP_MASK_SOURCE_TAG, TP_DEF_SOURCE_TAG);
330
331 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_MB,
332 TP_MASK_MB, TP_DEF_MB);
333 }
334
335 /*
336 * These properties can be changed in this driver. Only
337 * configure them if the values are non-default or if the TP is in
338 * an unknown state.
339 */
340 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, sensitivity);
341 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, inertia);
342 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, speed);
343 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, reach);
344 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, draghys);
345 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, mindrag);
346 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, thresh);
347 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, upthresh);
348 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ztime);
349 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, jenks);
350 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, drift_time);
351
352 /* toggles */
353 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, press_to_select);
354 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, skipback);
355 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ext_dev);
356
357 return 0;
358}
359
360static void trackpoint_defaults(struct trackpoint_data *tp)
361{
362 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, sensitivity);
363 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, speed);
364 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, reach);
365 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, draghys);
366 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, mindrag);
367 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, thresh);
368 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, upthresh);
369 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ztime);
370 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, jenks);
371 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, drift_time);
372 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, inertia);
373
374 /* toggles */
375 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, press_to_select);
376 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, skipback);
377 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ext_dev);
378}
379
380static void trackpoint_disconnect(struct psmouse *psmouse)
381{
382 device_remove_group(&psmouse->ps2dev.serio->dev,
383 &trackpoint_attr_group);
384
385 kfree(psmouse->private);
386 psmouse->private = NULL;
387}
388
389static int trackpoint_reconnect(struct psmouse *psmouse)
390{
391 struct trackpoint_data *tp = psmouse->private;
392 int error;
393 bool was_reset;
394
395 error = trackpoint_start_protocol(psmouse, NULL, NULL);
396 if (error)
397 return error;
398
399 was_reset = tp->variant_id == TP_VARIANT_IBM &&
400 trackpoint_power_on_reset(&psmouse->ps2dev) == 0;
401
402 error = trackpoint_sync(psmouse, was_reset);
403 if (error)
404 return error;
405
406 return 0;
407}
408
409int trackpoint_detect(struct psmouse *psmouse, bool set_properties)
410{
411 struct ps2dev *ps2dev = &psmouse->ps2dev;
412 struct trackpoint_data *tp;
413 u8 variant_id;
414 u8 firmware_id;
415 u8 button_info;
416 int error;
417
418 error = trackpoint_start_protocol(psmouse, &variant_id, &firmware_id);
419 if (error)
420 return error;
421
422 if (!set_properties)
423 return 0;
424
425 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
426 if (!tp)
427 return -ENOMEM;
428
429 trackpoint_defaults(tp);
430 tp->variant_id = variant_id;
431 tp->firmware_id = firmware_id;
432
433 psmouse->private = tp;
434
435 psmouse->vendor = trackpoint_variants[variant_id];
436 psmouse->name = "TrackPoint";
437
438 psmouse->reconnect = trackpoint_reconnect;
439 psmouse->disconnect = trackpoint_disconnect;
440
441 if (variant_id != TP_VARIANT_IBM) {
442 /* Newer variants do not support extended button query. */
443 button_info = 0x33;
444 } else {
445 error = trackpoint_read(ps2dev, TP_EXT_BTN, &button_info);
446 if (error) {
447 psmouse_warn(psmouse,
448 "failed to get extended button data, assuming 3 buttons\n");
449 button_info = 0x33;
450 } else if (!button_info) {
451 psmouse_warn(psmouse,
452 "got 0 in extended button data, assuming 3 buttons\n");
453 button_info = 0x33;
454 }
455 }
456
457 if ((button_info & 0x0f) >= 3)
458 input_set_capability(psmouse->dev, EV_KEY, BTN_MIDDLE);
459
460 __set_bit(INPUT_PROP_POINTER, psmouse->dev->propbit);
461 __set_bit(INPUT_PROP_POINTING_STICK, psmouse->dev->propbit);
462
463 if (variant_id != TP_VARIANT_IBM ||
464 trackpoint_power_on_reset(ps2dev) != 0) {
465 /*
466 * Write defaults to TP if we did not reset the trackpoint.
467 */
468 trackpoint_sync(psmouse, false);
469 }
470
471 error = device_add_group(&ps2dev->serio->dev, &trackpoint_attr_group);
472 if (error) {
473 psmouse_err(psmouse,
474 "failed to create sysfs attributes, error: %d\n",
475 error);
476 kfree(psmouse->private);
477 psmouse->private = NULL;
478 return -1;
479 }
480
481 psmouse_info(psmouse,
482 "%s TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
483 psmouse->vendor, firmware_id,
484 (button_info & 0xf0) >> 4, button_info & 0x0f);
485
486 return 0;
487}
488