blob: f73b47b8c578cfbe779d7204f3fa11992979691a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Dmitry Torokhovb5d21702011-10-10 18:27:03 -070014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15#define psmouse_fmt(fmt) fmt
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/delay.h>
18#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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>
Ingo Molnarc14471d2006-02-19 00:22:11 -050025#include <linux/mutex.h>
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "psmouse.h"
28#include "synaptics.h"
29#include "logips2pp.h"
30#include "alps.h"
Andres Salomondf08ef22008-09-16 12:30:34 -040031#include "hgpk.h"
Kenan Esau02d7f582005-05-29 02:30:22 -050032#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050033#include "trackpoint.h"
Stefan Lucke24bf10a2007-02-18 01:49:10 -050034#include "touchkit_ps2.h"
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040035#include "elantech.h"
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -070036#include "sentelic.h"
Dudley Du0799a922013-01-05 00:14:22 -080037#include "cypress_ps2.h"
Hans de Goede3ace3682014-09-12 17:24:47 -070038#include "focaltech.h"
Thomas Hellstrom8b8be512015-04-14 10:06:38 -070039#include "vmmouse.h"
Chris Diamand98ee3772016-01-27 17:04:35 -080040#include "byd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define DRIVER_DESC "PS/2 mouse driver"
43
44MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
45MODULE_DESCRIPTION(DRIVER_DESC);
46MODULE_LICENSE("GPL");
47
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050048static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Rusty Russell9bbb9e52010-08-11 23:04:12 -060049static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
50static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
Luis R. Rodriguez9c278472015-05-27 11:09:38 +093051static const struct kernel_param_ops param_ops_proto_abbrev = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -060052 .set = psmouse_set_maxproto,
53 .get = psmouse_get_maxproto,
54};
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050057MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static unsigned int psmouse_resolution = 200;
60module_param_named(resolution, psmouse_resolution, uint, 0644);
61MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
62
63static unsigned int psmouse_rate = 100;
64module_param_named(rate, psmouse_rate, uint, 0644);
65MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
66
Shailendra Vermafeb9eba2015-05-26 14:07:12 -070067static bool psmouse_smartscroll = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
69MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
70
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050071static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072module_param_named(resetafter, psmouse_resetafter, uint, 0644);
73MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
74
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050075static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050076module_param_named(resync_time, psmouse_resync_time, uint, 0644);
77MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
78
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050079PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
80 NULL,
81 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
82PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
83 (void *) offsetof(struct psmouse, rate),
84 psmouse_show_int_attr, psmouse_attr_set_rate);
85PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
86 (void *) offsetof(struct psmouse, resolution),
87 psmouse_show_int_attr, psmouse_attr_set_resolution);
88PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
89 (void *) offsetof(struct psmouse, resetafter),
90 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050091PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
92 (void *) offsetof(struct psmouse, resync_time),
93 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050094
95static struct attribute *psmouse_attributes[] = {
96 &psmouse_attr_protocol.dattr.attr,
97 &psmouse_attr_rate.dattr.attr,
98 &psmouse_attr_resolution.dattr.attr,
99 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500100 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500101 NULL
102};
103
104static struct attribute_group psmouse_attribute_group = {
105 .attrs = psmouse_attributes,
106};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500108/*
Ingo Molnarc14471d2006-02-19 00:22:11 -0500109 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500110 * (connecting, disconnecting, changing rate or resolution via
111 * sysfs). We could use a per-device semaphore but since there
112 * rarely more than one PS/2 mouse connected and since semaphore
113 * is taken in "slow" paths it is not worth it.
114 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500115static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500116
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500117static struct workqueue_struct *kpsmoused_wq;
118
Benjamin Tissoires19ba1eb2017-02-07 23:58:02 -0800119static void psmouse_report_standard_buttons(struct input_dev *dev, u8 buttons)
120{
121 input_report_key(dev, BTN_LEFT, buttons & BIT(0));
122 input_report_key(dev, BTN_MIDDLE, buttons & BIT(2));
123 input_report_key(dev, BTN_RIGHT, buttons & BIT(1));
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*
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 */
Daniel Drake7968a5d2011-11-08 00:00:35 -0800130psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500132 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 unsigned char *packet = psmouse->packet;
134
135 if (psmouse->pktcnt < psmouse->pktsize)
136 return PSMOUSE_GOOD_DATA;
137
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800138 /* Full packet accumulated, process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Dmitry Torokhov085fa802017-03-03 15:29:00 -0800140 switch (psmouse->protocol->type) {
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800141 case PSMOUSE_IMPS:
142 /* IntelliMouse has scroll wheel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800144 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800146 case PSMOUSE_IMEX:
147 /* Scroll wheel and buttons on IntelliMouse Explorer */
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400148 switch (packet[3] & 0xC0) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700149 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
150 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
151 break;
152 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
153 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
154 break;
155 case 0x00:
156 case 0xC0:
157 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
158 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
159 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
160 break;
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400161 }
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800162 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800164 case PSMOUSE_GENPS:
165 /* Report scroll buttons on NetMice */
166 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800168 /* Extra buttons on Genius NewNet 3D */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
170 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800173 case PSMOUSE_THINKPS:
174 /* Extra button on ThinkingMouse */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800177 /*
178 * Without this bit of weirdness moving up gives wildly
179 * high Y changes.
180 */
181 packet[1] |= (packet[0] & 0x40) << 1;
182 break;
183
184 case PSMOUSE_CORTRON:
185 /*
186 * Cortron PS2 Trackball reports SIDE button in the
187 * 4th bit of the first byte.
188 */
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400189 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
190 packet[0] |= 0x08;
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800191 break;
192
193 default:
194 break;
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400195 }
196
Dmitry Torokhov0a88d602015-11-27 20:29:22 -0800197 /* Generic PS/2 Mouse */
Benjamin Tissoires19ba1eb2017-02-07 23:58:02 -0800198 psmouse_report_standard_buttons(dev,
199 packet[0] | psmouse->extra_buttons);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
202 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
203
204 input_sync(dev);
205
206 return PSMOUSE_FULL_PACKET;
207}
208
Andres Salomon8bf020e2008-09-16 12:30:33 -0400209void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
210 unsigned long delay)
211{
212 queue_delayed_work(kpsmoused_wq, work, delay);
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500216 * __psmouse_set_state() sets new psmouse state and resets all flags.
217 */
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500218static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
219{
220 psmouse->state = new_state;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700221 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500222 psmouse->ps2dev.flags = 0;
223 psmouse->last = jiffies;
224}
225
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500226/*
227 * psmouse_set_state() sets new psmouse state and resets all flags and
228 * counters while holding serio lock so fighting with interrupt handler
229 * is not a concern.
230 */
Andres Salomona48cf5f2008-09-16 12:30:33 -0400231void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500232{
233 serio_pause_rx(psmouse->ps2dev.serio);
234 __psmouse_set_state(psmouse, new_state);
235 serio_continue_rx(psmouse->ps2dev.serio);
236}
237
238/*
239 * psmouse_handle_byte() processes one byte of the input data stream
240 * by calling corresponding protocol handler.
241 */
David Howells7d12e782006-10-05 14:55:46 +0100242static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500243{
David Howells7d12e782006-10-05 14:55:46 +0100244 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500245
246 switch (rc) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700247 case PSMOUSE_BAD_DATA:
248 if (psmouse->state == PSMOUSE_ACTIVATED) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700249 psmouse_warn(psmouse,
250 "%s at %s lost sync at byte %d\n",
251 psmouse->name, psmouse->phys,
252 psmouse->pktcnt);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700253 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
254 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700255 psmouse_notice(psmouse,
256 "issuing reconnect request\n");
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700257 serio_reconnect(psmouse->ps2dev.serio);
258 return -1;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500259 }
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700260 }
261 psmouse->pktcnt = 0;
262 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500263
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700264 case PSMOUSE_FULL_PACKET:
265 psmouse->pktcnt = 0;
266 if (psmouse->out_of_sync_cnt) {
267 psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700268 psmouse_notice(psmouse,
269 "%s at %s - driver resynced.\n",
270 psmouse->name, psmouse->phys);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700271 }
272 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500273
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700274 case PSMOUSE_GOOD_DATA:
275 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500276 }
277 return 0;
278}
279
Benjamin Tissoires19ba1eb2017-02-07 23:58:02 -0800280static void psmouse_handle_oob_data(struct psmouse *psmouse, u8 data)
281{
282 switch (psmouse->oob_data_type) {
283 case PSMOUSE_OOB_NONE:
284 psmouse->oob_data_type = data;
285 break;
286
287 case PSMOUSE_OOB_EXTRA_BTNS:
288 psmouse_report_standard_buttons(psmouse->dev, data);
289 input_sync(psmouse->dev);
290
291 psmouse->extra_buttons = data;
292 psmouse->oob_data_type = PSMOUSE_OOB_NONE;
293 break;
294
295 default:
296 psmouse_warn(psmouse,
297 "unknown OOB_DATA type: 0x%02x\n",
298 psmouse->oob_data_type);
299 psmouse->oob_data_type = PSMOUSE_OOB_NONE;
300 break;
301 }
302}
303
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500304/*
305 * psmouse_interrupt() handles incoming characters, either passing them
306 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100309 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (psmouse->state == PSMOUSE_IGNORE)
314 goto out;
315
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700316 if (unlikely((flags & SERIO_TIMEOUT) ||
Dmitry Torokhov085fa802017-03-03 15:29:00 -0800317 ((flags & SERIO_PARITY) &&
318 !psmouse->protocol->ignore_parity))) {
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (psmouse->state == PSMOUSE_ACTIVATED)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700321 psmouse_warn(psmouse,
322 "bad data from KBC -%s%s\n",
323 flags & SERIO_TIMEOUT ? " timeout" : "",
324 flags & SERIO_PARITY ? " bad parity" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 ps2_cmd_aborted(&psmouse->ps2dev);
326 goto out;
327 }
328
Benjamin Tissoires19ba1eb2017-02-07 23:58:02 -0800329 if (flags & SERIO_OOB_DATA) {
330 psmouse_handle_oob_data(psmouse, data);
331 goto out;
332 }
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
335 if (ps2_handle_ack(&psmouse->ps2dev, data))
336 goto out;
337
338 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
339 if (ps2_handle_response(&psmouse->ps2dev, data))
340 goto out;
341
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500342 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto out;
344
345 if (psmouse->state == PSMOUSE_ACTIVATED &&
346 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700347 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
348 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500349 psmouse->badbyte = psmouse->packet[0];
350 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400351 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovad530772015-11-27 20:47:08 -0800356
357 /* Check if this is a new device announcement (0xAA 0x00) */
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500358 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400359 if (psmouse->pktcnt == 1) {
360 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Zephaniah E. Hull535650f2009-05-14 22:02:33 -0700364 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
Dmitry Torokhov085fa802017-03-03 15:29:00 -0800365 (psmouse->protocol->type == PSMOUSE_HGPK &&
Zephaniah E. Hull535650f2009-05-14 22:02:33 -0700366 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500367 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
368 serio_reconnect(serio);
369 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Dmitry Torokhovad530772015-11-27 20:47:08 -0800371
372 /* Not a new device, try processing first byte normally */
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500373 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100374 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500375 goto out;
376
377 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Dmitry Torokhovad530772015-11-27 20:47:08 -0800380 /*
381 * See if we need to force resync because mouse was idle for
382 * too long.
383 */
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500384 if (psmouse->state == PSMOUSE_ACTIVATED &&
385 psmouse->pktcnt == 1 && psmouse->resync_time &&
386 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
387 psmouse->badbyte = psmouse->packet[0];
388 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400389 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500390 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500392
393 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100394 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500395
396 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return IRQ_HANDLED;
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
401 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
402 * using sliced syntax, understood by advanced devices, such as Logitech
403 * or Synaptics touchpads. The command is encoded as:
404 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
405 * is the command.
406 */
407int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
408{
409 int i;
410
411 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
412 return -1;
413
414 for (i = 6; i >= 0; i -= 2) {
415 unsigned char d = (command >> i) & 3;
416 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
417 return -1;
418 }
419
420 return 0;
421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
424 * psmouse_reset() resets the mouse into power-on state.
425 */
426int psmouse_reset(struct psmouse *psmouse)
427{
428 unsigned char param[2];
429
430 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
431 return -1;
432
433 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
434 return -1;
435
436 return 0;
437}
438
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800439/*
440 * Here we set the mouse resolution.
441 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800442void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
443{
444 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
445 unsigned char p;
446
447 if (resolution == 0 || resolution > 200)
448 resolution = 200;
449
450 p = params[resolution / 50];
451 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
452 psmouse->resolution = 25 << p;
453}
454
455/*
456 * Here we set the mouse report rate.
457 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800458static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
459{
460 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
461 unsigned char r;
462 int i = 0;
463
464 while (rates[i] > rate) i++;
465 r = rates[i];
466 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
467 psmouse->rate = r;
468}
469
470/*
Mathias Gottschlag4ec212f2015-03-07 13:32:10 -0800471 * Here we set the mouse scaling.
472 */
Mathias Gottschlag4ec212f2015-03-07 13:32:10 -0800473static void psmouse_set_scale(struct psmouse *psmouse, enum psmouse_scale scale)
474{
475 ps2_command(&psmouse->ps2dev, NULL,
476 scale == PSMOUSE_SCALE21 ? PSMOUSE_CMD_SETSCALE21 :
477 PSMOUSE_CMD_SETSCALE11);
478}
479
480/*
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800481 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
482 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800483static int psmouse_poll(struct psmouse *psmouse)
484{
485 return ps2_command(&psmouse->ps2dev, psmouse->packet,
486 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
487}
488
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800489static bool psmouse_check_pnp_id(const char *id, const char * const ids[])
490{
491 int i;
492
493 for (i = 0; ids[i]; i++)
494 if (!strcasecmp(id, ids[i]))
495 return true;
496
497 return false;
498}
499
Hans de Goede2c75ada2014-09-11 10:14:09 -0700500/*
501 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
502 */
503bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
504{
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800505 struct serio *serio = psmouse->ps2dev.serio;
506 char *p, *fw_id_copy, *save_ptr;
507 bool found = false;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700508
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800509 if (strncmp(serio->firmware_id, "PNP: ", 5))
510 return false;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700511
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800512 fw_id_copy = kstrndup(&serio->firmware_id[5],
513 sizeof(serio->firmware_id) - 5,
514 GFP_KERNEL);
515 if (!fw_id_copy)
516 return false;
517
518 save_ptr = fw_id_copy;
519 while ((p = strsep(&fw_id_copy, " ")) != NULL) {
520 if (psmouse_check_pnp_id(p, ids)) {
521 found = true;
522 break;
523 }
524 }
525
526 kfree(save_ptr);
527 return found;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700528}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530/*
531 * Genius NetMouse magic init.
532 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700533static int genius_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct ps2dev *ps2dev = &psmouse->ps2dev;
536 unsigned char param[4];
537
538 param[0] = 3;
539 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
540 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
541 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
542 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
543 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
544
545 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
546 return -1;
547
548 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800549 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700550 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
551 __set_bit(BTN_SIDE, psmouse->dev->keybit);
552 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500555 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 psmouse->pktsize = 4;
557 }
558
559 return 0;
560}
561
562/*
563 * IntelliMouse magic init.
564 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700565static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 struct ps2dev *ps2dev = &psmouse->ps2dev;
568 unsigned char param[2];
569
570 param[0] = 200;
571 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
572 param[0] = 100;
573 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
574 param[0] = 80;
575 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
576 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
577
578 if (param[0] != 3)
579 return -1;
580
581 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700582 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
583 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800585 if (!psmouse->vendor)
586 psmouse->vendor = "Generic";
587 if (!psmouse->name)
588 psmouse->name = "Wheel Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 psmouse->pktsize = 4;
590 }
591
592 return 0;
593}
594
595/*
596 * Try IntelliMouse/Explorer magic init.
597 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700598static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct ps2dev *ps2dev = &psmouse->ps2dev;
601 unsigned char param[2];
602
603 intellimouse_detect(psmouse, 0);
604
605 param[0] = 200;
606 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
607 param[0] = 200;
608 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
609 param[0] = 80;
610 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
611 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
612
613 if (param[0] != 4)
614 return -1;
615
Dmitry Torokhovad530772015-11-27 20:47:08 -0800616 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400617 param[0] = 200;
618 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
619 param[0] = 80;
620 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
621 param[0] = 40;
622 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700625 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
626 __set_bit(REL_WHEEL, psmouse->dev->relbit);
627 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
628 __set_bit(BTN_SIDE, psmouse->dev->keybit);
629 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800631 if (!psmouse->vendor)
632 psmouse->vendor = "Generic";
633 if (!psmouse->name)
634 psmouse->name = "Explorer Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 psmouse->pktsize = 4;
636 }
637
638 return 0;
639}
640
641/*
642 * Kensington ThinkingMouse / ExpertMouse magic init.
643 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700644static int thinking_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 struct ps2dev *ps2dev = &psmouse->ps2dev;
647 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400648 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int i;
650
651 param[0] = 10;
652 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
653 param[0] = 0;
654 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400655 for (i = 0; i < ARRAY_SIZE(seq); i++) {
656 param[0] = seq[i];
657 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
660
661 if (param[0] != 2)
662 return -1;
663
664 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800665 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700666 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 psmouse->vendor = "Kensington";
669 psmouse->name = "ThinkingMouse";
670 }
671
672 return 0;
673}
674
675/*
676 * Bare PS/2 protocol "detection". Always succeeds.
677 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700678static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500680 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800681 if (!psmouse->vendor)
682 psmouse->vendor = "Generic";
683 if (!psmouse->name)
684 psmouse->name = "Mouse";
685
Dmitry Torokhovad530772015-11-27 20:47:08 -0800686 /*
687 * We have no way of figuring true number of buttons so let's
688 * assume that the device has 3.
689 */
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800690 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 return 0;
694}
695
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400696/*
697 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
698 * must be forced by sysfs protocol writing.
699 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700700static int cortron_detect(struct psmouse *psmouse, bool set_properties)
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400701{
702 if (set_properties) {
703 psmouse->vendor = "Cortron";
704 psmouse->name = "PS/2 Trackball";
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800705
706 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700707 __set_bit(BTN_SIDE, psmouse->dev->keybit);
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400708 }
709
710 return 0;
711}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500712
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800713static const struct psmouse_protocol psmouse_protocols[] = {
714 {
715 .type = PSMOUSE_PS2,
716 .name = "PS/2",
717 .alias = "bare",
718 .maxproto = true,
719 .ignore_parity = true,
720 .detect = ps2bare_detect,
Dmitry Torokhovec6184b2015-11-24 12:58:46 -0800721 .try_passthru = true,
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800722 },
723#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
724 {
725 .type = PSMOUSE_PS2PP,
726 .name = "PS2++",
727 .alias = "logitech",
Dmitry Torokhov190e2032015-12-02 11:02:39 -0800728 .detect = ps2pp_detect,
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800729 },
730#endif
731 {
732 .type = PSMOUSE_THINKPS,
733 .name = "ThinkPS/2",
734 .alias = "thinkps",
735 .detect = thinking_detect,
736 },
737#ifdef CONFIG_MOUSE_PS2_CYPRESS
738 {
739 .type = PSMOUSE_CYPRESS,
740 .name = "CyPS/2",
741 .alias = "cypress",
742 .detect = cypress_detect,
743 .init = cypress_init,
744 },
745#endif
746 {
747 .type = PSMOUSE_GENPS,
748 .name = "GenPS/2",
749 .alias = "genius",
750 .detect = genius_detect,
751 },
752 {
753 .type = PSMOUSE_IMPS,
754 .name = "ImPS/2",
755 .alias = "imps",
756 .maxproto = true,
757 .ignore_parity = true,
758 .detect = intellimouse_detect,
Dmitry Torokhovec6184b2015-11-24 12:58:46 -0800759 .try_passthru = true,
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800760 },
761 {
762 .type = PSMOUSE_IMEX,
763 .name = "ImExPS/2",
764 .alias = "exps",
765 .maxproto = true,
766 .ignore_parity = true,
767 .detect = im_explorer_detect,
Dmitry Torokhovec6184b2015-11-24 12:58:46 -0800768 .try_passthru = true,
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800769 },
770#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
771 {
772 .type = PSMOUSE_SYNAPTICS,
773 .name = "SynPS/2",
774 .alias = "synaptics",
775 .detect = synaptics_detect,
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800776 .init = synaptics_init_absolute,
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800777 },
778 {
779 .type = PSMOUSE_SYNAPTICS_RELATIVE,
780 .name = "SynRelPS/2",
781 .alias = "synaptics-relative",
782 .detect = synaptics_detect,
783 .init = synaptics_init_relative,
784 },
785#endif
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800786#ifdef CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS
787 {
788 .type = PSMOUSE_SYNAPTICS_SMBUS,
789 .name = "SynSMBus",
790 .alias = "synaptics-smbus",
791 .detect = synaptics_detect,
792 .init = synaptics_init_smbus,
793 .smbus_companion = true,
794 },
795#endif
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800796#ifdef CONFIG_MOUSE_PS2_ALPS
797 {
798 .type = PSMOUSE_ALPS,
799 .name = "AlpsPS/2",
800 .alias = "alps",
801 .detect = alps_detect,
802 .init = alps_init,
803 },
804#endif
805#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
806 {
807 .type = PSMOUSE_LIFEBOOK,
808 .name = "LBPS/2",
809 .alias = "lifebook",
Dmitry Torokhovc378b512015-11-27 21:17:40 -0800810 .detect = lifebook_detect,
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800811 .init = lifebook_init,
812 },
813#endif
814#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
815 {
816 .type = PSMOUSE_TRACKPOINT,
817 .name = "TPPS/2",
818 .alias = "trackpoint",
819 .detect = trackpoint_detect,
Dmitry Torokhovec6184b2015-11-24 12:58:46 -0800820 .try_passthru = true,
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800821 },
822#endif
823#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
824 {
825 .type = PSMOUSE_TOUCHKIT_PS2,
826 .name = "touchkitPS/2",
827 .alias = "touchkit",
828 .detect = touchkit_ps2_detect,
829 },
830#endif
831#ifdef CONFIG_MOUSE_PS2_OLPC
832 {
833 .type = PSMOUSE_HGPK,
834 .name = "OLPC HGPK",
835 .alias = "hgpk",
836 .detect = hgpk_detect,
837 },
838#endif
839#ifdef CONFIG_MOUSE_PS2_ELANTECH
840 {
841 .type = PSMOUSE_ELANTECH,
842 .name = "ETPS/2",
843 .alias = "elantech",
844 .detect = elantech_detect,
845 .init = elantech_init,
846 },
847#endif
848#ifdef CONFIG_MOUSE_PS2_SENTELIC
849 {
850 .type = PSMOUSE_FSP,
851 .name = "FSPPS/2",
852 .alias = "fsp",
853 .detect = fsp_detect,
854 .init = fsp_init,
855 },
856#endif
857 {
858 .type = PSMOUSE_CORTRON,
859 .name = "CortronPS/2",
860 .alias = "cortps",
861 .detect = cortron_detect,
862 },
863#ifdef CONFIG_MOUSE_PS2_FOCALTECH
864 {
865 .type = PSMOUSE_FOCALTECH,
866 .name = "FocalTechPS/2",
867 .alias = "focaltech",
868 .detect = focaltech_detect,
869 .init = focaltech_init,
870 },
871#endif
872#ifdef CONFIG_MOUSE_PS2_VMMOUSE
873 {
874 .type = PSMOUSE_VMMOUSE,
875 .name = VMMOUSE_PSNAME,
876 .alias = "vmmouse",
877 .detect = vmmouse_detect,
878 .init = vmmouse_init,
879 },
880#endif
Chris Diamand98ee3772016-01-27 17:04:35 -0800881#ifdef CONFIG_MOUSE_PS2_BYD
882 {
883 .type = PSMOUSE_BYD,
Richard Pospesel2d5f5612016-03-14 09:41:16 -0700884 .name = "BYDPS/2",
Chris Diamand98ee3772016-01-27 17:04:35 -0800885 .alias = "byd",
886 .detect = byd_detect,
887 .init = byd_init,
888 },
889#endif
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800890 {
891 .type = PSMOUSE_AUTO,
892 .name = "auto",
893 .alias = "any",
894 .maxproto = true,
895 },
896};
897
Dmitry Torokhovc378b512015-11-27 21:17:40 -0800898static const struct psmouse_protocol *__psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800899{
900 int i;
901
902 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
903 if (psmouse_protocols[i].type == type)
904 return &psmouse_protocols[i];
905
Dmitry Torokhovc378b512015-11-27 21:17:40 -0800906 return NULL;
907}
908
909static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
910{
911 const struct psmouse_protocol *proto;
912
913 proto = __psmouse_protocol_by_type(type);
914 if (proto)
915 return proto;
916
Dmitry Torokhov5fa75cf2015-11-27 21:08:28 -0800917 WARN_ON(1);
918 return &psmouse_protocols[0];
919}
920
921static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
922{
923 const struct psmouse_protocol *p;
924 int i;
925
926 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
927 p = &psmouse_protocols[i];
928
929 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
930 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
931 return &psmouse_protocols[i];
932 }
933
934 return NULL;
935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800938 * Apply default settings to the psmouse structure. Most of them will
939 * be overridden by individual protocol initialization routines.
940 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800941static void psmouse_apply_defaults(struct psmouse *psmouse)
942{
943 struct input_dev *input_dev = psmouse->dev;
944
945 memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
946 memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
947 memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
948 memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
949 memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));
950
951 __set_bit(EV_KEY, input_dev->evbit);
952 __set_bit(EV_REL, input_dev->evbit);
953
954 __set_bit(BTN_LEFT, input_dev->keybit);
955 __set_bit(BTN_RIGHT, input_dev->keybit);
956
957 __set_bit(REL_X, input_dev->relbit);
958 __set_bit(REL_Y, input_dev->relbit);
959
Hans de Goede01d4cd52014-09-08 14:44:05 -0700960 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
961
Dmitry Torokhov085fa802017-03-03 15:29:00 -0800962 psmouse->protocol = &psmouse_protocols[0];
963
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800964 psmouse->set_rate = psmouse_set_rate;
965 psmouse->set_resolution = psmouse_set_resolution;
Mathias Gottschlag4ec212f2015-03-07 13:32:10 -0800966 psmouse->set_scale = psmouse_set_scale;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800967 psmouse->poll = psmouse_poll;
968 psmouse->protocol_handler = psmouse_process_byte;
969 psmouse->pktsize = 3;
970 psmouse->reconnect = NULL;
Dmitry Torokhov0ab3fa52017-03-05 23:19:22 -0800971 psmouse->fast_reconnect = NULL;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800972 psmouse->disconnect = NULL;
973 psmouse->cleanup = NULL;
974 psmouse->pt_activate = NULL;
975 psmouse->pt_deactivate = NULL;
976}
977
Dmitry Torokhovc378b512015-11-27 21:17:40 -0800978static bool psmouse_try_protocol(struct psmouse *psmouse,
979 enum psmouse_type type,
980 unsigned int *max_proto,
981 bool set_properties, bool init_allowed)
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800982{
Dmitry Torokhovc378b512015-11-27 21:17:40 -0800983 const struct psmouse_protocol *proto;
984
985 proto = __psmouse_protocol_by_type(type);
986 if (!proto)
987 return false;
988
Dmitry Torokhovec6184b2015-11-24 12:58:46 -0800989 if (psmouse->ps2dev.serio->id.type == SERIO_PS_PSTHRU &&
990 !proto->try_passthru) {
991 return false;
992 }
993
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800994 if (set_properties)
995 psmouse_apply_defaults(psmouse);
996
Dmitry Torokhovc378b512015-11-27 21:17:40 -0800997 if (proto->detect(psmouse, set_properties) != 0)
998 return false;
999
1000 if (set_properties && proto->init && init_allowed) {
1001 if (proto->init(psmouse) != 0) {
1002 /*
1003 * We detected device, but init failed. Adjust
1004 * max_proto so we only try standard protocols.
1005 */
1006 if (*max_proto > PSMOUSE_IMEX)
1007 *max_proto = PSMOUSE_IMEX;
1008
1009 return false;
1010 }
1011 }
1012
1013 return true;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001014}
1015
1016/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
1018 * the mouse may have.
1019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020static int psmouse_extensions(struct psmouse *psmouse,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001021 unsigned int max_proto, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Jiri Kosina06989892009-11-16 22:12:13 -08001023 bool synaptics_hardware = false;
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001024 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Dmitry Torokhovad530772015-11-27 20:47:08 -08001026 /*
1027 * Always check for focaltech, this is safe as it uses pnp-id
1028 * matching.
1029 */
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001030 if (psmouse_try_protocol(psmouse, PSMOUSE_FOCALTECH,
1031 &max_proto, set_properties, false)) {
1032 if (max_proto > PSMOUSE_IMEX &&
1033 IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH) &&
1034 (!set_properties || focaltech_init(psmouse) == 0)) {
1035 return PSMOUSE_FOCALTECH;
Hans de Goede3ace3682014-09-12 17:24:47 -07001036 }
Dmitry Torokhov2b6f39e2015-11-27 20:52:36 -08001037 /*
1038 * Restrict psmouse_max_proto so that psmouse_initialize()
1039 * does not try to reset rate and resolution, because even
1040 * that upsets the device.
1041 * This also causes us to basically fall through to basic
1042 * protocol detection, where we fully reset the mouse,
1043 * and set it up as bare PS/2 protocol device.
1044 */
1045 psmouse_max_proto = max_proto = PSMOUSE_PS2;
Hans de Goede3ace3682014-09-12 17:24:47 -07001046 }
1047
Dmitry Torokhovad530772015-11-27 20:47:08 -08001048 /*
1049 * We always check for LifeBook because it does not disturb mouse
1050 * (it only checks DMI information).
1051 */
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001052 if (psmouse_try_protocol(psmouse, PSMOUSE_LIFEBOOK, &max_proto,
1053 set_properties, max_proto > PSMOUSE_IMEX))
1054 return PSMOUSE_LIFEBOOK;
Kenan Esau02d7f582005-05-29 02:30:22 -05001055
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001056 if (psmouse_try_protocol(psmouse, PSMOUSE_VMMOUSE, &max_proto,
1057 set_properties, max_proto > PSMOUSE_IMEX))
1058 return PSMOUSE_VMMOUSE;
Thomas Hellstrom8b8be512015-04-14 10:06:38 -07001059
Dmitry Torokhovad530772015-11-27 20:47:08 -08001060 /*
1061 * Try Kensington ThinkingMouse (we try first, because Synaptics
1062 * probe upsets the ThinkingMouse).
1063 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001064 if (max_proto > PSMOUSE_IMEX &&
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001065 psmouse_try_protocol(psmouse, PSMOUSE_THINKPS, &max_proto,
1066 set_properties, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return PSMOUSE_THINKPS;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Dmitry Torokhovad530772015-11-27 20:47:08 -08001070 /*
1071 * Try Synaptics TouchPad. Note that probing is done even if
1072 * Synaptics protocol support is disabled in config - we need to
1073 * know if it is Synaptics so we can reset it properly after
1074 * probing for IntelliMouse.
1075 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001076 if (max_proto > PSMOUSE_PS2 &&
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001077 psmouse_try_protocol(psmouse, PSMOUSE_SYNAPTICS, &max_proto,
1078 set_properties, false)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001079 synaptics_hardware = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (max_proto > PSMOUSE_IMEX) {
Dmitry Torokhovad530772015-11-27 20:47:08 -08001082 /*
1083 * Try activating protocol, but check if support is
1084 * enabled first, since we try detecting Synaptics
1085 * even when protocol is disabled.
1086 */
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001087 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) ||
1088 IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)) {
1089 if (!set_properties)
1090 return PSMOUSE_SYNAPTICS;
1091
1092 ret = synaptics_init(psmouse);
1093 if (ret >= 0)
1094 return ret;
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001095 }
1096
Dmitry Torokhovad530772015-11-27 20:47:08 -08001097 /*
1098 * Some Synaptics touchpads can emulate extended
1099 * protocols (like IMPS/2). Unfortunately
1100 * Logitech/Genius probes confuse some firmware
1101 * versions so we'll have to skip them.
1102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 max_proto = PSMOUSE_IMEX;
1104 }
Dmitry Torokhovad530772015-11-27 20:47:08 -08001105
1106 /*
1107 * Make sure that touchpad is in relative mode, gestures
1108 * (taps) are enabled.
1109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 synaptics_reset(psmouse);
1111 }
1112
Dmitry Torokhovad530772015-11-27 20:47:08 -08001113 /*
1114 * Try Cypress Trackpad. We must try it before Finger Sensing Pad
1115 * because Finger Sensing Pad probe upsets some modules of Cypress
1116 * Trackpads.
1117 */
Dudley Du0799a922013-01-05 00:14:22 -08001118 if (max_proto > PSMOUSE_IMEX &&
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001119 psmouse_try_protocol(psmouse, PSMOUSE_CYPRESS, &max_proto,
1120 set_properties, true)) {
1121 return PSMOUSE_CYPRESS;
Dudley Du0799a922013-01-05 00:14:22 -08001122 }
1123
Dmitry Torokhovad530772015-11-27 20:47:08 -08001124 /* Try ALPS TouchPad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (max_proto > PSMOUSE_IMEX) {
1126 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001127 if (psmouse_try_protocol(psmouse, PSMOUSE_ALPS,
1128 &max_proto, set_properties, true))
1129 return PSMOUSE_ALPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
Dmitry Torokhovad530772015-11-27 20:47:08 -08001132 /* Try OLPC HGPK touchpad */
Andres Salomondf08ef22008-09-16 12:30:34 -04001133 if (max_proto > PSMOUSE_IMEX &&
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001134 psmouse_try_protocol(psmouse, PSMOUSE_HGPK, &max_proto,
1135 set_properties, true)) {
1136 return PSMOUSE_HGPK;
Andres Salomondf08ef22008-09-16 12:30:34 -04001137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Dmitry Torokhovad530772015-11-27 20:47:08 -08001139 /* Try Elantech touchpad */
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04001140 if (max_proto > PSMOUSE_IMEX &&
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001141 psmouse_try_protocol(psmouse, PSMOUSE_ELANTECH,
1142 &max_proto, set_properties, true)) {
1143 return PSMOUSE_ELANTECH;
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04001144 }
1145
Andres Salomondf08ef22008-09-16 12:30:34 -04001146 if (max_proto > PSMOUSE_IMEX) {
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001147 if (psmouse_try_protocol(psmouse, PSMOUSE_GENPS,
1148 &max_proto, set_properties, true))
Stefan Lucke24bf10a2007-02-18 01:49:10 -05001149 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001151 if (psmouse_try_protocol(psmouse, PSMOUSE_PS2PP,
1152 &max_proto, set_properties, true))
Stefan Lucke24bf10a2007-02-18 01:49:10 -05001153 return PSMOUSE_PS2PP;
1154
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001155 if (psmouse_try_protocol(psmouse, PSMOUSE_TRACKPOINT,
1156 &max_proto, set_properties, true))
Stefan Lucke24bf10a2007-02-18 01:49:10 -05001157 return PSMOUSE_TRACKPOINT;
1158
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001159 if (psmouse_try_protocol(psmouse, PSMOUSE_TOUCHKIT_PS2,
1160 &max_proto, set_properties, true))
Stefan Lucke24bf10a2007-02-18 01:49:10 -05001161 return PSMOUSE_TOUCHKIT_PS2;
1162 }
Dmitry Torokhovba449952005-12-21 00:51:31 -05001163
Dmitry Torokhovad530772015-11-27 20:47:08 -08001164 /*
1165 * Try Finger Sensing Pad. We do it here because its probe upsets
1166 * Trackpoint devices (causing TP_READ_ID command to time out).
1167 */
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001168 if (max_proto > PSMOUSE_IMEX &&
1169 psmouse_try_protocol(psmouse, PSMOUSE_FSP,
1170 &max_proto, set_properties, true)) {
1171 return PSMOUSE_FSP;
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -08001172 }
1173
Dmitry Torokhovad530772015-11-27 20:47:08 -08001174 /*
1175 * Reset to defaults in case the device got confused by extended
1176 * protocol probes. Note that we follow up with full reset because
1177 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
1178 */
Alon Ziv554fc192007-08-30 00:22:48 -04001179 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -05001180 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001182 if (max_proto >= PSMOUSE_IMEX &&
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001183 psmouse_try_protocol(psmouse, PSMOUSE_IMEX,
1184 &max_proto, set_properties, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return PSMOUSE_IMEX;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001188 if (max_proto >= PSMOUSE_IMPS &&
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001189 psmouse_try_protocol(psmouse, PSMOUSE_IMPS,
1190 &max_proto, set_properties, true)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return PSMOUSE_IMPS;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Dmitry Torokhovad530772015-11-27 20:47:08 -08001194 /*
1195 * Okay, all failed, we have a standard mouse here. The number of
1196 * the buttons is still a question, though. We assume 3.
1197 */
Dmitry Torokhovc378b512015-11-27 21:17:40 -08001198 psmouse_try_protocol(psmouse, PSMOUSE_PS2,
1199 &max_proto, set_properties, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 if (synaptics_hardware) {
Dmitry Torokhovad530772015-11-27 20:47:08 -08001202 /*
1203 * We detected Synaptics hardware but it did not respond to
1204 * IMPS/2 probes. We need to reset the touchpad because if
1205 * there is a track point on the pass through port it could
1206 * get disabled while probing for protocol extensions.
1207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210
1211 return PSMOUSE_PS2;
1212}
1213
1214/*
1215 * psmouse_probe() probes for a PS/2 mouse.
1216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217static int psmouse_probe(struct psmouse *psmouse)
1218{
1219 struct ps2dev *ps2dev = &psmouse->ps2dev;
1220 unsigned char param[2];
1221
Dmitry Torokhovad530772015-11-27 20:47:08 -08001222 /*
1223 * First, we check if it's a mouse. It should send 0x00 or 0x03 in
1224 * case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
1225 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and
1226 * subsequent ID queries, probably due to a firmware bug.
1227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 param[0] = 0xa5;
1229 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1230 return -1;
1231
Vojtech Pavlik7741e932005-05-28 02:11:42 -05001232 if (param[0] != 0x00 && param[0] != 0x03 &&
1233 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return -1;
1235
Dmitry Torokhovad530772015-11-27 20:47:08 -08001236 /*
1237 * Then we reset and disable the mouse so that it doesn't generate
1238 * events.
1239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001241 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
1242 ps2dev->serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 return 0;
1245}
1246
1247/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 * psmouse_initialize() initializes the mouse to a sane state.
1249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250static void psmouse_initialize(struct psmouse *psmouse)
1251{
Dmitry Torokhovad530772015-11-27 20:47:08 -08001252 /*
1253 * We set the mouse report rate, resolution and scaling.
1254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (psmouse_max_proto != PSMOUSE_PS2) {
1256 psmouse->set_rate(psmouse, psmouse->rate);
1257 psmouse->set_resolution(psmouse, psmouse->resolution);
Mathias Gottschlag4ec212f2015-03-07 13:32:10 -08001258 psmouse->set_scale(psmouse, PSMOUSE_SCALE11);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260}
1261
1262/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 * psmouse_activate() enables the mouse so that we get motion reports from it.
1264 */
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001265int psmouse_activate(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001267 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001268 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1269 psmouse->ps2dev.serio->phys);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001270 return -1;
1271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277/*
Dmitry Torokhovad530772015-11-27 20:47:08 -08001278 * psmouse_deactivate() puts the mouse into poll mode so that we don't get
1279 * motion reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 */
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001281int psmouse_deactivate(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001283 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001284 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1285 psmouse->ps2dev.serio->phys);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001286 return -1;
1287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001293/*
1294 * psmouse_resync() attempts to re-validate current protocol.
1295 */
David Howellsc4028952006-11-22 14:57:56 +00001296static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001297{
David Howellsc4028952006-11-22 14:57:56 +00001298 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -04001299 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001300 struct serio *serio = psmouse->ps2dev.serio;
1301 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001302 bool failed = false, enabled = false;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001303 int i;
1304
Ingo Molnarc14471d2006-02-19 00:22:11 -05001305 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001306
1307 if (psmouse->state != PSMOUSE_RESYNCING)
1308 goto out;
1309
1310 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1311 parent = serio_get_drvdata(serio->parent);
1312 psmouse_deactivate(parent);
1313 }
1314
Dmitry Torokhovad530772015-11-27 20:47:08 -08001315 /*
1316 * Some mice don't ACK commands sent while they are in the middle of
1317 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1318 * instead of ps2_command() which would wait for 200ms for an ACK
1319 * that may never come.
1320 * As an additional quirk ALPS touchpads may not only forget to ACK
1321 * disable command but will stop reporting taps, so if we see that
1322 * mouse at least once ACKs disable we will do full reconnect if ACK
1323 * is missing.
1324 */
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001325 psmouse->num_resyncs++;
1326
1327 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1328 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001329 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001330 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001331 psmouse->acks_disable_command = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001332
Dmitry Torokhovad530772015-11-27 20:47:08 -08001333 /*
1334 * Poll the mouse. If it was reset the packet will be shorter than
1335 * psmouse->pktsize and ps2_command will fail. We do not expect and
1336 * do not handle scenario when mouse "upgrades" its protocol while
1337 * disconnected since it would require additional delay. If we ever
1338 * see a mouse that does it we'll adjust the code.
1339 */
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001340 if (!failed) {
1341 if (psmouse->poll(psmouse))
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001342 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001343 else {
1344 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1345 for (i = 0; i < psmouse->pktsize; i++) {
1346 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001347 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001348 if (rc != PSMOUSE_GOOD_DATA)
1349 break;
1350 }
1351 if (rc != PSMOUSE_FULL_PACKET)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001352 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001353 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1354 }
1355 }
Dmitry Torokhovad530772015-11-27 20:47:08 -08001356
1357 /*
1358 * Now try to enable mouse. We try to do that even if poll failed
1359 * and also repeat our attempts 5 times, otherwise we may be left
1360 * out with disabled mouse.
1361 */
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001362 for (i = 0; i < 5; i++) {
1363 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001364 enabled = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001365 break;
1366 }
1367 msleep(200);
1368 }
1369
1370 if (!enabled) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001371 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1372 psmouse->ps2dev.serio->phys);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001373 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001374 }
1375
1376 if (failed) {
1377 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001378 psmouse_info(psmouse,
1379 "resync failed, issuing reconnect request\n");
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001380 serio_reconnect(serio);
1381 } else
1382 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1383
1384 if (parent)
1385 psmouse_activate(parent);
1386 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001387 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001388}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390/*
1391 * psmouse_cleanup() resets the mouse into power-on state.
1392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393static void psmouse_cleanup(struct serio *serio)
1394{
1395 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001396 struct psmouse *parent = NULL;
1397
1398 mutex_lock(&psmouse_mutex);
1399
1400 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1401 parent = serio_get_drvdata(serio->parent);
1402 psmouse_deactivate(parent);
1403 }
1404
Dmitry Torokhova9f0c382010-02-07 23:10:04 -08001405 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1406
1407 /*
1408 * Disable stream mode so cleanup routine can proceed undisturbed.
1409 */
1410 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001411 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1412 psmouse->ps2dev.serio->phys);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001413
1414 if (psmouse->cleanup)
1415 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Dmitry Torokhovad530772015-11-27 20:47:08 -08001417 /*
1418 * Reset the mouse to defaults (bare PS/2 protocol).
1419 */
Dmitry Torokhov4a299bf2009-12-24 21:40:24 -08001420 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001421
Dmitry Torokhovad530772015-11-27 20:47:08 -08001422 /*
1423 * Some boxes, such as HP nx7400, get terribly confused if mouse
1424 * is not fully enabled before suspending/shutting down.
1425 */
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001426 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1427
1428 if (parent) {
1429 if (parent->pt_deactivate)
1430 parent->pt_deactivate(parent);
1431
1432 psmouse_activate(parent);
1433 }
1434
1435 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
1438/*
1439 * psmouse_disconnect() closes and frees.
1440 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441static void psmouse_disconnect(struct serio *serio)
1442{
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001443 struct psmouse *psmouse = serio_get_drvdata(serio);
1444 struct psmouse *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001446 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Ingo Molnarc14471d2006-02-19 00:22:11 -05001448 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1451
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001452 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001453 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001454 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001455 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1458 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001459 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
1462 if (psmouse->disconnect)
1463 psmouse->disconnect(psmouse);
1464
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001465 if (parent && parent->pt_deactivate)
1466 parent->pt_deactivate(parent);
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 serio_close(serio);
1471 serio_set_drvdata(serio, NULL);
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001472
1473 if (psmouse->dev)
1474 input_unregister_device(psmouse->dev);
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001477
1478 if (parent)
1479 psmouse_activate(parent);
1480
Ingo Molnarc14471d2006-02-19 00:22:11 -05001481 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001484static int psmouse_switch_protocol(struct psmouse *psmouse,
1485 const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001486{
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001487 const struct psmouse_protocol *selected_proto;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001488 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhov085fa802017-03-03 15:29:00 -08001489 enum psmouse_type type;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001490
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001491 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001492
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001493 if (proto && (proto->detect || proto->init)) {
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001494 psmouse_apply_defaults(psmouse);
1495
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001496 if (proto->detect && proto->detect(psmouse, true) < 0)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001497 return -1;
1498
1499 if (proto->init && proto->init(psmouse) < 0)
1500 return -1;
1501
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001502 selected_proto = proto;
1503 } else {
Dmitry Torokhov085fa802017-03-03 15:29:00 -08001504 type = psmouse_extensions(psmouse, psmouse_max_proto, true);
1505 selected_proto = psmouse_protocol_by_type(type);
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001506 }
1507
Dmitry Torokhov085fa802017-03-03 15:29:00 -08001508 psmouse->protocol = selected_proto;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001509
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001510 /*
1511 * If mouse's packet size is 3 there is no point in polling the
1512 * device in hopes to detect protocol reset - we won't get less
1513 * than 3 bytes response anyhow.
1514 */
1515 if (psmouse->pktsize == 3)
1516 psmouse->resync_time = 0;
1517
1518 /*
1519 * Some smart KVMs fake response to POLL command returning just
1520 * 3 bytes and messing up our resync logic, so if initial poll
1521 * fails we won't try polling the device anymore. Hopefully
1522 * such KVM will maintain initially selected protocol.
1523 */
1524 if (psmouse->resync_time && psmouse->poll(psmouse))
1525 psmouse->resync_time = 0;
1526
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001527 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001528 selected_proto->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001529
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001530 input_dev->name = psmouse->devname;
1531 input_dev->phys = psmouse->phys;
1532 input_dev->id.bustype = BUS_I8042;
1533 input_dev->id.vendor = 0x0002;
Dmitry Torokhov085fa802017-03-03 15:29:00 -08001534 input_dev->id.product = psmouse->protocol->type;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001535 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001536
1537 return 0;
1538}
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540/*
1541 * psmouse_connect() is a callback from the serio module when
1542 * an unhandled serio port is found.
1543 */
1544static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1545{
1546 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001547 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001548 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Ingo Molnarc14471d2006-02-19 00:22:11 -05001550 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /*
1553 * If this is a pass-through port deactivate parent so the device
1554 * connected to this port can be successfully identified
1555 */
1556 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1557 parent = serio_get_drvdata(serio->parent);
1558 psmouse_deactivate(parent);
1559 }
1560
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001561 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1562 input_dev = input_allocate_device();
1563 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001564 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001567 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001568 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001569 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1572
1573 serio_set_drvdata(serio, psmouse);
1574
Dmitry Torokhov72155612006-11-05 22:40:19 -05001575 error = serio_open(serio, drv);
1576 if (error)
1577 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Stefan Assmann66bc2f52015-08-26 13:11:49 -07001579 /* give PT device some time to settle down before probing */
1580 if (serio->id.type == SERIO_PS_PSTHRU)
1581 usleep_range(10000, 15000);
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001584 error = -ENODEV;
1585 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
1588 psmouse->rate = psmouse_rate;
1589 psmouse->resolution = psmouse_resolution;
1590 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001591 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001594 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001596 if (!psmouse->protocol->smbus_companion) {
1597 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1598 psmouse_initialize(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001600 error = input_register_device(input_dev);
1601 if (error)
1602 goto err_protocol_disconnect;
1603 } else {
1604 /* Smbus companion will be reporting events, not us. */
1605 input_free_device(input_dev);
1606 psmouse->dev = input_dev = NULL;
1607 }
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (parent && parent->pt_activate)
1610 parent->pt_activate(parent);
1611
Dmitry Torokhov72155612006-11-05 22:40:19 -05001612 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1613 if (error)
1614 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001616 /*
1617 * PS/2 devices having SMBus companions should stay disabled
1618 * on PS/2 side, in order to have SMBus part operable.
1619 */
1620 if (!psmouse->protocol->smbus_companion)
1621 psmouse_activate(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Dmitry Torokhov72155612006-11-05 22:40:19 -05001623 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001624 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (parent)
1626 psmouse_activate(parent);
1627
Ingo Molnarc14471d2006-02-19 00:22:11 -05001628 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001630
1631 err_pt_deactivate:
1632 if (parent && parent->pt_deactivate)
1633 parent->pt_deactivate(parent);
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001634 if (input_dev) {
1635 input_unregister_device(input_dev);
1636 input_dev = NULL; /* so we don't try to free it below */
1637 }
Dmitry Torokhov72155612006-11-05 22:40:19 -05001638 err_protocol_disconnect:
1639 if (psmouse->disconnect)
1640 psmouse->disconnect(psmouse);
1641 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1642 err_close_serio:
1643 serio_close(serio);
1644 err_clear_drvdata:
1645 serio_set_drvdata(serio, NULL);
1646 err_free:
1647 input_free_device(input_dev);
1648 kfree(psmouse);
1649
1650 retval = error;
1651 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Dmitry Torokhov0ab3fa52017-03-05 23:19:22 -08001654static int __psmouse_reconnect(struct serio *serio, bool fast_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 struct psmouse *psmouse = serio_get_drvdata(serio);
1657 struct psmouse *parent = NULL;
Dmitry Torokhov0ab3fa52017-03-05 23:19:22 -08001658 int (*reconnect_handler)(struct psmouse *);
Dmitry Torokhov085fa802017-03-03 15:29:00 -08001659 enum psmouse_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 int rc = -1;
1661
Ingo Molnarc14471d2006-02-19 00:22:11 -05001662 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001663
Dmitry Torokhov0ab3fa52017-03-05 23:19:22 -08001664 if (fast_reconnect) {
1665 reconnect_handler = psmouse->fast_reconnect;
1666 if (!reconnect_handler) {
1667 rc = -ENOENT;
1668 goto out_unlock;
1669 }
1670 } else {
1671 reconnect_handler = psmouse->reconnect;
1672 }
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1675 parent = serio_get_drvdata(serio->parent);
1676 psmouse_deactivate(parent);
1677 }
1678
1679 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1680
Dmitry Torokhov0ab3fa52017-03-05 23:19:22 -08001681 if (reconnect_handler) {
1682 if (reconnect_handler(psmouse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 goto out;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001684 } else {
1685 psmouse_reset(psmouse);
1686
1687 if (psmouse_probe(psmouse) < 0)
1688 goto out;
1689
1690 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
Dmitry Torokhov085fa802017-03-03 15:29:00 -08001691 if (psmouse->protocol->type != type)
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001692 goto out;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001695 /*
1696 * OK, the device type (and capabilities) match the old one,
1697 * we can continue using it, complete initialization
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 */
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001699 if (!psmouse->protocol->smbus_companion) {
1700 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1701 psmouse_initialize(psmouse);
1702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 if (parent && parent->pt_activate)
1705 parent->pt_activate(parent);
1706
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001707 /*
1708 * PS/2 devices having SMBus companions should stay disabled
1709 * on PS/2 side, in order to have SMBus part operable.
1710 */
1711 if (!psmouse->protocol->smbus_companion)
1712 psmouse_activate(psmouse);
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 rc = 0;
1715
1716out:
1717 /* If this is a pass-through port the parent waits to be activated */
1718 if (parent)
1719 psmouse_activate(parent);
1720
Dmitry Torokhov0ab3fa52017-03-05 23:19:22 -08001721out_unlock:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001722 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return rc;
1724}
1725
Dmitry Torokhov0ab3fa52017-03-05 23:19:22 -08001726static int psmouse_reconnect(struct serio *serio)
1727{
1728 return __psmouse_reconnect(serio, false);
1729}
1730
1731static int psmouse_fast_reconnect(struct serio *serio)
1732{
1733 return __psmouse_reconnect(serio, true);
1734}
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736static struct serio_device_id psmouse_serio_ids[] = {
1737 {
1738 .type = SERIO_8042,
1739 .proto = SERIO_ANY,
1740 .id = SERIO_ANY,
1741 .extra = SERIO_ANY,
1742 },
1743 {
1744 .type = SERIO_PS_PSTHRU,
1745 .proto = SERIO_ANY,
1746 .id = SERIO_ANY,
1747 .extra = SERIO_ANY,
1748 },
1749 { 0 }
1750};
1751
1752MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1753
1754static struct serio_driver psmouse_drv = {
1755 .driver = {
1756 .name = "psmouse",
1757 },
1758 .description = DRIVER_DESC,
1759 .id_table = psmouse_serio_ids,
1760 .interrupt = psmouse_interrupt,
1761 .connect = psmouse_connect,
1762 .reconnect = psmouse_reconnect,
Dmitry Torokhov0ab3fa52017-03-05 23:19:22 -08001763 .fast_reconnect = psmouse_fast_reconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 .disconnect = psmouse_disconnect,
1765 .cleanup = psmouse_cleanup,
1766};
1767
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001768ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1769 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001772 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001773 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001775 if (psmouse->protocol->smbus_companion &&
1776 devattr != &psmouse_attr_protocol.dattr)
1777 return -ENOENT;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001778
Eric W. Biederman59b01512010-01-05 17:56:02 -08001779 return attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001782ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1783 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001786 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1787 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 int retval;
1789
Ingo Molnarc14471d2006-02-19 00:22:11 -05001790 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001791 if (retval)
Eric W. Biederman59b01512010-01-05 17:56:02 -08001792 goto out;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001793
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001794 psmouse = serio_get_drvdata(serio);
1795
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001796 if (psmouse->protocol->smbus_companion &&
1797 devattr != &psmouse_attr_protocol.dattr) {
1798 retval = -ENOENT;
1799 goto out_unlock;
1800 }
1801
Andres Salomon68d48222008-09-16 12:30:34 -04001802 if (attr->protect) {
1803 if (psmouse->state == PSMOUSE_IGNORE) {
1804 retval = -ENODEV;
1805 goto out_unlock;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Andres Salomon68d48222008-09-16 12:30:34 -04001808 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1809 parent = serio_get_drvdata(serio->parent);
1810 psmouse_deactivate(parent);
1811 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001812
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001813 if (!psmouse->protocol->smbus_companion)
1814 psmouse_deactivate(psmouse);
Andres Salomon68d48222008-09-16 12:30:34 -04001815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001817 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Andres Salomon68d48222008-09-16 12:30:34 -04001819 if (attr->protect) {
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001820 if (retval != -ENODEV && !psmouse->protocol->smbus_companion)
Andres Salomon68d48222008-09-16 12:30:34 -04001821 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001822
Andres Salomon68d48222008-09-16 12:30:34 -04001823 if (parent)
1824 psmouse_activate(parent);
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Ingo Molnarc14471d2006-02-19 00:22:11 -05001827 out_unlock:
1828 mutex_unlock(&psmouse_mutex);
Eric W. Biederman59b01512010-01-05 17:56:02 -08001829 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return retval;
1831}
1832
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001833static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1834{
Sergey Vlasoveb5d58292006-11-09 00:34:27 -05001835 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001836
Sergey Vlasoveb5d58292006-11-09 00:34:27 -05001837 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001838}
1839
1840static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1841{
Sergey Vlasoveb5d58292006-11-09 00:34:27 -05001842 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
JJ Ding76496e72011-11-09 10:20:14 -08001843 unsigned int value;
1844 int err;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001845
JJ Ding76496e72011-11-09 10:20:14 -08001846 err = kstrtouint(buf, 10, &value);
1847 if (err)
1848 return err;
Sergey Vlasoveb5d58292006-11-09 00:34:27 -05001849
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001850 *field = value;
1851
1852 return count;
1853}
1854
1855static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001856{
Dmitry Torokhov085fa802017-03-03 15:29:00 -08001857 return sprintf(buf, "%s\n", psmouse->protocol->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001858}
1859
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001860static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001861{
1862 struct serio *serio = psmouse->ps2dev.serio;
1863 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001864 struct input_dev *old_dev, *new_dev;
1865 const struct psmouse_protocol *proto, *old_proto;
1866 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001867 int retry = 0;
1868
Dmitry Torokhov72155612006-11-05 22:40:19 -05001869 proto = psmouse_protocol_by_name(buf, count);
1870 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001871 return -EINVAL;
1872
Dmitry Torokhov085fa802017-03-03 15:29:00 -08001873 if (psmouse->protocol == proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001874 return count;
1875
Dmitry Torokhov72155612006-11-05 22:40:19 -05001876 new_dev = input_allocate_device();
1877 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001878 return -ENOMEM;
1879
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -07001880 while (!list_empty(&serio->children)) {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001881 if (++retry > 3) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001882 psmouse_warn(psmouse,
1883 "failed to destroy children ports, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001884 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001885 return -EIO;
1886 }
1887
Ingo Molnarc14471d2006-02-19 00:22:11 -05001888 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001889 serio_unregister_child_port(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001890 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001891
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001892 if (serio->drv != &psmouse_drv) {
1893 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001894 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001895 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001896
Dmitry Torokhov085fa802017-03-03 15:29:00 -08001897 if (psmouse->protocol == proto) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001898 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001899 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001900 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001901 }
1902
1903 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1904 parent = serio_get_drvdata(serio->parent);
1905 if (parent->pt_deactivate)
1906 parent->pt_deactivate(parent);
1907 }
1908
Dmitry Torokhov72155612006-11-05 22:40:19 -05001909 old_dev = psmouse->dev;
Dmitry Torokhov085fa802017-03-03 15:29:00 -08001910 old_proto = psmouse->protocol;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001911
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001912 if (psmouse->disconnect)
1913 psmouse->disconnect(psmouse);
1914
1915 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001916
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001917 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001918 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1919
1920 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1921 psmouse_reset(psmouse);
1922 /* default to PSMOUSE_PS2 */
1923 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1924 }
1925
1926 psmouse_initialize(psmouse);
1927 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1928
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001929 if (psmouse->protocol->smbus_companion) {
1930 input_free_device(psmouse->dev);
1931 psmouse->dev = NULL;
1932 } else {
1933 error = input_register_device(psmouse->dev);
1934 if (error) {
1935 if (psmouse->disconnect)
1936 psmouse->disconnect(psmouse);
Dmitry Torokhov72155612006-11-05 22:40:19 -05001937
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001938 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1939 input_free_device(new_dev);
1940 psmouse->dev = old_dev;
1941 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1942 psmouse_switch_protocol(psmouse, old_proto);
1943 psmouse_initialize(psmouse);
1944 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Dmitry Torokhov72155612006-11-05 22:40:19 -05001945
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001946 return error;
1947 }
Dmitry Torokhov72155612006-11-05 22:40:19 -05001948 }
1949
Dmitry Torokhovc7743262017-03-02 15:50:56 -08001950 if (old_dev)
1951 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001952
1953 if (parent && parent->pt_activate)
1954 parent->pt_activate(parent);
1955
1956 return count;
1957}
1958
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001959static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
JJ Ding76496e72011-11-09 10:20:14 -08001961 unsigned int value;
1962 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
JJ Ding76496e72011-11-09 10:20:14 -08001964 err = kstrtouint(buf, 10, &value);
1965 if (err)
1966 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 psmouse->set_rate(psmouse, value);
1969 return count;
1970}
1971
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001972static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
JJ Ding76496e72011-11-09 10:20:14 -08001974 unsigned int value;
1975 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
JJ Ding76496e72011-11-09 10:20:14 -08001977 err = kstrtouint(buf, 10, &value);
1978 if (err)
1979 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 psmouse->set_resolution(psmouse, value);
1982 return count;
1983}
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001986static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Helge Dellere38de672006-09-10 21:54:39 -04001988 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 if (!val)
1991 return -EINVAL;
1992
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001993 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001995 if (!proto || !proto->maxproto)
1996 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001998 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Stephen Evanchik541e3162005-08-08 01:26:18 -05002000 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
2002
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002003static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05002005 int type = *((unsigned int *)kp->arg);
2006
Takashi Iwai3d4c3aa2009-11-12 23:30:52 -08002007 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
2010static int __init psmouse_init(void)
2011{
Akinobu Mita153a9df02006-11-23 23:35:10 -05002012 int err;
2013
Dmitry Torokhov7705d542009-12-03 23:21:14 -08002014 lifebook_module_init();
2015 synaptics_module_init();
Daniel Drakeca94ec42010-11-11 22:19:57 -08002016 hgpk_module_init();
Dmitry Torokhov7705d542009-12-03 23:21:14 -08002017
Benjamin Tissoires8eb92e52017-03-02 10:48:23 -08002018 err = psmouse_smbus_module_init();
2019 if (err)
2020 return err;
2021
Bhaktipriya Shridhar24dde602016-08-23 13:46:22 -07002022 kpsmoused_wq = alloc_ordered_workqueue("kpsmoused", 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002023 if (!kpsmoused_wq) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07002024 pr_err("failed to create kpsmoused workqueue\n");
Benjamin Tissoires8eb92e52017-03-02 10:48:23 -08002025 err = -ENOMEM;
2026 goto err_smbus_exit;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002027 }
2028
Akinobu Mita153a9df02006-11-23 23:35:10 -05002029 err = serio_register_driver(&psmouse_drv);
2030 if (err)
Benjamin Tissoires8eb92e52017-03-02 10:48:23 -08002031 goto err_destroy_wq;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002032
Benjamin Tissoires8eb92e52017-03-02 10:48:23 -08002033 return 0;
2034
2035err_destroy_wq:
2036 destroy_workqueue(kpsmoused_wq);
2037err_smbus_exit:
2038 psmouse_smbus_module_exit();
Akinobu Mita153a9df02006-11-23 23:35:10 -05002039 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
2042static void __exit psmouse_exit(void)
2043{
2044 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05002045 destroy_workqueue(kpsmoused_wq);
Benjamin Tissoires8eb92e52017-03-02 10:48:23 -08002046 psmouse_smbus_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049module_init(psmouse_init);
2050module_exit(psmouse_exit);