blob: b9f0fb2530e2357cb1f39ff1e52d48cf764ccc1b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PS/2 mouse driver
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2003-2004 Dmitry Torokhov
6 */
7
8/*
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
12 */
13
14#include <linux/delay.h>
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/slab.h>
18#include <linux/interrupt.h>
19#include <linux/input.h>
20#include <linux/serio.h>
21#include <linux/init.h>
22#include <linux/libps2.h>
Ingo Molnarc14471d2006-02-19 00:22:11 -050023#include <linux/mutex.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "psmouse.h"
26#include "synaptics.h"
27#include "logips2pp.h"
28#include "alps.h"
Kenan Esau02d7f582005-05-29 02:30:22 -050029#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050030#include "trackpoint.h"
Stefan Lucke24bf10a2007-02-18 01:49:10 -050031#include "touchkit_ps2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define DRIVER_DESC "PS/2 mouse driver"
34
35MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
36MODULE_DESCRIPTION(DRIVER_DESC);
37MODULE_LICENSE("GPL");
38
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050039static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
41static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
43#define param_set_proto_abbrev psmouse_set_maxproto
44#define param_get_proto_abbrev psmouse_get_maxproto
45module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050046MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static unsigned int psmouse_resolution = 200;
49module_param_named(resolution, psmouse_resolution, uint, 0644);
50MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
51
52static unsigned int psmouse_rate = 100;
53module_param_named(rate, psmouse_rate, uint, 0644);
54MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
55
56static unsigned int psmouse_smartscroll = 1;
57module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
58MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
59
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050060static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061module_param_named(resetafter, psmouse_resetafter, uint, 0644);
62MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
63
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050064static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050065module_param_named(resync_time, psmouse_resync_time, uint, 0644);
66MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
67
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050068PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
69 NULL,
70 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
71PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
72 (void *) offsetof(struct psmouse, rate),
73 psmouse_show_int_attr, psmouse_attr_set_rate);
74PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
75 (void *) offsetof(struct psmouse, resolution),
76 psmouse_show_int_attr, psmouse_attr_set_resolution);
77PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
78 (void *) offsetof(struct psmouse, resetafter),
79 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050080PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
81 (void *) offsetof(struct psmouse, resync_time),
82 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050083
84static struct attribute *psmouse_attributes[] = {
85 &psmouse_attr_protocol.dattr.attr,
86 &psmouse_attr_rate.dattr.attr,
87 &psmouse_attr_resolution.dattr.attr,
88 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050089 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050090 NULL
91};
92
93static struct attribute_group psmouse_attribute_group = {
94 .attrs = psmouse_attributes,
95};
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Dmitry Torokhov04df1922005-06-01 02:39:44 -050097/*
Ingo Molnarc14471d2006-02-19 00:22:11 -050098 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -050099 * (connecting, disconnecting, changing rate or resolution via
100 * sysfs). We could use a per-device semaphore but since there
101 * rarely more than one PS/2 mouse connected and since semaphore
102 * is taken in "slow" paths it is not worth it.
103 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500104static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500105
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500106static struct workqueue_struct *kpsmoused_wq;
107
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500108struct psmouse_protocol {
109 enum psmouse_type type;
Helge Dellere38de672006-09-10 21:54:39 -0400110 const char *name;
111 const char *alias;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500112 int maxproto;
113 int (*detect)(struct psmouse *, int);
114 int (*init)(struct psmouse *);
115};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/*
118 * psmouse_process_byte() analyzes the PS/2 data stream and reports
119 * relevant events to the input module once full packet has arrived.
120 */
121
David Howells7d12e782006-10-05 14:55:46 +0100122static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500124 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned char *packet = psmouse->packet;
126
127 if (psmouse->pktcnt < psmouse->pktsize)
128 return PSMOUSE_GOOD_DATA;
129
130/*
131 * Full packet accumulated, process it
132 */
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * Scroll wheel on IntelliMice, scroll buttons on NetMice
136 */
137
138 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
139 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
140
141/*
142 * Scroll wheel and buttons on IntelliMouse Explorer
143 */
144
145 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400146 switch (packet[3] & 0xC0) {
147 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
148 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
149 break;
150 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
151 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
152 break;
153 case 0x00:
154 case 0xC0:
155 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
156 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
157 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
158 break;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162/*
163 * Extra buttons on Genius NewNet 3D
164 */
165
166 if (psmouse->type == PSMOUSE_GENPS) {
167 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
168 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
169 }
170
171/*
172 * Extra button on ThinkingMouse
173 */
174 if (psmouse->type == PSMOUSE_THINKPS) {
175 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
176 /* Without this bit of weirdness moving up gives wildly high Y changes. */
177 packet[1] |= (packet[0] & 0x40) << 1;
178 }
179
180/*
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
205/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500206 * __psmouse_set_state() sets new psmouse state and resets all flags.
207 */
208
209static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
210{
211 psmouse->state = new_state;
212 psmouse->pktcnt = psmouse->out_of_sync = 0;
213 psmouse->ps2dev.flags = 0;
214 psmouse->last = jiffies;
215}
216
217
218/*
219 * psmouse_set_state() sets new psmouse state and resets all flags and
220 * counters while holding serio lock so fighting with interrupt handler
221 * is not a concern.
222 */
223
224static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
225{
226 serio_pause_rx(psmouse->ps2dev.serio);
227 __psmouse_set_state(psmouse, new_state);
228 serio_continue_rx(psmouse->ps2dev.serio);
229}
230
231/*
232 * psmouse_handle_byte() processes one byte of the input data stream
233 * by calling corresponding protocol handler.
234 */
235
David Howells7d12e782006-10-05 14:55:46 +0100236static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500237{
David Howells7d12e782006-10-05 14:55:46 +0100238 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500239
240 switch (rc) {
241 case PSMOUSE_BAD_DATA:
242 if (psmouse->state == PSMOUSE_ACTIVATED) {
243 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
244 psmouse->name, psmouse->phys, psmouse->pktcnt);
245 if (++psmouse->out_of_sync == psmouse->resetafter) {
246 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
247 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
248 serio_reconnect(psmouse->ps2dev.serio);
249 return -1;
250 }
251 }
252 psmouse->pktcnt = 0;
253 break;
254
255 case PSMOUSE_FULL_PACKET:
256 psmouse->pktcnt = 0;
257 if (psmouse->out_of_sync) {
258 psmouse->out_of_sync = 0;
259 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
260 psmouse->name, psmouse->phys);
261 }
262 break;
263
264 case PSMOUSE_GOOD_DATA:
265 break;
266 }
267 return 0;
268}
269
270/*
271 * psmouse_interrupt() handles incoming characters, either passing them
272 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
274
275static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100276 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (psmouse->state == PSMOUSE_IGNORE)
281 goto out;
282
283 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
284 if (psmouse->state == PSMOUSE_ACTIVATED)
285 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
286 flags & SERIO_TIMEOUT ? " timeout" : "",
287 flags & SERIO_PARITY ? " bad parity" : "");
288 ps2_cmd_aborted(&psmouse->ps2dev);
289 goto out;
290 }
291
292 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
293 if (ps2_handle_ack(&psmouse->ps2dev, data))
294 goto out;
295
296 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
297 if (ps2_handle_response(&psmouse->ps2dev, data))
298 goto out;
299
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500300 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto out;
302
303 if (psmouse->state == PSMOUSE_ACTIVATED &&
304 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500305 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500307 psmouse->badbyte = psmouse->packet[0];
308 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
309 queue_work(kpsmoused_wq, &psmouse->resync_work);
310 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500314/*
315 * Check if this is a new device announcement (0xAA 0x00)
316 */
317 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400318 if (psmouse->pktcnt == 1) {
319 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500323 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
324 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
325 serio_reconnect(serio);
326 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500328/*
329 * Not a new device, try processing first byte normally
330 */
331 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100332 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500333 goto out;
334
335 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500338/*
339 * See if we need to force resync because mouse was idle for too long
340 */
341 if (psmouse->state == PSMOUSE_ACTIVATED &&
342 psmouse->pktcnt == 1 && psmouse->resync_time &&
343 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
344 psmouse->badbyte = psmouse->packet[0];
345 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
346 queue_work(kpsmoused_wq, &psmouse->resync_work);
347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500349
350 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100351 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500352
353 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return IRQ_HANDLED;
355}
356
357
358/*
359 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
360 * using sliced syntax, understood by advanced devices, such as Logitech
361 * or Synaptics touchpads. The command is encoded as:
362 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
363 * is the command.
364 */
365int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
366{
367 int i;
368
369 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
370 return -1;
371
372 for (i = 6; i >= 0; i -= 2) {
373 unsigned char d = (command >> i) & 3;
374 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
375 return -1;
376 }
377
378 return 0;
379}
380
381
382/*
383 * psmouse_reset() resets the mouse into power-on state.
384 */
385int psmouse_reset(struct psmouse *psmouse)
386{
387 unsigned char param[2];
388
389 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
390 return -1;
391
392 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
393 return -1;
394
395 return 0;
396}
397
398
399/*
400 * Genius NetMouse magic init.
401 */
402static int genius_detect(struct psmouse *psmouse, int set_properties)
403{
404 struct ps2dev *ps2dev = &psmouse->ps2dev;
405 unsigned char param[4];
406
407 param[0] = 3;
408 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
409 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
410 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
411 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
412 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
413
414 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
415 return -1;
416
417 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500418 set_bit(BTN_EXTRA, psmouse->dev->keybit);
419 set_bit(BTN_SIDE, psmouse->dev->keybit);
420 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500423 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 psmouse->pktsize = 4;
425 }
426
427 return 0;
428}
429
430/*
431 * IntelliMouse magic init.
432 */
433static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
434{
435 struct ps2dev *ps2dev = &psmouse->ps2dev;
436 unsigned char param[2];
437
438 param[0] = 200;
439 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
440 param[0] = 100;
441 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
442 param[0] = 80;
443 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
444 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
445
446 if (param[0] != 3)
447 return -1;
448
449 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500450 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
451 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (!psmouse->vendor) psmouse->vendor = "Generic";
454 if (!psmouse->name) psmouse->name = "Wheel Mouse";
455 psmouse->pktsize = 4;
456 }
457
458 return 0;
459}
460
461/*
462 * Try IntelliMouse/Explorer magic init.
463 */
464static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
465{
466 struct ps2dev *ps2dev = &psmouse->ps2dev;
467 unsigned char param[2];
468
469 intellimouse_detect(psmouse, 0);
470
471 param[0] = 200;
472 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
473 param[0] = 200;
474 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
475 param[0] = 80;
476 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
477 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
478
479 if (param[0] != 4)
480 return -1;
481
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400482/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
483 param[0] = 200;
484 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
485 param[0] = 80;
486 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
487 param[0] = 40;
488 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500491 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
492 set_bit(REL_WHEEL, psmouse->dev->relbit);
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400493 set_bit(REL_HWHEEL, psmouse->dev->relbit);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500494 set_bit(BTN_SIDE, psmouse->dev->keybit);
495 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (!psmouse->vendor) psmouse->vendor = "Generic";
498 if (!psmouse->name) psmouse->name = "Explorer Mouse";
499 psmouse->pktsize = 4;
500 }
501
502 return 0;
503}
504
505/*
506 * Kensington ThinkingMouse / ExpertMouse magic init.
507 */
508static int thinking_detect(struct psmouse *psmouse, int set_properties)
509{
510 struct ps2dev *ps2dev = &psmouse->ps2dev;
511 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400512 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int i;
514
515 param[0] = 10;
516 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
517 param[0] = 0;
518 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400519 for (i = 0; i < ARRAY_SIZE(seq); i++) {
520 param[0] = seq[i];
521 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
524
525 if (param[0] != 2)
526 return -1;
527
528 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500529 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 psmouse->vendor = "Kensington";
532 psmouse->name = "ThinkingMouse";
533 }
534
535 return 0;
536}
537
538/*
539 * Bare PS/2 protocol "detection". Always succeeds.
540 */
541static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
542{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500543 if (set_properties) {
544 if (!psmouse->vendor) psmouse->vendor = "Generic";
545 if (!psmouse->name) psmouse->name = "Mouse";
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 return 0;
549}
550
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400551/*
552 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
553 * must be forced by sysfs protocol writing.
554 */
555static int cortron_detect(struct psmouse *psmouse, int set_properties)
556{
557 if (set_properties) {
558 psmouse->vendor = "Cortron";
559 psmouse->name = "PS/2 Trackball";
560 set_bit(BTN_SIDE, psmouse->dev->keybit);
561 }
562
563 return 0;
564}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/*
567 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
568 * the mouse may have.
569 */
570
571static int psmouse_extensions(struct psmouse *psmouse,
572 unsigned int max_proto, int set_properties)
573{
574 int synaptics_hardware = 0;
575
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500576/*
577 * We always check for lifebook because it does not disturb mouse
578 * (it only checks DMI information).
579 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500580 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500581 if (max_proto > PSMOUSE_IMEX) {
582 if (!set_properties || lifebook_init(psmouse) == 0)
583 return PSMOUSE_LIFEBOOK;
584 }
585 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/*
588 * Try Kensington ThinkingMouse (we try first, because synaptics probe
589 * upsets the thinkingmouse).
590 */
591
592 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
593 return PSMOUSE_THINKPS;
594
595/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500596 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
597 * support is disabled in config - we need to know if it is synaptics so we
598 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
600 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
601 synaptics_hardware = 1;
602
603 if (max_proto > PSMOUSE_IMEX) {
604 if (!set_properties || synaptics_init(psmouse) == 0)
605 return PSMOUSE_SYNAPTICS;
606/*
607 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
608 * Unfortunately Logitech/Genius probes confuse some firmware versions so
609 * we'll have to skip them.
610 */
611 max_proto = PSMOUSE_IMEX;
612 }
613/*
614 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
615 */
616 synaptics_reset(psmouse);
617 }
618
619/*
620 * Try ALPS TouchPad
621 */
622 if (max_proto > PSMOUSE_IMEX) {
623 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
624 if (alps_detect(psmouse, set_properties) == 0) {
625 if (!set_properties || alps_init(psmouse) == 0)
626 return PSMOUSE_ALPS;
627/*
628 * Init failed, try basic relative protocols
629 */
630 max_proto = PSMOUSE_IMEX;
631 }
632 }
633
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500634 if (max_proto > PSMOUSE_IMEX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500636 if (genius_detect(psmouse, set_properties) == 0)
637 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500639 if (ps2pp_init(psmouse, set_properties) == 0)
640 return PSMOUSE_PS2PP;
641
642 if (trackpoint_detect(psmouse, set_properties) == 0)
643 return PSMOUSE_TRACKPOINT;
644
645 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
646 return PSMOUSE_TOUCHKIT_PS2;
647 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/*
650 * Reset to defaults in case the device got confused by extended
Dmitry Torokhovba449952005-12-21 00:51:31 -0500651 * protocol probes. Note that we do full reset becuase some mice
652 * put themselves to sleep when see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
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
908 * reports from it unless we explicitely request it.
909 */
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
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001117 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1118 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1119 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001120
1121 psmouse->set_rate = psmouse_set_rate;
1122 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001123 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001124 psmouse->protocol_handler = psmouse_process_byte;
1125 psmouse->pktsize = 3;
1126
1127 if (proto && (proto->detect || proto->init)) {
1128 if (proto->detect && proto->detect(psmouse, 1) < 0)
1129 return -1;
1130
1131 if (proto->init && proto->init(psmouse) < 0)
1132 return -1;
1133
1134 psmouse->type = proto->type;
1135 }
1136 else
1137 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1138
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001139 /*
1140 * If mouse's packet size is 3 there is no point in polling the
1141 * device in hopes to detect protocol reset - we won't get less
1142 * than 3 bytes response anyhow.
1143 */
1144 if (psmouse->pktsize == 3)
1145 psmouse->resync_time = 0;
1146
1147 /*
1148 * Some smart KVMs fake response to POLL command returning just
1149 * 3 bytes and messing up our resync logic, so if initial poll
1150 * fails we won't try polling the device anymore. Hopefully
1151 * such KVM will maintain initially selected protocol.
1152 */
1153 if (psmouse->resync_time && psmouse->poll(psmouse))
1154 psmouse->resync_time = 0;
1155
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001156 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1157 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001158
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001159 input_dev->name = psmouse->devname;
1160 input_dev->phys = psmouse->phys;
1161 input_dev->id.bustype = BUS_I8042;
1162 input_dev->id.vendor = 0x0002;
1163 input_dev->id.product = psmouse->type;
1164 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001165
1166 return 0;
1167}
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/*
1170 * psmouse_connect() is a callback from the serio module when
1171 * an unhandled serio port is found.
1172 */
1173static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1174{
1175 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001176 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001177 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Ingo Molnarc14471d2006-02-19 00:22:11 -05001179 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /*
1182 * If this is a pass-through port deactivate parent so the device
1183 * connected to this port can be successfully identified
1184 */
1185 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1186 parent = serio_get_drvdata(serio->parent);
1187 psmouse_deactivate(parent);
1188 }
1189
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001190 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1191 input_dev = input_allocate_device();
1192 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001193 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 ps2_init(&psmouse->ps2dev, serio);
David Howellsc4028952006-11-22 14:57:56 +00001196 INIT_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001197 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001198 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1201
1202 serio_set_drvdata(serio, psmouse);
1203
Dmitry Torokhov72155612006-11-05 22:40:19 -05001204 error = serio_open(serio, drv);
1205 if (error)
1206 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001209 error = -ENODEV;
1210 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212
1213 psmouse->rate = psmouse_rate;
1214 psmouse->resolution = psmouse_resolution;
1215 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001216 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001219 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 psmouse_initialize(psmouse);
1223
Dmitry Torokhov72155612006-11-05 22:40:19 -05001224 error = input_register_device(psmouse->dev);
1225 if (error)
1226 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (parent && parent->pt_activate)
1229 parent->pt_activate(parent);
1230
Dmitry Torokhov72155612006-11-05 22:40:19 -05001231 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1232 if (error)
1233 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 psmouse_activate(psmouse);
1236
Dmitry Torokhov72155612006-11-05 22:40:19 -05001237 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001238 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (parent)
1240 psmouse_activate(parent);
1241
Ingo Molnarc14471d2006-02-19 00:22:11 -05001242 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001244
1245 err_pt_deactivate:
1246 if (parent && parent->pt_deactivate)
1247 parent->pt_deactivate(parent);
1248 err_protocol_disconnect:
1249 if (psmouse->disconnect)
1250 psmouse->disconnect(psmouse);
1251 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1252 err_close_serio:
1253 serio_close(serio);
1254 err_clear_drvdata:
1255 serio_set_drvdata(serio, NULL);
1256 err_free:
1257 input_free_device(input_dev);
1258 kfree(psmouse);
1259
1260 retval = error;
1261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264
1265static int psmouse_reconnect(struct serio *serio)
1266{
1267 struct psmouse *psmouse = serio_get_drvdata(serio);
1268 struct psmouse *parent = NULL;
1269 struct serio_driver *drv = serio->drv;
1270 int rc = -1;
1271
1272 if (!drv || !psmouse) {
1273 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1274 return -1;
1275 }
1276
Ingo Molnarc14471d2006-02-19 00:22:11 -05001277 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1280 parent = serio_get_drvdata(serio->parent);
1281 psmouse_deactivate(parent);
1282 }
1283
1284 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1285
1286 if (psmouse->reconnect) {
1287 if (psmouse->reconnect(psmouse))
1288 goto out;
1289 } else if (psmouse_probe(psmouse) < 0 ||
1290 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1291 goto out;
1292
1293 /* ok, the device type (and capabilities) match the old one,
1294 * we can continue using it, complete intialization
1295 */
1296 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1297
1298 psmouse_initialize(psmouse);
1299
1300 if (parent && parent->pt_activate)
1301 parent->pt_activate(parent);
1302
1303 psmouse_activate(psmouse);
1304 rc = 0;
1305
1306out:
1307 /* If this is a pass-through port the parent waits to be activated */
1308 if (parent)
1309 psmouse_activate(parent);
1310
Ingo Molnarc14471d2006-02-19 00:22:11 -05001311 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return rc;
1313}
1314
1315static struct serio_device_id psmouse_serio_ids[] = {
1316 {
1317 .type = SERIO_8042,
1318 .proto = SERIO_ANY,
1319 .id = SERIO_ANY,
1320 .extra = SERIO_ANY,
1321 },
1322 {
1323 .type = SERIO_PS_PSTHRU,
1324 .proto = SERIO_ANY,
1325 .id = SERIO_ANY,
1326 .extra = SERIO_ANY,
1327 },
1328 { 0 }
1329};
1330
1331MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1332
1333static struct serio_driver psmouse_drv = {
1334 .driver = {
1335 .name = "psmouse",
1336 },
1337 .description = DRIVER_DESC,
1338 .id_table = psmouse_serio_ids,
1339 .interrupt = psmouse_interrupt,
1340 .connect = psmouse_connect,
1341 .reconnect = psmouse_reconnect,
1342 .disconnect = psmouse_disconnect,
1343 .cleanup = psmouse_cleanup,
1344};
1345
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001346ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1347 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001350 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1351 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 int retval;
1353
1354 retval = serio_pin_driver(serio);
1355 if (retval)
1356 return retval;
1357
1358 if (serio->drv != &psmouse_drv) {
1359 retval = -ENODEV;
1360 goto out;
1361 }
1362
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001363 psmouse = serio_get_drvdata(serio);
1364
1365 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367out:
1368 serio_unpin_driver(serio);
1369 return retval;
1370}
1371
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001372ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1373 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001376 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1377 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 int retval;
1379
1380 retval = serio_pin_driver(serio);
1381 if (retval)
1382 return retval;
1383
1384 if (serio->drv != &psmouse_drv) {
1385 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001386 goto out_unpin;
1387 }
1388
Ingo Molnarc14471d2006-02-19 00:22:11 -05001389 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001390 if (retval)
1391 goto out_unpin;
1392
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001393 psmouse = serio_get_drvdata(serio);
1394
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001395 if (psmouse->state == PSMOUSE_IGNORE) {
1396 retval = -ENODEV;
Ingo Molnarc14471d2006-02-19 00:22:11 -05001397 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
1400 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1401 parent = serio_get_drvdata(serio->parent);
1402 psmouse_deactivate(parent);
1403 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 psmouse_deactivate(psmouse);
1406
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001407 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001409 if (retval != -ENODEV)
1410 psmouse_activate(psmouse);
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (parent)
1413 psmouse_activate(parent);
1414
Ingo Molnarc14471d2006-02-19 00:22:11 -05001415 out_unlock:
1416 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001417 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 serio_unpin_driver(serio);
1419 return retval;
1420}
1421
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001422static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1423{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001424 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001425
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001426 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001427}
1428
1429static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1430{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001431 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001432 unsigned long value;
1433 char *rest;
1434
1435 value = simple_strtoul(buf, &rest, 10);
1436 if (*rest)
1437 return -EINVAL;
1438
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001439 if ((unsigned int)value != value)
1440 return -EINVAL;
1441
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001442 *field = value;
1443
1444 return count;
1445}
1446
1447static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001448{
1449 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1450}
1451
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001452static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001453{
1454 struct serio *serio = psmouse->ps2dev.serio;
1455 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001456 struct input_dev *old_dev, *new_dev;
1457 const struct psmouse_protocol *proto, *old_proto;
1458 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001459 int retry = 0;
1460
Dmitry Torokhov72155612006-11-05 22:40:19 -05001461 proto = psmouse_protocol_by_name(buf, count);
1462 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001463 return -EINVAL;
1464
1465 if (psmouse->type == proto->type)
1466 return count;
1467
Dmitry Torokhov72155612006-11-05 22:40:19 -05001468 new_dev = input_allocate_device();
1469 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001470 return -ENOMEM;
1471
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001472 while (serio->child) {
1473 if (++retry > 3) {
1474 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001475 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001476 return -EIO;
1477 }
1478
Ingo Molnarc14471d2006-02-19 00:22:11 -05001479 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001480 serio_unpin_driver(serio);
1481 serio_unregister_child_port(serio);
1482 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001483 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001484
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001485 if (serio->drv != &psmouse_drv) {
1486 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001487 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001488 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001489
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001490 if (psmouse->type == proto->type) {
1491 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001492 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001493 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001494 }
1495
1496 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1497 parent = serio_get_drvdata(serio->parent);
1498 if (parent->pt_deactivate)
1499 parent->pt_deactivate(parent);
1500 }
1501
Dmitry Torokhov72155612006-11-05 22:40:19 -05001502 old_dev = psmouse->dev;
1503 old_proto = psmouse_protocol_by_type(psmouse->type);
1504
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001505 if (psmouse->disconnect)
1506 psmouse->disconnect(psmouse);
1507
1508 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001509
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001510 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001511 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1512
1513 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1514 psmouse_reset(psmouse);
1515 /* default to PSMOUSE_PS2 */
1516 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1517 }
1518
1519 psmouse_initialize(psmouse);
1520 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1521
Dmitry Torokhov72155612006-11-05 22:40:19 -05001522 error = input_register_device(psmouse->dev);
1523 if (error) {
1524 if (psmouse->disconnect)
1525 psmouse->disconnect(psmouse);
1526
1527 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1528 input_free_device(new_dev);
1529 psmouse->dev = old_dev;
1530 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1531 psmouse_switch_protocol(psmouse, old_proto);
1532 psmouse_initialize(psmouse);
1533 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1534
1535 return error;
1536 }
1537
1538 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001539
1540 if (parent && parent->pt_activate)
1541 parent->pt_activate(parent);
1542
1543 return count;
1544}
1545
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001546static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 unsigned long value;
1549 char *rest;
1550
1551 value = simple_strtoul(buf, &rest, 10);
1552 if (*rest)
1553 return -EINVAL;
1554
1555 psmouse->set_rate(psmouse, value);
1556 return count;
1557}
1558
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001559static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 unsigned long value;
1562 char *rest;
1563
1564 value = simple_strtoul(buf, &rest, 10);
1565 if (*rest)
1566 return -EINVAL;
1567
1568 psmouse->set_resolution(psmouse, value);
1569 return count;
1570}
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1574{
Helge Dellere38de672006-09-10 21:54:39 -04001575 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 if (!val)
1578 return -EINVAL;
1579
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001580 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001582 if (!proto || !proto->maxproto)
1583 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001585 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Stephen Evanchik541e3162005-08-08 01:26:18 -05001587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
1590static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1591{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001592 int type = *((unsigned int *)kp->arg);
1593
1594 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597static int __init psmouse_init(void)
1598{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001599 int err;
1600
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001601 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1602 if (!kpsmoused_wq) {
1603 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1604 return -ENOMEM;
1605 }
1606
Akinobu Mita153a9df02006-11-23 23:35:10 -05001607 err = serio_register_driver(&psmouse_drv);
1608 if (err)
1609 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001610
Akinobu Mita153a9df02006-11-23 23:35:10 -05001611 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
1614static void __exit psmouse_exit(void)
1615{
1616 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001617 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
1620module_init(psmouse_init);
1621module_exit(psmouse_exit);