Linux 2.6.21.1
[linux/fpc-iii.git] / drivers / input / mouse / psmouse-base.c
blob0fe5869d7d4c586bf782a794f48884b1564e0eec
1 /*
2 * PS/2 mouse driver
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2003-2004 Dmitry Torokhov
6 */
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.
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 <linux/mutex.h>
25 #include "psmouse.h"
26 #include "synaptics.h"
27 #include "logips2pp.h"
28 #include "alps.h"
29 #include "lifebook.h"
30 #include "trackpoint.h"
32 #define DRIVER_DESC "PS/2 mouse driver"
34 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
35 MODULE_DESCRIPTION(DRIVER_DESC);
36 MODULE_LICENSE("GPL");
38 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
39 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
40 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
41 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
42 #define param_set_proto_abbrev psmouse_set_maxproto
43 #define param_get_proto_abbrev psmouse_get_maxproto
44 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
45 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
47 static unsigned int psmouse_resolution = 200;
48 module_param_named(resolution, psmouse_resolution, uint, 0644);
49 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
51 static unsigned int psmouse_rate = 100;
52 module_param_named(rate, psmouse_rate, uint, 0644);
53 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
55 static unsigned int psmouse_smartscroll = 1;
56 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
57 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
59 static unsigned int psmouse_resetafter = 5;
60 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
61 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
63 static unsigned int psmouse_resync_time;
64 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
65 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
67 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
68 NULL,
69 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
70 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
71 (void *) offsetof(struct psmouse, rate),
72 psmouse_show_int_attr, psmouse_attr_set_rate);
73 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
74 (void *) offsetof(struct psmouse, resolution),
75 psmouse_show_int_attr, psmouse_attr_set_resolution);
76 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
77 (void *) offsetof(struct psmouse, resetafter),
78 psmouse_show_int_attr, psmouse_set_int_attr);
79 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
80 (void *) offsetof(struct psmouse, resync_time),
81 psmouse_show_int_attr, psmouse_set_int_attr);
83 static struct attribute *psmouse_attributes[] = {
84 &psmouse_attr_protocol.dattr.attr,
85 &psmouse_attr_rate.dattr.attr,
86 &psmouse_attr_resolution.dattr.attr,
87 &psmouse_attr_resetafter.dattr.attr,
88 &psmouse_attr_resync_time.dattr.attr,
89 NULL
92 static struct attribute_group psmouse_attribute_group = {
93 .attrs = psmouse_attributes,
97 * psmouse_mutex protects all operations changing state of mouse
98 * (connecting, disconnecting, changing rate or resolution via
99 * sysfs). We could use a per-device semaphore but since there
100 * rarely more than one PS/2 mouse connected and since semaphore
101 * is taken in "slow" paths it is not worth it.
103 static DEFINE_MUTEX(psmouse_mutex);
105 static struct workqueue_struct *kpsmoused_wq;
107 struct psmouse_protocol {
108 enum psmouse_type type;
109 const char *name;
110 const char *alias;
111 int maxproto;
112 int (*detect)(struct psmouse *, int);
113 int (*init)(struct psmouse *);
117 * psmouse_process_byte() analyzes the PS/2 data stream and reports
118 * relevant events to the input module once full packet has arrived.
121 static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
123 struct input_dev *dev = psmouse->dev;
124 unsigned char *packet = psmouse->packet;
126 if (psmouse->pktcnt < psmouse->pktsize)
127 return PSMOUSE_GOOD_DATA;
130 * Full packet accumulated, process it
134 * Scroll wheel on IntelliMice, scroll buttons on NetMice
137 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
138 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
141 * Scroll wheel and buttons on IntelliMouse Explorer
144 if (psmouse->type == PSMOUSE_IMEX) {
145 switch (packet[3] & 0xC0) {
146 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
147 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
148 break;
149 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
150 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
151 break;
152 case 0x00:
153 case 0xC0:
154 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
155 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
156 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
157 break;
162 * Extra buttons on Genius NewNet 3D
165 if (psmouse->type == PSMOUSE_GENPS) {
166 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
167 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
171 * Extra button on ThinkingMouse
173 if (psmouse->type == PSMOUSE_THINKPS) {
174 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
175 /* Without this bit of weirdness moving up gives wildly high Y changes. */
176 packet[1] |= (packet[0] & 0x40) << 1;
180 * Generic PS/2 Mouse
183 input_report_key(dev, BTN_LEFT, packet[0] & 1);
184 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
185 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
187 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
188 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
190 input_sync(dev);
192 return PSMOUSE_FULL_PACKET;
196 * __psmouse_set_state() sets new psmouse state and resets all flags.
199 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
201 psmouse->state = new_state;
202 psmouse->pktcnt = psmouse->out_of_sync = 0;
203 psmouse->ps2dev.flags = 0;
204 psmouse->last = jiffies;
209 * psmouse_set_state() sets new psmouse state and resets all flags and
210 * counters while holding serio lock so fighting with interrupt handler
211 * is not a concern.
214 static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
216 serio_pause_rx(psmouse->ps2dev.serio);
217 __psmouse_set_state(psmouse, new_state);
218 serio_continue_rx(psmouse->ps2dev.serio);
222 * psmouse_handle_byte() processes one byte of the input data stream
223 * by calling corresponding protocol handler.
226 static int psmouse_handle_byte(struct psmouse *psmouse)
228 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
230 switch (rc) {
231 case PSMOUSE_BAD_DATA:
232 if (psmouse->state == PSMOUSE_ACTIVATED) {
233 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
234 psmouse->name, psmouse->phys, psmouse->pktcnt);
235 if (++psmouse->out_of_sync == psmouse->resetafter) {
236 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
237 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
238 serio_reconnect(psmouse->ps2dev.serio);
239 return -1;
242 psmouse->pktcnt = 0;
243 break;
245 case PSMOUSE_FULL_PACKET:
246 psmouse->pktcnt = 0;
247 if (psmouse->out_of_sync) {
248 psmouse->out_of_sync = 0;
249 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
250 psmouse->name, psmouse->phys);
252 break;
254 case PSMOUSE_GOOD_DATA:
255 break;
257 return 0;
261 * psmouse_interrupt() handles incoming characters, either passing them
262 * for normal processing or gathering them as command response.
265 static irqreturn_t psmouse_interrupt(struct serio *serio,
266 unsigned char data, unsigned int flags)
268 struct psmouse *psmouse = serio_get_drvdata(serio);
270 if (psmouse->state == PSMOUSE_IGNORE)
271 goto out;
273 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
274 if (psmouse->state == PSMOUSE_ACTIVATED)
275 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
276 flags & SERIO_TIMEOUT ? " timeout" : "",
277 flags & SERIO_PARITY ? " bad parity" : "");
278 ps2_cmd_aborted(&psmouse->ps2dev);
279 goto out;
282 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
283 if (ps2_handle_ack(&psmouse->ps2dev, data))
284 goto out;
286 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
287 if (ps2_handle_response(&psmouse->ps2dev, data))
288 goto out;
290 if (psmouse->state <= PSMOUSE_RESYNCING)
291 goto out;
293 if (psmouse->state == PSMOUSE_ACTIVATED &&
294 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
295 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
296 psmouse->name, psmouse->phys, psmouse->pktcnt);
297 psmouse->badbyte = psmouse->packet[0];
298 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
299 queue_work(kpsmoused_wq, &psmouse->resync_work);
300 goto out;
303 psmouse->packet[psmouse->pktcnt++] = data;
305 * Check if this is a new device announcement (0xAA 0x00)
307 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
308 if (psmouse->pktcnt == 1) {
309 psmouse->last = jiffies;
310 goto out;
313 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
314 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
315 serio_reconnect(serio);
316 goto out;
319 * Not a new device, try processing first byte normally
321 psmouse->pktcnt = 1;
322 if (psmouse_handle_byte(psmouse))
323 goto out;
325 psmouse->packet[psmouse->pktcnt++] = data;
329 * See if we need to force resync because mouse was idle for too long
331 if (psmouse->state == PSMOUSE_ACTIVATED &&
332 psmouse->pktcnt == 1 && psmouse->resync_time &&
333 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
334 psmouse->badbyte = psmouse->packet[0];
335 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
336 queue_work(kpsmoused_wq, &psmouse->resync_work);
337 goto out;
340 psmouse->last = jiffies;
341 psmouse_handle_byte(psmouse);
343 out:
344 return IRQ_HANDLED;
349 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
350 * using sliced syntax, understood by advanced devices, such as Logitech
351 * or Synaptics touchpads. The command is encoded as:
352 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
353 * is the command.
355 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
357 int i;
359 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
360 return -1;
362 for (i = 6; i >= 0; i -= 2) {
363 unsigned char d = (command >> i) & 3;
364 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
365 return -1;
368 return 0;
373 * psmouse_reset() resets the mouse into power-on state.
375 int psmouse_reset(struct psmouse *psmouse)
377 unsigned char param[2];
379 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
380 return -1;
382 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
383 return -1;
385 return 0;
390 * Genius NetMouse magic init.
392 static int genius_detect(struct psmouse *psmouse, int set_properties)
394 struct ps2dev *ps2dev = &psmouse->ps2dev;
395 unsigned char param[4];
397 param[0] = 3;
398 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
399 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
400 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
401 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
402 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
404 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
405 return -1;
407 if (set_properties) {
408 set_bit(BTN_EXTRA, psmouse->dev->keybit);
409 set_bit(BTN_SIDE, psmouse->dev->keybit);
410 set_bit(REL_WHEEL, psmouse->dev->relbit);
412 psmouse->vendor = "Genius";
413 psmouse->name = "Mouse";
414 psmouse->pktsize = 4;
417 return 0;
421 * IntelliMouse magic init.
423 static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
425 struct ps2dev *ps2dev = &psmouse->ps2dev;
426 unsigned char param[2];
428 param[0] = 200;
429 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
430 param[0] = 100;
431 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
432 param[0] = 80;
433 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
434 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
436 if (param[0] != 3)
437 return -1;
439 if (set_properties) {
440 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
441 set_bit(REL_WHEEL, psmouse->dev->relbit);
443 if (!psmouse->vendor) psmouse->vendor = "Generic";
444 if (!psmouse->name) psmouse->name = "Wheel Mouse";
445 psmouse->pktsize = 4;
448 return 0;
452 * Try IntelliMouse/Explorer magic init.
454 static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
456 struct ps2dev *ps2dev = &psmouse->ps2dev;
457 unsigned char param[2];
459 intellimouse_detect(psmouse, 0);
461 param[0] = 200;
462 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
463 param[0] = 200;
464 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
465 param[0] = 80;
466 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
467 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
469 if (param[0] != 4)
470 return -1;
472 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
473 param[0] = 200;
474 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
475 param[0] = 80;
476 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
477 param[0] = 40;
478 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
480 if (set_properties) {
481 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
482 set_bit(REL_WHEEL, psmouse->dev->relbit);
483 set_bit(REL_HWHEEL, psmouse->dev->relbit);
484 set_bit(BTN_SIDE, psmouse->dev->keybit);
485 set_bit(BTN_EXTRA, psmouse->dev->keybit);
487 if (!psmouse->vendor) psmouse->vendor = "Generic";
488 if (!psmouse->name) psmouse->name = "Explorer Mouse";
489 psmouse->pktsize = 4;
492 return 0;
496 * Kensington ThinkingMouse / ExpertMouse magic init.
498 static int thinking_detect(struct psmouse *psmouse, int set_properties)
500 struct ps2dev *ps2dev = &psmouse->ps2dev;
501 unsigned char param[2];
502 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
503 int i;
505 param[0] = 10;
506 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
507 param[0] = 0;
508 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
509 for (i = 0; i < ARRAY_SIZE(seq); i++) {
510 param[0] = seq[i];
511 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
513 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
515 if (param[0] != 2)
516 return -1;
518 if (set_properties) {
519 set_bit(BTN_EXTRA, psmouse->dev->keybit);
521 psmouse->vendor = "Kensington";
522 psmouse->name = "ThinkingMouse";
525 return 0;
529 * Bare PS/2 protocol "detection". Always succeeds.
531 static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
533 if (set_properties) {
534 if (!psmouse->vendor) psmouse->vendor = "Generic";
535 if (!psmouse->name) psmouse->name = "Mouse";
538 return 0;
543 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
544 * the mouse may have.
547 static int psmouse_extensions(struct psmouse *psmouse,
548 unsigned int max_proto, int set_properties)
550 int synaptics_hardware = 0;
553 * We always check for lifebook because it does not disturb mouse
554 * (it only checks DMI information).
556 if (lifebook_detect(psmouse, set_properties) == 0) {
557 if (max_proto > PSMOUSE_IMEX) {
558 if (!set_properties || lifebook_init(psmouse) == 0)
559 return PSMOUSE_LIFEBOOK;
564 * Try Kensington ThinkingMouse (we try first, because synaptics probe
565 * upsets the thinkingmouse).
568 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
569 return PSMOUSE_THINKPS;
572 * Try Synaptics TouchPad
574 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
575 synaptics_hardware = 1;
577 if (max_proto > PSMOUSE_IMEX) {
578 if (!set_properties || synaptics_init(psmouse) == 0)
579 return PSMOUSE_SYNAPTICS;
581 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
582 * Unfortunately Logitech/Genius probes confuse some firmware versions so
583 * we'll have to skip them.
585 max_proto = PSMOUSE_IMEX;
588 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
590 synaptics_reset(psmouse);
594 * Try ALPS TouchPad
596 if (max_proto > PSMOUSE_IMEX) {
597 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
598 if (alps_detect(psmouse, set_properties) == 0) {
599 if (!set_properties || alps_init(psmouse) == 0)
600 return PSMOUSE_ALPS;
602 * Init failed, try basic relative protocols
604 max_proto = PSMOUSE_IMEX;
608 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
609 return PSMOUSE_GENPS;
611 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
612 return PSMOUSE_PS2PP;
614 if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
615 return PSMOUSE_TRACKPOINT;
618 * Reset to defaults in case the device got confused by extended
619 * protocol probes. Note that we do full reset becuase some mice
620 * put themselves to sleep when see PSMOUSE_RESET_DIS.
622 psmouse_reset(psmouse);
624 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
625 return PSMOUSE_IMEX;
627 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
628 return PSMOUSE_IMPS;
631 * Okay, all failed, we have a standard mouse here. The number of the buttons
632 * is still a question, though. We assume 3.
634 ps2bare_detect(psmouse, set_properties);
636 if (synaptics_hardware) {
638 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
639 * We need to reset the touchpad because if there is a track point on the
640 * pass through port it could get disabled while probing for protocol
641 * extensions.
643 psmouse_reset(psmouse);
646 return PSMOUSE_PS2;
649 static const struct psmouse_protocol psmouse_protocols[] = {
651 .type = PSMOUSE_PS2,
652 .name = "PS/2",
653 .alias = "bare",
654 .maxproto = 1,
655 .detect = ps2bare_detect,
658 .type = PSMOUSE_PS2PP,
659 .name = "PS2++",
660 .alias = "logitech",
661 .detect = ps2pp_init,
664 .type = PSMOUSE_THINKPS,
665 .name = "ThinkPS/2",
666 .alias = "thinkps",
667 .detect = thinking_detect,
670 .type = PSMOUSE_GENPS,
671 .name = "GenPS/2",
672 .alias = "genius",
673 .detect = genius_detect,
676 .type = PSMOUSE_IMPS,
677 .name = "ImPS/2",
678 .alias = "imps",
679 .maxproto = 1,
680 .detect = intellimouse_detect,
683 .type = PSMOUSE_IMEX,
684 .name = "ImExPS/2",
685 .alias = "exps",
686 .maxproto = 1,
687 .detect = im_explorer_detect,
690 .type = PSMOUSE_SYNAPTICS,
691 .name = "SynPS/2",
692 .alias = "synaptics",
693 .detect = synaptics_detect,
694 .init = synaptics_init,
697 .type = PSMOUSE_ALPS,
698 .name = "AlpsPS/2",
699 .alias = "alps",
700 .detect = alps_detect,
701 .init = alps_init,
704 .type = PSMOUSE_LIFEBOOK,
705 .name = "LBPS/2",
706 .alias = "lifebook",
707 .init = lifebook_init,
710 .type = PSMOUSE_TRACKPOINT,
711 .name = "TPPS/2",
712 .alias = "trackpoint",
713 .detect = trackpoint_detect,
716 .type = PSMOUSE_AUTO,
717 .name = "auto",
718 .alias = "any",
719 .maxproto = 1,
723 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
725 int i;
727 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
728 if (psmouse_protocols[i].type == type)
729 return &psmouse_protocols[i];
731 WARN_ON(1);
732 return &psmouse_protocols[0];
735 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
737 const struct psmouse_protocol *p;
738 int i;
740 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
741 p = &psmouse_protocols[i];
743 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
744 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
745 return &psmouse_protocols[i];
748 return NULL;
753 * psmouse_probe() probes for a PS/2 mouse.
756 static int psmouse_probe(struct psmouse *psmouse)
758 struct ps2dev *ps2dev = &psmouse->ps2dev;
759 unsigned char param[2];
762 * First, we check if it's a mouse. It should send 0x00 or 0x03
763 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
764 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
765 * ID queries, probably due to a firmware bug.
768 param[0] = 0xa5;
769 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
770 return -1;
772 if (param[0] != 0x00 && param[0] != 0x03 &&
773 param[0] != 0x04 && param[0] != 0xff)
774 return -1;
777 * Then we reset and disable the mouse so that it doesn't generate events.
780 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
781 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
783 return 0;
787 * Here we set the mouse resolution.
790 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
792 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
793 unsigned char p;
795 if (resolution == 0 || resolution > 200)
796 resolution = 200;
798 p = params[resolution / 50];
799 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
800 psmouse->resolution = 25 << p;
804 * Here we set the mouse report rate.
807 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
809 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
810 unsigned char r;
811 int i = 0;
813 while (rates[i] > rate) i++;
814 r = rates[i];
815 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
816 psmouse->rate = r;
820 * psmouse_initialize() initializes the mouse to a sane state.
823 static void psmouse_initialize(struct psmouse *psmouse)
826 * We set the mouse into streaming mode.
829 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
832 * We set the mouse report rate, resolution and scaling.
835 if (psmouse_max_proto != PSMOUSE_PS2) {
836 psmouse->set_rate(psmouse, psmouse->rate);
837 psmouse->set_resolution(psmouse, psmouse->resolution);
838 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
843 * psmouse_activate() enables the mouse so that we get motion reports from it.
846 static void psmouse_activate(struct psmouse *psmouse)
848 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
849 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
850 psmouse->ps2dev.serio->phys);
852 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
857 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
858 * reports from it unless we explicitely request it.
861 static void psmouse_deactivate(struct psmouse *psmouse)
863 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
864 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
865 psmouse->ps2dev.serio->phys);
867 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
871 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
874 static int psmouse_poll(struct psmouse *psmouse)
876 return ps2_command(&psmouse->ps2dev, psmouse->packet,
877 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
882 * psmouse_resync() attempts to re-validate current protocol.
885 static void psmouse_resync(struct work_struct *work)
887 struct psmouse *parent = NULL, *psmouse =
888 container_of(work, struct psmouse, resync_work);
889 struct serio *serio = psmouse->ps2dev.serio;
890 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
891 int failed = 0, enabled = 0;
892 int i;
894 mutex_lock(&psmouse_mutex);
896 if (psmouse->state != PSMOUSE_RESYNCING)
897 goto out;
899 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
900 parent = serio_get_drvdata(serio->parent);
901 psmouse_deactivate(parent);
905 * Some mice don't ACK commands sent while they are in the middle of
906 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
907 * instead of ps2_command() which would wait for 200ms for an ACK
908 * that may never come.
909 * As an additional quirk ALPS touchpads may not only forget to ACK
910 * disable command but will stop reporting taps, so if we see that
911 * mouse at least once ACKs disable we will do full reconnect if ACK
912 * is missing.
914 psmouse->num_resyncs++;
916 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
917 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
918 failed = 1;
919 } else
920 psmouse->acks_disable_command = 1;
923 * Poll the mouse. If it was reset the packet will be shorter than
924 * psmouse->pktsize and ps2_command will fail. We do not expect and
925 * do not handle scenario when mouse "upgrades" its protocol while
926 * disconnected since it would require additional delay. If we ever
927 * see a mouse that does it we'll adjust the code.
929 if (!failed) {
930 if (psmouse->poll(psmouse))
931 failed = 1;
932 else {
933 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
934 for (i = 0; i < psmouse->pktsize; i++) {
935 psmouse->pktcnt++;
936 rc = psmouse->protocol_handler(psmouse);
937 if (rc != PSMOUSE_GOOD_DATA)
938 break;
940 if (rc != PSMOUSE_FULL_PACKET)
941 failed = 1;
942 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
946 * Now try to enable mouse. We try to do that even if poll failed and also
947 * repeat our attempts 5 times, otherwise we may be left out with disabled
948 * mouse.
950 for (i = 0; i < 5; i++) {
951 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
952 enabled = 1;
953 break;
955 msleep(200);
958 if (!enabled) {
959 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
960 psmouse->ps2dev.serio->phys);
961 failed = 1;
964 if (failed) {
965 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
966 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
967 serio_reconnect(serio);
968 } else
969 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
971 if (parent)
972 psmouse_activate(parent);
973 out:
974 mutex_unlock(&psmouse_mutex);
978 * psmouse_cleanup() resets the mouse into power-on state.
981 static void psmouse_cleanup(struct serio *serio)
983 struct psmouse *psmouse = serio_get_drvdata(serio);
984 struct psmouse *parent = NULL;
986 mutex_lock(&psmouse_mutex);
988 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
989 parent = serio_get_drvdata(serio->parent);
990 psmouse_deactivate(parent);
993 psmouse_deactivate(psmouse);
995 if (psmouse->cleanup)
996 psmouse->cleanup(psmouse);
998 psmouse_reset(psmouse);
1001 * Some boxes, such as HP nx7400, get terribly confused if mouse
1002 * is not fully enabled before suspending/shutting down.
1004 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1006 if (parent) {
1007 if (parent->pt_deactivate)
1008 parent->pt_deactivate(parent);
1010 psmouse_activate(parent);
1013 mutex_unlock(&psmouse_mutex);
1017 * psmouse_disconnect() closes and frees.
1020 static void psmouse_disconnect(struct serio *serio)
1022 struct psmouse *psmouse, *parent = NULL;
1024 psmouse = serio_get_drvdata(serio);
1026 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1028 mutex_lock(&psmouse_mutex);
1030 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1032 /* make sure we don't have a resync in progress */
1033 mutex_unlock(&psmouse_mutex);
1034 flush_workqueue(kpsmoused_wq);
1035 mutex_lock(&psmouse_mutex);
1037 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1038 parent = serio_get_drvdata(serio->parent);
1039 psmouse_deactivate(parent);
1042 if (psmouse->disconnect)
1043 psmouse->disconnect(psmouse);
1045 if (parent && parent->pt_deactivate)
1046 parent->pt_deactivate(parent);
1048 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1050 serio_close(serio);
1051 serio_set_drvdata(serio, NULL);
1052 input_unregister_device(psmouse->dev);
1053 kfree(psmouse);
1055 if (parent)
1056 psmouse_activate(parent);
1058 mutex_unlock(&psmouse_mutex);
1061 static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
1063 struct input_dev *input_dev = psmouse->dev;
1065 input_dev->private = psmouse;
1066 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
1068 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1069 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1070 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
1072 psmouse->set_rate = psmouse_set_rate;
1073 psmouse->set_resolution = psmouse_set_resolution;
1074 psmouse->poll = psmouse_poll;
1075 psmouse->protocol_handler = psmouse_process_byte;
1076 psmouse->pktsize = 3;
1078 if (proto && (proto->detect || proto->init)) {
1079 if (proto->detect && proto->detect(psmouse, 1) < 0)
1080 return -1;
1082 if (proto->init && proto->init(psmouse) < 0)
1083 return -1;
1085 psmouse->type = proto->type;
1087 else
1088 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1091 * If mouse's packet size is 3 there is no point in polling the
1092 * device in hopes to detect protocol reset - we won't get less
1093 * than 3 bytes response anyhow.
1095 if (psmouse->pktsize == 3)
1096 psmouse->resync_time = 0;
1099 * Some smart KVMs fake response to POLL command returning just
1100 * 3 bytes and messing up our resync logic, so if initial poll
1101 * fails we won't try polling the device anymore. Hopefully
1102 * such KVM will maintain initially selected protocol.
1104 if (psmouse->resync_time && psmouse->poll(psmouse))
1105 psmouse->resync_time = 0;
1107 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1108 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1110 input_dev->name = psmouse->devname;
1111 input_dev->phys = psmouse->phys;
1112 input_dev->id.bustype = BUS_I8042;
1113 input_dev->id.vendor = 0x0002;
1114 input_dev->id.product = psmouse->type;
1115 input_dev->id.version = psmouse->model;
1117 return 0;
1121 * psmouse_connect() is a callback from the serio module when
1122 * an unhandled serio port is found.
1124 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1126 struct psmouse *psmouse, *parent = NULL;
1127 struct input_dev *input_dev;
1128 int retval = 0, error = -ENOMEM;
1130 mutex_lock(&psmouse_mutex);
1133 * If this is a pass-through port deactivate parent so the device
1134 * connected to this port can be successfully identified
1136 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1137 parent = serio_get_drvdata(serio->parent);
1138 psmouse_deactivate(parent);
1141 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1142 input_dev = input_allocate_device();
1143 if (!psmouse || !input_dev)
1144 goto err_free;
1146 ps2_init(&psmouse->ps2dev, serio);
1147 INIT_WORK(&psmouse->resync_work, psmouse_resync);
1148 psmouse->dev = input_dev;
1149 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1151 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1153 serio_set_drvdata(serio, psmouse);
1155 error = serio_open(serio, drv);
1156 if (error)
1157 goto err_clear_drvdata;
1159 if (psmouse_probe(psmouse) < 0) {
1160 error = -ENODEV;
1161 goto err_close_serio;
1164 psmouse->rate = psmouse_rate;
1165 psmouse->resolution = psmouse_resolution;
1166 psmouse->resetafter = psmouse_resetafter;
1167 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1168 psmouse->smartscroll = psmouse_smartscroll;
1170 psmouse_switch_protocol(psmouse, NULL);
1172 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1173 psmouse_initialize(psmouse);
1175 error = input_register_device(psmouse->dev);
1176 if (error)
1177 goto err_protocol_disconnect;
1179 if (parent && parent->pt_activate)
1180 parent->pt_activate(parent);
1182 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1183 if (error)
1184 goto err_pt_deactivate;
1186 psmouse_activate(psmouse);
1188 out:
1189 /* If this is a pass-through port the parent needs to be re-activated */
1190 if (parent)
1191 psmouse_activate(parent);
1193 mutex_unlock(&psmouse_mutex);
1194 return retval;
1196 err_pt_deactivate:
1197 if (parent && parent->pt_deactivate)
1198 parent->pt_deactivate(parent);
1199 err_protocol_disconnect:
1200 if (psmouse->disconnect)
1201 psmouse->disconnect(psmouse);
1202 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1203 err_close_serio:
1204 serio_close(serio);
1205 err_clear_drvdata:
1206 serio_set_drvdata(serio, NULL);
1207 err_free:
1208 input_free_device(input_dev);
1209 kfree(psmouse);
1211 retval = error;
1212 goto out;
1216 static int psmouse_reconnect(struct serio *serio)
1218 struct psmouse *psmouse = serio_get_drvdata(serio);
1219 struct psmouse *parent = NULL;
1220 struct serio_driver *drv = serio->drv;
1221 int rc = -1;
1223 if (!drv || !psmouse) {
1224 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1225 return -1;
1228 mutex_lock(&psmouse_mutex);
1230 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1231 parent = serio_get_drvdata(serio->parent);
1232 psmouse_deactivate(parent);
1235 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1237 if (psmouse->reconnect) {
1238 if (psmouse->reconnect(psmouse))
1239 goto out;
1240 } else if (psmouse_probe(psmouse) < 0 ||
1241 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1242 goto out;
1244 /* ok, the device type (and capabilities) match the old one,
1245 * we can continue using it, complete intialization
1247 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1249 psmouse_initialize(psmouse);
1251 if (parent && parent->pt_activate)
1252 parent->pt_activate(parent);
1254 psmouse_activate(psmouse);
1255 rc = 0;
1257 out:
1258 /* If this is a pass-through port the parent waits to be activated */
1259 if (parent)
1260 psmouse_activate(parent);
1262 mutex_unlock(&psmouse_mutex);
1263 return rc;
1266 static struct serio_device_id psmouse_serio_ids[] = {
1268 .type = SERIO_8042,
1269 .proto = SERIO_ANY,
1270 .id = SERIO_ANY,
1271 .extra = SERIO_ANY,
1274 .type = SERIO_PS_PSTHRU,
1275 .proto = SERIO_ANY,
1276 .id = SERIO_ANY,
1277 .extra = SERIO_ANY,
1279 { 0 }
1282 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1284 static struct serio_driver psmouse_drv = {
1285 .driver = {
1286 .name = "psmouse",
1288 .description = DRIVER_DESC,
1289 .id_table = psmouse_serio_ids,
1290 .interrupt = psmouse_interrupt,
1291 .connect = psmouse_connect,
1292 .reconnect = psmouse_reconnect,
1293 .disconnect = psmouse_disconnect,
1294 .cleanup = psmouse_cleanup,
1297 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1298 char *buf)
1300 struct serio *serio = to_serio_port(dev);
1301 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1302 struct psmouse *psmouse;
1303 int retval;
1305 retval = serio_pin_driver(serio);
1306 if (retval)
1307 return retval;
1309 if (serio->drv != &psmouse_drv) {
1310 retval = -ENODEV;
1311 goto out;
1314 psmouse = serio_get_drvdata(serio);
1316 retval = attr->show(psmouse, attr->data, buf);
1318 out:
1319 serio_unpin_driver(serio);
1320 return retval;
1323 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1324 const char *buf, size_t count)
1326 struct serio *serio = to_serio_port(dev);
1327 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1328 struct psmouse *psmouse, *parent = NULL;
1329 int retval;
1331 retval = serio_pin_driver(serio);
1332 if (retval)
1333 return retval;
1335 if (serio->drv != &psmouse_drv) {
1336 retval = -ENODEV;
1337 goto out_unpin;
1340 retval = mutex_lock_interruptible(&psmouse_mutex);
1341 if (retval)
1342 goto out_unpin;
1344 psmouse = serio_get_drvdata(serio);
1346 if (psmouse->state == PSMOUSE_IGNORE) {
1347 retval = -ENODEV;
1348 goto out_unlock;
1351 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1352 parent = serio_get_drvdata(serio->parent);
1353 psmouse_deactivate(parent);
1356 psmouse_deactivate(psmouse);
1358 retval = attr->set(psmouse, attr->data, buf, count);
1360 if (retval != -ENODEV)
1361 psmouse_activate(psmouse);
1363 if (parent)
1364 psmouse_activate(parent);
1366 out_unlock:
1367 mutex_unlock(&psmouse_mutex);
1368 out_unpin:
1369 serio_unpin_driver(serio);
1370 return retval;
1373 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1375 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1377 return sprintf(buf, "%u\n", *field);
1380 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1382 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1383 unsigned long value;
1384 char *rest;
1386 value = simple_strtoul(buf, &rest, 10);
1387 if (*rest)
1388 return -EINVAL;
1390 if ((unsigned int)value != value)
1391 return -EINVAL;
1393 *field = value;
1395 return count;
1398 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1400 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1403 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1405 struct serio *serio = psmouse->ps2dev.serio;
1406 struct psmouse *parent = NULL;
1407 struct input_dev *old_dev, *new_dev;
1408 const struct psmouse_protocol *proto, *old_proto;
1409 int error;
1410 int retry = 0;
1412 proto = psmouse_protocol_by_name(buf, count);
1413 if (!proto)
1414 return -EINVAL;
1416 if (psmouse->type == proto->type)
1417 return count;
1419 new_dev = input_allocate_device();
1420 if (!new_dev)
1421 return -ENOMEM;
1423 while (serio->child) {
1424 if (++retry > 3) {
1425 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
1426 input_free_device(new_dev);
1427 return -EIO;
1430 mutex_unlock(&psmouse_mutex);
1431 serio_unpin_driver(serio);
1432 serio_unregister_child_port(serio);
1433 serio_pin_driver_uninterruptible(serio);
1434 mutex_lock(&psmouse_mutex);
1436 if (serio->drv != &psmouse_drv) {
1437 input_free_device(new_dev);
1438 return -ENODEV;
1441 if (psmouse->type == proto->type) {
1442 input_free_device(new_dev);
1443 return count; /* switched by other thread */
1447 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1448 parent = serio_get_drvdata(serio->parent);
1449 if (parent->pt_deactivate)
1450 parent->pt_deactivate(parent);
1453 old_dev = psmouse->dev;
1454 old_proto = psmouse_protocol_by_type(psmouse->type);
1456 if (psmouse->disconnect)
1457 psmouse->disconnect(psmouse);
1459 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1461 psmouse->dev = new_dev;
1462 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1464 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1465 psmouse_reset(psmouse);
1466 /* default to PSMOUSE_PS2 */
1467 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1470 psmouse_initialize(psmouse);
1471 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1473 error = input_register_device(psmouse->dev);
1474 if (error) {
1475 if (psmouse->disconnect)
1476 psmouse->disconnect(psmouse);
1478 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1479 input_free_device(new_dev);
1480 psmouse->dev = old_dev;
1481 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1482 psmouse_switch_protocol(psmouse, old_proto);
1483 psmouse_initialize(psmouse);
1484 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1486 return error;
1489 input_unregister_device(old_dev);
1491 if (parent && parent->pt_activate)
1492 parent->pt_activate(parent);
1494 return count;
1497 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1499 unsigned long value;
1500 char *rest;
1502 value = simple_strtoul(buf, &rest, 10);
1503 if (*rest)
1504 return -EINVAL;
1506 psmouse->set_rate(psmouse, value);
1507 return count;
1510 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1512 unsigned long value;
1513 char *rest;
1515 value = simple_strtoul(buf, &rest, 10);
1516 if (*rest)
1517 return -EINVAL;
1519 psmouse->set_resolution(psmouse, value);
1520 return count;
1524 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1526 const struct psmouse_protocol *proto;
1528 if (!val)
1529 return -EINVAL;
1531 proto = psmouse_protocol_by_name(val, strlen(val));
1533 if (!proto || !proto->maxproto)
1534 return -EINVAL;
1536 *((unsigned int *)kp->arg) = proto->type;
1538 return 0;
1541 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1543 int type = *((unsigned int *)kp->arg);
1545 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
1548 static int __init psmouse_init(void)
1550 int err;
1552 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1553 if (!kpsmoused_wq) {
1554 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1555 return -ENOMEM;
1558 err = serio_register_driver(&psmouse_drv);
1559 if (err)
1560 destroy_workqueue(kpsmoused_wq);
1562 return err;
1565 static void __exit psmouse_exit(void)
1567 serio_unregister_driver(&psmouse_drv);
1568 destroy_workqueue(kpsmoused_wq);
1571 module_init(psmouse_init);
1572 module_exit(psmouse_exit);