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