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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 184f2f8a was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 6.0 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 <async.h>
34#include <ddf/driver.h>
35#include <ddf/log.h>
36#include <errno.h>
37#include <fibril.h>
38#include <io/chardev.h>
39#include <stdint.h>
40#include <stdlib.h>
41#include <stdbool.h>
42
43#include "ski-con.h"
44
45#define SKI_GETCHAR 21
46#define SKI_PUTCHAR 31
47
48#define POLL_INTERVAL 10000
49
50static errno_t ski_con_fibril(void *arg);
51static int32_t ski_con_getchar(void);
52static void ski_con_connection(ipc_call_t *, void *);
53
54static errno_t ski_con_read(chardev_srv_t *, void *, size_t, size_t *);
55static errno_t ski_con_write(chardev_srv_t *, const void *, size_t, size_t *);
56
57static chardev_ops_t ski_con_chardev_ops = {
58 .read = ski_con_read,
59 .write = ski_con_write
60};
61
62static void ski_con_putchar(ski_con_t *con, char ch); /* XXX */
63
64/** Add ski console device. */
65errno_t ski_con_add(ski_con_t *con)
66{
67 fid_t fid;
68 ddf_fun_t *fun = NULL;
69 bool bound = false;
70 errno_t rc;
71
72 circ_buf_init(&con->cbuf, con->buf, ski_con_buf_size, 1);
73 fibril_mutex_initialize(&con->buf_lock);
74 fibril_condvar_initialize(&con->buf_cv);
75
76 fun = ddf_fun_create(con->dev, fun_exposed, "a");
77 if (fun == NULL) {
78 ddf_msg(LVL_ERROR, "Error creating function 'a'.");
79 rc = ENOMEM;
80 goto error;
81 }
82
83 ddf_fun_set_conn_handler(fun, ski_con_connection);
84
85 chardev_srvs_init(&con->cds);
86 con->cds.ops = &ski_con_chardev_ops;
87 con->cds.sarg = con;
88
89 rc = ddf_fun_bind(fun);
90 if (rc != EOK) {
91 ddf_msg(LVL_ERROR, "Error binding function 'a'.");
92 goto error;
93 }
94
95 ddf_fun_add_to_category(fun, "console");
96
97 bound = true;
98
99 fid = fibril_create(ski_con_fibril, con);
100 if (fid == 0) {
101 ddf_msg(LVL_ERROR, "Error creating fibril.");
102 rc = ENOMEM;
103 goto error;
104 }
105
106 fibril_add_ready(fid);
107 return EOK;
108error:
109 if (bound)
110 ddf_fun_unbind(fun);
111 if (fun != NULL)
112 ddf_fun_destroy(fun);
113
114 return rc;
115}
116
117/** Remove ski console device */
118errno_t ski_con_remove(ski_con_t *con)
119{
120 return ENOTSUP;
121}
122
123/** Ski console device gone */
124errno_t ski_con_gone(ski_con_t *con)
125{
126 return ENOTSUP;
127}
128
129/** Poll Ski for keypresses. */
130static errno_t ski_con_fibril(void *arg)
131{
132 int32_t c;
133 ski_con_t *con = (ski_con_t *) arg;
134 errno_t rc;
135
136 while (true) {
137 while (true) {
138 c = ski_con_getchar();
139 if (c == 0)
140 break;
141
142 fibril_mutex_lock(&con->buf_lock);
143
144 rc = circ_buf_push(&con->cbuf, &c);
145 if (rc != EOK)
146 ddf_msg(LVL_ERROR, "Buffer overrun");
147
148 fibril_mutex_unlock(&con->buf_lock);
149 fibril_condvar_broadcast(&con->buf_cv);
150 }
151
152 fibril_usleep(POLL_INTERVAL);
153 }
154
155 return 0;
156}
157
158/** Ask Ski if a key was pressed.
159 *
160 * Use SSC (Simulator System Call) to get character from the debug console.
161 * This call is non-blocking.
162 *
163 * @return ASCII code of pressed key or 0 if no key pressed.
164 */
165static int32_t ski_con_getchar(void)
166{
167 uint64_t ch;
168
169#ifdef UARCH_ia64
170 asm volatile (
171 "mov r15 = %1\n"
172 "break 0x80000;;\n" /* modifies r8 */
173 "mov %0 = r8;;\n"
174
175 : "=r" (ch)
176 : "i" (SKI_GETCHAR)
177 : "r15", "r8"
178 );
179#else
180 ch = 0;
181#endif
182 return (int32_t) ch;
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 errno_t 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 errno_t 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 errno_t 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_call_t *icall, void *arg)
257{
258 ski_con_t *con = (ski_con_t *) ddf_dev_data_get(
259 ddf_fun_get_dev((ddf_fun_t *) arg));
260
261 chardev_conn(icall, &con->cds);
262}
263
264/** @}
265 */
Note: See TracBrowser for help on using the repository browser.