LCOV - code coverage report
Current view: top level - drivers/input/mouse - psmouse-base.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 8 718 1.1 %
Date: 2023-08-24 13:40:31 Functions: 1 54 1.9 %

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

Generated by: LCOV version 1.14