]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/input/serio/libps2.c
x86: save/restore eflags in context switch
[linux-2.6.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/moduleparam.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/input.h>
20 #include <linux/serio.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 EXPORT_SYMBOL(ps2_init);
31 EXPORT_SYMBOL(ps2_sendbyte);
32 EXPORT_SYMBOL(ps2_drain);
33 EXPORT_SYMBOL(ps2_command);
34 EXPORT_SYMBOL(ps2_schedule_command);
35 EXPORT_SYMBOL(ps2_handle_ack);
36 EXPORT_SYMBOL(ps2_handle_response);
37 EXPORT_SYMBOL(ps2_cmd_aborted);
38
39 /* Work structure to schedule execution of a command */
40 struct ps2work {
41         struct work_struct work;
42         struct ps2dev *ps2dev;
43         int command;
44         unsigned char param[0];
45 };
46
47
48 /*
49  * ps2_sendbyte() sends a byte to the device and waits for acknowledge.
50  * It doesn't handle retransmission, though it could - because if there
51  * is a need for retransmissions device has to be replaced anyway.
52  *
53  * ps2_sendbyte() can only be called from a process context.
54  */
55
56 int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout)
57 {
58         serio_pause_rx(ps2dev->serio);
59         ps2dev->nak = 1;
60         ps2dev->flags |= PS2_FLAG_ACK;
61         serio_continue_rx(ps2dev->serio);
62
63         if (serio_write(ps2dev->serio, byte) == 0)
64                 wait_event_timeout(ps2dev->wait,
65                                    !(ps2dev->flags & PS2_FLAG_ACK),
66                                    msecs_to_jiffies(timeout));
67
68         serio_pause_rx(ps2dev->serio);
69         ps2dev->flags &= ~PS2_FLAG_ACK;
70         serio_continue_rx(ps2dev->serio);
71
72         return -ps2dev->nak;
73 }
74
75 /*
76  * ps2_drain() waits for device to transmit requested number of bytes
77  * and discards them.
78  */
79
80 void ps2_drain(struct ps2dev *ps2dev, int maxbytes, int timeout)
81 {
82         if (maxbytes > sizeof(ps2dev->cmdbuf)) {
83                 WARN_ON(1);
84                 maxbytes = sizeof(ps2dev->cmdbuf);
85         }
86
87         mutex_lock(&ps2dev->cmd_mutex);
88
89         serio_pause_rx(ps2dev->serio);
90         ps2dev->flags = PS2_FLAG_CMD;
91         ps2dev->cmdcnt = maxbytes;
92         serio_continue_rx(ps2dev->serio);
93
94         wait_event_timeout(ps2dev->wait,
95                            !(ps2dev->flags & PS2_FLAG_CMD),
96                            msecs_to_jiffies(timeout));
97         mutex_unlock(&ps2dev->cmd_mutex);
98 }
99
100 /*
101  * ps2_is_keyboard_id() checks received ID byte against the list of
102  * known keyboard IDs.
103  */
104
105 static inline int ps2_is_keyboard_id(char id_byte)
106 {
107         static char keyboard_ids[] = {
108                 0xab,   /* Regular keyboards            */
109                 0xac,   /* NCD Sun keyboard             */
110                 0x2b,   /* Trust keyboard, translated   */
111                 0x5d,   /* Trust keyboard               */
112                 0x60,   /* NMB SGI keyboard, translated */
113                 0x47,   /* NMB SGI keyboard             */
114         };
115
116         return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
117 }
118
119 /*
120  * ps2_adjust_timeout() is called after receiving 1st byte of command
121  * response and tries to reduce remaining timeout to speed up command
122  * completion.
123  */
124
125 static int ps2_adjust_timeout(struct ps2dev *ps2dev, int command, int timeout)
126 {
127         switch (command) {
128                 case PS2_CMD_RESET_BAT:
129                         /*
130                          * Device has sent the first response byte after
131                          * reset command, reset is thus done, so we can
132                          * shorten the timeout.
133                          * The next byte will come soon (keyboard) or not
134                          * at all (mouse).
135                          */
136                         if (timeout > msecs_to_jiffies(100))
137                                 timeout = msecs_to_jiffies(100);
138                         break;
139
140                 case PS2_CMD_GETID:
141                         /*
142                          * If device behind the port is not a keyboard there
143                          * won't be 2nd byte of ID response.
144                          */
145                         if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) {
146                                 serio_pause_rx(ps2dev->serio);
147                                 ps2dev->flags = ps2dev->cmdcnt = 0;
148                                 serio_continue_rx(ps2dev->serio);
149                                 timeout = 0;
150                         }
151                         break;
152
153                 default:
154                         break;
155         }
156
157         return timeout;
158 }
159
160 /*
161  * ps2_command() sends a command and its parameters to the mouse,
162  * then waits for the response and puts it in the param array.
163  *
164  * ps2_command() can only be called from a process context
165  */
166
167 int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
168 {
169         int timeout;
170         int send = (command >> 12) & 0xf;
171         int receive = (command >> 8) & 0xf;
172         int rc = -1;
173         int i;
174
175         if (receive > sizeof(ps2dev->cmdbuf)) {
176                 WARN_ON(1);
177                 return -1;
178         }
179
180         if (send && !param) {
181                 WARN_ON(1);
182                 return -1;
183         }
184
185         mutex_lock_nested(&ps2dev->cmd_mutex, SINGLE_DEPTH_NESTING);
186
187         serio_pause_rx(ps2dev->serio);
188         ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
189         ps2dev->cmdcnt = receive;
190         if (receive && param)
191                 for (i = 0; i < receive; i++)
192                         ps2dev->cmdbuf[(receive - 1) - i] = param[i];
193         serio_continue_rx(ps2dev->serio);
194
195         /*
196          * Some devices (Synaptics) peform the reset before
197          * ACKing the reset command, and so it can take a long
198          * time before the ACK arrrives.
199          */
200         if (ps2_sendbyte(ps2dev, command & 0xff,
201                          command == PS2_CMD_RESET_BAT ? 1000 : 200))
202                 goto out;
203
204         for (i = 0; i < send; i++)
205                 if (ps2_sendbyte(ps2dev, param[i], 200))
206                         goto out;
207
208         /*
209          * The reset command takes a long time to execute.
210          */
211         timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
212
213         timeout = wait_event_timeout(ps2dev->wait,
214                                      !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
215
216         if (ps2dev->cmdcnt && timeout > 0) {
217
218                 timeout = ps2_adjust_timeout(ps2dev, command, timeout);
219                 wait_event_timeout(ps2dev->wait,
220                                    !(ps2dev->flags & PS2_FLAG_CMD), timeout);
221         }
222
223         if (param)
224                 for (i = 0; i < receive; i++)
225                         param[i] = ps2dev->cmdbuf[(receive - 1) - i];
226
227         if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
228                 goto out;
229
230         rc = 0;
231
232  out:
233         serio_pause_rx(ps2dev->serio);
234         ps2dev->flags = 0;
235         serio_continue_rx(ps2dev->serio);
236
237         mutex_unlock(&ps2dev->cmd_mutex);
238         return rc;
239 }
240
241 /*
242  * ps2_execute_scheduled_command() sends a command, previously scheduled by
243  * ps2_schedule_command(), to a PS/2 device (keyboard, mouse, etc.)
244  */
245
246 static void ps2_execute_scheduled_command(void *data)
247 {
248         struct ps2work *ps2work = data;
249
250         ps2_command(ps2work->ps2dev, ps2work->param, ps2work->command);
251         kfree(ps2work);
252 }
253
254 /*
255  * ps2_schedule_command() allows to schedule delayed execution of a PS/2
256  * command and can be used to issue a command from an interrupt or softirq
257  * context.
258  */
259
260 int ps2_schedule_command(struct ps2dev *ps2dev, unsigned char *param, int command)
261 {
262         struct ps2work *ps2work;
263         int send = (command >> 12) & 0xf;
264         int receive = (command >> 8) & 0xf;
265
266         if (!(ps2work = kmalloc(sizeof(struct ps2work) + max(send, receive), GFP_ATOMIC)))
267                 return -1;
268
269         memset(ps2work, 0, sizeof(struct ps2work));
270         ps2work->ps2dev = ps2dev;
271         ps2work->command = command;
272         memcpy(ps2work->param, param, send);
273         INIT_WORK(&ps2work->work, ps2_execute_scheduled_command, ps2work);
274
275         if (!schedule_work(&ps2work->work)) {
276                 kfree(ps2work);
277                 return -1;
278         }
279
280         return 0;
281 }
282
283 /*
284  * ps2_init() initializes ps2dev structure
285  */
286
287 void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
288 {
289         mutex_init(&ps2dev->cmd_mutex);
290         init_waitqueue_head(&ps2dev->wait);
291         ps2dev->serio = serio;
292 }
293
294 /*
295  * ps2_handle_ack() is supposed to be used in interrupt handler
296  * to properly process ACK/NAK of a command from a PS/2 device.
297  */
298
299 int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
300 {
301         switch (data) {
302                 case PS2_RET_ACK:
303                         ps2dev->nak = 0;
304                         break;
305
306                 case PS2_RET_NAK:
307                         ps2dev->nak = 1;
308                         break;
309
310                 /*
311                  * Workaround for mice which don't ACK the Get ID command.
312                  * These are valid mouse IDs that we recognize.
313                  */
314                 case 0x00:
315                 case 0x03:
316                 case 0x04:
317                         if (ps2dev->flags & PS2_FLAG_WAITID) {
318                                 ps2dev->nak = 0;
319                                 break;
320                         }
321                         /* Fall through */
322                 default:
323                         return 0;
324         }
325
326
327         if (!ps2dev->nak && ps2dev->cmdcnt)
328                 ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
329
330         ps2dev->flags &= ~PS2_FLAG_ACK;
331         wake_up(&ps2dev->wait);
332
333         if (data != PS2_RET_ACK)
334                 ps2_handle_response(ps2dev, data);
335
336         return 1;
337 }
338
339 /*
340  * ps2_handle_response() is supposed to be used in interrupt handler
341  * to properly store device's response to a command and notify process
342  * waiting for completion of the command.
343  */
344
345 int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
346 {
347         if (ps2dev->cmdcnt)
348                 ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
349
350         if (ps2dev->flags & PS2_FLAG_CMD1) {
351                 ps2dev->flags &= ~PS2_FLAG_CMD1;
352                 if (ps2dev->cmdcnt)
353                         wake_up(&ps2dev->wait);
354         }
355
356         if (!ps2dev->cmdcnt) {
357                 ps2dev->flags &= ~PS2_FLAG_CMD;
358                 wake_up(&ps2dev->wait);
359         }
360
361         return 1;
362 }
363
364 void ps2_cmd_aborted(struct ps2dev *ps2dev)
365 {
366         if (ps2dev->flags & PS2_FLAG_ACK)
367                 ps2dev->nak = 1;
368
369         if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
370                 wake_up(&ps2dev->wait);
371
372         ps2dev->flags = 0;
373 }
374