blob: 7665fd9ce559c49a153289bd64e19d6255134c11 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PS/2 mouse driver
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2003-2004 Dmitry Torokhov
6 */
7
8/*
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
12 */
13
14#include <linux/delay.h>
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/slab.h>
18#include <linux/interrupt.h>
19#include <linux/input.h>
20#include <linux/serio.h>
21#include <linux/init.h>
22#include <linux/libps2.h>
23#include "psmouse.h"
24#include "synaptics.h"
25#include "logips2pp.h"
26#include "alps.h"
Kenan Esau02d7f582005-05-29 02:30:22 -050027#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050028#include "trackpoint.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define DRIVER_DESC "PS/2 mouse driver"
31
32MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
33MODULE_DESCRIPTION(DRIVER_DESC);
34MODULE_LICENSE("GPL");
35
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050036static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
38static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
40#define param_set_proto_abbrev psmouse_set_maxproto
41#define param_get_proto_abbrev psmouse_get_maxproto
42module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050043MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static unsigned int psmouse_resolution = 200;
46module_param_named(resolution, psmouse_resolution, uint, 0644);
47MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
48
49static unsigned int psmouse_rate = 100;
50module_param_named(rate, psmouse_rate, uint, 0644);
51MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
52
53static unsigned int psmouse_smartscroll = 1;
54module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
55MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
56
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050057static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058module_param_named(resetafter, psmouse_resetafter, uint, 0644);
59MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
60
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050061static unsigned int psmouse_resync_time = 5;
62module_param_named(resync_time, psmouse_resync_time, uint, 0644);
63MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
64
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050065PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
66 NULL,
67 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
68PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
69 (void *) offsetof(struct psmouse, rate),
70 psmouse_show_int_attr, psmouse_attr_set_rate);
71PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
72 (void *) offsetof(struct psmouse, resolution),
73 psmouse_show_int_attr, psmouse_attr_set_resolution);
74PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
75 (void *) offsetof(struct psmouse, resetafter),
76 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050077PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
78 (void *) offsetof(struct psmouse, resync_time),
79 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050080
81static struct attribute *psmouse_attributes[] = {
82 &psmouse_attr_protocol.dattr.attr,
83 &psmouse_attr_rate.dattr.attr,
84 &psmouse_attr_resolution.dattr.attr,
85 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050086 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050087 NULL
88};
89
90static struct attribute_group psmouse_attribute_group = {
91 .attrs = psmouse_attributes,
92};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94__obsolete_setup("psmouse_noext");
95__obsolete_setup("psmouse_resolution=");
96__obsolete_setup("psmouse_smartscroll=");
97__obsolete_setup("psmouse_resetafter=");
98__obsolete_setup("psmouse_rate=");
99
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500100/*
101 * psmouse_sem protects all operations changing state of mouse
102 * (connecting, disconnecting, changing rate or resolution via
103 * sysfs). We could use a per-device semaphore but since there
104 * rarely more than one PS/2 mouse connected and since semaphore
105 * is taken in "slow" paths it is not worth it.
106 */
107static DECLARE_MUTEX(psmouse_sem);
108
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500109static struct workqueue_struct *kpsmoused_wq;
110
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500111struct psmouse_protocol {
112 enum psmouse_type type;
113 char *name;
114 char *alias;
115 int maxproto;
116 int (*detect)(struct psmouse *, int);
117 int (*init)(struct psmouse *);
118};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/*
121 * psmouse_process_byte() analyzes the PS/2 data stream and reports
122 * relevant events to the input module once full packet has arrived.
123 */
124
125static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
126{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500127 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned char *packet = psmouse->packet;
129
130 if (psmouse->pktcnt < psmouse->pktsize)
131 return PSMOUSE_GOOD_DATA;
132
133/*
134 * Full packet accumulated, process it
135 */
136
137 input_regs(dev, regs);
138
139/*
140 * Scroll wheel on IntelliMice, scroll buttons on NetMice
141 */
142
143 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
144 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
145
146/*
147 * Scroll wheel and buttons on IntelliMouse Explorer
148 */
149
150 if (psmouse->type == PSMOUSE_IMEX) {
151 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
152 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
153 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
154 }
155
156/*
157 * Extra buttons on Genius NewNet 3D
158 */
159
160 if (psmouse->type == PSMOUSE_GENPS) {
161 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
162 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
163 }
164
165/*
166 * Extra button on ThinkingMouse
167 */
168 if (psmouse->type == PSMOUSE_THINKPS) {
169 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
170 /* Without this bit of weirdness moving up gives wildly high Y changes. */
171 packet[1] |= (packet[0] & 0x40) << 1;
172 }
173
174/*
175 * Generic PS/2 Mouse
176 */
177
178 input_report_key(dev, BTN_LEFT, packet[0] & 1);
179 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
180 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
181
182 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
183 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
184
185 input_sync(dev);
186
187 return PSMOUSE_FULL_PACKET;
188}
189
190/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500191 * __psmouse_set_state() sets new psmouse state and resets all flags.
192 */
193
194static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
195{
196 psmouse->state = new_state;
197 psmouse->pktcnt = psmouse->out_of_sync = 0;
198 psmouse->ps2dev.flags = 0;
199 psmouse->last = jiffies;
200}
201
202
203/*
204 * psmouse_set_state() sets new psmouse state and resets all flags and
205 * counters while holding serio lock so fighting with interrupt handler
206 * is not a concern.
207 */
208
209static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
210{
211 serio_pause_rx(psmouse->ps2dev.serio);
212 __psmouse_set_state(psmouse, new_state);
213 serio_continue_rx(psmouse->ps2dev.serio);
214}
215
216/*
217 * psmouse_handle_byte() processes one byte of the input data stream
218 * by calling corresponding protocol handler.
219 */
220
221static int psmouse_handle_byte(struct psmouse *psmouse, struct pt_regs *regs)
222{
223 psmouse_ret_t rc = psmouse->protocol_handler(psmouse, regs);
224
225 switch (rc) {
226 case PSMOUSE_BAD_DATA:
227 if (psmouse->state == PSMOUSE_ACTIVATED) {
228 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
229 psmouse->name, psmouse->phys, psmouse->pktcnt);
230 if (++psmouse->out_of_sync == psmouse->resetafter) {
231 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
232 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
233 serio_reconnect(psmouse->ps2dev.serio);
234 return -1;
235 }
236 }
237 psmouse->pktcnt = 0;
238 break;
239
240 case PSMOUSE_FULL_PACKET:
241 psmouse->pktcnt = 0;
242 if (psmouse->out_of_sync) {
243 psmouse->out_of_sync = 0;
244 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
245 psmouse->name, psmouse->phys);
246 }
247 break;
248
249 case PSMOUSE_GOOD_DATA:
250 break;
251 }
252 return 0;
253}
254
255/*
256 * psmouse_interrupt() handles incoming characters, either passing them
257 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
259
260static irqreturn_t psmouse_interrupt(struct serio *serio,
261 unsigned char data, unsigned int flags, struct pt_regs *regs)
262{
263 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (psmouse->state == PSMOUSE_IGNORE)
266 goto out;
267
268 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
269 if (psmouse->state == PSMOUSE_ACTIVATED)
270 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
271 flags & SERIO_TIMEOUT ? " timeout" : "",
272 flags & SERIO_PARITY ? " bad parity" : "");
273 ps2_cmd_aborted(&psmouse->ps2dev);
274 goto out;
275 }
276
277 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
278 if (ps2_handle_ack(&psmouse->ps2dev, data))
279 goto out;
280
281 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
282 if (ps2_handle_response(&psmouse->ps2dev, data))
283 goto out;
284
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500285 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto out;
287
288 if (psmouse->state == PSMOUSE_ACTIVATED &&
289 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500290 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500292 psmouse->badbyte = psmouse->packet[0];
293 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
294 queue_work(kpsmoused_wq, &psmouse->resync_work);
295 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500299/*
300 * Check if this is a new device announcement (0xAA 0x00)
301 */
302 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (psmouse->pktcnt == 1)
304 goto out;
305
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500306 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
307 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
308 serio_reconnect(serio);
309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500311/*
312 * Not a new device, try processing first byte normally
313 */
314 psmouse->pktcnt = 1;
315 if (psmouse_handle_byte(psmouse, regs))
316 goto out;
317
318 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500321/*
322 * See if we need to force resync because mouse was idle for too long
323 */
324 if (psmouse->state == PSMOUSE_ACTIVATED &&
325 psmouse->pktcnt == 1 && psmouse->resync_time &&
326 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
327 psmouse->badbyte = psmouse->packet[0];
328 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
329 queue_work(kpsmoused_wq, &psmouse->resync_work);
330 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500332
333 psmouse->last = jiffies;
334 psmouse_handle_byte(psmouse, regs);
335
336 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return IRQ_HANDLED;
338}
339
340
341/*
342 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
343 * using sliced syntax, understood by advanced devices, such as Logitech
344 * or Synaptics touchpads. The command is encoded as:
345 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
346 * is the command.
347 */
348int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
349{
350 int i;
351
352 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
353 return -1;
354
355 for (i = 6; i >= 0; i -= 2) {
356 unsigned char d = (command >> i) & 3;
357 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
358 return -1;
359 }
360
361 return 0;
362}
363
364
365/*
366 * psmouse_reset() resets the mouse into power-on state.
367 */
368int psmouse_reset(struct psmouse *psmouse)
369{
370 unsigned char param[2];
371
372 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
373 return -1;
374
375 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
376 return -1;
377
378 return 0;
379}
380
381
382/*
383 * Genius NetMouse magic init.
384 */
385static int genius_detect(struct psmouse *psmouse, int set_properties)
386{
387 struct ps2dev *ps2dev = &psmouse->ps2dev;
388 unsigned char param[4];
389
390 param[0] = 3;
391 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
392 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
393 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
394 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
395 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
396
397 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
398 return -1;
399
400 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500401 set_bit(BTN_EXTRA, psmouse->dev->keybit);
402 set_bit(BTN_SIDE, psmouse->dev->keybit);
403 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 psmouse->vendor = "Genius";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 psmouse->pktsize = 4;
407 }
408
409 return 0;
410}
411
412/*
413 * IntelliMouse magic init.
414 */
415static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
416{
417 struct ps2dev *ps2dev = &psmouse->ps2dev;
418 unsigned char param[2];
419
420 param[0] = 200;
421 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
422 param[0] = 100;
423 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
424 param[0] = 80;
425 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
426 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
427
428 if (param[0] != 3)
429 return -1;
430
431 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500432 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
433 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 if (!psmouse->vendor) psmouse->vendor = "Generic";
436 if (!psmouse->name) psmouse->name = "Wheel Mouse";
437 psmouse->pktsize = 4;
438 }
439
440 return 0;
441}
442
443/*
444 * Try IntelliMouse/Explorer magic init.
445 */
446static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
447{
448 struct ps2dev *ps2dev = &psmouse->ps2dev;
449 unsigned char param[2];
450
451 intellimouse_detect(psmouse, 0);
452
453 param[0] = 200;
454 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
455 param[0] = 200;
456 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
457 param[0] = 80;
458 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
459 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
460
461 if (param[0] != 4)
462 return -1;
463
464 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500465 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
466 set_bit(REL_WHEEL, psmouse->dev->relbit);
467 set_bit(BTN_SIDE, psmouse->dev->keybit);
468 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if (!psmouse->vendor) psmouse->vendor = "Generic";
471 if (!psmouse->name) psmouse->name = "Explorer Mouse";
472 psmouse->pktsize = 4;
473 }
474
475 return 0;
476}
477
478/*
479 * Kensington ThinkingMouse / ExpertMouse magic init.
480 */
481static int thinking_detect(struct psmouse *psmouse, int set_properties)
482{
483 struct ps2dev *ps2dev = &psmouse->ps2dev;
484 unsigned char param[2];
485 unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20, 0 };
486 int i;
487
488 param[0] = 10;
489 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
490 param[0] = 0;
491 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
492 for (i = 0; seq[i]; i++)
493 ps2_command(ps2dev, seq + i, PSMOUSE_CMD_SETRATE);
494 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
495
496 if (param[0] != 2)
497 return -1;
498
499 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500500 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 psmouse->vendor = "Kensington";
503 psmouse->name = "ThinkingMouse";
504 }
505
506 return 0;
507}
508
509/*
510 * Bare PS/2 protocol "detection". Always succeeds.
511 */
512static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
513{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500514 if (set_properties) {
515 if (!psmouse->vendor) psmouse->vendor = "Generic";
516 if (!psmouse->name) psmouse->name = "Mouse";
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 return 0;
520}
521
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*
524 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
525 * the mouse may have.
526 */
527
528static int psmouse_extensions(struct psmouse *psmouse,
529 unsigned int max_proto, int set_properties)
530{
531 int synaptics_hardware = 0;
532
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500533/*
534 * We always check for lifebook because it does not disturb mouse
535 * (it only checks DMI information).
536 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500537 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500538 if (max_proto > PSMOUSE_IMEX) {
539 if (!set_properties || lifebook_init(psmouse) == 0)
540 return PSMOUSE_LIFEBOOK;
541 }
542 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/*
545 * Try Kensington ThinkingMouse (we try first, because synaptics probe
546 * upsets the thinkingmouse).
547 */
548
549 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
550 return PSMOUSE_THINKPS;
551
552/*
553 * Try Synaptics TouchPad
554 */
555 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
556 synaptics_hardware = 1;
557
558 if (max_proto > PSMOUSE_IMEX) {
559 if (!set_properties || synaptics_init(psmouse) == 0)
560 return PSMOUSE_SYNAPTICS;
561/*
562 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
563 * Unfortunately Logitech/Genius probes confuse some firmware versions so
564 * we'll have to skip them.
565 */
566 max_proto = PSMOUSE_IMEX;
567 }
568/*
569 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
570 */
571 synaptics_reset(psmouse);
572 }
573
574/*
575 * Try ALPS TouchPad
576 */
577 if (max_proto > PSMOUSE_IMEX) {
578 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
579 if (alps_detect(psmouse, set_properties) == 0) {
580 if (!set_properties || alps_init(psmouse) == 0)
581 return PSMOUSE_ALPS;
582/*
583 * Init failed, try basic relative protocols
584 */
585 max_proto = PSMOUSE_IMEX;
586 }
587 }
588
589 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
590 return PSMOUSE_GENPS;
591
592 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
593 return PSMOUSE_PS2PP;
594
Dmitry Torokhovba449952005-12-21 00:51:31 -0500595 if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
596 return PSMOUSE_TRACKPOINT;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/*
599 * Reset to defaults in case the device got confused by extended
Dmitry Torokhovba449952005-12-21 00:51:31 -0500600 * protocol probes. Note that we do full reset becuase some mice
601 * put themselves to sleep when see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Dmitry Torokhovba449952005-12-21 00:51:31 -0500603 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
606 return PSMOUSE_IMEX;
607
608 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
609 return PSMOUSE_IMPS;
610
611/*
612 * Okay, all failed, we have a standard mouse here. The number of the buttons
613 * is still a question, though. We assume 3.
614 */
615 ps2bare_detect(psmouse, set_properties);
616
617 if (synaptics_hardware) {
618/*
619 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
620 * We need to reset the touchpad because if there is a track point on the
621 * pass through port it could get disabled while probing for protocol
622 * extensions.
623 */
624 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
627 return PSMOUSE_PS2;
628}
629
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500630static struct psmouse_protocol psmouse_protocols[] = {
631 {
632 .type = PSMOUSE_PS2,
633 .name = "PS/2",
634 .alias = "bare",
635 .maxproto = 1,
636 .detect = ps2bare_detect,
637 },
638 {
639 .type = PSMOUSE_PS2PP,
640 .name = "PS2++",
641 .alias = "logitech",
642 .detect = ps2pp_init,
643 },
644 {
645 .type = PSMOUSE_THINKPS,
646 .name = "ThinkPS/2",
647 .alias = "thinkps",
648 .detect = thinking_detect,
649 },
650 {
651 .type = PSMOUSE_GENPS,
652 .name = "GenPS/2",
653 .alias = "genius",
654 .detect = genius_detect,
655 },
656 {
657 .type = PSMOUSE_IMPS,
658 .name = "ImPS/2",
659 .alias = "imps",
660 .maxproto = 1,
661 .detect = intellimouse_detect,
662 },
663 {
664 .type = PSMOUSE_IMEX,
665 .name = "ImExPS/2",
666 .alias = "exps",
667 .maxproto = 1,
668 .detect = im_explorer_detect,
669 },
670 {
671 .type = PSMOUSE_SYNAPTICS,
672 .name = "SynPS/2",
673 .alias = "synaptics",
674 .detect = synaptics_detect,
675 .init = synaptics_init,
676 },
677 {
678 .type = PSMOUSE_ALPS,
679 .name = "AlpsPS/2",
680 .alias = "alps",
681 .detect = alps_detect,
682 .init = alps_init,
683 },
684 {
685 .type = PSMOUSE_LIFEBOOK,
686 .name = "LBPS/2",
687 .alias = "lifebook",
688 .init = lifebook_init,
689 },
690 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500691 .type = PSMOUSE_TRACKPOINT,
692 .name = "TPPS/2",
693 .alias = "trackpoint",
694 .detect = trackpoint_detect,
695 },
696 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500697 .type = PSMOUSE_AUTO,
698 .name = "auto",
699 .alias = "any",
700 .maxproto = 1,
701 },
702};
703
704static struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
705{
706 int i;
707
708 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
709 if (psmouse_protocols[i].type == type)
710 return &psmouse_protocols[i];
711
712 WARN_ON(1);
713 return &psmouse_protocols[0];
714}
715
716static struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
717{
718 struct psmouse_protocol *p;
719 int i;
720
721 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
722 p = &psmouse_protocols[i];
723
724 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
725 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
726 return &psmouse_protocols[i];
727 }
728
729 return NULL;
730}
731
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*
734 * psmouse_probe() probes for a PS/2 mouse.
735 */
736
737static int psmouse_probe(struct psmouse *psmouse)
738{
739 struct ps2dev *ps2dev = &psmouse->ps2dev;
740 unsigned char param[2];
741
742/*
743 * First, we check if it's a mouse. It should send 0x00 or 0x03
744 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500745 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
746 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 */
748
749 param[0] = 0xa5;
750 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
751 return -1;
752
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500753 if (param[0] != 0x00 && param[0] != 0x03 &&
754 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -1;
756
757/*
758 * Then we reset and disable the mouse so that it doesn't generate events.
759 */
760
761 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
762 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
763
764 return 0;
765}
766
767/*
768 * Here we set the mouse resolution.
769 */
770
771void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
772{
773 unsigned char params[] = { 0, 1, 2, 2, 3 };
774
775 if (resolution == 0 || resolution > 200)
776 resolution = 200;
777
778 ps2_command(&psmouse->ps2dev, &params[resolution / 50], PSMOUSE_CMD_SETRES);
779 psmouse->resolution = 25 << params[resolution / 50];
780}
781
782/*
783 * Here we set the mouse report rate.
784 */
785
786static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
787{
788 unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
789 int i = 0;
790
791 while (rates[i] > rate) i++;
792 ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE);
793 psmouse->rate = rates[i];
794}
795
796/*
797 * psmouse_initialize() initializes the mouse to a sane state.
798 */
799
800static void psmouse_initialize(struct psmouse *psmouse)
801{
802/*
803 * We set the mouse into streaming mode.
804 */
805
806 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
807
808/*
809 * We set the mouse report rate, resolution and scaling.
810 */
811
812 if (psmouse_max_proto != PSMOUSE_PS2) {
813 psmouse->set_rate(psmouse, psmouse->rate);
814 psmouse->set_resolution(psmouse, psmouse->resolution);
815 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
816 }
817}
818
819/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * psmouse_activate() enables the mouse so that we get motion reports from it.
821 */
822
823static void psmouse_activate(struct psmouse *psmouse)
824{
825 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
826 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
827 psmouse->ps2dev.serio->phys);
828
829 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
830}
831
832
833/*
834 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
835 * reports from it unless we explicitely request it.
836 */
837
838static void psmouse_deactivate(struct psmouse *psmouse)
839{
840 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
841 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
842 psmouse->ps2dev.serio->phys);
843
844 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
845}
846
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500847/*
848 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
849 */
850
851static int psmouse_poll(struct psmouse *psmouse)
852{
853 return ps2_command(&psmouse->ps2dev, psmouse->packet,
854 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
855}
856
857
858/*
859 * psmouse_resync() attempts to re-validate current protocol.
860 */
861
862static void psmouse_resync(void *p)
863{
864 struct psmouse *psmouse = p, *parent = NULL;
865 struct serio *serio = psmouse->ps2dev.serio;
866 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
867 int failed = 0, enabled = 0;
868 int i;
869
870 down(&psmouse_sem);
871
872 if (psmouse->state != PSMOUSE_RESYNCING)
873 goto out;
874
875 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
876 parent = serio_get_drvdata(serio->parent);
877 psmouse_deactivate(parent);
878 }
879
880/*
881 * Some mice don't ACK commands sent while they are in the middle of
882 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
883 * instead of ps2_command() which would wait for 200ms for an ACK
884 * that may never come.
885 * As an additional quirk ALPS touchpads may not only forget to ACK
886 * disable command but will stop reporting taps, so if we see that
887 * mouse at least once ACKs disable we will do full reconnect if ACK
888 * is missing.
889 */
890 psmouse->num_resyncs++;
891
892 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
893 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
894 failed = 1;
895 } else
896 psmouse->acks_disable_command = 1;
897
898/*
899 * Poll the mouse. If it was reset the packet will be shorter than
900 * psmouse->pktsize and ps2_command will fail. We do not expect and
901 * do not handle scenario when mouse "upgrades" its protocol while
902 * disconnected since it would require additional delay. If we ever
903 * see a mouse that does it we'll adjust the code.
904 */
905 if (!failed) {
906 if (psmouse->poll(psmouse))
907 failed = 1;
908 else {
909 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
910 for (i = 0; i < psmouse->pktsize; i++) {
911 psmouse->pktcnt++;
912 rc = psmouse->protocol_handler(psmouse, NULL);
913 if (rc != PSMOUSE_GOOD_DATA)
914 break;
915 }
916 if (rc != PSMOUSE_FULL_PACKET)
917 failed = 1;
918 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
919 }
920 }
921/*
922 * Now try to enable mouse. We try to do that even if poll failed and also
923 * repeat our attempts 5 times, otherwise we may be left out with disabled
924 * mouse.
925 */
926 for (i = 0; i < 5; i++) {
927 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
928 enabled = 1;
929 break;
930 }
931 msleep(200);
932 }
933
934 if (!enabled) {
935 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
936 psmouse->ps2dev.serio->phys);
937 failed = 1;
938 }
939
940 if (failed) {
941 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
942 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
943 serio_reconnect(serio);
944 } else
945 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
946
947 if (parent)
948 psmouse_activate(parent);
949 out:
950 up(&psmouse_sem);
951}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953/*
954 * psmouse_cleanup() resets the mouse into power-on state.
955 */
956
957static void psmouse_cleanup(struct serio *serio)
958{
959 struct psmouse *psmouse = serio_get_drvdata(serio);
960
961 psmouse_reset(psmouse);
962}
963
964/*
965 * psmouse_disconnect() closes and frees.
966 */
967
968static void psmouse_disconnect(struct serio *serio)
969{
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500970 struct psmouse *psmouse, *parent = NULL;
971
972 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500974 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500976 down(&psmouse_sem);
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
979
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500980 /* make sure we don't have a resync in progress */
981 up(&psmouse_sem);
982 flush_workqueue(kpsmoused_wq);
983 down(&psmouse_sem);
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
986 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500987 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
990 if (psmouse->disconnect)
991 psmouse->disconnect(psmouse);
992
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500993 if (parent && parent->pt_deactivate)
994 parent->pt_deactivate(parent);
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 serio_close(serio);
999 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001000 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001002
1003 if (parent)
1004 psmouse_activate(parent);
1005
1006 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001009static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_protocol *proto)
1010{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001011 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001012
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001013 input_dev->private = psmouse;
1014 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001015
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001016 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1017 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1018 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001019
1020 psmouse->set_rate = psmouse_set_rate;
1021 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001022 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001023 psmouse->protocol_handler = psmouse_process_byte;
1024 psmouse->pktsize = 3;
1025
1026 if (proto && (proto->detect || proto->init)) {
1027 if (proto->detect && proto->detect(psmouse, 1) < 0)
1028 return -1;
1029
1030 if (proto->init && proto->init(psmouse) < 0)
1031 return -1;
1032
1033 psmouse->type = proto->type;
1034 }
1035 else
1036 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1037
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001038 /*
1039 * If mouse's packet size is 3 there is no point in polling the
1040 * device in hopes to detect protocol reset - we won't get less
1041 * than 3 bytes response anyhow.
1042 */
1043 if (psmouse->pktsize == 3)
1044 psmouse->resync_time = 0;
1045
1046 /*
1047 * Some smart KVMs fake response to POLL command returning just
1048 * 3 bytes and messing up our resync logic, so if initial poll
1049 * fails we won't try polling the device anymore. Hopefully
1050 * such KVM will maintain initially selected protocol.
1051 */
1052 if (psmouse->resync_time && psmouse->poll(psmouse))
1053 psmouse->resync_time = 0;
1054
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001055 sprintf(psmouse->devname, "%s %s %s",
1056 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1057
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001058 input_dev->name = psmouse->devname;
1059 input_dev->phys = psmouse->phys;
1060 input_dev->id.bustype = BUS_I8042;
1061 input_dev->id.vendor = 0x0002;
1062 input_dev->id.product = psmouse->type;
1063 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001064
1065 return 0;
1066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/*
1069 * psmouse_connect() is a callback from the serio module when
1070 * an unhandled serio port is found.
1071 */
1072static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1073{
1074 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001075 struct input_dev *input_dev;
1076 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001078 down(&psmouse_sem);
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /*
1081 * If this is a pass-through port deactivate parent so the device
1082 * connected to this port can be successfully identified
1083 */
1084 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1085 parent = serio_get_drvdata(serio->parent);
1086 psmouse_deactivate(parent);
1087 }
1088
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001089 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1090 input_dev = input_allocate_device();
1091 if (!psmouse || !input_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 ps2_init(&psmouse->ps2dev, serio);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001095 INIT_WORK(&psmouse->resync_work, psmouse_resync, psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001096 psmouse->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 sprintf(psmouse->phys, "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1100
1101 serio_set_drvdata(serio, psmouse);
1102
1103 retval = serio_open(serio, drv);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001104 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 if (psmouse_probe(psmouse) < 0) {
1108 serio_close(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 retval = -ENODEV;
1110 goto out;
1111 }
1112
1113 psmouse->rate = psmouse_rate;
1114 psmouse->resolution = psmouse_resolution;
1115 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001116 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001119 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 psmouse_initialize(psmouse);
1123
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001124 input_register_device(psmouse->dev);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (parent && parent->pt_activate)
1127 parent->pt_activate(parent);
1128
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001129 sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 psmouse_activate(psmouse);
1132
1133 retval = 0;
1134
1135out:
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001136 if (retval) {
1137 serio_set_drvdata(serio, NULL);
1138 input_free_device(input_dev);
1139 kfree(psmouse);
1140 }
1141
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001142 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (parent)
1144 psmouse_activate(parent);
1145
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001146 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return retval;
1148}
1149
1150
1151static int psmouse_reconnect(struct serio *serio)
1152{
1153 struct psmouse *psmouse = serio_get_drvdata(serio);
1154 struct psmouse *parent = NULL;
1155 struct serio_driver *drv = serio->drv;
1156 int rc = -1;
1157
1158 if (!drv || !psmouse) {
1159 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1160 return -1;
1161 }
1162
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001163 down(&psmouse_sem);
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1166 parent = serio_get_drvdata(serio->parent);
1167 psmouse_deactivate(parent);
1168 }
1169
1170 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1171
1172 if (psmouse->reconnect) {
1173 if (psmouse->reconnect(psmouse))
1174 goto out;
1175 } else if (psmouse_probe(psmouse) < 0 ||
1176 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1177 goto out;
1178
1179 /* ok, the device type (and capabilities) match the old one,
1180 * we can continue using it, complete intialization
1181 */
1182 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1183
1184 psmouse_initialize(psmouse);
1185
1186 if (parent && parent->pt_activate)
1187 parent->pt_activate(parent);
1188
1189 psmouse_activate(psmouse);
1190 rc = 0;
1191
1192out:
1193 /* If this is a pass-through port the parent waits to be activated */
1194 if (parent)
1195 psmouse_activate(parent);
1196
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001197 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return rc;
1199}
1200
1201static struct serio_device_id psmouse_serio_ids[] = {
1202 {
1203 .type = SERIO_8042,
1204 .proto = SERIO_ANY,
1205 .id = SERIO_ANY,
1206 .extra = SERIO_ANY,
1207 },
1208 {
1209 .type = SERIO_PS_PSTHRU,
1210 .proto = SERIO_ANY,
1211 .id = SERIO_ANY,
1212 .extra = SERIO_ANY,
1213 },
1214 { 0 }
1215};
1216
1217MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1218
1219static struct serio_driver psmouse_drv = {
1220 .driver = {
1221 .name = "psmouse",
1222 },
1223 .description = DRIVER_DESC,
1224 .id_table = psmouse_serio_ids,
1225 .interrupt = psmouse_interrupt,
1226 .connect = psmouse_connect,
1227 .reconnect = psmouse_reconnect,
1228 .disconnect = psmouse_disconnect,
1229 .cleanup = psmouse_cleanup,
1230};
1231
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001232ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1233 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001236 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1237 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 int retval;
1239
1240 retval = serio_pin_driver(serio);
1241 if (retval)
1242 return retval;
1243
1244 if (serio->drv != &psmouse_drv) {
1245 retval = -ENODEV;
1246 goto out;
1247 }
1248
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001249 psmouse = serio_get_drvdata(serio);
1250
1251 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253out:
1254 serio_unpin_driver(serio);
1255 return retval;
1256}
1257
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001258ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1259 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001262 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1263 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int retval;
1265
1266 retval = serio_pin_driver(serio);
1267 if (retval)
1268 return retval;
1269
1270 if (serio->drv != &psmouse_drv) {
1271 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001272 goto out_unpin;
1273 }
1274
1275 retval = down_interruptible(&psmouse_sem);
1276 if (retval)
1277 goto out_unpin;
1278
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001279 psmouse = serio_get_drvdata(serio);
1280
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001281 if (psmouse->state == PSMOUSE_IGNORE) {
1282 retval = -ENODEV;
1283 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285
1286 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1287 parent = serio_get_drvdata(serio->parent);
1288 psmouse_deactivate(parent);
1289 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 psmouse_deactivate(psmouse);
1292
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001293 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001295 if (retval != -ENODEV)
1296 psmouse_activate(psmouse);
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (parent)
1299 psmouse_activate(parent);
1300
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001301 out_up:
1302 up(&psmouse_sem);
1303 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 serio_unpin_driver(serio);
1305 return retval;
1306}
1307
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001308static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1309{
1310 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1311
1312 return sprintf(buf, "%lu\n", *field);
1313}
1314
1315static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1316{
1317 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1318 unsigned long value;
1319 char *rest;
1320
1321 value = simple_strtoul(buf, &rest, 10);
1322 if (*rest)
1323 return -EINVAL;
1324
1325 *field = value;
1326
1327 return count;
1328}
1329
1330static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001331{
1332 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1333}
1334
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001335static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001336{
1337 struct serio *serio = psmouse->ps2dev.serio;
1338 struct psmouse *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001339 struct input_dev *new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001340 struct psmouse_protocol *proto;
1341 int retry = 0;
1342
1343 if (!(proto = psmouse_protocol_by_name(buf, count)))
1344 return -EINVAL;
1345
1346 if (psmouse->type == proto->type)
1347 return count;
1348
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001349 if (!(new_dev = input_allocate_device()))
1350 return -ENOMEM;
1351
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001352 while (serio->child) {
1353 if (++retry > 3) {
1354 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001355 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001356 return -EIO;
1357 }
1358
1359 up(&psmouse_sem);
1360 serio_unpin_driver(serio);
1361 serio_unregister_child_port(serio);
1362 serio_pin_driver_uninterruptible(serio);
1363 down(&psmouse_sem);
1364
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001365 if (serio->drv != &psmouse_drv) {
1366 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001367 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001368 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001369
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001370 if (psmouse->type == proto->type) {
1371 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001372 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001373 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001374 }
1375
1376 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1377 parent = serio_get_drvdata(serio->parent);
1378 if (parent->pt_deactivate)
1379 parent->pt_deactivate(parent);
1380 }
1381
1382 if (psmouse->disconnect)
1383 psmouse->disconnect(psmouse);
1384
1385 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001386 input_unregister_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001387
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001388 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001389 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1390
1391 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1392 psmouse_reset(psmouse);
1393 /* default to PSMOUSE_PS2 */
1394 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1395 }
1396
1397 psmouse_initialize(psmouse);
1398 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1399
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001400 input_register_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001401
1402 if (parent && parent->pt_activate)
1403 parent->pt_activate(parent);
1404
1405 return count;
1406}
1407
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001408static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 unsigned long value;
1411 char *rest;
1412
1413 value = simple_strtoul(buf, &rest, 10);
1414 if (*rest)
1415 return -EINVAL;
1416
1417 psmouse->set_rate(psmouse, value);
1418 return count;
1419}
1420
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001421static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 unsigned long value;
1424 char *rest;
1425
1426 value = simple_strtoul(buf, &rest, 10);
1427 if (*rest)
1428 return -EINVAL;
1429
1430 psmouse->set_resolution(psmouse, value);
1431 return count;
1432}
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1436{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001437 struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 if (!val)
1440 return -EINVAL;
1441
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001442 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001444 if (!proto || !proto->maxproto)
1445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001447 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Stephen Evanchik541e3162005-08-08 01:26:18 -05001449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
1452static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1453{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001454 int type = *((unsigned int *)kp->arg);
1455
1456 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
1459static int __init psmouse_init(void)
1460{
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001461 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1462 if (!kpsmoused_wq) {
1463 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1464 return -ENOMEM;
1465 }
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 serio_register_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return 0;
1470}
1471
1472static void __exit psmouse_exit(void)
1473{
1474 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001475 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
1478module_init(psmouse_init);
1479module_exit(psmouse_exit);