blob: a0e4a033e2db553346e646044a8bf25c298735fb [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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-01-14 00:27:37 -0500233{
David Howells7d12e782006-10-05 14:55:46 +0100234 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-01-14 00:27:37 -0500345
346 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100347 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f2006-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 Torokhovf0d5c6f2006-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
David Howellsc4028952006-11-22 14:57:56 +0000891static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500892{
David Howellsc4028952006-11-22 14:57:56 +0000893 struct psmouse *parent = NULL, *psmouse =
894 container_of(work, struct psmouse, resync_work);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500895 struct serio *serio = psmouse->ps2dev.serio;
896 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
897 int failed = 0, enabled = 0;
898 int i;
899
Ingo Molnarc14471d2006-02-19 00:22:11 -0500900 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500901
902 if (psmouse->state != PSMOUSE_RESYNCING)
903 goto out;
904
905 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
906 parent = serio_get_drvdata(serio->parent);
907 psmouse_deactivate(parent);
908 }
909
910/*
911 * Some mice don't ACK commands sent while they are in the middle of
912 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
913 * instead of ps2_command() which would wait for 200ms for an ACK
914 * that may never come.
915 * As an additional quirk ALPS touchpads may not only forget to ACK
916 * disable command but will stop reporting taps, so if we see that
917 * mouse at least once ACKs disable we will do full reconnect if ACK
918 * is missing.
919 */
920 psmouse->num_resyncs++;
921
922 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
923 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
924 failed = 1;
925 } else
926 psmouse->acks_disable_command = 1;
927
928/*
929 * Poll the mouse. If it was reset the packet will be shorter than
930 * psmouse->pktsize and ps2_command will fail. We do not expect and
931 * do not handle scenario when mouse "upgrades" its protocol while
932 * disconnected since it would require additional delay. If we ever
933 * see a mouse that does it we'll adjust the code.
934 */
935 if (!failed) {
936 if (psmouse->poll(psmouse))
937 failed = 1;
938 else {
939 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
940 for (i = 0; i < psmouse->pktsize; i++) {
941 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +0100942 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500943 if (rc != PSMOUSE_GOOD_DATA)
944 break;
945 }
946 if (rc != PSMOUSE_FULL_PACKET)
947 failed = 1;
948 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
949 }
950 }
951/*
952 * Now try to enable mouse. We try to do that even if poll failed and also
953 * repeat our attempts 5 times, otherwise we may be left out with disabled
954 * mouse.
955 */
956 for (i = 0; i < 5; i++) {
957 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
958 enabled = 1;
959 break;
960 }
961 msleep(200);
962 }
963
964 if (!enabled) {
965 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
966 psmouse->ps2dev.serio->phys);
967 failed = 1;
968 }
969
970 if (failed) {
971 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
972 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
973 serio_reconnect(serio);
974 } else
975 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
976
977 if (parent)
978 psmouse_activate(parent);
979 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -0500980 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500981}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983/*
984 * psmouse_cleanup() resets the mouse into power-on state.
985 */
986
987static void psmouse_cleanup(struct serio *serio)
988{
989 struct psmouse *psmouse = serio_get_drvdata(serio);
990
991 psmouse_reset(psmouse);
992}
993
994/*
995 * psmouse_disconnect() closes and frees.
996 */
997
998static void psmouse_disconnect(struct serio *serio)
999{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001000 struct psmouse *psmouse, *parent = NULL;
1001
1002 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001004 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Ingo Molnarc14471d2006-02-19 00:22:11 -05001006 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1009
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001010 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001011 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001012 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001013 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1016 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001017 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019
1020 if (psmouse->disconnect)
1021 psmouse->disconnect(psmouse);
1022
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001023 if (parent && parent->pt_deactivate)
1024 parent->pt_deactivate(parent);
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 serio_close(serio);
1029 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001030 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001032
1033 if (parent)
1034 psmouse_activate(parent);
1035
Ingo Molnarc14471d2006-02-19 00:22:11 -05001036 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Helge Dellere38de672006-09-10 21:54:39 -04001039static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001040{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001041 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001042
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001043 input_dev->private = psmouse;
1044 input_dev->cdev.dev = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001045
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001046 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1047 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
1048 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001049
1050 psmouse->set_rate = psmouse_set_rate;
1051 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001052 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001053 psmouse->protocol_handler = psmouse_process_byte;
1054 psmouse->pktsize = 3;
1055
1056 if (proto && (proto->detect || proto->init)) {
1057 if (proto->detect && proto->detect(psmouse, 1) < 0)
1058 return -1;
1059
1060 if (proto->init && proto->init(psmouse) < 0)
1061 return -1;
1062
1063 psmouse->type = proto->type;
1064 }
1065 else
1066 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1067
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001068 /*
1069 * If mouse's packet size is 3 there is no point in polling the
1070 * device in hopes to detect protocol reset - we won't get less
1071 * than 3 bytes response anyhow.
1072 */
1073 if (psmouse->pktsize == 3)
1074 psmouse->resync_time = 0;
1075
1076 /*
1077 * Some smart KVMs fake response to POLL command returning just
1078 * 3 bytes and messing up our resync logic, so if initial poll
1079 * fails we won't try polling the device anymore. Hopefully
1080 * such KVM will maintain initially selected protocol.
1081 */
1082 if (psmouse->resync_time && psmouse->poll(psmouse))
1083 psmouse->resync_time = 0;
1084
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001085 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1086 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001087
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001088 input_dev->name = psmouse->devname;
1089 input_dev->phys = psmouse->phys;
1090 input_dev->id.bustype = BUS_I8042;
1091 input_dev->id.vendor = 0x0002;
1092 input_dev->id.product = psmouse->type;
1093 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001094
1095 return 0;
1096}
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098/*
1099 * psmouse_connect() is a callback from the serio module when
1100 * an unhandled serio port is found.
1101 */
1102static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1103{
1104 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001105 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001106 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Ingo Molnarc14471d2006-02-19 00:22:11 -05001108 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /*
1111 * If this is a pass-through port deactivate parent so the device
1112 * connected to this port can be successfully identified
1113 */
1114 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1115 parent = serio_get_drvdata(serio->parent);
1116 psmouse_deactivate(parent);
1117 }
1118
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001119 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1120 input_dev = input_allocate_device();
1121 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001122 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 ps2_init(&psmouse->ps2dev, serio);
David Howellsc4028952006-11-22 14:57:56 +00001125 INIT_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001126 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001127 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1130
1131 serio_set_drvdata(serio, psmouse);
1132
Dmitry Torokhov72155612006-11-05 22:40:19 -05001133 error = serio_open(serio, drv);
1134 if (error)
1135 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001138 error = -ENODEV;
1139 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141
1142 psmouse->rate = psmouse_rate;
1143 psmouse->resolution = psmouse_resolution;
1144 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001145 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001148 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 psmouse_initialize(psmouse);
1152
Dmitry Torokhov72155612006-11-05 22:40:19 -05001153 error = input_register_device(psmouse->dev);
1154 if (error)
1155 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (parent && parent->pt_activate)
1158 parent->pt_activate(parent);
1159
Dmitry Torokhov72155612006-11-05 22:40:19 -05001160 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1161 if (error)
1162 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 psmouse_activate(psmouse);
1165
Dmitry Torokhov72155612006-11-05 22:40:19 -05001166 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001167 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (parent)
1169 psmouse_activate(parent);
1170
Ingo Molnarc14471d2006-02-19 00:22:11 -05001171 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001173
1174 err_pt_deactivate:
1175 if (parent && parent->pt_deactivate)
1176 parent->pt_deactivate(parent);
1177 err_protocol_disconnect:
1178 if (psmouse->disconnect)
1179 psmouse->disconnect(psmouse);
1180 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1181 err_close_serio:
1182 serio_close(serio);
1183 err_clear_drvdata:
1184 serio_set_drvdata(serio, NULL);
1185 err_free:
1186 input_free_device(input_dev);
1187 kfree(psmouse);
1188
1189 retval = error;
1190 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193
1194static int psmouse_reconnect(struct serio *serio)
1195{
1196 struct psmouse *psmouse = serio_get_drvdata(serio);
1197 struct psmouse *parent = NULL;
1198 struct serio_driver *drv = serio->drv;
1199 int rc = -1;
1200
1201 if (!drv || !psmouse) {
1202 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1203 return -1;
1204 }
1205
Ingo Molnarc14471d2006-02-19 00:22:11 -05001206 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1209 parent = serio_get_drvdata(serio->parent);
1210 psmouse_deactivate(parent);
1211 }
1212
1213 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1214
1215 if (psmouse->reconnect) {
1216 if (psmouse->reconnect(psmouse))
1217 goto out;
1218 } else if (psmouse_probe(psmouse) < 0 ||
1219 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1220 goto out;
1221
1222 /* ok, the device type (and capabilities) match the old one,
1223 * we can continue using it, complete intialization
1224 */
1225 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1226
1227 psmouse_initialize(psmouse);
1228
1229 if (parent && parent->pt_activate)
1230 parent->pt_activate(parent);
1231
1232 psmouse_activate(psmouse);
1233 rc = 0;
1234
1235out:
1236 /* If this is a pass-through port the parent waits to be activated */
1237 if (parent)
1238 psmouse_activate(parent);
1239
Ingo Molnarc14471d2006-02-19 00:22:11 -05001240 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return rc;
1242}
1243
1244static struct serio_device_id psmouse_serio_ids[] = {
1245 {
1246 .type = SERIO_8042,
1247 .proto = SERIO_ANY,
1248 .id = SERIO_ANY,
1249 .extra = SERIO_ANY,
1250 },
1251 {
1252 .type = SERIO_PS_PSTHRU,
1253 .proto = SERIO_ANY,
1254 .id = SERIO_ANY,
1255 .extra = SERIO_ANY,
1256 },
1257 { 0 }
1258};
1259
1260MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1261
1262static struct serio_driver psmouse_drv = {
1263 .driver = {
1264 .name = "psmouse",
1265 },
1266 .description = DRIVER_DESC,
1267 .id_table = psmouse_serio_ids,
1268 .interrupt = psmouse_interrupt,
1269 .connect = psmouse_connect,
1270 .reconnect = psmouse_reconnect,
1271 .disconnect = psmouse_disconnect,
1272 .cleanup = psmouse_cleanup,
1273};
1274
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001275ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1276 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001279 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1280 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int retval;
1282
1283 retval = serio_pin_driver(serio);
1284 if (retval)
1285 return retval;
1286
1287 if (serio->drv != &psmouse_drv) {
1288 retval = -ENODEV;
1289 goto out;
1290 }
1291
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001292 psmouse = serio_get_drvdata(serio);
1293
1294 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296out:
1297 serio_unpin_driver(serio);
1298 return retval;
1299}
1300
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001301ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1302 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001305 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1306 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int retval;
1308
1309 retval = serio_pin_driver(serio);
1310 if (retval)
1311 return retval;
1312
1313 if (serio->drv != &psmouse_drv) {
1314 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001315 goto out_unpin;
1316 }
1317
Ingo Molnarc14471d2006-02-19 00:22:11 -05001318 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001319 if (retval)
1320 goto out_unpin;
1321
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001322 psmouse = serio_get_drvdata(serio);
1323
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001324 if (psmouse->state == PSMOUSE_IGNORE) {
1325 retval = -ENODEV;
Ingo Molnarc14471d2006-02-19 00:22:11 -05001326 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
1329 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1330 parent = serio_get_drvdata(serio->parent);
1331 psmouse_deactivate(parent);
1332 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 psmouse_deactivate(psmouse);
1335
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001336 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001338 if (retval != -ENODEV)
1339 psmouse_activate(psmouse);
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (parent)
1342 psmouse_activate(parent);
1343
Ingo Molnarc14471d2006-02-19 00:22:11 -05001344 out_unlock:
1345 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001346 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 serio_unpin_driver(serio);
1348 return retval;
1349}
1350
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001351static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1352{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001353 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001354
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001355 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001356}
1357
1358static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1359{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001360 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001361 unsigned long value;
1362 char *rest;
1363
1364 value = simple_strtoul(buf, &rest, 10);
1365 if (*rest)
1366 return -EINVAL;
1367
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001368 if ((unsigned int)value != value)
1369 return -EINVAL;
1370
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001371 *field = value;
1372
1373 return count;
1374}
1375
1376static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001377{
1378 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1379}
1380
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001381static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001382{
1383 struct serio *serio = psmouse->ps2dev.serio;
1384 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001385 struct input_dev *old_dev, *new_dev;
1386 const struct psmouse_protocol *proto, *old_proto;
1387 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001388 int retry = 0;
1389
Dmitry Torokhov72155612006-11-05 22:40:19 -05001390 proto = psmouse_protocol_by_name(buf, count);
1391 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001392 return -EINVAL;
1393
1394 if (psmouse->type == proto->type)
1395 return count;
1396
Dmitry Torokhov72155612006-11-05 22:40:19 -05001397 new_dev = input_allocate_device();
1398 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001399 return -ENOMEM;
1400
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001401 while (serio->child) {
1402 if (++retry > 3) {
1403 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001404 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001405 return -EIO;
1406 }
1407
Ingo Molnarc14471d2006-02-19 00:22:11 -05001408 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001409 serio_unpin_driver(serio);
1410 serio_unregister_child_port(serio);
1411 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001412 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001413
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001414 if (serio->drv != &psmouse_drv) {
1415 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001416 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001417 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001418
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001419 if (psmouse->type == proto->type) {
1420 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001421 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001422 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001423 }
1424
1425 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1426 parent = serio_get_drvdata(serio->parent);
1427 if (parent->pt_deactivate)
1428 parent->pt_deactivate(parent);
1429 }
1430
Dmitry Torokhov72155612006-11-05 22:40:19 -05001431 old_dev = psmouse->dev;
1432 old_proto = psmouse_protocol_by_type(psmouse->type);
1433
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001434 if (psmouse->disconnect)
1435 psmouse->disconnect(psmouse);
1436
1437 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001438
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001439 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001440 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1441
1442 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1443 psmouse_reset(psmouse);
1444 /* default to PSMOUSE_PS2 */
1445 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1446 }
1447
1448 psmouse_initialize(psmouse);
1449 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1450
Dmitry Torokhov72155612006-11-05 22:40:19 -05001451 error = input_register_device(psmouse->dev);
1452 if (error) {
1453 if (psmouse->disconnect)
1454 psmouse->disconnect(psmouse);
1455
1456 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1457 input_free_device(new_dev);
1458 psmouse->dev = old_dev;
1459 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1460 psmouse_switch_protocol(psmouse, old_proto);
1461 psmouse_initialize(psmouse);
1462 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1463
1464 return error;
1465 }
1466
1467 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001468
1469 if (parent && parent->pt_activate)
1470 parent->pt_activate(parent);
1471
1472 return count;
1473}
1474
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001475static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 unsigned long value;
1478 char *rest;
1479
1480 value = simple_strtoul(buf, &rest, 10);
1481 if (*rest)
1482 return -EINVAL;
1483
1484 psmouse->set_rate(psmouse, value);
1485 return count;
1486}
1487
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001488static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 unsigned long value;
1491 char *rest;
1492
1493 value = simple_strtoul(buf, &rest, 10);
1494 if (*rest)
1495 return -EINVAL;
1496
1497 psmouse->set_resolution(psmouse, value);
1498 return count;
1499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1503{
Helge Dellere38de672006-09-10 21:54:39 -04001504 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 if (!val)
1507 return -EINVAL;
1508
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001509 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001511 if (!proto || !proto->maxproto)
1512 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001514 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Stephen Evanchik541e3162005-08-08 01:26:18 -05001516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1520{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001521 int type = *((unsigned int *)kp->arg);
1522
1523 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526static int __init psmouse_init(void)
1527{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001528 int err;
1529
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001530 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1531 if (!kpsmoused_wq) {
1532 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1533 return -ENOMEM;
1534 }
1535
Akinobu Mita153a9df02006-11-23 23:35:10 -05001536 err = serio_register_driver(&psmouse_drv);
1537 if (err)
1538 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001539
Akinobu Mita153a9df02006-11-23 23:35:10 -05001540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
1543static void __exit psmouse_exit(void)
1544{
1545 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001546 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549module_init(psmouse_init);
1550module_exit(psmouse_exit);