Subversion Repositories HelenOS

Rev

Rev 1841 | Rev 1843 | 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. /** @addtogroup genarch
  30.  * @{
  31.  */
  32. /**
  33.  * @file
  34.  * @brief   i8042 processor driver.
  35.  *
  36.  * It takes care of low-level keyboard functions.
  37.  */
  38.  
  39. #include <genarch/kbd/i8042.h>
  40. #include <genarch/kbd/scanc.h>
  41. #include <genarch/kbd/scanc_pc.h>
  42. #include <arch/drivers/i8042.h>
  43. #include <arch/interrupt.h>
  44. #include <cpu.h>
  45. #include <arch/asm.h>
  46. #include <arch.h>
  47. #include <synch/spinlock.h>
  48. #include <typedefs.h>
  49. #include <console/chardev.h>
  50. #include <console/console.h>
  51. #include <macros.h>
  52. #include <interrupt.h>
  53.  
  54. /* Keyboard commands. */
  55. #define KBD_ENABLE  0xf4
  56. #define KBD_DISABLE 0xf5
  57. #define KBD_ACK     0xfa
  58.  
  59. /*
  60.  * 60  Write 8042 Command Byte: next data byte written to port 60h is
  61.  *     placed in 8042 command register. Format:
  62.  *
  63.  *    |7|6|5|4|3|2|1|0|8042 Command Byte
  64.  *     | | | | | | | `---- 1=enable output register full interrupt
  65.  *     | | | | | | `----- should be 0
  66.  *     | | | | | `------ 1=set status register system, 0=clear
  67.  *     | | | | `------- 1=override keyboard inhibit, 0=allow inhibit
  68.  *     | | | `-------- disable keyboard I/O by driving clock line low
  69.  *     | | `--------- disable auxiliary device, drives clock line low
  70.  *     | `---------- IBM scancode translation 0=AT, 1=PC/XT
  71.  *     `----------- reserved, should be 0
  72.  */
  73.  
  74. #define i8042_SET_COMMAND   0x60
  75. #define i8042_COMMAND       0x69
  76.  
  77. #define i8042_BUFFER_FULL_MASK  0x01
  78. #define i8042_WAIT_MASK     0x02
  79. #define i8042_MOUSE_DATA        0x20
  80.  
  81. #define KEY_RELEASE 0x80
  82.  
  83. static void key_released(uint8_t sc);
  84. static void key_pressed(uint8_t sc);
  85. static char key_read(chardev_t *d);
  86.  
  87. #define PRESSED_SHIFT       (1<<0)
  88. #define PRESSED_CAPSLOCK    (1<<1)
  89. #define LOCKED_CAPSLOCK     (1<<0)
  90.  
  91. #define ACTIVE_READ_BUFF_SIZE 16    /* Must be power of 2 */
  92.  
  93. static uint8_t active_read_buff[ACTIVE_READ_BUFF_SIZE];
  94.  
  95. SPINLOCK_INITIALIZE(keylock);       /**< keylock protects keyflags and lockflags. */
  96. static volatile int keyflags;       /**< Tracking of multiple keypresses. */
  97. static volatile int lockflags;      /**< Tracking of multiple keys lockings. */
  98.  
  99. static void i8042_suspend(chardev_t *);
  100. static void i8042_resume(chardev_t *);
  101.  
  102. static chardev_t kbrd;
  103. static chardev_operations_t ops = {
  104.     .suspend = i8042_suspend,
  105.     .resume = i8042_resume,
  106.     .read = key_read
  107. };
  108.  
  109. static void i8042_interrupt(int n, istate_t *istate);
  110. static void i8042_wait(void);
  111.  
  112. static iroutine oldvector;
  113. /** Initialize keyboard and service interrupts using kernel routine */
  114. void i8042_grab(void)
  115. {
  116.     oldvector = exc_register(VECTOR_KBD, "i8042_interrupt", (iroutine) i8042_interrupt);
  117.     i8042_wait();
  118.     i8042_command_write(i8042_SET_COMMAND);
  119.     i8042_wait();
  120.     i8042_data_write(i8042_COMMAND);
  121.     i8042_wait();
  122. }
  123. /** Resume the former interrupt vector */
  124. void i8042_release(void)
  125. {
  126.     if (oldvector)
  127.         exc_register(VECTOR_KBD, "user_interrupt", oldvector);
  128. }
  129.  
  130. /** Initialize i8042. */
  131. void i8042_init(void)
  132. {
  133.     int i;
  134.  
  135.     i8042_grab();
  136.         /* Prevent user from accidentaly releasing calling i8042_resume
  137.      * and disabling keyboard
  138.      */
  139.     oldvector = NULL;
  140.  
  141.     trap_virtual_enable_irqs(1<<IRQ_KBD);
  142.     chardev_initialize("i8042_kbd", &kbrd, &ops);
  143.     stdin = &kbrd;
  144.  
  145.     /*
  146.      * Clear input buffer.
  147.      * Number of iterations is limited to prevent infinite looping.
  148.      */
  149.     for (i = 0; (i8042_status_read() & i8042_BUFFER_FULL_MASK) && i < 100; i++) {
  150.         i8042_data_read();
  151.     }  
  152. }
  153.  
  154. /** Process i8042 interrupt.
  155.  *
  156.  * @param n Interrupt vector.
  157.  * @param istate Interrupted state.
  158.  */
  159. void i8042_interrupt(int n, istate_t *istate)
  160. {
  161.     uint8_t x;
  162.     uint8_t status;
  163.  
  164.     while (((status=i8042_status_read()) & i8042_BUFFER_FULL_MASK)) {
  165.         x = i8042_data_read();
  166.  
  167.         if ((status & i8042_MOUSE_DATA))
  168.             continue;
  169.  
  170.         if (x & KEY_RELEASE)
  171.             key_released(x ^ KEY_RELEASE);
  172.         else
  173.             key_pressed(x);
  174.     }
  175.     trap_virtual_eoi();
  176. }
  177.  
  178. /** Wait until the controller reads its data. */
  179. void i8042_wait(void) {
  180.     while (i8042_status_read() & i8042_WAIT_MASK) {
  181.         /* wait */
  182.     }
  183. }
  184.  
  185. /** Process release of key.
  186.  *
  187.  * @param sc Scancode of the key being released.
  188.  */
  189. void key_released(uint8_t sc)
  190. {
  191.     spinlock_lock(&keylock);
  192.     switch (sc) {
  193.         case SC_LSHIFT:
  194.         case SC_RSHIFT:
  195.         keyflags &= ~PRESSED_SHIFT;
  196.         break;
  197.         case SC_CAPSLOCK:
  198.         keyflags &= ~PRESSED_CAPSLOCK;
  199.         if (lockflags & LOCKED_CAPSLOCK)
  200.             lockflags &= ~LOCKED_CAPSLOCK;
  201.         else
  202.             lockflags |= LOCKED_CAPSLOCK;
  203.         break;
  204.         default:
  205.         break;
  206.     }
  207.     spinlock_unlock(&keylock);
  208. }
  209.  
  210. /** Process keypress.
  211.  *
  212.  * @param sc Scancode of the key being pressed.
  213.  */
  214. void key_pressed(uint8_t sc)
  215. {
  216.     char *map = sc_primary_map;
  217.     char ascii = sc_primary_map[sc];
  218.     bool shift, capslock;
  219.     bool letter = false;
  220.  
  221.     spinlock_lock(&keylock);
  222.     switch (sc) {
  223.     case SC_LSHIFT:
  224.     case SC_RSHIFT:
  225.             keyflags |= PRESSED_SHIFT;
  226.         break;
  227.     case SC_CAPSLOCK:
  228.         keyflags |= PRESSED_CAPSLOCK;
  229.         break;
  230.     case SC_SPEC_ESCAPE:
  231.         break;
  232.     case SC_LEFTARR:
  233.         chardev_push_character(&kbrd, 0x1b);
  234.         chardev_push_character(&kbrd, 0x5b);
  235.         chardev_push_character(&kbrd, 0x44);
  236.         break;
  237.     case SC_RIGHTARR:
  238.         chardev_push_character(&kbrd, 0x1b);
  239.         chardev_push_character(&kbrd, 0x5b);
  240.         chardev_push_character(&kbrd, 0x43);
  241.         break;
  242.     case SC_UPARR:
  243.         chardev_push_character(&kbrd, 0x1b);
  244.         chardev_push_character(&kbrd, 0x5b);
  245.         chardev_push_character(&kbrd, 0x41);
  246.         break;
  247.     case SC_DOWNARR:
  248.         chardev_push_character(&kbrd, 0x1b);
  249.         chardev_push_character(&kbrd, 0x5b);
  250.         chardev_push_character(&kbrd, 0x42);
  251.         break;
  252.     case SC_HOME:
  253.         chardev_push_character(&kbrd, 0x1b);
  254.         chardev_push_character(&kbrd, 0x4f);
  255.         chardev_push_character(&kbrd, 0x48);
  256.         break;
  257.     case SC_END:
  258.         chardev_push_character(&kbrd, 0x1b);
  259.         chardev_push_character(&kbrd, 0x4f);
  260.         chardev_push_character(&kbrd, 0x46);
  261.         break;
  262.     case SC_DELETE:
  263.         chardev_push_character(&kbrd, 0x1b);
  264.         chardev_push_character(&kbrd, 0x5b);
  265.         chardev_push_character(&kbrd, 0x33);
  266.         chardev_push_character(&kbrd, 0x7e);
  267.         break;
  268.     default:
  269.             letter = is_lower(ascii);
  270.         capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
  271.         shift = keyflags & PRESSED_SHIFT;
  272.         if (letter && capslock)
  273.             shift = !shift;
  274.         if (shift)
  275.             map = sc_secondary_map;
  276.         chardev_push_character(&kbrd, map[sc]);
  277.         break;
  278.     }
  279.     spinlock_unlock(&keylock);
  280. }
  281.  
  282. /* Called from getc(). */
  283. void i8042_resume(chardev_t *d)
  284. {
  285. }
  286.  
  287. /* Called from getc(). */
  288. void i8042_suspend(chardev_t *d)
  289. {
  290. }
  291.  
  292. static uint8_t active_read_buff_read(void)
  293. {
  294.     static int i=0;
  295.     i &= (ACTIVE_READ_BUFF_SIZE-1);
  296.     if(!active_read_buff[i]) {
  297.         return 0;
  298.     }
  299.     return active_read_buff[i++];
  300. }
  301.  
  302. static void active_read_buff_write(uint8_t ch)
  303. {
  304.     static int i=0;
  305.     active_read_buff[i] = ch;
  306.     i++;
  307.     i &= (ACTIVE_READ_BUFF_SIZE-1);
  308.     active_read_buff[i]=0;
  309. }
  310.  
  311.  
  312. static void active_read_key_pressed(uint8_t sc)
  313. {
  314.     char *map = sc_primary_map;
  315.     char ascii = sc_primary_map[sc];
  316.     bool shift, capslock;
  317.     bool letter = false;
  318.  
  319.     /*spinlock_lock(&keylock);*/
  320.     switch (sc) {
  321.     case SC_LSHIFT:
  322.     case SC_RSHIFT:
  323.             keyflags |= PRESSED_SHIFT;
  324.         break;
  325.     case SC_CAPSLOCK:
  326.         keyflags |= PRESSED_CAPSLOCK;
  327.         break;
  328.     case SC_SPEC_ESCAPE:
  329.         break;
  330.     case SC_LEFTARR:
  331.         active_read_buff_write(0x1b);
  332.         active_read_buff_write(0x5b);
  333.         active_read_buff_write(0x44);
  334.         break;
  335.     case SC_RIGHTARR:
  336.         active_read_buff_write(0x1b);
  337.         active_read_buff_write(0x5b);
  338.         active_read_buff_write(0x43);
  339.         break;
  340.     case SC_UPARR:
  341.         active_read_buff_write(0x1b);
  342.         active_read_buff_write(0x5b);
  343.         active_read_buff_write(0x41);
  344.         break;
  345.     case SC_DOWNARR:
  346.         active_read_buff_write(0x1b);
  347.         active_read_buff_write(0x5b);
  348.         active_read_buff_write(0x42);
  349.         break;
  350.     case SC_HOME:
  351.         active_read_buff_write(0x1b);
  352.         active_read_buff_write(0x4f);
  353.         active_read_buff_write(0x48);
  354.         break;
  355.     case SC_END:
  356.         active_read_buff_write(0x1b);
  357.         active_read_buff_write(0x4f);
  358.         active_read_buff_write(0x46);
  359.         break;
  360.     case SC_DELETE:
  361.         active_read_buff_write(0x1b);
  362.         active_read_buff_write(0x5b);
  363.         active_read_buff_write(0x33);
  364.         active_read_buff_write(0x7e);
  365.         break;
  366.     default:
  367.             letter = is_lower(ascii);
  368.         capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK);
  369.         shift = keyflags & PRESSED_SHIFT;
  370.         if (letter && capslock)
  371.             shift = !shift;
  372.         if (shift)
  373.             map = sc_secondary_map;
  374.         active_read_buff_write(map[sc]);
  375.         break;
  376.     }
  377.     /*spinlock_unlock(&keylock);*/
  378.  
  379. }
  380.  
  381. static char key_read(chardev_t *d)
  382. {
  383.     char ch;   
  384.  
  385.     while(!(ch = active_read_buff_read())) {
  386.         uint8_t x;
  387.         while (!(i8042_status_read() & i8042_BUFFER_FULL_MASK))
  388.             ;
  389.         x = i8042_data_read();
  390.         if (x & KEY_RELEASE)
  391.             key_released(x ^ KEY_RELEASE);
  392.         else
  393.             active_read_key_pressed(x);
  394.     }
  395.     return ch;
  396. }
  397.  
  398. /** Poll for key press and release events.
  399.  *
  400.  * This function can be used to implement keyboard polling.
  401.  */
  402. void i8042_poll(void)
  403. {
  404.     uint8_t x;
  405.  
  406.     while (((x = i8042_status_read() & i8042_BUFFER_FULL_MASK))) {
  407.         x = i8042_data_read();
  408.         if (x & KEY_RELEASE)
  409.             key_released(x ^ KEY_RELEASE);
  410.         else
  411.             key_pressed(x);
  412.     }
  413. }
  414.  
  415. /** @}
  416.  */
  417.