blob: cabf4e1caacc7643b78786e7a1578f8716ed3005 [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;
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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-01-14 00:27:37 -0500245{
David Howells7d12e782006-10-05 14:55:46 +0100246 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-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 Torokhovf0d5c6f42006-01-14 00:27:37 -0500357 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500359
360 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100361 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-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/*
694 * Try Finger Sensing Pad
695 */
696 if (max_proto > PSMOUSE_IMEX) {
697 if (fsp_detect(psmouse, set_properties) == 0) {
698 if (!set_properties || fsp_init(psmouse) == 0)
699 return PSMOUSE_FSP;
700/*
701 * Init failed, try basic relative protocols
702 */
703 max_proto = PSMOUSE_IMEX;
704 }
705 }
706
Andres Salomondf08ef22008-09-16 12:30:34 -0400707 if (max_proto > PSMOUSE_IMEX) {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500708 if (genius_detect(psmouse, set_properties) == 0)
709 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500711 if (ps2pp_init(psmouse, set_properties) == 0)
712 return PSMOUSE_PS2PP;
713
714 if (trackpoint_detect(psmouse, set_properties) == 0)
715 return PSMOUSE_TRACKPOINT;
716
717 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
718 return PSMOUSE_TOUCHKIT_PS2;
719 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
722 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400723 * protocol probes. Note that we follow up with full reset because
724 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Alon Ziv554fc192007-08-30 00:22:48 -0400726 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500727 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
730 return PSMOUSE_IMEX;
731
732 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
733 return PSMOUSE_IMPS;
734
735/*
736 * Okay, all failed, we have a standard mouse here. The number of the buttons
737 * is still a question, though. We assume 3.
738 */
739 ps2bare_detect(psmouse, set_properties);
740
741 if (synaptics_hardware) {
742/*
743 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
744 * We need to reset the touchpad because if there is a track point on the
745 * pass through port it could get disabled while probing for protocol
746 * extensions.
747 */
748 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
751 return PSMOUSE_PS2;
752}
753
Helge Dellere38de672006-09-10 21:54:39 -0400754static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500755 {
756 .type = PSMOUSE_PS2,
757 .name = "PS/2",
758 .alias = "bare",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700759 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500760 .detect = ps2bare_detect,
761 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500762#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500763 {
764 .type = PSMOUSE_PS2PP,
765 .name = "PS2++",
766 .alias = "logitech",
767 .detect = ps2pp_init,
768 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500769#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500770 {
771 .type = PSMOUSE_THINKPS,
772 .name = "ThinkPS/2",
773 .alias = "thinkps",
774 .detect = thinking_detect,
775 },
776 {
777 .type = PSMOUSE_GENPS,
778 .name = "GenPS/2",
779 .alias = "genius",
780 .detect = genius_detect,
781 },
782 {
783 .type = PSMOUSE_IMPS,
784 .name = "ImPS/2",
785 .alias = "imps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700786 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500787 .detect = intellimouse_detect,
788 },
789 {
790 .type = PSMOUSE_IMEX,
791 .name = "ImExPS/2",
792 .alias = "exps",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700793 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500794 .detect = im_explorer_detect,
795 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500796#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500797 {
798 .type = PSMOUSE_SYNAPTICS,
799 .name = "SynPS/2",
800 .alias = "synaptics",
801 .detect = synaptics_detect,
802 .init = synaptics_init,
803 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500804#endif
805#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500806 {
807 .type = PSMOUSE_ALPS,
808 .name = "AlpsPS/2",
809 .alias = "alps",
810 .detect = alps_detect,
811 .init = alps_init,
812 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500813#endif
814#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500815 {
816 .type = PSMOUSE_LIFEBOOK,
817 .name = "LBPS/2",
818 .alias = "lifebook",
819 .init = lifebook_init,
820 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500821#endif
822#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500823 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500824 .type = PSMOUSE_TRACKPOINT,
825 .name = "TPPS/2",
826 .alias = "trackpoint",
827 .detect = trackpoint_detect,
828 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500829#endif
830#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500831 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500832 .type = PSMOUSE_TOUCHKIT_PS2,
833 .name = "touchkitPS/2",
834 .alias = "touchkit",
835 .detect = touchkit_ps2_detect,
836 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500837#endif
Andres Salomondf08ef22008-09-16 12:30:34 -0400838#ifdef CONFIG_MOUSE_PS2_OLPC
839 {
840 .type = PSMOUSE_HGPK,
841 .name = "OLPC HGPK",
842 .alias = "hgpk",
843 .detect = hgpk_detect,
844 },
845#endif
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400846#ifdef CONFIG_MOUSE_PS2_ELANTECH
847 {
848 .type = PSMOUSE_ELANTECH,
849 .name = "ETPS/2",
850 .alias = "elantech",
851 .detect = elantech_detect,
852 .init = elantech_init,
853 },
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700854#endif
855#ifdef CONFIG_MOUSE_PS2_SENTELIC
856 {
857 .type = PSMOUSE_FSP,
858 .name = "FSPPS/2",
859 .alias = "fsp",
860 .detect = fsp_detect,
861 .init = fsp_init,
862 },
863#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500864 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400865 .type = PSMOUSE_CORTRON,
866 .name = "CortronPS/2",
867 .alias = "cortps",
868 .detect = cortron_detect,
869 },
870 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500871 .type = PSMOUSE_AUTO,
872 .name = "auto",
873 .alias = "any",
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700874 .maxproto = true,
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500875 },
876};
877
Helge Dellere38de672006-09-10 21:54:39 -0400878static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500879{
880 int i;
881
882 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
883 if (psmouse_protocols[i].type == type)
884 return &psmouse_protocols[i];
885
886 WARN_ON(1);
887 return &psmouse_protocols[0];
888}
889
Helge Dellere38de672006-09-10 21:54:39 -0400890static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500891{
Helge Dellere38de672006-09-10 21:54:39 -0400892 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500893 int i;
894
895 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
896 p = &psmouse_protocols[i];
897
898 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
899 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
900 return &psmouse_protocols[i];
901 }
902
903 return NULL;
904}
905
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907/*
908 * psmouse_probe() probes for a PS/2 mouse.
909 */
910
911static int psmouse_probe(struct psmouse *psmouse)
912{
913 struct ps2dev *ps2dev = &psmouse->ps2dev;
914 unsigned char param[2];
915
916/*
917 * First, we check if it's a mouse. It should send 0x00 or 0x03
918 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500919 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
920 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
922
923 param[0] = 0xa5;
924 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
925 return -1;
926
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500927 if (param[0] != 0x00 && param[0] != 0x03 &&
928 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return -1;
930
931/*
932 * Then we reset and disable the mouse so that it doesn't generate events.
933 */
934
935 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
936 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
937
938 return 0;
939}
940
941/*
942 * Here we set the mouse resolution.
943 */
944
945void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
946{
Helge Dellere38de672006-09-10 21:54:39 -0400947 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
948 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 if (resolution == 0 || resolution > 200)
951 resolution = 200;
952
Helge Dellere38de672006-09-10 21:54:39 -0400953 p = params[resolution / 50];
954 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
955 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958/*
959 * Here we set the mouse report rate.
960 */
961
962static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
963{
Helge Dellere38de672006-09-10 21:54:39 -0400964 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
965 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int i = 0;
967
968 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400969 r = rates[i];
970 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
971 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974/*
975 * psmouse_initialize() initializes the mouse to a sane state.
976 */
977
978static void psmouse_initialize(struct psmouse *psmouse)
979{
980/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * We set the mouse report rate, resolution and scaling.
982 */
983
984 if (psmouse_max_proto != PSMOUSE_PS2) {
985 psmouse->set_rate(psmouse, psmouse->rate);
986 psmouse->set_resolution(psmouse, psmouse->resolution);
987 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
988 }
989}
990
991/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * psmouse_activate() enables the mouse so that we get motion reports from it.
993 */
994
995static void psmouse_activate(struct psmouse *psmouse)
996{
997 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
998 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
999 psmouse->ps2dev.serio->phys);
1000
1001 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1002}
1003
1004
1005/*
1006 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +02001007 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
1009
1010static void psmouse_deactivate(struct psmouse *psmouse)
1011{
1012 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1013 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
1014 psmouse->ps2dev.serio->phys);
1015
1016 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1017}
1018
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001019/*
1020 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
1021 */
1022
1023static int psmouse_poll(struct psmouse *psmouse)
1024{
1025 return ps2_command(&psmouse->ps2dev, psmouse->packet,
1026 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
1027}
1028
1029
1030/*
1031 * psmouse_resync() attempts to re-validate current protocol.
1032 */
1033
David Howellsc4028952006-11-22 14:57:56 +00001034static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001035{
David Howellsc4028952006-11-22 14:57:56 +00001036 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -04001037 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001038 struct serio *serio = psmouse->ps2dev.serio;
1039 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001040 bool failed = false, enabled = false;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001041 int i;
1042
Ingo Molnarc14471d2006-02-19 00:22:11 -05001043 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001044
1045 if (psmouse->state != PSMOUSE_RESYNCING)
1046 goto out;
1047
1048 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1049 parent = serio_get_drvdata(serio->parent);
1050 psmouse_deactivate(parent);
1051 }
1052
1053/*
1054 * Some mice don't ACK commands sent while they are in the middle of
1055 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1056 * instead of ps2_command() which would wait for 200ms for an ACK
1057 * that may never come.
1058 * As an additional quirk ALPS touchpads may not only forget to ACK
1059 * disable command but will stop reporting taps, so if we see that
1060 * mouse at least once ACKs disable we will do full reconnect if ACK
1061 * is missing.
1062 */
1063 psmouse->num_resyncs++;
1064
1065 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1066 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001067 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001068 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001069 psmouse->acks_disable_command = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001070
1071/*
1072 * Poll the mouse. If it was reset the packet will be shorter than
1073 * psmouse->pktsize and ps2_command will fail. We do not expect and
1074 * do not handle scenario when mouse "upgrades" its protocol while
1075 * disconnected since it would require additional delay. If we ever
1076 * see a mouse that does it we'll adjust the code.
1077 */
1078 if (!failed) {
1079 if (psmouse->poll(psmouse))
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001080 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001081 else {
1082 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1083 for (i = 0; i < psmouse->pktsize; i++) {
1084 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001085 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001086 if (rc != PSMOUSE_GOOD_DATA)
1087 break;
1088 }
1089 if (rc != PSMOUSE_FULL_PACKET)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001090 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001091 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1092 }
1093 }
1094/*
1095 * Now try to enable mouse. We try to do that even if poll failed and also
1096 * repeat our attempts 5 times, otherwise we may be left out with disabled
1097 * mouse.
1098 */
1099 for (i = 0; i < 5; i++) {
1100 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001101 enabled = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001102 break;
1103 }
1104 msleep(200);
1105 }
1106
1107 if (!enabled) {
1108 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1109 psmouse->ps2dev.serio->phys);
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001110 failed = true;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001111 }
1112
1113 if (failed) {
1114 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1115 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1116 serio_reconnect(serio);
1117 } else
1118 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1119
1120 if (parent)
1121 psmouse_activate(parent);
1122 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001123 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126/*
1127 * psmouse_cleanup() resets the mouse into power-on state.
1128 */
1129
1130static void psmouse_cleanup(struct serio *serio)
1131{
1132 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001133 struct psmouse *parent = NULL;
1134
1135 mutex_lock(&psmouse_mutex);
1136
1137 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1138 parent = serio_get_drvdata(serio->parent);
1139 psmouse_deactivate(parent);
1140 }
1141
1142 psmouse_deactivate(psmouse);
1143
1144 if (psmouse->cleanup)
1145 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Dmitry Torokhov4a299bf2009-12-24 21:40:24 -08001147/*
1148 * Reset the mouse to defaults (bare PS/2 protocol).
1149 */
1150 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001151
1152/*
1153 * Some boxes, such as HP nx7400, get terribly confused if mouse
1154 * is not fully enabled before suspending/shutting down.
1155 */
1156 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1157
1158 if (parent) {
1159 if (parent->pt_deactivate)
1160 parent->pt_deactivate(parent);
1161
1162 psmouse_activate(parent);
1163 }
1164
1165 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
1168/*
1169 * psmouse_disconnect() closes and frees.
1170 */
1171
1172static void psmouse_disconnect(struct serio *serio)
1173{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001174 struct psmouse *psmouse, *parent = NULL;
1175
1176 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001178 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Ingo Molnarc14471d2006-02-19 00:22:11 -05001180 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1183
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001184 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001185 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001186 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001187 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1190 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001191 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
1194 if (psmouse->disconnect)
1195 psmouse->disconnect(psmouse);
1196
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001197 if (parent && parent->pt_deactivate)
1198 parent->pt_deactivate(parent);
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 serio_close(serio);
1203 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001204 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001206
1207 if (parent)
1208 psmouse_activate(parent);
1209
Ingo Molnarc14471d2006-02-19 00:22:11 -05001210 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001213static int psmouse_switch_protocol(struct psmouse *psmouse,
1214 const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001215{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001216 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001217
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001218 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001219
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001220 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001221 input_dev->keybit[BIT_WORD(BTN_MOUSE)] =
1222 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001223 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001224
1225 psmouse->set_rate = psmouse_set_rate;
1226 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001227 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001228 psmouse->protocol_handler = psmouse_process_byte;
1229 psmouse->pktsize = 3;
1230
1231 if (proto && (proto->detect || proto->init)) {
1232 if (proto->detect && proto->detect(psmouse, 1) < 0)
1233 return -1;
1234
1235 if (proto->init && proto->init(psmouse) < 0)
1236 return -1;
1237
1238 psmouse->type = proto->type;
Dmitry Torokhov315eb992009-11-16 22:12:21 -08001239 } else
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001240 psmouse->type = psmouse_extensions(psmouse,
1241 psmouse_max_proto, true);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001242
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001243 /*
1244 * If mouse's packet size is 3 there is no point in polling the
1245 * device in hopes to detect protocol reset - we won't get less
1246 * than 3 bytes response anyhow.
1247 */
1248 if (psmouse->pktsize == 3)
1249 psmouse->resync_time = 0;
1250
1251 /*
1252 * Some smart KVMs fake response to POLL command returning just
1253 * 3 bytes and messing up our resync logic, so if initial poll
1254 * fails we won't try polling the device anymore. Hopefully
1255 * such KVM will maintain initially selected protocol.
1256 */
1257 if (psmouse->resync_time && psmouse->poll(psmouse))
1258 psmouse->resync_time = 0;
1259
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001260 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1261 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001262
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001263 input_dev->name = psmouse->devname;
1264 input_dev->phys = psmouse->phys;
1265 input_dev->id.bustype = BUS_I8042;
1266 input_dev->id.vendor = 0x0002;
1267 input_dev->id.product = psmouse->type;
1268 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001269
1270 return 0;
1271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273/*
1274 * psmouse_connect() is a callback from the serio module when
1275 * an unhandled serio port is found.
1276 */
1277static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1278{
1279 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001280 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001281 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Ingo Molnarc14471d2006-02-19 00:22:11 -05001283 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /*
1286 * If this is a pass-through port deactivate parent so the device
1287 * connected to this port can be successfully identified
1288 */
1289 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1290 parent = serio_get_drvdata(serio->parent);
1291 psmouse_deactivate(parent);
1292 }
1293
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001294 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1295 input_dev = input_allocate_device();
1296 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001297 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001300 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001301 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001302 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1305
1306 serio_set_drvdata(serio, psmouse);
1307
Dmitry Torokhov72155612006-11-05 22:40:19 -05001308 error = serio_open(serio, drv);
1309 if (error)
1310 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001313 error = -ENODEV;
1314 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316
1317 psmouse->rate = psmouse_rate;
1318 psmouse->resolution = psmouse_resolution;
1319 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001320 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001323 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 psmouse_initialize(psmouse);
1327
Dmitry Torokhov72155612006-11-05 22:40:19 -05001328 error = input_register_device(psmouse->dev);
1329 if (error)
1330 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (parent && parent->pt_activate)
1333 parent->pt_activate(parent);
1334
Dmitry Torokhov72155612006-11-05 22:40:19 -05001335 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1336 if (error)
1337 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 psmouse_activate(psmouse);
1340
Dmitry Torokhov72155612006-11-05 22:40:19 -05001341 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001342 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (parent)
1344 psmouse_activate(parent);
1345
Ingo Molnarc14471d2006-02-19 00:22:11 -05001346 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001348
1349 err_pt_deactivate:
1350 if (parent && parent->pt_deactivate)
1351 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001352 input_unregister_device(psmouse->dev);
1353 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001354 err_protocol_disconnect:
1355 if (psmouse->disconnect)
1356 psmouse->disconnect(psmouse);
1357 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1358 err_close_serio:
1359 serio_close(serio);
1360 err_clear_drvdata:
1361 serio_set_drvdata(serio, NULL);
1362 err_free:
1363 input_free_device(input_dev);
1364 kfree(psmouse);
1365
1366 retval = error;
1367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
1370
1371static int psmouse_reconnect(struct serio *serio)
1372{
1373 struct psmouse *psmouse = serio_get_drvdata(serio);
1374 struct psmouse *parent = NULL;
1375 struct serio_driver *drv = serio->drv;
1376 int rc = -1;
1377
1378 if (!drv || !psmouse) {
1379 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1380 return -1;
1381 }
1382
Ingo Molnarc14471d2006-02-19 00:22:11 -05001383 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1386 parent = serio_get_drvdata(serio->parent);
1387 psmouse_deactivate(parent);
1388 }
1389
1390 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1391
1392 if (psmouse->reconnect) {
1393 if (psmouse->reconnect(psmouse))
1394 goto out;
1395 } else if (psmouse_probe(psmouse) < 0 ||
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001396 psmouse->type != psmouse_extensions(psmouse,
1397 psmouse_max_proto, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 goto out;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /* ok, the device type (and capabilities) match the old one,
1402 * we can continue using it, complete intialization
1403 */
1404 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1405
1406 psmouse_initialize(psmouse);
1407
1408 if (parent && parent->pt_activate)
1409 parent->pt_activate(parent);
1410
1411 psmouse_activate(psmouse);
1412 rc = 0;
1413
1414out:
1415 /* If this is a pass-through port the parent waits to be activated */
1416 if (parent)
1417 psmouse_activate(parent);
1418
Ingo Molnarc14471d2006-02-19 00:22:11 -05001419 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return rc;
1421}
1422
1423static struct serio_device_id psmouse_serio_ids[] = {
1424 {
1425 .type = SERIO_8042,
1426 .proto = SERIO_ANY,
1427 .id = SERIO_ANY,
1428 .extra = SERIO_ANY,
1429 },
1430 {
1431 .type = SERIO_PS_PSTHRU,
1432 .proto = SERIO_ANY,
1433 .id = SERIO_ANY,
1434 .extra = SERIO_ANY,
1435 },
1436 { 0 }
1437};
1438
1439MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1440
1441static struct serio_driver psmouse_drv = {
1442 .driver = {
1443 .name = "psmouse",
1444 },
1445 .description = DRIVER_DESC,
1446 .id_table = psmouse_serio_ids,
1447 .interrupt = psmouse_interrupt,
1448 .connect = psmouse_connect,
1449 .reconnect = psmouse_reconnect,
1450 .disconnect = psmouse_disconnect,
1451 .cleanup = psmouse_cleanup,
1452};
1453
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001454ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1455 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001458 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1459 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001461 psmouse = serio_get_drvdata(serio);
1462
Eric W. Biederman59b01512010-01-05 17:56:02 -08001463 return attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001466ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1467 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001470 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1471 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int retval;
1473
Ingo Molnarc14471d2006-02-19 00:22:11 -05001474 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001475 if (retval)
Eric W. Biederman59b01512010-01-05 17:56:02 -08001476 goto out;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001477
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001478 psmouse = serio_get_drvdata(serio);
1479
Andres Salomon68d48222008-09-16 12:30:34 -04001480 if (attr->protect) {
1481 if (psmouse->state == PSMOUSE_IGNORE) {
1482 retval = -ENODEV;
1483 goto out_unlock;
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Andres Salomon68d48222008-09-16 12:30:34 -04001486 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1487 parent = serio_get_drvdata(serio->parent);
1488 psmouse_deactivate(parent);
1489 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001490
Andres Salomon68d48222008-09-16 12:30:34 -04001491 psmouse_deactivate(psmouse);
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001494 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Andres Salomon68d48222008-09-16 12:30:34 -04001496 if (attr->protect) {
1497 if (retval != -ENODEV)
1498 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001499
Andres Salomon68d48222008-09-16 12:30:34 -04001500 if (parent)
1501 psmouse_activate(parent);
1502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Ingo Molnarc14471d2006-02-19 00:22:11 -05001504 out_unlock:
1505 mutex_unlock(&psmouse_mutex);
Eric W. Biederman59b01512010-01-05 17:56:02 -08001506 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return retval;
1508}
1509
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001510static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1511{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001512 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001513
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001514 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001515}
1516
1517static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1518{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001519 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001520 unsigned long value;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001521
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001522 if (strict_strtoul(buf, 10, &value))
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001523 return -EINVAL;
1524
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001525 if ((unsigned int)value != value)
1526 return -EINVAL;
1527
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001528 *field = value;
1529
1530 return count;
1531}
1532
1533static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001534{
1535 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1536}
1537
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001538static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001539{
1540 struct serio *serio = psmouse->ps2dev.serio;
1541 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001542 struct input_dev *old_dev, *new_dev;
1543 const struct psmouse_protocol *proto, *old_proto;
1544 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001545 int retry = 0;
1546
Dmitry Torokhov72155612006-11-05 22:40:19 -05001547 proto = psmouse_protocol_by_name(buf, count);
1548 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001549 return -EINVAL;
1550
1551 if (psmouse->type == proto->type)
1552 return count;
1553
Dmitry Torokhov72155612006-11-05 22:40:19 -05001554 new_dev = input_allocate_device();
1555 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001556 return -ENOMEM;
1557
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001558 while (serio->child) {
1559 if (++retry > 3) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001560 printk(KERN_WARNING
1561 "psmouse: failed to destroy child port, "
1562 "protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001563 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001564 return -EIO;
1565 }
1566
Ingo Molnarc14471d2006-02-19 00:22:11 -05001567 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001568 serio_unregister_child_port(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001569 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001570
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001571 if (serio->drv != &psmouse_drv) {
1572 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001573 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001574 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001575
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001576 if (psmouse->type == proto->type) {
1577 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001578 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001579 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001580 }
1581
1582 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1583 parent = serio_get_drvdata(serio->parent);
1584 if (parent->pt_deactivate)
1585 parent->pt_deactivate(parent);
1586 }
1587
Dmitry Torokhov72155612006-11-05 22:40:19 -05001588 old_dev = psmouse->dev;
1589 old_proto = psmouse_protocol_by_type(psmouse->type);
1590
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001591 if (psmouse->disconnect)
1592 psmouse->disconnect(psmouse);
1593
1594 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001595
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001596 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001597 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1598
1599 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1600 psmouse_reset(psmouse);
1601 /* default to PSMOUSE_PS2 */
1602 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1603 }
1604
1605 psmouse_initialize(psmouse);
1606 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1607
Dmitry Torokhov72155612006-11-05 22:40:19 -05001608 error = input_register_device(psmouse->dev);
1609 if (error) {
1610 if (psmouse->disconnect)
1611 psmouse->disconnect(psmouse);
1612
1613 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1614 input_free_device(new_dev);
1615 psmouse->dev = old_dev;
1616 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1617 psmouse_switch_protocol(psmouse, old_proto);
1618 psmouse_initialize(psmouse);
1619 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1620
1621 return error;
1622 }
1623
1624 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001625
1626 if (parent && parent->pt_activate)
1627 parent->pt_activate(parent);
1628
1629 return count;
1630}
1631
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001632static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001636 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return -EINVAL;
1638
1639 psmouse->set_rate(psmouse, value);
1640 return count;
1641}
1642
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001643static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001647 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return -EINVAL;
1649
1650 psmouse->set_resolution(psmouse, value);
1651 return count;
1652}
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1656{
Helge Dellere38de672006-09-10 21:54:39 -04001657 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 if (!val)
1660 return -EINVAL;
1661
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001662 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001664 if (!proto || !proto->maxproto)
1665 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001667 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Stephen Evanchik541e3162005-08-08 01:26:18 -05001669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
1672static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1673{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001674 int type = *((unsigned int *)kp->arg);
1675
Takashi Iwai3d4c3aa2009-11-12 23:30:52 -08001676 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
1679static int __init psmouse_init(void)
1680{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001681 int err;
1682
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001683 lifebook_module_init();
1684 synaptics_module_init();
1685
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001686 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1687 if (!kpsmoused_wq) {
1688 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1689 return -ENOMEM;
1690 }
1691
Akinobu Mita153a9df02006-11-23 23:35:10 -05001692 err = serio_register_driver(&psmouse_drv);
1693 if (err)
1694 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001695
Akinobu Mita153a9df02006-11-23 23:35:10 -05001696 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699static void __exit psmouse_exit(void)
1700{
1701 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001702 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
1705module_init(psmouse_init);
1706module_exit(psmouse_exit);