Subversion Repositories HelenOS-historic

Rev

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