Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PS/2 driver library |
| 3 | * |
| 4 | * Copyright (c) 1999-2002 Vojtech Pavlik |
| 5 | * Copyright (c) 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 | #include <linux/delay.h> |
| 15 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/input.h> |
| 19 | #include <linux/serio.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/libps2.h> |
| 22 | |
| 23 | #define DRIVER_DESC "PS/2 driver library" |
| 24 | |
| 25 | MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>"); |
| 26 | MODULE_DESCRIPTION("PS/2 driver library"); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* |
Dmitry Torokhov | c611763 | 2005-06-01 02:39:51 -0500 | [diff] [blame] | 30 | * ps2_sendbyte() sends a byte to the device and waits for acknowledge. |
| 31 | * It doesn't handle retransmission, though it could - because if there |
| 32 | * is a need for retransmissions device has to be replaced anyway. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * |
Dmitry Torokhov | c611763 | 2005-06-01 02:39:51 -0500 | [diff] [blame] | 34 | * ps2_sendbyte() can only be called from a process context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout) |
| 38 | { |
| 39 | serio_pause_rx(ps2dev->serio); |
| 40 | ps2dev->nak = 1; |
| 41 | ps2dev->flags |= PS2_FLAG_ACK; |
| 42 | serio_continue_rx(ps2dev->serio); |
| 43 | |
| 44 | if (serio_write(ps2dev->serio, byte) == 0) |
| 45 | wait_event_timeout(ps2dev->wait, |
| 46 | !(ps2dev->flags & PS2_FLAG_ACK), |
| 47 | msecs_to_jiffies(timeout)); |
| 48 | |
| 49 | serio_pause_rx(ps2dev->serio); |
| 50 | ps2dev->flags &= ~PS2_FLAG_ACK; |
| 51 | serio_continue_rx(ps2dev->serio); |
| 52 | |
| 53 | return -ps2dev->nak; |
| 54 | } |
Dmitry Torokhov | 5206c0d | 2006-09-14 01:31:40 -0400 | [diff] [blame] | 55 | EXPORT_SYMBOL(ps2_sendbyte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
Dmitry Torokhov | c611763 | 2005-06-01 02:39:51 -0500 | [diff] [blame] | 58 | * ps2_drain() waits for device to transmit requested number of bytes |
| 59 | * and discards them. |
| 60 | */ |
| 61 | |
| 62 | void ps2_drain(struct ps2dev *ps2dev, int maxbytes, int timeout) |
| 63 | { |
| 64 | if (maxbytes > sizeof(ps2dev->cmdbuf)) { |
| 65 | WARN_ON(1); |
| 66 | maxbytes = sizeof(ps2dev->cmdbuf); |
| 67 | } |
| 68 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 69 | mutex_lock(&ps2dev->cmd_mutex); |
Dmitry Torokhov | c611763 | 2005-06-01 02:39:51 -0500 | [diff] [blame] | 70 | |
| 71 | serio_pause_rx(ps2dev->serio); |
| 72 | ps2dev->flags = PS2_FLAG_CMD; |
| 73 | ps2dev->cmdcnt = maxbytes; |
| 74 | serio_continue_rx(ps2dev->serio); |
| 75 | |
| 76 | wait_event_timeout(ps2dev->wait, |
| 77 | !(ps2dev->flags & PS2_FLAG_CMD), |
| 78 | msecs_to_jiffies(timeout)); |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 79 | mutex_unlock(&ps2dev->cmd_mutex); |
Dmitry Torokhov | c611763 | 2005-06-01 02:39:51 -0500 | [diff] [blame] | 80 | } |
Dmitry Torokhov | 5206c0d | 2006-09-14 01:31:40 -0400 | [diff] [blame] | 81 | EXPORT_SYMBOL(ps2_drain); |
Dmitry Torokhov | c611763 | 2005-06-01 02:39:51 -0500 | [diff] [blame] | 82 | |
| 83 | /* |
Dmitry Torokhov | 905ab9d | 2005-06-01 02:39:53 -0500 | [diff] [blame] | 84 | * ps2_is_keyboard_id() checks received ID byte against the list of |
| 85 | * known keyboard IDs. |
| 86 | */ |
| 87 | |
Dmitry Torokhov | 9807879 | 2006-09-14 01:31:27 -0400 | [diff] [blame] | 88 | int ps2_is_keyboard_id(char id_byte) |
Dmitry Torokhov | 905ab9d | 2005-06-01 02:39:53 -0500 | [diff] [blame] | 89 | { |
Tobias Klauser | c5a69d5 | 2007-02-17 20:11:19 +0100 | [diff] [blame] | 90 | static const char keyboard_ids[] = { |
Dmitry Torokhov | 905ab9d | 2005-06-01 02:39:53 -0500 | [diff] [blame] | 91 | 0xab, /* Regular keyboards */ |
| 92 | 0xac, /* NCD Sun keyboard */ |
| 93 | 0x2b, /* Trust keyboard, translated */ |
| 94 | 0x5d, /* Trust keyboard */ |
| 95 | 0x60, /* NMB SGI keyboard, translated */ |
| 96 | 0x47, /* NMB SGI keyboard */ |
| 97 | }; |
| 98 | |
| 99 | return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL; |
| 100 | } |
Dmitry Torokhov | 5206c0d | 2006-09-14 01:31:40 -0400 | [diff] [blame] | 101 | EXPORT_SYMBOL(ps2_is_keyboard_id); |
Dmitry Torokhov | 905ab9d | 2005-06-01 02:39:53 -0500 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * ps2_adjust_timeout() is called after receiving 1st byte of command |
| 105 | * response and tries to reduce remaining timeout to speed up command |
| 106 | * completion. |
| 107 | */ |
| 108 | |
| 109 | static int ps2_adjust_timeout(struct ps2dev *ps2dev, int command, int timeout) |
| 110 | { |
| 111 | switch (command) { |
| 112 | case PS2_CMD_RESET_BAT: |
| 113 | /* |
| 114 | * Device has sent the first response byte after |
| 115 | * reset command, reset is thus done, so we can |
| 116 | * shorten the timeout. |
| 117 | * The next byte will come soon (keyboard) or not |
| 118 | * at all (mouse). |
| 119 | */ |
| 120 | if (timeout > msecs_to_jiffies(100)) |
| 121 | timeout = msecs_to_jiffies(100); |
| 122 | break; |
| 123 | |
| 124 | case PS2_CMD_GETID: |
| 125 | /* |
Dmitry Torokhov | 9807879 | 2006-09-14 01:31:27 -0400 | [diff] [blame] | 126 | * Microsoft Natural Elite keyboard responds to |
| 127 | * the GET ID command as it were a mouse, with |
| 128 | * a single byte. Fail the command so atkbd will |
| 129 | * use alternative probe to detect it. |
| 130 | */ |
| 131 | if (ps2dev->cmdbuf[1] == 0xaa) { |
| 132 | serio_pause_rx(ps2dev->serio); |
| 133 | ps2dev->flags = 0; |
| 134 | serio_continue_rx(ps2dev->serio); |
| 135 | timeout = 0; |
| 136 | } |
| 137 | |
| 138 | /* |
Dmitry Torokhov | 905ab9d | 2005-06-01 02:39:53 -0500 | [diff] [blame] | 139 | * If device behind the port is not a keyboard there |
| 140 | * won't be 2nd byte of ID response. |
| 141 | */ |
| 142 | if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) { |
| 143 | serio_pause_rx(ps2dev->serio); |
| 144 | ps2dev->flags = ps2dev->cmdcnt = 0; |
| 145 | serio_continue_rx(ps2dev->serio); |
| 146 | timeout = 0; |
| 147 | } |
| 148 | break; |
| 149 | |
| 150 | default: |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | return timeout; |
| 155 | } |
| 156 | |
| 157 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | * ps2_command() sends a command and its parameters to the mouse, |
| 159 | * then waits for the response and puts it in the param array. |
| 160 | * |
| 161 | * ps2_command() can only be called from a process context |
| 162 | */ |
| 163 | |
| 164 | int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command) |
| 165 | { |
| 166 | int timeout; |
| 167 | int send = (command >> 12) & 0xf; |
| 168 | int receive = (command >> 8) & 0xf; |
| 169 | int rc = -1; |
| 170 | int i; |
| 171 | |
Dmitry Torokhov | c611763 | 2005-06-01 02:39:51 -0500 | [diff] [blame] | 172 | if (receive > sizeof(ps2dev->cmdbuf)) { |
| 173 | WARN_ON(1); |
| 174 | return -1; |
| 175 | } |
| 176 | |
Dmitry Torokhov | 95349fe | 2006-07-06 23:54:48 -0400 | [diff] [blame] | 177 | if (send && !param) { |
| 178 | WARN_ON(1); |
| 179 | return -1; |
| 180 | } |
| 181 | |
Jiri Kosina | 88aa010 | 2006-10-11 01:45:31 -0400 | [diff] [blame] | 182 | mutex_lock(&ps2dev->cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
| 184 | serio_pause_rx(ps2dev->serio); |
| 185 | ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0; |
| 186 | ps2dev->cmdcnt = receive; |
| 187 | if (receive && param) |
| 188 | for (i = 0; i < receive; i++) |
| 189 | ps2dev->cmdbuf[(receive - 1) - i] = param[i]; |
| 190 | serio_continue_rx(ps2dev->serio); |
| 191 | |
| 192 | /* |
| 193 | * Some devices (Synaptics) peform the reset before |
| 194 | * ACKing the reset command, and so it can take a long |
| 195 | * time before the ACK arrrives. |
| 196 | */ |
Dmitry Torokhov | c611763 | 2005-06-01 02:39:51 -0500 | [diff] [blame] | 197 | if (ps2_sendbyte(ps2dev, command & 0xff, |
| 198 | command == PS2_CMD_RESET_BAT ? 1000 : 200)) |
| 199 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | for (i = 0; i < send; i++) |
| 202 | if (ps2_sendbyte(ps2dev, param[i], 200)) |
| 203 | goto out; |
| 204 | |
| 205 | /* |
| 206 | * The reset command takes a long time to execute. |
| 207 | */ |
| 208 | timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500); |
| 209 | |
| 210 | timeout = wait_event_timeout(ps2dev->wait, |
| 211 | !(ps2dev->flags & PS2_FLAG_CMD1), timeout); |
| 212 | |
Dmitry Torokhov | a3ce6ea | 2009-05-28 09:51:31 -0700 | [diff] [blame^] | 213 | if (ps2dev->cmdcnt && !(ps2dev->flags & PS2_FLAG_CMD1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Dmitry Torokhov | 905ab9d | 2005-06-01 02:39:53 -0500 | [diff] [blame] | 215 | timeout = ps2_adjust_timeout(ps2dev, command, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | wait_event_timeout(ps2dev->wait, |
| 217 | !(ps2dev->flags & PS2_FLAG_CMD), timeout); |
| 218 | } |
| 219 | |
| 220 | if (param) |
| 221 | for (i = 0; i < receive; i++) |
| 222 | param[i] = ps2dev->cmdbuf[(receive - 1) - i]; |
| 223 | |
| 224 | if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1)) |
| 225 | goto out; |
| 226 | |
| 227 | rc = 0; |
| 228 | |
Dmitry Torokhov | 905ab9d | 2005-06-01 02:39:53 -0500 | [diff] [blame] | 229 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | serio_pause_rx(ps2dev->serio); |
| 231 | ps2dev->flags = 0; |
| 232 | serio_continue_rx(ps2dev->serio); |
| 233 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 234 | mutex_unlock(&ps2dev->cmd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return rc; |
| 236 | } |
Dmitry Torokhov | 5206c0d | 2006-09-14 01:31:40 -0400 | [diff] [blame] | 237 | EXPORT_SYMBOL(ps2_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | * ps2_init() initializes ps2dev structure |
| 241 | */ |
| 242 | |
| 243 | void ps2_init(struct ps2dev *ps2dev, struct serio *serio) |
| 244 | { |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 245 | mutex_init(&ps2dev->cmd_mutex); |
Jiri Kosina | 88aa010 | 2006-10-11 01:45:31 -0400 | [diff] [blame] | 246 | lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | init_waitqueue_head(&ps2dev->wait); |
| 248 | ps2dev->serio = serio; |
| 249 | } |
Dmitry Torokhov | 5206c0d | 2006-09-14 01:31:40 -0400 | [diff] [blame] | 250 | EXPORT_SYMBOL(ps2_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * ps2_handle_ack() is supposed to be used in interrupt handler |
| 254 | * to properly process ACK/NAK of a command from a PS/2 device. |
| 255 | */ |
| 256 | |
| 257 | int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data) |
| 258 | { |
| 259 | switch (data) { |
| 260 | case PS2_RET_ACK: |
| 261 | ps2dev->nak = 0; |
| 262 | break; |
| 263 | |
| 264 | case PS2_RET_NAK: |
Dmitry Torokhov | a2d781f | 2008-11-19 17:02:24 -0500 | [diff] [blame] | 265 | ps2dev->flags |= PS2_FLAG_NAK; |
| 266 | ps2dev->nak = PS2_RET_NAK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | break; |
| 268 | |
Dmitry Torokhov | a2d781f | 2008-11-19 17:02:24 -0500 | [diff] [blame] | 269 | case PS2_RET_ERR: |
| 270 | if (ps2dev->flags & PS2_FLAG_NAK) { |
| 271 | ps2dev->flags &= ~PS2_FLAG_NAK; |
| 272 | ps2dev->nak = PS2_RET_ERR; |
| 273 | break; |
| 274 | } |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | /* |
| 277 | * Workaround for mice which don't ACK the Get ID command. |
| 278 | * These are valid mouse IDs that we recognize. |
| 279 | */ |
| 280 | case 0x00: |
| 281 | case 0x03: |
| 282 | case 0x04: |
| 283 | if (ps2dev->flags & PS2_FLAG_WAITID) { |
| 284 | ps2dev->nak = 0; |
| 285 | break; |
| 286 | } |
| 287 | /* Fall through */ |
| 288 | default: |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | |
Dmitry Torokhov | a2d781f | 2008-11-19 17:02:24 -0500 | [diff] [blame] | 293 | if (!ps2dev->nak) { |
| 294 | ps2dev->flags &= ~PS2_FLAG_NAK; |
| 295 | if (ps2dev->cmdcnt) |
| 296 | ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1; |
| 297 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | ps2dev->flags &= ~PS2_FLAG_ACK; |
| 300 | wake_up(&ps2dev->wait); |
| 301 | |
| 302 | if (data != PS2_RET_ACK) |
| 303 | ps2_handle_response(ps2dev, data); |
| 304 | |
| 305 | return 1; |
| 306 | } |
Dmitry Torokhov | 5206c0d | 2006-09-14 01:31:40 -0400 | [diff] [blame] | 307 | EXPORT_SYMBOL(ps2_handle_ack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * ps2_handle_response() is supposed to be used in interrupt handler |
| 311 | * to properly store device's response to a command and notify process |
| 312 | * waiting for completion of the command. |
| 313 | */ |
| 314 | |
| 315 | int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data) |
| 316 | { |
| 317 | if (ps2dev->cmdcnt) |
| 318 | ps2dev->cmdbuf[--ps2dev->cmdcnt] = data; |
| 319 | |
| 320 | if (ps2dev->flags & PS2_FLAG_CMD1) { |
| 321 | ps2dev->flags &= ~PS2_FLAG_CMD1; |
| 322 | if (ps2dev->cmdcnt) |
| 323 | wake_up(&ps2dev->wait); |
| 324 | } |
| 325 | |
| 326 | if (!ps2dev->cmdcnt) { |
| 327 | ps2dev->flags &= ~PS2_FLAG_CMD; |
| 328 | wake_up(&ps2dev->wait); |
| 329 | } |
| 330 | |
| 331 | return 1; |
| 332 | } |
Dmitry Torokhov | 5206c0d | 2006-09-14 01:31:40 -0400 | [diff] [blame] | 333 | EXPORT_SYMBOL(ps2_handle_response); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | void ps2_cmd_aborted(struct ps2dev *ps2dev) |
| 336 | { |
| 337 | if (ps2dev->flags & PS2_FLAG_ACK) |
| 338 | ps2dev->nak = 1; |
| 339 | |
| 340 | if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD)) |
| 341 | wake_up(&ps2dev->wait); |
| 342 | |
Dmitry Torokhov | a2d781f | 2008-11-19 17:02:24 -0500 | [diff] [blame] | 343 | /* reset all flags except last nack */ |
| 344 | ps2dev->flags &= PS2_FLAG_NAK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
Dmitry Torokhov | 5206c0d | 2006-09-14 01:31:40 -0400 | [diff] [blame] | 346 | EXPORT_SYMBOL(ps2_cmd_aborted); |