psmouse-base.c 48.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * PS/2 mouse driver
 *
 * Copyright (c) 1999-2002 Vojtech Pavlik
 * Copyright (c) 2003-2004 Dmitry Torokhov
 */

/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

14 15 16
#define pr_fmt(fmt)		KBUILD_MODNAME ": " fmt
#define psmouse_fmt(fmt)	fmt

L
Linus Torvalds 已提交
17 18 19 20 21 22 23 24
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/serio.h>
#include <linux/init.h>
#include <linux/libps2.h>
25 26
#include <linux/mutex.h>

L
Linus Torvalds 已提交
27 28 29 30
#include "psmouse.h"
#include "synaptics.h"
#include "logips2pp.h"
#include "alps.h"
31
#include "hgpk.h"
32
#include "lifebook.h"
33
#include "trackpoint.h"
34
#include "touchkit_ps2.h"
35
#include "elantech.h"
36
#include "sentelic.h"
37
#include "cypress_ps2.h"
38
#include "focaltech.h"
T
Thomas Hellstrom 已提交
39
#include "vmmouse.h"
40
#include "byd.h"
L
Linus Torvalds 已提交
41 42 43 44 45 46 47

#define DRIVER_DESC	"PS/2 mouse driver"

MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

48
static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
49 50
static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
51
static const struct kernel_param_ops param_ops_proto_abbrev = {
52 53 54
	.set = psmouse_set_maxproto,
	.get = psmouse_get_maxproto,
};
L
Linus Torvalds 已提交
55 56
#define param_check_proto_abbrev(name, p)	__param_check(name, p, unsigned int)
module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
57
MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66

static unsigned int psmouse_resolution = 200;
module_param_named(resolution, psmouse_resolution, uint, 0644);
MODULE_PARM_DESC(resolution, "Resolution, in dpi.");

static unsigned int psmouse_rate = 100;
module_param_named(rate, psmouse_rate, uint, 0644);
MODULE_PARM_DESC(rate, "Report rate, in reports per second.");

67
static bool psmouse_smartscroll = true;
L
Linus Torvalds 已提交
68 69 70
module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");

71
static unsigned int psmouse_resetafter = 5;
L
Linus Torvalds 已提交
72 73 74
module_param_named(resetafter, psmouse_resetafter, uint, 0644);
MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");

75
static unsigned int psmouse_resync_time;
76 77 78
module_param_named(resync_time, psmouse_resync_time, uint, 0644);
MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");

79 80 81 82 83 84 85 86 87 88 89 90
PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
			NULL,
			psmouse_attr_show_protocol, psmouse_attr_set_protocol);
PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
			(void *) offsetof(struct psmouse, rate),
			psmouse_show_int_attr, psmouse_attr_set_rate);
PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
			(void *) offsetof(struct psmouse, resolution),
			psmouse_show_int_attr, psmouse_attr_set_resolution);
PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
			(void *) offsetof(struct psmouse, resetafter),
			psmouse_show_int_attr, psmouse_set_int_attr);
91 92 93
PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
			(void *) offsetof(struct psmouse, resync_time),
			psmouse_show_int_attr, psmouse_set_int_attr);
94 95 96 97 98 99

static struct attribute *psmouse_attributes[] = {
	&psmouse_attr_protocol.dattr.attr,
	&psmouse_attr_rate.dattr.attr,
	&psmouse_attr_resolution.dattr.attr,
	&psmouse_attr_resetafter.dattr.attr,
100
	&psmouse_attr_resync_time.dattr.attr,
101 102 103 104 105 106
	NULL
};

static struct attribute_group psmouse_attribute_group = {
	.attrs	= psmouse_attributes,
};
L
Linus Torvalds 已提交
107

108
/*
109
 * psmouse_mutex protects all operations changing state of mouse
110 111 112 113 114
 * (connecting, disconnecting, changing rate or resolution via
 * sysfs). We could use a per-device semaphore but since there
 * rarely more than one PS/2 mouse connected and since semaphore
 * is taken in "slow" paths it is not worth it.
 */
115
static DEFINE_MUTEX(psmouse_mutex);
116

117 118
static struct workqueue_struct *kpsmoused_wq;

119 120
struct psmouse_protocol {
	enum psmouse_type type;
121
	bool maxproto;
122
	bool ignore_parity; /* Protocol should ignore parity errors from KBC */
123
	bool try_passthru; /* Try protocol also on passthrough ports */
H
Helge Deller 已提交
124 125
	const char *name;
	const char *alias;
126
	int (*detect)(struct psmouse *, bool);
127 128
	int (*init)(struct psmouse *);
};
L
Linus Torvalds 已提交
129 130 131 132 133

/*
 * psmouse_process_byte() analyzes the PS/2 data stream and reports
 * relevant events to the input module once full packet has arrived.
 */
134
psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
L
Linus Torvalds 已提交
135
{
136
	struct input_dev *dev = psmouse->dev;
L
Linus Torvalds 已提交
137 138 139 140 141
	unsigned char *packet = psmouse->packet;

	if (psmouse->pktcnt < psmouse->pktsize)
		return PSMOUSE_GOOD_DATA;

142
	/* Full packet accumulated, process it */
L
Linus Torvalds 已提交
143

144 145 146
	switch (psmouse->type) {
	case PSMOUSE_IMPS:
		/* IntelliMouse has scroll wheel */
L
Linus Torvalds 已提交
147
		input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
148
		break;
L
Linus Torvalds 已提交
149

150 151
	case PSMOUSE_IMEX:
		/* Scroll wheel and buttons on IntelliMouse Explorer */
152
		switch (packet[3] & 0xC0) {
153 154 155 156 157 158 159 160 161 162 163 164
		case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
			input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
			break;
		case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
			input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
			break;
		case 0x00:
		case 0xC0:
			input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
			input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
			input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
			break;
165
		}
166
		break;
L
Linus Torvalds 已提交
167

168 169 170
	case PSMOUSE_GENPS:
		/* Report scroll buttons on NetMice */
		input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
L
Linus Torvalds 已提交
171

172
		/* Extra buttons on Genius NewNet 3D */
L
Linus Torvalds 已提交
173 174
		input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
		input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
175
		break;
L
Linus Torvalds 已提交
176

177 178
	case PSMOUSE_THINKPS:
		/* Extra button on ThinkingMouse */
L
Linus Torvalds 已提交
179
		input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
180 181 182 183 184

		/*
		 * Without this bit of weirdness moving up gives wildly
		 * high Y changes.
		 */
L
Linus Torvalds 已提交
185
		packet[1] |= (packet[0] & 0x40) << 1;
186
		break;
L
Linus Torvalds 已提交
187

188 189 190 191 192
	case PSMOUSE_CORTRON:
		/*
		 * Cortron PS2 Trackball reports SIDE button in the
		 * 4th bit of the first byte.
		 */
193 194
		input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
		packet[0] |= 0x08;
195
		break;
196

197 198 199
	default:
		break;
	}
L
Linus Torvalds 已提交
200

201
	/* Generic PS/2 Mouse */
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209 210 211 212 213
	input_report_key(dev, BTN_LEFT,    packet[0]       & 1);
	input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
	input_report_key(dev, BTN_RIGHT,  (packet[0] >> 1) & 1);

	input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
	input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);

	input_sync(dev);

	return PSMOUSE_FULL_PACKET;
}

214 215 216 217 218 219
void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
		unsigned long delay)
{
	queue_delayed_work(kpsmoused_wq, work, delay);
}

L
Linus Torvalds 已提交
220
/*
221 222 223 224 225
 * __psmouse_set_state() sets new psmouse state and resets all flags.
 */
static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
{
	psmouse->state = new_state;
226
	psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
227 228 229 230 231 232 233 234 235
	psmouse->ps2dev.flags = 0;
	psmouse->last = jiffies;
}

/*
 * psmouse_set_state() sets new psmouse state and resets all flags and
 * counters while holding serio lock so fighting with interrupt handler
 * is not a concern.
 */
236
void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
237 238 239 240 241 242 243 244 245 246
{
	serio_pause_rx(psmouse->ps2dev.serio);
	__psmouse_set_state(psmouse, new_state);
	serio_continue_rx(psmouse->ps2dev.serio);
}

/*
 * psmouse_handle_byte() processes one byte of the input data stream
 * by calling corresponding protocol handler.
 */
247
static int psmouse_handle_byte(struct psmouse *psmouse)
248
{
249
	psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
250 251

	switch (rc) {
252 253
	case PSMOUSE_BAD_DATA:
		if (psmouse->state == PSMOUSE_ACTIVATED) {
254 255 256 257
			psmouse_warn(psmouse,
				     "%s at %s lost sync at byte %d\n",
				     psmouse->name, psmouse->phys,
				     psmouse->pktcnt);
258 259
			if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
				__psmouse_set_state(psmouse, PSMOUSE_IGNORE);
260 261
				psmouse_notice(psmouse,
						"issuing reconnect request\n");
262 263
				serio_reconnect(psmouse->ps2dev.serio);
				return -1;
264
			}
265 266 267 268 269 270 271 272
		}
		psmouse->pktcnt = 0;
		break;

	case PSMOUSE_FULL_PACKET:
		psmouse->pktcnt = 0;
		if (psmouse->out_of_sync_cnt) {
			psmouse->out_of_sync_cnt = 0;
273 274 275
			psmouse_notice(psmouse,
					"%s at %s - driver resynced.\n",
					psmouse->name, psmouse->phys);
276 277
		}
		break;
278

279 280
	case PSMOUSE_GOOD_DATA:
		break;
281 282 283 284 285 286 287
	}
	return 0;
}

/*
 * psmouse_interrupt() handles incoming characters, either passing them
 * for normal processing or gathering them as command response.
L
Linus Torvalds 已提交
288 289
 */
static irqreturn_t psmouse_interrupt(struct serio *serio,
290
		unsigned char data, unsigned int flags)
L
Linus Torvalds 已提交
291 292 293 294 295 296
{
	struct psmouse *psmouse = serio_get_drvdata(serio);

	if (psmouse->state == PSMOUSE_IGNORE)
		goto out;

297 298 299
	if (unlikely((flags & SERIO_TIMEOUT) ||
		     ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {

L
Linus Torvalds 已提交
300
		if (psmouse->state == PSMOUSE_ACTIVATED)
301 302 303 304
			psmouse_warn(psmouse,
				     "bad data from KBC -%s%s\n",
				     flags & SERIO_TIMEOUT ? " timeout" : "",
				     flags & SERIO_PARITY ? " bad parity" : "");
L
Linus Torvalds 已提交
305 306 307 308 309 310 311 312 313 314 315 316
		ps2_cmd_aborted(&psmouse->ps2dev);
		goto out;
	}

	if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
		if  (ps2_handle_ack(&psmouse->ps2dev, data))
			goto out;

	if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
		if  (ps2_handle_response(&psmouse->ps2dev, data))
			goto out;

317
	if (psmouse->state <= PSMOUSE_RESYNCING)
L
Linus Torvalds 已提交
318 319 320 321
		goto out;

	if (psmouse->state == PSMOUSE_ACTIVATED &&
	    psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
322 323
		psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
			     psmouse->name, psmouse->phys, psmouse->pktcnt);
324 325
		psmouse->badbyte = psmouse->packet[0];
		__psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
326
		psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
327
		goto out;
L
Linus Torvalds 已提交
328 329 330
	}

	psmouse->packet[psmouse->pktcnt++] = data;
331 332

	/* Check if this is a new device announcement (0xAA 0x00) */
333
	if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
334 335
		if (psmouse->pktcnt == 1) {
			psmouse->last = jiffies;
L
Linus Torvalds 已提交
336
			goto out;
337
		}
L
Linus Torvalds 已提交
338

339 340 341
		if (psmouse->packet[1] == PSMOUSE_RET_ID ||
		    (psmouse->type == PSMOUSE_HGPK &&
		     psmouse->packet[1] == PSMOUSE_RET_BAT)) {
342 343 344
			__psmouse_set_state(psmouse, PSMOUSE_IGNORE);
			serio_reconnect(serio);
			goto out;
L
Linus Torvalds 已提交
345
		}
346 347

		/* Not a new device, try processing first byte normally */
348
		psmouse->pktcnt = 1;
349
		if (psmouse_handle_byte(psmouse))
350
			goto out;
L
Linus Torvalds 已提交
351

352 353
		psmouse->packet[psmouse->pktcnt++] = data;
	}
L
Linus Torvalds 已提交
354

355 356 357 358
	/*
	 * See if we need to force resync because mouse was idle for
	 * too long.
	 */
359 360 361 362 363
	if (psmouse->state == PSMOUSE_ACTIVATED &&
	    psmouse->pktcnt == 1 && psmouse->resync_time &&
	    time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
		psmouse->badbyte = psmouse->packet[0];
		__psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
364
		psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
365 366
		goto out;
	}
L
Linus Torvalds 已提交
367

368
	psmouse->last = jiffies;
369
	psmouse_handle_byte(psmouse);
L
Linus Torvalds 已提交
370

371
 out:
L
Linus Torvalds 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	return IRQ_HANDLED;
}

/*
 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
 * using sliced syntax, understood by advanced devices, such as Logitech
 * or Synaptics touchpads. The command is encoded as:
 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
 * is the command.
 */
int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
{
	int i;

	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
		return -1;

	for (i = 6; i >= 0; i -= 2) {
		unsigned char d = (command >> i) & 3;
		if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
			return -1;
	}

	return 0;
}

/*
 * psmouse_reset() resets the mouse into power-on state.
 */
int psmouse_reset(struct psmouse *psmouse)
{
	unsigned char param[2];

	if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
		return -1;

	if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
		return -1;

	return 0;
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
/*
 * Here we set the mouse resolution.
 */
void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
{
	static const unsigned char params[] = { 0, 1, 2, 2, 3 };
	unsigned char p;

	if (resolution == 0 || resolution > 200)
		resolution = 200;

	p = params[resolution / 50];
	ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
	psmouse->resolution = 25 << p;
}

/*
 * Here we set the mouse report rate.
 */
static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
{
	static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
	unsigned char r;
	int i = 0;

	while (rates[i] > rate) i++;
	r = rates[i];
	ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
	psmouse->rate = r;
}

445 446 447 448 449 450 451 452 453 454
/*
 * Here we set the mouse scaling.
 */
static void psmouse_set_scale(struct psmouse *psmouse, enum psmouse_scale scale)
{
	ps2_command(&psmouse->ps2dev, NULL,
		    scale == PSMOUSE_SCALE21 ? PSMOUSE_CMD_SETSCALE21 :
					       PSMOUSE_CMD_SETSCALE11);
}

455 456 457 458 459 460 461 462 463
/*
 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
 */
static int psmouse_poll(struct psmouse *psmouse)
{
	return ps2_command(&psmouse->ps2dev, psmouse->packet,
			   PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
}

464 465 466 467 468 469 470 471 472 473 474
static bool psmouse_check_pnp_id(const char *id, const char * const ids[])
{
	int i;

	for (i = 0; ids[i]; i++)
		if (!strcasecmp(id, ids[i]))
			return true;

	return false;
}

475 476 477 478 479
/*
 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
 */
bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
{
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
	struct serio *serio = psmouse->ps2dev.serio;
	char *p, *fw_id_copy, *save_ptr;
	bool found = false;

	if (strncmp(serio->firmware_id, "PNP: ", 5))
		return false;

	fw_id_copy = kstrndup(&serio->firmware_id[5],
			      sizeof(serio->firmware_id) - 5,
			      GFP_KERNEL);
	if (!fw_id_copy)
		return false;

	save_ptr = fw_id_copy;
	while ((p = strsep(&fw_id_copy, " ")) != NULL) {
		if (psmouse_check_pnp_id(p, ids)) {
			found = true;
			break;
		}
	}
500

501 502
	kfree(save_ptr);
	return found;
503
}
L
Linus Torvalds 已提交
504 505 506 507

/*
 * Genius NetMouse magic init.
 */
508
static int genius_detect(struct psmouse *psmouse, bool set_properties)
L
Linus Torvalds 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	unsigned char param[4];

	param[0] = 3;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
	ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
	ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
	ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
	ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);

	if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
		return -1;

	if (set_properties) {
524
		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
525 526 527
		__set_bit(BTN_EXTRA, psmouse->dev->keybit);
		__set_bit(BTN_SIDE, psmouse->dev->keybit);
		__set_bit(REL_WHEEL, psmouse->dev->relbit);
L
Linus Torvalds 已提交
528 529

		psmouse->vendor = "Genius";
530
		psmouse->name = "Mouse";
L
Linus Torvalds 已提交
531 532 533 534 535 536 537 538 539
		psmouse->pktsize = 4;
	}

	return 0;
}

/*
 * IntelliMouse magic init.
 */
540
static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
L
Linus Torvalds 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	unsigned char param[2];

	param[0] = 200;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
	param[0] = 100;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
	param[0] =  80;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
	ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);

	if (param[0] != 3)
		return -1;

	if (set_properties) {
557 558
		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
		__set_bit(REL_WHEEL, psmouse->dev->relbit);
L
Linus Torvalds 已提交
559

560 561 562 563
		if (!psmouse->vendor)
			psmouse->vendor = "Generic";
		if (!psmouse->name)
			psmouse->name = "Wheel Mouse";
L
Linus Torvalds 已提交
564 565 566 567 568 569 570 571 572
		psmouse->pktsize = 4;
	}

	return 0;
}

/*
 * Try IntelliMouse/Explorer magic init.
 */
573
static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
L
Linus Torvalds 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	unsigned char param[2];

	intellimouse_detect(psmouse, 0);

	param[0] = 200;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
	param[0] = 200;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
	param[0] =  80;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
	ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);

	if (param[0] != 4)
		return -1;

591
	/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
592 593 594 595 596 597 598
	param[0] = 200;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
	param[0] =  80;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
	param[0] =  40;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);

L
Linus Torvalds 已提交
599
	if (set_properties) {
600 601 602 603 604
		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
		__set_bit(REL_WHEEL, psmouse->dev->relbit);
		__set_bit(REL_HWHEEL, psmouse->dev->relbit);
		__set_bit(BTN_SIDE, psmouse->dev->keybit);
		__set_bit(BTN_EXTRA, psmouse->dev->keybit);
L
Linus Torvalds 已提交
605

606 607 608 609
		if (!psmouse->vendor)
			psmouse->vendor = "Generic";
		if (!psmouse->name)
			psmouse->name = "Explorer Mouse";
L
Linus Torvalds 已提交
610 611 612 613 614 615 616 617 618
		psmouse->pktsize = 4;
	}

	return 0;
}

/*
 * Kensington ThinkingMouse / ExpertMouse magic init.
 */
619
static int thinking_detect(struct psmouse *psmouse, bool set_properties)
L
Linus Torvalds 已提交
620 621 622
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	unsigned char param[2];
H
Helge Deller 已提交
623
	static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
L
Linus Torvalds 已提交
624 625 626 627 628 629
	int i;

	param[0] = 10;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
	param[0] = 0;
	ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
H
Helge Deller 已提交
630 631 632 633
	for (i = 0; i < ARRAY_SIZE(seq); i++) {
		param[0] = seq[i];
		ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
	}
L
Linus Torvalds 已提交
634 635 636 637 638 639
	ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);

	if (param[0] != 2)
		return -1;

	if (set_properties) {
640
		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
641
		__set_bit(BTN_EXTRA, psmouse->dev->keybit);
L
Linus Torvalds 已提交
642 643 644 645 646 647 648 649 650 651 652

		psmouse->vendor = "Kensington";
		psmouse->name = "ThinkingMouse";
	}

	return 0;
}

/*
 * Bare PS/2 protocol "detection". Always succeeds.
 */
653
static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
L
Linus Torvalds 已提交
654
{
655
	if (set_properties) {
656 657 658 659 660
		if (!psmouse->vendor)
			psmouse->vendor = "Generic";
		if (!psmouse->name)
			psmouse->name = "Mouse";

661 662 663 664
		/*
		 * We have no way of figuring true number of buttons so let's
		 * assume that the device has 3.
		 */
665
		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
666
	}
L
Linus Torvalds 已提交
667 668 669 670

	return 0;
}

671 672 673 674
/*
 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
 * must be forced by sysfs protocol writing.
 */
675
static int cortron_detect(struct psmouse *psmouse, bool set_properties)
676 677 678 679
{
	if (set_properties) {
		psmouse->vendor = "Cortron";
		psmouse->name = "PS/2 Trackball";
680 681

		__set_bit(BTN_MIDDLE, psmouse->dev->keybit);
682
		__set_bit(BTN_SIDE, psmouse->dev->keybit);
683 684 685 686
	}

	return 0;
}
687

688 689 690 691 692 693 694 695
static const struct psmouse_protocol psmouse_protocols[] = {
	{
		.type		= PSMOUSE_PS2,
		.name		= "PS/2",
		.alias		= "bare",
		.maxproto	= true,
		.ignore_parity	= true,
		.detect		= ps2bare_detect,
696
		.try_passthru	= true,
697 698 699 700 701 702
	},
#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
	{
		.type		= PSMOUSE_PS2PP,
		.name		= "PS2++",
		.alias		= "logitech",
703
		.detect		= ps2pp_detect,
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	},
#endif
	{
		.type		= PSMOUSE_THINKPS,
		.name		= "ThinkPS/2",
		.alias		= "thinkps",
		.detect		= thinking_detect,
	},
#ifdef CONFIG_MOUSE_PS2_CYPRESS
	{
		.type		= PSMOUSE_CYPRESS,
		.name		= "CyPS/2",
		.alias		= "cypress",
		.detect		= cypress_detect,
		.init		= cypress_init,
	},
#endif
	{
		.type		= PSMOUSE_GENPS,
		.name		= "GenPS/2",
		.alias		= "genius",
		.detect		= genius_detect,
	},
	{
		.type		= PSMOUSE_IMPS,
		.name		= "ImPS/2",
		.alias		= "imps",
		.maxproto	= true,
		.ignore_parity	= true,
		.detect		= intellimouse_detect,
734
		.try_passthru	= true,
735 736 737 738 739 740 741 742
	},
	{
		.type		= PSMOUSE_IMEX,
		.name		= "ImExPS/2",
		.alias		= "exps",
		.maxproto	= true,
		.ignore_parity	= true,
		.detect		= im_explorer_detect,
743
		.try_passthru	= true,
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
	},
#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
	{
		.type		= PSMOUSE_SYNAPTICS,
		.name		= "SynPS/2",
		.alias		= "synaptics",
		.detect		= synaptics_detect,
		.init		= synaptics_init,
	},
	{
		.type		= PSMOUSE_SYNAPTICS_RELATIVE,
		.name		= "SynRelPS/2",
		.alias		= "synaptics-relative",
		.detect		= synaptics_detect,
		.init		= synaptics_init_relative,
	},
#endif
#ifdef CONFIG_MOUSE_PS2_ALPS
	{
		.type		= PSMOUSE_ALPS,
		.name		= "AlpsPS/2",
		.alias		= "alps",
		.detect		= alps_detect,
		.init		= alps_init,
	},
#endif
#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
	{
		.type		= PSMOUSE_LIFEBOOK,
		.name		= "LBPS/2",
		.alias		= "lifebook",
775
		.detect		= lifebook_detect,
776 777 778 779 780 781 782 783 784
		.init		= lifebook_init,
	},
#endif
#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
	{
		.type		= PSMOUSE_TRACKPOINT,
		.name		= "TPPS/2",
		.alias		= "trackpoint",
		.detect		= trackpoint_detect,
785
		.try_passthru	= true,
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
	},
#endif
#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
	{
		.type		= PSMOUSE_TOUCHKIT_PS2,
		.name		= "touchkitPS/2",
		.alias		= "touchkit",
		.detect		= touchkit_ps2_detect,
	},
#endif
#ifdef CONFIG_MOUSE_PS2_OLPC
	{
		.type		= PSMOUSE_HGPK,
		.name		= "OLPC HGPK",
		.alias		= "hgpk",
		.detect		= hgpk_detect,
	},
#endif
#ifdef CONFIG_MOUSE_PS2_ELANTECH
	{
		.type		= PSMOUSE_ELANTECH,
		.name		= "ETPS/2",
		.alias		= "elantech",
		.detect		= elantech_detect,
		.init		= elantech_init,
	},
#endif
#ifdef CONFIG_MOUSE_PS2_SENTELIC
	{
		.type		= PSMOUSE_FSP,
		.name		= "FSPPS/2",
		.alias		= "fsp",
		.detect		= fsp_detect,
		.init		= fsp_init,
	},
#endif
	{
		.type		= PSMOUSE_CORTRON,
		.name		= "CortronPS/2",
		.alias		= "cortps",
		.detect		= cortron_detect,
	},
#ifdef CONFIG_MOUSE_PS2_FOCALTECH
	{
		.type		= PSMOUSE_FOCALTECH,
		.name		= "FocalTechPS/2",
		.alias		= "focaltech",
		.detect		= focaltech_detect,
		.init		= focaltech_init,
	},
#endif
#ifdef CONFIG_MOUSE_PS2_VMMOUSE
	{
		.type		= PSMOUSE_VMMOUSE,
		.name		= VMMOUSE_PSNAME,
		.alias		= "vmmouse",
		.detect		= vmmouse_detect,
		.init		= vmmouse_init,
	},
845 846 847 848 849 850 851 852 853
#endif
#ifdef CONFIG_MOUSE_PS2_BYD
	{
		.type		= PSMOUSE_BYD,
		.name		= "BydPS/2",
		.alias		= "byd",
		.detect		= byd_detect,
		.init		= byd_init,
	},
854 855 856 857 858 859 860 861 862
#endif
	{
		.type		= PSMOUSE_AUTO,
		.name		= "auto",
		.alias		= "any",
		.maxproto	= true,
	},
};

863
static const struct psmouse_protocol *__psmouse_protocol_by_type(enum psmouse_type type)
864 865 866 867 868 869 870
{
	int i;

	for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
		if (psmouse_protocols[i].type == type)
			return &psmouse_protocols[i];

871 872 873 874 875 876 877 878 879 880 881
	return NULL;
}

static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
{
	const struct psmouse_protocol *proto;

	proto = __psmouse_protocol_by_type(type);
	if (proto)
		return proto;

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
	WARN_ON(1);
	return &psmouse_protocols[0];
}

static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
{
	const struct psmouse_protocol *p;
	int i;

	for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
		p = &psmouse_protocols[i];

		if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
		    (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
			return &psmouse_protocols[i];
	}

	return NULL;
}

902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
/*
 * Apply default settings to the psmouse structure. Most of them will
 * be overridden by individual protocol initialization routines.
 */
static void psmouse_apply_defaults(struct psmouse *psmouse)
{
	struct input_dev *input_dev = psmouse->dev;

	memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
	memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
	memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
	memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
	memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));

	__set_bit(EV_KEY, input_dev->evbit);
	__set_bit(EV_REL, input_dev->evbit);

	__set_bit(BTN_LEFT, input_dev->keybit);
	__set_bit(BTN_RIGHT, input_dev->keybit);

	__set_bit(REL_X, input_dev->relbit);
	__set_bit(REL_Y, input_dev->relbit);

925 926
	__set_bit(INPUT_PROP_POINTER, input_dev->propbit);

927 928
	psmouse->set_rate = psmouse_set_rate;
	psmouse->set_resolution = psmouse_set_resolution;
929
	psmouse->set_scale = psmouse_set_scale;
930 931 932 933 934 935 936 937 938 939
	psmouse->poll = psmouse_poll;
	psmouse->protocol_handler = psmouse_process_byte;
	psmouse->pktsize = 3;
	psmouse->reconnect = NULL;
	psmouse->disconnect = NULL;
	psmouse->cleanup = NULL;
	psmouse->pt_activate = NULL;
	psmouse->pt_deactivate = NULL;
}

940 941 942 943
static bool psmouse_try_protocol(struct psmouse *psmouse,
				 enum psmouse_type type,
				 unsigned int *max_proto,
				 bool set_properties, bool init_allowed)
944
{
945 946 947 948 949 950
	const struct psmouse_protocol *proto;

	proto = __psmouse_protocol_by_type(type);
	if (!proto)
		return false;

951 952 953 954 955
	if (psmouse->ps2dev.serio->id.type == SERIO_PS_PSTHRU &&
	    !proto->try_passthru) {
		return false;
	}

956 957 958
	if (set_properties)
		psmouse_apply_defaults(psmouse);

959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
	if (proto->detect(psmouse, set_properties) != 0)
		return false;

	if (set_properties && proto->init && init_allowed) {
		if (proto->init(psmouse) != 0) {
			/*
			 * We detected device, but init failed. Adjust
			 * max_proto so we only try standard protocols.
			 */
			if (*max_proto > PSMOUSE_IMEX)
				*max_proto = PSMOUSE_IMEX;

			return false;
		}
	}

	return true;
976 977
}

L
Linus Torvalds 已提交
978 979 980 981 982
/*
 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
 * the mouse may have.
 */
static int psmouse_extensions(struct psmouse *psmouse,
983
			      unsigned int max_proto, bool set_properties)
L
Linus Torvalds 已提交
984
{
985
	bool synaptics_hardware = false;
L
Linus Torvalds 已提交
986

987 988 989 990
	/*
	 * Always check for focaltech, this is safe as it uses pnp-id
	 * matching.
	 */
991 992 993 994 995 996
	if (psmouse_try_protocol(psmouse, PSMOUSE_FOCALTECH,
				 &max_proto, set_properties, false)) {
		if (max_proto > PSMOUSE_IMEX &&
		    IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH) &&
		    (!set_properties || focaltech_init(psmouse) == 0)) {
			return PSMOUSE_FOCALTECH;
997
		}
998 999 1000 1001 1002 1003 1004 1005 1006
		/*
		 * Restrict psmouse_max_proto so that psmouse_initialize()
		 * does not try to reset rate and resolution, because even
		 * that upsets the device.
		 * This also causes us to basically fall through to basic
		 * protocol detection, where we fully reset the mouse,
		 * and set it up as bare PS/2 protocol device.
		 */
		psmouse_max_proto = max_proto = PSMOUSE_PS2;
1007 1008
	}

1009 1010 1011 1012
	/*
	 * We always check for LifeBook because it does not disturb mouse
	 * (it only checks DMI information).
	 */
1013 1014 1015
	if (psmouse_try_protocol(psmouse, PSMOUSE_LIFEBOOK, &max_proto,
				 set_properties, max_proto > PSMOUSE_IMEX))
		return PSMOUSE_LIFEBOOK;
1016

1017 1018 1019
	if (psmouse_try_protocol(psmouse, PSMOUSE_VMMOUSE, &max_proto,
				 set_properties, max_proto > PSMOUSE_IMEX))
		return PSMOUSE_VMMOUSE;
T
Thomas Hellstrom 已提交
1020

1021 1022 1023 1024
	/*
	 * Try Kensington ThinkingMouse (we try first, because Synaptics
	 * probe upsets the ThinkingMouse).
	 */
1025
	if (max_proto > PSMOUSE_IMEX &&
1026 1027
	    psmouse_try_protocol(psmouse, PSMOUSE_THINKPS, &max_proto,
				 set_properties, true)) {
L
Linus Torvalds 已提交
1028
		return PSMOUSE_THINKPS;
1029
	}
L
Linus Torvalds 已提交
1030

1031 1032 1033 1034 1035 1036
	/*
	 * Try Synaptics TouchPad. Note that probing is done even if
	 * Synaptics protocol support is disabled in config - we need to
	 * know if it is Synaptics so we can reset it properly after
	 * probing for IntelliMouse.
	 */
1037
	if (max_proto > PSMOUSE_PS2 &&
1038 1039
	    psmouse_try_protocol(psmouse, PSMOUSE_SYNAPTICS, &max_proto,
				 set_properties, false)) {
1040
		synaptics_hardware = true;
L
Linus Torvalds 已提交
1041 1042

		if (max_proto > PSMOUSE_IMEX) {
1043 1044 1045 1046 1047
			/*
			 * Try activating protocol, but check if support is
			 * enabled first, since we try detecting Synaptics
			 * even when protocol is disabled.
			 */
1048
			if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) &&
1049
			    (!set_properties || synaptics_init(psmouse) == 0)) {
L
Linus Torvalds 已提交
1050
				return PSMOUSE_SYNAPTICS;
1051 1052
			}

1053 1054 1055 1056 1057 1058
			/*
			 * Some Synaptics touchpads can emulate extended
			 * protocols (like IMPS/2).  Unfortunately
			 * Logitech/Genius probes confuse some firmware
			 * versions so we'll have to skip them.
			 */
L
Linus Torvalds 已提交
1059 1060
			max_proto = PSMOUSE_IMEX;
		}
1061 1062 1063 1064 1065

		/*
		 * Make sure that touchpad is in relative mode, gestures
		 * (taps) are enabled.
		 */
L
Linus Torvalds 已提交
1066 1067 1068
		synaptics_reset(psmouse);
	}

1069 1070 1071 1072 1073
	/*
	 * Try Cypress Trackpad. We must try it before Finger Sensing Pad
	 * because Finger Sensing Pad probe upsets some modules of Cypress
	 * Trackpads.
	 */
1074
	if (max_proto > PSMOUSE_IMEX &&
1075 1076 1077
	    psmouse_try_protocol(psmouse, PSMOUSE_CYPRESS, &max_proto,
				 set_properties, true)) {
		return PSMOUSE_CYPRESS;
1078 1079
	}

1080
	/* Try ALPS TouchPad */
L
Linus Torvalds 已提交
1081 1082
	if (max_proto > PSMOUSE_IMEX) {
		ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1083 1084 1085
		if (psmouse_try_protocol(psmouse, PSMOUSE_ALPS,
					 &max_proto, set_properties, true))
			return PSMOUSE_ALPS;
L
Linus Torvalds 已提交
1086 1087
	}

1088
	/* Try OLPC HGPK touchpad */
1089
	if (max_proto > PSMOUSE_IMEX &&
1090 1091 1092
	    psmouse_try_protocol(psmouse, PSMOUSE_HGPK, &max_proto,
				 set_properties, true)) {
		return PSMOUSE_HGPK;
1093
	}
1094

1095
	/* Try Elantech touchpad */
1096
	if (max_proto > PSMOUSE_IMEX &&
1097 1098 1099
	    psmouse_try_protocol(psmouse, PSMOUSE_ELANTECH,
				 &max_proto, set_properties, true)) {
		return PSMOUSE_ELANTECH;
1100 1101
	}

1102
	if (max_proto > PSMOUSE_IMEX) {
1103 1104
		if (psmouse_try_protocol(psmouse, PSMOUSE_GENPS,
					 &max_proto, set_properties, true))
1105 1106
			return PSMOUSE_GENPS;

1107 1108
		if (psmouse_try_protocol(psmouse, PSMOUSE_PS2PP,
					 &max_proto, set_properties, true))
1109
			return PSMOUSE_PS2PP;
L
Linus Torvalds 已提交
1110

1111 1112
		if (psmouse_try_protocol(psmouse, PSMOUSE_TRACKPOINT,
					 &max_proto, set_properties, true))
1113
			return PSMOUSE_TRACKPOINT;
L
Linus Torvalds 已提交
1114

1115 1116
		if (psmouse_try_protocol(psmouse, PSMOUSE_TOUCHKIT_PS2,
					 &max_proto, set_properties, true))
1117
			return PSMOUSE_TOUCHKIT_PS2;
1118 1119 1120 1121

		if (psmouse_try_protocol(psmouse, PSMOUSE_BYD,
					 &max_proto, set_properties, true))
			return PSMOUSE_BYD;
1122
	}
1123

1124 1125 1126 1127
	/*
	 * Try Finger Sensing Pad. We do it here because its probe upsets
	 * Trackpoint devices (causing TP_READ_ID command to time out).
	 */
1128 1129 1130 1131
	if (max_proto > PSMOUSE_IMEX &&
	    psmouse_try_protocol(psmouse, PSMOUSE_FSP,
				 &max_proto, set_properties, true)) {
		return PSMOUSE_FSP;
1132 1133
	}

1134 1135 1136 1137 1138
	/*
	 * Reset to defaults in case the device got confused by extended
	 * protocol probes. Note that we follow up with full reset because
	 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
	 */
1139
	ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1140
	psmouse_reset(psmouse);
L
Linus Torvalds 已提交
1141

1142
	if (max_proto >= PSMOUSE_IMEX &&
1143 1144
	    psmouse_try_protocol(psmouse, PSMOUSE_IMEX,
				 &max_proto, set_properties, true)) {
L
Linus Torvalds 已提交
1145
		return PSMOUSE_IMEX;
1146
	}
L
Linus Torvalds 已提交
1147

1148
	if (max_proto >= PSMOUSE_IMPS &&
1149 1150
	    psmouse_try_protocol(psmouse, PSMOUSE_IMPS,
				 &max_proto, set_properties, true)) {
L
Linus Torvalds 已提交
1151
		return PSMOUSE_IMPS;
1152
	}
L
Linus Torvalds 已提交
1153

1154 1155 1156 1157
	/*
	 * Okay, all failed, we have a standard mouse here. The number of
	 * the buttons is still a question, though. We assume 3.
	 */
1158 1159
	psmouse_try_protocol(psmouse, PSMOUSE_PS2,
			     &max_proto, set_properties, true);
L
Linus Torvalds 已提交
1160 1161

	if (synaptics_hardware) {
1162 1163 1164 1165 1166 1167
		/*
		 * We detected Synaptics hardware but it did not respond to
		 * IMPS/2 probes.  We need to reset the touchpad because if
		 * there is a track point on the pass through port it could
		 * get disabled while probing for protocol extensions.
		 */
L
Linus Torvalds 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
		psmouse_reset(psmouse);
	}

	return PSMOUSE_PS2;
}

/*
 * psmouse_probe() probes for a PS/2 mouse.
 */
static int psmouse_probe(struct psmouse *psmouse)
{
	struct ps2dev *ps2dev = &psmouse->ps2dev;
	unsigned char param[2];

1182 1183 1184 1185 1186 1187
	/*
	 * First, we check if it's a mouse. It should send 0x00 or 0x03 in
	 * case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
	 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and
	 * subsequent ID queries, probably due to a firmware bug.
	 */
L
Linus Torvalds 已提交
1188 1189 1190 1191
	param[0] = 0xa5;
	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
		return -1;

1192 1193
	if (param[0] != 0x00 && param[0] != 0x03 &&
	    param[0] != 0x04 && param[0] != 0xff)
L
Linus Torvalds 已提交
1194 1195
		return -1;

1196 1197 1198 1199
	/*
	 * Then we reset and disable the mouse so that it doesn't generate
	 * events.
	 */
L
Linus Torvalds 已提交
1200
	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
1201 1202
		psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
			     ps2dev->serio->phys);
L
Linus Torvalds 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211

	return 0;
}

/*
 * psmouse_initialize() initializes the mouse to a sane state.
 */
static void psmouse_initialize(struct psmouse *psmouse)
{
1212 1213 1214
	/*
	 * We set the mouse report rate, resolution and scaling.
	 */
L
Linus Torvalds 已提交
1215 1216 1217
	if (psmouse_max_proto != PSMOUSE_PS2) {
		psmouse->set_rate(psmouse, psmouse->rate);
		psmouse->set_resolution(psmouse, psmouse->resolution);
1218
		psmouse->set_scale(psmouse, PSMOUSE_SCALE11);
L
Linus Torvalds 已提交
1219 1220 1221 1222 1223 1224
	}
}

/*
 * psmouse_activate() enables the mouse so that we get motion reports from it.
 */
1225
int psmouse_activate(struct psmouse *psmouse)
L
Linus Torvalds 已提交
1226
{
1227
	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1228 1229
		psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
			     psmouse->ps2dev.serio->phys);
1230 1231
		return -1;
	}
L
Linus Torvalds 已提交
1232 1233

	psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1234
	return 0;
L
Linus Torvalds 已提交
1235 1236 1237
}

/*
1238 1239
 * psmouse_deactivate() puts the mouse into poll mode so that we don't get
 * motion reports from it unless we explicitly request it.
L
Linus Torvalds 已提交
1240
 */
1241
int psmouse_deactivate(struct psmouse *psmouse)
L
Linus Torvalds 已提交
1242
{
1243
	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) {
1244 1245
		psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
			     psmouse->ps2dev.serio->phys);
1246 1247
		return -1;
	}
L
Linus Torvalds 已提交
1248 1249

	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1250
	return 0;
L
Linus Torvalds 已提交
1251 1252
}

1253 1254 1255
/*
 * psmouse_resync() attempts to re-validate current protocol.
 */
D
David Howells 已提交
1256
static void psmouse_resync(struct work_struct *work)
1257
{
D
David Howells 已提交
1258
	struct psmouse *parent = NULL, *psmouse =
1259
		container_of(work, struct psmouse, resync_work.work);
1260 1261
	struct serio *serio = psmouse->ps2dev.serio;
	psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1262
	bool failed = false, enabled = false;
1263 1264
	int i;

1265
	mutex_lock(&psmouse_mutex);
1266 1267 1268 1269 1270 1271 1272 1273 1274

	if (psmouse->state != PSMOUSE_RESYNCING)
		goto out;

	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
		parent = serio_get_drvdata(serio->parent);
		psmouse_deactivate(parent);
	}

1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
	/*
	 * Some mice don't ACK commands sent while they are in the middle of
	 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
	 * instead of ps2_command() which would wait for 200ms for an ACK
	 * that may never come.
	 * As an additional quirk ALPS touchpads may not only forget to ACK
	 * disable command but will stop reporting taps, so if we see that
	 * mouse at least once ACKs disable we will do full reconnect if ACK
	 * is missing.
	 */
1285 1286 1287 1288
	psmouse->num_resyncs++;

	if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
		if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1289
			failed = true;
1290
	} else
1291
		psmouse->acks_disable_command = true;
1292

1293 1294 1295 1296 1297 1298 1299
	/*
	 * Poll the mouse. If it was reset the packet will be shorter than
	 * psmouse->pktsize and ps2_command will fail. We do not expect and
	 * do not handle scenario when mouse "upgrades" its protocol while
	 * disconnected since it would require additional delay. If we ever
	 * see a mouse that does it we'll adjust the code.
	 */
1300 1301
	if (!failed) {
		if (psmouse->poll(psmouse))
1302
			failed = true;
1303 1304 1305 1306
		else {
			psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
			for (i = 0; i < psmouse->pktsize; i++) {
				psmouse->pktcnt++;
1307
				rc = psmouse->protocol_handler(psmouse);
1308 1309 1310 1311
				if (rc != PSMOUSE_GOOD_DATA)
					break;
			}
			if (rc != PSMOUSE_FULL_PACKET)
1312
				failed = true;
1313 1314 1315
			psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
		}
	}
1316 1317 1318 1319 1320 1321

	/*
	 * Now try to enable mouse. We try to do that even if poll failed
	 * and also repeat our attempts 5 times, otherwise we may be left
	 * out with disabled mouse.
	 */
1322 1323
	for (i = 0; i < 5; i++) {
		if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1324
			enabled = true;
1325 1326 1327 1328 1329 1330
			break;
		}
		msleep(200);
	}

	if (!enabled) {
1331 1332
		psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
			     psmouse->ps2dev.serio->phys);
1333
		failed = true;
1334 1335 1336 1337
	}

	if (failed) {
		psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1338 1339
		psmouse_info(psmouse,
			     "resync failed, issuing reconnect request\n");
1340 1341 1342 1343 1344 1345 1346
		serio_reconnect(serio);
	} else
		psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);

	if (parent)
		psmouse_activate(parent);
 out:
1347
	mutex_unlock(&psmouse_mutex);
1348
}
L
Linus Torvalds 已提交
1349 1350 1351 1352 1353 1354 1355

/*
 * psmouse_cleanup() resets the mouse into power-on state.
 */
static void psmouse_cleanup(struct serio *serio)
{
	struct psmouse *psmouse = serio_get_drvdata(serio);
1356 1357 1358 1359 1360 1361 1362 1363 1364
	struct psmouse *parent = NULL;

	mutex_lock(&psmouse_mutex);

	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
		parent = serio_get_drvdata(serio->parent);
		psmouse_deactivate(parent);
	}

1365 1366 1367 1368 1369 1370
	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);

	/*
	 * Disable stream mode so cleanup routine can proceed undisturbed.
	 */
	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1371 1372
		psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
			     psmouse->ps2dev.serio->phys);
1373 1374 1375

	if (psmouse->cleanup)
		psmouse->cleanup(psmouse);
L
Linus Torvalds 已提交
1376

1377 1378 1379
	/*
	 * Reset the mouse to defaults (bare PS/2 protocol).
	 */
1380
	ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1381

1382 1383 1384 1385
	/*
	 * Some boxes, such as HP nx7400, get terribly confused if mouse
	 * is not fully enabled before suspending/shutting down.
	 */
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
	ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);

	if (parent) {
		if (parent->pt_deactivate)
			parent->pt_deactivate(parent);

		psmouse_activate(parent);
	}

	mutex_unlock(&psmouse_mutex);
L
Linus Torvalds 已提交
1396 1397 1398 1399 1400 1401 1402
}

/*
 * psmouse_disconnect() closes and frees.
 */
static void psmouse_disconnect(struct serio *serio)
{
1403 1404 1405
	struct psmouse *psmouse, *parent = NULL;

	psmouse = serio_get_drvdata(serio);
L
Linus Torvalds 已提交
1406

1407
	sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
L
Linus Torvalds 已提交
1408

1409
	mutex_lock(&psmouse_mutex);
1410

L
Linus Torvalds 已提交
1411 1412
	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);

1413
	/* make sure we don't have a resync in progress */
1414
	mutex_unlock(&psmouse_mutex);
1415
	flush_workqueue(kpsmoused_wq);
1416
	mutex_lock(&psmouse_mutex);
1417

L
Linus Torvalds 已提交
1418 1419
	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
		parent = serio_get_drvdata(serio->parent);
1420
		psmouse_deactivate(parent);
L
Linus Torvalds 已提交
1421 1422 1423 1424 1425
	}

	if (psmouse->disconnect)
		psmouse->disconnect(psmouse);

1426 1427 1428
	if (parent && parent->pt_deactivate)
		parent->pt_deactivate(parent);

L
Linus Torvalds 已提交
1429 1430 1431 1432
	psmouse_set_state(psmouse, PSMOUSE_IGNORE);

	serio_close(serio);
	serio_set_drvdata(serio, NULL);
1433
	input_unregister_device(psmouse->dev);
L
Linus Torvalds 已提交
1434
	kfree(psmouse);
1435 1436 1437 1438

	if (parent)
		psmouse_activate(parent);

1439
	mutex_unlock(&psmouse_mutex);
L
Linus Torvalds 已提交
1440 1441
}

1442 1443
static int psmouse_switch_protocol(struct psmouse *psmouse,
				   const struct psmouse_protocol *proto)
1444
{
1445
	const struct psmouse_protocol *selected_proto;
1446
	struct input_dev *input_dev = psmouse->dev;
1447

1448
	input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1449 1450

	if (proto && (proto->detect || proto->init)) {
1451 1452
		psmouse_apply_defaults(psmouse);

1453
		if (proto->detect && proto->detect(psmouse, true) < 0)
1454 1455 1456 1457 1458 1459
			return -1;

		if (proto->init && proto->init(psmouse) < 0)
			return -1;

		psmouse->type = proto->type;
1460 1461
		selected_proto = proto;
	} else {
1462 1463
		psmouse->type = psmouse_extensions(psmouse,
						   psmouse_max_proto, true);
1464 1465 1466 1467
		selected_proto = psmouse_protocol_by_type(psmouse->type);
	}

	psmouse->ignore_parity = selected_proto->ignore_parity;
1468

1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
	/*
	 * If mouse's packet size is 3 there is no point in polling the
	 * device in hopes to detect protocol reset - we won't get less
	 * than 3 bytes response anyhow.
	 */
	if (psmouse->pktsize == 3)
		psmouse->resync_time = 0;

	/*
	 * Some smart KVMs fake response to POLL command returning just
	 * 3 bytes and messing up our resync logic, so if initial poll
	 * fails we won't try polling the device anymore. Hopefully
	 * such KVM will maintain initially selected protocol.
	 */
	if (psmouse->resync_time && psmouse->poll(psmouse))
		psmouse->resync_time = 0;

1486
	snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1487
		 selected_proto->name, psmouse->vendor, psmouse->name);
1488

1489 1490 1491 1492 1493 1494
	input_dev->name = psmouse->devname;
	input_dev->phys = psmouse->phys;
	input_dev->id.bustype = BUS_I8042;
	input_dev->id.vendor = 0x0002;
	input_dev->id.product = psmouse->type;
	input_dev->id.version = psmouse->model;
1495 1496 1497 1498

	return 0;
}

L
Linus Torvalds 已提交
1499 1500 1501 1502 1503 1504 1505
/*
 * psmouse_connect() is a callback from the serio module when
 * an unhandled serio port is found.
 */
static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
{
	struct psmouse *psmouse, *parent = NULL;
1506
	struct input_dev *input_dev;
1507
	int retval = 0, error = -ENOMEM;
L
Linus Torvalds 已提交
1508

1509
	mutex_lock(&psmouse_mutex);
1510

L
Linus Torvalds 已提交
1511 1512 1513 1514 1515 1516 1517 1518 1519
	/*
	 * If this is a pass-through port deactivate parent so the device
	 * connected to this port can be successfully identified
	 */
	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
		parent = serio_get_drvdata(serio->parent);
		psmouse_deactivate(parent);
	}

1520 1521 1522
	psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!psmouse || !input_dev)
1523
		goto err_free;
L
Linus Torvalds 已提交
1524 1525

	ps2_init(&psmouse->ps2dev, serio);
1526
	INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1527
	psmouse->dev = input_dev;
1528
	snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1529

L
Linus Torvalds 已提交
1530 1531 1532 1533
	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);

	serio_set_drvdata(serio, psmouse);

1534 1535 1536
	error = serio_open(serio, drv);
	if (error)
		goto err_clear_drvdata;
L
Linus Torvalds 已提交
1537

1538 1539 1540 1541
	/* give PT device some time to settle down before probing */
	if (serio->id.type == SERIO_PS_PSTHRU)
		usleep_range(10000, 15000);

L
Linus Torvalds 已提交
1542
	if (psmouse_probe(psmouse) < 0) {
1543 1544
		error = -ENODEV;
		goto err_close_serio;
L
Linus Torvalds 已提交
1545 1546 1547 1548 1549
	}

	psmouse->rate = psmouse_rate;
	psmouse->resolution = psmouse_resolution;
	psmouse->resetafter = psmouse_resetafter;
1550
	psmouse->resync_time = parent ? 0 : psmouse_resync_time;
L
Linus Torvalds 已提交
1551 1552
	psmouse->smartscroll = psmouse_smartscroll;

1553
	psmouse_switch_protocol(psmouse, NULL);
L
Linus Torvalds 已提交
1554 1555 1556 1557

	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
	psmouse_initialize(psmouse);

1558 1559 1560
	error = input_register_device(psmouse->dev);
	if (error)
		goto err_protocol_disconnect;
1561

L
Linus Torvalds 已提交
1562 1563 1564
	if (parent && parent->pt_activate)
		parent->pt_activate(parent);

1565 1566 1567
	error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
	if (error)
		goto err_pt_deactivate;
L
Linus Torvalds 已提交
1568 1569 1570

	psmouse_activate(psmouse);

1571
 out:
1572
	/* If this is a pass-through port the parent needs to be re-activated */
L
Linus Torvalds 已提交
1573 1574 1575
	if (parent)
		psmouse_activate(parent);

1576
	mutex_unlock(&psmouse_mutex);
L
Linus Torvalds 已提交
1577
	return retval;
1578 1579 1580 1581

 err_pt_deactivate:
	if (parent && parent->pt_deactivate)
		parent->pt_deactivate(parent);
1582 1583
	input_unregister_device(psmouse->dev);
	input_dev = NULL; /* so we don't try to free it below */
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
 err_protocol_disconnect:
	if (psmouse->disconnect)
		psmouse->disconnect(psmouse);
	psmouse_set_state(psmouse, PSMOUSE_IGNORE);
 err_close_serio:
	serio_close(serio);
 err_clear_drvdata:
	serio_set_drvdata(serio, NULL);
 err_free:
	input_free_device(input_dev);
	kfree(psmouse);

	retval = error;
	goto out;
L
Linus Torvalds 已提交
1598 1599 1600 1601 1602 1603
}

static int psmouse_reconnect(struct serio *serio)
{
	struct psmouse *psmouse = serio_get_drvdata(serio);
	struct psmouse *parent = NULL;
1604
	unsigned char type;
L
Linus Torvalds 已提交
1605 1606
	int rc = -1;

1607
	mutex_lock(&psmouse_mutex);
1608

L
Linus Torvalds 已提交
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
		parent = serio_get_drvdata(serio->parent);
		psmouse_deactivate(parent);
	}

	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);

	if (psmouse->reconnect) {
		if (psmouse->reconnect(psmouse))
			goto out;
1619 1620 1621 1622 1623 1624 1625 1626 1627
	} else {
		psmouse_reset(psmouse);

		if (psmouse_probe(psmouse) < 0)
			goto out;

		type = psmouse_extensions(psmouse, psmouse_max_proto, false);
		if (psmouse->type != type)
			goto out;
1628
	}
L
Linus Torvalds 已提交
1629

1630 1631 1632
	/*
	 * OK, the device type (and capabilities) match the old one,
	 * we can continue using it, complete initialization
L
Linus Torvalds 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
	 */
	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);

	psmouse_initialize(psmouse);

	if (parent && parent->pt_activate)
		parent->pt_activate(parent);

	psmouse_activate(psmouse);
	rc = 0;

out:
	/* If this is a pass-through port the parent waits to be activated */
	if (parent)
		psmouse_activate(parent);

1649
	mutex_unlock(&psmouse_mutex);
L
Linus Torvalds 已提交
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
	return rc;
}

static struct serio_device_id psmouse_serio_ids[] = {
	{
		.type	= SERIO_8042,
		.proto	= SERIO_ANY,
		.id	= SERIO_ANY,
		.extra	= SERIO_ANY,
	},
	{
		.type	= SERIO_PS_PSTHRU,
		.proto	= SERIO_ANY,
		.id	= SERIO_ANY,
		.extra	= SERIO_ANY,
	},
	{ 0 }
};

MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);

static struct serio_driver psmouse_drv = {
	.driver		= {
		.name	= "psmouse",
	},
	.description	= DRIVER_DESC,
	.id_table	= psmouse_serio_ids,
	.interrupt	= psmouse_interrupt,
	.connect	= psmouse_connect,
	.reconnect	= psmouse_reconnect,
	.disconnect	= psmouse_disconnect,
	.cleanup	= psmouse_cleanup,
};

1684 1685
ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
				 char *buf)
L
Linus Torvalds 已提交
1686 1687
{
	struct serio *serio = to_serio_port(dev);
1688 1689
	struct psmouse_attribute *attr = to_psmouse_attr(devattr);
	struct psmouse *psmouse;
L
Linus Torvalds 已提交
1690

1691 1692
	psmouse = serio_get_drvdata(serio);

1693
	return attr->show(psmouse, attr->data, buf);
L
Linus Torvalds 已提交
1694 1695
}

1696 1697
ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
				const char *buf, size_t count)
L
Linus Torvalds 已提交
1698 1699
{
	struct serio *serio = to_serio_port(dev);
1700 1701
	struct psmouse_attribute *attr = to_psmouse_attr(devattr);
	struct psmouse *psmouse, *parent = NULL;
L
Linus Torvalds 已提交
1702 1703
	int retval;

1704
	retval = mutex_lock_interruptible(&psmouse_mutex);
1705
	if (retval)
1706
		goto out;
1707

1708 1709
	psmouse = serio_get_drvdata(serio);

1710 1711 1712 1713 1714
	if (attr->protect) {
		if (psmouse->state == PSMOUSE_IGNORE) {
			retval = -ENODEV;
			goto out_unlock;
		}
L
Linus Torvalds 已提交
1715

1716 1717 1718 1719
		if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
			parent = serio_get_drvdata(serio->parent);
			psmouse_deactivate(parent);
		}
1720

1721 1722
		psmouse_deactivate(psmouse);
	}
L
Linus Torvalds 已提交
1723

1724
	retval = attr->set(psmouse, attr->data, buf, count);
L
Linus Torvalds 已提交
1725

1726 1727 1728
	if (attr->protect) {
		if (retval != -ENODEV)
			psmouse_activate(psmouse);
1729

1730 1731 1732
		if (parent)
			psmouse_activate(parent);
	}
L
Linus Torvalds 已提交
1733

1734 1735
 out_unlock:
	mutex_unlock(&psmouse_mutex);
1736
 out:
L
Linus Torvalds 已提交
1737 1738 1739
	return retval;
}

1740 1741
static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
{
1742
	unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1743

1744
	return sprintf(buf, "%u\n", *field);
1745 1746 1747 1748
}

static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
{
1749
	unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1750 1751
	unsigned int value;
	int err;
1752

1753 1754 1755
	err = kstrtouint(buf, 10, &value);
	if (err)
		return err;
1756

1757 1758 1759 1760 1761 1762
	*field = value;

	return count;
}

static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1763 1764 1765 1766
{
	return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
}

1767
static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1768 1769 1770
{
	struct serio *serio = psmouse->ps2dev.serio;
	struct psmouse *parent = NULL;
1771 1772 1773
	struct input_dev *old_dev, *new_dev;
	const struct psmouse_protocol *proto, *old_proto;
	int error;
1774 1775
	int retry = 0;

1776 1777
	proto = psmouse_protocol_by_name(buf, count);
	if (!proto)
1778 1779 1780 1781 1782
		return -EINVAL;

	if (psmouse->type == proto->type)
		return count;

1783 1784
	new_dev = input_allocate_device();
	if (!new_dev)
1785 1786
		return -ENOMEM;

1787
	while (!list_empty(&serio->children)) {
1788
		if (++retry > 3) {
1789 1790
			psmouse_warn(psmouse,
				     "failed to destroy children ports, protocol change aborted.\n");
1791
			input_free_device(new_dev);
1792 1793 1794
			return -EIO;
		}

1795
		mutex_unlock(&psmouse_mutex);
1796
		serio_unregister_child_port(serio);
1797
		mutex_lock(&psmouse_mutex);
1798

1799 1800
		if (serio->drv != &psmouse_drv) {
			input_free_device(new_dev);
1801
			return -ENODEV;
1802
		}
1803

1804 1805
		if (psmouse->type == proto->type) {
			input_free_device(new_dev);
1806
			return count; /* switched by other thread */
1807
		}
1808 1809 1810 1811 1812 1813 1814 1815
	}

	if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
		parent = serio_get_drvdata(serio->parent);
		if (parent->pt_deactivate)
			parent->pt_deactivate(parent);
	}

1816 1817 1818
	old_dev = psmouse->dev;
	old_proto = psmouse_protocol_by_type(psmouse->type);

1819 1820 1821 1822 1823
	if (psmouse->disconnect)
		psmouse->disconnect(psmouse);

	psmouse_set_state(psmouse, PSMOUSE_IGNORE);

1824
	psmouse->dev = new_dev;
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);

	if (psmouse_switch_protocol(psmouse, proto) < 0) {
		psmouse_reset(psmouse);
		/* default to PSMOUSE_PS2 */
		psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
	}

	psmouse_initialize(psmouse);
	psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);

1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
	error = input_register_device(psmouse->dev);
	if (error) {
		if (psmouse->disconnect)
			psmouse->disconnect(psmouse);

		psmouse_set_state(psmouse, PSMOUSE_IGNORE);
		input_free_device(new_dev);
		psmouse->dev = old_dev;
		psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
		psmouse_switch_protocol(psmouse, old_proto);
		psmouse_initialize(psmouse);
		psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);

		return error;
	}

	input_unregister_device(old_dev);
1853 1854 1855 1856 1857 1858 1859

	if (parent && parent->pt_activate)
		parent->pt_activate(parent);

	return count;
}

1860
static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
L
Linus Torvalds 已提交
1861
{
1862 1863
	unsigned int value;
	int err;
L
Linus Torvalds 已提交
1864

1865 1866 1867
	err = kstrtouint(buf, 10, &value);
	if (err)
		return err;
L
Linus Torvalds 已提交
1868 1869 1870 1871 1872

	psmouse->set_rate(psmouse, value);
	return count;
}

1873
static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
L
Linus Torvalds 已提交
1874
{
1875 1876
	unsigned int value;
	int err;
L
Linus Torvalds 已提交
1877

1878 1879 1880
	err = kstrtouint(buf, 10, &value);
	if (err)
		return err;
L
Linus Torvalds 已提交
1881 1882 1883 1884 1885 1886

	psmouse->set_resolution(psmouse, value);
	return count;
}


1887
static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
L
Linus Torvalds 已提交
1888
{
H
Helge Deller 已提交
1889
	const struct psmouse_protocol *proto;
L
Linus Torvalds 已提交
1890 1891 1892 1893

	if (!val)
		return -EINVAL;

1894
	proto = psmouse_protocol_by_name(val, strlen(val));
L
Linus Torvalds 已提交
1895

1896 1897
	if (!proto || !proto->maxproto)
		return -EINVAL;
L
Linus Torvalds 已提交
1898

1899
	*((unsigned int *)kp->arg) = proto->type;
L
Linus Torvalds 已提交
1900

1901
	return 0;
L
Linus Torvalds 已提交
1902 1903
}

1904
static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
L
Linus Torvalds 已提交
1905
{
1906 1907
	int type = *((unsigned int *)kp->arg);

1908
	return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
L
Linus Torvalds 已提交
1909 1910 1911 1912
}

static int __init psmouse_init(void)
{
1913 1914
	int err;

1915 1916
	lifebook_module_init();
	synaptics_module_init();
1917
	hgpk_module_init();
1918

1919 1920
	kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
	if (!kpsmoused_wq) {
1921
		pr_err("failed to create kpsmoused workqueue\n");
1922 1923 1924
		return -ENOMEM;
	}

1925 1926 1927
	err = serio_register_driver(&psmouse_drv);
	if (err)
		destroy_workqueue(kpsmoused_wq);
1928

1929
	return err;
L
Linus Torvalds 已提交
1930 1931 1932 1933 1934
}

static void __exit psmouse_exit(void)
{
	serio_unregister_driver(&psmouse_drv);
1935
	destroy_workqueue(kpsmoused_wq);
L
Linus Torvalds 已提交
1936 1937 1938 1939
}

module_init(psmouse_init);
module_exit(psmouse_exit);