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