Mouse Driver

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Mouse Driver

Post by melgmry0101b »

Hi everyone,
I was writing a mouse driver
But when i went to try it , i found that i can't use keyboard :|
That is my code :

Code: Select all

#include <string.h>
#include <ctype.h>
#include <hal.h>
int x = 0, y = 0;
static volatile int button[3] = {0, 0, 0};
unsigned char data[3];
void left_button_down(int x, int y)
{
}

void left_button_up(int x, int y)
{
}

void right_button_down(int x, int y)
{

}

void right_button_up(int x, int y)
{
}

void middle_button_down(int x, int y)
{
}

void middle_button_up(int x, int y)
{
}

void _cdecl ps2_mouse()
{
    data[0] = inportb(0x60);
    data[1] = inportb(0x60);
    data[2] = inportb(0x60);
    if ((data[0] & 0x01) != button[0])
    {
        button[0] ^= 1;
        if (button[0]) left_button_down(x, y);
        else left_button_up(x, y);
    }
    if ((data[0] & 0x04) != button[1])
    {
        button[1] ^= 1;
        if (button[1]) middle_button_down(x, y);
        else middle_button_up(x, y);
    }
    if ((data[0] & 0x02) != button[2])
    {
        button[2] ^= 1;
        if (button[2]) right_button_down(x, y);
        else right_button_up(x, y);
    }
    if (data[0] & 0x10)
        x += (int)((256 - data[1]) * -1);
    else
        x += (int)data[1];
    if (data[0] & 0x20)
        y += (int) (256 - data[2]);
    else
        y += (int)(data[2] * -1);
    if (y > 184) y = 184;
    else if (y < 0) y = 0;
    if (x > 311) x = 311;
    else if (x < 0) x = 0;
}

void init_ps2_mouse()
{
    int x;
    unsigned char data_read;
    for(x = 0; x < 5; x++)
    {
        outportb(0x64,0xA7);                   
        outportb(0x64,0xA8);
        outportb(0x64,0xD4);
        outportb(0x60,0xF5);
        data_read = inportb(0x60);          
        if (data_read != 0xFA) continue;    
        outportb(0x64,0xD4);
        outportb(0x60,0xFF);
        data_read = inportb(0x60);             
        if (data_read != 0xFA) continue;    
        data_read = inportb(0x60) ;           
        if (data_read != 0xAA) continue;    
 
        outportb(0x64,0xD4);
        outportb(0x60,0xE6);
        data_read = inportb(0x60) ;          
        if (data_read != 0xFA) continue;   
        outportb(0x64,0x20);                   
        data_read = inportb(0x60)  ;          
        data_read |= 0x02;                  
        outportb(0x64,0x60);                    
        outportb(0x60,data_read);           
        outportb(0x64,0xD4);                
        outportb(0x60,0xF4);
        data_read = inportb(0x60) ;           
        if (data_read != 0xFA) continue;    
        break;
    }
	setvect (12, (void (__cdecl &)(void))ps2_mouse);
}

-----------------
Thank you in advance
------------------------
DLBuunk
Member
Member
Posts: 39
Joined: Sun May 18, 2008 9:36 am
Location: The Netherlands

Re: Mouse Driver

Post by DLBuunk »

Ehm, if i read your story correctly, once you tried using the mouse, the keyboard stopped working? How? And the mouse, does it "work" ?

A reasonable debugging step might be checking if the keyboard killing code is in the setup part or in the interrupt handler. For one thing, are you sure it completes the init loop? Have you dumped the contents of the KBC registers? What do they say?

Oh, and one more thing, never forget the mantra "Comment your code, comment your code", this, and properly describing the problem makes it way more likely to get a functional answer from the people at this forum, or for the same sake, anywhere.
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Mouse Driver

Post by Chandra »

The only thing I would like to know is, does you mouse code work properly? I mean, do you recieve proper packets from the mouse?
Before worrying about the keyboard, I think it would help to know if the mouse code works as intended. Actually, I can figure out some problem in the mouse code itself, so before we continue, lets make things clear.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
jrepan
Posts: 11
Joined: Sat Feb 27, 2010 8:05 am
Location: Estonia

Re: Mouse Driver

Post by jrepan »

I think the problem is that when you get IRQ you may read only one byte, not all 3. So you need to do something similar to what is done here: http://forum.osdev.org/viewtopic.php?t=10247
melgmry0101b
Member
Member
Posts: 109
Joined: Wed Nov 10, 2010 10:49 am

Re: Mouse Driver

Post by melgmry0101b »

Hi ,
i will try again
Post Reply