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