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

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

Use proper boolean constant in while loops.

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[6d15572]1/*
2 * Copyright (c) 2005 Jakub Jermar
[7a6065c]3 * Copyright (c) 2017 Jiri Svoboda
[6d15572]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
[39026d7c]33#include <async.h>
[6d15572]34#include <ddf/driver.h>
35#include <ddf/log.h>
36#include <errno.h>
[7a6065c]37#include <fibril.h>
38#include <io/chardev.h>
[6d15572]39#include <stdint.h>
40#include <stdlib.h>
41#include <stdbool.h>
42
43#include "ski-con.h"
44
45#define SKI_GETCHAR 21
[9940ce0]46#define SKI_PUTCHAR 31
[6d15572]47
48#define POLL_INTERVAL 10000
49
[b7fd2a0]50static errno_t ski_con_fibril(void *arg);
[6d15572]51static int32_t ski_con_getchar(void);
[3be9d10]52static void ski_con_connection(cap_call_handle_t, ipc_call_t *, void *);
[6d15572]53
[b7fd2a0]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 *);
[7a6065c]56
57static chardev_ops_t ski_con_chardev_ops = {
58 .read = ski_con_read,
59 .write = ski_con_write
60};
61
[9940ce0]62static void ski_con_putchar(ski_con_t *con, char ch); /* XXX */
63
[676e833]64/** Add ski console device. */
[b7fd2a0]65errno_t ski_con_add(ski_con_t *con)
[6d15572]66{
[7a6065c]67 fid_t fid;
[6d15572]68 ddf_fun_t *fun = NULL;
69 bool bound = false;
[b7fd2a0]70 errno_t rc;
[6d15572]71
[7a6065c]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
[6d15572]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
[7a6065c]85 chardev_srvs_init(&con->cds);
86 con->cds.ops = &ski_con_chardev_ops;
87 con->cds.sarg = con;
88
[6d15572]89 rc = ddf_fun_bind(fun);
90 if (rc != EOK) {
91 ddf_msg(LVL_ERROR, "Error binding function 'a'.");
92 goto error;
93 }
94
[9940ce0]95 ddf_fun_add_to_category(fun, "console");
96
[6d15572]97 bound = true;
98
[7a6065c]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;
[6d15572]104 }
105
[7a6065c]106 fibril_add_ready(fid);
[6d15572]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 */
[b7fd2a0]118errno_t ski_con_remove(ski_con_t *con)
[6d15572]119{
120 return ENOTSUP;
121}
122
123/** Ski console device gone */
[b7fd2a0]124errno_t ski_con_gone(ski_con_t *con)
[6d15572]125{
126 return ENOTSUP;
127}
128
[7a6065c]129/** Poll Ski for keypresses. */
[b7fd2a0]130static errno_t ski_con_fibril(void *arg)
[6d15572]131{
132 int32_t c;
133 ski_con_t *con = (ski_con_t *) arg;
[b7fd2a0]134 errno_t rc;
[6d15572]135
[76d0981d]136 while (true) {
137 while (true) {
[6d15572]138 c = ski_con_getchar();
139 if (c == 0)
140 break;
141
[7a6065c]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);
[6d15572]150 }
151
[39026d7c]152 async_usleep(POLL_INTERVAL);
[6d15572]153 }
[7a6065c]154
155 return 0;
[6d15572]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 (
[1433ecda]171 "mov r15 = %1\n"
172 "break 0x80000;;\n" /* modifies r8 */
173 "mov %0 = r8;;\n"
[6d15572]174
[1433ecda]175 : "=r" (ch)
176 : "i" (SKI_GETCHAR)
177 : "r15", "r8"
[6d15572]178 );
179#else
180 ch = 0;
181#endif
182 return (int32_t) ch;
183}
184
[9940ce0]185
186/** Display character on ski debug console
187 *
188 * Use SSC (Simulator System Call) to
189 * display character on debug console.
190 *
191 * @param c Character to be printed.
192 *
193 */
[6d15572]194static void ski_con_putchar(ski_con_t *con, char ch)
195{
[5f4c41b2]196 if (ch == '\n')
197 ski_con_putchar(con, '\r');
198
[9940ce0]199#ifdef UARCH_ia64
200 asm volatile (
[1433ecda]201 "mov r15 = %0\n"
202 "mov r32 = %1\n" /* r32 is in0 */
203 "break 0x80000\n" /* modifies r8 */
204 :
205 : "i" (SKI_PUTCHAR), "r" (ch)
206 : "r15", "in0", "r8"
[9940ce0]207 );
208#else
209 (void) ch;
210#endif
[6d15572]211}
212
[7a6065c]213/** Read from Ski console device */
[b7fd2a0]214static errno_t ski_con_read(chardev_srv_t *srv, void *buf, size_t size,
[7a6065c]215 size_t *nread)
[6d15572]216{
[7a6065c]217 ski_con_t *con = (ski_con_t *) srv->srvs->sarg;
218 size_t p;
219 uint8_t *bp = (uint8_t *) buf;
[b7fd2a0]220 errno_t rc;
[6d15572]221
[7a6065c]222 fibril_mutex_lock(&con->buf_lock);
[6d15572]223
[7a6065c]224 while (circ_buf_nused(&con->cbuf) == 0)
225 fibril_condvar_wait(&con->buf_cv, &con->buf_lock);
[6d15572]226
[7a6065c]227 p = 0;
228 while (p < size) {
229 rc = circ_buf_pop(&con->cbuf, &bp[p]);
230 if (rc != EOK)
231 break;
232 ++p;
233 }
[6d15572]234
[7a6065c]235 fibril_mutex_unlock(&con->buf_lock);
[6d15572]236
[7a6065c]237 *nread = p;
238 return EOK;
239}
240
241/** Write to Ski console device */
[b7fd2a0]242static errno_t ski_con_write(chardev_srv_t *srv, const void *data, size_t size,
[7a6065c]243 size_t *nwr)
244{
245 ski_con_t *con = (ski_con_t *) srv->srvs->sarg;
246 size_t i;
247 uint8_t *dp = (uint8_t *) data;
248
249 for (i = 0; i < size; i++)
[1b20da0]250 ski_con_putchar(con, dp[i]);
[7a6065c]251
252 *nwr = size;
253 return EOK;
254}
255
256/** Character device connection handler. */
[a46e56b]257static void ski_con_connection(cap_call_handle_t icall_handle, ipc_call_t *icall,
[7a6065c]258 void *arg)
259{
260 ski_con_t *con = (ski_con_t *) ddf_dev_data_get(
261 ddf_fun_get_dev((ddf_fun_t *) arg));
262
[a46e56b]263 chardev_conn(icall_handle, icall, &con->cds);
[6d15572]264}
265
266/** @}
267 */
Note: See TracBrowser for help on using the repository browser.