1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Stephen Evanchik <evanchsa@gmail.com> 4 * 5 * Trademarks are the property of their respective owners. 6 */ 7 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> 16 #include "psmouse.h" 17 #include "trackpoint.h" 18 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", 26 }; 27 28 /* 29 * Power-on Reset: Resets all trackpoint parameters, including RAM values, 30 * to defaults. 31 * Returns zero on success, non-zero on failure. 32 */ 33 static int trackpoint_power_on_reset(struct ps2dev *ps2dev) 34 { 35 u8 param[2] = { TP_POR }; 36 int err; 37 38 err = ps2_command(ps2dev, param, MAKE_PS2_CMD(1, 2, TP_COMMAND)); 39 if (err) 40 return err; 41 42 /* Check for success response -- 0xAA00 */ 43 if (param[0] != 0xAA || param[1] != 0x00) 44 return -ENODEV; 45 46 return 0; 47 } 48 49 /* 50 * Device IO: read, write and toggle bit 51 */ 52 static int trackpoint_read(struct ps2dev *ps2dev, u8 loc, u8 *results) 53 { 54 results[0] = loc; 55 56 return ps2_command(ps2dev, results, MAKE_PS2_CMD(1, 1, TP_COMMAND)); 57 } 58 59 static int trackpoint_write(struct ps2dev *ps2dev, u8 loc, u8 val) 60 { 61 u8 param[3] = { TP_WRITE_MEM, loc, val }; 62 63 return ps2_command(ps2dev, param, MAKE_PS2_CMD(3, 0, TP_COMMAND)); 64 } 65 66 static int trackpoint_toggle_bit(struct ps2dev *ps2dev, u8 loc, u8 mask) 67 { 68 u8 param[3] = { TP_TOGGLE, loc, mask }; 69 70 /* Bad things will happen if the loc param isn't in this range */ 71 if (loc < 0x20 || loc >= 0x2F) 72 return -EINVAL; 73 74 return ps2_command(ps2dev, param, MAKE_PS2_CMD(3, 0, TP_COMMAND)); 75 } 76 77 static int trackpoint_update_bit(struct ps2dev *ps2dev, 78 u8 loc, u8 mask, u8 value) 79 { 80 int retval; 81 u8 data; 82 83 retval = trackpoint_read(ps2dev, loc, &data); 84 if (retval) 85 return retval; 86 87 if (((data & mask) == mask) != !!value) 88 retval = trackpoint_toggle_bit(ps2dev, loc, mask); 89 90 return retval; 91 } 92 93 /* 94 * Trackpoint-specific attributes 95 */ 96 struct trackpoint_attr_data { 97 size_t field_offset; 98 u8 command; 99 u8 mask; 100 bool inverted; 101 u8 power_on_default; 102 }; 103 104 static ssize_t trackpoint_show_int_attr(struct psmouse *psmouse, 105 void *data, char *buf) 106 { 107 struct trackpoint_data *tp = psmouse->private; 108 struct trackpoint_attr_data *attr = data; 109 u8 value = *(u8 *)((void *)tp + attr->field_offset); 110 111 if (attr->inverted) 112 value = !value; 113 114 return sprintf(buf, "%u\n", value); 115 } 116 117 static 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; 122 u8 *field = (void *)tp + attr->field_offset; 123 u8 value; 124 int err; 125 126 err = kstrtou8(buf, 10, &value); 127 if (err) 128 return err; 129 130 *field = value; 131 err = trackpoint_write(&psmouse->ps2dev, attr->command, value); 132 133 return err ?: count; 134 } 135 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, \ 141 }; \ 142 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \ 143 &trackpoint_attr_##_name, \ 144 trackpoint_show_int_attr, trackpoint_set_int_attr) 145 146 static 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; 151 bool *field = (void *)tp + attr->field_offset; 152 bool value; 153 int err; 154 155 err = kstrtobool(buf, &value); 156 if (err) 157 return err; 158 159 if (attr->inverted) 160 value = !value; 161 162 if (*field != value) { 163 *field = value; 164 err = trackpoint_toggle_bit(&psmouse->ps2dev, 165 attr->command, attr->mask); 166 } 167 168 return err ?: count; 169 } 170 171 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, \ 175 _name), \ 176 .command = _command, \ 177 .mask = _mask, \ 178 .inverted = _inv, \ 179 .power_on_default = _default, \ 180 }; \ 181 PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \ 182 &trackpoint_attr_##_name, \ 183 trackpoint_show_int_attr, trackpoint_set_bit_attr) 184 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); 196 197 TRACKPOINT_BIT_ATTR(press_to_select, TP_TOGGLE_PTSON, TP_MASK_PTSON, false, 198 TP_DEF_PTSON); 199 TRACKPOINT_BIT_ATTR(skipback, TP_TOGGLE_SKIPBACK, TP_MASK_SKIPBACK, false, 200 TP_DEF_SKIPBACK); 201 TRACKPOINT_BIT_ATTR(ext_dev, TP_TOGGLE_EXT_DEV, TP_MASK_EXT_DEV, true, 202 TP_DEF_EXT_DEV); 203 204 static 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 214 static umode_t trackpoint_is_attr_visible(struct kobject *kobj, 215 struct attribute *attr, int n) 216 { 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); 220 221 return trackpoint_is_attr_available(psmouse, attr) ? attr->mode : 0; 222 } 223 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, 239 NULL 240 }; 241 242 static struct attribute_group trackpoint_attr_group = { 243 .is_visible = trackpoint_is_attr_visible, 244 .attrs = trackpoint_attrs, 245 }; 246 247 #define TRACKPOINT_UPDATE(_power_on, _psmouse, _tp, _name) \ 248 do { \ 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) \ 265 do { \ 266 _tp->_name = trackpoint_attr_##_name.power_on_default; \ 267 } while (0) 268 269 static int trackpoint_start_protocol(struct psmouse *psmouse, 270 u8 *variant_id, u8 *firmware_id) 271 { 272 u8 param[2] = { 0 }; 273 int error; 274 275 error = ps2_command(&psmouse->ps2dev, 276 param, MAKE_PS2_CMD(0, 2, TP_READ_ID)); 277 if (error) 278 return error; 279 280 switch (param[0]) { 281 case TP_VARIANT_IBM: 282 case TP_VARIANT_ALPS: 283 case TP_VARIANT_ELAN: 284 case TP_VARIANT_NXP: 285 if (variant_id) 286 *variant_id = param[0]; 287 if (firmware_id) 288 *firmware_id = param[1]; 289 return 0; 290 } 291 292 return -ENODEV; 293 } 294 295 /* 296 * Write parameters to trackpad. 297 * in_power_on_state: Set to true if TP is in default / power-on state (ex. if 298 * power-on reset was run). If so, values will only be 299 * written to TP if they differ from power-on default. 300 */ 301 static int trackpoint_sync(struct psmouse *psmouse, bool in_power_on_state) 302 { 303 struct trackpoint_data *tp = psmouse->private; 304 305 if (!in_power_on_state && tp->variant_id == TP_VARIANT_IBM) { 306 /* 307 * Disable features that may make device unusable 308 * with this driver. 309 */ 310 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_TWOHAND, 311 TP_MASK_TWOHAND, TP_DEF_TWOHAND); 312 313 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_SOURCE_TAG, 314 TP_MASK_SOURCE_TAG, TP_DEF_SOURCE_TAG); 315 316 trackpoint_update_bit(&psmouse->ps2dev, TP_TOGGLE_MB, 317 TP_MASK_MB, TP_DEF_MB); 318 } 319 320 /* 321 * These properties can be changed in this driver. Only 322 * configure them if the values are non-default or if the TP is in 323 * an unknown state. 324 */ 325 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, sensitivity); 326 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, inertia); 327 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, speed); 328 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, reach); 329 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, draghys); 330 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, mindrag); 331 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, thresh); 332 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, upthresh); 333 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ztime); 334 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, jenks); 335 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, drift_time); 336 337 /* toggles */ 338 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, press_to_select); 339 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, skipback); 340 TRACKPOINT_UPDATE(in_power_on_state, psmouse, tp, ext_dev); 341 342 return 0; 343 } 344 345 static void trackpoint_defaults(struct trackpoint_data *tp) 346 { 347 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, sensitivity); 348 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, speed); 349 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, reach); 350 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, draghys); 351 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, mindrag); 352 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, thresh); 353 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, upthresh); 354 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ztime); 355 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, jenks); 356 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, drift_time); 357 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, inertia); 358 359 /* toggles */ 360 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, press_to_select); 361 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, skipback); 362 TRACKPOINT_SET_POWER_ON_DEFAULT(tp, ext_dev); 363 } 364 365 static void trackpoint_disconnect(struct psmouse *psmouse) 366 { 367 device_remove_group(&psmouse->ps2dev.serio->dev, 368 &trackpoint_attr_group); 369 370 kfree(psmouse->private); 371 psmouse->private = NULL; 372 } 373 374 static int trackpoint_reconnect(struct psmouse *psmouse) 375 { 376 struct trackpoint_data *tp = psmouse->private; 377 int error; 378 bool was_reset; 379 380 error = trackpoint_start_protocol(psmouse, NULL, NULL); 381 if (error) 382 return error; 383 384 was_reset = tp->variant_id == TP_VARIANT_IBM && 385 trackpoint_power_on_reset(&psmouse->ps2dev) == 0; 386 387 error = trackpoint_sync(psmouse, was_reset); 388 if (error) 389 return error; 390 391 return 0; 392 } 393 394 int trackpoint_detect(struct psmouse *psmouse, bool set_properties) 395 { 396 struct ps2dev *ps2dev = &psmouse->ps2dev; 397 struct trackpoint_data *tp; 398 u8 variant_id; 399 u8 firmware_id; 400 u8 button_info; 401 int error; 402 403 error = trackpoint_start_protocol(psmouse, &variant_id, &firmware_id); 404 if (error) 405 return error; 406 407 if (!set_properties) 408 return 0; 409 410 tp = kzalloc(sizeof(*tp), GFP_KERNEL); 411 if (!tp) 412 return -ENOMEM; 413 414 trackpoint_defaults(tp); 415 tp->variant_id = variant_id; 416 tp->firmware_id = firmware_id; 417 418 psmouse->private = tp; 419 420 psmouse->vendor = trackpoint_variants[variant_id]; 421 psmouse->name = "TrackPoint"; 422 423 psmouse->reconnect = trackpoint_reconnect; 424 psmouse->disconnect = trackpoint_disconnect; 425 426 if (variant_id != TP_VARIANT_IBM) { 427 /* Newer variants do not support extended button query. */ 428 button_info = 0x33; 429 } else { 430 error = trackpoint_read(ps2dev, TP_EXT_BTN, &button_info); 431 if (error) { 432 psmouse_warn(psmouse, 433 "failed to get extended button data, assuming 3 buttons\n"); 434 button_info = 0x33; 435 } else if (!button_info) { 436 psmouse_warn(psmouse, 437 "got 0 in extended button data, assuming 3 buttons\n"); 438 button_info = 0x33; 439 } 440 } 441 442 if ((button_info & 0x0f) >= 3) 443 input_set_capability(psmouse->dev, EV_KEY, BTN_MIDDLE); 444 445 __set_bit(INPUT_PROP_POINTER, psmouse->dev->propbit); 446 __set_bit(INPUT_PROP_POINTING_STICK, psmouse->dev->propbit); 447 448 if (variant_id != TP_VARIANT_IBM || 449 trackpoint_power_on_reset(ps2dev) != 0) { 450 /* 451 * Write defaults to TP if we did not reset the trackpoint. 452 */ 453 trackpoint_sync(psmouse, false); 454 } 455 456 error = device_add_group(&ps2dev->serio->dev, &trackpoint_attr_group); 457 if (error) { 458 psmouse_err(psmouse, 459 "failed to create sysfs attributes, error: %d\n", 460 error); 461 kfree(psmouse->private); 462 psmouse->private = NULL; 463 return -1; 464 } 465 466 psmouse_info(psmouse, 467 "%s TrackPoint firmware: 0x%02x, buttons: %d/%d\n", 468 psmouse->vendor, firmware_id, 469 (button_info & 0xf0) >> 4, button_info & 0x0f); 470 471 return 0; 472 } 473 474