Subversion Repositories HelenOS

Rev

Go to most recent revision | Blame | 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   NS 16550 serial port / keyboard driver.
  35.  */
  36.  
  37. #include <genarch/kbd/ns16550.h>
  38. #include <genarch/kbd/key.h>
  39. #include <genarch/kbd/scanc.h>
  40. #include <genarch/kbd/scanc_sun.h>
  41. #include <arch/drivers/ns16550.h>
  42. #include <arch/interrupt.h>
  43. #include <cpu.h>
  44. #include <arch/asm.h>
  45. #include <arch.h>
  46. #include <typedefs.h>
  47. #include <console/chardev.h>
  48. #include <console/console.h>
  49. #include <interrupt.h>
  50.  
  51. #define LSR_DATA_READY  0x01
  52.  
  53. /*
  54.  * These codes read from ns16550 data register are silently ignored.
  55.  */
  56. #define IGNORE_CODE 0x7f        /* all keys up */
  57.  
  58. static void ns16550_suspend(chardev_t *);
  59. static void ns16550_resume(chardev_t *);
  60.  
  61. chardev_t kbrd;
  62. static chardev_operations_t ops = {
  63.     .suspend = ns16550_suspend,
  64.     .resume = ns16550_resume,
  65.     .read = key_read
  66. };
  67.  
  68. void ns16550_interrupt(int n, istate_t *istate);
  69. void ns16550_wait(void);
  70.  
  71. /** Initialize keyboard and service interrupts using kernel routine */
  72. void ns16550_grab(void)
  73. {
  74.     /* TODO */
  75. }
  76.  
  77. /** Resume the former interrupt vector */
  78. void ns16550_release(void)
  79. {
  80.     /* TODO */
  81. }
  82.  
  83. /** Initialize ns16550. */
  84. void ns16550_init(void)
  85. {
  86.     ns16550_grab();
  87.     chardev_initialize("ns16550_kbd", &kbrd, &ops);
  88.     stdin = &kbrd;
  89. }
  90.  
  91. /** Process ns16550 interrupt.
  92.  *
  93.  * @param n Interrupt vector.
  94.  * @param istate Interrupted state.
  95.  */
  96. void ns16550_interrupt(int n, istate_t *istate)
  97. {
  98.     /* TODO */
  99. }
  100.  
  101. /** Wait until the controller reads its data. */
  102. void ns16550_wait(void) {
  103. }
  104.  
  105. /* Called from getc(). */
  106. void ns16550_resume(chardev_t *d)
  107. {
  108. }
  109.  
  110. /* Called from getc(). */
  111. void ns16550_suspend(chardev_t *d)
  112. {
  113. }
  114.  
  115. char key_read(chardev_t *d)
  116. {
  117.     char ch;   
  118.  
  119.     while(!(ch = active_read_buff_read())) {
  120.         uint8_t x;
  121.         while (!(ns16550_lsr_read() & LSR_DATA_READY))
  122.             ;
  123.         x = ns16550_rbr_read();
  124.         if (x != IGNORE_CODE) {
  125.             if (x & KEY_RELEASE)
  126.                 key_released(x ^ KEY_RELEASE);
  127.             else
  128.                 active_read_key_pressed(x);
  129.         }
  130.     }
  131.     return ch;
  132. }
  133.  
  134. /** Poll for key press and release events.
  135.  *
  136.  * This function can be used to implement keyboard polling.
  137.  */
  138. void ns16550_poll(void)
  139. {
  140.     uint8_t x;
  141.  
  142.     while (((x = ns16550_lsr_read() & LSR_DATA_READY))) {
  143.         x = ns16550_rbr_read();
  144.         if (x != IGNORE_CODE) {
  145.             if (x & KEY_RELEASE)
  146.                 key_released(x ^ KEY_RELEASE);
  147.             else
  148.                 key_pressed(x);
  149.         }
  150.     }
  151. }
  152.  
  153. /** @}
  154.  */
  155.