Keyboard and Mouse

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
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Keyboard and Mouse

Post by salil_bhagurkar »

I have written the keyboard handling irq and related code. I also wrote a ps2 mouse code which is working pretty fine. But in the mouse code init when i enable the mouse in stream mode (command F5h) the keyboard gets disabled (no irq from keyboard), inspite of sending the enable keyboard command. Help please...
User avatar
ces_mohab
Member
Member
Posts: 77
Joined: Wed Oct 18, 2006 3:08 am

Post by ces_mohab »

Could you say when your keyboard get disabled?
I mean on simulator or real machine.
Bochs??
To write an OS you need 2 minds one for coding and other for debugging.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Kbd

Post by salil_bhagurkar »

Both... It actually gets disabled in the init_mouse part: Heres the source:

Code: Select all


#define ENABLE_PS2		0xa8
#define KBD_STAT		0x64
#define MOUSE			0xd4
#define MOUSE_STREAM	0xf4
#define MOUSE_DISABLE	0xf5
#define KBD_CMD			0x60

void chkprt()
{
	unsigned char temp;
	while(1)
	{
		temp=inportb(KBD_STAT);
		if((temp&2)==0)	{
			break;
		}
	}
}

void ps2set()
{
	outportb(KBD_STAT,ENABLE_PS2);
	chkprt();
}

void wmous()
{
	outportb(KBD_STAT,MOUSE);
	chkprt();
}

void mbufful()
{
	unsigned char temp;
	while(1)
	{
		temp=inportb(KBD_STAT);
		if((temp&0x20)==0)
			break;
	}
}

char chkmous()
{
	unsigned char temp;
	temp=inportb(KBD_STAT);
	if(temp&1)	{
		return 0;
	}
	else	{
		return 1;
	}
}

void actmous()
{
	wmous();
	outportb(KBD_CMD,MOUSE_STREAM);
	chkprt();
	chkmous();
}

#define DISABLE_KBD		0xad
#define ENABLE_KBD		0xae

void dkeyb()
{
	outportb(KBD_STAT,DISABLE_KBD);
	chkprt();
}

void ekeyb()
{
	outportb(KBD_STAT,ENABLE_KBD);
	chkprt();
}

unsigned char getb()
{
	unsigned char bl=1;
	while(bl)	{
		bl=chkmous();
	}
	dkeyb();
	bl=inportb(0x60);
	ekeyb();
	return bl;
}

struct mouse_data	{
	unsigned char status;
	unsigned char xcoord;
	unsigned char ycoord;
};

typedef struct mouse_data md;

void get_mouse_data(md *data)
{
	data->status=getb();
	data->xcoord=getb();
	data->ycoord=getb();
}

void init_mouse()
{
	printf("\nSetting up PS2 mouse");
	ps2set();
	actmous();
}

I receive no irq after calling init_mouse[/code]
User avatar
ces_mohab
Member
Member
Posts: 77
Joined: Wed Oct 18, 2006 3:08 am

Post by ces_mohab »

have you any initialization code in keyboard.
To write an OS you need 2 minds one for coding and other for debugging.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Kbd init

Post by salil_bhagurkar »

Yes:

Code: Select all



#define K_RDWR 0x60
#define KBD_TIMEOUT 100000

void write_kbd(unsigned adr, unsigned data)
{
	unsigned long timeout;
	unsigned stat;
	for(timeout = KBD_TIMEOUT; timeout != 0; timeout--)
	{
		stat = inportb(K_STATUS);
		if((stat & K_IBUF_FUL) == 0)
			break;
	}
	if(timeout != 0)
		outportb(adr, data); 
}

void init_kbd()
{
	printf("\nSetting up keyboard ");
	empty_8042();
	write_kbd(K_RDWR,0xff);
	set_kbd_leds(NUMLOCK);
	bal=0;
	kbd_set_typematic(0,0);
	delay_ms(200);
}

void set_kbd_leds(unsigned char status)
{
	write_kbd(K_RDWR,K_CMD_LEDS);
	write_kbd(K_RDWR,status);
}

void empty_8042()
{
	unsigned char temp;
	do
	{
		temp = inportb(K_CMD);
		if((temp & K_OBUF_FUL) != 0)
		{
			(void)inportb(K_RDWR);
			continue;
		}
	} 
	while((temp & K_IBUF_FUL) != 0);
}

void kbd_set_typematic(unsigned char delay,unsigned char rate)
{
	unsigned char res=0;
	res|=0x80;
	res|=(delay*32);
	res|=rate;
	write_kbd(K_RDWR,res);
}

#define SCRLLOCK	0x1
#define NUMLOCK		0x2
#define CAPSLOCK	0x4
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

Do you init_mouse code in realmode or pmode ?, as i had a simular probblem and i end up init the mouse in real mode, which seem to fix it ?.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

init

Post by salil_bhagurkar »

Evrythings in pmode...
How can the cpu mode affect the code?
User avatar
ces_mohab
Member
Member
Posts: 77
Joined: Wed Oct 18, 2006 3:08 am

Post by ces_mohab »

I had a similar problem and I found the error in kb_set_leds

Try this code:

Code: Select all

void set_leds(void)
{
	kb_wait_input() ;
	outportb(KB_DATAREG_IO,0xED) ;
	kb_ack() ;
	
	kb_wait_input() ;
	outportb(KB_DATAREG_IO,key_flags) ;
	kb_ack()  ;
} 

inline int kb_wait_input( void )
{
	dword_t time_out = KB_TIMEOUT ; 
	volatile int i ;
    while( time_out-- )
    {
    	for(i=0;i<1000;i++) ;
    	if((inportb( KB_STATUS_IO ) & 2)==0)
        	return 0 ;
    }
    printf("time out error in kb_wait_input\n") ;
    return time_out ;
}


To write an OS you need 2 minds one for coding and other for debugging.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

thnx

Post by salil_bhagurkar »

Thanx... Il try and let you know...
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Um... have you checked the data coming in from the mouse? I've tried your code and the keyboard still works, but the mouse doesn't. Actually, the mouse gets stuck on the 'bl = chkmous();' loop.

One other thing, did you know that the information I'm recieving from the mouse is the keyboard data?

If your mouse code is tested and it works for what it's meant to, discount my opinions (but some help would be nice :D)

Edit: Try this in your mouse handler (I can't promise it'll work, but it's worth a try):

Code: Select all

void MouseWait( char thetype )
{
	long timeout = 100000;
	if( thetype == 0 )
	{
		while( timeout-- )
		{
			if( inportb( 0x64 ) & 1 )
			{
				return;
			}
		}
		return;
	}
	if( thetype == 1 )
	{
		while( timeout-- )
		{
			if( ( inportb( 0x64 ) & 2 ) == 0 )
			{
				return;
			}
		}
		return;
	}
}

// get a byte from the port
unsigned char GetByte()
{
	unsigned char ret;

	DisableKeyboard();
	MouseWait( 0 );
	ret = inportb( 0x60 );
	EnableKeyboard(); // yours are ekbd or something...

	return ret;
}
Edit 2: Something is wrong with the above code, sometimes it messes the status with the xcoord and ycoord members... Any ideas anyone?
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

News

Post by salil_bhagurkar »

Ok now check this out... I emptied the input and output buffer of the 8042 after the mouse and keyboard init and retried the code. Now i dont have a mouse interrupt handler as of now... When i booted on a real pc my keyboard worked fine.... But as soon as i moved the mouse, i got no IRQ from the keyboard... All my IRQs are enabled and all send an EOI to the respective PIC.. As required i chain handlers to the main IRQ handlers.. I dont have a chained handler to the mouse IRQ. It only sends an EOI.
Post Reply