blob: 32d70ed8f41dd7087f4fcc32f6522aec29ac3c3d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PS/2 mouse driver
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2003-2004 Dmitry Torokhov
6 */
7
8/*
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
12 */
13
14#include <linux/delay.h>
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/slab.h>
18#include <linux/interrupt.h>
19#include <linux/input.h>
20#include <linux/serio.h>
21#include <linux/init.h>
22#include <linux/libps2.h>
Ingo Molnarc14471d2006-02-19 00:22:11 -050023#include <linux/mutex.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "psmouse.h"
26#include "synaptics.h"
27#include "logips2pp.h"
28#include "alps.h"
Kenan Esau02d7f582005-05-29 02:30:22 -050029#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050030#include "trackpoint.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define DRIVER_DESC "PS/2 mouse driver"
33
34MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
35MODULE_DESCRIPTION(DRIVER_DESC);
36MODULE_LICENSE("GPL");
37
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050038static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
40static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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
44module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050045MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static unsigned int psmouse_resolution = 200;
48module_param_named(resolution, psmouse_resolution, uint, 0644);
49MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
50
51static unsigned int psmouse_rate = 100;
52module_param_named(rate, psmouse_rate, uint, 0644);
53MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
54
55static unsigned int psmouse_smartscroll = 1;
56module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
57MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
58
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050059static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060module_param_named(resetafter, psmouse_resetafter, uint, 0644);
61MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
62
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050063static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050064module_param_named(resync_time, psmouse_resync_time, uint, 0644);
65MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
66
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050067PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
68 NULL,
69 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
70PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
71 (void *) offsetof(struct psmouse, rate),
72 psmouse_show_int_attr, psmouse_attr_set_rate);
73PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
74 (void *) offsetof(struct psmouse, resolution),
75 psmouse_show_int_attr, psmouse_attr_set_resolution);
76PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
77 (void *) offsetof(struct psmouse, resetafter),
78 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050079PSMOUSE_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 Torokhovcfe9e882005-09-04 01:40:20 -050082
83static 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 Torokhovf0d5c6f42006-01-14 00:27:37 -050088 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050089 NULL
90};
91
92static struct attribute_group psmouse_attribute_group = {
93 .attrs = psmouse_attributes,
94};
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
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 Torokhov04df1922005-06-01 02:39:44 -0500102/*
Ingo Molnarc14471d2006-02-19 00:22:11 -0500103 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500104 * (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 Molnarc14471d2006-02-19 00:22:11 -0500109static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500110
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500111static struct workqueue_struct *kpsmoused_wq;
112
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500113struct 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 Torvalds1da177e2005-04-16 15:20:36 -0700121
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
127static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
128{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500129 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 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 Torokhovf0d5c6f42006-01-14 00:27:37 -0500193 * __psmouse_set_state() sets new psmouse state and resets all flags.
194 */
195
196static 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
211static 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
223static 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 Torvalds1da177e2005-04-16 15:20:36 -0700260 */
261
262static 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 Torvalds1da177e2005-04-16 15:20:36 -0700266
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 Torokhovf0d5c6f42006-01-14 00:27:37 -0500287 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 goto out;
289
290 if (psmouse->state == PSMOUSE_ACTIVATED &&
291 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500292 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500294 psmouse->badbyte = psmouse->packet[0];
295 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
296 queue_work(kpsmoused_wq, &psmouse->resync_work);
297 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500301/*
302 * Check if this is a new device announcement (0xAA 0x00)
303 */
304 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (psmouse->pktcnt == 1)
306 goto out;
307
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500308 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
309 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
310 serio_reconnect(serio);
311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500313/*
314 * Not a new device, try processing first byte normally
315 */
316 psmouse->pktcnt = 1;
317 if (psmouse_handle_byte(psmouse, regs))
318 goto out;
319
320 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500323/*
324 * See if we need to force resync because mouse was idle for too long
325 */
326 if (psmouse->state == PSMOUSE_ACTIVATED &&
327 psmouse->pktcnt == 1 && psmouse->resync_time &&
328 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
329 psmouse->badbyte = psmouse->packet[0];
330 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
331 queue_work(kpsmoused_wq, &psmouse->resync_work);
332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500334
335 psmouse->last = jiffies;
336 psmouse_handle_byte(psmouse, regs);
337
338 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return IRQ_HANDLED;
340}
341
342
343/*
344 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
345 * using sliced syntax, understood by advanced devices, such as Logitech
346 * or Synaptics touchpads. The command is encoded as:
347 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
348 * is the command.
349 */
350int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
351{
352 int i;
353
354 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
355 return -1;
356
357 for (i = 6; i >= 0; i -= 2) {
358 unsigned char d = (command >> i) & 3;
359 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
360 return -1;
361 }
362
363 return 0;
364}
365
366
367/*
368 * psmouse_reset() resets the mouse into power-on state.
369 */
370int psmouse_reset(struct psmouse *psmouse)
371{
372 unsigned char param[2];
373
374 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
375 return -1;
376
377 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
378 return -1;
379
380 return 0;
381}
382
383
384/*
385 * Genius NetMouse magic init.
386 */
387static int genius_detect(struct psmouse *psmouse, int set_properties)
388{
389 struct ps2dev *ps2dev = &psmouse->ps2dev;
390 unsigned char param[4];
391
392 param[0] = 3;
393 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
394 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
395 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
396 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
397 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
398
399 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
400 return -1;
401
402 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500403 set_bit(BTN_EXTRA, psmouse->dev->keybit);
404 set_bit(BTN_SIDE, psmouse->dev->keybit);
405 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500408 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 psmouse->pktsize = 4;
410 }
411
412 return 0;
413}
414
415/*
416 * IntelliMouse magic init.
417 */
418static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
419{
420 struct ps2dev *ps2dev = &psmouse->ps2dev;
421 unsigned char param[2];
422
423 param[0] = 200;
424 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
425 param[0] = 100;
426 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
427 param[0] = 80;
428 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
429 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
430
431 if (param[0] != 3)
432 return -1;
433
434 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500435 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
436 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (!psmouse->vendor) psmouse->vendor = "Generic";
439 if (!psmouse->name) psmouse->name = "Wheel Mouse";
440 psmouse->pktsize = 4;
441 }
442
443 return 0;
444}
445
446/*
447 * Try IntelliMouse/Explorer magic init.
448 */
449static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
450{
451 struct ps2dev *ps2dev = &psmouse->ps2dev;
452 unsigned char param[2];
453
454 intellimouse_detect(psmouse, 0);
455
456 param[0] = 200;
457 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
458 param[0] = 200;
459 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
460 param[0] = 80;
461 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
462 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
463
464 if (param[0] != 4)
465 return -1;
466
467 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500468 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
469 set_bit(REL_WHEEL, psmouse->dev->relbit);
470 set_bit(BTN_SIDE, psmouse->dev->keybit);
471 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (!psmouse->vendor) psmouse->vendor = "Generic";
474 if (!psmouse->name) psmouse->name = "Explorer Mouse";
475 psmouse->pktsize = 4;
476 }
477
478 return 0;
479}
480
481/*
482 * Kensington ThinkingMouse / ExpertMouse magic init.
483 */
484static int thinking_detect(struct psmouse *psmouse, int set_properties)
485{
486 struct ps2dev *ps2dev = &psmouse->ps2dev;
487 unsigned char param[2];
488 unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20, 0 };
489 int i;
490
491 param[0] = 10;
492 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
493 param[0] = 0;
494 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
495 for (i = 0; seq[i]; i++)
496 ps2_command(ps2dev, seq + i, PSMOUSE_CMD_SETRATE);
497 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
498
499 if (param[0] != 2)
500 return -1;
501
502 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500503 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 psmouse->vendor = "Kensington";
506 psmouse->name = "ThinkingMouse";
507 }
508
509 return 0;
510}
511
512/*
513 * Bare PS/2 protocol "detection". Always succeeds.
514 */
515static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
516{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500517 if (set_properties) {
518 if (!psmouse->vendor) psmouse->vendor = "Generic";
519 if (!psmouse->name) psmouse->name = "Mouse";
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 return 0;
523}
524
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/*
527 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
528 * the mouse may have.
529 */
530
531static int psmouse_extensions(struct psmouse *psmouse,
532 unsigned int max_proto, int set_properties)
533{
534 int synaptics_hardware = 0;
535
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500536/*
537 * We always check for lifebook because it does not disturb mouse
538 * (it only checks DMI information).
539 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500540 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500541 if (max_proto > PSMOUSE_IMEX) {
542 if (!set_properties || lifebook_init(psmouse) == 0)
543 return PSMOUSE_LIFEBOOK;
544 }
545 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/*
548 * Try Kensington ThinkingMouse (we try first, because synaptics probe
549 * upsets the thinkingmouse).
550 */
551
552 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
553 return PSMOUSE_THINKPS;
554
555/*
556 * Try Synaptics TouchPad
557 */
558 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
559 synaptics_hardware = 1;
560
561 if (max_proto > PSMOUSE_IMEX) {
562 if (!set_properties || synaptics_init(psmouse) == 0)
563 return PSMOUSE_SYNAPTICS;
564/*
565 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
566 * Unfortunately Logitech/Genius probes confuse some firmware versions so
567 * we'll have to skip them.
568 */
569 max_proto = PSMOUSE_IMEX;
570 }
571/*
572 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
573 */
574 synaptics_reset(psmouse);
575 }
576
577/*
578 * Try ALPS TouchPad
579 */
580 if (max_proto > PSMOUSE_IMEX) {
581 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
582 if (alps_detect(psmouse, set_properties) == 0) {
583 if (!set_properties || alps_init(psmouse) == 0)
584 return PSMOUSE_ALPS;
585/*
586 * Init failed, try basic relative protocols
587 */
588 max_proto = PSMOUSE_IMEX;
589 }
590 }
591
592 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
593 return PSMOUSE_GENPS;
594
595 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
596 return PSMOUSE_PS2PP;
597
Dmitry Torokhovba449952005-12-21 00:51:31 -0500598 if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
599 return PSMOUSE_TRACKPOINT;
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/*
602 * Reset to defaults in case the device got confused by extended
Dmitry Torokhovba449952005-12-21 00:51:31 -0500603 * protocol probes. Note that we do full reset becuase some mice
604 * put themselves to sleep when see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
Dmitry Torokhovba449952005-12-21 00:51:31 -0500606 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
609 return PSMOUSE_IMEX;
610
611 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
612 return PSMOUSE_IMPS;
613
614/*
615 * Okay, all failed, we have a standard mouse here. The number of the buttons
616 * is still a question, though. We assume 3.
617 */
618 ps2bare_detect(psmouse, set_properties);
619
620 if (synaptics_hardware) {
621/*
622 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
623 * We need to reset the touchpad because if there is a track point on the
624 * pass through port it could get disabled while probing for protocol
625 * extensions.
626 */
627 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 return PSMOUSE_PS2;
631}
632
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500633static struct psmouse_protocol psmouse_protocols[] = {
634 {
635 .type = PSMOUSE_PS2,
636 .name = "PS/2",
637 .alias = "bare",
638 .maxproto = 1,
639 .detect = ps2bare_detect,
640 },
641 {
642 .type = PSMOUSE_PS2PP,
643 .name = "PS2++",
644 .alias = "logitech",
645 .detect = ps2pp_init,
646 },
647 {
648 .type = PSMOUSE_THINKPS,
649 .name = "ThinkPS/2",
650 .alias = "thinkps",
651 .detect = thinking_detect,
652 },
653 {
654 .type = PSMOUSE_GENPS,
655 .name = "GenPS/2",
656 .alias = "genius",
657 .detect = genius_detect,
658 },
659 {
660 .type = PSMOUSE_IMPS,
661 .name = "ImPS/2",
662 .alias = "imps",
663 .maxproto = 1,
664 .detect = intellimouse_detect,
665 },
666 {
667 .type = PSMOUSE_IMEX,
668 .name = "ImExPS/2",
669 .alias = "exps",
670 .maxproto = 1,
671 .detect = im_explorer_detect,
672 },
673 {
674 .type = PSMOUSE_SYNAPTICS,
675 .name = "SynPS/2",
676 .alias = "synaptics",
677 .detect = synaptics_detect,
678 .init = synaptics_init,
679 },
680 {
681 .type = PSMOUSE_ALPS,
682 .name = "AlpsPS/2",
683 .alias = "alps",
684 .detect = alps_detect,
685 .init = alps_init,
686 },
687 {
688 .type = PSMOUSE_LIFEBOOK,
689 .name = "LBPS/2",
690 .alias = "lifebook",
691 .init = lifebook_init,
692 },
693 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500694 .type = PSMOUSE_TRACKPOINT,
695 .name = "TPPS/2",
696 .alias = "trackpoint",
697 .detect = trackpoint_detect,
698 },
699 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500700 .type = PSMOUSE_AUTO,
701 .name = "auto",
702 .alias = "any",
703 .maxproto = 1,
704 },
705};
706
707static struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
708{
709 int i;
710
711 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
712 if (psmouse_protocols[i].type == type)
713 return &psmouse_protocols[i];
714
715 WARN_ON(1);
716 return &psmouse_protocols[0];
717}
718
719static struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
720{
721 struct psmouse_protocol *p;
722 int i;
723
724 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
725 p = &psmouse_protocols[i];
726
727 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
728 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
729 return &psmouse_protocols[i];
730 }
731
732 return NULL;
733}
734
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/*
737 * psmouse_probe() probes for a PS/2 mouse.
738 */
739
740static int psmouse_probe(struct psmouse *psmouse)
741{
742 struct ps2dev *ps2dev = &psmouse->ps2dev;
743 unsigned char param[2];
744
745/*
746 * First, we check if it's a mouse. It should send 0x00 or 0x03
747 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500748 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
749 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
751
752 param[0] = 0xa5;
753 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
754 return -1;
755
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500756 if (param[0] != 0x00 && param[0] != 0x03 &&
757 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return -1;
759
760/*
761 * Then we reset and disable the mouse so that it doesn't generate events.
762 */
763
764 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
765 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
766
767 return 0;
768}
769
770/*
771 * Here we set the mouse resolution.
772 */
773
774void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
775{
776 unsigned char params[] = { 0, 1, 2, 2, 3 };
777
778 if (resolution == 0 || resolution > 200)
779 resolution = 200;
780
781 ps2_command(&psmouse->ps2dev, &params[resolution / 50], PSMOUSE_CMD_SETRES);
782 psmouse->resolution = 25 << params[resolution / 50];
783}
784
785/*
786 * Here we set the mouse report rate.
787 */
788
789static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
790{
791 unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
792 int i = 0;
793
794 while (rates[i] > rate) i++;
795 ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE);
796 psmouse->rate = rates[i];
797}
798
799/*
800 * psmouse_initialize() initializes the mouse to a sane state.
801 */
802
803static void psmouse_initialize(struct psmouse *psmouse)
804{
805/*
806 * We set the mouse into streaming mode.
807 */
808
809 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
810
811/*
812 * We set the mouse report rate, resolution and scaling.
813 */
814
815 if (psmouse_max_proto != PSMOUSE_PS2) {
816 psmouse->set_rate(psmouse, psmouse->rate);
817 psmouse->set_resolution(psmouse, psmouse->resolution);
818 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
819 }
820}
821
822/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 * psmouse_activate() enables the mouse so that we get motion reports from it.
824 */
825
826static void psmouse_activate(struct psmouse *psmouse)
827{
828 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
829 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
830 psmouse->ps2dev.serio->phys);
831
832 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
833}
834
835
836/*
837 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
838 * reports from it unless we explicitely request it.
839 */
840
841static void psmouse_deactivate(struct psmouse *psmouse)
842{
843 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
844 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
845 psmouse->ps2dev.serio->phys);
846
847 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
848}
849
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500850/*
851 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
852 */
853
854static int psmouse_poll(struct psmouse *psmouse)
855{
856 return ps2_command(&psmouse->ps2dev, psmouse->packet,
857 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
858}
859
860
861/*
862 * psmouse_resync() attempts to re-validate current protocol.
863 */
864
865static void psmouse_resync(void *p)
866{
867 struct psmouse *psmouse = p, *parent = NULL;
868 struct serio *serio = psmouse->ps2dev.serio;
869 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
870 int failed = 0, enabled = 0;
871 int i;
872
Ingo Molnarc14471d2006-02-19 00:22:11 -0500873 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500874
875 if (psmouse->state != PSMOUSE_RESYNCING)
876 goto out;
877
878 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
879 parent = serio_get_drvdata(serio->parent);
880 psmouse_deactivate(parent);
881 }
882
883/*
884 * Some mice don't ACK commands sent while they are in the middle of
885 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
886 * instead of ps2_command() which would wait for 200ms for an ACK
887 * that may never come.
888 * As an additional quirk ALPS touchpads may not only forget to ACK
889 * disable command but will stop reporting taps, so if we see that
890 * mouse at least once ACKs disable we will do full reconnect if ACK
891 * is missing.
892 */
893 psmouse->num_resyncs++;
894
895 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
896 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
897 failed = 1;
898 } else
899 psmouse->acks_disable_command = 1;
900
901/*
902 * Poll the mouse. If it was reset the packet will be shorter than
903 * psmouse->pktsize and ps2_command will fail. We do not expect and
904 * do not handle scenario when mouse "upgrades" its protocol while
905 * disconnected since it would require additional delay. If we ever
906 * see a mouse that does it we'll adjust the code.
907 */
908 if (!failed) {
909 if (psmouse->poll(psmouse))
910 failed = 1;
911 else {
912 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
913 for (i = 0; i < psmouse->pktsize; i++) {
914 psmouse->pktcnt++;
915 rc = psmouse->protocol_handler(psmouse, NULL);
916 if (rc != PSMOUSE_GOOD_DATA)
917 break;
918 }
919 if (rc != PSMOUSE_FULL_PACKET)
920 failed = 1;
921 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
922 }
923 }
924/*
925 * Now try to enable mouse. We try to do that even if poll failed and also
926 * repeat our attempts 5 times, otherwise we may be left out with disabled
927 * mouse.
928 */
929 for (i = 0; i < 5; i++) {
930 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
931 enabled = 1;
932 break;
933 }
934 msleep(200);
935 }
936
937 if (!enabled) {
938 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
939 psmouse->ps2dev.serio->phys);
940 failed = 1;
941 }
942
943 if (failed) {
944 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
945 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
946 serio_reconnect(serio);
947 } else
948 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
949
950 if (parent)
951 psmouse_activate(parent);
952 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -0500953 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500954}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956/*
957 * psmouse_cleanup() resets the mouse into power-on state.
958 */
959
960static void psmouse_cleanup(struct serio *serio)
961{
962 struct psmouse *psmouse = serio_get_drvdata(serio);
963
964 psmouse_reset(psmouse);
965}
966
967/*
968 * psmouse_disconnect() closes and frees.
969 */
970
971static void psmouse_disconnect(struct serio *serio)
972{
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500973 struct psmouse *psmouse, *parent = NULL;
974
975 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500977 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Ingo Molnarc14471d2006-02-19 00:22:11 -0500979 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
982
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500983 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500984 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500985 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -0500986 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
989 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500990 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
993 if (psmouse->disconnect)
994 psmouse->disconnect(psmouse);
995
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500996 if (parent && parent->pt_deactivate)
997 parent->pt_deactivate(parent);
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 serio_close(serio);
1002 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001003 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001005
1006 if (parent)
1007 psmouse_activate(parent);
1008
Ingo Molnarc14471d2006-02-19 00:22:11 -05001009 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001012static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_protocol *proto)
1013{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001014 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001015
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001016 input_dev->private = psmouse;
1017 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001018
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001019 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1020 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1021 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001022
1023 psmouse->set_rate = psmouse_set_rate;
1024 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001025 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001026 psmouse->protocol_handler = psmouse_process_byte;
1027 psmouse->pktsize = 3;
1028
1029 if (proto && (proto->detect || proto->init)) {
1030 if (proto->detect && proto->detect(psmouse, 1) < 0)
1031 return -1;
1032
1033 if (proto->init && proto->init(psmouse) < 0)
1034 return -1;
1035
1036 psmouse->type = proto->type;
1037 }
1038 else
1039 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1040
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001041 /*
1042 * If mouse's packet size is 3 there is no point in polling the
1043 * device in hopes to detect protocol reset - we won't get less
1044 * than 3 bytes response anyhow.
1045 */
1046 if (psmouse->pktsize == 3)
1047 psmouse->resync_time = 0;
1048
1049 /*
1050 * Some smart KVMs fake response to POLL command returning just
1051 * 3 bytes and messing up our resync logic, so if initial poll
1052 * fails we won't try polling the device anymore. Hopefully
1053 * such KVM will maintain initially selected protocol.
1054 */
1055 if (psmouse->resync_time && psmouse->poll(psmouse))
1056 psmouse->resync_time = 0;
1057
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001058 sprintf(psmouse->devname, "%s %s %s",
1059 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1060
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001061 input_dev->name = psmouse->devname;
1062 input_dev->phys = psmouse->phys;
1063 input_dev->id.bustype = BUS_I8042;
1064 input_dev->id.vendor = 0x0002;
1065 input_dev->id.product = psmouse->type;
1066 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001067
1068 return 0;
1069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*
1072 * psmouse_connect() is a callback from the serio module when
1073 * an unhandled serio port is found.
1074 */
1075static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1076{
1077 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001078 struct input_dev *input_dev;
1079 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Ingo Molnarc14471d2006-02-19 00:22:11 -05001081 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
1084 * If this is a pass-through port deactivate parent so the device
1085 * connected to this port can be successfully identified
1086 */
1087 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1088 parent = serio_get_drvdata(serio->parent);
1089 psmouse_deactivate(parent);
1090 }
1091
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001092 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1093 input_dev = input_allocate_device();
1094 if (!psmouse || !input_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 ps2_init(&psmouse->ps2dev, serio);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001098 INIT_WORK(&psmouse->resync_work, psmouse_resync, psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001099 psmouse->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 sprintf(psmouse->phys, "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1103
1104 serio_set_drvdata(serio, psmouse);
1105
1106 retval = serio_open(serio, drv);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001107 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (psmouse_probe(psmouse) < 0) {
1111 serio_close(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 retval = -ENODEV;
1113 goto out;
1114 }
1115
1116 psmouse->rate = psmouse_rate;
1117 psmouse->resolution = psmouse_resolution;
1118 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001119 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001122 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 psmouse_initialize(psmouse);
1126
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001127 input_register_device(psmouse->dev);
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (parent && parent->pt_activate)
1130 parent->pt_activate(parent);
1131
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001132 sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 psmouse_activate(psmouse);
1135
1136 retval = 0;
1137
1138out:
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001139 if (retval) {
1140 serio_set_drvdata(serio, NULL);
1141 input_free_device(input_dev);
1142 kfree(psmouse);
1143 }
1144
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001145 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (parent)
1147 psmouse_activate(parent);
1148
Ingo Molnarc14471d2006-02-19 00:22:11 -05001149 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return retval;
1151}
1152
1153
1154static int psmouse_reconnect(struct serio *serio)
1155{
1156 struct psmouse *psmouse = serio_get_drvdata(serio);
1157 struct psmouse *parent = NULL;
1158 struct serio_driver *drv = serio->drv;
1159 int rc = -1;
1160
1161 if (!drv || !psmouse) {
1162 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1163 return -1;
1164 }
1165
Ingo Molnarc14471d2006-02-19 00:22:11 -05001166 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1169 parent = serio_get_drvdata(serio->parent);
1170 psmouse_deactivate(parent);
1171 }
1172
1173 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1174
1175 if (psmouse->reconnect) {
1176 if (psmouse->reconnect(psmouse))
1177 goto out;
1178 } else if (psmouse_probe(psmouse) < 0 ||
1179 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1180 goto out;
1181
1182 /* ok, the device type (and capabilities) match the old one,
1183 * we can continue using it, complete intialization
1184 */
1185 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1186
1187 psmouse_initialize(psmouse);
1188
1189 if (parent && parent->pt_activate)
1190 parent->pt_activate(parent);
1191
1192 psmouse_activate(psmouse);
1193 rc = 0;
1194
1195out:
1196 /* If this is a pass-through port the parent waits to be activated */
1197 if (parent)
1198 psmouse_activate(parent);
1199
Ingo Molnarc14471d2006-02-19 00:22:11 -05001200 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return rc;
1202}
1203
1204static struct serio_device_id psmouse_serio_ids[] = {
1205 {
1206 .type = SERIO_8042,
1207 .proto = SERIO_ANY,
1208 .id = SERIO_ANY,
1209 .extra = SERIO_ANY,
1210 },
1211 {
1212 .type = SERIO_PS_PSTHRU,
1213 .proto = SERIO_ANY,
1214 .id = SERIO_ANY,
1215 .extra = SERIO_ANY,
1216 },
1217 { 0 }
1218};
1219
1220MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1221
1222static struct serio_driver psmouse_drv = {
1223 .driver = {
1224 .name = "psmouse",
1225 },
1226 .description = DRIVER_DESC,
1227 .id_table = psmouse_serio_ids,
1228 .interrupt = psmouse_interrupt,
1229 .connect = psmouse_connect,
1230 .reconnect = psmouse_reconnect,
1231 .disconnect = psmouse_disconnect,
1232 .cleanup = psmouse_cleanup,
1233};
1234
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001235ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1236 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001239 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1240 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int retval;
1242
1243 retval = serio_pin_driver(serio);
1244 if (retval)
1245 return retval;
1246
1247 if (serio->drv != &psmouse_drv) {
1248 retval = -ENODEV;
1249 goto out;
1250 }
1251
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001252 psmouse = serio_get_drvdata(serio);
1253
1254 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256out:
1257 serio_unpin_driver(serio);
1258 return retval;
1259}
1260
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001261ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1262 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001265 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1266 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int retval;
1268
1269 retval = serio_pin_driver(serio);
1270 if (retval)
1271 return retval;
1272
1273 if (serio->drv != &psmouse_drv) {
1274 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001275 goto out_unpin;
1276 }
1277
Ingo Molnarc14471d2006-02-19 00:22:11 -05001278 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001279 if (retval)
1280 goto out_unpin;
1281
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001282 psmouse = serio_get_drvdata(serio);
1283
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001284 if (psmouse->state == PSMOUSE_IGNORE) {
1285 retval = -ENODEV;
Ingo Molnarc14471d2006-02-19 00:22:11 -05001286 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288
1289 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1290 parent = serio_get_drvdata(serio->parent);
1291 psmouse_deactivate(parent);
1292 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 psmouse_deactivate(psmouse);
1295
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001296 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001298 if (retval != -ENODEV)
1299 psmouse_activate(psmouse);
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (parent)
1302 psmouse_activate(parent);
1303
Ingo Molnarc14471d2006-02-19 00:22:11 -05001304 out_unlock:
1305 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001306 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 serio_unpin_driver(serio);
1308 return retval;
1309}
1310
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001311static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1312{
1313 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1314
1315 return sprintf(buf, "%lu\n", *field);
1316}
1317
1318static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1319{
1320 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1321 unsigned long value;
1322 char *rest;
1323
1324 value = simple_strtoul(buf, &rest, 10);
1325 if (*rest)
1326 return -EINVAL;
1327
1328 *field = value;
1329
1330 return count;
1331}
1332
1333static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001334{
1335 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1336}
1337
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001338static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001339{
1340 struct serio *serio = psmouse->ps2dev.serio;
1341 struct psmouse *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001342 struct input_dev *new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001343 struct psmouse_protocol *proto;
1344 int retry = 0;
1345
1346 if (!(proto = psmouse_protocol_by_name(buf, count)))
1347 return -EINVAL;
1348
1349 if (psmouse->type == proto->type)
1350 return count;
1351
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001352 if (!(new_dev = input_allocate_device()))
1353 return -ENOMEM;
1354
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001355 while (serio->child) {
1356 if (++retry > 3) {
1357 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001358 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001359 return -EIO;
1360 }
1361
Ingo Molnarc14471d2006-02-19 00:22:11 -05001362 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001363 serio_unpin_driver(serio);
1364 serio_unregister_child_port(serio);
1365 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001366 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001367
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001368 if (serio->drv != &psmouse_drv) {
1369 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001370 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001371 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001372
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001373 if (psmouse->type == proto->type) {
1374 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001375 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001376 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001377 }
1378
1379 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1380 parent = serio_get_drvdata(serio->parent);
1381 if (parent->pt_deactivate)
1382 parent->pt_deactivate(parent);
1383 }
1384
1385 if (psmouse->disconnect)
1386 psmouse->disconnect(psmouse);
1387
1388 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001389 input_unregister_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001390
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001391 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001392 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1393
1394 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1395 psmouse_reset(psmouse);
1396 /* default to PSMOUSE_PS2 */
1397 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1398 }
1399
1400 psmouse_initialize(psmouse);
1401 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1402
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001403 input_register_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001404
1405 if (parent && parent->pt_activate)
1406 parent->pt_activate(parent);
1407
1408 return count;
1409}
1410
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001411static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 unsigned long value;
1414 char *rest;
1415
1416 value = simple_strtoul(buf, &rest, 10);
1417 if (*rest)
1418 return -EINVAL;
1419
1420 psmouse->set_rate(psmouse, value);
1421 return count;
1422}
1423
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001424static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 unsigned long value;
1427 char *rest;
1428
1429 value = simple_strtoul(buf, &rest, 10);
1430 if (*rest)
1431 return -EINVAL;
1432
1433 psmouse->set_resolution(psmouse, value);
1434 return count;
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1439{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001440 struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 if (!val)
1443 return -EINVAL;
1444
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001445 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001447 if (!proto || !proto->maxproto)
1448 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001450 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Stephen Evanchik541e3162005-08-08 01:26:18 -05001452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1456{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001457 int type = *((unsigned int *)kp->arg);
1458
1459 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
1462static int __init psmouse_init(void)
1463{
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001464 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1465 if (!kpsmoused_wq) {
1466 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1467 return -ENOMEM;
1468 }
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 serio_register_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return 0;
1473}
1474
1475static void __exit psmouse_exit(void)
1476{
1477 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001478 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481module_init(psmouse_init);
1482module_exit(psmouse_exit);