[2677758] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2003 Josef Cejka
|
---|
| 3 | * Copyright (c) 2005 Jakub Jermar
|
---|
[2677758] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[06e1e95] | 30 | /** @addtogroup genericconsole
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[2677758] | 36 | #include <console/console.h>
|
---|
| 37 | #include <console/chardev.h>
|
---|
| 38 | #include <synch/waitq.h>
|
---|
| 39 | #include <synch/spinlock.h>
|
---|
| 40 | #include <arch/types.h>
|
---|
| 41 | #include <arch.h>
|
---|
[93b84b3] | 42 | #include <func.h>
|
---|
| 43 | #include <print.h>
|
---|
[23684b7] | 44 | #include <atomic.h>
|
---|
[2677758] | 45 |
|
---|
[dabe6333] | 46 | #define BUFLEN 2048
|
---|
| 47 | static char debug_buffer[BUFLEN];
|
---|
| 48 | static size_t offset = 0;
|
---|
| 49 | /** Initialize stdout to something that does not print, but does not fail
|
---|
| 50 | *
|
---|
| 51 | * Save data in some buffer so that it could be retrieved in the debugger
|
---|
| 52 | */
|
---|
| 53 | static void null_putchar(chardev_t *d, const char ch)
|
---|
| 54 | {
|
---|
| 55 | if (offset >= BUFLEN)
|
---|
| 56 | offset = 0;
|
---|
| 57 | debug_buffer[offset++] = ch;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | static chardev_operations_t null_stdout_ops = {
|
---|
| 61 | .write = null_putchar
|
---|
| 62 | };
|
---|
| 63 | chardev_t null_stdout = {
|
---|
| 64 | .name = "null",
|
---|
| 65 | .op = &null_stdout_ops
|
---|
| 66 | };
|
---|
| 67 |
|
---|
[2677758] | 68 | /** Standard input character device. */
|
---|
| 69 | chardev_t *stdin = NULL;
|
---|
[dabe6333] | 70 | chardev_t *stdout = &null_stdout;
|
---|
[2677758] | 71 |
|
---|
[0c8e692] | 72 | /** Get character from character device. Do not echo character.
|
---|
[e8a9dc3] | 73 | *
|
---|
| 74 | * @param chardev Character device.
|
---|
| 75 | *
|
---|
| 76 | * @return Character read.
|
---|
| 77 | */
|
---|
[7f1c620] | 78 | uint8_t _getc(chardev_t *chardev)
|
---|
[e8a9dc3] | 79 | {
|
---|
[7f1c620] | 80 | uint8_t ch;
|
---|
[e8a9dc3] | 81 | ipl_t ipl;
|
---|
| 82 |
|
---|
[36e7ee98] | 83 | if (atomic_get(&haltstate)) {
|
---|
[93b84b3] | 84 | /* If we are here, we are hopefully on the processor, that
|
---|
| 85 | * issued the 'halt' command, so proceed to read the character
|
---|
| 86 | * directly from input
|
---|
| 87 | */
|
---|
| 88 | if (chardev->op->read)
|
---|
| 89 | return chardev->op->read(chardev);
|
---|
| 90 | /* no other way of interacting with user, halt */
|
---|
[36e7ee98] | 91 | if (CPU)
|
---|
| 92 | printf("cpu%d: ", CPU->id);
|
---|
| 93 | else
|
---|
| 94 | printf("cpu: ");
|
---|
| 95 | printf("halted - no kconsole\n");
|
---|
[93b84b3] | 96 | cpu_halt();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[e8a9dc3] | 99 | waitq_sleep(&chardev->wq);
|
---|
| 100 | ipl = interrupts_disable();
|
---|
| 101 | spinlock_lock(&chardev->lock);
|
---|
| 102 | ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN];
|
---|
| 103 | chardev->counter--;
|
---|
| 104 | spinlock_unlock(&chardev->lock);
|
---|
| 105 | interrupts_restore(ipl);
|
---|
| 106 |
|
---|
| 107 | chardev->op->resume(chardev);
|
---|
| 108 |
|
---|
| 109 | return ch;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[2677758] | 112 | /** Get string from character device.
|
---|
| 113 | *
|
---|
| 114 | * Read characters from character device until first occurrence
|
---|
| 115 | * of newline character.
|
---|
| 116 | *
|
---|
| 117 | * @param chardev Character device.
|
---|
[ff3b3197] | 118 | * @param buf Buffer where to store string terminated by '\0'.
|
---|
[abbc16e] | 119 | * @param buflen Size of the buffer.
|
---|
[ff3b3197] | 120 | *
|
---|
| 121 | * @return Number of characters read.
|
---|
[2677758] | 122 | */
|
---|
[ff3b3197] | 123 | count_t gets(chardev_t *chardev, char *buf, size_t buflen)
|
---|
[2677758] | 124 | {
|
---|
| 125 | index_t index = 0;
|
---|
| 126 | char ch;
|
---|
| 127 |
|
---|
| 128 | while (index < buflen) {
|
---|
[e8a9dc3] | 129 | ch = _getc(chardev);
|
---|
| 130 | if (ch == '\b') {
|
---|
| 131 | if (index > 0) {
|
---|
| 132 | index--;
|
---|
| 133 | /* Space backspace, space */
|
---|
| 134 | putchar('\b');
|
---|
| 135 | putchar(' ');
|
---|
| 136 | putchar('\b');
|
---|
| 137 | }
|
---|
| 138 | continue;
|
---|
| 139 | }
|
---|
| 140 | putchar(ch);
|
---|
| 141 |
|
---|
[2677758] | 142 | if (ch == '\n') { /* end of string => write 0, return */
|
---|
[ff3b3197] | 143 | buf[index] = '\0';
|
---|
| 144 | return (count_t) index;
|
---|
[2677758] | 145 | }
|
---|
[ff3b3197] | 146 | buf[index++] = ch;
|
---|
[2677758] | 147 | }
|
---|
[ff3b3197] | 148 | return (count_t) index;
|
---|
[2677758] | 149 | }
|
---|
| 150 |
|
---|
[e8a9dc3] | 151 | /** Get character from device & echo it to screen */
|
---|
[7f1c620] | 152 | uint8_t getc(chardev_t *chardev)
|
---|
[2677758] | 153 | {
|
---|
[7f1c620] | 154 | uint8_t ch;
|
---|
[2677758] | 155 |
|
---|
[e8a9dc3] | 156 | ch = _getc(chardev);
|
---|
| 157 | putchar(ch);
|
---|
[2677758] | 158 | return ch;
|
---|
| 159 | }
|
---|
[973be64e] | 160 |
|
---|
| 161 | void putchar(char c)
|
---|
| 162 | {
|
---|
[93b84b3] | 163 | if (stdout->op->write)
|
---|
| 164 | stdout->op->write(stdout, c);
|
---|
[973be64e] | 165 | }
|
---|
[b45c443] | 166 |
|
---|
[06e1e95] | 167 | /** @}
|
---|
[b45c443] | 168 | */
|
---|