Subversion Repositories HelenOS

Rev

Rev 3549 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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