Subversion Repositories HelenOS

Rev

Rev 1566 | Rev 1644 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2001-2004 Jakub Jermar
  3.  * Copyright (C) 2006 Josef Cejka
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  * - Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  * - The name of the author may not be used to endorse or promote products
  16.  *   derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  */
  29.  
  30. #include <arch/kbd.h>
  31. #include <ipc/ipc.h>
  32.  
  33. #define SPECIAL     255
  34. #define KEY_RELEASE 0x80
  35.  
  36. /**
  37.  * These codes read from i8042 data register are silently ignored.
  38.  */
  39. #define IGNORE_CODE 0x7f
  40.  
  41. #define PRESSED_SHIFT       (1<<0)
  42. #define PRESSED_CAPSLOCK    (1<<1)
  43. #define LOCKED_CAPSLOCK     (1<<0)
  44.  
  45. /** Scancodes. */
  46. #define SC_ESC      0x01
  47. #define SC_BACKSPACE    0x0e
  48. #define SC_LSHIFT   0x2a
  49. #define SC_RSHIFT   0x36
  50. #define SC_CAPSLOCK 0x3a
  51. #define SC_SPEC_ESCAPE  0xe0
  52. #define SC_LEFTARR      0x4b
  53. #define SC_RIGHTARR     0x4d
  54. #define SC_UPARR        0x48
  55. #define SC_DOWNARR      0x50
  56. #define SC_DELETE       0x53
  57. #define SC_HOME         0x47
  58. #define SC_END          0x4f
  59.  
  60. #define FUNCTION_KEYS 0x100
  61.  
  62. static volatile int keyflags;       /**< Tracking of multiple keypresses. */
  63. static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
  64.  
  65. /** Primary meaning of scancodes. */
  66. static int sc_primary_map[] = {
  67.     SPECIAL, /* 0x00 */
  68.     SPECIAL, /* 0x01 - Esc */
  69.     '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
  70.     '\b', /* 0x0e - Backspace */
  71.     '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
  72.     SPECIAL, /* 0x1d - LCtrl */
  73.     'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
  74.     '`',
  75.     SPECIAL, /* 0x2a - LShift */
  76.     '\\',
  77.     'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
  78.     SPECIAL, /* 0x36 - RShift */
  79.     '*',
  80.     SPECIAL, /* 0x38 - LAlt */
  81.     ' ',
  82.     SPECIAL, /* 0x3a - CapsLock */
  83.     (FUNCTION_KEYS | 1), /* 0x3b - F1 */
  84.     (FUNCTION_KEYS | 2), /* 0x3c - F2 */
  85.     (FUNCTION_KEYS | 3), /* 0x3d - F3 */
  86.     (FUNCTION_KEYS | 4), /* 0x3e - F4 */
  87.     (FUNCTION_KEYS | 5), /* 0x3f - F5 */
  88.     (FUNCTION_KEYS | 6), /* 0x40 - F6 */
  89.     (FUNCTION_KEYS | 7), /* 0x41 - F7 */
  90.     (FUNCTION_KEYS | 8), /* 0x42 - F8 */
  91.     (FUNCTION_KEYS | 9), /* 0x43 - F9 */
  92.     (FUNCTION_KEYS | 10), /* 0x44 - F10 */
  93.     SPECIAL, /* 0x45 - NumLock */
  94.     SPECIAL, /* 0x46 - ScrollLock */
  95.     '7', '8', '9', '-',
  96.     '4', '5', '6', '+',
  97.     '1', '2', '3',
  98.     '0', '.',
  99.     SPECIAL, /* 0x54 - Alt-SysRq */
  100.     SPECIAL, /* 0x55 - F11/F12/PF1/FN */
  101.     SPECIAL, /* 0x56 - unlabelled key next to LAlt */
  102.     (FUNCTION_KEYS | 11), /* 0x57 - F11 */
  103.     (FUNCTION_KEYS | 12), /* 0x58 - F12 */
  104.     SPECIAL, /* 0x59 */
  105.     SPECIAL, /* 0x5a */
  106.     SPECIAL, /* 0x5b */
  107.     SPECIAL, /* 0x5c */
  108.     SPECIAL, /* 0x5d */
  109.     SPECIAL, /* 0x5e */
  110.     SPECIAL, /* 0x5f */
  111.     SPECIAL, /* 0x60 */
  112.     SPECIAL, /* 0x61 */
  113.     SPECIAL, /* 0x62 */
  114.     SPECIAL, /* 0x63 */
  115.     SPECIAL, /* 0x64 */
  116.     SPECIAL, /* 0x65 */
  117.     SPECIAL, /* 0x66 */
  118.     SPECIAL, /* 0x67 */
  119.     SPECIAL, /* 0x68 */
  120.     SPECIAL, /* 0x69 */
  121.     SPECIAL, /* 0x6a */
  122.     SPECIAL, /* 0x6b */
  123.     SPECIAL, /* 0x6c */
  124.     SPECIAL, /* 0x6d */
  125.     SPECIAL, /* 0x6e */
  126.     SPECIAL, /* 0x6f */
  127.     SPECIAL, /* 0x70 */
  128.     SPECIAL, /* 0x71 */
  129.     SPECIAL, /* 0x72 */
  130.     SPECIAL, /* 0x73 */
  131.     SPECIAL, /* 0x74 */
  132.     SPECIAL, /* 0x75 */
  133.     SPECIAL, /* 0x76 */
  134.     SPECIAL, /* 0x77 */
  135.     SPECIAL, /* 0x78 */
  136.     SPECIAL, /* 0x79 */
  137.     SPECIAL, /* 0x7a */
  138.     SPECIAL, /* 0x7b */
  139.     SPECIAL, /* 0x7c */
  140.     SPECIAL, /* 0x7d */
  141.     SPECIAL, /* 0x7e */
  142.     SPECIAL, /* 0x7f */
  143. };
  144.  
  145. /** Secondary meaning of scancodes. */
  146. static int sc_secondary_map[] = {
  147.     SPECIAL, /* 0x00 */
  148.     SPECIAL, /* 0x01 - Esc */
  149.     '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
  150.     SPECIAL, /* 0x0e - Backspace */
  151.     '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
  152.     SPECIAL, /* 0x1d - LCtrl */
  153.     'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
  154.     '~',
  155.     SPECIAL, /* 0x2a - LShift */
  156.     '|',
  157.     'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
  158.     SPECIAL, /* 0x36 - RShift */
  159.     '*',
  160.     SPECIAL, /* 0x38 - LAlt */
  161.     ' ',
  162.     SPECIAL, /* 0x3a - CapsLock */
  163.     SPECIAL, /* 0x3b - F1 */
  164.     SPECIAL, /* 0x3c - F2 */
  165.     SPECIAL, /* 0x3d - F3 */
  166.     SPECIAL, /* 0x3e - F4 */
  167.     SPECIAL, /* 0x3f - F5 */
  168.     SPECIAL, /* 0x40 - F6 */
  169.     SPECIAL, /* 0x41 - F7 */
  170.     SPECIAL, /* 0x42 - F8 */
  171.     SPECIAL, /* 0x43 - F9 */
  172.     SPECIAL, /* 0x44 - F10 */
  173.     SPECIAL, /* 0x45 - NumLock */
  174.     SPECIAL, /* 0x46 - ScrollLock */
  175.     '7', '8', '9', '-',
  176.     '4', '5', '6', '+',
  177.     '1', '2', '3',
  178.     '0', '.',
  179.     SPECIAL, /* 0x54 - Alt-SysRq */
  180.     SPECIAL, /* 0x55 - F11/F12/PF1/FN */
  181.     SPECIAL, /* 0x56 - unlabelled key next to LAlt */
  182.     SPECIAL, /* 0x57 - F11 */
  183.     SPECIAL, /* 0x58 - F12 */
  184.     SPECIAL, /* 0x59 */
  185.     SPECIAL, /* 0x5a */
  186.     SPECIAL, /* 0x5b */
  187.     SPECIAL, /* 0x5c */
  188.     SPECIAL, /* 0x5d */
  189.     SPECIAL, /* 0x5e */
  190.     SPECIAL, /* 0x5f */
  191.     SPECIAL, /* 0x60 */
  192.     SPECIAL, /* 0x61 */
  193.     SPECIAL, /* 0x62 */
  194.     SPECIAL, /* 0x63 */
  195.     SPECIAL, /* 0x64 */
  196.     SPECIAL, /* 0x65 */
  197.     SPECIAL, /* 0x66 */
  198.     SPECIAL, /* 0x67 */
  199.     SPECIAL, /* 0x68 */
  200.     SPECIAL, /* 0x69 */
  201.     SPECIAL, /* 0x6a */
  202.     SPECIAL, /* 0x6b */
  203.     SPECIAL, /* 0x6c */
  204.     SPECIAL, /* 0x6d */
  205.     SPECIAL, /* 0x6e */
  206.     SPECIAL, /* 0x6f */
  207.     SPECIAL, /* 0x70 */
  208.     SPECIAL, /* 0x71 */
  209.     SPECIAL, /* 0x72 */
  210.     SPECIAL, /* 0x73 */
  211.     SPECIAL, /* 0x74 */
  212.     SPECIAL, /* 0x75 */
  213.     SPECIAL, /* 0x76 */
  214.     SPECIAL, /* 0x77 */
  215.     SPECIAL, /* 0x78 */
  216.     SPECIAL, /* 0x79 */
  217.     SPECIAL, /* 0x7a */
  218.     SPECIAL, /* 0x7b */
  219.     SPECIAL, /* 0x7c */
  220.     SPECIAL, /* 0x7d */
  221.     SPECIAL, /* 0x7e */
  222.     SPECIAL, /* 0x7f */
  223. };
  224.  
  225. irq_cmd_t i8042_cmds[1] = {
  226.     { CMD_PORT_READ_1, (void *)0x60, 0 }
  227. };
  228.  
  229. irq_code_t i8042_kbd = {
  230.     1,
  231.     i8042_cmds
  232. };
  233.  
  234. static int key_released(keybuffer_t *keybuffer, unsigned char key)
  235. {
  236.     switch (key) {
  237.         case SC_LSHIFT:
  238.         case SC_RSHIFT:
  239.             keyflags &= ~PRESSED_SHIFT;
  240.             break;
  241.         case SC_CAPSLOCK:
  242.             keyflags &= ~PRESSED_CAPSLOCK;
  243.             if (lockflags & LOCKED_CAPSLOCK)
  244.                 lockflags &= ~LOCKED_CAPSLOCK;
  245.                 else
  246.                 lockflags |= LOCKED_CAPSLOCK;
  247.             break;
  248.         default:
  249.             break;
  250.     }
  251. }
  252.  
  253. static int key_pressed(keybuffer_t *keybuffer, unsigned char key)
  254. {
  255.     int *map = sc_primary_map;
  256.     int ascii = sc_primary_map[key];
  257.     int shift, capslock;
  258.     int letter = 0;
  259.  
  260.     switch (key) {
  261.         case SC_LSHIFT:
  262.         case SC_RSHIFT:
  263.                 keyflags |= PRESSED_SHIFT;
  264.             break;
  265.         case SC_CAPSLOCK:
  266.             keyflags |= PRESSED_CAPSLOCK;
  267.             break;
  268.         case SC_SPEC_ESCAPE:
  269.             break;
  270.     /*  case SC_LEFTARR:
  271.             if (keybuffer_available(keybuffer) >= 3) {
  272.                 keybuffer_push(keybuffer, 0x1b);   
  273.                 keybuffer_push(keybuffer, 0x5b);   
  274.                 keybuffer_push(keybuffer, 0x44);   
  275.             }
  276.             break;
  277.         case SC_RIGHTARR:
  278.             if (keybuffer_available(keybuffer) >= 3) {
  279.                 keybuffer_push(keybuffer, 0x1b);   
  280.                 keybuffer_push(keybuffer, 0x5b);   
  281.                 keybuffer_push(keybuffer, 0x43);   
  282.             }
  283.             break;
  284.         case SC_UPARR:
  285.             if (keybuffer_available(keybuffer) >= 3) {
  286.                 keybuffer_push(keybuffer, 0x1b);   
  287.                 keybuffer_push(keybuffer, 0x5b);   
  288.                 keybuffer_push(keybuffer, 0x41);   
  289.             }
  290.             break;
  291.         case SC_DOWNARR:
  292.             if (keybuffer_available(keybuffer) >= 3) {
  293.                 keybuffer_push(keybuffer, 0x1b);   
  294.                 keybuffer_push(keybuffer, 0x5b);   
  295.                 keybuffer_push(keybuffer, 0x42);   
  296.             }
  297.             break;
  298.         case SC_HOME:
  299.             if (keybuffer_available(keybuffer) >= 3) {
  300.                 keybuffer_push(keybuffer, 0x1b);   
  301.                 keybuffer_push(keybuffer, 0x4f);   
  302.                 keybuffer_push(keybuffer, 0x48);   
  303.             }
  304.             break;
  305.         case SC_END:
  306.             if (keybuffer_available(keybuffer) >= 3) {
  307.                 keybuffer_push(keybuffer, 0x1b);   
  308.                 keybuffer_push(keybuffer, 0x4f);   
  309.                 keybuffer_push(keybuffer, 0x46);   
  310.             }
  311.             break;
  312.         case SC_DELETE:
  313.             if (keybuffer_available(keybuffer) >= 4) {
  314.                 keybuffer_push(keybuffer, 0x1b);   
  315.                 keybuffer_push(keybuffer, 0x5b);   
  316.                 keybuffer_push(keybuffer, 0x33);   
  317.                 keybuffer_push(keybuffer, 0x7e);   
  318.             }
  319.             break;
  320.     */  default:
  321.                 letter = ((ascii >= 'a') && (ascii <= 'z'));
  322.             capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
  323.             shift = keyflags & PRESSED_SHIFT;
  324.             if (letter && capslock)
  325.                 shift = !shift;
  326.             if (shift)
  327.                 map = sc_secondary_map;
  328.             if (map[key] != SPECIAL)
  329.                 keybuffer_push(keybuffer, map[key]);   
  330.             break;
  331.     }
  332. }
  333.  
  334. /** Register uspace irq handler
  335.  * @return
  336.  */
  337. int kbd_arch_init(void)
  338. {
  339.     return !(ipc_register_irq(1, &i8042_kbd));
  340. }
  341.  
  342. int kbd_arch_process(keybuffer_t *keybuffer, int scan_code)
  343. {
  344.     if (scan_code != IGNORE_CODE) {
  345.         if (scan_code & KEY_RELEASE)
  346.             key_released(keybuffer, scan_code ^ KEY_RELEASE);
  347.         else
  348.             key_pressed(keybuffer, scan_code);
  349.     }
  350.     return  1;
  351. }
  352.