]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/input/mouse/psmouse-base.c
Input: psmouse - reset all types of mice before reconnecting
[karo-tx-linux.git] / drivers / input / mouse / psmouse-base.c
1 /*
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/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>
22 #include <linux/mutex.h>
23
24 #include "psmouse.h"
25 #include "synaptics.h"
26 #include "logips2pp.h"
27 #include "alps.h"
28 #include "hgpk.h"
29 #include "lifebook.h"
30 #include "trackpoint.h"
31 #include "touchkit_ps2.h"
32 #include "elantech.h"
33 #include "sentelic.h"
34
35 #define DRIVER_DESC     "PS/2 mouse driver"
36
37 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
38 MODULE_DESCRIPTION(DRIVER_DESC);
39 MODULE_LICENSE("GPL");
40
41 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
42 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
43 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
44 #define param_check_proto_abbrev(name, p)       __param_check(name, p, unsigned int)
45 #define param_set_proto_abbrev                  psmouse_set_maxproto
46 #define param_get_proto_abbrev                  psmouse_get_maxproto
47 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
48 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
49
50 static unsigned int psmouse_resolution = 200;
51 module_param_named(resolution, psmouse_resolution, uint, 0644);
52 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
53
54 static unsigned int psmouse_rate = 100;
55 module_param_named(rate, psmouse_rate, uint, 0644);
56 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
57
58 static unsigned int psmouse_smartscroll = 1;
59 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
60 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
61
62 static unsigned int psmouse_resetafter = 5;
63 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
64 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
65
66 static unsigned int psmouse_resync_time;
67 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
68 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
69
70 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
71                         NULL,
72                         psmouse_attr_show_protocol, psmouse_attr_set_protocol);
73 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
74                         (void *) offsetof(struct psmouse, rate),
75                         psmouse_show_int_attr, psmouse_attr_set_rate);
76 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
77                         (void *) offsetof(struct psmouse, resolution),
78                         psmouse_show_int_attr, psmouse_attr_set_resolution);
79 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
80                         (void *) offsetof(struct psmouse, resetafter),
81                         psmouse_show_int_attr, psmouse_set_int_attr);
82 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
83                         (void *) offsetof(struct psmouse, resync_time),
84                         psmouse_show_int_attr, psmouse_set_int_attr);
85
86 static struct attribute *psmouse_attributes[] = {
87         &psmouse_attr_protocol.dattr.attr,
88         &psmouse_attr_rate.dattr.attr,
89         &psmouse_attr_resolution.dattr.attr,
90         &psmouse_attr_resetafter.dattr.attr,
91         &psmouse_attr_resync_time.dattr.attr,
92         NULL
93 };
94
95 static struct attribute_group psmouse_attribute_group = {
96         .attrs  = psmouse_attributes,
97 };
98
99 /*
100  * psmouse_mutex protects all operations changing state of mouse
101  * (connecting, disconnecting, changing rate or resolution via
102  * sysfs). We could use a per-device semaphore but since there
103  * rarely more than one PS/2 mouse connected and since semaphore
104  * is taken in "slow" paths it is not worth it.
105  */
106 static DEFINE_MUTEX(psmouse_mutex);
107
108 static struct workqueue_struct *kpsmoused_wq;
109
110 struct psmouse_protocol {
111         enum psmouse_type type;
112         bool maxproto;
113         const char *name;
114         const char *alias;
115         int (*detect)(struct psmouse *, bool);
116         int (*init)(struct psmouse *);
117 };
118
119 /*
120  * psmouse_process_byte() analyzes the PS/2 data stream and reports
121  * relevant events to the input module once full packet has arrived.
122  */
123
124 static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
125 {
126         struct input_dev *dev = psmouse->dev;
127         unsigned char *packet = psmouse->packet;
128
129         if (psmouse->pktcnt < psmouse->pktsize)
130                 return PSMOUSE_GOOD_DATA;
131
132 /*
133  * Full packet accumulated, process it
134  */
135
136 /*
137  * Scroll wheel on IntelliMice, scroll buttons on NetMice
138  */
139
140         if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
141                 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
142
143 /*
144  * Scroll wheel and buttons on IntelliMouse Explorer
145  */
146
147         if (psmouse->type == PSMOUSE_IMEX) {
148                 switch (packet[3] & 0xC0) {
149                         case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
150                                 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
151                                 break;
152                         case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
153                                 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
154                                 break;
155                         case 0x00:
156                         case 0xC0:
157                                 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
158                                 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
159                                 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
160                                 break;
161                 }
162         }
163
164 /*
165  * Extra buttons on Genius NewNet 3D
166  */
167
168         if (psmouse->type == PSMOUSE_GENPS) {
169                 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
170                 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
171         }
172
173 /*
174  * Extra button on ThinkingMouse
175  */
176         if (psmouse->type == PSMOUSE_THINKPS) {
177                 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
178                 /* Without this bit of weirdness moving up gives wildly high Y changes. */
179                 packet[1] |= (packet[0] & 0x40) << 1;
180         }
181
182 /*
183  * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
184  * byte.
185  */
186         if (psmouse->type == PSMOUSE_CORTRON) {
187                 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
188                 packet[0] |= 0x08;
189         }
190
191 /*
192  * Generic PS/2 Mouse
193  */
194
195         input_report_key(dev, BTN_LEFT,    packet[0]       & 1);
196         input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
197         input_report_key(dev, BTN_RIGHT,  (packet[0] >> 1) & 1);
198
199         input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
200         input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
201
202         input_sync(dev);
203
204         return PSMOUSE_FULL_PACKET;
205 }
206
207 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
208                 unsigned long delay)
209 {
210         queue_delayed_work(kpsmoused_wq, work, delay);
211 }
212
213 /*
214  * __psmouse_set_state() sets new psmouse state and resets all flags.
215  */
216
217 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
218 {
219         psmouse->state = new_state;
220         psmouse->pktcnt = psmouse->out_of_sync_cnt = 0;
221         psmouse->ps2dev.flags = 0;
222         psmouse->last = jiffies;
223 }
224
225
226 /*
227  * psmouse_set_state() sets new psmouse state and resets all flags and
228  * counters while holding serio lock so fighting with interrupt handler
229  * is not a concern.
230  */
231
232 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
233 {
234         serio_pause_rx(psmouse->ps2dev.serio);
235         __psmouse_set_state(psmouse, new_state);
236         serio_continue_rx(psmouse->ps2dev.serio);
237 }
238
239 /*
240  * psmouse_handle_byte() processes one byte of the input data stream
241  * by calling corresponding protocol handler.
242  */
243
244 static int psmouse_handle_byte(struct psmouse *psmouse)
245 {
246         psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
247
248         switch (rc) {
249                 case PSMOUSE_BAD_DATA:
250                         if (psmouse->state == PSMOUSE_ACTIVATED) {
251                                 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
252                                         psmouse->name, psmouse->phys, psmouse->pktcnt);
253                                 if (++psmouse->out_of_sync_cnt == psmouse->resetafter) {
254                                         __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
255                                         printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
256                                         serio_reconnect(psmouse->ps2dev.serio);
257                                         return -1;
258                                 }
259                         }
260                         psmouse->pktcnt = 0;
261                         break;
262
263                 case PSMOUSE_FULL_PACKET:
264                         psmouse->pktcnt = 0;
265                         if (psmouse->out_of_sync_cnt) {
266                                 psmouse->out_of_sync_cnt = 0;
267                                 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
268                                         psmouse->name, psmouse->phys);
269                         }
270                         break;
271
272                 case PSMOUSE_GOOD_DATA:
273                         break;
274         }
275         return 0;
276 }
277
278 /*
279  * psmouse_interrupt() handles incoming characters, either passing them
280  * for normal processing or gathering them as command response.
281  */
282
283 static irqreturn_t psmouse_interrupt(struct serio *serio,
284                 unsigned char data, unsigned int flags)
285 {
286         struct psmouse *psmouse = serio_get_drvdata(serio);
287
288         if (psmouse->state == PSMOUSE_IGNORE)
289                 goto out;
290
291         if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
292                 if (psmouse->state == PSMOUSE_ACTIVATED)
293                         printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
294                                 flags & SERIO_TIMEOUT ? " timeout" : "",
295                                 flags & SERIO_PARITY ? " bad parity" : "");
296                 ps2_cmd_aborted(&psmouse->ps2dev);
297                 goto out;
298         }
299
300         if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
301                 if  (ps2_handle_ack(&psmouse->ps2dev, data))
302                         goto out;
303
304         if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
305                 if  (ps2_handle_response(&psmouse->ps2dev, data))
306                         goto out;
307
308         if (psmouse->state <= PSMOUSE_RESYNCING)
309                 goto out;
310
311         if (psmouse->state == PSMOUSE_ACTIVATED &&
312             psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
313                 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
314                        psmouse->name, psmouse->phys, psmouse->pktcnt);
315                 psmouse->badbyte = psmouse->packet[0];
316                 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
317                 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
318                 goto out;
319         }
320
321         psmouse->packet[psmouse->pktcnt++] = data;
322 /*
323  * Check if this is a new device announcement (0xAA 0x00)
324  */
325         if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
326                 if (psmouse->pktcnt == 1) {
327                         psmouse->last = jiffies;
328                         goto out;
329                 }
330
331                 if (psmouse->packet[1] == PSMOUSE_RET_ID ||
332                     (psmouse->type == PSMOUSE_HGPK &&
333                      psmouse->packet[1] == PSMOUSE_RET_BAT)) {
334                         __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
335                         serio_reconnect(serio);
336                         goto out;
337                 }
338 /*
339  * Not a new device, try processing first byte normally
340  */
341                 psmouse->pktcnt = 1;
342                 if (psmouse_handle_byte(psmouse))
343                         goto out;
344
345                 psmouse->packet[psmouse->pktcnt++] = data;
346         }
347
348 /*
349  * See if we need to force resync because mouse was idle for too long
350  */
351         if (psmouse->state == PSMOUSE_ACTIVATED &&
352             psmouse->pktcnt == 1 && psmouse->resync_time &&
353             time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
354                 psmouse->badbyte = psmouse->packet[0];
355                 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
356                 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
357                 goto out;
358         }
359
360         psmouse->last = jiffies;
361         psmouse_handle_byte(psmouse);
362
363  out:
364         return IRQ_HANDLED;
365 }
366
367
368 /*
369  * psmouse_sliced_command() sends an extended PS/2 command to the mouse
370  * using sliced syntax, understood by advanced devices, such as Logitech
371  * or Synaptics touchpads. The command is encoded as:
372  * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
373  * is the command.
374  */
375 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
376 {
377         int i;
378
379         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
380                 return -1;
381
382         for (i = 6; i >= 0; i -= 2) {
383                 unsigned char d = (command >> i) & 3;
384                 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
385                         return -1;
386         }
387
388         return 0;
389 }
390
391
392 /*
393  * psmouse_reset() resets the mouse into power-on state.
394  */
395 int psmouse_reset(struct psmouse *psmouse)
396 {
397         unsigned char param[2];
398
399         if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
400                 return -1;
401
402         if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
403                 return -1;
404
405         return 0;
406 }
407
408
409 /*
410  * Genius NetMouse magic init.
411  */
412 static int genius_detect(struct psmouse *psmouse, bool set_properties)
413 {
414         struct ps2dev *ps2dev = &psmouse->ps2dev;
415         unsigned char param[4];
416
417         param[0] = 3;
418         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
419         ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
420         ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
421         ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11);
422         ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
423
424         if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
425                 return -1;
426
427         if (set_properties) {
428                 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
429                 __set_bit(BTN_SIDE, psmouse->dev->keybit);
430                 __set_bit(REL_WHEEL, psmouse->dev->relbit);
431
432                 psmouse->vendor = "Genius";
433                 psmouse->name = "Mouse";
434                 psmouse->pktsize = 4;
435         }
436
437         return 0;
438 }
439
440 /*
441  * IntelliMouse magic init.
442  */
443 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
444 {
445         struct ps2dev *ps2dev = &psmouse->ps2dev;
446         unsigned char param[2];
447
448         param[0] = 200;
449         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
450         param[0] = 100;
451         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
452         param[0] =  80;
453         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
454         ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
455
456         if (param[0] != 3)
457                 return -1;
458
459         if (set_properties) {
460                 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
461                 __set_bit(REL_WHEEL, psmouse->dev->relbit);
462
463                 if (!psmouse->vendor) psmouse->vendor = "Generic";
464                 if (!psmouse->name) psmouse->name = "Wheel Mouse";
465                 psmouse->pktsize = 4;
466         }
467
468         return 0;
469 }
470
471 /*
472  * Try IntelliMouse/Explorer magic init.
473  */
474 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
475 {
476         struct ps2dev *ps2dev = &psmouse->ps2dev;
477         unsigned char param[2];
478
479         intellimouse_detect(psmouse, 0);
480
481         param[0] = 200;
482         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
483         param[0] = 200;
484         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
485         param[0] =  80;
486         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
487         ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
488
489         if (param[0] != 4)
490                 return -1;
491
492 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
493         param[0] = 200;
494         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
495         param[0] =  80;
496         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
497         param[0] =  40;
498         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
499
500         if (set_properties) {
501                 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
502                 __set_bit(REL_WHEEL, psmouse->dev->relbit);
503                 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
504                 __set_bit(BTN_SIDE, psmouse->dev->keybit);
505                 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
506
507                 if (!psmouse->vendor) psmouse->vendor = "Generic";
508                 if (!psmouse->name) psmouse->name = "Explorer Mouse";
509                 psmouse->pktsize = 4;
510         }
511
512         return 0;
513 }
514
515 /*
516  * Kensington ThinkingMouse / ExpertMouse magic init.
517  */
518 static int thinking_detect(struct psmouse *psmouse, bool set_properties)
519 {
520         struct ps2dev *ps2dev = &psmouse->ps2dev;
521         unsigned char param[2];
522         static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
523         int i;
524
525         param[0] = 10;
526         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
527         param[0] = 0;
528         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
529         for (i = 0; i < ARRAY_SIZE(seq); i++) {
530                 param[0] = seq[i];
531                 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
532         }
533         ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
534
535         if (param[0] != 2)
536                 return -1;
537
538         if (set_properties) {
539                 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
540
541                 psmouse->vendor = "Kensington";
542                 psmouse->name = "ThinkingMouse";
543         }
544
545         return 0;
546 }
547
548 /*
549  * Bare PS/2 protocol "detection". Always succeeds.
550  */
551 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
552 {
553         if (set_properties) {
554                 if (!psmouse->vendor) psmouse->vendor = "Generic";
555                 if (!psmouse->name) psmouse->name = "Mouse";
556         }
557
558         return 0;
559 }
560
561 /*
562  * Cortron PS/2 protocol detection. There's no special way to detect it, so it
563  * must be forced by sysfs protocol writing.
564  */
565 static int cortron_detect(struct psmouse *psmouse, bool set_properties)
566 {
567         if (set_properties) {
568                 psmouse->vendor = "Cortron";
569                 psmouse->name = "PS/2 Trackball";
570                 __set_bit(BTN_SIDE, psmouse->dev->keybit);
571         }
572
573         return 0;
574 }
575
576 /*
577  * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
578  * the mouse may have.
579  */
580
581 static int psmouse_extensions(struct psmouse *psmouse,
582                               unsigned int max_proto, bool set_properties)
583 {
584         bool synaptics_hardware = false;
585
586 /*
587  * We always check for lifebook because it does not disturb mouse
588  * (it only checks DMI information).
589  */
590         if (lifebook_detect(psmouse, set_properties) == 0) {
591                 if (max_proto > PSMOUSE_IMEX) {
592                         if (!set_properties || lifebook_init(psmouse) == 0)
593                                 return PSMOUSE_LIFEBOOK;
594                 }
595         }
596
597 /*
598  * Try Kensington ThinkingMouse (we try first, because synaptics probe
599  * upsets the thinkingmouse).
600  */
601
602         if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
603                 return PSMOUSE_THINKPS;
604
605 /*
606  * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
607  * support is disabled in config - we need to know if it is synaptics so we
608  * can reset it properly after probing for intellimouse.
609  */
610         if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
611                 synaptics_hardware = true;
612
613                 if (max_proto > PSMOUSE_IMEX) {
614                         if (!set_properties || synaptics_init(psmouse) == 0)
615                                 return PSMOUSE_SYNAPTICS;
616 /*
617  * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
618  * Unfortunately Logitech/Genius probes confuse some firmware versions so
619  * we'll have to skip them.
620  */
621                         max_proto = PSMOUSE_IMEX;
622                 }
623 /*
624  * Make sure that touchpad is in relative mode, gestures (taps) are enabled
625  */
626                 synaptics_reset(psmouse);
627         }
628
629 /*
630  * Try ALPS TouchPad
631  */
632         if (max_proto > PSMOUSE_IMEX) {
633                 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
634                 if (alps_detect(psmouse, set_properties) == 0) {
635                         if (!set_properties || alps_init(psmouse) == 0)
636                                 return PSMOUSE_ALPS;
637 /*
638  * Init failed, try basic relative protocols
639  */
640                         max_proto = PSMOUSE_IMEX;
641                 }
642         }
643
644 /*
645  * Try OLPC HGPK touchpad.
646  */
647         if (max_proto > PSMOUSE_IMEX &&
648                         hgpk_detect(psmouse, set_properties) == 0) {
649                 if (!set_properties || hgpk_init(psmouse) == 0)
650                         return PSMOUSE_HGPK;
651 /*
652  * Init failed, try basic relative protocols
653  */
654                 max_proto = PSMOUSE_IMEX;
655         }
656
657 /*
658  * Try Elantech touchpad.
659  */
660         if (max_proto > PSMOUSE_IMEX &&
661                         elantech_detect(psmouse, set_properties) == 0) {
662                 if (!set_properties || elantech_init(psmouse) == 0)
663                         return PSMOUSE_ELANTECH;
664 /*
665  * Init failed, try basic relative protocols
666  */
667                 max_proto = PSMOUSE_IMEX;
668         }
669
670
671         if (max_proto > PSMOUSE_IMEX) {
672                 if (genius_detect(psmouse, set_properties) == 0)
673                         return PSMOUSE_GENPS;
674
675                 if (ps2pp_init(psmouse, set_properties) == 0)
676                         return PSMOUSE_PS2PP;
677
678                 if (trackpoint_detect(psmouse, set_properties) == 0)
679                         return PSMOUSE_TRACKPOINT;
680
681                 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
682                         return PSMOUSE_TOUCHKIT_PS2;
683         }
684
685 /*
686  * Try Finger Sensing Pad. We do it here because its probe upsets
687  * Trackpoint devices (causing TP_READ_ID command to time out).
688  */
689         if (max_proto > PSMOUSE_IMEX) {
690                 if (fsp_detect(psmouse, set_properties) == 0) {
691                         if (!set_properties || fsp_init(psmouse) == 0)
692                                 return PSMOUSE_FSP;
693 /*
694  * Init failed, try basic relative protocols
695  */
696                         max_proto = PSMOUSE_IMEX;
697                 }
698         }
699
700 /*
701  * Reset to defaults in case the device got confused by extended
702  * protocol probes. Note that we follow up with full reset because
703  * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
704  */
705         ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
706         psmouse_reset(psmouse);
707
708         if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
709                 return PSMOUSE_IMEX;
710
711         if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
712                 return PSMOUSE_IMPS;
713
714 /*
715  * Okay, all failed, we have a standard mouse here. The number of the buttons
716  * is still a question, though. We assume 3.
717  */
718         ps2bare_detect(psmouse, set_properties);
719
720         if (synaptics_hardware) {
721 /*
722  * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
723  * We need to reset the touchpad because if there is a track point on the
724  * pass through port it could get disabled while probing for protocol
725  * extensions.
726  */
727                 psmouse_reset(psmouse);
728         }
729
730         return PSMOUSE_PS2;
731 }
732
733 static const struct psmouse_protocol psmouse_protocols[] = {
734         {
735                 .type           = PSMOUSE_PS2,
736                 .name           = "PS/2",
737                 .alias          = "bare",
738                 .maxproto       = true,
739                 .detect         = ps2bare_detect,
740         },
741 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
742         {
743                 .type           = PSMOUSE_PS2PP,
744                 .name           = "PS2++",
745                 .alias          = "logitech",
746                 .detect         = ps2pp_init,
747         },
748 #endif
749         {
750                 .type           = PSMOUSE_THINKPS,
751                 .name           = "ThinkPS/2",
752                 .alias          = "thinkps",
753                 .detect         = thinking_detect,
754         },
755         {
756                 .type           = PSMOUSE_GENPS,
757                 .name           = "GenPS/2",
758                 .alias          = "genius",
759                 .detect         = genius_detect,
760         },
761         {
762                 .type           = PSMOUSE_IMPS,
763                 .name           = "ImPS/2",
764                 .alias          = "imps",
765                 .maxproto       = true,
766                 .detect         = intellimouse_detect,
767         },
768         {
769                 .type           = PSMOUSE_IMEX,
770                 .name           = "ImExPS/2",
771                 .alias          = "exps",
772                 .maxproto       = true,
773                 .detect         = im_explorer_detect,
774         },
775 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
776         {
777                 .type           = PSMOUSE_SYNAPTICS,
778                 .name           = "SynPS/2",
779                 .alias          = "synaptics",
780                 .detect         = synaptics_detect,
781                 .init           = synaptics_init,
782         },
783 #endif
784 #ifdef CONFIG_MOUSE_PS2_ALPS
785         {
786                 .type           = PSMOUSE_ALPS,
787                 .name           = "AlpsPS/2",
788                 .alias          = "alps",
789                 .detect         = alps_detect,
790                 .init           = alps_init,
791         },
792 #endif
793 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
794         {
795                 .type           = PSMOUSE_LIFEBOOK,
796                 .name           = "LBPS/2",
797                 .alias          = "lifebook",
798                 .init           = lifebook_init,
799         },
800 #endif
801 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
802         {
803                 .type           = PSMOUSE_TRACKPOINT,
804                 .name           = "TPPS/2",
805                 .alias          = "trackpoint",
806                 .detect         = trackpoint_detect,
807         },
808 #endif
809 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
810         {
811                 .type           = PSMOUSE_TOUCHKIT_PS2,
812                 .name           = "touchkitPS/2",
813                 .alias          = "touchkit",
814                 .detect         = touchkit_ps2_detect,
815         },
816 #endif
817 #ifdef CONFIG_MOUSE_PS2_OLPC
818         {
819                 .type           = PSMOUSE_HGPK,
820                 .name           = "OLPC HGPK",
821                 .alias          = "hgpk",
822                 .detect         = hgpk_detect,
823         },
824 #endif
825 #ifdef CONFIG_MOUSE_PS2_ELANTECH
826         {
827                 .type           = PSMOUSE_ELANTECH,
828                 .name           = "ETPS/2",
829                 .alias          = "elantech",
830                 .detect         = elantech_detect,
831                 .init           = elantech_init,
832         },
833 #endif
834 #ifdef CONFIG_MOUSE_PS2_SENTELIC
835         {
836                 .type           = PSMOUSE_FSP,
837                 .name           = "FSPPS/2",
838                 .alias          = "fsp",
839                 .detect         = fsp_detect,
840                 .init           = fsp_init,
841         },
842 #endif
843         {
844                 .type           = PSMOUSE_CORTRON,
845                 .name           = "CortronPS/2",
846                 .alias          = "cortps",
847                 .detect         = cortron_detect,
848         },
849         {
850                 .type           = PSMOUSE_AUTO,
851                 .name           = "auto",
852                 .alias          = "any",
853                 .maxproto       = true,
854         },
855 };
856
857 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
858 {
859         int i;
860
861         for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
862                 if (psmouse_protocols[i].type == type)
863                         return &psmouse_protocols[i];
864
865         WARN_ON(1);
866         return &psmouse_protocols[0];
867 }
868
869 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
870 {
871         const struct psmouse_protocol *p;
872         int i;
873
874         for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
875                 p = &psmouse_protocols[i];
876
877                 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
878                     (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
879                         return &psmouse_protocols[i];
880         }
881
882         return NULL;
883 }
884
885
886 /*
887  * psmouse_probe() probes for a PS/2 mouse.
888  */
889
890 static int psmouse_probe(struct psmouse *psmouse)
891 {
892         struct ps2dev *ps2dev = &psmouse->ps2dev;
893         unsigned char param[2];
894
895 /*
896  * First, we check if it's a mouse. It should send 0x00 or 0x03
897  * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
898  * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
899  * ID queries, probably due to a firmware bug.
900  */
901
902         param[0] = 0xa5;
903         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
904                 return -1;
905
906         if (param[0] != 0x00 && param[0] != 0x03 &&
907             param[0] != 0x04 && param[0] != 0xff)
908                 return -1;
909
910 /*
911  * Then we reset and disable the mouse so that it doesn't generate events.
912  */
913
914         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
915                 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
916
917         return 0;
918 }
919
920 /*
921  * Here we set the mouse resolution.
922  */
923
924 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
925 {
926         static const unsigned char params[] = { 0, 1, 2, 2, 3 };
927         unsigned char p;
928
929         if (resolution == 0 || resolution > 200)
930                 resolution = 200;
931
932         p = params[resolution / 50];
933         ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
934         psmouse->resolution = 25 << p;
935 }
936
937 /*
938  * Here we set the mouse report rate.
939  */
940
941 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
942 {
943         static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
944         unsigned char r;
945         int i = 0;
946
947         while (rates[i] > rate) i++;
948         r = rates[i];
949         ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
950         psmouse->rate = r;
951 }
952
953 /*
954  * psmouse_initialize() initializes the mouse to a sane state.
955  */
956
957 static void psmouse_initialize(struct psmouse *psmouse)
958 {
959 /*
960  * We set the mouse report rate, resolution and scaling.
961  */
962
963         if (psmouse_max_proto != PSMOUSE_PS2) {
964                 psmouse->set_rate(psmouse, psmouse->rate);
965                 psmouse->set_resolution(psmouse, psmouse->resolution);
966                 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
967         }
968 }
969
970 /*
971  * psmouse_activate() enables the mouse so that we get motion reports from it.
972  */
973
974 static void psmouse_activate(struct psmouse *psmouse)
975 {
976         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
977                 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
978                         psmouse->ps2dev.serio->phys);
979
980         psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
981 }
982
983
984 /*
985  * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
986  * reports from it unless we explicitly request it.
987  */
988
989 static void psmouse_deactivate(struct psmouse *psmouse)
990 {
991         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
992                 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
993                         psmouse->ps2dev.serio->phys);
994
995         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
996 }
997
998 /*
999  * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
1000  */
1001
1002 static int psmouse_poll(struct psmouse *psmouse)
1003 {
1004         return ps2_command(&psmouse->ps2dev, psmouse->packet,
1005                            PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
1006 }
1007
1008
1009 /*
1010  * psmouse_resync() attempts to re-validate current protocol.
1011  */
1012
1013 static void psmouse_resync(struct work_struct *work)
1014 {
1015         struct psmouse *parent = NULL, *psmouse =
1016                 container_of(work, struct psmouse, resync_work.work);
1017         struct serio *serio = psmouse->ps2dev.serio;
1018         psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1019         bool failed = false, enabled = false;
1020         int i;
1021
1022         mutex_lock(&psmouse_mutex);
1023
1024         if (psmouse->state != PSMOUSE_RESYNCING)
1025                 goto out;
1026
1027         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1028                 parent = serio_get_drvdata(serio->parent);
1029                 psmouse_deactivate(parent);
1030         }
1031
1032 /*
1033  * Some mice don't ACK commands sent while they are in the middle of
1034  * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1035  * instead of ps2_command() which would wait for 200ms for an ACK
1036  * that may never come.
1037  * As an additional quirk ALPS touchpads may not only forget to ACK
1038  * disable command but will stop reporting taps, so if we see that
1039  * mouse at least once ACKs disable we will do full reconnect if ACK
1040  * is missing.
1041  */
1042         psmouse->num_resyncs++;
1043
1044         if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1045                 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1046                         failed = true;
1047         } else
1048                 psmouse->acks_disable_command = true;
1049
1050 /*
1051  * Poll the mouse. If it was reset the packet will be shorter than
1052  * psmouse->pktsize and ps2_command will fail. We do not expect and
1053  * do not handle scenario when mouse "upgrades" its protocol while
1054  * disconnected since it would require additional delay. If we ever
1055  * see a mouse that does it we'll adjust the code.
1056  */
1057         if (!failed) {
1058                 if (psmouse->poll(psmouse))
1059                         failed = true;
1060                 else {
1061                         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1062                         for (i = 0; i < psmouse->pktsize; i++) {
1063                                 psmouse->pktcnt++;
1064                                 rc = psmouse->protocol_handler(psmouse);
1065                                 if (rc != PSMOUSE_GOOD_DATA)
1066                                         break;
1067                         }
1068                         if (rc != PSMOUSE_FULL_PACKET)
1069                                 failed = true;
1070                         psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1071                 }
1072         }
1073 /*
1074  * Now try to enable mouse. We try to do that even if poll failed and also
1075  * repeat our attempts 5 times, otherwise we may be left out with disabled
1076  * mouse.
1077  */
1078         for (i = 0; i < 5; i++) {
1079                 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1080                         enabled = true;
1081                         break;
1082                 }
1083                 msleep(200);
1084         }
1085
1086         if (!enabled) {
1087                 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1088                         psmouse->ps2dev.serio->phys);
1089                 failed = true;
1090         }
1091
1092         if (failed) {
1093                 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1094                 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1095                 serio_reconnect(serio);
1096         } else
1097                 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1098
1099         if (parent)
1100                 psmouse_activate(parent);
1101  out:
1102         mutex_unlock(&psmouse_mutex);
1103 }
1104
1105 /*
1106  * psmouse_cleanup() resets the mouse into power-on state.
1107  */
1108
1109 static void psmouse_cleanup(struct serio *serio)
1110 {
1111         struct psmouse *psmouse = serio_get_drvdata(serio);
1112         struct psmouse *parent = NULL;
1113
1114         mutex_lock(&psmouse_mutex);
1115
1116         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1117                 parent = serio_get_drvdata(serio->parent);
1118                 psmouse_deactivate(parent);
1119         }
1120
1121         psmouse_deactivate(psmouse);
1122
1123         if (psmouse->cleanup)
1124                 psmouse->cleanup(psmouse);
1125
1126         psmouse_reset(psmouse);
1127
1128 /*
1129  * Some boxes, such as HP nx7400, get terribly confused if mouse
1130  * is not fully enabled before suspending/shutting down.
1131  */
1132         ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1133
1134         if (parent) {
1135                 if (parent->pt_deactivate)
1136                         parent->pt_deactivate(parent);
1137
1138                 psmouse_activate(parent);
1139         }
1140
1141         mutex_unlock(&psmouse_mutex);
1142 }
1143
1144 /*
1145  * psmouse_disconnect() closes and frees.
1146  */
1147
1148 static void psmouse_disconnect(struct serio *serio)
1149 {
1150         struct psmouse *psmouse, *parent = NULL;
1151
1152         psmouse = serio_get_drvdata(serio);
1153
1154         sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1155
1156         mutex_lock(&psmouse_mutex);
1157
1158         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1159
1160         /* make sure we don't have a resync in progress */
1161         mutex_unlock(&psmouse_mutex);
1162         flush_workqueue(kpsmoused_wq);
1163         mutex_lock(&psmouse_mutex);
1164
1165         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1166                 parent = serio_get_drvdata(serio->parent);
1167                 psmouse_deactivate(parent);
1168         }
1169
1170         if (psmouse->disconnect)
1171                 psmouse->disconnect(psmouse);
1172
1173         if (parent && parent->pt_deactivate)
1174                 parent->pt_deactivate(parent);
1175
1176         psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1177
1178         serio_close(serio);
1179         serio_set_drvdata(serio, NULL);
1180         input_unregister_device(psmouse->dev);
1181         kfree(psmouse);
1182
1183         if (parent)
1184                 psmouse_activate(parent);
1185
1186         mutex_unlock(&psmouse_mutex);
1187 }
1188
1189 static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
1190 {
1191         struct input_dev *input_dev = psmouse->dev;
1192
1193         input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1194
1195         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1196         input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
1197                 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1198         input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1199
1200         psmouse->set_rate = psmouse_set_rate;
1201         psmouse->set_resolution = psmouse_set_resolution;
1202         psmouse->poll = psmouse_poll;
1203         psmouse->protocol_handler = psmouse_process_byte;
1204         psmouse->pktsize = 3;
1205
1206         if (proto && (proto->detect || proto->init)) {
1207                 if (proto->detect && proto->detect(psmouse, 1) < 0)
1208                         return -1;
1209
1210                 if (proto->init && proto->init(psmouse) < 0)
1211                         return -1;
1212
1213                 psmouse->type = proto->type;
1214         }
1215         else
1216                 psmouse->type = psmouse_extensions(psmouse,
1217                                                    psmouse_max_proto, true);
1218
1219         /*
1220          * If mouse's packet size is 3 there is no point in polling the
1221          * device in hopes to detect protocol reset - we won't get less
1222          * than 3 bytes response anyhow.
1223          */
1224         if (psmouse->pktsize == 3)
1225                 psmouse->resync_time = 0;
1226
1227         /*
1228          * Some smart KVMs fake response to POLL command returning just
1229          * 3 bytes and messing up our resync logic, so if initial poll
1230          * fails we won't try polling the device anymore. Hopefully
1231          * such KVM will maintain initially selected protocol.
1232          */
1233         if (psmouse->resync_time && psmouse->poll(psmouse))
1234                 psmouse->resync_time = 0;
1235
1236         snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1237                  psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1238
1239         input_dev->name = psmouse->devname;
1240         input_dev->phys = psmouse->phys;
1241         input_dev->id.bustype = BUS_I8042;
1242         input_dev->id.vendor = 0x0002;
1243         input_dev->id.product = psmouse->type;
1244         input_dev->id.version = psmouse->model;
1245
1246         return 0;
1247 }
1248
1249 /*
1250  * psmouse_connect() is a callback from the serio module when
1251  * an unhandled serio port is found.
1252  */
1253 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1254 {
1255         struct psmouse *psmouse, *parent = NULL;
1256         struct input_dev *input_dev;
1257         int retval = 0, error = -ENOMEM;
1258
1259         mutex_lock(&psmouse_mutex);
1260
1261         /*
1262          * If this is a pass-through port deactivate parent so the device
1263          * connected to this port can be successfully identified
1264          */
1265         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1266                 parent = serio_get_drvdata(serio->parent);
1267                 psmouse_deactivate(parent);
1268         }
1269
1270         psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1271         input_dev = input_allocate_device();
1272         if (!psmouse || !input_dev)
1273                 goto err_free;
1274
1275         ps2_init(&psmouse->ps2dev, serio);
1276         INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1277         psmouse->dev = input_dev;
1278         snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1279
1280         psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1281
1282         serio_set_drvdata(serio, psmouse);
1283
1284         error = serio_open(serio, drv);
1285         if (error)
1286                 goto err_clear_drvdata;
1287
1288         if (psmouse_probe(psmouse) < 0) {
1289                 error = -ENODEV;
1290                 goto err_close_serio;
1291         }
1292
1293         psmouse->rate = psmouse_rate;
1294         psmouse->resolution = psmouse_resolution;
1295         psmouse->resetafter = psmouse_resetafter;
1296         psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1297         psmouse->smartscroll = psmouse_smartscroll;
1298
1299         psmouse_switch_protocol(psmouse, NULL);
1300
1301         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1302         psmouse_initialize(psmouse);
1303
1304         error = input_register_device(psmouse->dev);
1305         if (error)
1306                 goto err_protocol_disconnect;
1307
1308         if (parent && parent->pt_activate)
1309                 parent->pt_activate(parent);
1310
1311         error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1312         if (error)
1313                 goto err_pt_deactivate;
1314
1315         psmouse_activate(psmouse);
1316
1317  out:
1318         /* If this is a pass-through port the parent needs to be re-activated */
1319         if (parent)
1320                 psmouse_activate(parent);
1321
1322         mutex_unlock(&psmouse_mutex);
1323         return retval;
1324
1325  err_pt_deactivate:
1326         if (parent && parent->pt_deactivate)
1327                 parent->pt_deactivate(parent);
1328         input_unregister_device(psmouse->dev);
1329         input_dev = NULL; /* so we don't try to free it below */
1330  err_protocol_disconnect:
1331         if (psmouse->disconnect)
1332                 psmouse->disconnect(psmouse);
1333         psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1334  err_close_serio:
1335         serio_close(serio);
1336  err_clear_drvdata:
1337         serio_set_drvdata(serio, NULL);
1338  err_free:
1339         input_free_device(input_dev);
1340         kfree(psmouse);
1341
1342         retval = error;
1343         goto out;
1344 }
1345
1346
1347 static int psmouse_reconnect(struct serio *serio)
1348 {
1349         struct psmouse *psmouse = serio_get_drvdata(serio);
1350         struct psmouse *parent = NULL;
1351         struct serio_driver *drv = serio->drv;
1352         unsigned char type;
1353         int rc = -1;
1354
1355         if (!drv || !psmouse) {
1356                 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1357                 return -1;
1358         }
1359
1360         mutex_lock(&psmouse_mutex);
1361
1362         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1363                 parent = serio_get_drvdata(serio->parent);
1364                 psmouse_deactivate(parent);
1365         }
1366
1367         psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1368
1369         if (psmouse->reconnect) {
1370                 if (psmouse->reconnect(psmouse))
1371                         goto out;
1372         } else {
1373                 psmouse_reset(psmouse);
1374
1375                 if (psmouse_probe(psmouse) < 0)
1376                         goto out;
1377
1378                 type = psmouse_extensions(psmouse, psmouse_max_proto, false);
1379                 if (psmouse->type != type)
1380                         goto out;
1381         }
1382
1383         /* ok, the device type (and capabilities) match the old one,
1384          * we can continue using it, complete intialization
1385          */
1386         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1387
1388         psmouse_initialize(psmouse);
1389
1390         if (parent && parent->pt_activate)
1391                 parent->pt_activate(parent);
1392
1393         psmouse_activate(psmouse);
1394         rc = 0;
1395
1396 out:
1397         /* If this is a pass-through port the parent waits to be activated */
1398         if (parent)
1399                 psmouse_activate(parent);
1400
1401         mutex_unlock(&psmouse_mutex);
1402         return rc;
1403 }
1404
1405 static struct serio_device_id psmouse_serio_ids[] = {
1406         {
1407                 .type   = SERIO_8042,
1408                 .proto  = SERIO_ANY,
1409                 .id     = SERIO_ANY,
1410                 .extra  = SERIO_ANY,
1411         },
1412         {
1413                 .type   = SERIO_PS_PSTHRU,
1414                 .proto  = SERIO_ANY,
1415                 .id     = SERIO_ANY,
1416                 .extra  = SERIO_ANY,
1417         },
1418         { 0 }
1419 };
1420
1421 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1422
1423 static struct serio_driver psmouse_drv = {
1424         .driver         = {
1425                 .name   = "psmouse",
1426         },
1427         .description    = DRIVER_DESC,
1428         .id_table       = psmouse_serio_ids,
1429         .interrupt      = psmouse_interrupt,
1430         .connect        = psmouse_connect,
1431         .reconnect      = psmouse_reconnect,
1432         .disconnect     = psmouse_disconnect,
1433         .cleanup        = psmouse_cleanup,
1434 };
1435
1436 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1437                                  char *buf)
1438 {
1439         struct serio *serio = to_serio_port(dev);
1440         struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1441         struct psmouse *psmouse;
1442         int retval;
1443
1444         retval = serio_pin_driver(serio);
1445         if (retval)
1446                 return retval;
1447
1448         if (serio->drv != &psmouse_drv) {
1449                 retval = -ENODEV;
1450                 goto out;
1451         }
1452
1453         psmouse = serio_get_drvdata(serio);
1454
1455         retval = attr->show(psmouse, attr->data, buf);
1456
1457 out:
1458         serio_unpin_driver(serio);
1459         return retval;
1460 }
1461
1462 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1463                                 const char *buf, size_t count)
1464 {
1465         struct serio *serio = to_serio_port(dev);
1466         struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1467         struct psmouse *psmouse, *parent = NULL;
1468         int retval;
1469
1470         retval = serio_pin_driver(serio);
1471         if (retval)
1472                 return retval;
1473
1474         if (serio->drv != &psmouse_drv) {
1475                 retval = -ENODEV;
1476                 goto out_unpin;
1477         }
1478
1479         retval = mutex_lock_interruptible(&psmouse_mutex);
1480         if (retval)
1481                 goto out_unpin;
1482
1483         psmouse = serio_get_drvdata(serio);
1484
1485         if (attr->protect) {
1486                 if (psmouse->state == PSMOUSE_IGNORE) {
1487                         retval = -ENODEV;
1488                         goto out_unlock;
1489                 }
1490
1491                 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1492                         parent = serio_get_drvdata(serio->parent);
1493                         psmouse_deactivate(parent);
1494                 }
1495
1496                 psmouse_deactivate(psmouse);
1497         }
1498
1499         retval = attr->set(psmouse, attr->data, buf, count);
1500
1501         if (attr->protect) {
1502                 if (retval != -ENODEV)
1503                         psmouse_activate(psmouse);
1504
1505                 if (parent)
1506                         psmouse_activate(parent);
1507         }
1508
1509  out_unlock:
1510         mutex_unlock(&psmouse_mutex);
1511  out_unpin:
1512         serio_unpin_driver(serio);
1513         return retval;
1514 }
1515
1516 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1517 {
1518         unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1519
1520         return sprintf(buf, "%u\n", *field);
1521 }
1522
1523 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1524 {
1525         unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1526         unsigned long value;
1527
1528         if (strict_strtoul(buf, 10, &value))
1529                 return -EINVAL;
1530
1531         if ((unsigned int)value != value)
1532                 return -EINVAL;
1533
1534         *field = value;
1535
1536         return count;
1537 }
1538
1539 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1540 {
1541         return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1542 }
1543
1544 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1545 {
1546         struct serio *serio = psmouse->ps2dev.serio;
1547         struct psmouse *parent = NULL;
1548         struct input_dev *old_dev, *new_dev;
1549         const struct psmouse_protocol *proto, *old_proto;
1550         int error;
1551         int retry = 0;
1552
1553         proto = psmouse_protocol_by_name(buf, count);
1554         if (!proto)
1555                 return -EINVAL;
1556
1557         if (psmouse->type == proto->type)
1558                 return count;
1559
1560         new_dev = input_allocate_device();
1561         if (!new_dev)
1562                 return -ENOMEM;
1563
1564         while (serio->child) {
1565                 if (++retry > 3) {
1566                         printk(KERN_WARNING
1567                                 "psmouse: failed to destroy child port, "
1568                                 "protocol change aborted.\n");
1569                         input_free_device(new_dev);
1570                         return -EIO;
1571                 }
1572
1573                 mutex_unlock(&psmouse_mutex);
1574                 serio_unpin_driver(serio);
1575                 serio_unregister_child_port(serio);
1576                 serio_pin_driver_uninterruptible(serio);
1577                 mutex_lock(&psmouse_mutex);
1578
1579                 if (serio->drv != &psmouse_drv) {
1580                         input_free_device(new_dev);
1581                         return -ENODEV;
1582                 }
1583
1584                 if (psmouse->type == proto->type) {
1585                         input_free_device(new_dev);
1586                         return count; /* switched by other thread */
1587                 }
1588         }
1589
1590         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1591                 parent = serio_get_drvdata(serio->parent);
1592                 if (parent->pt_deactivate)
1593                         parent->pt_deactivate(parent);
1594         }
1595
1596         old_dev = psmouse->dev;
1597         old_proto = psmouse_protocol_by_type(psmouse->type);
1598
1599         if (psmouse->disconnect)
1600                 psmouse->disconnect(psmouse);
1601
1602         psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1603
1604         psmouse->dev = new_dev;
1605         psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1606
1607         if (psmouse_switch_protocol(psmouse, proto) < 0) {
1608                 psmouse_reset(psmouse);
1609                 /* default to PSMOUSE_PS2 */
1610                 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1611         }
1612
1613         psmouse_initialize(psmouse);
1614         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1615
1616         error = input_register_device(psmouse->dev);
1617         if (error) {
1618                 if (psmouse->disconnect)
1619                         psmouse->disconnect(psmouse);
1620
1621                 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1622                 input_free_device(new_dev);
1623                 psmouse->dev = old_dev;
1624                 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1625                 psmouse_switch_protocol(psmouse, old_proto);
1626                 psmouse_initialize(psmouse);
1627                 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1628
1629                 return error;
1630         }
1631
1632         input_unregister_device(old_dev);
1633
1634         if (parent && parent->pt_activate)
1635                 parent->pt_activate(parent);
1636
1637         return count;
1638 }
1639
1640 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1641 {
1642         unsigned long value;
1643
1644         if (strict_strtoul(buf, 10, &value))
1645                 return -EINVAL;
1646
1647         psmouse->set_rate(psmouse, value);
1648         return count;
1649 }
1650
1651 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1652 {
1653         unsigned long value;
1654
1655         if (strict_strtoul(buf, 10, &value))
1656                 return -EINVAL;
1657
1658         psmouse->set_resolution(psmouse, value);
1659         return count;
1660 }
1661
1662
1663 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1664 {
1665         const struct psmouse_protocol *proto;
1666
1667         if (!val)
1668                 return -EINVAL;
1669
1670         proto = psmouse_protocol_by_name(val, strlen(val));
1671
1672         if (!proto || !proto->maxproto)
1673                 return -EINVAL;
1674
1675         *((unsigned int *)kp->arg) = proto->type;
1676
1677         return 0;
1678 }
1679
1680 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1681 {
1682         int type = *((unsigned int *)kp->arg);
1683
1684         return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
1685 }
1686
1687 static int __init psmouse_init(void)
1688 {
1689         int err;
1690
1691         kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1692         if (!kpsmoused_wq) {
1693                 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1694                 return -ENOMEM;
1695         }
1696
1697         err = serio_register_driver(&psmouse_drv);
1698         if (err)
1699                 destroy_workqueue(kpsmoused_wq);
1700
1701         return err;
1702 }
1703
1704 static void __exit psmouse_exit(void)
1705 {
1706         serio_unregister_driver(&psmouse_drv);
1707         destroy_workqueue(kpsmoused_wq);
1708 }
1709
1710 module_init(psmouse_init);
1711 module_exit(psmouse_exit);