Subversion Repositories HelenOS

Rev

Rev 1957 | Rev 2089 | 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/key.h>
  41. #include <genarch/kbd/scanc.h>
  42. #include <genarch/kbd/scanc_pc.h>
  43. #include <arch/drivers/i8042.h>
  44. #include <cpu.h>
  45. #include <arch/asm.h>
  46. #include <arch.h>
  47. #include <typedefs.h>
  48. #include <console/chardev.h>
  49. #include <console/console.h>
  50. #include <interrupt.h>
  51. #include <sysinfo/sysinfo.h>
  52. #include <ddi/irq.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. static void i8042_suspend(chardev_t *);
  82. static void i8042_resume(chardev_t *);
  83.  
  84. static chardev_operations_t ops = {
  85.     .suspend = i8042_suspend,
  86.     .resume = i8042_resume,
  87.     .read = i8042_key_read
  88. };
  89.  
  90. /** Structure for i8042's IRQ. */
  91. static irq_t i8042_kbd_irq;
  92. static irq_t i8042_mouse_irq;
  93.  
  94. /** Wait until the controller reads its data. */
  95. static void i8042_wait(void) {
  96.     while (i8042_status_read() & i8042_WAIT_MASK) {
  97.         /* wait */
  98.     }
  99. }
  100.  
  101. void i8042_grab(void)
  102. {
  103.     ipl_t ipl = interrupts_disable();
  104.    
  105.     i8042_wait();
  106.     i8042_command_write(i8042_SET_COMMAND);
  107.     i8042_wait();
  108.     i8042_data_write(i8042_COMMAND);
  109.     i8042_wait();
  110.  
  111.     spinlock_lock(&i8042_kbd_irq.lock);
  112.     i8042_kbd_irq.notif_cfg.notify = false;
  113.     spinlock_unlock(&i8042_kbd_irq.lock);
  114.    
  115.     spinlock_lock(&i8042_mouse_irq.lock);
  116.     i8042_mouse_irq.notif_cfg.notify = false;
  117.     spinlock_unlock(&i8042_mouse_irq.lock);
  118.    
  119.     interrupts_restore(ipl);
  120. }
  121.  
  122. void i8042_release(void)
  123. {
  124.     ipl_t ipl = interrupts_disable();
  125.    
  126.     spinlock_lock(&i8042_kbd_irq.lock);
  127.     if (i8042_kbd_irq.notif_cfg.answerbox)
  128.         i8042_kbd_irq.notif_cfg.notify = true;
  129.     spinlock_unlock(&i8042_kbd_irq.lock);
  130.    
  131.     spinlock_lock(&i8042_mouse_irq.lock);
  132.     if (i8042_mouse_irq.notif_cfg.answerbox)
  133.         i8042_mouse_irq.notif_cfg.notify = true;
  134.     spinlock_unlock(&i8042_mouse_irq.lock);
  135.    
  136.     interrupts_restore(ipl);
  137. }
  138.  
  139. static irq_ownership_t i8042_claim(void)
  140. {
  141.     return IRQ_ACCEPT;
  142. }
  143.  
  144. static void i8042_kbd_irq_handler(irq_t *irq, void *arg, ...)
  145. {
  146.     if (irq->notif_cfg.notify && irq->notif_cfg.answerbox)
  147.         ipc_irq_send_notif(irq);
  148.     else {
  149.         uint8_t x;
  150.         uint8_t status;
  151.        
  152.         while (((status = i8042_status_read()) & i8042_BUFFER_FULL_MASK)) {
  153.             x = i8042_data_read();
  154.            
  155.             if ((status & i8042_MOUSE_DATA))
  156.                 continue;
  157.            
  158.             if (x & KEY_RELEASE)
  159.                 key_released(x ^ KEY_RELEASE);
  160.             else
  161.                 key_pressed(x);
  162.         }
  163.     }
  164. }
  165.  
  166. static void i8042_mouse_irq_handler(irq_t *irq, void *arg, ...)
  167. {
  168.     if (irq->notif_cfg.notify && irq->notif_cfg.answerbox)
  169.         ipc_irq_send_notif(irq);
  170. }
  171.  
  172. /** Initialize i8042. */
  173. void i8042_init(devno_t kbd_devno, inr_t kbd_inr, devno_t mouse_devno, inr_t mouse_inr)
  174. {
  175.     chardev_initialize("i8042_kbd", &kbrd, &ops);
  176.     stdin = &kbrd;
  177.    
  178.     irq_initialize(&i8042_kbd_irq);
  179.     i8042_kbd_irq.devno = kbd_devno;
  180.     i8042_kbd_irq.inr = kbd_inr;
  181.     i8042_kbd_irq.claim = i8042_claim;
  182.     i8042_kbd_irq.handler = i8042_kbd_irq_handler;
  183.     irq_register(&i8042_kbd_irq);
  184.    
  185.     irq_initialize(&i8042_mouse_irq);
  186.     i8042_mouse_irq.devno = mouse_devno;
  187.     i8042_mouse_irq.inr = mouse_inr;
  188.     i8042_mouse_irq.claim = i8042_claim;
  189.     i8042_mouse_irq.handler = i8042_mouse_irq_handler;
  190.     irq_register(&i8042_mouse_irq);
  191.    
  192.     trap_virtual_enable_irqs(1 << kbd_inr);
  193.     trap_virtual_enable_irqs(1 << mouse_inr);
  194.    
  195.     /*
  196.      * Clear input buffer.
  197.      * Number of iterations is limited to prevent infinite looping.
  198.      */
  199.     int i;
  200.     for (i = 0; (i8042_status_read() & i8042_BUFFER_FULL_MASK) && i < 100; i++) {
  201.         i8042_data_read();
  202.     }
  203.    
  204.     sysinfo_set_item_val("kbd", NULL, true);
  205.     sysinfo_set_item_val("kbd.devno", NULL, kbd_devno);
  206.     sysinfo_set_item_val("kbd.inr", NULL, kbd_inr);
  207.    
  208.     sysinfo_set_item_val("mouse", NULL, true);
  209.     sysinfo_set_item_val("mouse.devno", NULL, mouse_devno);
  210.     sysinfo_set_item_val("mouse.inr", NULL, mouse_inr);
  211.    
  212.     i8042_grab();
  213. }
  214.  
  215. /* Called from getc(). */
  216. void i8042_resume(chardev_t *d)
  217. {
  218. }
  219.  
  220. /* Called from getc(). */
  221. void i8042_suspend(chardev_t *d)
  222. {
  223. }
  224.  
  225. char i8042_key_read(chardev_t *d)
  226. {
  227.     char ch;   
  228.  
  229.     while(!(ch = active_read_buff_read())) {
  230.         uint8_t x;
  231.         while (!(i8042_status_read() & i8042_BUFFER_FULL_MASK))
  232.             ;
  233.         x = i8042_data_read();
  234.         if (x & KEY_RELEASE)
  235.             key_released(x ^ KEY_RELEASE);
  236.         else
  237.             active_read_key_pressed(x);
  238.     }
  239.     return ch;
  240. }
  241.  
  242. /** Poll for key press and release events.
  243.  *
  244.  * This function can be used to implement keyboard polling.
  245.  */
  246. void i8042_poll(void)
  247. {
  248.     uint8_t x;
  249.  
  250.     while (((x = i8042_status_read() & i8042_BUFFER_FULL_MASK))) {
  251.         x = i8042_data_read();
  252.         if (x & KEY_RELEASE)
  253.             key_released(x ^ KEY_RELEASE);
  254.         else
  255.             key_pressed(x);
  256.     }
  257. }
  258.  
  259. /** @}
  260.  */
  261.