blob: 136321a2cfdb57b9211972fc4e2240e7ce86697e [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>
Ingo Molnarc14471d2006-02-19 00:22:11 -050023#include <linux/mutex.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "psmouse.h"
26#include "synaptics.h"
27#include "logips2pp.h"
28#include "alps.h"
Kenan Esau02d7f582005-05-29 02:30:22 -050029#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050030#include "trackpoint.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define DRIVER_DESC "PS/2 mouse driver"
33
34MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
35MODULE_DESCRIPTION(DRIVER_DESC);
36MODULE_LICENSE("GPL");
37
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050038static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
40static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
42#define param_set_proto_abbrev psmouse_set_maxproto
43#define param_get_proto_abbrev psmouse_get_maxproto
44module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050045MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static unsigned int psmouse_resolution = 200;
48module_param_named(resolution, psmouse_resolution, uint, 0644);
49MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
50
51static unsigned int psmouse_rate = 100;
52module_param_named(rate, psmouse_rate, uint, 0644);
53MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
54
55static unsigned int psmouse_smartscroll = 1;
56module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
57MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
58
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050059static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060module_param_named(resetafter, psmouse_resetafter, uint, 0644);
61MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
62
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050063static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050064module_param_named(resync_time, psmouse_resync_time, uint, 0644);
65MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
66
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050067PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
68 NULL,
69 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
70PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
71 (void *) offsetof(struct psmouse, rate),
72 psmouse_show_int_attr, psmouse_attr_set_rate);
73PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
74 (void *) offsetof(struct psmouse, resolution),
75 psmouse_show_int_attr, psmouse_attr_set_resolution);
76PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
77 (void *) offsetof(struct psmouse, resetafter),
78 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050079PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
80 (void *) offsetof(struct psmouse, resync_time),
81 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050082
83static struct attribute *psmouse_attributes[] = {
84 &psmouse_attr_protocol.dattr.attr,
85 &psmouse_attr_rate.dattr.attr,
86 &psmouse_attr_resolution.dattr.attr,
87 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050088 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050089 NULL
90};
91
92static struct attribute_group psmouse_attribute_group = {
93 .attrs = psmouse_attributes,
94};
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96__obsolete_setup("psmouse_noext");
97__obsolete_setup("psmouse_resolution=");
98__obsolete_setup("psmouse_smartscroll=");
99__obsolete_setup("psmouse_resetafter=");
100__obsolete_setup("psmouse_rate=");
101
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500102/*
Ingo Molnarc14471d2006-02-19 00:22:11 -0500103 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500104 * (connecting, disconnecting, changing rate or resolution via
105 * sysfs). We could use a per-device semaphore but since there
106 * rarely more than one PS/2 mouse connected and since semaphore
107 * is taken in "slow" paths it is not worth it.
108 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500109static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500110
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500111static struct workqueue_struct *kpsmoused_wq;
112
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500113struct psmouse_protocol {
114 enum psmouse_type type;
115 char *name;
116 char *alias;
117 int maxproto;
118 int (*detect)(struct psmouse *, int);
119 int (*init)(struct psmouse *);
120};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/*
123 * psmouse_process_byte() analyzes the PS/2 data stream and reports
124 * relevant events to the input module once full packet has arrived.
125 */
126
127static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
128{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500129 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 unsigned char *packet = psmouse->packet;
131
132 if (psmouse->pktcnt < psmouse->pktsize)
133 return PSMOUSE_GOOD_DATA;
134
135/*
136 * Full packet accumulated, process it
137 */
138
139 input_regs(dev, regs);
140
141/*
142 * Scroll wheel on IntelliMice, scroll buttons on NetMice
143 */
144
145 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
146 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
147
148/*
149 * Scroll wheel and buttons on IntelliMouse Explorer
150 */
151
152 if (psmouse->type == PSMOUSE_IMEX) {
153 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
154 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
155 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
156 }
157
158/*
159 * Extra buttons on Genius NewNet 3D
160 */
161
162 if (psmouse->type == PSMOUSE_GENPS) {
163 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
164 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
165 }
166
167/*
168 * Extra button on ThinkingMouse
169 */
170 if (psmouse->type == PSMOUSE_THINKPS) {
171 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
172 /* Without this bit of weirdness moving up gives wildly high Y changes. */
173 packet[1] |= (packet[0] & 0x40) << 1;
174 }
175
176/*
177 * Generic PS/2 Mouse
178 */
179
180 input_report_key(dev, BTN_LEFT, packet[0] & 1);
181 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
182 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
183
184 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
185 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
186
187 input_sync(dev);
188
189 return PSMOUSE_FULL_PACKET;
190}
191
192/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500193 * __psmouse_set_state() sets new psmouse state and resets all flags.
194 */
195
196static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
197{
198 psmouse->state = new_state;
199 psmouse->pktcnt = psmouse->out_of_sync = 0;
200 psmouse->ps2dev.flags = 0;
201 psmouse->last = jiffies;
202}
203
204
205/*
206 * psmouse_set_state() sets new psmouse state and resets all flags and
207 * counters while holding serio lock so fighting with interrupt handler
208 * is not a concern.
209 */
210
211static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
212{
213 serio_pause_rx(psmouse->ps2dev.serio);
214 __psmouse_set_state(psmouse, new_state);
215 serio_continue_rx(psmouse->ps2dev.serio);
216}
217
218/*
219 * psmouse_handle_byte() processes one byte of the input data stream
220 * by calling corresponding protocol handler.
221 */
222
223static int psmouse_handle_byte(struct psmouse *psmouse, struct pt_regs *regs)
224{
225 psmouse_ret_t rc = psmouse->protocol_handler(psmouse, regs);
226
227 switch (rc) {
228 case PSMOUSE_BAD_DATA:
229 if (psmouse->state == PSMOUSE_ACTIVATED) {
230 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
231 psmouse->name, psmouse->phys, psmouse->pktcnt);
232 if (++psmouse->out_of_sync == psmouse->resetafter) {
233 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
234 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
235 serio_reconnect(psmouse->ps2dev.serio);
236 return -1;
237 }
238 }
239 psmouse->pktcnt = 0;
240 break;
241
242 case PSMOUSE_FULL_PACKET:
243 psmouse->pktcnt = 0;
244 if (psmouse->out_of_sync) {
245 psmouse->out_of_sync = 0;
246 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
247 psmouse->name, psmouse->phys);
248 }
249 break;
250
251 case PSMOUSE_GOOD_DATA:
252 break;
253 }
254 return 0;
255}
256
257/*
258 * psmouse_interrupt() handles incoming characters, either passing them
259 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
261
262static irqreturn_t psmouse_interrupt(struct serio *serio,
263 unsigned char data, unsigned int flags, struct pt_regs *regs)
264{
265 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if (psmouse->state == PSMOUSE_IGNORE)
268 goto out;
269
270 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
271 if (psmouse->state == PSMOUSE_ACTIVATED)
272 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
273 flags & SERIO_TIMEOUT ? " timeout" : "",
274 flags & SERIO_PARITY ? " bad parity" : "");
275 ps2_cmd_aborted(&psmouse->ps2dev);
276 goto out;
277 }
278
279 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
280 if (ps2_handle_ack(&psmouse->ps2dev, data))
281 goto out;
282
283 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
284 if (ps2_handle_response(&psmouse->ps2dev, data))
285 goto out;
286
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500287 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 goto out;
289
290 if (psmouse->state == PSMOUSE_ACTIVATED &&
291 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500292 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500294 psmouse->badbyte = psmouse->packet[0];
295 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
296 queue_work(kpsmoused_wq, &psmouse->resync_work);
297 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500301/*
302 * Check if this is a new device announcement (0xAA 0x00)
303 */
304 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400305 if (psmouse->pktcnt == 1) {
306 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500310 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
311 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
312 serio_reconnect(serio);
313 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500315/*
316 * Not a new device, try processing first byte normally
317 */
318 psmouse->pktcnt = 1;
319 if (psmouse_handle_byte(psmouse, regs))
320 goto out;
321
322 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500325/*
326 * See if we need to force resync because mouse was idle for too long
327 */
328 if (psmouse->state == PSMOUSE_ACTIVATED &&
329 psmouse->pktcnt == 1 && psmouse->resync_time &&
330 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
331 psmouse->badbyte = psmouse->packet[0];
332 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
333 queue_work(kpsmoused_wq, &psmouse->resync_work);
334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500336
337 psmouse->last = jiffies;
338 psmouse_handle_byte(psmouse, regs);
339
340 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return IRQ_HANDLED;
342}
343
344
345/*
346 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
347 * using sliced syntax, understood by advanced devices, such as Logitech
348 * or Synaptics touchpads. The command is encoded as:
349 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
350 * is the command.
351 */
352int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
353{
354 int i;
355
356 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
357 return -1;
358
359 for (i = 6; i >= 0; i -= 2) {
360 unsigned char d = (command >> i) & 3;
361 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
362 return -1;
363 }
364
365 return 0;
366}
367
368
369/*
370 * psmouse_reset() resets the mouse into power-on state.
371 */
372int psmouse_reset(struct psmouse *psmouse)
373{
374 unsigned char param[2];
375
376 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
377 return -1;
378
379 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
380 return -1;
381
382 return 0;
383}
384
385
386/*
387 * Genius NetMouse magic init.
388 */
389static int genius_detect(struct psmouse *psmouse, int set_properties)
390{
391 struct ps2dev *ps2dev = &psmouse->ps2dev;
392 unsigned char param[4];
393
394 param[0] = 3;
395 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
396 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
397 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
398 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
399 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
400
401 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
402 return -1;
403
404 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500405 set_bit(BTN_EXTRA, psmouse->dev->keybit);
406 set_bit(BTN_SIDE, psmouse->dev->keybit);
407 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500410 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 psmouse->pktsize = 4;
412 }
413
414 return 0;
415}
416
417/*
418 * IntelliMouse magic init.
419 */
420static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
421{
422 struct ps2dev *ps2dev = &psmouse->ps2dev;
423 unsigned char param[2];
424
425 param[0] = 200;
426 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
427 param[0] = 100;
428 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
429 param[0] = 80;
430 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
431 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
432
433 if (param[0] != 3)
434 return -1;
435
436 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500437 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
438 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (!psmouse->vendor) psmouse->vendor = "Generic";
441 if (!psmouse->name) psmouse->name = "Wheel Mouse";
442 psmouse->pktsize = 4;
443 }
444
445 return 0;
446}
447
448/*
449 * Try IntelliMouse/Explorer magic init.
450 */
451static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
452{
453 struct ps2dev *ps2dev = &psmouse->ps2dev;
454 unsigned char param[2];
455
456 intellimouse_detect(psmouse, 0);
457
458 param[0] = 200;
459 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
460 param[0] = 200;
461 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
462 param[0] = 80;
463 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
464 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
465
466 if (param[0] != 4)
467 return -1;
468
469 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500470 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
471 set_bit(REL_WHEEL, psmouse->dev->relbit);
472 set_bit(BTN_SIDE, psmouse->dev->keybit);
473 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if (!psmouse->vendor) psmouse->vendor = "Generic";
476 if (!psmouse->name) psmouse->name = "Explorer Mouse";
477 psmouse->pktsize = 4;
478 }
479
480 return 0;
481}
482
483/*
484 * Kensington ThinkingMouse / ExpertMouse magic init.
485 */
486static int thinking_detect(struct psmouse *psmouse, int set_properties)
487{
488 struct ps2dev *ps2dev = &psmouse->ps2dev;
489 unsigned char param[2];
490 unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20, 0 };
491 int i;
492
493 param[0] = 10;
494 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
495 param[0] = 0;
496 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
497 for (i = 0; seq[i]; i++)
498 ps2_command(ps2dev, seq + i, PSMOUSE_CMD_SETRATE);
499 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
500
501 if (param[0] != 2)
502 return -1;
503
504 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500505 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 psmouse->vendor = "Kensington";
508 psmouse->name = "ThinkingMouse";
509 }
510
511 return 0;
512}
513
514/*
515 * Bare PS/2 protocol "detection". Always succeeds.
516 */
517static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
518{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500519 if (set_properties) {
520 if (!psmouse->vendor) psmouse->vendor = "Generic";
521 if (!psmouse->name) psmouse->name = "Mouse";
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 return 0;
525}
526
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/*
529 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
530 * the mouse may have.
531 */
532
533static int psmouse_extensions(struct psmouse *psmouse,
534 unsigned int max_proto, int set_properties)
535{
536 int synaptics_hardware = 0;
537
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500538/*
539 * We always check for lifebook because it does not disturb mouse
540 * (it only checks DMI information).
541 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500542 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500543 if (max_proto > PSMOUSE_IMEX) {
544 if (!set_properties || lifebook_init(psmouse) == 0)
545 return PSMOUSE_LIFEBOOK;
546 }
547 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/*
550 * Try Kensington ThinkingMouse (we try first, because synaptics probe
551 * upsets the thinkingmouse).
552 */
553
554 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
555 return PSMOUSE_THINKPS;
556
557/*
558 * Try Synaptics TouchPad
559 */
560 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
561 synaptics_hardware = 1;
562
563 if (max_proto > PSMOUSE_IMEX) {
564 if (!set_properties || synaptics_init(psmouse) == 0)
565 return PSMOUSE_SYNAPTICS;
566/*
567 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
568 * Unfortunately Logitech/Genius probes confuse some firmware versions so
569 * we'll have to skip them.
570 */
571 max_proto = PSMOUSE_IMEX;
572 }
573/*
574 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
575 */
576 synaptics_reset(psmouse);
577 }
578
579/*
580 * Try ALPS TouchPad
581 */
582 if (max_proto > PSMOUSE_IMEX) {
583 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
584 if (alps_detect(psmouse, set_properties) == 0) {
585 if (!set_properties || alps_init(psmouse) == 0)
586 return PSMOUSE_ALPS;
587/*
588 * Init failed, try basic relative protocols
589 */
590 max_proto = PSMOUSE_IMEX;
591 }
592 }
593
594 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
595 return PSMOUSE_GENPS;
596
597 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
598 return PSMOUSE_PS2PP;
599
Dmitry Torokhovba449952005-12-21 00:51:31 -0500600 if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
601 return PSMOUSE_TRACKPOINT;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/*
604 * Reset to defaults in case the device got confused by extended
Dmitry Torokhovba449952005-12-21 00:51:31 -0500605 * protocol probes. Note that we do full reset becuase some mice
606 * put themselves to sleep when see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Dmitry Torokhovba449952005-12-21 00:51:31 -0500608 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
611 return PSMOUSE_IMEX;
612
613 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
614 return PSMOUSE_IMPS;
615
616/*
617 * Okay, all failed, we have a standard mouse here. The number of the buttons
618 * is still a question, though. We assume 3.
619 */
620 ps2bare_detect(psmouse, set_properties);
621
622 if (synaptics_hardware) {
623/*
624 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
625 * We need to reset the touchpad because if there is a track point on the
626 * pass through port it could get disabled while probing for protocol
627 * extensions.
628 */
629 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
632 return PSMOUSE_PS2;
633}
634
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500635static struct psmouse_protocol psmouse_protocols[] = {
636 {
637 .type = PSMOUSE_PS2,
638 .name = "PS/2",
639 .alias = "bare",
640 .maxproto = 1,
641 .detect = ps2bare_detect,
642 },
643 {
644 .type = PSMOUSE_PS2PP,
645 .name = "PS2++",
646 .alias = "logitech",
647 .detect = ps2pp_init,
648 },
649 {
650 .type = PSMOUSE_THINKPS,
651 .name = "ThinkPS/2",
652 .alias = "thinkps",
653 .detect = thinking_detect,
654 },
655 {
656 .type = PSMOUSE_GENPS,
657 .name = "GenPS/2",
658 .alias = "genius",
659 .detect = genius_detect,
660 },
661 {
662 .type = PSMOUSE_IMPS,
663 .name = "ImPS/2",
664 .alias = "imps",
665 .maxproto = 1,
666 .detect = intellimouse_detect,
667 },
668 {
669 .type = PSMOUSE_IMEX,
670 .name = "ImExPS/2",
671 .alias = "exps",
672 .maxproto = 1,
673 .detect = im_explorer_detect,
674 },
675 {
676 .type = PSMOUSE_SYNAPTICS,
677 .name = "SynPS/2",
678 .alias = "synaptics",
679 .detect = synaptics_detect,
680 .init = synaptics_init,
681 },
682 {
683 .type = PSMOUSE_ALPS,
684 .name = "AlpsPS/2",
685 .alias = "alps",
686 .detect = alps_detect,
687 .init = alps_init,
688 },
689 {
690 .type = PSMOUSE_LIFEBOOK,
691 .name = "LBPS/2",
692 .alias = "lifebook",
693 .init = lifebook_init,
694 },
695 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500696 .type = PSMOUSE_TRACKPOINT,
697 .name = "TPPS/2",
698 .alias = "trackpoint",
699 .detect = trackpoint_detect,
700 },
701 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500702 .type = PSMOUSE_AUTO,
703 .name = "auto",
704 .alias = "any",
705 .maxproto = 1,
706 },
707};
708
709static struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
710{
711 int i;
712
713 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
714 if (psmouse_protocols[i].type == type)
715 return &psmouse_protocols[i];
716
717 WARN_ON(1);
718 return &psmouse_protocols[0];
719}
720
721static struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
722{
723 struct psmouse_protocol *p;
724 int i;
725
726 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
727 p = &psmouse_protocols[i];
728
729 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
730 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
731 return &psmouse_protocols[i];
732 }
733
734 return NULL;
735}
736
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/*
739 * psmouse_probe() probes for a PS/2 mouse.
740 */
741
742static int psmouse_probe(struct psmouse *psmouse)
743{
744 struct ps2dev *ps2dev = &psmouse->ps2dev;
745 unsigned char param[2];
746
747/*
748 * First, we check if it's a mouse. It should send 0x00 or 0x03
749 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500750 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
751 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
753
754 param[0] = 0xa5;
755 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
756 return -1;
757
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500758 if (param[0] != 0x00 && param[0] != 0x03 &&
759 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -1;
761
762/*
763 * Then we reset and disable the mouse so that it doesn't generate events.
764 */
765
766 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
767 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
768
769 return 0;
770}
771
772/*
773 * Here we set the mouse resolution.
774 */
775
776void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
777{
778 unsigned char params[] = { 0, 1, 2, 2, 3 };
779
780 if (resolution == 0 || resolution > 200)
781 resolution = 200;
782
783 ps2_command(&psmouse->ps2dev, &params[resolution / 50], PSMOUSE_CMD_SETRES);
784 psmouse->resolution = 25 << params[resolution / 50];
785}
786
787/*
788 * Here we set the mouse report rate.
789 */
790
791static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
792{
793 unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
794 int i = 0;
795
796 while (rates[i] > rate) i++;
797 ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE);
798 psmouse->rate = rates[i];
799}
800
801/*
802 * psmouse_initialize() initializes the mouse to a sane state.
803 */
804
805static void psmouse_initialize(struct psmouse *psmouse)
806{
807/*
808 * We set the mouse into streaming mode.
809 */
810
811 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
812
813/*
814 * We set the mouse report rate, resolution and scaling.
815 */
816
817 if (psmouse_max_proto != PSMOUSE_PS2) {
818 psmouse->set_rate(psmouse, psmouse->rate);
819 psmouse->set_resolution(psmouse, psmouse->resolution);
820 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
821 }
822}
823
824/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * psmouse_activate() enables the mouse so that we get motion reports from it.
826 */
827
828static void psmouse_activate(struct psmouse *psmouse)
829{
830 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
831 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
832 psmouse->ps2dev.serio->phys);
833
834 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
835}
836
837
838/*
839 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
840 * reports from it unless we explicitely request it.
841 */
842
843static void psmouse_deactivate(struct psmouse *psmouse)
844{
845 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
846 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
847 psmouse->ps2dev.serio->phys);
848
849 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
850}
851
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500852/*
853 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
854 */
855
856static int psmouse_poll(struct psmouse *psmouse)
857{
858 return ps2_command(&psmouse->ps2dev, psmouse->packet,
859 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
860}
861
862
863/*
864 * psmouse_resync() attempts to re-validate current protocol.
865 */
866
867static void psmouse_resync(void *p)
868{
869 struct psmouse *psmouse = p, *parent = NULL;
870 struct serio *serio = psmouse->ps2dev.serio;
871 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
872 int failed = 0, enabled = 0;
873 int i;
874
Ingo Molnarc14471d2006-02-19 00:22:11 -0500875 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500876
877 if (psmouse->state != PSMOUSE_RESYNCING)
878 goto out;
879
880 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
881 parent = serio_get_drvdata(serio->parent);
882 psmouse_deactivate(parent);
883 }
884
885/*
886 * Some mice don't ACK commands sent while they are in the middle of
887 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
888 * instead of ps2_command() which would wait for 200ms for an ACK
889 * that may never come.
890 * As an additional quirk ALPS touchpads may not only forget to ACK
891 * disable command but will stop reporting taps, so if we see that
892 * mouse at least once ACKs disable we will do full reconnect if ACK
893 * is missing.
894 */
895 psmouse->num_resyncs++;
896
897 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
898 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
899 failed = 1;
900 } else
901 psmouse->acks_disable_command = 1;
902
903/*
904 * Poll the mouse. If it was reset the packet will be shorter than
905 * psmouse->pktsize and ps2_command will fail. We do not expect and
906 * do not handle scenario when mouse "upgrades" its protocol while
907 * disconnected since it would require additional delay. If we ever
908 * see a mouse that does it we'll adjust the code.
909 */
910 if (!failed) {
911 if (psmouse->poll(psmouse))
912 failed = 1;
913 else {
914 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
915 for (i = 0; i < psmouse->pktsize; i++) {
916 psmouse->pktcnt++;
917 rc = psmouse->protocol_handler(psmouse, NULL);
918 if (rc != PSMOUSE_GOOD_DATA)
919 break;
920 }
921 if (rc != PSMOUSE_FULL_PACKET)
922 failed = 1;
923 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
924 }
925 }
926/*
927 * Now try to enable mouse. We try to do that even if poll failed and also
928 * repeat our attempts 5 times, otherwise we may be left out with disabled
929 * mouse.
930 */
931 for (i = 0; i < 5; i++) {
932 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
933 enabled = 1;
934 break;
935 }
936 msleep(200);
937 }
938
939 if (!enabled) {
940 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
941 psmouse->ps2dev.serio->phys);
942 failed = 1;
943 }
944
945 if (failed) {
946 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
947 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
948 serio_reconnect(serio);
949 } else
950 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
951
952 if (parent)
953 psmouse_activate(parent);
954 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -0500955 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500956}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958/*
959 * psmouse_cleanup() resets the mouse into power-on state.
960 */
961
962static void psmouse_cleanup(struct serio *serio)
963{
964 struct psmouse *psmouse = serio_get_drvdata(serio);
965
966 psmouse_reset(psmouse);
967}
968
969/*
970 * psmouse_disconnect() closes and frees.
971 */
972
973static void psmouse_disconnect(struct serio *serio)
974{
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500975 struct psmouse *psmouse, *parent = NULL;
976
977 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -0500979 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Ingo Molnarc14471d2006-02-19 00:22:11 -0500981 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
984
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500985 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500986 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500987 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -0500988 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
991 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500992 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
995 if (psmouse->disconnect)
996 psmouse->disconnect(psmouse);
997
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500998 if (parent && parent->pt_deactivate)
999 parent->pt_deactivate(parent);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 serio_close(serio);
1004 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001005 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001007
1008 if (parent)
1009 psmouse_activate(parent);
1010
Ingo Molnarc14471d2006-02-19 00:22:11 -05001011 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001014static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_protocol *proto)
1015{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001016 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001017
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001018 input_dev->private = psmouse;
1019 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001020
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001021 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1022 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1023 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001024
1025 psmouse->set_rate = psmouse_set_rate;
1026 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001027 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001028 psmouse->protocol_handler = psmouse_process_byte;
1029 psmouse->pktsize = 3;
1030
1031 if (proto && (proto->detect || proto->init)) {
1032 if (proto->detect && proto->detect(psmouse, 1) < 0)
1033 return -1;
1034
1035 if (proto->init && proto->init(psmouse) < 0)
1036 return -1;
1037
1038 psmouse->type = proto->type;
1039 }
1040 else
1041 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1042
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001043 /*
1044 * If mouse's packet size is 3 there is no point in polling the
1045 * device in hopes to detect protocol reset - we won't get less
1046 * than 3 bytes response anyhow.
1047 */
1048 if (psmouse->pktsize == 3)
1049 psmouse->resync_time = 0;
1050
1051 /*
1052 * Some smart KVMs fake response to POLL command returning just
1053 * 3 bytes and messing up our resync logic, so if initial poll
1054 * fails we won't try polling the device anymore. Hopefully
1055 * such KVM will maintain initially selected protocol.
1056 */
1057 if (psmouse->resync_time && psmouse->poll(psmouse))
1058 psmouse->resync_time = 0;
1059
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001060 sprintf(psmouse->devname, "%s %s %s",
1061 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1062
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001063 input_dev->name = psmouse->devname;
1064 input_dev->phys = psmouse->phys;
1065 input_dev->id.bustype = BUS_I8042;
1066 input_dev->id.vendor = 0x0002;
1067 input_dev->id.product = psmouse->type;
1068 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001069
1070 return 0;
1071}
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073/*
1074 * psmouse_connect() is a callback from the serio module when
1075 * an unhandled serio port is found.
1076 */
1077static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1078{
1079 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001080 struct input_dev *input_dev;
1081 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Ingo Molnarc14471d2006-02-19 00:22:11 -05001083 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /*
1086 * If this is a pass-through port deactivate parent so the device
1087 * connected to this port can be successfully identified
1088 */
1089 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1090 parent = serio_get_drvdata(serio->parent);
1091 psmouse_deactivate(parent);
1092 }
1093
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001094 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1095 input_dev = input_allocate_device();
1096 if (!psmouse || !input_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 ps2_init(&psmouse->ps2dev, serio);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001100 INIT_WORK(&psmouse->resync_work, psmouse_resync, psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001101 psmouse->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 sprintf(psmouse->phys, "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1105
1106 serio_set_drvdata(serio, psmouse);
1107
1108 retval = serio_open(serio, drv);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001109 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (psmouse_probe(psmouse) < 0) {
1113 serio_close(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 retval = -ENODEV;
1115 goto out;
1116 }
1117
1118 psmouse->rate = psmouse_rate;
1119 psmouse->resolution = psmouse_resolution;
1120 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001121 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001124 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 psmouse_initialize(psmouse);
1128
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001129 input_register_device(psmouse->dev);
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (parent && parent->pt_activate)
1132 parent->pt_activate(parent);
1133
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001134 sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 psmouse_activate(psmouse);
1137
1138 retval = 0;
1139
1140out:
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001141 if (retval) {
1142 serio_set_drvdata(serio, NULL);
1143 input_free_device(input_dev);
1144 kfree(psmouse);
1145 }
1146
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001147 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (parent)
1149 psmouse_activate(parent);
1150
Ingo Molnarc14471d2006-02-19 00:22:11 -05001151 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return retval;
1153}
1154
1155
1156static int psmouse_reconnect(struct serio *serio)
1157{
1158 struct psmouse *psmouse = serio_get_drvdata(serio);
1159 struct psmouse *parent = NULL;
1160 struct serio_driver *drv = serio->drv;
1161 int rc = -1;
1162
1163 if (!drv || !psmouse) {
1164 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1165 return -1;
1166 }
1167
Ingo Molnarc14471d2006-02-19 00:22:11 -05001168 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1171 parent = serio_get_drvdata(serio->parent);
1172 psmouse_deactivate(parent);
1173 }
1174
1175 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1176
1177 if (psmouse->reconnect) {
1178 if (psmouse->reconnect(psmouse))
1179 goto out;
1180 } else if (psmouse_probe(psmouse) < 0 ||
1181 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1182 goto out;
1183
1184 /* ok, the device type (and capabilities) match the old one,
1185 * we can continue using it, complete intialization
1186 */
1187 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1188
1189 psmouse_initialize(psmouse);
1190
1191 if (parent && parent->pt_activate)
1192 parent->pt_activate(parent);
1193
1194 psmouse_activate(psmouse);
1195 rc = 0;
1196
1197out:
1198 /* If this is a pass-through port the parent waits to be activated */
1199 if (parent)
1200 psmouse_activate(parent);
1201
Ingo Molnarc14471d2006-02-19 00:22:11 -05001202 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return rc;
1204}
1205
1206static struct serio_device_id psmouse_serio_ids[] = {
1207 {
1208 .type = SERIO_8042,
1209 .proto = SERIO_ANY,
1210 .id = SERIO_ANY,
1211 .extra = SERIO_ANY,
1212 },
1213 {
1214 .type = SERIO_PS_PSTHRU,
1215 .proto = SERIO_ANY,
1216 .id = SERIO_ANY,
1217 .extra = SERIO_ANY,
1218 },
1219 { 0 }
1220};
1221
1222MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1223
1224static struct serio_driver psmouse_drv = {
1225 .driver = {
1226 .name = "psmouse",
1227 },
1228 .description = DRIVER_DESC,
1229 .id_table = psmouse_serio_ids,
1230 .interrupt = psmouse_interrupt,
1231 .connect = psmouse_connect,
1232 .reconnect = psmouse_reconnect,
1233 .disconnect = psmouse_disconnect,
1234 .cleanup = psmouse_cleanup,
1235};
1236
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001237ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1238 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001241 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1242 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int retval;
1244
1245 retval = serio_pin_driver(serio);
1246 if (retval)
1247 return retval;
1248
1249 if (serio->drv != &psmouse_drv) {
1250 retval = -ENODEV;
1251 goto out;
1252 }
1253
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001254 psmouse = serio_get_drvdata(serio);
1255
1256 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258out:
1259 serio_unpin_driver(serio);
1260 return retval;
1261}
1262
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001263ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1264 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001267 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1268 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int retval;
1270
1271 retval = serio_pin_driver(serio);
1272 if (retval)
1273 return retval;
1274
1275 if (serio->drv != &psmouse_drv) {
1276 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001277 goto out_unpin;
1278 }
1279
Ingo Molnarc14471d2006-02-19 00:22:11 -05001280 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001281 if (retval)
1282 goto out_unpin;
1283
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001284 psmouse = serio_get_drvdata(serio);
1285
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001286 if (psmouse->state == PSMOUSE_IGNORE) {
1287 retval = -ENODEV;
Ingo Molnarc14471d2006-02-19 00:22:11 -05001288 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
1291 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1292 parent = serio_get_drvdata(serio->parent);
1293 psmouse_deactivate(parent);
1294 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 psmouse_deactivate(psmouse);
1297
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001298 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001300 if (retval != -ENODEV)
1301 psmouse_activate(psmouse);
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (parent)
1304 psmouse_activate(parent);
1305
Ingo Molnarc14471d2006-02-19 00:22:11 -05001306 out_unlock:
1307 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001308 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 serio_unpin_driver(serio);
1310 return retval;
1311}
1312
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001313static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1314{
1315 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1316
1317 return sprintf(buf, "%lu\n", *field);
1318}
1319
1320static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1321{
1322 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1323 unsigned long value;
1324 char *rest;
1325
1326 value = simple_strtoul(buf, &rest, 10);
1327 if (*rest)
1328 return -EINVAL;
1329
1330 *field = value;
1331
1332 return count;
1333}
1334
1335static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001336{
1337 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1338}
1339
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001340static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001341{
1342 struct serio *serio = psmouse->ps2dev.serio;
1343 struct psmouse *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001344 struct input_dev *new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001345 struct psmouse_protocol *proto;
1346 int retry = 0;
1347
1348 if (!(proto = psmouse_protocol_by_name(buf, count)))
1349 return -EINVAL;
1350
1351 if (psmouse->type == proto->type)
1352 return count;
1353
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001354 if (!(new_dev = input_allocate_device()))
1355 return -ENOMEM;
1356
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001357 while (serio->child) {
1358 if (++retry > 3) {
1359 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001360 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001361 return -EIO;
1362 }
1363
Ingo Molnarc14471d2006-02-19 00:22:11 -05001364 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001365 serio_unpin_driver(serio);
1366 serio_unregister_child_port(serio);
1367 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001368 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001369
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001370 if (serio->drv != &psmouse_drv) {
1371 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001372 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001373 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001374
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001375 if (psmouse->type == proto->type) {
1376 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001377 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001378 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001379 }
1380
1381 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1382 parent = serio_get_drvdata(serio->parent);
1383 if (parent->pt_deactivate)
1384 parent->pt_deactivate(parent);
1385 }
1386
1387 if (psmouse->disconnect)
1388 psmouse->disconnect(psmouse);
1389
1390 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001391 input_unregister_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001392
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001393 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001394 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1395
1396 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1397 psmouse_reset(psmouse);
1398 /* default to PSMOUSE_PS2 */
1399 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1400 }
1401
1402 psmouse_initialize(psmouse);
1403 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1404
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001405 input_register_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001406
1407 if (parent && parent->pt_activate)
1408 parent->pt_activate(parent);
1409
1410 return count;
1411}
1412
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001413static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 unsigned long value;
1416 char *rest;
1417
1418 value = simple_strtoul(buf, &rest, 10);
1419 if (*rest)
1420 return -EINVAL;
1421
1422 psmouse->set_rate(psmouse, value);
1423 return count;
1424}
1425
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001426static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 unsigned long value;
1429 char *rest;
1430
1431 value = simple_strtoul(buf, &rest, 10);
1432 if (*rest)
1433 return -EINVAL;
1434
1435 psmouse->set_resolution(psmouse, value);
1436 return count;
1437}
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1441{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001442 struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 if (!val)
1445 return -EINVAL;
1446
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001447 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001449 if (!proto || !proto->maxproto)
1450 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001452 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Stephen Evanchik541e3162005-08-08 01:26:18 -05001454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
1457static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1458{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001459 int type = *((unsigned int *)kp->arg);
1460
1461 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464static int __init psmouse_init(void)
1465{
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001466 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1467 if (!kpsmoused_wq) {
1468 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1469 return -ENOMEM;
1470 }
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 serio_register_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 0;
1475}
1476
1477static void __exit psmouse_exit(void)
1478{
1479 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001480 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483module_init(psmouse_init);
1484module_exit(psmouse_exit);