| 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 kernel_ia64
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <arch/drivers/ski.h>
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <console/console.h>
|
|---|
| 38 | #include <console/chardev.h>
|
|---|
| 39 | #include <ddi/ddi.h>
|
|---|
| 40 | #include <sysinfo/sysinfo.h>
|
|---|
| 41 | #include <stdint.h>
|
|---|
| 42 | #include <proc/thread.h>
|
|---|
| 43 | #include <synch/spinlock.h>
|
|---|
| 44 | #include <arch/asm.h>
|
|---|
| 45 | #include <arch/drivers/kbd.h>
|
|---|
| 46 | #include <str.h>
|
|---|
| 47 | #include <arch.h>
|
|---|
| 48 | #include <stdlib.h>
|
|---|
| 49 |
|
|---|
| 50 | enum {
|
|---|
| 51 | /** Interval between polling in microseconds */
|
|---|
| 52 | POLL_INTERVAL = 10000, /* 0.01 s */
|
|---|
| 53 |
|
|---|
| 54 | /** Max. number of characters to pull out at a time */
|
|---|
| 55 | POLL_LIMIT = 30,
|
|---|
| 56 |
|
|---|
| 57 | SKI_INIT_CONSOLE = 20,
|
|---|
| 58 | SKI_GETCHAR = 21,
|
|---|
| 59 | SKI_PUTCHAR = 31
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | static void ski_write(outdev_t *, const char *, size_t);
|
|---|
| 63 |
|
|---|
| 64 | static outdev_operations_t skidev_ops = {
|
|---|
| 65 | .write = ski_write,
|
|---|
| 66 | .redraw = NULL,
|
|---|
| 67 | .scroll_up = NULL,
|
|---|
| 68 | .scroll_down = NULL
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | static ski_instance_t *instance = NULL;
|
|---|
| 72 | static parea_t ski_parea;
|
|---|
| 73 |
|
|---|
| 74 | /** Ask debug console if a key was pressed.
|
|---|
| 75 | *
|
|---|
| 76 | * Use SSC (Simulator System Call) to
|
|---|
| 77 | * get character from debug console.
|
|---|
| 78 | *
|
|---|
| 79 | * This call is non-blocking.
|
|---|
| 80 | *
|
|---|
| 81 | * @return ASCII code of pressed key or 0 if no key pressed.
|
|---|
| 82 | *
|
|---|
| 83 | */
|
|---|
| 84 | static char32_t ski_getchar(void)
|
|---|
| 85 | {
|
|---|
| 86 | uint64_t ch;
|
|---|
| 87 |
|
|---|
| 88 | asm volatile (
|
|---|
| 89 | "mov r15 = %1\n"
|
|---|
| 90 | "break 0x80000;;\n" /* modifies r8 */
|
|---|
| 91 | "mov %0 = r8;;\n"
|
|---|
| 92 |
|
|---|
| 93 | : "=r" (ch)
|
|---|
| 94 | : "i" (SKI_GETCHAR)
|
|---|
| 95 | : "r15", "r8"
|
|---|
| 96 | );
|
|---|
| 97 |
|
|---|
| 98 | return (char32_t) ch;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /** Ask keyboard if a key was pressed.
|
|---|
| 102 | *
|
|---|
| 103 | * If so, it will repeat and pull up to POLL_LIMIT characters.
|
|---|
| 104 | */
|
|---|
| 105 | static void poll_keyboard(ski_instance_t *instance)
|
|---|
| 106 | {
|
|---|
| 107 | int count = POLL_LIMIT;
|
|---|
| 108 |
|
|---|
| 109 | if (ski_parea.mapped && !console_override)
|
|---|
| 110 | return;
|
|---|
| 111 |
|
|---|
| 112 | while (count > 0) {
|
|---|
| 113 | char32_t ch = ski_getchar();
|
|---|
| 114 |
|
|---|
| 115 | if (ch == '\0')
|
|---|
| 116 | break;
|
|---|
| 117 |
|
|---|
| 118 | indev_push_character(instance->srlnin, ch);
|
|---|
| 119 | --count;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /** Kernel thread for polling keyboard. */
|
|---|
| 124 | static void kskipoll(void *arg)
|
|---|
| 125 | {
|
|---|
| 126 | ski_instance_t *instance = (ski_instance_t *) arg;
|
|---|
| 127 |
|
|---|
| 128 | while (true) {
|
|---|
| 129 | poll_keyboard(instance);
|
|---|
| 130 | thread_usleep(POLL_INTERVAL);
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /** Initialize debug console
|
|---|
| 135 | *
|
|---|
| 136 | * Issue SSC (Simulator System Call) to
|
|---|
| 137 | * to open debug console.
|
|---|
| 138 | *
|
|---|
| 139 | */
|
|---|
| 140 | static void ski_init(void)
|
|---|
| 141 | {
|
|---|
| 142 | uintptr_t faddr;
|
|---|
| 143 |
|
|---|
| 144 | if (instance)
|
|---|
| 145 | return;
|
|---|
| 146 |
|
|---|
| 147 | asm volatile (
|
|---|
| 148 | "mov r15 = %0\n"
|
|---|
| 149 | "break 0x80000\n"
|
|---|
| 150 | :
|
|---|
| 151 | : "i" (SKI_INIT_CONSOLE)
|
|---|
| 152 | : "r15", "r8"
|
|---|
| 153 | );
|
|---|
| 154 |
|
|---|
| 155 | faddr = frame_alloc(1, FRAME_LOWMEM | FRAME_ATOMIC, 0);
|
|---|
| 156 | if (faddr == 0)
|
|---|
| 157 | panic("Cannot allocate page for ski console.");
|
|---|
| 158 |
|
|---|
| 159 | ddi_parea_init(&ski_parea);
|
|---|
| 160 | ski_parea.pbase = faddr;
|
|---|
| 161 | ski_parea.frames = 1;
|
|---|
| 162 | ski_parea.unpriv = false;
|
|---|
| 163 | ski_parea.mapped = false;
|
|---|
| 164 | ddi_parea_register(&ski_parea);
|
|---|
| 165 |
|
|---|
| 166 | sysinfo_set_item_val("ski.paddr", NULL, (sysarg_t) faddr);
|
|---|
| 167 |
|
|---|
| 168 | instance = malloc(sizeof(ski_instance_t));
|
|---|
| 169 |
|
|---|
| 170 | if (instance) {
|
|---|
| 171 | instance->thread = thread_create(kskipoll, instance, TASK,
|
|---|
| 172 | THREAD_FLAG_UNCOUNTED, "kskipoll");
|
|---|
| 173 |
|
|---|
| 174 | if (!instance->thread) {
|
|---|
| 175 | free(instance);
|
|---|
| 176 | instance = NULL;
|
|---|
| 177 | return;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | instance->srlnin = NULL;
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | static void ski_do_putchar(uint8_t ch)
|
|---|
| 185 | {
|
|---|
| 186 | asm volatile (
|
|---|
| 187 | "mov r15 = %[cmd]\n"
|
|---|
| 188 | "mov r32 = %[ch]\n" /* r32 is in0 */
|
|---|
| 189 | "break 0x80000\n" /* modifies r8 */
|
|---|
| 190 | :
|
|---|
| 191 | : [cmd] "i" (SKI_PUTCHAR), [ch] "r" (ch)
|
|---|
| 192 | : "r15", "in0", "r8"
|
|---|
| 193 | );
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /** Display character on debug console
|
|---|
| 197 | *
|
|---|
| 198 | * Use SSC (Simulator System Call) to
|
|---|
| 199 | * display character on debug console.
|
|---|
| 200 | *
|
|---|
| 201 | * @param dev Character device.
|
|---|
| 202 | * @param ch Character to be printed.
|
|---|
| 203 | *
|
|---|
| 204 | */
|
|---|
| 205 | static void ski_write(outdev_t *dev, const char *s, size_t n)
|
|---|
| 206 | {
|
|---|
| 207 | /* If the userspace owns the console, do not output anything. */
|
|---|
| 208 | if (ski_parea.mapped && !console_override)
|
|---|
| 209 | return;
|
|---|
| 210 |
|
|---|
| 211 | const char *top = s + n;
|
|---|
| 212 | assert(top >= s);
|
|---|
| 213 |
|
|---|
| 214 | for (; s < top; s++) {
|
|---|
| 215 | if (*s == '\n')
|
|---|
| 216 | ski_do_putchar('\r');
|
|---|
| 217 |
|
|---|
| 218 | ski_do_putchar((uint8_t) *s);
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | outdev_t *skiout_init(void)
|
|---|
| 223 | {
|
|---|
| 224 | ski_init();
|
|---|
| 225 | if (!instance)
|
|---|
| 226 | return NULL;
|
|---|
| 227 |
|
|---|
| 228 | outdev_t *skidev = malloc(sizeof(outdev_t));
|
|---|
| 229 | if (!skidev)
|
|---|
| 230 | return NULL;
|
|---|
| 231 |
|
|---|
| 232 | outdev_initialize("skidev", skidev, &skidev_ops);
|
|---|
| 233 | skidev->data = instance;
|
|---|
| 234 |
|
|---|
| 235 | if (!fb_exported) {
|
|---|
| 236 | /*
|
|---|
| 237 | * This is the necessary evil until
|
|---|
| 238 | * the userspace driver is entirely
|
|---|
| 239 | * self-sufficient.
|
|---|
| 240 | */
|
|---|
| 241 | sysinfo_set_item_val("fb", NULL, true);
|
|---|
| 242 | sysinfo_set_item_val("fb.kind", NULL, 6);
|
|---|
| 243 |
|
|---|
| 244 | fb_exported = true;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | return skidev;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | ski_instance_t *skiin_init(void)
|
|---|
| 251 | {
|
|---|
| 252 | ski_init();
|
|---|
| 253 | return instance;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | void skiin_wire(ski_instance_t *instance, indev_t *srlnin)
|
|---|
| 257 | {
|
|---|
| 258 | assert(instance);
|
|---|
| 259 | assert(srlnin);
|
|---|
| 260 |
|
|---|
| 261 | instance->srlnin = srlnin;
|
|---|
| 262 | thread_start(instance->thread);
|
|---|
| 263 |
|
|---|
| 264 | sysinfo_set_item_val("kbd", NULL, true);
|
|---|
| 265 | sysinfo_set_item_val("kbd.type", NULL, KBD_SKI);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /** @}
|
|---|
| 269 | */
|
|---|