| 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 |
|
|---|
| 50 | static errno_t ski_con_fibril(void *arg);
|
|---|
| 51 | static int32_t ski_con_getchar(void);
|
|---|
| 52 | static void ski_con_connection(cap_call_handle_t, ipc_call_t *, void *);
|
|---|
| 53 |
|
|---|
| 54 | static errno_t ski_con_read(chardev_srv_t *, void *, size_t, size_t *);
|
|---|
| 55 | static errno_t ski_con_write(chardev_srv_t *, const void *, size_t, size_t *);
|
|---|
| 56 |
|
|---|
| 57 | static chardev_ops_t ski_con_chardev_ops = {
|
|---|
| 58 | .read = ski_con_read,
|
|---|
| 59 | .write = ski_con_write
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | static void ski_con_putchar(ski_con_t *con, char ch); /* XXX */
|
|---|
| 63 |
|
|---|
| 64 | /** Add ski console device. */
|
|---|
| 65 | errno_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;
|
|---|
| 108 | error:
|
|---|
| 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 */
|
|---|
| 118 | errno_t ski_con_remove(ski_con_t *con)
|
|---|
| 119 | {
|
|---|
| 120 | return ENOTSUP;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /** Ski console device gone */
|
|---|
| 124 | errno_t ski_con_gone(ski_con_t *con)
|
|---|
| 125 | {
|
|---|
| 126 | return ENOTSUP;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /** Poll Ski for keypresses. */
|
|---|
| 130 | static 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 | async_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 | */
|
|---|
| 165 | static 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 |
|
|---|
| 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 | */
|
|---|
| 194 | static void ski_con_putchar(ski_con_t *con, char ch)
|
|---|
| 195 | {
|
|---|
| 196 | if (ch == '\n')
|
|---|
| 197 | ski_con_putchar(con, '\r');
|
|---|
| 198 |
|
|---|
| 199 | #ifdef UARCH_ia64
|
|---|
| 200 | asm volatile (
|
|---|
| 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"
|
|---|
| 207 | );
|
|---|
| 208 | #else
|
|---|
| 209 | (void) ch;
|
|---|
| 210 | #endif
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /** Read from Ski console device */
|
|---|
| 214 | static errno_t ski_con_read(chardev_srv_t *srv, void *buf, size_t size,
|
|---|
| 215 | size_t *nread)
|
|---|
| 216 | {
|
|---|
| 217 | ski_con_t *con = (ski_con_t *) srv->srvs->sarg;
|
|---|
| 218 | size_t p;
|
|---|
| 219 | uint8_t *bp = (uint8_t *) buf;
|
|---|
| 220 | errno_t rc;
|
|---|
| 221 |
|
|---|
| 222 | fibril_mutex_lock(&con->buf_lock);
|
|---|
| 223 |
|
|---|
| 224 | while (circ_buf_nused(&con->cbuf) == 0)
|
|---|
| 225 | fibril_condvar_wait(&con->buf_cv, &con->buf_lock);
|
|---|
| 226 |
|
|---|
| 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 | }
|
|---|
| 234 |
|
|---|
| 235 | fibril_mutex_unlock(&con->buf_lock);
|
|---|
| 236 |
|
|---|
| 237 | *nread = p;
|
|---|
| 238 | return EOK;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | /** Write to Ski console device */
|
|---|
| 242 | static errno_t ski_con_write(chardev_srv_t *srv, const void *data, size_t size,
|
|---|
| 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++)
|
|---|
| 250 | ski_con_putchar(con, dp[i]);
|
|---|
| 251 |
|
|---|
| 252 | *nwr = size;
|
|---|
| 253 | return EOK;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /** Character device connection handler. */
|
|---|
| 257 | static void ski_con_connection(cap_call_handle_t icall_handle, ipc_call_t *icall,
|
|---|
| 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 |
|
|---|
| 263 | chardev_conn(icall_handle, icall, &con->cds);
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | /** @}
|
|---|
| 267 | */
|
|---|