blob: ad62174676761e095ba26e232db99e6df843e52d [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>
16#include <linux/moduleparam.h>
17#include <linux/slab.h>
18#include <linux/interrupt.h>
19#include <linux/input.h>
20#include <linux/serio.h>
21#include <linux/init.h>
22#include <linux/libps2.h>
23#include "psmouse.h"
24#include "synaptics.h"
25#include "logips2pp.h"
26#include "alps.h"
Kenan Esau02d7f582005-05-29 02:30:22 -050027#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050028#include "trackpoint.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define DRIVER_DESC "PS/2 mouse driver"
31
32MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
33MODULE_DESCRIPTION(DRIVER_DESC);
34MODULE_LICENSE("GPL");
35
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050036static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
38static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
40#define param_set_proto_abbrev psmouse_set_maxproto
41#define param_get_proto_abbrev psmouse_get_maxproto
42module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050043MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static unsigned int psmouse_resolution = 200;
46module_param_named(resolution, psmouse_resolution, uint, 0644);
47MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
48
49static unsigned int psmouse_rate = 100;
50module_param_named(rate, psmouse_rate, uint, 0644);
51MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
52
53static unsigned int psmouse_smartscroll = 1;
54module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
55MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
56
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050057static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058module_param_named(resetafter, psmouse_resetafter, uint, 0644);
59MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
60
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050061static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050062module_param_named(resync_time, psmouse_resync_time, uint, 0644);
63MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
64
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050065PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
66 NULL,
67 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
68PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
69 (void *) offsetof(struct psmouse, rate),
70 psmouse_show_int_attr, psmouse_attr_set_rate);
71PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
72 (void *) offsetof(struct psmouse, resolution),
73 psmouse_show_int_attr, psmouse_attr_set_resolution);
74PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
75 (void *) offsetof(struct psmouse, resetafter),
76 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050077PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
78 (void *) offsetof(struct psmouse, resync_time),
79 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050080
81static struct attribute *psmouse_attributes[] = {
82 &psmouse_attr_protocol.dattr.attr,
83 &psmouse_attr_rate.dattr.attr,
84 &psmouse_attr_resolution.dattr.attr,
85 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050086 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050087 NULL
88};
89
90static struct attribute_group psmouse_attribute_group = {
91 .attrs = psmouse_attributes,
92};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94__obsolete_setup("psmouse_noext");
95__obsolete_setup("psmouse_resolution=");
96__obsolete_setup("psmouse_smartscroll=");
97__obsolete_setup("psmouse_resetafter=");
98__obsolete_setup("psmouse_rate=");
99
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500100/*
101 * psmouse_sem protects all operations changing state of mouse
102 * (connecting, disconnecting, changing rate or resolution via
103 * sysfs). We could use a per-device semaphore but since there
104 * rarely more than one PS/2 mouse connected and since semaphore
105 * is taken in "slow" paths it is not worth it.
106 */
107static DECLARE_MUTEX(psmouse_sem);
108
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500109static struct workqueue_struct *kpsmoused_wq;
110
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500111struct psmouse_protocol {
112 enum psmouse_type type;
113 char *name;
114 char *alias;
115 int maxproto;
116 int (*detect)(struct psmouse *, int);
117 int (*init)(struct psmouse *);
118};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/*
121 * psmouse_process_byte() analyzes the PS/2 data stream and reports
122 * relevant events to the input module once full packet has arrived.
123 */
124
125static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
126{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500127 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned char *packet = psmouse->packet;
129
130 if (psmouse->pktcnt < psmouse->pktsize)
131 return PSMOUSE_GOOD_DATA;
132
133/*
134 * Full packet accumulated, process it
135 */
136
137 input_regs(dev, regs);
138
139/*
140 * Scroll wheel on IntelliMice, scroll buttons on NetMice
141 */
142
143 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
144 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
145
146/*
147 * Scroll wheel and buttons on IntelliMouse Explorer
148 */
149
150 if (psmouse->type == PSMOUSE_IMEX) {
151 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
152 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
153 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
154 }
155
156/*
157 * Extra buttons on Genius NewNet 3D
158 */
159
160 if (psmouse->type == PSMOUSE_GENPS) {
161 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
162 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
163 }
164
165/*
166 * Extra button on ThinkingMouse
167 */
168 if (psmouse->type == PSMOUSE_THINKPS) {
169 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
170 /* Without this bit of weirdness moving up gives wildly high Y changes. */
171 packet[1] |= (packet[0] & 0x40) << 1;
172 }
173
174/*
175 * Generic PS/2 Mouse
176 */
177
178 input_report_key(dev, BTN_LEFT, packet[0] & 1);
179 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
180 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
181
182 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
183 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
184
185 input_sync(dev);
186
187 return PSMOUSE_FULL_PACKET;
188}
189
190/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500191 * __psmouse_set_state() sets new psmouse state and resets all flags.
192 */
193
194static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
195{
196 psmouse->state = new_state;
197 psmouse->pktcnt = psmouse->out_of_sync = 0;
198 psmouse->ps2dev.flags = 0;
199 psmouse->last = jiffies;
200}
201
202
203/*
204 * psmouse_set_state() sets new psmouse state and resets all flags and
205 * counters while holding serio lock so fighting with interrupt handler
206 * is not a concern.
207 */
208
209static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
210{
211 serio_pause_rx(psmouse->ps2dev.serio);
212 __psmouse_set_state(psmouse, new_state);
213 serio_continue_rx(psmouse->ps2dev.serio);
214}
215
216/*
217 * psmouse_handle_byte() processes one byte of the input data stream
218 * by calling corresponding protocol handler.
219 */
220
221static int psmouse_handle_byte(struct psmouse *psmouse, struct pt_regs *regs)
222{
223 psmouse_ret_t rc = psmouse->protocol_handler(psmouse, regs);
224
225 switch (rc) {
226 case PSMOUSE_BAD_DATA:
227 if (psmouse->state == PSMOUSE_ACTIVATED) {
228 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
229 psmouse->name, psmouse->phys, psmouse->pktcnt);
230 if (++psmouse->out_of_sync == psmouse->resetafter) {
231 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
232 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
233 serio_reconnect(psmouse->ps2dev.serio);
234 return -1;
235 }
236 }
237 psmouse->pktcnt = 0;
238 break;
239
240 case PSMOUSE_FULL_PACKET:
241 psmouse->pktcnt = 0;
242 if (psmouse->out_of_sync) {
243 psmouse->out_of_sync = 0;
244 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
245 psmouse->name, psmouse->phys);
246 }
247 break;
248
249 case PSMOUSE_GOOD_DATA:
250 break;
251 }
252 return 0;
253}
254
255/*
256 * psmouse_interrupt() handles incoming characters, either passing them
257 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
259
260static irqreturn_t psmouse_interrupt(struct serio *serio,
261 unsigned char data, unsigned int flags, struct pt_regs *regs)
262{
263 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (psmouse->state == PSMOUSE_IGNORE)
266 goto out;
267
268 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
269 if (psmouse->state == PSMOUSE_ACTIVATED)
270 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
271 flags & SERIO_TIMEOUT ? " timeout" : "",
272 flags & SERIO_PARITY ? " bad parity" : "");
273 ps2_cmd_aborted(&psmouse->ps2dev);
274 goto out;
275 }
276
277 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
278 if (ps2_handle_ack(&psmouse->ps2dev, data))
279 goto out;
280
281 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
282 if (ps2_handle_response(&psmouse->ps2dev, data))
283 goto out;
284
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500285 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto out;
287
288 if (psmouse->state == PSMOUSE_ACTIVATED &&
289 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500290 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500292 psmouse->badbyte = psmouse->packet[0];
293 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
294 queue_work(kpsmoused_wq, &psmouse->resync_work);
295 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500299/*
300 * Check if this is a new device announcement (0xAA 0x00)
301 */
302 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (psmouse->pktcnt == 1)
304 goto out;
305
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500306 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
307 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
308 serio_reconnect(serio);
309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500311/*
312 * Not a new device, try processing first byte normally
313 */
314 psmouse->pktcnt = 1;
315 if (psmouse_handle_byte(psmouse, regs))
316 goto out;
317
318 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500321/*
322 * See if we need to force resync because mouse was idle for too long
323 */
324 if (psmouse->state == PSMOUSE_ACTIVATED &&
325 psmouse->pktcnt == 1 && psmouse->resync_time &&
326 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
327 psmouse->badbyte = psmouse->packet[0];
328 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
329 queue_work(kpsmoused_wq, &psmouse->resync_work);
330 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500332
333 psmouse->last = jiffies;
334 psmouse_handle_byte(psmouse, regs);
335
336 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return IRQ_HANDLED;
338}
339
340
341/*
342 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
343 * using sliced syntax, understood by advanced devices, such as Logitech
344 * or Synaptics touchpads. The command is encoded as:
345 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
346 * is the command.
347 */
348int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
349{
350 int i;
351
352 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
353 return -1;
354
355 for (i = 6; i >= 0; i -= 2) {
356 unsigned char d = (command >> i) & 3;
357 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
358 return -1;
359 }
360
361 return 0;
362}
363
364
365/*
366 * psmouse_reset() resets the mouse into power-on state.
367 */
368int psmouse_reset(struct psmouse *psmouse)
369{
370 unsigned char param[2];
371
372 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
373 return -1;
374
375 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
376 return -1;
377
378 return 0;
379}
380
381
382/*
383 * Genius NetMouse magic init.
384 */
385static int genius_detect(struct psmouse *psmouse, int set_properties)
386{
387 struct ps2dev *ps2dev = &psmouse->ps2dev;
388 unsigned char param[4];
389
390 param[0] = 3;
391 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
392 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
393 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
394 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
395 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
396
397 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
398 return -1;
399
400 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500401 set_bit(BTN_EXTRA, psmouse->dev->keybit);
402 set_bit(BTN_SIDE, psmouse->dev->keybit);
403 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500406 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 psmouse->pktsize = 4;
408 }
409
410 return 0;
411}
412
413/*
414 * IntelliMouse magic init.
415 */
416static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
417{
418 struct ps2dev *ps2dev = &psmouse->ps2dev;
419 unsigned char param[2];
420
421 param[0] = 200;
422 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
423 param[0] = 100;
424 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
425 param[0] = 80;
426 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
427 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
428
429 if (param[0] != 3)
430 return -1;
431
432 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500433 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
434 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (!psmouse->vendor) psmouse->vendor = "Generic";
437 if (!psmouse->name) psmouse->name = "Wheel Mouse";
438 psmouse->pktsize = 4;
439 }
440
441 return 0;
442}
443
444/*
445 * Try IntelliMouse/Explorer magic init.
446 */
447static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
448{
449 struct ps2dev *ps2dev = &psmouse->ps2dev;
450 unsigned char param[2];
451
452 intellimouse_detect(psmouse, 0);
453
454 param[0] = 200;
455 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
456 param[0] = 200;
457 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
458 param[0] = 80;
459 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
460 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
461
462 if (param[0] != 4)
463 return -1;
464
465 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500466 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
467 set_bit(REL_WHEEL, psmouse->dev->relbit);
468 set_bit(BTN_SIDE, psmouse->dev->keybit);
469 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 if (!psmouse->vendor) psmouse->vendor = "Generic";
472 if (!psmouse->name) psmouse->name = "Explorer Mouse";
473 psmouse->pktsize = 4;
474 }
475
476 return 0;
477}
478
479/*
480 * Kensington ThinkingMouse / ExpertMouse magic init.
481 */
482static int thinking_detect(struct psmouse *psmouse, int set_properties)
483{
484 struct ps2dev *ps2dev = &psmouse->ps2dev;
485 unsigned char param[2];
486 unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20, 0 };
487 int i;
488
489 param[0] = 10;
490 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
491 param[0] = 0;
492 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
493 for (i = 0; seq[i]; i++)
494 ps2_command(ps2dev, seq + i, PSMOUSE_CMD_SETRATE);
495 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
496
497 if (param[0] != 2)
498 return -1;
499
500 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500501 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 psmouse->vendor = "Kensington";
504 psmouse->name = "ThinkingMouse";
505 }
506
507 return 0;
508}
509
510/*
511 * Bare PS/2 protocol "detection". Always succeeds.
512 */
513static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
514{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500515 if (set_properties) {
516 if (!psmouse->vendor) psmouse->vendor = "Generic";
517 if (!psmouse->name) psmouse->name = "Mouse";
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 return 0;
521}
522
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*
525 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
526 * the mouse may have.
527 */
528
529static int psmouse_extensions(struct psmouse *psmouse,
530 unsigned int max_proto, int set_properties)
531{
532 int synaptics_hardware = 0;
533
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500534/*
535 * We always check for lifebook because it does not disturb mouse
536 * (it only checks DMI information).
537 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500538 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500539 if (max_proto > PSMOUSE_IMEX) {
540 if (!set_properties || lifebook_init(psmouse) == 0)
541 return PSMOUSE_LIFEBOOK;
542 }
543 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/*
546 * Try Kensington ThinkingMouse (we try first, because synaptics probe
547 * upsets the thinkingmouse).
548 */
549
550 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
551 return PSMOUSE_THINKPS;
552
553/*
554 * Try Synaptics TouchPad
555 */
556 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
557 synaptics_hardware = 1;
558
559 if (max_proto > PSMOUSE_IMEX) {
560 if (!set_properties || synaptics_init(psmouse) == 0)
561 return PSMOUSE_SYNAPTICS;
562/*
563 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
564 * Unfortunately Logitech/Genius probes confuse some firmware versions so
565 * we'll have to skip them.
566 */
567 max_proto = PSMOUSE_IMEX;
568 }
569/*
570 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
571 */
572 synaptics_reset(psmouse);
573 }
574
575/*
576 * Try ALPS TouchPad
577 */
578 if (max_proto > PSMOUSE_IMEX) {
579 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
580 if (alps_detect(psmouse, set_properties) == 0) {
581 if (!set_properties || alps_init(psmouse) == 0)
582 return PSMOUSE_ALPS;
583/*
584 * Init failed, try basic relative protocols
585 */
586 max_proto = PSMOUSE_IMEX;
587 }
588 }
589
590 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
591 return PSMOUSE_GENPS;
592
593 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
594 return PSMOUSE_PS2PP;
595
Dmitry Torokhovba449952005-12-21 00:51:31 -0500596 if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
597 return PSMOUSE_TRACKPOINT;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
600 * Reset to defaults in case the device got confused by extended
Dmitry Torokhovba449952005-12-21 00:51:31 -0500601 * protocol probes. Note that we do full reset becuase some mice
602 * put themselves to sleep when see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Dmitry Torokhovba449952005-12-21 00:51:31 -0500604 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
607 return PSMOUSE_IMEX;
608
609 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
610 return PSMOUSE_IMPS;
611
612/*
613 * Okay, all failed, we have a standard mouse here. The number of the buttons
614 * is still a question, though. We assume 3.
615 */
616 ps2bare_detect(psmouse, set_properties);
617
618 if (synaptics_hardware) {
619/*
620 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
621 * We need to reset the touchpad because if there is a track point on the
622 * pass through port it could get disabled while probing for protocol
623 * extensions.
624 */
625 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 return PSMOUSE_PS2;
629}
630
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500631static struct psmouse_protocol psmouse_protocols[] = {
632 {
633 .type = PSMOUSE_PS2,
634 .name = "PS/2",
635 .alias = "bare",
636 .maxproto = 1,
637 .detect = ps2bare_detect,
638 },
639 {
640 .type = PSMOUSE_PS2PP,
641 .name = "PS2++",
642 .alias = "logitech",
643 .detect = ps2pp_init,
644 },
645 {
646 .type = PSMOUSE_THINKPS,
647 .name = "ThinkPS/2",
648 .alias = "thinkps",
649 .detect = thinking_detect,
650 },
651 {
652 .type = PSMOUSE_GENPS,
653 .name = "GenPS/2",
654 .alias = "genius",
655 .detect = genius_detect,
656 },
657 {
658 .type = PSMOUSE_IMPS,
659 .name = "ImPS/2",
660 .alias = "imps",
661 .maxproto = 1,
662 .detect = intellimouse_detect,
663 },
664 {
665 .type = PSMOUSE_IMEX,
666 .name = "ImExPS/2",
667 .alias = "exps",
668 .maxproto = 1,
669 .detect = im_explorer_detect,
670 },
671 {
672 .type = PSMOUSE_SYNAPTICS,
673 .name = "SynPS/2",
674 .alias = "synaptics",
675 .detect = synaptics_detect,
676 .init = synaptics_init,
677 },
678 {
679 .type = PSMOUSE_ALPS,
680 .name = "AlpsPS/2",
681 .alias = "alps",
682 .detect = alps_detect,
683 .init = alps_init,
684 },
685 {
686 .type = PSMOUSE_LIFEBOOK,
687 .name = "LBPS/2",
688 .alias = "lifebook",
689 .init = lifebook_init,
690 },
691 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500692 .type = PSMOUSE_TRACKPOINT,
693 .name = "TPPS/2",
694 .alias = "trackpoint",
695 .detect = trackpoint_detect,
696 },
697 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500698 .type = PSMOUSE_AUTO,
699 .name = "auto",
700 .alias = "any",
701 .maxproto = 1,
702 },
703};
704
705static struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
706{
707 int i;
708
709 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
710 if (psmouse_protocols[i].type == type)
711 return &psmouse_protocols[i];
712
713 WARN_ON(1);
714 return &psmouse_protocols[0];
715}
716
717static struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
718{
719 struct psmouse_protocol *p;
720 int i;
721
722 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
723 p = &psmouse_protocols[i];
724
725 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
726 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
727 return &psmouse_protocols[i];
728 }
729
730 return NULL;
731}
732
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/*
735 * psmouse_probe() probes for a PS/2 mouse.
736 */
737
738static int psmouse_probe(struct psmouse *psmouse)
739{
740 struct ps2dev *ps2dev = &psmouse->ps2dev;
741 unsigned char param[2];
742
743/*
744 * First, we check if it's a mouse. It should send 0x00 or 0x03
745 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500746 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
747 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
749
750 param[0] = 0xa5;
751 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
752 return -1;
753
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500754 if (param[0] != 0x00 && param[0] != 0x03 &&
755 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -1;
757
758/*
759 * Then we reset and disable the mouse so that it doesn't generate events.
760 */
761
762 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
763 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
764
765 return 0;
766}
767
768/*
769 * Here we set the mouse resolution.
770 */
771
772void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
773{
774 unsigned char params[] = { 0, 1, 2, 2, 3 };
775
776 if (resolution == 0 || resolution > 200)
777 resolution = 200;
778
779 ps2_command(&psmouse->ps2dev, &params[resolution / 50], PSMOUSE_CMD_SETRES);
780 psmouse->resolution = 25 << params[resolution / 50];
781}
782
783/*
784 * Here we set the mouse report rate.
785 */
786
787static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
788{
789 unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
790 int i = 0;
791
792 while (rates[i] > rate) i++;
793 ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE);
794 psmouse->rate = rates[i];
795}
796
797/*
798 * psmouse_initialize() initializes the mouse to a sane state.
799 */
800
801static void psmouse_initialize(struct psmouse *psmouse)
802{
803/*
804 * We set the mouse into streaming mode.
805 */
806
807 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
808
809/*
810 * We set the mouse report rate, resolution and scaling.
811 */
812
813 if (psmouse_max_proto != PSMOUSE_PS2) {
814 psmouse->set_rate(psmouse, psmouse->rate);
815 psmouse->set_resolution(psmouse, psmouse->resolution);
816 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
817 }
818}
819
820/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * psmouse_activate() enables the mouse so that we get motion reports from it.
822 */
823
824static void psmouse_activate(struct psmouse *psmouse)
825{
826 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
827 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
828 psmouse->ps2dev.serio->phys);
829
830 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
831}
832
833
834/*
835 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
836 * reports from it unless we explicitely request it.
837 */
838
839static void psmouse_deactivate(struct psmouse *psmouse)
840{
841 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
842 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
843 psmouse->ps2dev.serio->phys);
844
845 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
846}
847
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500848/*
849 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
850 */
851
852static int psmouse_poll(struct psmouse *psmouse)
853{
854 return ps2_command(&psmouse->ps2dev, psmouse->packet,
855 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
856}
857
858
859/*
860 * psmouse_resync() attempts to re-validate current protocol.
861 */
862
863static void psmouse_resync(void *p)
864{
865 struct psmouse *psmouse = p, *parent = NULL;
866 struct serio *serio = psmouse->ps2dev.serio;
867 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
868 int failed = 0, enabled = 0;
869 int i;
870
871 down(&psmouse_sem);
872
873 if (psmouse->state != PSMOUSE_RESYNCING)
874 goto out;
875
876 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
877 parent = serio_get_drvdata(serio->parent);
878 psmouse_deactivate(parent);
879 }
880
881/*
882 * Some mice don't ACK commands sent while they are in the middle of
883 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
884 * instead of ps2_command() which would wait for 200ms for an ACK
885 * that may never come.
886 * As an additional quirk ALPS touchpads may not only forget to ACK
887 * disable command but will stop reporting taps, so if we see that
888 * mouse at least once ACKs disable we will do full reconnect if ACK
889 * is missing.
890 */
891 psmouse->num_resyncs++;
892
893 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
894 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
895 failed = 1;
896 } else
897 psmouse->acks_disable_command = 1;
898
899/*
900 * Poll the mouse. If it was reset the packet will be shorter than
901 * psmouse->pktsize and ps2_command will fail. We do not expect and
902 * do not handle scenario when mouse "upgrades" its protocol while
903 * disconnected since it would require additional delay. If we ever
904 * see a mouse that does it we'll adjust the code.
905 */
906 if (!failed) {
907 if (psmouse->poll(psmouse))
908 failed = 1;
909 else {
910 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
911 for (i = 0; i < psmouse->pktsize; i++) {
912 psmouse->pktcnt++;
913 rc = psmouse->protocol_handler(psmouse, NULL);
914 if (rc != PSMOUSE_GOOD_DATA)
915 break;
916 }
917 if (rc != PSMOUSE_FULL_PACKET)
918 failed = 1;
919 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
920 }
921 }
922/*
923 * Now try to enable mouse. We try to do that even if poll failed and also
924 * repeat our attempts 5 times, otherwise we may be left out with disabled
925 * mouse.
926 */
927 for (i = 0; i < 5; i++) {
928 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
929 enabled = 1;
930 break;
931 }
932 msleep(200);
933 }
934
935 if (!enabled) {
936 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
937 psmouse->ps2dev.serio->phys);
938 failed = 1;
939 }
940
941 if (failed) {
942 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
943 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
944 serio_reconnect(serio);
945 } else
946 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
947
948 if (parent)
949 psmouse_activate(parent);
950 out:
951 up(&psmouse_sem);
952}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954/*
955 * psmouse_cleanup() resets the mouse into power-on state.
956 */
957
958static void psmouse_cleanup(struct serio *serio)
959{
960 struct psmouse *psmouse = serio_get_drvdata(serio);
961
962 psmouse_reset(psmouse);
963}
964
965/*
966 * psmouse_disconnect() closes and frees.
967 */
968
969static void psmouse_disconnect(struct serio *serio)
970{
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500971 struct psmouse *psmouse, *parent = NULL;
972
973 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500975 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500977 down(&psmouse_sem);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
980
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500981 /* make sure we don't have a resync in progress */
982 up(&psmouse_sem);
983 flush_workqueue(kpsmoused_wq);
984 down(&psmouse_sem);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
987 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500988 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
991 if (psmouse->disconnect)
992 psmouse->disconnect(psmouse);
993
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500994 if (parent && parent->pt_deactivate)
995 parent->pt_deactivate(parent);
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 serio_close(serio);
1000 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001001 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001003
1004 if (parent)
1005 psmouse_activate(parent);
1006
1007 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001010static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_protocol *proto)
1011{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001012 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001013
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001014 input_dev->private = psmouse;
1015 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001016
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001017 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1018 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1019 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001020
1021 psmouse->set_rate = psmouse_set_rate;
1022 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001023 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001024 psmouse->protocol_handler = psmouse_process_byte;
1025 psmouse->pktsize = 3;
1026
1027 if (proto && (proto->detect || proto->init)) {
1028 if (proto->detect && proto->detect(psmouse, 1) < 0)
1029 return -1;
1030
1031 if (proto->init && proto->init(psmouse) < 0)
1032 return -1;
1033
1034 psmouse->type = proto->type;
1035 }
1036 else
1037 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1038
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001039 /*
1040 * If mouse's packet size is 3 there is no point in polling the
1041 * device in hopes to detect protocol reset - we won't get less
1042 * than 3 bytes response anyhow.
1043 */
1044 if (psmouse->pktsize == 3)
1045 psmouse->resync_time = 0;
1046
1047 /*
1048 * Some smart KVMs fake response to POLL command returning just
1049 * 3 bytes and messing up our resync logic, so if initial poll
1050 * fails we won't try polling the device anymore. Hopefully
1051 * such KVM will maintain initially selected protocol.
1052 */
1053 if (psmouse->resync_time && psmouse->poll(psmouse))
1054 psmouse->resync_time = 0;
1055
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001056 sprintf(psmouse->devname, "%s %s %s",
1057 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1058
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001059 input_dev->name = psmouse->devname;
1060 input_dev->phys = psmouse->phys;
1061 input_dev->id.bustype = BUS_I8042;
1062 input_dev->id.vendor = 0x0002;
1063 input_dev->id.product = psmouse->type;
1064 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001065
1066 return 0;
1067}
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069/*
1070 * psmouse_connect() is a callback from the serio module when
1071 * an unhandled serio port is found.
1072 */
1073static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1074{
1075 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001076 struct input_dev *input_dev;
1077 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001079 down(&psmouse_sem);
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /*
1082 * If this is a pass-through port deactivate parent so the device
1083 * connected to this port can be successfully identified
1084 */
1085 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1086 parent = serio_get_drvdata(serio->parent);
1087 psmouse_deactivate(parent);
1088 }
1089
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001090 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1091 input_dev = input_allocate_device();
1092 if (!psmouse || !input_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 ps2_init(&psmouse->ps2dev, serio);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001096 INIT_WORK(&psmouse->resync_work, psmouse_resync, psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001097 psmouse->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 sprintf(psmouse->phys, "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1101
1102 serio_set_drvdata(serio, psmouse);
1103
1104 retval = serio_open(serio, drv);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001105 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 if (psmouse_probe(psmouse) < 0) {
1109 serio_close(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 retval = -ENODEV;
1111 goto out;
1112 }
1113
1114 psmouse->rate = psmouse_rate;
1115 psmouse->resolution = psmouse_resolution;
1116 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001117 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001120 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 psmouse_initialize(psmouse);
1124
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001125 input_register_device(psmouse->dev);
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (parent && parent->pt_activate)
1128 parent->pt_activate(parent);
1129
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001130 sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 psmouse_activate(psmouse);
1133
1134 retval = 0;
1135
1136out:
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001137 if (retval) {
1138 serio_set_drvdata(serio, NULL);
1139 input_free_device(input_dev);
1140 kfree(psmouse);
1141 }
1142
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001143 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (parent)
1145 psmouse_activate(parent);
1146
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001147 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return retval;
1149}
1150
1151
1152static int psmouse_reconnect(struct serio *serio)
1153{
1154 struct psmouse *psmouse = serio_get_drvdata(serio);
1155 struct psmouse *parent = NULL;
1156 struct serio_driver *drv = serio->drv;
1157 int rc = -1;
1158
1159 if (!drv || !psmouse) {
1160 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1161 return -1;
1162 }
1163
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001164 down(&psmouse_sem);
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1167 parent = serio_get_drvdata(serio->parent);
1168 psmouse_deactivate(parent);
1169 }
1170
1171 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1172
1173 if (psmouse->reconnect) {
1174 if (psmouse->reconnect(psmouse))
1175 goto out;
1176 } else if (psmouse_probe(psmouse) < 0 ||
1177 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1178 goto out;
1179
1180 /* ok, the device type (and capabilities) match the old one,
1181 * we can continue using it, complete intialization
1182 */
1183 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1184
1185 psmouse_initialize(psmouse);
1186
1187 if (parent && parent->pt_activate)
1188 parent->pt_activate(parent);
1189
1190 psmouse_activate(psmouse);
1191 rc = 0;
1192
1193out:
1194 /* If this is a pass-through port the parent waits to be activated */
1195 if (parent)
1196 psmouse_activate(parent);
1197
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001198 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return rc;
1200}
1201
1202static struct serio_device_id psmouse_serio_ids[] = {
1203 {
1204 .type = SERIO_8042,
1205 .proto = SERIO_ANY,
1206 .id = SERIO_ANY,
1207 .extra = SERIO_ANY,
1208 },
1209 {
1210 .type = SERIO_PS_PSTHRU,
1211 .proto = SERIO_ANY,
1212 .id = SERIO_ANY,
1213 .extra = SERIO_ANY,
1214 },
1215 { 0 }
1216};
1217
1218MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1219
1220static struct serio_driver psmouse_drv = {
1221 .driver = {
1222 .name = "psmouse",
1223 },
1224 .description = DRIVER_DESC,
1225 .id_table = psmouse_serio_ids,
1226 .interrupt = psmouse_interrupt,
1227 .connect = psmouse_connect,
1228 .reconnect = psmouse_reconnect,
1229 .disconnect = psmouse_disconnect,
1230 .cleanup = psmouse_cleanup,
1231};
1232
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001233ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1234 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001237 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1238 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int retval;
1240
1241 retval = serio_pin_driver(serio);
1242 if (retval)
1243 return retval;
1244
1245 if (serio->drv != &psmouse_drv) {
1246 retval = -ENODEV;
1247 goto out;
1248 }
1249
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001250 psmouse = serio_get_drvdata(serio);
1251
1252 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254out:
1255 serio_unpin_driver(serio);
1256 return retval;
1257}
1258
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001259ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1260 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001263 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1264 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 int retval;
1266
1267 retval = serio_pin_driver(serio);
1268 if (retval)
1269 return retval;
1270
1271 if (serio->drv != &psmouse_drv) {
1272 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001273 goto out_unpin;
1274 }
1275
1276 retval = down_interruptible(&psmouse_sem);
1277 if (retval)
1278 goto out_unpin;
1279
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001280 psmouse = serio_get_drvdata(serio);
1281
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001282 if (psmouse->state == PSMOUSE_IGNORE) {
1283 retval = -ENODEV;
1284 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286
1287 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1288 parent = serio_get_drvdata(serio->parent);
1289 psmouse_deactivate(parent);
1290 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 psmouse_deactivate(psmouse);
1293
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001294 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001296 if (retval != -ENODEV)
1297 psmouse_activate(psmouse);
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (parent)
1300 psmouse_activate(parent);
1301
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001302 out_up:
1303 up(&psmouse_sem);
1304 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 serio_unpin_driver(serio);
1306 return retval;
1307}
1308
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001309static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1310{
1311 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1312
1313 return sprintf(buf, "%lu\n", *field);
1314}
1315
1316static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1317{
1318 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1319 unsigned long value;
1320 char *rest;
1321
1322 value = simple_strtoul(buf, &rest, 10);
1323 if (*rest)
1324 return -EINVAL;
1325
1326 *field = value;
1327
1328 return count;
1329}
1330
1331static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001332{
1333 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1334}
1335
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001336static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001337{
1338 struct serio *serio = psmouse->ps2dev.serio;
1339 struct psmouse *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001340 struct input_dev *new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001341 struct psmouse_protocol *proto;
1342 int retry = 0;
1343
1344 if (!(proto = psmouse_protocol_by_name(buf, count)))
1345 return -EINVAL;
1346
1347 if (psmouse->type == proto->type)
1348 return count;
1349
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001350 if (!(new_dev = input_allocate_device()))
1351 return -ENOMEM;
1352
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001353 while (serio->child) {
1354 if (++retry > 3) {
1355 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001356 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001357 return -EIO;
1358 }
1359
1360 up(&psmouse_sem);
1361 serio_unpin_driver(serio);
1362 serio_unregister_child_port(serio);
1363 serio_pin_driver_uninterruptible(serio);
1364 down(&psmouse_sem);
1365
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001366 if (serio->drv != &psmouse_drv) {
1367 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001368 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001369 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001370
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001371 if (psmouse->type == proto->type) {
1372 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001373 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001374 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001375 }
1376
1377 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1378 parent = serio_get_drvdata(serio->parent);
1379 if (parent->pt_deactivate)
1380 parent->pt_deactivate(parent);
1381 }
1382
1383 if (psmouse->disconnect)
1384 psmouse->disconnect(psmouse);
1385
1386 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001387 input_unregister_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001388
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001389 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001390 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1391
1392 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1393 psmouse_reset(psmouse);
1394 /* default to PSMOUSE_PS2 */
1395 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1396 }
1397
1398 psmouse_initialize(psmouse);
1399 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1400
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001401 input_register_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001402
1403 if (parent && parent->pt_activate)
1404 parent->pt_activate(parent);
1405
1406 return count;
1407}
1408
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001409static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 unsigned long value;
1412 char *rest;
1413
1414 value = simple_strtoul(buf, &rest, 10);
1415 if (*rest)
1416 return -EINVAL;
1417
1418 psmouse->set_rate(psmouse, value);
1419 return count;
1420}
1421
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001422static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 unsigned long value;
1425 char *rest;
1426
1427 value = simple_strtoul(buf, &rest, 10);
1428 if (*rest)
1429 return -EINVAL;
1430
1431 psmouse->set_resolution(psmouse, value);
1432 return count;
1433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1437{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001438 struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (!val)
1441 return -EINVAL;
1442
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001443 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001445 if (!proto || !proto->maxproto)
1446 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001448 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Stephen Evanchik541e3162005-08-08 01:26:18 -05001450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
1453static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1454{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001455 int type = *((unsigned int *)kp->arg);
1456
1457 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460static int __init psmouse_init(void)
1461{
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001462 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1463 if (!kpsmoused_wq) {
1464 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1465 return -ENOMEM;
1466 }
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 serio_register_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return 0;
1471}
1472
1473static void __exit psmouse_exit(void)
1474{
1475 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001476 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479module_init(psmouse_init);
1480module_exit(psmouse_exit);