deleted errors
[ZeXOS.git] / kernel / drivers / char / mouse / ps2.c
blob56069e1fdb4da55a3015895097332eaddd495151
1 #include <build.h>
3 #ifdef ARCH_i386
5 #include <system.h>
6 #include <arch/io.h>
7 #include <string.h>
9 #define PS2MOUSE_SAMPLES 0x64 // Samples in second - 0x50 for 80, 0x64 for 100, 0xC8 for 200
10 #define PS2MOUSE_RESOLUTION 2
12 #define PS2MOUSE_TIMEOUT 500000
13 #define PS2MOUSE_ACK 0xFA
14 #define PS2MOUSE_PORT 0x60
15 #define PS2MOUSE_CTRL 0x64
16 #define PS2MOUSE_COMMAND 0xD4
17 #define PS2MOUSE_COMMAND_RESET 0xFF
18 #define PS2MOUSE COMMAND_GET_MOUSE_ID 0xF2
19 #define PS2MOUSE_COMMAND_ENABLE_PACKETS 0xF4
20 #define PS2MOUSE_COMMAND_SET_SAMPLES 0xF3
21 #define PS2MOUSE_COMMAND_SET_RESOLUTION 0xE8
22 #define PS2MOUSE_RESET_ACK 0xAA
23 #define PS2MOUSE_IRQ 12
25 static int cursor_x;
26 static int cursor_y;
27 static int cursor_state;
29 bool ps2mouse_reset ();
30 bool ps2mouse_command_write (unsigned char command);
31 bool ps2mouse_write (unsigned char command);
32 unsigned char ps2mouse_read ();
34 /* ps2mouse_reset - reset mouse */
36 bool ps2mouse_reset()
38 int timeout;
40 for (timeout = 0; timeout < PS2MOUSE_TIMEOUT; timeout ++)
42 if((inb (PS2MOUSE_CTRL) && 0x02) == 0x02)
43 break;
44 schedule ();
47 outb (PS2MOUSE_CTRL, PS2MOUSE_COMMAND);
48 outb (PS2MOUSE_PORT, PS2MOUSE_COMMAND_RESET);
50 for (timeout = 0; timeout < PS2MOUSE_TIMEOUT; timeout ++)
52 int read = ps2mouse_read ();
53 if (read == PS2MOUSE_RESET_ACK)
54 return 1;
56 if (read == -1)
57 return 0;
59 schedule ();
62 return -1;
65 /* ps2mouse_command_write - send a command to mouse */
67 bool ps2mouse_command_write(unsigned char command)
69 int timeout;
71 for (timeout = 0; timeout < PS2MOUSE_TIMEOUT; timeout ++)
73 if((inb (PS2MOUSE_CTRL) && 0x02) == 0x02)
74 break;
75 schedule ();
78 outb (PS2MOUSE_CTRL, PS2MOUSE_COMMAND);
79 outb (PS2MOUSE_PORT, command);
81 for (timeout = 0; timeout < PS2MOUSE_TIMEOUT; timeout ++)
83 int read = ps2mouse_read ();
84 if (read == PS2MOUSE_ACK)
85 return 1;
87 if (read == -1)
88 return 0;
90 schedule ();
93 return -1;
96 /* ps2mouse_write - send a command to mouse */
98 bool ps2mouse_write(unsigned char command)
100 int timeout;
102 for (timeout = 0; timeout < PS2MOUSE_TIMEOUT; timeout ++)
104 if((inb (PS2MOUSE_CTRL) && 0x02) == 0x02)
105 break;
106 schedule ();
109 outb (PS2MOUSE_PORT, command);
111 for (timeout = 0; timeout < PS2MOUSE_TIMEOUT; timeout ++)
113 int read = ps2mouse_read ();
114 if (read == PS2MOUSE_ACK)
115 return 1;
117 if (read == -1)
118 return 0;
120 schedule ();
123 return -1;
126 /* ps2mouse_read - read a value from mouse */
128 unsigned char ps2mouse_read()
130 int timeout;
132 for (timeout = 0; timeout < PS2MOUSE_TIMEOUT; timeout ++)
134 if ((inb (PS2MOUSE_CTRL) && 0x01) != 0x01)
135 return inb (PS2MOUSE_PORT);
136 schedule ();
139 return -1;
142 void ps2mouse_handler (struct regs *r)
144 kprintf("handler ()\n");
146 /* while (inb(0x64) == 0x02) // Wait until on port 0x64 isn´t 02h
147 schedule();
149 outb(0x64,0xD4);
150 outb(0x60,0xEB);
152 while (inb(0x64) == 0x01) // Wait until on port 0x64 isn´t 01h
153 schedule ();
155 char b1 = inb (0x60);
157 while (inb(0x64) == 0x01) // Wait until on port 0x64 isn´t 01h
158 schedule ();
160 char b2 = inb (0x60);
162 while (inb(0x64) == 0x01) // Wait until on port 0x64 isn´t 01h
163 schedule();
165 char b3 = inb(0x60);
167 if ((b1 & (1 << 6)) == 0)
168 return;
170 cursor_x += (int) (char) ((b1 << 6) | (b2 & 0x3f));
171 cursor_y += (int) (char) (((b1 << 4) & 0xc0) | (b3 & 0x3f));
173 if (cursor_y < 0)
174 cursor_y = 0;
176 if (cursor_y > 199)
177 cursor_y = 199;
179 if (cursor_x < 0)
180 cursor_x = 0;
182 if (cursor_x > 319)
183 cursor_x = 319;
185 cursor_state = (b1 >> 4) & 3;*/
188 unsigned ps2mouse_init ()
190 unsigned char status_byte = 0xD8;
191 unsigned char mouse_id;
193 if(ps2mouse_reset () == -1)
194 return -1;
196 mouse_id = ps2mouse_read ();
198 ps2mouse_command_write (PS2MOUSE_COMMAND_SET_SAMPLES);
199 ps2mouse_write (PS2MOUSE_SAMPLES);
201 ps2mouse_command_write (PS2MOUSE_COMMAND_SET_RESOLUTION);
202 ps2mouse_write (PS2MOUSE_RESOLUTION);
204 kprintf ("ps2mouse -> ID: %d \n", mouse_id);
206 /* irq installing */
208 irq_install_handler(PS2MOUSE_IRQ, ps2mouse_handler);
210 timer_wait(1000);
212 return 1;
215 void ps2mouse_get_status (int *x, int *y, int *state)
217 *x = cursor_x;
218 *y = cursor_y;
219 *state = cursor_state;
222 bool ps2mouse_acthandler (unsigned act, char *block, unsigned block_len)
224 switch (act) {
225 case DEV_ACT_INIT:
227 return ps2mouse_init ();
229 break;
230 case DEV_ACT_READ:
232 return 0;
234 break;
235 case DEV_ACT_WRITE:
237 return 0;
239 break;
242 return 0;
245 #endif