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/sched.h>
17 #include <linux/interrupt.h>
18 #include <linux/input.h>
19 #include <linux/serio.h>
20 #include <linux/i8042.h>
21 #include <linux/init.h>
22 #include <linux/libps2.h>
24 #define DRIVER_DESC "PS/2 driver library"
26 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
27 MODULE_DESCRIPTION("PS/2 driver library");
28 MODULE_LICENSE("GPL");
31 * ps2_sendbyte() sends a byte to the device and waits for acknowledge.
32 * It doesn't handle retransmission, though it could - because if there
33 * is a need for retransmissions device has to be replaced anyway.
35 * ps2_sendbyte() can only be called from a process context.
38 int ps2_sendbyte(struct ps2dev
*ps2dev
, unsigned char byte
, int timeout
)
40 serio_pause_rx(ps2dev
->serio
);
42 ps2dev
->flags
|= PS2_FLAG_ACK
;
43 serio_continue_rx(ps2dev
->serio
);
45 if (serio_write(ps2dev
->serio
, byte
) == 0)
46 wait_event_timeout(ps2dev
->wait
,
47 !(ps2dev
->flags
& PS2_FLAG_ACK
),
48 msecs_to_jiffies(timeout
));
50 serio_pause_rx(ps2dev
->serio
);
51 ps2dev
->flags
&= ~PS2_FLAG_ACK
;
52 serio_continue_rx(ps2dev
->serio
);
56 EXPORT_SYMBOL(ps2_sendbyte
);
58 void ps2_begin_command(struct ps2dev
*ps2dev
)
60 struct mutex
*m
= ps2dev
->serio
->ps2_cmd_mutex
?: &ps2dev
->cmd_mutex
;
64 EXPORT_SYMBOL(ps2_begin_command
);
66 void ps2_end_command(struct ps2dev
*ps2dev
)
68 struct mutex
*m
= ps2dev
->serio
->ps2_cmd_mutex
?: &ps2dev
->cmd_mutex
;
72 EXPORT_SYMBOL(ps2_end_command
);
75 * ps2_drain() waits for device to transmit requested number of bytes
79 void ps2_drain(struct ps2dev
*ps2dev
, int maxbytes
, int timeout
)
81 if (maxbytes
> sizeof(ps2dev
->cmdbuf
)) {
83 maxbytes
= sizeof(ps2dev
->cmdbuf
);
86 ps2_begin_command(ps2dev
);
88 serio_pause_rx(ps2dev
->serio
);
89 ps2dev
->flags
= PS2_FLAG_CMD
;
90 ps2dev
->cmdcnt
= maxbytes
;
91 serio_continue_rx(ps2dev
->serio
);
93 wait_event_timeout(ps2dev
->wait
,
94 !(ps2dev
->flags
& PS2_FLAG_CMD
),
95 msecs_to_jiffies(timeout
));
97 ps2_end_command(ps2dev
);
99 EXPORT_SYMBOL(ps2_drain
);
102 * ps2_is_keyboard_id() checks received ID byte against the list of
103 * known keyboard IDs.
106 int ps2_is_keyboard_id(char id_byte
)
108 static const char keyboard_ids
[] = {
109 0xab, /* Regular keyboards */
110 0xac, /* NCD Sun keyboard */
111 0x2b, /* Trust keyboard, translated */
112 0x5d, /* Trust keyboard */
113 0x60, /* NMB SGI keyboard, translated */
114 0x47, /* NMB SGI keyboard */
117 return memchr(keyboard_ids
, id_byte
, sizeof(keyboard_ids
)) != NULL
;
119 EXPORT_SYMBOL(ps2_is_keyboard_id
);
122 * ps2_adjust_timeout() is called after receiving 1st byte of command
123 * response and tries to reduce remaining timeout to speed up command
127 static int ps2_adjust_timeout(struct ps2dev
*ps2dev
, int command
, int timeout
)
130 case PS2_CMD_RESET_BAT
:
132 * Device has sent the first response byte after
133 * reset command, reset is thus done, so we can
134 * shorten the timeout.
135 * The next byte will come soon (keyboard) or not
138 if (timeout
> msecs_to_jiffies(100))
139 timeout
= msecs_to_jiffies(100);
144 * Microsoft Natural Elite keyboard responds to
145 * the GET ID command as it were a mouse, with
146 * a single byte. Fail the command so atkbd will
147 * use alternative probe to detect it.
149 if (ps2dev
->cmdbuf
[1] == 0xaa) {
150 serio_pause_rx(ps2dev
->serio
);
152 serio_continue_rx(ps2dev
->serio
);
157 * If device behind the port is not a keyboard there
158 * won't be 2nd byte of ID response.
160 if (!ps2_is_keyboard_id(ps2dev
->cmdbuf
[1])) {
161 serio_pause_rx(ps2dev
->serio
);
162 ps2dev
->flags
= ps2dev
->cmdcnt
= 0;
163 serio_continue_rx(ps2dev
->serio
);
176 * ps2_command() sends a command and its parameters to the mouse,
177 * then waits for the response and puts it in the param array.
179 * ps2_command() can only be called from a process context
182 int __ps2_command(struct ps2dev
*ps2dev
, unsigned char *param
, int command
)
185 int send
= (command
>> 12) & 0xf;
186 int receive
= (command
>> 8) & 0xf;
190 if (receive
> sizeof(ps2dev
->cmdbuf
)) {
195 if (send
&& !param
) {
200 serio_pause_rx(ps2dev
->serio
);
201 ps2dev
->flags
= command
== PS2_CMD_GETID
? PS2_FLAG_WAITID
: 0;
202 ps2dev
->cmdcnt
= receive
;
203 if (receive
&& param
)
204 for (i
= 0; i
< receive
; i
++)
205 ps2dev
->cmdbuf
[(receive
- 1) - i
] = param
[i
];
206 serio_continue_rx(ps2dev
->serio
);
209 * Some devices (Synaptics) peform the reset before
210 * ACKing the reset command, and so it can take a long
211 * time before the ACK arrives.
213 if (ps2_sendbyte(ps2dev
, command
& 0xff,
214 command
== PS2_CMD_RESET_BAT
? 1000 : 200))
217 for (i
= 0; i
< send
; i
++)
218 if (ps2_sendbyte(ps2dev
, param
[i
], 200))
222 * The reset command takes a long time to execute.
224 timeout
= msecs_to_jiffies(command
== PS2_CMD_RESET_BAT
? 4000 : 500);
226 timeout
= wait_event_timeout(ps2dev
->wait
,
227 !(ps2dev
->flags
& PS2_FLAG_CMD1
), timeout
);
229 if (ps2dev
->cmdcnt
&& !(ps2dev
->flags
& PS2_FLAG_CMD1
)) {
231 timeout
= ps2_adjust_timeout(ps2dev
, command
, timeout
);
232 wait_event_timeout(ps2dev
->wait
,
233 !(ps2dev
->flags
& PS2_FLAG_CMD
), timeout
);
237 for (i
= 0; i
< receive
; i
++)
238 param
[i
] = ps2dev
->cmdbuf
[(receive
- 1) - i
];
240 if (ps2dev
->cmdcnt
&& (command
!= PS2_CMD_RESET_BAT
|| ps2dev
->cmdcnt
!= 1))
246 serio_pause_rx(ps2dev
->serio
);
248 serio_continue_rx(ps2dev
->serio
);
252 EXPORT_SYMBOL(__ps2_command
);
254 int ps2_command(struct ps2dev
*ps2dev
, unsigned char *param
, int command
)
258 ps2_begin_command(ps2dev
);
259 rc
= __ps2_command(ps2dev
, param
, command
);
260 ps2_end_command(ps2dev
);
264 EXPORT_SYMBOL(ps2_command
);
267 * ps2_init() initializes ps2dev structure
270 void ps2_init(struct ps2dev
*ps2dev
, struct serio
*serio
)
272 mutex_init(&ps2dev
->cmd_mutex
);
273 lockdep_set_subclass(&ps2dev
->cmd_mutex
, serio
->depth
);
274 init_waitqueue_head(&ps2dev
->wait
);
275 ps2dev
->serio
= serio
;
277 EXPORT_SYMBOL(ps2_init
);
280 * ps2_handle_ack() is supposed to be used in interrupt handler
281 * to properly process ACK/NAK of a command from a PS/2 device.
284 int ps2_handle_ack(struct ps2dev
*ps2dev
, unsigned char data
)
292 ps2dev
->flags
|= PS2_FLAG_NAK
;
293 ps2dev
->nak
= PS2_RET_NAK
;
297 if (ps2dev
->flags
& PS2_FLAG_NAK
) {
298 ps2dev
->flags
&= ~PS2_FLAG_NAK
;
299 ps2dev
->nak
= PS2_RET_ERR
;
304 * Workaround for mice which don't ACK the Get ID command.
305 * These are valid mouse IDs that we recognize.
310 if (ps2dev
->flags
& PS2_FLAG_WAITID
) {
321 ps2dev
->flags
&= ~PS2_FLAG_NAK
;
323 ps2dev
->flags
|= PS2_FLAG_CMD
| PS2_FLAG_CMD1
;
326 ps2dev
->flags
&= ~PS2_FLAG_ACK
;
327 wake_up(&ps2dev
->wait
);
329 if (data
!= PS2_RET_ACK
)
330 ps2_handle_response(ps2dev
, data
);
334 EXPORT_SYMBOL(ps2_handle_ack
);
337 * ps2_handle_response() is supposed to be used in interrupt handler
338 * to properly store device's response to a command and notify process
339 * waiting for completion of the command.
342 int ps2_handle_response(struct ps2dev
*ps2dev
, unsigned char data
)
345 ps2dev
->cmdbuf
[--ps2dev
->cmdcnt
] = data
;
347 if (ps2dev
->flags
& PS2_FLAG_CMD1
) {
348 ps2dev
->flags
&= ~PS2_FLAG_CMD1
;
350 wake_up(&ps2dev
->wait
);
353 if (!ps2dev
->cmdcnt
) {
354 ps2dev
->flags
&= ~PS2_FLAG_CMD
;
355 wake_up(&ps2dev
->wait
);
360 EXPORT_SYMBOL(ps2_handle_response
);
362 void ps2_cmd_aborted(struct ps2dev
*ps2dev
)
364 if (ps2dev
->flags
& PS2_FLAG_ACK
)
367 if (ps2dev
->flags
& (PS2_FLAG_ACK
| PS2_FLAG_CMD
))
368 wake_up(&ps2dev
->wait
);
370 /* reset all flags except last nack */
371 ps2dev
->flags
&= PS2_FLAG_NAK
;
373 EXPORT_SYMBOL(ps2_cmd_aborted
);