KEYS: call_sbin_request_key() must write lock keyrings before modifying them
[pandora-kernel.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_MIDDLE, psmouse->dev->keybit);
429                 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
430                 __set_bit(BTN_SIDE, psmouse->dev->keybit);
431                 __set_bit(REL_WHEEL, psmouse->dev->relbit);
432
433                 psmouse->vendor = "Genius";
434                 psmouse->name = "Mouse";
435                 psmouse->pktsize = 4;
436         }
437
438         return 0;
439 }
440
441 /*
442  * IntelliMouse magic init.
443  */
444 static int intellimouse_detect(struct psmouse *psmouse, bool set_properties)
445 {
446         struct ps2dev *ps2dev = &psmouse->ps2dev;
447         unsigned char param[2];
448
449         param[0] = 200;
450         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
451         param[0] = 100;
452         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
453         param[0] =  80;
454         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
455         ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
456
457         if (param[0] != 3)
458                 return -1;
459
460         if (set_properties) {
461                 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
462                 __set_bit(REL_WHEEL, psmouse->dev->relbit);
463
464                 if (!psmouse->vendor)
465                         psmouse->vendor = "Generic";
466                 if (!psmouse->name)
467                         psmouse->name = "Wheel Mouse";
468                 psmouse->pktsize = 4;
469         }
470
471         return 0;
472 }
473
474 /*
475  * Try IntelliMouse/Explorer magic init.
476  */
477 static int im_explorer_detect(struct psmouse *psmouse, bool set_properties)
478 {
479         struct ps2dev *ps2dev = &psmouse->ps2dev;
480         unsigned char param[2];
481
482         intellimouse_detect(psmouse, 0);
483
484         param[0] = 200;
485         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
486         param[0] = 200;
487         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
488         param[0] =  80;
489         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
490         ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
491
492         if (param[0] != 4)
493                 return -1;
494
495 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
496         param[0] = 200;
497         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
498         param[0] =  80;
499         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
500         param[0] =  40;
501         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
502
503         if (set_properties) {
504                 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
505                 __set_bit(REL_WHEEL, psmouse->dev->relbit);
506                 __set_bit(REL_HWHEEL, psmouse->dev->relbit);
507                 __set_bit(BTN_SIDE, psmouse->dev->keybit);
508                 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
509
510                 if (!psmouse->vendor)
511                         psmouse->vendor = "Generic";
512                 if (!psmouse->name)
513                         psmouse->name = "Explorer Mouse";
514                 psmouse->pktsize = 4;
515         }
516
517         return 0;
518 }
519
520 /*
521  * Kensington ThinkingMouse / ExpertMouse magic init.
522  */
523 static int thinking_detect(struct psmouse *psmouse, bool set_properties)
524 {
525         struct ps2dev *ps2dev = &psmouse->ps2dev;
526         unsigned char param[2];
527         static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
528         int i;
529
530         param[0] = 10;
531         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
532         param[0] = 0;
533         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
534         for (i = 0; i < ARRAY_SIZE(seq); i++) {
535                 param[0] = seq[i];
536                 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
537         }
538         ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
539
540         if (param[0] != 2)
541                 return -1;
542
543         if (set_properties) {
544                 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
545                 __set_bit(BTN_EXTRA, psmouse->dev->keybit);
546
547                 psmouse->vendor = "Kensington";
548                 psmouse->name = "ThinkingMouse";
549         }
550
551         return 0;
552 }
553
554 /*
555  * Bare PS/2 protocol "detection". Always succeeds.
556  */
557 static int ps2bare_detect(struct psmouse *psmouse, bool set_properties)
558 {
559         if (set_properties) {
560                 if (!psmouse->vendor)
561                         psmouse->vendor = "Generic";
562                 if (!psmouse->name)
563                         psmouse->name = "Mouse";
564
565 /*
566  * We have no way of figuring true number of buttons so let's
567  * assume that the device has 3.
568  */
569                 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
570         }
571
572         return 0;
573 }
574
575 /*
576  * Cortron PS/2 protocol detection. There's no special way to detect it, so it
577  * must be forced by sysfs protocol writing.
578  */
579 static int cortron_detect(struct psmouse *psmouse, bool set_properties)
580 {
581         if (set_properties) {
582                 psmouse->vendor = "Cortron";
583                 psmouse->name = "PS/2 Trackball";
584
585                 __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
586                 __set_bit(BTN_SIDE, psmouse->dev->keybit);
587         }
588
589         return 0;
590 }
591
592 /*
593  * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
594  * the mouse may have.
595  */
596
597 static int psmouse_extensions(struct psmouse *psmouse,
598                               unsigned int max_proto, bool set_properties)
599 {
600         bool synaptics_hardware = false;
601
602 /*
603  * We always check for lifebook because it does not disturb mouse
604  * (it only checks DMI information).
605  */
606         if (lifebook_detect(psmouse, set_properties) == 0) {
607                 if (max_proto > PSMOUSE_IMEX) {
608                         if (!set_properties || lifebook_init(psmouse) == 0)
609                                 return PSMOUSE_LIFEBOOK;
610                 }
611         }
612
613 /*
614  * Try Kensington ThinkingMouse (we try first, because synaptics probe
615  * upsets the thinkingmouse).
616  */
617
618         if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
619                 return PSMOUSE_THINKPS;
620
621 /*
622  * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
623  * support is disabled in config - we need to know if it is synaptics so we
624  * can reset it properly after probing for intellimouse.
625  */
626         if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
627                 synaptics_hardware = true;
628
629                 if (max_proto > PSMOUSE_IMEX) {
630 /*
631  * Try activating protocol, but check if support is enabled first, since
632  * we try detecting Synaptics even when protocol is disabled.
633  */
634                         if (synaptics_supported() &&
635                             (!set_properties || synaptics_init(psmouse) == 0)) {
636                                 return PSMOUSE_SYNAPTICS;
637                         }
638
639 /*
640  * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
641  * Unfortunately Logitech/Genius probes confuse some firmware versions so
642  * we'll have to skip them.
643  */
644                         max_proto = PSMOUSE_IMEX;
645                 }
646 /*
647  * Make sure that touchpad is in relative mode, gestures (taps) are enabled
648  */
649                 synaptics_reset(psmouse);
650         }
651
652 /*
653  * Try ALPS TouchPad
654  */
655         if (max_proto > PSMOUSE_IMEX) {
656                 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
657                 if (alps_detect(psmouse, set_properties) == 0) {
658                         if (!set_properties || alps_init(psmouse) == 0)
659                                 return PSMOUSE_ALPS;
660 /*
661  * Init failed, try basic relative protocols
662  */
663                         max_proto = PSMOUSE_IMEX;
664                 }
665         }
666
667 /*
668  * Try OLPC HGPK touchpad.
669  */
670         if (max_proto > PSMOUSE_IMEX &&
671                         hgpk_detect(psmouse, set_properties) == 0) {
672                 if (!set_properties || hgpk_init(psmouse) == 0)
673                         return PSMOUSE_HGPK;
674 /*
675  * Init failed, try basic relative protocols
676  */
677                 max_proto = PSMOUSE_IMEX;
678         }
679
680 /*
681  * Try Elantech touchpad.
682  */
683         if (max_proto > PSMOUSE_IMEX &&
684                         elantech_detect(psmouse, set_properties) == 0) {
685                 if (!set_properties || elantech_init(psmouse) == 0)
686                         return PSMOUSE_ELANTECH;
687 /*
688  * Init failed, try basic relative protocols
689  */
690                 max_proto = PSMOUSE_IMEX;
691         }
692
693
694         if (max_proto > PSMOUSE_IMEX) {
695                 if (genius_detect(psmouse, set_properties) == 0)
696                         return PSMOUSE_GENPS;
697
698                 if (ps2pp_init(psmouse, set_properties) == 0)
699                         return PSMOUSE_PS2PP;
700
701                 if (trackpoint_detect(psmouse, set_properties) == 0)
702                         return PSMOUSE_TRACKPOINT;
703
704                 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
705                         return PSMOUSE_TOUCHKIT_PS2;
706         }
707
708 /*
709  * Try Finger Sensing Pad. We do it here because its probe upsets
710  * Trackpoint devices (causing TP_READ_ID command to time out).
711  */
712         if (max_proto > PSMOUSE_IMEX) {
713                 if (fsp_detect(psmouse, set_properties) == 0) {
714                         if (!set_properties || fsp_init(psmouse) == 0)
715                                 return PSMOUSE_FSP;
716 /*
717  * Init failed, try basic relative protocols
718  */
719                         max_proto = PSMOUSE_IMEX;
720                 }
721         }
722
723 /*
724  * Reset to defaults in case the device got confused by extended
725  * protocol probes. Note that we follow up with full reset because
726  * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
727  */
728         ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
729         psmouse_reset(psmouse);
730
731         if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
732                 return PSMOUSE_IMEX;
733
734         if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
735                 return PSMOUSE_IMPS;
736
737 /*
738  * Okay, all failed, we have a standard mouse here. The number of the buttons
739  * is still a question, though. We assume 3.
740  */
741         ps2bare_detect(psmouse, set_properties);
742
743         if (synaptics_hardware) {
744 /*
745  * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
746  * We need to reset the touchpad because if there is a track point on the
747  * pass through port it could get disabled while probing for protocol
748  * extensions.
749  */
750                 psmouse_reset(psmouse);
751         }
752
753         return PSMOUSE_PS2;
754 }
755
756 static const struct psmouse_protocol psmouse_protocols[] = {
757         {
758                 .type           = PSMOUSE_PS2,
759                 .name           = "PS/2",
760                 .alias          = "bare",
761                 .maxproto       = true,
762                 .detect         = ps2bare_detect,
763         },
764 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
765         {
766                 .type           = PSMOUSE_PS2PP,
767                 .name           = "PS2++",
768                 .alias          = "logitech",
769                 .detect         = ps2pp_init,
770         },
771 #endif
772         {
773                 .type           = PSMOUSE_THINKPS,
774                 .name           = "ThinkPS/2",
775                 .alias          = "thinkps",
776                 .detect         = thinking_detect,
777         },
778         {
779                 .type           = PSMOUSE_GENPS,
780                 .name           = "GenPS/2",
781                 .alias          = "genius",
782                 .detect         = genius_detect,
783         },
784         {
785                 .type           = PSMOUSE_IMPS,
786                 .name           = "ImPS/2",
787                 .alias          = "imps",
788                 .maxproto       = true,
789                 .detect         = intellimouse_detect,
790         },
791         {
792                 .type           = PSMOUSE_IMEX,
793                 .name           = "ImExPS/2",
794                 .alias          = "exps",
795                 .maxproto       = true,
796                 .detect         = im_explorer_detect,
797         },
798 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
799         {
800                 .type           = PSMOUSE_SYNAPTICS,
801                 .name           = "SynPS/2",
802                 .alias          = "synaptics",
803                 .detect         = synaptics_detect,
804                 .init           = synaptics_init,
805         },
806 #endif
807 #ifdef CONFIG_MOUSE_PS2_ALPS
808         {
809                 .type           = PSMOUSE_ALPS,
810                 .name           = "AlpsPS/2",
811                 .alias          = "alps",
812                 .detect         = alps_detect,
813                 .init           = alps_init,
814         },
815 #endif
816 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
817         {
818                 .type           = PSMOUSE_LIFEBOOK,
819                 .name           = "LBPS/2",
820                 .alias          = "lifebook",
821                 .init           = lifebook_init,
822         },
823 #endif
824 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
825         {
826                 .type           = PSMOUSE_TRACKPOINT,
827                 .name           = "TPPS/2",
828                 .alias          = "trackpoint",
829                 .detect         = trackpoint_detect,
830         },
831 #endif
832 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
833         {
834                 .type           = PSMOUSE_TOUCHKIT_PS2,
835                 .name           = "touchkitPS/2",
836                 .alias          = "touchkit",
837                 .detect         = touchkit_ps2_detect,
838         },
839 #endif
840 #ifdef CONFIG_MOUSE_PS2_OLPC
841         {
842                 .type           = PSMOUSE_HGPK,
843                 .name           = "OLPC HGPK",
844                 .alias          = "hgpk",
845                 .detect         = hgpk_detect,
846         },
847 #endif
848 #ifdef CONFIG_MOUSE_PS2_ELANTECH
849         {
850                 .type           = PSMOUSE_ELANTECH,
851                 .name           = "ETPS/2",
852                 .alias          = "elantech",
853                 .detect         = elantech_detect,
854                 .init           = elantech_init,
855         },
856 #endif
857 #ifdef CONFIG_MOUSE_PS2_SENTELIC
858         {
859                 .type           = PSMOUSE_FSP,
860                 .name           = "FSPPS/2",
861                 .alias          = "fsp",
862                 .detect         = fsp_detect,
863                 .init           = fsp_init,
864         },
865 #endif
866         {
867                 .type           = PSMOUSE_CORTRON,
868                 .name           = "CortronPS/2",
869                 .alias          = "cortps",
870                 .detect         = cortron_detect,
871         },
872         {
873                 .type           = PSMOUSE_AUTO,
874                 .name           = "auto",
875                 .alias          = "any",
876                 .maxproto       = true,
877         },
878 };
879
880 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
881 {
882         int i;
883
884         for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
885                 if (psmouse_protocols[i].type == type)
886                         return &psmouse_protocols[i];
887
888         WARN_ON(1);
889         return &psmouse_protocols[0];
890 }
891
892 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
893 {
894         const struct psmouse_protocol *p;
895         int i;
896
897         for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
898                 p = &psmouse_protocols[i];
899
900                 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
901                     (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
902                         return &psmouse_protocols[i];
903         }
904
905         return NULL;
906 }
907
908
909 /*
910  * psmouse_probe() probes for a PS/2 mouse.
911  */
912
913 static int psmouse_probe(struct psmouse *psmouse)
914 {
915         struct ps2dev *ps2dev = &psmouse->ps2dev;
916         unsigned char param[2];
917
918 /*
919  * First, we check if it's a mouse. It should send 0x00 or 0x03
920  * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
921  * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
922  * ID queries, probably due to a firmware bug.
923  */
924
925         param[0] = 0xa5;
926         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
927                 return -1;
928
929         if (param[0] != 0x00 && param[0] != 0x03 &&
930             param[0] != 0x04 && param[0] != 0xff)
931                 return -1;
932
933 /*
934  * Then we reset and disable the mouse so that it doesn't generate events.
935  */
936
937         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
938                 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
939
940         return 0;
941 }
942
943 /*
944  * Here we set the mouse resolution.
945  */
946
947 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
948 {
949         static const unsigned char params[] = { 0, 1, 2, 2, 3 };
950         unsigned char p;
951
952         if (resolution == 0 || resolution > 200)
953                 resolution = 200;
954
955         p = params[resolution / 50];
956         ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
957         psmouse->resolution = 25 << p;
958 }
959
960 /*
961  * Here we set the mouse report rate.
962  */
963
964 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
965 {
966         static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
967         unsigned char r;
968         int i = 0;
969
970         while (rates[i] > rate) i++;
971         r = rates[i];
972         ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
973         psmouse->rate = r;
974 }
975
976 /*
977  * psmouse_initialize() initializes the mouse to a sane state.
978  */
979
980 static void psmouse_initialize(struct psmouse *psmouse)
981 {
982 /*
983  * We set the mouse report rate, resolution and scaling.
984  */
985
986         if (psmouse_max_proto != PSMOUSE_PS2) {
987                 psmouse->set_rate(psmouse, psmouse->rate);
988                 psmouse->set_resolution(psmouse, psmouse->resolution);
989                 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
990         }
991 }
992
993 /*
994  * psmouse_activate() enables the mouse so that we get motion reports from it.
995  */
996
997 static void psmouse_activate(struct psmouse *psmouse)
998 {
999         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1000                 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
1001                         psmouse->ps2dev.serio->phys);
1002
1003         psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1004 }
1005
1006
1007 /*
1008  * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
1009  * reports from it unless we explicitly request it.
1010  */
1011
1012 static void psmouse_deactivate(struct psmouse *psmouse)
1013 {
1014         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1015                 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
1016                         psmouse->ps2dev.serio->phys);
1017
1018         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1019 }
1020
1021 /*
1022  * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
1023  */
1024
1025 static int psmouse_poll(struct psmouse *psmouse)
1026 {
1027         return ps2_command(&psmouse->ps2dev, psmouse->packet,
1028                            PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
1029 }
1030
1031
1032 /*
1033  * psmouse_resync() attempts to re-validate current protocol.
1034  */
1035
1036 static void psmouse_resync(struct work_struct *work)
1037 {
1038         struct psmouse *parent = NULL, *psmouse =
1039                 container_of(work, struct psmouse, resync_work.work);
1040         struct serio *serio = psmouse->ps2dev.serio;
1041         psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
1042         bool failed = false, enabled = false;
1043         int i;
1044
1045         mutex_lock(&psmouse_mutex);
1046
1047         if (psmouse->state != PSMOUSE_RESYNCING)
1048                 goto out;
1049
1050         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1051                 parent = serio_get_drvdata(serio->parent);
1052                 psmouse_deactivate(parent);
1053         }
1054
1055 /*
1056  * Some mice don't ACK commands sent while they are in the middle of
1057  * transmitting motion packet. To avoid delay we use ps2_sendbyte()
1058  * instead of ps2_command() which would wait for 200ms for an ACK
1059  * that may never come.
1060  * As an additional quirk ALPS touchpads may not only forget to ACK
1061  * disable command but will stop reporting taps, so if we see that
1062  * mouse at least once ACKs disable we will do full reconnect if ACK
1063  * is missing.
1064  */
1065         psmouse->num_resyncs++;
1066
1067         if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
1068                 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
1069                         failed = true;
1070         } else
1071                 psmouse->acks_disable_command = true;
1072
1073 /*
1074  * Poll the mouse. If it was reset the packet will be shorter than
1075  * psmouse->pktsize and ps2_command will fail. We do not expect and
1076  * do not handle scenario when mouse "upgrades" its protocol while
1077  * disconnected since it would require additional delay. If we ever
1078  * see a mouse that does it we'll adjust the code.
1079  */
1080         if (!failed) {
1081                 if (psmouse->poll(psmouse))
1082                         failed = true;
1083                 else {
1084                         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1085                         for (i = 0; i < psmouse->pktsize; i++) {
1086                                 psmouse->pktcnt++;
1087                                 rc = psmouse->protocol_handler(psmouse);
1088                                 if (rc != PSMOUSE_GOOD_DATA)
1089                                         break;
1090                         }
1091                         if (rc != PSMOUSE_FULL_PACKET)
1092                                 failed = true;
1093                         psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1094                 }
1095         }
1096 /*
1097  * Now try to enable mouse. We try to do that even if poll failed and also
1098  * repeat our attempts 5 times, otherwise we may be left out with disabled
1099  * mouse.
1100  */
1101         for (i = 0; i < 5; i++) {
1102                 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1103                         enabled = true;
1104                         break;
1105                 }
1106                 msleep(200);
1107         }
1108
1109         if (!enabled) {
1110                 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1111                         psmouse->ps2dev.serio->phys);
1112                 failed = true;
1113         }
1114
1115         if (failed) {
1116                 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1117                 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1118                 serio_reconnect(serio);
1119         } else
1120                 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1121
1122         if (parent)
1123                 psmouse_activate(parent);
1124  out:
1125         mutex_unlock(&psmouse_mutex);
1126 }
1127
1128 /*
1129  * psmouse_cleanup() resets the mouse into power-on state.
1130  */
1131
1132 static void psmouse_cleanup(struct serio *serio)
1133 {
1134         struct psmouse *psmouse = serio_get_drvdata(serio);
1135         struct psmouse *parent = NULL;
1136
1137         mutex_lock(&psmouse_mutex);
1138
1139         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1140                 parent = serio_get_drvdata(serio->parent);
1141                 psmouse_deactivate(parent);
1142         }
1143
1144         psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1145
1146         /*
1147          * Disable stream mode so cleanup routine can proceed undisturbed.
1148          */
1149         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1150                 printk(KERN_WARNING "psmouse.c: Failed to disable mouse on %s\n",
1151                         psmouse->ps2dev.serio->phys);
1152
1153         if (psmouse->cleanup)
1154                 psmouse->cleanup(psmouse);
1155
1156 /*
1157  * Reset the mouse to defaults (bare PS/2 protocol).
1158  */
1159         ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
1160
1161 /*
1162  * Some boxes, such as HP nx7400, get terribly confused if mouse
1163  * is not fully enabled before suspending/shutting down.
1164  */
1165         ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1166
1167         if (parent) {
1168                 if (parent->pt_deactivate)
1169                         parent->pt_deactivate(parent);
1170
1171                 psmouse_activate(parent);
1172         }
1173
1174         mutex_unlock(&psmouse_mutex);
1175 }
1176
1177 /*
1178  * psmouse_disconnect() closes and frees.
1179  */
1180
1181 static void psmouse_disconnect(struct serio *serio)
1182 {
1183         struct psmouse *psmouse, *parent = NULL;
1184
1185         psmouse = serio_get_drvdata(serio);
1186
1187         sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1188
1189         mutex_lock(&psmouse_mutex);
1190
1191         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1192
1193         /* make sure we don't have a resync in progress */
1194         mutex_unlock(&psmouse_mutex);
1195         flush_workqueue(kpsmoused_wq);
1196         mutex_lock(&psmouse_mutex);
1197
1198         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1199                 parent = serio_get_drvdata(serio->parent);
1200                 psmouse_deactivate(parent);
1201         }
1202
1203         if (psmouse->disconnect)
1204                 psmouse->disconnect(psmouse);
1205
1206         if (parent && parent->pt_deactivate)
1207                 parent->pt_deactivate(parent);
1208
1209         psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1210
1211         serio_close(serio);
1212         serio_set_drvdata(serio, NULL);
1213         input_unregister_device(psmouse->dev);
1214         kfree(psmouse);
1215
1216         if (parent)
1217                 psmouse_activate(parent);
1218
1219         mutex_unlock(&psmouse_mutex);
1220 }
1221
1222 static int psmouse_switch_protocol(struct psmouse *psmouse,
1223                                    const struct psmouse_protocol *proto)
1224 {
1225         struct input_dev *input_dev = psmouse->dev;
1226
1227         input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1228
1229         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1230         input_dev->keybit[BIT_WORD(BTN_MOUSE)] =
1231                                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1232         input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1233
1234         psmouse->set_rate = psmouse_set_rate;
1235         psmouse->set_resolution = psmouse_set_resolution;
1236         psmouse->poll = psmouse_poll;
1237         psmouse->protocol_handler = psmouse_process_byte;
1238         psmouse->pktsize = 3;
1239
1240         if (proto && (proto->detect || proto->init)) {
1241                 if (proto->detect && proto->detect(psmouse, 1) < 0)
1242                         return -1;
1243
1244                 if (proto->init && proto->init(psmouse) < 0)
1245                         return -1;
1246
1247                 psmouse->type = proto->type;
1248         } else
1249                 psmouse->type = psmouse_extensions(psmouse,
1250                                                    psmouse_max_proto, true);
1251
1252         /*
1253          * If mouse's packet size is 3 there is no point in polling the
1254          * device in hopes to detect protocol reset - we won't get less
1255          * than 3 bytes response anyhow.
1256          */
1257         if (psmouse->pktsize == 3)
1258                 psmouse->resync_time = 0;
1259
1260         /*
1261          * Some smart KVMs fake response to POLL command returning just
1262          * 3 bytes and messing up our resync logic, so if initial poll
1263          * fails we won't try polling the device anymore. Hopefully
1264          * such KVM will maintain initially selected protocol.
1265          */
1266         if (psmouse->resync_time && psmouse->poll(psmouse))
1267                 psmouse->resync_time = 0;
1268
1269         snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1270                  psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1271
1272         input_dev->name = psmouse->devname;
1273         input_dev->phys = psmouse->phys;
1274         input_dev->id.bustype = BUS_I8042;
1275         input_dev->id.vendor = 0x0002;
1276         input_dev->id.product = psmouse->type;
1277         input_dev->id.version = psmouse->model;
1278
1279         return 0;
1280 }
1281
1282 /*
1283  * psmouse_connect() is a callback from the serio module when
1284  * an unhandled serio port is found.
1285  */
1286 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1287 {
1288         struct psmouse *psmouse, *parent = NULL;
1289         struct input_dev *input_dev;
1290         int retval = 0, error = -ENOMEM;
1291
1292         mutex_lock(&psmouse_mutex);
1293
1294         /*
1295          * If this is a pass-through port deactivate parent so the device
1296          * connected to this port can be successfully identified
1297          */
1298         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1299                 parent = serio_get_drvdata(serio->parent);
1300                 psmouse_deactivate(parent);
1301         }
1302
1303         psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1304         input_dev = input_allocate_device();
1305         if (!psmouse || !input_dev)
1306                 goto err_free;
1307
1308         ps2_init(&psmouse->ps2dev, serio);
1309         INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1310         psmouse->dev = input_dev;
1311         snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1312
1313         psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1314
1315         serio_set_drvdata(serio, psmouse);
1316
1317         error = serio_open(serio, drv);
1318         if (error)
1319                 goto err_clear_drvdata;
1320
1321         if (psmouse_probe(psmouse) < 0) {
1322                 error = -ENODEV;
1323                 goto err_close_serio;
1324         }
1325
1326         psmouse->rate = psmouse_rate;
1327         psmouse->resolution = psmouse_resolution;
1328         psmouse->resetafter = psmouse_resetafter;
1329         psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1330         psmouse->smartscroll = psmouse_smartscroll;
1331
1332         psmouse_switch_protocol(psmouse, NULL);
1333
1334         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1335         psmouse_initialize(psmouse);
1336
1337         error = input_register_device(psmouse->dev);
1338         if (error)
1339                 goto err_protocol_disconnect;
1340
1341         if (parent && parent->pt_activate)
1342                 parent->pt_activate(parent);
1343
1344         error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1345         if (error)
1346                 goto err_pt_deactivate;
1347
1348         psmouse_activate(psmouse);
1349
1350  out:
1351         /* If this is a pass-through port the parent needs to be re-activated */
1352         if (parent)
1353                 psmouse_activate(parent);
1354
1355         mutex_unlock(&psmouse_mutex);
1356         return retval;
1357
1358  err_pt_deactivate:
1359         if (parent && parent->pt_deactivate)
1360                 parent->pt_deactivate(parent);
1361         input_unregister_device(psmouse->dev);
1362         input_dev = NULL; /* so we don't try to free it below */
1363  err_protocol_disconnect:
1364         if (psmouse->disconnect)
1365                 psmouse->disconnect(psmouse);
1366         psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1367  err_close_serio:
1368         serio_close(serio);
1369  err_clear_drvdata:
1370         serio_set_drvdata(serio, NULL);
1371  err_free:
1372         input_free_device(input_dev);
1373         kfree(psmouse);
1374
1375         retval = error;
1376         goto out;
1377 }
1378
1379
1380 static int psmouse_reconnect(struct serio *serio)
1381 {
1382         struct psmouse *psmouse = serio_get_drvdata(serio);
1383         struct psmouse *parent = NULL;
1384         struct serio_driver *drv = serio->drv;
1385         int rc = -1;
1386
1387         if (!drv || !psmouse) {
1388                 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1389                 return -1;
1390         }
1391
1392         mutex_lock(&psmouse_mutex);
1393
1394         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1395                 parent = serio_get_drvdata(serio->parent);
1396                 psmouse_deactivate(parent);
1397         }
1398
1399         psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1400
1401         if (psmouse->reconnect) {
1402                 if (psmouse->reconnect(psmouse))
1403                         goto out;
1404         } else if (psmouse_probe(psmouse) < 0 ||
1405                    psmouse->type != psmouse_extensions(psmouse,
1406                                                 psmouse_max_proto, false)) {
1407                 goto out;
1408         }
1409
1410         /* ok, the device type (and capabilities) match the old one,
1411          * we can continue using it, complete intialization
1412          */
1413         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1414
1415         psmouse_initialize(psmouse);
1416
1417         if (parent && parent->pt_activate)
1418                 parent->pt_activate(parent);
1419
1420         psmouse_activate(psmouse);
1421         rc = 0;
1422
1423 out:
1424         /* If this is a pass-through port the parent waits to be activated */
1425         if (parent)
1426                 psmouse_activate(parent);
1427
1428         mutex_unlock(&psmouse_mutex);
1429         return rc;
1430 }
1431
1432 static struct serio_device_id psmouse_serio_ids[] = {
1433         {
1434                 .type   = SERIO_8042,
1435                 .proto  = SERIO_ANY,
1436                 .id     = SERIO_ANY,
1437                 .extra  = SERIO_ANY,
1438         },
1439         {
1440                 .type   = SERIO_PS_PSTHRU,
1441                 .proto  = SERIO_ANY,
1442                 .id     = SERIO_ANY,
1443                 .extra  = SERIO_ANY,
1444         },
1445         { 0 }
1446 };
1447
1448 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1449
1450 static struct serio_driver psmouse_drv = {
1451         .driver         = {
1452                 .name   = "psmouse",
1453         },
1454         .description    = DRIVER_DESC,
1455         .id_table       = psmouse_serio_ids,
1456         .interrupt      = psmouse_interrupt,
1457         .connect        = psmouse_connect,
1458         .reconnect      = psmouse_reconnect,
1459         .disconnect     = psmouse_disconnect,
1460         .cleanup        = psmouse_cleanup,
1461 };
1462
1463 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1464                                  char *buf)
1465 {
1466         struct serio *serio = to_serio_port(dev);
1467         struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1468         struct psmouse *psmouse;
1469
1470         psmouse = serio_get_drvdata(serio);
1471
1472         return attr->show(psmouse, attr->data, buf);
1473 }
1474
1475 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1476                                 const char *buf, size_t count)
1477 {
1478         struct serio *serio = to_serio_port(dev);
1479         struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1480         struct psmouse *psmouse, *parent = NULL;
1481         int retval;
1482
1483         retval = mutex_lock_interruptible(&psmouse_mutex);
1484         if (retval)
1485                 goto out;
1486
1487         psmouse = serio_get_drvdata(serio);
1488
1489         if (attr->protect) {
1490                 if (psmouse->state == PSMOUSE_IGNORE) {
1491                         retval = -ENODEV;
1492                         goto out_unlock;
1493                 }
1494
1495                 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1496                         parent = serio_get_drvdata(serio->parent);
1497                         psmouse_deactivate(parent);
1498                 }
1499
1500                 psmouse_deactivate(psmouse);
1501         }
1502
1503         retval = attr->set(psmouse, attr->data, buf, count);
1504
1505         if (attr->protect) {
1506                 if (retval != -ENODEV)
1507                         psmouse_activate(psmouse);
1508
1509                 if (parent)
1510                         psmouse_activate(parent);
1511         }
1512
1513  out_unlock:
1514         mutex_unlock(&psmouse_mutex);
1515  out:
1516         return retval;
1517 }
1518
1519 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1520 {
1521         unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1522
1523         return sprintf(buf, "%u\n", *field);
1524 }
1525
1526 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1527 {
1528         unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1529         unsigned long value;
1530
1531         if (strict_strtoul(buf, 10, &value))
1532                 return -EINVAL;
1533
1534         if ((unsigned int)value != value)
1535                 return -EINVAL;
1536
1537         *field = value;
1538
1539         return count;
1540 }
1541
1542 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1543 {
1544         return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1545 }
1546
1547 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1548 {
1549         struct serio *serio = psmouse->ps2dev.serio;
1550         struct psmouse *parent = NULL;
1551         struct input_dev *old_dev, *new_dev;
1552         const struct psmouse_protocol *proto, *old_proto;
1553         int error;
1554         int retry = 0;
1555
1556         proto = psmouse_protocol_by_name(buf, count);
1557         if (!proto)
1558                 return -EINVAL;
1559
1560         if (psmouse->type == proto->type)
1561                 return count;
1562
1563         new_dev = input_allocate_device();
1564         if (!new_dev)
1565                 return -ENOMEM;
1566
1567         while (serio->child) {
1568                 if (++retry > 3) {
1569                         printk(KERN_WARNING
1570                                 "psmouse: failed to destroy child port, "
1571                                 "protocol change aborted.\n");
1572                         input_free_device(new_dev);
1573                         return -EIO;
1574                 }
1575
1576                 mutex_unlock(&psmouse_mutex);
1577                 serio_unregister_child_port(serio);
1578                 mutex_lock(&psmouse_mutex);
1579
1580                 if (serio->drv != &psmouse_drv) {
1581                         input_free_device(new_dev);
1582                         return -ENODEV;
1583                 }
1584
1585                 if (psmouse->type == proto->type) {
1586                         input_free_device(new_dev);
1587                         return count; /* switched by other thread */
1588                 }
1589         }
1590
1591         if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1592                 parent = serio_get_drvdata(serio->parent);
1593                 if (parent->pt_deactivate)
1594                         parent->pt_deactivate(parent);
1595         }
1596
1597         old_dev = psmouse->dev;
1598         old_proto = psmouse_protocol_by_type(psmouse->type);
1599
1600         if (psmouse->disconnect)
1601                 psmouse->disconnect(psmouse);
1602
1603         psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1604
1605         psmouse->dev = new_dev;
1606         psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1607
1608         if (psmouse_switch_protocol(psmouse, proto) < 0) {
1609                 psmouse_reset(psmouse);
1610                 /* default to PSMOUSE_PS2 */
1611                 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1612         }
1613
1614         psmouse_initialize(psmouse);
1615         psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1616
1617         error = input_register_device(psmouse->dev);
1618         if (error) {
1619                 if (psmouse->disconnect)
1620                         psmouse->disconnect(psmouse);
1621
1622                 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1623                 input_free_device(new_dev);
1624                 psmouse->dev = old_dev;
1625                 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1626                 psmouse_switch_protocol(psmouse, old_proto);
1627                 psmouse_initialize(psmouse);
1628                 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1629
1630                 return error;
1631         }
1632
1633         input_unregister_device(old_dev);
1634
1635         if (parent && parent->pt_activate)
1636                 parent->pt_activate(parent);
1637
1638         return count;
1639 }
1640
1641 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1642 {
1643         unsigned long value;
1644
1645         if (strict_strtoul(buf, 10, &value))
1646                 return -EINVAL;
1647
1648         psmouse->set_rate(psmouse, value);
1649         return count;
1650 }
1651
1652 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1653 {
1654         unsigned long value;
1655
1656         if (strict_strtoul(buf, 10, &value))
1657                 return -EINVAL;
1658
1659         psmouse->set_resolution(psmouse, value);
1660         return count;
1661 }
1662
1663
1664 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1665 {
1666         const struct psmouse_protocol *proto;
1667
1668         if (!val)
1669                 return -EINVAL;
1670
1671         proto = psmouse_protocol_by_name(val, strlen(val));
1672
1673         if (!proto || !proto->maxproto)
1674                 return -EINVAL;
1675
1676         *((unsigned int *)kp->arg) = proto->type;
1677
1678         return 0;
1679 }
1680
1681 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1682 {
1683         int type = *((unsigned int *)kp->arg);
1684
1685         return sprintf(buffer, "%s", psmouse_protocol_by_type(type)->name);
1686 }
1687
1688 static int __init psmouse_init(void)
1689 {
1690         int err;
1691
1692         lifebook_module_init();
1693         synaptics_module_init();
1694
1695         kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1696         if (!kpsmoused_wq) {
1697                 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1698                 return -ENOMEM;
1699         }
1700
1701         err = serio_register_driver(&psmouse_drv);
1702         if (err)
1703                 destroy_workqueue(kpsmoused_wq);
1704
1705         return err;
1706 }
1707
1708 static void __exit psmouse_exit(void)
1709 {
1710         serio_unregister_driver(&psmouse_drv);
1711         destroy_workqueue(kpsmoused_wq);
1712 }
1713
1714 module_init(psmouse_init);
1715 module_exit(psmouse_exit);