]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
d43c36dc | 16 | #include <linux/sched.h> |
1da177e4 LT |
17 | #include <linux/interrupt.h> |
18 | #include <linux/input.h> | |
19 | #include <linux/serio.h> | |
181d683d | 20 | #include <linux/i8042.h> |
1da177e4 LT |
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 | ||
1da177e4 | 29 | /* |
c611763d DT |
30 | * ps2_sendbyte() sends a byte to the device and waits for acknowledge. |
31 | * It doesn't handle retransmission, though it could - because if there | |
32 | * is a need for retransmissions device has to be replaced anyway. | |
1da177e4 | 33 | * |
c611763d | 34 | * ps2_sendbyte() can only be called from a process context. |
1da177e4 LT |
35 | */ |
36 | ||
b28bad65 | 37 | int ps2_sendbyte(struct ps2dev *ps2dev, u8 byte, unsigned int timeout) |
1da177e4 LT |
38 | { |
39 | serio_pause_rx(ps2dev->serio); | |
40 | ps2dev->nak = 1; | |
41 | ps2dev->flags |= PS2_FLAG_ACK; | |
42 | serio_continue_rx(ps2dev->serio); | |
43 | ||
44 | if (serio_write(ps2dev->serio, byte) == 0) | |
45 | wait_event_timeout(ps2dev->wait, | |
46 | !(ps2dev->flags & PS2_FLAG_ACK), | |
47 | msecs_to_jiffies(timeout)); | |
48 | ||
49 | serio_pause_rx(ps2dev->serio); | |
50 | ps2dev->flags &= ~PS2_FLAG_ACK; | |
51 | serio_continue_rx(ps2dev->serio); | |
52 | ||
53 | return -ps2dev->nak; | |
54 | } | |
5206c0d5 | 55 | EXPORT_SYMBOL(ps2_sendbyte); |
1da177e4 | 56 | |
181d683d DT |
57 | void ps2_begin_command(struct ps2dev *ps2dev) |
58 | { | |
40974618 | 59 | struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex; |
181d683d | 60 | |
40974618 | 61 | mutex_lock(m); |
181d683d DT |
62 | } |
63 | EXPORT_SYMBOL(ps2_begin_command); | |
64 | ||
65 | void ps2_end_command(struct ps2dev *ps2dev) | |
66 | { | |
40974618 | 67 | struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex; |
181d683d | 68 | |
40974618 | 69 | mutex_unlock(m); |
181d683d DT |
70 | } |
71 | EXPORT_SYMBOL(ps2_end_command); | |
72 | ||
c611763d DT |
73 | /* |
74 | * ps2_drain() waits for device to transmit requested number of bytes | |
75 | * and discards them. | |
76 | */ | |
77 | ||
b28bad65 | 78 | void ps2_drain(struct ps2dev *ps2dev, size_t maxbytes, unsigned int timeout) |
c611763d DT |
79 | { |
80 | if (maxbytes > sizeof(ps2dev->cmdbuf)) { | |
81 | WARN_ON(1); | |
82 | maxbytes = sizeof(ps2dev->cmdbuf); | |
83 | } | |
84 | ||
181d683d | 85 | ps2_begin_command(ps2dev); |
c611763d DT |
86 | |
87 | serio_pause_rx(ps2dev->serio); | |
88 | ps2dev->flags = PS2_FLAG_CMD; | |
89 | ps2dev->cmdcnt = maxbytes; | |
90 | serio_continue_rx(ps2dev->serio); | |
91 | ||
92 | wait_event_timeout(ps2dev->wait, | |
93 | !(ps2dev->flags & PS2_FLAG_CMD), | |
94 | msecs_to_jiffies(timeout)); | |
181d683d DT |
95 | |
96 | ps2_end_command(ps2dev); | |
c611763d | 97 | } |
5206c0d5 | 98 | EXPORT_SYMBOL(ps2_drain); |
c611763d | 99 | |
905ab9d1 DT |
100 | /* |
101 | * ps2_is_keyboard_id() checks received ID byte against the list of | |
102 | * known keyboard IDs. | |
103 | */ | |
104 | ||
b28bad65 | 105 | bool ps2_is_keyboard_id(u8 id_byte) |
905ab9d1 | 106 | { |
b28bad65 | 107 | static const u8 keyboard_ids[] = { |
905ab9d1 DT |
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 | } | |
5206c0d5 | 118 | EXPORT_SYMBOL(ps2_is_keyboard_id); |
905ab9d1 DT |
119 | |
120 | /* | |
121 | * ps2_adjust_timeout() is called after receiving 1st byte of command | |
122 | * response and tries to reduce remaining timeout to speed up command | |
123 | * completion. | |
124 | */ | |
125 | ||
b28bad65 DT |
126 | static int ps2_adjust_timeout(struct ps2dev *ps2dev, |
127 | unsigned int command, unsigned int timeout) | |
905ab9d1 DT |
128 | { |
129 | switch (command) { | |
d5e0d918 DT |
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; | |
905ab9d1 | 141 | |
d5e0d918 DT |
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 | } | |
905ab9d1 | 155 | |
d5e0d918 DT |
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; | |
905ab9d1 DT |
170 | } |
171 | ||
172 | return timeout; | |
173 | } | |
174 | ||
1da177e4 LT |
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 | ||
b28bad65 | 182 | int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command) |
1da177e4 | 183 | { |
b28bad65 DT |
184 | unsigned int timeout; |
185 | unsigned int send = (command >> 12) & 0xf; | |
186 | unsigned int receive = (command >> 8) & 0xf; | |
1da177e4 LT |
187 | int rc = -1; |
188 | int i; | |
189 | ||
c611763d DT |
190 | if (receive > sizeof(ps2dev->cmdbuf)) { |
191 | WARN_ON(1); | |
192 | return -1; | |
193 | } | |
194 | ||
95349fe8 DT |
195 | if (send && !param) { |
196 | WARN_ON(1); | |
197 | return -1; | |
198 | } | |
199 | ||
1da177e4 LT |
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 | |
83ae417b | 211 | * time before the ACK arrives. |
1da177e4 | 212 | */ |
c611763d | 213 | if (ps2_sendbyte(ps2dev, command & 0xff, |
218c1f76 DV |
214 | command == PS2_CMD_RESET_BAT ? 1000 : 200)) { |
215 | serio_pause_rx(ps2dev->serio); | |
216 | goto out_reset_flags; | |
217 | } | |
1da177e4 | 218 | |
218c1f76 DV |
219 | for (i = 0; i < send; i++) { |
220 | if (ps2_sendbyte(ps2dev, param[i], 200)) { | |
221 | serio_pause_rx(ps2dev->serio); | |
222 | goto out_reset_flags; | |
223 | } | |
224 | } | |
1da177e4 LT |
225 | |
226 | /* | |
227 | * The reset command takes a long time to execute. | |
228 | */ | |
229 | timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500); | |
230 | ||
231 | timeout = wait_event_timeout(ps2dev->wait, | |
232 | !(ps2dev->flags & PS2_FLAG_CMD1), timeout); | |
233 | ||
a3ce6ea4 | 234 | if (ps2dev->cmdcnt && !(ps2dev->flags & PS2_FLAG_CMD1)) { |
1da177e4 | 235 | |
905ab9d1 | 236 | timeout = ps2_adjust_timeout(ps2dev, command, timeout); |
1da177e4 LT |
237 | wait_event_timeout(ps2dev->wait, |
238 | !(ps2dev->flags & PS2_FLAG_CMD), timeout); | |
239 | } | |
240 | ||
218c1f76 DV |
241 | serio_pause_rx(ps2dev->serio); |
242 | ||
1da177e4 LT |
243 | if (param) |
244 | for (i = 0; i < receive; i++) | |
245 | param[i] = ps2dev->cmdbuf[(receive - 1) - i]; | |
246 | ||
247 | if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1)) | |
218c1f76 | 248 | goto out_reset_flags; |
1da177e4 LT |
249 | |
250 | rc = 0; | |
251 | ||
218c1f76 | 252 | out_reset_flags: |
1da177e4 LT |
253 | ps2dev->flags = 0; |
254 | serio_continue_rx(ps2dev->serio); | |
255 | ||
fc69f4a6 TL |
256 | return rc; |
257 | } | |
258 | EXPORT_SYMBOL(__ps2_command); | |
259 | ||
b28bad65 | 260 | int ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command) |
fc69f4a6 TL |
261 | { |
262 | int rc; | |
263 | ||
181d683d | 264 | ps2_begin_command(ps2dev); |
fc69f4a6 | 265 | rc = __ps2_command(ps2dev, param, command); |
181d683d | 266 | ps2_end_command(ps2dev); |
fc69f4a6 | 267 | |
1da177e4 LT |
268 | return rc; |
269 | } | |
5206c0d5 | 270 | EXPORT_SYMBOL(ps2_command); |
1da177e4 | 271 | |
1da177e4 LT |
272 | /* |
273 | * ps2_init() initializes ps2dev structure | |
274 | */ | |
275 | ||
276 | void ps2_init(struct ps2dev *ps2dev, struct serio *serio) | |
277 | { | |
c4e32e9f | 278 | mutex_init(&ps2dev->cmd_mutex); |
88aa0103 | 279 | lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth); |
1da177e4 LT |
280 | init_waitqueue_head(&ps2dev->wait); |
281 | ps2dev->serio = serio; | |
282 | } | |
5206c0d5 | 283 | EXPORT_SYMBOL(ps2_init); |
1da177e4 LT |
284 | |
285 | /* | |
286 | * ps2_handle_ack() is supposed to be used in interrupt handler | |
287 | * to properly process ACK/NAK of a command from a PS/2 device. | |
288 | */ | |
289 | ||
b28bad65 | 290 | bool ps2_handle_ack(struct ps2dev *ps2dev, u8 data) |
1da177e4 LT |
291 | { |
292 | switch (data) { | |
d5e0d918 DT |
293 | case PS2_RET_ACK: |
294 | ps2dev->nak = 0; | |
295 | break; | |
296 | ||
297 | case PS2_RET_NAK: | |
298 | ps2dev->flags |= PS2_FLAG_NAK; | |
299 | ps2dev->nak = PS2_RET_NAK; | |
300 | break; | |
301 | ||
302 | case PS2_RET_ERR: | |
303 | if (ps2dev->flags & PS2_FLAG_NAK) { | |
304 | ps2dev->flags &= ~PS2_FLAG_NAK; | |
305 | ps2dev->nak = PS2_RET_ERR; | |
1da177e4 | 306 | break; |
d5e0d918 | 307 | } |
1da177e4 | 308 | |
d5e0d918 DT |
309 | /* |
310 | * Workaround for mice which don't ACK the Get ID command. | |
311 | * These are valid mouse IDs that we recognize. | |
312 | */ | |
313 | case 0x00: | |
314 | case 0x03: | |
315 | case 0x04: | |
316 | if (ps2dev->flags & PS2_FLAG_WAITID) { | |
317 | ps2dev->nak = 0; | |
1da177e4 | 318 | break; |
d5e0d918 DT |
319 | } |
320 | /* Fall through */ | |
321 | default: | |
b28bad65 | 322 | return false; |
1da177e4 LT |
323 | } |
324 | ||
a2d781fc DT |
325 | if (!ps2dev->nak) { |
326 | ps2dev->flags &= ~PS2_FLAG_NAK; | |
327 | if (ps2dev->cmdcnt) | |
328 | ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1; | |
329 | } | |
1da177e4 LT |
330 | |
331 | ps2dev->flags &= ~PS2_FLAG_ACK; | |
332 | wake_up(&ps2dev->wait); | |
333 | ||
334 | if (data != PS2_RET_ACK) | |
335 | ps2_handle_response(ps2dev, data); | |
336 | ||
b28bad65 | 337 | return true; |
1da177e4 | 338 | } |
5206c0d5 | 339 | EXPORT_SYMBOL(ps2_handle_ack); |
1da177e4 LT |
340 | |
341 | /* | |
342 | * ps2_handle_response() is supposed to be used in interrupt handler | |
343 | * to properly store device's response to a command and notify process | |
344 | * waiting for completion of the command. | |
345 | */ | |
346 | ||
b28bad65 | 347 | bool ps2_handle_response(struct ps2dev *ps2dev, u8 data) |
1da177e4 LT |
348 | { |
349 | if (ps2dev->cmdcnt) | |
350 | ps2dev->cmdbuf[--ps2dev->cmdcnt] = data; | |
351 | ||
352 | if (ps2dev->flags & PS2_FLAG_CMD1) { | |
353 | ps2dev->flags &= ~PS2_FLAG_CMD1; | |
354 | if (ps2dev->cmdcnt) | |
355 | wake_up(&ps2dev->wait); | |
356 | } | |
357 | ||
358 | if (!ps2dev->cmdcnt) { | |
359 | ps2dev->flags &= ~PS2_FLAG_CMD; | |
360 | wake_up(&ps2dev->wait); | |
361 | } | |
362 | ||
b28bad65 | 363 | return true; |
1da177e4 | 364 | } |
5206c0d5 | 365 | EXPORT_SYMBOL(ps2_handle_response); |
1da177e4 LT |
366 | |
367 | void ps2_cmd_aborted(struct ps2dev *ps2dev) | |
368 | { | |
369 | if (ps2dev->flags & PS2_FLAG_ACK) | |
370 | ps2dev->nak = 1; | |
371 | ||
372 | if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD)) | |
373 | wake_up(&ps2dev->wait); | |
374 | ||
a2d781fc DT |
375 | /* reset all flags except last nack */ |
376 | ps2dev->flags &= PS2_FLAG_NAK; | |
1da177e4 | 377 | } |
5206c0d5 | 378 | EXPORT_SYMBOL(ps2_cmd_aborted); |