blob: e626b1e737fa46a076402453c4ad4817d812f961 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define DRIVER_DESC "PS/2 mouse driver"
33
34MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
35MODULE_DESCRIPTION(DRIVER_DESC);
36MODULE_LICENSE("GPL");
37
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050038static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
40static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
42#define param_set_proto_abbrev psmouse_set_maxproto
43#define param_get_proto_abbrev psmouse_get_maxproto
44module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050045MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static unsigned int psmouse_resolution = 200;
48module_param_named(resolution, psmouse_resolution, uint, 0644);
49MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
50
51static unsigned int psmouse_rate = 100;
52module_param_named(rate, psmouse_rate, uint, 0644);
53MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
54
55static unsigned int psmouse_smartscroll = 1;
56module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
57MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
58
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050059static unsigned int psmouse_resetafter = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060module_param_named(resetafter, psmouse_resetafter, uint, 0644);
61MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
62
Dmitry Torokhov8bd0ee92006-03-11 00:23:38 -050063static unsigned int psmouse_resync_time;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050064module_param_named(resync_time, psmouse_resync_time, uint, 0644);
65MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
66
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050067PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
68 NULL,
69 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
70PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
71 (void *) offsetof(struct psmouse, rate),
72 psmouse_show_int_attr, psmouse_attr_set_rate);
73PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
74 (void *) offsetof(struct psmouse, resolution),
75 psmouse_show_int_attr, psmouse_attr_set_resolution);
76PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
77 (void *) offsetof(struct psmouse, resetafter),
78 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050079PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
80 (void *) offsetof(struct psmouse, resync_time),
81 psmouse_show_int_attr, psmouse_set_int_attr);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050082
83static struct attribute *psmouse_attributes[] = {
84 &psmouse_attr_protocol.dattr.attr,
85 &psmouse_attr_rate.dattr.attr,
86 &psmouse_attr_resolution.dattr.attr,
87 &psmouse_attr_resetafter.dattr.attr,
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -050088 &psmouse_attr_resync_time.dattr.attr,
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -050089 NULL
90};
91
92static struct attribute_group psmouse_attribute_group = {
93 .attrs = psmouse_attributes,
94};
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96__obsolete_setup("psmouse_noext");
97__obsolete_setup("psmouse_resolution=");
98__obsolete_setup("psmouse_smartscroll=");
99__obsolete_setup("psmouse_resetafter=");
100__obsolete_setup("psmouse_rate=");
101
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500102/*
Ingo Molnarc14471d2006-02-19 00:22:11 -0500103 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500104 * (connecting, disconnecting, changing rate or resolution via
105 * sysfs). We could use a per-device semaphore but since there
106 * rarely more than one PS/2 mouse connected and since semaphore
107 * is taken in "slow" paths it is not worth it.
108 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500109static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500110
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500111static struct workqueue_struct *kpsmoused_wq;
112
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500113struct psmouse_protocol {
114 enum psmouse_type type;
Helge Dellere38de672006-09-10 21:54:39 -0400115 const char *name;
116 const char *alias;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500117 int maxproto;
118 int (*detect)(struct psmouse *, int);
119 int (*init)(struct psmouse *);
120};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/*
123 * psmouse_process_byte() analyzes the PS/2 data stream and reports
124 * relevant events to the input module once full packet has arrived.
125 */
126
David Howells7d12e782006-10-05 14:55:46 +0100127static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500129 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 unsigned char *packet = psmouse->packet;
131
132 if (psmouse->pktcnt < psmouse->pktsize)
133 return PSMOUSE_GOOD_DATA;
134
135/*
136 * Full packet accumulated, process it
137 */
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/*
140 * Scroll wheel on IntelliMice, scroll buttons on NetMice
141 */
142
143 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
144 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
145
146/*
147 * Scroll wheel and buttons on IntelliMouse Explorer
148 */
149
150 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400151 switch (packet[3] & 0xC0) {
152 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
153 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
154 break;
155 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
156 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
157 break;
158 case 0x00:
159 case 0xC0:
160 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
161 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
162 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
163 break;
164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
167/*
168 * Extra buttons on Genius NewNet 3D
169 */
170
171 if (psmouse->type == PSMOUSE_GENPS) {
172 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
173 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
174 }
175
176/*
177 * Extra button on ThinkingMouse
178 */
179 if (psmouse->type == PSMOUSE_THINKPS) {
180 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
181 /* Without this bit of weirdness moving up gives wildly high Y changes. */
182 packet[1] |= (packet[0] & 0x40) << 1;
183 }
184
185/*
186 * Generic PS/2 Mouse
187 */
188
189 input_report_key(dev, BTN_LEFT, packet[0] & 1);
190 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
191 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
192
193 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
194 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
195
196 input_sync(dev);
197
198 return PSMOUSE_FULL_PACKET;
199}
200
201/*
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500202 * __psmouse_set_state() sets new psmouse state and resets all flags.
203 */
204
205static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
206{
207 psmouse->state = new_state;
208 psmouse->pktcnt = psmouse->out_of_sync = 0;
209 psmouse->ps2dev.flags = 0;
210 psmouse->last = jiffies;
211}
212
213
214/*
215 * psmouse_set_state() sets new psmouse state and resets all flags and
216 * counters while holding serio lock so fighting with interrupt handler
217 * is not a concern.
218 */
219
220static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
221{
222 serio_pause_rx(psmouse->ps2dev.serio);
223 __psmouse_set_state(psmouse, new_state);
224 serio_continue_rx(psmouse->ps2dev.serio);
225}
226
227/*
228 * psmouse_handle_byte() processes one byte of the input data stream
229 * by calling corresponding protocol handler.
230 */
231
David Howells7d12e782006-10-05 14:55:46 +0100232static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500233{
David Howells7d12e782006-10-05 14:55:46 +0100234 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500235
236 switch (rc) {
237 case PSMOUSE_BAD_DATA:
238 if (psmouse->state == PSMOUSE_ACTIVATED) {
239 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
240 psmouse->name, psmouse->phys, psmouse->pktcnt);
241 if (++psmouse->out_of_sync == psmouse->resetafter) {
242 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
243 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
244 serio_reconnect(psmouse->ps2dev.serio);
245 return -1;
246 }
247 }
248 psmouse->pktcnt = 0;
249 break;
250
251 case PSMOUSE_FULL_PACKET:
252 psmouse->pktcnt = 0;
253 if (psmouse->out_of_sync) {
254 psmouse->out_of_sync = 0;
255 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
256 psmouse->name, psmouse->phys);
257 }
258 break;
259
260 case PSMOUSE_GOOD_DATA:
261 break;
262 }
263 return 0;
264}
265
266/*
267 * psmouse_interrupt() handles incoming characters, either passing them
268 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
270
271static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100272 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 if (psmouse->state == PSMOUSE_IGNORE)
277 goto out;
278
279 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
280 if (psmouse->state == PSMOUSE_ACTIVATED)
281 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
282 flags & SERIO_TIMEOUT ? " timeout" : "",
283 flags & SERIO_PARITY ? " bad parity" : "");
284 ps2_cmd_aborted(&psmouse->ps2dev);
285 goto out;
286 }
287
288 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
289 if (ps2_handle_ack(&psmouse->ps2dev, data))
290 goto out;
291
292 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
293 if (ps2_handle_response(&psmouse->ps2dev, data))
294 goto out;
295
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500296 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto out;
298
299 if (psmouse->state == PSMOUSE_ACTIVATED &&
300 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500301 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500303 psmouse->badbyte = psmouse->packet[0];
304 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
305 queue_work(kpsmoused_wq, &psmouse->resync_work);
306 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500310/*
311 * Check if this is a new device announcement (0xAA 0x00)
312 */
313 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400314 if (psmouse->pktcnt == 1) {
315 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500319 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
320 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
321 serio_reconnect(serio);
322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500324/*
325 * Not a new device, try processing first byte normally
326 */
327 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100328 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500329 goto out;
330
331 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500334/*
335 * See if we need to force resync because mouse was idle for too long
336 */
337 if (psmouse->state == PSMOUSE_ACTIVATED &&
338 psmouse->pktcnt == 1 && psmouse->resync_time &&
339 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
340 psmouse->badbyte = psmouse->packet[0];
341 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
342 queue_work(kpsmoused_wq, &psmouse->resync_work);
343 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500345
346 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100347 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500348
349 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return IRQ_HANDLED;
351}
352
353
354/*
355 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
356 * using sliced syntax, understood by advanced devices, such as Logitech
357 * or Synaptics touchpads. The command is encoded as:
358 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
359 * is the command.
360 */
361int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
362{
363 int i;
364
365 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
366 return -1;
367
368 for (i = 6; i >= 0; i -= 2) {
369 unsigned char d = (command >> i) & 3;
370 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
371 return -1;
372 }
373
374 return 0;
375}
376
377
378/*
379 * psmouse_reset() resets the mouse into power-on state.
380 */
381int psmouse_reset(struct psmouse *psmouse)
382{
383 unsigned char param[2];
384
385 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
386 return -1;
387
388 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
389 return -1;
390
391 return 0;
392}
393
394
395/*
396 * Genius NetMouse magic init.
397 */
398static int genius_detect(struct psmouse *psmouse, int set_properties)
399{
400 struct ps2dev *ps2dev = &psmouse->ps2dev;
401 unsigned char param[4];
402
403 param[0] = 3;
404 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
405 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
406 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
407 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
408 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
409
410 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
411 return -1;
412
413 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500414 set_bit(BTN_EXTRA, psmouse->dev->keybit);
415 set_bit(BTN_SIDE, psmouse->dev->keybit);
416 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500419 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 psmouse->pktsize = 4;
421 }
422
423 return 0;
424}
425
426/*
427 * IntelliMouse magic init.
428 */
429static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
430{
431 struct ps2dev *ps2dev = &psmouse->ps2dev;
432 unsigned char param[2];
433
434 param[0] = 200;
435 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
436 param[0] = 100;
437 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
438 param[0] = 80;
439 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
440 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
441
442 if (param[0] != 3)
443 return -1;
444
445 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500446 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
447 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if (!psmouse->vendor) psmouse->vendor = "Generic";
450 if (!psmouse->name) psmouse->name = "Wheel Mouse";
451 psmouse->pktsize = 4;
452 }
453
454 return 0;
455}
456
457/*
458 * Try IntelliMouse/Explorer magic init.
459 */
460static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
461{
462 struct ps2dev *ps2dev = &psmouse->ps2dev;
463 unsigned char param[2];
464
465 intellimouse_detect(psmouse, 0);
466
467 param[0] = 200;
468 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
469 param[0] = 200;
470 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
471 param[0] = 80;
472 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
473 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
474
475 if (param[0] != 4)
476 return -1;
477
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400478/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
479 param[0] = 200;
480 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
481 param[0] = 80;
482 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
483 param[0] = 40;
484 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500487 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
488 set_bit(REL_WHEEL, psmouse->dev->relbit);
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400489 set_bit(REL_HWHEEL, psmouse->dev->relbit);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500490 set_bit(BTN_SIDE, psmouse->dev->keybit);
491 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if (!psmouse->vendor) psmouse->vendor = "Generic";
494 if (!psmouse->name) psmouse->name = "Explorer Mouse";
495 psmouse->pktsize = 4;
496 }
497
498 return 0;
499}
500
501/*
502 * Kensington ThinkingMouse / ExpertMouse magic init.
503 */
504static int thinking_detect(struct psmouse *psmouse, int set_properties)
505{
506 struct ps2dev *ps2dev = &psmouse->ps2dev;
507 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400508 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int i;
510
511 param[0] = 10;
512 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
513 param[0] = 0;
514 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400515 for (i = 0; i < ARRAY_SIZE(seq); i++) {
516 param[0] = seq[i];
517 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
520
521 if (param[0] != 2)
522 return -1;
523
524 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500525 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 psmouse->vendor = "Kensington";
528 psmouse->name = "ThinkingMouse";
529 }
530
531 return 0;
532}
533
534/*
535 * Bare PS/2 protocol "detection". Always succeeds.
536 */
537static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
538{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500539 if (set_properties) {
540 if (!psmouse->vendor) psmouse->vendor = "Generic";
541 if (!psmouse->name) psmouse->name = "Mouse";
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 return 0;
545}
546
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/*
549 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
550 * the mouse may have.
551 */
552
553static int psmouse_extensions(struct psmouse *psmouse,
554 unsigned int max_proto, int set_properties)
555{
556 int synaptics_hardware = 0;
557
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500558/*
559 * We always check for lifebook because it does not disturb mouse
560 * (it only checks DMI information).
561 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500562 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500563 if (max_proto > PSMOUSE_IMEX) {
564 if (!set_properties || lifebook_init(psmouse) == 0)
565 return PSMOUSE_LIFEBOOK;
566 }
567 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/*
570 * Try Kensington ThinkingMouse (we try first, because synaptics probe
571 * upsets the thinkingmouse).
572 */
573
574 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
575 return PSMOUSE_THINKPS;
576
577/*
578 * Try Synaptics TouchPad
579 */
580 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
581 synaptics_hardware = 1;
582
583 if (max_proto > PSMOUSE_IMEX) {
584 if (!set_properties || synaptics_init(psmouse) == 0)
585 return PSMOUSE_SYNAPTICS;
586/*
587 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
588 * Unfortunately Logitech/Genius probes confuse some firmware versions so
589 * we'll have to skip them.
590 */
591 max_proto = PSMOUSE_IMEX;
592 }
593/*
594 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
595 */
596 synaptics_reset(psmouse);
597 }
598
599/*
600 * Try ALPS TouchPad
601 */
602 if (max_proto > PSMOUSE_IMEX) {
603 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
604 if (alps_detect(psmouse, set_properties) == 0) {
605 if (!set_properties || alps_init(psmouse) == 0)
606 return PSMOUSE_ALPS;
607/*
608 * Init failed, try basic relative protocols
609 */
610 max_proto = PSMOUSE_IMEX;
611 }
612 }
613
614 if (max_proto > PSMOUSE_IMEX && genius_detect(psmouse, set_properties) == 0)
615 return PSMOUSE_GENPS;
616
617 if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
618 return PSMOUSE_PS2PP;
619
Dmitry Torokhovba449952005-12-21 00:51:31 -0500620 if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
621 return PSMOUSE_TRACKPOINT;
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/*
624 * Reset to defaults in case the device got confused by extended
Dmitry Torokhovba449952005-12-21 00:51:31 -0500625 * protocol probes. Note that we do full reset becuase some mice
626 * put themselves to sleep when see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
Dmitry Torokhovba449952005-12-21 00:51:31 -0500628 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
631 return PSMOUSE_IMEX;
632
633 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
634 return PSMOUSE_IMPS;
635
636/*
637 * Okay, all failed, we have a standard mouse here. The number of the buttons
638 * is still a question, though. We assume 3.
639 */
640 ps2bare_detect(psmouse, set_properties);
641
642 if (synaptics_hardware) {
643/*
644 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
645 * We need to reset the touchpad because if there is a track point on the
646 * pass through port it could get disabled while probing for protocol
647 * extensions.
648 */
649 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 return PSMOUSE_PS2;
653}
654
Helge Dellere38de672006-09-10 21:54:39 -0400655static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500656 {
657 .type = PSMOUSE_PS2,
658 .name = "PS/2",
659 .alias = "bare",
660 .maxproto = 1,
661 .detect = ps2bare_detect,
662 },
663 {
664 .type = PSMOUSE_PS2PP,
665 .name = "PS2++",
666 .alias = "logitech",
667 .detect = ps2pp_init,
668 },
669 {
670 .type = PSMOUSE_THINKPS,
671 .name = "ThinkPS/2",
672 .alias = "thinkps",
673 .detect = thinking_detect,
674 },
675 {
676 .type = PSMOUSE_GENPS,
677 .name = "GenPS/2",
678 .alias = "genius",
679 .detect = genius_detect,
680 },
681 {
682 .type = PSMOUSE_IMPS,
683 .name = "ImPS/2",
684 .alias = "imps",
685 .maxproto = 1,
686 .detect = intellimouse_detect,
687 },
688 {
689 .type = PSMOUSE_IMEX,
690 .name = "ImExPS/2",
691 .alias = "exps",
692 .maxproto = 1,
693 .detect = im_explorer_detect,
694 },
695 {
696 .type = PSMOUSE_SYNAPTICS,
697 .name = "SynPS/2",
698 .alias = "synaptics",
699 .detect = synaptics_detect,
700 .init = synaptics_init,
701 },
702 {
703 .type = PSMOUSE_ALPS,
704 .name = "AlpsPS/2",
705 .alias = "alps",
706 .detect = alps_detect,
707 .init = alps_init,
708 },
709 {
710 .type = PSMOUSE_LIFEBOOK,
711 .name = "LBPS/2",
712 .alias = "lifebook",
713 .init = lifebook_init,
714 },
715 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500716 .type = PSMOUSE_TRACKPOINT,
717 .name = "TPPS/2",
718 .alias = "trackpoint",
719 .detect = trackpoint_detect,
720 },
721 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500722 .type = PSMOUSE_AUTO,
723 .name = "auto",
724 .alias = "any",
725 .maxproto = 1,
726 },
727};
728
Helge Dellere38de672006-09-10 21:54:39 -0400729static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500730{
731 int i;
732
733 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
734 if (psmouse_protocols[i].type == type)
735 return &psmouse_protocols[i];
736
737 WARN_ON(1);
738 return &psmouse_protocols[0];
739}
740
Helge Dellere38de672006-09-10 21:54:39 -0400741static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500742{
Helge Dellere38de672006-09-10 21:54:39 -0400743 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500744 int i;
745
746 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
747 p = &psmouse_protocols[i];
748
749 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
750 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
751 return &psmouse_protocols[i];
752 }
753
754 return NULL;
755}
756
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
759 * psmouse_probe() probes for a PS/2 mouse.
760 */
761
762static int psmouse_probe(struct psmouse *psmouse)
763{
764 struct ps2dev *ps2dev = &psmouse->ps2dev;
765 unsigned char param[2];
766
767/*
768 * First, we check if it's a mouse. It should send 0x00 or 0x03
769 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500770 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
771 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
773
774 param[0] = 0xa5;
775 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
776 return -1;
777
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500778 if (param[0] != 0x00 && param[0] != 0x03 &&
779 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -1;
781
782/*
783 * Then we reset and disable the mouse so that it doesn't generate events.
784 */
785
786 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
787 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
788
789 return 0;
790}
791
792/*
793 * Here we set the mouse resolution.
794 */
795
796void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
797{
Helge Dellere38de672006-09-10 21:54:39 -0400798 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
799 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (resolution == 0 || resolution > 200)
802 resolution = 200;
803
Helge Dellere38de672006-09-10 21:54:39 -0400804 p = params[resolution / 50];
805 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
806 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809/*
810 * Here we set the mouse report rate.
811 */
812
813static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
814{
Helge Dellere38de672006-09-10 21:54:39 -0400815 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
816 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int i = 0;
818
819 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400820 r = rates[i];
821 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
822 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825/*
826 * psmouse_initialize() initializes the mouse to a sane state.
827 */
828
829static void psmouse_initialize(struct psmouse *psmouse)
830{
831/*
832 * We set the mouse into streaming mode.
833 */
834
835 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM);
836
837/*
838 * We set the mouse report rate, resolution and scaling.
839 */
840
841 if (psmouse_max_proto != PSMOUSE_PS2) {
842 psmouse->set_rate(psmouse, psmouse->rate);
843 psmouse->set_resolution(psmouse, psmouse->resolution);
844 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
845 }
846}
847
848/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * psmouse_activate() enables the mouse so that we get motion reports from it.
850 */
851
852static void psmouse_activate(struct psmouse *psmouse)
853{
854 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
855 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
856 psmouse->ps2dev.serio->phys);
857
858 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
859}
860
861
862/*
863 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
864 * reports from it unless we explicitely request it.
865 */
866
867static void psmouse_deactivate(struct psmouse *psmouse)
868{
869 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
870 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
871 psmouse->ps2dev.serio->phys);
872
873 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
874}
875
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500876/*
877 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
878 */
879
880static int psmouse_poll(struct psmouse *psmouse)
881{
882 return ps2_command(&psmouse->ps2dev, psmouse->packet,
883 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
884}
885
886
887/*
888 * psmouse_resync() attempts to re-validate current protocol.
889 */
890
891static void psmouse_resync(void *p)
892{
893 struct psmouse *psmouse = p, *parent = NULL;
894 struct serio *serio = psmouse->ps2dev.serio;
895 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
896 int failed = 0, enabled = 0;
897 int i;
898
Ingo Molnarc14471d2006-02-19 00:22:11 -0500899 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500900
901 if (psmouse->state != PSMOUSE_RESYNCING)
902 goto out;
903
904 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
905 parent = serio_get_drvdata(serio->parent);
906 psmouse_deactivate(parent);
907 }
908
909/*
910 * Some mice don't ACK commands sent while they are in the middle of
911 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
912 * instead of ps2_command() which would wait for 200ms for an ACK
913 * that may never come.
914 * As an additional quirk ALPS touchpads may not only forget to ACK
915 * disable command but will stop reporting taps, so if we see that
916 * mouse at least once ACKs disable we will do full reconnect if ACK
917 * is missing.
918 */
919 psmouse->num_resyncs++;
920
921 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
922 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
923 failed = 1;
924 } else
925 psmouse->acks_disable_command = 1;
926
927/*
928 * Poll the mouse. If it was reset the packet will be shorter than
929 * psmouse->pktsize and ps2_command will fail. We do not expect and
930 * do not handle scenario when mouse "upgrades" its protocol while
931 * disconnected since it would require additional delay. If we ever
932 * see a mouse that does it we'll adjust the code.
933 */
934 if (!failed) {
935 if (psmouse->poll(psmouse))
936 failed = 1;
937 else {
938 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
939 for (i = 0; i < psmouse->pktsize; i++) {
940 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +0100941 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500942 if (rc != PSMOUSE_GOOD_DATA)
943 break;
944 }
945 if (rc != PSMOUSE_FULL_PACKET)
946 failed = 1;
947 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
948 }
949 }
950/*
951 * Now try to enable mouse. We try to do that even if poll failed and also
952 * repeat our attempts 5 times, otherwise we may be left out with disabled
953 * mouse.
954 */
955 for (i = 0; i < 5; i++) {
956 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
957 enabled = 1;
958 break;
959 }
960 msleep(200);
961 }
962
963 if (!enabled) {
964 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
965 psmouse->ps2dev.serio->phys);
966 failed = 1;
967 }
968
969 if (failed) {
970 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
971 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
972 serio_reconnect(serio);
973 } else
974 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
975
976 if (parent)
977 psmouse_activate(parent);
978 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -0500979 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -0500980}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982/*
983 * psmouse_cleanup() resets the mouse into power-on state.
984 */
985
986static void psmouse_cleanup(struct serio *serio)
987{
988 struct psmouse *psmouse = serio_get_drvdata(serio);
989
990 psmouse_reset(psmouse);
991}
992
993/*
994 * psmouse_disconnect() closes and frees.
995 */
996
997static void psmouse_disconnect(struct serio *serio)
998{
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500999 struct psmouse *psmouse, *parent = NULL;
1000
1001 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001003 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Ingo Molnarc14471d2006-02-19 00:22:11 -05001005 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1008
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001009 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001010 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001011 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001012 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1015 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001016 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018
1019 if (psmouse->disconnect)
1020 psmouse->disconnect(psmouse);
1021
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001022 if (parent && parent->pt_deactivate)
1023 parent->pt_deactivate(parent);
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 serio_close(serio);
1028 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001029 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001031
1032 if (parent)
1033 psmouse_activate(parent);
1034
Ingo Molnarc14471d2006-02-19 00:22:11 -05001035 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Helge Dellere38de672006-09-10 21:54:39 -04001038static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001039{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001040 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001041
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001042 input_dev->private = psmouse;
1043 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001044
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001045 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1046 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1047 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001048
1049 psmouse->set_rate = psmouse_set_rate;
1050 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001051 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001052 psmouse->protocol_handler = psmouse_process_byte;
1053 psmouse->pktsize = 3;
1054
1055 if (proto && (proto->detect || proto->init)) {
1056 if (proto->detect && proto->detect(psmouse, 1) < 0)
1057 return -1;
1058
1059 if (proto->init && proto->init(psmouse) < 0)
1060 return -1;
1061
1062 psmouse->type = proto->type;
1063 }
1064 else
1065 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1066
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001067 /*
1068 * If mouse's packet size is 3 there is no point in polling the
1069 * device in hopes to detect protocol reset - we won't get less
1070 * than 3 bytes response anyhow.
1071 */
1072 if (psmouse->pktsize == 3)
1073 psmouse->resync_time = 0;
1074
1075 /*
1076 * Some smart KVMs fake response to POLL command returning just
1077 * 3 bytes and messing up our resync logic, so if initial poll
1078 * fails we won't try polling the device anymore. Hopefully
1079 * such KVM will maintain initially selected protocol.
1080 */
1081 if (psmouse->resync_time && psmouse->poll(psmouse))
1082 psmouse->resync_time = 0;
1083
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001084 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1085 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001086
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001087 input_dev->name = psmouse->devname;
1088 input_dev->phys = psmouse->phys;
1089 input_dev->id.bustype = BUS_I8042;
1090 input_dev->id.vendor = 0x0002;
1091 input_dev->id.product = psmouse->type;
1092 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001093
1094 return 0;
1095}
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/*
1098 * psmouse_connect() is a callback from the serio module when
1099 * an unhandled serio port is found.
1100 */
1101static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1102{
1103 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001104 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001105 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Ingo Molnarc14471d2006-02-19 00:22:11 -05001107 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /*
1110 * If this is a pass-through port deactivate parent so the device
1111 * connected to this port can be successfully identified
1112 */
1113 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1114 parent = serio_get_drvdata(serio->parent);
1115 psmouse_deactivate(parent);
1116 }
1117
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001118 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1119 input_dev = input_allocate_device();
1120 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001121 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 ps2_init(&psmouse->ps2dev, serio);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001124 INIT_WORK(&psmouse->resync_work, psmouse_resync, psmouse);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001125 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001126 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1129
1130 serio_set_drvdata(serio, psmouse);
1131
Dmitry Torokhov72155612006-11-05 22:40:19 -05001132 error = serio_open(serio, drv);
1133 if (error)
1134 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001137 error = -ENODEV;
1138 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
1141 psmouse->rate = psmouse_rate;
1142 psmouse->resolution = psmouse_resolution;
1143 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001144 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001147 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 psmouse_initialize(psmouse);
1151
Dmitry Torokhov72155612006-11-05 22:40:19 -05001152 error = input_register_device(psmouse->dev);
1153 if (error)
1154 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (parent && parent->pt_activate)
1157 parent->pt_activate(parent);
1158
Dmitry Torokhov72155612006-11-05 22:40:19 -05001159 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1160 if (error)
1161 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 psmouse_activate(psmouse);
1164
Dmitry Torokhov72155612006-11-05 22:40:19 -05001165 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001166 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (parent)
1168 psmouse_activate(parent);
1169
Ingo Molnarc14471d2006-02-19 00:22:11 -05001170 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001172
1173 err_pt_deactivate:
1174 if (parent && parent->pt_deactivate)
1175 parent->pt_deactivate(parent);
1176 err_protocol_disconnect:
1177 if (psmouse->disconnect)
1178 psmouse->disconnect(psmouse);
1179 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1180 err_close_serio:
1181 serio_close(serio);
1182 err_clear_drvdata:
1183 serio_set_drvdata(serio, NULL);
1184 err_free:
1185 input_free_device(input_dev);
1186 kfree(psmouse);
1187
1188 retval = error;
1189 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192
1193static int psmouse_reconnect(struct serio *serio)
1194{
1195 struct psmouse *psmouse = serio_get_drvdata(serio);
1196 struct psmouse *parent = NULL;
1197 struct serio_driver *drv = serio->drv;
1198 int rc = -1;
1199
1200 if (!drv || !psmouse) {
1201 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1202 return -1;
1203 }
1204
Ingo Molnarc14471d2006-02-19 00:22:11 -05001205 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1208 parent = serio_get_drvdata(serio->parent);
1209 psmouse_deactivate(parent);
1210 }
1211
1212 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1213
1214 if (psmouse->reconnect) {
1215 if (psmouse->reconnect(psmouse))
1216 goto out;
1217 } else if (psmouse_probe(psmouse) < 0 ||
1218 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1219 goto out;
1220
1221 /* ok, the device type (and capabilities) match the old one,
1222 * we can continue using it, complete intialization
1223 */
1224 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1225
1226 psmouse_initialize(psmouse);
1227
1228 if (parent && parent->pt_activate)
1229 parent->pt_activate(parent);
1230
1231 psmouse_activate(psmouse);
1232 rc = 0;
1233
1234out:
1235 /* If this is a pass-through port the parent waits to be activated */
1236 if (parent)
1237 psmouse_activate(parent);
1238
Ingo Molnarc14471d2006-02-19 00:22:11 -05001239 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return rc;
1241}
1242
1243static struct serio_device_id psmouse_serio_ids[] = {
1244 {
1245 .type = SERIO_8042,
1246 .proto = SERIO_ANY,
1247 .id = SERIO_ANY,
1248 .extra = SERIO_ANY,
1249 },
1250 {
1251 .type = SERIO_PS_PSTHRU,
1252 .proto = SERIO_ANY,
1253 .id = SERIO_ANY,
1254 .extra = SERIO_ANY,
1255 },
1256 { 0 }
1257};
1258
1259MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1260
1261static struct serio_driver psmouse_drv = {
1262 .driver = {
1263 .name = "psmouse",
1264 },
1265 .description = DRIVER_DESC,
1266 .id_table = psmouse_serio_ids,
1267 .interrupt = psmouse_interrupt,
1268 .connect = psmouse_connect,
1269 .reconnect = psmouse_reconnect,
1270 .disconnect = psmouse_disconnect,
1271 .cleanup = psmouse_cleanup,
1272};
1273
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001274ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1275 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001278 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1279 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int retval;
1281
1282 retval = serio_pin_driver(serio);
1283 if (retval)
1284 return retval;
1285
1286 if (serio->drv != &psmouse_drv) {
1287 retval = -ENODEV;
1288 goto out;
1289 }
1290
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001291 psmouse = serio_get_drvdata(serio);
1292
1293 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295out:
1296 serio_unpin_driver(serio);
1297 return retval;
1298}
1299
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001300ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1301 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001304 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1305 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int retval;
1307
1308 retval = serio_pin_driver(serio);
1309 if (retval)
1310 return retval;
1311
1312 if (serio->drv != &psmouse_drv) {
1313 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001314 goto out_unpin;
1315 }
1316
Ingo Molnarc14471d2006-02-19 00:22:11 -05001317 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001318 if (retval)
1319 goto out_unpin;
1320
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001321 psmouse = serio_get_drvdata(serio);
1322
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001323 if (psmouse->state == PSMOUSE_IGNORE) {
1324 retval = -ENODEV;
Ingo Molnarc14471d2006-02-19 00:22:11 -05001325 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
1328 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1329 parent = serio_get_drvdata(serio->parent);
1330 psmouse_deactivate(parent);
1331 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 psmouse_deactivate(psmouse);
1334
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001335 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001337 if (retval != -ENODEV)
1338 psmouse_activate(psmouse);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (parent)
1341 psmouse_activate(parent);
1342
Ingo Molnarc14471d2006-02-19 00:22:11 -05001343 out_unlock:
1344 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001345 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 serio_unpin_driver(serio);
1347 return retval;
1348}
1349
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001350static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1351{
Sergey Vlasoveb5d58292006-11-09 00:34:27 -05001352 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001353
Sergey Vlasoveb5d58292006-11-09 00:34:27 -05001354 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001355}
1356
1357static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1358{
Sergey Vlasoveb5d58292006-11-09 00:34:27 -05001359 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001360 unsigned long value;
1361 char *rest;
1362
1363 value = simple_strtoul(buf, &rest, 10);
1364 if (*rest)
1365 return -EINVAL;
1366
Sergey Vlasoveb5d58292006-11-09 00:34:27 -05001367 if ((unsigned int)value != value)
1368 return -EINVAL;
1369
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001370 *field = value;
1371
1372 return count;
1373}
1374
1375static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001376{
1377 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1378}
1379
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001380static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001381{
1382 struct serio *serio = psmouse->ps2dev.serio;
1383 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001384 struct input_dev *old_dev, *new_dev;
1385 const struct psmouse_protocol *proto, *old_proto;
1386 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001387 int retry = 0;
1388
Dmitry Torokhov72155612006-11-05 22:40:19 -05001389 proto = psmouse_protocol_by_name(buf, count);
1390 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001391 return -EINVAL;
1392
1393 if (psmouse->type == proto->type)
1394 return count;
1395
Dmitry Torokhov72155612006-11-05 22:40:19 -05001396 new_dev = input_allocate_device();
1397 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001398 return -ENOMEM;
1399
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001400 while (serio->child) {
1401 if (++retry > 3) {
1402 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001403 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001404 return -EIO;
1405 }
1406
Ingo Molnarc14471d2006-02-19 00:22:11 -05001407 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001408 serio_unpin_driver(serio);
1409 serio_unregister_child_port(serio);
1410 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001411 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001412
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001413 if (serio->drv != &psmouse_drv) {
1414 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001415 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001416 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001417
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001418 if (psmouse->type == proto->type) {
1419 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001420 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001421 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001422 }
1423
1424 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1425 parent = serio_get_drvdata(serio->parent);
1426 if (parent->pt_deactivate)
1427 parent->pt_deactivate(parent);
1428 }
1429
Dmitry Torokhov72155612006-11-05 22:40:19 -05001430 old_dev = psmouse->dev;
1431 old_proto = psmouse_protocol_by_type(psmouse->type);
1432
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001433 if (psmouse->disconnect)
1434 psmouse->disconnect(psmouse);
1435
1436 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001437
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001438 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001439 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1440
1441 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1442 psmouse_reset(psmouse);
1443 /* default to PSMOUSE_PS2 */
1444 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1445 }
1446
1447 psmouse_initialize(psmouse);
1448 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1449
Dmitry Torokhov72155612006-11-05 22:40:19 -05001450 error = input_register_device(psmouse->dev);
1451 if (error) {
1452 if (psmouse->disconnect)
1453 psmouse->disconnect(psmouse);
1454
1455 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1456 input_free_device(new_dev);
1457 psmouse->dev = old_dev;
1458 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1459 psmouse_switch_protocol(psmouse, old_proto);
1460 psmouse_initialize(psmouse);
1461 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1462
1463 return error;
1464 }
1465
1466 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001467
1468 if (parent && parent->pt_activate)
1469 parent->pt_activate(parent);
1470
1471 return count;
1472}
1473
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001474static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 unsigned long value;
1477 char *rest;
1478
1479 value = simple_strtoul(buf, &rest, 10);
1480 if (*rest)
1481 return -EINVAL;
1482
1483 psmouse->set_rate(psmouse, value);
1484 return count;
1485}
1486
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001487static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 unsigned long value;
1490 char *rest;
1491
1492 value = simple_strtoul(buf, &rest, 10);
1493 if (*rest)
1494 return -EINVAL;
1495
1496 psmouse->set_resolution(psmouse, value);
1497 return count;
1498}
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1502{
Helge Dellere38de672006-09-10 21:54:39 -04001503 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 if (!val)
1506 return -EINVAL;
1507
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001508 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001510 if (!proto || !proto->maxproto)
1511 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001513 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Stephen Evanchik541e3162005-08-08 01:26:18 -05001515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
1518static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1519{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001520 int type = *((unsigned int *)kp->arg);
1521
1522 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525static int __init psmouse_init(void)
1526{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001527 int err;
1528
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001529 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1530 if (!kpsmoused_wq) {
1531 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1532 return -ENOMEM;
1533 }
1534
Akinobu Mita153a9df02006-11-23 23:35:10 -05001535 err = serio_register_driver(&psmouse_drv);
1536 if (err)
1537 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001538
Akinobu Mita153a9df02006-11-23 23:35:10 -05001539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
1542static void __exit psmouse_exit(void)
1543{
1544 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001545 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548module_init(psmouse_init);
1549module_exit(psmouse_exit);