Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / input / serio / libps2.c
blobb819239d74dc15f0c7fa41fce8a8c9658b33fc1a
1 /*
2 * PS/2 driver library
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2004 Dmitry Torokhov
6 */
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.
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");
29 /* Work structure to schedule execution of a command */
30 struct ps2work {
31 struct work_struct work;
32 struct ps2dev *ps2dev;
33 int command;
34 unsigned char param[0];
39 * 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.
43 * ps2_sendbyte() can only be called from a process context.
46 int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout)
48 serio_pause_rx(ps2dev->serio);
49 ps2dev->nak = 1;
50 ps2dev->flags |= PS2_FLAG_ACK;
51 serio_continue_rx(ps2dev->serio);
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));
58 serio_pause_rx(ps2dev->serio);
59 ps2dev->flags &= ~PS2_FLAG_ACK;
60 serio_continue_rx(ps2dev->serio);
62 return -ps2dev->nak;
64 EXPORT_SYMBOL(ps2_sendbyte);
67 * ps2_drain() waits for device to transmit requested number of bytes
68 * and discards them.
71 void ps2_drain(struct ps2dev *ps2dev, int maxbytes, int timeout)
73 if (maxbytes > sizeof(ps2dev->cmdbuf)) {
74 WARN_ON(1);
75 maxbytes = sizeof(ps2dev->cmdbuf);
78 mutex_lock(&ps2dev->cmd_mutex);
80 serio_pause_rx(ps2dev->serio);
81 ps2dev->flags = PS2_FLAG_CMD;
82 ps2dev->cmdcnt = maxbytes;
83 serio_continue_rx(ps2dev->serio);
85 wait_event_timeout(ps2dev->wait,
86 !(ps2dev->flags & PS2_FLAG_CMD),
87 msecs_to_jiffies(timeout));
88 mutex_unlock(&ps2dev->cmd_mutex);
90 EXPORT_SYMBOL(ps2_drain);
93 * ps2_is_keyboard_id() checks received ID byte against the list of
94 * known keyboard IDs.
97 int ps2_is_keyboard_id(char id_byte)
99 static const char keyboard_ids[] = {
100 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 */
108 return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
110 EXPORT_SYMBOL(ps2_is_keyboard_id);
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.
118 static int ps2_adjust_timeout(struct ps2dev *ps2dev, int command, int timeout)
120 switch (command) {
121 case PS2_CMD_RESET_BAT:
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).
129 if (timeout > msecs_to_jiffies(100))
130 timeout = msecs_to_jiffies(100);
131 break;
133 case PS2_CMD_GETID:
135 * 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.
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;
148 * If device behind the port is not a keyboard there
149 * won't be 2nd byte of ID response.
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;
157 break;
159 default:
160 break;
163 return timeout;
167 * ps2_command() sends a command and its parameters to the mouse,
168 * then waits for the response and puts it in the param array.
170 * ps2_command() can only be called from a process context
173 int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
175 int timeout;
176 int send = (command >> 12) & 0xf;
177 int receive = (command >> 8) & 0xf;
178 int rc = -1;
179 int i;
181 if (receive > sizeof(ps2dev->cmdbuf)) {
182 WARN_ON(1);
183 return -1;
186 if (send && !param) {
187 WARN_ON(1);
188 return -1;
191 mutex_lock(&ps2dev->cmd_mutex);
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);
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.
206 if (ps2_sendbyte(ps2dev, command & 0xff,
207 command == PS2_CMD_RESET_BAT ? 1000 : 200))
208 goto out;
210 for (i = 0; i < send; i++)
211 if (ps2_sendbyte(ps2dev, param[i], 200))
212 goto out;
215 * The reset command takes a long time to execute.
217 timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
219 timeout = wait_event_timeout(ps2dev->wait,
220 !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
222 if (ps2dev->cmdcnt && timeout > 0) {
224 timeout = ps2_adjust_timeout(ps2dev, command, timeout);
225 wait_event_timeout(ps2dev->wait,
226 !(ps2dev->flags & PS2_FLAG_CMD), timeout);
229 if (param)
230 for (i = 0; i < receive; i++)
231 param[i] = ps2dev->cmdbuf[(receive - 1) - i];
233 if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
234 goto out;
236 rc = 0;
238 out:
239 serio_pause_rx(ps2dev->serio);
240 ps2dev->flags = 0;
241 serio_continue_rx(ps2dev->serio);
243 mutex_unlock(&ps2dev->cmd_mutex);
244 return rc;
246 EXPORT_SYMBOL(ps2_command);
249 * ps2_execute_scheduled_command() sends a command, previously scheduled by
250 * ps2_schedule_command(), to a PS/2 device (keyboard, mouse, etc.)
253 static void ps2_execute_scheduled_command(struct work_struct *work)
255 struct ps2work *ps2work = container_of(work, struct ps2work, work);
257 ps2_command(ps2work->ps2dev, ps2work->param, ps2work->command);
258 kfree(ps2work);
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.
267 int ps2_schedule_command(struct ps2dev *ps2dev, unsigned char *param, int command)
269 struct ps2work *ps2work;
270 int send = (command >> 12) & 0xf;
271 int receive = (command >> 8) & 0xf;
273 if (!(ps2work = kmalloc(sizeof(struct ps2work) + max(send, receive), GFP_ATOMIC)))
274 return -1;
276 memset(ps2work, 0, sizeof(struct ps2work));
277 ps2work->ps2dev = ps2dev;
278 ps2work->command = command;
279 memcpy(ps2work->param, param, send);
280 INIT_WORK(&ps2work->work, ps2_execute_scheduled_command);
282 if (!schedule_work(&ps2work->work)) {
283 kfree(ps2work);
284 return -1;
287 return 0;
289 EXPORT_SYMBOL(ps2_schedule_command);
292 * ps2_init() initializes ps2dev structure
295 void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
297 mutex_init(&ps2dev->cmd_mutex);
298 lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth);
299 init_waitqueue_head(&ps2dev->wait);
300 ps2dev->serio = serio;
302 EXPORT_SYMBOL(ps2_init);
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.
309 int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
311 switch (data) {
312 case PS2_RET_ACK:
313 ps2dev->nak = 0;
314 break;
316 case PS2_RET_NAK:
317 ps2dev->nak = 1;
318 break;
321 * Workaround for mice which don't ACK the Get ID command.
322 * These are valid mouse IDs that we recognize.
324 case 0x00:
325 case 0x03:
326 case 0x04:
327 if (ps2dev->flags & PS2_FLAG_WAITID) {
328 ps2dev->nak = 0;
329 break;
331 /* Fall through */
332 default:
333 return 0;
337 if (!ps2dev->nak && ps2dev->cmdcnt)
338 ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
340 ps2dev->flags &= ~PS2_FLAG_ACK;
341 wake_up(&ps2dev->wait);
343 if (data != PS2_RET_ACK)
344 ps2_handle_response(ps2dev, data);
346 return 1;
348 EXPORT_SYMBOL(ps2_handle_ack);
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.
356 int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
358 if (ps2dev->cmdcnt)
359 ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
361 if (ps2dev->flags & PS2_FLAG_CMD1) {
362 ps2dev->flags &= ~PS2_FLAG_CMD1;
363 if (ps2dev->cmdcnt)
364 wake_up(&ps2dev->wait);
367 if (!ps2dev->cmdcnt) {
368 ps2dev->flags &= ~PS2_FLAG_CMD;
369 wake_up(&ps2dev->wait);
372 return 1;
374 EXPORT_SYMBOL(ps2_handle_response);
376 void ps2_cmd_aborted(struct ps2dev *ps2dev)
378 if (ps2dev->flags & PS2_FLAG_ACK)
379 ps2dev->nak = 1;
381 if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
382 wake_up(&ps2dev->wait);
384 ps2dev->flags = 0;
386 EXPORT_SYMBOL(ps2_cmd_aborted);