blob: bd91a8efa0f177a9a0216b8278e0bc089479b34e [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#define DRIVER_DESC "PS/2 mouse driver"
41
42MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
43MODULE_DESCRIPTION(DRIVER_DESC);
44MODULE_LICENSE("GPL");
45
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050046static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Rusty Russell9bbb9e52010-08-11 23:04:12 -060047static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
48static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
49static struct kernel_param_ops param_ops_proto_abbrev = {
50 .set = psmouse_set_maxproto,
51 .get = psmouse_get_maxproto,
52};
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050055MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static unsigned int psmouse_resolution = 200;
58module_param_named(resolution, psmouse_resolution, uint, 0644);
59MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
60
61static unsigned int psmouse_rate = 100;
62module_param_named(rate, psmouse_rate, uint, 0644);
63MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
64
Rusty Russell90ab5ee2012-01-13 09:32:20 +103065static bool psmouse_smartscroll = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
67MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
68
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050069static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070module_param_named(resetafter, psmouse_resetafter, uint, 0644);
71MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
72
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050073static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050074module_param_named(resync_time, psmouse_resync_time, uint, 0644);
75MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
76
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050077PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
78 NULL,
79 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
80PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
81 (void *) offsetof(struct psmouse, rate),
82 psmouse_show_int_attr, psmouse_attr_set_rate);
83PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
84 (void *) offsetof(struct psmouse, resolution),
85 psmouse_show_int_attr, psmouse_attr_set_resolution);
86PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
87 (void *) offsetof(struct psmouse, resetafter),
88 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050089PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
90 (void *) offsetof(struct psmouse, resync_time),
91 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050092
93static struct attribute *psmouse_attributes[] = {
94 &psmouse_attr_protocol.dattr.attr,
95 &psmouse_attr_rate.dattr.attr,
96 &psmouse_attr_resolution.dattr.attr,
97 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050098 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050099 NULL
100};
101
102static struct attribute_group psmouse_attribute_group = {
103 .attrs = psmouse_attributes,
104};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500106/*
Ingo Molnarc14471d2006-02-19 00:22:11 -0500107 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500108 * (connecting, disconnecting, changing rate or resolution via
109 * sysfs). We could use a per-device semaphore but since there
110 * rarely more than one PS/2 mouse connected and since semaphore
111 * is taken in "slow" paths it is not worth it.
112 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500113static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500114
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500115static struct workqueue_struct *kpsmoused_wq;
116
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500117struct psmouse_protocol {
118 enum psmouse_type type;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700119 bool maxproto;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700120 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
Helge Dellere38de672006-09-10 21:54:39 -0400121 const char *name;
122 const char *alias;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700123 int (*detect)(struct psmouse *, bool);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500124 int (*init)(struct psmouse *);
125};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/*
128 * psmouse_process_byte() analyzes the PS/2 data stream and reports
129 * relevant events to the input module once full packet has arrived.
130 */
131
Daniel Drake7968a5d2011-11-08 00:00:35 -0800132psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500134 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned char *packet = psmouse->packet;
136
137 if (psmouse->pktcnt < psmouse->pktsize)
138 return PSMOUSE_GOOD_DATA;
139
140/*
141 * Full packet accumulated, process it
142 */
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
145 * Scroll wheel on IntelliMice, scroll buttons on NetMice
146 */
147
148 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
149 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
150
151/*
152 * Scroll wheel and buttons on IntelliMouse Explorer
153 */
154
155 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400156 switch (packet[3] & 0xC0) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700157 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
158 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
159 break;
160 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
161 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
162 break;
163 case 0x00:
164 case 0xC0:
165 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
166 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
167 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
168 break;
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
172/*
173 * Extra buttons on Genius NewNet 3D
174 */
175
176 if (psmouse->type == PSMOUSE_GENPS) {
177 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
178 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
179 }
180
181/*
182 * Extra button on ThinkingMouse
183 */
184 if (psmouse->type == PSMOUSE_THINKPS) {
185 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
186 /* Without this bit of weirdness moving up gives wildly high Y changes. */
187 packet[1] |= (packet[0] & 0x40) << 1;
188 }
189
190/*
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400191 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
192 * byte.
193 */
194 if (psmouse->type == PSMOUSE_CORTRON) {
195 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
196 packet[0] |= 0x08;
197 }
198
199/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * Generic PS/2 Mouse
201 */
202
203 input_report_key(dev, BTN_LEFT, packet[0] & 1);
204 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
205 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
206
207 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
208 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
209
210 input_sync(dev);
211
212 return PSMOUSE_FULL_PACKET;
213}
214
Andres Salomon8bf020e2008-09-16 12:30:33 -0400215void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
216 unsigned long delay)
217{
218 queue_delayed_work(kpsmoused_wq, work, delay);
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500222 * __psmouse_set_state() sets new psmouse state and resets all flags.
223 */
224
225static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
226{
227 psmouse->state = new_state;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700228 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500229 psmouse->ps2dev.flags = 0;
230 psmouse->last = jiffies;
231}
232
233
234/*
235 * psmouse_set_state() sets new psmouse state and resets all flags and
236 * counters while holding serio lock so fighting with interrupt handler
237 * is not a concern.
238 */
239
Andres Salomona48cf5f2008-09-16 12:30:33 -0400240void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500241{
242 serio_pause_rx(psmouse->ps2dev.serio);
243 __psmouse_set_state(psmouse, new_state);
244 serio_continue_rx(psmouse->ps2dev.serio);
245}
246
247/*
248 * psmouse_handle_byte() processes one byte of the input data stream
249 * by calling corresponding protocol handler.
250 */
251
David Howells7d12e782006-10-05 14:55:46 +0100252static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500253{
David Howells7d12e782006-10-05 14:55:46 +0100254 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500255
256 switch (rc) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700257 case PSMOUSE_BAD_DATA:
258 if (psmouse->state == PSMOUSE_ACTIVATED) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700259 psmouse_warn(psmouse,
260 "%s at %s lost sync at byte %d\n",
261 psmouse->name, psmouse->phys,
262 psmouse->pktcnt);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700263 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
264 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700265 psmouse_notice(psmouse,
266 "issuing reconnect request\n");
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700267 serio_reconnect(psmouse->ps2dev.serio);
268 return -1;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500269 }
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700270 }
271 psmouse->pktcnt = 0;
272 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500273
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700274 case PSMOUSE_FULL_PACKET:
275 psmouse->pktcnt = 0;
276 if (psmouse->out_of_sync_cnt) {
277 psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700278 psmouse_notice(psmouse,
279 "%s at %s - driver resynced.\n",
280 psmouse->name, psmouse->phys);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700281 }
282 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500283
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700284 case PSMOUSE_GOOD_DATA:
285 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500286 }
287 return 0;
288}
289
290/*
291 * psmouse_interrupt() handles incoming characters, either passing them
292 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
294
295static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100296 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (psmouse->state == PSMOUSE_IGNORE)
301 goto out;
302
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700303 if (unlikely((flags & SERIO_TIMEOUT) ||
304 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (psmouse->state == PSMOUSE_ACTIVATED)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700307 psmouse_warn(psmouse,
308 "bad data from KBC -%s%s\n",
309 flags & SERIO_TIMEOUT ? " timeout" : "",
310 flags & SERIO_PARITY ? " bad parity" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 ps2_cmd_aborted(&psmouse->ps2dev);
312 goto out;
313 }
314
315 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
316 if (ps2_handle_ack(&psmouse->ps2dev, data))
317 goto out;
318
319 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
320 if (ps2_handle_response(&psmouse->ps2dev, data))
321 goto out;
322
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500323 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 goto out;
325
326 if (psmouse->state == PSMOUSE_ACTIVATED &&
327 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700328 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
329 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500330 psmouse->badbyte = psmouse->packet[0];
331 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400332 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500333 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500337/*
338 * Check if this is a new device announcement (0xAA 0x00)
339 */
340 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400341 if (psmouse->pktcnt == 1) {
342 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Zephaniah E. Hull535650f2009-05-14 22:02:33 -0700346 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
347 (psmouse->type == PSMOUSE_HGPK &&
348 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500349 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
350 serio_reconnect(serio);
351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500353/*
354 * Not a new device, try processing first byte normally
355 */
356 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100357 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500358 goto out;
359
360 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500363/*
364 * See if we need to force resync because mouse was idle for too long
365 */
366 if (psmouse->state == PSMOUSE_ACTIVATED &&
367 psmouse->pktcnt == 1 && psmouse->resync_time &&
368 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
369 psmouse->badbyte = psmouse->packet[0];
370 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400371 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500372 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500374
375 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100376 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500377
378 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return IRQ_HANDLED;
380}
381
382
383/*
384 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
385 * using sliced syntax, understood by advanced devices, such as Logitech
386 * or Synaptics touchpads. The command is encoded as:
387 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
388 * is the command.
389 */
390int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
391{
392 int i;
393
394 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
395 return -1;
396
397 for (i = 6; i >= 0; i -= 2) {
398 unsigned char d = (command >> i) & 3;
399 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
400 return -1;
401 }
402
403 return 0;
404}
405
406
407/*
408 * psmouse_reset() resets the mouse into power-on state.
409 */
410int psmouse_reset(struct psmouse *psmouse)
411{
412 unsigned char param[2];
413
414 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
415 return -1;
416
417 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
418 return -1;
419
420 return 0;
421}
422
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800423/*
424 * Here we set the mouse resolution.
425 */
426
427void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
428{
429 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
430 unsigned char p;
431
432 if (resolution == 0 || resolution > 200)
433 resolution = 200;
434
435 p = params[resolution / 50];
436 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
437 psmouse->resolution = 25 << p;
438}
439
440/*
441 * Here we set the mouse report rate.
442 */
443
444static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
445{
446 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
447 unsigned char r;
448 int i = 0;
449
450 while (rates[i] > rate) i++;
451 r = rates[i];
452 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
453 psmouse->rate = r;
454}
455
456/*
457 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
458 */
459
460static int psmouse_poll(struct psmouse *psmouse)
461{
462 return ps2_command(&psmouse->ps2dev, psmouse->packet,
463 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
464}
465
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800466static bool psmouse_check_pnp_id(const char *id, const char * const ids[])
467{
468 int i;
469
470 for (i = 0; ids[i]; i++)
471 if (!strcasecmp(id, ids[i]))
472 return true;
473
474 return false;
475}
476
Hans de Goede2c75ada2014-09-11 10:14:09 -0700477/*
478 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
479 */
480bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
481{
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800482 struct serio *serio = psmouse->ps2dev.serio;
483 char *p, *fw_id_copy, *save_ptr;
484 bool found = false;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700485
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800486 if (strncmp(serio->firmware_id, "PNP: ", 5))
487 return false;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700488
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800489 fw_id_copy = kstrndup(&serio->firmware_id[5],
490 sizeof(serio->firmware_id) - 5,
491 GFP_KERNEL);
492 if (!fw_id_copy)
493 return false;
494
495 save_ptr = fw_id_copy;
496 while ((p = strsep(&fw_id_copy, " ")) != NULL) {
497 if (psmouse_check_pnp_id(p, ids)) {
498 found = true;
499 break;
500 }
501 }
502
503 kfree(save_ptr);
504 return found;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700505}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507/*
508 * Genius NetMouse magic init.
509 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700510static int genius_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct ps2dev *ps2dev = &psmouse->ps2dev;
513 unsigned char param[4];
514
515 param[0] = 3;
516 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
517 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
518 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
519 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
520 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
521
522 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
523 return -1;
524
525 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800526 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700527 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
528 __set_bit(BTN_SIDE, psmouse->dev->keybit);
529 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500532 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 psmouse->pktsize = 4;
534 }
535
536 return 0;
537}
538
539/*
540 * IntelliMouse magic init.
541 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700542static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct ps2dev *ps2dev = &psmouse->ps2dev;
545 unsigned char param[2];
546
547 param[0] = 200;
548 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
549 param[0] = 100;
550 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
551 param[0] = 80;
552 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
553 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
554
555 if (param[0] != 3)
556 return -1;
557
558 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700559 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
560 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800562 if (!psmouse->vendor)
563 psmouse->vendor = "Generic";
564 if (!psmouse->name)
565 psmouse->name = "Wheel Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 psmouse->pktsize = 4;
567 }
568
569 return 0;
570}
571
572/*
573 * Try IntelliMouse/Explorer magic init.
574 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700575static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct ps2dev *ps2dev = &psmouse->ps2dev;
578 unsigned char param[2];
579
580 intellimouse_detect(psmouse, 0);
581
582 param[0] = 200;
583 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
584 param[0] = 200;
585 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
586 param[0] = 80;
587 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
588 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
589
590 if (param[0] != 4)
591 return -1;
592
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400593/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
594 param[0] = 200;
595 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
596 param[0] = 80;
597 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
598 param[0] = 40;
599 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700602 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
603 __set_bit(REL_WHEEL, psmouse->dev->relbit);
604 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
605 __set_bit(BTN_SIDE, psmouse->dev->keybit);
606 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800608 if (!psmouse->vendor)
609 psmouse->vendor = "Generic";
610 if (!psmouse->name)
611 psmouse->name = "Explorer Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 psmouse->pktsize = 4;
613 }
614
615 return 0;
616}
617
618/*
619 * Kensington ThinkingMouse / ExpertMouse magic init.
620 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700621static int thinking_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct ps2dev *ps2dev = &psmouse->ps2dev;
624 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400625 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int i;
627
628 param[0] = 10;
629 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
630 param[0] = 0;
631 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400632 for (i = 0; i < ARRAY_SIZE(seq); i++) {
633 param[0] = seq[i];
634 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
637
638 if (param[0] != 2)
639 return -1;
640
641 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800642 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700643 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 psmouse->vendor = "Kensington";
646 psmouse->name = "ThinkingMouse";
647 }
648
649 return 0;
650}
651
652/*
653 * Bare PS/2 protocol "detection". Always succeeds.
654 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700655static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500657 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800658 if (!psmouse->vendor)
659 psmouse->vendor = "Generic";
660 if (!psmouse->name)
661 psmouse->name = "Mouse";
662
663/*
664 * We have no way of figuring true number of buttons so let's
665 * assume that the device has 3.
666 */
667 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 return 0;
671}
672
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400673/*
674 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
675 * must be forced by sysfs protocol writing.
676 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700677static int cortron_detect(struct psmouse *psmouse, bool set_properties)
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400678{
679 if (set_properties) {
680 psmouse->vendor = "Cortron";
681 psmouse->name = "PS/2 Trackball";
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800682
683 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700684 __set_bit(BTN_SIDE, psmouse->dev->keybit);
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400685 }
686
687 return 0;
688}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/*
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800691 * Apply default settings to the psmouse structure. Most of them will
692 * be overridden by individual protocol initialization routines.
693 */
694
695static void psmouse_apply_defaults(struct psmouse *psmouse)
696{
697 struct input_dev *input_dev = psmouse->dev;
698
699 memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
700 memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
701 memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
702 memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
703 memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));
704
705 __set_bit(EV_KEY, input_dev->evbit);
706 __set_bit(EV_REL, input_dev->evbit);
707
708 __set_bit(BTN_LEFT, input_dev->keybit);
709 __set_bit(BTN_RIGHT, input_dev->keybit);
710
711 __set_bit(REL_X, input_dev->relbit);
712 __set_bit(REL_Y, input_dev->relbit);
713
Hans de Goede01d4cd52014-09-08 14:44:05 -0700714 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
715
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800716 psmouse->set_rate = psmouse_set_rate;
717 psmouse->set_resolution = psmouse_set_resolution;
718 psmouse->poll = psmouse_poll;
719 psmouse->protocol_handler = psmouse_process_byte;
720 psmouse->pktsize = 3;
721 psmouse->reconnect = NULL;
722 psmouse->disconnect = NULL;
723 psmouse->cleanup = NULL;
724 psmouse->pt_activate = NULL;
725 psmouse->pt_deactivate = NULL;
726}
727
728/*
729 * Apply default settings to the psmouse structure and call specified
730 * protocol detection or initialization routine.
731 */
732static int psmouse_do_detect(int (*detect)(struct psmouse *psmouse,
733 bool set_properties),
734 struct psmouse *psmouse, bool set_properties)
735{
736 if (set_properties)
737 psmouse_apply_defaults(psmouse);
738
739 return detect(psmouse, set_properties);
740}
741
742/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
744 * the mouse may have.
745 */
746
747static int psmouse_extensions(struct psmouse *psmouse,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700748 unsigned int max_proto, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Jiri Kosina06989892009-11-16 22:12:13 -0800750 bool synaptics_hardware = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Hans de Goede3ace3682014-09-12 17:24:47 -0700752/* Always check for focaltech, this is safe as it uses pnp-id matching */
753 if (psmouse_do_detect(focaltech_detect, psmouse, set_properties) == 0) {
Mathias Gottschlag05be1d02014-12-29 09:26:35 -0800754 if (max_proto > PSMOUSE_IMEX) {
755 if (!set_properties || focaltech_init(psmouse) == 0) {
Dmitry Torokhov290b7992014-12-29 12:06:38 -0800756 if (IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH))
Mathias Gottschlag05be1d02014-12-29 09:26:35 -0800757 return PSMOUSE_FOCALTECH;
758 /*
759 * Note that we need to also restrict
760 * psmouse_max_proto so that psmouse_initialize()
761 * does not try to reset rate and resolution,
762 * because even that upsets the device.
763 */
764 psmouse_max_proto = PSMOUSE_PS2;
765 return PSMOUSE_PS2;
766 }
Hans de Goede3ace3682014-09-12 17:24:47 -0700767 }
768 }
769
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500770/*
771 * We always check for lifebook because it does not disturb mouse
772 * (it only checks DMI information).
773 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800774 if (psmouse_do_detect(lifebook_detect, psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500775 if (max_proto > PSMOUSE_IMEX) {
776 if (!set_properties || lifebook_init(psmouse) == 0)
777 return PSMOUSE_LIFEBOOK;
778 }
779 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781/*
782 * Try Kensington ThinkingMouse (we try first, because synaptics probe
783 * upsets the thinkingmouse).
784 */
785
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800786 if (max_proto > PSMOUSE_IMEX &&
787 psmouse_do_detect(thinking_detect, psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return PSMOUSE_THINKPS;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500792 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
793 * support is disabled in config - we need to know if it is synaptics so we
794 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800796 if (max_proto > PSMOUSE_PS2 &&
797 psmouse_do_detect(synaptics_detect, psmouse, set_properties) == 0) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700798 synaptics_hardware = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (max_proto > PSMOUSE_IMEX) {
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800801/*
802 * Try activating protocol, but check if support is enabled first, since
803 * we try detecting Synaptics even when protocol is disabled.
804 */
Dmitry Torokhov290b7992014-12-29 12:06:38 -0800805 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) &&
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800806 (!set_properties || synaptics_init(psmouse) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return PSMOUSE_SYNAPTICS;
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800808 }
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810/*
811 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
812 * Unfortunately Logitech/Genius probes confuse some firmware versions so
813 * we'll have to skip them.
814 */
815 max_proto = PSMOUSE_IMEX;
816 }
817/*
818 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
819 */
820 synaptics_reset(psmouse);
821 }
822
823/*
Dudley Du0799a922013-01-05 00:14:22 -0800824 * Try Cypress Trackpad.
825 * Must try it before Finger Sensing Pad because Finger Sensing Pad probe
826 * upsets some modules of Cypress Trackpads.
827 */
828 if (max_proto > PSMOUSE_IMEX &&
829 cypress_detect(psmouse, set_properties) == 0) {
Dmitry Torokhov290b7992014-12-29 12:06:38 -0800830 if (IS_ENABLED(CONFIG_MOUSE_PS2_CYPRESS)) {
Dudley Du0799a922013-01-05 00:14:22 -0800831 if (cypress_init(psmouse) == 0)
832 return PSMOUSE_CYPRESS;
833
834 /*
835 * Finger Sensing Pad probe upsets some modules of
836 * Cypress Trackpad, must avoid Finger Sensing Pad
837 * probe if Cypress Trackpad device detected.
838 */
839 return PSMOUSE_PS2;
840 }
841
842 max_proto = PSMOUSE_IMEX;
843 }
844
845/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * Try ALPS TouchPad
847 */
848 if (max_proto > PSMOUSE_IMEX) {
849 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800850 if (psmouse_do_detect(alps_detect,
851 psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!set_properties || alps_init(psmouse) == 0)
853 return PSMOUSE_ALPS;
854/*
855 * Init failed, try basic relative protocols
856 */
857 max_proto = PSMOUSE_IMEX;
858 }
859 }
860
Andres Salomondf08ef22008-09-16 12:30:34 -0400861/*
862 * Try OLPC HGPK touchpad.
863 */
864 if (max_proto > PSMOUSE_IMEX &&
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800865 psmouse_do_detect(hgpk_detect, psmouse, set_properties) == 0) {
Andres Salomondf08ef22008-09-16 12:30:34 -0400866 if (!set_properties || hgpk_init(psmouse) == 0)
867 return PSMOUSE_HGPK;
868/*
869 * Init failed, try basic relative protocols
870 */
871 max_proto = PSMOUSE_IMEX;
872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400874/*
875 * Try Elantech touchpad.
876 */
877 if (max_proto > PSMOUSE_IMEX &&
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800878 psmouse_do_detect(elantech_detect, psmouse, set_properties) == 0) {
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400879 if (!set_properties || elantech_init(psmouse) == 0)
880 return PSMOUSE_ELANTECH;
881/*
882 * Init failed, try basic relative protocols
883 */
884 max_proto = PSMOUSE_IMEX;
885 }
886
Andres Salomondf08ef22008-09-16 12:30:34 -0400887 if (max_proto > PSMOUSE_IMEX) {
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800888 if (psmouse_do_detect(genius_detect,
889 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500890 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800892 if (psmouse_do_detect(ps2pp_init,
893 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500894 return PSMOUSE_PS2PP;
895
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800896 if (psmouse_do_detect(trackpoint_detect,
897 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500898 return PSMOUSE_TRACKPOINT;
899
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800900 if (psmouse_do_detect(touchkit_ps2_detect,
901 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500902 return PSMOUSE_TOUCHKIT_PS2;
903 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/*
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -0800906 * Try Finger Sensing Pad. We do it here because its probe upsets
907 * Trackpoint devices (causing TP_READ_ID command to time out).
908 */
909 if (max_proto > PSMOUSE_IMEX) {
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800910 if (psmouse_do_detect(fsp_detect,
911 psmouse, set_properties) == 0) {
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -0800912 if (!set_properties || fsp_init(psmouse) == 0)
913 return PSMOUSE_FSP;
914/*
915 * Init failed, try basic relative protocols
916 */
917 max_proto = PSMOUSE_IMEX;
918 }
919 }
920
921/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400923 * protocol probes. Note that we follow up with full reset because
924 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
Alon Ziv554fc192007-08-30 00:22:48 -0400926 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500927 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800929 if (max_proto >= PSMOUSE_IMEX &&
930 psmouse_do_detect(im_explorer_detect,
931 psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return PSMOUSE_IMEX;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800935 if (max_proto >= PSMOUSE_IMPS &&
936 psmouse_do_detect(intellimouse_detect,
937 psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return PSMOUSE_IMPS;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941/*
942 * Okay, all failed, we have a standard mouse here. The number of the buttons
943 * is still a question, though. We assume 3.
944 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800945 psmouse_do_detect(ps2bare_detect, psmouse, set_properties);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 if (synaptics_hardware) {
948/*
949 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
950 * We need to reset the touchpad because if there is a track point on the
951 * pass through port it could get disabled while probing for protocol
952 * extensions.
953 */
954 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956
957 return PSMOUSE_PS2;
958}
959
Helge Dellere38de672006-09-10 21:54:39 -0400960static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500961 {
962 .type = PSMOUSE_PS2,
963 .name = "PS/2",
964 .alias = "bare",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700965 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700966 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500967 .detect = ps2bare_detect,
968 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500969#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500970 {
971 .type = PSMOUSE_PS2PP,
972 .name = "PS2++",
973 .alias = "logitech",
974 .detect = ps2pp_init,
975 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500976#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500977 {
978 .type = PSMOUSE_THINKPS,
979 .name = "ThinkPS/2",
980 .alias = "thinkps",
981 .detect = thinking_detect,
982 },
Dudley Du0799a922013-01-05 00:14:22 -0800983#ifdef CONFIG_MOUSE_PS2_CYPRESS
984 {
985 .type = PSMOUSE_CYPRESS,
986 .name = "CyPS/2",
987 .alias = "cypress",
988 .detect = cypress_detect,
989 .init = cypress_init,
990 },
991#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500992 {
993 .type = PSMOUSE_GENPS,
994 .name = "GenPS/2",
995 .alias = "genius",
996 .detect = genius_detect,
997 },
998 {
999 .type = PSMOUSE_IMPS,
1000 .name = "ImPS/2",
1001 .alias = "imps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001002 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001003 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001004 .detect = intellimouse_detect,
1005 },
1006 {
1007 .type = PSMOUSE_IMEX,
1008 .name = "ImExPS/2",
1009 .alias = "exps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001010 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001011 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001012 .detect = im_explorer_detect,
1013 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001014#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001015 {
1016 .type = PSMOUSE_SYNAPTICS,
1017 .name = "SynPS/2",
1018 .alias = "synaptics",
1019 .detect = synaptics_detect,
1020 .init = synaptics_init,
1021 },
Daniel Drake7968a5d2011-11-08 00:00:35 -08001022 {
1023 .type = PSMOUSE_SYNAPTICS_RELATIVE,
1024 .name = "SynRelPS/2",
1025 .alias = "synaptics-relative",
1026 .detect = synaptics_detect,
1027 .init = synaptics_init_relative,
1028 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001029#endif
1030#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001031 {
1032 .type = PSMOUSE_ALPS,
1033 .name = "AlpsPS/2",
1034 .alias = "alps",
1035 .detect = alps_detect,
1036 .init = alps_init,
1037 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001038#endif
1039#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001040 {
1041 .type = PSMOUSE_LIFEBOOK,
1042 .name = "LBPS/2",
1043 .alias = "lifebook",
1044 .init = lifebook_init,
1045 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001046#endif
1047#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001048 {
Stephen Evanchik541e3162005-08-08 01:26:18 -05001049 .type = PSMOUSE_TRACKPOINT,
1050 .name = "TPPS/2",
1051 .alias = "trackpoint",
1052 .detect = trackpoint_detect,
1053 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001054#endif
1055#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -05001056 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -05001057 .type = PSMOUSE_TOUCHKIT_PS2,
1058 .name = "touchkitPS/2",
1059 .alias = "touchkit",
1060 .detect = touchkit_ps2_detect,
1061 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001062#endif
Andres Salomondf08ef22008-09-16 12:30:34 -04001063#ifdef CONFIG_MOUSE_PS2_OLPC
1064 {
1065 .type = PSMOUSE_HGPK,
1066 .name = "OLPC HGPK",
1067 .alias = "hgpk",
1068 .detect = hgpk_detect,
1069 },
1070#endif
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04001071#ifdef CONFIG_MOUSE_PS2_ELANTECH
1072 {
1073 .type = PSMOUSE_ELANTECH,
1074 .name = "ETPS/2",
1075 .alias = "elantech",
1076 .detect = elantech_detect,
1077 .init = elantech_init,
1078 },
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -07001079#endif
1080#ifdef CONFIG_MOUSE_PS2_SENTELIC
1081 {
1082 .type = PSMOUSE_FSP,
1083 .name = "FSPPS/2",
1084 .alias = "fsp",
1085 .detect = fsp_detect,
1086 .init = fsp_init,
1087 },
1088#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -05001089 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -04001090 .type = PSMOUSE_CORTRON,
1091 .name = "CortronPS/2",
1092 .alias = "cortps",
1093 .detect = cortron_detect,
1094 },
Mathias Gottschlag05be1d02014-12-29 09:26:35 -08001095#ifdef CONFIG_MOUSE_PS2_FOCALTECH
1096 {
1097 .type = PSMOUSE_FOCALTECH,
1098 .name = "FocalTechPS/2",
1099 .alias = "focaltech",
1100 .detect = focaltech_detect,
1101 .init = focaltech_init,
1102 },
1103#endif
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -04001104 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001105 .type = PSMOUSE_AUTO,
1106 .name = "auto",
1107 .alias = "any",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001108 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001109 },
1110};
1111
Helge Dellere38de672006-09-10 21:54:39 -04001112static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001113{
1114 int i;
1115
1116 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
1117 if (psmouse_protocols[i].type == type)
1118 return &psmouse_protocols[i];
1119
1120 WARN_ON(1);
1121 return &psmouse_protocols[0];
1122}
1123
Helge Dellere38de672006-09-10 21:54:39 -04001124static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001125{
Helge Dellere38de672006-09-10 21:54:39 -04001126 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001127 int i;
1128
1129 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
1130 p = &psmouse_protocols[i];
1131
1132 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
1133 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
1134 return &psmouse_protocols[i];
1135 }
1136
1137 return NULL;
1138}
1139
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141/*
1142 * psmouse_probe() probes for a PS/2 mouse.
1143 */
1144
1145static int psmouse_probe(struct psmouse *psmouse)
1146{
1147 struct ps2dev *ps2dev = &psmouse->ps2dev;
1148 unsigned char param[2];
1149
1150/*
1151 * First, we check if it's a mouse. It should send 0x00 or 0x03
1152 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -05001153 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
1154 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
1156
1157 param[0] = 0xa5;
1158 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1159 return -1;
1160
Vojtech Pavlik7741e932005-05-28 02:11:42 -05001161 if (param[0] != 0x00 && param[0] != 0x03 &&
1162 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return -1;
1164
1165/*
1166 * Then we reset and disable the mouse so that it doesn't generate events.
1167 */
1168
1169 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001170 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
1171 ps2dev->serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 return 0;
1174}
1175
1176/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 * psmouse_initialize() initializes the mouse to a sane state.
1178 */
1179
1180static void psmouse_initialize(struct psmouse *psmouse)
1181{
1182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 * We set the mouse report rate, resolution and scaling.
1184 */
1185
1186 if (psmouse_max_proto != PSMOUSE_PS2) {
1187 psmouse->set_rate(psmouse, psmouse->rate);
1188 psmouse->set_resolution(psmouse, psmouse->resolution);
1189 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
1190 }
1191}
1192
1193/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 * psmouse_activate() enables the mouse so that we get motion reports from it.
1195 */
1196
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001197int psmouse_activate(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001199 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001200 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1201 psmouse->ps2dev.serio->phys);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001202 return -1;
1203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209/*
1210 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +02001211 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 */
1213
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001214int psmouse_deactivate(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001216 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001217 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1218 psmouse->ps2dev.serio->phys);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001219 return -1;
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001226
1227/*
1228 * psmouse_resync() attempts to re-validate current protocol.
1229 */
1230
David Howellsc4028952006-11-22 14:57:56 +00001231static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001232{
David Howellsc4028952006-11-22 14:57:56 +00001233 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -04001234 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001235 struct serio *serio = psmouse->ps2dev.serio;
1236 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001237 bool failed = false, enabled = false;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001238 int i;
1239
Ingo Molnarc14471d2006-02-19 00:22:11 -05001240 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001241
1242 if (psmouse->state != PSMOUSE_RESYNCING)
1243 goto out;
1244
1245 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1246 parent = serio_get_drvdata(serio->parent);
1247 psmouse_deactivate(parent);
1248 }
1249
1250/*
1251 * Some mice don't ACK commands sent while they are in the middle of
1252 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1253 * instead of ps2_command() which would wait for 200ms for an ACK
1254 * that may never come.
1255 * As an additional quirk ALPS touchpads may not only forget to ACK
1256 * disable command but will stop reporting taps, so if we see that
1257 * mouse at least once ACKs disable we will do full reconnect if ACK
1258 * is missing.
1259 */
1260 psmouse->num_resyncs++;
1261
1262 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1263 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001264 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001265 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001266 psmouse->acks_disable_command = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001267
1268/*
1269 * Poll the mouse. If it was reset the packet will be shorter than
1270 * psmouse->pktsize and ps2_command will fail. We do not expect and
1271 * do not handle scenario when mouse "upgrades" its protocol while
1272 * disconnected since it would require additional delay. If we ever
1273 * see a mouse that does it we'll adjust the code.
1274 */
1275 if (!failed) {
1276 if (psmouse->poll(psmouse))
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001277 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001278 else {
1279 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1280 for (i = 0; i < psmouse->pktsize; i++) {
1281 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001282 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001283 if (rc != PSMOUSE_GOOD_DATA)
1284 break;
1285 }
1286 if (rc != PSMOUSE_FULL_PACKET)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001287 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001288 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1289 }
1290 }
1291/*
1292 * Now try to enable mouse. We try to do that even if poll failed and also
1293 * repeat our attempts 5 times, otherwise we may be left out with disabled
1294 * mouse.
1295 */
1296 for (i = 0; i < 5; i++) {
1297 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001298 enabled = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001299 break;
1300 }
1301 msleep(200);
1302 }
1303
1304 if (!enabled) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001305 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1306 psmouse->ps2dev.serio->phys);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001307 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001308 }
1309
1310 if (failed) {
1311 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001312 psmouse_info(psmouse,
1313 "resync failed, issuing reconnect request\n");
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001314 serio_reconnect(serio);
1315 } else
1316 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1317
1318 if (parent)
1319 psmouse_activate(parent);
1320 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001321 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001322}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324/*
1325 * psmouse_cleanup() resets the mouse into power-on state.
1326 */
1327
1328static void psmouse_cleanup(struct serio *serio)
1329{
1330 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001331 struct psmouse *parent = NULL;
1332
1333 mutex_lock(&psmouse_mutex);
1334
1335 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1336 parent = serio_get_drvdata(serio->parent);
1337 psmouse_deactivate(parent);
1338 }
1339
Dmitry Torokhova9f0c382010-02-07 23:10:04 -08001340 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1341
1342 /*
1343 * Disable stream mode so cleanup routine can proceed undisturbed.
1344 */
1345 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001346 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1347 psmouse->ps2dev.serio->phys);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001348
1349 if (psmouse->cleanup)
1350 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Dmitry Torokhov4a299bf2009-12-24 21:40:24 -08001352/*
1353 * Reset the mouse to defaults (bare PS/2 protocol).
1354 */
1355 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001356
1357/*
1358 * Some boxes, such as HP nx7400, get terribly confused if mouse
1359 * is not fully enabled before suspending/shutting down.
1360 */
1361 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1362
1363 if (parent) {
1364 if (parent->pt_deactivate)
1365 parent->pt_deactivate(parent);
1366
1367 psmouse_activate(parent);
1368 }
1369
1370 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373/*
1374 * psmouse_disconnect() closes and frees.
1375 */
1376
1377static void psmouse_disconnect(struct serio *serio)
1378{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001379 struct psmouse *psmouse, *parent = NULL;
1380
1381 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001383 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Ingo Molnarc14471d2006-02-19 00:22:11 -05001385 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1388
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001389 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001390 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001391 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001392 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1395 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001396 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
1399 if (psmouse->disconnect)
1400 psmouse->disconnect(psmouse);
1401
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001402 if (parent && parent->pt_deactivate)
1403 parent->pt_deactivate(parent);
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 serio_close(serio);
1408 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001409 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001411
1412 if (parent)
1413 psmouse_activate(parent);
1414
Ingo Molnarc14471d2006-02-19 00:22:11 -05001415 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001418static int psmouse_switch_protocol(struct psmouse *psmouse,
1419 const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001420{
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001421 const struct psmouse_protocol *selected_proto;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001422 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001423
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001424 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001425
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001426 if (proto && (proto->detect || proto->init)) {
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001427 psmouse_apply_defaults(psmouse);
1428
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001429 if (proto->detect && proto->detect(psmouse, true) < 0)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001430 return -1;
1431
1432 if (proto->init && proto->init(psmouse) < 0)
1433 return -1;
1434
1435 psmouse->type = proto->type;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001436 selected_proto = proto;
1437 } else {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001438 psmouse->type = psmouse_extensions(psmouse,
1439 psmouse_max_proto, true);
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001440 selected_proto = psmouse_protocol_by_type(psmouse->type);
1441 }
1442
1443 psmouse->ignore_parity = selected_proto->ignore_parity;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001444
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001445 /*
1446 * If mouse's packet size is 3 there is no point in polling the
1447 * device in hopes to detect protocol reset - we won't get less
1448 * than 3 bytes response anyhow.
1449 */
1450 if (psmouse->pktsize == 3)
1451 psmouse->resync_time = 0;
1452
1453 /*
1454 * Some smart KVMs fake response to POLL command returning just
1455 * 3 bytes and messing up our resync logic, so if initial poll
1456 * fails we won't try polling the device anymore. Hopefully
1457 * such KVM will maintain initially selected protocol.
1458 */
1459 if (psmouse->resync_time && psmouse->poll(psmouse))
1460 psmouse->resync_time = 0;
1461
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001462 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001463 selected_proto->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001464
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001465 input_dev->name = psmouse->devname;
1466 input_dev->phys = psmouse->phys;
1467 input_dev->id.bustype = BUS_I8042;
1468 input_dev->id.vendor = 0x0002;
1469 input_dev->id.product = psmouse->type;
1470 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001471
1472 return 0;
1473}
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475/*
1476 * psmouse_connect() is a callback from the serio module when
1477 * an unhandled serio port is found.
1478 */
1479static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1480{
1481 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001482 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001483 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Ingo Molnarc14471d2006-02-19 00:22:11 -05001485 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /*
1488 * If this is a pass-through port deactivate parent so the device
1489 * connected to this port can be successfully identified
1490 */
1491 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1492 parent = serio_get_drvdata(serio->parent);
1493 psmouse_deactivate(parent);
1494 }
1495
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001496 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1497 input_dev = input_allocate_device();
1498 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001499 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001502 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001503 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001504 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1507
1508 serio_set_drvdata(serio, psmouse);
1509
Dmitry Torokhov72155612006-11-05 22:40:19 -05001510 error = serio_open(serio, drv);
1511 if (error)
1512 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001515 error = -ENODEV;
1516 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518
1519 psmouse->rate = psmouse_rate;
1520 psmouse->resolution = psmouse_resolution;
1521 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001522 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001525 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 psmouse_initialize(psmouse);
1529
Dmitry Torokhov72155612006-11-05 22:40:19 -05001530 error = input_register_device(psmouse->dev);
1531 if (error)
1532 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (parent && parent->pt_activate)
1535 parent->pt_activate(parent);
1536
Dmitry Torokhov72155612006-11-05 22:40:19 -05001537 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1538 if (error)
1539 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 psmouse_activate(psmouse);
1542
Dmitry Torokhov72155612006-11-05 22:40:19 -05001543 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001544 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (parent)
1546 psmouse_activate(parent);
1547
Ingo Molnarc14471d2006-02-19 00:22:11 -05001548 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001550
1551 err_pt_deactivate:
1552 if (parent && parent->pt_deactivate)
1553 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001554 input_unregister_device(psmouse->dev);
1555 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001556 err_protocol_disconnect:
1557 if (psmouse->disconnect)
1558 psmouse->disconnect(psmouse);
1559 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1560 err_close_serio:
1561 serio_close(serio);
1562 err_clear_drvdata:
1563 serio_set_drvdata(serio, NULL);
1564 err_free:
1565 input_free_device(input_dev);
1566 kfree(psmouse);
1567
1568 retval = error;
1569 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
1572
1573static int psmouse_reconnect(struct serio *serio)
1574{
1575 struct psmouse *psmouse = serio_get_drvdata(serio);
1576 struct psmouse *parent = NULL;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001577 unsigned char type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 int rc = -1;
1579
Ingo Molnarc14471d2006-02-19 00:22:11 -05001580 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1583 parent = serio_get_drvdata(serio->parent);
1584 psmouse_deactivate(parent);
1585 }
1586
1587 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1588
1589 if (psmouse->reconnect) {
1590 if (psmouse->reconnect(psmouse))
1591 goto out;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001592 } else {
1593 psmouse_reset(psmouse);
1594
1595 if (psmouse_probe(psmouse) < 0)
1596 goto out;
1597
1598 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1599 if (psmouse->type != type)
1600 goto out;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001603 /*
1604 * OK, the device type (and capabilities) match the old one,
1605 * we can continue using it, complete initialization
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 */
1607 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1608
1609 psmouse_initialize(psmouse);
1610
1611 if (parent && parent->pt_activate)
1612 parent->pt_activate(parent);
1613
1614 psmouse_activate(psmouse);
1615 rc = 0;
1616
1617out:
1618 /* If this is a pass-through port the parent waits to be activated */
1619 if (parent)
1620 psmouse_activate(parent);
1621
Ingo Molnarc14471d2006-02-19 00:22:11 -05001622 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return rc;
1624}
1625
1626static struct serio_device_id psmouse_serio_ids[] = {
1627 {
1628 .type = SERIO_8042,
1629 .proto = SERIO_ANY,
1630 .id = SERIO_ANY,
1631 .extra = SERIO_ANY,
1632 },
1633 {
1634 .type = SERIO_PS_PSTHRU,
1635 .proto = SERIO_ANY,
1636 .id = SERIO_ANY,
1637 .extra = SERIO_ANY,
1638 },
1639 { 0 }
1640};
1641
1642MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1643
1644static struct serio_driver psmouse_drv = {
1645 .driver = {
1646 .name = "psmouse",
1647 },
1648 .description = DRIVER_DESC,
1649 .id_table = psmouse_serio_ids,
1650 .interrupt = psmouse_interrupt,
1651 .connect = psmouse_connect,
1652 .reconnect = psmouse_reconnect,
1653 .disconnect = psmouse_disconnect,
1654 .cleanup = psmouse_cleanup,
1655};
1656
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001657ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1658 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001661 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1662 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001664 psmouse = serio_get_drvdata(serio);
1665
Eric W. Biederman59b01512010-01-05 17:56:02 -08001666 return attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001669ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1670 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001673 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1674 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int retval;
1676
Ingo Molnarc14471d2006-02-19 00:22:11 -05001677 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001678 if (retval)
Eric W. Biederman59b01512010-01-05 17:56:02 -08001679 goto out;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001680
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001681 psmouse = serio_get_drvdata(serio);
1682
Andres Salomon68d48222008-09-16 12:30:34 -04001683 if (attr->protect) {
1684 if (psmouse->state == PSMOUSE_IGNORE) {
1685 retval = -ENODEV;
1686 goto out_unlock;
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Andres Salomon68d48222008-09-16 12:30:34 -04001689 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1690 parent = serio_get_drvdata(serio->parent);
1691 psmouse_deactivate(parent);
1692 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001693
Andres Salomon68d48222008-09-16 12:30:34 -04001694 psmouse_deactivate(psmouse);
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001697 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Andres Salomon68d48222008-09-16 12:30:34 -04001699 if (attr->protect) {
1700 if (retval != -ENODEV)
1701 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001702
Andres Salomon68d48222008-09-16 12:30:34 -04001703 if (parent)
1704 psmouse_activate(parent);
1705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Ingo Molnarc14471d2006-02-19 00:22:11 -05001707 out_unlock:
1708 mutex_unlock(&psmouse_mutex);
Eric W. Biederman59b01512010-01-05 17:56:02 -08001709 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return retval;
1711}
1712
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001713static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1714{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001715 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001716
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001717 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001718}
1719
1720static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1721{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001722 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
JJ Ding76496e72011-11-09 10:20:14 -08001723 unsigned int value;
1724 int err;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001725
JJ Ding76496e72011-11-09 10:20:14 -08001726 err = kstrtouint(buf, 10, &value);
1727 if (err)
1728 return err;
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001729
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001730 *field = value;
1731
1732 return count;
1733}
1734
1735static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001736{
1737 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1738}
1739
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001740static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001741{
1742 struct serio *serio = psmouse->ps2dev.serio;
1743 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001744 struct input_dev *old_dev, *new_dev;
1745 const struct psmouse_protocol *proto, *old_proto;
1746 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001747 int retry = 0;
1748
Dmitry Torokhov72155612006-11-05 22:40:19 -05001749 proto = psmouse_protocol_by_name(buf, count);
1750 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001751 return -EINVAL;
1752
1753 if (psmouse->type == proto->type)
1754 return count;
1755
Dmitry Torokhov72155612006-11-05 22:40:19 -05001756 new_dev = input_allocate_device();
1757 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001758 return -ENOMEM;
1759
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -07001760 while (!list_empty(&serio->children)) {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001761 if (++retry > 3) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001762 psmouse_warn(psmouse,
1763 "failed to destroy children ports, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001764 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001765 return -EIO;
1766 }
1767
Ingo Molnarc14471d2006-02-19 00:22:11 -05001768 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001769 serio_unregister_child_port(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001770 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001771
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001772 if (serio->drv != &psmouse_drv) {
1773 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001774 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001775 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001776
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001777 if (psmouse->type == proto->type) {
1778 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001779 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001780 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001781 }
1782
1783 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1784 parent = serio_get_drvdata(serio->parent);
1785 if (parent->pt_deactivate)
1786 parent->pt_deactivate(parent);
1787 }
1788
Dmitry Torokhov72155612006-11-05 22:40:19 -05001789 old_dev = psmouse->dev;
1790 old_proto = psmouse_protocol_by_type(psmouse->type);
1791
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001792 if (psmouse->disconnect)
1793 psmouse->disconnect(psmouse);
1794
1795 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001796
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001797 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001798 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1799
1800 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1801 psmouse_reset(psmouse);
1802 /* default to PSMOUSE_PS2 */
1803 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1804 }
1805
1806 psmouse_initialize(psmouse);
1807 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1808
Dmitry Torokhov72155612006-11-05 22:40:19 -05001809 error = input_register_device(psmouse->dev);
1810 if (error) {
1811 if (psmouse->disconnect)
1812 psmouse->disconnect(psmouse);
1813
1814 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1815 input_free_device(new_dev);
1816 psmouse->dev = old_dev;
1817 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1818 psmouse_switch_protocol(psmouse, old_proto);
1819 psmouse_initialize(psmouse);
1820 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1821
1822 return error;
1823 }
1824
1825 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001826
1827 if (parent && parent->pt_activate)
1828 parent->pt_activate(parent);
1829
1830 return count;
1831}
1832
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001833static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
JJ Ding76496e72011-11-09 10:20:14 -08001835 unsigned int value;
1836 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
JJ Ding76496e72011-11-09 10:20:14 -08001838 err = kstrtouint(buf, 10, &value);
1839 if (err)
1840 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 psmouse->set_rate(psmouse, value);
1843 return count;
1844}
1845
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001846static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
JJ Ding76496e72011-11-09 10:20:14 -08001848 unsigned int value;
1849 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
JJ Ding76496e72011-11-09 10:20:14 -08001851 err = kstrtouint(buf, 10, &value);
1852 if (err)
1853 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 psmouse->set_resolution(psmouse, value);
1856 return count;
1857}
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001860static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Helge Dellere38de672006-09-10 21:54:39 -04001862 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 if (!val)
1865 return -EINVAL;
1866
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001867 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001869 if (!proto || !proto->maxproto)
1870 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001872 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Stephen Evanchik541e3162005-08-08 01:26:18 -05001874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001877static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001879 int type = *((unsigned int *)kp->arg);
1880
Takashi Iwai3d4c3aa2009-11-12 23:30:52 -08001881 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
1884static int __init psmouse_init(void)
1885{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001886 int err;
1887
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001888 lifebook_module_init();
1889 synaptics_module_init();
Daniel Drakeca94ec42010-11-11 22:19:57 -08001890 hgpk_module_init();
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001891
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001892 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1893 if (!kpsmoused_wq) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001894 pr_err("failed to create kpsmoused workqueue\n");
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001895 return -ENOMEM;
1896 }
1897
Akinobu Mita153a9df02006-11-23 23:35:10 -05001898 err = serio_register_driver(&psmouse_drv);
1899 if (err)
1900 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001901
Akinobu Mita153a9df02006-11-23 23:35:10 -05001902 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
1905static void __exit psmouse_exit(void)
1906{
1907 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001908 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911module_init(psmouse_init);
1912module_exit(psmouse_exit);