]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/input/mouse/psmouse-base.c
Merge remote-tracking branches 'asoc/topic/inntel', 'asoc/topic/input', 'asoc/topic...
[mirror_ubuntu-bionic-kernel.git] / drivers / input / mouse / psmouse-base.c
CommitLineData
1da177e4
LT
1/*
2 * PS/2 mouse driver
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2003-2004 Dmitry Torokhov
6 */
7
8/*
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
12 */
13
b5d21704
DT
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15#define psmouse_fmt(fmt) fmt
16
1da177e4
LT
17#include <linux/delay.h>
18#include <linux/module.h>
1da177e4
LT
19#include <linux/slab.h>
20#include <linux/interrupt.h>
21#include <linux/input.h>
22#include <linux/serio.h>
23#include <linux/init.h>
24#include <linux/libps2.h>
c14471dc
IM
25#include <linux/mutex.h>
26
1da177e4
LT
27#include "psmouse.h"
28#include "synaptics.h"
29#include "logips2pp.h"
30#include "alps.h"
df08ef27 31#include "hgpk.h"
02d7f589 32#include "lifebook.h"
541e316a 33#include "trackpoint.h"
24bf10ab 34#include "touchkit_ps2.h"
2a0bd75e 35#include "elantech.h"
fc69f4a6 36#include "sentelic.h"
0799a924 37#include "cypress_ps2.h"
3ace3686 38#include "focaltech.h"
8b8be51b 39#include "vmmouse.h"
98ee3771 40#include "byd.h"
1da177e4
LT
41
42#define DRIVER_DESC "PS/2 mouse driver"
43
44MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
45MODULE_DESCRIPTION(DRIVER_DESC);
46MODULE_LICENSE("GPL");
47
dbf4ccd6 48static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
9bbb9e5a
RR
49static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
50static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
9c27847d 51static const struct kernel_param_ops param_ops_proto_abbrev = {
9bbb9e5a
RR
52 .set = psmouse_set_maxproto,
53 .get = psmouse_get_maxproto,
54};
1da177e4 55#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
1da177e4 56module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
dbf4ccd6 57MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
1da177e4
LT
58
59static unsigned int psmouse_resolution = 200;
60module_param_named(resolution, psmouse_resolution, uint, 0644);
61MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
62
63static unsigned int psmouse_rate = 100;
64module_param_named(rate, psmouse_rate, uint, 0644);
65MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
66
feb9eba8 67static bool psmouse_smartscroll = true;
1da177e4
LT
68module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
69MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
70
f0d5c6f4 71static unsigned int psmouse_resetafter = 5;
1da177e4
LT
72module_param_named(resetafter, psmouse_resetafter, uint, 0644);
73MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
74
8bd0ee93 75static unsigned int psmouse_resync_time;
f0d5c6f4
DT
76module_param_named(resync_time, psmouse_resync_time, uint, 0644);
77MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
78
cfe9e888
DT
79PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
80 NULL,
81 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
82PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
83 (void *) offsetof(struct psmouse, rate),
84 psmouse_show_int_attr, psmouse_attr_set_rate);
85PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
86 (void *) offsetof(struct psmouse, resolution),
87 psmouse_show_int_attr, psmouse_attr_set_resolution);
88PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
89 (void *) offsetof(struct psmouse, resetafter),
90 psmouse_show_int_attr, psmouse_set_int_attr);
f0d5c6f4
DT
91PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
92 (void *) offsetof(struct psmouse, resync_time),
93 psmouse_show_int_attr, psmouse_set_int_attr);
cfe9e888
DT
94
95static struct attribute *psmouse_attributes[] = {
96 &psmouse_attr_protocol.dattr.attr,
97 &psmouse_attr_rate.dattr.attr,
98 &psmouse_attr_resolution.dattr.attr,
99 &psmouse_attr_resetafter.dattr.attr,
f0d5c6f4 100 &psmouse_attr_resync_time.dattr.attr,
cfe9e888
DT
101 NULL
102};
103
104static struct attribute_group psmouse_attribute_group = {
105 .attrs = psmouse_attributes,
106};
1da177e4 107
04df1925 108/*
c14471dc 109 * psmouse_mutex protects all operations changing state of mouse
04df1925
DT
110 * (connecting, disconnecting, changing rate or resolution via
111 * sysfs). We could use a per-device semaphore but since there
112 * rarely more than one PS/2 mouse connected and since semaphore
113 * is taken in "slow" paths it is not worth it.
114 */
c14471dc 115static DEFINE_MUTEX(psmouse_mutex);
04df1925 116
f0d5c6f4
DT
117static struct workqueue_struct *kpsmoused_wq;
118
dbf4ccd6
DT
119struct psmouse_protocol {
120 enum psmouse_type type;
b7802c5c 121 bool maxproto;
6b9d363c 122 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
ec6184b1 123 bool try_passthru; /* Try protocol also on passthrough ports */
e38de678
HD
124 const char *name;
125 const char *alias;
b7802c5c 126 int (*detect)(struct psmouse *, bool);
dbf4ccd6
DT
127 int (*init)(struct psmouse *);
128};
1da177e4
LT
129
130/*
131 * psmouse_process_byte() analyzes the PS/2 data stream and reports
132 * relevant events to the input module once full packet has arrived.
133 */
7968a5dd 134psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
1da177e4 135{
2e5b636b 136 struct input_dev *dev = psmouse->dev;
1da177e4
LT
137 unsigned char *packet = psmouse->packet;
138
139 if (psmouse->pktcnt < psmouse->pktsize)
140 return PSMOUSE_GOOD_DATA;
141
0a88d607 142 /* Full packet accumulated, process it */
1da177e4 143
0a88d607
DT
144 switch (psmouse->type) {
145 case PSMOUSE_IMPS:
146 /* IntelliMouse has scroll wheel */
1da177e4 147 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
0a88d607 148 break;
1da177e4 149
0a88d607
DT
150 case PSMOUSE_IMEX:
151 /* Scroll wheel and buttons on IntelliMouse Explorer */
b0c9ad8e 152 switch (packet[3] & 0xC0) {
a62f0d27
DT
153 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
154 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
155 break;
156 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
157 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
158 break;
159 case 0x00:
160 case 0xC0:
161 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
162 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
163 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
164 break;
b0c9ad8e 165 }
0a88d607 166 break;
1da177e4 167
0a88d607
DT
168 case PSMOUSE_GENPS:
169 /* Report scroll buttons on NetMice */
170 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
1da177e4 171
0a88d607 172 /* Extra buttons on Genius NewNet 3D */
1da177e4
LT
173 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
174 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
0a88d607 175 break;
1da177e4 176
0a88d607
DT
177 case PSMOUSE_THINKPS:
178 /* Extra button on ThinkingMouse */
1da177e4 179 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
0a88d607
DT
180
181 /*
182 * Without this bit of weirdness moving up gives wildly
183 * high Y changes.
184 */
1da177e4 185 packet[1] |= (packet[0] & 0x40) << 1;
0a88d607 186 break;
1da177e4 187
0a88d607
DT
188 case PSMOUSE_CORTRON:
189 /*
190 * Cortron PS2 Trackball reports SIDE button in the
191 * 4th bit of the first byte.
192 */
aea6a461
AR
193 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
194 packet[0] |= 0x08;
0a88d607 195 break;
aea6a461 196
0a88d607
DT
197 default:
198 break;
199 }
1da177e4 200
0a88d607 201 /* Generic PS/2 Mouse */
1da177e4
LT
202 input_report_key(dev, BTN_LEFT, packet[0] & 1);
203 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
204 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
205
206 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
207 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
208
209 input_sync(dev);
210
211 return PSMOUSE_FULL_PACKET;
212}
213
8bf020ee
AS
214void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
215 unsigned long delay)
216{
217 queue_delayed_work(kpsmoused_wq, work, delay);
218}
219
1da177e4 220/*
f0d5c6f4
DT
221 * __psmouse_set_state() sets new psmouse state and resets all flags.
222 */
f0d5c6f4
DT
223static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
224{
225 psmouse->state = new_state;
b7802c5c 226 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
f0d5c6f4
DT
227 psmouse->ps2dev.flags = 0;
228 psmouse->last = jiffies;
229}
230
f0d5c6f4
DT
231/*
232 * psmouse_set_state() sets new psmouse state and resets all flags and
233 * counters while holding serio lock so fighting with interrupt handler
234 * is not a concern.
235 */
a48cf5f3 236void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
f0d5c6f4
DT
237{
238 serio_pause_rx(psmouse->ps2dev.serio);
239 __psmouse_set_state(psmouse, new_state);
240 serio_continue_rx(psmouse->ps2dev.serio);
241}
242
243/*
244 * psmouse_handle_byte() processes one byte of the input data stream
245 * by calling corresponding protocol handler.
246 */
7d12e780 247static int psmouse_handle_byte(struct psmouse *psmouse)
f0d5c6f4 248{
7d12e780 249 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
f0d5c6f4
DT
250
251 switch (rc) {
a62f0d27
DT
252 case PSMOUSE_BAD_DATA:
253 if (psmouse->state == PSMOUSE_ACTIVATED) {
b5d21704
DT
254 psmouse_warn(psmouse,
255 "%s at %s lost sync at byte %d\n",
256 psmouse->name, psmouse->phys,
257 psmouse->pktcnt);
a62f0d27
DT
258 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
259 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
b5d21704
DT
260 psmouse_notice(psmouse,
261 "issuing reconnect request\n");
a62f0d27
DT
262 serio_reconnect(psmouse->ps2dev.serio);
263 return -1;
f0d5c6f4 264 }
a62f0d27
DT
265 }
266 psmouse->pktcnt = 0;
267 break;
268
269 case PSMOUSE_FULL_PACKET:
270 psmouse->pktcnt = 0;
271 if (psmouse->out_of_sync_cnt) {
272 psmouse->out_of_sync_cnt = 0;
b5d21704
DT
273 psmouse_notice(psmouse,
274 "%s at %s - driver resynced.\n",
275 psmouse->name, psmouse->phys);
a62f0d27
DT
276 }
277 break;
f0d5c6f4 278
a62f0d27
DT
279 case PSMOUSE_GOOD_DATA:
280 break;
f0d5c6f4
DT
281 }
282 return 0;
283}
284
285/*
286 * psmouse_interrupt() handles incoming characters, either passing them
287 * for normal processing or gathering them as command response.
1da177e4 288 */
1da177e4 289static irqreturn_t psmouse_interrupt(struct serio *serio,
7d12e780 290 unsigned char data, unsigned int flags)
1da177e4
LT
291{
292 struct psmouse *psmouse = serio_get_drvdata(serio);
1da177e4
LT
293
294 if (psmouse->state == PSMOUSE_IGNORE)
295 goto out;
296
6b9d363c
DT
297 if (unlikely((flags & SERIO_TIMEOUT) ||
298 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
299
1da177e4 300 if (psmouse->state == PSMOUSE_ACTIVATED)
b5d21704
DT
301 psmouse_warn(psmouse,
302 "bad data from KBC -%s%s\n",
303 flags & SERIO_TIMEOUT ? " timeout" : "",
304 flags & SERIO_PARITY ? " bad parity" : "");
1da177e4
LT
305 ps2_cmd_aborted(&psmouse->ps2dev);
306 goto out;
307 }
308
309 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
310 if (ps2_handle_ack(&psmouse->ps2dev, data))
311 goto out;
312
313 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
314 if (ps2_handle_response(&psmouse->ps2dev, data))
315 goto out;
316
f0d5c6f4 317 if (psmouse->state <= PSMOUSE_RESYNCING)
1da177e4
LT
318 goto out;
319
320 if (psmouse->state == PSMOUSE_ACTIVATED &&
321 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
b5d21704
DT
322 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
323 psmouse->name, psmouse->phys, psmouse->pktcnt);
f0d5c6f4
DT
324 psmouse->badbyte = psmouse->packet[0];
325 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
8bf020ee 326 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
f0d5c6f4 327 goto out;
1da177e4
LT
328 }
329
1da177e4 330 psmouse->packet[psmouse->pktcnt++] = data;
ad530771
DT
331
332 /* Check if this is a new device announcement (0xAA 0x00) */
f0d5c6f4 333 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
89c9b480
DT
334 if (psmouse->pktcnt == 1) {
335 psmouse->last = jiffies;
1da177e4 336 goto out;
89c9b480 337 }
1da177e4 338
535650fd
ZH
339 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
340 (psmouse->type == PSMOUSE_HGPK &&
341 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
f0d5c6f4
DT
342 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
343 serio_reconnect(serio);
344 goto out;
1da177e4 345 }
ad530771
DT
346
347 /* Not a new device, try processing first byte normally */
f0d5c6f4 348 psmouse->pktcnt = 1;
7d12e780 349 if (psmouse_handle_byte(psmouse))
f0d5c6f4 350 goto out;
1da177e4 351
f0d5c6f4
DT
352 psmouse->packet[psmouse->pktcnt++] = data;
353 }
1da177e4 354
ad530771
DT
355 /*
356 * See if we need to force resync because mouse was idle for
357 * too long.
358 */
f0d5c6f4
DT
359 if (psmouse->state == PSMOUSE_ACTIVATED &&
360 psmouse->pktcnt == 1 && psmouse->resync_time &&
361 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
362 psmouse->badbyte = psmouse->packet[0];
363 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
8bf020ee 364 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
f0d5c6f4
DT
365 goto out;
366 }
1da177e4 367
f0d5c6f4 368 psmouse->last = jiffies;
7d12e780 369 psmouse_handle_byte(psmouse);
1da177e4 370
f0d5c6f4 371 out:
1da177e4
LT
372 return IRQ_HANDLED;
373}
374
1da177e4
LT
375/*
376 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
377 * using sliced syntax, understood by advanced devices, such as Logitech
378 * or Synaptics touchpads. The command is encoded as:
379 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
380 * is the command.
381 */
382int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
383{
384 int i;
385
386 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
387 return -1;
388
389 for (i = 6; i >= 0; i -= 2) {
390 unsigned char d = (command >> i) & 3;
391 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
392 return -1;
393 }
394
395 return 0;
396}
397
1da177e4
LT
398/*
399 * psmouse_reset() resets the mouse into power-on state.
400 */
401int psmouse_reset(struct psmouse *psmouse)
402{
403 unsigned char param[2];
404
405 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
406 return -1;
407
408 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
409 return -1;
410
411 return 0;
412}
413
ee9dfd7a
DT
414/*
415 * Here we set the mouse resolution.
416 */
ee9dfd7a
DT
417void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
418{
419 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
420 unsigned char p;
421
422 if (resolution == 0 || resolution > 200)
423 resolution = 200;
424
425 p = params[resolution / 50];
426 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
427 psmouse->resolution = 25 << p;
428}
429
430/*
431 * Here we set the mouse report rate.
432 */
ee9dfd7a
DT
433static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
434{
435 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
436 unsigned char r;
437 int i = 0;
438
439 while (rates[i] > rate) i++;
440 r = rates[i];
441 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
442 psmouse->rate = r;
443}
444
4ec212f0
MG
445/*
446 * Here we set the mouse scaling.
447 */
4ec212f0
MG
448static void psmouse_set_scale(struct psmouse *psmouse, enum psmouse_scale scale)
449{
450 ps2_command(&psmouse->ps2dev, NULL,
451 scale == PSMOUSE_SCALE21 ? PSMOUSE_CMD_SETSCALE21 :
452 PSMOUSE_CMD_SETSCALE11);
453}
454
ee9dfd7a
DT
455/*
456 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
457 */
ee9dfd7a
DT
458static int psmouse_poll(struct psmouse *psmouse)
459{
460 return ps2_command(&psmouse->ps2dev, psmouse->packet,
461 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
462}
463
99e14c1e
DT
464static bool psmouse_check_pnp_id(const char *id, const char * const ids[])
465{
466 int i;
467
468 for (i = 0; ids[i]; i++)
469 if (!strcasecmp(id, ids[i]))
470 return true;
471
472 return false;
473}
474
2c75ada6
HG
475/*
476 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
477 */
478bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
479{
99e14c1e
DT
480 struct serio *serio = psmouse->ps2dev.serio;
481 char *p, *fw_id_copy, *save_ptr;
482 bool found = false;
483
484 if (strncmp(serio->firmware_id, "PNP: ", 5))
485 return false;
486
487 fw_id_copy = kstrndup(&serio->firmware_id[5],
488 sizeof(serio->firmware_id) - 5,
489 GFP_KERNEL);
490 if (!fw_id_copy)
491 return false;
492
493 save_ptr = fw_id_copy;
494 while ((p = strsep(&fw_id_copy, " ")) != NULL) {
495 if (psmouse_check_pnp_id(p, ids)) {
496 found = true;
497 break;
498 }
499 }
2c75ada6 500
99e14c1e
DT
501 kfree(save_ptr);
502 return found;
2c75ada6 503}
1da177e4
LT
504
505/*
506 * Genius NetMouse magic init.
507 */
b7802c5c 508static int genius_detect(struct psmouse *psmouse, bool set_properties)
1da177e4
LT
509{
510 struct ps2dev *ps2dev = &psmouse->ps2dev;
511 unsigned char param[4];
512
513 param[0] = 3;
514 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
515 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
516 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
517 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
518 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
519
520 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
521 return -1;
522
523 if (set_properties) {
315eb996 524 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
b7802c5c
DT
525 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
526 __set_bit(BTN_SIDE, psmouse->dev->keybit);
527 __set_bit(REL_WHEEL, psmouse->dev->relbit);
1da177e4
LT
528
529 psmouse->vendor = "Genius";
a3f3f317 530 psmouse->name = "Mouse";
1da177e4
LT
531 psmouse->pktsize = 4;
532 }
533
534 return 0;
535}
536
537/*
538 * IntelliMouse magic init.
539 */
b7802c5c 540static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
1da177e4
LT
541{
542 struct ps2dev *ps2dev = &psmouse->ps2dev;
543 unsigned char param[2];
544
545 param[0] = 200;
546 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
547 param[0] = 100;
548 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
549 param[0] = 80;
550 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
551 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
552
553 if (param[0] != 3)
554 return -1;
555
556 if (set_properties) {
b7802c5c
DT
557 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
558 __set_bit(REL_WHEEL, psmouse->dev->relbit);
1da177e4 559
315eb996
DT
560 if (!psmouse->vendor)
561 psmouse->vendor = "Generic";
562 if (!psmouse->name)
563 psmouse->name = "Wheel Mouse";
1da177e4
LT
564 psmouse->pktsize = 4;
565 }
566
567 return 0;
568}
569
570/*
571 * Try IntelliMouse/Explorer magic init.
572 */
b7802c5c 573static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
1da177e4
LT
574{
575 struct ps2dev *ps2dev = &psmouse->ps2dev;
576 unsigned char param[2];
577
578 intellimouse_detect(psmouse, 0);
579
580 param[0] = 200;
581 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
582 param[0] = 200;
583 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
584 param[0] = 80;
585 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
586 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
587
588 if (param[0] != 4)
589 return -1;
590
ad530771 591 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
b0c9ad8e
PB
592 param[0] = 200;
593 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
594 param[0] = 80;
595 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
596 param[0] = 40;
597 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
598
1da177e4 599 if (set_properties) {
b7802c5c
DT
600 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
601 __set_bit(REL_WHEEL, psmouse->dev->relbit);
602 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
603 __set_bit(BTN_SIDE, psmouse->dev->keybit);
604 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
1da177e4 605
315eb996
DT
606 if (!psmouse->vendor)
607 psmouse->vendor = "Generic";
608 if (!psmouse->name)
609 psmouse->name = "Explorer Mouse";
1da177e4
LT
610 psmouse->pktsize = 4;
611 }
612
613 return 0;
614}
615
616/*
617 * Kensington ThinkingMouse / ExpertMouse magic init.
618 */
b7802c5c 619static int thinking_detect(struct psmouse *psmouse, bool set_properties)
1da177e4
LT
620{
621 struct ps2dev *ps2dev = &psmouse->ps2dev;
622 unsigned char param[2];
e38de678 623 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
1da177e4
LT
624 int i;
625
626 param[0] = 10;
627 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
628 param[0] = 0;
629 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
e38de678
HD
630 for (i = 0; i < ARRAY_SIZE(seq); i++) {
631 param[0] = seq[i];
632 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
633 }
1da177e4
LT
634 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
635
636 if (param[0] != 2)
637 return -1;
638
639 if (set_properties) {
315eb996 640 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
b7802c5c 641 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
1da177e4
LT
642
643 psmouse->vendor = "Kensington";
644 psmouse->name = "ThinkingMouse";
645 }
646
647 return 0;
648}
649
650/*
651 * Bare PS/2 protocol "detection". Always succeeds.
652 */
b7802c5c 653static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
1da177e4 654{
dbf4ccd6 655 if (set_properties) {
315eb996
DT
656 if (!psmouse->vendor)
657 psmouse->vendor = "Generic";
658 if (!psmouse->name)
659 psmouse->name = "Mouse";
660
ad530771
DT
661 /*
662 * We have no way of figuring true number of buttons so let's
663 * assume that the device has 3.
664 */
315eb996 665 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
dbf4ccd6 666 }
1da177e4
LT
667
668 return 0;
669}
670
aea6a461
AR
671/*
672 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
673 * must be forced by sysfs protocol writing.
674 */
b7802c5c 675static int cortron_detect(struct psmouse *psmouse, bool set_properties)
aea6a461
AR
676{
677 if (set_properties) {
678 psmouse->vendor = "Cortron";
679 psmouse->name = "PS/2 Trackball";
315eb996
DT
680
681 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
b7802c5c 682 __set_bit(BTN_SIDE, psmouse->dev->keybit);
aea6a461
AR
683 }
684
685 return 0;
686}
dbf4ccd6 687
5fa75cfe
DT
688static const struct psmouse_protocol psmouse_protocols[] = {
689 {
690 .type = PSMOUSE_PS2,
691 .name = "PS/2",
692 .alias = "bare",
693 .maxproto = true,
694 .ignore_parity = true,
695 .detect = ps2bare_detect,
ec6184b1 696 .try_passthru = true,
5fa75cfe
DT
697 },
698#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
699 {
700 .type = PSMOUSE_PS2PP,
701 .name = "PS2++",
702 .alias = "logitech",
190e2031 703 .detect = ps2pp_detect,
5fa75cfe
DT
704 },
705#endif
706 {
707 .type = PSMOUSE_THINKPS,
708 .name = "ThinkPS/2",
709 .alias = "thinkps",
710 .detect = thinking_detect,
711 },
712#ifdef CONFIG_MOUSE_PS2_CYPRESS
713 {
714 .type = PSMOUSE_CYPRESS,
715 .name = "CyPS/2",
716 .alias = "cypress",
717 .detect = cypress_detect,
718 .init = cypress_init,
719 },
720#endif
721 {
722 .type = PSMOUSE_GENPS,
723 .name = "GenPS/2",
724 .alias = "genius",
725 .detect = genius_detect,
726 },
727 {
728 .type = PSMOUSE_IMPS,
729 .name = "ImPS/2",
730 .alias = "imps",
731 .maxproto = true,
732 .ignore_parity = true,
733 .detect = intellimouse_detect,
ec6184b1 734 .try_passthru = true,
5fa75cfe
DT
735 },
736 {
737 .type = PSMOUSE_IMEX,
738 .name = "ImExPS/2",
739 .alias = "exps",
740 .maxproto = true,
741 .ignore_parity = true,
742 .detect = im_explorer_detect,
ec6184b1 743 .try_passthru = true,
5fa75cfe
DT
744 },
745#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
746 {
747 .type = PSMOUSE_SYNAPTICS,
748 .name = "SynPS/2",
749 .alias = "synaptics",
750 .detect = synaptics_detect,
751 .init = synaptics_init,
752 },
753 {
754 .type = PSMOUSE_SYNAPTICS_RELATIVE,
755 .name = "SynRelPS/2",
756 .alias = "synaptics-relative",
757 .detect = synaptics_detect,
758 .init = synaptics_init_relative,
759 },
760#endif
761#ifdef CONFIG_MOUSE_PS2_ALPS
762 {
763 .type = PSMOUSE_ALPS,
764 .name = "AlpsPS/2",
765 .alias = "alps",
766 .detect = alps_detect,
767 .init = alps_init,
768 },
769#endif
770#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
771 {
772 .type = PSMOUSE_LIFEBOOK,
773 .name = "LBPS/2",
774 .alias = "lifebook",
c378b511 775 .detect = lifebook_detect,
5fa75cfe
DT
776 .init = lifebook_init,
777 },
778#endif
779#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
780 {
781 .type = PSMOUSE_TRACKPOINT,
782 .name = "TPPS/2",
783 .alias = "trackpoint",
784 .detect = trackpoint_detect,
ec6184b1 785 .try_passthru = true,
5fa75cfe
DT
786 },
787#endif
788#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
789 {
790 .type = PSMOUSE_TOUCHKIT_PS2,
791 .name = "touchkitPS/2",
792 .alias = "touchkit",
793 .detect = touchkit_ps2_detect,
794 },
795#endif
796#ifdef CONFIG_MOUSE_PS2_OLPC
797 {
798 .type = PSMOUSE_HGPK,
799 .name = "OLPC HGPK",
800 .alias = "hgpk",
801 .detect = hgpk_detect,
802 },
803#endif
804#ifdef CONFIG_MOUSE_PS2_ELANTECH
805 {
806 .type = PSMOUSE_ELANTECH,
807 .name = "ETPS/2",
808 .alias = "elantech",
809 .detect = elantech_detect,
810 .init = elantech_init,
811 },
812#endif
813#ifdef CONFIG_MOUSE_PS2_SENTELIC
814 {
815 .type = PSMOUSE_FSP,
816 .name = "FSPPS/2",
817 .alias = "fsp",
818 .detect = fsp_detect,
819 .init = fsp_init,
820 },
821#endif
822 {
823 .type = PSMOUSE_CORTRON,
824 .name = "CortronPS/2",
825 .alias = "cortps",
826 .detect = cortron_detect,
827 },
828#ifdef CONFIG_MOUSE_PS2_FOCALTECH
829 {
830 .type = PSMOUSE_FOCALTECH,
831 .name = "FocalTechPS/2",
832 .alias = "focaltech",
833 .detect = focaltech_detect,
834 .init = focaltech_init,
835 },
836#endif
837#ifdef CONFIG_MOUSE_PS2_VMMOUSE
838 {
839 .type = PSMOUSE_VMMOUSE,
840 .name = VMMOUSE_PSNAME,
841 .alias = "vmmouse",
842 .detect = vmmouse_detect,
843 .init = vmmouse_init,
844 },
98ee3771
CD
845#endif
846#ifdef CONFIG_MOUSE_PS2_BYD
847 {
848 .type = PSMOUSE_BYD,
2d5f5611 849 .name = "BYDPS/2",
98ee3771
CD
850 .alias = "byd",
851 .detect = byd_detect,
852 .init = byd_init,
853 },
5fa75cfe
DT
854#endif
855 {
856 .type = PSMOUSE_AUTO,
857 .name = "auto",
858 .alias = "any",
859 .maxproto = true,
860 },
861};
862
c378b511 863static const struct psmouse_protocol *__psmouse_protocol_by_type(enum psmouse_type type)
5fa75cfe
DT
864{
865 int i;
866
867 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
868 if (psmouse_protocols[i].type == type)
869 return &psmouse_protocols[i];
870
c378b511
DT
871 return NULL;
872}
873
874static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
875{
876 const struct psmouse_protocol *proto;
877
878 proto = __psmouse_protocol_by_type(type);
879 if (proto)
880 return proto;
881
5fa75cfe
DT
882 WARN_ON(1);
883 return &psmouse_protocols[0];
884}
885
886static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
887{
888 const struct psmouse_protocol *p;
889 int i;
890
891 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
892 p = &psmouse_protocols[i];
893
894 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
895 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
896 return &psmouse_protocols[i];
897 }
898
899 return NULL;
900}
901
ee9dfd7a
DT
902/*
903 * Apply default settings to the psmouse structure. Most of them will
904 * be overridden by individual protocol initialization routines.
905 */
ee9dfd7a
DT
906static void psmouse_apply_defaults(struct psmouse *psmouse)
907{
908 struct input_dev *input_dev = psmouse->dev;
909
910 memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
911 memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
912 memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
913 memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
914 memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));
915
916 __set_bit(EV_KEY, input_dev->evbit);
917 __set_bit(EV_REL, input_dev->evbit);
918
919 __set_bit(BTN_LEFT, input_dev->keybit);
920 __set_bit(BTN_RIGHT, input_dev->keybit);
921
922 __set_bit(REL_X, input_dev->relbit);
923 __set_bit(REL_Y, input_dev->relbit);
924
01d4cd5c
HG
925 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
926
ee9dfd7a
DT
927 psmouse->set_rate = psmouse_set_rate;
928 psmouse->set_resolution = psmouse_set_resolution;
4ec212f0 929 psmouse->set_scale = psmouse_set_scale;
ee9dfd7a
DT
930 psmouse->poll = psmouse_poll;
931 psmouse->protocol_handler = psmouse_process_byte;
932 psmouse->pktsize = 3;
933 psmouse->reconnect = NULL;
934 psmouse->disconnect = NULL;
935 psmouse->cleanup = NULL;
936 psmouse->pt_activate = NULL;
937 psmouse->pt_deactivate = NULL;
938}
939
c378b511
DT
940static bool psmouse_try_protocol(struct psmouse *psmouse,
941 enum psmouse_type type,
942 unsigned int *max_proto,
943 bool set_properties, bool init_allowed)
ee9dfd7a 944{
c378b511
DT
945 const struct psmouse_protocol *proto;
946
947 proto = __psmouse_protocol_by_type(type);
948 if (!proto)
949 return false;
950
ec6184b1
DT
951 if (psmouse->ps2dev.serio->id.type == SERIO_PS_PSTHRU &&
952 !proto->try_passthru) {
953 return false;
954 }
955
ee9dfd7a
DT
956 if (set_properties)
957 psmouse_apply_defaults(psmouse);
958
c378b511
DT
959 if (proto->detect(psmouse, set_properties) != 0)
960 return false;
961
962 if (set_properties && proto->init && init_allowed) {
963 if (proto->init(psmouse) != 0) {
964 /*
965 * We detected device, but init failed. Adjust
966 * max_proto so we only try standard protocols.
967 */
968 if (*max_proto > PSMOUSE_IMEX)
969 *max_proto = PSMOUSE_IMEX;
970
971 return false;
972 }
973 }
974
975 return true;
ee9dfd7a
DT
976}
977
1da177e4
LT
978/*
979 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
980 * the mouse may have.
981 */
1da177e4 982static int psmouse_extensions(struct psmouse *psmouse,
b7802c5c 983 unsigned int max_proto, bool set_properties)
1da177e4 984{
0698989d 985 bool synaptics_hardware = false;
1da177e4 986
ad530771
DT
987 /*
988 * Always check for focaltech, this is safe as it uses pnp-id
989 * matching.
990 */
c378b511
DT
991 if (psmouse_try_protocol(psmouse, PSMOUSE_FOCALTECH,
992 &max_proto, set_properties, false)) {
993 if (max_proto > PSMOUSE_IMEX &&
994 IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH) &&
995 (!set_properties || focaltech_init(psmouse) == 0)) {
996 return PSMOUSE_FOCALTECH;
3ace3686 997 }
2b6f39e9
DT
998 /*
999 * Restrict psmouse_max_proto so that psmouse_initialize()
1000 * does not try to reset rate and resolution, because even
1001 * that upsets the device.
1002 * This also causes us to basically fall through to basic
1003 * protocol detection, where we fully reset the mouse,
1004 * and set it up as bare PS/2 protocol device.
1005 */
1006 psmouse_max_proto = max_proto = PSMOUSE_PS2;
3ace3686
HG
1007 }
1008
ad530771
DT
1009 /*
1010 * We always check for LifeBook because it does not disturb mouse
1011 * (it only checks DMI information).
1012 */
c378b511
DT
1013 if (psmouse_try_protocol(psmouse, PSMOUSE_LIFEBOOK, &max_proto,
1014 set_properties, max_proto > PSMOUSE_IMEX))
1015 return PSMOUSE_LIFEBOOK;
02d7f589 1016
c378b511
DT
1017 if (psmouse_try_protocol(psmouse, PSMOUSE_VMMOUSE, &max_proto,
1018 set_properties, max_proto > PSMOUSE_IMEX))
1019 return PSMOUSE_VMMOUSE;
8b8be51b 1020
ad530771
DT
1021 /*
1022 * Try Kensington ThinkingMouse (we try first, because Synaptics
1023 * probe upsets the ThinkingMouse).
1024 */
ee9dfd7a 1025 if (max_proto > PSMOUSE_IMEX &&
c378b511
DT
1026 psmouse_try_protocol(psmouse, PSMOUSE_THINKPS, &max_proto,
1027 set_properties, true)) {
1da177e4 1028 return PSMOUSE_THINKPS;
ee9dfd7a 1029 }
1da177e4 1030
ad530771
DT
1031 /*
1032 * Try Synaptics TouchPad. Note that probing is done even if
1033 * Synaptics protocol support is disabled in config - we need to
1034 * know if it is Synaptics so we can reset it properly after
1035 * probing for IntelliMouse.
1036 */
ee9dfd7a 1037 if (max_proto > PSMOUSE_PS2 &&
c378b511
DT
1038 psmouse_try_protocol(psmouse, PSMOUSE_SYNAPTICS, &max_proto,
1039 set_properties, false)) {
b7802c5c 1040 synaptics_hardware = true;
1da177e4
LT
1041
1042 if (max_proto > PSMOUSE_IMEX) {
ad530771
DT
1043 /*
1044 * Try activating protocol, but check if support is
1045 * enabled first, since we try detecting Synaptics
1046 * even when protocol is disabled.
1047 */
290b799c 1048 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) &&
e4e6efd2 1049 (!set_properties || synaptics_init(psmouse) == 0)) {
1da177e4 1050 return PSMOUSE_SYNAPTICS;
e4e6efd2
DD
1051 }
1052
ad530771
DT
1053 /*
1054 * Some Synaptics touchpads can emulate extended
1055 * protocols (like IMPS/2). Unfortunately
1056 * Logitech/Genius probes confuse some firmware
1057 * versions so we'll have to skip them.
1058 */
1da177e4
LT
1059 max_proto = PSMOUSE_IMEX;
1060 }
ad530771
DT
1061
1062 /*
1063 * Make sure that touchpad is in relative mode, gestures
1064 * (taps) are enabled.
1065 */
1da177e4
LT
1066 synaptics_reset(psmouse);
1067 }
1068
ad530771
DT
1069 /*
1070 * Try Cypress Trackpad. We must try it before Finger Sensing Pad
1071 * because Finger Sensing Pad probe upsets some modules of Cypress
1072 * Trackpads.
1073 */
0799a924 1074 if (max_proto > PSMOUSE_IMEX &&
c378b511
DT
1075 psmouse_try_protocol(psmouse, PSMOUSE_CYPRESS, &max_proto,
1076 set_properties, true)) {
1077 return PSMOUSE_CYPRESS;
0799a924
DD
1078 }
1079
ad530771 1080 /* Try ALPS TouchPad */
1da177e4
LT
1081 if (max_proto > PSMOUSE_IMEX) {
1082 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
c378b511
DT
1083 if (psmouse_try_protocol(psmouse, PSMOUSE_ALPS,
1084 &max_proto, set_properties, true))
1085 return PSMOUSE_ALPS;
1da177e4
LT
1086 }
1087
ad530771 1088 /* Try OLPC HGPK touchpad */
df08ef27 1089 if (max_proto > PSMOUSE_IMEX &&
c378b511
DT
1090 psmouse_try_protocol(psmouse, PSMOUSE_HGPK, &max_proto,
1091 set_properties, true)) {
1092 return PSMOUSE_HGPK;
df08ef27 1093 }
24bf10ab 1094
ad530771 1095 /* Try Elantech touchpad */
2a0bd75e 1096 if (max_proto > PSMOUSE_IMEX &&
c378b511
DT
1097 psmouse_try_protocol(psmouse, PSMOUSE_ELANTECH,
1098 &max_proto, set_properties, true)) {
1099 return PSMOUSE_ELANTECH;
2a0bd75e
AO
1100 }
1101
df08ef27 1102 if (max_proto > PSMOUSE_IMEX) {
c378b511
DT
1103 if (psmouse_try_protocol(psmouse, PSMOUSE_GENPS,
1104 &max_proto, set_properties, true))
24bf10ab
SL
1105 return PSMOUSE_GENPS;
1106
c378b511
DT
1107 if (psmouse_try_protocol(psmouse, PSMOUSE_PS2PP,
1108 &max_proto, set_properties, true))
24bf10ab 1109 return PSMOUSE_PS2PP;
1da177e4 1110
c378b511
DT
1111 if (psmouse_try_protocol(psmouse, PSMOUSE_TRACKPOINT,
1112 &max_proto, set_properties, true))
24bf10ab 1113 return PSMOUSE_TRACKPOINT;
1da177e4 1114
c378b511
DT
1115 if (psmouse_try_protocol(psmouse, PSMOUSE_TOUCHKIT_PS2,
1116 &max_proto, set_properties, true))
24bf10ab
SL
1117 return PSMOUSE_TOUCHKIT_PS2;
1118 }
ba44995a 1119
ad530771
DT
1120 /*
1121 * Try Finger Sensing Pad. We do it here because its probe upsets
1122 * Trackpoint devices (causing TP_READ_ID command to time out).
1123 */
c378b511
DT
1124 if (max_proto > PSMOUSE_IMEX &&
1125 psmouse_try_protocol(psmouse, PSMOUSE_FSP,
1126 &max_proto, set_properties, true)) {
1127 return PSMOUSE_FSP;
4a18b3ab
TL
1128 }
1129
ad530771
DT
1130 /*
1131 * Reset to defaults in case the device got confused by extended
1132 * protocol probes. Note that we follow up with full reset because
1133 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
1134 */
554fc193 1135 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
ba44995a 1136 psmouse_reset(psmouse);
1da177e4 1137
ee9dfd7a 1138 if (max_proto >= PSMOUSE_IMEX &&
c378b511
DT
1139 psmouse_try_protocol(psmouse, PSMOUSE_IMEX,
1140 &max_proto, set_properties, true)) {
1da177e4 1141 return PSMOUSE_IMEX;
ee9dfd7a 1142 }
1da177e4 1143
ee9dfd7a 1144 if (max_proto >= PSMOUSE_IMPS &&
c378b511
DT
1145 psmouse_try_protocol(psmouse, PSMOUSE_IMPS,
1146 &max_proto, set_properties, true)) {
1da177e4 1147 return PSMOUSE_IMPS;
ee9dfd7a 1148 }
1da177e4 1149
ad530771
DT
1150 /*
1151 * Okay, all failed, we have a standard mouse here. The number of
1152 * the buttons is still a question, though. We assume 3.
1153 */
c378b511
DT
1154 psmouse_try_protocol(psmouse, PSMOUSE_PS2,
1155 &max_proto, set_properties, true);
1da177e4
LT
1156
1157 if (synaptics_hardware) {
ad530771
DT
1158 /*
1159 * We detected Synaptics hardware but it did not respond to
1160 * IMPS/2 probes. We need to reset the touchpad because if
1161 * there is a track point on the pass through port it could
1162 * get disabled while probing for protocol extensions.
1163 */
1da177e4 1164 psmouse_reset(psmouse);
1da177e4
LT
1165 }
1166
1167 return PSMOUSE_PS2;
1168}
1169
1170/*
1171 * psmouse_probe() probes for a PS/2 mouse.
1172 */
1da177e4
LT
1173static int psmouse_probe(struct psmouse *psmouse)
1174{
1175 struct ps2dev *ps2dev = &psmouse->ps2dev;
1176 unsigned char param[2];
1177
ad530771
DT
1178 /*
1179 * First, we check if it's a mouse. It should send 0x00 or 0x03 in
1180 * case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
1181 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and
1182 * subsequent ID queries, probably due to a firmware bug.
1183 */
1da177e4
LT
1184 param[0] = 0xa5;
1185 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1186 return -1;
1187
7741e931
VP
1188 if (param[0] != 0x00 && param[0] != 0x03 &&
1189 param[0] != 0x04 && param[0] != 0xff)
1da177e4
LT
1190 return -1;
1191
ad530771
DT
1192 /*
1193 * Then we reset and disable the mouse so that it doesn't generate
1194 * events.
1195 */
1da177e4 1196 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
b5d21704
DT
1197 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
1198 ps2dev->serio->phys);
1da177e4
LT
1199
1200 return 0;
1201}
1202
1da177e4
LT
1203/*
1204 * psmouse_initialize() initializes the mouse to a sane state.
1205 */
1da177e4
LT
1206static void psmouse_initialize(struct psmouse *psmouse)
1207{
ad530771
DT
1208 /*
1209 * We set the mouse report rate, resolution and scaling.
1210 */
1da177e4
LT
1211 if (psmouse_max_proto != PSMOUSE_PS2) {
1212 psmouse->set_rate(psmouse, psmouse->rate);
1213 psmouse->set_resolution(psmouse, psmouse->resolution);
4ec212f0 1214 psmouse->set_scale(psmouse, PSMOUSE_SCALE11);
1da177e4
LT
1215 }
1216}
1217
1da177e4
LT
1218/*
1219 * psmouse_activate() enables the mouse so that we get motion reports from it.
1220 */
bd26f3d6 1221int psmouse_activate(struct psmouse *psmouse)
1da177e4 1222{
bd26f3d6 1223 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
b5d21704
DT
1224 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1225 psmouse->ps2dev.serio->phys);
bd26f3d6
AS
1226 return -1;
1227 }
1da177e4
LT
1228
1229 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
bd26f3d6 1230 return 0;
1da177e4
LT
1231}
1232
1da177e4 1233/*
ad530771
DT
1234 * psmouse_deactivate() puts the mouse into poll mode so that we don't get
1235 * motion reports from it unless we explicitly request it.
1da177e4 1236 */
bd26f3d6 1237int psmouse_deactivate(struct psmouse *psmouse)
1da177e4 1238{
bd26f3d6 1239 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) {
b5d21704
DT
1240 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1241 psmouse->ps2dev.serio->phys);
bd26f3d6
AS
1242 return -1;
1243 }
1da177e4
LT
1244
1245 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
bd26f3d6 1246 return 0;
1da177e4
LT
1247}
1248
f0d5c6f4
DT
1249/*
1250 * psmouse_resync() attempts to re-validate current protocol.
1251 */
c4028958 1252static void psmouse_resync(struct work_struct *work)
f0d5c6f4 1253{
c4028958 1254 struct psmouse *parent = NULL, *psmouse =
8bf020ee 1255 container_of(work, struct psmouse, resync_work.work);
f0d5c6f4
DT
1256 struct serio *serio = psmouse->ps2dev.serio;
1257 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
b7802c5c 1258 bool failed = false, enabled = false;
f0d5c6f4
DT
1259 int i;
1260
c14471dc 1261 mutex_lock(&psmouse_mutex);
f0d5c6f4
DT
1262
1263 if (psmouse->state != PSMOUSE_RESYNCING)
1264 goto out;
1265
1266 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1267 parent = serio_get_drvdata(serio->parent);
1268 psmouse_deactivate(parent);
1269 }
1270
ad530771
DT
1271 /*
1272 * Some mice don't ACK commands sent while they are in the middle of
1273 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1274 * instead of ps2_command() which would wait for 200ms for an ACK
1275 * that may never come.
1276 * As an additional quirk ALPS touchpads may not only forget to ACK
1277 * disable command but will stop reporting taps, so if we see that
1278 * mouse at least once ACKs disable we will do full reconnect if ACK
1279 * is missing.
1280 */
f0d5c6f4
DT
1281 psmouse->num_resyncs++;
1282
1283 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1284 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
b7802c5c 1285 failed = true;
f0d5c6f4 1286 } else
b7802c5c 1287 psmouse->acks_disable_command = true;
f0d5c6f4 1288
ad530771
DT
1289 /*
1290 * Poll the mouse. If it was reset the packet will be shorter than
1291 * psmouse->pktsize and ps2_command will fail. We do not expect and
1292 * do not handle scenario when mouse "upgrades" its protocol while
1293 * disconnected since it would require additional delay. If we ever
1294 * see a mouse that does it we'll adjust the code.
1295 */
f0d5c6f4
DT
1296 if (!failed) {
1297 if (psmouse->poll(psmouse))
b7802c5c 1298 failed = true;
f0d5c6f4
DT
1299 else {
1300 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1301 for (i = 0; i < psmouse->pktsize; i++) {
1302 psmouse->pktcnt++;
7d12e780 1303 rc = psmouse->protocol_handler(psmouse);
f0d5c6f4
DT
1304 if (rc != PSMOUSE_GOOD_DATA)
1305 break;
1306 }
1307 if (rc != PSMOUSE_FULL_PACKET)
b7802c5c 1308 failed = true;
f0d5c6f4
DT
1309 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1310 }
1311 }
ad530771
DT
1312
1313 /*
1314 * Now try to enable mouse. We try to do that even if poll failed
1315 * and also repeat our attempts 5 times, otherwise we may be left
1316 * out with disabled mouse.
1317 */
f0d5c6f4
DT
1318 for (i = 0; i < 5; i++) {
1319 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
b7802c5c 1320 enabled = true;
f0d5c6f4
DT
1321 break;
1322 }
1323 msleep(200);
1324 }
1325
1326 if (!enabled) {
b5d21704
DT
1327 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1328 psmouse->ps2dev.serio->phys);
b7802c5c 1329 failed = true;
f0d5c6f4
DT
1330 }
1331
1332 if (failed) {
1333 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
b5d21704
DT
1334 psmouse_info(psmouse,
1335 "resync failed, issuing reconnect request\n");
f0d5c6f4
DT
1336 serio_reconnect(serio);
1337 } else
1338 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1339
1340 if (parent)
1341 psmouse_activate(parent);
1342 out:
c14471dc 1343 mutex_unlock(&psmouse_mutex);
f0d5c6f4 1344}
1da177e4
LT
1345
1346/*
1347 * psmouse_cleanup() resets the mouse into power-on state.
1348 */
1da177e4
LT
1349static void psmouse_cleanup(struct serio *serio)
1350{
1351 struct psmouse *psmouse = serio_get_drvdata(serio);
a1cec061
DT
1352 struct psmouse *parent = NULL;
1353
1354 mutex_lock(&psmouse_mutex);
1355
1356 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1357 parent = serio_get_drvdata(serio->parent);
1358 psmouse_deactivate(parent);
1359 }
1360
a9f0c381
DT
1361 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1362
1363 /*
1364 * Disable stream mode so cleanup routine can proceed undisturbed.
1365 */
1366 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
b5d21704
DT
1367 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1368 psmouse->ps2dev.serio->phys);
a1cec061
DT
1369
1370 if (psmouse->cleanup)
1371 psmouse->cleanup(psmouse);
1da177e4 1372
ad530771
DT
1373 /*
1374 * Reset the mouse to defaults (bare PS/2 protocol).
1375 */
4a299bf5 1376 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
a1cec061 1377
ad530771
DT
1378 /*
1379 * Some boxes, such as HP nx7400, get terribly confused if mouse
1380 * is not fully enabled before suspending/shutting down.
1381 */
a1cec061
DT
1382 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1383
1384 if (parent) {
1385 if (parent->pt_deactivate)
1386 parent->pt_deactivate(parent);
1387
1388 psmouse_activate(parent);
1389 }
1390
1391 mutex_unlock(&psmouse_mutex);
1da177e4
LT
1392}
1393
1394/*
1395 * psmouse_disconnect() closes and frees.
1396 */
1da177e4
LT
1397static void psmouse_disconnect(struct serio *serio)
1398{
04df1925
DT
1399 struct psmouse *psmouse, *parent = NULL;
1400
1401 psmouse = serio_get_drvdata(serio);
1da177e4 1402
cfe9e888 1403 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1da177e4 1404
c14471dc 1405 mutex_lock(&psmouse_mutex);
04df1925 1406
1da177e4
LT
1407 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1408
f0d5c6f4 1409 /* make sure we don't have a resync in progress */
c14471dc 1410 mutex_unlock(&psmouse_mutex);
f0d5c6f4 1411 flush_workqueue(kpsmoused_wq);
c14471dc 1412 mutex_lock(&psmouse_mutex);
f0d5c6f4 1413
1da177e4
LT
1414 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1415 parent = serio_get_drvdata(serio->parent);
04df1925 1416 psmouse_deactivate(parent);
1da177e4
LT
1417 }
1418
1419 if (psmouse->disconnect)
1420 psmouse->disconnect(psmouse);
1421
04df1925
DT
1422 if (parent && parent->pt_deactivate)
1423 parent->pt_deactivate(parent);
1424
1da177e4
LT
1425 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1426
1da177e4
LT
1427 serio_close(serio);
1428 serio_set_drvdata(serio, NULL);
2e5b636b 1429 input_unregister_device(psmouse->dev);
1da177e4 1430 kfree(psmouse);
04df1925
DT
1431
1432 if (parent)
1433 psmouse_activate(parent);
1434
c14471dc 1435 mutex_unlock(&psmouse_mutex);
1da177e4
LT
1436}
1437
315eb996
DT
1438static int psmouse_switch_protocol(struct psmouse *psmouse,
1439 const struct psmouse_protocol *proto)
dbf4ccd6 1440{
6b9d363c 1441 const struct psmouse_protocol *selected_proto;
2e5b636b 1442 struct input_dev *input_dev = psmouse->dev;
dbf4ccd6 1443
28aa7f1c 1444 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
dbf4ccd6 1445
dbf4ccd6 1446 if (proto && (proto->detect || proto->init)) {
ee9dfd7a
DT
1447 psmouse_apply_defaults(psmouse);
1448
a62f0d27 1449 if (proto->detect && proto->detect(psmouse, true) < 0)
dbf4ccd6
DT
1450 return -1;
1451
1452 if (proto->init && proto->init(psmouse) < 0)
1453 return -1;
1454
1455 psmouse->type = proto->type;
6b9d363c
DT
1456 selected_proto = proto;
1457 } else {
b7802c5c
DT
1458 psmouse->type = psmouse_extensions(psmouse,
1459 psmouse_max_proto, true);
6b9d363c
DT
1460 selected_proto = psmouse_protocol_by_type(psmouse->type);
1461 }
1462
1463 psmouse->ignore_parity = selected_proto->ignore_parity;
dbf4ccd6 1464
f0d5c6f4
DT
1465 /*
1466 * If mouse's packet size is 3 there is no point in polling the
1467 * device in hopes to detect protocol reset - we won't get less
1468 * than 3 bytes response anyhow.
1469 */
1470 if (psmouse->pktsize == 3)
1471 psmouse->resync_time = 0;
1472
1473 /*
1474 * Some smart KVMs fake response to POLL command returning just
1475 * 3 bytes and messing up our resync logic, so if initial poll
1476 * fails we won't try polling the device anymore. Hopefully
1477 * such KVM will maintain initially selected protocol.
1478 */
1479 if (psmouse->resync_time && psmouse->poll(psmouse))
1480 psmouse->resync_time = 0;
1481
08ffce45 1482 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
6b9d363c 1483 selected_proto->name, psmouse->vendor, psmouse->name);
dbf4ccd6 1484
2e5b636b
DT
1485 input_dev->name = psmouse->devname;
1486 input_dev->phys = psmouse->phys;
1487 input_dev->id.bustype = BUS_I8042;
1488 input_dev->id.vendor = 0x0002;
1489 input_dev->id.product = psmouse->type;
1490 input_dev->id.version = psmouse->model;
dbf4ccd6
DT
1491
1492 return 0;
1493}
1494
1da177e4
LT
1495/*
1496 * psmouse_connect() is a callback from the serio module when
1497 * an unhandled serio port is found.
1498 */
1499static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1500{
1501 struct psmouse *psmouse, *parent = NULL;
2e5b636b 1502 struct input_dev *input_dev;
72155615 1503 int retval = 0, error = -ENOMEM;
1da177e4 1504
c14471dc 1505 mutex_lock(&psmouse_mutex);
04df1925 1506
1da177e4
LT
1507 /*
1508 * If this is a pass-through port deactivate parent so the device
1509 * connected to this port can be successfully identified
1510 */
1511 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1512 parent = serio_get_drvdata(serio->parent);
1513 psmouse_deactivate(parent);
1514 }
1515
2e5b636b
DT
1516 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1517 input_dev = input_allocate_device();
1518 if (!psmouse || !input_dev)
72155615 1519 goto err_free;
1da177e4 1520
1da177e4 1521 ps2_init(&psmouse->ps2dev, serio);
8bf020ee 1522 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
2e5b636b 1523 psmouse->dev = input_dev;
08ffce45 1524 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
dbf4ccd6 1525
1da177e4
LT
1526 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1527
1528 serio_set_drvdata(serio, psmouse);
1529
72155615
DT
1530 error = serio_open(serio, drv);
1531 if (error)
1532 goto err_clear_drvdata;
1da177e4 1533
66bc2f51
SA
1534 /* give PT device some time to settle down before probing */
1535 if (serio->id.type == SERIO_PS_PSTHRU)
1536 usleep_range(10000, 15000);
1537
1da177e4 1538 if (psmouse_probe(psmouse) < 0) {
72155615
DT
1539 error = -ENODEV;
1540 goto err_close_serio;
1da177e4
LT
1541 }
1542
1543 psmouse->rate = psmouse_rate;
1544 psmouse->resolution = psmouse_resolution;
1545 psmouse->resetafter = psmouse_resetafter;
f0d5c6f4 1546 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1da177e4 1547 psmouse->smartscroll = psmouse_smartscroll;
1da177e4 1548
dbf4ccd6 1549 psmouse_switch_protocol(psmouse, NULL);
1da177e4 1550
1da177e4 1551 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1da177e4
LT
1552 psmouse_initialize(psmouse);
1553
72155615
DT
1554 error = input_register_device(psmouse->dev);
1555 if (error)
1556 goto err_protocol_disconnect;
2e5b636b 1557
1da177e4
LT
1558 if (parent && parent->pt_activate)
1559 parent->pt_activate(parent);
1560
72155615
DT
1561 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1562 if (error)
1563 goto err_pt_deactivate;
1da177e4
LT
1564
1565 psmouse_activate(psmouse);
1566
72155615 1567 out:
04df1925 1568 /* If this is a pass-through port the parent needs to be re-activated */
1da177e4
LT
1569 if (parent)
1570 psmouse_activate(parent);
1571
c14471dc 1572 mutex_unlock(&psmouse_mutex);
1da177e4 1573 return retval;
72155615
DT
1574
1575 err_pt_deactivate:
1576 if (parent && parent->pt_deactivate)
1577 parent->pt_deactivate(parent);
746b31a9
AS
1578 input_unregister_device(psmouse->dev);
1579 input_dev = NULL; /* so we don't try to free it below */
72155615
DT
1580 err_protocol_disconnect:
1581 if (psmouse->disconnect)
1582 psmouse->disconnect(psmouse);
1583 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1584 err_close_serio:
1585 serio_close(serio);
1586 err_clear_drvdata:
1587 serio_set_drvdata(serio, NULL);
1588 err_free:
1589 input_free_device(input_dev);
1590 kfree(psmouse);
1591
1592 retval = error;
1593 goto out;
1da177e4
LT
1594}
1595
1da177e4
LT
1596static int psmouse_reconnect(struct serio *serio)
1597{
1598 struct psmouse *psmouse = serio_get_drvdata(serio);
1599 struct psmouse *parent = NULL;
ef110b24 1600 unsigned char type;
1da177e4
LT
1601 int rc = -1;
1602
c14471dc 1603 mutex_lock(&psmouse_mutex);
04df1925 1604
1da177e4
LT
1605 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1606 parent = serio_get_drvdata(serio->parent);
1607 psmouse_deactivate(parent);
1608 }
1609
1610 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1611
1612 if (psmouse->reconnect) {
1613 if (psmouse->reconnect(psmouse))
1614 goto out;
ef110b24
DT
1615 } else {
1616 psmouse_reset(psmouse);
1617
1618 if (psmouse_probe(psmouse) < 0)
1619 goto out;
1620
1621 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1622 if (psmouse->type != type)
1623 goto out;
b7802c5c 1624 }
1da177e4 1625
b5d21704
DT
1626 /*
1627 * OK, the device type (and capabilities) match the old one,
1628 * we can continue using it, complete initialization
1da177e4
LT
1629 */
1630 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1631
1632 psmouse_initialize(psmouse);
1633
1634 if (parent && parent->pt_activate)
1635 parent->pt_activate(parent);
1636
1637 psmouse_activate(psmouse);
1638 rc = 0;
1639
1640out:
1641 /* If this is a pass-through port the parent waits to be activated */
1642 if (parent)
1643 psmouse_activate(parent);
1644
c14471dc 1645 mutex_unlock(&psmouse_mutex);
1da177e4
LT
1646 return rc;
1647}
1648
1649static struct serio_device_id psmouse_serio_ids[] = {
1650 {
1651 .type = SERIO_8042,
1652 .proto = SERIO_ANY,
1653 .id = SERIO_ANY,
1654 .extra = SERIO_ANY,
1655 },
1656 {
1657 .type = SERIO_PS_PSTHRU,
1658 .proto = SERIO_ANY,
1659 .id = SERIO_ANY,
1660 .extra = SERIO_ANY,
1661 },
1662 { 0 }
1663};
1664
1665MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1666
1667static struct serio_driver psmouse_drv = {
1668 .driver = {
1669 .name = "psmouse",
1670 },
1671 .description = DRIVER_DESC,
1672 .id_table = psmouse_serio_ids,
1673 .interrupt = psmouse_interrupt,
1674 .connect = psmouse_connect,
1675 .reconnect = psmouse_reconnect,
1676 .disconnect = psmouse_disconnect,
1677 .cleanup = psmouse_cleanup,
1678};
1679
cfe9e888
DT
1680ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1681 char *buf)
1da177e4
LT
1682{
1683 struct serio *serio = to_serio_port(dev);
cfe9e888
DT
1684 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1685 struct psmouse *psmouse;
1da177e4 1686
cfe9e888
DT
1687 psmouse = serio_get_drvdata(serio);
1688
59b01513 1689 return attr->show(psmouse, attr->data, buf);
1da177e4
LT
1690}
1691
cfe9e888
DT
1692ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1693 const char *buf, size_t count)
1da177e4
LT
1694{
1695 struct serio *serio = to_serio_port(dev);
cfe9e888
DT
1696 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1697 struct psmouse *psmouse, *parent = NULL;
1da177e4
LT
1698 int retval;
1699
c14471dc 1700 retval = mutex_lock_interruptible(&psmouse_mutex);
04df1925 1701 if (retval)
59b01513 1702 goto out;
04df1925 1703
cfe9e888
DT
1704 psmouse = serio_get_drvdata(serio);
1705
68d48221
AS
1706 if (attr->protect) {
1707 if (psmouse->state == PSMOUSE_IGNORE) {
1708 retval = -ENODEV;
1709 goto out_unlock;
1710 }
1da177e4 1711
68d48221
AS
1712 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1713 parent = serio_get_drvdata(serio->parent);
1714 psmouse_deactivate(parent);
1715 }
dbf4ccd6 1716
68d48221
AS
1717 psmouse_deactivate(psmouse);
1718 }
1da177e4 1719
cfe9e888 1720 retval = attr->set(psmouse, attr->data, buf, count);
1da177e4 1721
68d48221
AS
1722 if (attr->protect) {
1723 if (retval != -ENODEV)
1724 psmouse_activate(psmouse);
dbf4ccd6 1725
68d48221
AS
1726 if (parent)
1727 psmouse_activate(parent);
1728 }
1da177e4 1729
c14471dc
IM
1730 out_unlock:
1731 mutex_unlock(&psmouse_mutex);
59b01513 1732 out:
1da177e4
LT
1733 return retval;
1734}
1735
cfe9e888
DT
1736static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1737{
eb5d5829 1738 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
cfe9e888 1739
eb5d5829 1740 return sprintf(buf, "%u\n", *field);
cfe9e888
DT
1741}
1742
1743static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1744{
eb5d5829 1745 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
76496e7a
JD
1746 unsigned int value;
1747 int err;
cfe9e888 1748
76496e7a
JD
1749 err = kstrtouint(buf, 10, &value);
1750 if (err)
1751 return err;
eb5d5829 1752
cfe9e888
DT
1753 *field = value;
1754
1755 return count;
1756}
1757
1758static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
dbf4ccd6
DT
1759{
1760 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1761}
1762
cfe9e888 1763static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
dbf4ccd6
DT
1764{
1765 struct serio *serio = psmouse->ps2dev.serio;
1766 struct psmouse *parent = NULL;
72155615
DT
1767 struct input_dev *old_dev, *new_dev;
1768 const struct psmouse_protocol *proto, *old_proto;
1769 int error;
dbf4ccd6
DT
1770 int retry = 0;
1771
72155615
DT
1772 proto = psmouse_protocol_by_name(buf, count);
1773 if (!proto)
dbf4ccd6
DT
1774 return -EINVAL;
1775
1776 if (psmouse->type == proto->type)
1777 return count;
1778
72155615
DT
1779 new_dev = input_allocate_device();
1780 if (!new_dev)
2e5b636b
DT
1781 return -ENOMEM;
1782
09822582 1783 while (!list_empty(&serio->children)) {
dbf4ccd6 1784 if (++retry > 3) {
b5d21704
DT
1785 psmouse_warn(psmouse,
1786 "failed to destroy children ports, protocol change aborted.\n");
2e5b636b 1787 input_free_device(new_dev);
dbf4ccd6
DT
1788 return -EIO;
1789 }
1790
c14471dc 1791 mutex_unlock(&psmouse_mutex);
dbf4ccd6 1792 serio_unregister_child_port(serio);
c14471dc 1793 mutex_lock(&psmouse_mutex);
dbf4ccd6 1794
2e5b636b
DT
1795 if (serio->drv != &psmouse_drv) {
1796 input_free_device(new_dev);
dbf4ccd6 1797 return -ENODEV;
2e5b636b 1798 }
dbf4ccd6 1799
2e5b636b
DT
1800 if (psmouse->type == proto->type) {
1801 input_free_device(new_dev);
dbf4ccd6 1802 return count; /* switched by other thread */
2e5b636b 1803 }
dbf4ccd6
DT
1804 }
1805
1806 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1807 parent = serio_get_drvdata(serio->parent);
1808 if (parent->pt_deactivate)
1809 parent->pt_deactivate(parent);
1810 }
1811
72155615
DT
1812 old_dev = psmouse->dev;
1813 old_proto = psmouse_protocol_by_type(psmouse->type);
1814
dbf4ccd6
DT
1815 if (psmouse->disconnect)
1816 psmouse->disconnect(psmouse);
1817
1818 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
dbf4ccd6 1819
2e5b636b 1820 psmouse->dev = new_dev;
dbf4ccd6
DT
1821 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1822
1823 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1824 psmouse_reset(psmouse);
1825 /* default to PSMOUSE_PS2 */
1826 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1827 }
1828
1829 psmouse_initialize(psmouse);
1830 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1831
72155615
DT
1832 error = input_register_device(psmouse->dev);
1833 if (error) {
1834 if (psmouse->disconnect)
1835 psmouse->disconnect(psmouse);
1836
1837 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1838 input_free_device(new_dev);
1839 psmouse->dev = old_dev;
1840 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1841 psmouse_switch_protocol(psmouse, old_proto);
1842 psmouse_initialize(psmouse);
1843 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1844
1845 return error;
1846 }
1847
1848 input_unregister_device(old_dev);
dbf4ccd6
DT
1849
1850 if (parent && parent->pt_activate)
1851 parent->pt_activate(parent);
1852
1853 return count;
1854}
1855
cfe9e888 1856static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1da177e4 1857{
76496e7a
JD
1858 unsigned int value;
1859 int err;
1da177e4 1860
76496e7a
JD
1861 err = kstrtouint(buf, 10, &value);
1862 if (err)
1863 return err;
1da177e4
LT
1864
1865 psmouse->set_rate(psmouse, value);
1866 return count;
1867}
1868
cfe9e888 1869static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1da177e4 1870{
76496e7a
JD
1871 unsigned int value;
1872 int err;
1da177e4 1873
76496e7a
JD
1874 err = kstrtouint(buf, 10, &value);
1875 if (err)
1876 return err;
1da177e4
LT
1877
1878 psmouse->set_resolution(psmouse, value);
1879 return count;
1880}
1881
1da177e4 1882
9bbb9e5a 1883static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
1da177e4 1884{
e38de678 1885 const struct psmouse_protocol *proto;
1da177e4
LT
1886
1887 if (!val)
1888 return -EINVAL;
1889
dbf4ccd6 1890 proto = psmouse_protocol_by_name(val, strlen(val));
1da177e4 1891
dbf4ccd6
DT
1892 if (!proto || !proto->maxproto)
1893 return -EINVAL;
1da177e4 1894
dbf4ccd6 1895 *((unsigned int *)kp->arg) = proto->type;
1da177e4 1896
541e316a 1897 return 0;
1da177e4
LT
1898}
1899
9bbb9e5a 1900static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
1da177e4 1901{
dbf4ccd6
DT
1902 int type = *((unsigned int *)kp->arg);
1903
3d4c3aa9 1904 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
1da177e4
LT
1905}
1906
1907static int __init psmouse_init(void)
1908{
153a9df0
AM
1909 int err;
1910
7705d548
DT
1911 lifebook_module_init();
1912 synaptics_module_init();
ca94ec43 1913 hgpk_module_init();
7705d548 1914
24dde60f 1915 kpsmoused_wq = alloc_ordered_workqueue("kpsmoused", 0);
f0d5c6f4 1916 if (!kpsmoused_wq) {
b5d21704 1917 pr_err("failed to create kpsmoused workqueue\n");
f0d5c6f4
DT
1918 return -ENOMEM;
1919 }
1920
153a9df0
AM
1921 err = serio_register_driver(&psmouse_drv);
1922 if (err)
1923 destroy_workqueue(kpsmoused_wq);
f0d5c6f4 1924
153a9df0 1925 return err;
1da177e4
LT
1926}
1927
1928static void __exit psmouse_exit(void)
1929{
1930 serio_unregister_driver(&psmouse_drv);
f0d5c6f4 1931 destroy_workqueue(kpsmoused_wq);
1da177e4
LT
1932}
1933
1934module_init(psmouse_init);
1935module_exit(psmouse_exit);