CS350 COS
COS
Loading...
Searching...
No Matches
ps2.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/kassert.h>
#include <sys/kdebug.h>
#include <sys/irq.h>
#include <machine/amd64.h>
#include <machine/amd64op.h>
#include "../console.h"
#include "ps2.h"
Include dependency graph for ps2.c:

Go to the source code of this file.

Macros

#define PS2_STATUS_PORT   0x64
 
#define PS2_COMMAND_PORT   0x64
 
#define PS2_DATA_PORT   0x60
 
#define PS2_STATUS_OBS   0x01 /* Input Buffer Ready */
 
#define PS2_STATUS_IBS   0x02 /* Output Buffer Full */
 
#define PS2_COMMAND_AUXDISABLE   0xA7 /* Disable Auxiliary */
 
#define PS2_COMMAND_AUXENABLE   0xA8 /* Enable Auxiliary */
 
#define PS2_COMMAND_SELFTEST   0xAA /* Test Controller */
 
#define PS2_COMMAND_KBDDISABLE   0xAD /* Disable Keyboard */
 
#define PS2_COMMAND_KBDENABLE   0xAE /* Enable Keyboard */
 
#define PS2_KBD_RESET   0xFF /* Reset */
 
#define PS2_KBD_SETLED   0xED /* Set LED */
 
#define PS2_KBD_RESETDONE   0xAA /* Reset Done */
 
#define KS_SHIFT   0x01
 
#define KS_ALT   0x02
 
#define KS_CONTROL   0x04
 
#define KS_CAPSLOCK   0x08
 
#define KS_NUMLOCK   0x10
 
#define KS_SCROLLLOCK   0x20
 
#define KS_E0ESCAPE   0x30
 

Functions

static void PS2_KeyboardIntr (void *arg)
 
static void PS2_MouseIntr (void *arg)
 
void PS2Wait ()
 
void PS2_Init ()
 

Variables

static IRQHandler kbdHandler
 
static IRQHandler psmHandler
 
static uint32_t keyState
 
static uint8_t keyMap [256]
 
static uint8_t shiftKeyMap [256]
 
static uint8_t controlKeyMap [256]
 

Macro Definition Documentation

◆ KS_ALT

#define KS_ALT   0x02

Definition at line 44 of file ps2.c.

◆ KS_CAPSLOCK

#define KS_CAPSLOCK   0x08

Definition at line 46 of file ps2.c.

◆ KS_CONTROL

#define KS_CONTROL   0x04

Definition at line 45 of file ps2.c.

◆ KS_E0ESCAPE

#define KS_E0ESCAPE   0x30

Definition at line 49 of file ps2.c.

◆ KS_NUMLOCK

#define KS_NUMLOCK   0x10

Definition at line 47 of file ps2.c.

◆ KS_SCROLLLOCK

#define KS_SCROLLLOCK   0x20

Definition at line 48 of file ps2.c.

◆ KS_SHIFT

#define KS_SHIFT   0x01

Definition at line 43 of file ps2.c.

◆ PS2_COMMAND_AUXDISABLE

#define PS2_COMMAND_AUXDISABLE   0xA7 /* Disable Auxiliary */

Definition at line 26 of file ps2.c.

◆ PS2_COMMAND_AUXENABLE

#define PS2_COMMAND_AUXENABLE   0xA8 /* Enable Auxiliary */

Definition at line 27 of file ps2.c.

◆ PS2_COMMAND_KBDDISABLE

#define PS2_COMMAND_KBDDISABLE   0xAD /* Disable Keyboard */

Definition at line 29 of file ps2.c.

◆ PS2_COMMAND_KBDENABLE

#define PS2_COMMAND_KBDENABLE   0xAE /* Enable Keyboard */

Definition at line 30 of file ps2.c.

◆ PS2_COMMAND_PORT

#define PS2_COMMAND_PORT   0x64

Definition at line 18 of file ps2.c.

◆ PS2_COMMAND_SELFTEST

#define PS2_COMMAND_SELFTEST   0xAA /* Test Controller */

Definition at line 28 of file ps2.c.

◆ PS2_DATA_PORT

#define PS2_DATA_PORT   0x60

Definition at line 19 of file ps2.c.

◆ PS2_KBD_RESET

#define PS2_KBD_RESET   0xFF /* Reset */

Definition at line 33 of file ps2.c.

◆ PS2_KBD_RESETDONE

#define PS2_KBD_RESETDONE   0xAA /* Reset Done */

Definition at line 37 of file ps2.c.

◆ PS2_KBD_SETLED

#define PS2_KBD_SETLED   0xED /* Set LED */

Definition at line 34 of file ps2.c.

◆ PS2_STATUS_IBS

#define PS2_STATUS_IBS   0x02 /* Output Buffer Full */

Definition at line 23 of file ps2.c.

◆ PS2_STATUS_OBS

#define PS2_STATUS_OBS   0x01 /* Input Buffer Ready */

Definition at line 22 of file ps2.c.

◆ PS2_STATUS_PORT

#define PS2_STATUS_PORT   0x64

Definition at line 17 of file ps2.c.

Function Documentation

◆ PS2_Init()

void PS2_Init ( )

Definition at line 274 of file ps2.c.

275{
276 uint8_t status, data;
277
278 keyState = 0;
279
281
282 while (1) {
283 status = inb(PS2_STATUS_PORT);
284 if ((status & PS2_STATUS_OBS) == 0)
285 break;
286
287 data = inb(PS2_DATA_PORT);
288 }
289
290 // Self test
292 PS2Wait();
293 data = inb(PS2_DATA_PORT);
294 if (data != 0x55)
295 kprintf("PS2: Controller test failed\n");
296
298
299 kbdHandler.irq = 1;
302 psmHandler.irq = 12;
305
308}
static INLINE uint8_t inb(uint16_t port)
Definition: amd64op.h:452
static INLINE void outb(uint16_t port, uint8_t data)
Definition: amd64op.h:431
void IRQ_Register(int irq, struct IRQHandler *h)
Definition: irq.c:35
int kprintf(const char *fmt,...)
Definition: printf.c:210
static uint32_t keyState
Definition: ps2.c:50
#define PS2_COMMAND_KBDDISABLE
Definition: ps2.c:29
void PS2Wait()
Definition: ps2.c:262
#define PS2_STATUS_PORT
Definition: ps2.c:17
static void PS2_MouseIntr(void *arg)
Definition: ps2.c:257
static IRQHandler psmHandler
Definition: ps2.c:40
#define PS2_COMMAND_KBDENABLE
Definition: ps2.c:30
#define PS2_DATA_PORT
Definition: ps2.c:19
#define PS2_COMMAND_SELFTEST
Definition: ps2.c:28
#define PS2_COMMAND_PORT
Definition: ps2.c:18
#define PS2_STATUS_OBS
Definition: ps2.c:22
static IRQHandler kbdHandler
Definition: ps2.c:39
static void PS2_KeyboardIntr(void *arg)
Definition: ps2.c:167
#define NULL
Definition: stddef.h:6
void * arg
Definition: irq.h:10
int irq
Definition: irq.h:8
void(* cb)(void *)
Definition: irq.h:9
unsigned char uint8_t
Definition: types.h:10
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PS2_KeyboardIntr()

static void PS2_KeyboardIntr ( void *  arg)
static

Definition at line 167 of file ps2.c.

168{
169 uint8_t status;
170 uint8_t data;
171 uint8_t key;
172
173 while (1) {
174 status = inb(PS2_STATUS_PORT);
175 if ((status & PS2_STATUS_OBS) == 0)
176 return;
177
178 data = inb(PS2_DATA_PORT);
179
180 // E0 escape sequence
181 if (data == 0xE0) {
183 continue;
184 }
185
186 // Check for release of shift, alt, control
187 if (data & 0x80) {
188 if (data == 0xAA || data == 0xB6)
189 keyState &= ~KS_SHIFT;
190 if (data == 0x9D)
191 keyState &= ~KS_CONTROL;
192 if (data == 0x38)
193 keyState &= ~KS_ALT;
194
195 // Other key release
196 continue;
197 }
198
199 // Check for shift, alt, control
200 if (data == 0x2A || data == 0x36)
202 if (data == 0x1D)
204 if (data == 0x38)
205 keyState |= KS_ALT;
206
207 // Check for numlock, capslock, scrolllock
208 if (data == 0x3A)
210 if (data == 0x45)
212 if (data == 0x46)
214
215 // Decode character
216 if (keyState & KS_E0ESCAPE) {
217 keyState &= ~KS_E0ESCAPE;
218
219 // Escape sequences
220 continue;
221 }
222
223 // Debugger sequences
224 if ((keyState & (KS_ALT | KS_CONTROL)) == (KS_ALT | KS_CONTROL)) {
225 // Debugger: Alt-Control-Backspace
226 if (data == 0x0E)
227 breakpoint();
228
229 // Reboot: Alt-Control-Delete
230 }
231
232 int isShift = 0;
233 if (keyState & KS_SHIFT)
234 isShift = 1;
235 if (keyState & KS_CAPSLOCK)
236 isShift ^= 1;
237
238 if (keyState & KS_CONTROL) {
239 key = controlKeyMap[data];
240 } else if (isShift) {
241 key = shiftKeyMap[data];
242 } else {
243 key = keyMap[data];
244 }
245
246 if (key == 0x00)
247 continue;
248
249 //kprintf("Key Press: %02X\n", key);
251
252 continue;
253 }
254}
static INLINE void breakpoint()
Definition: amd64op.h:29
void Console_EnqueueKey(char key)
Definition: console.c:75
#define KS_NUMLOCK
Definition: ps2.c:47
#define KS_CAPSLOCK
Definition: ps2.c:46
#define KS_CONTROL
Definition: ps2.c:45
#define KS_ALT
Definition: ps2.c:44
#define KS_SCROLLLOCK
Definition: ps2.c:48
static uint8_t controlKeyMap[256]
Definition: ps2.c:128
static uint8_t shiftKeyMap[256]
Definition: ps2.c:90
#define KS_E0ESCAPE
Definition: ps2.c:49
static uint8_t keyMap[256]
Definition: ps2.c:52
#define KS_SHIFT
Definition: ps2.c:43
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PS2_MouseIntr()

static void PS2_MouseIntr ( void *  arg)
static

Definition at line 257 of file ps2.c.

258{
259}
Here is the caller graph for this function:

◆ PS2Wait()

void PS2Wait ( )

Definition at line 262 of file ps2.c.

263{
264 uint8_t status;
265
266 while (1) {
267 status = inb(PS2_STATUS_PORT);
268 if ((status & PS2_STATUS_OBS) != 0)
269 return;
270 }
271}
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ controlKeyMap

uint8_t controlKeyMap[256]
static

Definition at line 128 of file ps2.c.

◆ kbdHandler

IRQHandler kbdHandler
static

Definition at line 39 of file ps2.c.

◆ keyMap

uint8_t keyMap[256]
static

Definition at line 52 of file ps2.c.

◆ keyState

uint32_t keyState
static

Definition at line 50 of file ps2.c.

◆ psmHandler

IRQHandler psmHandler
static

Definition at line 40 of file ps2.c.

◆ shiftKeyMap

uint8_t shiftKeyMap[256]
static

Definition at line 90 of file ps2.c.