blob: 0ecf1297b6a84ba16fa6718e01259f5c2bfee981 [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
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500427/*
428 * We always check for lifebook because it does not disturb mouse
429 * (it only checks DMI information).
430 */
431 if (lifebook_detect(psmouse, set_properties) == 0 ||
432 max_proto == PSMOUSE_LIFEBOOK) {
433
434 if (max_proto > PSMOUSE_IMEX) {
435 if (!set_properties || lifebook_init(psmouse) == 0)
436 return PSMOUSE_LIFEBOOK;
437 }
438 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
441 * Try Kensington ThinkingMouse (we try first, because synaptics probe
442 * upsets the thinkingmouse).
443 */
444
445 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
446 return PSMOUSE_THINKPS;
447
448/*
449 * Try Synaptics TouchPad
450 */
451 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
452 synaptics_hardware = 1;
453
454 if (max_proto > PSMOUSE_IMEX) {
455 if (!set_properties || synaptics_init(psmouse) == 0)
456 return PSMOUSE_SYNAPTICS;
457/*
458 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
459 * Unfortunately Logitech/Genius probes confuse some firmware versions so
460 * we'll have to skip them.
461 */
462 max_proto = PSMOUSE_IMEX;
463 }
464/*
465 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
466 */
467 synaptics_reset(psmouse);
468 }
469
470/*
471 * Try ALPS TouchPad
472 */
473 if (max_proto > PSMOUSE_IMEX) {
474 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
475 if (alps_detect(psmouse, set_properties) == 0) {
476 if (!set_properties || alps_init(psmouse) == 0)
477 return PSMOUSE_ALPS;
478/*
479 * Init failed, try basic relative protocols
480 */
481 max_proto = PSMOUSE_IMEX;
482 }
483 }
484
485 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
486 return PSMOUSE_GENPS;
487
488 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
489 return PSMOUSE_PS2PP;
490
491/*
492 * Reset to defaults in case the device got confused by extended
493 * protocol probes.
494 */
495 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
496
497 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
498 return PSMOUSE_IMEX;
499
500 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
501 return PSMOUSE_IMPS;
502
503/*
504 * Okay, all failed, we have a standard mouse here. The number of the buttons
505 * is still a question, though. We assume 3.
506 */
507 ps2bare_detect(psmouse, set_properties);
508
509 if (synaptics_hardware) {
510/*
511 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
512 * We need to reset the touchpad because if there is a track point on the
513 * pass through port it could get disabled while probing for protocol
514 * extensions.
515 */
516 psmouse_reset(psmouse);
517 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
518 }
519
520 return PSMOUSE_PS2;
521}
522
523/*
524 * psmouse_probe() probes for a PS/2 mouse.
525 */
526
527static int psmouse_probe(struct psmouse *psmouse)
528{
529 struct ps2dev *ps2dev = &psmouse->ps2dev;
530 unsigned char param[2];
531
532/*
533 * First, we check if it's a mouse. It should send 0x00 or 0x03
534 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500535 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
536 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538
539 param[0] = 0xa5;
540 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
541 return -1;
542
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500543 if (param[0] != 0x00 && param[0] != 0x03 &&
544 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return -1;
546
547/*
548 * Then we reset and disable the mouse so that it doesn't generate events.
549 */
550
551 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
552 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
553
554 return 0;
555}
556
557/*
558 * Here we set the mouse resolution.
559 */
560
561void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
562{
563 unsigned char params[] = { 0, 1, 2, 2, 3 };
564
565 if (resolution == 0 || resolution > 200)
566 resolution = 200;
567
568 ps2_command(&psmouse->ps2dev, &params[resolution / 50], PSMOUSE_CMD_SETRES);
569 psmouse->resolution = 25 << params[resolution / 50];
570}
571
572/*
573 * Here we set the mouse report rate.
574 */
575
576static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
577{
578 unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
579 int i = 0;
580
581 while (rates[i] > rate) i++;
582 ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE);
583 psmouse->rate = rates[i];
584}
585
586/*
587 * psmouse_initialize() initializes the mouse to a sane state.
588 */
589
590static void psmouse_initialize(struct psmouse *psmouse)
591{
592/*
593 * We set the mouse into streaming mode.
594 */
595
596 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
597
598/*
599 * We set the mouse report rate, resolution and scaling.
600 */
601
602 if (psmouse_max_proto != PSMOUSE_PS2) {
603 psmouse->set_rate(psmouse, psmouse->rate);
604 psmouse->set_resolution(psmouse, psmouse->resolution);
605 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
606 }
607}
608
609/*
610 * psmouse_set_state() sets new psmouse state and resets all flags and
611 * counters while holding serio lock so fighting with interrupt handler
612 * is not a concern.
613 */
614
615static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
616{
617 serio_pause_rx(psmouse->ps2dev.serio);
618 psmouse->state = new_state;
619 psmouse->pktcnt = psmouse->out_of_sync = 0;
620 psmouse->ps2dev.flags = 0;
621 serio_continue_rx(psmouse->ps2dev.serio);
622}
623
624/*
625 * psmouse_activate() enables the mouse so that we get motion reports from it.
626 */
627
628static void psmouse_activate(struct psmouse *psmouse)
629{
630 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
631 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
632 psmouse->ps2dev.serio->phys);
633
634 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
635}
636
637
638/*
639 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
640 * reports from it unless we explicitely request it.
641 */
642
643static void psmouse_deactivate(struct psmouse *psmouse)
644{
645 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
646 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
647 psmouse->ps2dev.serio->phys);
648
649 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
650}
651
652
653/*
654 * psmouse_cleanup() resets the mouse into power-on state.
655 */
656
657static void psmouse_cleanup(struct serio *serio)
658{
659 struct psmouse *psmouse = serio_get_drvdata(serio);
660
661 psmouse_reset(psmouse);
662}
663
664/*
665 * psmouse_disconnect() closes and frees.
666 */
667
668static void psmouse_disconnect(struct serio *serio)
669{
670 struct psmouse *psmouse, *parent;
671
672 device_remove_file(&serio->dev, &psmouse_attr_rate);
673 device_remove_file(&serio->dev, &psmouse_attr_resolution);
674 device_remove_file(&serio->dev, &psmouse_attr_resetafter);
675
676 psmouse = serio_get_drvdata(serio);
677 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
678
679 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
680 parent = serio_get_drvdata(serio->parent);
681 if (parent->pt_deactivate)
682 parent->pt_deactivate(parent);
683 }
684
685 if (psmouse->disconnect)
686 psmouse->disconnect(psmouse);
687
688 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
689
690 input_unregister_device(&psmouse->dev);
691 serio_close(serio);
692 serio_set_drvdata(serio, NULL);
693 kfree(psmouse);
694}
695
696/*
697 * psmouse_connect() is a callback from the serio module when
698 * an unhandled serio port is found.
699 */
700static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
701{
702 struct psmouse *psmouse, *parent = NULL;
703 int retval;
704
705 /*
706 * If this is a pass-through port deactivate parent so the device
707 * connected to this port can be successfully identified
708 */
709 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
710 parent = serio_get_drvdata(serio->parent);
711 psmouse_deactivate(parent);
712 }
713
714 if (!(psmouse = kmalloc(sizeof(struct psmouse), GFP_KERNEL))) {
715 retval = -ENOMEM;
716 goto out;
717 }
718
719 memset(psmouse, 0, sizeof(struct psmouse));
720
721 ps2_init(&psmouse->ps2dev, serio);
722 sprintf(psmouse->phys, "%s/input0", serio->phys);
723 psmouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
724 psmouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
725 psmouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
726 psmouse->dev.private = psmouse;
727 psmouse->dev.dev = &serio->dev;
728 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
729
730 serio_set_drvdata(serio, psmouse);
731
732 retval = serio_open(serio, drv);
733 if (retval) {
734 serio_set_drvdata(serio, NULL);
735 kfree(psmouse);
736 goto out;
737 }
738
739 if (psmouse_probe(psmouse) < 0) {
740 serio_close(serio);
741 serio_set_drvdata(serio, NULL);
742 kfree(psmouse);
743 retval = -ENODEV;
744 goto out;
745 }
746
747 psmouse->rate = psmouse_rate;
748 psmouse->resolution = psmouse_resolution;
749 psmouse->resetafter = psmouse_resetafter;
750 psmouse->smartscroll = psmouse_smartscroll;
751 psmouse->set_rate = psmouse_set_rate;
752 psmouse->set_resolution = psmouse_set_resolution;
753 psmouse->protocol_handler = psmouse_process_byte;
754 psmouse->pktsize = 3;
755
756 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
757
758 sprintf(psmouse->devname, "%s %s %s",
759 psmouse_protocols[psmouse->type], psmouse->vendor, psmouse->name);
760
761 psmouse->dev.name = psmouse->devname;
762 psmouse->dev.phys = psmouse->phys;
763 psmouse->dev.id.bustype = BUS_I8042;
764 psmouse->dev.id.vendor = 0x0002;
765 psmouse->dev.id.product = psmouse->type;
766 psmouse->dev.id.version = psmouse->model;
767
768 input_register_device(&psmouse->dev);
769
770 printk(KERN_INFO "input: %s on %s\n", psmouse->devname, serio->phys);
771
772 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
773
774 psmouse_initialize(psmouse);
775
776 if (parent && parent->pt_activate)
777 parent->pt_activate(parent);
778
779 device_create_file(&serio->dev, &psmouse_attr_rate);
780 device_create_file(&serio->dev, &psmouse_attr_resolution);
781 device_create_file(&serio->dev, &psmouse_attr_resetafter);
782
783 psmouse_activate(psmouse);
784
785 retval = 0;
786
787out:
788 /* If this is a pass-through port the parent awaits to be activated */
789 if (parent)
790 psmouse_activate(parent);
791
792 return retval;
793}
794
795
796static int psmouse_reconnect(struct serio *serio)
797{
798 struct psmouse *psmouse = serio_get_drvdata(serio);
799 struct psmouse *parent = NULL;
800 struct serio_driver *drv = serio->drv;
801 int rc = -1;
802
803 if (!drv || !psmouse) {
804 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
805 return -1;
806 }
807
808 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
809 parent = serio_get_drvdata(serio->parent);
810 psmouse_deactivate(parent);
811 }
812
813 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
814
815 if (psmouse->reconnect) {
816 if (psmouse->reconnect(psmouse))
817 goto out;
818 } else if (psmouse_probe(psmouse) < 0 ||
819 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
820 goto out;
821
822 /* ok, the device type (and capabilities) match the old one,
823 * we can continue using it, complete intialization
824 */
825 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
826
827 psmouse_initialize(psmouse);
828
829 if (parent && parent->pt_activate)
830 parent->pt_activate(parent);
831
832 psmouse_activate(psmouse);
833 rc = 0;
834
835out:
836 /* If this is a pass-through port the parent waits to be activated */
837 if (parent)
838 psmouse_activate(parent);
839
840 return rc;
841}
842
843static struct serio_device_id psmouse_serio_ids[] = {
844 {
845 .type = SERIO_8042,
846 .proto = SERIO_ANY,
847 .id = SERIO_ANY,
848 .extra = SERIO_ANY,
849 },
850 {
851 .type = SERIO_PS_PSTHRU,
852 .proto = SERIO_ANY,
853 .id = SERIO_ANY,
854 .extra = SERIO_ANY,
855 },
856 { 0 }
857};
858
859MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
860
861static struct serio_driver psmouse_drv = {
862 .driver = {
863 .name = "psmouse",
864 },
865 .description = DRIVER_DESC,
866 .id_table = psmouse_serio_ids,
867 .interrupt = psmouse_interrupt,
868 .connect = psmouse_connect,
869 .reconnect = psmouse_reconnect,
870 .disconnect = psmouse_disconnect,
871 .cleanup = psmouse_cleanup,
872};
873
874ssize_t psmouse_attr_show_helper(struct device *dev, char *buf,
875 ssize_t (*handler)(struct psmouse *, char *))
876{
877 struct serio *serio = to_serio_port(dev);
878 int retval;
879
880 retval = serio_pin_driver(serio);
881 if (retval)
882 return retval;
883
884 if (serio->drv != &psmouse_drv) {
885 retval = -ENODEV;
886 goto out;
887 }
888
889 retval = handler(serio_get_drvdata(serio), buf);
890
891out:
892 serio_unpin_driver(serio);
893 return retval;
894}
895
896ssize_t psmouse_attr_set_helper(struct device *dev, const char *buf, size_t count,
897 ssize_t (*handler)(struct psmouse *, const char *, size_t))
898{
899 struct serio *serio = to_serio_port(dev);
900 struct psmouse *psmouse = serio_get_drvdata(serio);
901 struct psmouse *parent = NULL;
902 int retval;
903
904 retval = serio_pin_driver(serio);
905 if (retval)
906 return retval;
907
908 if (serio->drv != &psmouse_drv) {
909 retval = -ENODEV;
910 goto out;
911 }
912
913 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
914 parent = serio_get_drvdata(serio->parent);
915 psmouse_deactivate(parent);
916 }
917 psmouse_deactivate(psmouse);
918
919 retval = handler(psmouse, buf, count);
920
921 psmouse_activate(psmouse);
922 if (parent)
923 psmouse_activate(parent);
924
925out:
926 serio_unpin_driver(serio);
927 return retval;
928}
929
930static ssize_t psmouse_attr_show_rate(struct psmouse *psmouse, char *buf)
931{
932 return sprintf(buf, "%d\n", psmouse->rate);
933}
934
935static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, const char *buf, size_t count)
936{
937 unsigned long value;
938 char *rest;
939
940 value = simple_strtoul(buf, &rest, 10);
941 if (*rest)
942 return -EINVAL;
943
944 psmouse->set_rate(psmouse, value);
945 return count;
946}
947
948static ssize_t psmouse_attr_show_resolution(struct psmouse *psmouse, char *buf)
949{
950 return sprintf(buf, "%d\n", psmouse->resolution);
951}
952
953static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, const char *buf, size_t count)
954{
955 unsigned long value;
956 char *rest;
957
958 value = simple_strtoul(buf, &rest, 10);
959 if (*rest)
960 return -EINVAL;
961
962 psmouse->set_resolution(psmouse, value);
963 return count;
964}
965
966static ssize_t psmouse_attr_show_resetafter(struct psmouse *psmouse, char *buf)
967{
968 return sprintf(buf, "%d\n", psmouse->resetafter);
969}
970
971static ssize_t psmouse_attr_set_resetafter(struct psmouse *psmouse, const char *buf, size_t count)
972{
973 unsigned long value;
974 char *rest;
975
976 value = simple_strtoul(buf, &rest, 10);
977 if (*rest)
978 return -EINVAL;
979
980 psmouse->resetafter = value;
981 return count;
982}
983
984static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
985{
986 int i;
987
988 if (!val)
989 return -EINVAL;
990
991 if (!strncmp(val, "any", 3)) {
Andrew Morton271b74d2005-05-28 02:11:06 -0500992 *((unsigned int *)kp->arg) = -1U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return 0;
994 }
995
996 for (i = 0; i < ARRAY_SIZE(psmouse_proto_abbrev); i++) {
997 if (!psmouse_proto_abbrev[i])
998 continue;
999
1000 if (!strncmp(val, psmouse_proto_abbrev[i], strlen(psmouse_proto_abbrev[i]))) {
1001 *((unsigned int *)kp->arg) = i;
1002 return 0;
1003 }
1004 }
1005
1006 return -EINVAL; \
1007}
1008
1009static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1010{
1011 return sprintf(buffer, "%s\n",
1012 psmouse_max_proto < ARRAY_SIZE(psmouse_proto_abbrev) ?
1013 psmouse_proto_abbrev[psmouse_max_proto] : "any");
1014}
1015
1016static int __init psmouse_init(void)
1017{
1018 serio_register_driver(&psmouse_drv);
1019 return 0;
1020}
1021
1022static void __exit psmouse_exit(void)
1023{
1024 serio_unregister_driver(&psmouse_drv);
1025}
1026
1027module_init(psmouse_init);
1028module_exit(psmouse_exit);