Page 1 of 2
PS/2 Mouse IRQ Problem
Posted: Thu Apr 24, 2014 8:06 am
by inixsoftware
I started writing an driver for the PS/2 mouse. I know that QEMU does emulate it, so I wanted to make the driver. However, I can't get IRQs for it to work. (My other IRQS - like keyboard and PIT work fine). First, I send the mouse: 0xF4 and I get an ACK, so all should be well.
Next, I add an IRQ handler for it, but, it never gets called, so, the mouse isn't generating interrupts. Am I missing something after sending the mouse 0xF4 to tell it to send interrupts?
Re: PS/2 Mouse IRQ Problem
Posted: Thu Apr 24, 2014 8:08 am
by gerryg400
You also need to enable mouse interrupts by setting b1 in the config byte.
Re: PS/2 Mouse IRQ Problem
Posted: Thu Apr 24, 2014 8:13 am
by inixsoftware
OK. Thanks! So, basically, I read a byte from port 0x64 (right?) and then set bit 1 and then write it back to port 0x64 (right?)
I am just unsure on the port to write and read from for the config byte...
Re: PS/2 Mouse IRQ Problem
Posted: Thu Apr 24, 2014 8:22 am
by gerryg400
inixsoftware wrote:OK. Thanks! So, basically, I read a byte from port 0x64 (right?) and then set bit 1 and then write it back to port 0x64 (right?)
I am just unsure on the port to write and read from for the config byte...
To read the config byte you write 0x20 to port 0x64 then read from port 0x60.
To write the config byte back you write 0x60 to port 0x64 then write the new config byte to 0x60.
In all cases don't forget to wait for the buffer to empty before writing a byte and wait until it's ready to read.
http://wiki.osdev.org/%228042%22_PS/2_Controller is an excellent article.
Re: PS/2 Mouse IRQ Problem
Posted: Thu Apr 24, 2014 8:28 am
by inixsoftware
OK. So, the code would look something like this:
Code: Select all
outb(0x64, 0x20); /* Write 0x20 to port 0x64 */
unsigned char config = inb(0x60); /* Read byte from port 0x60 */
config |= 1 << 1; /* Set bit 1*/
outb(0x64, 0x60); /* Write 0x60 to port 0x64 */
outb(0x60, config); /* Write config byte to port 0x60 */
Re: PS/2 Mouse IRQ Problem
Posted: Thu Apr 24, 2014 8:42 am
by gerryg400
That looks correct, however, to make it work on real hardware you must look at bits 0 and 1 of the status register to see when it's okay to read and write.
PS/2 IRQ Not Firing
Posted: Thu Apr 24, 2014 5:25 pm
by inixsoftware
Hello. I am struggling still with the PS/2 Mouse (i got the PS/2 keyboard working fine). So, the code I am using is:
Code: Select all
outb(0x60, 0xF4);
while (inb(0x60) != 0xFA); /* Wait for ACK from mouse... */
/* Tell mouse to enable interrupts (IRQ12) */
outb(0x64, 0x20);
unsigned char res = inb(0x60);
res |= 1 << 1;
outb(0x64, 0x60);
outb(0x60, res);
/* Install a handler (which prints "Fired!") */
irq_install_handler(12, mouse_handler);
However, I am never getting the text "Fired!". I know my IDT and IRQs are working properly since the PIT and keyboard are working
just fine. Is there something I am missing?
Thanks!
Re: PS/2 IRQ Not Firing
Posted: Thu Apr 24, 2014 6:41 pm
by sortie
This is likely not the problem, but you should install the interrupt handler before enabling interrupt delivery from the device, or you might miss the interrupt (and the lack of a EOI might mean no futher interrupts arive).
Re: PS/2 IRQ Not Firing
Posted: Thu Apr 24, 2014 7:17 pm
by inixsoftware
OK. So I put the irq_install_handler before any of the mouse init code and it still doesn't work...
Re: PS/2 IRQ Not Firing
Posted: Thu Apr 24, 2014 7:34 pm
by gerryg400
I wonder if you are using a real ps/2 mouse ? Or a usb mouse and hoping for ps/2 emulation ?
Re: PS/2 IRQ Not Firing
Posted: Thu Apr 24, 2014 7:35 pm
by inixsoftware
Sorry. Forgot to say I am using QEMU, which I believe does emulate a PS/2 Mouse
Re: PS/2 IRQ Not Firing
Posted: Thu Apr 24, 2014 7:40 pm
by gerryg400
Oh I just noticed. You need to write 0xd4 to the 0x64 register to address the mouse before writing commands to it. Like this ...
Code: Select all
// Address the 2nd device
outb(0x64, 0xd4);
// Write it
outb(0x60, 0xF4);
// Read back
while (inb(0x60) != 0xFA); /* Wait for ACK from mouse... */
Re: PS/2 IRQ Not Firing
Posted: Thu Apr 24, 2014 7:43 pm
by inixsoftware
Thanks! Yes, it now works (I get the text "Fired!")
Re: PS/2 IRQ Not Firing
Posted: Thu Apr 24, 2014 7:48 pm
by inixsoftware
Oh yes. Now, whenever I type something I get a (Mouse) "Fired!" event... Is there something that I need to do to change this?
Re: PS/2 IRQ Not Firing
Posted: Thu Apr 24, 2014 8:10 pm
by sortie
The PS/2 bus delivers both keyboard and mouse events.