Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * PS/2 driver library
0004  *
0005  * Copyright (c) 1999-2002 Vojtech Pavlik
0006  * Copyright (c) 2004 Dmitry Torokhov
0007  */
0008 
0009 
0010 #include <linux/delay.h>
0011 #include <linux/module.h>
0012 #include <linux/sched.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/input.h>
0015 #include <linux/serio.h>
0016 #include <linux/i8042.h>
0017 #include <linux/libps2.h>
0018 
0019 #define DRIVER_DESC "PS/2 driver library"
0020 
0021 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
0022 MODULE_DESCRIPTION("PS/2 driver library");
0023 MODULE_LICENSE("GPL");
0024 
0025 static int ps2_do_sendbyte(struct ps2dev *ps2dev, u8 byte,
0026                unsigned int timeout, unsigned int max_attempts)
0027     __releases(&ps2dev->serio->lock) __acquires(&ps2dev->serio->lock)
0028 {
0029     int attempt = 0;
0030     int error;
0031 
0032     lockdep_assert_held(&ps2dev->serio->lock);
0033 
0034     do {
0035         ps2dev->nak = 1;
0036         ps2dev->flags |= PS2_FLAG_ACK;
0037 
0038         serio_continue_rx(ps2dev->serio);
0039 
0040         error = serio_write(ps2dev->serio, byte);
0041         if (error)
0042             dev_dbg(&ps2dev->serio->dev,
0043                 "failed to write %#02x: %d\n", byte, error);
0044         else
0045             wait_event_timeout(ps2dev->wait,
0046                        !(ps2dev->flags & PS2_FLAG_ACK),
0047                        msecs_to_jiffies(timeout));
0048 
0049         serio_pause_rx(ps2dev->serio);
0050     } while (ps2dev->nak == PS2_RET_NAK && ++attempt < max_attempts);
0051 
0052     ps2dev->flags &= ~PS2_FLAG_ACK;
0053 
0054     if (!error) {
0055         switch (ps2dev->nak) {
0056         case 0:
0057             break;
0058         case PS2_RET_NAK:
0059             error = -EAGAIN;
0060             break;
0061         case PS2_RET_ERR:
0062             error = -EPROTO;
0063             break;
0064         default:
0065             error = -EIO;
0066             break;
0067         }
0068     }
0069 
0070     if (error || attempt > 1)
0071         dev_dbg(&ps2dev->serio->dev,
0072             "%02x - %d (%x), attempt %d\n",
0073             byte, error, ps2dev->nak, attempt);
0074 
0075     return error;
0076 }
0077 
0078 /*
0079  * ps2_sendbyte() sends a byte to the device and waits for acknowledge.
0080  * It doesn't handle retransmission, the caller is expected to handle
0081  * it when needed.
0082  *
0083  * ps2_sendbyte() can only be called from a process context.
0084  */
0085 
0086 int ps2_sendbyte(struct ps2dev *ps2dev, u8 byte, unsigned int timeout)
0087 {
0088     int retval;
0089 
0090     serio_pause_rx(ps2dev->serio);
0091 
0092     retval = ps2_do_sendbyte(ps2dev, byte, timeout, 1);
0093     dev_dbg(&ps2dev->serio->dev, "%02x - %x\n", byte, ps2dev->nak);
0094 
0095     serio_continue_rx(ps2dev->serio);
0096 
0097     return retval;
0098 }
0099 EXPORT_SYMBOL(ps2_sendbyte);
0100 
0101 void ps2_begin_command(struct ps2dev *ps2dev)
0102 {
0103     struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex;
0104 
0105     mutex_lock(m);
0106 }
0107 EXPORT_SYMBOL(ps2_begin_command);
0108 
0109 void ps2_end_command(struct ps2dev *ps2dev)
0110 {
0111     struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex;
0112 
0113     mutex_unlock(m);
0114 }
0115 EXPORT_SYMBOL(ps2_end_command);
0116 
0117 /*
0118  * ps2_drain() waits for device to transmit requested number of bytes
0119  * and discards them.
0120  */
0121 
0122 void ps2_drain(struct ps2dev *ps2dev, size_t maxbytes, unsigned int timeout)
0123 {
0124     if (maxbytes > sizeof(ps2dev->cmdbuf)) {
0125         WARN_ON(1);
0126         maxbytes = sizeof(ps2dev->cmdbuf);
0127     }
0128 
0129     ps2_begin_command(ps2dev);
0130 
0131     serio_pause_rx(ps2dev->serio);
0132     ps2dev->flags = PS2_FLAG_CMD;
0133     ps2dev->cmdcnt = maxbytes;
0134     serio_continue_rx(ps2dev->serio);
0135 
0136     wait_event_timeout(ps2dev->wait,
0137                !(ps2dev->flags & PS2_FLAG_CMD),
0138                msecs_to_jiffies(timeout));
0139 
0140     ps2_end_command(ps2dev);
0141 }
0142 EXPORT_SYMBOL(ps2_drain);
0143 
0144 /*
0145  * ps2_is_keyboard_id() checks received ID byte against the list of
0146  * known keyboard IDs.
0147  */
0148 
0149 bool ps2_is_keyboard_id(u8 id_byte)
0150 {
0151     static const u8 keyboard_ids[] = {
0152         0xab,   /* Regular keyboards        */
0153         0xac,   /* NCD Sun keyboard     */
0154         0x2b,   /* Trust keyboard, translated   */
0155         0x5d,   /* Trust keyboard       */
0156         0x60,   /* NMB SGI keyboard, translated */
0157         0x47,   /* NMB SGI keyboard     */
0158     };
0159 
0160     return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
0161 }
0162 EXPORT_SYMBOL(ps2_is_keyboard_id);
0163 
0164 /*
0165  * ps2_adjust_timeout() is called after receiving 1st byte of command
0166  * response and tries to reduce remaining timeout to speed up command
0167  * completion.
0168  */
0169 
0170 static int ps2_adjust_timeout(struct ps2dev *ps2dev,
0171                   unsigned int command, unsigned int timeout)
0172 {
0173     switch (command) {
0174     case PS2_CMD_RESET_BAT:
0175         /*
0176          * Device has sent the first response byte after
0177          * reset command, reset is thus done, so we can
0178          * shorten the timeout.
0179          * The next byte will come soon (keyboard) or not
0180          * at all (mouse).
0181          */
0182         if (timeout > msecs_to_jiffies(100))
0183             timeout = msecs_to_jiffies(100);
0184         break;
0185 
0186     case PS2_CMD_GETID:
0187         /*
0188          * Microsoft Natural Elite keyboard responds to
0189          * the GET ID command as it were a mouse, with
0190          * a single byte. Fail the command so atkbd will
0191          * use alternative probe to detect it.
0192          */
0193         if (ps2dev->cmdbuf[1] == 0xaa) {
0194             serio_pause_rx(ps2dev->serio);
0195             ps2dev->flags = 0;
0196             serio_continue_rx(ps2dev->serio);
0197             timeout = 0;
0198         }
0199 
0200         /*
0201          * If device behind the port is not a keyboard there
0202          * won't be 2nd byte of ID response.
0203          */
0204         if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) {
0205             serio_pause_rx(ps2dev->serio);
0206             ps2dev->flags = ps2dev->cmdcnt = 0;
0207             serio_continue_rx(ps2dev->serio);
0208             timeout = 0;
0209         }
0210         break;
0211 
0212     default:
0213         break;
0214     }
0215 
0216     return timeout;
0217 }
0218 
0219 /*
0220  * ps2_command() sends a command and its parameters to the mouse,
0221  * then waits for the response and puts it in the param array.
0222  *
0223  * ps2_command() can only be called from a process context
0224  */
0225 
0226 int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command)
0227 {
0228     unsigned int timeout;
0229     unsigned int send = (command >> 12) & 0xf;
0230     unsigned int receive = (command >> 8) & 0xf;
0231     int rc;
0232     int i;
0233     u8 send_param[16];
0234 
0235     if (receive > sizeof(ps2dev->cmdbuf)) {
0236         WARN_ON(1);
0237         return -EINVAL;
0238     }
0239 
0240     if (send && !param) {
0241         WARN_ON(1);
0242         return -EINVAL;
0243     }
0244 
0245     memcpy(send_param, param, send);
0246 
0247     serio_pause_rx(ps2dev->serio);
0248 
0249     ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
0250     ps2dev->cmdcnt = receive;
0251     if (receive && param)
0252         for (i = 0; i < receive; i++)
0253             ps2dev->cmdbuf[(receive - 1) - i] = param[i];
0254 
0255     /* Signal that we are sending the command byte */
0256     ps2dev->flags |= PS2_FLAG_ACK_CMD;
0257 
0258     /*
0259      * Some devices (Synaptics) peform the reset before
0260      * ACKing the reset command, and so it can take a long
0261      * time before the ACK arrives.
0262      */
0263     timeout = command == PS2_CMD_RESET_BAT ? 1000 : 200;
0264 
0265     rc = ps2_do_sendbyte(ps2dev, command & 0xff, timeout, 2);
0266     if (rc)
0267         goto out_reset_flags;
0268 
0269     /* Now we are sending command parameters, if any */
0270     ps2dev->flags &= ~PS2_FLAG_ACK_CMD;
0271 
0272     for (i = 0; i < send; i++) {
0273         rc = ps2_do_sendbyte(ps2dev, param[i], 200, 2);
0274         if (rc)
0275             goto out_reset_flags;
0276     }
0277 
0278     serio_continue_rx(ps2dev->serio);
0279 
0280     /*
0281      * The reset command takes a long time to execute.
0282      */
0283     timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
0284 
0285     timeout = wait_event_timeout(ps2dev->wait,
0286                      !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
0287 
0288     if (ps2dev->cmdcnt && !(ps2dev->flags & PS2_FLAG_CMD1)) {
0289 
0290         timeout = ps2_adjust_timeout(ps2dev, command, timeout);
0291         wait_event_timeout(ps2dev->wait,
0292                    !(ps2dev->flags & PS2_FLAG_CMD), timeout);
0293     }
0294 
0295     serio_pause_rx(ps2dev->serio);
0296 
0297     if (param)
0298         for (i = 0; i < receive; i++)
0299             param[i] = ps2dev->cmdbuf[(receive - 1) - i];
0300 
0301     if (ps2dev->cmdcnt &&
0302         (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1)) {
0303         rc = -EPROTO;
0304         goto out_reset_flags;
0305     }
0306 
0307     rc = 0;
0308 
0309  out_reset_flags:
0310     ps2dev->flags = 0;
0311     serio_continue_rx(ps2dev->serio);
0312 
0313     dev_dbg(&ps2dev->serio->dev,
0314         "%02x [%*ph] - %x/%08lx [%*ph]\n",
0315         command & 0xff, send, send_param,
0316         ps2dev->nak, ps2dev->flags,
0317         receive, param ?: send_param);
0318 
0319     /*
0320      * ps_command() handles resends itself, so do not leak -EAGAIN
0321      * to the callers.
0322      */
0323     return rc != -EAGAIN ? rc : -EPROTO;
0324 }
0325 EXPORT_SYMBOL(__ps2_command);
0326 
0327 int ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command)
0328 {
0329     int rc;
0330 
0331     ps2_begin_command(ps2dev);
0332     rc = __ps2_command(ps2dev, param, command);
0333     ps2_end_command(ps2dev);
0334 
0335     return rc;
0336 }
0337 EXPORT_SYMBOL(ps2_command);
0338 
0339 /*
0340  * ps2_sliced_command() sends an extended PS/2 command to the mouse
0341  * using sliced syntax, understood by advanced devices, such as Logitech
0342  * or Synaptics touchpads. The command is encoded as:
0343  * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
0344  * is the command.
0345  */
0346 
0347 int ps2_sliced_command(struct ps2dev *ps2dev, u8 command)
0348 {
0349     int i;
0350     int retval;
0351 
0352     ps2_begin_command(ps2dev);
0353 
0354     retval = __ps2_command(ps2dev, NULL, PS2_CMD_SETSCALE11);
0355     if (retval)
0356         goto out;
0357 
0358     for (i = 6; i >= 0; i -= 2) {
0359         u8 d = (command >> i) & 3;
0360         retval = __ps2_command(ps2dev, &d, PS2_CMD_SETRES);
0361         if (retval)
0362             break;
0363     }
0364 
0365 out:
0366     dev_dbg(&ps2dev->serio->dev, "%02x - %d\n", command, retval);
0367     ps2_end_command(ps2dev);
0368     return retval;
0369 }
0370 EXPORT_SYMBOL(ps2_sliced_command);
0371 
0372 /*
0373  * ps2_init() initializes ps2dev structure
0374  */
0375 
0376 void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
0377 {
0378     mutex_init(&ps2dev->cmd_mutex);
0379     lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth);
0380     init_waitqueue_head(&ps2dev->wait);
0381     ps2dev->serio = serio;
0382 }
0383 EXPORT_SYMBOL(ps2_init);
0384 
0385 /*
0386  * ps2_handle_ack() is supposed to be used in interrupt handler
0387  * to properly process ACK/NAK of a command from a PS/2 device.
0388  */
0389 
0390 bool ps2_handle_ack(struct ps2dev *ps2dev, u8 data)
0391 {
0392     switch (data) {
0393     case PS2_RET_ACK:
0394         ps2dev->nak = 0;
0395         break;
0396 
0397     case PS2_RET_NAK:
0398         ps2dev->flags |= PS2_FLAG_NAK;
0399         ps2dev->nak = PS2_RET_NAK;
0400         break;
0401 
0402     case PS2_RET_ERR:
0403         if (ps2dev->flags & PS2_FLAG_NAK) {
0404             ps2dev->flags &= ~PS2_FLAG_NAK;
0405             ps2dev->nak = PS2_RET_ERR;
0406             break;
0407         }
0408         fallthrough;
0409 
0410     /*
0411      * Workaround for mice which don't ACK the Get ID command.
0412      * These are valid mouse IDs that we recognize.
0413      */
0414     case 0x00:
0415     case 0x03:
0416     case 0x04:
0417         if (ps2dev->flags & PS2_FLAG_WAITID) {
0418             ps2dev->nak = 0;
0419             break;
0420         }
0421         fallthrough;
0422     default:
0423         /*
0424          * Do not signal errors if we get unexpected reply while
0425          * waiting for an ACK to the initial (first) command byte:
0426          * the device might not be quiesced yet and continue
0427          * delivering data.
0428          * Note that we reset PS2_FLAG_WAITID flag, so the workaround
0429          * for mice not acknowledging the Get ID command only triggers
0430          * on the 1st byte; if device spews data we really want to see
0431          * a real ACK from it.
0432          */
0433         dev_dbg(&ps2dev->serio->dev, "unexpected %#02x\n", data);
0434         ps2dev->flags &= ~PS2_FLAG_WAITID;
0435         return ps2dev->flags & PS2_FLAG_ACK_CMD;
0436     }
0437 
0438     if (!ps2dev->nak) {
0439         ps2dev->flags &= ~PS2_FLAG_NAK;
0440         if (ps2dev->cmdcnt)
0441             ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
0442     }
0443 
0444     ps2dev->flags &= ~PS2_FLAG_ACK;
0445     wake_up(&ps2dev->wait);
0446 
0447     if (data != PS2_RET_ACK)
0448         ps2_handle_response(ps2dev, data);
0449 
0450     return true;
0451 }
0452 EXPORT_SYMBOL(ps2_handle_ack);
0453 
0454 /*
0455  * ps2_handle_response() is supposed to be used in interrupt handler
0456  * to properly store device's response to a command and notify process
0457  * waiting for completion of the command.
0458  */
0459 
0460 bool ps2_handle_response(struct ps2dev *ps2dev, u8 data)
0461 {
0462     if (ps2dev->cmdcnt)
0463         ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
0464 
0465     if (ps2dev->flags & PS2_FLAG_CMD1) {
0466         ps2dev->flags &= ~PS2_FLAG_CMD1;
0467         if (ps2dev->cmdcnt)
0468             wake_up(&ps2dev->wait);
0469     }
0470 
0471     if (!ps2dev->cmdcnt) {
0472         ps2dev->flags &= ~PS2_FLAG_CMD;
0473         wake_up(&ps2dev->wait);
0474     }
0475 
0476     return true;
0477 }
0478 EXPORT_SYMBOL(ps2_handle_response);
0479 
0480 void ps2_cmd_aborted(struct ps2dev *ps2dev)
0481 {
0482     if (ps2dev->flags & PS2_FLAG_ACK)
0483         ps2dev->nak = 1;
0484 
0485     if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
0486         wake_up(&ps2dev->wait);
0487 
0488     /* reset all flags except last nack */
0489     ps2dev->flags &= PS2_FLAG_NAK;
0490 }
0491 EXPORT_SYMBOL(ps2_cmd_aborted);