blob: a3c97315a4730fd5b26fc2c87313ab14f6450aee [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
43static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
45#define param_set_proto_abbrev psmouse_set_maxproto
46#define param_get_proto_abbrev psmouse_get_maxproto
47module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050048MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static unsigned int psmouse_resolution = 200;
51module_param_named(resolution, psmouse_resolution, uint, 0644);
52MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
53
54static unsigned int psmouse_rate = 100;
55module_param_named(rate, psmouse_rate, uint, 0644);
56MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
57
58static unsigned int psmouse_smartscroll = 1;
59module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
60MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
61
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050062static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063module_param_named(resetafter, psmouse_resetafter, uint, 0644);
64MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
65
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050066static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050067module_param_named(resync_time, psmouse_resync_time, uint, 0644);
68MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
69
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050070PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
71 NULL,
72 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
73PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
74 (void *) offsetof(struct psmouse, rate),
75 psmouse_show_int_attr, psmouse_attr_set_rate);
76PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
77 (void *) offsetof(struct psmouse, resolution),
78 psmouse_show_int_attr, psmouse_attr_set_resolution);
79PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
80 (void *) offsetof(struct psmouse, resetafter),
81 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050082PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
83 (void *) offsetof(struct psmouse, resync_time),
84 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050085
86static struct attribute *psmouse_attributes[] = {
87 &psmouse_attr_protocol.dattr.attr,
88 &psmouse_attr_rate.dattr.attr,
89 &psmouse_attr_resolution.dattr.attr,
90 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050091 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050092 NULL
93};
94
95static struct attribute_group psmouse_attribute_group = {
96 .attrs = psmouse_attributes,
97};
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Dmitry Torokhov04df1922005-06-01 02:39:44 -050099/*
Ingo Molnarc14471d2006-02-19 00:22:11 -0500100 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500101 * (connecting, disconnecting, changing rate or resolution via
102 * sysfs). We could use a per-device semaphore but since there
103 * rarely more than one PS/2 mouse connected and since semaphore
104 * is taken in "slow" paths it is not worth it.
105 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500106static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500107
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500108static struct workqueue_struct *kpsmoused_wq;
109
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500110struct psmouse_protocol {
111 enum psmouse_type type;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700112 bool maxproto;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700113 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
Helge Dellere38de672006-09-10 21:54:39 -0400114 const char *name;
115 const char *alias;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700116 int (*detect)(struct psmouse *, bool);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500117 int (*init)(struct psmouse *);
118};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/*
121 * psmouse_process_byte() analyzes the PS/2 data stream and reports
122 * relevant events to the input module once full packet has arrived.
123 */
124
David Howells7d12e782006-10-05 14:55:46 +0100125static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500127 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned char *packet = psmouse->packet;
129
130 if (psmouse->pktcnt < psmouse->pktsize)
131 return PSMOUSE_GOOD_DATA;
132
133/*
134 * Full packet accumulated, process it
135 */
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
138 * Scroll wheel on IntelliMice, scroll buttons on NetMice
139 */
140
141 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
142 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
143
144/*
145 * Scroll wheel and buttons on IntelliMouse Explorer
146 */
147
148 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400149 switch (packet[3] & 0xC0) {
150 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
151 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
152 break;
153 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
154 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
155 break;
156 case 0x00:
157 case 0xC0:
158 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
159 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
160 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
161 break;
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164
165/*
166 * Extra buttons on Genius NewNet 3D
167 */
168
169 if (psmouse->type == PSMOUSE_GENPS) {
170 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
171 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
172 }
173
174/*
175 * Extra button on ThinkingMouse
176 */
177 if (psmouse->type == PSMOUSE_THINKPS) {
178 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
179 /* Without this bit of weirdness moving up gives wildly high Y changes. */
180 packet[1] |= (packet[0] & 0x40) << 1;
181 }
182
183/*
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400184 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
185 * byte.
186 */
187 if (psmouse->type == PSMOUSE_CORTRON) {
188 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
189 packet[0] |= 0x08;
190 }
191
192/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * Generic PS/2 Mouse
194 */
195
196 input_report_key(dev, BTN_LEFT, packet[0] & 1);
197 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
198 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
199
200 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
201 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
202
203 input_sync(dev);
204
205 return PSMOUSE_FULL_PACKET;
206}
207
Andres Salomon8bf020e2008-09-16 12:30:33 -0400208void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
209 unsigned long delay)
210{
211 queue_delayed_work(kpsmoused_wq, work, delay);
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500215 * __psmouse_set_state() sets new psmouse state and resets all flags.
216 */
217
218static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
219{
220 psmouse->state = new_state;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700221 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500222 psmouse->ps2dev.flags = 0;
223 psmouse->last = jiffies;
224}
225
226
227/*
228 * psmouse_set_state() sets new psmouse state and resets all flags and
229 * counters while holding serio lock so fighting with interrupt handler
230 * is not a concern.
231 */
232
Andres Salomona48cf5f2008-09-16 12:30:33 -0400233void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500234{
235 serio_pause_rx(psmouse->ps2dev.serio);
236 __psmouse_set_state(psmouse, new_state);
237 serio_continue_rx(psmouse->ps2dev.serio);
238}
239
240/*
241 * psmouse_handle_byte() processes one byte of the input data stream
242 * by calling corresponding protocol handler.
243 */
244
David Howells7d12e782006-10-05 14:55:46 +0100245static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500246{
David Howells7d12e782006-10-05 14:55:46 +0100247 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500248
249 switch (rc) {
250 case PSMOUSE_BAD_DATA:
251 if (psmouse->state == PSMOUSE_ACTIVATED) {
252 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
253 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700254 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500255 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
256 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
257 serio_reconnect(psmouse->ps2dev.serio);
258 return -1;
259 }
260 }
261 psmouse->pktcnt = 0;
262 break;
263
264 case PSMOUSE_FULL_PACKET:
265 psmouse->pktcnt = 0;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700266 if (psmouse->out_of_sync_cnt) {
267 psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500268 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
269 psmouse->name, psmouse->phys);
270 }
271 break;
272
273 case PSMOUSE_GOOD_DATA:
274 break;
275 }
276 return 0;
277}
278
279/*
280 * psmouse_interrupt() handles incoming characters, either passing them
281 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
283
284static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100285 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 if (psmouse->state == PSMOUSE_IGNORE)
290 goto out;
291
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700292 if (unlikely((flags & SERIO_TIMEOUT) ||
293 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (psmouse->state == PSMOUSE_ACTIVATED)
296 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
297 flags & SERIO_TIMEOUT ? " timeout" : "",
298 flags & SERIO_PARITY ? " bad parity" : "");
299 ps2_cmd_aborted(&psmouse->ps2dev);
300 goto out;
301 }
302
303 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
304 if (ps2_handle_ack(&psmouse->ps2dev, data))
305 goto out;
306
307 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
308 if (ps2_handle_response(&psmouse->ps2dev, data))
309 goto out;
310
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500311 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto out;
313
314 if (psmouse->state == PSMOUSE_ACTIVATED &&
315 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500316 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500318 psmouse->badbyte = psmouse->packet[0];
319 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400320 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500321 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500325/*
326 * Check if this is a new device announcement (0xAA 0x00)
327 */
328 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400329 if (psmouse->pktcnt == 1) {
330 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Zephaniah E. Hull535650f2009-05-14 22:02:33 -0700334 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
335 (psmouse->type == PSMOUSE_HGPK &&
336 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500337 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
338 serio_reconnect(serio);
339 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500341/*
342 * Not a new device, try processing first byte normally
343 */
344 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100345 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500346 goto out;
347
348 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500351/*
352 * See if we need to force resync because mouse was idle for too long
353 */
354 if (psmouse->state == PSMOUSE_ACTIVATED &&
355 psmouse->pktcnt == 1 && psmouse->resync_time &&
356 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
357 psmouse->badbyte = psmouse->packet[0];
358 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400359 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500360 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500362
363 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100364 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500365
366 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return IRQ_HANDLED;
368}
369
370
371/*
372 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
373 * using sliced syntax, understood by advanced devices, such as Logitech
374 * or Synaptics touchpads. The command is encoded as:
375 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
376 * is the command.
377 */
378int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
379{
380 int i;
381
382 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
383 return -1;
384
385 for (i = 6; i >= 0; i -= 2) {
386 unsigned char d = (command >> i) & 3;
387 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
388 return -1;
389 }
390
391 return 0;
392}
393
394
395/*
396 * psmouse_reset() resets the mouse into power-on state.
397 */
398int psmouse_reset(struct psmouse *psmouse)
399{
400 unsigned char param[2];
401
402 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
403 return -1;
404
405 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
406 return -1;
407
408 return 0;
409}
410
411
412/*
413 * Genius NetMouse magic init.
414 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700415static int genius_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct ps2dev *ps2dev = &psmouse->ps2dev;
418 unsigned char param[4];
419
420 param[0] = 3;
421 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
422 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
423 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
424 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
425 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
426
427 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
428 return -1;
429
430 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800431 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700432 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
433 __set_bit(BTN_SIDE, psmouse->dev->keybit);
434 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500437 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 psmouse->pktsize = 4;
439 }
440
441 return 0;
442}
443
444/*
445 * IntelliMouse magic init.
446 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700447static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct ps2dev *ps2dev = &psmouse->ps2dev;
450 unsigned char param[2];
451
452 param[0] = 200;
453 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
454 param[0] = 100;
455 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
456 param[0] = 80;
457 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
458 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
459
460 if (param[0] != 3)
461 return -1;
462
463 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700464 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
465 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800467 if (!psmouse->vendor)
468 psmouse->vendor = "Generic";
469 if (!psmouse->name)
470 psmouse->name = "Wheel Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 psmouse->pktsize = 4;
472 }
473
474 return 0;
475}
476
477/*
478 * Try IntelliMouse/Explorer magic init.
479 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700480static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct ps2dev *ps2dev = &psmouse->ps2dev;
483 unsigned char param[2];
484
485 intellimouse_detect(psmouse, 0);
486
487 param[0] = 200;
488 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
489 param[0] = 200;
490 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
491 param[0] = 80;
492 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
493 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
494
495 if (param[0] != 4)
496 return -1;
497
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400498/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
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 param[0] = 40;
504 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700507 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
508 __set_bit(REL_WHEEL, psmouse->dev->relbit);
509 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
510 __set_bit(BTN_SIDE, psmouse->dev->keybit);
511 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800513 if (!psmouse->vendor)
514 psmouse->vendor = "Generic";
515 if (!psmouse->name)
516 psmouse->name = "Explorer Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 psmouse->pktsize = 4;
518 }
519
520 return 0;
521}
522
523/*
524 * Kensington ThinkingMouse / ExpertMouse magic init.
525 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700526static int thinking_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct ps2dev *ps2dev = &psmouse->ps2dev;
529 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400530 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int i;
532
533 param[0] = 10;
534 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
535 param[0] = 0;
536 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400537 for (i = 0; i < ARRAY_SIZE(seq); i++) {
538 param[0] = seq[i];
539 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
542
543 if (param[0] != 2)
544 return -1;
545
546 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800547 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700548 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 psmouse->vendor = "Kensington";
551 psmouse->name = "ThinkingMouse";
552 }
553
554 return 0;
555}
556
557/*
558 * Bare PS/2 protocol "detection". Always succeeds.
559 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700560static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500562 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800563 if (!psmouse->vendor)
564 psmouse->vendor = "Generic";
565 if (!psmouse->name)
566 psmouse->name = "Mouse";
567
568/*
569 * We have no way of figuring true number of buttons so let's
570 * assume that the device has 3.
571 */
572 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 return 0;
576}
577
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400578/*
579 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
580 * must be forced by sysfs protocol writing.
581 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700582static int cortron_detect(struct psmouse *psmouse, bool set_properties)
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400583{
584 if (set_properties) {
585 psmouse->vendor = "Cortron";
586 psmouse->name = "PS/2 Trackball";
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800587
588 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700589 __set_bit(BTN_SIDE, psmouse->dev->keybit);
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400590 }
591
592 return 0;
593}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*
596 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
597 * the mouse may have.
598 */
599
600static int psmouse_extensions(struct psmouse *psmouse,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700601 unsigned int max_proto, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Jiri Kosina06989892009-11-16 22:12:13 -0800603 bool synaptics_hardware = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500605/*
606 * We always check for lifebook because it does not disturb mouse
607 * (it only checks DMI information).
608 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500609 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500610 if (max_proto > PSMOUSE_IMEX) {
611 if (!set_properties || lifebook_init(psmouse) == 0)
612 return PSMOUSE_LIFEBOOK;
613 }
614 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/*
617 * Try Kensington ThinkingMouse (we try first, because synaptics probe
618 * upsets the thinkingmouse).
619 */
620
621 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
622 return PSMOUSE_THINKPS;
623
624/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500625 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
626 * support is disabled in config - we need to know if it is synaptics so we
627 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
629 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700630 synaptics_hardware = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (max_proto > PSMOUSE_IMEX) {
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800633/*
634 * Try activating protocol, but check if support is enabled first, since
635 * we try detecting Synaptics even when protocol is disabled.
636 */
637 if (synaptics_supported() &&
638 (!set_properties || synaptics_init(psmouse) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return PSMOUSE_SYNAPTICS;
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800640 }
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*
643 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
644 * Unfortunately Logitech/Genius probes confuse some firmware versions so
645 * we'll have to skip them.
646 */
647 max_proto = PSMOUSE_IMEX;
648 }
649/*
650 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
651 */
652 synaptics_reset(psmouse);
653 }
654
655/*
656 * Try ALPS TouchPad
657 */
658 if (max_proto > PSMOUSE_IMEX) {
659 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
660 if (alps_detect(psmouse, set_properties) == 0) {
661 if (!set_properties || alps_init(psmouse) == 0)
662 return PSMOUSE_ALPS;
663/*
664 * Init failed, try basic relative protocols
665 */
666 max_proto = PSMOUSE_IMEX;
667 }
668 }
669
Andres Salomondf08ef22008-09-16 12:30:34 -0400670/*
671 * Try OLPC HGPK touchpad.
672 */
673 if (max_proto > PSMOUSE_IMEX &&
674 hgpk_detect(psmouse, set_properties) == 0) {
675 if (!set_properties || hgpk_init(psmouse) == 0)
676 return PSMOUSE_HGPK;
677/*
678 * Init failed, try basic relative protocols
679 */
680 max_proto = PSMOUSE_IMEX;
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400683/*
684 * Try Elantech touchpad.
685 */
686 if (max_proto > PSMOUSE_IMEX &&
687 elantech_detect(psmouse, set_properties) == 0) {
688 if (!set_properties || elantech_init(psmouse) == 0)
689 return PSMOUSE_ELANTECH;
690/*
691 * Init failed, try basic relative protocols
692 */
693 max_proto = PSMOUSE_IMEX;
694 }
695
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700696
Andres Salomondf08ef22008-09-16 12:30:34 -0400697 if (max_proto > PSMOUSE_IMEX) {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500698 if (genius_detect(psmouse, set_properties) == 0)
699 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500701 if (ps2pp_init(psmouse, set_properties) == 0)
702 return PSMOUSE_PS2PP;
703
704 if (trackpoint_detect(psmouse, set_properties) == 0)
705 return PSMOUSE_TRACKPOINT;
706
707 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
708 return PSMOUSE_TOUCHKIT_PS2;
709 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/*
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -0800712 * Try Finger Sensing Pad. We do it here because its probe upsets
713 * Trackpoint devices (causing TP_READ_ID command to time out).
714 */
715 if (max_proto > PSMOUSE_IMEX) {
716 if (fsp_detect(psmouse, set_properties) == 0) {
717 if (!set_properties || fsp_init(psmouse) == 0)
718 return PSMOUSE_FSP;
719/*
720 * Init failed, try basic relative protocols
721 */
722 max_proto = PSMOUSE_IMEX;
723 }
724 }
725
726/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400728 * protocol probes. Note that we follow up with full reset because
729 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Alon Ziv554fc192007-08-30 00:22:48 -0400731 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500732 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
735 return PSMOUSE_IMEX;
736
737 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
738 return PSMOUSE_IMPS;
739
740/*
741 * Okay, all failed, we have a standard mouse here. The number of the buttons
742 * is still a question, though. We assume 3.
743 */
744 ps2bare_detect(psmouse, set_properties);
745
746 if (synaptics_hardware) {
747/*
748 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
749 * We need to reset the touchpad because if there is a track point on the
750 * pass through port it could get disabled while probing for protocol
751 * extensions.
752 */
753 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
756 return PSMOUSE_PS2;
757}
758
Helge Dellere38de672006-09-10 21:54:39 -0400759static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500760 {
761 .type = PSMOUSE_PS2,
762 .name = "PS/2",
763 .alias = "bare",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700764 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700765 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500766 .detect = ps2bare_detect,
767 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500768#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500769 {
770 .type = PSMOUSE_PS2PP,
771 .name = "PS2++",
772 .alias = "logitech",
773 .detect = ps2pp_init,
774 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500775#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500776 {
777 .type = PSMOUSE_THINKPS,
778 .name = "ThinkPS/2",
779 .alias = "thinkps",
780 .detect = thinking_detect,
781 },
782 {
783 .type = PSMOUSE_GENPS,
784 .name = "GenPS/2",
785 .alias = "genius",
786 .detect = genius_detect,
787 },
788 {
789 .type = PSMOUSE_IMPS,
790 .name = "ImPS/2",
791 .alias = "imps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700792 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700793 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500794 .detect = intellimouse_detect,
795 },
796 {
797 .type = PSMOUSE_IMEX,
798 .name = "ImExPS/2",
799 .alias = "exps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700800 .maxproto = true,
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -0700801 .ignore_parity = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500802 .detect = im_explorer_detect,
803 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500804#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500805 {
806 .type = PSMOUSE_SYNAPTICS,
807 .name = "SynPS/2",
808 .alias = "synaptics",
809 .detect = synaptics_detect,
810 .init = synaptics_init,
811 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500812#endif
813#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500814 {
815 .type = PSMOUSE_ALPS,
816 .name = "AlpsPS/2",
817 .alias = "alps",
818 .detect = alps_detect,
819 .init = alps_init,
820 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500821#endif
822#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500823 {
824 .type = PSMOUSE_LIFEBOOK,
825 .name = "LBPS/2",
826 .alias = "lifebook",
827 .init = lifebook_init,
828 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500829#endif
830#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500831 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500832 .type = PSMOUSE_TRACKPOINT,
833 .name = "TPPS/2",
834 .alias = "trackpoint",
835 .detect = trackpoint_detect,
836 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500837#endif
838#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500839 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500840 .type = PSMOUSE_TOUCHKIT_PS2,
841 .name = "touchkitPS/2",
842 .alias = "touchkit",
843 .detect = touchkit_ps2_detect,
844 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500845#endif
Andres Salomondf08ef22008-09-16 12:30:34 -0400846#ifdef CONFIG_MOUSE_PS2_OLPC
847 {
848 .type = PSMOUSE_HGPK,
849 .name = "OLPC HGPK",
850 .alias = "hgpk",
851 .detect = hgpk_detect,
852 },
853#endif
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400854#ifdef CONFIG_MOUSE_PS2_ELANTECH
855 {
856 .type = PSMOUSE_ELANTECH,
857 .name = "ETPS/2",
858 .alias = "elantech",
859 .detect = elantech_detect,
860 .init = elantech_init,
861 },
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700862#endif
863#ifdef CONFIG_MOUSE_PS2_SENTELIC
864 {
865 .type = PSMOUSE_FSP,
866 .name = "FSPPS/2",
867 .alias = "fsp",
868 .detect = fsp_detect,
869 .init = fsp_init,
870 },
871#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500872 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400873 .type = PSMOUSE_CORTRON,
874 .name = "CortronPS/2",
875 .alias = "cortps",
876 .detect = cortron_detect,
877 },
878 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500879 .type = PSMOUSE_AUTO,
880 .name = "auto",
881 .alias = "any",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700882 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500883 },
884};
885
Helge Dellere38de672006-09-10 21:54:39 -0400886static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500887{
888 int i;
889
890 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
891 if (psmouse_protocols[i].type == type)
892 return &psmouse_protocols[i];
893
894 WARN_ON(1);
895 return &psmouse_protocols[0];
896}
897
Helge Dellere38de672006-09-10 21:54:39 -0400898static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500899{
Helge Dellere38de672006-09-10 21:54:39 -0400900 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500901 int i;
902
903 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
904 p = &psmouse_protocols[i];
905
906 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
907 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
908 return &psmouse_protocols[i];
909 }
910
911 return NULL;
912}
913
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/*
916 * psmouse_probe() probes for a PS/2 mouse.
917 */
918
919static int psmouse_probe(struct psmouse *psmouse)
920{
921 struct ps2dev *ps2dev = &psmouse->ps2dev;
922 unsigned char param[2];
923
924/*
925 * First, we check if it's a mouse. It should send 0x00 or 0x03
926 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500927 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
928 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 */
930
931 param[0] = 0xa5;
932 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
933 return -1;
934
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500935 if (param[0] != 0x00 && param[0] != 0x03 &&
936 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -1;
938
939/*
940 * Then we reset and disable the mouse so that it doesn't generate events.
941 */
942
943 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
944 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
945
946 return 0;
947}
948
949/*
950 * Here we set the mouse resolution.
951 */
952
953void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
954{
Helge Dellere38de672006-09-10 21:54:39 -0400955 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
956 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (resolution == 0 || resolution > 200)
959 resolution = 200;
960
Helge Dellere38de672006-09-10 21:54:39 -0400961 p = params[resolution / 50];
962 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
963 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/*
967 * Here we set the mouse report rate.
968 */
969
970static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
971{
Helge Dellere38de672006-09-10 21:54:39 -0400972 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
973 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int i = 0;
975
976 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400977 r = rates[i];
978 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
979 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982/*
983 * psmouse_initialize() initializes the mouse to a sane state.
984 */
985
986static void psmouse_initialize(struct psmouse *psmouse)
987{
988/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * We set the mouse report rate, resolution and scaling.
990 */
991
992 if (psmouse_max_proto != PSMOUSE_PS2) {
993 psmouse->set_rate(psmouse, psmouse->rate);
994 psmouse->set_resolution(psmouse, psmouse->resolution);
995 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
996 }
997}
998
999/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 * psmouse_activate() enables the mouse so that we get motion reports from it.
1001 */
1002
1003static void psmouse_activate(struct psmouse *psmouse)
1004{
1005 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1006 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
1007 psmouse->ps2dev.serio->phys);
1008
1009 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1010}
1011
1012
1013/*
1014 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +02001015 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 */
1017
1018static void psmouse_deactivate(struct psmouse *psmouse)
1019{
1020 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1021 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
1022 psmouse->ps2dev.serio->phys);
1023
1024 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1025}
1026
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001027/*
1028 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
1029 */
1030
1031static int psmouse_poll(struct psmouse *psmouse)
1032{
1033 return ps2_command(&psmouse->ps2dev, psmouse->packet,
1034 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
1035}
1036
1037
1038/*
1039 * psmouse_resync() attempts to re-validate current protocol.
1040 */
1041
David Howellsc4028952006-11-22 14:57:56 +00001042static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001043{
David Howellsc4028952006-11-22 14:57:56 +00001044 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -04001045 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001046 struct serio *serio = psmouse->ps2dev.serio;
1047 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001048 bool failed = false, enabled = false;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001049 int i;
1050
Ingo Molnarc14471d2006-02-19 00:22:11 -05001051 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001052
1053 if (psmouse->state != PSMOUSE_RESYNCING)
1054 goto out;
1055
1056 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1057 parent = serio_get_drvdata(serio->parent);
1058 psmouse_deactivate(parent);
1059 }
1060
1061/*
1062 * Some mice don't ACK commands sent while they are in the middle of
1063 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1064 * instead of ps2_command() which would wait for 200ms for an ACK
1065 * that may never come.
1066 * As an additional quirk ALPS touchpads may not only forget to ACK
1067 * disable command but will stop reporting taps, so if we see that
1068 * mouse at least once ACKs disable we will do full reconnect if ACK
1069 * is missing.
1070 */
1071 psmouse->num_resyncs++;
1072
1073 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1074 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001075 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001076 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001077 psmouse->acks_disable_command = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001078
1079/*
1080 * Poll the mouse. If it was reset the packet will be shorter than
1081 * psmouse->pktsize and ps2_command will fail. We do not expect and
1082 * do not handle scenario when mouse "upgrades" its protocol while
1083 * disconnected since it would require additional delay. If we ever
1084 * see a mouse that does it we'll adjust the code.
1085 */
1086 if (!failed) {
1087 if (psmouse->poll(psmouse))
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001088 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001089 else {
1090 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1091 for (i = 0; i < psmouse->pktsize; i++) {
1092 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001093 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001094 if (rc != PSMOUSE_GOOD_DATA)
1095 break;
1096 }
1097 if (rc != PSMOUSE_FULL_PACKET)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001098 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001099 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1100 }
1101 }
1102/*
1103 * Now try to enable mouse. We try to do that even if poll failed and also
1104 * repeat our attempts 5 times, otherwise we may be left out with disabled
1105 * mouse.
1106 */
1107 for (i = 0; i < 5; i++) {
1108 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001109 enabled = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001110 break;
1111 }
1112 msleep(200);
1113 }
1114
1115 if (!enabled) {
1116 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1117 psmouse->ps2dev.serio->phys);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001118 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001119 }
1120
1121 if (failed) {
1122 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1123 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1124 serio_reconnect(serio);
1125 } else
1126 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1127
1128 if (parent)
1129 psmouse_activate(parent);
1130 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001131 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134/*
1135 * psmouse_cleanup() resets the mouse into power-on state.
1136 */
1137
1138static void psmouse_cleanup(struct serio *serio)
1139{
1140 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001141 struct psmouse *parent = NULL;
1142
1143 mutex_lock(&psmouse_mutex);
1144
1145 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1146 parent = serio_get_drvdata(serio->parent);
1147 psmouse_deactivate(parent);
1148 }
1149
Dmitry Torokhova9f0c382010-02-07 23:10:04 -08001150 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1151
1152 /*
1153 * Disable stream mode so cleanup routine can proceed undisturbed.
1154 */
1155 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1156 printk(KERN_WARNING "psmouse.c: Failed to disable mouse on %s\n",
1157 psmouse->ps2dev.serio->phys);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001158
1159 if (psmouse->cleanup)
1160 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Dmitry Torokhov4a299bf2009-12-24 21:40:24 -08001162/*
1163 * Reset the mouse to defaults (bare PS/2 protocol).
1164 */
1165 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001166
1167/*
1168 * Some boxes, such as HP nx7400, get terribly confused if mouse
1169 * is not fully enabled before suspending/shutting down.
1170 */
1171 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1172
1173 if (parent) {
1174 if (parent->pt_deactivate)
1175 parent->pt_deactivate(parent);
1176
1177 psmouse_activate(parent);
1178 }
1179
1180 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183/*
1184 * psmouse_disconnect() closes and frees.
1185 */
1186
1187static void psmouse_disconnect(struct serio *serio)
1188{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001189 struct psmouse *psmouse, *parent = NULL;
1190
1191 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001193 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Ingo Molnarc14471d2006-02-19 00:22:11 -05001195 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1198
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001199 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001200 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001201 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001202 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1205 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001206 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
1209 if (psmouse->disconnect)
1210 psmouse->disconnect(psmouse);
1211
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001212 if (parent && parent->pt_deactivate)
1213 parent->pt_deactivate(parent);
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 serio_close(serio);
1218 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001219 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001221
1222 if (parent)
1223 psmouse_activate(parent);
1224
Ingo Molnarc14471d2006-02-19 00:22:11 -05001225 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001228static int psmouse_switch_protocol(struct psmouse *psmouse,
1229 const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001230{
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001231 const struct psmouse_protocol *selected_proto;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001232 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001233
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001234 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001235
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001236 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001237 input_dev->keybit[BIT_WORD(BTN_MOUSE)] =
1238 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001239 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001240
1241 psmouse->set_rate = psmouse_set_rate;
1242 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001243 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001244 psmouse->protocol_handler = psmouse_process_byte;
1245 psmouse->pktsize = 3;
1246
1247 if (proto && (proto->detect || proto->init)) {
1248 if (proto->detect && proto->detect(psmouse, 1) < 0)
1249 return -1;
1250
1251 if (proto->init && proto->init(psmouse) < 0)
1252 return -1;
1253
1254 psmouse->type = proto->type;
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001255 selected_proto = proto;
1256 } else {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001257 psmouse->type = psmouse_extensions(psmouse,
1258 psmouse_max_proto, true);
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001259 selected_proto = psmouse_protocol_by_type(psmouse->type);
1260 }
1261
1262 psmouse->ignore_parity = selected_proto->ignore_parity;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001263
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001264 /*
1265 * If mouse's packet size is 3 there is no point in polling the
1266 * device in hopes to detect protocol reset - we won't get less
1267 * than 3 bytes response anyhow.
1268 */
1269 if (psmouse->pktsize == 3)
1270 psmouse->resync_time = 0;
1271
1272 /*
1273 * Some smart KVMs fake response to POLL command returning just
1274 * 3 bytes and messing up our resync logic, so if initial poll
1275 * fails we won't try polling the device anymore. Hopefully
1276 * such KVM will maintain initially selected protocol.
1277 */
1278 if (psmouse->resync_time && psmouse->poll(psmouse))
1279 psmouse->resync_time = 0;
1280
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001281 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
Dmitry Torokhov6b9d363c2010-04-19 00:42:16 -07001282 selected_proto->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001283
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001284 input_dev->name = psmouse->devname;
1285 input_dev->phys = psmouse->phys;
1286 input_dev->id.bustype = BUS_I8042;
1287 input_dev->id.vendor = 0x0002;
1288 input_dev->id.product = psmouse->type;
1289 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001290
1291 return 0;
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/*
1295 * psmouse_connect() is a callback from the serio module when
1296 * an unhandled serio port is found.
1297 */
1298static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1299{
1300 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001301 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001302 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Ingo Molnarc14471d2006-02-19 00:22:11 -05001304 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /*
1307 * If this is a pass-through port deactivate parent so the device
1308 * connected to this port can be successfully identified
1309 */
1310 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1311 parent = serio_get_drvdata(serio->parent);
1312 psmouse_deactivate(parent);
1313 }
1314
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001315 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1316 input_dev = input_allocate_device();
1317 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001318 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001321 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001322 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001323 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1326
1327 serio_set_drvdata(serio, psmouse);
1328
Dmitry Torokhov72155612006-11-05 22:40:19 -05001329 error = serio_open(serio, drv);
1330 if (error)
1331 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001334 error = -ENODEV;
1335 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337
1338 psmouse->rate = psmouse_rate;
1339 psmouse->resolution = psmouse_resolution;
1340 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001341 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001344 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 psmouse_initialize(psmouse);
1348
Dmitry Torokhov72155612006-11-05 22:40:19 -05001349 error = input_register_device(psmouse->dev);
1350 if (error)
1351 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (parent && parent->pt_activate)
1354 parent->pt_activate(parent);
1355
Dmitry Torokhov72155612006-11-05 22:40:19 -05001356 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1357 if (error)
1358 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 psmouse_activate(psmouse);
1361
Dmitry Torokhov72155612006-11-05 22:40:19 -05001362 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001363 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (parent)
1365 psmouse_activate(parent);
1366
Ingo Molnarc14471d2006-02-19 00:22:11 -05001367 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001369
1370 err_pt_deactivate:
1371 if (parent && parent->pt_deactivate)
1372 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001373 input_unregister_device(psmouse->dev);
1374 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001375 err_protocol_disconnect:
1376 if (psmouse->disconnect)
1377 psmouse->disconnect(psmouse);
1378 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1379 err_close_serio:
1380 serio_close(serio);
1381 err_clear_drvdata:
1382 serio_set_drvdata(serio, NULL);
1383 err_free:
1384 input_free_device(input_dev);
1385 kfree(psmouse);
1386
1387 retval = error;
1388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391
1392static int psmouse_reconnect(struct serio *serio)
1393{
1394 struct psmouse *psmouse = serio_get_drvdata(serio);
1395 struct psmouse *parent = NULL;
1396 struct serio_driver *drv = serio->drv;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001397 unsigned char type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int rc = -1;
1399
1400 if (!drv || !psmouse) {
1401 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1402 return -1;
1403 }
1404
Ingo Molnarc14471d2006-02-19 00:22:11 -05001405 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1408 parent = serio_get_drvdata(serio->parent);
1409 psmouse_deactivate(parent);
1410 }
1411
1412 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1413
1414 if (psmouse->reconnect) {
1415 if (psmouse->reconnect(psmouse))
1416 goto out;
Dmitry Torokhovef110b22010-05-13 00:42:23 -07001417 } else {
1418 psmouse_reset(psmouse);
1419
1420 if (psmouse_probe(psmouse) < 0)
1421 goto out;
1422
1423 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1424 if (psmouse->type != type)
1425 goto out;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 /* ok, the device type (and capabilities) match the old one,
1429 * we can continue using it, complete intialization
1430 */
1431 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1432
1433 psmouse_initialize(psmouse);
1434
1435 if (parent && parent->pt_activate)
1436 parent->pt_activate(parent);
1437
1438 psmouse_activate(psmouse);
1439 rc = 0;
1440
1441out:
1442 /* If this is a pass-through port the parent waits to be activated */
1443 if (parent)
1444 psmouse_activate(parent);
1445
Ingo Molnarc14471d2006-02-19 00:22:11 -05001446 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return rc;
1448}
1449
1450static struct serio_device_id psmouse_serio_ids[] = {
1451 {
1452 .type = SERIO_8042,
1453 .proto = SERIO_ANY,
1454 .id = SERIO_ANY,
1455 .extra = SERIO_ANY,
1456 },
1457 {
1458 .type = SERIO_PS_PSTHRU,
1459 .proto = SERIO_ANY,
1460 .id = SERIO_ANY,
1461 .extra = SERIO_ANY,
1462 },
1463 { 0 }
1464};
1465
1466MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1467
1468static struct serio_driver psmouse_drv = {
1469 .driver = {
1470 .name = "psmouse",
1471 },
1472 .description = DRIVER_DESC,
1473 .id_table = psmouse_serio_ids,
1474 .interrupt = psmouse_interrupt,
1475 .connect = psmouse_connect,
1476 .reconnect = psmouse_reconnect,
1477 .disconnect = psmouse_disconnect,
1478 .cleanup = psmouse_cleanup,
1479};
1480
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001481ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1482 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001485 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1486 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001488 psmouse = serio_get_drvdata(serio);
1489
Eric W. Biederman59b01512010-01-05 17:56:02 -08001490 return attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001493ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1494 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001497 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1498 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int retval;
1500
Ingo Molnarc14471d2006-02-19 00:22:11 -05001501 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001502 if (retval)
Eric W. Biederman59b01512010-01-05 17:56:02 -08001503 goto out;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001504
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001505 psmouse = serio_get_drvdata(serio);
1506
Andres Salomon68d48222008-09-16 12:30:34 -04001507 if (attr->protect) {
1508 if (psmouse->state == PSMOUSE_IGNORE) {
1509 retval = -ENODEV;
1510 goto out_unlock;
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Andres Salomon68d48222008-09-16 12:30:34 -04001513 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1514 parent = serio_get_drvdata(serio->parent);
1515 psmouse_deactivate(parent);
1516 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001517
Andres Salomon68d48222008-09-16 12:30:34 -04001518 psmouse_deactivate(psmouse);
1519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001521 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Andres Salomon68d48222008-09-16 12:30:34 -04001523 if (attr->protect) {
1524 if (retval != -ENODEV)
1525 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001526
Andres Salomon68d48222008-09-16 12:30:34 -04001527 if (parent)
1528 psmouse_activate(parent);
1529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Ingo Molnarc14471d2006-02-19 00:22:11 -05001531 out_unlock:
1532 mutex_unlock(&psmouse_mutex);
Eric W. Biederman59b01512010-01-05 17:56:02 -08001533 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return retval;
1535}
1536
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001537static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1538{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001539 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001540
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001541 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001542}
1543
1544static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1545{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001546 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001547 unsigned long value;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001548
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001549 if (strict_strtoul(buf, 10, &value))
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001550 return -EINVAL;
1551
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001552 if ((unsigned int)value != value)
1553 return -EINVAL;
1554
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001555 *field = value;
1556
1557 return count;
1558}
1559
1560static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001561{
1562 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1563}
1564
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001565static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001566{
1567 struct serio *serio = psmouse->ps2dev.serio;
1568 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001569 struct input_dev *old_dev, *new_dev;
1570 const struct psmouse_protocol *proto, *old_proto;
1571 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001572 int retry = 0;
1573
Dmitry Torokhov72155612006-11-05 22:40:19 -05001574 proto = psmouse_protocol_by_name(buf, count);
1575 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001576 return -EINVAL;
1577
1578 if (psmouse->type == proto->type)
1579 return count;
1580
Dmitry Torokhov72155612006-11-05 22:40:19 -05001581 new_dev = input_allocate_device();
1582 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001583 return -ENOMEM;
1584
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001585 while (serio->child) {
1586 if (++retry > 3) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001587 printk(KERN_WARNING
1588 "psmouse: failed to destroy child port, "
1589 "protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001590 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001591 return -EIO;
1592 }
1593
Ingo Molnarc14471d2006-02-19 00:22:11 -05001594 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001595 serio_unregister_child_port(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001596 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001597
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001598 if (serio->drv != &psmouse_drv) {
1599 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001600 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001601 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001602
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001603 if (psmouse->type == proto->type) {
1604 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001605 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001606 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001607 }
1608
1609 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1610 parent = serio_get_drvdata(serio->parent);
1611 if (parent->pt_deactivate)
1612 parent->pt_deactivate(parent);
1613 }
1614
Dmitry Torokhov72155612006-11-05 22:40:19 -05001615 old_dev = psmouse->dev;
1616 old_proto = psmouse_protocol_by_type(psmouse->type);
1617
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001618 if (psmouse->disconnect)
1619 psmouse->disconnect(psmouse);
1620
1621 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001622
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001623 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001624 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1625
1626 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1627 psmouse_reset(psmouse);
1628 /* default to PSMOUSE_PS2 */
1629 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1630 }
1631
1632 psmouse_initialize(psmouse);
1633 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1634
Dmitry Torokhov72155612006-11-05 22:40:19 -05001635 error = input_register_device(psmouse->dev);
1636 if (error) {
1637 if (psmouse->disconnect)
1638 psmouse->disconnect(psmouse);
1639
1640 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1641 input_free_device(new_dev);
1642 psmouse->dev = old_dev;
1643 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1644 psmouse_switch_protocol(psmouse, old_proto);
1645 psmouse_initialize(psmouse);
1646 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1647
1648 return error;
1649 }
1650
1651 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001652
1653 if (parent && parent->pt_activate)
1654 parent->pt_activate(parent);
1655
1656 return count;
1657}
1658
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001659static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
1661 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001663 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return -EINVAL;
1665
1666 psmouse->set_rate(psmouse, value);
1667 return count;
1668}
1669
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001670static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001674 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return -EINVAL;
1676
1677 psmouse->set_resolution(psmouse, value);
1678 return count;
1679}
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1683{
Helge Dellere38de672006-09-10 21:54:39 -04001684 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 if (!val)
1687 return -EINVAL;
1688
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001689 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001691 if (!proto || !proto->maxproto)
1692 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001694 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Stephen Evanchik541e3162005-08-08 01:26:18 -05001696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1700{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001701 int type = *((unsigned int *)kp->arg);
1702
Takashi Iwai3d4c3aa2009-11-12 23:30:52 -08001703 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706static int __init psmouse_init(void)
1707{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001708 int err;
1709
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001710 lifebook_module_init();
1711 synaptics_module_init();
1712
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001713 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1714 if (!kpsmoused_wq) {
1715 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1716 return -ENOMEM;
1717 }
1718
Akinobu Mita153a9df02006-11-23 23:35:10 -05001719 err = serio_register_driver(&psmouse_drv);
1720 if (err)
1721 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001722
Akinobu Mita153a9df02006-11-23 23:35:10 -05001723 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726static void __exit psmouse_exit(void)
1727{
1728 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001729 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
1732module_init(psmouse_init);
1733module_exit(psmouse_exit);