blob: 3c76f6f9c835dc643865850e4eeaaf5070643386 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/interrupt.h>
18#include <linux/input.h>
19#include <linux/serio.h>
20#include <linux/init.h>
21#include <linux/libps2.h>
Ingo Molnarc14471d2006-02-19 00:22:11 -050022#include <linux/mutex.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "psmouse.h"
25#include "synaptics.h"
26#include "logips2pp.h"
27#include "alps.h"
Kenan Esau02d7f582005-05-29 02:30:22 -050028#include "lifebook.h"
Stephen Evanchik541e3162005-08-08 01:26:18 -050029#include "trackpoint.h"
Stefan Lucke24bf10a2007-02-18 01:49:10 -050030#include "touchkit_ps2.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
Dmitry Torokhov04df1922005-06-01 02:39:44 -050096/*
Ingo Molnarc14471d2006-02-19 00:22:11 -050097 * psmouse_mutex protects all operations changing state of mouse
Dmitry Torokhov04df1922005-06-01 02:39:44 -050098 * (connecting, disconnecting, changing rate or resolution via
99 * sysfs). We could use a per-device semaphore but since there
100 * rarely more than one PS/2 mouse connected and since semaphore
101 * is taken in "slow" paths it is not worth it.
102 */
Ingo Molnarc14471d2006-02-19 00:22:11 -0500103static DEFINE_MUTEX(psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -0500104
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500105static struct workqueue_struct *kpsmoused_wq;
106
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500107struct psmouse_protocol {
108 enum psmouse_type type;
Helge Dellere38de672006-09-10 21:54:39 -0400109 const char *name;
110 const char *alias;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500111 int maxproto;
112 int (*detect)(struct psmouse *, int);
113 int (*init)(struct psmouse *);
114};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/*
117 * psmouse_process_byte() analyzes the PS/2 data stream and reports
118 * relevant events to the input module once full packet has arrived.
119 */
120
David Howells7d12e782006-10-05 14:55:46 +0100121static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500123 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unsigned char *packet = psmouse->packet;
125
126 if (psmouse->pktcnt < psmouse->pktsize)
127 return PSMOUSE_GOOD_DATA;
128
129/*
130 * Full packet accumulated, process it
131 */
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
134 * Scroll wheel on IntelliMice, scroll buttons on NetMice
135 */
136
137 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
138 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
139
140/*
141 * Scroll wheel and buttons on IntelliMouse Explorer
142 */
143
144 if (psmouse->type == PSMOUSE_IMEX) {
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400145 switch (packet[3] & 0xC0) {
146 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
147 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
148 break;
149 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
150 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
151 break;
152 case 0x00:
153 case 0xC0:
154 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
155 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
156 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
157 break;
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
161/*
162 * Extra buttons on Genius NewNet 3D
163 */
164
165 if (psmouse->type == PSMOUSE_GENPS) {
166 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
167 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
168 }
169
170/*
171 * Extra button on ThinkingMouse
172 */
173 if (psmouse->type == PSMOUSE_THINKPS) {
174 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
175 /* Without this bit of weirdness moving up gives wildly high Y changes. */
176 packet[1] |= (packet[0] & 0x40) << 1;
177 }
178
179/*
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400180 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
181 * byte.
182 */
183 if (psmouse->type == PSMOUSE_CORTRON) {
184 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
185 packet[0] |= 0x08;
186 }
187
188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * Generic PS/2 Mouse
190 */
191
192 input_report_key(dev, BTN_LEFT, packet[0] & 1);
193 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
194 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
195
196 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
197 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
198
199 input_sync(dev);
200
201 return PSMOUSE_FULL_PACKET;
202}
203
Andres Salomon8bf020e2008-09-16 12:30:33 -0400204void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
205 unsigned long delay)
206{
207 queue_delayed_work(kpsmoused_wq, work, delay);
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500211 * __psmouse_set_state() sets new psmouse state and resets all flags.
212 */
213
214static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
215{
216 psmouse->state = new_state;
217 psmouse->pktcnt = psmouse->out_of_sync = 0;
218 psmouse->ps2dev.flags = 0;
219 psmouse->last = jiffies;
220}
221
222
223/*
224 * psmouse_set_state() sets new psmouse state and resets all flags and
225 * counters while holding serio lock so fighting with interrupt handler
226 * is not a concern.
227 */
228
Andres Salomona48cf5f2008-09-16 12:30:33 -0400229void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500230{
231 serio_pause_rx(psmouse->ps2dev.serio);
232 __psmouse_set_state(psmouse, new_state);
233 serio_continue_rx(psmouse->ps2dev.serio);
234}
235
236/*
237 * psmouse_handle_byte() processes one byte of the input data stream
238 * by calling corresponding protocol handler.
239 */
240
David Howells7d12e782006-10-05 14:55:46 +0100241static int psmouse_handle_byte(struct psmouse *psmouse)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500242{
David Howells7d12e782006-10-05 14:55:46 +0100243 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500244
245 switch (rc) {
246 case PSMOUSE_BAD_DATA:
247 if (psmouse->state == PSMOUSE_ACTIVATED) {
248 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
249 psmouse->name, psmouse->phys, psmouse->pktcnt);
250 if (++psmouse->out_of_sync == psmouse->resetafter) {
251 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
252 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
253 serio_reconnect(psmouse->ps2dev.serio);
254 return -1;
255 }
256 }
257 psmouse->pktcnt = 0;
258 break;
259
260 case PSMOUSE_FULL_PACKET:
261 psmouse->pktcnt = 0;
262 if (psmouse->out_of_sync) {
263 psmouse->out_of_sync = 0;
264 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
265 psmouse->name, psmouse->phys);
266 }
267 break;
268
269 case PSMOUSE_GOOD_DATA:
270 break;
271 }
272 return 0;
273}
274
275/*
276 * psmouse_interrupt() handles incoming characters, either passing them
277 * for normal processing or gathering them as command response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279
280static irqreturn_t psmouse_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100281 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct psmouse *psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (psmouse->state == PSMOUSE_IGNORE)
286 goto out;
287
288 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
289 if (psmouse->state == PSMOUSE_ACTIVATED)
290 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
291 flags & SERIO_TIMEOUT ? " timeout" : "",
292 flags & SERIO_PARITY ? " bad parity" : "");
293 ps2_cmd_aborted(&psmouse->ps2dev);
294 goto out;
295 }
296
297 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
298 if (ps2_handle_ack(&psmouse->ps2dev, data))
299 goto out;
300
301 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
302 if (ps2_handle_response(&psmouse->ps2dev, data))
303 goto out;
304
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500305 if (psmouse->state <= PSMOUSE_RESYNCING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 goto out;
307
308 if (psmouse->state == PSMOUSE_ACTIVATED &&
309 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500310 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 psmouse->name, psmouse->phys, psmouse->pktcnt);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500312 psmouse->badbyte = psmouse->packet[0];
313 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400314 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500315 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 psmouse->packet[psmouse->pktcnt++] = data;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500319/*
320 * Check if this is a new device announcement (0xAA 0x00)
321 */
322 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400323 if (psmouse->pktcnt == 1) {
324 psmouse->last = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto out;
Dmitry Torokhov89c9b482006-04-29 01:12:44 -0400326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500328 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
329 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
330 serio_reconnect(serio);
331 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500333/*
334 * Not a new device, try processing first byte normally
335 */
336 psmouse->pktcnt = 1;
David Howells7d12e782006-10-05 14:55:46 +0100337 if (psmouse_handle_byte(psmouse))
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500338 goto out;
339
340 psmouse->packet[psmouse->pktcnt++] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500343/*
344 * See if we need to force resync because mouse was idle for too long
345 */
346 if (psmouse->state == PSMOUSE_ACTIVATED &&
347 psmouse->pktcnt == 1 && psmouse->resync_time &&
348 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
349 psmouse->badbyte = psmouse->packet[0];
350 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
Andres Salomon8bf020e2008-09-16 12:30:33 -0400351 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500354
355 psmouse->last = jiffies;
David Howells7d12e782006-10-05 14:55:46 +0100356 psmouse_handle_byte(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500357
358 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return IRQ_HANDLED;
360}
361
362
363/*
364 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
365 * using sliced syntax, understood by advanced devices, such as Logitech
366 * or Synaptics touchpads. The command is encoded as:
367 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
368 * is the command.
369 */
370int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
371{
372 int i;
373
374 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
375 return -1;
376
377 for (i = 6; i >= 0; i -= 2) {
378 unsigned char d = (command >> i) & 3;
379 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
380 return -1;
381 }
382
383 return 0;
384}
385
386
387/*
388 * psmouse_reset() resets the mouse into power-on state.
389 */
390int psmouse_reset(struct psmouse *psmouse)
391{
392 unsigned char param[2];
393
394 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
395 return -1;
396
397 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
398 return -1;
399
400 return 0;
401}
402
403
404/*
405 * Genius NetMouse magic init.
406 */
407static int genius_detect(struct psmouse *psmouse, int set_properties)
408{
409 struct ps2dev *ps2dev = &psmouse->ps2dev;
410 unsigned char param[4];
411
412 param[0] = 3;
413 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
414 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
415 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
416 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
417 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
418
419 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
420 return -1;
421
422 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500423 set_bit(BTN_EXTRA, psmouse->dev->keybit);
424 set_bit(BTN_SIDE, psmouse->dev->keybit);
425 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 psmouse->vendor = "Genius";
Dmitry Torokhova3f3f312006-01-29 21:50:46 -0500428 psmouse->name = "Mouse";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 psmouse->pktsize = 4;
430 }
431
432 return 0;
433}
434
435/*
436 * IntelliMouse magic init.
437 */
438static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
439{
440 struct ps2dev *ps2dev = &psmouse->ps2dev;
441 unsigned char param[2];
442
443 param[0] = 200;
444 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
445 param[0] = 100;
446 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
447 param[0] = 80;
448 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
449 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
450
451 if (param[0] != 3)
452 return -1;
453
454 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500455 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
456 set_bit(REL_WHEEL, psmouse->dev->relbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 if (!psmouse->vendor) psmouse->vendor = "Generic";
459 if (!psmouse->name) psmouse->name = "Wheel Mouse";
460 psmouse->pktsize = 4;
461 }
462
463 return 0;
464}
465
466/*
467 * Try IntelliMouse/Explorer magic init.
468 */
469static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
470{
471 struct ps2dev *ps2dev = &psmouse->ps2dev;
472 unsigned char param[2];
473
474 intellimouse_detect(psmouse, 0);
475
476 param[0] = 200;
477 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
478 param[0] = 200;
479 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
480 param[0] = 80;
481 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
482 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
483
484 if (param[0] != 4)
485 return -1;
486
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400487/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
488 param[0] = 200;
489 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
490 param[0] = 80;
491 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
492 param[0] = 40;
493 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500496 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
497 set_bit(REL_WHEEL, psmouse->dev->relbit);
Pozsar Balazsb0c9ad82006-06-26 01:56:08 -0400498 set_bit(REL_HWHEEL, psmouse->dev->relbit);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500499 set_bit(BTN_SIDE, psmouse->dev->keybit);
500 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (!psmouse->vendor) psmouse->vendor = "Generic";
503 if (!psmouse->name) psmouse->name = "Explorer Mouse";
504 psmouse->pktsize = 4;
505 }
506
507 return 0;
508}
509
510/*
511 * Kensington ThinkingMouse / ExpertMouse magic init.
512 */
513static int thinking_detect(struct psmouse *psmouse, int set_properties)
514{
515 struct ps2dev *ps2dev = &psmouse->ps2dev;
516 unsigned char param[2];
Helge Dellere38de672006-09-10 21:54:39 -0400517 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int i;
519
520 param[0] = 10;
521 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
522 param[0] = 0;
523 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
Helge Dellere38de672006-09-10 21:54:39 -0400524 for (i = 0; i < ARRAY_SIZE(seq); i++) {
525 param[0] = seq[i];
526 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
529
530 if (param[0] != 2)
531 return -1;
532
533 if (set_properties) {
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500534 set_bit(BTN_EXTRA, psmouse->dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 psmouse->vendor = "Kensington";
537 psmouse->name = "ThinkingMouse";
538 }
539
540 return 0;
541}
542
543/*
544 * Bare PS/2 protocol "detection". Always succeeds.
545 */
546static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
547{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500548 if (set_properties) {
549 if (!psmouse->vendor) psmouse->vendor = "Generic";
550 if (!psmouse->name) psmouse->name = "Mouse";
551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 return 0;
554}
555
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400556/*
557 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
558 * must be forced by sysfs protocol writing.
559 */
560static int cortron_detect(struct psmouse *psmouse, int set_properties)
561{
562 if (set_properties) {
563 psmouse->vendor = "Cortron";
564 psmouse->name = "PS/2 Trackball";
565 set_bit(BTN_SIDE, psmouse->dev->keybit);
566 }
567
568 return 0;
569}
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/*
572 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
573 * the mouse may have.
574 */
575
576static int psmouse_extensions(struct psmouse *psmouse,
577 unsigned int max_proto, int set_properties)
578{
579 int synaptics_hardware = 0;
580
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500581/*
582 * We always check for lifebook because it does not disturb mouse
583 * (it only checks DMI information).
584 */
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500585 if (lifebook_detect(psmouse, set_properties) == 0) {
Dmitry Torokhova15d60f2005-05-29 02:30:32 -0500586 if (max_proto > PSMOUSE_IMEX) {
587 if (!set_properties || lifebook_init(psmouse) == 0)
588 return PSMOUSE_LIFEBOOK;
589 }
590 }
Kenan Esau02d7f582005-05-29 02:30:22 -0500591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/*
593 * Try Kensington ThinkingMouse (we try first, because synaptics probe
594 * upsets the thinkingmouse).
595 */
596
597 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
598 return PSMOUSE_THINKPS;
599
600/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500601 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
602 * support is disabled in config - we need to know if it is synaptics so we
603 * can reset it properly after probing for intellimouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
605 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
606 synaptics_hardware = 1;
607
608 if (max_proto > PSMOUSE_IMEX) {
609 if (!set_properties || synaptics_init(psmouse) == 0)
610 return PSMOUSE_SYNAPTICS;
611/*
612 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
613 * Unfortunately Logitech/Genius probes confuse some firmware versions so
614 * we'll have to skip them.
615 */
616 max_proto = PSMOUSE_IMEX;
617 }
618/*
619 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
620 */
621 synaptics_reset(psmouse);
622 }
623
624/*
625 * Try ALPS TouchPad
626 */
627 if (max_proto > PSMOUSE_IMEX) {
628 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
629 if (alps_detect(psmouse, set_properties) == 0) {
630 if (!set_properties || alps_init(psmouse) == 0)
631 return PSMOUSE_ALPS;
632/*
633 * Init failed, try basic relative protocols
634 */
635 max_proto = PSMOUSE_IMEX;
636 }
637 }
638
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500639 if (max_proto > PSMOUSE_IMEX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500641 if (genius_detect(psmouse, set_properties) == 0)
642 return PSMOUSE_GENPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500644 if (ps2pp_init(psmouse, set_properties) == 0)
645 return PSMOUSE_PS2PP;
646
647 if (trackpoint_detect(psmouse, set_properties) == 0)
648 return PSMOUSE_TRACKPOINT;
649
650 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
651 return PSMOUSE_TOUCHKIT_PS2;
652 }
Dmitry Torokhovba449952005-12-21 00:51:31 -0500653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/*
655 * Reset to defaults in case the device got confused by extended
Alon Ziv554fc192007-08-30 00:22:48 -0400656 * protocol probes. Note that we follow up with full reset because
657 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
Alon Ziv554fc192007-08-30 00:22:48 -0400659 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
Dmitry Torokhovba449952005-12-21 00:51:31 -0500660 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
663 return PSMOUSE_IMEX;
664
665 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
666 return PSMOUSE_IMPS;
667
668/*
669 * Okay, all failed, we have a standard mouse here. The number of the buttons
670 * is still a question, though. We assume 3.
671 */
672 ps2bare_detect(psmouse, set_properties);
673
674 if (synaptics_hardware) {
675/*
676 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
677 * We need to reset the touchpad because if there is a track point on the
678 * pass through port it could get disabled while probing for protocol
679 * extensions.
680 */
681 psmouse_reset(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
684 return PSMOUSE_PS2;
685}
686
Helge Dellere38de672006-09-10 21:54:39 -0400687static const struct psmouse_protocol psmouse_protocols[] = {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500688 {
689 .type = PSMOUSE_PS2,
690 .name = "PS/2",
691 .alias = "bare",
692 .maxproto = 1,
693 .detect = ps2bare_detect,
694 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500695#ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500696 {
697 .type = PSMOUSE_PS2PP,
698 .name = "PS2++",
699 .alias = "logitech",
700 .detect = ps2pp_init,
701 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500702#endif
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500703 {
704 .type = PSMOUSE_THINKPS,
705 .name = "ThinkPS/2",
706 .alias = "thinkps",
707 .detect = thinking_detect,
708 },
709 {
710 .type = PSMOUSE_GENPS,
711 .name = "GenPS/2",
712 .alias = "genius",
713 .detect = genius_detect,
714 },
715 {
716 .type = PSMOUSE_IMPS,
717 .name = "ImPS/2",
718 .alias = "imps",
719 .maxproto = 1,
720 .detect = intellimouse_detect,
721 },
722 {
723 .type = PSMOUSE_IMEX,
724 .name = "ImExPS/2",
725 .alias = "exps",
726 .maxproto = 1,
727 .detect = im_explorer_detect,
728 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500729#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500730 {
731 .type = PSMOUSE_SYNAPTICS,
732 .name = "SynPS/2",
733 .alias = "synaptics",
734 .detect = synaptics_detect,
735 .init = synaptics_init,
736 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500737#endif
738#ifdef CONFIG_MOUSE_PS2_ALPS
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500739 {
740 .type = PSMOUSE_ALPS,
741 .name = "AlpsPS/2",
742 .alias = "alps",
743 .detect = alps_detect,
744 .init = alps_init,
745 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500746#endif
747#ifdef CONFIG_MOUSE_PS2_LIFEBOOK
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500748 {
749 .type = PSMOUSE_LIFEBOOK,
750 .name = "LBPS/2",
751 .alias = "lifebook",
752 .init = lifebook_init,
753 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500754#endif
755#ifdef CONFIG_MOUSE_PS2_TRACKPOINT
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500756 {
Stephen Evanchik541e3162005-08-08 01:26:18 -0500757 .type = PSMOUSE_TRACKPOINT,
758 .name = "TPPS/2",
759 .alias = "trackpoint",
760 .detect = trackpoint_detect,
761 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500762#endif
763#ifdef CONFIG_MOUSE_PS2_TOUCHKIT
Stephen Evanchik541e3162005-08-08 01:26:18 -0500764 {
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500765 .type = PSMOUSE_TOUCHKIT_PS2,
766 .name = "touchkitPS/2",
767 .alias = "touchkit",
768 .detect = touchkit_ps2_detect,
769 },
Andres Salomon55e3d922007-03-10 01:39:54 -0500770#endif
Stefan Lucke24bf10a2007-02-18 01:49:10 -0500771 {
Aristeu Rozanskiaea6a462007-05-10 01:47:18 -0400772 .type = PSMOUSE_CORTRON,
773 .name = "CortronPS/2",
774 .alias = "cortps",
775 .detect = cortron_detect,
776 },
777 {
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500778 .type = PSMOUSE_AUTO,
779 .name = "auto",
780 .alias = "any",
781 .maxproto = 1,
782 },
783};
784
Helge Dellere38de672006-09-10 21:54:39 -0400785static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500786{
787 int i;
788
789 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
790 if (psmouse_protocols[i].type == type)
791 return &psmouse_protocols[i];
792
793 WARN_ON(1);
794 return &psmouse_protocols[0];
795}
796
Helge Dellere38de672006-09-10 21:54:39 -0400797static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500798{
Helge Dellere38de672006-09-10 21:54:39 -0400799 const struct psmouse_protocol *p;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -0500800 int i;
801
802 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
803 p = &psmouse_protocols[i];
804
805 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
806 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
807 return &psmouse_protocols[i];
808 }
809
810 return NULL;
811}
812
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/*
815 * psmouse_probe() probes for a PS/2 mouse.
816 */
817
818static int psmouse_probe(struct psmouse *psmouse)
819{
820 struct ps2dev *ps2dev = &psmouse->ps2dev;
821 unsigned char param[2];
822
823/*
824 * First, we check if it's a mouse. It should send 0x00 or 0x03
825 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500826 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
827 * ID queries, probably due to a firmware bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 */
829
830 param[0] = 0xa5;
831 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
832 return -1;
833
Vojtech Pavlik7741e932005-05-28 02:11:42 -0500834 if (param[0] != 0x00 && param[0] != 0x03 &&
835 param[0] != 0x04 && param[0] != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return -1;
837
838/*
839 * Then we reset and disable the mouse so that it doesn't generate events.
840 */
841
842 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
843 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
844
845 return 0;
846}
847
848/*
849 * Here we set the mouse resolution.
850 */
851
852void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
853{
Helge Dellere38de672006-09-10 21:54:39 -0400854 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
855 unsigned char p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 if (resolution == 0 || resolution > 200)
858 resolution = 200;
859
Helge Dellere38de672006-09-10 21:54:39 -0400860 p = params[resolution / 50];
861 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
862 psmouse->resolution = 25 << p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865/*
866 * Here we set the mouse report rate.
867 */
868
869static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
870{
Helge Dellere38de672006-09-10 21:54:39 -0400871 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
872 unsigned char r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int i = 0;
874
875 while (rates[i] > rate) i++;
Helge Dellere38de672006-09-10 21:54:39 -0400876 r = rates[i];
877 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
878 psmouse->rate = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881/*
882 * psmouse_initialize() initializes the mouse to a sane state.
883 */
884
885static void psmouse_initialize(struct psmouse *psmouse)
886{
887/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * We set the mouse report rate, resolution and scaling.
889 */
890
891 if (psmouse_max_proto != PSMOUSE_PS2) {
892 psmouse->set_rate(psmouse, psmouse->rate);
893 psmouse->set_resolution(psmouse, psmouse->resolution);
894 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
895 }
896}
897
898/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * psmouse_activate() enables the mouse so that we get motion reports from it.
900 */
901
902static void psmouse_activate(struct psmouse *psmouse)
903{
904 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
905 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
906 psmouse->ps2dev.serio->phys);
907
908 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
909}
910
911
912/*
913 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
Jean Delvarec03983a2007-10-19 23:22:55 +0200914 * reports from it unless we explicitly request it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
916
917static void psmouse_deactivate(struct psmouse *psmouse)
918{
919 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
920 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
921 psmouse->ps2dev.serio->phys);
922
923 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
924}
925
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500926/*
927 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
928 */
929
930static int psmouse_poll(struct psmouse *psmouse)
931{
932 return ps2_command(&psmouse->ps2dev, psmouse->packet,
933 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
934}
935
936
937/*
938 * psmouse_resync() attempts to re-validate current protocol.
939 */
940
David Howellsc4028952006-11-22 14:57:56 +0000941static void psmouse_resync(struct work_struct *work)
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500942{
David Howellsc4028952006-11-22 14:57:56 +0000943 struct psmouse *parent = NULL, *psmouse =
Andres Salomon8bf020e2008-09-16 12:30:33 -0400944 container_of(work, struct psmouse, resync_work.work);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500945 struct serio *serio = psmouse->ps2dev.serio;
946 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
947 int failed = 0, enabled = 0;
948 int i;
949
Ingo Molnarc14471d2006-02-19 00:22:11 -0500950 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500951
952 if (psmouse->state != PSMOUSE_RESYNCING)
953 goto out;
954
955 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
956 parent = serio_get_drvdata(serio->parent);
957 psmouse_deactivate(parent);
958 }
959
960/*
961 * Some mice don't ACK commands sent while they are in the middle of
962 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
963 * instead of ps2_command() which would wait for 200ms for an ACK
964 * that may never come.
965 * As an additional quirk ALPS touchpads may not only forget to ACK
966 * disable command but will stop reporting taps, so if we see that
967 * mouse at least once ACKs disable we will do full reconnect if ACK
968 * is missing.
969 */
970 psmouse->num_resyncs++;
971
972 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
973 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
974 failed = 1;
975 } else
976 psmouse->acks_disable_command = 1;
977
978/*
979 * Poll the mouse. If it was reset the packet will be shorter than
980 * psmouse->pktsize and ps2_command will fail. We do not expect and
981 * do not handle scenario when mouse "upgrades" its protocol while
982 * disconnected since it would require additional delay. If we ever
983 * see a mouse that does it we'll adjust the code.
984 */
985 if (!failed) {
986 if (psmouse->poll(psmouse))
987 failed = 1;
988 else {
989 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
990 for (i = 0; i < psmouse->pktsize; i++) {
991 psmouse->pktcnt++;
David Howells7d12e782006-10-05 14:55:46 +0100992 rc = psmouse->protocol_handler(psmouse);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -0500993 if (rc != PSMOUSE_GOOD_DATA)
994 break;
995 }
996 if (rc != PSMOUSE_FULL_PACKET)
997 failed = 1;
998 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
999 }
1000 }
1001/*
1002 * Now try to enable mouse. We try to do that even if poll failed and also
1003 * repeat our attempts 5 times, otherwise we may be left out with disabled
1004 * mouse.
1005 */
1006 for (i = 0; i < 5; i++) {
1007 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1008 enabled = 1;
1009 break;
1010 }
1011 msleep(200);
1012 }
1013
1014 if (!enabled) {
1015 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1016 psmouse->ps2dev.serio->phys);
1017 failed = 1;
1018 }
1019
1020 if (failed) {
1021 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1022 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1023 serio_reconnect(serio);
1024 } else
1025 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1026
1027 if (parent)
1028 psmouse_activate(parent);
1029 out:
Ingo Molnarc14471d2006-02-19 00:22:11 -05001030 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001031}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033/*
1034 * psmouse_cleanup() resets the mouse into power-on state.
1035 */
1036
1037static void psmouse_cleanup(struct serio *serio)
1038{
1039 struct psmouse *psmouse = serio_get_drvdata(serio);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001040 struct psmouse *parent = NULL;
1041
1042 mutex_lock(&psmouse_mutex);
1043
1044 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1045 parent = serio_get_drvdata(serio->parent);
1046 psmouse_deactivate(parent);
1047 }
1048
1049 psmouse_deactivate(psmouse);
1050
1051 if (psmouse->cleanup)
1052 psmouse->cleanup(psmouse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 psmouse_reset(psmouse);
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001055
1056/*
1057 * Some boxes, such as HP nx7400, get terribly confused if mouse
1058 * is not fully enabled before suspending/shutting down.
1059 */
1060 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1061
1062 if (parent) {
1063 if (parent->pt_deactivate)
1064 parent->pt_deactivate(parent);
1065
1066 psmouse_activate(parent);
1067 }
1068
1069 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072/*
1073 * psmouse_disconnect() closes and frees.
1074 */
1075
1076static void psmouse_disconnect(struct serio *serio)
1077{
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001078 struct psmouse *psmouse, *parent = NULL;
1079
1080 psmouse = serio_get_drvdata(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001082 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Ingo Molnarc14471d2006-02-19 00:22:11 -05001084 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1087
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001088 /* make sure we don't have a resync in progress */
Ingo Molnarc14471d2006-02-19 00:22:11 -05001089 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001090 flush_workqueue(kpsmoused_wq);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001091 mutex_lock(&psmouse_mutex);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1094 parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001095 psmouse_deactivate(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
1098 if (psmouse->disconnect)
1099 psmouse->disconnect(psmouse);
1100
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001101 if (parent && parent->pt_deactivate)
1102 parent->pt_deactivate(parent);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 serio_close(serio);
1107 serio_set_drvdata(serio, NULL);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001108 input_unregister_device(psmouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 kfree(psmouse);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001110
1111 if (parent)
1112 psmouse_activate(parent);
1113
Ingo Molnarc14471d2006-02-19 00:22:11 -05001114 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Helge Dellere38de672006-09-10 21:54:39 -04001117static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001118{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001119 struct input_dev *input_dev = psmouse->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001120
Dmitry Torokhov28aa7f12007-04-12 01:35:09 -04001121 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001122
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001123 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1124 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
1125 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1126 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001127
1128 psmouse->set_rate = psmouse_set_rate;
1129 psmouse->set_resolution = psmouse_set_resolution;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001130 psmouse->poll = psmouse_poll;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001131 psmouse->protocol_handler = psmouse_process_byte;
1132 psmouse->pktsize = 3;
1133
1134 if (proto && (proto->detect || proto->init)) {
1135 if (proto->detect && proto->detect(psmouse, 1) < 0)
1136 return -1;
1137
1138 if (proto->init && proto->init(psmouse) < 0)
1139 return -1;
1140
1141 psmouse->type = proto->type;
1142 }
1143 else
1144 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1145
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001146 /*
1147 * If mouse's packet size is 3 there is no point in polling the
1148 * device in hopes to detect protocol reset - we won't get less
1149 * than 3 bytes response anyhow.
1150 */
1151 if (psmouse->pktsize == 3)
1152 psmouse->resync_time = 0;
1153
1154 /*
1155 * Some smart KVMs fake response to POLL command returning just
1156 * 3 bytes and messing up our resync logic, so if initial poll
1157 * fails we won't try polling the device anymore. Hopefully
1158 * such KVM will maintain initially selected protocol.
1159 */
1160 if (psmouse->resync_time && psmouse->poll(psmouse))
1161 psmouse->resync_time = 0;
1162
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001163 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1164 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001165
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001166 input_dev->name = psmouse->devname;
1167 input_dev->phys = psmouse->phys;
1168 input_dev->id.bustype = BUS_I8042;
1169 input_dev->id.vendor = 0x0002;
1170 input_dev->id.product = psmouse->type;
1171 input_dev->id.version = psmouse->model;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001172
1173 return 0;
1174}
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/*
1177 * psmouse_connect() is a callback from the serio module when
1178 * an unhandled serio port is found.
1179 */
1180static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1181{
1182 struct psmouse *psmouse, *parent = NULL;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001183 struct input_dev *input_dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001184 int retval = 0, error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Ingo Molnarc14471d2006-02-19 00:22:11 -05001186 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /*
1189 * If this is a pass-through port deactivate parent so the device
1190 * connected to this port can be successfully identified
1191 */
1192 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1193 parent = serio_get_drvdata(serio->parent);
1194 psmouse_deactivate(parent);
1195 }
1196
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001197 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1198 input_dev = input_allocate_device();
1199 if (!psmouse || !input_dev)
Dmitry Torokhov72155612006-11-05 22:40:19 -05001200 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 ps2_init(&psmouse->ps2dev, serio);
Andres Salomon8bf020e2008-09-16 12:30:33 -04001203 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001204 psmouse->dev = input_dev;
Dmitry Torokhov08ffce42006-06-26 01:45:10 -04001205 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1208
1209 serio_set_drvdata(serio, psmouse);
1210
Dmitry Torokhov72155612006-11-05 22:40:19 -05001211 error = serio_open(serio, drv);
1212 if (error)
1213 goto err_clear_drvdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if (psmouse_probe(psmouse) < 0) {
Dmitry Torokhov72155612006-11-05 22:40:19 -05001216 error = -ENODEV;
1217 goto err_close_serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
1220 psmouse->rate = psmouse_rate;
1221 psmouse->resolution = psmouse_resolution;
1222 psmouse->resetafter = psmouse_resetafter;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001223 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 psmouse->smartscroll = psmouse_smartscroll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001226 psmouse_switch_protocol(psmouse, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 psmouse_initialize(psmouse);
1230
Dmitry Torokhov72155612006-11-05 22:40:19 -05001231 error = input_register_device(psmouse->dev);
1232 if (error)
1233 goto err_protocol_disconnect;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (parent && parent->pt_activate)
1236 parent->pt_activate(parent);
1237
Dmitry Torokhov72155612006-11-05 22:40:19 -05001238 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1239 if (error)
1240 goto err_pt_deactivate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 psmouse_activate(psmouse);
1243
Dmitry Torokhov72155612006-11-05 22:40:19 -05001244 out:
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001245 /* If this is a pass-through port the parent needs to be re-activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (parent)
1247 psmouse_activate(parent);
1248
Ingo Molnarc14471d2006-02-19 00:22:11 -05001249 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return retval;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001251
1252 err_pt_deactivate:
1253 if (parent && parent->pt_deactivate)
1254 parent->pt_deactivate(parent);
Andres Salomon746b31a2008-01-17 12:01:30 -05001255 input_unregister_device(psmouse->dev);
1256 input_dev = NULL; /* so we don't try to free it below */
Dmitry Torokhov72155612006-11-05 22:40:19 -05001257 err_protocol_disconnect:
1258 if (psmouse->disconnect)
1259 psmouse->disconnect(psmouse);
1260 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1261 err_close_serio:
1262 serio_close(serio);
1263 err_clear_drvdata:
1264 serio_set_drvdata(serio, NULL);
1265 err_free:
1266 input_free_device(input_dev);
1267 kfree(psmouse);
1268
1269 retval = error;
1270 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273
1274static int psmouse_reconnect(struct serio *serio)
1275{
1276 struct psmouse *psmouse = serio_get_drvdata(serio);
1277 struct psmouse *parent = NULL;
1278 struct serio_driver *drv = serio->drv;
1279 int rc = -1;
1280
1281 if (!drv || !psmouse) {
1282 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1283 return -1;
1284 }
1285
Ingo Molnarc14471d2006-02-19 00:22:11 -05001286 mutex_lock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1289 parent = serio_get_drvdata(serio->parent);
1290 psmouse_deactivate(parent);
1291 }
1292
1293 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1294
1295 if (psmouse->reconnect) {
1296 if (psmouse->reconnect(psmouse))
1297 goto out;
1298 } else if (psmouse_probe(psmouse) < 0 ||
1299 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1300 goto out;
1301
1302 /* ok, the device type (and capabilities) match the old one,
1303 * we can continue using it, complete intialization
1304 */
1305 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1306
1307 psmouse_initialize(psmouse);
1308
1309 if (parent && parent->pt_activate)
1310 parent->pt_activate(parent);
1311
1312 psmouse_activate(psmouse);
1313 rc = 0;
1314
1315out:
1316 /* If this is a pass-through port the parent waits to be activated */
1317 if (parent)
1318 psmouse_activate(parent);
1319
Ingo Molnarc14471d2006-02-19 00:22:11 -05001320 mutex_unlock(&psmouse_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return rc;
1322}
1323
1324static struct serio_device_id psmouse_serio_ids[] = {
1325 {
1326 .type = SERIO_8042,
1327 .proto = SERIO_ANY,
1328 .id = SERIO_ANY,
1329 .extra = SERIO_ANY,
1330 },
1331 {
1332 .type = SERIO_PS_PSTHRU,
1333 .proto = SERIO_ANY,
1334 .id = SERIO_ANY,
1335 .extra = SERIO_ANY,
1336 },
1337 { 0 }
1338};
1339
1340MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1341
1342static struct serio_driver psmouse_drv = {
1343 .driver = {
1344 .name = "psmouse",
1345 },
1346 .description = DRIVER_DESC,
1347 .id_table = psmouse_serio_ids,
1348 .interrupt = psmouse_interrupt,
1349 .connect = psmouse_connect,
1350 .reconnect = psmouse_reconnect,
1351 .disconnect = psmouse_disconnect,
1352 .cleanup = psmouse_cleanup,
1353};
1354
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001355ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1356 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001359 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1360 struct psmouse *psmouse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 int retval;
1362
1363 retval = serio_pin_driver(serio);
1364 if (retval)
1365 return retval;
1366
1367 if (serio->drv != &psmouse_drv) {
1368 retval = -ENODEV;
1369 goto out;
1370 }
1371
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001372 psmouse = serio_get_drvdata(serio);
1373
1374 retval = attr->show(psmouse, attr->data, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376out:
1377 serio_unpin_driver(serio);
1378 return retval;
1379}
1380
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001381ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1382 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 struct serio *serio = to_serio_port(dev);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001385 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1386 struct psmouse *psmouse, *parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 int retval;
1388
1389 retval = serio_pin_driver(serio);
1390 if (retval)
1391 return retval;
1392
1393 if (serio->drv != &psmouse_drv) {
1394 retval = -ENODEV;
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001395 goto out_unpin;
1396 }
1397
Ingo Molnarc14471d2006-02-19 00:22:11 -05001398 retval = mutex_lock_interruptible(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001399 if (retval)
1400 goto out_unpin;
1401
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001402 psmouse = serio_get_drvdata(serio);
1403
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001404 if (psmouse->state == PSMOUSE_IGNORE) {
1405 retval = -ENODEV;
Ingo Molnarc14471d2006-02-19 00:22:11 -05001406 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
1409 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1410 parent = serio_get_drvdata(serio->parent);
1411 psmouse_deactivate(parent);
1412 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 psmouse_deactivate(psmouse);
1415
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001416 retval = attr->set(psmouse, attr->data, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001418 if (retval != -ENODEV)
1419 psmouse_activate(psmouse);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (parent)
1422 psmouse_activate(parent);
1423
Ingo Molnarc14471d2006-02-19 00:22:11 -05001424 out_unlock:
1425 mutex_unlock(&psmouse_mutex);
Dmitry Torokhov04df1922005-06-01 02:39:44 -05001426 out_unpin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 serio_unpin_driver(serio);
1428 return retval;
1429}
1430
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001431static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1432{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001433 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001434
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001435 return sprintf(buf, "%u\n", *field);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001436}
1437
1438static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1439{
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001440 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001441 unsigned long value;
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001442
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001443 if (strict_strtoul(buf, 10, &value))
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001444 return -EINVAL;
1445
Sergey Vlasoveb5d5822006-11-09 00:34:27 -05001446 if ((unsigned int)value != value)
1447 return -EINVAL;
1448
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001449 *field = value;
1450
1451 return count;
1452}
1453
1454static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001455{
1456 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1457}
1458
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001459static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001460{
1461 struct serio *serio = psmouse->ps2dev.serio;
1462 struct psmouse *parent = NULL;
Dmitry Torokhov72155612006-11-05 22:40:19 -05001463 struct input_dev *old_dev, *new_dev;
1464 const struct psmouse_protocol *proto, *old_proto;
1465 int error;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001466 int retry = 0;
1467
Dmitry Torokhov72155612006-11-05 22:40:19 -05001468 proto = psmouse_protocol_by_name(buf, count);
1469 if (!proto)
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001470 return -EINVAL;
1471
1472 if (psmouse->type == proto->type)
1473 return count;
1474
Dmitry Torokhov72155612006-11-05 22:40:19 -05001475 new_dev = input_allocate_device();
1476 if (!new_dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001477 return -ENOMEM;
1478
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001479 while (serio->child) {
1480 if (++retry > 3) {
1481 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001482 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001483 return -EIO;
1484 }
1485
Ingo Molnarc14471d2006-02-19 00:22:11 -05001486 mutex_unlock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001487 serio_unpin_driver(serio);
1488 serio_unregister_child_port(serio);
1489 serio_pin_driver_uninterruptible(serio);
Ingo Molnarc14471d2006-02-19 00:22:11 -05001490 mutex_lock(&psmouse_mutex);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001491
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001492 if (serio->drv != &psmouse_drv) {
1493 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001494 return -ENODEV;
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001495 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001496
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001497 if (psmouse->type == proto->type) {
1498 input_free_device(new_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001499 return count; /* switched by other thread */
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001500 }
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001501 }
1502
1503 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1504 parent = serio_get_drvdata(serio->parent);
1505 if (parent->pt_deactivate)
1506 parent->pt_deactivate(parent);
1507 }
1508
Dmitry Torokhov72155612006-11-05 22:40:19 -05001509 old_dev = psmouse->dev;
1510 old_proto = psmouse_protocol_by_type(psmouse->type);
1511
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001512 if (psmouse->disconnect)
1513 psmouse->disconnect(psmouse);
1514
1515 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001516
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001517 psmouse->dev = new_dev;
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001518 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1519
1520 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1521 psmouse_reset(psmouse);
1522 /* default to PSMOUSE_PS2 */
1523 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1524 }
1525
1526 psmouse_initialize(psmouse);
1527 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1528
Dmitry Torokhov72155612006-11-05 22:40:19 -05001529 error = input_register_device(psmouse->dev);
1530 if (error) {
1531 if (psmouse->disconnect)
1532 psmouse->disconnect(psmouse);
1533
1534 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1535 input_free_device(new_dev);
1536 psmouse->dev = old_dev;
1537 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1538 psmouse_switch_protocol(psmouse, old_proto);
1539 psmouse_initialize(psmouse);
1540 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1541
1542 return error;
1543 }
1544
1545 input_unregister_device(old_dev);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001546
1547 if (parent && parent->pt_activate)
1548 parent->pt_activate(parent);
1549
1550 return count;
1551}
1552
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001553static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001557 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return -EINVAL;
1559
1560 psmouse->set_rate(psmouse, value);
1561 return count;
1562}
1563
Dmitry Torokhovcfe9e882005-09-04 01:40:20 -05001564static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Joe Rouvier160f1fe2008-08-10 00:29:25 -04001568 if (strict_strtoul(buf, 10, &value))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return -EINVAL;
1570
1571 psmouse->set_resolution(psmouse, value);
1572 return count;
1573}
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1577{
Helge Dellere38de672006-09-10 21:54:39 -04001578 const struct psmouse_protocol *proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 if (!val)
1581 return -EINVAL;
1582
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001583 proto = psmouse_protocol_by_name(val, strlen(val));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001585 if (!proto || !proto->maxproto)
1586 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001588 *((unsigned int *)kp->arg) = proto->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Stephen Evanchik541e3162005-08-08 01:26:18 -05001590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
1593static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1594{
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -05001595 int type = *((unsigned int *)kp->arg);
1596
1597 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
1600static int __init psmouse_init(void)
1601{
Akinobu Mita153a9df02006-11-23 23:35:10 -05001602 int err;
1603
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001604 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1605 if (!kpsmoused_wq) {
1606 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1607 return -ENOMEM;
1608 }
1609
Akinobu Mita153a9df02006-11-23 23:35:10 -05001610 err = serio_register_driver(&psmouse_drv);
1611 if (err)
1612 destroy_workqueue(kpsmoused_wq);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001613
Akinobu Mita153a9df02006-11-23 23:35:10 -05001614 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
1617static void __exit psmouse_exit(void)
1618{
1619 serio_unregister_driver(&psmouse_drv);
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001620 destroy_workqueue(kpsmoused_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
1623module_init(psmouse_init);
1624module_exit(psmouse_exit);