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