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