Revision 16
added function 1 and 2, working
mouse.c | ||
---|---|---|
1 |
#include "mouse.c" |
|
1 |
#include "mouse.h" |
|
2 |
#include <lcom/lcf.h> |
|
2 | 3 |
|
3 | 4 |
uint8_t byte; |
5 |
int mouse_id=2; //KBD_AUX_IRQ is defined in interrupt.h in .minix-src folder |
|
4 | 6 |
|
5 | 7 |
int (util_sys_inb)(int port, uint8_t *value) { //transform 8 bit into 32 bit |
6 | 8 |
|
... | ... | |
19 | 21 |
} |
20 | 22 |
|
21 | 23 |
void (mouse_ih)(void){ |
24 |
//kbc_read_out_buf(&byte); |
|
22 | 25 |
uint8_t status_reg; |
23 | 26 |
|
24 |
if(util_sys_inb(STATUS_REG,&status_reg)!=0){ //checks if there is an error |
|
25 |
printf("Error reading status register in the keyboard interruption\n"); |
|
26 |
return; |
|
27 |
for(int i=0; i<10;i++){ |
|
28 |
if(util_sys_inb(STATUS_REG,&status_reg)!=0){ //checks if there is an error |
|
29 |
printf("Error reading status register in the mouse interruption\n"); |
|
30 |
return; |
|
31 |
} |
|
32 |
if((status_reg & OUTPUT_BUF)!=0){ |
|
33 |
if((status_reg & (STAT_REG_PAR|STAT_REG_TIMEOUT))!=0){ //checks if there is a parity or timeout error (mask -> 0xC0, bit 7 and 6 set), checks if output buffer is empty |
|
34 |
printf("Parity/Timeout error or output buffer is empty or data coming from the mouse\n"); |
|
35 |
return; |
|
36 |
} |
|
37 |
|
|
38 |
if(util_sys_inb(OUTPUT_BUF,&byte)!=0){//checks if there is an error |
|
39 |
printf("Error reading output buffer in the mouse interruption\n"); |
|
40 |
return; |
|
41 |
} |
|
42 |
|
|
43 |
} |
|
44 |
tickdelay(micros_to_ticks(DELAY_US)); |
|
27 | 45 |
} |
28 |
if(((status_reg & STAT_REG_OBF)==0) ||((status_reg&(STAT_REG_PAR|STAT_REG_TIMEOUT))!=0)){ //checks if there is a parity or timeout error (mask -> 0xC0, bit 7 and 6 set), checks if output buffer is empty |
|
29 |
printf("Parity/Timeout error or output buffer is empty or data coming from the mouse\n"); |
|
30 |
return; |
|
31 |
} |
|
32 |
if(util_sys_inb(OUTPUT_BUF,&byte)!=0){//checks if there is an error |
|
33 |
printf("Error reading output buffer in the keyboard interruption\n"); |
|
34 |
return; |
|
35 |
} |
|
46 |
return; |
|
36 | 47 |
|
37 | 48 |
} |
38 | 49 |
|
39 | 50 |
int (mouse_subscribe_int)(uint8_t *bit_no) { //similar function to that of timer_subscribe_int |
40 |
*bit_no = BIT(keyboard_id);
|
|
41 |
if(sys_irqsetpolicy(KEYBOARD_IRQ,(IRQ_REENABLE|IRQ_EXCLUSIVE),&keyboard_id)==1){ //operation to subscribe int
|
|
51 |
*bit_no = BIT(mouse_id);
|
|
52 |
if(sys_irqsetpolicy(12,(IRQ_REENABLE|IRQ_EXCLUSIVE),&mouse_id)==1){ //operation to subscribe int
|
|
42 | 53 |
printf("Error subscribing int\n"); |
43 | 54 |
return 1; |
44 | 55 |
} |
... | ... | |
46 | 57 |
} |
47 | 58 |
|
48 | 59 |
int (mouse_unsubscribe_int)() { //similar function to that of timer_unsubscribe_int |
49 |
if(sys_irqrmpolicy(&keyboard_id)==1){
|
|
60 |
if(sys_irqrmpolicy(&mouse_id)==1){
|
|
50 | 61 |
printf("Error unsubscribing int\n"); |
51 | 62 |
return 1; |
52 | 63 |
} |
... | ... | |
92 | 103 |
pp->lb=false; |
93 | 104 |
} |
94 | 105 |
|
95 |
if((pp->bytes[0]&0x20)==0x20){ //it means y_delta is negative |
|
96 |
pp->delta_y=~pp->bytes[2]; |
|
97 |
pp->delta_y+=1; |
|
106 |
if((pp->bytes[0] &0x10)==0x10){ |
|
107 |
pp->delta_x = pp->bytes[1] -256; |
|
108 |
} |
|
109 |
else{ |
|
110 |
pp->delta_x = pp->bytes[1]; |
|
111 |
} |
|
98 | 112 |
|
113 |
if((pp->bytes[0]&0x20)==0x20){ |
|
114 |
pp->delta_y = pp->bytes[2] -256; |
|
99 | 115 |
} |
100 | 116 |
else{ |
101 |
pp->delta_y=pp->bytes[2];
|
|
117 |
pp->delta_y = pp->bytes[2];
|
|
102 | 118 |
} |
103 | 119 |
|
104 |
if((pp->bytes[0]&0x10)==0x10){ //it means x_delta is negative |
|
105 |
pp->delta_x=~pp->bytes[1]; |
|
106 |
pp->delta_x+=1; |
|
120 |
return 0; |
|
121 |
} |
|
107 | 122 |
|
123 |
/*int kbc_write(int port, uint8_t cmd) { |
|
124 |
uint8_t status; |
|
125 |
int retry = 0; |
|
126 |
|
|
127 |
while (retry < 10) { |
|
128 |
if(util_sys_inb(STATUS_REG, &status)!=0){ |
|
129 |
printf("Error in kbc_write"); |
|
130 |
return 1; |
|
131 |
} |
|
132 |
|
|
133 |
//... Checking if IN_BUF is OK to write to |
|
134 |
if ((status & 0x02)!=0) { |
|
135 |
//.... Writing the command |
|
136 |
if (sys_outb(port, cmd)!=0) { |
|
137 |
printf("Error"); |
|
138 |
return 1; |
|
139 |
} |
|
140 |
return 0; |
|
141 |
} |
|
142 |
|
|
143 |
//tickdelay(micros_to_ticks(DELAY_US)); // IF NOT EMPTY wait for IN_BUF to be empty |
|
144 |
retry++; |
|
145 |
} |
|
146 |
|
|
147 |
return 1; |
|
148 |
} |
|
149 |
|
|
150 |
int read_out_buf(uint8_t *content) { |
|
151 |
uint8_t status; |
|
152 |
uint8_t content8; // 8-bit content |
|
153 |
int retry = 0; |
|
154 |
|
|
155 |
while (retry < 4) { |
|
156 |
util_sys_inb(STATUS_REG, &status); |
|
157 |
|
|
158 |
//... Checking if there is any error and if the byte came from AUX |
|
159 |
if (((status_reg & STAT_REG_OBF)==0) ||((status_reg&(STAT_REG_PAR|STAT_REG_TIMEOUT))!=0)||((status_reg&STAT_REG_AUX)==0)) { // Parity or Timeout error, invalid data |
|
160 |
return 1; |
|
161 |
} |
|
162 |
|
|
163 |
//... Checking if OUT_BUF is FULL to read |
|
164 |
if (status & STAT_REG_OBF) { |
|
165 |
//... Reading the Output Buffer |
|
166 |
if (UTIL_sys_inb(OUTPUT_BUF, &content8)) { |
|
167 |
return 1; |
|
168 |
} |
|
169 |
// The information in address content now contains content32 in 8-bit |
|
170 |
*content = (uint8_t)content8; |
|
171 |
return 0; |
|
172 |
} |
|
173 |
|
|
174 |
tickdelay(micros_to_ticks(DELAY_US)); // IF NOT EMPTY wait for IN_BUF to be empty |
|
175 |
retry++; |
|
176 |
} |
|
177 |
|
|
178 |
return 1; |
|
179 |
} |
|
180 |
|
|
181 |
|
|
182 |
int kbc_write_mouse_command(uint8_t cmd) { |
|
183 |
uint8_t response; |
|
184 |
|
|
185 |
if(kbc_write(KBC_CMD_REG, 0xD4)!=0){ |
|
186 |
printf("Error"); |
|
187 |
return 1; |
|
108 | 188 |
} |
109 |
else{ |
|
110 |
pp->delta_y=pp->bytes[1]; |
|
189 |
if(kbc_write(INPUT_BUF, cmd)!=0){ |
|
190 |
printf("Error"); |
|
191 |
return 1; |
|
111 | 192 |
} |
112 |
} |
|
193 |
tickdelay(micros_to_ticks(DELAY_US)); |
|
194 |
if(kbc_read_out_buf(&response)!=0){ |
|
195 |
printf("Error"); |
|
196 |
return 1; |
|
197 |
} |
|
198 |
|
|
199 |
// Checking if invalid byte |
|
200 |
if (response != 0xFA) { |
|
201 |
// repeat process if invalid |
|
202 |
kbc_write(KBC_CTRL_REG, 0xD4); |
|
203 |
kbc_write(INPUT_BUF, cmd); |
|
204 |
tickdelay(micros_to_ticks(DELAY_US)); |
|
205 |
kbc_read_out_buf(&response); |
|
206 |
// Checking if second consecutive invalid byte |
|
207 |
if (response == 0xFC){ |
|
208 |
return 1; |
|
209 |
} |
|
210 |
} |
|
211 |
|
|
212 |
return 0; |
|
213 |
}*/ |
|
214 |
|
|
215 |
int write_cmd_mouse(uint8_t cmd) |
|
216 |
{ |
|
217 |
uint8_t status_reg, ack; |
|
218 |
|
|
219 |
do |
|
220 |
{ |
|
221 |
if (util_sys_inb(STATUS_REG, &status_reg) != 0){ |
|
222 |
return 1; |
|
223 |
} |
|
224 |
|
|
225 |
if ((status_reg & STAT_REG_IBF) == 0){ |
|
226 |
if (sys_outb(KBC_CMD_REG, 0xD4) != 0){ //0xD4 -> code to write byte to mouse |
|
227 |
return 1; |
|
228 |
} |
|
229 |
} |
|
230 |
else |
|
231 |
continue; |
|
232 |
|
|
233 |
if (util_sys_inb(STATUS_REG, &status_reg) != 0){ |
|
234 |
return 1; |
|
235 |
} |
|
236 |
|
|
237 |
if ((status_reg & STAT_REG_IBF) == 0){ |
|
238 |
if (sys_outb(OUTPUT_BUF, cmd) != 0){ |
|
239 |
return 1; |
|
240 |
} |
|
241 |
} |
|
242 |
else |
|
243 |
continue; |
|
244 |
|
|
245 |
tickdelay(micros_to_ticks(DELAY_US)); |
|
246 |
|
|
247 |
if (util_sys_inb(INPUT_BUF, &ack) != 0){ |
|
248 |
return 1; |
|
249 |
} |
|
250 |
|
|
251 |
} while (ack != 0xFA); // 0xFA -> mask that checks if everything is ok |
|
252 |
|
|
253 |
return 0; |
|
254 |
} |
Also available in: Unified diff