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