blob: 4be21847c67d232a799aab5fd71fadef6dca72be [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#define DRIVER_DESC "PS/2 mouse driver"
30
31MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
32MODULE_DESCRIPTION(DRIVER_DESC);
33MODULE_LICENSE("GPL");
34
35static unsigned int psmouse_max_proto = -1U;
36static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
37static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Kenan Esau02d7f582005-05-29 02:30:22 -050038static char *psmouse_proto_abbrev[] = { NULL, "bare", NULL, NULL, NULL, "imps", "exps", NULL, NULL, "lifebook" };
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);
Kenan Esau02d7f582005-05-29 02:30:22 -050043MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, lifebook, 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
57static unsigned int psmouse_resetafter;
58module_param_named(resetafter, psmouse_resetafter, uint, 0644);
59MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
60
61PSMOUSE_DEFINE_ATTR(rate);
62PSMOUSE_DEFINE_ATTR(resolution);
63PSMOUSE_DEFINE_ATTR(resetafter);
64
65__obsolete_setup("psmouse_noext");
66__obsolete_setup("psmouse_resolution=");
67__obsolete_setup("psmouse_smartscroll=");
68__obsolete_setup("psmouse_resetafter=");
69__obsolete_setup("psmouse_rate=");
70
Kenan Esau02d7f582005-05-29 02:30:22 -050071static char *psmouse_protocols[] = { "None", "PS/2", "PS2++", "ThinkPS/2", "GenPS/2", "ImPS/2", "ImExPS/2", "SynPS/2", "AlpsPS/2", "LBPS/2" };
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * psmouse_process_byte() analyzes the PS/2 data stream and reports
75 * relevant events to the input module once full packet has arrived.
76 */
77
78static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
79{
80 struct input_dev *dev = &psmouse->dev;
81 unsigned char *packet = psmouse->packet;
82
83 if (psmouse->pktcnt < psmouse->pktsize)
84 return PSMOUSE_GOOD_DATA;
85
86/*
87 * Full packet accumulated, process it
88 */
89
90 input_regs(dev, regs);
91
92/*
93 * Scroll wheel on IntelliMice, scroll buttons on NetMice
94 */
95
96 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
97 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
98
99/*
100 * Scroll wheel and buttons on IntelliMouse Explorer
101 */
102
103 if (psmouse->type == PSMOUSE_IMEX) {
104 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
105 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
106 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
107 }
108
109/*
110 * Extra buttons on Genius NewNet 3D
111 */
112
113 if (psmouse->type == PSMOUSE_GENPS) {
114 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
115 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
116 }
117
118/*
119 * Extra button on ThinkingMouse
120 */
121 if (psmouse->type == PSMOUSE_THINKPS) {
122 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
123 /* Without this bit of weirdness moving up gives wildly high Y changes. */
124 packet[1] |= (packet[0] & 0x40) << 1;
125 }
126
127/*
128 * Generic PS/2 Mouse
129 */
130
131 input_report_key(dev, BTN_LEFT, packet[0] & 1);
132 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
133 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
134
135 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
136 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
137
138 input_sync(dev);
139
140 return PSMOUSE_FULL_PACKET;
141}
142
143/*
144 * psmouse_interrupt() handles incoming characters, either gathering them into
145 * packets or passing them to the command routine as command output.
146 */
147
148static irqreturn_t psmouse_interrupt(struct serio *serio,
149 unsigned char data, unsigned int flags, struct pt_regs *regs)
150{
151 struct psmouse *psmouse = serio_get_drvdata(serio);
152 psmouse_ret_t rc;
153
154 if (psmouse->state == PSMOUSE_IGNORE)
155 goto out;
156
157 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
158 if (psmouse->state == PSMOUSE_ACTIVATED)
159 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
160 flags & SERIO_TIMEOUT ? " timeout" : "",
161 flags & SERIO_PARITY ? " bad parity" : "");
162 ps2_cmd_aborted(&psmouse->ps2dev);
163 goto out;
164 }
165
166 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
167 if (ps2_handle_ack(&psmouse->ps2dev, data))
168 goto out;
169
170 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
171 if (ps2_handle_response(&psmouse->ps2dev, data))
172 goto out;
173
174 if (psmouse->state == PSMOUSE_INITIALIZING)
175 goto out;
176
177 if (psmouse->state == PSMOUSE_ACTIVATED &&
178 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
179 printk(KERN_WARNING "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
180 psmouse->name, psmouse->phys, psmouse->pktcnt);
181 psmouse->pktcnt = 0;
182 }
183
184 psmouse->last = jiffies;
185 psmouse->packet[psmouse->pktcnt++] = data;
186
187 if (psmouse->packet[0] == PSMOUSE_RET_BAT) {
188 if (psmouse->pktcnt == 1)
189 goto out;
190
191 if (psmouse->pktcnt == 2) {
192 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
193 psmouse->state = PSMOUSE_IGNORE;
194 serio_reconnect(serio);
195 goto out;
196 }
197 if (psmouse->type == PSMOUSE_SYNAPTICS) {
198 /* neither 0xAA nor 0x00 are valid first bytes
199 * for a packet in absolute mode
200 */
201 psmouse->pktcnt = 0;
202 goto out;
203 }
204 }
205 }
206
207 rc = psmouse->protocol_handler(psmouse, regs);
208
209 switch (rc) {
210 case PSMOUSE_BAD_DATA:
211 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
212 psmouse->name, psmouse->phys, psmouse->pktcnt);
213 psmouse->pktcnt = 0;
214
215 if (++psmouse->out_of_sync == psmouse->resetafter) {
216 psmouse->state = PSMOUSE_IGNORE;
217 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
218 serio_reconnect(psmouse->ps2dev.serio);
219 }
220 break;
221
222 case PSMOUSE_FULL_PACKET:
223 psmouse->pktcnt = 0;
224 if (psmouse->out_of_sync) {
225 psmouse->out_of_sync = 0;
226 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
227 psmouse->name, psmouse->phys);
228 }
229 break;
230
231 case PSMOUSE_GOOD_DATA:
232 break;
233 }
234out:
235 return IRQ_HANDLED;
236}
237
238
239/*
240 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
241 * using sliced syntax, understood by advanced devices, such as Logitech
242 * or Synaptics touchpads. The command is encoded as:
243 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
244 * is the command.
245 */
246int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
247{
248 int i;
249
250 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
251 return -1;
252
253 for (i = 6; i >= 0; i -= 2) {
254 unsigned char d = (command >> i) & 3;
255 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
256 return -1;
257 }
258
259 return 0;
260}
261
262
263/*
264 * psmouse_reset() resets the mouse into power-on state.
265 */
266int psmouse_reset(struct psmouse *psmouse)
267{
268 unsigned char param[2];
269
270 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
271 return -1;
272
273 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
274 return -1;
275
276 return 0;
277}
278
279
280/*
281 * Genius NetMouse magic init.
282 */
283static int genius_detect(struct psmouse *psmouse, int set_properties)
284{
285 struct ps2dev *ps2dev = &psmouse->ps2dev;
286 unsigned char param[4];
287
288 param[0] = 3;
289 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
290 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
291 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
292 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
293 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
294
295 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
296 return -1;
297
298 if (set_properties) {
299 set_bit(BTN_EXTRA, psmouse->dev.keybit);
300 set_bit(BTN_SIDE, psmouse->dev.keybit);
301 set_bit(REL_WHEEL, psmouse->dev.relbit);
302
303 psmouse->vendor = "Genius";
304 psmouse->name = "Wheel Mouse";
305 psmouse->pktsize = 4;
306 }
307
308 return 0;
309}
310
311/*
312 * IntelliMouse magic init.
313 */
314static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
315{
316 struct ps2dev *ps2dev = &psmouse->ps2dev;
317 unsigned char param[2];
318
319 param[0] = 200;
320 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
321 param[0] = 100;
322 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
323 param[0] = 80;
324 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
325 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
326
327 if (param[0] != 3)
328 return -1;
329
330 if (set_properties) {
331 set_bit(REL_WHEEL, psmouse->dev.relbit);
332
333 if (!psmouse->vendor) psmouse->vendor = "Generic";
334 if (!psmouse->name) psmouse->name = "Wheel Mouse";
335 psmouse->pktsize = 4;
336 }
337
338 return 0;
339}
340
341/*
342 * Try IntelliMouse/Explorer magic init.
343 */
344static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
345{
346 struct ps2dev *ps2dev = &psmouse->ps2dev;
347 unsigned char param[2];
348
349 intellimouse_detect(psmouse, 0);
350
351 param[0] = 200;
352 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
353 param[0] = 200;
354 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
355 param[0] = 80;
356 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
357 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
358
359 if (param[0] != 4)
360 return -1;
361
362 if (set_properties) {
363 set_bit(REL_WHEEL, psmouse->dev.relbit);
364 set_bit(BTN_SIDE, psmouse->dev.keybit);
365 set_bit(BTN_EXTRA, psmouse->dev.keybit);
366
367 if (!psmouse->vendor) psmouse->vendor = "Generic";
368 if (!psmouse->name) psmouse->name = "Explorer Mouse";
369 psmouse->pktsize = 4;
370 }
371
372 return 0;
373}
374
375/*
376 * Kensington ThinkingMouse / ExpertMouse magic init.
377 */
378static int thinking_detect(struct psmouse *psmouse, int set_properties)
379{
380 struct ps2dev *ps2dev = &psmouse->ps2dev;
381 unsigned char param[2];
382 unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20, 0 };
383 int i;
384
385 param[0] = 10;
386 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
387 param[0] = 0;
388 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
389 for (i = 0; seq[i]; i++)
390 ps2_command(ps2dev, seq + i, PSMOUSE_CMD_SETRATE);
391 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
392
393 if (param[0] != 2)
394 return -1;
395
396 if (set_properties) {
397 set_bit(BTN_EXTRA, psmouse->dev.keybit);
398
399 psmouse->vendor = "Kensington";
400 psmouse->name = "ThinkingMouse";
401 }
402
403 return 0;
404}
405
406/*
407 * Bare PS/2 protocol "detection". Always succeeds.
408 */
409static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
410{
411 if (!psmouse->vendor) psmouse->vendor = "Generic";
412 if (!psmouse->name) psmouse->name = "Mouse";
413
414 return 0;
415}
416
417/*
418 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
419 * the mouse may have.
420 */
421
422static int psmouse_extensions(struct psmouse *psmouse,
423 unsigned int max_proto, int set_properties)
424{
425 int synaptics_hardware = 0;
426
Kenan Esau02d7f582005-05-29 02:30:22 -0500427 if (lifebook_detect(psmouse, max_proto, set_properties) == 0)
428 return PSMOUSE_LIFEBOOK;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/*
431 * Try Kensington ThinkingMouse (we try first, because synaptics probe
432 * upsets the thinkingmouse).
433 */
434
435 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
436 return PSMOUSE_THINKPS;
437
438/*
439 * Try Synaptics TouchPad
440 */
441 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
442 synaptics_hardware = 1;
443
444 if (max_proto > PSMOUSE_IMEX) {
445 if (!set_properties || synaptics_init(psmouse) == 0)
446 return PSMOUSE_SYNAPTICS;
447/*
448 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
449 * Unfortunately Logitech/Genius probes confuse some firmware versions so
450 * we'll have to skip them.
451 */
452 max_proto = PSMOUSE_IMEX;
453 }
454/*
455 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
456 */
457 synaptics_reset(psmouse);
458 }
459
460/*
461 * Try ALPS TouchPad
462 */
463 if (max_proto > PSMOUSE_IMEX) {
464 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
465 if (alps_detect(psmouse, set_properties) == 0) {
466 if (!set_properties || alps_init(psmouse) == 0)
467 return PSMOUSE_ALPS;
468/*
469 * Init failed, try basic relative protocols
470 */
471 max_proto = PSMOUSE_IMEX;
472 }
473 }
474
475 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
476 return PSMOUSE_GENPS;
477
478 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
479 return PSMOUSE_PS2PP;
480
481/*
482 * Reset to defaults in case the device got confused by extended
483 * protocol probes.
484 */
485 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
486
487 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
488 return PSMOUSE_IMEX;
489
490 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
491 return PSMOUSE_IMPS;
492
493/*
494 * Okay, all failed, we have a standard mouse here. The number of the buttons
495 * is still a question, though. We assume 3.
496 */
497 ps2bare_detect(psmouse, set_properties);
498
499 if (synaptics_hardware) {
500/*
501 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
502 * We need to reset the touchpad because if there is a track point on the
503 * pass through port it could get disabled while probing for protocol
504 * extensions.
505 */
506 psmouse_reset(psmouse);
507 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
508 }
509
510 return PSMOUSE_PS2;
511}
512
513/*
514 * psmouse_probe() probes for a PS/2 mouse.
515 */
516
517static int psmouse_probe(struct psmouse *psmouse)
518{
519 struct ps2dev *ps2dev = &psmouse->ps2dev;
520 unsigned char param[2];
521
522/*
523 * First, we check if it's a mouse. It should send 0x00 or 0x03
524 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500525 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
526 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
528
529 param[0] = 0xa5;
530 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
531 return -1;
532
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500533 if (param[0] != 0x00 && param[0] != 0x03 &&
534 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return -1;
536
537/*
538 * Then we reset and disable the mouse so that it doesn't generate events.
539 */
540
541 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
542 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
543
544 return 0;
545}
546
547/*
548 * Here we set the mouse resolution.
549 */
550
551void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
552{
553 unsigned char params[] = { 0, 1, 2, 2, 3 };
554
555 if (resolution == 0 || resolution > 200)
556 resolution = 200;
557
558 ps2_command(&psmouse->ps2dev, &params[resolution / 50], PSMOUSE_CMD_SETRES);
559 psmouse->resolution = 25 << params[resolution / 50];
560}
561
562/*
563 * Here we set the mouse report rate.
564 */
565
566static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
567{
568 unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
569 int i = 0;
570
571 while (rates[i] > rate) i++;
572 ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE);
573 psmouse->rate = rates[i];
574}
575
576/*
577 * psmouse_initialize() initializes the mouse to a sane state.
578 */
579
580static void psmouse_initialize(struct psmouse *psmouse)
581{
582/*
583 * We set the mouse into streaming mode.
584 */
585
586 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
587
588/*
589 * We set the mouse report rate, resolution and scaling.
590 */
591
592 if (psmouse_max_proto != PSMOUSE_PS2) {
593 psmouse->set_rate(psmouse, psmouse->rate);
594 psmouse->set_resolution(psmouse, psmouse->resolution);
595 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
596 }
597}
598
599/*
600 * psmouse_set_state() sets new psmouse state and resets all flags and
601 * counters while holding serio lock so fighting with interrupt handler
602 * is not a concern.
603 */
604
605static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
606{
607 serio_pause_rx(psmouse->ps2dev.serio);
608 psmouse->state = new_state;
609 psmouse->pktcnt = psmouse->out_of_sync = 0;
610 psmouse->ps2dev.flags = 0;
611 serio_continue_rx(psmouse->ps2dev.serio);
612}
613
614/*
615 * psmouse_activate() enables the mouse so that we get motion reports from it.
616 */
617
618static void psmouse_activate(struct psmouse *psmouse)
619{
620 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
621 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
622 psmouse->ps2dev.serio->phys);
623
624 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
625}
626
627
628/*
629 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
630 * reports from it unless we explicitely request it.
631 */
632
633static void psmouse_deactivate(struct psmouse *psmouse)
634{
635 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
636 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
637 psmouse->ps2dev.serio->phys);
638
639 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
640}
641
642
643/*
644 * psmouse_cleanup() resets the mouse into power-on state.
645 */
646
647static void psmouse_cleanup(struct serio *serio)
648{
649 struct psmouse *psmouse = serio_get_drvdata(serio);
650
651 psmouse_reset(psmouse);
652}
653
654/*
655 * psmouse_disconnect() closes and frees.
656 */
657
658static void psmouse_disconnect(struct serio *serio)
659{
660 struct psmouse *psmouse, *parent;
661
662 device_remove_file(&serio->dev, &psmouse_attr_rate);
663 device_remove_file(&serio->dev, &psmouse_attr_resolution);
664 device_remove_file(&serio->dev, &psmouse_attr_resetafter);
665
666 psmouse = serio_get_drvdata(serio);
667 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
668
669 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
670 parent = serio_get_drvdata(serio->parent);
671 if (parent->pt_deactivate)
672 parent->pt_deactivate(parent);
673 }
674
675 if (psmouse->disconnect)
676 psmouse->disconnect(psmouse);
677
678 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
679
680 input_unregister_device(&psmouse->dev);
681 serio_close(serio);
682 serio_set_drvdata(serio, NULL);
683 kfree(psmouse);
684}
685
686/*
687 * psmouse_connect() is a callback from the serio module when
688 * an unhandled serio port is found.
689 */
690static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
691{
692 struct psmouse *psmouse, *parent = NULL;
693 int retval;
694
695 /*
696 * If this is a pass-through port deactivate parent so the device
697 * connected to this port can be successfully identified
698 */
699 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
700 parent = serio_get_drvdata(serio->parent);
701 psmouse_deactivate(parent);
702 }
703
704 if (!(psmouse = kmalloc(sizeof(struct psmouse), GFP_KERNEL))) {
705 retval = -ENOMEM;
706 goto out;
707 }
708
709 memset(psmouse, 0, sizeof(struct psmouse));
710
711 ps2_init(&psmouse->ps2dev, serio);
712 sprintf(psmouse->phys, "%s/input0", serio->phys);
713 psmouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
714 psmouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
715 psmouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
716 psmouse->dev.private = psmouse;
717 psmouse->dev.dev = &serio->dev;
718 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
719
720 serio_set_drvdata(serio, psmouse);
721
722 retval = serio_open(serio, drv);
723 if (retval) {
724 serio_set_drvdata(serio, NULL);
725 kfree(psmouse);
726 goto out;
727 }
728
729 if (psmouse_probe(psmouse) < 0) {
730 serio_close(serio);
731 serio_set_drvdata(serio, NULL);
732 kfree(psmouse);
733 retval = -ENODEV;
734 goto out;
735 }
736
737 psmouse->rate = psmouse_rate;
738 psmouse->resolution = psmouse_resolution;
739 psmouse->resetafter = psmouse_resetafter;
740 psmouse->smartscroll = psmouse_smartscroll;
741 psmouse->set_rate = psmouse_set_rate;
742 psmouse->set_resolution = psmouse_set_resolution;
743 psmouse->protocol_handler = psmouse_process_byte;
744 psmouse->pktsize = 3;
745
746 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
747
748 sprintf(psmouse->devname, "%s %s %s",
749 psmouse_protocols[psmouse->type], psmouse->vendor, psmouse->name);
750
751 psmouse->dev.name = psmouse->devname;
752 psmouse->dev.phys = psmouse->phys;
753 psmouse->dev.id.bustype = BUS_I8042;
754 psmouse->dev.id.vendor = 0x0002;
755 psmouse->dev.id.product = psmouse->type;
756 psmouse->dev.id.version = psmouse->model;
757
758 input_register_device(&psmouse->dev);
759
760 printk(KERN_INFO "input: %s on %s\n", psmouse->devname, serio->phys);
761
762 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
763
764 psmouse_initialize(psmouse);
765
766 if (parent && parent->pt_activate)
767 parent->pt_activate(parent);
768
769 device_create_file(&serio->dev, &psmouse_attr_rate);
770 device_create_file(&serio->dev, &psmouse_attr_resolution);
771 device_create_file(&serio->dev, &psmouse_attr_resetafter);
772
773 psmouse_activate(psmouse);
774
775 retval = 0;
776
777out:
778 /* If this is a pass-through port the parent awaits to be activated */
779 if (parent)
780 psmouse_activate(parent);
781
782 return retval;
783}
784
785
786static int psmouse_reconnect(struct serio *serio)
787{
788 struct psmouse *psmouse = serio_get_drvdata(serio);
789 struct psmouse *parent = NULL;
790 struct serio_driver *drv = serio->drv;
791 int rc = -1;
792
793 if (!drv || !psmouse) {
794 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
795 return -1;
796 }
797
798 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
799 parent = serio_get_drvdata(serio->parent);
800 psmouse_deactivate(parent);
801 }
802
803 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
804
805 if (psmouse->reconnect) {
806 if (psmouse->reconnect(psmouse))
807 goto out;
808 } else if (psmouse_probe(psmouse) < 0 ||
809 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
810 goto out;
811
812 /* ok, the device type (and capabilities) match the old one,
813 * we can continue using it, complete intialization
814 */
815 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
816
817 psmouse_initialize(psmouse);
818
819 if (parent && parent->pt_activate)
820 parent->pt_activate(parent);
821
822 psmouse_activate(psmouse);
823 rc = 0;
824
825out:
826 /* If this is a pass-through port the parent waits to be activated */
827 if (parent)
828 psmouse_activate(parent);
829
830 return rc;
831}
832
833static struct serio_device_id psmouse_serio_ids[] = {
834 {
835 .type = SERIO_8042,
836 .proto = SERIO_ANY,
837 .id = SERIO_ANY,
838 .extra = SERIO_ANY,
839 },
840 {
841 .type = SERIO_PS_PSTHRU,
842 .proto = SERIO_ANY,
843 .id = SERIO_ANY,
844 .extra = SERIO_ANY,
845 },
846 { 0 }
847};
848
849MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
850
851static struct serio_driver psmouse_drv = {
852 .driver = {
853 .name = "psmouse",
854 },
855 .description = DRIVER_DESC,
856 .id_table = psmouse_serio_ids,
857 .interrupt = psmouse_interrupt,
858 .connect = psmouse_connect,
859 .reconnect = psmouse_reconnect,
860 .disconnect = psmouse_disconnect,
861 .cleanup = psmouse_cleanup,
862};
863
864ssize_t psmouse_attr_show_helper(struct device *dev, char *buf,
865 ssize_t (*handler)(struct psmouse *, char *))
866{
867 struct serio *serio = to_serio_port(dev);
868 int retval;
869
870 retval = serio_pin_driver(serio);
871 if (retval)
872 return retval;
873
874 if (serio->drv != &psmouse_drv) {
875 retval = -ENODEV;
876 goto out;
877 }
878
879 retval = handler(serio_get_drvdata(serio), buf);
880
881out:
882 serio_unpin_driver(serio);
883 return retval;
884}
885
886ssize_t psmouse_attr_set_helper(struct device *dev, const char *buf, size_t count,
887 ssize_t (*handler)(struct psmouse *, const char *, size_t))
888{
889 struct serio *serio = to_serio_port(dev);
890 struct psmouse *psmouse = serio_get_drvdata(serio);
891 struct psmouse *parent = NULL;
892 int retval;
893
894 retval = serio_pin_driver(serio);
895 if (retval)
896 return retval;
897
898 if (serio->drv != &psmouse_drv) {
899 retval = -ENODEV;
900 goto out;
901 }
902
903 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
904 parent = serio_get_drvdata(serio->parent);
905 psmouse_deactivate(parent);
906 }
907 psmouse_deactivate(psmouse);
908
909 retval = handler(psmouse, buf, count);
910
911 psmouse_activate(psmouse);
912 if (parent)
913 psmouse_activate(parent);
914
915out:
916 serio_unpin_driver(serio);
917 return retval;
918}
919
920static ssize_t psmouse_attr_show_rate(struct psmouse *psmouse, char *buf)
921{
922 return sprintf(buf, "%d\n", psmouse->rate);
923}
924
925static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, const char *buf, size_t count)
926{
927 unsigned long value;
928 char *rest;
929
930 value = simple_strtoul(buf, &rest, 10);
931 if (*rest)
932 return -EINVAL;
933
934 psmouse->set_rate(psmouse, value);
935 return count;
936}
937
938static ssize_t psmouse_attr_show_resolution(struct psmouse *psmouse, char *buf)
939{
940 return sprintf(buf, "%d\n", psmouse->resolution);
941}
942
943static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, const char *buf, size_t count)
944{
945 unsigned long value;
946 char *rest;
947
948 value = simple_strtoul(buf, &rest, 10);
949 if (*rest)
950 return -EINVAL;
951
952 psmouse->set_resolution(psmouse, value);
953 return count;
954}
955
956static ssize_t psmouse_attr_show_resetafter(struct psmouse *psmouse, char *buf)
957{
958 return sprintf(buf, "%d\n", psmouse->resetafter);
959}
960
961static ssize_t psmouse_attr_set_resetafter(struct psmouse *psmouse, const char *buf, size_t count)
962{
963 unsigned long value;
964 char *rest;
965
966 value = simple_strtoul(buf, &rest, 10);
967 if (*rest)
968 return -EINVAL;
969
970 psmouse->resetafter = value;
971 return count;
972}
973
974static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
975{
976 int i;
977
978 if (!val)
979 return -EINVAL;
980
981 if (!strncmp(val, "any", 3)) {
Andrew Morton271b74d2005-05-28 02:11:06 -0500982 *((unsigned int *)kp->arg) = -1U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return 0;
984 }
985
986 for (i = 0; i < ARRAY_SIZE(psmouse_proto_abbrev); i++) {
987 if (!psmouse_proto_abbrev[i])
988 continue;
989
990 if (!strncmp(val, psmouse_proto_abbrev[i], strlen(psmouse_proto_abbrev[i]))) {
991 *((unsigned int *)kp->arg) = i;
992 return 0;
993 }
994 }
995
996 return -EINVAL; \
997}
998
999static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1000{
1001 return sprintf(buffer, "%s\n",
1002 psmouse_max_proto < ARRAY_SIZE(psmouse_proto_abbrev) ?
1003 psmouse_proto_abbrev[psmouse_max_proto] : "any");
1004}
1005
1006static int __init psmouse_init(void)
1007{
1008 serio_register_driver(&psmouse_drv);
1009 return 0;
1010}
1011
1012static void __exit psmouse_exit(void)
1013{
1014 serio_unregister_driver(&psmouse_drv);
1015}
1016
1017module_init(psmouse_init);
1018module_exit(psmouse_exit);