blob: ad18dab0ac476323f1e7d7b73276c41f8524d337 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define DRIVER_DESC "PS/2 mouse driver"
42
43MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
44MODULE_DESCRIPTION(DRIVER_DESC);
45MODULE_LICENSE("GPL");
46
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050047static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Rusty Russell9bbb9e52010-08-11 23:04:12 -060048static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
49static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
Luis R. Rodriguez9c278472015-05-27 11:09:38 +093050static const struct kernel_param_ops param_ops_proto_abbrev = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -060051 .set = psmouse_set_maxproto,
52 .get = psmouse_get_maxproto,
53};
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050056MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58static unsigned int psmouse_resolution = 200;
59module_param_named(resolution, psmouse_resolution, uint, 0644);
60MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
61
62static unsigned int psmouse_rate = 100;
63module_param_named(rate, psmouse_rate, uint, 0644);
64MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
65
Shailendra Vermafeb9eba2015-05-26 14:07:12 -070066static bool psmouse_smartscroll = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
68MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
69
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050070static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071module_param_named(resetafter, psmouse_resetafter, uint, 0644);
72MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
73
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050074static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050075module_param_named(resync_time, psmouse_resync_time, uint, 0644);
76MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
77
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050078PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
79 NULL,
80 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
81PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
82 (void *) offsetof(struct psmouse, rate),
83 psmouse_show_int_attr, psmouse_attr_set_rate);
84PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
85 (void *) offsetof(struct psmouse, resolution),
86 psmouse_show_int_attr, psmouse_attr_set_resolution);
87PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
88 (void *) offsetof(struct psmouse, resetafter),
89 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050090PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
91 (void *) offsetof(struct psmouse, resync_time),
92 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050093
94static struct attribute *psmouse_attributes[] = {
95 &psmouse_attr_protocol.dattr.attr,
96 &psmouse_attr_rate.dattr.attr,
97 &psmouse_attr_resolution.dattr.attr,
98 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050099 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500100 NULL
101};
102
103static struct attribute_group psmouse_attribute_group = {
104 .attrs = psmouse_attributes,
105};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500107/*
Ingo Molnarc14471d2006-02-19 00:22:11 -0500108 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500109 * (connecting, disconnecting, changing rate or resolution via
110 * sysfs). We could use a per-device semaphore but since there
111 * rarely more than one PS/2 mouse connected and since semaphore
112 * is taken in "slow" paths it is not worth it.
113 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500114static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500115
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500116static struct workqueue_struct *kpsmoused_wq;
117
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500118struct psmouse_protocol {
119 enum psmouse_type type;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700120 bool maxproto;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700121 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
Helge Dellere38de672006-09-10 21:54:39 -0400122 const char *name;
123 const char *alias;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700124 int (*detect)(struct psmouse *, bool);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500125 int (*init)(struct psmouse *);
126};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
129 * psmouse_process_byte() analyzes the PS/2 data stream and reports
130 * relevant events to the input module once full packet has arrived.
131 */
132
Daniel Drake7968a5d2011-11-08 00:00:35 -0800133psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500135 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned char *packet = psmouse->packet;
137
138 if (psmouse->pktcnt < psmouse->pktsize)
139 return PSMOUSE_GOOD_DATA;
140
141/*
142 * Full packet accumulated, process it
143 */
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
146 * Scroll wheel on IntelliMice, scroll buttons on NetMice
147 */
148
149 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
150 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
151
152/*
153 * Scroll wheel and buttons on IntelliMouse Explorer
154 */
155
156 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400157 switch (packet[3] & 0xC0) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700158 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
159 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
160 break;
161 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
162 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
163 break;
164 case 0x00:
165 case 0xC0:
166 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
167 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
168 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
169 break;
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
173/*
174 * Extra buttons on Genius NewNet 3D
175 */
176
177 if (psmouse->type == PSMOUSE_GENPS) {
178 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
179 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
180 }
181
182/*
183 * Extra button on ThinkingMouse
184 */
185 if (psmouse->type == PSMOUSE_THINKPS) {
186 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
187 /* Without this bit of weirdness moving up gives wildly high Y changes. */
188 packet[1] |= (packet[0] & 0x40) << 1;
189 }
190
191/*
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400192 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
193 * byte.
194 */
195 if (psmouse->type == PSMOUSE_CORTRON) {
196 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
197 packet[0] |= 0x08;
198 }
199
200/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * Generic PS/2 Mouse
202 */
203
204 input_report_key(dev, BTN_LEFT, packet[0] & 1);
205 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
206 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
207
208 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
209 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
210
211 input_sync(dev);
212
213 return PSMOUSE_FULL_PACKET;
214}
215
Andres Salomon8bf020e2008-09-16 12:30:33 -0400216void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
217 unsigned long delay)
218{
219 queue_delayed_work(kpsmoused_wq, work, delay);
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500223 * __psmouse_set_state() sets new psmouse state and resets all flags.
224 */
225
226static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
227{
228 psmouse->state = new_state;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700229 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500230 psmouse->ps2dev.flags = 0;
231 psmouse->last = jiffies;
232}
233
234
235/*
236 * psmouse_set_state() sets new psmouse state and resets all flags and
237 * counters while holding serio lock so fighting with interrupt handler
238 * is not a concern.
239 */
240
Andres Salomona48cf5f2008-09-16 12:30:33 -0400241void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500242{
243 serio_pause_rx(psmouse->ps2dev.serio);
244 __psmouse_set_state(psmouse, new_state);
245 serio_continue_rx(psmouse->ps2dev.serio);
246}
247
248/*
249 * psmouse_handle_byte() processes one byte of the input data stream
250 * by calling corresponding protocol handler.
251 */
252
David Howells7d12e782006-10-05 14:55:46 +0100253static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500254{
David Howells7d12e782006-10-05 14:55:46 +0100255 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500256
257 switch (rc) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700258 case PSMOUSE_BAD_DATA:
259 if (psmouse->state == PSMOUSE_ACTIVATED) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700260 psmouse_warn(psmouse,
261 "%s at %s lost sync at byte %d\n",
262 psmouse->name, psmouse->phys,
263 psmouse->pktcnt);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700264 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
265 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700266 psmouse_notice(psmouse,
267 "issuing reconnect request\n");
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700268 serio_reconnect(psmouse->ps2dev.serio);
269 return -1;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500270 }
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700271 }
272 psmouse->pktcnt = 0;
273 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500274
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700275 case PSMOUSE_FULL_PACKET:
276 psmouse->pktcnt = 0;
277 if (psmouse->out_of_sync_cnt) {
278 psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700279 psmouse_notice(psmouse,
280 "%s at %s - driver resynced.\n",
281 psmouse->name, psmouse->phys);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700282 }
283 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500284
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700285 case PSMOUSE_GOOD_DATA:
286 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500287 }
288 return 0;
289}
290
291/*
292 * psmouse_interrupt() handles incoming characters, either passing them
293 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
295
296static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100297 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (psmouse->state == PSMOUSE_IGNORE)
302 goto out;
303
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700304 if (unlikely((flags & SERIO_TIMEOUT) ||
305 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (psmouse->state == PSMOUSE_ACTIVATED)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700308 psmouse_warn(psmouse,
309 "bad data from KBC -%s%s\n",
310 flags & SERIO_TIMEOUT ? " timeout" : "",
311 flags & SERIO_PARITY ? " bad parity" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ps2_cmd_aborted(&psmouse->ps2dev);
313 goto out;
314 }
315
316 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
317 if (ps2_handle_ack(&psmouse->ps2dev, data))
318 goto out;
319
320 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
321 if (ps2_handle_response(&psmouse->ps2dev, data))
322 goto out;
323
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500324 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto out;
326
327 if (psmouse->state == PSMOUSE_ACTIVATED &&
328 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700329 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
330 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500331 psmouse->badbyte = psmouse->packet[0];
332 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400333 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500338/*
339 * Check if this is a new device announcement (0xAA 0x00)
340 */
341 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400342 if (psmouse->pktcnt == 1) {
343 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Zephaniah E. Hull535650f2009-05-14 22:02:33 -0700347 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
348 (psmouse->type == PSMOUSE_HGPK &&
349 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500350 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
351 serio_reconnect(serio);
352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500354/*
355 * Not a new device, try processing first byte normally
356 */
357 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100358 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500359 goto out;
360
361 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500364/*
365 * See if we need to force resync because mouse was idle for too long
366 */
367 if (psmouse->state == PSMOUSE_ACTIVATED &&
368 psmouse->pktcnt == 1 && psmouse->resync_time &&
369 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
370 psmouse->badbyte = psmouse->packet[0];
371 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400372 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500375
376 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100377 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500378
379 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return IRQ_HANDLED;
381}
382
383
384/*
385 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
386 * using sliced syntax, understood by advanced devices, such as Logitech
387 * or Synaptics touchpads. The command is encoded as:
388 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
389 * is the command.
390 */
391int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
392{
393 int i;
394
395 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
396 return -1;
397
398 for (i = 6; i >= 0; i -= 2) {
399 unsigned char d = (command >> i) & 3;
400 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
401 return -1;
402 }
403
404 return 0;
405}
406
407
408/*
409 * psmouse_reset() resets the mouse into power-on state.
410 */
411int psmouse_reset(struct psmouse *psmouse)
412{
413 unsigned char param[2];
414
415 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
416 return -1;
417
418 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
419 return -1;
420
421 return 0;
422}
423
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800424/*
425 * Here we set the mouse resolution.
426 */
427
428void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
429{
430 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
431 unsigned char p;
432
433 if (resolution == 0 || resolution > 200)
434 resolution = 200;
435
436 p = params[resolution / 50];
437 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
438 psmouse->resolution = 25 << p;
439}
440
441/*
442 * Here we set the mouse report rate.
443 */
444
445static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
446{
447 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
448 unsigned char r;
449 int i = 0;
450
451 while (rates[i] > rate) i++;
452 r = rates[i];
453 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
454 psmouse->rate = r;
455}
456
457/*
Mathias Gottschlag4ec212f2015-03-07 13:32:10 -0800458 * Here we set the mouse scaling.
459 */
460
461static void psmouse_set_scale(struct psmouse *psmouse, enum psmouse_scale scale)
462{
463 ps2_command(&psmouse->ps2dev, NULL,
464 scale == PSMOUSE_SCALE21 ? PSMOUSE_CMD_SETSCALE21 :
465 PSMOUSE_CMD_SETSCALE11);
466}
467
468/*
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800469 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
470 */
471
472static int psmouse_poll(struct psmouse *psmouse)
473{
474 return ps2_command(&psmouse->ps2dev, psmouse->packet,
475 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
476}
477
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800478static bool psmouse_check_pnp_id(const char *id, const char * const ids[])
479{
480 int i;
481
482 for (i = 0; ids[i]; i++)
483 if (!strcasecmp(id, ids[i]))
484 return true;
485
486 return false;
487}
488
Hans de Goede2c75ada2014-09-11 10:14:09 -0700489/*
490 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
491 */
492bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
493{
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800494 struct serio *serio = psmouse->ps2dev.serio;
495 char *p, *fw_id_copy, *save_ptr;
496 bool found = false;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700497
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800498 if (strncmp(serio->firmware_id, "PNP: ", 5))
499 return false;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700500
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800501 fw_id_copy = kstrndup(&serio->firmware_id[5],
502 sizeof(serio->firmware_id) - 5,
503 GFP_KERNEL);
504 if (!fw_id_copy)
505 return false;
506
507 save_ptr = fw_id_copy;
508 while ((p = strsep(&fw_id_copy, " ")) != NULL) {
509 if (psmouse_check_pnp_id(p, ids)) {
510 found = true;
511 break;
512 }
513 }
514
515 kfree(save_ptr);
516 return found;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700517}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519/*
520 * Genius NetMouse magic init.
521 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700522static int genius_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 struct ps2dev *ps2dev = &psmouse->ps2dev;
525 unsigned char param[4];
526
527 param[0] = 3;
528 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
529 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
530 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
531 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
532 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
533
534 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
535 return -1;
536
537 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800538 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700539 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
540 __set_bit(BTN_SIDE, psmouse->dev->keybit);
541 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500544 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 psmouse->pktsize = 4;
546 }
547
548 return 0;
549}
550
551/*
552 * IntelliMouse magic init.
553 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700554static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct ps2dev *ps2dev = &psmouse->ps2dev;
557 unsigned char param[2];
558
559 param[0] = 200;
560 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
561 param[0] = 100;
562 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
563 param[0] = 80;
564 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
565 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
566
567 if (param[0] != 3)
568 return -1;
569
570 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700571 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
572 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800574 if (!psmouse->vendor)
575 psmouse->vendor = "Generic";
576 if (!psmouse->name)
577 psmouse->name = "Wheel Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 psmouse->pktsize = 4;
579 }
580
581 return 0;
582}
583
584/*
585 * Try IntelliMouse/Explorer magic init.
586 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700587static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct ps2dev *ps2dev = &psmouse->ps2dev;
590 unsigned char param[2];
591
592 intellimouse_detect(psmouse, 0);
593
594 param[0] = 200;
595 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
596 param[0] = 200;
597 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
598 param[0] = 80;
599 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
600 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
601
602 if (param[0] != 4)
603 return -1;
604
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400605/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
606 param[0] = 200;
607 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
608 param[0] = 80;
609 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
610 param[0] = 40;
611 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700614 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
615 __set_bit(REL_WHEEL, psmouse->dev->relbit);
616 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
617 __set_bit(BTN_SIDE, psmouse->dev->keybit);
618 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800620 if (!psmouse->vendor)
621 psmouse->vendor = "Generic";
622 if (!psmouse->name)
623 psmouse->name = "Explorer Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 psmouse->pktsize = 4;
625 }
626
627 return 0;
628}
629
630/*
631 * Kensington ThinkingMouse / ExpertMouse magic init.
632 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700633static int thinking_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 struct ps2dev *ps2dev = &psmouse->ps2dev;
636 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400637 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int i;
639
640 param[0] = 10;
641 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
642 param[0] = 0;
643 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400644 for (i = 0; i < ARRAY_SIZE(seq); i++) {
645 param[0] = seq[i];
646 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
649
650 if (param[0] != 2)
651 return -1;
652
653 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800654 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700655 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 psmouse->vendor = "Kensington";
658 psmouse->name = "ThinkingMouse";
659 }
660
661 return 0;
662}
663
664/*
665 * Bare PS/2 protocol "detection". Always succeeds.
666 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700667static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500669 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800670 if (!psmouse->vendor)
671 psmouse->vendor = "Generic";
672 if (!psmouse->name)
673 psmouse->name = "Mouse";
674
675/*
676 * We have no way of figuring true number of buttons so let's
677 * assume that the device has 3.
678 */
679 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 return 0;
683}
684
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400685/*
686 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
687 * must be forced by sysfs protocol writing.
688 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700689static int cortron_detect(struct psmouse *psmouse, bool set_properties)
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400690{
691 if (set_properties) {
692 psmouse->vendor = "Cortron";
693 psmouse->name = "PS/2 Trackball";
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800694
695 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700696 __set_bit(BTN_SIDE, psmouse->dev->keybit);
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400697 }
698
699 return 0;
700}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/*
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800703 * Apply default settings to the psmouse structure. Most of them will
704 * be overridden by individual protocol initialization routines.
705 */
706
707static void psmouse_apply_defaults(struct psmouse *psmouse)
708{
709 struct input_dev *input_dev = psmouse->dev;
710
711 memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
712 memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
713 memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
714 memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
715 memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));
716
717 __set_bit(EV_KEY, input_dev->evbit);
718 __set_bit(EV_REL, input_dev->evbit);
719
720 __set_bit(BTN_LEFT, input_dev->keybit);
721 __set_bit(BTN_RIGHT, input_dev->keybit);
722
723 __set_bit(REL_X, input_dev->relbit);
724 __set_bit(REL_Y, input_dev->relbit);
725
Hans de Goede01d4cd52014-09-08 14:44:05 -0700726 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
727
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800728 psmouse->set_rate = psmouse_set_rate;
729 psmouse->set_resolution = psmouse_set_resolution;
Mathias Gottschlag4ec212f2015-03-07 13:32:10 -0800730 psmouse->set_scale = psmouse_set_scale;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800731 psmouse->poll = psmouse_poll;
732 psmouse->protocol_handler = psmouse_process_byte;
733 psmouse->pktsize = 3;
734 psmouse->reconnect = NULL;
735 psmouse->disconnect = NULL;
736 psmouse->cleanup = NULL;
737 psmouse->pt_activate = NULL;
738 psmouse->pt_deactivate = NULL;
739}
740
741/*
742 * Apply default settings to the psmouse structure and call specified
743 * protocol detection or initialization routine.
744 */
745static int psmouse_do_detect(int (*detect)(struct psmouse *psmouse,
746 bool set_properties),
747 struct psmouse *psmouse, bool set_properties)
748{
749 if (set_properties)
750 psmouse_apply_defaults(psmouse);
751
752 return detect(psmouse, set_properties);
753}
754
755/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
757 * the mouse may have.
758 */
759
760static int psmouse_extensions(struct psmouse *psmouse,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700761 unsigned int max_proto, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Jiri Kosina06989892009-11-16 22:12:13 -0800763 bool synaptics_hardware = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Hans de Goede3ace3682014-09-12 17:24:47 -0700765/* Always check for focaltech, this is safe as it uses pnp-id matching */
766 if (psmouse_do_detect(focaltech_detect, psmouse, set_properties) == 0) {
Mathias Gottschlag05be1d02014-12-29 09:26:35 -0800767 if (max_proto > PSMOUSE_IMEX) {
768 if (!set_properties || focaltech_init(psmouse) == 0) {
Dmitry Torokhov290b7992014-12-29 12:06:38 -0800769 if (IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH))
Mathias Gottschlag05be1d02014-12-29 09:26:35 -0800770 return PSMOUSE_FOCALTECH;
771 /*
772 * Note that we need to also restrict
773 * psmouse_max_proto so that psmouse_initialize()
774 * does not try to reset rate and resolution,
775 * because even that upsets the device.
776 */
777 psmouse_max_proto = PSMOUSE_PS2;
778 return PSMOUSE_PS2;
779 }
Hans de Goede3ace3682014-09-12 17:24:47 -0700780 }
781 }
782
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500783/*
784 * We always check for lifebook because it does not disturb mouse
785 * (it only checks DMI information).
786 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800787 if (psmouse_do_detect(lifebook_detect, psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500788 if (max_proto > PSMOUSE_IMEX) {
789 if (!set_properties || lifebook_init(psmouse) == 0)
790 return PSMOUSE_LIFEBOOK;
791 }
792 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500793
Thomas Hellstrom8b8be512015-04-14 10:06:38 -0700794 if (psmouse_do_detect(vmmouse_detect, psmouse, set_properties) == 0) {
795 if (max_proto > PSMOUSE_IMEX) {
796 if (!set_properties || vmmouse_init(psmouse) == 0)
797 return PSMOUSE_VMMOUSE;
798 }
799 }
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/*
802 * Try Kensington ThinkingMouse (we try first, because synaptics probe
803 * upsets the thinkingmouse).
804 */
805
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800806 if (max_proto > PSMOUSE_IMEX &&
807 psmouse_do_detect(thinking_detect, psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return PSMOUSE_THINKPS;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500812 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
813 * support is disabled in config - we need to know if it is synaptics so we
814 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800816 if (max_proto > PSMOUSE_PS2 &&
817 psmouse_do_detect(synaptics_detect, psmouse, set_properties) == 0) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700818 synaptics_hardware = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (max_proto > PSMOUSE_IMEX) {
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800821/*
822 * Try activating protocol, but check if support is enabled first, since
823 * we try detecting Synaptics even when protocol is disabled.
824 */
Dmitry Torokhov290b7992014-12-29 12:06:38 -0800825 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) &&
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800826 (!set_properties || synaptics_init(psmouse) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return PSMOUSE_SYNAPTICS;
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830/*
831 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
832 * Unfortunately Logitech/Genius probes confuse some firmware versions so
833 * we'll have to skip them.
834 */
835 max_proto = PSMOUSE_IMEX;
836 }
837/*
838 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
839 */
840 synaptics_reset(psmouse);
841 }
842
843/*
Dudley Du0799a922013-01-05 00:14:22 -0800844 * Try Cypress Trackpad.
845 * Must try it before Finger Sensing Pad because Finger Sensing Pad probe
846 * upsets some modules of Cypress Trackpads.
847 */
848 if (max_proto > PSMOUSE_IMEX &&
849 cypress_detect(psmouse, set_properties) == 0) {
Dmitry Torokhov290b7992014-12-29 12:06:38 -0800850 if (IS_ENABLED(CONFIG_MOUSE_PS2_CYPRESS)) {
Dudley Du0799a922013-01-05 00:14:22 -0800851 if (cypress_init(psmouse) == 0)
852 return PSMOUSE_CYPRESS;
853
854 /*
855 * Finger Sensing Pad probe upsets some modules of
856 * Cypress Trackpad, must avoid Finger Sensing Pad
857 * probe if Cypress Trackpad device detected.
858 */
859 return PSMOUSE_PS2;
860 }
861
862 max_proto = PSMOUSE_IMEX;
863 }
864
865/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * Try ALPS TouchPad
867 */
868 if (max_proto > PSMOUSE_IMEX) {
869 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800870 if (psmouse_do_detect(alps_detect,
871 psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (!set_properties || alps_init(psmouse) == 0)
873 return PSMOUSE_ALPS;
874/*
875 * Init failed, try basic relative protocols
876 */
877 max_proto = PSMOUSE_IMEX;
878 }
879 }
880
Andres Salomondf08ef22008-09-16 12:30:34 -0400881/*
882 * Try OLPC HGPK touchpad.
883 */
884 if (max_proto > PSMOUSE_IMEX &&
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800885 psmouse_do_detect(hgpk_detect, psmouse, set_properties) == 0) {
Andres Salomondf08ef22008-09-16 12:30:34 -0400886 if (!set_properties || hgpk_init(psmouse) == 0)
887 return PSMOUSE_HGPK;
888/*
889 * Init failed, try basic relative protocols
890 */
891 max_proto = PSMOUSE_IMEX;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400894/*
895 * Try Elantech touchpad.
896 */
897 if (max_proto > PSMOUSE_IMEX &&
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800898 psmouse_do_detect(elantech_detect, psmouse, set_properties) == 0) {
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400899 if (!set_properties || elantech_init(psmouse) == 0)
900 return PSMOUSE_ELANTECH;
901/*
902 * Init failed, try basic relative protocols
903 */
904 max_proto = PSMOUSE_IMEX;
905 }
906
Andres Salomondf08ef22008-09-16 12:30:34 -0400907 if (max_proto > PSMOUSE_IMEX) {
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800908 if (psmouse_do_detect(genius_detect,
909 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500910 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800912 if (psmouse_do_detect(ps2pp_init,
913 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500914 return PSMOUSE_PS2PP;
915
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800916 if (psmouse_do_detect(trackpoint_detect,
917 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500918 return PSMOUSE_TRACKPOINT;
919
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800920 if (psmouse_do_detect(touchkit_ps2_detect,
921 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500922 return PSMOUSE_TOUCHKIT_PS2;
923 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -0800926 * Try Finger Sensing Pad. We do it here because its probe upsets
927 * Trackpoint devices (causing TP_READ_ID command to time out).
928 */
929 if (max_proto > PSMOUSE_IMEX) {
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800930 if (psmouse_do_detect(fsp_detect,
931 psmouse, set_properties) == 0) {
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -0800932 if (!set_properties || fsp_init(psmouse) == 0)
933 return PSMOUSE_FSP;
934/*
935 * Init failed, try basic relative protocols
936 */
937 max_proto = PSMOUSE_IMEX;
938 }
939 }
940
941/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400943 * protocol probes. Note that we follow up with full reset because
944 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
Alon Ziv554fc192007-08-30 00:22:48 -0400946 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500947 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800949 if (max_proto >= PSMOUSE_IMEX &&
950 psmouse_do_detect(im_explorer_detect,
951 psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return PSMOUSE_IMEX;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800955 if (max_proto >= PSMOUSE_IMPS &&
956 psmouse_do_detect(intellimouse_detect,
957 psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return PSMOUSE_IMPS;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961/*
962 * Okay, all failed, we have a standard mouse here. The number of the buttons
963 * is still a question, though. We assume 3.
964 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800965 psmouse_do_detect(ps2bare_detect, psmouse, set_properties);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (synaptics_hardware) {
968/*
969 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
970 * We need to reset the touchpad because if there is a track point on the
971 * pass through port it could get disabled while probing for protocol
972 * extensions.
973 */
974 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
977 return PSMOUSE_PS2;
978}
979
Helge Dellere38de672006-09-10 21:54:39 -0400980static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500981 {
982 .type = PSMOUSE_PS2,
983 .name = "PS/2",
984 .alias = "bare",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700985 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700986 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500987 .detect = ps2bare_detect,
988 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500989#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500990 {
991 .type = PSMOUSE_PS2PP,
992 .name = "PS2++",
993 .alias = "logitech",
994 .detect = ps2pp_init,
995 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500996#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500997 {
998 .type = PSMOUSE_THINKPS,
999 .name = "ThinkPS/2",
1000 .alias = "thinkps",
1001 .detect = thinking_detect,
1002 },
Dudley Du0799a922013-01-05 00:14:22 -08001003#ifdef CONFIG_MOUSE_PS2_CYPRESS
1004 {
1005 .type = PSMOUSE_CYPRESS,
1006 .name = "CyPS/2",
1007 .alias = "cypress",
1008 .detect = cypress_detect,
1009 .init = cypress_init,
1010 },
1011#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001012 {
1013 .type = PSMOUSE_GENPS,
1014 .name = "GenPS/2",
1015 .alias = "genius",
1016 .detect = genius_detect,
1017 },
1018 {
1019 .type = PSMOUSE_IMPS,
1020 .name = "ImPS/2",
1021 .alias = "imps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001022 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001023 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001024 .detect = intellimouse_detect,
1025 },
1026 {
1027 .type = PSMOUSE_IMEX,
1028 .name = "ImExPS/2",
1029 .alias = "exps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001030 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001031 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001032 .detect = im_explorer_detect,
1033 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001034#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001035 {
1036 .type = PSMOUSE_SYNAPTICS,
1037 .name = "SynPS/2",
1038 .alias = "synaptics",
1039 .detect = synaptics_detect,
1040 .init = synaptics_init,
1041 },
Daniel Drake7968a5d2011-11-08 00:00:35 -08001042 {
1043 .type = PSMOUSE_SYNAPTICS_RELATIVE,
1044 .name = "SynRelPS/2",
1045 .alias = "synaptics-relative",
1046 .detect = synaptics_detect,
1047 .init = synaptics_init_relative,
1048 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001049#endif
1050#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001051 {
1052 .type = PSMOUSE_ALPS,
1053 .name = "AlpsPS/2",
1054 .alias = "alps",
1055 .detect = alps_detect,
1056 .init = alps_init,
1057 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001058#endif
1059#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001060 {
1061 .type = PSMOUSE_LIFEBOOK,
1062 .name = "LBPS/2",
1063 .alias = "lifebook",
1064 .init = lifebook_init,
1065 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001066#endif
1067#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001068 {
Stephen Evanchik541e3162005-08-08 01:26:18 -05001069 .type = PSMOUSE_TRACKPOINT,
1070 .name = "TPPS/2",
1071 .alias = "trackpoint",
1072 .detect = trackpoint_detect,
1073 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001074#endif
1075#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -05001076 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -05001077 .type = PSMOUSE_TOUCHKIT_PS2,
1078 .name = "touchkitPS/2",
1079 .alias = "touchkit",
1080 .detect = touchkit_ps2_detect,
1081 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001082#endif
Andres Salomondf08ef22008-09-16 12:30:34 -04001083#ifdef CONFIG_MOUSE_PS2_OLPC
1084 {
1085 .type = PSMOUSE_HGPK,
1086 .name = "OLPC HGPK",
1087 .alias = "hgpk",
1088 .detect = hgpk_detect,
1089 },
1090#endif
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04001091#ifdef CONFIG_MOUSE_PS2_ELANTECH
1092 {
1093 .type = PSMOUSE_ELANTECH,
1094 .name = "ETPS/2",
1095 .alias = "elantech",
1096 .detect = elantech_detect,
1097 .init = elantech_init,
1098 },
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -07001099#endif
1100#ifdef CONFIG_MOUSE_PS2_SENTELIC
1101 {
1102 .type = PSMOUSE_FSP,
1103 .name = "FSPPS/2",
1104 .alias = "fsp",
1105 .detect = fsp_detect,
1106 .init = fsp_init,
1107 },
1108#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -05001109 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -04001110 .type = PSMOUSE_CORTRON,
1111 .name = "CortronPS/2",
1112 .alias = "cortps",
1113 .detect = cortron_detect,
1114 },
Mathias Gottschlag05be1d02014-12-29 09:26:35 -08001115#ifdef CONFIG_MOUSE_PS2_FOCALTECH
1116 {
1117 .type = PSMOUSE_FOCALTECH,
1118 .name = "FocalTechPS/2",
1119 .alias = "focaltech",
1120 .detect = focaltech_detect,
1121 .init = focaltech_init,
1122 },
1123#endif
Thomas Hellstrom8b8be512015-04-14 10:06:38 -07001124#ifdef CONFIG_MOUSE_PS2_VMMOUSE
1125 {
1126 .type = PSMOUSE_VMMOUSE,
1127 .name = VMMOUSE_PSNAME,
1128 .alias = "vmmouse",
1129 .detect = vmmouse_detect,
1130 .init = vmmouse_init,
1131 },
1132#endif
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -04001133 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001134 .type = PSMOUSE_AUTO,
1135 .name = "auto",
1136 .alias = "any",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001137 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001138 },
1139};
1140
Helge Dellere38de672006-09-10 21:54:39 -04001141static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001142{
1143 int i;
1144
1145 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
1146 if (psmouse_protocols[i].type == type)
1147 return &psmouse_protocols[i];
1148
1149 WARN_ON(1);
1150 return &psmouse_protocols[0];
1151}
1152
Helge Dellere38de672006-09-10 21:54:39 -04001153static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001154{
Helge Dellere38de672006-09-10 21:54:39 -04001155 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001156 int i;
1157
1158 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
1159 p = &psmouse_protocols[i];
1160
1161 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
1162 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
1163 return &psmouse_protocols[i];
1164 }
1165
1166 return NULL;
1167}
1168
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/*
1171 * psmouse_probe() probes for a PS/2 mouse.
1172 */
1173
1174static int psmouse_probe(struct psmouse *psmouse)
1175{
1176 struct ps2dev *ps2dev = &psmouse->ps2dev;
1177 unsigned char param[2];
1178
1179/*
1180 * First, we check if it's a mouse. It should send 0x00 or 0x03
1181 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -05001182 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
1183 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 */
1185
1186 param[0] = 0xa5;
1187 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1188 return -1;
1189
Vojtech Pavlik7741e932005-05-28 02:11:42 -05001190 if (param[0] != 0x00 && param[0] != 0x03 &&
1191 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return -1;
1193
1194/*
1195 * Then we reset and disable the mouse so that it doesn't generate events.
1196 */
1197
1198 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001199 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
1200 ps2dev->serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 return 0;
1203}
1204
1205/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 * psmouse_initialize() initializes the mouse to a sane state.
1207 */
1208
1209static void psmouse_initialize(struct psmouse *psmouse)
1210{
1211/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 * We set the mouse report rate, resolution and scaling.
1213 */
1214
1215 if (psmouse_max_proto != PSMOUSE_PS2) {
1216 psmouse->set_rate(psmouse, psmouse->rate);
1217 psmouse->set_resolution(psmouse, psmouse->resolution);
Mathias Gottschlag4ec212f2015-03-07 13:32:10 -08001218 psmouse->set_scale(psmouse, PSMOUSE_SCALE11);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220}
1221
1222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 * psmouse_activate() enables the mouse so that we get motion reports from it.
1224 */
1225
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001226int psmouse_activate(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001228 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001229 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1230 psmouse->ps2dev.serio->phys);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001231 return -1;
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/*
1239 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +02001240 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 */
1242
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001243int psmouse_deactivate(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001245 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001246 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1247 psmouse->ps2dev.serio->phys);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001248 return -1;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001255
1256/*
1257 * psmouse_resync() attempts to re-validate current protocol.
1258 */
1259
David Howellsc4028952006-11-22 14:57:56 +00001260static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001261{
David Howellsc4028952006-11-22 14:57:56 +00001262 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -04001263 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001264 struct serio *serio = psmouse->ps2dev.serio;
1265 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001266 bool failed = false, enabled = false;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001267 int i;
1268
Ingo Molnarc14471d2006-02-19 00:22:11 -05001269 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001270
1271 if (psmouse->state != PSMOUSE_RESYNCING)
1272 goto out;
1273
1274 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1275 parent = serio_get_drvdata(serio->parent);
1276 psmouse_deactivate(parent);
1277 }
1278
1279/*
1280 * Some mice don't ACK commands sent while they are in the middle of
1281 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1282 * instead of ps2_command() which would wait for 200ms for an ACK
1283 * that may never come.
1284 * As an additional quirk ALPS touchpads may not only forget to ACK
1285 * disable command but will stop reporting taps, so if we see that
1286 * mouse at least once ACKs disable we will do full reconnect if ACK
1287 * is missing.
1288 */
1289 psmouse->num_resyncs++;
1290
1291 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1292 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001293 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001294 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001295 psmouse->acks_disable_command = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001296
1297/*
1298 * Poll the mouse. If it was reset the packet will be shorter than
1299 * psmouse->pktsize and ps2_command will fail. We do not expect and
1300 * do not handle scenario when mouse "upgrades" its protocol while
1301 * disconnected since it would require additional delay. If we ever
1302 * see a mouse that does it we'll adjust the code.
1303 */
1304 if (!failed) {
1305 if (psmouse->poll(psmouse))
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001306 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001307 else {
1308 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1309 for (i = 0; i < psmouse->pktsize; i++) {
1310 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001311 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001312 if (rc != PSMOUSE_GOOD_DATA)
1313 break;
1314 }
1315 if (rc != PSMOUSE_FULL_PACKET)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001316 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001317 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1318 }
1319 }
1320/*
1321 * Now try to enable mouse. We try to do that even if poll failed and also
1322 * repeat our attempts 5 times, otherwise we may be left out with disabled
1323 * mouse.
1324 */
1325 for (i = 0; i < 5; i++) {
1326 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001327 enabled = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001328 break;
1329 }
1330 msleep(200);
1331 }
1332
1333 if (!enabled) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001334 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1335 psmouse->ps2dev.serio->phys);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001336 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001337 }
1338
1339 if (failed) {
1340 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001341 psmouse_info(psmouse,
1342 "resync failed, issuing reconnect request\n");
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001343 serio_reconnect(serio);
1344 } else
1345 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1346
1347 if (parent)
1348 psmouse_activate(parent);
1349 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001350 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001351}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353/*
1354 * psmouse_cleanup() resets the mouse into power-on state.
1355 */
1356
1357static void psmouse_cleanup(struct serio *serio)
1358{
1359 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001360 struct psmouse *parent = NULL;
1361
1362 mutex_lock(&psmouse_mutex);
1363
1364 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1365 parent = serio_get_drvdata(serio->parent);
1366 psmouse_deactivate(parent);
1367 }
1368
Dmitry Torokhova9f0c382010-02-07 23:10:04 -08001369 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1370
1371 /*
1372 * Disable stream mode so cleanup routine can proceed undisturbed.
1373 */
1374 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001375 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1376 psmouse->ps2dev.serio->phys);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001377
1378 if (psmouse->cleanup)
1379 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Dmitry Torokhov4a299bf2009-12-24 21:40:24 -08001381/*
1382 * Reset the mouse to defaults (bare PS/2 protocol).
1383 */
1384 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001385
1386/*
1387 * Some boxes, such as HP nx7400, get terribly confused if mouse
1388 * is not fully enabled before suspending/shutting down.
1389 */
1390 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1391
1392 if (parent) {
1393 if (parent->pt_deactivate)
1394 parent->pt_deactivate(parent);
1395
1396 psmouse_activate(parent);
1397 }
1398
1399 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402/*
1403 * psmouse_disconnect() closes and frees.
1404 */
1405
1406static void psmouse_disconnect(struct serio *serio)
1407{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001408 struct psmouse *psmouse, *parent = NULL;
1409
1410 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001412 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Ingo Molnarc14471d2006-02-19 00:22:11 -05001414 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1417
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001418 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001419 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001420 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001421 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1424 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001425 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
1428 if (psmouse->disconnect)
1429 psmouse->disconnect(psmouse);
1430
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001431 if (parent && parent->pt_deactivate)
1432 parent->pt_deactivate(parent);
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 serio_close(serio);
1437 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001438 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001440
1441 if (parent)
1442 psmouse_activate(parent);
1443
Ingo Molnarc14471d2006-02-19 00:22:11 -05001444 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001447static int psmouse_switch_protocol(struct psmouse *psmouse,
1448 const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001449{
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001450 const struct psmouse_protocol *selected_proto;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001451 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001452
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001453 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001454
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001455 if (proto && (proto->detect || proto->init)) {
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001456 psmouse_apply_defaults(psmouse);
1457
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001458 if (proto->detect && proto->detect(psmouse, true) < 0)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001459 return -1;
1460
1461 if (proto->init && proto->init(psmouse) < 0)
1462 return -1;
1463
1464 psmouse->type = proto->type;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001465 selected_proto = proto;
1466 } else {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001467 psmouse->type = psmouse_extensions(psmouse,
1468 psmouse_max_proto, true);
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001469 selected_proto = psmouse_protocol_by_type(psmouse->type);
1470 }
1471
1472 psmouse->ignore_parity = selected_proto->ignore_parity;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001473
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001474 /*
1475 * If mouse's packet size is 3 there is no point in polling the
1476 * device in hopes to detect protocol reset - we won't get less
1477 * than 3 bytes response anyhow.
1478 */
1479 if (psmouse->pktsize == 3)
1480 psmouse->resync_time = 0;
1481
1482 /*
1483 * Some smart KVMs fake response to POLL command returning just
1484 * 3 bytes and messing up our resync logic, so if initial poll
1485 * fails we won't try polling the device anymore. Hopefully
1486 * such KVM will maintain initially selected protocol.
1487 */
1488 if (psmouse->resync_time && psmouse->poll(psmouse))
1489 psmouse->resync_time = 0;
1490
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001491 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001492 selected_proto->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001493
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001494 input_dev->name = psmouse->devname;
1495 input_dev->phys = psmouse->phys;
1496 input_dev->id.bustype = BUS_I8042;
1497 input_dev->id.vendor = 0x0002;
1498 input_dev->id.product = psmouse->type;
1499 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001500
1501 return 0;
1502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/*
1505 * psmouse_connect() is a callback from the serio module when
1506 * an unhandled serio port is found.
1507 */
1508static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1509{
1510 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001511 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001512 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Ingo Molnarc14471d2006-02-19 00:22:11 -05001514 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /*
1517 * If this is a pass-through port deactivate parent so the device
1518 * connected to this port can be successfully identified
1519 */
1520 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1521 parent = serio_get_drvdata(serio->parent);
1522 psmouse_deactivate(parent);
1523 }
1524
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001525 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1526 input_dev = input_allocate_device();
1527 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001528 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001531 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001532 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001533 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1536
1537 serio_set_drvdata(serio, psmouse);
1538
Dmitry Torokhov72155612006-11-05 22:40:19 -05001539 error = serio_open(serio, drv);
1540 if (error)
1541 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Stefan Assmann66bc2f52015-08-26 13:11:49 -07001543 /* give PT device some time to settle down before probing */
1544 if (serio->id.type == SERIO_PS_PSTHRU)
1545 usleep_range(10000, 15000);
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001548 error = -ENODEV;
1549 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551
1552 psmouse->rate = psmouse_rate;
1553 psmouse->resolution = psmouse_resolution;
1554 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001555 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001558 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 psmouse_initialize(psmouse);
1562
Dmitry Torokhov72155612006-11-05 22:40:19 -05001563 error = input_register_device(psmouse->dev);
1564 if (error)
1565 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (parent && parent->pt_activate)
1568 parent->pt_activate(parent);
1569
Dmitry Torokhov72155612006-11-05 22:40:19 -05001570 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1571 if (error)
1572 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 psmouse_activate(psmouse);
1575
Dmitry Torokhov72155612006-11-05 22:40:19 -05001576 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001577 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (parent)
1579 psmouse_activate(parent);
1580
Ingo Molnarc14471d2006-02-19 00:22:11 -05001581 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001583
1584 err_pt_deactivate:
1585 if (parent && parent->pt_deactivate)
1586 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001587 input_unregister_device(psmouse->dev);
1588 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001589 err_protocol_disconnect:
1590 if (psmouse->disconnect)
1591 psmouse->disconnect(psmouse);
1592 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1593 err_close_serio:
1594 serio_close(serio);
1595 err_clear_drvdata:
1596 serio_set_drvdata(serio, NULL);
1597 err_free:
1598 input_free_device(input_dev);
1599 kfree(psmouse);
1600
1601 retval = error;
1602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605
1606static int psmouse_reconnect(struct serio *serio)
1607{
1608 struct psmouse *psmouse = serio_get_drvdata(serio);
1609 struct psmouse *parent = NULL;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001610 unsigned char type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 int rc = -1;
1612
Ingo Molnarc14471d2006-02-19 00:22:11 -05001613 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1616 parent = serio_get_drvdata(serio->parent);
1617 psmouse_deactivate(parent);
1618 }
1619
1620 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1621
1622 if (psmouse->reconnect) {
1623 if (psmouse->reconnect(psmouse))
1624 goto out;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001625 } else {
1626 psmouse_reset(psmouse);
1627
1628 if (psmouse_probe(psmouse) < 0)
1629 goto out;
1630
1631 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1632 if (psmouse->type != type)
1633 goto out;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001636 /*
1637 * OK, the device type (and capabilities) match the old one,
1638 * we can continue using it, complete initialization
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 */
1640 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1641
1642 psmouse_initialize(psmouse);
1643
1644 if (parent && parent->pt_activate)
1645 parent->pt_activate(parent);
1646
1647 psmouse_activate(psmouse);
1648 rc = 0;
1649
1650out:
1651 /* If this is a pass-through port the parent waits to be activated */
1652 if (parent)
1653 psmouse_activate(parent);
1654
Ingo Molnarc14471d2006-02-19 00:22:11 -05001655 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return rc;
1657}
1658
1659static struct serio_device_id psmouse_serio_ids[] = {
1660 {
1661 .type = SERIO_8042,
1662 .proto = SERIO_ANY,
1663 .id = SERIO_ANY,
1664 .extra = SERIO_ANY,
1665 },
1666 {
1667 .type = SERIO_PS_PSTHRU,
1668 .proto = SERIO_ANY,
1669 .id = SERIO_ANY,
1670 .extra = SERIO_ANY,
1671 },
1672 { 0 }
1673};
1674
1675MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1676
1677static struct serio_driver psmouse_drv = {
1678 .driver = {
1679 .name = "psmouse",
1680 },
1681 .description = DRIVER_DESC,
1682 .id_table = psmouse_serio_ids,
1683 .interrupt = psmouse_interrupt,
1684 .connect = psmouse_connect,
1685 .reconnect = psmouse_reconnect,
1686 .disconnect = psmouse_disconnect,
1687 .cleanup = psmouse_cleanup,
1688};
1689
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001690ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1691 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001694 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1695 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001697 psmouse = serio_get_drvdata(serio);
1698
Eric W. Biederman59b01512010-01-05 17:56:02 -08001699 return attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001702ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1703 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001706 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1707 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int retval;
1709
Ingo Molnarc14471d2006-02-19 00:22:11 -05001710 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001711 if (retval)
Eric W. Biederman59b01512010-01-05 17:56:02 -08001712 goto out;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001713
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001714 psmouse = serio_get_drvdata(serio);
1715
Andres Salomon68d48222008-09-16 12:30:34 -04001716 if (attr->protect) {
1717 if (psmouse->state == PSMOUSE_IGNORE) {
1718 retval = -ENODEV;
1719 goto out_unlock;
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Andres Salomon68d48222008-09-16 12:30:34 -04001722 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1723 parent = serio_get_drvdata(serio->parent);
1724 psmouse_deactivate(parent);
1725 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001726
Andres Salomon68d48222008-09-16 12:30:34 -04001727 psmouse_deactivate(psmouse);
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001730 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Andres Salomon68d48222008-09-16 12:30:34 -04001732 if (attr->protect) {
1733 if (retval != -ENODEV)
1734 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001735
Andres Salomon68d48222008-09-16 12:30:34 -04001736 if (parent)
1737 psmouse_activate(parent);
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Ingo Molnarc14471d2006-02-19 00:22:11 -05001740 out_unlock:
1741 mutex_unlock(&psmouse_mutex);
Eric W. Biederman59b01512010-01-05 17:56:02 -08001742 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return retval;
1744}
1745
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001746static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1747{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001748 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001749
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001750 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001751}
1752
1753static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1754{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001755 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
JJ Ding76496e72011-11-09 10:20:14 -08001756 unsigned int value;
1757 int err;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001758
JJ Ding76496e72011-11-09 10:20:14 -08001759 err = kstrtouint(buf, 10, &value);
1760 if (err)
1761 return err;
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001762
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001763 *field = value;
1764
1765 return count;
1766}
1767
1768static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001769{
1770 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1771}
1772
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001773static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001774{
1775 struct serio *serio = psmouse->ps2dev.serio;
1776 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001777 struct input_dev *old_dev, *new_dev;
1778 const struct psmouse_protocol *proto, *old_proto;
1779 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001780 int retry = 0;
1781
Dmitry Torokhov72155612006-11-05 22:40:19 -05001782 proto = psmouse_protocol_by_name(buf, count);
1783 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001784 return -EINVAL;
1785
1786 if (psmouse->type == proto->type)
1787 return count;
1788
Dmitry Torokhov72155612006-11-05 22:40:19 -05001789 new_dev = input_allocate_device();
1790 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001791 return -ENOMEM;
1792
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -07001793 while (!list_empty(&serio->children)) {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001794 if (++retry > 3) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001795 psmouse_warn(psmouse,
1796 "failed to destroy children ports, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001797 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001798 return -EIO;
1799 }
1800
Ingo Molnarc14471d2006-02-19 00:22:11 -05001801 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001802 serio_unregister_child_port(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001803 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001804
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001805 if (serio->drv != &psmouse_drv) {
1806 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001807 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001808 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001809
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001810 if (psmouse->type == proto->type) {
1811 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001812 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001813 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001814 }
1815
1816 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1817 parent = serio_get_drvdata(serio->parent);
1818 if (parent->pt_deactivate)
1819 parent->pt_deactivate(parent);
1820 }
1821
Dmitry Torokhov72155612006-11-05 22:40:19 -05001822 old_dev = psmouse->dev;
1823 old_proto = psmouse_protocol_by_type(psmouse->type);
1824
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001825 if (psmouse->disconnect)
1826 psmouse->disconnect(psmouse);
1827
1828 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001829
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001830 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001831 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1832
1833 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1834 psmouse_reset(psmouse);
1835 /* default to PSMOUSE_PS2 */
1836 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1837 }
1838
1839 psmouse_initialize(psmouse);
1840 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1841
Dmitry Torokhov72155612006-11-05 22:40:19 -05001842 error = input_register_device(psmouse->dev);
1843 if (error) {
1844 if (psmouse->disconnect)
1845 psmouse->disconnect(psmouse);
1846
1847 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1848 input_free_device(new_dev);
1849 psmouse->dev = old_dev;
1850 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1851 psmouse_switch_protocol(psmouse, old_proto);
1852 psmouse_initialize(psmouse);
1853 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1854
1855 return error;
1856 }
1857
1858 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001859
1860 if (parent && parent->pt_activate)
1861 parent->pt_activate(parent);
1862
1863 return count;
1864}
1865
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001866static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
JJ Ding76496e72011-11-09 10:20:14 -08001868 unsigned int value;
1869 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
JJ Ding76496e72011-11-09 10:20:14 -08001871 err = kstrtouint(buf, 10, &value);
1872 if (err)
1873 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 psmouse->set_rate(psmouse, value);
1876 return count;
1877}
1878
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001879static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
JJ Ding76496e72011-11-09 10:20:14 -08001881 unsigned int value;
1882 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
JJ Ding76496e72011-11-09 10:20:14 -08001884 err = kstrtouint(buf, 10, &value);
1885 if (err)
1886 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 psmouse->set_resolution(psmouse, value);
1889 return count;
1890}
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001893static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Helge Dellere38de672006-09-10 21:54:39 -04001895 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 if (!val)
1898 return -EINVAL;
1899
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001900 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001902 if (!proto || !proto->maxproto)
1903 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001905 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Stephen Evanchik541e3162005-08-08 01:26:18 -05001907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001910static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001912 int type = *((unsigned int *)kp->arg);
1913
Takashi Iwai3d4c3aa2009-11-12 23:30:52 -08001914 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
1917static int __init psmouse_init(void)
1918{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001919 int err;
1920
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001921 lifebook_module_init();
1922 synaptics_module_init();
Daniel Drakeca94ec42010-11-11 22:19:57 -08001923 hgpk_module_init();
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001924
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001925 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1926 if (!kpsmoused_wq) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001927 pr_err("failed to create kpsmoused workqueue\n");
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001928 return -ENOMEM;
1929 }
1930
Akinobu Mita153a9df02006-11-23 23:35:10 -05001931 err = serio_register_driver(&psmouse_drv);
1932 if (err)
1933 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001934
Akinobu Mita153a9df02006-11-23 23:35:10 -05001935 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
1938static void __exit psmouse_exit(void)
1939{
1940 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001941 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
1944module_init(psmouse_init);
1945module_exit(psmouse_exit);