blob: 27057df7ba74eeeba06f24f0e24fcf6d4387eda3 [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/*
Mathias Gottschlag4ec212f2015-03-07 13:32:10 -0800457 * Here we set the mouse scaling.
458 */
459
460static void psmouse_set_scale(struct psmouse *psmouse, enum psmouse_scale scale)
461{
462 ps2_command(&psmouse->ps2dev, NULL,
463 scale == PSMOUSE_SCALE21 ? PSMOUSE_CMD_SETSCALE21 :
464 PSMOUSE_CMD_SETSCALE11);
465}
466
467/*
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800468 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
469 */
470
471static int psmouse_poll(struct psmouse *psmouse)
472{
473 return ps2_command(&psmouse->ps2dev, psmouse->packet,
474 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
475}
476
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800477static bool psmouse_check_pnp_id(const char *id, const char * const ids[])
478{
479 int i;
480
481 for (i = 0; ids[i]; i++)
482 if (!strcasecmp(id, ids[i]))
483 return true;
484
485 return false;
486}
487
Hans de Goede2c75ada2014-09-11 10:14:09 -0700488/*
489 * psmouse_matches_pnp_id - check if psmouse matches one of the passed in ids.
490 */
491bool psmouse_matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
492{
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800493 struct serio *serio = psmouse->ps2dev.serio;
494 char *p, *fw_id_copy, *save_ptr;
495 bool found = false;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700496
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800497 if (strncmp(serio->firmware_id, "PNP: ", 5))
498 return false;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700499
Dmitry Torokhov99e14c12015-02-27 16:17:59 -0800500 fw_id_copy = kstrndup(&serio->firmware_id[5],
501 sizeof(serio->firmware_id) - 5,
502 GFP_KERNEL);
503 if (!fw_id_copy)
504 return false;
505
506 save_ptr = fw_id_copy;
507 while ((p = strsep(&fw_id_copy, " ")) != NULL) {
508 if (psmouse_check_pnp_id(p, ids)) {
509 found = true;
510 break;
511 }
512 }
513
514 kfree(save_ptr);
515 return found;
Hans de Goede2c75ada2014-09-11 10:14:09 -0700516}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518/*
519 * Genius NetMouse magic init.
520 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700521static int genius_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct ps2dev *ps2dev = &psmouse->ps2dev;
524 unsigned char param[4];
525
526 param[0] = 3;
527 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
528 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
529 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
530 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
531 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
532
533 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
534 return -1;
535
536 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800537 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700538 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
539 __set_bit(BTN_SIDE, psmouse->dev->keybit);
540 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500543 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 psmouse->pktsize = 4;
545 }
546
547 return 0;
548}
549
550/*
551 * IntelliMouse magic init.
552 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700553static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct ps2dev *ps2dev = &psmouse->ps2dev;
556 unsigned char param[2];
557
558 param[0] = 200;
559 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
560 param[0] = 100;
561 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
562 param[0] = 80;
563 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
564 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
565
566 if (param[0] != 3)
567 return -1;
568
569 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700570 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
571 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800573 if (!psmouse->vendor)
574 psmouse->vendor = "Generic";
575 if (!psmouse->name)
576 psmouse->name = "Wheel Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 psmouse->pktsize = 4;
578 }
579
580 return 0;
581}
582
583/*
584 * Try IntelliMouse/Explorer magic init.
585 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700586static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 struct ps2dev *ps2dev = &psmouse->ps2dev;
589 unsigned char param[2];
590
591 intellimouse_detect(psmouse, 0);
592
593 param[0] = 200;
594 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
595 param[0] = 200;
596 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
597 param[0] = 80;
598 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
599 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
600
601 if (param[0] != 4)
602 return -1;
603
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400604/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
605 param[0] = 200;
606 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
607 param[0] = 80;
608 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
609 param[0] = 40;
610 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700613 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
614 __set_bit(REL_WHEEL, psmouse->dev->relbit);
615 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
616 __set_bit(BTN_SIDE, psmouse->dev->keybit);
617 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800619 if (!psmouse->vendor)
620 psmouse->vendor = "Generic";
621 if (!psmouse->name)
622 psmouse->name = "Explorer Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 psmouse->pktsize = 4;
624 }
625
626 return 0;
627}
628
629/*
630 * Kensington ThinkingMouse / ExpertMouse magic init.
631 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700632static int thinking_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct ps2dev *ps2dev = &psmouse->ps2dev;
635 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400636 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 int i;
638
639 param[0] = 10;
640 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
641 param[0] = 0;
642 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400643 for (i = 0; i < ARRAY_SIZE(seq); i++) {
644 param[0] = seq[i];
645 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
648
649 if (param[0] != 2)
650 return -1;
651
652 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800653 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700654 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 psmouse->vendor = "Kensington";
657 psmouse->name = "ThinkingMouse";
658 }
659
660 return 0;
661}
662
663/*
664 * Bare PS/2 protocol "detection". Always succeeds.
665 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700666static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500668 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800669 if (!psmouse->vendor)
670 psmouse->vendor = "Generic";
671 if (!psmouse->name)
672 psmouse->name = "Mouse";
673
674/*
675 * We have no way of figuring true number of buttons so let's
676 * assume that the device has 3.
677 */
678 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 return 0;
682}
683
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400684/*
685 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
686 * must be forced by sysfs protocol writing.
687 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700688static int cortron_detect(struct psmouse *psmouse, bool set_properties)
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400689{
690 if (set_properties) {
691 psmouse->vendor = "Cortron";
692 psmouse->name = "PS/2 Trackball";
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800693
694 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700695 __set_bit(BTN_SIDE, psmouse->dev->keybit);
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400696 }
697
698 return 0;
699}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/*
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800702 * Apply default settings to the psmouse structure. Most of them will
703 * be overridden by individual protocol initialization routines.
704 */
705
706static void psmouse_apply_defaults(struct psmouse *psmouse)
707{
708 struct input_dev *input_dev = psmouse->dev;
709
710 memset(input_dev->evbit, 0, sizeof(input_dev->evbit));
711 memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
712 memset(input_dev->relbit, 0, sizeof(input_dev->relbit));
713 memset(input_dev->absbit, 0, sizeof(input_dev->absbit));
714 memset(input_dev->mscbit, 0, sizeof(input_dev->mscbit));
715
716 __set_bit(EV_KEY, input_dev->evbit);
717 __set_bit(EV_REL, input_dev->evbit);
718
719 __set_bit(BTN_LEFT, input_dev->keybit);
720 __set_bit(BTN_RIGHT, input_dev->keybit);
721
722 __set_bit(REL_X, input_dev->relbit);
723 __set_bit(REL_Y, input_dev->relbit);
724
Hans de Goede01d4cd52014-09-08 14:44:05 -0700725 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
726
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800727 psmouse->set_rate = psmouse_set_rate;
728 psmouse->set_resolution = psmouse_set_resolution;
Mathias Gottschlag4ec212f2015-03-07 13:32:10 -0800729 psmouse->set_scale = psmouse_set_scale;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800730 psmouse->poll = psmouse_poll;
731 psmouse->protocol_handler = psmouse_process_byte;
732 psmouse->pktsize = 3;
733 psmouse->reconnect = NULL;
734 psmouse->disconnect = NULL;
735 psmouse->cleanup = NULL;
736 psmouse->pt_activate = NULL;
737 psmouse->pt_deactivate = NULL;
738}
739
740/*
741 * Apply default settings to the psmouse structure and call specified
742 * protocol detection or initialization routine.
743 */
744static int psmouse_do_detect(int (*detect)(struct psmouse *psmouse,
745 bool set_properties),
746 struct psmouse *psmouse, bool set_properties)
747{
748 if (set_properties)
749 psmouse_apply_defaults(psmouse);
750
751 return detect(psmouse, set_properties);
752}
753
754/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
756 * the mouse may have.
757 */
758
759static int psmouse_extensions(struct psmouse *psmouse,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700760 unsigned int max_proto, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Jiri Kosina06989892009-11-16 22:12:13 -0800762 bool synaptics_hardware = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Hans de Goede3ace3682014-09-12 17:24:47 -0700764/* Always check for focaltech, this is safe as it uses pnp-id matching */
765 if (psmouse_do_detect(focaltech_detect, psmouse, set_properties) == 0) {
Mathias Gottschlag05be1d02014-12-29 09:26:35 -0800766 if (max_proto > PSMOUSE_IMEX) {
767 if (!set_properties || focaltech_init(psmouse) == 0) {
Dmitry Torokhov290b7992014-12-29 12:06:38 -0800768 if (IS_ENABLED(CONFIG_MOUSE_PS2_FOCALTECH))
Mathias Gottschlag05be1d02014-12-29 09:26:35 -0800769 return PSMOUSE_FOCALTECH;
770 /*
771 * Note that we need to also restrict
772 * psmouse_max_proto so that psmouse_initialize()
773 * does not try to reset rate and resolution,
774 * because even that upsets the device.
775 */
776 psmouse_max_proto = PSMOUSE_PS2;
777 return PSMOUSE_PS2;
778 }
Hans de Goede3ace3682014-09-12 17:24:47 -0700779 }
780 }
781
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500782/*
783 * We always check for lifebook because it does not disturb mouse
784 * (it only checks DMI information).
785 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800786 if (psmouse_do_detect(lifebook_detect, psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500787 if (max_proto > PSMOUSE_IMEX) {
788 if (!set_properties || lifebook_init(psmouse) == 0)
789 return PSMOUSE_LIFEBOOK;
790 }
791 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/*
794 * Try Kensington ThinkingMouse (we try first, because synaptics probe
795 * upsets the thinkingmouse).
796 */
797
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800798 if (max_proto > PSMOUSE_IMEX &&
799 psmouse_do_detect(thinking_detect, psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return PSMOUSE_THINKPS;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500804 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
805 * support is disabled in config - we need to know if it is synaptics so we
806 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800808 if (max_proto > PSMOUSE_PS2 &&
809 psmouse_do_detect(synaptics_detect, psmouse, set_properties) == 0) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700810 synaptics_hardware = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (max_proto > PSMOUSE_IMEX) {
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800813/*
814 * Try activating protocol, but check if support is enabled first, since
815 * we try detecting Synaptics even when protocol is disabled.
816 */
Dmitry Torokhov290b7992014-12-29 12:06:38 -0800817 if (IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS) &&
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800818 (!set_properties || synaptics_init(psmouse) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return PSMOUSE_SYNAPTICS;
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800820 }
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/*
823 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
824 * Unfortunately Logitech/Genius probes confuse some firmware versions so
825 * we'll have to skip them.
826 */
827 max_proto = PSMOUSE_IMEX;
828 }
829/*
830 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
831 */
832 synaptics_reset(psmouse);
833 }
834
835/*
Dudley Du0799a922013-01-05 00:14:22 -0800836 * Try Cypress Trackpad.
837 * Must try it before Finger Sensing Pad because Finger Sensing Pad probe
838 * upsets some modules of Cypress Trackpads.
839 */
840 if (max_proto > PSMOUSE_IMEX &&
841 cypress_detect(psmouse, set_properties) == 0) {
Dmitry Torokhov290b7992014-12-29 12:06:38 -0800842 if (IS_ENABLED(CONFIG_MOUSE_PS2_CYPRESS)) {
Dudley Du0799a922013-01-05 00:14:22 -0800843 if (cypress_init(psmouse) == 0)
844 return PSMOUSE_CYPRESS;
845
846 /*
847 * Finger Sensing Pad probe upsets some modules of
848 * Cypress Trackpad, must avoid Finger Sensing Pad
849 * probe if Cypress Trackpad device detected.
850 */
851 return PSMOUSE_PS2;
852 }
853
854 max_proto = PSMOUSE_IMEX;
855 }
856
857/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * Try ALPS TouchPad
859 */
860 if (max_proto > PSMOUSE_IMEX) {
861 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800862 if (psmouse_do_detect(alps_detect,
863 psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (!set_properties || alps_init(psmouse) == 0)
865 return PSMOUSE_ALPS;
866/*
867 * Init failed, try basic relative protocols
868 */
869 max_proto = PSMOUSE_IMEX;
870 }
871 }
872
Andres Salomondf08ef22008-09-16 12:30:34 -0400873/*
874 * Try OLPC HGPK touchpad.
875 */
876 if (max_proto > PSMOUSE_IMEX &&
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800877 psmouse_do_detect(hgpk_detect, psmouse, set_properties) == 0) {
Andres Salomondf08ef22008-09-16 12:30:34 -0400878 if (!set_properties || hgpk_init(psmouse) == 0)
879 return PSMOUSE_HGPK;
880/*
881 * Init failed, try basic relative protocols
882 */
883 max_proto = PSMOUSE_IMEX;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400886/*
887 * Try Elantech touchpad.
888 */
889 if (max_proto > PSMOUSE_IMEX &&
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800890 psmouse_do_detect(elantech_detect, psmouse, set_properties) == 0) {
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400891 if (!set_properties || elantech_init(psmouse) == 0)
892 return PSMOUSE_ELANTECH;
893/*
894 * Init failed, try basic relative protocols
895 */
896 max_proto = PSMOUSE_IMEX;
897 }
898
Andres Salomondf08ef22008-09-16 12:30:34 -0400899 if (max_proto > PSMOUSE_IMEX) {
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800900 if (psmouse_do_detect(genius_detect,
901 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500902 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800904 if (psmouse_do_detect(ps2pp_init,
905 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500906 return PSMOUSE_PS2PP;
907
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800908 if (psmouse_do_detect(trackpoint_detect,
909 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500910 return PSMOUSE_TRACKPOINT;
911
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800912 if (psmouse_do_detect(touchkit_ps2_detect,
913 psmouse, set_properties) == 0)
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500914 return PSMOUSE_TOUCHKIT_PS2;
915 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/*
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -0800918 * Try Finger Sensing Pad. We do it here because its probe upsets
919 * Trackpoint devices (causing TP_READ_ID command to time out).
920 */
921 if (max_proto > PSMOUSE_IMEX) {
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800922 if (psmouse_do_detect(fsp_detect,
923 psmouse, set_properties) == 0) {
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -0800924 if (!set_properties || fsp_init(psmouse) == 0)
925 return PSMOUSE_FSP;
926/*
927 * Init failed, try basic relative protocols
928 */
929 max_proto = PSMOUSE_IMEX;
930 }
931 }
932
933/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400935 * protocol probes. Note that we follow up with full reset because
936 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
Alon Ziv554fc192007-08-30 00:22:48 -0400938 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500939 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800941 if (max_proto >= PSMOUSE_IMEX &&
942 psmouse_do_detect(im_explorer_detect,
943 psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return PSMOUSE_IMEX;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800947 if (max_proto >= PSMOUSE_IMPS &&
948 psmouse_do_detect(intellimouse_detect,
949 psmouse, set_properties) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return PSMOUSE_IMPS;
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953/*
954 * Okay, all failed, we have a standard mouse here. The number of the buttons
955 * is still a question, though. We assume 3.
956 */
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -0800957 psmouse_do_detect(ps2bare_detect, psmouse, set_properties);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (synaptics_hardware) {
960/*
961 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
962 * We need to reset the touchpad because if there is a track point on the
963 * pass through port it could get disabled while probing for protocol
964 * extensions.
965 */
966 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
969 return PSMOUSE_PS2;
970}
971
Helge Dellere38de672006-09-10 21:54:39 -0400972static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500973 {
974 .type = PSMOUSE_PS2,
975 .name = "PS/2",
976 .alias = "bare",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700977 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700978 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500979 .detect = ps2bare_detect,
980 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500981#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500982 {
983 .type = PSMOUSE_PS2PP,
984 .name = "PS2++",
985 .alias = "logitech",
986 .detect = ps2pp_init,
987 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500988#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500989 {
990 .type = PSMOUSE_THINKPS,
991 .name = "ThinkPS/2",
992 .alias = "thinkps",
993 .detect = thinking_detect,
994 },
Dudley Du0799a922013-01-05 00:14:22 -0800995#ifdef CONFIG_MOUSE_PS2_CYPRESS
996 {
997 .type = PSMOUSE_CYPRESS,
998 .name = "CyPS/2",
999 .alias = "cypress",
1000 .detect = cypress_detect,
1001 .init = cypress_init,
1002 },
1003#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001004 {
1005 .type = PSMOUSE_GENPS,
1006 .name = "GenPS/2",
1007 .alias = "genius",
1008 .detect = genius_detect,
1009 },
1010 {
1011 .type = PSMOUSE_IMPS,
1012 .name = "ImPS/2",
1013 .alias = "imps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001014 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001015 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001016 .detect = intellimouse_detect,
1017 },
1018 {
1019 .type = PSMOUSE_IMEX,
1020 .name = "ImExPS/2",
1021 .alias = "exps",
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 = im_explorer_detect,
1025 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001026#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001027 {
1028 .type = PSMOUSE_SYNAPTICS,
1029 .name = "SynPS/2",
1030 .alias = "synaptics",
1031 .detect = synaptics_detect,
1032 .init = synaptics_init,
1033 },
Daniel Drake7968a5d2011-11-08 00:00:35 -08001034 {
1035 .type = PSMOUSE_SYNAPTICS_RELATIVE,
1036 .name = "SynRelPS/2",
1037 .alias = "synaptics-relative",
1038 .detect = synaptics_detect,
1039 .init = synaptics_init_relative,
1040 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001041#endif
1042#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001043 {
1044 .type = PSMOUSE_ALPS,
1045 .name = "AlpsPS/2",
1046 .alias = "alps",
1047 .detect = alps_detect,
1048 .init = alps_init,
1049 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001050#endif
1051#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001052 {
1053 .type = PSMOUSE_LIFEBOOK,
1054 .name = "LBPS/2",
1055 .alias = "lifebook",
1056 .init = lifebook_init,
1057 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001058#endif
1059#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001060 {
Stephen Evanchik541e3162005-08-08 01:26:18 -05001061 .type = PSMOUSE_TRACKPOINT,
1062 .name = "TPPS/2",
1063 .alias = "trackpoint",
1064 .detect = trackpoint_detect,
1065 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001066#endif
1067#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -05001068 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -05001069 .type = PSMOUSE_TOUCHKIT_PS2,
1070 .name = "touchkitPS/2",
1071 .alias = "touchkit",
1072 .detect = touchkit_ps2_detect,
1073 },
Andres Salomon55e3d922007-03-10 01:39:54 -05001074#endif
Andres Salomondf08ef22008-09-16 12:30:34 -04001075#ifdef CONFIG_MOUSE_PS2_OLPC
1076 {
1077 .type = PSMOUSE_HGPK,
1078 .name = "OLPC HGPK",
1079 .alias = "hgpk",
1080 .detect = hgpk_detect,
1081 },
1082#endif
Arjan Opmeer2a0bd752008-10-16 22:10:19 -04001083#ifdef CONFIG_MOUSE_PS2_ELANTECH
1084 {
1085 .type = PSMOUSE_ELANTECH,
1086 .name = "ETPS/2",
1087 .alias = "elantech",
1088 .detect = elantech_detect,
1089 .init = elantech_init,
1090 },
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -07001091#endif
1092#ifdef CONFIG_MOUSE_PS2_SENTELIC
1093 {
1094 .type = PSMOUSE_FSP,
1095 .name = "FSPPS/2",
1096 .alias = "fsp",
1097 .detect = fsp_detect,
1098 .init = fsp_init,
1099 },
1100#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -05001101 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -04001102 .type = PSMOUSE_CORTRON,
1103 .name = "CortronPS/2",
1104 .alias = "cortps",
1105 .detect = cortron_detect,
1106 },
Mathias Gottschlag05be1d02014-12-29 09:26:35 -08001107#ifdef CONFIG_MOUSE_PS2_FOCALTECH
1108 {
1109 .type = PSMOUSE_FOCALTECH,
1110 .name = "FocalTechPS/2",
1111 .alias = "focaltech",
1112 .detect = focaltech_detect,
1113 .init = focaltech_init,
1114 },
1115#endif
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -04001116 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001117 .type = PSMOUSE_AUTO,
1118 .name = "auto",
1119 .alias = "any",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001120 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001121 },
1122};
1123
Helge Dellere38de672006-09-10 21:54:39 -04001124static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001125{
1126 int i;
1127
1128 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
1129 if (psmouse_protocols[i].type == type)
1130 return &psmouse_protocols[i];
1131
1132 WARN_ON(1);
1133 return &psmouse_protocols[0];
1134}
1135
Helge Dellere38de672006-09-10 21:54:39 -04001136static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001137{
Helge Dellere38de672006-09-10 21:54:39 -04001138 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001139 int i;
1140
1141 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
1142 p = &psmouse_protocols[i];
1143
1144 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
1145 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
1146 return &psmouse_protocols[i];
1147 }
1148
1149 return NULL;
1150}
1151
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/*
1154 * psmouse_probe() probes for a PS/2 mouse.
1155 */
1156
1157static int psmouse_probe(struct psmouse *psmouse)
1158{
1159 struct ps2dev *ps2dev = &psmouse->ps2dev;
1160 unsigned char param[2];
1161
1162/*
1163 * First, we check if it's a mouse. It should send 0x00 or 0x03
1164 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -05001165 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
1166 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
1168
1169 param[0] = 0xa5;
1170 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1171 return -1;
1172
Vojtech Pavlik7741e932005-05-28 02:11:42 -05001173 if (param[0] != 0x00 && param[0] != 0x03 &&
1174 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return -1;
1176
1177/*
1178 * Then we reset and disable the mouse so that it doesn't generate events.
1179 */
1180
1181 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001182 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
1183 ps2dev->serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 return 0;
1186}
1187
1188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * psmouse_initialize() initializes the mouse to a sane state.
1190 */
1191
1192static void psmouse_initialize(struct psmouse *psmouse)
1193{
1194/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 * We set the mouse report rate, resolution and scaling.
1196 */
1197
1198 if (psmouse_max_proto != PSMOUSE_PS2) {
1199 psmouse->set_rate(psmouse, psmouse->rate);
1200 psmouse->set_resolution(psmouse, psmouse->resolution);
Mathias Gottschlag4ec212f2015-03-07 13:32:10 -08001201 psmouse->set_scale(psmouse, PSMOUSE_SCALE11);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203}
1204
1205/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 * psmouse_activate() enables the mouse so that we get motion reports from it.
1207 */
1208
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001209int psmouse_activate(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001211 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001212 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1213 psmouse->ps2dev.serio->phys);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001214 return -1;
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/*
1222 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +02001223 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 */
1225
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001226int psmouse_deactivate(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_DISABLE)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001229 psmouse_warn(psmouse, "Failed to deactivate 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_CMD_MODE);
Andres Salomonbd26f3d2012-02-24 00:51:37 -08001235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001238
1239/*
1240 * psmouse_resync() attempts to re-validate current protocol.
1241 */
1242
David Howellsc4028952006-11-22 14:57:56 +00001243static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001244{
David Howellsc4028952006-11-22 14:57:56 +00001245 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -04001246 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001247 struct serio *serio = psmouse->ps2dev.serio;
1248 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001249 bool failed = false, enabled = false;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001250 int i;
1251
Ingo Molnarc14471d2006-02-19 00:22:11 -05001252 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001253
1254 if (psmouse->state != PSMOUSE_RESYNCING)
1255 goto out;
1256
1257 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1258 parent = serio_get_drvdata(serio->parent);
1259 psmouse_deactivate(parent);
1260 }
1261
1262/*
1263 * Some mice don't ACK commands sent while they are in the middle of
1264 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1265 * instead of ps2_command() which would wait for 200ms for an ACK
1266 * that may never come.
1267 * As an additional quirk ALPS touchpads may not only forget to ACK
1268 * disable command but will stop reporting taps, so if we see that
1269 * mouse at least once ACKs disable we will do full reconnect if ACK
1270 * is missing.
1271 */
1272 psmouse->num_resyncs++;
1273
1274 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1275 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001276 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001277 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001278 psmouse->acks_disable_command = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001279
1280/*
1281 * Poll the mouse. If it was reset the packet will be shorter than
1282 * psmouse->pktsize and ps2_command will fail. We do not expect and
1283 * do not handle scenario when mouse "upgrades" its protocol while
1284 * disconnected since it would require additional delay. If we ever
1285 * see a mouse that does it we'll adjust the code.
1286 */
1287 if (!failed) {
1288 if (psmouse->poll(psmouse))
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001289 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001290 else {
1291 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1292 for (i = 0; i < psmouse->pktsize; i++) {
1293 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001294 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001295 if (rc != PSMOUSE_GOOD_DATA)
1296 break;
1297 }
1298 if (rc != PSMOUSE_FULL_PACKET)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001299 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001300 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1301 }
1302 }
1303/*
1304 * Now try to enable mouse. We try to do that even if poll failed and also
1305 * repeat our attempts 5 times, otherwise we may be left out with disabled
1306 * mouse.
1307 */
1308 for (i = 0; i < 5; i++) {
1309 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001310 enabled = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001311 break;
1312 }
1313 msleep(200);
1314 }
1315
1316 if (!enabled) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001317 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1318 psmouse->ps2dev.serio->phys);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001319 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001320 }
1321
1322 if (failed) {
1323 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001324 psmouse_info(psmouse,
1325 "resync failed, issuing reconnect request\n");
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001326 serio_reconnect(serio);
1327 } else
1328 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1329
1330 if (parent)
1331 psmouse_activate(parent);
1332 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001333 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001334}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336/*
1337 * psmouse_cleanup() resets the mouse into power-on state.
1338 */
1339
1340static void psmouse_cleanup(struct serio *serio)
1341{
1342 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001343 struct psmouse *parent = NULL;
1344
1345 mutex_lock(&psmouse_mutex);
1346
1347 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1348 parent = serio_get_drvdata(serio->parent);
1349 psmouse_deactivate(parent);
1350 }
1351
Dmitry Torokhova9f0c382010-02-07 23:10:04 -08001352 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1353
1354 /*
1355 * Disable stream mode so cleanup routine can proceed undisturbed.
1356 */
1357 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001358 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1359 psmouse->ps2dev.serio->phys);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001360
1361 if (psmouse->cleanup)
1362 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Dmitry Torokhov4a299bf2009-12-24 21:40:24 -08001364/*
1365 * Reset the mouse to defaults (bare PS/2 protocol).
1366 */
1367 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001368
1369/*
1370 * Some boxes, such as HP nx7400, get terribly confused if mouse
1371 * is not fully enabled before suspending/shutting down.
1372 */
1373 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1374
1375 if (parent) {
1376 if (parent->pt_deactivate)
1377 parent->pt_deactivate(parent);
1378
1379 psmouse_activate(parent);
1380 }
1381
1382 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
1385/*
1386 * psmouse_disconnect() closes and frees.
1387 */
1388
1389static void psmouse_disconnect(struct serio *serio)
1390{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001391 struct psmouse *psmouse, *parent = NULL;
1392
1393 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001395 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Ingo Molnarc14471d2006-02-19 00:22:11 -05001397 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1400
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001401 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001402 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001403 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001404 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1407 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001408 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
1411 if (psmouse->disconnect)
1412 psmouse->disconnect(psmouse);
1413
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001414 if (parent && parent->pt_deactivate)
1415 parent->pt_deactivate(parent);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 serio_close(serio);
1420 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001421 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001423
1424 if (parent)
1425 psmouse_activate(parent);
1426
Ingo Molnarc14471d2006-02-19 00:22:11 -05001427 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001430static int psmouse_switch_protocol(struct psmouse *psmouse,
1431 const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001432{
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001433 const struct psmouse_protocol *selected_proto;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001434 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001435
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001436 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001437
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001438 if (proto && (proto->detect || proto->init)) {
Dmitry Torokhovee9dfd72011-12-30 15:16:45 -08001439 psmouse_apply_defaults(psmouse);
1440
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001441 if (proto->detect && proto->detect(psmouse, true) < 0)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001442 return -1;
1443
1444 if (proto->init && proto->init(psmouse) < 0)
1445 return -1;
1446
1447 psmouse->type = proto->type;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001448 selected_proto = proto;
1449 } else {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001450 psmouse->type = psmouse_extensions(psmouse,
1451 psmouse_max_proto, true);
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001452 selected_proto = psmouse_protocol_by_type(psmouse->type);
1453 }
1454
1455 psmouse->ignore_parity = selected_proto->ignore_parity;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001456
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001457 /*
1458 * If mouse's packet size is 3 there is no point in polling the
1459 * device in hopes to detect protocol reset - we won't get less
1460 * than 3 bytes response anyhow.
1461 */
1462 if (psmouse->pktsize == 3)
1463 psmouse->resync_time = 0;
1464
1465 /*
1466 * Some smart KVMs fake response to POLL command returning just
1467 * 3 bytes and messing up our resync logic, so if initial poll
1468 * fails we won't try polling the device anymore. Hopefully
1469 * such KVM will maintain initially selected protocol.
1470 */
1471 if (psmouse->resync_time && psmouse->poll(psmouse))
1472 psmouse->resync_time = 0;
1473
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001474 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001475 selected_proto->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001476
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001477 input_dev->name = psmouse->devname;
1478 input_dev->phys = psmouse->phys;
1479 input_dev->id.bustype = BUS_I8042;
1480 input_dev->id.vendor = 0x0002;
1481 input_dev->id.product = psmouse->type;
1482 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001483
1484 return 0;
1485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487/*
1488 * psmouse_connect() is a callback from the serio module when
1489 * an unhandled serio port is found.
1490 */
1491static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1492{
1493 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001494 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001495 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Ingo Molnarc14471d2006-02-19 00:22:11 -05001497 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /*
1500 * If this is a pass-through port deactivate parent so the device
1501 * connected to this port can be successfully identified
1502 */
1503 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1504 parent = serio_get_drvdata(serio->parent);
1505 psmouse_deactivate(parent);
1506 }
1507
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001508 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1509 input_dev = input_allocate_device();
1510 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001511 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001514 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001515 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001516 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1519
1520 serio_set_drvdata(serio, psmouse);
1521
Dmitry Torokhov72155612006-11-05 22:40:19 -05001522 error = serio_open(serio, drv);
1523 if (error)
1524 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001527 error = -ENODEV;
1528 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530
1531 psmouse->rate = psmouse_rate;
1532 psmouse->resolution = psmouse_resolution;
1533 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001534 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001537 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 psmouse_initialize(psmouse);
1541
Dmitry Torokhov72155612006-11-05 22:40:19 -05001542 error = input_register_device(psmouse->dev);
1543 if (error)
1544 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (parent && parent->pt_activate)
1547 parent->pt_activate(parent);
1548
Dmitry Torokhov72155612006-11-05 22:40:19 -05001549 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1550 if (error)
1551 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 psmouse_activate(psmouse);
1554
Dmitry Torokhov72155612006-11-05 22:40:19 -05001555 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001556 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (parent)
1558 psmouse_activate(parent);
1559
Ingo Molnarc14471d2006-02-19 00:22:11 -05001560 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001562
1563 err_pt_deactivate:
1564 if (parent && parent->pt_deactivate)
1565 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001566 input_unregister_device(psmouse->dev);
1567 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001568 err_protocol_disconnect:
1569 if (psmouse->disconnect)
1570 psmouse->disconnect(psmouse);
1571 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1572 err_close_serio:
1573 serio_close(serio);
1574 err_clear_drvdata:
1575 serio_set_drvdata(serio, NULL);
1576 err_free:
1577 input_free_device(input_dev);
1578 kfree(psmouse);
1579
1580 retval = error;
1581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584
1585static int psmouse_reconnect(struct serio *serio)
1586{
1587 struct psmouse *psmouse = serio_get_drvdata(serio);
1588 struct psmouse *parent = NULL;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001589 unsigned char type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 int rc = -1;
1591
Ingo Molnarc14471d2006-02-19 00:22:11 -05001592 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1595 parent = serio_get_drvdata(serio->parent);
1596 psmouse_deactivate(parent);
1597 }
1598
1599 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1600
1601 if (psmouse->reconnect) {
1602 if (psmouse->reconnect(psmouse))
1603 goto out;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001604 } else {
1605 psmouse_reset(psmouse);
1606
1607 if (psmouse_probe(psmouse) < 0)
1608 goto out;
1609
1610 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1611 if (psmouse->type != type)
1612 goto out;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001615 /*
1616 * OK, the device type (and capabilities) match the old one,
1617 * we can continue using it, complete initialization
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
1619 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1620
1621 psmouse_initialize(psmouse);
1622
1623 if (parent && parent->pt_activate)
1624 parent->pt_activate(parent);
1625
1626 psmouse_activate(psmouse);
1627 rc = 0;
1628
1629out:
1630 /* If this is a pass-through port the parent waits to be activated */
1631 if (parent)
1632 psmouse_activate(parent);
1633
Ingo Molnarc14471d2006-02-19 00:22:11 -05001634 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return rc;
1636}
1637
1638static struct serio_device_id psmouse_serio_ids[] = {
1639 {
1640 .type = SERIO_8042,
1641 .proto = SERIO_ANY,
1642 .id = SERIO_ANY,
1643 .extra = SERIO_ANY,
1644 },
1645 {
1646 .type = SERIO_PS_PSTHRU,
1647 .proto = SERIO_ANY,
1648 .id = SERIO_ANY,
1649 .extra = SERIO_ANY,
1650 },
1651 { 0 }
1652};
1653
1654MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1655
1656static struct serio_driver psmouse_drv = {
1657 .driver = {
1658 .name = "psmouse",
1659 },
1660 .description = DRIVER_DESC,
1661 .id_table = psmouse_serio_ids,
1662 .interrupt = psmouse_interrupt,
1663 .connect = psmouse_connect,
1664 .reconnect = psmouse_reconnect,
1665 .disconnect = psmouse_disconnect,
1666 .cleanup = psmouse_cleanup,
1667};
1668
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001669ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1670 char *buf)
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001676 psmouse = serio_get_drvdata(serio);
1677
Eric W. Biederman59b01512010-01-05 17:56:02 -08001678 return attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001681ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1682 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001685 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1686 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int retval;
1688
Ingo Molnarc14471d2006-02-19 00:22:11 -05001689 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001690 if (retval)
Eric W. Biederman59b01512010-01-05 17:56:02 -08001691 goto out;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001692
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001693 psmouse = serio_get_drvdata(serio);
1694
Andres Salomon68d48222008-09-16 12:30:34 -04001695 if (attr->protect) {
1696 if (psmouse->state == PSMOUSE_IGNORE) {
1697 retval = -ENODEV;
1698 goto out_unlock;
1699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Andres Salomon68d48222008-09-16 12:30:34 -04001701 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1702 parent = serio_get_drvdata(serio->parent);
1703 psmouse_deactivate(parent);
1704 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001705
Andres Salomon68d48222008-09-16 12:30:34 -04001706 psmouse_deactivate(psmouse);
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001709 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Andres Salomon68d48222008-09-16 12:30:34 -04001711 if (attr->protect) {
1712 if (retval != -ENODEV)
1713 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001714
Andres Salomon68d48222008-09-16 12:30:34 -04001715 if (parent)
1716 psmouse_activate(parent);
1717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Ingo Molnarc14471d2006-02-19 00:22:11 -05001719 out_unlock:
1720 mutex_unlock(&psmouse_mutex);
Eric W. Biederman59b01512010-01-05 17:56:02 -08001721 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return retval;
1723}
1724
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001725static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1726{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001727 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001728
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001729 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001730}
1731
1732static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1733{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001734 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
JJ Ding76496e72011-11-09 10:20:14 -08001735 unsigned int value;
1736 int err;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001737
JJ Ding76496e72011-11-09 10:20:14 -08001738 err = kstrtouint(buf, 10, &value);
1739 if (err)
1740 return err;
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001741
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001742 *field = value;
1743
1744 return count;
1745}
1746
1747static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001748{
1749 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1750}
1751
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001752static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001753{
1754 struct serio *serio = psmouse->ps2dev.serio;
1755 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001756 struct input_dev *old_dev, *new_dev;
1757 const struct psmouse_protocol *proto, *old_proto;
1758 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001759 int retry = 0;
1760
Dmitry Torokhov72155612006-11-05 22:40:19 -05001761 proto = psmouse_protocol_by_name(buf, count);
1762 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001763 return -EINVAL;
1764
1765 if (psmouse->type == proto->type)
1766 return count;
1767
Dmitry Torokhov72155612006-11-05 22:40:19 -05001768 new_dev = input_allocate_device();
1769 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001770 return -ENOMEM;
1771
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -07001772 while (!list_empty(&serio->children)) {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001773 if (++retry > 3) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001774 psmouse_warn(psmouse,
1775 "failed to destroy children ports, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001776 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001777 return -EIO;
1778 }
1779
Ingo Molnarc14471d2006-02-19 00:22:11 -05001780 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001781 serio_unregister_child_port(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001782 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001783
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001784 if (serio->drv != &psmouse_drv) {
1785 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001786 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001787 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001788
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001789 if (psmouse->type == proto->type) {
1790 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001791 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001792 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001793 }
1794
1795 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1796 parent = serio_get_drvdata(serio->parent);
1797 if (parent->pt_deactivate)
1798 parent->pt_deactivate(parent);
1799 }
1800
Dmitry Torokhov72155612006-11-05 22:40:19 -05001801 old_dev = psmouse->dev;
1802 old_proto = psmouse_protocol_by_type(psmouse->type);
1803
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001804 if (psmouse->disconnect)
1805 psmouse->disconnect(psmouse);
1806
1807 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001808
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001809 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001810 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1811
1812 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1813 psmouse_reset(psmouse);
1814 /* default to PSMOUSE_PS2 */
1815 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1816 }
1817
1818 psmouse_initialize(psmouse);
1819 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1820
Dmitry Torokhov72155612006-11-05 22:40:19 -05001821 error = input_register_device(psmouse->dev);
1822 if (error) {
1823 if (psmouse->disconnect)
1824 psmouse->disconnect(psmouse);
1825
1826 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1827 input_free_device(new_dev);
1828 psmouse->dev = old_dev;
1829 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1830 psmouse_switch_protocol(psmouse, old_proto);
1831 psmouse_initialize(psmouse);
1832 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1833
1834 return error;
1835 }
1836
1837 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001838
1839 if (parent && parent->pt_activate)
1840 parent->pt_activate(parent);
1841
1842 return count;
1843}
1844
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001845static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
JJ Ding76496e72011-11-09 10:20:14 -08001847 unsigned int value;
1848 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
JJ Ding76496e72011-11-09 10:20:14 -08001850 err = kstrtouint(buf, 10, &value);
1851 if (err)
1852 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 psmouse->set_rate(psmouse, value);
1855 return count;
1856}
1857
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001858static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
JJ Ding76496e72011-11-09 10:20:14 -08001860 unsigned int value;
1861 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
JJ Ding76496e72011-11-09 10:20:14 -08001863 err = kstrtouint(buf, 10, &value);
1864 if (err)
1865 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 psmouse->set_resolution(psmouse, value);
1868 return count;
1869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001872static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Helge Dellere38de672006-09-10 21:54:39 -04001874 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 if (!val)
1877 return -EINVAL;
1878
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001879 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001881 if (!proto || !proto->maxproto)
1882 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001884 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Stephen Evanchik541e3162005-08-08 01:26:18 -05001886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001889static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001891 int type = *((unsigned int *)kp->arg);
1892
Takashi Iwai3d4c3aa2009-11-12 23:30:52 -08001893 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
1896static int __init psmouse_init(void)
1897{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001898 int err;
1899
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001900 lifebook_module_init();
1901 synaptics_module_init();
Daniel Drakeca94ec42010-11-11 22:19:57 -08001902 hgpk_module_init();
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001903
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001904 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1905 if (!kpsmoused_wq) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001906 pr_err("failed to create kpsmoused workqueue\n");
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001907 return -ENOMEM;
1908 }
1909
Akinobu Mita153a9df02006-11-23 23:35:10 -05001910 err = serio_register_driver(&psmouse_drv);
1911 if (err)
1912 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001913
Akinobu Mita153a9df02006-11-23 23:35:10 -05001914 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
1917static void __exit psmouse_exit(void)
1918{
1919 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001920 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
1923module_init(psmouse_init);
1924module_exit(psmouse_exit);