Subversion Repositories HelenOS-historic

Rev

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