Linux Audio

Check our new training course

Linux kernel drivers training

May 6-19, 2025
Register
Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * OLPC HGPK (XO-1) touchpad PS/2 mouse driver
   4 *
   5 * Copyright (c) 2006-2008 One Laptop Per Child
   6 * Authors:
   7 *   Zephaniah E. Hull
   8 *   Andres Salomon <dilinger@debian.org>
   9 *
  10 * This driver is partly based on the ALPS driver, which is:
  11 *
  12 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
  13 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
  14 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
  15 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
 
 
 
 
  16 */
  17
  18/*
  19 * The spec from ALPS is available from
  20 * <http://wiki.laptop.org/go/Touch_Pad/Tablet>.  It refers to this
  21 * device as HGPK (Hybrid GS, PT, and Keymatrix).
  22 *
  23 * The earliest versions of the device had simultaneous reporting; that
  24 * was removed.  After that, the device used the Advanced Mode GS/PT streaming
  25 * stuff.  That turned out to be too buggy to support, so we've finally
  26 * switched to Mouse Mode (which utilizes only the center 1/3 of the touchpad).
  27 */
  28
  29#define DEBUG
  30#include <linux/slab.h>
  31#include <linux/input.h>
  32#include <linux/module.h>
  33#include <linux/serio.h>
  34#include <linux/libps2.h>
  35#include <linux/delay.h>
  36#include <asm/olpc.h>
  37
  38#include "psmouse.h"
  39#include "hgpk.h"
  40
  41#define ILLEGAL_XY 999999
  42
  43static bool tpdebug;
  44module_param(tpdebug, bool, 0644);
  45MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG.");
  46
  47static int recalib_delta = 100;
  48module_param(recalib_delta, int, 0644);
  49MODULE_PARM_DESC(recalib_delta,
  50	"packets containing a delta this large will be discarded, and a "
  51	"recalibration may be scheduled.");
  52
  53static int jumpy_delay = 20;
  54module_param(jumpy_delay, int, 0644);
  55MODULE_PARM_DESC(jumpy_delay,
  56	"delay (ms) before recal after jumpiness detected");
  57
  58static int spew_delay = 1;
  59module_param(spew_delay, int, 0644);
  60MODULE_PARM_DESC(spew_delay,
  61	"delay (ms) before recal after packet spew detected");
  62
  63static int recal_guard_time;
  64module_param(recal_guard_time, int, 0644);
  65MODULE_PARM_DESC(recal_guard_time,
  66	"interval (ms) during which recal will be restarted if packet received");
  67
  68static int post_interrupt_delay = 40;
  69module_param(post_interrupt_delay, int, 0644);
  70MODULE_PARM_DESC(post_interrupt_delay,
  71	"delay (ms) before recal after recal interrupt detected");
  72
  73static bool autorecal = true;
  74module_param(autorecal, bool, 0644);
  75MODULE_PARM_DESC(autorecal, "enable recalibration in the driver");
  76
  77static char hgpk_mode_name[16];
  78module_param_string(hgpk_mode, hgpk_mode_name, sizeof(hgpk_mode_name), 0644);
  79MODULE_PARM_DESC(hgpk_mode,
  80	"default hgpk mode: mouse, glidesensor or pentablet");
  81
  82static int hgpk_default_mode = HGPK_MODE_MOUSE;
  83
  84static const char * const hgpk_mode_names[] = {
  85	[HGPK_MODE_MOUSE] = "Mouse",
  86	[HGPK_MODE_GLIDESENSOR] = "GlideSensor",
  87	[HGPK_MODE_PENTABLET] = "PenTablet",
  88};
  89
  90static int hgpk_mode_from_name(const char *buf, int len)
  91{
  92	int i;
  93
  94	for (i = 0; i < ARRAY_SIZE(hgpk_mode_names); i++) {
  95		const char *name = hgpk_mode_names[i];
  96		if (strlen(name) == len && !strncasecmp(name, buf, len))
  97			return i;
  98	}
  99
 100	return HGPK_MODE_INVALID;
 101}
 102
 103/*
 104 * see if new value is within 20% of half of old value
 105 */
 106static int approx_half(int curr, int prev)
 107{
 108	int belowhalf, abovehalf;
 109
 110	if (curr < 5 || prev < 5)
 111		return 0;
 112
 113	belowhalf = (prev * 8) / 20;
 114	abovehalf = (prev * 12) / 20;
 115
 116	return belowhalf < curr && curr <= abovehalf;
 117}
 118
 119/*
 120 * Throw out oddly large delta packets, and any that immediately follow whose
 121 * values are each approximately half of the previous.  It seems that the ALPS
 122 * firmware emits errant packets, and they get averaged out slowly.
 123 */
 124static int hgpk_discard_decay_hack(struct psmouse *psmouse, int x, int y)
 125{
 126	struct hgpk_data *priv = psmouse->private;
 127	int avx, avy;
 128	bool do_recal = false;
 129
 130	avx = abs(x);
 131	avy = abs(y);
 132
 133	/* discard if too big, or half that but > 4 times the prev delta */
 134	if (avx > recalib_delta ||
 135		(avx > recalib_delta / 2 && ((avx / 4) > priv->xlast))) {
 136		psmouse_warn(psmouse, "detected %dpx jump in x\n", x);
 137		priv->xbigj = avx;
 138	} else if (approx_half(avx, priv->xbigj)) {
 139		psmouse_warn(psmouse, "detected secondary %dpx jump in x\n", x);
 140		priv->xbigj = avx;
 141		priv->xsaw_secondary++;
 142	} else {
 143		if (priv->xbigj && priv->xsaw_secondary > 1)
 144			do_recal = true;
 145		priv->xbigj = 0;
 146		priv->xsaw_secondary = 0;
 147	}
 148
 149	if (avy > recalib_delta ||
 150		(avy > recalib_delta / 2 && ((avy / 4) > priv->ylast))) {
 151		psmouse_warn(psmouse, "detected %dpx jump in y\n", y);
 152		priv->ybigj = avy;
 153	} else if (approx_half(avy, priv->ybigj)) {
 154		psmouse_warn(psmouse, "detected secondary %dpx jump in y\n", y);
 155		priv->ybigj = avy;
 156		priv->ysaw_secondary++;
 157	} else {
 158		if (priv->ybigj && priv->ysaw_secondary > 1)
 159			do_recal = true;
 160		priv->ybigj = 0;
 161		priv->ysaw_secondary = 0;
 162	}
 163
 164	priv->xlast = avx;
 165	priv->ylast = avy;
 166
 167	if (do_recal && jumpy_delay) {
 168		psmouse_warn(psmouse, "scheduling recalibration\n");
 169		psmouse_queue_work(psmouse, &priv->recalib_wq,
 170				msecs_to_jiffies(jumpy_delay));
 171	}
 172
 173	return priv->xbigj || priv->ybigj;
 174}
 175
 176static void hgpk_reset_spew_detection(struct hgpk_data *priv)
 177{
 178	priv->spew_count = 0;
 179	priv->dupe_count = 0;
 180	priv->x_tally = 0;
 181	priv->y_tally = 0;
 182	priv->spew_flag = NO_SPEW;
 183}
 184
 185static void hgpk_reset_hack_state(struct psmouse *psmouse)
 186{
 187	struct hgpk_data *priv = psmouse->private;
 188
 189	priv->abs_x = priv->abs_y = -1;
 190	priv->xlast = priv->ylast = ILLEGAL_XY;
 191	priv->xbigj = priv->ybigj = 0;
 192	priv->xsaw_secondary = priv->ysaw_secondary = 0;
 193	hgpk_reset_spew_detection(priv);
 194}
 195
 196/*
 197 * We have no idea why this particular hardware bug occurs.  The touchpad
 198 * will randomly start spewing packets without anything touching the
 199 * pad.  This wouldn't necessarily be bad, but it's indicative of a
 200 * severely miscalibrated pad; attempting to use the touchpad while it's
 201 * spewing means the cursor will jump all over the place, and act "drunk".
 202 *
 203 * The packets that are spewed tend to all have deltas between -2 and 2, and
 204 * the cursor will move around without really going very far.  It will
 205 * tend to end up in the same location; if we tally up the changes over
 206 * 100 packets, we end up w/ a final delta of close to 0.  This happens
 207 * pretty regularly when the touchpad is spewing, and is pretty hard to
 208 * manually trigger (at least for *my* fingers).  So, it makes a perfect
 209 * scheme for detecting spews.
 210 */
 211static void hgpk_spewing_hack(struct psmouse *psmouse,
 212			      int l, int r, int x, int y)
 213{
 214	struct hgpk_data *priv = psmouse->private;
 215
 216	/* ignore button press packets; many in a row could trigger
 217	 * a false-positive! */
 218	if (l || r)
 219		return;
 220
 221	/* don't track spew if the workaround feature has been turned off */
 222	if (!spew_delay)
 223		return;
 224
 225	if (abs(x) > 3 || abs(y) > 3) {
 226		/* no spew, or spew ended */
 227		hgpk_reset_spew_detection(priv);
 228		return;
 229	}
 230
 231	/* Keep a tally of the overall delta to the cursor position caused by
 232	 * the spew */
 233	priv->x_tally += x;
 234	priv->y_tally += y;
 235
 236	switch (priv->spew_flag) {
 237	case NO_SPEW:
 238		/* we're not spewing, but this packet might be the start */
 239		priv->spew_flag = MAYBE_SPEWING;
 240
 241		fallthrough;
 242
 243	case MAYBE_SPEWING:
 244		priv->spew_count++;
 245
 246		if (priv->spew_count < SPEW_WATCH_COUNT)
 247			break;
 248
 249		/* excessive spew detected, request recalibration */
 250		priv->spew_flag = SPEW_DETECTED;
 251
 252		fallthrough;
 253
 254	case SPEW_DETECTED:
 255		/* only recalibrate when the overall delta to the cursor
 256		 * is really small. if the spew is causing significant cursor
 257		 * movement, it is probably a case of the user moving the
 258		 * cursor very slowly across the screen. */
 259		if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) {
 260			psmouse_warn(psmouse, "packet spew detected (%d,%d)\n",
 261				     priv->x_tally, priv->y_tally);
 262			priv->spew_flag = RECALIBRATING;
 263			psmouse_queue_work(psmouse, &priv->recalib_wq,
 264					   msecs_to_jiffies(spew_delay));
 265		}
 266
 267		break;
 268	case RECALIBRATING:
 269		/* we already detected a spew and requested a recalibration,
 270		 * just wait for the queue to kick into action. */
 271		break;
 272	}
 273}
 274
 275/*
 276 * HGPK Mouse Mode format (standard mouse format, sans middle button)
 277 *
 278 * byte 0:	y-over	x-over	y-neg	x-neg	1	0	swr	swl
 279 * byte 1:	x7	x6	x5	x4	x3	x2	x1	x0
 280 * byte 2:	y7	y6	y5	y4	y3	y2	y1	y0
 281 *
 282 * swr/swl are the left/right buttons.
 283 * x-neg/y-neg are the x and y delta negative bits
 284 * x-over/y-over are the x and y overflow bits
 285 *
 286 * ---
 287 *
 288 * HGPK Advanced Mode - single-mode format
 289 *
 290 * byte 0(PT):  1    1    0    0    1    1     1     1
 291 * byte 0(GS):  1    1    1    1    1    1     1     1
 292 * byte 1:      0   x6   x5   x4   x3   x2    x1    x0
 293 * byte 2(PT):  0    0   x9   x8   x7    ? pt-dsw    0
 294 * byte 2(GS):  0  x10   x9   x8   x7    ? gs-dsw pt-dsw
 295 * byte 3:      0   y9   y8   y7    1    0   swr   swl
 296 * byte 4:      0   y6   y5   y4   y3   y2    y1    y0
 297 * byte 5:      0   z6   z5   z4   z3   z2    z1    z0
 298 *
 299 * ?'s are not defined in the protocol spec, may vary between models.
 300 *
 301 * swr/swl are the left/right buttons.
 302 *
 303 * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a
 304 * pen/finger
 305 */
 306static bool hgpk_is_byte_valid(struct psmouse *psmouse, unsigned char *packet)
 307{
 308	struct hgpk_data *priv = psmouse->private;
 309	int pktcnt = psmouse->pktcnt;
 310	bool valid;
 311
 312	switch (priv->mode) {
 313	case HGPK_MODE_MOUSE:
 314		valid = (packet[0] & 0x0C) == 0x08;
 315		break;
 316
 317	case HGPK_MODE_GLIDESENSOR:
 318		valid = pktcnt == 1 ?
 319			packet[0] == HGPK_GS : !(packet[pktcnt - 1] & 0x80);
 320		break;
 321
 322	case HGPK_MODE_PENTABLET:
 323		valid = pktcnt == 1 ?
 324			packet[0] == HGPK_PT : !(packet[pktcnt - 1] & 0x80);
 325		break;
 326
 327	default:
 328		valid = false;
 329		break;
 330	}
 331
 332	if (!valid)
 333		psmouse_dbg(psmouse,
 334			    "bad data, mode %d (%d) %*ph\n",
 335			    priv->mode, pktcnt, 6, psmouse->packet);
 
 
 
 336
 337	return valid;
 338}
 339
 340static void hgpk_process_advanced_packet(struct psmouse *psmouse)
 341{
 342	struct hgpk_data *priv = psmouse->private;
 343	struct input_dev *idev = psmouse->dev;
 344	unsigned char *packet = psmouse->packet;
 345	int down = !!(packet[2] & 2);
 346	int left = !!(packet[3] & 1);
 347	int right = !!(packet[3] & 2);
 348	int x = packet[1] | ((packet[2] & 0x78) << 4);
 349	int y = packet[4] | ((packet[3] & 0x70) << 3);
 350
 351	if (priv->mode == HGPK_MODE_GLIDESENSOR) {
 352		int pt_down = !!(packet[2] & 1);
 353		int finger_down = !!(packet[2] & 2);
 354		int z = packet[5];
 355
 356		input_report_abs(idev, ABS_PRESSURE, z);
 357		if (tpdebug)
 358			psmouse_dbg(psmouse, "pd=%d fd=%d z=%d",
 359				    pt_down, finger_down, z);
 360	} else {
 361		/*
 362		 * PenTablet mode does not report pressure, so we don't
 363		 * report it here
 364		 */
 365		if (tpdebug)
 366			psmouse_dbg(psmouse, "pd=%d ", down);
 367	}
 368
 369	if (tpdebug)
 370		psmouse_dbg(psmouse, "l=%d r=%d x=%d y=%d\n",
 371			    left, right, x, y);
 372
 373	input_report_key(idev, BTN_TOUCH, down);
 374	input_report_key(idev, BTN_LEFT, left);
 375	input_report_key(idev, BTN_RIGHT, right);
 376
 377	/*
 378	 * If this packet says that the finger was removed, reset our position
 379	 * tracking so that we don't erroneously detect a jump on next press.
 380	 */
 381	if (!down) {
 382		hgpk_reset_hack_state(psmouse);
 383		goto done;
 384	}
 385
 386	/*
 387	 * Weed out duplicate packets (we get quite a few, and they mess up
 388	 * our jump detection)
 389	 */
 390	if (x == priv->abs_x && y == priv->abs_y) {
 391		if (++priv->dupe_count > SPEW_WATCH_COUNT) {
 392			if (tpdebug)
 393				psmouse_dbg(psmouse, "hard spew detected\n");
 394			priv->spew_flag = RECALIBRATING;
 395			psmouse_queue_work(psmouse, &priv->recalib_wq,
 396					   msecs_to_jiffies(spew_delay));
 397		}
 398		goto done;
 399	}
 400
 401	/* not a duplicate, continue with position reporting */
 402	priv->dupe_count = 0;
 403
 404	/* Don't apply hacks in PT mode, it seems reliable */
 405	if (priv->mode != HGPK_MODE_PENTABLET && priv->abs_x != -1) {
 406		int x_diff = priv->abs_x - x;
 407		int y_diff = priv->abs_y - y;
 408		if (hgpk_discard_decay_hack(psmouse, x_diff, y_diff)) {
 409			if (tpdebug)
 410				psmouse_dbg(psmouse, "discarding\n");
 411			goto done;
 412		}
 413		hgpk_spewing_hack(psmouse, left, right, x_diff, y_diff);
 414	}
 415
 416	input_report_abs(idev, ABS_X, x);
 417	input_report_abs(idev, ABS_Y, y);
 418	priv->abs_x = x;
 419	priv->abs_y = y;
 420
 421done:
 422	input_sync(idev);
 423}
 424
 425static void hgpk_process_simple_packet(struct psmouse *psmouse)
 426{
 427	struct input_dev *dev = psmouse->dev;
 428	unsigned char *packet = psmouse->packet;
 429	int left = packet[0] & 1;
 430	int right = (packet[0] >> 1) & 1;
 431	int x = packet[1] - ((packet[0] << 4) & 0x100);
 432	int y = ((packet[0] << 3) & 0x100) - packet[2];
 433
 434	if (packet[0] & 0xc0)
 435		psmouse_dbg(psmouse,
 436			    "overflow -- 0x%02x 0x%02x 0x%02x\n",
 437			    packet[0], packet[1], packet[2]);
 438
 439	if (hgpk_discard_decay_hack(psmouse, x, y)) {
 440		if (tpdebug)
 441			psmouse_dbg(psmouse, "discarding\n");
 442		return;
 443	}
 444
 445	hgpk_spewing_hack(psmouse, left, right, x, y);
 446
 447	if (tpdebug)
 448		psmouse_dbg(psmouse, "l=%d r=%d x=%d y=%d\n",
 449			    left, right, x, y);
 450
 451	input_report_key(dev, BTN_LEFT, left);
 452	input_report_key(dev, BTN_RIGHT, right);
 453
 454	input_report_rel(dev, REL_X, x);
 455	input_report_rel(dev, REL_Y, y);
 456
 457	input_sync(dev);
 458}
 459
 460static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
 461{
 462	struct hgpk_data *priv = psmouse->private;
 463
 464	if (!hgpk_is_byte_valid(psmouse, psmouse->packet))
 465		return PSMOUSE_BAD_DATA;
 466
 467	if (psmouse->pktcnt >= psmouse->pktsize) {
 468		if (priv->mode == HGPK_MODE_MOUSE)
 469			hgpk_process_simple_packet(psmouse);
 470		else
 471			hgpk_process_advanced_packet(psmouse);
 472		return PSMOUSE_FULL_PACKET;
 473	}
 474
 475	if (priv->recalib_window) {
 476		if (time_before(jiffies, priv->recalib_window)) {
 477			/*
 478			 * ugh, got a packet inside our recalibration
 479			 * window, schedule another recalibration.
 480			 */
 481			psmouse_dbg(psmouse,
 482				    "packet inside calibration window, queueing another recalibration\n");
 483			psmouse_queue_work(psmouse, &priv->recalib_wq,
 484					msecs_to_jiffies(post_interrupt_delay));
 485		}
 486		priv->recalib_window = 0;
 487	}
 488
 489	return PSMOUSE_GOOD_DATA;
 490}
 491
 492static int hgpk_select_mode(struct psmouse *psmouse)
 493{
 494	struct ps2dev *ps2dev = &psmouse->ps2dev;
 495	struct hgpk_data *priv = psmouse->private;
 496	int i;
 497	int cmd;
 498
 499	/*
 500	 * 4 disables to enable advanced mode
 501	 * then 3 0xf2 bytes as the preamble for GS/PT selection
 502	 */
 503	const int advanced_init[] = {
 504		PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
 505		PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
 506		0xf2, 0xf2, 0xf2,
 507	};
 508
 509	switch (priv->mode) {
 510	case HGPK_MODE_MOUSE:
 511		psmouse->pktsize = 3;
 512		break;
 513
 514	case HGPK_MODE_GLIDESENSOR:
 515	case HGPK_MODE_PENTABLET:
 516		psmouse->pktsize = 6;
 517
 518		/* Switch to 'Advanced mode.', four disables in a row. */
 519		for (i = 0; i < ARRAY_SIZE(advanced_init); i++)
 520			if (ps2_command(ps2dev, NULL, advanced_init[i]))
 521				return -EIO;
 522
 523		/* select between GlideSensor (mouse) or PenTablet */
 524		cmd = priv->mode == HGPK_MODE_GLIDESENSOR ?
 525			PSMOUSE_CMD_SETSCALE11 : PSMOUSE_CMD_SETSCALE21;
 526
 527		if (ps2_command(ps2dev, NULL, cmd))
 528			return -EIO;
 529		break;
 530
 531	default:
 532		return -EINVAL;
 533	}
 534
 535	return 0;
 536}
 537
 538static void hgpk_setup_input_device(struct input_dev *input,
 539				    struct input_dev *old_input,
 540				    enum hgpk_mode mode)
 541{
 542	if (old_input) {
 543		input->name = old_input->name;
 544		input->phys = old_input->phys;
 545		input->id = old_input->id;
 546		input->dev.parent = old_input->dev.parent;
 547	}
 548
 549	memset(input->evbit, 0, sizeof(input->evbit));
 550	memset(input->relbit, 0, sizeof(input->relbit));
 551	memset(input->keybit, 0, sizeof(input->keybit));
 552
 553	/* All modes report left and right buttons */
 554	__set_bit(EV_KEY, input->evbit);
 555	__set_bit(BTN_LEFT, input->keybit);
 556	__set_bit(BTN_RIGHT, input->keybit);
 557
 558	switch (mode) {
 559	case HGPK_MODE_MOUSE:
 560		__set_bit(EV_REL, input->evbit);
 561		__set_bit(REL_X, input->relbit);
 562		__set_bit(REL_Y, input->relbit);
 563		break;
 564
 565	case HGPK_MODE_GLIDESENSOR:
 566		__set_bit(BTN_TOUCH, input->keybit);
 567		__set_bit(BTN_TOOL_FINGER, input->keybit);
 568
 569		__set_bit(EV_ABS, input->evbit);
 570
 571		/* GlideSensor has pressure sensor, PenTablet does not */
 572		input_set_abs_params(input, ABS_PRESSURE, 0, 15, 0, 0);
 573
 574		/* From device specs */
 575		input_set_abs_params(input, ABS_X, 0, 399, 0, 0);
 576		input_set_abs_params(input, ABS_Y, 0, 290, 0, 0);
 577
 578		/* Calculated by hand based on usable size (52mm x 38mm) */
 579		input_abs_set_res(input, ABS_X, 8);
 580		input_abs_set_res(input, ABS_Y, 8);
 581		break;
 582
 583	case HGPK_MODE_PENTABLET:
 584		__set_bit(BTN_TOUCH, input->keybit);
 585		__set_bit(BTN_TOOL_FINGER, input->keybit);
 586
 587		__set_bit(EV_ABS, input->evbit);
 588
 589		/* From device specs */
 590		input_set_abs_params(input, ABS_X, 0, 999, 0, 0);
 591		input_set_abs_params(input, ABS_Y, 5, 239, 0, 0);
 592
 593		/* Calculated by hand based on usable size (156mm x 38mm) */
 594		input_abs_set_res(input, ABS_X, 6);
 595		input_abs_set_res(input, ABS_Y, 8);
 596		break;
 597
 598	default:
 599		BUG();
 600	}
 601}
 602
 603static int hgpk_reset_device(struct psmouse *psmouse, bool recalibrate)
 604{
 605	int err;
 606
 607	psmouse_reset(psmouse);
 608
 609	if (recalibrate) {
 610		struct ps2dev *ps2dev = &psmouse->ps2dev;
 611
 612		/* send the recalibrate request */
 613		if (ps2_command(ps2dev, NULL, 0xf5) ||
 614		    ps2_command(ps2dev, NULL, 0xf5) ||
 615		    ps2_command(ps2dev, NULL, 0xe6) ||
 616		    ps2_command(ps2dev, NULL, 0xf5)) {
 617			return -1;
 618		}
 619
 620		/* according to ALPS, 150mS is required for recalibration */
 621		msleep(150);
 622	}
 623
 624	err = hgpk_select_mode(psmouse);
 625	if (err) {
 626		psmouse_err(psmouse, "failed to select mode\n");
 627		return err;
 628	}
 629
 630	hgpk_reset_hack_state(psmouse);
 631
 632	return 0;
 633}
 634
 635static int hgpk_force_recalibrate(struct psmouse *psmouse)
 636{
 637	struct hgpk_data *priv = psmouse->private;
 638	int err;
 639
 640	/* C-series touchpads added the recalibrate command */
 641	if (psmouse->model < HGPK_MODEL_C)
 642		return 0;
 643
 644	if (!autorecal) {
 645		psmouse_dbg(psmouse, "recalibration disabled, ignoring\n");
 646		return 0;
 647	}
 648
 649	psmouse_dbg(psmouse, "recalibrating touchpad..\n");
 650
 651	/* we don't want to race with the irq handler, nor with resyncs */
 652	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
 653
 654	/* start by resetting the device */
 655	err = hgpk_reset_device(psmouse, true);
 656	if (err)
 657		return err;
 658
 659	/*
 660	 * XXX: If a finger is down during this delay, recalibration will
 661	 * detect capacitance incorrectly.  This is a hardware bug, and
 662	 * we don't have a good way to deal with it.  The 2s window stuff
 663	 * (below) is our best option for now.
 664	 */
 665	if (psmouse_activate(psmouse))
 666		return -1;
 667
 668	if (tpdebug)
 669		psmouse_dbg(psmouse, "touchpad reactivated\n");
 670
 671	/*
 672	 * If we get packets right away after recalibrating, it's likely
 673	 * that a finger was on the touchpad.  If so, it's probably
 674	 * miscalibrated, so we optionally schedule another.
 675	 */
 676	if (recal_guard_time)
 677		priv->recalib_window = jiffies +
 678			msecs_to_jiffies(recal_guard_time);
 679
 680	return 0;
 681}
 682
 683/*
 684 * This puts the touchpad in a power saving mode; according to ALPS, current
 685 * consumption goes down to 50uA after running this.  To turn power back on,
 686 * we drive MS-DAT low.  Measuring with a 1mA resolution ammeter says that
 687 * the current on the SUS_3.3V rail drops from 3mA or 4mA to 0 when we do this.
 688 *
 689 * We have no formal spec that details this operation -- the low-power
 690 * sequence came from a long-lost email trail.
 691 */
 692static int hgpk_toggle_powersave(struct psmouse *psmouse, int enable)
 693{
 694	struct ps2dev *ps2dev = &psmouse->ps2dev;
 695	int timeo;
 696	int err;
 697
 698	/* Added on D-series touchpads */
 699	if (psmouse->model < HGPK_MODEL_D)
 700		return 0;
 701
 702	if (enable) {
 703		psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
 704
 705		/*
 706		 * Sending a byte will drive MS-DAT low; this will wake up
 707		 * the controller.  Once we get an ACK back from it, it
 708		 * means we can continue with the touchpad re-init.  ALPS
 709		 * tells us that 1s should be long enough, so set that as
 710		 * the upper bound. (in practice, it takes about 3 loops.)
 711		 */
 712		for (timeo = 20; timeo > 0; timeo--) {
 713			if (!ps2_sendbyte(ps2dev, PSMOUSE_CMD_DISABLE, 20))
 
 714				break;
 715			msleep(25);
 716		}
 717
 718		err = hgpk_reset_device(psmouse, false);
 719		if (err) {
 720			psmouse_err(psmouse, "Failed to reset device!\n");
 721			return err;
 722		}
 723
 724		/* should be all set, enable the touchpad */
 725		psmouse_activate(psmouse);
 726		psmouse_dbg(psmouse, "Touchpad powered up.\n");
 727	} else {
 728		psmouse_dbg(psmouse, "Powering off touchpad.\n");
 729
 730		if (ps2_command(ps2dev, NULL, 0xec) ||
 731		    ps2_command(ps2dev, NULL, 0xec) ||
 732		    ps2_command(ps2dev, NULL, 0xea)) {
 733			return -1;
 734		}
 735
 736		psmouse_set_state(psmouse, PSMOUSE_IGNORE);
 737
 738		/* probably won't see an ACK, the touchpad will be off */
 739		ps2_sendbyte(ps2dev, 0xec, 20);
 740	}
 741
 742	return 0;
 743}
 744
 745static int hgpk_poll(struct psmouse *psmouse)
 746{
 747	/* We can't poll, so always return failure. */
 748	return -1;
 749}
 750
 751static int hgpk_reconnect(struct psmouse *psmouse)
 752{
 753	struct hgpk_data *priv = psmouse->private;
 754
 755	/*
 756	 * During suspend/resume the ps2 rails remain powered.  We don't want
 757	 * to do a reset because it's flush data out of buffers; however,
 758	 * earlier prototypes (B1) had some brokenness that required a reset.
 759	 */
 760	if (olpc_board_at_least(olpc_board(0xb2)))
 761		if (psmouse->ps2dev.serio->dev.power.power_state.event !=
 762				PM_EVENT_ON)
 763			return 0;
 764
 765	priv->powered = 1;
 766	return hgpk_reset_device(psmouse, false);
 767}
 768
 769static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
 770{
 771	struct hgpk_data *priv = psmouse->private;
 772
 773	return sprintf(buf, "%d\n", priv->powered);
 774}
 775
 776static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
 777				const char *buf, size_t count)
 778{
 779	struct hgpk_data *priv = psmouse->private;
 780	unsigned int value;
 781	int err;
 782
 783	err = kstrtouint(buf, 10, &value);
 784	if (err)
 785		return err;
 786
 787	if (value > 1)
 788		return -EINVAL;
 789
 790	if (value != priv->powered) {
 791		/*
 792		 * hgpk_toggle_power will deal w/ state so
 793		 * we're not racing w/ irq
 794		 */
 795		err = hgpk_toggle_powersave(psmouse, value);
 796		if (!err)
 797			priv->powered = value;
 798	}
 799
 800	return err ? err : count;
 801}
 802
 803__PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
 804		      hgpk_show_powered, hgpk_set_powered, false);
 805
 806static ssize_t attr_show_mode(struct psmouse *psmouse, void *data, char *buf)
 807{
 808	struct hgpk_data *priv = psmouse->private;
 809
 810	return sprintf(buf, "%s\n", hgpk_mode_names[priv->mode]);
 811}
 812
 813static ssize_t attr_set_mode(struct psmouse *psmouse, void *data,
 814			     const char *buf, size_t len)
 815{
 816	struct hgpk_data *priv = psmouse->private;
 817	enum hgpk_mode old_mode = priv->mode;
 818	enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len);
 819	struct input_dev *old_dev = psmouse->dev;
 820	struct input_dev *new_dev;
 821	int err;
 822
 823	if (new_mode == HGPK_MODE_INVALID)
 824		return -EINVAL;
 825
 826	if (old_mode == new_mode)
 827		return len;
 828
 829	new_dev = input_allocate_device();
 830	if (!new_dev)
 831		return -ENOMEM;
 832
 833	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
 834
 835	/* Switch device into the new mode */
 836	priv->mode = new_mode;
 837	err = hgpk_reset_device(psmouse, false);
 838	if (err)
 839		goto err_try_restore;
 840
 841	hgpk_setup_input_device(new_dev, old_dev, new_mode);
 842
 843	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
 844
 845	err = input_register_device(new_dev);
 846	if (err)
 847		goto err_try_restore;
 848
 849	psmouse->dev = new_dev;
 850	input_unregister_device(old_dev);
 851
 852	return len;
 853
 854err_try_restore:
 855	input_free_device(new_dev);
 856	priv->mode = old_mode;
 857	hgpk_reset_device(psmouse, false);
 858
 859	return err;
 860}
 861
 862PSMOUSE_DEFINE_ATTR(hgpk_mode, S_IWUSR | S_IRUGO, NULL,
 863		    attr_show_mode, attr_set_mode);
 864
 865static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse,
 866		void *data, char *buf)
 867{
 868	return -EINVAL;
 869}
 870
 871static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data,
 872				const char *buf, size_t count)
 873{
 874	struct hgpk_data *priv = psmouse->private;
 875	unsigned int value;
 876	int err;
 877
 878	err = kstrtouint(buf, 10, &value);
 879	if (err)
 880		return err;
 881
 882	if (value != 1)
 883		return -EINVAL;
 884
 885	/*
 886	 * We queue work instead of doing recalibration right here
 887	 * to avoid adding locking to hgpk_force_recalibrate()
 888	 * since workqueue provides serialization.
 889	 */
 890	psmouse_queue_work(psmouse, &priv->recalib_wq, 0);
 891	return count;
 892}
 893
 894__PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL,
 895		      hgpk_trigger_recal_show, hgpk_trigger_recal, false);
 896
 897static void hgpk_disconnect(struct psmouse *psmouse)
 898{
 899	struct hgpk_data *priv = psmouse->private;
 900
 901	device_remove_file(&psmouse->ps2dev.serio->dev,
 902			   &psmouse_attr_powered.dattr);
 903	device_remove_file(&psmouse->ps2dev.serio->dev,
 904			   &psmouse_attr_hgpk_mode.dattr);
 905
 906	if (psmouse->model >= HGPK_MODEL_C)
 907		device_remove_file(&psmouse->ps2dev.serio->dev,
 908				   &psmouse_attr_recalibrate.dattr);
 909
 910	psmouse_reset(psmouse);
 911	kfree(priv);
 912}
 913
 914static void hgpk_recalib_work(struct work_struct *work)
 915{
 916	struct delayed_work *w = to_delayed_work(work);
 917	struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
 918	struct psmouse *psmouse = priv->psmouse;
 919
 920	if (hgpk_force_recalibrate(psmouse))
 921		psmouse_err(psmouse, "recalibration failed!\n");
 922}
 923
 924static int hgpk_register(struct psmouse *psmouse)
 925{
 926	struct hgpk_data *priv = psmouse->private;
 927	int err;
 928
 929	/* register handlers */
 930	psmouse->protocol_handler = hgpk_process_byte;
 931	psmouse->poll = hgpk_poll;
 932	psmouse->disconnect = hgpk_disconnect;
 933	psmouse->reconnect = hgpk_reconnect;
 934
 935	/* Disable the idle resync. */
 936	psmouse->resync_time = 0;
 937	/* Reset after a lot of bad bytes. */
 938	psmouse->resetafter = 1024;
 939
 940	hgpk_setup_input_device(psmouse->dev, NULL, priv->mode);
 941
 942	err = device_create_file(&psmouse->ps2dev.serio->dev,
 943				 &psmouse_attr_powered.dattr);
 944	if (err) {
 945		psmouse_err(psmouse, "Failed creating 'powered' sysfs node\n");
 946		return err;
 947	}
 948
 949	err = device_create_file(&psmouse->ps2dev.serio->dev,
 950				 &psmouse_attr_hgpk_mode.dattr);
 951	if (err) {
 952		psmouse_err(psmouse,
 953			    "Failed creating 'hgpk_mode' sysfs node\n");
 954		goto err_remove_powered;
 955	}
 956
 957	/* C-series touchpads added the recalibrate command */
 958	if (psmouse->model >= HGPK_MODEL_C) {
 959		err = device_create_file(&psmouse->ps2dev.serio->dev,
 960					 &psmouse_attr_recalibrate.dattr);
 961		if (err) {
 962			psmouse_err(psmouse,
 963				    "Failed creating 'recalibrate' sysfs node\n");
 964			goto err_remove_mode;
 965		}
 966	}
 967
 968	return 0;
 969
 970err_remove_mode:
 971	device_remove_file(&psmouse->ps2dev.serio->dev,
 972			   &psmouse_attr_hgpk_mode.dattr);
 973err_remove_powered:
 974	device_remove_file(&psmouse->ps2dev.serio->dev,
 975			   &psmouse_attr_powered.dattr);
 976	return err;
 977}
 978
 979int hgpk_init(struct psmouse *psmouse)
 980{
 981	struct hgpk_data *priv;
 982	int err;
 983
 984	priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
 985	if (!priv) {
 986		err = -ENOMEM;
 987		goto alloc_fail;
 988	}
 989
 990	psmouse->private = priv;
 991
 992	priv->psmouse = psmouse;
 993	priv->powered = true;
 994	priv->mode = hgpk_default_mode;
 995	INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
 996
 997	err = hgpk_reset_device(psmouse, false);
 998	if (err)
 999		goto init_fail;
1000
1001	err = hgpk_register(psmouse);
1002	if (err)
1003		goto init_fail;
1004
1005	return 0;
1006
1007init_fail:
1008	kfree(priv);
1009alloc_fail:
1010	return err;
1011}
1012
1013static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
1014{
1015	struct ps2dev *ps2dev = &psmouse->ps2dev;
1016	unsigned char param[3];
1017
1018	/* E7, E7, E7, E9 gets us a 3 byte identifier */
1019	if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
1020	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
1021	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
1022	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1023		return -EIO;
1024	}
1025
1026	psmouse_dbg(psmouse, "ID: %*ph\n", 3, param);
1027
1028	/* HGPK signature: 0x67, 0x00, 0x<model> */
1029	if (param[0] != 0x67 || param[1] != 0x00)
1030		return -ENODEV;
1031
1032	psmouse_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
1033
1034	return param[2];
1035}
1036
1037int hgpk_detect(struct psmouse *psmouse, bool set_properties)
1038{
1039	int version;
1040
1041	version = hgpk_get_model(psmouse);
1042	if (version < 0)
1043		return version;
1044
1045	if (set_properties) {
1046		psmouse->vendor = "ALPS";
1047		psmouse->name = "HGPK";
1048		psmouse->model = version;
1049	}
1050
1051	return 0;
1052}
1053
1054void hgpk_module_init(void)
1055{
1056	hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name,
1057						strlen(hgpk_mode_name));
1058	if (hgpk_default_mode == HGPK_MODE_INVALID) {
1059		hgpk_default_mode = HGPK_MODE_MOUSE;
1060		strscpy(hgpk_mode_name, hgpk_mode_names[HGPK_MODE_MOUSE],
1061			sizeof(hgpk_mode_name));
1062	}
1063}
v3.5.6
 
   1/*
   2 * OLPC HGPK (XO-1) touchpad PS/2 mouse driver
   3 *
   4 * Copyright (c) 2006-2008 One Laptop Per Child
   5 * Authors:
   6 *   Zephaniah E. Hull
   7 *   Andres Salomon <dilinger@debian.org>
   8 *
   9 * This driver is partly based on the ALPS driver, which is:
  10 *
  11 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
  12 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
  13 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
  14 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
  15 *
  16 * This program is free software; you can redistribute it and/or modify
  17 * it under the terms of the GNU General Public License version 2 as
  18 * published by the Free Software Foundation.
  19 */
  20
  21/*
  22 * The spec from ALPS is available from
  23 * <http://wiki.laptop.org/go/Touch_Pad/Tablet>.  It refers to this
  24 * device as HGPK (Hybrid GS, PT, and Keymatrix).
  25 *
  26 * The earliest versions of the device had simultaneous reporting; that
  27 * was removed.  After that, the device used the Advanced Mode GS/PT streaming
  28 * stuff.  That turned out to be too buggy to support, so we've finally
  29 * switched to Mouse Mode (which utilizes only the center 1/3 of the touchpad).
  30 */
  31
  32#define DEBUG
  33#include <linux/slab.h>
  34#include <linux/input.h>
  35#include <linux/module.h>
  36#include <linux/serio.h>
  37#include <linux/libps2.h>
  38#include <linux/delay.h>
  39#include <asm/olpc.h>
  40
  41#include "psmouse.h"
  42#include "hgpk.h"
  43
  44#define ILLEGAL_XY 999999
  45
  46static bool tpdebug;
  47module_param(tpdebug, bool, 0644);
  48MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG.");
  49
  50static int recalib_delta = 100;
  51module_param(recalib_delta, int, 0644);
  52MODULE_PARM_DESC(recalib_delta,
  53	"packets containing a delta this large will be discarded, and a "
  54	"recalibration may be scheduled.");
  55
  56static int jumpy_delay = 20;
  57module_param(jumpy_delay, int, 0644);
  58MODULE_PARM_DESC(jumpy_delay,
  59	"delay (ms) before recal after jumpiness detected");
  60
  61static int spew_delay = 1;
  62module_param(spew_delay, int, 0644);
  63MODULE_PARM_DESC(spew_delay,
  64	"delay (ms) before recal after packet spew detected");
  65
  66static int recal_guard_time;
  67module_param(recal_guard_time, int, 0644);
  68MODULE_PARM_DESC(recal_guard_time,
  69	"interval (ms) during which recal will be restarted if packet received");
  70
  71static int post_interrupt_delay = 40;
  72module_param(post_interrupt_delay, int, 0644);
  73MODULE_PARM_DESC(post_interrupt_delay,
  74	"delay (ms) before recal after recal interrupt detected");
  75
  76static bool autorecal = true;
  77module_param(autorecal, bool, 0644);
  78MODULE_PARM_DESC(autorecal, "enable recalibration in the driver");
  79
  80static char hgpk_mode_name[16];
  81module_param_string(hgpk_mode, hgpk_mode_name, sizeof(hgpk_mode_name), 0644);
  82MODULE_PARM_DESC(hgpk_mode,
  83	"default hgpk mode: mouse, glidesensor or pentablet");
  84
  85static int hgpk_default_mode = HGPK_MODE_MOUSE;
  86
  87static const char * const hgpk_mode_names[] = {
  88	[HGPK_MODE_MOUSE] = "Mouse",
  89	[HGPK_MODE_GLIDESENSOR] = "GlideSensor",
  90	[HGPK_MODE_PENTABLET] = "PenTablet",
  91};
  92
  93static int hgpk_mode_from_name(const char *buf, int len)
  94{
  95	int i;
  96
  97	for (i = 0; i < ARRAY_SIZE(hgpk_mode_names); i++) {
  98		const char *name = hgpk_mode_names[i];
  99		if (strlen(name) == len && !strncasecmp(name, buf, len))
 100			return i;
 101	}
 102
 103	return HGPK_MODE_INVALID;
 104}
 105
 106/*
 107 * see if new value is within 20% of half of old value
 108 */
 109static int approx_half(int curr, int prev)
 110{
 111	int belowhalf, abovehalf;
 112
 113	if (curr < 5 || prev < 5)
 114		return 0;
 115
 116	belowhalf = (prev * 8) / 20;
 117	abovehalf = (prev * 12) / 20;
 118
 119	return belowhalf < curr && curr <= abovehalf;
 120}
 121
 122/*
 123 * Throw out oddly large delta packets, and any that immediately follow whose
 124 * values are each approximately half of the previous.  It seems that the ALPS
 125 * firmware emits errant packets, and they get averaged out slowly.
 126 */
 127static int hgpk_discard_decay_hack(struct psmouse *psmouse, int x, int y)
 128{
 129	struct hgpk_data *priv = psmouse->private;
 130	int avx, avy;
 131	bool do_recal = false;
 132
 133	avx = abs(x);
 134	avy = abs(y);
 135
 136	/* discard if too big, or half that but > 4 times the prev delta */
 137	if (avx > recalib_delta ||
 138		(avx > recalib_delta / 2 && ((avx / 4) > priv->xlast))) {
 139		psmouse_warn(psmouse, "detected %dpx jump in x\n", x);
 140		priv->xbigj = avx;
 141	} else if (approx_half(avx, priv->xbigj)) {
 142		psmouse_warn(psmouse, "detected secondary %dpx jump in x\n", x);
 143		priv->xbigj = avx;
 144		priv->xsaw_secondary++;
 145	} else {
 146		if (priv->xbigj && priv->xsaw_secondary > 1)
 147			do_recal = true;
 148		priv->xbigj = 0;
 149		priv->xsaw_secondary = 0;
 150	}
 151
 152	if (avy > recalib_delta ||
 153		(avy > recalib_delta / 2 && ((avy / 4) > priv->ylast))) {
 154		psmouse_warn(psmouse, "detected %dpx jump in y\n", y);
 155		priv->ybigj = avy;
 156	} else if (approx_half(avy, priv->ybigj)) {
 157		psmouse_warn(psmouse, "detected secondary %dpx jump in y\n", y);
 158		priv->ybigj = avy;
 159		priv->ysaw_secondary++;
 160	} else {
 161		if (priv->ybigj && priv->ysaw_secondary > 1)
 162			do_recal = true;
 163		priv->ybigj = 0;
 164		priv->ysaw_secondary = 0;
 165	}
 166
 167	priv->xlast = avx;
 168	priv->ylast = avy;
 169
 170	if (do_recal && jumpy_delay) {
 171		psmouse_warn(psmouse, "scheduling recalibration\n");
 172		psmouse_queue_work(psmouse, &priv->recalib_wq,
 173				msecs_to_jiffies(jumpy_delay));
 174	}
 175
 176	return priv->xbigj || priv->ybigj;
 177}
 178
 179static void hgpk_reset_spew_detection(struct hgpk_data *priv)
 180{
 181	priv->spew_count = 0;
 182	priv->dupe_count = 0;
 183	priv->x_tally = 0;
 184	priv->y_tally = 0;
 185	priv->spew_flag = NO_SPEW;
 186}
 187
 188static void hgpk_reset_hack_state(struct psmouse *psmouse)
 189{
 190	struct hgpk_data *priv = psmouse->private;
 191
 192	priv->abs_x = priv->abs_y = -1;
 193	priv->xlast = priv->ylast = ILLEGAL_XY;
 194	priv->xbigj = priv->ybigj = 0;
 195	priv->xsaw_secondary = priv->ysaw_secondary = 0;
 196	hgpk_reset_spew_detection(priv);
 197}
 198
 199/*
 200 * We have no idea why this particular hardware bug occurs.  The touchpad
 201 * will randomly start spewing packets without anything touching the
 202 * pad.  This wouldn't necessarily be bad, but it's indicative of a
 203 * severely miscalibrated pad; attempting to use the touchpad while it's
 204 * spewing means the cursor will jump all over the place, and act "drunk".
 205 *
 206 * The packets that are spewed tend to all have deltas between -2 and 2, and
 207 * the cursor will move around without really going very far.  It will
 208 * tend to end up in the same location; if we tally up the changes over
 209 * 100 packets, we end up w/ a final delta of close to 0.  This happens
 210 * pretty regularly when the touchpad is spewing, and is pretty hard to
 211 * manually trigger (at least for *my* fingers).  So, it makes a perfect
 212 * scheme for detecting spews.
 213 */
 214static void hgpk_spewing_hack(struct psmouse *psmouse,
 215			      int l, int r, int x, int y)
 216{
 217	struct hgpk_data *priv = psmouse->private;
 218
 219	/* ignore button press packets; many in a row could trigger
 220	 * a false-positive! */
 221	if (l || r)
 222		return;
 223
 224	/* don't track spew if the workaround feature has been turned off */
 225	if (!spew_delay)
 226		return;
 227
 228	if (abs(x) > 3 || abs(y) > 3) {
 229		/* no spew, or spew ended */
 230		hgpk_reset_spew_detection(priv);
 231		return;
 232	}
 233
 234	/* Keep a tally of the overall delta to the cursor position caused by
 235	 * the spew */
 236	priv->x_tally += x;
 237	priv->y_tally += y;
 238
 239	switch (priv->spew_flag) {
 240	case NO_SPEW:
 241		/* we're not spewing, but this packet might be the start */
 242		priv->spew_flag = MAYBE_SPEWING;
 243
 244		/* fall-through */
 245
 246	case MAYBE_SPEWING:
 247		priv->spew_count++;
 248
 249		if (priv->spew_count < SPEW_WATCH_COUNT)
 250			break;
 251
 252		/* excessive spew detected, request recalibration */
 253		priv->spew_flag = SPEW_DETECTED;
 254
 255		/* fall-through */
 256
 257	case SPEW_DETECTED:
 258		/* only recalibrate when the overall delta to the cursor
 259		 * is really small. if the spew is causing significant cursor
 260		 * movement, it is probably a case of the user moving the
 261		 * cursor very slowly across the screen. */
 262		if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) {
 263			psmouse_warn(psmouse, "packet spew detected (%d,%d)\n",
 264				     priv->x_tally, priv->y_tally);
 265			priv->spew_flag = RECALIBRATING;
 266			psmouse_queue_work(psmouse, &priv->recalib_wq,
 267					   msecs_to_jiffies(spew_delay));
 268		}
 269
 270		break;
 271	case RECALIBRATING:
 272		/* we already detected a spew and requested a recalibration,
 273		 * just wait for the queue to kick into action. */
 274		break;
 275	}
 276}
 277
 278/*
 279 * HGPK Mouse Mode format (standard mouse format, sans middle button)
 280 *
 281 * byte 0:	y-over	x-over	y-neg	x-neg	1	0	swr	swl
 282 * byte 1:	x7	x6	x5	x4	x3	x2	x1	x0
 283 * byte 2:	y7	y6	y5	y4	y3	y2	y1	y0
 284 *
 285 * swr/swl are the left/right buttons.
 286 * x-neg/y-neg are the x and y delta negative bits
 287 * x-over/y-over are the x and y overflow bits
 288 *
 289 * ---
 290 *
 291 * HGPK Advanced Mode - single-mode format
 292 *
 293 * byte 0(PT):  1    1    0    0    1    1     1     1
 294 * byte 0(GS):  1    1    1    1    1    1     1     1
 295 * byte 1:      0   x6   x5   x4   x3   x2    x1    x0
 296 * byte 2(PT):  0    0   x9   x8   x7    ? pt-dsw    0
 297 * byte 2(GS):  0  x10   x9   x8   x7    ? gs-dsw pt-dsw
 298 * byte 3:      0   y9   y8   y7    1    0   swr   swl
 299 * byte 4:      0   y6   y5   y4   y3   y2    y1    y0
 300 * byte 5:      0   z6   z5   z4   z3   z2    z1    z0
 301 *
 302 * ?'s are not defined in the protocol spec, may vary between models.
 303 *
 304 * swr/swl are the left/right buttons.
 305 *
 306 * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a
 307 * pen/finger
 308 */
 309static bool hgpk_is_byte_valid(struct psmouse *psmouse, unsigned char *packet)
 310{
 311	struct hgpk_data *priv = psmouse->private;
 312	int pktcnt = psmouse->pktcnt;
 313	bool valid;
 314
 315	switch (priv->mode) {
 316	case HGPK_MODE_MOUSE:
 317		valid = (packet[0] & 0x0C) == 0x08;
 318		break;
 319
 320	case HGPK_MODE_GLIDESENSOR:
 321		valid = pktcnt == 1 ?
 322			packet[0] == HGPK_GS : !(packet[pktcnt - 1] & 0x80);
 323		break;
 324
 325	case HGPK_MODE_PENTABLET:
 326		valid = pktcnt == 1 ?
 327			packet[0] == HGPK_PT : !(packet[pktcnt - 1] & 0x80);
 328		break;
 329
 330	default:
 331		valid = false;
 332		break;
 333	}
 334
 335	if (!valid)
 336		psmouse_dbg(psmouse,
 337			    "bad data, mode %d (%d) %02x %02x %02x %02x %02x %02x\n",
 338			    priv->mode, pktcnt,
 339			    psmouse->packet[0], psmouse->packet[1],
 340			    psmouse->packet[2], psmouse->packet[3],
 341			    psmouse->packet[4], psmouse->packet[5]);
 342
 343	return valid;
 344}
 345
 346static void hgpk_process_advanced_packet(struct psmouse *psmouse)
 347{
 348	struct hgpk_data *priv = psmouse->private;
 349	struct input_dev *idev = psmouse->dev;
 350	unsigned char *packet = psmouse->packet;
 351	int down = !!(packet[2] & 2);
 352	int left = !!(packet[3] & 1);
 353	int right = !!(packet[3] & 2);
 354	int x = packet[1] | ((packet[2] & 0x78) << 4);
 355	int y = packet[4] | ((packet[3] & 0x70) << 3);
 356
 357	if (priv->mode == HGPK_MODE_GLIDESENSOR) {
 358		int pt_down = !!(packet[2] & 1);
 359		int finger_down = !!(packet[2] & 2);
 360		int z = packet[5];
 361
 362		input_report_abs(idev, ABS_PRESSURE, z);
 363		if (tpdebug)
 364			psmouse_dbg(psmouse, "pd=%d fd=%d z=%d",
 365				    pt_down, finger_down, z);
 366	} else {
 367		/*
 368		 * PenTablet mode does not report pressure, so we don't
 369		 * report it here
 370		 */
 371		if (tpdebug)
 372			psmouse_dbg(psmouse, "pd=%d ", down);
 373	}
 374
 375	if (tpdebug)
 376		psmouse_dbg(psmouse, "l=%d r=%d x=%d y=%d\n",
 377			    left, right, x, y);
 378
 379	input_report_key(idev, BTN_TOUCH, down);
 380	input_report_key(idev, BTN_LEFT, left);
 381	input_report_key(idev, BTN_RIGHT, right);
 382
 383	/*
 384	 * If this packet says that the finger was removed, reset our position
 385	 * tracking so that we don't erroneously detect a jump on next press.
 386	 */
 387	if (!down) {
 388		hgpk_reset_hack_state(psmouse);
 389		goto done;
 390	}
 391
 392	/*
 393	 * Weed out duplicate packets (we get quite a few, and they mess up
 394	 * our jump detection)
 395	 */
 396	if (x == priv->abs_x && y == priv->abs_y) {
 397		if (++priv->dupe_count > SPEW_WATCH_COUNT) {
 398			if (tpdebug)
 399				psmouse_dbg(psmouse, "hard spew detected\n");
 400			priv->spew_flag = RECALIBRATING;
 401			psmouse_queue_work(psmouse, &priv->recalib_wq,
 402					   msecs_to_jiffies(spew_delay));
 403		}
 404		goto done;
 405	}
 406
 407	/* not a duplicate, continue with position reporting */
 408	priv->dupe_count = 0;
 409
 410	/* Don't apply hacks in PT mode, it seems reliable */
 411	if (priv->mode != HGPK_MODE_PENTABLET && priv->abs_x != -1) {
 412		int x_diff = priv->abs_x - x;
 413		int y_diff = priv->abs_y - y;
 414		if (hgpk_discard_decay_hack(psmouse, x_diff, y_diff)) {
 415			if (tpdebug)
 416				psmouse_dbg(psmouse, "discarding\n");
 417			goto done;
 418		}
 419		hgpk_spewing_hack(psmouse, left, right, x_diff, y_diff);
 420	}
 421
 422	input_report_abs(idev, ABS_X, x);
 423	input_report_abs(idev, ABS_Y, y);
 424	priv->abs_x = x;
 425	priv->abs_y = y;
 426
 427done:
 428	input_sync(idev);
 429}
 430
 431static void hgpk_process_simple_packet(struct psmouse *psmouse)
 432{
 433	struct input_dev *dev = psmouse->dev;
 434	unsigned char *packet = psmouse->packet;
 435	int left = packet[0] & 1;
 436	int right = (packet[0] >> 1) & 1;
 437	int x = packet[1] - ((packet[0] << 4) & 0x100);
 438	int y = ((packet[0] << 3) & 0x100) - packet[2];
 439
 440	if (packet[0] & 0xc0)
 441		psmouse_dbg(psmouse,
 442			    "overflow -- 0x%02x 0x%02x 0x%02x\n",
 443			    packet[0], packet[1], packet[2]);
 444
 445	if (hgpk_discard_decay_hack(psmouse, x, y)) {
 446		if (tpdebug)
 447			psmouse_dbg(psmouse, "discarding\n");
 448		return;
 449	}
 450
 451	hgpk_spewing_hack(psmouse, left, right, x, y);
 452
 453	if (tpdebug)
 454		psmouse_dbg(psmouse, "l=%d r=%d x=%d y=%d\n",
 455			    left, right, x, y);
 456
 457	input_report_key(dev, BTN_LEFT, left);
 458	input_report_key(dev, BTN_RIGHT, right);
 459
 460	input_report_rel(dev, REL_X, x);
 461	input_report_rel(dev, REL_Y, y);
 462
 463	input_sync(dev);
 464}
 465
 466static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
 467{
 468	struct hgpk_data *priv = psmouse->private;
 469
 470	if (!hgpk_is_byte_valid(psmouse, psmouse->packet))
 471		return PSMOUSE_BAD_DATA;
 472
 473	if (psmouse->pktcnt >= psmouse->pktsize) {
 474		if (priv->mode == HGPK_MODE_MOUSE)
 475			hgpk_process_simple_packet(psmouse);
 476		else
 477			hgpk_process_advanced_packet(psmouse);
 478		return PSMOUSE_FULL_PACKET;
 479	}
 480
 481	if (priv->recalib_window) {
 482		if (time_before(jiffies, priv->recalib_window)) {
 483			/*
 484			 * ugh, got a packet inside our recalibration
 485			 * window, schedule another recalibration.
 486			 */
 487			psmouse_dbg(psmouse,
 488				    "packet inside calibration window, queueing another recalibration\n");
 489			psmouse_queue_work(psmouse, &priv->recalib_wq,
 490					msecs_to_jiffies(post_interrupt_delay));
 491		}
 492		priv->recalib_window = 0;
 493	}
 494
 495	return PSMOUSE_GOOD_DATA;
 496}
 497
 498static int hgpk_select_mode(struct psmouse *psmouse)
 499{
 500	struct ps2dev *ps2dev = &psmouse->ps2dev;
 501	struct hgpk_data *priv = psmouse->private;
 502	int i;
 503	int cmd;
 504
 505	/*
 506	 * 4 disables to enable advanced mode
 507	 * then 3 0xf2 bytes as the preamble for GS/PT selection
 508	 */
 509	const int advanced_init[] = {
 510		PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
 511		PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
 512		0xf2, 0xf2, 0xf2,
 513	};
 514
 515	switch (priv->mode) {
 516	case HGPK_MODE_MOUSE:
 517		psmouse->pktsize = 3;
 518		break;
 519
 520	case HGPK_MODE_GLIDESENSOR:
 521	case HGPK_MODE_PENTABLET:
 522		psmouse->pktsize = 6;
 523
 524		/* Switch to 'Advanced mode.', four disables in a row. */
 525		for (i = 0; i < ARRAY_SIZE(advanced_init); i++)
 526			if (ps2_command(ps2dev, NULL, advanced_init[i]))
 527				return -EIO;
 528
 529		/* select between GlideSensor (mouse) or PenTablet */
 530		cmd = priv->mode == HGPK_MODE_GLIDESENSOR ?
 531			PSMOUSE_CMD_SETSCALE11 : PSMOUSE_CMD_SETSCALE21;
 532
 533		if (ps2_command(ps2dev, NULL, cmd))
 534			return -EIO;
 535		break;
 536
 537	default:
 538		return -EINVAL;
 539	}
 540
 541	return 0;
 542}
 543
 544static void hgpk_setup_input_device(struct input_dev *input,
 545				    struct input_dev *old_input,
 546				    enum hgpk_mode mode)
 547{
 548	if (old_input) {
 549		input->name = old_input->name;
 550		input->phys = old_input->phys;
 551		input->id = old_input->id;
 552		input->dev.parent = old_input->dev.parent;
 553	}
 554
 555	memset(input->evbit, 0, sizeof(input->evbit));
 556	memset(input->relbit, 0, sizeof(input->relbit));
 557	memset(input->keybit, 0, sizeof(input->keybit));
 558
 559	/* All modes report left and right buttons */
 560	__set_bit(EV_KEY, input->evbit);
 561	__set_bit(BTN_LEFT, input->keybit);
 562	__set_bit(BTN_RIGHT, input->keybit);
 563
 564	switch (mode) {
 565	case HGPK_MODE_MOUSE:
 566		__set_bit(EV_REL, input->evbit);
 567		__set_bit(REL_X, input->relbit);
 568		__set_bit(REL_Y, input->relbit);
 569		break;
 570
 571	case HGPK_MODE_GLIDESENSOR:
 572		__set_bit(BTN_TOUCH, input->keybit);
 573		__set_bit(BTN_TOOL_FINGER, input->keybit);
 574
 575		__set_bit(EV_ABS, input->evbit);
 576
 577		/* GlideSensor has pressure sensor, PenTablet does not */
 578		input_set_abs_params(input, ABS_PRESSURE, 0, 15, 0, 0);
 579
 580		/* From device specs */
 581		input_set_abs_params(input, ABS_X, 0, 399, 0, 0);
 582		input_set_abs_params(input, ABS_Y, 0, 290, 0, 0);
 583
 584		/* Calculated by hand based on usable size (52mm x 38mm) */
 585		input_abs_set_res(input, ABS_X, 8);
 586		input_abs_set_res(input, ABS_Y, 8);
 587		break;
 588
 589	case HGPK_MODE_PENTABLET:
 590		__set_bit(BTN_TOUCH, input->keybit);
 591		__set_bit(BTN_TOOL_FINGER, input->keybit);
 592
 593		__set_bit(EV_ABS, input->evbit);
 594
 595		/* From device specs */
 596		input_set_abs_params(input, ABS_X, 0, 999, 0, 0);
 597		input_set_abs_params(input, ABS_Y, 5, 239, 0, 0);
 598
 599		/* Calculated by hand based on usable size (156mm x 38mm) */
 600		input_abs_set_res(input, ABS_X, 6);
 601		input_abs_set_res(input, ABS_Y, 8);
 602		break;
 603
 604	default:
 605		BUG();
 606	}
 607}
 608
 609static int hgpk_reset_device(struct psmouse *psmouse, bool recalibrate)
 610{
 611	int err;
 612
 613	psmouse_reset(psmouse);
 614
 615	if (recalibrate) {
 616		struct ps2dev *ps2dev = &psmouse->ps2dev;
 617
 618		/* send the recalibrate request */
 619		if (ps2_command(ps2dev, NULL, 0xf5) ||
 620		    ps2_command(ps2dev, NULL, 0xf5) ||
 621		    ps2_command(ps2dev, NULL, 0xe6) ||
 622		    ps2_command(ps2dev, NULL, 0xf5)) {
 623			return -1;
 624		}
 625
 626		/* according to ALPS, 150mS is required for recalibration */
 627		msleep(150);
 628	}
 629
 630	err = hgpk_select_mode(psmouse);
 631	if (err) {
 632		psmouse_err(psmouse, "failed to select mode\n");
 633		return err;
 634	}
 635
 636	hgpk_reset_hack_state(psmouse);
 637
 638	return 0;
 639}
 640
 641static int hgpk_force_recalibrate(struct psmouse *psmouse)
 642{
 643	struct hgpk_data *priv = psmouse->private;
 644	int err;
 645
 646	/* C-series touchpads added the recalibrate command */
 647	if (psmouse->model < HGPK_MODEL_C)
 648		return 0;
 649
 650	if (!autorecal) {
 651		psmouse_dbg(psmouse, "recalibration disabled, ignoring\n");
 652		return 0;
 653	}
 654
 655	psmouse_dbg(psmouse, "recalibrating touchpad..\n");
 656
 657	/* we don't want to race with the irq handler, nor with resyncs */
 658	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
 659
 660	/* start by resetting the device */
 661	err = hgpk_reset_device(psmouse, true);
 662	if (err)
 663		return err;
 664
 665	/*
 666	 * XXX: If a finger is down during this delay, recalibration will
 667	 * detect capacitance incorrectly.  This is a hardware bug, and
 668	 * we don't have a good way to deal with it.  The 2s window stuff
 669	 * (below) is our best option for now.
 670	 */
 671	if (psmouse_activate(psmouse))
 672		return -1;
 673
 674	if (tpdebug)
 675		psmouse_dbg(psmouse, "touchpad reactivated\n");
 676
 677	/*
 678	 * If we get packets right away after recalibrating, it's likely
 679	 * that a finger was on the touchpad.  If so, it's probably
 680	 * miscalibrated, so we optionally schedule another.
 681	 */
 682	if (recal_guard_time)
 683		priv->recalib_window = jiffies +
 684			msecs_to_jiffies(recal_guard_time);
 685
 686	return 0;
 687}
 688
 689/*
 690 * This puts the touchpad in a power saving mode; according to ALPS, current
 691 * consumption goes down to 50uA after running this.  To turn power back on,
 692 * we drive MS-DAT low.  Measuring with a 1mA resolution ammeter says that
 693 * the current on the SUS_3.3V rail drops from 3mA or 4mA to 0 when we do this.
 694 *
 695 * We have no formal spec that details this operation -- the low-power
 696 * sequence came from a long-lost email trail.
 697 */
 698static int hgpk_toggle_powersave(struct psmouse *psmouse, int enable)
 699{
 700	struct ps2dev *ps2dev = &psmouse->ps2dev;
 701	int timeo;
 702	int err;
 703
 704	/* Added on D-series touchpads */
 705	if (psmouse->model < HGPK_MODEL_D)
 706		return 0;
 707
 708	if (enable) {
 709		psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
 710
 711		/*
 712		 * Sending a byte will drive MS-DAT low; this will wake up
 713		 * the controller.  Once we get an ACK back from it, it
 714		 * means we can continue with the touchpad re-init.  ALPS
 715		 * tells us that 1s should be long enough, so set that as
 716		 * the upper bound. (in practice, it takes about 3 loops.)
 717		 */
 718		for (timeo = 20; timeo > 0; timeo--) {
 719			if (!ps2_sendbyte(&psmouse->ps2dev,
 720					PSMOUSE_CMD_DISABLE, 20))
 721				break;
 722			msleep(25);
 723		}
 724
 725		err = hgpk_reset_device(psmouse, false);
 726		if (err) {
 727			psmouse_err(psmouse, "Failed to reset device!\n");
 728			return err;
 729		}
 730
 731		/* should be all set, enable the touchpad */
 732		psmouse_activate(psmouse);
 733		psmouse_dbg(psmouse, "Touchpad powered up.\n");
 734	} else {
 735		psmouse_dbg(psmouse, "Powering off touchpad.\n");
 736
 737		if (ps2_command(ps2dev, NULL, 0xec) ||
 738		    ps2_command(ps2dev, NULL, 0xec) ||
 739		    ps2_command(ps2dev, NULL, 0xea)) {
 740			return -1;
 741		}
 742
 743		psmouse_set_state(psmouse, PSMOUSE_IGNORE);
 744
 745		/* probably won't see an ACK, the touchpad will be off */
 746		ps2_sendbyte(&psmouse->ps2dev, 0xec, 20);
 747	}
 748
 749	return 0;
 750}
 751
 752static int hgpk_poll(struct psmouse *psmouse)
 753{
 754	/* We can't poll, so always return failure. */
 755	return -1;
 756}
 757
 758static int hgpk_reconnect(struct psmouse *psmouse)
 759{
 760	struct hgpk_data *priv = psmouse->private;
 761
 762	/*
 763	 * During suspend/resume the ps2 rails remain powered.  We don't want
 764	 * to do a reset because it's flush data out of buffers; however,
 765	 * earlier prototypes (B1) had some brokenness that required a reset.
 766	 */
 767	if (olpc_board_at_least(olpc_board(0xb2)))
 768		if (psmouse->ps2dev.serio->dev.power.power_state.event !=
 769				PM_EVENT_ON)
 770			return 0;
 771
 772	priv->powered = 1;
 773	return hgpk_reset_device(psmouse, false);
 774}
 775
 776static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
 777{
 778	struct hgpk_data *priv = psmouse->private;
 779
 780	return sprintf(buf, "%d\n", priv->powered);
 781}
 782
 783static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
 784				const char *buf, size_t count)
 785{
 786	struct hgpk_data *priv = psmouse->private;
 787	unsigned int value;
 788	int err;
 789
 790	err = kstrtouint(buf, 10, &value);
 791	if (err)
 792		return err;
 793
 794	if (value > 1)
 795		return -EINVAL;
 796
 797	if (value != priv->powered) {
 798		/*
 799		 * hgpk_toggle_power will deal w/ state so
 800		 * we're not racing w/ irq
 801		 */
 802		err = hgpk_toggle_powersave(psmouse, value);
 803		if (!err)
 804			priv->powered = value;
 805	}
 806
 807	return err ? err : count;
 808}
 809
 810__PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
 811		      hgpk_show_powered, hgpk_set_powered, false);
 812
 813static ssize_t attr_show_mode(struct psmouse *psmouse, void *data, char *buf)
 814{
 815	struct hgpk_data *priv = psmouse->private;
 816
 817	return sprintf(buf, "%s\n", hgpk_mode_names[priv->mode]);
 818}
 819
 820static ssize_t attr_set_mode(struct psmouse *psmouse, void *data,
 821			     const char *buf, size_t len)
 822{
 823	struct hgpk_data *priv = psmouse->private;
 824	enum hgpk_mode old_mode = priv->mode;
 825	enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len);
 826	struct input_dev *old_dev = psmouse->dev;
 827	struct input_dev *new_dev;
 828	int err;
 829
 830	if (new_mode == HGPK_MODE_INVALID)
 831		return -EINVAL;
 832
 833	if (old_mode == new_mode)
 834		return len;
 835
 836	new_dev = input_allocate_device();
 837	if (!new_dev)
 838		return -ENOMEM;
 839
 840	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
 841
 842	/* Switch device into the new mode */
 843	priv->mode = new_mode;
 844	err = hgpk_reset_device(psmouse, false);
 845	if (err)
 846		goto err_try_restore;
 847
 848	hgpk_setup_input_device(new_dev, old_dev, new_mode);
 849
 850	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
 851
 852	err = input_register_device(new_dev);
 853	if (err)
 854		goto err_try_restore;
 855
 856	psmouse->dev = new_dev;
 857	input_unregister_device(old_dev);
 858
 859	return len;
 860
 861err_try_restore:
 862	input_free_device(new_dev);
 863	priv->mode = old_mode;
 864	hgpk_reset_device(psmouse, false);
 865
 866	return err;
 867}
 868
 869PSMOUSE_DEFINE_ATTR(hgpk_mode, S_IWUSR | S_IRUGO, NULL,
 870		    attr_show_mode, attr_set_mode);
 871
 872static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse,
 873		void *data, char *buf)
 874{
 875	return -EINVAL;
 876}
 877
 878static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data,
 879				const char *buf, size_t count)
 880{
 881	struct hgpk_data *priv = psmouse->private;
 882	unsigned int value;
 883	int err;
 884
 885	err = kstrtouint(buf, 10, &value);
 886	if (err)
 887		return err;
 888
 889	if (value != 1)
 890		return -EINVAL;
 891
 892	/*
 893	 * We queue work instead of doing recalibration right here
 894	 * to avoid adding locking to to hgpk_force_recalibrate()
 895	 * since workqueue provides serialization.
 896	 */
 897	psmouse_queue_work(psmouse, &priv->recalib_wq, 0);
 898	return count;
 899}
 900
 901__PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL,
 902		      hgpk_trigger_recal_show, hgpk_trigger_recal, false);
 903
 904static void hgpk_disconnect(struct psmouse *psmouse)
 905{
 906	struct hgpk_data *priv = psmouse->private;
 907
 908	device_remove_file(&psmouse->ps2dev.serio->dev,
 909			   &psmouse_attr_powered.dattr);
 910	device_remove_file(&psmouse->ps2dev.serio->dev,
 911			   &psmouse_attr_hgpk_mode.dattr);
 912
 913	if (psmouse->model >= HGPK_MODEL_C)
 914		device_remove_file(&psmouse->ps2dev.serio->dev,
 915				   &psmouse_attr_recalibrate.dattr);
 916
 917	psmouse_reset(psmouse);
 918	kfree(priv);
 919}
 920
 921static void hgpk_recalib_work(struct work_struct *work)
 922{
 923	struct delayed_work *w = to_delayed_work(work);
 924	struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
 925	struct psmouse *psmouse = priv->psmouse;
 926
 927	if (hgpk_force_recalibrate(psmouse))
 928		psmouse_err(psmouse, "recalibration failed!\n");
 929}
 930
 931static int hgpk_register(struct psmouse *psmouse)
 932{
 933	struct hgpk_data *priv = psmouse->private;
 934	int err;
 935
 936	/* register handlers */
 937	psmouse->protocol_handler = hgpk_process_byte;
 938	psmouse->poll = hgpk_poll;
 939	psmouse->disconnect = hgpk_disconnect;
 940	psmouse->reconnect = hgpk_reconnect;
 941
 942	/* Disable the idle resync. */
 943	psmouse->resync_time = 0;
 944	/* Reset after a lot of bad bytes. */
 945	psmouse->resetafter = 1024;
 946
 947	hgpk_setup_input_device(psmouse->dev, NULL, priv->mode);
 948
 949	err = device_create_file(&psmouse->ps2dev.serio->dev,
 950				 &psmouse_attr_powered.dattr);
 951	if (err) {
 952		psmouse_err(psmouse, "Failed creating 'powered' sysfs node\n");
 953		return err;
 954	}
 955
 956	err = device_create_file(&psmouse->ps2dev.serio->dev,
 957				 &psmouse_attr_hgpk_mode.dattr);
 958	if (err) {
 959		psmouse_err(psmouse,
 960			    "Failed creating 'hgpk_mode' sysfs node\n");
 961		goto err_remove_powered;
 962	}
 963
 964	/* C-series touchpads added the recalibrate command */
 965	if (psmouse->model >= HGPK_MODEL_C) {
 966		err = device_create_file(&psmouse->ps2dev.serio->dev,
 967					 &psmouse_attr_recalibrate.dattr);
 968		if (err) {
 969			psmouse_err(psmouse,
 970				    "Failed creating 'recalibrate' sysfs node\n");
 971			goto err_remove_mode;
 972		}
 973	}
 974
 975	return 0;
 976
 977err_remove_mode:
 978	device_remove_file(&psmouse->ps2dev.serio->dev,
 979			   &psmouse_attr_hgpk_mode.dattr);
 980err_remove_powered:
 981	device_remove_file(&psmouse->ps2dev.serio->dev,
 982			   &psmouse_attr_powered.dattr);
 983	return err;
 984}
 985
 986int hgpk_init(struct psmouse *psmouse)
 987{
 988	struct hgpk_data *priv;
 989	int err;
 990
 991	priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
 992	if (!priv) {
 993		err = -ENOMEM;
 994		goto alloc_fail;
 995	}
 996
 997	psmouse->private = priv;
 998
 999	priv->psmouse = psmouse;
1000	priv->powered = true;
1001	priv->mode = hgpk_default_mode;
1002	INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
1003
1004	err = hgpk_reset_device(psmouse, false);
1005	if (err)
1006		goto init_fail;
1007
1008	err = hgpk_register(psmouse);
1009	if (err)
1010		goto init_fail;
1011
1012	return 0;
1013
1014init_fail:
1015	kfree(priv);
1016alloc_fail:
1017	return err;
1018}
1019
1020static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
1021{
1022	struct ps2dev *ps2dev = &psmouse->ps2dev;
1023	unsigned char param[3];
1024
1025	/* E7, E7, E7, E9 gets us a 3 byte identifier */
1026	if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
1027	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
1028	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
1029	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1030		return -EIO;
1031	}
1032
1033	psmouse_dbg(psmouse, "ID: %02x %02x %02x\n", param[0], param[1], param[2]);
1034
1035	/* HGPK signature: 0x67, 0x00, 0x<model> */
1036	if (param[0] != 0x67 || param[1] != 0x00)
1037		return -ENODEV;
1038
1039	psmouse_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
1040
1041	return param[2];
1042}
1043
1044int hgpk_detect(struct psmouse *psmouse, bool set_properties)
1045{
1046	int version;
1047
1048	version = hgpk_get_model(psmouse);
1049	if (version < 0)
1050		return version;
1051
1052	if (set_properties) {
1053		psmouse->vendor = "ALPS";
1054		psmouse->name = "HGPK";
1055		psmouse->model = version;
1056	}
1057
1058	return 0;
1059}
1060
1061void hgpk_module_init(void)
1062{
1063	hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name,
1064						strlen(hgpk_mode_name));
1065	if (hgpk_default_mode == HGPK_MODE_INVALID) {
1066		hgpk_default_mode = HGPK_MODE_MOUSE;
1067		strlcpy(hgpk_mode_name, hgpk_mode_names[HGPK_MODE_MOUSE],
1068			sizeof(hgpk_mode_name));
1069	}
1070}