blob: d8c0c8d6992c92936db326dc28920350b899272d [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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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;
Helge Dellere38de672006-09-10 21:54:39 -0400113 const char *name;
114 const char *alias;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700115 int (*detect)(struct psmouse *, bool);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500116 int (*init)(struct psmouse *);
117};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119/*
120 * psmouse_process_byte() analyzes the PS/2 data stream and reports
121 * relevant events to the input module once full packet has arrived.
122 */
123
David Howells7d12e782006-10-05 14:55:46 +0100124static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500126 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 unsigned char *packet = psmouse->packet;
128
129 if (psmouse->pktcnt < psmouse->pktsize)
130 return PSMOUSE_GOOD_DATA;
131
132/*
133 * Full packet accumulated, process it
134 */
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
137 * Scroll wheel on IntelliMice, scroll buttons on NetMice
138 */
139
140 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
141 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
142
143/*
144 * Scroll wheel and buttons on IntelliMouse Explorer
145 */
146
147 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400148 switch (packet[3] & 0xC0) {
149 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
150 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
151 break;
152 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
153 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
154 break;
155 case 0x00:
156 case 0xC0:
157 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
158 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
159 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
160 break;
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163
164/*
165 * Extra buttons on Genius NewNet 3D
166 */
167
168 if (psmouse->type == PSMOUSE_GENPS) {
169 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
170 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
171 }
172
173/*
174 * Extra button on ThinkingMouse
175 */
176 if (psmouse->type == PSMOUSE_THINKPS) {
177 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
178 /* Without this bit of weirdness moving up gives wildly high Y changes. */
179 packet[1] |= (packet[0] & 0x40) << 1;
180 }
181
182/*
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400183 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
184 * byte.
185 */
186 if (psmouse->type == PSMOUSE_CORTRON) {
187 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
188 packet[0] |= 0x08;
189 }
190
191/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * Generic PS/2 Mouse
193 */
194
195 input_report_key(dev, BTN_LEFT, packet[0] & 1);
196 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
197 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
198
199 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
200 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
201
202 input_sync(dev);
203
204 return PSMOUSE_FULL_PACKET;
205}
206
Andres Salomon8bf020e2008-09-16 12:30:33 -0400207void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
208 unsigned long delay)
209{
210 queue_delayed_work(kpsmoused_wq, work, delay);
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500214 * __psmouse_set_state() sets new psmouse state and resets all flags.
215 */
216
217static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
218{
219 psmouse->state = new_state;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700220 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500221 psmouse->ps2dev.flags = 0;
222 psmouse->last = jiffies;
223}
224
225
226/*
227 * psmouse_set_state() sets new psmouse state and resets all flags and
228 * counters while holding serio lock so fighting with interrupt handler
229 * is not a concern.
230 */
231
Andres Salomona48cf5f2008-09-16 12:30:33 -0400232void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500233{
234 serio_pause_rx(psmouse->ps2dev.serio);
235 __psmouse_set_state(psmouse, new_state);
236 serio_continue_rx(psmouse->ps2dev.serio);
237}
238
239/*
240 * psmouse_handle_byte() processes one byte of the input data stream
241 * by calling corresponding protocol handler.
242 */
243
David Howells7d12e782006-10-05 14:55:46 +0100244static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500245{
David Howells7d12e782006-10-05 14:55:46 +0100246 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500247
248 switch (rc) {
249 case PSMOUSE_BAD_DATA:
250 if (psmouse->state == PSMOUSE_ACTIVATED) {
251 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
252 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700253 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500254 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
255 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
256 serio_reconnect(psmouse->ps2dev.serio);
257 return -1;
258 }
259 }
260 psmouse->pktcnt = 0;
261 break;
262
263 case PSMOUSE_FULL_PACKET:
264 psmouse->pktcnt = 0;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700265 if (psmouse->out_of_sync_cnt) {
266 psmouse->out_of_sync_cnt = 0;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500267 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
268 psmouse->name, psmouse->phys);
269 }
270 break;
271
272 case PSMOUSE_GOOD_DATA:
273 break;
274 }
275 return 0;
276}
277
278/*
279 * psmouse_interrupt() handles incoming characters, either passing them
280 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
282
283static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100284 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (psmouse->state == PSMOUSE_IGNORE)
289 goto out;
290
291 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
292 if (psmouse->state == PSMOUSE_ACTIVATED)
293 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
294 flags & SERIO_TIMEOUT ? " timeout" : "",
295 flags & SERIO_PARITY ? " bad parity" : "");
296 ps2_cmd_aborted(&psmouse->ps2dev);
297 goto out;
298 }
299
300 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
301 if (ps2_handle_ack(&psmouse->ps2dev, data))
302 goto out;
303
304 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
305 if (ps2_handle_response(&psmouse->ps2dev, data))
306 goto out;
307
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500308 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto out;
310
311 if (psmouse->state == PSMOUSE_ACTIVATED &&
312 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500313 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500315 psmouse->badbyte = psmouse->packet[0];
316 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400317 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500318 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500322/*
323 * Check if this is a new device announcement (0xAA 0x00)
324 */
325 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400326 if (psmouse->pktcnt == 1) {
327 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Zephaniah E. Hull535650f2009-05-14 22:02:33 -0700331 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
332 (psmouse->type == PSMOUSE_HGPK &&
333 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500334 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
335 serio_reconnect(serio);
336 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500338/*
339 * Not a new device, try processing first byte normally
340 */
341 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100342 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500343 goto out;
344
345 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500348/*
349 * See if we need to force resync because mouse was idle for too long
350 */
351 if (psmouse->state == PSMOUSE_ACTIVATED &&
352 psmouse->pktcnt == 1 && psmouse->resync_time &&
353 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
354 psmouse->badbyte = psmouse->packet[0];
355 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400356 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500357 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500359
360 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100361 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500362
363 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return IRQ_HANDLED;
365}
366
367
368/*
369 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
370 * using sliced syntax, understood by advanced devices, such as Logitech
371 * or Synaptics touchpads. The command is encoded as:
372 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
373 * is the command.
374 */
375int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
376{
377 int i;
378
379 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
380 return -1;
381
382 for (i = 6; i >= 0; i -= 2) {
383 unsigned char d = (command >> i) & 3;
384 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
385 return -1;
386 }
387
388 return 0;
389}
390
391
392/*
393 * psmouse_reset() resets the mouse into power-on state.
394 */
395int psmouse_reset(struct psmouse *psmouse)
396{
397 unsigned char param[2];
398
399 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
400 return -1;
401
402 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
403 return -1;
404
405 return 0;
406}
407
408
409/*
410 * Genius NetMouse magic init.
411 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700412static int genius_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct ps2dev *ps2dev = &psmouse->ps2dev;
415 unsigned char param[4];
416
417 param[0] = 3;
418 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
419 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
420 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
421 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
422 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
423
424 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
425 return -1;
426
427 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800428 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700429 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
430 __set_bit(BTN_SIDE, psmouse->dev->keybit);
431 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500434 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 psmouse->pktsize = 4;
436 }
437
438 return 0;
439}
440
441/*
442 * IntelliMouse magic init.
443 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700444static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct ps2dev *ps2dev = &psmouse->ps2dev;
447 unsigned char param[2];
448
449 param[0] = 200;
450 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
451 param[0] = 100;
452 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
453 param[0] = 80;
454 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
455 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
456
457 if (param[0] != 3)
458 return -1;
459
460 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700461 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
462 __set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800464 if (!psmouse->vendor)
465 psmouse->vendor = "Generic";
466 if (!psmouse->name)
467 psmouse->name = "Wheel Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 psmouse->pktsize = 4;
469 }
470
471 return 0;
472}
473
474/*
475 * Try IntelliMouse/Explorer magic init.
476 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700477static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct ps2dev *ps2dev = &psmouse->ps2dev;
480 unsigned char param[2];
481
482 intellimouse_detect(psmouse, 0);
483
484 param[0] = 200;
485 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
486 param[0] = 200;
487 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
488 param[0] = 80;
489 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
490 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
491
492 if (param[0] != 4)
493 return -1;
494
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400495/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
496 param[0] = 200;
497 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
498 param[0] = 80;
499 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
500 param[0] = 40;
501 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (set_properties) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700504 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
505 __set_bit(REL_WHEEL, psmouse->dev->relbit);
506 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
507 __set_bit(BTN_SIDE, psmouse->dev->keybit);
508 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800510 if (!psmouse->vendor)
511 psmouse->vendor = "Generic";
512 if (!psmouse->name)
513 psmouse->name = "Explorer Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 psmouse->pktsize = 4;
515 }
516
517 return 0;
518}
519
520/*
521 * Kensington ThinkingMouse / ExpertMouse magic init.
522 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700523static int thinking_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct ps2dev *ps2dev = &psmouse->ps2dev;
526 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400527 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int i;
529
530 param[0] = 10;
531 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
532 param[0] = 0;
533 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400534 for (i = 0; i < ARRAY_SIZE(seq); i++) {
535 param[0] = seq[i];
536 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
539
540 if (param[0] != 2)
541 return -1;
542
543 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800544 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700545 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 psmouse->vendor = "Kensington";
548 psmouse->name = "ThinkingMouse";
549 }
550
551 return 0;
552}
553
554/*
555 * Bare PS/2 protocol "detection". Always succeeds.
556 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700557static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500559 if (set_properties) {
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800560 if (!psmouse->vendor)
561 psmouse->vendor = "Generic";
562 if (!psmouse->name)
563 psmouse->name = "Mouse";
564
565/*
566 * We have no way of figuring true number of buttons so let's
567 * assume that the device has 3.
568 */
569 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 return 0;
573}
574
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400575/*
576 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
577 * must be forced by sysfs protocol writing.
578 */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700579static int cortron_detect(struct psmouse *psmouse, bool set_properties)
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400580{
581 if (set_properties) {
582 psmouse->vendor = "Cortron";
583 psmouse->name = "PS/2 Trackball";
Dmitry Torokhov315eb992009-11-16 22:12:21 -0800584
585 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700586 __set_bit(BTN_SIDE, psmouse->dev->keybit);
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400587 }
588
589 return 0;
590}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/*
593 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
594 * the mouse may have.
595 */
596
597static int psmouse_extensions(struct psmouse *psmouse,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700598 unsigned int max_proto, bool set_properties)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Jiri Kosina06989892009-11-16 22:12:13 -0800600 bool synaptics_hardware = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500602/*
603 * We always check for lifebook because it does not disturb mouse
604 * (it only checks DMI information).
605 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500606 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500607 if (max_proto > PSMOUSE_IMEX) {
608 if (!set_properties || lifebook_init(psmouse) == 0)
609 return PSMOUSE_LIFEBOOK;
610 }
611 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/*
614 * Try Kensington ThinkingMouse (we try first, because synaptics probe
615 * upsets the thinkingmouse).
616 */
617
618 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
619 return PSMOUSE_THINKPS;
620
621/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500622 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
623 * support is disabled in config - we need to know if it is synaptics so we
624 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
626 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700627 synaptics_hardware = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (max_proto > PSMOUSE_IMEX) {
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800630/*
631 * Try activating protocol, but check if support is enabled first, since
632 * we try detecting Synaptics even when protocol is disabled.
633 */
634 if (synaptics_supported() &&
635 (!set_properties || synaptics_init(psmouse) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return PSMOUSE_SYNAPTICS;
Daniel Drakee4e6efd2010-01-07 01:52:39 -0800637 }
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/*
640 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
641 * Unfortunately Logitech/Genius probes confuse some firmware versions so
642 * we'll have to skip them.
643 */
644 max_proto = PSMOUSE_IMEX;
645 }
646/*
647 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
648 */
649 synaptics_reset(psmouse);
650 }
651
652/*
653 * Try ALPS TouchPad
654 */
655 if (max_proto > PSMOUSE_IMEX) {
656 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
657 if (alps_detect(psmouse, set_properties) == 0) {
658 if (!set_properties || alps_init(psmouse) == 0)
659 return PSMOUSE_ALPS;
660/*
661 * Init failed, try basic relative protocols
662 */
663 max_proto = PSMOUSE_IMEX;
664 }
665 }
666
Andres Salomondf08ef22008-09-16 12:30:34 -0400667/*
668 * Try OLPC HGPK touchpad.
669 */
670 if (max_proto > PSMOUSE_IMEX &&
671 hgpk_detect(psmouse, set_properties) == 0) {
672 if (!set_properties || hgpk_init(psmouse) == 0)
673 return PSMOUSE_HGPK;
674/*
675 * Init failed, try basic relative protocols
676 */
677 max_proto = PSMOUSE_IMEX;
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400680/*
681 * Try Elantech touchpad.
682 */
683 if (max_proto > PSMOUSE_IMEX &&
684 elantech_detect(psmouse, set_properties) == 0) {
685 if (!set_properties || elantech_init(psmouse) == 0)
686 return PSMOUSE_ELANTECH;
687/*
688 * Init failed, try basic relative protocols
689 */
690 max_proto = PSMOUSE_IMEX;
691 }
692
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700693
Andres Salomondf08ef22008-09-16 12:30:34 -0400694 if (max_proto > PSMOUSE_IMEX) {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500695 if (genius_detect(psmouse, set_properties) == 0)
696 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500698 if (ps2pp_init(psmouse, set_properties) == 0)
699 return PSMOUSE_PS2PP;
700
701 if (trackpoint_detect(psmouse, set_properties) == 0)
702 return PSMOUSE_TRACKPOINT;
703
704 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
705 return PSMOUSE_TOUCHKIT_PS2;
706 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/*
Tai-hwa Liang4a18b3a2010-01-13 00:16:27 -0800709 * Try Finger Sensing Pad. We do it here because its probe upsets
710 * Trackpoint devices (causing TP_READ_ID command to time out).
711 */
712 if (max_proto > PSMOUSE_IMEX) {
713 if (fsp_detect(psmouse, set_properties) == 0) {
714 if (!set_properties || fsp_init(psmouse) == 0)
715 return PSMOUSE_FSP;
716/*
717 * Init failed, try basic relative protocols
718 */
719 max_proto = PSMOUSE_IMEX;
720 }
721 }
722
723/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400725 * protocol probes. Note that we follow up with full reset because
726 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
Alon Ziv554fc192007-08-30 00:22:48 -0400728 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500729 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
732 return PSMOUSE_IMEX;
733
734 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
735 return PSMOUSE_IMPS;
736
737/*
738 * Okay, all failed, we have a standard mouse here. The number of the buttons
739 * is still a question, though. We assume 3.
740 */
741 ps2bare_detect(psmouse, set_properties);
742
743 if (synaptics_hardware) {
744/*
745 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
746 * We need to reset the touchpad because if there is a track point on the
747 * pass through port it could get disabled while probing for protocol
748 * extensions.
749 */
750 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
753 return PSMOUSE_PS2;
754}
755
Helge Dellere38de672006-09-10 21:54:39 -0400756static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500757 {
758 .type = PSMOUSE_PS2,
759 .name = "PS/2",
760 .alias = "bare",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700761 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500762 .detect = ps2bare_detect,
763 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500764#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500765 {
766 .type = PSMOUSE_PS2PP,
767 .name = "PS2++",
768 .alias = "logitech",
769 .detect = ps2pp_init,
770 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500771#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500772 {
773 .type = PSMOUSE_THINKPS,
774 .name = "ThinkPS/2",
775 .alias = "thinkps",
776 .detect = thinking_detect,
777 },
778 {
779 .type = PSMOUSE_GENPS,
780 .name = "GenPS/2",
781 .alias = "genius",
782 .detect = genius_detect,
783 },
784 {
785 .type = PSMOUSE_IMPS,
786 .name = "ImPS/2",
787 .alias = "imps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700788 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500789 .detect = intellimouse_detect,
790 },
791 {
792 .type = PSMOUSE_IMEX,
793 .name = "ImExPS/2",
794 .alias = "exps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700795 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500796 .detect = im_explorer_detect,
797 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500798#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500799 {
800 .type = PSMOUSE_SYNAPTICS,
801 .name = "SynPS/2",
802 .alias = "synaptics",
803 .detect = synaptics_detect,
804 .init = synaptics_init,
805 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500806#endif
807#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500808 {
809 .type = PSMOUSE_ALPS,
810 .name = "AlpsPS/2",
811 .alias = "alps",
812 .detect = alps_detect,
813 .init = alps_init,
814 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500815#endif
816#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500817 {
818 .type = PSMOUSE_LIFEBOOK,
819 .name = "LBPS/2",
820 .alias = "lifebook",
821 .init = lifebook_init,
822 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500823#endif
824#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500825 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500826 .type = PSMOUSE_TRACKPOINT,
827 .name = "TPPS/2",
828 .alias = "trackpoint",
829 .detect = trackpoint_detect,
830 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500831#endif
832#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500833 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500834 .type = PSMOUSE_TOUCHKIT_PS2,
835 .name = "touchkitPS/2",
836 .alias = "touchkit",
837 .detect = touchkit_ps2_detect,
838 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500839#endif
Andres Salomondf08ef22008-09-16 12:30:34 -0400840#ifdef CONFIG_MOUSE_PS2_OLPC
841 {
842 .type = PSMOUSE_HGPK,
843 .name = "OLPC HGPK",
844 .alias = "hgpk",
845 .detect = hgpk_detect,
846 },
847#endif
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400848#ifdef CONFIG_MOUSE_PS2_ELANTECH
849 {
850 .type = PSMOUSE_ELANTECH,
851 .name = "ETPS/2",
852 .alias = "elantech",
853 .detect = elantech_detect,
854 .init = elantech_init,
855 },
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700856#endif
857#ifdef CONFIG_MOUSE_PS2_SENTELIC
858 {
859 .type = PSMOUSE_FSP,
860 .name = "FSPPS/2",
861 .alias = "fsp",
862 .detect = fsp_detect,
863 .init = fsp_init,
864 },
865#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500866 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400867 .type = PSMOUSE_CORTRON,
868 .name = "CortronPS/2",
869 .alias = "cortps",
870 .detect = cortron_detect,
871 },
872 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500873 .type = PSMOUSE_AUTO,
874 .name = "auto",
875 .alias = "any",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700876 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500877 },
878};
879
Helge Dellere38de672006-09-10 21:54:39 -0400880static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500881{
882 int i;
883
884 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
885 if (psmouse_protocols[i].type == type)
886 return &psmouse_protocols[i];
887
888 WARN_ON(1);
889 return &psmouse_protocols[0];
890}
891
Helge Dellere38de672006-09-10 21:54:39 -0400892static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500893{
Helge Dellere38de672006-09-10 21:54:39 -0400894 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500895 int i;
896
897 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
898 p = &psmouse_protocols[i];
899
900 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
901 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
902 return &psmouse_protocols[i];
903 }
904
905 return NULL;
906}
907
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/*
910 * psmouse_probe() probes for a PS/2 mouse.
911 */
912
913static int psmouse_probe(struct psmouse *psmouse)
914{
915 struct ps2dev *ps2dev = &psmouse->ps2dev;
916 unsigned char param[2];
917
918/*
919 * First, we check if it's a mouse. It should send 0x00 or 0x03
920 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500921 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
922 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 */
924
925 param[0] = 0xa5;
926 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
927 return -1;
928
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500929 if (param[0] != 0x00 && param[0] != 0x03 &&
930 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -1;
932
933/*
934 * Then we reset and disable the mouse so that it doesn't generate events.
935 */
936
937 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
938 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
939
940 return 0;
941}
942
943/*
944 * Here we set the mouse resolution.
945 */
946
947void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
948{
Helge Dellere38de672006-09-10 21:54:39 -0400949 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
950 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if (resolution == 0 || resolution > 200)
953 resolution = 200;
954
Helge Dellere38de672006-09-10 21:54:39 -0400955 p = params[resolution / 50];
956 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
957 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960/*
961 * Here we set the mouse report rate.
962 */
963
964static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
965{
Helge Dellere38de672006-09-10 21:54:39 -0400966 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
967 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int i = 0;
969
970 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400971 r = rates[i];
972 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
973 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976/*
977 * psmouse_initialize() initializes the mouse to a sane state.
978 */
979
980static void psmouse_initialize(struct psmouse *psmouse)
981{
982/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * We set the mouse report rate, resolution and scaling.
984 */
985
986 if (psmouse_max_proto != PSMOUSE_PS2) {
987 psmouse->set_rate(psmouse, psmouse->rate);
988 psmouse->set_resolution(psmouse, psmouse->resolution);
989 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
990 }
991}
992
993/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 * psmouse_activate() enables the mouse so that we get motion reports from it.
995 */
996
997static void psmouse_activate(struct psmouse *psmouse)
998{
999 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1000 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
1001 psmouse->ps2dev.serio->phys);
1002
1003 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1004}
1005
1006
1007/*
1008 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +02001009 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
1011
1012static void psmouse_deactivate(struct psmouse *psmouse)
1013{
1014 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1015 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
1016 psmouse->ps2dev.serio->phys);
1017
1018 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1019}
1020
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001021/*
1022 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
1023 */
1024
1025static int psmouse_poll(struct psmouse *psmouse)
1026{
1027 return ps2_command(&psmouse->ps2dev, psmouse->packet,
1028 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
1029}
1030
1031
1032/*
1033 * psmouse_resync() attempts to re-validate current protocol.
1034 */
1035
David Howellsc4028952006-11-22 14:57:56 +00001036static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001037{
David Howellsc4028952006-11-22 14:57:56 +00001038 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -04001039 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001040 struct serio *serio = psmouse->ps2dev.serio;
1041 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001042 bool failed = false, enabled = false;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001043 int i;
1044
Ingo Molnarc14471d2006-02-19 00:22:11 -05001045 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001046
1047 if (psmouse->state != PSMOUSE_RESYNCING)
1048 goto out;
1049
1050 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1051 parent = serio_get_drvdata(serio->parent);
1052 psmouse_deactivate(parent);
1053 }
1054
1055/*
1056 * Some mice don't ACK commands sent while they are in the middle of
1057 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1058 * instead of ps2_command() which would wait for 200ms for an ACK
1059 * that may never come.
1060 * As an additional quirk ALPS touchpads may not only forget to ACK
1061 * disable command but will stop reporting taps, so if we see that
1062 * mouse at least once ACKs disable we will do full reconnect if ACK
1063 * is missing.
1064 */
1065 psmouse->num_resyncs++;
1066
1067 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1068 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001069 failed = true;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001070 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001071 psmouse->acks_disable_command = true;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001072
1073/*
1074 * Poll the mouse. If it was reset the packet will be shorter than
1075 * psmouse->pktsize and ps2_command will fail. We do not expect and
1076 * do not handle scenario when mouse "upgrades" its protocol while
1077 * disconnected since it would require additional delay. If we ever
1078 * see a mouse that does it we'll adjust the code.
1079 */
1080 if (!failed) {
1081 if (psmouse->poll(psmouse))
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001082 failed = true;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001083 else {
1084 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1085 for (i = 0; i < psmouse->pktsize; i++) {
1086 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001087 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001088 if (rc != PSMOUSE_GOOD_DATA)
1089 break;
1090 }
1091 if (rc != PSMOUSE_FULL_PACKET)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001092 failed = true;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001093 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1094 }
1095 }
1096/*
1097 * Now try to enable mouse. We try to do that even if poll failed and also
1098 * repeat our attempts 5 times, otherwise we may be left out with disabled
1099 * mouse.
1100 */
1101 for (i = 0; i < 5; i++) {
1102 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001103 enabled = true;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001104 break;
1105 }
1106 msleep(200);
1107 }
1108
1109 if (!enabled) {
1110 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1111 psmouse->ps2dev.serio->phys);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001112 failed = true;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001113 }
1114
1115 if (failed) {
1116 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1117 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1118 serio_reconnect(serio);
1119 } else
1120 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1121
1122 if (parent)
1123 psmouse_activate(parent);
1124 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001125 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128/*
1129 * psmouse_cleanup() resets the mouse into power-on state.
1130 */
1131
1132static void psmouse_cleanup(struct serio *serio)
1133{
1134 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001135 struct psmouse *parent = NULL;
1136
1137 mutex_lock(&psmouse_mutex);
1138
1139 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1140 parent = serio_get_drvdata(serio->parent);
1141 psmouse_deactivate(parent);
1142 }
1143
Dmitry Torokhova9f0c382010-02-07 23:10:04 -08001144 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1145
1146 /*
1147 * Disable stream mode so cleanup routine can proceed undisturbed.
1148 */
1149 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1150 printk(KERN_WARNING "psmouse.c: Failed to disable mouse on %s\n",
1151 psmouse->ps2dev.serio->phys);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001152
1153 if (psmouse->cleanup)
1154 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Dmitry Torokhov4a299bf2009-12-24 21:40:24 -08001156/*
1157 * Reset the mouse to defaults (bare PS/2 protocol).
1158 */
1159 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001160
1161/*
1162 * Some boxes, such as HP nx7400, get terribly confused if mouse
1163 * is not fully enabled before suspending/shutting down.
1164 */
1165 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1166
1167 if (parent) {
1168 if (parent->pt_deactivate)
1169 parent->pt_deactivate(parent);
1170
1171 psmouse_activate(parent);
1172 }
1173
1174 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177/*
1178 * psmouse_disconnect() closes and frees.
1179 */
1180
1181static void psmouse_disconnect(struct serio *serio)
1182{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001183 struct psmouse *psmouse, *parent = NULL;
1184
1185 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001187 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Ingo Molnarc14471d2006-02-19 00:22:11 -05001189 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1192
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001193 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001194 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001195 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001196 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1199 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001200 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
1203 if (psmouse->disconnect)
1204 psmouse->disconnect(psmouse);
1205
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001206 if (parent && parent->pt_deactivate)
1207 parent->pt_deactivate(parent);
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 serio_close(serio);
1212 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001213 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001215
1216 if (parent)
1217 psmouse_activate(parent);
1218
Ingo Molnarc14471d2006-02-19 00:22:11 -05001219 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001222static int psmouse_switch_protocol(struct psmouse *psmouse,
1223 const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001224{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001225 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001226
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001227 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001228
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001229 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001230 input_dev->keybit[BIT_WORD(BTN_MOUSE)] =
1231 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001232 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001233
1234 psmouse->set_rate = psmouse_set_rate;
1235 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001236 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001237 psmouse->protocol_handler = psmouse_process_byte;
1238 psmouse->pktsize = 3;
1239
1240 if (proto && (proto->detect || proto->init)) {
1241 if (proto->detect && proto->detect(psmouse, 1) < 0)
1242 return -1;
1243
1244 if (proto->init && proto->init(psmouse) < 0)
1245 return -1;
1246
1247 psmouse->type = proto->type;
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001248 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001249 psmouse->type = psmouse_extensions(psmouse,
1250 psmouse_max_proto, true);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001251
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001252 /*
1253 * If mouse's packet size is 3 there is no point in polling the
1254 * device in hopes to detect protocol reset - we won't get less
1255 * than 3 bytes response anyhow.
1256 */
1257 if (psmouse->pktsize == 3)
1258 psmouse->resync_time = 0;
1259
1260 /*
1261 * Some smart KVMs fake response to POLL command returning just
1262 * 3 bytes and messing up our resync logic, so if initial poll
1263 * fails we won't try polling the device anymore. Hopefully
1264 * such KVM will maintain initially selected protocol.
1265 */
1266 if (psmouse->resync_time && psmouse->poll(psmouse))
1267 psmouse->resync_time = 0;
1268
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001269 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1270 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001271
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001272 input_dev->name = psmouse->devname;
1273 input_dev->phys = psmouse->phys;
1274 input_dev->id.bustype = BUS_I8042;
1275 input_dev->id.vendor = 0x0002;
1276 input_dev->id.product = psmouse->type;
1277 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001278
1279 return 0;
1280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/*
1283 * psmouse_connect() is a callback from the serio module when
1284 * an unhandled serio port is found.
1285 */
1286static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1287{
1288 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001289 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001290 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Ingo Molnarc14471d2006-02-19 00:22:11 -05001292 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /*
1295 * If this is a pass-through port deactivate parent so the device
1296 * connected to this port can be successfully identified
1297 */
1298 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1299 parent = serio_get_drvdata(serio->parent);
1300 psmouse_deactivate(parent);
1301 }
1302
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001303 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1304 input_dev = input_allocate_device();
1305 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001306 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001309 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001310 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001311 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1314
1315 serio_set_drvdata(serio, psmouse);
1316
Dmitry Torokhov72155612006-11-05 22:40:19 -05001317 error = serio_open(serio, drv);
1318 if (error)
1319 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001322 error = -ENODEV;
1323 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
1326 psmouse->rate = psmouse_rate;
1327 psmouse->resolution = psmouse_resolution;
1328 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001329 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001332 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 psmouse_initialize(psmouse);
1336
Dmitry Torokhov72155612006-11-05 22:40:19 -05001337 error = input_register_device(psmouse->dev);
1338 if (error)
1339 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (parent && parent->pt_activate)
1342 parent->pt_activate(parent);
1343
Dmitry Torokhov72155612006-11-05 22:40:19 -05001344 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1345 if (error)
1346 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 psmouse_activate(psmouse);
1349
Dmitry Torokhov72155612006-11-05 22:40:19 -05001350 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001351 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (parent)
1353 psmouse_activate(parent);
1354
Ingo Molnarc14471d2006-02-19 00:22:11 -05001355 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001357
1358 err_pt_deactivate:
1359 if (parent && parent->pt_deactivate)
1360 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001361 input_unregister_device(psmouse->dev);
1362 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001363 err_protocol_disconnect:
1364 if (psmouse->disconnect)
1365 psmouse->disconnect(psmouse);
1366 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1367 err_close_serio:
1368 serio_close(serio);
1369 err_clear_drvdata:
1370 serio_set_drvdata(serio, NULL);
1371 err_free:
1372 input_free_device(input_dev);
1373 kfree(psmouse);
1374
1375 retval = error;
1376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
1379
1380static int psmouse_reconnect(struct serio *serio)
1381{
1382 struct psmouse *psmouse = serio_get_drvdata(serio);
1383 struct psmouse *parent = NULL;
1384 struct serio_driver *drv = serio->drv;
1385 int rc = -1;
1386
1387 if (!drv || !psmouse) {
1388 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1389 return -1;
1390 }
1391
Ingo Molnarc14471d2006-02-19 00:22:11 -05001392 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1395 parent = serio_get_drvdata(serio->parent);
1396 psmouse_deactivate(parent);
1397 }
1398
1399 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1400
1401 if (psmouse->reconnect) {
1402 if (psmouse->reconnect(psmouse))
1403 goto out;
1404 } else if (psmouse_probe(psmouse) < 0 ||
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001405 psmouse->type != psmouse_extensions(psmouse,
1406 psmouse_max_proto, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 goto out;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* ok, the device type (and capabilities) match the old one,
1411 * we can continue using it, complete intialization
1412 */
1413 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1414
1415 psmouse_initialize(psmouse);
1416
1417 if (parent && parent->pt_activate)
1418 parent->pt_activate(parent);
1419
1420 psmouse_activate(psmouse);
1421 rc = 0;
1422
1423out:
1424 /* If this is a pass-through port the parent waits to be activated */
1425 if (parent)
1426 psmouse_activate(parent);
1427
Ingo Molnarc14471d2006-02-19 00:22:11 -05001428 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return rc;
1430}
1431
1432static struct serio_device_id psmouse_serio_ids[] = {
1433 {
1434 .type = SERIO_8042,
1435 .proto = SERIO_ANY,
1436 .id = SERIO_ANY,
1437 .extra = SERIO_ANY,
1438 },
1439 {
1440 .type = SERIO_PS_PSTHRU,
1441 .proto = SERIO_ANY,
1442 .id = SERIO_ANY,
1443 .extra = SERIO_ANY,
1444 },
1445 { 0 }
1446};
1447
1448MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1449
1450static struct serio_driver psmouse_drv = {
1451 .driver = {
1452 .name = "psmouse",
1453 },
1454 .description = DRIVER_DESC,
1455 .id_table = psmouse_serio_ids,
1456 .interrupt = psmouse_interrupt,
1457 .connect = psmouse_connect,
1458 .reconnect = psmouse_reconnect,
1459 .disconnect = psmouse_disconnect,
1460 .cleanup = psmouse_cleanup,
1461};
1462
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001463ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1464 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001467 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1468 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001470 psmouse = serio_get_drvdata(serio);
1471
Eric W. Biederman59b01512010-01-05 17:56:02 -08001472 return attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001475ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1476 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001479 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1480 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 int retval;
1482
Ingo Molnarc14471d2006-02-19 00:22:11 -05001483 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001484 if (retval)
Eric W. Biederman59b01512010-01-05 17:56:02 -08001485 goto out;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001486
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001487 psmouse = serio_get_drvdata(serio);
1488
Andres Salomon68d48222008-09-16 12:30:34 -04001489 if (attr->protect) {
1490 if (psmouse->state == PSMOUSE_IGNORE) {
1491 retval = -ENODEV;
1492 goto out_unlock;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Andres Salomon68d48222008-09-16 12:30:34 -04001495 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1496 parent = serio_get_drvdata(serio->parent);
1497 psmouse_deactivate(parent);
1498 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001499
Andres Salomon68d48222008-09-16 12:30:34 -04001500 psmouse_deactivate(psmouse);
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001503 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Andres Salomon68d48222008-09-16 12:30:34 -04001505 if (attr->protect) {
1506 if (retval != -ENODEV)
1507 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001508
Andres Salomon68d48222008-09-16 12:30:34 -04001509 if (parent)
1510 psmouse_activate(parent);
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Ingo Molnarc14471d2006-02-19 00:22:11 -05001513 out_unlock:
1514 mutex_unlock(&psmouse_mutex);
Eric W. Biederman59b01512010-01-05 17:56:02 -08001515 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return retval;
1517}
1518
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001519static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1520{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001521 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001522
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001523 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001524}
1525
1526static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1527{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001528 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001529 unsigned long value;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001530
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001531 if (strict_strtoul(buf, 10, &value))
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001532 return -EINVAL;
1533
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001534 if ((unsigned int)value != value)
1535 return -EINVAL;
1536
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001537 *field = value;
1538
1539 return count;
1540}
1541
1542static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001543{
1544 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1545}
1546
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001547static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001548{
1549 struct serio *serio = psmouse->ps2dev.serio;
1550 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001551 struct input_dev *old_dev, *new_dev;
1552 const struct psmouse_protocol *proto, *old_proto;
1553 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001554 int retry = 0;
1555
Dmitry Torokhov72155612006-11-05 22:40:19 -05001556 proto = psmouse_protocol_by_name(buf, count);
1557 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001558 return -EINVAL;
1559
1560 if (psmouse->type == proto->type)
1561 return count;
1562
Dmitry Torokhov72155612006-11-05 22:40:19 -05001563 new_dev = input_allocate_device();
1564 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001565 return -ENOMEM;
1566
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001567 while (serio->child) {
1568 if (++retry > 3) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001569 printk(KERN_WARNING
1570 "psmouse: failed to destroy child port, "
1571 "protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001572 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001573 return -EIO;
1574 }
1575
Ingo Molnarc14471d2006-02-19 00:22:11 -05001576 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001577 serio_unregister_child_port(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001578 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001579
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001580 if (serio->drv != &psmouse_drv) {
1581 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001582 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001583 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001584
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001585 if (psmouse->type == proto->type) {
1586 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001587 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001588 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001589 }
1590
1591 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1592 parent = serio_get_drvdata(serio->parent);
1593 if (parent->pt_deactivate)
1594 parent->pt_deactivate(parent);
1595 }
1596
Dmitry Torokhov72155612006-11-05 22:40:19 -05001597 old_dev = psmouse->dev;
1598 old_proto = psmouse_protocol_by_type(psmouse->type);
1599
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001600 if (psmouse->disconnect)
1601 psmouse->disconnect(psmouse);
1602
1603 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001604
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001605 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001606 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1607
1608 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1609 psmouse_reset(psmouse);
1610 /* default to PSMOUSE_PS2 */
1611 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1612 }
1613
1614 psmouse_initialize(psmouse);
1615 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1616
Dmitry Torokhov72155612006-11-05 22:40:19 -05001617 error = input_register_device(psmouse->dev);
1618 if (error) {
1619 if (psmouse->disconnect)
1620 psmouse->disconnect(psmouse);
1621
1622 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1623 input_free_device(new_dev);
1624 psmouse->dev = old_dev;
1625 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1626 psmouse_switch_protocol(psmouse, old_proto);
1627 psmouse_initialize(psmouse);
1628 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1629
1630 return error;
1631 }
1632
1633 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001634
1635 if (parent && parent->pt_activate)
1636 parent->pt_activate(parent);
1637
1638 return count;
1639}
1640
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001641static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001645 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return -EINVAL;
1647
1648 psmouse->set_rate(psmouse, value);
1649 return count;
1650}
1651
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001652static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001656 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return -EINVAL;
1658
1659 psmouse->set_resolution(psmouse, value);
1660 return count;
1661}
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1665{
Helge Dellere38de672006-09-10 21:54:39 -04001666 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 if (!val)
1669 return -EINVAL;
1670
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001671 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001673 if (!proto || !proto->maxproto)
1674 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001676 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Stephen Evanchik541e3162005-08-08 01:26:18 -05001678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
1681static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1682{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001683 int type = *((unsigned int *)kp->arg);
1684
Takashi Iwai3d4c3aa2009-11-12 23:30:52 -08001685 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
1688static int __init psmouse_init(void)
1689{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001690 int err;
1691
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001692 lifebook_module_init();
1693 synaptics_module_init();
1694
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001695 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1696 if (!kpsmoused_wq) {
1697 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1698 return -ENOMEM;
1699 }
1700
Akinobu Mita153a9df02006-11-23 23:35:10 -05001701 err = serio_register_driver(&psmouse_drv);
1702 if (err)
1703 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001704
Akinobu Mita153a9df02006-11-23 23:35:10 -05001705 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
1708static void __exit psmouse_exit(void)
1709{
1710 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001711 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
1714module_init(psmouse_init);
1715module_exit(psmouse_exit);