Version:  2.0.40 2.2.26 2.4.37 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us