blob: 4a86b3e31d3bd3589f536b0861daf46e1ce87a4e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Stephen Evanchik541e3162005-08-08 01:26:18 -05002/*
3 * Stephen Evanchik <evanchsa@gmail.com>
4 *
Stephen Evanchik541e3162005-08-08 01:26:18 -05005 * Trademarks are the property of their respective owners.
6 */
7
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Stephen Evanchik541e3162005-08-08 01:26:18 -05009#include <linux/delay.h>
10#include <linux/serio.h>
11#include <linux/module.h>
Stephen Evanchik541e3162005-08-08 01:26:18 -050012#include <linux/input.h>
13#include <linux/libps2.h>
14#include <linux/proc_fs.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080015#include <linux/uaccess.h>
Stephen Evanchik541e3162005-08-08 01:26:18 -050016#include "psmouse.h"
17#include "trackpoint.h"
18
Dmitry Torokhov2a924d72018-01-05 13:28:47 -080019static const char * const trackpoint_variants[] = {
Vincent Huang6c775452020-09-14 12:19:08 -070020 [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",
Dmitry Torokhov2a924d72018-01-05 13:28:47 -080026};
27
Stephen Evanchik541e3162005-08-08 01:26:18 -050028/*
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -070029 * Power-on Reset: Resets all trackpoint parameters, including RAM values,
30 * to defaults.
31 * Returns zero on success, non-zero on failure.
32 */
33static int trackpoint_power_on_reset(struct ps2dev *ps2dev)
34{
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080035 u8 param[2] = { TP_POR };
36 int err;
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -070037
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080038 err = ps2_command(ps2dev, param, MAKE_PS2_CMD(1, 2, TP_COMMAND));
39 if (err)
40 return err;
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -070041
42 /* Check for success response -- 0xAA00 */
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080043 if (param[0] != 0xAA || param[1] != 0x00)
Dmitry Torokhov2a924d72018-01-05 13:28:47 -080044 return -ENODEV;
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -070045
46 return 0;
47}
48
49/*
Stephen Evanchik541e3162005-08-08 01:26:18 -050050 * Device IO: read, write and toggle bit
51 */
Dmitry Torokhov2a924d72018-01-05 13:28:47 -080052static int trackpoint_read(struct ps2dev *ps2dev, u8 loc, u8 *results)
Stephen Evanchik541e3162005-08-08 01:26:18 -050053{
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080054 results[0] = loc;
Stephen Evanchik541e3162005-08-08 01:26:18 -050055
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080056 return ps2_command(ps2dev, results, MAKE_PS2_CMD(1, 1, TP_COMMAND));
Stephen Evanchik541e3162005-08-08 01:26:18 -050057}
58
Dmitry Torokhov2a924d72018-01-05 13:28:47 -080059static int trackpoint_write(struct ps2dev *ps2dev, u8 loc, u8 val)
Stephen Evanchik541e3162005-08-08 01:26:18 -050060{
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080061 u8 param[3] = { TP_WRITE_MEM, loc, val };
Stephen Evanchik541e3162005-08-08 01:26:18 -050062
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080063 return ps2_command(ps2dev, param, MAKE_PS2_CMD(3, 0, TP_COMMAND));
Stephen Evanchik541e3162005-08-08 01:26:18 -050064}
65
Dmitry Torokhov2a924d72018-01-05 13:28:47 -080066static int trackpoint_toggle_bit(struct ps2dev *ps2dev, u8 loc, u8 mask)
Stephen Evanchik541e3162005-08-08 01:26:18 -050067{
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080068 u8 param[3] = { TP_TOGGLE, loc, mask };
69
Stephen Evanchik541e3162005-08-08 01:26:18 -050070 /* Bad things will happen if the loc param isn't in this range */
71 if (loc < 0x20 || loc >= 0x2F)
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080072 return -EINVAL;
Stephen Evanchik541e3162005-08-08 01:26:18 -050073
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080074 return ps2_command(ps2dev, param, MAKE_PS2_CMD(3, 0, TP_COMMAND));
Stephen Evanchik541e3162005-08-08 01:26:18 -050075}
76
Dmitry Torokhov2a924d72018-01-05 13:28:47 -080077static int trackpoint_update_bit(struct ps2dev *ps2dev,
78 u8 loc, u8 mask, u8 value)
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -070079{
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080080 int retval;
Dmitry Torokhov2a924d72018-01-05 13:28:47 -080081 u8 data;
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -070082
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -080083 retval = trackpoint_read(ps2dev, loc, &data);
84 if (retval)
85 return retval;
86
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -070087 if (((data & mask) == mask) != !!value)
88 retval = trackpoint_toggle_bit(ps2dev, loc, mask);
89
90 return retval;
91}
Stephen Evanchik541e3162005-08-08 01:26:18 -050092
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050093/*
94 * Trackpoint-specific attributes
95 */
96struct trackpoint_attr_data {
97 size_t field_offset;
Dmitry Torokhov2a924d72018-01-05 13:28:47 -080098 u8 command;
99 u8 mask;
100 bool inverted;
101 u8 power_on_default;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500102};
Stephen Evanchik541e3162005-08-08 01:26:18 -0500103
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800104static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse,
105 void *data, char *buf)
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500106{
107 struct trackpoint_data *tp = psmouse->private;
108 struct trackpoint_attr_data *attr = data;
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800109 u8 value = *(u8 *)((void *)tp + attr->field_offset);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500110
Dmitry Torokhovb8044c72006-02-15 00:49:09 -0500111 if (attr->inverted)
112 value = !value;
113
114 return sprintf(buf, "%u\n", value);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500115}
Stephen Evanchik541e3162005-08-08 01:26:18 -0500116
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500117static ssize_t trackpoint_set_int_attr(struct psmouse *psmouse, void *data,
118 const char *buf, size_t count)
119{
120 struct trackpoint_data *tp = psmouse->private;
121 struct trackpoint_attr_data *attr = data;
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800122 u8 *field = (void *)tp + attr->field_offset;
123 u8 value;
JJ Ding76496e72011-11-09 10:20:14 -0800124 int err;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500125
JJ Ding76496e72011-11-09 10:20:14 -0800126 err = kstrtou8(buf, 10, &value);
127 if (err)
128 return err;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500129
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500130 *field = value;
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -0800131 err = trackpoint_write(&psmouse->ps2dev, attr->command, value);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500132
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -0800133 return err ?: count;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500134}
Stephen Evanchik541e3162005-08-08 01:26:18 -0500135
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700136#define TRACKPOINT_INT_ATTR(_name, _command, _default) \
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500137 static struct trackpoint_attr_data trackpoint_attr_##_name = { \
138 .field_offset = offsetof(struct trackpoint_data, _name), \
139 .command = _command, \
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700140 .power_on_default = _default, \
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500141 }; \
142 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
143 &trackpoint_attr_##_name, \
144 trackpoint_show_int_attr, trackpoint_set_int_attr)
Stephen Evanchik541e3162005-08-08 01:26:18 -0500145
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500146static ssize_t trackpoint_set_bit_attr(struct psmouse *psmouse, void *data,
147 const char *buf, size_t count)
148{
149 struct trackpoint_data *tp = psmouse->private;
150 struct trackpoint_attr_data *attr = data;
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800151 bool *field = (void *)tp + attr->field_offset;
152 bool value;
JJ Ding76496e72011-11-09 10:20:14 -0800153 int err;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500154
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800155 err = kstrtobool(buf, &value);
JJ Ding76496e72011-11-09 10:20:14 -0800156 if (err)
157 return err;
158
Dmitry Torokhovb8044c72006-02-15 00:49:09 -0500159 if (attr->inverted)
160 value = !value;
161
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500162 if (*field != value) {
163 *field = value;
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -0800164 err = trackpoint_toggle_bit(&psmouse->ps2dev,
165 attr->command, attr->mask);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500166 }
Stephen Evanchik541e3162005-08-08 01:26:18 -0500167
Dmitry Torokhov3aceaa32018-01-04 22:52:55 -0800168 return err ?: count;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500169}
170
171
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700172#define TRACKPOINT_BIT_ATTR(_name, _command, _mask, _inv, _default) \
173static struct trackpoint_attr_data trackpoint_attr_##_name = { \
174 .field_offset = offsetof(struct trackpoint_data, \
175 _name), \
176 .command = _command, \
177 .mask = _mask, \
178 .inverted = _inv, \
179 .power_on_default = _default, \
180 }; \
181PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
182 &trackpoint_attr_##_name, \
183 trackpoint_show_int_attr, trackpoint_set_bit_attr)
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500184
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700185TRACKPOINT_INT_ATTR(sensitivity, TP_SENS, TP_DEF_SENS);
186TRACKPOINT_INT_ATTR(speed, TP_SPEED, TP_DEF_SPEED);
187TRACKPOINT_INT_ATTR(inertia, TP_INERTIA, TP_DEF_INERTIA);
188TRACKPOINT_INT_ATTR(reach, TP_REACH, TP_DEF_REACH);
189TRACKPOINT_INT_ATTR(draghys, TP_DRAGHYS, TP_DEF_DRAGHYS);
190TRACKPOINT_INT_ATTR(mindrag, TP_MINDRAG, TP_DEF_MINDRAG);
191TRACKPOINT_INT_ATTR(thresh, TP_THRESH, TP_DEF_THRESH);
192TRACKPOINT_INT_ATTR(upthresh, TP_UP_THRESH, TP_DEF_UP_THRESH);
193TRACKPOINT_INT_ATTR(ztime, TP_Z_TIME, TP_DEF_Z_TIME);
194TRACKPOINT_INT_ATTR(jenks, TP_JENKS_CURV, TP_DEF_JENKS_CURV);
haarp2ba35322014-12-17 15:22:08 -0800195TRACKPOINT_INT_ATTR(drift_time, TP_DRIFT_TIME, TP_DEF_DRIFT_TIME);
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700196
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800197TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, false,
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700198 TP_DEF_PTSON);
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800199TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, false,
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700200 TP_DEF_SKIPBACK);
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800201TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, true,
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700202 TP_DEF_EXT_DEV);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500203
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800204static bool trackpoint_is_attr_available(struct psmouse *psmouse,
205 struct attribute *attr)
206{
207 struct trackpoint_data *tp = psmouse->private;
208
209 return tp->variant_id == TP_VARIANT_IBM ||
210 attr == &psmouse_attr_sensitivity.dattr.attr ||
211 attr == &psmouse_attr_press_to_select.dattr.attr;
212}
213
214static umode_t trackpoint_is_attr_visible(struct kobject *kobj,
215 struct attribute *attr, int n)
216{
YueHaibingeacacdb2021-06-19 22:13:29 -0700217 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800218 struct serio *serio = to_serio_port(dev);
219 struct psmouse *psmouse = serio_get_drvdata(serio);
220
221 return trackpoint_is_attr_available(psmouse, attr) ? attr->mode : 0;
222}
223
Stephen Evanchik541e3162005-08-08 01:26:18 -0500224static struct attribute *trackpoint_attrs[] = {
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500225 &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,
haarp2ba35322014-12-17 15:22:08 -0800235 &psmouse_attr_drift_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500236 &psmouse_attr_press_to_select.dattr.attr,
237 &psmouse_attr_skipback.dattr.attr,
238 &psmouse_attr_ext_dev.dattr.attr,
Stephen Evanchik541e3162005-08-08 01:26:18 -0500239 NULL
240};
241
242static struct attribute_group trackpoint_attr_group = {
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800243 .is_visible = trackpoint_is_attr_visible,
244 .attrs = trackpoint_attrs,
Stephen Evanchik541e3162005-08-08 01:26:18 -0500245};
246
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800247#define TRACKPOINT_UPDATE(_power_on, _psmouse, _tp, _name) \
248do { \
249 struct trackpoint_attr_data *_attr = &trackpoint_attr_##_name; \
250 \
251 if ((!_power_on || _tp->_name != _attr->power_on_default) && \
252 trackpoint_is_attr_available(_psmouse, \
253 &psmouse_attr_##_name.dattr.attr)) { \
254 if (!_attr->mask) \
255 trackpoint_write(&_psmouse->ps2dev, \
256 _attr->command, _tp->_name); \
257 else \
258 trackpoint_update_bit(&_psmouse->ps2dev, \
259 _attr->command, _attr->mask, \
260 _tp->_name); \
261 } \
262} while (0)
263
264#define TRACKPOINT_SET_POWER_ON_DEFAULT(_tp, _name) \
265do { \
266 _tp->_name = trackpoint_attr_##_name.power_on_default; \
267} while (0)
268
269static int trackpoint_start_protocol(struct psmouse *psmouse,
270 u8 *variant_id, u8 *firmware_id)
Stephen Evanchik541e3162005-08-08 01:26:18 -0500271{
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800272 u8 param[2] = { 0 };
273 int error;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500274
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800275 error = ps2_command(&psmouse->ps2dev,
276 param, MAKE_PS2_CMD(0, 2, TP_READ_ID));
277 if (error)
278 return error;
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400279
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800280 switch (param[0]) {
281 case TP_VARIANT_IBM:
282 case TP_VARIANT_ALPS:
283 case TP_VARIANT_ELAN:
284 case TP_VARIANT_NXP:
Vincent Huang996d5852020-09-28 16:19:05 -0700285 case TP_VARIANT_JYT_SYNAPTICS:
286 case TP_VARIANT_SYNAPTICS:
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800287 if (variant_id)
288 *variant_id = param[0];
289 if (firmware_id)
290 *firmware_id = param[1];
291 return 0;
292 }
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400293
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800294 return -ENODEV;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500295}
296
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700297/*
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.
302 */
303static int trackpoint_sync(struct psmouse *psmouse, bool in_power_on_state)
Stephen Evanchik541e3162005-08-08 01:26:18 -0500304{
Stephen Evanchik541e3162005-08-08 01:26:18 -0500305 struct trackpoint_data *tp = psmouse->private;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500306
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800307 if (!in_power_on_state && tp->variant_id == TP_VARIANT_IBM) {
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700308 /*
309 * Disable features that may make device unusable
310 * with this driver.
311 */
312 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND,
313 TP_MASK_TWOHAND, TP_DEF_TWOHAND);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500314
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700315 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG,
316 TP_MASK_SOURCE_TAG, TP_DEF_SOURCE_TAG);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500317
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700318 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_MB,
319 TP_MASK_MB, TP_DEF_MB);
320 }
Stephen Evanchik541e3162005-08-08 01:26:18 -0500321
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700322 /*
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
325 * an unknown state.
326 */
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);
haarp2ba35322014-12-17 15:22:08 -0800337 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, drift_time);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500338
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700339 /* toggles */
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);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500343
344 return 0;
345}
346
347static void trackpoint_defaults(struct trackpoint_data *tp)
348{
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700349 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);
haarp2ba35322014-12-17 15:22:08 -0800358 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, drift_time);
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700359 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, inertia);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500360
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700361 /* toggles */
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);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500365}
366
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400367static void trackpoint_disconnect(struct psmouse *psmouse)
368{
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800369 device_remove_group(&psmouse->ps2dev.serio->dev,
370 &trackpoint_attr_group);
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400371
372 kfree(psmouse->private);
373 psmouse->private = NULL;
374}
375
376static int trackpoint_reconnect(struct psmouse *psmouse)
377{
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800378 struct trackpoint_data *tp = psmouse->private;
379 int error;
380 bool was_reset;
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700381
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800382 error = trackpoint_start_protocol(psmouse, NULL, NULL);
383 if (error)
384 return error;
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400385
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800386 was_reset = tp->variant_id == TP_VARIANT_IBM &&
387 trackpoint_power_on_reset(&psmouse->ps2dev) == 0;
388
389 error = trackpoint_sync(psmouse, was_reset);
390 if (error)
391 return error;
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400392
393 return 0;
394}
395
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700396int trackpoint_detect(struct psmouse *psmouse, bool set_properties)
Stephen Evanchik541e3162005-08-08 01:26:18 -0500397{
Stephen Evanchik541e3162005-08-08 01:26:18 -0500398 struct ps2dev *ps2dev = &psmouse->ps2dev;
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800399 struct trackpoint_data *tp;
400 u8 variant_id;
401 u8 firmware_id;
402 u8 button_info;
Jeff Garzik8ff22ea2006-11-02 23:59:30 -0500403 int error;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500404
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800405 error = trackpoint_start_protocol(psmouse, &variant_id, &firmware_id);
406 if (error)
407 return error;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500408
409 if (!set_properties)
410 return 0;
411
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800412 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
413 if (!tp)
Davidlohr Bueso6792cbb2010-09-29 18:53:35 -0700414 return -ENOMEM;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500415
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800416 trackpoint_defaults(tp);
417 tp->variant_id = variant_id;
418 tp->firmware_id = firmware_id;
419
420 psmouse->private = tp;
421
422 psmouse->vendor = trackpoint_variants[variant_id];
Stephen Evanchik541e3162005-08-08 01:26:18 -0500423 psmouse->name = "TrackPoint";
424
Dmitry Torokhov184dd272006-08-04 22:52:26 -0400425 psmouse->reconnect = trackpoint_reconnect;
Stephen Evanchik541e3162005-08-08 01:26:18 -0500426 psmouse->disconnect = trackpoint_disconnect;
427
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800428 if (variant_id != TP_VARIANT_IBM) {
429 /* Newer variants do not support extended button query. */
430 button_info = 0x33;
431 } else {
432 error = trackpoint_read(ps2dev, TP_EXT_BTN, &button_info);
433 if (error) {
434 psmouse_warn(psmouse,
435 "failed to get extended button data, assuming 3 buttons\n");
436 button_info = 0x33;
437 } else if (!button_info) {
438 psmouse_warn(psmouse,
439 "got 0 in extended button data, assuming 3 buttons\n");
440 button_info = 0x33;
441 }
442 }
443
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800444 if ((button_info & 0x0f) >= 3)
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800445 input_set_capability(psmouse->dev, EV_KEY, BTN_MIDDLE);
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800446
Hans de Goede01d4cd52014-09-08 14:44:05 -0700447 __set_bit(INPUT_PROP_POINTER, psmouse->dev->propbit);
Hans de Goede76113922014-09-08 14:42:12 -0700448 __set_bit(INPUT_PROP_POINTING_STICK, psmouse->dev->propbit);
449
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800450 if (variant_id != TP_VARIANT_IBM ||
451 trackpoint_power_on_reset(ps2dev) != 0) {
452 /*
453 * Write defaults to TP if we did not reset the trackpoint.
454 */
Shawn Nematbakhsh0c6a6162013-04-15 13:49:34 -0700455 trackpoint_sync(psmouse, false);
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800456 }
Stephen Evanchik541e3162005-08-08 01:26:18 -0500457
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800458 error = device_add_group(&ps2dev->serio->dev, &trackpoint_attr_group);
Jeff Garzik8ff22ea2006-11-02 23:59:30 -0500459 if (error) {
JJ Dingfb163952012-04-10 00:25:01 -0700460 psmouse_err(psmouse,
461 "failed to create sysfs attributes, error: %d\n",
462 error);
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800463 kfree(psmouse->private);
464 psmouse->private = NULL;
Jeff Garzik8ff22ea2006-11-02 23:59:30 -0500465 return -1;
466 }
Stephen Evanchik541e3162005-08-08 01:26:18 -0500467
JJ Dingfb163952012-04-10 00:25:01 -0700468 psmouse_info(psmouse,
Dmitry Torokhov2a924d72018-01-05 13:28:47 -0800469 "%s TrackPoint firmware: 0x%02x, buttons: %d/%d\n",
470 psmouse->vendor, firmware_id,
JJ Dingfb163952012-04-10 00:25:01 -0700471 (button_info & 0xf0) >> 4, button_info & 0x0f);
Stephen Evanchik541e3162005-08-08 01:26:18 -0500472
473 return 0;
474}
475