xref: /openbmc/linux/drivers/input/serio/libps2.c (revision f42b3800)
1 /*
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>
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>
22 
23 #define DRIVER_DESC	"PS/2 driver library"
24 
25 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
26 MODULE_DESCRIPTION("PS/2 driver library");
27 MODULE_LICENSE("GPL");
28 
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];
35 };
36 
37 
38 /*
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.
42  *
43  * ps2_sendbyte() can only be called from a process context.
44  */
45 
46 int 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 }
64 EXPORT_SYMBOL(ps2_sendbyte);
65 
66 /*
67  * ps2_drain() waits for device to transmit requested number of bytes
68  * and discards them.
69  */
70 
71 void 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 
78 	mutex_lock(&ps2dev->cmd_mutex);
79 
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));
88 	mutex_unlock(&ps2dev->cmd_mutex);
89 }
90 EXPORT_SYMBOL(ps2_drain);
91 
92 /*
93  * ps2_is_keyboard_id() checks received ID byte against the list of
94  * known keyboard IDs.
95  */
96 
97 int ps2_is_keyboard_id(char id_byte)
98 {
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		*/
106 	};
107 
108 	return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
109 }
110 EXPORT_SYMBOL(ps2_is_keyboard_id);
111 
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 
118 static 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 			/*
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.
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 			/*
148 			 * 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 /*
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.
169  *
170  * ps2_command() can only be called from a process context
171  */
172 
173 int 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 
181 	if (receive > sizeof(ps2dev->cmdbuf)) {
182 		WARN_ON(1);
183 		return -1;
184 	}
185 
186 	if (send && !param) {
187 		WARN_ON(1);
188 		return -1;
189 	}
190 
191 	mutex_lock(&ps2dev->cmd_mutex);
192 
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 	 */
206 	if (ps2_sendbyte(ps2dev, command & 0xff,
207 			 command == PS2_CMD_RESET_BAT ? 1000 : 200))
208 		goto out;
209 
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 
224 		timeout = ps2_adjust_timeout(ps2dev, command, timeout);
225 		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 
238  out:
239 	serio_pause_rx(ps2dev->serio);
240 	ps2dev->flags = 0;
241 	serio_continue_rx(ps2dev->serio);
242 
243 	mutex_unlock(&ps2dev->cmd_mutex);
244 	return rc;
245 }
246 EXPORT_SYMBOL(ps2_command);
247 
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 
253 static void ps2_execute_scheduled_command(struct work_struct *work)
254 {
255 	struct ps2work *ps2work = container_of(work, struct ps2work, work);
256 
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 
267 int 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);
280 	INIT_WORK(&ps2work->work, ps2_execute_scheduled_command);
281 
282 	if (!schedule_work(&ps2work->work)) {
283 		kfree(ps2work);
284 		return -1;
285 	}
286 
287 	return 0;
288 }
289 EXPORT_SYMBOL(ps2_schedule_command);
290 
291 /*
292  * ps2_init() initializes ps2dev structure
293  */
294 
295 void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
296 {
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;
301 }
302 EXPORT_SYMBOL(ps2_init);
303 
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 
309 int 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 }
348 EXPORT_SYMBOL(ps2_handle_ack);
349 
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 
356 int 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 }
374 EXPORT_SYMBOL(ps2_handle_response);
375 
376 void 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 }
386 EXPORT_SYMBOL(ps2_cmd_aborted);
387