blob: 126e977e199e00764d3f2ab32aafbd0e26fbe7e8 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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>
Ingo Molnarc14471d2006-02-19 00:22:11 -050022#include <linux/mutex.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "psmouse.h"
25#include "synaptics.h"
26#include "logips2pp.h"
27#include "alps.h"
Andres Salomondf08ef22008-09-16 12:30:34 -040028#include "hgpk.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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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/*
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400181 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
182 * byte.
183 */
184 if (psmouse->type == PSMOUSE_CORTRON) {
185 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
186 packet[0] |= 0x08;
187 }
188
189/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * Generic PS/2 Mouse
191 */
192
193 input_report_key(dev, BTN_LEFT, packet[0] & 1);
194 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
195 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
196
197 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
198 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
199
200 input_sync(dev);
201
202 return PSMOUSE_FULL_PACKET;
203}
204
Andres Salomon8bf020e2008-09-16 12:30:33 -0400205void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
206 unsigned long delay)
207{
208 queue_delayed_work(kpsmoused_wq, work, delay);
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500212 * __psmouse_set_state() sets new psmouse state and resets all flags.
213 */
214
215static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
216{
217 psmouse->state = new_state;
218 psmouse->pktcnt = psmouse->out_of_sync = 0;
219 psmouse->ps2dev.flags = 0;
220 psmouse->last = jiffies;
221}
222
223
224/*
225 * psmouse_set_state() sets new psmouse state and resets all flags and
226 * counters while holding serio lock so fighting with interrupt handler
227 * is not a concern.
228 */
229
Andres Salomona48cf5f2008-09-16 12:30:33 -0400230void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500231{
232 serio_pause_rx(psmouse->ps2dev.serio);
233 __psmouse_set_state(psmouse, new_state);
234 serio_continue_rx(psmouse->ps2dev.serio);
235}
236
237/*
238 * psmouse_handle_byte() processes one byte of the input data stream
239 * by calling corresponding protocol handler.
240 */
241
David Howells7d12e782006-10-05 14:55:46 +0100242static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500243{
David Howells7d12e782006-10-05 14:55:46 +0100244 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500245
246 switch (rc) {
247 case PSMOUSE_BAD_DATA:
248 if (psmouse->state == PSMOUSE_ACTIVATED) {
249 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
250 psmouse->name, psmouse->phys, psmouse->pktcnt);
251 if (++psmouse->out_of_sync == psmouse->resetafter) {
252 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
253 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
254 serio_reconnect(psmouse->ps2dev.serio);
255 return -1;
256 }
257 }
258 psmouse->pktcnt = 0;
259 break;
260
261 case PSMOUSE_FULL_PACKET:
262 psmouse->pktcnt = 0;
263 if (psmouse->out_of_sync) {
264 psmouse->out_of_sync = 0;
265 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
266 psmouse->name, psmouse->phys);
267 }
268 break;
269
270 case PSMOUSE_GOOD_DATA:
271 break;
272 }
273 return 0;
274}
275
276/*
277 * psmouse_interrupt() handles incoming characters, either passing them
278 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280
281static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100282 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (psmouse->state == PSMOUSE_IGNORE)
287 goto out;
288
289 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
290 if (psmouse->state == PSMOUSE_ACTIVATED)
291 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
292 flags & SERIO_TIMEOUT ? " timeout" : "",
293 flags & SERIO_PARITY ? " bad parity" : "");
294 ps2_cmd_aborted(&psmouse->ps2dev);
295 goto out;
296 }
297
298 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
299 if (ps2_handle_ack(&psmouse->ps2dev, data))
300 goto out;
301
302 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
303 if (ps2_handle_response(&psmouse->ps2dev, data))
304 goto out;
305
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500306 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 goto out;
308
309 if (psmouse->state == PSMOUSE_ACTIVATED &&
310 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500311 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500313 psmouse->badbyte = psmouse->packet[0];
314 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400315 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500316 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500320/*
321 * Check if this is a new device announcement (0xAA 0x00)
322 */
323 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400324 if (psmouse->pktcnt == 1) {
325 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500329 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
330 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
331 serio_reconnect(serio);
332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500334/*
335 * Not a new device, try processing first byte normally
336 */
337 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100338 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500339 goto out;
340
341 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500344/*
345 * See if we need to force resync because mouse was idle for too long
346 */
347 if (psmouse->state == PSMOUSE_ACTIVATED &&
348 psmouse->pktcnt == 1 && psmouse->resync_time &&
349 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
350 psmouse->badbyte = psmouse->packet[0];
351 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400352 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500355
356 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100357 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500358
359 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return IRQ_HANDLED;
361}
362
363
364/*
365 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
366 * using sliced syntax, understood by advanced devices, such as Logitech
367 * or Synaptics touchpads. The command is encoded as:
368 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
369 * is the command.
370 */
371int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
372{
373 int i;
374
375 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
376 return -1;
377
378 for (i = 6; i >= 0; i -= 2) {
379 unsigned char d = (command >> i) & 3;
380 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
381 return -1;
382 }
383
384 return 0;
385}
386
387
388/*
389 * psmouse_reset() resets the mouse into power-on state.
390 */
391int psmouse_reset(struct psmouse *psmouse)
392{
393 unsigned char param[2];
394
395 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
396 return -1;
397
398 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
399 return -1;
400
401 return 0;
402}
403
404
405/*
406 * Genius NetMouse magic init.
407 */
408static int genius_detect(struct psmouse *psmouse, int set_properties)
409{
410 struct ps2dev *ps2dev = &psmouse->ps2dev;
411 unsigned char param[4];
412
413 param[0] = 3;
414 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
415 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
416 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
417 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
418 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
419
420 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
421 return -1;
422
423 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500424 set_bit(BTN_EXTRA, psmouse->dev->keybit);
425 set_bit(BTN_SIDE, psmouse->dev->keybit);
426 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500429 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 psmouse->pktsize = 4;
431 }
432
433 return 0;
434}
435
436/*
437 * IntelliMouse magic init.
438 */
439static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
440{
441 struct ps2dev *ps2dev = &psmouse->ps2dev;
442 unsigned char param[2];
443
444 param[0] = 200;
445 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
446 param[0] = 100;
447 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
448 param[0] = 80;
449 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
450 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
451
452 if (param[0] != 3)
453 return -1;
454
455 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500456 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
457 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if (!psmouse->vendor) psmouse->vendor = "Generic";
460 if (!psmouse->name) psmouse->name = "Wheel Mouse";
461 psmouse->pktsize = 4;
462 }
463
464 return 0;
465}
466
467/*
468 * Try IntelliMouse/Explorer magic init.
469 */
470static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
471{
472 struct ps2dev *ps2dev = &psmouse->ps2dev;
473 unsigned char param[2];
474
475 intellimouse_detect(psmouse, 0);
476
477 param[0] = 200;
478 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
479 param[0] = 200;
480 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
481 param[0] = 80;
482 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
483 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
484
485 if (param[0] != 4)
486 return -1;
487
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400488/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
489 param[0] = 200;
490 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
491 param[0] = 80;
492 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
493 param[0] = 40;
494 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500497 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
498 set_bit(REL_WHEEL, psmouse->dev->relbit);
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400499 set_bit(REL_HWHEEL, psmouse->dev->relbit);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500500 set_bit(BTN_SIDE, psmouse->dev->keybit);
501 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 if (!psmouse->vendor) psmouse->vendor = "Generic";
504 if (!psmouse->name) psmouse->name = "Explorer Mouse";
505 psmouse->pktsize = 4;
506 }
507
508 return 0;
509}
510
511/*
512 * Kensington ThinkingMouse / ExpertMouse magic init.
513 */
514static int thinking_detect(struct psmouse *psmouse, int set_properties)
515{
516 struct ps2dev *ps2dev = &psmouse->ps2dev;
517 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400518 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int i;
520
521 param[0] = 10;
522 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
523 param[0] = 0;
524 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400525 for (i = 0; i < ARRAY_SIZE(seq); i++) {
526 param[0] = seq[i];
527 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
530
531 if (param[0] != 2)
532 return -1;
533
534 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500535 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 psmouse->vendor = "Kensington";
538 psmouse->name = "ThinkingMouse";
539 }
540
541 return 0;
542}
543
544/*
545 * Bare PS/2 protocol "detection". Always succeeds.
546 */
547static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
548{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500549 if (set_properties) {
550 if (!psmouse->vendor) psmouse->vendor = "Generic";
551 if (!psmouse->name) psmouse->name = "Mouse";
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 return 0;
555}
556
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400557/*
558 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
559 * must be forced by sysfs protocol writing.
560 */
561static int cortron_detect(struct psmouse *psmouse, int set_properties)
562{
563 if (set_properties) {
564 psmouse->vendor = "Cortron";
565 psmouse->name = "PS/2 Trackball";
566 set_bit(BTN_SIDE, psmouse->dev->keybit);
567 }
568
569 return 0;
570}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/*
573 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
574 * the mouse may have.
575 */
576
577static int psmouse_extensions(struct psmouse *psmouse,
578 unsigned int max_proto, int set_properties)
579{
580 int synaptics_hardware = 0;
581
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500582/*
583 * We always check for lifebook because it does not disturb mouse
584 * (it only checks DMI information).
585 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500586 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500587 if (max_proto > PSMOUSE_IMEX) {
588 if (!set_properties || lifebook_init(psmouse) == 0)
589 return PSMOUSE_LIFEBOOK;
590 }
591 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/*
594 * Try Kensington ThinkingMouse (we try first, because synaptics probe
595 * upsets the thinkingmouse).
596 */
597
598 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
599 return PSMOUSE_THINKPS;
600
601/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500602 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
603 * support is disabled in config - we need to know if it is synaptics so we
604 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
606 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
607 synaptics_hardware = 1;
608
609 if (max_proto > PSMOUSE_IMEX) {
610 if (!set_properties || synaptics_init(psmouse) == 0)
611 return PSMOUSE_SYNAPTICS;
612/*
613 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
614 * Unfortunately Logitech/Genius probes confuse some firmware versions so
615 * we'll have to skip them.
616 */
617 max_proto = PSMOUSE_IMEX;
618 }
619/*
620 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
621 */
622 synaptics_reset(psmouse);
623 }
624
625/*
626 * Try ALPS TouchPad
627 */
628 if (max_proto > PSMOUSE_IMEX) {
629 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
630 if (alps_detect(psmouse, set_properties) == 0) {
631 if (!set_properties || alps_init(psmouse) == 0)
632 return PSMOUSE_ALPS;
633/*
634 * Init failed, try basic relative protocols
635 */
636 max_proto = PSMOUSE_IMEX;
637 }
638 }
639
Andres Salomondf08ef22008-09-16 12:30:34 -0400640/*
641 * Try OLPC HGPK touchpad.
642 */
643 if (max_proto > PSMOUSE_IMEX &&
644 hgpk_detect(psmouse, set_properties) == 0) {
645 if (!set_properties || hgpk_init(psmouse) == 0)
646 return PSMOUSE_HGPK;
647/*
648 * Init failed, try basic relative protocols
649 */
650 max_proto = PSMOUSE_IMEX;
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Andres Salomondf08ef22008-09-16 12:30:34 -0400653 if (max_proto > PSMOUSE_IMEX) {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500654 if (genius_detect(psmouse, set_properties) == 0)
655 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500657 if (ps2pp_init(psmouse, set_properties) == 0)
658 return PSMOUSE_PS2PP;
659
660 if (trackpoint_detect(psmouse, set_properties) == 0)
661 return PSMOUSE_TRACKPOINT;
662
663 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
664 return PSMOUSE_TOUCHKIT_PS2;
665 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/*
668 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400669 * protocol probes. Note that we follow up with full reset because
670 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Alon Ziv554fc192007-08-30 00:22:48 -0400672 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500673 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
676 return PSMOUSE_IMEX;
677
678 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
679 return PSMOUSE_IMPS;
680
681/*
682 * Okay, all failed, we have a standard mouse here. The number of the buttons
683 * is still a question, though. We assume 3.
684 */
685 ps2bare_detect(psmouse, set_properties);
686
687 if (synaptics_hardware) {
688/*
689 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
690 * We need to reset the touchpad because if there is a track point on the
691 * pass through port it could get disabled while probing for protocol
692 * extensions.
693 */
694 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 return PSMOUSE_PS2;
698}
699
Helge Dellere38de672006-09-10 21:54:39 -0400700static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500701 {
702 .type = PSMOUSE_PS2,
703 .name = "PS/2",
704 .alias = "bare",
705 .maxproto = 1,
706 .detect = ps2bare_detect,
707 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500708#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500709 {
710 .type = PSMOUSE_PS2PP,
711 .name = "PS2++",
712 .alias = "logitech",
713 .detect = ps2pp_init,
714 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500715#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500716 {
717 .type = PSMOUSE_THINKPS,
718 .name = "ThinkPS/2",
719 .alias = "thinkps",
720 .detect = thinking_detect,
721 },
722 {
723 .type = PSMOUSE_GENPS,
724 .name = "GenPS/2",
725 .alias = "genius",
726 .detect = genius_detect,
727 },
728 {
729 .type = PSMOUSE_IMPS,
730 .name = "ImPS/2",
731 .alias = "imps",
732 .maxproto = 1,
733 .detect = intellimouse_detect,
734 },
735 {
736 .type = PSMOUSE_IMEX,
737 .name = "ImExPS/2",
738 .alias = "exps",
739 .maxproto = 1,
740 .detect = im_explorer_detect,
741 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500742#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500743 {
744 .type = PSMOUSE_SYNAPTICS,
745 .name = "SynPS/2",
746 .alias = "synaptics",
747 .detect = synaptics_detect,
748 .init = synaptics_init,
749 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500750#endif
751#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500752 {
753 .type = PSMOUSE_ALPS,
754 .name = "AlpsPS/2",
755 .alias = "alps",
756 .detect = alps_detect,
757 .init = alps_init,
758 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500759#endif
760#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500761 {
762 .type = PSMOUSE_LIFEBOOK,
763 .name = "LBPS/2",
764 .alias = "lifebook",
765 .init = lifebook_init,
766 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500767#endif
768#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500769 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500770 .type = PSMOUSE_TRACKPOINT,
771 .name = "TPPS/2",
772 .alias = "trackpoint",
773 .detect = trackpoint_detect,
774 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500775#endif
776#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500777 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500778 .type = PSMOUSE_TOUCHKIT_PS2,
779 .name = "touchkitPS/2",
780 .alias = "touchkit",
781 .detect = touchkit_ps2_detect,
782 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500783#endif
Andres Salomondf08ef22008-09-16 12:30:34 -0400784#ifdef CONFIG_MOUSE_PS2_OLPC
785 {
786 .type = PSMOUSE_HGPK,
787 .name = "OLPC HGPK",
788 .alias = "hgpk",
789 .detect = hgpk_detect,
790 },
791#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500792 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400793 .type = PSMOUSE_CORTRON,
794 .name = "CortronPS/2",
795 .alias = "cortps",
796 .detect = cortron_detect,
797 },
798 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500799 .type = PSMOUSE_AUTO,
800 .name = "auto",
801 .alias = "any",
802 .maxproto = 1,
803 },
804};
805
Helge Dellere38de672006-09-10 21:54:39 -0400806static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500807{
808 int i;
809
810 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
811 if (psmouse_protocols[i].type == type)
812 return &psmouse_protocols[i];
813
814 WARN_ON(1);
815 return &psmouse_protocols[0];
816}
817
Helge Dellere38de672006-09-10 21:54:39 -0400818static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500819{
Helge Dellere38de672006-09-10 21:54:39 -0400820 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500821 int i;
822
823 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
824 p = &psmouse_protocols[i];
825
826 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
827 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
828 return &psmouse_protocols[i];
829 }
830
831 return NULL;
832}
833
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835/*
836 * psmouse_probe() probes for a PS/2 mouse.
837 */
838
839static int psmouse_probe(struct psmouse *psmouse)
840{
841 struct ps2dev *ps2dev = &psmouse->ps2dev;
842 unsigned char param[2];
843
844/*
845 * First, we check if it's a mouse. It should send 0x00 or 0x03
846 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500847 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
848 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
850
851 param[0] = 0xa5;
852 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
853 return -1;
854
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500855 if (param[0] != 0x00 && param[0] != 0x03 &&
856 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return -1;
858
859/*
860 * Then we reset and disable the mouse so that it doesn't generate events.
861 */
862
863 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
864 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
865
866 return 0;
867}
868
869/*
870 * Here we set the mouse resolution.
871 */
872
873void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
874{
Helge Dellere38de672006-09-10 21:54:39 -0400875 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
876 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 if (resolution == 0 || resolution > 200)
879 resolution = 200;
880
Helge Dellere38de672006-09-10 21:54:39 -0400881 p = params[resolution / 50];
882 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
883 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886/*
887 * Here we set the mouse report rate.
888 */
889
890static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
891{
Helge Dellere38de672006-09-10 21:54:39 -0400892 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
893 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 int i = 0;
895
896 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400897 r = rates[i];
898 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
899 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902/*
903 * psmouse_initialize() initializes the mouse to a sane state.
904 */
905
906static void psmouse_initialize(struct psmouse *psmouse)
907{
908/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * We set the mouse report rate, resolution and scaling.
910 */
911
912 if (psmouse_max_proto != PSMOUSE_PS2) {
913 psmouse->set_rate(psmouse, psmouse->rate);
914 psmouse->set_resolution(psmouse, psmouse->resolution);
915 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
916 }
917}
918
919/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 * psmouse_activate() enables the mouse so that we get motion reports from it.
921 */
922
923static void psmouse_activate(struct psmouse *psmouse)
924{
925 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
926 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
927 psmouse->ps2dev.serio->phys);
928
929 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
930}
931
932
933/*
934 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +0200935 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
937
938static void psmouse_deactivate(struct psmouse *psmouse)
939{
940 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
941 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
942 psmouse->ps2dev.serio->phys);
943
944 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
945}
946
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500947/*
948 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
949 */
950
951static int psmouse_poll(struct psmouse *psmouse)
952{
953 return ps2_command(&psmouse->ps2dev, psmouse->packet,
954 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
955}
956
957
958/*
959 * psmouse_resync() attempts to re-validate current protocol.
960 */
961
David Howellsc4028952006-11-22 14:57:56 +0000962static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500963{
David Howellsc4028952006-11-22 14:57:56 +0000964 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -0400965 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500966 struct serio *serio = psmouse->ps2dev.serio;
967 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
968 int failed = 0, enabled = 0;
969 int i;
970
Ingo Molnarc14471d2006-02-19 00:22:11 -0500971 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500972
973 if (psmouse->state != PSMOUSE_RESYNCING)
974 goto out;
975
976 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
977 parent = serio_get_drvdata(serio->parent);
978 psmouse_deactivate(parent);
979 }
980
981/*
982 * Some mice don't ACK commands sent while they are in the middle of
983 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
984 * instead of ps2_command() which would wait for 200ms for an ACK
985 * that may never come.
986 * As an additional quirk ALPS touchpads may not only forget to ACK
987 * disable command but will stop reporting taps, so if we see that
988 * mouse at least once ACKs disable we will do full reconnect if ACK
989 * is missing.
990 */
991 psmouse->num_resyncs++;
992
993 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
994 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
995 failed = 1;
996 } else
997 psmouse->acks_disable_command = 1;
998
999/*
1000 * Poll the mouse. If it was reset the packet will be shorter than
1001 * psmouse->pktsize and ps2_command will fail. We do not expect and
1002 * do not handle scenario when mouse "upgrades" its protocol while
1003 * disconnected since it would require additional delay. If we ever
1004 * see a mouse that does it we'll adjust the code.
1005 */
1006 if (!failed) {
1007 if (psmouse->poll(psmouse))
1008 failed = 1;
1009 else {
1010 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1011 for (i = 0; i < psmouse->pktsize; i++) {
1012 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +01001013 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001014 if (rc != PSMOUSE_GOOD_DATA)
1015 break;
1016 }
1017 if (rc != PSMOUSE_FULL_PACKET)
1018 failed = 1;
1019 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1020 }
1021 }
1022/*
1023 * Now try to enable mouse. We try to do that even if poll failed and also
1024 * repeat our attempts 5 times, otherwise we may be left out with disabled
1025 * mouse.
1026 */
1027 for (i = 0; i < 5; i++) {
1028 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1029 enabled = 1;
1030 break;
1031 }
1032 msleep(200);
1033 }
1034
1035 if (!enabled) {
1036 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1037 psmouse->ps2dev.serio->phys);
1038 failed = 1;
1039 }
1040
1041 if (failed) {
1042 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1043 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1044 serio_reconnect(serio);
1045 } else
1046 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1047
1048 if (parent)
1049 psmouse_activate(parent);
1050 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001051 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054/*
1055 * psmouse_cleanup() resets the mouse into power-on state.
1056 */
1057
1058static void psmouse_cleanup(struct serio *serio)
1059{
1060 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001061 struct psmouse *parent = NULL;
1062
1063 mutex_lock(&psmouse_mutex);
1064
1065 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1066 parent = serio_get_drvdata(serio->parent);
1067 psmouse_deactivate(parent);
1068 }
1069
1070 psmouse_deactivate(psmouse);
1071
1072 if (psmouse->cleanup)
1073 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 psmouse_reset(psmouse);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001076
1077/*
1078 * Some boxes, such as HP nx7400, get terribly confused if mouse
1079 * is not fully enabled before suspending/shutting down.
1080 */
1081 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1082
1083 if (parent) {
1084 if (parent->pt_deactivate)
1085 parent->pt_deactivate(parent);
1086
1087 psmouse_activate(parent);
1088 }
1089
1090 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093/*
1094 * psmouse_disconnect() closes and frees.
1095 */
1096
1097static void psmouse_disconnect(struct serio *serio)
1098{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001099 struct psmouse *psmouse, *parent = NULL;
1100
1101 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001103 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Ingo Molnarc14471d2006-02-19 00:22:11 -05001105 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1108
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001109 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001110 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001111 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001112 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1115 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001116 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
1119 if (psmouse->disconnect)
1120 psmouse->disconnect(psmouse);
1121
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001122 if (parent && parent->pt_deactivate)
1123 parent->pt_deactivate(parent);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 serio_close(serio);
1128 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001129 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001131
1132 if (parent)
1133 psmouse_activate(parent);
1134
Ingo Molnarc14471d2006-02-19 00:22:11 -05001135 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Helge Dellere38de672006-09-10 21:54:39 -04001138static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001139{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001140 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001141
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001142 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001143
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001144 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1145 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
1146 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1147 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001148
1149 psmouse->set_rate = psmouse_set_rate;
1150 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001151 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001152 psmouse->protocol_handler = psmouse_process_byte;
1153 psmouse->pktsize = 3;
1154
1155 if (proto && (proto->detect || proto->init)) {
1156 if (proto->detect && proto->detect(psmouse, 1) < 0)
1157 return -1;
1158
1159 if (proto->init && proto->init(psmouse) < 0)
1160 return -1;
1161
1162 psmouse->type = proto->type;
1163 }
1164 else
1165 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1166
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001167 /*
1168 * If mouse's packet size is 3 there is no point in polling the
1169 * device in hopes to detect protocol reset - we won't get less
1170 * than 3 bytes response anyhow.
1171 */
1172 if (psmouse->pktsize == 3)
1173 psmouse->resync_time = 0;
1174
1175 /*
1176 * Some smart KVMs fake response to POLL command returning just
1177 * 3 bytes and messing up our resync logic, so if initial poll
1178 * fails we won't try polling the device anymore. Hopefully
1179 * such KVM will maintain initially selected protocol.
1180 */
1181 if (psmouse->resync_time && psmouse->poll(psmouse))
1182 psmouse->resync_time = 0;
1183
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001184 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1185 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001186
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001187 input_dev->name = psmouse->devname;
1188 input_dev->phys = psmouse->phys;
1189 input_dev->id.bustype = BUS_I8042;
1190 input_dev->id.vendor = 0x0002;
1191 input_dev->id.product = psmouse->type;
1192 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001193
1194 return 0;
1195}
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/*
1198 * psmouse_connect() is a callback from the serio module when
1199 * an unhandled serio port is found.
1200 */
1201static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1202{
1203 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001204 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001205 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Ingo Molnarc14471d2006-02-19 00:22:11 -05001207 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /*
1210 * If this is a pass-through port deactivate parent so the device
1211 * connected to this port can be successfully identified
1212 */
1213 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1214 parent = serio_get_drvdata(serio->parent);
1215 psmouse_deactivate(parent);
1216 }
1217
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001218 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1219 input_dev = input_allocate_device();
1220 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001221 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001224 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001225 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001226 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1229
1230 serio_set_drvdata(serio, psmouse);
1231
Dmitry Torokhov72155612006-11-05 22:40:19 -05001232 error = serio_open(serio, drv);
1233 if (error)
1234 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001237 error = -ENODEV;
1238 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
1241 psmouse->rate = psmouse_rate;
1242 psmouse->resolution = psmouse_resolution;
1243 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001244 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001247 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 psmouse_initialize(psmouse);
1251
Dmitry Torokhov72155612006-11-05 22:40:19 -05001252 error = input_register_device(psmouse->dev);
1253 if (error)
1254 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (parent && parent->pt_activate)
1257 parent->pt_activate(parent);
1258
Dmitry Torokhov72155612006-11-05 22:40:19 -05001259 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1260 if (error)
1261 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 psmouse_activate(psmouse);
1264
Dmitry Torokhov72155612006-11-05 22:40:19 -05001265 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001266 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (parent)
1268 psmouse_activate(parent);
1269
Ingo Molnarc14471d2006-02-19 00:22:11 -05001270 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001272
1273 err_pt_deactivate:
1274 if (parent && parent->pt_deactivate)
1275 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001276 input_unregister_device(psmouse->dev);
1277 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001278 err_protocol_disconnect:
1279 if (psmouse->disconnect)
1280 psmouse->disconnect(psmouse);
1281 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1282 err_close_serio:
1283 serio_close(serio);
1284 err_clear_drvdata:
1285 serio_set_drvdata(serio, NULL);
1286 err_free:
1287 input_free_device(input_dev);
1288 kfree(psmouse);
1289
1290 retval = error;
1291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294
1295static int psmouse_reconnect(struct serio *serio)
1296{
1297 struct psmouse *psmouse = serio_get_drvdata(serio);
1298 struct psmouse *parent = NULL;
1299 struct serio_driver *drv = serio->drv;
1300 int rc = -1;
1301
1302 if (!drv || !psmouse) {
1303 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1304 return -1;
1305 }
1306
Ingo Molnarc14471d2006-02-19 00:22:11 -05001307 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1310 parent = serio_get_drvdata(serio->parent);
1311 psmouse_deactivate(parent);
1312 }
1313
1314 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1315
1316 if (psmouse->reconnect) {
1317 if (psmouse->reconnect(psmouse))
1318 goto out;
1319 } else if (psmouse_probe(psmouse) < 0 ||
1320 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1321 goto out;
1322
1323 /* ok, the device type (and capabilities) match the old one,
1324 * we can continue using it, complete intialization
1325 */
1326 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1327
1328 psmouse_initialize(psmouse);
1329
1330 if (parent && parent->pt_activate)
1331 parent->pt_activate(parent);
1332
1333 psmouse_activate(psmouse);
1334 rc = 0;
1335
1336out:
1337 /* If this is a pass-through port the parent waits to be activated */
1338 if (parent)
1339 psmouse_activate(parent);
1340
Ingo Molnarc14471d2006-02-19 00:22:11 -05001341 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return rc;
1343}
1344
1345static struct serio_device_id psmouse_serio_ids[] = {
1346 {
1347 .type = SERIO_8042,
1348 .proto = SERIO_ANY,
1349 .id = SERIO_ANY,
1350 .extra = SERIO_ANY,
1351 },
1352 {
1353 .type = SERIO_PS_PSTHRU,
1354 .proto = SERIO_ANY,
1355 .id = SERIO_ANY,
1356 .extra = SERIO_ANY,
1357 },
1358 { 0 }
1359};
1360
1361MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1362
1363static struct serio_driver psmouse_drv = {
1364 .driver = {
1365 .name = "psmouse",
1366 },
1367 .description = DRIVER_DESC,
1368 .id_table = psmouse_serio_ids,
1369 .interrupt = psmouse_interrupt,
1370 .connect = psmouse_connect,
1371 .reconnect = psmouse_reconnect,
1372 .disconnect = psmouse_disconnect,
1373 .cleanup = psmouse_cleanup,
1374};
1375
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001376ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1377 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001380 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1381 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 int retval;
1383
1384 retval = serio_pin_driver(serio);
1385 if (retval)
1386 return retval;
1387
1388 if (serio->drv != &psmouse_drv) {
1389 retval = -ENODEV;
1390 goto out;
1391 }
1392
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001393 psmouse = serio_get_drvdata(serio);
1394
1395 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397out:
1398 serio_unpin_driver(serio);
1399 return retval;
1400}
1401
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001402ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1403 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001406 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1407 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int retval;
1409
1410 retval = serio_pin_driver(serio);
1411 if (retval)
1412 return retval;
1413
1414 if (serio->drv != &psmouse_drv) {
1415 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001416 goto out_unpin;
1417 }
1418
Ingo Molnarc14471d2006-02-19 00:22:11 -05001419 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001420 if (retval)
1421 goto out_unpin;
1422
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001423 psmouse = serio_get_drvdata(serio);
1424
Andres Salomon68d48222008-09-16 12:30:34 -04001425 if (attr->protect) {
1426 if (psmouse->state == PSMOUSE_IGNORE) {
1427 retval = -ENODEV;
1428 goto out_unlock;
1429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Andres Salomon68d48222008-09-16 12:30:34 -04001431 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1432 parent = serio_get_drvdata(serio->parent);
1433 psmouse_deactivate(parent);
1434 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001435
Andres Salomon68d48222008-09-16 12:30:34 -04001436 psmouse_deactivate(psmouse);
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001439 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Andres Salomon68d48222008-09-16 12:30:34 -04001441 if (attr->protect) {
1442 if (retval != -ENODEV)
1443 psmouse_activate(psmouse);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001444
Andres Salomon68d48222008-09-16 12:30:34 -04001445 if (parent)
1446 psmouse_activate(parent);
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Ingo Molnarc14471d2006-02-19 00:22:11 -05001449 out_unlock:
1450 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001451 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 serio_unpin_driver(serio);
1453 return retval;
1454}
1455
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001456static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1457{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001458 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001459
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001460 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001461}
1462
1463static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1464{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001465 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001466 unsigned long value;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001467
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001468 if (strict_strtoul(buf, 10, &value))
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001469 return -EINVAL;
1470
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001471 if ((unsigned int)value != value)
1472 return -EINVAL;
1473
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001474 *field = value;
1475
1476 return count;
1477}
1478
1479static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001480{
1481 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1482}
1483
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001484static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001485{
1486 struct serio *serio = psmouse->ps2dev.serio;
1487 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001488 struct input_dev *old_dev, *new_dev;
1489 const struct psmouse_protocol *proto, *old_proto;
1490 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001491 int retry = 0;
1492
Dmitry Torokhov72155612006-11-05 22:40:19 -05001493 proto = psmouse_protocol_by_name(buf, count);
1494 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001495 return -EINVAL;
1496
1497 if (psmouse->type == proto->type)
1498 return count;
1499
Dmitry Torokhov72155612006-11-05 22:40:19 -05001500 new_dev = input_allocate_device();
1501 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001502 return -ENOMEM;
1503
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001504 while (serio->child) {
1505 if (++retry > 3) {
1506 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001507 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001508 return -EIO;
1509 }
1510
Ingo Molnarc14471d2006-02-19 00:22:11 -05001511 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001512 serio_unpin_driver(serio);
1513 serio_unregister_child_port(serio);
1514 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001515 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001516
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001517 if (serio->drv != &psmouse_drv) {
1518 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001519 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001520 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001521
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001522 if (psmouse->type == proto->type) {
1523 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001524 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001525 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001526 }
1527
1528 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1529 parent = serio_get_drvdata(serio->parent);
1530 if (parent->pt_deactivate)
1531 parent->pt_deactivate(parent);
1532 }
1533
Dmitry Torokhov72155612006-11-05 22:40:19 -05001534 old_dev = psmouse->dev;
1535 old_proto = psmouse_protocol_by_type(psmouse->type);
1536
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001537 if (psmouse->disconnect)
1538 psmouse->disconnect(psmouse);
1539
1540 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001541
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001542 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001543 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1544
1545 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1546 psmouse_reset(psmouse);
1547 /* default to PSMOUSE_PS2 */
1548 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1549 }
1550
1551 psmouse_initialize(psmouse);
1552 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1553
Dmitry Torokhov72155612006-11-05 22:40:19 -05001554 error = input_register_device(psmouse->dev);
1555 if (error) {
1556 if (psmouse->disconnect)
1557 psmouse->disconnect(psmouse);
1558
1559 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1560 input_free_device(new_dev);
1561 psmouse->dev = old_dev;
1562 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1563 psmouse_switch_protocol(psmouse, old_proto);
1564 psmouse_initialize(psmouse);
1565 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1566
1567 return error;
1568 }
1569
1570 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001571
1572 if (parent && parent->pt_activate)
1573 parent->pt_activate(parent);
1574
1575 return count;
1576}
1577
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001578static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001582 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return -EINVAL;
1584
1585 psmouse->set_rate(psmouse, value);
1586 return count;
1587}
1588
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001589static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001593 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return -EINVAL;
1595
1596 psmouse->set_resolution(psmouse, value);
1597 return count;
1598}
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1602{
Helge Dellere38de672006-09-10 21:54:39 -04001603 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (!val)
1606 return -EINVAL;
1607
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001608 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001610 if (!proto || !proto->maxproto)
1611 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001613 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Stephen Evanchik541e3162005-08-08 01:26:18 -05001615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
1618static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1619{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001620 int type = *((unsigned int *)kp->arg);
1621
1622 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
1625static int __init psmouse_init(void)
1626{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001627 int err;
1628
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001629 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1630 if (!kpsmoused_wq) {
1631 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1632 return -ENOMEM;
1633 }
1634
Akinobu Mita153a9df02006-11-23 23:35:10 -05001635 err = serio_register_driver(&psmouse_drv);
1636 if (err)
1637 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001638
Akinobu Mita153a9df02006-11-23 23:35:10 -05001639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
1642static void __exit psmouse_exit(void)
1643{
1644 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001645 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
1648module_init(psmouse_init);
1649module_exit(psmouse_exit);