Rev 1708 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
510 | jermar | 1 | /* |
2 | * Copyright (C) 2003 Josef Cejka |
||
3 | * Copyright (C) 2005 Jakub Jermar |
||
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 | |||
1702 | cejka | 30 | /** @addtogroup genericconsole |
31 | * @{ |
||
32 | */ |
||
33 | /** @file |
||
34 | */ |
||
35 | |||
510 | jermar | 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 <typedefs.h> |
||
42 | #include <arch.h> |
||
607 | palkovsky | 43 | #include <func.h> |
44 | #include <print.h> |
||
1104 | jermar | 45 | #include <atomic.h> |
510 | jermar | 46 | |
1050 | palkovsky | 47 | #define BUFLEN 2048 |
48 | static char debug_buffer[BUFLEN]; |
||
49 | static size_t offset = 0; |
||
50 | /** Initialize stdout to something that does not print, but does not fail |
||
51 | * |
||
52 | * Save data in some buffer so that it could be retrieved in the debugger |
||
53 | */ |
||
54 | static void null_putchar(chardev_t *d, const char ch) |
||
55 | { |
||
56 | if (offset >= BUFLEN) |
||
57 | offset = 0; |
||
58 | debug_buffer[offset++] = ch; |
||
59 | } |
||
60 | |||
61 | static chardev_operations_t null_stdout_ops = { |
||
62 | .write = null_putchar |
||
63 | }; |
||
64 | chardev_t null_stdout = { |
||
65 | .name = "null", |
||
66 | .op = &null_stdout_ops |
||
67 | }; |
||
68 | |||
510 | jermar | 69 | /** Standard input character device. */ |
70 | chardev_t *stdin = NULL; |
||
1050 | palkovsky | 71 | chardev_t *stdout = &null_stdout; |
510 | jermar | 72 | |
601 | palkovsky | 73 | /** Get character from character device. Do not echo character. |
588 | palkovsky | 74 | * |
75 | * @param chardev Character device. |
||
76 | * |
||
77 | * @return Character read. |
||
78 | */ |
||
1780 | jermar | 79 | uint8_t _getc(chardev_t *chardev) |
588 | palkovsky | 80 | { |
1780 | jermar | 81 | uint8_t ch; |
588 | palkovsky | 82 | ipl_t ipl; |
83 | |||
631 | palkovsky | 84 | if (atomic_get(&haltstate)) { |
607 | palkovsky | 85 | /* If we are here, we are hopefully on the processor, that |
86 | * issued the 'halt' command, so proceed to read the character |
||
87 | * directly from input |
||
88 | */ |
||
89 | if (chardev->op->read) |
||
90 | return chardev->op->read(chardev); |
||
91 | /* no other way of interacting with user, halt */ |
||
631 | palkovsky | 92 | if (CPU) |
93 | printf("cpu%d: ", CPU->id); |
||
94 | else |
||
95 | printf("cpu: "); |
||
96 | printf("halted - no kconsole\n"); |
||
607 | palkovsky | 97 | cpu_halt(); |
98 | } |
||
99 | |||
588 | palkovsky | 100 | waitq_sleep(&chardev->wq); |
101 | ipl = interrupts_disable(); |
||
102 | spinlock_lock(&chardev->lock); |
||
103 | ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN]; |
||
104 | chardev->counter--; |
||
105 | spinlock_unlock(&chardev->lock); |
||
106 | interrupts_restore(ipl); |
||
107 | |||
108 | chardev->op->resume(chardev); |
||
109 | |||
110 | return ch; |
||
111 | } |
||
112 | |||
510 | jermar | 113 | /** Get string from character device. |
114 | * |
||
115 | * Read characters from character device until first occurrence |
||
116 | * of newline character. |
||
117 | * |
||
118 | * @param chardev Character device. |
||
517 | jermar | 119 | * @param buf Buffer where to store string terminated by '\0'. |
1708 | jermar | 120 | * @param buflen Size of the buffer. |
517 | jermar | 121 | * |
122 | * @return Number of characters read. |
||
510 | jermar | 123 | */ |
517 | jermar | 124 | count_t gets(chardev_t *chardev, char *buf, size_t buflen) |
510 | jermar | 125 | { |
126 | index_t index = 0; |
||
127 | char ch; |
||
128 | |||
129 | while (index < buflen) { |
||
588 | palkovsky | 130 | ch = _getc(chardev); |
131 | if (ch == '\b') { |
||
132 | if (index > 0) { |
||
133 | index--; |
||
134 | /* Space backspace, space */ |
||
135 | putchar('\b'); |
||
136 | putchar(' '); |
||
137 | putchar('\b'); |
||
138 | } |
||
139 | continue; |
||
140 | } |
||
141 | putchar(ch); |
||
142 | |||
510 | jermar | 143 | if (ch == '\n') { /* end of string => write 0, return */ |
517 | jermar | 144 | buf[index] = '\0'; |
145 | return (count_t) index; |
||
510 | jermar | 146 | } |
517 | jermar | 147 | buf[index++] = ch; |
510 | jermar | 148 | } |
517 | jermar | 149 | return (count_t) index; |
510 | jermar | 150 | } |
151 | |||
588 | palkovsky | 152 | /** Get character from device & echo it to screen */ |
1780 | jermar | 153 | uint8_t getc(chardev_t *chardev) |
510 | jermar | 154 | { |
1780 | jermar | 155 | uint8_t ch; |
510 | jermar | 156 | |
588 | palkovsky | 157 | ch = _getc(chardev); |
158 | putchar(ch); |
||
510 | jermar | 159 | return ch; |
160 | } |
||
575 | palkovsky | 161 | |
162 | void putchar(char c) |
||
163 | { |
||
607 | palkovsky | 164 | if (stdout->op->write) |
165 | stdout->op->write(stdout, c); |
||
575 | palkovsky | 166 | } |
1702 | cejka | 167 | |
168 | /** @} |
||
169 | */ |
||
170 |