Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 23 | #include <linux/mutex.h> |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "psmouse.h" |
| 26 | #include "synaptics.h" |
| 27 | #include "logips2pp.h" |
| 28 | #include "alps.h" |
Kenan Esau | 02d7f58 | 2005-05-29 02:30:22 -0500 | [diff] [blame] | 29 | #include "lifebook.h" |
Stephen Evanchik | 541e316 | 2005-08-08 01:26:18 -0500 | [diff] [blame] | 30 | #include "trackpoint.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #define DRIVER_DESC "PS/2 mouse driver" |
| 33 | |
| 34 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); |
| 35 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 36 | MODULE_LICENSE("GPL"); |
| 37 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 38 | static unsigned int psmouse_max_proto = PSMOUSE_AUTO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 45 | MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | static unsigned int psmouse_resolution = 200; |
| 48 | module_param_named(resolution, psmouse_resolution, uint, 0644); |
| 49 | MODULE_PARM_DESC(resolution, "Resolution, in dpi."); |
| 50 | |
| 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."); |
| 54 | |
| 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."); |
| 58 | |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 59 | static unsigned int psmouse_resetafter = 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | module_param_named(resetafter, psmouse_resetafter, uint, 0644); |
| 61 | MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never)."); |
| 62 | |
Dmitry Torokhov | 8bd0ee9 | 2006-03-11 00:23:38 -0500 | [diff] [blame] | 63 | static unsigned int psmouse_resync_time; |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 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)."); |
| 66 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 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); |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 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); |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 82 | |
| 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, |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 88 | &psmouse_attr_resync_time.dattr.attr, |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 89 | NULL |
| 90 | }; |
| 91 | |
| 92 | static struct attribute_group psmouse_attribute_group = { |
| 93 | .attrs = psmouse_attributes, |
| 94 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | __obsolete_setup("psmouse_noext"); |
| 97 | __obsolete_setup("psmouse_resolution="); |
| 98 | __obsolete_setup("psmouse_smartscroll="); |
| 99 | __obsolete_setup("psmouse_resetafter="); |
| 100 | __obsolete_setup("psmouse_rate="); |
| 101 | |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 102 | /* |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 103 | * psmouse_mutex protects all operations changing state of mouse |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 104 | * (connecting, disconnecting, changing rate or resolution via |
| 105 | * sysfs). We could use a per-device semaphore but since there |
| 106 | * rarely more than one PS/2 mouse connected and since semaphore |
| 107 | * is taken in "slow" paths it is not worth it. |
| 108 | */ |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 109 | static DEFINE_MUTEX(psmouse_mutex); |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 110 | |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 111 | static struct workqueue_struct *kpsmoused_wq; |
| 112 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 113 | struct psmouse_protocol { |
| 114 | enum psmouse_type type; |
| 115 | char *name; |
| 116 | char *alias; |
| 117 | int maxproto; |
| 118 | int (*detect)(struct psmouse *, int); |
| 119 | int (*init)(struct psmouse *); |
| 120 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * psmouse_process_byte() analyzes the PS/2 data stream and reports |
| 124 | * relevant events to the input module once full packet has arrived. |
| 125 | */ |
| 126 | |
| 127 | static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_regs *regs) |
| 128 | { |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 129 | struct input_dev *dev = psmouse->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | unsigned char *packet = psmouse->packet; |
| 131 | |
| 132 | if (psmouse->pktcnt < psmouse->pktsize) |
| 133 | return PSMOUSE_GOOD_DATA; |
| 134 | |
| 135 | /* |
| 136 | * Full packet accumulated, process it |
| 137 | */ |
| 138 | |
| 139 | input_regs(dev, regs); |
| 140 | |
| 141 | /* |
| 142 | * Scroll wheel on IntelliMice, scroll buttons on NetMice |
| 143 | */ |
| 144 | |
| 145 | if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS) |
| 146 | input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]); |
| 147 | |
| 148 | /* |
| 149 | * Scroll wheel and buttons on IntelliMouse Explorer |
| 150 | */ |
| 151 | |
| 152 | if (psmouse->type == PSMOUSE_IMEX) { |
| 153 | input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7)); |
| 154 | input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1); |
| 155 | input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Extra buttons on Genius NewNet 3D |
| 160 | */ |
| 161 | |
| 162 | if (psmouse->type == PSMOUSE_GENPS) { |
| 163 | input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1); |
| 164 | input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1); |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Extra button on ThinkingMouse |
| 169 | */ |
| 170 | if (psmouse->type == PSMOUSE_THINKPS) { |
| 171 | input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1); |
| 172 | /* Without this bit of weirdness moving up gives wildly high Y changes. */ |
| 173 | packet[1] |= (packet[0] & 0x40) << 1; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Generic PS/2 Mouse |
| 178 | */ |
| 179 | |
| 180 | input_report_key(dev, BTN_LEFT, packet[0] & 1); |
| 181 | input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1); |
| 182 | input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1); |
| 183 | |
| 184 | input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0); |
| 185 | input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0); |
| 186 | |
| 187 | input_sync(dev); |
| 188 | |
| 189 | return PSMOUSE_FULL_PACKET; |
| 190 | } |
| 191 | |
| 192 | /* |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 193 | * __psmouse_set_state() sets new psmouse state and resets all flags. |
| 194 | */ |
| 195 | |
| 196 | static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state) |
| 197 | { |
| 198 | psmouse->state = new_state; |
| 199 | psmouse->pktcnt = psmouse->out_of_sync = 0; |
| 200 | psmouse->ps2dev.flags = 0; |
| 201 | psmouse->last = jiffies; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /* |
| 206 | * psmouse_set_state() sets new psmouse state and resets all flags and |
| 207 | * counters while holding serio lock so fighting with interrupt handler |
| 208 | * is not a concern. |
| 209 | */ |
| 210 | |
| 211 | static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state) |
| 212 | { |
| 213 | serio_pause_rx(psmouse->ps2dev.serio); |
| 214 | __psmouse_set_state(psmouse, new_state); |
| 215 | serio_continue_rx(psmouse->ps2dev.serio); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * psmouse_handle_byte() processes one byte of the input data stream |
| 220 | * by calling corresponding protocol handler. |
| 221 | */ |
| 222 | |
| 223 | static int psmouse_handle_byte(struct psmouse *psmouse, struct pt_regs *regs) |
| 224 | { |
| 225 | psmouse_ret_t rc = psmouse->protocol_handler(psmouse, regs); |
| 226 | |
| 227 | switch (rc) { |
| 228 | case PSMOUSE_BAD_DATA: |
| 229 | if (psmouse->state == PSMOUSE_ACTIVATED) { |
| 230 | printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n", |
| 231 | psmouse->name, psmouse->phys, psmouse->pktcnt); |
| 232 | if (++psmouse->out_of_sync == psmouse->resetafter) { |
| 233 | __psmouse_set_state(psmouse, PSMOUSE_IGNORE); |
| 234 | printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n"); |
| 235 | serio_reconnect(psmouse->ps2dev.serio); |
| 236 | return -1; |
| 237 | } |
| 238 | } |
| 239 | psmouse->pktcnt = 0; |
| 240 | break; |
| 241 | |
| 242 | case PSMOUSE_FULL_PACKET: |
| 243 | psmouse->pktcnt = 0; |
| 244 | if (psmouse->out_of_sync) { |
| 245 | psmouse->out_of_sync = 0; |
| 246 | printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n", |
| 247 | psmouse->name, psmouse->phys); |
| 248 | } |
| 249 | break; |
| 250 | |
| 251 | case PSMOUSE_GOOD_DATA: |
| 252 | break; |
| 253 | } |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * psmouse_interrupt() handles incoming characters, either passing them |
| 259 | * for normal processing or gathering them as command response. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | */ |
| 261 | |
| 262 | static irqreturn_t psmouse_interrupt(struct serio *serio, |
| 263 | unsigned char data, unsigned int flags, struct pt_regs *regs) |
| 264 | { |
| 265 | struct psmouse *psmouse = serio_get_drvdata(serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | if (psmouse->state == PSMOUSE_IGNORE) |
| 268 | goto out; |
| 269 | |
| 270 | if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) { |
| 271 | if (psmouse->state == PSMOUSE_ACTIVATED) |
| 272 | printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n", |
| 273 | flags & SERIO_TIMEOUT ? " timeout" : "", |
| 274 | flags & SERIO_PARITY ? " bad parity" : ""); |
| 275 | ps2_cmd_aborted(&psmouse->ps2dev); |
| 276 | goto out; |
| 277 | } |
| 278 | |
| 279 | if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK)) |
| 280 | if (ps2_handle_ack(&psmouse->ps2dev, data)) |
| 281 | goto out; |
| 282 | |
| 283 | if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD)) |
| 284 | if (ps2_handle_response(&psmouse->ps2dev, data)) |
| 285 | goto out; |
| 286 | |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 287 | if (psmouse->state <= PSMOUSE_RESYNCING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | goto out; |
| 289 | |
| 290 | if (psmouse->state == PSMOUSE_ACTIVATED && |
| 291 | psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) { |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 292 | printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | psmouse->name, psmouse->phys, psmouse->pktcnt); |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 294 | psmouse->badbyte = psmouse->packet[0]; |
| 295 | __psmouse_set_state(psmouse, PSMOUSE_RESYNCING); |
| 296 | queue_work(kpsmoused_wq, &psmouse->resync_work); |
| 297 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | psmouse->packet[psmouse->pktcnt++] = data; |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 301 | /* |
| 302 | * Check if this is a new device announcement (0xAA 0x00) |
| 303 | */ |
| 304 | if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) { |
Dmitry Torokhov | 89c9b48 | 2006-04-29 01:12:44 -0400 | [diff] [blame] | 305 | if (psmouse->pktcnt == 1) { |
| 306 | psmouse->last = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | goto out; |
Dmitry Torokhov | 89c9b48 | 2006-04-29 01:12:44 -0400 | [diff] [blame] | 308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 310 | if (psmouse->packet[1] == PSMOUSE_RET_ID) { |
| 311 | __psmouse_set_state(psmouse, PSMOUSE_IGNORE); |
| 312 | serio_reconnect(serio); |
| 313 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 315 | /* |
| 316 | * Not a new device, try processing first byte normally |
| 317 | */ |
| 318 | psmouse->pktcnt = 1; |
| 319 | if (psmouse_handle_byte(psmouse, regs)) |
| 320 | goto out; |
| 321 | |
| 322 | psmouse->packet[psmouse->pktcnt++] = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 325 | /* |
| 326 | * See if we need to force resync because mouse was idle for too long |
| 327 | */ |
| 328 | if (psmouse->state == PSMOUSE_ACTIVATED && |
| 329 | psmouse->pktcnt == 1 && psmouse->resync_time && |
| 330 | time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) { |
| 331 | psmouse->badbyte = psmouse->packet[0]; |
| 332 | __psmouse_set_state(psmouse, PSMOUSE_RESYNCING); |
| 333 | queue_work(kpsmoused_wq, &psmouse->resync_work); |
| 334 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 336 | |
| 337 | psmouse->last = jiffies; |
| 338 | psmouse_handle_byte(psmouse, regs); |
| 339 | |
| 340 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return IRQ_HANDLED; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | /* |
| 346 | * psmouse_sliced_command() sends an extended PS/2 command to the mouse |
| 347 | * using sliced syntax, understood by advanced devices, such as Logitech |
| 348 | * or Synaptics touchpads. The command is encoded as: |
| 349 | * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu |
| 350 | * is the command. |
| 351 | */ |
| 352 | int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command) |
| 353 | { |
| 354 | int i; |
| 355 | |
| 356 | if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) |
| 357 | return -1; |
| 358 | |
| 359 | for (i = 6; i >= 0; i -= 2) { |
| 360 | unsigned char d = (command >> i) & 3; |
| 361 | if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES)) |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | |
| 369 | /* |
| 370 | * psmouse_reset() resets the mouse into power-on state. |
| 371 | */ |
| 372 | int psmouse_reset(struct psmouse *psmouse) |
| 373 | { |
| 374 | unsigned char param[2]; |
| 375 | |
| 376 | if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT)) |
| 377 | return -1; |
| 378 | |
| 379 | if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID) |
| 380 | return -1; |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | /* |
| 387 | * Genius NetMouse magic init. |
| 388 | */ |
| 389 | static int genius_detect(struct psmouse *psmouse, int set_properties) |
| 390 | { |
| 391 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 392 | unsigned char param[4]; |
| 393 | |
| 394 | param[0] = 3; |
| 395 | ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES); |
| 396 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11); |
| 397 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11); |
| 398 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11); |
| 399 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO); |
| 400 | |
| 401 | if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55) |
| 402 | return -1; |
| 403 | |
| 404 | if (set_properties) { |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 405 | set_bit(BTN_EXTRA, psmouse->dev->keybit); |
| 406 | set_bit(BTN_SIDE, psmouse->dev->keybit); |
| 407 | set_bit(REL_WHEEL, psmouse->dev->relbit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
| 409 | psmouse->vendor = "Genius"; |
Dmitry Torokhov | a3f3f31 | 2006-01-29 21:50:46 -0500 | [diff] [blame] | 410 | psmouse->name = "Mouse"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | psmouse->pktsize = 4; |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * IntelliMouse magic init. |
| 419 | */ |
| 420 | static int intellimouse_detect(struct psmouse *psmouse, int set_properties) |
| 421 | { |
| 422 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 423 | unsigned char param[2]; |
| 424 | |
| 425 | param[0] = 200; |
| 426 | ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); |
| 427 | param[0] = 100; |
| 428 | ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); |
| 429 | param[0] = 80; |
| 430 | ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); |
| 431 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETID); |
| 432 | |
| 433 | if (param[0] != 3) |
| 434 | return -1; |
| 435 | |
| 436 | if (set_properties) { |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 437 | set_bit(BTN_MIDDLE, psmouse->dev->keybit); |
| 438 | set_bit(REL_WHEEL, psmouse->dev->relbit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | if (!psmouse->vendor) psmouse->vendor = "Generic"; |
| 441 | if (!psmouse->name) psmouse->name = "Wheel Mouse"; |
| 442 | psmouse->pktsize = 4; |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Try IntelliMouse/Explorer magic init. |
| 450 | */ |
| 451 | static int im_explorer_detect(struct psmouse *psmouse, int set_properties) |
| 452 | { |
| 453 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 454 | unsigned char param[2]; |
| 455 | |
| 456 | intellimouse_detect(psmouse, 0); |
| 457 | |
| 458 | param[0] = 200; |
| 459 | ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); |
| 460 | param[0] = 200; |
| 461 | ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); |
| 462 | param[0] = 80; |
| 463 | ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); |
| 464 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETID); |
| 465 | |
| 466 | if (param[0] != 4) |
| 467 | return -1; |
| 468 | |
| 469 | if (set_properties) { |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 470 | set_bit(BTN_MIDDLE, psmouse->dev->keybit); |
| 471 | set_bit(REL_WHEEL, psmouse->dev->relbit); |
| 472 | set_bit(BTN_SIDE, psmouse->dev->keybit); |
| 473 | set_bit(BTN_EXTRA, psmouse->dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | if (!psmouse->vendor) psmouse->vendor = "Generic"; |
| 476 | if (!psmouse->name) psmouse->name = "Explorer Mouse"; |
| 477 | psmouse->pktsize = 4; |
| 478 | } |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Kensington ThinkingMouse / ExpertMouse magic init. |
| 485 | */ |
| 486 | static int thinking_detect(struct psmouse *psmouse, int set_properties) |
| 487 | { |
| 488 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 489 | unsigned char param[2]; |
| 490 | unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20, 0 }; |
| 491 | int i; |
| 492 | |
| 493 | param[0] = 10; |
| 494 | ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE); |
| 495 | param[0] = 0; |
| 496 | ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES); |
| 497 | for (i = 0; seq[i]; i++) |
| 498 | ps2_command(ps2dev, seq + i, PSMOUSE_CMD_SETRATE); |
| 499 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETID); |
| 500 | |
| 501 | if (param[0] != 2) |
| 502 | return -1; |
| 503 | |
| 504 | if (set_properties) { |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 505 | set_bit(BTN_EXTRA, psmouse->dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | psmouse->vendor = "Kensington"; |
| 508 | psmouse->name = "ThinkingMouse"; |
| 509 | } |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Bare PS/2 protocol "detection". Always succeeds. |
| 516 | */ |
| 517 | static int ps2bare_detect(struct psmouse *psmouse, int set_properties) |
| 518 | { |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 519 | if (set_properties) { |
| 520 | if (!psmouse->vendor) psmouse->vendor = "Generic"; |
| 521 | if (!psmouse->name) psmouse->name = "Mouse"; |
| 522 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | /* |
| 529 | * psmouse_extensions() probes for any extensions to the basic PS/2 protocol |
| 530 | * the mouse may have. |
| 531 | */ |
| 532 | |
| 533 | static int psmouse_extensions(struct psmouse *psmouse, |
| 534 | unsigned int max_proto, int set_properties) |
| 535 | { |
| 536 | int synaptics_hardware = 0; |
| 537 | |
Dmitry Torokhov | a15d60f | 2005-05-29 02:30:32 -0500 | [diff] [blame] | 538 | /* |
| 539 | * We always check for lifebook because it does not disturb mouse |
| 540 | * (it only checks DMI information). |
| 541 | */ |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 542 | if (lifebook_detect(psmouse, set_properties) == 0) { |
Dmitry Torokhov | a15d60f | 2005-05-29 02:30:32 -0500 | [diff] [blame] | 543 | if (max_proto > PSMOUSE_IMEX) { |
| 544 | if (!set_properties || lifebook_init(psmouse) == 0) |
| 545 | return PSMOUSE_LIFEBOOK; |
| 546 | } |
| 547 | } |
Kenan Esau | 02d7f58 | 2005-05-29 02:30:22 -0500 | [diff] [blame] | 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | /* |
| 550 | * Try Kensington ThinkingMouse (we try first, because synaptics probe |
| 551 | * upsets the thinkingmouse). |
| 552 | */ |
| 553 | |
| 554 | if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0) |
| 555 | return PSMOUSE_THINKPS; |
| 556 | |
| 557 | /* |
| 558 | * Try Synaptics TouchPad |
| 559 | */ |
| 560 | if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) { |
| 561 | synaptics_hardware = 1; |
| 562 | |
| 563 | if (max_proto > PSMOUSE_IMEX) { |
| 564 | if (!set_properties || synaptics_init(psmouse) == 0) |
| 565 | return PSMOUSE_SYNAPTICS; |
| 566 | /* |
| 567 | * Some Synaptics touchpads can emulate extended protocols (like IMPS/2). |
| 568 | * Unfortunately Logitech/Genius probes confuse some firmware versions so |
| 569 | * we'll have to skip them. |
| 570 | */ |
| 571 | max_proto = PSMOUSE_IMEX; |
| 572 | } |
| 573 | /* |
| 574 | * Make sure that touchpad is in relative mode, gestures (taps) are enabled |
| 575 | */ |
| 576 | synaptics_reset(psmouse); |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * Try ALPS TouchPad |
| 581 | */ |
| 582 | if (max_proto > PSMOUSE_IMEX) { |
| 583 | ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS); |
| 584 | if (alps_detect(psmouse, set_properties) == 0) { |
| 585 | if (!set_properties || alps_init(psmouse) == 0) |
| 586 | return PSMOUSE_ALPS; |
| 587 | /* |
| 588 | * Init failed, try basic relative protocols |
| 589 | */ |
| 590 | max_proto = PSMOUSE_IMEX; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0) |
| 595 | return PSMOUSE_GENPS; |
| 596 | |
| 597 | if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0) |
| 598 | return PSMOUSE_PS2PP; |
| 599 | |
Dmitry Torokhov | ba44995 | 2005-12-21 00:51:31 -0500 | [diff] [blame] | 600 | if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0) |
| 601 | return PSMOUSE_TRACKPOINT; |
| 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | /* |
| 604 | * Reset to defaults in case the device got confused by extended |
Dmitry Torokhov | ba44995 | 2005-12-21 00:51:31 -0500 | [diff] [blame] | 605 | * protocol probes. Note that we do full reset becuase some mice |
| 606 | * put themselves to sleep when see PSMOUSE_RESET_DIS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | */ |
Dmitry Torokhov | ba44995 | 2005-12-21 00:51:31 -0500 | [diff] [blame] | 608 | psmouse_reset(psmouse); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
| 610 | if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0) |
| 611 | return PSMOUSE_IMEX; |
| 612 | |
| 613 | if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0) |
| 614 | return PSMOUSE_IMPS; |
| 615 | |
| 616 | /* |
| 617 | * Okay, all failed, we have a standard mouse here. The number of the buttons |
| 618 | * is still a question, though. We assume 3. |
| 619 | */ |
| 620 | ps2bare_detect(psmouse, set_properties); |
| 621 | |
| 622 | if (synaptics_hardware) { |
| 623 | /* |
| 624 | * We detected Synaptics hardware but it did not respond to IMPS/2 probes. |
| 625 | * We need to reset the touchpad because if there is a track point on the |
| 626 | * pass through port it could get disabled while probing for protocol |
| 627 | * extensions. |
| 628 | */ |
| 629 | psmouse_reset(psmouse); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | return PSMOUSE_PS2; |
| 633 | } |
| 634 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 635 | static struct psmouse_protocol psmouse_protocols[] = { |
| 636 | { |
| 637 | .type = PSMOUSE_PS2, |
| 638 | .name = "PS/2", |
| 639 | .alias = "bare", |
| 640 | .maxproto = 1, |
| 641 | .detect = ps2bare_detect, |
| 642 | }, |
| 643 | { |
| 644 | .type = PSMOUSE_PS2PP, |
| 645 | .name = "PS2++", |
| 646 | .alias = "logitech", |
| 647 | .detect = ps2pp_init, |
| 648 | }, |
| 649 | { |
| 650 | .type = PSMOUSE_THINKPS, |
| 651 | .name = "ThinkPS/2", |
| 652 | .alias = "thinkps", |
| 653 | .detect = thinking_detect, |
| 654 | }, |
| 655 | { |
| 656 | .type = PSMOUSE_GENPS, |
| 657 | .name = "GenPS/2", |
| 658 | .alias = "genius", |
| 659 | .detect = genius_detect, |
| 660 | }, |
| 661 | { |
| 662 | .type = PSMOUSE_IMPS, |
| 663 | .name = "ImPS/2", |
| 664 | .alias = "imps", |
| 665 | .maxproto = 1, |
| 666 | .detect = intellimouse_detect, |
| 667 | }, |
| 668 | { |
| 669 | .type = PSMOUSE_IMEX, |
| 670 | .name = "ImExPS/2", |
| 671 | .alias = "exps", |
| 672 | .maxproto = 1, |
| 673 | .detect = im_explorer_detect, |
| 674 | }, |
| 675 | { |
| 676 | .type = PSMOUSE_SYNAPTICS, |
| 677 | .name = "SynPS/2", |
| 678 | .alias = "synaptics", |
| 679 | .detect = synaptics_detect, |
| 680 | .init = synaptics_init, |
| 681 | }, |
| 682 | { |
| 683 | .type = PSMOUSE_ALPS, |
| 684 | .name = "AlpsPS/2", |
| 685 | .alias = "alps", |
| 686 | .detect = alps_detect, |
| 687 | .init = alps_init, |
| 688 | }, |
| 689 | { |
| 690 | .type = PSMOUSE_LIFEBOOK, |
| 691 | .name = "LBPS/2", |
| 692 | .alias = "lifebook", |
| 693 | .init = lifebook_init, |
| 694 | }, |
| 695 | { |
Stephen Evanchik | 541e316 | 2005-08-08 01:26:18 -0500 | [diff] [blame] | 696 | .type = PSMOUSE_TRACKPOINT, |
| 697 | .name = "TPPS/2", |
| 698 | .alias = "trackpoint", |
| 699 | .detect = trackpoint_detect, |
| 700 | }, |
| 701 | { |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 702 | .type = PSMOUSE_AUTO, |
| 703 | .name = "auto", |
| 704 | .alias = "any", |
| 705 | .maxproto = 1, |
| 706 | }, |
| 707 | }; |
| 708 | |
| 709 | static struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type) |
| 710 | { |
| 711 | int i; |
| 712 | |
| 713 | for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) |
| 714 | if (psmouse_protocols[i].type == type) |
| 715 | return &psmouse_protocols[i]; |
| 716 | |
| 717 | WARN_ON(1); |
| 718 | return &psmouse_protocols[0]; |
| 719 | } |
| 720 | |
| 721 | static struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len) |
| 722 | { |
| 723 | struct psmouse_protocol *p; |
| 724 | int i; |
| 725 | |
| 726 | for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) { |
| 727 | p = &psmouse_protocols[i]; |
| 728 | |
| 729 | if ((strlen(p->name) == len && !strncmp(p->name, name, len)) || |
| 730 | (strlen(p->alias) == len && !strncmp(p->alias, name, len))) |
| 731 | return &psmouse_protocols[i]; |
| 732 | } |
| 733 | |
| 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | /* |
| 739 | * psmouse_probe() probes for a PS/2 mouse. |
| 740 | */ |
| 741 | |
| 742 | static int psmouse_probe(struct psmouse *psmouse) |
| 743 | { |
| 744 | struct ps2dev *ps2dev = &psmouse->ps2dev; |
| 745 | unsigned char param[2]; |
| 746 | |
| 747 | /* |
| 748 | * First, we check if it's a mouse. It should send 0x00 or 0x03 |
| 749 | * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer. |
Vojtech Pavlik | 7741e93 | 2005-05-28 02:11:42 -0500 | [diff] [blame] | 750 | * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent |
| 751 | * ID queries, probably due to a firmware bug. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | */ |
| 753 | |
| 754 | param[0] = 0xa5; |
| 755 | if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID)) |
| 756 | return -1; |
| 757 | |
Vojtech Pavlik | 7741e93 | 2005-05-28 02:11:42 -0500 | [diff] [blame] | 758 | if (param[0] != 0x00 && param[0] != 0x03 && |
| 759 | param[0] != 0x04 && param[0] != 0xff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | return -1; |
| 761 | |
| 762 | /* |
| 763 | * Then we reset and disable the mouse so that it doesn't generate events. |
| 764 | */ |
| 765 | |
| 766 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS)) |
| 767 | printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys); |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * Here we set the mouse resolution. |
| 774 | */ |
| 775 | |
| 776 | void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution) |
| 777 | { |
| 778 | unsigned char params[] = { 0, 1, 2, 2, 3 }; |
| 779 | |
| 780 | if (resolution == 0 || resolution > 200) |
| 781 | resolution = 200; |
| 782 | |
| 783 | ps2_command(&psmouse->ps2dev, ¶ms[resolution / 50], PSMOUSE_CMD_SETRES); |
| 784 | psmouse->resolution = 25 << params[resolution / 50]; |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Here we set the mouse report rate. |
| 789 | */ |
| 790 | |
| 791 | static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate) |
| 792 | { |
| 793 | unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 }; |
| 794 | int i = 0; |
| 795 | |
| 796 | while (rates[i] > rate) i++; |
| 797 | ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE); |
| 798 | psmouse->rate = rates[i]; |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * psmouse_initialize() initializes the mouse to a sane state. |
| 803 | */ |
| 804 | |
| 805 | static void psmouse_initialize(struct psmouse *psmouse) |
| 806 | { |
| 807 | /* |
| 808 | * We set the mouse into streaming mode. |
| 809 | */ |
| 810 | |
| 811 | ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM); |
| 812 | |
| 813 | /* |
| 814 | * We set the mouse report rate, resolution and scaling. |
| 815 | */ |
| 816 | |
| 817 | if (psmouse_max_proto != PSMOUSE_PS2) { |
| 818 | psmouse->set_rate(psmouse, psmouse->rate); |
| 819 | psmouse->set_resolution(psmouse, psmouse->resolution); |
| 820 | ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | * psmouse_activate() enables the mouse so that we get motion reports from it. |
| 826 | */ |
| 827 | |
| 828 | static void psmouse_activate(struct psmouse *psmouse) |
| 829 | { |
| 830 | if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) |
| 831 | printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n", |
| 832 | psmouse->ps2dev.serio->phys); |
| 833 | |
| 834 | psmouse_set_state(psmouse, PSMOUSE_ACTIVATED); |
| 835 | } |
| 836 | |
| 837 | |
| 838 | /* |
| 839 | * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion |
| 840 | * reports from it unless we explicitely request it. |
| 841 | */ |
| 842 | |
| 843 | static void psmouse_deactivate(struct psmouse *psmouse) |
| 844 | { |
| 845 | if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE)) |
| 846 | printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n", |
| 847 | psmouse->ps2dev.serio->phys); |
| 848 | |
| 849 | psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); |
| 850 | } |
| 851 | |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 852 | /* |
| 853 | * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it. |
| 854 | */ |
| 855 | |
| 856 | static int psmouse_poll(struct psmouse *psmouse) |
| 857 | { |
| 858 | return ps2_command(&psmouse->ps2dev, psmouse->packet, |
| 859 | PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | /* |
| 864 | * psmouse_resync() attempts to re-validate current protocol. |
| 865 | */ |
| 866 | |
| 867 | static void psmouse_resync(void *p) |
| 868 | { |
| 869 | struct psmouse *psmouse = p, *parent = NULL; |
| 870 | struct serio *serio = psmouse->ps2dev.serio; |
| 871 | psmouse_ret_t rc = PSMOUSE_GOOD_DATA; |
| 872 | int failed = 0, enabled = 0; |
| 873 | int i; |
| 874 | |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 875 | mutex_lock(&psmouse_mutex); |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 876 | |
| 877 | if (psmouse->state != PSMOUSE_RESYNCING) |
| 878 | goto out; |
| 879 | |
| 880 | if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { |
| 881 | parent = serio_get_drvdata(serio->parent); |
| 882 | psmouse_deactivate(parent); |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * Some mice don't ACK commands sent while they are in the middle of |
| 887 | * transmitting motion packet. To avoid delay we use ps2_sendbyte() |
| 888 | * instead of ps2_command() which would wait for 200ms for an ACK |
| 889 | * that may never come. |
| 890 | * As an additional quirk ALPS touchpads may not only forget to ACK |
| 891 | * disable command but will stop reporting taps, so if we see that |
| 892 | * mouse at least once ACKs disable we will do full reconnect if ACK |
| 893 | * is missing. |
| 894 | */ |
| 895 | psmouse->num_resyncs++; |
| 896 | |
| 897 | if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) { |
| 898 | if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command) |
| 899 | failed = 1; |
| 900 | } else |
| 901 | psmouse->acks_disable_command = 1; |
| 902 | |
| 903 | /* |
| 904 | * Poll the mouse. If it was reset the packet will be shorter than |
| 905 | * psmouse->pktsize and ps2_command will fail. We do not expect and |
| 906 | * do not handle scenario when mouse "upgrades" its protocol while |
| 907 | * disconnected since it would require additional delay. If we ever |
| 908 | * see a mouse that does it we'll adjust the code. |
| 909 | */ |
| 910 | if (!failed) { |
| 911 | if (psmouse->poll(psmouse)) |
| 912 | failed = 1; |
| 913 | else { |
| 914 | psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); |
| 915 | for (i = 0; i < psmouse->pktsize; i++) { |
| 916 | psmouse->pktcnt++; |
| 917 | rc = psmouse->protocol_handler(psmouse, NULL); |
| 918 | if (rc != PSMOUSE_GOOD_DATA) |
| 919 | break; |
| 920 | } |
| 921 | if (rc != PSMOUSE_FULL_PACKET) |
| 922 | failed = 1; |
| 923 | psmouse_set_state(psmouse, PSMOUSE_RESYNCING); |
| 924 | } |
| 925 | } |
| 926 | /* |
| 927 | * Now try to enable mouse. We try to do that even if poll failed and also |
| 928 | * repeat our attempts 5 times, otherwise we may be left out with disabled |
| 929 | * mouse. |
| 930 | */ |
| 931 | for (i = 0; i < 5; i++) { |
| 932 | if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) { |
| 933 | enabled = 1; |
| 934 | break; |
| 935 | } |
| 936 | msleep(200); |
| 937 | } |
| 938 | |
| 939 | if (!enabled) { |
| 940 | printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n", |
| 941 | psmouse->ps2dev.serio->phys); |
| 942 | failed = 1; |
| 943 | } |
| 944 | |
| 945 | if (failed) { |
| 946 | psmouse_set_state(psmouse, PSMOUSE_IGNORE); |
| 947 | printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n"); |
| 948 | serio_reconnect(serio); |
| 949 | } else |
| 950 | psmouse_set_state(psmouse, PSMOUSE_ACTIVATED); |
| 951 | |
| 952 | if (parent) |
| 953 | psmouse_activate(parent); |
| 954 | out: |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 955 | mutex_unlock(&psmouse_mutex); |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 956 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
| 958 | /* |
| 959 | * psmouse_cleanup() resets the mouse into power-on state. |
| 960 | */ |
| 961 | |
| 962 | static void psmouse_cleanup(struct serio *serio) |
| 963 | { |
| 964 | struct psmouse *psmouse = serio_get_drvdata(serio); |
| 965 | |
| 966 | psmouse_reset(psmouse); |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * psmouse_disconnect() closes and frees. |
| 971 | */ |
| 972 | |
| 973 | static void psmouse_disconnect(struct serio *serio) |
| 974 | { |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 975 | struct psmouse *psmouse, *parent = NULL; |
| 976 | |
| 977 | psmouse = serio_get_drvdata(serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 979 | sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 981 | mutex_lock(&psmouse_mutex); |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); |
| 984 | |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 985 | /* make sure we don't have a resync in progress */ |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 986 | mutex_unlock(&psmouse_mutex); |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 987 | flush_workqueue(kpsmoused_wq); |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 988 | mutex_lock(&psmouse_mutex); |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { |
| 991 | parent = serio_get_drvdata(serio->parent); |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 992 | psmouse_deactivate(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | if (psmouse->disconnect) |
| 996 | psmouse->disconnect(psmouse); |
| 997 | |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 998 | if (parent && parent->pt_deactivate) |
| 999 | parent->pt_deactivate(parent); |
| 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | psmouse_set_state(psmouse, PSMOUSE_IGNORE); |
| 1002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | serio_close(serio); |
| 1004 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1005 | input_unregister_device(psmouse->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | kfree(psmouse); |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 1007 | |
| 1008 | if (parent) |
| 1009 | psmouse_activate(parent); |
| 1010 | |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 1011 | mutex_unlock(&psmouse_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1014 | static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_protocol *proto) |
| 1015 | { |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1016 | struct input_dev *input_dev = psmouse->dev; |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1017 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1018 | input_dev->private = psmouse; |
| 1019 | input_dev->cdev.dev = &psmouse->ps2dev.serio->dev; |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1020 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1021 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL); |
| 1022 | input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT); |
| 1023 | input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1024 | |
| 1025 | psmouse->set_rate = psmouse_set_rate; |
| 1026 | psmouse->set_resolution = psmouse_set_resolution; |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 1027 | psmouse->poll = psmouse_poll; |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1028 | psmouse->protocol_handler = psmouse_process_byte; |
| 1029 | psmouse->pktsize = 3; |
| 1030 | |
| 1031 | if (proto && (proto->detect || proto->init)) { |
| 1032 | if (proto->detect && proto->detect(psmouse, 1) < 0) |
| 1033 | return -1; |
| 1034 | |
| 1035 | if (proto->init && proto->init(psmouse) < 0) |
| 1036 | return -1; |
| 1037 | |
| 1038 | psmouse->type = proto->type; |
| 1039 | } |
| 1040 | else |
| 1041 | psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1); |
| 1042 | |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 1043 | /* |
| 1044 | * If mouse's packet size is 3 there is no point in polling the |
| 1045 | * device in hopes to detect protocol reset - we won't get less |
| 1046 | * than 3 bytes response anyhow. |
| 1047 | */ |
| 1048 | if (psmouse->pktsize == 3) |
| 1049 | psmouse->resync_time = 0; |
| 1050 | |
| 1051 | /* |
| 1052 | * Some smart KVMs fake response to POLL command returning just |
| 1053 | * 3 bytes and messing up our resync logic, so if initial poll |
| 1054 | * fails we won't try polling the device anymore. Hopefully |
| 1055 | * such KVM will maintain initially selected protocol. |
| 1056 | */ |
| 1057 | if (psmouse->resync_time && psmouse->poll(psmouse)) |
| 1058 | psmouse->resync_time = 0; |
| 1059 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1060 | sprintf(psmouse->devname, "%s %s %s", |
| 1061 | psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name); |
| 1062 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1063 | input_dev->name = psmouse->devname; |
| 1064 | input_dev->phys = psmouse->phys; |
| 1065 | input_dev->id.bustype = BUS_I8042; |
| 1066 | input_dev->id.vendor = 0x0002; |
| 1067 | input_dev->id.product = psmouse->type; |
| 1068 | input_dev->id.version = psmouse->model; |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | /* |
| 1074 | * psmouse_connect() is a callback from the serio module when |
| 1075 | * an unhandled serio port is found. |
| 1076 | */ |
| 1077 | static int psmouse_connect(struct serio *serio, struct serio_driver *drv) |
| 1078 | { |
| 1079 | struct psmouse *psmouse, *parent = NULL; |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1080 | struct input_dev *input_dev; |
| 1081 | int retval = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 1083 | mutex_lock(&psmouse_mutex); |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 1084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | /* |
| 1086 | * If this is a pass-through port deactivate parent so the device |
| 1087 | * connected to this port can be successfully identified |
| 1088 | */ |
| 1089 | if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { |
| 1090 | parent = serio_get_drvdata(serio->parent); |
| 1091 | psmouse_deactivate(parent); |
| 1092 | } |
| 1093 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1094 | psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL); |
| 1095 | input_dev = input_allocate_device(); |
| 1096 | if (!psmouse || !input_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | ps2_init(&psmouse->ps2dev, serio); |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 1100 | INIT_WORK(&psmouse->resync_work, psmouse_resync, psmouse); |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1101 | psmouse->dev = input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | sprintf(psmouse->phys, "%s/input0", serio->phys); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); |
| 1105 | |
| 1106 | serio_set_drvdata(serio, psmouse); |
| 1107 | |
| 1108 | retval = serio_open(serio, drv); |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1109 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
| 1112 | if (psmouse_probe(psmouse) < 0) { |
| 1113 | serio_close(serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | retval = -ENODEV; |
| 1115 | goto out; |
| 1116 | } |
| 1117 | |
| 1118 | psmouse->rate = psmouse_rate; |
| 1119 | psmouse->resolution = psmouse_resolution; |
| 1120 | psmouse->resetafter = psmouse_resetafter; |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 1121 | psmouse->resync_time = parent ? 0 : psmouse_resync_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | psmouse->smartscroll = psmouse_smartscroll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1124 | psmouse_switch_protocol(psmouse, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | psmouse_initialize(psmouse); |
| 1128 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1129 | input_register_device(psmouse->dev); |
| 1130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | if (parent && parent->pt_activate) |
| 1132 | parent->pt_activate(parent); |
| 1133 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1134 | sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
| 1136 | psmouse_activate(psmouse); |
| 1137 | |
| 1138 | retval = 0; |
| 1139 | |
| 1140 | out: |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1141 | if (retval) { |
| 1142 | serio_set_drvdata(serio, NULL); |
| 1143 | input_free_device(input_dev); |
| 1144 | kfree(psmouse); |
| 1145 | } |
| 1146 | |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 1147 | /* If this is a pass-through port the parent needs to be re-activated */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | if (parent) |
| 1149 | psmouse_activate(parent); |
| 1150 | |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 1151 | mutex_unlock(&psmouse_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | return retval; |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | static int psmouse_reconnect(struct serio *serio) |
| 1157 | { |
| 1158 | struct psmouse *psmouse = serio_get_drvdata(serio); |
| 1159 | struct psmouse *parent = NULL; |
| 1160 | struct serio_driver *drv = serio->drv; |
| 1161 | int rc = -1; |
| 1162 | |
| 1163 | if (!drv || !psmouse) { |
| 1164 | printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n"); |
| 1165 | return -1; |
| 1166 | } |
| 1167 | |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 1168 | mutex_lock(&psmouse_mutex); |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 1169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { |
| 1171 | parent = serio_get_drvdata(serio->parent); |
| 1172 | psmouse_deactivate(parent); |
| 1173 | } |
| 1174 | |
| 1175 | psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); |
| 1176 | |
| 1177 | if (psmouse->reconnect) { |
| 1178 | if (psmouse->reconnect(psmouse)) |
| 1179 | goto out; |
| 1180 | } else if (psmouse_probe(psmouse) < 0 || |
| 1181 | psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0)) |
| 1182 | goto out; |
| 1183 | |
| 1184 | /* ok, the device type (and capabilities) match the old one, |
| 1185 | * we can continue using it, complete intialization |
| 1186 | */ |
| 1187 | psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); |
| 1188 | |
| 1189 | psmouse_initialize(psmouse); |
| 1190 | |
| 1191 | if (parent && parent->pt_activate) |
| 1192 | parent->pt_activate(parent); |
| 1193 | |
| 1194 | psmouse_activate(psmouse); |
| 1195 | rc = 0; |
| 1196 | |
| 1197 | out: |
| 1198 | /* If this is a pass-through port the parent waits to be activated */ |
| 1199 | if (parent) |
| 1200 | psmouse_activate(parent); |
| 1201 | |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 1202 | mutex_unlock(&psmouse_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | return rc; |
| 1204 | } |
| 1205 | |
| 1206 | static struct serio_device_id psmouse_serio_ids[] = { |
| 1207 | { |
| 1208 | .type = SERIO_8042, |
| 1209 | .proto = SERIO_ANY, |
| 1210 | .id = SERIO_ANY, |
| 1211 | .extra = SERIO_ANY, |
| 1212 | }, |
| 1213 | { |
| 1214 | .type = SERIO_PS_PSTHRU, |
| 1215 | .proto = SERIO_ANY, |
| 1216 | .id = SERIO_ANY, |
| 1217 | .extra = SERIO_ANY, |
| 1218 | }, |
| 1219 | { 0 } |
| 1220 | }; |
| 1221 | |
| 1222 | MODULE_DEVICE_TABLE(serio, psmouse_serio_ids); |
| 1223 | |
| 1224 | static struct serio_driver psmouse_drv = { |
| 1225 | .driver = { |
| 1226 | .name = "psmouse", |
| 1227 | }, |
| 1228 | .description = DRIVER_DESC, |
| 1229 | .id_table = psmouse_serio_ids, |
| 1230 | .interrupt = psmouse_interrupt, |
| 1231 | .connect = psmouse_connect, |
| 1232 | .reconnect = psmouse_reconnect, |
| 1233 | .disconnect = psmouse_disconnect, |
| 1234 | .cleanup = psmouse_cleanup, |
| 1235 | }; |
| 1236 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1237 | ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr, |
| 1238 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | { |
| 1240 | struct serio *serio = to_serio_port(dev); |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1241 | struct psmouse_attribute *attr = to_psmouse_attr(devattr); |
| 1242 | struct psmouse *psmouse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | int retval; |
| 1244 | |
| 1245 | retval = serio_pin_driver(serio); |
| 1246 | if (retval) |
| 1247 | return retval; |
| 1248 | |
| 1249 | if (serio->drv != &psmouse_drv) { |
| 1250 | retval = -ENODEV; |
| 1251 | goto out; |
| 1252 | } |
| 1253 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1254 | psmouse = serio_get_drvdata(serio); |
| 1255 | |
| 1256 | retval = attr->show(psmouse, attr->data, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
| 1258 | out: |
| 1259 | serio_unpin_driver(serio); |
| 1260 | return retval; |
| 1261 | } |
| 1262 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1263 | ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr, |
| 1264 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | { |
| 1266 | struct serio *serio = to_serio_port(dev); |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1267 | struct psmouse_attribute *attr = to_psmouse_attr(devattr); |
| 1268 | struct psmouse *psmouse, *parent = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | int retval; |
| 1270 | |
| 1271 | retval = serio_pin_driver(serio); |
| 1272 | if (retval) |
| 1273 | return retval; |
| 1274 | |
| 1275 | if (serio->drv != &psmouse_drv) { |
| 1276 | retval = -ENODEV; |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 1277 | goto out_unpin; |
| 1278 | } |
| 1279 | |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 1280 | retval = mutex_lock_interruptible(&psmouse_mutex); |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 1281 | if (retval) |
| 1282 | goto out_unpin; |
| 1283 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1284 | psmouse = serio_get_drvdata(serio); |
| 1285 | |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 1286 | if (psmouse->state == PSMOUSE_IGNORE) { |
| 1287 | retval = -ENODEV; |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 1288 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { |
| 1292 | parent = serio_get_drvdata(serio->parent); |
| 1293 | psmouse_deactivate(parent); |
| 1294 | } |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | psmouse_deactivate(psmouse); |
| 1297 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1298 | retval = attr->set(psmouse, attr->data, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1300 | if (retval != -ENODEV) |
| 1301 | psmouse_activate(psmouse); |
| 1302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | if (parent) |
| 1304 | psmouse_activate(parent); |
| 1305 | |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 1306 | out_unlock: |
| 1307 | mutex_unlock(&psmouse_mutex); |
Dmitry Torokhov | 04df192 | 2005-06-01 02:39:44 -0500 | [diff] [blame] | 1308 | out_unpin: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | serio_unpin_driver(serio); |
| 1310 | return retval; |
| 1311 | } |
| 1312 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1313 | static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf) |
| 1314 | { |
| 1315 | unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset); |
| 1316 | |
| 1317 | return sprintf(buf, "%lu\n", *field); |
| 1318 | } |
| 1319 | |
| 1320 | static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count) |
| 1321 | { |
| 1322 | unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset); |
| 1323 | unsigned long value; |
| 1324 | char *rest; |
| 1325 | |
| 1326 | value = simple_strtoul(buf, &rest, 10); |
| 1327 | if (*rest) |
| 1328 | return -EINVAL; |
| 1329 | |
| 1330 | *field = value; |
| 1331 | |
| 1332 | return count; |
| 1333 | } |
| 1334 | |
| 1335 | static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf) |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1336 | { |
| 1337 | return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name); |
| 1338 | } |
| 1339 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1340 | static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count) |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1341 | { |
| 1342 | struct serio *serio = psmouse->ps2dev.serio; |
| 1343 | struct psmouse *parent = NULL; |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1344 | struct input_dev *new_dev; |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1345 | struct psmouse_protocol *proto; |
| 1346 | int retry = 0; |
| 1347 | |
| 1348 | if (!(proto = psmouse_protocol_by_name(buf, count))) |
| 1349 | return -EINVAL; |
| 1350 | |
| 1351 | if (psmouse->type == proto->type) |
| 1352 | return count; |
| 1353 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1354 | if (!(new_dev = input_allocate_device())) |
| 1355 | return -ENOMEM; |
| 1356 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1357 | while (serio->child) { |
| 1358 | if (++retry > 3) { |
| 1359 | printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n"); |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1360 | input_free_device(new_dev); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1361 | return -EIO; |
| 1362 | } |
| 1363 | |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 1364 | mutex_unlock(&psmouse_mutex); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1365 | serio_unpin_driver(serio); |
| 1366 | serio_unregister_child_port(serio); |
| 1367 | serio_pin_driver_uninterruptible(serio); |
Ingo Molnar | c14471d | 2006-02-19 00:22:11 -0500 | [diff] [blame] | 1368 | mutex_lock(&psmouse_mutex); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1369 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1370 | if (serio->drv != &psmouse_drv) { |
| 1371 | input_free_device(new_dev); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1372 | return -ENODEV; |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1373 | } |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1374 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1375 | if (psmouse->type == proto->type) { |
| 1376 | input_free_device(new_dev); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1377 | return count; /* switched by other thread */ |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1378 | } |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) { |
| 1382 | parent = serio_get_drvdata(serio->parent); |
| 1383 | if (parent->pt_deactivate) |
| 1384 | parent->pt_deactivate(parent); |
| 1385 | } |
| 1386 | |
| 1387 | if (psmouse->disconnect) |
| 1388 | psmouse->disconnect(psmouse); |
| 1389 | |
| 1390 | psmouse_set_state(psmouse, PSMOUSE_IGNORE); |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1391 | input_unregister_device(psmouse->dev); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1392 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1393 | psmouse->dev = new_dev; |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1394 | psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); |
| 1395 | |
| 1396 | if (psmouse_switch_protocol(psmouse, proto) < 0) { |
| 1397 | psmouse_reset(psmouse); |
| 1398 | /* default to PSMOUSE_PS2 */ |
| 1399 | psmouse_switch_protocol(psmouse, &psmouse_protocols[0]); |
| 1400 | } |
| 1401 | |
| 1402 | psmouse_initialize(psmouse); |
| 1403 | psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); |
| 1404 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 1405 | input_register_device(psmouse->dev); |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1406 | |
| 1407 | if (parent && parent->pt_activate) |
| 1408 | parent->pt_activate(parent); |
| 1409 | |
| 1410 | return count; |
| 1411 | } |
| 1412 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1413 | static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | { |
| 1415 | unsigned long value; |
| 1416 | char *rest; |
| 1417 | |
| 1418 | value = simple_strtoul(buf, &rest, 10); |
| 1419 | if (*rest) |
| 1420 | return -EINVAL; |
| 1421 | |
| 1422 | psmouse->set_rate(psmouse, value); |
| 1423 | return count; |
| 1424 | } |
| 1425 | |
Dmitry Torokhov | cfe9e88 | 2005-09-04 01:40:20 -0500 | [diff] [blame] | 1426 | static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | { |
| 1428 | unsigned long value; |
| 1429 | char *rest; |
| 1430 | |
| 1431 | value = simple_strtoul(buf, &rest, 10); |
| 1432 | if (*rest) |
| 1433 | return -EINVAL; |
| 1434 | |
| 1435 | psmouse->set_resolution(psmouse, value); |
| 1436 | return count; |
| 1437 | } |
| 1438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | |
| 1440 | static int psmouse_set_maxproto(const char *val, struct kernel_param *kp) |
| 1441 | { |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1442 | struct psmouse_protocol *proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
| 1444 | if (!val) |
| 1445 | return -EINVAL; |
| 1446 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1447 | proto = psmouse_protocol_by_name(val, strlen(val)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1449 | if (!proto || !proto->maxproto) |
| 1450 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1452 | *((unsigned int *)kp->arg) = proto->type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | |
Stephen Evanchik | 541e316 | 2005-08-08 01:26:18 -0500 | [diff] [blame] | 1454 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp) |
| 1458 | { |
Dmitry Torokhov | dbf4ccd | 2005-06-01 02:40:01 -0500 | [diff] [blame] | 1459 | int type = *((unsigned int *)kp->arg); |
| 1460 | |
| 1461 | return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | static int __init psmouse_init(void) |
| 1465 | { |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 1466 | kpsmoused_wq = create_singlethread_workqueue("kpsmoused"); |
| 1467 | if (!kpsmoused_wq) { |
| 1468 | printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n"); |
| 1469 | return -ENOMEM; |
| 1470 | } |
| 1471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | serio_register_driver(&psmouse_drv); |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 1473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | return 0; |
| 1475 | } |
| 1476 | |
| 1477 | static void __exit psmouse_exit(void) |
| 1478 | { |
| 1479 | serio_unregister_driver(&psmouse_drv); |
Dmitry Torokhov | f0d5c6f | 2006-01-14 00:27:37 -0500 | [diff] [blame] | 1480 | destroy_workqueue(kpsmoused_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | module_init(psmouse_init); |
| 1484 | module_exit(psmouse_exit); |