blob: eb63855f7cc197d70cc5aab93727b140220b2cce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PS/2 mouse driver
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2003-2004 Dmitry Torokhov
6 */
7
8/*
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
12 */
13
14#include <linux/delay.h>
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/slab.h>
18#include <linux/interrupt.h>
19#include <linux/input.h>
20#include <linux/serio.h>
21#include <linux/init.h>
22#include <linux/libps2.h>
Ingo Molnarc14471d2006-02-19 00:22:11 -050023#include <linux/mutex.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "psmouse.h"
26#include "synaptics.h"
27#include "logips2pp.h"
28#include "alps.h"
Kenan Esau02d7f582005-05-29 02:30:22 -050029#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050030#include "trackpoint.h"
Stefan Lucke24bf10a2007-02-18 01:49:10 -050031#include "touchkit_ps2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define DRIVER_DESC "PS/2 mouse driver"
34
35MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
36MODULE_DESCRIPTION(DRIVER_DESC);
37MODULE_LICENSE("GPL");
38
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050039static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
41static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
43#define param_set_proto_abbrev psmouse_set_maxproto
44#define param_get_proto_abbrev psmouse_get_maxproto
45module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050046MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static unsigned int psmouse_resolution = 200;
49module_param_named(resolution, psmouse_resolution, uint, 0644);
50MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
51
52static unsigned int psmouse_rate = 100;
53module_param_named(rate, psmouse_rate, uint, 0644);
54MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
55
56static unsigned int psmouse_smartscroll = 1;
57module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
58MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
59
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050060static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061module_param_named(resetafter, psmouse_resetafter, uint, 0644);
62MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
63
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050064static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050065module_param_named(resync_time, psmouse_resync_time, uint, 0644);
66MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
67
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050068PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
69 NULL,
70 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
71PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
72 (void *) offsetof(struct psmouse, rate),
73 psmouse_show_int_attr, psmouse_attr_set_rate);
74PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
75 (void *) offsetof(struct psmouse, resolution),
76 psmouse_show_int_attr, psmouse_attr_set_resolution);
77PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
78 (void *) offsetof(struct psmouse, resetafter),
79 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050080PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
81 (void *) offsetof(struct psmouse, resync_time),
82 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050083
84static struct attribute *psmouse_attributes[] = {
85 &psmouse_attr_protocol.dattr.attr,
86 &psmouse_attr_rate.dattr.attr,
87 &psmouse_attr_resolution.dattr.attr,
88 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050089 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050090 NULL
91};
92
93static struct attribute_group psmouse_attribute_group = {
94 .attrs = psmouse_attributes,
95};
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Dmitry Torokhov04df1922005-06-01 02:39:44 -050097/*
Ingo Molnarc14471d2006-02-19 00:22:11 -050098 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -050099 * (connecting, disconnecting, changing rate or resolution via
100 * sysfs). We could use a per-device semaphore but since there
101 * rarely more than one PS/2 mouse connected and since semaphore
102 * is taken in "slow" paths it is not worth it.
103 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500104static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500105
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500106static struct workqueue_struct *kpsmoused_wq;
107
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500108struct psmouse_protocol {
109 enum psmouse_type type;
Helge Dellere38de672006-09-10 21:54:39 -0400110 const char *name;
111 const char *alias;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500112 int maxproto;
113 int (*detect)(struct psmouse *, int);
114 int (*init)(struct psmouse *);
115};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/*
118 * psmouse_process_byte() analyzes the PS/2 data stream and reports
119 * relevant events to the input module once full packet has arrived.
120 */
121
David Howells7d12e782006-10-05 14:55:46 +0100122static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500124 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned char *packet = psmouse->packet;
126
127 if (psmouse->pktcnt < psmouse->pktsize)
128 return PSMOUSE_GOOD_DATA;
129
130/*
131 * Full packet accumulated, process it
132 */
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * Scroll wheel on IntelliMice, scroll buttons on NetMice
136 */
137
138 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
139 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
140
141/*
142 * Scroll wheel and buttons on IntelliMouse Explorer
143 */
144
145 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400146 switch (packet[3] & 0xC0) {
147 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
148 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
149 break;
150 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
151 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
152 break;
153 case 0x00:
154 case 0xC0:
155 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
156 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
157 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
158 break;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162/*
163 * Extra buttons on Genius NewNet 3D
164 */
165
166 if (psmouse->type == PSMOUSE_GENPS) {
167 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
168 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
169 }
170
171/*
172 * Extra button on ThinkingMouse
173 */
174 if (psmouse->type == PSMOUSE_THINKPS) {
175 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
176 /* Without this bit of weirdness moving up gives wildly high Y changes. */
177 packet[1] |= (packet[0] & 0x40) << 1;
178 }
179
180/*
181 * Generic PS/2 Mouse
182 */
183
184 input_report_key(dev, BTN_LEFT, packet[0] & 1);
185 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
186 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
187
188 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
189 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
190
191 input_sync(dev);
192
193 return PSMOUSE_FULL_PACKET;
194}
195
196/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500197 * __psmouse_set_state() sets new psmouse state and resets all flags.
198 */
199
200static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
201{
202 psmouse->state = new_state;
203 psmouse->pktcnt = psmouse->out_of_sync = 0;
204 psmouse->ps2dev.flags = 0;
205 psmouse->last = jiffies;
206}
207
208
209/*
210 * psmouse_set_state() sets new psmouse state and resets all flags and
211 * counters while holding serio lock so fighting with interrupt handler
212 * is not a concern.
213 */
214
215static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
216{
217 serio_pause_rx(psmouse->ps2dev.serio);
218 __psmouse_set_state(psmouse, new_state);
219 serio_continue_rx(psmouse->ps2dev.serio);
220}
221
222/*
223 * psmouse_handle_byte() processes one byte of the input data stream
224 * by calling corresponding protocol handler.
225 */
226
David Howells7d12e782006-10-05 14:55:46 +0100227static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500228{
David Howells7d12e782006-10-05 14:55:46 +0100229 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500230
231 switch (rc) {
232 case PSMOUSE_BAD_DATA:
233 if (psmouse->state == PSMOUSE_ACTIVATED) {
234 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
235 psmouse->name, psmouse->phys, psmouse->pktcnt);
236 if (++psmouse->out_of_sync == psmouse->resetafter) {
237 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
238 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
239 serio_reconnect(psmouse->ps2dev.serio);
240 return -1;
241 }
242 }
243 psmouse->pktcnt = 0;
244 break;
245
246 case PSMOUSE_FULL_PACKET:
247 psmouse->pktcnt = 0;
248 if (psmouse->out_of_sync) {
249 psmouse->out_of_sync = 0;
250 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
251 psmouse->name, psmouse->phys);
252 }
253 break;
254
255 case PSMOUSE_GOOD_DATA:
256 break;
257 }
258 return 0;
259}
260
261/*
262 * psmouse_interrupt() handles incoming characters, either passing them
263 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
265
266static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100267 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (psmouse->state == PSMOUSE_IGNORE)
272 goto out;
273
274 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
275 if (psmouse->state == PSMOUSE_ACTIVATED)
276 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
277 flags & SERIO_TIMEOUT ? " timeout" : "",
278 flags & SERIO_PARITY ? " bad parity" : "");
279 ps2_cmd_aborted(&psmouse->ps2dev);
280 goto out;
281 }
282
283 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
284 if (ps2_handle_ack(&psmouse->ps2dev, data))
285 goto out;
286
287 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
288 if (ps2_handle_response(&psmouse->ps2dev, data))
289 goto out;
290
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500291 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto out;
293
294 if (psmouse->state == PSMOUSE_ACTIVATED &&
295 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500296 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500298 psmouse->badbyte = psmouse->packet[0];
299 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
300 queue_work(kpsmoused_wq, &psmouse->resync_work);
301 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500305/*
306 * Check if this is a new device announcement (0xAA 0x00)
307 */
308 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400309 if (psmouse->pktcnt == 1) {
310 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500314 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
315 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
316 serio_reconnect(serio);
317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500319/*
320 * Not a new device, try processing first byte normally
321 */
322 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100323 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500324 goto out;
325
326 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500329/*
330 * See if we need to force resync because mouse was idle for too long
331 */
332 if (psmouse->state == PSMOUSE_ACTIVATED &&
333 psmouse->pktcnt == 1 && psmouse->resync_time &&
334 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
335 psmouse->badbyte = psmouse->packet[0];
336 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
337 queue_work(kpsmoused_wq, &psmouse->resync_work);
338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500340
341 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100342 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500343
344 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return IRQ_HANDLED;
346}
347
348
349/*
350 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
351 * using sliced syntax, understood by advanced devices, such as Logitech
352 * or Synaptics touchpads. The command is encoded as:
353 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
354 * is the command.
355 */
356int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
357{
358 int i;
359
360 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
361 return -1;
362
363 for (i = 6; i >= 0; i -= 2) {
364 unsigned char d = (command >> i) & 3;
365 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
366 return -1;
367 }
368
369 return 0;
370}
371
372
373/*
374 * psmouse_reset() resets the mouse into power-on state.
375 */
376int psmouse_reset(struct psmouse *psmouse)
377{
378 unsigned char param[2];
379
380 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
381 return -1;
382
383 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
384 return -1;
385
386 return 0;
387}
388
389
390/*
391 * Genius NetMouse magic init.
392 */
393static int genius_detect(struct psmouse *psmouse, int set_properties)
394{
395 struct ps2dev *ps2dev = &psmouse->ps2dev;
396 unsigned char param[4];
397
398 param[0] = 3;
399 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
400 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
401 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
402 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
403 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
404
405 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
406 return -1;
407
408 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500409 set_bit(BTN_EXTRA, psmouse->dev->keybit);
410 set_bit(BTN_SIDE, psmouse->dev->keybit);
411 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500414 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 psmouse->pktsize = 4;
416 }
417
418 return 0;
419}
420
421/*
422 * IntelliMouse magic init.
423 */
424static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
425{
426 struct ps2dev *ps2dev = &psmouse->ps2dev;
427 unsigned char param[2];
428
429 param[0] = 200;
430 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
431 param[0] = 100;
432 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
433 param[0] = 80;
434 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
435 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
436
437 if (param[0] != 3)
438 return -1;
439
440 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500441 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
442 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (!psmouse->vendor) psmouse->vendor = "Generic";
445 if (!psmouse->name) psmouse->name = "Wheel Mouse";
446 psmouse->pktsize = 4;
447 }
448
449 return 0;
450}
451
452/*
453 * Try IntelliMouse/Explorer magic init.
454 */
455static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
456{
457 struct ps2dev *ps2dev = &psmouse->ps2dev;
458 unsigned char param[2];
459
460 intellimouse_detect(psmouse, 0);
461
462 param[0] = 200;
463 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
464 param[0] = 200;
465 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
466 param[0] = 80;
467 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
468 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
469
470 if (param[0] != 4)
471 return -1;
472
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400473/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
474 param[0] = 200;
475 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
476 param[0] = 80;
477 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
478 param[0] = 40;
479 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500482 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
483 set_bit(REL_WHEEL, psmouse->dev->relbit);
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400484 set_bit(REL_HWHEEL, psmouse->dev->relbit);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500485 set_bit(BTN_SIDE, psmouse->dev->keybit);
486 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (!psmouse->vendor) psmouse->vendor = "Generic";
489 if (!psmouse->name) psmouse->name = "Explorer Mouse";
490 psmouse->pktsize = 4;
491 }
492
493 return 0;
494}
495
496/*
497 * Kensington ThinkingMouse / ExpertMouse magic init.
498 */
499static int thinking_detect(struct psmouse *psmouse, int set_properties)
500{
501 struct ps2dev *ps2dev = &psmouse->ps2dev;
502 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400503 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int i;
505
506 param[0] = 10;
507 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
508 param[0] = 0;
509 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400510 for (i = 0; i < ARRAY_SIZE(seq); i++) {
511 param[0] = seq[i];
512 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
515
516 if (param[0] != 2)
517 return -1;
518
519 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500520 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 psmouse->vendor = "Kensington";
523 psmouse->name = "ThinkingMouse";
524 }
525
526 return 0;
527}
528
529/*
530 * Bare PS/2 protocol "detection". Always succeeds.
531 */
532static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
533{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500534 if (set_properties) {
535 if (!psmouse->vendor) psmouse->vendor = "Generic";
536 if (!psmouse->name) psmouse->name = "Mouse";
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 return 0;
540}
541
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/*
544 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
545 * the mouse may have.
546 */
547
548static int psmouse_extensions(struct psmouse *psmouse,
549 unsigned int max_proto, int set_properties)
550{
551 int synaptics_hardware = 0;
552
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500553/*
554 * We always check for lifebook because it does not disturb mouse
555 * (it only checks DMI information).
556 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500557 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500558 if (max_proto > PSMOUSE_IMEX) {
559 if (!set_properties || lifebook_init(psmouse) == 0)
560 return PSMOUSE_LIFEBOOK;
561 }
562 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * Try Kensington ThinkingMouse (we try first, because synaptics probe
566 * upsets the thinkingmouse).
567 */
568
569 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
570 return PSMOUSE_THINKPS;
571
572/*
573 * Try Synaptics TouchPad
574 */
575 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
576 synaptics_hardware = 1;
577
578 if (max_proto > PSMOUSE_IMEX) {
579 if (!set_properties || synaptics_init(psmouse) == 0)
580 return PSMOUSE_SYNAPTICS;
581/*
582 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
583 * Unfortunately Logitech/Genius probes confuse some firmware versions so
584 * we'll have to skip them.
585 */
586 max_proto = PSMOUSE_IMEX;
587 }
588/*
589 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
590 */
591 synaptics_reset(psmouse);
592 }
593
594/*
595 * Try ALPS TouchPad
596 */
597 if (max_proto > PSMOUSE_IMEX) {
598 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
599 if (alps_detect(psmouse, set_properties) == 0) {
600 if (!set_properties || alps_init(psmouse) == 0)
601 return PSMOUSE_ALPS;
602/*
603 * Init failed, try basic relative protocols
604 */
605 max_proto = PSMOUSE_IMEX;
606 }
607 }
608
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500609 if (max_proto > PSMOUSE_IMEX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500611 if (genius_detect(psmouse, set_properties) == 0)
612 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500614 if (ps2pp_init(psmouse, set_properties) == 0)
615 return PSMOUSE_PS2PP;
616
617 if (trackpoint_detect(psmouse, set_properties) == 0)
618 return PSMOUSE_TRACKPOINT;
619
620 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
621 return PSMOUSE_TOUCHKIT_PS2;
622 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/*
625 * Reset to defaults in case the device got confused by extended
Dmitry Torokhovba449952005-12-21 00:51:31 -0500626 * protocol probes. Note that we do full reset becuase some mice
627 * put themselves to sleep when see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
Dmitry Torokhovba449952005-12-21 00:51:31 -0500629 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
632 return PSMOUSE_IMEX;
633
634 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
635 return PSMOUSE_IMPS;
636
637/*
638 * Okay, all failed, we have a standard mouse here. The number of the buttons
639 * is still a question, though. We assume 3.
640 */
641 ps2bare_detect(psmouse, set_properties);
642
643 if (synaptics_hardware) {
644/*
645 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
646 * We need to reset the touchpad because if there is a track point on the
647 * pass through port it could get disabled while probing for protocol
648 * extensions.
649 */
650 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
653 return PSMOUSE_PS2;
654}
655
Helge Dellere38de672006-09-10 21:54:39 -0400656static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500657 {
658 .type = PSMOUSE_PS2,
659 .name = "PS/2",
660 .alias = "bare",
661 .maxproto = 1,
662 .detect = ps2bare_detect,
663 },
664 {
665 .type = PSMOUSE_PS2PP,
666 .name = "PS2++",
667 .alias = "logitech",
668 .detect = ps2pp_init,
669 },
670 {
671 .type = PSMOUSE_THINKPS,
672 .name = "ThinkPS/2",
673 .alias = "thinkps",
674 .detect = thinking_detect,
675 },
676 {
677 .type = PSMOUSE_GENPS,
678 .name = "GenPS/2",
679 .alias = "genius",
680 .detect = genius_detect,
681 },
682 {
683 .type = PSMOUSE_IMPS,
684 .name = "ImPS/2",
685 .alias = "imps",
686 .maxproto = 1,
687 .detect = intellimouse_detect,
688 },
689 {
690 .type = PSMOUSE_IMEX,
691 .name = "ImExPS/2",
692 .alias = "exps",
693 .maxproto = 1,
694 .detect = im_explorer_detect,
695 },
696 {
697 .type = PSMOUSE_SYNAPTICS,
698 .name = "SynPS/2",
699 .alias = "synaptics",
700 .detect = synaptics_detect,
701 .init = synaptics_init,
702 },
703 {
704 .type = PSMOUSE_ALPS,
705 .name = "AlpsPS/2",
706 .alias = "alps",
707 .detect = alps_detect,
708 .init = alps_init,
709 },
710 {
711 .type = PSMOUSE_LIFEBOOK,
712 .name = "LBPS/2",
713 .alias = "lifebook",
714 .init = lifebook_init,
715 },
716 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500717 .type = PSMOUSE_TRACKPOINT,
718 .name = "TPPS/2",
719 .alias = "trackpoint",
720 .detect = trackpoint_detect,
721 },
722 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500723 .type = PSMOUSE_TOUCHKIT_PS2,
724 .name = "touchkitPS/2",
725 .alias = "touchkit",
726 .detect = touchkit_ps2_detect,
727 },
728 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500729 .type = PSMOUSE_AUTO,
730 .name = "auto",
731 .alias = "any",
732 .maxproto = 1,
733 },
734};
735
Helge Dellere38de672006-09-10 21:54:39 -0400736static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500737{
738 int i;
739
740 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
741 if (psmouse_protocols[i].type == type)
742 return &psmouse_protocols[i];
743
744 WARN_ON(1);
745 return &psmouse_protocols[0];
746}
747
Helge Dellere38de672006-09-10 21:54:39 -0400748static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500749{
Helge Dellere38de672006-09-10 21:54:39 -0400750 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500751 int i;
752
753 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
754 p = &psmouse_protocols[i];
755
756 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
757 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
758 return &psmouse_protocols[i];
759 }
760
761 return NULL;
762}
763
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/*
766 * psmouse_probe() probes for a PS/2 mouse.
767 */
768
769static int psmouse_probe(struct psmouse *psmouse)
770{
771 struct ps2dev *ps2dev = &psmouse->ps2dev;
772 unsigned char param[2];
773
774/*
775 * First, we check if it's a mouse. It should send 0x00 or 0x03
776 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500777 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
778 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
780
781 param[0] = 0xa5;
782 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
783 return -1;
784
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500785 if (param[0] != 0x00 && param[0] != 0x03 &&
786 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -1;
788
789/*
790 * Then we reset and disable the mouse so that it doesn't generate events.
791 */
792
793 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
794 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
795
796 return 0;
797}
798
799/*
800 * Here we set the mouse resolution.
801 */
802
803void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
804{
Helge Dellere38de672006-09-10 21:54:39 -0400805 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
806 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 if (resolution == 0 || resolution > 200)
809 resolution = 200;
810
Helge Dellere38de672006-09-10 21:54:39 -0400811 p = params[resolution / 50];
812 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
813 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816/*
817 * Here we set the mouse report rate.
818 */
819
820static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
821{
Helge Dellere38de672006-09-10 21:54:39 -0400822 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
823 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int i = 0;
825
826 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400827 r = rates[i];
828 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
829 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832/*
833 * psmouse_initialize() initializes the mouse to a sane state.
834 */
835
836static void psmouse_initialize(struct psmouse *psmouse)
837{
838/*
839 * We set the mouse into streaming mode.
840 */
841
842 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
843
844/*
845 * We set the mouse report rate, resolution and scaling.
846 */
847
848 if (psmouse_max_proto != PSMOUSE_PS2) {
849 psmouse->set_rate(psmouse, psmouse->rate);
850 psmouse->set_resolution(psmouse, psmouse->resolution);
851 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
852 }
853}
854
855/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * psmouse_activate() enables the mouse so that we get motion reports from it.
857 */
858
859static void psmouse_activate(struct psmouse *psmouse)
860{
861 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
862 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
863 psmouse->ps2dev.serio->phys);
864
865 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
866}
867
868
869/*
870 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
871 * reports from it unless we explicitely request it.
872 */
873
874static void psmouse_deactivate(struct psmouse *psmouse)
875{
876 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
877 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
878 psmouse->ps2dev.serio->phys);
879
880 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
881}
882
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500883/*
884 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
885 */
886
887static int psmouse_poll(struct psmouse *psmouse)
888{
889 return ps2_command(&psmouse->ps2dev, psmouse->packet,
890 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
891}
892
893
894/*
895 * psmouse_resync() attempts to re-validate current protocol.
896 */
897
David Howellsc4028952006-11-22 14:57:56 +0000898static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500899{
David Howellsc4028952006-11-22 14:57:56 +0000900 struct psmouse *parent = NULL, *psmouse =
901 container_of(work, struct psmouse, resync_work);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500902 struct serio *serio = psmouse->ps2dev.serio;
903 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
904 int failed = 0, enabled = 0;
905 int i;
906
Ingo Molnarc14471d2006-02-19 00:22:11 -0500907 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500908
909 if (psmouse->state != PSMOUSE_RESYNCING)
910 goto out;
911
912 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
913 parent = serio_get_drvdata(serio->parent);
914 psmouse_deactivate(parent);
915 }
916
917/*
918 * Some mice don't ACK commands sent while they are in the middle of
919 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
920 * instead of ps2_command() which would wait for 200ms for an ACK
921 * that may never come.
922 * As an additional quirk ALPS touchpads may not only forget to ACK
923 * disable command but will stop reporting taps, so if we see that
924 * mouse at least once ACKs disable we will do full reconnect if ACK
925 * is missing.
926 */
927 psmouse->num_resyncs++;
928
929 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
930 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
931 failed = 1;
932 } else
933 psmouse->acks_disable_command = 1;
934
935/*
936 * Poll the mouse. If it was reset the packet will be shorter than
937 * psmouse->pktsize and ps2_command will fail. We do not expect and
938 * do not handle scenario when mouse "upgrades" its protocol while
939 * disconnected since it would require additional delay. If we ever
940 * see a mouse that does it we'll adjust the code.
941 */
942 if (!failed) {
943 if (psmouse->poll(psmouse))
944 failed = 1;
945 else {
946 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
947 for (i = 0; i < psmouse->pktsize; i++) {
948 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +0100949 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500950 if (rc != PSMOUSE_GOOD_DATA)
951 break;
952 }
953 if (rc != PSMOUSE_FULL_PACKET)
954 failed = 1;
955 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
956 }
957 }
958/*
959 * Now try to enable mouse. We try to do that even if poll failed and also
960 * repeat our attempts 5 times, otherwise we may be left out with disabled
961 * mouse.
962 */
963 for (i = 0; i < 5; i++) {
964 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
965 enabled = 1;
966 break;
967 }
968 msleep(200);
969 }
970
971 if (!enabled) {
972 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
973 psmouse->ps2dev.serio->phys);
974 failed = 1;
975 }
976
977 if (failed) {
978 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
979 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
980 serio_reconnect(serio);
981 } else
982 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
983
984 if (parent)
985 psmouse_activate(parent);
986 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -0500987 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500988}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990/*
991 * psmouse_cleanup() resets the mouse into power-on state.
992 */
993
994static void psmouse_cleanup(struct serio *serio)
995{
996 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -0500997 struct psmouse *parent = NULL;
998
999 mutex_lock(&psmouse_mutex);
1000
1001 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1002 parent = serio_get_drvdata(serio->parent);
1003 psmouse_deactivate(parent);
1004 }
1005
1006 psmouse_deactivate(psmouse);
1007
1008 if (psmouse->cleanup)
1009 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 psmouse_reset(psmouse);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001012
1013/*
1014 * Some boxes, such as HP nx7400, get terribly confused if mouse
1015 * is not fully enabled before suspending/shutting down.
1016 */
1017 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1018
1019 if (parent) {
1020 if (parent->pt_deactivate)
1021 parent->pt_deactivate(parent);
1022
1023 psmouse_activate(parent);
1024 }
1025
1026 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029/*
1030 * psmouse_disconnect() closes and frees.
1031 */
1032
1033static void psmouse_disconnect(struct serio *serio)
1034{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001035 struct psmouse *psmouse, *parent = NULL;
1036
1037 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001039 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Ingo Molnarc14471d2006-02-19 00:22:11 -05001041 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1044
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001045 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001046 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001047 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001048 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1051 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001052 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
1055 if (psmouse->disconnect)
1056 psmouse->disconnect(psmouse);
1057
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001058 if (parent && parent->pt_deactivate)
1059 parent->pt_deactivate(parent);
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 serio_close(serio);
1064 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001065 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001067
1068 if (parent)
1069 psmouse_activate(parent);
1070
Ingo Molnarc14471d2006-02-19 00:22:11 -05001071 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Helge Dellere38de672006-09-10 21:54:39 -04001074static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001075{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001076 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001077
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001078 input_dev->private = psmouse;
1079 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001080
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001081 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1082 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1083 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001084
1085 psmouse->set_rate = psmouse_set_rate;
1086 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001087 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001088 psmouse->protocol_handler = psmouse_process_byte;
1089 psmouse->pktsize = 3;
1090
1091 if (proto && (proto->detect || proto->init)) {
1092 if (proto->detect && proto->detect(psmouse, 1) < 0)
1093 return -1;
1094
1095 if (proto->init && proto->init(psmouse) < 0)
1096 return -1;
1097
1098 psmouse->type = proto->type;
1099 }
1100 else
1101 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1102
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001103 /*
1104 * If mouse's packet size is 3 there is no point in polling the
1105 * device in hopes to detect protocol reset - we won't get less
1106 * than 3 bytes response anyhow.
1107 */
1108 if (psmouse->pktsize == 3)
1109 psmouse->resync_time = 0;
1110
1111 /*
1112 * Some smart KVMs fake response to POLL command returning just
1113 * 3 bytes and messing up our resync logic, so if initial poll
1114 * fails we won't try polling the device anymore. Hopefully
1115 * such KVM will maintain initially selected protocol.
1116 */
1117 if (psmouse->resync_time && psmouse->poll(psmouse))
1118 psmouse->resync_time = 0;
1119
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001120 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1121 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001122
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001123 input_dev->name = psmouse->devname;
1124 input_dev->phys = psmouse->phys;
1125 input_dev->id.bustype = BUS_I8042;
1126 input_dev->id.vendor = 0x0002;
1127 input_dev->id.product = psmouse->type;
1128 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001129
1130 return 0;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/*
1134 * psmouse_connect() is a callback from the serio module when
1135 * an unhandled serio port is found.
1136 */
1137static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1138{
1139 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001140 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001141 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Ingo Molnarc14471d2006-02-19 00:22:11 -05001143 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /*
1146 * If this is a pass-through port deactivate parent so the device
1147 * connected to this port can be successfully identified
1148 */
1149 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1150 parent = serio_get_drvdata(serio->parent);
1151 psmouse_deactivate(parent);
1152 }
1153
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001154 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1155 input_dev = input_allocate_device();
1156 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001157 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 ps2_init(&psmouse->ps2dev, serio);
David Howellsc4028952006-11-22 14:57:56 +00001160 INIT_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001161 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001162 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1165
1166 serio_set_drvdata(serio, psmouse);
1167
Dmitry Torokhov72155612006-11-05 22:40:19 -05001168 error = serio_open(serio, drv);
1169 if (error)
1170 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001173 error = -ENODEV;
1174 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
1177 psmouse->rate = psmouse_rate;
1178 psmouse->resolution = psmouse_resolution;
1179 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001180 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001183 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 psmouse_initialize(psmouse);
1187
Dmitry Torokhov72155612006-11-05 22:40:19 -05001188 error = input_register_device(psmouse->dev);
1189 if (error)
1190 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (parent && parent->pt_activate)
1193 parent->pt_activate(parent);
1194
Dmitry Torokhov72155612006-11-05 22:40:19 -05001195 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1196 if (error)
1197 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 psmouse_activate(psmouse);
1200
Dmitry Torokhov72155612006-11-05 22:40:19 -05001201 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001202 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (parent)
1204 psmouse_activate(parent);
1205
Ingo Molnarc14471d2006-02-19 00:22:11 -05001206 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001208
1209 err_pt_deactivate:
1210 if (parent && parent->pt_deactivate)
1211 parent->pt_deactivate(parent);
1212 err_protocol_disconnect:
1213 if (psmouse->disconnect)
1214 psmouse->disconnect(psmouse);
1215 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1216 err_close_serio:
1217 serio_close(serio);
1218 err_clear_drvdata:
1219 serio_set_drvdata(serio, NULL);
1220 err_free:
1221 input_free_device(input_dev);
1222 kfree(psmouse);
1223
1224 retval = error;
1225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
1228
1229static int psmouse_reconnect(struct serio *serio)
1230{
1231 struct psmouse *psmouse = serio_get_drvdata(serio);
1232 struct psmouse *parent = NULL;
1233 struct serio_driver *drv = serio->drv;
1234 int rc = -1;
1235
1236 if (!drv || !psmouse) {
1237 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1238 return -1;
1239 }
1240
Ingo Molnarc14471d2006-02-19 00:22:11 -05001241 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1244 parent = serio_get_drvdata(serio->parent);
1245 psmouse_deactivate(parent);
1246 }
1247
1248 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1249
1250 if (psmouse->reconnect) {
1251 if (psmouse->reconnect(psmouse))
1252 goto out;
1253 } else if (psmouse_probe(psmouse) < 0 ||
1254 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1255 goto out;
1256
1257 /* ok, the device type (and capabilities) match the old one,
1258 * we can continue using it, complete intialization
1259 */
1260 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1261
1262 psmouse_initialize(psmouse);
1263
1264 if (parent && parent->pt_activate)
1265 parent->pt_activate(parent);
1266
1267 psmouse_activate(psmouse);
1268 rc = 0;
1269
1270out:
1271 /* If this is a pass-through port the parent waits to be activated */
1272 if (parent)
1273 psmouse_activate(parent);
1274
Ingo Molnarc14471d2006-02-19 00:22:11 -05001275 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return rc;
1277}
1278
1279static struct serio_device_id psmouse_serio_ids[] = {
1280 {
1281 .type = SERIO_8042,
1282 .proto = SERIO_ANY,
1283 .id = SERIO_ANY,
1284 .extra = SERIO_ANY,
1285 },
1286 {
1287 .type = SERIO_PS_PSTHRU,
1288 .proto = SERIO_ANY,
1289 .id = SERIO_ANY,
1290 .extra = SERIO_ANY,
1291 },
1292 { 0 }
1293};
1294
1295MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1296
1297static struct serio_driver psmouse_drv = {
1298 .driver = {
1299 .name = "psmouse",
1300 },
1301 .description = DRIVER_DESC,
1302 .id_table = psmouse_serio_ids,
1303 .interrupt = psmouse_interrupt,
1304 .connect = psmouse_connect,
1305 .reconnect = psmouse_reconnect,
1306 .disconnect = psmouse_disconnect,
1307 .cleanup = psmouse_cleanup,
1308};
1309
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001310ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1311 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001314 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1315 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 int retval;
1317
1318 retval = serio_pin_driver(serio);
1319 if (retval)
1320 return retval;
1321
1322 if (serio->drv != &psmouse_drv) {
1323 retval = -ENODEV;
1324 goto out;
1325 }
1326
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001327 psmouse = serio_get_drvdata(serio);
1328
1329 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331out:
1332 serio_unpin_driver(serio);
1333 return retval;
1334}
1335
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001336ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1337 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001340 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1341 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int retval;
1343
1344 retval = serio_pin_driver(serio);
1345 if (retval)
1346 return retval;
1347
1348 if (serio->drv != &psmouse_drv) {
1349 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001350 goto out_unpin;
1351 }
1352
Ingo Molnarc14471d2006-02-19 00:22:11 -05001353 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001354 if (retval)
1355 goto out_unpin;
1356
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001357 psmouse = serio_get_drvdata(serio);
1358
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001359 if (psmouse->state == PSMOUSE_IGNORE) {
1360 retval = -ENODEV;
Ingo Molnarc14471d2006-02-19 00:22:11 -05001361 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
1364 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1365 parent = serio_get_drvdata(serio->parent);
1366 psmouse_deactivate(parent);
1367 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 psmouse_deactivate(psmouse);
1370
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001371 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001373 if (retval != -ENODEV)
1374 psmouse_activate(psmouse);
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (parent)
1377 psmouse_activate(parent);
1378
Ingo Molnarc14471d2006-02-19 00:22:11 -05001379 out_unlock:
1380 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001381 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 serio_unpin_driver(serio);
1383 return retval;
1384}
1385
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001386static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1387{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001388 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001389
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001390 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001391}
1392
1393static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1394{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001395 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001396 unsigned long value;
1397 char *rest;
1398
1399 value = simple_strtoul(buf, &rest, 10);
1400 if (*rest)
1401 return -EINVAL;
1402
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001403 if ((unsigned int)value != value)
1404 return -EINVAL;
1405
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001406 *field = value;
1407
1408 return count;
1409}
1410
1411static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001412{
1413 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1414}
1415
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001416static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001417{
1418 struct serio *serio = psmouse->ps2dev.serio;
1419 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001420 struct input_dev *old_dev, *new_dev;
1421 const struct psmouse_protocol *proto, *old_proto;
1422 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001423 int retry = 0;
1424
Dmitry Torokhov72155612006-11-05 22:40:19 -05001425 proto = psmouse_protocol_by_name(buf, count);
1426 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001427 return -EINVAL;
1428
1429 if (psmouse->type == proto->type)
1430 return count;
1431
Dmitry Torokhov72155612006-11-05 22:40:19 -05001432 new_dev = input_allocate_device();
1433 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001434 return -ENOMEM;
1435
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001436 while (serio->child) {
1437 if (++retry > 3) {
1438 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001439 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001440 return -EIO;
1441 }
1442
Ingo Molnarc14471d2006-02-19 00:22:11 -05001443 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001444 serio_unpin_driver(serio);
1445 serio_unregister_child_port(serio);
1446 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001447 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001448
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001449 if (serio->drv != &psmouse_drv) {
1450 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001451 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001452 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001453
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001454 if (psmouse->type == proto->type) {
1455 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001456 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001457 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001458 }
1459
1460 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1461 parent = serio_get_drvdata(serio->parent);
1462 if (parent->pt_deactivate)
1463 parent->pt_deactivate(parent);
1464 }
1465
Dmitry Torokhov72155612006-11-05 22:40:19 -05001466 old_dev = psmouse->dev;
1467 old_proto = psmouse_protocol_by_type(psmouse->type);
1468
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001469 if (psmouse->disconnect)
1470 psmouse->disconnect(psmouse);
1471
1472 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001473
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001474 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001475 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1476
1477 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1478 psmouse_reset(psmouse);
1479 /* default to PSMOUSE_PS2 */
1480 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1481 }
1482
1483 psmouse_initialize(psmouse);
1484 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1485
Dmitry Torokhov72155612006-11-05 22:40:19 -05001486 error = input_register_device(psmouse->dev);
1487 if (error) {
1488 if (psmouse->disconnect)
1489 psmouse->disconnect(psmouse);
1490
1491 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1492 input_free_device(new_dev);
1493 psmouse->dev = old_dev;
1494 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1495 psmouse_switch_protocol(psmouse, old_proto);
1496 psmouse_initialize(psmouse);
1497 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1498
1499 return error;
1500 }
1501
1502 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001503
1504 if (parent && parent->pt_activate)
1505 parent->pt_activate(parent);
1506
1507 return count;
1508}
1509
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001510static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 unsigned long value;
1513 char *rest;
1514
1515 value = simple_strtoul(buf, &rest, 10);
1516 if (*rest)
1517 return -EINVAL;
1518
1519 psmouse->set_rate(psmouse, value);
1520 return count;
1521}
1522
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001523static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 unsigned long value;
1526 char *rest;
1527
1528 value = simple_strtoul(buf, &rest, 10);
1529 if (*rest)
1530 return -EINVAL;
1531
1532 psmouse->set_resolution(psmouse, value);
1533 return count;
1534}
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1538{
Helge Dellere38de672006-09-10 21:54:39 -04001539 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 if (!val)
1542 return -EINVAL;
1543
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001544 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001546 if (!proto || !proto->maxproto)
1547 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001549 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Stephen Evanchik541e3162005-08-08 01:26:18 -05001551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
1554static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1555{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001556 int type = *((unsigned int *)kp->arg);
1557
1558 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561static int __init psmouse_init(void)
1562{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001563 int err;
1564
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001565 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1566 if (!kpsmoused_wq) {
1567 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1568 return -ENOMEM;
1569 }
1570
Akinobu Mita153a9df02006-11-23 23:35:10 -05001571 err = serio_register_driver(&psmouse_drv);
1572 if (err)
1573 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001574
Akinobu Mita153a9df02006-11-23 23:35:10 -05001575 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578static void __exit psmouse_exit(void)
1579{
1580 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001581 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584module_init(psmouse_init);
1585module_exit(psmouse_exit);