文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>转:键盘驱动

转:键盘驱动

时间:2006-07-20  来源:lengxb

linux键盘驱动

有兴趣研究键盘驱动的朋友吗?欢迎跟贴:)

1。键盘和网卡类似,在/dev/目录下面没有对应于任何设备节点。也就是说,键盘不需要主次设备号。

2。键盘属于tty驱动范畴。对于tty驱动,都是一个IN+OUT成对出现的。象串口驱动。而键盘则是通过VT成对出现。IN是键盘,OUT则是fb。

3。内核参数,console=tty0。表明使用VT为tty。console=ttyS0。表明使用串口为tty.

4。历史原因造成了键盘编码的复杂性。由于键盘存在语言差异,不同的国家键盘可以大不同。因此出现了,扫描码,键盘码,ASCII等等。

5。嵌入式场合,很少会用到键盘。但是多会用到按键。这里,我是提倡使用键盘驱动的。当然,也可以基于misc驱动。只有键盘驱动才可以成为VT。

键盘驱动的流程:
中断驱动键盘矩阵
申请中断-》中断检测=》中断处理=》获取scancode=》hand_scancode=》tasklet_schlue.。

查询键盘矩阵
初始化定时器=》定时器溢出=》扫描获取scancode=》hand_scancode=》tasklet_schelue

偶做的试验:
在S3C44B0X的公板上,有4个按键,分别接到了EINT4,EINT5,EINT6,EINT7。通过对中断21号的处理程序,设置。
按键1=》l
按键2=》s
按键3=》回车
内核启动时,console=tty0。
内核启动后,LCD上有输出。进入shell.
单击1,2,3。
屏幕上>ls 命令可以正常执行。

签名:

嵌入式linux内核驱动开发

go on
     
图片签名
good02xaut的blog

amzhang 发表于 2006-6-30 09:59 LINUX|编程 ←返回版面

代码贴出来大家讨论一下嘛

 

weiglink 发表于 2006-7-3 07:54 LINUX|编程 ←返回版面

是可以有设备接点的

如果完全是自己写的键盘驱动的话,以前看到过别人这么写过
不过不是完全标准的键盘驱动,也可以挂标准键盘,只是大部分是键盘往上发东西


good02xaut 发表于 2006-7-3 08:39 LINUX|编程 ←返回版面

有设备节点就不是标准的键盘了

把键盘作为普通的char设备,可以采用设备节点。不过这种驱动,不是作为tty层出现的,console不支持其输入的。

我这里指得都是“完全标准的键盘驱动”:)

往键盘上发东西是什么意思啊?不明白。键盘是输入设备,主要是keycode的输入。

签名:

嵌入式linux内核驱动开发

go on
     
图片签名
good02xaut的blog

good02xaut 发表于 2006-7-3 08:40 LINUX|编程 ←返回版面

s3c44b0x_kbd.c第一部分

从char目录下的某个kbd.c改出来的:)

#include <linux/config.h>

#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/mm.h>
#include <linux/keyboard.h>
#include <linux/signal.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/kbd_ll.h>
#include <linux/kbd_kern.h>
#include <linux/delay.h>
#include <linux/sysrq.h>
#include <linux/random.h>
#include <linux/poll.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>

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

#include <asm/arch-S3C44B0X/irqs.h>
#include <asm/arch-S3C44B0X/s3c44b0x.h>
#include <asm/irq.h>

/* Simple translation table for the SysRq keys */

#define SYSRQ_KEY 0x54

#ifdef CONFIG_MAGIC_SYSRQ
unsigned char q40kbd_sysrq_xlate[128] =
    "\000\0331234567890-=\177\t"            /* 0x00 - 0x0f */
    "qwertyuiop[]\r\000as"                /* 0x10 - 0x1f */
    "dfghjkl;'`\000\\zxcv"                /* 0x20 - 0x2f */
    "bnm,./\000*\000 \000\201\202\203\204\205"    /* 0x30 - 0x3f */
    "\206\207\210\211\212\000\000789-456+1"        /* 0x40 - 0x4f */
    "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
    "\r\000/";                    /* 0x60 - 0x6f */
#endif

/* Q40 uses AT scancodes - no way to change it. so we have to translate ..*/
/* 0x00 means not a valid entry or no conversion known                    */

unsigned static char q40cl[256] =
{/* 0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   a,   b,   c,   d,   e,   f, */
0x00,0x43,0x00,0x3f,0x3d,0x3b,0x3c,0x58,0x00,0x44,0x42,0x40,0x3e,0x0f,0x29,0x00,     /* 0x00 - 0x0f */
0x00,0x38,0x2a,0x00,0x1d,0x10,0x02,0x00,0x00,0x00,0x2c,0x1f,0x1e,0x11,0x03,0x00,     /* 0x10 - 0x1f */
0x00,0x2e,0x2d,0x20,0x12,0x05,0x04,0x00,0x21,0x39,0x2f,0x21,0x14,0x13,0x06,0x00,     /* 0x20 - 0x2f  'f' is at 0x2b, what is 0x28 ???*/
0x00,0x31,0x30,0x23,0x22,0x15,0x07,0x00,0x24,0x00,0x32,0x24,0x16,0x08,0x09,0x00,     /* 0x30 - 0x3f */
0x00,0x33,0x25,0x17,0x18,0x0b,0x0a,0x00,0x00,0x34,0x35,0x26,0x27,0x19,0x0c,0x00,     /* 0x40 - 0x4f */
0x00,0x00,0x28,0x00,0x1a,0x0d,0x00,0x00,0x3a,0x36,0x1c,0x1b,0x00,0x2b,0x00,0x00,     /* 0x50 - 0x5f*/
0x00,0x56,0x00,0x00,0x00,0x00,0x0e,0x00,0x00,0x4f,0x00,0x4b,0x47,0x00,0x00,0x00,     /* 0x60 - 0x6f */
0x52,0x53,0x50,0x4c,0x4d,0x48,0x01,0x45,0x57,0x4e,0x51,0x4a,0x37,0x49,0x46,0x00,     /* 0x70 - 0x7f */
0x00,0x00,0x00,0x41,0x37,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x80 - 0x8f  0x84/0x37 is SySrq*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x90 - 0x9f */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xa0 - 0xaf */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xb0 - 0xbf */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xc0 - 0xcf */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xd0 - 0xdf */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xe0 - 0xef */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xf0 - 0xff */
};

/* another table, AT 0xe0 codes to PC 0xe0 codes,
   0xff special entry for SysRq - DROPPED right now  */
static unsigned char q40ecl[]=
{/* 0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   a,   b,   c,   d,   e,   f, */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x00 - 0x0f*/
0x00,0x38,0x2a,0x00,0x1d,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x10 - 0x1f */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x20 - 0x2f*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x30 - 0x3f*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x35,0x00,0x00,0x00,0x00,0x00,     /* 0x40 - 0x4f*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x1c,0x00,0x00,0x00,0x00,0x00,     /* 0x50 - 0x5f*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x4f,0x00,0x4b,0x47,0x00,0x00,0x00,     /* 0x60 - 0x6f*/
0x52,0x53,0x50,0x00,0x4d,0x48,0x00,0x00,0x00,0x00,0x51,0x00,0x00,0x49,0x00,0x00,     /* 0x70 - 0x7f*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x80 - 0x8f*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x90 - 0x9f*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xa0 - 0xaf*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xb0 - 0xbf*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xc0 - 0xcf*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xd0 - 0xdf*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xe0 - 0xef*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00      /* 0xf0 - 0xff*/
};

static spinlock_t kbd_controller_lock = SPIN_LOCK_UNLOCKED;

/*
* Translation of escaped scancodes to keycodes.
* This is now user-settable.
* The keycodes 1-88,96-111,119 are fairly standard, and
* should probably not be changed - changing might confuse X.
* X also interprets scancode 0x5d (KEY_Begin).
*
* For 1-88 keycode equals scancode.
*/

#define E0_KPENTER 96
#define E0_RCTRL   97
#define E0_KPSLASH 98
#define E0_PRSCR   99
#define E0_RALT    100
#define E0_BREAK   101  /* (control-pause) */
#define E0_HOME    102
#define E0_UP      103
#define E0_PGUP    104
#define E0_LEFT    105
#define E0_RIGHT   106
#define E0_END     107
#define E0_DOWN    108
#define E0_PGDN    109
#define E0_INS     110
#define E0_DEL     111

#define E1_PAUSE   119

/*
* The keycodes below are randomly located in 89-95,112-118,120-127.
* They could be thrown away (and all occurrences below replaced by 0),
* but that would force many users to use the `setkeycodes' utility, where
* they needed not before. It does not matter that there are duplicates, as
* long as no duplication occurs for any single keyboard.
*/
#define SC_LIM 89

#define FOCUS_PF1 85           /* actual code! */
#define FOCUS_PF2 89
#define FOCUS_PF3 90
#define FOCUS_PF4 91
#define FOCUS_PF5 92
#define FOCUS_PF6 93
#define FOCUS_PF7 94
#define FOCUS_PF8 95
#define FOCUS_PF9 120
#define FOCUS_PF10 121
#define FOCUS_PF11 122
#define FOCUS_PF12 123

#define JAP_86     124
/* [email protected]:
* The four keys are located over the numeric keypad, and are
* labelled A1-A4. It's an rc930 keyboard, from
* Regnecentralen/RC International, Now ICL.
* Scancodes: 59, 5a, 5b, 5c.
*/
#define RGN1 124
#define RGN2 125
#define RGN3 126
#define RGN4 127

static unsigned char high_keys[128 - SC_LIM] = {
  RGN1, RGN2, RGN3, RGN4, 0, 0, 0,                   /* 0x59-0x5f */
  0, 0, 0, 0, 0, 0, 0, 0,                            /* 0x60-0x67 */
  0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12,          /* 0x68-0x6f */
  0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3,    /* 0x70-0x77 */
  FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7,        /* 0x78-0x7b */
  FOCUS_PF8, JAP_86, FOCUS_PF10, 0                   /* 0x7c-0x7f */
};

/* BTC */
#define E0_MACRO   112
/* LK450 */
#define E0_F13     113
#define E0_F14     114
#define E0_HELP    115
#define E0_DO      116
#define E0_F17     117
#define E0_KPMINPLUS 118
/*
* My OmniKey generates e0 4c for  the "OMNI" key and the
* right alt key does nada. [[email protected]]
*/
#define E0_OK    124
/*
* New microsoft keyboard is rumoured to have
* e0 5b (left window button), e0 5c (right window button),
* e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
* [or: Windows_L, Windows_R, TaskMan]
*/
#define E0_MSLW    125
#define E0_MSRW    126
#define E0_MSTM    127

签名:

嵌入式linux内核驱动开发

go on
     
图片签名
good02xaut的blog

good02xaut 发表于 2006-7-3 08:41 LINUX|编程 ←返回版面

continue

/* this can be changed using setkeys : */
static unsigned char e0_keys[128] = {
  0, 0, 0, 0, 0, 0, 0, 0,                  /* 0x00-0x07 */
  0, 0, 0, 0, 0, 0, 0, 0,                  /* 0x08-0x0f */
  0, 0, 0, 0, 0, 0, 0, 0,                  /* 0x10-0x17 */
  0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0,          /* 0x18-0x1f */
  0, 0, 0, 0, 0, 0, 0, 0,                  /* 0x20-0x27 */
  0, 0, 0, 0, 0, 0, 0, 0,                  /* 0x28-0x2f */
  0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR,          /* 0x30-0x37 */
  E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP,          /* 0x38-0x3f */
  E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME,          /* 0x40-0x47 */
  E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
  E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0,          /* 0x50-0x57 */
  0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0,          /* 0x58-0x5f */
  0, 0, 0, 0, 0, 0, 0, 0,                  /* 0x60-0x67 */
  0, 0, 0, 0, 0, 0, 0, E0_MACRO,              /* 0x68-0x6f */
  0, 0, 0, 0, 0, 0, 0, 0,                  /* 0x70-0x77 */
  0, 0, 0, 0, 0, 0, 0, 0                  /* 0x78-0x7f */
};

int q40kbd_setkeycode(unsigned int scancode, unsigned int keycode)
{
    if (scancode < SC_LIM || scancode > 255 || keycode > 127)
      return -EINVAL;
    if (scancode < 128)
      high_keys[scancode - SC_LIM] = keycode;
    else
      e0_keys[scancode - 128] = keycode;
    return 0;
}

int q40kbd_getkeycode(unsigned int scancode)
{
    return
      (scancode < SC_LIM || scancode > 255) ? -EINVAL :
      (scancode < 128) ? high_keys[scancode - SC_LIM] :
        e0_keys[scancode - 128];
}

#define disable_keyboard()    
#define enable_keyboard()    

int q40kbd_translate(unsigned char scancode, unsigned char *keycode,
            char raw_mode)
{
    static int prev_scancode;

    /* special prefix scancodes.. */
    if (scancode == 0xe0 || scancode == 0xe1) {
        prev_scancode = scancode;
        return 0;
    }

    /* 0xFF is sent by a few keyboards, ignore it. 0x00 is error */
    if (scancode == 0x00 || scancode == 0xff) {
        prev_scancode = 0;
        return 0;
    }

    scancode &= 0x7f;

    if (prev_scancode) {
      /*
       * usually it will be 0xe0, but a Pause key generates
       * e1 1d 45 e1 9d c5 when pressed, and nothing when released
       */
      if (prev_scancode != 0xe0) {
          if (prev_scancode == 0xe1 && scancode == 0x1d) {
          prev_scancode = 0x100;
          return 0;
          } else if (prev_scancode == 0x100 && scancode == 0x45) {
          *keycode = E1_PAUSE;
          prev_scancode = 0;
          } else {
#ifdef KBD_REPORT_UNKN
          if (!raw_mode)
            printk(KERN_INFO "keyboard: unknown e1 escape sequence\n");
#endif
          prev_scancode = 0;
          return 0;
          }
      } else {
          prev_scancode = 0;
          /*
           *  The keyboard maintains its own internal caps lock and
           *  num lock statuses. In caps lock mode E0 AA precedes make
           *  code and E0 2A follows break code. In num lock mode,
           *  E0 2A precedes make code and E0 AA follows break code.
           *  We do our own book-keeping, so we will just ignore these.
           */
          /*
           *  For my keyboard there is no caps lock mode, but there are
           *  both Shift-L and Shift-R modes. The former mode generates
           *  E0 2A / E0 AA pairs, the latter E0 B6 / E0 36 pairs.
           *  So, we should also ignore the latter. - [email protected]
           */
          if (scancode == 0x2a || scancode == 0x36)
        return 0;

          if (e0_keys[scancode])
        *keycode = e0_keys[scancode];
          else {
#ifdef KBD_REPORT_UNKN
          if (!raw_mode)
            printk(KERN_INFO "keyboard: unknown scancode e0 %02x\n",
               scancode);
#endif
          return 0;
          }
      }
    } else if (scancode >= SC_LIM) {
        /* This happens with the FOCUS 9000 keyboard
           Its keys PF1..PF12 are reported to generate
           55 73 77 78 79 7a 7b 7c 74 7e 6d 6f
           Moreover, unless repeated, they do not generate
           key-down events, so we have to zero up_flag below */
        /* Also, Japanese 86/106 keyboards are reported to
           generate 0x73 and 0x7d for \ - and \ | respectively. */
        /* Also, some Brazilian keyboard is reported to produce
           0x73 and 0x7e for \ ? and KP-dot, respectively. */

      *keycode = high_keys[scancode - SC_LIM];

      if (!*keycode) {
          if (!raw_mode) {
#ifdef KBD_REPORT_UNKN
          printk(KERN_INFO "keyboard: unrecognized scancode (%02x)"
             " - ignored\n", scancode);
#endif
          }
          return 0;
      }
     } else
      *keycode = scancode;
     return 1;
}

char q40kbd_unexpected_up(unsigned char keycode)
{
    /* unexpected, but this can happen: maybe this was a key release for a
       FOCUS 9000 PF key; if we want to see it, we have to clear up_flag */
    if (keycode >= SC_LIM || keycode == 85)
        return 0;
    else
        return 0200;
}

static int keyup=0;
static int qprev=0;

static void keyboard_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
    unsigned char status;
     unsigned char scancode;

    status=(*(unsigned char *)S3C44B0X_EXTINTPND);
    if(status&0x01){
    printk("eint 4 ok \n");
    scancode=0x26;        //l
    }
    else if(status&0x02)
    {
    printk("eint 5 ok \n");
    scancode=0x1f;        //s
    }
    else if(status&0x04)
    {
    printk("eint 6 ok \n");
    scancode=96;        //enter回车
    }
    else
    {
     printk("eint 7 ok \n");
    goto exit ;    
    }
    (*(unsigned int *)S3C44B0X_EXTINTPND)=0x0f;

    spin_lock(&kbd_controller_lock);
    kbd_pt_regs = regs;

    handle_scancode(scancode, ! keyup );
        keyup=0;
        tasklet_schedule(&keyboard_tasklet);
    

exit:
    spin_unlock(&kbd_controller_lock);
    
}

#define KBD_NO_DATA    (-1)    /* No data */
#define KBD_BAD_DATA    (-2)    /* Parity or other error */

* - 本贴最后修改时间:2006-7-3 8:44:59 修改者:good02xaut

签名:

嵌入式linux内核驱动开发

go on
     
图片签名
good02xaut的blog

good02xaut 发表于 2006-7-3 08:42 LINUX|编程 ←返回版面

continue


int __init s3c40b0x_kbd_init_hw(void)
{

    /* Flush any pending input. */
    //kbd_clear_input();

    // added by liusj
    (*(unsigned int *)S3C44B0X_PCONG) &= 0x00ff;
    
    (*(unsigned int *)S3C44B0X_PCONG) &= 0x00ff;
    
    
         (*(unsigned int *)S3C44B0X_PCONG) |= 0x0ff00;

     (*(unsigned int *)S3C44B0X_EXTINT) &=0x0000ffff;
    (*(unsigned int *)S3C44B0X_EXTINT) |=0x2222ffff;
    /*
    if((*(unsigned int *)S3C44B0X_PDATG)&0x08){
             (*(unsigned int *)S3C44B0X_PCONG) |= 0x000c0;
         }
    */
     printk("S3C44B0X keyboard driver 1.0 \n");
    /* Ok, finally allocate the IRQ, and off we go.. */
    request_irq(INT_EXTINT4_7, keyboard_interrupt, 0, "keyboard", NULL);
    
    //CLEAR_PEND_INT(INT_EXTINT4_7);
     //INT_ENABLE(INT_EXTINT4_7);
    return 0;
}

签名:

嵌入式linux内核驱动开发

go on
     
图片签名
good02xaut的blog

good02xaut 发表于 2006-7-3 09:00 LINUX|编程 ←返回版面

总结一下

1。此类键盘驱动必须依赖于char/keyboard.c文件。
2。代码中的有些函数,虽然不明白具体的功能,但是不影响开发
3。代码中加粗部分实现了“ls”命令
4。快速上手,实现tty层的键盘驱动就是如此简单。然而要彻底剖析键盘的原理,就必须研究Keyboard.c和tty_io.c了。

本文只是抛砖引玉,鼓励大家开发出更好的kbd驱动,并共享出来。
相关阅读 更多 +
排行榜 更多 +
僵尸你别拽安卓版

僵尸你别拽安卓版

飞行射击 下载
突击队行动团队最新版

突击队行动团队最新版

飞行射击 下载
仓鼠村庄最新版

仓鼠村庄最新版

模拟经营 下载