blob: b819239d74dc15f0c7fa41fce8a8c9658b33fc1a [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/* Work structure to schedule execution of a command */
30struct ps2work {
31 struct work_struct work;
32 struct ps2dev *ps2dev;
33 int command;
34 unsigned char param[0];
35};
36
37
38/*
Dmitry Torokhovc6117632005-06-01 02:39:51 -050039 * ps2_sendbyte() sends a byte to the device and waits for acknowledge.
40 * It doesn't handle retransmission, though it could - because if there
41 * is a need for retransmissions device has to be replaced anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
Dmitry Torokhovc6117632005-06-01 02:39:51 -050043 * ps2_sendbyte() can only be called from a process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
45
46int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout)
47{
48 serio_pause_rx(ps2dev->serio);
49 ps2dev->nak = 1;
50 ps2dev->flags |= PS2_FLAG_ACK;
51 serio_continue_rx(ps2dev->serio);
52
53 if (serio_write(ps2dev->serio, byte) == 0)
54 wait_event_timeout(ps2dev->wait,
55 !(ps2dev->flags & PS2_FLAG_ACK),
56 msecs_to_jiffies(timeout));
57
58 serio_pause_rx(ps2dev->serio);
59 ps2dev->flags &= ~PS2_FLAG_ACK;
60 serio_continue_rx(ps2dev->serio);
61
62 return -ps2dev->nak;
63}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -040064EXPORT_SYMBOL(ps2_sendbyte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
Dmitry Torokhovc6117632005-06-01 02:39:51 -050067 * ps2_drain() waits for device to transmit requested number of bytes
68 * and discards them.
69 */
70
71void ps2_drain(struct ps2dev *ps2dev, int maxbytes, int timeout)
72{
73 if (maxbytes > sizeof(ps2dev->cmdbuf)) {
74 WARN_ON(1);
75 maxbytes = sizeof(ps2dev->cmdbuf);
76 }
77
Arjan van de Venc4e32e92006-02-19 00:21:55 -050078 mutex_lock(&ps2dev->cmd_mutex);
Dmitry Torokhovc6117632005-06-01 02:39:51 -050079
80 serio_pause_rx(ps2dev->serio);
81 ps2dev->flags = PS2_FLAG_CMD;
82 ps2dev->cmdcnt = maxbytes;
83 serio_continue_rx(ps2dev->serio);
84
85 wait_event_timeout(ps2dev->wait,
86 !(ps2dev->flags & PS2_FLAG_CMD),
87 msecs_to_jiffies(timeout));
Arjan van de Venc4e32e92006-02-19 00:21:55 -050088 mutex_unlock(&ps2dev->cmd_mutex);
Dmitry Torokhovc6117632005-06-01 02:39:51 -050089}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -040090EXPORT_SYMBOL(ps2_drain);
Dmitry Torokhovc6117632005-06-01 02:39:51 -050091
92/*
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -050093 * ps2_is_keyboard_id() checks received ID byte against the list of
94 * known keyboard IDs.
95 */
96
Dmitry Torokhov98078792006-09-14 01:31:27 -040097int ps2_is_keyboard_id(char id_byte)
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -050098{
Tobias Klauserc5a69d52007-02-17 20:11:19 +010099 static const char keyboard_ids[] = {
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -0500100 0xab, /* Regular keyboards */
101 0xac, /* NCD Sun keyboard */
102 0x2b, /* Trust keyboard, translated */
103 0x5d, /* Trust keyboard */
104 0x60, /* NMB SGI keyboard, translated */
105 0x47, /* NMB SGI keyboard */
106 };
107
108 return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
109}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400110EXPORT_SYMBOL(ps2_is_keyboard_id);
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -0500111
112/*
113 * ps2_adjust_timeout() is called after receiving 1st byte of command
114 * response and tries to reduce remaining timeout to speed up command
115 * completion.
116 */
117
118static int ps2_adjust_timeout(struct ps2dev *ps2dev, int command, int timeout)
119{
120 switch (command) {
121 case PS2_CMD_RESET_BAT:
122 /*
123 * Device has sent the first response byte after
124 * reset command, reset is thus done, so we can
125 * shorten the timeout.
126 * The next byte will come soon (keyboard) or not
127 * at all (mouse).
128 */
129 if (timeout > msecs_to_jiffies(100))
130 timeout = msecs_to_jiffies(100);
131 break;
132
133 case PS2_CMD_GETID:
134 /*
Dmitry Torokhov98078792006-09-14 01:31:27 -0400135 * Microsoft Natural Elite keyboard responds to
136 * the GET ID command as it were a mouse, with
137 * a single byte. Fail the command so atkbd will
138 * use alternative probe to detect it.
139 */
140 if (ps2dev->cmdbuf[1] == 0xaa) {
141 serio_pause_rx(ps2dev->serio);
142 ps2dev->flags = 0;
143 serio_continue_rx(ps2dev->serio);
144 timeout = 0;
145 }
146
147 /*
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -0500148 * If device behind the port is not a keyboard there
149 * won't be 2nd byte of ID response.
150 */
151 if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) {
152 serio_pause_rx(ps2dev->serio);
153 ps2dev->flags = ps2dev->cmdcnt = 0;
154 serio_continue_rx(ps2dev->serio);
155 timeout = 0;
156 }
157 break;
158
159 default:
160 break;
161 }
162
163 return timeout;
164}
165
166/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * ps2_command() sends a command and its parameters to the mouse,
168 * then waits for the response and puts it in the param array.
169 *
170 * ps2_command() can only be called from a process context
171 */
172
173int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
174{
175 int timeout;
176 int send = (command >> 12) & 0xf;
177 int receive = (command >> 8) & 0xf;
178 int rc = -1;
179 int i;
180
Dmitry Torokhovc6117632005-06-01 02:39:51 -0500181 if (receive > sizeof(ps2dev->cmdbuf)) {
182 WARN_ON(1);
183 return -1;
184 }
185
Dmitry Torokhov95349fe2006-07-06 23:54:48 -0400186 if (send && !param) {
187 WARN_ON(1);
188 return -1;
189 }
190
Jiri Kosina88aa0102006-10-11 01:45:31 -0400191 mutex_lock(&ps2dev->cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 serio_pause_rx(ps2dev->serio);
194 ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
195 ps2dev->cmdcnt = receive;
196 if (receive && param)
197 for (i = 0; i < receive; i++)
198 ps2dev->cmdbuf[(receive - 1) - i] = param[i];
199 serio_continue_rx(ps2dev->serio);
200
201 /*
202 * Some devices (Synaptics) peform the reset before
203 * ACKing the reset command, and so it can take a long
204 * time before the ACK arrrives.
205 */
Dmitry Torokhovc6117632005-06-01 02:39:51 -0500206 if (ps2_sendbyte(ps2dev, command & 0xff,
207 command == PS2_CMD_RESET_BAT ? 1000 : 200))
208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 for (i = 0; i < send; i++)
211 if (ps2_sendbyte(ps2dev, param[i], 200))
212 goto out;
213
214 /*
215 * The reset command takes a long time to execute.
216 */
217 timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
218
219 timeout = wait_event_timeout(ps2dev->wait,
220 !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
221
222 if (ps2dev->cmdcnt && timeout > 0) {
223
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -0500224 timeout = ps2_adjust_timeout(ps2dev, command, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 wait_event_timeout(ps2dev->wait,
226 !(ps2dev->flags & PS2_FLAG_CMD), timeout);
227 }
228
229 if (param)
230 for (i = 0; i < receive; i++)
231 param[i] = ps2dev->cmdbuf[(receive - 1) - i];
232
233 if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
234 goto out;
235
236 rc = 0;
237
Dmitry Torokhov905ab9d2005-06-01 02:39:53 -0500238 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 serio_pause_rx(ps2dev->serio);
240 ps2dev->flags = 0;
241 serio_continue_rx(ps2dev->serio);
242
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500243 mutex_unlock(&ps2dev->cmd_mutex);
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/*
249 * ps2_execute_scheduled_command() sends a command, previously scheduled by
250 * ps2_schedule_command(), to a PS/2 device (keyboard, mouse, etc.)
251 */
252
David Howells65f27f32006-11-22 14:55:48 +0000253static void ps2_execute_scheduled_command(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
David Howells65f27f32006-11-22 14:55:48 +0000255 struct ps2work *ps2work = container_of(work, struct ps2work, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 ps2_command(ps2work->ps2dev, ps2work->param, ps2work->command);
258 kfree(ps2work);
259}
260
261/*
262 * ps2_schedule_command() allows to schedule delayed execution of a PS/2
263 * command and can be used to issue a command from an interrupt or softirq
264 * context.
265 */
266
267int ps2_schedule_command(struct ps2dev *ps2dev, unsigned char *param, int command)
268{
269 struct ps2work *ps2work;
270 int send = (command >> 12) & 0xf;
271 int receive = (command >> 8) & 0xf;
272
273 if (!(ps2work = kmalloc(sizeof(struct ps2work) + max(send, receive), GFP_ATOMIC)))
274 return -1;
275
276 memset(ps2work, 0, sizeof(struct ps2work));
277 ps2work->ps2dev = ps2dev;
278 ps2work->command = command;
279 memcpy(ps2work->param, param, send);
David Howells65f27f32006-11-22 14:55:48 +0000280 INIT_WORK(&ps2work->work, ps2_execute_scheduled_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (!schedule_work(&ps2work->work)) {
283 kfree(ps2work);
284 return -1;
285 }
286
287 return 0;
288}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400289EXPORT_SYMBOL(ps2_schedule_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/*
292 * ps2_init() initializes ps2dev structure
293 */
294
295void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
296{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500297 mutex_init(&ps2dev->cmd_mutex);
Jiri Kosina88aa0102006-10-11 01:45:31 -0400298 lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 init_waitqueue_head(&ps2dev->wait);
300 ps2dev->serio = serio;
301}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400302EXPORT_SYMBOL(ps2_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304/*
305 * ps2_handle_ack() is supposed to be used in interrupt handler
306 * to properly process ACK/NAK of a command from a PS/2 device.
307 */
308
309int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
310{
311 switch (data) {
312 case PS2_RET_ACK:
313 ps2dev->nak = 0;
314 break;
315
316 case PS2_RET_NAK:
317 ps2dev->nak = 1;
318 break;
319
320 /*
321 * Workaround for mice which don't ACK the Get ID command.
322 * These are valid mouse IDs that we recognize.
323 */
324 case 0x00:
325 case 0x03:
326 case 0x04:
327 if (ps2dev->flags & PS2_FLAG_WAITID) {
328 ps2dev->nak = 0;
329 break;
330 }
331 /* Fall through */
332 default:
333 return 0;
334 }
335
336
337 if (!ps2dev->nak && ps2dev->cmdcnt)
338 ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
339
340 ps2dev->flags &= ~PS2_FLAG_ACK;
341 wake_up(&ps2dev->wait);
342
343 if (data != PS2_RET_ACK)
344 ps2_handle_response(ps2dev, data);
345
346 return 1;
347}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400348EXPORT_SYMBOL(ps2_handle_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350/*
351 * ps2_handle_response() is supposed to be used in interrupt handler
352 * to properly store device's response to a command and notify process
353 * waiting for completion of the command.
354 */
355
356int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
357{
358 if (ps2dev->cmdcnt)
359 ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
360
361 if (ps2dev->flags & PS2_FLAG_CMD1) {
362 ps2dev->flags &= ~PS2_FLAG_CMD1;
363 if (ps2dev->cmdcnt)
364 wake_up(&ps2dev->wait);
365 }
366
367 if (!ps2dev->cmdcnt) {
368 ps2dev->flags &= ~PS2_FLAG_CMD;
369 wake_up(&ps2dev->wait);
370 }
371
372 return 1;
373}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400374EXPORT_SYMBOL(ps2_handle_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376void ps2_cmd_aborted(struct ps2dev *ps2dev)
377{
378 if (ps2dev->flags & PS2_FLAG_ACK)
379 ps2dev->nak = 1;
380
381 if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
382 wake_up(&ps2dev->wait);
383
384 ps2dev->flags = 0;
385}
Dmitry Torokhov5206c0d2006-09-14 01:31:40 -0400386EXPORT_SYMBOL(ps2_cmd_aborted);