blob: ec0119459bc636ab5c8abb7cba28ac6618ef15a8 [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;
Helge Dellere38de672006-09-10 21:54:39 -0400115 const char *name;
116 const char *alias;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500117 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) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400153 switch (packet[3] & 0xC0) {
154 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
155 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
156 break;
157 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
158 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
159 break;
160 case 0x00:
161 case 0xC0:
162 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
163 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
164 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
165 break;
166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
169/*
170 * Extra buttons on Genius NewNet 3D
171 */
172
173 if (psmouse->type == PSMOUSE_GENPS) {
174 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
175 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
176 }
177
178/*
179 * Extra button on ThinkingMouse
180 */
181 if (psmouse->type == PSMOUSE_THINKPS) {
182 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
183 /* Without this bit of weirdness moving up gives wildly high Y changes. */
184 packet[1] |= (packet[0] & 0x40) << 1;
185 }
186
187/*
188 * Generic PS/2 Mouse
189 */
190
191 input_report_key(dev, BTN_LEFT, packet[0] & 1);
192 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
193 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
194
195 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
196 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
197
198 input_sync(dev);
199
200 return PSMOUSE_FULL_PACKET;
201}
202
203/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500204 * __psmouse_set_state() sets new psmouse state and resets all flags.
205 */
206
207static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
208{
209 psmouse->state = new_state;
210 psmouse->pktcnt = psmouse->out_of_sync = 0;
211 psmouse->ps2dev.flags = 0;
212 psmouse->last = jiffies;
213}
214
215
216/*
217 * psmouse_set_state() sets new psmouse state and resets all flags and
218 * counters while holding serio lock so fighting with interrupt handler
219 * is not a concern.
220 */
221
222static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
223{
224 serio_pause_rx(psmouse->ps2dev.serio);
225 __psmouse_set_state(psmouse, new_state);
226 serio_continue_rx(psmouse->ps2dev.serio);
227}
228
229/*
230 * psmouse_handle_byte() processes one byte of the input data stream
231 * by calling corresponding protocol handler.
232 */
233
234static int psmouse_handle_byte(struct psmouse *psmouse, struct pt_regs *regs)
235{
236 psmouse_ret_t rc = psmouse->protocol_handler(psmouse, regs);
237
238 switch (rc) {
239 case PSMOUSE_BAD_DATA:
240 if (psmouse->state == PSMOUSE_ACTIVATED) {
241 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
242 psmouse->name, psmouse->phys, psmouse->pktcnt);
243 if (++psmouse->out_of_sync == psmouse->resetafter) {
244 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
245 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
246 serio_reconnect(psmouse->ps2dev.serio);
247 return -1;
248 }
249 }
250 psmouse->pktcnt = 0;
251 break;
252
253 case PSMOUSE_FULL_PACKET:
254 psmouse->pktcnt = 0;
255 if (psmouse->out_of_sync) {
256 psmouse->out_of_sync = 0;
257 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
258 psmouse->name, psmouse->phys);
259 }
260 break;
261
262 case PSMOUSE_GOOD_DATA:
263 break;
264 }
265 return 0;
266}
267
268/*
269 * psmouse_interrupt() handles incoming characters, either passing them
270 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
272
273static irqreturn_t psmouse_interrupt(struct serio *serio,
274 unsigned char data, unsigned int flags, struct pt_regs *regs)
275{
276 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if (psmouse->state == PSMOUSE_IGNORE)
279 goto out;
280
281 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
282 if (psmouse->state == PSMOUSE_ACTIVATED)
283 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
284 flags & SERIO_TIMEOUT ? " timeout" : "",
285 flags & SERIO_PARITY ? " bad parity" : "");
286 ps2_cmd_aborted(&psmouse->ps2dev);
287 goto out;
288 }
289
290 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
291 if (ps2_handle_ack(&psmouse->ps2dev, data))
292 goto out;
293
294 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
295 if (ps2_handle_response(&psmouse->ps2dev, data))
296 goto out;
297
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500298 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto out;
300
301 if (psmouse->state == PSMOUSE_ACTIVATED &&
302 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500303 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500305 psmouse->badbyte = psmouse->packet[0];
306 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
307 queue_work(kpsmoused_wq, &psmouse->resync_work);
308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500312/*
313 * Check if this is a new device announcement (0xAA 0x00)
314 */
315 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400316 if (psmouse->pktcnt == 1) {
317 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500321 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
322 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
323 serio_reconnect(serio);
324 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500326/*
327 * Not a new device, try processing first byte normally
328 */
329 psmouse->pktcnt = 1;
330 if (psmouse_handle_byte(psmouse, regs))
331 goto out;
332
333 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500336/*
337 * See if we need to force resync because mouse was idle for too long
338 */
339 if (psmouse->state == PSMOUSE_ACTIVATED &&
340 psmouse->pktcnt == 1 && psmouse->resync_time &&
341 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
342 psmouse->badbyte = psmouse->packet[0];
343 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
344 queue_work(kpsmoused_wq, &psmouse->resync_work);
345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500347
348 psmouse->last = jiffies;
349 psmouse_handle_byte(psmouse, regs);
350
351 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return IRQ_HANDLED;
353}
354
355
356/*
357 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
358 * using sliced syntax, understood by advanced devices, such as Logitech
359 * or Synaptics touchpads. The command is encoded as:
360 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
361 * is the command.
362 */
363int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
364{
365 int i;
366
367 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
368 return -1;
369
370 for (i = 6; i >= 0; i -= 2) {
371 unsigned char d = (command >> i) & 3;
372 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
373 return -1;
374 }
375
376 return 0;
377}
378
379
380/*
381 * psmouse_reset() resets the mouse into power-on state.
382 */
383int psmouse_reset(struct psmouse *psmouse)
384{
385 unsigned char param[2];
386
387 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
388 return -1;
389
390 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
391 return -1;
392
393 return 0;
394}
395
396
397/*
398 * Genius NetMouse magic init.
399 */
400static int genius_detect(struct psmouse *psmouse, int set_properties)
401{
402 struct ps2dev *ps2dev = &psmouse->ps2dev;
403 unsigned char param[4];
404
405 param[0] = 3;
406 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
407 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
408 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
409 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
410 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
411
412 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
413 return -1;
414
415 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500416 set_bit(BTN_EXTRA, psmouse->dev->keybit);
417 set_bit(BTN_SIDE, psmouse->dev->keybit);
418 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500421 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 psmouse->pktsize = 4;
423 }
424
425 return 0;
426}
427
428/*
429 * IntelliMouse magic init.
430 */
431static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
432{
433 struct ps2dev *ps2dev = &psmouse->ps2dev;
434 unsigned char param[2];
435
436 param[0] = 200;
437 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
438 param[0] = 100;
439 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
440 param[0] = 80;
441 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
442 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
443
444 if (param[0] != 3)
445 return -1;
446
447 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500448 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
449 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (!psmouse->vendor) psmouse->vendor = "Generic";
452 if (!psmouse->name) psmouse->name = "Wheel Mouse";
453 psmouse->pktsize = 4;
454 }
455
456 return 0;
457}
458
459/*
460 * Try IntelliMouse/Explorer magic init.
461 */
462static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
463{
464 struct ps2dev *ps2dev = &psmouse->ps2dev;
465 unsigned char param[2];
466
467 intellimouse_detect(psmouse, 0);
468
469 param[0] = 200;
470 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
471 param[0] = 200;
472 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
473 param[0] = 80;
474 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
475 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
476
477 if (param[0] != 4)
478 return -1;
479
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400480/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
481 param[0] = 200;
482 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
483 param[0] = 80;
484 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
485 param[0] = 40;
486 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
487
488 param[0] = 200;
489 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
490 param[0] = 200;
491 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
492 param[0] = 60;
493 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500496 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
497 set_bit(REL_WHEEL, psmouse->dev->relbit);
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400498 set_bit(REL_HWHEEL, psmouse->dev->relbit);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500499 set_bit(BTN_SIDE, psmouse->dev->keybit);
500 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (!psmouse->vendor) psmouse->vendor = "Generic";
503 if (!psmouse->name) psmouse->name = "Explorer Mouse";
504 psmouse->pktsize = 4;
505 }
506
507 return 0;
508}
509
510/*
511 * Kensington ThinkingMouse / ExpertMouse magic init.
512 */
513static int thinking_detect(struct psmouse *psmouse, int set_properties)
514{
515 struct ps2dev *ps2dev = &psmouse->ps2dev;
516 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400517 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int i;
519
520 param[0] = 10;
521 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
522 param[0] = 0;
523 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400524 for (i = 0; i < ARRAY_SIZE(seq); i++) {
525 param[0] = seq[i];
526 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
529
530 if (param[0] != 2)
531 return -1;
532
533 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500534 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 psmouse->vendor = "Kensington";
537 psmouse->name = "ThinkingMouse";
538 }
539
540 return 0;
541}
542
543/*
544 * Bare PS/2 protocol "detection". Always succeeds.
545 */
546static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
547{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500548 if (set_properties) {
549 if (!psmouse->vendor) psmouse->vendor = "Generic";
550 if (!psmouse->name) psmouse->name = "Mouse";
551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 return 0;
554}
555
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/*
558 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
559 * the mouse may have.
560 */
561
562static int psmouse_extensions(struct psmouse *psmouse,
563 unsigned int max_proto, int set_properties)
564{
565 int synaptics_hardware = 0;
566
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500567/*
568 * We always check for lifebook because it does not disturb mouse
569 * (it only checks DMI information).
570 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500571 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500572 if (max_proto > PSMOUSE_IMEX) {
573 if (!set_properties || lifebook_init(psmouse) == 0)
574 return PSMOUSE_LIFEBOOK;
575 }
576 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
579 * Try Kensington ThinkingMouse (we try first, because synaptics probe
580 * upsets the thinkingmouse).
581 */
582
583 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
584 return PSMOUSE_THINKPS;
585
586/*
587 * Try Synaptics TouchPad
588 */
589 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
590 synaptics_hardware = 1;
591
592 if (max_proto > PSMOUSE_IMEX) {
593 if (!set_properties || synaptics_init(psmouse) == 0)
594 return PSMOUSE_SYNAPTICS;
595/*
596 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
597 * Unfortunately Logitech/Genius probes confuse some firmware versions so
598 * we'll have to skip them.
599 */
600 max_proto = PSMOUSE_IMEX;
601 }
602/*
603 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
604 */
605 synaptics_reset(psmouse);
606 }
607
608/*
609 * Try ALPS TouchPad
610 */
611 if (max_proto > PSMOUSE_IMEX) {
612 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
613 if (alps_detect(psmouse, set_properties) == 0) {
614 if (!set_properties || alps_init(psmouse) == 0)
615 return PSMOUSE_ALPS;
616/*
617 * Init failed, try basic relative protocols
618 */
619 max_proto = PSMOUSE_IMEX;
620 }
621 }
622
623 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
624 return PSMOUSE_GENPS;
625
626 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
627 return PSMOUSE_PS2PP;
628
Dmitry Torokhovba449952005-12-21 00:51:31 -0500629 if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
630 return PSMOUSE_TRACKPOINT;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
633 * Reset to defaults in case the device got confused by extended
Dmitry Torokhovba449952005-12-21 00:51:31 -0500634 * protocol probes. Note that we do full reset becuase some mice
635 * put themselves to sleep when see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Dmitry Torokhovba449952005-12-21 00:51:31 -0500637 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
640 return PSMOUSE_IMEX;
641
642 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
643 return PSMOUSE_IMPS;
644
645/*
646 * Okay, all failed, we have a standard mouse here. The number of the buttons
647 * is still a question, though. We assume 3.
648 */
649 ps2bare_detect(psmouse, set_properties);
650
651 if (synaptics_hardware) {
652/*
653 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
654 * We need to reset the touchpad because if there is a track point on the
655 * pass through port it could get disabled while probing for protocol
656 * extensions.
657 */
658 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
661 return PSMOUSE_PS2;
662}
663
Helge Dellere38de672006-09-10 21:54:39 -0400664static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500665 {
666 .type = PSMOUSE_PS2,
667 .name = "PS/2",
668 .alias = "bare",
669 .maxproto = 1,
670 .detect = ps2bare_detect,
671 },
672 {
673 .type = PSMOUSE_PS2PP,
674 .name = "PS2++",
675 .alias = "logitech",
676 .detect = ps2pp_init,
677 },
678 {
679 .type = PSMOUSE_THINKPS,
680 .name = "ThinkPS/2",
681 .alias = "thinkps",
682 .detect = thinking_detect,
683 },
684 {
685 .type = PSMOUSE_GENPS,
686 .name = "GenPS/2",
687 .alias = "genius",
688 .detect = genius_detect,
689 },
690 {
691 .type = PSMOUSE_IMPS,
692 .name = "ImPS/2",
693 .alias = "imps",
694 .maxproto = 1,
695 .detect = intellimouse_detect,
696 },
697 {
698 .type = PSMOUSE_IMEX,
699 .name = "ImExPS/2",
700 .alias = "exps",
701 .maxproto = 1,
702 .detect = im_explorer_detect,
703 },
704 {
705 .type = PSMOUSE_SYNAPTICS,
706 .name = "SynPS/2",
707 .alias = "synaptics",
708 .detect = synaptics_detect,
709 .init = synaptics_init,
710 },
711 {
712 .type = PSMOUSE_ALPS,
713 .name = "AlpsPS/2",
714 .alias = "alps",
715 .detect = alps_detect,
716 .init = alps_init,
717 },
718 {
719 .type = PSMOUSE_LIFEBOOK,
720 .name = "LBPS/2",
721 .alias = "lifebook",
722 .init = lifebook_init,
723 },
724 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500725 .type = PSMOUSE_TRACKPOINT,
726 .name = "TPPS/2",
727 .alias = "trackpoint",
728 .detect = trackpoint_detect,
729 },
730 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500731 .type = PSMOUSE_AUTO,
732 .name = "auto",
733 .alias = "any",
734 .maxproto = 1,
735 },
736};
737
Helge Dellere38de672006-09-10 21:54:39 -0400738static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500739{
740 int i;
741
742 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
743 if (psmouse_protocols[i].type == type)
744 return &psmouse_protocols[i];
745
746 WARN_ON(1);
747 return &psmouse_protocols[0];
748}
749
Helge Dellere38de672006-09-10 21:54:39 -0400750static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500751{
Helge Dellere38de672006-09-10 21:54:39 -0400752 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500753 int i;
754
755 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
756 p = &psmouse_protocols[i];
757
758 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
759 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
760 return &psmouse_protocols[i];
761 }
762
763 return NULL;
764}
765
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767/*
768 * psmouse_probe() probes for a PS/2 mouse.
769 */
770
771static int psmouse_probe(struct psmouse *psmouse)
772{
773 struct ps2dev *ps2dev = &psmouse->ps2dev;
774 unsigned char param[2];
775
776/*
777 * First, we check if it's a mouse. It should send 0x00 or 0x03
778 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500779 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
780 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
782
783 param[0] = 0xa5;
784 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
785 return -1;
786
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500787 if (param[0] != 0x00 && param[0] != 0x03 &&
788 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -1;
790
791/*
792 * Then we reset and disable the mouse so that it doesn't generate events.
793 */
794
795 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
796 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
797
798 return 0;
799}
800
801/*
802 * Here we set the mouse resolution.
803 */
804
805void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
806{
Helge Dellere38de672006-09-10 21:54:39 -0400807 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
808 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (resolution == 0 || resolution > 200)
811 resolution = 200;
812
Helge Dellere38de672006-09-10 21:54:39 -0400813 p = params[resolution / 50];
814 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
815 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818/*
819 * Here we set the mouse report rate.
820 */
821
822static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
823{
Helge Dellere38de672006-09-10 21:54:39 -0400824 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
825 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 int i = 0;
827
828 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400829 r = rates[i];
830 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
831 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834/*
835 * psmouse_initialize() initializes the mouse to a sane state.
836 */
837
838static void psmouse_initialize(struct psmouse *psmouse)
839{
840/*
841 * We set the mouse into streaming mode.
842 */
843
844 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
845
846/*
847 * We set the mouse report rate, resolution and scaling.
848 */
849
850 if (psmouse_max_proto != PSMOUSE_PS2) {
851 psmouse->set_rate(psmouse, psmouse->rate);
852 psmouse->set_resolution(psmouse, psmouse->resolution);
853 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
854 }
855}
856
857/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * psmouse_activate() enables the mouse so that we get motion reports from it.
859 */
860
861static void psmouse_activate(struct psmouse *psmouse)
862{
863 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
864 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
865 psmouse->ps2dev.serio->phys);
866
867 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
868}
869
870
871/*
872 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
873 * reports from it unless we explicitely request it.
874 */
875
876static void psmouse_deactivate(struct psmouse *psmouse)
877{
878 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
879 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
880 psmouse->ps2dev.serio->phys);
881
882 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
883}
884
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500885/*
886 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
887 */
888
889static int psmouse_poll(struct psmouse *psmouse)
890{
891 return ps2_command(&psmouse->ps2dev, psmouse->packet,
892 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
893}
894
895
896/*
897 * psmouse_resync() attempts to re-validate current protocol.
898 */
899
900static void psmouse_resync(void *p)
901{
902 struct psmouse *psmouse = p, *parent = NULL;
903 struct serio *serio = psmouse->ps2dev.serio;
904 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
905 int failed = 0, enabled = 0;
906 int i;
907
Ingo Molnarc14471d2006-02-19 00:22:11 -0500908 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500909
910 if (psmouse->state != PSMOUSE_RESYNCING)
911 goto out;
912
913 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
914 parent = serio_get_drvdata(serio->parent);
915 psmouse_deactivate(parent);
916 }
917
918/*
919 * Some mice don't ACK commands sent while they are in the middle of
920 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
921 * instead of ps2_command() which would wait for 200ms for an ACK
922 * that may never come.
923 * As an additional quirk ALPS touchpads may not only forget to ACK
924 * disable command but will stop reporting taps, so if we see that
925 * mouse at least once ACKs disable we will do full reconnect if ACK
926 * is missing.
927 */
928 psmouse->num_resyncs++;
929
930 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
931 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
932 failed = 1;
933 } else
934 psmouse->acks_disable_command = 1;
935
936/*
937 * Poll the mouse. If it was reset the packet will be shorter than
938 * psmouse->pktsize and ps2_command will fail. We do not expect and
939 * do not handle scenario when mouse "upgrades" its protocol while
940 * disconnected since it would require additional delay. If we ever
941 * see a mouse that does it we'll adjust the code.
942 */
943 if (!failed) {
944 if (psmouse->poll(psmouse))
945 failed = 1;
946 else {
947 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
948 for (i = 0; i < psmouse->pktsize; i++) {
949 psmouse->pktcnt++;
950 rc = psmouse->protocol_handler(psmouse, NULL);
951 if (rc != PSMOUSE_GOOD_DATA)
952 break;
953 }
954 if (rc != PSMOUSE_FULL_PACKET)
955 failed = 1;
956 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
957 }
958 }
959/*
960 * Now try to enable mouse. We try to do that even if poll failed and also
961 * repeat our attempts 5 times, otherwise we may be left out with disabled
962 * mouse.
963 */
964 for (i = 0; i < 5; i++) {
965 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
966 enabled = 1;
967 break;
968 }
969 msleep(200);
970 }
971
972 if (!enabled) {
973 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
974 psmouse->ps2dev.serio->phys);
975 failed = 1;
976 }
977
978 if (failed) {
979 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
980 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
981 serio_reconnect(serio);
982 } else
983 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
984
985 if (parent)
986 psmouse_activate(parent);
987 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -0500988 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500989}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991/*
992 * psmouse_cleanup() resets the mouse into power-on state.
993 */
994
995static void psmouse_cleanup(struct serio *serio)
996{
997 struct psmouse *psmouse = serio_get_drvdata(serio);
998
999 psmouse_reset(psmouse);
1000}
1001
1002/*
1003 * psmouse_disconnect() closes and frees.
1004 */
1005
1006static void psmouse_disconnect(struct serio *serio)
1007{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001008 struct psmouse *psmouse, *parent = NULL;
1009
1010 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001012 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Ingo Molnarc14471d2006-02-19 00:22:11 -05001014 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1017
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001018 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001019 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001020 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001021 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1024 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001025 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
1028 if (psmouse->disconnect)
1029 psmouse->disconnect(psmouse);
1030
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001031 if (parent && parent->pt_deactivate)
1032 parent->pt_deactivate(parent);
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 serio_close(serio);
1037 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001038 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001040
1041 if (parent)
1042 psmouse_activate(parent);
1043
Ingo Molnarc14471d2006-02-19 00:22:11 -05001044 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Helge Dellere38de672006-09-10 21:54:39 -04001047static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001048{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001049 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001050
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001051 input_dev->private = psmouse;
1052 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001053
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001054 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1055 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1056 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001057
1058 psmouse->set_rate = psmouse_set_rate;
1059 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001060 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001061 psmouse->protocol_handler = psmouse_process_byte;
1062 psmouse->pktsize = 3;
1063
1064 if (proto && (proto->detect || proto->init)) {
1065 if (proto->detect && proto->detect(psmouse, 1) < 0)
1066 return -1;
1067
1068 if (proto->init && proto->init(psmouse) < 0)
1069 return -1;
1070
1071 psmouse->type = proto->type;
1072 }
1073 else
1074 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1075
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001076 /*
1077 * If mouse's packet size is 3 there is no point in polling the
1078 * device in hopes to detect protocol reset - we won't get less
1079 * than 3 bytes response anyhow.
1080 */
1081 if (psmouse->pktsize == 3)
1082 psmouse->resync_time = 0;
1083
1084 /*
1085 * Some smart KVMs fake response to POLL command returning just
1086 * 3 bytes and messing up our resync logic, so if initial poll
1087 * fails we won't try polling the device anymore. Hopefully
1088 * such KVM will maintain initially selected protocol.
1089 */
1090 if (psmouse->resync_time && psmouse->poll(psmouse))
1091 psmouse->resync_time = 0;
1092
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001093 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1094 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001095
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001096 input_dev->name = psmouse->devname;
1097 input_dev->phys = psmouse->phys;
1098 input_dev->id.bustype = BUS_I8042;
1099 input_dev->id.vendor = 0x0002;
1100 input_dev->id.product = psmouse->type;
1101 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001102
1103 return 0;
1104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/*
1107 * psmouse_connect() is a callback from the serio module when
1108 * an unhandled serio port is found.
1109 */
1110static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1111{
1112 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001113 struct input_dev *input_dev;
1114 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Ingo Molnarc14471d2006-02-19 00:22:11 -05001116 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /*
1119 * If this is a pass-through port deactivate parent so the device
1120 * connected to this port can be successfully identified
1121 */
1122 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1123 parent = serio_get_drvdata(serio->parent);
1124 psmouse_deactivate(parent);
1125 }
1126
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001127 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1128 input_dev = input_allocate_device();
1129 if (!psmouse || !input_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 ps2_init(&psmouse->ps2dev, serio);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001133 INIT_WORK(&psmouse->resync_work, psmouse_resync, psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001134 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001135 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1138
1139 serio_set_drvdata(serio, psmouse);
1140
1141 retval = serio_open(serio, drv);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001142 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 if (psmouse_probe(psmouse) < 0) {
1146 serio_close(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 retval = -ENODEV;
1148 goto out;
1149 }
1150
1151 psmouse->rate = psmouse_rate;
1152 psmouse->resolution = psmouse_resolution;
1153 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001154 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001157 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 psmouse_initialize(psmouse);
1161
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001162 input_register_device(psmouse->dev);
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (parent && parent->pt_activate)
1165 parent->pt_activate(parent);
1166
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001167 sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 psmouse_activate(psmouse);
1170
1171 retval = 0;
1172
1173out:
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001174 if (retval) {
1175 serio_set_drvdata(serio, NULL);
1176 input_free_device(input_dev);
1177 kfree(psmouse);
1178 }
1179
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001180 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (parent)
1182 psmouse_activate(parent);
1183
Ingo Molnarc14471d2006-02-19 00:22:11 -05001184 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return retval;
1186}
1187
1188
1189static int psmouse_reconnect(struct serio *serio)
1190{
1191 struct psmouse *psmouse = serio_get_drvdata(serio);
1192 struct psmouse *parent = NULL;
1193 struct serio_driver *drv = serio->drv;
1194 int rc = -1;
1195
1196 if (!drv || !psmouse) {
1197 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1198 return -1;
1199 }
1200
Ingo Molnarc14471d2006-02-19 00:22:11 -05001201 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1204 parent = serio_get_drvdata(serio->parent);
1205 psmouse_deactivate(parent);
1206 }
1207
1208 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1209
1210 if (psmouse->reconnect) {
1211 if (psmouse->reconnect(psmouse))
1212 goto out;
1213 } else if (psmouse_probe(psmouse) < 0 ||
1214 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1215 goto out;
1216
1217 /* ok, the device type (and capabilities) match the old one,
1218 * we can continue using it, complete intialization
1219 */
1220 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1221
1222 psmouse_initialize(psmouse);
1223
1224 if (parent && parent->pt_activate)
1225 parent->pt_activate(parent);
1226
1227 psmouse_activate(psmouse);
1228 rc = 0;
1229
1230out:
1231 /* If this is a pass-through port the parent waits to be activated */
1232 if (parent)
1233 psmouse_activate(parent);
1234
Ingo Molnarc14471d2006-02-19 00:22:11 -05001235 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return rc;
1237}
1238
1239static struct serio_device_id psmouse_serio_ids[] = {
1240 {
1241 .type = SERIO_8042,
1242 .proto = SERIO_ANY,
1243 .id = SERIO_ANY,
1244 .extra = SERIO_ANY,
1245 },
1246 {
1247 .type = SERIO_PS_PSTHRU,
1248 .proto = SERIO_ANY,
1249 .id = SERIO_ANY,
1250 .extra = SERIO_ANY,
1251 },
1252 { 0 }
1253};
1254
1255MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1256
1257static struct serio_driver psmouse_drv = {
1258 .driver = {
1259 .name = "psmouse",
1260 },
1261 .description = DRIVER_DESC,
1262 .id_table = psmouse_serio_ids,
1263 .interrupt = psmouse_interrupt,
1264 .connect = psmouse_connect,
1265 .reconnect = psmouse_reconnect,
1266 .disconnect = psmouse_disconnect,
1267 .cleanup = psmouse_cleanup,
1268};
1269
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001270ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1271 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001274 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1275 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int retval;
1277
1278 retval = serio_pin_driver(serio);
1279 if (retval)
1280 return retval;
1281
1282 if (serio->drv != &psmouse_drv) {
1283 retval = -ENODEV;
1284 goto out;
1285 }
1286
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001287 psmouse = serio_get_drvdata(serio);
1288
1289 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291out:
1292 serio_unpin_driver(serio);
1293 return retval;
1294}
1295
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001296ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1297 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001300 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1301 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int retval;
1303
1304 retval = serio_pin_driver(serio);
1305 if (retval)
1306 return retval;
1307
1308 if (serio->drv != &psmouse_drv) {
1309 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001310 goto out_unpin;
1311 }
1312
Ingo Molnarc14471d2006-02-19 00:22:11 -05001313 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001314 if (retval)
1315 goto out_unpin;
1316
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001317 psmouse = serio_get_drvdata(serio);
1318
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001319 if (psmouse->state == PSMOUSE_IGNORE) {
1320 retval = -ENODEV;
Ingo Molnarc14471d2006-02-19 00:22:11 -05001321 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
1324 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1325 parent = serio_get_drvdata(serio->parent);
1326 psmouse_deactivate(parent);
1327 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 psmouse_deactivate(psmouse);
1330
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001331 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001333 if (retval != -ENODEV)
1334 psmouse_activate(psmouse);
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (parent)
1337 psmouse_activate(parent);
1338
Ingo Molnarc14471d2006-02-19 00:22:11 -05001339 out_unlock:
1340 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001341 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 serio_unpin_driver(serio);
1343 return retval;
1344}
1345
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001346static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1347{
1348 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1349
1350 return sprintf(buf, "%lu\n", *field);
1351}
1352
1353static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1354{
1355 unsigned long *field = (unsigned long *)((char *)psmouse + (size_t)offset);
1356 unsigned long value;
1357 char *rest;
1358
1359 value = simple_strtoul(buf, &rest, 10);
1360 if (*rest)
1361 return -EINVAL;
1362
1363 *field = value;
1364
1365 return count;
1366}
1367
1368static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001369{
1370 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1371}
1372
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001373static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001374{
1375 struct serio *serio = psmouse->ps2dev.serio;
1376 struct psmouse *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001377 struct input_dev *new_dev;
Helge Dellere38de672006-09-10 21:54:39 -04001378 const struct psmouse_protocol *proto;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001379 int retry = 0;
1380
1381 if (!(proto = psmouse_protocol_by_name(buf, count)))
1382 return -EINVAL;
1383
1384 if (psmouse->type == proto->type)
1385 return count;
1386
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001387 if (!(new_dev = input_allocate_device()))
1388 return -ENOMEM;
1389
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001390 while (serio->child) {
1391 if (++retry > 3) {
1392 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001393 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001394 return -EIO;
1395 }
1396
Ingo Molnarc14471d2006-02-19 00:22:11 -05001397 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001398 serio_unpin_driver(serio);
1399 serio_unregister_child_port(serio);
1400 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001401 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001402
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001403 if (serio->drv != &psmouse_drv) {
1404 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001405 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001406 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001407
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001408 if (psmouse->type == proto->type) {
1409 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001410 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001411 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001412 }
1413
1414 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1415 parent = serio_get_drvdata(serio->parent);
1416 if (parent->pt_deactivate)
1417 parent->pt_deactivate(parent);
1418 }
1419
1420 if (psmouse->disconnect)
1421 psmouse->disconnect(psmouse);
1422
1423 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001424 input_unregister_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001425
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001426 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001427 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1428
1429 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1430 psmouse_reset(psmouse);
1431 /* default to PSMOUSE_PS2 */
1432 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1433 }
1434
1435 psmouse_initialize(psmouse);
1436 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1437
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001438 input_register_device(psmouse->dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001439
1440 if (parent && parent->pt_activate)
1441 parent->pt_activate(parent);
1442
1443 return count;
1444}
1445
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001446static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 unsigned long value;
1449 char *rest;
1450
1451 value = simple_strtoul(buf, &rest, 10);
1452 if (*rest)
1453 return -EINVAL;
1454
1455 psmouse->set_rate(psmouse, value);
1456 return count;
1457}
1458
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001459static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 unsigned long value;
1462 char *rest;
1463
1464 value = simple_strtoul(buf, &rest, 10);
1465 if (*rest)
1466 return -EINVAL;
1467
1468 psmouse->set_resolution(psmouse, value);
1469 return count;
1470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1474{
Helge Dellere38de672006-09-10 21:54:39 -04001475 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 if (!val)
1478 return -EINVAL;
1479
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001480 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001482 if (!proto || !proto->maxproto)
1483 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001485 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Stephen Evanchik541e3162005-08-08 01:26:18 -05001487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
1490static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1491{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001492 int type = *((unsigned int *)kp->arg);
1493
1494 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
1497static int __init psmouse_init(void)
1498{
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001499 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1500 if (!kpsmoused_wq) {
1501 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1502 return -ENOMEM;
1503 }
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 serio_register_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return 0;
1508}
1509
1510static void __exit psmouse_exit(void)
1511{
1512 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001513 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516module_init(psmouse_init);
1517module_exit(psmouse_exit);