4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2004 Dmitry Torokhov
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.
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/input.h>
19 #include <linux/serio.h>
20 #include <linux/init.h>
21 #include <linux/libps2.h>
23 #define DRIVER_DESC "PS/2 driver library"
25 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
26 MODULE_DESCRIPTION("PS/2 driver library");
27 MODULE_LICENSE("GPL");
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.
34 * ps2_sendbyte() can only be called from a process context.
37 int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout)
39 serio_pause_rx(ps2dev->serio);
41 ps2dev->flags |= PS2_FLAG_ACK;
42 serio_continue_rx(ps2dev->serio);
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));
49 serio_pause_rx(ps2dev->serio);
50 ps2dev->flags &= ~PS2_FLAG_ACK;
51 serio_continue_rx(ps2dev->serio);
55 EXPORT_SYMBOL(ps2_sendbyte);
58 * ps2_drain() waits for device to transmit requested number of bytes
62 void ps2_drain(struct ps2dev *ps2dev, int maxbytes, int timeout)
64 if (maxbytes > sizeof(ps2dev->cmdbuf)) {
66 maxbytes = sizeof(ps2dev->cmdbuf);
69 mutex_lock(&ps2dev->cmd_mutex);
71 serio_pause_rx(ps2dev->serio);
72 ps2dev->flags = PS2_FLAG_CMD;
73 ps2dev->cmdcnt = maxbytes;
74 serio_continue_rx(ps2dev->serio);
76 wait_event_timeout(ps2dev->wait,
77 !(ps2dev->flags & PS2_FLAG_CMD),
78 msecs_to_jiffies(timeout));
79 mutex_unlock(&ps2dev->cmd_mutex);
81 EXPORT_SYMBOL(ps2_drain);
84 * ps2_is_keyboard_id() checks received ID byte against the list of
88 int ps2_is_keyboard_id(char id_byte)
90 static const char keyboard_ids[] = {
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 */
99 return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
101 EXPORT_SYMBOL(ps2_is_keyboard_id);
104 * ps2_adjust_timeout() is called after receiving 1st byte of command
105 * response and tries to reduce remaining timeout to speed up command
109 static int ps2_adjust_timeout(struct ps2dev *ps2dev, int command, int timeout)
112 case PS2_CMD_RESET_BAT:
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
120 if (timeout > msecs_to_jiffies(100))
121 timeout = msecs_to_jiffies(100);
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.
131 if (ps2dev->cmdbuf[1] == 0xaa) {
132 serio_pause_rx(ps2dev->serio);
134 serio_continue_rx(ps2dev->serio);
139 * If device behind the port is not a keyboard there
140 * won't be 2nd byte of ID response.
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);
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.
161 * ps2_command() can only be called from a process context
164 int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
167 int send = (command >> 12) & 0xf;
168 int receive = (command >> 8) & 0xf;
172 if (receive > sizeof(ps2dev->cmdbuf)) {
177 if (send && !param) {
182 mutex_lock(&ps2dev->cmd_mutex);
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);
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.
197 if (ps2_sendbyte(ps2dev, command & 0xff,
198 command == PS2_CMD_RESET_BAT ? 1000 : 200))
201 for (i = 0; i < send; i++)
202 if (ps2_sendbyte(ps2dev, param[i], 200))
206 * The reset command takes a long time to execute.
208 timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
210 timeout = wait_event_timeout(ps2dev->wait,
211 !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
213 if (ps2dev->cmdcnt && timeout > 0) {
215 timeout = ps2_adjust_timeout(ps2dev, command, timeout);
216 wait_event_timeout(ps2dev->wait,
217 !(ps2dev->flags & PS2_FLAG_CMD), timeout);
221 for (i = 0; i < receive; i++)
222 param[i] = ps2dev->cmdbuf[(receive - 1) - i];
224 if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
230 serio_pause_rx(ps2dev->serio);
232 serio_continue_rx(ps2dev->serio);
234 mutex_unlock(&ps2dev->cmd_mutex);
237 EXPORT_SYMBOL(ps2_command);
240 * ps2_init() initializes ps2dev structure
243 void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
245 mutex_init(&ps2dev->cmd_mutex);
246 lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth);
247 init_waitqueue_head(&ps2dev->wait);
248 ps2dev->serio = serio;
250 EXPORT_SYMBOL(ps2_init);
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.
257 int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
269 * Workaround for mice which don't ACK the Get ID command.
270 * These are valid mouse IDs that we recognize.
275 if (ps2dev->flags & PS2_FLAG_WAITID) {
285 if (!ps2dev->nak && ps2dev->cmdcnt)
286 ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
288 ps2dev->flags &= ~PS2_FLAG_ACK;
289 wake_up(&ps2dev->wait);
291 if (data != PS2_RET_ACK)
292 ps2_handle_response(ps2dev, data);
296 EXPORT_SYMBOL(ps2_handle_ack);
299 * ps2_handle_response() is supposed to be used in interrupt handler
300 * to properly store device's response to a command and notify process
301 * waiting for completion of the command.
304 int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
307 ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
309 if (ps2dev->flags & PS2_FLAG_CMD1) {
310 ps2dev->flags &= ~PS2_FLAG_CMD1;
312 wake_up(&ps2dev->wait);
315 if (!ps2dev->cmdcnt) {
316 ps2dev->flags &= ~PS2_FLAG_CMD;
317 wake_up(&ps2dev->wait);
322 EXPORT_SYMBOL(ps2_handle_response);
324 void ps2_cmd_aborted(struct ps2dev *ps2dev)
326 if (ps2dev->flags & PS2_FLAG_ACK)
329 if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
330 wake_up(&ps2dev->wait);
334 EXPORT_SYMBOL(ps2_cmd_aborted);