source: mainline/uspace/drv/char/ski-con/ski-con.c@ f4cfd271

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f4cfd271 was 5f4c41b2, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Move sending side of Sun4v console out of output server.

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * Copyright (c) 2005 Jakub Jermar
3 * Copyright (c) 2017 Jiri Svoboda
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
30/** @file Ski console driver.
31 */
32
33#include <ddf/driver.h>
34#include <ddf/log.h>
35#include <errno.h>
36#include <fibril.h>
37#include <io/chardev.h>
38#include <stdint.h>
39#include <stdlib.h>
40#include <stdbool.h>
41
42#include "ski-con.h"
43
44#define SKI_GETCHAR 21
45#define SKI_PUTCHAR 31
46
47#define POLL_INTERVAL 10000
48
49static int ski_con_fibril(void *arg);
50static int32_t ski_con_getchar(void);
51static void ski_con_connection(ipc_callid_t, ipc_call_t *, void *);
52
53static int ski_con_read(chardev_srv_t *, void *, size_t, size_t *);
54static int ski_con_write(chardev_srv_t *, const void *, size_t, size_t *);
55
56static chardev_ops_t ski_con_chardev_ops = {
57 .read = ski_con_read,
58 .write = ski_con_write
59};
60
61static void ski_con_putchar(ski_con_t *con, char ch); /* XXX */
62
63/** Add ski console device. */
64int ski_con_add(ski_con_t *con)
65{
66 fid_t fid;
67 ddf_fun_t *fun = NULL;
68 bool bound = false;
69 int rc;
70
71 circ_buf_init(&con->cbuf, con->buf, ski_con_buf_size, 1);
72 fibril_mutex_initialize(&con->buf_lock);
73 fibril_condvar_initialize(&con->buf_cv);
74
75 fun = ddf_fun_create(con->dev, fun_exposed, "a");
76 if (fun == NULL) {
77 ddf_msg(LVL_ERROR, "Error creating function 'a'.");
78 rc = ENOMEM;
79 goto error;
80 }
81
82 ddf_fun_set_conn_handler(fun, ski_con_connection);
83
84 chardev_srvs_init(&con->cds);
85 con->cds.ops = &ski_con_chardev_ops;
86 con->cds.sarg = con;
87
88 rc = ddf_fun_bind(fun);
89 if (rc != EOK) {
90 ddf_msg(LVL_ERROR, "Error binding function 'a'.");
91 goto error;
92 }
93
94 ddf_fun_add_to_category(fun, "console");
95
96 bound = true;
97
98 fid = fibril_create(ski_con_fibril, con);
99 if (fid == 0) {
100 ddf_msg(LVL_ERROR, "Error creating fibril.");
101 rc = ENOMEM;
102 goto error;
103 }
104
105 fibril_add_ready(fid);
106 return EOK;
107error:
108 if (bound)
109 ddf_fun_unbind(fun);
110 if (fun != NULL)
111 ddf_fun_destroy(fun);
112
113 return rc;
114}
115
116/** Remove ski console device */
117int ski_con_remove(ski_con_t *con)
118{
119 return ENOTSUP;
120}
121
122/** Ski console device gone */
123int ski_con_gone(ski_con_t *con)
124{
125 return ENOTSUP;
126}
127
128/** Poll Ski for keypresses. */
129static int ski_con_fibril(void *arg)
130{
131 int32_t c;
132 ski_con_t *con = (ski_con_t *) arg;
133 int rc;
134
135 while (1) {
136 while (1) {
137 c = ski_con_getchar();
138 if (c == 0)
139 break;
140
141 fibril_mutex_lock(&con->buf_lock);
142
143 rc = circ_buf_push(&con->cbuf, &c);
144 if (rc != EOK)
145 ddf_msg(LVL_ERROR, "Buffer overrun");
146
147 fibril_mutex_unlock(&con->buf_lock);
148 fibril_condvar_broadcast(&con->buf_cv);
149 }
150
151 fibril_usleep(POLL_INTERVAL);
152 }
153
154 return 0;
155}
156
157/** Ask Ski if a key was pressed.
158 *
159 * Use SSC (Simulator System Call) to get character from the debug console.
160 * This call is non-blocking.
161 *
162 * @return ASCII code of pressed key or 0 if no key pressed.
163 */
164static int32_t ski_con_getchar(void)
165{
166 uint64_t ch;
167
168#ifdef UARCH_ia64
169 asm volatile (
170 "mov r15 = %1\n"
171 "break 0x80000;;\n" /* modifies r8 */
172 "mov %0 = r8;;\n"
173
174 : "=r" (ch)
175 : "i" (SKI_GETCHAR)
176 : "r15", "r8"
177 );
178#else
179 ch = 0;
180#endif
181 return (int32_t) ch;
182}
183
184
185/** Display character on ski debug console
186 *
187 * Use SSC (Simulator System Call) to
188 * display character on debug console.
189 *
190 * @param c Character to be printed.
191 *
192 */
193static void ski_con_putchar(ski_con_t *con, char ch)
194{
195 if (ch == '\n')
196 ski_con_putchar(con, '\r');
197
198#ifdef UARCH_ia64
199 asm volatile (
200 "mov r15 = %0\n"
201 "mov r32 = %1\n" /* r32 is in0 */
202 "break 0x80000\n" /* modifies r8 */
203 :
204 : "i" (SKI_PUTCHAR), "r" (ch)
205 : "r15", "in0", "r8"
206 );
207#else
208 (void) ch;
209#endif
210}
211
212/** Read from Ski console device */
213static int ski_con_read(chardev_srv_t *srv, void *buf, size_t size,
214 size_t *nread)
215{
216 ski_con_t *con = (ski_con_t *) srv->srvs->sarg;
217 size_t p;
218 uint8_t *bp = (uint8_t *) buf;
219 int rc;
220
221 fibril_mutex_lock(&con->buf_lock);
222
223 while (circ_buf_nused(&con->cbuf) == 0)
224 fibril_condvar_wait(&con->buf_cv, &con->buf_lock);
225
226 p = 0;
227 while (p < size) {
228 rc = circ_buf_pop(&con->cbuf, &bp[p]);
229 if (rc != EOK)
230 break;
231 ++p;
232 }
233
234 fibril_mutex_unlock(&con->buf_lock);
235
236 *nread = p;
237 return EOK;
238}
239
240/** Write to Ski console device */
241static int ski_con_write(chardev_srv_t *srv, const void *data, size_t size,
242 size_t *nwr)
243{
244 ski_con_t *con = (ski_con_t *) srv->srvs->sarg;
245 size_t i;
246 uint8_t *dp = (uint8_t *) data;
247
248 for (i = 0; i < size; i++)
249 ski_con_putchar(con, dp[i]);
250
251 *nwr = size;
252 return EOK;
253}
254
255/** Character device connection handler. */
256static void ski_con_connection(ipc_callid_t iid, ipc_call_t *icall,
257 void *arg)
258{
259 ski_con_t *con = (ski_con_t *) ddf_dev_data_get(
260 ddf_fun_get_dev((ddf_fun_t *) arg));
261
262 chardev_conn(iid, icall, &con->cds);
263}
264
265/** @}
266 */
Note: See TracBrowser for help on using the repository browser.