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