blob: f8f86de694bbe6cdeced1097d0ac57a4aebab44c [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#define DRIVER_DESC "PS/2 mouse driver"
35
36MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
37MODULE_DESCRIPTION(DRIVER_DESC);
38MODULE_LICENSE("GPL");
39
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050040static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
42static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
44#define param_set_proto_abbrev psmouse_set_maxproto
45#define param_get_proto_abbrev psmouse_get_maxproto
46module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050047MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static unsigned int psmouse_resolution = 200;
50module_param_named(resolution, psmouse_resolution, uint, 0644);
51MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
52
53static unsigned int psmouse_rate = 100;
54module_param_named(rate, psmouse_rate, uint, 0644);
55MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
56
57static unsigned int psmouse_smartscroll = 1;
58module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
59MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
60
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050061static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062module_param_named(resetafter, psmouse_resetafter, uint, 0644);
63MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
64
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050065static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050066module_param_named(resync_time, psmouse_resync_time, uint, 0644);
67MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
68
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050069PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
70 NULL,
71 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
72PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
73 (void *) offsetof(struct psmouse, rate),
74 psmouse_show_int_attr, psmouse_attr_set_rate);
75PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
76 (void *) offsetof(struct psmouse, resolution),
77 psmouse_show_int_attr, psmouse_attr_set_resolution);
78PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
79 (void *) offsetof(struct psmouse, resetafter),
80 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050081PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
82 (void *) offsetof(struct psmouse, resync_time),
83 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050084
85static struct attribute *psmouse_attributes[] = {
86 &psmouse_attr_protocol.dattr.attr,
87 &psmouse_attr_rate.dattr.attr,
88 &psmouse_attr_resolution.dattr.attr,
89 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050090 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050091 NULL
92};
93
94static struct attribute_group psmouse_attribute_group = {
95 .attrs = psmouse_attributes,
96};
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Dmitry Torokhov04df1922005-06-01 02:39:44 -050098/*
Ingo Molnarc14471d2006-02-19 00:22:11 -050099 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500100 * (connecting, disconnecting, changing rate or resolution via
101 * sysfs). We could use a per-device semaphore but since there
102 * rarely more than one PS/2 mouse connected and since semaphore
103 * is taken in "slow" paths it is not worth it.
104 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500105static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500106
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500107static struct workqueue_struct *kpsmoused_wq;
108
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500109struct psmouse_protocol {
110 enum psmouse_type type;
Helge Dellere38de672006-09-10 21:54:39 -0400111 const char *name;
112 const char *alias;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500113 int maxproto;
114 int (*detect)(struct psmouse *, int);
115 int (*init)(struct psmouse *);
116};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/*
119 * psmouse_process_byte() analyzes the PS/2 data stream and reports
120 * relevant events to the input module once full packet has arrived.
121 */
122
David Howells7d12e782006-10-05 14:55:46 +0100123static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500125 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned char *packet = psmouse->packet;
127
128 if (psmouse->pktcnt < psmouse->pktsize)
129 return PSMOUSE_GOOD_DATA;
130
131/*
132 * Full packet accumulated, process it
133 */
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * Scroll wheel on IntelliMice, scroll buttons on NetMice
137 */
138
139 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
140 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
141
142/*
143 * Scroll wheel and buttons on IntelliMouse Explorer
144 */
145
146 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400147 switch (packet[3] & 0xC0) {
148 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
149 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
150 break;
151 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
152 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
153 break;
154 case 0x00:
155 case 0xC0:
156 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
157 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
158 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
159 break;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
163/*
164 * Extra buttons on Genius NewNet 3D
165 */
166
167 if (psmouse->type == PSMOUSE_GENPS) {
168 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
169 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
170 }
171
172/*
173 * Extra button on ThinkingMouse
174 */
175 if (psmouse->type == PSMOUSE_THINKPS) {
176 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
177 /* Without this bit of weirdness moving up gives wildly high Y changes. */
178 packet[1] |= (packet[0] & 0x40) << 1;
179 }
180
181/*
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400182 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
183 * byte.
184 */
185 if (psmouse->type == PSMOUSE_CORTRON) {
186 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
187 packet[0] |= 0x08;
188 }
189
190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * Generic PS/2 Mouse
192 */
193
194 input_report_key(dev, BTN_LEFT, packet[0] & 1);
195 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
196 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
197
198 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
199 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
200
201 input_sync(dev);
202
203 return PSMOUSE_FULL_PACKET;
204}
205
Andres Salomon8bf020e2008-09-16 12:30:33 -0400206void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
207 unsigned long delay)
208{
209 queue_delayed_work(kpsmoused_wq, work, delay);
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500213 * __psmouse_set_state() sets new psmouse state and resets all flags.
214 */
215
216static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
217{
218 psmouse->state = new_state;
219 psmouse->pktcnt = psmouse->out_of_sync = 0;
220 psmouse->ps2dev.flags = 0;
221 psmouse->last = jiffies;
222}
223
224
225/*
226 * psmouse_set_state() sets new psmouse state and resets all flags and
227 * counters while holding serio lock so fighting with interrupt handler
228 * is not a concern.
229 */
230
Andres Salomona48cf5f2008-09-16 12:30:33 -0400231void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500232{
233 serio_pause_rx(psmouse->ps2dev.serio);
234 __psmouse_set_state(psmouse, new_state);
235 serio_continue_rx(psmouse->ps2dev.serio);
236}
237
238/*
239 * psmouse_handle_byte() processes one byte of the input data stream
240 * by calling corresponding protocol handler.
241 */
242
David Howells7d12e782006-10-05 14:55:46 +0100243static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500244{
David Howells7d12e782006-10-05 14:55:46 +0100245 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500246
247 switch (rc) {
248 case PSMOUSE_BAD_DATA:
249 if (psmouse->state == PSMOUSE_ACTIVATED) {
250 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
251 psmouse->name, psmouse->phys, psmouse->pktcnt);
252 if (++psmouse->out_of_sync == psmouse->resetafter) {
253 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
254 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
255 serio_reconnect(psmouse->ps2dev.serio);
256 return -1;
257 }
258 }
259 psmouse->pktcnt = 0;
260 break;
261
262 case PSMOUSE_FULL_PACKET:
263 psmouse->pktcnt = 0;
264 if (psmouse->out_of_sync) {
265 psmouse->out_of_sync = 0;
266 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
267 psmouse->name, psmouse->phys);
268 }
269 break;
270
271 case PSMOUSE_GOOD_DATA:
272 break;
273 }
274 return 0;
275}
276
277/*
278 * psmouse_interrupt() handles incoming characters, either passing them
279 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
281
282static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100283 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (psmouse->state == PSMOUSE_IGNORE)
288 goto out;
289
290 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
291 if (psmouse->state == PSMOUSE_ACTIVATED)
292 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
293 flags & SERIO_TIMEOUT ? " timeout" : "",
294 flags & SERIO_PARITY ? " bad parity" : "");
295 ps2_cmd_aborted(&psmouse->ps2dev);
296 goto out;
297 }
298
299 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
300 if (ps2_handle_ack(&psmouse->ps2dev, data))
301 goto out;
302
303 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
304 if (ps2_handle_response(&psmouse->ps2dev, data))
305 goto out;
306
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500307 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto out;
309
310 if (psmouse->state == PSMOUSE_ACTIVATED &&
311 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500312 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500314 psmouse->badbyte = psmouse->packet[0];
315 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400316 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500321/*
322 * Check if this is a new device announcement (0xAA 0x00)
323 */
324 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400325 if (psmouse->pktcnt == 1) {
326 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500330 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
331 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
332 serio_reconnect(serio);
333 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500335/*
336 * Not a new device, try processing first byte normally
337 */
338 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100339 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500340 goto out;
341
342 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500345/*
346 * See if we need to force resync because mouse was idle for too long
347 */
348 if (psmouse->state == PSMOUSE_ACTIVATED &&
349 psmouse->pktcnt == 1 && psmouse->resync_time &&
350 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
351 psmouse->badbyte = psmouse->packet[0];
352 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400353 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500356
357 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100358 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500359
360 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return IRQ_HANDLED;
362}
363
364
365/*
366 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
367 * using sliced syntax, understood by advanced devices, such as Logitech
368 * or Synaptics touchpads. The command is encoded as:
369 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
370 * is the command.
371 */
372int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
373{
374 int i;
375
376 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
377 return -1;
378
379 for (i = 6; i >= 0; i -= 2) {
380 unsigned char d = (command >> i) & 3;
381 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
382 return -1;
383 }
384
385 return 0;
386}
387
388
389/*
390 * psmouse_reset() resets the mouse into power-on state.
391 */
392int psmouse_reset(struct psmouse *psmouse)
393{
394 unsigned char param[2];
395
396 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
397 return -1;
398
399 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
400 return -1;
401
402 return 0;
403}
404
405
406/*
407 * Genius NetMouse magic init.
408 */
409static int genius_detect(struct psmouse *psmouse, int set_properties)
410{
411 struct ps2dev *ps2dev = &psmouse->ps2dev;
412 unsigned char param[4];
413
414 param[0] = 3;
415 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
416 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
417 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
418 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
419 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
420
421 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
422 return -1;
423
424 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500425 set_bit(BTN_EXTRA, psmouse->dev->keybit);
426 set_bit(BTN_SIDE, psmouse->dev->keybit);
427 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500430 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 psmouse->pktsize = 4;
432 }
433
434 return 0;
435}
436
437/*
438 * IntelliMouse magic init.
439 */
440static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
441{
442 struct ps2dev *ps2dev = &psmouse->ps2dev;
443 unsigned char param[2];
444
445 param[0] = 200;
446 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
447 param[0] = 100;
448 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
449 param[0] = 80;
450 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
451 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
452
453 if (param[0] != 3)
454 return -1;
455
456 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500457 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
458 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (!psmouse->vendor) psmouse->vendor = "Generic";
461 if (!psmouse->name) psmouse->name = "Wheel Mouse";
462 psmouse->pktsize = 4;
463 }
464
465 return 0;
466}
467
468/*
469 * Try IntelliMouse/Explorer magic init.
470 */
471static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
472{
473 struct ps2dev *ps2dev = &psmouse->ps2dev;
474 unsigned char param[2];
475
476 intellimouse_detect(psmouse, 0);
477
478 param[0] = 200;
479 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
480 param[0] = 200;
481 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
482 param[0] = 80;
483 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
484 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
485
486 if (param[0] != 4)
487 return -1;
488
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400489/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
490 param[0] = 200;
491 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
492 param[0] = 80;
493 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
494 param[0] = 40;
495 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500498 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
499 set_bit(REL_WHEEL, psmouse->dev->relbit);
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400500 set_bit(REL_HWHEEL, psmouse->dev->relbit);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500501 set_bit(BTN_SIDE, psmouse->dev->keybit);
502 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (!psmouse->vendor) psmouse->vendor = "Generic";
505 if (!psmouse->name) psmouse->name = "Explorer Mouse";
506 psmouse->pktsize = 4;
507 }
508
509 return 0;
510}
511
512/*
513 * Kensington ThinkingMouse / ExpertMouse magic init.
514 */
515static int thinking_detect(struct psmouse *psmouse, int set_properties)
516{
517 struct ps2dev *ps2dev = &psmouse->ps2dev;
518 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400519 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int i;
521
522 param[0] = 10;
523 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
524 param[0] = 0;
525 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400526 for (i = 0; i < ARRAY_SIZE(seq); i++) {
527 param[0] = seq[i];
528 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
531
532 if (param[0] != 2)
533 return -1;
534
535 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500536 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 psmouse->vendor = "Kensington";
539 psmouse->name = "ThinkingMouse";
540 }
541
542 return 0;
543}
544
545/*
546 * Bare PS/2 protocol "detection". Always succeeds.
547 */
548static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
549{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500550 if (set_properties) {
551 if (!psmouse->vendor) psmouse->vendor = "Generic";
552 if (!psmouse->name) psmouse->name = "Mouse";
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 return 0;
556}
557
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400558/*
559 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
560 * must be forced by sysfs protocol writing.
561 */
562static int cortron_detect(struct psmouse *psmouse, int set_properties)
563{
564 if (set_properties) {
565 psmouse->vendor = "Cortron";
566 psmouse->name = "PS/2 Trackball";
567 set_bit(BTN_SIDE, psmouse->dev->keybit);
568 }
569
570 return 0;
571}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
574 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
575 * the mouse may have.
576 */
577
578static int psmouse_extensions(struct psmouse *psmouse,
579 unsigned int max_proto, int set_properties)
580{
581 int synaptics_hardware = 0;
582
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500583/*
584 * We always check for lifebook because it does not disturb mouse
585 * (it only checks DMI information).
586 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500587 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500588 if (max_proto > PSMOUSE_IMEX) {
589 if (!set_properties || lifebook_init(psmouse) == 0)
590 return PSMOUSE_LIFEBOOK;
591 }
592 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/*
595 * Try Kensington ThinkingMouse (we try first, because synaptics probe
596 * upsets the thinkingmouse).
597 */
598
599 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
600 return PSMOUSE_THINKPS;
601
602/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500603 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
604 * support is disabled in config - we need to know if it is synaptics so we
605 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
607 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
608 synaptics_hardware = 1;
609
610 if (max_proto > PSMOUSE_IMEX) {
611 if (!set_properties || synaptics_init(psmouse) == 0)
612 return PSMOUSE_SYNAPTICS;
613/*
614 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
615 * Unfortunately Logitech/Genius probes confuse some firmware versions so
616 * we'll have to skip them.
617 */
618 max_proto = PSMOUSE_IMEX;
619 }
620/*
621 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
622 */
623 synaptics_reset(psmouse);
624 }
625
626/*
627 * Try ALPS TouchPad
628 */
629 if (max_proto > PSMOUSE_IMEX) {
630 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
631 if (alps_detect(psmouse, set_properties) == 0) {
632 if (!set_properties || alps_init(psmouse) == 0)
633 return PSMOUSE_ALPS;
634/*
635 * Init failed, try basic relative protocols
636 */
637 max_proto = PSMOUSE_IMEX;
638 }
639 }
640
Andres Salomondf08ef22008-09-16 12:30:34 -0400641/*
642 * Try OLPC HGPK touchpad.
643 */
644 if (max_proto > PSMOUSE_IMEX &&
645 hgpk_detect(psmouse, set_properties) == 0) {
646 if (!set_properties || hgpk_init(psmouse) == 0)
647 return PSMOUSE_HGPK;
648/*
649 * Init failed, try basic relative protocols
650 */
651 max_proto = PSMOUSE_IMEX;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400654/*
655 * Try Elantech touchpad.
656 */
657 if (max_proto > PSMOUSE_IMEX &&
658 elantech_detect(psmouse, set_properties) == 0) {
659 if (!set_properties || elantech_init(psmouse) == 0)
660 return PSMOUSE_ELANTECH;
661/*
662 * Init failed, try basic relative protocols
663 */
664 max_proto = PSMOUSE_IMEX;
665 }
666
Andres Salomondf08ef22008-09-16 12:30:34 -0400667 if (max_proto > PSMOUSE_IMEX) {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500668 if (genius_detect(psmouse, set_properties) == 0)
669 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500671 if (ps2pp_init(psmouse, set_properties) == 0)
672 return PSMOUSE_PS2PP;
673
674 if (trackpoint_detect(psmouse, set_properties) == 0)
675 return PSMOUSE_TRACKPOINT;
676
677 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
678 return PSMOUSE_TOUCHKIT_PS2;
679 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/*
682 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400683 * protocol probes. Note that we follow up with full reset because
684 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Alon Ziv554fc192007-08-30 00:22:48 -0400686 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500687 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
690 return PSMOUSE_IMEX;
691
692 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
693 return PSMOUSE_IMPS;
694
695/*
696 * Okay, all failed, we have a standard mouse here. The number of the buttons
697 * is still a question, though. We assume 3.
698 */
699 ps2bare_detect(psmouse, set_properties);
700
701 if (synaptics_hardware) {
702/*
703 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
704 * We need to reset the touchpad because if there is a track point on the
705 * pass through port it could get disabled while probing for protocol
706 * extensions.
707 */
708 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
711 return PSMOUSE_PS2;
712}
713
Helge Dellere38de672006-09-10 21:54:39 -0400714static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500715 {
716 .type = PSMOUSE_PS2,
717 .name = "PS/2",
718 .alias = "bare",
719 .maxproto = 1,
720 .detect = ps2bare_detect,
721 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500722#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500723 {
724 .type = PSMOUSE_PS2PP,
725 .name = "PS2++",
726 .alias = "logitech",
727 .detect = ps2pp_init,
728 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500729#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500730 {
731 .type = PSMOUSE_THINKPS,
732 .name = "ThinkPS/2",
733 .alias = "thinkps",
734 .detect = thinking_detect,
735 },
736 {
737 .type = PSMOUSE_GENPS,
738 .name = "GenPS/2",
739 .alias = "genius",
740 .detect = genius_detect,
741 },
742 {
743 .type = PSMOUSE_IMPS,
744 .name = "ImPS/2",
745 .alias = "imps",
746 .maxproto = 1,
747 .detect = intellimouse_detect,
748 },
749 {
750 .type = PSMOUSE_IMEX,
751 .name = "ImExPS/2",
752 .alias = "exps",
753 .maxproto = 1,
754 .detect = im_explorer_detect,
755 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500756#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500757 {
758 .type = PSMOUSE_SYNAPTICS,
759 .name = "SynPS/2",
760 .alias = "synaptics",
761 .detect = synaptics_detect,
762 .init = synaptics_init,
763 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500764#endif
765#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500766 {
767 .type = PSMOUSE_ALPS,
768 .name = "AlpsPS/2",
769 .alias = "alps",
770 .detect = alps_detect,
771 .init = alps_init,
772 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500773#endif
774#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500775 {
776 .type = PSMOUSE_LIFEBOOK,
777 .name = "LBPS/2",
778 .alias = "lifebook",
779 .init = lifebook_init,
780 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500781#endif
782#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500783 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500784 .type = PSMOUSE_TRACKPOINT,
785 .name = "TPPS/2",
786 .alias = "trackpoint",
787 .detect = trackpoint_detect,
788 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500789#endif
790#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500791 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500792 .type = PSMOUSE_TOUCHKIT_PS2,
793 .name = "touchkitPS/2",
794 .alias = "touchkit",
795 .detect = touchkit_ps2_detect,
796 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500797#endif
Andres Salomondf08ef22008-09-16 12:30:34 -0400798#ifdef CONFIG_MOUSE_PS2_OLPC
799 {
800 .type = PSMOUSE_HGPK,
801 .name = "OLPC HGPK",
802 .alias = "hgpk",
803 .detect = hgpk_detect,
804 },
805#endif
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400806#ifdef CONFIG_MOUSE_PS2_ELANTECH
807 {
808 .type = PSMOUSE_ELANTECH,
809 .name = "ETPS/2",
810 .alias = "elantech",
811 .detect = elantech_detect,
812 .init = elantech_init,
813 },
814 #endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500815 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400816 .type = PSMOUSE_CORTRON,
817 .name = "CortronPS/2",
818 .alias = "cortps",
819 .detect = cortron_detect,
820 },
821 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500822 .type = PSMOUSE_AUTO,
823 .name = "auto",
824 .alias = "any",
825 .maxproto = 1,
826 },
827};
828
Helge Dellere38de672006-09-10 21:54:39 -0400829static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500830{
831 int i;
832
833 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
834 if (psmouse_protocols[i].type == type)
835 return &psmouse_protocols[i];
836
837 WARN_ON(1);
838 return &psmouse_protocols[0];
839}
840
Helge Dellere38de672006-09-10 21:54:39 -0400841static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500842{
Helge Dellere38de672006-09-10 21:54:39 -0400843 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500844 int i;
845
846 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
847 p = &psmouse_protocols[i];
848
849 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
850 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
851 return &psmouse_protocols[i];
852 }
853
854 return NULL;
855}
856
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/*
859 * psmouse_probe() probes for a PS/2 mouse.
860 */
861
862static int psmouse_probe(struct psmouse *psmouse)
863{
864 struct ps2dev *ps2dev = &psmouse->ps2dev;
865 unsigned char param[2];
866
867/*
868 * First, we check if it's a mouse. It should send 0x00 or 0x03
869 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500870 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
871 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
873
874 param[0] = 0xa5;
875 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
876 return -1;
877
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500878 if (param[0] != 0x00 && param[0] != 0x03 &&
879 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return -1;
881
882/*
883 * Then we reset and disable the mouse so that it doesn't generate events.
884 */
885
886 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
887 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
888
889 return 0;
890}
891
892/*
893 * Here we set the mouse resolution.
894 */
895
896void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
897{
Helge Dellere38de672006-09-10 21:54:39 -0400898 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
899 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (resolution == 0 || resolution > 200)
902 resolution = 200;
903
Helge Dellere38de672006-09-10 21:54:39 -0400904 p = params[resolution / 50];
905 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
906 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/*
910 * Here we set the mouse report rate.
911 */
912
913static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
914{
Helge Dellere38de672006-09-10 21:54:39 -0400915 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
916 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int i = 0;
918
919 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400920 r = rates[i];
921 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
922 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925/*
926 * psmouse_initialize() initializes the mouse to a sane state.
927 */
928
929static void psmouse_initialize(struct psmouse *psmouse)
930{
931/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 * We set the mouse report rate, resolution and scaling.
933 */
934
935 if (psmouse_max_proto != PSMOUSE_PS2) {
936 psmouse->set_rate(psmouse, psmouse->rate);
937 psmouse->set_resolution(psmouse, psmouse->resolution);
938 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
939 }
940}
941
942/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * psmouse_activate() enables the mouse so that we get motion reports from it.
944 */
945
946static void psmouse_activate(struct psmouse *psmouse)
947{
948 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
949 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
950 psmouse->ps2dev.serio->phys);
951
952 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
953}
954
955
956/*
957 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +0200958 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 */
960
961static void psmouse_deactivate(struct psmouse *psmouse)
962{
963 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
964 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
965 psmouse->ps2dev.serio->phys);
966
967 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
968}
969
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500970/*
971 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
972 */
973
974static int psmouse_poll(struct psmouse *psmouse)
975{
976 return ps2_command(&psmouse->ps2dev, psmouse->packet,
977 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
978}
979
980
981/*
982 * psmouse_resync() attempts to re-validate current protocol.
983 */
984
David Howellsc4028952006-11-22 14:57:56 +0000985static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500986{
David Howellsc4028952006-11-22 14:57:56 +0000987 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -0400988 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500989 struct serio *serio = psmouse->ps2dev.serio;
990 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
991 int failed = 0, enabled = 0;
992 int i;
993
Ingo Molnarc14471d2006-02-19 00:22:11 -0500994 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500995
996 if (psmouse->state != PSMOUSE_RESYNCING)
997 goto out;
998
999 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1000 parent = serio_get_drvdata(serio->parent);
1001 psmouse_deactivate(parent);
1002 }
1003
1004/*
1005 * Some mice don't ACK commands sent while they are in the middle of
1006 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1007 * instead of ps2_command() which would wait for 200ms for an ACK
1008 * that may never come.
1009 * As an additional quirk ALPS touchpads may not only forget to ACK
1010 * disable command but will stop reporting taps, so if we see that
1011 * mouse at least once ACKs disable we will do full reconnect if ACK
1012 * is missing.
1013 */
1014 psmouse->num_resyncs++;
1015
1016 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1017 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1018 failed = 1;
1019 } else
1020 psmouse->acks_disable_command = 1;
1021
1022/*
1023 * Poll the mouse. If it was reset the packet will be shorter than
1024 * psmouse->pktsize and ps2_command will fail. We do not expect and
1025 * do not handle scenario when mouse "upgrades" its protocol while
1026 * disconnected since it would require additional delay. If we ever
1027 * see a mouse that does it we'll adjust the code.
1028 */
1029 if (!failed) {
1030 if (psmouse->poll(psmouse))
1031 failed = 1;
1032 else {
1033 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1034 for (i = 0; i < psmouse->pktsize; i++) {
1035 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001036 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001037 if (rc != PSMOUSE_GOOD_DATA)
1038 break;
1039 }
1040 if (rc != PSMOUSE_FULL_PACKET)
1041 failed = 1;
1042 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1043 }
1044 }
1045/*
1046 * Now try to enable mouse. We try to do that even if poll failed and also
1047 * repeat our attempts 5 times, otherwise we may be left out with disabled
1048 * mouse.
1049 */
1050 for (i = 0; i < 5; i++) {
1051 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1052 enabled = 1;
1053 break;
1054 }
1055 msleep(200);
1056 }
1057
1058 if (!enabled) {
1059 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1060 psmouse->ps2dev.serio->phys);
1061 failed = 1;
1062 }
1063
1064 if (failed) {
1065 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1066 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1067 serio_reconnect(serio);
1068 } else
1069 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1070
1071 if (parent)
1072 psmouse_activate(parent);
1073 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001074 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001075}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077/*
1078 * psmouse_cleanup() resets the mouse into power-on state.
1079 */
1080
1081static void psmouse_cleanup(struct serio *serio)
1082{
1083 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001084 struct psmouse *parent = NULL;
1085
1086 mutex_lock(&psmouse_mutex);
1087
1088 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1089 parent = serio_get_drvdata(serio->parent);
1090 psmouse_deactivate(parent);
1091 }
1092
1093 psmouse_deactivate(psmouse);
1094
1095 if (psmouse->cleanup)
1096 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 psmouse_reset(psmouse);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001099
1100/*
1101 * Some boxes, such as HP nx7400, get terribly confused if mouse
1102 * is not fully enabled before suspending/shutting down.
1103 */
1104 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1105
1106 if (parent) {
1107 if (parent->pt_deactivate)
1108 parent->pt_deactivate(parent);
1109
1110 psmouse_activate(parent);
1111 }
1112
1113 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
1116/*
1117 * psmouse_disconnect() closes and frees.
1118 */
1119
1120static void psmouse_disconnect(struct serio *serio)
1121{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001122 struct psmouse *psmouse, *parent = NULL;
1123
1124 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001126 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Ingo Molnarc14471d2006-02-19 00:22:11 -05001128 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1131
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001132 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001133 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001134 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001135 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1138 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001139 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141
1142 if (psmouse->disconnect)
1143 psmouse->disconnect(psmouse);
1144
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001145 if (parent && parent->pt_deactivate)
1146 parent->pt_deactivate(parent);
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 serio_close(serio);
1151 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001152 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001154
1155 if (parent)
1156 psmouse_activate(parent);
1157
Ingo Molnarc14471d2006-02-19 00:22:11 -05001158 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Helge Dellere38de672006-09-10 21:54:39 -04001161static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001162{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001163 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001164
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001165 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001166
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001167 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1168 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
1169 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1170 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001171
1172 psmouse->set_rate = psmouse_set_rate;
1173 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001174 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001175 psmouse->protocol_handler = psmouse_process_byte;
1176 psmouse->pktsize = 3;
1177
1178 if (proto && (proto->detect || proto->init)) {
1179 if (proto->detect && proto->detect(psmouse, 1) < 0)
1180 return -1;
1181
1182 if (proto->init && proto->init(psmouse) < 0)
1183 return -1;
1184
1185 psmouse->type = proto->type;
1186 }
1187 else
1188 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1189
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001190 /*
1191 * If mouse's packet size is 3 there is no point in polling the
1192 * device in hopes to detect protocol reset - we won't get less
1193 * than 3 bytes response anyhow.
1194 */
1195 if (psmouse->pktsize == 3)
1196 psmouse->resync_time = 0;
1197
1198 /*
1199 * Some smart KVMs fake response to POLL command returning just
1200 * 3 bytes and messing up our resync logic, so if initial poll
1201 * fails we won't try polling the device anymore. Hopefully
1202 * such KVM will maintain initially selected protocol.
1203 */
1204 if (psmouse->resync_time && psmouse->poll(psmouse))
1205 psmouse->resync_time = 0;
1206
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001207 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1208 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001209
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001210 input_dev->name = psmouse->devname;
1211 input_dev->phys = psmouse->phys;
1212 input_dev->id.bustype = BUS_I8042;
1213 input_dev->id.vendor = 0x0002;
1214 input_dev->id.product = psmouse->type;
1215 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001216
1217 return 0;
1218}
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/*
1221 * psmouse_connect() is a callback from the serio module when
1222 * an unhandled serio port is found.
1223 */
1224static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1225{
1226 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001227 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001228 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Ingo Molnarc14471d2006-02-19 00:22:11 -05001230 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /*
1233 * If this is a pass-through port deactivate parent so the device
1234 * connected to this port can be successfully identified
1235 */
1236 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1237 parent = serio_get_drvdata(serio->parent);
1238 psmouse_deactivate(parent);
1239 }
1240
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001241 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1242 input_dev = input_allocate_device();
1243 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001244 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001247 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001248 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001249 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1252
1253 serio_set_drvdata(serio, psmouse);
1254
Dmitry Torokhov72155612006-11-05 22:40:19 -05001255 error = serio_open(serio, drv);
1256 if (error)
1257 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001260 error = -ENODEV;
1261 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
1264 psmouse->rate = psmouse_rate;
1265 psmouse->resolution = psmouse_resolution;
1266 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001267 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001270 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 psmouse_initialize(psmouse);
1274
Dmitry Torokhov72155612006-11-05 22:40:19 -05001275 error = input_register_device(psmouse->dev);
1276 if (error)
1277 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (parent && parent->pt_activate)
1280 parent->pt_activate(parent);
1281
Dmitry Torokhov72155612006-11-05 22:40:19 -05001282 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1283 if (error)
1284 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 psmouse_activate(psmouse);
1287
Dmitry Torokhov72155612006-11-05 22:40:19 -05001288 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001289 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (parent)
1291 psmouse_activate(parent);
1292
Ingo Molnarc14471d2006-02-19 00:22:11 -05001293 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001295
1296 err_pt_deactivate:
1297 if (parent && parent->pt_deactivate)
1298 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001299 input_unregister_device(psmouse->dev);
1300 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001301 err_protocol_disconnect:
1302 if (psmouse->disconnect)
1303 psmouse->disconnect(psmouse);
1304 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1305 err_close_serio:
1306 serio_close(serio);
1307 err_clear_drvdata:
1308 serio_set_drvdata(serio, NULL);
1309 err_free:
1310 input_free_device(input_dev);
1311 kfree(psmouse);
1312
1313 retval = error;
1314 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317
1318static int psmouse_reconnect(struct serio *serio)
1319{
1320 struct psmouse *psmouse = serio_get_drvdata(serio);
1321 struct psmouse *parent = NULL;
1322 struct serio_driver *drv = serio->drv;
1323 int rc = -1;
1324
1325 if (!drv || !psmouse) {
1326 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1327 return -1;
1328 }
1329
Ingo Molnarc14471d2006-02-19 00:22:11 -05001330 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1333 parent = serio_get_drvdata(serio->parent);
1334 psmouse_deactivate(parent);
1335 }
1336
1337 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1338
1339 if (psmouse->reconnect) {
1340 if (psmouse->reconnect(psmouse))
1341 goto out;
1342 } else if (psmouse_probe(psmouse) < 0 ||
1343 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1344 goto out;
1345
1346 /* ok, the device type (and capabilities) match the old one,
1347 * we can continue using it, complete intialization
1348 */
1349 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1350
1351 psmouse_initialize(psmouse);
1352
1353 if (parent && parent->pt_activate)
1354 parent->pt_activate(parent);
1355
1356 psmouse_activate(psmouse);
1357 rc = 0;
1358
1359out:
1360 /* If this is a pass-through port the parent waits to be activated */
1361 if (parent)
1362 psmouse_activate(parent);
1363
Ingo Molnarc14471d2006-02-19 00:22:11 -05001364 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return rc;
1366}
1367
1368static struct serio_device_id psmouse_serio_ids[] = {
1369 {
1370 .type = SERIO_8042,
1371 .proto = SERIO_ANY,
1372 .id = SERIO_ANY,
1373 .extra = SERIO_ANY,
1374 },
1375 {
1376 .type = SERIO_PS_PSTHRU,
1377 .proto = SERIO_ANY,
1378 .id = SERIO_ANY,
1379 .extra = SERIO_ANY,
1380 },
1381 { 0 }
1382};
1383
1384MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1385
1386static struct serio_driver psmouse_drv = {
1387 .driver = {
1388 .name = "psmouse",
1389 },
1390 .description = DRIVER_DESC,
1391 .id_table = psmouse_serio_ids,
1392 .interrupt = psmouse_interrupt,
1393 .connect = psmouse_connect,
1394 .reconnect = psmouse_reconnect,
1395 .disconnect = psmouse_disconnect,
1396 .cleanup = psmouse_cleanup,
1397};
1398
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001399ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1400 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001403 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1404 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 int retval;
1406
1407 retval = serio_pin_driver(serio);
1408 if (retval)
1409 return retval;
1410
1411 if (serio->drv != &psmouse_drv) {
1412 retval = -ENODEV;
1413 goto out;
1414 }
1415
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001416 psmouse = serio_get_drvdata(serio);
1417
1418 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420out:
1421 serio_unpin_driver(serio);
1422 return retval;
1423}
1424
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001425ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1426 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001429 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1430 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int retval;
1432
1433 retval = serio_pin_driver(serio);
1434 if (retval)
1435 return retval;
1436
1437 if (serio->drv != &psmouse_drv) {
1438 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001439 goto out_unpin;
1440 }
1441
Ingo Molnarc14471d2006-02-19 00:22:11 -05001442 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001443 if (retval)
1444 goto out_unpin;
1445
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001446 psmouse = serio_get_drvdata(serio);
1447
Andres Salomon68d48222008-09-16 12:30:34 -04001448 if (attr->protect) {
1449 if (psmouse->state == PSMOUSE_IGNORE) {
1450 retval = -ENODEV;
1451 goto out_unlock;
1452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Andres Salomon68d48222008-09-16 12:30:34 -04001454 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1455 parent = serio_get_drvdata(serio->parent);
1456 psmouse_deactivate(parent);
1457 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001458
Andres Salomon68d48222008-09-16 12:30:34 -04001459 psmouse_deactivate(psmouse);
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001462 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Andres Salomon68d48222008-09-16 12:30:34 -04001464 if (attr->protect) {
1465 if (retval != -ENODEV)
1466 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001467
Andres Salomon68d48222008-09-16 12:30:34 -04001468 if (parent)
1469 psmouse_activate(parent);
1470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Ingo Molnarc14471d2006-02-19 00:22:11 -05001472 out_unlock:
1473 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001474 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 serio_unpin_driver(serio);
1476 return retval;
1477}
1478
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001479static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1480{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001481 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001482
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001483 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001484}
1485
1486static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1487{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001488 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001489 unsigned long value;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001490
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001491 if (strict_strtoul(buf, 10, &value))
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001492 return -EINVAL;
1493
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001494 if ((unsigned int)value != value)
1495 return -EINVAL;
1496
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001497 *field = value;
1498
1499 return count;
1500}
1501
1502static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001503{
1504 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1505}
1506
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001507static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001508{
1509 struct serio *serio = psmouse->ps2dev.serio;
1510 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001511 struct input_dev *old_dev, *new_dev;
1512 const struct psmouse_protocol *proto, *old_proto;
1513 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001514 int retry = 0;
1515
Dmitry Torokhov72155612006-11-05 22:40:19 -05001516 proto = psmouse_protocol_by_name(buf, count);
1517 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001518 return -EINVAL;
1519
1520 if (psmouse->type == proto->type)
1521 return count;
1522
Dmitry Torokhov72155612006-11-05 22:40:19 -05001523 new_dev = input_allocate_device();
1524 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001525 return -ENOMEM;
1526
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001527 while (serio->child) {
1528 if (++retry > 3) {
1529 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001530 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001531 return -EIO;
1532 }
1533
Ingo Molnarc14471d2006-02-19 00:22:11 -05001534 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001535 serio_unpin_driver(serio);
1536 serio_unregister_child_port(serio);
1537 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001538 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001539
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001540 if (serio->drv != &psmouse_drv) {
1541 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001542 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001543 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001544
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001545 if (psmouse->type == proto->type) {
1546 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001547 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001548 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001549 }
1550
1551 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1552 parent = serio_get_drvdata(serio->parent);
1553 if (parent->pt_deactivate)
1554 parent->pt_deactivate(parent);
1555 }
1556
Dmitry Torokhov72155612006-11-05 22:40:19 -05001557 old_dev = psmouse->dev;
1558 old_proto = psmouse_protocol_by_type(psmouse->type);
1559
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001560 if (psmouse->disconnect)
1561 psmouse->disconnect(psmouse);
1562
1563 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001564
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001565 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001566 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1567
1568 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1569 psmouse_reset(psmouse);
1570 /* default to PSMOUSE_PS2 */
1571 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1572 }
1573
1574 psmouse_initialize(psmouse);
1575 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1576
Dmitry Torokhov72155612006-11-05 22:40:19 -05001577 error = input_register_device(psmouse->dev);
1578 if (error) {
1579 if (psmouse->disconnect)
1580 psmouse->disconnect(psmouse);
1581
1582 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1583 input_free_device(new_dev);
1584 psmouse->dev = old_dev;
1585 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1586 psmouse_switch_protocol(psmouse, old_proto);
1587 psmouse_initialize(psmouse);
1588 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1589
1590 return error;
1591 }
1592
1593 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001594
1595 if (parent && parent->pt_activate)
1596 parent->pt_activate(parent);
1597
1598 return count;
1599}
1600
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001601static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001605 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return -EINVAL;
1607
1608 psmouse->set_rate(psmouse, value);
1609 return count;
1610}
1611
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001612static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001616 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return -EINVAL;
1618
1619 psmouse->set_resolution(psmouse, value);
1620 return count;
1621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1625{
Helge Dellere38de672006-09-10 21:54:39 -04001626 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 if (!val)
1629 return -EINVAL;
1630
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001631 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001633 if (!proto || !proto->maxproto)
1634 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001636 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Stephen Evanchik541e3162005-08-08 01:26:18 -05001638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1642{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001643 int type = *((unsigned int *)kp->arg);
1644
1645 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
1648static int __init psmouse_init(void)
1649{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001650 int err;
1651
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001652 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1653 if (!kpsmoused_wq) {
1654 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1655 return -ENOMEM;
1656 }
1657
Akinobu Mita153a9df02006-11-23 23:35:10 -05001658 err = serio_register_driver(&psmouse_drv);
1659 if (err)
1660 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001661
Akinobu Mita153a9df02006-11-23 23:35:10 -05001662 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
1665static void __exit psmouse_exit(void)
1666{
1667 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001668 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
1671module_init(psmouse_init);
1672module_exit(psmouse_exit);