Input: evdev - do not block waiting for an event if fd is nonblock
[deliverable/linux.git] / drivers / input / mouse / psmouse-base.c
CommitLineData
1da177e4
LT
1/*
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
b5d21704
DT
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15#define psmouse_fmt(fmt) fmt
16
1da177e4
LT
17#include <linux/delay.h>
18#include <linux/module.h>
1da177e4
LT
19#include <linux/slab.h>
20#include <linux/interrupt.h>
21#include <linux/input.h>
22#include <linux/serio.h>
23#include <linux/init.h>
24#include <linux/libps2.h>
c14471dc
IM
25#include <linux/mutex.h>
26
1da177e4
LT
27#include "psmouse.h"
28#include "synaptics.h"
29#include "logips2pp.h"
30#include "alps.h"
df08ef27 31#include "hgpk.h"
02d7f589 32#include "lifebook.h"
541e316a 33#include "trackpoint.h"
24bf10ab 34#include "touchkit_ps2.h"
2a0bd75e 35#include "elantech.h"
fc69f4a6 36#include "sentelic.h"
1da177e4
LT
37
38#define DRIVER_DESC "PS/2 mouse driver"
39
40MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
41MODULE_DESCRIPTION(DRIVER_DESC);
42MODULE_LICENSE("GPL");
43
dbf4ccd6 44static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
9bbb9e5a
RR
45static int psmouse_set_maxproto(const char *val, const struct kernel_param *);
46static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp);
47static struct kernel_param_ops param_ops_proto_abbrev = {
48 .set = psmouse_set_maxproto,
49 .get = psmouse_get_maxproto,
50};
1da177e4 51#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
1da177e4 52module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
dbf4ccd6 53MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
1da177e4
LT
54
55static unsigned int psmouse_resolution = 200;
56module_param_named(resolution, psmouse_resolution, uint, 0644);
57MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
58
59static unsigned int psmouse_rate = 100;
60module_param_named(rate, psmouse_rate, uint, 0644);
61MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
62
63static unsigned int psmouse_smartscroll = 1;
64module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
65MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
66
f0d5c6f4 67static unsigned int psmouse_resetafter = 5;
1da177e4
LT
68module_param_named(resetafter, psmouse_resetafter, uint, 0644);
69MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
70
8bd0ee93 71static unsigned int psmouse_resync_time;
f0d5c6f4
DT
72module_param_named(resync_time, psmouse_resync_time, uint, 0644);
73MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
74
cfe9e888
DT
75PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
76 NULL,
77 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
78PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
79 (void *) offsetof(struct psmouse, rate),
80 psmouse_show_int_attr, psmouse_attr_set_rate);
81PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
82 (void *) offsetof(struct psmouse, resolution),
83 psmouse_show_int_attr, psmouse_attr_set_resolution);
84PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
85 (void *) offsetof(struct psmouse, resetafter),
86 psmouse_show_int_attr, psmouse_set_int_attr);
f0d5c6f4
DT
87PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
88 (void *) offsetof(struct psmouse, resync_time),
89 psmouse_show_int_attr, psmouse_set_int_attr);
cfe9e888
DT
90
91static struct attribute *psmouse_attributes[] = {
92 &psmouse_attr_protocol.dattr.attr,
93 &psmouse_attr_rate.dattr.attr,
94 &psmouse_attr_resolution.dattr.attr,
95 &psmouse_attr_resetafter.dattr.attr,
f0d5c6f4 96 &psmouse_attr_resync_time.dattr.attr,
cfe9e888
DT
97 NULL
98};
99
100static struct attribute_group psmouse_attribute_group = {
101 .attrs = psmouse_attributes,
102};
1da177e4 103
04df1925 104/*
c14471dc 105 * psmouse_mutex protects all operations changing state of mouse
04df1925
DT
106 * (connecting, disconnecting, changing rate or resolution via
107 * sysfs). We could use a per-device semaphore but since there
108 * rarely more than one PS/2 mouse connected and since semaphore
109 * is taken in "slow" paths it is not worth it.
110 */
c14471dc 111static DEFINE_MUTEX(psmouse_mutex);
04df1925 112
f0d5c6f4
DT
113static struct workqueue_struct *kpsmoused_wq;
114
dbf4ccd6
DT
115struct psmouse_protocol {
116 enum psmouse_type type;
b7802c5c 117 bool maxproto;
6b9d363c 118 bool ignore_parity; /* Protocol should ignore parity errors from KBC */
e38de678
HD
119 const char *name;
120 const char *alias;
b7802c5c 121 int (*detect)(struct psmouse *, bool);
dbf4ccd6
DT
122 int (*init)(struct psmouse *);
123};
1da177e4
LT
124
125/*
126 * psmouse_process_byte() analyzes the PS/2 data stream and reports
127 * relevant events to the input module once full packet has arrived.
128 */
129
7968a5dd 130psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
1da177e4 131{
2e5b636b 132 struct input_dev *dev = psmouse->dev;
1da177e4
LT
133 unsigned char *packet = psmouse->packet;
134
135 if (psmouse->pktcnt < psmouse->pktsize)
136 return PSMOUSE_GOOD_DATA;
137
138/*
139 * Full packet accumulated, process it
140 */
141
1da177e4
LT
142/*
143 * Scroll wheel on IntelliMice, scroll buttons on NetMice
144 */
145
146 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
147 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
148
149/*
150 * Scroll wheel and buttons on IntelliMouse Explorer
151 */
152
153 if (psmouse->type == PSMOUSE_IMEX) {
b0c9ad8e 154 switch (packet[3] & 0xC0) {
a62f0d27
DT
155 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
156 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
157 break;
158 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
159 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
160 break;
161 case 0x00:
162 case 0xC0:
163 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
164 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
165 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
166 break;
b0c9ad8e 167 }
1da177e4
LT
168 }
169
170/*
171 * Extra buttons on Genius NewNet 3D
172 */
173
174 if (psmouse->type == PSMOUSE_GENPS) {
175 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
176 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
177 }
178
179/*
180 * Extra button on ThinkingMouse
181 */
182 if (psmouse->type == PSMOUSE_THINKPS) {
183 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
184 /* Without this bit of weirdness moving up gives wildly high Y changes. */
185 packet[1] |= (packet[0] & 0x40) << 1;
186 }
187
aea6a461
AR
188/*
189 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
190 * byte.
191 */
192 if (psmouse->type == PSMOUSE_CORTRON) {
193 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
194 packet[0] |= 0x08;
195 }
196
1da177e4
LT
197/*
198 * Generic PS/2 Mouse
199 */
200
201 input_report_key(dev, BTN_LEFT, packet[0] & 1);
202 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
203 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
204
205 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
206 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
207
208 input_sync(dev);
209
210 return PSMOUSE_FULL_PACKET;
211}
212
8bf020ee
AS
213void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
214 unsigned long delay)
215{
216 queue_delayed_work(kpsmoused_wq, work, delay);
217}
218
1da177e4 219/*
f0d5c6f4
DT
220 * __psmouse_set_state() sets new psmouse state and resets all flags.
221 */
222
223static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
224{
225 psmouse->state = new_state;
b7802c5c 226 psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
f0d5c6f4
DT
227 psmouse->ps2dev.flags = 0;
228 psmouse->last = jiffies;
229}
230
231
232/*
233 * psmouse_set_state() sets new psmouse state and resets all flags and
234 * counters while holding serio lock so fighting with interrupt handler
235 * is not a concern.
236 */
237
a48cf5f3 238void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
f0d5c6f4
DT
239{
240 serio_pause_rx(psmouse->ps2dev.serio);
241 __psmouse_set_state(psmouse, new_state);
242 serio_continue_rx(psmouse->ps2dev.serio);
243}
244
245/*
246 * psmouse_handle_byte() processes one byte of the input data stream
247 * by calling corresponding protocol handler.
248 */
249
7d12e780 250static int psmouse_handle_byte(struct psmouse *psmouse)
f0d5c6f4 251{
7d12e780 252 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
f0d5c6f4
DT
253
254 switch (rc) {
a62f0d27
DT
255 case PSMOUSE_BAD_DATA:
256 if (psmouse->state == PSMOUSE_ACTIVATED) {
b5d21704
DT
257 psmouse_warn(psmouse,
258 "%s at %s lost sync at byte %d\n",
259 psmouse->name, psmouse->phys,
260 psmouse->pktcnt);
a62f0d27
DT
261 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
262 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
b5d21704
DT
263 psmouse_notice(psmouse,
264 "issuing reconnect request\n");
a62f0d27
DT
265 serio_reconnect(psmouse->ps2dev.serio);
266 return -1;
f0d5c6f4 267 }
a62f0d27
DT
268 }
269 psmouse->pktcnt = 0;
270 break;
271
272 case PSMOUSE_FULL_PACKET:
273 psmouse->pktcnt = 0;
274 if (psmouse->out_of_sync_cnt) {
275 psmouse->out_of_sync_cnt = 0;
b5d21704
DT
276 psmouse_notice(psmouse,
277 "%s at %s - driver resynced.\n",
278 psmouse->name, psmouse->phys);
a62f0d27
DT
279 }
280 break;
f0d5c6f4 281
a62f0d27
DT
282 case PSMOUSE_GOOD_DATA:
283 break;
f0d5c6f4
DT
284 }
285 return 0;
286}
287
288/*
289 * psmouse_interrupt() handles incoming characters, either passing them
290 * for normal processing or gathering them as command response.
1da177e4
LT
291 */
292
293static irqreturn_t psmouse_interrupt(struct serio *serio,
7d12e780 294 unsigned char data, unsigned int flags)
1da177e4
LT
295{
296 struct psmouse *psmouse = serio_get_drvdata(serio);
1da177e4
LT
297
298 if (psmouse->state == PSMOUSE_IGNORE)
299 goto out;
300
6b9d363c
DT
301 if (unlikely((flags & SERIO_TIMEOUT) ||
302 ((flags & SERIO_PARITY) && !psmouse->ignore_parity))) {
303
1da177e4 304 if (psmouse->state == PSMOUSE_ACTIVATED)
b5d21704
DT
305 psmouse_warn(psmouse,
306 "bad data from KBC -%s%s\n",
307 flags & SERIO_TIMEOUT ? " timeout" : "",
308 flags & SERIO_PARITY ? " bad parity" : "");
1da177e4
LT
309 ps2_cmd_aborted(&psmouse->ps2dev);
310 goto out;
311 }
312
313 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
314 if (ps2_handle_ack(&psmouse->ps2dev, data))
315 goto out;
316
317 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
318 if (ps2_handle_response(&psmouse->ps2dev, data))
319 goto out;
320
f0d5c6f4 321 if (psmouse->state <= PSMOUSE_RESYNCING)
1da177e4
LT
322 goto out;
323
324 if (psmouse->state == PSMOUSE_ACTIVATED &&
325 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
b5d21704
DT
326 psmouse_info(psmouse, "%s at %s lost synchronization, throwing %d bytes away.\n",
327 psmouse->name, psmouse->phys, psmouse->pktcnt);
f0d5c6f4
DT
328 psmouse->badbyte = psmouse->packet[0];
329 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
8bf020ee 330 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
f0d5c6f4 331 goto out;
1da177e4
LT
332 }
333
1da177e4 334 psmouse->packet[psmouse->pktcnt++] = data;
f0d5c6f4
DT
335/*
336 * Check if this is a new device announcement (0xAA 0x00)
337 */
338 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
89c9b480
DT
339 if (psmouse->pktcnt == 1) {
340 psmouse->last = jiffies;
1da177e4 341 goto out;
89c9b480 342 }
1da177e4 343
535650fd
ZH
344 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
345 (psmouse->type == PSMOUSE_HGPK &&
346 psmouse->packet[1] == PSMOUSE_RET_BAT)) {
f0d5c6f4
DT
347 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
348 serio_reconnect(serio);
349 goto out;
1da177e4 350 }
f0d5c6f4
DT
351/*
352 * Not a new device, try processing first byte normally
353 */
354 psmouse->pktcnt = 1;
7d12e780 355 if (psmouse_handle_byte(psmouse))
f0d5c6f4 356 goto out;
1da177e4 357
f0d5c6f4
DT
358 psmouse->packet[psmouse->pktcnt++] = data;
359 }
1da177e4 360
f0d5c6f4
DT
361/*
362 * See if we need to force resync because mouse was idle for too long
363 */
364 if (psmouse->state == PSMOUSE_ACTIVATED &&
365 psmouse->pktcnt == 1 && psmouse->resync_time &&
366 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
367 psmouse->badbyte = psmouse->packet[0];
368 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
8bf020ee 369 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
f0d5c6f4
DT
370 goto out;
371 }
1da177e4 372
f0d5c6f4 373 psmouse->last = jiffies;
7d12e780 374 psmouse_handle_byte(psmouse);
1da177e4 375
f0d5c6f4 376 out:
1da177e4
LT
377 return IRQ_HANDLED;
378}
379
380
381/*
382 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
383 * using sliced syntax, understood by advanced devices, such as Logitech
384 * or Synaptics touchpads. The command is encoded as:
385 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
386 * is the command.
387 */
388int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
389{
390 int i;
391
392 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
393 return -1;
394
395 for (i = 6; i >= 0; i -= 2) {
396 unsigned char d = (command >> i) & 3;
397 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
398 return -1;
399 }
400
401 return 0;
402}
403
404
405/*
406 * psmouse_reset() resets the mouse into power-on state.
407 */
408int psmouse_reset(struct psmouse *psmouse)
409{
410 unsigned char param[2];
411
412 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
413 return -1;
414
415 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
416 return -1;
417
418 return 0;
419}
420
421
422/*
423 * Genius NetMouse magic init.
424 */
b7802c5c 425static int genius_detect(struct psmouse *psmouse, bool set_properties)
1da177e4
LT
426{
427 struct ps2dev *ps2dev = &psmouse->ps2dev;
428 unsigned char param[4];
429
430 param[0] = 3;
431 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
432 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
433 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
434 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
435 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
436
437 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
438 return -1;
439
440 if (set_properties) {
315eb996 441 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
b7802c5c
DT
442 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
443 __set_bit(BTN_SIDE, psmouse->dev->keybit);
444 __set_bit(REL_WHEEL, psmouse->dev->relbit);
1da177e4
LT
445
446 psmouse->vendor = "Genius";
a3f3f317 447 psmouse->name = "Mouse";
1da177e4
LT
448 psmouse->pktsize = 4;
449 }
450
451 return 0;
452}
453
454/*
455 * IntelliMouse magic init.
456 */
b7802c5c 457static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
1da177e4
LT
458{
459 struct ps2dev *ps2dev = &psmouse->ps2dev;
460 unsigned char param[2];
461
462 param[0] = 200;
463 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
464 param[0] = 100;
465 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
466 param[0] = 80;
467 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
468 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
469
470 if (param[0] != 3)
471 return -1;
472
473 if (set_properties) {
b7802c5c
DT
474 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
475 __set_bit(REL_WHEEL, psmouse->dev->relbit);
1da177e4 476
315eb996
DT
477 if (!psmouse->vendor)
478 psmouse->vendor = "Generic";
479 if (!psmouse->name)
480 psmouse->name = "Wheel Mouse";
1da177e4
LT
481 psmouse->pktsize = 4;
482 }
483
484 return 0;
485}
486
487/*
488 * Try IntelliMouse/Explorer magic init.
489 */
b7802c5c 490static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
1da177e4
LT
491{
492 struct ps2dev *ps2dev = &psmouse->ps2dev;
493 unsigned char param[2];
494
495 intellimouse_detect(psmouse, 0);
496
497 param[0] = 200;
498 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
499 param[0] = 200;
500 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
501 param[0] = 80;
502 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
503 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
504
505 if (param[0] != 4)
506 return -1;
507
b0c9ad8e
PB
508/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
509 param[0] = 200;
510 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
511 param[0] = 80;
512 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
513 param[0] = 40;
514 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
515
1da177e4 516 if (set_properties) {
b7802c5c
DT
517 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
518 __set_bit(REL_WHEEL, psmouse->dev->relbit);
519 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
520 __set_bit(BTN_SIDE, psmouse->dev->keybit);
521 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
1da177e4 522
315eb996
DT
523 if (!psmouse->vendor)
524 psmouse->vendor = "Generic";
525 if (!psmouse->name)
526 psmouse->name = "Explorer Mouse";
1da177e4
LT
527 psmouse->pktsize = 4;
528 }
529
530 return 0;
531}
532
533/*
534 * Kensington ThinkingMouse / ExpertMouse magic init.
535 */
b7802c5c 536static int thinking_detect(struct psmouse *psmouse, bool set_properties)
1da177e4
LT
537{
538 struct ps2dev *ps2dev = &psmouse->ps2dev;
539 unsigned char param[2];
e38de678 540 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
1da177e4
LT
541 int i;
542
543 param[0] = 10;
544 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
545 param[0] = 0;
546 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
e38de678
HD
547 for (i = 0; i < ARRAY_SIZE(seq); i++) {
548 param[0] = seq[i];
549 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
550 }
1da177e4
LT
551 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
552
553 if (param[0] != 2)
554 return -1;
555
556 if (set_properties) {
315eb996 557 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
b7802c5c 558 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
1da177e4
LT
559
560 psmouse->vendor = "Kensington";
561 psmouse->name = "ThinkingMouse";
562 }
563
564 return 0;
565}
566
567/*
568 * Bare PS/2 protocol "detection". Always succeeds.
569 */
b7802c5c 570static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
1da177e4 571{
dbf4ccd6 572 if (set_properties) {
315eb996
DT
573 if (!psmouse->vendor)
574 psmouse->vendor = "Generic";
575 if (!psmouse->name)
576 psmouse->name = "Mouse";
577
578/*
579 * We have no way of figuring true number of buttons so let's
580 * assume that the device has 3.
581 */
582 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
dbf4ccd6 583 }
1da177e4
LT
584
585 return 0;
586}
587
aea6a461
AR
588/*
589 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
590 * must be forced by sysfs protocol writing.
591 */
b7802c5c 592static int cortron_detect(struct psmouse *psmouse, bool set_properties)
aea6a461
AR
593{
594 if (set_properties) {
595 psmouse->vendor = "Cortron";
596 psmouse->name = "PS/2 Trackball";
315eb996
DT
597
598 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
b7802c5c 599 __set_bit(BTN_SIDE, psmouse->dev->keybit);
aea6a461
AR
600 }
601
602 return 0;
603}
dbf4ccd6 604
1da177e4
LT
605/*
606 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
607 * the mouse may have.
608 */
609
610static int psmouse_extensions(struct psmouse *psmouse,
b7802c5c 611 unsigned int max_proto, bool set_properties)
1da177e4 612{
0698989d 613 bool synaptics_hardware = false;
1da177e4 614
a15d60f8
DT
615/*
616 * We always check for lifebook because it does not disturb mouse
617 * (it only checks DMI information).
618 */
dbf4ccd6 619 if (lifebook_detect(psmouse, set_properties) == 0) {
a15d60f8
DT
620 if (max_proto > PSMOUSE_IMEX) {
621 if (!set_properties || lifebook_init(psmouse) == 0)
622 return PSMOUSE_LIFEBOOK;
623 }
624 }
02d7f589 625
1da177e4
LT
626/*
627 * Try Kensington ThinkingMouse (we try first, because synaptics probe
628 * upsets the thinkingmouse).
629 */
630
631 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
632 return PSMOUSE_THINKPS;
633
634/*
55e3d922
AS
635 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
636 * support is disabled in config - we need to know if it is synaptics so we
637 * can reset it properly after probing for intellimouse.
1da177e4
LT
638 */
639 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
b7802c5c 640 synaptics_hardware = true;
1da177e4
LT
641
642 if (max_proto > PSMOUSE_IMEX) {
e4e6efd2
DD
643/*
644 * Try activating protocol, but check if support is enabled first, since
645 * we try detecting Synaptics even when protocol is disabled.
646 */
647 if (synaptics_supported() &&
648 (!set_properties || synaptics_init(psmouse) == 0)) {
1da177e4 649 return PSMOUSE_SYNAPTICS;
e4e6efd2
DD
650 }
651
1da177e4
LT
652/*
653 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
654 * Unfortunately Logitech/Genius probes confuse some firmware versions so
655 * we'll have to skip them.
656 */
657 max_proto = PSMOUSE_IMEX;
658 }
659/*
660 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
661 */
662 synaptics_reset(psmouse);
663 }
664
665/*
666 * Try ALPS TouchPad
667 */
668 if (max_proto > PSMOUSE_IMEX) {
669 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
670 if (alps_detect(psmouse, set_properties) == 0) {
671 if (!set_properties || alps_init(psmouse) == 0)
672 return PSMOUSE_ALPS;
673/*
674 * Init failed, try basic relative protocols
675 */
676 max_proto = PSMOUSE_IMEX;
677 }
678 }
679
df08ef27
AS
680/*
681 * Try OLPC HGPK touchpad.
682 */
683 if (max_proto > PSMOUSE_IMEX &&
684 hgpk_detect(psmouse, set_properties) == 0) {
685 if (!set_properties || hgpk_init(psmouse) == 0)
686 return PSMOUSE_HGPK;
687/*
688 * Init failed, try basic relative protocols
689 */
690 max_proto = PSMOUSE_IMEX;
691 }
24bf10ab 692
2a0bd75e
AO
693/*
694 * Try Elantech touchpad.
695 */
696 if (max_proto > PSMOUSE_IMEX &&
697 elantech_detect(psmouse, set_properties) == 0) {
698 if (!set_properties || elantech_init(psmouse) == 0)
699 return PSMOUSE_ELANTECH;
700/*
701 * Init failed, try basic relative protocols
702 */
703 max_proto = PSMOUSE_IMEX;
704 }
705
fc69f4a6 706
df08ef27 707 if (max_proto > PSMOUSE_IMEX) {
24bf10ab
SL
708 if (genius_detect(psmouse, set_properties) == 0)
709 return PSMOUSE_GENPS;
710
711 if (ps2pp_init(psmouse, set_properties) == 0)
712 return PSMOUSE_PS2PP;
1da177e4 713
24bf10ab
SL
714 if (trackpoint_detect(psmouse, set_properties) == 0)
715 return PSMOUSE_TRACKPOINT;
1da177e4 716
24bf10ab
SL
717 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
718 return PSMOUSE_TOUCHKIT_PS2;
719 }
ba44995a 720
4a18b3ab
TL
721/*
722 * Try Finger Sensing Pad. We do it here because its probe upsets
723 * Trackpoint devices (causing TP_READ_ID command to time out).
724 */
725 if (max_proto > PSMOUSE_IMEX) {
726 if (fsp_detect(psmouse, set_properties) == 0) {
727 if (!set_properties || fsp_init(psmouse) == 0)
728 return PSMOUSE_FSP;
729/*
730 * Init failed, try basic relative protocols
731 */
732 max_proto = PSMOUSE_IMEX;
733 }
734 }
735
1da177e4
LT
736/*
737 * Reset to defaults in case the device got confused by extended
554fc193
AZ
738 * protocol probes. Note that we follow up with full reset because
739 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
1da177e4 740 */
554fc193 741 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
ba44995a 742 psmouse_reset(psmouse);
1da177e4
LT
743
744 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
745 return PSMOUSE_IMEX;
746
747 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
748 return PSMOUSE_IMPS;
749
750/*
751 * Okay, all failed, we have a standard mouse here. The number of the buttons
752 * is still a question, though. We assume 3.
753 */
754 ps2bare_detect(psmouse, set_properties);
755
756 if (synaptics_hardware) {
757/*
758 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
759 * We need to reset the touchpad because if there is a track point on the
760 * pass through port it could get disabled while probing for protocol
761 * extensions.
762 */
763 psmouse_reset(psmouse);
1da177e4
LT
764 }
765
766 return PSMOUSE_PS2;
767}
768
e38de678 769static const struct psmouse_protocol psmouse_protocols[] = {
dbf4ccd6
DT
770 {
771 .type = PSMOUSE_PS2,
772 .name = "PS/2",
773 .alias = "bare",
b7802c5c 774 .maxproto = true,
6b9d363c 775 .ignore_parity = true,
dbf4ccd6
DT
776 .detect = ps2bare_detect,
777 },
55e3d922 778#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
dbf4ccd6
DT
779 {
780 .type = PSMOUSE_PS2PP,
781 .name = "PS2++",
782 .alias = "logitech",
783 .detect = ps2pp_init,
784 },
55e3d922 785#endif
dbf4ccd6
DT
786 {
787 .type = PSMOUSE_THINKPS,
788 .name = "ThinkPS/2",
789 .alias = "thinkps",
790 .detect = thinking_detect,
791 },
792 {
793 .type = PSMOUSE_GENPS,
794 .name = "GenPS/2",
795 .alias = "genius",
796 .detect = genius_detect,
797 },
798 {
799 .type = PSMOUSE_IMPS,
800 .name = "ImPS/2",
801 .alias = "imps",
b7802c5c 802 .maxproto = true,
6b9d363c 803 .ignore_parity = true,
dbf4ccd6
DT
804 .detect = intellimouse_detect,
805 },
806 {
807 .type = PSMOUSE_IMEX,
808 .name = "ImExPS/2",
809 .alias = "exps",
b7802c5c 810 .maxproto = true,
6b9d363c 811 .ignore_parity = true,
dbf4ccd6
DT
812 .detect = im_explorer_detect,
813 },
55e3d922 814#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
dbf4ccd6
DT
815 {
816 .type = PSMOUSE_SYNAPTICS,
817 .name = "SynPS/2",
818 .alias = "synaptics",
819 .detect = synaptics_detect,
820 .init = synaptics_init,
821 },
7968a5dd
DD
822 {
823 .type = PSMOUSE_SYNAPTICS_RELATIVE,
824 .name = "SynRelPS/2",
825 .alias = "synaptics-relative",
826 .detect = synaptics_detect,
827 .init = synaptics_init_relative,
828 },
55e3d922
AS
829#endif
830#ifdef CONFIG_MOUSE_PS2_ALPS
dbf4ccd6
DT
831 {
832 .type = PSMOUSE_ALPS,
833 .name = "AlpsPS/2",
834 .alias = "alps",
835 .detect = alps_detect,
836 .init = alps_init,
837 },
55e3d922
AS
838#endif
839#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
dbf4ccd6
DT
840 {
841 .type = PSMOUSE_LIFEBOOK,
842 .name = "LBPS/2",
843 .alias = "lifebook",
844 .init = lifebook_init,
845 },
55e3d922
AS
846#endif
847#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
541e316a
SE
848 {
849 .type = PSMOUSE_TRACKPOINT,
850 .name = "TPPS/2",
851 .alias = "trackpoint",
852 .detect = trackpoint_detect,
853 },
55e3d922
AS
854#endif
855#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
24bf10ab
SL
856 {
857 .type = PSMOUSE_TOUCHKIT_PS2,
858 .name = "touchkitPS/2",
859 .alias = "touchkit",
860 .detect = touchkit_ps2_detect,
861 },
df08ef27
AS
862#endif
863#ifdef CONFIG_MOUSE_PS2_OLPC
864 {
865 .type = PSMOUSE_HGPK,
866 .name = "OLPC HGPK",
867 .alias = "hgpk",
868 .detect = hgpk_detect,
869 },
55e3d922 870#endif
2a0bd75e
AO
871#ifdef CONFIG_MOUSE_PS2_ELANTECH
872 {
873 .type = PSMOUSE_ELANTECH,
874 .name = "ETPS/2",
875 .alias = "elantech",
876 .detect = elantech_detect,
877 .init = elantech_init,
878 },
fc69f4a6
TL
879#endif
880#ifdef CONFIG_MOUSE_PS2_SENTELIC
881 {
882 .type = PSMOUSE_FSP,
883 .name = "FSPPS/2",
884 .alias = "fsp",
885 .detect = fsp_detect,
886 .init = fsp_init,
887 },
888#endif
aea6a461
AR
889 {
890 .type = PSMOUSE_CORTRON,
891 .name = "CortronPS/2",
892 .alias = "cortps",
893 .detect = cortron_detect,
894 },
dbf4ccd6
DT
895 {
896 .type = PSMOUSE_AUTO,
897 .name = "auto",
898 .alias = "any",
b7802c5c 899 .maxproto = true,
dbf4ccd6
DT
900 },
901};
902
e38de678 903static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
dbf4ccd6
DT
904{
905 int i;
906
907 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
908 if (psmouse_protocols[i].type == type)
909 return &psmouse_protocols[i];
910
911 WARN_ON(1);
912 return &psmouse_protocols[0];
913}
914
e38de678 915static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
dbf4ccd6 916{
e38de678 917 const struct psmouse_protocol *p;
dbf4ccd6
DT
918 int i;
919
920 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
921 p = &psmouse_protocols[i];
922
923 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
924 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
925 return &psmouse_protocols[i];
926 }
927
928 return NULL;
929}
930
931
1da177e4
LT
932/*
933 * psmouse_probe() probes for a PS/2 mouse.
934 */
935
936static int psmouse_probe(struct psmouse *psmouse)
937{
938 struct ps2dev *ps2dev = &psmouse->ps2dev;
939 unsigned char param[2];
940
941/*
942 * First, we check if it's a mouse. It should send 0x00 or 0x03
943 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
7741e931
VP
944 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
945 * ID queries, probably due to a firmware bug.
1da177e4
LT
946 */
947
948 param[0] = 0xa5;
949 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
950 return -1;
951
7741e931
VP
952 if (param[0] != 0x00 && param[0] != 0x03 &&
953 param[0] != 0x04 && param[0] != 0xff)
1da177e4
LT
954 return -1;
955
956/*
957 * Then we reset and disable the mouse so that it doesn't generate events.
958 */
959
960 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
b5d21704
DT
961 psmouse_warn(psmouse, "Failed to reset mouse on %s\n",
962 ps2dev->serio->phys);
1da177e4
LT
963
964 return 0;
965}
966
967/*
968 * Here we set the mouse resolution.
969 */
970
971void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
972{
e38de678
HD
973 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
974 unsigned char p;
1da177e4
LT
975
976 if (resolution == 0 || resolution > 200)
977 resolution = 200;
978
e38de678
HD
979 p = params[resolution / 50];
980 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
981 psmouse->resolution = 25 << p;
1da177e4
LT
982}
983
984/*
985 * Here we set the mouse report rate.
986 */
987
988static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
989{
e38de678
HD
990 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
991 unsigned char r;
1da177e4
LT
992 int i = 0;
993
994 while (rates[i] > rate) i++;
e38de678
HD
995 r = rates[i];
996 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
997 psmouse->rate = r;
1da177e4
LT
998}
999
1000/*
1001 * psmouse_initialize() initializes the mouse to a sane state.
1002 */
1003
1004static void psmouse_initialize(struct psmouse *psmouse)
1005{
1da177e4
LT
1006/*
1007 * We set the mouse report rate, resolution and scaling.
1008 */
1009
1010 if (psmouse_max_proto != PSMOUSE_PS2) {
1011 psmouse->set_rate(psmouse, psmouse->rate);
1012 psmouse->set_resolution(psmouse, psmouse->resolution);
1013 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
1014 }
1015}
1016
1da177e4
LT
1017/*
1018 * psmouse_activate() enables the mouse so that we get motion reports from it.
1019 */
1020
1021static void psmouse_activate(struct psmouse *psmouse)
1022{
1023 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
b5d21704
DT
1024 psmouse_warn(psmouse, "Failed to enable mouse on %s\n",
1025 psmouse->ps2dev.serio->phys);
1da177e4
LT
1026
1027 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1028}
1029
1030
1031/*
1032 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
c03983ac 1033 * reports from it unless we explicitly request it.
1da177e4
LT
1034 */
1035
1036static void psmouse_deactivate(struct psmouse *psmouse)
1037{
1038 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
b5d21704
DT
1039 psmouse_warn(psmouse, "Failed to deactivate mouse on %s\n",
1040 psmouse->ps2dev.serio->phys);
1da177e4
LT
1041
1042 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1043}
1044
f0d5c6f4 1045/*
b5d21704 1046 * psmouse_poll() - default poll handler. Everyone except for ALPS uses it.
f0d5c6f4
DT
1047 */
1048
1049static int psmouse_poll(struct psmouse *psmouse)
1050{
1051 return ps2_command(&psmouse->ps2dev, psmouse->packet,
1052 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
1053}
1054
1055
1056/*
1057 * psmouse_resync() attempts to re-validate current protocol.
1058 */
1059
c4028958 1060static void psmouse_resync(struct work_struct *work)
f0d5c6f4 1061{
c4028958 1062 struct psmouse *parent = NULL, *psmouse =
8bf020ee 1063 container_of(work, struct psmouse, resync_work.work);
f0d5c6f4
DT
1064 struct serio *serio = psmouse->ps2dev.serio;
1065 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
b7802c5c 1066 bool failed = false, enabled = false;
f0d5c6f4
DT
1067 int i;
1068
c14471dc 1069 mutex_lock(&psmouse_mutex);
f0d5c6f4
DT
1070
1071 if (psmouse->state != PSMOUSE_RESYNCING)
1072 goto out;
1073
1074 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1075 parent = serio_get_drvdata(serio->parent);
1076 psmouse_deactivate(parent);
1077 }
1078
1079/*
1080 * Some mice don't ACK commands sent while they are in the middle of
1081 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1082 * instead of ps2_command() which would wait for 200ms for an ACK
1083 * that may never come.
1084 * As an additional quirk ALPS touchpads may not only forget to ACK
1085 * disable command but will stop reporting taps, so if we see that
1086 * mouse at least once ACKs disable we will do full reconnect if ACK
1087 * is missing.
1088 */
1089 psmouse->num_resyncs++;
1090
1091 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1092 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
b7802c5c 1093 failed = true;
f0d5c6f4 1094 } else
b7802c5c 1095 psmouse->acks_disable_command = true;
f0d5c6f4
DT
1096
1097/*
1098 * Poll the mouse. If it was reset the packet will be shorter than
1099 * psmouse->pktsize and ps2_command will fail. We do not expect and
1100 * do not handle scenario when mouse "upgrades" its protocol while
1101 * disconnected since it would require additional delay. If we ever
1102 * see a mouse that does it we'll adjust the code.
1103 */
1104 if (!failed) {
1105 if (psmouse->poll(psmouse))
b7802c5c 1106 failed = true;
f0d5c6f4
DT
1107 else {
1108 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1109 for (i = 0; i < psmouse->pktsize; i++) {
1110 psmouse->pktcnt++;
7d12e780 1111 rc = psmouse->protocol_handler(psmouse);
f0d5c6f4
DT
1112 if (rc != PSMOUSE_GOOD_DATA)
1113 break;
1114 }
1115 if (rc != PSMOUSE_FULL_PACKET)
b7802c5c 1116 failed = true;
f0d5c6f4
DT
1117 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1118 }
1119 }
1120/*
1121 * Now try to enable mouse. We try to do that even if poll failed and also
1122 * repeat our attempts 5 times, otherwise we may be left out with disabled
1123 * mouse.
1124 */
1125 for (i = 0; i < 5; i++) {
1126 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
b7802c5c 1127 enabled = true;
f0d5c6f4
DT
1128 break;
1129 }
1130 msleep(200);
1131 }
1132
1133 if (!enabled) {
b5d21704
DT
1134 psmouse_warn(psmouse, "failed to re-enable mouse on %s\n",
1135 psmouse->ps2dev.serio->phys);
b7802c5c 1136 failed = true;
f0d5c6f4
DT
1137 }
1138
1139 if (failed) {
1140 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
b5d21704
DT
1141 psmouse_info(psmouse,
1142 "resync failed, issuing reconnect request\n");
f0d5c6f4
DT
1143 serio_reconnect(serio);
1144 } else
1145 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1146
1147 if (parent)
1148 psmouse_activate(parent);
1149 out:
c14471dc 1150 mutex_unlock(&psmouse_mutex);
f0d5c6f4 1151}
1da177e4
LT
1152
1153/*
1154 * psmouse_cleanup() resets the mouse into power-on state.
1155 */
1156
1157static void psmouse_cleanup(struct serio *serio)
1158{
1159 struct psmouse *psmouse = serio_get_drvdata(serio);
a1cec061
DT
1160 struct psmouse *parent = NULL;
1161
1162 mutex_lock(&psmouse_mutex);
1163
1164 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1165 parent = serio_get_drvdata(serio->parent);
1166 psmouse_deactivate(parent);
1167 }
1168
a9f0c381
DT
1169 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1170
1171 /*
1172 * Disable stream mode so cleanup routine can proceed undisturbed.
1173 */
1174 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
b5d21704
DT
1175 psmouse_warn(psmouse, "Failed to disable mouse on %s\n",
1176 psmouse->ps2dev.serio->phys);
a1cec061
DT
1177
1178 if (psmouse->cleanup)
1179 psmouse->cleanup(psmouse);
1da177e4 1180
4a299bf5
DT
1181/*
1182 * Reset the mouse to defaults (bare PS/2 protocol).
1183 */
1184 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
a1cec061
DT
1185
1186/*
1187 * Some boxes, such as HP nx7400, get terribly confused if mouse
1188 * is not fully enabled before suspending/shutting down.
1189 */
1190 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1191
1192 if (parent) {
1193 if (parent->pt_deactivate)
1194 parent->pt_deactivate(parent);
1195
1196 psmouse_activate(parent);
1197 }
1198
1199 mutex_unlock(&psmouse_mutex);
1da177e4
LT
1200}
1201
1202/*
1203 * psmouse_disconnect() closes and frees.
1204 */
1205
1206static void psmouse_disconnect(struct serio *serio)
1207{
04df1925
DT
1208 struct psmouse *psmouse, *parent = NULL;
1209
1210 psmouse = serio_get_drvdata(serio);
1da177e4 1211
cfe9e888 1212 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1da177e4 1213
c14471dc 1214 mutex_lock(&psmouse_mutex);
04df1925 1215
1da177e4
LT
1216 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1217
f0d5c6f4 1218 /* make sure we don't have a resync in progress */
c14471dc 1219 mutex_unlock(&psmouse_mutex);
f0d5c6f4 1220 flush_workqueue(kpsmoused_wq);
c14471dc 1221 mutex_lock(&psmouse_mutex);
f0d5c6f4 1222
1da177e4
LT
1223 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1224 parent = serio_get_drvdata(serio->parent);
04df1925 1225 psmouse_deactivate(parent);
1da177e4
LT
1226 }
1227
1228 if (psmouse->disconnect)
1229 psmouse->disconnect(psmouse);
1230
04df1925
DT
1231 if (parent && parent->pt_deactivate)
1232 parent->pt_deactivate(parent);
1233
1da177e4
LT
1234 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1235
1da177e4
LT
1236 serio_close(serio);
1237 serio_set_drvdata(serio, NULL);
2e5b636b 1238 input_unregister_device(psmouse->dev);
1da177e4 1239 kfree(psmouse);
04df1925
DT
1240
1241 if (parent)
1242 psmouse_activate(parent);
1243
c14471dc 1244 mutex_unlock(&psmouse_mutex);
1da177e4
LT
1245}
1246
315eb996
DT
1247static int psmouse_switch_protocol(struct psmouse *psmouse,
1248 const struct psmouse_protocol *proto)
dbf4ccd6 1249{
6b9d363c 1250 const struct psmouse_protocol *selected_proto;
2e5b636b 1251 struct input_dev *input_dev = psmouse->dev;
dbf4ccd6 1252
28aa7f1c 1253 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
dbf4ccd6 1254
7b19ada2 1255 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
315eb996
DT
1256 input_dev->keybit[BIT_WORD(BTN_MOUSE)] =
1257 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
7b19ada2 1258 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
dbf4ccd6
DT
1259
1260 psmouse->set_rate = psmouse_set_rate;
1261 psmouse->set_resolution = psmouse_set_resolution;
f0d5c6f4 1262 psmouse->poll = psmouse_poll;
dbf4ccd6
DT
1263 psmouse->protocol_handler = psmouse_process_byte;
1264 psmouse->pktsize = 3;
1265
1266 if (proto && (proto->detect || proto->init)) {
a62f0d27 1267 if (proto->detect && proto->detect(psmouse, true) < 0)
dbf4ccd6
DT
1268 return -1;
1269
1270 if (proto->init && proto->init(psmouse) < 0)
1271 return -1;
1272
1273 psmouse->type = proto->type;
6b9d363c
DT
1274 selected_proto = proto;
1275 } else {
b7802c5c
DT
1276 psmouse->type = psmouse_extensions(psmouse,
1277 psmouse_max_proto, true);
6b9d363c
DT
1278 selected_proto = psmouse_protocol_by_type(psmouse->type);
1279 }
1280
1281 psmouse->ignore_parity = selected_proto->ignore_parity;
dbf4ccd6 1282
f0d5c6f4
DT
1283 /*
1284 * If mouse's packet size is 3 there is no point in polling the
1285 * device in hopes to detect protocol reset - we won't get less
1286 * than 3 bytes response anyhow.
1287 */
1288 if (psmouse->pktsize == 3)
1289 psmouse->resync_time = 0;
1290
1291 /*
1292 * Some smart KVMs fake response to POLL command returning just
1293 * 3 bytes and messing up our resync logic, so if initial poll
1294 * fails we won't try polling the device anymore. Hopefully
1295 * such KVM will maintain initially selected protocol.
1296 */
1297 if (psmouse->resync_time && psmouse->poll(psmouse))
1298 psmouse->resync_time = 0;
1299
08ffce45 1300 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
6b9d363c 1301 selected_proto->name, psmouse->vendor, psmouse->name);
dbf4ccd6 1302
2e5b636b
DT
1303 input_dev->name = psmouse->devname;
1304 input_dev->phys = psmouse->phys;
1305 input_dev->id.bustype = BUS_I8042;
1306 input_dev->id.vendor = 0x0002;
1307 input_dev->id.product = psmouse->type;
1308 input_dev->id.version = psmouse->model;
dbf4ccd6
DT
1309
1310 return 0;
1311}
1312
1da177e4
LT
1313/*
1314 * psmouse_connect() is a callback from the serio module when
1315 * an unhandled serio port is found.
1316 */
1317static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1318{
1319 struct psmouse *psmouse, *parent = NULL;
2e5b636b 1320 struct input_dev *input_dev;
72155615 1321 int retval = 0, error = -ENOMEM;
1da177e4 1322
c14471dc 1323 mutex_lock(&psmouse_mutex);
04df1925 1324
1da177e4
LT
1325 /*
1326 * If this is a pass-through port deactivate parent so the device
1327 * connected to this port can be successfully identified
1328 */
1329 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1330 parent = serio_get_drvdata(serio->parent);
1331 psmouse_deactivate(parent);
1332 }
1333
2e5b636b
DT
1334 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1335 input_dev = input_allocate_device();
1336 if (!psmouse || !input_dev)
72155615 1337 goto err_free;
1da177e4 1338
1da177e4 1339 ps2_init(&psmouse->ps2dev, serio);
8bf020ee 1340 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
2e5b636b 1341 psmouse->dev = input_dev;
08ffce45 1342 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
dbf4ccd6 1343
1da177e4
LT
1344 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1345
1346 serio_set_drvdata(serio, psmouse);
1347
72155615
DT
1348 error = serio_open(serio, drv);
1349 if (error)
1350 goto err_clear_drvdata;
1da177e4
LT
1351
1352 if (psmouse_probe(psmouse) < 0) {
72155615
DT
1353 error = -ENODEV;
1354 goto err_close_serio;
1da177e4
LT
1355 }
1356
1357 psmouse->rate = psmouse_rate;
1358 psmouse->resolution = psmouse_resolution;
1359 psmouse->resetafter = psmouse_resetafter;
f0d5c6f4 1360 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1da177e4 1361 psmouse->smartscroll = psmouse_smartscroll;
1da177e4 1362
dbf4ccd6 1363 psmouse_switch_protocol(psmouse, NULL);
1da177e4 1364
1da177e4 1365 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1da177e4
LT
1366 psmouse_initialize(psmouse);
1367
72155615
DT
1368 error = input_register_device(psmouse->dev);
1369 if (error)
1370 goto err_protocol_disconnect;
2e5b636b 1371
1da177e4
LT
1372 if (parent && parent->pt_activate)
1373 parent->pt_activate(parent);
1374
72155615
DT
1375 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1376 if (error)
1377 goto err_pt_deactivate;
1da177e4
LT
1378
1379 psmouse_activate(psmouse);
1380
72155615 1381 out:
04df1925 1382 /* If this is a pass-through port the parent needs to be re-activated */
1da177e4
LT
1383 if (parent)
1384 psmouse_activate(parent);
1385
c14471dc 1386 mutex_unlock(&psmouse_mutex);
1da177e4 1387 return retval;
72155615
DT
1388
1389 err_pt_deactivate:
1390 if (parent && parent->pt_deactivate)
1391 parent->pt_deactivate(parent);
746b31a9
AS
1392 input_unregister_device(psmouse->dev);
1393 input_dev = NULL; /* so we don't try to free it below */
72155615
DT
1394 err_protocol_disconnect:
1395 if (psmouse->disconnect)
1396 psmouse->disconnect(psmouse);
1397 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1398 err_close_serio:
1399 serio_close(serio);
1400 err_clear_drvdata:
1401 serio_set_drvdata(serio, NULL);
1402 err_free:
1403 input_free_device(input_dev);
1404 kfree(psmouse);
1405
1406 retval = error;
1407 goto out;
1da177e4
LT
1408}
1409
1410
1411static int psmouse_reconnect(struct serio *serio)
1412{
1413 struct psmouse *psmouse = serio_get_drvdata(serio);
1414 struct psmouse *parent = NULL;
1415 struct serio_driver *drv = serio->drv;
ef110b24 1416 unsigned char type;
1da177e4
LT
1417 int rc = -1;
1418
1419 if (!drv || !psmouse) {
b5d21704
DT
1420 psmouse_dbg(psmouse,
1421 "reconnect request, but serio is disconnected, ignoring...\n");
1da177e4
LT
1422 return -1;
1423 }
1424
c14471dc 1425 mutex_lock(&psmouse_mutex);
04df1925 1426
1da177e4
LT
1427 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1428 parent = serio_get_drvdata(serio->parent);
1429 psmouse_deactivate(parent);
1430 }
1431
1432 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1433
1434 if (psmouse->reconnect) {
1435 if (psmouse->reconnect(psmouse))
1436 goto out;
ef110b24
DT
1437 } else {
1438 psmouse_reset(psmouse);
1439
1440 if (psmouse_probe(psmouse) < 0)
1441 goto out;
1442
1443 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1444 if (psmouse->type != type)
1445 goto out;
b7802c5c 1446 }
1da177e4 1447
b5d21704
DT
1448 /*
1449 * OK, the device type (and capabilities) match the old one,
1450 * we can continue using it, complete initialization
1da177e4
LT
1451 */
1452 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1453
1454 psmouse_initialize(psmouse);
1455
1456 if (parent && parent->pt_activate)
1457 parent->pt_activate(parent);
1458
1459 psmouse_activate(psmouse);
1460 rc = 0;
1461
1462out:
1463 /* If this is a pass-through port the parent waits to be activated */
1464 if (parent)
1465 psmouse_activate(parent);
1466
c14471dc 1467 mutex_unlock(&psmouse_mutex);
1da177e4
LT
1468 return rc;
1469}
1470
1471static struct serio_device_id psmouse_serio_ids[] = {
1472 {
1473 .type = SERIO_8042,
1474 .proto = SERIO_ANY,
1475 .id = SERIO_ANY,
1476 .extra = SERIO_ANY,
1477 },
1478 {
1479 .type = SERIO_PS_PSTHRU,
1480 .proto = SERIO_ANY,
1481 .id = SERIO_ANY,
1482 .extra = SERIO_ANY,
1483 },
1484 { 0 }
1485};
1486
1487MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1488
1489static struct serio_driver psmouse_drv = {
1490 .driver = {
1491 .name = "psmouse",
1492 },
1493 .description = DRIVER_DESC,
1494 .id_table = psmouse_serio_ids,
1495 .interrupt = psmouse_interrupt,
1496 .connect = psmouse_connect,
1497 .reconnect = psmouse_reconnect,
1498 .disconnect = psmouse_disconnect,
1499 .cleanup = psmouse_cleanup,
1500};
1501
cfe9e888
DT
1502ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1503 char *buf)
1da177e4
LT
1504{
1505 struct serio *serio = to_serio_port(dev);
cfe9e888
DT
1506 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1507 struct psmouse *psmouse;
1da177e4 1508
cfe9e888
DT
1509 psmouse = serio_get_drvdata(serio);
1510
59b01513 1511 return attr->show(psmouse, attr->data, buf);
1da177e4
LT
1512}
1513
cfe9e888
DT
1514ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1515 const char *buf, size_t count)
1da177e4
LT
1516{
1517 struct serio *serio = to_serio_port(dev);
cfe9e888
DT
1518 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1519 struct psmouse *psmouse, *parent = NULL;
1da177e4
LT
1520 int retval;
1521
c14471dc 1522 retval = mutex_lock_interruptible(&psmouse_mutex);
04df1925 1523 if (retval)
59b01513 1524 goto out;
04df1925 1525
cfe9e888
DT
1526 psmouse = serio_get_drvdata(serio);
1527
68d48221
AS
1528 if (attr->protect) {
1529 if (psmouse->state == PSMOUSE_IGNORE) {
1530 retval = -ENODEV;
1531 goto out_unlock;
1532 }
1da177e4 1533
68d48221
AS
1534 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1535 parent = serio_get_drvdata(serio->parent);
1536 psmouse_deactivate(parent);
1537 }
dbf4ccd6 1538
68d48221
AS
1539 psmouse_deactivate(psmouse);
1540 }
1da177e4 1541
cfe9e888 1542 retval = attr->set(psmouse, attr->data, buf, count);
1da177e4 1543
68d48221
AS
1544 if (attr->protect) {
1545 if (retval != -ENODEV)
1546 psmouse_activate(psmouse);
dbf4ccd6 1547
68d48221
AS
1548 if (parent)
1549 psmouse_activate(parent);
1550 }
1da177e4 1551
c14471dc
IM
1552 out_unlock:
1553 mutex_unlock(&psmouse_mutex);
59b01513 1554 out:
1da177e4
LT
1555 return retval;
1556}
1557
cfe9e888
DT
1558static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1559{
eb5d5829 1560 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
cfe9e888 1561
eb5d5829 1562 return sprintf(buf, "%u\n", *field);
cfe9e888
DT
1563}
1564
1565static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1566{
eb5d5829 1567 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
76496e7a
JD
1568 unsigned int value;
1569 int err;
cfe9e888 1570
76496e7a
JD
1571 err = kstrtouint(buf, 10, &value);
1572 if (err)
1573 return err;
eb5d5829 1574
cfe9e888
DT
1575 *field = value;
1576
1577 return count;
1578}
1579
1580static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
dbf4ccd6
DT
1581{
1582 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1583}
1584
cfe9e888 1585static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
dbf4ccd6
DT
1586{
1587 struct serio *serio = psmouse->ps2dev.serio;
1588 struct psmouse *parent = NULL;
72155615
DT
1589 struct input_dev *old_dev, *new_dev;
1590 const struct psmouse_protocol *proto, *old_proto;
1591 int error;
dbf4ccd6
DT
1592 int retry = 0;
1593
72155615
DT
1594 proto = psmouse_protocol_by_name(buf, count);
1595 if (!proto)
dbf4ccd6
DT
1596 return -EINVAL;
1597
1598 if (psmouse->type == proto->type)
1599 return count;
1600
72155615
DT
1601 new_dev = input_allocate_device();
1602 if (!new_dev)
2e5b636b
DT
1603 return -ENOMEM;
1604
09822582 1605 while (!list_empty(&serio->children)) {
dbf4ccd6 1606 if (++retry > 3) {
b5d21704
DT
1607 psmouse_warn(psmouse,
1608 "failed to destroy children ports, protocol change aborted.\n");
2e5b636b 1609 input_free_device(new_dev);
dbf4ccd6
DT
1610 return -EIO;
1611 }
1612
c14471dc 1613 mutex_unlock(&psmouse_mutex);
dbf4ccd6 1614 serio_unregister_child_port(serio);
c14471dc 1615 mutex_lock(&psmouse_mutex);
dbf4ccd6 1616
2e5b636b
DT
1617 if (serio->drv != &psmouse_drv) {
1618 input_free_device(new_dev);
dbf4ccd6 1619 return -ENODEV;
2e5b636b 1620 }
dbf4ccd6 1621
2e5b636b
DT
1622 if (psmouse->type == proto->type) {
1623 input_free_device(new_dev);
dbf4ccd6 1624 return count; /* switched by other thread */
2e5b636b 1625 }
dbf4ccd6
DT
1626 }
1627
1628 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1629 parent = serio_get_drvdata(serio->parent);
1630 if (parent->pt_deactivate)
1631 parent->pt_deactivate(parent);
1632 }
1633
72155615
DT
1634 old_dev = psmouse->dev;
1635 old_proto = psmouse_protocol_by_type(psmouse->type);
1636
dbf4ccd6
DT
1637 if (psmouse->disconnect)
1638 psmouse->disconnect(psmouse);
1639
1640 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
dbf4ccd6 1641
2e5b636b 1642 psmouse->dev = new_dev;
dbf4ccd6
DT
1643 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1644
1645 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1646 psmouse_reset(psmouse);
1647 /* default to PSMOUSE_PS2 */
1648 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1649 }
1650
1651 psmouse_initialize(psmouse);
1652 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1653
72155615
DT
1654 error = input_register_device(psmouse->dev);
1655 if (error) {
1656 if (psmouse->disconnect)
1657 psmouse->disconnect(psmouse);
1658
1659 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1660 input_free_device(new_dev);
1661 psmouse->dev = old_dev;
1662 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1663 psmouse_switch_protocol(psmouse, old_proto);
1664 psmouse_initialize(psmouse);
1665 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1666
1667 return error;
1668 }
1669
1670 input_unregister_device(old_dev);
dbf4ccd6
DT
1671
1672 if (parent && parent->pt_activate)
1673 parent->pt_activate(parent);
1674
1675 return count;
1676}
1677
cfe9e888 1678static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1da177e4 1679{
76496e7a
JD
1680 unsigned int value;
1681 int err;
1da177e4 1682
76496e7a
JD
1683 err = kstrtouint(buf, 10, &value);
1684 if (err)
1685 return err;
1da177e4
LT
1686
1687 psmouse->set_rate(psmouse, value);
1688 return count;
1689}
1690
cfe9e888 1691static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1da177e4 1692{
76496e7a
JD
1693 unsigned int value;
1694 int err;
1da177e4 1695
76496e7a
JD
1696 err = kstrtouint(buf, 10, &value);
1697 if (err)
1698 return err;
1da177e4
LT
1699
1700 psmouse->set_resolution(psmouse, value);
1701 return count;
1702}
1703
1da177e4 1704
9bbb9e5a 1705static int psmouse_set_maxproto(const char *val, const struct kernel_param *kp)
1da177e4 1706{
e38de678 1707 const struct psmouse_protocol *proto;
1da177e4
LT
1708
1709 if (!val)
1710 return -EINVAL;
1711
dbf4ccd6 1712 proto = psmouse_protocol_by_name(val, strlen(val));
1da177e4 1713
dbf4ccd6
DT
1714 if (!proto || !proto->maxproto)
1715 return -EINVAL;
1da177e4 1716
dbf4ccd6 1717 *((unsigned int *)kp->arg) = proto->type;
1da177e4 1718
541e316a 1719 return 0;
1da177e4
LT
1720}
1721
9bbb9e5a 1722static int psmouse_get_maxproto(char *buffer, const struct kernel_param *kp)
1da177e4 1723{
dbf4ccd6
DT
1724 int type = *((unsigned int *)kp->arg);
1725
3d4c3aa9 1726 return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
1da177e4
LT
1727}
1728
1729static int __init psmouse_init(void)
1730{
153a9df0
AM
1731 int err;
1732
7705d548
DT
1733 lifebook_module_init();
1734 synaptics_module_init();
ca94ec43 1735 hgpk_module_init();
7705d548 1736
f0d5c6f4
DT
1737 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1738 if (!kpsmoused_wq) {
b5d21704 1739 pr_err("failed to create kpsmoused workqueue\n");
f0d5c6f4
DT
1740 return -ENOMEM;
1741 }
1742
153a9df0
AM
1743 err = serio_register_driver(&psmouse_drv);
1744 if (err)
1745 destroy_workqueue(kpsmoused_wq);
f0d5c6f4 1746
153a9df0 1747 return err;
1da177e4
LT
1748}
1749
1750static void __exit psmouse_exit(void)
1751{
1752 serio_unregister_driver(&psmouse_drv);
f0d5c6f4 1753 destroy_workqueue(kpsmoused_wq);
1da177e4
LT
1754}
1755
1756module_init(psmouse_init);
1757module_exit(psmouse_exit);
This page took 0.440195 seconds and 5 git commands to generate.