Subversion Repositories HelenOS

Rev

Rev 893 | Rev 895 | 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 <genarch/i8042/i8042.h>
  30. #include <arch/drivers/i8042.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. #include <macros.h>
  41. #include <interrupt.h>
  42.  
  43. /**
  44.  * i8042 processor driver.
  45.  * It takes care of low-level keyboard functions.
  46.  */
  47.  
  48. /** Keyboard commands. */
  49. #define KBD_ENABLE  0xf4
  50. #define KBD_DISABLE 0xf5
  51. #define KBD_ACK     0xfa
  52.  
  53. /*
  54.  * 60  Write 8042 Command Byte: next data byte written to port 60h is
  55.  *     placed in 8042 command register.Format:
  56.  *
  57.  *    |7|6|5|4|3|2|1|0|8042 Command Byte
  58.  *     | | | | | | | `---- 1=enable output register full interrupt
  59.  *     | | | | | | `----- should be 0
  60.  *     | | | | | `------ 1=set status register system, 0=clear
  61.  *     | | | | `------- 1=override keyboard inhibit, 0=allow inhibit
  62.  *     | | | `-------- disable keyboard I/O by driving clock line low
  63.  *     | | `--------- disable auxiliary device, drives clock line low
  64.  *     | `---------- IBM scancode translation 0=AT, 1=PC/XT
  65.  *     `----------- reserved, should be 0
  66.  */
  67.  
  68. #define i8042_SET_COMMAND   0x60
  69. #define i8042_COMMAND       0x49
  70.  
  71. #define i8042_BUFFER_FULL_MASK  0x01
  72. #define i8042_WAIT_MASK     0x02
  73.  
  74. #define SPECIAL     '?'
  75. #define KEY_RELEASE 0x80
  76.  
  77. static void key_released(__u8 sc);
  78. static void key_pressed(__u8 sc);
  79. static char key_read(chardev_t *d);
  80.  
  81. #define PRESSED_SHIFT       (1<<0)
  82. #define PRESSED_CAPSLOCK    (1<<1)
  83. #define LOCKED_CAPSLOCK     (1<<0)
  84.  
  85. #define ACTIVE_READ_BUFF_SIZE 16    /* Must be power of 2 */
  86.  
  87. static __u8 active_read_buff[ACTIVE_READ_BUFF_SIZE];
  88.  
  89. SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
  90. static volatile int keyflags;       /**< Tracking of multiple keypresses. */
  91. static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
  92.  
  93. static void i8042_suspend(chardev_t *);
  94. static void i8042_resume(chardev_t *);
  95.  
  96. static chardev_t kbrd;
  97. static chardev_operations_t ops = {
  98.     .suspend = i8042_suspend,
  99.     .resume = i8042_resume,
  100.     .read = key_read
  101. };
  102.  
  103. /** Primary meaning of scancodes. */
  104. static char sc_primary_map[] = {
  105.     SPECIAL, /* 0x00 */
  106.     SPECIAL, /* 0x01 - Esc */
  107.     '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
  108.     '\b', /* 0x0e - Backspace */
  109.     '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
  110.     SPECIAL, /* 0x1d - LCtrl */
  111.     'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
  112.     '`',
  113.     SPECIAL, /* 0x2a - LShift */
  114.     '\\',
  115.     'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
  116.     SPECIAL, /* 0x36 - RShift */
  117.     '*',
  118.     SPECIAL, /* 0x38 - LAlt */
  119.     ' ',
  120.     SPECIAL, /* 0x3a - CapsLock */
  121.     SPECIAL, /* 0x3b - F1 */
  122.     SPECIAL, /* 0x3c - F2 */
  123.     SPECIAL, /* 0x3d - F3 */
  124.     SPECIAL, /* 0x3e - F4 */
  125.     SPECIAL, /* 0x3f - F5 */
  126.     SPECIAL, /* 0x40 - F6 */
  127.     SPECIAL, /* 0x41 - F7 */
  128.     SPECIAL, /* 0x42 - F8 */
  129.     SPECIAL, /* 0x43 - F9 */
  130.     SPECIAL, /* 0x44 - F10 */
  131.     SPECIAL, /* 0x45 - NumLock */
  132.     SPECIAL, /* 0x46 - ScrollLock */
  133.     '7', '8', '9', '-',
  134.     '4', '5', '6', '+',
  135.     '1', '2', '3',
  136.     '0', '.',
  137.     SPECIAL, /* 0x54 - Alt-SysRq */
  138.     SPECIAL, /* 0x55 - F11/F12/PF1/FN */
  139.     SPECIAL, /* 0x56 - unlabelled key next to LAlt */
  140.     SPECIAL, /* 0x57 - F11 */
  141.     SPECIAL, /* 0x58 - F12 */
  142.     SPECIAL, /* 0x59 */
  143.     SPECIAL, /* 0x5a */
  144.     SPECIAL, /* 0x5b */
  145.     SPECIAL, /* 0x5c */
  146.     SPECIAL, /* 0x5d */
  147.     SPECIAL, /* 0x5e */
  148.     SPECIAL, /* 0x5f */
  149.     SPECIAL, /* 0x60 */
  150.     SPECIAL, /* 0x61 */
  151.     SPECIAL, /* 0x62 */
  152.     SPECIAL, /* 0x63 */
  153.     SPECIAL, /* 0x64 */
  154.     SPECIAL, /* 0x65 */
  155.     SPECIAL, /* 0x66 */
  156.     SPECIAL, /* 0x67 */
  157.     SPECIAL, /* 0x68 */
  158.     SPECIAL, /* 0x69 */
  159.     SPECIAL, /* 0x6a */
  160.     SPECIAL, /* 0x6b */
  161.     SPECIAL, /* 0x6c */
  162.     SPECIAL, /* 0x6d */
  163.     SPECIAL, /* 0x6e */
  164.     SPECIAL, /* 0x6f */
  165.     SPECIAL, /* 0x70 */
  166.     SPECIAL, /* 0x71 */
  167.     SPECIAL, /* 0x72 */
  168.     SPECIAL, /* 0x73 */
  169.     SPECIAL, /* 0x74 */
  170.     SPECIAL, /* 0x75 */
  171.     SPECIAL, /* 0x76 */
  172.     SPECIAL, /* 0x77 */
  173.     SPECIAL, /* 0x78 */
  174.     SPECIAL, /* 0x79 */
  175.     SPECIAL, /* 0x7a */
  176.     SPECIAL, /* 0x7b */
  177.     SPECIAL, /* 0x7c */
  178.     SPECIAL, /* 0x7d */
  179.     SPECIAL, /* 0x7e */
  180.     SPECIAL, /* 0x7f */
  181. };
  182.  
  183. /** Secondary meaning of scancodes. */
  184. static char sc_secondary_map[] = {
  185.     SPECIAL, /* 0x00 */
  186.     SPECIAL, /* 0x01 - Esc */
  187.     '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',
  188.     SPECIAL, /* 0x0e - Backspace */
  189.     '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
  190.     SPECIAL, /* 0x1d - LCtrl */
  191.     'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',
  192.     '~',
  193.     SPECIAL, /* 0x2a - LShift */
  194.     '|',
  195.     'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
  196.     SPECIAL, /* 0x36 - RShift */
  197.     '*',
  198.     SPECIAL, /* 0x38 - LAlt */
  199.     ' ',
  200.     SPECIAL, /* 0x3a - CapsLock */
  201.     SPECIAL, /* 0x3b - F1 */
  202.     SPECIAL, /* 0x3c - F2 */
  203.     SPECIAL, /* 0x3d - F3 */
  204.     SPECIAL, /* 0x3e - F4 */
  205.     SPECIAL, /* 0x3f - F5 */
  206.     SPECIAL, /* 0x40 - F6 */
  207.     SPECIAL, /* 0x41 - F7 */
  208.     SPECIAL, /* 0x42 - F8 */
  209.     SPECIAL, /* 0x43 - F9 */
  210.     SPECIAL, /* 0x44 - F10 */
  211.     SPECIAL, /* 0x45 - NumLock */
  212.     SPECIAL, /* 0x46 - ScrollLock */
  213.     '7', '8', '9', '-',
  214.     '4', '5', '6', '+',
  215.     '1', '2', '3',
  216.     '0', '.',
  217.     SPECIAL, /* 0x54 - Alt-SysRq */
  218.     SPECIAL, /* 0x55 - F11/F12/PF1/FN */
  219.     SPECIAL, /* 0x56 - unlabelled key next to LAlt */
  220.     SPECIAL, /* 0x57 - F11 */
  221.     SPECIAL, /* 0x58 - F12 */
  222.     SPECIAL, /* 0x59 */
  223.     SPECIAL, /* 0x5a */
  224.     SPECIAL, /* 0x5b */
  225.     SPECIAL, /* 0x5c */
  226.     SPECIAL, /* 0x5d */
  227.     SPECIAL, /* 0x5e */
  228.     SPECIAL, /* 0x5f */
  229.     SPECIAL, /* 0x60 */
  230.     SPECIAL, /* 0x61 */
  231.     SPECIAL, /* 0x62 */
  232.     SPECIAL, /* 0x63 */
  233.     SPECIAL, /* 0x64 */
  234.     SPECIAL, /* 0x65 */
  235.     SPECIAL, /* 0x66 */
  236.     SPECIAL, /* 0x67 */
  237.     SPECIAL, /* 0x68 */
  238.     SPECIAL, /* 0x69 */
  239.     SPECIAL, /* 0x6a */
  240.     SPECIAL, /* 0x6b */
  241.     SPECIAL, /* 0x6c */
  242.     SPECIAL, /* 0x6d */
  243.     SPECIAL, /* 0x6e */
  244.     SPECIAL, /* 0x6f */
  245.     SPECIAL, /* 0x70 */
  246.     SPECIAL, /* 0x71 */
  247.     SPECIAL, /* 0x72 */
  248.     SPECIAL, /* 0x73 */
  249.     SPECIAL, /* 0x74 */
  250.     SPECIAL, /* 0x75 */
  251.     SPECIAL, /* 0x76 */
  252.     SPECIAL, /* 0x77 */
  253.     SPECIAL, /* 0x78 */
  254.     SPECIAL, /* 0x79 */
  255.     SPECIAL, /* 0x7a */
  256.     SPECIAL, /* 0x7b */
  257.     SPECIAL, /* 0x7c */
  258.     SPECIAL, /* 0x7d */
  259.     SPECIAL, /* 0x7e */
  260.     SPECIAL, /* 0x7f */
  261. };
  262.  
  263. static void i8042_interrupt(int n, void *stack);
  264.  
  265. /** Initialize i8042. */
  266. void i8042_init(void)
  267. {
  268.     exc_register(VECTOR_KBD, "i8042_interrupt", i8042_interrupt);
  269.     while (i8042_status_read() & i8042_WAIT_MASK) {
  270.         /* wait */
  271.     }
  272.     i8042_command_write(i8042_SET_COMMAND);
  273.     while (i8042_status_read() & i8042_WAIT_MASK) {
  274.         /* wait */
  275.     }
  276.     i8042_data_write(i8042_COMMAND);
  277.  
  278.     trap_virtual_enable_irqs(1<<IRQ_KBD);
  279.     chardev_initialize("i8042_kbd", &kbrd, &ops);
  280.     stdin = &kbrd;
  281. }
  282.  
  283. /** Process i8042 interrupt.
  284.  *
  285.  * @param n Interrupt vector.
  286.  * @param stack Interrupted stack.
  287.  */
  288. void i8042_interrupt(int n, void *stack)
  289. {
  290.     __u8 x;
  291.  
  292.     trap_virtual_eoi();
  293.     x = i8042_data_read();
  294.     if (x & KEY_RELEASE)
  295.         key_released(x ^ KEY_RELEASE);
  296.     else
  297.         key_pressed(x);
  298. }
  299.  
  300. /** Process release of key.
  301.  *
  302.  * @param sc Scancode of the key being released.
  303.  */
  304. void key_released(__u8 sc)
  305. {
  306.     spinlock_lock(&keylock);
  307.     switch (sc) {
  308.         case SC_LSHIFT:
  309.         case SC_RSHIFT:
  310.         keyflags &= ~PRESSED_SHIFT;
  311.         break;
  312.         case SC_CAPSLOCK:
  313.         keyflags &= ~PRESSED_CAPSLOCK;
  314.         if (lockflags & LOCKED_CAPSLOCK)
  315.             lockflags &= ~LOCKED_CAPSLOCK;
  316.         else
  317.             lockflags |= LOCKED_CAPSLOCK;
  318.         break;
  319.         default:
  320.         break;
  321.     }
  322.     spinlock_unlock(&keylock);
  323. }
  324.  
  325. /** Process keypress.
  326.  *
  327.  * @param sc Scancode of the key being pressed.
  328.  */
  329. void key_pressed(__u8 sc)
  330. {
  331.     char *map = sc_primary_map;
  332.     char ascii = sc_primary_map[sc];
  333.     bool shift, capslock;
  334.     bool letter = false;
  335.  
  336.     spinlock_lock(&keylock);
  337.     switch (sc) {
  338.     case SC_LSHIFT:
  339.     case SC_RSHIFT:
  340.             keyflags |= PRESSED_SHIFT;
  341.         break;
  342.     case SC_CAPSLOCK:
  343.         keyflags |= PRESSED_CAPSLOCK;
  344.         break;
  345.     case SC_SPEC_ESCAPE:
  346.         break;
  347.     case SC_LEFTARR:
  348.         chardev_push_character(&kbrd, 0x1b);
  349.         chardev_push_character(&kbrd, 0x5b);
  350.         chardev_push_character(&kbrd, 0x44);
  351.         break;
  352.     case SC_RIGHTARR:
  353.         chardev_push_character(&kbrd, 0x1b);
  354.         chardev_push_character(&kbrd, 0x5b);
  355.         chardev_push_character(&kbrd, 0x43);
  356.         break;
  357.     case SC_UPARR:
  358.         chardev_push_character(&kbrd, 0x1b);
  359.         chardev_push_character(&kbrd, 0x5b);
  360.         chardev_push_character(&kbrd, 0x41);
  361.         break;
  362.     case SC_DOWNARR:
  363.         chardev_push_character(&kbrd, 0x1b);
  364.         chardev_push_character(&kbrd, 0x5b);
  365.         chardev_push_character(&kbrd, 0x42);
  366.         break;
  367.     case SC_HOME:
  368.         chardev_push_character(&kbrd, 0x1b);
  369.         chardev_push_character(&kbrd, 0x4f);
  370.         chardev_push_character(&kbrd, 0x48);
  371.         break;
  372.     case SC_END:
  373.         chardev_push_character(&kbrd, 0x1b);
  374.         chardev_push_character(&kbrd, 0x4f);
  375.         chardev_push_character(&kbrd, 0x46);
  376.         break;
  377.     case SC_DELETE:
  378.         chardev_push_character(&kbrd, 0x1b);
  379.         chardev_push_character(&kbrd, 0x5b);
  380.         chardev_push_character(&kbrd, 0x33);
  381.         chardev_push_character(&kbrd, 0x7e);
  382.         break;
  383.     default:
  384.             letter = is_lower(ascii);
  385.         capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
  386.         shift = keyflags & PRESSED_SHIFT;
  387.         if (letter && capslock)
  388.             shift = !shift;
  389.         if (shift)
  390.             map = sc_secondary_map;
  391.         chardev_push_character(&kbrd, map[sc]);
  392.         break;
  393.     }
  394.     spinlock_unlock(&keylock);
  395. }
  396.  
  397. /* Called from getc(). */
  398. void i8042_resume(chardev_t *d)
  399. {
  400. }
  401.  
  402. /* Called from getc(). */
  403. void i8042_suspend(chardev_t *d)
  404. {
  405. }
  406.  
  407. static __u8 active_read_buff_read(void)
  408. {
  409.     static int i=0;
  410.     i &= (ACTIVE_READ_BUFF_SIZE-1);
  411.     if(!active_read_buff[i]) {
  412.         return 0;
  413.     }
  414.     return active_read_buff[i++];
  415. }
  416.  
  417. static void active_read_buff_write(__u8 ch)
  418. {
  419.     static int i=0;
  420.     active_read_buff[i] = ch;
  421.     i++;
  422.     i &= (ACTIVE_READ_BUFF_SIZE-1);
  423.     active_read_buff[i]=0;
  424. }
  425.  
  426.  
  427. static void active_read_key_pressed(__u8 sc)
  428. {
  429.     char *map = sc_primary_map;
  430.     char ascii = sc_primary_map[sc];
  431.     bool shift, capslock;
  432.     bool letter = false;
  433.  
  434.     /*spinlock_lock(&keylock);*/
  435.     switch (sc) {
  436.     case SC_LSHIFT:
  437.     case SC_RSHIFT:
  438.             keyflags |= PRESSED_SHIFT;
  439.         break;
  440.     case SC_CAPSLOCK:
  441.         keyflags |= PRESSED_CAPSLOCK;
  442.         break;
  443.     case SC_SPEC_ESCAPE:
  444.         break;
  445.     case SC_LEFTARR:
  446.         active_read_buff_write(0x1b);
  447.         active_read_buff_write(0x5b);
  448.         active_read_buff_write(0x44);
  449.         break;
  450.     case SC_RIGHTARR:
  451.         active_read_buff_write(0x1b);
  452.         active_read_buff_write(0x5b);
  453.         active_read_buff_write(0x43);
  454.         break;
  455.     case SC_UPARR:
  456.         active_read_buff_write(0x1b);
  457.         active_read_buff_write(0x5b);
  458.         active_read_buff_write(0x41);
  459.         break;
  460.     case SC_DOWNARR:
  461.         active_read_buff_write(0x1b);
  462.         active_read_buff_write(0x5b);
  463.         active_read_buff_write(0x42);
  464.         break;
  465.     case SC_HOME:
  466.         active_read_buff_write(0x1b);
  467.         active_read_buff_write(0x4f);
  468.         active_read_buff_write(0x48);
  469.         break;
  470.     case SC_END:
  471.         active_read_buff_write(0x1b);
  472.         active_read_buff_write(0x4f);
  473.         active_read_buff_write(0x46);
  474.         break;
  475.     case SC_DELETE:
  476.         active_read_buff_write(0x1b);
  477.         active_read_buff_write(0x5b);
  478.         active_read_buff_write(0x33);
  479.         active_read_buff_write(0x7e);
  480.         break;
  481.     default:
  482.             letter = is_lower(ascii);
  483.         capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
  484.         shift = keyflags & PRESSED_SHIFT;
  485.         if (letter && capslock)
  486.             shift = !shift;
  487.         if (shift)
  488.             map = sc_secondary_map;
  489.         active_read_buff_write(map[sc]);
  490.         break;
  491.     }
  492.     /*spinlock_unlock(&keylock);*/
  493.  
  494. }
  495.  
  496. static char key_read(chardev_t *d)
  497. {
  498.     char ch;   
  499.  
  500.     while(!(ch = active_read_buff_read())) {
  501.         __u8 x;
  502.         while (!((x=i8042_status_read() & i8042_BUFFER_FULL_MASK)))
  503.             ;
  504.         x = i8042_data_read();
  505.         if (x & KEY_RELEASE)
  506.             key_released(x ^ KEY_RELEASE);
  507.         else
  508.             active_read_key_pressed(x);
  509.     }
  510.     return ch;
  511. }
  512.