blob: 3a95b508bf27b93b014503f06687a7e9ff92b86b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070016#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
25MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
26MODULE_DESCRIPTION("PS/2 driver library");
27MODULE_LICENSE("GPL");
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
Dmitry Torokhovc6117632005-06-01 02:39:51 -050030 * 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 Torvalds1da177e2005-04-16 15:20:36 -070033 *
Dmitry Torokhovc6117632005-06-01 02:39:51 -050034 * ps2_sendbyte() can only be called from a process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
36
37int 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 Torokhov5206c0d2006-09-14 01:31:40 -040055EXPORT_SYMBOL(ps2_sendbyte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
Dmitry Torokhovc6117632005-06-01 02:39:51 -050058 * ps2_drain() waits for device to transmit requested number of bytes
59 * and discards them.
60 */
61
62void 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 Venc4e32e92006-02-19 00:21:55 -050069 mutex_lock(&ps2dev->cmd_mutex);
Dmitry Torokhovc6117632005-06-01 02:39:51 -050070
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 Venc4e32e92006-02-19 00:21:55 -050079 mutex_unlock(&ps2dev->cmd_mutex);
Dmitry Torokhovc6117632005-06-01 02:39:51 -050080}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -040081EXPORT_SYMBOL(ps2_drain);
Dmitry Torokhovc6117632005-06-01 02:39:51 -050082
83/*
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -050084 * ps2_is_keyboard_id() checks received ID byte against the list of
85 * known keyboard IDs.
86 */
87
Dmitry Torokhov98078792006-09-14 01:31:27 -040088int ps2_is_keyboard_id(char id_byte)
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -050089{
Tobias Klauserc5a69d52007-02-17 20:11:19 +010090 static const char keyboard_ids[] = {
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -050091 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 Torokhov5206c0d2006-09-14 01:31:40 -0400101EXPORT_SYMBOL(ps2_is_keyboard_id);
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -0500102
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
109static 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 Torokhov98078792006-09-14 01:31:27 -0400126 * 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 Torokhov905ab9d2005-06-01 02:39:53 -0500139 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700158 * 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
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700164int __ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 int timeout;
167 int send = (command >> 12) & 0xf;
168 int receive = (command >> 8) & 0xf;
169 int rc = -1;
170 int i;
171
Dmitry Torokhovc6117632005-06-01 02:39:51 -0500172 if (receive > sizeof(ps2dev->cmdbuf)) {
173 WARN_ON(1);
174 return -1;
175 }
176
Dmitry Torokhov95349fe2006-07-06 23:54:48 -0400177 if (send && !param) {
178 WARN_ON(1);
179 return -1;
180 }
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 serio_pause_rx(ps2dev->serio);
183 ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
184 ps2dev->cmdcnt = receive;
185 if (receive && param)
186 for (i = 0; i < receive; i++)
187 ps2dev->cmdbuf[(receive - 1) - i] = param[i];
188 serio_continue_rx(ps2dev->serio);
189
190 /*
191 * Some devices (Synaptics) peform the reset before
192 * ACKing the reset command, and so it can take a long
193 * time before the ACK arrrives.
194 */
Dmitry Torokhovc6117632005-06-01 02:39:51 -0500195 if (ps2_sendbyte(ps2dev, command & 0xff,
196 command == PS2_CMD_RESET_BAT ? 1000 : 200))
197 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 for (i = 0; i < send; i++)
200 if (ps2_sendbyte(ps2dev, param[i], 200))
201 goto out;
202
203 /*
204 * The reset command takes a long time to execute.
205 */
206 timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
207
208 timeout = wait_event_timeout(ps2dev->wait,
209 !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
210
Dmitry Torokhova3ce6ea2009-05-28 09:51:31 -0700211 if (ps2dev->cmdcnt && !(ps2dev->flags & PS2_FLAG_CMD1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -0500213 timeout = ps2_adjust_timeout(ps2dev, command, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 wait_event_timeout(ps2dev->wait,
215 !(ps2dev->flags & PS2_FLAG_CMD), timeout);
216 }
217
218 if (param)
219 for (i = 0; i < receive; i++)
220 param[i] = ps2dev->cmdbuf[(receive - 1) - i];
221
222 if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
223 goto out;
224
225 rc = 0;
226
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -0500227 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 serio_pause_rx(ps2dev->serio);
229 ps2dev->flags = 0;
230 serio_continue_rx(ps2dev->serio);
231
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700232 return rc;
233}
234EXPORT_SYMBOL(__ps2_command);
235
236int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
237{
238 int rc;
239
240 mutex_lock(&ps2dev->cmd_mutex);
241 rc = __ps2_command(ps2dev, param, command);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500242 mutex_unlock(&ps2dev->cmd_mutex);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return rc;
245}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400246EXPORT_SYMBOL(ps2_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * ps2_init() initializes ps2dev structure
250 */
251
252void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
253{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500254 mutex_init(&ps2dev->cmd_mutex);
Jiri Kosina88aa0102006-10-11 01:45:31 -0400255 lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 init_waitqueue_head(&ps2dev->wait);
257 ps2dev->serio = serio;
258}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400259EXPORT_SYMBOL(ps2_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261/*
262 * ps2_handle_ack() is supposed to be used in interrupt handler
263 * to properly process ACK/NAK of a command from a PS/2 device.
264 */
265
266int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
267{
268 switch (data) {
269 case PS2_RET_ACK:
270 ps2dev->nak = 0;
271 break;
272
273 case PS2_RET_NAK:
Dmitry Torokhova2d781f2008-11-19 17:02:24 -0500274 ps2dev->flags |= PS2_FLAG_NAK;
275 ps2dev->nak = PS2_RET_NAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277
Dmitry Torokhova2d781f2008-11-19 17:02:24 -0500278 case PS2_RET_ERR:
279 if (ps2dev->flags & PS2_FLAG_NAK) {
280 ps2dev->flags &= ~PS2_FLAG_NAK;
281 ps2dev->nak = PS2_RET_ERR;
282 break;
283 }
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /*
286 * Workaround for mice which don't ACK the Get ID command.
287 * These are valid mouse IDs that we recognize.
288 */
289 case 0x00:
290 case 0x03:
291 case 0x04:
292 if (ps2dev->flags & PS2_FLAG_WAITID) {
293 ps2dev->nak = 0;
294 break;
295 }
296 /* Fall through */
297 default:
298 return 0;
299 }
300
301
Dmitry Torokhova2d781f2008-11-19 17:02:24 -0500302 if (!ps2dev->nak) {
303 ps2dev->flags &= ~PS2_FLAG_NAK;
304 if (ps2dev->cmdcnt)
305 ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 ps2dev->flags &= ~PS2_FLAG_ACK;
309 wake_up(&ps2dev->wait);
310
311 if (data != PS2_RET_ACK)
312 ps2_handle_response(ps2dev, data);
313
314 return 1;
315}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400316EXPORT_SYMBOL(ps2_handle_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318/*
319 * ps2_handle_response() is supposed to be used in interrupt handler
320 * to properly store device's response to a command and notify process
321 * waiting for completion of the command.
322 */
323
324int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
325{
326 if (ps2dev->cmdcnt)
327 ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
328
329 if (ps2dev->flags & PS2_FLAG_CMD1) {
330 ps2dev->flags &= ~PS2_FLAG_CMD1;
331 if (ps2dev->cmdcnt)
332 wake_up(&ps2dev->wait);
333 }
334
335 if (!ps2dev->cmdcnt) {
336 ps2dev->flags &= ~PS2_FLAG_CMD;
337 wake_up(&ps2dev->wait);
338 }
339
340 return 1;
341}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400342EXPORT_SYMBOL(ps2_handle_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344void ps2_cmd_aborted(struct ps2dev *ps2dev)
345{
346 if (ps2dev->flags & PS2_FLAG_ACK)
347 ps2dev->nak = 1;
348
349 if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
350 wake_up(&ps2dev->wait);
351
Dmitry Torokhova2d781f2008-11-19 17:02:24 -0500352 /* reset all flags except last nack */
353 ps2dev->flags &= PS2_FLAG_NAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400355EXPORT_SYMBOL(ps2_cmd_aborted);