blob: 291da6285b18b32d91076f955de6cf45038d2ab9 [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"
Kenan Esau02d7f582005-05-29 02:30:22 -050028#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050029#include "trackpoint.h"
Stefan Lucke24bf10a2007-02-18 01:49:10 -050030#include "touchkit_ps2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define DRIVER_DESC "PS/2 mouse driver"
33
34MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
35MODULE_DESCRIPTION(DRIVER_DESC);
36MODULE_LICENSE("GPL");
37
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050038static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
40static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
42#define param_set_proto_abbrev psmouse_set_maxproto
43#define param_get_proto_abbrev psmouse_get_maxproto
44module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050045MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static unsigned int psmouse_resolution = 200;
48module_param_named(resolution, psmouse_resolution, uint, 0644);
49MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
50
51static unsigned int psmouse_rate = 100;
52module_param_named(rate, psmouse_rate, uint, 0644);
53MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
54
55static unsigned int psmouse_smartscroll = 1;
56module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
57MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
58
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050059static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060module_param_named(resetafter, psmouse_resetafter, uint, 0644);
61MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
62
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050063static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050064module_param_named(resync_time, psmouse_resync_time, uint, 0644);
65MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
66
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050067PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
68 NULL,
69 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
70PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
71 (void *) offsetof(struct psmouse, rate),
72 psmouse_show_int_attr, psmouse_attr_set_rate);
73PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
74 (void *) offsetof(struct psmouse, resolution),
75 psmouse_show_int_attr, psmouse_attr_set_resolution);
76PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
77 (void *) offsetof(struct psmouse, resetafter),
78 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050079PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
80 (void *) offsetof(struct psmouse, resync_time),
81 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050082
83static struct attribute *psmouse_attributes[] = {
84 &psmouse_attr_protocol.dattr.attr,
85 &psmouse_attr_rate.dattr.attr,
86 &psmouse_attr_resolution.dattr.attr,
87 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050088 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050089 NULL
90};
91
92static struct attribute_group psmouse_attribute_group = {
93 .attrs = psmouse_attributes,
94};
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Dmitry Torokhov04df1922005-06-01 02:39:44 -050096/*
Ingo Molnarc14471d2006-02-19 00:22:11 -050097 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -050098 * (connecting, disconnecting, changing rate or resolution via
99 * sysfs). We could use a per-device semaphore but since there
100 * rarely more than one PS/2 mouse connected and since semaphore
101 * is taken in "slow" paths it is not worth it.
102 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500103static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500104
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500105static struct workqueue_struct *kpsmoused_wq;
106
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500107struct psmouse_protocol {
108 enum psmouse_type type;
Helge Dellere38de672006-09-10 21:54:39 -0400109 const char *name;
110 const char *alias;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500111 int maxproto;
112 int (*detect)(struct psmouse *, int);
113 int (*init)(struct psmouse *);
114};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/*
117 * psmouse_process_byte() analyzes the PS/2 data stream and reports
118 * relevant events to the input module once full packet has arrived.
119 */
120
David Howells7d12e782006-10-05 14:55:46 +0100121static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500123 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unsigned char *packet = psmouse->packet;
125
126 if (psmouse->pktcnt < psmouse->pktsize)
127 return PSMOUSE_GOOD_DATA;
128
129/*
130 * Full packet accumulated, process it
131 */
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
134 * Scroll wheel on IntelliMice, scroll buttons on NetMice
135 */
136
137 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
138 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
139
140/*
141 * Scroll wheel and buttons on IntelliMouse Explorer
142 */
143
144 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400145 switch (packet[3] & 0xC0) {
146 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
147 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
148 break;
149 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
150 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
151 break;
152 case 0x00:
153 case 0xC0:
154 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
155 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
156 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
157 break;
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
161/*
162 * Extra buttons on Genius NewNet 3D
163 */
164
165 if (psmouse->type == PSMOUSE_GENPS) {
166 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
167 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
168 }
169
170/*
171 * Extra button on ThinkingMouse
172 */
173 if (psmouse->type == PSMOUSE_THINKPS) {
174 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
175 /* Without this bit of weirdness moving up gives wildly high Y changes. */
176 packet[1] |= (packet[0] & 0x40) << 1;
177 }
178
179/*
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400180 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
181 * byte.
182 */
183 if (psmouse->type == PSMOUSE_CORTRON) {
184 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
185 packet[0] |= 0x08;
186 }
187
188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * Generic PS/2 Mouse
190 */
191
192 input_report_key(dev, BTN_LEFT, packet[0] & 1);
193 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
194 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
195
196 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
197 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
198
199 input_sync(dev);
200
201 return PSMOUSE_FULL_PACKET;
202}
203
204/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500205 * __psmouse_set_state() sets new psmouse state and resets all flags.
206 */
207
208static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
209{
210 psmouse->state = new_state;
211 psmouse->pktcnt = psmouse->out_of_sync = 0;
212 psmouse->ps2dev.flags = 0;
213 psmouse->last = jiffies;
214}
215
216
217/*
218 * psmouse_set_state() sets new psmouse state and resets all flags and
219 * counters while holding serio lock so fighting with interrupt handler
220 * is not a concern.
221 */
222
Andres Salomona48cf5f2008-09-16 12:30:33 -0400223void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500224{
225 serio_pause_rx(psmouse->ps2dev.serio);
226 __psmouse_set_state(psmouse, new_state);
227 serio_continue_rx(psmouse->ps2dev.serio);
228}
229
230/*
231 * psmouse_handle_byte() processes one byte of the input data stream
232 * by calling corresponding protocol handler.
233 */
234
David Howells7d12e782006-10-05 14:55:46 +0100235static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500236{
David Howells7d12e782006-10-05 14:55:46 +0100237 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500238
239 switch (rc) {
240 case PSMOUSE_BAD_DATA:
241 if (psmouse->state == PSMOUSE_ACTIVATED) {
242 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
243 psmouse->name, psmouse->phys, psmouse->pktcnt);
244 if (++psmouse->out_of_sync == psmouse->resetafter) {
245 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
246 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
247 serio_reconnect(psmouse->ps2dev.serio);
248 return -1;
249 }
250 }
251 psmouse->pktcnt = 0;
252 break;
253
254 case PSMOUSE_FULL_PACKET:
255 psmouse->pktcnt = 0;
256 if (psmouse->out_of_sync) {
257 psmouse->out_of_sync = 0;
258 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
259 psmouse->name, psmouse->phys);
260 }
261 break;
262
263 case PSMOUSE_GOOD_DATA:
264 break;
265 }
266 return 0;
267}
268
269/*
270 * psmouse_interrupt() handles incoming characters, either passing them
271 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
273
274static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100275 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (psmouse->state == PSMOUSE_IGNORE)
280 goto out;
281
282 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
283 if (psmouse->state == PSMOUSE_ACTIVATED)
284 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
285 flags & SERIO_TIMEOUT ? " timeout" : "",
286 flags & SERIO_PARITY ? " bad parity" : "");
287 ps2_cmd_aborted(&psmouse->ps2dev);
288 goto out;
289 }
290
291 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
292 if (ps2_handle_ack(&psmouse->ps2dev, data))
293 goto out;
294
295 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
296 if (ps2_handle_response(&psmouse->ps2dev, data))
297 goto out;
298
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500299 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 goto out;
301
302 if (psmouse->state == PSMOUSE_ACTIVATED &&
303 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500304 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500306 psmouse->badbyte = psmouse->packet[0];
307 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
308 queue_work(kpsmoused_wq, &psmouse->resync_work);
309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500313/*
314 * Check if this is a new device announcement (0xAA 0x00)
315 */
316 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400317 if (psmouse->pktcnt == 1) {
318 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500322 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
323 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
324 serio_reconnect(serio);
325 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500327/*
328 * Not a new device, try processing first byte normally
329 */
330 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100331 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500332 goto out;
333
334 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500337/*
338 * See if we need to force resync because mouse was idle for too long
339 */
340 if (psmouse->state == PSMOUSE_ACTIVATED &&
341 psmouse->pktcnt == 1 && psmouse->resync_time &&
342 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
343 psmouse->badbyte = psmouse->packet[0];
344 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
345 queue_work(kpsmoused_wq, &psmouse->resync_work);
346 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500348
349 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100350 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500351
352 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return IRQ_HANDLED;
354}
355
356
357/*
358 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
359 * using sliced syntax, understood by advanced devices, such as Logitech
360 * or Synaptics touchpads. The command is encoded as:
361 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
362 * is the command.
363 */
364int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
365{
366 int i;
367
368 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
369 return -1;
370
371 for (i = 6; i >= 0; i -= 2) {
372 unsigned char d = (command >> i) & 3;
373 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
374 return -1;
375 }
376
377 return 0;
378}
379
380
381/*
382 * psmouse_reset() resets the mouse into power-on state.
383 */
384int psmouse_reset(struct psmouse *psmouse)
385{
386 unsigned char param[2];
387
388 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
389 return -1;
390
391 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
392 return -1;
393
394 return 0;
395}
396
397
398/*
399 * Genius NetMouse magic init.
400 */
401static int genius_detect(struct psmouse *psmouse, int set_properties)
402{
403 struct ps2dev *ps2dev = &psmouse->ps2dev;
404 unsigned char param[4];
405
406 param[0] = 3;
407 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
408 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
409 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
410 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
411 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
412
413 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
414 return -1;
415
416 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500417 set_bit(BTN_EXTRA, psmouse->dev->keybit);
418 set_bit(BTN_SIDE, psmouse->dev->keybit);
419 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500422 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 psmouse->pktsize = 4;
424 }
425
426 return 0;
427}
428
429/*
430 * IntelliMouse magic init.
431 */
432static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
433{
434 struct ps2dev *ps2dev = &psmouse->ps2dev;
435 unsigned char param[2];
436
437 param[0] = 200;
438 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
439 param[0] = 100;
440 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
441 param[0] = 80;
442 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
443 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
444
445 if (param[0] != 3)
446 return -1;
447
448 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500449 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
450 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if (!psmouse->vendor) psmouse->vendor = "Generic";
453 if (!psmouse->name) psmouse->name = "Wheel Mouse";
454 psmouse->pktsize = 4;
455 }
456
457 return 0;
458}
459
460/*
461 * Try IntelliMouse/Explorer magic init.
462 */
463static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
464{
465 struct ps2dev *ps2dev = &psmouse->ps2dev;
466 unsigned char param[2];
467
468 intellimouse_detect(psmouse, 0);
469
470 param[0] = 200;
471 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
472 param[0] = 200;
473 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
474 param[0] = 80;
475 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
476 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
477
478 if (param[0] != 4)
479 return -1;
480
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400481/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
482 param[0] = 200;
483 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
484 param[0] = 80;
485 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
486 param[0] = 40;
487 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500490 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
491 set_bit(REL_WHEEL, psmouse->dev->relbit);
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400492 set_bit(REL_HWHEEL, psmouse->dev->relbit);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500493 set_bit(BTN_SIDE, psmouse->dev->keybit);
494 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 if (!psmouse->vendor) psmouse->vendor = "Generic";
497 if (!psmouse->name) psmouse->name = "Explorer Mouse";
498 psmouse->pktsize = 4;
499 }
500
501 return 0;
502}
503
504/*
505 * Kensington ThinkingMouse / ExpertMouse magic init.
506 */
507static int thinking_detect(struct psmouse *psmouse, int set_properties)
508{
509 struct ps2dev *ps2dev = &psmouse->ps2dev;
510 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400511 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 int i;
513
514 param[0] = 10;
515 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
516 param[0] = 0;
517 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400518 for (i = 0; i < ARRAY_SIZE(seq); i++) {
519 param[0] = seq[i];
520 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
523
524 if (param[0] != 2)
525 return -1;
526
527 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500528 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 psmouse->vendor = "Kensington";
531 psmouse->name = "ThinkingMouse";
532 }
533
534 return 0;
535}
536
537/*
538 * Bare PS/2 protocol "detection". Always succeeds.
539 */
540static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
541{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500542 if (set_properties) {
543 if (!psmouse->vendor) psmouse->vendor = "Generic";
544 if (!psmouse->name) psmouse->name = "Mouse";
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 return 0;
548}
549
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400550/*
551 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
552 * must be forced by sysfs protocol writing.
553 */
554static int cortron_detect(struct psmouse *psmouse, int set_properties)
555{
556 if (set_properties) {
557 psmouse->vendor = "Cortron";
558 psmouse->name = "PS/2 Trackball";
559 set_bit(BTN_SIDE, psmouse->dev->keybit);
560 }
561
562 return 0;
563}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/*
566 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
567 * the mouse may have.
568 */
569
570static int psmouse_extensions(struct psmouse *psmouse,
571 unsigned int max_proto, int set_properties)
572{
573 int synaptics_hardware = 0;
574
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500575/*
576 * We always check for lifebook because it does not disturb mouse
577 * (it only checks DMI information).
578 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500579 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500580 if (max_proto > PSMOUSE_IMEX) {
581 if (!set_properties || lifebook_init(psmouse) == 0)
582 return PSMOUSE_LIFEBOOK;
583 }
584 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * Try Kensington ThinkingMouse (we try first, because synaptics probe
588 * upsets the thinkingmouse).
589 */
590
591 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
592 return PSMOUSE_THINKPS;
593
594/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500595 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
596 * support is disabled in config - we need to know if it is synaptics so we
597 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
599 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
600 synaptics_hardware = 1;
601
602 if (max_proto > PSMOUSE_IMEX) {
603 if (!set_properties || synaptics_init(psmouse) == 0)
604 return PSMOUSE_SYNAPTICS;
605/*
606 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
607 * Unfortunately Logitech/Genius probes confuse some firmware versions so
608 * we'll have to skip them.
609 */
610 max_proto = PSMOUSE_IMEX;
611 }
612/*
613 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
614 */
615 synaptics_reset(psmouse);
616 }
617
618/*
619 * Try ALPS TouchPad
620 */
621 if (max_proto > PSMOUSE_IMEX) {
622 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
623 if (alps_detect(psmouse, set_properties) == 0) {
624 if (!set_properties || alps_init(psmouse) == 0)
625 return PSMOUSE_ALPS;
626/*
627 * Init failed, try basic relative protocols
628 */
629 max_proto = PSMOUSE_IMEX;
630 }
631 }
632
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500633 if (max_proto > PSMOUSE_IMEX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500635 if (genius_detect(psmouse, set_properties) == 0)
636 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500638 if (ps2pp_init(psmouse, set_properties) == 0)
639 return PSMOUSE_PS2PP;
640
641 if (trackpoint_detect(psmouse, set_properties) == 0)
642 return PSMOUSE_TRACKPOINT;
643
644 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
645 return PSMOUSE_TOUCHKIT_PS2;
646 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648/*
649 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400650 * protocol probes. Note that we follow up with full reset because
651 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
Alon Ziv554fc192007-08-30 00:22:48 -0400653 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500654 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
657 return PSMOUSE_IMEX;
658
659 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
660 return PSMOUSE_IMPS;
661
662/*
663 * Okay, all failed, we have a standard mouse here. The number of the buttons
664 * is still a question, though. We assume 3.
665 */
666 ps2bare_detect(psmouse, set_properties);
667
668 if (synaptics_hardware) {
669/*
670 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
671 * We need to reset the touchpad because if there is a track point on the
672 * pass through port it could get disabled while probing for protocol
673 * extensions.
674 */
675 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
678 return PSMOUSE_PS2;
679}
680
Helge Dellere38de672006-09-10 21:54:39 -0400681static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500682 {
683 .type = PSMOUSE_PS2,
684 .name = "PS/2",
685 .alias = "bare",
686 .maxproto = 1,
687 .detect = ps2bare_detect,
688 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500689#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500690 {
691 .type = PSMOUSE_PS2PP,
692 .name = "PS2++",
693 .alias = "logitech",
694 .detect = ps2pp_init,
695 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500696#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500697 {
698 .type = PSMOUSE_THINKPS,
699 .name = "ThinkPS/2",
700 .alias = "thinkps",
701 .detect = thinking_detect,
702 },
703 {
704 .type = PSMOUSE_GENPS,
705 .name = "GenPS/2",
706 .alias = "genius",
707 .detect = genius_detect,
708 },
709 {
710 .type = PSMOUSE_IMPS,
711 .name = "ImPS/2",
712 .alias = "imps",
713 .maxproto = 1,
714 .detect = intellimouse_detect,
715 },
716 {
717 .type = PSMOUSE_IMEX,
718 .name = "ImExPS/2",
719 .alias = "exps",
720 .maxproto = 1,
721 .detect = im_explorer_detect,
722 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500723#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500724 {
725 .type = PSMOUSE_SYNAPTICS,
726 .name = "SynPS/2",
727 .alias = "synaptics",
728 .detect = synaptics_detect,
729 .init = synaptics_init,
730 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500731#endif
732#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500733 {
734 .type = PSMOUSE_ALPS,
735 .name = "AlpsPS/2",
736 .alias = "alps",
737 .detect = alps_detect,
738 .init = alps_init,
739 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500740#endif
741#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500742 {
743 .type = PSMOUSE_LIFEBOOK,
744 .name = "LBPS/2",
745 .alias = "lifebook",
746 .init = lifebook_init,
747 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500748#endif
749#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500750 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500751 .type = PSMOUSE_TRACKPOINT,
752 .name = "TPPS/2",
753 .alias = "trackpoint",
754 .detect = trackpoint_detect,
755 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500756#endif
757#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500758 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500759 .type = PSMOUSE_TOUCHKIT_PS2,
760 .name = "touchkitPS/2",
761 .alias = "touchkit",
762 .detect = touchkit_ps2_detect,
763 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500764#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500765 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400766 .type = PSMOUSE_CORTRON,
767 .name = "CortronPS/2",
768 .alias = "cortps",
769 .detect = cortron_detect,
770 },
771 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500772 .type = PSMOUSE_AUTO,
773 .name = "auto",
774 .alias = "any",
775 .maxproto = 1,
776 },
777};
778
Helge Dellere38de672006-09-10 21:54:39 -0400779static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500780{
781 int i;
782
783 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
784 if (psmouse_protocols[i].type == type)
785 return &psmouse_protocols[i];
786
787 WARN_ON(1);
788 return &psmouse_protocols[0];
789}
790
Helge Dellere38de672006-09-10 21:54:39 -0400791static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500792{
Helge Dellere38de672006-09-10 21:54:39 -0400793 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500794 int i;
795
796 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
797 p = &psmouse_protocols[i];
798
799 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
800 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
801 return &psmouse_protocols[i];
802 }
803
804 return NULL;
805}
806
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
809 * psmouse_probe() probes for a PS/2 mouse.
810 */
811
812static int psmouse_probe(struct psmouse *psmouse)
813{
814 struct ps2dev *ps2dev = &psmouse->ps2dev;
815 unsigned char param[2];
816
817/*
818 * First, we check if it's a mouse. It should send 0x00 or 0x03
819 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500820 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
821 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823
824 param[0] = 0xa5;
825 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
826 return -1;
827
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500828 if (param[0] != 0x00 && param[0] != 0x03 &&
829 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -1;
831
832/*
833 * Then we reset and disable the mouse so that it doesn't generate events.
834 */
835
836 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
837 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
838
839 return 0;
840}
841
842/*
843 * Here we set the mouse resolution.
844 */
845
846void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
847{
Helge Dellere38de672006-09-10 21:54:39 -0400848 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
849 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 if (resolution == 0 || resolution > 200)
852 resolution = 200;
853
Helge Dellere38de672006-09-10 21:54:39 -0400854 p = params[resolution / 50];
855 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
856 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859/*
860 * Here we set the mouse report rate.
861 */
862
863static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
864{
Helge Dellere38de672006-09-10 21:54:39 -0400865 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
866 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int i = 0;
868
869 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400870 r = rates[i];
871 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
872 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875/*
876 * psmouse_initialize() initializes the mouse to a sane state.
877 */
878
879static void psmouse_initialize(struct psmouse *psmouse)
880{
881/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 * We set the mouse report rate, resolution and scaling.
883 */
884
885 if (psmouse_max_proto != PSMOUSE_PS2) {
886 psmouse->set_rate(psmouse, psmouse->rate);
887 psmouse->set_resolution(psmouse, psmouse->resolution);
888 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
889 }
890}
891
892/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * psmouse_activate() enables the mouse so that we get motion reports from it.
894 */
895
896static void psmouse_activate(struct psmouse *psmouse)
897{
898 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
899 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
900 psmouse->ps2dev.serio->phys);
901
902 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
903}
904
905
906/*
907 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +0200908 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
910
911static void psmouse_deactivate(struct psmouse *psmouse)
912{
913 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
914 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
915 psmouse->ps2dev.serio->phys);
916
917 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
918}
919
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500920/*
921 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
922 */
923
924static int psmouse_poll(struct psmouse *psmouse)
925{
926 return ps2_command(&psmouse->ps2dev, psmouse->packet,
927 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
928}
929
930
931/*
932 * psmouse_resync() attempts to re-validate current protocol.
933 */
934
David Howellsc4028952006-11-22 14:57:56 +0000935static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500936{
David Howellsc4028952006-11-22 14:57:56 +0000937 struct psmouse *parent = NULL, *psmouse =
938 container_of(work, struct psmouse, resync_work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500939 struct serio *serio = psmouse->ps2dev.serio;
940 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
941 int failed = 0, enabled = 0;
942 int i;
943
Ingo Molnarc14471d2006-02-19 00:22:11 -0500944 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500945
946 if (psmouse->state != PSMOUSE_RESYNCING)
947 goto out;
948
949 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
950 parent = serio_get_drvdata(serio->parent);
951 psmouse_deactivate(parent);
952 }
953
954/*
955 * Some mice don't ACK commands sent while they are in the middle of
956 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
957 * instead of ps2_command() which would wait for 200ms for an ACK
958 * that may never come.
959 * As an additional quirk ALPS touchpads may not only forget to ACK
960 * disable command but will stop reporting taps, so if we see that
961 * mouse at least once ACKs disable we will do full reconnect if ACK
962 * is missing.
963 */
964 psmouse->num_resyncs++;
965
966 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
967 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
968 failed = 1;
969 } else
970 psmouse->acks_disable_command = 1;
971
972/*
973 * Poll the mouse. If it was reset the packet will be shorter than
974 * psmouse->pktsize and ps2_command will fail. We do not expect and
975 * do not handle scenario when mouse "upgrades" its protocol while
976 * disconnected since it would require additional delay. If we ever
977 * see a mouse that does it we'll adjust the code.
978 */
979 if (!failed) {
980 if (psmouse->poll(psmouse))
981 failed = 1;
982 else {
983 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
984 for (i = 0; i < psmouse->pktsize; i++) {
985 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +0100986 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500987 if (rc != PSMOUSE_GOOD_DATA)
988 break;
989 }
990 if (rc != PSMOUSE_FULL_PACKET)
991 failed = 1;
992 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
993 }
994 }
995/*
996 * Now try to enable mouse. We try to do that even if poll failed and also
997 * repeat our attempts 5 times, otherwise we may be left out with disabled
998 * mouse.
999 */
1000 for (i = 0; i < 5; i++) {
1001 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1002 enabled = 1;
1003 break;
1004 }
1005 msleep(200);
1006 }
1007
1008 if (!enabled) {
1009 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1010 psmouse->ps2dev.serio->phys);
1011 failed = 1;
1012 }
1013
1014 if (failed) {
1015 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1016 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1017 serio_reconnect(serio);
1018 } else
1019 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1020
1021 if (parent)
1022 psmouse_activate(parent);
1023 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001024 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027/*
1028 * psmouse_cleanup() resets the mouse into power-on state.
1029 */
1030
1031static void psmouse_cleanup(struct serio *serio)
1032{
1033 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001034 struct psmouse *parent = NULL;
1035
1036 mutex_lock(&psmouse_mutex);
1037
1038 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1039 parent = serio_get_drvdata(serio->parent);
1040 psmouse_deactivate(parent);
1041 }
1042
1043 psmouse_deactivate(psmouse);
1044
1045 if (psmouse->cleanup)
1046 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 psmouse_reset(psmouse);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001049
1050/*
1051 * Some boxes, such as HP nx7400, get terribly confused if mouse
1052 * is not fully enabled before suspending/shutting down.
1053 */
1054 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1055
1056 if (parent) {
1057 if (parent->pt_deactivate)
1058 parent->pt_deactivate(parent);
1059
1060 psmouse_activate(parent);
1061 }
1062
1063 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066/*
1067 * psmouse_disconnect() closes and frees.
1068 */
1069
1070static void psmouse_disconnect(struct serio *serio)
1071{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001072 struct psmouse *psmouse, *parent = NULL;
1073
1074 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001076 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Ingo Molnarc14471d2006-02-19 00:22:11 -05001078 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1081
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001082 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001083 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001084 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001085 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1088 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001089 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
1092 if (psmouse->disconnect)
1093 psmouse->disconnect(psmouse);
1094
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001095 if (parent && parent->pt_deactivate)
1096 parent->pt_deactivate(parent);
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 serio_close(serio);
1101 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001102 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001104
1105 if (parent)
1106 psmouse_activate(parent);
1107
Ingo Molnarc14471d2006-02-19 00:22:11 -05001108 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Helge Dellere38de672006-09-10 21:54:39 -04001111static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001112{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001113 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001114
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001115 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001116
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001117 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1118 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
1119 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1120 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001121
1122 psmouse->set_rate = psmouse_set_rate;
1123 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001124 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001125 psmouse->protocol_handler = psmouse_process_byte;
1126 psmouse->pktsize = 3;
1127
1128 if (proto && (proto->detect || proto->init)) {
1129 if (proto->detect && proto->detect(psmouse, 1) < 0)
1130 return -1;
1131
1132 if (proto->init && proto->init(psmouse) < 0)
1133 return -1;
1134
1135 psmouse->type = proto->type;
1136 }
1137 else
1138 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1139
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001140 /*
1141 * If mouse's packet size is 3 there is no point in polling the
1142 * device in hopes to detect protocol reset - we won't get less
1143 * than 3 bytes response anyhow.
1144 */
1145 if (psmouse->pktsize == 3)
1146 psmouse->resync_time = 0;
1147
1148 /*
1149 * Some smart KVMs fake response to POLL command returning just
1150 * 3 bytes and messing up our resync logic, so if initial poll
1151 * fails we won't try polling the device anymore. Hopefully
1152 * such KVM will maintain initially selected protocol.
1153 */
1154 if (psmouse->resync_time && psmouse->poll(psmouse))
1155 psmouse->resync_time = 0;
1156
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001157 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1158 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001159
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001160 input_dev->name = psmouse->devname;
1161 input_dev->phys = psmouse->phys;
1162 input_dev->id.bustype = BUS_I8042;
1163 input_dev->id.vendor = 0x0002;
1164 input_dev->id.product = psmouse->type;
1165 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001166
1167 return 0;
1168}
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/*
1171 * psmouse_connect() is a callback from the serio module when
1172 * an unhandled serio port is found.
1173 */
1174static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1175{
1176 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001177 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001178 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Ingo Molnarc14471d2006-02-19 00:22:11 -05001180 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 /*
1183 * If this is a pass-through port deactivate parent so the device
1184 * connected to this port can be successfully identified
1185 */
1186 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1187 parent = serio_get_drvdata(serio->parent);
1188 psmouse_deactivate(parent);
1189 }
1190
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001191 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1192 input_dev = input_allocate_device();
1193 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001194 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 ps2_init(&psmouse->ps2dev, serio);
David Howellsc4028952006-11-22 14:57:56 +00001197 INIT_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001198 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001199 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1202
1203 serio_set_drvdata(serio, psmouse);
1204
Dmitry Torokhov72155612006-11-05 22:40:19 -05001205 error = serio_open(serio, drv);
1206 if (error)
1207 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001210 error = -ENODEV;
1211 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
1214 psmouse->rate = psmouse_rate;
1215 psmouse->resolution = psmouse_resolution;
1216 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001217 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001220 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 psmouse_initialize(psmouse);
1224
Dmitry Torokhov72155612006-11-05 22:40:19 -05001225 error = input_register_device(psmouse->dev);
1226 if (error)
1227 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (parent && parent->pt_activate)
1230 parent->pt_activate(parent);
1231
Dmitry Torokhov72155612006-11-05 22:40:19 -05001232 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1233 if (error)
1234 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 psmouse_activate(psmouse);
1237
Dmitry Torokhov72155612006-11-05 22:40:19 -05001238 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001239 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (parent)
1241 psmouse_activate(parent);
1242
Ingo Molnarc14471d2006-02-19 00:22:11 -05001243 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001245
1246 err_pt_deactivate:
1247 if (parent && parent->pt_deactivate)
1248 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001249 input_unregister_device(psmouse->dev);
1250 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001251 err_protocol_disconnect:
1252 if (psmouse->disconnect)
1253 psmouse->disconnect(psmouse);
1254 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1255 err_close_serio:
1256 serio_close(serio);
1257 err_clear_drvdata:
1258 serio_set_drvdata(serio, NULL);
1259 err_free:
1260 input_free_device(input_dev);
1261 kfree(psmouse);
1262
1263 retval = error;
1264 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
1267
1268static int psmouse_reconnect(struct serio *serio)
1269{
1270 struct psmouse *psmouse = serio_get_drvdata(serio);
1271 struct psmouse *parent = NULL;
1272 struct serio_driver *drv = serio->drv;
1273 int rc = -1;
1274
1275 if (!drv || !psmouse) {
1276 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1277 return -1;
1278 }
1279
Ingo Molnarc14471d2006-02-19 00:22:11 -05001280 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1283 parent = serio_get_drvdata(serio->parent);
1284 psmouse_deactivate(parent);
1285 }
1286
1287 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1288
1289 if (psmouse->reconnect) {
1290 if (psmouse->reconnect(psmouse))
1291 goto out;
1292 } else if (psmouse_probe(psmouse) < 0 ||
1293 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1294 goto out;
1295
1296 /* ok, the device type (and capabilities) match the old one,
1297 * we can continue using it, complete intialization
1298 */
1299 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1300
1301 psmouse_initialize(psmouse);
1302
1303 if (parent && parent->pt_activate)
1304 parent->pt_activate(parent);
1305
1306 psmouse_activate(psmouse);
1307 rc = 0;
1308
1309out:
1310 /* If this is a pass-through port the parent waits to be activated */
1311 if (parent)
1312 psmouse_activate(parent);
1313
Ingo Molnarc14471d2006-02-19 00:22:11 -05001314 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return rc;
1316}
1317
1318static struct serio_device_id psmouse_serio_ids[] = {
1319 {
1320 .type = SERIO_8042,
1321 .proto = SERIO_ANY,
1322 .id = SERIO_ANY,
1323 .extra = SERIO_ANY,
1324 },
1325 {
1326 .type = SERIO_PS_PSTHRU,
1327 .proto = SERIO_ANY,
1328 .id = SERIO_ANY,
1329 .extra = SERIO_ANY,
1330 },
1331 { 0 }
1332};
1333
1334MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1335
1336static struct serio_driver psmouse_drv = {
1337 .driver = {
1338 .name = "psmouse",
1339 },
1340 .description = DRIVER_DESC,
1341 .id_table = psmouse_serio_ids,
1342 .interrupt = psmouse_interrupt,
1343 .connect = psmouse_connect,
1344 .reconnect = psmouse_reconnect,
1345 .disconnect = psmouse_disconnect,
1346 .cleanup = psmouse_cleanup,
1347};
1348
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001349ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1350 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001353 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1354 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 int retval;
1356
1357 retval = serio_pin_driver(serio);
1358 if (retval)
1359 return retval;
1360
1361 if (serio->drv != &psmouse_drv) {
1362 retval = -ENODEV;
1363 goto out;
1364 }
1365
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001366 psmouse = serio_get_drvdata(serio);
1367
1368 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370out:
1371 serio_unpin_driver(serio);
1372 return retval;
1373}
1374
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001375ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1376 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001379 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1380 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 int retval;
1382
1383 retval = serio_pin_driver(serio);
1384 if (retval)
1385 return retval;
1386
1387 if (serio->drv != &psmouse_drv) {
1388 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001389 goto out_unpin;
1390 }
1391
Ingo Molnarc14471d2006-02-19 00:22:11 -05001392 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001393 if (retval)
1394 goto out_unpin;
1395
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001396 psmouse = serio_get_drvdata(serio);
1397
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001398 if (psmouse->state == PSMOUSE_IGNORE) {
1399 retval = -ENODEV;
Ingo Molnarc14471d2006-02-19 00:22:11 -05001400 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
1403 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1404 parent = serio_get_drvdata(serio->parent);
1405 psmouse_deactivate(parent);
1406 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 psmouse_deactivate(psmouse);
1409
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001410 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001412 if (retval != -ENODEV)
1413 psmouse_activate(psmouse);
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (parent)
1416 psmouse_activate(parent);
1417
Ingo Molnarc14471d2006-02-19 00:22:11 -05001418 out_unlock:
1419 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001420 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 serio_unpin_driver(serio);
1422 return retval;
1423}
1424
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001425static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1426{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001427 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001428
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001429 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001430}
1431
1432static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1433{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001434 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001435 unsigned long value;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001436
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001437 if (strict_strtoul(buf, 10, &value))
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001438 return -EINVAL;
1439
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001440 if ((unsigned int)value != value)
1441 return -EINVAL;
1442
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001443 *field = value;
1444
1445 return count;
1446}
1447
1448static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001449{
1450 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1451}
1452
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001453static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001454{
1455 struct serio *serio = psmouse->ps2dev.serio;
1456 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001457 struct input_dev *old_dev, *new_dev;
1458 const struct psmouse_protocol *proto, *old_proto;
1459 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001460 int retry = 0;
1461
Dmitry Torokhov72155612006-11-05 22:40:19 -05001462 proto = psmouse_protocol_by_name(buf, count);
1463 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001464 return -EINVAL;
1465
1466 if (psmouse->type == proto->type)
1467 return count;
1468
Dmitry Torokhov72155612006-11-05 22:40:19 -05001469 new_dev = input_allocate_device();
1470 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001471 return -ENOMEM;
1472
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001473 while (serio->child) {
1474 if (++retry > 3) {
1475 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001476 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001477 return -EIO;
1478 }
1479
Ingo Molnarc14471d2006-02-19 00:22:11 -05001480 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001481 serio_unpin_driver(serio);
1482 serio_unregister_child_port(serio);
1483 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001484 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001485
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001486 if (serio->drv != &psmouse_drv) {
1487 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001488 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001489 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001490
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001491 if (psmouse->type == proto->type) {
1492 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001493 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001494 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001495 }
1496
1497 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1498 parent = serio_get_drvdata(serio->parent);
1499 if (parent->pt_deactivate)
1500 parent->pt_deactivate(parent);
1501 }
1502
Dmitry Torokhov72155612006-11-05 22:40:19 -05001503 old_dev = psmouse->dev;
1504 old_proto = psmouse_protocol_by_type(psmouse->type);
1505
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001506 if (psmouse->disconnect)
1507 psmouse->disconnect(psmouse);
1508
1509 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001510
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001511 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001512 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1513
1514 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1515 psmouse_reset(psmouse);
1516 /* default to PSMOUSE_PS2 */
1517 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1518 }
1519
1520 psmouse_initialize(psmouse);
1521 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1522
Dmitry Torokhov72155612006-11-05 22:40:19 -05001523 error = input_register_device(psmouse->dev);
1524 if (error) {
1525 if (psmouse->disconnect)
1526 psmouse->disconnect(psmouse);
1527
1528 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1529 input_free_device(new_dev);
1530 psmouse->dev = old_dev;
1531 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1532 psmouse_switch_protocol(psmouse, old_proto);
1533 psmouse_initialize(psmouse);
1534 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1535
1536 return error;
1537 }
1538
1539 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001540
1541 if (parent && parent->pt_activate)
1542 parent->pt_activate(parent);
1543
1544 return count;
1545}
1546
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001547static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001551 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return -EINVAL;
1553
1554 psmouse->set_rate(psmouse, value);
1555 return count;
1556}
1557
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001558static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001562 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return -EINVAL;
1564
1565 psmouse->set_resolution(psmouse, value);
1566 return count;
1567}
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1571{
Helge Dellere38de672006-09-10 21:54:39 -04001572 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 if (!val)
1575 return -EINVAL;
1576
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001577 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001579 if (!proto || !proto->maxproto)
1580 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001582 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Stephen Evanchik541e3162005-08-08 01:26:18 -05001584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
1587static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1588{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001589 int type = *((unsigned int *)kp->arg);
1590
1591 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
1594static int __init psmouse_init(void)
1595{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001596 int err;
1597
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001598 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1599 if (!kpsmoused_wq) {
1600 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1601 return -ENOMEM;
1602 }
1603
Akinobu Mita153a9df02006-11-23 23:35:10 -05001604 err = serio_register_driver(&psmouse_drv);
1605 if (err)
1606 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001607
Akinobu Mita153a9df02006-11-23 23:35:10 -05001608 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
1611static void __exit psmouse_exit(void)
1612{
1613 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001614 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
1617module_init(psmouse_init);
1618module_exit(psmouse_exit);