RDMA/ucma: Check that device exists prior to accessing it
[pandora-kernel.git] / drivers / input / serio / libps2.c
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/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>
23
24 #define DRIVER_DESC     "PS/2 driver library"
25
26 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
27 MODULE_DESCRIPTION("PS/2 driver library");
28 MODULE_LICENSE("GPL");
29
30 /*
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.
34  *
35  * ps2_sendbyte() can only be called from a process context.
36  */
37
38 int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout)
39 {
40         serio_pause_rx(ps2dev->serio);
41         ps2dev->nak = 1;
42         ps2dev->flags |= PS2_FLAG_ACK;
43         serio_continue_rx(ps2dev->serio);
44
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));
49
50         serio_pause_rx(ps2dev->serio);
51         ps2dev->flags &= ~PS2_FLAG_ACK;
52         serio_continue_rx(ps2dev->serio);
53
54         return -ps2dev->nak;
55 }
56 EXPORT_SYMBOL(ps2_sendbyte);
57
58 void ps2_begin_command(struct ps2dev *ps2dev)
59 {
60         struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex;
61
62         mutex_lock(m);
63 }
64 EXPORT_SYMBOL(ps2_begin_command);
65
66 void ps2_end_command(struct ps2dev *ps2dev)
67 {
68         struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex;
69
70         mutex_unlock(m);
71 }
72 EXPORT_SYMBOL(ps2_end_command);
73
74 /*
75  * ps2_drain() waits for device to transmit requested number of bytes
76  * and discards them.
77  */
78
79 void ps2_drain(struct ps2dev *ps2dev, int maxbytes, int timeout)
80 {
81         if (maxbytes > sizeof(ps2dev->cmdbuf)) {
82                 WARN_ON(1);
83                 maxbytes = sizeof(ps2dev->cmdbuf);
84         }
85
86         ps2_begin_command(ps2dev);
87
88         serio_pause_rx(ps2dev->serio);
89         ps2dev->flags = PS2_FLAG_CMD;
90         ps2dev->cmdcnt = maxbytes;
91         serio_continue_rx(ps2dev->serio);
92
93         wait_event_timeout(ps2dev->wait,
94                            !(ps2dev->flags & PS2_FLAG_CMD),
95                            msecs_to_jiffies(timeout));
96
97         ps2_end_command(ps2dev);
98 }
99 EXPORT_SYMBOL(ps2_drain);
100
101 /*
102  * ps2_is_keyboard_id() checks received ID byte against the list of
103  * known keyboard IDs.
104  */
105
106 int ps2_is_keyboard_id(char id_byte)
107 {
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             */
115         };
116
117         return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
118 }
119 EXPORT_SYMBOL(ps2_is_keyboard_id);
120
121 /*
122  * ps2_adjust_timeout() is called after receiving 1st byte of command
123  * response and tries to reduce remaining timeout to speed up command
124  * completion.
125  */
126
127 static int ps2_adjust_timeout(struct ps2dev *ps2dev, int command, int timeout)
128 {
129         switch (command) {
130                 case PS2_CMD_RESET_BAT:
131                         /*
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
136                          * at all (mouse).
137                          */
138                         if (timeout > msecs_to_jiffies(100))
139                                 timeout = msecs_to_jiffies(100);
140                         break;
141
142                 case PS2_CMD_GETID:
143                         /*
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.
148                          */
149                         if (ps2dev->cmdbuf[1] == 0xaa) {
150                                 serio_pause_rx(ps2dev->serio);
151                                 ps2dev->flags = 0;
152                                 serio_continue_rx(ps2dev->serio);
153                                 timeout = 0;
154                         }
155
156                         /*
157                          * If device behind the port is not a keyboard there
158                          * won't be 2nd byte of ID response.
159                          */
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);
164                                 timeout = 0;
165                         }
166                         break;
167
168                 default:
169                         break;
170         }
171
172         return timeout;
173 }
174
175 /*
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.
178  *
179  * ps2_command() can only be called from a process context
180  */
181
182 int __ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
183 {
184         int timeout;
185         int send = (command >> 12) & 0xf;
186         int receive = (command >> 8) & 0xf;
187         int rc = -1;
188         int i;
189
190         if (receive > sizeof(ps2dev->cmdbuf)) {
191                 WARN_ON(1);
192                 return -1;
193         }
194
195         if (send && !param) {
196                 WARN_ON(1);
197                 return -1;
198         }
199
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);
207
208         /*
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.
212          */
213         if (ps2_sendbyte(ps2dev, command & 0xff,
214                          command == PS2_CMD_RESET_BAT ? 1000 : 200))
215                 goto out;
216
217         for (i = 0; i < send; i++)
218                 if (ps2_sendbyte(ps2dev, param[i], 200))
219                         goto out;
220
221         /*
222          * The reset command takes a long time to execute.
223          */
224         timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
225
226         timeout = wait_event_timeout(ps2dev->wait,
227                                      !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
228
229         if (ps2dev->cmdcnt && !(ps2dev->flags & PS2_FLAG_CMD1)) {
230
231                 timeout = ps2_adjust_timeout(ps2dev, command, timeout);
232                 wait_event_timeout(ps2dev->wait,
233                                    !(ps2dev->flags & PS2_FLAG_CMD), timeout);
234         }
235
236         if (param)
237                 for (i = 0; i < receive; i++)
238                         param[i] = ps2dev->cmdbuf[(receive - 1) - i];
239
240         if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
241                 goto out;
242
243         rc = 0;
244
245  out:
246         serio_pause_rx(ps2dev->serio);
247         ps2dev->flags = 0;
248         serio_continue_rx(ps2dev->serio);
249
250         return rc;
251 }
252 EXPORT_SYMBOL(__ps2_command);
253
254 int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
255 {
256         int rc;
257
258         ps2_begin_command(ps2dev);
259         rc = __ps2_command(ps2dev, param, command);
260         ps2_end_command(ps2dev);
261
262         return rc;
263 }
264 EXPORT_SYMBOL(ps2_command);
265
266 /*
267  * ps2_init() initializes ps2dev structure
268  */
269
270 void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
271 {
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;
276 }
277 EXPORT_SYMBOL(ps2_init);
278
279 /*
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.
282  */
283
284 int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
285 {
286         switch (data) {
287                 case PS2_RET_ACK:
288                         ps2dev->nak = 0;
289                         break;
290
291                 case PS2_RET_NAK:
292                         ps2dev->flags |= PS2_FLAG_NAK;
293                         ps2dev->nak = PS2_RET_NAK;
294                         break;
295
296                 case PS2_RET_ERR:
297                         if (ps2dev->flags & PS2_FLAG_NAK) {
298                                 ps2dev->flags &= ~PS2_FLAG_NAK;
299                                 ps2dev->nak = PS2_RET_ERR;
300                                 break;
301                         }
302
303                 /*
304                  * Workaround for mice which don't ACK the Get ID command.
305                  * These are valid mouse IDs that we recognize.
306                  */
307                 case 0x00:
308                 case 0x03:
309                 case 0x04:
310                         if (ps2dev->flags & PS2_FLAG_WAITID) {
311                                 ps2dev->nak = 0;
312                                 break;
313                         }
314                         /* Fall through */
315                 default:
316                         return 0;
317         }
318
319
320         if (!ps2dev->nak) {
321                 ps2dev->flags &= ~PS2_FLAG_NAK;
322                 if (ps2dev->cmdcnt)
323                         ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
324         }
325
326         ps2dev->flags &= ~PS2_FLAG_ACK;
327         wake_up(&ps2dev->wait);
328
329         if (data != PS2_RET_ACK)
330                 ps2_handle_response(ps2dev, data);
331
332         return 1;
333 }
334 EXPORT_SYMBOL(ps2_handle_ack);
335
336 /*
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.
340  */
341
342 int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
343 {
344         if (ps2dev->cmdcnt)
345                 ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
346
347         if (ps2dev->flags & PS2_FLAG_CMD1) {
348                 ps2dev->flags &= ~PS2_FLAG_CMD1;
349                 if (ps2dev->cmdcnt)
350                         wake_up(&ps2dev->wait);
351         }
352
353         if (!ps2dev->cmdcnt) {
354                 ps2dev->flags &= ~PS2_FLAG_CMD;
355                 wake_up(&ps2dev->wait);
356         }
357
358         return 1;
359 }
360 EXPORT_SYMBOL(ps2_handle_response);
361
362 void ps2_cmd_aborted(struct ps2dev *ps2dev)
363 {
364         if (ps2dev->flags & PS2_FLAG_ACK)
365                 ps2dev->nak = 1;
366
367         if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
368                 wake_up(&ps2dev->wait);
369
370         /* reset all flags except last nack */
371         ps2dev->flags &= PS2_FLAG_NAK;
372 }
373 EXPORT_SYMBOL(ps2_cmd_aborted);