Subversion Repositories HelenOS-historic

Rev

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