blob: 73a7af2542a8c7a32c1815e72df9fb10dac3ee1c [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
14#include <linux/delay.h>
15#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/interrupt.h>
18#include <linux/input.h>
19#include <linux/serio.h>
20#include <linux/init.h>
21#include <linux/libps2.h>
Ingo Molnarc14471d2006-02-19 00:22:11 -050022#include <linux/mutex.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "psmouse.h"
25#include "synaptics.h"
26#include "logips2pp.h"
27#include "alps.h"
Andres Salomondf08ef22008-09-16 12:30:34 -040028#include "hgpk.h"
Kenan Esau02d7f582005-05-29 02:30:22 -050029#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050030#include "trackpoint.h"
Stefan Lucke24bf10a2007-02-18 01:49:10 -050031#include "touchkit_ps2.h"
Arjan Opmeer2a0bd752008-10-16 22:10:19 -040032#include "elantech.h"
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -070033#include "sentelic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define DRIVER_DESC "PS/2 mouse driver"
36
37MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
38MODULE_DESCRIPTION(DRIVER_DESC);
39MODULE_LICENSE("GPL");
40
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050041static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Rusty Russell9bbb9e52010-08-11 23:04:12 -060042static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
43static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
44static struct kernel_param_ops param_ops_proto_abbrev = {
45 .set = psmouse_set_maxproto,
46 .get = psmouse_get_maxproto,
47};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050050MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static unsigned int psmouse_resolution = 200;
53module_param_named(resolution, psmouse_resolution, uint, 0644);
54MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
55
56static unsigned int psmouse_rate = 100;
57module_param_named(rate, psmouse_rate, uint, 0644);
58MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
59
60static unsigned int psmouse_smartscroll = 1;
61module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
62MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
63
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050064static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065module_param_named(resetafter, psmouse_resetafter, uint, 0644);
66MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
67
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050068static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050069module_param_named(resync_time, psmouse_resync_time, uint, 0644);
70MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
71
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050072PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
73 NULL,
74 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
75PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
76 (void *) offsetof(struct psmouse, rate),
77 psmouse_show_int_attr, psmouse_attr_set_rate);
78PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
79 (void *) offsetof(struct psmouse, resolution),
80 psmouse_show_int_attr, psmouse_attr_set_resolution);
81PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
82 (void *) offsetof(struct psmouse, resetafter),
83 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050084PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
85 (void *) offsetof(struct psmouse, resync_time),
86 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050087
88static struct attribute *psmouse_attributes[] = {
89 &psmouse_attr_protocol.dattr.attr,
90 &psmouse_attr_rate.dattr.attr,
91 &psmouse_attr_resolution.dattr.attr,
92 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050093 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050094 NULL
95};
96
97static struct attribute_group psmouse_attribute_group = {
98 .attrs = psmouse_attributes,
99};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500101/*
Ingo Molnarc14471d2006-02-19 00:22:11 -0500102 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500103 * (connecting, disconnecting, changing rate or resolution via
104 * sysfs). We could use a per-device semaphore but since there
105 * rarely more than one PS/2 mouse connected and since semaphore
106 * is taken in "slow" paths it is not worth it.
107 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500108static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500109
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500110static struct workqueue_struct *kpsmoused_wq;
111
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500112struct psmouse_protocol {
113 enum psmouse_type type;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700114 bool maxproto;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700115 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
Helge Dellere38de672006-09-10 21:54:39 -0400116 const char *name;
117 const char *alias;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700118 int (*detect)(struct psmouse *, bool);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500119 int (*init)(struct psmouse *);
120};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/*
123 * psmouse_process_byte() analyzes the PS/2 data stream and reports
124 * relevant events to the input module once full packet has arrived.
125 */
126
David Howells7d12e782006-10-05 14:55:46 +0100127static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500129 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 unsigned char *packet = psmouse->packet;
131
132 if (psmouse->pktcnt < psmouse->pktsize)
133 return PSMOUSE_GOOD_DATA;
134
135/*
136 * Full packet accumulated, process it
137 */
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/*
140 * Scroll wheel on IntelliMice, scroll buttons on NetMice
141 */
142
143 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
144 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
145
146/*
147 * Scroll wheel and buttons on IntelliMouse Explorer
148 */
149
150 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400151 switch (packet[3] & 0xC0) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700152 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
153 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
154 break;
155 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
156 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
157 break;
158 case 0x00:
159 case 0xC0:
160 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
161 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
162 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
163 break;
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
167/*
168 * Extra buttons on Genius NewNet 3D
169 */
170
171 if (psmouse->type == PSMOUSE_GENPS) {
172 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
173 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
174 }
175
176/*
177 * Extra button on ThinkingMouse
178 */
179 if (psmouse->type == PSMOUSE_THINKPS) {
180 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
181 /* Without this bit of weirdness moving up gives wildly high Y changes. */
182 packet[1] |= (packet[0] & 0x40) << 1;
183 }
184
185/*
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400186 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
187 * byte.
188 */
189 if (psmouse->type == PSMOUSE_CORTRON) {
190 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
191 packet[0] |= 0x08;
192 }
193
194/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * Generic PS/2 Mouse
196 */
197
198 input_report_key(dev, BTN_LEFT, packet[0] & 1);
199 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
200 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
201
202 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
203 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
204
205 input_sync(dev);
206
207 return PSMOUSE_FULL_PACKET;
208}
209
Andres Salomon8bf020e2008-09-16 12:30:33 -0400210void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
211 unsigned long delay)
212{
213 queue_delayed_work(kpsmoused_wq, work, delay);
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500217 * __psmouse_set_state() sets new psmouse state and resets all flags.
218 */
219
220static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
221{
222 psmouse->state = new_state;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700223 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500224 psmouse->ps2dev.flags = 0;
225 psmouse->last = jiffies;
226}
227
228
229/*
230 * psmouse_set_state() sets new psmouse state and resets all flags and
231 * counters while holding serio lock so fighting with interrupt handler
232 * is not a concern.
233 */
234
Andres Salomona48cf5f2008-09-16 12:30:33 -0400235void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500236{
237 serio_pause_rx(psmouse->ps2dev.serio);
238 __psmouse_set_state(psmouse, new_state);
239 serio_continue_rx(psmouse->ps2dev.serio);
240}
241
242/*
243 * psmouse_handle_byte() processes one byte of the input data stream
244 * by calling corresponding protocol handler.
245 */
246
David Howells7d12e782006-10-05 14:55:46 +0100247static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500248{
David Howells7d12e782006-10-05 14:55:46 +0100249 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500250
251 switch (rc) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700252 case PSMOUSE_BAD_DATA:
253 if (psmouse->state == PSMOUSE_ACTIVATED) {
254 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
255 psmouse->name, psmouse->phys, psmouse->pktcnt);
256 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
257 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
258 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
259 serio_reconnect(psmouse->ps2dev.serio);
260 return -1;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500261 }
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700262 }
263 psmouse->pktcnt = 0;
264 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500265
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700266 case PSMOUSE_FULL_PACKET:
267 psmouse->pktcnt = 0;
268 if (psmouse->out_of_sync_cnt) {
269 psmouse->out_of_sync_cnt = 0;
270 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
271 psmouse->name, psmouse->phys);
272 }
273 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500274
Dmitry Torokhova62f0d22010-05-19 10:39:17 -0700275 case PSMOUSE_GOOD_DATA:
276 break;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500277 }
278 return 0;
279}
280
281/*
282 * psmouse_interrupt() handles incoming characters, either passing them
283 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285
286static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100287 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (psmouse->state == PSMOUSE_IGNORE)
292 goto out;
293
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700294 if (unlikely((flags & SERIO_TIMEOUT) ||
295 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (psmouse->state == PSMOUSE_ACTIVATED)
298 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
299 flags & SERIO_TIMEOUT ? " timeout" : "",
300 flags & SERIO_PARITY ? " bad parity" : "");
301 ps2_cmd_aborted(&psmouse->ps2dev);
302 goto out;
303 }
304
305 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
306 if (ps2_handle_ack(&psmouse->ps2dev, data))
307 goto out;
308
309 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
310 if (ps2_handle_response(&psmouse->ps2dev, data))
311 goto out;
312
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500313 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 goto out;
315
316 if (psmouse->state == PSMOUSE_ACTIVATED &&
317 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500318 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500320 psmouse->badbyte = psmouse->packet[0];
321 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400322 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500323 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500327/*
328 * Check if this is a new device announcement (0xAA 0x00)
329 */
330 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400331 if (psmouse->pktcnt == 1) {
332 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Zephaniah E. Hull535650f2009-05-14 22:02:33 -0700336 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
337 (psmouse->type == PSMOUSE_HGPK &&
338 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500339 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
340 serio_reconnect(serio);
341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500343/*
344 * Not a new device, try processing first byte normally
345 */
346 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100347 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500348 goto out;
349
350 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500353/*
354 * See if we need to force resync because mouse was idle for too long
355 */
356 if (psmouse->state == PSMOUSE_ACTIVATED &&
357 psmouse->pktcnt == 1 && psmouse->resync_time &&
358 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
359 psmouse->badbyte = psmouse->packet[0];
360 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400361 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500364
365 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100366 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500367
368 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return IRQ_HANDLED;
370}
371
372
373/*
374 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
375 * using sliced syntax, understood by advanced devices, such as Logitech
376 * or Synaptics touchpads. The command is encoded as:
377 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
378 * is the command.
379 */
380int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
381{
382 int i;
383
384 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
385 return -1;
386
387 for (i = 6; i >= 0; i -= 2) {
388 unsigned char d = (command >> i) & 3;
389 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
390 return -1;
391 }
392
393 return 0;
394}
395
396
397/*
398 * psmouse_reset() resets the mouse into power-on state.
399 */
400int psmouse_reset(struct psmouse *psmouse)
401{
402 unsigned char param[2];
403
404 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
405 return -1;
406
407 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
408 return -1;
409
410 return 0;
411}
412
413
414/*
415 * Genius NetMouse magic init.
416 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700417static int genius_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct ps2dev *ps2dev = &psmouse->ps2dev;
420 unsigned char param[4];
421
422 param[0] = 3;
423 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
424 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
425 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
426 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
427 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
428
429 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
430 return -1;
431
432 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800433 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700434 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
435 __set_bit(BTN_SIDE, psmouse->dev->keybit);
436 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500439 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 psmouse->pktsize = 4;
441 }
442
443 return 0;
444}
445
446/*
447 * IntelliMouse magic init.
448 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700449static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct ps2dev *ps2dev = &psmouse->ps2dev;
452 unsigned char param[2];
453
454 param[0] = 200;
455 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
456 param[0] = 100;
457 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
458 param[0] = 80;
459 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
460 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
461
462 if (param[0] != 3)
463 return -1;
464
465 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700466 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
467 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800469 if (!psmouse->vendor)
470 psmouse->vendor = "Generic";
471 if (!psmouse->name)
472 psmouse->name = "Wheel Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 psmouse->pktsize = 4;
474 }
475
476 return 0;
477}
478
479/*
480 * Try IntelliMouse/Explorer magic init.
481 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700482static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct ps2dev *ps2dev = &psmouse->ps2dev;
485 unsigned char param[2];
486
487 intellimouse_detect(psmouse, 0);
488
489 param[0] = 200;
490 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
491 param[0] = 200;
492 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
493 param[0] = 80;
494 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
495 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
496
497 if (param[0] != 4)
498 return -1;
499
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400500/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
501 param[0] = 200;
502 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
503 param[0] = 80;
504 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
505 param[0] = 40;
506 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700509 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
510 __set_bit(REL_WHEEL, psmouse->dev->relbit);
511 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
512 __set_bit(BTN_SIDE, psmouse->dev->keybit);
513 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800515 if (!psmouse->vendor)
516 psmouse->vendor = "Generic";
517 if (!psmouse->name)
518 psmouse->name = "Explorer Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 psmouse->pktsize = 4;
520 }
521
522 return 0;
523}
524
525/*
526 * Kensington ThinkingMouse / ExpertMouse magic init.
527 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700528static int thinking_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct ps2dev *ps2dev = &psmouse->ps2dev;
531 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400532 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int i;
534
535 param[0] = 10;
536 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
537 param[0] = 0;
538 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400539 for (i = 0; i < ARRAY_SIZE(seq); i++) {
540 param[0] = seq[i];
541 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
544
545 if (param[0] != 2)
546 return -1;
547
548 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800549 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700550 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 psmouse->vendor = "Kensington";
553 psmouse->name = "ThinkingMouse";
554 }
555
556 return 0;
557}
558
559/*
560 * Bare PS/2 protocol "detection". Always succeeds.
561 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700562static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500564 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800565 if (!psmouse->vendor)
566 psmouse->vendor = "Generic";
567 if (!psmouse->name)
568 psmouse->name = "Mouse";
569
570/*
571 * We have no way of figuring true number of buttons so let's
572 * assume that the device has 3.
573 */
574 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 return 0;
578}
579
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400580/*
581 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
582 * must be forced by sysfs protocol writing.
583 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700584static int cortron_detect(struct psmouse *psmouse, bool set_properties)
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400585{
586 if (set_properties) {
587 psmouse->vendor = "Cortron";
588 psmouse->name = "PS/2 Trackball";
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800589
590 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700591 __set_bit(BTN_SIDE, psmouse->dev->keybit);
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400592 }
593
594 return 0;
595}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/*
598 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
599 * the mouse may have.
600 */
601
602static int psmouse_extensions(struct psmouse *psmouse,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700603 unsigned int max_proto, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Jiri Kosina06989892009-11-16 22:12:13 -0800605 bool synaptics_hardware = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500607/*
608 * We always check for lifebook because it does not disturb mouse
609 * (it only checks DMI information).
610 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500611 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500612 if (max_proto > PSMOUSE_IMEX) {
613 if (!set_properties || lifebook_init(psmouse) == 0)
614 return PSMOUSE_LIFEBOOK;
615 }
616 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/*
619 * Try Kensington ThinkingMouse (we try first, because synaptics probe
620 * upsets the thinkingmouse).
621 */
622
623 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
624 return PSMOUSE_THINKPS;
625
626/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500627 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
628 * support is disabled in config - we need to know if it is synaptics so we
629 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
631 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700632 synaptics_hardware = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (max_proto > PSMOUSE_IMEX) {
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800635/*
636 * Try activating protocol, but check if support is enabled first, since
637 * we try detecting Synaptics even when protocol is disabled.
638 */
639 if (synaptics_supported() &&
640 (!set_properties || synaptics_init(psmouse) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return PSMOUSE_SYNAPTICS;
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800642 }
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/*
645 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
646 * Unfortunately Logitech/Genius probes confuse some firmware versions so
647 * we'll have to skip them.
648 */
649 max_proto = PSMOUSE_IMEX;
650 }
651/*
652 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
653 */
654 synaptics_reset(psmouse);
655 }
656
657/*
658 * Try ALPS TouchPad
659 */
660 if (max_proto > PSMOUSE_IMEX) {
661 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
662 if (alps_detect(psmouse, set_properties) == 0) {
663 if (!set_properties || alps_init(psmouse) == 0)
664 return PSMOUSE_ALPS;
665/*
666 * Init failed, try basic relative protocols
667 */
668 max_proto = PSMOUSE_IMEX;
669 }
670 }
671
Andres Salomondf08ef22008-09-16 12:30:34 -0400672/*
673 * Try OLPC HGPK touchpad.
674 */
675 if (max_proto > PSMOUSE_IMEX &&
676 hgpk_detect(psmouse, set_properties) == 0) {
677 if (!set_properties || hgpk_init(psmouse) == 0)
678 return PSMOUSE_HGPK;
679/*
680 * Init failed, try basic relative protocols
681 */
682 max_proto = PSMOUSE_IMEX;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400685/*
686 * Try Elantech touchpad.
687 */
688 if (max_proto > PSMOUSE_IMEX &&
689 elantech_detect(psmouse, set_properties) == 0) {
690 if (!set_properties || elantech_init(psmouse) == 0)
691 return PSMOUSE_ELANTECH;
692/*
693 * Init failed, try basic relative protocols
694 */
695 max_proto = PSMOUSE_IMEX;
696 }
697
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700698
Andres Salomondf08ef22008-09-16 12:30:34 -0400699 if (max_proto > PSMOUSE_IMEX) {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500700 if (genius_detect(psmouse, set_properties) == 0)
701 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500703 if (ps2pp_init(psmouse, set_properties) == 0)
704 return PSMOUSE_PS2PP;
705
706 if (trackpoint_detect(psmouse, set_properties) == 0)
707 return PSMOUSE_TRACKPOINT;
708
709 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
710 return PSMOUSE_TOUCHKIT_PS2;
711 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/*
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -0800714 * Try Finger Sensing Pad. We do it here because its probe upsets
715 * Trackpoint devices (causing TP_READ_ID command to time out).
716 */
717 if (max_proto > PSMOUSE_IMEX) {
718 if (fsp_detect(psmouse, set_properties) == 0) {
719 if (!set_properties || fsp_init(psmouse) == 0)
720 return PSMOUSE_FSP;
721/*
722 * Init failed, try basic relative protocols
723 */
724 max_proto = PSMOUSE_IMEX;
725 }
726 }
727
728/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400730 * protocol probes. Note that we follow up with full reset because
731 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
Alon Ziv554fc192007-08-30 00:22:48 -0400733 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500734 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
737 return PSMOUSE_IMEX;
738
739 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
740 return PSMOUSE_IMPS;
741
742/*
743 * Okay, all failed, we have a standard mouse here. The number of the buttons
744 * is still a question, though. We assume 3.
745 */
746 ps2bare_detect(psmouse, set_properties);
747
748 if (synaptics_hardware) {
749/*
750 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
751 * We need to reset the touchpad because if there is a track point on the
752 * pass through port it could get disabled while probing for protocol
753 * extensions.
754 */
755 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757
758 return PSMOUSE_PS2;
759}
760
Helge Dellere38de672006-09-10 21:54:39 -0400761static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500762 {
763 .type = PSMOUSE_PS2,
764 .name = "PS/2",
765 .alias = "bare",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700766 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700767 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500768 .detect = ps2bare_detect,
769 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500770#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500771 {
772 .type = PSMOUSE_PS2PP,
773 .name = "PS2++",
774 .alias = "logitech",
775 .detect = ps2pp_init,
776 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500777#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500778 {
779 .type = PSMOUSE_THINKPS,
780 .name = "ThinkPS/2",
781 .alias = "thinkps",
782 .detect = thinking_detect,
783 },
784 {
785 .type = PSMOUSE_GENPS,
786 .name = "GenPS/2",
787 .alias = "genius",
788 .detect = genius_detect,
789 },
790 {
791 .type = PSMOUSE_IMPS,
792 .name = "ImPS/2",
793 .alias = "imps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700794 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700795 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500796 .detect = intellimouse_detect,
797 },
798 {
799 .type = PSMOUSE_IMEX,
800 .name = "ImExPS/2",
801 .alias = "exps",
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 = im_explorer_detect,
805 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500806#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500807 {
808 .type = PSMOUSE_SYNAPTICS,
809 .name = "SynPS/2",
810 .alias = "synaptics",
811 .detect = synaptics_detect,
812 .init = synaptics_init,
813 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500814#endif
815#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500816 {
817 .type = PSMOUSE_ALPS,
818 .name = "AlpsPS/2",
819 .alias = "alps",
820 .detect = alps_detect,
821 .init = alps_init,
822 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500823#endif
824#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500825 {
826 .type = PSMOUSE_LIFEBOOK,
827 .name = "LBPS/2",
828 .alias = "lifebook",
829 .init = lifebook_init,
830 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500831#endif
832#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500833 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500834 .type = PSMOUSE_TRACKPOINT,
835 .name = "TPPS/2",
836 .alias = "trackpoint",
837 .detect = trackpoint_detect,
838 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500839#endif
840#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500841 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500842 .type = PSMOUSE_TOUCHKIT_PS2,
843 .name = "touchkitPS/2",
844 .alias = "touchkit",
845 .detect = touchkit_ps2_detect,
846 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500847#endif
Andres Salomondf08ef22008-09-16 12:30:34 -0400848#ifdef CONFIG_MOUSE_PS2_OLPC
849 {
850 .type = PSMOUSE_HGPK,
851 .name = "OLPC HGPK",
852 .alias = "hgpk",
853 .detect = hgpk_detect,
854 },
855#endif
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400856#ifdef CONFIG_MOUSE_PS2_ELANTECH
857 {
858 .type = PSMOUSE_ELANTECH,
859 .name = "ETPS/2",
860 .alias = "elantech",
861 .detect = elantech_detect,
862 .init = elantech_init,
863 },
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700864#endif
865#ifdef CONFIG_MOUSE_PS2_SENTELIC
866 {
867 .type = PSMOUSE_FSP,
868 .name = "FSPPS/2",
869 .alias = "fsp",
870 .detect = fsp_detect,
871 .init = fsp_init,
872 },
873#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500874 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400875 .type = PSMOUSE_CORTRON,
876 .name = "CortronPS/2",
877 .alias = "cortps",
878 .detect = cortron_detect,
879 },
880 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500881 .type = PSMOUSE_AUTO,
882 .name = "auto",
883 .alias = "any",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700884 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500885 },
886};
887
Helge Dellere38de672006-09-10 21:54:39 -0400888static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500889{
890 int i;
891
892 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
893 if (psmouse_protocols[i].type == type)
894 return &psmouse_protocols[i];
895
896 WARN_ON(1);
897 return &psmouse_protocols[0];
898}
899
Helge Dellere38de672006-09-10 21:54:39 -0400900static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500901{
Helge Dellere38de672006-09-10 21:54:39 -0400902 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500903 int i;
904
905 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
906 p = &psmouse_protocols[i];
907
908 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
909 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
910 return &psmouse_protocols[i];
911 }
912
913 return NULL;
914}
915
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/*
918 * psmouse_probe() probes for a PS/2 mouse.
919 */
920
921static int psmouse_probe(struct psmouse *psmouse)
922{
923 struct ps2dev *ps2dev = &psmouse->ps2dev;
924 unsigned char param[2];
925
926/*
927 * First, we check if it's a mouse. It should send 0x00 or 0x03
928 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500929 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
930 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
932
933 param[0] = 0xa5;
934 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
935 return -1;
936
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500937 if (param[0] != 0x00 && param[0] != 0x03 &&
938 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return -1;
940
941/*
942 * Then we reset and disable the mouse so that it doesn't generate events.
943 */
944
945 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
946 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
947
948 return 0;
949}
950
951/*
952 * Here we set the mouse resolution.
953 */
954
955void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
956{
Helge Dellere38de672006-09-10 21:54:39 -0400957 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
958 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (resolution == 0 || resolution > 200)
961 resolution = 200;
962
Helge Dellere38de672006-09-10 21:54:39 -0400963 p = params[resolution / 50];
964 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
965 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
969 * Here we set the mouse report rate.
970 */
971
972static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
973{
Helge Dellere38de672006-09-10 21:54:39 -0400974 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
975 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int i = 0;
977
978 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400979 r = rates[i];
980 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
981 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984/*
985 * psmouse_initialize() initializes the mouse to a sane state.
986 */
987
988static void psmouse_initialize(struct psmouse *psmouse)
989{
990/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * We set the mouse report rate, resolution and scaling.
992 */
993
994 if (psmouse_max_proto != PSMOUSE_PS2) {
995 psmouse->set_rate(psmouse, psmouse->rate);
996 psmouse->set_resolution(psmouse, psmouse->resolution);
997 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
998 }
999}
1000
1001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * psmouse_activate() enables the mouse so that we get motion reports from it.
1003 */
1004
1005static void psmouse_activate(struct psmouse *psmouse)
1006{
1007 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1008 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
1009 psmouse->ps2dev.serio->phys);
1010
1011 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1012}
1013
1014
1015/*
1016 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +02001017 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 */
1019
1020static void psmouse_deactivate(struct psmouse *psmouse)
1021{
1022 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1023 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
1024 psmouse->ps2dev.serio->phys);
1025
1026 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1027}
1028
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001029/*
1030 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
1031 */
1032
1033static int psmouse_poll(struct psmouse *psmouse)
1034{
1035 return ps2_command(&psmouse->ps2dev, psmouse->packet,
1036 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
1037}
1038
1039
1040/*
1041 * psmouse_resync() attempts to re-validate current protocol.
1042 */
1043
David Howellsc4028952006-11-22 14:57:56 +00001044static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001045{
David Howellsc4028952006-11-22 14:57:56 +00001046 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -04001047 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001048 struct serio *serio = psmouse->ps2dev.serio;
1049 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001050 bool failed = false, enabled = false;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001051 int i;
1052
Ingo Molnarc14471d2006-02-19 00:22:11 -05001053 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001054
1055 if (psmouse->state != PSMOUSE_RESYNCING)
1056 goto out;
1057
1058 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1059 parent = serio_get_drvdata(serio->parent);
1060 psmouse_deactivate(parent);
1061 }
1062
1063/*
1064 * Some mice don't ACK commands sent while they are in the middle of
1065 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1066 * instead of ps2_command() which would wait for 200ms for an ACK
1067 * that may never come.
1068 * As an additional quirk ALPS touchpads may not only forget to ACK
1069 * disable command but will stop reporting taps, so if we see that
1070 * mouse at least once ACKs disable we will do full reconnect if ACK
1071 * is missing.
1072 */
1073 psmouse->num_resyncs++;
1074
1075 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1076 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001077 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001078 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001079 psmouse->acks_disable_command = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001080
1081/*
1082 * Poll the mouse. If it was reset the packet will be shorter than
1083 * psmouse->pktsize and ps2_command will fail. We do not expect and
1084 * do not handle scenario when mouse "upgrades" its protocol while
1085 * disconnected since it would require additional delay. If we ever
1086 * see a mouse that does it we'll adjust the code.
1087 */
1088 if (!failed) {
1089 if (psmouse->poll(psmouse))
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001090 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001091 else {
1092 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1093 for (i = 0; i < psmouse->pktsize; i++) {
1094 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001095 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001096 if (rc != PSMOUSE_GOOD_DATA)
1097 break;
1098 }
1099 if (rc != PSMOUSE_FULL_PACKET)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001100 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001101 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1102 }
1103 }
1104/*
1105 * Now try to enable mouse. We try to do that even if poll failed and also
1106 * repeat our attempts 5 times, otherwise we may be left out with disabled
1107 * mouse.
1108 */
1109 for (i = 0; i < 5; i++) {
1110 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001111 enabled = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001112 break;
1113 }
1114 msleep(200);
1115 }
1116
1117 if (!enabled) {
1118 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1119 psmouse->ps2dev.serio->phys);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001120 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001121 }
1122
1123 if (failed) {
1124 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1125 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1126 serio_reconnect(serio);
1127 } else
1128 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1129
1130 if (parent)
1131 psmouse_activate(parent);
1132 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001133 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001134}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136/*
1137 * psmouse_cleanup() resets the mouse into power-on state.
1138 */
1139
1140static void psmouse_cleanup(struct serio *serio)
1141{
1142 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001143 struct psmouse *parent = NULL;
1144
1145 mutex_lock(&psmouse_mutex);
1146
1147 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1148 parent = serio_get_drvdata(serio->parent);
1149 psmouse_deactivate(parent);
1150 }
1151
Dmitry Torokhova9f0c382010-02-07 23:10:04 -08001152 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1153
1154 /*
1155 * Disable stream mode so cleanup routine can proceed undisturbed.
1156 */
1157 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1158 printk(KERN_WARNING "psmouse.c: Failed to disable mouse on %s\n",
1159 psmouse->ps2dev.serio->phys);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001160
1161 if (psmouse->cleanup)
1162 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Dmitry Torokhov4a299bf2009-12-24 21:40:24 -08001164/*
1165 * Reset the mouse to defaults (bare PS/2 protocol).
1166 */
1167 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001168
1169/*
1170 * Some boxes, such as HP nx7400, get terribly confused if mouse
1171 * is not fully enabled before suspending/shutting down.
1172 */
1173 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1174
1175 if (parent) {
1176 if (parent->pt_deactivate)
1177 parent->pt_deactivate(parent);
1178
1179 psmouse_activate(parent);
1180 }
1181
1182 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185/*
1186 * psmouse_disconnect() closes and frees.
1187 */
1188
1189static void psmouse_disconnect(struct serio *serio)
1190{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001191 struct psmouse *psmouse, *parent = NULL;
1192
1193 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001195 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Ingo Molnarc14471d2006-02-19 00:22:11 -05001197 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1200
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001201 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001202 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001203 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001204 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1207 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001208 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210
1211 if (psmouse->disconnect)
1212 psmouse->disconnect(psmouse);
1213
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001214 if (parent && parent->pt_deactivate)
1215 parent->pt_deactivate(parent);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 serio_close(serio);
1220 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001221 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001223
1224 if (parent)
1225 psmouse_activate(parent);
1226
Ingo Molnarc14471d2006-02-19 00:22:11 -05001227 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001230static int psmouse_switch_protocol(struct psmouse *psmouse,
1231 const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001232{
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001233 const struct psmouse_protocol *selected_proto;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001234 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001235
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001236 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001237
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001238 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001239 input_dev->keybit[BIT_WORD(BTN_MOUSE)] =
1240 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001241 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001242
1243 psmouse->set_rate = psmouse_set_rate;
1244 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001245 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001246 psmouse->protocol_handler = psmouse_process_byte;
1247 psmouse->pktsize = 3;
1248
1249 if (proto && (proto->detect || proto->init)) {
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001250 if (proto->detect && proto->detect(psmouse, true) < 0)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001251 return -1;
1252
1253 if (proto->init && proto->init(psmouse) < 0)
1254 return -1;
1255
1256 psmouse->type = proto->type;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001257 selected_proto = proto;
1258 } else {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001259 psmouse->type = psmouse_extensions(psmouse,
1260 psmouse_max_proto, true);
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001261 selected_proto = psmouse_protocol_by_type(psmouse->type);
1262 }
1263
1264 psmouse->ignore_parity = selected_proto->ignore_parity;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001265
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001266 /*
1267 * If mouse's packet size is 3 there is no point in polling the
1268 * device in hopes to detect protocol reset - we won't get less
1269 * than 3 bytes response anyhow.
1270 */
1271 if (psmouse->pktsize == 3)
1272 psmouse->resync_time = 0;
1273
1274 /*
1275 * Some smart KVMs fake response to POLL command returning just
1276 * 3 bytes and messing up our resync logic, so if initial poll
1277 * fails we won't try polling the device anymore. Hopefully
1278 * such KVM will maintain initially selected protocol.
1279 */
1280 if (psmouse->resync_time && psmouse->poll(psmouse))
1281 psmouse->resync_time = 0;
1282
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001283 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001284 selected_proto->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001285
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001286 input_dev->name = psmouse->devname;
1287 input_dev->phys = psmouse->phys;
1288 input_dev->id.bustype = BUS_I8042;
1289 input_dev->id.vendor = 0x0002;
1290 input_dev->id.product = psmouse->type;
1291 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001292
1293 return 0;
1294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/*
1297 * psmouse_connect() is a callback from the serio module when
1298 * an unhandled serio port is found.
1299 */
1300static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1301{
1302 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001303 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001304 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Ingo Molnarc14471d2006-02-19 00:22:11 -05001306 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /*
1309 * If this is a pass-through port deactivate parent so the device
1310 * connected to this port can be successfully identified
1311 */
1312 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1313 parent = serio_get_drvdata(serio->parent);
1314 psmouse_deactivate(parent);
1315 }
1316
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001317 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1318 input_dev = input_allocate_device();
1319 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001320 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001323 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001324 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001325 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1328
1329 serio_set_drvdata(serio, psmouse);
1330
Dmitry Torokhov72155612006-11-05 22:40:19 -05001331 error = serio_open(serio, drv);
1332 if (error)
1333 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001336 error = -ENODEV;
1337 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
1340 psmouse->rate = psmouse_rate;
1341 psmouse->resolution = psmouse_resolution;
1342 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001343 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001346 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 psmouse_initialize(psmouse);
1350
Dmitry Torokhov72155612006-11-05 22:40:19 -05001351 error = input_register_device(psmouse->dev);
1352 if (error)
1353 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (parent && parent->pt_activate)
1356 parent->pt_activate(parent);
1357
Dmitry Torokhov72155612006-11-05 22:40:19 -05001358 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1359 if (error)
1360 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 psmouse_activate(psmouse);
1363
Dmitry Torokhov72155612006-11-05 22:40:19 -05001364 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001365 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (parent)
1367 psmouse_activate(parent);
1368
Ingo Molnarc14471d2006-02-19 00:22:11 -05001369 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001371
1372 err_pt_deactivate:
1373 if (parent && parent->pt_deactivate)
1374 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001375 input_unregister_device(psmouse->dev);
1376 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001377 err_protocol_disconnect:
1378 if (psmouse->disconnect)
1379 psmouse->disconnect(psmouse);
1380 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1381 err_close_serio:
1382 serio_close(serio);
1383 err_clear_drvdata:
1384 serio_set_drvdata(serio, NULL);
1385 err_free:
1386 input_free_device(input_dev);
1387 kfree(psmouse);
1388
1389 retval = error;
1390 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393
1394static int psmouse_reconnect(struct serio *serio)
1395{
1396 struct psmouse *psmouse = serio_get_drvdata(serio);
1397 struct psmouse *parent = NULL;
1398 struct serio_driver *drv = serio->drv;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001399 unsigned char type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int rc = -1;
1401
1402 if (!drv || !psmouse) {
1403 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1404 return -1;
1405 }
1406
Ingo Molnarc14471d2006-02-19 00:22:11 -05001407 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1410 parent = serio_get_drvdata(serio->parent);
1411 psmouse_deactivate(parent);
1412 }
1413
1414 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1415
1416 if (psmouse->reconnect) {
1417 if (psmouse->reconnect(psmouse))
1418 goto out;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001419 } else {
1420 psmouse_reset(psmouse);
1421
1422 if (psmouse_probe(psmouse) < 0)
1423 goto out;
1424
1425 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1426 if (psmouse->type != type)
1427 goto out;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 /* ok, the device type (and capabilities) match the old one,
1431 * we can continue using it, complete intialization
1432 */
1433 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1434
1435 psmouse_initialize(psmouse);
1436
1437 if (parent && parent->pt_activate)
1438 parent->pt_activate(parent);
1439
1440 psmouse_activate(psmouse);
1441 rc = 0;
1442
1443out:
1444 /* If this is a pass-through port the parent waits to be activated */
1445 if (parent)
1446 psmouse_activate(parent);
1447
Ingo Molnarc14471d2006-02-19 00:22:11 -05001448 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return rc;
1450}
1451
1452static struct serio_device_id psmouse_serio_ids[] = {
1453 {
1454 .type = SERIO_8042,
1455 .proto = SERIO_ANY,
1456 .id = SERIO_ANY,
1457 .extra = SERIO_ANY,
1458 },
1459 {
1460 .type = SERIO_PS_PSTHRU,
1461 .proto = SERIO_ANY,
1462 .id = SERIO_ANY,
1463 .extra = SERIO_ANY,
1464 },
1465 { 0 }
1466};
1467
1468MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1469
1470static struct serio_driver psmouse_drv = {
1471 .driver = {
1472 .name = "psmouse",
1473 },
1474 .description = DRIVER_DESC,
1475 .id_table = psmouse_serio_ids,
1476 .interrupt = psmouse_interrupt,
1477 .connect = psmouse_connect,
1478 .reconnect = psmouse_reconnect,
1479 .disconnect = psmouse_disconnect,
1480 .cleanup = psmouse_cleanup,
1481};
1482
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001483ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1484 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001487 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1488 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001490 psmouse = serio_get_drvdata(serio);
1491
Eric W. Biederman59b01512010-01-05 17:56:02 -08001492 return attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001495ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1496 const char *buf, size_t count)
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, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int retval;
1502
Ingo Molnarc14471d2006-02-19 00:22:11 -05001503 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001504 if (retval)
Eric W. Biederman59b01512010-01-05 17:56:02 -08001505 goto out;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001506
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001507 psmouse = serio_get_drvdata(serio);
1508
Andres Salomon68d48222008-09-16 12:30:34 -04001509 if (attr->protect) {
1510 if (psmouse->state == PSMOUSE_IGNORE) {
1511 retval = -ENODEV;
1512 goto out_unlock;
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Andres Salomon68d48222008-09-16 12:30:34 -04001515 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1516 parent = serio_get_drvdata(serio->parent);
1517 psmouse_deactivate(parent);
1518 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001519
Andres Salomon68d48222008-09-16 12:30:34 -04001520 psmouse_deactivate(psmouse);
1521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001523 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Andres Salomon68d48222008-09-16 12:30:34 -04001525 if (attr->protect) {
1526 if (retval != -ENODEV)
1527 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001528
Andres Salomon68d48222008-09-16 12:30:34 -04001529 if (parent)
1530 psmouse_activate(parent);
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Ingo Molnarc14471d2006-02-19 00:22:11 -05001533 out_unlock:
1534 mutex_unlock(&psmouse_mutex);
Eric W. Biederman59b01512010-01-05 17:56:02 -08001535 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return retval;
1537}
1538
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001539static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1540{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001541 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001542
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001543 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001544}
1545
1546static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1547{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001548 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001549 unsigned long value;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001550
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001551 if (strict_strtoul(buf, 10, &value))
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001552 return -EINVAL;
1553
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001554 if ((unsigned int)value != value)
1555 return -EINVAL;
1556
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001557 *field = value;
1558
1559 return count;
1560}
1561
1562static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001563{
1564 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1565}
1566
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001567static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001568{
1569 struct serio *serio = psmouse->ps2dev.serio;
1570 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001571 struct input_dev *old_dev, *new_dev;
1572 const struct psmouse_protocol *proto, *old_proto;
1573 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001574 int retry = 0;
1575
Dmitry Torokhov72155612006-11-05 22:40:19 -05001576 proto = psmouse_protocol_by_name(buf, count);
1577 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001578 return -EINVAL;
1579
1580 if (psmouse->type == proto->type)
1581 return count;
1582
Dmitry Torokhov72155612006-11-05 22:40:19 -05001583 new_dev = input_allocate_device();
1584 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001585 return -ENOMEM;
1586
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001587 while (serio->child) {
1588 if (++retry > 3) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001589 printk(KERN_WARNING
1590 "psmouse: failed to destroy child port, "
1591 "protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001592 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001593 return -EIO;
1594 }
1595
Ingo Molnarc14471d2006-02-19 00:22:11 -05001596 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001597 serio_unregister_child_port(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001598 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001599
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001600 if (serio->drv != &psmouse_drv) {
1601 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001602 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001603 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001604
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001605 if (psmouse->type == proto->type) {
1606 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001607 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001608 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001609 }
1610
1611 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1612 parent = serio_get_drvdata(serio->parent);
1613 if (parent->pt_deactivate)
1614 parent->pt_deactivate(parent);
1615 }
1616
Dmitry Torokhov72155612006-11-05 22:40:19 -05001617 old_dev = psmouse->dev;
1618 old_proto = psmouse_protocol_by_type(psmouse->type);
1619
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001620 if (psmouse->disconnect)
1621 psmouse->disconnect(psmouse);
1622
1623 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001624
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001625 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001626 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1627
1628 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1629 psmouse_reset(psmouse);
1630 /* default to PSMOUSE_PS2 */
1631 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1632 }
1633
1634 psmouse_initialize(psmouse);
1635 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1636
Dmitry Torokhov72155612006-11-05 22:40:19 -05001637 error = input_register_device(psmouse->dev);
1638 if (error) {
1639 if (psmouse->disconnect)
1640 psmouse->disconnect(psmouse);
1641
1642 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1643 input_free_device(new_dev);
1644 psmouse->dev = old_dev;
1645 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1646 psmouse_switch_protocol(psmouse, old_proto);
1647 psmouse_initialize(psmouse);
1648 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1649
1650 return error;
1651 }
1652
1653 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001654
1655 if (parent && parent->pt_activate)
1656 parent->pt_activate(parent);
1657
1658 return count;
1659}
1660
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001661static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001665 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return -EINVAL;
1667
1668 psmouse->set_rate(psmouse, value);
1669 return count;
1670}
1671
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001672static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001676 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return -EINVAL;
1678
1679 psmouse->set_resolution(psmouse, value);
1680 return count;
1681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001684static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Helge Dellere38de672006-09-10 21:54:39 -04001686 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 if (!val)
1689 return -EINVAL;
1690
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001691 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001693 if (!proto || !proto->maxproto)
1694 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001696 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Stephen Evanchik541e3162005-08-08 01:26:18 -05001698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Rusty Russell9bbb9e52010-08-11 23:04:12 -06001701static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001703 int type = *((unsigned int *)kp->arg);
1704
Takashi Iwai3d4c3aa2009-11-12 23:30:52 -08001705 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
1708static int __init psmouse_init(void)
1709{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001710 int err;
1711
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001712 lifebook_module_init();
1713 synaptics_module_init();
1714
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001715 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1716 if (!kpsmoused_wq) {
1717 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1718 return -ENOMEM;
1719 }
1720
Akinobu Mita153a9df02006-11-23 23:35:10 -05001721 err = serio_register_driver(&psmouse_drv);
1722 if (err)
1723 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001724
Akinobu Mita153a9df02006-11-23 23:35:10 -05001725 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728static void __exit psmouse_exit(void)
1729{
1730 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001731 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734module_init(psmouse_init);
1735module_exit(psmouse_exit);