Page 1 of 1

keyboard interrupt working only once

Posted: Sun Apr 01, 2012 12:52 am
by aki522
Hi
I am trying to write a basic keyboard driver and I am only getting one keypress that prints the character correctly onto the screen and I do not see any output on the screen for subsequent keypresses..its like the system becomes unresponsive. Please help.

Code: Select all

SET_IDT_ENTRY(idt[33],keyboard);

Code: Select all

void keyboard(){
asm( "cli;"
	"call keyboard_handler;"
         "iret;");
	}

Code: Select all

char kbdus[] =
{
    0,  27, '1', '2', '3', '4', '5', '6', '7', '8',	/* 9 */
  '9', '0', '-', '=', '\b',	/* Backspace */
  '\t',			/* Tab */
  'q', 'w', 'e', 'r',	/* 19 */
  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',	/* Enter key */
    0,			/* 29   - Control */
  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',	/* 39 */
 '\'', '`',   0,		/* Left shift */
 '\\', 'z', 'x', 'c', 'v', 'b', 'n',			/* 49 */
  'm', ',', '.', '/',   0,				/* Right shift */
  '*',
    0,	/* Alt */
  ' ',	/* Space bar */
    0,	/* Caps lock */
    0,	/* 59 - F1 key ... > */
    0,   0,   0,   0,   0,   0,   0,   0,
    0,	/* < ... F10 */
    0,	/* 69 - Num lock*/
    0,	/* Scroll Lock */
    0,	/* Home key */
    0,	/* Up Arrow */
    0,	/* Page Up */
  '-',
    0,	/* Left Arrow */
    0,
    0,	/* Right Arrow */
  '+',
    0,	/* 79 - End key*/
    0,	/* Down Arrow */
    0,	/* Page Down */
    0,	/* Insert Key */
    0,	/* Delete Key */
    0,   0,   0,
    0,	/* F11 Key */
    0,	/* F12 Key */
    0,	/* All other keys are undefined */
};

Code: Select all

void keyboard_handler(long exceptionNum)
{
	outportb_1(0x20,0x20);
	int a;
        uint32_t scancode;
	uint8_t keychar;
	scancode = inportb_1(0x60);
	a = inportb_1(0x61);
	// check if a key was just released
	if(!(scancode & 0x0080)) //key pressed
	{
		keychar = kbdus[scancode];
		putch(keychar);
	}
	outportb_1(0x61, a|0x80);
	outportb_1(0x61,a);
	send_eoi(1);
        asm("sti;");
 }

Code: Select all

void keyboard_init(void)
 {
 	printf("Initializing Keyboard... \n");
	enable_irq(1);
 }
In main.c

Code: Select all

pic_init();
keyboard_init();
enable_irq(2);//slave pic

Re: keyboard interrupt working only once

Posted: Sun Apr 01, 2012 1:01 am
by brain
What does your send_eoi function look like? Are you properly acknowledging the irq?

Re: keyboard interrupt working only once

Posted: Sun Apr 01, 2012 9:09 am
by aki522

Code: Select all

#define charb_array(x,y)	(((unsigned char*)&(y))[x])
#define master_mask_1		(charb_array(0,0xFFFF))
#define slave_mask_1		(charb_array(1,0xFFFF))
#define EOI             0x60
#define MASTER_8259_PORT 0x20
#define SLAVE_8259_PORT  0xA0
#define MASTER_8259_PORT_PLUS_ONE MASTER_8259_PORT+1
#define SLAVE_8259_PORT_PLUS_ONE SLAVE_8259_PORT+1
#define ICW3_SLAVE    0x02
void send_eoi(uint32_t irq_num)
{
	if(!(8 & irq_num))
	{
		inb(0x21);
		outb(master_mask_1,0x21);//data, port format
		outb(irq_num+EOI, 0x20);//data, port format
         }
         else
         {
                  inb(0xA1);
		  outb(slave_mask_1,0xA1); //data, port format
		  outb(ICW3_SLAVE+0x60,0xA0);//data, port format
         }
}

Re: keyboard interrupt working only once

Posted: Sun Apr 01, 2012 11:33 am
by Brendan
Hi,

Code: Select all

SET_IDT_ENTRY(idt[33],keyboard);

Code: Select all

void keyboard(){
asm( "cli;"
	"call keyboard_handler;"
         "iret;");
	}
IRQ occurs and the CPU uses IDT to start the IRQ handler "void keyboard()". The IRQ handler is a C function so it trashes registers and the stack during the C function's prologue, then does a pointless "cli" (that should never be needed - see Intel manuals about the difference between "interrupt gate" and "trap gate"). After that it calls the real IRQ handler (keyboard_handler). When the real keyboard handler returns you do an IRET to the trashed data that the C function's prologue left on the stack (that was meant to be cleaned up by the C function's epilogue which is never executed).

Code: Select all

void keyboard_handler(long exceptionNum)
Where did the "exceptionNum" argument come from? Surely this is a keyboard IRQ handler and therefore isn't an exception. Is it meant to be an IRQ number? In any case, why is it "long" when it's impossible to have more than 256 interrupts (surely "unsigned int" would be more sane)?

Code: Select all

{
	outportb_1(0x20,0x20);
Is this an EOI? Does this mean the IRQ handler is finished now?

Code: Select all

	a = inportb_1(0x61);
	outportb_1(0x61, a|0x80);
	outportb_1(0x61,a);
Why are you messing with IO port 0x61? Is this here in case you find an ancient XT computer that happens to support protected mode? This is not necessary for "AT or later" computers.

Code: Select all

	send_eoi(1);
Again?

Code: Select all

        asm("sti;");
Why bother - IRET will do that.

Code: Select all

#define charb_array(x,y)	(((unsigned char*)&(y))[x])
#define master_mask_1		(charb_array(0,0xFFFF))
#define slave_mask_1		(charb_array(1,0xFFFF))
What is this idiotic "charb_array()" mess supposed to achieve?

Code: Select all

#define MASTER_8259_PORT_PLUS_ONE MASTER_8259_PORT+1
#define SLAVE_8259_PORT_PLUS_ONE SLAVE_8259_PORT+1
You could probably just do this:

Code: Select all

#define ONE 1

Code: Select all

#define ICW3_SLAVE    0x02
void send_eoi(uint32_t irq_num)
{
	if(!(8 & irq_num))
	{
		inb(0x21);
		outb(master_mask_1,0x21);//data, port format
		outb(irq_num+EOI, 0x20);//data, port format
         }
         else
         {
                  inb(0xA1);
		  outb(slave_mask_1,0xA1); //data, port format
		  outb(ICW3_SLAVE+0x60,0xA0);//data, port format
         }
}
Why are you messing with PIC masks?

For IRQ numbers 8 to 15, you have to send the EOI to the slave PIC and another to the master PIC.

Do you realise there's 9 different "#define" statements, and you're still using "magic numbers" like 0x60, 0xA1, 0xA0, etc?



Cheers,

Brendan

Re: keyboard interrupt working only once

Posted: Sun Apr 01, 2012 12:05 pm
by aki522
so its better to use assembly rather than using inline assembly in void keyboard() to not mess up the stack?

Re: keyboard interrupt working only once

Posted: Sun Apr 01, 2012 12:25 pm
by Brendan
Hi,
aki522 wrote:so its better to use assembly rather than using inline assembly in void keyboard() to not mess up the stack?
Yes.

Most people have "assembly stubs" that call C IRQ handlers, like:

Code: Select all

IRQ1handler:

    ;Save interrupted code's stuff

    pushad
    push ds
    push es

    ;Setup stuff that the kernel's code expects

    mov ax,KERNEL_DATA_SEG
    mov ds,ax
    mov es,ax

    ;Call the C handler

    call keyboard_handler      ;"void keyboard_handler(void);"

    ;Do the EOI

    push dword 1               ;Push "IRQ number" argument
    call sendEOI               ;"void sendEOI(int IRQnumber);"
    add esp,4                  ;Clean up stack (C calling conventions)

    ;Restore interrupted code's stuff and return to interrupted code

    pop ds
    pop es
    popad
    iret
Because you're probably going to want 16 of these, some people would create a macro and do something like this:

Code: Select all

IRQ0handler:   IRQ_STUB(0)
IRQ1handler:   IRQ_STUB(1)
IRQ3handler:   IRQ_STUB(3)
IRQ4handler:   IRQ_STUB(4)
IRQ5handler:   IRQ_STUB(5)
The alternative is to have a generic IRQ handler with smaller stubs. For example:

Code: Select all

IRQ0handler:
    push dword 0
    jmp commonHandler

IRQ1handler:
    push dword 1
    jmp commonHandler

commonHandler:

    ;Save interrupted code's stuff

    pushad
    push ds
    push es

    ;Setup stuff that the kernel's code expects

    mov ax,KERNEL_DATA_SEG
    mov ds,ax
    mov es,ax

    ;Call the C handler

    call keyboard_handler      ;"void keyboard_handler(void);"

    ;Do the EOI

    mov eax,[esp+something]    ;Get IRQ number
    push eax                   ;Push "IRQ number" argument
    call sendEOI               ;"void sendEOI(int IRQnumber);"
    add esp,4                  ;Clean up stack (C calling conventions)

    ;Restore interrupted code's stuff and return to interrupted code

    pop ds
    pop es
    popad
    add esp,4                  ;Don't forget to remove the original IRQ number!
    iret
Of course in both of these cases you're probably going to want to use function pointers for calling the C IRQ handler eventually (e.g. once you get to PCI and/or IO APICs, where you can't hard-code things like this).


Cheers,

Brendan