Something wrong with KEYBOARD 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
User avatar
negcit.K
Member
Member
Posts: 34
Joined: Fri Dec 07, 2007 9:57 am

Something wrong with KEYBOARD driver

Post by negcit.K »

I have written a keyboard driver file, keyboard.c, that just do nothing but display the general_protection interrupt information.
i want it to handle a keybaord interrupt but it only handled the general_protection interrupt which is not my except. Now i have no ideal how to fix it well, and i really need your help. Thanks.

there is the code i wrote:

Scancode set 2 is what i wanna take.
I set the idt of IRQ1 in the file trap.c set_trap_gate(0x21,keyboard_interrupt)
And i do enable the IRQ1 in the function con_write(). outb_p(inb_p(0x21)&0xfd, 0x21);
And i do set the interrupt bit in main function init.c sti();

Code: Select all

#include <keyboard.h>
#include <asm/io.h>


static unsigned char scancode;
static unsigned char brkflag;
static unsigned char mode;
static unsigned char e0;
static unsigned char key;
static unsigned char leds;


unsigned char key_map[] = {
 /*0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f*/
  0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 9 ,'`',/*00-0F*/
  0x0,0x0,0x0,0x0,0x0,'q','1',0x0,0x0,0x0,'z','s','a','w','2',0x0,/*10-1F*/
  0x0,'c','x','d','e','4','3',0x0,0x0,32, 'v','f','t','r','5',0x0,/*20-2F*/
  0x0,'n','b','k','g','y','6',0x0,0x0,0x0,'m','j','u','7','8',0x0,/*30-3F*/
  0x0,',','k','i','o','0','9',0x0,0x0,'.','/','l',';','p','-',0x0,/*40-4F*/
  0x0,0x0,'\'',0x0,'[','=',0x0,0x0,0x0,0x0,13,']',0x0,'\\',0x0,0x0,/*50-5F*/
  0x0,0x0,0x0,0x0,0x0,0x0, 8 ,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,/*60-6F*/
  '0','.','2','5','6','8',0x0,0x0,0x0,'+','3','1','*','9',0x0,0x0,/*70-7F*/
  0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};/*80-8F*/

unsigned char shift_map[] = {
 /*0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f*/
  0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 9 ,'~',/*00-0F*/
  0x0,0x0,0x0,0x0,0x0,'Q','!',0x0,0x0,0x0,'Z','S','A','W','@',0x0,/*10-1F*/
  0x0,'C','X','D','E','$','#',0x0,0x0,32, 'V','F','T','R','%',0x0,/*20-2F*/
  0x0,'N','B','K','G','Y','^',0x0,0x0,0x0,'M','J','U','&','*',0x0,/*30-3F*/
  0x0,'<','K','I','O',')','(',0x0,0x0,'>','?','L',':','p','_',0x0,/*40-4F*/
  0x0,0x0,'\"',0x0,'{','+',0x0,0x0,0x0,0x0,13,'}',0x0,'|',0x0,0x0,/*50-5F*/
  0x0,0x0,0x0,0x0,0x0,0x0, 8 ,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,/*60-6F*/
  0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,/*70-7F*/
  0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};/*80-8F*/

unsigned char alt_map[] = {
 /*0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f*/
  0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 9 ,'`',/*00-0F*/
  0x0,0x0,0x0,0x0,0x0,'q','1',0x0,0x0,0x0,'z','s','a','w','2',0x0,/*10-1F*/
  0x0,'c','x','d','e','4','3',0x0,0x0,32, 'v','f','t','r','5',0x0,/*20-2F*/
  0x0,'n','b','k','g','y','6',0x0,0x0,0x0,'m','j','u','7','8',0x0,/*30-3F*/
  0x0,',','k','i','o','0','9',0x0,0x0,'.','/','l',';','p','-',0x0,/*40-4F*/
  0x0,0x0,'\'',0x0,'[','=',0x0,0x0,0x0,0x0,13,']',0x0,'\\',0x0,0x0,/*50-5F*/
  0x0,0x0,0x0,0x0,0x0,0x0, 8 ,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,/*60-6F*/
  '0','.','2','5','6','8',0x0,0x0,0x0,'+','3','1','*','9',0x0,0x0,/*70-7F*/
  0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};/*80-8F*/



static void kb_wait(void);

static void non(void)
{
  /* just doing nothing */
}

static void fun(void)
{
  /* Not implement */
}

static void prt(void)
{
  unsigned char *map;
  
  if (mode & (RALT | LALT))
    map = alt_map;
  else if (mode & (RSHIFT | LSHIFT))
    map = shift_map;
  else
    map = key_map;
  
  key = *(map + scancode);
  
  if (mode & (LCTRL | RCTRL | CAPS_STATE))
    if (key >= 'a' && key <= '}')
      key -= 32;
  if (mode & (LCTRL | RCTRL))
      if (key >= 64 && key <= 64 + 32)
	key -= 64;
    
  printk("%c",key);
}

static void ctl(void)
{
  unsigned char temp;
  temp = LCTRL;

  if (e0 & E0)
    temp <<= 1;

  mode |= temp;
}

static void alt(void)
{
  unsigned char temp;

  if (e0 & E0)
    temp <<= 1;
  
  mode |= temp;
}

static void sft(void)
{
  if (scancode == 0x12)
    mode |= LSHIFT;
  else if (scancode == 0x59)
    mode |= RSHIFT;
}

static void cur(void)
{
  /*Not implement*/
}

static void set_leds(void)
{
  kb_wait();
  outb(0xed, 0x60);   /* set leds command */
  kb_wait();
  outb(leds, 0x60);   /* send param */
}

static void sroll(void)
{
  leds ^= SCROLL_LOCK;
  set_leds();
}

static void num_lock(void)
{
  leds ^= NUM_LOCK;
  set_leds();
}

static void cap(void)
{
  leds ^= CAPS_LOCK;
     set_leds();
}

static void kb_wait(void)
{
  while (inb(0x64) & 0x02)
    ;
}



void keyboard_interrupt(void)
{
  unsigned char a;

  void (*kfun_table[])(void) = {
    /*0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f*/
    non,fun,non,fun,fun,fun,fun,fun,non,fun,fun,fun,fun,prt,prt,non,/*00-0F*/
    non,alt,alt,non,ctl,prt,prt,non,non,non,prt,prt,prt,prt,prt,non,/*10-1F*/
    non,prt,prt,prt,prt,prt,prt,non,non,prt,prt,prt,prt,prt,prt,prt,/*20-2F*/
    non,prt,prt,prt,prt,prt,prt,non,non,non,prt,prt,prt,prt,prt,prt,/*30-3F*/
    non,prt,prt,prt,prt,prt,prt,non,non,prt,prt,prt,prt,prt,prt,prt,/*40-4F*/
    non,non,prt,non,prt,prt,non,non,cap,sft,prt,prt,non,prt,non,non,/*50-5F*/
    non,non,non,non,non,non,prt,non,non,cur,non,cur,cur,non,non,non,/*60-6F*/
    cur,cur,cur,cur,cur,cur,cur,cur,cur,cur,cur,cur,cur,cur,cur,cur,/*70-7F*/
    non,non,non,fun,non,non,non,non,non,non,non,non,non,non,non,non /*80-8F*/
  };

  scancode = inb(0x60);

  if (brkflag && scancode == 0x11) {
    if (e0 & E0) {
      mode &= ~RALT;
      e0   &= ~E0;
    }else
      mode &= ~LALT;
    brkflag = 0;
  }else if (brkflag && scancode == 0x14) {
    if (e0 & E0) {
      mode &= ~RCTRL;
      e0   &= ~E0;
    }else
      mode &= ~LCTRL;
    brkflag = 0;
  }else if (brkflag) {
    e0 = 0;
    brkflag = 0;
  }else if (scancode == 0xe0)
    e0 |= E0;
  else if (scancode == 0xe1)
    e0 |= E1;
  else if (scancode == 0xF0)       /* set brkflag  */
    brkflag = 1;
  else
    (*kfun_table[scancode])();

  a = inb(0x61);		/* disable KB   *
  outb(a|0x80, 0x61);		/* enable  KB   */
  outb(a&0x7f, 0x61);		/* E O I        */
       
    
                
     
  
 printk("%c",key);
  

}


and the macro values of mode, leds, e0 are defined at keyboard.h

Code: Select all

#ifndef KEYBOARD_H
#define KEYBOADR_H

/* macro of mode */
#define LSHIFT      0x01
#define RSHIFT      0x02
#define LCTRL       0x04
#define RCTRL       0x08
#define LALT        0x10
#define RALT        0x20
#define CAPS_STATE  0x40
#define CAPS        0x80

/* macro of leds */
#define SCROLL_LOCK 0x01
#define NUM_LOCK    0x02
#define CAPS_LOCK   0x04


/* macro of eo */
#define E0          0x01
#define E1          0x02



void keyboard_interrupt(void);			
		      


#endif  /*keyboard.h*/


Thanks a lot for you reading here.
Last edited by negcit.K on Thu Apr 17, 2008 2:56 am, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

[wiki]Category:FAQ[/wiki] - I suggest you try that first.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
edfed
Member
Member
Posts: 42
Joined: Wed Apr 09, 2008 5:44 pm
Location: Mars

Post by edfed »

before to be able to manage a keyboard from PM, you sould reprogramm the PIC to point to the vector you want for keybord.

in PM, the 16 first interrupts are for internal errors managment.

i'm pretty sure that each time you hit a key, it generate the error n°9 coprossessor segment overun (IRQ 9).

Code: Select all

if you want to know more about it, please seek documentations and yeahhh!
remapirq:
        mov     al,11h
        out     20h,al
        out     0a0h,al
        mov     al,32       
        out     21h,al
        mov     al,40
        out     0a1h,al
        mov     al,4
        out     21h,al
        mov     al,2
        out     0a1h,al
        mov     al,1
        out     21h,al
        out     0a1h,al
        mov     al,0fbh
        out     21h,al
        mov     al,0ffh
        out     0a1h,al
        ret
welcome in my dream.
User avatar
negcit.K
Member
Member
Posts: 34
Joined: Fri Dec 07, 2007 9:57 am

Post by negcit.K »

Combuster wrote:...
Which part do you want me to search?

before to be able to manage a keyboard from PM, you sould reprogramm the PIC to point to the vector you want for keybord.
I dit it
Post Reply