blob: a6973e575aa9308660f2bd543a16fad945d053df [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define DRIVER_DESC "PS/2 mouse driver"
39
40MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
41MODULE_DESCRIPTION(DRIVER_DESC);
42MODULE_LICENSE("GPL");
43
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050044static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Rusty Russell9bbb9e52010-08-11 23:04:12 -060045static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
46static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
47static struct kernel_param_ops param_ops_proto_abbrev = {
48 .set = psmouse_set_maxproto,
49 .get = psmouse_get_maxproto,
50};
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050053MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55static unsigned int psmouse_resolution = 200;
56module_param_named(resolution, psmouse_resolution, uint, 0644);
57MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
58
59static unsigned int psmouse_rate = 100;
60module_param_named(rate, psmouse_rate, uint, 0644);
61MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
62
63static unsigned int psmouse_smartscroll = 1;
64module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
65MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
66
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050067static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068module_param_named(resetafter, psmouse_resetafter, uint, 0644);
69MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
70
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050071static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050072module_param_named(resync_time, psmouse_resync_time, uint, 0644);
73MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
74
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050075PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
76 NULL,
77 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
78PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
79 (void *) offsetof(struct psmouse, rate),
80 psmouse_show_int_attr, psmouse_attr_set_rate);
81PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
82 (void *) offsetof(struct psmouse, resolution),
83 psmouse_show_int_attr, psmouse_attr_set_resolution);
84PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
85 (void *) offsetof(struct psmouse, resetafter),
86 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050087PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
88 (void *) offsetof(struct psmouse, resync_time),
89 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050090
91static struct attribute *psmouse_attributes[] = {
92 &psmouse_attr_protocol.dattr.attr,
93 &psmouse_attr_rate.dattr.attr,
94 &psmouse_attr_resolution.dattr.attr,
95 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050096 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050097 NULL
98};
99
100static struct attribute_group psmouse_attribute_group = {
101 .attrs = psmouse_attributes,
102};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500104/*
Ingo Molnarc14471d2006-02-19 00:22:11 -0500105 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500106 * (connecting, disconnecting, changing rate or resolution via
107 * sysfs). We could use a per-device semaphore but since there
108 * rarely more than one PS/2 mouse connected and since semaphore
109 * is taken in "slow" paths it is not worth it.
110 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500111static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500112
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500113static struct workqueue_struct *kpsmoused_wq;
114
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500115struct psmouse_protocol {
116 enum psmouse_type type;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700117 bool maxproto;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700118 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
Helge Dellere38de672006-09-10 21:54:39 -0400119 const char *name;
120 const char *alias;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700121 int (*detect)(struct psmouse *, bool);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500122 int (*init)(struct psmouse *);
123};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/*
126 * psmouse_process_byte() analyzes the PS/2 data stream and reports
127 * relevant events to the input module once full packet has arrived.
128 */
129
David Howells7d12e782006-10-05 14:55:46 +0100130static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500132 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 unsigned char *packet = psmouse->packet;
134
135 if (psmouse->pktcnt < psmouse->pktsize)
136 return PSMOUSE_GOOD_DATA;
137
138/*
139 * Full packet accumulated, process it
140 */
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143 * Scroll wheel on IntelliMice, scroll buttons on NetMice
144 */
145
146 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
147 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
148
149/*
150 * Scroll wheel and buttons on IntelliMouse Explorer
151 */
152
153 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400154 switch (packet[3] & 0xC0) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700155 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
156 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
157 break;
158 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
159 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
160 break;
161 case 0x00:
162 case 0xC0:
163 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
164 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
165 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
166 break;
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169
170/*
171 * Extra buttons on Genius NewNet 3D
172 */
173
174 if (psmouse->type == PSMOUSE_GENPS) {
175 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
176 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
177 }
178
179/*
180 * Extra button on ThinkingMouse
181 */
182 if (psmouse->type == PSMOUSE_THINKPS) {
183 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
184 /* Without this bit of weirdness moving up gives wildly high Y changes. */
185 packet[1] |= (packet[0] & 0x40) << 1;
186 }
187
188/*
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400189 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
190 * byte.
191 */
192 if (psmouse->type == PSMOUSE_CORTRON) {
193 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
194 packet[0] |= 0x08;
195 }
196
197/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * Generic PS/2 Mouse
199 */
200
201 input_report_key(dev, BTN_LEFT, packet[0] & 1);
202 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
203 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
204
205 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
206 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
207
208 input_sync(dev);
209
210 return PSMOUSE_FULL_PACKET;
211}
212
Andres Salomon8bf020e2008-09-16 12:30:33 -0400213void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
214 unsigned long delay)
215{
216 queue_delayed_work(kpsmoused_wq, work, delay);
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500220 * __psmouse_set_state() sets new psmouse state and resets all flags.
221 */
222
223static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
224{
225 psmouse->state = new_state;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700226 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500227 psmouse->ps2dev.flags = 0;
228 psmouse->last = jiffies;
229}
230
231
232/*
233 * psmouse_set_state() sets new psmouse state and resets all flags and
234 * counters while holding serio lock so fighting with interrupt handler
235 * is not a concern.
236 */
237
Andres Salomona48cf5f2008-09-16 12:30:33 -0400238void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500239{
240 serio_pause_rx(psmouse->ps2dev.serio);
241 __psmouse_set_state(psmouse, new_state);
242 serio_continue_rx(psmouse->ps2dev.serio);
243}
244
245/*
246 * psmouse_handle_byte() processes one byte of the input data stream
247 * by calling corresponding protocol handler.
248 */
249
David Howells7d12e782006-10-05 14:55:46 +0100250static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500251{
David Howells7d12e782006-10-05 14:55:46 +0100252 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500253
254 switch (rc) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700255 case PSMOUSE_BAD_DATA:
256 if (psmouse->state == PSMOUSE_ACTIVATED) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700257 psmouse_warn(psmouse,
258 "%s at %s lost sync at byte %d\n",
259 psmouse->name, psmouse->phys,
260 psmouse->pktcnt);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700261 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
262 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700263 psmouse_notice(psmouse,
264 "issuing reconnect request\n");
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700265 serio_reconnect(psmouse->ps2dev.serio);
266 return -1;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500267 }
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700268 }
269 psmouse->pktcnt = 0;
270 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500271
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700272 case PSMOUSE_FULL_PACKET:
273 psmouse->pktcnt = 0;
274 if (psmouse->out_of_sync_cnt) {
275 psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700276 psmouse_notice(psmouse,
277 "%s at %s - driver resynced.\n",
278 psmouse->name, psmouse->phys);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700279 }
280 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500281
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700282 case PSMOUSE_GOOD_DATA:
283 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500284 }
285 return 0;
286}
287
288/*
289 * psmouse_interrupt() handles incoming characters, either passing them
290 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292
293static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100294 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (psmouse->state == PSMOUSE_IGNORE)
299 goto out;
300
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700301 if (unlikely((flags & SERIO_TIMEOUT) ||
302 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (psmouse->state == PSMOUSE_ACTIVATED)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700305 psmouse_warn(psmouse,
306 "bad data from KBC -%s%s\n",
307 flags & SERIO_TIMEOUT ? " timeout" : "",
308 flags & SERIO_PARITY ? " bad parity" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 ps2_cmd_aborted(&psmouse->ps2dev);
310 goto out;
311 }
312
313 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
314 if (ps2_handle_ack(&psmouse->ps2dev, data))
315 goto out;
316
317 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
318 if (ps2_handle_response(&psmouse->ps2dev, data))
319 goto out;
320
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500321 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 goto out;
323
324 if (psmouse->state == PSMOUSE_ACTIVATED &&
325 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700326 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
327 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500328 psmouse->badbyte = psmouse->packet[0];
329 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400330 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500331 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500335/*
336 * Check if this is a new device announcement (0xAA 0x00)
337 */
338 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400339 if (psmouse->pktcnt == 1) {
340 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Zephaniah E. Hull535650f2009-05-14 22:02:33 -0700344 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
345 (psmouse->type == PSMOUSE_HGPK &&
346 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500347 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
348 serio_reconnect(serio);
349 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500351/*
352 * Not a new device, try processing first byte normally
353 */
354 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100355 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500356 goto out;
357
358 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500361/*
362 * See if we need to force resync because mouse was idle for too long
363 */
364 if (psmouse->state == PSMOUSE_ACTIVATED &&
365 psmouse->pktcnt == 1 && psmouse->resync_time &&
366 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
367 psmouse->badbyte = psmouse->packet[0];
368 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400369 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500372
373 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100374 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500375
376 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return IRQ_HANDLED;
378}
379
380
381/*
382 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
383 * using sliced syntax, understood by advanced devices, such as Logitech
384 * or Synaptics touchpads. The command is encoded as:
385 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
386 * is the command.
387 */
388int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
389{
390 int i;
391
392 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
393 return -1;
394
395 for (i = 6; i >= 0; i -= 2) {
396 unsigned char d = (command >> i) & 3;
397 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
398 return -1;
399 }
400
401 return 0;
402}
403
404
405/*
406 * psmouse_reset() resets the mouse into power-on state.
407 */
408int psmouse_reset(struct psmouse *psmouse)
409{
410 unsigned char param[2];
411
412 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
413 return -1;
414
415 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
416 return -1;
417
418 return 0;
419}
420
421
422/*
423 * Genius NetMouse magic init.
424 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700425static int genius_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct ps2dev *ps2dev = &psmouse->ps2dev;
428 unsigned char param[4];
429
430 param[0] = 3;
431 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
432 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
433 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
434 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
435 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
436
437 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
438 return -1;
439
440 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800441 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700442 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
443 __set_bit(BTN_SIDE, psmouse->dev->keybit);
444 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500447 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 psmouse->pktsize = 4;
449 }
450
451 return 0;
452}
453
454/*
455 * IntelliMouse magic init.
456 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700457static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct ps2dev *ps2dev = &psmouse->ps2dev;
460 unsigned char param[2];
461
462 param[0] = 200;
463 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
464 param[0] = 100;
465 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
466 param[0] = 80;
467 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
468 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
469
470 if (param[0] != 3)
471 return -1;
472
473 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700474 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
475 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800477 if (!psmouse->vendor)
478 psmouse->vendor = "Generic";
479 if (!psmouse->name)
480 psmouse->name = "Wheel Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 psmouse->pktsize = 4;
482 }
483
484 return 0;
485}
486
487/*
488 * Try IntelliMouse/Explorer magic init.
489 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700490static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct ps2dev *ps2dev = &psmouse->ps2dev;
493 unsigned char param[2];
494
495 intellimouse_detect(psmouse, 0);
496
497 param[0] = 200;
498 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
499 param[0] = 200;
500 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
501 param[0] = 80;
502 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
503 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
504
505 if (param[0] != 4)
506 return -1;
507
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400508/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
509 param[0] = 200;
510 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
511 param[0] = 80;
512 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
513 param[0] = 40;
514 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700517 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
518 __set_bit(REL_WHEEL, psmouse->dev->relbit);
519 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
520 __set_bit(BTN_SIDE, psmouse->dev->keybit);
521 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800523 if (!psmouse->vendor)
524 psmouse->vendor = "Generic";
525 if (!psmouse->name)
526 psmouse->name = "Explorer Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 psmouse->pktsize = 4;
528 }
529
530 return 0;
531}
532
533/*
534 * Kensington ThinkingMouse / ExpertMouse magic init.
535 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700536static int thinking_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct ps2dev *ps2dev = &psmouse->ps2dev;
539 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400540 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int i;
542
543 param[0] = 10;
544 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
545 param[0] = 0;
546 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400547 for (i = 0; i < ARRAY_SIZE(seq); i++) {
548 param[0] = seq[i];
549 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
552
553 if (param[0] != 2)
554 return -1;
555
556 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800557 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700558 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 psmouse->vendor = "Kensington";
561 psmouse->name = "ThinkingMouse";
562 }
563
564 return 0;
565}
566
567/*
568 * Bare PS/2 protocol "detection". Always succeeds.
569 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700570static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500572 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800573 if (!psmouse->vendor)
574 psmouse->vendor = "Generic";
575 if (!psmouse->name)
576 psmouse->name = "Mouse";
577
578/*
579 * We have no way of figuring true number of buttons so let's
580 * assume that the device has 3.
581 */
582 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 return 0;
586}
587
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400588/*
589 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
590 * must be forced by sysfs protocol writing.
591 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700592static int cortron_detect(struct psmouse *psmouse, bool set_properties)
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400593{
594 if (set_properties) {
595 psmouse->vendor = "Cortron";
596 psmouse->name = "PS/2 Trackball";
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800597
598 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700599 __set_bit(BTN_SIDE, psmouse->dev->keybit);
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400600 }
601
602 return 0;
603}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*
606 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
607 * the mouse may have.
608 */
609
610static int psmouse_extensions(struct psmouse *psmouse,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700611 unsigned int max_proto, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Jiri Kosina06989892009-11-16 22:12:13 -0800613 bool synaptics_hardware = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500615/*
616 * We always check for lifebook because it does not disturb mouse
617 * (it only checks DMI information).
618 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500619 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500620 if (max_proto > PSMOUSE_IMEX) {
621 if (!set_properties || lifebook_init(psmouse) == 0)
622 return PSMOUSE_LIFEBOOK;
623 }
624 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/*
627 * Try Kensington ThinkingMouse (we try first, because synaptics probe
628 * upsets the thinkingmouse).
629 */
630
631 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
632 return PSMOUSE_THINKPS;
633
634/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500635 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
636 * support is disabled in config - we need to know if it is synaptics so we
637 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
639 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700640 synaptics_hardware = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (max_proto > PSMOUSE_IMEX) {
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800643/*
644 * Try activating protocol, but check if support is enabled first, since
645 * we try detecting Synaptics even when protocol is disabled.
646 */
647 if (synaptics_supported() &&
648 (!set_properties || synaptics_init(psmouse) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return PSMOUSE_SYNAPTICS;
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652/*
653 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
654 * Unfortunately Logitech/Genius probes confuse some firmware versions so
655 * we'll have to skip them.
656 */
657 max_proto = PSMOUSE_IMEX;
658 }
659/*
660 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
661 */
662 synaptics_reset(psmouse);
663 }
664
665/*
666 * Try ALPS TouchPad
667 */
668 if (max_proto > PSMOUSE_IMEX) {
669 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
670 if (alps_detect(psmouse, set_properties) == 0) {
671 if (!set_properties || alps_init(psmouse) == 0)
672 return PSMOUSE_ALPS;
673/*
674 * Init failed, try basic relative protocols
675 */
676 max_proto = PSMOUSE_IMEX;
677 }
678 }
679
Andres Salomondf08ef22008-09-16 12:30:34 -0400680/*
681 * Try OLPC HGPK touchpad.
682 */
683 if (max_proto > PSMOUSE_IMEX &&
684 hgpk_detect(psmouse, set_properties) == 0) {
685 if (!set_properties || hgpk_init(psmouse) == 0)
686 return PSMOUSE_HGPK;
687/*
688 * Init failed, try basic relative protocols
689 */
690 max_proto = PSMOUSE_IMEX;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400693/*
694 * Try Elantech touchpad.
695 */
696 if (max_proto > PSMOUSE_IMEX &&
697 elantech_detect(psmouse, set_properties) == 0) {
698 if (!set_properties || elantech_init(psmouse) == 0)
699 return PSMOUSE_ELANTECH;
700/*
701 * Init failed, try basic relative protocols
702 */
703 max_proto = PSMOUSE_IMEX;
704 }
705
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700706
Andres Salomondf08ef22008-09-16 12:30:34 -0400707 if (max_proto > PSMOUSE_IMEX) {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500708 if (genius_detect(psmouse, set_properties) == 0)
709 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500711 if (ps2pp_init(psmouse, set_properties) == 0)
712 return PSMOUSE_PS2PP;
713
714 if (trackpoint_detect(psmouse, set_properties) == 0)
715 return PSMOUSE_TRACKPOINT;
716
717 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
718 return PSMOUSE_TOUCHKIT_PS2;
719 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -0800722 * Try Finger Sensing Pad. We do it here because its probe upsets
723 * Trackpoint devices (causing TP_READ_ID command to time out).
724 */
725 if (max_proto > PSMOUSE_IMEX) {
726 if (fsp_detect(psmouse, set_properties) == 0) {
727 if (!set_properties || fsp_init(psmouse) == 0)
728 return PSMOUSE_FSP;
729/*
730 * Init failed, try basic relative protocols
731 */
732 max_proto = PSMOUSE_IMEX;
733 }
734 }
735
736/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400738 * protocol probes. Note that we follow up with full reset because
739 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Alon Ziv554fc192007-08-30 00:22:48 -0400741 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500742 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
745 return PSMOUSE_IMEX;
746
747 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
748 return PSMOUSE_IMPS;
749
750/*
751 * Okay, all failed, we have a standard mouse here. The number of the buttons
752 * is still a question, though. We assume 3.
753 */
754 ps2bare_detect(psmouse, set_properties);
755
756 if (synaptics_hardware) {
757/*
758 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
759 * We need to reset the touchpad because if there is a track point on the
760 * pass through port it could get disabled while probing for protocol
761 * extensions.
762 */
763 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
766 return PSMOUSE_PS2;
767}
768
Helge Dellere38de672006-09-10 21:54:39 -0400769static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500770 {
771 .type = PSMOUSE_PS2,
772 .name = "PS/2",
773 .alias = "bare",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700774 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700775 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500776 .detect = ps2bare_detect,
777 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500778#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500779 {
780 .type = PSMOUSE_PS2PP,
781 .name = "PS2++",
782 .alias = "logitech",
783 .detect = ps2pp_init,
784 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500785#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500786 {
787 .type = PSMOUSE_THINKPS,
788 .name = "ThinkPS/2",
789 .alias = "thinkps",
790 .detect = thinking_detect,
791 },
792 {
793 .type = PSMOUSE_GENPS,
794 .name = "GenPS/2",
795 .alias = "genius",
796 .detect = genius_detect,
797 },
798 {
799 .type = PSMOUSE_IMPS,
800 .name = "ImPS/2",
801 .alias = "imps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700802 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700803 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500804 .detect = intellimouse_detect,
805 },
806 {
807 .type = PSMOUSE_IMEX,
808 .name = "ImExPS/2",
809 .alias = "exps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700810 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700811 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500812 .detect = im_explorer_detect,
813 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500814#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500815 {
816 .type = PSMOUSE_SYNAPTICS,
817 .name = "SynPS/2",
818 .alias = "synaptics",
819 .detect = synaptics_detect,
820 .init = synaptics_init,
821 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500822#endif
823#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500824 {
825 .type = PSMOUSE_ALPS,
826 .name = "AlpsPS/2",
827 .alias = "alps",
828 .detect = alps_detect,
829 .init = alps_init,
830 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500831#endif
832#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500833 {
834 .type = PSMOUSE_LIFEBOOK,
835 .name = "LBPS/2",
836 .alias = "lifebook",
837 .init = lifebook_init,
838 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500839#endif
840#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500841 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500842 .type = PSMOUSE_TRACKPOINT,
843 .name = "TPPS/2",
844 .alias = "trackpoint",
845 .detect = trackpoint_detect,
846 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500847#endif
848#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500849 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500850 .type = PSMOUSE_TOUCHKIT_PS2,
851 .name = "touchkitPS/2",
852 .alias = "touchkit",
853 .detect = touchkit_ps2_detect,
854 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500855#endif
Andres Salomondf08ef22008-09-16 12:30:34 -0400856#ifdef CONFIG_MOUSE_PS2_OLPC
857 {
858 .type = PSMOUSE_HGPK,
859 .name = "OLPC HGPK",
860 .alias = "hgpk",
861 .detect = hgpk_detect,
862 },
863#endif
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400864#ifdef CONFIG_MOUSE_PS2_ELANTECH
865 {
866 .type = PSMOUSE_ELANTECH,
867 .name = "ETPS/2",
868 .alias = "elantech",
869 .detect = elantech_detect,
870 .init = elantech_init,
871 },
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700872#endif
873#ifdef CONFIG_MOUSE_PS2_SENTELIC
874 {
875 .type = PSMOUSE_FSP,
876 .name = "FSPPS/2",
877 .alias = "fsp",
878 .detect = fsp_detect,
879 .init = fsp_init,
880 },
881#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500882 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400883 .type = PSMOUSE_CORTRON,
884 .name = "CortronPS/2",
885 .alias = "cortps",
886 .detect = cortron_detect,
887 },
888 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500889 .type = PSMOUSE_AUTO,
890 .name = "auto",
891 .alias = "any",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700892 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500893 },
894};
895
Helge Dellere38de672006-09-10 21:54:39 -0400896static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500897{
898 int i;
899
900 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
901 if (psmouse_protocols[i].type == type)
902 return &psmouse_protocols[i];
903
904 WARN_ON(1);
905 return &psmouse_protocols[0];
906}
907
Helge Dellere38de672006-09-10 21:54:39 -0400908static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500909{
Helge Dellere38de672006-09-10 21:54:39 -0400910 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500911 int i;
912
913 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
914 p = &psmouse_protocols[i];
915
916 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
917 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
918 return &psmouse_protocols[i];
919 }
920
921 return NULL;
922}
923
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * psmouse_probe() probes for a PS/2 mouse.
927 */
928
929static int psmouse_probe(struct psmouse *psmouse)
930{
931 struct ps2dev *ps2dev = &psmouse->ps2dev;
932 unsigned char param[2];
933
934/*
935 * First, we check if it's a mouse. It should send 0x00 or 0x03
936 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500937 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
938 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
940
941 param[0] = 0xa5;
942 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
943 return -1;
944
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500945 if (param[0] != 0x00 && param[0] != 0x03 &&
946 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return -1;
948
949/*
950 * Then we reset and disable the mouse so that it doesn't generate events.
951 */
952
953 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700954 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
955 ps2dev->serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 return 0;
958}
959
960/*
961 * Here we set the mouse resolution.
962 */
963
964void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
965{
Helge Dellere38de672006-09-10 21:54:39 -0400966 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
967 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (resolution == 0 || resolution > 200)
970 resolution = 200;
971
Helge Dellere38de672006-09-10 21:54:39 -0400972 p = params[resolution / 50];
973 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
974 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977/*
978 * Here we set the mouse report rate.
979 */
980
981static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
982{
Helge Dellere38de672006-09-10 21:54:39 -0400983 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
984 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int i = 0;
986
987 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400988 r = rates[i];
989 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
990 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
993/*
994 * psmouse_initialize() initializes the mouse to a sane state.
995 */
996
997static void psmouse_initialize(struct psmouse *psmouse)
998{
999/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 * We set the mouse report rate, resolution and scaling.
1001 */
1002
1003 if (psmouse_max_proto != PSMOUSE_PS2) {
1004 psmouse->set_rate(psmouse, psmouse->rate);
1005 psmouse->set_resolution(psmouse, psmouse->resolution);
1006 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
1007 }
1008}
1009
1010/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * psmouse_activate() enables the mouse so that we get motion reports from it.
1012 */
1013
1014static void psmouse_activate(struct psmouse *psmouse)
1015{
1016 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001017 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1018 psmouse->ps2dev.serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1021}
1022
1023
1024/*
1025 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +02001026 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
1028
1029static void psmouse_deactivate(struct psmouse *psmouse)
1030{
1031 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001032 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1033 psmouse->ps2dev.serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1036}
1037
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001038/*
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001039 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001040 */
1041
1042static int psmouse_poll(struct psmouse *psmouse)
1043{
1044 return ps2_command(&psmouse->ps2dev, psmouse->packet,
1045 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
1046}
1047
1048
1049/*
1050 * psmouse_resync() attempts to re-validate current protocol.
1051 */
1052
David Howellsc4028952006-11-22 14:57:56 +00001053static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001054{
David Howellsc4028952006-11-22 14:57:56 +00001055 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -04001056 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001057 struct serio *serio = psmouse->ps2dev.serio;
1058 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001059 bool failed = false, enabled = false;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001060 int i;
1061
Ingo Molnarc14471d2006-02-19 00:22:11 -05001062 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001063
1064 if (psmouse->state != PSMOUSE_RESYNCING)
1065 goto out;
1066
1067 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1068 parent = serio_get_drvdata(serio->parent);
1069 psmouse_deactivate(parent);
1070 }
1071
1072/*
1073 * Some mice don't ACK commands sent while they are in the middle of
1074 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1075 * instead of ps2_command() which would wait for 200ms for an ACK
1076 * that may never come.
1077 * As an additional quirk ALPS touchpads may not only forget to ACK
1078 * disable command but will stop reporting taps, so if we see that
1079 * mouse at least once ACKs disable we will do full reconnect if ACK
1080 * is missing.
1081 */
1082 psmouse->num_resyncs++;
1083
1084 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1085 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001086 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001087 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001088 psmouse->acks_disable_command = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001089
1090/*
1091 * Poll the mouse. If it was reset the packet will be shorter than
1092 * psmouse->pktsize and ps2_command will fail. We do not expect and
1093 * do not handle scenario when mouse "upgrades" its protocol while
1094 * disconnected since it would require additional delay. If we ever
1095 * see a mouse that does it we'll adjust the code.
1096 */
1097 if (!failed) {
1098 if (psmouse->poll(psmouse))
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001099 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001100 else {
1101 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1102 for (i = 0; i < psmouse->pktsize; i++) {
1103 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001104 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001105 if (rc != PSMOUSE_GOOD_DATA)
1106 break;
1107 }
1108 if (rc != PSMOUSE_FULL_PACKET)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001109 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001110 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1111 }
1112 }
1113/*
1114 * Now try to enable mouse. We try to do that even if poll failed and also
1115 * repeat our attempts 5 times, otherwise we may be left out with disabled
1116 * mouse.
1117 */
1118 for (i = 0; i < 5; i++) {
1119 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001120 enabled = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001121 break;
1122 }
1123 msleep(200);
1124 }
1125
1126 if (!enabled) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001127 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1128 psmouse->ps2dev.serio->phys);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001129 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001130 }
1131
1132 if (failed) {
1133 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001134 psmouse_info(psmouse,
1135 "resync failed, issuing reconnect request\n");
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001136 serio_reconnect(serio);
1137 } else
1138 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1139
1140 if (parent)
1141 psmouse_activate(parent);
1142 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001143 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146/*
1147 * psmouse_cleanup() resets the mouse into power-on state.
1148 */
1149
1150static void psmouse_cleanup(struct serio *serio)
1151{
1152 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001153 struct psmouse *parent = NULL;
1154
1155 mutex_lock(&psmouse_mutex);
1156
1157 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1158 parent = serio_get_drvdata(serio->parent);
1159 psmouse_deactivate(parent);
1160 }
1161
Dmitry Torokhova9f0c382010-02-07 23:10:04 -08001162 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1163
1164 /*
1165 * Disable stream mode so cleanup routine can proceed undisturbed.
1166 */
1167 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001168 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1169 psmouse->ps2dev.serio->phys);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001170
1171 if (psmouse->cleanup)
1172 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Dmitry Torokhov4a299bf2009-12-24 21:40:24 -08001174/*
1175 * Reset the mouse to defaults (bare PS/2 protocol).
1176 */
1177 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001178
1179/*
1180 * Some boxes, such as HP nx7400, get terribly confused if mouse
1181 * is not fully enabled before suspending/shutting down.
1182 */
1183 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1184
1185 if (parent) {
1186 if (parent->pt_deactivate)
1187 parent->pt_deactivate(parent);
1188
1189 psmouse_activate(parent);
1190 }
1191
1192 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
1195/*
1196 * psmouse_disconnect() closes and frees.
1197 */
1198
1199static void psmouse_disconnect(struct serio *serio)
1200{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001201 struct psmouse *psmouse, *parent = NULL;
1202
1203 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001205 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Ingo Molnarc14471d2006-02-19 00:22:11 -05001207 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1210
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001211 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001212 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001213 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001214 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1217 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001218 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
1221 if (psmouse->disconnect)
1222 psmouse->disconnect(psmouse);
1223
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001224 if (parent && parent->pt_deactivate)
1225 parent->pt_deactivate(parent);
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 serio_close(serio);
1230 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001231 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001233
1234 if (parent)
1235 psmouse_activate(parent);
1236
Ingo Molnarc14471d2006-02-19 00:22:11 -05001237 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001240static int psmouse_switch_protocol(struct psmouse *psmouse,
1241 const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001242{
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001243 const struct psmouse_protocol *selected_proto;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001244 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001245
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001246 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001247
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001248 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001249 input_dev->keybit[BIT_WORD(BTN_MOUSE)] =
1250 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001251 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001252
1253 psmouse->set_rate = psmouse_set_rate;
1254 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001255 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001256 psmouse->protocol_handler = psmouse_process_byte;
1257 psmouse->pktsize = 3;
1258
1259 if (proto && (proto->detect || proto->init)) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001260 if (proto->detect && proto->detect(psmouse, true) < 0)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001261 return -1;
1262
1263 if (proto->init && proto->init(psmouse) < 0)
1264 return -1;
1265
1266 psmouse->type = proto->type;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001267 selected_proto = proto;
1268 } else {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001269 psmouse->type = psmouse_extensions(psmouse,
1270 psmouse_max_proto, true);
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001271 selected_proto = psmouse_protocol_by_type(psmouse->type);
1272 }
1273
1274 psmouse->ignore_parity = selected_proto->ignore_parity;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001275
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001276 /*
1277 * If mouse's packet size is 3 there is no point in polling the
1278 * device in hopes to detect protocol reset - we won't get less
1279 * than 3 bytes response anyhow.
1280 */
1281 if (psmouse->pktsize == 3)
1282 psmouse->resync_time = 0;
1283
1284 /*
1285 * Some smart KVMs fake response to POLL command returning just
1286 * 3 bytes and messing up our resync logic, so if initial poll
1287 * fails we won't try polling the device anymore. Hopefully
1288 * such KVM will maintain initially selected protocol.
1289 */
1290 if (psmouse->resync_time && psmouse->poll(psmouse))
1291 psmouse->resync_time = 0;
1292
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001293 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001294 selected_proto->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001295
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001296 input_dev->name = psmouse->devname;
1297 input_dev->phys = psmouse->phys;
1298 input_dev->id.bustype = BUS_I8042;
1299 input_dev->id.vendor = 0x0002;
1300 input_dev->id.product = psmouse->type;
1301 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001302
1303 return 0;
1304}
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306/*
1307 * psmouse_connect() is a callback from the serio module when
1308 * an unhandled serio port is found.
1309 */
1310static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1311{
1312 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001313 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001314 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Ingo Molnarc14471d2006-02-19 00:22:11 -05001316 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /*
1319 * If this is a pass-through port deactivate parent so the device
1320 * connected to this port can be successfully identified
1321 */
1322 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1323 parent = serio_get_drvdata(serio->parent);
1324 psmouse_deactivate(parent);
1325 }
1326
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001327 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1328 input_dev = input_allocate_device();
1329 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001330 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001333 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001334 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001335 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1338
1339 serio_set_drvdata(serio, psmouse);
1340
Dmitry Torokhov72155612006-11-05 22:40:19 -05001341 error = serio_open(serio, drv);
1342 if (error)
1343 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001346 error = -ENODEV;
1347 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
1350 psmouse->rate = psmouse_rate;
1351 psmouse->resolution = psmouse_resolution;
1352 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001353 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001356 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 psmouse_initialize(psmouse);
1360
Dmitry Torokhov72155612006-11-05 22:40:19 -05001361 error = input_register_device(psmouse->dev);
1362 if (error)
1363 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (parent && parent->pt_activate)
1366 parent->pt_activate(parent);
1367
Dmitry Torokhov72155612006-11-05 22:40:19 -05001368 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1369 if (error)
1370 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 psmouse_activate(psmouse);
1373
Dmitry Torokhov72155612006-11-05 22:40:19 -05001374 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001375 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (parent)
1377 psmouse_activate(parent);
1378
Ingo Molnarc14471d2006-02-19 00:22:11 -05001379 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001381
1382 err_pt_deactivate:
1383 if (parent && parent->pt_deactivate)
1384 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001385 input_unregister_device(psmouse->dev);
1386 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001387 err_protocol_disconnect:
1388 if (psmouse->disconnect)
1389 psmouse->disconnect(psmouse);
1390 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1391 err_close_serio:
1392 serio_close(serio);
1393 err_clear_drvdata:
1394 serio_set_drvdata(serio, NULL);
1395 err_free:
1396 input_free_device(input_dev);
1397 kfree(psmouse);
1398
1399 retval = error;
1400 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403
1404static int psmouse_reconnect(struct serio *serio)
1405{
1406 struct psmouse *psmouse = serio_get_drvdata(serio);
1407 struct psmouse *parent = NULL;
1408 struct serio_driver *drv = serio->drv;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001409 unsigned char type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int rc = -1;
1411
1412 if (!drv || !psmouse) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001413 psmouse_dbg(psmouse,
1414 "reconnect request, but serio is disconnected, ignoring...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return -1;
1416 }
1417
Ingo Molnarc14471d2006-02-19 00:22:11 -05001418 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1421 parent = serio_get_drvdata(serio->parent);
1422 psmouse_deactivate(parent);
1423 }
1424
1425 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1426
1427 if (psmouse->reconnect) {
1428 if (psmouse->reconnect(psmouse))
1429 goto out;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001430 } else {
1431 psmouse_reset(psmouse);
1432
1433 if (psmouse_probe(psmouse) < 0)
1434 goto out;
1435
1436 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1437 if (psmouse->type != type)
1438 goto out;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001441 /*
1442 * OK, the device type (and capabilities) match the old one,
1443 * we can continue using it, complete initialization
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 */
1445 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1446
1447 psmouse_initialize(psmouse);
1448
1449 if (parent && parent->pt_activate)
1450 parent->pt_activate(parent);
1451
1452 psmouse_activate(psmouse);
1453 rc = 0;
1454
1455out:
1456 /* If this is a pass-through port the parent waits to be activated */
1457 if (parent)
1458 psmouse_activate(parent);
1459
Ingo Molnarc14471d2006-02-19 00:22:11 -05001460 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return rc;
1462}
1463
1464static struct serio_device_id psmouse_serio_ids[] = {
1465 {
1466 .type = SERIO_8042,
1467 .proto = SERIO_ANY,
1468 .id = SERIO_ANY,
1469 .extra = SERIO_ANY,
1470 },
1471 {
1472 .type = SERIO_PS_PSTHRU,
1473 .proto = SERIO_ANY,
1474 .id = SERIO_ANY,
1475 .extra = SERIO_ANY,
1476 },
1477 { 0 }
1478};
1479
1480MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1481
1482static struct serio_driver psmouse_drv = {
1483 .driver = {
1484 .name = "psmouse",
1485 },
1486 .description = DRIVER_DESC,
1487 .id_table = psmouse_serio_ids,
1488 .interrupt = psmouse_interrupt,
1489 .connect = psmouse_connect,
1490 .reconnect = psmouse_reconnect,
1491 .disconnect = psmouse_disconnect,
1492 .cleanup = psmouse_cleanup,
1493};
1494
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001495ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1496 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001499 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1500 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001502 psmouse = serio_get_drvdata(serio);
1503
Eric W. Biederman59b01512010-01-05 17:56:02 -08001504 return attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001507ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1508 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001511 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1512 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 int retval;
1514
Ingo Molnarc14471d2006-02-19 00:22:11 -05001515 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001516 if (retval)
Eric W. Biederman59b01512010-01-05 17:56:02 -08001517 goto out;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001518
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001519 psmouse = serio_get_drvdata(serio);
1520
Andres Salomon68d48222008-09-16 12:30:34 -04001521 if (attr->protect) {
1522 if (psmouse->state == PSMOUSE_IGNORE) {
1523 retval = -ENODEV;
1524 goto out_unlock;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Andres Salomon68d48222008-09-16 12:30:34 -04001527 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1528 parent = serio_get_drvdata(serio->parent);
1529 psmouse_deactivate(parent);
1530 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001531
Andres Salomon68d48222008-09-16 12:30:34 -04001532 psmouse_deactivate(psmouse);
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001535 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Andres Salomon68d48222008-09-16 12:30:34 -04001537 if (attr->protect) {
1538 if (retval != -ENODEV)
1539 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001540
Andres Salomon68d48222008-09-16 12:30:34 -04001541 if (parent)
1542 psmouse_activate(parent);
1543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Ingo Molnarc14471d2006-02-19 00:22:11 -05001545 out_unlock:
1546 mutex_unlock(&psmouse_mutex);
Eric W. Biederman59b01512010-01-05 17:56:02 -08001547 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return retval;
1549}
1550
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001551static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1552{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001553 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001554
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001555 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001556}
1557
1558static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1559{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001560 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
JJ Ding76496e72011-11-09 10:20:14 -08001561 unsigned int value;
1562 int err;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001563
JJ Ding76496e72011-11-09 10:20:14 -08001564 err = kstrtouint(buf, 10, &value);
1565 if (err)
1566 return err;
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001567
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001568 *field = value;
1569
1570 return count;
1571}
1572
1573static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001574{
1575 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1576}
1577
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001578static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001579{
1580 struct serio *serio = psmouse->ps2dev.serio;
1581 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001582 struct input_dev *old_dev, *new_dev;
1583 const struct psmouse_protocol *proto, *old_proto;
1584 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001585 int retry = 0;
1586
Dmitry Torokhov72155612006-11-05 22:40:19 -05001587 proto = psmouse_protocol_by_name(buf, count);
1588 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001589 return -EINVAL;
1590
1591 if (psmouse->type == proto->type)
1592 return count;
1593
Dmitry Torokhov72155612006-11-05 22:40:19 -05001594 new_dev = input_allocate_device();
1595 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001596 return -ENOMEM;
1597
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -07001598 while (!list_empty(&serio->children)) {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001599 if (++retry > 3) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001600 psmouse_warn(psmouse,
1601 "failed to destroy children ports, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001602 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001603 return -EIO;
1604 }
1605
Ingo Molnarc14471d2006-02-19 00:22:11 -05001606 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001607 serio_unregister_child_port(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001608 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001609
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001610 if (serio->drv != &psmouse_drv) {
1611 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001612 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001613 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001614
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001615 if (psmouse->type == proto->type) {
1616 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001617 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001618 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001619 }
1620
1621 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1622 parent = serio_get_drvdata(serio->parent);
1623 if (parent->pt_deactivate)
1624 parent->pt_deactivate(parent);
1625 }
1626
Dmitry Torokhov72155612006-11-05 22:40:19 -05001627 old_dev = psmouse->dev;
1628 old_proto = psmouse_protocol_by_type(psmouse->type);
1629
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001630 if (psmouse->disconnect)
1631 psmouse->disconnect(psmouse);
1632
1633 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001634
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001635 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001636 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1637
1638 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1639 psmouse_reset(psmouse);
1640 /* default to PSMOUSE_PS2 */
1641 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1642 }
1643
1644 psmouse_initialize(psmouse);
1645 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1646
Dmitry Torokhov72155612006-11-05 22:40:19 -05001647 error = input_register_device(psmouse->dev);
1648 if (error) {
1649 if (psmouse->disconnect)
1650 psmouse->disconnect(psmouse);
1651
1652 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1653 input_free_device(new_dev);
1654 psmouse->dev = old_dev;
1655 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1656 psmouse_switch_protocol(psmouse, old_proto);
1657 psmouse_initialize(psmouse);
1658 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1659
1660 return error;
1661 }
1662
1663 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001664
1665 if (parent && parent->pt_activate)
1666 parent->pt_activate(parent);
1667
1668 return count;
1669}
1670
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001671static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
JJ Ding76496e72011-11-09 10:20:14 -08001673 unsigned int value;
1674 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
JJ Ding76496e72011-11-09 10:20:14 -08001676 err = kstrtouint(buf, 10, &value);
1677 if (err)
1678 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 psmouse->set_rate(psmouse, value);
1681 return count;
1682}
1683
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001684static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
JJ Ding76496e72011-11-09 10:20:14 -08001686 unsigned int value;
1687 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
JJ Ding76496e72011-11-09 10:20:14 -08001689 err = kstrtouint(buf, 10, &value);
1690 if (err)
1691 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 psmouse->set_resolution(psmouse, value);
1694 return count;
1695}
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001698static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Helge Dellere38de672006-09-10 21:54:39 -04001700 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 if (!val)
1703 return -EINVAL;
1704
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001705 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001707 if (!proto || !proto->maxproto)
1708 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001710 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Stephen Evanchik541e3162005-08-08 01:26:18 -05001712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001715static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001717 int type = *((unsigned int *)kp->arg);
1718
Takashi Iwai3d4c3aa2009-11-12 23:30:52 -08001719 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
1722static int __init psmouse_init(void)
1723{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001724 int err;
1725
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001726 lifebook_module_init();
1727 synaptics_module_init();
Daniel Drakeca94ec42010-11-11 22:19:57 -08001728 hgpk_module_init();
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001729
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001730 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1731 if (!kpsmoused_wq) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001732 pr_err("failed to create kpsmoused workqueue\n");
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001733 return -ENOMEM;
1734 }
1735
Akinobu Mita153a9df02006-11-23 23:35:10 -05001736 err = serio_register_driver(&psmouse_drv);
1737 if (err)
1738 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001739
Akinobu Mita153a9df02006-11-23 23:35:10 -05001740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
1743static void __exit psmouse_exit(void)
1744{
1745 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001746 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
1749module_init(psmouse_init);
1750module_exit(psmouse_exit);