4 ** Written by Jason Filby (jasonfilby@yahoo.com)
5 ** For ReactOS (www.sid-dis.com/reactos)
7 ** Note: The serial.o driver must be loaded before loading this driver
10 ** Only supports mice on COM port 1
12 ** Old Driver, We done build it. Just keep for History. (S.E.)
16 #include <ddk/ntddk.h>
17 #include <internal/mmhal.h>
18 #include <internal/halio.h>
19 /* #include <internal/hal/ddk.h> */
22 #define MOUSE_IRQ_COM1 4
23 #define MOUSE_IRQ_COM2 3
25 #define COM1_PORT 0x3f8
26 #define COM2_PORT 0x2f8
28 #define max_screen_x 79
29 #define max_screen_y 24
31 static unsigned int MOUSE_IRQ=MOUSE_IRQ_COM1;
32 static unsigned int MOUSE_COM=COM1_PORT;
34 static unsigned int bytepos=0, coordinate;
35 static unsigned char mpacket[3];
36 static signed int mouse_x=40, mouse_y=12;
37 static unsigned char mouse_button1, mouse_button2;
38 static signed int horiz_sensitivity, vert_sensitivity;
40 BOOLEAN microsoft_mouse_handler(PKINTERRUPT Interrupt, PVOID ServiceContext)
42 unsigned int mbyte=inb(MOUSE_COM);
45 if((mbyte&64)==64) { bytepos=0; };
47 mpacket[bytepos]=mbyte;
52 // Retrieve change in x and y from packet
53 int change_x=((mpacket[0] & 3) << 6) + mpacket[1];
54 int change_y=((mpacket[0] & 12) << 4) + mpacket[2];
56 // Some mice need this
63 if(change_x>=128) { change_x=change_x-256; };
64 if(change_y>=128) { change_y=change_y-256; };
66 // Adjust mouse position according to sensitivity
67 mouse_x+=change_x/horiz_sensitivity;
68 mouse_y+=change_y/vert_sensitivity;
70 // Check that mouse is still in screen
71 if(mouse_x<0) { mouse_x=0; };
72 if(mouse_x>max_screen_x) { mouse_x=max_screen_x; };
73 if(mouse_y<0) { mouse_y=0; };
74 if(mouse_y>max_screen_y) { mouse_y=max_screen_y; };
76 // Retrieve mouse button status from packet
77 mouse_button1=mpacket[0] & 32;
78 mouse_button2=mpacket[0] & 16;
85 void InitializeMouseHardware(unsigned int mtype)
87 char clear_error_bits;
89 outb_p(MOUSE_COM+3, 0x80); // set DLAB on
90 outb_p(MOUSE_COM, 0x60); // speed LO byte
91 outb_p(MOUSE_COM+1, 0); // speed HI byte
92 outb_p(MOUSE_COM+3, mtype); // 2=MS Mouse; 3=Mouse systems mouse
93 outb_p(MOUSE_COM+1, 0); // set comm and DLAB to 0
94 outb_p(MOUSE_COM+4, 1); // DR int enable
96 clear_error_bits=inb_p(MOUSE_COM+5); // clear error bits
99 int DetMicrosoft(void)
104 outb_p(MOUSE_COM+4, 0x0b);
105 tmp=inb_p(MOUSE_COM);
107 // Check the first for bytes for signs that this is an MS mouse
109 while((inb_p(MOUSE_COM+5) & 1)==0) ;
110 ind=inb_p(MOUSE_COM);
111 if(ind==0x33) buttons=3;
112 if(ind==0x4d) buttons=2;
118 int CheckMouseType(unsigned int mtype)
120 unsigned int retval=0;
122 InitializeMouseHardware(mtype);
123 if(mtype==2) retval=DetMicrosoft();
125 outb_p(MOUSE_COM+4, 11);
128 outb_p(MOUSE_COM+1, 1);
133 void ClearMouse(void)
135 // Waits until the mouse calms down but also quits out after a while
136 // in case some destructive user wants to keep moving the mouse
139 unsigned int restarts=0, i;
140 for (i=0; i<60000; i++)
142 unsigned temp=inb(MOUSE_COM);
145 if(restarts<300000) {
155 void InitializeMouse(void)
157 int mbuttons=0, gotmouse=0;
161 PKINTERRUPT IrqObject;
166 // Check for Microsoft mouse (2 buttons)
167 if(CheckMouseType(2)!=0)
170 DbgPrint("Microsoft Mouse Detected\n");
175 // Check for Microsoft Systems mouse (3 buttons)
177 if(CheckMouseType(3)!=0)
180 DbgPrint("Microsoft Mouse Detected\n");
187 DbgPrint("No Mouse Detected!\n");
189 MappedIrq = HalGetInterruptVector(Internal, 0, 0, MOUSE_IRQ,
192 IoConnectInterrupt(&IrqObject, microsoft_mouse_handler, NULL,
193 NULL, MappedIrq, Dirql, Dirql, 0, FALSE,
198 // For test purposes only
199 unsigned char get_text_char(int x, int y)
201 unsigned char getchar;
202 char *vidmem=(char*)physical_to_linear((0xb8000+(y*160)+(x*2)));
207 // For test purposes only
208 unsigned char get_text_color(int x, int y)
210 unsigned char getcolor;
211 char *vidmem=(char*)physical_to_linear((0xb8000+(y*160)+(x*2)));
217 // For test purposes only
218 void put_text_char(int x, int y, unsigned char putchar[2])
220 char *vidmem=(char*)physical_to_linear((0xb8000+(y*160)+(x*2)));
226 // For test purposes only
227 void test_mouse(void)
229 static int i=0, forcechange=0;
230 static int old_x=40, old_y=12;
231 static unsigned char old_cursor[2], new_cursor[2];
233 DbgPrint("Testing mouse...");
240 old_cursor[0]=get_text_char(mouse_x, mouse_y);
241 old_cursor[1]=get_text_color(mouse_x, mouse_y);
242 put_text_char(mouse_x, mouse_y, new_cursor);
246 if(mouse_button1!=0) { new_cursor[1]=10; mouse_button1=0; forcechange=1; };
247 if(mouse_button2!=0) { new_cursor[1]=12; mouse_button2=0; forcechange=1; };
249 if((mouse_x!=old_x) || (mouse_y!=old_y) || (forcechange==1)) {
252 put_text_char(old_x, old_y, old_cursor);
253 old_cursor[0]=get_text_char(mouse_x, mouse_y);
254 old_cursor[1]=get_text_color(mouse_x, mouse_y);
255 put_text_char(mouse_x, mouse_y, new_cursor);
264 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
266 DbgPrint("Mouse Driver 0.0.3\n");
270 return(STATUS_SUCCESS);