I have written "some" keyboard drivers and I wanted make a little mouse support, maybe a cursor. I read several guides on ps/2 mouse especially on the wiki and copied a bunch of code from Sanik's to start. (viewtopic.php?t=10247)
However I cannot get the mouse to respond. I initialise the mouse and setup irq12 but the output is not consistent. Here is the code.
Code: Select all
void mouse_install()
{
unsigned char _status; //unsigned char
mouse_write(0xFF);
mouse_read();
//Enable the interrupts
mouse_wait(1);
outb(0x64, 0x20);
mouse_wait(0);
_status=inb(0x60);
_status = (_status | 2) ;
mouse_wait(1);
outb(0x64, 0x60);
mouse_wait(1);
outb(0x60, _status);
mouse_read();
//Enable the mouse
mouse_write(0xF4);
mouse_read(); //Acknowledge
}
Code: Select all
void irq12_handler(interrupt_frame_t* frame) //struct regs *a_r (not used but just there)
{
switch(mouse_cycle)
{
case 0:
mouse_byte[0]=inb(0x60);
mouse_cycle++;
break;
case 1:
mouse_byte[1]=inb(0x60);
mouse_cycle++;
break;
case 2:
mouse_byte[2]=inb(0x60);
mouse_x = mouse_byte[1];
mouse_y = mouse_byte[2];
mouse_cycle=0;
break;
}
printf(mouse_x);
default_irq_handler(frame); // <- This is just outb(0x20, 0x20); outb(0xA0, 0x20);
}
What may the problem be? I am running this on QEmu.
Important edit:
I just kept making little changes and I didnt change anything related to the mouse code however I got the following output
This is the output I got as I moved the mouse. It is very interesting because I didn't change anything at all. The code that didn't work before now works, and it is likely to get broken in the future.
Edit: I think I know the problem. If I were to run the program on qemu and at the moment it runs, click on the qemu screen so I can capture the input; the mouse is detected and some garbage is printed on the screen (that garbage is the unformatted deltaXs). The problem is that if I wait for a moment before clicking the screen I think the mouse is not detected thus it is not emulated as a ps/2 mouse in the qemu therefore it is not initialised. How can I resolve this issue.
Edit: These are the output with printf("%d", mouse_x) they make more sense
These are the deltaX from the mouse which means it works. The problem is with the timing that the mouse is connected to the computer. How can I solve the problem?
So the overall problem is that I need to detect the mouse being plugged into the computer (or QEmu in this case) and initialise the mouse after it is plugged. Is this normal behaviour? How can I solve the issue?
Feel free to request more code. Thank you.