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