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