Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2005 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 ia64   
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <arch/ski/ski.h>
  36. #include <console/console.h>
  37. #include <console/chardev.h>
  38. #include <arch/interrupt.h>
  39. #include <sysinfo/sysinfo.h>
  40. #include <arch/types.h>
  41. #include <ddi/device.h>
  42. #include <ddi/irq.h>
  43. #include <ipc/irq.h>
  44. #include <proc/thread.h>
  45. #include <synch/spinlock.h>
  46. #include <arch/asm.h>
  47. #include <arch/drivers/kbd.h>
  48.  
  49. #define SKI_KBD_INR 0
  50.  
  51. static irq_t ski_kbd_irq;
  52. static devno_t ski_kbd_devno;
  53.  
  54. chardev_t ski_console;
  55. chardev_t ski_uconsole;
  56.  
  57. static bool kbd_disabled;
  58.  
  59. static void ski_putchar(chardev_t *d, const char ch);
  60. static int32_t ski_getchar(void);
  61.  
  62. /** Display character on debug console
  63.  *
  64.  * Use SSC (Simulator System Call) to
  65.  * display character on debug console.
  66.  *
  67.  * @param d Character device.
  68.  * @param ch Character to be printed.
  69.  */
  70. void ski_putchar(chardev_t *d, const char ch)
  71. {
  72.     asm volatile (
  73.         "mov r15 = %0\n"
  74.         "mov r32 = %1\n"    /* r32 is in0 */
  75.         "break 0x80000\n"   /* modifies r8 */
  76.         :
  77.         : "i" (SKI_PUTCHAR), "r" (ch)
  78.         : "r15", "in0", "r8"
  79.     );
  80.    
  81.     if (ch == '\n')
  82.         ski_putchar(d, '\r');
  83. }
  84.  
  85. /** Ask debug console if a key was pressed.
  86.  *
  87.  * Use SSC (Simulator System Call) to
  88.  * get character from debug console.
  89.  *
  90.  * This call is non-blocking.
  91.  *
  92.  * @return ASCII code of pressed key or 0 if no key pressed.
  93.  */
  94. int32_t ski_getchar(void)
  95. {
  96.     uint64_t ch;
  97.    
  98.     asm volatile (
  99.         "mov r15 = %1\n"
  100.         "break 0x80000;;\n" /* modifies r8 */
  101.         "mov %0 = r8;;\n"      
  102.  
  103.         : "=r" (ch)
  104.         : "i" (SKI_GETCHAR)
  105.         : "r15", "r8"
  106.     );
  107.  
  108.     return (int32_t) ch;
  109. }
  110.  
  111. /**
  112.  * This is a blocking wrapper for ski_getchar().
  113.  * To be used when the kernel crashes.
  114.  */
  115. static char ski_getchar_blocking(chardev_t *d)
  116. {
  117.     int ch;
  118.  
  119.     while(!(ch = ski_getchar()))
  120.         ;
  121.     if (ch == '\r')
  122.         ch = '\n';
  123.     return (char) ch;
  124. }
  125.  
  126. /** Ask keyboard if a key was pressed. */
  127. static void poll_keyboard(void)
  128. {
  129.     char ch;
  130.     static char last;
  131.     ipl_t ipl;
  132.  
  133.     ipl = interrupts_disable();
  134.  
  135.     if (kbd_disabled) {
  136.         interrupts_restore(ipl);
  137.         return;
  138.     }
  139.        
  140.     spinlock_lock(&ski_kbd_irq.lock);
  141.  
  142.     ch = ski_getchar();
  143.     if(ch == '\r')
  144.         ch = '\n';
  145.     if (ch) {
  146.         if (ski_kbd_irq.notif_cfg.notify &&
  147.             ski_kbd_irq.notif_cfg.answerbox) {
  148.             chardev_push_character(&ski_uconsole, ch);
  149.             ipc_irq_send_notif(&ski_kbd_irq);
  150.         } else {
  151.             chardev_push_character(&ski_console, ch);
  152.         }  
  153.         last = ch;
  154.         spinlock_unlock(&ski_kbd_irq.lock);
  155.         interrupts_restore(ipl);
  156.         return;
  157.     }
  158.  
  159.     if (last) {
  160.         if (ski_kbd_irq.notif_cfg.notify &&
  161.             ski_kbd_irq.notif_cfg.answerbox) {
  162.             chardev_push_character(&ski_uconsole, 0);
  163.             ipc_irq_send_notif(&ski_kbd_irq);
  164.         }
  165.         last = 0;
  166.     }
  167.  
  168.     spinlock_unlock(&ski_kbd_irq.lock);
  169.     interrupts_restore(ipl);
  170. }
  171.  
  172. /* Called from getc(). */
  173. static void ski_kbd_enable(chardev_t *d)
  174. {
  175.     kbd_disabled = false;
  176. }
  177.  
  178. /* Called from getc(). */
  179. static void ski_kbd_disable(chardev_t *d)
  180. {
  181.     kbd_disabled = true;   
  182. }
  183.  
  184. /** Decline to service hardware IRQ.
  185.  *
  186.  * This is only a virtual IRQ, so always decline.
  187.  *
  188.  * @return Always IRQ_DECLINE.
  189.  */
  190. static irq_ownership_t ski_kbd_claim(void)
  191. {
  192.     return IRQ_DECLINE;
  193. }
  194.  
  195. static chardev_operations_t ski_ops = {
  196.     .resume = ski_kbd_enable,
  197.     .suspend = ski_kbd_disable,
  198.     .write = ski_putchar,
  199.     .read = ski_getchar_blocking
  200. };
  201.  
  202. /** Initialize debug console
  203.  *
  204.  * Issue SSC (Simulator System Call) to
  205.  * to open debug console.
  206.  */
  207. void ski_init_console(void)
  208. {
  209.     asm volatile (
  210.         "mov r15 = %0\n"
  211.         "break 0x80000\n"
  212.         :
  213.         : "i" (SKI_INIT_CONSOLE)
  214.         : "r15", "r8"
  215.     );
  216.  
  217.     chardev_initialize("ski_console", &ski_console, &ski_ops);
  218.     chardev_initialize("ski_uconsole", &ski_uconsole, &ski_ops);
  219.     stdin = &ski_console;
  220.     stdout = &ski_console;
  221.  
  222.     ski_kbd_devno = device_assign_devno();
  223.    
  224.     irq_initialize(&ski_kbd_irq);
  225.     ski_kbd_irq.inr = SKI_KBD_INR;
  226.     ski_kbd_irq.devno = ski_kbd_devno;
  227.     ski_kbd_irq.claim = ski_kbd_claim;
  228.     irq_register(&ski_kbd_irq);
  229.  
  230.     sysinfo_set_item_val("kbd", NULL, true);
  231.     sysinfo_set_item_val("kbd.inr", NULL, SKI_KBD_INR);
  232.     sysinfo_set_item_val("kbd.devno", NULL, ski_kbd_devno);
  233.     sysinfo_set_item_val("kbd.type", NULL, KBD_SKI);
  234.  
  235.     sysinfo_set_item_val("fb", NULL, false);
  236. }
  237.  
  238. void ski_kbd_grab(void)
  239. {
  240.     ipl_t ipl = interrupts_disable();
  241.     spinlock_lock(&ski_kbd_irq.lock);
  242.     ski_kbd_irq.notif_cfg.notify = false;
  243.     spinlock_unlock(&ski_kbd_irq.lock);
  244.     interrupts_restore(ipl);
  245. }
  246.  
  247. void ski_kbd_release(void)
  248. {
  249.     ipl_t ipl = interrupts_disable();
  250.     spinlock_lock(&ski_kbd_irq.lock);
  251.     if (ski_kbd_irq.notif_cfg.answerbox)
  252.         ski_kbd_irq.notif_cfg.notify = true;
  253.     spinlock_unlock(&ski_kbd_irq.lock);
  254.     interrupts_restore(ipl);
  255. }
  256.  
  257.  
  258. #define POLL_INTERVAL       50000       /* 50 ms */
  259.  
  260. /** Kernel thread for polling keyboard. */
  261. void kkbdpoll(void *arg)
  262. {
  263.     while (1) {
  264.         poll_keyboard();
  265.         thread_usleep(POLL_INTERVAL);
  266.     }
  267. }
  268.  
  269. /** @}
  270.  */
  271.