Version:  2.0.40 2.2.26 2.4.37 3.4 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

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

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