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

Generated by: LCOV version 1.14