Subversion Repositories HelenOS

Rev

Rev 1920 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2001-2006 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 <ddi/irq.h>
  43. #include <arch/interrupt.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.  
  53. #define LSR_DATA_READY  0x01
  54.  
  55. /** Structure representing the ns16550. */
  56. static ns16550_t ns16550;
  57.  
  58. /** Structure for ns16550's IRQ. */
  59. static irq_t ns16550_irq;
  60.  
  61. /*
  62.  * These codes read from ns16550 data register are silently ignored.
  63.  */
  64. #define IGNORE_CODE 0x7f        /* all keys up */
  65.  
  66. static void ns16550_suspend(chardev_t *);
  67. static void ns16550_resume(chardev_t *);
  68.  
  69. static chardev_operations_t ops = {
  70.     .suspend = ns16550_suspend,
  71.     .resume = ns16550_resume,
  72.     .read = ns16550_key_read
  73. };
  74.  
  75. void ns16550_interrupt(int n, istate_t *istate);
  76. void ns16550_wait(void);
  77.  
  78. /** Initialize keyboard and service interrupts using kernel routine */
  79. void ns16550_grab(void)
  80. {
  81.     /* TODO */
  82. }
  83.  
  84. /** Resume the former interrupt vector */
  85. void ns16550_release(void)
  86. {
  87.     /* TODO */
  88. }
  89.  
  90. /** Initialize ns16550.
  91.  *
  92.  * @param devno Device number.
  93.  * @param inr Interrupt number.
  94.  * @param vaddr Virtual address of device's registers.
  95.  */
  96. void ns16550_init(devno_t devno, inr_t inr, uintptr_t vaddr)
  97. {
  98.     ns16550_grab();
  99.     chardev_initialize("ns16550_kbd", &kbrd, &ops);
  100.     stdin = &kbrd;
  101.    
  102.     ns16550.devno = devno;
  103.     ns16550.reg = (uint8_t *) vaddr;
  104.    
  105.     irq_initialize(&ns16550_irq);
  106.     ns16550_irq.devno = devno;
  107.     ns16550_irq.inr = inr;
  108.     ns16550_irq.claim = ns16550_claim;
  109.     ns16550_irq.handler = ns16550_irq_handler;
  110.     irq_register(&ns16550_irq);
  111.    
  112.     sysinfo_set_item_val("kbd", NULL, true);
  113.     sysinfo_set_item_val("kbd.devno", NULL, devno);
  114.     sysinfo_set_item_val("kbd.inr", NULL, inr);
  115.     sysinfo_set_item_val("kbd.address.virtual", NULL, vaddr);
  116.    
  117.     ns16550_ier_write(&ns16550, IER_ERBFI);     /* enable receiver interrupt */
  118.    
  119.     while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY)
  120.         (void) ns16550_rbr_read(&ns16550);
  121. }
  122.  
  123. /** Process ns16550 interrupt.
  124.  *
  125.  * @param n Interrupt vector.
  126.  * @param istate Interrupted state.
  127.  */
  128. void ns16550_interrupt(int n, istate_t *istate)
  129. {
  130.     /* TODO */
  131. }
  132.  
  133. /** Wait until the controller reads its data. */
  134. void ns16550_wait(void)
  135. {
  136. }
  137.  
  138. /* Called from getc(). */
  139. void ns16550_resume(chardev_t *d)
  140. {
  141. }
  142.  
  143. /* Called from getc(). */
  144. void ns16550_suspend(chardev_t *d)
  145. {
  146. }
  147.  
  148. char ns16550_key_read(chardev_t *d)
  149. {
  150.     char ch;   
  151.  
  152.     while(!(ch = active_read_buff_read())) {
  153.         uint8_t x;
  154.         while (!(ns16550_lsr_read(&ns16550) & LSR_DATA_READY))
  155.             ;
  156.         x = ns16550_rbr_read(&ns16550);
  157.         if (x != IGNORE_CODE) {
  158.             if (x & KEY_RELEASE)
  159.                 key_released(x ^ KEY_RELEASE);
  160.             else
  161.                 active_read_key_pressed(x);
  162.         }
  163.     }
  164.     return ch;
  165. }
  166.  
  167. /** Poll for key press and release events.
  168.  *
  169.  * This function can be used to implement keyboard polling.
  170.  */
  171. void ns16550_poll(void)
  172. {
  173.     uint8_t x;
  174.  
  175.     while (ns16550_lsr_read(&ns16550) & LSR_DATA_READY) {
  176.         x = ns16550_rbr_read(&ns16550);
  177.         if (x != IGNORE_CODE) {
  178.             if (x & KEY_RELEASE)
  179.                 key_released(x ^ KEY_RELEASE);
  180.             else
  181.                 key_pressed(x);
  182.         }
  183.     }
  184. }
  185.  
  186. irq_ownership_t ns16550_claim(void)
  187. {
  188.     /* TODO */
  189.     return IRQ_ACCEPT;
  190. }
  191.  
  192. void ns16550_irq_handler(irq_t *irq, void *arg, ...)
  193. {
  194.     panic("Not yet implemented.\n");
  195. }
  196.  
  197. /** @}
  198.  */
  199.