rt2x00: Simplify rt2x00_check_rev
[linux/fpc-iii.git] / drivers / input / mouse / psmouse-base.c
blobf8f86de694bbe6cdeced1097d0ac57a4aebab44c
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/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/input.h>
19 #include <linux/serio.h>
20 #include <linux/init.h>
21 #include <linux/libps2.h>
22 #include <linux/mutex.h>
24 #include "psmouse.h"
25 #include "synaptics.h"
26 #include "logips2pp.h"
27 #include "alps.h"
28 #include "hgpk.h"
29 #include "lifebook.h"
30 #include "trackpoint.h"
31 #include "touchkit_ps2.h"
32 #include "elantech.h"
34 #define DRIVER_DESC "PS/2 mouse driver"
36 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
37 MODULE_DESCRIPTION(DRIVER_DESC);
38 MODULE_LICENSE("GPL");
40 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
41 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
42 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
43 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
44 #define param_set_proto_abbrev psmouse_set_maxproto
45 #define param_get_proto_abbrev psmouse_get_maxproto
46 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
47 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
49 static unsigned int psmouse_resolution = 200;
50 module_param_named(resolution, psmouse_resolution, uint, 0644);
51 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
53 static unsigned int psmouse_rate = 100;
54 module_param_named(rate, psmouse_rate, uint, 0644);
55 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
57 static unsigned int psmouse_smartscroll = 1;
58 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
59 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
61 static unsigned int psmouse_resetafter = 5;
62 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
63 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
65 static unsigned int psmouse_resync_time;
66 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
67 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
69 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
70 NULL,
71 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
72 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
73 (void *) offsetof(struct psmouse, rate),
74 psmouse_show_int_attr, psmouse_attr_set_rate);
75 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
76 (void *) offsetof(struct psmouse, resolution),
77 psmouse_show_int_attr, psmouse_attr_set_resolution);
78 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
79 (void *) offsetof(struct psmouse, resetafter),
80 psmouse_show_int_attr, psmouse_set_int_attr);
81 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
82 (void *) offsetof(struct psmouse, resync_time),
83 psmouse_show_int_attr, psmouse_set_int_attr);
85 static struct attribute *psmouse_attributes[] = {
86 &psmouse_attr_protocol.dattr.attr,
87 &psmouse_attr_rate.dattr.attr,
88 &psmouse_attr_resolution.dattr.attr,
89 &psmouse_attr_resetafter.dattr.attr,
90 &psmouse_attr_resync_time.dattr.attr,
91 NULL
94 static struct attribute_group psmouse_attribute_group = {
95 .attrs = psmouse_attributes,
99 * psmouse_mutex protects all operations changing state of mouse
100 * (connecting, disconnecting, changing rate or resolution via
101 * sysfs). We could use a per-device semaphore but since there
102 * rarely more than one PS/2 mouse connected and since semaphore
103 * is taken in "slow" paths it is not worth it.
105 static DEFINE_MUTEX(psmouse_mutex);
107 static struct workqueue_struct *kpsmoused_wq;
109 struct psmouse_protocol {
110 enum psmouse_type type;
111 const char *name;
112 const char *alias;
113 int maxproto;
114 int (*detect)(struct psmouse *, int);
115 int (*init)(struct psmouse *);
119 * psmouse_process_byte() analyzes the PS/2 data stream and reports
120 * relevant events to the input module once full packet has arrived.
123 static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
125 struct input_dev *dev = psmouse->dev;
126 unsigned char *packet = psmouse->packet;
128 if (psmouse->pktcnt < psmouse->pktsize)
129 return PSMOUSE_GOOD_DATA;
132 * Full packet accumulated, process it
136 * Scroll wheel on IntelliMice, scroll buttons on NetMice
139 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
140 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
143 * Scroll wheel and buttons on IntelliMouse Explorer
146 if (psmouse->type == PSMOUSE_IMEX) {
147 switch (packet[3] & 0xC0) {
148 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
149 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
150 break;
151 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
152 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
153 break;
154 case 0x00:
155 case 0xC0:
156 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
157 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
158 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
159 break;
164 * Extra buttons on Genius NewNet 3D
167 if (psmouse->type == PSMOUSE_GENPS) {
168 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
169 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
173 * Extra button on ThinkingMouse
175 if (psmouse->type == PSMOUSE_THINKPS) {
176 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
177 /* Without this bit of weirdness moving up gives wildly high Y changes. */
178 packet[1] |= (packet[0] & 0x40) << 1;
182 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
183 * byte.
185 if (psmouse->type == PSMOUSE_CORTRON) {
186 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
187 packet[0] |= 0x08;
191 * Generic PS/2 Mouse
194 input_report_key(dev, BTN_LEFT, packet[0] & 1);
195 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
196 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
198 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
199 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
201 input_sync(dev);
203 return PSMOUSE_FULL_PACKET;
206 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
207 unsigned long delay)
209 queue_delayed_work(kpsmoused_wq, work, delay);
213 * __psmouse_set_state() sets new psmouse state and resets all flags.
216 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
218 psmouse->state = new_state;
219 psmouse->pktcnt = psmouse->out_of_sync = 0;
220 psmouse->ps2dev.flags = 0;
221 psmouse->last = jiffies;
226 * psmouse_set_state() sets new psmouse state and resets all flags and
227 * counters while holding serio lock so fighting with interrupt handler
228 * is not a concern.
231 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
233 serio_pause_rx(psmouse->ps2dev.serio);
234 __psmouse_set_state(psmouse, new_state);
235 serio_continue_rx(psmouse->ps2dev.serio);
239 * psmouse_handle_byte() processes one byte of the input data stream
240 * by calling corresponding protocol handler.
243 static int psmouse_handle_byte(struct psmouse *psmouse)
245 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
247 switch (rc) {
248 case PSMOUSE_BAD_DATA:
249 if (psmouse->state == PSMOUSE_ACTIVATED) {
250 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
251 psmouse->name, psmouse->phys, psmouse->pktcnt);
252 if (++psmouse->out_of_sync == psmouse->resetafter) {
253 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
254 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
255 serio_reconnect(psmouse->ps2dev.serio);
256 return -1;
259 psmouse->pktcnt = 0;
260 break;
262 case PSMOUSE_FULL_PACKET:
263 psmouse->pktcnt = 0;
264 if (psmouse->out_of_sync) {
265 psmouse->out_of_sync = 0;
266 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
267 psmouse->name, psmouse->phys);
269 break;
271 case PSMOUSE_GOOD_DATA:
272 break;
274 return 0;
278 * psmouse_interrupt() handles incoming characters, either passing them
279 * for normal processing or gathering them as command response.
282 static irqreturn_t psmouse_interrupt(struct serio *serio,
283 unsigned char data, unsigned int flags)
285 struct psmouse *psmouse = serio_get_drvdata(serio);
287 if (psmouse->state == PSMOUSE_IGNORE)
288 goto out;
290 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
291 if (psmouse->state == PSMOUSE_ACTIVATED)
292 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
293 flags & SERIO_TIMEOUT ? " timeout" : "",
294 flags & SERIO_PARITY ? " bad parity" : "");
295 ps2_cmd_aborted(&psmouse->ps2dev);
296 goto out;
299 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
300 if (ps2_handle_ack(&psmouse->ps2dev, data))
301 goto out;
303 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
304 if (ps2_handle_response(&psmouse->ps2dev, data))
305 goto out;
307 if (psmouse->state <= PSMOUSE_RESYNCING)
308 goto out;
310 if (psmouse->state == PSMOUSE_ACTIVATED &&
311 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
312 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
313 psmouse->name, psmouse->phys, psmouse->pktcnt);
314 psmouse->badbyte = psmouse->packet[0];
315 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
316 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
317 goto out;
320 psmouse->packet[psmouse->pktcnt++] = data;
322 * Check if this is a new device announcement (0xAA 0x00)
324 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
325 if (psmouse->pktcnt == 1) {
326 psmouse->last = jiffies;
327 goto out;
330 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
331 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
332 serio_reconnect(serio);
333 goto out;
336 * Not a new device, try processing first byte normally
338 psmouse->pktcnt = 1;
339 if (psmouse_handle_byte(psmouse))
340 goto out;
342 psmouse->packet[psmouse->pktcnt++] = data;
346 * See if we need to force resync because mouse was idle for too long
348 if (psmouse->state == PSMOUSE_ACTIVATED &&
349 psmouse->pktcnt == 1 && psmouse->resync_time &&
350 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
351 psmouse->badbyte = psmouse->packet[0];
352 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
353 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
354 goto out;
357 psmouse->last = jiffies;
358 psmouse_handle_byte(psmouse);
360 out:
361 return IRQ_HANDLED;
366 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
367 * using sliced syntax, understood by advanced devices, such as Logitech
368 * or Synaptics touchpads. The command is encoded as:
369 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
370 * is the command.
372 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
374 int i;
376 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
377 return -1;
379 for (i = 6; i >= 0; i -= 2) {
380 unsigned char d = (command >> i) & 3;
381 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
382 return -1;
385 return 0;
390 * psmouse_reset() resets the mouse into power-on state.
392 int psmouse_reset(struct psmouse *psmouse)
394 unsigned char param[2];
396 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
397 return -1;
399 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
400 return -1;
402 return 0;
407 * Genius NetMouse magic init.
409 static int genius_detect(struct psmouse *psmouse, int set_properties)
411 struct ps2dev *ps2dev = &psmouse->ps2dev;
412 unsigned char param[4];
414 param[0] = 3;
415 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
416 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
417 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
418 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
419 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
421 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
422 return -1;
424 if (set_properties) {
425 set_bit(BTN_EXTRA, psmouse->dev->keybit);
426 set_bit(BTN_SIDE, psmouse->dev->keybit);
427 set_bit(REL_WHEEL, psmouse->dev->relbit);
429 psmouse->vendor = "Genius";
430 psmouse->name = "Mouse";
431 psmouse->pktsize = 4;
434 return 0;
438 * IntelliMouse magic init.
440 static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
442 struct ps2dev *ps2dev = &psmouse->ps2dev;
443 unsigned char param[2];
445 param[0] = 200;
446 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
447 param[0] = 100;
448 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
449 param[0] = 80;
450 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
451 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
453 if (param[0] != 3)
454 return -1;
456 if (set_properties) {
457 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
458 set_bit(REL_WHEEL, psmouse->dev->relbit);
460 if (!psmouse->vendor) psmouse->vendor = "Generic";
461 if (!psmouse->name) psmouse->name = "Wheel Mouse";
462 psmouse->pktsize = 4;
465 return 0;
469 * Try IntelliMouse/Explorer magic init.
471 static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
473 struct ps2dev *ps2dev = &psmouse->ps2dev;
474 unsigned char param[2];
476 intellimouse_detect(psmouse, 0);
478 param[0] = 200;
479 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
480 param[0] = 200;
481 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
482 param[0] = 80;
483 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
484 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
486 if (param[0] != 4)
487 return -1;
489 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
490 param[0] = 200;
491 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
492 param[0] = 80;
493 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
494 param[0] = 40;
495 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
497 if (set_properties) {
498 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
499 set_bit(REL_WHEEL, psmouse->dev->relbit);
500 set_bit(REL_HWHEEL, psmouse->dev->relbit);
501 set_bit(BTN_SIDE, psmouse->dev->keybit);
502 set_bit(BTN_EXTRA, psmouse->dev->keybit);
504 if (!psmouse->vendor) psmouse->vendor = "Generic";
505 if (!psmouse->name) psmouse->name = "Explorer Mouse";
506 psmouse->pktsize = 4;
509 return 0;
513 * Kensington ThinkingMouse / ExpertMouse magic init.
515 static int thinking_detect(struct psmouse *psmouse, int set_properties)
517 struct ps2dev *ps2dev = &psmouse->ps2dev;
518 unsigned char param[2];
519 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
520 int i;
522 param[0] = 10;
523 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
524 param[0] = 0;
525 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
526 for (i = 0; i < ARRAY_SIZE(seq); i++) {
527 param[0] = seq[i];
528 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
530 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
532 if (param[0] != 2)
533 return -1;
535 if (set_properties) {
536 set_bit(BTN_EXTRA, psmouse->dev->keybit);
538 psmouse->vendor = "Kensington";
539 psmouse->name = "ThinkingMouse";
542 return 0;
546 * Bare PS/2 protocol "detection". Always succeeds.
548 static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
550 if (set_properties) {
551 if (!psmouse->vendor) psmouse->vendor = "Generic";
552 if (!psmouse->name) psmouse->name = "Mouse";
555 return 0;
559 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
560 * must be forced by sysfs protocol writing.
562 static int cortron_detect(struct psmouse *psmouse, int set_properties)
564 if (set_properties) {
565 psmouse->vendor = "Cortron";
566 psmouse->name = "PS/2 Trackball";
567 set_bit(BTN_SIDE, psmouse->dev->keybit);
570 return 0;
574 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
575 * the mouse may have.
578 static int psmouse_extensions(struct psmouse *psmouse,
579 unsigned int max_proto, int set_properties)
581 int synaptics_hardware = 0;
584 * We always check for lifebook because it does not disturb mouse
585 * (it only checks DMI information).
587 if (lifebook_detect(psmouse, set_properties) == 0) {
588 if (max_proto > PSMOUSE_IMEX) {
589 if (!set_properties || lifebook_init(psmouse) == 0)
590 return PSMOUSE_LIFEBOOK;
595 * Try Kensington ThinkingMouse (we try first, because synaptics probe
596 * upsets the thinkingmouse).
599 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
600 return PSMOUSE_THINKPS;
603 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
604 * support is disabled in config - we need to know if it is synaptics so we
605 * can reset it properly after probing for intellimouse.
607 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
608 synaptics_hardware = 1;
610 if (max_proto > PSMOUSE_IMEX) {
611 if (!set_properties || synaptics_init(psmouse) == 0)
612 return PSMOUSE_SYNAPTICS;
614 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
615 * Unfortunately Logitech/Genius probes confuse some firmware versions so
616 * we'll have to skip them.
618 max_proto = PSMOUSE_IMEX;
621 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
623 synaptics_reset(psmouse);
627 * Try ALPS TouchPad
629 if (max_proto > PSMOUSE_IMEX) {
630 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
631 if (alps_detect(psmouse, set_properties) == 0) {
632 if (!set_properties || alps_init(psmouse) == 0)
633 return PSMOUSE_ALPS;
635 * Init failed, try basic relative protocols
637 max_proto = PSMOUSE_IMEX;
642 * Try OLPC HGPK touchpad.
644 if (max_proto > PSMOUSE_IMEX &&
645 hgpk_detect(psmouse, set_properties) == 0) {
646 if (!set_properties || hgpk_init(psmouse) == 0)
647 return PSMOUSE_HGPK;
649 * Init failed, try basic relative protocols
651 max_proto = PSMOUSE_IMEX;
655 * Try Elantech touchpad.
657 if (max_proto > PSMOUSE_IMEX &&
658 elantech_detect(psmouse, set_properties) == 0) {
659 if (!set_properties || elantech_init(psmouse) == 0)
660 return PSMOUSE_ELANTECH;
662 * Init failed, try basic relative protocols
664 max_proto = PSMOUSE_IMEX;
667 if (max_proto > PSMOUSE_IMEX) {
668 if (genius_detect(psmouse, set_properties) == 0)
669 return PSMOUSE_GENPS;
671 if (ps2pp_init(psmouse, set_properties) == 0)
672 return PSMOUSE_PS2PP;
674 if (trackpoint_detect(psmouse, set_properties) == 0)
675 return PSMOUSE_TRACKPOINT;
677 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
678 return PSMOUSE_TOUCHKIT_PS2;
682 * Reset to defaults in case the device got confused by extended
683 * protocol probes. Note that we follow up with full reset because
684 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
686 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
687 psmouse_reset(psmouse);
689 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
690 return PSMOUSE_IMEX;
692 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
693 return PSMOUSE_IMPS;
696 * Okay, all failed, we have a standard mouse here. The number of the buttons
697 * is still a question, though. We assume 3.
699 ps2bare_detect(psmouse, set_properties);
701 if (synaptics_hardware) {
703 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
704 * We need to reset the touchpad because if there is a track point on the
705 * pass through port it could get disabled while probing for protocol
706 * extensions.
708 psmouse_reset(psmouse);
711 return PSMOUSE_PS2;
714 static const struct psmouse_protocol psmouse_protocols[] = {
716 .type = PSMOUSE_PS2,
717 .name = "PS/2",
718 .alias = "bare",
719 .maxproto = 1,
720 .detect = ps2bare_detect,
722 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
724 .type = PSMOUSE_PS2PP,
725 .name = "PS2++",
726 .alias = "logitech",
727 .detect = ps2pp_init,
729 #endif
731 .type = PSMOUSE_THINKPS,
732 .name = "ThinkPS/2",
733 .alias = "thinkps",
734 .detect = thinking_detect,
737 .type = PSMOUSE_GENPS,
738 .name = "GenPS/2",
739 .alias = "genius",
740 .detect = genius_detect,
743 .type = PSMOUSE_IMPS,
744 .name = "ImPS/2",
745 .alias = "imps",
746 .maxproto = 1,
747 .detect = intellimouse_detect,
750 .type = PSMOUSE_IMEX,
751 .name = "ImExPS/2",
752 .alias = "exps",
753 .maxproto = 1,
754 .detect = im_explorer_detect,
756 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
758 .type = PSMOUSE_SYNAPTICS,
759 .name = "SynPS/2",
760 .alias = "synaptics",
761 .detect = synaptics_detect,
762 .init = synaptics_init,
764 #endif
765 #ifdef CONFIG_MOUSE_PS2_ALPS
767 .type = PSMOUSE_ALPS,
768 .name = "AlpsPS/2",
769 .alias = "alps",
770 .detect = alps_detect,
771 .init = alps_init,
773 #endif
774 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
776 .type = PSMOUSE_LIFEBOOK,
777 .name = "LBPS/2",
778 .alias = "lifebook",
779 .init = lifebook_init,
781 #endif
782 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
784 .type = PSMOUSE_TRACKPOINT,
785 .name = "TPPS/2",
786 .alias = "trackpoint",
787 .detect = trackpoint_detect,
789 #endif
790 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
792 .type = PSMOUSE_TOUCHKIT_PS2,
793 .name = "touchkitPS/2",
794 .alias = "touchkit",
795 .detect = touchkit_ps2_detect,
797 #endif
798 #ifdef CONFIG_MOUSE_PS2_OLPC
800 .type = PSMOUSE_HGPK,
801 .name = "OLPC HGPK",
802 .alias = "hgpk",
803 .detect = hgpk_detect,
805 #endif
806 #ifdef CONFIG_MOUSE_PS2_ELANTECH
808 .type = PSMOUSE_ELANTECH,
809 .name = "ETPS/2",
810 .alias = "elantech",
811 .detect = elantech_detect,
812 .init = elantech_init,
814 #endif
816 .type = PSMOUSE_CORTRON,
817 .name = "CortronPS/2",
818 .alias = "cortps",
819 .detect = cortron_detect,
822 .type = PSMOUSE_AUTO,
823 .name = "auto",
824 .alias = "any",
825 .maxproto = 1,
829 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
831 int i;
833 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
834 if (psmouse_protocols[i].type == type)
835 return &psmouse_protocols[i];
837 WARN_ON(1);
838 return &psmouse_protocols[0];
841 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
843 const struct psmouse_protocol *p;
844 int i;
846 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
847 p = &psmouse_protocols[i];
849 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
850 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
851 return &psmouse_protocols[i];
854 return NULL;
859 * psmouse_probe() probes for a PS/2 mouse.
862 static int psmouse_probe(struct psmouse *psmouse)
864 struct ps2dev *ps2dev = &psmouse->ps2dev;
865 unsigned char param[2];
868 * First, we check if it's a mouse. It should send 0x00 or 0x03
869 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
870 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
871 * ID queries, probably due to a firmware bug.
874 param[0] = 0xa5;
875 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
876 return -1;
878 if (param[0] != 0x00 && param[0] != 0x03 &&
879 param[0] != 0x04 && param[0] != 0xff)
880 return -1;
883 * Then we reset and disable the mouse so that it doesn't generate events.
886 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
887 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
889 return 0;
893 * Here we set the mouse resolution.
896 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
898 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
899 unsigned char p;
901 if (resolution == 0 || resolution > 200)
902 resolution = 200;
904 p = params[resolution / 50];
905 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
906 psmouse->resolution = 25 << p;
910 * Here we set the mouse report rate.
913 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
915 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
916 unsigned char r;
917 int i = 0;
919 while (rates[i] > rate) i++;
920 r = rates[i];
921 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
922 psmouse->rate = r;
926 * psmouse_initialize() initializes the mouse to a sane state.
929 static void psmouse_initialize(struct psmouse *psmouse)
932 * We set the mouse report rate, resolution and scaling.
935 if (psmouse_max_proto != PSMOUSE_PS2) {
936 psmouse->set_rate(psmouse, psmouse->rate);
937 psmouse->set_resolution(psmouse, psmouse->resolution);
938 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
943 * psmouse_activate() enables the mouse so that we get motion reports from it.
946 static void psmouse_activate(struct psmouse *psmouse)
948 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
949 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
950 psmouse->ps2dev.serio->phys);
952 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
957 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
958 * reports from it unless we explicitly request it.
961 static void psmouse_deactivate(struct psmouse *psmouse)
963 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
964 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
965 psmouse->ps2dev.serio->phys);
967 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
971 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
974 static int psmouse_poll(struct psmouse *psmouse)
976 return ps2_command(&psmouse->ps2dev, psmouse->packet,
977 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
982 * psmouse_resync() attempts to re-validate current protocol.
985 static void psmouse_resync(struct work_struct *work)
987 struct psmouse *parent = NULL, *psmouse =
988 container_of(work, struct psmouse, resync_work.work);
989 struct serio *serio = psmouse->ps2dev.serio;
990 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
991 int failed = 0, enabled = 0;
992 int i;
994 mutex_lock(&psmouse_mutex);
996 if (psmouse->state != PSMOUSE_RESYNCING)
997 goto out;
999 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1000 parent = serio_get_drvdata(serio->parent);
1001 psmouse_deactivate(parent);
1005 * Some mice don't ACK commands sent while they are in the middle of
1006 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1007 * instead of ps2_command() which would wait for 200ms for an ACK
1008 * that may never come.
1009 * As an additional quirk ALPS touchpads may not only forget to ACK
1010 * disable command but will stop reporting taps, so if we see that
1011 * mouse at least once ACKs disable we will do full reconnect if ACK
1012 * is missing.
1014 psmouse->num_resyncs++;
1016 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1017 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1018 failed = 1;
1019 } else
1020 psmouse->acks_disable_command = 1;
1023 * Poll the mouse. If it was reset the packet will be shorter than
1024 * psmouse->pktsize and ps2_command will fail. We do not expect and
1025 * do not handle scenario when mouse "upgrades" its protocol while
1026 * disconnected since it would require additional delay. If we ever
1027 * see a mouse that does it we'll adjust the code.
1029 if (!failed) {
1030 if (psmouse->poll(psmouse))
1031 failed = 1;
1032 else {
1033 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1034 for (i = 0; i < psmouse->pktsize; i++) {
1035 psmouse->pktcnt++;
1036 rc = psmouse->protocol_handler(psmouse);
1037 if (rc != PSMOUSE_GOOD_DATA)
1038 break;
1040 if (rc != PSMOUSE_FULL_PACKET)
1041 failed = 1;
1042 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1046 * Now try to enable mouse. We try to do that even if poll failed and also
1047 * repeat our attempts 5 times, otherwise we may be left out with disabled
1048 * mouse.
1050 for (i = 0; i < 5; i++) {
1051 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1052 enabled = 1;
1053 break;
1055 msleep(200);
1058 if (!enabled) {
1059 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1060 psmouse->ps2dev.serio->phys);
1061 failed = 1;
1064 if (failed) {
1065 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1066 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1067 serio_reconnect(serio);
1068 } else
1069 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1071 if (parent)
1072 psmouse_activate(parent);
1073 out:
1074 mutex_unlock(&psmouse_mutex);
1078 * psmouse_cleanup() resets the mouse into power-on state.
1081 static void psmouse_cleanup(struct serio *serio)
1083 struct psmouse *psmouse = serio_get_drvdata(serio);
1084 struct psmouse *parent = NULL;
1086 mutex_lock(&psmouse_mutex);
1088 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1089 parent = serio_get_drvdata(serio->parent);
1090 psmouse_deactivate(parent);
1093 psmouse_deactivate(psmouse);
1095 if (psmouse->cleanup)
1096 psmouse->cleanup(psmouse);
1098 psmouse_reset(psmouse);
1101 * Some boxes, such as HP nx7400, get terribly confused if mouse
1102 * is not fully enabled before suspending/shutting down.
1104 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1106 if (parent) {
1107 if (parent->pt_deactivate)
1108 parent->pt_deactivate(parent);
1110 psmouse_activate(parent);
1113 mutex_unlock(&psmouse_mutex);
1117 * psmouse_disconnect() closes and frees.
1120 static void psmouse_disconnect(struct serio *serio)
1122 struct psmouse *psmouse, *parent = NULL;
1124 psmouse = serio_get_drvdata(serio);
1126 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1128 mutex_lock(&psmouse_mutex);
1130 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1132 /* make sure we don't have a resync in progress */
1133 mutex_unlock(&psmouse_mutex);
1134 flush_workqueue(kpsmoused_wq);
1135 mutex_lock(&psmouse_mutex);
1137 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1138 parent = serio_get_drvdata(serio->parent);
1139 psmouse_deactivate(parent);
1142 if (psmouse->disconnect)
1143 psmouse->disconnect(psmouse);
1145 if (parent && parent->pt_deactivate)
1146 parent->pt_deactivate(parent);
1148 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1150 serio_close(serio);
1151 serio_set_drvdata(serio, NULL);
1152 input_unregister_device(psmouse->dev);
1153 kfree(psmouse);
1155 if (parent)
1156 psmouse_activate(parent);
1158 mutex_unlock(&psmouse_mutex);
1161 static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
1163 struct input_dev *input_dev = psmouse->dev;
1165 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1167 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1168 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
1169 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1170 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1172 psmouse->set_rate = psmouse_set_rate;
1173 psmouse->set_resolution = psmouse_set_resolution;
1174 psmouse->poll = psmouse_poll;
1175 psmouse->protocol_handler = psmouse_process_byte;
1176 psmouse->pktsize = 3;
1178 if (proto && (proto->detect || proto->init)) {
1179 if (proto->detect && proto->detect(psmouse, 1) < 0)
1180 return -1;
1182 if (proto->init && proto->init(psmouse) < 0)
1183 return -1;
1185 psmouse->type = proto->type;
1187 else
1188 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1191 * If mouse's packet size is 3 there is no point in polling the
1192 * device in hopes to detect protocol reset - we won't get less
1193 * than 3 bytes response anyhow.
1195 if (psmouse->pktsize == 3)
1196 psmouse->resync_time = 0;
1199 * Some smart KVMs fake response to POLL command returning just
1200 * 3 bytes and messing up our resync logic, so if initial poll
1201 * fails we won't try polling the device anymore. Hopefully
1202 * such KVM will maintain initially selected protocol.
1204 if (psmouse->resync_time && psmouse->poll(psmouse))
1205 psmouse->resync_time = 0;
1207 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1208 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1210 input_dev->name = psmouse->devname;
1211 input_dev->phys = psmouse->phys;
1212 input_dev->id.bustype = BUS_I8042;
1213 input_dev->id.vendor = 0x0002;
1214 input_dev->id.product = psmouse->type;
1215 input_dev->id.version = psmouse->model;
1217 return 0;
1221 * psmouse_connect() is a callback from the serio module when
1222 * an unhandled serio port is found.
1224 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1226 struct psmouse *psmouse, *parent = NULL;
1227 struct input_dev *input_dev;
1228 int retval = 0, error = -ENOMEM;
1230 mutex_lock(&psmouse_mutex);
1233 * If this is a pass-through port deactivate parent so the device
1234 * connected to this port can be successfully identified
1236 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1237 parent = serio_get_drvdata(serio->parent);
1238 psmouse_deactivate(parent);
1241 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1242 input_dev = input_allocate_device();
1243 if (!psmouse || !input_dev)
1244 goto err_free;
1246 ps2_init(&psmouse->ps2dev, serio);
1247 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1248 psmouse->dev = input_dev;
1249 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1251 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1253 serio_set_drvdata(serio, psmouse);
1255 error = serio_open(serio, drv);
1256 if (error)
1257 goto err_clear_drvdata;
1259 if (psmouse_probe(psmouse) < 0) {
1260 error = -ENODEV;
1261 goto err_close_serio;
1264 psmouse->rate = psmouse_rate;
1265 psmouse->resolution = psmouse_resolution;
1266 psmouse->resetafter = psmouse_resetafter;
1267 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1268 psmouse->smartscroll = psmouse_smartscroll;
1270 psmouse_switch_protocol(psmouse, NULL);
1272 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1273 psmouse_initialize(psmouse);
1275 error = input_register_device(psmouse->dev);
1276 if (error)
1277 goto err_protocol_disconnect;
1279 if (parent && parent->pt_activate)
1280 parent->pt_activate(parent);
1282 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1283 if (error)
1284 goto err_pt_deactivate;
1286 psmouse_activate(psmouse);
1288 out:
1289 /* If this is a pass-through port the parent needs to be re-activated */
1290 if (parent)
1291 psmouse_activate(parent);
1293 mutex_unlock(&psmouse_mutex);
1294 return retval;
1296 err_pt_deactivate:
1297 if (parent && parent->pt_deactivate)
1298 parent->pt_deactivate(parent);
1299 input_unregister_device(psmouse->dev);
1300 input_dev = NULL; /* so we don't try to free it below */
1301 err_protocol_disconnect:
1302 if (psmouse->disconnect)
1303 psmouse->disconnect(psmouse);
1304 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1305 err_close_serio:
1306 serio_close(serio);
1307 err_clear_drvdata:
1308 serio_set_drvdata(serio, NULL);
1309 err_free:
1310 input_free_device(input_dev);
1311 kfree(psmouse);
1313 retval = error;
1314 goto out;
1318 static int psmouse_reconnect(struct serio *serio)
1320 struct psmouse *psmouse = serio_get_drvdata(serio);
1321 struct psmouse *parent = NULL;
1322 struct serio_driver *drv = serio->drv;
1323 int rc = -1;
1325 if (!drv || !psmouse) {
1326 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1327 return -1;
1330 mutex_lock(&psmouse_mutex);
1332 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1333 parent = serio_get_drvdata(serio->parent);
1334 psmouse_deactivate(parent);
1337 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1339 if (psmouse->reconnect) {
1340 if (psmouse->reconnect(psmouse))
1341 goto out;
1342 } else if (psmouse_probe(psmouse) < 0 ||
1343 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1344 goto out;
1346 /* ok, the device type (and capabilities) match the old one,
1347 * we can continue using it, complete intialization
1349 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1351 psmouse_initialize(psmouse);
1353 if (parent && parent->pt_activate)
1354 parent->pt_activate(parent);
1356 psmouse_activate(psmouse);
1357 rc = 0;
1359 out:
1360 /* If this is a pass-through port the parent waits to be activated */
1361 if (parent)
1362 psmouse_activate(parent);
1364 mutex_unlock(&psmouse_mutex);
1365 return rc;
1368 static struct serio_device_id psmouse_serio_ids[] = {
1370 .type = SERIO_8042,
1371 .proto = SERIO_ANY,
1372 .id = SERIO_ANY,
1373 .extra = SERIO_ANY,
1376 .type = SERIO_PS_PSTHRU,
1377 .proto = SERIO_ANY,
1378 .id = SERIO_ANY,
1379 .extra = SERIO_ANY,
1381 { 0 }
1384 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1386 static struct serio_driver psmouse_drv = {
1387 .driver = {
1388 .name = "psmouse",
1390 .description = DRIVER_DESC,
1391 .id_table = psmouse_serio_ids,
1392 .interrupt = psmouse_interrupt,
1393 .connect = psmouse_connect,
1394 .reconnect = psmouse_reconnect,
1395 .disconnect = psmouse_disconnect,
1396 .cleanup = psmouse_cleanup,
1399 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1400 char *buf)
1402 struct serio *serio = to_serio_port(dev);
1403 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1404 struct psmouse *psmouse;
1405 int retval;
1407 retval = serio_pin_driver(serio);
1408 if (retval)
1409 return retval;
1411 if (serio->drv != &psmouse_drv) {
1412 retval = -ENODEV;
1413 goto out;
1416 psmouse = serio_get_drvdata(serio);
1418 retval = attr->show(psmouse, attr->data, buf);
1420 out:
1421 serio_unpin_driver(serio);
1422 return retval;
1425 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1426 const char *buf, size_t count)
1428 struct serio *serio = to_serio_port(dev);
1429 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1430 struct psmouse *psmouse, *parent = NULL;
1431 int retval;
1433 retval = serio_pin_driver(serio);
1434 if (retval)
1435 return retval;
1437 if (serio->drv != &psmouse_drv) {
1438 retval = -ENODEV;
1439 goto out_unpin;
1442 retval = mutex_lock_interruptible(&psmouse_mutex);
1443 if (retval)
1444 goto out_unpin;
1446 psmouse = serio_get_drvdata(serio);
1448 if (attr->protect) {
1449 if (psmouse->state == PSMOUSE_IGNORE) {
1450 retval = -ENODEV;
1451 goto out_unlock;
1454 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1455 parent = serio_get_drvdata(serio->parent);
1456 psmouse_deactivate(parent);
1459 psmouse_deactivate(psmouse);
1462 retval = attr->set(psmouse, attr->data, buf, count);
1464 if (attr->protect) {
1465 if (retval != -ENODEV)
1466 psmouse_activate(psmouse);
1468 if (parent)
1469 psmouse_activate(parent);
1472 out_unlock:
1473 mutex_unlock(&psmouse_mutex);
1474 out_unpin:
1475 serio_unpin_driver(serio);
1476 return retval;
1479 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1481 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1483 return sprintf(buf, "%u\n", *field);
1486 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1488 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1489 unsigned long value;
1491 if (strict_strtoul(buf, 10, &value))
1492 return -EINVAL;
1494 if ((unsigned int)value != value)
1495 return -EINVAL;
1497 *field = value;
1499 return count;
1502 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1504 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1507 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1509 struct serio *serio = psmouse->ps2dev.serio;
1510 struct psmouse *parent = NULL;
1511 struct input_dev *old_dev, *new_dev;
1512 const struct psmouse_protocol *proto, *old_proto;
1513 int error;
1514 int retry = 0;
1516 proto = psmouse_protocol_by_name(buf, count);
1517 if (!proto)
1518 return -EINVAL;
1520 if (psmouse->type == proto->type)
1521 return count;
1523 new_dev = input_allocate_device();
1524 if (!new_dev)
1525 return -ENOMEM;
1527 while (serio->child) {
1528 if (++retry > 3) {
1529 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
1530 input_free_device(new_dev);
1531 return -EIO;
1534 mutex_unlock(&psmouse_mutex);
1535 serio_unpin_driver(serio);
1536 serio_unregister_child_port(serio);
1537 serio_pin_driver_uninterruptible(serio);
1538 mutex_lock(&psmouse_mutex);
1540 if (serio->drv != &psmouse_drv) {
1541 input_free_device(new_dev);
1542 return -ENODEV;
1545 if (psmouse->type == proto->type) {
1546 input_free_device(new_dev);
1547 return count; /* switched by other thread */
1551 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1552 parent = serio_get_drvdata(serio->parent);
1553 if (parent->pt_deactivate)
1554 parent->pt_deactivate(parent);
1557 old_dev = psmouse->dev;
1558 old_proto = psmouse_protocol_by_type(psmouse->type);
1560 if (psmouse->disconnect)
1561 psmouse->disconnect(psmouse);
1563 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1565 psmouse->dev = new_dev;
1566 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1568 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1569 psmouse_reset(psmouse);
1570 /* default to PSMOUSE_PS2 */
1571 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1574 psmouse_initialize(psmouse);
1575 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1577 error = input_register_device(psmouse->dev);
1578 if (error) {
1579 if (psmouse->disconnect)
1580 psmouse->disconnect(psmouse);
1582 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1583 input_free_device(new_dev);
1584 psmouse->dev = old_dev;
1585 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1586 psmouse_switch_protocol(psmouse, old_proto);
1587 psmouse_initialize(psmouse);
1588 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1590 return error;
1593 input_unregister_device(old_dev);
1595 if (parent && parent->pt_activate)
1596 parent->pt_activate(parent);
1598 return count;
1601 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1603 unsigned long value;
1605 if (strict_strtoul(buf, 10, &value))
1606 return -EINVAL;
1608 psmouse->set_rate(psmouse, value);
1609 return count;
1612 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1614 unsigned long value;
1616 if (strict_strtoul(buf, 10, &value))
1617 return -EINVAL;
1619 psmouse->set_resolution(psmouse, value);
1620 return count;
1624 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1626 const struct psmouse_protocol *proto;
1628 if (!val)
1629 return -EINVAL;
1631 proto = psmouse_protocol_by_name(val, strlen(val));
1633 if (!proto || !proto->maxproto)
1634 return -EINVAL;
1636 *((unsigned int *)kp->arg) = proto->type;
1638 return 0;
1641 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1643 int type = *((unsigned int *)kp->arg);
1645 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
1648 static int __init psmouse_init(void)
1650 int err;
1652 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1653 if (!kpsmoused_wq) {
1654 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1655 return -ENOMEM;
1658 err = serio_register_driver(&psmouse_drv);
1659 if (err)
1660 destroy_workqueue(kpsmoused_wq);
1662 return err;
1665 static void __exit psmouse_exit(void)
1667 serio_unregister_driver(&psmouse_drv);
1668 destroy_workqueue(kpsmoused_wq);
1671 module_init(psmouse_init);
1672 module_exit(psmouse_exit);