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