blob: b407b355dcebb6ff8e67617047d8878d742d1ef5 [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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-01-14 00:27:37 -0500244{
David Howells7d12e782006-10-05 14:55:46 +0100245 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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
Zephaniah E. Hull535650f2009-05-14 22:02:33 -0700330 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
331 (psmouse->type == PSMOUSE_HGPK &&
332 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500333 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
334 serio_reconnect(serio);
335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500337/*
338 * Not a new device, try processing first byte normally
339 */
340 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100341 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500342 goto out;
343
344 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500347/*
348 * See if we need to force resync because mouse was idle for too long
349 */
350 if (psmouse->state == PSMOUSE_ACTIVATED &&
351 psmouse->pktcnt == 1 && psmouse->resync_time &&
352 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
353 psmouse->badbyte = psmouse->packet[0];
354 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400355 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500356 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500358
359 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100360 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500361
362 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return IRQ_HANDLED;
364}
365
366
367/*
368 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
369 * using sliced syntax, understood by advanced devices, such as Logitech
370 * or Synaptics touchpads. The command is encoded as:
371 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
372 * is the command.
373 */
374int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
375{
376 int i;
377
378 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
379 return -1;
380
381 for (i = 6; i >= 0; i -= 2) {
382 unsigned char d = (command >> i) & 3;
383 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
384 return -1;
385 }
386
387 return 0;
388}
389
390
391/*
392 * psmouse_reset() resets the mouse into power-on state.
393 */
394int psmouse_reset(struct psmouse *psmouse)
395{
396 unsigned char param[2];
397
398 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
399 return -1;
400
401 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
402 return -1;
403
404 return 0;
405}
406
407
408/*
409 * Genius NetMouse magic init.
410 */
411static int genius_detect(struct psmouse *psmouse, int set_properties)
412{
413 struct ps2dev *ps2dev = &psmouse->ps2dev;
414 unsigned char param[4];
415
416 param[0] = 3;
417 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
418 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
419 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
420 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
421 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
422
423 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
424 return -1;
425
426 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500427 set_bit(BTN_EXTRA, psmouse->dev->keybit);
428 set_bit(BTN_SIDE, psmouse->dev->keybit);
429 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500432 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 psmouse->pktsize = 4;
434 }
435
436 return 0;
437}
438
439/*
440 * IntelliMouse magic init.
441 */
442static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
443{
444 struct ps2dev *ps2dev = &psmouse->ps2dev;
445 unsigned char param[2];
446
447 param[0] = 200;
448 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
449 param[0] = 100;
450 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
451 param[0] = 80;
452 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
453 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
454
455 if (param[0] != 3)
456 return -1;
457
458 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500459 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
460 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (!psmouse->vendor) psmouse->vendor = "Generic";
463 if (!psmouse->name) psmouse->name = "Wheel Mouse";
464 psmouse->pktsize = 4;
465 }
466
467 return 0;
468}
469
470/*
471 * Try IntelliMouse/Explorer magic init.
472 */
473static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
474{
475 struct ps2dev *ps2dev = &psmouse->ps2dev;
476 unsigned char param[2];
477
478 intellimouse_detect(psmouse, 0);
479
480 param[0] = 200;
481 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
482 param[0] = 200;
483 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
484 param[0] = 80;
485 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
486 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
487
488 if (param[0] != 4)
489 return -1;
490
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400491/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
492 param[0] = 200;
493 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
494 param[0] = 80;
495 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
496 param[0] = 40;
497 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500500 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
501 set_bit(REL_WHEEL, psmouse->dev->relbit);
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400502 set_bit(REL_HWHEEL, psmouse->dev->relbit);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500503 set_bit(BTN_SIDE, psmouse->dev->keybit);
504 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (!psmouse->vendor) psmouse->vendor = "Generic";
507 if (!psmouse->name) psmouse->name = "Explorer Mouse";
508 psmouse->pktsize = 4;
509 }
510
511 return 0;
512}
513
514/*
515 * Kensington ThinkingMouse / ExpertMouse magic init.
516 */
517static int thinking_detect(struct psmouse *psmouse, int set_properties)
518{
519 struct ps2dev *ps2dev = &psmouse->ps2dev;
520 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400521 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int i;
523
524 param[0] = 10;
525 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
526 param[0] = 0;
527 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400528 for (i = 0; i < ARRAY_SIZE(seq); i++) {
529 param[0] = seq[i];
530 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
533
534 if (param[0] != 2)
535 return -1;
536
537 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500538 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 psmouse->vendor = "Kensington";
541 psmouse->name = "ThinkingMouse";
542 }
543
544 return 0;
545}
546
547/*
548 * Bare PS/2 protocol "detection". Always succeeds.
549 */
550static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
551{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500552 if (set_properties) {
553 if (!psmouse->vendor) psmouse->vendor = "Generic";
554 if (!psmouse->name) psmouse->name = "Mouse";
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 return 0;
558}
559
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400560/*
561 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
562 * must be forced by sysfs protocol writing.
563 */
564static int cortron_detect(struct psmouse *psmouse, int set_properties)
565{
566 if (set_properties) {
567 psmouse->vendor = "Cortron";
568 psmouse->name = "PS/2 Trackball";
569 set_bit(BTN_SIDE, psmouse->dev->keybit);
570 }
571
572 return 0;
573}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/*
576 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
577 * the mouse may have.
578 */
579
580static int psmouse_extensions(struct psmouse *psmouse,
581 unsigned int max_proto, int set_properties)
582{
583 int synaptics_hardware = 0;
584
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500585/*
586 * We always check for lifebook because it does not disturb mouse
587 * (it only checks DMI information).
588 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500589 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500590 if (max_proto > PSMOUSE_IMEX) {
591 if (!set_properties || lifebook_init(psmouse) == 0)
592 return PSMOUSE_LIFEBOOK;
593 }
594 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/*
597 * Try Kensington ThinkingMouse (we try first, because synaptics probe
598 * upsets the thinkingmouse).
599 */
600
601 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
602 return PSMOUSE_THINKPS;
603
604/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500605 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
606 * support is disabled in config - we need to know if it is synaptics so we
607 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
609 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
610 synaptics_hardware = 1;
611
612 if (max_proto > PSMOUSE_IMEX) {
613 if (!set_properties || synaptics_init(psmouse) == 0)
614 return PSMOUSE_SYNAPTICS;
615/*
616 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
617 * Unfortunately Logitech/Genius probes confuse some firmware versions so
618 * we'll have to skip them.
619 */
620 max_proto = PSMOUSE_IMEX;
621 }
622/*
623 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
624 */
625 synaptics_reset(psmouse);
626 }
627
628/*
629 * Try ALPS TouchPad
630 */
631 if (max_proto > PSMOUSE_IMEX) {
632 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
633 if (alps_detect(psmouse, set_properties) == 0) {
634 if (!set_properties || alps_init(psmouse) == 0)
635 return PSMOUSE_ALPS;
636/*
637 * Init failed, try basic relative protocols
638 */
639 max_proto = PSMOUSE_IMEX;
640 }
641 }
642
Andres Salomondf08ef22008-09-16 12:30:34 -0400643/*
644 * Try OLPC HGPK touchpad.
645 */
646 if (max_proto > PSMOUSE_IMEX &&
647 hgpk_detect(psmouse, set_properties) == 0) {
648 if (!set_properties || hgpk_init(psmouse) == 0)
649 return PSMOUSE_HGPK;
650/*
651 * Init failed, try basic relative protocols
652 */
653 max_proto = PSMOUSE_IMEX;
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400656/*
657 * Try Elantech touchpad.
658 */
659 if (max_proto > PSMOUSE_IMEX &&
660 elantech_detect(psmouse, set_properties) == 0) {
661 if (!set_properties || elantech_init(psmouse) == 0)
662 return PSMOUSE_ELANTECH;
663/*
664 * Init failed, try basic relative protocols
665 */
666 max_proto = PSMOUSE_IMEX;
667 }
668
Andres Salomondf08ef22008-09-16 12:30:34 -0400669 if (max_proto > PSMOUSE_IMEX) {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500670 if (genius_detect(psmouse, set_properties) == 0)
671 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500673 if (ps2pp_init(psmouse, set_properties) == 0)
674 return PSMOUSE_PS2PP;
675
676 if (trackpoint_detect(psmouse, set_properties) == 0)
677 return PSMOUSE_TRACKPOINT;
678
679 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
680 return PSMOUSE_TOUCHKIT_PS2;
681 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/*
684 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400685 * protocol probes. Note that we follow up with full reset because
686 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Alon Ziv554fc192007-08-30 00:22:48 -0400688 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500689 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
692 return PSMOUSE_IMEX;
693
694 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
695 return PSMOUSE_IMPS;
696
697/*
698 * Okay, all failed, we have a standard mouse here. The number of the buttons
699 * is still a question, though. We assume 3.
700 */
701 ps2bare_detect(psmouse, set_properties);
702
703 if (synaptics_hardware) {
704/*
705 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
706 * We need to reset the touchpad because if there is a track point on the
707 * pass through port it could get disabled while probing for protocol
708 * extensions.
709 */
710 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
713 return PSMOUSE_PS2;
714}
715
Helge Dellere38de672006-09-10 21:54:39 -0400716static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500717 {
718 .type = PSMOUSE_PS2,
719 .name = "PS/2",
720 .alias = "bare",
721 .maxproto = 1,
722 .detect = ps2bare_detect,
723 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500724#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500725 {
726 .type = PSMOUSE_PS2PP,
727 .name = "PS2++",
728 .alias = "logitech",
729 .detect = ps2pp_init,
730 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500731#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500732 {
733 .type = PSMOUSE_THINKPS,
734 .name = "ThinkPS/2",
735 .alias = "thinkps",
736 .detect = thinking_detect,
737 },
738 {
739 .type = PSMOUSE_GENPS,
740 .name = "GenPS/2",
741 .alias = "genius",
742 .detect = genius_detect,
743 },
744 {
745 .type = PSMOUSE_IMPS,
746 .name = "ImPS/2",
747 .alias = "imps",
748 .maxproto = 1,
749 .detect = intellimouse_detect,
750 },
751 {
752 .type = PSMOUSE_IMEX,
753 .name = "ImExPS/2",
754 .alias = "exps",
755 .maxproto = 1,
756 .detect = im_explorer_detect,
757 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500758#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500759 {
760 .type = PSMOUSE_SYNAPTICS,
761 .name = "SynPS/2",
762 .alias = "synaptics",
763 .detect = synaptics_detect,
764 .init = synaptics_init,
765 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500766#endif
767#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500768 {
769 .type = PSMOUSE_ALPS,
770 .name = "AlpsPS/2",
771 .alias = "alps",
772 .detect = alps_detect,
773 .init = alps_init,
774 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500775#endif
776#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500777 {
778 .type = PSMOUSE_LIFEBOOK,
779 .name = "LBPS/2",
780 .alias = "lifebook",
781 .init = lifebook_init,
782 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500783#endif
784#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500785 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500786 .type = PSMOUSE_TRACKPOINT,
787 .name = "TPPS/2",
788 .alias = "trackpoint",
789 .detect = trackpoint_detect,
790 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500791#endif
792#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500793 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500794 .type = PSMOUSE_TOUCHKIT_PS2,
795 .name = "touchkitPS/2",
796 .alias = "touchkit",
797 .detect = touchkit_ps2_detect,
798 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500799#endif
Andres Salomondf08ef22008-09-16 12:30:34 -0400800#ifdef CONFIG_MOUSE_PS2_OLPC
801 {
802 .type = PSMOUSE_HGPK,
803 .name = "OLPC HGPK",
804 .alias = "hgpk",
805 .detect = hgpk_detect,
806 },
807#endif
Arjan Opmeer2a0bd752008-10-16 22:10:19 -0400808#ifdef CONFIG_MOUSE_PS2_ELANTECH
809 {
810 .type = PSMOUSE_ELANTECH,
811 .name = "ETPS/2",
812 .alias = "elantech",
813 .detect = elantech_detect,
814 .init = elantech_init,
815 },
816 #endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500817 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400818 .type = PSMOUSE_CORTRON,
819 .name = "CortronPS/2",
820 .alias = "cortps",
821 .detect = cortron_detect,
822 },
823 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500824 .type = PSMOUSE_AUTO,
825 .name = "auto",
826 .alias = "any",
827 .maxproto = 1,
828 },
829};
830
Helge Dellere38de672006-09-10 21:54:39 -0400831static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500832{
833 int i;
834
835 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
836 if (psmouse_protocols[i].type == type)
837 return &psmouse_protocols[i];
838
839 WARN_ON(1);
840 return &psmouse_protocols[0];
841}
842
Helge Dellere38de672006-09-10 21:54:39 -0400843static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500844{
Helge Dellere38de672006-09-10 21:54:39 -0400845 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500846 int i;
847
848 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
849 p = &psmouse_protocols[i];
850
851 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
852 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
853 return &psmouse_protocols[i];
854 }
855
856 return NULL;
857}
858
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860/*
861 * psmouse_probe() probes for a PS/2 mouse.
862 */
863
864static int psmouse_probe(struct psmouse *psmouse)
865{
866 struct ps2dev *ps2dev = &psmouse->ps2dev;
867 unsigned char param[2];
868
869/*
870 * First, we check if it's a mouse. It should send 0x00 or 0x03
871 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500872 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
873 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 */
875
876 param[0] = 0xa5;
877 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
878 return -1;
879
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500880 if (param[0] != 0x00 && param[0] != 0x03 &&
881 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return -1;
883
884/*
885 * Then we reset and disable the mouse so that it doesn't generate events.
886 */
887
888 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
889 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
890
891 return 0;
892}
893
894/*
895 * Here we set the mouse resolution.
896 */
897
898void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
899{
Helge Dellere38de672006-09-10 21:54:39 -0400900 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
901 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (resolution == 0 || resolution > 200)
904 resolution = 200;
905
Helge Dellere38de672006-09-10 21:54:39 -0400906 p = params[resolution / 50];
907 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
908 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911/*
912 * Here we set the mouse report rate.
913 */
914
915static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
916{
Helge Dellere38de672006-09-10 21:54:39 -0400917 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
918 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 int i = 0;
920
921 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400922 r = rates[i];
923 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
924 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927/*
928 * psmouse_initialize() initializes the mouse to a sane state.
929 */
930
931static void psmouse_initialize(struct psmouse *psmouse)
932{
933/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * We set the mouse report rate, resolution and scaling.
935 */
936
937 if (psmouse_max_proto != PSMOUSE_PS2) {
938 psmouse->set_rate(psmouse, psmouse->rate);
939 psmouse->set_resolution(psmouse, psmouse->resolution);
940 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
941 }
942}
943
944/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * psmouse_activate() enables the mouse so that we get motion reports from it.
946 */
947
948static void psmouse_activate(struct psmouse *psmouse)
949{
950 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
951 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
952 psmouse->ps2dev.serio->phys);
953
954 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
955}
956
957
958/*
959 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +0200960 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
962
963static void psmouse_deactivate(struct psmouse *psmouse)
964{
965 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
966 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
967 psmouse->ps2dev.serio->phys);
968
969 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
970}
971
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500972/*
973 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
974 */
975
976static int psmouse_poll(struct psmouse *psmouse)
977{
978 return ps2_command(&psmouse->ps2dev, psmouse->packet,
979 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
980}
981
982
983/*
984 * psmouse_resync() attempts to re-validate current protocol.
985 */
986
David Howellsc4028952006-11-22 14:57:56 +0000987static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500988{
David Howellsc4028952006-11-22 14:57:56 +0000989 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -0400990 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500991 struct serio *serio = psmouse->ps2dev.serio;
992 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
993 int failed = 0, enabled = 0;
994 int i;
995
Ingo Molnarc14471d2006-02-19 00:22:11 -0500996 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500997
998 if (psmouse->state != PSMOUSE_RESYNCING)
999 goto out;
1000
1001 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1002 parent = serio_get_drvdata(serio->parent);
1003 psmouse_deactivate(parent);
1004 }
1005
1006/*
1007 * Some mice don't ACK commands sent while they are in the middle of
1008 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1009 * instead of ps2_command() which would wait for 200ms for an ACK
1010 * that may never come.
1011 * As an additional quirk ALPS touchpads may not only forget to ACK
1012 * disable command but will stop reporting taps, so if we see that
1013 * mouse at least once ACKs disable we will do full reconnect if ACK
1014 * is missing.
1015 */
1016 psmouse->num_resyncs++;
1017
1018 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1019 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1020 failed = 1;
1021 } else
1022 psmouse->acks_disable_command = 1;
1023
1024/*
1025 * Poll the mouse. If it was reset the packet will be shorter than
1026 * psmouse->pktsize and ps2_command will fail. We do not expect and
1027 * do not handle scenario when mouse "upgrades" its protocol while
1028 * disconnected since it would require additional delay. If we ever
1029 * see a mouse that does it we'll adjust the code.
1030 */
1031 if (!failed) {
1032 if (psmouse->poll(psmouse))
1033 failed = 1;
1034 else {
1035 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1036 for (i = 0; i < psmouse->pktsize; i++) {
1037 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001038 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001039 if (rc != PSMOUSE_GOOD_DATA)
1040 break;
1041 }
1042 if (rc != PSMOUSE_FULL_PACKET)
1043 failed = 1;
1044 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1045 }
1046 }
1047/*
1048 * Now try to enable mouse. We try to do that even if poll failed and also
1049 * repeat our attempts 5 times, otherwise we may be left out with disabled
1050 * mouse.
1051 */
1052 for (i = 0; i < 5; i++) {
1053 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1054 enabled = 1;
1055 break;
1056 }
1057 msleep(200);
1058 }
1059
1060 if (!enabled) {
1061 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1062 psmouse->ps2dev.serio->phys);
1063 failed = 1;
1064 }
1065
1066 if (failed) {
1067 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1068 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1069 serio_reconnect(serio);
1070 } else
1071 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1072
1073 if (parent)
1074 psmouse_activate(parent);
1075 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001076 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001077}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079/*
1080 * psmouse_cleanup() resets the mouse into power-on state.
1081 */
1082
1083static void psmouse_cleanup(struct serio *serio)
1084{
1085 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001086 struct psmouse *parent = NULL;
1087
1088 mutex_lock(&psmouse_mutex);
1089
1090 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1091 parent = serio_get_drvdata(serio->parent);
1092 psmouse_deactivate(parent);
1093 }
1094
1095 psmouse_deactivate(psmouse);
1096
1097 if (psmouse->cleanup)
1098 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 psmouse_reset(psmouse);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001101
1102/*
1103 * Some boxes, such as HP nx7400, get terribly confused if mouse
1104 * is not fully enabled before suspending/shutting down.
1105 */
1106 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1107
1108 if (parent) {
1109 if (parent->pt_deactivate)
1110 parent->pt_deactivate(parent);
1111
1112 psmouse_activate(parent);
1113 }
1114
1115 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118/*
1119 * psmouse_disconnect() closes and frees.
1120 */
1121
1122static void psmouse_disconnect(struct serio *serio)
1123{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001124 struct psmouse *psmouse, *parent = NULL;
1125
1126 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001128 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Ingo Molnarc14471d2006-02-19 00:22:11 -05001130 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1133
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001134 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001135 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001136 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001137 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1140 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001141 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 if (psmouse->disconnect)
1145 psmouse->disconnect(psmouse);
1146
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001147 if (parent && parent->pt_deactivate)
1148 parent->pt_deactivate(parent);
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 serio_close(serio);
1153 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001154 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001156
1157 if (parent)
1158 psmouse_activate(parent);
1159
Ingo Molnarc14471d2006-02-19 00:22:11 -05001160 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Helge Dellere38de672006-09-10 21:54:39 -04001163static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001164{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001165 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001166
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001167 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001168
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001169 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1170 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
1171 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1172 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001173
1174 psmouse->set_rate = psmouse_set_rate;
1175 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001176 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001177 psmouse->protocol_handler = psmouse_process_byte;
1178 psmouse->pktsize = 3;
1179
1180 if (proto && (proto->detect || proto->init)) {
1181 if (proto->detect && proto->detect(psmouse, 1) < 0)
1182 return -1;
1183
1184 if (proto->init && proto->init(psmouse) < 0)
1185 return -1;
1186
1187 psmouse->type = proto->type;
1188 }
1189 else
1190 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1191
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001192 /*
1193 * If mouse's packet size is 3 there is no point in polling the
1194 * device in hopes to detect protocol reset - we won't get less
1195 * than 3 bytes response anyhow.
1196 */
1197 if (psmouse->pktsize == 3)
1198 psmouse->resync_time = 0;
1199
1200 /*
1201 * Some smart KVMs fake response to POLL command returning just
1202 * 3 bytes and messing up our resync logic, so if initial poll
1203 * fails we won't try polling the device anymore. Hopefully
1204 * such KVM will maintain initially selected protocol.
1205 */
1206 if (psmouse->resync_time && psmouse->poll(psmouse))
1207 psmouse->resync_time = 0;
1208
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001209 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1210 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001211
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001212 input_dev->name = psmouse->devname;
1213 input_dev->phys = psmouse->phys;
1214 input_dev->id.bustype = BUS_I8042;
1215 input_dev->id.vendor = 0x0002;
1216 input_dev->id.product = psmouse->type;
1217 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001218
1219 return 0;
1220}
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222/*
1223 * psmouse_connect() is a callback from the serio module when
1224 * an unhandled serio port is found.
1225 */
1226static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1227{
1228 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001229 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001230 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Ingo Molnarc14471d2006-02-19 00:22:11 -05001232 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * If this is a pass-through port deactivate parent so the device
1236 * connected to this port can be successfully identified
1237 */
1238 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1239 parent = serio_get_drvdata(serio->parent);
1240 psmouse_deactivate(parent);
1241 }
1242
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001243 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1244 input_dev = input_allocate_device();
1245 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001246 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001249 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001250 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001251 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1254
1255 serio_set_drvdata(serio, psmouse);
1256
Dmitry Torokhov72155612006-11-05 22:40:19 -05001257 error = serio_open(serio, drv);
1258 if (error)
1259 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001262 error = -ENODEV;
1263 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
1266 psmouse->rate = psmouse_rate;
1267 psmouse->resolution = psmouse_resolution;
1268 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001269 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001272 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 psmouse_initialize(psmouse);
1276
Dmitry Torokhov72155612006-11-05 22:40:19 -05001277 error = input_register_device(psmouse->dev);
1278 if (error)
1279 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (parent && parent->pt_activate)
1282 parent->pt_activate(parent);
1283
Dmitry Torokhov72155612006-11-05 22:40:19 -05001284 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1285 if (error)
1286 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 psmouse_activate(psmouse);
1289
Dmitry Torokhov72155612006-11-05 22:40:19 -05001290 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001291 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (parent)
1293 psmouse_activate(parent);
1294
Ingo Molnarc14471d2006-02-19 00:22:11 -05001295 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001297
1298 err_pt_deactivate:
1299 if (parent && parent->pt_deactivate)
1300 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001301 input_unregister_device(psmouse->dev);
1302 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001303 err_protocol_disconnect:
1304 if (psmouse->disconnect)
1305 psmouse->disconnect(psmouse);
1306 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1307 err_close_serio:
1308 serio_close(serio);
1309 err_clear_drvdata:
1310 serio_set_drvdata(serio, NULL);
1311 err_free:
1312 input_free_device(input_dev);
1313 kfree(psmouse);
1314
1315 retval = error;
1316 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
1319
1320static int psmouse_reconnect(struct serio *serio)
1321{
1322 struct psmouse *psmouse = serio_get_drvdata(serio);
1323 struct psmouse *parent = NULL;
1324 struct serio_driver *drv = serio->drv;
1325 int rc = -1;
1326
1327 if (!drv || !psmouse) {
1328 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1329 return -1;
1330 }
1331
Ingo Molnarc14471d2006-02-19 00:22:11 -05001332 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1335 parent = serio_get_drvdata(serio->parent);
1336 psmouse_deactivate(parent);
1337 }
1338
1339 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1340
1341 if (psmouse->reconnect) {
1342 if (psmouse->reconnect(psmouse))
1343 goto out;
1344 } else if (psmouse_probe(psmouse) < 0 ||
1345 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1346 goto out;
1347
1348 /* ok, the device type (and capabilities) match the old one,
1349 * we can continue using it, complete intialization
1350 */
1351 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1352
1353 psmouse_initialize(psmouse);
1354
1355 if (parent && parent->pt_activate)
1356 parent->pt_activate(parent);
1357
1358 psmouse_activate(psmouse);
1359 rc = 0;
1360
1361out:
1362 /* If this is a pass-through port the parent waits to be activated */
1363 if (parent)
1364 psmouse_activate(parent);
1365
Ingo Molnarc14471d2006-02-19 00:22:11 -05001366 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return rc;
1368}
1369
1370static struct serio_device_id psmouse_serio_ids[] = {
1371 {
1372 .type = SERIO_8042,
1373 .proto = SERIO_ANY,
1374 .id = SERIO_ANY,
1375 .extra = SERIO_ANY,
1376 },
1377 {
1378 .type = SERIO_PS_PSTHRU,
1379 .proto = SERIO_ANY,
1380 .id = SERIO_ANY,
1381 .extra = SERIO_ANY,
1382 },
1383 { 0 }
1384};
1385
1386MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1387
1388static struct serio_driver psmouse_drv = {
1389 .driver = {
1390 .name = "psmouse",
1391 },
1392 .description = DRIVER_DESC,
1393 .id_table = psmouse_serio_ids,
1394 .interrupt = psmouse_interrupt,
1395 .connect = psmouse_connect,
1396 .reconnect = psmouse_reconnect,
1397 .disconnect = psmouse_disconnect,
1398 .cleanup = psmouse_cleanup,
1399};
1400
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001401ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1402 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001405 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1406 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int retval;
1408
1409 retval = serio_pin_driver(serio);
1410 if (retval)
1411 return retval;
1412
1413 if (serio->drv != &psmouse_drv) {
1414 retval = -ENODEV;
1415 goto out;
1416 }
1417
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001418 psmouse = serio_get_drvdata(serio);
1419
1420 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422out:
1423 serio_unpin_driver(serio);
1424 return retval;
1425}
1426
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001427ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1428 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001431 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1432 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int retval;
1434
1435 retval = serio_pin_driver(serio);
1436 if (retval)
1437 return retval;
1438
1439 if (serio->drv != &psmouse_drv) {
1440 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001441 goto out_unpin;
1442 }
1443
Ingo Molnarc14471d2006-02-19 00:22:11 -05001444 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001445 if (retval)
1446 goto out_unpin;
1447
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001448 psmouse = serio_get_drvdata(serio);
1449
Andres Salomon68d48222008-09-16 12:30:34 -04001450 if (attr->protect) {
1451 if (psmouse->state == PSMOUSE_IGNORE) {
1452 retval = -ENODEV;
1453 goto out_unlock;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Andres Salomon68d48222008-09-16 12:30:34 -04001456 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1457 parent = serio_get_drvdata(serio->parent);
1458 psmouse_deactivate(parent);
1459 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001460
Andres Salomon68d48222008-09-16 12:30:34 -04001461 psmouse_deactivate(psmouse);
1462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001464 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Andres Salomon68d48222008-09-16 12:30:34 -04001466 if (attr->protect) {
1467 if (retval != -ENODEV)
1468 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001469
Andres Salomon68d48222008-09-16 12:30:34 -04001470 if (parent)
1471 psmouse_activate(parent);
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Ingo Molnarc14471d2006-02-19 00:22:11 -05001474 out_unlock:
1475 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001476 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 serio_unpin_driver(serio);
1478 return retval;
1479}
1480
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001481static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1482{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001483 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001484
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001485 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001486}
1487
1488static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1489{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001490 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001491 unsigned long value;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001492
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001493 if (strict_strtoul(buf, 10, &value))
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001494 return -EINVAL;
1495
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001496 if ((unsigned int)value != value)
1497 return -EINVAL;
1498
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001499 *field = value;
1500
1501 return count;
1502}
1503
1504static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001505{
1506 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1507}
1508
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001509static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001510{
1511 struct serio *serio = psmouse->ps2dev.serio;
1512 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001513 struct input_dev *old_dev, *new_dev;
1514 const struct psmouse_protocol *proto, *old_proto;
1515 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001516 int retry = 0;
1517
Dmitry Torokhov72155612006-11-05 22:40:19 -05001518 proto = psmouse_protocol_by_name(buf, count);
1519 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001520 return -EINVAL;
1521
1522 if (psmouse->type == proto->type)
1523 return count;
1524
Dmitry Torokhov72155612006-11-05 22:40:19 -05001525 new_dev = input_allocate_device();
1526 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001527 return -ENOMEM;
1528
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001529 while (serio->child) {
1530 if (++retry > 3) {
1531 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001532 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001533 return -EIO;
1534 }
1535
Ingo Molnarc14471d2006-02-19 00:22:11 -05001536 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001537 serio_unpin_driver(serio);
1538 serio_unregister_child_port(serio);
1539 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001540 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001541
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001542 if (serio->drv != &psmouse_drv) {
1543 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001544 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001545 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001546
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001547 if (psmouse->type == proto->type) {
1548 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001549 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001550 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001551 }
1552
1553 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1554 parent = serio_get_drvdata(serio->parent);
1555 if (parent->pt_deactivate)
1556 parent->pt_deactivate(parent);
1557 }
1558
Dmitry Torokhov72155612006-11-05 22:40:19 -05001559 old_dev = psmouse->dev;
1560 old_proto = psmouse_protocol_by_type(psmouse->type);
1561
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001562 if (psmouse->disconnect)
1563 psmouse->disconnect(psmouse);
1564
1565 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001566
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001567 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001568 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1569
1570 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1571 psmouse_reset(psmouse);
1572 /* default to PSMOUSE_PS2 */
1573 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1574 }
1575
1576 psmouse_initialize(psmouse);
1577 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1578
Dmitry Torokhov72155612006-11-05 22:40:19 -05001579 error = input_register_device(psmouse->dev);
1580 if (error) {
1581 if (psmouse->disconnect)
1582 psmouse->disconnect(psmouse);
1583
1584 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1585 input_free_device(new_dev);
1586 psmouse->dev = old_dev;
1587 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1588 psmouse_switch_protocol(psmouse, old_proto);
1589 psmouse_initialize(psmouse);
1590 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1591
1592 return error;
1593 }
1594
1595 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001596
1597 if (parent && parent->pt_activate)
1598 parent->pt_activate(parent);
1599
1600 return count;
1601}
1602
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001603static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001607 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return -EINVAL;
1609
1610 psmouse->set_rate(psmouse, value);
1611 return count;
1612}
1613
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001614static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001618 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return -EINVAL;
1620
1621 psmouse->set_resolution(psmouse, value);
1622 return count;
1623}
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1627{
Helge Dellere38de672006-09-10 21:54:39 -04001628 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 if (!val)
1631 return -EINVAL;
1632
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001633 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001635 if (!proto || !proto->maxproto)
1636 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001638 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Stephen Evanchik541e3162005-08-08 01:26:18 -05001640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1644{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001645 int type = *((unsigned int *)kp->arg);
1646
1647 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650static int __init psmouse_init(void)
1651{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001652 int err;
1653
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001654 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1655 if (!kpsmoused_wq) {
1656 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1657 return -ENOMEM;
1658 }
1659
Akinobu Mita153a9df02006-11-23 23:35:10 -05001660 err = serio_register_driver(&psmouse_drv);
1661 if (err)
1662 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001663
Akinobu Mita153a9df02006-11-23 23:35:10 -05001664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
1667static void __exit psmouse_exit(void)
1668{
1669 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001670 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
1673module_init(psmouse_init);
1674module_exit(psmouse_exit);