blob: 4d5ecc04c5b65382d5b8de27c345e2ba8813e05e [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
Dmitry Torokhovba449952005-12-21 00:51:31 -0500530 if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
531 return PSMOUSE_TRACKPOINT;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/*
534 * Reset to defaults in case the device got confused by extended
Dmitry Torokhovba449952005-12-21 00:51:31 -0500535 * protocol probes. Note that we do full reset becuase some mice
536 * put themselves to sleep when see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
Dmitry Torokhovba449952005-12-21 00:51:31 -0500538 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
541 return PSMOUSE_IMEX;
542
543 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
544 return PSMOUSE_IMPS;
545
546/*
547 * Okay, all failed, we have a standard mouse here. The number of the buttons
548 * is still a question, though. We assume 3.
549 */
550 ps2bare_detect(psmouse, set_properties);
551
552 if (synaptics_hardware) {
553/*
554 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
555 * We need to reset the touchpad because if there is a track point on the
556 * pass through port it could get disabled while probing for protocol
557 * extensions.
558 */
559 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 return PSMOUSE_PS2;
563}
564
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500565static struct psmouse_protocol psmouse_protocols[] = {
566 {
567 .type = PSMOUSE_PS2,
568 .name = "PS/2",
569 .alias = "bare",
570 .maxproto = 1,
571 .detect = ps2bare_detect,
572 },
573 {
574 .type = PSMOUSE_PS2PP,
575 .name = "PS2++",
576 .alias = "logitech",
577 .detect = ps2pp_init,
578 },
579 {
580 .type = PSMOUSE_THINKPS,
581 .name = "ThinkPS/2",
582 .alias = "thinkps",
583 .detect = thinking_detect,
584 },
585 {
586 .type = PSMOUSE_GENPS,
587 .name = "GenPS/2",
588 .alias = "genius",
589 .detect = genius_detect,
590 },
591 {
592 .type = PSMOUSE_IMPS,
593 .name = "ImPS/2",
594 .alias = "imps",
595 .maxproto = 1,
596 .detect = intellimouse_detect,
597 },
598 {
599 .type = PSMOUSE_IMEX,
600 .name = "ImExPS/2",
601 .alias = "exps",
602 .maxproto = 1,
603 .detect = im_explorer_detect,
604 },
605 {
606 .type = PSMOUSE_SYNAPTICS,
607 .name = "SynPS/2",
608 .alias = "synaptics",
609 .detect = synaptics_detect,
610 .init = synaptics_init,
611 },
612 {
613 .type = PSMOUSE_ALPS,
614 .name = "AlpsPS/2",
615 .alias = "alps",
616 .detect = alps_detect,
617 .init = alps_init,
618 },
619 {
620 .type = PSMOUSE_LIFEBOOK,
621 .name = "LBPS/2",
622 .alias = "lifebook",
623 .init = lifebook_init,
624 },
625 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500626 .type = PSMOUSE_TRACKPOINT,
627 .name = "TPPS/2",
628 .alias = "trackpoint",
629 .detect = trackpoint_detect,
630 },
631 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500632 .type = PSMOUSE_AUTO,
633 .name = "auto",
634 .alias = "any",
635 .maxproto = 1,
636 },
637};
638
639static struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
640{
641 int i;
642
643 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
644 if (psmouse_protocols[i].type == type)
645 return &psmouse_protocols[i];
646
647 WARN_ON(1);
648 return &psmouse_protocols[0];
649}
650
651static struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
652{
653 struct psmouse_protocol *p;
654 int i;
655
656 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
657 p = &psmouse_protocols[i];
658
659 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
660 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
661 return &psmouse_protocols[i];
662 }
663
664 return NULL;
665}
666
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/*
669 * psmouse_probe() probes for a PS/2 mouse.
670 */
671
672static int psmouse_probe(struct psmouse *psmouse)
673{
674 struct ps2dev *ps2dev = &psmouse->ps2dev;
675 unsigned char param[2];
676
677/*
678 * First, we check if it's a mouse. It should send 0x00 or 0x03
679 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500680 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
681 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
683
684 param[0] = 0xa5;
685 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
686 return -1;
687
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500688 if (param[0] != 0x00 && param[0] != 0x03 &&
689 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -1;
691
692/*
693 * Then we reset and disable the mouse so that it doesn't generate events.
694 */
695
696 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
697 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
698
699 return 0;
700}
701
702/*
703 * Here we set the mouse resolution.
704 */
705
706void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
707{
708 unsigned char params[] = { 0, 1, 2, 2, 3 };
709
710 if (resolution == 0 || resolution > 200)
711 resolution = 200;
712
713 ps2_command(&psmouse->ps2dev, &params[resolution / 50], PSMOUSE_CMD_SETRES);
714 psmouse->resolution = 25 << params[resolution / 50];
715}
716
717/*
718 * Here we set the mouse report rate.
719 */
720
721static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
722{
723 unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
724 int i = 0;
725
726 while (rates[i] > rate) i++;
727 ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE);
728 psmouse->rate = rates[i];
729}
730
731/*
732 * psmouse_initialize() initializes the mouse to a sane state.
733 */
734
735static void psmouse_initialize(struct psmouse *psmouse)
736{
737/*
738 * We set the mouse into streaming mode.
739 */
740
741 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
742
743/*
744 * We set the mouse report rate, resolution and scaling.
745 */
746
747 if (psmouse_max_proto != PSMOUSE_PS2) {
748 psmouse->set_rate(psmouse, psmouse->rate);
749 psmouse->set_resolution(psmouse, psmouse->resolution);
750 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
751 }
752}
753
754/*
755 * psmouse_set_state() sets new psmouse state and resets all flags and
756 * counters while holding serio lock so fighting with interrupt handler
757 * is not a concern.
758 */
759
760static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
761{
762 serio_pause_rx(psmouse->ps2dev.serio);
763 psmouse->state = new_state;
764 psmouse->pktcnt = psmouse->out_of_sync = 0;
765 psmouse->ps2dev.flags = 0;
766 serio_continue_rx(psmouse->ps2dev.serio);
767}
768
769/*
770 * psmouse_activate() enables the mouse so that we get motion reports from it.
771 */
772
773static void psmouse_activate(struct psmouse *psmouse)
774{
775 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
776 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
777 psmouse->ps2dev.serio->phys);
778
779 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
780}
781
782
783/*
784 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
785 * reports from it unless we explicitely request it.
786 */
787
788static void psmouse_deactivate(struct psmouse *psmouse)
789{
790 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
791 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
792 psmouse->ps2dev.serio->phys);
793
794 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
795}
796
797
798/*
799 * psmouse_cleanup() resets the mouse into power-on state.
800 */
801
802static void psmouse_cleanup(struct serio *serio)
803{
804 struct psmouse *psmouse = serio_get_drvdata(serio);
805
806 psmouse_reset(psmouse);
807}
808
809/*
810 * psmouse_disconnect() closes and frees.
811 */
812
813static void psmouse_disconnect(struct serio *serio)
814{
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500815 struct psmouse *psmouse, *parent = NULL;
816
817 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500819 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500821 down(&psmouse_sem);
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
824
825 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
826 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500827 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
830 if (psmouse->disconnect)
831 psmouse->disconnect(psmouse);
832
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500833 if (parent && parent->pt_deactivate)
834 parent->pt_deactivate(parent);
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 serio_close(serio);
839 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500840 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500842
843 if (parent)
844 psmouse_activate(parent);
845
846 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500849static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_protocol *proto)
850{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500851 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500852
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500853 input_dev->private = psmouse;
854 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500855
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500856 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
857 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
858 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500859
860 psmouse->set_rate = psmouse_set_rate;
861 psmouse->set_resolution = psmouse_set_resolution;
862 psmouse->protocol_handler = psmouse_process_byte;
863 psmouse->pktsize = 3;
864
865 if (proto && (proto->detect || proto->init)) {
866 if (proto->detect && proto->detect(psmouse, 1) < 0)
867 return -1;
868
869 if (proto->init && proto->init(psmouse) < 0)
870 return -1;
871
872 psmouse->type = proto->type;
873 }
874 else
875 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
876
877 sprintf(psmouse->devname, "%s %s %s",
878 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
879
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500880 input_dev->name = psmouse->devname;
881 input_dev->phys = psmouse->phys;
882 input_dev->id.bustype = BUS_I8042;
883 input_dev->id.vendor = 0x0002;
884 input_dev->id.product = psmouse->type;
885 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500886
887 return 0;
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/*
891 * psmouse_connect() is a callback from the serio module when
892 * an unhandled serio port is found.
893 */
894static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
895{
896 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500897 struct input_dev *input_dev;
898 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500900 down(&psmouse_sem);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
903 * If this is a pass-through port deactivate parent so the device
904 * connected to this port can be successfully identified
905 */
906 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
907 parent = serio_get_drvdata(serio->parent);
908 psmouse_deactivate(parent);
909 }
910
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500911 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
912 input_dev = input_allocate_device();
913 if (!psmouse || !input_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 ps2_init(&psmouse->ps2dev, serio);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500917 psmouse->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 sprintf(psmouse->phys, "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
921
922 serio_set_drvdata(serio, psmouse);
923
924 retval = serio_open(serio, drv);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500925 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (psmouse_probe(psmouse) < 0) {
929 serio_close(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 retval = -ENODEV;
931 goto out;
932 }
933
934 psmouse->rate = psmouse_rate;
935 psmouse->resolution = psmouse_resolution;
936 psmouse->resetafter = psmouse_resetafter;
937 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500939 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 psmouse_initialize(psmouse);
943
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500944 input_register_device(psmouse->dev);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (parent && parent->pt_activate)
947 parent->pt_activate(parent);
948
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500949 sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 psmouse_activate(psmouse);
952
953 retval = 0;
954
955out:
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500956 if (retval) {
957 serio_set_drvdata(serio, NULL);
958 input_free_device(input_dev);
959 kfree(psmouse);
960 }
961
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500962 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (parent)
964 psmouse_activate(parent);
965
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500966 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return retval;
968}
969
970
971static int psmouse_reconnect(struct serio *serio)
972{
973 struct psmouse *psmouse = serio_get_drvdata(serio);
974 struct psmouse *parent = NULL;
975 struct serio_driver *drv = serio->drv;
976 int rc = -1;
977
978 if (!drv || !psmouse) {
979 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
980 return -1;
981 }
982
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500983 down(&psmouse_sem);
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
986 parent = serio_get_drvdata(serio->parent);
987 psmouse_deactivate(parent);
988 }
989
990 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
991
992 if (psmouse->reconnect) {
993 if (psmouse->reconnect(psmouse))
994 goto out;
995 } else if (psmouse_probe(psmouse) < 0 ||
996 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
997 goto out;
998
999 /* ok, the device type (and capabilities) match the old one,
1000 * we can continue using it, complete intialization
1001 */
1002 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1003
1004 psmouse_initialize(psmouse);
1005
1006 if (parent && parent->pt_activate)
1007 parent->pt_activate(parent);
1008
1009 psmouse_activate(psmouse);
1010 rc = 0;
1011
1012out:
1013 /* If this is a pass-through port the parent waits to be activated */
1014 if (parent)
1015 psmouse_activate(parent);
1016
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001017 up(&psmouse_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return rc;
1019}
1020
1021static struct serio_device_id psmouse_serio_ids[] = {
1022 {
1023 .type = SERIO_8042,
1024 .proto = SERIO_ANY,
1025 .id = SERIO_ANY,
1026 .extra = SERIO_ANY,
1027 },
1028 {
1029 .type = SERIO_PS_PSTHRU,
1030 .proto = SERIO_ANY,
1031 .id = SERIO_ANY,
1032 .extra = SERIO_ANY,
1033 },
1034 { 0 }
1035};
1036
1037MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1038
1039static struct serio_driver psmouse_drv = {
1040 .driver = {
1041 .name = "psmouse",
1042 },
1043 .description = DRIVER_DESC,
1044 .id_table = psmouse_serio_ids,
1045 .interrupt = psmouse_interrupt,
1046 .connect = psmouse_connect,
1047 .reconnect = psmouse_reconnect,
1048 .disconnect = psmouse_disconnect,
1049 .cleanup = psmouse_cleanup,
1050};
1051
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001052ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1053 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001056 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1057 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 int retval;
1059
1060 retval = serio_pin_driver(serio);
1061 if (retval)
1062 return retval;
1063
1064 if (serio->drv != &psmouse_drv) {
1065 retval = -ENODEV;
1066 goto out;
1067 }
1068
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001069 psmouse = serio_get_drvdata(serio);
1070
1071 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073out:
1074 serio_unpin_driver(serio);
1075 return retval;
1076}
1077
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001078ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1079 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001082 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1083 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 int retval;
1085
1086 retval = serio_pin_driver(serio);
1087 if (retval)
1088 return retval;
1089
1090 if (serio->drv != &psmouse_drv) {
1091 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001092 goto out_unpin;
1093 }
1094
1095 retval = down_interruptible(&psmouse_sem);
1096 if (retval)
1097 goto out_unpin;
1098
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001099 psmouse = serio_get_drvdata(serio);
1100
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001101 if (psmouse->state == PSMOUSE_IGNORE) {
1102 retval = -ENODEV;
1103 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105
1106 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1107 parent = serio_get_drvdata(serio->parent);
1108 psmouse_deactivate(parent);
1109 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 psmouse_deactivate(psmouse);
1112
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001113 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001115 if (retval != -ENODEV)
1116 psmouse_activate(psmouse);
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (parent)
1119 psmouse_activate(parent);
1120
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001121 out_up:
1122 up(&psmouse_sem);
1123 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 serio_unpin_driver(serio);
1125 return retval;
1126}
1127
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001128static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1129{
1130 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1131
1132 return sprintf(buf, "%lu\n", *field);
1133}
1134
1135static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1136{
1137 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1138 unsigned long value;
1139 char *rest;
1140
1141 value = simple_strtoul(buf, &rest, 10);
1142 if (*rest)
1143 return -EINVAL;
1144
1145 *field = value;
1146
1147 return count;
1148}
1149
1150static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001151{
1152 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1153}
1154
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001155static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001156{
1157 struct serio *serio = psmouse->ps2dev.serio;
1158 struct psmouse *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001159 struct input_dev *new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001160 struct psmouse_protocol *proto;
1161 int retry = 0;
1162
1163 if (!(proto = psmouse_protocol_by_name(buf, count)))
1164 return -EINVAL;
1165
1166 if (psmouse->type == proto->type)
1167 return count;
1168
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001169 if (!(new_dev = input_allocate_device()))
1170 return -ENOMEM;
1171
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001172 while (serio->child) {
1173 if (++retry > 3) {
1174 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001175 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001176 return -EIO;
1177 }
1178
1179 up(&psmouse_sem);
1180 serio_unpin_driver(serio);
1181 serio_unregister_child_port(serio);
1182 serio_pin_driver_uninterruptible(serio);
1183 down(&psmouse_sem);
1184
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001185 if (serio->drv != &psmouse_drv) {
1186 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001187 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001188 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001189
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001190 if (psmouse->type == proto->type) {
1191 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001192 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001193 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001194 }
1195
1196 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1197 parent = serio_get_drvdata(serio->parent);
1198 if (parent->pt_deactivate)
1199 parent->pt_deactivate(parent);
1200 }
1201
1202 if (psmouse->disconnect)
1203 psmouse->disconnect(psmouse);
1204
1205 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001206 input_unregister_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001207
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001208 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001209 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1210
1211 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1212 psmouse_reset(psmouse);
1213 /* default to PSMOUSE_PS2 */
1214 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1215 }
1216
1217 psmouse_initialize(psmouse);
1218 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1219
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001220 input_register_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001221
1222 if (parent && parent->pt_activate)
1223 parent->pt_activate(parent);
1224
1225 return count;
1226}
1227
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001228static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 unsigned long value;
1231 char *rest;
1232
1233 value = simple_strtoul(buf, &rest, 10);
1234 if (*rest)
1235 return -EINVAL;
1236
1237 psmouse->set_rate(psmouse, value);
1238 return count;
1239}
1240
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001241static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 unsigned long value;
1244 char *rest;
1245
1246 value = simple_strtoul(buf, &rest, 10);
1247 if (*rest)
1248 return -EINVAL;
1249
1250 psmouse->set_resolution(psmouse, value);
1251 return count;
1252}
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1256{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001257 struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 if (!val)
1260 return -EINVAL;
1261
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001262 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001264 if (!proto || !proto->maxproto)
1265 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001267 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Stephen Evanchik541e3162005-08-08 01:26:18 -05001269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1273{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001274 int type = *((unsigned int *)kp->arg);
1275
1276 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279static int __init psmouse_init(void)
1280{
1281 serio_register_driver(&psmouse_drv);
1282 return 0;
1283}
1284
1285static void __exit psmouse_exit(void)
1286{
1287 serio_unregister_driver(&psmouse_drv);
1288}
1289
1290module_init(psmouse_init);
1291module_exit(psmouse_exit);