-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.c
executable file
·114 lines (85 loc) · 2.32 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "utils.h"
unsigned long int no_of_calls = 0;
static uint8_t st;
int(util_get_LSB)(uint16_t val, uint8_t *lsb) {
if (!lsb)
return 1;
*lsb = (uint8_t) val;
return 0;
}
int (util_get_MSB)(uint16_t val, uint8_t *msb) {
if (!msb)
return 1;
val = val >> 8;
*msb = (uint8_t) val;
return 0;
}
int (util_sys_inb)(int port, uint8_t *value) {
if (!value)
return 1;
u32_t value32;
if (sys_inb(port, &value32))
return 1;
*value = (uint8_t) value32;
no_of_calls++;
return 0;
}
uint8_t (kbc_send_cmd)(uint8_t port, uint8_t cmd) {
// This section waits until it can write to input buffer or it reaches a timeout state
uint8_t i = TIMEOUT_ATTEMPTS;
while (i) { // Tries for i attempts
if (util_sys_inb(STAT_REG, &st)) // Read Status
return 1;
// Can only write if the ST_IN_BUF is set to 0
if (st & ST_IN_BUF) {
i--;
if (tickdelay(micros_to_ticks(KBC_WAIT)))
return 1;
continue;
}
else
break;
}
if (i == 0) // Timed out
return 1;
if (sys_outb(port, cmd)) // Write the command
return 1;
return 0;
}
uint8_t kbc_read_outbf(uint8_t port, uint8_t *content, bool isMouse)
{
// This section waits until there is something
// to read from the output buffer
// or it reaches a time out state
uint8_t i = TIMEOUT_ATTEMPTS;
while (i) // Tries for i attempts
{
//printf("Tries: %u\n", i);
if (util_sys_inb(STAT_REG, &st)) // Read Status
return 1;
// Can only read if the ST_OUT_BUF is set to 1
// AND if isMouse is true, check if the status has Mouse Data
if ((st & ST_OUT_BUF) && ((st & ST_MOUSE_DATA) == isMouse * ST_MOUSE_DATA))
{ // (st & BIT(n)) has BIT(n) as 0 or 1, compare to isMouse (1 or 0) * BIT(n)
break;
}
else
{
--i;
if (tickdelay(micros_to_ticks(KBC_WAIT)))
return 1;
continue;
}
}
if (i == 0) // Timed out
return 1;
if (util_sys_inb(OUT_BUF, content))
return 1;
return 0;
}
// Restore KBC byte to default state (returned by minix_get_dflt_kbc_byte())
uint8_t restore_kbc_byte() {
if (kbc_send_cmd(IN_BUF_CMD, WRITE_CMD_BYTE))
return 1;
return kbc_send_cmd(IN_BUF_ARGS, minix_get_dflt_kbc_cmd_byte());
}