1 /** @file
2   PS2 Mouse Communication Interface
3 
4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #ifndef _COMMPS2_H_
10 #define _COMMPS2_H_
11 
12 #include "Ps2Mouse.h"
13 
14 #define PS2_PACKET_LENGTH       3
15 #define PS2_SYNC_MASK           0xc
16 #define PS2_SYNC_BYTE           0x8
17 
18 #define IS_PS2_SYNC_BYTE(byte)  ((byte & PS2_SYNC_MASK) == PS2_SYNC_BYTE)
19 
20 #define PS2_READ_BYTE_ONE       0
21 #define PS2_READ_DATA_BYTE      1
22 #define PS2_PROCESS_PACKET      2
23 
24 #define TIMEOUT                 50000
25 #define BAT_TIMEOUT             500000
26 
27 //
28 // 8042 I/O Port
29 //
30 #define KBC_DATA_PORT     0x60
31 #define KBC_CMD_STS_PORT  0x64
32 
33 //
34 // 8042 Command
35 //
36 #define READ_CMD_BYTE   0x20
37 #define WRITE_CMD_BYTE  0x60
38 #define DISABLE_AUX     0xa7
39 #define ENABLE_AUX      0xa8
40 #define SELF_TEST       0xaa
41 #define DISABLE_KB      0xad
42 #define ENABLE_KB       0xae
43 #define WRITE_AUX_DEV   0xd4
44 
45 #define CMD_SYS_FLAG    0x04
46 #define CMD_KB_STS      0x10
47 #define CMD_KB_DIS      0x10
48 #define CMD_KB_EN       0x0
49 
50 //
51 // 8042 Auxiliary Device Command
52 //
53 #define SETSF1_CMD  0xe6
54 #define SETSF2_CMD  0xe7
55 #define SETRE_CMD   0xe8
56 #define READ_CMD    0xeb
57 #define SETRM_CMD   0xf0
58 #define SETSR_CMD   0xf3
59 #define ENABLE_CMD  0xf4
60 #define DISABLE_CMD 0xf5
61 #define RESET_CMD   0xff
62 
63 //
64 // return code
65 //
66 #define PS2_ACK       0xfa
67 #define PS2_RESEND    0xfe
68 #define PS2MOUSE_BAT1 0xaa
69 #define PS2MOUSE_BAT2 0x0
70 
71 //
72 // Keyboard Controller Status
73 //
74 ///
75 /// Parity Error
76 ///
77 #define KBC_PARE  0x80
78 ///
79 /// General Time Out
80 ///
81 #define KBC_TIM   0x40
82 ///
83 /// Output buffer for auxiliary device (PS/2):
84 ///    0 - Holds keyboard data
85 ///    1 - Holds data for auxiliary device
86 ///
87 #define KBC_AUXB  0x20
88 ///
89 /// Keyboard lock status:
90 ///    0 - keyboard locked
91 ///    1 - keyboard free
92 ///
93 #define KBC_KEYL  0x10
94 ///
95 /// Command/Data:
96 ///    0 - data byte written via port 60h
97 ///    1 - command byte written via port 64h
98 ///
99 #define KBC_CD  0x08
100 ///
101 /// System Flag:
102 ///    0 - power-on reset
103 ///    1 - self-test successful
104 ///
105 #define KBC_SYSF  0x04
106 ///
107 /// Input Buffer Status :
108 ///    0 - input buffer empty
109 ///    1 - CPU data in input buffer
110 ///
111 #define KBC_INPB  0x02
112 ///
113 /// Output Buffer Status :
114 ///    0 - output buffer empty
115 ///    1 - keyboard controller data in output buffer
116 ///
117 #define KBC_OUTB  0x01
118 
119 /**
120   Issue self test command via IsaIo interface.
121 
122   @return EFI_SUCCESS  Success to do keyboard self testing.
123   @return others       Fail to do keyboard self testing.
124 **/
125 EFI_STATUS
126 KbcSelfTest (
127   VOID
128   );
129 
130 /**
131   Issue command to enable keyboard AUX functionality.
132 
133   @return Status of command issuing.
134 **/
135 EFI_STATUS
136 KbcEnableAux (
137   VOID
138   );
139 
140 /**
141   Issue command to disable keyboard AUX functionality.
142 
143   @return Status of command issuing.
144 **/
145 EFI_STATUS
146 KbcDisableAux (
147   VOID
148   );
149 
150 /**
151   Issue command to enable keyboard.
152 
153   @return Status of command issuing.
154 **/
155 EFI_STATUS
156 KbcEnableKb (
157   VOID
158   );
159 
160 /**
161   Issue command to disable keyboard.
162 
163   @return Status of command issuing.
164 **/
165 EFI_STATUS
166 KbcDisableKb (
167   VOID
168   );
169 
170 /**
171   Issue command to check keyboard status.
172 
173   @param KeyboardEnable return whether keyboard is enable.
174 
175   @return Status of command issuing.
176 **/
177 EFI_STATUS
178 CheckKbStatus (
179   OUT BOOLEAN                             *KeyboardEnable
180   );
181 
182 /**
183   Issue command to reset keyboard.
184 
185   @return Status of command issuing.
186 **/
187 EFI_STATUS
188 PS2MouseReset (
189   VOID
190   );
191 
192 /**
193   Issue command to set mouse's sample rate
194 
195   @param SampleRate value of sample rate
196 
197   @return Status of command issuing.
198 **/
199 EFI_STATUS
200 PS2MouseSetSampleRate (
201   IN MOUSE_SR                             SampleRate
202   );
203 
204 /**
205   Issue command to set mouse's resolution.
206 
207   @param Resolution value of resolution
208 
209   @return Status of command issuing.
210 **/
211 EFI_STATUS
212 PS2MouseSetResolution (
213   IN MOUSE_RE                             Resolution
214   );
215 
216 /**
217   Issue command to set mouse's scaling.
218 
219   @param Scaling value of scaling
220 
221   @return Status of command issuing.
222 **/
223 EFI_STATUS
224 PS2MouseSetScaling (
225   IN MOUSE_SF                             Scaling
226   );
227 
228 /**
229   Issue command to enable Ps2 mouse.
230 
231   @return Status of command issuing.
232 **/
233 EFI_STATUS
234 PS2MouseEnable (
235   VOID
236   );
237 
238 /**
239   Get mouse packet . Only care first 3 bytes
240 
241   @param MouseDev  Pointer of PS2 Mouse Private Data Structure
242 
243   @retval EFI_NOT_READY  Mouse Device not ready to input data packet, or some error happened during getting the packet
244   @retval EFI_SUCCESS    The data packet is gotten successfully.
245 
246 **/
247 EFI_STATUS
248 PS2MouseGetPacket (
249   PS2_MOUSE_DEV     *MouseDev
250   );
251 
252 /**
253   Read data via IsaIo protocol with given number.
254 
255   @param Buffer  Buffer receive data of mouse
256   @param BufSize The size of buffer
257   @param State   Check input or read data
258 
259   @return status of reading mouse data.
260 **/
261 EFI_STATUS
262 PS2MouseRead (
263   OUT UINT8                               *Buffer,
264   IN OUT UINTN                            *BufSize,
265   IN  UINTN                               State
266   );
267 
268 //
269 // 8042 I/O function
270 //
271 /**
272   I/O work flow of outing 8042 command.
273 
274   @param Command I/O command.
275 
276   @retval EFI_SUCCESS Success to execute I/O work flow
277   @retval EFI_TIMEOUT Keyboard controller time out.
278 **/
279 EFI_STATUS
280 Out8042Command (
281   IN UINT8                                Command
282   );
283 
284 /**
285   I/O work flow of in 8042 data.
286 
287   @param Data    Data value
288 
289   @retval EFI_SUCCESS Success to execute I/O work flow
290   @retval EFI_TIMEOUT Keyboard controller time out.
291 **/
292 EFI_STATUS
293 In8042Data (
294   IN OUT UINT8                            *Data
295   );
296 
297 /**
298   I/O work flow of outing 8042 data.
299 
300   @param Data    Data value
301 
302   @retval EFI_SUCCESS Success to execute I/O work flow
303   @retval EFI_TIMEOUT Keyboard controller time out.
304 **/
305 EFI_STATUS
306 Out8042Data (
307   IN UINT8                                Data
308   );
309 
310 /**
311   I/O work flow of outing 8042 Aux command.
312 
313   @param Command Aux I/O command
314   @param Resend  Whether need resend the Aux command.
315 
316   @retval EFI_SUCCESS Success to execute I/O work flow
317   @retval EFI_TIMEOUT Keyboard controller time out.
318 **/
319 EFI_STATUS
320 Out8042AuxCommand (
321   IN UINT8                                Command,
322   IN BOOLEAN                              Resend
323   );
324 
325 /**
326   I/O work flow of in 8042 Aux data.
327 
328   @param Data    Buffer holding return value.
329 
330   @retval EFI_SUCCESS Success to execute I/O work flow
331   @retval EFI_TIMEOUT Keyboard controller time out.
332 **/
333 EFI_STATUS
334 In8042AuxData (
335   IN OUT UINT8                            *Data
336   );
337 
338 /**
339   I/O work flow of outing 8042 Aux data.
340 
341   @param Data    Buffer holding return value
342 
343   @retval EFI_SUCCESS Success to execute I/O work flow
344   @retval EFI_TIMEOUT Keyboard controller time out.
345 **/
346 EFI_STATUS
347 Out8042AuxData (
348   IN UINT8                                Data
349   );
350 
351 /**
352   Check keyboard controller status, if it is output buffer full and for auxiliary device.
353 
354   @retval EFI_SUCCESS   Keyboard controller is ready
355   @retval EFI_NOT_READY Keyboard controller is not ready
356 **/
357 EFI_STATUS
358 CheckForInput (
359   VOID
360   );
361 
362 /**
363   I/O work flow to wait input buffer empty in given time.
364 
365   @param Timeout Wating time.
366 
367   @retval EFI_TIMEOUT if input is still not empty in given time.
368   @retval EFI_SUCCESS input is empty.
369 **/
370 EFI_STATUS
371 WaitInputEmpty (
372   IN UINTN                                Timeout
373   );
374 
375 /**
376   I/O work flow to wait output buffer full in given time.
377 
378   @param Timeout given time
379 
380   @retval EFI_TIMEOUT  output is not full in given time
381   @retval EFI_SUCCESS  output is full in given time.
382 **/
383 EFI_STATUS
384 WaitOutputFull (
385   IN UINTN                                Timeout
386   );
387 
388 #endif
389 
390