q40_keyb.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:14k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/drivers/char/q40_keyb.c
  3.  *
  4.  */
  5. #include <linux/config.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/sched.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/tty.h>
  10. #include <linux/mm.h>
  11. #include <linux/keyboard.h>
  12. #include <linux/signal.h>
  13. #include <linux/ioport.h>
  14. #include <linux/init.h>
  15. #include <linux/kbd_ll.h>
  16. #include <linux/kbd_kern.h>
  17. #include <linux/delay.h>
  18. #include <linux/sysrq.h>
  19. #include <linux/random.h>
  20. #include <linux/poll.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/slab.h>
  23. #include <asm/keyboard.h>
  24. #include <asm/bitops.h>
  25. #include <asm/io.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/q40_master.h>
  28. #include <asm/irq.h>
  29. #include <asm/q40ints.h>
  30. /* Simple translation table for the SysRq keys */
  31. #define SYSRQ_KEY 0x54
  32. #ifdef CONFIG_MAGIC_SYSRQ
  33. unsigned char q40kbd_sysrq_xlate[128] =
  34. "00331234567890-=177t" /* 0x00 - 0x0f */
  35. "qwertyuiop[]r00as" /* 0x10 - 0x1f */
  36. "dfghjkl;'`00\zxcv" /* 0x20 - 0x2f */
  37. "bnm,./00*00 00201202203204205" /* 0x30 - 0x3f */
  38. "2062072102112120000789-456+1" /* 0x40 - 0x4f */
  39. "230177000021321400000000000000000000" /* 0x50 - 0x5f */
  40. "r00/"; /* 0x60 - 0x6f */
  41. #endif
  42. /* Q40 uses AT scancodes - no way to change it. so we have to translate ..*/
  43. /* 0x00 means not a valid entry or no conversion known                    */
  44. unsigned static char q40cl[256] =
  45. {/* 0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   a,   b,   c,   d,   e,   f, */
  46.  0x00,0x43,0x00,0x3f,0x3d,0x3b,0x3c,0x58,0x00,0x44,0x42,0x40,0x3e,0x0f,0x29,0x00,     /* 0x00 - 0x0f */
  47.  0x00,0x38,0x2a,0x00,0x1d,0x10,0x02,0x00,0x00,0x00,0x2c,0x1f,0x1e,0x11,0x03,0x00,     /* 0x10 - 0x1f */
  48.  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 ???*/
  49.  0x00,0x31,0x30,0x23,0x22,0x15,0x07,0x00,0x24,0x00,0x32,0x24,0x16,0x08,0x09,0x00,     /* 0x30 - 0x3f */
  50.  0x00,0x33,0x25,0x17,0x18,0x0b,0x0a,0x00,0x00,0x34,0x35,0x26,0x27,0x19,0x0c,0x00,     /* 0x40 - 0x4f */
  51.  0x00,0x00,0x28,0x00,0x1a,0x0d,0x00,0x00,0x3a,0x36,0x1c,0x1b,0x00,0x2b,0x00,0x00,     /* 0x50 - 0x5f*/
  52.  0x00,0x56,0x00,0x00,0x00,0x00,0x0e,0x00,0x00,0x4f,0x00,0x4b,0x47,0x00,0x00,0x00,     /* 0x60 - 0x6f */
  53.  0x52,0x53,0x50,0x4c,0x4d,0x48,0x01,0x45,0x57,0x4e,0x51,0x4a,0x37,0x49,0x46,0x00,     /* 0x70 - 0x7f */
  54.  0x00,0x00,0x00,0x41,0x37,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x80 - 0x8f  0x84/0x37 is SySrq*/
  55.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x90 - 0x9f */
  56.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xa0 - 0xaf */
  57.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xb0 - 0xbf */
  58.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xc0 - 0xcf */
  59.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xd0 - 0xdf */
  60.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xe0 - 0xef */
  61.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xf0 - 0xff */
  62. };
  63. /* another table, AT 0xe0 codes to PC 0xe0 codes, 
  64.    0xff special entry for SysRq - DROPPED right now  */
  65. static unsigned char q40ecl[]=
  66. {/* 0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   a,   b,   c,   d,   e,   f, */
  67.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x00 - 0x0f*/
  68.  0x00,0x38,0x2a,0x00,0x1d,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x10 - 0x1f */
  69.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x20 - 0x2f*/
  70.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x30 - 0x3f*/
  71.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x35,0x00,0x00,0x00,0x00,0x00,     /* 0x40 - 0x4f*/
  72.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x1c,0x00,0x00,0x00,0x00,0x00,     /* 0x50 - 0x5f*/
  73.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x4f,0x00,0x4b,0x47,0x00,0x00,0x00,     /* 0x60 - 0x6f*/
  74.  0x52,0x53,0x50,0x00,0x4d,0x48,0x00,0x00,0x00,0x00,0x51,0x00,0x00,0x49,0x00,0x00,     /* 0x70 - 0x7f*/
  75.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x80 - 0x8f*/
  76.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0x90 - 0x9f*/
  77.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xa0 - 0xaf*/
  78.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xb0 - 0xbf*/
  79.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xc0 - 0xcf*/
  80.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xd0 - 0xdf*/
  81.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,     /* 0xe0 - 0xef*/
  82.  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00      /* 0xf0 - 0xff*/
  83. };
  84. static spinlock_t kbd_controller_lock = SPIN_LOCK_UNLOCKED;
  85. /*
  86.  * Translation of escaped scancodes to keycodes.
  87.  * This is now user-settable.
  88.  * The keycodes 1-88,96-111,119 are fairly standard, and
  89.  * should probably not be changed - changing might confuse X.
  90.  * X also interprets scancode 0x5d (KEY_Begin).
  91.  *
  92.  * For 1-88 keycode equals scancode.
  93.  */
  94. #define E0_KPENTER 96
  95. #define E0_RCTRL   97
  96. #define E0_KPSLASH 98
  97. #define E0_PRSCR   99
  98. #define E0_RALT    100
  99. #define E0_BREAK   101  /* (control-pause) */
  100. #define E0_HOME    102
  101. #define E0_UP      103
  102. #define E0_PGUP    104
  103. #define E0_LEFT    105
  104. #define E0_RIGHT   106
  105. #define E0_END     107
  106. #define E0_DOWN    108
  107. #define E0_PGDN    109
  108. #define E0_INS     110
  109. #define E0_DEL     111
  110. #define E1_PAUSE   119
  111. /*
  112.  * The keycodes below are randomly located in 89-95,112-118,120-127.
  113.  * They could be thrown away (and all occurrences below replaced by 0),
  114.  * but that would force many users to use the `setkeycodes' utility, where
  115.  * they needed not before. It does not matter that there are duplicates, as
  116.  * long as no duplication occurs for any single keyboard.
  117.  */
  118. #define SC_LIM 89
  119. #define FOCUS_PF1 85           /* actual code! */
  120. #define FOCUS_PF2 89
  121. #define FOCUS_PF3 90
  122. #define FOCUS_PF4 91
  123. #define FOCUS_PF5 92
  124. #define FOCUS_PF6 93
  125. #define FOCUS_PF7 94
  126. #define FOCUS_PF8 95
  127. #define FOCUS_PF9 120
  128. #define FOCUS_PF10 121
  129. #define FOCUS_PF11 122
  130. #define FOCUS_PF12 123
  131. #define JAP_86     124
  132. /* tfj@olivia.ping.dk:
  133.  * The four keys are located over the numeric keypad, and are
  134.  * labelled A1-A4. It's an rc930 keyboard, from
  135.  * Regnecentralen/RC International, Now ICL.
  136.  * Scancodes: 59, 5a, 5b, 5c.
  137.  */
  138. #define RGN1 124
  139. #define RGN2 125
  140. #define RGN3 126
  141. #define RGN4 127
  142. static unsigned char high_keys[128 - SC_LIM] = {
  143.   RGN1, RGN2, RGN3, RGN4, 0, 0, 0,                   /* 0x59-0x5f */
  144.   0, 0, 0, 0, 0, 0, 0, 0,                            /* 0x60-0x67 */
  145.   0, 0, 0, 0, 0, FOCUS_PF11, 0, FOCUS_PF12,          /* 0x68-0x6f */
  146.   0, 0, 0, FOCUS_PF2, FOCUS_PF9, 0, 0, FOCUS_PF3,    /* 0x70-0x77 */
  147.   FOCUS_PF4, FOCUS_PF5, FOCUS_PF6, FOCUS_PF7,        /* 0x78-0x7b */
  148.   FOCUS_PF8, JAP_86, FOCUS_PF10, 0                   /* 0x7c-0x7f */
  149. };
  150. /* BTC */
  151. #define E0_MACRO   112
  152. /* LK450 */
  153. #define E0_F13     113
  154. #define E0_F14     114
  155. #define E0_HELP    115
  156. #define E0_DO      116
  157. #define E0_F17     117
  158. #define E0_KPMINPLUS 118
  159. /*
  160.  * My OmniKey generates e0 4c for  the "OMNI" key and the
  161.  * right alt key does nada. [kkoller@nyx10.cs.du.edu]
  162.  */
  163. #define E0_OK 124
  164. /*
  165.  * New microsoft keyboard is rumoured to have
  166.  * e0 5b (left window button), e0 5c (right window button),
  167.  * e0 5d (menu button). [or: LBANNER, RBANNER, RMENU]
  168.  * [or: Windows_L, Windows_R, TaskMan]
  169.  */
  170. #define E0_MSLW 125
  171. #define E0_MSRW 126
  172. #define E0_MSTM 127
  173. /* this can be changed using setkeys : */
  174. static unsigned char e0_keys[128] = {
  175.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x00-0x07 */
  176.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x08-0x0f */
  177.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x10-0x17 */
  178.   0, 0, 0, 0, E0_KPENTER, E0_RCTRL, 0, 0,       /* 0x18-0x1f */
  179.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x20-0x27 */
  180.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x28-0x2f */
  181.   0, 0, 0, 0, 0, E0_KPSLASH, 0, E0_PRSCR,       /* 0x30-0x37 */
  182.   E0_RALT, 0, 0, 0, 0, E0_F13, E0_F14, E0_HELP,       /* 0x38-0x3f */
  183.   E0_DO, E0_F17, 0, 0, 0, 0, E0_BREAK, E0_HOME,       /* 0x40-0x47 */
  184.   E0_UP, E0_PGUP, 0, E0_LEFT, E0_OK, E0_RIGHT, E0_KPMINPLUS, E0_END,/* 0x48-0x4f */
  185.   E0_DOWN, E0_PGDN, E0_INS, E0_DEL, 0, 0, 0, 0,       /* 0x50-0x57 */
  186.   0, 0, 0, E0_MSLW, E0_MSRW, E0_MSTM, 0, 0,       /* 0x58-0x5f */
  187.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x60-0x67 */
  188.   0, 0, 0, 0, 0, 0, 0, E0_MACRO,       /* 0x68-0x6f */
  189.   0, 0, 0, 0, 0, 0, 0, 0,       /* 0x70-0x77 */
  190.   0, 0, 0, 0, 0, 0, 0, 0       /* 0x78-0x7f */
  191. };
  192. int q40kbd_setkeycode(unsigned int scancode, unsigned int keycode)
  193. {
  194. if (scancode < SC_LIM || scancode > 255 || keycode > 127)
  195.   return -EINVAL;
  196. if (scancode < 128)
  197.   high_keys[scancode - SC_LIM] = keycode;
  198. else
  199.   e0_keys[scancode - 128] = keycode;
  200. return 0;
  201. }
  202. int q40kbd_getkeycode(unsigned int scancode)
  203. {
  204. return
  205.   (scancode < SC_LIM || scancode > 255) ? -EINVAL :
  206.   (scancode < 128) ? high_keys[scancode - SC_LIM] :
  207.     e0_keys[scancode - 128];
  208. }
  209. #define disable_keyboard()
  210. #define enable_keyboard()
  211. int q40kbd_translate(unsigned char scancode, unsigned char *keycode,
  212.     char raw_mode)
  213. {
  214. static int prev_scancode;
  215. /* special prefix scancodes.. */
  216. if (scancode == 0xe0 || scancode == 0xe1) {
  217. prev_scancode = scancode;
  218. return 0;
  219. }
  220. /* 0xFF is sent by a few keyboards, ignore it. 0x00 is error */
  221. if (scancode == 0x00 || scancode == 0xff) {
  222. prev_scancode = 0;
  223. return 0;
  224. }
  225. scancode &= 0x7f;
  226. if (prev_scancode) {
  227.   /*
  228.    * usually it will be 0xe0, but a Pause key generates
  229.    * e1 1d 45 e1 9d c5 when pressed, and nothing when released
  230.    */
  231.   if (prev_scancode != 0xe0) {
  232.       if (prev_scancode == 0xe1 && scancode == 0x1d) {
  233.   prev_scancode = 0x100;
  234.   return 0;
  235.       } else if (prev_scancode == 0x100 && scancode == 0x45) {
  236.   *keycode = E1_PAUSE;
  237.   prev_scancode = 0;
  238.       } else {
  239. #ifdef KBD_REPORT_UNKN
  240.   if (!raw_mode)
  241.     printk(KERN_INFO "keyboard: unknown e1 escape sequencen");
  242. #endif
  243.   prev_scancode = 0;
  244.   return 0;
  245.       }
  246.   } else {
  247.       prev_scancode = 0;
  248.       /*
  249.        *  The keyboard maintains its own internal caps lock and
  250.        *  num lock statuses. In caps lock mode E0 AA precedes make
  251.        *  code and E0 2A follows break code. In num lock mode,
  252.        *  E0 2A precedes make code and E0 AA follows break code.
  253.        *  We do our own book-keeping, so we will just ignore these.
  254.        */
  255.       /*
  256.        *  For my keyboard there is no caps lock mode, but there are
  257.        *  both Shift-L and Shift-R modes. The former mode generates
  258.        *  E0 2A / E0 AA pairs, the latter E0 B6 / E0 36 pairs.
  259.        *  So, we should also ignore the latter. - aeb@cwi.nl
  260.        */
  261.       if (scancode == 0x2a || scancode == 0x36)
  262. return 0;
  263.       if (e0_keys[scancode])
  264. *keycode = e0_keys[scancode];
  265.       else {
  266. #ifdef KBD_REPORT_UNKN
  267.   if (!raw_mode)
  268.     printk(KERN_INFO "keyboard: unknown scancode e0 %02xn",
  269.    scancode);
  270. #endif
  271.   return 0;
  272.       }
  273.   }
  274. } else if (scancode >= SC_LIM) {
  275.     /* This happens with the FOCUS 9000 keyboard
  276.        Its keys PF1..PF12 are reported to generate
  277.        55 73 77 78 79 7a 7b 7c 74 7e 6d 6f
  278.        Moreover, unless repeated, they do not generate
  279.        key-down events, so we have to zero up_flag below */
  280.     /* Also, Japanese 86/106 keyboards are reported to
  281.        generate 0x73 and 0x7d for  - and  | respectively. */
  282.     /* Also, some Brazilian keyboard is reported to produce
  283.        0x73 and 0x7e for  ? and KP-dot, respectively. */
  284.   *keycode = high_keys[scancode - SC_LIM];
  285.   if (!*keycode) {
  286.       if (!raw_mode) {
  287. #ifdef KBD_REPORT_UNKN
  288.   printk(KERN_INFO "keyboard: unrecognized scancode (%02x)"
  289.  " - ignoredn", scancode);
  290. #endif
  291.       }
  292.       return 0;
  293.   }
  294.   } else
  295.   *keycode = scancode;
  296.   return 1;
  297. }
  298. char q40kbd_unexpected_up(unsigned char keycode)
  299. {
  300. /* unexpected, but this can happen: maybe this was a key release for a
  301.    FOCUS 9000 PF key; if we want to see it, we have to clear up_flag */
  302. if (keycode >= SC_LIM || keycode == 85)
  303.     return 0;
  304. else
  305.     return 0200;
  306. }
  307. static int keyup=0;
  308. static int qprev=0;
  309. static void keyboard_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  310. {
  311. unsigned char status;
  312. spin_lock(&kbd_controller_lock);
  313. kbd_pt_regs = regs;
  314. status = Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG);
  315. if (status ) 
  316.   {
  317.     unsigned char scancode,qcode;
  318.     
  319.     qcode = master_inb(KEYCODE_REG);
  320.     
  321.     if (qcode != 0xf0)
  322.       {
  323. if (qcode == 0xe0)
  324.   {
  325.     qprev=0xe0;
  326.     handle_scancode(qprev , 1);
  327.     goto exit;
  328.   }
  329. scancode=qprev ? q40ecl[qcode] : q40cl[qcode];
  330. #if 0
  331. /* next line is last resort to hanlde some oddities */
  332. if (qprev && !scancode) scancode=q40cl[qcode];
  333. #endif
  334. qprev=0;
  335. if (!scancode)
  336.   {
  337.     printk("unknown scancode %xn",qcode);
  338.     goto exit;
  339.   }
  340. if (scancode==0xff)  /* SySrq */
  341.   scancode=SYSRQ_KEY;
  342. handle_scancode(scancode, ! keyup );
  343. keyup=0;
  344. tasklet_schedule(&keyboard_tasklet);
  345.       }
  346.     else
  347.       keyup=1;
  348.   }
  349. exit:
  350. spin_unlock(&kbd_controller_lock);
  351. master_outb(-1,KEYBOARD_UNLOCK_REG); /* keyb ints reenabled herewith */
  352. }
  353. #define KBD_NO_DATA (-1) /* No data */
  354. #define KBD_BAD_DATA (-2) /* Parity or other error */
  355. static int __init kbd_read_input(void)
  356. {
  357. int retval = KBD_NO_DATA;
  358. unsigned char status;
  359. status = Q40_IRQ_KEYB_MASK & master_inb(INTERRUPT_REG);
  360. if (status) {
  361. unsigned char data = master_inb(KEYCODE_REG);
  362. retval = data;
  363. master_outb(-1,KEYBOARD_UNLOCK_REG);
  364. }
  365. return retval;
  366. }
  367. static void __init kbd_clear_input(void)
  368. {
  369. int maxread = 100; /* Random number */
  370. do {
  371. if (kbd_read_input() == KBD_NO_DATA)
  372. break;
  373. } while (--maxread);
  374. }
  375. int __init q40kbd_init_hw(void)
  376. {
  377. /* Flush any pending input. */
  378. kbd_clear_input();
  379. /* Ok, finally allocate the IRQ, and off we go.. */
  380. request_irq(Q40_IRQ_KEYBOARD, keyboard_interrupt, 0, "keyboard", NULL);
  381. master_outb(-1,KEYBOARD_UNLOCK_REG);
  382. master_outb(1,KEY_IRQ_ENABLE_REG);
  383. return 0;
  384. }