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