Subversion Repositories HelenOS-historic

Rev

Rev 1353 | Rev 1560 | 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     '?'
  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. static volatile int keyflags;       /**< Tracking of multiple keypresses. */
  61. static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
  62.  
  63. /** Primary meaning of scancodes. */
  64. static char sc_primary_map[] = {
  65.     SPECIAL, /* 0x00 */
  66.     SPECIAL, /* 0x01 - Esc */
  67.     '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
  68.     '\b', /* 0x0e - Backspace */
  69.     '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
  70.     SPECIAL, /* 0x1d - LCtrl */
  71.     'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
  72.     '`',
  73.     SPECIAL, /* 0x2a - LShift */
  74.     '\\',
  75.     'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
  76.     SPECIAL, /* 0x36 - RShift */
  77.     '*',
  78.     SPECIAL, /* 0x38 - LAlt */
  79.     ' ',
  80.     SPECIAL, /* 0x3a - CapsLock */
  81.     0x3b, /* 0x3b - F1 */
  82. //  SPECIAL, /* 0x3b - F1 */
  83.     0x3c, /* 0x3c - F2 */
  84. //  SPECIAL, /* 0x3c - F2 */
  85.     0x3d, /* 0x3d - F3 */
  86. //  SPECIAL, /* 0x3d - F3 */
  87.     0x3e, /* 0x3e - F4 */
  88. //  SPECIAL, /* 0x3e - F4 */
  89. //  SPECIAL, /* 0x3f - F5 */
  90.     0x3f, /* 0x3f - F5 */
  91. //  SPECIAL, /* 0x40 - F6 */
  92.     0x40, /* 0x40 - F6 */
  93. //  SPECIAL, /* 0x41 - F7 */
  94.     0x41, /* 0x41 - F7 */
  95. //  SPECIAL, /* 0x42 - F8 */
  96.     0x42, /* 0x42 - F8 */
  97. //  SPECIAL, /* 0x43 - F9 */
  98.     0x43, /* 0x43 - F9 */
  99. //  SPECIAL, /* 0x44 - F10 */
  100.     0x44, /* 0x44 - F10 */
  101.     SPECIAL, /* 0x45 - NumLock */
  102.     SPECIAL, /* 0x46 - ScrollLock */
  103.     '7', '8', '9', '-',
  104.     '4', '5', '6', '+',
  105.     '1', '2', '3',
  106.     '0', '.',
  107.     SPECIAL, /* 0x54 - Alt-SysRq */
  108.     SPECIAL, /* 0x55 - F11/F12/PF1/FN */
  109.     SPECIAL, /* 0x56 - unlabelled key next to LAlt */
  110.     SPECIAL, /* 0x57 - F11 */
  111.     SPECIAL, /* 0x58 - F12 */
  112.     SPECIAL, /* 0x59 */
  113.     SPECIAL, /* 0x5a */
  114.     SPECIAL, /* 0x5b */
  115.     SPECIAL, /* 0x5c */
  116.     SPECIAL, /* 0x5d */
  117.     SPECIAL, /* 0x5e */
  118.     SPECIAL, /* 0x5f */
  119.     SPECIAL, /* 0x60 */
  120.     SPECIAL, /* 0x61 */
  121.     SPECIAL, /* 0x62 */
  122.     SPECIAL, /* 0x63 */
  123.     SPECIAL, /* 0x64 */
  124.     SPECIAL, /* 0x65 */
  125.     SPECIAL, /* 0x66 */
  126.     SPECIAL, /* 0x67 */
  127.     SPECIAL, /* 0x68 */
  128.     SPECIAL, /* 0x69 */
  129.     SPECIAL, /* 0x6a */
  130.     SPECIAL, /* 0x6b */
  131.     SPECIAL, /* 0x6c */
  132.     SPECIAL, /* 0x6d */
  133.     SPECIAL, /* 0x6e */
  134.     SPECIAL, /* 0x6f */
  135.     SPECIAL, /* 0x70 */
  136.     SPECIAL, /* 0x71 */
  137.     SPECIAL, /* 0x72 */
  138.     SPECIAL, /* 0x73 */
  139.     SPECIAL, /* 0x74 */
  140.     SPECIAL, /* 0x75 */
  141.     SPECIAL, /* 0x76 */
  142.     SPECIAL, /* 0x77 */
  143.     SPECIAL, /* 0x78 */
  144.     SPECIAL, /* 0x79 */
  145.     SPECIAL, /* 0x7a */
  146.     SPECIAL, /* 0x7b */
  147.     SPECIAL, /* 0x7c */
  148.     SPECIAL, /* 0x7d */
  149.     SPECIAL, /* 0x7e */
  150.     SPECIAL, /* 0x7f */
  151. };
  152.  
  153. /** Secondary meaning of scancodes. */
  154. static char sc_secondary_map[] = {
  155.     SPECIAL, /* 0x00 */
  156.     SPECIAL, /* 0x01 - Esc */
  157.     '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
  158.     SPECIAL, /* 0x0e - Backspace */
  159.     '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
  160.     SPECIAL, /* 0x1d - LCtrl */
  161.     'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
  162.     '~',
  163.     SPECIAL, /* 0x2a - LShift */
  164.     '|',
  165.     'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
  166.     SPECIAL, /* 0x36 - RShift */
  167.     '*',
  168.     SPECIAL, /* 0x38 - LAlt */
  169.     ' ',
  170.     SPECIAL, /* 0x3a - CapsLock */
  171.     0x3b, /* 0x3b - F1 */
  172.     0x3c, /* 0x3c - F2 */
  173.     0x3d, /* 0x3d - F3 */
  174.     0x3e, /* 0x3e - F4 */
  175.     0x3f, /* 0x3f - F5 */
  176.     0x40, /* 0x40 - F6 */
  177.     0x41, /* 0x41 - F7 */
  178.     0x42, /* 0x42 - F8 */
  179.     0x43, /* 0x43 - F9 */
  180.     0x44, /* 0x44 - F10 */
  181. //  SPECIAL, /* 0x3b - F1 */
  182. //  SPECIAL, /* 0x3c - F2 */
  183. //  SPECIAL, /* 0x3d - F3 */
  184. //  SPECIAL, /* 0x3e - F4 */
  185. //  SPECIAL, /* 0x3f - F5 */
  186. //  SPECIAL, /* 0x40 - F6 */
  187. //  SPECIAL, /* 0x41 - F7 */
  188. //  SPECIAL, /* 0x42 - F8 */
  189. //  SPECIAL, /* 0x43 - F9 */
  190. //  SPECIAL, /* 0x44 - F10 */
  191.     SPECIAL, /* 0x45 - NumLock */
  192.     SPECIAL, /* 0x46 - ScrollLock */
  193.     '7', '8', '9', '-',
  194.     '4', '5', '6', '+',
  195.     '1', '2', '3',
  196.     '0', '.',
  197.     SPECIAL, /* 0x54 - Alt-SysRq */
  198.     SPECIAL, /* 0x55 - F11/F12/PF1/FN */
  199.     SPECIAL, /* 0x56 - unlabelled key next to LAlt */
  200.     SPECIAL, /* 0x57 - F11 */
  201.     SPECIAL, /* 0x58 - F12 */
  202.     SPECIAL, /* 0x59 */
  203.     SPECIAL, /* 0x5a */
  204.     SPECIAL, /* 0x5b */
  205.     SPECIAL, /* 0x5c */
  206.     SPECIAL, /* 0x5d */
  207.     SPECIAL, /* 0x5e */
  208.     SPECIAL, /* 0x5f */
  209.     SPECIAL, /* 0x60 */
  210.     SPECIAL, /* 0x61 */
  211.     SPECIAL, /* 0x62 */
  212.     SPECIAL, /* 0x63 */
  213.     SPECIAL, /* 0x64 */
  214.     SPECIAL, /* 0x65 */
  215.     SPECIAL, /* 0x66 */
  216.     SPECIAL, /* 0x67 */
  217.     SPECIAL, /* 0x68 */
  218.     SPECIAL, /* 0x69 */
  219.     SPECIAL, /* 0x6a */
  220.     SPECIAL, /* 0x6b */
  221.     SPECIAL, /* 0x6c */
  222.     SPECIAL, /* 0x6d */
  223.     SPECIAL, /* 0x6e */
  224.     SPECIAL, /* 0x6f */
  225.     SPECIAL, /* 0x70 */
  226.     SPECIAL, /* 0x71 */
  227.     SPECIAL, /* 0x72 */
  228.     SPECIAL, /* 0x73 */
  229.     SPECIAL, /* 0x74 */
  230.     SPECIAL, /* 0x75 */
  231.     SPECIAL, /* 0x76 */
  232.     SPECIAL, /* 0x77 */
  233.     SPECIAL, /* 0x78 */
  234.     SPECIAL, /* 0x79 */
  235.     SPECIAL, /* 0x7a */
  236.     SPECIAL, /* 0x7b */
  237.     SPECIAL, /* 0x7c */
  238.     SPECIAL, /* 0x7d */
  239.     SPECIAL, /* 0x7e */
  240.     SPECIAL, /* 0x7f */
  241. };
  242.  
  243. irq_cmd_t i8042_cmds[1] = {
  244.     { CMD_PORT_READ_1, (void *)0x60, 0 }
  245. };
  246.  
  247. irq_code_t i8042_kbd = {
  248.     1,
  249.     i8042_cmds
  250. };
  251.  
  252. static int key_released(keybuffer_t *keybuffer, unsigned char key)
  253. {
  254.     switch (key) {
  255.         case SC_LSHIFT:
  256.         case SC_RSHIFT:
  257.             keyflags &= ~PRESSED_SHIFT;
  258.             break;
  259.         case SC_CAPSLOCK:
  260.             keyflags &= ~PRESSED_CAPSLOCK;
  261.             if (lockflags & LOCKED_CAPSLOCK)
  262.                 lockflags &= ~LOCKED_CAPSLOCK;
  263.                 else
  264.                 lockflags |= LOCKED_CAPSLOCK;
  265.             break;
  266.         default:
  267.             break;
  268.     }
  269. }
  270.  
  271. static int key_pressed(keybuffer_t *keybuffer, unsigned char key)
  272. {
  273.     char *map = sc_primary_map;
  274.     char ascii = sc_primary_map[key];
  275.     char shift, capslock;
  276.     char letter = 0;
  277.  
  278.     switch (key) {
  279.         case SC_LSHIFT:
  280.         case SC_RSHIFT:
  281.                 keyflags |= PRESSED_SHIFT;
  282.             break;
  283.         case SC_CAPSLOCK:
  284.             keyflags |= PRESSED_CAPSLOCK;
  285.             break;
  286.         case SC_SPEC_ESCAPE:
  287.             break;
  288.         case SC_LEFTARR:
  289.             if (keybuffer_available(keybuffer) >= 3) {
  290.                 keybuffer_push(keybuffer, 0x1b);   
  291.                 keybuffer_push(keybuffer, 0x5b);   
  292.                 keybuffer_push(keybuffer, 0x44);   
  293.             }
  294.             break;
  295.         case SC_RIGHTARR:
  296.             if (keybuffer_available(keybuffer) >= 3) {
  297.                 keybuffer_push(keybuffer, 0x1b);   
  298.                 keybuffer_push(keybuffer, 0x5b);   
  299.                 keybuffer_push(keybuffer, 0x43);   
  300.             }
  301.             break;
  302.         case SC_UPARR:
  303.             if (keybuffer_available(keybuffer) >= 3) {
  304.                 keybuffer_push(keybuffer, 0x1b);   
  305.                 keybuffer_push(keybuffer, 0x5b);   
  306.                 keybuffer_push(keybuffer, 0x41);   
  307.             }
  308.             break;
  309.         case SC_DOWNARR:
  310.             if (keybuffer_available(keybuffer) >= 3) {
  311.                 keybuffer_push(keybuffer, 0x1b);   
  312.                 keybuffer_push(keybuffer, 0x5b);   
  313.                 keybuffer_push(keybuffer, 0x42);   
  314.             }
  315.             break;
  316.         case SC_HOME:
  317.             if (keybuffer_available(keybuffer) >= 3) {
  318.                 keybuffer_push(keybuffer, 0x1b);   
  319.                 keybuffer_push(keybuffer, 0x4f);   
  320.                 keybuffer_push(keybuffer, 0x48);   
  321.             }
  322.             break;
  323.         case SC_END:
  324.             if (keybuffer_available(keybuffer) >= 3) {
  325.                 keybuffer_push(keybuffer, 0x1b);   
  326.                 keybuffer_push(keybuffer, 0x4f);   
  327.                 keybuffer_push(keybuffer, 0x46);   
  328.             }
  329.             break;
  330.         case SC_DELETE:
  331.             if (keybuffer_available(keybuffer) >= 4) {
  332.                 keybuffer_push(keybuffer, 0x1b);   
  333.                 keybuffer_push(keybuffer, 0x5b);   
  334.                 keybuffer_push(keybuffer, 0x33);   
  335.                 keybuffer_push(keybuffer, 0x7e);   
  336.             }
  337.             break;
  338.         default:
  339.                 letter = ((ascii >= 'a') && (ascii <= 'z'));
  340.             capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
  341.             shift = keyflags & PRESSED_SHIFT;
  342.             if (letter && capslock)
  343.                 shift = !shift;
  344.             if (shift)
  345.                 map = sc_secondary_map;
  346.             keybuffer_push(keybuffer, map[key]);   
  347.             break;
  348.     }
  349. }
  350.  
  351. /** Register uspace irq handler
  352.  * @return
  353.  */
  354. int kbd_arch_init(void)
  355. {
  356.     return !(ipc_register_irq(1, &i8042_kbd));
  357. }
  358.  
  359. int kbd_arch_process(keybuffer_t *keybuffer, int scan_code)
  360. {
  361.     if (scan_code != IGNORE_CODE) {
  362.         if (scan_code & KEY_RELEASE)
  363.             key_released(keybuffer, scan_code ^ KEY_RELEASE);
  364.         else
  365.             key_pressed(keybuffer, scan_code);
  366.     }
  367.     return  1;
  368. }
  369.