Rev 3618 | Rev 3716 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3618 | Rev 3672 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Ondrej Palkovsky |
2 | * Copyright (c) 2006 Ondrej Palkovsky |
3 | * Copyright (c) 2008 Martin Decky |
3 | * Copyright (c) 2008 Martin Decky |
4 | * Copyright (c) 2008 Pavel Rimsky |
4 | * Copyright (c) 2008 Pavel Rimsky |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
9 | * are met: |
10 | * |
10 | * |
11 | * - Redistributions of source code must retain the above copyright |
11 | * - Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * - Redistributions in binary form must reproduce the above copyright |
13 | * - Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * - The name of the author may not be used to endorse or promote products |
16 | * - The name of the author may not be used to endorse or promote products |
17 | * derived from this software without specific prior written permission. |
17 | * derived from this software without specific prior written permission. |
18 | * |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | /** @defgroup sgcnfb SGCN |
31 | /** @defgroup sgcnfb SGCN |
32 | * @brief userland driver of the Serengeti console output |
32 | * @brief userland driver of the Serengeti console output |
33 | * @{ |
33 | * @{ |
34 | */ |
34 | */ |
35 | /** @file |
35 | /** @file |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <async.h> |
38 | #include <async.h> |
39 | #include <ipc/ipc.h> |
39 | #include <ipc/ipc.h> |
40 | #include <ipc/fb.h> |
40 | #include <ipc/fb.h> |
41 | #include <sysinfo.h> |
41 | #include <sysinfo.h> |
42 | #include <as.h> |
42 | #include <as.h> |
43 | #include <errno.h> |
43 | #include <errno.h> |
44 | #include <stdio.h> |
44 | #include <stdio.h> |
45 | #include <ddi.h> |
45 | #include <ddi.h> |
46 | 46 | ||
47 | #include "serial_console.h" |
47 | #include "serial_console.h" |
48 | #include "sgcn.h" |
48 | #include "sgcn.h" |
49 | 49 | ||
50 | #define WIDTH 80 |
50 | #define WIDTH 80 |
51 | #define HEIGHT 24 |
51 | #define HEIGHT 24 |
52 | 52 | ||
53 | /** |
53 | /** |
54 | * Virtual address mapped to SRAM. |
54 | * Virtual address mapped to SRAM. |
55 | */ |
55 | */ |
56 | static uintptr_t sram_virt_addr; |
56 | static uintptr_t sram_virt_addr; |
57 | 57 | ||
58 | /** |
58 | /** |
59 | * SGCN buffer offset within SGCN. |
59 | * SGCN buffer offset within SGCN. |
60 | */ |
60 | */ |
61 | static uintptr_t sram_buffer_offset; |
61 | static uintptr_t sram_buffer_offset; |
62 | 62 | ||
63 | /* Allow only 1 connection */ |
63 | /* Allow only 1 connection */ |
64 | static int client_connected = 0; |
64 | static int client_connected = 0; |
65 | 65 | ||
66 | /** |
66 | /** |
67 | * SGCN buffer header. It is placed at the very beginning of the SGCN |
67 | * SGCN buffer header. It is placed at the very beginning of the SGCN |
68 | * buffer. |
68 | * buffer. |
69 | */ |
69 | */ |
70 | typedef struct { |
70 | typedef struct { |
71 | /** hard-wired to "CON" */ |
71 | /** hard-wired to "CON" */ |
72 | char magic[4]; |
72 | char magic[4]; |
73 | 73 | ||
74 | /** we don't need this */ |
74 | /** we don't need this */ |
75 | char unused[24]; |
75 | char unused[24]; |
76 | 76 | ||
77 | /** offset within the SGCN buffer of the output buffer start */ |
77 | /** offset within the SGCN buffer of the output buffer start */ |
78 | uint32_t out_begin; |
78 | uint32_t out_begin; |
79 | 79 | ||
80 | /** offset within the SGCN buffer of the output buffer end */ |
80 | /** offset within the SGCN buffer of the output buffer end */ |
81 | uint32_t out_end; |
81 | uint32_t out_end; |
82 | 82 | ||
83 | /** offset within the SGCN buffer of the output buffer read pointer */ |
83 | /** offset within the SGCN buffer of the output buffer read pointer */ |
84 | uint32_t out_rdptr; |
84 | uint32_t out_rdptr; |
85 | 85 | ||
86 | /** offset within the SGCN buffer of the output buffer write pointer */ |
86 | /** offset within the SGCN buffer of the output buffer write pointer */ |
87 | uint32_t out_wrptr; |
87 | uint32_t out_wrptr; |
88 | } __attribute__ ((packed)) sgcn_buffer_header_t; |
88 | } __attribute__ ((packed)) sgcn_buffer_header_t; |
89 | 89 | ||
90 | 90 | ||
91 | /* |
91 | /* |
92 | * Returns a pointer to the object of a given type which is placed at the given |
92 | * Returns a pointer to the object of a given type which is placed at the given |
93 | * offset from the console buffer beginning. |
93 | * offset from the console buffer beginning. |
94 | */ |
94 | */ |
95 | #define SGCN_BUFFER(type, offset) \ |
95 | #define SGCN_BUFFER(type, offset) \ |
96 | ((type *) (sram_virt_addr + sram_buffer_offset + (offset))) |
96 | ((type *) (sram_virt_addr + sram_buffer_offset + (offset))) |
97 | 97 | ||
98 | /** Returns a pointer to the console buffer header. */ |
98 | /** Returns a pointer to the console buffer header. */ |
99 | #define SGCN_BUFFER_HEADER (SGCN_BUFFER(sgcn_buffer_header_t, 0)) |
99 | #define SGCN_BUFFER_HEADER (SGCN_BUFFER(sgcn_buffer_header_t, 0)) |
100 | 100 | ||
101 | /** |
101 | /** |
102 | * Pushes the character to the SGCN serial. |
102 | * Pushes the character to the SGCN serial. |
103 | * @param c character to be pushed |
103 | * @param c character to be pushed |
104 | */ |
104 | */ |
105 | static void sgcn_putc(char c) |
105 | static void sgcn_putc(char c) |
106 | { |
106 | { |
107 | uint32_t begin = SGCN_BUFFER_HEADER->out_begin; |
107 | uint32_t begin = SGCN_BUFFER_HEADER->out_begin; |
108 | uint32_t end = SGCN_BUFFER_HEADER->out_end; |
108 | uint32_t end = SGCN_BUFFER_HEADER->out_end; |
109 | uint32_t size = end - begin; |
109 | uint32_t size = end - begin; |
110 | 110 | ||
111 | /* we need pointers to volatile variables */ |
111 | /* we need pointers to volatile variables */ |
112 | volatile char *buf_ptr = (volatile char *) |
112 | volatile char *buf_ptr = (volatile char *) |
113 | SGCN_BUFFER(char, SGCN_BUFFER_HEADER->out_wrptr); |
113 | SGCN_BUFFER(char, SGCN_BUFFER_HEADER->out_wrptr); |
114 | volatile uint32_t *out_wrptr_ptr = &(SGCN_BUFFER_HEADER->out_wrptr); |
114 | volatile uint32_t *out_wrptr_ptr = &(SGCN_BUFFER_HEADER->out_wrptr); |
115 | volatile uint32_t *out_rdptr_ptr = &(SGCN_BUFFER_HEADER->out_rdptr); |
115 | volatile uint32_t *out_rdptr_ptr = &(SGCN_BUFFER_HEADER->out_rdptr); |
116 | 116 | ||
117 | uint32_t new_wrptr = (((*out_wrptr_ptr) - begin + 1) % size) + begin; |
117 | uint32_t new_wrptr = (((*out_wrptr_ptr) - begin + 1) % size) + begin; |
118 | while (*out_rdptr_ptr == new_wrptr) |
118 | while (*out_rdptr_ptr == new_wrptr) |
119 | ; |
119 | ; |
120 | *buf_ptr = c; |
120 | *buf_ptr = c; |
121 | *out_wrptr_ptr = new_wrptr; |
121 | *out_wrptr_ptr = new_wrptr; |
122 | } |
122 | } |
123 | 123 | ||
124 | /** |
124 | /** |
125 | * Main function of the thread serving client connections. |
125 | * Main function of the thread serving client connections. |
126 | */ |
126 | */ |
127 | static void sgcn_client_connection(ipc_callid_t iid, ipc_call_t *icall) |
127 | static void sgcn_client_connection(ipc_callid_t iid, ipc_call_t *icall) |
128 | { |
128 | { |
129 | int retval; |
129 | int retval; |
130 | ipc_callid_t callid; |
130 | ipc_callid_t callid; |
131 | ipc_call_t call; |
131 | ipc_call_t call; |
132 | char c; |
132 | char c; |
133 | int lastcol = 0; |
133 | int lastcol = 0; |
134 | int lastrow = 0; |
134 | int lastrow = 0; |
135 | int newcol; |
135 | int newcol; |
136 | int newrow; |
136 | int newrow; |
137 | int fgcolor; |
137 | int fgcolor; |
138 | int bgcolor; |
138 | int bgcolor; |
139 | int i; |
139 | int i; |
140 | 140 | ||
141 | if (client_connected) { |
141 | if (client_connected) { |
142 | ipc_answer_0(iid, ELIMIT); |
142 | ipc_answer_0(iid, ELIMIT); |
143 | return; |
143 | return; |
144 | } |
144 | } |
145 | 145 | ||
146 | client_connected = 1; |
146 | client_connected = 1; |
147 | ipc_answer_0(iid, EOK); |
147 | ipc_answer_0(iid, EOK); |
148 | 148 | ||
149 | /* Clear the terminal, set scrolling region |
149 | /* Clear the terminal, set scrolling region |
150 | to 0 - 24 lines */ |
150 | to 0 - 24 lines */ |
151 | serial_clrscr(); |
151 | serial_clrscr(); |
152 | serial_goto(0, 0); |
152 | serial_goto(0, 0); |
153 | serial_puts("\033[0;24r"); |
153 | serial_puts("\033[0;24r"); |
154 | 154 | ||
155 | while (true) { |
155 | while (true) { |
156 | callid = async_get_call(&call); |
156 | callid = async_get_call(&call); |
157 | switch (IPC_GET_METHOD(call)) { |
157 | switch (IPC_GET_METHOD(call)) { |
158 | case IPC_M_PHONE_HUNGUP: |
158 | case IPC_M_PHONE_HUNGUP: |
159 | client_connected = 0; |
159 | client_connected = 0; |
160 | ipc_answer_0(callid, EOK); |
160 | ipc_answer_0(callid, EOK); |
161 | return; |
161 | return; |
162 | case FB_PUTCHAR: |
162 | case FB_PUTCHAR: |
163 | c = IPC_GET_ARG1(call); |
163 | c = IPC_GET_ARG1(call); |
164 | newrow = IPC_GET_ARG2(call); |
164 | newrow = IPC_GET_ARG2(call); |
165 | newcol = IPC_GET_ARG3(call); |
165 | newcol = IPC_GET_ARG3(call); |
166 | if ((lastcol != newcol) || (lastrow != newrow)) |
166 | if ((lastcol != newcol) || (lastrow != newrow)) |
167 | serial_goto(newrow, newcol); |
167 | serial_goto(newrow, newcol); |
168 | lastcol = newcol + 1; |
168 | lastcol = newcol + 1; |
169 | lastrow = newrow; |
169 | lastrow = newrow; |
170 | sgcn_putc(c); |
170 | sgcn_putc(c); |
171 | retval = 0; |
171 | retval = 0; |
172 | break; |
172 | break; |
173 | case FB_CURSOR_GOTO: |
173 | case FB_CURSOR_GOTO: |
174 | newrow = IPC_GET_ARG1(call); |
174 | newrow = IPC_GET_ARG1(call); |
175 | newcol = IPC_GET_ARG2(call); |
175 | newcol = IPC_GET_ARG2(call); |
176 | serial_goto(newrow, newcol); |
176 | serial_goto(newrow, newcol); |
177 | lastrow = newrow; |
177 | lastrow = newrow; |
178 | lastcol = newcol; |
178 | lastcol = newcol; |
179 | retval = 0; |
179 | retval = 0; |
180 | break; |
180 | break; |
181 | case FB_GET_CSIZE: |
181 | case FB_GET_CSIZE: |
182 | ipc_answer_2(callid, EOK, HEIGHT, WIDTH); |
182 | ipc_answer_2(callid, EOK, HEIGHT, WIDTH); |
183 | continue; |
183 | continue; |
184 | case FB_CLEAR: |
184 | case FB_CLEAR: |
185 | serial_clrscr(); |
185 | serial_clrscr(); |
186 | retval = 0; |
186 | retval = 0; |
187 | break; |
187 | break; |
188 | case FB_SET_STYLE: |
188 | case FB_SET_STYLE: |
189 | fgcolor = IPC_GET_ARG1(call); |
189 | fgcolor = IPC_GET_ARG1(call); |
190 | bgcolor = IPC_GET_ARG2(call); |
190 | bgcolor = IPC_GET_ARG2(call); |
191 | if (fgcolor < bgcolor) |
191 | if (fgcolor < bgcolor) |
192 | serial_set_style(0); |
192 | serial_set_style(0); |
193 | else |
193 | else |
194 | serial_set_style(7); |
194 | serial_set_style(7); |
195 | retval = 0; |
195 | retval = 0; |
196 | break; |
196 | break; |
197 | case FB_SCROLL: |
197 | case FB_SCROLL: |
198 | i = IPC_GET_ARG1(call); |
198 | i = IPC_GET_ARG1(call); |
199 | if ((i > HEIGHT) || (i < -HEIGHT)) { |
199 | if ((i > HEIGHT) || (i < -HEIGHT)) { |
200 | retval = EINVAL; |
200 | retval = EINVAL; |
201 | break; |
201 | break; |
202 | } |
202 | } |
203 | serial_scroll(i); |
203 | serial_scroll(i); |
204 | serial_goto(lastrow, lastcol); |
204 | serial_goto(lastrow, lastcol); |
205 | retval = 0; |
205 | retval = 0; |
206 | break; |
206 | break; |
207 | case FB_CURSOR_VISIBILITY: |
207 | case FB_CURSOR_VISIBILITY: |
208 | if(IPC_GET_ARG1(call)) |
208 | if(IPC_GET_ARG1(call)) |
209 | serial_cursor_enable(); |
209 | serial_cursor_enable(); |
210 | else |
210 | else |
211 | serial_cursor_disable(); |
211 | serial_cursor_disable(); |
212 | retval = 0; |
212 | retval = 0; |
213 | break; |
213 | break; |
214 | default: |
214 | default: |
215 | retval = ENOENT; |
215 | retval = ENOENT; |
216 | } |
216 | } |
217 | ipc_answer_0(callid, retval); |
217 | ipc_answer_0(callid, retval); |
218 | } |
218 | } |
219 | } |
219 | } |
220 | 220 | ||
221 | /** |
221 | /** |
222 | * Initializes the SGCN serial driver. |
222 | * Initializes the SGCN serial driver. |
223 | */ |
223 | */ |
224 | int sgcn_init(void) |
224 | int sgcn_init(void) |
225 | { |
225 | { |
226 | sram_virt_addr = (uintptr_t) as_get_mappable_page( |
226 | sram_virt_addr = (uintptr_t) as_get_mappable_page( |
227 | sysinfo_value("sram.area.size")); |
227 | sysinfo_value("sram.area.size")); |
228 | int result = physmem_map( |
228 | int result = physmem_map( |
229 | (void *) sysinfo_value("sram.address.physical"), |
229 | (void *) sysinfo_value("sram.address.physical"), |
230 | (void *) sram_virt_addr, |
230 | (void *) sram_virt_addr, |
231 | sysinfo_value("sram.area.size") / PAGE_SIZE, |
231 | sysinfo_value("sram.area.size") / PAGE_SIZE, |
232 | AS_AREA_READ | AS_AREA_WRITE |
232 | AS_AREA_READ | AS_AREA_WRITE |
233 | ); |
233 | ); |
234 | if (result != 0) { |
234 | if (result != 0) { |
235 | printf("SGCN: uspace driver couldn't map physical memory: %d\n", |
235 | printf("SGCN: uspace driver couldn't map physical memory: %d\n", |
236 | result); |
236 | result); |
237 | } |
237 | } |
238 | 238 | ||
239 | serial_console_init(sgcn_putc, WIDTH, HEIGHT); |
239 | serial_console_init(sgcn_putc, WIDTH, HEIGHT); |
240 | 240 | ||
241 | sram_buffer_offset = sysinfo_value("sram.buffer.offset"); |
241 | sram_buffer_offset = sysinfo_value("sram.buffer.offset"); |
242 | 242 | ||
243 | async_set_client_connection(sgcn_client_connection); |
243 | async_set_client_connection(sgcn_client_connection); |
244 | return 0; |
244 | return 0; |
245 | } |
245 | } |
246 | 246 | ||
247 | /** |
247 | /** |
248 | * @} |
248 | * @} |
249 | */ |
249 | */ |
250 | 250 | ||
251 | 251 |