blob: 6ee9999a2eaa3fdfb8a87812d625c67ef4373fa9 [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
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
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050061PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
62 NULL,
63 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
64PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
65 (void *) offsetof(struct psmouse, rate),
66 psmouse_show_int_attr, psmouse_attr_set_rate);
67PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
68 (void *) offsetof(struct psmouse, resolution),
69 psmouse_show_int_attr, psmouse_attr_set_resolution);
70PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
71 (void *) offsetof(struct psmouse, resetafter),
72 psmouse_show_int_attr, psmouse_set_int_attr);
73
74static struct attribute *psmouse_attributes[] = {
75 &psmouse_attr_protocol.dattr.attr,
76 &psmouse_attr_rate.dattr.attr,
77 &psmouse_attr_resolution.dattr.attr,
78 &psmouse_attr_resetafter.dattr.attr,
79 NULL
80};
81
82static struct attribute_group psmouse_attribute_group = {
83 .attrs = psmouse_attributes,
84};
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86__obsolete_setup("psmouse_noext");
87__obsolete_setup("psmouse_resolution=");
88__obsolete_setup("psmouse_smartscroll=");
89__obsolete_setup("psmouse_resetafter=");
90__obsolete_setup("psmouse_rate=");
91
Dmitry Torokhov04df1922005-06-01 02:39:44 -050092/*
93 * psmouse_sem protects all operations changing state of mouse
94 * (connecting, disconnecting, changing rate or resolution via
95 * sysfs). We could use a per-device semaphore but since there
96 * rarely more than one PS/2 mouse connected and since semaphore
97 * is taken in "slow" paths it is not worth it.
98 */
99static DECLARE_MUTEX(psmouse_sem);
100
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500101struct psmouse_protocol {
102 enum psmouse_type type;
103 char *name;
104 char *alias;
105 int maxproto;
106 int (*detect)(struct psmouse *, int);
107 int (*init)(struct psmouse *);
108};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/*
111 * psmouse_process_byte() analyzes the PS/2 data stream and reports
112 * relevant events to the input module once full packet has arrived.
113 */
114
115static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
116{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500117 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned char *packet = psmouse->packet;
119
120 if (psmouse->pktcnt < psmouse->pktsize)
121 return PSMOUSE_GOOD_DATA;
122
123/*
124 * Full packet accumulated, process it
125 */
126
127 input_regs(dev, regs);
128
129/*
130 * Scroll wheel on IntelliMice, scroll buttons on NetMice
131 */
132
133 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
134 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
135
136/*
137 * Scroll wheel and buttons on IntelliMouse Explorer
138 */
139
140 if (psmouse->type == PSMOUSE_IMEX) {
141 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
142 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
143 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
144 }
145
146/*
147 * Extra buttons on Genius NewNet 3D
148 */
149
150 if (psmouse->type == PSMOUSE_GENPS) {
151 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
152 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
153 }
154
155/*
156 * Extra button on ThinkingMouse
157 */
158 if (psmouse->type == PSMOUSE_THINKPS) {
159 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
160 /* Without this bit of weirdness moving up gives wildly high Y changes. */
161 packet[1] |= (packet[0] & 0x40) << 1;
162 }
163
164/*
165 * Generic PS/2 Mouse
166 */
167
168 input_report_key(dev, BTN_LEFT, packet[0] & 1);
169 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
170 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
171
172 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
173 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
174
175 input_sync(dev);
176
177 return PSMOUSE_FULL_PACKET;
178}
179
180/*
181 * psmouse_interrupt() handles incoming characters, either gathering them into
182 * packets or passing them to the command routine as command output.
183 */
184
185static irqreturn_t psmouse_interrupt(struct serio *serio,
186 unsigned char data, unsigned int flags, struct pt_regs *regs)
187{
188 struct psmouse *psmouse = serio_get_drvdata(serio);
189 psmouse_ret_t rc;
190
191 if (psmouse->state == PSMOUSE_IGNORE)
192 goto out;
193
194 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
195 if (psmouse->state == PSMOUSE_ACTIVATED)
196 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
197 flags & SERIO_TIMEOUT ? " timeout" : "",
198 flags & SERIO_PARITY ? " bad parity" : "");
199 ps2_cmd_aborted(&psmouse->ps2dev);
200 goto out;
201 }
202
203 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
204 if (ps2_handle_ack(&psmouse->ps2dev, data))
205 goto out;
206
207 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
208 if (ps2_handle_response(&psmouse->ps2dev, data))
209 goto out;
210
211 if (psmouse->state == PSMOUSE_INITIALIZING)
212 goto out;
213
214 if (psmouse->state == PSMOUSE_ACTIVATED &&
215 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
216 printk(KERN_WARNING "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
217 psmouse->name, psmouse->phys, psmouse->pktcnt);
218 psmouse->pktcnt = 0;
219 }
220
221 psmouse->last = jiffies;
222 psmouse->packet[psmouse->pktcnt++] = data;
223
224 if (psmouse->packet[0] == PSMOUSE_RET_BAT) {
225 if (psmouse->pktcnt == 1)
226 goto out;
227
228 if (psmouse->pktcnt == 2) {
229 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
230 psmouse->state = PSMOUSE_IGNORE;
231 serio_reconnect(serio);
232 goto out;
233 }
234 if (psmouse->type == PSMOUSE_SYNAPTICS) {
235 /* neither 0xAA nor 0x00 are valid first bytes
236 * for a packet in absolute mode
237 */
238 psmouse->pktcnt = 0;
239 goto out;
240 }
241 }
242 }
243
244 rc = psmouse->protocol_handler(psmouse, regs);
245
246 switch (rc) {
247 case PSMOUSE_BAD_DATA:
248 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
249 psmouse->name, psmouse->phys, psmouse->pktcnt);
250 psmouse->pktcnt = 0;
251
252 if (++psmouse->out_of_sync == psmouse->resetafter) {
253 psmouse->state = PSMOUSE_IGNORE;
254 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
255 serio_reconnect(psmouse->ps2dev.serio);
256 }
257 break;
258
259 case PSMOUSE_FULL_PACKET:
260 psmouse->pktcnt = 0;
261 if (psmouse->out_of_sync) {
262 psmouse->out_of_sync = 0;
263 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
264 psmouse->name, psmouse->phys);
265 }
266 break;
267
268 case PSMOUSE_GOOD_DATA:
269 break;
270 }
271out:
272 return IRQ_HANDLED;
273}
274
275
276/*
277 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
278 * using sliced syntax, understood by advanced devices, such as Logitech
279 * or Synaptics touchpads. The command is encoded as:
280 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
281 * is the command.
282 */
283int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
284{
285 int i;
286
287 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
288 return -1;
289
290 for (i = 6; i >= 0; i -= 2) {
291 unsigned char d = (command >> i) & 3;
292 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
293 return -1;
294 }
295
296 return 0;
297}
298
299
300/*
301 * psmouse_reset() resets the mouse into power-on state.
302 */
303int psmouse_reset(struct psmouse *psmouse)
304{
305 unsigned char param[2];
306
307 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
308 return -1;
309
310 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
311 return -1;
312
313 return 0;
314}
315
316
317/*
318 * Genius NetMouse magic init.
319 */
320static int genius_detect(struct psmouse *psmouse, int set_properties)
321{
322 struct ps2dev *ps2dev = &psmouse->ps2dev;
323 unsigned char param[4];
324
325 param[0] = 3;
326 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
327 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
328 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
329 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
330 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
331
332 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
333 return -1;
334
335 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500336 set_bit(BTN_EXTRA, psmouse->dev->keybit);
337 set_bit(BTN_SIDE, psmouse->dev->keybit);
338 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 psmouse->vendor = "Genius";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 psmouse->pktsize = 4;
342 }
343
344 return 0;
345}
346
347/*
348 * IntelliMouse magic init.
349 */
350static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
351{
352 struct ps2dev *ps2dev = &psmouse->ps2dev;
353 unsigned char param[2];
354
355 param[0] = 200;
356 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
357 param[0] = 100;
358 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
359 param[0] = 80;
360 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
361 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
362
363 if (param[0] != 3)
364 return -1;
365
366 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500367 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
368 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (!psmouse->vendor) psmouse->vendor = "Generic";
371 if (!psmouse->name) psmouse->name = "Wheel Mouse";
372 psmouse->pktsize = 4;
373 }
374
375 return 0;
376}
377
378/*
379 * Try IntelliMouse/Explorer magic init.
380 */
381static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
382{
383 struct ps2dev *ps2dev = &psmouse->ps2dev;
384 unsigned char param[2];
385
386 intellimouse_detect(psmouse, 0);
387
388 param[0] = 200;
389 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
390 param[0] = 200;
391 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
392 param[0] = 80;
393 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
394 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
395
396 if (param[0] != 4)
397 return -1;
398
399 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500400 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
401 set_bit(REL_WHEEL, psmouse->dev->relbit);
402 set_bit(BTN_SIDE, psmouse->dev->keybit);
403 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!psmouse->vendor) psmouse->vendor = "Generic";
406 if (!psmouse->name) psmouse->name = "Explorer Mouse";
407 psmouse->pktsize = 4;
408 }
409
410 return 0;
411}
412
413/*
414 * Kensington ThinkingMouse / ExpertMouse magic init.
415 */
416static int thinking_detect(struct psmouse *psmouse, int set_properties)
417{
418 struct ps2dev *ps2dev = &psmouse->ps2dev;
419 unsigned char param[2];
420 unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20, 0 };
421 int i;
422
423 param[0] = 10;
424 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
425 param[0] = 0;
426 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
427 for (i = 0; seq[i]; i++)
428 ps2_command(ps2dev, seq + i, PSMOUSE_CMD_SETRATE);
429 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
430
431 if (param[0] != 2)
432 return -1;
433
434 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500435 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 psmouse->vendor = "Kensington";
438 psmouse->name = "ThinkingMouse";
439 }
440
441 return 0;
442}
443
444/*
445 * Bare PS/2 protocol "detection". Always succeeds.
446 */
447static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
448{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500449 if (set_properties) {
450 if (!psmouse->vendor) psmouse->vendor = "Generic";
451 if (!psmouse->name) psmouse->name = "Mouse";
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 return 0;
455}
456
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
460 * the mouse may have.
461 */
462
463static int psmouse_extensions(struct psmouse *psmouse,
464 unsigned int max_proto, int set_properties)
465{
466 int synaptics_hardware = 0;
467
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500468/*
469 * We always check for lifebook because it does not disturb mouse
470 * (it only checks DMI information).
471 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500472 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500473 if (max_proto > PSMOUSE_IMEX) {
474 if (!set_properties || lifebook_init(psmouse) == 0)
475 return PSMOUSE_LIFEBOOK;
476 }
477 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/*
480 * Try Kensington ThinkingMouse (we try first, because synaptics probe
481 * upsets the thinkingmouse).
482 */
483
484 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
485 return PSMOUSE_THINKPS;
486
487/*
488 * Try Synaptics TouchPad
489 */
490 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
491 synaptics_hardware = 1;
492
493 if (max_proto > PSMOUSE_IMEX) {
494 if (!set_properties || synaptics_init(psmouse) == 0)
495 return PSMOUSE_SYNAPTICS;
496/*
497 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
498 * Unfortunately Logitech/Genius probes confuse some firmware versions so
499 * we'll have to skip them.
500 */
501 max_proto = PSMOUSE_IMEX;
502 }
503/*
504 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
505 */
506 synaptics_reset(psmouse);
507 }
508
509/*
510 * Try ALPS TouchPad
511 */
512 if (max_proto > PSMOUSE_IMEX) {
513 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
514 if (alps_detect(psmouse, set_properties) == 0) {
515 if (!set_properties || alps_init(psmouse) == 0)
516 return PSMOUSE_ALPS;
517/*
518 * Init failed, try basic relative protocols
519 */
520 max_proto = PSMOUSE_IMEX;
521 }
522 }
523
524 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
525 return PSMOUSE_GENPS;
526
527 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
528 return PSMOUSE_PS2PP;
529
530/*
531 * Reset to defaults in case the device got confused by extended
532 * protocol probes.
533 */
534 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
535
536 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
537 return PSMOUSE_IMEX;
538
539 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
540 return PSMOUSE_IMPS;
541
542/*
Stephen Evanchik541e3162005-08-08 01:26:18 -0500543 * Try to initialize the IBM TrackPoint
544 */
545 if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
546 return PSMOUSE_TRACKPOINT;
547
548/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * Okay, all failed, we have a standard mouse here. The number of the buttons
550 * is still a question, though. We assume 3.
551 */
552 ps2bare_detect(psmouse, set_properties);
553
554 if (synaptics_hardware) {
555/*
556 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
557 * We need to reset the touchpad because if there is a track point on the
558 * pass through port it could get disabled while probing for protocol
559 * extensions.
560 */
561 psmouse_reset(psmouse);
562 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
563 }
564
565 return PSMOUSE_PS2;
566}
567
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500568static struct psmouse_protocol psmouse_protocols[] = {
569 {
570 .type = PSMOUSE_PS2,
571 .name = "PS/2",
572 .alias = "bare",
573 .maxproto = 1,
574 .detect = ps2bare_detect,
575 },
576 {
577 .type = PSMOUSE_PS2PP,
578 .name = "PS2++",
579 .alias = "logitech",
580 .detect = ps2pp_init,
581 },
582 {
583 .type = PSMOUSE_THINKPS,
584 .name = "ThinkPS/2",
585 .alias = "thinkps",
586 .detect = thinking_detect,
587 },
588 {
589 .type = PSMOUSE_GENPS,
590 .name = "GenPS/2",
591 .alias = "genius",
592 .detect = genius_detect,
593 },
594 {
595 .type = PSMOUSE_IMPS,
596 .name = "ImPS/2",
597 .alias = "imps",
598 .maxproto = 1,
599 .detect = intellimouse_detect,
600 },
601 {
602 .type = PSMOUSE_IMEX,
603 .name = "ImExPS/2",
604 .alias = "exps",
605 .maxproto = 1,
606 .detect = im_explorer_detect,
607 },
608 {
609 .type = PSMOUSE_SYNAPTICS,
610 .name = "SynPS/2",
611 .alias = "synaptics",
612 .detect = synaptics_detect,
613 .init = synaptics_init,
614 },
615 {
616 .type = PSMOUSE_ALPS,
617 .name = "AlpsPS/2",
618 .alias = "alps",
619 .detect = alps_detect,
620 .init = alps_init,
621 },
622 {
623 .type = PSMOUSE_LIFEBOOK,
624 .name = "LBPS/2",
625 .alias = "lifebook",
626 .init = lifebook_init,
627 },
628 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500629 .type = PSMOUSE_TRACKPOINT,
630 .name = "TPPS/2",
631 .alias = "trackpoint",
632 .detect = trackpoint_detect,
633 },
634 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500635 .type = PSMOUSE_AUTO,
636 .name = "auto",
637 .alias = "any",
638 .maxproto = 1,
639 },
640};
641
642static struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
643{
644 int i;
645
646 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
647 if (psmouse_protocols[i].type == type)
648 return &psmouse_protocols[i];
649
650 WARN_ON(1);
651 return &psmouse_protocols[0];
652}
653
654static struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
655{
656 struct psmouse_protocol *p;
657 int i;
658
659 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
660 p = &psmouse_protocols[i];
661
662 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
663 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
664 return &psmouse_protocols[i];
665 }
666
667 return NULL;
668}
669
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/*
672 * psmouse_probe() probes for a PS/2 mouse.
673 */
674
675static int psmouse_probe(struct psmouse *psmouse)
676{
677 struct ps2dev *ps2dev = &psmouse->ps2dev;
678 unsigned char param[2];
679
680/*
681 * First, we check if it's a mouse. It should send 0x00 or 0x03
682 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500683 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
684 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
686
687 param[0] = 0xa5;
688 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
689 return -1;
690
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500691 if (param[0] != 0x00 && param[0] != 0x03 &&
692 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -1;
694
695/*
696 * Then we reset and disable the mouse so that it doesn't generate events.
697 */
698
699 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
700 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
701
702 return 0;
703}
704
705/*
706 * Here we set the mouse resolution.
707 */
708
709void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
710{
711 unsigned char params[] = { 0, 1, 2, 2, 3 };
712
713 if (resolution == 0 || resolution > 200)
714 resolution = 200;
715
716 ps2_command(&psmouse->ps2dev, &params[resolution / 50], PSMOUSE_CMD_SETRES);
717 psmouse->resolution = 25 << params[resolution / 50];
718}
719
720/*
721 * Here we set the mouse report rate.
722 */
723
724static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
725{
726 unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
727 int i = 0;
728
729 while (rates[i] > rate) i++;
730 ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE);
731 psmouse->rate = rates[i];
732}
733
734/*
735 * psmouse_initialize() initializes the mouse to a sane state.
736 */
737
738static void psmouse_initialize(struct psmouse *psmouse)
739{
740/*
741 * We set the mouse into streaming mode.
742 */
743
744 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
745
746/*
747 * We set the mouse report rate, resolution and scaling.
748 */
749
750 if (psmouse_max_proto != PSMOUSE_PS2) {
751 psmouse->set_rate(psmouse, psmouse->rate);
752 psmouse->set_resolution(psmouse, psmouse->resolution);
753 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
754 }
755}
756
757/*
758 * psmouse_set_state() sets new psmouse state and resets all flags and
759 * counters while holding serio lock so fighting with interrupt handler
760 * is not a concern.
761 */
762
763static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
764{
765 serio_pause_rx(psmouse->ps2dev.serio);
766 psmouse->state = new_state;
767 psmouse->pktcnt = psmouse->out_of_sync = 0;
768 psmouse->ps2dev.flags = 0;
769 serio_continue_rx(psmouse->ps2dev.serio);
770}
771
772/*
773 * psmouse_activate() enables the mouse so that we get motion reports from it.
774 */
775
776static void psmouse_activate(struct psmouse *psmouse)
777{
778 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
779 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
780 psmouse->ps2dev.serio->phys);
781
782 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
783}
784
785
786/*
787 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
788 * reports from it unless we explicitely request it.
789 */
790
791static void psmouse_deactivate(struct psmouse *psmouse)
792{
793 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
794 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
795 psmouse->ps2dev.serio->phys);
796
797 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
798}
799
800
801/*
802 * psmouse_cleanup() resets the mouse into power-on state.
803 */
804
805static void psmouse_cleanup(struct serio *serio)
806{
807 struct psmouse *psmouse = serio_get_drvdata(serio);
808
809 psmouse_reset(psmouse);
810}
811
812/*
813 * psmouse_disconnect() closes and frees.
814 */
815
816static void psmouse_disconnect(struct serio *serio)
817{
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500818 struct psmouse *psmouse, *parent = NULL;
819
820 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500822 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500824 down(&psmouse_sem);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
827
828 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
829 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500830 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 if (psmouse->disconnect)
834 psmouse->disconnect(psmouse);
835
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500836 if (parent && parent->pt_deactivate)
837 parent->pt_deactivate(parent);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 serio_close(serio);
842 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500843 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500845
846 if (parent)
847 psmouse_activate(parent);
848
849 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500852static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_protocol *proto)
853{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500854 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500855
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500856 input_dev->private = psmouse;
857 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500858
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500859 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
860 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
861 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500862
863 psmouse->set_rate = psmouse_set_rate;
864 psmouse->set_resolution = psmouse_set_resolution;
865 psmouse->protocol_handler = psmouse_process_byte;
866 psmouse->pktsize = 3;
867
868 if (proto && (proto->detect || proto->init)) {
869 if (proto->detect && proto->detect(psmouse, 1) < 0)
870 return -1;
871
872 if (proto->init && proto->init(psmouse) < 0)
873 return -1;
874
875 psmouse->type = proto->type;
876 }
877 else
878 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
879
880 sprintf(psmouse->devname, "%s %s %s",
881 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
882
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500883 input_dev->name = psmouse->devname;
884 input_dev->phys = psmouse->phys;
885 input_dev->id.bustype = BUS_I8042;
886 input_dev->id.vendor = 0x0002;
887 input_dev->id.product = psmouse->type;
888 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500889
890 return 0;
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/*
894 * psmouse_connect() is a callback from the serio module when
895 * an unhandled serio port is found.
896 */
897static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
898{
899 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500900 struct input_dev *input_dev;
901 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500903 down(&psmouse_sem);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /*
906 * If this is a pass-through port deactivate parent so the device
907 * connected to this port can be successfully identified
908 */
909 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
910 parent = serio_get_drvdata(serio->parent);
911 psmouse_deactivate(parent);
912 }
913
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500914 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
915 input_dev = input_allocate_device();
916 if (!psmouse || !input_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 ps2_init(&psmouse->ps2dev, serio);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500920 psmouse->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 sprintf(psmouse->phys, "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
924
925 serio_set_drvdata(serio, psmouse);
926
927 retval = serio_open(serio, drv);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500928 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if (psmouse_probe(psmouse) < 0) {
932 serio_close(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 retval = -ENODEV;
934 goto out;
935 }
936
937 psmouse->rate = psmouse_rate;
938 psmouse->resolution = psmouse_resolution;
939 psmouse->resetafter = psmouse_resetafter;
940 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500942 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 psmouse_initialize(psmouse);
946
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500947 input_register_device(psmouse->dev);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (parent && parent->pt_activate)
950 parent->pt_activate(parent);
951
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500952 sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 psmouse_activate(psmouse);
955
956 retval = 0;
957
958out:
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500959 if (retval) {
960 serio_set_drvdata(serio, NULL);
961 input_free_device(input_dev);
962 kfree(psmouse);
963 }
964
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500965 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (parent)
967 psmouse_activate(parent);
968
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500969 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return retval;
971}
972
973
974static int psmouse_reconnect(struct serio *serio)
975{
976 struct psmouse *psmouse = serio_get_drvdata(serio);
977 struct psmouse *parent = NULL;
978 struct serio_driver *drv = serio->drv;
979 int rc = -1;
980
981 if (!drv || !psmouse) {
982 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
983 return -1;
984 }
985
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500986 down(&psmouse_sem);
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
989 parent = serio_get_drvdata(serio->parent);
990 psmouse_deactivate(parent);
991 }
992
993 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
994
995 if (psmouse->reconnect) {
996 if (psmouse->reconnect(psmouse))
997 goto out;
998 } else if (psmouse_probe(psmouse) < 0 ||
999 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1000 goto out;
1001
1002 /* ok, the device type (and capabilities) match the old one,
1003 * we can continue using it, complete intialization
1004 */
1005 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1006
1007 psmouse_initialize(psmouse);
1008
1009 if (parent && parent->pt_activate)
1010 parent->pt_activate(parent);
1011
1012 psmouse_activate(psmouse);
1013 rc = 0;
1014
1015out:
1016 /* If this is a pass-through port the parent waits to be activated */
1017 if (parent)
1018 psmouse_activate(parent);
1019
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001020 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return rc;
1022}
1023
1024static struct serio_device_id psmouse_serio_ids[] = {
1025 {
1026 .type = SERIO_8042,
1027 .proto = SERIO_ANY,
1028 .id = SERIO_ANY,
1029 .extra = SERIO_ANY,
1030 },
1031 {
1032 .type = SERIO_PS_PSTHRU,
1033 .proto = SERIO_ANY,
1034 .id = SERIO_ANY,
1035 .extra = SERIO_ANY,
1036 },
1037 { 0 }
1038};
1039
1040MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1041
1042static struct serio_driver psmouse_drv = {
1043 .driver = {
1044 .name = "psmouse",
1045 },
1046 .description = DRIVER_DESC,
1047 .id_table = psmouse_serio_ids,
1048 .interrupt = psmouse_interrupt,
1049 .connect = psmouse_connect,
1050 .reconnect = psmouse_reconnect,
1051 .disconnect = psmouse_disconnect,
1052 .cleanup = psmouse_cleanup,
1053};
1054
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001055ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1056 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001059 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1060 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 int retval;
1062
1063 retval = serio_pin_driver(serio);
1064 if (retval)
1065 return retval;
1066
1067 if (serio->drv != &psmouse_drv) {
1068 retval = -ENODEV;
1069 goto out;
1070 }
1071
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001072 psmouse = serio_get_drvdata(serio);
1073
1074 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076out:
1077 serio_unpin_driver(serio);
1078 return retval;
1079}
1080
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001081ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1082 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001085 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1086 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int retval;
1088
1089 retval = serio_pin_driver(serio);
1090 if (retval)
1091 return retval;
1092
1093 if (serio->drv != &psmouse_drv) {
1094 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001095 goto out_unpin;
1096 }
1097
1098 retval = down_interruptible(&psmouse_sem);
1099 if (retval)
1100 goto out_unpin;
1101
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001102 psmouse = serio_get_drvdata(serio);
1103
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001104 if (psmouse->state == PSMOUSE_IGNORE) {
1105 retval = -ENODEV;
1106 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
1109 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1110 parent = serio_get_drvdata(serio->parent);
1111 psmouse_deactivate(parent);
1112 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 psmouse_deactivate(psmouse);
1115
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001116 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001118 if (retval != -ENODEV)
1119 psmouse_activate(psmouse);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (parent)
1122 psmouse_activate(parent);
1123
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001124 out_up:
1125 up(&psmouse_sem);
1126 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 serio_unpin_driver(serio);
1128 return retval;
1129}
1130
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001131static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1132{
1133 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1134
1135 return sprintf(buf, "%lu\n", *field);
1136}
1137
1138static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1139{
1140 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1141 unsigned long value;
1142 char *rest;
1143
1144 value = simple_strtoul(buf, &rest, 10);
1145 if (*rest)
1146 return -EINVAL;
1147
1148 *field = value;
1149
1150 return count;
1151}
1152
1153static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001154{
1155 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1156}
1157
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001158static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001159{
1160 struct serio *serio = psmouse->ps2dev.serio;
1161 struct psmouse *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001162 struct input_dev *new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001163 struct psmouse_protocol *proto;
1164 int retry = 0;
1165
1166 if (!(proto = psmouse_protocol_by_name(buf, count)))
1167 return -EINVAL;
1168
1169 if (psmouse->type == proto->type)
1170 return count;
1171
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001172 if (!(new_dev = input_allocate_device()))
1173 return -ENOMEM;
1174
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001175 while (serio->child) {
1176 if (++retry > 3) {
1177 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001178 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001179 return -EIO;
1180 }
1181
1182 up(&psmouse_sem);
1183 serio_unpin_driver(serio);
1184 serio_unregister_child_port(serio);
1185 serio_pin_driver_uninterruptible(serio);
1186 down(&psmouse_sem);
1187
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001188 if (serio->drv != &psmouse_drv) {
1189 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001190 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001191 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001192
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001193 if (psmouse->type == proto->type) {
1194 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001195 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001196 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001197 }
1198
1199 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1200 parent = serio_get_drvdata(serio->parent);
1201 if (parent->pt_deactivate)
1202 parent->pt_deactivate(parent);
1203 }
1204
1205 if (psmouse->disconnect)
1206 psmouse->disconnect(psmouse);
1207
1208 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001209 input_unregister_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001210
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001211 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001212 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1213
1214 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1215 psmouse_reset(psmouse);
1216 /* default to PSMOUSE_PS2 */
1217 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1218 }
1219
1220 psmouse_initialize(psmouse);
1221 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1222
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001223 input_register_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001224
1225 if (parent && parent->pt_activate)
1226 parent->pt_activate(parent);
1227
1228 return count;
1229}
1230
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001231static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 unsigned long value;
1234 char *rest;
1235
1236 value = simple_strtoul(buf, &rest, 10);
1237 if (*rest)
1238 return -EINVAL;
1239
1240 psmouse->set_rate(psmouse, value);
1241 return count;
1242}
1243
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001244static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 unsigned long value;
1247 char *rest;
1248
1249 value = simple_strtoul(buf, &rest, 10);
1250 if (*rest)
1251 return -EINVAL;
1252
1253 psmouse->set_resolution(psmouse, value);
1254 return count;
1255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1259{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001260 struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (!val)
1263 return -EINVAL;
1264
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001265 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001267 if (!proto || !proto->maxproto)
1268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001270 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Stephen Evanchik541e3162005-08-08 01:26:18 -05001272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1276{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001277 int type = *((unsigned int *)kp->arg);
1278
1279 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
1282static int __init psmouse_init(void)
1283{
1284 serio_register_driver(&psmouse_drv);
1285 return 0;
1286}
1287
1288static void __exit psmouse_exit(void)
1289{
1290 serio_unregister_driver(&psmouse_drv);
1291}
1292
1293module_init(psmouse_init);
1294module_exit(psmouse_exit);