blob: f15f695777f8ff6b0c352a948a9a1bc0fe942a2f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
16#include <linux/moduleparam.h>
17#include <linux/slab.h>
18#include <linux/interrupt.h>
19#include <linux/input.h>
20#include <linux/serio.h>
21#include <linux/init.h>
22#include <linux/libps2.h>
Ingo Molnarc14471d2006-02-19 00:22:11 -050023#include <linux/mutex.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "psmouse.h"
26#include "synaptics.h"
27#include "logips2pp.h"
28#include "alps.h"
Kenan Esau02d7f582005-05-29 02:30:22 -050029#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050030#include "trackpoint.h"
Stefan Lucke24bf10a2007-02-18 01:49:10 -050031#include "touchkit_ps2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define DRIVER_DESC "PS/2 mouse driver"
34
35MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
36MODULE_DESCRIPTION(DRIVER_DESC);
37MODULE_LICENSE("GPL");
38
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050039static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
41static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
43#define param_set_proto_abbrev psmouse_set_maxproto
44#define param_get_proto_abbrev psmouse_get_maxproto
45module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050046MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static unsigned int psmouse_resolution = 200;
49module_param_named(resolution, psmouse_resolution, uint, 0644);
50MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
51
52static unsigned int psmouse_rate = 100;
53module_param_named(rate, psmouse_rate, uint, 0644);
54MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
55
56static unsigned int psmouse_smartscroll = 1;
57module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
58MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
59
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050060static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061module_param_named(resetafter, psmouse_resetafter, uint, 0644);
62MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
63
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050064static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050065module_param_named(resync_time, psmouse_resync_time, uint, 0644);
66MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
67
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050068PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
69 NULL,
70 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
71PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
72 (void *) offsetof(struct psmouse, rate),
73 psmouse_show_int_attr, psmouse_attr_set_rate);
74PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
75 (void *) offsetof(struct psmouse, resolution),
76 psmouse_show_int_attr, psmouse_attr_set_resolution);
77PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
78 (void *) offsetof(struct psmouse, resetafter),
79 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050080PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
81 (void *) offsetof(struct psmouse, resync_time),
82 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050083
84static struct attribute *psmouse_attributes[] = {
85 &psmouse_attr_protocol.dattr.attr,
86 &psmouse_attr_rate.dattr.attr,
87 &psmouse_attr_resolution.dattr.attr,
88 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050089 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050090 NULL
91};
92
93static struct attribute_group psmouse_attribute_group = {
94 .attrs = psmouse_attributes,
95};
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Dmitry Torokhov04df1922005-06-01 02:39:44 -050097/*
Ingo Molnarc14471d2006-02-19 00:22:11 -050098 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -050099 * (connecting, disconnecting, changing rate or resolution via
100 * sysfs). We could use a per-device semaphore but since there
101 * rarely more than one PS/2 mouse connected and since semaphore
102 * is taken in "slow" paths it is not worth it.
103 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500104static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500105
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500106static struct workqueue_struct *kpsmoused_wq;
107
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500108struct psmouse_protocol {
109 enum psmouse_type type;
Helge Dellere38de672006-09-10 21:54:39 -0400110 const char *name;
111 const char *alias;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500112 int maxproto;
113 int (*detect)(struct psmouse *, int);
114 int (*init)(struct psmouse *);
115};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/*
118 * psmouse_process_byte() analyzes the PS/2 data stream and reports
119 * relevant events to the input module once full packet has arrived.
120 */
121
David Howells7d12e782006-10-05 14:55:46 +0100122static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500124 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned char *packet = psmouse->packet;
126
127 if (psmouse->pktcnt < psmouse->pktsize)
128 return PSMOUSE_GOOD_DATA;
129
130/*
131 * Full packet accumulated, process it
132 */
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * Scroll wheel on IntelliMice, scroll buttons on NetMice
136 */
137
138 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
139 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
140
141/*
142 * Scroll wheel and buttons on IntelliMouse Explorer
143 */
144
145 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400146 switch (packet[3] & 0xC0) {
147 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
148 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
149 break;
150 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
151 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
152 break;
153 case 0x00:
154 case 0xC0:
155 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
156 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
157 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
158 break;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162/*
163 * Extra buttons on Genius NewNet 3D
164 */
165
166 if (psmouse->type == PSMOUSE_GENPS) {
167 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
168 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
169 }
170
171/*
172 * Extra button on ThinkingMouse
173 */
174 if (psmouse->type == PSMOUSE_THINKPS) {
175 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
176 /* Without this bit of weirdness moving up gives wildly high Y changes. */
177 packet[1] |= (packet[0] & 0x40) << 1;
178 }
179
180/*
181 * Generic PS/2 Mouse
182 */
183
184 input_report_key(dev, BTN_LEFT, packet[0] & 1);
185 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
186 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
187
188 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
189 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
190
191 input_sync(dev);
192
193 return PSMOUSE_FULL_PACKET;
194}
195
196/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500197 * __psmouse_set_state() sets new psmouse state and resets all flags.
198 */
199
200static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
201{
202 psmouse->state = new_state;
203 psmouse->pktcnt = psmouse->out_of_sync = 0;
204 psmouse->ps2dev.flags = 0;
205 psmouse->last = jiffies;
206}
207
208
209/*
210 * psmouse_set_state() sets new psmouse state and resets all flags and
211 * counters while holding serio lock so fighting with interrupt handler
212 * is not a concern.
213 */
214
215static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
216{
217 serio_pause_rx(psmouse->ps2dev.serio);
218 __psmouse_set_state(psmouse, new_state);
219 serio_continue_rx(psmouse->ps2dev.serio);
220}
221
222/*
223 * psmouse_handle_byte() processes one byte of the input data stream
224 * by calling corresponding protocol handler.
225 */
226
David Howells7d12e782006-10-05 14:55:46 +0100227static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500228{
David Howells7d12e782006-10-05 14:55:46 +0100229 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500230
231 switch (rc) {
232 case PSMOUSE_BAD_DATA:
233 if (psmouse->state == PSMOUSE_ACTIVATED) {
234 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
235 psmouse->name, psmouse->phys, psmouse->pktcnt);
236 if (++psmouse->out_of_sync == psmouse->resetafter) {
237 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
238 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
239 serio_reconnect(psmouse->ps2dev.serio);
240 return -1;
241 }
242 }
243 psmouse->pktcnt = 0;
244 break;
245
246 case PSMOUSE_FULL_PACKET:
247 psmouse->pktcnt = 0;
248 if (psmouse->out_of_sync) {
249 psmouse->out_of_sync = 0;
250 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
251 psmouse->name, psmouse->phys);
252 }
253 break;
254
255 case PSMOUSE_GOOD_DATA:
256 break;
257 }
258 return 0;
259}
260
261/*
262 * psmouse_interrupt() handles incoming characters, either passing them
263 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
265
266static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100267 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (psmouse->state == PSMOUSE_IGNORE)
272 goto out;
273
274 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
275 if (psmouse->state == PSMOUSE_ACTIVATED)
276 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
277 flags & SERIO_TIMEOUT ? " timeout" : "",
278 flags & SERIO_PARITY ? " bad parity" : "");
279 ps2_cmd_aborted(&psmouse->ps2dev);
280 goto out;
281 }
282
283 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
284 if (ps2_handle_ack(&psmouse->ps2dev, data))
285 goto out;
286
287 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
288 if (ps2_handle_response(&psmouse->ps2dev, data))
289 goto out;
290
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500291 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto out;
293
294 if (psmouse->state == PSMOUSE_ACTIVATED &&
295 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500296 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500298 psmouse->badbyte = psmouse->packet[0];
299 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
300 queue_work(kpsmoused_wq, &psmouse->resync_work);
301 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500305/*
306 * Check if this is a new device announcement (0xAA 0x00)
307 */
308 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400309 if (psmouse->pktcnt == 1) {
310 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500314 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
315 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
316 serio_reconnect(serio);
317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500319/*
320 * Not a new device, try processing first byte normally
321 */
322 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100323 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500324 goto out;
325
326 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500329/*
330 * See if we need to force resync because mouse was idle for too long
331 */
332 if (psmouse->state == PSMOUSE_ACTIVATED &&
333 psmouse->pktcnt == 1 && psmouse->resync_time &&
334 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
335 psmouse->badbyte = psmouse->packet[0];
336 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
337 queue_work(kpsmoused_wq, &psmouse->resync_work);
338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500340
341 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100342 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500343
344 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return IRQ_HANDLED;
346}
347
348
349/*
350 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
351 * using sliced syntax, understood by advanced devices, such as Logitech
352 * or Synaptics touchpads. The command is encoded as:
353 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
354 * is the command.
355 */
356int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
357{
358 int i;
359
360 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
361 return -1;
362
363 for (i = 6; i >= 0; i -= 2) {
364 unsigned char d = (command >> i) & 3;
365 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
366 return -1;
367 }
368
369 return 0;
370}
371
372
373/*
374 * psmouse_reset() resets the mouse into power-on state.
375 */
376int psmouse_reset(struct psmouse *psmouse)
377{
378 unsigned char param[2];
379
380 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
381 return -1;
382
383 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
384 return -1;
385
386 return 0;
387}
388
389
390/*
391 * Genius NetMouse magic init.
392 */
393static int genius_detect(struct psmouse *psmouse, int set_properties)
394{
395 struct ps2dev *ps2dev = &psmouse->ps2dev;
396 unsigned char param[4];
397
398 param[0] = 3;
399 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
400 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
401 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
402 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
403 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
404
405 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
406 return -1;
407
408 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500409 set_bit(BTN_EXTRA, psmouse->dev->keybit);
410 set_bit(BTN_SIDE, psmouse->dev->keybit);
411 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500414 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 psmouse->pktsize = 4;
416 }
417
418 return 0;
419}
420
421/*
422 * IntelliMouse magic init.
423 */
424static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
425{
426 struct ps2dev *ps2dev = &psmouse->ps2dev;
427 unsigned char param[2];
428
429 param[0] = 200;
430 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
431 param[0] = 100;
432 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
433 param[0] = 80;
434 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
435 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
436
437 if (param[0] != 3)
438 return -1;
439
440 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500441 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
442 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (!psmouse->vendor) psmouse->vendor = "Generic";
445 if (!psmouse->name) psmouse->name = "Wheel Mouse";
446 psmouse->pktsize = 4;
447 }
448
449 return 0;
450}
451
452/*
453 * Try IntelliMouse/Explorer magic init.
454 */
455static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
456{
457 struct ps2dev *ps2dev = &psmouse->ps2dev;
458 unsigned char param[2];
459
460 intellimouse_detect(psmouse, 0);
461
462 param[0] = 200;
463 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
464 param[0] = 200;
465 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
466 param[0] = 80;
467 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
468 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
469
470 if (param[0] != 4)
471 return -1;
472
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400473/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
474 param[0] = 200;
475 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
476 param[0] = 80;
477 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
478 param[0] = 40;
479 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500482 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
483 set_bit(REL_WHEEL, psmouse->dev->relbit);
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400484 set_bit(REL_HWHEEL, psmouse->dev->relbit);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500485 set_bit(BTN_SIDE, psmouse->dev->keybit);
486 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (!psmouse->vendor) psmouse->vendor = "Generic";
489 if (!psmouse->name) psmouse->name = "Explorer Mouse";
490 psmouse->pktsize = 4;
491 }
492
493 return 0;
494}
495
496/*
497 * Kensington ThinkingMouse / ExpertMouse magic init.
498 */
499static int thinking_detect(struct psmouse *psmouse, int set_properties)
500{
501 struct ps2dev *ps2dev = &psmouse->ps2dev;
502 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400503 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int i;
505
506 param[0] = 10;
507 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
508 param[0] = 0;
509 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400510 for (i = 0; i < ARRAY_SIZE(seq); i++) {
511 param[0] = seq[i];
512 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
515
516 if (param[0] != 2)
517 return -1;
518
519 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500520 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 psmouse->vendor = "Kensington";
523 psmouse->name = "ThinkingMouse";
524 }
525
526 return 0;
527}
528
529/*
530 * Bare PS/2 protocol "detection". Always succeeds.
531 */
532static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
533{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500534 if (set_properties) {
535 if (!psmouse->vendor) psmouse->vendor = "Generic";
536 if (!psmouse->name) psmouse->name = "Mouse";
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 return 0;
540}
541
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/*
544 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
545 * the mouse may have.
546 */
547
548static int psmouse_extensions(struct psmouse *psmouse,
549 unsigned int max_proto, int set_properties)
550{
551 int synaptics_hardware = 0;
552
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500553/*
554 * We always check for lifebook because it does not disturb mouse
555 * (it only checks DMI information).
556 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500557 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500558 if (max_proto > PSMOUSE_IMEX) {
559 if (!set_properties || lifebook_init(psmouse) == 0)
560 return PSMOUSE_LIFEBOOK;
561 }
562 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * Try Kensington ThinkingMouse (we try first, because synaptics probe
566 * upsets the thinkingmouse).
567 */
568
569 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
570 return PSMOUSE_THINKPS;
571
572/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500573 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
574 * support is disabled in config - we need to know if it is synaptics so we
575 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
577 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
578 synaptics_hardware = 1;
579
580 if (max_proto > PSMOUSE_IMEX) {
581 if (!set_properties || synaptics_init(psmouse) == 0)
582 return PSMOUSE_SYNAPTICS;
583/*
584 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
585 * Unfortunately Logitech/Genius probes confuse some firmware versions so
586 * we'll have to skip them.
587 */
588 max_proto = PSMOUSE_IMEX;
589 }
590/*
591 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
592 */
593 synaptics_reset(psmouse);
594 }
595
596/*
597 * Try ALPS TouchPad
598 */
599 if (max_proto > PSMOUSE_IMEX) {
600 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
601 if (alps_detect(psmouse, set_properties) == 0) {
602 if (!set_properties || alps_init(psmouse) == 0)
603 return PSMOUSE_ALPS;
604/*
605 * Init failed, try basic relative protocols
606 */
607 max_proto = PSMOUSE_IMEX;
608 }
609 }
610
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500611 if (max_proto > PSMOUSE_IMEX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500613 if (genius_detect(psmouse, set_properties) == 0)
614 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500616 if (ps2pp_init(psmouse, set_properties) == 0)
617 return PSMOUSE_PS2PP;
618
619 if (trackpoint_detect(psmouse, set_properties) == 0)
620 return PSMOUSE_TRACKPOINT;
621
622 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
623 return PSMOUSE_TOUCHKIT_PS2;
624 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/*
627 * Reset to defaults in case the device got confused by extended
Dmitry Torokhovba449952005-12-21 00:51:31 -0500628 * protocol probes. Note that we do full reset becuase some mice
629 * put themselves to sleep when see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
Dmitry Torokhovba449952005-12-21 00:51:31 -0500631 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
634 return PSMOUSE_IMEX;
635
636 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
637 return PSMOUSE_IMPS;
638
639/*
640 * Okay, all failed, we have a standard mouse here. The number of the buttons
641 * is still a question, though. We assume 3.
642 */
643 ps2bare_detect(psmouse, set_properties);
644
645 if (synaptics_hardware) {
646/*
647 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
648 * We need to reset the touchpad because if there is a track point on the
649 * pass through port it could get disabled while probing for protocol
650 * extensions.
651 */
652 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
655 return PSMOUSE_PS2;
656}
657
Helge Dellere38de672006-09-10 21:54:39 -0400658static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500659 {
660 .type = PSMOUSE_PS2,
661 .name = "PS/2",
662 .alias = "bare",
663 .maxproto = 1,
664 .detect = ps2bare_detect,
665 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500666#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500667 {
668 .type = PSMOUSE_PS2PP,
669 .name = "PS2++",
670 .alias = "logitech",
671 .detect = ps2pp_init,
672 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500673#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500674 {
675 .type = PSMOUSE_THINKPS,
676 .name = "ThinkPS/2",
677 .alias = "thinkps",
678 .detect = thinking_detect,
679 },
680 {
681 .type = PSMOUSE_GENPS,
682 .name = "GenPS/2",
683 .alias = "genius",
684 .detect = genius_detect,
685 },
686 {
687 .type = PSMOUSE_IMPS,
688 .name = "ImPS/2",
689 .alias = "imps",
690 .maxproto = 1,
691 .detect = intellimouse_detect,
692 },
693 {
694 .type = PSMOUSE_IMEX,
695 .name = "ImExPS/2",
696 .alias = "exps",
697 .maxproto = 1,
698 .detect = im_explorer_detect,
699 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500700#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500701 {
702 .type = PSMOUSE_SYNAPTICS,
703 .name = "SynPS/2",
704 .alias = "synaptics",
705 .detect = synaptics_detect,
706 .init = synaptics_init,
707 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500708#endif
709#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500710 {
711 .type = PSMOUSE_ALPS,
712 .name = "AlpsPS/2",
713 .alias = "alps",
714 .detect = alps_detect,
715 .init = alps_init,
716 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500717#endif
718#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500719 {
720 .type = PSMOUSE_LIFEBOOK,
721 .name = "LBPS/2",
722 .alias = "lifebook",
723 .init = lifebook_init,
724 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500725#endif
726#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500727 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500728 .type = PSMOUSE_TRACKPOINT,
729 .name = "TPPS/2",
730 .alias = "trackpoint",
731 .detect = trackpoint_detect,
732 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500733#endif
734#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500735 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500736 .type = PSMOUSE_TOUCHKIT_PS2,
737 .name = "touchkitPS/2",
738 .alias = "touchkit",
739 .detect = touchkit_ps2_detect,
740 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500741#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500742 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500743 .type = PSMOUSE_AUTO,
744 .name = "auto",
745 .alias = "any",
746 .maxproto = 1,
747 },
748};
749
Helge Dellere38de672006-09-10 21:54:39 -0400750static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500751{
752 int i;
753
754 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
755 if (psmouse_protocols[i].type == type)
756 return &psmouse_protocols[i];
757
758 WARN_ON(1);
759 return &psmouse_protocols[0];
760}
761
Helge Dellere38de672006-09-10 21:54:39 -0400762static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500763{
Helge Dellere38de672006-09-10 21:54:39 -0400764 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500765 int i;
766
767 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
768 p = &psmouse_protocols[i];
769
770 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
771 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
772 return &psmouse_protocols[i];
773 }
774
775 return NULL;
776}
777
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779/*
780 * psmouse_probe() probes for a PS/2 mouse.
781 */
782
783static int psmouse_probe(struct psmouse *psmouse)
784{
785 struct ps2dev *ps2dev = &psmouse->ps2dev;
786 unsigned char param[2];
787
788/*
789 * First, we check if it's a mouse. It should send 0x00 or 0x03
790 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500791 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
792 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
794
795 param[0] = 0xa5;
796 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
797 return -1;
798
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500799 if (param[0] != 0x00 && param[0] != 0x03 &&
800 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return -1;
802
803/*
804 * Then we reset and disable the mouse so that it doesn't generate events.
805 */
806
807 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
808 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
809
810 return 0;
811}
812
813/*
814 * Here we set the mouse resolution.
815 */
816
817void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
818{
Helge Dellere38de672006-09-10 21:54:39 -0400819 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
820 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (resolution == 0 || resolution > 200)
823 resolution = 200;
824
Helge Dellere38de672006-09-10 21:54:39 -0400825 p = params[resolution / 50];
826 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
827 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830/*
831 * Here we set the mouse report rate.
832 */
833
834static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
835{
Helge Dellere38de672006-09-10 21:54:39 -0400836 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
837 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 int i = 0;
839
840 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400841 r = rates[i];
842 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
843 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846/*
847 * psmouse_initialize() initializes the mouse to a sane state.
848 */
849
850static void psmouse_initialize(struct psmouse *psmouse)
851{
852/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * We set the mouse report rate, resolution and scaling.
854 */
855
856 if (psmouse_max_proto != PSMOUSE_PS2) {
857 psmouse->set_rate(psmouse, psmouse->rate);
858 psmouse->set_resolution(psmouse, psmouse->resolution);
859 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
860 }
861}
862
863/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 * psmouse_activate() enables the mouse so that we get motion reports from it.
865 */
866
867static void psmouse_activate(struct psmouse *psmouse)
868{
869 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
870 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
871 psmouse->ps2dev.serio->phys);
872
873 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
874}
875
876
877/*
878 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
879 * reports from it unless we explicitely request it.
880 */
881
882static void psmouse_deactivate(struct psmouse *psmouse)
883{
884 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
885 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
886 psmouse->ps2dev.serio->phys);
887
888 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
889}
890
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500891/*
892 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
893 */
894
895static int psmouse_poll(struct psmouse *psmouse)
896{
897 return ps2_command(&psmouse->ps2dev, psmouse->packet,
898 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
899}
900
901
902/*
903 * psmouse_resync() attempts to re-validate current protocol.
904 */
905
David Howellsc4028952006-11-22 14:57:56 +0000906static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500907{
David Howellsc4028952006-11-22 14:57:56 +0000908 struct psmouse *parent = NULL, *psmouse =
909 container_of(work, struct psmouse, resync_work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500910 struct serio *serio = psmouse->ps2dev.serio;
911 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
912 int failed = 0, enabled = 0;
913 int i;
914
Ingo Molnarc14471d2006-02-19 00:22:11 -0500915 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500916
917 if (psmouse->state != PSMOUSE_RESYNCING)
918 goto out;
919
920 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
921 parent = serio_get_drvdata(serio->parent);
922 psmouse_deactivate(parent);
923 }
924
925/*
926 * Some mice don't ACK commands sent while they are in the middle of
927 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
928 * instead of ps2_command() which would wait for 200ms for an ACK
929 * that may never come.
930 * As an additional quirk ALPS touchpads may not only forget to ACK
931 * disable command but will stop reporting taps, so if we see that
932 * mouse at least once ACKs disable we will do full reconnect if ACK
933 * is missing.
934 */
935 psmouse->num_resyncs++;
936
937 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
938 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
939 failed = 1;
940 } else
941 psmouse->acks_disable_command = 1;
942
943/*
944 * Poll the mouse. If it was reset the packet will be shorter than
945 * psmouse->pktsize and ps2_command will fail. We do not expect and
946 * do not handle scenario when mouse "upgrades" its protocol while
947 * disconnected since it would require additional delay. If we ever
948 * see a mouse that does it we'll adjust the code.
949 */
950 if (!failed) {
951 if (psmouse->poll(psmouse))
952 failed = 1;
953 else {
954 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
955 for (i = 0; i < psmouse->pktsize; i++) {
956 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +0100957 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500958 if (rc != PSMOUSE_GOOD_DATA)
959 break;
960 }
961 if (rc != PSMOUSE_FULL_PACKET)
962 failed = 1;
963 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
964 }
965 }
966/*
967 * Now try to enable mouse. We try to do that even if poll failed and also
968 * repeat our attempts 5 times, otherwise we may be left out with disabled
969 * mouse.
970 */
971 for (i = 0; i < 5; i++) {
972 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
973 enabled = 1;
974 break;
975 }
976 msleep(200);
977 }
978
979 if (!enabled) {
980 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
981 psmouse->ps2dev.serio->phys);
982 failed = 1;
983 }
984
985 if (failed) {
986 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
987 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
988 serio_reconnect(serio);
989 } else
990 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
991
992 if (parent)
993 psmouse_activate(parent);
994 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -0500995 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500996}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998/*
999 * psmouse_cleanup() resets the mouse into power-on state.
1000 */
1001
1002static void psmouse_cleanup(struct serio *serio)
1003{
1004 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001005 struct psmouse *parent = NULL;
1006
1007 mutex_lock(&psmouse_mutex);
1008
1009 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1010 parent = serio_get_drvdata(serio->parent);
1011 psmouse_deactivate(parent);
1012 }
1013
1014 psmouse_deactivate(psmouse);
1015
1016 if (psmouse->cleanup)
1017 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 psmouse_reset(psmouse);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001020
1021/*
1022 * Some boxes, such as HP nx7400, get terribly confused if mouse
1023 * is not fully enabled before suspending/shutting down.
1024 */
1025 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1026
1027 if (parent) {
1028 if (parent->pt_deactivate)
1029 parent->pt_deactivate(parent);
1030
1031 psmouse_activate(parent);
1032 }
1033
1034 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037/*
1038 * psmouse_disconnect() closes and frees.
1039 */
1040
1041static void psmouse_disconnect(struct serio *serio)
1042{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001043 struct psmouse *psmouse, *parent = NULL;
1044
1045 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001047 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Ingo Molnarc14471d2006-02-19 00:22:11 -05001049 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1052
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001053 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001054 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001055 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001056 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1059 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001060 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
1063 if (psmouse->disconnect)
1064 psmouse->disconnect(psmouse);
1065
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001066 if (parent && parent->pt_deactivate)
1067 parent->pt_deactivate(parent);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 serio_close(serio);
1072 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001073 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001075
1076 if (parent)
1077 psmouse_activate(parent);
1078
Ingo Molnarc14471d2006-02-19 00:22:11 -05001079 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Helge Dellere38de672006-09-10 21:54:39 -04001082static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001083{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001084 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001085
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001086 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001087
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001088 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1089 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1090 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001091
1092 psmouse->set_rate = psmouse_set_rate;
1093 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001094 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001095 psmouse->protocol_handler = psmouse_process_byte;
1096 psmouse->pktsize = 3;
1097
1098 if (proto && (proto->detect || proto->init)) {
1099 if (proto->detect && proto->detect(psmouse, 1) < 0)
1100 return -1;
1101
1102 if (proto->init && proto->init(psmouse) < 0)
1103 return -1;
1104
1105 psmouse->type = proto->type;
1106 }
1107 else
1108 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1109
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001110 /*
1111 * If mouse's packet size is 3 there is no point in polling the
1112 * device in hopes to detect protocol reset - we won't get less
1113 * than 3 bytes response anyhow.
1114 */
1115 if (psmouse->pktsize == 3)
1116 psmouse->resync_time = 0;
1117
1118 /*
1119 * Some smart KVMs fake response to POLL command returning just
1120 * 3 bytes and messing up our resync logic, so if initial poll
1121 * fails we won't try polling the device anymore. Hopefully
1122 * such KVM will maintain initially selected protocol.
1123 */
1124 if (psmouse->resync_time && psmouse->poll(psmouse))
1125 psmouse->resync_time = 0;
1126
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001127 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1128 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001129
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001130 input_dev->name = psmouse->devname;
1131 input_dev->phys = psmouse->phys;
1132 input_dev->id.bustype = BUS_I8042;
1133 input_dev->id.vendor = 0x0002;
1134 input_dev->id.product = psmouse->type;
1135 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001136
1137 return 0;
1138}
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/*
1141 * psmouse_connect() is a callback from the serio module when
1142 * an unhandled serio port is found.
1143 */
1144static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1145{
1146 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001147 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001148 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Ingo Molnarc14471d2006-02-19 00:22:11 -05001150 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /*
1153 * If this is a pass-through port deactivate parent so the device
1154 * connected to this port can be successfully identified
1155 */
1156 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1157 parent = serio_get_drvdata(serio->parent);
1158 psmouse_deactivate(parent);
1159 }
1160
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001161 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1162 input_dev = input_allocate_device();
1163 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001164 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ps2_init(&psmouse->ps2dev, serio);
David Howellsc4028952006-11-22 14:57:56 +00001167 INIT_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001168 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001169 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1172
1173 serio_set_drvdata(serio, psmouse);
1174
Dmitry Torokhov72155612006-11-05 22:40:19 -05001175 error = serio_open(serio, drv);
1176 if (error)
1177 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001180 error = -ENODEV;
1181 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
1184 psmouse->rate = psmouse_rate;
1185 psmouse->resolution = psmouse_resolution;
1186 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001187 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001190 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 psmouse_initialize(psmouse);
1194
Dmitry Torokhov72155612006-11-05 22:40:19 -05001195 error = input_register_device(psmouse->dev);
1196 if (error)
1197 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (parent && parent->pt_activate)
1200 parent->pt_activate(parent);
1201
Dmitry Torokhov72155612006-11-05 22:40:19 -05001202 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1203 if (error)
1204 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 psmouse_activate(psmouse);
1207
Dmitry Torokhov72155612006-11-05 22:40:19 -05001208 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001209 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (parent)
1211 psmouse_activate(parent);
1212
Ingo Molnarc14471d2006-02-19 00:22:11 -05001213 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001215
1216 err_pt_deactivate:
1217 if (parent && parent->pt_deactivate)
1218 parent->pt_deactivate(parent);
1219 err_protocol_disconnect:
1220 if (psmouse->disconnect)
1221 psmouse->disconnect(psmouse);
1222 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1223 err_close_serio:
1224 serio_close(serio);
1225 err_clear_drvdata:
1226 serio_set_drvdata(serio, NULL);
1227 err_free:
1228 input_free_device(input_dev);
1229 kfree(psmouse);
1230
1231 retval = error;
1232 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235
1236static int psmouse_reconnect(struct serio *serio)
1237{
1238 struct psmouse *psmouse = serio_get_drvdata(serio);
1239 struct psmouse *parent = NULL;
1240 struct serio_driver *drv = serio->drv;
1241 int rc = -1;
1242
1243 if (!drv || !psmouse) {
1244 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1245 return -1;
1246 }
1247
Ingo Molnarc14471d2006-02-19 00:22:11 -05001248 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1251 parent = serio_get_drvdata(serio->parent);
1252 psmouse_deactivate(parent);
1253 }
1254
1255 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1256
1257 if (psmouse->reconnect) {
1258 if (psmouse->reconnect(psmouse))
1259 goto out;
1260 } else if (psmouse_probe(psmouse) < 0 ||
1261 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1262 goto out;
1263
1264 /* ok, the device type (and capabilities) match the old one,
1265 * we can continue using it, complete intialization
1266 */
1267 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1268
1269 psmouse_initialize(psmouse);
1270
1271 if (parent && parent->pt_activate)
1272 parent->pt_activate(parent);
1273
1274 psmouse_activate(psmouse);
1275 rc = 0;
1276
1277out:
1278 /* If this is a pass-through port the parent waits to be activated */
1279 if (parent)
1280 psmouse_activate(parent);
1281
Ingo Molnarc14471d2006-02-19 00:22:11 -05001282 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return rc;
1284}
1285
1286static struct serio_device_id psmouse_serio_ids[] = {
1287 {
1288 .type = SERIO_8042,
1289 .proto = SERIO_ANY,
1290 .id = SERIO_ANY,
1291 .extra = SERIO_ANY,
1292 },
1293 {
1294 .type = SERIO_PS_PSTHRU,
1295 .proto = SERIO_ANY,
1296 .id = SERIO_ANY,
1297 .extra = SERIO_ANY,
1298 },
1299 { 0 }
1300};
1301
1302MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1303
1304static struct serio_driver psmouse_drv = {
1305 .driver = {
1306 .name = "psmouse",
1307 },
1308 .description = DRIVER_DESC,
1309 .id_table = psmouse_serio_ids,
1310 .interrupt = psmouse_interrupt,
1311 .connect = psmouse_connect,
1312 .reconnect = psmouse_reconnect,
1313 .disconnect = psmouse_disconnect,
1314 .cleanup = psmouse_cleanup,
1315};
1316
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001317ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1318 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001321 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1322 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int retval;
1324
1325 retval = serio_pin_driver(serio);
1326 if (retval)
1327 return retval;
1328
1329 if (serio->drv != &psmouse_drv) {
1330 retval = -ENODEV;
1331 goto out;
1332 }
1333
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001334 psmouse = serio_get_drvdata(serio);
1335
1336 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338out:
1339 serio_unpin_driver(serio);
1340 return retval;
1341}
1342
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001343ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1344 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001347 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1348 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int retval;
1350
1351 retval = serio_pin_driver(serio);
1352 if (retval)
1353 return retval;
1354
1355 if (serio->drv != &psmouse_drv) {
1356 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001357 goto out_unpin;
1358 }
1359
Ingo Molnarc14471d2006-02-19 00:22:11 -05001360 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001361 if (retval)
1362 goto out_unpin;
1363
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001364 psmouse = serio_get_drvdata(serio);
1365
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001366 if (psmouse->state == PSMOUSE_IGNORE) {
1367 retval = -ENODEV;
Ingo Molnarc14471d2006-02-19 00:22:11 -05001368 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
1371 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1372 parent = serio_get_drvdata(serio->parent);
1373 psmouse_deactivate(parent);
1374 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 psmouse_deactivate(psmouse);
1377
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001378 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001380 if (retval != -ENODEV)
1381 psmouse_activate(psmouse);
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (parent)
1384 psmouse_activate(parent);
1385
Ingo Molnarc14471d2006-02-19 00:22:11 -05001386 out_unlock:
1387 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001388 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 serio_unpin_driver(serio);
1390 return retval;
1391}
1392
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001393static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1394{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001395 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001396
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001397 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001398}
1399
1400static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1401{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001402 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001403 unsigned long value;
1404 char *rest;
1405
1406 value = simple_strtoul(buf, &rest, 10);
1407 if (*rest)
1408 return -EINVAL;
1409
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001410 if ((unsigned int)value != value)
1411 return -EINVAL;
1412
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001413 *field = value;
1414
1415 return count;
1416}
1417
1418static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001419{
1420 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1421}
1422
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001423static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001424{
1425 struct serio *serio = psmouse->ps2dev.serio;
1426 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001427 struct input_dev *old_dev, *new_dev;
1428 const struct psmouse_protocol *proto, *old_proto;
1429 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001430 int retry = 0;
1431
Dmitry Torokhov72155612006-11-05 22:40:19 -05001432 proto = psmouse_protocol_by_name(buf, count);
1433 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001434 return -EINVAL;
1435
1436 if (psmouse->type == proto->type)
1437 return count;
1438
Dmitry Torokhov72155612006-11-05 22:40:19 -05001439 new_dev = input_allocate_device();
1440 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001441 return -ENOMEM;
1442
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001443 while (serio->child) {
1444 if (++retry > 3) {
1445 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001446 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001447 return -EIO;
1448 }
1449
Ingo Molnarc14471d2006-02-19 00:22:11 -05001450 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001451 serio_unpin_driver(serio);
1452 serio_unregister_child_port(serio);
1453 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001454 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001455
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001456 if (serio->drv != &psmouse_drv) {
1457 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001458 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001459 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001460
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001461 if (psmouse->type == proto->type) {
1462 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001463 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001464 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001465 }
1466
1467 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1468 parent = serio_get_drvdata(serio->parent);
1469 if (parent->pt_deactivate)
1470 parent->pt_deactivate(parent);
1471 }
1472
Dmitry Torokhov72155612006-11-05 22:40:19 -05001473 old_dev = psmouse->dev;
1474 old_proto = psmouse_protocol_by_type(psmouse->type);
1475
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001476 if (psmouse->disconnect)
1477 psmouse->disconnect(psmouse);
1478
1479 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001480
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001481 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001482 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1483
1484 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1485 psmouse_reset(psmouse);
1486 /* default to PSMOUSE_PS2 */
1487 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1488 }
1489
1490 psmouse_initialize(psmouse);
1491 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1492
Dmitry Torokhov72155612006-11-05 22:40:19 -05001493 error = input_register_device(psmouse->dev);
1494 if (error) {
1495 if (psmouse->disconnect)
1496 psmouse->disconnect(psmouse);
1497
1498 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1499 input_free_device(new_dev);
1500 psmouse->dev = old_dev;
1501 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1502 psmouse_switch_protocol(psmouse, old_proto);
1503 psmouse_initialize(psmouse);
1504 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1505
1506 return error;
1507 }
1508
1509 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001510
1511 if (parent && parent->pt_activate)
1512 parent->pt_activate(parent);
1513
1514 return count;
1515}
1516
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001517static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 unsigned long value;
1520 char *rest;
1521
1522 value = simple_strtoul(buf, &rest, 10);
1523 if (*rest)
1524 return -EINVAL;
1525
1526 psmouse->set_rate(psmouse, value);
1527 return count;
1528}
1529
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001530static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 unsigned long value;
1533 char *rest;
1534
1535 value = simple_strtoul(buf, &rest, 10);
1536 if (*rest)
1537 return -EINVAL;
1538
1539 psmouse->set_resolution(psmouse, value);
1540 return count;
1541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1545{
Helge Dellere38de672006-09-10 21:54:39 -04001546 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 if (!val)
1549 return -EINVAL;
1550
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001551 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001553 if (!proto || !proto->maxproto)
1554 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001556 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Stephen Evanchik541e3162005-08-08 01:26:18 -05001558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1562{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001563 int type = *((unsigned int *)kp->arg);
1564
1565 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
1568static int __init psmouse_init(void)
1569{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001570 int err;
1571
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001572 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1573 if (!kpsmoused_wq) {
1574 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1575 return -ENOMEM;
1576 }
1577
Akinobu Mita153a9df02006-11-23 23:35:10 -05001578 err = serio_register_driver(&psmouse_drv);
1579 if (err)
1580 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001581
Akinobu Mita153a9df02006-11-23 23:35:10 -05001582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585static void __exit psmouse_exit(void)
1586{
1587 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001588 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
1591module_init(psmouse_init);
1592module_exit(psmouse_exit);