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