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